if_ethersubr.c revision 1.218 1 /* $NetBSD: if_ethersubr.c,v 1.218 2016/04/15 01:31:29 ozaki-r Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.218 2016/04/15 01:31:29 ozaki-r Exp $");
65
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_atalk.h"
69 #include "opt_mbuftrace.h"
70 #include "opt_mpls.h"
71 #include "opt_gateway.h"
72 #include "opt_pppoe.h"
73 #include "opt_net_mpsafe.h"
74 #endif
75
76 #include "vlan.h"
77 #include "pppoe.h"
78 #include "bridge.h"
79 #include "arp.h"
80 #include "agr.h"
81
82 #include <sys/sysctl.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/mutex.h>
86 #include <sys/ioctl.h>
87 #include <sys/errno.h>
88 #include <sys/device.h>
89 #include <sys/rnd.h>
90 #include <sys/rndsource.h>
91 #include <sys/cpu.h>
92
93 #include <net/if.h>
94 #include <net/netisr.h>
95 #include <net/route.h>
96 #include <net/if_llc.h>
97 #include <net/if_dl.h>
98 #include <net/if_types.h>
99 #include <net/pktqueue.h>
100
101 #include <net/if_media.h>
102 #include <dev/mii/mii.h>
103 #include <dev/mii/miivar.h>
104
105 #if NARP == 0
106 /*
107 * XXX there should really be a way to issue this warning from within config(8)
108 */
109 #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.
110 #endif
111
112 #include <net/bpf.h>
113
114 #include <net/if_ether.h>
115 #include <net/if_vlanvar.h>
116
117 #if NPPPOE > 0
118 #include <net/if_pppoe.h>
119 #endif
120
121 #if NAGR > 0
122 #include <net/agr/ieee8023_slowprotocols.h> /* XXX */
123 #include <net/agr/ieee8023ad.h>
124 #include <net/agr/if_agrvar.h>
125 #endif
126
127 #if NBRIDGE > 0
128 #include <net/if_bridgevar.h>
129 #endif
130
131 #include <netinet/in.h>
132 #ifdef INET
133 #include <netinet/in_var.h>
134 #endif
135 #include <netinet/if_inarp.h>
136
137 #ifdef INET6
138 #ifndef INET
139 #include <netinet/in.h>
140 #endif
141 #include <netinet6/in6_var.h>
142 #include <netinet6/nd6.h>
143 #endif
144
145
146 #include "carp.h"
147 #if NCARP > 0
148 #include <netinet/ip_carp.h>
149 #endif
150
151 #ifdef NETATALK
152 #include <netatalk/at.h>
153 #include <netatalk/at_var.h>
154 #include <netatalk/at_extern.h>
155
156 #define llc_snap_org_code llc_un.type_snap.org_code
157 #define llc_snap_ether_type llc_un.type_snap.ether_type
158
159 extern u_char at_org_code[3];
160 extern u_char aarp_org_code[3];
161 #endif /* NETATALK */
162
163 #ifdef MPLS
164 #include <netmpls/mpls.h>
165 #include <netmpls/mpls_var.h>
166 #endif
167
168 static struct timeval bigpktppslim_last;
169 static int bigpktppslim = 2; /* XXX */
170 static int bigpktpps_count;
171 static kmutex_t bigpktpps_lock __cacheline_aligned;
172
173
174 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
175 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
176 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
177 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
178 #define senderr(e) { error = (e); goto bad;}
179
180 static int ether_output(struct ifnet *, struct mbuf *,
181 const struct sockaddr *, struct rtentry *);
182
183 /*
184 * Ethernet output routine.
185 * Encapsulate a packet of type family for the local net.
186 * Assumes that ifp is actually pointer to ethercom structure.
187 */
188 static int
189 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
190 const struct sockaddr * const dst,
191 struct rtentry *rt)
192 {
193 uint16_t etype = 0;
194 int error = 0, hdrcmplt = 0;
195 uint8_t esrc[6], edst[6];
196 struct mbuf *m = m0;
197 struct mbuf *mcopy = NULL;
198 struct ether_header *eh;
199 struct ifnet *ifp = ifp0;
200 ALTQ_DECL(struct altq_pktattr pktattr;)
201 #ifdef INET
202 struct arphdr *ah;
203 #endif /* INET */
204 #ifdef NETATALK
205 struct at_ifaddr *aa;
206 #endif /* NETATALK */
207
208 #ifndef NET_MPSAFE
209 KASSERT(KERNEL_LOCKED_P());
210 #endif
211
212 #ifdef MBUFTRACE
213 m_claimm(m, ifp->if_mowner);
214 #endif
215
216 #if NCARP > 0
217 if (ifp->if_type == IFT_CARP) {
218 struct ifaddr *ifa;
219
220 /* loop back if this is going to the carp interface */
221 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
222 (ifa = ifa_ifwithaddr(dst)) != NULL &&
223 ifa->ifa_ifp == ifp0)
224 return looutput(ifp0, m, dst, rt);
225
226 ifp = ifp->if_carpdev;
227 /* ac = (struct arpcom *)ifp; */
228
229 if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
230 (IFF_UP|IFF_RUNNING))
231 senderr(ENETDOWN);
232 }
233 #endif /* NCARP > 0 */
234
235 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
236 senderr(ENETDOWN);
237
238 switch (dst->sa_family) {
239
240 #ifdef INET
241 case AF_INET:
242 if (m->m_flags & M_BCAST)
243 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
244 else if (m->m_flags & M_MCAST)
245 ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
246 else if ((error = arpresolve(ifp, rt, m, dst, edst,
247 sizeof(edst))) != 0)
248 return error == EWOULDBLOCK ? 0 : error;
249 /* If broadcasting on a simplex interface, loopback a copy */
250 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
251 mcopy = m_copy(m, 0, (int)M_COPYALL);
252 etype = htons(ETHERTYPE_IP);
253 break;
254
255 case AF_ARP:
256 ah = mtod(m, struct arphdr *);
257 if (m->m_flags & M_BCAST)
258 (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
259 else {
260 void *tha = ar_tha(ah);
261
262 if (tha == NULL) {
263 /* fake with ARPHDR_IEEE1394 */
264 return 0;
265 }
266 memcpy(edst, tha, sizeof(edst));
267 }
268
269 ah->ar_hrd = htons(ARPHRD_ETHER);
270
271 switch (ntohs(ah->ar_op)) {
272 case ARPOP_REVREQUEST:
273 case ARPOP_REVREPLY:
274 etype = htons(ETHERTYPE_REVARP);
275 break;
276
277 case ARPOP_REQUEST:
278 case ARPOP_REPLY:
279 default:
280 etype = htons(ETHERTYPE_ARP);
281 }
282
283 break;
284 #endif
285 #ifdef INET6
286 case AF_INET6:
287 if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
288 /* something bad happened */
289 return (0);
290 }
291 etype = htons(ETHERTYPE_IPV6);
292 break;
293 #endif
294 #ifdef NETATALK
295 case AF_APPLETALK:
296 if (aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
297 #ifdef NETATALKDEBUG
298 printf("aarpresolv failed\n");
299 #endif /* NETATALKDEBUG */
300 return (0);
301 }
302 /*
303 * ifaddr is the first thing in at_ifaddr
304 */
305 aa = (struct at_ifaddr *) at_ifawithnet(
306 (const struct sockaddr_at *)dst, ifp);
307 if (aa == NULL)
308 goto bad;
309
310 /*
311 * In the phase 2 case, we need to prepend an mbuf for the
312 * llc header. Since we must preserve the value of m,
313 * which is passed to us by value, we m_copy() the first
314 * mbuf, and use it for our llc header.
315 */
316 if (aa->aa_flags & AFA_PHASE2) {
317 struct llc llc;
318
319 M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
320 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
321 llc.llc_control = LLC_UI;
322 memcpy(llc.llc_snap_org_code, at_org_code,
323 sizeof(llc.llc_snap_org_code));
324 llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
325 memcpy(mtod(m, void *), &llc, sizeof(struct llc));
326 } else {
327 etype = htons(ETHERTYPE_ATALK);
328 }
329 break;
330 #endif /* NETATALK */
331 case pseudo_AF_HDRCMPLT:
332 hdrcmplt = 1;
333 memcpy(esrc,
334 ((const struct ether_header *)dst->sa_data)->ether_shost,
335 sizeof(esrc));
336 /* FALLTHROUGH */
337
338 case AF_UNSPEC:
339 memcpy(edst,
340 ((const struct ether_header *)dst->sa_data)->ether_dhost,
341 sizeof(edst));
342 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
343 etype = ((const struct ether_header *)dst->sa_data)->ether_type;
344 break;
345
346 default:
347 printf("%s: can't handle af%d\n", ifp->if_xname,
348 dst->sa_family);
349 senderr(EAFNOSUPPORT);
350 }
351
352 #ifdef MPLS
353 {
354 struct m_tag *mtag;
355 mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
356 if (mtag != NULL) {
357 /* Having the tag itself indicates it's MPLS */
358 etype = htons(ETHERTYPE_MPLS);
359 m_tag_delete(m, mtag);
360 }
361 }
362 #endif
363
364 if (mcopy)
365 (void)looutput(ifp, mcopy, dst, rt);
366
367 /* If no ether type is set, this must be a 802.2 formatted packet.
368 */
369 if (etype == 0)
370 etype = htons(m->m_pkthdr.len);
371 /*
372 * Add local net header. If no space in first mbuf,
373 * allocate another.
374 */
375 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
376 if (m == 0)
377 senderr(ENOBUFS);
378 eh = mtod(m, struct ether_header *);
379 /* Note: etype is already in network byte order. */
380 (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
381 memcpy(eh->ether_dhost, edst, sizeof(edst));
382 if (hdrcmplt)
383 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
384 else
385 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
386 sizeof(eh->ether_shost));
387
388 #if NCARP > 0
389 if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
390 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
391 sizeof(eh->ether_shost));
392 }
393 #endif /* NCARP > 0 */
394
395 if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
396 return (error);
397 if (m == NULL)
398 return (0);
399
400 #if NBRIDGE > 0
401 /*
402 * Bridges require special output handling.
403 */
404 if (ifp->if_bridge)
405 return (bridge_output(ifp, m, NULL, NULL));
406 #endif
407
408 #if NCARP > 0
409 if (ifp != ifp0)
410 ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
411 #endif /* NCARP > 0 */
412
413 #ifdef ALTQ
414 /*
415 * If ALTQ is enabled on the parent interface, do
416 * classification; the queueing discipline might not
417 * require classification, but might require the
418 * address family/header pointer in the pktattr.
419 */
420 if (ALTQ_IS_ENABLED(&ifp->if_snd))
421 altq_etherclassify(&ifp->if_snd, m, &pktattr);
422 #endif
423 return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
424
425 bad:
426 if (m)
427 m_freem(m);
428 return (error);
429 }
430
431 #ifdef ALTQ
432 /*
433 * This routine is a slight hack to allow a packet to be classified
434 * if the Ethernet headers are present. It will go away when ALTQ's
435 * classification engine understands link headers.
436 */
437 void
438 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
439 struct altq_pktattr *pktattr)
440 {
441 struct ether_header *eh;
442 uint16_t ether_type;
443 int hlen, af, hdrsize;
444 void *hdr;
445
446 hlen = ETHER_HDR_LEN;
447 eh = mtod(m, struct ether_header *);
448
449 ether_type = htons(eh->ether_type);
450
451 if (ether_type < ETHERMTU) {
452 /* LLC/SNAP */
453 struct llc *llc = (struct llc *)(eh + 1);
454 hlen += 8;
455
456 if (m->m_len < hlen ||
457 llc->llc_dsap != LLC_SNAP_LSAP ||
458 llc->llc_ssap != LLC_SNAP_LSAP ||
459 llc->llc_control != LLC_UI) {
460 /* Not SNAP. */
461 goto bad;
462 }
463
464 ether_type = htons(llc->llc_un.type_snap.ether_type);
465 }
466
467 switch (ether_type) {
468 case ETHERTYPE_IP:
469 af = AF_INET;
470 hdrsize = 20; /* sizeof(struct ip) */
471 break;
472
473 case ETHERTYPE_IPV6:
474 af = AF_INET6;
475 hdrsize = 40; /* sizeof(struct ip6_hdr) */
476 break;
477
478 default:
479 af = AF_UNSPEC;
480 hdrsize = 0;
481 break;
482 }
483
484 while (m->m_len <= hlen) {
485 hlen -= m->m_len;
486 m = m->m_next;
487 }
488 if (m->m_len < (hlen + hdrsize)) {
489 /*
490 * protocol header not in a single mbuf.
491 * We can't cope with this situation right
492 * now (but it shouldn't ever happen, really, anyhow).
493 */
494 #ifdef DEBUG
495 printf("altq_etherclassify: headers span multiple mbufs: "
496 "%d < %d\n", m->m_len, (hlen + hdrsize));
497 #endif
498 goto bad;
499 }
500
501 m->m_data += hlen;
502 m->m_len -= hlen;
503
504 hdr = mtod(m, void *);
505
506 if (ALTQ_NEEDS_CLASSIFY(ifq))
507 pktattr->pattr_class =
508 (*ifq->altq_classify)(ifq->altq_clfier, m, af);
509 pktattr->pattr_af = af;
510 pktattr->pattr_hdr = hdr;
511
512 m->m_data -= hlen;
513 m->m_len += hlen;
514
515 return;
516
517 bad:
518 pktattr->pattr_class = NULL;
519 pktattr->pattr_hdr = NULL;
520 pktattr->pattr_af = AF_UNSPEC;
521 }
522 #endif /* ALTQ */
523
524 /*
525 * Process a received Ethernet packet;
526 * the packet is in the mbuf chain m with
527 * the ether header.
528 */
529 void
530 ether_input(struct ifnet *ifp, struct mbuf *m)
531 {
532 struct ethercom *ec = (struct ethercom *) ifp;
533 pktqueue_t *pktq = NULL;
534 struct ifqueue *inq = NULL;
535 uint16_t etype;
536 struct ether_header *eh;
537 size_t ehlen;
538 static int earlypkts;
539 int isr = 0;
540 #if defined (LLC) || defined(NETATALK)
541 struct llc *l;
542 #endif
543
544 KASSERT(!cpu_intr_p());
545
546 if ((ifp->if_flags & IFF_UP) == 0) {
547 m_freem(m);
548 return;
549 }
550
551 #ifdef MBUFTRACE
552 m_claimm(m, &ec->ec_rx_mowner);
553 #endif
554 eh = mtod(m, struct ether_header *);
555 etype = ntohs(eh->ether_type);
556 ehlen = sizeof(*eh);
557
558 if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
559 rnd_add_data(NULL, eh, ehlen, 0);
560 earlypkts++;
561 }
562
563 /*
564 * Determine if the packet is within its size limits.
565 */
566 if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
567 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
568 mutex_enter(&bigpktpps_lock);
569 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
570 bigpktppslim)) {
571 printf("%s: discarding oversize frame (len=%d)\n",
572 ifp->if_xname, m->m_pkthdr.len);
573 }
574 mutex_exit(&bigpktpps_lock);
575 m_freem(m);
576 return;
577 }
578
579 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
580 /*
581 * If this is not a simplex interface, drop the packet
582 * if it came from us.
583 */
584 if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
585 memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
586 ETHER_ADDR_LEN) == 0) {
587 m_freem(m);
588 return;
589 }
590
591 if (memcmp(etherbroadcastaddr,
592 eh->ether_dhost, ETHER_ADDR_LEN) == 0)
593 m->m_flags |= M_BCAST;
594 else
595 m->m_flags |= M_MCAST;
596 ifp->if_imcasts++;
597 }
598
599 /* If the CRC is still on the packet, trim it off. */
600 if (m->m_flags & M_HASFCS) {
601 m_adj(m, -ETHER_CRC_LEN);
602 m->m_flags &= ~M_HASFCS;
603 }
604
605 ifp->if_ibytes += m->m_pkthdr.len;
606
607 #if NCARP > 0
608 if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
609 /*
610 * clear M_PROMISC, in case the packets comes from a
611 * vlan
612 */
613 m->m_flags &= ~M_PROMISC;
614 if (carp_input(m, (uint8_t *)&eh->ether_shost,
615 (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
616 return;
617 }
618 #endif /* NCARP > 0 */
619 if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
620 (ifp->if_flags & IFF_PROMISC) != 0 &&
621 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
622 ETHER_ADDR_LEN) != 0) {
623 m->m_flags |= M_PROMISC;
624 }
625
626 if ((m->m_flags & M_PROMISC) == 0) {
627 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
628 return;
629 if (m == NULL)
630 return;
631
632 eh = mtod(m, struct ether_header *);
633 etype = ntohs(eh->ether_type);
634 ehlen = sizeof(*eh);
635 }
636
637 #if NAGR > 0
638 if (ifp->if_agrprivate &&
639 __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
640 m->m_flags &= ~M_PROMISC;
641 agr_input(ifp, m);
642 return;
643 }
644 #endif /* NAGR > 0 */
645
646 /*
647 * If VLANs are configured on the interface, check to
648 * see if the device performed the decapsulation and
649 * provided us with the tag.
650 */
651 if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
652 #if NVLAN > 0
653 /*
654 * vlan_input() will either recursively call ether_input()
655 * or drop the packet.
656 */
657 vlan_input(ifp, m);
658 #else
659 m_freem(m);
660 #endif
661 return;
662 }
663
664 /*
665 * Handle protocols that expect to have the Ethernet header
666 * (and possibly FCS) intact.
667 */
668 switch (etype) {
669 case ETHERTYPE_VLAN: {
670 struct ether_vlan_header *evl = (void *)eh;
671 /*
672 * If there is a tag of 0, then the VLAN header was probably
673 * just being used to store the priority. Extract the ether
674 * type, and if IP or IPV6, let them deal with it.
675 */
676 if (m->m_len <= sizeof(*evl)
677 && EVL_VLANOFTAG(evl->evl_tag) == 0) {
678 etype = ntohs(evl->evl_proto);
679 ehlen = sizeof(*evl);
680 if ((m->m_flags & M_PROMISC) == 0
681 && (etype == ETHERTYPE_IP
682 || etype == ETHERTYPE_IPV6))
683 break;
684 }
685 #if NVLAN > 0
686 /*
687 * vlan_input() will either recursively call ether_input()
688 * or drop the packet.
689 */
690 if (((struct ethercom *)ifp)->ec_nvlans != 0)
691 vlan_input(ifp, m);
692 else
693 #endif /* NVLAN > 0 */
694 m_freem(m);
695 return;
696 }
697 #if NPPPOE > 0
698 case ETHERTYPE_PPPOEDISC:
699 pppoedisc_input(ifp, m);
700 return;
701 case ETHERTYPE_PPPOE:
702 pppoe_input(ifp, m);
703 return;
704 #endif /* NPPPOE > 0 */
705 case ETHERTYPE_SLOWPROTOCOLS: {
706 uint8_t subtype;
707
708 #if defined(DIAGNOSTIC)
709 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
710 panic("ether_input: too short slow protocol packet");
711 }
712 #endif
713 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
714 switch (subtype) {
715 #if NAGR > 0
716 case SLOWPROTOCOLS_SUBTYPE_LACP:
717 if (ifp->if_agrprivate) {
718 ieee8023ad_lacp_input(ifp, m);
719 return;
720 }
721 break;
722
723 case SLOWPROTOCOLS_SUBTYPE_MARKER:
724 if (ifp->if_agrprivate) {
725 ieee8023ad_marker_input(ifp, m);
726 return;
727 }
728 break;
729 #endif /* NAGR > 0 */
730 default:
731 if (subtype == 0 || subtype > 10) {
732 /* illegal value */
733 m_freem(m);
734 return;
735 }
736 /* unknown subtype */
737 break;
738 }
739 /* FALLTHROUGH */
740 }
741 default:
742 if (m->m_flags & M_PROMISC) {
743 m_freem(m);
744 return;
745 }
746 }
747
748 /* If the CRC is still on the packet, trim it off. */
749 if (m->m_flags & M_HASFCS) {
750 m_adj(m, -ETHER_CRC_LEN);
751 m->m_flags &= ~M_HASFCS;
752 }
753
754 if (etype > ETHERMTU + sizeof (struct ether_header)) {
755 /* Strip off the Ethernet header. */
756 m_adj(m, ehlen);
757
758 switch (etype) {
759 #ifdef INET
760 case ETHERTYPE_IP:
761 #ifdef GATEWAY
762 if (ipflow_fastforward(m))
763 return;
764 #endif
765 pktq = ip_pktq;
766 break;
767
768 case ETHERTYPE_ARP:
769 isr = NETISR_ARP;
770 inq = &arpintrq;
771 break;
772
773 case ETHERTYPE_REVARP:
774 revarpinput(m); /* XXX queue? */
775 return;
776 #endif
777 #ifdef INET6
778 case ETHERTYPE_IPV6:
779 if (__predict_false(!in6_present)) {
780 m_freem(m);
781 return;
782 }
783 #ifdef GATEWAY
784 if (ip6flow_fastforward(&m))
785 return;
786 #endif
787 pktq = ip6_pktq;
788 break;
789 #endif
790 #ifdef NETATALK
791 case ETHERTYPE_ATALK:
792 isr = NETISR_ATALK;
793 inq = &atintrq1;
794 break;
795 case ETHERTYPE_AARP:
796 /* probably this should be done with a NETISR as well */
797 aarpinput(ifp, m); /* XXX */
798 return;
799 #endif /* NETATALK */
800 #ifdef MPLS
801 case ETHERTYPE_MPLS:
802 isr = NETISR_MPLS;
803 inq = &mplsintrq;
804 break;
805 #endif
806 default:
807 m_freem(m);
808 return;
809 }
810 } else {
811 #if defined (LLC) || defined (NETATALK)
812 l = (struct llc *)(eh+1);
813 switch (l->llc_dsap) {
814 #ifdef NETATALK
815 case LLC_SNAP_LSAP:
816 switch (l->llc_control) {
817 case LLC_UI:
818 if (l->llc_ssap != LLC_SNAP_LSAP) {
819 goto dropanyway;
820 }
821
822 if (memcmp(&(l->llc_snap_org_code)[0],
823 at_org_code, sizeof(at_org_code)) == 0 &&
824 ntohs(l->llc_snap_ether_type) ==
825 ETHERTYPE_ATALK) {
826 inq = &atintrq2;
827 m_adj(m, sizeof(struct ether_header)
828 + sizeof(struct llc));
829 isr = NETISR_ATALK;
830 break;
831 }
832
833 if (memcmp(&(l->llc_snap_org_code)[0],
834 aarp_org_code,
835 sizeof(aarp_org_code)) == 0 &&
836 ntohs(l->llc_snap_ether_type) ==
837 ETHERTYPE_AARP) {
838 m_adj( m, sizeof(struct ether_header)
839 + sizeof(struct llc));
840 aarpinput(ifp, m); /* XXX */
841 return;
842 }
843
844 default:
845 goto dropanyway;
846 }
847 break;
848 dropanyway:
849 #endif
850 default:
851 m_freem(m);
852 return;
853 }
854 #else /* ISO || LLC || NETATALK*/
855 m_freem(m);
856 return;
857 #endif /* ISO || LLC || NETATALK*/
858 }
859
860 if (__predict_true(pktq)) {
861 const uint32_t h = pktq_rps_hash(m);
862 if (__predict_false(!pktq_enqueue(pktq, m, h))) {
863 m_freem(m);
864 }
865 return;
866 }
867
868 if (__predict_false(!inq)) {
869 /* Should not happen. */
870 m_freem(m);
871 return;
872 }
873 if (IF_QFULL(inq)) {
874 IF_DROP(inq);
875 m_freem(m);
876 } else {
877 IF_ENQUEUE(inq, m);
878 schednetisr(isr);
879 }
880 }
881
882 /*
883 * Convert Ethernet address to printable (loggable) representation.
884 */
885 char *
886 ether_sprintf(const u_char *ap)
887 {
888 static char etherbuf[3 * ETHER_ADDR_LEN];
889 return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
890 }
891
892 char *
893 ether_snprintf(char *buf, size_t len, const u_char *ap)
894 {
895 char *cp = buf;
896 size_t i;
897
898 for (i = 0; i < len / 3; i++) {
899 *cp++ = hexdigits[*ap >> 4];
900 *cp++ = hexdigits[*ap++ & 0xf];
901 *cp++ = ':';
902 }
903 *--cp = '\0';
904 return buf;
905 }
906
907 /*
908 * Perform common duties while attaching to interface list
909 */
910 void
911 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
912 {
913 struct ethercom *ec = (struct ethercom *)ifp;
914
915 ifp->if_type = IFT_ETHER;
916 ifp->if_hdrlen = ETHER_HDR_LEN;
917 ifp->if_dlt = DLT_EN10MB;
918 ifp->if_mtu = ETHERMTU;
919 ifp->if_output = ether_output;
920 ifp->_if_input = ether_input;
921 if (ifp->if_baudrate == 0)
922 ifp->if_baudrate = IF_Mbps(10); /* just a default */
923
924 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
925
926 LIST_INIT(&ec->ec_multiaddrs);
927 ifp->if_broadcastaddr = etherbroadcastaddr;
928 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
929 #ifdef MBUFTRACE
930 strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
931 sizeof(ec->ec_tx_mowner.mo_name));
932 strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
933 sizeof(ec->ec_tx_mowner.mo_descr));
934 strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
935 sizeof(ec->ec_rx_mowner.mo_name));
936 strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
937 sizeof(ec->ec_rx_mowner.mo_descr));
938 MOWNER_ATTACH(&ec->ec_tx_mowner);
939 MOWNER_ATTACH(&ec->ec_rx_mowner);
940 ifp->if_mowner = &ec->ec_tx_mowner;
941 #endif
942 }
943
944 void
945 ether_ifdetach(struct ifnet *ifp)
946 {
947 struct ethercom *ec = (void *) ifp;
948 struct ether_multi *enm;
949 int s;
950
951 /*
952 * Prevent further calls to ioctl (for example turning off
953 * promiscuous mode from the bridge code), which eventually can
954 * call if_init() which can cause panics because the interface
955 * is in the process of being detached. Return device not configured
956 * instead.
957 */
958 ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
959
960 #if NBRIDGE > 0
961 if (ifp->if_bridge)
962 bridge_ifdetach(ifp);
963 #endif
964
965 bpf_detach(ifp);
966
967 #if NVLAN > 0
968 if (ec->ec_nvlans)
969 vlan_ifdetach(ifp);
970 #endif
971
972 s = splnet();
973 while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
974 LIST_REMOVE(enm, enm_list);
975 free(enm, M_IFMADDR);
976 ec->ec_multicnt--;
977 }
978 splx(s);
979
980 ifp->if_mowner = NULL;
981 MOWNER_DETACH(&ec->ec_rx_mowner);
982 MOWNER_DETACH(&ec->ec_tx_mowner);
983 }
984
985 #if 0
986 /*
987 * This is for reference. We have a table-driven version
988 * of the little-endian crc32 generator, which is faster
989 * than the double-loop.
990 */
991 uint32_t
992 ether_crc32_le(const uint8_t *buf, size_t len)
993 {
994 uint32_t c, crc, carry;
995 size_t i, j;
996
997 crc = 0xffffffffU; /* initial value */
998
999 for (i = 0; i < len; i++) {
1000 c = buf[i];
1001 for (j = 0; j < 8; j++) {
1002 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1003 crc >>= 1;
1004 c >>= 1;
1005 if (carry)
1006 crc = (crc ^ ETHER_CRC_POLY_LE);
1007 }
1008 }
1009
1010 return (crc);
1011 }
1012 #else
1013 uint32_t
1014 ether_crc32_le(const uint8_t *buf, size_t len)
1015 {
1016 static const uint32_t crctab[] = {
1017 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1018 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1019 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1020 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1021 };
1022 uint32_t crc;
1023 size_t i;
1024
1025 crc = 0xffffffffU; /* initial value */
1026
1027 for (i = 0; i < len; i++) {
1028 crc ^= buf[i];
1029 crc = (crc >> 4) ^ crctab[crc & 0xf];
1030 crc = (crc >> 4) ^ crctab[crc & 0xf];
1031 }
1032
1033 return (crc);
1034 }
1035 #endif
1036
1037 uint32_t
1038 ether_crc32_be(const uint8_t *buf, size_t len)
1039 {
1040 uint32_t c, crc, carry;
1041 size_t i, j;
1042
1043 crc = 0xffffffffU; /* initial value */
1044
1045 for (i = 0; i < len; i++) {
1046 c = buf[i];
1047 for (j = 0; j < 8; j++) {
1048 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1049 crc <<= 1;
1050 c >>= 1;
1051 if (carry)
1052 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1053 }
1054 }
1055
1056 return (crc);
1057 }
1058
1059 #ifdef INET
1060 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1061 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1062 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1063 { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1064 #endif
1065 #ifdef INET6
1066 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1067 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1068 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1069 { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1070 #endif
1071
1072 /*
1073 * ether_aton implementation, not using a static buffer.
1074 */
1075 int
1076 ether_aton_r(u_char *dest, size_t len, const char *str)
1077 {
1078 const u_char *cp = (const void *)str;
1079 u_char *ep;
1080
1081 #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1082
1083 if (len < ETHER_ADDR_LEN)
1084 return ENOSPC;
1085
1086 ep = dest + ETHER_ADDR_LEN;
1087
1088 while (*cp) {
1089 if (!isxdigit(*cp))
1090 return EINVAL;
1091 *dest = atox(*cp);
1092 cp++;
1093 if (isxdigit(*cp)) {
1094 *dest = (*dest << 4) | atox(*cp);
1095 dest++;
1096 cp++;
1097 } else
1098 dest++;
1099 if (dest == ep)
1100 return *cp == '\0' ? 0 : ENAMETOOLONG;
1101 switch (*cp) {
1102 case ':':
1103 case '-':
1104 case '.':
1105 cp++;
1106 break;
1107 }
1108 }
1109 return ENOBUFS;
1110 }
1111
1112 /*
1113 * Convert a sockaddr into an Ethernet address or range of Ethernet
1114 * addresses.
1115 */
1116 int
1117 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1118 uint8_t addrhi[ETHER_ADDR_LEN])
1119 {
1120 #ifdef INET
1121 const struct sockaddr_in *sin;
1122 #endif /* INET */
1123 #ifdef INET6
1124 const struct sockaddr_in6 *sin6;
1125 #endif /* INET6 */
1126
1127 switch (sa->sa_family) {
1128
1129 case AF_UNSPEC:
1130 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1131 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1132 break;
1133
1134 #ifdef INET
1135 case AF_INET:
1136 sin = satocsin(sa);
1137 if (sin->sin_addr.s_addr == INADDR_ANY) {
1138 /*
1139 * An IP address of INADDR_ANY means listen to
1140 * or stop listening to all of the Ethernet
1141 * multicast addresses used for IP.
1142 * (This is for the sake of IP multicast routers.)
1143 */
1144 memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1145 memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1146 }
1147 else {
1148 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1149 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1150 }
1151 break;
1152 #endif
1153 #ifdef INET6
1154 case AF_INET6:
1155 sin6 = satocsin6(sa);
1156 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1157 /*
1158 * An IP6 address of 0 means listen to or stop
1159 * listening to all of the Ethernet multicast
1160 * address used for IP6.
1161 * (This is used for multicast routers.)
1162 */
1163 memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1164 memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1165 } else {
1166 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1167 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1168 }
1169 break;
1170 #endif
1171
1172 default:
1173 return EAFNOSUPPORT;
1174 }
1175 return 0;
1176 }
1177
1178 /*
1179 * Add an Ethernet multicast address or range of addresses to the list for a
1180 * given interface.
1181 */
1182 int
1183 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1184 {
1185 struct ether_multi *enm;
1186 u_char addrlo[ETHER_ADDR_LEN];
1187 u_char addrhi[ETHER_ADDR_LEN];
1188 int s = splnet(), error;
1189
1190 error = ether_multiaddr(sa, addrlo, addrhi);
1191 if (error != 0) {
1192 splx(s);
1193 return error;
1194 }
1195
1196 /*
1197 * Verify that we have valid Ethernet multicast addresses.
1198 */
1199 if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1200 splx(s);
1201 return EINVAL;
1202 }
1203 /*
1204 * See if the address range is already in the list.
1205 */
1206 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1207 if (enm != NULL) {
1208 /*
1209 * Found it; just increment the reference count.
1210 */
1211 ++enm->enm_refcount;
1212 splx(s);
1213 return 0;
1214 }
1215 /*
1216 * New address or range; malloc a new multicast record
1217 * and link it into the interface's multicast list.
1218 */
1219 enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1220 if (enm == NULL) {
1221 splx(s);
1222 return ENOBUFS;
1223 }
1224 memcpy(enm->enm_addrlo, addrlo, 6);
1225 memcpy(enm->enm_addrhi, addrhi, 6);
1226 enm->enm_refcount = 1;
1227 LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1228 ec->ec_multicnt++;
1229 splx(s);
1230 /*
1231 * Return ENETRESET to inform the driver that the list has changed
1232 * and its reception filter should be adjusted accordingly.
1233 */
1234 return ENETRESET;
1235 }
1236
1237 /*
1238 * Delete a multicast address record.
1239 */
1240 int
1241 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1242 {
1243 struct ether_multi *enm;
1244 u_char addrlo[ETHER_ADDR_LEN];
1245 u_char addrhi[ETHER_ADDR_LEN];
1246 int s = splnet(), error;
1247
1248 error = ether_multiaddr(sa, addrlo, addrhi);
1249 if (error != 0) {
1250 splx(s);
1251 return (error);
1252 }
1253
1254 /*
1255 * Look ur the address in our list.
1256 */
1257 ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1258 if (enm == NULL) {
1259 splx(s);
1260 return (ENXIO);
1261 }
1262 if (--enm->enm_refcount != 0) {
1263 /*
1264 * Still some claims to this record.
1265 */
1266 splx(s);
1267 return (0);
1268 }
1269 /*
1270 * No remaining claims to this record; unlink and free it.
1271 */
1272 LIST_REMOVE(enm, enm_list);
1273 free(enm, M_IFMADDR);
1274 ec->ec_multicnt--;
1275 splx(s);
1276 /*
1277 * Return ENETRESET to inform the driver that the list has changed
1278 * and its reception filter should be adjusted accordingly.
1279 */
1280 return (ENETRESET);
1281 }
1282
1283 void
1284 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1285 {
1286 ec->ec_ifflags_cb = cb;
1287 }
1288
1289 /*
1290 * Common ioctls for Ethernet interfaces. Note, we must be
1291 * called at splnet().
1292 */
1293 int
1294 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1295 {
1296 struct ethercom *ec = (void *) ifp;
1297 struct eccapreq *eccr;
1298 struct ifreq *ifr = (struct ifreq *)data;
1299 struct if_laddrreq *iflr = data;
1300 const struct sockaddr_dl *sdl;
1301 static const uint8_t zero[ETHER_ADDR_LEN];
1302 int error;
1303
1304 switch (cmd) {
1305 case SIOCINITIFADDR:
1306 {
1307 struct ifaddr *ifa = (struct ifaddr *)data;
1308 if (ifa->ifa_addr->sa_family != AF_LINK
1309 && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1310 (IFF_UP|IFF_RUNNING)) {
1311 ifp->if_flags |= IFF_UP;
1312 if ((error = (*ifp->if_init)(ifp)) != 0)
1313 return error;
1314 }
1315 #ifdef INET
1316 if (ifa->ifa_addr->sa_family == AF_INET)
1317 arp_ifinit(ifp, ifa);
1318 #endif /* INET */
1319 return 0;
1320 }
1321
1322 case SIOCSIFMTU:
1323 {
1324 int maxmtu;
1325
1326 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1327 maxmtu = ETHERMTU_JUMBO;
1328 else
1329 maxmtu = ETHERMTU;
1330
1331 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1332 return EINVAL;
1333 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1334 return error;
1335 else if (ifp->if_flags & IFF_UP) {
1336 /* Make sure the device notices the MTU change. */
1337 return (*ifp->if_init)(ifp);
1338 } else
1339 return 0;
1340 }
1341
1342 case SIOCSIFFLAGS:
1343 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1344 return error;
1345 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1346 case IFF_RUNNING:
1347 /*
1348 * If interface is marked down and it is running,
1349 * then stop and disable it.
1350 */
1351 (*ifp->if_stop)(ifp, 1);
1352 break;
1353 case IFF_UP:
1354 /*
1355 * If interface is marked up and it is stopped, then
1356 * start it.
1357 */
1358 return (*ifp->if_init)(ifp);
1359 case IFF_UP|IFF_RUNNING:
1360 error = 0;
1361 if (ec->ec_ifflags_cb == NULL ||
1362 (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1363 /*
1364 * Reset the interface to pick up
1365 * changes in any other flags that
1366 * affect the hardware state.
1367 */
1368 return (*ifp->if_init)(ifp);
1369 } else
1370 return error;
1371 case 0:
1372 break;
1373 }
1374 return 0;
1375 case SIOCGETHERCAP:
1376 eccr = (struct eccapreq *)data;
1377 eccr->eccr_capabilities = ec->ec_capabilities;
1378 eccr->eccr_capenable = ec->ec_capenable;
1379 return 0;
1380 case SIOCADDMULTI:
1381 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1382 case SIOCDELMULTI:
1383 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1384 case SIOCSIFMEDIA:
1385 case SIOCGIFMEDIA:
1386 if (ec->ec_mii == NULL)
1387 return ENOTTY;
1388 return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1389 case SIOCALIFADDR:
1390 sdl = satocsdl(sstocsa(&iflr->addr));
1391 if (sdl->sdl_family != AF_LINK)
1392 ;
1393 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1394 return EINVAL;
1395 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1396 return EINVAL;
1397 /*FALLTHROUGH*/
1398 default:
1399 return ifioctl_common(ifp, cmd, data);
1400 }
1401 return 0;
1402 }
1403
1404 /*
1405 * Enable/disable passing VLAN packets if the parent interface supports it.
1406 * Return:
1407 * 0: Ok
1408 * -1: Parent interface does not support vlans
1409 * >0: Error
1410 */
1411 int
1412 ether_enable_vlan_mtu(struct ifnet *ifp)
1413 {
1414 int error;
1415 struct ethercom *ec = (void *)ifp;
1416
1417 /* Already have VLAN's do nothing. */
1418 if (ec->ec_nvlans != 0)
1419 return 0;
1420
1421 /* Parent does not support VLAN's */
1422 if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
1423 return -1;
1424
1425 /*
1426 * Parent supports the VLAN_MTU capability,
1427 * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
1428 * enable it.
1429 */
1430 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1431
1432 /* Interface is down, defer for later */
1433 if ((ifp->if_flags & IFF_UP) == 0)
1434 return 0;
1435
1436 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1437 return 0;
1438
1439 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1440 return error;
1441 }
1442
1443 int
1444 ether_disable_vlan_mtu(struct ifnet *ifp)
1445 {
1446 int error;
1447 struct ethercom *ec = (void *)ifp;
1448
1449 /* We still have VLAN's, defer for later */
1450 if (ec->ec_nvlans != 0)
1451 return 0;
1452
1453 /* Parent does not support VLAB's, nothing to do. */
1454 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
1455 return -1;
1456
1457 /*
1458 * Disable Tx/Rx of VLAN-sized frames.
1459 */
1460 ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1461
1462 /* Interface is down, defer for later */
1463 if ((ifp->if_flags & IFF_UP) == 0)
1464 return 0;
1465
1466 if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
1467 return 0;
1468
1469 ec->ec_capenable |= ETHERCAP_VLAN_MTU;
1470 return error;
1471 }
1472
1473 static int
1474 ether_multicast_sysctl(SYSCTLFN_ARGS)
1475 {
1476 struct ether_multi *enm;
1477 struct ether_multi_sysctl addr;
1478 struct ifnet *ifp;
1479 struct ethercom *ec;
1480 int error;
1481 size_t written;
1482
1483 if (namelen != 1)
1484 return EINVAL;
1485
1486 ifp = if_byindex(name[0]);
1487 if (ifp == NULL)
1488 return ENODEV;
1489 if (ifp->if_type != IFT_ETHER) {
1490 *oldlenp = 0;
1491 return 0;
1492 }
1493 ec = (struct ethercom *)ifp;
1494
1495 if (oldp == NULL) {
1496 *oldlenp = ec->ec_multicnt * sizeof(addr);
1497 return 0;
1498 }
1499
1500 memset(&addr, 0, sizeof(addr));
1501 error = 0;
1502 written = 0;
1503
1504 LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1505 if (written + sizeof(addr) > *oldlenp)
1506 break;
1507 addr.enm_refcount = enm->enm_refcount;
1508 memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1509 memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1510 error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
1511 if (error)
1512 break;
1513 written += sizeof(addr);
1514 oldp = (char *)oldp + sizeof(addr);
1515 }
1516
1517 *oldlenp = written;
1518 return error;
1519 }
1520
1521 SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
1522 {
1523 const struct sysctlnode *rnode = NULL;
1524
1525 sysctl_createv(clog, 0, NULL, &rnode,
1526 CTLFLAG_PERMANENT,
1527 CTLTYPE_NODE, "ether",
1528 SYSCTL_DESCR("Ethernet-specific information"),
1529 NULL, 0, NULL, 0,
1530 CTL_NET, CTL_CREATE, CTL_EOL);
1531
1532 sysctl_createv(clog, 0, &rnode, NULL,
1533 CTLFLAG_PERMANENT,
1534 CTLTYPE_NODE, "multicast",
1535 SYSCTL_DESCR("multicast addresses"),
1536 ether_multicast_sysctl, 0, NULL, 0,
1537 CTL_CREATE, CTL_EOL);
1538 }
1539
1540 void
1541 etherinit(void)
1542 {
1543 mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1544 }
1545