/
if.c
3223 lines (2729 loc) · 66.9 KB
/
if.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: if.c,v 1.618 2020/08/05 11:07:34 mvs Exp $ */
/* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1980, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)if.c 8.3 (Berkeley) 1/4/94
*/
#include "bpfilter.h"
#include "bridge.h"
#include "carp.h"
#include "ether.h"
#include "pf.h"
#include "pfsync.h"
#include "ppp.h"
#include "pppoe.h"
#include "switch.h"
#include "if_wg.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/timeout.h>
#include <sys/protosw.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/domain.h>
#include <sys/task.h>
#include <sys/atomic.h>
#include <sys/percpu.h>
#include <sys/proc.h>
#include <sys/stdint.h> /* uintptr_t */
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/netisr.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/igmp.h>
#ifdef MROUTING
#include <netinet/ip_mroute.h>
#endif
#ifdef INET6
#include <netinet6/in6_var.h>
#include <netinet6/in6_ifattach.h>
#include <netinet6/nd6.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#endif
#ifdef MPLS
#include <netmpls/mpls.h>
#endif
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#if NBRIDGE > 0
#include <net/if_bridge.h>
#endif
#if NCARP > 0
#include <netinet/ip_carp.h>
#endif
#if NPF > 0
#include <net/pfvar.h>
#endif
#include <sys/device.h>
void if_attachsetup(struct ifnet *);
void if_attachdomain(struct ifnet *);
void if_attach_common(struct ifnet *);
int if_createrdomain(int, struct ifnet *);
int if_setrdomain(struct ifnet *, int);
void if_slowtimo(void *);
void if_detached_qstart(struct ifqueue *);
int if_detached_ioctl(struct ifnet *, u_long, caddr_t);
int ifioctl_get(u_long, caddr_t);
int ifconf(caddr_t);
static int
if_sffpage_check(const caddr_t);
int if_getgroup(caddr_t, struct ifnet *);
int if_getgroupmembers(caddr_t);
int if_getgroupattribs(caddr_t);
int if_setgroupattribs(caddr_t);
int if_getgrouplist(caddr_t);
void if_linkstate(struct ifnet *);
void if_linkstate_task(void *);
int if_clone_list(struct if_clonereq *);
struct if_clone *if_clone_lookup(const char *, int *);
int if_group_egress_build(void);
void if_watchdog_task(void *);
void if_netisr(void *);
#ifdef DDB
void ifa_print_all(void);
#endif
void if_qstart_compat(struct ifqueue *);
/*
* interface index map
*
* the kernel maintains a mapping of interface indexes to struct ifnet
* pointers.
*
* the map is an array of struct ifnet pointers prefixed by an if_map
* structure. the if_map structure stores the length of its array.
*
* as interfaces are attached to the system, the map is grown on demand
* up to USHRT_MAX entries.
*
* interface index 0 is reserved and represents no interface. this
* supports the use of the interface index as the scope for IPv6 link
* local addresses, where scope 0 means no scope has been specified.
* it also supports the use of interface index as the unique identifier
* for network interfaces in SNMP applications as per RFC2863. therefore
* if_get(0) returns NULL.
*/
void if_ifp_dtor(void *, void *);
void if_map_dtor(void *, void *);
struct ifnet *if_ref(struct ifnet *);
/*
* struct if_map
*
* bounded array of ifnet srp pointers used to fetch references of live
* interfaces with if_get().
*/
struct if_map {
unsigned long limit;
/* followed by limit ifnet srp pointers */
};
/*
* struct if_idxmap
*
* infrastructure to manage updates and accesses to the current if_map.
*/
struct if_idxmap {
unsigned int serial;
unsigned int count;
struct srp map;
};
void if_idxmap_init(unsigned int);
void if_idxmap_insert(struct ifnet *);
void if_idxmap_remove(struct ifnet *);
TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
int if_cloners_count;
/* hooks should only be added, deleted, and run from a process context */
struct mutex if_hooks_mtx = MUTEX_INITIALIZER(IPL_NONE);
void if_hooks_run(struct task_list *);
int ifq_congestion;
int netisr;
#define NET_TASKQ 1
struct taskq *nettqmp[NET_TASKQ];
struct task if_input_task_locked = TASK_INITIALIZER(if_netisr, NULL);
/*
* Serialize socket operations to ensure no new sleeping points
* are introduced in IP output paths.
*/
struct rwlock netlock = RWLOCK_INITIALIZER("netlock");
/*
* Network interface utility routines.
*/
void
ifinit(void)
{
unsigned int i;
/*
* most machines boot with 4 or 5 interfaces, so size the initial map
* to accomodate this
*/
if_idxmap_init(8);
for (i = 0; i < NET_TASKQ; i++) {
nettqmp[i] = taskq_create("softnet", 1, IPL_NET, TASKQ_MPSAFE);
if (nettqmp[i] == NULL)
panic("unable to create network taskq %d", i);
}
}
static struct if_idxmap if_idxmap = {
0,
0,
SRP_INITIALIZER()
};
struct srp_gc if_ifp_gc = SRP_GC_INITIALIZER(if_ifp_dtor, NULL);
struct srp_gc if_map_gc = SRP_GC_INITIALIZER(if_map_dtor, NULL);
struct ifnet_head ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
void
if_idxmap_init(unsigned int limit)
{
struct if_map *if_map;
struct srp *map;
unsigned int i;
if_idxmap.serial = 1; /* skip ifidx 0 so it can return NULL */
if_map = malloc(sizeof(*if_map) + limit * sizeof(*map),
M_IFADDR, M_WAITOK);
if_map->limit = limit;
map = (struct srp *)(if_map + 1);
for (i = 0; i < limit; i++)
srp_init(&map[i]);
/* this is called early so there's nothing to race with */
srp_update_locked(&if_map_gc, &if_idxmap.map, if_map);
}
void
if_idxmap_insert(struct ifnet *ifp)
{
struct if_map *if_map;
struct srp *map;
unsigned int index, i;
refcnt_init(&ifp->if_refcnt);
/* the kernel lock guarantees serialised modifications to if_idxmap */
KERNEL_ASSERT_LOCKED();
if (++if_idxmap.count > USHRT_MAX)
panic("too many interfaces");
if_map = srp_get_locked(&if_idxmap.map);
map = (struct srp *)(if_map + 1);
index = if_idxmap.serial++ & USHRT_MAX;
if (index >= if_map->limit) {
struct if_map *nif_map;
struct srp *nmap;
unsigned int nlimit;
struct ifnet *nifp;
nlimit = if_map->limit * 2;
nif_map = malloc(sizeof(*nif_map) + nlimit * sizeof(*nmap),
M_IFADDR, M_WAITOK);
nmap = (struct srp *)(nif_map + 1);
nif_map->limit = nlimit;
for (i = 0; i < if_map->limit; i++) {
srp_init(&nmap[i]);
nifp = srp_get_locked(&map[i]);
if (nifp != NULL) {
srp_update_locked(&if_ifp_gc, &nmap[i],
if_ref(nifp));
}
}
while (i < nlimit) {
srp_init(&nmap[i]);
i++;
}
srp_update_locked(&if_map_gc, &if_idxmap.map, nif_map);
if_map = nif_map;
map = nmap;
}
/* pick the next free index */
for (i = 0; i < USHRT_MAX; i++) {
if (index != 0 && srp_get_locked(&map[index]) == NULL)
break;
index = if_idxmap.serial++ & USHRT_MAX;
}
/* commit */
ifp->if_index = index;
srp_update_locked(&if_ifp_gc, &map[index], if_ref(ifp));
}
void
if_idxmap_remove(struct ifnet *ifp)
{
struct if_map *if_map;
struct srp *map;
unsigned int index;
index = ifp->if_index;
/* the kernel lock guarantees serialised modifications to if_idxmap */
KERNEL_ASSERT_LOCKED();
if_map = srp_get_locked(&if_idxmap.map);
KASSERT(index < if_map->limit);
map = (struct srp *)(if_map + 1);
KASSERT(ifp == (struct ifnet *)srp_get_locked(&map[index]));
srp_update_locked(&if_ifp_gc, &map[index], NULL);
if_idxmap.count--;
/* end of if_idxmap modifications */
/* sleep until the last reference is released */
refcnt_finalize(&ifp->if_refcnt, "ifidxrm");
}
void
if_ifp_dtor(void *null, void *ifp)
{
if_put(ifp);
}
void
if_map_dtor(void *null, void *m)
{
struct if_map *if_map = m;
struct srp *map = (struct srp *)(if_map + 1);
unsigned int i;
/*
* dont need to serialize the use of update_locked since this is
* the last reference to this map. there's nothing to race against.
*/
for (i = 0; i < if_map->limit; i++)
srp_update_locked(&if_ifp_gc, &map[i], NULL);
free(if_map, M_IFADDR, sizeof(*if_map) + if_map->limit * sizeof(*map));
}
/*
* Attach an interface to the
* list of "active" interfaces.
*/
void
if_attachsetup(struct ifnet *ifp)
{
unsigned long ifidx;
NET_ASSERT_LOCKED();
TAILQ_INIT(&ifp->if_groups);
if_addgroup(ifp, IFG_ALL);
if_attachdomain(ifp);
#if NPF > 0
pfi_attach_ifnet(ifp);
#endif
timeout_set(&ifp->if_slowtimo, if_slowtimo, ifp);
if_slowtimo(ifp);
if_idxmap_insert(ifp);
KASSERT(if_get(0) == NULL);
ifidx = ifp->if_index;
task_set(&ifp->if_watchdogtask, if_watchdog_task, (void *)ifidx);
task_set(&ifp->if_linkstatetask, if_linkstate_task, (void *)ifidx);
/* Announce the interface. */
rtm_ifannounce(ifp, IFAN_ARRIVAL);
}
/*
* Allocate the link level name for the specified interface. This
* is an attachment helper. It must be called after ifp->if_addrlen
* is initialized, which may not be the case when if_attach() is
* called.
*/
void
if_alloc_sadl(struct ifnet *ifp)
{
unsigned int socksize;
int namelen, masklen;
struct sockaddr_dl *sdl;
/*
* If the interface already has a link name, release it
* now. This is useful for interfaces that can change
* link types, and thus switch link names often.
*/
if_free_sadl(ifp);
namelen = strlen(ifp->if_xname);
masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
socksize = masklen + ifp->if_addrlen;
#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
if (socksize < sizeof(*sdl))
socksize = sizeof(*sdl);
socksize = ROUNDUP(socksize);
sdl = malloc(socksize, M_IFADDR, M_WAITOK|M_ZERO);
sdl->sdl_len = socksize;
sdl->sdl_family = AF_LINK;
bcopy(ifp->if_xname, sdl->sdl_data, namelen);
sdl->sdl_nlen = namelen;
sdl->sdl_alen = ifp->if_addrlen;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = ifp->if_type;
ifp->if_sadl = sdl;
}
/*
* Free the link level name for the specified interface. This is
* a detach helper. This is called from if_detach() or from
* link layer type specific detach functions.
*/
void
if_free_sadl(struct ifnet *ifp)
{
if (ifp->if_sadl == NULL)
return;
free(ifp->if_sadl, M_IFADDR, ifp->if_sadl->sdl_len);
ifp->if_sadl = NULL;
}
void
if_attachdomain(struct ifnet *ifp)
{
struct domain *dp;
int i, s;
s = splnet();
/* address family dependent data region */
bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_ifattach)
ifp->if_afdata[dp->dom_family] =
(*dp->dom_ifattach)(ifp);
}
splx(s);
}
void
if_attachhead(struct ifnet *ifp)
{
if_attach_common(ifp);
NET_LOCK();
TAILQ_INSERT_HEAD(&ifnet, ifp, if_list);
if_attachsetup(ifp);
NET_UNLOCK();
}
void
if_attach(struct ifnet *ifp)
{
if_attach_common(ifp);
NET_LOCK();
TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
if_attachsetup(ifp);
NET_UNLOCK();
}
void
if_attach_queues(struct ifnet *ifp, unsigned int nqs)
{
struct ifqueue **map;
struct ifqueue *ifq;
int i;
KASSERT(ifp->if_ifqs == ifp->if_snd.ifq_ifqs);
KASSERT(nqs != 0);
map = mallocarray(sizeof(*map), nqs, M_DEVBUF, M_WAITOK);
ifp->if_snd.ifq_softc = NULL;
map[0] = &ifp->if_snd;
for (i = 1; i < nqs; i++) {
ifq = malloc(sizeof(*ifq), M_DEVBUF, M_WAITOK|M_ZERO);
ifq_set_maxlen(ifq, ifp->if_snd.ifq_maxlen);
ifq_init(ifq, ifp, i);
map[i] = ifq;
}
ifp->if_ifqs = map;
ifp->if_nifqs = nqs;
}
void
if_attach_iqueues(struct ifnet *ifp, unsigned int niqs)
{
struct ifiqueue **map;
struct ifiqueue *ifiq;
unsigned int i;
KASSERT(niqs != 0);
map = mallocarray(niqs, sizeof(*map), M_DEVBUF, M_WAITOK);
ifp->if_rcv.ifiq_softc = NULL;
map[0] = &ifp->if_rcv;
for (i = 1; i < niqs; i++) {
ifiq = malloc(sizeof(*ifiq), M_DEVBUF, M_WAITOK|M_ZERO);
ifiq_init(ifiq, ifp, i);
map[i] = ifiq;
}
ifp->if_iqs = map;
ifp->if_niqs = niqs;
}
void
if_attach_common(struct ifnet *ifp)
{
KASSERT(ifp->if_ioctl != NULL);
TAILQ_INIT(&ifp->if_addrlist);
TAILQ_INIT(&ifp->if_maddrlist);
if (!ISSET(ifp->if_xflags, IFXF_MPSAFE)) {
KASSERTMSG(ifp->if_qstart == NULL,
"%s: if_qstart set without MPSAFE set", ifp->if_xname);
ifp->if_qstart = if_qstart_compat;
} else {
KASSERTMSG(ifp->if_start == NULL,
"%s: if_start set with MPSAFE set", ifp->if_xname);
KASSERTMSG(ifp->if_qstart != NULL,
"%s: if_qstart not set with MPSAFE set", ifp->if_xname);
}
ifq_init(&ifp->if_snd, ifp, 0);
ifp->if_snd.ifq_ifqs[0] = &ifp->if_snd;
ifp->if_ifqs = ifp->if_snd.ifq_ifqs;
ifp->if_nifqs = 1;
if (ifp->if_txmit == 0)
ifp->if_txmit = IF_TXMIT_DEFAULT;
ifiq_init(&ifp->if_rcv, ifp, 0);
ifp->if_rcv.ifiq_ifiqs[0] = &ifp->if_rcv;
ifp->if_iqs = ifp->if_rcv.ifiq_ifiqs;
ifp->if_niqs = 1;
TAILQ_INIT(&ifp->if_addrhooks);
TAILQ_INIT(&ifp->if_linkstatehooks);
TAILQ_INIT(&ifp->if_detachhooks);
if (ifp->if_rtrequest == NULL)
ifp->if_rtrequest = if_rtrequest_dummy;
if (ifp->if_enqueue == NULL)
ifp->if_enqueue = if_enqueue_ifq;
ifp->if_llprio = IFQ_DEFPRIO;
}
void
if_attach_ifq(struct ifnet *ifp, const struct ifq_ops *newops, void *args)
{
/*
* only switch the ifq_ops on the first ifq on an interface.
*
* the only ifq_ops we provide priq and hfsc, and hfsc only
* works on a single ifq. because the code uses the ifq_ops
* on the first ifq (if_snd) to select a queue for an mbuf,
* by switching only the first one we change both the algorithm
* and force the routing of all new packets to it.
*/
ifq_attach(&ifp->if_snd, newops, args);
}
void
if_start(struct ifnet *ifp)
{
KASSERT(ifp->if_qstart == if_qstart_compat);
if_qstart_compat(&ifp->if_snd);
}
void
if_qstart_compat(struct ifqueue *ifq)
{
struct ifnet *ifp = ifq->ifq_if;
int s;
/*
* the stack assumes that an interface can have multiple
* transmit rings, but a lot of drivers are still written
* so that interfaces and send rings have a 1:1 mapping.
* this provides compatability between the stack and the older
* drivers by translating from the only queue they have
* (ifp->if_snd) back to the interface and calling if_start.
*/
KERNEL_LOCK();
s = splnet();
(*ifp->if_start)(ifp);
splx(s);
KERNEL_UNLOCK();
}
int
if_enqueue(struct ifnet *ifp, struct mbuf *m)
{
#if NPF > 0
if (m->m_pkthdr.pf.delay > 0)
return (pf_delay_pkt(m, ifp->if_index));
#endif
#if NBRIDGE > 0
if (ifp->if_bridgeidx && (m->m_flags & M_PROTO1) == 0) {
int error;
error = bridge_enqueue(ifp, m);
return (error);
}
#endif
#if NPF > 0
pf_pkt_addr_changed(m);
#endif /* NPF > 0 */
return ((*ifp->if_enqueue)(ifp, m));
}
int
if_enqueue_ifq(struct ifnet *ifp, struct mbuf *m)
{
struct ifqueue *ifq = &ifp->if_snd;
int error;
if (ifp->if_nifqs > 1) {
unsigned int idx;
/*
* use the operations on the first ifq to pick which of
* the array gets this mbuf.
*/
idx = ifq_idx(&ifp->if_snd, ifp->if_nifqs, m);
ifq = ifp->if_ifqs[idx];
}
error = ifq_enqueue(ifq, m);
if (error)
return (error);
ifq_start(ifq);
return (0);
}
void
if_input(struct ifnet *ifp, struct mbuf_list *ml)
{
ifiq_input(&ifp->if_rcv, ml);
}
int
if_input_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af)
{
#if NBPFILTER > 0
/*
* Only send packets to bpf if they are destinated to local
* addresses.
*
* if_input_local() is also called for SIMPLEX interfaces to
* duplicate packets for local use. But don't dup them to bpf.
*/
if (ifp->if_flags & IFF_LOOPBACK) {
caddr_t if_bpf = ifp->if_bpf;
if (if_bpf)
bpf_mtap_af(if_bpf, af, m, BPF_DIRECTION_OUT);
}
#endif
m_resethdr(m);
m->m_flags |= M_LOOP;
m->m_pkthdr.ph_ifidx = ifp->if_index;
m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
ifp->if_opackets++;
ifp->if_obytes += m->m_pkthdr.len;
ifp->if_ipackets++;
ifp->if_ibytes += m->m_pkthdr.len;
switch (af) {
case AF_INET:
ipv4_input(ifp, m);
break;
#ifdef INET6
case AF_INET6:
ipv6_input(ifp, m);
break;
#endif /* INET6 */
#ifdef MPLS
case AF_MPLS:
mpls_input(ifp, m);
break;
#endif /* MPLS */
default:
printf("%s: can't handle af%d\n", ifp->if_xname, af);
m_freem(m);
return (EAFNOSUPPORT);
}
return (0);
}
int
if_output_local(struct ifnet *ifp, struct mbuf *m, sa_family_t af)
{
struct ifiqueue *ifiq;
unsigned int flow = 0;
m->m_pkthdr.ph_family = af;
m->m_pkthdr.ph_ifidx = ifp->if_index;
m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
if (ISSET(m->m_pkthdr.csum_flags, M_FLOWID))
flow = m->m_pkthdr.ph_flowid;
ifiq = ifp->if_iqs[flow % ifp->if_niqs];
return (ifiq_enqueue(ifiq, m) == 0 ? 0 : ENOBUFS);
}
void
if_input_process(struct ifnet *ifp, struct mbuf_list *ml)
{
struct mbuf *m;
if (ml_empty(ml))
return;
if (!ISSET(ifp->if_xflags, IFXF_CLONED))
enqueue_randomness(ml_len(ml) ^ (uintptr_t)MBUF_LIST_FIRST(ml));
/*
* We grab the NET_LOCK() before processing any packet to
* ensure there's no contention on the routing table lock.
*
* Without it we could race with a userland thread to insert
* a L2 entry in ip{6,}_output(). Such race would result in
* one of the threads sleeping *inside* the IP output path.
*
* Since we have a NET_LOCK() we also use it to serialize access
* to PF globals, pipex globals, unicast and multicast addresses
* lists and the socket layer.
*/
NET_LOCK();
while ((m = ml_dequeue(ml)) != NULL)
(*ifp->if_input)(ifp, m);
NET_UNLOCK();
}
void
if_vinput(struct ifnet *ifp, struct mbuf *m)
{
#if NBPFILTER > 0
caddr_t if_bpf;
#endif
m->m_pkthdr.ph_ifidx = ifp->if_index;
m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
counters_pkt(ifp->if_counters,
ifc_ipackets, ifc_ibytes, m->m_pkthdr.len);
#if NBPFILTER > 0
if_bpf = ifp->if_bpf;
if (if_bpf) {
if (bpf_mtap_ether(if_bpf, m, BPF_DIRECTION_IN)) {
m_freem(m);
return;
}
}
#endif
(*ifp->if_input)(ifp, m);
}
void
if_netisr(void *unused)
{
int n, t = 0;
NET_LOCK();
while ((n = netisr) != 0) {
/* Like sched_pause() but with a rwlock dance. */
if (curcpu()->ci_schedstate.spc_schedflags & SPCF_SHOULDYIELD) {
NET_UNLOCK();
yield();
NET_LOCK();
}
atomic_clearbits_int(&netisr, n);
#if NETHER > 0
if (n & (1 << NETISR_ARP)) {
KERNEL_LOCK();
arpintr();
KERNEL_UNLOCK();
}
#endif
#if NPPP > 0
if (n & (1 << NETISR_PPP)) {
KERNEL_LOCK();
pppintr();
KERNEL_UNLOCK();
}
#endif
#if NBRIDGE > 0
if (n & (1 << NETISR_BRIDGE))
bridgeintr();
#endif
#if NSWITCH > 0
if (n & (1 << NETISR_SWITCH)) {
KERNEL_LOCK();
switchintr();
KERNEL_UNLOCK();
}
#endif
#if NPPPOE > 0
if (n & (1 << NETISR_PPPOE)) {
KERNEL_LOCK();
pppoeintr();
KERNEL_UNLOCK();
}
#endif
t |= n;
}
#if NPFSYNC > 0
if (t & (1 << NETISR_PFSYNC)) {
KERNEL_LOCK();
pfsyncintr();
KERNEL_UNLOCK();
}
#endif
NET_UNLOCK();
}
void
if_hooks_run(struct task_list *hooks)
{
struct task *t, *nt;
struct task cursor = { .t_func = NULL };
void (*func)(void *);
void *arg;
mtx_enter(&if_hooks_mtx);
for (t = TAILQ_FIRST(hooks); t != NULL; t = nt) {
if (t->t_func == NULL) { /* skip cursors */
nt = TAILQ_NEXT(t, t_entry);
continue;
}
func = t->t_func;
arg = t->t_arg;
TAILQ_INSERT_AFTER(hooks, t, &cursor, t_entry);
mtx_leave(&if_hooks_mtx);
(*func)(arg);
mtx_enter(&if_hooks_mtx);
nt = TAILQ_NEXT(&cursor, t_entry); /* avoid _Q_INVALIDATE */
TAILQ_REMOVE(hooks, &cursor, t_entry);
}
mtx_leave(&if_hooks_mtx);
}
void
if_deactivate(struct ifnet *ifp)
{
/*
* Call detach hooks from head to tail. To make sure detach
* hooks are executed in the reverse order they were added, all
* the hooks have to be added to the head!
*/
NET_LOCK();
if_hooks_run(&ifp->if_detachhooks);
NET_UNLOCK();
}
void
if_detachhook_add(struct ifnet *ifp, struct task *t)
{
mtx_enter(&if_hooks_mtx);
TAILQ_INSERT_HEAD(&ifp->if_detachhooks, t, t_entry);
mtx_leave(&if_hooks_mtx);
}
void
if_detachhook_del(struct ifnet *ifp, struct task *t)
{
mtx_enter(&if_hooks_mtx);
TAILQ_REMOVE(&ifp->if_detachhooks, t, t_entry);
mtx_leave(&if_hooks_mtx);
}
/*
* Detach an interface from everything in the kernel. Also deallocate
* private resources.
*/
void
if_detach(struct ifnet *ifp)
{
struct ifaddr *ifa;
struct ifg_list *ifg;
struct domain *dp;
int i, s;
/* Undo pseudo-driver changes. */
if_deactivate(ifp);
ifq_clr_oactive(&ifp->if_snd);
/* Other CPUs must not have a reference before we start destroying. */