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