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