Home | History | Annotate | Line # | Download | only in netatalk
ddp_input.c revision 1.14
      1 /*	$NetBSD: ddp_input.c,v 1.14 2007/03/04 06:03:19 christos 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 (at) umich.edu
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ddp_input.c,v 1.14 2007/03/04 06:03:19 christos Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <net/netisr.h>
     36 #include <sys/mbuf.h>
     37 #include <sys/socket.h>
     38 #include <sys/socketvar.h>
     39 #include <sys/syslog.h>
     40 #include <net/if.h>
     41 #include <net/route.h>
     42 #include <net/if_ether.h>
     43 #include <netinet/in.h>
     44 
     45 #include <netatalk/at.h>
     46 #include <netatalk/at_var.h>
     47 #include <netatalk/ddp.h>
     48 #include <netatalk/ddp_var.h>
     49 #include <netatalk/at_extern.h>
     50 
     51 int             ddp_forward = 1;
     52 int             ddp_firewall = 0;
     53 extern int      ddp_cksum;
     54 void            ddp_input __P((struct mbuf *, struct ifnet *,
     55     struct elaphdr *, int));
     56 
     57 /*
     58  * Could probably merge these two code segments a little better...
     59  */
     60 void
     61 atintr()
     62 {
     63 	struct elaphdr *elhp, elh;
     64 	struct ifnet   *ifp;
     65 	struct mbuf    *m;
     66 	struct at_ifaddr *aa;
     67 	int             s;
     68 
     69 	for (;;) {
     70 		s = splnet();
     71 
     72 		IF_DEQUEUE(&atintrq2, m);
     73 
     74 		splx(s);
     75 
     76 		if (m == 0)	/* no more queued packets */
     77 			break;
     78 
     79 		m_claimm(m, &atalk_rx_mowner);
     80 		ifp = m->m_pkthdr.rcvif;
     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, (struct elaphdr *) NULL, 2);
     90 	}
     91 
     92 	for (;;) {
     93 		s = splnet();
     94 
     95 		IF_DEQUEUE(&atintrq1, m);
     96 
     97 		splx(s);
     98 
     99 		if (m == 0)	/* no more queued packets */
    100 
    101 			break;
    102 
    103 		m_claimm(m, &atalk_rx_mowner);
    104 		ifp = m->m_pkthdr.rcvif;
    105 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    106 			if (aa->aa_ifp == ifp &&
    107 			    (aa->aa_flags & AFA_PHASE2) == 0)
    108 				break;
    109 		}
    110 		if (aa == NULL) {	/* ifp not an appletalk interface */
    111 			m_freem(m);
    112 			continue;
    113 		}
    114 		if (m->m_len < SZ_ELAPHDR &&
    115 		    ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
    116 			ddpstat.ddps_tooshort++;
    117 			continue;
    118 		}
    119 		elhp = mtod(m, struct elaphdr *);
    120 		m_adj(m, SZ_ELAPHDR);
    121 
    122 		if (elhp->el_type == ELAP_DDPEXTEND) {
    123 			ddp_input(m, ifp, (struct elaphdr *) NULL, 1);
    124 		} else {
    125 			bcopy((void *) elhp, (void *) & elh, SZ_ELAPHDR);
    126 			ddp_input(m, ifp, &elh, 1);
    127 		}
    128 	}
    129 }
    130 
    131 struct route    forwro;
    132 
    133 void
    134 ddp_input(m, ifp, elh, phase)
    135 	struct mbuf    *m;
    136 	struct ifnet   *ifp;
    137 	struct elaphdr *elh;
    138 	int             phase;
    139 {
    140 	struct sockaddr_at from, to;
    141 	struct ddpshdr *dsh, ddps;
    142 	struct at_ifaddr *aa;
    143 	struct ddpehdr *deh = NULL, ddpe;
    144 	struct ddpcb   *ddp;
    145 	int             dlen, mlen;
    146 	u_short         cksum = 0;
    147 
    148 	bzero((void *) & from, sizeof(struct sockaddr_at));
    149 	if (elh) {
    150 		ddpstat.ddps_short++;
    151 
    152 		if (m->m_len < sizeof(struct ddpshdr) &&
    153 		    ((m = m_pullup(m, sizeof(struct ddpshdr))) == 0)) {
    154 			ddpstat.ddps_tooshort++;
    155 			return;
    156 		}
    157 		dsh = mtod(m, struct ddpshdr *);
    158 		bcopy((void *) dsh, (void *) & ddps, 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 		ddpstat.ddps_long++;
    183 
    184 		if (m->m_len < sizeof(struct ddpehdr) &&
    185 		    ((m = m_pullup(m, sizeof(struct ddpehdr))) == 0)) {
    186 			ddpstat.ddps_tooshort++;
    187 			return;
    188 		}
    189 		deh = mtod(m, struct ddpehdr *);
    190 		bcopy((void *) deh, (void *) & ddpe, 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 			ddpstat.ddps_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 		ddpstat.ddps_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 		if (satocsat(rtcache_getdst(&forwro))->sat_addr.s_net != to.sat_addr.s_net ||
    271 		    satocsat(rtcache_getdst(&forwro))->sat_addr.s_node != to.sat_addr.s_node)
    272 			rtcache_free(&forwro);
    273 		else
    274 			rtcache_check(&forwro);
    275 		if (forwro.ro_rt == NULL) {
    276 			memset(&forwro.ro_dst, 0, sizeof(struct sockaddr_at));
    277 			forwro.ro_dst.sa_len = sizeof(struct sockaddr_at);
    278 			forwro.ro_dst.sa_family = AF_APPLETALK;
    279 			satosat(&forwro.ro_dst)->sat_addr.s_net =
    280 			    to.sat_addr.s_net;
    281 			satosat(&forwro.ro_dst)->sat_addr.s_node =
    282 			    to.sat_addr.s_node;
    283 			rtcache_init(&forwro);
    284 		}
    285 		if (to.sat_addr.s_net !=
    286 		    satocsat(rtcache_getdst(&forwro))->sat_addr.s_net &&
    287 		    ddpe.deh_hops == DDP_MAXHOPS) {
    288 			m_freem(m);
    289 			return;
    290 		}
    291 		if (ddp_firewall &&
    292 		    (forwro.ro_rt == NULL || forwro.ro_rt->rt_ifp != ifp)) {
    293 			m_freem(m);
    294 			return;
    295 		}
    296 		ddpe.deh_hops++;
    297 		ddpe.deh_bytes = htonl(ddpe.deh_bytes);
    298 		bcopy((void *) & ddpe, (void *) deh, sizeof(u_short));/*XXX*/
    299 		if (ddp_route(m, &forwro)) {
    300 			ddpstat.ddps_cantforward++;
    301 		} else {
    302 			ddpstat.ddps_forward++;
    303 		}
    304 		return;
    305 	}
    306 	from.sat_len = sizeof(struct sockaddr_at);
    307 	from.sat_family = AF_APPLETALK;
    308 
    309 	if (elh) {
    310 		m_adj(m, sizeof(struct ddpshdr));
    311 	} else {
    312 		if (ddp_cksum && cksum && cksum != at_cksum(m, sizeof(int))) {
    313 			ddpstat.ddps_badsum++;
    314 			m_freem(m);
    315 			return;
    316 		}
    317 		m_adj(m, sizeof(struct ddpehdr));
    318 	}
    319 
    320 	if ((ddp = ddp_search(&from, &to, aa)) == NULL) {
    321 		m_freem(m);
    322 		return;
    323 	}
    324 	if (sbappendaddr(&ddp->ddp_socket->so_rcv, (struct sockaddr *) & from,
    325 			 m, (struct mbuf *) 0) == 0) {
    326 		ddpstat.ddps_nosockspace++;
    327 		m_freem(m);
    328 		return;
    329 	}
    330 #if IFA_STATS
    331 	if (aa)
    332 		aa->aa_ifa.ifa_data.ifad_inbytes += dlen;
    333 #endif
    334 	sorwakeup(ddp->ddp_socket);
    335 }
    336 
    337 #if 0
    338 
    339 #define BPXLEN	48
    340 #define BPALEN	16
    341 #include <ctype.h>
    342 
    343 static void
    344 bprint(data, len)
    345 	char *data;
    346 	int len;
    347 {
    348 	char            xout[BPXLEN], aout[BPALEN];
    349 	int             i = 0;
    350 
    351 	bzero(xout, BPXLEN);
    352 	bzero(aout, BPALEN);
    353 
    354 	for (;;) {
    355 		if (len < 1) {
    356 			if (i != 0) {
    357 				printf("%s\t%s\n", xout, aout);
    358 			}
    359 			printf("%s\n", "(end)");
    360 			break;
    361 		}
    362 		xout[(i * 3)] = hexdigits[(*data & 0xf0) >> 4];
    363 		xout[(i * 3) + 1] = hexdigits[*data & 0x0f];
    364 
    365 		if ((u_char) * data < 0x7f && (u_char) * data > 0x20) {
    366 			aout[i] = *data;
    367 		} else {
    368 			aout[i] = '.';
    369 		}
    370 
    371 		xout[(i * 3) + 2] = ' ';
    372 
    373 		i++;
    374 		len--;
    375 		data++;
    376 
    377 		if (i > BPALEN - 2) {
    378 			printf("%s\t%s\n", xout, aout);
    379 			bzero(xout, BPXLEN);
    380 			bzero(aout, BPALEN);
    381 			i = 0;
    382 			continue;
    383 		}
    384 	}
    385 }
    386 
    387 static void
    388 m_printm(m)
    389 	struct mbuf *m;
    390 {
    391 	for (; m; m = m->m_next)
    392 		bprint(mtod(m, char *), m->m_len);
    393 }
    394 #endif
    395