/
if.c
3591 lines (3036 loc) · 75.3 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.718 2024/02/06 00:18:53 bluhm 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 "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 <sys/rwlock.h>
#include <sys/smr.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/netisr.h>
#include "vlan.h"
#if NVLAN > 0
#include <net/if_vlan_var.h>
#endif
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/igmp.h>
#ifdef MROUTING
#include <netinet/ip_mroute.h>
#endif
#include <netinet/tcp.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#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_attach_common(struct ifnet *);
void if_remove(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.
*/
struct ifnet *if_ref(struct ifnet *);
/*
* struct if_idxmap
*
* infrastructure to manage updates and accesses to the current if_map.
*
* interface index 0 is special and represents "no interface", so we
* use the 0th slot in map to store the length of the array.
*/
struct if_idxmap {
unsigned int serial;
unsigned int count;
struct ifnet **map; /* SMR protected */
struct rwlock lock;
unsigned char *usedidx; /* bitmap of indices in use */
};
struct if_idxmap_dtor {
struct smr_entry smr;
struct ifnet **map;
};
void if_idxmap_init(unsigned int);
void if_idxmap_free(void *);
void if_idxmap_alloc(struct ifnet *);
void if_idxmap_insert(struct ifnet *);
void if_idxmap_remove(struct ifnet *);
TAILQ_HEAD(, ifg_group) ifg_head =
TAILQ_HEAD_INITIALIZER(ifg_head); /* [N] list of interface groups */
LIST_HEAD(, if_clone) if_cloners =
LIST_HEAD_INITIALIZER(if_cloners); /* [I] list of clonable interfaces */
int if_cloners_count; /* [I] number of clonable interfaces */
struct rwlock if_cloners_lock = RWLOCK_INITIALIZER("clonelk");
/* 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;
struct softnet {
char sn_name[16];
struct taskq *sn_taskq;
};
#define NET_TASKQ 4
struct softnet softnets[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 accommodate this
*/
if_idxmap_init(8); /* 8 is a nice power of 2 for malloc */
for (i = 0; i < NET_TASKQ; i++) {
struct softnet *sn = &softnets[i];
snprintf(sn->sn_name, sizeof(sn->sn_name), "softnet%u", i);
sn->sn_taskq = taskq_create(sn->sn_name, 1, IPL_NET,
TASKQ_MPSAFE);
if (sn->sn_taskq == NULL)
panic("unable to create network taskq %d", i);
}
}
static struct if_idxmap if_idxmap;
/*
* XXXSMP: For `ifnetlist' modification both kernel and net locks
* should be taken. For read-only access only one lock of them required.
*/
struct ifnet_head ifnetlist = TAILQ_HEAD_INITIALIZER(ifnetlist);
static inline unsigned int
if_idxmap_limit(struct ifnet **if_map)
{
return ((uintptr_t)if_map[0]);
}
static inline size_t
if_idxmap_usedidx_size(unsigned int limit)
{
return (max(howmany(limit, NBBY), sizeof(struct if_idxmap_dtor)));
}
void
if_idxmap_init(unsigned int limit)
{
struct ifnet **if_map;
rw_init(&if_idxmap.lock, "idxmaplk");
if_idxmap.serial = 1; /* skip ifidx 0 */
if_map = mallocarray(limit, sizeof(*if_map), M_IFADDR,
M_WAITOK | M_ZERO);
if_map[0] = (struct ifnet *)(uintptr_t)limit;
if_idxmap.usedidx = malloc(if_idxmap_usedidx_size(limit),
M_IFADDR, M_WAITOK | M_ZERO);
setbit(if_idxmap.usedidx, 0); /* blacklist ifidx 0 */
/* this is called early so there's nothing to race with */
SMR_PTR_SET_LOCKED(&if_idxmap.map, if_map);
}
void
if_idxmap_alloc(struct ifnet *ifp)
{
struct ifnet **if_map;
unsigned int limit;
unsigned int index, i;
refcnt_init(&ifp->if_refcnt);
rw_enter_write(&if_idxmap.lock);
if (++if_idxmap.count >= USHRT_MAX)
panic("too many interfaces");
if_map = SMR_PTR_GET_LOCKED(&if_idxmap.map);
limit = if_idxmap_limit(if_map);
index = if_idxmap.serial++ & USHRT_MAX;
if (index >= limit) {
struct if_idxmap_dtor *dtor;
struct ifnet **oif_map;
unsigned int olimit;
unsigned char *nusedidx;
oif_map = if_map;
olimit = limit;
limit = olimit * 2;
if_map = mallocarray(limit, sizeof(*if_map), M_IFADDR,
M_WAITOK | M_ZERO);
if_map[0] = (struct ifnet *)(uintptr_t)limit;
for (i = 1; i < olimit; i++) {
struct ifnet *oifp = SMR_PTR_GET_LOCKED(&oif_map[i]);
if (oifp == NULL)
continue;
/*
* nif_map isn't visible yet, so don't need
* SMR_PTR_SET_LOCKED and its membar.
*/
if_map[i] = if_ref(oifp);
}
nusedidx = malloc(if_idxmap_usedidx_size(limit),
M_IFADDR, M_WAITOK | M_ZERO);
memcpy(nusedidx, if_idxmap.usedidx, howmany(olimit, NBBY));
/* use the old usedidx bitmap as an smr_entry for the if_map */
dtor = (struct if_idxmap_dtor *)if_idxmap.usedidx;
if_idxmap.usedidx = nusedidx;
SMR_PTR_SET_LOCKED(&if_idxmap.map, if_map);
dtor->map = oif_map;
smr_init(&dtor->smr);
smr_call(&dtor->smr, if_idxmap_free, dtor);
}
/* pick the next free index */
for (i = 0; i < USHRT_MAX; i++) {
if (index != 0 && isclr(if_idxmap.usedidx, index))
break;
index = if_idxmap.serial++ & USHRT_MAX;
}
KASSERT(index != 0 && index < limit);
KASSERT(isclr(if_idxmap.usedidx, index));
setbit(if_idxmap.usedidx, index);
ifp->if_index = index;
rw_exit_write(&if_idxmap.lock);
}
void
if_idxmap_free(void *arg)
{
struct if_idxmap_dtor *dtor = arg;
struct ifnet **oif_map = dtor->map;
unsigned int olimit = if_idxmap_limit(oif_map);
unsigned int i;
for (i = 1; i < olimit; i++)
if_put(oif_map[i]);
free(oif_map, M_IFADDR, olimit * sizeof(*oif_map));
free(dtor, M_IFADDR, if_idxmap_usedidx_size(olimit));
}
void
if_idxmap_insert(struct ifnet *ifp)
{
struct ifnet **if_map;
unsigned int index = ifp->if_index;
rw_enter_write(&if_idxmap.lock);
if_map = SMR_PTR_GET_LOCKED(&if_idxmap.map);
KASSERTMSG(index != 0 && index < if_idxmap_limit(if_map),
"%s(%p) index %u vs limit %u", ifp->if_xname, ifp, index,
if_idxmap_limit(if_map));
KASSERT(SMR_PTR_GET_LOCKED(&if_map[index]) == NULL);
KASSERT(isset(if_idxmap.usedidx, index));
/* commit */
SMR_PTR_SET_LOCKED(&if_map[index], if_ref(ifp));
rw_exit_write(&if_idxmap.lock);
}
void
if_idxmap_remove(struct ifnet *ifp)
{
struct ifnet **if_map;
unsigned int index = ifp->if_index;
rw_enter_write(&if_idxmap.lock);
if_map = SMR_PTR_GET_LOCKED(&if_idxmap.map);
KASSERT(index != 0 && index < if_idxmap_limit(if_map));
KASSERT(SMR_PTR_GET_LOCKED(&if_map[index]) == ifp);
KASSERT(isset(if_idxmap.usedidx, index));
SMR_PTR_SET_LOCKED(&if_map[index], NULL);
if_idxmap.count--;
clrbit(if_idxmap.usedidx, index);
/* end of if_idxmap modifications */
rw_exit_write(&if_idxmap.lock);
smr_barrier();
if_put(ifp);
}
/*
* Attach an interface to the
* list of "active" interfaces.
*/
void
if_attachsetup(struct ifnet *ifp)
{
unsigned long ifidx;
NET_ASSERT_LOCKED();
if_addgroup(ifp, IFG_ALL);
#ifdef INET6
nd6_ifattach(ifp);
#endif
#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_attachhead(struct ifnet *ifp)
{
if_attach_common(ifp);
NET_LOCK();
TAILQ_INSERT_HEAD(&ifnetlist, ifp, if_list);
if_attachsetup(ifp);
NET_UNLOCK();
}
void
if_attach(struct ifnet *ifp)
{
if_attach_common(ifp);
NET_LOCK();
TAILQ_INSERT_TAIL(&ifnetlist, 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_init_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);
TAILQ_INIT(&ifp->if_groups);
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);
}
if_idxmap_alloc(ifp);
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;
#if NBPFILTER > 0
if (ifp->if_bpf_mtap == NULL)
ifp->if_bpf_mtap = bpf_mtap_ether;
#endif
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 compatibility 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)
{
CLR(m->m_pkthdr.csum_flags, M_TIMESTAMP);
#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)
{
int keepflags, keepcksum;
uint16_t keepmss;
#if NBPFILTER > 0
/*
* Only send packets to bpf if they are destined 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
keepflags = m->m_flags & (M_BCAST|M_MCAST);
/*
* Preserve outgoing checksum flags, in case the packet is
* forwarded to another interface. Then the checksum, which
* is now incorrect, will be calculated before sending.
*/
keepcksum = m->m_pkthdr.csum_flags & (M_IPV4_CSUM_OUT |
M_TCP_CSUM_OUT | M_UDP_CSUM_OUT | M_ICMP_CSUM_OUT |
M_TCP_TSO);
keepmss = m->m_pkthdr.ph_mss;
m_resethdr(m);
m->m_flags |= M_LOOP | keepflags;
m->m_pkthdr.csum_flags = keepcksum;
m->m_pkthdr.ph_mss = keepmss;
m->m_pkthdr.ph_ifidx = ifp->if_index;
m->m_pkthdr.ph_rtableid = ifp->if_rdomain;
if (ISSET(keepcksum, M_TCP_TSO) && m->m_pkthdr.len > ifp->if_mtu) {
if (ifp->if_mtu > 0 &&
((af == AF_INET &&
ISSET(ifp->if_capabilities, IFCAP_TSOv4)) ||
(af == AF_INET6 &&
ISSET(ifp->if_capabilities, IFCAP_TSOv6)))) {
tcpstat_inc(tcps_inswlro);
tcpstat_add(tcps_inpktlro,
(m->m_pkthdr.len + ifp->if_mtu - 1) / ifp->if_mtu);
} else {
tcpstat_inc(tcps_inbadlro);
m_freem(m);
return (EPROTONOSUPPORT);
}
}
if (ISSET(keepcksum, M_TCP_CSUM_OUT))
m->m_pkthdr.csum_flags |= M_TCP_CSUM_IN_OK;
if (ISSET(keepcksum, M_UDP_CSUM_OUT))
m->m_pkthdr.csum_flags |= M_UDP_CSUM_IN_OK;
if (ISSET(keepcksum, M_ICMP_CSUM_OUT))
m->m_pkthdr.csum_flags |= M_ICMP_CSUM_IN_OK;
/* do not count multicast loopback and simplex interfaces */
if (ISSET(ifp->if_flags, IFF_LOOPBACK)) {
counters_pkt(ifp->if_counters, ifc_opackets, ifc_obytes,
m->m_pkthdr.len);
}
switch (af) {
case AF_INET:
if (ISSET(keepcksum, M_IPV4_CSUM_OUT))
m->m_pkthdr.csum_flags |= M_IPV4_CSUM_IN_OK;
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_ml(struct ifnet *ifp, struct mbuf_list *ml,
struct sockaddr *dst, struct rtentry *rt)
{
struct mbuf *m;
int error = 0;
while ((m = ml_dequeue(ml)) != NULL) {
error = ifp->if_output(ifp, m, dst, rt);
if (error)
break;
}
if (error)
ml_purge(ml);
return error;
}
int
if_output_tso(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
struct rtentry *rt, u_int mtu)
{
uint32_t ifcap;
int error;
switch (dst->sa_family) {
case AF_INET:
ifcap = IFCAP_TSOv4;
break;
#ifdef INET6
case AF_INET6:
ifcap = IFCAP_TSOv6;
break;
#endif
default:
unhandled_af(dst->sa_family);
}
/*
* Try to send with TSO first. When forwarding LRO may set
* maximum segment size in mbuf header. Chop TCP segment
* even if it would fit interface MTU to preserve maximum
* path MTU.
*/
error = tcp_if_output_tso(ifp, mp, dst, rt, ifcap, mtu);
if (error || *mp == NULL)
return error;
if ((*mp)->m_pkthdr.len <= mtu) {
switch (dst->sa_family) {
case AF_INET:
in_hdr_cksum_out(*mp, ifp);
in_proto_cksum_out(*mp, ifp);
break;
#ifdef INET6
case AF_INET6:
in6_proto_cksum_out(*mp, ifp);
break;
#endif
}
error = ifp->if_output(ifp, *mp, dst, rt);
*mp = NULL;
return error;
}
/* mp still contains mbuf that has to be fragmented or dropped. */
return 0;
}
int
if_output_mq(struct ifnet *ifp, struct mbuf_queue *mq, unsigned int *total,
struct sockaddr *dst, struct rtentry *rt)
{
struct mbuf_list ml;
unsigned int len;
int error;
mq_delist(mq, &ml);
len = ml_len(&ml);
error = if_output_ml(ifp, &ml, dst, rt);
/* XXXSMP we also discard if other CPU enqueues */
if (mq_len(mq) > 0) {
/* mbuf is back in queue. Discard. */
atomic_sub_int(total, len + mq_purge(mq));
} else
atomic_sub_int(total, len);
return error;
}
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 shared netlock for packet processing in the softnet
* threads. Packets can regrab the exclusive lock via queues.
* ioctl, sysctl, and socket syscall may use shared lock if access is
* read only or MP safe. Usually they hold the exclusive net lock.
*/
NET_LOCK_SHARED();