allow this to compile
[dragonfly.git] / sys / net / if.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/net/if.c,v 1.85.2.23 2003/04/15 18:11:19 fjoe Exp $
158abb01 35 * $DragonFly: src/sys/net/if.c,v 1.6 2003/08/26 20:49:47 rob Exp $
984263bc
MD
36 */
37
38#include "opt_compat.h"
39#include "opt_inet6.h"
40#include "opt_inet.h"
41
42#include <sys/param.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/systm.h>
46#include <sys/proc.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/protosw.h>
50#include <sys/kernel.h>
51#include <sys/sockio.h>
52#include <sys/syslog.h>
53#include <sys/sysctl.h>
54
55#include <net/if.h>
56#include <net/if_arp.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/if_var.h>
60#include <net/radix.h>
61#include <net/route.h>
62#include <machine/stdarg.h>
63
64#if defined(INET) || defined(INET6)
65/*XXX*/
66#include <netinet/in.h>
67#include <netinet/in_var.h>
68#include <netinet/if_ether.h>
69#ifdef INET6
70#include <machine/clock.h> /* XXX: temporal workaround for fxp issue */
71#include <netinet6/in6_var.h>
72#include <netinet6/in6_ifattach.h>
73#endif
74#endif
75
76/*
77 * System initialization
78 */
79
158abb01
RG
80static int ifconf (u_long, caddr_t);
81static void ifinit (void *);
82static void if_qflush (struct ifqueue *);
83static void if_slowtimo (void *);
84static void link_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
85static int if_rtdel (struct radix_node *, void *);
984263bc
MD
86
87SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
88
89MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
90MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
91
92int ifqmaxlen = IFQ_MAXLEN;
93struct ifnethead ifnet; /* depend on static init XXX */
94
95#ifdef INET6
96/*
97 * XXX: declare here to avoid to include many inet6 related files..
98 * should be more generalized?
99 */
158abb01 100extern void nd6_setmtu (struct ifnet *);
984263bc
MD
101#endif
102
158abb01
RG
103struct if_clone *if_clone_lookup (const char *, int *);
104int if_clone_list (struct if_clonereq *);
984263bc
MD
105
106LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
107int if_cloners_count;
108
109/*
110 * Network interface utility routines.
111 *
112 * Routines with ifa_ifwith* names take sockaddr *'s as
113 * parameters.
114 */
115/* ARGSUSED*/
116void
117ifinit(dummy)
118 void *dummy;
119{
120 struct ifnet *ifp;
121 int s;
122
123 s = splimp();
124 TAILQ_FOREACH(ifp, &ifnet, if_link) {
125 if (ifp->if_snd.ifq_maxlen == 0) {
126 printf("%s%d XXX: driver didn't set ifq_maxlen\n",
127 ifp->if_name, ifp->if_unit);
128 ifp->if_snd.ifq_maxlen = ifqmaxlen;
129 }
130 }
131 splx(s);
132 if_slowtimo(0);
133}
134
135int if_index = 0;
136struct ifaddr **ifnet_addrs;
137struct ifnet **ifindex2ifnet = NULL;
138
139
140/*
141 * Attach an interface to the
142 * list of "active" interfaces.
143 */
144void
145if_attach(ifp)
146 struct ifnet *ifp;
147{
148 unsigned socksize, ifasize;
149 int namelen, masklen;
150 char workbuf[64];
82ed7fc2
RG
151 struct sockaddr_dl *sdl;
152 struct ifaddr *ifa;
984263bc
MD
153 static int if_indexlim = 8;
154 static int inited;
155
156 if (!inited) {
157 TAILQ_INIT(&ifnet);
158 inited = 1;
159 }
160
161 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
162 ifp->if_index = ++if_index;
163 /*
164 * XXX -
165 * The old code would work if the interface passed a pre-existing
166 * chain of ifaddrs to this code. We don't trust our callers to
167 * properly initialize the tailq, however, so we no longer allow
168 * this unlikely case.
169 */
170 TAILQ_INIT(&ifp->if_addrhead);
171 TAILQ_INIT(&ifp->if_prefixhead);
172 LIST_INIT(&ifp->if_multiaddrs);
173 getmicrotime(&ifp->if_lastchange);
174 if (ifnet_addrs == 0 || if_index >= if_indexlim) {
175 unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
176 caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
177 bzero(q, n);
178 if (ifnet_addrs) {
179 bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
180 free((caddr_t)ifnet_addrs, M_IFADDR);
181 }
182 ifnet_addrs = (struct ifaddr **)q;
183
184 /* grow ifindex2ifnet */
185 n = if_indexlim * sizeof(struct ifnet *);
186 q = malloc(n, M_IFADDR, M_WAITOK);
187 bzero(q, n);
188 if (ifindex2ifnet) {
189 bcopy((caddr_t)ifindex2ifnet, q, n/2);
190 free((caddr_t)ifindex2ifnet, M_IFADDR);
191 }
192 ifindex2ifnet = (struct ifnet **)q;
193 }
194
195 ifindex2ifnet[if_index] = ifp;
196
197 /*
198 * create a Link Level name for this device
199 */
200 namelen = snprintf(workbuf, sizeof(workbuf),
201 "%s%d", ifp->if_name, ifp->if_unit);
202#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
203 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
204 socksize = masklen + ifp->if_addrlen;
205#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
206 if (socksize < sizeof(*sdl))
207 socksize = sizeof(*sdl);
208 socksize = ROUNDUP(socksize);
209 ifasize = sizeof(*ifa) + 2 * socksize;
210 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
211 if (ifa) {
212 bzero((caddr_t)ifa, ifasize);
213 sdl = (struct sockaddr_dl *)(ifa + 1);
214 sdl->sdl_len = socksize;
215 sdl->sdl_family = AF_LINK;
216 bcopy(workbuf, sdl->sdl_data, namelen);
217 sdl->sdl_nlen = namelen;
218 sdl->sdl_index = ifp->if_index;
219 sdl->sdl_type = ifp->if_type;
220 ifnet_addrs[if_index - 1] = ifa;
221 ifa->ifa_ifp = ifp;
222 ifa->ifa_rtrequest = link_rtrequest;
223 ifa->ifa_addr = (struct sockaddr *)sdl;
224 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
225 ifa->ifa_netmask = (struct sockaddr *)sdl;
226 sdl->sdl_len = masklen;
227 while (namelen != 0)
228 sdl->sdl_data[--namelen] = 0xff;
229 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
230 }
231
232 /* Announce the interface. */
233 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
234}
235
236/*
237 * Detach an interface, removing it from the
238 * list of "active" interfaces.
239 */
240void
241if_detach(ifp)
242 struct ifnet *ifp;
243{
244 struct ifaddr *ifa;
245 struct radix_node_head *rnh;
246 int s;
247 int i;
248
249 /*
250 * Remove routes and flush queues.
251 */
252 s = splnet();
253 if_down(ifp);
254
255 /*
256 * Remove address from ifnet_addrs[] and maybe decrement if_index.
257 * Clean up all addresses.
258 */
259 ifnet_addrs[ifp->if_index - 1] = 0;
260 while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
261 if_index--;
262
263 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
264 ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
265#ifdef INET
266 /* XXX: Ugly!! ad hoc just for INET */
267 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
268 struct ifaliasreq ifr;
269
270 bzero(&ifr, sizeof(ifr));
271 ifr.ifra_addr = *ifa->ifa_addr;
272 if (ifa->ifa_dstaddr)
273 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
274 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
275 NULL) == 0)
276 continue;
277 }
278#endif /* INET */
279#ifdef INET6
280 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
281 in6_purgeaddr(ifa);
282 /* ifp_addrhead is already updated */
283 continue;
284 }
285#endif /* INET6 */
286 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
287 IFAFREE(ifa);
288 }
289
290#ifdef INET6
291 /*
292 * Remove all IPv6 kernel structs related to ifp. This should be done
293 * before removing routing entries below, since IPv6 interface direct
294 * routes are expected to be removed by the IPv6-specific kernel API.
295 * Otherwise, the kernel will detect some inconsistency and bark it.
296 */
297 in6_ifdetach(ifp);
298#endif
299
300 /*
301 * Delete all remaining routes using this interface
302 * Unfortuneatly the only way to do this is to slog through
303 * the entire routing table looking for routes which point
304 * to this interface...oh well...
305 */
306 for (i = 1; i <= AF_MAX; i++) {
307 if ((rnh = rt_tables[i]) == NULL)
308 continue;
309 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
310 }
311
312 /* Announce that the interface is gone. */
313 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
314
315 TAILQ_REMOVE(&ifnet, ifp, if_link);
316 splx(s);
317}
318
319/*
320 * Delete Routes for a Network Interface
321 *
322 * Called for each routing entry via the rnh->rnh_walktree() call above
323 * to delete all route entries referencing a detaching network interface.
324 *
325 * Arguments:
326 * rn pointer to node in the routing table
327 * arg argument passed to rnh->rnh_walktree() - detaching interface
328 *
329 * Returns:
330 * 0 successful
331 * errno failed - reason indicated
332 *
333 */
334static int
335if_rtdel(rn, arg)
336 struct radix_node *rn;
337 void *arg;
338{
339 struct rtentry *rt = (struct rtentry *)rn;
340 struct ifnet *ifp = arg;
341 int err;
342
343 if (rt->rt_ifp == ifp) {
344
345 /*
346 * Protect (sorta) against walktree recursion problems
347 * with cloned routes
348 */
349 if ((rt->rt_flags & RTF_UP) == 0)
350 return (0);
351
352 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
353 rt_mask(rt), rt->rt_flags,
354 (struct rtentry **) NULL);
355 if (err) {
356 log(LOG_WARNING, "if_rtdel: error %d\n", err);
357 }
358 }
359
360 return (0);
361}
362
363/*
364 * Create a clone network interface.
365 */
366int
367if_clone_create(name, len)
368 char *name;
369 int len;
370{
371 struct if_clone *ifc;
372 char *dp;
373 int wildcard;
374 int unit;
375 int err;
376
377 ifc = if_clone_lookup(name, &unit);
378 if (ifc == NULL)
379 return (EINVAL);
380
381 if (ifunit(name) != NULL)
382 return (EEXIST);
383
384 wildcard = (unit < 0);
385
386 err = (*ifc->ifc_create)(ifc, &unit);
387 if (err != 0)
388 return (err);
389
390 /* In the wildcard case, we need to update the name. */
391 if (wildcard) {
392 for (dp = name; *dp != '\0'; dp++);
393 if (snprintf(dp, len - (dp-name), "%d", unit) >
394 len - (dp-name) - 1) {
395 /*
396 * This can only be a programmer error and
397 * there's no straightforward way to recover if
398 * it happens.
399 */
400 panic("if_clone_create(): interface name too long");
401 }
402
403 }
404
405 return (0);
406}
407
408/*
409 * Destroy a clone network interface.
410 */
411int
412if_clone_destroy(name)
413 const char *name;
414{
415 struct if_clone *ifc;
416 struct ifnet *ifp;
417
418 ifc = if_clone_lookup(name, NULL);
419 if (ifc == NULL)
420 return (EINVAL);
421
422 ifp = ifunit(name);
423 if (ifp == NULL)
424 return (ENXIO);
425
426 if (ifc->ifc_destroy == NULL)
427 return (EOPNOTSUPP);
428
429 (*ifc->ifc_destroy)(ifp);
430 return (0);
431}
432
433/*
434 * Look up a network interface cloner.
435 */
436struct if_clone *
437if_clone_lookup(name, unitp)
438 const char *name;
439 int *unitp;
440{
441 struct if_clone *ifc;
442 const char *cp;
443 int i;
444
445 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
446 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
447 if (ifc->ifc_name[i] != *cp)
448 goto next_ifc;
449 }
450 goto found_name;
451 next_ifc:
452 ifc = LIST_NEXT(ifc, ifc_list);
453 }
454
455 /* No match. */
456 return ((struct if_clone *)NULL);
457
458 found_name:
459 if (*cp == '\0') {
460 i = -1;
461 } else {
462 for (i = 0; *cp != '\0'; cp++) {
463 if (*cp < '0' || *cp > '9') {
464 /* Bogus unit number. */
465 return (NULL);
466 }
467 i = (i * 10) + (*cp - '0');
468 }
469 }
470
471 if (unitp != NULL)
472 *unitp = i;
473 return (ifc);
474}
475
476/*
477 * Register a network interface cloner.
478 */
479void
480if_clone_attach(ifc)
481 struct if_clone *ifc;
482{
483
484 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
485 if_cloners_count++;
486}
487
488/*
489 * Unregister a network interface cloner.
490 */
491void
492if_clone_detach(ifc)
493 struct if_clone *ifc;
494{
495
496 LIST_REMOVE(ifc, ifc_list);
497 if_cloners_count--;
498}
499
500/*
501 * Provide list of interface cloners to userspace.
502 */
503int
504if_clone_list(ifcr)
505 struct if_clonereq *ifcr;
506{
507 char outbuf[IFNAMSIZ], *dst;
508 struct if_clone *ifc;
509 int count, error = 0;
510
511 ifcr->ifcr_total = if_cloners_count;
512 if ((dst = ifcr->ifcr_buffer) == NULL) {
513 /* Just asking how many there are. */
514 return (0);
515 }
516
517 if (ifcr->ifcr_count < 0)
518 return (EINVAL);
519
520 count = (if_cloners_count < ifcr->ifcr_count) ?
521 if_cloners_count : ifcr->ifcr_count;
522
523 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
524 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
525 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
526 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
527 error = copyout(outbuf, dst, IFNAMSIZ);
528 if (error)
529 break;
530 }
531
532 return (error);
533}
534
535/*
536 * Locate an interface based on a complete address.
537 */
538/*ARGSUSED*/
539struct ifaddr *
540ifa_ifwithaddr(addr)
82ed7fc2 541 struct sockaddr *addr;
984263bc 542{
82ed7fc2
RG
543 struct ifnet *ifp;
544 struct ifaddr *ifa;
984263bc
MD
545
546#define equal(a1, a2) \
547 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
548 TAILQ_FOREACH(ifp, &ifnet, if_link)
549 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
550 if (ifa->ifa_addr->sa_family != addr->sa_family)
551 continue;
552 if (equal(addr, ifa->ifa_addr))
553 return (ifa);
554 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
555 /* IP6 doesn't have broadcast */
556 ifa->ifa_broadaddr->sa_len != 0 &&
557 equal(ifa->ifa_broadaddr, addr))
558 return (ifa);
559 }
560 return ((struct ifaddr *)0);
561}
562/*
563 * Locate the point to point interface with a given destination address.
564 */
565/*ARGSUSED*/
566struct ifaddr *
567ifa_ifwithdstaddr(addr)
82ed7fc2 568 struct sockaddr *addr;
984263bc 569{
82ed7fc2
RG
570 struct ifnet *ifp;
571 struct ifaddr *ifa;
984263bc
MD
572
573 TAILQ_FOREACH(ifp, &ifnet, if_link)
574 if (ifp->if_flags & IFF_POINTOPOINT)
575 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
576 if (ifa->ifa_addr->sa_family != addr->sa_family)
577 continue;
578 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
579 return (ifa);
580 }
581 return ((struct ifaddr *)0);
582}
583
584/*
585 * Find an interface on a specific network. If many, choice
586 * is most specific found.
587 */
588struct ifaddr *
589ifa_ifwithnet(addr)
590 struct sockaddr *addr;
591{
82ed7fc2
RG
592 struct ifnet *ifp;
593 struct ifaddr *ifa;
984263bc
MD
594 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
595 u_int af = addr->sa_family;
596 char *addr_data = addr->sa_data, *cplim;
597
598 /*
599 * AF_LINK addresses can be looked up directly by their index number,
600 * so do that if we can.
601 */
602 if (af == AF_LINK) {
82ed7fc2 603 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
984263bc
MD
604 if (sdl->sdl_index && sdl->sdl_index <= if_index)
605 return (ifnet_addrs[sdl->sdl_index - 1]);
606 }
607
608 /*
609 * Scan though each interface, looking for ones that have
610 * addresses in this address family.
611 */
612 TAILQ_FOREACH(ifp, &ifnet, if_link) {
613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
82ed7fc2 614 char *cp, *cp2, *cp3;
984263bc
MD
615
616 if (ifa->ifa_addr->sa_family != af)
617next: continue;
618 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
619 /*
620 * This is a bit broken as it doesn't
621 * take into account that the remote end may
622 * be a single node in the network we are
623 * looking for.
624 * The trouble is that we don't know the
625 * netmask for the remote end.
626 */
627 if (ifa->ifa_dstaddr != 0
628 && equal(addr, ifa->ifa_dstaddr))
629 return (ifa);
630 } else {
631 /*
632 * if we have a special address handler,
633 * then use it instead of the generic one.
634 */
635 if (ifa->ifa_claim_addr) {
636 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
637 return (ifa);
638 } else {
639 continue;
640 }
641 }
642
643 /*
644 * Scan all the bits in the ifa's address.
645 * If a bit dissagrees with what we are
646 * looking for, mask it with the netmask
647 * to see if it really matters.
648 * (A byte at a time)
649 */
650 if (ifa->ifa_netmask == 0)
651 continue;
652 cp = addr_data;
653 cp2 = ifa->ifa_addr->sa_data;
654 cp3 = ifa->ifa_netmask->sa_data;
655 cplim = ifa->ifa_netmask->sa_len
656 + (char *)ifa->ifa_netmask;
657 while (cp3 < cplim)
658 if ((*cp++ ^ *cp2++) & *cp3++)
659 goto next; /* next address! */
660 /*
661 * If the netmask of what we just found
662 * is more specific than what we had before
663 * (if we had one) then remember the new one
664 * before continuing to search
665 * for an even better one.
666 */
667 if (ifa_maybe == 0 ||
668 rn_refines((caddr_t)ifa->ifa_netmask,
669 (caddr_t)ifa_maybe->ifa_netmask))
670 ifa_maybe = ifa;
671 }
672 }
673 }
674 return (ifa_maybe);
675}
676
677/*
678 * Find an interface address specific to an interface best matching
679 * a given address.
680 */
681struct ifaddr *
682ifaof_ifpforaddr(addr, ifp)
683 struct sockaddr *addr;
82ed7fc2 684 struct ifnet *ifp;
984263bc 685{
82ed7fc2
RG
686 struct ifaddr *ifa;
687 char *cp, *cp2, *cp3;
688 char *cplim;
984263bc
MD
689 struct ifaddr *ifa_maybe = 0;
690 u_int af = addr->sa_family;
691
692 if (af >= AF_MAX)
693 return (0);
694 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
695 if (ifa->ifa_addr->sa_family != af)
696 continue;
697 if (ifa_maybe == 0)
698 ifa_maybe = ifa;
699 if (ifa->ifa_netmask == 0) {
700 if (equal(addr, ifa->ifa_addr) ||
701 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
702 return (ifa);
703 continue;
704 }
705 if (ifp->if_flags & IFF_POINTOPOINT) {
706 if (equal(addr, ifa->ifa_dstaddr))
707 return (ifa);
708 } else {
709 cp = addr->sa_data;
710 cp2 = ifa->ifa_addr->sa_data;
711 cp3 = ifa->ifa_netmask->sa_data;
712 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
713 for (; cp3 < cplim; cp3++)
714 if ((*cp++ ^ *cp2++) & *cp3)
715 break;
716 if (cp3 == cplim)
717 return (ifa);
718 }
719 }
720 return (ifa_maybe);
721}
722
723#include <net/route.h>
724
725/*
726 * Default action when installing a route with a Link Level gateway.
727 * Lookup an appropriate real ifa to point to.
728 * This should be moved to /sys/net/link.c eventually.
729 */
730static void
731link_rtrequest(cmd, rt, info)
732 int cmd;
82ed7fc2 733 struct rtentry *rt;
984263bc
MD
734 struct rt_addrinfo *info;
735{
82ed7fc2 736 struct ifaddr *ifa;
984263bc
MD
737 struct sockaddr *dst;
738 struct ifnet *ifp;
739
740 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
741 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
742 return;
743 ifa = ifaof_ifpforaddr(dst, ifp);
744 if (ifa) {
745 IFAFREE(rt->rt_ifa);
746 rt->rt_ifa = ifa;
747 ifa->ifa_refcnt++;
748 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
749 ifa->ifa_rtrequest(cmd, rt, info);
750 }
751}
752
753/*
754 * Mark an interface down and notify protocols of
755 * the transition.
756 * NOTE: must be called at splnet or eqivalent.
757 */
758void
759if_unroute(ifp, flag, fam)
82ed7fc2 760 struct ifnet *ifp;
984263bc
MD
761 int flag, fam;
762{
82ed7fc2 763 struct ifaddr *ifa;
984263bc
MD
764
765 ifp->if_flags &= ~flag;
766 getmicrotime(&ifp->if_lastchange);
767 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
768 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
769 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
770 if_qflush(&ifp->if_snd);
771 rt_ifmsg(ifp);
772}
773
774/*
775 * Mark an interface up and notify protocols of
776 * the transition.
777 * NOTE: must be called at splnet or eqivalent.
778 */
779void
780if_route(ifp, flag, fam)
82ed7fc2 781 struct ifnet *ifp;
984263bc
MD
782 int flag, fam;
783{
82ed7fc2 784 struct ifaddr *ifa;
984263bc
MD
785
786 ifp->if_flags |= flag;
787 getmicrotime(&ifp->if_lastchange);
788 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
789 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
790 pfctlinput(PRC_IFUP, ifa->ifa_addr);
791 rt_ifmsg(ifp);
792#ifdef INET6
793 in6_if_up(ifp);
794#endif
795}
796
797/*
798 * Mark an interface down and notify protocols of
799 * the transition.
800 * NOTE: must be called at splnet or eqivalent.
801 */
802void
803if_down(ifp)
82ed7fc2 804 struct ifnet *ifp;
984263bc
MD
805{
806
807 if_unroute(ifp, IFF_UP, AF_UNSPEC);
808}
809
810/*
811 * Mark an interface up and notify protocols of
812 * the transition.
813 * NOTE: must be called at splnet or eqivalent.
814 */
815void
816if_up(ifp)
82ed7fc2 817 struct ifnet *ifp;
984263bc
MD
818{
819
820 if_route(ifp, IFF_UP, AF_UNSPEC);
821}
822
823/*
824 * Flush an interface queue.
825 */
826static void
827if_qflush(ifq)
82ed7fc2 828 struct ifqueue *ifq;
984263bc 829{
82ed7fc2 830 struct mbuf *m, *n;
984263bc
MD
831
832 n = ifq->ifq_head;
833 while ((m = n) != 0) {
834 n = m->m_act;
835 m_freem(m);
836 }
837 ifq->ifq_head = 0;
838 ifq->ifq_tail = 0;
839 ifq->ifq_len = 0;
840}
841
842/*
843 * Handle interface watchdog timer routines. Called
844 * from softclock, we decrement timers (if set) and
845 * call the appropriate interface routine on expiration.
846 */
847static void
848if_slowtimo(arg)
849 void *arg;
850{
82ed7fc2 851 struct ifnet *ifp;
984263bc
MD
852 int s = splimp();
853
854 TAILQ_FOREACH(ifp, &ifnet, if_link) {
855 if (ifp->if_timer == 0 || --ifp->if_timer)
856 continue;
857 if (ifp->if_watchdog)
858 (*ifp->if_watchdog)(ifp);
859 }
860 splx(s);
861 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
862}
863
864/*
865 * Map interface name to
866 * interface structure pointer.
867 */
868struct ifnet *
869ifunit(const char *name)
870{
871 char namebuf[IFNAMSIZ + 1];
872 const char *cp;
873 struct ifnet *ifp;
874 int unit;
875 unsigned len, m;
876 char c;
877
878 len = strlen(name);
879 if (len < 2 || len > IFNAMSIZ)
880 return NULL;
881 cp = name + len - 1;
882 c = *cp;
883 if (c < '0' || c > '9')
884 return NULL; /* trailing garbage */
885 unit = 0;
886 m = 1;
887 do {
888 if (cp == name)
889 return NULL; /* no interface name */
890 unit += (c - '0') * m;
891 if (unit > 1000000)
892 return NULL; /* number is unreasonable */
893 m *= 10;
894 c = *--cp;
895 } while (c >= '0' && c <= '9');
896 len = cp - name + 1;
897 bcopy(name, namebuf, len);
898 namebuf[len] = '\0';
899 /*
900 * Now search all the interfaces for this name/number
901 */
902 TAILQ_FOREACH(ifp, &ifnet, if_link) {
903 if (strcmp(ifp->if_name, namebuf))
904 continue;
905 if (unit == ifp->if_unit)
906 break;
907 }
908 return (ifp);
909}
910
911
912/*
913 * Map interface name in a sockaddr_dl to
914 * interface structure pointer.
915 */
916struct ifnet *
917if_withname(sa)
918 struct sockaddr *sa;
919{
920 char ifname[IFNAMSIZ+1];
921 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
922
923 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
924 (sdl->sdl_nlen > IFNAMSIZ) )
925 return NULL;
926
927 /*
928 * ifunit wants a null-terminated name. It may not be null-terminated
929 * in the sockaddr. We don't want to change the caller's sockaddr,
930 * and there might not be room to put the trailing null anyway, so we
931 * make a local copy that we know we can null terminate safely.
932 */
933
934 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
935 ifname[sdl->sdl_nlen] = '\0';
936 return ifunit(ifname);
937}
938
939
940/*
941 * Interface ioctls.
942 */
943int
dadab5e9 944ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
984263bc 945{
41c20dac
MD
946 struct ifnet *ifp;
947 struct ifreq *ifr;
984263bc
MD
948 struct ifstat *ifs;
949 int error;
950 short oif_flags;
951 int new_flags;
952
953 switch (cmd) {
954
955 case SIOCGIFCONF:
956 case OSIOCGIFCONF:
957 return (ifconf(cmd, data));
958 }
959 ifr = (struct ifreq *)data;
960
961 switch (cmd) {
962 case SIOCIFCREATE:
963 case SIOCIFDESTROY:
dadab5e9 964 if ((error = suser(td)) != 0)
984263bc
MD
965 return (error);
966 return ((cmd == SIOCIFCREATE) ?
967 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
968 if_clone_destroy(ifr->ifr_name));
969
970 case SIOCIFGCLONERS:
971 return (if_clone_list((struct if_clonereq *)data));
972 }
973
974 ifp = ifunit(ifr->ifr_name);
975 if (ifp == 0)
976 return (ENXIO);
977 switch (cmd) {
978
979 case SIOCGIFFLAGS:
980 ifr->ifr_flags = ifp->if_flags;
981 ifr->ifr_flagshigh = ifp->if_ipending >> 16;
982 break;
983
984 case SIOCGIFCAP:
985 ifr->ifr_reqcap = ifp->if_capabilities;
986 ifr->ifr_curcap = ifp->if_capenable;
987 break;
988
989 case SIOCGIFMETRIC:
990 ifr->ifr_metric = ifp->if_metric;
991 break;
992
993 case SIOCGIFMTU:
994 ifr->ifr_mtu = ifp->if_mtu;
995 break;
996
997 case SIOCGIFPHYS:
998 ifr->ifr_phys = ifp->if_physical;
999 break;
1000
1001 case SIOCSIFFLAGS:
dadab5e9 1002 error = suser(td);
984263bc
MD
1003 if (error)
1004 return (error);
1005 new_flags = (ifr->ifr_flags & 0xffff) |
1006 (ifr->ifr_flagshigh << 16);
1007 if (ifp->if_flags & IFF_SMART) {
1008 /* Smart drivers twiddle their own routes */
1009 } else if (ifp->if_flags & IFF_UP &&
1010 (new_flags & IFF_UP) == 0) {
1011 int s = splimp();
1012 if_down(ifp);
1013 splx(s);
1014 } else if (new_flags & IFF_UP &&
1015 (ifp->if_flags & IFF_UP) == 0) {
1016 int s = splimp();
1017 if_up(ifp);
1018 splx(s);
1019 }
1020 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1021 (new_flags &~ IFF_CANTCHANGE);
1022 ifp->if_ipending = (ifp->if_ipending & IFF_CANTCHANGE) |
1023 (new_flags &~ IFF_CANTCHANGE);
1024 if (new_flags & IFF_PPROMISC) {
1025 /* Permanently promiscuous mode requested */
1026 ifp->if_flags |= IFF_PROMISC;
1027 } else if (ifp->if_pcount == 0) {
1028 ifp->if_flags &= ~IFF_PROMISC;
1029 }
1030 if (ifp->if_ioctl)
1031 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1032 getmicrotime(&ifp->if_lastchange);
1033 break;
1034
1035 case SIOCSIFCAP:
dadab5e9 1036 error = suser(td);
984263bc
MD
1037 if (error)
1038 return (error);
1039 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1040 return (EINVAL);
1041 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1042 break;
1043
1044 case SIOCSIFMETRIC:
dadab5e9 1045 error = suser(td);
984263bc
MD
1046 if (error)
1047 return (error);
1048 ifp->if_metric = ifr->ifr_metric;
1049 getmicrotime(&ifp->if_lastchange);
1050 break;
1051
1052 case SIOCSIFPHYS:
dadab5e9 1053 error = suser(td);
984263bc
MD
1054 if (error)
1055 return error;
1056 if (!ifp->if_ioctl)
1057 return EOPNOTSUPP;
1058 error = (*ifp->if_ioctl)(ifp, cmd, data);
1059 if (error == 0)
1060 getmicrotime(&ifp->if_lastchange);
1061 return(error);
1062
1063 case SIOCSIFMTU:
1064 {
1065 u_long oldmtu = ifp->if_mtu;
1066
dadab5e9 1067 error = suser(td);
984263bc
MD
1068 if (error)
1069 return (error);
1070 if (ifp->if_ioctl == NULL)
1071 return (EOPNOTSUPP);
1072 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1073 return (EINVAL);
1074 error = (*ifp->if_ioctl)(ifp, cmd, data);
1075 if (error == 0) {
1076 getmicrotime(&ifp->if_lastchange);
1077 rt_ifmsg(ifp);
1078 }
1079 /*
1080 * If the link MTU changed, do network layer specific procedure.
1081 */
1082 if (ifp->if_mtu != oldmtu) {
1083#ifdef INET6
1084 nd6_setmtu(ifp);
1085#endif
1086 }
1087 return (error);
1088 }
1089
1090 case SIOCADDMULTI:
1091 case SIOCDELMULTI:
dadab5e9 1092 error = suser(td);
984263bc
MD
1093 if (error)
1094 return (error);
1095
1096 /* Don't allow group membership on non-multicast interfaces. */
1097 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1098 return EOPNOTSUPP;
1099
1100 /* Don't let users screw up protocols' entries. */
1101 if (ifr->ifr_addr.sa_family != AF_LINK)
1102 return EINVAL;
1103
1104 if (cmd == SIOCADDMULTI) {
1105 struct ifmultiaddr *ifma;
1106 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1107 } else {
1108 error = if_delmulti(ifp, &ifr->ifr_addr);
1109 }
1110 if (error == 0)
1111 getmicrotime(&ifp->if_lastchange);
1112 return error;
1113
1114 case SIOCSIFPHYADDR:
1115 case SIOCDIFPHYADDR:
1116#ifdef INET6
1117 case SIOCSIFPHYADDR_IN6:
1118#endif
1119 case SIOCSLIFPHYADDR:
1120 case SIOCSIFMEDIA:
1121 case SIOCSIFGENERIC:
dadab5e9 1122 error = suser(td);
984263bc
MD
1123 if (error)
1124 return (error);
1125 if (ifp->if_ioctl == 0)
1126 return (EOPNOTSUPP);
1127 error = (*ifp->if_ioctl)(ifp, cmd, data);
1128 if (error == 0)
1129 getmicrotime(&ifp->if_lastchange);
1130 return error;
1131
1132 case SIOCGIFSTATUS:
1133 ifs = (struct ifstat *)data;
1134 ifs->ascii[0] = '\0';
1135
1136 case SIOCGIFPSRCADDR:
1137 case SIOCGIFPDSTADDR:
1138 case SIOCGLIFPHYADDR:
1139 case SIOCGIFMEDIA:
1140 case SIOCGIFGENERIC:
1141 if (ifp->if_ioctl == 0)
1142 return (EOPNOTSUPP);
1143 return ((*ifp->if_ioctl)(ifp, cmd, data));
1144
1145 case SIOCSIFLLADDR:
dadab5e9 1146 error = suser(td);
984263bc
MD
1147 if (error)
1148 return (error);
1149 return if_setlladdr(ifp,
1150 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1151
1152 default:
1153 oif_flags = ifp->if_flags;
1154 if (so->so_proto == 0)
1155 return (EOPNOTSUPP);
1156#ifndef COMPAT_43
1157 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1158 data,
1159 ifp, p));
1160#else
1161 {
1162 int ocmd = cmd;
1163
1164 switch (cmd) {
1165
1166 case SIOCSIFDSTADDR:
1167 case SIOCSIFADDR:
1168 case SIOCSIFBRDADDR:
1169 case SIOCSIFNETMASK:
1170#if BYTE_ORDER != BIG_ENDIAN
1171 if (ifr->ifr_addr.sa_family == 0 &&
1172 ifr->ifr_addr.sa_len < 16) {
1173 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1174 ifr->ifr_addr.sa_len = 16;
1175 }
1176#else
1177 if (ifr->ifr_addr.sa_len == 0)
1178 ifr->ifr_addr.sa_len = 16;
1179#endif
1180 break;
1181
1182 case OSIOCGIFADDR:
1183 cmd = SIOCGIFADDR;
1184 break;
1185
1186 case OSIOCGIFDSTADDR:
1187 cmd = SIOCGIFDSTADDR;
1188 break;
1189
1190 case OSIOCGIFBRDADDR:
1191 cmd = SIOCGIFBRDADDR;
1192 break;
1193
1194 case OSIOCGIFNETMASK:
1195 cmd = SIOCGIFNETMASK;
1196 }
dadab5e9
MD
1197 error = ((*so->so_proto->pr_usrreqs->pru_control)
1198 (so, cmd, data, ifp, td));
984263bc
MD
1199 switch (ocmd) {
1200
1201 case OSIOCGIFADDR:
1202 case OSIOCGIFDSTADDR:
1203 case OSIOCGIFBRDADDR:
1204 case OSIOCGIFNETMASK:
1205 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1206
1207 }
1208 }
1209#endif /* COMPAT_43 */
1210
1211 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1212#ifdef INET6
1213 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1214 if (ifp->if_flags & IFF_UP) {
1215 int s = splimp();
1216 in6_if_up(ifp);
1217 splx(s);
1218 }
1219#endif
1220 }
1221 return (error);
1222
1223 }
1224 return (0);
1225}
1226
1227/*
1228 * Set/clear promiscuous mode on interface ifp based on the truth value
1229 * of pswitch. The calls are reference counted so that only the first
1230 * "on" request actually has an effect, as does the final "off" request.
1231 * Results are undefined if the "off" and "on" requests are not matched.
1232 */
1233int
1234ifpromisc(ifp, pswitch)
1235 struct ifnet *ifp;
1236 int pswitch;
1237{
1238 struct ifreq ifr;
1239 int error;
1240 int oldflags;
1241
1242 oldflags = ifp->if_flags;
1243 if (ifp->if_ipending & IFF_PPROMISC) {
1244 /* Do nothing if device is in permanently promiscuous mode */
1245 ifp->if_pcount += pswitch ? 1 : -1;
1246 return (0);
1247 }
1248 if (pswitch) {
1249 /*
1250 * If the device is not configured up, we cannot put it in
1251 * promiscuous mode.
1252 */
1253 if ((ifp->if_flags & IFF_UP) == 0)
1254 return (ENETDOWN);
1255 if (ifp->if_pcount++ != 0)
1256 return (0);
1257 ifp->if_flags |= IFF_PROMISC;
1258 log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1259 ifp->if_name, ifp->if_unit);
1260 } else {
1261 if (--ifp->if_pcount > 0)
1262 return (0);
1263 ifp->if_flags &= ~IFF_PROMISC;
1264 log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1265 ifp->if_name, ifp->if_unit);
1266 }
1267 ifr.ifr_flags = ifp->if_flags;
1268 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1269 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1270 if (error == 0)
1271 rt_ifmsg(ifp);
1272 else
1273 ifp->if_flags = oldflags;
1274 return error;
1275}
1276
1277/*
1278 * Return interface configuration
1279 * of system. List may be used
1280 * in later ioctl's (above) to get
1281 * other information.
1282 */
1283/*ARGSUSED*/
1284static int
41c20dac 1285ifconf(u_long cmd, caddr_t data)
984263bc 1286{
41c20dac
MD
1287 struct ifconf *ifc = (struct ifconf *)data;
1288 struct ifnet *ifp;
1289 struct ifaddr *ifa;
984263bc
MD
1290 struct sockaddr *sa;
1291 struct ifreq ifr, *ifrp;
1292 int space = ifc->ifc_len, error = 0;
1293
1294 ifrp = ifc->ifc_req;
1295 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1296 char workbuf[64];
1297 int ifnlen, addrs;
1298
1299 if (space <= sizeof (ifr))
1300 break;
1301 ifnlen = snprintf(workbuf, sizeof(workbuf),
1302 "%s%d", ifp->if_name, ifp->if_unit);
1303 if(ifnlen + 1 > sizeof ifr.ifr_name) {
1304 error = ENAMETOOLONG;
1305 break;
1306 } else {
1307 strcpy(ifr.ifr_name, workbuf);
1308 }
1309
1310 addrs = 0;
1311 ifa = ifp->if_addrhead.tqh_first;
1312 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1313 if (space <= sizeof(ifr))
1314 break;
1315 sa = ifa->ifa_addr;
dadab5e9 1316 if (curproc->p_ucred->cr_prison && prison_if(curthread, sa))
984263bc
MD
1317 continue;
1318 addrs++;
1319#ifdef COMPAT_43
1320 if (cmd == OSIOCGIFCONF) {
1321 struct osockaddr *osa =
1322 (struct osockaddr *)&ifr.ifr_addr;
1323 ifr.ifr_addr = *sa;
1324 osa->sa_family = sa->sa_family;
1325 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1326 sizeof (ifr));
1327 ifrp++;
1328 } else
1329#endif
1330 if (sa->sa_len <= sizeof(*sa)) {
1331 ifr.ifr_addr = *sa;
1332 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1333 sizeof (ifr));
1334 ifrp++;
1335 } else {
1336 if (space < sizeof (ifr) + sa->sa_len -
1337 sizeof(*sa))
1338 break;
1339 space -= sa->sa_len - sizeof(*sa);
1340 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1341 sizeof (ifr.ifr_name));
1342 if (error == 0)
1343 error = copyout((caddr_t)sa,
1344 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1345 ifrp = (struct ifreq *)
1346 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1347 }
1348 if (error)
1349 break;
1350 space -= sizeof (ifr);
1351 }
1352 if (error)
1353 break;
1354 if (!addrs) {
1355 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1356 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1357 sizeof (ifr));
1358 if (error)
1359 break;
1360 space -= sizeof (ifr);
1361 ifrp++;
1362 }
1363 }
1364 ifc->ifc_len -= space;
1365 return (error);
1366}
1367
1368/*
1369 * Just like if_promisc(), but for all-multicast-reception mode.
1370 */
1371int
1372if_allmulti(ifp, onswitch)
1373 struct ifnet *ifp;
1374 int onswitch;
1375{
1376 int error = 0;
1377 int s = splimp();
1378 struct ifreq ifr;
1379
1380 if (onswitch) {
1381 if (ifp->if_amcount++ == 0) {
1382 ifp->if_flags |= IFF_ALLMULTI;
1383 ifr.ifr_flags = ifp->if_flags;
1384 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1385 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1386 }
1387 } else {
1388 if (ifp->if_amcount > 1) {
1389 ifp->if_amcount--;
1390 } else {
1391 ifp->if_amcount = 0;
1392 ifp->if_flags &= ~IFF_ALLMULTI;
1393 ifr.ifr_flags = ifp->if_flags;
1394 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1395 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1396 }
1397 }
1398 splx(s);
1399
1400 if (error == 0)
1401 rt_ifmsg(ifp);
1402 return error;
1403}
1404
1405/*
1406 * Add a multicast listenership to the interface in question.
1407 * The link layer provides a routine which converts
1408 */
1409int
1410if_addmulti(ifp, sa, retifma)
1411 struct ifnet *ifp; /* interface to manipulate */
1412 struct sockaddr *sa; /* address to add */
1413 struct ifmultiaddr **retifma;
1414{
1415 struct sockaddr *llsa, *dupsa;
1416 int error, s;
1417 struct ifmultiaddr *ifma;
1418
1419 /*
1420 * If the matching multicast address already exists
1421 * then don't add a new one, just add a reference
1422 */
1423 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1424 if (equal(sa, ifma->ifma_addr)) {
1425 ifma->ifma_refcount++;
1426 if (retifma)
1427 *retifma = ifma;
1428 return 0;
1429 }
1430 }
1431
1432 /*
1433 * Give the link layer a chance to accept/reject it, and also
1434 * find out which AF_LINK address this maps to, if it isn't one
1435 * already.
1436 */
1437 if (ifp->if_resolvemulti) {
1438 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1439 if (error) return error;
1440 } else {
1441 llsa = 0;
1442 }
1443
1444 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1445 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1446 bcopy(sa, dupsa, sa->sa_len);
1447
1448 ifma->ifma_addr = dupsa;
1449 ifma->ifma_lladdr = llsa;
1450 ifma->ifma_ifp = ifp;
1451 ifma->ifma_refcount = 1;
1452 ifma->ifma_protospec = 0;
1453 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1454
1455 /*
1456 * Some network interfaces can scan the address list at
1457 * interrupt time; lock them out.
1458 */
1459 s = splimp();
1460 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1461 splx(s);
1462 *retifma = ifma;
1463
1464 if (llsa != 0) {
1465 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1466 if (equal(ifma->ifma_addr, llsa))
1467 break;
1468 }
1469 if (ifma) {
1470 ifma->ifma_refcount++;
1471 } else {
1472 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1473 M_IFMADDR, M_WAITOK);
1474 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1475 M_IFMADDR, M_WAITOK);
1476 bcopy(llsa, dupsa, llsa->sa_len);
1477 ifma->ifma_addr = dupsa;
1478 ifma->ifma_ifp = ifp;
1479 ifma->ifma_refcount = 1;
1480 s = splimp();
1481 LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1482 splx(s);
1483 }
1484 }
1485 /*
1486 * We are certain we have added something, so call down to the
1487 * interface to let them know about it.
1488 */
1489 s = splimp();
1490 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1491 splx(s);
1492
1493 return 0;
1494}
1495
1496/*
1497 * Remove a reference to a multicast address on this interface. Yell
1498 * if the request does not match an existing membership.
1499 */
1500int
1501if_delmulti(ifp, sa)
1502 struct ifnet *ifp;
1503 struct sockaddr *sa;
1504{
1505 struct ifmultiaddr *ifma;
1506 int s;
1507
1508 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1509 if (equal(sa, ifma->ifma_addr))
1510 break;
1511 if (ifma == 0)
1512 return ENOENT;
1513
1514 if (ifma->ifma_refcount > 1) {
1515 ifma->ifma_refcount--;
1516 return 0;
1517 }
1518
1519 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1520 sa = ifma->ifma_lladdr;
1521 s = splimp();
1522 LIST_REMOVE(ifma, ifma_link);
1523 /*
1524 * Make sure the interface driver is notified
1525 * in the case of a link layer mcast group being left.
1526 */
1527 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1528 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1529 splx(s);
1530 free(ifma->ifma_addr, M_IFMADDR);
1531 free(ifma, M_IFMADDR);
1532 if (sa == 0)
1533 return 0;
1534
1535 /*
1536 * Now look for the link-layer address which corresponds to
1537 * this network address. It had been squirreled away in
1538 * ifma->ifma_lladdr for this purpose (so we don't have
1539 * to call ifp->if_resolvemulti() again), and we saved that
1540 * value in sa above. If some nasty deleted the
1541 * link-layer address out from underneath us, we can deal because
1542 * the address we stored was is not the same as the one which was
1543 * in the record for the link-layer address. (So we don't complain
1544 * in that case.)
1545 */
1546 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1547 if (equal(sa, ifma->ifma_addr))
1548 break;
1549 if (ifma == 0)
1550 return 0;
1551
1552 if (ifma->ifma_refcount > 1) {
1553 ifma->ifma_refcount--;
1554 return 0;
1555 }
1556
1557 s = splimp();
1558 LIST_REMOVE(ifma, ifma_link);
1559 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1560 splx(s);
1561 free(ifma->ifma_addr, M_IFMADDR);
1562 free(sa, M_IFMADDR);
1563 free(ifma, M_IFMADDR);
1564
1565 return 0;
1566}
1567
1568/*
1569 * Set the link layer address on an interface.
1570 *
1571 * At this time we only support certain types of interfaces,
1572 * and we don't allow the length of the address to change.
1573 */
1574int
1575if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1576{
1577 struct sockaddr_dl *sdl;
1578 struct ifaddr *ifa;
1579 struct ifreq ifr;
1580
1581 ifa = ifnet_addrs[ifp->if_index - 1];
1582 if (ifa == NULL)
1583 return (EINVAL);
1584 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1585 if (sdl == NULL)
1586 return (EINVAL);
1587 if (len != sdl->sdl_alen) /* don't allow length to change */
1588 return (EINVAL);
1589 switch (ifp->if_type) {
1590 case IFT_ETHER: /* these types use struct arpcom */
1591 case IFT_FDDI:
1592 case IFT_XETHER:
1593 case IFT_ISO88025:
1594 case IFT_L2VLAN:
1595 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1596 /* FALLTHROUGH */
1597 case IFT_ARCNET:
1598 bcopy(lladdr, LLADDR(sdl), len);
1599 break;
1600 default:
1601 return (ENODEV);
1602 }
1603 /*
1604 * If the interface is already up, we need
1605 * to re-init it in order to reprogram its
1606 * address filter.
1607 */
1608 if ((ifp->if_flags & IFF_UP) != 0) {
1609 ifp->if_flags &= ~IFF_UP;
1610 ifr.ifr_flags = ifp->if_flags;
1611 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1612 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1613 ifp->if_flags |= IFF_UP;
1614 ifr.ifr_flags = ifp->if_flags;
1615 ifr.ifr_flagshigh = ifp->if_ipending >> 16;
1616 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1617#ifdef INET
1618 /*
1619 * Also send gratuitous ARPs to notify other nodes about
1620 * the address change.
1621 */
1622 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1623 if (ifa->ifa_addr != NULL &&
1624 ifa->ifa_addr->sa_family == AF_INET)
1625 arp_ifinit(ifp, ifa);
1626 }
1627#endif
1628 }
1629 return (0);
1630}
1631
1632struct ifmultiaddr *
1633ifmaof_ifpforaddr(sa, ifp)
1634 struct sockaddr *sa;
1635 struct ifnet *ifp;
1636{
1637 struct ifmultiaddr *ifma;
1638
1639 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1640 if (equal(ifma->ifma_addr, sa))
1641 break;
1642
1643 return ifma;
1644}
1645
1646int
1647if_printf(struct ifnet *ifp, const char *fmt, ...)
1648{
1649 va_list ap;
1650 int retval;
1651
1652 retval = printf("%s%d: ", ifp->if_name, ifp->if_unit);
1653 va_start(ap, fmt);
1654 retval += vprintf(fmt, ap);
1655 va_end(ap);
1656 return (retval);
1657}
1658
1659SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1660SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");