Deleted Added
full compact
if.c (193166) if.c (193232)
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
30 * $FreeBSD: head/sys/net/if.c 193166 2009-05-31 12:10:04Z zec $
30 * $FreeBSD: head/sys/net/if.c 193232 2009-06-01 15:49:42Z bz $
31 */
32
33#include "opt_compat.h"
34#include "opt_inet6.h"
35#include "opt_inet.h"
36#include "opt_route.h"
37#include "opt_mac.h"
38#include "opt_carp.h"
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/conf.h>
43#include <sys/malloc.h>
44#include <sys/sbuf.h>
45#include <sys/bus.h>
46#include <sys/mbuf.h>
47#include <sys/systm.h>
48#include <sys/priv.h>
49#include <sys/proc.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/protosw.h>
53#include <sys/kernel.h>
54#include <sys/lock.h>
55#include <sys/refcount.h>
56#include <sys/module.h>
57#include <sys/rwlock.h>
58#include <sys/sockio.h>
59#include <sys/syslog.h>
60#include <sys/sysctl.h>
61#include <sys/taskqueue.h>
62#include <sys/domain.h>
63#include <sys/jail.h>
64#include <sys/vimage.h>
65#include <machine/stdarg.h>
66#include <vm/uma.h>
67
68#include <net/if.h>
69#include <net/if_arp.h>
70#include <net/if_clone.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
73#include <net/if_var.h>
74#include <net/radix.h>
75#include <net/route.h>
76#include <net/vnet.h>
77
78#if defined(INET) || defined(INET6)
79/*XXX*/
80#include <netinet/in.h>
81#include <netinet/in_var.h>
82#ifdef INET6
83#include <netinet6/in6_var.h>
84#include <netinet6/in6_ifattach.h>
85#endif
86#endif
87#ifdef INET
88#include <netinet/if_ether.h>
89#include <netinet/vinet.h>
90#endif
91#ifdef DEV_CARP
92#include <netinet/ip_carp.h>
93#endif
94
95#include <security/mac/mac_framework.h>
96
97#ifndef VIMAGE
98#ifndef VIMAGE_GLOBALS
99struct vnet_net vnet_net_0;
100#endif
101#endif
102
103static int slowtimo_started;
104
105SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
106SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
107
108/* Log link state change events */
109static int log_link_state_change = 1;
110
111SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
112 &log_link_state_change, 0,
113 "log interface link state change events");
114
115void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
116void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
117void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
118
119struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
120
121/*
122 * XXX: Style; these should be sorted alphabetically, and unprototyped
123 * static functions should be prototyped. Currently they are sorted by
124 * declaration order.
125 */
126static void if_attachdomain(void *);
127static void if_attachdomain1(struct ifnet *);
128static int ifconf(u_long, caddr_t);
129static void if_freemulti(struct ifmultiaddr *);
130static void if_init(void *);
131static void if_check(void *);
132static void if_route(struct ifnet *, int flag, int fam);
133static int if_setflag(struct ifnet *, int, int, int *, int);
134static void if_slowtimo(void *);
135static int if_transmit(struct ifnet *ifp, struct mbuf *m);
136static void if_unroute(struct ifnet *, int flag, int fam);
137static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
138static int if_rtdel(struct radix_node *, void *);
139static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
140static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
141static void do_link_state_change(void *, int);
142static int if_getgroup(struct ifgroupreq *, struct ifnet *);
143static int if_getgroupmembers(struct ifgroupreq *);
144static void if_delgroups(struct ifnet *);
145static void if_attach_internal(struct ifnet *, int);
146static void if_detach_internal(struct ifnet *, int);
147
148#ifdef INET6
149/*
150 * XXX: declare here to avoid to include many inet6 related files..
151 * should be more generalized?
152 */
153extern void nd6_setmtu(struct ifnet *);
154#endif
155
156static int vnet_net_iattach(const void *);
157
158#ifdef VIMAGE_GLOBALS
159struct ifnethead ifnet; /* depend on static init XXX */
160struct ifgrouphead ifg_head;
161int if_index;
162static int if_indexlim;
163/* Table of ifnet/cdev by index. Locked with ifnet_lock. */
164static struct ifindex_entry *ifindex_table;
165static struct knlist ifklist;
166#endif
167
168int ifqmaxlen = IFQ_MAXLEN;
169struct rwlock ifnet_lock;
170static if_com_alloc_t *if_com_alloc[256];
171static if_com_free_t *if_com_free[256];
172
173static void filt_netdetach(struct knote *kn);
174static int filt_netdev(struct knote *kn, long hint);
175
176static struct filterops netdev_filtops =
177 { 1, NULL, filt_netdetach, filt_netdev };
178
179#ifndef VIMAGE_GLOBALS
180static struct vnet_symmap vnet_net_symmap[] = {
181 VNET_SYMMAP(net, ifnet),
182 VNET_SYMMAP(net, rt_tables),
183 VNET_SYMMAP(net, rtstat),
184 VNET_SYMMAP(net, rttrash),
185 VNET_SYMMAP_END
186};
187
188static const vnet_modinfo_t vnet_net_modinfo = {
189 .vmi_id = VNET_MOD_NET,
190 .vmi_name = "net",
191 .vmi_size = sizeof(struct vnet_net),
192 .vmi_symmap = vnet_net_symmap,
193 .vmi_iattach = vnet_net_iattach
194};
195#endif /* !VIMAGE_GLOBALS */
196
197/*
198 * System initialization
199 */
200SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
201SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
202
203MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
204MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
205MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
206
207struct ifnet *
208ifnet_byindex_locked(u_short idx)
209{
210 INIT_VNET_NET(curvnet);
211
212 if (idx > V_if_index)
213 return (NULL);
214 return (V_ifindex_table[idx].ife_ifnet);
215}
216
217struct ifnet *
218ifnet_byindex(u_short idx)
219{
220 struct ifnet *ifp;
221
222 IFNET_RLOCK();
223 ifp = ifnet_byindex_locked(idx);
224 IFNET_RUNLOCK();
225 return (ifp);
226}
227
228struct ifnet *
229ifnet_byindex_ref(u_short idx)
230{
231 struct ifnet *ifp;
232
233 IFNET_RLOCK();
234 ifp = ifnet_byindex_locked(idx);
235 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
236 IFNET_RUNLOCK();
237 return (NULL);
238 }
239 if_ref(ifp);
240 IFNET_RUNLOCK();
241 return (ifp);
242}
243
244static void
245ifnet_setbyindex(u_short idx, struct ifnet *ifp)
246{
247 INIT_VNET_NET(curvnet);
248
249 IFNET_WLOCK_ASSERT();
250
251 V_ifindex_table[idx].ife_ifnet = ifp;
252}
253
254struct ifaddr *
255ifaddr_byindex(u_short idx)
256{
257 struct ifaddr *ifa;
258
259 IFNET_RLOCK();
260 ifa = ifnet_byindex_locked(idx)->if_addr;
261 IFNET_RUNLOCK();
262 return (ifa);
263}
264
265struct cdev *
266ifdev_byindex(u_short idx)
267{
268 INIT_VNET_NET(curvnet);
269 struct cdev *cdev;
270
271 IFNET_RLOCK();
272 cdev = V_ifindex_table[idx].ife_dev;
273 IFNET_RUNLOCK();
274 return (cdev);
275}
276
277static void
278ifdev_setbyindex(u_short idx, struct cdev *cdev)
279{
280 INIT_VNET_NET(curvnet);
281
282 IFNET_WLOCK();
283 V_ifindex_table[idx].ife_dev = cdev;
284 IFNET_WUNLOCK();
285}
286
287static d_open_t netopen;
288static d_close_t netclose;
289static d_ioctl_t netioctl;
290static d_kqfilter_t netkqfilter;
291
292static struct cdevsw net_cdevsw = {
293 .d_version = D_VERSION,
294 .d_flags = D_NEEDGIANT,
295 .d_open = netopen,
296 .d_close = netclose,
297 .d_ioctl = netioctl,
298 .d_name = "net",
299 .d_kqfilter = netkqfilter,
300};
301
302static int
303netopen(struct cdev *dev, int flag, int mode, struct thread *td)
304{
305 return (0);
306}
307
308static int
309netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
310{
311 return (0);
312}
313
314static int
315netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
316{
317 struct ifnet *ifp;
318 int error, idx;
319
320 /* only support interface specific ioctls */
321 if (IOCGROUP(cmd) != 'i')
322 return (EOPNOTSUPP);
323 idx = dev2unit(dev);
324 if (idx == 0) {
325 /*
326 * special network device, not interface.
327 */
328 if (cmd == SIOCGIFCONF)
329 return (ifconf(cmd, data)); /* XXX remove cmd */
330#ifdef __amd64__
331 if (cmd == SIOCGIFCONF32)
332 return (ifconf(cmd, data)); /* XXX remove cmd */
333#endif
334 return (EOPNOTSUPP);
335 }
336
337 ifp = ifnet_byindex(idx);
338 if (ifp == NULL)
339 return (ENXIO);
340
341 error = ifhwioctl(cmd, ifp, data, td);
342 if (error == ENOIOCTL)
343 error = EOPNOTSUPP;
344 return (error);
345}
346
347static int
348netkqfilter(struct cdev *dev, struct knote *kn)
349{
350 INIT_VNET_NET(curvnet);
351 struct knlist *klist;
352 struct ifnet *ifp;
353 int idx;
354
355 switch (kn->kn_filter) {
356 case EVFILT_NETDEV:
357 kn->kn_fop = &netdev_filtops;
358 break;
359 default:
360 return (EINVAL);
361 }
362
363 idx = dev2unit(dev);
364 if (idx == 0) {
365 klist = &V_ifklist;
366 } else {
367 ifp = ifnet_byindex(idx);
368 if (ifp == NULL)
369 return (1);
370 klist = &ifp->if_klist;
371 }
372
373 kn->kn_hook = (caddr_t)klist;
374
375 knlist_add(klist, kn, 0);
376
377 return (0);
378}
379
380static void
381filt_netdetach(struct knote *kn)
382{
383 struct knlist *klist = (struct knlist *)kn->kn_hook;
384
385 knlist_remove(klist, kn, 0);
386}
387
388static int
389filt_netdev(struct knote *kn, long hint)
390{
391 struct knlist *klist = (struct knlist *)kn->kn_hook;
392
393 /*
394 * Currently NOTE_EXIT is abused to indicate device detach.
395 */
396 if (hint == NOTE_EXIT) {
397 kn->kn_data = NOTE_LINKINV;
398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
399 knlist_remove_inevent(klist, kn);
400 return (1);
401 }
402 if (hint != 0)
403 kn->kn_data = hint; /* current status */
404 if (kn->kn_sfflags & hint)
405 kn->kn_fflags |= hint;
406 return (kn->kn_fflags != 0);
407}
408
409/*
410 * Network interface utility routines.
411 *
412 * Routines with ifa_ifwith* names take sockaddr *'s as
413 * parameters.
414 */
415
416/* ARGSUSED*/
417static void
418if_init(void *dummy __unused)
419{
420
421#ifndef VIMAGE_GLOBALS
422 vnet_mod_register(&vnet_net_modinfo);
423#else
424 vnet_net_iattach(NULL);
425#endif
426
427 IFNET_LOCK_INIT();
428 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL,
429 0600, "network"));
430 if_clone_init();
431}
432
433static int
434vnet_net_iattach(const void *unused __unused)
435{
436 INIT_VNET_NET(curvnet);
437
438 V_if_index = 0;
439 V_ifindex_table = NULL;
440 V_if_indexlim = 8;
441
442 TAILQ_INIT(&V_ifnet);
443 TAILQ_INIT(&V_ifg_head);
444 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL);
445 if_grow(); /* create initial table */
446
447 return (0);
448}
449
450void
451if_grow(void)
452{
453 INIT_VNET_NET(curvnet);
454 u_int n;
455 struct ifindex_entry *e;
456
457 V_if_indexlim <<= 1;
458 n = V_if_indexlim * sizeof(*e);
459 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
460 if (V_ifindex_table != NULL) {
461 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
462 free((caddr_t)V_ifindex_table, M_IFNET);
463 }
464 V_ifindex_table = e;
465}
466
467static void
468if_check(void *dummy __unused)
469{
470
471 /*
472 * If at least one interface added during boot uses
473 * if_watchdog then start the timer.
474 */
475 if (slowtimo_started)
476 if_slowtimo(0);
477}
478
479/*
480 * Allocate a struct ifnet and an index for an interface. A layer 2
481 * common structure will also be allocated if an allocation routine is
482 * registered for the passed type.
483 */
484struct ifnet *
485if_alloc(u_char type)
486{
487 INIT_VNET_NET(curvnet);
488 struct ifnet *ifp;
489
490 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
491
492 /*
493 * Try to find an empty slot below if_index. If we fail, take
494 * the next slot.
495 *
496 * XXX: should be locked!
497 */
498 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
499 if (ifnet_byindex(ifp->if_index) == NULL)
500 break;
501 }
502 /* Catch if_index overflow. */
503 if (ifp->if_index < 1) {
504 free(ifp, M_IFNET);
505 return (NULL);
506 }
507 if (ifp->if_index > V_if_index)
508 V_if_index = ifp->if_index;
509 if (V_if_index >= V_if_indexlim)
510 if_grow();
511
512 ifp->if_type = type;
513 ifp->if_alloctype = type;
514
515 if (if_com_alloc[type] != NULL) {
516 ifp->if_l2com = if_com_alloc[type](type, ifp);
517 if (ifp->if_l2com == NULL) {
518 free(ifp, M_IFNET);
519 return (NULL);
520 }
521 }
522
523 IF_ADDR_LOCK_INIT(ifp);
524 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
525 ifp->if_afdata_initialized = 0;
526 IF_AFDATA_LOCK_INIT(ifp);
527 TAILQ_INIT(&ifp->if_addrhead);
528 TAILQ_INIT(&ifp->if_prefixhead);
529 TAILQ_INIT(&ifp->if_multiaddrs);
530 TAILQ_INIT(&ifp->if_groups);
531 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
532#ifdef MAC
533 mac_ifnet_init(ifp);
534#endif
535
536 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
537 IFNET_WLOCK();
538 ifnet_setbyindex(ifp->if_index, ifp);
539 IFNET_WUNLOCK();
540 return (ifp);
541}
542
543/*
544 * Do the actual work of freeing a struct ifnet, associated index, and layer
545 * 2 common structure. This call is made when the last reference to an
546 * interface is released.
547 */
548static void
549if_free_internal(struct ifnet *ifp)
550{
551 INIT_VNET_NET(curvnet); /* ifp->if_vnet is already NULL here */
552
553 KASSERT((ifp->if_flags & IFF_DYING),
554 ("if_free_internal: interface not dying"));
555
556 IFNET_WLOCK();
557 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
558 ("%s: freeing unallocated ifnet", ifp->if_xname));
559
560 ifnet_setbyindex(ifp->if_index, NULL);
561 while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL)
562 V_if_index--;
563 IFNET_WUNLOCK();
564
565 if (if_com_free[ifp->if_alloctype] != NULL)
566 if_com_free[ifp->if_alloctype](ifp->if_l2com,
567 ifp->if_alloctype);
568
569#ifdef MAC
570 mac_ifnet_destroy(ifp);
571#endif /* MAC */
572 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
573 knlist_clear(&ifp->if_klist, 0);
574 knlist_destroy(&ifp->if_klist);
575 IF_AFDATA_DESTROY(ifp);
576 IF_ADDR_LOCK_DESTROY(ifp);
577 free(ifp, M_IFNET);
578}
579
580/*
581 * This version should only be called by intefaces that switch their type
582 * after calling if_alloc(). if_free_type() will go away again now that we
583 * have if_alloctype to cache the original allocation type. For now, assert
584 * that they match, since we require that in practice.
585 */
586void
587if_free_type(struct ifnet *ifp, u_char type)
588{
589
590 KASSERT(ifp->if_alloctype == type,
591 ("if_free_type: type (%d) != alloctype (%d)", type,
592 ifp->if_alloctype));
593
594 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
595 if (!refcount_release(&ifp->if_refcount))
596 return;
597 if_free_internal(ifp);
598}
599
600/*
601 * This is the normal version of if_free(), used by device drivers to free a
602 * detached network interface. The contents of if_free_type() will move into
603 * here when if_free_type() goes away.
604 */
605void
606if_free(struct ifnet *ifp)
607{
608
609 if_free_type(ifp, ifp->if_alloctype);
610}
611
612/*
613 * Interfaces to keep an ifnet type-stable despite the possibility of the
614 * driver calling if_free(). If there are additional references, we defer
615 * freeing the underlying data structure.
616 */
617void
618if_ref(struct ifnet *ifp)
619{
620
621 /* We don't assert the ifnet list lock here, but arguably should. */
622 refcount_acquire(&ifp->if_refcount);
623}
624
625void
626if_rele(struct ifnet *ifp)
627{
628
629 if (!refcount_release(&ifp->if_refcount))
630 return;
631 if_free_internal(ifp);
632}
633
634void
635ifq_attach(struct ifaltq *ifq, struct ifnet *ifp)
636{
637
638 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
639
640 if (ifq->ifq_maxlen == 0)
641 ifq->ifq_maxlen = ifqmaxlen;
642
643 ifq->altq_type = 0;
644 ifq->altq_disc = NULL;
645 ifq->altq_flags &= ALTQF_CANTCHANGE;
646 ifq->altq_tbr = NULL;
647 ifq->altq_ifp = ifp;
648}
649
650void
651ifq_detach(struct ifaltq *ifq)
652{
653 mtx_destroy(&ifq->ifq_mtx);
654}
655
656/*
657 * Perform generic interface initalization tasks and attach the interface
658 * to the list of "active" interfaces. If vmove flag is set on entry
659 * to if_attach_internal(), perform only a limited subset of initialization
660 * tasks, given that we are moving from one vnet to another an ifnet which
661 * has already been fully initialized.
662 *
663 * XXX:
664 * - The decision to return void and thus require this function to
665 * succeed is questionable.
666 * - We should probably do more sanity checking. For instance we don't
667 * do anything to insure if_xname is unique or non-empty.
668 */
669void
670if_attach(struct ifnet *ifp)
671{
672
673 if_attach_internal(ifp, 0);
674}
675
676static void
677if_attach_internal(struct ifnet *ifp, int vmove)
678{
679 INIT_VNET_NET(curvnet);
680 unsigned socksize, ifasize;
681 int namelen, masklen;
682 struct sockaddr_dl *sdl;
683 struct ifaddr *ifa;
684
685 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
686 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
687 ifp->if_xname);
688
689#ifdef VIMAGE
690 ifp->if_vnet = curvnet;
691#endif
692
693 if_addgroup(ifp, IFG_ALL);
694
695 getmicrotime(&ifp->if_lastchange);
696 ifp->if_data.ifi_epoch = time_uptime;
697 ifp->if_data.ifi_datalen = sizeof(struct if_data);
698
699 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
700 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
701 ("transmit and qflush must both either be set or both be NULL"));
702 if (ifp->if_transmit == NULL) {
703 ifp->if_transmit = if_transmit;
704 ifp->if_qflush = if_qflush;
705 }
706
707 if (!vmove) {
708#ifdef MAC
709 mac_ifnet_create(ifp);
710#endif
711
712 if (IS_DEFAULT_VNET(curvnet)) {
713 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
714 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s",
715 net_cdevsw.d_name, ifp->if_xname));
716 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
717 net_cdevsw.d_name, ifp->if_index);
718 }
719
720 ifq_attach(&ifp->if_snd, ifp);
721
722 /*
723 * Create a Link Level name for this device.
724 */
725 namelen = strlen(ifp->if_xname);
726 /*
727 * Always save enough space for any possiable name so we
728 * can do a rename in place later.
729 */
730 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
731 socksize = masklen + ifp->if_addrlen;
732 if (socksize < sizeof(*sdl))
733 socksize = sizeof(*sdl);
734 socksize = roundup2(socksize, sizeof(long));
735 ifasize = sizeof(*ifa) + 2 * socksize;
736 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
737 IFA_LOCK_INIT(ifa);
738 sdl = (struct sockaddr_dl *)(ifa + 1);
739 sdl->sdl_len = socksize;
740 sdl->sdl_family = AF_LINK;
741 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
742 sdl->sdl_nlen = namelen;
743 sdl->sdl_index = ifp->if_index;
744 sdl->sdl_type = ifp->if_type;
745 ifp->if_addr = ifa;
746 ifa->ifa_ifp = ifp;
747 ifa->ifa_rtrequest = link_rtrequest;
748 ifa->ifa_addr = (struct sockaddr *)sdl;
749 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
750 ifa->ifa_netmask = (struct sockaddr *)sdl;
751 sdl->sdl_len = masklen;
752 while (namelen != 0)
753 sdl->sdl_data[--namelen] = 0xff;
754 ifa->ifa_refcnt = 1;
755 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
756 /* Reliably crash if used uninitialized. */
757 ifp->if_broadcastaddr = NULL;
758 }
759
760 IFNET_WLOCK();
761 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
762#ifdef VIMAGE
763 curvnet->ifcnt++;
764#endif
765 IFNET_WUNLOCK();
766
767 if (domain_init_status >= 2)
768 if_attachdomain1(ifp);
769
770 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
771 if (IS_DEFAULT_VNET(curvnet))
772 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
773
774 /* Announce the interface. */
775 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
776
777 if (!vmove && ifp->if_watchdog != NULL) {
778 if_printf(ifp,
779 "WARNING: using obsoleted if_watchdog interface\n");
780
781 /*
782 * Note that we need if_slowtimo(). If this happens after
783 * boot, then call if_slowtimo() directly.
784 */
785 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
786 if_slowtimo(0);
787 }
788}
789
790static void
791if_attachdomain(void *dummy)
792{
793 INIT_VNET_NET(curvnet);
794 struct ifnet *ifp;
795 int s;
796
797 s = splnet();
798 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
799 if_attachdomain1(ifp);
800 splx(s);
801}
802SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
803 if_attachdomain, NULL);
804
805static void
806if_attachdomain1(struct ifnet *ifp)
807{
808 struct domain *dp;
809 int s;
810
811 s = splnet();
812
813 /*
814 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
815 * cannot lock ifp->if_afdata initialization, entirely.
816 */
817 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
818 splx(s);
819 return;
820 }
821 if (ifp->if_afdata_initialized >= domain_init_status) {
822 IF_AFDATA_UNLOCK(ifp);
823 splx(s);
824 printf("if_attachdomain called more than once on %s\n",
825 ifp->if_xname);
826 return;
827 }
828 ifp->if_afdata_initialized = domain_init_status;
829 IF_AFDATA_UNLOCK(ifp);
830
831 /* address family dependent data region */
832 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
833 for (dp = domains; dp; dp = dp->dom_next) {
834 if (dp->dom_ifattach)
835 ifp->if_afdata[dp->dom_family] =
836 (*dp->dom_ifattach)(ifp);
837 }
838
839 splx(s);
840}
841
842/*
843 * Remove any unicast or broadcast network addresses from an interface.
844 */
845void
846if_purgeaddrs(struct ifnet *ifp)
847{
848 struct ifaddr *ifa, *next;
849
850 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
851 if (ifa->ifa_addr->sa_family == AF_LINK)
852 continue;
853#ifdef INET
854 /* XXX: Ugly!! ad hoc just for INET */
855 if (ifa->ifa_addr->sa_family == AF_INET) {
856 struct ifaliasreq ifr;
857
858 bzero(&ifr, sizeof(ifr));
859 ifr.ifra_addr = *ifa->ifa_addr;
860 if (ifa->ifa_dstaddr)
861 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
862 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
863 NULL) == 0)
864 continue;
865 }
866#endif /* INET */
867#ifdef INET6
868 if (ifa->ifa_addr->sa_family == AF_INET6) {
869 in6_purgeaddr(ifa);
870 /* ifp_addrhead is already updated */
871 continue;
872 }
873#endif /* INET6 */
874 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
875 IFAFREE(ifa);
876 }
877}
878
879/*
880 * Remove any multicast network addresses from an interface.
881 */
882void
883if_purgemaddrs(struct ifnet *ifp)
884{
885 struct ifmultiaddr *ifma;
886 struct ifmultiaddr *next;
887
888 IF_ADDR_LOCK(ifp);
889 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
890 if_delmulti_locked(ifp, ifma, 1);
891 IF_ADDR_UNLOCK(ifp);
892}
893
894/*
895 * Detach an interface, removing it from the list of "active" interfaces.
896 * If vmove flag is set on entry to if_detach_internal(), perform only a
897 * limited subset of cleanup tasks, given that we are moving an ifnet from
898 * one vnet to another, where it must be fully operational.
899 *
900 * XXXRW: There are some significant questions about event ordering, and
901 * how to prevent things from starting to use the interface during detach.
902 */
903void
904if_detach(struct ifnet *ifp)
905{
906
907 if_detach_internal(ifp, 0);
908}
909
910static void
911if_detach_internal(struct ifnet *ifp, int vmove)
912{
913 INIT_VNET_NET(ifp->if_vnet);
914 struct ifaddr *ifa;
915 struct radix_node_head *rnh;
916 int i, j;
917 struct domain *dp;
918 struct ifnet *iter;
919 int found = 0;
920
921 IFNET_WLOCK();
922 TAILQ_FOREACH(iter, &V_ifnet, if_link)
923 if (iter == ifp) {
924 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
925 found = 1;
926 break;
927 }
928#ifdef VIMAGE
929 if (found)
930 curvnet->ifcnt--;
931#endif
932 IFNET_WUNLOCK();
933 if (!found) {
934 if (vmove)
935 panic("interface not in it's own ifnet list");
936 else
937 return; /* XXX this should panic as well? */
938 }
939
940 /*
941 * Remove/wait for pending events.
942 */
943 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
944
945 /*
946 * Remove routes and flush queues.
947 */
948 if_down(ifp);
949#ifdef ALTQ
950 if (ALTQ_IS_ENABLED(&ifp->if_snd))
951 altq_disable(&ifp->if_snd);
952 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
953 altq_detach(&ifp->if_snd);
954#endif
955
956 if_purgeaddrs(ifp);
957
958#ifdef INET
959 in_ifdetach(ifp);
960#endif
961
962#ifdef INET6
963 /*
964 * Remove all IPv6 kernel structs related to ifp. This should be done
965 * before removing routing entries below, since IPv6 interface direct
966 * routes are expected to be removed by the IPv6-specific kernel API.
967 * Otherwise, the kernel will detect some inconsistency and bark it.
968 */
969 in6_ifdetach(ifp);
970#endif
971 if_purgemaddrs(ifp);
972
973 if (!vmove) {
974 /*
975 * Prevent further calls into the device driver via ifnet.
976 */
977 if_dead(ifp);
978
979 /*
980 * Remove link ifaddr pointer and maybe decrement if_index.
981 * Clean up all addresses.
982 */
983 ifp->if_addr = NULL;
984 if (IS_DEFAULT_VNET(curvnet))
985 destroy_dev(ifdev_byindex(ifp->if_index));
986 ifdev_setbyindex(ifp->if_index, NULL);
987
988 /* We can now free link ifaddr. */
989 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
990 ifa = TAILQ_FIRST(&ifp->if_addrhead);
991 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
992 IFAFREE(ifa);
993 }
994 }
995
996 /*
997 * Delete all remaining routes using this interface
998 * Unfortuneatly the only way to do this is to slog through
999 * the entire routing table looking for routes which point
1000 * to this interface...oh well...
1001 */
1002 for (i = 1; i <= AF_MAX; i++) {
1003 for (j = 0; j < rt_numfibs; j++) {
31 */
32
33#include "opt_compat.h"
34#include "opt_inet6.h"
35#include "opt_inet.h"
36#include "opt_route.h"
37#include "opt_mac.h"
38#include "opt_carp.h"
39
40#include <sys/param.h>
41#include <sys/types.h>
42#include <sys/conf.h>
43#include <sys/malloc.h>
44#include <sys/sbuf.h>
45#include <sys/bus.h>
46#include <sys/mbuf.h>
47#include <sys/systm.h>
48#include <sys/priv.h>
49#include <sys/proc.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/protosw.h>
53#include <sys/kernel.h>
54#include <sys/lock.h>
55#include <sys/refcount.h>
56#include <sys/module.h>
57#include <sys/rwlock.h>
58#include <sys/sockio.h>
59#include <sys/syslog.h>
60#include <sys/sysctl.h>
61#include <sys/taskqueue.h>
62#include <sys/domain.h>
63#include <sys/jail.h>
64#include <sys/vimage.h>
65#include <machine/stdarg.h>
66#include <vm/uma.h>
67
68#include <net/if.h>
69#include <net/if_arp.h>
70#include <net/if_clone.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
73#include <net/if_var.h>
74#include <net/radix.h>
75#include <net/route.h>
76#include <net/vnet.h>
77
78#if defined(INET) || defined(INET6)
79/*XXX*/
80#include <netinet/in.h>
81#include <netinet/in_var.h>
82#ifdef INET6
83#include <netinet6/in6_var.h>
84#include <netinet6/in6_ifattach.h>
85#endif
86#endif
87#ifdef INET
88#include <netinet/if_ether.h>
89#include <netinet/vinet.h>
90#endif
91#ifdef DEV_CARP
92#include <netinet/ip_carp.h>
93#endif
94
95#include <security/mac/mac_framework.h>
96
97#ifndef VIMAGE
98#ifndef VIMAGE_GLOBALS
99struct vnet_net vnet_net_0;
100#endif
101#endif
102
103static int slowtimo_started;
104
105SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
106SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
107
108/* Log link state change events */
109static int log_link_state_change = 1;
110
111SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
112 &log_link_state_change, 0,
113 "log interface link state change events");
114
115void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
116void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
117void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
118
119struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
120
121/*
122 * XXX: Style; these should be sorted alphabetically, and unprototyped
123 * static functions should be prototyped. Currently they are sorted by
124 * declaration order.
125 */
126static void if_attachdomain(void *);
127static void if_attachdomain1(struct ifnet *);
128static int ifconf(u_long, caddr_t);
129static void if_freemulti(struct ifmultiaddr *);
130static void if_init(void *);
131static void if_check(void *);
132static void if_route(struct ifnet *, int flag, int fam);
133static int if_setflag(struct ifnet *, int, int, int *, int);
134static void if_slowtimo(void *);
135static int if_transmit(struct ifnet *ifp, struct mbuf *m);
136static void if_unroute(struct ifnet *, int flag, int fam);
137static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
138static int if_rtdel(struct radix_node *, void *);
139static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
140static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
141static void do_link_state_change(void *, int);
142static int if_getgroup(struct ifgroupreq *, struct ifnet *);
143static int if_getgroupmembers(struct ifgroupreq *);
144static void if_delgroups(struct ifnet *);
145static void if_attach_internal(struct ifnet *, int);
146static void if_detach_internal(struct ifnet *, int);
147
148#ifdef INET6
149/*
150 * XXX: declare here to avoid to include many inet6 related files..
151 * should be more generalized?
152 */
153extern void nd6_setmtu(struct ifnet *);
154#endif
155
156static int vnet_net_iattach(const void *);
157
158#ifdef VIMAGE_GLOBALS
159struct ifnethead ifnet; /* depend on static init XXX */
160struct ifgrouphead ifg_head;
161int if_index;
162static int if_indexlim;
163/* Table of ifnet/cdev by index. Locked with ifnet_lock. */
164static struct ifindex_entry *ifindex_table;
165static struct knlist ifklist;
166#endif
167
168int ifqmaxlen = IFQ_MAXLEN;
169struct rwlock ifnet_lock;
170static if_com_alloc_t *if_com_alloc[256];
171static if_com_free_t *if_com_free[256];
172
173static void filt_netdetach(struct knote *kn);
174static int filt_netdev(struct knote *kn, long hint);
175
176static struct filterops netdev_filtops =
177 { 1, NULL, filt_netdetach, filt_netdev };
178
179#ifndef VIMAGE_GLOBALS
180static struct vnet_symmap vnet_net_symmap[] = {
181 VNET_SYMMAP(net, ifnet),
182 VNET_SYMMAP(net, rt_tables),
183 VNET_SYMMAP(net, rtstat),
184 VNET_SYMMAP(net, rttrash),
185 VNET_SYMMAP_END
186};
187
188static const vnet_modinfo_t vnet_net_modinfo = {
189 .vmi_id = VNET_MOD_NET,
190 .vmi_name = "net",
191 .vmi_size = sizeof(struct vnet_net),
192 .vmi_symmap = vnet_net_symmap,
193 .vmi_iattach = vnet_net_iattach
194};
195#endif /* !VIMAGE_GLOBALS */
196
197/*
198 * System initialization
199 */
200SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
201SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
202
203MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
204MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
205MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
206
207struct ifnet *
208ifnet_byindex_locked(u_short idx)
209{
210 INIT_VNET_NET(curvnet);
211
212 if (idx > V_if_index)
213 return (NULL);
214 return (V_ifindex_table[idx].ife_ifnet);
215}
216
217struct ifnet *
218ifnet_byindex(u_short idx)
219{
220 struct ifnet *ifp;
221
222 IFNET_RLOCK();
223 ifp = ifnet_byindex_locked(idx);
224 IFNET_RUNLOCK();
225 return (ifp);
226}
227
228struct ifnet *
229ifnet_byindex_ref(u_short idx)
230{
231 struct ifnet *ifp;
232
233 IFNET_RLOCK();
234 ifp = ifnet_byindex_locked(idx);
235 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
236 IFNET_RUNLOCK();
237 return (NULL);
238 }
239 if_ref(ifp);
240 IFNET_RUNLOCK();
241 return (ifp);
242}
243
244static void
245ifnet_setbyindex(u_short idx, struct ifnet *ifp)
246{
247 INIT_VNET_NET(curvnet);
248
249 IFNET_WLOCK_ASSERT();
250
251 V_ifindex_table[idx].ife_ifnet = ifp;
252}
253
254struct ifaddr *
255ifaddr_byindex(u_short idx)
256{
257 struct ifaddr *ifa;
258
259 IFNET_RLOCK();
260 ifa = ifnet_byindex_locked(idx)->if_addr;
261 IFNET_RUNLOCK();
262 return (ifa);
263}
264
265struct cdev *
266ifdev_byindex(u_short idx)
267{
268 INIT_VNET_NET(curvnet);
269 struct cdev *cdev;
270
271 IFNET_RLOCK();
272 cdev = V_ifindex_table[idx].ife_dev;
273 IFNET_RUNLOCK();
274 return (cdev);
275}
276
277static void
278ifdev_setbyindex(u_short idx, struct cdev *cdev)
279{
280 INIT_VNET_NET(curvnet);
281
282 IFNET_WLOCK();
283 V_ifindex_table[idx].ife_dev = cdev;
284 IFNET_WUNLOCK();
285}
286
287static d_open_t netopen;
288static d_close_t netclose;
289static d_ioctl_t netioctl;
290static d_kqfilter_t netkqfilter;
291
292static struct cdevsw net_cdevsw = {
293 .d_version = D_VERSION,
294 .d_flags = D_NEEDGIANT,
295 .d_open = netopen,
296 .d_close = netclose,
297 .d_ioctl = netioctl,
298 .d_name = "net",
299 .d_kqfilter = netkqfilter,
300};
301
302static int
303netopen(struct cdev *dev, int flag, int mode, struct thread *td)
304{
305 return (0);
306}
307
308static int
309netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
310{
311 return (0);
312}
313
314static int
315netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
316{
317 struct ifnet *ifp;
318 int error, idx;
319
320 /* only support interface specific ioctls */
321 if (IOCGROUP(cmd) != 'i')
322 return (EOPNOTSUPP);
323 idx = dev2unit(dev);
324 if (idx == 0) {
325 /*
326 * special network device, not interface.
327 */
328 if (cmd == SIOCGIFCONF)
329 return (ifconf(cmd, data)); /* XXX remove cmd */
330#ifdef __amd64__
331 if (cmd == SIOCGIFCONF32)
332 return (ifconf(cmd, data)); /* XXX remove cmd */
333#endif
334 return (EOPNOTSUPP);
335 }
336
337 ifp = ifnet_byindex(idx);
338 if (ifp == NULL)
339 return (ENXIO);
340
341 error = ifhwioctl(cmd, ifp, data, td);
342 if (error == ENOIOCTL)
343 error = EOPNOTSUPP;
344 return (error);
345}
346
347static int
348netkqfilter(struct cdev *dev, struct knote *kn)
349{
350 INIT_VNET_NET(curvnet);
351 struct knlist *klist;
352 struct ifnet *ifp;
353 int idx;
354
355 switch (kn->kn_filter) {
356 case EVFILT_NETDEV:
357 kn->kn_fop = &netdev_filtops;
358 break;
359 default:
360 return (EINVAL);
361 }
362
363 idx = dev2unit(dev);
364 if (idx == 0) {
365 klist = &V_ifklist;
366 } else {
367 ifp = ifnet_byindex(idx);
368 if (ifp == NULL)
369 return (1);
370 klist = &ifp->if_klist;
371 }
372
373 kn->kn_hook = (caddr_t)klist;
374
375 knlist_add(klist, kn, 0);
376
377 return (0);
378}
379
380static void
381filt_netdetach(struct knote *kn)
382{
383 struct knlist *klist = (struct knlist *)kn->kn_hook;
384
385 knlist_remove(klist, kn, 0);
386}
387
388static int
389filt_netdev(struct knote *kn, long hint)
390{
391 struct knlist *klist = (struct knlist *)kn->kn_hook;
392
393 /*
394 * Currently NOTE_EXIT is abused to indicate device detach.
395 */
396 if (hint == NOTE_EXIT) {
397 kn->kn_data = NOTE_LINKINV;
398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
399 knlist_remove_inevent(klist, kn);
400 return (1);
401 }
402 if (hint != 0)
403 kn->kn_data = hint; /* current status */
404 if (kn->kn_sfflags & hint)
405 kn->kn_fflags |= hint;
406 return (kn->kn_fflags != 0);
407}
408
409/*
410 * Network interface utility routines.
411 *
412 * Routines with ifa_ifwith* names take sockaddr *'s as
413 * parameters.
414 */
415
416/* ARGSUSED*/
417static void
418if_init(void *dummy __unused)
419{
420
421#ifndef VIMAGE_GLOBALS
422 vnet_mod_register(&vnet_net_modinfo);
423#else
424 vnet_net_iattach(NULL);
425#endif
426
427 IFNET_LOCK_INIT();
428 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL,
429 0600, "network"));
430 if_clone_init();
431}
432
433static int
434vnet_net_iattach(const void *unused __unused)
435{
436 INIT_VNET_NET(curvnet);
437
438 V_if_index = 0;
439 V_ifindex_table = NULL;
440 V_if_indexlim = 8;
441
442 TAILQ_INIT(&V_ifnet);
443 TAILQ_INIT(&V_ifg_head);
444 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL);
445 if_grow(); /* create initial table */
446
447 return (0);
448}
449
450void
451if_grow(void)
452{
453 INIT_VNET_NET(curvnet);
454 u_int n;
455 struct ifindex_entry *e;
456
457 V_if_indexlim <<= 1;
458 n = V_if_indexlim * sizeof(*e);
459 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
460 if (V_ifindex_table != NULL) {
461 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
462 free((caddr_t)V_ifindex_table, M_IFNET);
463 }
464 V_ifindex_table = e;
465}
466
467static void
468if_check(void *dummy __unused)
469{
470
471 /*
472 * If at least one interface added during boot uses
473 * if_watchdog then start the timer.
474 */
475 if (slowtimo_started)
476 if_slowtimo(0);
477}
478
479/*
480 * Allocate a struct ifnet and an index for an interface. A layer 2
481 * common structure will also be allocated if an allocation routine is
482 * registered for the passed type.
483 */
484struct ifnet *
485if_alloc(u_char type)
486{
487 INIT_VNET_NET(curvnet);
488 struct ifnet *ifp;
489
490 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
491
492 /*
493 * Try to find an empty slot below if_index. If we fail, take
494 * the next slot.
495 *
496 * XXX: should be locked!
497 */
498 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
499 if (ifnet_byindex(ifp->if_index) == NULL)
500 break;
501 }
502 /* Catch if_index overflow. */
503 if (ifp->if_index < 1) {
504 free(ifp, M_IFNET);
505 return (NULL);
506 }
507 if (ifp->if_index > V_if_index)
508 V_if_index = ifp->if_index;
509 if (V_if_index >= V_if_indexlim)
510 if_grow();
511
512 ifp->if_type = type;
513 ifp->if_alloctype = type;
514
515 if (if_com_alloc[type] != NULL) {
516 ifp->if_l2com = if_com_alloc[type](type, ifp);
517 if (ifp->if_l2com == NULL) {
518 free(ifp, M_IFNET);
519 return (NULL);
520 }
521 }
522
523 IF_ADDR_LOCK_INIT(ifp);
524 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
525 ifp->if_afdata_initialized = 0;
526 IF_AFDATA_LOCK_INIT(ifp);
527 TAILQ_INIT(&ifp->if_addrhead);
528 TAILQ_INIT(&ifp->if_prefixhead);
529 TAILQ_INIT(&ifp->if_multiaddrs);
530 TAILQ_INIT(&ifp->if_groups);
531 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
532#ifdef MAC
533 mac_ifnet_init(ifp);
534#endif
535
536 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
537 IFNET_WLOCK();
538 ifnet_setbyindex(ifp->if_index, ifp);
539 IFNET_WUNLOCK();
540 return (ifp);
541}
542
543/*
544 * Do the actual work of freeing a struct ifnet, associated index, and layer
545 * 2 common structure. This call is made when the last reference to an
546 * interface is released.
547 */
548static void
549if_free_internal(struct ifnet *ifp)
550{
551 INIT_VNET_NET(curvnet); /* ifp->if_vnet is already NULL here */
552
553 KASSERT((ifp->if_flags & IFF_DYING),
554 ("if_free_internal: interface not dying"));
555
556 IFNET_WLOCK();
557 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
558 ("%s: freeing unallocated ifnet", ifp->if_xname));
559
560 ifnet_setbyindex(ifp->if_index, NULL);
561 while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL)
562 V_if_index--;
563 IFNET_WUNLOCK();
564
565 if (if_com_free[ifp->if_alloctype] != NULL)
566 if_com_free[ifp->if_alloctype](ifp->if_l2com,
567 ifp->if_alloctype);
568
569#ifdef MAC
570 mac_ifnet_destroy(ifp);
571#endif /* MAC */
572 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
573 knlist_clear(&ifp->if_klist, 0);
574 knlist_destroy(&ifp->if_klist);
575 IF_AFDATA_DESTROY(ifp);
576 IF_ADDR_LOCK_DESTROY(ifp);
577 free(ifp, M_IFNET);
578}
579
580/*
581 * This version should only be called by intefaces that switch their type
582 * after calling if_alloc(). if_free_type() will go away again now that we
583 * have if_alloctype to cache the original allocation type. For now, assert
584 * that they match, since we require that in practice.
585 */
586void
587if_free_type(struct ifnet *ifp, u_char type)
588{
589
590 KASSERT(ifp->if_alloctype == type,
591 ("if_free_type: type (%d) != alloctype (%d)", type,
592 ifp->if_alloctype));
593
594 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
595 if (!refcount_release(&ifp->if_refcount))
596 return;
597 if_free_internal(ifp);
598}
599
600/*
601 * This is the normal version of if_free(), used by device drivers to free a
602 * detached network interface. The contents of if_free_type() will move into
603 * here when if_free_type() goes away.
604 */
605void
606if_free(struct ifnet *ifp)
607{
608
609 if_free_type(ifp, ifp->if_alloctype);
610}
611
612/*
613 * Interfaces to keep an ifnet type-stable despite the possibility of the
614 * driver calling if_free(). If there are additional references, we defer
615 * freeing the underlying data structure.
616 */
617void
618if_ref(struct ifnet *ifp)
619{
620
621 /* We don't assert the ifnet list lock here, but arguably should. */
622 refcount_acquire(&ifp->if_refcount);
623}
624
625void
626if_rele(struct ifnet *ifp)
627{
628
629 if (!refcount_release(&ifp->if_refcount))
630 return;
631 if_free_internal(ifp);
632}
633
634void
635ifq_attach(struct ifaltq *ifq, struct ifnet *ifp)
636{
637
638 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
639
640 if (ifq->ifq_maxlen == 0)
641 ifq->ifq_maxlen = ifqmaxlen;
642
643 ifq->altq_type = 0;
644 ifq->altq_disc = NULL;
645 ifq->altq_flags &= ALTQF_CANTCHANGE;
646 ifq->altq_tbr = NULL;
647 ifq->altq_ifp = ifp;
648}
649
650void
651ifq_detach(struct ifaltq *ifq)
652{
653 mtx_destroy(&ifq->ifq_mtx);
654}
655
656/*
657 * Perform generic interface initalization tasks and attach the interface
658 * to the list of "active" interfaces. If vmove flag is set on entry
659 * to if_attach_internal(), perform only a limited subset of initialization
660 * tasks, given that we are moving from one vnet to another an ifnet which
661 * has already been fully initialized.
662 *
663 * XXX:
664 * - The decision to return void and thus require this function to
665 * succeed is questionable.
666 * - We should probably do more sanity checking. For instance we don't
667 * do anything to insure if_xname is unique or non-empty.
668 */
669void
670if_attach(struct ifnet *ifp)
671{
672
673 if_attach_internal(ifp, 0);
674}
675
676static void
677if_attach_internal(struct ifnet *ifp, int vmove)
678{
679 INIT_VNET_NET(curvnet);
680 unsigned socksize, ifasize;
681 int namelen, masklen;
682 struct sockaddr_dl *sdl;
683 struct ifaddr *ifa;
684
685 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
686 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
687 ifp->if_xname);
688
689#ifdef VIMAGE
690 ifp->if_vnet = curvnet;
691#endif
692
693 if_addgroup(ifp, IFG_ALL);
694
695 getmicrotime(&ifp->if_lastchange);
696 ifp->if_data.ifi_epoch = time_uptime;
697 ifp->if_data.ifi_datalen = sizeof(struct if_data);
698
699 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
700 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
701 ("transmit and qflush must both either be set or both be NULL"));
702 if (ifp->if_transmit == NULL) {
703 ifp->if_transmit = if_transmit;
704 ifp->if_qflush = if_qflush;
705 }
706
707 if (!vmove) {
708#ifdef MAC
709 mac_ifnet_create(ifp);
710#endif
711
712 if (IS_DEFAULT_VNET(curvnet)) {
713 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
714 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s",
715 net_cdevsw.d_name, ifp->if_xname));
716 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
717 net_cdevsw.d_name, ifp->if_index);
718 }
719
720 ifq_attach(&ifp->if_snd, ifp);
721
722 /*
723 * Create a Link Level name for this device.
724 */
725 namelen = strlen(ifp->if_xname);
726 /*
727 * Always save enough space for any possiable name so we
728 * can do a rename in place later.
729 */
730 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
731 socksize = masklen + ifp->if_addrlen;
732 if (socksize < sizeof(*sdl))
733 socksize = sizeof(*sdl);
734 socksize = roundup2(socksize, sizeof(long));
735 ifasize = sizeof(*ifa) + 2 * socksize;
736 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
737 IFA_LOCK_INIT(ifa);
738 sdl = (struct sockaddr_dl *)(ifa + 1);
739 sdl->sdl_len = socksize;
740 sdl->sdl_family = AF_LINK;
741 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
742 sdl->sdl_nlen = namelen;
743 sdl->sdl_index = ifp->if_index;
744 sdl->sdl_type = ifp->if_type;
745 ifp->if_addr = ifa;
746 ifa->ifa_ifp = ifp;
747 ifa->ifa_rtrequest = link_rtrequest;
748 ifa->ifa_addr = (struct sockaddr *)sdl;
749 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
750 ifa->ifa_netmask = (struct sockaddr *)sdl;
751 sdl->sdl_len = masklen;
752 while (namelen != 0)
753 sdl->sdl_data[--namelen] = 0xff;
754 ifa->ifa_refcnt = 1;
755 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
756 /* Reliably crash if used uninitialized. */
757 ifp->if_broadcastaddr = NULL;
758 }
759
760 IFNET_WLOCK();
761 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
762#ifdef VIMAGE
763 curvnet->ifcnt++;
764#endif
765 IFNET_WUNLOCK();
766
767 if (domain_init_status >= 2)
768 if_attachdomain1(ifp);
769
770 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
771 if (IS_DEFAULT_VNET(curvnet))
772 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
773
774 /* Announce the interface. */
775 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
776
777 if (!vmove && ifp->if_watchdog != NULL) {
778 if_printf(ifp,
779 "WARNING: using obsoleted if_watchdog interface\n");
780
781 /*
782 * Note that we need if_slowtimo(). If this happens after
783 * boot, then call if_slowtimo() directly.
784 */
785 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
786 if_slowtimo(0);
787 }
788}
789
790static void
791if_attachdomain(void *dummy)
792{
793 INIT_VNET_NET(curvnet);
794 struct ifnet *ifp;
795 int s;
796
797 s = splnet();
798 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
799 if_attachdomain1(ifp);
800 splx(s);
801}
802SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
803 if_attachdomain, NULL);
804
805static void
806if_attachdomain1(struct ifnet *ifp)
807{
808 struct domain *dp;
809 int s;
810
811 s = splnet();
812
813 /*
814 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
815 * cannot lock ifp->if_afdata initialization, entirely.
816 */
817 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
818 splx(s);
819 return;
820 }
821 if (ifp->if_afdata_initialized >= domain_init_status) {
822 IF_AFDATA_UNLOCK(ifp);
823 splx(s);
824 printf("if_attachdomain called more than once on %s\n",
825 ifp->if_xname);
826 return;
827 }
828 ifp->if_afdata_initialized = domain_init_status;
829 IF_AFDATA_UNLOCK(ifp);
830
831 /* address family dependent data region */
832 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
833 for (dp = domains; dp; dp = dp->dom_next) {
834 if (dp->dom_ifattach)
835 ifp->if_afdata[dp->dom_family] =
836 (*dp->dom_ifattach)(ifp);
837 }
838
839 splx(s);
840}
841
842/*
843 * Remove any unicast or broadcast network addresses from an interface.
844 */
845void
846if_purgeaddrs(struct ifnet *ifp)
847{
848 struct ifaddr *ifa, *next;
849
850 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
851 if (ifa->ifa_addr->sa_family == AF_LINK)
852 continue;
853#ifdef INET
854 /* XXX: Ugly!! ad hoc just for INET */
855 if (ifa->ifa_addr->sa_family == AF_INET) {
856 struct ifaliasreq ifr;
857
858 bzero(&ifr, sizeof(ifr));
859 ifr.ifra_addr = *ifa->ifa_addr;
860 if (ifa->ifa_dstaddr)
861 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
862 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
863 NULL) == 0)
864 continue;
865 }
866#endif /* INET */
867#ifdef INET6
868 if (ifa->ifa_addr->sa_family == AF_INET6) {
869 in6_purgeaddr(ifa);
870 /* ifp_addrhead is already updated */
871 continue;
872 }
873#endif /* INET6 */
874 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
875 IFAFREE(ifa);
876 }
877}
878
879/*
880 * Remove any multicast network addresses from an interface.
881 */
882void
883if_purgemaddrs(struct ifnet *ifp)
884{
885 struct ifmultiaddr *ifma;
886 struct ifmultiaddr *next;
887
888 IF_ADDR_LOCK(ifp);
889 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
890 if_delmulti_locked(ifp, ifma, 1);
891 IF_ADDR_UNLOCK(ifp);
892}
893
894/*
895 * Detach an interface, removing it from the list of "active" interfaces.
896 * If vmove flag is set on entry to if_detach_internal(), perform only a
897 * limited subset of cleanup tasks, given that we are moving an ifnet from
898 * one vnet to another, where it must be fully operational.
899 *
900 * XXXRW: There are some significant questions about event ordering, and
901 * how to prevent things from starting to use the interface during detach.
902 */
903void
904if_detach(struct ifnet *ifp)
905{
906
907 if_detach_internal(ifp, 0);
908}
909
910static void
911if_detach_internal(struct ifnet *ifp, int vmove)
912{
913 INIT_VNET_NET(ifp->if_vnet);
914 struct ifaddr *ifa;
915 struct radix_node_head *rnh;
916 int i, j;
917 struct domain *dp;
918 struct ifnet *iter;
919 int found = 0;
920
921 IFNET_WLOCK();
922 TAILQ_FOREACH(iter, &V_ifnet, if_link)
923 if (iter == ifp) {
924 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
925 found = 1;
926 break;
927 }
928#ifdef VIMAGE
929 if (found)
930 curvnet->ifcnt--;
931#endif
932 IFNET_WUNLOCK();
933 if (!found) {
934 if (vmove)
935 panic("interface not in it's own ifnet list");
936 else
937 return; /* XXX this should panic as well? */
938 }
939
940 /*
941 * Remove/wait for pending events.
942 */
943 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
944
945 /*
946 * Remove routes and flush queues.
947 */
948 if_down(ifp);
949#ifdef ALTQ
950 if (ALTQ_IS_ENABLED(&ifp->if_snd))
951 altq_disable(&ifp->if_snd);
952 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
953 altq_detach(&ifp->if_snd);
954#endif
955
956 if_purgeaddrs(ifp);
957
958#ifdef INET
959 in_ifdetach(ifp);
960#endif
961
962#ifdef INET6
963 /*
964 * Remove all IPv6 kernel structs related to ifp. This should be done
965 * before removing routing entries below, since IPv6 interface direct
966 * routes are expected to be removed by the IPv6-specific kernel API.
967 * Otherwise, the kernel will detect some inconsistency and bark it.
968 */
969 in6_ifdetach(ifp);
970#endif
971 if_purgemaddrs(ifp);
972
973 if (!vmove) {
974 /*
975 * Prevent further calls into the device driver via ifnet.
976 */
977 if_dead(ifp);
978
979 /*
980 * Remove link ifaddr pointer and maybe decrement if_index.
981 * Clean up all addresses.
982 */
983 ifp->if_addr = NULL;
984 if (IS_DEFAULT_VNET(curvnet))
985 destroy_dev(ifdev_byindex(ifp->if_index));
986 ifdev_setbyindex(ifp->if_index, NULL);
987
988 /* We can now free link ifaddr. */
989 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
990 ifa = TAILQ_FIRST(&ifp->if_addrhead);
991 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
992 IFAFREE(ifa);
993 }
994 }
995
996 /*
997 * Delete all remaining routes using this interface
998 * Unfortuneatly the only way to do this is to slog through
999 * the entire routing table looking for routes which point
1000 * to this interface...oh well...
1001 */
1002 for (i = 1; i <= AF_MAX; i++) {
1003 for (j = 0; j < rt_numfibs; j++) {
1004 if ((rnh = V_rt_tables[j][i]) == NULL)
1004 rnh = rt_tables_get_rnh(j, i);
1005 if (rnh == NULL)
1005 continue;
1006 RADIX_NODE_HEAD_LOCK(rnh);
1007 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
1008 RADIX_NODE_HEAD_UNLOCK(rnh);
1009 }
1010 }
1011
1012 /* Announce that the interface is gone. */
1013 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1014 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1015 if (IS_DEFAULT_VNET(curvnet))
1016 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1017 if_delgroups(ifp);
1018
1019 IF_AFDATA_LOCK(ifp);
1020 for (dp = domains; dp; dp = dp->dom_next) {
1021 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1022 (*dp->dom_ifdetach)(ifp,
1023 ifp->if_afdata[dp->dom_family]);
1024 }
1025 ifp->if_afdata_initialized = 0;
1026 IF_AFDATA_UNLOCK(ifp);
1027
1028 if (!vmove)
1029 ifq_detach(&ifp->if_snd);
1030}
1031
1032#ifdef VIMAGE
1033/*
1034 * if_vmove() performs a limited version of if_detach() in current
1035 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1036 * An attempt is made to shrink if_index in current vnet, find an
1037 * unused if_index in target vnet and calls if_grow() if necessary,
1038 * and finally find an unused if_xname for the target vnet.
1039 */
1040void
1041if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1042{
1043
1044 /*
1045 * Detach from current vnet, but preserve LLADDR info, do not
1046 * mark as dead etc. so that the ifnet can be reattached later.
1047 */
1048 if_detach_internal(ifp, 1);
1049
1050 /*
1051 * Unlink the ifnet from ifindex_table[] in current vnet,
1052 * and shrink the if_index for that vnet if possible.
1053 * do / while construct below is needed to confine the scope
1054 * of INIT_VNET_NET().
1055 */
1056 {
1057 INIT_VNET_NET(curvnet);
1058
1059 IFNET_WLOCK();
1060 ifnet_setbyindex(ifp->if_index, NULL);
1061 while (V_if_index > 0 && \
1062 ifnet_byindex_locked(V_if_index) == NULL)
1063 V_if_index--;
1064 IFNET_WUNLOCK();
1065 };
1066
1067 /*
1068 * Switch to the context of the target vnet.
1069 */
1070 CURVNET_SET_QUIET(new_vnet);
1071 INIT_VNET_NET(new_vnet);
1072
1073 /*
1074 * Try to find an empty slot below if_index. If we fail, take
1075 * the next slot.
1076 */
1077 IFNET_WLOCK();
1078 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
1079 if (ifnet_byindex_locked(ifp->if_index) == NULL)
1080 break;
1081 }
1082 /* Catch if_index overflow. */
1083 if (ifp->if_index < 1)
1084 panic("if_index overflow");
1085
1086 if (ifp->if_index > V_if_index)
1087 V_if_index = ifp->if_index;
1088 if (V_if_index >= V_if_indexlim)
1089 if_grow();
1090 ifnet_setbyindex(ifp->if_index, ifp);
1091 IFNET_WUNLOCK();
1092
1093 if_attach_internal(ifp, 1);
1094
1095 CURVNET_RESTORE();
1096}
1097#endif /* VIMAGE */
1098
1099/*
1100 * Add a group to an interface
1101 */
1102int
1103if_addgroup(struct ifnet *ifp, const char *groupname)
1104{
1105 INIT_VNET_NET(ifp->if_vnet);
1106 struct ifg_list *ifgl;
1107 struct ifg_group *ifg = NULL;
1108 struct ifg_member *ifgm;
1109
1110 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1111 groupname[strlen(groupname) - 1] <= '9')
1112 return (EINVAL);
1113
1114 IFNET_WLOCK();
1115 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1116 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1117 IFNET_WUNLOCK();
1118 return (EEXIST);
1119 }
1120
1121 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1122 M_NOWAIT)) == NULL) {
1123 IFNET_WUNLOCK();
1124 return (ENOMEM);
1125 }
1126
1127 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1128 M_TEMP, M_NOWAIT)) == NULL) {
1129 free(ifgl, M_TEMP);
1130 IFNET_WUNLOCK();
1131 return (ENOMEM);
1132 }
1133
1134 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1135 if (!strcmp(ifg->ifg_group, groupname))
1136 break;
1137
1138 if (ifg == NULL) {
1139 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1140 M_TEMP, M_NOWAIT)) == NULL) {
1141 free(ifgl, M_TEMP);
1142 free(ifgm, M_TEMP);
1143 IFNET_WUNLOCK();
1144 return (ENOMEM);
1145 }
1146 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1147 ifg->ifg_refcnt = 0;
1148 TAILQ_INIT(&ifg->ifg_members);
1149 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1150 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1151 }
1152
1153 ifg->ifg_refcnt++;
1154 ifgl->ifgl_group = ifg;
1155 ifgm->ifgm_ifp = ifp;
1156
1157 IF_ADDR_LOCK(ifp);
1158 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1159 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1160 IF_ADDR_UNLOCK(ifp);
1161
1162 IFNET_WUNLOCK();
1163
1164 EVENTHANDLER_INVOKE(group_change_event, groupname);
1165
1166 return (0);
1167}
1168
1169/*
1170 * Remove a group from an interface
1171 */
1172int
1173if_delgroup(struct ifnet *ifp, const char *groupname)
1174{
1175 INIT_VNET_NET(ifp->if_vnet);
1176 struct ifg_list *ifgl;
1177 struct ifg_member *ifgm;
1178
1179 IFNET_WLOCK();
1180 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1181 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1182 break;
1183 if (ifgl == NULL) {
1184 IFNET_WUNLOCK();
1185 return (ENOENT);
1186 }
1187
1188 IF_ADDR_LOCK(ifp);
1189 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1190 IF_ADDR_UNLOCK(ifp);
1191
1192 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1193 if (ifgm->ifgm_ifp == ifp)
1194 break;
1195
1196 if (ifgm != NULL) {
1197 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1198 free(ifgm, M_TEMP);
1199 }
1200
1201 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1202 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1203 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1204 free(ifgl->ifgl_group, M_TEMP);
1205 }
1206 IFNET_WUNLOCK();
1207
1208 free(ifgl, M_TEMP);
1209
1210 EVENTHANDLER_INVOKE(group_change_event, groupname);
1211
1212 return (0);
1213}
1214
1215/*
1216 * Remove an interface from all groups
1217 */
1218static void
1219if_delgroups(struct ifnet *ifp)
1220{
1221 INIT_VNET_NET(ifp->if_vnet);
1222 struct ifg_list *ifgl;
1223 struct ifg_member *ifgm;
1224 char groupname[IFNAMSIZ];
1225
1226 IFNET_WLOCK();
1227 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1228 ifgl = TAILQ_FIRST(&ifp->if_groups);
1229
1230 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1231
1232 IF_ADDR_LOCK(ifp);
1233 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1234 IF_ADDR_UNLOCK(ifp);
1235
1236 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1237 if (ifgm->ifgm_ifp == ifp)
1238 break;
1239
1240 if (ifgm != NULL) {
1241 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1242 ifgm_next);
1243 free(ifgm, M_TEMP);
1244 }
1245
1246 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1247 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1248 EVENTHANDLER_INVOKE(group_detach_event,
1249 ifgl->ifgl_group);
1250 free(ifgl->ifgl_group, M_TEMP);
1251 }
1252 IFNET_WUNLOCK();
1253
1254 free(ifgl, M_TEMP);
1255
1256 EVENTHANDLER_INVOKE(group_change_event, groupname);
1257
1258 IFNET_WLOCK();
1259 }
1260 IFNET_WUNLOCK();
1261}
1262
1263/*
1264 * Stores all groups from an interface in memory pointed
1265 * to by data
1266 */
1267static int
1268if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1269{
1270 int len, error;
1271 struct ifg_list *ifgl;
1272 struct ifg_req ifgrq, *ifgp;
1273 struct ifgroupreq *ifgr = data;
1274
1275 if (ifgr->ifgr_len == 0) {
1276 IF_ADDR_LOCK(ifp);
1277 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1278 ifgr->ifgr_len += sizeof(struct ifg_req);
1279 IF_ADDR_UNLOCK(ifp);
1280 return (0);
1281 }
1282
1283 len = ifgr->ifgr_len;
1284 ifgp = ifgr->ifgr_groups;
1285 /* XXX: wire */
1286 IF_ADDR_LOCK(ifp);
1287 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1288 if (len < sizeof(ifgrq)) {
1289 IF_ADDR_UNLOCK(ifp);
1290 return (EINVAL);
1291 }
1292 bzero(&ifgrq, sizeof ifgrq);
1293 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1294 sizeof(ifgrq.ifgrq_group));
1295 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1296 IF_ADDR_UNLOCK(ifp);
1297 return (error);
1298 }
1299 len -= sizeof(ifgrq);
1300 ifgp++;
1301 }
1302 IF_ADDR_UNLOCK(ifp);
1303
1304 return (0);
1305}
1306
1307/*
1308 * Stores all members of a group in memory pointed to by data
1309 */
1310static int
1311if_getgroupmembers(struct ifgroupreq *data)
1312{
1313 INIT_VNET_NET(curvnet);
1314 struct ifgroupreq *ifgr = data;
1315 struct ifg_group *ifg;
1316 struct ifg_member *ifgm;
1317 struct ifg_req ifgrq, *ifgp;
1318 int len, error;
1319
1320 IFNET_RLOCK();
1321 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1322 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1323 break;
1324 if (ifg == NULL) {
1325 IFNET_RUNLOCK();
1326 return (ENOENT);
1327 }
1328
1329 if (ifgr->ifgr_len == 0) {
1330 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1331 ifgr->ifgr_len += sizeof(ifgrq);
1332 IFNET_RUNLOCK();
1333 return (0);
1334 }
1335
1336 len = ifgr->ifgr_len;
1337 ifgp = ifgr->ifgr_groups;
1338 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1339 if (len < sizeof(ifgrq)) {
1340 IFNET_RUNLOCK();
1341 return (EINVAL);
1342 }
1343 bzero(&ifgrq, sizeof ifgrq);
1344 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1345 sizeof(ifgrq.ifgrq_member));
1346 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1347 IFNET_RUNLOCK();
1348 return (error);
1349 }
1350 len -= sizeof(ifgrq);
1351 ifgp++;
1352 }
1353 IFNET_RUNLOCK();
1354
1355 return (0);
1356}
1357
1358/*
1359 * Delete Routes for a Network Interface
1360 *
1361 * Called for each routing entry via the rnh->rnh_walktree() call above
1362 * to delete all route entries referencing a detaching network interface.
1363 *
1364 * Arguments:
1365 * rn pointer to node in the routing table
1366 * arg argument passed to rnh->rnh_walktree() - detaching interface
1367 *
1368 * Returns:
1369 * 0 successful
1370 * errno failed - reason indicated
1371 *
1372 */
1373static int
1374if_rtdel(struct radix_node *rn, void *arg)
1375{
1376 struct rtentry *rt = (struct rtentry *)rn;
1377 struct ifnet *ifp = arg;
1378 int err;
1379
1380 if (rt->rt_ifp == ifp) {
1381
1382 /*
1383 * Protect (sorta) against walktree recursion problems
1384 * with cloned routes
1385 */
1386 if ((rt->rt_flags & RTF_UP) == 0)
1387 return (0);
1388
1389 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1390 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1391 (struct rtentry **) NULL, rt->rt_fibnum);
1392 if (err) {
1393 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1394 }
1395 }
1396
1397 return (0);
1398}
1399
1400/*
1401 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1402 * structs used to represent other address families, it is necessary
1403 * to perform a different comparison.
1404 */
1405
1406#define sa_equal(a1, a2) \
1407 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1408
1409#define sa_dl_equal(a1, a2) \
1410 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1411 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1412 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1413 LLADDR((struct sockaddr_dl *)(a2)), \
1414 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1415
1416/*
1417 * Locate an interface based on a complete address.
1418 */
1419/*ARGSUSED*/
1420struct ifaddr *
1421ifa_ifwithaddr(struct sockaddr *addr)
1422{
1423 INIT_VNET_NET(curvnet);
1424 struct ifnet *ifp;
1425 struct ifaddr *ifa;
1426
1427 IFNET_RLOCK();
1428 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1429 IF_ADDR_LOCK(ifp);
1430 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1431 if (ifa->ifa_addr->sa_family != addr->sa_family)
1432 continue;
1433 if (sa_equal(addr, ifa->ifa_addr)) {
1434 IF_ADDR_UNLOCK(ifp);
1435 goto done;
1436 }
1437 /* IP6 doesn't have broadcast */
1438 if ((ifp->if_flags & IFF_BROADCAST) &&
1439 ifa->ifa_broadaddr &&
1440 ifa->ifa_broadaddr->sa_len != 0 &&
1441 sa_equal(ifa->ifa_broadaddr, addr)) {
1442 IF_ADDR_UNLOCK(ifp);
1443 goto done;
1444 }
1445 }
1446 IF_ADDR_UNLOCK(ifp);
1447 }
1448 ifa = NULL;
1449done:
1450 IFNET_RUNLOCK();
1451 return (ifa);
1452}
1453
1454/*
1455 * Locate an interface based on the broadcast address.
1456 */
1457/* ARGSUSED */
1458struct ifaddr *
1459ifa_ifwithbroadaddr(struct sockaddr *addr)
1460{
1461 INIT_VNET_NET(curvnet);
1462 struct ifnet *ifp;
1463 struct ifaddr *ifa;
1464
1465 IFNET_RLOCK();
1466 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1467 IF_ADDR_LOCK(ifp);
1468 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1469 if (ifa->ifa_addr->sa_family != addr->sa_family)
1470 continue;
1471 if ((ifp->if_flags & IFF_BROADCAST) &&
1472 ifa->ifa_broadaddr &&
1473 ifa->ifa_broadaddr->sa_len != 0 &&
1474 sa_equal(ifa->ifa_broadaddr, addr)) {
1475 IF_ADDR_UNLOCK(ifp);
1476 goto done;
1477 }
1478 }
1479 IF_ADDR_UNLOCK(ifp);
1480 }
1481 ifa = NULL;
1482done:
1483 IFNET_RUNLOCK();
1484 return (ifa);
1485}
1486
1487/*
1488 * Locate the point to point interface with a given destination address.
1489 */
1490/*ARGSUSED*/
1491struct ifaddr *
1492ifa_ifwithdstaddr(struct sockaddr *addr)
1493{
1494 INIT_VNET_NET(curvnet);
1495 struct ifnet *ifp;
1496 struct ifaddr *ifa;
1497
1498 IFNET_RLOCK();
1499 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1500 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1501 continue;
1502 IF_ADDR_LOCK(ifp);
1503 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1504 if (ifa->ifa_addr->sa_family != addr->sa_family)
1505 continue;
1506 if (ifa->ifa_dstaddr != NULL &&
1507 sa_equal(addr, ifa->ifa_dstaddr)) {
1508 IF_ADDR_UNLOCK(ifp);
1509 goto done;
1510 }
1511 }
1512 IF_ADDR_UNLOCK(ifp);
1513 }
1514 ifa = NULL;
1515done:
1516 IFNET_RUNLOCK();
1517 return (ifa);
1518}
1519
1520/*
1521 * Find an interface on a specific network. If many, choice
1522 * is most specific found.
1523 */
1524struct ifaddr *
1525ifa_ifwithnet(struct sockaddr *addr)
1526{
1527 INIT_VNET_NET(curvnet);
1528 struct ifnet *ifp;
1529 struct ifaddr *ifa;
1530 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1531 u_int af = addr->sa_family;
1532 char *addr_data = addr->sa_data, *cplim;
1533
1534 /*
1535 * AF_LINK addresses can be looked up directly by their index number,
1536 * so do that if we can.
1537 */
1538 if (af == AF_LINK) {
1539 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1540 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1541 return (ifaddr_byindex(sdl->sdl_index));
1542 }
1543
1544 /*
1545 * Scan though each interface, looking for ones that have
1546 * addresses in this address family.
1547 */
1548 IFNET_RLOCK();
1549 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1550 IF_ADDR_LOCK(ifp);
1551 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1552 char *cp, *cp2, *cp3;
1553
1554 if (ifa->ifa_addr->sa_family != af)
1555next: continue;
1556 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1557 /*
1558 * This is a bit broken as it doesn't
1559 * take into account that the remote end may
1560 * be a single node in the network we are
1561 * looking for.
1562 * The trouble is that we don't know the
1563 * netmask for the remote end.
1564 */
1565 if (ifa->ifa_dstaddr != NULL &&
1566 sa_equal(addr, ifa->ifa_dstaddr)) {
1567 IF_ADDR_UNLOCK(ifp);
1568 goto done;
1569 }
1570 } else {
1571 /*
1572 * if we have a special address handler,
1573 * then use it instead of the generic one.
1574 */
1575 if (ifa->ifa_claim_addr) {
1576 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1577 IF_ADDR_UNLOCK(ifp);
1578 goto done;
1579 }
1580 continue;
1581 }
1582
1583 /*
1584 * Scan all the bits in the ifa's address.
1585 * If a bit dissagrees with what we are
1586 * looking for, mask it with the netmask
1587 * to see if it really matters.
1588 * (A byte at a time)
1589 */
1590 if (ifa->ifa_netmask == 0)
1591 continue;
1592 cp = addr_data;
1593 cp2 = ifa->ifa_addr->sa_data;
1594 cp3 = ifa->ifa_netmask->sa_data;
1595 cplim = ifa->ifa_netmask->sa_len
1596 + (char *)ifa->ifa_netmask;
1597 while (cp3 < cplim)
1598 if ((*cp++ ^ *cp2++) & *cp3++)
1599 goto next; /* next address! */
1600 /*
1601 * If the netmask of what we just found
1602 * is more specific than what we had before
1603 * (if we had one) then remember the new one
1604 * before continuing to search
1605 * for an even better one.
1606 */
1607 if (ifa_maybe == 0 ||
1608 rn_refines((caddr_t)ifa->ifa_netmask,
1609 (caddr_t)ifa_maybe->ifa_netmask))
1610 ifa_maybe = ifa;
1611 }
1612 }
1613 IF_ADDR_UNLOCK(ifp);
1614 }
1615 ifa = ifa_maybe;
1616done:
1617 IFNET_RUNLOCK();
1618 return (ifa);
1619}
1620
1621/*
1622 * Find an interface address specific to an interface best matching
1623 * a given address.
1624 */
1625struct ifaddr *
1626ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1627{
1628 struct ifaddr *ifa;
1629 char *cp, *cp2, *cp3;
1630 char *cplim;
1631 struct ifaddr *ifa_maybe = 0;
1632 u_int af = addr->sa_family;
1633
1634 if (af >= AF_MAX)
1635 return (0);
1636 IF_ADDR_LOCK(ifp);
1637 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1638 if (ifa->ifa_addr->sa_family != af)
1639 continue;
1640 if (ifa_maybe == 0)
1641 ifa_maybe = ifa;
1642 if (ifa->ifa_netmask == 0) {
1643 if (sa_equal(addr, ifa->ifa_addr) ||
1644 (ifa->ifa_dstaddr &&
1645 sa_equal(addr, ifa->ifa_dstaddr)))
1646 goto done;
1647 continue;
1648 }
1649 if (ifp->if_flags & IFF_POINTOPOINT) {
1650 if (sa_equal(addr, ifa->ifa_dstaddr))
1651 goto done;
1652 } else {
1653 cp = addr->sa_data;
1654 cp2 = ifa->ifa_addr->sa_data;
1655 cp3 = ifa->ifa_netmask->sa_data;
1656 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1657 for (; cp3 < cplim; cp3++)
1658 if ((*cp++ ^ *cp2++) & *cp3)
1659 break;
1660 if (cp3 == cplim)
1661 goto done;
1662 }
1663 }
1664 ifa = ifa_maybe;
1665done:
1666 IF_ADDR_UNLOCK(ifp);
1667 return (ifa);
1668}
1669
1670#include <net/route.h>
1671#include <net/if_llatbl.h>
1672
1673/*
1674 * Default action when installing a route with a Link Level gateway.
1675 * Lookup an appropriate real ifa to point to.
1676 * This should be moved to /sys/net/link.c eventually.
1677 */
1678static void
1679link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1680{
1681 struct ifaddr *ifa, *oifa;
1682 struct sockaddr *dst;
1683 struct ifnet *ifp;
1684
1685 RT_LOCK_ASSERT(rt);
1686
1687 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1688 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1689 return;
1690 ifa = ifaof_ifpforaddr(dst, ifp);
1691 if (ifa) {
1692 IFAREF(ifa); /* XXX */
1693 oifa = rt->rt_ifa;
1694 rt->rt_ifa = ifa;
1695 IFAFREE(oifa);
1696 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1697 ifa->ifa_rtrequest(cmd, rt, info);
1698 }
1699}
1700
1701/*
1702 * Mark an interface down and notify protocols of
1703 * the transition.
1704 * NOTE: must be called at splnet or eqivalent.
1705 */
1706static void
1707if_unroute(struct ifnet *ifp, int flag, int fam)
1708{
1709 struct ifaddr *ifa;
1710
1711 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1712
1713 ifp->if_flags &= ~flag;
1714 getmicrotime(&ifp->if_lastchange);
1715 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1716 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1717 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1718 ifp->if_qflush(ifp);
1719
1720#ifdef DEV_CARP
1721 if (ifp->if_carp)
1722 carp_carpdev_state(ifp->if_carp);
1723#endif
1724 rt_ifmsg(ifp);
1725}
1726
1727/*
1728 * Mark an interface up and notify protocols of
1729 * the transition.
1730 * NOTE: must be called at splnet or eqivalent.
1731 */
1732static void
1733if_route(struct ifnet *ifp, int flag, int fam)
1734{
1735 struct ifaddr *ifa;
1736
1737 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1738
1739 ifp->if_flags |= flag;
1740 getmicrotime(&ifp->if_lastchange);
1741 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1742 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1743 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1744#ifdef DEV_CARP
1745 if (ifp->if_carp)
1746 carp_carpdev_state(ifp->if_carp);
1747#endif
1748 rt_ifmsg(ifp);
1749#ifdef INET6
1750 in6_if_up(ifp);
1751#endif
1752}
1753
1754void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1755void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1756
1757/*
1758 * Handle a change in the interface link state. To avoid LORs
1759 * between driver lock and upper layer locks, as well as possible
1760 * recursions, we post event to taskqueue, and all job
1761 * is done in static do_link_state_change().
1762 */
1763void
1764if_link_state_change(struct ifnet *ifp, int link_state)
1765{
1766 /* Return if state hasn't changed. */
1767 if (ifp->if_link_state == link_state)
1768 return;
1769
1770 ifp->if_link_state = link_state;
1771
1772 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1773}
1774
1775static void
1776do_link_state_change(void *arg, int pending)
1777{
1778 struct ifnet *ifp = (struct ifnet *)arg;
1779 int link_state = ifp->if_link_state;
1780 int link;
1781 CURVNET_SET(ifp->if_vnet);
1782
1783 /* Notify that the link state has changed. */
1784 rt_ifmsg(ifp);
1785 if (link_state == LINK_STATE_UP)
1786 link = NOTE_LINKUP;
1787 else if (link_state == LINK_STATE_DOWN)
1788 link = NOTE_LINKDOWN;
1789 else
1790 link = NOTE_LINKINV;
1791 KNOTE_UNLOCKED(&ifp->if_klist, link);
1792 if (ifp->if_vlantrunk != NULL)
1793 (*vlan_link_state_p)(ifp, link);
1794
1795 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1796 IFP2AC(ifp)->ac_netgraph != NULL)
1797 (*ng_ether_link_state_p)(ifp, link_state);
1798#ifdef DEV_CARP
1799 if (ifp->if_carp)
1800 carp_carpdev_state(ifp->if_carp);
1801#endif
1802 if (ifp->if_bridge) {
1803 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1804 (*bstp_linkstate_p)(ifp, link_state);
1805 }
1806 if (ifp->if_lagg) {
1807 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1808 (*lagg_linkstate_p)(ifp, link_state);
1809 }
1810
1811 if (IS_DEFAULT_VNET(curvnet))
1812 devctl_notify("IFNET", ifp->if_xname,
1813 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1814 NULL);
1815 if (pending > 1)
1816 if_printf(ifp, "%d link states coalesced\n", pending);
1817 if (log_link_state_change)
1818 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1819 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1820 CURVNET_RESTORE();
1821}
1822
1823/*
1824 * Mark an interface down and notify protocols of
1825 * the transition.
1826 * NOTE: must be called at splnet or eqivalent.
1827 */
1828void
1829if_down(struct ifnet *ifp)
1830{
1831
1832 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1833}
1834
1835/*
1836 * Mark an interface up and notify protocols of
1837 * the transition.
1838 * NOTE: must be called at splnet or eqivalent.
1839 */
1840void
1841if_up(struct ifnet *ifp)
1842{
1843
1844 if_route(ifp, IFF_UP, AF_UNSPEC);
1845}
1846
1847/*
1848 * Flush an interface queue.
1849 */
1850void
1851if_qflush(struct ifnet *ifp)
1852{
1853 struct mbuf *m, *n;
1854 struct ifaltq *ifq;
1855
1856 ifq = &ifp->if_snd;
1857 IFQ_LOCK(ifq);
1858#ifdef ALTQ
1859 if (ALTQ_IS_ENABLED(ifq))
1860 ALTQ_PURGE(ifq);
1861#endif
1862 n = ifq->ifq_head;
1863 while ((m = n) != 0) {
1864 n = m->m_act;
1865 m_freem(m);
1866 }
1867 ifq->ifq_head = 0;
1868 ifq->ifq_tail = 0;
1869 ifq->ifq_len = 0;
1870 IFQ_UNLOCK(ifq);
1871}
1872
1873/*
1874 * Handle interface watchdog timer routines. Called
1875 * from softclock, we decrement timers (if set) and
1876 * call the appropriate interface routine on expiration.
1877 *
1878 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1879 * holding Giant.
1880 */
1881static void
1882if_slowtimo(void *arg)
1883{
1884 VNET_ITERATOR_DECL(vnet_iter);
1885 struct ifnet *ifp;
1886 int s = splimp();
1887
1888 IFNET_RLOCK();
1889 VNET_LIST_RLOCK();
1890 VNET_FOREACH(vnet_iter) {
1891 CURVNET_SET(vnet_iter);
1892 INIT_VNET_NET(vnet_iter);
1893 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1894 if (ifp->if_timer == 0 || --ifp->if_timer)
1895 continue;
1896 if (ifp->if_watchdog)
1897 (*ifp->if_watchdog)(ifp);
1898 }
1899 CURVNET_RESTORE();
1900 }
1901 VNET_LIST_RUNLOCK();
1902 IFNET_RUNLOCK();
1903 splx(s);
1904 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1905}
1906
1907/*
1908 * Map interface name to interface structure pointer, with or without
1909 * returning a reference.
1910 */
1911struct ifnet *
1912ifunit_ref(const char *name)
1913{
1914 INIT_VNET_NET(curvnet);
1915 struct ifnet *ifp;
1916
1917 IFNET_RLOCK();
1918 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1919 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
1920 !(ifp->if_flags & IFF_DYING))
1921 break;
1922 }
1923 if (ifp != NULL)
1924 if_ref(ifp);
1925 IFNET_RUNLOCK();
1926 return (ifp);
1927}
1928
1929struct ifnet *
1930ifunit(const char *name)
1931{
1932 INIT_VNET_NET(curvnet);
1933 struct ifnet *ifp;
1934
1935 IFNET_RLOCK();
1936 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1937 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1938 break;
1939 }
1940 IFNET_RUNLOCK();
1941 return (ifp);
1942}
1943
1944/*
1945 * Hardware specific interface ioctls.
1946 */
1947static int
1948ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1949{
1950 struct ifreq *ifr;
1951 struct ifstat *ifs;
1952 int error = 0;
1953 int new_flags, temp_flags;
1954 size_t namelen, onamelen;
1955 char new_name[IFNAMSIZ];
1956 struct ifaddr *ifa;
1957 struct sockaddr_dl *sdl;
1958
1959 ifr = (struct ifreq *)data;
1960 switch (cmd) {
1961 case SIOCGIFINDEX:
1962 ifr->ifr_index = ifp->if_index;
1963 break;
1964
1965 case SIOCGIFFLAGS:
1966 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1967 ifr->ifr_flags = temp_flags & 0xffff;
1968 ifr->ifr_flagshigh = temp_flags >> 16;
1969 break;
1970
1971 case SIOCGIFCAP:
1972 ifr->ifr_reqcap = ifp->if_capabilities;
1973 ifr->ifr_curcap = ifp->if_capenable;
1974 break;
1975
1976#ifdef MAC
1977 case SIOCGIFMAC:
1978 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
1979 break;
1980#endif
1981
1982 case SIOCGIFMETRIC:
1983 ifr->ifr_metric = ifp->if_metric;
1984 break;
1985
1986 case SIOCGIFMTU:
1987 ifr->ifr_mtu = ifp->if_mtu;
1988 break;
1989
1990 case SIOCGIFPHYS:
1991 ifr->ifr_phys = ifp->if_physical;
1992 break;
1993
1994 case SIOCSIFFLAGS:
1995 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1996 if (error)
1997 return (error);
1998 /*
1999 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2000 * check, so we don't need special handling here yet.
2001 */
2002 new_flags = (ifr->ifr_flags & 0xffff) |
2003 (ifr->ifr_flagshigh << 16);
2004 if (ifp->if_flags & IFF_SMART) {
2005 /* Smart drivers twiddle their own routes */
2006 } else if (ifp->if_flags & IFF_UP &&
2007 (new_flags & IFF_UP) == 0) {
2008 int s = splimp();
2009 if_down(ifp);
2010 splx(s);
2011 } else if (new_flags & IFF_UP &&
2012 (ifp->if_flags & IFF_UP) == 0) {
2013 int s = splimp();
2014 if_up(ifp);
2015 splx(s);
2016 }
2017 /* See if permanently promiscuous mode bit is about to flip */
2018 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2019 if (new_flags & IFF_PPROMISC)
2020 ifp->if_flags |= IFF_PROMISC;
2021 else if (ifp->if_pcount == 0)
2022 ifp->if_flags &= ~IFF_PROMISC;
2023 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2024 ifp->if_xname,
2025 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2026 }
2027 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2028 (new_flags &~ IFF_CANTCHANGE);
2029 if (ifp->if_ioctl) {
2030 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2031 }
2032 getmicrotime(&ifp->if_lastchange);
2033 break;
2034
2035 case SIOCSIFCAP:
2036 error = priv_check(td, PRIV_NET_SETIFCAP);
2037 if (error)
2038 return (error);
2039 if (ifp->if_ioctl == NULL)
2040 return (EOPNOTSUPP);
2041 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2042 return (EINVAL);
2043 error = (*ifp->if_ioctl)(ifp, cmd, data);
2044 if (error == 0)
2045 getmicrotime(&ifp->if_lastchange);
2046 break;
2047
2048#ifdef MAC
2049 case SIOCSIFMAC:
2050 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2051 break;
2052#endif
2053
2054 case SIOCSIFNAME:
2055 error = priv_check(td, PRIV_NET_SETIFNAME);
2056 if (error)
2057 return (error);
2058 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2059 if (error != 0)
2060 return (error);
2061 if (new_name[0] == '\0')
2062 return (EINVAL);
2063 if (ifunit(new_name) != NULL)
2064 return (EEXIST);
2065
2066 /* Announce the departure of the interface. */
2067 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2068 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2069
2070 log(LOG_INFO, "%s: changing name to '%s'\n",
2071 ifp->if_xname, new_name);
2072
2073 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2074 ifa = ifp->if_addr;
2075 IFA_LOCK(ifa);
2076 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2077 namelen = strlen(new_name);
2078 onamelen = sdl->sdl_nlen;
2079 /*
2080 * Move the address if needed. This is safe because we
2081 * allocate space for a name of length IFNAMSIZ when we
2082 * create this in if_attach().
2083 */
2084 if (namelen != onamelen) {
2085 bcopy(sdl->sdl_data + onamelen,
2086 sdl->sdl_data + namelen, sdl->sdl_alen);
2087 }
2088 bcopy(new_name, sdl->sdl_data, namelen);
2089 sdl->sdl_nlen = namelen;
2090 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2091 bzero(sdl->sdl_data, onamelen);
2092 while (namelen != 0)
2093 sdl->sdl_data[--namelen] = 0xff;
2094 IFA_UNLOCK(ifa);
2095
2096 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2097 /* Announce the return of the interface. */
2098 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2099 break;
2100
2101 case SIOCSIFMETRIC:
2102 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2103 if (error)
2104 return (error);
2105 ifp->if_metric = ifr->ifr_metric;
2106 getmicrotime(&ifp->if_lastchange);
2107 break;
2108
2109 case SIOCSIFPHYS:
2110 error = priv_check(td, PRIV_NET_SETIFPHYS);
2111 if (error)
2112 return (error);
2113 if (ifp->if_ioctl == NULL)
2114 return (EOPNOTSUPP);
2115 error = (*ifp->if_ioctl)(ifp, cmd, data);
2116 if (error == 0)
2117 getmicrotime(&ifp->if_lastchange);
2118 break;
2119
2120 case SIOCSIFMTU:
2121 {
2122 u_long oldmtu = ifp->if_mtu;
2123
2124 error = priv_check(td, PRIV_NET_SETIFMTU);
2125 if (error)
2126 return (error);
2127 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2128 return (EINVAL);
2129 if (ifp->if_ioctl == NULL)
2130 return (EOPNOTSUPP);
2131 error = (*ifp->if_ioctl)(ifp, cmd, data);
2132 if (error == 0) {
2133 getmicrotime(&ifp->if_lastchange);
2134 rt_ifmsg(ifp);
2135 }
2136 /*
2137 * If the link MTU changed, do network layer specific procedure.
2138 */
2139 if (ifp->if_mtu != oldmtu) {
2140#ifdef INET6
2141 nd6_setmtu(ifp);
2142#endif
2143 }
2144 break;
2145 }
2146
2147 case SIOCADDMULTI:
2148 case SIOCDELMULTI:
2149 if (cmd == SIOCADDMULTI)
2150 error = priv_check(td, PRIV_NET_ADDMULTI);
2151 else
2152 error = priv_check(td, PRIV_NET_DELMULTI);
2153 if (error)
2154 return (error);
2155
2156 /* Don't allow group membership on non-multicast interfaces. */
2157 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2158 return (EOPNOTSUPP);
2159
2160 /* Don't let users screw up protocols' entries. */
2161 if (ifr->ifr_addr.sa_family != AF_LINK)
2162 return (EINVAL);
2163
2164 if (cmd == SIOCADDMULTI) {
2165 struct ifmultiaddr *ifma;
2166
2167 /*
2168 * Userland is only permitted to join groups once
2169 * via the if_addmulti() KPI, because it cannot hold
2170 * struct ifmultiaddr * between calls. It may also
2171 * lose a race while we check if the membership
2172 * already exists.
2173 */
2174 IF_ADDR_LOCK(ifp);
2175 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2176 IF_ADDR_UNLOCK(ifp);
2177 if (ifma != NULL)
2178 error = EADDRINUSE;
2179 else
2180 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2181 } else {
2182 error = if_delmulti(ifp, &ifr->ifr_addr);
2183 }
2184 if (error == 0)
2185 getmicrotime(&ifp->if_lastchange);
2186 break;
2187
2188 case SIOCSIFPHYADDR:
2189 case SIOCDIFPHYADDR:
2190#ifdef INET6
2191 case SIOCSIFPHYADDR_IN6:
2192#endif
2193 case SIOCSLIFPHYADDR:
2194 case SIOCSIFMEDIA:
2195 case SIOCSIFGENERIC:
2196 error = priv_check(td, PRIV_NET_HWIOCTL);
2197 if (error)
2198 return (error);
2199 if (ifp->if_ioctl == NULL)
2200 return (EOPNOTSUPP);
2201 error = (*ifp->if_ioctl)(ifp, cmd, data);
2202 if (error == 0)
2203 getmicrotime(&ifp->if_lastchange);
2204 break;
2205
2206 case SIOCGIFSTATUS:
2207 ifs = (struct ifstat *)data;
2208 ifs->ascii[0] = '\0';
2209
2210 case SIOCGIFPSRCADDR:
2211 case SIOCGIFPDSTADDR:
2212 case SIOCGLIFPHYADDR:
2213 case SIOCGIFMEDIA:
2214 case SIOCGIFGENERIC:
2215 if (ifp->if_ioctl == NULL)
2216 return (EOPNOTSUPP);
2217 error = (*ifp->if_ioctl)(ifp, cmd, data);
2218 break;
2219
2220 case SIOCSIFLLADDR:
2221 error = priv_check(td, PRIV_NET_SETLLADDR);
2222 if (error)
2223 return (error);
2224 error = if_setlladdr(ifp,
2225 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2226 break;
2227
2228 case SIOCAIFGROUP:
2229 {
2230 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2231
2232 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2233 if (error)
2234 return (error);
2235 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2236 return (error);
2237 break;
2238 }
2239
2240 case SIOCGIFGROUP:
2241 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2242 return (error);
2243 break;
2244
2245 case SIOCDIFGROUP:
2246 {
2247 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2248
2249 error = priv_check(td, PRIV_NET_DELIFGROUP);
2250 if (error)
2251 return (error);
2252 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2253 return (error);
2254 break;
2255 }
2256
2257 default:
2258 error = ENOIOCTL;
2259 break;
2260 }
2261 return (error);
2262}
2263
2264/*
2265 * Interface ioctls.
2266 */
2267int
2268ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2269{
2270 struct ifnet *ifp;
2271 struct ifreq *ifr;
2272 int error;
2273 int oif_flags;
2274
2275 switch (cmd) {
2276 case SIOCGIFCONF:
2277 case OSIOCGIFCONF:
2278#ifdef __amd64__
2279 case SIOCGIFCONF32:
2280#endif
2281 return (ifconf(cmd, data));
2282 }
2283 ifr = (struct ifreq *)data;
2284
2285 switch (cmd) {
2286#ifdef VIMAGE
2287 /*
2288 * XXX vnet creation will be implemented through the new jail
2289 * framework - this is just a temporary hack for testing the
2290 * vnet create / destroy mechanisms.
2291 */
2292 case SIOCSIFVIMAGE:
2293 error = vi_if_move((struct vi_req *) data, NULL,
2294 TD_TO_VIMAGE(td));
2295 return (error);
2296 case SIOCSPVIMAGE:
2297 case SIOCGPVIMAGE:
2298 error = vi_td_ioctl(cmd, (struct vi_req *) data, td);
2299 return (error);
2300#endif
2301 case SIOCIFCREATE:
2302 case SIOCIFCREATE2:
2303 error = priv_check(td, PRIV_NET_IFCREATE);
2304 if (error)
2305 return (error);
2306 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2307 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2308 case SIOCIFDESTROY:
2309 error = priv_check(td, PRIV_NET_IFDESTROY);
2310 if (error)
2311 return (error);
2312 return if_clone_destroy(ifr->ifr_name);
2313
2314 case SIOCIFGCLONERS:
2315 return (if_clone_list((struct if_clonereq *)data));
2316 case SIOCGIFGMEMB:
2317 return (if_getgroupmembers((struct ifgroupreq *)data));
2318 }
2319
2320 ifp = ifunit_ref(ifr->ifr_name);
2321 if (ifp == NULL)
2322 return (ENXIO);
2323
2324 error = ifhwioctl(cmd, ifp, data, td);
2325 if (error != ENOIOCTL) {
2326 if_rele(ifp);
2327 return (error);
2328 }
2329
2330 oif_flags = ifp->if_flags;
2331 if (so->so_proto == NULL) {
2332 if_rele(ifp);
2333 return (EOPNOTSUPP);
2334 }
2335#ifndef COMPAT_43
2336 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2337 data,
2338 ifp, td));
2339 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2340 error = (*ifp->if_ioctl)(ifp, cmd, data);
2341#else
2342 {
2343 int ocmd = cmd;
2344
2345 switch (cmd) {
2346
2347 case SIOCSIFDSTADDR:
2348 case SIOCSIFADDR:
2349 case SIOCSIFBRDADDR:
2350 case SIOCSIFNETMASK:
2351#if BYTE_ORDER != BIG_ENDIAN
2352 if (ifr->ifr_addr.sa_family == 0 &&
2353 ifr->ifr_addr.sa_len < 16) {
2354 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2355 ifr->ifr_addr.sa_len = 16;
2356 }
2357#else
2358 if (ifr->ifr_addr.sa_len == 0)
2359 ifr->ifr_addr.sa_len = 16;
2360#endif
2361 break;
2362
2363 case OSIOCGIFADDR:
2364 cmd = SIOCGIFADDR;
2365 break;
2366
2367 case OSIOCGIFDSTADDR:
2368 cmd = SIOCGIFDSTADDR;
2369 break;
2370
2371 case OSIOCGIFBRDADDR:
2372 cmd = SIOCGIFBRDADDR;
2373 break;
2374
2375 case OSIOCGIFNETMASK:
2376 cmd = SIOCGIFNETMASK;
2377 }
2378 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2379 cmd,
2380 data,
2381 ifp, td));
2382 if (error == EOPNOTSUPP && ifp != NULL &&
2383 ifp->if_ioctl != NULL)
2384 error = (*ifp->if_ioctl)(ifp, cmd, data);
2385 switch (ocmd) {
2386
2387 case OSIOCGIFADDR:
2388 case OSIOCGIFDSTADDR:
2389 case OSIOCGIFBRDADDR:
2390 case OSIOCGIFNETMASK:
2391 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2392
2393 }
2394 }
2395#endif /* COMPAT_43 */
2396
2397 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2398#ifdef INET6
2399 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2400 if (ifp->if_flags & IFF_UP) {
2401 int s = splimp();
2402 in6_if_up(ifp);
2403 splx(s);
2404 }
2405#endif
2406 }
2407 if_rele(ifp);
2408 return (error);
2409}
2410
2411/*
2412 * The code common to handling reference counted flags,
2413 * e.g., in ifpromisc() and if_allmulti().
2414 * The "pflag" argument can specify a permanent mode flag to check,
2415 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2416 *
2417 * Only to be used on stack-owned flags, not driver-owned flags.
2418 */
2419static int
2420if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2421{
2422 struct ifreq ifr;
2423 int error;
2424 int oldflags, oldcount;
2425
2426 /* Sanity checks to catch programming errors */
2427 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2428 ("%s: setting driver-owned flag %d", __func__, flag));
2429
2430 if (onswitch)
2431 KASSERT(*refcount >= 0,
2432 ("%s: increment negative refcount %d for flag %d",
2433 __func__, *refcount, flag));
2434 else
2435 KASSERT(*refcount > 0,
2436 ("%s: decrement non-positive refcount %d for flag %d",
2437 __func__, *refcount, flag));
2438
2439 /* In case this mode is permanent, just touch refcount */
2440 if (ifp->if_flags & pflag) {
2441 *refcount += onswitch ? 1 : -1;
2442 return (0);
2443 }
2444
2445 /* Save ifnet parameters for if_ioctl() may fail */
2446 oldcount = *refcount;
2447 oldflags = ifp->if_flags;
2448
2449 /*
2450 * See if we aren't the only and touching refcount is enough.
2451 * Actually toggle interface flag if we are the first or last.
2452 */
2453 if (onswitch) {
2454 if ((*refcount)++)
2455 return (0);
2456 ifp->if_flags |= flag;
2457 } else {
2458 if (--(*refcount))
2459 return (0);
2460 ifp->if_flags &= ~flag;
2461 }
2462
2463 /* Call down the driver since we've changed interface flags */
2464 if (ifp->if_ioctl == NULL) {
2465 error = EOPNOTSUPP;
2466 goto recover;
2467 }
2468 ifr.ifr_flags = ifp->if_flags & 0xffff;
2469 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2470 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2471 if (error)
2472 goto recover;
2473 /* Notify userland that interface flags have changed */
2474 rt_ifmsg(ifp);
2475 return (0);
2476
2477recover:
2478 /* Recover after driver error */
2479 *refcount = oldcount;
2480 ifp->if_flags = oldflags;
2481 return (error);
2482}
2483
2484/*
2485 * Set/clear promiscuous mode on interface ifp based on the truth value
2486 * of pswitch. The calls are reference counted so that only the first
2487 * "on" request actually has an effect, as does the final "off" request.
2488 * Results are undefined if the "off" and "on" requests are not matched.
2489 */
2490int
2491ifpromisc(struct ifnet *ifp, int pswitch)
2492{
2493 int error;
2494 int oldflags = ifp->if_flags;
2495
2496 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2497 &ifp->if_pcount, pswitch);
2498 /* If promiscuous mode status has changed, log a message */
2499 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2500 log(LOG_INFO, "%s: promiscuous mode %s\n",
2501 ifp->if_xname,
2502 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2503 return (error);
2504}
2505
2506/*
2507 * Return interface configuration
2508 * of system. List may be used
2509 * in later ioctl's (above) to get
2510 * other information.
2511 */
2512/*ARGSUSED*/
2513static int
2514ifconf(u_long cmd, caddr_t data)
2515{
2516 INIT_VNET_NET(curvnet);
2517 struct ifconf *ifc = (struct ifconf *)data;
2518#ifdef __amd64__
2519 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2520 struct ifconf ifc_swab;
2521#endif
2522 struct ifnet *ifp;
2523 struct ifaddr *ifa;
2524 struct ifreq ifr;
2525 struct sbuf *sb;
2526 int error, full = 0, valid_len, max_len;
2527
2528#ifdef __amd64__
2529 if (cmd == SIOCGIFCONF32) {
2530 ifc_swab.ifc_len = ifc32->ifc_len;
2531 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2532 ifc = &ifc_swab;
2533 }
2534#endif
2535 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2536 max_len = MAXPHYS - 1;
2537
2538 /* Prevent hostile input from being able to crash the system */
2539 if (ifc->ifc_len <= 0)
2540 return (EINVAL);
2541
2542again:
2543 if (ifc->ifc_len <= max_len) {
2544 max_len = ifc->ifc_len;
2545 full = 1;
2546 }
2547 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2548 max_len = 0;
2549 valid_len = 0;
2550
2551 IFNET_RLOCK(); /* could sleep XXX */
2552 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2553 int addrs;
2554
2555 /*
2556 * Zero the ifr_name buffer to make sure we don't
2557 * disclose the contents of the stack.
2558 */
2559 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2560
2561 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2562 >= sizeof(ifr.ifr_name)) {
2563 sbuf_delete(sb);
2564 IFNET_RUNLOCK();
2565 return (ENAMETOOLONG);
2566 }
2567
2568 addrs = 0;
2569 IF_ADDR_LOCK(ifp);
2570 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2571 struct sockaddr *sa = ifa->ifa_addr;
2572
2573 if (prison_if(curthread->td_ucred, sa) != 0)
2574 continue;
2575 addrs++;
2576#ifdef COMPAT_43
2577 if (cmd == OSIOCGIFCONF) {
2578 struct osockaddr *osa =
2579 (struct osockaddr *)&ifr.ifr_addr;
2580 ifr.ifr_addr = *sa;
2581 osa->sa_family = sa->sa_family;
2582 sbuf_bcat(sb, &ifr, sizeof(ifr));
2583 max_len += sizeof(ifr);
2584 } else
2585#endif
2586 if (sa->sa_len <= sizeof(*sa)) {
2587 ifr.ifr_addr = *sa;
2588 sbuf_bcat(sb, &ifr, sizeof(ifr));
2589 max_len += sizeof(ifr);
2590 } else {
2591 sbuf_bcat(sb, &ifr,
2592 offsetof(struct ifreq, ifr_addr));
2593 max_len += offsetof(struct ifreq, ifr_addr);
2594 sbuf_bcat(sb, sa, sa->sa_len);
2595 max_len += sa->sa_len;
2596 }
2597
2598 if (!sbuf_overflowed(sb))
2599 valid_len = sbuf_len(sb);
2600 }
2601 IF_ADDR_UNLOCK(ifp);
2602 if (addrs == 0) {
2603 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2604 sbuf_bcat(sb, &ifr, sizeof(ifr));
2605 max_len += sizeof(ifr);
2606
2607 if (!sbuf_overflowed(sb))
2608 valid_len = sbuf_len(sb);
2609 }
2610 }
2611 IFNET_RUNLOCK();
2612
2613 /*
2614 * If we didn't allocate enough space (uncommon), try again. If
2615 * we have already allocated as much space as we are allowed,
2616 * return what we've got.
2617 */
2618 if (valid_len != max_len && !full) {
2619 sbuf_delete(sb);
2620 goto again;
2621 }
2622
2623 ifc->ifc_len = valid_len;
2624#ifdef __amd64__
2625 if (cmd == SIOCGIFCONF32)
2626 ifc32->ifc_len = valid_len;
2627#endif
2628 sbuf_finish(sb);
2629 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2630 sbuf_delete(sb);
2631 return (error);
2632}
2633
2634/*
2635 * Just like ifpromisc(), but for all-multicast-reception mode.
2636 */
2637int
2638if_allmulti(struct ifnet *ifp, int onswitch)
2639{
2640
2641 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2642}
2643
2644struct ifmultiaddr *
2645if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2646{
2647 struct ifmultiaddr *ifma;
2648
2649 IF_ADDR_LOCK_ASSERT(ifp);
2650
2651 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2652 if (sa->sa_family == AF_LINK) {
2653 if (sa_dl_equal(ifma->ifma_addr, sa))
2654 break;
2655 } else {
2656 if (sa_equal(ifma->ifma_addr, sa))
2657 break;
2658 }
2659 }
2660
2661 return ifma;
2662}
2663
2664/*
2665 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2666 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2667 * the ifnet multicast address list here, so the caller must do that and
2668 * other setup work (such as notifying the device driver). The reference
2669 * count is initialized to 1.
2670 */
2671static struct ifmultiaddr *
2672if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2673 int mflags)
2674{
2675 struct ifmultiaddr *ifma;
2676 struct sockaddr *dupsa;
2677
2678 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2679 M_ZERO);
2680 if (ifma == NULL)
2681 return (NULL);
2682
2683 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2684 if (dupsa == NULL) {
2685 free(ifma, M_IFMADDR);
2686 return (NULL);
2687 }
2688 bcopy(sa, dupsa, sa->sa_len);
2689 ifma->ifma_addr = dupsa;
2690
2691 ifma->ifma_ifp = ifp;
2692 ifma->ifma_refcount = 1;
2693 ifma->ifma_protospec = NULL;
2694
2695 if (llsa == NULL) {
2696 ifma->ifma_lladdr = NULL;
2697 return (ifma);
2698 }
2699
2700 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2701 if (dupsa == NULL) {
2702 free(ifma->ifma_addr, M_IFMADDR);
2703 free(ifma, M_IFMADDR);
2704 return (NULL);
2705 }
2706 bcopy(llsa, dupsa, llsa->sa_len);
2707 ifma->ifma_lladdr = dupsa;
2708
2709 return (ifma);
2710}
2711
2712/*
2713 * if_freemulti: free ifmultiaddr structure and possibly attached related
2714 * addresses. The caller is responsible for implementing reference
2715 * counting, notifying the driver, handling routing messages, and releasing
2716 * any dependent link layer state.
2717 */
2718static void
2719if_freemulti(struct ifmultiaddr *ifma)
2720{
2721
2722 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2723 ifma->ifma_refcount));
2724 KASSERT(ifma->ifma_protospec == NULL,
2725 ("if_freemulti: protospec not NULL"));
2726
2727 if (ifma->ifma_lladdr != NULL)
2728 free(ifma->ifma_lladdr, M_IFMADDR);
2729 free(ifma->ifma_addr, M_IFMADDR);
2730 free(ifma, M_IFMADDR);
2731}
2732
2733/*
2734 * Register an additional multicast address with a network interface.
2735 *
2736 * - If the address is already present, bump the reference count on the
2737 * address and return.
2738 * - If the address is not link-layer, look up a link layer address.
2739 * - Allocate address structures for one or both addresses, and attach to the
2740 * multicast address list on the interface. If automatically adding a link
2741 * layer address, the protocol address will own a reference to the link
2742 * layer address, to be freed when it is freed.
2743 * - Notify the network device driver of an addition to the multicast address
2744 * list.
2745 *
2746 * 'sa' points to caller-owned memory with the desired multicast address.
2747 *
2748 * 'retifma' will be used to return a pointer to the resulting multicast
2749 * address reference, if desired.
2750 */
2751int
2752if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2753 struct ifmultiaddr **retifma)
2754{
2755 struct ifmultiaddr *ifma, *ll_ifma;
2756 struct sockaddr *llsa;
2757 int error;
2758
2759 /*
2760 * If the address is already present, return a new reference to it;
2761 * otherwise, allocate storage and set up a new address.
2762 */
2763 IF_ADDR_LOCK(ifp);
2764 ifma = if_findmulti(ifp, sa);
2765 if (ifma != NULL) {
2766 ifma->ifma_refcount++;
2767 if (retifma != NULL)
2768 *retifma = ifma;
2769 IF_ADDR_UNLOCK(ifp);
2770 return (0);
2771 }
2772
2773 /*
2774 * The address isn't already present; resolve the protocol address
2775 * into a link layer address, and then look that up, bump its
2776 * refcount or allocate an ifma for that also. If 'llsa' was
2777 * returned, we will need to free it later.
2778 */
2779 llsa = NULL;
2780 ll_ifma = NULL;
2781 if (ifp->if_resolvemulti != NULL) {
2782 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2783 if (error)
2784 goto unlock_out;
2785 }
2786
2787 /*
2788 * Allocate the new address. Don't hook it up yet, as we may also
2789 * need to allocate a link layer multicast address.
2790 */
2791 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2792 if (ifma == NULL) {
2793 error = ENOMEM;
2794 goto free_llsa_out;
2795 }
2796
2797 /*
2798 * If a link layer address is found, we'll need to see if it's
2799 * already present in the address list, or allocate is as well.
2800 * When this block finishes, the link layer address will be on the
2801 * list.
2802 */
2803 if (llsa != NULL) {
2804 ll_ifma = if_findmulti(ifp, llsa);
2805 if (ll_ifma == NULL) {
2806 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2807 if (ll_ifma == NULL) {
2808 --ifma->ifma_refcount;
2809 if_freemulti(ifma);
2810 error = ENOMEM;
2811 goto free_llsa_out;
2812 }
2813 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2814 ifma_link);
2815 } else
2816 ll_ifma->ifma_refcount++;
2817 ifma->ifma_llifma = ll_ifma;
2818 }
2819
2820 /*
2821 * We now have a new multicast address, ifma, and possibly a new or
2822 * referenced link layer address. Add the primary address to the
2823 * ifnet address list.
2824 */
2825 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2826
2827 if (retifma != NULL)
2828 *retifma = ifma;
2829
2830 /*
2831 * Must generate the message while holding the lock so that 'ifma'
2832 * pointer is still valid.
2833 */
2834 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2835 IF_ADDR_UNLOCK(ifp);
2836
2837 /*
2838 * We are certain we have added something, so call down to the
2839 * interface to let them know about it.
2840 */
2841 if (ifp->if_ioctl != NULL) {
2842 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2843 }
2844
2845 if (llsa != NULL)
2846 free(llsa, M_IFMADDR);
2847
2848 return (0);
2849
2850free_llsa_out:
2851 if (llsa != NULL)
2852 free(llsa, M_IFMADDR);
2853
2854unlock_out:
2855 IF_ADDR_UNLOCK(ifp);
2856 return (error);
2857}
2858
2859/*
2860 * Delete a multicast group membership by network-layer group address.
2861 *
2862 * Returns ENOENT if the entry could not be found. If ifp no longer
2863 * exists, results are undefined. This entry point should only be used
2864 * from subsystems which do appropriate locking to hold ifp for the
2865 * duration of the call.
2866 * Network-layer protocol domains must use if_delmulti_ifma().
2867 */
2868int
2869if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2870{
2871 struct ifmultiaddr *ifma;
2872 int lastref;
2873#ifdef INVARIANTS
2874 struct ifnet *oifp;
2875 INIT_VNET_NET(ifp->if_vnet);
2876
2877 IFNET_RLOCK();
2878 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2879 if (ifp == oifp)
2880 break;
2881 if (ifp != oifp)
2882 ifp = NULL;
2883 IFNET_RUNLOCK();
2884
2885 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2886#endif
2887 if (ifp == NULL)
2888 return (ENOENT);
2889
2890 IF_ADDR_LOCK(ifp);
2891 lastref = 0;
2892 ifma = if_findmulti(ifp, sa);
2893 if (ifma != NULL)
2894 lastref = if_delmulti_locked(ifp, ifma, 0);
2895 IF_ADDR_UNLOCK(ifp);
2896
2897 if (ifma == NULL)
2898 return (ENOENT);
2899
2900 if (lastref && ifp->if_ioctl != NULL) {
2901 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2902 }
2903
2904 return (0);
2905}
2906
2907/*
2908 * Delete a multicast group membership by group membership pointer.
2909 * Network-layer protocol domains must use this routine.
2910 *
2911 * It is safe to call this routine if the ifp disappeared.
2912 */
2913void
2914if_delmulti_ifma(struct ifmultiaddr *ifma)
2915{
2916#ifdef DIAGNOSTIC
2917 INIT_VNET_NET(curvnet);
2918#endif
2919 struct ifnet *ifp;
2920 int lastref;
2921
2922 ifp = ifma->ifma_ifp;
2923#ifdef DIAGNOSTIC
2924 if (ifp == NULL) {
2925 printf("%s: ifma_ifp seems to be detached\n", __func__);
2926 } else {
2927 struct ifnet *oifp;
2928
2929 IFNET_RLOCK();
2930 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2931 if (ifp == oifp)
2932 break;
2933 if (ifp != oifp) {
2934 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2935 ifp = NULL;
2936 }
2937 IFNET_RUNLOCK();
2938 }
2939#endif
2940 /*
2941 * If and only if the ifnet instance exists: Acquire the address lock.
2942 */
2943 if (ifp != NULL)
2944 IF_ADDR_LOCK(ifp);
2945
2946 lastref = if_delmulti_locked(ifp, ifma, 0);
2947
2948 if (ifp != NULL) {
2949 /*
2950 * If and only if the ifnet instance exists:
2951 * Release the address lock.
2952 * If the group was left: update the hardware hash filter.
2953 */
2954 IF_ADDR_UNLOCK(ifp);
2955 if (lastref && ifp->if_ioctl != NULL) {
2956 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2957 }
2958 }
2959}
2960
2961/*
2962 * Perform deletion of network-layer and/or link-layer multicast address.
2963 *
2964 * Return 0 if the reference count was decremented.
2965 * Return 1 if the final reference was released, indicating that the
2966 * hardware hash filter should be reprogrammed.
2967 */
2968static int
2969if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2970{
2971 struct ifmultiaddr *ll_ifma;
2972
2973 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2974 KASSERT(ifma->ifma_ifp == ifp,
2975 ("%s: inconsistent ifp %p", __func__, ifp));
2976 IF_ADDR_LOCK_ASSERT(ifp);
2977 }
2978
2979 ifp = ifma->ifma_ifp;
2980
2981 /*
2982 * If the ifnet is detaching, null out references to ifnet,
2983 * so that upper protocol layers will notice, and not attempt
2984 * to obtain locks for an ifnet which no longer exists. The
2985 * routing socket announcement must happen before the ifnet
2986 * instance is detached from the system.
2987 */
2988 if (detaching) {
2989#ifdef DIAGNOSTIC
2990 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2991#endif
2992 /*
2993 * ifp may already be nulled out if we are being reentered
2994 * to delete the ll_ifma.
2995 */
2996 if (ifp != NULL) {
2997 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2998 ifma->ifma_ifp = NULL;
2999 }
3000 }
3001
3002 if (--ifma->ifma_refcount > 0)
3003 return 0;
3004
3005 /*
3006 * If this ifma is a network-layer ifma, a link-layer ifma may
3007 * have been associated with it. Release it first if so.
3008 */
3009 ll_ifma = ifma->ifma_llifma;
3010 if (ll_ifma != NULL) {
3011 KASSERT(ifma->ifma_lladdr != NULL,
3012 ("%s: llifma w/o lladdr", __func__));
3013 if (detaching)
3014 ll_ifma->ifma_ifp = NULL; /* XXX */
3015 if (--ll_ifma->ifma_refcount == 0) {
3016 if (ifp != NULL) {
3017 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3018 ifma_link);
3019 }
3020 if_freemulti(ll_ifma);
3021 }
3022 }
3023
3024 if (ifp != NULL)
3025 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3026
3027 if_freemulti(ifma);
3028
3029 /*
3030 * The last reference to this instance of struct ifmultiaddr
3031 * was released; the hardware should be notified of this change.
3032 */
3033 return 1;
3034}
3035
3036/*
3037 * Set the link layer address on an interface.
3038 *
3039 * At this time we only support certain types of interfaces,
3040 * and we don't allow the length of the address to change.
3041 */
3042int
3043if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3044{
3045 struct sockaddr_dl *sdl;
3046 struct ifaddr *ifa;
3047 struct ifreq ifr;
3048
3049 ifa = ifp->if_addr;
3050 if (ifa == NULL)
3051 return (EINVAL);
3052 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3053 if (sdl == NULL)
3054 return (EINVAL);
3055 if (len != sdl->sdl_alen) /* don't allow length to change */
3056 return (EINVAL);
3057 switch (ifp->if_type) {
3058 case IFT_ETHER:
3059 case IFT_FDDI:
3060 case IFT_XETHER:
3061 case IFT_ISO88025:
3062 case IFT_L2VLAN:
3063 case IFT_BRIDGE:
3064 case IFT_ARCNET:
3065 case IFT_IEEE8023ADLAG:
3066 case IFT_IEEE80211:
3067 bcopy(lladdr, LLADDR(sdl), len);
3068 break;
3069 default:
3070 return (ENODEV);
3071 }
3072 /*
3073 * If the interface is already up, we need
3074 * to re-init it in order to reprogram its
3075 * address filter.
3076 */
3077 if ((ifp->if_flags & IFF_UP) != 0) {
3078 if (ifp->if_ioctl) {
3079 ifp->if_flags &= ~IFF_UP;
3080 ifr.ifr_flags = ifp->if_flags & 0xffff;
3081 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3082 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3083 ifp->if_flags |= IFF_UP;
3084 ifr.ifr_flags = ifp->if_flags & 0xffff;
3085 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3086 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3087 }
3088#ifdef INET
3089 /*
3090 * Also send gratuitous ARPs to notify other nodes about
3091 * the address change.
3092 */
3093 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3094 if (ifa->ifa_addr->sa_family == AF_INET)
3095 arp_ifinit(ifp, ifa);
3096 }
3097#endif
3098 }
3099 return (0);
3100}
3101
3102/*
3103 * The name argument must be a pointer to storage which will last as
3104 * long as the interface does. For physical devices, the result of
3105 * device_get_name(dev) is a good choice and for pseudo-devices a
3106 * static string works well.
3107 */
3108void
3109if_initname(struct ifnet *ifp, const char *name, int unit)
3110{
3111 ifp->if_dname = name;
3112 ifp->if_dunit = unit;
3113 if (unit != IF_DUNIT_NONE)
3114 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3115 else
3116 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3117}
3118
3119int
3120if_printf(struct ifnet *ifp, const char * fmt, ...)
3121{
3122 va_list ap;
3123 int retval;
3124
3125 retval = printf("%s: ", ifp->if_xname);
3126 va_start(ap, fmt);
3127 retval += vprintf(fmt, ap);
3128 va_end(ap);
3129 return (retval);
3130}
3131
3132void
3133if_start(struct ifnet *ifp)
3134{
3135
3136 (*(ifp)->if_start)(ifp);
3137}
3138
3139/*
3140 * Backwards compatibility interface for drivers
3141 * that have not implemented it
3142 */
3143static int
3144if_transmit(struct ifnet *ifp, struct mbuf *m)
3145{
3146 int error;
3147
3148 IFQ_HANDOFF(ifp, m, error);
3149 return (error);
3150}
3151
3152int
3153if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3154{
3155 int active = 0;
3156
3157 IF_LOCK(ifq);
3158 if (_IF_QFULL(ifq)) {
3159 _IF_DROP(ifq);
3160 IF_UNLOCK(ifq);
3161 m_freem(m);
3162 return (0);
3163 }
3164 if (ifp != NULL) {
3165 ifp->if_obytes += m->m_pkthdr.len + adjust;
3166 if (m->m_flags & (M_BCAST|M_MCAST))
3167 ifp->if_omcasts++;
3168 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3169 }
3170 _IF_ENQUEUE(ifq, m);
3171 IF_UNLOCK(ifq);
3172 if (ifp != NULL && !active)
3173 (*(ifp)->if_start)(ifp);
3174 return (1);
3175}
3176
3177void
3178if_register_com_alloc(u_char type,
3179 if_com_alloc_t *a, if_com_free_t *f)
3180{
3181
3182 KASSERT(if_com_alloc[type] == NULL,
3183 ("if_register_com_alloc: %d already registered", type));
3184 KASSERT(if_com_free[type] == NULL,
3185 ("if_register_com_alloc: %d free already registered", type));
3186
3187 if_com_alloc[type] = a;
3188 if_com_free[type] = f;
3189}
3190
3191void
3192if_deregister_com_alloc(u_char type)
3193{
3194
3195 KASSERT(if_com_alloc[type] != NULL,
3196 ("if_deregister_com_alloc: %d not registered", type));
3197 KASSERT(if_com_free[type] != NULL,
3198 ("if_deregister_com_alloc: %d free not registered", type));
3199 if_com_alloc[type] = NULL;
3200 if_com_free[type] = NULL;
3201}
1006 continue;
1007 RADIX_NODE_HEAD_LOCK(rnh);
1008 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
1009 RADIX_NODE_HEAD_UNLOCK(rnh);
1010 }
1011 }
1012
1013 /* Announce that the interface is gone. */
1014 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1015 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1016 if (IS_DEFAULT_VNET(curvnet))
1017 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1018 if_delgroups(ifp);
1019
1020 IF_AFDATA_LOCK(ifp);
1021 for (dp = domains; dp; dp = dp->dom_next) {
1022 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1023 (*dp->dom_ifdetach)(ifp,
1024 ifp->if_afdata[dp->dom_family]);
1025 }
1026 ifp->if_afdata_initialized = 0;
1027 IF_AFDATA_UNLOCK(ifp);
1028
1029 if (!vmove)
1030 ifq_detach(&ifp->if_snd);
1031}
1032
1033#ifdef VIMAGE
1034/*
1035 * if_vmove() performs a limited version of if_detach() in current
1036 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1037 * An attempt is made to shrink if_index in current vnet, find an
1038 * unused if_index in target vnet and calls if_grow() if necessary,
1039 * and finally find an unused if_xname for the target vnet.
1040 */
1041void
1042if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1043{
1044
1045 /*
1046 * Detach from current vnet, but preserve LLADDR info, do not
1047 * mark as dead etc. so that the ifnet can be reattached later.
1048 */
1049 if_detach_internal(ifp, 1);
1050
1051 /*
1052 * Unlink the ifnet from ifindex_table[] in current vnet,
1053 * and shrink the if_index for that vnet if possible.
1054 * do / while construct below is needed to confine the scope
1055 * of INIT_VNET_NET().
1056 */
1057 {
1058 INIT_VNET_NET(curvnet);
1059
1060 IFNET_WLOCK();
1061 ifnet_setbyindex(ifp->if_index, NULL);
1062 while (V_if_index > 0 && \
1063 ifnet_byindex_locked(V_if_index) == NULL)
1064 V_if_index--;
1065 IFNET_WUNLOCK();
1066 };
1067
1068 /*
1069 * Switch to the context of the target vnet.
1070 */
1071 CURVNET_SET_QUIET(new_vnet);
1072 INIT_VNET_NET(new_vnet);
1073
1074 /*
1075 * Try to find an empty slot below if_index. If we fail, take
1076 * the next slot.
1077 */
1078 IFNET_WLOCK();
1079 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) {
1080 if (ifnet_byindex_locked(ifp->if_index) == NULL)
1081 break;
1082 }
1083 /* Catch if_index overflow. */
1084 if (ifp->if_index < 1)
1085 panic("if_index overflow");
1086
1087 if (ifp->if_index > V_if_index)
1088 V_if_index = ifp->if_index;
1089 if (V_if_index >= V_if_indexlim)
1090 if_grow();
1091 ifnet_setbyindex(ifp->if_index, ifp);
1092 IFNET_WUNLOCK();
1093
1094 if_attach_internal(ifp, 1);
1095
1096 CURVNET_RESTORE();
1097}
1098#endif /* VIMAGE */
1099
1100/*
1101 * Add a group to an interface
1102 */
1103int
1104if_addgroup(struct ifnet *ifp, const char *groupname)
1105{
1106 INIT_VNET_NET(ifp->if_vnet);
1107 struct ifg_list *ifgl;
1108 struct ifg_group *ifg = NULL;
1109 struct ifg_member *ifgm;
1110
1111 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1112 groupname[strlen(groupname) - 1] <= '9')
1113 return (EINVAL);
1114
1115 IFNET_WLOCK();
1116 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1117 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1118 IFNET_WUNLOCK();
1119 return (EEXIST);
1120 }
1121
1122 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1123 M_NOWAIT)) == NULL) {
1124 IFNET_WUNLOCK();
1125 return (ENOMEM);
1126 }
1127
1128 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1129 M_TEMP, M_NOWAIT)) == NULL) {
1130 free(ifgl, M_TEMP);
1131 IFNET_WUNLOCK();
1132 return (ENOMEM);
1133 }
1134
1135 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1136 if (!strcmp(ifg->ifg_group, groupname))
1137 break;
1138
1139 if (ifg == NULL) {
1140 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1141 M_TEMP, M_NOWAIT)) == NULL) {
1142 free(ifgl, M_TEMP);
1143 free(ifgm, M_TEMP);
1144 IFNET_WUNLOCK();
1145 return (ENOMEM);
1146 }
1147 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1148 ifg->ifg_refcnt = 0;
1149 TAILQ_INIT(&ifg->ifg_members);
1150 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1151 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1152 }
1153
1154 ifg->ifg_refcnt++;
1155 ifgl->ifgl_group = ifg;
1156 ifgm->ifgm_ifp = ifp;
1157
1158 IF_ADDR_LOCK(ifp);
1159 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1160 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1161 IF_ADDR_UNLOCK(ifp);
1162
1163 IFNET_WUNLOCK();
1164
1165 EVENTHANDLER_INVOKE(group_change_event, groupname);
1166
1167 return (0);
1168}
1169
1170/*
1171 * Remove a group from an interface
1172 */
1173int
1174if_delgroup(struct ifnet *ifp, const char *groupname)
1175{
1176 INIT_VNET_NET(ifp->if_vnet);
1177 struct ifg_list *ifgl;
1178 struct ifg_member *ifgm;
1179
1180 IFNET_WLOCK();
1181 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1182 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1183 break;
1184 if (ifgl == NULL) {
1185 IFNET_WUNLOCK();
1186 return (ENOENT);
1187 }
1188
1189 IF_ADDR_LOCK(ifp);
1190 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1191 IF_ADDR_UNLOCK(ifp);
1192
1193 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1194 if (ifgm->ifgm_ifp == ifp)
1195 break;
1196
1197 if (ifgm != NULL) {
1198 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1199 free(ifgm, M_TEMP);
1200 }
1201
1202 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1203 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1204 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1205 free(ifgl->ifgl_group, M_TEMP);
1206 }
1207 IFNET_WUNLOCK();
1208
1209 free(ifgl, M_TEMP);
1210
1211 EVENTHANDLER_INVOKE(group_change_event, groupname);
1212
1213 return (0);
1214}
1215
1216/*
1217 * Remove an interface from all groups
1218 */
1219static void
1220if_delgroups(struct ifnet *ifp)
1221{
1222 INIT_VNET_NET(ifp->if_vnet);
1223 struct ifg_list *ifgl;
1224 struct ifg_member *ifgm;
1225 char groupname[IFNAMSIZ];
1226
1227 IFNET_WLOCK();
1228 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1229 ifgl = TAILQ_FIRST(&ifp->if_groups);
1230
1231 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1232
1233 IF_ADDR_LOCK(ifp);
1234 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1235 IF_ADDR_UNLOCK(ifp);
1236
1237 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1238 if (ifgm->ifgm_ifp == ifp)
1239 break;
1240
1241 if (ifgm != NULL) {
1242 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1243 ifgm_next);
1244 free(ifgm, M_TEMP);
1245 }
1246
1247 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1248 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1249 EVENTHANDLER_INVOKE(group_detach_event,
1250 ifgl->ifgl_group);
1251 free(ifgl->ifgl_group, M_TEMP);
1252 }
1253 IFNET_WUNLOCK();
1254
1255 free(ifgl, M_TEMP);
1256
1257 EVENTHANDLER_INVOKE(group_change_event, groupname);
1258
1259 IFNET_WLOCK();
1260 }
1261 IFNET_WUNLOCK();
1262}
1263
1264/*
1265 * Stores all groups from an interface in memory pointed
1266 * to by data
1267 */
1268static int
1269if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1270{
1271 int len, error;
1272 struct ifg_list *ifgl;
1273 struct ifg_req ifgrq, *ifgp;
1274 struct ifgroupreq *ifgr = data;
1275
1276 if (ifgr->ifgr_len == 0) {
1277 IF_ADDR_LOCK(ifp);
1278 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1279 ifgr->ifgr_len += sizeof(struct ifg_req);
1280 IF_ADDR_UNLOCK(ifp);
1281 return (0);
1282 }
1283
1284 len = ifgr->ifgr_len;
1285 ifgp = ifgr->ifgr_groups;
1286 /* XXX: wire */
1287 IF_ADDR_LOCK(ifp);
1288 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1289 if (len < sizeof(ifgrq)) {
1290 IF_ADDR_UNLOCK(ifp);
1291 return (EINVAL);
1292 }
1293 bzero(&ifgrq, sizeof ifgrq);
1294 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1295 sizeof(ifgrq.ifgrq_group));
1296 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1297 IF_ADDR_UNLOCK(ifp);
1298 return (error);
1299 }
1300 len -= sizeof(ifgrq);
1301 ifgp++;
1302 }
1303 IF_ADDR_UNLOCK(ifp);
1304
1305 return (0);
1306}
1307
1308/*
1309 * Stores all members of a group in memory pointed to by data
1310 */
1311static int
1312if_getgroupmembers(struct ifgroupreq *data)
1313{
1314 INIT_VNET_NET(curvnet);
1315 struct ifgroupreq *ifgr = data;
1316 struct ifg_group *ifg;
1317 struct ifg_member *ifgm;
1318 struct ifg_req ifgrq, *ifgp;
1319 int len, error;
1320
1321 IFNET_RLOCK();
1322 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1323 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1324 break;
1325 if (ifg == NULL) {
1326 IFNET_RUNLOCK();
1327 return (ENOENT);
1328 }
1329
1330 if (ifgr->ifgr_len == 0) {
1331 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1332 ifgr->ifgr_len += sizeof(ifgrq);
1333 IFNET_RUNLOCK();
1334 return (0);
1335 }
1336
1337 len = ifgr->ifgr_len;
1338 ifgp = ifgr->ifgr_groups;
1339 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1340 if (len < sizeof(ifgrq)) {
1341 IFNET_RUNLOCK();
1342 return (EINVAL);
1343 }
1344 bzero(&ifgrq, sizeof ifgrq);
1345 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1346 sizeof(ifgrq.ifgrq_member));
1347 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1348 IFNET_RUNLOCK();
1349 return (error);
1350 }
1351 len -= sizeof(ifgrq);
1352 ifgp++;
1353 }
1354 IFNET_RUNLOCK();
1355
1356 return (0);
1357}
1358
1359/*
1360 * Delete Routes for a Network Interface
1361 *
1362 * Called for each routing entry via the rnh->rnh_walktree() call above
1363 * to delete all route entries referencing a detaching network interface.
1364 *
1365 * Arguments:
1366 * rn pointer to node in the routing table
1367 * arg argument passed to rnh->rnh_walktree() - detaching interface
1368 *
1369 * Returns:
1370 * 0 successful
1371 * errno failed - reason indicated
1372 *
1373 */
1374static int
1375if_rtdel(struct radix_node *rn, void *arg)
1376{
1377 struct rtentry *rt = (struct rtentry *)rn;
1378 struct ifnet *ifp = arg;
1379 int err;
1380
1381 if (rt->rt_ifp == ifp) {
1382
1383 /*
1384 * Protect (sorta) against walktree recursion problems
1385 * with cloned routes
1386 */
1387 if ((rt->rt_flags & RTF_UP) == 0)
1388 return (0);
1389
1390 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1391 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED,
1392 (struct rtentry **) NULL, rt->rt_fibnum);
1393 if (err) {
1394 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1395 }
1396 }
1397
1398 return (0);
1399}
1400
1401/*
1402 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1403 * structs used to represent other address families, it is necessary
1404 * to perform a different comparison.
1405 */
1406
1407#define sa_equal(a1, a2) \
1408 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1409
1410#define sa_dl_equal(a1, a2) \
1411 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1412 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1413 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1414 LLADDR((struct sockaddr_dl *)(a2)), \
1415 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1416
1417/*
1418 * Locate an interface based on a complete address.
1419 */
1420/*ARGSUSED*/
1421struct ifaddr *
1422ifa_ifwithaddr(struct sockaddr *addr)
1423{
1424 INIT_VNET_NET(curvnet);
1425 struct ifnet *ifp;
1426 struct ifaddr *ifa;
1427
1428 IFNET_RLOCK();
1429 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1430 IF_ADDR_LOCK(ifp);
1431 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1432 if (ifa->ifa_addr->sa_family != addr->sa_family)
1433 continue;
1434 if (sa_equal(addr, ifa->ifa_addr)) {
1435 IF_ADDR_UNLOCK(ifp);
1436 goto done;
1437 }
1438 /* IP6 doesn't have broadcast */
1439 if ((ifp->if_flags & IFF_BROADCAST) &&
1440 ifa->ifa_broadaddr &&
1441 ifa->ifa_broadaddr->sa_len != 0 &&
1442 sa_equal(ifa->ifa_broadaddr, addr)) {
1443 IF_ADDR_UNLOCK(ifp);
1444 goto done;
1445 }
1446 }
1447 IF_ADDR_UNLOCK(ifp);
1448 }
1449 ifa = NULL;
1450done:
1451 IFNET_RUNLOCK();
1452 return (ifa);
1453}
1454
1455/*
1456 * Locate an interface based on the broadcast address.
1457 */
1458/* ARGSUSED */
1459struct ifaddr *
1460ifa_ifwithbroadaddr(struct sockaddr *addr)
1461{
1462 INIT_VNET_NET(curvnet);
1463 struct ifnet *ifp;
1464 struct ifaddr *ifa;
1465
1466 IFNET_RLOCK();
1467 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1468 IF_ADDR_LOCK(ifp);
1469 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1470 if (ifa->ifa_addr->sa_family != addr->sa_family)
1471 continue;
1472 if ((ifp->if_flags & IFF_BROADCAST) &&
1473 ifa->ifa_broadaddr &&
1474 ifa->ifa_broadaddr->sa_len != 0 &&
1475 sa_equal(ifa->ifa_broadaddr, addr)) {
1476 IF_ADDR_UNLOCK(ifp);
1477 goto done;
1478 }
1479 }
1480 IF_ADDR_UNLOCK(ifp);
1481 }
1482 ifa = NULL;
1483done:
1484 IFNET_RUNLOCK();
1485 return (ifa);
1486}
1487
1488/*
1489 * Locate the point to point interface with a given destination address.
1490 */
1491/*ARGSUSED*/
1492struct ifaddr *
1493ifa_ifwithdstaddr(struct sockaddr *addr)
1494{
1495 INIT_VNET_NET(curvnet);
1496 struct ifnet *ifp;
1497 struct ifaddr *ifa;
1498
1499 IFNET_RLOCK();
1500 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1501 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1502 continue;
1503 IF_ADDR_LOCK(ifp);
1504 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1505 if (ifa->ifa_addr->sa_family != addr->sa_family)
1506 continue;
1507 if (ifa->ifa_dstaddr != NULL &&
1508 sa_equal(addr, ifa->ifa_dstaddr)) {
1509 IF_ADDR_UNLOCK(ifp);
1510 goto done;
1511 }
1512 }
1513 IF_ADDR_UNLOCK(ifp);
1514 }
1515 ifa = NULL;
1516done:
1517 IFNET_RUNLOCK();
1518 return (ifa);
1519}
1520
1521/*
1522 * Find an interface on a specific network. If many, choice
1523 * is most specific found.
1524 */
1525struct ifaddr *
1526ifa_ifwithnet(struct sockaddr *addr)
1527{
1528 INIT_VNET_NET(curvnet);
1529 struct ifnet *ifp;
1530 struct ifaddr *ifa;
1531 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1532 u_int af = addr->sa_family;
1533 char *addr_data = addr->sa_data, *cplim;
1534
1535 /*
1536 * AF_LINK addresses can be looked up directly by their index number,
1537 * so do that if we can.
1538 */
1539 if (af == AF_LINK) {
1540 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1541 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1542 return (ifaddr_byindex(sdl->sdl_index));
1543 }
1544
1545 /*
1546 * Scan though each interface, looking for ones that have
1547 * addresses in this address family.
1548 */
1549 IFNET_RLOCK();
1550 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1551 IF_ADDR_LOCK(ifp);
1552 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1553 char *cp, *cp2, *cp3;
1554
1555 if (ifa->ifa_addr->sa_family != af)
1556next: continue;
1557 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1558 /*
1559 * This is a bit broken as it doesn't
1560 * take into account that the remote end may
1561 * be a single node in the network we are
1562 * looking for.
1563 * The trouble is that we don't know the
1564 * netmask for the remote end.
1565 */
1566 if (ifa->ifa_dstaddr != NULL &&
1567 sa_equal(addr, ifa->ifa_dstaddr)) {
1568 IF_ADDR_UNLOCK(ifp);
1569 goto done;
1570 }
1571 } else {
1572 /*
1573 * if we have a special address handler,
1574 * then use it instead of the generic one.
1575 */
1576 if (ifa->ifa_claim_addr) {
1577 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1578 IF_ADDR_UNLOCK(ifp);
1579 goto done;
1580 }
1581 continue;
1582 }
1583
1584 /*
1585 * Scan all the bits in the ifa's address.
1586 * If a bit dissagrees with what we are
1587 * looking for, mask it with the netmask
1588 * to see if it really matters.
1589 * (A byte at a time)
1590 */
1591 if (ifa->ifa_netmask == 0)
1592 continue;
1593 cp = addr_data;
1594 cp2 = ifa->ifa_addr->sa_data;
1595 cp3 = ifa->ifa_netmask->sa_data;
1596 cplim = ifa->ifa_netmask->sa_len
1597 + (char *)ifa->ifa_netmask;
1598 while (cp3 < cplim)
1599 if ((*cp++ ^ *cp2++) & *cp3++)
1600 goto next; /* next address! */
1601 /*
1602 * If the netmask of what we just found
1603 * is more specific than what we had before
1604 * (if we had one) then remember the new one
1605 * before continuing to search
1606 * for an even better one.
1607 */
1608 if (ifa_maybe == 0 ||
1609 rn_refines((caddr_t)ifa->ifa_netmask,
1610 (caddr_t)ifa_maybe->ifa_netmask))
1611 ifa_maybe = ifa;
1612 }
1613 }
1614 IF_ADDR_UNLOCK(ifp);
1615 }
1616 ifa = ifa_maybe;
1617done:
1618 IFNET_RUNLOCK();
1619 return (ifa);
1620}
1621
1622/*
1623 * Find an interface address specific to an interface best matching
1624 * a given address.
1625 */
1626struct ifaddr *
1627ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1628{
1629 struct ifaddr *ifa;
1630 char *cp, *cp2, *cp3;
1631 char *cplim;
1632 struct ifaddr *ifa_maybe = 0;
1633 u_int af = addr->sa_family;
1634
1635 if (af >= AF_MAX)
1636 return (0);
1637 IF_ADDR_LOCK(ifp);
1638 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1639 if (ifa->ifa_addr->sa_family != af)
1640 continue;
1641 if (ifa_maybe == 0)
1642 ifa_maybe = ifa;
1643 if (ifa->ifa_netmask == 0) {
1644 if (sa_equal(addr, ifa->ifa_addr) ||
1645 (ifa->ifa_dstaddr &&
1646 sa_equal(addr, ifa->ifa_dstaddr)))
1647 goto done;
1648 continue;
1649 }
1650 if (ifp->if_flags & IFF_POINTOPOINT) {
1651 if (sa_equal(addr, ifa->ifa_dstaddr))
1652 goto done;
1653 } else {
1654 cp = addr->sa_data;
1655 cp2 = ifa->ifa_addr->sa_data;
1656 cp3 = ifa->ifa_netmask->sa_data;
1657 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1658 for (; cp3 < cplim; cp3++)
1659 if ((*cp++ ^ *cp2++) & *cp3)
1660 break;
1661 if (cp3 == cplim)
1662 goto done;
1663 }
1664 }
1665 ifa = ifa_maybe;
1666done:
1667 IF_ADDR_UNLOCK(ifp);
1668 return (ifa);
1669}
1670
1671#include <net/route.h>
1672#include <net/if_llatbl.h>
1673
1674/*
1675 * Default action when installing a route with a Link Level gateway.
1676 * Lookup an appropriate real ifa to point to.
1677 * This should be moved to /sys/net/link.c eventually.
1678 */
1679static void
1680link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1681{
1682 struct ifaddr *ifa, *oifa;
1683 struct sockaddr *dst;
1684 struct ifnet *ifp;
1685
1686 RT_LOCK_ASSERT(rt);
1687
1688 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1689 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1690 return;
1691 ifa = ifaof_ifpforaddr(dst, ifp);
1692 if (ifa) {
1693 IFAREF(ifa); /* XXX */
1694 oifa = rt->rt_ifa;
1695 rt->rt_ifa = ifa;
1696 IFAFREE(oifa);
1697 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1698 ifa->ifa_rtrequest(cmd, rt, info);
1699 }
1700}
1701
1702/*
1703 * Mark an interface down and notify protocols of
1704 * the transition.
1705 * NOTE: must be called at splnet or eqivalent.
1706 */
1707static void
1708if_unroute(struct ifnet *ifp, int flag, int fam)
1709{
1710 struct ifaddr *ifa;
1711
1712 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1713
1714 ifp->if_flags &= ~flag;
1715 getmicrotime(&ifp->if_lastchange);
1716 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1717 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1718 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1719 ifp->if_qflush(ifp);
1720
1721#ifdef DEV_CARP
1722 if (ifp->if_carp)
1723 carp_carpdev_state(ifp->if_carp);
1724#endif
1725 rt_ifmsg(ifp);
1726}
1727
1728/*
1729 * Mark an interface up and notify protocols of
1730 * the transition.
1731 * NOTE: must be called at splnet or eqivalent.
1732 */
1733static void
1734if_route(struct ifnet *ifp, int flag, int fam)
1735{
1736 struct ifaddr *ifa;
1737
1738 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1739
1740 ifp->if_flags |= flag;
1741 getmicrotime(&ifp->if_lastchange);
1742 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1743 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1744 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1745#ifdef DEV_CARP
1746 if (ifp->if_carp)
1747 carp_carpdev_state(ifp->if_carp);
1748#endif
1749 rt_ifmsg(ifp);
1750#ifdef INET6
1751 in6_if_up(ifp);
1752#endif
1753}
1754
1755void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1756void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1757
1758/*
1759 * Handle a change in the interface link state. To avoid LORs
1760 * between driver lock and upper layer locks, as well as possible
1761 * recursions, we post event to taskqueue, and all job
1762 * is done in static do_link_state_change().
1763 */
1764void
1765if_link_state_change(struct ifnet *ifp, int link_state)
1766{
1767 /* Return if state hasn't changed. */
1768 if (ifp->if_link_state == link_state)
1769 return;
1770
1771 ifp->if_link_state = link_state;
1772
1773 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1774}
1775
1776static void
1777do_link_state_change(void *arg, int pending)
1778{
1779 struct ifnet *ifp = (struct ifnet *)arg;
1780 int link_state = ifp->if_link_state;
1781 int link;
1782 CURVNET_SET(ifp->if_vnet);
1783
1784 /* Notify that the link state has changed. */
1785 rt_ifmsg(ifp);
1786 if (link_state == LINK_STATE_UP)
1787 link = NOTE_LINKUP;
1788 else if (link_state == LINK_STATE_DOWN)
1789 link = NOTE_LINKDOWN;
1790 else
1791 link = NOTE_LINKINV;
1792 KNOTE_UNLOCKED(&ifp->if_klist, link);
1793 if (ifp->if_vlantrunk != NULL)
1794 (*vlan_link_state_p)(ifp, link);
1795
1796 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1797 IFP2AC(ifp)->ac_netgraph != NULL)
1798 (*ng_ether_link_state_p)(ifp, link_state);
1799#ifdef DEV_CARP
1800 if (ifp->if_carp)
1801 carp_carpdev_state(ifp->if_carp);
1802#endif
1803 if (ifp->if_bridge) {
1804 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1805 (*bstp_linkstate_p)(ifp, link_state);
1806 }
1807 if (ifp->if_lagg) {
1808 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1809 (*lagg_linkstate_p)(ifp, link_state);
1810 }
1811
1812 if (IS_DEFAULT_VNET(curvnet))
1813 devctl_notify("IFNET", ifp->if_xname,
1814 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1815 NULL);
1816 if (pending > 1)
1817 if_printf(ifp, "%d link states coalesced\n", pending);
1818 if (log_link_state_change)
1819 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1820 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1821 CURVNET_RESTORE();
1822}
1823
1824/*
1825 * Mark an interface down and notify protocols of
1826 * the transition.
1827 * NOTE: must be called at splnet or eqivalent.
1828 */
1829void
1830if_down(struct ifnet *ifp)
1831{
1832
1833 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1834}
1835
1836/*
1837 * Mark an interface up and notify protocols of
1838 * the transition.
1839 * NOTE: must be called at splnet or eqivalent.
1840 */
1841void
1842if_up(struct ifnet *ifp)
1843{
1844
1845 if_route(ifp, IFF_UP, AF_UNSPEC);
1846}
1847
1848/*
1849 * Flush an interface queue.
1850 */
1851void
1852if_qflush(struct ifnet *ifp)
1853{
1854 struct mbuf *m, *n;
1855 struct ifaltq *ifq;
1856
1857 ifq = &ifp->if_snd;
1858 IFQ_LOCK(ifq);
1859#ifdef ALTQ
1860 if (ALTQ_IS_ENABLED(ifq))
1861 ALTQ_PURGE(ifq);
1862#endif
1863 n = ifq->ifq_head;
1864 while ((m = n) != 0) {
1865 n = m->m_act;
1866 m_freem(m);
1867 }
1868 ifq->ifq_head = 0;
1869 ifq->ifq_tail = 0;
1870 ifq->ifq_len = 0;
1871 IFQ_UNLOCK(ifq);
1872}
1873
1874/*
1875 * Handle interface watchdog timer routines. Called
1876 * from softclock, we decrement timers (if set) and
1877 * call the appropriate interface routine on expiration.
1878 *
1879 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1880 * holding Giant.
1881 */
1882static void
1883if_slowtimo(void *arg)
1884{
1885 VNET_ITERATOR_DECL(vnet_iter);
1886 struct ifnet *ifp;
1887 int s = splimp();
1888
1889 IFNET_RLOCK();
1890 VNET_LIST_RLOCK();
1891 VNET_FOREACH(vnet_iter) {
1892 CURVNET_SET(vnet_iter);
1893 INIT_VNET_NET(vnet_iter);
1894 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1895 if (ifp->if_timer == 0 || --ifp->if_timer)
1896 continue;
1897 if (ifp->if_watchdog)
1898 (*ifp->if_watchdog)(ifp);
1899 }
1900 CURVNET_RESTORE();
1901 }
1902 VNET_LIST_RUNLOCK();
1903 IFNET_RUNLOCK();
1904 splx(s);
1905 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1906}
1907
1908/*
1909 * Map interface name to interface structure pointer, with or without
1910 * returning a reference.
1911 */
1912struct ifnet *
1913ifunit_ref(const char *name)
1914{
1915 INIT_VNET_NET(curvnet);
1916 struct ifnet *ifp;
1917
1918 IFNET_RLOCK();
1919 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1920 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
1921 !(ifp->if_flags & IFF_DYING))
1922 break;
1923 }
1924 if (ifp != NULL)
1925 if_ref(ifp);
1926 IFNET_RUNLOCK();
1927 return (ifp);
1928}
1929
1930struct ifnet *
1931ifunit(const char *name)
1932{
1933 INIT_VNET_NET(curvnet);
1934 struct ifnet *ifp;
1935
1936 IFNET_RLOCK();
1937 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1938 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1939 break;
1940 }
1941 IFNET_RUNLOCK();
1942 return (ifp);
1943}
1944
1945/*
1946 * Hardware specific interface ioctls.
1947 */
1948static int
1949ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1950{
1951 struct ifreq *ifr;
1952 struct ifstat *ifs;
1953 int error = 0;
1954 int new_flags, temp_flags;
1955 size_t namelen, onamelen;
1956 char new_name[IFNAMSIZ];
1957 struct ifaddr *ifa;
1958 struct sockaddr_dl *sdl;
1959
1960 ifr = (struct ifreq *)data;
1961 switch (cmd) {
1962 case SIOCGIFINDEX:
1963 ifr->ifr_index = ifp->if_index;
1964 break;
1965
1966 case SIOCGIFFLAGS:
1967 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1968 ifr->ifr_flags = temp_flags & 0xffff;
1969 ifr->ifr_flagshigh = temp_flags >> 16;
1970 break;
1971
1972 case SIOCGIFCAP:
1973 ifr->ifr_reqcap = ifp->if_capabilities;
1974 ifr->ifr_curcap = ifp->if_capenable;
1975 break;
1976
1977#ifdef MAC
1978 case SIOCGIFMAC:
1979 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
1980 break;
1981#endif
1982
1983 case SIOCGIFMETRIC:
1984 ifr->ifr_metric = ifp->if_metric;
1985 break;
1986
1987 case SIOCGIFMTU:
1988 ifr->ifr_mtu = ifp->if_mtu;
1989 break;
1990
1991 case SIOCGIFPHYS:
1992 ifr->ifr_phys = ifp->if_physical;
1993 break;
1994
1995 case SIOCSIFFLAGS:
1996 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1997 if (error)
1998 return (error);
1999 /*
2000 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2001 * check, so we don't need special handling here yet.
2002 */
2003 new_flags = (ifr->ifr_flags & 0xffff) |
2004 (ifr->ifr_flagshigh << 16);
2005 if (ifp->if_flags & IFF_SMART) {
2006 /* Smart drivers twiddle their own routes */
2007 } else if (ifp->if_flags & IFF_UP &&
2008 (new_flags & IFF_UP) == 0) {
2009 int s = splimp();
2010 if_down(ifp);
2011 splx(s);
2012 } else if (new_flags & IFF_UP &&
2013 (ifp->if_flags & IFF_UP) == 0) {
2014 int s = splimp();
2015 if_up(ifp);
2016 splx(s);
2017 }
2018 /* See if permanently promiscuous mode bit is about to flip */
2019 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2020 if (new_flags & IFF_PPROMISC)
2021 ifp->if_flags |= IFF_PROMISC;
2022 else if (ifp->if_pcount == 0)
2023 ifp->if_flags &= ~IFF_PROMISC;
2024 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2025 ifp->if_xname,
2026 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2027 }
2028 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2029 (new_flags &~ IFF_CANTCHANGE);
2030 if (ifp->if_ioctl) {
2031 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2032 }
2033 getmicrotime(&ifp->if_lastchange);
2034 break;
2035
2036 case SIOCSIFCAP:
2037 error = priv_check(td, PRIV_NET_SETIFCAP);
2038 if (error)
2039 return (error);
2040 if (ifp->if_ioctl == NULL)
2041 return (EOPNOTSUPP);
2042 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2043 return (EINVAL);
2044 error = (*ifp->if_ioctl)(ifp, cmd, data);
2045 if (error == 0)
2046 getmicrotime(&ifp->if_lastchange);
2047 break;
2048
2049#ifdef MAC
2050 case SIOCSIFMAC:
2051 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2052 break;
2053#endif
2054
2055 case SIOCSIFNAME:
2056 error = priv_check(td, PRIV_NET_SETIFNAME);
2057 if (error)
2058 return (error);
2059 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2060 if (error != 0)
2061 return (error);
2062 if (new_name[0] == '\0')
2063 return (EINVAL);
2064 if (ifunit(new_name) != NULL)
2065 return (EEXIST);
2066
2067 /* Announce the departure of the interface. */
2068 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2069 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2070
2071 log(LOG_INFO, "%s: changing name to '%s'\n",
2072 ifp->if_xname, new_name);
2073
2074 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2075 ifa = ifp->if_addr;
2076 IFA_LOCK(ifa);
2077 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2078 namelen = strlen(new_name);
2079 onamelen = sdl->sdl_nlen;
2080 /*
2081 * Move the address if needed. This is safe because we
2082 * allocate space for a name of length IFNAMSIZ when we
2083 * create this in if_attach().
2084 */
2085 if (namelen != onamelen) {
2086 bcopy(sdl->sdl_data + onamelen,
2087 sdl->sdl_data + namelen, sdl->sdl_alen);
2088 }
2089 bcopy(new_name, sdl->sdl_data, namelen);
2090 sdl->sdl_nlen = namelen;
2091 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2092 bzero(sdl->sdl_data, onamelen);
2093 while (namelen != 0)
2094 sdl->sdl_data[--namelen] = 0xff;
2095 IFA_UNLOCK(ifa);
2096
2097 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2098 /* Announce the return of the interface. */
2099 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2100 break;
2101
2102 case SIOCSIFMETRIC:
2103 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2104 if (error)
2105 return (error);
2106 ifp->if_metric = ifr->ifr_metric;
2107 getmicrotime(&ifp->if_lastchange);
2108 break;
2109
2110 case SIOCSIFPHYS:
2111 error = priv_check(td, PRIV_NET_SETIFPHYS);
2112 if (error)
2113 return (error);
2114 if (ifp->if_ioctl == NULL)
2115 return (EOPNOTSUPP);
2116 error = (*ifp->if_ioctl)(ifp, cmd, data);
2117 if (error == 0)
2118 getmicrotime(&ifp->if_lastchange);
2119 break;
2120
2121 case SIOCSIFMTU:
2122 {
2123 u_long oldmtu = ifp->if_mtu;
2124
2125 error = priv_check(td, PRIV_NET_SETIFMTU);
2126 if (error)
2127 return (error);
2128 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2129 return (EINVAL);
2130 if (ifp->if_ioctl == NULL)
2131 return (EOPNOTSUPP);
2132 error = (*ifp->if_ioctl)(ifp, cmd, data);
2133 if (error == 0) {
2134 getmicrotime(&ifp->if_lastchange);
2135 rt_ifmsg(ifp);
2136 }
2137 /*
2138 * If the link MTU changed, do network layer specific procedure.
2139 */
2140 if (ifp->if_mtu != oldmtu) {
2141#ifdef INET6
2142 nd6_setmtu(ifp);
2143#endif
2144 }
2145 break;
2146 }
2147
2148 case SIOCADDMULTI:
2149 case SIOCDELMULTI:
2150 if (cmd == SIOCADDMULTI)
2151 error = priv_check(td, PRIV_NET_ADDMULTI);
2152 else
2153 error = priv_check(td, PRIV_NET_DELMULTI);
2154 if (error)
2155 return (error);
2156
2157 /* Don't allow group membership on non-multicast interfaces. */
2158 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2159 return (EOPNOTSUPP);
2160
2161 /* Don't let users screw up protocols' entries. */
2162 if (ifr->ifr_addr.sa_family != AF_LINK)
2163 return (EINVAL);
2164
2165 if (cmd == SIOCADDMULTI) {
2166 struct ifmultiaddr *ifma;
2167
2168 /*
2169 * Userland is only permitted to join groups once
2170 * via the if_addmulti() KPI, because it cannot hold
2171 * struct ifmultiaddr * between calls. It may also
2172 * lose a race while we check if the membership
2173 * already exists.
2174 */
2175 IF_ADDR_LOCK(ifp);
2176 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2177 IF_ADDR_UNLOCK(ifp);
2178 if (ifma != NULL)
2179 error = EADDRINUSE;
2180 else
2181 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2182 } else {
2183 error = if_delmulti(ifp, &ifr->ifr_addr);
2184 }
2185 if (error == 0)
2186 getmicrotime(&ifp->if_lastchange);
2187 break;
2188
2189 case SIOCSIFPHYADDR:
2190 case SIOCDIFPHYADDR:
2191#ifdef INET6
2192 case SIOCSIFPHYADDR_IN6:
2193#endif
2194 case SIOCSLIFPHYADDR:
2195 case SIOCSIFMEDIA:
2196 case SIOCSIFGENERIC:
2197 error = priv_check(td, PRIV_NET_HWIOCTL);
2198 if (error)
2199 return (error);
2200 if (ifp->if_ioctl == NULL)
2201 return (EOPNOTSUPP);
2202 error = (*ifp->if_ioctl)(ifp, cmd, data);
2203 if (error == 0)
2204 getmicrotime(&ifp->if_lastchange);
2205 break;
2206
2207 case SIOCGIFSTATUS:
2208 ifs = (struct ifstat *)data;
2209 ifs->ascii[0] = '\0';
2210
2211 case SIOCGIFPSRCADDR:
2212 case SIOCGIFPDSTADDR:
2213 case SIOCGLIFPHYADDR:
2214 case SIOCGIFMEDIA:
2215 case SIOCGIFGENERIC:
2216 if (ifp->if_ioctl == NULL)
2217 return (EOPNOTSUPP);
2218 error = (*ifp->if_ioctl)(ifp, cmd, data);
2219 break;
2220
2221 case SIOCSIFLLADDR:
2222 error = priv_check(td, PRIV_NET_SETLLADDR);
2223 if (error)
2224 return (error);
2225 error = if_setlladdr(ifp,
2226 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2227 break;
2228
2229 case SIOCAIFGROUP:
2230 {
2231 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2232
2233 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2234 if (error)
2235 return (error);
2236 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2237 return (error);
2238 break;
2239 }
2240
2241 case SIOCGIFGROUP:
2242 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2243 return (error);
2244 break;
2245
2246 case SIOCDIFGROUP:
2247 {
2248 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2249
2250 error = priv_check(td, PRIV_NET_DELIFGROUP);
2251 if (error)
2252 return (error);
2253 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2254 return (error);
2255 break;
2256 }
2257
2258 default:
2259 error = ENOIOCTL;
2260 break;
2261 }
2262 return (error);
2263}
2264
2265/*
2266 * Interface ioctls.
2267 */
2268int
2269ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2270{
2271 struct ifnet *ifp;
2272 struct ifreq *ifr;
2273 int error;
2274 int oif_flags;
2275
2276 switch (cmd) {
2277 case SIOCGIFCONF:
2278 case OSIOCGIFCONF:
2279#ifdef __amd64__
2280 case SIOCGIFCONF32:
2281#endif
2282 return (ifconf(cmd, data));
2283 }
2284 ifr = (struct ifreq *)data;
2285
2286 switch (cmd) {
2287#ifdef VIMAGE
2288 /*
2289 * XXX vnet creation will be implemented through the new jail
2290 * framework - this is just a temporary hack for testing the
2291 * vnet create / destroy mechanisms.
2292 */
2293 case SIOCSIFVIMAGE:
2294 error = vi_if_move((struct vi_req *) data, NULL,
2295 TD_TO_VIMAGE(td));
2296 return (error);
2297 case SIOCSPVIMAGE:
2298 case SIOCGPVIMAGE:
2299 error = vi_td_ioctl(cmd, (struct vi_req *) data, td);
2300 return (error);
2301#endif
2302 case SIOCIFCREATE:
2303 case SIOCIFCREATE2:
2304 error = priv_check(td, PRIV_NET_IFCREATE);
2305 if (error)
2306 return (error);
2307 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2308 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2309 case SIOCIFDESTROY:
2310 error = priv_check(td, PRIV_NET_IFDESTROY);
2311 if (error)
2312 return (error);
2313 return if_clone_destroy(ifr->ifr_name);
2314
2315 case SIOCIFGCLONERS:
2316 return (if_clone_list((struct if_clonereq *)data));
2317 case SIOCGIFGMEMB:
2318 return (if_getgroupmembers((struct ifgroupreq *)data));
2319 }
2320
2321 ifp = ifunit_ref(ifr->ifr_name);
2322 if (ifp == NULL)
2323 return (ENXIO);
2324
2325 error = ifhwioctl(cmd, ifp, data, td);
2326 if (error != ENOIOCTL) {
2327 if_rele(ifp);
2328 return (error);
2329 }
2330
2331 oif_flags = ifp->if_flags;
2332 if (so->so_proto == NULL) {
2333 if_rele(ifp);
2334 return (EOPNOTSUPP);
2335 }
2336#ifndef COMPAT_43
2337 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2338 data,
2339 ifp, td));
2340 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2341 error = (*ifp->if_ioctl)(ifp, cmd, data);
2342#else
2343 {
2344 int ocmd = cmd;
2345
2346 switch (cmd) {
2347
2348 case SIOCSIFDSTADDR:
2349 case SIOCSIFADDR:
2350 case SIOCSIFBRDADDR:
2351 case SIOCSIFNETMASK:
2352#if BYTE_ORDER != BIG_ENDIAN
2353 if (ifr->ifr_addr.sa_family == 0 &&
2354 ifr->ifr_addr.sa_len < 16) {
2355 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2356 ifr->ifr_addr.sa_len = 16;
2357 }
2358#else
2359 if (ifr->ifr_addr.sa_len == 0)
2360 ifr->ifr_addr.sa_len = 16;
2361#endif
2362 break;
2363
2364 case OSIOCGIFADDR:
2365 cmd = SIOCGIFADDR;
2366 break;
2367
2368 case OSIOCGIFDSTADDR:
2369 cmd = SIOCGIFDSTADDR;
2370 break;
2371
2372 case OSIOCGIFBRDADDR:
2373 cmd = SIOCGIFBRDADDR;
2374 break;
2375
2376 case OSIOCGIFNETMASK:
2377 cmd = SIOCGIFNETMASK;
2378 }
2379 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2380 cmd,
2381 data,
2382 ifp, td));
2383 if (error == EOPNOTSUPP && ifp != NULL &&
2384 ifp->if_ioctl != NULL)
2385 error = (*ifp->if_ioctl)(ifp, cmd, data);
2386 switch (ocmd) {
2387
2388 case OSIOCGIFADDR:
2389 case OSIOCGIFDSTADDR:
2390 case OSIOCGIFBRDADDR:
2391 case OSIOCGIFNETMASK:
2392 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2393
2394 }
2395 }
2396#endif /* COMPAT_43 */
2397
2398 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2399#ifdef INET6
2400 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2401 if (ifp->if_flags & IFF_UP) {
2402 int s = splimp();
2403 in6_if_up(ifp);
2404 splx(s);
2405 }
2406#endif
2407 }
2408 if_rele(ifp);
2409 return (error);
2410}
2411
2412/*
2413 * The code common to handling reference counted flags,
2414 * e.g., in ifpromisc() and if_allmulti().
2415 * The "pflag" argument can specify a permanent mode flag to check,
2416 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2417 *
2418 * Only to be used on stack-owned flags, not driver-owned flags.
2419 */
2420static int
2421if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2422{
2423 struct ifreq ifr;
2424 int error;
2425 int oldflags, oldcount;
2426
2427 /* Sanity checks to catch programming errors */
2428 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2429 ("%s: setting driver-owned flag %d", __func__, flag));
2430
2431 if (onswitch)
2432 KASSERT(*refcount >= 0,
2433 ("%s: increment negative refcount %d for flag %d",
2434 __func__, *refcount, flag));
2435 else
2436 KASSERT(*refcount > 0,
2437 ("%s: decrement non-positive refcount %d for flag %d",
2438 __func__, *refcount, flag));
2439
2440 /* In case this mode is permanent, just touch refcount */
2441 if (ifp->if_flags & pflag) {
2442 *refcount += onswitch ? 1 : -1;
2443 return (0);
2444 }
2445
2446 /* Save ifnet parameters for if_ioctl() may fail */
2447 oldcount = *refcount;
2448 oldflags = ifp->if_flags;
2449
2450 /*
2451 * See if we aren't the only and touching refcount is enough.
2452 * Actually toggle interface flag if we are the first or last.
2453 */
2454 if (onswitch) {
2455 if ((*refcount)++)
2456 return (0);
2457 ifp->if_flags |= flag;
2458 } else {
2459 if (--(*refcount))
2460 return (0);
2461 ifp->if_flags &= ~flag;
2462 }
2463
2464 /* Call down the driver since we've changed interface flags */
2465 if (ifp->if_ioctl == NULL) {
2466 error = EOPNOTSUPP;
2467 goto recover;
2468 }
2469 ifr.ifr_flags = ifp->if_flags & 0xffff;
2470 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2471 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2472 if (error)
2473 goto recover;
2474 /* Notify userland that interface flags have changed */
2475 rt_ifmsg(ifp);
2476 return (0);
2477
2478recover:
2479 /* Recover after driver error */
2480 *refcount = oldcount;
2481 ifp->if_flags = oldflags;
2482 return (error);
2483}
2484
2485/*
2486 * Set/clear promiscuous mode on interface ifp based on the truth value
2487 * of pswitch. The calls are reference counted so that only the first
2488 * "on" request actually has an effect, as does the final "off" request.
2489 * Results are undefined if the "off" and "on" requests are not matched.
2490 */
2491int
2492ifpromisc(struct ifnet *ifp, int pswitch)
2493{
2494 int error;
2495 int oldflags = ifp->if_flags;
2496
2497 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2498 &ifp->if_pcount, pswitch);
2499 /* If promiscuous mode status has changed, log a message */
2500 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2501 log(LOG_INFO, "%s: promiscuous mode %s\n",
2502 ifp->if_xname,
2503 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2504 return (error);
2505}
2506
2507/*
2508 * Return interface configuration
2509 * of system. List may be used
2510 * in later ioctl's (above) to get
2511 * other information.
2512 */
2513/*ARGSUSED*/
2514static int
2515ifconf(u_long cmd, caddr_t data)
2516{
2517 INIT_VNET_NET(curvnet);
2518 struct ifconf *ifc = (struct ifconf *)data;
2519#ifdef __amd64__
2520 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2521 struct ifconf ifc_swab;
2522#endif
2523 struct ifnet *ifp;
2524 struct ifaddr *ifa;
2525 struct ifreq ifr;
2526 struct sbuf *sb;
2527 int error, full = 0, valid_len, max_len;
2528
2529#ifdef __amd64__
2530 if (cmd == SIOCGIFCONF32) {
2531 ifc_swab.ifc_len = ifc32->ifc_len;
2532 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2533 ifc = &ifc_swab;
2534 }
2535#endif
2536 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2537 max_len = MAXPHYS - 1;
2538
2539 /* Prevent hostile input from being able to crash the system */
2540 if (ifc->ifc_len <= 0)
2541 return (EINVAL);
2542
2543again:
2544 if (ifc->ifc_len <= max_len) {
2545 max_len = ifc->ifc_len;
2546 full = 1;
2547 }
2548 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2549 max_len = 0;
2550 valid_len = 0;
2551
2552 IFNET_RLOCK(); /* could sleep XXX */
2553 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2554 int addrs;
2555
2556 /*
2557 * Zero the ifr_name buffer to make sure we don't
2558 * disclose the contents of the stack.
2559 */
2560 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2561
2562 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2563 >= sizeof(ifr.ifr_name)) {
2564 sbuf_delete(sb);
2565 IFNET_RUNLOCK();
2566 return (ENAMETOOLONG);
2567 }
2568
2569 addrs = 0;
2570 IF_ADDR_LOCK(ifp);
2571 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2572 struct sockaddr *sa = ifa->ifa_addr;
2573
2574 if (prison_if(curthread->td_ucred, sa) != 0)
2575 continue;
2576 addrs++;
2577#ifdef COMPAT_43
2578 if (cmd == OSIOCGIFCONF) {
2579 struct osockaddr *osa =
2580 (struct osockaddr *)&ifr.ifr_addr;
2581 ifr.ifr_addr = *sa;
2582 osa->sa_family = sa->sa_family;
2583 sbuf_bcat(sb, &ifr, sizeof(ifr));
2584 max_len += sizeof(ifr);
2585 } else
2586#endif
2587 if (sa->sa_len <= sizeof(*sa)) {
2588 ifr.ifr_addr = *sa;
2589 sbuf_bcat(sb, &ifr, sizeof(ifr));
2590 max_len += sizeof(ifr);
2591 } else {
2592 sbuf_bcat(sb, &ifr,
2593 offsetof(struct ifreq, ifr_addr));
2594 max_len += offsetof(struct ifreq, ifr_addr);
2595 sbuf_bcat(sb, sa, sa->sa_len);
2596 max_len += sa->sa_len;
2597 }
2598
2599 if (!sbuf_overflowed(sb))
2600 valid_len = sbuf_len(sb);
2601 }
2602 IF_ADDR_UNLOCK(ifp);
2603 if (addrs == 0) {
2604 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2605 sbuf_bcat(sb, &ifr, sizeof(ifr));
2606 max_len += sizeof(ifr);
2607
2608 if (!sbuf_overflowed(sb))
2609 valid_len = sbuf_len(sb);
2610 }
2611 }
2612 IFNET_RUNLOCK();
2613
2614 /*
2615 * If we didn't allocate enough space (uncommon), try again. If
2616 * we have already allocated as much space as we are allowed,
2617 * return what we've got.
2618 */
2619 if (valid_len != max_len && !full) {
2620 sbuf_delete(sb);
2621 goto again;
2622 }
2623
2624 ifc->ifc_len = valid_len;
2625#ifdef __amd64__
2626 if (cmd == SIOCGIFCONF32)
2627 ifc32->ifc_len = valid_len;
2628#endif
2629 sbuf_finish(sb);
2630 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2631 sbuf_delete(sb);
2632 return (error);
2633}
2634
2635/*
2636 * Just like ifpromisc(), but for all-multicast-reception mode.
2637 */
2638int
2639if_allmulti(struct ifnet *ifp, int onswitch)
2640{
2641
2642 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2643}
2644
2645struct ifmultiaddr *
2646if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2647{
2648 struct ifmultiaddr *ifma;
2649
2650 IF_ADDR_LOCK_ASSERT(ifp);
2651
2652 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2653 if (sa->sa_family == AF_LINK) {
2654 if (sa_dl_equal(ifma->ifma_addr, sa))
2655 break;
2656 } else {
2657 if (sa_equal(ifma->ifma_addr, sa))
2658 break;
2659 }
2660 }
2661
2662 return ifma;
2663}
2664
2665/*
2666 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2667 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2668 * the ifnet multicast address list here, so the caller must do that and
2669 * other setup work (such as notifying the device driver). The reference
2670 * count is initialized to 1.
2671 */
2672static struct ifmultiaddr *
2673if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2674 int mflags)
2675{
2676 struct ifmultiaddr *ifma;
2677 struct sockaddr *dupsa;
2678
2679 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2680 M_ZERO);
2681 if (ifma == NULL)
2682 return (NULL);
2683
2684 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2685 if (dupsa == NULL) {
2686 free(ifma, M_IFMADDR);
2687 return (NULL);
2688 }
2689 bcopy(sa, dupsa, sa->sa_len);
2690 ifma->ifma_addr = dupsa;
2691
2692 ifma->ifma_ifp = ifp;
2693 ifma->ifma_refcount = 1;
2694 ifma->ifma_protospec = NULL;
2695
2696 if (llsa == NULL) {
2697 ifma->ifma_lladdr = NULL;
2698 return (ifma);
2699 }
2700
2701 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2702 if (dupsa == NULL) {
2703 free(ifma->ifma_addr, M_IFMADDR);
2704 free(ifma, M_IFMADDR);
2705 return (NULL);
2706 }
2707 bcopy(llsa, dupsa, llsa->sa_len);
2708 ifma->ifma_lladdr = dupsa;
2709
2710 return (ifma);
2711}
2712
2713/*
2714 * if_freemulti: free ifmultiaddr structure and possibly attached related
2715 * addresses. The caller is responsible for implementing reference
2716 * counting, notifying the driver, handling routing messages, and releasing
2717 * any dependent link layer state.
2718 */
2719static void
2720if_freemulti(struct ifmultiaddr *ifma)
2721{
2722
2723 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2724 ifma->ifma_refcount));
2725 KASSERT(ifma->ifma_protospec == NULL,
2726 ("if_freemulti: protospec not NULL"));
2727
2728 if (ifma->ifma_lladdr != NULL)
2729 free(ifma->ifma_lladdr, M_IFMADDR);
2730 free(ifma->ifma_addr, M_IFMADDR);
2731 free(ifma, M_IFMADDR);
2732}
2733
2734/*
2735 * Register an additional multicast address with a network interface.
2736 *
2737 * - If the address is already present, bump the reference count on the
2738 * address and return.
2739 * - If the address is not link-layer, look up a link layer address.
2740 * - Allocate address structures for one or both addresses, and attach to the
2741 * multicast address list on the interface. If automatically adding a link
2742 * layer address, the protocol address will own a reference to the link
2743 * layer address, to be freed when it is freed.
2744 * - Notify the network device driver of an addition to the multicast address
2745 * list.
2746 *
2747 * 'sa' points to caller-owned memory with the desired multicast address.
2748 *
2749 * 'retifma' will be used to return a pointer to the resulting multicast
2750 * address reference, if desired.
2751 */
2752int
2753if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2754 struct ifmultiaddr **retifma)
2755{
2756 struct ifmultiaddr *ifma, *ll_ifma;
2757 struct sockaddr *llsa;
2758 int error;
2759
2760 /*
2761 * If the address is already present, return a new reference to it;
2762 * otherwise, allocate storage and set up a new address.
2763 */
2764 IF_ADDR_LOCK(ifp);
2765 ifma = if_findmulti(ifp, sa);
2766 if (ifma != NULL) {
2767 ifma->ifma_refcount++;
2768 if (retifma != NULL)
2769 *retifma = ifma;
2770 IF_ADDR_UNLOCK(ifp);
2771 return (0);
2772 }
2773
2774 /*
2775 * The address isn't already present; resolve the protocol address
2776 * into a link layer address, and then look that up, bump its
2777 * refcount or allocate an ifma for that also. If 'llsa' was
2778 * returned, we will need to free it later.
2779 */
2780 llsa = NULL;
2781 ll_ifma = NULL;
2782 if (ifp->if_resolvemulti != NULL) {
2783 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2784 if (error)
2785 goto unlock_out;
2786 }
2787
2788 /*
2789 * Allocate the new address. Don't hook it up yet, as we may also
2790 * need to allocate a link layer multicast address.
2791 */
2792 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2793 if (ifma == NULL) {
2794 error = ENOMEM;
2795 goto free_llsa_out;
2796 }
2797
2798 /*
2799 * If a link layer address is found, we'll need to see if it's
2800 * already present in the address list, or allocate is as well.
2801 * When this block finishes, the link layer address will be on the
2802 * list.
2803 */
2804 if (llsa != NULL) {
2805 ll_ifma = if_findmulti(ifp, llsa);
2806 if (ll_ifma == NULL) {
2807 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2808 if (ll_ifma == NULL) {
2809 --ifma->ifma_refcount;
2810 if_freemulti(ifma);
2811 error = ENOMEM;
2812 goto free_llsa_out;
2813 }
2814 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2815 ifma_link);
2816 } else
2817 ll_ifma->ifma_refcount++;
2818 ifma->ifma_llifma = ll_ifma;
2819 }
2820
2821 /*
2822 * We now have a new multicast address, ifma, and possibly a new or
2823 * referenced link layer address. Add the primary address to the
2824 * ifnet address list.
2825 */
2826 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2827
2828 if (retifma != NULL)
2829 *retifma = ifma;
2830
2831 /*
2832 * Must generate the message while holding the lock so that 'ifma'
2833 * pointer is still valid.
2834 */
2835 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2836 IF_ADDR_UNLOCK(ifp);
2837
2838 /*
2839 * We are certain we have added something, so call down to the
2840 * interface to let them know about it.
2841 */
2842 if (ifp->if_ioctl != NULL) {
2843 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2844 }
2845
2846 if (llsa != NULL)
2847 free(llsa, M_IFMADDR);
2848
2849 return (0);
2850
2851free_llsa_out:
2852 if (llsa != NULL)
2853 free(llsa, M_IFMADDR);
2854
2855unlock_out:
2856 IF_ADDR_UNLOCK(ifp);
2857 return (error);
2858}
2859
2860/*
2861 * Delete a multicast group membership by network-layer group address.
2862 *
2863 * Returns ENOENT if the entry could not be found. If ifp no longer
2864 * exists, results are undefined. This entry point should only be used
2865 * from subsystems which do appropriate locking to hold ifp for the
2866 * duration of the call.
2867 * Network-layer protocol domains must use if_delmulti_ifma().
2868 */
2869int
2870if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2871{
2872 struct ifmultiaddr *ifma;
2873 int lastref;
2874#ifdef INVARIANTS
2875 struct ifnet *oifp;
2876 INIT_VNET_NET(ifp->if_vnet);
2877
2878 IFNET_RLOCK();
2879 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2880 if (ifp == oifp)
2881 break;
2882 if (ifp != oifp)
2883 ifp = NULL;
2884 IFNET_RUNLOCK();
2885
2886 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2887#endif
2888 if (ifp == NULL)
2889 return (ENOENT);
2890
2891 IF_ADDR_LOCK(ifp);
2892 lastref = 0;
2893 ifma = if_findmulti(ifp, sa);
2894 if (ifma != NULL)
2895 lastref = if_delmulti_locked(ifp, ifma, 0);
2896 IF_ADDR_UNLOCK(ifp);
2897
2898 if (ifma == NULL)
2899 return (ENOENT);
2900
2901 if (lastref && ifp->if_ioctl != NULL) {
2902 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2903 }
2904
2905 return (0);
2906}
2907
2908/*
2909 * Delete a multicast group membership by group membership pointer.
2910 * Network-layer protocol domains must use this routine.
2911 *
2912 * It is safe to call this routine if the ifp disappeared.
2913 */
2914void
2915if_delmulti_ifma(struct ifmultiaddr *ifma)
2916{
2917#ifdef DIAGNOSTIC
2918 INIT_VNET_NET(curvnet);
2919#endif
2920 struct ifnet *ifp;
2921 int lastref;
2922
2923 ifp = ifma->ifma_ifp;
2924#ifdef DIAGNOSTIC
2925 if (ifp == NULL) {
2926 printf("%s: ifma_ifp seems to be detached\n", __func__);
2927 } else {
2928 struct ifnet *oifp;
2929
2930 IFNET_RLOCK();
2931 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
2932 if (ifp == oifp)
2933 break;
2934 if (ifp != oifp) {
2935 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2936 ifp = NULL;
2937 }
2938 IFNET_RUNLOCK();
2939 }
2940#endif
2941 /*
2942 * If and only if the ifnet instance exists: Acquire the address lock.
2943 */
2944 if (ifp != NULL)
2945 IF_ADDR_LOCK(ifp);
2946
2947 lastref = if_delmulti_locked(ifp, ifma, 0);
2948
2949 if (ifp != NULL) {
2950 /*
2951 * If and only if the ifnet instance exists:
2952 * Release the address lock.
2953 * If the group was left: update the hardware hash filter.
2954 */
2955 IF_ADDR_UNLOCK(ifp);
2956 if (lastref && ifp->if_ioctl != NULL) {
2957 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2958 }
2959 }
2960}
2961
2962/*
2963 * Perform deletion of network-layer and/or link-layer multicast address.
2964 *
2965 * Return 0 if the reference count was decremented.
2966 * Return 1 if the final reference was released, indicating that the
2967 * hardware hash filter should be reprogrammed.
2968 */
2969static int
2970if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2971{
2972 struct ifmultiaddr *ll_ifma;
2973
2974 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2975 KASSERT(ifma->ifma_ifp == ifp,
2976 ("%s: inconsistent ifp %p", __func__, ifp));
2977 IF_ADDR_LOCK_ASSERT(ifp);
2978 }
2979
2980 ifp = ifma->ifma_ifp;
2981
2982 /*
2983 * If the ifnet is detaching, null out references to ifnet,
2984 * so that upper protocol layers will notice, and not attempt
2985 * to obtain locks for an ifnet which no longer exists. The
2986 * routing socket announcement must happen before the ifnet
2987 * instance is detached from the system.
2988 */
2989 if (detaching) {
2990#ifdef DIAGNOSTIC
2991 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2992#endif
2993 /*
2994 * ifp may already be nulled out if we are being reentered
2995 * to delete the ll_ifma.
2996 */
2997 if (ifp != NULL) {
2998 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2999 ifma->ifma_ifp = NULL;
3000 }
3001 }
3002
3003 if (--ifma->ifma_refcount > 0)
3004 return 0;
3005
3006 /*
3007 * If this ifma is a network-layer ifma, a link-layer ifma may
3008 * have been associated with it. Release it first if so.
3009 */
3010 ll_ifma = ifma->ifma_llifma;
3011 if (ll_ifma != NULL) {
3012 KASSERT(ifma->ifma_lladdr != NULL,
3013 ("%s: llifma w/o lladdr", __func__));
3014 if (detaching)
3015 ll_ifma->ifma_ifp = NULL; /* XXX */
3016 if (--ll_ifma->ifma_refcount == 0) {
3017 if (ifp != NULL) {
3018 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3019 ifma_link);
3020 }
3021 if_freemulti(ll_ifma);
3022 }
3023 }
3024
3025 if (ifp != NULL)
3026 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3027
3028 if_freemulti(ifma);
3029
3030 /*
3031 * The last reference to this instance of struct ifmultiaddr
3032 * was released; the hardware should be notified of this change.
3033 */
3034 return 1;
3035}
3036
3037/*
3038 * Set the link layer address on an interface.
3039 *
3040 * At this time we only support certain types of interfaces,
3041 * and we don't allow the length of the address to change.
3042 */
3043int
3044if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3045{
3046 struct sockaddr_dl *sdl;
3047 struct ifaddr *ifa;
3048 struct ifreq ifr;
3049
3050 ifa = ifp->if_addr;
3051 if (ifa == NULL)
3052 return (EINVAL);
3053 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3054 if (sdl == NULL)
3055 return (EINVAL);
3056 if (len != sdl->sdl_alen) /* don't allow length to change */
3057 return (EINVAL);
3058 switch (ifp->if_type) {
3059 case IFT_ETHER:
3060 case IFT_FDDI:
3061 case IFT_XETHER:
3062 case IFT_ISO88025:
3063 case IFT_L2VLAN:
3064 case IFT_BRIDGE:
3065 case IFT_ARCNET:
3066 case IFT_IEEE8023ADLAG:
3067 case IFT_IEEE80211:
3068 bcopy(lladdr, LLADDR(sdl), len);
3069 break;
3070 default:
3071 return (ENODEV);
3072 }
3073 /*
3074 * If the interface is already up, we need
3075 * to re-init it in order to reprogram its
3076 * address filter.
3077 */
3078 if ((ifp->if_flags & IFF_UP) != 0) {
3079 if (ifp->if_ioctl) {
3080 ifp->if_flags &= ~IFF_UP;
3081 ifr.ifr_flags = ifp->if_flags & 0xffff;
3082 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3083 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3084 ifp->if_flags |= IFF_UP;
3085 ifr.ifr_flags = ifp->if_flags & 0xffff;
3086 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3087 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3088 }
3089#ifdef INET
3090 /*
3091 * Also send gratuitous ARPs to notify other nodes about
3092 * the address change.
3093 */
3094 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3095 if (ifa->ifa_addr->sa_family == AF_INET)
3096 arp_ifinit(ifp, ifa);
3097 }
3098#endif
3099 }
3100 return (0);
3101}
3102
3103/*
3104 * The name argument must be a pointer to storage which will last as
3105 * long as the interface does. For physical devices, the result of
3106 * device_get_name(dev) is a good choice and for pseudo-devices a
3107 * static string works well.
3108 */
3109void
3110if_initname(struct ifnet *ifp, const char *name, int unit)
3111{
3112 ifp->if_dname = name;
3113 ifp->if_dunit = unit;
3114 if (unit != IF_DUNIT_NONE)
3115 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3116 else
3117 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3118}
3119
3120int
3121if_printf(struct ifnet *ifp, const char * fmt, ...)
3122{
3123 va_list ap;
3124 int retval;
3125
3126 retval = printf("%s: ", ifp->if_xname);
3127 va_start(ap, fmt);
3128 retval += vprintf(fmt, ap);
3129 va_end(ap);
3130 return (retval);
3131}
3132
3133void
3134if_start(struct ifnet *ifp)
3135{
3136
3137 (*(ifp)->if_start)(ifp);
3138}
3139
3140/*
3141 * Backwards compatibility interface for drivers
3142 * that have not implemented it
3143 */
3144static int
3145if_transmit(struct ifnet *ifp, struct mbuf *m)
3146{
3147 int error;
3148
3149 IFQ_HANDOFF(ifp, m, error);
3150 return (error);
3151}
3152
3153int
3154if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3155{
3156 int active = 0;
3157
3158 IF_LOCK(ifq);
3159 if (_IF_QFULL(ifq)) {
3160 _IF_DROP(ifq);
3161 IF_UNLOCK(ifq);
3162 m_freem(m);
3163 return (0);
3164 }
3165 if (ifp != NULL) {
3166 ifp->if_obytes += m->m_pkthdr.len + adjust;
3167 if (m->m_flags & (M_BCAST|M_MCAST))
3168 ifp->if_omcasts++;
3169 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3170 }
3171 _IF_ENQUEUE(ifq, m);
3172 IF_UNLOCK(ifq);
3173 if (ifp != NULL && !active)
3174 (*(ifp)->if_start)(ifp);
3175 return (1);
3176}
3177
3178void
3179if_register_com_alloc(u_char type,
3180 if_com_alloc_t *a, if_com_free_t *f)
3181{
3182
3183 KASSERT(if_com_alloc[type] == NULL,
3184 ("if_register_com_alloc: %d already registered", type));
3185 KASSERT(if_com_free[type] == NULL,
3186 ("if_register_com_alloc: %d free already registered", type));
3187
3188 if_com_alloc[type] = a;
3189 if_com_free[type] = f;
3190}
3191
3192void
3193if_deregister_com_alloc(u_char type)
3194{
3195
3196 KASSERT(if_com_alloc[type] != NULL,
3197 ("if_deregister_com_alloc: %d not registered", type));
3198 KASSERT(if_com_free[type] != NULL,
3199 ("if_deregister_com_alloc: %d free not registered", type));
3200 if_com_alloc[type] = NULL;
3201 if_com_free[type] = NULL;
3202}