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