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