1 /* $NetBSD: ddp_output.c,v 1.3 2001/11/13 00:00:58 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.3 2001/11/13 00:00:58 lukem Exp $"); 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/mbuf.h> 36 #include <sys/socket.h> 37 #include <sys/errno.h> 38 #include <sys/syslog.h> 39 40 #include <net/if.h> 41 #include <net/route.h> 42 #include <net/if_ether.h> 43 44 #include <netinet/in.h> 45 #undef s_net 46 47 #include <netatalk/at.h> 48 #include <netatalk/at_var.h> 49 #include <netatalk/ddp.h> 50 #include <netatalk/ddp_var.h> 51 #include <netatalk/at_extern.h> 52 53 #include <machine/stdarg.h> 54 55 int ddp_cksum = 1; 56 57 int 58 #if __STDC__ 59 ddp_output(struct mbuf *m,...) 60 #else 61 ddp_output(va_alist) 62 va_dcl 63 #endif 64 { 65 struct ddpcb *ddp; 66 struct ddpehdr *deh; 67 va_list ap; 68 69 #if __STDC__ 70 va_start(ap, m); 71 #else 72 struct mbuf *m; 73 74 va_start(ap); 75 m = va_arg(ap, struct mbuf *); 76 #endif 77 ddp = va_arg(ap, struct ddpcb *); 78 va_end(ap); 79 80 M_PREPEND(m, sizeof(struct ddpehdr), M_WAIT); 81 82 deh = mtod(m, struct ddpehdr *); 83 deh->deh_pad = 0; 84 deh->deh_hops = 0; 85 86 deh->deh_len = m->m_pkthdr.len; 87 88 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net; 89 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node; 90 deh->deh_dport = ddp->ddp_fsat.sat_port; 91 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net; 92 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node; 93 deh->deh_sport = ddp->ddp_lsat.sat_port; 94 95 /* 96 * The checksum calculation is done after all of the other bytes have 97 * been filled in. 98 */ 99 if (ddp_cksum) { 100 deh->deh_sum = at_cksum(m, sizeof(int)); 101 } else { 102 deh->deh_sum = 0; 103 } 104 deh->deh_bytes = htonl(deh->deh_bytes); 105 106 return (ddp_route(m, &ddp->ddp_route)); 107 } 108 109 u_short 110 at_cksum(m, skip) 111 struct mbuf *m; 112 int skip; 113 { 114 u_char *data, *end; 115 u_long cksum = 0; 116 117 for (; m; m = m->m_next) { 118 for (data = mtod(m, u_char *), end = data + m->m_len; 119 data < end; data++) { 120 if (skip) { 121 skip--; 122 continue; 123 } 124 cksum = (cksum + *data) << 1; 125 if (cksum & 0x00010000) { 126 cksum++; 127 } 128 cksum &= 0x0000ffff; 129 } 130 } 131 132 if (cksum == 0) { 133 cksum = 0x0000ffff; 134 } 135 return ((u_short) cksum); 136 } 137 138 int 139 ddp_route(m, ro) 140 struct mbuf *m; 141 struct route *ro; 142 { 143 struct sockaddr_at gate; 144 struct elaphdr *elh; 145 struct mbuf *m0; 146 struct at_ifaddr *aa = NULL; 147 struct ifnet *ifp = NULL; 148 u_short net; 149 150 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) { 151 net = satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net; 152 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 153 if (aa->aa_ifp == ifp && 154 ntohs(net) >= ntohs(aa->aa_firstnet) && 155 ntohs(net) <= ntohs(aa->aa_lastnet)) { 156 break; 157 } 158 } 159 } 160 if (aa == NULL) { 161 printf("ddp_route: oops\n"); 162 m_freem(m); 163 return (EINVAL); 164 } 165 /* 166 * There are several places in the kernel where data is added to 167 * an mbuf without ensuring that the mbuf pointer is aligned. 168 * This is bad for transition routing, since phase 1 and phase 2 169 * packets end up poorly aligned due to the three byte elap header. 170 */ 171 if (!(aa->aa_flags & AFA_PHASE2)) { 172 MGET(m0, M_WAIT, MT_HEADER); 173 if (m0 == 0) { 174 m_freem(m); 175 printf("ddp_route: no buffers\n"); 176 return (ENOBUFS); 177 } 178 m0->m_next = m; 179 /* XXX perhaps we ought to align the header? */ 180 m0->m_len = SZ_ELAPHDR; 181 m = m0; 182 183 elh = mtod(m, struct elaphdr *); 184 elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node; 185 elh->el_type = ELAP_DDPEXTEND; 186 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 187 ntohs(aa->aa_firstnet) && 188 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 189 ntohs(aa->aa_lastnet)) { 190 elh->el_dnode = satosat(&ro->ro_dst)->sat_addr.s_node; 191 } else { 192 elh->el_dnode = 193 satosat(ro->ro_rt->rt_gateway)->sat_addr.s_node; 194 } 195 } 196 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 197 ntohs(aa->aa_firstnet) && 198 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 199 ntohs(aa->aa_lastnet)) { 200 gate = *satosat(&ro->ro_dst); 201 } else { 202 gate = *satosat(ro->ro_rt->rt_gateway); 203 } 204 ro->ro_rt->rt_use++; 205 206 #if IFA_STATS 207 aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; 208 #endif 209 210 /* XXX */ 211 return ((*ifp->if_output) (ifp, m, (struct sockaddr *) &gate, NULL)); 212 } 213