1 /* $NetBSD: ddp_input.c,v 1.31 2018/03/21 14:23:54 roy Exp $ */ 2 3 /* 4 * Copyright (c) 1990,1994 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_input.c,v 1.31 2018/03/21 14:23:54 roy Exp $"); 31 #include "opt_atalk.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <net/netisr.h> 37 #include <sys/mbuf.h> 38 #include <sys/socket.h> 39 #include <sys/socketvar.h> 40 #include <sys/syslog.h> 41 #include <net/if.h> 42 #include <net/route.h> 43 #include <net/if_ether.h> 44 #include <netinet/in.h> 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/ddp_private.h> 51 #include <netatalk/at_extern.h> 52 53 int ddp_forward = 1; 54 int ddp_firewall = 0; 55 extern int ddp_cksum; 56 void ddp_input(struct mbuf *, struct ifnet *, 57 struct elaphdr *, int); 58 59 /* 60 * Could probably merge these two code segments a little better... 61 */ 62 void 63 atintr(void) 64 { 65 struct elaphdr *elhp, elh; 66 struct ifnet *ifp; 67 struct mbuf *m; 68 struct at_ifaddr *aa; 69 70 mutex_enter(softnet_lock); 71 for (;;) { 72 IFQ_LOCK(&atintrq2); 73 IF_DEQUEUE(&atintrq2, m); 74 IFQ_UNLOCK(&atintrq2); 75 76 if (m == 0) /* no more queued packets */ 77 break; 78 79 m_claimm(m, &atalk_rx_mowner); 80 ifp = m_get_rcvif_NOMPSAFE(m); 81 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 82 if (aa->aa_ifp == ifp && (aa->aa_flags & AFA_PHASE2)) 83 break; 84 } 85 if (aa == NULL) { /* ifp not an appletalk interface */ 86 m_freem(m); 87 continue; 88 } 89 ddp_input(m, ifp, NULL, 2); 90 } 91 92 for (;;) { 93 IFQ_LOCK(&atintrq1); 94 IF_DEQUEUE(&atintrq1, m); 95 IFQ_UNLOCK(&atintrq1); 96 97 if (m == 0) /* no more queued packets */ 98 99 break; 100 101 m_claimm(m, &atalk_rx_mowner); 102 ifp = m_get_rcvif_NOMPSAFE(m); 103 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 104 if (aa->aa_ifp == ifp && 105 (aa->aa_flags & AFA_PHASE2) == 0) 106 break; 107 } 108 if (aa == NULL) { /* ifp not an appletalk interface */ 109 m_freem(m); 110 continue; 111 } 112 if (m->m_len < SZ_ELAPHDR && 113 ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) { 114 DDP_STATINC(DDP_STAT_TOOSHORT); 115 continue; 116 } 117 elhp = mtod(m, struct elaphdr *); 118 m_adj(m, SZ_ELAPHDR); 119 120 if (elhp->el_type == ELAP_DDPEXTEND) { 121 ddp_input(m, ifp, NULL, 1); 122 } else { 123 memcpy((void *) & elh, (void *) elhp, SZ_ELAPHDR); 124 ddp_input(m, ifp, &elh, 1); 125 } 126 } 127 mutex_exit(softnet_lock); 128 } 129 130 struct route forwro; 131 132 void 133 ddp_input(struct mbuf *m, struct ifnet *ifp, struct elaphdr *elh, int phase) 134 { 135 struct rtentry *rt; 136 struct sockaddr_at from, to; 137 struct ddpshdr *dsh, ddps; 138 struct at_ifaddr *aa; 139 struct ddpehdr *deh = NULL, ddpe; 140 struct ddpcb *ddp; 141 int dlen, mlen; 142 u_short cksum = 0; 143 union { 144 struct sockaddr dst; 145 struct sockaddr_at dsta; 146 } u; 147 148 memset((void *) & from, 0, sizeof(struct sockaddr_at)); 149 if (elh) { 150 DDP_STATINC(DDP_STAT_SHORT); 151 152 if (m->m_len < sizeof(struct ddpshdr) && 153 ((m = m_pullup(m, sizeof(struct ddpshdr))) == 0)) { 154 DDP_STATINC(DDP_STAT_TOOSHORT); 155 return; 156 } 157 dsh = mtod(m, struct ddpshdr *); 158 memcpy((void *) & ddps, (void *) dsh, sizeof(struct ddpshdr)); 159 ddps.dsh_bytes = ntohl(ddps.dsh_bytes); 160 dlen = ddps.dsh_len; 161 162 to.sat_addr.s_net = ATADDR_ANYNET; 163 to.sat_addr.s_node = elh->el_dnode; 164 to.sat_port = ddps.dsh_dport; 165 from.sat_addr.s_net = ATADDR_ANYNET; 166 from.sat_addr.s_node = elh->el_snode; 167 from.sat_port = ddps.dsh_sport; 168 169 for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) { 170 if (aa->aa_ifp == ifp && 171 (aa->aa_flags & AFA_PHASE2) == 0 && 172 (AA_SAT(aa)->sat_addr.s_node == 173 to.sat_addr.s_node || 174 to.sat_addr.s_node == ATADDR_BCAST)) 175 break; 176 } 177 if (aa == NULL) { 178 m_freem(m); 179 return; 180 } 181 } else { 182 DDP_STATINC(DDP_STAT_LONG); 183 184 if (m->m_len < sizeof(struct ddpehdr) && 185 ((m = m_pullup(m, sizeof(struct ddpehdr))) == 0)) { 186 DDP_STATINC(DDP_STAT_TOOSHORT); 187 return; 188 } 189 deh = mtod(m, struct ddpehdr *); 190 memcpy((void *) & ddpe, (void *) deh, sizeof(struct ddpehdr)); 191 ddpe.deh_bytes = ntohl(ddpe.deh_bytes); 192 dlen = ddpe.deh_len; 193 194 if ((cksum = ddpe.deh_sum) == 0) { 195 DDP_STATINC(DDP_STAT_NOSUM); 196 } 197 from.sat_addr.s_net = ddpe.deh_snet; 198 from.sat_addr.s_node = ddpe.deh_snode; 199 from.sat_port = ddpe.deh_sport; 200 to.sat_addr.s_net = ddpe.deh_dnet; 201 to.sat_addr.s_node = ddpe.deh_dnode; 202 to.sat_port = ddpe.deh_dport; 203 204 if (to.sat_addr.s_net == ATADDR_ANYNET) { 205 for (aa = at_ifaddr.tqh_first; aa; 206 aa = aa->aa_list.tqe_next) { 207 if (phase == 1 && (aa->aa_flags & AFA_PHASE2)) 208 continue; 209 210 if (phase == 2 && 211 (aa->aa_flags & AFA_PHASE2) == 0) 212 continue; 213 214 if (aa->aa_ifp == ifp && 215 (AA_SAT(aa)->sat_addr.s_node == 216 to.sat_addr.s_node || 217 to.sat_addr.s_node == ATADDR_BCAST || 218 (ifp->if_flags & IFF_LOOPBACK))) 219 break; 220 } 221 } else { 222 for (aa = at_ifaddr.tqh_first; aa; 223 aa = aa->aa_list.tqe_next) { 224 if (to.sat_addr.s_net == aa->aa_firstnet && 225 to.sat_addr.s_node == 0) 226 break; 227 228 if ((ntohs(to.sat_addr.s_net) < 229 ntohs(aa->aa_firstnet) || 230 ntohs(to.sat_addr.s_net) > 231 ntohs(aa->aa_lastnet)) && 232 (ntohs(to.sat_addr.s_net) < 0xff00 || 233 ntohs(to.sat_addr.s_net) > 0xfffe)) 234 continue; 235 236 if (to.sat_addr.s_node != 237 AA_SAT(aa)->sat_addr.s_node && 238 to.sat_addr.s_node != ATADDR_BCAST) 239 continue; 240 241 break; 242 } 243 } 244 } 245 246 /* 247 * Adjust the length, removing any padding that may have been added 248 * at a link layer. We do this before we attempt to forward a packet, 249 * possibly on a different media. 250 */ 251 mlen = m->m_pkthdr.len; 252 if (mlen < dlen) { 253 DDP_STATINC(DDP_STAT_TOOSMALL); 254 m_freem(m); 255 return; 256 } 257 if (mlen > dlen) { 258 m_adj(m, dlen - mlen); 259 } 260 /* 261 * XXX Should we deliver broadcasts locally, also, or rely on the 262 * link layer to give us a copy? For the moment, the latter. 263 */ 264 if (aa == NULL || (to.sat_addr.s_node == ATADDR_BCAST && 265 aa->aa_ifp != ifp && (ifp->if_flags & IFF_LOOPBACK) == 0)) { 266 if (ddp_forward == 0) { 267 m_freem(m); 268 return; 269 } 270 sockaddr_at_init(&u.dsta, &to.sat_addr, 0); 271 rt = rtcache_lookup(&forwro, &u.dst); 272 #if 0 /* XXX The if-condition is always false. What was this 273 * actually trying to test? 274 */ 275 if (to.sat_addr.s_net != 276 satocsat(rtcache_getdst(&forwro))->sat_addr.s_net && 277 ddpe.deh_hops == DDP_MAXHOPS) { 278 m_freem(m); 279 return; 280 } 281 #endif 282 if (ddp_firewall && (rt == NULL || rt->rt_ifp != ifp)) { 283 rtcache_unref(rt, &forwro); 284 m_freem(m); 285 return; 286 } 287 rtcache_unref(rt, &forwro); 288 ddpe.deh_hops++; 289 ddpe.deh_bytes = htonl(ddpe.deh_bytes); 290 memcpy((void *) deh, (void *) & ddpe, sizeof(u_short));/*XXX*/ 291 if (ddp_route(m, &forwro)) { 292 DDP_STATINC(DDP_STAT_CANTFORWARD); 293 } else { 294 DDP_STATINC(DDP_STAT_FORWARD); 295 } 296 return; 297 } 298 from.sat_len = sizeof(struct sockaddr_at); 299 from.sat_family = AF_APPLETALK; 300 301 if (elh) { 302 m_adj(m, sizeof(struct ddpshdr)); 303 } else { 304 if (ddp_cksum && cksum && cksum != at_cksum(m, sizeof(int))) { 305 DDP_STATINC(DDP_STAT_BADSUM); 306 m_freem(m); 307 return; 308 } 309 m_adj(m, sizeof(struct ddpehdr)); 310 } 311 312 if ((ddp = ddp_search(&from, &to, aa)) == NULL) { 313 m_freem(m); 314 return; 315 } 316 if (sbappendaddr(&ddp->ddp_socket->so_rcv, (struct sockaddr *) & from, 317 m, (struct mbuf *) 0) == 0) { 318 DDP_STATINC(DDP_STAT_NOSOCKSPACE); 319 soroverflow(ddp->ddp_socket); 320 m_freem(m); 321 return; 322 } 323 #if IFA_STATS 324 if (aa) 325 aa->aa_ifa.ifa_data.ifad_inbytes += dlen; 326 #endif 327 sorwakeup(ddp->ddp_socket); 328 } 329 330 #if 0 331 332 #define BPXLEN 48 333 #define BPALEN 16 334 #include <ctype.h> 335 336 static void 337 bprint(char *data, int len) 338 { 339 char xout[BPXLEN], aout[BPALEN]; 340 int i = 0; 341 342 memset(xout, 0, BPXLEN); 343 memset(aout, 0, BPALEN); 344 345 for (;;) { 346 if (len < 1) { 347 if (i != 0) { 348 printf("%s\t%s\n", xout, aout); 349 } 350 printf("%s\n", "(end)"); 351 break; 352 } 353 xout[(i * 3)] = hexdigits[(*data & 0xf0) >> 4]; 354 xout[(i * 3) + 1] = hexdigits[*data & 0x0f]; 355 356 if ((u_char) * data < 0x7f && (u_char) * data > 0x20) { 357 aout[i] = *data; 358 } else { 359 aout[i] = '.'; 360 } 361 362 xout[(i * 3) + 2] = ' '; 363 364 i++; 365 len--; 366 data++; 367 368 if (i > BPALEN - 2) { 369 printf("%s\t%s\n", xout, aout); 370 memset(xout, 0, BPXLEN); 371 memset(aout, 0, BPALEN); 372 i = 0; 373 continue; 374 } 375 } 376 } 377 378 static void 379 m_printm(struct mbuf *m) 380 { 381 for (; m; m = m->m_next) 382 bprint(mtod(m, char *), m->m_len); 383 } 384 #endif 385