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