Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.24 2008/05/14 11:59:24 sephe 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 #include <sys/mplock2.h>
21
22 #include <net/if.h>
23 #include <net/netisr.h>
24
25 #include <netinet/in.h>
26 #undef s_net
27 #include <netinet/if_ether.h>
28
29 #include "at.h"
30 #include "at_var.h"
31 #include "aarp.h"
32 #include "phase2.h"
33 #include "at_extern.h"
34
35 static void aarptfree( struct aarptab *aat);
36 static void at_aarpinput( struct arpcom *ac, struct mbuf *m);
37
38 #define AARPTAB_BSIZ    9
39 #define AARPTAB_NB      19
40 #define AARPTAB_SIZE    (AARPTAB_BSIZ * AARPTAB_NB)
41 static struct aarptab   aarptab[AARPTAB_SIZE];
42
43 #define AARPTAB_HASH(a) \
44     ((((a).s_net << 8 ) + (a).s_node ) % AARPTAB_NB )
45
46 #define AARPTAB_LOOK(aat,addr) { \
47     int         n; \
48     aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
49     for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) \
50         if ( aat->aat_ataddr.s_net == (addr).s_net && \
51              aat->aat_ataddr.s_node == (addr).s_node ) \
52             break; \
53         if ( n >= AARPTAB_BSIZ ) \
54             aat = 0; \
55 }
56
57 #define AARPT_AGE       (60 * 1)
58 #define AARPT_KILLC     20
59 #define AARPT_KILLI     3
60
61 static u_char atmulticastaddr[ 6 ] = {
62     0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
63 };
64
65 u_char  at_org_code[ 3 ] = {
66     0x08, 0x00, 0x07,
67 };
68 u_char  aarp_org_code[ 3 ] = {
69     0x00, 0x00, 0x00,
70 };
71
72 static struct callout aarptimer_ch;
73
74 static void
75 aarptimer(void *ignored)
76 {
77     struct aarptab      *aat;
78     int                 i;
79
80     aat = aarptab;
81     for ( i = 0; i < AARPTAB_SIZE; i++, aat++ ) {
82         if ( aat->aat_flags == 0 || ( aat->aat_flags & ATF_PERM ))
83             continue;
84         if ( ++aat->aat_timer < (( aat->aat_flags & ATF_COM ) ?
85                 AARPT_KILLC : AARPT_KILLI ))
86             continue;
87         crit_enter();
88         aarptfree( aat );
89         crit_exit();
90     }
91     callout_reset(&aarptimer_ch, AARPT_AGE * hz, aarptimer, NULL);
92 }
93
94 /* 
95  * search through the network addresses to find one that includes
96  * the given network.. remember to take netranges into
97  * consideration.
98  */
99 struct at_ifaddr *
100 at_ifawithnet(struct sockaddr_at *sat)
101 {
102     struct at_ifaddr    *aa;
103     struct sockaddr_at  *sat2;
104
105         for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
106                 sat2 = &(aa->aa_addr);
107                 if ( sat2->sat_addr.s_net == sat->sat_addr.s_net ) {
108                         break;
109                 }
110                 if( (aa->aa_flags & AFA_PHASE2 )
111                 && (ntohs(aa->aa_firstnet) <= ntohs(sat->sat_addr.s_net))
112                 && (ntohs(aa->aa_lastnet) >= ntohs(sat->sat_addr.s_net))) {
113                         break;
114                 }
115         }
116         return( aa );
117 }
118
119 static void
120 aarpwhohas(struct arpcom *ac, struct sockaddr_at *sat)
121 {
122     struct mbuf         *m;
123     struct ether_header *eh;
124     struct ether_aarp   *ea;
125     struct at_ifaddr    *aa;
126     struct llc          *llc;
127     struct sockaddr     sa;
128
129     if (( m = m_gethdr( MB_DONTWAIT, MT_DATA )) == NULL ) {
130         return;
131     }
132     m->m_len = sizeof( *ea );
133     m->m_pkthdr.len = sizeof( *ea );
134     MH_ALIGN( m, sizeof( *ea ));
135
136     ea = mtod( m, struct ether_aarp *);
137     bzero((caddr_t)ea, sizeof( *ea ));
138
139     ea->aarp_hrd = htons( AARPHRD_ETHER );
140     ea->aarp_pro = htons( ETHERTYPE_AT );
141     ea->aarp_hln = sizeof( ea->aarp_sha );
142     ea->aarp_pln = sizeof( ea->aarp_spu );
143     ea->aarp_op = htons( AARPOP_REQUEST );
144     bcopy(ac->ac_enaddr, ea->aarp_sha, sizeof ea->aarp_sha);
145
146     /*
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.
152      */
153     if ((aa = at_ifawithnet( sat )) == NULL) {
154         m_freem( m );
155         return;
156     }
157
158     eh = (struct ether_header *)sa.sa_data;
159
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 );
170
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;
177     } else {
178         bcopy(ac->ac_if.if_broadcastaddr, eh->ether_dhost,
179               ac->ac_if.if_addrlen);
180         eh->ether_type = htons( ETHERTYPE_AARP );
181
182         ea->aarp_spa = AA_SAT( aa )->sat_addr.s_node;
183         ea->aarp_tpa = sat->sat_addr.s_node;
184     }
185
186 #ifdef NETATALKDEBUG
187     kprintf("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 */
191
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 */
196 }
197
198 int
199 aarpresolve(struct arpcom *ac, struct mbuf *m, struct sockaddr_at *destsat,
200             u_char *desten )
201 {
202     struct at_ifaddr    *aa;
203     struct aarptab      *aat;
204
205     if (at_broadcast(destsat)) {
206         m->m_flags |= M_BCAST;
207         if ((aa = at_ifawithnet(destsat)) == NULL)  {
208             m_freem(m);
209             return (0);
210         }
211         if (aa->aa_flags & AFA_PHASE2)
212             bcopy(atmulticastaddr, desten, sizeof atmulticastaddr);
213         else
214             bcopy(ac->ac_if.if_broadcastaddr, desten, ac->ac_if.if_addrlen);
215         return (1);
216     }
217
218     crit_enter();
219     AARPTAB_LOOK( aat, destsat->sat_addr );
220     if (aat == NULL) {                  /* No entry */
221         aat = aarptnew( &destsat->sat_addr );
222         if (aat == NULL) {
223             panic("aarpresolve: no free entry");
224         }
225         aat->aat_hold = m;
226         aarpwhohas(ac, destsat);
227         crit_exit();
228         return (0);
229     }
230     /* found an entry */
231     aat->aat_timer = 0;
232     if (aat->aat_flags & ATF_COM) {     /* entry is COMplete */
233         bcopy(aat->aat_enaddr, desten, sizeof aat->aat_enaddr);
234         crit_exit();
235         return (1);
236     }
237     /* entry has not completed */
238     if (aat->aat_hold) {
239         m_freem(aat->aat_hold);
240     }
241     aat->aat_hold = m;
242     aarpwhohas(ac, destsat);
243     crit_exit();
244     return (0);
245 }
246
247 void
248 aarpintr(struct netmsg *msg)
249 {
250     struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;   
251     struct arphdr       *ar;
252     struct arpcom       *ac;
253
254     get_mplock();
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     rel_mplock();
287     /* msg was embedded in the mbuf, do not reply! */
288 }
289
290 static void
291 at_aarpinput( struct arpcom *ac, struct mbuf *m)
292 {
293     struct ether_aarp   *ea;
294     struct at_ifaddr    *aa = NULL;
295     struct aarptab      *aat;
296     struct ether_header *eh;
297     struct llc          *llc;
298     struct sockaddr_at  sat;
299     struct sockaddr     sa;
300     struct at_addr      spa, tpa, ma;
301     int                 op;
302     u_short             net;
303
304     ea = mtod( m, struct ether_aarp *);
305
306     /* Check to see if from my hardware address */
307     if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
308             sizeof( ac->ac_enaddr ))) {
309         m_freem( m );
310         return;
311     }
312
313     op = ntohs(ea->aarp_op);
314     bcopy(ea->aarp_tpnet, &net, sizeof net);
315
316     if ( net != 0 ) { /* should be ATADDR_ANYNET? */
317         sat.sat_len = sizeof(struct sockaddr_at);
318         sat.sat_family = AF_APPLETALK;
319         sat.sat_addr.s_net = net;
320         if ((aa = at_ifawithnet(&sat)) == NULL) {
321             m_freem( m );
322             return;
323         }
324         bcopy(ea->aarp_spnet, &spa.s_net, sizeof spa.s_net);
325         bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof tpa.s_net);
326     } else {
327         struct ifaddr_container *ifac;
328
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(ifac, &ac->ac_if.if_addrheads[mycpuid], ifa_link) {
334             aa = (struct at_ifaddr *)(ifac->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(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_container *ifac;
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(ifac, &ac->ac_if.if_addrheads[mycpuid], ifa_link) {
531         aa = (struct at_ifaddr *)(ifac->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         kprintf( "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     kprintf("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 }