2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
10 #include <sys/sockio.h>
11 #include <sys/malloc.h>
12 #include <sys/kernel.h>
13 #include <sys/socket.h>
15 #include <net/route.h>
16 #include <netinet/in.h>
18 #include <netinet/if_ether.h>
20 #include <netatalk/at.h>
21 #include <netatalk/at_var.h>
22 #include <netatalk/at_extern.h>
24 struct at_ifaddr *at_ifaddr_list;
26 static int aa_dorangeroute(struct ifaddr *ifa,
27 u_int first, u_int last, int cmd);
28 static int aa_addsingleroute(struct ifaddr *ifa,
29 struct at_addr *addr, struct at_addr *mask);
30 static int aa_delsingleroute(struct ifaddr *ifa,
31 struct at_addr *addr, struct at_addr *mask);
32 static int aa_dosingleroute(struct ifaddr *ifa, struct at_addr *addr,
33 struct at_addr *mask, int cmd, int flags);
34 static int at_scrub(struct ifnet *ifp, struct at_ifaddr *aa);
35 static int at_ifinit(struct ifnet *ifp, struct at_ifaddr *aa,
36 struct sockaddr_at *sat);
37 static int aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw);
39 # define sateqaddr(a,b) ((a)->sat_len == (b)->sat_len && \
40 (a)->sat_family == (b)->sat_family && \
41 (a)->sat_addr.s_net == (b)->sat_addr.s_net && \
42 (a)->sat_addr.s_node == (b)->sat_addr.s_node)
45 at_control(struct socket *so, u_long cmd, caddr_t data,
46 struct ifnet *ifp, struct thread *td)
48 struct ifreq *ifr = (struct ifreq *)data;
49 struct sockaddr_at *sat;
51 struct at_aliasreq *ifra = (struct at_aliasreq *)data;
52 struct at_ifaddr *aa0;
53 struct at_ifaddr *aa = NULL;
54 struct ifaddr *ifa, *ifa0;
57 * If we have an ifp, then find the matching at_ifaddr if it exists
60 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
61 if (aa->aa_ifp == ifp) break;
66 * In this first switch table we are basically getting ready for
67 * the second one, by getting the atalk-specific things set up
68 * so that they start to look more similar to other protocols etc.
75 * If we have an appletalk sockaddr, scan forward of where
76 * we are now on the at_ifaddr list to find one with a matching
77 * address on this interface.
78 * This may leave aa pointing to the first address on the
81 if (ifra->ifra_addr.sat_family == AF_APPLETALK) {
82 for (; aa; aa = aa->aa_next) {
83 if (aa->aa_ifp == ifp &&
84 sateqaddr(&aa->aa_addr, &ifra->ifra_addr)) {
90 * If we a retrying to delete an addres but didn't find such,
91 * then rewurn with an error
93 if (cmd == SIOCDIFADDR && aa == NULL) {
94 return (EADDRNOTAVAIL);
100 * If we are not superuser, then we don't get to do these ops.
106 sat = satosat(&ifr->ifr_addr);
107 nr = (struct netrange *)sat->sat_zero;
108 if (nr->nr_phase == 1) {
110 * Look for a phase 1 address on this interface.
111 * This may leave aa pointing to the first address on the
114 for (; aa; aa = aa->aa_next) {
115 if (aa->aa_ifp == ifp &&
116 (aa->aa_flags & AFA_PHASE2) == 0) {
120 } else { /* default to phase 2 */
122 * Look for a phase 2 address on this interface.
123 * This may leave aa pointing to the first address on the
126 for (; aa; aa = aa->aa_next) {
127 if (aa->aa_ifp == ifp && (aa->aa_flags & AFA_PHASE2)) {
137 * If we failed to find an existing at_ifaddr entry, then we
138 * allocate a fresh one.
141 aa0 = malloc(sizeof(struct at_ifaddr), M_IFADDR, M_WAITOK | M_ZERO);
142 if ((aa = at_ifaddr_list) != NULL) {
144 * Don't let the loopback be first, since the first
145 * address is the machine's default address for
147 * If it is, stick ourself in front, otherwise
148 * go to the back of the list.
150 if (at_ifaddr_list->aa_ifp->if_flags & IFF_LOOPBACK) {
152 aa->aa_next = at_ifaddr_list;
155 for (; aa->aa_next; aa = aa->aa_next)
160 at_ifaddr_list = aa0;
165 * Find the end of the interface's addresses
166 * and link our new one on the end
168 ifa = (struct ifaddr *)aa;
171 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
174 * As the at_ifaddr contains the actual sockaddrs,
175 * and the ifaddr itself, link them al together correctly.
177 ifa->ifa_addr = (struct sockaddr *)&aa->aa_addr;
178 ifa->ifa_dstaddr = (struct sockaddr *)&aa->aa_addr;
179 ifa->ifa_netmask = (struct sockaddr *)&aa->aa_netmask;
182 * Set/clear the phase 2 bit.
184 if (nr->nr_phase == 1) {
185 aa->aa_flags &= ~AFA_PHASE2;
187 aa->aa_flags |= AFA_PHASE2;
191 * and link it all together
196 * If we DID find one then we clobber any routes dependent on it..
203 sat = satosat(&ifr->ifr_addr);
204 nr = (struct netrange *)sat->sat_zero;
205 if (nr->nr_phase == 1) {
207 * If the request is specifying phase 1, then
208 * only look at a phase one address
210 for (; aa; aa = aa->aa_next) {
211 if (aa->aa_ifp == ifp &&
212 (aa->aa_flags & AFA_PHASE2) == 0) {
220 for (; aa; aa = aa->aa_next) {
221 if (aa->aa_ifp == ifp && (aa->aa_flags & AFA_PHASE2)) {
228 return (EADDRNOTAVAIL);
233 * By the time this switch is run we should be able to assume that
234 * the "aa" pointer is valid when needed.
240 * copy the contents of the sockaddr blindly.
242 sat = (struct sockaddr_at *)&ifr->ifr_addr;
246 * and do some cleanups
248 ((struct netrange *)&sat->sat_zero)->nr_phase
249 = (aa->aa_flags & AFA_PHASE2) ? 2 : 1;
250 ((struct netrange *)&sat->sat_zero)->nr_firstnet = aa->aa_firstnet;
251 ((struct netrange *)&sat->sat_zero)->nr_lastnet = aa->aa_lastnet;
255 return (at_ifinit(ifp, aa, (struct sockaddr_at *)&ifr->ifr_addr));
258 if (sateqaddr(&ifra->ifra_addr, &aa->aa_addr)) {
261 return (at_ifinit(ifp, aa, (struct sockaddr_at *)&ifr->ifr_addr));
265 * scrub all routes.. didn't we just DO this? XXX yes, del it
270 * remove the ifaddr from the interface
272 ifa0 = (struct ifaddr *)aa;
273 TAILQ_REMOVE(&ifp->if_addrhead, ifa0, ifa_link);
276 * Now remove the at_ifaddr from the parallel structure
277 * as well, or we'd be in deep trouble
280 if (aa0 == (aa = at_ifaddr_list)) {
281 at_ifaddr_list = aa->aa_next;
283 while (aa->aa_next && (aa->aa_next != aa0)) {
288 * if we found it, remove it, otherwise we screwed up.
291 aa->aa_next = aa0->aa_next;
298 * Now reclaim the reference.
304 if (ifp == NULL || ifp->if_ioctl == NULL)
306 return ((*ifp->if_ioctl)(ifp, cmd, data));
312 * Given an interface and an at_ifaddr (supposedly on that interface)
313 * remove any routes that depend on this.
314 * Why ifp is needed I'm not sure,
315 * as aa->at_ifaddr.ifa_ifp should be the same.
320 struct at_ifaddr *aa;
324 if (aa->aa_flags & AFA_ROUTE) {
325 if (ifp->if_flags & IFF_LOOPBACK) {
326 if ((error = aa_delsingleroute(&aa->aa_ifa,
327 &aa->aa_addr.sat_addr,
328 &aa->aa_netmask.sat_addr)) != 0) {
331 } else if (ifp->if_flags & IFF_POINTOPOINT) {
332 if ((error = rtinit(&aa->aa_ifa, RTM_DELETE, RTF_HOST)) != 0)
334 } else if (ifp->if_flags & IFF_BROADCAST) {
335 error = aa_dorangeroute(&aa->aa_ifa,
336 ntohs(aa->aa_firstnet),
337 ntohs(aa->aa_lastnet),
340 aa->aa_ifa.ifa_flags &= ~IFA_ROUTE;
341 aa->aa_flags &= ~AFA_ROUTE;
347 * given an at_ifaddr,a sockaddr_at and an ifp,
348 * bang them all together at high speed and see what happens
351 at_ifinit(ifp, aa, sat)
353 struct at_ifaddr *aa;
354 struct sockaddr_at *sat;
356 struct netrange nr, onr;
357 struct sockaddr_at oldaddr;
358 int s = splimp(), error = 0, i, j;
359 int netinc, nodeinc, nnets;
363 * save the old addresses in the at_ifaddr just in case we need them.
365 oldaddr = aa->aa_addr;
366 onr.nr_firstnet = aa->aa_firstnet;
367 onr.nr_lastnet = aa->aa_lastnet;
370 * take the address supplied as an argument, and add it to the
371 * at_ifnet (also given). Remember ing to update
372 * those parts of the at_ifaddr that need special processing
374 bzero(AA_SAT(aa), sizeof(struct sockaddr_at));
375 bcopy(sat->sat_zero, &nr, sizeof(struct netrange));
376 bcopy(sat->sat_zero, AA_SAT(aa)->sat_zero, sizeof(struct netrange));
377 nnets = ntohs(nr.nr_lastnet) - ntohs(nr.nr_firstnet) + 1;
378 aa->aa_firstnet = nr.nr_firstnet;
379 aa->aa_lastnet = nr.nr_lastnet;
383 printf("at_ifinit: %s: %u.%u range %u-%u phase %d\n",
385 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
386 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet),
387 (aa->aa_flags & AFA_PHASE2) ? 2 : 1);
391 * We could eliminate the need for a second phase 1 probe (post
392 * autoconf) if we check whether we're resetting the node. Note
393 * that phase 1 probes use only nodes, not net.node pairs. Under
394 * phase 2, both the net and node must be the same.
396 if (ifp->if_flags & IFF_LOOPBACK) {
397 AA_SAT(aa)->sat_len = sat->sat_len;
398 AA_SAT(aa)->sat_family = AF_APPLETALK;
399 AA_SAT(aa)->sat_addr.s_net = sat->sat_addr.s_net;
400 AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
402 } else if (fp->if_flags & IFF_POINTOPOINT) {
405 * we'd have to copy the dstaddr field over from the sat
406 * but it's not clear that it would contain the right info..
411 * We are a normal (probably ethernet) interface.
412 * apply the new address to the interface structures etc.
413 * We will probe this address on the net first, before
414 * applying it to ensure that it is free.. If it is not, then
415 * we will try a number of other randomly generated addresses
416 * in this net and then increment the net. etc.etc. until
417 * we find an unused address.
419 aa->aa_flags |= AFA_PROBING; /* if not loopback we Must probe? */
420 AA_SAT(aa)->sat_len = sizeof(struct sockaddr_at);
421 AA_SAT(aa)->sat_family = AF_APPLETALK;
422 if (aa->aa_flags & AFA_PHASE2) {
423 if (sat->sat_addr.s_net == ATADDR_ANYNET) {
425 * If we are phase 2, and the net was not specified
426 * then we select a random net within the supplied netrange.
427 * XXX use /dev/random?
430 net = ntohs(nr.nr_firstnet) + time_second % (nnets - 1);
432 net = ntohs(nr.nr_firstnet);
436 * if a net was supplied, then check that it is within
437 * the netrange. If it is not then replace the old values
438 * and return an error
440 if (ntohs(sat->sat_addr.s_net) < ntohs(nr.nr_firstnet) ||
441 ntohs(sat->sat_addr.s_net) > ntohs(nr.nr_lastnet)) {
442 aa->aa_addr = oldaddr;
443 aa->aa_firstnet = onr.nr_firstnet;
444 aa->aa_lastnet = onr.nr_lastnet;
449 * otherwise just use the new net number..
451 net = ntohs(sat->sat_addr.s_net);
455 * we must be phase one, so just use whatever we were given.
456 * I guess it really isn't going to be used... RIGHT?
458 net = ntohs(sat->sat_addr.s_net);
462 * set the node part of the address into the ifaddr.
463 * If it's not specified, be random about it...
464 * XXX use /dev/random?
466 if (sat->sat_addr.s_node == ATADDR_ANYNODE) {
467 AA_SAT(aa)->sat_addr.s_node = time_second;
469 AA_SAT(aa)->sat_addr.s_node = sat->sat_addr.s_node;
475 AA_SAT(aa)->sat_range.r_netrange.nr_phase
476 = ((aa->aa_flags & AFA_PHASE2) ? 2:1);
479 * step through the nets in the range
480 * starting at the (possibly random) start point.
482 for (i = nnets, netinc = 1; i > 0; net = ntohs(nr.nr_firstnet) +
483 ((net - ntohs(nr.nr_firstnet) + netinc) % nnets), i--) {
484 AA_SAT(aa)->sat_addr.s_net = htons(net);
487 * using a rather strange stepping method,
488 * stagger through the possible node addresses
489 * Once again, starting at the (possibly random)
490 * initial node address.
492 for (j = 0, nodeinc = time_second | 1; j < 256;
493 j++, AA_SAT(aa)->sat_addr.s_node += nodeinc) {
494 if (AA_SAT(aa)->sat_addr.s_node > 253 ||
495 AA_SAT(aa)->sat_addr.s_node < 1) {
501 * start off the probes as an asynchronous activity.
502 * though why wait 200mSec?
504 aa->aa_ch = timeout(aarpprobe, (caddr_t)ifp, hz / 5);
505 if (tsleep(aa, PPAUSE|PCATCH, "at_ifinit", 0)) {
507 * theoretically we shouldn't time out here
508 * so if we returned with an error..
510 printf("at_ifinit: why did this happen?!\n");
511 aa->aa_addr = oldaddr;
512 aa->aa_firstnet = onr.nr_firstnet;
513 aa->aa_lastnet = onr.nr_lastnet;
519 * The async activity should have woken us up.
520 * We need to see if it was successful in finding
521 * a free spot, or if we need to iterate to the next
524 if ((aa->aa_flags & AFA_PROBING) == 0) {
530 * of course we need to break out through two loops...
532 if ((aa->aa_flags & AFA_PROBING) == 0) {
535 /* reset node for next network */
536 AA_SAT(aa)->sat_addr.s_node = time_second;
540 * if we are still trying to probe, then we have finished all
541 * the possible addresses, so we need to give up
544 if (aa->aa_flags & AFA_PROBING) {
545 aa->aa_addr = oldaddr;
546 aa->aa_firstnet = onr.nr_firstnet;
547 aa->aa_lastnet = onr.nr_lastnet;
554 * Now that we have selected an address, we need to tell the interface
555 * about it, just in case it needs to adjust something.
557 if (ifp->if_ioctl != NULL&&
558 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)aa))) {
560 * of course this could mean that it objects violently
561 * so if it does, we back out again..
563 aa->aa_addr = oldaddr;
564 aa->aa_firstnet = onr.nr_firstnet;
565 aa->aa_lastnet = onr.nr_lastnet;
571 * set up the netmask part of the at_ifaddr
572 * and point the appropriate pointer in the ifaddr to it.
573 * probably pointless, but what the heck.. XXX
575 bzero(&aa->aa_netmask, sizeof(aa->aa_netmask));
576 aa->aa_netmask.sat_len = sizeof(struct sockaddr_at);
577 aa->aa_netmask.sat_family = AF_APPLETALK;
578 aa->aa_netmask.sat_addr.s_net = 0xffff;
579 aa->aa_netmask.sat_addr.s_node = 0;
580 aa->aa_ifa.ifa_netmask =(struct sockaddr *) &(aa->aa_netmask); /* XXX */
583 * Initialize broadcast (or remote p2p) address
585 bzero(&aa->aa_broadaddr, sizeof(aa->aa_broadaddr));
586 aa->aa_broadaddr.sat_len = sizeof(struct sockaddr_at);
587 aa->aa_broadaddr.sat_family = AF_APPLETALK;
589 aa->aa_ifa.ifa_metric = ifp->if_metric;
590 if (ifp->if_flags & IFF_BROADCAST) {
591 aa->aa_broadaddr.sat_addr.s_net = htons(0);
592 aa->aa_broadaddr.sat_addr.s_node = 0xff;
593 aa->aa_ifa.ifa_broadaddr = (struct sockaddr *) &aa->aa_broadaddr;
594 /* add the range of routes needed */
595 error = aa_dorangeroute(&aa->aa_ifa,
596 ntohs(aa->aa_firstnet), ntohs(aa->aa_lastnet), RTM_ADD);
598 else if (ifp->if_flags & IFF_POINTOPOINT) {
599 struct at_addr rtaddr, rtmask;
601 bzero(&rtaddr, sizeof(rtaddr));
602 bzero(&rtmask, sizeof(rtmask));
603 /* fill in the far end if we know it here XXX */
604 aa->aa_ifa.ifa_dstaddr = (struct sockaddr *) &aa->aa_dstaddr;
605 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
607 else if (ifp->if_flags & IFF_LOOPBACK) {
608 struct at_addr rtaddr, rtmask;
610 bzero(&rtaddr, sizeof(rtaddr));
611 bzero(&rtmask, sizeof(rtmask));
612 rtaddr.s_net = AA_SAT(aa)->sat_addr.s_net;
613 rtaddr.s_node = AA_SAT(aa)->sat_addr.s_node;
614 rtmask.s_net = 0xffff;
615 rtmask.s_node = 0x0; /* XXX should not be so.. should be HOST route */
616 error = aa_addsingleroute(&aa->aa_ifa, &rtaddr, &rtmask);
621 * set the address of our "check if this addr is ours" routine.
623 aa->aa_ifa.ifa_claim_addr = aa_claim_addr;
626 * of course if we can't add these routes we back out, but it's getting
631 aa->aa_addr = oldaddr;
632 aa->aa_firstnet = onr.nr_firstnet;
633 aa->aa_lastnet = onr.nr_lastnet;
639 * note that the address has a route associated with it....
641 aa->aa_ifa.ifa_flags |= IFA_ROUTE;
642 aa->aa_flags |= AFA_ROUTE;
648 * check whether a given address is a broadcast address for us..
652 struct sockaddr_at *sat;
654 struct at_ifaddr *aa;
657 * If the node is not right, it can't be a broadcast
659 if (sat->sat_addr.s_node != ATADDR_BCAST) {
664 * If the node was right then if the net is right, it's a broadcast
666 if (sat->sat_addr.s_net == ATADDR_ANYNET) {
671 * failing that, if the net is one we have, it's a broadcast as well.
673 for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
674 if ((aa->aa_ifp->if_flags & IFF_BROADCAST)
675 && (ntohs(sat->sat_addr.s_net) >= ntohs(aa->aa_firstnet)
676 && ntohs(sat->sat_addr.s_net) <= ntohs(aa->aa_lastnet))) {
686 * Add a route for a range of networks from bot to top - 1.
689 * Split the range into two subranges such that the middle
690 * of the two ranges is the point where the highest bit of difference
691 * between the two addresses makes its transition.
692 * Each of the upper and lower ranges might not exist, or might be
693 * representable by 1 or more netmasks. In addition, if both
694 * ranges can be represented by the same netmask, then they can be merged
695 * by using the next higher netmask..
699 aa_dorangeroute(struct ifaddr *ifa, u_int bot, u_int top, int cmd)
707 * slight sanity check
709 if (bot > top) return (EINVAL);
714 * just start out with the lowest boundary
715 * and keep extending the mask till it's too big.
720 while (((bot & ~mask1) >= bot)
721 && ((bot | mask1) <= top)) {
726 mask.s_net = htons(~mask1);
727 addr.s_net = htons(bot);
729 error = aa_addsingleroute(ifa,&addr,&mask);
735 error = aa_delsingleroute(ifa,&addr,&mask);
737 bot = (bot | mask1) + 1;
743 aa_addsingleroute(struct ifaddr *ifa,
744 struct at_addr *addr, struct at_addr *mask)
749 printf("aa_addsingleroute: %x.%x mask %x.%x ...\n",
750 ntohs(addr->s_net), addr->s_node,
751 ntohs(mask->s_net), mask->s_node);
754 error = aa_dosingleroute(ifa, addr, mask, RTM_ADD, RTF_UP);
756 printf("aa_addsingleroute: error %d\n", error);
761 aa_delsingleroute(struct ifaddr *ifa,
762 struct at_addr *addr, struct at_addr *mask)
766 error = aa_dosingleroute(ifa, addr, mask, RTM_DELETE, 0);
768 printf("aa_delsingleroute: error %d\n", error);
773 aa_dosingleroute(struct ifaddr *ifa,
774 struct at_addr *at_addr, struct at_addr *at_mask, int cmd, int flags)
776 struct sockaddr_at addr, mask;
778 bzero(&addr, sizeof(addr));
779 bzero(&mask, sizeof(mask));
780 addr.sat_family = AF_APPLETALK;
781 addr.sat_len = sizeof(struct sockaddr_at);
782 addr.sat_addr.s_net = at_addr->s_net;
783 addr.sat_addr.s_node = at_addr->s_node;
784 mask.sat_family = AF_APPLETALK;
785 mask.sat_len = sizeof(struct sockaddr_at);
786 mask.sat_addr.s_net = at_mask->s_net;
787 mask.sat_addr.s_node = at_mask->s_node;
790 return (rtrequest(cmd, (struct sockaddr *) &addr,
791 (flags & RTF_HOST)?(ifa->ifa_dstaddr):(ifa->ifa_addr),
792 (struct sockaddr *) &mask, flags, NULL));
800 struct at_ifaddr *aa;
804 while ((aa = at_ifaddr_list) != NULL) {
807 at_ifaddr_list = aa->aa_next;
808 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)aa) {
809 ifp->if_addrlist = ifa->ifa_next;
811 while (ifa->ifa_next &&
812 (ifa->ifa_next != (struct ifaddr *)aa)) {
816 ifa->ifa_next = ((struct ifaddr *)aa)->ifa_next;
827 aa_claim_addr(struct ifaddr *ifa, struct sockaddr *gw0)
829 struct sockaddr_at *addr = (struct sockaddr_at *)ifa->ifa_addr;
830 struct sockaddr_at *gw = (struct sockaddr_at *)gw0;
832 switch (gw->sat_range.r_netrange.nr_phase) {
834 if(addr->sat_range.r_netrange.nr_phase == 1)
839 * if it's our net (including 0),
840 * or netranges are valid, and we are in the range,
843 if ((addr->sat_addr.s_net == gw->sat_addr.s_net)
844 || ((addr->sat_range.r_netrange.nr_lastnet)
845 && (ntohs(gw->sat_addr.s_net)
846 >= ntohs(addr->sat_range.r_netrange.nr_firstnet))
847 && (ntohs(gw->sat_addr.s_net)
848 <= ntohs(addr->sat_range.r_netrange.nr_lastnet)))) {
853 printf("atalk: bad phase\n");