2 * Copyright (c) 2004-2005 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"
56 #include <sys/param.h>
57 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/syslog.h>
64 #include <net/if_dl.h>
66 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
70 #include <netatalk/at.h>
71 #include <netatalk/at_var.h>
72 #include <netatalk/aarp.h>
73 #include <netatalk/phase2.h>
74 #include <netatalk/at_extern.h>
76 #include <security/mac/mac_framework.h>
78 static void aarptfree(struct aarptab *aat);
79 static void at_aarpinput(struct ifnet *ifp, struct mbuf *m);
81 #define AARPTAB_BSIZ 9
83 #define AARPTAB_SIZE (AARPTAB_BSIZ * AARPTAB_NB)
84 static struct aarptab aarptab[AARPTAB_SIZE];
86 struct mtx aarptab_mtx;
87 MTX_SYSINIT(aarptab_mtx, &aarptab_mtx, "aarptab_mtx", MTX_DEF);
89 #define AARPTAB_HASH(a) ((((a).s_net << 8) + (a).s_node) % AARPTAB_NB)
91 #define AARPTAB_LOOK(aat, addr) do { \
94 AARPTAB_LOCK_ASSERT(); \
95 aat = &aarptab[ AARPTAB_HASH(addr) * AARPTAB_BSIZ ]; \
96 for (n = 0; n < AARPTAB_BSIZ; n++, aat++) { \
97 if (aat->aat_ataddr.s_net == (addr).s_net && \
98 aat->aat_ataddr.s_node == (addr).s_node) \
101 if (n >= AARPTAB_BSIZ) \
105 #define AARPT_AGE (60 * 1)
106 #define AARPT_KILLC 20
107 #define AARPT_KILLI 3
109 static const u_char atmulticastaddr[6] = {
110 0x09, 0x00, 0x07, 0xff, 0xff, 0xff,
113 u_char at_org_code[3] = {
116 const u_char aarp_org_code[3] = {
120 static struct callout_handle aarptimer_ch =
121 CALLOUT_HANDLE_INITIALIZER(&aarptimer_ch);
124 aarptimer(void *ignored)
129 aarptimer_ch = timeout(aarptimer, NULL, AARPT_AGE * hz);
132 for (i = 0; i < AARPTAB_SIZE; i++, aat++) {
133 if (aat->aat_flags == 0 || (aat->aat_flags & ATF_PERM))
135 if (++aat->aat_timer < ((aat->aat_flags & ATF_COM) ?
136 AARPT_KILLC : AARPT_KILLI))
144 * Search through the network addresses to find one that includes the given
145 * network. Remember to take netranges into consideration.
148 at_ifawithnet(struct sockaddr_at *sat)
150 struct at_ifaddr *aa;
151 struct sockaddr_at *sat2;
153 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
154 sat2 = &(aa->aa_addr);
155 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
157 if ((aa->aa_flags & AFA_PHASE2) &&
158 (ntohs(aa->aa_firstnet) <= ntohs(sat->sat_addr.s_net)) &&
159 (ntohs(aa->aa_lastnet) >= ntohs(sat->sat_addr.s_net)))
166 aarpwhohas(struct ifnet *ifp, struct sockaddr_at *sat)
169 struct ether_header *eh;
170 struct ether_aarp *ea;
171 struct at_ifaddr *aa;
175 AARPTAB_UNLOCK_ASSERT();
176 m = m_gethdr(M_DONTWAIT, MT_DATA);
180 mac_netatalk_aarp_send(ifp, m);
182 m->m_len = sizeof(*ea);
183 m->m_pkthdr.len = sizeof(*ea);
184 MH_ALIGN(m, sizeof(*ea));
186 ea = mtod(m, struct ether_aarp *);
187 bzero((caddr_t)ea, sizeof(*ea));
189 ea->aarp_hrd = htons(AARPHRD_ETHER);
190 ea->aarp_pro = htons(ETHERTYPE_AT);
191 ea->aarp_hln = sizeof(ea->aarp_sha);
192 ea->aarp_pln = sizeof(ea->aarp_spu);
193 ea->aarp_op = htons(AARPOP_REQUEST);
194 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha, sizeof(ea->aarp_sha));
197 * We need to check whether the output ethernet type should be phase
198 * 1 or 2. We have the interface that we'll be sending the aarp out.
199 * We need to find an AppleTalk network on that interface with the
200 * same address as we're looking for. If the net is phase 2,
201 * generate an 802.2 and SNAP header.
203 if ((aa = at_ifawithnet(sat)) == NULL) {
208 eh = (struct ether_header *)sa.sa_data;
210 if (aa->aa_flags & AFA_PHASE2) {
211 bcopy(atmulticastaddr, eh->ether_dhost,
212 sizeof(eh->ether_dhost));
213 eh->ether_type = htons(sizeof(struct llc) +
214 sizeof(struct ether_aarp));
215 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
218 llc = mtod(m, struct llc *);
219 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
220 llc->llc_control = LLC_UI;
221 bcopy(aarp_org_code, llc->llc_org_code,
222 sizeof(aarp_org_code));
223 llc->llc_ether_type = htons(ETHERTYPE_AARP);
224 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
225 sizeof(ea->aarp_spnet));
226 bcopy(&sat->sat_addr.s_net, ea->aarp_tpnet,
227 sizeof(ea->aarp_tpnet));
228 ea->aarp_spnode = AA_SAT(aa)->sat_addr.s_node;
229 ea->aarp_tpnode = sat->sat_addr.s_node;
231 bcopy(ifp->if_broadcastaddr, (caddr_t)eh->ether_dhost,
232 sizeof(eh->ether_dhost));
233 eh->ether_type = htons(ETHERTYPE_AARP);
234 ea->aarp_spa = AA_SAT(aa)->sat_addr.s_node;
235 ea->aarp_tpa = sat->sat_addr.s_node;
239 printf("aarp: sending request for %u.%u\n",
240 ntohs(AA_SAT(aa)->sat_addr.s_net), AA_SAT(aa)->sat_addr.s_node);
241 #endif /* NETATALKDEBUG */
243 sa.sa_len = sizeof(struct sockaddr);
244 sa.sa_family = AF_UNSPEC;
245 ifp->if_output(ifp, m, &sa, NULL);
249 aarpresolve(struct ifnet *ifp, struct mbuf *m, struct sockaddr_at *destsat,
252 struct at_ifaddr *aa;
255 if (at_broadcast(destsat)) {
256 m->m_flags |= M_BCAST;
257 if ((aa = at_ifawithnet(destsat)) == NULL) {
261 if (aa->aa_flags & AFA_PHASE2)
262 bcopy(atmulticastaddr, (caddr_t)desten,
263 sizeof(atmulticastaddr));
265 bcopy(ifp->if_broadcastaddr, (caddr_t)desten,
266 sizeof(ifp->if_addrlen));
271 AARPTAB_LOOK(aat, destsat->sat_addr);
274 aat = aarptnew(&destsat->sat_addr);
276 /* We should fail more gracefully. */
278 panic("aarpresolve: no free entry");
282 /* Found an entry. */
284 if (aat->aat_flags & ATF_COM) {
285 /* Entry is COMplete. */
286 bcopy((caddr_t)aat->aat_enaddr, (caddr_t)desten,
287 sizeof(aat->aat_enaddr));
292 /* Entry has not completed. */
294 m_freem(aat->aat_hold);
298 aarpwhohas(ifp, destsat);
303 aarpintr(struct mbuf *m)
308 ifp = m->m_pkthdr.rcvif;
309 if (ifp->if_flags & IFF_NOARP)
312 if (m->m_len < sizeof(struct arphdr))
315 ar = mtod(m, struct arphdr *);
316 if (ntohs(ar->ar_hrd) != AARPHRD_ETHER)
319 if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln +
323 switch(ntohs(ar->ar_pro)) {
325 at_aarpinput(ifp, m);
336 at_aarpinput(struct ifnet *ifp, struct mbuf *m)
338 struct ether_aarp *ea;
339 struct at_ifaddr *aa;
341 struct ether_header *eh;
343 struct sockaddr_at sat;
345 struct at_addr spa, tpa, ma;
349 ea = mtod(m, struct ether_aarp *);
351 /* Check to see if from my hardware address. */
352 if (!bcmp((caddr_t)ea->aarp_sha, IF_LLADDR(ifp), ETHER_ADDR_LEN)) {
357 /* Don't accept requests from broadcast address. */
358 if (!bcmp(ea->aarp_sha, ifp->if_broadcastaddr, ifp->if_addrlen)) {
359 log(LOG_ERR, "aarp: source link address is broadcast\n");
364 op = ntohs(ea->aarp_op);
365 bcopy(ea->aarp_tpnet, &net, sizeof(net));
368 /* Should be ATADDR_ANYNET? */
369 sat.sat_len = sizeof(struct sockaddr_at);
370 sat.sat_family = AF_APPLETALK;
371 sat.sat_addr.s_net = net;
372 if ((aa = at_ifawithnet(&sat)) == NULL) {
376 bcopy(ea->aarp_spnet, &spa.s_net, sizeof(spa.s_net));
377 bcopy(ea->aarp_tpnet, &tpa.s_net, sizeof(tpa.s_net));
380 * Since we don't know the net, we just look for the first
381 * phase 1 address on the interface.
383 for (aa = (struct at_ifaddr *)TAILQ_FIRST(&ifp->if_addrhead);
385 aa = (struct at_ifaddr *)aa->aa_ifa.ifa_link.tqe_next) {
386 if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
387 (aa->aa_flags & AFA_PHASE2) == 0) {
395 tpa.s_net = spa.s_net = AA_SAT(aa)->sat_addr.s_net;
398 spa.s_node = ea->aarp_spnode;
399 tpa.s_node = ea->aarp_tpnode;
400 ma.s_net = AA_SAT(aa)->sat_addr.s_net;
401 ma.s_node = AA_SAT(aa)->sat_addr.s_node;
404 * This looks like it's from us.
406 if (spa.s_net == ma.s_net && spa.s_node == ma.s_node) {
407 if (aa->aa_flags & AFA_PROBING) {
409 * We're probing, someone either responded to our
410 * probe, or probed for the same address we'd like to
411 * use. Change the address we're probing for.
413 callout_stop(&aa->aa_callout);
417 } else if (op != AARPOP_PROBE) {
419 * This is not a probe, and we're not probing. This
420 * means that someone's saying they have the same
421 * source address as the one we're using. Get upset.
424 "aarp: duplicate AT address!! %x:%x:%x:%x:%x:%x\n",
425 ea->aarp_sha[0], ea->aarp_sha[1],
426 ea->aarp_sha[2], ea->aarp_sha[3],
427 ea->aarp_sha[4], ea->aarp_sha[5]);
434 AARPTAB_LOOK(aat, spa);
436 if (op == AARPOP_PROBE) {
438 * Someone's probing for spa, dealocate the one we've
439 * got, so that if the prober keeps the address,
440 * we'll be able to arp for him.
448 bcopy((caddr_t)ea->aarp_sha, (caddr_t)aat->aat_enaddr,
449 sizeof(ea->aarp_sha));
450 aat->aat_flags |= ATF_COM;
452 struct mbuf *mhold = aat->aat_hold;
453 aat->aat_hold = NULL;
455 sat.sat_len = sizeof(struct sockaddr_at);
456 sat.sat_family = AF_APPLETALK;
458 (*ifp->if_output)(ifp, mhold,
459 (struct sockaddr *)&sat, NULL); /* XXX */
462 } else if ((tpa.s_net == ma.s_net) && (tpa.s_node == ma.s_node)
463 && (op != AARPOP_PROBE) && ((aat = aarptnew(&spa)) != NULL)) {
464 bcopy((caddr_t)ea->aarp_sha, (caddr_t)aat->aat_enaddr,
465 sizeof(ea->aarp_sha));
466 aat->aat_flags |= ATF_COM;
472 * Don't respond to responses, and never respond if we're still
475 if (tpa.s_net != ma.s_net || tpa.s_node != ma.s_node ||
476 op == AARPOP_RESPONSE || (aa->aa_flags & AFA_PROBING)) {
481 bcopy((caddr_t)ea->aarp_sha, (caddr_t)ea->aarp_tha,
482 sizeof(ea->aarp_sha));
483 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha, sizeof(ea->aarp_sha));
486 eh = (struct ether_header *)sa.sa_data;
487 bcopy((caddr_t)ea->aarp_tha, (caddr_t)eh->ether_dhost,
488 sizeof(eh->ether_dhost));
490 if (aa->aa_flags & AFA_PHASE2) {
491 eh->ether_type = htons(sizeof(struct llc) +
492 sizeof(struct ether_aarp));
493 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
496 llc = mtod(m, struct llc *);
497 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
498 llc->llc_control = LLC_UI;
499 bcopy(aarp_org_code, llc->llc_org_code,
500 sizeof(aarp_org_code));
501 llc->llc_ether_type = htons(ETHERTYPE_AARP);
503 bcopy(ea->aarp_spnet, ea->aarp_tpnet,
504 sizeof(ea->aarp_tpnet));
505 bcopy(&ma.s_net, ea->aarp_spnet, sizeof(ea->aarp_spnet));
507 eh->ether_type = htons(ETHERTYPE_AARP);
509 ea->aarp_tpnode = ea->aarp_spnode;
510 ea->aarp_spnode = ma.s_node;
511 ea->aarp_op = htons(AARPOP_RESPONSE);
513 sa.sa_len = sizeof(struct sockaddr);
514 sa.sa_family = AF_UNSPEC;
515 (*ifp->if_output)(ifp, m, &sa, NULL); /* XXX */
520 aarptfree(struct aarptab *aat)
523 AARPTAB_LOCK_ASSERT();
525 m_freem(aat->aat_hold);
526 aat->aat_hold = NULL;
527 aat->aat_timer = aat->aat_flags = 0;
528 aat->aat_ataddr.s_net = 0;
529 aat->aat_ataddr.s_node = 0;
533 aarptnew(struct at_addr *addr)
537 struct aarptab *aat, *aato = NULL;
538 static int first = 1;
540 AARPTAB_LOCK_ASSERT();
543 aarptimer_ch = timeout(aarptimer, (caddr_t)0, hz);
545 aat = &aarptab[AARPTAB_HASH(*addr) * AARPTAB_BSIZ];
546 for (n = 0; n < AARPTAB_BSIZ; n++, aat++) {
547 if (aat->aat_flags == 0)
549 if (aat->aat_flags & ATF_PERM)
551 if ((int) aat->aat_timer > oldest) {
552 oldest = aat->aat_timer;
561 aat->aat_ataddr = *addr;
562 aat->aat_flags = ATF_INUSE;
570 struct ifnet *ifp = arg;
572 struct ether_header *eh;
573 struct ether_aarp *ea;
574 struct at_ifaddr *aa;
579 * We need to check whether the output ethernet type should be phase
580 * 1 or 2. We have the interface that we'll be sending the aarp out.
581 * We need to find an AppleTalk network on that interface with the
582 * same address as we're looking for. If the net is phase 2,
583 * generate an 802.2 and SNAP header.
586 for (aa = (struct at_ifaddr *)TAILQ_FIRST(&ifp->if_addrhead); aa;
587 aa = (struct at_ifaddr *)aa->aa_ifa.ifa_link.tqe_next) {
588 if (AA_SAT(aa)->sat_family == AF_APPLETALK &&
589 (aa->aa_flags & AFA_PROBING))
593 /* Serious error XXX. */
595 printf("aarpprobe why did this happen?!\n");
599 if (aa->aa_probcnt <= 0) {
600 aa->aa_flags &= ~AFA_PROBING;
605 callout_reset(&aa->aa_callout, hz / 5, aarpprobe, ifp);
608 m = m_gethdr(M_DONTWAIT, MT_DATA);
612 mac_netatalk_aarp_send(ifp, m);
614 m->m_len = sizeof(*ea);
615 m->m_pkthdr.len = sizeof(*ea);
616 MH_ALIGN(m, sizeof(*ea));
618 ea = mtod(m, struct ether_aarp *);
619 bzero((caddr_t)ea, sizeof(*ea));
621 ea->aarp_hrd = htons(AARPHRD_ETHER);
622 ea->aarp_pro = htons(ETHERTYPE_AT);
623 ea->aarp_hln = sizeof(ea->aarp_sha);
624 ea->aarp_pln = sizeof(ea->aarp_spu);
625 ea->aarp_op = htons(AARPOP_PROBE);
626 bcopy(IF_LLADDR(ifp), (caddr_t)ea->aarp_sha,
627 sizeof(ea->aarp_sha));
629 eh = (struct ether_header *)sa.sa_data;
631 if (aa->aa_flags & AFA_PHASE2) {
632 bcopy(atmulticastaddr, eh->ether_dhost,
633 sizeof(eh->ether_dhost));
634 eh->ether_type = htons(sizeof(struct llc) +
635 sizeof(struct ether_aarp));
636 M_PREPEND(m, sizeof(struct llc), M_WAIT);
637 llc = mtod(m, struct llc *);
638 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
639 llc->llc_control = LLC_UI;
640 bcopy(aarp_org_code, llc->llc_org_code,
641 sizeof(aarp_org_code));
642 llc->llc_ether_type = htons(ETHERTYPE_AARP);
644 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_spnet,
645 sizeof(ea->aarp_spnet));
646 bcopy(&AA_SAT(aa)->sat_addr.s_net, ea->aarp_tpnet,
647 sizeof(ea->aarp_tpnet));
648 ea->aarp_spnode = ea->aarp_tpnode =
649 AA_SAT(aa)->sat_addr.s_node;
651 bcopy(ifp->if_broadcastaddr, (caddr_t)eh->ether_dhost,
652 sizeof(eh->ether_dhost));
653 eh->ether_type = htons(ETHERTYPE_AARP);
654 ea->aarp_spa = ea->aarp_tpa = AA_SAT(aa)->sat_addr.s_node;
658 printf("aarp: sending probe for %u.%u\n",
659 ntohs(AA_SAT(aa)->sat_addr.s_net), AA_SAT(aa)->sat_addr.s_node);
660 #endif /* NETATALKDEBUG */
662 sa.sa_len = sizeof(struct sockaddr);
663 sa.sa_family = AF_UNSPEC;
664 (*ifp->if_output)(ifp, m, &sa, NULL); /* XXX */
674 untimeout(aarptimer, 0, aarptimer_ch);
676 for (i = 0, aat = aarptab; i < AARPTAB_SIZE; i++, aat++) {
678 m_freem(aat->aat_hold);
679 aat->aat_hold = NULL;