2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * $FreeBSD: src/sys/netatalk/aarp.c,v 1.12.2.2 2001/06/23 20:43:09 iedowse Exp $
6 * $DragonFly: src/sys/netproto/atalk/aarp.c,v 1.14 2004/09/16 21:55:03 joerg Exp $
11 #include <sys/param.h>
12 #include <sys/systm.h>
14 #include <sys/kernel.h>
15 #include <sys/socket.h>
16 #include <sys/syslog.h>
18 #include <sys/thread2.h>
19 #include <sys/msgport2.h>
22 #include <net/netisr.h>
24 #include <netinet/in.h>
26 #include <netinet/if_ether.h>
32 #include "at_extern.h"
34 static void aarptfree( struct aarptab *aat);
35 static void at_aarpinput( struct arpcom *ac, struct mbuf *m);
37 #define AARPTAB_BSIZ 9
39 #define AARPTAB_SIZE (AARPTAB_BSIZ * AARPTAB_NB)
40 static struct aarptab aarptab[AARPTAB_SIZE];
42 #define AARPTAB_HASH(a) \
43 ((((a).s_net << 8 ) + (a).s_node ) % AARPTAB_NB )
45 #define AARPTAB_LOOK(aat,addr) { \
47 aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
48 for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) \
49 if ( aat->aat_ataddr.s_net == (addr).s_net && \
50 aat->aat_ataddr.s_node == (addr).s_node ) \
52 if ( n >= AARPTAB_BSIZ ) \
56 #define AARPT_AGE (60 * 1)
57 #define AARPT_KILLC 20
60 static u_char atmulticastaddr[ 6 ] = {
61 0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
64 u_char at_org_code[ 3 ] = {
67 u_char aarp_org_code[ 3 ] = {
71 static struct callout aarptimer_ch;
74 aarptimer(void *ignored)
80 for ( i = 0; i < AARPTAB_SIZE; i++, aat++ ) {
81 if ( aat->aat_flags == 0 || ( aat->aat_flags & ATF_PERM ))
83 if ( ++aat->aat_timer < (( aat->aat_flags & ATF_COM ) ?
84 AARPT_KILLC : AARPT_KILLI ))
90 callout_reset(&aarptimer_ch, AARPT_AGE * hz, aarptimer, NULL);
94 * search through the network addresses to find one that includes
95 * the given network.. remember to take netranges into
99 at_ifawithnet(struct sockaddr_at *sat )
101 struct at_ifaddr *aa;
102 struct sockaddr_at *sat2;
104 for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
105 sat2 = &(aa->aa_addr);
106 if ( sat2->sat_addr.s_net == sat->sat_addr.s_net ) {
109 if( (aa->aa_flags & AFA_PHASE2 )
110 && (ntohs(aa->aa_firstnet) <= ntohs(sat->sat_addr.s_net))
111 && (ntohs(aa->aa_lastnet) >= ntohs(sat->sat_addr.s_net))) {
119 aarpwhohas( struct arpcom *ac, struct sockaddr_at *sat )
122 struct ether_header *eh;
123 struct ether_aarp *ea;
124 struct at_ifaddr *aa;
128 if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
131 m->m_len = sizeof( *ea );
132 m->m_pkthdr.len = sizeof( *ea );
133 MH_ALIGN( m, sizeof( *ea ));
135 ea = mtod( m, struct ether_aarp *);
136 bzero((caddr_t)ea, sizeof( *ea ));
138 ea->aarp_hrd = htons( AARPHRD_ETHER );
139 ea->aarp_pro = htons( ETHERTYPE_AT );
140 ea->aarp_hln = sizeof( ea->aarp_sha );
141 ea->aarp_pln = sizeof( ea->aarp_spu );
142 ea->aarp_op = htons( AARPOP_REQUEST );
143 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
144 sizeof( ea->aarp_sha ));
147 * We need to check whether the output ethernet type should
148 * be phase 1 or 2. We have the interface that we'll be sending
149 * the aarp out. We need to find an AppleTalk network on that
150 * interface with the same address as we're looking for. If the
151 * net is phase 2, generate an 802.2 and SNAP header.
153 if ((aa = at_ifawithnet( sat )) == NULL) {
158 eh = (struct ether_header *)sa.sa_data;
160 if ( aa->aa_flags & AFA_PHASE2 ) {
161 bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
162 sizeof( eh->ether_dhost ));
163 eh->ether_type = htons(sizeof(struct llc) + sizeof(struct ether_aarp));
164 M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
165 llc = mtod( m, struct llc *);
166 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
167 llc->llc_control = LLC_UI;
168 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
169 llc->llc_ether_type = htons( ETHERTYPE_AARP );
171 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
172 sizeof( ea->aarp_spnet ));
173 bcopy( &sat->sat_addr.s_net, ea->aarp_tpnet,
174 sizeof( ea->aarp_tpnet ));
175 ea->aarp_spnode = AA_SAT( aa )->sat_addr.s_node;
176 ea->aarp_tpnode = sat->sat_addr.s_node;
178 bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
179 ac->ac_if.if_addrlen);
180 eh->ether_type = htons( ETHERTYPE_AARP );
182 ea->aarp_spa = AA_SAT( aa )->sat_addr.s_node;
183 ea->aarp_tpa = sat->sat_addr.s_node;
187 printf("aarp: sending request for %u.%u\n",
188 ntohs(AA_SAT( aa )->sat_addr.s_net),
189 AA_SAT( aa )->sat_addr.s_node);
190 #endif /* NETATALKDEBUG */
192 sa.sa_len = sizeof( struct sockaddr );
193 sa.sa_family = AF_UNSPEC;
194 (*ac->ac_if.if_output)(&ac->ac_if,
195 m, &sa, NULL); /* XXX NULL should be routing information */
199 aarpresolve( ac, m, destsat, desten )
202 struct sockaddr_at *destsat;
205 struct at_ifaddr *aa;
209 if ( at_broadcast( destsat )) {
210 m->m_flags |= M_BCAST;
211 if ((aa = at_ifawithnet( destsat )) == NULL) {
215 if ( aa->aa_flags & AFA_PHASE2 ) {
216 bcopy( (caddr_t)atmulticastaddr, (caddr_t)desten,
217 sizeof( atmulticastaddr ));
219 bcopy(ac->ac_if.if_broadcastaddr, desten, ac->ac_if.if_addrlen);
225 AARPTAB_LOOK( aat, destsat->sat_addr );
226 if ( aat == 0 ) { /* No entry */
227 aat = aarptnew( &destsat->sat_addr );
229 panic( "aarpresolve: no free entry" );
232 aarpwhohas( ac, destsat );
238 if ( aat->aat_flags & ATF_COM ) { /* entry is COMplete */
239 bcopy( (caddr_t)aat->aat_enaddr, (caddr_t)desten,
240 sizeof( aat->aat_enaddr ));
244 /* entry has not completed */
245 if ( aat->aat_hold ) {
246 m_freem( aat->aat_hold );
249 aarpwhohas( ac, destsat );
255 aarpintr(struct netmsg *msg)
257 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
261 ac = (struct arpcom *)m->m_pkthdr.rcvif;
262 if ( ac->ac_if.if_flags & IFF_NOARP )
265 if ( m->m_len < sizeof( struct arphdr )) {
269 ar = mtod( m, struct arphdr *);
270 if ( ntohs( ar->ar_hrd ) != AARPHRD_ETHER ) {
274 if ( m->m_len < sizeof( struct arphdr ) + 2 * ar->ar_hln +
279 switch( ntohs( ar->ar_pro )) {
281 at_aarpinput( ac, m );
291 lwkt_replymsg(&msg->nm_lmsg, 0);
296 at_aarpinput( struct arpcom *ac, struct mbuf *m)
298 struct ether_aarp *ea;
300 struct at_ifaddr *aa;
302 struct ether_header *eh;
304 struct sockaddr_at sat;
306 struct at_addr spa, tpa, ma;
310 ea = mtod( m, struct ether_aarp *);
312 /* Check to see if from my hardware address */
313 if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
314 sizeof( ac->ac_enaddr ))) {
319 op = ntohs( ea->aarp_op );
320 bcopy( ea->aarp_tpnet, &net, sizeof( net ));
322 if ( net != 0 ) { /* should be ATADDR_ANYNET? */
323 sat.sat_len = sizeof(struct sockaddr_at);
324 sat.sat_family = AF_APPLETALK;
325 sat.sat_addr.s_net = net;
326 if ((aa = at_ifawithnet( &sat )) == NULL) {
330 bcopy( ea->aarp_spnet, &spa.s_net, sizeof( spa.s_net ));
331 bcopy( ea->aarp_tpnet, &tpa.s_net, sizeof( tpa.s_net ));
334 * Since we don't know the net, we just look for the first
335 * phase 1 address on the interface.
337 TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
338 aa = (struct at_ifaddr *)ifa;
339 if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
340 ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
348 tpa.s_net = spa.s_net = AA_SAT( aa )->sat_addr.s_net;
351 spa.s_node = ea->aarp_spnode;
352 tpa.s_node = ea->aarp_tpnode;
353 ma.s_net = AA_SAT( aa )->sat_addr.s_net;
354 ma.s_node = AA_SAT( aa )->sat_addr.s_node;
357 * This looks like it's from us.
359 if ( spa.s_net == ma.s_net && spa.s_node == ma.s_node ) {
360 if ( aa->aa_flags & AFA_PROBING ) {
362 * We're probing, someone either responded to our probe, or
363 * probed for the same address we'd like to use. Change the
364 * address we're probing for.
366 callout_stop(&aa->aa_ch);
370 } else if ( op != AARPOP_PROBE ) {
372 * This is not a probe, and we're not probing. This means
373 * that someone's saying they have the same source address
374 * as the one we're using. Get upset...
377 "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
378 ea->aarp_sha[ 0 ], ea->aarp_sha[ 1 ], ea->aarp_sha[ 2 ],
379 ea->aarp_sha[ 3 ], ea->aarp_sha[ 4 ], ea->aarp_sha[ 5 ]);
385 AARPTAB_LOOK( aat, spa );
387 if ( op == AARPOP_PROBE ) {
389 * Someone's probing for spa, dealocate the one we've got,
390 * so that if the prober keeps the address, we'll be able
398 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
399 sizeof( ea->aarp_sha ));
400 aat->aat_flags |= ATF_COM;
401 if ( aat->aat_hold ) {
402 struct mbuf *mhold = aat->aat_hold;
403 aat->aat_hold = NULL;
404 sat.sat_len = sizeof(struct sockaddr_at);
405 sat.sat_family = AF_APPLETALK;
407 (*ac->ac_if.if_output)( &ac->ac_if, mhold,
408 (struct sockaddr *)&sat, NULL); /* XXX */
410 } else if ((tpa.s_net == ma.s_net)
411 && (tpa.s_node == ma.s_node)
412 && (op != AARPOP_PROBE)
413 && ((aat = aarptnew( &spa )) != NULL)) {
414 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
415 sizeof( ea->aarp_sha ));
416 aat->aat_flags |= ATF_COM;
420 * Don't respond to responses, and never respond if we're
423 if ( tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
424 op == AARPOP_RESPONSE || ( aa->aa_flags & AFA_PROBING )) {
429 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )ea->aarp_tha,
430 sizeof( ea->aarp_sha ));
431 bcopy(( caddr_t )ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
432 sizeof( ea->aarp_sha ));
435 eh = (struct ether_header *)sa.sa_data;
436 bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )eh->ether_dhost,
437 sizeof( eh->ether_dhost ));
439 if ( aa->aa_flags & AFA_PHASE2 ) {
440 eh->ether_type = htons( sizeof( struct llc ) +
441 sizeof( struct ether_aarp ));
442 M_PREPEND( m, sizeof( struct llc ), MB_DONTWAIT );
446 llc = mtod( m, struct llc *);
447 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
448 llc->llc_control = LLC_UI;
449 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
450 llc->llc_ether_type = htons( ETHERTYPE_AARP );
452 bcopy( ea->aarp_spnet, ea->aarp_tpnet, sizeof( ea->aarp_tpnet ));
453 bcopy( &ma.s_net, ea->aarp_spnet, sizeof( ea->aarp_spnet ));
455 eh->ether_type = htons( ETHERTYPE_AARP );
458 ea->aarp_tpnode = ea->aarp_spnode;
459 ea->aarp_spnode = ma.s_node;
460 ea->aarp_op = htons( AARPOP_RESPONSE );
462 sa.sa_len = sizeof( struct sockaddr );
463 sa.sa_family = AF_UNSPEC;
464 (*ac->ac_if.if_output)( &ac->ac_if, m, &sa, NULL); /* XXX */
469 aarptfree( struct aarptab *aat)
473 m_freem( aat->aat_hold );
474 aat->aat_hold = NULL;
475 aat->aat_timer = aat->aat_flags = 0;
476 aat->aat_ataddr.s_net = 0;
477 aat->aat_ataddr.s_node = 0;
482 struct at_addr *addr;
486 struct aarptab *aat, *aato = NULL;
487 static int first = 1;
491 callout_init(&aarptimer_ch);
492 callout_reset(&aarptimer_ch, hz, aarptimer, NULL);
494 aat = &aarptab[ AARPTAB_HASH( *addr ) * AARPTAB_BSIZ ];
495 for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) {
496 if ( aat->aat_flags == 0 )
498 if ( aat->aat_flags & ATF_PERM )
500 if ((int) aat->aat_timer > oldest ) {
501 oldest = aat->aat_timer;
510 aat->aat_ataddr = *addr;
511 aat->aat_flags = ATF_INUSE;
517 aarpprobe( void *arg )
519 struct arpcom *ac = arg;
521 struct ether_header *eh;
522 struct ether_aarp *ea;
524 struct at_ifaddr *aa;
529 * We need to check whether the output ethernet type should
530 * be phase 1 or 2. We have the interface that we'll be sending
531 * the aarp out. We need to find an AppleTalk network on that
532 * interface with the same address as we're looking for. If the
533 * net is phase 2, generate an 802.2 and SNAP header.
535 TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
536 aa = (struct at_ifaddr *)ifa;
537 if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
538 ( aa->aa_flags & AFA_PROBING )) {
542 if ( aa == NULL ) { /* serious error XXX */
543 printf( "aarpprobe why did this happen?!\n" );
547 if ( aa->aa_probcnt <= 0 ) {
548 aa->aa_flags &= ~AFA_PROBING;
552 callout_reset(&aa->aa_ch, hz / 5, aarpprobe, ac);
555 if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
558 m->m_len = sizeof( *ea );
559 m->m_pkthdr.len = sizeof( *ea );
560 MH_ALIGN( m, sizeof( *ea ));
562 ea = mtod( m, struct ether_aarp *);
563 bzero((caddr_t)ea, sizeof( *ea ));
565 ea->aarp_hrd = htons( AARPHRD_ETHER );
566 ea->aarp_pro = htons( ETHERTYPE_AT );
567 ea->aarp_hln = sizeof( ea->aarp_sha );
568 ea->aarp_pln = sizeof( ea->aarp_spu );
569 ea->aarp_op = htons( AARPOP_PROBE );
570 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
571 sizeof( ea->aarp_sha ));
573 eh = (struct ether_header *)sa.sa_data;
575 if ( aa->aa_flags & AFA_PHASE2 ) {
576 bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
577 sizeof( eh->ether_dhost ));
578 eh->ether_type = htons( sizeof( struct llc ) +
579 sizeof( struct ether_aarp ));
580 M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
582 llc = mtod( m, struct llc *);
583 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
584 llc->llc_control = LLC_UI;
585 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
586 llc->llc_ether_type = htons( ETHERTYPE_AARP );
588 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
589 sizeof( ea->aarp_spnet ));
590 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_tpnet,
591 sizeof( ea->aarp_tpnet ));
592 ea->aarp_spnode = ea->aarp_tpnode = AA_SAT( aa )->sat_addr.s_node;
594 bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
595 ac->ac_if.if_addrlen);
596 eh->ether_type = htons( ETHERTYPE_AARP );
597 ea->aarp_spa = ea->aarp_tpa = AA_SAT( aa )->sat_addr.s_node;
601 printf("aarp: sending probe for %u.%u\n",
602 ntohs(AA_SAT( aa )->sat_addr.s_net),
603 AA_SAT( aa )->sat_addr.s_node);
604 #endif /* NETATALKDEBUG */
606 sa.sa_len = sizeof( struct sockaddr );
607 sa.sa_family = AF_UNSPEC;
608 (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, NULL); /* XXX */
618 callout_stop(&aarptimer_ch);
619 for ( i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++ ) {
620 if ( aat->aat_hold ) {
621 m_freem( aat->aat_hold );
622 aat->aat_hold = NULL;