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