Merge from vendor branch DHCP:
[dragonfly.git] / sys / netproto / atalk / aarp.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
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.15 2004/12/21 02:54:48 hsu Exp $
7  */
8
9 #include "opt_atalk.h"
10
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>
17
18 #include <sys/thread2.h>
19 #include <sys/msgport2.h>
20
21 #include <net/if.h>
22 #include <net/netisr.h>
23
24 #include <netinet/in.h>
25 #undef s_net
26 #include <netinet/if_ether.h>
27
28 #include "at.h"
29 #include "at_var.h"
30 #include "aarp.h"
31 #include "phase2.h"
32 #include "at_extern.h"
33
34 static void aarptfree( struct aarptab *aat);
35 static void at_aarpinput( struct arpcom *ac, struct mbuf *m);
36
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];
41
42 #define AARPTAB_HASH(a) \
43     ((((a).s_net << 8 ) + (a).s_node ) % AARPTAB_NB )
44
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; \
54 }
55
56 #define AARPT_AGE       (60 * 1)
57 #define AARPT_KILLC     20
58 #define AARPT_KILLI     3
59
60 static u_char atmulticastaddr[ 6 ] = {
61     0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
62 };
63
64 u_char  at_org_code[ 3 ] = {
65     0x08, 0x00, 0x07,
66 };
67 u_char  aarp_org_code[ 3 ] = {
68     0x00, 0x00, 0x00,
69 };
70
71 static struct callout aarptimer_ch;
72
73 static void
74 aarptimer(void *ignored)
75 {
76     struct aarptab      *aat;
77     int                 i, s;
78
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         s = splimp();
87         aarptfree( aat );
88         splx( s );
89     }
90     callout_reset(&aarptimer_ch, AARPT_AGE * hz, aarptimer, NULL);
91 }
92
93 /* 
94  * search through the network addresses to find one that includes
95  * the given network.. remember to take netranges into
96  * consideration.
97  */
98 struct at_ifaddr *
99 at_ifawithnet(struct sockaddr_at  *sat )
100 {
101     struct at_ifaddr    *aa;
102     struct sockaddr_at  *sat2;
103
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;
108                 }
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;
113                 }
114         }
115         return( aa );
116 }
117
118 static void
119 aarpwhohas( struct arpcom *ac, struct sockaddr_at *sat )
120 {
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;
127
128     if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
129         return;
130     }
131     m->m_len = sizeof( *ea );
132     m->m_pkthdr.len = sizeof( *ea );
133     MH_ALIGN( m, sizeof( *ea ));
134
135     ea = mtod( m, struct ether_aarp *);
136     bzero((caddr_t)ea, sizeof( *ea ));
137
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);
144
145     /*
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.
151      */
152     if ((aa = at_ifawithnet( sat )) == NULL) {
153         m_freem( m );
154         return;
155     }
156
157     eh = (struct ether_header *)sa.sa_data;
158
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 );
169
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 );
180
181         ea->aarp_spa = AA_SAT( aa )->sat_addr.s_node;
182         ea->aarp_tpa = sat->sat_addr.s_node;
183     }
184
185 #ifdef NETATALKDEBUG
186     printf("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 */
190
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 */
195 }
196
197 int
198 aarpresolve( ac, m, destsat, desten )
199     struct arpcom       *ac;
200     struct mbuf         *m;
201     struct sockaddr_at  *destsat;
202     u_char              *desten;
203 {
204     struct at_ifaddr    *aa;
205     struct aarptab      *aat;
206     int                 s;
207
208     if (at_broadcast(destsat)) {
209         m->m_flags |= M_BCAST;
210         if ((aa = at_ifawithnet(destsat)) == NULL)  {
211             m_freem(m);
212             return (0);
213         }
214         if (aa->aa_flags & AFA_PHASE2)
215             bcopy(atmulticastaddr, desten, sizeof atmulticastaddr);
216         else
217             bcopy(ac->ac_if.if_broadcastaddr, desten, ac->ac_if.if_addrlen);
218         return (1);
219     }
220
221     s = splimp();
222     AARPTAB_LOOK( aat, destsat->sat_addr );
223     if (aat == NULL) {                  /* No entry */
224         aat = aarptnew( &destsat->sat_addr );
225         if (aat == NULL) {
226             panic("aarpresolve: no free entry");
227         }
228         aat->aat_hold = m;
229         aarpwhohas(ac, destsat);
230         splx(s);
231         return (0);
232     }
233     /* found an entry */
234     aat->aat_timer = 0;
235     if (aat->aat_flags & ATF_COM) {     /* entry is COMplete */
236         bcopy(aat->aat_enaddr, desten, sizeof aat->aat_enaddr);
237         splx(s);
238         return (1);
239     }
240     /* entry has not completed */
241     if (aat->aat_hold) {
242         m_freem(aat->aat_hold);
243     }
244     aat->aat_hold = m;
245     aarpwhohas(ac, destsat);
246     splx(s);
247     return (0);
248 }
249
250 int
251 aarpintr(struct netmsg *msg)
252 {
253     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;   
254     struct arphdr       *ar;
255     struct arpcom       *ac;
256
257     ac = (struct arpcom *)m->m_pkthdr.rcvif;
258     if ( ac->ac_if.if_flags & IFF_NOARP )
259         goto out;
260
261     if ( m->m_len < sizeof( struct arphdr )) {
262         goto out;
263     }
264
265     ar = mtod( m, struct arphdr *);
266     if ( ntohs( ar->ar_hrd ) != AARPHRD_ETHER ) {
267         goto out;
268     }
269     
270     if ( m->m_len < sizeof( struct arphdr ) + 2 * ar->ar_hln +
271             2 * ar->ar_pln ) {
272         goto out;
273     }
274     
275     switch( ntohs( ar->ar_pro )) {
276     case ETHERTYPE_AT :
277         at_aarpinput( ac, m );
278         goto out2;
279
280     default:
281         break;
282     }
283
284 out:
285     m_freem(m);
286 out2:
287     lwkt_replymsg(&msg->nm_lmsg, 0);
288     return(EASYNC);
289 }
290
291 static void
292 at_aarpinput( struct arpcom *ac, struct mbuf *m)
293 {
294     struct ether_aarp   *ea;
295     struct ifaddr       *ifa;
296     struct at_ifaddr    *aa = NULL;
297     struct aarptab      *aat;
298     struct ether_header *eh;
299     struct llc          *llc;
300     struct sockaddr_at  sat;
301     struct sockaddr     sa;
302     struct at_addr      spa, tpa, ma;
303     int                 op;
304     u_short             net;
305
306     ea = mtod( m, struct ether_aarp *);
307
308     /* Check to see if from my hardware address */
309     if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
310             sizeof( ac->ac_enaddr ))) {
311         m_freem( m );
312         return;
313     }
314
315     op = ntohs(ea->aarp_op);
316     bcopy(ea->aarp_tpnet, &net, sizeof net);
317
318     if ( net != 0 ) { /* should be ATADDR_ANYNET? */
319         sat.sat_len = sizeof(struct sockaddr_at);
320         sat.sat_family = AF_APPLETALK;
321         sat.sat_addr.s_net = net;
322         if ((aa = at_ifawithnet(&sat)) == NULL) {
323             m_freem( m );
324             return;
325         }
326         bcopy(ea->aarp_spnet, &spa.s_net, sizeof spa.s_net);
327         bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof tpa.s_net);
328     } else {
329         /*
330          * Since we don't know the net, we just look for the first
331          * phase 1 address on the interface.
332          */
333         TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
334             aa = (struct at_ifaddr *)ifa;
335             if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
336                     ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
337                 break;
338             }
339         }
340         if ( aa == NULL ) {
341             m_freem( m );
342             return;
343         }
344         tpa.s_net = spa.s_net = AA_SAT( aa )->sat_addr.s_net;
345     }
346
347     spa.s_node = ea->aarp_spnode;
348     tpa.s_node = ea->aarp_tpnode;
349     ma.s_net = AA_SAT( aa )->sat_addr.s_net;
350     ma.s_node = AA_SAT( aa )->sat_addr.s_node;
351
352     /*
353      * This looks like it's from us.
354      */
355     if ( spa.s_net == ma.s_net && spa.s_node == ma.s_node ) {
356         if ( aa->aa_flags & AFA_PROBING ) {
357             /*
358              * We're probing, someone either responded to our probe, or
359              * probed for the same address we'd like to use. Change the
360              * address we're probing for.
361              */
362             callout_stop(&aa->aa_ch);
363             wakeup( aa );
364             m_freem( m );
365             return;
366         } else if ( op != AARPOP_PROBE ) {
367             /*
368              * This is not a probe, and we're not probing. This means
369              * that someone's saying they have the same source address
370              * as the one we're using. Get upset...
371              */
372             log( LOG_ERR,
373                     "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
374                     ea->aarp_sha[ 0 ], ea->aarp_sha[ 1 ], ea->aarp_sha[ 2 ],
375                     ea->aarp_sha[ 3 ], ea->aarp_sha[ 4 ], ea->aarp_sha[ 5 ]);
376             m_freem( m );
377             return;
378         }
379     }
380
381     AARPTAB_LOOK( aat, spa );
382     if ( aat ) {
383         if ( op == AARPOP_PROBE ) {
384             /*
385              * Someone's probing for spa, dealocate the one we've got,
386              * so that if the prober keeps the address, we'll be able
387              * to arp for him.
388              */
389             aarptfree( aat );
390             m_freem( m );
391             return;
392         }
393
394         bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
395                 sizeof( ea->aarp_sha ));
396         aat->aat_flags |= ATF_COM;
397         if ( aat->aat_hold ) {
398             struct mbuf *mhold = aat->aat_hold;
399             aat->aat_hold = NULL;
400             sat.sat_len = sizeof(struct sockaddr_at);
401             sat.sat_family = AF_APPLETALK;
402             sat.sat_addr = spa;
403             (*ac->ac_if.if_output)( &ac->ac_if, mhold,
404                     (struct sockaddr *)&sat, NULL); /* XXX */
405         }
406     } else if ((tpa.s_net == ma.s_net)
407            && (tpa.s_node == ma.s_node)
408            && (op != AARPOP_PROBE)
409            && ((aat = aarptnew( &spa )) != NULL)) {
410                 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
411                     sizeof( ea->aarp_sha ));
412                 aat->aat_flags |= ATF_COM;
413     }
414
415     /*
416      * Don't respond to responses, and never respond if we're
417      * still probing.
418      */
419     if ( tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
420             op == AARPOP_RESPONSE || ( aa->aa_flags & AFA_PROBING )) {
421         m_freem( m );
422         return;
423     }
424
425     bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )ea->aarp_tha,
426             sizeof( ea->aarp_sha ));
427     bcopy(( caddr_t )ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
428             sizeof( ea->aarp_sha ));
429
430     /* XXX */
431     eh = (struct ether_header *)sa.sa_data;
432     bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )eh->ether_dhost,
433             sizeof( eh->ether_dhost ));
434
435     if ( aa->aa_flags & AFA_PHASE2 ) {
436         eh->ether_type = htons( sizeof( struct llc ) +
437                 sizeof( struct ether_aarp ));
438         M_PREPEND( m, sizeof( struct llc ), MB_DONTWAIT );
439         if ( m == NULL ) {
440             return;
441         }
442         llc = mtod( m, struct llc *);
443         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
444         llc->llc_control = LLC_UI;
445         bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
446         llc->llc_ether_type = htons( ETHERTYPE_AARP );
447
448         bcopy( ea->aarp_spnet, ea->aarp_tpnet, sizeof( ea->aarp_tpnet ));
449         bcopy( &ma.s_net, ea->aarp_spnet, sizeof( ea->aarp_spnet ));
450     } else {
451         eh->ether_type = htons( ETHERTYPE_AARP );
452     }
453
454     ea->aarp_tpnode = ea->aarp_spnode;
455     ea->aarp_spnode = ma.s_node;
456     ea->aarp_op = htons( AARPOP_RESPONSE );
457
458     sa.sa_len = sizeof( struct sockaddr );
459     sa.sa_family = AF_UNSPEC;
460     (*ac->ac_if.if_output)( &ac->ac_if, m, &sa, NULL); /* XXX */
461     return;
462 }
463
464 static void
465 aarptfree( struct aarptab *aat)
466 {
467
468     if ( aat->aat_hold )
469         m_freem( aat->aat_hold );
470     aat->aat_hold = NULL;
471     aat->aat_timer = aat->aat_flags = 0;
472     aat->aat_ataddr.s_net = 0;
473     aat->aat_ataddr.s_node = 0;
474 }
475
476     struct aarptab *
477 aarptnew( addr )
478     struct at_addr      *addr;
479 {
480     int                 n;
481     int                 oldest = -1;
482     struct aarptab      *aat, *aato = NULL;
483     static int          first = 1;
484
485     if ( first ) {
486         first = 0;
487         callout_init(&aarptimer_ch);
488         callout_reset(&aarptimer_ch, hz, aarptimer, NULL);
489     }
490     aat = &aarptab[ AARPTAB_HASH( *addr ) * AARPTAB_BSIZ ];
491     for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) {
492         if ( aat->aat_flags == 0 )
493             goto out;
494         if ( aat->aat_flags & ATF_PERM )
495             continue;
496         if ((int) aat->aat_timer > oldest ) {
497             oldest = aat->aat_timer;
498             aato = aat;
499         }
500     }
501     if ( aato == NULL )
502         return( NULL );
503     aat = aato;
504     aarptfree( aat );
505 out:
506     aat->aat_ataddr = *addr;
507     aat->aat_flags = ATF_INUSE;
508     return( aat );
509 }
510
511
512 void
513 aarpprobe( void *arg )
514 {
515     struct arpcom       *ac = arg;
516     struct mbuf         *m;
517     struct ether_header *eh;
518     struct ether_aarp   *ea;
519     struct ifaddr       *ifa;
520     struct at_ifaddr    *aa = NULL;
521     struct llc          *llc;
522     struct sockaddr     sa;
523
524     /*
525      * We need to check whether the output ethernet type should
526      * be phase 1 or 2. We have the interface that we'll be sending
527      * the aarp out. We need to find an AppleTalk network on that
528      * interface with the same address as we're looking for. If the
529      * net is phase 2, generate an 802.2 and SNAP header.
530      */
531     TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
532         aa = (struct at_ifaddr *)ifa;
533         if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
534                 ( aa->aa_flags & AFA_PROBING )) {
535             break;
536         }
537     }
538     if ( aa == NULL ) {         /* serious error XXX */
539         printf( "aarpprobe why did this happen?!\n" );
540         return;
541     }
542
543     if ( aa->aa_probcnt <= 0 ) {
544         aa->aa_flags &= ~AFA_PROBING;
545         wakeup( aa );
546         return;
547     } else {
548         callout_reset(&aa->aa_ch, hz / 5, aarpprobe, ac);
549     }
550
551     if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
552         return;
553     }
554     m->m_len = sizeof( *ea );
555     m->m_pkthdr.len = sizeof( *ea );
556     MH_ALIGN( m, sizeof( *ea ));
557
558     ea = mtod( m, struct ether_aarp *);
559     bzero((caddr_t)ea, sizeof( *ea ));
560
561     ea->aarp_hrd = htons( AARPHRD_ETHER );
562     ea->aarp_pro = htons( ETHERTYPE_AT );
563     ea->aarp_hln = sizeof( ea->aarp_sha );
564     ea->aarp_pln = sizeof( ea->aarp_spu );
565     ea->aarp_op = htons( AARPOP_PROBE );
566     bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
567             sizeof( ea->aarp_sha ));
568
569     eh = (struct ether_header *)sa.sa_data;
570
571     if ( aa->aa_flags & AFA_PHASE2 ) {
572         bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
573                 sizeof( eh->ether_dhost ));
574         eh->ether_type = htons( sizeof( struct llc ) +
575                 sizeof( struct ether_aarp ));
576         M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
577         /* XXX-MBUF */
578         llc = mtod( m, struct llc *);
579         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
580         llc->llc_control = LLC_UI;
581         bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
582         llc->llc_ether_type = htons( ETHERTYPE_AARP );
583
584         bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
585                 sizeof( ea->aarp_spnet ));
586         bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_tpnet,
587                 sizeof( ea->aarp_tpnet ));
588         ea->aarp_spnode = ea->aarp_tpnode = AA_SAT( aa )->sat_addr.s_node;
589     } else {
590         bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
591               ac->ac_if.if_addrlen);
592         eh->ether_type = htons( ETHERTYPE_AARP );
593         ea->aarp_spa = ea->aarp_tpa = AA_SAT( aa )->sat_addr.s_node;
594     }
595
596 #ifdef NETATALKDEBUG
597     printf("aarp: sending probe for %u.%u\n",
598            ntohs(AA_SAT( aa )->sat_addr.s_net),
599            AA_SAT( aa )->sat_addr.s_node);
600 #endif /* NETATALKDEBUG */
601
602     sa.sa_len = sizeof( struct sockaddr );
603     sa.sa_family = AF_UNSPEC;
604     (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, NULL); /* XXX */
605     aa->aa_probcnt--;
606 }
607
608 void
609 aarp_clean(void)
610 {
611     struct aarptab      *aat;
612     int                 i;
613
614     callout_stop(&aarptimer_ch);
615     for ( i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++ ) {
616         if ( aat->aat_hold ) {
617             m_freem( aat->aat_hold );
618             aat->aat_hold = NULL;
619         }
620     }
621 }