/
if_ethersubr.c
1720 lines (1508 loc) · 41.1 KB
/
if_ethersubr.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
/*
* Copyright (c) 1982, 1989, 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_ethersubr.c 8.1 (Berkeley) 6/10/93
* $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
*/
#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_mpls.h"
#include "opt_netgraph.h"
#include "opt_carp.h"
#include "opt_rss.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/globaldata.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/msgport.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/thread.h>
#include <sys/thread2.h>
#include <sys/mplock2.h>
#include <net/if.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/if_llc.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/ifq_var.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/vlan/if_vlan_ether.h>
#include <net/vlan/if_vlan_var.h>
#include <net/netmsg2.h>
#include <net/netisr2.h>
#if defined(INET) || defined(INET6)
#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/tcp_var.h>
#include <netinet/if_ether.h>
#include <netinet/ip_flow.h>
#include <net/ipfw/ip_fw.h>
#include <net/ipfw3/ip_fw.h>
#include <net/dummynet/ip_dummynet.h>
#endif
#ifdef INET6
#include <netinet6/nd6.h>
#endif
#ifdef CARP
#include <netinet/ip_carp.h>
#endif
#ifdef MPLS
#include <netproto/mpls/mpls.h>
#endif
/* netgraph node hooks for ng_ether(4) */
void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
void (*ng_ether_attach_p)(struct ifnet *ifp);
void (*ng_ether_detach_p)(struct ifnet *ifp);
void (*vlan_input_p)(struct mbuf *);
static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
struct rtentry *);
static void ether_restore_header(struct mbuf **, const struct ether_header *,
const struct ether_header *);
static int ether_characterize(struct mbuf **);
static void ether_dispatch(struct ifnet *, int, struct mbuf *, int);
/*
* if_bridge support
*/
struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
int (*bridge_output_p)(struct ifnet *, struct mbuf *);
void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
struct ifnet *(*bridge_interface_p)(void *if_bridge);
static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
struct sockaddr *);
/*
* if_lagg(4) support
*/
void (*lagg_input_p)(struct ifnet *, struct mbuf *);
int (*lagg_output_p)(struct ifnet *, struct mbuf *);
const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
#define gotoerr(e) do { error = (e); goto bad; } while (0)
#define IFP2AC(ifp) ((struct arpcom *)(ifp))
static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
struct ip_fw **rule,
const struct ether_header *eh);
static int ether_ipfw;
static u_long ether_restore_hdr;
static u_long ether_prepend_hdr;
static u_long ether_input_wronghash;
static int ether_debug;
#ifdef RSS_DEBUG
static u_long ether_pktinfo_try;
static u_long ether_pktinfo_hit;
static u_long ether_rss_nopi;
static u_long ether_rss_nohash;
static u_long ether_input_requeue;
#endif
static u_long ether_input_wronghwhash;
static int ether_input_ckhash;
#define ETHER_TSOLEN_DEFAULT (4 * ETHERMTU)
#define ETHER_NMBCLUSTERS_DEFMIN 32
#define ETHER_NMBCLUSTERS_DEFAULT 256
static int ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
TUNABLE_INT("net.link.ether.tsolen", ðer_tsolen_default);
static int ether_nmbclusters_default = ETHER_NMBCLUSTERS_DEFAULT;
TUNABLE_INT("net.link.ether.nmbclusters", ðer_nmbclusters_default);
SYSCTL_DECL(_net_link);
SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
ðer_debug, 0, "Ether debug");
SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
ðer_ipfw, 0, "Pass ether pkts through firewall");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
ðer_restore_hdr, 0, "# of ether header restoration");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
ðer_prepend_hdr, 0,
"# of ether header restoration which prepends mbuf");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
ðer_input_wronghash, 0, "# of input packets with wrong hash");
SYSCTL_INT(_net_link_ether, OID_AUTO, tsolen, CTLFLAG_RW,
ðer_tsolen_default, 0, "Default max TSO length");
#ifdef RSS_DEBUG
SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
ðer_rss_nopi, 0, "# of packets do not have pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
ðer_rss_nohash, 0, "# of packets do not have hash");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
ðer_pktinfo_try, 0,
"# of tries to find packets' msgport using pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
ðer_pktinfo_hit, 0,
"# of packets whose msgport are found using pktinfo");
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
ðer_input_requeue, 0, "# of input packets gets requeued");
#endif
SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
ðer_input_wronghwhash, 0, "# of input packets with wrong hw hash");
SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
ðer_input_ckhash, 0, "always check hash");
#define ETHER_KTR_STR "ifp=%p"
#define ETHER_KTR_ARGS struct ifnet *ifp
#ifndef KTR_ETHERNET
#define KTR_ETHERNET KTR_ALL
#endif
KTR_INFO_MASTER(ether);
KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
#define logether(name, arg) KTR_LOG(ether_ ## name, arg)
/*
* Ethernet output routine.
* Encapsulate a packet of type family for the local net.
* Use trailer local net encapsulation if enough data in first
* packet leaves a multiple of 512 bytes of data in remainder.
* Assumes that ifp is actually pointer to arpcom structure.
*/
static int
ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
struct rtentry *rt)
{
struct ether_header *eh, *deh;
u_char *edst;
int loop_copy = 0;
int hlen = ETHER_HDR_LEN; /* link layer header length */
struct arpcom *ac = IFP2AC(ifp);
int error;
ASSERT_NETISR_NCPUS(mycpuid);
ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
if (ifp->if_flags & IFF_MONITOR)
gotoerr(ENETDOWN);
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
gotoerr(ENETDOWN);
M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
if (m == NULL)
return (ENOBUFS);
m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
eh = mtod(m, struct ether_header *);
edst = eh->ether_dhost;
/*
* Fill in the destination ethernet address and frame type.
*/
switch (dst->sa_family) {
#ifdef INET
case AF_INET:
error = arpresolve(ifp, rt, m, dst, edst);
if (error != 0)
return error == EWOULDBLOCK ? 0 : error;
#ifdef MPLS
if (m->m_flags & M_MPLSLABELED)
eh->ether_type = htons(ETHERTYPE_MPLS);
else
#endif
eh->ether_type = htons(ETHERTYPE_IP);
break;
case AF_ARP:
{
struct arphdr *ah;
ah = mtod(m, struct arphdr *);
ah->ar_hrd = htons(ARPHRD_ETHER);
loop_copy = -1; /* if this is for us, don't do it */
switch(ntohs(ah->ar_op)) {
case ARPOP_REVREQUEST:
case ARPOP_REVREPLY:
eh->ether_type = htons(ETHERTYPE_REVARP);
break;
case ARPOP_REQUEST:
case ARPOP_REPLY:
default:
eh->ether_type = htons(ETHERTYPE_ARP);
break;
}
if (m->m_flags & M_BCAST)
bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
else
bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
}
#endif
#ifdef INET6
case AF_INET6:
error = nd6_resolve(&ac->ac_if, rt, m, dst, edst);
if (error != 0)
return error == EWOULDBLOCK ? 0 : error;
eh->ether_type = htons(ETHERTYPE_IPV6);
break;
#endif
case pseudo_AF_HDRCMPLT:
case AF_UNSPEC:
loop_copy = -1; /* if this is for us, don't do it */
deh = (struct ether_header *)dst->sa_data;
memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
eh->ether_type = deh->ether_type;
break;
default:
if_printf(ifp, "can't handle af%d\n", dst->sa_family);
gotoerr(EAFNOSUPPORT);
}
if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
memcpy(eh->ether_shost,
((struct ether_header *)dst->sa_data)->ether_shost,
ETHER_ADDR_LEN);
else
memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
/*
* Bridges require special output handling.
*/
if (ifp->if_bridge) {
KASSERT(bridge_output_p != NULL,
("%s: if_bridge not loaded!", __func__));
return bridge_output_p(ifp, m);
}
#if 0 /* XXX */
if (ifp->if_lagg) {
KASSERT(lagg_output_p != NULL,
("%s: if_lagg not loaded!", __func__));
return lagg_output_p(ifp, m);
}
#endif
/*
* If a simplex interface, and the packet is being sent to our
* Ethernet address or a broadcast address, loopback a copy.
* XXX To make a simplex device behave exactly like a duplex
* device, we should copy in the case of sending to our own
* ethernet address (thus letting the original actually appear
* on the wire). However, we don't do that here for security
* reasons and compatibility with the original behavior.
*/
if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
int csum_flags = 0;
if (m->m_pkthdr.csum_flags & CSUM_IP)
csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
struct mbuf *n;
if ((n = m_copypacket(m, M_NOWAIT)) != NULL) {
n->m_pkthdr.csum_flags |= csum_flags;
if (csum_flags & CSUM_DATA_VALID)
n->m_pkthdr.csum_data = 0xffff;
if_simloop(ifp, n, dst->sa_family, hlen);
} else
IFNET_STAT_INC(ifp, iqdrops, 1);
} else if (bcmp(eh->ether_dhost, eh->ether_shost,
ETHER_ADDR_LEN) == 0) {
m->m_pkthdr.csum_flags |= csum_flags;
if (csum_flags & CSUM_DATA_VALID)
m->m_pkthdr.csum_data = 0xffff;
if_simloop(ifp, m, dst->sa_family, hlen);
return (0); /* XXX */
}
}
#ifdef CARP
if (ifp->if_type == IFT_CARP) {
ifp = carp_parent(ifp);
if (ifp == NULL)
gotoerr(ENETUNREACH);
ac = IFP2AC(ifp);
/*
* Check precondition again
*/
ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
if (ifp->if_flags & IFF_MONITOR)
gotoerr(ENETDOWN);
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
(IFF_UP | IFF_RUNNING))
gotoerr(ENETDOWN);
}
#endif
/* Handle ng_ether(4) processing, if any */
if (ng_ether_output_p != NULL) {
/*
* Hold BGL and recheck ng_ether_output_p
*/
get_mplock();
if (ng_ether_output_p != NULL) {
if ((error = ng_ether_output_p(ifp, &m)) != 0) {
rel_mplock();
goto bad;
}
if (m == NULL) {
rel_mplock();
return (0);
}
}
rel_mplock();
}
/* Continue with link-layer output */
return ether_output_frame(ifp, m);
bad:
m_freem(m);
return (error);
}
/*
* Returns the bridge interface an ifp is associated
* with.
*
* Only call if ifp->if_bridge != NULL.
*/
struct ifnet *
ether_bridge_interface(struct ifnet *ifp)
{
if (bridge_interface_p)
return(bridge_interface_p(ifp->if_bridge));
return (ifp);
}
/*
* Ethernet link layer output routine to send a raw frame to the device.
*
* This assumes that the 14 byte Ethernet header is present and contiguous
* in the first mbuf.
*/
int
ether_output_frame(struct ifnet *ifp, struct mbuf *m)
{
struct ip_fw *rule = NULL;
int error = 0;
struct altq_pktattr pktattr;
ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
struct m_tag *mtag;
/* Extract info from dummynet tag */
mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
KKASSERT(mtag != NULL);
rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
KKASSERT(rule != NULL);
m_tag_delete(m, mtag);
m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
}
if (ifq_is_enabled(&ifp->if_snd))
altq_etherclassify(&ifp->if_snd, m, &pktattr);
crit_enter();
if ((IPFW_LOADED || IPFW3_LOADED) && ether_ipfw != 0) {
struct ether_header save_eh, *eh;
eh = mtod(m, struct ether_header *);
save_eh = *eh;
m_adj(m, ETHER_HDR_LEN);
if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
crit_exit();
if (m != NULL) {
m_freem(m);
return ENOBUFS; /* pkt dropped */
} else
return 0; /* consumed e.g. in a pipe */
}
/* packet was ok, restore the ethernet header */
ether_restore_header(&m, eh, &save_eh);
if (m == NULL) {
crit_exit();
return ENOBUFS;
}
}
crit_exit();
/*
* Queue message on interface, update output statistics if
* successful, and start output if interface not yet active.
*/
error = ifq_dispatch(ifp, m, &pktattr);
return (error);
}
/*
* ipfw processing for ethernet packets (in and out).
* The second parameter is NULL from ether_demux(), and ifp from
* ether_output_frame().
*/
static boolean_t
ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
const struct ether_header *eh)
{
struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
struct ip_fw_args args;
struct m_tag *mtag;
struct mbuf *m;
int i;
if (*rule != NULL && fw_one_pass)
return TRUE; /* dummynet packet, already partially processed */
/*
* I need some amount of data to be contiguous.
*/
i = min((*m0)->m_pkthdr.len, max_protohdr);
if ((*m0)->m_len < i) {
*m0 = m_pullup(*m0, i);
if (*m0 == NULL)
return FALSE;
}
/*
* Clean up tags
*/
if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
m_tag_delete(*m0, mtag);
if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
KKASSERT(mtag != NULL);
m_tag_delete(*m0, mtag);
(*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
}
args.flags = 0;
args.xlat = NULL;
args.m = *m0; /* the packet we are looking at */
args.oif = dst; /* destination, if any */
args.rule = *rule; /* matching rule to restart */
args.eh = &save_eh; /* MAC header for bridged/MAC packets */
i = ip_fw_chk_ptr(&args);
*m0 = args.m;
*rule = args.rule;
if (*m0 == NULL)
return FALSE;
switch (i) {
case IP_FW_PASS:
return TRUE;
case IP_FW_DIVERT:
case IP_FW_TEE:
case IP_FW_DENY:
/*
* XXX at some point add support for divert/forward actions.
* If none of the above matches, we have to drop the pkt.
*/
return FALSE;
case IP_FW_DUMMYNET:
/*
* Pass the pkt to dummynet, which consumes it.
*/
m = *m0; /* pass the original to dummynet */
*m0 = NULL; /* and nothing back to the caller */
ether_restore_header(&m, eh, &save_eh);
if (m == NULL)
return FALSE;
m = ip_fw_dn_io_ptr(m, args.cookie,
dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
if (m != NULL)
ip_dn_queue(m);
return FALSE;
default:
panic("unknown ipfw return value: %d", i);
}
}
/*
* Perform common duties while attaching to interface list
*/
void
ether_ifattach(struct ifnet *ifp, const uint8_t *lla,
lwkt_serialize_t serializer)
{
ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
serializer);
}
void
ether_ifattach_bpf(struct ifnet *ifp, const uint8_t *lla,
u_int dlt, u_int hdrlen, lwkt_serialize_t serializer)
{
struct sockaddr_dl *sdl;
char ethstr[ETHER_ADDRSTRLEN + 1];
struct ifaltq *ifq;
int i;
/*
* If driver does not configure # of mbuf clusters/jclusters
* that could sit on the device queues for quite some time,
* we then assume:
* - The device queues only consume mbuf clusters.
* - No more than ether_nmbclusters_default (by default 256)
* mbuf clusters will sit on the device queues for quite
* some time.
*/
if (ifp->if_nmbclusters <= 0 && ifp->if_nmbjclusters <= 0) {
if (ether_nmbclusters_default < ETHER_NMBCLUSTERS_DEFMIN) {
kprintf("ether nmbclusters %d -> %d\n",
ether_nmbclusters_default,
ETHER_NMBCLUSTERS_DEFAULT);
ether_nmbclusters_default = ETHER_NMBCLUSTERS_DEFAULT;
}
ifp->if_nmbclusters = ether_nmbclusters_default;
}
ifp->if_type = IFT_ETHER;
ifp->if_addrlen = ETHER_ADDR_LEN;
ifp->if_hdrlen = ETHER_HDR_LEN;
if_attach(ifp, serializer);
ifq = &ifp->if_snd;
for (i = 0; i < ifq->altq_subq_cnt; ++i) {
struct ifaltq_subque *ifsq = ifq_get_subq(ifq, i);
ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen *
(ETHER_MAX_LEN - ETHER_CRC_LEN);
}
ifp->if_mtu = ETHERMTU;
if (ifp->if_tsolen <= 0) {
if ((ether_tsolen_default / ETHERMTU) < 2) {
kprintf("ether TSO maxlen %d -> %d\n",
ether_tsolen_default, ETHER_TSOLEN_DEFAULT);
ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
}
ifp->if_tsolen = ether_tsolen_default;
}
if (ifp->if_baudrate == 0)
ifp->if_baudrate = 10000000;
ifp->if_output = ether_output;
ifp->if_input = ether_input;
ifp->if_resolvemulti = ether_resolvemulti;
ifp->if_broadcastaddr = etherbroadcastaddr;
sdl = IF_LLSOCKADDR(ifp);
sdl->sdl_type = IFT_ETHER;
sdl->sdl_alen = ifp->if_addrlen;
bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
/*
* XXX Keep the current drivers happy.
* XXX Remove once all drivers have been cleaned up
*/
if (lla != IFP2AC(ifp)->ac_enaddr)
bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
bpfattach(ifp, dlt, hdrlen);
if (ng_ether_attach_p != NULL)
(*ng_ether_attach_p)(ifp);
if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
}
/*
* Perform common duties while detaching an Ethernet interface
*/
void
ether_ifdetach(struct ifnet *ifp)
{
if_down(ifp);
if (ng_ether_detach_p != NULL)
(*ng_ether_detach_p)(ifp);
bpfdetach(ifp);
if_detach(ifp);
}
int
ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct ifaddr *ifa = (struct ifaddr *) data;
struct ifreq *ifr = (struct ifreq *) data;
int error = 0;
#define IF_INIT(ifp) \
do { \
if (((ifp)->if_flags & IFF_UP) == 0) { \
(ifp)->if_flags |= IFF_UP; \
(ifp)->if_init((ifp)->if_softc); \
} \
} while (0)
ASSERT_IFNET_SERIALIZED_ALL(ifp);
switch (command) {
case SIOCSIFADDR:
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
IF_INIT(ifp); /* before arpwhohas */
arp_ifinit(ifp, ifa);
break;
#endif
default:
IF_INIT(ifp);
break;
}
break;
case SIOCGIFADDR:
case SIOCGHWADDR:
error = copyout(IFP2AC(ifp)->ac_enaddr,
((struct sockaddr *)ifr->ifr_data)->sa_data,
ETHER_ADDR_LEN);
break;
case SIOCSIFMTU:
/*
* Set the interface MTU.
*/
if (ifr->ifr_mtu > ETHERMTU) {
error = EINVAL;
} else {
ifp->if_mtu = ifr->ifr_mtu;
}
break;
default:
error = EINVAL;
break;
}
return (error);
#undef IF_INIT
}
static int
ether_resolvemulti(
struct ifnet *ifp,
struct sockaddr **llsa,
struct sockaddr *sa)
{
struct sockaddr_dl *sdl;
#ifdef INET
struct sockaddr_in *sin;
#endif
#ifdef INET6
struct sockaddr_in6 *sin6;
#endif
u_char *e_addr;
switch(sa->sa_family) {
case AF_LINK:
/*
* No mapping needed. Just check that it's a valid MC address.
*/
sdl = (struct sockaddr_dl *)sa;
e_addr = LLADDR(sdl);
if ((e_addr[0] & 1) != 1)
return EADDRNOTAVAIL;
*llsa = NULL;
return 0;
#ifdef INET
case AF_INET:
sin = (struct sockaddr_in *)sa;
if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
return EADDRNOTAVAIL;
sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
sdl->sdl_len = sizeof *sdl;
sdl->sdl_family = AF_LINK;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = IFT_ETHER;
sdl->sdl_alen = ETHER_ADDR_LEN;
e_addr = LLADDR(sdl);
ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
*llsa = (struct sockaddr *)sdl;
return 0;
#endif
#ifdef INET6
case AF_INET6:
sin6 = (struct sockaddr_in6 *)sa;
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
/*
* An IP6 address of 0 means listen to all
* of the Ethernet multicast address used for IP6.
* (This is used for multicast routers.)
*/
ifp->if_flags |= IFF_ALLMULTI;
*llsa = NULL;
return 0;
}
if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
return EADDRNOTAVAIL;
sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
sdl->sdl_len = sizeof *sdl;
sdl->sdl_family = AF_LINK;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = IFT_ETHER;
sdl->sdl_alen = ETHER_ADDR_LEN;
e_addr = LLADDR(sdl);
ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
*llsa = (struct sockaddr *)sdl;
return 0;
#endif
default:
/*
* Well, the text isn't quite right, but it's the name
* that counts...
*/
return EAFNOSUPPORT;
}
}
#if 0
/*
* This is for reference. We have a table-driven version
* of the little-endian crc32 generator, which is faster
* than the double-loop.
*/
uint32_t
ether_crc32_le(const uint8_t *buf, size_t len)
{
uint32_t c, crc, carry;
size_t i, j;
crc = 0xffffffffU; /* initial value */
for (i = 0; i < len; i++) {
c = buf[i];
for (j = 0; j < 8; j++) {
carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
crc >>= 1;
c >>= 1;
if (carry)
crc = (crc ^ ETHER_CRC_POLY_LE);
}
}
return (crc);
}
#else
uint32_t
ether_crc32_le(const uint8_t *buf, size_t len)
{
static const uint32_t crctab[] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
uint32_t crc;
size_t i;
crc = 0xffffffffU; /* initial value */
for (i = 0; i < len; i++) {
crc ^= buf[i];
crc = (crc >> 4) ^ crctab[crc & 0xf];
crc = (crc >> 4) ^ crctab[crc & 0xf];
}
return (crc);
}
#endif
uint32_t
ether_crc32_be(const uint8_t *buf, size_t len)
{
uint32_t c, crc, carry;
size_t i, j;
crc = 0xffffffffU; /* initial value */
for (i = 0; i < len; i++) {
c = buf[i];
for (j = 0; j < 8; j++) {
carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
crc <<= 1;
c >>= 1;
if (carry)
crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
}
}
return (crc);
}
/*
* find the size of ethernet header, and call classifier
*/
void
altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
struct altq_pktattr *pktattr)
{
struct ether_header *eh;
uint16_t ether_type;
int hlen, af, hdrsize;
hlen = sizeof(struct ether_header);
eh = mtod(m, struct ether_header *);
ether_type = ntohs(eh->ether_type);
if (ether_type < ETHERMTU) {
/* ick! LLC/SNAP */
struct llc *llc = (struct llc *)(eh + 1);
hlen += 8;
if (m->m_len < hlen ||
llc->llc_dsap != LLC_SNAP_LSAP ||
llc->llc_ssap != LLC_SNAP_LSAP ||
llc->llc_control != LLC_UI)
goto bad; /* not snap! */
ether_type = ntohs(llc->llc_un.type_snap.ether_type);
}
if (ether_type == ETHERTYPE_IP) {
af = AF_INET;
hdrsize = 20; /* sizeof(struct ip) */
#ifdef INET6
} else if (ether_type == ETHERTYPE_IPV6) {
af = AF_INET6;
hdrsize = 40; /* sizeof(struct ip6_hdr) */
#endif
} else
goto bad;
while (m->m_len <= hlen) {
hlen -= m->m_len;
m = m->m_next;
}
if (m->m_len < hlen + hdrsize) {
/*
* ip header is not in a single mbuf. this should not
* happen in the current code.
* (todo: use m_pulldown in the future)
*/
goto bad;
}
m->m_data += hlen;
m->m_len -= hlen;
ifq_classify(ifq, m, af, pktattr);
m->m_data -= hlen;
m->m_len += hlen;
return;
bad:
pktattr->pattr_class = NULL;
pktattr->pattr_hdr = NULL;
pktattr->pattr_af = AF_UNSPEC;
}
static void
ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
const struct ether_header *save_eh)
{
struct mbuf *m = *m0;
ether_restore_hdr++;
/*
* Prepend the header, optimize for the common case of
* eh pointing into the mbuf.
*/
if ((const void *)(eh + 1) == (void *)m->m_data) {
m->m_data -= ETHER_HDR_LEN;
m->m_len += ETHER_HDR_LEN;
m->m_pkthdr.len += ETHER_HDR_LEN;
} else {
ether_prepend_hdr++;
M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
if (m != NULL) {
bcopy(save_eh, mtod(m, struct ether_header *),
ETHER_HDR_LEN);
}
}
*m0 = m;
}
/*
* Upper layer processing for a received Ethernet packet.
*/
void
ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
{
struct ether_header *eh;
int isr, discard = 0;
u_short ether_type;
struct ip_fw *rule = NULL;
M_ASSERTPKTHDR(m);
KASSERT(m->m_len >= ETHER_HDR_LEN,
("ether header is not contiguous!"));
eh = mtod(m, struct ether_header *);
if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
struct m_tag *mtag;