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