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