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