Use system's RT_ROUNDUP and RT_ADVANCE macros instead of local copies.
[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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.3 (Berkeley) 1/4/94
f23061d4 30 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
984263bc
MD
31 */
32
33#include "opt_compat.h"
34#include "opt_inet6.h"
35#include "opt_inet.h"
b3a7093f 36#include "opt_ifpoll.h"
984263bc
MD
37
38#include <sys/param.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/systm.h>
42#include <sys/proc.h>
895c1f85 43#include <sys/priv.h>
6b6e0885 44#include <sys/protosw.h>
984263bc
MD
45#include <sys/socket.h>
46#include <sys/socketvar.h>
6b6e0885 47#include <sys/socketops.h>
984263bc 48#include <sys/kernel.h>
9db4b353 49#include <sys/ktr.h>
9683f229 50#include <sys/mutex.h>
984263bc
MD
51#include <sys/sockio.h>
52#include <sys/syslog.h>
53#include <sys/sysctl.h>
698ac46c 54#include <sys/domain.h>
e9cb6d99 55#include <sys/thread.h>
78195a76 56#include <sys/serialize.h>
71fc104f 57#include <sys/bus.h>
984263bc 58
9683f229
MD
59#include <sys/thread2.h>
60#include <sys/msgport2.h>
61#include <sys/mutex2.h>
62
984263bc
MD
63#include <net/if.h>
64#include <net/if_arp.h>
65#include <net/if_dl.h>
66#include <net/if_types.h>
67#include <net/if_var.h>
4d723e5a 68#include <net/ifq_var.h>
984263bc
MD
69#include <net/radix.h>
70#include <net/route.h>
65a24520 71#include <net/if_clone.h>
5337421c 72#include <net/netisr2.h>
b2632176
SZ
73#include <net/netmsg2.h>
74
d5a2b87c 75#include <machine/atomic.h>
984263bc 76#include <machine/stdarg.h>
b2632176 77#include <machine/smp.h>
984263bc
MD
78
79#if defined(INET) || defined(INET6)
80/*XXX*/
81#include <netinet/in.h>
82#include <netinet/in_var.h>
83#include <netinet/if_ether.h>
84#ifdef INET6
984263bc
MD
85#include <netinet6/in6_var.h>
86#include <netinet6/in6_ifattach.h>
87#endif
88#endif
89
9eee10d0
DRJ
90#if defined(COMPAT_43)
91#include <emulation/43bsd/43bsd_socket.h>
92#endif /* COMPAT_43 */
93
b2632176 94struct netmsg_ifaddr {
002c1265 95 struct netmsg_base base;
b2632176
SZ
96 struct ifaddr *ifa;
97 struct ifnet *ifp;
98 int tail;
99};
100
f0a26983
SZ
101struct ifsubq_stage_head {
102 TAILQ_HEAD(, ifsubq_stage) stg_head;
28cc0c29
SZ
103} __cachealign;
104
984263bc
MD
105/*
106 * System initialization
107 */
698ac46c
HS
108static void if_attachdomain(void *);
109static void if_attachdomain1(struct ifnet *);
436c57ea
SZ
110static int ifconf(u_long, caddr_t, struct ucred *);
111static void ifinit(void *);
90af4fd3 112static void ifnetinit(void *);
436c57ea 113static void if_slowtimo(void *);
3ffea39d 114static void link_rtrequest(int, struct rtentry *);
436c57ea 115static int if_rtdel(struct radix_node *, void *);
984263bc 116
8a248085
SZ
117/* Helper functions */
118static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
72659ed0 119static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
8a248085 120
984263bc
MD
121#ifdef INET6
122/*
123 * XXX: declare here to avoid to include many inet6 related files..
124 * should be more generalized?
125 */
436c57ea 126extern void nd6_setmtu(struct ifnet *);
984263bc
MD
127#endif
128
436c57ea
SZ
129SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
130SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
131
f0a26983
SZ
132static int ifsq_stage_cntmax = 4;
133TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
28cc0c29 134SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
f0a26983 135 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
28cc0c29 136
6517ec3f
SZ
137static int if_stats_compat = 0;
138SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
139 &if_stats_compat, 0, "Compat the old ifnet stats");
140
436c57ea 141SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
b2632176 142/* Must be after netisr_init */
90af4fd3 143SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
436c57ea 144
aeb3c11e
RP
145static if_com_alloc_t *if_com_alloc[256];
146static if_com_free_t *if_com_free[256];
147
436c57ea
SZ
148MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
149MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
cb80735c 150MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
984263bc 151
436c57ea 152int ifqmaxlen = IFQ_MAXLEN;
b64bfcc3 153struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
984263bc 154
436c57ea
SZ
155struct callout if_slowtimo_timer;
156
157int if_index = 0;
158struct ifnet **ifindex2ifnet = NULL;
90af4fd3 159static struct thread ifnet_threads[MAXCPU];
abbb44bb 160
f0a26983 161static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
28cc0c29 162
f0a26983 163#ifdef notyet
9db4b353 164#define IFQ_KTR_STRING "ifq=%p"
5bf48697 165#define IFQ_KTR_ARGS struct ifaltq *ifq
9db4b353
SZ
166#ifndef KTR_IFQ
167#define KTR_IFQ KTR_ALL
168#endif
169KTR_INFO_MASTER(ifq);
5bf48697
AE
170KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
171KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
9db4b353
SZ
172#define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
173
174#define IF_START_KTR_STRING "ifp=%p"
5bf48697 175#define IF_START_KTR_ARGS struct ifnet *ifp
9db4b353
SZ
176#ifndef KTR_IF_START
177#define KTR_IF_START KTR_ALL
178#endif
179KTR_INFO_MASTER(if_start);
180KTR_INFO(KTR_IF_START, if_start, run, 0,
5bf48697 181 IF_START_KTR_STRING, IF_START_KTR_ARGS);
9db4b353 182KTR_INFO(KTR_IF_START, if_start, sched, 1,
5bf48697 183 IF_START_KTR_STRING, IF_START_KTR_ARGS);
9db4b353 184KTR_INFO(KTR_IF_START, if_start, avoid, 2,
5bf48697 185 IF_START_KTR_STRING, IF_START_KTR_ARGS);
9db4b353 186KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
5bf48697 187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
9db4b353 188KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
5bf48697 189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
9db4b353 190#define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
f0a26983 191#endif
9db4b353 192
743da179 193TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
315a7da3 194
984263bc
MD
195/*
196 * Network interface utility routines.
197 *
198 * Routines with ifa_ifwith* names take sockaddr *'s as
199 * parameters.
200 */
201/* ARGSUSED*/
202void
f23061d4 203ifinit(void *dummy)
984263bc
MD
204{
205 struct ifnet *ifp;
984263bc 206
abbb44bb
JS
207 callout_init(&if_slowtimo_timer);
208
4986965b 209 crit_enter();
984263bc 210 TAILQ_FOREACH(ifp, &ifnet, if_link) {
f0a26983 211 if (ifp->if_snd.altq_maxlen == 0) {
b21c2105 212 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
ef9870ec 213 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
984263bc
MD
214 }
215 }
4986965b 216 crit_exit();
abbb44bb 217
984263bc
MD
218 if_slowtimo(0);
219}
220
9db4b353 221static void
f0a26983 222ifsq_ifstart_ipifunc(void *arg)
9db4b353 223{
f0a26983
SZ
224 struct ifaltq_subque *ifsq = arg;
225 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
9db4b353
SZ
226
227 crit_enter();
228 if (lmsg->ms_flags & MSGF_DONE)
f6192acf 229 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
9db4b353
SZ
230 crit_exit();
231}
232
3cab6b0d 233static __inline void
f0a26983 234ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
3cab6b0d 235{
f0a26983
SZ
236 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
237 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
238 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
239 stage->stg_cnt = 0;
240 stage->stg_len = 0;
3cab6b0d
SZ
241}
242
243static __inline void
f0a26983 244ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
3cab6b0d 245{
f0a26983
SZ
246 KKASSERT((stage->stg_flags &
247 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
248 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
249 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
3cab6b0d
SZ
250}
251
9db4b353 252/*
5c593c2a 253 * Schedule ifnet.if_start on the subqueue owner CPU
9db4b353
SZ
254 */
255static void
f0a26983 256ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
9db4b353 257{
9db4b353
SZ
258 int cpu;
259
3cab6b0d 260 if (!force && curthread->td_type == TD_TYPE_NETISR &&
f0a26983
SZ
261 ifsq_stage_cntmax > 0) {
262 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
263
264 stage->stg_cnt = 0;
265 stage->stg_len = 0;
266 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
267 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
268 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
3cab6b0d
SZ
269 return;
270 }
271
f0a26983 272 cpu = ifsq_get_cpuid(ifsq);
9db4b353 273 if (cpu != mycpuid)
f0a26983 274 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
9db4b353 275 else
f0a26983 276 ifsq_ifstart_ipifunc(ifsq);
9db4b353
SZ
277}
278
279/*
280 * NOTE:
5c593c2a
SZ
281 * This function will release ifnet.if_start subqueue interlock,
282 * if ifnet.if_start for the subqueue does not need to be scheduled
9db4b353
SZ
283 */
284static __inline int
f0a26983 285ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
9db4b353 286{
f0a26983 287 if (!running || ifsq_is_empty(ifsq)
9db4b353 288#ifdef ALTQ
f0a26983 289 || ifsq->ifsq_altq->altq_tbr != NULL
9db4b353
SZ
290#endif
291 ) {
f0a26983 292 ALTQ_SQ_LOCK(ifsq);
9db4b353 293 /*
5c593c2a 294 * ifnet.if_start subqueue interlock is released, if:
9db4b353
SZ
295 * 1) Hardware can not take any packets, due to
296 * o interface is marked down
5c593c2a 297 * o hardware queue is full (ifsq_is_oactive)
9db4b353
SZ
298 * Under the second situation, hardware interrupt
299 * or polling(4) will call/schedule ifnet.if_start
5c593c2a
SZ
300 * on the subqueue when hardware queue is ready
301 * 2) There is no packet in the subqueue.
9db4b353 302 * Further ifq_dispatch or ifq_handoff will call/
5c593c2a 303 * schedule ifnet.if_start on the subqueue.
9db4b353
SZ
304 * 3) TBR is used and it does not allow further
305 * dequeueing.
5c593c2a
SZ
306 * TBR callout will call ifnet.if_start on the
307 * subqueue.
9db4b353 308 */
f0a26983
SZ
309 if (!running || !ifsq_data_ready(ifsq)) {
310 ifsq_clr_started(ifsq);
311 ALTQ_SQ_UNLOCK(ifsq);
9db4b353
SZ
312 return 0;
313 }
f0a26983 314 ALTQ_SQ_UNLOCK(ifsq);
9db4b353
SZ
315 }
316 return 1;
317}
318
319static void
f0a26983 320ifsq_ifstart_dispatch(netmsg_t msg)
9db4b353 321{
002c1265 322 struct lwkt_msg *lmsg = &msg->base.lmsg;
f0a26983
SZ
323 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
324 struct ifnet *ifp = ifsq_get_ifp(ifsq);
ac7fc6f0 325 struct globaldata *gd = mycpu;
404c9fd9 326 int running = 0, need_sched;
9db4b353 327
ac7fc6f0
SZ
328 crit_enter_gd(gd);
329
9db4b353 330 lwkt_replymsg(lmsg, 0); /* reply ASAP */
9db4b353 331
ac7fc6f0 332 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
9db4b353 333 /*
5c593c2a 334 * We need to chase the subqueue owner CPU change.
9db4b353 335 */
f0a26983 336 ifsq_ifstart_schedule(ifsq, 1);
ac7fc6f0 337 crit_exit_gd(gd);
404c9fd9 338 return;
9db4b353 339 }
9db4b353 340
bfefe4a6 341 ifsq_serialize_hw(ifsq);
f0a26983
SZ
342 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
343 ifp->if_start(ifp, ifsq);
344 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
404c9fd9 345 running = 1;
9db4b353 346 }
f0a26983 347 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
bfefe4a6 348 ifsq_deserialize_hw(ifsq);
404c9fd9
SZ
349
350 if (need_sched) {
2b2f1d64
SZ
351 /*
352 * More data need to be transmitted, ifnet.if_start is
5c593c2a
SZ
353 * scheduled on the subqueue owner CPU, and we keep going.
354 * NOTE: ifnet.if_start subqueue interlock is not released.
2b2f1d64 355 */
f0a26983 356 ifsq_ifstart_schedule(ifsq, 0);
9db4b353 357 }
ac7fc6f0
SZ
358
359 crit_exit_gd(gd);
9db4b353
SZ
360}
361
362/* Device driver ifnet.if_start helper function */
363void
f0a26983 364ifsq_devstart(struct ifaltq_subque *ifsq)
9db4b353 365{
f0a26983 366 struct ifnet *ifp = ifsq_get_ifp(ifsq);
9db4b353
SZ
367 int running = 0;
368
bfefe4a6 369 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
9db4b353 370
f0a26983
SZ
371 ALTQ_SQ_LOCK(ifsq);
372 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
373 ALTQ_SQ_UNLOCK(ifsq);
9db4b353
SZ
374 return;
375 }
f0a26983
SZ
376 ifsq_set_started(ifsq);
377 ALTQ_SQ_UNLOCK(ifsq);
9db4b353 378
f0a26983 379 ifp->if_start(ifp, ifsq);
9db4b353 380
f0a26983 381 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
9db4b353
SZ
382 running = 1;
383
f0a26983 384 if (ifsq_ifstart_need_schedule(ifsq, running)) {
9db4b353
SZ
385 /*
386 * More data need to be transmitted, ifnet.if_start is
387 * scheduled on ifnet's CPU, and we keep going.
388 * NOTE: ifnet.if_start interlock is not released.
389 */
f0a26983 390 ifsq_ifstart_schedule(ifsq, 0);
9db4b353
SZ
391 }
392}
393
f0a26983
SZ
394void
395if_devstart(struct ifnet *ifp)
396{
397 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
398}
399
2dffecda 400/* Device driver ifnet.if_start schedule helper function */
f0a26983
SZ
401void
402ifsq_devstart_sched(struct ifaltq_subque *ifsq)
403{
404 ifsq_ifstart_schedule(ifsq, 1);
405}
406
2dffecda
SZ
407void
408if_devstart_sched(struct ifnet *ifp)
409{
f0a26983 410 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
2dffecda
SZ
411}
412
a3dd34d2
SZ
413static void
414if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
415{
416 lwkt_serialize_enter(ifp->if_serializer);
417}
418
419static void
420if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
421{
422 lwkt_serialize_exit(ifp->if_serializer);
423}
424
425static int
426if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
427{
428 return lwkt_serialize_try(ifp->if_serializer);
429}
430
2c9effcf
SZ
431#ifdef INVARIANTS
432static void
433if_default_serialize_assert(struct ifnet *ifp,
434 enum ifnet_serialize slz __unused,
435 boolean_t serialized)
436{
437 if (serialized)
438 ASSERT_SERIALIZED(ifp->if_serializer);
439 else
440 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
441}
442#endif
443
984263bc 444/*
78195a76
MD
445 * Attach an interface to the list of "active" interfaces.
446 *
5c593c2a 447 * The serializer is optional.
984263bc
MD
448 */
449void
78195a76 450if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
984263bc
MD
451{
452 unsigned socksize, ifasize;
453 int namelen, masklen;
82ed7fc2
RG
454 struct sockaddr_dl *sdl;
455 struct ifaddr *ifa;
e3e4574a 456 struct ifaltq *ifq;
f0a26983 457 int i, q;
590b8cd4 458
984263bc 459 static int if_indexlim = 8;
984263bc 460
a3dd34d2
SZ
461 if (ifp->if_serialize != NULL) {
462 KASSERT(ifp->if_deserialize != NULL &&
2c9effcf
SZ
463 ifp->if_tryserialize != NULL &&
464 ifp->if_serialize_assert != NULL,
ed20d0e3 465 ("serialize functions are partially setup"));
ae474cfa
SZ
466
467 /*
468 * If the device supplies serialize functions,
469 * then clear if_serializer to catch any invalid
470 * usage of this field.
471 */
472 KASSERT(serializer == NULL,
473 ("both serialize functions and default serializer "
ed20d0e3 474 "are supplied"));
ae474cfa 475 ifp->if_serializer = NULL;
a3dd34d2
SZ
476 } else {
477 KASSERT(ifp->if_deserialize == NULL &&
2c9effcf
SZ
478 ifp->if_tryserialize == NULL &&
479 ifp->if_serialize_assert == NULL,
ed20d0e3 480 ("serialize functions are partially setup"));
a3dd34d2
SZ
481 ifp->if_serialize = if_default_serialize;
482 ifp->if_deserialize = if_default_deserialize;
483 ifp->if_tryserialize = if_default_tryserialize;
2c9effcf
SZ
484#ifdef INVARIANTS
485 ifp->if_serialize_assert = if_default_serialize_assert;
486#endif
ae474cfa
SZ
487
488 /*
489 * The serializer can be passed in from the device,
490 * allowing the same serializer to be used for both
491 * the interrupt interlock and the device queue.
492 * If not specified, the netif structure will use an
493 * embedded serializer.
494 */
495 if (serializer == NULL) {
496 serializer = &ifp->if_default_serializer;
497 lwkt_serialize_init(serializer);
498 }
499 ifp->if_serializer = serializer;
a3dd34d2
SZ
500 }
501
9683f229
MD
502 mtx_init(&ifp->if_ioctl_mtx);
503 mtx_lock(&ifp->if_ioctl_mtx);
504
8e13abe7 505 lwkt_gettoken(&ifnet_token); /* protect if_index and ifnet tailq */
984263bc 506 ifp->if_index = ++if_index;
b2632176 507
984263bc
MD
508 /*
509 * XXX -
510 * The old code would work if the interface passed a pre-existing
511 * chain of ifaddrs to this code. We don't trust our callers to
512 * properly initialize the tailq, however, so we no longer allow
513 * this unlikely case.
514 */
b2632176
SZ
515 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
516 M_IFADDR, M_WAITOK | M_ZERO);
517 for (i = 0; i < ncpus; ++i)
518 TAILQ_INIT(&ifp->if_addrheads[i]);
519
984263bc 520 TAILQ_INIT(&ifp->if_prefixhead);
441d34b2 521 TAILQ_INIT(&ifp->if_multiaddrs);
2097a299 522 TAILQ_INIT(&ifp->if_groups);
984263bc 523 getmicrotime(&ifp->if_lastchange);
141697b6 524 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
590b8cd4 525 unsigned int n;
141697b6 526 struct ifnet **q;
590b8cd4
JH
527
528 if_indexlim <<= 1;
984263bc
MD
529
530 /* grow ifindex2ifnet */
141697b6 531 n = if_indexlim * sizeof(*q);
efda3bd0 532 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
984263bc 533 if (ifindex2ifnet) {
f23061d4 534 bcopy(ifindex2ifnet, q, n/2);
efda3bd0 535 kfree(ifindex2ifnet, M_IFADDR);
984263bc 536 }
141697b6 537 ifindex2ifnet = q;
984263bc
MD
538 }
539
540 ifindex2ifnet[if_index] = ifp;
541
542 /*
543 * create a Link Level name for this device
544 */
3e4a09e7 545 namelen = strlen(ifp->if_xname);
60615e94 546 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
984263bc 547 socksize = masklen + ifp->if_addrlen;
984263bc
MD
548 if (socksize < sizeof(*sdl))
549 socksize = sizeof(*sdl);
4ff4d99f 550 socksize = RT_ROUNDUP(socksize);
590b8cd4 551 ifasize = sizeof(struct ifaddr) + 2 * socksize;
b2632176 552 ifa = ifa_create(ifasize, M_WAITOK);
590b8cd4
JH
553 sdl = (struct sockaddr_dl *)(ifa + 1);
554 sdl->sdl_len = socksize;
555 sdl->sdl_family = AF_LINK;
556 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
557 sdl->sdl_nlen = namelen;
558 sdl->sdl_index = ifp->if_index;
559 sdl->sdl_type = ifp->if_type;
141697b6 560 ifp->if_lladdr = ifa;
590b8cd4
JH
561 ifa->ifa_ifp = ifp;
562 ifa->ifa_rtrequest = link_rtrequest;
563 ifa->ifa_addr = (struct sockaddr *)sdl;
564 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
565 ifa->ifa_netmask = (struct sockaddr *)sdl;
566 sdl->sdl_len = masklen;
567 while (namelen != 0)
568 sdl->sdl_data[--namelen] = 0xff;
b2632176 569 ifa_iflink(ifa, ifp, 0 /* Insert head */);
984263bc 570
e1fcdad7
SZ
571 ifp->if_data_pcpu = kmalloc_cachealign(
572 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
573
2cc2f639
SZ
574 if (ifp->if_mapsubq == NULL)
575 ifp->if_mapsubq = ifq_mapsubq_default;
576
e3e4574a
JS
577 ifq = &ifp->if_snd;
578 ifq->altq_type = 0;
579 ifq->altq_disc = NULL;
580 ifq->altq_flags &= ALTQF_CANTCHANGE;
581 ifq->altq_tbr = NULL;
582 ifq->altq_ifp = ifp;
4d723e5a 583
f0a26983
SZ
584 if (ifq->altq_subq_cnt <= 0)
585 ifq->altq_subq_cnt = 1;
586 ifq->altq_subq = kmalloc_cachealign(
587 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
28cc0c29 588 M_DEVBUF, M_WAITOK | M_ZERO);
28cc0c29 589
f0a26983 590 if (ifq->altq_maxlen == 0) {
b21c2105 591 if_printf(ifp, "driver didn't set altq_maxlen\n");
f0a26983 592 ifq_set_maxlen(ifq, ifqmaxlen);
42fdf81e
SZ
593 }
594
f0a26983
SZ
595 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
596 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
597
598 ALTQ_SQ_LOCK_INIT(ifsq);
599 ifsq->ifsq_index = q;
600
601 ifsq->ifsq_altq = ifq;
602 ifsq->ifsq_ifp = ifp;
603
b21c2105 604 ifsq->ifsq_maxlen = ifq->altq_maxlen;
68dc1916 605 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
f0a26983
SZ
606 ifsq->ifsq_prepended = NULL;
607 ifsq->ifsq_started = 0;
608 ifsq->ifsq_hw_oactive = 0;
609 ifsq_set_cpuid(ifsq, 0);
bfefe4a6
SZ
610 if (ifp->if_serializer != NULL)
611 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
f0a26983
SZ
612
613 ifsq->ifsq_stage =
614 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
615 M_DEVBUF, M_WAITOK | M_ZERO);
616 for (i = 0; i < ncpus; ++i)
617 ifsq->ifsq_stage[i].stg_subq = ifsq;
618
619 ifsq->ifsq_ifstart_nmsg =
620 kmalloc(ncpus * sizeof(struct netmsg_base),
621 M_LWKTMSG, M_WAITOK);
622 for (i = 0; i < ncpus; ++i) {
623 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
624 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
625 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
626 }
627 }
628 ifq_set_classic(ifq);
629
9c70fe43 630 if (!SLIST_EMPTY(&domains))
698ac46c
HS
631 if_attachdomain1(ifp);
632
8e13abe7
MD
633 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
634 lwkt_reltoken(&ifnet_token);
635
984263bc 636 /* Announce the interface. */
8e13abe7
MD
637 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
638 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
984263bc 639 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
9683f229
MD
640
641 mtx_unlock(&ifp->if_ioctl_mtx);
984263bc
MD
642}
643
698ac46c
HS
644static void
645if_attachdomain(void *dummy)
646{
647 struct ifnet *ifp;
698ac46c 648
4986965b
JS
649 crit_enter();
650 TAILQ_FOREACH(ifp, &ifnet, if_list)
698ac46c 651 if_attachdomain1(ifp);
4986965b 652 crit_exit();
698ac46c
HS
653}
654SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
655 if_attachdomain, NULL);
656
657static void
658if_attachdomain1(struct ifnet *ifp)
659{
660 struct domain *dp;
698ac46c 661
4986965b 662 crit_enter();
698ac46c
HS
663
664 /* address family dependent data region */
665 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
9c70fe43 666 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
667 if (dp->dom_ifattach)
668 ifp->if_afdata[dp->dom_family] =
669 (*dp->dom_ifattach)(ifp);
4986965b 670 crit_exit();
698ac46c
HS
671}
672
c727e142
SZ
673/*
674 * Purge all addresses whose type is _not_ AF_LINK
675 */
676void
677if_purgeaddrs_nolink(struct ifnet *ifp)
678{
b2632176
SZ
679 struct ifaddr_container *ifac, *next;
680
681 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
682 ifa_link, next) {
683 struct ifaddr *ifa = ifac->ifa;
c727e142 684
c727e142
SZ
685 /* Leave link ifaddr as it is */
686 if (ifa->ifa_addr->sa_family == AF_LINK)
687 continue;
688#ifdef INET
689 /* XXX: Ugly!! ad hoc just for INET */
690 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
691 struct ifaliasreq ifr;
b2632176
SZ
692#ifdef IFADDR_DEBUG_VERBOSE
693 int i;
694
695 kprintf("purge in4 addr %p: ", ifa);
696 for (i = 0; i < ncpus; ++i)
697 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
698 kprintf("\n");
699#endif
c727e142
SZ
700
701 bzero(&ifr, sizeof ifr);
702 ifr.ifra_addr = *ifa->ifa_addr;
703 if (ifa->ifa_dstaddr)
704 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
705 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
706 NULL) == 0)
707 continue;
708 }
709#endif /* INET */
710#ifdef INET6
711 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
b2632176
SZ
712#ifdef IFADDR_DEBUG_VERBOSE
713 int i;
714
715 kprintf("purge in6 addr %p: ", ifa);
716 for (i = 0; i < ncpus; ++i)
717 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
718 kprintf("\n");
719#endif
720
c727e142
SZ
721 in6_purgeaddr(ifa);
722 /* ifp_addrhead is already updated */
723 continue;
724 }
725#endif /* INET6 */
b2632176
SZ
726 ifa_ifunlink(ifa, ifp);
727 ifa_destroy(ifa);
c727e142
SZ
728 }
729}
730
5804f3d1
SZ
731static void
732ifq_stage_detach_handler(netmsg_t nmsg)
733{
734 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
f0a26983 735 int q;
5804f3d1 736
f0a26983
SZ
737 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
738 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
739 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
740
741 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
742 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
743 }
5804f3d1
SZ
744 lwkt_replymsg(&nmsg->lmsg, 0);
745}
746
747static void
748ifq_stage_detach(struct ifaltq *ifq)
749{
750 struct netmsg_base base;
751 int cpu;
752
753 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
754 ifq_stage_detach_handler);
755 base.lmsg.u.ms_resultp = ifq;
756
757 for (cpu = 0; cpu < ncpus; ++cpu)
ec7f7fc8 758 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
5804f3d1
SZ
759}
760
a29ef6e8
SZ
761struct netmsg_if_rtdel {
762 struct netmsg_base base;
763 struct ifnet *ifp;
764};
765
766static void
767if_rtdel_dispatch(netmsg_t msg)
768{
769 struct netmsg_if_rtdel *rmsg = (void *)msg;
770 int i, nextcpu, cpu;
771
772 cpu = mycpuid;
773 for (i = 1; i <= AF_MAX; i++) {
774 struct radix_node_head *rnh;
775
776 if ((rnh = rt_tables[cpu][i]) == NULL)
777 continue;
778 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
779 }
780
781 nextcpu = cpu + 1;
782 if (nextcpu < ncpus)
9763404f 783 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
a29ef6e8
SZ
784 else
785 lwkt_replymsg(&rmsg->base.lmsg, 0);
786}
787
984263bc
MD
788/*
789 * Detach an interface, removing it from the
790 * list of "active" interfaces.
791 */
792void
f23061d4 793if_detach(struct ifnet *ifp)
984263bc 794{
a29ef6e8 795 struct netmsg_if_rtdel msg;
698ac46c 796 struct domain *dp;
a29ef6e8 797 int q;
984263bc 798
f2bd8b67
JS
799 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
800
984263bc
MD
801 /*
802 * Remove routes and flush queues.
803 */
4986965b 804 crit_enter();
b3a7093f
SZ
805#ifdef IFPOLL_ENABLE
806 if (ifp->if_flags & IFF_NPOLLING)
807 ifpoll_deregister(ifp);
323f031d 808#endif
984263bc
MD
809 if_down(ifp);
810
5b1156d4 811#ifdef ALTQ
4d723e5a
JS
812 if (ifq_is_enabled(&ifp->if_snd))
813 altq_disable(&ifp->if_snd);
814 if (ifq_is_attached(&ifp->if_snd))
815 altq_detach(&ifp->if_snd);
5b1156d4 816#endif
4d723e5a 817
984263bc 818 /*
984263bc
MD
819 * Clean up all addresses.
820 */
141697b6 821 ifp->if_lladdr = NULL;
984263bc 822
c727e142 823 if_purgeaddrs_nolink(ifp);
b2632176 824 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
c727e142
SZ
825 struct ifaddr *ifa;
826
b2632176 827 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
c727e142 828 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
27eaa4f1 829 ("non-link ifaddr is left on if_addrheads"));
984263bc 830
b2632176
SZ
831 ifa_ifunlink(ifa, ifp);
832 ifa_destroy(ifa);
833 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
27eaa4f1 834 ("there are still ifaddrs left on if_addrheads"));
984263bc
MD
835 }
836
a98eb818
JS
837#ifdef INET
838 /*
839 * Remove all IPv4 kernel structures related to ifp.
840 */
841 in_ifdetach(ifp);
842#endif
843
984263bc
MD
844#ifdef INET6
845 /*
846 * Remove all IPv6 kernel structs related to ifp. This should be done
847 * before removing routing entries below, since IPv6 interface direct
848 * routes are expected to be removed by the IPv6-specific kernel API.
849 * Otherwise, the kernel will detect some inconsistency and bark it.
850 */
851 in6_ifdetach(ifp);
852#endif
853
854 /*
855 * Delete all remaining routes using this interface
984263bc 856 */
d20d6787 857 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
a29ef6e8
SZ
858 if_rtdel_dispatch);
859 msg.ifp = ifp;
2c4bf548 860 rt_domsg_global(&msg.base);
984263bc
MD
861
862 /* Announce that the interface is gone. */
863 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
71fc104f 864 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
984263bc 865
9c70fe43 866 SLIST_FOREACH(dp, &domains, dom_next)
698ac46c
HS
867 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
868 (*dp->dom_ifdetach)(ifp,
869 ifp->if_afdata[dp->dom_family]);
698ac46c 870
141697b6
JS
871 /*
872 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
873 */
8e13abe7 874 lwkt_gettoken(&ifnet_token);
75857e7c 875 ifindex2ifnet[ifp->if_index] = NULL;
141697b6
JS
876 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
877 if_index--;
984263bc 878 TAILQ_REMOVE(&ifnet, ifp, if_link);
8e13abe7
MD
879 lwkt_reltoken(&ifnet_token);
880
b2632176 881 kfree(ifp->if_addrheads, M_IFADDR);
5804f3d1
SZ
882
883 lwkt_synchronize_ipiqs("if_detach");
884 ifq_stage_detach(&ifp->if_snd);
885
f0a26983
SZ
886 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
887 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
888
889 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
890 kfree(ifsq->ifsq_stage, M_DEVBUF);
891 }
407cde39
SZ
892 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
893
e1fcdad7
SZ
894 kfree(ifp->if_data_pcpu, M_DEVBUF);
895
4986965b 896 crit_exit();
984263bc
MD
897}
898
315a7da3
JL
899/*
900 * Create interface group without members
901 */
902struct ifg_group *
903if_creategroup(const char *groupname)
904{
905 struct ifg_group *ifg = NULL;
906
907 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
908 M_TEMP, M_NOWAIT)) == NULL)
909 return (NULL);
910
911 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
912 ifg->ifg_refcnt = 0;
913 ifg->ifg_carp_demoted = 0;
914 TAILQ_INIT(&ifg->ifg_members);
915#if NPF > 0
916 pfi_attach_ifgroup(ifg);
917#endif
918 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
919
920 return (ifg);
921}
922
923/*
924 * Add a group to an interface
925 */
926int
927if_addgroup(struct ifnet *ifp, const char *groupname)
928{
929 struct ifg_list *ifgl;
930 struct ifg_group *ifg = NULL;
931 struct ifg_member *ifgm;
932
933 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
934 groupname[strlen(groupname) - 1] <= '9')
935 return (EINVAL);
936
937 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
938 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
939 return (EEXIST);
940
941 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
942 return (ENOMEM);
943
944 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
945 kfree(ifgl, M_TEMP);
946 return (ENOMEM);
947 }
948
949 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
950 if (!strcmp(ifg->ifg_group, groupname))
951 break;
952
953 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
954 kfree(ifgl, M_TEMP);
955 kfree(ifgm, M_TEMP);
956 return (ENOMEM);
957 }
958
959 ifg->ifg_refcnt++;
960 ifgl->ifgl_group = ifg;
961 ifgm->ifgm_ifp = ifp;
962
963 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
964 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
965
966#if NPF > 0
967 pfi_group_change(groupname);
968#endif
969
970 return (0);
971}
972
973/*
974 * Remove a group from an interface
975 */
976int
977if_delgroup(struct ifnet *ifp, const char *groupname)
978{
979 struct ifg_list *ifgl;
980 struct ifg_member *ifgm;
981
982 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
983 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
984 break;
985 if (ifgl == NULL)
986 return (ENOENT);
987
988 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
989
990 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
991 if (ifgm->ifgm_ifp == ifp)
992 break;
993
994 if (ifgm != NULL) {
995 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
996 kfree(ifgm, M_TEMP);
997 }
998
999 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1000 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1001#if NPF > 0
1002 pfi_detach_ifgroup(ifgl->ifgl_group);
1003#endif
1004 kfree(ifgl->ifgl_group, M_TEMP);
1005 }
1006
1007 kfree(ifgl, M_TEMP);
1008
1009#if NPF > 0
1010 pfi_group_change(groupname);
1011#endif
1012
1013 return (0);
1014}
1015
1016/*
1017 * Stores all groups from an interface in memory pointed
1018 * to by data
1019 */
1020int
1021if_getgroup(caddr_t data, struct ifnet *ifp)
1022{
1023 int len, error;
1024 struct ifg_list *ifgl;
1025 struct ifg_req ifgrq, *ifgp;
1026 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1027
1028 if (ifgr->ifgr_len == 0) {
1029 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1030 ifgr->ifgr_len += sizeof(struct ifg_req);
1031 return (0);
1032 }
1033
1034 len = ifgr->ifgr_len;
1035 ifgp = ifgr->ifgr_groups;
1036 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1037 if (len < sizeof(ifgrq))
1038 return (EINVAL);
1039 bzero(&ifgrq, sizeof ifgrq);
1040 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1041 sizeof(ifgrq.ifgrq_group));
1042 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1043 sizeof(struct ifg_req))))
1044 return (error);
1045 len -= sizeof(ifgrq);
1046 ifgp++;
1047 }
1048
1049 return (0);
1050}
1051
1052/*
1053 * Stores all members of a group in memory pointed to by data
1054 */
1055int
1056if_getgroupmembers(caddr_t data)
1057{
1058 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1059 struct ifg_group *ifg;
1060 struct ifg_member *ifgm;
1061 struct ifg_req ifgrq, *ifgp;
1062 int len, error;
1063
1064 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1065 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1066 break;
1067 if (ifg == NULL)
1068 return (ENOENT);
1069
1070 if (ifgr->ifgr_len == 0) {
1071 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1072 ifgr->ifgr_len += sizeof(ifgrq);
1073 return (0);
1074 }
1075
1076 len = ifgr->ifgr_len;
1077 ifgp = ifgr->ifgr_groups;
1078 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1079 if (len < sizeof(ifgrq))
1080 return (EINVAL);
1081 bzero(&ifgrq, sizeof ifgrq);
1082 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1083 sizeof(ifgrq.ifgrq_member));
1084 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1085 sizeof(struct ifg_req))))
1086 return (error);
1087 len -= sizeof(ifgrq);
1088 ifgp++;
1089 }
1090
1091 return (0);
1092}
1093
984263bc
MD
1094/*
1095 * Delete Routes for a Network Interface
f23061d4 1096 *
984263bc
MD
1097 * Called for each routing entry via the rnh->rnh_walktree() call above
1098 * to delete all route entries referencing a detaching network interface.
1099 *
1100 * Arguments:
1101 * rn pointer to node in the routing table
1102 * arg argument passed to rnh->rnh_walktree() - detaching interface
1103 *
1104 * Returns:
1105 * 0 successful
1106 * errno failed - reason indicated
1107 *
1108 */
1109static int
f23061d4 1110if_rtdel(struct radix_node *rn, void *arg)
984263bc
MD
1111{
1112 struct rtentry *rt = (struct rtentry *)rn;
1113 struct ifnet *ifp = arg;
1114 int err;
1115
1116 if (rt->rt_ifp == ifp) {
1117
1118 /*
1119 * Protect (sorta) against walktree recursion problems
1120 * with cloned routes
1121 */
f23061d4 1122 if (!(rt->rt_flags & RTF_UP))
984263bc
MD
1123 return (0);
1124
1125 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1126 rt_mask(rt), rt->rt_flags,
2038fb68 1127 NULL);
984263bc
MD
1128 if (err) {
1129 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1130 }
1131 }
1132
1133 return (0);
1134}
1135
984263bc
MD
1136/*
1137 * Locate an interface based on a complete address.
1138 */
984263bc 1139struct ifaddr *
f23061d4 1140ifa_ifwithaddr(struct sockaddr *addr)
984263bc 1141{
82ed7fc2 1142 struct ifnet *ifp;
984263bc 1143
b2632176
SZ
1144 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1145 struct ifaddr_container *ifac;
1146
1147 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1148 struct ifaddr *ifa = ifac->ifa;
1149
1150 if (ifa->ifa_addr->sa_family != addr->sa_family)
1151 continue;
1152 if (sa_equal(addr, ifa->ifa_addr))
1153 return (ifa);
1154 if ((ifp->if_flags & IFF_BROADCAST) &&
1155 ifa->ifa_broadaddr &&
1156 /* IPv6 doesn't have broadcast */
1157 ifa->ifa_broadaddr->sa_len != 0 &&
1158 sa_equal(ifa->ifa_broadaddr, addr))
1159 return (ifa);
1160 }
984263bc 1161 }
b2632176 1162 return (NULL);
984263bc
MD
1163}
1164/*
1165 * Locate the point to point interface with a given destination address.
1166 */
984263bc 1167struct ifaddr *
f23061d4 1168ifa_ifwithdstaddr(struct sockaddr *addr)
984263bc 1169{
82ed7fc2 1170 struct ifnet *ifp;
984263bc 1171
b2632176
SZ
1172 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1173 struct ifaddr_container *ifac;
1174
1175 if (!(ifp->if_flags & IFF_POINTOPOINT))
1176 continue;
1177
1178 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1179 struct ifaddr *ifa = ifac->ifa;
1180
984263bc
MD
1181 if (ifa->ifa_addr->sa_family != addr->sa_family)
1182 continue;
0c3c561c
JH
1183 if (ifa->ifa_dstaddr &&
1184 sa_equal(addr, ifa->ifa_dstaddr))
984263bc 1185 return (ifa);
b2632176 1186 }
984263bc 1187 }
b2632176 1188 return (NULL);
984263bc
MD
1189}
1190
1191/*
1192 * Find an interface on a specific network. If many, choice
1193 * is most specific found.
1194 */
1195struct ifaddr *
f23061d4 1196ifa_ifwithnet(struct sockaddr *addr)
984263bc 1197{
82ed7fc2 1198 struct ifnet *ifp;
b2632176 1199 struct ifaddr *ifa_maybe = NULL;
984263bc
MD
1200 u_int af = addr->sa_family;
1201 char *addr_data = addr->sa_data, *cplim;
1202
1203 /*
1204 * AF_LINK addresses can be looked up directly by their index number,
1205 * so do that if we can.
1206 */
1207 if (af == AF_LINK) {
b2632176 1208 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
590b8cd4 1209
b2632176
SZ
1210 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1211 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
984263bc
MD
1212 }
1213
1214 /*
1215 * Scan though each interface, looking for ones that have
1216 * addresses in this address family.
1217 */
1218 TAILQ_FOREACH(ifp, &ifnet, if_link) {
b2632176
SZ
1219 struct ifaddr_container *ifac;
1220
1221 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1222 struct ifaddr *ifa = ifac->ifa;
82ed7fc2 1223 char *cp, *cp2, *cp3;
984263bc
MD
1224
1225 if (ifa->ifa_addr->sa_family != af)
1226next: continue;
1227 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1228 /*
1229 * This is a bit broken as it doesn't
1230 * take into account that the remote end may
1231 * be a single node in the network we are
1232 * looking for.
1233 * The trouble is that we don't know the
1234 * netmask for the remote end.
1235 */
0c3c561c
JH
1236 if (ifa->ifa_dstaddr != NULL &&
1237 sa_equal(addr, ifa->ifa_dstaddr))
f23061d4 1238 return (ifa);
984263bc
MD
1239 } else {
1240 /*
1241 * if we have a special address handler,
1242 * then use it instead of the generic one.
1243 */
f23061d4 1244 if (ifa->ifa_claim_addr) {
984263bc
MD
1245 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1246 return (ifa);
1247 } else {
1248 continue;
1249 }
1250 }
1251
1252 /*
1253 * Scan all the bits in the ifa's address.
1254 * If a bit dissagrees with what we are
1255 * looking for, mask it with the netmask
1256 * to see if it really matters.
1257 * (A byte at a time)
1258 */
1259 if (ifa->ifa_netmask == 0)
1260 continue;
1261 cp = addr_data;
1262 cp2 = ifa->ifa_addr->sa_data;
1263 cp3 = ifa->ifa_netmask->sa_data;
590b8cd4
JH
1264 cplim = ifa->ifa_netmask->sa_len +
1265 (char *)ifa->ifa_netmask;
984263bc
MD
1266 while (cp3 < cplim)
1267 if ((*cp++ ^ *cp2++) & *cp3++)
1268 goto next; /* next address! */
1269 /*
1270 * If the netmask of what we just found
1271 * is more specific than what we had before
1272 * (if we had one) then remember the new one
1273 * before continuing to search
1274 * for an even better one.
1275 */
4090d6ff 1276 if (ifa_maybe == NULL ||
f23061d4
JH
1277 rn_refines((char *)ifa->ifa_netmask,
1278 (char *)ifa_maybe->ifa_netmask))
984263bc
MD
1279 ifa_maybe = ifa;
1280 }
1281 }
1282 }
1283 return (ifa_maybe);
1284}
1285
1286/*
1287 * Find an interface address specific to an interface best matching
1288 * a given address.
1289 */
1290struct ifaddr *
f23061d4 1291ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
984263bc 1292{
b2632176 1293 struct ifaddr_container *ifac;
82ed7fc2
RG
1294 char *cp, *cp2, *cp3;
1295 char *cplim;
4090d6ff 1296 struct ifaddr *ifa_maybe = NULL;
984263bc
MD
1297 u_int af = addr->sa_family;
1298
1299 if (af >= AF_MAX)
1300 return (0);
b2632176
SZ
1301 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1302 struct ifaddr *ifa = ifac->ifa;
1303
984263bc
MD
1304 if (ifa->ifa_addr->sa_family != af)
1305 continue;
4090d6ff 1306 if (ifa_maybe == NULL)
984263bc 1307 ifa_maybe = ifa;
0c3c561c
JH
1308 if (ifa->ifa_netmask == NULL) {
1309 if (sa_equal(addr, ifa->ifa_addr) ||
1310 (ifa->ifa_dstaddr != NULL &&
1311 sa_equal(addr, ifa->ifa_dstaddr)))
984263bc
MD
1312 return (ifa);
1313 continue;
1314 }
1315 if (ifp->if_flags & IFF_POINTOPOINT) {
0c3c561c 1316 if (sa_equal(addr, ifa->ifa_dstaddr))
984263bc
MD
1317 return (ifa);
1318 } else {
1319 cp = addr->sa_data;
1320 cp2 = ifa->ifa_addr->sa_data;
1321 cp3 = ifa->ifa_netmask->sa_data;
1322 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1323 for (; cp3 < cplim; cp3++)
1324 if ((*cp++ ^ *cp2++) & *cp3)
1325 break;
1326 if (cp3 == cplim)
1327 return (ifa);
1328 }
1329 }
1330 return (ifa_maybe);
1331}
1332
984263bc
MD
1333/*
1334 * Default action when installing a route with a Link Level gateway.
1335 * Lookup an appropriate real ifa to point to.
1336 * This should be moved to /sys/net/link.c eventually.
1337 */
1338static void
3ffea39d 1339link_rtrequest(int cmd, struct rtentry *rt)
984263bc 1340{
82ed7fc2 1341 struct ifaddr *ifa;
984263bc
MD
1342 struct sockaddr *dst;
1343 struct ifnet *ifp;
1344
f23061d4
JH
1345 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1346 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
984263bc
MD
1347 return;
1348 ifa = ifaof_ifpforaddr(dst, ifp);
f23061d4 1349 if (ifa != NULL) {
984263bc 1350 IFAFREE(rt->rt_ifa);
f23061d4 1351 IFAREF(ifa);
984263bc 1352 rt->rt_ifa = ifa;
984263bc 1353 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
3ffea39d 1354 ifa->ifa_rtrequest(cmd, rt);
984263bc
MD
1355 }
1356}
1357
1358/*
1359 * Mark an interface down and notify protocols of
1360 * the transition.
1361 * NOTE: must be called at splnet or eqivalent.
1362 */
1363void
f23061d4 1364if_unroute(struct ifnet *ifp, int flag, int fam)
984263bc 1365{
b2632176 1366 struct ifaddr_container *ifac;
984263bc
MD
1367
1368 ifp->if_flags &= ~flag;
1369 getmicrotime(&ifp->if_lastchange);
b2632176
SZ
1370 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1371 struct ifaddr *ifa = ifac->ifa;
1372
984263bc 1373 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
91be174d 1374 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
b2632176 1375 }
9275f515 1376 ifq_purge_all(&ifp->if_snd);
984263bc
MD
1377 rt_ifmsg(ifp);
1378}
1379
1380/*
1381 * Mark an interface up and notify protocols of
1382 * the transition.
1383 * NOTE: must be called at splnet or eqivalent.
1384 */
1385void
f23061d4 1386if_route(struct ifnet *ifp, int flag, int fam)
984263bc 1387{
b2632176 1388 struct ifaddr_container *ifac;
984263bc 1389
9275f515 1390 ifq_purge_all(&ifp->if_snd);
984263bc
MD
1391 ifp->if_flags |= flag;
1392 getmicrotime(&ifp->if_lastchange);
b2632176
SZ
1393 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1394 struct ifaddr *ifa = ifac->ifa;
1395
984263bc 1396 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
91be174d 1397 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
b2632176 1398 }
984263bc
MD
1399 rt_ifmsg(ifp);
1400#ifdef INET6
1401 in6_if_up(ifp);
1402#endif
1403}
1404
1405/*
5c703385
MD
1406 * Mark an interface down and notify protocols of the transition. An
1407 * interface going down is also considered to be a synchronizing event.
1408 * We must ensure that all packet processing related to the interface
1409 * has completed before we return so e.g. the caller can free the ifnet
1410 * structure that the mbufs may be referencing.
1411 *
984263bc
MD
1412 * NOTE: must be called at splnet or eqivalent.
1413 */
1414void
f23061d4 1415if_down(struct ifnet *ifp)
984263bc 1416{
984263bc 1417 if_unroute(ifp, IFF_UP, AF_UNSPEC);
5c703385 1418 netmsg_service_sync();
984263bc
MD
1419}
1420
1421/*
1422 * Mark an interface up and notify protocols of
1423 * the transition.
1424 * NOTE: must be called at splnet or eqivalent.
1425 */
1426void
f23061d4 1427if_up(struct ifnet *ifp)
984263bc 1428{
984263bc
MD
1429 if_route(ifp, IFF_UP, AF_UNSPEC);
1430}
1431
6de83abe
SZ
1432/*
1433 * Process a link state change.
1434 * NOTE: must be called at splsoftnet or equivalent.
1435 */
1436void
1437if_link_state_change(struct ifnet *ifp)
1438{
71fc104f
HT
1439 int link_state = ifp->if_link_state;
1440
6de83abe 1441 rt_ifmsg(ifp);
71fc104f
HT
1442 devctl_notify("IFNET", ifp->if_xname,
1443 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
6de83abe
SZ
1444}
1445
984263bc
MD
1446/*
1447 * Handle interface watchdog timer routines. Called
1448 * from softclock, we decrement timers (if set) and
1449 * call the appropriate interface routine on expiration.
1450 */
1451static void
f23061d4 1452if_slowtimo(void *arg)
984263bc 1453{
82ed7fc2 1454 struct ifnet *ifp;
4986965b
JS
1455
1456 crit_enter();
984263bc
MD
1457
1458 TAILQ_FOREACH(ifp, &ifnet, if_link) {
6517ec3f
SZ
1459 if (if_stats_compat) {
1460 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1461 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1462 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1463 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1464 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1465 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1466 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1467 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1468 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1469 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1470 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1471 }
1472
984263bc
MD
1473 if (ifp->if_timer == 0 || --ifp->if_timer)
1474 continue;
78195a76 1475 if (ifp->if_watchdog) {
a3dd34d2 1476 if (ifnet_tryserialize_all(ifp)) {
78195a76 1477 (*ifp->if_watchdog)(ifp);
a3dd34d2 1478 ifnet_deserialize_all(ifp);
78195a76
MD
1479 } else {
1480 /* try again next timeout */
1481 ++ifp->if_timer;
1482 }
1483 }
984263bc 1484 }
4986965b
JS
1485
1486 crit_exit();
1487
abbb44bb 1488 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
984263bc
MD
1489}
1490
1491/*
1492 * Map interface name to
1493 * interface structure pointer.
1494 */
1495struct ifnet *
1496ifunit(const char *name)
1497{
984263bc 1498 struct ifnet *ifp;
984263bc 1499
984263bc 1500 /*
3e4a09e7 1501 * Search all the interfaces for this name/number
984263bc 1502 */
3e4a09e7 1503
984263bc 1504 TAILQ_FOREACH(ifp, &ifnet, if_link) {
3e4a09e7 1505 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
984263bc
MD
1506 break;
1507 }
1508 return (ifp);
1509}
1510
1511
1512/*
1513 * Map interface name in a sockaddr_dl to
1514 * interface structure pointer.
1515 */
1516struct ifnet *
f23061d4 1517if_withname(struct sockaddr *sa)
984263bc
MD
1518{
1519 char ifname[IFNAMSIZ+1];
1520 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1521
1522 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1523 (sdl->sdl_nlen > IFNAMSIZ) )
1524 return NULL;
1525
1526 /*
1527 * ifunit wants a null-terminated name. It may not be null-terminated
1528 * in the sockaddr. We don't want to change the caller's sockaddr,
1529 * and there might not be room to put the trailing null anyway, so we
1530 * make a local copy that we know we can null terminate safely.
1531 */
1532
1533 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1534 ifname[sdl->sdl_nlen] = '\0';
1535 return ifunit(ifname);
1536}
1537
1538
1539/*
1540 * Interface ioctls.
1541 */
1542int
87de5057 1543ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
984263bc 1544{
41c20dac
MD
1545 struct ifnet *ifp;
1546 struct ifreq *ifr;
984263bc
MD
1547 struct ifstat *ifs;
1548 int error;
1549 short oif_flags;
1550 int new_flags;
9683f229
MD
1551#ifdef COMPAT_43
1552 int ocmd;
1553#endif
1fdf0954
HP
1554 size_t namelen, onamelen;
1555 char new_name[IFNAMSIZ];
1556 struct ifaddr *ifa;
1557 struct sockaddr_dl *sdl;
984263bc
MD
1558
1559 switch (cmd) {
984263bc
MD
1560 case SIOCGIFCONF:
1561 case OSIOCGIFCONF:
87de5057 1562 return (ifconf(cmd, data, cred));
9683f229
MD
1563 default:
1564 break;
984263bc 1565 }
9683f229 1566
984263bc
MD
1567 ifr = (struct ifreq *)data;
1568
1569 switch (cmd) {
1570 case SIOCIFCREATE:
c5e14c14
RP
1571 case SIOCIFCREATE2:
1572 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1573 return (error);
1574 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1575 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
984263bc 1576 case SIOCIFDESTROY:
895c1f85 1577 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
984263bc 1578 return (error);
c5e14c14 1579 return (if_clone_destroy(ifr->ifr_name));
984263bc
MD
1580 case SIOCIFGCLONERS:
1581 return (if_clone_list((struct if_clonereq *)data));
9683f229
MD
1582 default:
1583 break;
984263bc
MD
1584 }
1585
9683f229
MD
1586 /*
1587 * Nominal ioctl through interface, lookup the ifp and obtain a
1588 * lock to serialize the ifconfig ioctl operation.
1589 */
984263bc 1590 ifp = ifunit(ifr->ifr_name);
9683f229 1591 if (ifp == NULL)
984263bc 1592 return (ENXIO);
9683f229
MD
1593 error = 0;
1594 mtx_lock(&ifp->if_ioctl_mtx);
984263bc 1595
9683f229 1596 switch (cmd) {
12b71966
PA
1597 case SIOCGIFINDEX:
1598 ifr->ifr_index = ifp->if_index;
1599 break;
1600
984263bc
MD
1601 case SIOCGIFFLAGS:
1602 ifr->ifr_flags = ifp->if_flags;
46f25451 1603 ifr->ifr_flagshigh = ifp->if_flags >> 16;
984263bc
MD
1604 break;
1605
1606 case SIOCGIFCAP:
1607 ifr->ifr_reqcap = ifp->if_capabilities;
1608 ifr->ifr_curcap = ifp->if_capenable;
1609 break;
1610
1611 case SIOCGIFMETRIC:
1612 ifr->ifr_metric = ifp->if_metric;
1613 break;
1614
1615 case SIOCGIFMTU:
1616 ifr->ifr_mtu = ifp->if_mtu;
1617 break;
1618
e41e61d5
SZ
1619 case SIOCGIFTSOLEN:
1620 ifr->ifr_tsolen = ifp->if_tsolen;
1621 break;
1622
315a7da3
JL
1623 case SIOCGIFDATA:
1624 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
9683f229 1625 sizeof(ifp->if_data));
315a7da3
JL
1626 break;
1627
984263bc
MD
1628 case SIOCGIFPHYS:
1629 ifr->ifr_phys = ifp->if_physical;
1630 break;
1631
1630efc5 1632 case SIOCGIFPOLLCPU:
1630efc5 1633 ifr->ifr_pollcpu = -1;
1630efc5
SZ
1634 break;
1635
1636 case SIOCSIFPOLLCPU:
1630efc5
SZ
1637 break;
1638
984263bc 1639 case SIOCSIFFLAGS:
895c1f85 1640 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1641 if (error)
9683f229 1642 break;
984263bc
MD
1643 new_flags = (ifr->ifr_flags & 0xffff) |
1644 (ifr->ifr_flagshigh << 16);
1645 if (ifp->if_flags & IFF_SMART) {
1646 /* Smart drivers twiddle their own routes */
1647 } else if (ifp->if_flags & IFF_UP &&
1648 (new_flags & IFF_UP) == 0) {
4986965b 1649 crit_enter();
984263bc 1650 if_down(ifp);
4986965b 1651 crit_exit();
984263bc
MD
1652 } else if (new_flags & IFF_UP &&
1653 (ifp->if_flags & IFF_UP) == 0) {
4986965b 1654 crit_enter();
984263bc 1655 if_up(ifp);
4986965b 1656 crit_exit();
984263bc 1657 }
9c095379 1658
b3a7093f
SZ
1659#ifdef IFPOLL_ENABLE
1660 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1661 if (new_flags & IFF_NPOLLING)
1662 ifpoll_register(ifp);
1663 else
1664 ifpoll_deregister(ifp);
1665 }
1666#endif
9c095379 1667
984263bc
MD
1668 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1669 (new_flags &~ IFF_CANTCHANGE);
984263bc
MD
1670 if (new_flags & IFF_PPROMISC) {
1671 /* Permanently promiscuous mode requested */
1672 ifp->if_flags |= IFF_PROMISC;
1673 } else if (ifp->if_pcount == 0) {
1674 ifp->if_flags &= ~IFF_PROMISC;
1675 }
78195a76 1676 if (ifp->if_ioctl) {
a3dd34d2 1677 ifnet_serialize_all(ifp);
87de5057 1678 ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1679 ifnet_deserialize_all(ifp);
78195a76 1680 }
984263bc
MD
1681 getmicrotime(&ifp->if_lastchange);
1682 break;
1683
1684 case SIOCSIFCAP:
895c1f85 1685 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1686 if (error)
9683f229
MD
1687 break;
1688 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1689 error = EINVAL;
1690 break;
1691 }
a3dd34d2 1692 ifnet_serialize_all(ifp);
87de5057 1693 ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1694 ifnet_deserialize_all(ifp);
984263bc
MD
1695 break;
1696
f23061d4 1697 case SIOCSIFNAME:
895c1f85 1698 error = priv_check_cred(cred, PRIV_ROOT, 0);
9683f229
MD
1699 if (error)
1700 break;
f23061d4 1701 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
9683f229
MD
1702 if (error)
1703 break;
1704 if (new_name[0] == '\0') {
1705 error = EINVAL;
1706 break;
1707 }
1708 if (ifunit(new_name) != NULL) {
1709 error = EEXIST;
1710 break;
1711 }
f2bd8b67
JS
1712
1713 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
f23061d4
JH
1714
1715 /* Announce the departure of the interface. */
1716 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1717
1718 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
b2632176 1719 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
f23061d4
JH
1720 /* XXX IFA_LOCK(ifa); */
1721 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1722 namelen = strlen(new_name);
1723 onamelen = sdl->sdl_nlen;
1724 /*
1725 * Move the address if needed. This is safe because we
1726 * allocate space for a name of length IFNAMSIZ when we
1727 * create this in if_attach().
1728 */
1729 if (namelen != onamelen) {
1730 bcopy(sdl->sdl_data + onamelen,
1731 sdl->sdl_data + namelen, sdl->sdl_alen);
1732 }
1733 bcopy(new_name, sdl->sdl_data, namelen);
1734 sdl->sdl_nlen = namelen;
1735 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1736 bzero(sdl->sdl_data, onamelen);
1737 while (namelen != 0)
1738 sdl->sdl_data[--namelen] = 0xff;
1739 /* XXX IFA_UNLOCK(ifa) */
f2bd8b67
JS
1740
1741 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
f23061d4
JH
1742
1743 /* Announce the return of the interface. */
1744 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1745 break;
1fdf0954 1746
984263bc 1747 case SIOCSIFMETRIC:
895c1f85 1748 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1749 if (error)
9683f229 1750 break;
984263bc
MD
1751 ifp->if_metric = ifr->ifr_metric;
1752 getmicrotime(&ifp->if_lastchange);
1753 break;
1754
1755 case SIOCSIFPHYS:
895c1f85 1756 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1757 if (error)
9683f229
MD
1758 break;
1759 if (ifp->if_ioctl == NULL) {
1760 error = EOPNOTSUPP;
1761 break;
1762 }
a3dd34d2 1763 ifnet_serialize_all(ifp);
87de5057 1764 error = ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1765 ifnet_deserialize_all(ifp);
984263bc
MD
1766 if (error == 0)
1767 getmicrotime(&ifp->if_lastchange);
9683f229 1768 break;
984263bc
MD
1769
1770 case SIOCSIFMTU:
1771 {
1772 u_long oldmtu = ifp->if_mtu;
1773
895c1f85 1774 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1775 if (error)
9683f229
MD
1776 break;
1777 if (ifp->if_ioctl == NULL) {
1778 error = EOPNOTSUPP;
1779 break;
1780 }
1781 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1782 error = EINVAL;
1783 break;
1784 }
a3dd34d2 1785 ifnet_serialize_all(ifp);
87de5057 1786 error = ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1787 ifnet_deserialize_all(ifp);
984263bc
MD
1788 if (error == 0) {
1789 getmicrotime(&ifp->if_lastchange);
1790 rt_ifmsg(ifp);
1791 }
1792 /*
1793 * If the link MTU changed, do network layer specific procedure.
1794 */
1795 if (ifp->if_mtu != oldmtu) {
1796#ifdef INET6
1797 nd6_setmtu(ifp);
1798#endif
1799 }
9683f229 1800 break;
984263bc
MD
1801 }
1802
e41e61d5
SZ
1803 case SIOCSIFTSOLEN:
1804 error = priv_check_cred(cred, PRIV_ROOT, 0);
1805 if (error)
1806 break;
1807
1808 /* XXX need driver supplied upper limit */
1809 if (ifr->ifr_tsolen <= 0) {
1810 error = EINVAL;
1811 break;
1812 }
1813 ifp->if_tsolen = ifr->ifr_tsolen;
1814 break;
1815
984263bc
MD
1816 case SIOCADDMULTI:
1817 case SIOCDELMULTI:
895c1f85 1818 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1819 if (error)
9683f229 1820 break;
984263bc
MD
1821
1822 /* Don't allow group membership on non-multicast interfaces. */
9683f229
MD
1823 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1824 error = EOPNOTSUPP;
1825 break;
1826 }
984263bc
MD
1827
1828 /* Don't let users screw up protocols' entries. */
9683f229
MD
1829 if (ifr->ifr_addr.sa_family != AF_LINK) {
1830 error = EINVAL;
1831 break;
1832 }
984263bc
MD
1833
1834 if (cmd == SIOCADDMULTI) {
1835 struct ifmultiaddr *ifma;
1836 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1837 } else {
1838 error = if_delmulti(ifp, &ifr->ifr_addr);
1839 }
1840 if (error == 0)
1841 getmicrotime(&ifp->if_lastchange);
9683f229 1842 break;
984263bc
MD
1843
1844 case SIOCSIFPHYADDR:
1845 case SIOCDIFPHYADDR:
1846#ifdef INET6
1847 case SIOCSIFPHYADDR_IN6:
1848#endif
1849 case SIOCSLIFPHYADDR:
1850 case SIOCSIFMEDIA:
1851 case SIOCSIFGENERIC:
895c1f85 1852 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1853 if (error)
9683f229
MD
1854 break;
1855 if (ifp->if_ioctl == 0) {
1856 error = EOPNOTSUPP;
1857 break;
1858 }
a3dd34d2 1859 ifnet_serialize_all(ifp);
87de5057 1860 error = ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1861 ifnet_deserialize_all(ifp);
984263bc
MD
1862 if (error == 0)
1863 getmicrotime(&ifp->if_lastchange);
9683f229 1864 break;
984263bc
MD
1865
1866 case SIOCGIFSTATUS:
1867 ifs = (struct ifstat *)data;
1868 ifs->ascii[0] = '\0';
9683f229 1869 /* fall through */
984263bc
MD
1870 case SIOCGIFPSRCADDR:
1871 case SIOCGIFPDSTADDR:
1872 case SIOCGLIFPHYADDR:
1873 case SIOCGIFMEDIA:
1874 case SIOCGIFGENERIC:
9683f229
MD
1875 if (ifp->if_ioctl == NULL) {
1876 error = EOPNOTSUPP;
1877 break;
1878 }
a3dd34d2 1879 ifnet_serialize_all(ifp);
87de5057 1880 error = ifp->if_ioctl(ifp, cmd, data, cred);
a3dd34d2 1881 ifnet_deserialize_all(ifp);
9683f229 1882 break;
984263bc
MD
1883
1884 case SIOCSIFLLADDR:
895c1f85 1885 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc 1886 if (error)
9683f229
MD
1887 break;
1888 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1889 ifr->ifr_addr.sa_len);
19f10c78 1890 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
9683f229 1891 break;
984263bc
MD
1892
1893 default:
1894 oif_flags = ifp->if_flags;
9683f229
MD
1895 if (so->so_proto == 0) {
1896 error = EOPNOTSUPP;
1897 break;
1898 }
984263bc 1899#ifndef COMPAT_43
04951810 1900 error = so_pru_control_direct(so, cmd, data, ifp);
984263bc 1901#else
9683f229 1902 ocmd = cmd;
984263bc
MD
1903
1904 switch (cmd) {
984263bc
MD
1905 case SIOCSIFDSTADDR:
1906 case SIOCSIFADDR:
1907 case SIOCSIFBRDADDR:
1908 case SIOCSIFNETMASK:
1909#if BYTE_ORDER != BIG_ENDIAN
1910 if (ifr->ifr_addr.sa_family == 0 &&
1911 ifr->ifr_addr.sa_len < 16) {
1912 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1913 ifr->ifr_addr.sa_len = 16;
1914 }
1915#else
1916 if (ifr->ifr_addr.sa_len == 0)
1917 ifr->ifr_addr.sa_len = 16;
1918#endif
1919 break;
984263bc
MD
1920 case OSIOCGIFADDR:
1921 cmd = SIOCGIFADDR;
1922 break;
984263bc
MD
1923 case OSIOCGIFDSTADDR:
1924 cmd = SIOCGIFDSTADDR;
1925 break;
984263bc
MD
1926 case OSIOCGIFBRDADDR:
1927 cmd = SIOCGIFBRDADDR;
1928 break;
984263bc
MD
1929 case OSIOCGIFNETMASK:
1930 cmd = SIOCGIFNETMASK;
9683f229
MD
1931 break;
1932 default:
1933 break;
984263bc 1934 }
984263bc 1935
002c1265
MD
1936 error = so_pru_control_direct(so, cmd, data, ifp);
1937
1938 switch (ocmd) {
984263bc
MD
1939 case OSIOCGIFADDR:
1940 case OSIOCGIFDSTADDR:
1941 case OSIOCGIFBRDADDR:
1942 case OSIOCGIFNETMASK:
1943 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
002c1265 1944 break;
984263bc 1945 }
984263bc
MD
1946#endif /* COMPAT_43 */
1947
1948 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1949#ifdef INET6
1950 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1951 if (ifp->if_flags & IFF_UP) {
4986965b 1952 crit_enter();
984263bc 1953 in6_if_up(ifp);
4986965b 1954 crit_exit();
984263bc
MD
1955 }
1956#endif
1957 }
9683f229 1958 break;
984263bc 1959 }
9683f229
MD
1960
1961 mtx_unlock(&ifp->if_ioctl_mtx);
1962 return (error);
984263bc
MD
1963}
1964
1965/*
1966 * Set/clear promiscuous mode on interface ifp based on the truth value
1967 * of pswitch. The calls are reference counted so that only the first
1968 * "on" request actually has an effect, as does the final "off" request.
1969 * Results are undefined if the "off" and "on" requests are not matched.
1970 */
1971int
f23061d4 1972ifpromisc(struct ifnet *ifp, int pswitch)
984263bc
MD
1973{
1974 struct ifreq ifr;
1975 int error;
1976 int oldflags;
1977
1978 oldflags = ifp->if_flags;
46f25451 1979 if (ifp->if_flags & IFF_PPROMISC) {
984263bc
MD
1980 /* Do nothing if device is in permanently promiscuous mode */
1981 ifp->if_pcount += pswitch ? 1 : -1;
1982 return (0);
1983 }
1984 if (pswitch) {
1985 /*
1986 * If the device is not configured up, we cannot put it in
1987 * promiscuous mode.
1988 */
1989 if ((ifp->if_flags & IFF_UP) == 0)
1990 return (ENETDOWN);
1991 if (ifp->if_pcount++ != 0)
1992 return (0);
1993 ifp->if_flags |= IFF_PROMISC;
3e4a09e7
MD
1994 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1995 ifp->if_xname);
984263bc
MD
1996 } else {
1997 if (--ifp->if_pcount > 0)
1998 return (0);
1999 ifp->if_flags &= ~IFF_PROMISC;
3e4a09e7
MD
2000 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2001 ifp->if_xname);
984263bc
MD
2002 }
2003 ifr.ifr_flags = ifp->if_flags;
46f25451 2004 ifr.ifr_flagshigh = ifp->if_flags >> 16;
a3dd34d2
SZ
2005 ifnet_serialize_all(ifp);
2006 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2007 ifnet_deserialize_all(ifp);
984263bc
MD
2008 if (error == 0)
2009 rt_ifmsg(ifp);
2010 else
2011 ifp->if_flags = oldflags;
2012 return error;
2013}
2014
2015/*
2016 * Return interface configuration
2017 * of system. List may be used
2018 * in later ioctl's (above) to get
2019 * other information.
2020 */
984263bc 2021static int
87de5057 2022ifconf(u_long cmd, caddr_t data, struct ucred *cred)
984263bc 2023{
41c20dac
MD
2024 struct ifconf *ifc = (struct ifconf *)data;
2025 struct ifnet *ifp;
984263bc
MD
2026 struct sockaddr *sa;
2027 struct ifreq ifr, *ifrp;
2028 int space = ifc->ifc_len, error = 0;
2029
2030 ifrp = ifc->ifc_req;
2031 TAILQ_FOREACH(ifp, &ifnet, if_link) {
b2632176 2032 struct ifaddr_container *ifac;
3e4a09e7 2033 int addrs;
984263bc 2034
f23061d4 2035 if (space <= sizeof ifr)
984263bc 2036 break;
623c059e
JS
2037
2038 /*
95f018e8
MD
2039 * Zero the stack declared structure first to prevent
2040 * memory disclosure.
623c059e 2041 */
95f018e8 2042 bzero(&ifr, sizeof(ifr));
3e4a09e7
MD
2043 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2044 >= sizeof(ifr.ifr_name)) {
984263bc
MD
2045 error = ENAMETOOLONG;
2046 break;
984263bc
MD
2047 }
2048
2049 addrs = 0;
b2632176
SZ
2050 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2051 struct ifaddr *ifa = ifac->ifa;
2052
f23061d4 2053 if (space <= sizeof ifr)
984263bc
MD
2054 break;
2055 sa = ifa->ifa_addr;
87de5057
MD
2056 if (cred->cr_prison &&
2057 prison_if(cred, sa))
984263bc
MD
2058 continue;
2059 addrs++;
2060#ifdef COMPAT_43
2061 if (cmd == OSIOCGIFCONF) {
2062 struct osockaddr *osa =
2063 (struct osockaddr *)&ifr.ifr_addr;
2064 ifr.ifr_addr = *sa;
2065 osa->sa_family = sa->sa_family;
f23061d4 2066 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
2067 ifrp++;
2068 } else
2069#endif
2070 if (sa->sa_len <= sizeof(*sa)) {
2071 ifr.ifr_addr = *sa;
f23061d4 2072 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
2073 ifrp++;
2074 } else {
f23061d4 2075 if (space < (sizeof ifr) + sa->sa_len -
984263bc
MD
2076 sizeof(*sa))
2077 break;
2078 space -= sa->sa_len - sizeof(*sa);
f23061d4
JH
2079 error = copyout(&ifr, ifrp,
2080 sizeof ifr.ifr_name);
984263bc 2081 if (error == 0)
f23061d4
JH
2082 error = copyout(sa, &ifrp->ifr_addr,
2083 sa->sa_len);
984263bc
MD
2084 ifrp = (struct ifreq *)
2085 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2086 }
2087 if (error)
2088 break;
f23061d4 2089 space -= sizeof ifr;
984263bc
MD
2090 }
2091 if (error)
2092 break;
2093 if (!addrs) {
f23061d4
JH
2094 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2095 error = copyout(&ifr, ifrp, sizeof ifr);
984263bc
MD
2096 if (error)
2097 break;
f23061d4 2098 space -= sizeof ifr;
984263bc
MD
2099 ifrp++;
2100 }
2101 }
2102 ifc->ifc_len -= space;
2103 return (error);
2104}
2105
2106/*
2107 * Just like if_promisc(), but for all-multicast-reception mode.
2108 */
2109int
f23061d4 2110if_allmulti(struct ifnet *ifp, int onswitch)
984263bc
MD
2111{
2112 int error = 0;
984263bc
MD
2113 struct ifreq ifr;
2114
4986965b
JS
2115 crit_enter();
2116
984263bc
MD
2117 if (onswitch) {
2118 if (ifp->if_amcount++ == 0) {
2119 ifp->if_flags |= IFF_ALLMULTI;
2120 ifr.ifr_flags = ifp->if_flags;
46f25451 2121 ifr.ifr_flagshigh = ifp->if_flags >> 16;
a3dd34d2 2122 ifnet_serialize_all(ifp);
bd4539cc 2123 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2038fb68 2124 NULL);
a3dd34d2 2125 ifnet_deserialize_all(ifp);
984263bc
MD
2126 }
2127 } else {
2128 if (ifp->if_amcount > 1) {
2129 ifp->if_amcount--;
2130 } else {
2131 ifp->if_amcount = 0;
2132 ifp->if_flags &= ~IFF_ALLMULTI;
2133 ifr.ifr_flags = ifp->if_flags;
46f25451 2134 ifr.ifr_flagshigh = ifp->if_flags >> 16;
a3dd34d2 2135 ifnet_serialize_all(ifp);
bd4539cc 2136 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2038fb68 2137 NULL);
a3dd34d2 2138 ifnet_deserialize_all(ifp);
984263bc
MD
2139 }
2140 }
4986965b
JS
2141
2142 crit_exit();
984263bc
MD
2143
2144 if (error == 0)
2145 rt_ifmsg(ifp);
2146 return error;
2147}
2148
2149/*
2150 * Add a multicast listenership to the interface in question.
2151 * The link layer provides a routine which converts
2152 */
2153int
72659ed0
SZ
2154if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2155 struct ifmultiaddr **retifma)
984263bc
MD
2156{
2157 struct sockaddr *llsa, *dupsa;
4986965b 2158 int error;
984263bc
MD
2159 struct ifmultiaddr *ifma;
2160
72659ed0
SZ
2161 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2162
984263bc
MD
2163 /*
2164 * If the matching multicast address already exists
2165 * then don't add a new one, just add a reference
2166 */
441d34b2 2167 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 2168 if (sa_equal(sa, ifma->ifma_addr)) {
984263bc
MD
2169 ifma->ifma_refcount++;
2170 if (retifma)
2171 *retifma = ifma;
2172 return 0;
2173 }
2174 }
2175
2176 /*
2177 * Give the link layer a chance to accept/reject it, and also
2178 * find out which AF_LINK address this maps to, if it isn't one
2179 * already.
2180 */
2181 if (ifp->if_resolvemulti) {
2182 error = ifp->if_resolvemulti(ifp, &llsa, sa);
72659ed0 2183 if (error)
78195a76 2184 return error;
984263bc 2185 } else {
4090d6ff 2186 llsa = NULL;
984263bc
MD
2187 }
2188
884717e1
SW
2189 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2190 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
984263bc
MD
2191 bcopy(sa, dupsa, sa->sa_len);
2192
2193 ifma->ifma_addr = dupsa;
2194 ifma->ifma_lladdr = llsa;
2195 ifma->ifma_ifp = ifp;
2196 ifma->ifma_refcount = 1;
e333f801 2197 ifma->ifma_protospec = NULL;
984263bc
MD
2198 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2199
441d34b2 2200 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
6cd0715f
RP
2201 if (retifma)
2202 *retifma = ifma;
984263bc 2203
4090d6ff 2204 if (llsa != NULL) {
441d34b2 2205 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
0c3c561c 2206 if (sa_equal(ifma->ifma_addr, llsa))
984263bc
MD
2207 break;
2208 }
2209 if (ifma) {
2210 ifma->ifma_refcount++;
2211 } else {
884717e1
SW
2212 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2213 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
984263bc
MD
2214 bcopy(llsa, dupsa, llsa->sa_len);
2215 ifma->ifma_addr = dupsa;
2216 ifma->ifma_ifp = ifp;
2217 ifma->ifma_refcount = 1;
441d34b2 2218 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
984263bc
MD
2219 }
2220 }
2221 /*
2222 * We are certain we have added something, so call down to the
2223 * interface to let them know about it.
2224 */
6cd0715f
RP
2225 if (ifp->if_ioctl)
2226 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
984263bc
MD
2227
2228 return 0;
2229}
2230
72659ed0
SZ
2231int
2232if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2233 struct ifmultiaddr **retifma)
2234{
2235 int error;
2236
2237 ifnet_serialize_all(ifp);
2238 error = if_addmulti_serialized(ifp, sa, retifma);
2239 ifnet_deserialize_all(ifp);
2240
2241 return error;
2242}
2243
984263bc
MD
2244/*
2245 * Remove a reference to a multicast address on this interface. Yell
2246 * if the request does not match an existing membership.
2247 */
72659ed0
SZ
2248static int
2249if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
984263bc
MD
2250{
2251 struct ifmultiaddr *ifma;
984263bc 2252
72659ed0
SZ
2253 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2254
441d34b2 2255 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 2256 if (sa_equal(sa, ifma->ifma_addr))
984263bc 2257 break;
4090d6ff 2258 if (ifma == NULL)
984263bc
MD
2259 return ENOENT;
2260
2261 if (ifma->ifma_refcount > 1) {
2262 ifma->ifma_refcount--;
2263 return 0;
2264 }
2265
2266 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2267 sa = ifma->ifma_lladdr;
441d34b2 2268 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
984263bc
MD
2269 /*
2270 * Make sure the interface driver is notified
2271 * in the case of a link layer mcast group being left.
2272 */
72659ed0 2273 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2038fb68 2274 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
efda3bd0
MD
2275 kfree(ifma->ifma_addr, M_IFMADDR);
2276 kfree(ifma, M_IFMADDR);
4090d6ff 2277 if (sa == NULL)
984263bc
MD
2278 return 0;
2279
2280 /*
2281 * Now look for the link-layer address which corresponds to
2282 * this network address. It had been squirreled away in
2283 * ifma->ifma_lladdr for this purpose (so we don't have
2284 * to call ifp->if_resolvemulti() again), and we saved that
2285 * value in sa above. If some nasty deleted the
2286 * link-layer address out from underneath us, we can deal because
2287 * the address we stored was is not the same as the one which was
2288 * in the record for the link-layer address. (So we don't complain
2289 * in that case.)
2290 */
441d34b2 2291 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 2292 if (sa_equal(sa, ifma->ifma_addr))
984263bc 2293 break;
4090d6ff 2294 if (ifma == NULL)
984263bc
MD
2295 return 0;
2296
2297 if (ifma->ifma_refcount > 1) {
2298 ifma->ifma_refcount--;
2299 return 0;
2300 }
2301
441d34b2 2302 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2038fb68 2303 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
efda3bd0
MD
2304 kfree(ifma->ifma_addr, M_IFMADDR);
2305 kfree(sa, M_IFMADDR);
2306 kfree(ifma, M_IFMADDR);
984263bc
MD
2307
2308 return 0;
2309}
2310
72659ed0
SZ
2311int
2312if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2313{
2314 int error;
2315
2316 ifnet_serialize_all(ifp);
2317 error = if_delmulti_serialized(ifp, sa);
2318 ifnet_deserialize_all(ifp);
2319
2320 return error;
2321}
2322
3976c93a
RP
2323/*
2324 * Delete all multicast group membership for an interface.
2325 * Should be used to quickly flush all multicast filters.
2326 */
2327void
72659ed0 2328if_delallmulti_serialized(struct ifnet *ifp)
3976c93a 2329{
72659ed0
SZ
2330 struct ifmultiaddr *ifma, mark;
2331 struct sockaddr sa;
2332
2333 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2334
2335 bzero(&sa, sizeof(sa));
2336 sa.sa_family = AF_UNSPEC;
2337 sa.sa_len = sizeof(sa);
2338
2339 bzero(&mark, sizeof(mark));
2340 mark.ifma_addr = &sa;
3976c93a 2341
72659ed0 2342 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
72659ed0
SZ
2343 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2344 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2345 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2346 ifma_link);
2347
2348 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2349 continue;
2350
2351 if_delmulti_serialized(ifp, ifma->ifma_addr);
2352 }
89d620aa 2353 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
3976c93a
RP
2354}
2355
2356
984263bc
MD
2357/*
2358 * Set the link layer address on an interface.
2359 *
2360 * At this time we only support certain types of interfaces,
2361 * and we don't allow the length of the address to change.
2362 */
2363int
2364if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2365{
2366 struct sockaddr_dl *sdl;
984263bc
MD
2367 struct ifreq ifr;
2368
f2682cb9 2369 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
2370 if (sdl == NULL)
2371 return (EINVAL);
2372 if (len != sdl->sdl_alen) /* don't allow length to change */
2373 return (EINVAL);
2374 switch (ifp->if_type) {
2375 case IFT_ETHER: /* these types use struct arpcom */
984263bc 2376 case IFT_XETHER:
984263bc
MD
2377 case IFT_L2VLAN:
2378 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
984263bc
MD
2379 bcopy(lladdr, LLADDR(sdl), len);
2380 break;
2381 default:
2382 return (ENODEV);
2383 }
2384 /*
2385 * If the interface is already up, we need
2386 * to re-init it in order to reprogram its
2387 * address filter.
2388 */
a3dd34d2 2389 ifnet_serialize_all(ifp);
984263bc 2390 if ((ifp->if_flags & IFF_UP) != 0) {
c97d9b76 2391#ifdef INET
b2632176 2392 struct ifaddr_container *ifac;
c97d9b76 2393#endif
b2632176 2394
984263bc
MD
2395 ifp->if_flags &= ~IFF_UP;
2396 ifr.ifr_flags = ifp->if_flags;
46f25451 2397 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76 2398 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2038fb68 2399 NULL);
984263bc
MD
2400 ifp->if_flags |= IFF_UP;
2401 ifr.ifr_flags = ifp->if_flags;
46f25451 2402 ifr.ifr_flagshigh = ifp->if_flags >> 16;
78195a76 2403 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2038fb68 2404 NULL);
984263bc
MD
2405#ifdef INET
2406 /*
2407 * Also send gratuitous ARPs to notify other nodes about
2408 * the address change.
2409 */
b2632176
SZ
2410 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2411 struct ifaddr *ifa = ifac->ifa;
2412
984263bc
MD
2413 if (ifa->ifa_addr != NULL &&
2414 ifa->ifa_addr->sa_family == AF_INET)
69b66ae8 2415 arp_gratuitous(ifp, ifa);
984263bc
MD
2416 }
2417#endif
2418 }
a3dd34d2 2419 ifnet_deserialize_all(ifp);
984263bc
MD
2420 return (0);
2421}
2422
2423struct ifmultiaddr *
f23061d4 2424ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
984263bc
MD
2425{
2426 struct ifmultiaddr *ifma;
2427
72659ed0
SZ
2428 /* TODO: need ifnet_serialize_main */
2429 ifnet_serialize_all(ifp);
441d34b2 2430 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
0c3c561c 2431 if (sa_equal(ifma->ifma_addr, sa))
984263bc 2432 break;
72659ed0 2433 ifnet_deserialize_all(ifp);
984263bc
MD
2434
2435 return ifma;
2436}
2437
e9bd1548
MD
2438/*
2439 * This function locates the first real ethernet MAC from a network
2440 * card and loads it into node, returning 0 on success or ENOENT if
2441 * no suitable interfaces were found. It is used by the uuid code to
2442 * generate a unique 6-byte number.
2443 */
2444int
2445if_getanyethermac(uint16_t *node, int minlen)
2446{
2447 struct ifnet *ifp;
2448 struct sockaddr_dl *sdl;
2449
2450 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2451 if (ifp->if_type != IFT_ETHER)
2452 continue;
2453 sdl = IF_LLSOCKADDR(ifp);
2454 if (sdl->sdl_alen < minlen)
2455 continue;
2456 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2457 minlen);
2458 return(0);
2459 }
2460 return (ENOENT);
2461}
2462
1550dfd9
MD
2463/*
2464 * The name argument must be a pointer to storage which will last as
2465 * long as the interface does. For physical devices, the result of
2466 * device_get_name(dev) is a good choice and for pseudo-devices a
2467 * static string works well.
2468 */
2469void
2470if_initname(struct ifnet *ifp, const char *name, int unit)
2471{
3e4a09e7
MD
2472 ifp->if_dname = name;
2473 ifp->if_dunit = unit;
1550dfd9 2474 if (unit != IF_DUNIT_NONE)
f8c7a42d 2475 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1550dfd9
MD
2476 else
2477 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2478}
2479
984263bc
MD
2480int
2481if_printf(struct ifnet *ifp, const char *fmt, ...)
2482{
e2565a42 2483 __va_list ap;
984263bc
MD
2484 int retval;
2485
4b1cf444 2486 retval = kprintf("%s: ", ifp->if_xname);
e2565a42 2487 __va_start(ap, fmt);
379210cb 2488 retval += kvprintf(fmt, ap);
e2565a42 2489 __va_end(ap);
984263bc
MD
2490 return (retval);
2491}
2492
cb80735c
RP
2493struct ifnet *
2494if_alloc(uint8_t type)
2495{
2496 struct ifnet *ifp;
7e395935 2497 size_t size;
cb80735c 2498
7e395935
MD
2499 /*
2500 * XXX temporary hack until arpcom is setup in if_l2com
2501 */
2502 if (type == IFT_ETHER)
2503 size = sizeof(struct arpcom);
2504 else
2505 size = sizeof(struct ifnet);
2506
2507 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
cb80735c
RP
2508
2509 ifp->if_type = type;
2510
aeb3c11e
RP
2511 if (if_com_alloc[type] != NULL) {
2512 ifp->if_l2com = if_com_alloc[type](type, ifp);
2513 if (ifp->if_l2com == NULL) {
2514 kfree(ifp, M_IFNET);
2515 return (NULL);
2516 }
2517 }
cb80735c
RP
2518 return (ifp);
2519}
2520
2521void
2522if_free(struct ifnet *ifp)
2523{
2524 kfree(ifp, M_IFNET);
2525}
2526
b2f93efe
JS
2527void
2528ifq_set_classic(struct ifaltq *ifq)
2529{
2cc2f639
SZ
2530 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2531 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
f0a26983
SZ
2532}
2533
2534void
2cc2f639
SZ
2535ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2536 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
f0a26983
SZ
2537{
2538 int q;
2539
2cc2f639
SZ
2540 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2541 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2542 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2543 KASSERT(request != NULL, ("request is not specified"));
2544
2545 ifq->altq_mapsubq = mapsubq;
f0a26983
SZ
2546 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2547 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2548
2549 ifsq->ifsq_enqueue = enqueue;
2550 ifsq->ifsq_dequeue = dequeue;
2551 ifsq->ifsq_request = request;
2552 }
b2f93efe
JS
2553}
2554
4cc8caef
SZ
2555static void
2556ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2557{
2558 m->m_nextpkt = NULL;
2559 if (ifsq->ifsq_norm_tail == NULL)
2560 ifsq->ifsq_norm_head = m;
2561 else
2562 ifsq->ifsq_norm_tail->m_nextpkt = m;
2563 ifsq->ifsq_norm_tail = m;
2564 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2565}
2566
2567static void
2568ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2569{
2570 m->m_nextpkt = NULL;
2571 if (ifsq->ifsq_prio_tail == NULL)
2572 ifsq->ifsq_prio_head = m;
2573 else
2574 ifsq->ifsq_prio_tail->m_nextpkt = m;
2575 ifsq->ifsq_prio_tail = m;
2576 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2577 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2578}
2579
2580static struct mbuf *
2581ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2582{
2583 struct mbuf *m;
2584
2585 m = ifsq->ifsq_norm_head;
2586 if (m != NULL) {
2587 if ((ifsq->ifsq_norm_head = m->m_nextpkt) == NULL)
2588 ifsq->ifsq_norm_tail = NULL;
2589 m->m_nextpkt = NULL;
2590 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2591 }
2592 return m;
2593}
2594
2595static struct mbuf *
2596ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2597{
2598 struct mbuf *m;
2599
2600 m = ifsq->ifsq_prio_head;
2601 if (m != NULL) {
2602 if ((ifsq->ifsq_prio_head = m->m_nextpkt) == NULL)
2603 ifsq->ifsq_prio_tail = NULL;
2604 m->m_nextpkt = NULL;
2605 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2606 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2607 }
2608 return m;
2609}
2610
9db4b353 2611int
f0a26983
SZ
2612ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2613 struct altq_pktattr *pa __unused)
e3e4574a 2614{
0ec85f2e 2615 M_ASSERTPKTHDR(m);
68dc1916
SZ
2616 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2617 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
4cc8caef
SZ
2618 if ((m->m_flags & M_PRIO) &&
2619 ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen / 2) &&
2620 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt / 2)) {
2621 struct mbuf *m_drop;
2622
2623 /*
2624 * Perform drop-head on normal queue
2625 */
2626 m_drop = ifsq_norm_dequeue(ifsq);
2627 if (m_drop != NULL) {
2628 m_freem(m_drop);
2629 ifsq_prio_enqueue(ifsq, m);
2630 return 0;
2631 }
2632 /* XXX nothing could be dropped? */
2633 }
e3e4574a 2634 m_freem(m);
338bb46c 2635 return ENOBUFS;
e3e4574a 2636 } else {
4cc8caef
SZ
2637 if (m->m_flags & M_PRIO)
2638 ifsq_prio_enqueue(ifsq, m);
338bb46c 2639 else
4cc8caef 2640 ifsq_norm_enqueue(ifsq, m);
338bb46c 2641 return 0;
0ec85f2e 2642 }
e3e4574a
JS
2643}
2644
9db4b353 2645struct mbuf *
6dadc833 2646ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
e3e4574a
JS
2647{
2648 struct mbuf *m;
2649
2650 switch (op) {
2651 case ALTDQ_POLL:
4cc8caef
SZ
2652 m = ifsq->ifsq_prio_head;
2653 if (m == NULL)
2654 m = ifsq->ifsq_norm_head;
e3e4574a 2655 break;
338bb46c 2656
e3e4574a 2657 case ALTDQ_REMOVE:
4cc8caef
SZ
2658 m = ifsq_prio_dequeue(ifsq);
2659 if (m == NULL)
2660 m = ifsq_norm_dequeue(ifsq);
e3e4574a 2661 break;
338bb46c 2662
e3e4574a
JS
2663 default:
2664 panic("unsupported ALTQ dequeue op: %d", op);
2665 }
338bb46c 2666 return m;
e3e4574a
JS
2667}
2668
9db4b353 2669int
f0a26983 2670ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
e3e4574a
JS
2671{
2672 switch (req) {
2673 case ALTRQ_PURGE:
338bb46c
SZ
2674 for (;;) {
2675 struct mbuf *m;
2676
6dadc833 2677 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
338bb46c
SZ
2678 if (m == NULL)
2679 break;
2680 m_freem(m);
2681 }
e3e4574a 2682 break;
338bb46c 2683
e3e4574a 2684 default:
3f625015 2685 panic("unsupported ALTQ request: %d", req);
e3e4574a 2686 }
338bb46c 2687 return 0;
e3e4574a 2688}
b2632176 2689
28cc0c29 2690static void
f0a26983 2691ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
28cc0c29 2692{
f0a26983 2693 struct ifnet *ifp = ifsq_get_ifp(ifsq);
28cc0c29
SZ
2694 int running = 0, need_sched;
2695
2696 /*
5c593c2a
SZ
2697 * Try to do direct ifnet.if_start on the subqueue first, if there is
2698 * contention on the subqueue hardware serializer, ifnet.if_start on
2699 * the subqueue will be scheduled on the subqueue owner CPU.
28cc0c29 2700 */
bfefe4a6 2701 if (!ifsq_tryserialize_hw(ifsq)) {
28cc0c29 2702 /*
5c593c2a
SZ
2703 * Subqueue hardware serializer contention happened,
2704 * ifnet.if_start on the subqueue is scheduled on
2705 * the subqueue owner CPU, and we keep going.
28cc0c29 2706 */
f0a26983 2707 ifsq_ifstart_schedule(ifsq, 1);
28cc0c29
SZ
2708 return;
2709 }
2710
f0a26983
SZ
2711 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2712 ifp->if_start(ifp, ifsq);
2713 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
28cc0c29
SZ
2714 running = 1;
2715 }
f0a26983 2716 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
28cc0c29 2717
bfefe4a6 2718 ifsq_deserialize_hw(ifsq);
28cc0c29
SZ
2719
2720 if (need_sched) {
2721 /*
5c593c2a
SZ
2722 * More data need to be transmitted, ifnet.if_start on the
2723 * subqueue is scheduled on the subqueue owner CPU, and we
2724 * keep going.
2725 * NOTE: ifnet.if_start subqueue interlock is not released.
28cc0c29 2726 */
f0a26983 2727 ifsq_ifstart_schedule(ifsq, force_sched);
28cc0c29
SZ
2728 }
2729}
2730
2aa7f7f8 2731/*
5c593c2a 2732 * Subqeue packets staging mechanism:
2aa7f7f8 2733 *
5c593c2a
SZ
2734 * The packets enqueued into the subqueue are staged to a certain amount
2735 * before the ifnet.if_start on the subqueue is called. In this way, the
2736 * driver could avoid writing to hardware registers upon every packet,
2737 * instead, hardware registers could be written when certain amount of
2738 * packets are put onto hardware TX ring. The measurement on several modern
2739 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2740 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2741 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2742 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2743 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2aa7f7f8 2744 *
5c593c2a 2745 * Subqueue packets staging is performed for two entry points into drivers'
2aa7f7f8 2746 * transmission function:
5c593c2a
SZ
2747 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2748 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2aa7f7f8 2749 *
5c593c2a
SZ
2750 * Subqueue packets staging will be stopped upon any of the following
2751 * conditions:
2aa7f7f8 2752 * - If the count of packets enqueued on the current CPU is great than or
f0a26983 2753 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2aa7f7f8
SZ
2754 * - If the total length of packets enqueued on the current CPU is great
2755 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2756 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2757 * is usually less than hardware's MTU.
5c593c2a
SZ
2758 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2759 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2760 * released.
2aa7f7f8
SZ
2761 * - The if_start_rollup(), which is registered as low priority netisr
2762 * rollup function, is called; probably because no more work is pending
2763 * for netisr.
2764 *
2765 * NOTE:
5c593c2a 2766 * Currently subqueue packet staging is only performed in netisr threads.
2aa7f7f8 2767 */
9db4b353
SZ
2768int
2769ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2770{
2771 struct ifaltq *ifq = &ifp->if_snd;
f0a26983 2772 struct ifaltq_subque *ifsq;
28cc0c29 2773 int error, start = 0, len, mcast = 0, avoid_start = 0;
f0a26983
SZ
2774 struct ifsubq_stage_head *head = NULL;
2775 struct ifsubq_stage *stage = NULL;
ac7fc6f0
SZ
2776 struct globaldata *gd = mycpu;
2777 struct thread *td = gd->gd_curthread;
2778
2779 crit_enter_quick(td);
57dff79c 2780
ac7fc6f0 2781 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
bfefe4a6 2782 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
9db4b353 2783
fe53d127
SZ
2784 len = m->m_pkthdr.len;
2785 if (m->m_flags & M_MCAST)
2786 mcast = 1;
2787
ac7fc6f0 2788 if (td->td_type == TD_TYPE_NETISR) {
f0a26983
SZ
2789 head = &ifsubq_stage_heads[mycpuid];
2790 stage = ifsq_get_stage(ifsq, mycpuid);
28cc0c29 2791
f0a26983
SZ
2792 stage->stg_cnt++;
2793 stage->stg_len += len;
2794 if (stage->stg_cnt < ifsq_stage_cntmax &&
2795 stage->stg_len < (ifp->if_mtu - max_protohdr))
28cc0c29
SZ
2796 avoid_start = 1;
2797 }
2798
f0a26983
SZ
2799 ALTQ_SQ_LOCK(ifsq);
2800 error = ifsq_enqueue_locked(ifsq, m, pa);
9db4b353 2801 if (error) {
f0a26983
SZ
2802 if (!ifsq_data_ready(ifsq)) {
2803 ALTQ_SQ_UNLOCK(ifsq);
ac7fc6f0 2804 crit_exit_quick(td);
087561ef
SZ
2805 return error;
2806 }
28cc0c29 2807 avoid_start = 0;
9db4b353 2808 }
f0a26983 2809 if (!ifsq_is_started(ifsq)) {
28cc0c29 2810 if (avoid_start) {
f0a26983 2811 ALTQ_SQ_UNLOCK(ifsq);
28cc0c29
SZ
2812
2813 KKASSERT(!error);
f0a26983
SZ
2814 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2815 ifsq_stage_insert(head, stage);
28cc0c29 2816
d40991ef 2817 IFNET_STAT_INC(ifp, obytes, len);
28cc0c29 2818 if (mcast)
d40991ef 2819 IFNET_STAT_INC(ifp, omcasts, 1);
ac7fc6f0 2820 crit_exit_quick(td);
28cc0c29
SZ
2821 return error;
2822 }
2823
9db4b353 2824 /*
5c593c2a 2825 * Hold the subqueue interlock of ifnet.if_start
9db4b353 2826 */
f0a26983 2827 ifsq_set_started(ifsq);
9db4b353
SZ
2828 start = 1;
2829 }
f0a26983 2830 ALTQ_SQ_UNLOCK(ifsq);
9db4b353 2831
fe53d127 2832 if (!error) {
d40991ef 2833 IFNET_STAT_INC(ifp, obytes, len);
fe53d127 2834 if (mcast)
d40991ef 2835 IFNET_STAT_INC(ifp, omcasts, 1);
fe53d127 2836 }
9db4b353 2837
28cc0c29 2838 if (stage != NULL) {
f0a26983
SZ
2839 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2840 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3cab6b0d 2841 if (!avoid_start) {
f0a26983
SZ
2842 ifsq_stage_remove(head, stage);
2843 ifsq_ifstart_schedule(ifsq, 1);
3cab6b0d 2844 }
ac7fc6f0 2845 crit_exit_quick(td);
3cab6b0d
SZ
2846 return error;
2847 }
2848
f0a26983
SZ
2849 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2850 ifsq_stage_remove(head, stage);
28cc0c29 2851 } else {
f0a26983
SZ
2852 stage->stg_cnt = 0;
2853 stage->stg_len = 0;
28cc0c29 2854 }
9db4b353
SZ
2855 }
2856
ac7fc6f0
SZ
2857 if (!start) {
2858 crit_exit_quick(td);
087561ef 2859 return error;
ac7fc6f0 2860 }
9db4b353 2861
f0a26983 2862 ifsq_ifstart_try(ifsq, 0);
ac7fc6f0
SZ
2863
2864 crit_exit_quick(td);
087561ef 2865 return error;
9db4b353
SZ
2866}
2867
b2632176
SZ
2868void *
2869ifa_create(int size, int flags)
2870{
2871 struct ifaddr *ifa;
2872 int i;
2873
ed20d0e3 2874 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
b2632176
SZ
2875
2876 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2877 if (ifa == NULL)
2878 return NULL;
2879
7d1c3473
SZ
2880 ifa->ifa_containers =
2881 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
2882 M_IFADDR, M_WAITOK | M_ZERO);
d5a2b87c 2883 ifa->ifa_ncnt = ncpus;
b2632176
SZ
2884 for (i = 0; i < ncpus; ++i) {
2885 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2886
2887 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2888 ifac->ifa = ifa;
2889 ifac->ifa_refcnt = 1;
2890 }
2891#ifdef IFADDR_DEBUG
2892 kprintf("alloc ifa %p %d\n", ifa, size);
2893#endif
2894 return ifa;
2895}
2896
b2632176
SZ
2897void
2898ifac_free(struct ifaddr_container *ifac, int cpu_id)
2899{
d5a2b87c 2900 struct ifaddr *ifa = ifac->ifa;
b2632176
SZ
2901
2902 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2903 KKASSERT(ifac->ifa_refcnt == 0);
40f667f2 2904 KASSERT(ifac->ifa_listmask == 0,
ed20d0e3 2905 ("ifa is still on %#x lists", ifac->ifa_listmask));
b2632176
SZ
2906
2907 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2908
b2632176 2909#ifdef IFADDR_DEBUG_VERBOSE
8967ddc7 2910 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
b2632176
SZ
2911#endif
2912
d5a2b87c 2913 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
ed20d0e3 2914 ("invalid # of ifac, %d", ifa->ifa_ncnt));
d5a2b87c
SZ
2915 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2916#ifdef IFADDR_DEBUG
2917 kprintf("free ifa %p\n", ifa);
2918#endif
2919 kfree(ifa->ifa_containers, M_IFADDR);
2920 kfree(ifa, M_IFADDR);
2921 }
b2632176
SZ
2922}
2923
2924static void
002c1265 2925ifa_iflink_dispatch(netmsg_t nmsg)
b2632176
SZ
2926{
2927 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2928 struct ifaddr *ifa = msg->ifa;
2929 struct ifnet *ifp = msg->ifp;
2930 int cpu = mycpuid;
40f667f2 2931 struct ifaddr_container *ifac;
b2632176
SZ
2932
2933 crit_enter();
23027d35 2934
40f667f2 2935 ifac = &ifa->ifa_containers[cpu];
2adb7bc2 2936 ASSERT_IFAC_VALID(ifac);
40f667f2 2937 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
ed20d0e3 2938 ("ifaddr is on if_addrheads"));
23027d35 2939
40f667f2
SZ
2940 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2941 if (msg->tail)
2942 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2943 else
2944 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
23027d35 2945
b2632176
SZ
2946 crit_exit();
2947
002c1265 2948 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
b2632176
SZ
2949}
2950
2951void
2952ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2953{
2954 struct netmsg_ifaddr msg;
2955
002c1265 2956 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
48e7b118 2957 0, ifa_iflink_dispatch);
b2632176
SZ
2958 msg.ifa = ifa;
2959 msg.ifp = ifp;
2960 msg.tail = tail;
2961
002c1265 2962 ifa_domsg(&msg.base.lmsg, 0);
b2632176
SZ
2963}
2964
2965static void
002c1265 2966ifa_ifunlink_dispatch(netmsg_t nmsg)
b2632176
SZ
2967{
2968 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2969 struct ifaddr *ifa = msg->ifa;
2970 struct ifnet *ifp = msg->ifp;
2971 int cpu = mycpuid;
40f667f2 2972 struct ifaddr_container *ifac;
b2632176
SZ
2973
2974 crit_enter();
23027d35 2975
40f667f2 2976 ifac = &ifa->ifa_containers[cpu];
2adb7bc2 2977 ASSERT_IFAC_VALID(ifac);
40f667f2 2978 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
ed20d0e3 2979 ("ifaddr is not on if_addrhead"));
23027d35 2980
40f667f2
SZ
2981 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2982 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
23027d35 2983
b2632176
SZ
2984 crit_exit();
2985
002c1265 2986 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
b2632176
SZ
2987}
2988
2989void
2990ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2991{
2992 struct netmsg_ifaddr msg;
2993
002c1265 2994 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
48e7b118 2995 0, ifa_ifunlink_dispatch);
b2632176
SZ
2996 msg.ifa = ifa;
2997 msg.ifp = ifp;
2998
002c1265 2999 ifa_domsg(&msg.base.lmsg, 0);
b2632176
SZ
3000}
3001
3002static void
002c1265 3003ifa_destroy_dispatch(netmsg_t nmsg)
b2632176
SZ
3004{
3005 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3006
3007 IFAFREE(msg->ifa);
002c1265 3008 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
b2632176
SZ
3009}
3010
3011void
3012ifa_destroy(struct ifaddr *ifa)
3013{
3014 struct netmsg_ifaddr msg;
3015
002c1265 3016 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
48e7b118 3017 0, ifa_destroy_dispatch);
b2632176
SZ
3018 msg.ifa = ifa;
3019
002c1265 3020 ifa_domsg(&msg.base.lmsg, 0);
b2632176
SZ
3021}
3022
3023struct lwkt_port *
d7944f0b 3024ifnet_portfn(int cpu)
b2632176 3025{
90af4fd3 3026 return &ifnet_threads[cpu].td_msgport;
b2632176
SZ
3027}
3028
c4882b7e
SZ
3029void
3030ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
3031{
ea2e6532
SZ
3032 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
3033
c4882b7e
SZ
3034 if (next_cpu < ncpus)
3035 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
3036 else
3037 lwkt_replymsg(lmsg, 0);
3038}
3039
2a3e1dbd 3040int
c4882b7e
SZ
3041ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
3042{
3043 KKASSERT(cpu < ncpus);
2a3e1dbd 3044 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
c4882b7e
SZ
3045}
3046
8967ddc7
SZ
3047void
3048ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
3049{
3050 KKASSERT(cpu < ncpus);
3051 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
3052}
3053
c3c96e44
MD
3054/*
3055 * Generic netmsg service loop. Some protocols may roll their own but all
3056 * must do the basic command dispatch function call done here.
3057 */
3058static void
3059ifnet_service_loop(void *arg __unused)
3060{
002c1265 3061 netmsg_t msg;
c3c96e44
MD
3062
3063 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
002c1265
MD
3064 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
3065 msg->base.nm_dispatch(msg);
c3c96e44
MD
3066 }
3067}
3068
239bdb58
SZ
3069static void
3070if_start_rollup(void)
3071{
f0a26983
SZ
3072 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3073 struct ifsubq_stage *stage;
28cc0c29 3074
ac7fc6f0
SZ
3075 crit_enter();
3076
f0a26983
SZ
3077 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3078 struct ifaltq_subque *ifsq = stage->stg_subq;
3cab6b0d 3079 int is_sched = 0;
28cc0c29 3080
f0a26983 3081 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3cab6b0d 3082 is_sched = 1;
f0a26983 3083 ifsq_stage_remove(head, stage);
28cc0c29 3084
3cab6b0d 3085 if (is_sched) {
f0a26983 3086 ifsq_ifstart_schedule(ifsq, 1);
3cab6b0d
SZ
3087 } else {
3088 int start = 0;
28cc0c29 3089
f0a26983
SZ
3090 ALTQ_SQ_LOCK(ifsq);
3091 if (!ifsq_is_started(ifsq)) {
3cab6b0d 3092 /*
5c593c2a
SZ
3093 * Hold the subqueue interlock of
3094 * ifnet.if_start
3cab6b0d 3095 */
f0a26983 3096 ifsq_set_started(ifsq);
3cab6b0d
SZ
3097 start = 1;
3098 }
f0a26983 3099 ALTQ_SQ_UNLOCK(ifsq);
3cab6b0d
SZ
3100
3101 if (start)
f0a26983 3102 ifsq_ifstart_try(ifsq, 1);
3cab6b0d 3103 }
f0a26983
SZ
3104 KKASSERT((stage->stg_flags &
3105 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
28cc0c29 3106 }
ac7fc6f0
SZ
3107
3108 crit_exit();
239bdb58 3109}
239bdb58 3110
b2632176 3111static void
90af4fd3 3112ifnetinit(void *dummy __unused)
b2632176
SZ
3113{
3114 int i;
3115
3116 for (i = 0; i < ncpus; ++i) {
90af4fd3 3117 struct thread *thr = &ifnet_threads[i];
b2632176 3118
c3c96e44 3119 lwkt_create(ifnet_service_loop, NULL, NULL,
c068fb59 3120 thr, TDF_NOSTART|TDF_FORCE_SPINPORT|TDF_FIXEDCPU,
392cd266 3121 i, "ifnet %d", i);
b2632176 3122 netmsg_service_port_init(&thr->td_msgport);
c3c96e44 3123 lwkt_schedule(thr);
b2632176 3124 }
28cc0c29
SZ
3125
3126 for (i = 0; i < ncpus; ++i)
f0a26983 3127 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
239bdb58 3128 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
b2632176 3129}
bd08b792
RP
3130
3131struct ifnet *
3132ifnet_byindex(unsigned short idx)
3133{
3134 if (idx > if_index)
3135 return NULL;
3136 return ifindex2ifnet[idx];
3137}
3138
3139struct ifaddr *
3140ifaddr_byindex(unsigned short idx)
3141{
3142 struct ifnet *ifp;
3143
3144 ifp = ifnet_byindex(idx);
ec27babc
RP
3145 if (!ifp)
3146 return NULL;
d79c4535 3147 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
bd08b792 3148}
aeb3c11e
RP
3149
3150void
3151if_register_com_alloc(u_char type,
3152 if_com_alloc_t *a, if_com_free_t *f)
3153{
3154
3155 KASSERT(if_com_alloc[type] == NULL,
3156 ("if_register_com_alloc: %d already registered", type));
3157 KASSERT(if_com_free[type] == NULL,
3158 ("if_register_com_alloc: %d free already registered", type));
3159
3160 if_com_alloc[type] = a;
3161 if_com_free[type] = f;
3162}
3163
3164void
3165if_deregister_com_alloc(u_char type)
3166{
3167
3168 KASSERT(if_com_alloc[type] != NULL,
3169 ("if_deregister_com_alloc: %d not registered", type));
3170 KASSERT(if_com_free[type] != NULL,
3171 ("if_deregister_com_alloc: %d free not registered", type));
3172 if_com_alloc[type] = NULL;
3173 if_com_free[type] = NULL;
3174}
a317449e
SZ
3175
3176int
3177if_ring_count2(int cnt, int cnt_max)
3178{
3179 int shift = 0;
3180
3181 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
ed20d0e3 3182 ("invalid ring count max %d", cnt_max));
a317449e
SZ
3183
3184 if (cnt <= 0)
3185 cnt = cnt_max;
3186 if (cnt > ncpus2)
3187 cnt = ncpus2;
3188 if (cnt > cnt_max)
3189 cnt = cnt_max;
3190
3191 while ((1 << (shift + 1)) <= cnt)
3192 ++shift;
3193 cnt = 1 << shift;
3194
3195 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
ed20d0e3 3196 ("calculate cnt %d, ncpus2 %d, cnt max %d",
a317449e
SZ
3197 cnt, ncpus2, cnt_max));
3198 return cnt;
3199}
b7a0c958
SZ
3200
3201void
3202ifq_set_maxlen(struct ifaltq *ifq, int len)
3203{
f0a26983 3204 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
b7a0c958 3205}
2cc2f639
SZ
3206
3207int
3208ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3209{
3210 return ALTQ_SUBQ_INDEX_DEFAULT;
3211}
8a248085 3212
c3fb75dd
SZ
3213int
3214ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3215{
3216 return (cpuid & ifq->altq_subq_mask);
3217}
3218
8a248085
SZ
3219static void
3220ifsq_watchdog(void *arg)
3221{
3222 struct ifsubq_watchdog *wd = arg;
3223 struct ifnet *ifp;
3224
3225 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3226 goto done;
3227
3228 ifp = ifsq_get_ifp(wd->wd_subq);
3229 if (ifnet_tryserialize_all(ifp)) {
3230 wd->wd_watchdog(wd->wd_subq);
3231 ifnet_deserialize_all(ifp);
3232 } else {
3233 /* try again next timeout */
3234 wd->wd_timer = 1;
3235 }
3236done:
3237 ifsq_watchdog_reset(wd);
3238}
3239
3240static void
3241ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3242{
3243 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3244 ifsq_get_cpuid(wd->wd_subq));
3245}
3246
3247void
3248ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3249 ifsq_watchdog_t watchdog)
3250{
3251 callout_init_mp(&wd->wd_callout);
3252 wd->wd_timer = 0;
3253 wd->wd_subq = ifsq;
3254 wd->wd_watchdog = watchdog;
3255}
3256
3257void
3258ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3259{
3260 wd->wd_timer = 0;
3261 ifsq_watchdog_reset(wd);
3262}
3263
3264void
3265ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3266{
3267 wd->wd_timer = 0;
3268 callout_stop(&wd->wd_callout);
3269}