FreeBSD/Linux Kernel Cross Reference
sys/net/if.c
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: releng/6.1/sys/net/if.c 155707 2006-02-15 03:37:15Z ps $
31 */
32
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_mac.h"
37 #include "opt_carp.h"
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/conf.h>
42 #include <sys/mac.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/proc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/domain.h>
58 #include <sys/jail.h>
59 #include <machine/stdarg.h>
60
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/if_clone.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/radix.h>
68 #include <net/route.h>
69
70 #if defined(INET) || defined(INET6)
71 /*XXX*/
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #ifdef INET6
75 #include <netinet6/in6_var.h>
76 #include <netinet6/in6_ifattach.h>
77 #endif
78 #endif
79 #ifdef INET
80 #include <netinet/if_ether.h>
81 #endif
82 #ifdef DEV_CARP
83 #include <netinet/ip_carp.h>
84 #endif
85
86 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
87 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
88
89 /* Log link state change events */
90 static int log_link_state_change = 1;
91
92 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
93 &log_link_state_change, 0,
94 "log interface link state change events");
95
96 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
97 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
98
99 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
100
101 static void if_attachdomain(void *);
102 static void if_attachdomain1(struct ifnet *);
103 static int ifconf(u_long, caddr_t);
104 static void if_grow(void);
105 static void if_init(void *);
106 static void if_check(void *);
107 static void if_qflush(struct ifaltq *);
108 static void if_route(struct ifnet *, int flag, int fam);
109 static int if_setflag(struct ifnet *, int, int, int *, int);
110 static void if_slowtimo(void *);
111 static void if_unroute(struct ifnet *, int flag, int fam);
112 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
113 static int if_rtdel(struct radix_node *, void *);
114 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
115 static void if_start_deferred(void *context, int pending);
116 static void do_link_state_change(void *, int);
117 #ifdef INET6
118 /*
119 * XXX: declare here to avoid to include many inet6 related files..
120 * should be more generalized?
121 */
122 extern void nd6_setmtu(struct ifnet *);
123 #endif
124
125 int if_index = 0;
126 struct ifindex_entry *ifindex_table = NULL;
127 int ifqmaxlen = IFQ_MAXLEN;
128 struct ifnethead ifnet; /* depend on static init XXX */
129 struct mtx ifnet_lock;
130 static if_com_alloc_t *if_com_alloc[256];
131 static if_com_free_t *if_com_free[256];
132
133 static int if_indexlim = 8;
134 static struct knlist ifklist;
135
136 static void filt_netdetach(struct knote *kn);
137 static int filt_netdev(struct knote *kn, long hint);
138
139 static struct filterops netdev_filtops =
140 { 1, NULL, filt_netdetach, filt_netdev };
141
142 /*
143 * System initialization
144 */
145 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
146 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
147
148 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
151
152 static d_open_t netopen;
153 static d_close_t netclose;
154 static d_ioctl_t netioctl;
155 static d_kqfilter_t netkqfilter;
156
157 static struct cdevsw net_cdevsw = {
158 .d_version = D_VERSION,
159 .d_flags = D_NEEDGIANT,
160 .d_open = netopen,
161 .d_close = netclose,
162 .d_ioctl = netioctl,
163 .d_name = "net",
164 .d_kqfilter = netkqfilter,
165 };
166
167 static int
168 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
169 {
170 return (0);
171 }
172
173 static int
174 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
175 {
176 return (0);
177 }
178
179 static int
180 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
181 {
182 struct ifnet *ifp;
183 int error, idx;
184
185 /* only support interface specific ioctls */
186 if (IOCGROUP(cmd) != 'i')
187 return (EOPNOTSUPP);
188 idx = minor(dev);
189 if (idx == 0) {
190 /*
191 * special network device, not interface.
192 */
193 if (cmd == SIOCGIFCONF)
194 return (ifconf(cmd, data)); /* XXX remove cmd */
195 #ifdef __amd64__
196 if (cmd == SIOCGIFCONF32)
197 return (ifconf(cmd, data)); /* XXX remove cmd */
198 #endif
199 return (EOPNOTSUPP);
200 }
201
202 ifp = ifnet_byindex(idx);
203 if (ifp == NULL)
204 return (ENXIO);
205
206 error = ifhwioctl(cmd, ifp, data, td);
207 if (error == ENOIOCTL)
208 error = EOPNOTSUPP;
209 return (error);
210 }
211
212 static int
213 netkqfilter(struct cdev *dev, struct knote *kn)
214 {
215 struct knlist *klist;
216 struct ifnet *ifp;
217 int idx;
218
219 switch (kn->kn_filter) {
220 case EVFILT_NETDEV:
221 kn->kn_fop = &netdev_filtops;
222 break;
223 default:
224 return (EINVAL);
225 }
226
227 idx = minor(dev);
228 if (idx == 0) {
229 klist = &ifklist;
230 } else {
231 ifp = ifnet_byindex(idx);
232 if (ifp == NULL)
233 return (1);
234 klist = &ifp->if_klist;
235 }
236
237 kn->kn_hook = (caddr_t)klist;
238
239 knlist_add(klist, kn, 0);
240
241 return (0);
242 }
243
244 static void
245 filt_netdetach(struct knote *kn)
246 {
247 struct knlist *klist = (struct knlist *)kn->kn_hook;
248
249 knlist_remove(klist, kn, 0);
250 }
251
252 static int
253 filt_netdev(struct knote *kn, long hint)
254 {
255 struct knlist *klist = (struct knlist *)kn->kn_hook;
256
257 /*
258 * Currently NOTE_EXIT is abused to indicate device detach.
259 */
260 if (hint == NOTE_EXIT) {
261 kn->kn_data = NOTE_LINKINV;
262 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
263 knlist_remove_inevent(klist, kn);
264 return (1);
265 }
266 if (hint != 0)
267 kn->kn_data = hint; /* current status */
268 if (kn->kn_sfflags & hint)
269 kn->kn_fflags |= hint;
270 return (kn->kn_fflags != 0);
271 }
272
273 /*
274 * Network interface utility routines.
275 *
276 * Routines with ifa_ifwith* names take sockaddr *'s as
277 * parameters.
278 */
279 /* ARGSUSED*/
280 static void
281 if_init(void *dummy __unused)
282 {
283
284 IFNET_LOCK_INIT();
285 TAILQ_INIT(&ifnet);
286 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
287 if_grow(); /* create initial table */
288 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
289 UID_ROOT, GID_WHEEL, 0600, "network");
290 if_clone_init();
291 }
292
293 static void
294 if_grow(void)
295 {
296 u_int n;
297 struct ifindex_entry *e;
298
299 if_indexlim <<= 1;
300 n = if_indexlim * sizeof(*e);
301 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
302 if (ifindex_table != NULL) {
303 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
304 free((caddr_t)ifindex_table, M_IFNET);
305 }
306 ifindex_table = e;
307 }
308
309 /* ARGSUSED*/
310 static void
311 if_check(void *dummy __unused)
312 {
313 struct ifnet *ifp;
314 int s;
315
316 s = splimp();
317 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
318 TAILQ_FOREACH(ifp, &ifnet, if_link) {
319 if (ifp->if_snd.ifq_maxlen == 0) {
320 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
321 ifp->if_snd.ifq_maxlen = ifqmaxlen;
322 }
323 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
324 if_printf(ifp,
325 "XXX: driver didn't initialize queue mtx\n");
326 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
327 MTX_NETWORK_LOCK, MTX_DEF);
328 }
329 }
330 IFNET_RUNLOCK();
331 splx(s);
332 if_slowtimo(0);
333 }
334
335 /*
336 * Allocate a struct ifnet and in index for an interface.
337 */
338 struct ifnet*
339 if_alloc(u_char type)
340 {
341 struct ifnet *ifp;
342
343 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
344
345 /*
346 * Try to find an empty slot below if_index. If we fail, take
347 * the next slot.
348 *
349 * XXX: should be locked!
350 */
351 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
352 if (ifnet_byindex(ifp->if_index) == NULL)
353 break;
354 }
355 /* Catch if_index overflow. */
356 if (ifp->if_index < 1) {
357 free(ifp, M_IFNET);
358 return (NULL);
359 }
360 if (ifp->if_index > if_index)
361 if_index = ifp->if_index;
362 if (if_index >= if_indexlim)
363 if_grow();
364 ifnet_byindex(ifp->if_index) = ifp;
365
366 ifp->if_type = type;
367
368 if (if_com_alloc[type] != NULL) {
369 ifp->if_l2com = if_com_alloc[type](type, ifp);
370 if (ifp->if_l2com == NULL) {
371 free(ifp, M_IFNET);
372 return (NULL);
373 }
374 }
375 IF_ADDR_LOCK_INIT(ifp);
376
377 return (ifp);
378 }
379
380 void
381 if_free(struct ifnet *ifp)
382 {
383
384 /* Do not add code to this function! Add it to if_free_type(). */
385 if_free_type(ifp, ifp->if_type);
386 }
387
388 void
389 if_free_type(struct ifnet *ifp, u_char type)
390 {
391
392 if (ifp != ifnet_byindex(ifp->if_index)) {
393 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
394 __func__);
395 return;
396 }
397
398 IF_ADDR_LOCK_DESTROY(ifp);
399
400 ifnet_byindex(ifp->if_index) = NULL;
401
402 /* XXX: should be locked with if_findindex() */
403 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
404 if_index--;
405
406 if (if_com_free[type] != NULL)
407 if_com_free[type](ifp->if_l2com, type);
408
409 free(ifp, M_IFNET);
410 };
411
412 /*
413 * Attach an interface to the
414 * list of "active" interfaces.
415 */
416 void
417 if_attach(struct ifnet *ifp)
418 {
419 unsigned socksize, ifasize;
420 int namelen, masklen;
421 struct sockaddr_dl *sdl;
422 struct ifaddr *ifa;
423
424 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
425 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
426 ifp->if_xname);
427
428 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
429 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
430 IF_AFDATA_LOCK_INIT(ifp);
431 ifp->if_afdata_initialized = 0;
432 IFNET_WLOCK();
433 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
434 IFNET_WUNLOCK();
435 /*
436 * XXX -
437 * The old code would work if the interface passed a pre-existing
438 * chain of ifaddrs to this code. We don't trust our callers to
439 * properly initialize the tailq, however, so we no longer allow
440 * this unlikely case.
441 */
442 TAILQ_INIT(&ifp->if_addrhead);
443 TAILQ_INIT(&ifp->if_prefixhead);
444 TAILQ_INIT(&ifp->if_multiaddrs);
445 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
446 getmicrotime(&ifp->if_lastchange);
447 ifp->if_data.ifi_epoch = time_uptime;
448 ifp->if_data.ifi_datalen = sizeof(struct if_data);
449
450 #ifdef MAC
451 mac_init_ifnet(ifp);
452 mac_create_ifnet(ifp);
453 #endif
454
455 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
456 unit2minor(ifp->if_index),
457 UID_ROOT, GID_WHEEL, 0600, "%s/%s",
458 net_cdevsw.d_name, ifp->if_xname);
459 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
460 net_cdevsw.d_name, ifp->if_index);
461
462 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
463
464 /*
465 * create a Link Level name for this device
466 */
467 namelen = strlen(ifp->if_xname);
468 /*
469 * Always save enough space for any possiable name so we can do
470 * a rename in place later.
471 */
472 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
473 socksize = masklen + ifp->if_addrlen;
474 if (socksize < sizeof(*sdl))
475 socksize = sizeof(*sdl);
476 socksize = roundup2(socksize, sizeof(long));
477 ifasize = sizeof(*ifa) + 2 * socksize;
478 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
479 IFA_LOCK_INIT(ifa);
480 sdl = (struct sockaddr_dl *)(ifa + 1);
481 sdl->sdl_len = socksize;
482 sdl->sdl_family = AF_LINK;
483 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
484 sdl->sdl_nlen = namelen;
485 sdl->sdl_index = ifp->if_index;
486 sdl->sdl_type = ifp->if_type;
487 ifaddr_byindex(ifp->if_index) = ifa;
488 ifa->ifa_ifp = ifp;
489 ifa->ifa_rtrequest = link_rtrequest;
490 ifa->ifa_addr = (struct sockaddr *)sdl;
491 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
492 ifa->ifa_netmask = (struct sockaddr *)sdl;
493 sdl->sdl_len = masklen;
494 while (namelen != 0)
495 sdl->sdl_data[--namelen] = 0xff;
496 ifa->ifa_refcnt = 1;
497 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
498 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
499 ifp->if_snd.altq_type = 0;
500 ifp->if_snd.altq_disc = NULL;
501 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
502 ifp->if_snd.altq_tbr = NULL;
503 ifp->if_snd.altq_ifp = ifp;
504
505 if (domain_init_status >= 2)
506 if_attachdomain1(ifp);
507
508 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
509
510 /* Announce the interface. */
511 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
512 }
513
514 static void
515 if_attachdomain(void *dummy)
516 {
517 struct ifnet *ifp;
518 int s;
519
520 s = splnet();
521 TAILQ_FOREACH(ifp, &ifnet, if_link)
522 if_attachdomain1(ifp);
523 splx(s);
524 }
525 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
526 if_attachdomain, NULL);
527
528 static void
529 if_attachdomain1(struct ifnet *ifp)
530 {
531 struct domain *dp;
532 int s;
533
534 s = splnet();
535
536 /*
537 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
538 * cannot lock ifp->if_afdata initialization, entirely.
539 */
540 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
541 splx(s);
542 return;
543 }
544 if (ifp->if_afdata_initialized >= domain_init_status) {
545 IF_AFDATA_UNLOCK(ifp);
546 splx(s);
547 printf("if_attachdomain called more than once on %s\n",
548 ifp->if_xname);
549 return;
550 }
551 ifp->if_afdata_initialized = domain_init_status;
552 IF_AFDATA_UNLOCK(ifp);
553
554 /* address family dependent data region */
555 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
556 for (dp = domains; dp; dp = dp->dom_next) {
557 if (dp->dom_ifattach)
558 ifp->if_afdata[dp->dom_family] =
559 (*dp->dom_ifattach)(ifp);
560 }
561
562 splx(s);
563 }
564
565 /*
566 * Remove any network addresses from an interface.
567 */
568
569 void
570 if_purgeaddrs(struct ifnet *ifp)
571 {
572 struct ifaddr *ifa, *next;
573
574 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
575
576 if (ifa->ifa_addr->sa_family == AF_LINK)
577 continue;
578 #ifdef INET
579 /* XXX: Ugly!! ad hoc just for INET */
580 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
581 struct ifaliasreq ifr;
582
583 bzero(&ifr, sizeof(ifr));
584 ifr.ifra_addr = *ifa->ifa_addr;
585 if (ifa->ifa_dstaddr)
586 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
587 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
588 NULL) == 0)
589 continue;
590 }
591 #endif /* INET */
592 #ifdef INET6
593 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
594 in6_purgeaddr(ifa);
595 /* ifp_addrhead is already updated */
596 continue;
597 }
598 #endif /* INET6 */
599 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
600 IFAFREE(ifa);
601 }
602 }
603
604 /*
605 * Detach an interface, removing it from the
606 * list of "active" interfaces and freeing the struct ifnet.
607 *
608 * XXXRW: There are some significant questions about event ordering, and
609 * how to prevent things from starting to use the interface during detach.
610 */
611 void
612 if_detach(struct ifnet *ifp)
613 {
614 struct ifaddr *ifa;
615 struct radix_node_head *rnh;
616 int s;
617 int i;
618 struct domain *dp;
619 struct ifnet *iter;
620 int found;
621
622 /*
623 * Remove/wait for pending events.
624 */
625 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
626
627 #ifdef DEV_CARP
628 /* Maybe hook to the generalized departure handler above?!? */
629 if (ifp->if_carp)
630 carp_ifdetach(ifp);
631 #endif
632
633 /*
634 * Remove routes and flush queues.
635 */
636 s = splnet();
637 if_down(ifp);
638 #ifdef ALTQ
639 if (ALTQ_IS_ENABLED(&ifp->if_snd))
640 altq_disable(&ifp->if_snd);
641 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
642 altq_detach(&ifp->if_snd);
643 #endif
644
645 if_purgeaddrs(ifp);
646
647 #ifdef INET
648 in_ifdetach(ifp);
649 #endif
650
651 #ifdef INET6
652 /*
653 * Remove all IPv6 kernel structs related to ifp. This should be done
654 * before removing routing entries below, since IPv6 interface direct
655 * routes are expected to be removed by the IPv6-specific kernel API.
656 * Otherwise, the kernel will detect some inconsistency and bark it.
657 */
658 in6_ifdetach(ifp);
659 #endif
660 /*
661 * Remove address from ifindex_table[] and maybe decrement if_index.
662 * Clean up all addresses.
663 */
664 ifaddr_byindex(ifp->if_index) = NULL;
665 destroy_dev(ifdev_byindex(ifp->if_index));
666 ifdev_byindex(ifp->if_index) = NULL;
667
668 /* We can now free link ifaddr. */
669 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
670 ifa = TAILQ_FIRST(&ifp->if_addrhead);
671 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
672 IFAFREE(ifa);
673 }
674
675 /*
676 * Delete all remaining routes using this interface
677 * Unfortuneatly the only way to do this is to slog through
678 * the entire routing table looking for routes which point
679 * to this interface...oh well...
680 */
681 for (i = 1; i <= AF_MAX; i++) {
682 if ((rnh = rt_tables[i]) == NULL)
683 continue;
684 RADIX_NODE_HEAD_LOCK(rnh);
685 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
686 RADIX_NODE_HEAD_UNLOCK(rnh);
687 }
688
689 /* Announce that the interface is gone. */
690 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
691 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
692
693 IF_AFDATA_LOCK(ifp);
694 for (dp = domains; dp; dp = dp->dom_next) {
695 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
696 (*dp->dom_ifdetach)(ifp,
697 ifp->if_afdata[dp->dom_family]);
698 }
699 IF_AFDATA_UNLOCK(ifp);
700
701 #ifdef MAC
702 mac_destroy_ifnet(ifp);
703 #endif /* MAC */
704 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
705 knlist_clear(&ifp->if_klist, 0);
706 knlist_destroy(&ifp->if_klist);
707 IFNET_WLOCK();
708 found = 0;
709 TAILQ_FOREACH(iter, &ifnet, if_link)
710 if (iter == ifp) {
711 found = 1;
712 break;
713 }
714 if (found)
715 TAILQ_REMOVE(&ifnet, ifp, if_link);
716 IFNET_WUNLOCK();
717 mtx_destroy(&ifp->if_snd.ifq_mtx);
718 IF_AFDATA_DESTROY(ifp);
719 splx(s);
720 }
721
722 /*
723 * Delete Routes for a Network Interface
724 *
725 * Called for each routing entry via the rnh->rnh_walktree() call above
726 * to delete all route entries referencing a detaching network interface.
727 *
728 * Arguments:
729 * rn pointer to node in the routing table
730 * arg argument passed to rnh->rnh_walktree() - detaching interface
731 *
732 * Returns:
733 * 0 successful
734 * errno failed - reason indicated
735 *
736 */
737 static int
738 if_rtdel(struct radix_node *rn, void *arg)
739 {
740 struct rtentry *rt = (struct rtentry *)rn;
741 struct ifnet *ifp = arg;
742 int err;
743
744 if (rt->rt_ifp == ifp) {
745
746 /*
747 * Protect (sorta) against walktree recursion problems
748 * with cloned routes
749 */
750 if ((rt->rt_flags & RTF_UP) == 0)
751 return (0);
752
753 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
754 rt_mask(rt), rt->rt_flags,
755 (struct rtentry **) NULL);
756 if (err) {
757 log(LOG_WARNING, "if_rtdel: error %d\n", err);
758 }
759 }
760
761 return (0);
762 }
763
764 #define sa_equal(a1, a2) (bcmp((a1), (a2), ((a1))->sa_len) == 0)
765
766 /*
767 * Locate an interface based on a complete address.
768 */
769 /*ARGSUSED*/
770 struct ifaddr *
771 ifa_ifwithaddr(struct sockaddr *addr)
772 {
773 struct ifnet *ifp;
774 struct ifaddr *ifa;
775
776 IFNET_RLOCK();
777 TAILQ_FOREACH(ifp, &ifnet, if_link)
778 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
779 if (ifa->ifa_addr->sa_family != addr->sa_family)
780 continue;
781 if (sa_equal(addr, ifa->ifa_addr))
782 goto done;
783 /* IP6 doesn't have broadcast */
784 if ((ifp->if_flags & IFF_BROADCAST) &&
785 ifa->ifa_broadaddr &&
786 ifa->ifa_broadaddr->sa_len != 0 &&
787 sa_equal(ifa->ifa_broadaddr, addr))
788 goto done;
789 }
790 ifa = NULL;
791 done:
792 IFNET_RUNLOCK();
793 return (ifa);
794 }
795
796 /*
797 * Locate the point to point interface with a given destination address.
798 */
799 /*ARGSUSED*/
800 struct ifaddr *
801 ifa_ifwithdstaddr(struct sockaddr *addr)
802 {
803 struct ifnet *ifp;
804 struct ifaddr *ifa;
805
806 IFNET_RLOCK();
807 TAILQ_FOREACH(ifp, &ifnet, if_link) {
808 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
809 continue;
810 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
811 if (ifa->ifa_addr->sa_family != addr->sa_family)
812 continue;
813 if (ifa->ifa_dstaddr &&
814 sa_equal(addr, ifa->ifa_dstaddr))
815 goto done;
816 }
817 }
818 ifa = NULL;
819 done:
820 IFNET_RUNLOCK();
821 return (ifa);
822 }
823
824 /*
825 * Find an interface on a specific network. If many, choice
826 * is most specific found.
827 */
828 struct ifaddr *
829 ifa_ifwithnet(struct sockaddr *addr)
830 {
831 struct ifnet *ifp;
832 struct ifaddr *ifa;
833 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
834 u_int af = addr->sa_family;
835 char *addr_data = addr->sa_data, *cplim;
836
837 /*
838 * AF_LINK addresses can be looked up directly by their index number,
839 * so do that if we can.
840 */
841 if (af == AF_LINK) {
842 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
843 if (sdl->sdl_index && sdl->sdl_index <= if_index)
844 return (ifaddr_byindex(sdl->sdl_index));
845 }
846
847 /*
848 * Scan though each interface, looking for ones that have
849 * addresses in this address family.
850 */
851 IFNET_RLOCK();
852 TAILQ_FOREACH(ifp, &ifnet, if_link) {
853 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
854 char *cp, *cp2, *cp3;
855
856 if (ifa->ifa_addr->sa_family != af)
857 next: continue;
858 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
859 /*
860 * This is a bit broken as it doesn't
861 * take into account that the remote end may
862 * be a single node in the network we are
863 * looking for.
864 * The trouble is that we don't know the
865 * netmask for the remote end.
866 */
867 if (ifa->ifa_dstaddr != 0 &&
868 sa_equal(addr, ifa->ifa_dstaddr))
869 goto done;
870 } else {
871 /*
872 * if we have a special address handler,
873 * then use it instead of the generic one.
874 */
875 if (ifa->ifa_claim_addr) {
876 if ((*ifa->ifa_claim_addr)(ifa, addr))
877 goto done;
878 continue;
879 }
880
881 /*
882 * Scan all the bits in the ifa's address.
883 * If a bit dissagrees with what we are
884 * looking for, mask it with the netmask
885 * to see if it really matters.
886 * (A byte at a time)
887 */
888 if (ifa->ifa_netmask == 0)
889 continue;
890 cp = addr_data;
891 cp2 = ifa->ifa_addr->sa_data;
892 cp3 = ifa->ifa_netmask->sa_data;
893 cplim = ifa->ifa_netmask->sa_len
894 + (char *)ifa->ifa_netmask;
895 while (cp3 < cplim)
896 if ((*cp++ ^ *cp2++) & *cp3++)
897 goto next; /* next address! */
898 /*
899 * If the netmask of what we just found
900 * is more specific than what we had before
901 * (if we had one) then remember the new one
902 * before continuing to search
903 * for an even better one.
904 */
905 if (ifa_maybe == 0 ||
906 rn_refines((caddr_t)ifa->ifa_netmask,
907 (caddr_t)ifa_maybe->ifa_netmask))
908 ifa_maybe = ifa;
909 }
910 }
911 }
912 ifa = ifa_maybe;
913 done:
914 IFNET_RUNLOCK();
915 return (ifa);
916 }
917
918 /*
919 * Find an interface address specific to an interface best matching
920 * a given address.
921 */
922 struct ifaddr *
923 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
924 {
925 struct ifaddr *ifa;
926 char *cp, *cp2, *cp3;
927 char *cplim;
928 struct ifaddr *ifa_maybe = 0;
929 u_int af = addr->sa_family;
930
931 if (af >= AF_MAX)
932 return (0);
933 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
934 if (ifa->ifa_addr->sa_family != af)
935 continue;
936 if (ifa_maybe == 0)
937 ifa_maybe = ifa;
938 if (ifa->ifa_netmask == 0) {
939 if (sa_equal(addr, ifa->ifa_addr) ||
940 (ifa->ifa_dstaddr &&
941 sa_equal(addr, ifa->ifa_dstaddr)))
942 goto done;
943 continue;
944 }
945 if (ifp->if_flags & IFF_POINTOPOINT) {
946 if (sa_equal(addr, ifa->ifa_dstaddr))
947 goto done;
948 } else {
949 cp = addr->sa_data;
950 cp2 = ifa->ifa_addr->sa_data;
951 cp3 = ifa->ifa_netmask->sa_data;
952 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
953 for (; cp3 < cplim; cp3++)
954 if ((*cp++ ^ *cp2++) & *cp3)
955 break;
956 if (cp3 == cplim)
957 goto done;
958 }
959 }
960 ifa = ifa_maybe;
961 done:
962 return (ifa);
963 }
964
965 #include <net/route.h>
966
967 /*
968 * Default action when installing a route with a Link Level gateway.
969 * Lookup an appropriate real ifa to point to.
970 * This should be moved to /sys/net/link.c eventually.
971 */
972 static void
973 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
974 {
975 struct ifaddr *ifa, *oifa;
976 struct sockaddr *dst;
977 struct ifnet *ifp;
978
979 RT_LOCK_ASSERT(rt);
980
981 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
982 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
983 return;
984 ifa = ifaof_ifpforaddr(dst, ifp);
985 if (ifa) {
986 IFAREF(ifa); /* XXX */
987 oifa = rt->rt_ifa;
988 rt->rt_ifa = ifa;
989 IFAFREE(oifa);
990 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
991 ifa->ifa_rtrequest(cmd, rt, info);
992 }
993 }
994
995 /*
996 * Mark an interface down and notify protocols of
997 * the transition.
998 * NOTE: must be called at splnet or eqivalent.
999 */
1000 static void
1001 if_unroute(struct ifnet *ifp, int flag, int fam)
1002 {
1003 struct ifaddr *ifa;
1004
1005 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1006
1007 ifp->if_flags &= ~flag;
1008 getmicrotime(&ifp->if_lastchange);
1009 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1010 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1011 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1012 if_qflush(&ifp->if_snd);
1013 #ifdef DEV_CARP
1014 if (ifp->if_carp)
1015 carp_carpdev_state(ifp->if_carp);
1016 #endif
1017 rt_ifmsg(ifp);
1018 }
1019
1020 /*
1021 * Mark an interface up and notify protocols of
1022 * the transition.
1023 * NOTE: must be called at splnet or eqivalent.
1024 */
1025 static void
1026 if_route(struct ifnet *ifp, int flag, int fam)
1027 {
1028 struct ifaddr *ifa;
1029
1030 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1031
1032 ifp->if_flags |= flag;
1033 getmicrotime(&ifp->if_lastchange);
1034 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1035 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1036 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1037 #ifdef DEV_CARP
1038 if (ifp->if_carp)
1039 carp_carpdev_state(ifp->if_carp);
1040 #endif
1041 rt_ifmsg(ifp);
1042 #ifdef INET6
1043 in6_if_up(ifp);
1044 #endif
1045 }
1046
1047 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1048
1049 /*
1050 * Handle a change in the interface link state. To avoid LORs
1051 * between driver lock and upper layer locks, as well as possible
1052 * recursions, we post event to taskqueue, and all job
1053 * is done in static do_link_state_change().
1054 */
1055 void
1056 if_link_state_change(struct ifnet *ifp, int link_state)
1057 {
1058 /* Return if state hasn't changed. */
1059 if (ifp->if_link_state == link_state)
1060 return;
1061
1062 ifp->if_link_state = link_state;
1063
1064 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1065 }
1066
1067 static void
1068 do_link_state_change(void *arg, int pending)
1069 {
1070 struct ifnet *ifp = (struct ifnet *)arg;
1071 int link_state = ifp->if_link_state;
1072 int link;
1073
1074 /* Notify that the link state has changed. */
1075 rt_ifmsg(ifp);
1076 if (link_state == LINK_STATE_UP)
1077 link = NOTE_LINKUP;
1078 else if (link_state == LINK_STATE_DOWN)
1079 link = NOTE_LINKDOWN;
1080 else
1081 link = NOTE_LINKINV;
1082 KNOTE_UNLOCKED(&ifp->if_klist, link);
1083 if (ifp->if_nvlans != 0)
1084 (*vlan_link_state_p)(ifp, link);
1085
1086 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1087 IFP2AC(ifp)->ac_netgraph != NULL)
1088 (*ng_ether_link_state_p)(ifp, link_state);
1089 #ifdef DEV_CARP
1090 if (ifp->if_carp)
1091 carp_carpdev_state(ifp->if_carp);
1092 #endif
1093 if (ifp->if_bridge) {
1094 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1095 (*bstp_linkstate_p)(ifp, link_state);
1096 }
1097
1098 devctl_notify("IFNET", ifp->if_xname,
1099 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1100 if (pending > 1)
1101 if_printf(ifp, "%d link states coalesced\n", pending);
1102 if (log_link_state_change)
1103 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1104 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1105 }
1106
1107 /*
1108 * Mark an interface down and notify protocols of
1109 * the transition.
1110 * NOTE: must be called at splnet or eqivalent.
1111 */
1112 void
1113 if_down(struct ifnet *ifp)
1114 {
1115
1116 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1117 }
1118
1119 /*
1120 * Mark an interface up and notify protocols of
1121 * the transition.
1122 * NOTE: must be called at splnet or eqivalent.
1123 */
1124 void
1125 if_up(struct ifnet *ifp)
1126 {
1127
1128 if_route(ifp, IFF_UP, AF_UNSPEC);
1129 }
1130
1131 /*
1132 * Flush an interface queue.
1133 */
1134 static void
1135 if_qflush(struct ifaltq *ifq)
1136 {
1137 struct mbuf *m, *n;
1138
1139 IFQ_LOCK(ifq);
1140 #ifdef ALTQ
1141 if (ALTQ_IS_ENABLED(ifq))
1142 ALTQ_PURGE(ifq);
1143 #endif
1144 n = ifq->ifq_head;
1145 while ((m = n) != 0) {
1146 n = m->m_act;
1147 m_freem(m);
1148 }
1149 ifq->ifq_head = 0;
1150 ifq->ifq_tail = 0;
1151 ifq->ifq_len = 0;
1152 IFQ_UNLOCK(ifq);
1153 }
1154
1155 /*
1156 * Handle interface watchdog timer routines. Called
1157 * from softclock, we decrement timers (if set) and
1158 * call the appropriate interface routine on expiration.
1159 *
1160 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1161 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1162 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1163 */
1164 static void
1165 if_slowtimo(void *arg)
1166 {
1167 struct ifnet *ifp;
1168 int s = splimp();
1169
1170 IFNET_RLOCK();
1171 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1172 if (ifp->if_timer == 0 || --ifp->if_timer)
1173 continue;
1174 if (ifp->if_watchdog)
1175 (*ifp->if_watchdog)(ifp);
1176 }
1177 IFNET_RUNLOCK();
1178 splx(s);
1179 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1180 }
1181
1182 /*
1183 * Map interface name to
1184 * interface structure pointer.
1185 */
1186 struct ifnet *
1187 ifunit(const char *name)
1188 {
1189 struct ifnet *ifp;
1190
1191 IFNET_RLOCK();
1192 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1193 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1194 break;
1195 }
1196 IFNET_RUNLOCK();
1197 return (ifp);
1198 }
1199
1200 /*
1201 * Hardware specific interface ioctls.
1202 */
1203 static int
1204 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1205 {
1206 struct ifreq *ifr;
1207 struct ifstat *ifs;
1208 int error = 0;
1209 int new_flags, temp_flags;
1210 size_t namelen, onamelen;
1211 char new_name[IFNAMSIZ];
1212 struct ifaddr *ifa;
1213 struct sockaddr_dl *sdl;
1214
1215 ifr = (struct ifreq *)data;
1216 switch (cmd) {
1217 case SIOCGIFINDEX:
1218 ifr->ifr_index = ifp->if_index;
1219 break;
1220
1221 case SIOCGIFFLAGS:
1222 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1223 ifr->ifr_flags = temp_flags & 0xffff;
1224 ifr->ifr_flagshigh = temp_flags >> 16;
1225 break;
1226
1227 case SIOCGIFCAP:
1228 ifr->ifr_reqcap = ifp->if_capabilities;
1229 ifr->ifr_curcap = ifp->if_capenable;
1230 break;
1231
1232 #ifdef MAC
1233 case SIOCGIFMAC:
1234 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1235 break;
1236 #endif
1237
1238 case SIOCGIFMETRIC:
1239 ifr->ifr_metric = ifp->if_metric;
1240 break;
1241
1242 case SIOCGIFMTU:
1243 ifr->ifr_mtu = ifp->if_mtu;
1244 break;
1245
1246 case SIOCGIFPHYS:
1247 ifr->ifr_phys = ifp->if_physical;
1248 break;
1249
1250 case SIOCSIFFLAGS:
1251 error = suser(td);
1252 if (error)
1253 return (error);
1254 /*
1255 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1256 * check, so we don't need special handling here yet.
1257 */
1258 new_flags = (ifr->ifr_flags & 0xffff) |
1259 (ifr->ifr_flagshigh << 16);
1260 if (ifp->if_flags & IFF_SMART) {
1261 /* Smart drivers twiddle their own routes */
1262 } else if (ifp->if_flags & IFF_UP &&
1263 (new_flags & IFF_UP) == 0) {
1264 int s = splimp();
1265 if_down(ifp);
1266 splx(s);
1267 } else if (new_flags & IFF_UP &&
1268 (ifp->if_flags & IFF_UP) == 0) {
1269 int s = splimp();
1270 if_up(ifp);
1271 splx(s);
1272 }
1273 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1274 (new_flags &~ IFF_CANTCHANGE);
1275 if (new_flags & IFF_PPROMISC) {
1276 /* Permanently promiscuous mode requested */
1277 ifp->if_flags |= IFF_PROMISC;
1278 } else if (ifp->if_pcount == 0) {
1279 ifp->if_flags &= ~IFF_PROMISC;
1280 }
1281 if (ifp->if_ioctl != NULL) {
1282 IFF_LOCKGIANT(ifp);
1283 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1284 IFF_UNLOCKGIANT(ifp);
1285 }
1286 getmicrotime(&ifp->if_lastchange);
1287 break;
1288
1289 case SIOCSIFCAP:
1290 error = suser(td);
1291 if (error)
1292 return (error);
1293 if (ifp->if_ioctl == NULL)
1294 return (EOPNOTSUPP);
1295 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1296 return (EINVAL);
1297 IFF_LOCKGIANT(ifp);
1298 error = (*ifp->if_ioctl)(ifp, cmd, data);
1299 IFF_UNLOCKGIANT(ifp);
1300 if (error == 0)
1301 getmicrotime(&ifp->if_lastchange);
1302 break;
1303
1304 #ifdef MAC
1305 case SIOCSIFMAC:
1306 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1307 break;
1308 #endif
1309
1310 case SIOCSIFNAME:
1311 error = suser(td);
1312 if (error != 0)
1313 return (error);
1314 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1315 if (error != 0)
1316 return (error);
1317 if (new_name[0] == '\0')
1318 return (EINVAL);
1319 if (ifunit(new_name) != NULL)
1320 return (EEXIST);
1321
1322 /* Announce the departure of the interface. */
1323 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1324 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1325
1326 log(LOG_INFO, "%s: changing name to '%s'\n",
1327 ifp->if_xname, new_name);
1328
1329 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1330 ifa = ifaddr_byindex(ifp->if_index);
1331 IFA_LOCK(ifa);
1332 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1333 namelen = strlen(new_name);
1334 onamelen = sdl->sdl_nlen;
1335 /*
1336 * Move the address if needed. This is safe because we
1337 * allocate space for a name of length IFNAMSIZ when we
1338 * create this in if_attach().
1339 */
1340 if (namelen != onamelen) {
1341 bcopy(sdl->sdl_data + onamelen,
1342 sdl->sdl_data + namelen, sdl->sdl_alen);
1343 }
1344 bcopy(new_name, sdl->sdl_data, namelen);
1345 sdl->sdl_nlen = namelen;
1346 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1347 bzero(sdl->sdl_data, onamelen);
1348 while (namelen != 0)
1349 sdl->sdl_data[--namelen] = 0xff;
1350 IFA_UNLOCK(ifa);
1351
1352 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1353 /* Announce the return of the interface. */
1354 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1355 break;
1356
1357 case SIOCSIFMETRIC:
1358 error = suser(td);
1359 if (error)
1360 return (error);
1361 ifp->if_metric = ifr->ifr_metric;
1362 getmicrotime(&ifp->if_lastchange);
1363 break;
1364
1365 case SIOCSIFPHYS:
1366 error = suser(td);
1367 if (error)
1368 return (error);
1369 if (ifp->if_ioctl == NULL)
1370 return (EOPNOTSUPP);
1371 IFF_LOCKGIANT(ifp);
1372 error = (*ifp->if_ioctl)(ifp, cmd, data);
1373 IFF_UNLOCKGIANT(ifp);
1374 if (error == 0)
1375 getmicrotime(&ifp->if_lastchange);
1376 break;
1377
1378 case SIOCSIFMTU:
1379 {
1380 u_long oldmtu = ifp->if_mtu;
1381
1382 error = suser(td);
1383 if (error)
1384 return (error);
1385 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1386 return (EINVAL);
1387 if (ifp->if_ioctl == NULL)
1388 return (EOPNOTSUPP);
1389 IFF_LOCKGIANT(ifp);
1390 error = (*ifp->if_ioctl)(ifp, cmd, data);
1391 IFF_UNLOCKGIANT(ifp);
1392 if (error == 0) {
1393 getmicrotime(&ifp->if_lastchange);
1394 rt_ifmsg(ifp);
1395 }
1396 /*
1397 * If the link MTU changed, do network layer specific procedure.
1398 */
1399 if (ifp->if_mtu != oldmtu) {
1400 #ifdef INET6
1401 nd6_setmtu(ifp);
1402 #endif
1403 }
1404 break;
1405 }
1406
1407 case SIOCADDMULTI:
1408 case SIOCDELMULTI:
1409 error = suser(td);
1410 if (error)
1411 return (error);
1412
1413 /* Don't allow group membership on non-multicast interfaces. */
1414 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1415 return (EOPNOTSUPP);
1416
1417 /* Don't let users screw up protocols' entries. */
1418 if (ifr->ifr_addr.sa_family != AF_LINK)
1419 return (EINVAL);
1420
1421 if (cmd == SIOCADDMULTI) {
1422 struct ifmultiaddr *ifma;
1423 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1424 } else {
1425 error = if_delmulti(ifp, &ifr->ifr_addr);
1426 }
1427 if (error == 0)
1428 getmicrotime(&ifp->if_lastchange);
1429 break;
1430
1431 case SIOCSIFPHYADDR:
1432 case SIOCDIFPHYADDR:
1433 #ifdef INET6
1434 case SIOCSIFPHYADDR_IN6:
1435 #endif
1436 case SIOCSLIFPHYADDR:
1437 case SIOCSIFMEDIA:
1438 case SIOCSIFGENERIC:
1439 error = suser(td);
1440 if (error)
1441 return (error);
1442 if (ifp->if_ioctl == NULL)
1443 return (EOPNOTSUPP);
1444 IFF_LOCKGIANT(ifp);
1445 error = (*ifp->if_ioctl)(ifp, cmd, data);
1446 IFF_UNLOCKGIANT(ifp);
1447 if (error == 0)
1448 getmicrotime(&ifp->if_lastchange);
1449 break;
1450
1451 case SIOCGIFSTATUS:
1452 ifs = (struct ifstat *)data;
1453 ifs->ascii[0] = '\0';
1454
1455 case SIOCGIFPSRCADDR:
1456 case SIOCGIFPDSTADDR:
1457 case SIOCGLIFPHYADDR:
1458 case SIOCGIFMEDIA:
1459 case SIOCGIFGENERIC:
1460 if (ifp->if_ioctl == NULL)
1461 return (EOPNOTSUPP);
1462 IFF_LOCKGIANT(ifp);
1463 error = (*ifp->if_ioctl)(ifp, cmd, data);
1464 IFF_UNLOCKGIANT(ifp);
1465 break;
1466
1467 case SIOCSIFLLADDR:
1468 error = suser(td);
1469 if (error)
1470 return (error);
1471 error = if_setlladdr(ifp,
1472 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1473 break;
1474
1475 default:
1476 error = ENOIOCTL;
1477 break;
1478 }
1479 return (error);
1480 }
1481
1482 /*
1483 * Interface ioctls.
1484 */
1485 int
1486 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1487 {
1488 struct ifnet *ifp;
1489 struct ifreq *ifr;
1490 int error;
1491 int oif_flags;
1492
1493 switch (cmd) {
1494 case SIOCGIFCONF:
1495 case OSIOCGIFCONF:
1496 #ifdef __amd64__
1497 case SIOCGIFCONF32:
1498 #endif
1499 return (ifconf(cmd, data));
1500 }
1501 ifr = (struct ifreq *)data;
1502
1503 switch (cmd) {
1504 case SIOCIFCREATE:
1505 case SIOCIFDESTROY:
1506 if ((error = suser(td)) != 0)
1507 return (error);
1508 return ((cmd == SIOCIFCREATE) ?
1509 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1510 if_clone_destroy(ifr->ifr_name));
1511
1512 case SIOCIFGCLONERS:
1513 return (if_clone_list((struct if_clonereq *)data));
1514 }
1515
1516 ifp = ifunit(ifr->ifr_name);
1517 if (ifp == 0)
1518 return (ENXIO);
1519
1520 error = ifhwioctl(cmd, ifp, data, td);
1521 if (error != ENOIOCTL)
1522 return (error);
1523
1524 oif_flags = ifp->if_flags;
1525 if (so->so_proto == 0)
1526 return (EOPNOTSUPP);
1527 #ifndef COMPAT_43
1528 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1529 data,
1530 ifp, td));
1531 #else
1532 {
1533 int ocmd = cmd;
1534
1535 switch (cmd) {
1536
1537 case SIOCSIFDSTADDR:
1538 case SIOCSIFADDR:
1539 case SIOCSIFBRDADDR:
1540 case SIOCSIFNETMASK:
1541 #if BYTE_ORDER != BIG_ENDIAN
1542 if (ifr->ifr_addr.sa_family == 0 &&
1543 ifr->ifr_addr.sa_len < 16) {
1544 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1545 ifr->ifr_addr.sa_len = 16;
1546 }
1547 #else
1548 if (ifr->ifr_addr.sa_len == 0)
1549 ifr->ifr_addr.sa_len = 16;
1550 #endif
1551 break;
1552
1553 case OSIOCGIFADDR:
1554 cmd = SIOCGIFADDR;
1555 break;
1556
1557 case OSIOCGIFDSTADDR:
1558 cmd = SIOCGIFDSTADDR;
1559 break;
1560
1561 case OSIOCGIFBRDADDR:
1562 cmd = SIOCGIFBRDADDR;
1563 break;
1564
1565 case OSIOCGIFNETMASK:
1566 cmd = SIOCGIFNETMASK;
1567 }
1568 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1569 cmd,
1570 data,
1571 ifp, td));
1572 switch (ocmd) {
1573
1574 case OSIOCGIFADDR:
1575 case OSIOCGIFDSTADDR:
1576 case OSIOCGIFBRDADDR:
1577 case OSIOCGIFNETMASK:
1578 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1579
1580 }
1581 }
1582 #endif /* COMPAT_43 */
1583
1584 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1585 #ifdef INET6
1586 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1587 if (ifp->if_flags & IFF_UP) {
1588 int s = splimp();
1589 in6_if_up(ifp);
1590 splx(s);
1591 }
1592 #endif
1593 }
1594 return (error);
1595 }
1596
1597 /*
1598 * The code common to handling reference counted flags,
1599 * e.g., in ifpromisc() and if_allmulti().
1600 * The "pflag" argument can specify a permanent mode flag,
1601 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
1602 *
1603 * Only to be used on stack-owned flags, not driver-owned flags.
1604 */
1605 static int
1606 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
1607 {
1608 struct ifreq ifr;
1609 int error;
1610 int oldflags, oldcount;
1611
1612 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
1613 ("if_setflag: setting driver-ownded flag %d", flag));
1614
1615 /* Sanity checks to catch programming errors */
1616 if (onswitch) {
1617 if (*refcount < 0) {
1618 if_printf(ifp,
1619 "refusing to increment negative refcount %d "
1620 "for interface flag %d\n", *refcount, flag);
1621 return (EINVAL);
1622 }
1623 } else {
1624 if (*refcount <= 0) {
1625 if_printf(ifp,
1626 "refusing to decrement non-positive refcount %d"
1627 "for interface flag %d\n", *refcount, flag);
1628 return (EINVAL);
1629 }
1630 }
1631
1632 /* In case this mode is permanent, just touch refcount */
1633 if (ifp->if_flags & pflag) {
1634 *refcount += onswitch ? 1 : -1;
1635 return (0);
1636 }
1637
1638 /* Save ifnet parameters for if_ioctl() may fail */
1639 oldcount = *refcount;
1640 oldflags = ifp->if_flags;
1641
1642 /*
1643 * See if we aren't the only and touching refcount is enough.
1644 * Actually toggle interface flag if we are the first or last.
1645 */
1646 if (onswitch) {
1647 if ((*refcount)++)
1648 return (0);
1649 ifp->if_flags |= flag;
1650 } else {
1651 if (--(*refcount))
1652 return (0);
1653 ifp->if_flags &= ~flag;
1654 }
1655
1656 /* Call down the driver since we've changed interface flags */
1657 if (ifp->if_ioctl == NULL) {
1658 error = EOPNOTSUPP;
1659 goto recover;
1660 }
1661 ifr.ifr_flags = ifp->if_flags & 0xffff;
1662 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1663 IFF_LOCKGIANT(ifp);
1664 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1665 IFF_UNLOCKGIANT(ifp);
1666 if (error)
1667 goto recover;
1668 /* Notify userland that interface flags have changed */
1669 rt_ifmsg(ifp);
1670 return (0);
1671
1672 recover:
1673 /* Recover after driver error */
1674 *refcount = oldcount;
1675 ifp->if_flags = oldflags;
1676 return (error);
1677 }
1678
1679 /*
1680 * Set/clear promiscuous mode on interface ifp based on the truth value
1681 * of pswitch. The calls are reference counted so that only the first
1682 * "on" request actually has an effect, as does the final "off" request.
1683 * Results are undefined if the "off" and "on" requests are not matched.
1684 */
1685 int
1686 ifpromisc(struct ifnet *ifp, int pswitch)
1687 {
1688 int error;
1689 int oldflags = ifp->if_flags;
1690
1691 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
1692 &ifp->if_pcount, pswitch);
1693 /* If promiscuous mode status has changed, log a message */
1694 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
1695 log(LOG_INFO, "%s: promiscuous mode %s\n",
1696 ifp->if_xname,
1697 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1698 return (error);
1699 }
1700
1701 /*
1702 * Return interface configuration
1703 * of system. List may be used
1704 * in later ioctl's (above) to get
1705 * other information.
1706 */
1707 /*ARGSUSED*/
1708 static int
1709 ifconf(u_long cmd, caddr_t data)
1710 {
1711 struct ifconf *ifc = (struct ifconf *)data;
1712 #ifdef __amd64__
1713 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
1714 struct ifconf ifc_swab;
1715 #endif
1716 struct ifnet *ifp;
1717 struct ifaddr *ifa;
1718 struct ifreq ifr;
1719 struct sbuf *sb;
1720 int error, full = 0, valid_len, max_len;
1721
1722 #ifdef __amd64__
1723 if (cmd == SIOCGIFCONF32) {
1724 ifc_swab.ifc_len = ifc32->ifc_len;
1725 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
1726 ifc = &ifc_swab;
1727 }
1728 #endif
1729 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
1730 max_len = MAXPHYS - 1;
1731
1732 /* Prevent hostile input from being able to crash the system */
1733 if (ifc->ifc_len <= 0)
1734 return (EINVAL);
1735
1736 again:
1737 if (ifc->ifc_len <= max_len) {
1738 max_len = ifc->ifc_len;
1739 full = 1;
1740 }
1741 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
1742 max_len = 0;
1743 valid_len = 0;
1744
1745 IFNET_RLOCK(); /* could sleep XXX */
1746 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1747 int addrs;
1748
1749 /*
1750 * Zero the ifr_name buffer to make sure we don't
1751 * disclose the contents of the stack.
1752 */
1753 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
1754
1755 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1756 >= sizeof(ifr.ifr_name)) {
1757 sbuf_delete(sb);
1758 IFNET_RUNLOCK();
1759 return (ENAMETOOLONG);
1760 }
1761
1762 addrs = 0;
1763 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1764 struct sockaddr *sa = ifa->ifa_addr;
1765
1766 if (jailed(curthread->td_ucred) &&
1767 prison_if(curthread->td_ucred, sa))
1768 continue;
1769 addrs++;
1770 #ifdef COMPAT_43
1771 if (cmd == OSIOCGIFCONF) {
1772 struct osockaddr *osa =
1773 (struct osockaddr *)&ifr.ifr_addr;
1774 ifr.ifr_addr = *sa;
1775 osa->sa_family = sa->sa_family;
1776 sbuf_bcat(sb, &ifr, sizeof(ifr));
1777 max_len += sizeof(ifr);
1778 } else
1779 #endif
1780 if (sa->sa_len <= sizeof(*sa)) {
1781 ifr.ifr_addr = *sa;
1782 sbuf_bcat(sb, &ifr, sizeof(ifr));
1783 max_len += sizeof(ifr);
1784 } else {
1785 sbuf_bcat(sb, &ifr,
1786 offsetof(struct ifreq, ifr_addr));
1787 max_len += offsetof(struct ifreq, ifr_addr);
1788 sbuf_bcat(sb, sa, sa->sa_len);
1789 max_len += sa->sa_len;
1790 }
1791
1792 if (!sbuf_overflowed(sb))
1793 valid_len = sbuf_len(sb);
1794 }
1795 if (addrs == 0) {
1796 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1797 sbuf_bcat(sb, &ifr, sizeof(ifr));
1798 max_len += sizeof(ifr);
1799
1800 if (!sbuf_overflowed(sb))
1801 valid_len = sbuf_len(sb);
1802 }
1803 }
1804 IFNET_RUNLOCK();
1805
1806 /*
1807 * If we didn't allocate enough space (uncommon), try again. If
1808 * we have already allocated as much space as we are allowed,
1809 * return what we've got.
1810 */
1811 if (valid_len != max_len && !full) {
1812 sbuf_delete(sb);
1813 goto again;
1814 }
1815
1816 ifc->ifc_len = valid_len;
1817 #ifdef __amd64__
1818 if (cmd == SIOCGIFCONF32)
1819 ifc32->ifc_len = valid_len;
1820 #endif
1821 sbuf_finish(sb);
1822 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
1823 sbuf_delete(sb);
1824 return (error);
1825 }
1826
1827 /*
1828 * Just like ifpromisc(), but for all-multicast-reception mode.
1829 */
1830 int
1831 if_allmulti(struct ifnet *ifp, int onswitch)
1832 {
1833
1834 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
1835 }
1836
1837 static struct ifmultiaddr *
1838 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
1839 {
1840 struct ifmultiaddr *ifma;
1841
1842 IF_ADDR_LOCK_ASSERT(ifp);
1843
1844 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1845 if (sa_equal(ifma->ifma_addr, sa))
1846 break;
1847 }
1848
1849 return ifma;
1850 }
1851
1852 /*
1853 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
1854 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
1855 * the ifnet multicast address list here, so the caller must do that and
1856 * other setup work (such as notifying the device driver). The reference
1857 * count is initialized to 1.
1858 */
1859 static struct ifmultiaddr *
1860 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
1861 int mflags)
1862 {
1863 struct ifmultiaddr *ifma;
1864 struct sockaddr *dupsa;
1865
1866 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
1867 M_ZERO);
1868 if (ifma == NULL)
1869 return (NULL);
1870
1871 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
1872 if (dupsa == NULL) {
1873 FREE(ifma, M_IFMADDR);
1874 return (NULL);
1875 }
1876 bcopy(sa, dupsa, sa->sa_len);
1877 ifma->ifma_addr = dupsa;
1878
1879 ifma->ifma_ifp = ifp;
1880 ifma->ifma_refcount = 1;
1881 ifma->ifma_protospec = NULL;
1882
1883 if (llsa == NULL) {
1884 ifma->ifma_lladdr = NULL;
1885 return (ifma);
1886 }
1887
1888 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
1889 if (dupsa == NULL) {
1890 FREE(ifma->ifma_addr, M_IFMADDR);
1891 FREE(ifma, M_IFMADDR);
1892 return (NULL);
1893 }
1894 bcopy(llsa, dupsa, llsa->sa_len);
1895 ifma->ifma_lladdr = dupsa;
1896
1897 return (ifma);
1898 }
1899
1900 /*
1901 * if_freemulti: free ifmultiaddr structure and possibly attached related
1902 * addresses. The caller is responsible for implementing reference
1903 * counting, notifying the driver, handling routing messages, and releasing
1904 * any dependent link layer state.
1905 */
1906 static void
1907 if_freemulti(struct ifmultiaddr *ifma)
1908 {
1909
1910 KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d",
1911 ifma->ifma_refcount));
1912 KASSERT(ifma->ifma_protospec == NULL,
1913 ("if_freemulti: protospec not NULL"));
1914
1915 if (ifma->ifma_lladdr != NULL)
1916 FREE(ifma->ifma_lladdr, M_IFMADDR);
1917 FREE(ifma->ifma_addr, M_IFMADDR);
1918 FREE(ifma, M_IFMADDR);
1919 }
1920
1921 /*
1922 * Register an additional multicast address with a network interface.
1923 *
1924 * - If the address is already present, bump the reference count on the
1925 * address and return.
1926 * - If the address is not link-layer, look up a link layer address.
1927 * - Allocate address structures for one or both addresses, and attach to the
1928 * multicast address list on the interface. If automatically adding a link
1929 * layer address, the protocol address will own a reference to the link
1930 * layer address, to be freed when it is freed.
1931 * - Notify the network device driver of an addition to the multicast address
1932 * list.
1933 *
1934 * 'sa' points to caller-owned memory with the desired multicast address.
1935 *
1936 * 'retifma' will be used to return a pointer to the resulting multicast
1937 * address reference, if desired.
1938 */
1939 int
1940 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
1941 struct ifmultiaddr **retifma)
1942 {
1943 struct ifmultiaddr *ifma, *ll_ifma;
1944 struct sockaddr *llsa;
1945 int error;
1946
1947 /*
1948 * If the address is already present, return a new reference to it;
1949 * otherwise, allocate storage and set up a new address.
1950 */
1951 IF_ADDR_LOCK(ifp);
1952 ifma = if_findmulti(ifp, sa);
1953 if (ifma != NULL) {
1954 ifma->ifma_refcount++;
1955 if (retifma != NULL)
1956 *retifma = ifma;
1957 IF_ADDR_UNLOCK(ifp);
1958 return (0);
1959 }
1960
1961 /*
1962 * The address isn't already present; resolve the protocol address
1963 * into a link layer address, and then look that up, bump its
1964 * refcount or allocate an ifma for that also. If 'llsa' was
1965 * returned, we will need to free it later.
1966 */
1967 llsa = NULL;
1968 ll_ifma = NULL;
1969 if (ifp->if_resolvemulti != NULL) {
1970 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1971 if (error)
1972 goto unlock_out;
1973 }
1974
1975 /*
1976 * Allocate the new address. Don't hook it up yet, as we may also
1977 * need to allocate a link layer multicast address.
1978 */
1979 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
1980 if (ifma == NULL) {
1981 error = ENOMEM;
1982 goto free_llsa_out;
1983 }
1984
1985 /*
1986 * If a link layer address is found, we'll need to see if it's
1987 * already present in the address list, or allocate is as well.
1988 * When this block finishes, the link layer address will be on the
1989 * list.
1990 */
1991 if (llsa != NULL) {
1992 ll_ifma = if_findmulti(ifp, llsa);
1993 if (ll_ifma == NULL) {
1994 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
1995 if (ll_ifma == NULL) {
1996 if_freemulti(ifma);
1997 error = ENOMEM;
1998 goto free_llsa_out;
1999 }
2000 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2001 ifma_link);
2002 } else
2003 ll_ifma->ifma_refcount++;
2004 }
2005
2006 /*
2007 * We now have a new multicast address, ifma, and possibly a new or
2008 * referenced link layer address. Add the primary address to the
2009 * ifnet address list.
2010 */
2011 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2012
2013 if (retifma != NULL)
2014 *retifma = ifma;
2015
2016 /*
2017 * Must generate the message while holding the lock so that 'ifma'
2018 * pointer is still valid.
2019 *
2020 * XXXRW: How come we don't announce ll_ifma?
2021 */
2022 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2023 IF_ADDR_UNLOCK(ifp);
2024
2025 /*
2026 * We are certain we have added something, so call down to the
2027 * interface to let them know about it.
2028 */
2029 if (ifp->if_ioctl != NULL) {
2030 IFF_LOCKGIANT(ifp);
2031 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2032 IFF_UNLOCKGIANT(ifp);
2033 }
2034
2035 if (llsa != NULL)
2036 FREE(llsa, M_IFMADDR);
2037
2038 return (0);
2039
2040 free_llsa_out:
2041 if (llsa != NULL)
2042 FREE(llsa, M_IFMADDR);
2043
2044 unlock_out:
2045 IF_ADDR_UNLOCK(ifp);
2046 return (error);
2047 }
2048
2049 /*
2050 * Remove a reference to a multicast address on this interface. Yell
2051 * if the request does not match an existing membership.
2052 */
2053 int
2054 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2055 {
2056 struct ifmultiaddr *ifma, *ll_ifma;
2057
2058 IF_ADDR_LOCK(ifp);
2059 ifma = if_findmulti(ifp, sa);
2060 if (ifma == NULL) {
2061 IF_ADDR_UNLOCK(ifp);
2062 return ENOENT;
2063 }
2064
2065 if (ifma->ifma_refcount > 1) {
2066 ifma->ifma_refcount--;
2067 IF_ADDR_UNLOCK(ifp);
2068 return 0;
2069 }
2070
2071 sa = ifma->ifma_lladdr;
2072 if (sa != NULL)
2073 ll_ifma = if_findmulti(ifp, sa);
2074 else
2075 ll_ifma = NULL;
2076
2077 /*
2078 * XXXRW: How come we don't announce ll_ifma?
2079 */
2080 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2081
2082 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2083 if_freemulti(ifma);
2084
2085 if (ll_ifma != NULL) {
2086 if (ll_ifma->ifma_refcount == 1) {
2087 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link);
2088 if_freemulti(ll_ifma);
2089 } else
2090 ll_ifma->ifma_refcount--;
2091 }
2092 IF_ADDR_UNLOCK(ifp);
2093
2094 /*
2095 * Make sure the interface driver is notified
2096 * in the case of a link layer mcast group being left.
2097 */
2098 if (ifp->if_ioctl) {
2099 IFF_LOCKGIANT(ifp);
2100 (void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2101 IFF_UNLOCKGIANT(ifp);
2102 }
2103
2104 return 0;
2105 }
2106
2107 /*
2108 * Set the link layer address on an interface.
2109 *
2110 * At this time we only support certain types of interfaces,
2111 * and we don't allow the length of the address to change.
2112 */
2113 int
2114 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2115 {
2116 struct sockaddr_dl *sdl;
2117 struct ifaddr *ifa;
2118 struct ifreq ifr;
2119
2120 ifa = ifaddr_byindex(ifp->if_index);
2121 if (ifa == NULL)
2122 return (EINVAL);
2123 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2124 if (sdl == NULL)
2125 return (EINVAL);
2126 if (len != sdl->sdl_alen) /* don't allow length to change */
2127 return (EINVAL);
2128 switch (ifp->if_type) {
2129 case IFT_ETHER: /* these types use struct arpcom */
2130 case IFT_FDDI:
2131 case IFT_XETHER:
2132 case IFT_ISO88025:
2133 case IFT_L2VLAN:
2134 case IFT_BRIDGE:
2135 bcopy(lladdr, IFP2ENADDR(ifp), len);
2136 /*
2137 * XXX We also need to store the lladdr in LLADDR(sdl),
2138 * which is done below. This is a pain because we must
2139 * remember to keep the info in sync.
2140 */
2141 /* FALLTHROUGH */
2142 case IFT_ARCNET:
2143 bcopy(lladdr, LLADDR(sdl), len);
2144 break;
2145 default:
2146 return (ENODEV);
2147 }
2148 /*
2149 * If the interface is already up, we need
2150 * to re-init it in order to reprogram its
2151 * address filter.
2152 */
2153 if ((ifp->if_flags & IFF_UP) != 0) {
2154 if (ifp->if_ioctl) {
2155 IFF_LOCKGIANT(ifp);
2156 ifp->if_flags &= ~IFF_UP;
2157 ifr.ifr_flags = ifp->if_flags & 0xffff;
2158 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2159 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2160 ifp->if_flags |= IFF_UP;
2161 ifr.ifr_flags = ifp->if_flags & 0xffff;
2162 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2163 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2164 IFF_UNLOCKGIANT(ifp);
2165 }
2166 #ifdef INET
2167 /*
2168 * Also send gratuitous ARPs to notify other nodes about
2169 * the address change.
2170 */
2171 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2172 if (ifa->ifa_addr != NULL &&
2173 ifa->ifa_addr->sa_family == AF_INET)
2174 arp_ifinit(ifp, ifa);
2175 }
2176 #endif
2177 }
2178 return (0);
2179 }
2180
2181 /*
2182 * The name argument must be a pointer to storage which will last as
2183 * long as the interface does. For physical devices, the result of
2184 * device_get_name(dev) is a good choice and for pseudo-devices a
2185 * static string works well.
2186 */
2187 void
2188 if_initname(struct ifnet *ifp, const char *name, int unit)
2189 {
2190 ifp->if_dname = name;
2191 ifp->if_dunit = unit;
2192 if (unit != IF_DUNIT_NONE)
2193 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2194 else
2195 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2196 }
2197
2198 int
2199 if_printf(struct ifnet *ifp, const char * fmt, ...)
2200 {
2201 va_list ap;
2202 int retval;
2203
2204 retval = printf("%s: ", ifp->if_xname);
2205 va_start(ap, fmt);
2206 retval += vprintf(fmt, ap);
2207 va_end(ap);
2208 return (retval);
2209 }
2210
2211 /*
2212 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2213 * be called without Giant. However, we often can't acquire the Giant lock
2214 * at those points; instead, we run it via a task queue that holds Giant via
2215 * if_start_deferred.
2216 *
2217 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2218 * outstanding if_start_deferred() tasks that will run after the free. This
2219 * probably means waiting in if_detach().
2220 */
2221 void
2222 if_start(struct ifnet *ifp)
2223 {
2224
2225 NET_ASSERT_GIANT();
2226
2227 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
2228 if (mtx_owned(&Giant))
2229 (*(ifp)->if_start)(ifp);
2230 else
2231 taskqueue_enqueue(taskqueue_swi_giant,
2232 &ifp->if_starttask);
2233 } else
2234 (*(ifp)->if_start)(ifp);
2235 }
2236
2237 static void
2238 if_start_deferred(void *context, int pending)
2239 {
2240 struct ifnet *ifp;
2241
2242 /*
2243 * This code must be entered with Giant, and should never run if
2244 * we're not running with debug.mpsafenet.
2245 */
2246 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
2247 GIANT_REQUIRED;
2248
2249 ifp = context;
2250 (ifp->if_start)(ifp);
2251 }
2252
2253 int
2254 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2255 {
2256 int active = 0;
2257
2258 IF_LOCK(ifq);
2259 if (_IF_QFULL(ifq)) {
2260 _IF_DROP(ifq);
2261 IF_UNLOCK(ifq);
2262 m_freem(m);
2263 return (0);
2264 }
2265 if (ifp != NULL) {
2266 ifp->if_obytes += m->m_pkthdr.len + adjust;
2267 if (m->m_flags & (M_BCAST|M_MCAST))
2268 ifp->if_omcasts++;
2269 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2270 }
2271 _IF_ENQUEUE(ifq, m);
2272 IF_UNLOCK(ifq);
2273 if (ifp != NULL && !active)
2274 if_start(ifp);
2275 return (1);
2276 }
2277
2278 void
2279 if_register_com_alloc(u_char type,
2280 if_com_alloc_t *a, if_com_free_t *f)
2281 {
2282
2283 KASSERT(if_com_alloc[type] == NULL,
2284 ("if_register_com_alloc: %d already registered", type));
2285 KASSERT(if_com_free[type] == NULL,
2286 ("if_register_com_alloc: %d free already registered", type));
2287
2288 if_com_alloc[type] = a;
2289 if_com_free[type] = f;
2290 }
2291
2292 void
2293 if_deregister_com_alloc(u_char type)
2294 {
2295
2296 KASSERT(if_com_alloc[type] == NULL,
2297 ("if_deregister_com_alloc: %d not registered", type));
2298 KASSERT(if_com_free[type] == NULL,
2299 ("if_deregister_com_alloc: %d free not registered", type));
2300 if_com_alloc[type] = NULL;
2301 if_com_free[type] = NULL;
2302 }
Cache object: 94f51886a0b32aecab1830c497cbfe86
|