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