MFC r1.3 r1.13 r1.8 (HEAD):
[dragonfly.git] / sys / netproto / atalk / aarp.c
blob86f36324921cfc29831e1b5aa2e6b1fa1f07b142
1 /*
2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
3 * All Rights Reserved.
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.24 2008/05/14 11:59:24 sephe Exp $
7 */
9 #include "opt_atalk.h"
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/mbuf.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>
21 #include <net/if.h>
22 #include <net/netisr.h>
24 #include <netinet/in.h>
25 #undef s_net
26 #include <netinet/if_ether.h>
28 #include "at.h"
29 #include "at_var.h"
30 #include "aarp.h"
31 #include "phase2.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
38 #define AARPTAB_NB 19
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) { \
46 int n; \
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 ) \
51 break; \
52 if ( n >= AARPTAB_BSIZ ) \
53 aat = 0; \
56 #define AARPT_AGE (60 * 1)
57 #define AARPT_KILLC 20
58 #define AARPT_KILLI 3
60 static u_char atmulticastaddr[ 6 ] = {
61 0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
64 u_char at_org_code[ 3 ] = {
65 0x08, 0x00, 0x07,
67 u_char aarp_org_code[ 3 ] = {
68 0x00, 0x00, 0x00,
71 static struct callout aarptimer_ch;
73 static void
74 aarptimer(void *ignored)
76 struct aarptab *aat;
77 int i;
79 aat = aarptab;
80 for ( i = 0; i < AARPTAB_SIZE; i++, aat++ ) {
81 if ( aat->aat_flags == 0 || ( aat->aat_flags & ATF_PERM ))
82 continue;
83 if ( ++aat->aat_timer < (( aat->aat_flags & ATF_COM ) ?
84 AARPT_KILLC : AARPT_KILLI ))
85 continue;
86 crit_enter();
87 aarptfree( aat );
88 crit_exit();
90 callout_reset(&aarptimer_ch, AARPT_AGE * hz, aarptimer, NULL);
93 /*
94 * search through the network addresses to find one that includes
95 * the given network.. remember to take netranges into
96 * consideration.
98 struct at_ifaddr *
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 ) {
107 break;
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))) {
112 break;
115 return( aa );
118 static void
119 aarpwhohas(struct arpcom *ac, struct sockaddr_at *sat)
121 struct mbuf *m;
122 struct ether_header *eh;
123 struct ether_aarp *ea;
124 struct at_ifaddr *aa;
125 struct llc *llc;
126 struct sockaddr sa;
128 if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
129 return;
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(ac->ac_enaddr, ea->aarp_sha, sizeof ea->aarp_sha);
146 * We need to check whether the output ethernet type should
147 * be phase 1 or 2. We have the interface that we'll be sending
148 * the aarp out. We need to find an AppleTalk network on that
149 * interface with the same address as we're looking for. If the
150 * net is phase 2, generate an 802.2 and SNAP header.
152 if ((aa = at_ifawithnet( sat )) == NULL) {
153 m_freem( m );
154 return;
157 eh = (struct ether_header *)sa.sa_data;
159 if ( aa->aa_flags & AFA_PHASE2 ) {
160 bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
161 sizeof( eh->ether_dhost ));
162 eh->ether_type = htons(sizeof(struct llc) + sizeof(struct ether_aarp));
163 M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
164 llc = mtod( m, struct llc *);
165 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
166 llc->llc_control = LLC_UI;
167 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
168 llc->llc_ether_type = htons( ETHERTYPE_AARP );
170 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
171 sizeof( ea->aarp_spnet ));
172 bcopy( &sat->sat_addr.s_net, ea->aarp_tpnet,
173 sizeof( ea->aarp_tpnet ));
174 ea->aarp_spnode = AA_SAT( aa )->sat_addr.s_node;
175 ea->aarp_tpnode = sat->sat_addr.s_node;
176 } else {
177 bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
178 ac->ac_if.if_addrlen);
179 eh->ether_type = htons( ETHERTYPE_AARP );
181 ea->aarp_spa = AA_SAT( aa )->sat_addr.s_node;
182 ea->aarp_tpa = sat->sat_addr.s_node;
185 #ifdef NETATALKDEBUG
186 kprintf("aarp: sending request for %u.%u\n",
187 ntohs(AA_SAT( aa )->sat_addr.s_net),
188 AA_SAT( aa )->sat_addr.s_node);
189 #endif /* NETATALKDEBUG */
191 sa.sa_len = sizeof( struct sockaddr );
192 sa.sa_family = AF_UNSPEC;
193 ac->ac_if.if_output(&ac->ac_if,
194 m, &sa, NULL); /* XXX NULL should be routing information */
198 aarpresolve(struct arpcom *ac, struct mbuf *m, struct sockaddr_at *destsat,
199 u_char *desten )
201 struct at_ifaddr *aa;
202 struct aarptab *aat;
204 if (at_broadcast(destsat)) {
205 m->m_flags |= M_BCAST;
206 if ((aa = at_ifawithnet(destsat)) == NULL) {
207 m_freem(m);
208 return (0);
210 if (aa->aa_flags & AFA_PHASE2)
211 bcopy(atmulticastaddr, desten, sizeof atmulticastaddr);
212 else
213 bcopy(ac->ac_if.if_broadcastaddr, desten, ac->ac_if.if_addrlen);
214 return (1);
217 crit_enter();
218 AARPTAB_LOOK( aat, destsat->sat_addr );
219 if (aat == NULL) { /* No entry */
220 aat = aarptnew( &destsat->sat_addr );
221 if (aat == NULL) {
222 panic("aarpresolve: no free entry");
224 aat->aat_hold = m;
225 aarpwhohas(ac, destsat);
226 crit_exit();
227 return (0);
229 /* found an entry */
230 aat->aat_timer = 0;
231 if (aat->aat_flags & ATF_COM) { /* entry is COMplete */
232 bcopy(aat->aat_enaddr, desten, sizeof aat->aat_enaddr);
233 crit_exit();
234 return (1);
236 /* entry has not completed */
237 if (aat->aat_hold) {
238 m_freem(aat->aat_hold);
240 aat->aat_hold = m;
241 aarpwhohas(ac, destsat);
242 crit_exit();
243 return (0);
246 void
247 aarpintr(struct netmsg *msg)
249 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
250 struct arphdr *ar;
251 struct arpcom *ac;
253 ac = (struct arpcom *)m->m_pkthdr.rcvif;
254 if ( ac->ac_if.if_flags & IFF_NOARP )
255 goto out;
257 if ( m->m_len < sizeof( struct arphdr )) {
258 goto out;
261 ar = mtod( m, struct arphdr *);
262 if ( ntohs( ar->ar_hrd ) != AARPHRD_ETHER ) {
263 goto out;
266 if ( m->m_len < sizeof( struct arphdr ) + 2 * ar->ar_hln +
267 2 * ar->ar_pln ) {
268 goto out;
271 switch( ntohs( ar->ar_pro )) {
272 case ETHERTYPE_AT :
273 at_aarpinput( ac, m );
274 goto out2;
276 default:
277 break;
280 out:
281 m_freem(m);
282 out2:
284 /* msg was embedded in the mbuf, do not reply! */
287 static void
288 at_aarpinput( struct arpcom *ac, struct mbuf *m)
290 struct ether_aarp *ea;
291 struct at_ifaddr *aa = NULL;
292 struct aarptab *aat;
293 struct ether_header *eh;
294 struct llc *llc;
295 struct sockaddr_at sat;
296 struct sockaddr sa;
297 struct at_addr spa, tpa, ma;
298 int op;
299 u_short net;
301 ea = mtod( m, struct ether_aarp *);
303 /* Check to see if from my hardware address */
304 if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
305 sizeof( ac->ac_enaddr ))) {
306 m_freem( m );
307 return;
310 op = ntohs(ea->aarp_op);
311 bcopy(ea->aarp_tpnet, &net, sizeof net);
313 if ( net != 0 ) { /* should be ATADDR_ANYNET? */
314 sat.sat_len = sizeof(struct sockaddr_at);
315 sat.sat_family = AF_APPLETALK;
316 sat.sat_addr.s_net = net;
317 if ((aa = at_ifawithnet(&sat)) == NULL) {
318 m_freem( m );
319 return;
321 bcopy(ea->aarp_spnet, &spa.s_net, sizeof spa.s_net);
322 bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof tpa.s_net);
323 } else {
324 struct ifaddr_container *ifac;
327 * Since we don't know the net, we just look for the first
328 * phase 1 address on the interface.
330 TAILQ_FOREACH(ifac, &ac->ac_if.if_addrheads[mycpuid], ifa_link) {
331 aa = (struct at_ifaddr *)(ifac->ifa);
332 if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
333 ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
334 break;
337 if ( aa == NULL ) {
338 m_freem( m );
339 return;
341 tpa.s_net = spa.s_net = AA_SAT( aa )->sat_addr.s_net;
344 spa.s_node = ea->aarp_spnode;
345 tpa.s_node = ea->aarp_tpnode;
346 ma.s_net = AA_SAT( aa )->sat_addr.s_net;
347 ma.s_node = AA_SAT( aa )->sat_addr.s_node;
350 * This looks like it's from us.
352 if ( spa.s_net == ma.s_net && spa.s_node == ma.s_node ) {
353 if ( aa->aa_flags & AFA_PROBING ) {
355 * We're probing, someone either responded to our probe, or
356 * probed for the same address we'd like to use. Change the
357 * address we're probing for.
359 callout_stop(&aa->aa_ch);
360 wakeup( aa );
361 m_freem( m );
362 return;
363 } else if ( op != AARPOP_PROBE ) {
365 * This is not a probe, and we're not probing. This means
366 * that someone's saying they have the same source address
367 * as the one we're using. Get upset...
369 log( LOG_ERR,
370 "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
371 ea->aarp_sha[ 0 ], ea->aarp_sha[ 1 ], ea->aarp_sha[ 2 ],
372 ea->aarp_sha[ 3 ], ea->aarp_sha[ 4 ], ea->aarp_sha[ 5 ]);
373 m_freem( m );
374 return;
378 AARPTAB_LOOK( aat, spa );
379 if ( aat ) {
380 if ( op == AARPOP_PROBE ) {
382 * Someone's probing for spa, dealocate the one we've got,
383 * so that if the prober keeps the address, we'll be able
384 * to arp for him.
386 aarptfree( aat );
387 m_freem( m );
388 return;
391 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
392 sizeof( ea->aarp_sha ));
393 aat->aat_flags |= ATF_COM;
394 if ( aat->aat_hold ) {
395 struct mbuf *mhold = aat->aat_hold;
396 aat->aat_hold = NULL;
397 sat.sat_len = sizeof(struct sockaddr_at);
398 sat.sat_family = AF_APPLETALK;
399 sat.sat_addr = spa;
400 ac->ac_if.if_output(&ac->ac_if, mhold,
401 (struct sockaddr *)&sat, NULL); /* XXX */
403 } else if ((tpa.s_net == ma.s_net)
404 && (tpa.s_node == ma.s_node)
405 && (op != AARPOP_PROBE)
406 && ((aat = aarptnew( &spa )) != NULL)) {
407 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
408 sizeof( ea->aarp_sha ));
409 aat->aat_flags |= ATF_COM;
413 * Don't respond to responses, and never respond if we're
414 * still probing.
416 if ( tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
417 op == AARPOP_RESPONSE || ( aa->aa_flags & AFA_PROBING )) {
418 m_freem( m );
419 return;
422 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )ea->aarp_tha,
423 sizeof( ea->aarp_sha ));
424 bcopy(( caddr_t )ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
425 sizeof( ea->aarp_sha ));
427 /* XXX */
428 eh = (struct ether_header *)sa.sa_data;
429 bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )eh->ether_dhost,
430 sizeof( eh->ether_dhost ));
432 if ( aa->aa_flags & AFA_PHASE2 ) {
433 eh->ether_type = htons( sizeof( struct llc ) +
434 sizeof( struct ether_aarp ));
435 M_PREPEND( m, sizeof( struct llc ), MB_DONTWAIT );
436 if ( m == NULL ) {
437 return;
439 llc = mtod( m, struct llc *);
440 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
441 llc->llc_control = LLC_UI;
442 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
443 llc->llc_ether_type = htons( ETHERTYPE_AARP );
445 bcopy( ea->aarp_spnet, ea->aarp_tpnet, sizeof( ea->aarp_tpnet ));
446 bcopy( &ma.s_net, ea->aarp_spnet, sizeof( ea->aarp_spnet ));
447 } else {
448 eh->ether_type = htons( ETHERTYPE_AARP );
451 ea->aarp_tpnode = ea->aarp_spnode;
452 ea->aarp_spnode = ma.s_node;
453 ea->aarp_op = htons( AARPOP_RESPONSE );
455 sa.sa_len = sizeof( struct sockaddr );
456 sa.sa_family = AF_UNSPEC;
457 ac->ac_if.if_output(&ac->ac_if, m, &sa, NULL); /* XXX */
458 return;
461 static void
462 aarptfree(struct aarptab *aat)
465 if ( aat->aat_hold )
466 m_freem( aat->aat_hold );
467 aat->aat_hold = NULL;
468 aat->aat_timer = aat->aat_flags = 0;
469 aat->aat_ataddr.s_net = 0;
470 aat->aat_ataddr.s_node = 0;
473 struct aarptab *
474 aarptnew(struct at_addr *addr)
476 int n;
477 int oldest = -1;
478 struct aarptab *aat, *aato = NULL;
479 static int first = 1;
481 if ( first ) {
482 first = 0;
483 callout_init(&aarptimer_ch);
484 callout_reset(&aarptimer_ch, hz, aarptimer, NULL);
486 aat = &aarptab[ AARPTAB_HASH( *addr ) * AARPTAB_BSIZ ];
487 for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) {
488 if ( aat->aat_flags == 0 )
489 goto out;
490 if ( aat->aat_flags & ATF_PERM )
491 continue;
492 if ((int) aat->aat_timer > oldest ) {
493 oldest = aat->aat_timer;
494 aato = aat;
497 if ( aato == NULL )
498 return( NULL );
499 aat = aato;
500 aarptfree( aat );
501 out:
502 aat->aat_ataddr = *addr;
503 aat->aat_flags = ATF_INUSE;
504 return( aat );
508 void
509 aarpprobe(void *arg)
511 struct arpcom *ac = arg;
512 struct mbuf *m;
513 struct ether_header *eh;
514 struct ether_aarp *ea;
515 struct ifaddr_container *ifac;
516 struct at_ifaddr *aa = NULL;
517 struct llc *llc;
518 struct sockaddr sa;
521 * We need to check whether the output ethernet type should
522 * be phase 1 or 2. We have the interface that we'll be sending
523 * the aarp out. We need to find an AppleTalk network on that
524 * interface with the same address as we're looking for. If the
525 * net is phase 2, generate an 802.2 and SNAP header.
527 TAILQ_FOREACH(ifac, &ac->ac_if.if_addrheads[mycpuid], ifa_link) {
528 aa = (struct at_ifaddr *)(ifac->ifa);
529 if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
530 ( aa->aa_flags & AFA_PROBING )) {
531 break;
534 if ( aa == NULL ) { /* serious error XXX */
535 kprintf( "aarpprobe why did this happen?!\n" );
536 return;
539 if ( aa->aa_probcnt <= 0 ) {
540 aa->aa_flags &= ~AFA_PROBING;
541 wakeup( aa );
542 return;
543 } else {
544 callout_reset(&aa->aa_ch, hz / 5, aarpprobe, ac);
547 if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
548 return;
550 m->m_len = sizeof( *ea );
551 m->m_pkthdr.len = sizeof( *ea );
552 MH_ALIGN( m, sizeof( *ea ));
554 ea = mtod( m, struct ether_aarp *);
555 bzero((caddr_t)ea, sizeof( *ea ));
557 ea->aarp_hrd = htons( AARPHRD_ETHER );
558 ea->aarp_pro = htons( ETHERTYPE_AT );
559 ea->aarp_hln = sizeof( ea->aarp_sha );
560 ea->aarp_pln = sizeof( ea->aarp_spu );
561 ea->aarp_op = htons( AARPOP_PROBE );
562 bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
563 sizeof( ea->aarp_sha ));
565 eh = (struct ether_header *)sa.sa_data;
567 if ( aa->aa_flags & AFA_PHASE2 ) {
568 bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
569 sizeof( eh->ether_dhost ));
570 eh->ether_type = htons( sizeof( struct llc ) +
571 sizeof( struct ether_aarp ));
572 M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
573 /* XXX-MBUF */
574 llc = mtod( m, struct llc *);
575 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
576 llc->llc_control = LLC_UI;
577 bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
578 llc->llc_ether_type = htons( ETHERTYPE_AARP );
580 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
581 sizeof( ea->aarp_spnet ));
582 bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_tpnet,
583 sizeof( ea->aarp_tpnet ));
584 ea->aarp_spnode = ea->aarp_tpnode = AA_SAT( aa )->sat_addr.s_node;
585 } else {
586 bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
587 ac->ac_if.if_addrlen);
588 eh->ether_type = htons( ETHERTYPE_AARP );
589 ea->aarp_spa = ea->aarp_tpa = AA_SAT( aa )->sat_addr.s_node;
592 #ifdef NETATALKDEBUG
593 kprintf("aarp: sending probe for %u.%u\n",
594 ntohs(AA_SAT( aa )->sat_addr.s_net),
595 AA_SAT( aa )->sat_addr.s_node);
596 #endif /* NETATALKDEBUG */
598 sa.sa_len = sizeof( struct sockaddr );
599 sa.sa_family = AF_UNSPEC;
600 ac->ac_if.if_output(&ac->ac_if, m, &sa, NULL); /* XXX */
601 aa->aa_probcnt--;
604 void
605 aarp_clean(void)
607 struct aarptab *aat;
608 int i;
610 callout_stop(&aarptimer_ch);
611 for ( i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++ ) {
612 if ( aat->aat_hold ) {
613 m_freem( aat->aat_hold );
614 aat->aat_hold = NULL;