1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1989, 1993
5 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_netgraph.h"
35 #include "opt_mbuf_profiling.h"
36 #include "opt_rss.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/devctl.h>
41 #include <sys/eventhandler.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/module.h>
48 #include <sys/msan.h>
49 #include <sys/proc.h>
50 #include <sys/priv.h>
51 #include <sys/random.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/uuid.h>
56 #ifdef KDB
57 #include <sys/kdb.h>
58 #endif
59
60 #include <net/ieee_oui.h>
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #include <net/if_private.h>
64 #include <net/if_arp.h>
65 #include <net/netisr.h>
66 #include <net/route.h>
67 #include <net/if_llc.h>
68 #include <net/if_dl.h>
69 #include <net/if_types.h>
70 #include <net/bpf.h>
71 #include <net/ethernet.h>
72 #include <net/if_bridgevar.h>
73 #include <net/if_vlan_var.h>
74 #include <net/if_llatbl.h>
75 #include <net/pfil.h>
76 #include <net/rss_config.h>
77 #include <net/vnet.h>
78
79 #include <netpfil/pf/pf_mtag.h>
80
81 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
85 #include <netinet/ip_carp.h>
86 #include <netinet/ip_var.h>
87 #endif
88 #ifdef INET6
89 #include <netinet6/nd6.h>
90 #endif
91 #include <security/mac/mac_framework.h>
92
93 #include <crypto/sha1.h>
94
95 #ifdef CTASSERT
96 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
97 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
98 #endif
99
100 VNET_DEFINE(pfil_head_t, link_pfil_head); /* Packet filter hooks */
101
102 /* netgraph node hooks for ng_ether(4) */
103 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
104 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
105 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
106 void (*ng_ether_attach_p)(struct ifnet *ifp);
107 void (*ng_ether_detach_p)(struct ifnet *ifp);
108
109 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
110
111 /* if_bridge(4) support */
112 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
113 bool (*bridge_same_p)(const void *, const void *);
114 void *(*bridge_get_softc_p)(struct ifnet *);
115 bool (*bridge_member_ifaddrs_p)(void);
116
117 /* if_lagg(4) support */
118 struct mbuf *(*lagg_input_ethernet_p)(struct ifnet *, struct mbuf *);
119
120 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
121 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
122
123 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
124 struct sockaddr *);
125 static int ether_requestencap(struct ifnet *, struct if_encap_req *);
126
127 static inline bool ether_do_pcp(struct ifnet *, struct mbuf *);
128
129 #define senderr(e) do { error = (e); goto bad;} while (0)
130
131 static void
update_mbuf_csumflags(struct mbuf * src,struct mbuf * dst)132 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
133 {
134 int csum_flags = 0;
135
136 if (src->m_pkthdr.csum_flags & CSUM_IP)
137 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
138 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
139 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
140 if (src->m_pkthdr.csum_flags & CSUM_SCTP)
141 csum_flags |= CSUM_SCTP_VALID;
142 dst->m_pkthdr.csum_flags |= csum_flags;
143 if (csum_flags & CSUM_DATA_VALID)
144 dst->m_pkthdr.csum_data = 0xffff;
145 }
146
147 /*
148 * Handle link-layer encapsulation requests.
149 */
150 static int
ether_requestencap(struct ifnet * ifp,struct if_encap_req * req)151 ether_requestencap(struct ifnet *ifp, struct if_encap_req *req)
152 {
153 struct ether_header *eh;
154 struct arphdr *ah;
155 uint16_t etype;
156 const u_char *lladdr;
157
158 if (req->rtype != IFENCAP_LL)
159 return (EOPNOTSUPP);
160
161 if (req->bufsize < ETHER_HDR_LEN)
162 return (ENOMEM);
163
164 eh = (struct ether_header *)req->buf;
165 lladdr = req->lladdr;
166 req->lladdr_off = 0;
167
168 switch (req->family) {
169 case AF_INET:
170 etype = htons(ETHERTYPE_IP);
171 break;
172 case AF_INET6:
173 etype = htons(ETHERTYPE_IPV6);
174 break;
175 case AF_ARP:
176 ah = (struct arphdr *)req->hdata;
177 ah->ar_hrd = htons(ARPHRD_ETHER);
178
179 switch(ntohs(ah->ar_op)) {
180 case ARPOP_REVREQUEST:
181 case ARPOP_REVREPLY:
182 etype = htons(ETHERTYPE_REVARP);
183 break;
184 case ARPOP_REQUEST:
185 case ARPOP_REPLY:
186 default:
187 etype = htons(ETHERTYPE_ARP);
188 break;
189 }
190
191 if (req->flags & IFENCAP_FLAG_BROADCAST)
192 lladdr = ifp->if_broadcastaddr;
193 break;
194 default:
195 return (EAFNOSUPPORT);
196 }
197
198 memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
199 memcpy(eh->ether_dhost, lladdr, ETHER_ADDR_LEN);
200 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
201 req->bufsize = sizeof(struct ether_header);
202
203 return (0);
204 }
205
206 static int
ether_resolve_addr(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro,u_char * phdr,uint32_t * pflags,struct llentry ** plle)207 ether_resolve_addr(struct ifnet *ifp, struct mbuf *m,
208 const struct sockaddr *dst, struct route *ro, u_char *phdr,
209 uint32_t *pflags, struct llentry **plle)
210 {
211 uint32_t lleflags = 0;
212 int error = 0;
213 #if defined(INET) || defined(INET6)
214 struct ether_header *eh = (struct ether_header *)phdr;
215 uint16_t etype;
216 #endif
217
218 if (plle)
219 *plle = NULL;
220
221 switch (dst->sa_family) {
222 #ifdef INET
223 case AF_INET:
224 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
225 error = arpresolve(ifp, 0, m, dst, phdr, &lleflags,
226 plle);
227 else {
228 if (m->m_flags & M_BCAST)
229 memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
230 ETHER_ADDR_LEN);
231 else {
232 const struct in_addr *a;
233 a = &(((const struct sockaddr_in *)dst)->sin_addr);
234 ETHER_MAP_IP_MULTICAST(a, eh->ether_dhost);
235 }
236 etype = htons(ETHERTYPE_IP);
237 memcpy(&eh->ether_type, &etype, sizeof(etype));
238 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
239 }
240 break;
241 #endif
242 #ifdef INET6
243 case AF_INET6:
244 if ((m->m_flags & M_MCAST) == 0) {
245 int af = RO_GET_FAMILY(ro, dst);
246 error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr,
247 &lleflags, plle);
248 } else {
249 const struct in6_addr *a6;
250 a6 = &(((const struct sockaddr_in6 *)dst)->sin6_addr);
251 ETHER_MAP_IPV6_MULTICAST(a6, eh->ether_dhost);
252 etype = htons(ETHERTYPE_IPV6);
253 memcpy(&eh->ether_type, &etype, sizeof(etype));
254 memcpy(eh->ether_shost, IF_LLADDR(ifp), ETHER_ADDR_LEN);
255 }
256 break;
257 #endif
258 default:
259 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
260 if (m != NULL)
261 m_freem(m);
262 return (EAFNOSUPPORT);
263 }
264
265 if (error == EHOSTDOWN) {
266 if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0)
267 error = EHOSTUNREACH;
268 }
269
270 if (error != 0)
271 return (error);
272
273 *pflags = RT_MAY_LOOP;
274 if (lleflags & LLE_IFADDR)
275 *pflags |= RT_L2_ME;
276
277 return (0);
278 }
279
280 /*
281 * Ethernet output routine.
282 * Encapsulate a packet of type family for the local net.
283 * Use trailer local net encapsulation if enough data in first
284 * packet leaves a multiple of 512 bytes of data in remainder.
285 */
286 int
ether_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)287 ether_output(struct ifnet *ifp, struct mbuf *m,
288 const struct sockaddr *dst, struct route *ro)
289 {
290 int error = 0;
291 char linkhdr[ETHER_HDR_LEN], *phdr;
292 struct ether_header *eh;
293 struct pf_mtag *t;
294 bool loop_copy;
295 int hlen; /* link layer header length */
296 uint32_t pflags;
297 struct llentry *lle = NULL;
298 int addref = 0;
299
300 phdr = NULL;
301 pflags = 0;
302 if (ro != NULL) {
303 /* XXX BPF uses ro_prepend */
304 if (ro->ro_prepend != NULL) {
305 phdr = ro->ro_prepend;
306 hlen = ro->ro_plen;
307 } else if (!(m->m_flags & (M_BCAST | M_MCAST))) {
308 if ((ro->ro_flags & RT_LLE_CACHE) != 0) {
309 lle = ro->ro_lle;
310 if (lle != NULL &&
311 (lle->la_flags & LLE_VALID) == 0) {
312 LLE_FREE(lle);
313 lle = NULL; /* redundant */
314 ro->ro_lle = NULL;
315 }
316 if (lle == NULL) {
317 /* if we lookup, keep cache */
318 addref = 1;
319 } else
320 /*
321 * Notify LLE code that
322 * the entry was used
323 * by datapath.
324 */
325 llentry_provide_feedback(lle);
326 }
327 if (lle != NULL) {
328 phdr = lle->r_linkdata;
329 hlen = lle->r_hdrlen;
330 pflags = lle->r_flags;
331 }
332 }
333 }
334
335 #ifdef MAC
336 error = mac_ifnet_check_transmit(ifp, m);
337 if (error)
338 senderr(error);
339 #endif
340
341 M_PROFILE(m);
342 if (ifp->if_flags & IFF_MONITOR)
343 senderr(ENETDOWN);
344 if (!((ifp->if_flags & IFF_UP) &&
345 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
346 senderr(ENETDOWN);
347
348 if (phdr == NULL) {
349 /* No prepend data supplied. Try to calculate ourselves. */
350 phdr = linkhdr;
351 hlen = ETHER_HDR_LEN;
352 error = ether_resolve_addr(ifp, m, dst, ro, phdr, &pflags,
353 addref ? &lle : NULL);
354 if (addref && lle != NULL)
355 ro->ro_lle = lle;
356 if (error != 0)
357 return (error == EWOULDBLOCK ? 0 : error);
358 }
359
360 if ((pflags & RT_L2_ME) != 0) {
361 update_mbuf_csumflags(m, m);
362 return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0));
363 }
364 loop_copy = (pflags & RT_MAY_LOOP) != 0;
365
366 /*
367 * Add local net header. If no space in first mbuf,
368 * allocate another.
369 *
370 * Note that we do prepend regardless of RT_HAS_HEADER flag.
371 * This is done because BPF code shifts m_data pointer
372 * to the end of ethernet header prior to calling if_output().
373 */
374 M_PREPEND(m, hlen, M_NOWAIT);
375 if (m == NULL)
376 senderr(ENOBUFS);
377 if ((pflags & RT_HAS_HEADER) == 0) {
378 eh = mtod(m, struct ether_header *);
379 memcpy(eh, phdr, hlen);
380 }
381
382 /*
383 * If a simplex interface, and the packet is being sent to our
384 * Ethernet address or a broadcast address, loopback a copy.
385 * XXX To make a simplex device behave exactly like a duplex
386 * device, we should copy in the case of sending to our own
387 * ethernet address (thus letting the original actually appear
388 * on the wire). However, we don't do that here for security
389 * reasons and compatibility with the original behavior.
390 */
391 if ((m->m_flags & M_BCAST) && loop_copy && (ifp->if_flags & IFF_SIMPLEX) &&
392 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
393 struct mbuf *n;
394
395 /*
396 * Because if_simloop() modifies the packet, we need a
397 * writable copy through m_dup() instead of a readonly
398 * one as m_copy[m] would give us. The alternative would
399 * be to modify if_simloop() to handle the readonly mbuf,
400 * but performancewise it is mostly equivalent (trading
401 * extra data copying vs. extra locking).
402 *
403 * XXX This is a local workaround. A number of less
404 * often used kernel parts suffer from the same bug.
405 * See PR kern/105943 for a proposed general solution.
406 */
407 if ((n = m_dup(m, M_NOWAIT)) != NULL) {
408 update_mbuf_csumflags(m, n);
409 (void)if_simloop(ifp, n, RO_GET_FAMILY(ro, dst), hlen);
410 } else
411 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
412 }
413
414 /*
415 * Bridges require special output handling.
416 */
417 if (ifp->if_bridge) {
418 BRIDGE_OUTPUT(ifp, m, error);
419 return (error);
420 }
421
422 #if defined(INET) || defined(INET6)
423 if (ifp->if_carp &&
424 (error = (*carp_output_p)(ifp, m, dst)))
425 goto bad;
426 #endif
427
428 /* Handle ng_ether(4) processing, if any */
429 if (ifp->if_l2com != NULL) {
430 KASSERT(ng_ether_output_p != NULL,
431 ("ng_ether_output_p is NULL"));
432 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
433 bad: if (m != NULL)
434 m_freem(m);
435 return (error);
436 }
437 if (m == NULL)
438 return (0);
439 }
440
441 /* Continue with link-layer output */
442 return ether_output_frame(ifp, m);
443 }
444
445 static bool
ether_set_pcp(struct mbuf ** mp,struct ifnet * ifp,uint8_t pcp)446 ether_set_pcp(struct mbuf **mp, struct ifnet *ifp, uint8_t pcp)
447 {
448 struct ether_8021q_tag qtag;
449 struct ether_header *eh;
450
451 eh = mtod(*mp, struct ether_header *);
452 if (eh->ether_type == htons(ETHERTYPE_VLAN) ||
453 eh->ether_type == htons(ETHERTYPE_QINQ)) {
454 (*mp)->m_flags &= ~M_VLANTAG;
455 return (true);
456 }
457
458 qtag.vid = 0;
459 qtag.pcp = pcp;
460 qtag.proto = ETHERTYPE_VLAN;
461 if (ether_8021q_frame(mp, ifp, ifp, &qtag))
462 return (true);
463 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
464 return (false);
465 }
466
467 /*
468 * Ethernet link layer output routine to send a raw frame to the device.
469 *
470 * This assumes that the 14 byte Ethernet header is present and contiguous
471 * in the first mbuf (if BRIDGE'ing).
472 */
473 int
ether_output_frame(struct ifnet * ifp,struct mbuf * m)474 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
475 {
476 if (ether_do_pcp(ifp, m) && !ether_set_pcp(&m, ifp, ifp->if_pcp))
477 return (0);
478
479 if (PFIL_HOOKED_OUT(V_link_pfil_head))
480 switch (pfil_mbuf_out(V_link_pfil_head, &m, ifp, NULL)) {
481 case PFIL_DROPPED:
482 return (EACCES);
483 case PFIL_CONSUMED:
484 return (0);
485 }
486
487 #ifdef EXPERIMENTAL
488 #if defined(INET6) && defined(INET)
489 /* draft-ietf-6man-ipv6only-flag */
490 /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */
491 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) {
492 struct ether_header *eh;
493
494 eh = mtod(m, struct ether_header *);
495 switch (ntohs(eh->ether_type)) {
496 case ETHERTYPE_IP:
497 case ETHERTYPE_ARP:
498 case ETHERTYPE_REVARP:
499 m_freem(m);
500 return (EAFNOSUPPORT);
501 /* NOTREACHED */
502 break;
503 };
504 }
505 #endif
506 #endif
507
508 /*
509 * Queue message on interface, update output statistics if successful,
510 * and start output if interface not yet active.
511 *
512 * If KMSAN is enabled, use it to verify that the data does not contain
513 * any uninitialized bytes.
514 */
515 kmsan_check_mbuf(m, "ether_output");
516 return ((ifp->if_transmit)(ifp, m));
517 }
518
519 /*
520 * Process a received Ethernet packet; the packet is in the
521 * mbuf chain m with the ethernet header at the front.
522 */
523 static void
ether_input_internal(struct ifnet * ifp,struct mbuf * m)524 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
525 {
526 struct ether_header *eh;
527 u_short etype;
528
529 if ((ifp->if_flags & IFF_UP) == 0) {
530 m_freem(m);
531 return;
532 }
533 #ifdef DIAGNOSTIC
534 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
535 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
536 m_freem(m);
537 return;
538 }
539 #endif
540 if (__predict_false(m->m_len < ETHER_HDR_LEN)) {
541 /* Drivers should pullup and ensure the mbuf is valid */
542 if_printf(ifp, "discard frame w/o leading ethernet "
543 "header (len %d pkt len %d)\n",
544 m->m_len, m->m_pkthdr.len);
545 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
546 m_freem(m);
547 return;
548 }
549 eh = mtod(m, struct ether_header *);
550 etype = ntohs(eh->ether_type);
551 random_harvest_queue_ether(m, sizeof(*m));
552
553 #ifdef EXPERIMENTAL
554 #if defined(INET6) && defined(INET)
555 /* draft-ietf-6man-ipv6only-flag */
556 /* Catch ETHERTYPE_IP, and ETHERTYPE_[REV]ARP if we are v6-only. */
557 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY_MASK) != 0) {
558 switch (etype) {
559 case ETHERTYPE_IP:
560 case ETHERTYPE_ARP:
561 case ETHERTYPE_REVARP:
562 m_freem(m);
563 return;
564 /* NOTREACHED */
565 break;
566 };
567 }
568 #endif
569 #endif
570
571 CURVNET_SET_QUIET(ifp->if_vnet);
572
573 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
574 if (ETHER_IS_BROADCAST(eh->ether_dhost))
575 m->m_flags |= M_BCAST;
576 else
577 m->m_flags |= M_MCAST;
578 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
579 }
580
581 #ifdef MAC
582 /*
583 * Tag the mbuf with an appropriate MAC label before any other
584 * consumers can get to it.
585 */
586 mac_ifnet_create_mbuf(ifp, m);
587 #endif
588
589 /*
590 * Give bpf a chance at the packet.
591 */
592 ETHER_BPF_MTAP(ifp, m);
593
594 if (!(ifp->if_capenable & IFCAP_HWSTATS))
595 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
596
597 /* Allow monitor mode to claim this frame, after stats are updated. */
598 if (ifp->if_flags & IFF_MONITOR) {
599 m_freem(m);
600 CURVNET_RESTORE();
601 return;
602 }
603
604 /* Handle input from a lagg(4) port */
605 if (ifp->if_type == IFT_IEEE8023ADLAG) {
606 KASSERT(lagg_input_ethernet_p != NULL,
607 ("%s: if_lagg not loaded!", __func__));
608 m = (*lagg_input_ethernet_p)(ifp, m);
609 if (m != NULL)
610 ifp = m->m_pkthdr.rcvif;
611 else {
612 CURVNET_RESTORE();
613 return;
614 }
615 }
616
617 /*
618 * If the hardware did not process an 802.1Q tag, do this now,
619 * to allow 802.1P priority frames to be passed to the main input
620 * path correctly.
621 */
622 if ((m->m_flags & M_VLANTAG) == 0 &&
623 ((etype == ETHERTYPE_VLAN) || (etype == ETHERTYPE_QINQ))) {
624 struct ether_vlan_header *evl;
625
626 if (m->m_len < sizeof(*evl) &&
627 (m = m_pullup(m, sizeof(*evl))) == NULL) {
628 #ifdef DIAGNOSTIC
629 if_printf(ifp, "cannot pullup VLAN header\n");
630 #endif
631 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
632 CURVNET_RESTORE();
633 return;
634 }
635
636 evl = mtod(m, struct ether_vlan_header *);
637 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
638 m->m_flags |= M_VLANTAG;
639
640 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
641 ETHER_HDR_LEN - ETHER_TYPE_LEN);
642 m_adj(m, ETHER_VLAN_ENCAP_LEN);
643 eh = mtod(m, struct ether_header *);
644 }
645
646 M_SETFIB(m, ifp->if_fib);
647
648 /* Allow ng_ether(4) to claim this frame. */
649 if (ifp->if_l2com != NULL) {
650 KASSERT(ng_ether_input_p != NULL,
651 ("%s: ng_ether_input_p is NULL", __func__));
652 m->m_flags &= ~M_PROMISC;
653 (*ng_ether_input_p)(ifp, &m);
654 if (m == NULL) {
655 CURVNET_RESTORE();
656 return;
657 }
658 eh = mtod(m, struct ether_header *);
659 }
660
661 /*
662 * Allow if_bridge(4) to claim this frame.
663 *
664 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
665 * and the frame should be delivered locally.
666 *
667 * If M_BRIDGE_INJECT is set, the packet was received directly by the
668 * bridge via netmap, so "ifp" is the bridge itself and the packet
669 * should be re-examined.
670 */
671 if (ifp->if_bridge != NULL || (m->m_flags & M_BRIDGE_INJECT) != 0) {
672 m->m_flags &= ~M_PROMISC;
673 BRIDGE_INPUT(ifp, m);
674 if (m == NULL) {
675 CURVNET_RESTORE();
676 return;
677 }
678 eh = mtod(m, struct ether_header *);
679 }
680
681 #if defined(INET) || defined(INET6)
682 /*
683 * Clear M_PROMISC on frame so that carp(4) will see it when the
684 * mbuf flows up to Layer 3.
685 * FreeBSD's implementation of carp(4) uses the inprotosw
686 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
687 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
688 * is outside the scope of the M_PROMISC test below.
689 * TODO: Maintain a hash table of ethernet addresses other than
690 * ether_dhost which may be active on this ifp.
691 */
692 if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
693 m->m_flags &= ~M_PROMISC;
694 } else
695 #endif
696 {
697 /*
698 * If the frame received was not for our MAC address, set the
699 * M_PROMISC flag on the mbuf chain. The frame may need to
700 * be seen by the rest of the Ethernet input path in case of
701 * re-entry (e.g. bridge, vlan, netgraph) but should not be
702 * seen by upper protocol layers.
703 */
704 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
705 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
706 m->m_flags |= M_PROMISC;
707 }
708
709 ether_demux(ifp, m);
710 CURVNET_RESTORE();
711 }
712
713 /*
714 * Ethernet input dispatch; by default, direct dispatch here regardless of
715 * global configuration. However, if RSS is enabled, hook up RSS affinity
716 * so that when deferred or hybrid dispatch is enabled, we can redistribute
717 * load based on RSS.
718 *
719 * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
720 * not it had already done work distribution via multi-queue. Then we could
721 * direct dispatch in the event load balancing was already complete and
722 * handle the case of interfaces with different capabilities better.
723 *
724 * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
725 * at multiple layers?
726 *
727 * XXXRW: For now, enable all this only if RSS is compiled in, although it
728 * works fine without RSS. Need to characterise the performance overhead
729 * of the detour through the netisr code in the event the result is always
730 * direct dispatch.
731 */
732 static void
ether_nh_input(struct mbuf * m)733 ether_nh_input(struct mbuf *m)
734 {
735
736 M_ASSERTPKTHDR(m);
737 KASSERT(m->m_pkthdr.rcvif != NULL,
738 ("%s: NULL interface pointer", __func__));
739 ether_input_internal(m->m_pkthdr.rcvif, m);
740 }
741
742 static struct netisr_handler ether_nh = {
743 .nh_name = "ether",
744 .nh_handler = ether_nh_input,
745 .nh_proto = NETISR_ETHER,
746 #ifdef RSS
747 .nh_policy = NETISR_POLICY_CPU,
748 .nh_dispatch = NETISR_DISPATCH_DIRECT,
749 .nh_m2cpuid = rss_m2cpuid,
750 #else
751 .nh_policy = NETISR_POLICY_SOURCE,
752 .nh_dispatch = NETISR_DISPATCH_DIRECT,
753 #endif
754 };
755
756 static void
ether_init(__unused void * arg)757 ether_init(__unused void *arg)
758 {
759
760 netisr_register(ðer_nh);
761 }
762 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
763
764 static void
vnet_ether_init(const __unused void * arg)765 vnet_ether_init(const __unused void *arg)
766 {
767 struct pfil_head_args args;
768
769 args.pa_version = PFIL_VERSION;
770 args.pa_flags = PFIL_IN | PFIL_OUT;
771 args.pa_type = PFIL_TYPE_ETHERNET;
772 args.pa_headname = PFIL_ETHER_NAME;
773 V_link_pfil_head = pfil_head_register(&args);
774
775 #ifdef VIMAGE
776 netisr_register_vnet(ðer_nh);
777 #endif
778 }
779 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
780 vnet_ether_init, NULL);
781
782 #ifdef VIMAGE
783 static void
vnet_ether_pfil_destroy(const __unused void * arg)784 vnet_ether_pfil_destroy(const __unused void *arg)
785 {
786
787 pfil_head_unregister(V_link_pfil_head);
788 }
789 VNET_SYSUNINIT(vnet_ether_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_ANY,
790 vnet_ether_pfil_destroy, NULL);
791
792 static void
vnet_ether_destroy(__unused void * arg)793 vnet_ether_destroy(__unused void *arg)
794 {
795
796 netisr_unregister_vnet(ðer_nh);
797 }
798 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
799 vnet_ether_destroy, NULL);
800 #endif
801
802 static void
ether_input(struct ifnet * ifp,struct mbuf * m)803 ether_input(struct ifnet *ifp, struct mbuf *m)
804 {
805 struct epoch_tracker et;
806 struct mbuf *mn;
807 bool needs_epoch;
808
809 needs_epoch = (ifp->if_flags & IFF_NEEDSEPOCH);
810 #ifdef INVARIANTS
811 /*
812 * This temporary code is here to prevent epoch unaware and unmarked
813 * drivers to panic the system. Once all drivers are taken care of,
814 * the whole INVARIANTS block should go away.
815 */
816 if (!needs_epoch && !in_epoch(net_epoch_preempt)) {
817 static bool printedonce;
818
819 needs_epoch = true;
820 if (!printedonce) {
821 printedonce = true;
822 if_printf(ifp, "called %s w/o net epoch! "
823 "PLEASE file a bug report.", __func__);
824 #ifdef KDB
825 kdb_backtrace();
826 #endif
827 }
828 }
829 #endif
830
831 /*
832 * The drivers are allowed to pass in a chain of packets linked with
833 * m_nextpkt. We split them up into separate packets here and pass
834 * them up. This allows the drivers to amortize the receive lock.
835 */
836 CURVNET_SET_QUIET(ifp->if_vnet);
837 if (__predict_false(needs_epoch))
838 NET_EPOCH_ENTER(et);
839 while (m) {
840 mn = m->m_nextpkt;
841 m->m_nextpkt = NULL;
842
843 /*
844 * We will rely on rcvif being set properly in the deferred
845 * context, so assert it is correct here.
846 */
847 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
848 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch m %p "
849 "rcvif %p ifp %p", __func__, m, m->m_pkthdr.rcvif, ifp));
850 netisr_dispatch(NETISR_ETHER, m);
851 m = mn;
852 }
853 if (__predict_false(needs_epoch))
854 NET_EPOCH_EXIT(et);
855 CURVNET_RESTORE();
856 }
857
858 /*
859 * Upper layer processing for a received Ethernet packet.
860 */
861 void
ether_demux(struct ifnet * ifp,struct mbuf * m)862 ether_demux(struct ifnet *ifp, struct mbuf *m)
863 {
864 struct ether_header *eh;
865 int i, isr;
866 u_short ether_type;
867
868 NET_EPOCH_ASSERT();
869 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
870
871 /* Do not grab PROMISC frames in case we are re-entered. */
872 if (PFIL_HOOKED_IN(V_link_pfil_head) && !(m->m_flags & M_PROMISC)) {
873 i = pfil_mbuf_in(V_link_pfil_head, &m, ifp, NULL);
874 if (i != PFIL_PASS)
875 return;
876 }
877
878 eh = mtod(m, struct ether_header *);
879 ether_type = ntohs(eh->ether_type);
880
881 /*
882 * If this frame has a VLAN tag other than 0, call vlan_input()
883 * if its module is loaded. Otherwise, drop.
884 */
885 if ((m->m_flags & M_VLANTAG) &&
886 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
887 if (ifp->if_vlantrunk == NULL) {
888 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
889 m_freem(m);
890 return;
891 }
892 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
893 __func__));
894 /* Clear before possibly re-entering ether_input(). */
895 m->m_flags &= ~M_PROMISC;
896 (*vlan_input_p)(ifp, m);
897 return;
898 }
899
900 /*
901 * Pass promiscuously received frames to the upper layer if the user
902 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
903 */
904 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
905 m_freem(m);
906 return;
907 }
908
909 /*
910 * Reset layer specific mbuf flags to avoid confusing upper layers.
911 */
912 m->m_flags &= ~M_VLANTAG;
913 m_clrprotoflags(m);
914
915 /*
916 * Dispatch frame to upper layer.
917 */
918 switch (ether_type) {
919 #ifdef INET
920 case ETHERTYPE_IP:
921 isr = NETISR_IP;
922 break;
923
924 case ETHERTYPE_ARP:
925 if (ifp->if_flags & IFF_NOARP) {
926 /* Discard packet if ARP is disabled on interface */
927 m_freem(m);
928 return;
929 }
930 isr = NETISR_ARP;
931 break;
932 #endif
933 #ifdef INET6
934 case ETHERTYPE_IPV6:
935 isr = NETISR_IPV6;
936 break;
937 #endif
938 default:
939 goto discard;
940 }
941
942 /* Strip off Ethernet header. */
943 m_adj(m, ETHER_HDR_LEN);
944
945 netisr_dispatch(isr, m);
946 return;
947
948 discard:
949 /*
950 * Packet is to be discarded. If netgraph is present,
951 * hand the packet to it for last chance processing;
952 * otherwise dispose of it.
953 */
954 if (ifp->if_l2com != NULL) {
955 KASSERT(ng_ether_input_orphan_p != NULL,
956 ("ng_ether_input_orphan_p is NULL"));
957 (*ng_ether_input_orphan_p)(ifp, m);
958 return;
959 }
960 m_freem(m);
961 }
962
963 /*
964 * Convert Ethernet address to printable (loggable) representation.
965 * This routine is for compatibility; it's better to just use
966 *
967 * printf("%6D", <pointer to address>, ":");
968 *
969 * since there's no static buffer involved.
970 */
971 char *
ether_sprintf(const u_char * ap)972 ether_sprintf(const u_char *ap)
973 {
974 static char etherbuf[18];
975 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
976 return (etherbuf);
977 }
978
979 /*
980 * Perform common duties while attaching to interface list
981 */
982 void
ether_ifattach(struct ifnet * ifp,const u_int8_t * lla)983 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
984 {
985 int i;
986 struct ifaddr *ifa;
987 struct sockaddr_dl *sdl;
988
989 ifp->if_addrlen = ETHER_ADDR_LEN;
990 ifp->if_hdrlen = ETHER_HDR_LEN;
991 ifp->if_mtu = ETHERMTU;
992 if_attach(ifp);
993 ifp->if_output = ether_output;
994 ifp->if_input = ether_input;
995 ifp->if_resolvemulti = ether_resolvemulti;
996 ifp->if_requestencap = ether_requestencap;
997 #ifdef VIMAGE
998 ifp->if_reassign = ether_reassign;
999 #endif
1000 if (ifp->if_baudrate == 0)
1001 ifp->if_baudrate = IF_Mbps(10); /* just a default */
1002 ifp->if_broadcastaddr = etherbroadcastaddr;
1003
1004 ifa = ifp->if_addr;
1005 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
1006 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1007 sdl->sdl_type = IFT_ETHER;
1008 sdl->sdl_alen = ifp->if_addrlen;
1009 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
1010
1011 if (ifp->if_hw_addr != NULL)
1012 bcopy(lla, ifp->if_hw_addr, ifp->if_addrlen);
1013
1014 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
1015 if (ng_ether_attach_p != NULL)
1016 (*ng_ether_attach_p)(ifp);
1017
1018 /* Announce Ethernet MAC address if non-zero. */
1019 for (i = 0; i < ifp->if_addrlen; i++)
1020 if (lla[i] != 0)
1021 break;
1022 if (i != ifp->if_addrlen)
1023 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
1024
1025 uuid_ether_add(LLADDR(sdl));
1026
1027 /* Add necessary bits are setup; announce it now. */
1028 EVENTHANDLER_INVOKE(ether_ifattach_event, ifp);
1029 if (IS_DEFAULT_VNET(curvnet))
1030 devctl_notify("ETHERNET", ifp->if_xname, "IFATTACH", NULL);
1031 }
1032
1033 /*
1034 * Perform common duties while detaching an Ethernet interface
1035 */
1036 void
ether_ifdetach(struct ifnet * ifp)1037 ether_ifdetach(struct ifnet *ifp)
1038 {
1039 struct sockaddr_dl *sdl;
1040
1041 sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
1042 uuid_ether_del(LLADDR(sdl));
1043
1044 if (ifp->if_l2com != NULL) {
1045 KASSERT(ng_ether_detach_p != NULL,
1046 ("ng_ether_detach_p is NULL"));
1047 (*ng_ether_detach_p)(ifp);
1048 }
1049
1050 bpfdetach(ifp);
1051 if_detach(ifp);
1052 }
1053
1054 #ifdef VIMAGE
1055 void
ether_reassign(struct ifnet * ifp,struct vnet * new_vnet,char * unused __unused)1056 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
1057 {
1058
1059 if (ifp->if_l2com != NULL) {
1060 KASSERT(ng_ether_detach_p != NULL,
1061 ("ng_ether_detach_p is NULL"));
1062 (*ng_ether_detach_p)(ifp);
1063 }
1064
1065 if (ng_ether_attach_p != NULL) {
1066 CURVNET_SET_QUIET(new_vnet);
1067 (*ng_ether_attach_p)(ifp);
1068 CURVNET_RESTORE();
1069 }
1070 }
1071 #endif
1072
1073 SYSCTL_DECL(_net_link);
1074 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1075 "Ethernet");
1076
1077 #if 0
1078 /*
1079 * This is for reference. We have a table-driven version
1080 * of the little-endian crc32 generator, which is faster
1081 * than the double-loop.
1082 */
1083 uint32_t
1084 ether_crc32_le(const uint8_t *buf, size_t len)
1085 {
1086 size_t i;
1087 uint32_t crc;
1088 int bit;
1089 uint8_t data;
1090
1091 crc = 0xffffffff; /* initial value */
1092
1093 for (i = 0; i < len; i++) {
1094 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1095 carry = (crc ^ data) & 1;
1096 crc >>= 1;
1097 if (carry)
1098 crc = (crc ^ ETHER_CRC_POLY_LE);
1099 }
1100 }
1101
1102 return (crc);
1103 }
1104 #else
1105 uint32_t
ether_crc32_le(const uint8_t * buf,size_t len)1106 ether_crc32_le(const uint8_t *buf, size_t len)
1107 {
1108 static const uint32_t crctab[] = {
1109 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1110 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1111 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1112 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1113 };
1114 size_t i;
1115 uint32_t crc;
1116
1117 crc = 0xffffffff; /* initial value */
1118
1119 for (i = 0; i < len; i++) {
1120 crc ^= buf[i];
1121 crc = (crc >> 4) ^ crctab[crc & 0xf];
1122 crc = (crc >> 4) ^ crctab[crc & 0xf];
1123 }
1124
1125 return (crc);
1126 }
1127 #endif
1128
1129 uint32_t
ether_crc32_be(const uint8_t * buf,size_t len)1130 ether_crc32_be(const uint8_t *buf, size_t len)
1131 {
1132 size_t i;
1133 uint32_t crc, carry;
1134 int bit;
1135 uint8_t data;
1136
1137 crc = 0xffffffff; /* initial value */
1138
1139 for (i = 0; i < len; i++) {
1140 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1141 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1142 crc <<= 1;
1143 if (carry)
1144 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1145 }
1146 }
1147
1148 return (crc);
1149 }
1150
1151 int
ether_ioctl(struct ifnet * ifp,u_long command,caddr_t data)1152 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1153 {
1154 struct ifaddr *ifa = (struct ifaddr *) data;
1155 struct ifreq *ifr = (struct ifreq *) data;
1156 int error = 0;
1157
1158 switch (command) {
1159 case SIOCSIFADDR:
1160 ifp->if_flags |= IFF_UP;
1161
1162 switch (ifa->ifa_addr->sa_family) {
1163 #ifdef INET
1164 case AF_INET:
1165 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1166 arp_ifinit(ifp, ifa);
1167 break;
1168 #endif
1169 default:
1170 ifp->if_init(ifp->if_softc);
1171 break;
1172 }
1173 break;
1174
1175 case SIOCGIFADDR:
1176 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
1177 ETHER_ADDR_LEN);
1178 break;
1179
1180 case SIOCSIFMTU:
1181 /*
1182 * Set the interface MTU.
1183 */
1184 if (ifr->ifr_mtu > ETHERMTU) {
1185 error = EINVAL;
1186 } else {
1187 ifp->if_mtu = ifr->ifr_mtu;
1188 }
1189 break;
1190
1191 case SIOCSLANPCP:
1192 error = priv_check(curthread, PRIV_NET_SETLANPCP);
1193 if (error != 0)
1194 break;
1195 if (ifr->ifr_lan_pcp > 7 &&
1196 ifr->ifr_lan_pcp != IFNET_PCP_NONE) {
1197 error = EINVAL;
1198 } else {
1199 ifp->if_pcp = ifr->ifr_lan_pcp;
1200 /* broadcast event about PCP change */
1201 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_PCP);
1202 }
1203 break;
1204
1205 case SIOCGLANPCP:
1206 ifr->ifr_lan_pcp = ifp->if_pcp;
1207 break;
1208
1209 default:
1210 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1211 break;
1212 }
1213 return (error);
1214 }
1215
1216 static int
ether_resolvemulti(struct ifnet * ifp,struct sockaddr ** llsa,struct sockaddr * sa)1217 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1218 struct sockaddr *sa)
1219 {
1220 struct sockaddr_dl *sdl;
1221 #ifdef INET
1222 struct sockaddr_in *sin;
1223 #endif
1224 #ifdef INET6
1225 struct sockaddr_in6 *sin6;
1226 #endif
1227 u_char *e_addr;
1228
1229 switch(sa->sa_family) {
1230 case AF_LINK:
1231 /*
1232 * No mapping needed. Just check that it's a valid MC address.
1233 */
1234 sdl = (struct sockaddr_dl *)sa;
1235 e_addr = LLADDR(sdl);
1236 if (!ETHER_IS_MULTICAST(e_addr))
1237 return EADDRNOTAVAIL;
1238 *llsa = NULL;
1239 return 0;
1240
1241 #ifdef INET
1242 case AF_INET:
1243 sin = (struct sockaddr_in *)sa;
1244 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1245 return EADDRNOTAVAIL;
1246 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1247 sdl->sdl_alen = ETHER_ADDR_LEN;
1248 e_addr = LLADDR(sdl);
1249 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1250 *llsa = (struct sockaddr *)sdl;
1251 return 0;
1252 #endif
1253 #ifdef INET6
1254 case AF_INET6:
1255 sin6 = (struct sockaddr_in6 *)sa;
1256 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1257 /*
1258 * An IP6 address of 0 means listen to all
1259 * of the Ethernet multicast address used for IP6.
1260 * (This is used for multicast routers.)
1261 */
1262 ifp->if_flags |= IFF_ALLMULTI;
1263 *llsa = NULL;
1264 return 0;
1265 }
1266 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1267 return EADDRNOTAVAIL;
1268 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1269 sdl->sdl_alen = ETHER_ADDR_LEN;
1270 e_addr = LLADDR(sdl);
1271 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1272 *llsa = (struct sockaddr *)sdl;
1273 return 0;
1274 #endif
1275
1276 default:
1277 /*
1278 * Well, the text isn't quite right, but it's the name
1279 * that counts...
1280 */
1281 return EAFNOSUPPORT;
1282 }
1283 }
1284
1285 static moduledata_t ether_mod = {
1286 .name = "ether",
1287 };
1288
1289 void
ether_vlan_mtap(struct bpf_if * bp,struct mbuf * m,void * data,u_int dlen)1290 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1291 {
1292 struct ether_vlan_header vlan;
1293 struct mbuf mv, mb;
1294
1295 KASSERT((m->m_flags & M_VLANTAG) != 0,
1296 ("%s: vlan information not present", __func__));
1297 KASSERT(m->m_len >= sizeof(struct ether_header),
1298 ("%s: mbuf not large enough for header", __func__));
1299 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1300 vlan.evl_proto = vlan.evl_encap_proto;
1301 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1302 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1303 m->m_len -= sizeof(struct ether_header);
1304 m->m_data += sizeof(struct ether_header);
1305 /*
1306 * If a data link has been supplied by the caller, then we will need to
1307 * re-create a stack allocated mbuf chain with the following structure:
1308 *
1309 * (1) mbuf #1 will contain the supplied data link
1310 * (2) mbuf #2 will contain the vlan header
1311 * (3) mbuf #3 will contain the original mbuf's packet data
1312 *
1313 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1314 */
1315 if (data != NULL) {
1316 mv.m_next = m;
1317 mv.m_data = (caddr_t)&vlan;
1318 mv.m_len = sizeof(vlan);
1319 mb.m_next = &mv;
1320 mb.m_data = data;
1321 mb.m_len = dlen;
1322 bpf_mtap(bp, &mb);
1323 } else
1324 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1325 m->m_len += sizeof(struct ether_header);
1326 m->m_data -= sizeof(struct ether_header);
1327 }
1328
1329 struct mbuf *
ether_vlanencap_proto(struct mbuf * m,uint16_t tag,uint16_t proto)1330 ether_vlanencap_proto(struct mbuf *m, uint16_t tag, uint16_t proto)
1331 {
1332 struct ether_vlan_header *evl;
1333
1334 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1335 if (m == NULL)
1336 return (NULL);
1337 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1338
1339 if (m->m_len < sizeof(*evl)) {
1340 m = m_pullup(m, sizeof(*evl));
1341 if (m == NULL)
1342 return (NULL);
1343 }
1344
1345 /*
1346 * Transform the Ethernet header into an Ethernet header
1347 * with 802.1Q encapsulation.
1348 */
1349 evl = mtod(m, struct ether_vlan_header *);
1350 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1351 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1352 evl->evl_encap_proto = htons(proto);
1353 evl->evl_tag = htons(tag);
1354 return (m);
1355 }
1356
1357 void
ether_bpf_mtap_if(struct ifnet * ifp,struct mbuf * m)1358 ether_bpf_mtap_if(struct ifnet *ifp, struct mbuf *m)
1359 {
1360 if (bpf_peers_present(ifp->if_bpf)) {
1361 M_ASSERTVALID(m);
1362 if ((m->m_flags & M_VLANTAG) != 0)
1363 ether_vlan_mtap(ifp->if_bpf, m, NULL, 0);
1364 else
1365 bpf_mtap(ifp->if_bpf, m);
1366 }
1367 }
1368
1369 static SYSCTL_NODE(_net_link, IFT_L2VLAN, vlan, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1370 "IEEE 802.1Q VLAN");
1371 static SYSCTL_NODE(_net_link_vlan, PF_LINK, link,
1372 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1373 "for consistency");
1374
1375 VNET_DEFINE_STATIC(int, soft_pad);
1376 #define V_soft_pad VNET(soft_pad)
1377 SYSCTL_INT(_net_link_vlan, OID_AUTO, soft_pad, CTLFLAG_RW | CTLFLAG_VNET,
1378 &VNET_NAME(soft_pad), 0,
1379 "pad short frames before tagging");
1380
1381 /*
1382 * For now, make preserving PCP via an mbuf tag optional, as it increases
1383 * per-packet memory allocations and frees. In the future, it would be
1384 * preferable to reuse ether_vtag for this, or similar.
1385 */
1386 VNET_DEFINE(int, vlan_mtag_pcp) = 0;
1387 #define V_vlan_mtag_pcp VNET(vlan_mtag_pcp)
1388 SYSCTL_INT(_net_link_vlan, OID_AUTO, mtag_pcp, CTLFLAG_RW | CTLFLAG_VNET,
1389 &VNET_NAME(vlan_mtag_pcp), 0,
1390 "Retain VLAN PCP information as packets are passed up the stack");
1391
1392 static inline bool
ether_do_pcp(struct ifnet * ifp,struct mbuf * m)1393 ether_do_pcp(struct ifnet *ifp, struct mbuf *m)
1394 {
1395 if (ifp->if_type == IFT_L2VLAN)
1396 return (false);
1397 if (ifp->if_pcp != IFNET_PCP_NONE || (m->m_flags & M_VLANTAG) != 0)
1398 return (true);
1399 if (V_vlan_mtag_pcp &&
1400 m_tag_locate(m, MTAG_8021Q, MTAG_8021Q_PCP_OUT, NULL) != NULL)
1401 return (true);
1402 return (false);
1403 }
1404
1405 bool
ether_8021q_frame(struct mbuf ** mp,struct ifnet * ife,struct ifnet * p,const struct ether_8021q_tag * qtag)1406 ether_8021q_frame(struct mbuf **mp, struct ifnet *ife, struct ifnet *p,
1407 const struct ether_8021q_tag *qtag)
1408 {
1409 struct m_tag *mtag;
1410 int n;
1411 uint16_t tag;
1412 uint8_t pcp = qtag->pcp;
1413 static const char pad[8]; /* just zeros */
1414
1415 /*
1416 * Pad the frame to the minimum size allowed if told to.
1417 * This option is in accord with IEEE Std 802.1Q, 2003 Ed.,
1418 * paragraph C.4.4.3.b. It can help to work around buggy
1419 * bridges that violate paragraph C.4.4.3.a from the same
1420 * document, i.e., fail to pad short frames after untagging.
1421 * E.g., a tagged frame 66 bytes long (incl. FCS) is OK, but
1422 * untagging it will produce a 62-byte frame, which is a runt
1423 * and requires padding. There are VLAN-enabled network
1424 * devices that just discard such runts instead or mishandle
1425 * them somehow.
1426 */
1427 if (V_soft_pad && p->if_type == IFT_ETHER) {
1428 for (n = ETHERMIN + ETHER_HDR_LEN - (*mp)->m_pkthdr.len;
1429 n > 0; n -= sizeof(pad)) {
1430 if (!m_append(*mp, min(n, sizeof(pad)), pad))
1431 break;
1432 }
1433 if (n > 0) {
1434 m_freem(*mp);
1435 *mp = NULL;
1436 if_printf(ife, "cannot pad short frame");
1437 return (false);
1438 }
1439 }
1440
1441 /*
1442 * If PCP is set in mbuf, use it
1443 */
1444 if ((*mp)->m_flags & M_VLANTAG) {
1445 pcp = EVL_PRIOFTAG((*mp)->m_pkthdr.ether_vtag);
1446 }
1447
1448 /*
1449 * If underlying interface can do VLAN tag insertion itself,
1450 * just pass the packet along. However, we need some way to
1451 * tell the interface where the packet came from so that it
1452 * knows how to find the VLAN tag to use, so we attach a
1453 * packet tag that holds it.
1454 */
1455 if (V_vlan_mtag_pcp && (mtag = m_tag_locate(*mp, MTAG_8021Q,
1456 MTAG_8021Q_PCP_OUT, NULL)) != NULL)
1457 tag = EVL_MAKETAG(qtag->vid, *(uint8_t *)(mtag + 1), 0);
1458 else
1459 tag = EVL_MAKETAG(qtag->vid, pcp, 0);
1460 if ((p->if_capenable & IFCAP_VLAN_HWTAGGING) &&
1461 (qtag->proto == ETHERTYPE_VLAN)) {
1462 (*mp)->m_pkthdr.ether_vtag = tag;
1463 (*mp)->m_flags |= M_VLANTAG;
1464 } else {
1465 *mp = ether_vlanencap_proto(*mp, tag, qtag->proto);
1466 if (*mp == NULL) {
1467 if_printf(ife, "unable to prepend 802.1Q header");
1468 return (false);
1469 }
1470 (*mp)->m_flags &= ~M_VLANTAG;
1471 }
1472 return (true);
1473 }
1474
1475 /*
1476 * Allocate an address from the FreeBSD Foundation OUI. This uses a
1477 * cryptographic hash function on the containing jail's name, UUID and the
1478 * interface name to attempt to provide a unique but stable address.
1479 * Pseudo-interfaces which require a MAC address should use this function to
1480 * allocate non-locally-administered addresses.
1481 */
1482 void
ether_gen_addr_byname(const char * nameunit,struct ether_addr * hwaddr)1483 ether_gen_addr_byname(const char *nameunit, struct ether_addr *hwaddr)
1484 {
1485 SHA1_CTX ctx;
1486 char *buf;
1487 char uuid[HOSTUUIDLEN + 1];
1488 uint64_t addr;
1489 int i, sz;
1490 char digest[SHA1_RESULTLEN];
1491 char jailname[MAXHOSTNAMELEN];
1492
1493 getcredhostuuid(curthread->td_ucred, uuid, sizeof(uuid));
1494 if (strncmp(uuid, DEFAULT_HOSTUUID, sizeof(uuid)) == 0) {
1495 /* Fall back to a random mac address. */
1496 goto rando;
1497 }
1498
1499 /* If each (vnet) jail would also have a unique hostuuid this would not
1500 * be necessary. */
1501 getjailname(curthread->td_ucred, jailname, sizeof(jailname));
1502 sz = asprintf(&buf, M_TEMP, "%s-%s-%s", uuid, nameunit,
1503 jailname);
1504 if (sz < 0) {
1505 /* Fall back to a random mac address. */
1506 goto rando;
1507 }
1508
1509 SHA1Init(&ctx);
1510 SHA1Update(&ctx, buf, sz);
1511 SHA1Final(digest, &ctx);
1512 free(buf, M_TEMP);
1513
1514 addr = ((digest[0] << 16) | (digest[1] << 8) | digest[2]) &
1515 OUI_FREEBSD_GENERATED_MASK;
1516 addr = OUI_FREEBSD(addr);
1517 for (i = 0; i < ETHER_ADDR_LEN; ++i) {
1518 hwaddr->octet[i] = addr >> ((ETHER_ADDR_LEN - i - 1) * 8) &
1519 0xFF;
1520 }
1521
1522 return;
1523 rando:
1524 arc4rand(hwaddr, sizeof(*hwaddr), 0);
1525 /* Unicast */
1526 hwaddr->octet[0] &= 0xFE;
1527 /* Locally administered. */
1528 hwaddr->octet[0] |= 0x02;
1529 }
1530
1531 void
ether_gen_addr(struct ifnet * ifp,struct ether_addr * hwaddr)1532 ether_gen_addr(struct ifnet *ifp, struct ether_addr *hwaddr)
1533 {
1534 ether_gen_addr_byname(if_name(ifp), hwaddr);
1535 }
1536
1537 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1538 MODULE_VERSION(ether, 1);
1539