Home | History | Annotate | Line # | Download | only in netatalk
ddp_input.c revision 1.3.6.1
      1  1.3.6.1      kenh /*	$NetBSD: ddp_input.c,v 1.3.6.1 1998/12/11 04:53:06 kenh 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.1  christos 
     29      1.1  christos #include <sys/types.h>
     30      1.1  christos #include <sys/param.h>
     31      1.1  christos #include <sys/systm.h>
     32      1.1  christos #include <sys/kernel.h>
     33      1.1  christos #include <net/netisr.h>
     34      1.1  christos #include <sys/mbuf.h>
     35      1.1  christos #include <sys/socket.h>
     36      1.1  christos #include <sys/socketvar.h>
     37      1.1  christos #include <sys/syslog.h>
     38      1.1  christos #include <net/if.h>
     39      1.1  christos #include <net/route.h>
     40      1.1  christos #include <net/if_ether.h>
     41      1.1  christos #include <netinet/in.h>
     42      1.1  christos 
     43      1.1  christos #include <netatalk/at.h>
     44      1.1  christos #include <netatalk/at_var.h>
     45      1.1  christos #include <netatalk/ddp.h>
     46      1.1  christos #include <netatalk/ddp_var.h>
     47      1.1  christos #include <netatalk/at_extern.h>
     48      1.1  christos 
     49      1.1  christos int             ddp_forward = 1;
     50      1.1  christos int             ddp_firewall = 0;
     51      1.1  christos extern int      ddp_cksum;
     52      1.1  christos void            ddp_input __P((struct mbuf *, struct ifnet *,
     53      1.1  christos     struct elaphdr *, int));
     54      1.1  christos 
     55      1.1  christos /*
     56      1.1  christos  * Could probably merge these two code segments a little better...
     57      1.1  christos  */
     58      1.1  christos void
     59      1.1  christos atintr()
     60      1.1  christos {
     61      1.1  christos 	struct elaphdr *elhp, elh;
     62      1.1  christos 	struct ifnet   *ifp;
     63      1.1  christos 	struct mbuf    *m;
     64      1.1  christos 	struct at_ifaddr *aa;
     65      1.1  christos 	int             s;
     66      1.1  christos 
     67      1.1  christos 	for (;;) {
     68      1.1  christos 		s = splimp();
     69      1.1  christos 
     70      1.1  christos 		IF_DEQUEUE(&atintrq2, m);
     71      1.1  christos 
     72      1.1  christos 		splx(s);
     73      1.1  christos 
     74      1.1  christos 		if (m == 0) {	/* no more queued packets */
     75      1.1  christos 			break;
     76      1.1  christos 		}
     77      1.1  christos 		ifp = m->m_pkthdr.rcvif;
     78  1.3.6.1      kenh 		s = splimp();
     79      1.1  christos 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
     80      1.1  christos 			if (aa->aa_ifp == ifp && (aa->aa_flags & AFA_PHASE2))
     81      1.1  christos 				break;
     82      1.1  christos 		}
     83  1.3.6.1      kenh 		splx(s);
     84      1.1  christos 		if (aa == NULL) {	/* ifp not an appletalk interface */
     85      1.1  christos 			m_freem(m);
     86      1.1  christos 			continue;
     87      1.1  christos 		}
     88      1.1  christos 		ddp_input(m, ifp, (struct elaphdr *) NULL, 2);
     89      1.1  christos 	}
     90      1.1  christos 
     91      1.1  christos 	for (;;) {
     92      1.1  christos 		s = splimp();
     93      1.1  christos 
     94      1.1  christos 		IF_DEQUEUE(&atintrq1, m);
     95      1.1  christos 
     96      1.1  christos 		splx(s);
     97      1.1  christos 
     98      1.1  christos 		if (m == 0) /* no more queued packets */
     99      1.1  christos 
    100      1.1  christos 			break;
    101      1.1  christos 		ifp = m->m_pkthdr.rcvif;
    102  1.3.6.1      kenh 		s = splimp();
    103      1.1  christos 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    104      1.1  christos 			if (aa->aa_ifp == ifp &&
    105      1.1  christos 			    (aa->aa_flags & AFA_PHASE2) == 0)
    106      1.1  christos 				break;
    107      1.1  christos 		}
    108  1.3.6.1      kenh 		splx(s);
    109      1.1  christos 		if (aa == NULL) {	/* ifp not an appletalk interface */
    110      1.1  christos 			m_freem(m);
    111      1.1  christos 			continue;
    112      1.1  christos 		}
    113      1.1  christos 		if (m->m_len < SZ_ELAPHDR &&
    114      1.1  christos 		    ((m = m_pullup(m, SZ_ELAPHDR)) == 0)) {
    115      1.1  christos 			ddpstat.ddps_tooshort++;
    116      1.1  christos 			continue;
    117      1.1  christos 		}
    118      1.1  christos 		elhp = mtod(m, struct elaphdr *);
    119      1.1  christos 		m_adj(m, SZ_ELAPHDR);
    120      1.1  christos 
    121      1.1  christos 		if (elhp->el_type == ELAP_DDPEXTEND) {
    122      1.1  christos 			ddp_input(m, ifp, (struct elaphdr *) NULL, 1);
    123      1.1  christos 		} else {
    124      1.1  christos 			bcopy((caddr_t) elhp, (caddr_t) & elh, SZ_ELAPHDR);
    125      1.1  christos 			ddp_input(m, ifp, &elh, 1);
    126      1.1  christos 		}
    127      1.1  christos 	}
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos struct route    forwro;
    131      1.1  christos 
    132      1.1  christos void
    133      1.1  christos ddp_input(m, ifp, elh, phase)
    134      1.1  christos 	struct mbuf    *m;
    135      1.1  christos 	struct ifnet   *ifp;
    136      1.1  christos 	struct elaphdr *elh;
    137      1.1  christos 	int             phase;
    138      1.1  christos {
    139      1.1  christos 	struct sockaddr_at from, to;
    140      1.1  christos 	struct ddpshdr *dsh, ddps;
    141  1.3.6.1      kenh 	struct at_ifaddr *aa = NULL;
    142      1.1  christos 	struct ddpehdr *deh = NULL, ddpe;
    143      1.1  christos 	struct ddpcb   *ddp;
    144  1.3.6.1      kenh 	int             dlen, mlen, s;
    145      1.1  christos 	u_short         cksum = 0;
    146      1.1  christos 
    147      1.1  christos 	bzero((caddr_t) & from, sizeof(struct sockaddr_at));
    148      1.1  christos 	if (elh) {
    149      1.1  christos 		ddpstat.ddps_short++;
    150      1.1  christos 
    151      1.1  christos 		if (m->m_len < sizeof(struct ddpshdr) &&
    152      1.1  christos 		    ((m = m_pullup(m, sizeof(struct ddpshdr))) == 0)) {
    153      1.1  christos 			ddpstat.ddps_tooshort++;
    154      1.1  christos 			return;
    155      1.1  christos 		}
    156      1.1  christos 		dsh = mtod(m, struct ddpshdr *);
    157      1.1  christos 		bcopy((caddr_t) dsh, (caddr_t) & ddps, sizeof(struct ddpshdr));
    158      1.1  christos 		ddps.dsh_bytes = ntohl(ddps.dsh_bytes);
    159      1.1  christos 		dlen = ddps.dsh_len;
    160      1.1  christos 
    161      1.1  christos 		to.sat_addr.s_net = ATADDR_ANYNET;
    162      1.1  christos 		to.sat_addr.s_node = elh->el_dnode;
    163      1.1  christos 		to.sat_port = ddps.dsh_dport;
    164      1.1  christos 		from.sat_addr.s_net = ATADDR_ANYNET;
    165      1.1  christos 		from.sat_addr.s_node = elh->el_snode;
    166      1.1  christos 		from.sat_port = ddps.dsh_sport;
    167      1.1  christos 
    168  1.3.6.1      kenh 		s = splimp();
    169      1.1  christos 		for (aa = at_ifaddr.tqh_first; aa; aa = aa->aa_list.tqe_next) {
    170      1.1  christos 			if (aa->aa_ifp == ifp &&
    171      1.1  christos 			    (aa->aa_flags & AFA_PHASE2) == 0 &&
    172      1.1  christos 			    (AA_SAT(aa)->sat_addr.s_node ==
    173      1.1  christos 			     to.sat_addr.s_node ||
    174      1.1  christos 			     to.sat_addr.s_node == ATADDR_BCAST))
    175      1.1  christos 				break;
    176      1.1  christos 		}
    177      1.1  christos 		if (aa == NULL) {
    178      1.1  christos 			m_freem(m);
    179  1.3.6.1      kenh 			splx(s);
    180      1.1  christos 			return;
    181      1.1  christos 		}
    182  1.3.6.1      kenh 		ifa_addref(&aa->aa_ifa);
    183  1.3.6.1      kenh 		splx(s);
    184      1.1  christos 	} else {
    185      1.1  christos 		ddpstat.ddps_long++;
    186      1.1  christos 
    187      1.1  christos 		if (m->m_len < sizeof(struct ddpehdr) &&
    188      1.1  christos 		    ((m = m_pullup(m, sizeof(struct ddpehdr))) == 0)) {
    189      1.1  christos 			ddpstat.ddps_tooshort++;
    190      1.1  christos 			return;
    191      1.1  christos 		}
    192      1.1  christos 		deh = mtod(m, struct ddpehdr *);
    193      1.1  christos 		bcopy((caddr_t) deh, (caddr_t) & ddpe, sizeof(struct ddpehdr));
    194      1.1  christos 		ddpe.deh_bytes = ntohl(ddpe.deh_bytes);
    195      1.1  christos 		dlen = ddpe.deh_len;
    196      1.1  christos 
    197      1.1  christos 		if ((cksum = ddpe.deh_sum) == 0) {
    198      1.1  christos 			ddpstat.ddps_nosum++;
    199      1.1  christos 		}
    200      1.1  christos 		from.sat_addr.s_net = ddpe.deh_snet;
    201      1.1  christos 		from.sat_addr.s_node = ddpe.deh_snode;
    202      1.1  christos 		from.sat_port = ddpe.deh_sport;
    203      1.1  christos 		to.sat_addr.s_net = ddpe.deh_dnet;
    204      1.1  christos 		to.sat_addr.s_node = ddpe.deh_dnode;
    205      1.1  christos 		to.sat_port = ddpe.deh_dport;
    206      1.1  christos 
    207  1.3.6.1      kenh 		s = splimp();
    208      1.1  christos 		if (to.sat_addr.s_net == ATADDR_ANYNET) {
    209      1.1  christos 			for (aa = at_ifaddr.tqh_first; aa;
    210      1.1  christos 			    aa = aa->aa_list.tqe_next) {
    211      1.1  christos 				if (phase == 1 && (aa->aa_flags & AFA_PHASE2))
    212      1.1  christos 					continue;
    213      1.1  christos 
    214      1.1  christos 				if (phase == 2 &&
    215      1.1  christos 				    (aa->aa_flags & AFA_PHASE2) == 0)
    216      1.1  christos 					continue;
    217      1.1  christos 
    218      1.1  christos 				if (aa->aa_ifp == ifp &&
    219      1.1  christos 				    (AA_SAT(aa)->sat_addr.s_node ==
    220      1.1  christos 				     to.sat_addr.s_node ||
    221      1.1  christos 				     to.sat_addr.s_node == ATADDR_BCAST ||
    222      1.1  christos 				     (ifp->if_flags & IFF_LOOPBACK)))
    223      1.1  christos 					break;
    224      1.1  christos 			}
    225      1.1  christos 		} else {
    226      1.1  christos 			for (aa = at_ifaddr.tqh_first; aa;
    227      1.1  christos 			    aa = aa->aa_list.tqe_next) {
    228      1.1  christos 				if (to.sat_addr.s_net == aa->aa_firstnet &&
    229      1.1  christos 				    to.sat_addr.s_node == 0)
    230      1.1  christos 					break;
    231      1.1  christos 
    232      1.1  christos 				if ((ntohs(to.sat_addr.s_net) <
    233      1.1  christos 				     ntohs(aa->aa_firstnet) ||
    234      1.1  christos 				     ntohs(to.sat_addr.s_net) >
    235      1.1  christos 				     ntohs(aa->aa_lastnet)) &&
    236      1.3  wrstuden 				    (ntohs(to.sat_addr.s_net) < 0xff00 ||
    237      1.3  wrstuden 				     ntohs(to.sat_addr.s_net) > 0xfffe))
    238      1.1  christos 					continue;
    239      1.1  christos 
    240      1.1  christos 				if (to.sat_addr.s_node !=
    241      1.1  christos 				    AA_SAT(aa)->sat_addr.s_node &&
    242      1.1  christos 				    to.sat_addr.s_node != ATADDR_BCAST)
    243      1.1  christos 					continue;
    244      1.1  christos 
    245      1.1  christos 				break;
    246      1.1  christos 			}
    247      1.1  christos 		}
    248  1.3.6.1      kenh 		if (aa)
    249  1.3.6.1      kenh 			ifa_addref(&aa->aa_ifa);
    250  1.3.6.1      kenh 		splx(s);
    251      1.1  christos 	}
    252      1.1  christos 
    253      1.1  christos 	/*
    254      1.1  christos          * Adjust the length, removing any padding that may have been added
    255      1.1  christos          * at a link layer.  We do this before we attempt to forward a packet,
    256      1.1  christos          * possibly on a different media.
    257      1.1  christos          */
    258      1.1  christos 	mlen = m->m_pkthdr.len;
    259      1.1  christos 	if (mlen < dlen) {
    260      1.1  christos 		ddpstat.ddps_toosmall++;
    261  1.3.6.1      kenh 		ifa_delref(&aa->aa_ifa);
    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.3.6.1      kenh 			if (aa)
    276  1.3.6.1      kenh 				ifa_delref(&aa->aa_ifa);
    277      1.1  christos 			m_freem(m);
    278      1.1  christos 			return;
    279      1.1  christos 		}
    280      1.1  christos 		if (forwro.ro_rt &&
    281      1.1  christos 		    (satosat(&forwro.ro_dst)->sat_addr.s_net !=
    282      1.1  christos 		     to.sat_addr.s_net ||
    283      1.1  christos 		     satosat(&forwro.ro_dst)->sat_addr.s_node !=
    284      1.1  christos 		     to.sat_addr.s_node)) {
    285      1.1  christos 			RTFREE(forwro.ro_rt);
    286      1.1  christos 			forwro.ro_rt = (struct rtentry *) 0;
    287      1.1  christos 		}
    288      1.1  christos 		if (forwro.ro_rt == (struct rtentry *) 0 ||
    289      1.1  christos 		    forwro.ro_rt->rt_ifp == (struct ifnet *) 0) {
    290      1.1  christos 			bzero(&forwro.ro_dst, sizeof(struct sockaddr_at));
    291      1.1  christos 			forwro.ro_dst.sa_len = sizeof(struct sockaddr_at);
    292      1.1  christos 			forwro.ro_dst.sa_family = AF_APPLETALK;
    293      1.1  christos 			satosat(&forwro.ro_dst)->sat_addr.s_net =
    294      1.1  christos 			    to.sat_addr.s_net;
    295      1.1  christos 			satosat(&forwro.ro_dst)->sat_addr.s_node =
    296      1.1  christos 			    to.sat_addr.s_node;
    297      1.1  christos 			rtalloc(&forwro);
    298      1.1  christos 		}
    299      1.1  christos 		if (to.sat_addr.s_net !=
    300      1.1  christos 		    satosat(&forwro.ro_dst)->sat_addr.s_net &&
    301      1.1  christos 		    ddpe.deh_hops == DDP_MAXHOPS) {
    302  1.3.6.1      kenh 			if (aa)
    303  1.3.6.1      kenh 				ifa_delref(&aa->aa_ifa);
    304      1.1  christos 			m_freem(m);
    305      1.1  christos 			return;
    306      1.1  christos 		}
    307      1.1  christos 		if (ddp_firewall &&
    308      1.1  christos 		    (forwro.ro_rt == NULL || forwro.ro_rt->rt_ifp != ifp)) {
    309  1.3.6.1      kenh 			if (aa)
    310  1.3.6.1      kenh 				ifa_delref(&aa->aa_ifa);
    311      1.1  christos 			m_freem(m);
    312      1.1  christos 			return;
    313      1.1  christos 		}
    314      1.1  christos 		ddpe.deh_hops++;
    315      1.1  christos 		ddpe.deh_bytes = htonl(ddpe.deh_bytes);
    316      1.1  christos 		bcopy((caddr_t) & ddpe, (caddr_t) deh, sizeof(u_short));/*XXX*/
    317      1.1  christos 		if (ddp_route(m, &forwro)) {
    318      1.1  christos 			ddpstat.ddps_cantforward++;
    319      1.1  christos 		} else {
    320      1.1  christos 			ddpstat.ddps_forward++;
    321      1.1  christos 		}
    322  1.3.6.1      kenh 		if (aa)
    323  1.3.6.1      kenh 			ifa_delref(&aa->aa_ifa);
    324      1.1  christos 		return;
    325      1.1  christos 	}
    326      1.1  christos 	from.sat_len = sizeof(struct sockaddr_at);
    327      1.1  christos 	from.sat_family = AF_APPLETALK;
    328      1.1  christos 
    329      1.1  christos 	if (elh) {
    330      1.1  christos 		m_adj(m, sizeof(struct ddpshdr));
    331      1.1  christos 	} else {
    332      1.1  christos 		if (ddp_cksum && cksum && cksum != at_cksum(m, sizeof(int))) {
    333      1.1  christos 			ddpstat.ddps_badsum++;
    334  1.3.6.1      kenh 			if (aa)
    335  1.3.6.1      kenh 				ifa_delref(&aa->aa_ifa);
    336      1.1  christos 			m_freem(m);
    337      1.1  christos 			return;
    338      1.1  christos 		}
    339      1.1  christos 		m_adj(m, sizeof(struct ddpehdr));
    340      1.1  christos 	}
    341      1.1  christos 
    342      1.1  christos 	if ((ddp = ddp_search(&from, &to, aa)) == NULL) {
    343  1.3.6.1      kenh 		if (aa)
    344  1.3.6.1      kenh 			ifa_delref(&aa->aa_ifa);
    345      1.1  christos 		m_freem(m);
    346      1.1  christos 		return;
    347      1.1  christos 	}
    348  1.3.6.1      kenh 	if (aa)
    349  1.3.6.1      kenh 		ifa_delref(&aa->aa_ifa);
    350      1.1  christos 	if (sbappendaddr(&ddp->ddp_socket->so_rcv, (struct sockaddr *) & from,
    351      1.1  christos 			 m, (struct mbuf *) 0) == 0) {
    352      1.1  christos 		ddpstat.ddps_nosockspace++;
    353      1.1  christos 		m_freem(m);
    354      1.1  christos 		return;
    355      1.1  christos 	}
    356      1.1  christos 	sorwakeup(ddp->ddp_socket);
    357      1.1  christos }
    358      1.1  christos 
    359      1.1  christos #if 0
    360      1.1  christos 
    361      1.1  christos #define BPXLEN	48
    362      1.1  christos #define BPALEN	16
    363      1.1  christos #include <ctype.h>
    364      1.1  christos char            hexdig[] = "0123456789ABCDEF";
    365      1.1  christos 
    366      1.1  christos static void
    367      1.1  christos bprint(data, len)
    368      1.1  christos 	char *data;
    369      1.1  christos 	int len;
    370      1.1  christos {
    371      1.1  christos 	char            xout[BPXLEN], aout[BPALEN];
    372      1.1  christos 	int             i = 0;
    373      1.1  christos 
    374      1.1  christos 	bzero(xout, BPXLEN);
    375      1.1  christos 	bzero(aout, BPALEN);
    376      1.1  christos 
    377      1.1  christos 	for (;;) {
    378      1.1  christos 		if (len < 1) {
    379      1.1  christos 			if (i != 0) {
    380      1.1  christos 				printf("%s\t%s\n", xout, aout);
    381      1.1  christos 			}
    382      1.1  christos 			printf("%s\n", "(end)");
    383      1.1  christos 			break;
    384      1.1  christos 		}
    385      1.1  christos 		xout[(i * 3)] = hexdig[(*data & 0xf0) >> 4];
    386      1.1  christos 		xout[(i * 3) + 1] = hexdig[*data & 0x0f];
    387      1.1  christos 
    388      1.1  christos 		if ((u_char) * data < 0x7f && (u_char) * data > 0x20) {
    389      1.1  christos 			aout[i] = *data;
    390      1.1  christos 		} else {
    391      1.1  christos 			aout[i] = '.';
    392      1.1  christos 		}
    393      1.1  christos 
    394      1.1  christos 		xout[(i * 3) + 2] = ' ';
    395      1.1  christos 
    396      1.1  christos 		i++;
    397      1.1  christos 		len--;
    398      1.1  christos 		data++;
    399      1.1  christos 
    400      1.1  christos 		if (i > BPALEN - 2) {
    401      1.1  christos 			printf("%s\t%s\n", xout, aout);
    402      1.1  christos 			bzero(xout, BPXLEN);
    403      1.1  christos 			bzero(aout, BPALEN);
    404      1.1  christos 			i = 0;
    405      1.1  christos 			continue;
    406      1.1  christos 		}
    407      1.1  christos 	}
    408      1.1  christos }
    409      1.1  christos 
    410      1.1  christos static void
    411      1.1  christos m_printm(m)
    412      1.1  christos 	struct mbuf *m;
    413      1.1  christos {
    414      1.1  christos 	for (; m; m = m->m_next)
    415      1.1  christos 		bprint(mtod(m, char *), m->m_len);
    416      1.1  christos }
    417      1.1  christos #endif
    418