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