1 /* $NetBSD: ddp_output.c,v 1.19 2016/06/20 06:46:38 knakahara 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.19 2016/06/20 06:46:38 knakahara 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 int ddp_cksum = 1; 53 54 int 55 ddp_output(struct mbuf *m, struct ddpcb *ddp) 56 { 57 struct ddpehdr *deh; 58 59 M_PREPEND(m, sizeof(struct ddpehdr), M_DONTWAIT); 60 if (!m) 61 return (ENOBUFS); 62 63 deh = mtod(m, struct ddpehdr *); 64 deh->deh_pad = 0; 65 deh->deh_hops = 0; 66 67 deh->deh_len = m->m_pkthdr.len; 68 69 deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net; 70 deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node; 71 deh->deh_dport = ddp->ddp_fsat.sat_port; 72 deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net; 73 deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node; 74 deh->deh_sport = ddp->ddp_lsat.sat_port; 75 76 /* 77 * The checksum calculation is done after all of the other bytes have 78 * been filled in. 79 */ 80 if (ddp_cksum) 81 deh->deh_sum = at_cksum(m, sizeof(int)); 82 else 83 deh->deh_sum = 0; 84 deh->deh_bytes = htonl(deh->deh_bytes); 85 86 return ddp_route(m, &ddp->ddp_route); 87 } 88 89 u_short 90 at_cksum(struct mbuf *m, int skip) 91 { 92 u_char *data, *end; 93 u_long cksum = 0; 94 95 for (; m; m = m->m_next) { 96 for (data = mtod(m, u_char *), end = data + m->m_len; 97 data < end; data++) { 98 if (skip) { 99 skip--; 100 continue; 101 } 102 cksum = (cksum + *data) << 1; 103 if (cksum & 0x00010000) 104 cksum++; 105 cksum &= 0x0000ffff; 106 } 107 } 108 109 if (cksum == 0) { 110 cksum = 0x0000ffff; 111 } 112 return (u_short)cksum; 113 } 114 115 int 116 ddp_route(struct mbuf *m, struct route *ro) 117 { 118 struct rtentry *rt; 119 struct sockaddr_at gate; 120 struct elaphdr *elh; 121 struct at_ifaddr *aa = NULL; 122 struct ifnet *ifp = NULL; 123 uint16_t net; 124 uint8_t loopback = 0; 125 126 if ((rt = rtcache_validate(ro)) != NULL && (ifp = rt->rt_ifp) != NULL) { 127 const struct sockaddr_at *dst = satocsat(rtcache_getdst(ro)); 128 uint16_t dnet = dst->sat_addr.s_net; 129 uint8_t dnode = dst->sat_addr.s_node; 130 net = satosat(rt->rt_gateway)->sat_addr.s_net; 131 132 TAILQ_FOREACH(aa, &at_ifaddr, aa_list) { 133 if (ntohs(net) >= ntohs(aa->aa_firstnet) && 134 ntohs(net) <= ntohs(aa->aa_lastnet)) { 135 /* Are we talking to ourselves? */ 136 if (dnet == aa->aa_addr.sat_addr.s_net && 137 dnode == aa->aa_addr.sat_addr.s_node) { 138 /* If to us, redirect to lo0. */ 139 ifp = lo0ifp; 140 } 141 /* Or is it a broadcast? */ 142 else if (dnet == aa->aa_addr.sat_addr.s_net && 143 dnode == 255) { 144 /* If broadcast, loop back a copy. */ 145 loopback = 1; 146 } 147 break; 148 } 149 } 150 } 151 if (aa == NULL) { 152 #ifdef NETATALKDEBUG 153 printf("%s: no address found\n", __func__); 154 #endif 155 m_freem(m); 156 return EINVAL; 157 } 158 /* 159 * There are several places in the kernel where data is added to 160 * an mbuf without ensuring that the mbuf pointer is aligned. 161 * This is bad for transition routing, since phase 1 and phase 2 162 * packets end up poorly aligned due to the three byte elap header. 163 */ 164 if (!(aa->aa_flags & AFA_PHASE2)) { 165 M_PREPEND(m, SZ_ELAPHDR, M_DONTWAIT); 166 if (m == NULL) 167 return ENOBUFS; 168 169 elh = mtod(m, struct elaphdr *); 170 elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node; 171 elh->el_type = ELAP_DDPEXTEND; 172 if (ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) >= 173 ntohs(aa->aa_firstnet) && 174 ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) <= 175 ntohs(aa->aa_lastnet)) { 176 elh->el_dnode = 177 satocsat(rtcache_getdst(ro))->sat_addr.s_node; 178 } else { 179 elh->el_dnode = 180 satosat(rt->rt_gateway)->sat_addr.s_node; 181 } 182 } 183 if (ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) >= 184 ntohs(aa->aa_firstnet) && 185 ntohs(satocsat(rtcache_getdst(ro))->sat_addr.s_net) <= 186 ntohs(aa->aa_lastnet)) { 187 gate = *satocsat(rtcache_getdst(ro)); 188 } else { 189 gate = *satosat(rt->rt_gateway); 190 } 191 rt->rt_use++; 192 193 #if IFA_STATS 194 aa->aa_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len; 195 #endif 196 197 /* XXX */ 198 if (loopback && rtcache_getdst(ro)->sa_family == AF_APPLETALK) { 199 struct mbuf *copym = m_copypacket(m, M_DONTWAIT); 200 201 #ifdef NETATALKDEBUG 202 printf("Looping back (not AARP).\n"); 203 #endif 204 looutput(lo0ifp, copym, rtcache_getdst(ro), NULL); 205 } 206 return if_output_lock(ifp, ifp, m, (struct sockaddr *)&gate, NULL); 207 } 208