Merge from vendor branch BINUTILS:
[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.16 2005/06/10 22:43:58 dillon 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;
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         crit_enter();
87         aarptfree( aat );
88         crit_exit();
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
207     if (at_broadcast(destsat)) {
208         m->m_flags |= M_BCAST;
209         if ((aa = at_ifawithnet(destsat)) == NULL)  {
210             m_freem(m);
211             return (0);
212         }
213         if (aa->aa_flags & AFA_PHASE2)
214             bcopy(atmulticastaddr, desten, sizeof atmulticastaddr);
215         else
216             bcopy(ac->ac_if.if_broadcastaddr, desten, ac->ac_if.if_addrlen);
217         return (1);
218     }
219
220     crit_enter();
221     AARPTAB_LOOK( aat, destsat->sat_addr );
222     if (aat == NULL) {                  /* No entry */
223         aat = aarptnew( &destsat->sat_addr );
224         if (aat == NULL) {
225             panic("aarpresolve: no free entry");
226         }
227         aat->aat_hold = m;
228         aarpwhohas(ac, destsat);
229         crit_exit();
230         return (0);
231     }
232     /* found an entry */
233     aat->aat_timer = 0;
234     if (aat->aat_flags & ATF_COM) {     /* entry is COMplete */
235         bcopy(aat->aat_enaddr, desten, sizeof aat->aat_enaddr);
236         crit_exit();
237         return (1);
238     }
239     /* entry has not completed */
240     if (aat->aat_hold) {
241         m_freem(aat->aat_hold);
242     }
243     aat->aat_hold = m;
244     aarpwhohas(ac, destsat);
245     crit_exit();
246     return (0);
247 }
248
249 int
250 aarpintr(struct netmsg *msg)
251 {
252     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;   
253     struct arphdr       *ar;
254     struct arpcom       *ac;
255
256     ac = (struct arpcom *)m->m_pkthdr.rcvif;
257     if ( ac->ac_if.if_flags & IFF_NOARP )
258         goto out;
259
260     if ( m->m_len < sizeof( struct arphdr )) {
261         goto out;
262     }
263
264     ar = mtod( m, struct arphdr *);
265     if ( ntohs( ar->ar_hrd ) != AARPHRD_ETHER ) {
266         goto out;
267     }
268     
269     if ( m->m_len < sizeof( struct arphdr ) + 2 * ar->ar_hln +
270             2 * ar->ar_pln ) {
271         goto out;
272     }
273     
274     switch( ntohs( ar->ar_pro )) {
275     case ETHERTYPE_AT :
276         at_aarpinput( ac, m );
277         goto out2;
278
279     default:
280         break;
281     }
282
283 out:
284     m_freem(m);
285 out2:
286     lwkt_replymsg(&msg->nm_lmsg, 0);
287     return(EASYNC);
288 }
289
290 static void
291 at_aarpinput( struct arpcom *ac, struct mbuf *m)
292 {
293     struct ether_aarp   *ea;
294     struct ifaddr       *ifa;
295     struct at_ifaddr    *aa = NULL;
296     struct aarptab      *aat;
297     struct ether_header *eh;
298     struct llc          *llc;
299     struct sockaddr_at  sat;
300     struct sockaddr     sa;
301     struct at_addr      spa, tpa, ma;
302     int                 op;
303     u_short             net;
304
305     ea = mtod( m, struct ether_aarp *);
306
307     /* Check to see if from my hardware address */
308     if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
309             sizeof( ac->ac_enaddr ))) {
310         m_freem( m );
311         return;
312     }
313
314     op = ntohs(ea->aarp_op);
315     bcopy(ea->aarp_tpnet, &net, sizeof net);
316
317     if ( net != 0 ) { /* should be ATADDR_ANYNET? */
318         sat.sat_len = sizeof(struct sockaddr_at);
319         sat.sat_family = AF_APPLETALK;
320         sat.sat_addr.s_net = net;
321         if ((aa = at_ifawithnet(&sat)) == NULL) {
322             m_freem( m );
323             return;
324         }
325         bcopy(ea->aarp_spnet, &spa.s_net, sizeof spa.s_net);
326         bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof tpa.s_net);
327     } else {
328         /*
329          * Since we don't know the net, we just look for the first
330          * phase 1 address on the interface.
331          */
332         TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
333             aa = (struct at_ifaddr *)ifa;
334             if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
335                     ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
336                 break;
337             }
338         }
339         if ( aa == NULL ) {
340             m_freem( m );
341             return;
342         }
343         tpa.s_net = spa.s_net = AA_SAT( aa )->sat_addr.s_net;
344     }
345
346     spa.s_node = ea->aarp_spnode;
347     tpa.s_node = ea->aarp_tpnode;
348     ma.s_net = AA_SAT( aa )->sat_addr.s_net;
349     ma.s_node = AA_SAT( aa )->sat_addr.s_node;
350
351     /*
352      * This looks like it's from us.
353      */
354     if ( spa.s_net == ma.s_net && spa.s_node == ma.s_node ) {
355         if ( aa->aa_flags & AFA_PROBING ) {
356             /*
357              * We're probing, someone either responded to our probe, or
358              * probed for the same address we'd like to use. Change the
359              * address we're probing for.
360              */
361             callout_stop(&aa->aa_ch);
362             wakeup( aa );
363             m_freem( m );
364             return;
365         } else if ( op != AARPOP_PROBE ) {
366             /*
367              * This is not a probe, and we're not probing. This means
368              * that someone's saying they have the same source address
369              * as the one we're using. Get upset...
370              */
371             log( LOG_ERR,
372                     "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
373                     ea->aarp_sha[ 0 ], ea->aarp_sha[ 1 ], ea->aarp_sha[ 2 ],
374                     ea->aarp_sha[ 3 ], ea->aarp_sha[ 4 ], ea->aarp_sha[ 5 ]);
375             m_freem( m );
376             return;
377         }
378     }
379
380     AARPTAB_LOOK( aat, spa );
381     if ( aat ) {
382         if ( op == AARPOP_PROBE ) {
383             /*
384              * Someone's probing for spa, dealocate the one we've got,
385              * so that if the prober keeps the address, we'll be able
386              * to arp for him.
387              */
388             aarptfree( aat );
389             m_freem( m );
390             return;
391         }
392
393         bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
394                 sizeof( ea->aarp_sha ));
395         aat->aat_flags |= ATF_COM;
396         if ( aat->aat_hold ) {
397             struct mbuf *mhold = aat->aat_hold;
398             aat->aat_hold = NULL;
399             sat.sat_len = sizeof(struct sockaddr_at);
400             sat.sat_family = AF_APPLETALK;
401             sat.sat_addr = spa;
402             (*ac->ac_if.if_output)( &ac->ac_if, mhold,
403                     (struct sockaddr *)&sat, NULL); /* XXX */
404         }
405     } else if ((tpa.s_net == ma.s_net)
406            && (tpa.s_node == ma.s_node)
407            && (op != AARPOP_PROBE)
408            && ((aat = aarptnew( &spa )) != NULL)) {
409                 bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
410                     sizeof( ea->aarp_sha ));
411                 aat->aat_flags |= ATF_COM;
412     }
413
414     /*
415      * Don't respond to responses, and never respond if we're
416      * still probing.
417      */
418     if ( tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
419             op == AARPOP_RESPONSE || ( aa->aa_flags & AFA_PROBING )) {
420         m_freem( m );
421         return;
422     }
423
424     bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )ea->aarp_tha,
425             sizeof( ea->aarp_sha ));
426     bcopy(( caddr_t )ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
427             sizeof( ea->aarp_sha ));
428
429     /* XXX */
430     eh = (struct ether_header *)sa.sa_data;
431     bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )eh->ether_dhost,
432             sizeof( eh->ether_dhost ));
433
434     if ( aa->aa_flags & AFA_PHASE2 ) {
435         eh->ether_type = htons( sizeof( struct llc ) +
436                 sizeof( struct ether_aarp ));
437         M_PREPEND( m, sizeof( struct llc ), MB_DONTWAIT );
438         if ( m == NULL ) {
439             return;
440         }
441         llc = mtod( m, struct llc *);
442         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
443         llc->llc_control = LLC_UI;
444         bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
445         llc->llc_ether_type = htons( ETHERTYPE_AARP );
446
447         bcopy( ea->aarp_spnet, ea->aarp_tpnet, sizeof( ea->aarp_tpnet ));
448         bcopy( &ma.s_net, ea->aarp_spnet, sizeof( ea->aarp_spnet ));
449     } else {
450         eh->ether_type = htons( ETHERTYPE_AARP );
451     }
452
453     ea->aarp_tpnode = ea->aarp_spnode;
454     ea->aarp_spnode = ma.s_node;
455     ea->aarp_op = htons( AARPOP_RESPONSE );
456
457     sa.sa_len = sizeof( struct sockaddr );
458     sa.sa_family = AF_UNSPEC;
459     (*ac->ac_if.if_output)( &ac->ac_if, m, &sa, NULL); /* XXX */
460     return;
461 }
462
463 static void
464 aarptfree( struct aarptab *aat)
465 {
466
467     if ( aat->aat_hold )
468         m_freem( aat->aat_hold );
469     aat->aat_hold = NULL;
470     aat->aat_timer = aat->aat_flags = 0;
471     aat->aat_ataddr.s_net = 0;
472     aat->aat_ataddr.s_node = 0;
473 }
474
475     struct aarptab *
476 aarptnew( addr )
477     struct at_addr      *addr;
478 {
479     int                 n;
480     int                 oldest = -1;
481     struct aarptab      *aat, *aato = NULL;
482     static int          first = 1;
483
484     if ( first ) {
485         first = 0;
486         callout_init(&aarptimer_ch);
487         callout_reset(&aarptimer_ch, hz, aarptimer, NULL);
488     }
489     aat = &aarptab[ AARPTAB_HASH( *addr ) * AARPTAB_BSIZ ];
490     for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) {
491         if ( aat->aat_flags == 0 )
492             goto out;
493         if ( aat->aat_flags & ATF_PERM )
494             continue;
495         if ((int) aat->aat_timer > oldest ) {
496             oldest = aat->aat_timer;
497             aato = aat;
498         }
499     }
500     if ( aato == NULL )
501         return( NULL );
502     aat = aato;
503     aarptfree( aat );
504 out:
505     aat->aat_ataddr = *addr;
506     aat->aat_flags = ATF_INUSE;
507     return( aat );
508 }
509
510
511 void
512 aarpprobe( void *arg )
513 {
514     struct arpcom       *ac = arg;
515     struct mbuf         *m;
516     struct ether_header *eh;
517     struct ether_aarp   *ea;
518     struct ifaddr       *ifa;
519     struct at_ifaddr    *aa = NULL;
520     struct llc          *llc;
521     struct sockaddr     sa;
522
523     /*
524      * We need to check whether the output ethernet type should
525      * be phase 1 or 2. We have the interface that we'll be sending
526      * the aarp out. We need to find an AppleTalk network on that
527      * interface with the same address as we're looking for. If the
528      * net is phase 2, generate an 802.2 and SNAP header.
529      */
530     TAILQ_FOREACH(ifa, &ac->ac_if.if_addrhead, ifa_link) {
531         aa = (struct at_ifaddr *)ifa;
532         if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
533                 ( aa->aa_flags & AFA_PROBING )) {
534             break;
535         }
536     }
537     if ( aa == NULL ) {         /* serious error XXX */
538         printf( "aarpprobe why did this happen?!\n" );
539         return;
540     }
541
542     if ( aa->aa_probcnt <= 0 ) {
543         aa->aa_flags &= ~AFA_PROBING;
544         wakeup( aa );
545         return;
546     } else {
547         callout_reset(&aa->aa_ch, hz / 5, aarpprobe, ac);
548     }
549
550     if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
551         return;
552     }
553     m->m_len = sizeof( *ea );
554     m->m_pkthdr.len = sizeof( *ea );
555     MH_ALIGN( m, sizeof( *ea ));
556
557     ea = mtod( m, struct ether_aarp *);
558     bzero((caddr_t)ea, sizeof( *ea ));
559
560     ea->aarp_hrd = htons( AARPHRD_ETHER );
561     ea->aarp_pro = htons( ETHERTYPE_AT );
562     ea->aarp_hln = sizeof( ea->aarp_sha );
563     ea->aarp_pln = sizeof( ea->aarp_spu );
564     ea->aarp_op = htons( AARPOP_PROBE );
565     bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
566             sizeof( ea->aarp_sha ));
567
568     eh = (struct ether_header *)sa.sa_data;
569
570     if ( aa->aa_flags & AFA_PHASE2 ) {
571         bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
572                 sizeof( eh->ether_dhost ));
573         eh->ether_type = htons( sizeof( struct llc ) +
574                 sizeof( struct ether_aarp ));
575         M_PREPEND( m, sizeof( struct llc ), MB_WAIT );
576         /* XXX-MBUF */
577         llc = mtod( m, struct llc *);
578         llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
579         llc->llc_control = LLC_UI;
580         bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
581         llc->llc_ether_type = htons( ETHERTYPE_AARP );
582
583         bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
584                 sizeof( ea->aarp_spnet ));
585         bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_tpnet,
586                 sizeof( ea->aarp_tpnet ));
587         ea->aarp_spnode = ea->aarp_tpnode = AA_SAT( aa )->sat_addr.s_node;
588     } else {
589         bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
590               ac->ac_if.if_addrlen);
591         eh->ether_type = htons( ETHERTYPE_AARP );
592         ea->aarp_spa = ea->aarp_tpa = AA_SAT( aa )->sat_addr.s_node;
593     }
594
595 #ifdef NETATALKDEBUG
596     printf("aarp: sending probe for %u.%u\n",
597            ntohs(AA_SAT( aa )->sat_addr.s_net),
598            AA_SAT( aa )->sat_addr.s_node);
599 #endif /* NETATALKDEBUG */
600
601     sa.sa_len = sizeof( struct sockaddr );
602     sa.sa_family = AF_UNSPEC;
603     (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, NULL); /* XXX */
604     aa->aa_probcnt--;
605 }
606
607 void
608 aarp_clean(void)
609 {
610     struct aarptab      *aat;
611     int                 i;
612
613     callout_stop(&aarptimer_ch);
614     for ( i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++ ) {
615         if ( aat->aat_hold ) {
616             m_freem( aat->aat_hold );
617             aat->aat_hold = NULL;
618         }
619     }
620 }