Home | History | Annotate | Line # | Download | only in netatalk
aarp.c revision 1.27.12.1
      1  1.27.12.1  sborrill /*	$NetBSD: aarp.c,v 1.27.12.1 2011/06/30 09:31:15 sborrill 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.7     lukem 
     29        1.7     lukem #include <sys/cdefs.h>
     30  1.27.12.1  sborrill __KERNEL_RCSID(0, "$NetBSD: aarp.c,v 1.27.12.1 2011/06/30 09:31:15 sborrill Exp $");
     31       1.14    martin 
     32       1.14    martin #include "opt_mbuftrace.h"
     33        1.1  christos 
     34        1.8     lukem #include <sys/param.h>
     35        1.1  christos #include <sys/socket.h>
     36        1.1  christos #include <sys/syslog.h>
     37        1.1  christos #include <sys/systm.h>
     38        1.5   thorpej #include <sys/callout.h>
     39        1.1  christos #include <sys/proc.h>
     40        1.1  christos #include <sys/mbuf.h>
     41        1.1  christos #include <sys/time.h>
     42        1.1  christos #include <sys/kernel.h>
     43       1.27        ad #include <sys/socketvar.h>
     44        1.1  christos #include <net/if.h>
     45        1.1  christos #include <net/route.h>
     46        1.1  christos #include <net/if_ether.h>
     47        1.1  christos #include <net/if_dl.h>
     48        1.1  christos #include <netinet/in.h>
     49        1.1  christos #undef s_net
     50        1.1  christos 
     51        1.1  christos #include <netatalk/at.h>
     52        1.1  christos #include <netatalk/at_var.h>
     53        1.1  christos #include <netatalk/aarp.h>
     54        1.1  christos #include <netatalk/ddp_var.h>
     55        1.1  christos #include <netatalk/phase2.h>
     56        1.1  christos #include <netatalk/at_extern.h>
     57        1.1  christos 
     58       1.22    dyoung static struct aarptab *aarptnew(const struct at_addr *);
     59       1.22    dyoung static void aarptfree(struct aarptab *);
     60       1.22    dyoung static void at_aarpinput(struct ifnet *, struct mbuf *);
     61       1.22    dyoung static void aarptimer(void *);
     62       1.22    dyoung static void aarpwhohas(struct ifnet *, const struct sockaddr_at *);
     63        1.1  christos 
     64        1.1  christos #define AARPTAB_BSIZ	9
     65        1.1  christos #define AARPTAB_NB	19
     66        1.1  christos #define AARPTAB_SIZE	(AARPTAB_BSIZ * AARPTAB_NB)
     67        1.1  christos struct aarptab  aarptab[AARPTAB_SIZE];
     68        1.1  christos 
     69        1.1  christos #define AARPTAB_HASH(a) \
     70        1.1  christos     ((((a).s_net << 8 ) + (a).s_node ) % AARPTAB_NB )
     71        1.1  christos 
     72        1.1  christos #define AARPTAB_LOOK(aat,addr) { \
     73        1.1  christos     int		n; \
     74        1.1  christos     aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
     75        1.1  christos     for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) \
     76        1.1  christos 	if ( aat->aat_ataddr.s_net == (addr).s_net && \
     77        1.1  christos 	     aat->aat_ataddr.s_node == (addr).s_node ) \
     78        1.1  christos 	    break; \
     79        1.1  christos 	if ( n >= AARPTAB_BSIZ ) \
     80        1.1  christos 	    aat = 0; \
     81        1.1  christos }
     82        1.1  christos 
     83        1.1  christos #define AARPT_AGE	(60 * 1)
     84        1.1  christos #define AARPT_KILLC	20
     85        1.1  christos #define AARPT_KILLI	3
     86        1.1  christos 
     87       1.12      matt const u_char atmulticastaddr[6] = {
     88        1.1  christos 	0x09, 0x00, 0x07, 0xff, 0xff, 0xff
     89        1.1  christos };
     90        1.1  christos 
     91       1.12      matt const u_char at_org_code[3] = {
     92        1.1  christos 	0x08, 0x00, 0x07
     93        1.1  christos };
     94       1.12      matt const u_char aarp_org_code[3] = {
     95        1.1  christos 	0x00, 0x00, 0x00
     96        1.1  christos };
     97        1.1  christos 
     98        1.5   thorpej struct callout aarptimer_callout;
     99       1.12      matt #ifdef MBUFTRACE
    100       1.19    dogcow struct mowner aarp_mowner = MOWNER_INIT("atalk", "arp");
    101       1.12      matt #endif
    102        1.1  christos 
    103        1.9     perry /*ARGSUSED*/
    104        1.1  christos static void
    105       1.21  christos aarptimer(void *ignored)
    106        1.1  christos {
    107        1.1  christos 	struct aarptab *aat;
    108        1.1  christos 	int             i, s;
    109        1.1  christos 
    110       1.27        ad 	mutex_enter(softnet_lock);
    111        1.5   thorpej 	callout_reset(&aarptimer_callout, AARPT_AGE * hz, aarptimer, NULL);
    112        1.1  christos 	aat = aarptab;
    113        1.1  christos 	for (i = 0; i < AARPTAB_SIZE; i++, aat++) {
    114        1.1  christos 		int killtime = (aat->aat_flags & ATF_COM) ? AARPT_KILLC :
    115        1.1  christos 		    AARPT_KILLI;
    116        1.1  christos 		if (aat->aat_flags == 0 || (aat->aat_flags & ATF_PERM))
    117        1.1  christos 			continue;
    118        1.1  christos 		if (++aat->aat_timer < killtime)
    119        1.1  christos 			continue;
    120        1.6   thorpej 		s = splnet();
    121        1.1  christos 		aarptfree(aat);
    122        1.1  christos 		splx(s);
    123        1.1  christos 	}
    124       1.27        ad 	mutex_exit(softnet_lock);
    125        1.1  christos }
    126        1.1  christos 
    127        1.1  christos /*
    128        1.1  christos  * search through the network addresses to find one that includes the given
    129        1.1  christos  * network.. remember to take netranges into consideration.
    130        1.1  christos  */
    131        1.2  christos struct ifaddr *
    132        1.2  christos at_ifawithnet(sat, ifp)
    133       1.16  christos 	const struct sockaddr_at *sat;
    134        1.2  christos 	struct ifnet *ifp;
    135        1.2  christos {
    136        1.1  christos 	struct ifaddr  *ifa;
    137        1.1  christos 	struct sockaddr_at *sat2;
    138        1.1  christos 	struct netrange *nr;
    139        1.1  christos 
    140       1.26    dyoung 	IFADDR_FOREACH(ifa, ifp) {
    141        1.1  christos 		if (ifa->ifa_addr->sa_family != AF_APPLETALK)
    142        1.1  christos 			continue;
    143        1.1  christos 
    144        1.1  christos 		sat2 = satosat(ifa->ifa_addr);
    145        1.1  christos 		if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
    146        1.1  christos 			break;
    147        1.1  christos 
    148        1.1  christos 		nr = (struct netrange *) (sat2->sat_zero);
    149        1.1  christos 		if ((nr->nr_phase == 2)
    150       1.17  christos 		    && (ntohs(nr->nr_firstnet) <= ntohs(sat->sat_addr.s_net))
    151       1.17  christos 		    && (ntohs(nr->nr_lastnet) >= ntohs(sat->sat_addr.s_net)))
    152        1.1  christos 			break;
    153        1.1  christos 	}
    154        1.1  christos 	return ifa;
    155        1.1  christos }
    156        1.1  christos 
    157        1.1  christos static void
    158        1.1  christos aarpwhohas(ifp, sat)
    159        1.1  christos 	struct ifnet *ifp;
    160       1.22    dyoung 	const struct sockaddr_at *sat;
    161        1.1  christos {
    162        1.1  christos 	struct mbuf    *m;
    163        1.1  christos 	struct ether_header *eh;
    164        1.1  christos 	struct ether_aarp *ea;
    165        1.1  christos 	struct at_ifaddr *aa;
    166        1.1  christos 	struct llc     *llc;
    167        1.1  christos 	struct sockaddr sa;
    168        1.1  christos 
    169        1.1  christos 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    170        1.1  christos 		return;
    171        1.1  christos 
    172       1.12      matt 	MCLAIM(m, &aarp_mowner);
    173        1.1  christos 	m->m_len = sizeof(*ea);
    174        1.1  christos 	m->m_pkthdr.len = sizeof(*ea);
    175        1.1  christos 	MH_ALIGN(m, sizeof(*ea));
    176        1.1  christos 
    177        1.1  christos 	ea = mtod(m, struct ether_aarp *);
    178        1.1  christos 	bzero(ea, sizeof(*ea));
    179        1.1  christos 
    180        1.1  christos 	ea->aarp_hrd = htons(AARPHRD_ETHER);
    181        1.3       kim 	ea->aarp_pro = htons(ETHERTYPE_ATALK);
    182        1.1  christos 	ea->aarp_hln = sizeof(ea->aarp_sha);
    183        1.1  christos 	ea->aarp_pln = sizeof(ea->aarp_spu);
    184        1.1  christos 	ea->aarp_op = htons(AARPOP_REQUEST);
    185       1.25    dyoung 	bcopy(CLLADDR(ifp->if_sadl), ea->aarp_sha, sizeof(ea->aarp_sha));
    186        1.1  christos 
    187        1.1  christos 	/*
    188        1.1  christos          * We need to check whether the output ethernet type should
    189        1.1  christos          * be phase 1 or 2. We have the interface that we'll be sending
    190        1.1  christos          * the aarp out. We need to find an AppleTalk network on that
    191        1.1  christos          * interface with the same address as we're looking for. If the
    192        1.1  christos          * net is phase 2, generate an 802.2 and SNAP header.
    193        1.1  christos          */
    194        1.2  christos 	if ((aa = (struct at_ifaddr *) at_ifawithnet(sat, ifp)) == NULL) {
    195        1.1  christos 		m_freem(m);
    196        1.1  christos 		return;
    197        1.1  christos 	}
    198        1.1  christos 	eh = (struct ether_header *) sa.sa_data;
    199        1.1  christos 
    200        1.1  christos 	if (aa->aa_flags & AFA_PHASE2) {
    201        1.1  christos 		bcopy(atmulticastaddr, eh->ether_dhost,
    202        1.1  christos 		    sizeof(eh->ether_dhost));
    203        1.4      matt 		eh->ether_type = 0;	/* if_output will treat as 802 */
    204       1.13    itojun 		M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    205       1.13    itojun 		if (!m)
    206       1.13    itojun 			return;
    207       1.13    itojun 
    208        1.1  christos 		llc = mtod(m, struct llc *);
    209        1.1  christos 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    210        1.1  christos 		llc->llc_control = LLC_UI;
    211        1.1  christos 		bcopy(aarp_org_code, llc->llc_org_code, sizeof(aarp_org_code));
    212        1.1  christos 		llc->llc_ether_type = htons(ETHERTYPE_AARP);
    213        1.1  christos 
    214        1.1  christos 		bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
    215        1.1  christos 		      sizeof(ea->aarp_spnet));
    216        1.1  christos 		bcopy(&sat->sat_addr.s_net, ea->aarp_tpnet,
    217        1.1  christos 		      sizeof(ea->aarp_tpnet));
    218        1.1  christos 		ea->aarp_spnode = AA_SAT(aa)->sat_addr.s_node;
    219        1.1  christos 		ea->aarp_tpnode = sat->sat_addr.s_node;
    220        1.1  christos 	} else {
    221        1.1  christos 		bcopy(etherbroadcastaddr, eh->ether_dhost,
    222        1.1  christos 		    sizeof(eh->ether_dhost));
    223        1.1  christos 		eh->ether_type = htons(ETHERTYPE_AARP);
    224        1.1  christos 
    225        1.1  christos 		ea->aarp_spa = AA_SAT(aa)->sat_addr.s_node;
    226        1.1  christos 		ea->aarp_tpa = sat->sat_addr.s_node;
    227        1.1  christos 	}
    228        1.1  christos 
    229        1.1  christos #ifdef NETATALKDEBUG
    230        1.1  christos 	printf("aarp: sending request via %u.%u seaking %u.%u\n",
    231        1.1  christos 	    ntohs(AA_SAT(aa)->sat_addr.s_net), AA_SAT(aa)->sat_addr.s_node,
    232        1.1  christos 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    233        1.1  christos #endif	/* NETATALKDEBUG */
    234        1.1  christos 
    235        1.1  christos 	sa.sa_len = sizeof(struct sockaddr);
    236        1.1  christos 	sa.sa_family = AF_UNSPEC;
    237        1.1  christos 	(*ifp->if_output) (ifp, m, &sa, NULL);	/* XXX NULL should be routing */
    238        1.1  christos 						/* information */
    239        1.1  christos }
    240        1.1  christos 
    241        1.1  christos int
    242       1.22    dyoung aarpresolve(struct ifnet *ifp, struct mbuf *m,
    243       1.22    dyoung     const struct sockaddr_at *destsat, u_char *desten)
    244        1.1  christos {
    245        1.1  christos 	struct at_ifaddr *aa;
    246        1.1  christos 	struct aarptab *aat;
    247        1.1  christos 	int             s;
    248        1.1  christos 
    249        1.1  christos 	if (at_broadcast(destsat)) {
    250        1.2  christos 		aa = (struct at_ifaddr *) at_ifawithnet(destsat, ifp);
    251        1.2  christos 		if (aa == NULL) {
    252        1.1  christos 			m_freem(m);
    253        1.1  christos 			return (0);
    254        1.1  christos 		}
    255        1.1  christos 		if (aa->aa_flags & AFA_PHASE2)
    256        1.1  christos 			bcopy(atmulticastaddr, desten,
    257        1.1  christos 			    sizeof(atmulticastaddr));
    258        1.1  christos 		else
    259        1.1  christos 			bcopy(etherbroadcastaddr, desten,
    260        1.1  christos 			    sizeof(etherbroadcastaddr));
    261        1.1  christos 		return 1;
    262        1.1  christos 	}
    263        1.6   thorpej 	s = splnet();
    264        1.1  christos 	AARPTAB_LOOK(aat, destsat->sat_addr);
    265        1.1  christos 	if (aat == 0) {		/* No entry */
    266        1.1  christos 		aat = aarptnew(&destsat->sat_addr);
    267        1.1  christos 		if (aat == 0)
    268        1.1  christos 			panic("aarpresolve: no free entry");
    269        1.1  christos 
    270        1.1  christos 		aat->aat_hold = m;
    271        1.1  christos 		aarpwhohas(ifp, destsat);
    272        1.1  christos 		splx(s);
    273        1.1  christos 		return 0;
    274        1.1  christos 	}
    275        1.1  christos 
    276        1.1  christos 	/* found an entry */
    277        1.1  christos 	aat->aat_timer = 0;
    278        1.1  christos 	if (aat->aat_flags & ATF_COM) {	/* entry is COMplete */
    279        1.1  christos 		bcopy(aat->aat_enaddr, desten, sizeof(aat->aat_enaddr));
    280        1.1  christos 		splx(s);
    281        1.1  christos 		return 1;
    282        1.1  christos 	}
    283        1.1  christos 
    284        1.1  christos 	/* entry has not completed */
    285        1.1  christos 	if (aat->aat_hold)
    286        1.1  christos 		m_freem(aat->aat_hold);
    287        1.1  christos 	aat->aat_hold = m;
    288        1.1  christos 	aarpwhohas(ifp, destsat);
    289        1.1  christos 	splx(s);
    290        1.1  christos 
    291        1.1  christos 	return 0;
    292        1.1  christos }
    293        1.1  christos 
    294        1.1  christos void
    295        1.1  christos aarpinput(ifp, m)
    296        1.1  christos 	struct ifnet   *ifp;
    297        1.1  christos 	struct mbuf    *m;
    298        1.1  christos {
    299        1.1  christos 	struct arphdr  *ar;
    300        1.1  christos 
    301        1.1  christos 	if (ifp->if_flags & IFF_NOARP)
    302        1.1  christos 		goto out;
    303        1.1  christos 
    304        1.1  christos 	if (m->m_len < sizeof(struct arphdr))
    305        1.1  christos 		goto out;
    306        1.1  christos 
    307        1.1  christos 	ar = mtod(m, struct arphdr *);
    308        1.1  christos 	if (ntohs(ar->ar_hrd) != AARPHRD_ETHER)
    309        1.1  christos 		goto out;
    310        1.1  christos 
    311        1.1  christos 	if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
    312        1.1  christos 		goto out;
    313        1.1  christos 
    314        1.1  christos 	switch (ntohs(ar->ar_pro)) {
    315        1.3       kim 	case ETHERTYPE_ATALK:
    316        1.1  christos 		at_aarpinput(ifp, m);
    317        1.1  christos 		return;
    318        1.1  christos 
    319        1.1  christos 	default:
    320        1.1  christos 		break;
    321        1.1  christos 	}
    322        1.1  christos 
    323        1.1  christos out:
    324        1.1  christos 	m_freem(m);
    325        1.1  christos }
    326        1.1  christos 
    327        1.1  christos static void
    328        1.1  christos at_aarpinput(ifp, m)
    329        1.1  christos 	struct ifnet *ifp;
    330        1.1  christos 	struct mbuf *m;
    331        1.1  christos {
    332        1.1  christos 	struct ether_aarp *ea;
    333        1.1  christos 	struct at_ifaddr *aa;
    334       1.23    dyoung 	struct ifaddr *ia;
    335        1.1  christos 	struct aarptab *aat;
    336        1.1  christos 	struct ether_header *eh;
    337        1.1  christos 	struct llc     *llc;
    338        1.1  christos 	struct sockaddr_at sat;
    339        1.1  christos 	struct sockaddr sa;
    340        1.1  christos 	struct at_addr  spa, tpa, ma;
    341        1.1  christos 	int             op;
    342        1.1  christos 	u_int16_t       net;
    343        1.1  christos 
    344        1.1  christos 	ea = mtod(m, struct ether_aarp *);
    345        1.1  christos 
    346        1.1  christos 	/* Check to see if from my hardware address */
    347       1.25    dyoung 	if (!bcmp(ea->aarp_sha, CLLADDR(ifp->if_sadl), sizeof(ea->aarp_sha))) {
    348        1.1  christos 		m_freem(m);
    349        1.1  christos 		return;
    350        1.1  christos 	}
    351        1.1  christos 	op = ntohs(ea->aarp_op);
    352        1.1  christos 	bcopy(ea->aarp_tpnet, &net, sizeof(net));
    353        1.1  christos 
    354        1.1  christos 	if (net != 0) {		/* should be ATADDR_ANYNET? */
    355        1.1  christos 		sat.sat_len = sizeof(struct sockaddr_at);
    356        1.1  christos 		sat.sat_family = AF_APPLETALK;
    357        1.1  christos 		sat.sat_addr.s_net = net;
    358        1.2  christos 		aa = (struct at_ifaddr *) at_ifawithnet(&sat, ifp);
    359        1.2  christos 		if (aa == NULL) {
    360        1.1  christos 			m_freem(m);
    361        1.1  christos 			return;
    362        1.1  christos 		}
    363        1.1  christos 		bcopy(ea->aarp_spnet, &spa.s_net, sizeof(spa.s_net));
    364        1.1  christos 		bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof(tpa.s_net));
    365        1.1  christos 	} else {
    366        1.1  christos 		/*
    367        1.1  christos 		 * Since we don't know the net, we just look for the first
    368        1.1  christos 		 * phase 1 address on the interface.
    369        1.1  christos 		 */
    370       1.26    dyoung 		IFADDR_FOREACH(ia, ifp) {
    371       1.23    dyoung 			aa = (struct at_ifaddr *)ia;
    372        1.1  christos 			if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
    373        1.1  christos 			    (aa->aa_flags & AFA_PHASE2) == 0)
    374        1.1  christos 				break;
    375        1.1  christos 		}
    376       1.23    dyoung 		if (ia == NULL) {
    377        1.1  christos 			m_freem(m);
    378        1.1  christos 			return;
    379        1.1  christos 		}
    380        1.1  christos 		tpa.s_net = spa.s_net = AA_SAT(aa)->sat_addr.s_net;
    381        1.1  christos 	}
    382        1.1  christos 
    383        1.1  christos 	spa.s_node = ea->aarp_spnode;
    384        1.1  christos 	tpa.s_node = ea->aarp_tpnode;
    385        1.1  christos 	ma.s_net = AA_SAT(aa)->sat_addr.s_net;
    386        1.1  christos 	ma.s_node = AA_SAT(aa)->sat_addr.s_node;
    387        1.1  christos 
    388        1.1  christos 	/*
    389        1.1  christos          * This looks like it's from us.
    390        1.1  christos          */
    391        1.1  christos 	if (spa.s_net == ma.s_net && spa.s_node == ma.s_node) {
    392        1.1  christos 		if (aa->aa_flags & AFA_PROBING) {
    393        1.1  christos 			/*
    394        1.1  christos 		         * We're probing, someone either responded to our
    395        1.1  christos 			 * probe, or probed for the same address we'd like
    396        1.1  christos 			 * to use. Change the address we're probing for.
    397        1.1  christos 		         */
    398        1.5   thorpej 			callout_stop(&aa->aa_probe_ch);
    399        1.1  christos 			wakeup(aa);
    400        1.1  christos 			m_freem(m);
    401        1.1  christos 			return;
    402        1.1  christos 		} else if (op != AARPOP_PROBE) {
    403        1.1  christos 			/*
    404        1.1  christos 		         * This is not a probe, and we're not probing.
    405        1.1  christos 			 * This means that someone's saying they have the same
    406        1.1  christos 			 * source address as the one we're using. Get upset...
    407        1.1  christos 		         */
    408        1.1  christos 			log(LOG_ERR, "aarp: duplicate AT address!! %s\n",
    409        1.1  christos 			    ether_sprintf(ea->aarp_sha));
    410        1.1  christos 			m_freem(m);
    411        1.1  christos 			return;
    412        1.1  christos 		}
    413        1.1  christos 	}
    414        1.1  christos 	AARPTAB_LOOK(aat, spa);
    415        1.1  christos 	if (aat) {
    416        1.1  christos 		if (op == AARPOP_PROBE) {
    417        1.1  christos 			/*
    418       1.11       wiz 		         * Someone's probing for spa, deallocate the one we've
    419        1.1  christos 			 * got, so that if the prober keeps the address, we'll
    420        1.1  christos 			 * be able to arp for him.
    421        1.1  christos 		         */
    422        1.1  christos 			aarptfree(aat);
    423        1.1  christos 			m_freem(m);
    424        1.1  christos 			return;
    425        1.1  christos 		}
    426        1.1  christos 		bcopy(ea->aarp_sha, aat->aat_enaddr, sizeof(ea->aarp_sha));
    427        1.1  christos 		aat->aat_flags |= ATF_COM;
    428        1.1  christos 		if (aat->aat_hold) {
    429        1.1  christos 			sat.sat_len = sizeof(struct sockaddr_at);
    430        1.1  christos 			sat.sat_family = AF_APPLETALK;
    431        1.1  christos 			sat.sat_addr = spa;
    432        1.1  christos 			(*ifp->if_output)(ifp, aat->aat_hold,
    433        1.1  christos 			    (struct sockaddr *) & sat, NULL);	/* XXX */
    434        1.1  christos 			aat->aat_hold = 0;
    435        1.1  christos 		}
    436        1.1  christos 	}
    437        1.1  christos 	if (aat == 0 && tpa.s_net == ma.s_net && tpa.s_node == ma.s_node
    438        1.1  christos 	    && op != AARPOP_PROBE) {
    439        1.1  christos 		if ((aat = aarptnew(&spa)) != NULL) {
    440        1.1  christos 			bcopy(ea->aarp_sha, aat->aat_enaddr,
    441        1.1  christos 			    sizeof(ea->aarp_sha));
    442        1.1  christos 			aat->aat_flags |= ATF_COM;
    443        1.1  christos 		}
    444        1.1  christos 	}
    445        1.1  christos 	/*
    446        1.1  christos          * Don't respond to responses, and never respond if we're
    447        1.1  christos          * still probing.
    448        1.1  christos          */
    449        1.1  christos 	if (tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
    450        1.1  christos 	    op == AARPOP_RESPONSE || (aa->aa_flags & AFA_PROBING)) {
    451        1.1  christos 		m_freem(m);
    452        1.1  christos 		return;
    453        1.1  christos 	}
    454        1.1  christos 	bcopy(ea->aarp_sha, ea->aarp_tha, sizeof(ea->aarp_sha));
    455       1.25    dyoung 	bcopy(CLLADDR(ifp->if_sadl), ea->aarp_sha, sizeof(ea->aarp_sha));
    456        1.1  christos 
    457        1.1  christos 	/* XXX */
    458        1.1  christos 	eh = (struct ether_header *) sa.sa_data;
    459        1.1  christos 	bcopy(ea->aarp_tha, eh->ether_dhost, sizeof(eh->ether_dhost));
    460        1.1  christos 
    461        1.1  christos 	if (aa->aa_flags & AFA_PHASE2) {
    462        1.1  christos 		M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    463        1.1  christos 		if (m == NULL)
    464        1.1  christos 			return;
    465        1.1  christos 
    466        1.1  christos 		llc = mtod(m, struct llc *);
    467        1.1  christos 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    468        1.1  christos 		llc->llc_control = LLC_UI;
    469        1.1  christos 		bcopy(aarp_org_code, llc->llc_org_code, sizeof(aarp_org_code));
    470        1.1  christos 		llc->llc_ether_type = htons(ETHERTYPE_AARP);
    471        1.1  christos 
    472        1.1  christos 		bcopy(ea->aarp_spnet, ea->aarp_tpnet, sizeof(ea->aarp_tpnet));
    473        1.1  christos 		bcopy(&ma.s_net, ea->aarp_spnet, sizeof(ea->aarp_spnet));
    474        1.4      matt 		eh->ether_type = 0;	/* if_output will treat as 802 */
    475        1.1  christos 	} else {
    476        1.1  christos 		eh->ether_type = htons(ETHERTYPE_AARP);
    477        1.1  christos 	}
    478        1.1  christos 
    479        1.1  christos 	ea->aarp_tpnode = ea->aarp_spnode;
    480        1.1  christos 	ea->aarp_spnode = ma.s_node;
    481        1.1  christos 	ea->aarp_op = htons(AARPOP_RESPONSE);
    482        1.1  christos 
    483        1.1  christos 	sa.sa_len = sizeof(struct sockaddr);
    484        1.1  christos 	sa.sa_family = AF_UNSPEC;
    485        1.1  christos 	(*ifp->if_output) (ifp, m, &sa, NULL);	/* XXX */
    486        1.1  christos 	return;
    487        1.1  christos }
    488        1.1  christos 
    489        1.1  christos static void
    490        1.1  christos aarptfree(aat)
    491        1.1  christos 	struct aarptab *aat;
    492        1.1  christos {
    493        1.1  christos 
    494        1.1  christos 	if (aat->aat_hold)
    495        1.1  christos 		m_freem(aat->aat_hold);
    496        1.1  christos 	aat->aat_hold = 0;
    497        1.1  christos 	aat->aat_timer = aat->aat_flags = 0;
    498        1.1  christos 	aat->aat_ataddr.s_net = 0;
    499        1.1  christos 	aat->aat_ataddr.s_node = 0;
    500        1.1  christos }
    501        1.1  christos 
    502        1.1  christos static struct aarptab *
    503       1.22    dyoung aarptnew(const struct at_addr *addr)
    504        1.1  christos {
    505        1.1  christos 	int             n;
    506        1.1  christos 	int             oldest = -1;
    507        1.1  christos 	struct aarptab *aat, *aato = NULL;
    508        1.1  christos 	static int      first = 1;
    509        1.1  christos 
    510        1.1  christos 	if (first) {
    511        1.1  christos 		first = 0;
    512       1.24        ad 		callout_init(&aarptimer_callout, 0);
    513        1.5   thorpej 		callout_reset(&aarptimer_callout, hz, aarptimer, NULL);
    514        1.1  christos 	}
    515        1.1  christos 	aat = &aarptab[AARPTAB_HASH(*addr) * AARPTAB_BSIZ];
    516        1.1  christos 	for (n = 0; n < AARPTAB_BSIZ; n++, aat++) {
    517        1.1  christos 		if (aat->aat_flags == 0)
    518        1.1  christos 			goto out;
    519        1.1  christos 		if (aat->aat_flags & ATF_PERM)
    520        1.1  christos 			continue;
    521        1.1  christos 		if ((int) aat->aat_timer > oldest) {
    522        1.1  christos 			oldest = aat->aat_timer;
    523        1.1  christos 			aato = aat;
    524        1.1  christos 		}
    525        1.1  christos 	}
    526        1.1  christos 	if (aato == NULL)
    527        1.1  christos 		return (NULL);
    528        1.1  christos 	aat = aato;
    529        1.1  christos 	aarptfree(aat);
    530        1.1  christos out:
    531        1.1  christos 	aat->aat_ataddr = *addr;
    532        1.1  christos 	aat->aat_flags = ATF_INUSE;
    533        1.1  christos 	return (aat);
    534        1.1  christos }
    535        1.1  christos 
    536        1.1  christos 
    537        1.1  christos void
    538        1.1  christos aarpprobe(arp)
    539        1.1  christos 	void *arp;
    540        1.1  christos {
    541        1.1  christos 	struct mbuf    *m;
    542        1.1  christos 	struct ether_header *eh;
    543        1.1  christos 	struct ether_aarp *ea;
    544       1.23    dyoung 	struct ifaddr *ia;
    545        1.1  christos 	struct at_ifaddr *aa;
    546        1.1  christos 	struct llc     *llc;
    547        1.1  christos 	struct sockaddr sa;
    548        1.1  christos 	struct ifnet   *ifp = arp;
    549        1.1  christos 
    550       1.27        ad 	mutex_enter(softnet_lock);
    551       1.27        ad 
    552        1.1  christos 	/*
    553        1.1  christos          * We need to check whether the output ethernet type should
    554        1.1  christos          * be phase 1 or 2. We have the interface that we'll be sending
    555        1.1  christos          * the aarp out. We need to find an AppleTalk network on that
    556        1.1  christos          * interface with the same address as we're looking for. If the
    557        1.1  christos          * net is phase 2, generate an 802.2 and SNAP header.
    558        1.1  christos          */
    559       1.26    dyoung 	IFADDR_FOREACH(ia, ifp) {
    560       1.23    dyoung 		aa = (struct at_ifaddr *)ia;
    561        1.1  christos 		if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
    562        1.1  christos 		    (aa->aa_flags & AFA_PROBING))
    563        1.1  christos 			break;
    564        1.1  christos 	}
    565       1.23    dyoung 	if (ia == NULL) {	/* serious error XXX */
    566        1.1  christos 		printf("aarpprobe why did this happen?!\n");
    567       1.27        ad 		mutex_exit(softnet_lock);
    568        1.1  christos 		return;
    569        1.1  christos 	}
    570        1.1  christos 	if (aa->aa_probcnt <= 0) {
    571        1.1  christos 		aa->aa_flags &= ~AFA_PROBING;
    572        1.1  christos 		wakeup(aa);
    573       1.27        ad 		mutex_exit(softnet_lock);
    574        1.1  christos 		return;
    575        1.1  christos 	} else {
    576        1.5   thorpej 		callout_reset(&aa->aa_probe_ch, hz / 5, aarpprobe, arp);
    577        1.1  christos 	}
    578        1.1  christos 
    579       1.27        ad 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) {
    580       1.27        ad 		mutex_exit(softnet_lock);
    581        1.1  christos 		return;
    582       1.27        ad 	}
    583       1.12      matt 
    584       1.12      matt 	MCLAIM(m, &aarp_mowner);
    585        1.1  christos 	m->m_len = sizeof(*ea);
    586        1.1  christos 	m->m_pkthdr.len = sizeof(*ea);
    587        1.1  christos 	MH_ALIGN(m, sizeof(*ea));
    588        1.1  christos 
    589        1.1  christos 	ea = mtod(m, struct ether_aarp *);
    590        1.1  christos 	bzero(ea, sizeof(*ea));
    591        1.1  christos 
    592        1.1  christos 	ea->aarp_hrd = htons(AARPHRD_ETHER);
    593        1.3       kim 	ea->aarp_pro = htons(ETHERTYPE_ATALK);
    594        1.1  christos 	ea->aarp_hln = sizeof(ea->aarp_sha);
    595        1.1  christos 	ea->aarp_pln = sizeof(ea->aarp_spu);
    596        1.1  christos 	ea->aarp_op = htons(AARPOP_PROBE);
    597       1.25    dyoung 	bcopy(CLLADDR(ifp->if_sadl), ea->aarp_sha, sizeof(ea->aarp_sha));
    598        1.1  christos 
    599        1.1  christos 	eh = (struct ether_header *) sa.sa_data;
    600        1.1  christos 
    601        1.1  christos 	if (aa->aa_flags & AFA_PHASE2) {
    602        1.1  christos 		bcopy(atmulticastaddr, eh->ether_dhost,
    603        1.1  christos 		    sizeof(eh->ether_dhost));
    604        1.4      matt 		eh->ether_type = 0;	/* if_output will treat as 802 */
    605       1.13    itojun 		M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
    606       1.27        ad 		if (!m) {
    607       1.27        ad 			mutex_exit(softnet_lock);
    608       1.13    itojun 			return;
    609       1.27        ad 		}
    610       1.13    itojun 
    611        1.1  christos 		llc = mtod(m, struct llc *);
    612        1.1  christos 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    613        1.1  christos 		llc->llc_control = LLC_UI;
    614        1.1  christos 		bcopy(aarp_org_code, llc->llc_org_code, sizeof(aarp_org_code));
    615        1.1  christos 		llc->llc_ether_type = htons(ETHERTYPE_AARP);
    616        1.1  christos 
    617        1.1  christos 		bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
    618        1.1  christos 		      sizeof(ea->aarp_spnet));
    619        1.1  christos 		bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_tpnet,
    620        1.1  christos 		      sizeof(ea->aarp_tpnet));
    621        1.1  christos 		ea->aarp_spnode = ea->aarp_tpnode =
    622        1.1  christos 		    AA_SAT(aa)->sat_addr.s_node;
    623        1.1  christos 	} else {
    624        1.1  christos 		bcopy(etherbroadcastaddr, eh->ether_dhost,
    625        1.1  christos 		    sizeof(eh->ether_dhost));
    626        1.1  christos 		eh->ether_type = htons(ETHERTYPE_AARP);
    627        1.1  christos 		ea->aarp_spa = ea->aarp_tpa = AA_SAT(aa)->sat_addr.s_node;
    628        1.1  christos 	}
    629        1.1  christos 
    630        1.1  christos #ifdef NETATALKDEBUG
    631        1.1  christos 	printf("aarp: sending probe for %u.%u\n",
    632        1.1  christos 	       ntohs(AA_SAT(aa)->sat_addr.s_net),
    633        1.1  christos 	       AA_SAT(aa)->sat_addr.s_node);
    634        1.1  christos #endif	/* NETATALKDEBUG */
    635        1.1  christos 
    636        1.1  christos 	sa.sa_len = sizeof(struct sockaddr);
    637        1.1  christos 	sa.sa_family = AF_UNSPEC;
    638        1.1  christos 	(*ifp->if_output) (ifp, m, &sa, NULL);	/* XXX */
    639        1.1  christos 	aa->aa_probcnt--;
    640       1.27        ad 	mutex_exit(softnet_lock);
    641        1.1  christos }
    642        1.1  christos 
    643        1.1  christos void
    644        1.1  christos aarp_clean()
    645        1.1  christos {
    646        1.1  christos 	struct aarptab *aat;
    647        1.1  christos 	int             i;
    648        1.1  christos 
    649        1.5   thorpej 	callout_stop(&aarptimer_callout);
    650        1.1  christos 	for (i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++)
    651        1.1  christos 		if (aat->aat_hold)
    652        1.1  christos 			m_freem(aat->aat_hold);
    653        1.1  christos }
    654