2 * Copyright (c) 2004-2009 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Copyright (c) 1990,1991,1994 Regents of The University of Michigan.
27 * All Rights Reserved.
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation, and that the name of The University
34 * of Michigan not be used in advertising or publicity pertaining to
35 * distribution of the software without specific, written prior
36 * permission. This software is supplied as is without expressed or
37 * implied warranties of any kind.
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
42 * Research Systems Unix Group
43 * The University of Michigan
45 * 535 W. William Street
53 #include "opt_atalk.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/syslog.h>
63 #include <net/if_dl.h>
65 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
69 #include <netatalk/at.h>
70 #include <netatalk/at_var.h>
71 #include <netatalk/aarp.h>
72 #include <netatalk/phase2.h>
73 #include <netatalk/at_extern.h>
75 #include <security/mac/mac_framework.h>
77 static void aarptfree(struct aarptab *aat);
78 static void at_aarpinput(struct ifnet *ifp, struct mbuf *m);
80 #define AARPTAB_BSIZ 9
82 #define AARPTAB_SIZE (AARPTAB_BSIZ * AARPTAB_NB)
83 static struct aarptab aarptab[AARPTAB_SIZE];
85 struct mtx aarptab_mtx;
86 MTX_SYSINIT(aarptab_mtx, &aarptab_mtx, "aarptab_mtx", MTX_DEF);
88 #define AARPTAB_HASH(a) ((((a).s_net << 8) + (a).s_node) % AARPTAB_NB)
90 #define AARPTAB_LOOK(aat, addr) do { \
93 AARPTAB_LOCK_ASSERT(); \
94 aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
95 for (n = 0; n < AARPTAB_BSIZ; n++, aat++) { \
96 if (aat->aat_ataddr.s_net == (addr).s_net && \
97 aat->aat_ataddr.s_node == (addr).s_node) \
100 if (n >= AARPTAB_BSIZ) \
104 #define AARPT_AGE (60 * 1)
105 #define AARPT_KILLC 20
106 #define AARPT_KILLI 3
108 static const u_char atmulticastaddr[6] = {
109 0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
112 u_char at_org_code[3] = {
115 const u_char aarp_org_code[3] = {
119 static struct callout_handle aarptimer_ch =
120 CALLOUT_HANDLE_INITIALIZER(&aarptimer_ch);
123 aarptimer(void *ignored)
128 aarptimer_ch = timeout(aarptimer, NULL, AARPT_AGE * hz);
131 for (i = 0; i < AARPTAB_SIZE; i++, aat++) {
132 if (aat->aat_flags == 0 || (aat->aat_flags & ATF_PERM))
134 if (++aat->aat_timer < ((aat->aat_flags & ATF_COM) ?
135 AARPT_KILLC : AARPT_KILLI))
143 * Search through the network addresses to find one that includes the given
144 * network. Remember to take netranges into consideration.
146 * The _locked variant relies on the caller holding the at_ifaddr lock; the
147 * unlocked variant returns a reference that the caller must dispose of.
150 at_ifawithnet_locked(struct sockaddr_at *sat)
152 struct at_ifaddr *aa;
153 struct sockaddr_at *sat2;
155 AT_IFADDR_LOCK_ASSERT();
157 TAILQ_FOREACH(aa, &at_ifaddrhead, aa_link) {
158 sat2 = &(aa->aa_addr);
159 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
161 if ((aa->aa_flags & AFA_PHASE2) &&
162 (ntohs(aa->aa_firstnet) <= ntohs(sat->sat_addr.s_net)) &&
163 (ntohs(aa->aa_lastnet) >= ntohs(sat->sat_addr.s_net)))
170 at_ifawithnet(struct sockaddr_at *sat)
172 struct at_ifaddr *aa;
175 aa = at_ifawithnet_locked(sat);
177 ifa_ref(&aa->aa_ifa);
183 aarpwhohas(struct ifnet *ifp, struct sockaddr_at *sat)
186 struct ether_header *eh;
187 struct ether_aarp *ea;
188 struct at_ifaddr *aa;
192 AARPTAB_UNLOCK_ASSERT();
193 m = m_gethdr(M_DONTWAIT, MT_DATA);
197 mac_netatalk_aarp_send(ifp, m);
199 m->m_len = sizeof(*ea);
200 m->m_pkthdr.len = sizeof(*ea);
201 MH_ALIGN(m, sizeof(*ea));
203 ea = mtod(m, struct ether_aarp *);
204 bzero((caddr_t)ea, sizeof(*ea));
206 ea->aarp_hrd = htons(AARPHRD_ETHER);
207 ea->aarp_pro = htons(ETHERTYPE_AT);
208 ea->aarp_hln = sizeof(ea->aarp_sha);
209 ea->aarp_pln = sizeof(ea->aarp_spu);
210 ea->aarp_op = htons(AARPOP_REQUEST);
211 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha, sizeof(ea->aarp_sha));
214 * We need to check whether the output ethernet type should be phase
215 * 1 or 2. We have the interface that we'll be sending the aarp out.
216 * We need to find an AppleTalk network on that interface with the
217 * same address as we're looking for. If the net is phase 2,
218 * generate an 802.2 and SNAP header.
220 aa = at_ifawithnet(sat);
226 eh = (struct ether_header *)sa.sa_data;
228 if (aa->aa_flags & AFA_PHASE2) {
229 bcopy(atmulticastaddr, eh->ether_dhost,
230 sizeof(eh->ether_dhost));
231 eh->ether_type = htons(sizeof(struct llc) +
232 sizeof(struct ether_aarp));
233 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
235 ifa_free(&aa->aa_ifa);
238 llc = mtod(m, struct llc *);
239 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
240 llc->llc_control = LLC_UI;
241 bcopy(aarp_org_code, llc->llc_org_code,
242 sizeof(aarp_org_code));
243 llc->llc_ether_type = htons(ETHERTYPE_AARP);
244 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
245 sizeof(ea->aarp_spnet));
246 bcopy(&sat->sat_addr.s_net, ea->aarp_tpnet,
247 sizeof(ea->aarp_tpnet));
248 ea->aarp_spnode = AA_SAT(aa)->sat_addr.s_node;
249 ea->aarp_tpnode = sat->sat_addr.s_node;
251 bcopy(ifp->if_broadcastaddr, (caddr_t)eh->ether_dhost,
252 sizeof(eh->ether_dhost));
253 eh->ether_type = htons(ETHERTYPE_AARP);
254 ea->aarp_spa = AA_SAT(aa)->sat_addr.s_node;
255 ea->aarp_tpa = sat->sat_addr.s_node;
259 printf("aarp: sending request for %u.%u\n",
260 ntohs(AA_SAT(aa)->sat_addr.s_net), AA_SAT(aa)->sat_addr.s_node);
261 #endif /* NETATALKDEBUG */
262 ifa_free(&aa->aa_ifa);
264 sa.sa_len = sizeof(struct sockaddr);
265 sa.sa_family = AF_UNSPEC;
266 ifp->if_output(ifp, m, &sa, NULL);
270 aarpresolve(struct ifnet *ifp, struct mbuf *m, struct sockaddr_at *destsat,
273 struct at_ifaddr *aa;
277 if (at_broadcast(destsat)) {
278 m->m_flags |= M_BCAST;
279 if ((aa = at_ifawithnet_locked(destsat)) == NULL) {
284 if (aa->aa_flags & AFA_PHASE2)
285 bcopy(atmulticastaddr, (caddr_t)desten,
286 sizeof(atmulticastaddr));
288 bcopy(ifp->if_broadcastaddr, (caddr_t)desten,
289 sizeof(ifp->if_addrlen));
296 AARPTAB_LOOK(aat, destsat->sat_addr);
299 aat = aarptnew(&destsat->sat_addr);
301 /* We should fail more gracefully. */
303 panic("aarpresolve: no free entry");
307 /* Found an entry. */
309 if (aat->aat_flags & ATF_COM) {
310 /* Entry is COMplete. */
311 bcopy((caddr_t)aat->aat_enaddr, (caddr_t)desten,
312 sizeof(aat->aat_enaddr));
317 /* Entry has not completed. */
319 m_freem(aat->aat_hold);
323 aarpwhohas(ifp, destsat);
328 aarpintr(struct mbuf *m)
333 ifp = m->m_pkthdr.rcvif;
334 if (ifp->if_flags & IFF_NOARP)
337 if (m->m_len < sizeof(struct arphdr))
340 ar = mtod(m, struct arphdr *);
341 if (ntohs(ar->ar_hrd) != AARPHRD_ETHER)
344 if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln +
348 switch(ntohs(ar->ar_pro)) {
350 at_aarpinput(ifp, m);
361 at_aarpinput(struct ifnet *ifp, struct mbuf *m)
363 struct ether_aarp *ea;
364 struct at_ifaddr *aa;
366 struct ether_header *eh;
368 struct sockaddr_at sat;
370 struct at_addr spa, tpa, ma;
374 ea = mtod(m, struct ether_aarp *);
376 /* Check to see if from my hardware address. */
377 if (!bcmp((caddr_t)ea->aarp_sha, IF_LLADDR(ifp), ETHER_ADDR_LEN)) {
382 /* Don't accept requests from broadcast address. */
383 if (!bcmp(ea->aarp_sha, ifp->if_broadcastaddr, ifp->if_addrlen)) {
384 log(LOG_ERR, "aarp: source link address is broadcast\n");
389 op = ntohs(ea->aarp_op);
390 bcopy(ea->aarp_tpnet, &net, sizeof(net));
393 /* Should be ATADDR_ANYNET? */
394 sat.sat_len = sizeof(struct sockaddr_at);
395 sat.sat_family = AF_APPLETALK;
396 sat.sat_addr.s_net = net;
397 aa = at_ifawithnet(&sat);
402 bcopy(ea->aarp_spnet, &spa.s_net, sizeof(spa.s_net));
403 bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof(tpa.s_net));
406 * Since we don't know the net, we just look for the first
407 * phase 1 address on the interface.
410 for (aa = (struct at_ifaddr *)TAILQ_FIRST(&ifp->if_addrhead);
412 aa = (struct at_ifaddr *)aa->aa_ifa.ifa_link.tqe_next) {
413 if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
414 (aa->aa_flags & AFA_PHASE2) == 0) {
419 IF_ADDR_RUNLOCK(ifp);
423 ifa_ref(&aa->aa_ifa);
424 IF_ADDR_RUNLOCK(ifp);
425 tpa.s_net = spa.s_net = AA_SAT(aa)->sat_addr.s_net;
428 spa.s_node = ea->aarp_spnode;
429 tpa.s_node = ea->aarp_tpnode;
430 ma.s_net = AA_SAT(aa)->sat_addr.s_net;
431 ma.s_node = AA_SAT(aa)->sat_addr.s_node;
434 * This looks like it's from us.
436 if (spa.s_net == ma.s_net && spa.s_node == ma.s_node) {
437 if (aa->aa_flags & AFA_PROBING) {
439 * We're probing, someone either responded to our
440 * probe, or probed for the same address we'd like to
441 * use. Change the address we're probing for.
443 callout_stop(&aa->aa_callout);
445 ifa_free(&aa->aa_ifa);
448 } else if (op != AARPOP_PROBE) {
450 * This is not a probe, and we're not probing. This
451 * means that someone's saying they have the same
452 * source address as the one we're using. Get upset.
454 ifa_free(&aa->aa_ifa);
456 "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
457 ea->aarp_sha[0], ea->aarp_sha[1],
458 ea->aarp_sha[2], ea->aarp_sha[3],
459 ea->aarp_sha[4], ea->aarp_sha[5]);
466 AARPTAB_LOOK(aat, spa);
468 if (op == AARPOP_PROBE) {
470 * Someone's probing for spa, dealocate the one we've
471 * got, so that if the prober keeps the address,
472 * we'll be able to arp for him.
476 ifa_free(&aa->aa_ifa);
481 bcopy((caddr_t)ea->aarp_sha, (caddr_t)aat->aat_enaddr,
482 sizeof(ea->aarp_sha));
483 aat->aat_flags |= ATF_COM;
485 struct mbuf *mhold = aat->aat_hold;
486 aat->aat_hold = NULL;
488 sat.sat_len = sizeof(struct sockaddr_at);
489 sat.sat_family = AF_APPLETALK;
491 (*ifp->if_output)(ifp, mhold,
492 (struct sockaddr *)&sat, NULL); /* XXX */
495 } else if ((tpa.s_net == ma.s_net) && (tpa.s_node == ma.s_node)
496 && (op != AARPOP_PROBE) && ((aat = aarptnew(&spa)) != NULL)) {
497 bcopy((caddr_t)ea->aarp_sha, (caddr_t)aat->aat_enaddr,
498 sizeof(ea->aarp_sha));
499 aat->aat_flags |= ATF_COM;
505 * Don't respond to responses, and never respond if we're still
508 if (tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
509 op == AARPOP_RESPONSE || (aa->aa_flags & AFA_PROBING)) {
510 ifa_free(&aa->aa_ifa);
515 bcopy((caddr_t)ea->aarp_sha, (caddr_t)ea->aarp_tha,
516 sizeof(ea->aarp_sha));
517 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha, sizeof(ea->aarp_sha));
520 eh = (struct ether_header *)sa.sa_data;
521 bcopy((caddr_t)ea->aarp_tha, (caddr_t)eh->ether_dhost,
522 sizeof(eh->ether_dhost));
524 if (aa->aa_flags & AFA_PHASE2) {
525 eh->ether_type = htons(sizeof(struct llc) +
526 sizeof(struct ether_aarp));
527 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
529 ifa_free(&aa->aa_ifa);
532 llc = mtod(m, struct llc *);
533 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
534 llc->llc_control = LLC_UI;
535 bcopy(aarp_org_code, llc->llc_org_code,
536 sizeof(aarp_org_code));
537 llc->llc_ether_type = htons(ETHERTYPE_AARP);
539 bcopy(ea->aarp_spnet, ea->aarp_tpnet,
540 sizeof(ea->aarp_tpnet));
541 bcopy(&ma.s_net, ea->aarp_spnet, sizeof(ea->aarp_spnet));
543 eh->ether_type = htons(ETHERTYPE_AARP);
544 ifa_free(&aa->aa_ifa);
546 ea->aarp_tpnode = ea->aarp_spnode;
547 ea->aarp_spnode = ma.s_node;
548 ea->aarp_op = htons(AARPOP_RESPONSE);
550 sa.sa_len = sizeof(struct sockaddr);
551 sa.sa_family = AF_UNSPEC;
552 (*ifp->if_output)(ifp, m, &sa, NULL); /* XXX */
557 aarptfree(struct aarptab *aat)
560 AARPTAB_LOCK_ASSERT();
562 m_freem(aat->aat_hold);
563 aat->aat_hold = NULL;
564 aat->aat_timer = aat->aat_flags = 0;
565 aat->aat_ataddr.s_net = 0;
566 aat->aat_ataddr.s_node = 0;
570 aarptnew(struct at_addr *addr)
574 struct aarptab *aat, *aato = NULL;
575 static int first = 1;
577 AARPTAB_LOCK_ASSERT();
580 aarptimer_ch = timeout(aarptimer, (caddr_t)0, hz);
582 aat = &aarptab[AARPTAB_HASH(*addr) * AARPTAB_BSIZ];
583 for (n = 0; n < AARPTAB_BSIZ; n++, aat++) {
584 if (aat->aat_flags == 0)
586 if (aat->aat_flags & ATF_PERM)
588 if ((int) aat->aat_timer > oldest) {
589 oldest = aat->aat_timer;
598 aat->aat_ataddr = *addr;
599 aat->aat_flags = ATF_INUSE;
607 struct ifnet *ifp = arg;
609 struct ether_header *eh;
610 struct ether_aarp *ea;
611 struct at_ifaddr *aa;
616 * We need to check whether the output ethernet type should be phase
617 * 1 or 2. We have the interface that we'll be sending the aarp out.
618 * We need to find an AppleTalk network on that interface with the
619 * same address as we're looking for. If the net is phase 2,
620 * generate an 802.2 and SNAP header.
623 for (aa = (struct at_ifaddr *)TAILQ_FIRST(&ifp->if_addrhead); aa;
624 aa = (struct at_ifaddr *)aa->aa_ifa.ifa_link.tqe_next) {
625 if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
626 (aa->aa_flags & AFA_PROBING))
630 /* Serious error XXX. */
632 printf("aarpprobe why did this happen?!\n");
636 if (aa->aa_probcnt <= 0) {
637 aa->aa_flags &= ~AFA_PROBING;
642 callout_reset(&aa->aa_callout, hz / 5, aarpprobe, ifp);
643 ifa_ref(&aa->aa_ifa);
646 m = m_gethdr(M_DONTWAIT, MT_DATA);
648 ifa_free(&aa->aa_ifa);
652 mac_netatalk_aarp_send(ifp, m);
654 m->m_len = sizeof(*ea);
655 m->m_pkthdr.len = sizeof(*ea);
656 MH_ALIGN(m, sizeof(*ea));
658 ea = mtod(m, struct ether_aarp *);
659 bzero((caddr_t)ea, sizeof(*ea));
661 ea->aarp_hrd = htons(AARPHRD_ETHER);
662 ea->aarp_pro = htons(ETHERTYPE_AT);
663 ea->aarp_hln = sizeof(ea->aarp_sha);
664 ea->aarp_pln = sizeof(ea->aarp_spu);
665 ea->aarp_op = htons(AARPOP_PROBE);
666 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha,
667 sizeof(ea->aarp_sha));
669 eh = (struct ether_header *)sa.sa_data;
671 if (aa->aa_flags & AFA_PHASE2) {
672 bcopy(atmulticastaddr, eh->ether_dhost,
673 sizeof(eh->ether_dhost));
674 eh->ether_type = htons(sizeof(struct llc) +
675 sizeof(struct ether_aarp));
676 M_PREPEND(m, sizeof(struct llc), M_WAIT);
677 llc = mtod(m, struct llc *);
678 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
679 llc->llc_control = LLC_UI;
680 bcopy(aarp_org_code, llc->llc_org_code,
681 sizeof(aarp_org_code));
682 llc->llc_ether_type = htons(ETHERTYPE_AARP);
684 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
685 sizeof(ea->aarp_spnet));
686 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_tpnet,
687 sizeof(ea->aarp_tpnet));
688 ea->aarp_spnode = ea->aarp_tpnode =
689 AA_SAT(aa)->sat_addr.s_node;
691 bcopy(ifp->if_broadcastaddr, (caddr_t)eh->ether_dhost,
692 sizeof(eh->ether_dhost));
693 eh->ether_type = htons(ETHERTYPE_AARP);
694 ea->aarp_spa = ea->aarp_tpa = AA_SAT(aa)->sat_addr.s_node;
698 printf("aarp: sending probe for %u.%u\n",
699 ntohs(AA_SAT(aa)->sat_addr.s_net), AA_SAT(aa)->sat_addr.s_node);
700 #endif /* NETATALKDEBUG */
701 ifa_free(&aa->aa_ifa);
703 sa.sa_len = sizeof(struct sockaddr);
704 sa.sa_family = AF_UNSPEC;
705 (*ifp->if_output)(ifp, m, &sa, NULL); /* XXX */
715 untimeout(aarptimer, 0, aarptimer_ch);
717 for (i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++) {
719 m_freem(aat->aat_hold);
720 aat->aat_hold = NULL;