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