1 /* $NetBSD: ddp_output.c,v 1.7 2003/05/27 22:27:21 itojun 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.7 2003/05/27 22:27:21 itojun 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_DONTWAIT); 80 if (!m) 81 return (ENOBUFS); 82 83 deh = mtod(m, struct ddpehdr *); 84 deh->deh_pad = 0; 85 deh->deh_hops = 0; 86 87 deh->deh_len = m->m_pkthdr.len; 88 89 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net; 90 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node; 91 deh->deh_dport = ddp->ddp_fsat.sat_port; 92 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net; 93 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node; 94 deh->deh_sport = ddp->ddp_lsat.sat_port; 95 96 /* 97 * The checksum calculation is done after all of the other bytes have 98 * been filled in. 99 */ 100 if (ddp_cksum) { 101 deh->deh_sum = at_cksum(m, sizeof(int)); 102 } else { 103 deh->deh_sum = 0; 104 } 105 deh->deh_bytes = htonl(deh->deh_bytes); 106 107 return (ddp_route(m, &ddp->ddp_route)); 108 } 109 110 u_short 111 at_cksum(m, skip) 112 struct mbuf *m; 113 int skip; 114 { 115 u_char *data, *end; 116 u_long cksum = 0; 117 118 for (; m; m = m->m_next) { 119 for (data = mtod(m, u_char *), end = data + m->m_len; 120 data < end; data++) { 121 if (skip) { 122 skip--; 123 continue; 124 } 125 cksum = (cksum + *data) << 1; 126 if (cksum & 0x00010000) { 127 cksum++; 128 } 129 cksum &= 0x0000ffff; 130 } 131 } 132 133 if (cksum == 0) { 134 cksum = 0x0000ffff; 135 } 136 return ((u_short) cksum); 137 } 138 139 int 140 ddp_route(m, ro) 141 struct mbuf *m; 142 struct route *ro; 143 { 144 struct sockaddr_at gate; 145 struct elaphdr *elh; 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 M_PREPEND(m, SZ_ELAPHDR, M_DONTWAIT); 173 if (!m) 174 return (ENOBUFS); 175 176 elh = mtod(m, struct elaphdr *); 177 elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node; 178 elh->el_type = ELAP_DDPEXTEND; 179 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 180 ntohs(aa->aa_firstnet) && 181 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 182 ntohs(aa->aa_lastnet)) { 183 elh->el_dnode = satosat(&ro->ro_dst)->sat_addr.s_node; 184 } else { 185 elh->el_dnode = 186 satosat(ro->ro_rt->rt_gateway)->sat_addr.s_node; 187 } 188 } 189 if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >= 190 ntohs(aa->aa_firstnet) && 191 ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <= 192 ntohs(aa->aa_lastnet)) { 193 gate = *satosat(&ro->ro_dst); 194 } else { 195 gate = *satosat(ro->ro_rt->rt_gateway); 196 } 197 ro->ro_rt->rt_use++; 198 199 #if IFA_STATS 200 aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; 201 #endif 202 203 /* XXX */ 204 return ((*ifp->if_output) (ifp, m, (struct sockaddr *) &gate, NULL)); 205 } 206