1 /* $NetBSD: ddp_output.c,v 1.4 2001/11/15 09:48:26 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1990,1991 Regents of The University of Michigan. 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify, and distribute this software and 8 * its documentation for any purpose and without fee is hereby granted, 9 * provided that the above copyright notice appears in all copies and 10 * that both that copyright notice and this permission notice appear 11 * in supporting documentation, and that the name of The University 12 * of Michigan not be used in advertising or publicity pertaining to 13 * distribution of the software without specific, written prior 14 * permission. This software is supplied as is without expressed or 15 * implied warranties of any kind. 16 * 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 20 * Research Systems Unix Group 21 * The University of Michigan 22 * c/o Wesley Craig 23 * 535 W. William Street 24 * Ann Arbor, Michigan 25 * +1-313-764-2278 26 * netatalk@umich.edu 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: ddp_output.c,v 1.4 2001/11/15 09:48:26 lukem Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/mbuf.h> 35 #include <sys/socket.h> 36 #include <sys/errno.h> 37 #include <sys/syslog.h> 38 39 #include <net/if.h> 40 #include <net/route.h> 41 #include <net/if_ether.h> 42 43 #include <netinet/in.h> 44 #undef s_net 45 46 #include <netatalk/at.h> 47 #include <netatalk/at_var.h> 48 #include <netatalk/ddp.h> 49 #include <netatalk/ddp_var.h> 50 #include <netatalk/at_extern.h> 51 52 #include <machine/stdarg.h> 53 54 int ddp_cksum = 1; 55 56 int 57 #if __STDC__ 58 ddp_output(struct mbuf *m,...) 59 #else 60 ddp_output(va_alist) 61 va_dcl 62 #endif 63 { 64 struct ddpcb *ddp; 65 struct ddpehdr *deh; 66 va_list ap; 67 68 #if __STDC__ 69 va_start(ap, m); 70 #else 71 struct mbuf *m; 72 73 va_start(ap); 74 m = va_arg(ap, struct mbuf *); 75 #endif 76 ddp = va_arg(ap, struct ddpcb *); 77 va_end(ap); 78 79 M_PREPEND(m, sizeof(struct ddpehdr), M_WAIT); 80 81 deh = mtod(m, struct ddpehdr *); 82 deh->deh_pad = 0; 83 deh->deh_hops = 0; 84 85 deh->deh_len = m->m_pkthdr.len; 86 87 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net; 88 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node; 89 deh->deh_dport = ddp->ddp_fsat.sat_port; 90 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net; 91 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node; 92 deh->deh_sport = ddp->ddp_lsat.sat_port; 93 94 /* 95 * The checksum calculation is done after all of the other bytes have 96 * been filled in. 97 */ 98 if (ddp_cksum) { 99 deh->deh_sum = at_cksum(m, sizeof(int)); 100 } else { 101 deh->deh_sum = 0; 102 } 103 deh->deh_bytes = htonl(deh->deh_bytes); 104 105 return (ddp_route(m, &ddp->ddp_route)); 106 } 107 108 u_short 109 at_cksum(m, skip) 110 struct mbuf *m; 111 int skip; 112 { 113 u_char *data, *end; 114 u_long cksum = 0; 115 116 for (; m; m = m->m_next) { 117 for (data = mtod(m, u_char *), end = data + m->m_len; 118 data < end; data++) { 119 if (skip) { 120 skip--; 121 continue; 122 } 123 cksum = (cksum + *data) << 1; 124 if (cksum & 0x00010000) { 125 cksum++; 126 } 127 cksum &= 0x0000ffff; 128 } 129 } 130 131 if (cksum == 0) { 132 cksum = 0x0000ffff; 133 } 134 return ((u_short) cksum); 135 } 136 137 int 138 ddp_route(m, ro) 139 struct mbuf *m; 140 struct route *ro; 141 { 142 struct sockaddr_at gate; 143 struct elaphdr *elh; 144 struct mbuf *m0; 145 struct at_ifaddr *aa = NULL; 146 struct ifnet *ifp = NULL; 147 u_short net; 148 149 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) { 150 net = satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net; 151 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 152 if (aa->aa_ifp == ifp && 153 ntohs(net) >= ntohs(aa->aa_firstnet) && 154 ntohs(net) <= ntohs(aa->aa_lastnet)) { 155 break; 156 } 157 } 158 } 159 if (aa == NULL) { 160 printf("ddp_route: oops\n"); 161 m_freem(m); 162 return (EINVAL); 163 } 164 /* 165 * There are several places in the kernel where data is added to 166 * an mbuf without ensuring that the mbuf pointer is aligned. 167 * This is bad for transition routing, since phase 1 and phase 2 168 * packets end up poorly aligned due to the three byte elap header. 169 */ 170 if (!(aa->aa_flags & AFA_PHASE2)) { 171 MGET(m0, M_WAIT, MT_HEADER); 172 if (m0 == 0) { 173 m_freem(m); 174 printf("ddp_route: no buffers\n"); 175 return (ENOBUFS); 176 } 177 m0->m_next = m; 178 /* XXX perhaps we ought to align the header? */ 179 m0->m_len = SZ_ELAPHDR; 180 m = m0; 181 182 elh = mtod(m, struct elaphdr *); 183 elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node; 184 elh->el_type = ELAP_DDPEXTEND; 185 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 186 ntohs(aa->aa_firstnet) && 187 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 188 ntohs(aa->aa_lastnet)) { 189 elh->el_dnode = satosat(&ro->ro_dst)->sat_addr.s_node; 190 } else { 191 elh->el_dnode = 192 satosat(ro->ro_rt->rt_gateway)->sat_addr.s_node; 193 } 194 } 195 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 196 ntohs(aa->aa_firstnet) && 197 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 198 ntohs(aa->aa_lastnet)) { 199 gate = *satosat(&ro->ro_dst); 200 } else { 201 gate = *satosat(ro->ro_rt->rt_gateway); 202 } 203 ro->ro_rt->rt_use++; 204 205 #if IFA_STATS 206 aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; 207 #endif 208 209 /* XXX */ 210 return ((*ifp->if_output) (ifp, m, (struct sockaddr *) &gate, NULL)); 211 } 212