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