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