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