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