-
Notifications
You must be signed in to change notification settings - Fork 24
/
if_ethersubr.c
1490 lines (1331 loc) · 34.6 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
/* $NetBSD: if_ethersubr.c,v 1.213 2015/08/31 08:05:20 ozaki-r 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) 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.2 (Berkeley) 4/4/96
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.213 2015/08/31 08:05:20 ozaki-r Exp $");
#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_atalk.h"
#include "opt_mbuftrace.h"
#include "opt_mpls.h"
#include "opt_gateway.h"
#include "opt_pppoe.h"
#include "opt_net_mpsafe.h"
#endif
#include "vlan.h"
#include "pppoe.h"
#include "bridge.h"
#include "arp.h"
#include "agr.h"
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/rnd.h>
#include <sys/rndsource.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/pktqueue.h>
#include <net/if_media.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#if NARP == 0
/*
* XXX there should really be a way to issue this warning from within config(8)
*/
#error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
#endif
#include <net/bpf.h>
#include <net/if_ether.h>
#include <net/if_vlanvar.h>
#if NPPPOE > 0
#include <net/if_pppoe.h>
#endif
#if NAGR > 0
#include <net/agr/ieee8023_slowprotocols.h> /* XXX */
#include <net/agr/ieee8023ad.h>
#include <net/agr/if_agrvar.h>
#endif
#if NBRIDGE > 0
#include <net/if_bridgevar.h>
#endif
#include <netinet/in.h>
#ifdef INET
#include <netinet/in_var.h>
#endif
#include <netinet/if_inarp.h>
#ifdef INET6
#ifndef INET
#include <netinet/in.h>
#endif
#include <netinet6/in6_var.h>
#include <netinet6/nd6.h>
#endif
#include "carp.h"
#if NCARP > 0
#include <netinet/ip_carp.h>
#endif
#ifdef NETATALK
#include <netatalk/at.h>
#include <netatalk/at_var.h>
#include <netatalk/at_extern.h>
#define llc_snap_org_code llc_un.type_snap.org_code
#define llc_snap_ether_type llc_un.type_snap.ether_type
extern u_char at_org_code[3];
extern u_char aarp_org_code[3];
#endif /* NETATALK */
#ifdef MPLS
#include <netmpls/mpls.h>
#include <netmpls/mpls_var.h>
#endif
static struct timeval bigpktppslim_last;
static int bigpktppslim = 2; /* XXX */
static int bigpktpps_count;
static kmutex_t bigpktpps_lock __cacheline_aligned;
const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
#define senderr(e) { error = (e); goto bad;}
static int ether_output(struct ifnet *, struct mbuf *,
const struct sockaddr *, struct rtentry *);
/*
* Ethernet output routine.
* Encapsulate a packet of type family for the local net.
* Assumes that ifp is actually pointer to ethercom structure.
*/
static int
ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
const struct sockaddr * const dst,
struct rtentry *rt)
{
uint16_t etype = 0;
int error = 0, hdrcmplt = 0;
uint8_t esrc[6], edst[6];
struct mbuf *m = m0;
struct mbuf *mcopy = NULL;
struct ether_header *eh;
struct ifnet *ifp = ifp0;
ALTQ_DECL(struct altq_pktattr pktattr;)
#ifdef INET
struct arphdr *ah;
#endif /* INET */
#ifdef NETATALK
struct at_ifaddr *aa;
#endif /* NETATALK */
#ifndef NET_MPSAFE
KASSERT(KERNEL_LOCKED_P());
#endif
#ifdef MBUFTRACE
m_claimm(m, ifp->if_mowner);
#endif
#if NCARP > 0
if (ifp->if_type == IFT_CARP) {
struct ifaddr *ifa;
/* loop back if this is going to the carp interface */
if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
(ifa = ifa_ifwithaddr(dst)) != NULL &&
ifa->ifa_ifp == ifp0)
return looutput(ifp0, m, dst, rt);
ifp = ifp->if_carpdev;
/* ac = (struct arpcom *)ifp; */
if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
(IFF_UP|IFF_RUNNING))
senderr(ENETDOWN);
}
#endif /* NCARP > 0 */
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
senderr(ENETDOWN);
switch (dst->sa_family) {
#ifdef INET
case AF_INET:
if (m->m_flags & M_BCAST)
(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
else if (m->m_flags & M_MCAST)
ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
else if (!arpresolve(ifp, rt, m, dst, edst))
return 0; /* if not yet resolved */
/* If broadcasting on a simplex interface, loopback a copy */
if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
mcopy = m_copy(m, 0, (int)M_COPYALL);
etype = htons(ETHERTYPE_IP);
break;
case AF_ARP:
ah = mtod(m, struct arphdr *);
if (m->m_flags & M_BCAST)
(void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
else {
void *tha = ar_tha(ah);
if (tha == NULL) {
/* fake with ARPHDR_IEEE1394 */
return 0;
}
memcpy(edst, tha, sizeof(edst));
}
ah->ar_hrd = htons(ARPHRD_ETHER);
switch (ntohs(ah->ar_op)) {
case ARPOP_REVREQUEST:
case ARPOP_REVREPLY:
etype = htons(ETHERTYPE_REVARP);
break;
case ARPOP_REQUEST:
case ARPOP_REPLY:
default:
etype = htons(ETHERTYPE_ARP);
}
break;
#endif
#ifdef INET6
case AF_INET6:
if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
/* something bad happened */
return (0);
}
etype = htons(ETHERTYPE_IPV6);
break;
#endif
#ifdef NETATALK
case AF_APPLETALK:
if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
#ifdef NETATALKDEBUG
printf("aarpresolv failed\n");
#endif /* NETATALKDEBUG */
return (0);
}
/*
* ifaddr is the first thing in at_ifaddr
*/
aa = (struct at_ifaddr *) at_ifawithnet(
(const struct sockaddr_at *)dst, ifp);
if (aa == NULL)
goto bad;
/*
* In the phase 2 case, we need to prepend an mbuf for the
* llc header. Since we must preserve the value of m,
* which is passed to us by value, we m_copy() the first
* mbuf, and use it for our llc header.
*/
if (aa->aa_flags & AFA_PHASE2) {
struct llc llc;
M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
llc.llc_control = LLC_UI;
memcpy(llc.llc_snap_org_code, at_org_code,
sizeof(llc.llc_snap_org_code));
llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
memcpy(mtod(m, void *), &llc, sizeof(struct llc));
} else {
etype = htons(ETHERTYPE_ATALK);
}
break;
#endif /* NETATALK */
case pseudo_AF_HDRCMPLT:
hdrcmplt = 1;
memcpy(esrc,
((const struct ether_header *)dst->sa_data)->ether_shost,
sizeof(esrc));
/* FALLTHROUGH */
case AF_UNSPEC:
memcpy(edst,
((const struct ether_header *)dst->sa_data)->ether_dhost,
sizeof(edst));
/* AF_UNSPEC doesn't swap the byte order of the ether_type. */
etype = ((const struct ether_header *)dst->sa_data)->ether_type;
break;
default:
printf("%s: can't handle af%d\n", ifp->if_xname,
dst->sa_family);
senderr(EAFNOSUPPORT);
}
#ifdef MPLS
{
struct m_tag *mtag;
mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
if (mtag != NULL) {
/* Having the tag itself indicates it's MPLS */
etype = htons(ETHERTYPE_MPLS);
m_tag_delete(m, mtag);
}
}
#endif
if (mcopy)
(void)looutput(ifp, mcopy, dst, rt);
/* If no ether type is set, this must be a 802.2 formatted packet.
*/
if (etype == 0)
etype = htons(m->m_pkthdr.len);
/*
* Add local net header. If no space in first mbuf,
* allocate another.
*/
M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
if (m == 0)
senderr(ENOBUFS);
eh = mtod(m, struct ether_header *);
/* Note: etype is already in network byte order. */
(void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
memcpy(eh->ether_dhost, edst, sizeof(edst));
if (hdrcmplt)
memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
else
memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
sizeof(eh->ether_shost));
#if NCARP > 0
if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
sizeof(eh->ether_shost));
}
#endif /* NCARP > 0 */
if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
return (error);
if (m == NULL)
return (0);
#if NBRIDGE > 0
/*
* Bridges require special output handling.
*/
if (ifp->if_bridge)
return (bridge_output(ifp, m, NULL, NULL));
#endif
#if NCARP > 0
if (ifp != ifp0)
ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
#endif /* NCARP > 0 */
#ifdef ALTQ
/*
* If ALTQ is enabled on the parent interface, do
* classification; the queueing discipline might not
* require classification, but might require the
* address family/header pointer in the pktattr.
*/
if (ALTQ_IS_ENABLED(&ifp->if_snd))
altq_etherclassify(&ifp->if_snd, m, &pktattr);
#endif
return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
bad:
if (m)
m_freem(m);
return (error);
}
#ifdef ALTQ
/*
* This routine is a slight hack to allow a packet to be classified
* if the Ethernet headers are present. It will go away when ALTQ's
* classification engine understands link headers.
*/
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;
void *hdr;
hlen = ETHER_HDR_LEN;
eh = mtod(m, struct ether_header *);
ether_type = htons(eh->ether_type);
if (ether_type < ETHERMTU) {
/* 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) {
/* Not SNAP. */
goto bad;
}
ether_type = htons(llc->llc_un.type_snap.ether_type);
}
switch (ether_type) {
case ETHERTYPE_IP:
af = AF_INET;
hdrsize = 20; /* sizeof(struct ip) */
break;
case ETHERTYPE_IPV6:
af = AF_INET6;
hdrsize = 40; /* sizeof(struct ip6_hdr) */
break;
default:
af = AF_UNSPEC;
hdrsize = 0;
break;
}
while (m->m_len <= hlen) {
hlen -= m->m_len;
m = m->m_next;
}
if (m->m_len < (hlen + hdrsize)) {
/*
* protocol header not in a single mbuf.
* We can't cope with this situation right
* now (but it shouldn't ever happen, really, anyhow).
*/
#ifdef DEBUG
printf("altq_etherclassify: headers span multiple mbufs: "
"%d < %d\n", m->m_len, (hlen + hdrsize));
#endif
goto bad;
}
m->m_data += hlen;
m->m_len -= hlen;
hdr = mtod(m, void *);
if (ALTQ_NEEDS_CLASSIFY(ifq))
pktattr->pattr_class =
(*ifq->altq_classify)(ifq->altq_clfier, m, af);
pktattr->pattr_af = af;
pktattr->pattr_hdr = hdr;
m->m_data -= hlen;
m->m_len += hlen;
return;
bad:
pktattr->pattr_class = NULL;
pktattr->pattr_hdr = NULL;
pktattr->pattr_af = AF_UNSPEC;
}
#endif /* ALTQ */
/*
* Process a received Ethernet packet;
* the packet is in the mbuf chain m with
* the ether header.
*/
void
ether_input(struct ifnet *ifp, struct mbuf *m)
{
struct ethercom *ec = (struct ethercom *) ifp;
pktqueue_t *pktq = NULL;
struct ifqueue *inq = NULL;
uint16_t etype;
struct ether_header *eh;
size_t ehlen;
static int earlypkts;
int isr = 0;
#if defined (LLC) || defined(NETATALK)
struct llc *l;
#endif
if ((ifp->if_flags & IFF_UP) == 0) {
m_freem(m);
return;
}
#ifdef MBUFTRACE
m_claimm(m, &ec->ec_rx_mowner);
#endif
eh = mtod(m, struct ether_header *);
etype = ntohs(eh->ether_type);
ehlen = sizeof(*eh);
if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
rnd_add_data(NULL, eh, ehlen, 0);
earlypkts++;
}
/*
* Determine if the packet is within its size limits.
*/
if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
mutex_enter(&bigpktpps_lock);
if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
bigpktppslim)) {
printf("%s: discarding oversize frame (len=%d)\n",
ifp->if_xname, m->m_pkthdr.len);
}
mutex_exit(&bigpktpps_lock);
m_freem(m);
return;
}
if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
/*
* If this is not a simplex interface, drop the packet
* if it came from us.
*/
if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
ETHER_ADDR_LEN) == 0) {
m_freem(m);
return;
}
if (memcmp(etherbroadcastaddr,
eh->ether_dhost, ETHER_ADDR_LEN) == 0)
m->m_flags |= M_BCAST;
else
m->m_flags |= M_MCAST;
ifp->if_imcasts++;
}
/* If the CRC is still on the packet, trim it off. */
if (m->m_flags & M_HASFCS) {
m_adj(m, -ETHER_CRC_LEN);
m->m_flags &= ~M_HASFCS;
}
ifp->if_ibytes += m->m_pkthdr.len;
#if NCARP > 0
if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
/*
* clear M_PROMISC, in case the packets comes from a
* vlan
*/
m->m_flags &= ~M_PROMISC;
if (carp_input(m, (uint8_t *)&eh->ether_shost,
(uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
return;
}
#endif /* NCARP > 0 */
if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
(ifp->if_flags & IFF_PROMISC) != 0 &&
memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
ETHER_ADDR_LEN) != 0) {
m->m_flags |= M_PROMISC;
}
if ((m->m_flags & M_PROMISC) == 0) {
if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
return;
if (m == NULL)
return;
eh = mtod(m, struct ether_header *);
etype = ntohs(eh->ether_type);
ehlen = sizeof(*eh);
}
#if NAGR > 0
if (ifp->if_agrprivate &&
__predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
m->m_flags &= ~M_PROMISC;
agr_input(ifp, m);
return;
}
#endif /* NAGR > 0 */
/*
* If VLANs are configured on the interface, check to
* see if the device performed the decapsulation and
* provided us with the tag.
*/
if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
#if NVLAN > 0
/*
* vlan_input() will either recursively call ether_input()
* or drop the packet.
*/
vlan_input(ifp, m);
#else
m_freem(m);
#endif
return;
}
/*
* Handle protocols that expect to have the Ethernet header
* (and possibly FCS) intact.
*/
switch (etype) {
case ETHERTYPE_VLAN: {
struct ether_vlan_header *evl = (void *)eh;
/*
* If there is a tag of 0, then the VLAN header was probably
* just being used to store the priority. Extract the ether
* type, and if IP or IPV6, let them deal with it.
*/
if (m->m_len <= sizeof(*evl)
&& EVL_VLANOFTAG(evl->evl_tag) == 0) {
etype = ntohs(evl->evl_proto);
ehlen = sizeof(*evl);
if ((m->m_flags & M_PROMISC) == 0
&& (etype == ETHERTYPE_IP
|| etype == ETHERTYPE_IPV6))
break;
}
#if NVLAN > 0
/*
* vlan_input() will either recursively call ether_input()
* or drop the packet.
*/
if (((struct ethercom *)ifp)->ec_nvlans != 0)
vlan_input(ifp, m);
else
#endif /* NVLAN > 0 */
m_freem(m);
return;
}
#if NPPPOE > 0
case ETHERTYPE_PPPOEDISC:
case ETHERTYPE_PPPOE:
if (m->m_flags & M_PROMISC) {
m_freem(m);
return;
}
#ifndef PPPOE_SERVER
if (m->m_flags & (M_MCAST | M_BCAST)) {
m_freem(m);
return;
}
#endif
if (etype == ETHERTYPE_PPPOEDISC)
inq = &ppoediscinq;
else
inq = &ppoeinq;
if (IF_QFULL(inq)) {
IF_DROP(inq);
m_freem(m);
} else {
IF_ENQUEUE(inq, m);
softint_schedule(pppoe_softintr);
}
return;
#endif /* NPPPOE > 0 */
case ETHERTYPE_SLOWPROTOCOLS: {
uint8_t subtype;
#if defined(DIAGNOSTIC)
if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
panic("ether_input: too short slow protocol packet");
}
#endif
m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
switch (subtype) {
#if NAGR > 0
case SLOWPROTOCOLS_SUBTYPE_LACP:
if (ifp->if_agrprivate) {
ieee8023ad_lacp_input(ifp, m);
return;
}
break;
case SLOWPROTOCOLS_SUBTYPE_MARKER:
if (ifp->if_agrprivate) {
ieee8023ad_marker_input(ifp, m);
return;
}
break;
#endif /* NAGR > 0 */
default:
if (subtype == 0 || subtype > 10) {
/* illegal value */
m_freem(m);
return;
}
/* unknown subtype */
break;
}
/* FALLTHROUGH */
}
default:
if (m->m_flags & M_PROMISC) {
m_freem(m);
return;
}
}
/* If the CRC is still on the packet, trim it off. */
if (m->m_flags & M_HASFCS) {
m_adj(m, -ETHER_CRC_LEN);
m->m_flags &= ~M_HASFCS;
}
if (etype > ETHERMTU + sizeof (struct ether_header)) {
/* Strip off the Ethernet header. */
m_adj(m, ehlen);
switch (etype) {
#ifdef INET
case ETHERTYPE_IP:
#ifdef GATEWAY
if (ipflow_fastforward(m))
return;
#endif
pktq = ip_pktq;
break;
case ETHERTYPE_ARP:
isr = NETISR_ARP;
inq = &arpintrq;
break;
case ETHERTYPE_REVARP:
revarpinput(m); /* XXX queue? */
return;
#endif
#ifdef INET6
case ETHERTYPE_IPV6:
if (__predict_false(!in6_present)) {
m_freem(m);
return;
}
#ifdef GATEWAY
if (ip6flow_fastforward(&m))
return;
#endif
pktq = ip6_pktq;
break;
#endif
#ifdef NETATALK
case ETHERTYPE_ATALK:
isr = NETISR_ATALK;
inq = &atintrq1;
break;
case ETHERTYPE_AARP:
/* probably this should be done with a NETISR as well */
aarpinput(ifp, m); /* XXX */
return;
#endif /* NETATALK */
#ifdef MPLS
case ETHERTYPE_MPLS:
isr = NETISR_MPLS;
inq = &mplsintrq;
break;
#endif
default:
m_freem(m);
return;
}
} else {
#if defined (LLC) || defined (NETATALK)
l = (struct llc *)(eh+1);
switch (l->llc_dsap) {
#ifdef NETATALK
case LLC_SNAP_LSAP:
switch (l->llc_control) {
case LLC_UI:
if (l->llc_ssap != LLC_SNAP_LSAP) {
goto dropanyway;
}
if (memcmp(&(l->llc_snap_org_code)[0],
at_org_code, sizeof(at_org_code)) == 0 &&
ntohs(l->llc_snap_ether_type) ==
ETHERTYPE_ATALK) {
inq = &atintrq2;
m_adj(m, sizeof(struct ether_header)
+ sizeof(struct llc));
isr = NETISR_ATALK;
break;
}
if (memcmp(&(l->llc_snap_org_code)[0],
aarp_org_code,
sizeof(aarp_org_code)) == 0 &&
ntohs(l->llc_snap_ether_type) ==
ETHERTYPE_AARP) {
m_adj( m, sizeof(struct ether_header)
+ sizeof(struct llc));
aarpinput(ifp, m); /* XXX */
return;
}
default:
goto dropanyway;
}
break;
dropanyway:
#endif
default:
m_freem(m);
return;
}
#else /* ISO || LLC || NETATALK*/
m_freem(m);
return;
#endif /* ISO || LLC || NETATALK*/
}
if (__predict_true(pktq)) {
const uint32_t h = pktq_rps_hash(m);
if (__predict_false(!pktq_enqueue(pktq, m, h))) {
m_freem(m);
}
return;
}
if (__predict_false(!inq)) {
/* Should not happen. */
m_freem(m);
return;
}
if (IF_QFULL(inq)) {
IF_DROP(inq);
m_freem(m);
} else {
IF_ENQUEUE(inq, m);
schednetisr(isr);
}
}
/*
* Convert Ethernet address to printable (loggable) representation.
*/
char *
ether_sprintf(const u_char *ap)
{
static char etherbuf[3 * ETHER_ADDR_LEN];
return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
}
char *
ether_snprintf(char *buf, size_t len, const u_char *ap)
{
char *cp = buf;
size_t i;
for (i = 0; i < len / 3; i++) {
*cp++ = hexdigits[*ap >> 4];
*cp++ = hexdigits[*ap++ & 0xf];
*cp++ = ':';
}
*--cp = '\0';
return buf;
}
/*
* Perform common duties while attaching to interface list
*/
void
ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
{
struct ethercom *ec = (struct ethercom *)ifp;
ifp->if_type = IFT_ETHER;
ifp->if_hdrlen = ETHER_HDR_LEN;
ifp->if_dlt = DLT_EN10MB;
ifp->if_mtu = ETHERMTU;
ifp->if_output = ether_output;
ifp->if_input = ether_input;
if (ifp->if_baudrate == 0)
ifp->if_baudrate = IF_Mbps(10); /* just a default */
if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
LIST_INIT(&ec->ec_multiaddrs);
ifp->if_broadcastaddr = etherbroadcastaddr;
bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
#ifdef MBUFTRACE
strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
sizeof(ec->ec_tx_mowner.mo_name));
strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
sizeof(ec->ec_tx_mowner.mo_descr));
strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
sizeof(ec->ec_rx_mowner.mo_name));
strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
sizeof(ec->ec_rx_mowner.mo_descr));
MOWNER_ATTACH(&ec->ec_tx_mowner);
MOWNER_ATTACH(&ec->ec_rx_mowner);
ifp->if_mowner = &ec->ec_tx_mowner;
#endif
}
void
ether_ifdetach(struct ifnet *ifp)
{
struct ethercom *ec = (void *) ifp;
struct ether_multi *enm;
int s;
/*
* Prevent further calls to ioctl (for example turning off
* promiscuous mode from the bridge code), which eventually can
* call if_init() which can cause panics because the interface
* is in the process of being detached. Return device not configured
* instead.
*/
ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
#if NBRIDGE > 0
if (ifp->if_bridge)
bridge_ifdetach(ifp);
#endif
bpf_detach(ifp);
#if NVLAN > 0
if (ec->ec_nvlans)
vlan_ifdetach(ifp);
#endif
s = splnet();
while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
LIST_REMOVE(enm, enm_list);
free(enm, M_IFMADDR);
ec->ec_multicnt--;
}
splx(s);
ifp->if_mowner = NULL;
MOWNER_DETACH(&ec->ec_rx_mowner);
MOWNER_DETACH(&ec->ec_tx_mowner);
}
#if 0