File: aarp.c

package info (click to toggle)
netatalk 2.0.3-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 9,004 kB
  • ctags: 6,109
  • sloc: ansic: 67,633; sh: 8,424; perl: 1,187; makefile: 1,001
file content (802 lines) | stat: -rw-r--r-- 20,629 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
 * $Id: aarp.c,v 1.3 2002/01/03 17:49:39 sibaz Exp $
 *
 * Copyright (c) 1990,1991 Regents of The University of Michigan.
 * All Rights Reserved.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <sys/types.h>
#include <sys/socket.h>
#include <syslog.h>
#include <sys/param.h>
#include <sys/mbuf.h>
#include <sys/time.h>
#ifndef _IBMR2
#include <sys/kernel.h>
#endif /* _IBMR2 */
#include <net/if.h>
#include <net/route.h>
#include <net/af.h>
#include <netinet/in.h>
#undef s_net
#include <netinet/if_ether.h>
#ifdef _IBMR2
#include <netinet/in_netarp.h>
#include <net/spl.h>
#include <sys/errno.h>
#include <sys/err_rec.h>
#endif /* _IBMR2 */

#include "at.h"
#include "at_var.h"
#include "aarp.h"
#include "ddp_var.h"
#include "endian.h"
#include "phase2.h"

#ifdef GATEWAY
#define AARPTAB_BSIZ	16
#define AARPTAB_NB	37
#else /* GATEWAY */
#define AARPTAB_BSIZ	9
#define AARPTAB_NB	19
#endif /* GATEWAY */
#define AARPTAB_SIZE	(AARPTAB_BSIZ * AARPTAB_NB)
struct aarptab		aarptab[AARPTAB_SIZE];
int			aarptab_size = AARPTAB_SIZE;

#define AARPTAB_HASH(a) \
    ((((a).s_net << 8 ) + (a).s_node ) % AARPTAB_NB )

#define AARPTAB_LOOK(aat,addr) { \
    int		n; \
    aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
    for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) \
	if ( aat->aat_ataddr.s_net == (addr).s_net && \
	     aat->aat_ataddr.s_node == (addr).s_node ) \
	    break; \
	if ( n >= AARPTAB_BSIZ ) \
	    aat = 0; \
}

#define AARPT_AGE	(60 * 1)
#define AARPT_KILLC	20
#define AARPT_KILLI	3

#ifdef sun
extern struct ether_addr	etherbroadcastaddr;
#else /* sun */
extern u_char			etherbroadcastaddr[6];
#endif /* sun */

u_char	atmulticastaddr[ 6 ] = {
    0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
};

u_char	at_org_code[ 3 ] = {
    0x08, 0x00, 0x07,
};
u_char	aarp_org_code[ 3 ] = {
    0x00, 0x00, 0x00,
};

aarptimer()
{
    struct aarptab	*aat;
    int			i, s;

    timeout( aarptimer, (caddr_t)0, AARPT_AGE * hz );
    aat = aarptab;
    for ( i = 0; i < AARPTAB_SIZE; i++, aat++ ) {
	if ( aat->aat_flags == 0 || ( aat->aat_flags & ATF_PERM ))
	    continue;
	if ( ++aat->aat_timer < (( aat->aat_flags & ATF_COM ) ?
		AARPT_KILLC : AARPT_KILLI ))
	    continue;
	s = splimp();
	aarptfree( aat );
	splx( s );
    }
}

struct ifaddr *
at_ifawithnet( sat, ifa )
    struct sockaddr_at	*sat;
    struct ifaddr	*ifa;
{
    struct at_ifaddr	*aa;

    for (; ifa; ifa = ifa->ifa_next ) {
#ifdef BSD4_4
	if ( ifa->ifa_addr->sa_family != AF_APPLETALK ) {
	    continue;
	}
	if ( satosat( ifa->ifa_addr )->sat_addr.s_net ==
		sat->sat_addr.s_net ) {
	    break;
	}
#else /* BSD4_4 */
	if ( ifa->ifa_addr.sa_family != AF_APPLETALK ) {
	    continue;
	}
	aa = (struct at_ifaddr *)ifa;
	if ( ntohs( sat->sat_addr.s_net ) >= ntohs( aa->aa_firstnet ) &&
		ntohs( sat->sat_addr.s_net ) <= ntohs( aa->aa_lastnet )) {
	    break;
	}
#endif /* BSD4_4 */
    }
    return( ifa );
}

aarpwhohas( ac, sat )
    struct arpcom	*ac;
    struct sockaddr_at	*sat;
{
    struct mbuf		*m;
    struct ether_header	*eh;
    struct ether_aarp	*ea;
    struct at_ifaddr	*aa;
    struct llc		*llc;
    struct sockaddr	sa;

#ifdef BSD4_4
    if (( m = m_gethdr( M_DONTWAIT, MT_DATA )) == NULL ) {
	return;
    }
    m->m_len = sizeof( *ea );
    m->m_pkthdr.len = sizeof( *ea );
    MH_ALIGN( m, sizeof( *ea ));
#else /* BSD4_4 */
    if (( m = m_get( M_DONTWAIT, MT_DATA )) == NULL ) {
	return;
    }
    m->m_len = sizeof( *ea );
    m->m_off = MMAXOFF - sizeof( *ea );
#endif /* BSD4_4 */

    ea = mtod( m, struct ether_aarp *);
    bzero((caddr_t)ea, sizeof( *ea ));

    ea->aarp_hrd = htons( AARPHRD_ETHER );
    ea->aarp_pro = htons( ETHERTYPE_AT );
    ea->aarp_hln = sizeof( ea->aarp_sha );
    ea->aarp_pln = sizeof( ea->aarp_spu );
    ea->aarp_op = htons( AARPOP_REQUEST );
#ifdef sun
    bcopy((caddr_t)&ac->ac_enaddr, (caddr_t)ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#else /* sun */
    bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#endif /* sun */

    /*
     * We need to check whether the output ethernet type should
     * be phase 1 or 2. We have the interface that we'll be sending
     * the aarp out. We need to find an AppleTalk network on that
     * interface with the same address as we're looking for. If the
     * net is phase 2, generate an 802.2 and SNAP header.
     */
    if (( aa = (struct at_ifaddr *)at_ifawithnet( sat, ac->ac_if.if_addrlist ))
	    == NULL ) {
	m_freem( m );
	return;
    }

    eh = (struct ether_header *)sa.sa_data;

    if ( aa->aa_flags & AFA_PHASE2 ) {
#ifdef sun
	bcopy((caddr_t)atmulticastaddr, (caddr_t)&eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#else /* sun */
	bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#endif /* sun */
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( sizeof( struct llc ) +
		sizeof( struct ether_aarp ));
#else /* sun && i386 */
	eh->ether_type = sizeof( struct llc ) + sizeof( struct ether_aarp );
#endif /* sun && i386 */
#ifdef BSD4_4
	M_PREPEND( m, sizeof( struct llc ), M_WAIT );
#else /* BSD4_4 */
	m->m_len += sizeof( struct llc );
	m->m_off -= sizeof( struct llc );
#endif /* BSD4_4 */
	llc = mtod( m, struct llc *);
	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
	llc->llc_control = LLC_UI;
	bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
	llc->llc_ether_type = htons( ETHERTYPE_AARP );

	
	bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
		sizeof( ea->aarp_spnet ));
	ea->aarp_spnode = AA_SAT( aa )->sat_addr.s_node;
	bcopy( &sat->sat_addr.s_net, ea->aarp_tpnet,
		sizeof( ea->aarp_tpnet ));
	ea->aarp_tpnode = sat->sat_addr.s_node;
    } else {
#ifdef sun
	bcopy((caddr_t)&etherbroadcastaddr, (caddr_t)&eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#else /* sun */
	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#endif /* sun */
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( ETHERTYPE_AARP );
#else /* sun && i386 */
	eh->ether_type = ETHERTYPE_AARP;
#endif /* sun && i386 */

	ea->aarp_spa = AA_SAT( aa )->sat_addr.s_node;
	ea->aarp_tpa = sat->sat_addr.s_node;
    }

#ifdef BSD4_4
    sa.sa_len = sizeof( struct sockaddr );
#endif /* BSD4_4 */
    sa.sa_family = AF_UNSPEC;
    (*ac->ac_if.if_output)(&ac->ac_if, m, &sa );
}

aarpresolve( ac, m, destsat, desten )
    struct arpcom	*ac;
    struct mbuf		*m;
    struct sockaddr_at	*destsat;
#ifdef sun
    struct ether_addr	*desten;
#else /* sun */
    u_char		*desten;
#endif /* sun */
{
    struct at_ifaddr	*aa;
    struct ifaddr	ifa;
    struct aarptab	*aat;
    int			s;

    if ( at_broadcast( destsat )) {
	if (( aa = (struct at_ifaddr *)at_ifawithnet( destsat,
		((struct ifnet *)ac)->if_addrlist )) == NULL ) {
	    m_freem( m );
	    return( 0 );
	}
	if ( aa->aa_flags & AFA_PHASE2 ) {
	    bcopy( (caddr_t)atmulticastaddr, (caddr_t)desten,
		    sizeof( atmulticastaddr ));
	} else {
#ifdef sun
	    bcopy( (caddr_t)&etherbroadcastaddr, (caddr_t)desten,
		    sizeof( etherbroadcastaddr ));
#else /* sun */
	    bcopy( (caddr_t)etherbroadcastaddr, (caddr_t)desten,
		    sizeof( etherbroadcastaddr ));
#endif /* sun */
	}
	return( 1 );
    }

    s = splimp();
    AARPTAB_LOOK( aat, destsat->sat_addr );
    if ( aat == 0 ) {			/* No entry */
	aat = aarptnew( &destsat->sat_addr );
	if ( aat == 0 ) {
	    panic( "aarpresolve: no free entry" );
	}
	aat->aat_hold = m;
	aarpwhohas( ac, destsat );
	splx( s );
	return( 0 );
    }
    /* found an entry */
    aat->aat_timer = 0;
    if ( aat->aat_flags & ATF_COM ) {	/* entry is COMplete */
	bcopy( (caddr_t)aat->aat_enaddr, (caddr_t)desten,
		sizeof( aat->aat_enaddr ));
	splx( s );
	return( 1 );
    }
    /* entry has not completed */
    if ( aat->aat_hold ) {
	m_freem( aat->aat_hold );
    }
    aat->aat_hold = m;
    aarpwhohas( ac, destsat );
    splx( s );
    return( 0 );
}

aarpinput( ac, m )
    struct arpcom	*ac;
    struct mbuf		*m;
{
    struct arphdr	*ar;

    if ( ac->ac_if.if_flags & IFF_NOARP )
	goto out;

#ifndef BSD4_4
    IF_ADJ( m );
#endif /* BSD4_4 */

    if ( m->m_len < sizeof( struct arphdr )) {
	goto out;
    }

    ar = mtod( m, struct arphdr *);
    if ( ntohs( ar->ar_hrd ) != AARPHRD_ETHER ) {
	goto out;
    }
    
    if ( m->m_len < sizeof( struct arphdr ) + 2 * ar->ar_hln +
	    2 * ar->ar_pln ) {
	goto out;
    }
    
    switch( ntohs( ar->ar_pro )) {
    case ETHERTYPE_AT :
	at_aarpinput( ac, m );
	return;

    default:
	break;
    }

out:
    m_freem( m );
}


at_aarpinput( ac, m )
    struct arpcom	*ac;
    struct mbuf		*m;
{
    struct mbuf		*m0;
    struct ether_aarp	*ea;
    struct at_ifaddr	*aa;
    struct aarptab	*aat;
    struct ether_header	*eh;
    struct llc		*llc;
    struct sockaddr_at	sat;
    struct sockaddr	sa;
    struct at_addr	spa, tpa, ma;
    int			op, s;
    u_short		net;

    ea = mtod( m, struct ether_aarp *);

    /* Check to see if from my hardware address */
#ifdef sun
    if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )&ac->ac_enaddr,
	    sizeof( ac->ac_enaddr ))) {
	m_freem( m );
	return;
    }
#else /* sun */
    if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )ac->ac_enaddr,
	    sizeof( ac->ac_enaddr ))) {
	m_freem( m );
	return;
    }
#endif /* sun */

    /*
     * Check if from broadcast address.  This could be a more robust
     * check, since we could look for multicasts.
     */
#ifdef sun
    if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )&etherbroadcastaddr,
	    sizeof( etherbroadcastaddr ))) {
	log( LOG_ERR, "aarp: source is broadcast!\n" );
	m_freem( m );
	return;
    }
#else /* sun */
    if ( !bcmp(( caddr_t )ea->aarp_sha, ( caddr_t )etherbroadcastaddr,
	    sizeof( etherbroadcastaddr ))) {
#ifndef _IBMR2
#ifdef ultrix
	mprintf( LOG_ERR,
#else /* ultrix */
	log( LOG_ERR,
#endif /* ultrix */
		"aarp: source is broadcast!\n" );
#endif /* ! _IBMR2 */
	m_freem( m );
	return;
    }
#endif /* sun */

    op = ntohs( ea->aarp_op );
    bcopy( ea->aarp_tpnet, &net, sizeof( net ));

    if ( net != 0 ) {
	sat.sat_family = AF_APPLETALK;
	sat.sat_addr.s_net = net;
	if (( aa = (struct at_ifaddr *)at_ifawithnet( &sat,
		ac->ac_if.if_addrlist )) == NULL ) {
	    m_freem( m );
	    return;
	}
	bcopy( ea->aarp_spnet, &spa.s_net, sizeof( spa.s_net ));
	bcopy( ea->aarp_tpnet, &tpa.s_net, sizeof( tpa.s_net ));
    } else {
	/*
	 * Since we don't know the net, we just look for the first
	 * phase 1 address on the interface.
	 */
	for ( aa = (struct at_ifaddr *)ac->ac_if.if_addrlist; aa;
		aa = (struct at_ifaddr *)aa->aa_ifa.ifa_next ) {
	    if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
		    ( aa->aa_flags & AFA_PHASE2 ) == 0 ) {
		break;
	    }
	}
	if ( aa == NULL ) {
	    m_freem( m );
	    return;
	}
	tpa.s_net = spa.s_net = AA_SAT( aa )->sat_addr.s_net;
    }

    spa.s_node = ea->aarp_spnode;
    tpa.s_node = ea->aarp_tpnode;
    ma.s_net = AA_SAT( aa )->sat_addr.s_net;
    ma.s_node = AA_SAT( aa )->sat_addr.s_node;

    /*
     * This looks like it's from us.
     */
    if ( spa.s_net == ma.s_net && spa.s_node == ma.s_node ) {
	if ( aa->aa_flags & AFA_PROBING ) {
	    /*
	     * We're probing, someone either responded to our probe, or
	     * probed for the same address we'd like to use. Change the
	     * address we're probing for.
	     */
	    untimeout( aarpprobe, ac );
	    wakeup( aa );
	    m_freem( m );
	    return;
	} else if ( op != AARPOP_PROBE ) {
	    /*
	     * This is not a probe, and we're not probing. This means
	     * that someone's saying they have the same source address
	     * as the one we're using. Get upset...
	     */
#ifndef _IBMR2
#ifdef ultrix
	    mprintf( LOG_ERR,
#else /* ultrix */
	    log( LOG_ERR,
#endif /* ultrix */
		    "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
		    ea->aarp_sha[ 0 ], ea->aarp_sha[ 1 ], ea->aarp_sha[ 2 ],
		    ea->aarp_sha[ 3 ], ea->aarp_sha[ 4 ], ea->aarp_sha[ 5 ]);
#endif /* ! _IBMR2 */
	    m_freem( m );
	    return;
	}
    }

    AARPTAB_LOOK( aat, spa );
    if ( aat ) {
	if ( op == AARPOP_PROBE ) {
	    /*
	     * Someone's probing for spa, dealocate the one we've got,
	     * so that if the prober keeps the address, we'll be able
	     * to arp for him.
	     */
	    aarptfree( aat );
	    m_freem( m );
	    return;
	}

	bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
		sizeof( ea->aarp_sha ));
	aat->aat_flags |= ATF_COM;
	if ( aat->aat_hold ) {
#ifdef _IBMR2
	    /*
	     * Like in ddp_output(), we can't rely on the if_output
	     * routine to resolve AF_APPLETALK addresses, on the rs6k.
	     * So, we fill the destination ethernet address here.
	     *
	     * This should really be replaced with something like
	     * rsif_output(). XXX Will have to be for phase 2.
	     */
	     /* XXX maybe fill in the rest of the frame header */
	    sat.sat_family = AF_UNSPEC;
	    bcopy( aat->aat_enaddr, (*(struct sockaddr *)&sat).sa_data,
		    sizeof( aat->aat_enaddr ));
#else /* _IBMR2 */
	    sat.sat_family = AF_APPLETALK;
	    sat.sat_addr = spa;
#endif /* _IBMR2 */
	    (*ac->ac_if.if_output)( &ac->ac_if, aat->aat_hold,
		    (struct sockaddr *)&sat );
	    aat->aat_hold = 0;
	}
    }

    if ( aat == 0 && tpa.s_net == ma.s_net && tpa.s_node == ma.s_node
	    && op != AARPOP_PROBE ) {
	if ( aat = aarptnew( &spa )) {
	    bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )aat->aat_enaddr,
		    sizeof( ea->aarp_sha ));
	    aat->aat_flags |= ATF_COM;
	}
    }

    /*
     * Don't respond to responses, and never respond if we're
     * still probing.
     */
    if ( tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
	    op == AARPOP_RESPONSE || ( aa->aa_flags & AFA_PROBING )) {
	m_freem( m );
	return;
    }

    bcopy(( caddr_t )ea->aarp_sha, ( caddr_t )ea->aarp_tha,
	    sizeof( ea->aarp_sha ));
#ifdef sun
    bcopy(( caddr_t )&ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#else /* sun */
    bcopy(( caddr_t )ac->ac_enaddr, ( caddr_t )ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#endif /* sun */

    eh = (struct ether_header *)sa.sa_data;
#ifdef sun
    bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )&eh->ether_dhost,
	    sizeof( eh->ether_dhost ));
#else /* sun */
    bcopy(( caddr_t )ea->aarp_tha, ( caddr_t )eh->ether_dhost,
	    sizeof( eh->ether_dhost ));
#endif /* sun */

    if ( aa->aa_flags & AFA_PHASE2 ) {
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( sizeof( struct llc ) +
		sizeof( struct ether_aarp ));
#else /* sun && i386 */
	eh->ether_type = sizeof( struct llc ) + sizeof( struct ether_aarp );
#endif /* sun && i386 */
#ifdef BSD4_4
	M_PREPEND( m, sizeof( struct llc ), M_DONTWAIT );
	if ( m == NULL ) {
	    m_freem( m );
	    return;
	}
#else /* BSD4_4 */
	MGET( m0, M_DONTWAIT, MT_HEADER );
	if ( m0 == NULL ) {
	    m_freem( m );
	    return;
	}
	m0->m_next = m;
	m = m0;
	m->m_off = MMAXOFF - sizeof( struct llc );
	m->m_len = sizeof ( struct llc );
#endif /* BSD4_4 */
	llc = mtod( m, struct llc *);
	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
	llc->llc_control = LLC_UI;
	bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
	llc->llc_ether_type = htons( ETHERTYPE_AARP );

	bcopy( ea->aarp_spnet, ea->aarp_tpnet, sizeof( ea->aarp_tpnet ));
	bcopy( &ma.s_net, ea->aarp_spnet, sizeof( ea->aarp_spnet ));
    } else {
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( ETHERTYPE_AARP );
#else /* sun && i386 */
	eh->ether_type = ETHERTYPE_AARP;
#endif /* sun && i386 */
    }

    ea->aarp_tpnode = ea->aarp_spnode;
    ea->aarp_spnode = ma.s_node;
    ea->aarp_op = htons( AARPOP_RESPONSE );

#ifdef BSD4_4
    sa.sa_len = sizeof( struct sockaddr );
#endif /* BSD4_4 */
    sa.sa_family = AF_UNSPEC;
    (*ac->ac_if.if_output)( &ac->ac_if, m, &sa );
    return;
}

aarptfree( aat )
    struct aarptab	*aat;
{

    if ( aat->aat_hold )
	m_freem( aat->aat_hold );
    aat->aat_hold = 0;
    aat->aat_timer = aat->aat_flags = 0;
    aat->aat_ataddr.s_net = 0;
    aat->aat_ataddr.s_node = 0;
}

    struct aarptab *
aarptnew( addr )
    struct at_addr	*addr;
{
    int			n;
    int			oldest = -1;
    struct aarptab	*aat, *aato = NULL;
    static int		first = 1;

    if ( first ) {
	first = 0;
	timeout( aarptimer, (caddr_t)0, hz );
    }
    aat = &aarptab[ AARPTAB_HASH( *addr ) * AARPTAB_BSIZ ];
    for ( n = 0; n < AARPTAB_BSIZ; n++, aat++ ) {
	if ( aat->aat_flags == 0 )
	    goto out;
	if ( aat->aat_flags & ATF_PERM )
	    continue;
	if ((int) aat->aat_timer > oldest ) {
	    oldest = aat->aat_timer;
	    aato = aat;
	}
    }
    if ( aato == NULL )
	return( NULL );
    aat = aato;
    aarptfree( aat );
out:
    aat->aat_ataddr = *addr;
    aat->aat_flags = ATF_INUSE;
    return( aat );
}

aarpprobe( ac )
    struct arpcom	*ac;
{
    struct mbuf		*m;
    struct ether_header	*eh;
    struct ether_aarp	*ea;
    struct at_ifaddr	*aa;
    struct llc		*llc;
    struct sockaddr	sa;

    /*
     * We need to check whether the output ethernet type should
     * be phase 1 or 2. We have the interface that we'll be sending
     * the aarp out. We need to find an AppleTalk network on that
     * interface with the same address as we're looking for. If the
     * net is phase 2, generate an 802.2 and SNAP header.
     */
    for ( aa = (struct at_ifaddr *)ac->ac_if.if_addrlist; aa;
	    aa = (struct at_ifaddr *)aa->aa_ifa.ifa_next ) {
	if ( AA_SAT( aa )->sat_family == AF_APPLETALK &&
		( aa->aa_flags & AFA_PROBING )) {
	    break;
	}
    }
    if ( aa == NULL ) {		/* serious error XXX */
	printf( "aarpprobe why did this happen?!\n" );
	return;
    }

    if ( aa->aa_probcnt <= 0 ) {
	aa->aa_flags &= ~AFA_PROBING;
	wakeup( aa );
	return;
    } else {
	timeout( aarpprobe, (caddr_t)ac, hz / 5 );
    }

#ifdef BSD4_4
    if (( m = m_gethdr( M_DONTWAIT, MT_DATA )) == NULL ) {
	return;
    }
    m->m_len = sizeof( *ea );
    m->m_pkthdr.len = sizeof( *ea );
    MH_ALIGN( m, sizeof( *ea ));
#else /* BSD4_4 */
    if (( m = m_get( M_DONTWAIT, MT_DATA )) == NULL ) {
	return;
    }
    m->m_len = sizeof( *ea );
    m->m_off = MMAXOFF - sizeof( *ea );
#endif /* BSD4_4 */

    ea = mtod( m, struct ether_aarp *);
    bzero((caddr_t)ea, sizeof( *ea ));

    ea->aarp_hrd = htons( AARPHRD_ETHER );
    ea->aarp_pro = htons( ETHERTYPE_AT );
    ea->aarp_hln = sizeof( ea->aarp_sha );
    ea->aarp_pln = sizeof( ea->aarp_spu );
    ea->aarp_op = htons( AARPOP_PROBE );
#ifdef sun
    bcopy((caddr_t)&ac->ac_enaddr, (caddr_t)ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#else /* sun */
    bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->aarp_sha,
	    sizeof( ea->aarp_sha ));
#endif /* sun */

    eh = (struct ether_header *)sa.sa_data;

    if ( aa->aa_flags & AFA_PHASE2 ) {
#ifdef sun
	bcopy((caddr_t)atmulticastaddr, (caddr_t)&eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#else /* sun */
	bcopy((caddr_t)atmulticastaddr, (caddr_t)eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#endif /* sun */
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( sizeof( struct llc ) +
		sizeof( struct ether_aarp ));
#else /* sun && i386 */
	eh->ether_type = sizeof( struct llc ) + sizeof( struct ether_aarp );
#endif /* sun && i386 */
#ifdef BSD4_4
	M_PREPEND( m, sizeof( struct llc ), M_WAIT );
#else /* BSD4_4 */
	m->m_len += sizeof( struct llc );
	m->m_off -= sizeof( struct llc );
#endif /* BSD4_4 */
	llc = mtod( m, struct llc *);
	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
	llc->llc_control = LLC_UI;
	bcopy( aarp_org_code, llc->llc_org_code, sizeof( aarp_org_code ));
	llc->llc_ether_type = htons( ETHERTYPE_AARP );

	bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_spnet,
		sizeof( ea->aarp_spnet ));
	bcopy( &AA_SAT( aa )->sat_addr.s_net, ea->aarp_tpnet,
		sizeof( ea->aarp_tpnet ));
	ea->aarp_spnode = ea->aarp_tpnode = AA_SAT( aa )->sat_addr.s_node;
    } else {
#ifdef sun
	bcopy((caddr_t)&etherbroadcastaddr, (caddr_t)&eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#else /* sun */
	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
		sizeof( eh->ether_dhost ));
#endif /* sun */
#if defined( sun ) && defined( i386 )
	eh->ether_type = htons( ETHERTYPE_AARP );
#else /* sun && i386 */
	eh->ether_type = ETHERTYPE_AARP;
#endif /* sun && i386 */
	ea->aarp_spa = ea->aarp_tpa = AA_SAT( aa )->sat_addr.s_node;
    }

#ifdef BSD4_4
    sa.sa_len = sizeof( struct sockaddr );
#endif /* BSD4_4 */
    sa.sa_family = AF_UNSPEC;
    (*ac->ac_if.if_output)(&ac->ac_if, m, &sa );
    aa->aa_probcnt--;
}

aarp_clean()
{
    struct aarptab	*aat;
    int			i;

    untimeout( aarptimer, 0 );
    for ( i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++ ) {
	if ( aat->aat_hold ) {
	    m_freem( aat->aat_hold );
	}
    }
}