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