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