1 /*-
2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: releng/8.1/sys/net/if_ethersubr.c 205511 2010-03-23 09:58:59Z luigi $
31 */
32
33 #include "opt_atalk.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipx.h"
37 #include "opt_netgraph.h"
38 #include "opt_carp.h"
39 #include "opt_mbuf_profiling.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mbuf.h>
48 #include <sys/random.h>
49 #include <sys/rwlock.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <net/if_llc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/bpf.h>
62 #include <net/ethernet.h>
63 #include <net/if_bridgevar.h>
64 #include <net/if_vlan_var.h>
65 #include <net/if_llatbl.h>
66 #include <net/pf_mtag.h>
67 #include <net/vnet.h>
68
69 #if defined(INET) || defined(INET6)
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_fw.h>
75 #include <netinet/ipfw/ip_fw_private.h>
76 #endif
77 #ifdef INET6
78 #include <netinet6/nd6.h>
79 #endif
80
81 #if defined(INET) || defined(INET6)
82 #ifdef DEV_CARP
83 #include <netinet/ip_carp.h>
84 #endif
85 #endif
86
87 #ifdef IPX
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
90 #endif
91
92 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
93 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
94 struct sockaddr *dst, short *tp, int *hlen);
95
96 #ifdef NETATALK
97 #include <netatalk/at.h>
98 #include <netatalk/at_var.h>
99 #include <netatalk/at_extern.h>
100
101 #define llc_snap_org_code llc_un.type_snap.org_code
102 #define llc_snap_ether_type llc_un.type_snap.ether_type
103
104 extern u_char at_org_code[3];
105 extern u_char aarp_org_code[3];
106 #endif /* NETATALK */
107
108 #include <security/mac/mac_framework.h>
109
110 #ifdef CTASSERT
111 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
112 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
113 #endif
114
115 /* netgraph node hooks for ng_ether(4) */
116 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
117 void (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
118 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
119 void (*ng_ether_attach_p)(struct ifnet *ifp);
120 void (*ng_ether_detach_p)(struct ifnet *ifp);
121
122 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
123
124 /* if_bridge(4) support */
125 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
126 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
127 struct sockaddr *, struct rtentry *);
128 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
129
130 /* if_lagg(4) support */
131 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *);
132
133 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
134 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
135
136 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
137 struct sockaddr *);
138
139 /* XXX: should be in an arp support file, not here */
140 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
141
142 #define ETHER_IS_BROADCAST(addr) \
143 (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
144
145 #define senderr(e) do { error = (e); goto bad;} while (0)
146
147 #if defined(INET) || defined(INET6)
148 int
149 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared);
150 static VNET_DEFINE(int, ether_ipfw);
151 #define V_ether_ipfw VNET(ether_ipfw)
152 #endif
153
154
155 /*
156 * Ethernet output routine.
157 * Encapsulate a packet of type family for the local net.
158 * Use trailer local net encapsulation if enough data in first
159 * packet leaves a multiple of 512 bytes of data in remainder.
160 */
161 int
162 ether_output(struct ifnet *ifp, struct mbuf *m,
163 struct sockaddr *dst, struct route *ro)
164 {
165 short type;
166 int error = 0, hdrcmplt = 0;
167 u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
168 struct llentry *lle = NULL;
169 struct rtentry *rt0 = NULL;
170 struct ether_header *eh;
171 struct pf_mtag *t;
172 int loop_copy = 1;
173 int hlen; /* link layer header length */
174
175 if (ro != NULL) {
176 if (!(m->m_flags & (M_BCAST | M_MCAST)))
177 lle = ro->ro_lle;
178 rt0 = ro->ro_rt;
179 }
180 #ifdef MAC
181 error = mac_ifnet_check_transmit(ifp, m);
182 if (error)
183 senderr(error);
184 #endif
185
186 M_PROFILE(m);
187 if (ifp->if_flags & IFF_MONITOR)
188 senderr(ENETDOWN);
189 if (!((ifp->if_flags & IFF_UP) &&
190 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
191 senderr(ENETDOWN);
192
193 hlen = ETHER_HDR_LEN;
194 switch (dst->sa_family) {
195 #ifdef INET
196 case AF_INET:
197 if (lle != NULL && (lle->la_flags & LLE_VALID))
198 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
199 else
200 error = arpresolve(ifp, rt0, m, dst, edst, &lle);
201 if (error)
202 return (error == EWOULDBLOCK ? 0 : error);
203 type = htons(ETHERTYPE_IP);
204 break;
205 case AF_ARP:
206 {
207 struct arphdr *ah;
208 ah = mtod(m, struct arphdr *);
209 ah->ar_hrd = htons(ARPHRD_ETHER);
210
211 loop_copy = 0; /* if this is for us, don't do it */
212
213 switch(ntohs(ah->ar_op)) {
214 case ARPOP_REVREQUEST:
215 case ARPOP_REVREPLY:
216 type = htons(ETHERTYPE_REVARP);
217 break;
218 case ARPOP_REQUEST:
219 case ARPOP_REPLY:
220 default:
221 type = htons(ETHERTYPE_ARP);
222 break;
223 }
224
225 if (m->m_flags & M_BCAST)
226 bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
227 else
228 bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
229
230 }
231 break;
232 #endif
233 #ifdef INET6
234 case AF_INET6:
235 if (lle != NULL && (lle->la_flags & LLE_VALID))
236 memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
237 else
238 error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
239 if (error)
240 return error;
241 type = htons(ETHERTYPE_IPV6);
242 break;
243 #endif
244 #ifdef IPX
245 case AF_IPX:
246 if (ef_outputp) {
247 error = ef_outputp(ifp, &m, dst, &type, &hlen);
248 if (error)
249 goto bad;
250 } else
251 type = htons(ETHERTYPE_IPX);
252 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
253 (caddr_t)edst, sizeof (edst));
254 break;
255 #endif
256 #ifdef NETATALK
257 case AF_APPLETALK:
258 {
259 struct at_ifaddr *aa;
260
261 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
262 senderr(EHOSTUNREACH); /* XXX */
263 if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst)) {
264 ifa_free(&aa->aa_ifa);
265 return (0);
266 }
267 /*
268 * In the phase 2 case, need to prepend an mbuf for the llc header.
269 */
270 if ( aa->aa_flags & AFA_PHASE2 ) {
271 struct llc llc;
272
273 ifa_free(&aa->aa_ifa);
274 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
275 if (m == NULL)
276 senderr(ENOBUFS);
277 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
278 llc.llc_control = LLC_UI;
279 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
280 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
281 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
282 type = htons(m->m_pkthdr.len);
283 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
284 } else {
285 ifa_free(&aa->aa_ifa);
286 type = htons(ETHERTYPE_AT);
287 }
288 break;
289 }
290 #endif /* NETATALK */
291
292 case pseudo_AF_HDRCMPLT:
293 hdrcmplt = 1;
294 eh = (struct ether_header *)dst->sa_data;
295 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
296 /* FALLTHROUGH */
297
298 case AF_UNSPEC:
299 loop_copy = 0; /* if this is for us, don't do it */
300 eh = (struct ether_header *)dst->sa_data;
301 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
302 type = eh->ether_type;
303 break;
304
305 default:
306 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
307 senderr(EAFNOSUPPORT);
308 }
309
310 if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
311 int csum_flags = 0;
312 if (m->m_pkthdr.csum_flags & CSUM_IP)
313 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
314 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
315 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
316 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
317 csum_flags |= CSUM_SCTP_VALID;
318 m->m_pkthdr.csum_flags |= csum_flags;
319 m->m_pkthdr.csum_data = 0xffff;
320 return (if_simloop(ifp, m, dst->sa_family, 0));
321 }
322
323 /*
324 * Add local net header. If no space in first mbuf,
325 * allocate another.
326 */
327 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
328 if (m == NULL)
329 senderr(ENOBUFS);
330 eh = mtod(m, struct ether_header *);
331 (void)memcpy(&eh->ether_type, &type,
332 sizeof(eh->ether_type));
333 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
334 if (hdrcmplt)
335 (void)memcpy(eh->ether_shost, esrc,
336 sizeof(eh->ether_shost));
337 else
338 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
339 sizeof(eh->ether_shost));
340
341 /*
342 * If a simplex interface, and the packet is being sent to our
343 * Ethernet address or a broadcast address, loopback a copy.
344 * XXX To make a simplex device behave exactly like a duplex
345 * device, we should copy in the case of sending to our own
346 * ethernet address (thus letting the original actually appear
347 * on the wire). However, we don't do that here for security
348 * reasons and compatibility with the original behavior.
349 */
350 if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
351 ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
352 int csum_flags = 0;
353
354 if (m->m_pkthdr.csum_flags & CSUM_IP)
355 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
356 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
357 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
358 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
359 csum_flags |= CSUM_SCTP_VALID;
360
361 if (m->m_flags & M_BCAST) {
362 struct mbuf *n;
363
364 /*
365 * Because if_simloop() modifies the packet, we need a
366 * writable copy through m_dup() instead of a readonly
367 * one as m_copy[m] would give us. The alternative would
368 * be to modify if_simloop() to handle the readonly mbuf,
369 * but performancewise it is mostly equivalent (trading
370 * extra data copying vs. extra locking).
371 *
372 * XXX This is a local workaround. A number of less
373 * often used kernel parts suffer from the same bug.
374 * See PR kern/105943 for a proposed general solution.
375 */
376 if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
377 n->m_pkthdr.csum_flags |= csum_flags;
378 if (csum_flags & CSUM_DATA_VALID)
379 n->m_pkthdr.csum_data = 0xffff;
380 (void)if_simloop(ifp, n, dst->sa_family, hlen);
381 } else
382 ifp->if_iqdrops++;
383 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
384 ETHER_ADDR_LEN) == 0) {
385 m->m_pkthdr.csum_flags |= csum_flags;
386 if (csum_flags & CSUM_DATA_VALID)
387 m->m_pkthdr.csum_data = 0xffff;
388 (void) if_simloop(ifp, m, dst->sa_family, hlen);
389 return (0); /* XXX */
390 }
391 }
392
393 /*
394 * Bridges require special output handling.
395 */
396 if (ifp->if_bridge) {
397 BRIDGE_OUTPUT(ifp, m, error);
398 return (error);
399 }
400
401 #if defined(INET) || defined(INET6)
402 #ifdef DEV_CARP
403 if (ifp->if_carp &&
404 (error = carp_output(ifp, m, dst, NULL)))
405 goto bad;
406 #endif
407 #endif
408
409 /* Handle ng_ether(4) processing, if any */
410 if (IFP2AC(ifp)->ac_netgraph != NULL) {
411 KASSERT(ng_ether_output_p != NULL,
412 ("ng_ether_output_p is NULL"));
413 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
414 bad: if (m != NULL)
415 m_freem(m);
416 return (error);
417 }
418 if (m == NULL)
419 return (0);
420 }
421
422 /* Continue with link-layer output */
423 return ether_output_frame(ifp, m);
424 }
425
426 /*
427 * Ethernet link layer output routine to send a raw frame to the device.
428 *
429 * This assumes that the 14 byte Ethernet header is present and contiguous
430 * in the first mbuf (if BRIDGE'ing).
431 */
432 int
433 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
434 {
435 #if defined(INET) || defined(INET6)
436
437 if (V_ip_fw_chk_ptr && V_ether_ipfw != 0) {
438 if (ether_ipfw_chk(&m, ifp, 0) == 0) {
439 if (m) {
440 m_freem(m);
441 return EACCES; /* pkt dropped */
442 } else
443 return 0; /* consumed e.g. in a pipe */
444 }
445 }
446 #endif
447
448 /*
449 * Queue message on interface, update output statistics if
450 * successful, and start output if interface not yet active.
451 */
452 return ((ifp->if_transmit)(ifp, m));
453 }
454
455 #if defined(INET) || defined(INET6)
456 /*
457 * ipfw processing for ethernet packets (in and out).
458 * The second parameter is NULL from ether_demux, and ifp from
459 * ether_output_frame.
460 */
461 int
462 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, int shared)
463 {
464 struct ether_header *eh;
465 struct ether_header save_eh;
466 struct mbuf *m;
467 int i;
468 struct ip_fw_args args;
469 struct m_tag *mtag;
470
471 /* fetch start point from rule, if any */
472 mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
473 if (mtag == NULL) {
474 args.rule.slot = 0;
475 } else {
476 /* dummynet packet, already partially processed */
477 struct ipfw_rule_ref *r;
478
479 /* XXX can we free it after use ? */
480 mtag->m_tag_id = PACKET_TAG_NONE;
481 r = (struct ipfw_rule_ref *)(mtag + 1);
482 if (r->info & IPFW_ONEPASS)
483 return (1);
484 args.rule = *r;
485 }
486
487 /*
488 * I need some amt of data to be contiguous, and in case others need
489 * the packet (shared==1) also better be in the first mbuf.
490 */
491 m = *m0;
492 i = min( m->m_pkthdr.len, max_protohdr);
493 if ( shared || m->m_len < i) {
494 m = m_pullup(m, i);
495 if (m == NULL) {
496 *m0 = m;
497 return 0;
498 }
499 }
500 eh = mtod(m, struct ether_header *);
501 save_eh = *eh; /* save copy for restore below */
502 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
503
504 args.m = m; /* the packet we are looking at */
505 args.oif = dst; /* destination, if any */
506 args.next_hop = NULL; /* we do not support forward yet */
507 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
508 args.inp = NULL; /* used by ipfw uid/gid/jail rules */
509 i = V_ip_fw_chk_ptr(&args);
510 m = args.m;
511 if (m != NULL) {
512 /*
513 * Restore Ethernet header, as needed, in case the
514 * mbuf chain was replaced by ipfw.
515 */
516 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
517 if (m == NULL) {
518 *m0 = m;
519 return 0;
520 }
521 if (eh != mtod(m, struct ether_header *))
522 bcopy(&save_eh, mtod(m, struct ether_header *),
523 ETHER_HDR_LEN);
524 }
525 *m0 = m;
526
527 if (i == IP_FW_DENY) /* drop */
528 return 0;
529
530 KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
531
532 if (i == IP_FW_PASS) /* a PASS rule. */
533 return 1;
534
535 if (ip_dn_io_ptr && (i == IP_FW_DUMMYNET)) {
536 int dir;
537 /*
538 * Pass the pkt to dummynet, which consumes it.
539 * If shared, make a copy and keep the original.
540 */
541 if (shared) {
542 m = m_copypacket(m, M_DONTWAIT);
543 if (m == NULL)
544 return 0;
545 } else {
546 /*
547 * Pass the original to dummynet and
548 * nothing back to the caller
549 */
550 *m0 = NULL ;
551 }
552 dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
553 ip_dn_io_ptr(&m, dir, &args);
554 return 0;
555 }
556 /*
557 * XXX at some point add support for divert/forward actions.
558 * If none of the above matches, we have to drop the pkt.
559 */
560 return 0;
561 }
562 #endif
563
564 /*
565 * Process a received Ethernet packet; the packet is in the
566 * mbuf chain m with the ethernet header at the front.
567 */
568 static void
569 ether_input(struct ifnet *ifp, struct mbuf *m)
570 {
571 struct ether_header *eh;
572 u_short etype;
573
574 if ((ifp->if_flags & IFF_UP) == 0) {
575 m_freem(m);
576 return;
577 }
578 #ifdef DIAGNOSTIC
579 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
580 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
581 m_freem(m);
582 return;
583 }
584 #endif
585 /*
586 * Do consistency checks to verify assumptions
587 * made by code past this point.
588 */
589 if ((m->m_flags & M_PKTHDR) == 0) {
590 if_printf(ifp, "discard frame w/o packet header\n");
591 ifp->if_ierrors++;
592 m_freem(m);
593 return;
594 }
595 if (m->m_len < ETHER_HDR_LEN) {
596 /* XXX maybe should pullup? */
597 if_printf(ifp, "discard frame w/o leading ethernet "
598 "header (len %u pkt len %u)\n",
599 m->m_len, m->m_pkthdr.len);
600 ifp->if_ierrors++;
601 m_freem(m);
602 return;
603 }
604 eh = mtod(m, struct ether_header *);
605 etype = ntohs(eh->ether_type);
606 if (m->m_pkthdr.rcvif == NULL) {
607 if_printf(ifp, "discard frame w/o interface pointer\n");
608 ifp->if_ierrors++;
609 m_freem(m);
610 return;
611 }
612 #ifdef DIAGNOSTIC
613 if (m->m_pkthdr.rcvif != ifp) {
614 if_printf(ifp, "Warning, frame marked as received on %s\n",
615 m->m_pkthdr.rcvif->if_xname);
616 }
617 #endif
618
619 CURVNET_SET_QUIET(ifp->if_vnet);
620
621 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
622 if (ETHER_IS_BROADCAST(eh->ether_dhost))
623 m->m_flags |= M_BCAST;
624 else
625 m->m_flags |= M_MCAST;
626 ifp->if_imcasts++;
627 }
628
629 #ifdef MAC
630 /*
631 * Tag the mbuf with an appropriate MAC label before any other
632 * consumers can get to it.
633 */
634 mac_ifnet_create_mbuf(ifp, m);
635 #endif
636
637 /*
638 * Give bpf a chance at the packet.
639 */
640 ETHER_BPF_MTAP(ifp, m);
641
642 /*
643 * If the CRC is still on the packet, trim it off. We do this once
644 * and once only in case we are re-entered. Nothing else on the
645 * Ethernet receive path expects to see the FCS.
646 */
647 if (m->m_flags & M_HASFCS) {
648 m_adj(m, -ETHER_CRC_LEN);
649 m->m_flags &= ~M_HASFCS;
650 }
651
652 ifp->if_ibytes += m->m_pkthdr.len;
653
654 /* Allow monitor mode to claim this frame, after stats are updated. */
655 if (ifp->if_flags & IFF_MONITOR) {
656 m_freem(m);
657 CURVNET_RESTORE();
658 return;
659 }
660
661 /* Handle input from a lagg(4) port */
662 if (ifp->if_type == IFT_IEEE8023ADLAG) {
663 KASSERT(lagg_input_p != NULL,
664 ("%s: if_lagg not loaded!", __func__));
665 m = (*lagg_input_p)(ifp, m);
666 if (m != NULL)
667 ifp = m->m_pkthdr.rcvif;
668 else
669 return;
670 }
671
672 /*
673 * If the hardware did not process an 802.1Q tag, do this now,
674 * to allow 802.1P priority frames to be passed to the main input
675 * path correctly.
676 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
677 */
678 if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
679 struct ether_vlan_header *evl;
680
681 if (m->m_len < sizeof(*evl) &&
682 (m = m_pullup(m, sizeof(*evl))) == NULL) {
683 #ifdef DIAGNOSTIC
684 if_printf(ifp, "cannot pullup VLAN header\n");
685 #endif
686 ifp->if_ierrors++;
687 m_freem(m);
688 return;
689 }
690
691 evl = mtod(m, struct ether_vlan_header *);
692 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
693 m->m_flags |= M_VLANTAG;
694
695 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
696 ETHER_HDR_LEN - ETHER_TYPE_LEN);
697 m_adj(m, ETHER_VLAN_ENCAP_LEN);
698 }
699
700 /* Allow ng_ether(4) to claim this frame. */
701 if (IFP2AC(ifp)->ac_netgraph != NULL) {
702 KASSERT(ng_ether_input_p != NULL,
703 ("%s: ng_ether_input_p is NULL", __func__));
704 m->m_flags &= ~M_PROMISC;
705 (*ng_ether_input_p)(ifp, &m);
706 if (m == NULL) {
707 CURVNET_RESTORE();
708 return;
709 }
710 }
711
712 /*
713 * Allow if_bridge(4) to claim this frame.
714 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
715 * and the frame should be delivered locally.
716 */
717 if (ifp->if_bridge != NULL) {
718 m->m_flags &= ~M_PROMISC;
719 BRIDGE_INPUT(ifp, m);
720 if (m == NULL) {
721 CURVNET_RESTORE();
722 return;
723 }
724 }
725
726 #if defined(INET) || defined(INET6)
727 #ifdef DEV_CARP
728 /*
729 * Clear M_PROMISC on frame so that carp(4) will see it when the
730 * mbuf flows up to Layer 3.
731 * FreeBSD's implementation of carp(4) uses the inprotosw
732 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
733 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
734 * is outside the scope of the M_PROMISC test below.
735 * TODO: Maintain a hash table of ethernet addresses other than
736 * ether_dhost which may be active on this ifp.
737 */
738 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
739 m->m_flags &= ~M_PROMISC;
740 } else
741 #endif
742 #endif
743 {
744 /*
745 * If the frame received was not for our MAC address, set the
746 * M_PROMISC flag on the mbuf chain. The frame may need to
747 * be seen by the rest of the Ethernet input path in case of
748 * re-entry (e.g. bridge, vlan, netgraph) but should not be
749 * seen by upper protocol layers.
750 */
751 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
752 bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
753 m->m_flags |= M_PROMISC;
754 }
755
756 /* First chunk of an mbuf contains good entropy */
757 if (harvest.ethernet)
758 random_harvest(m, 16, 3, 0, RANDOM_NET);
759
760 ether_demux(ifp, m);
761 CURVNET_RESTORE();
762 }
763
764 /*
765 * Upper layer processing for a received Ethernet packet.
766 */
767 void
768 ether_demux(struct ifnet *ifp, struct mbuf *m)
769 {
770 struct ether_header *eh;
771 int isr;
772 u_short ether_type;
773 #if defined(NETATALK)
774 struct llc *l;
775 #endif
776
777 KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
778
779 #if defined(INET) || defined(INET6)
780 /*
781 * Allow dummynet and/or ipfw to claim the frame.
782 * Do not do this for PROMISC frames in case we are re-entered.
783 */
784 if (V_ip_fw_chk_ptr && V_ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
785 if (ether_ipfw_chk(&m, NULL, 0) == 0) {
786 if (m)
787 m_freem(m); /* dropped; free mbuf chain */
788 return; /* consumed */
789 }
790 }
791 #endif
792 eh = mtod(m, struct ether_header *);
793 ether_type = ntohs(eh->ether_type);
794
795 /*
796 * If this frame has a VLAN tag other than 0, call vlan_input()
797 * if its module is loaded. Otherwise, drop.
798 */
799 if ((m->m_flags & M_VLANTAG) &&
800 EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
801 if (ifp->if_vlantrunk == NULL) {
802 ifp->if_noproto++;
803 m_freem(m);
804 return;
805 }
806 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
807 __func__));
808 /* Clear before possibly re-entering ether_input(). */
809 m->m_flags &= ~M_PROMISC;
810 (*vlan_input_p)(ifp, m);
811 return;
812 }
813
814 /*
815 * Pass promiscuously received frames to the upper layer if the user
816 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
817 */
818 if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
819 m_freem(m);
820 return;
821 }
822
823 /*
824 * Reset layer specific mbuf flags to avoid confusing upper layers.
825 * Strip off Ethernet header.
826 */
827 m->m_flags &= ~M_VLANTAG;
828 m->m_flags &= ~(M_PROTOFLAGS);
829 m_adj(m, ETHER_HDR_LEN);
830
831 /*
832 * Dispatch frame to upper layer.
833 */
834 switch (ether_type) {
835 #ifdef INET
836 case ETHERTYPE_IP:
837 if ((m = ip_fastforward(m)) == NULL)
838 return;
839 isr = NETISR_IP;
840 break;
841
842 case ETHERTYPE_ARP:
843 if (ifp->if_flags & IFF_NOARP) {
844 /* Discard packet if ARP is disabled on interface */
845 m_freem(m);
846 return;
847 }
848 isr = NETISR_ARP;
849 break;
850 #endif
851 #ifdef IPX
852 case ETHERTYPE_IPX:
853 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
854 return;
855 isr = NETISR_IPX;
856 break;
857 #endif
858 #ifdef INET6
859 case ETHERTYPE_IPV6:
860 isr = NETISR_IPV6;
861 break;
862 #endif
863 #ifdef NETATALK
864 case ETHERTYPE_AT:
865 isr = NETISR_ATALK1;
866 break;
867 case ETHERTYPE_AARP:
868 isr = NETISR_AARP;
869 break;
870 #endif /* NETATALK */
871 default:
872 #ifdef IPX
873 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
874 return;
875 #endif /* IPX */
876 #if defined(NETATALK)
877 if (ether_type > ETHERMTU)
878 goto discard;
879 l = mtod(m, struct llc *);
880 if (l->llc_dsap == LLC_SNAP_LSAP &&
881 l->llc_ssap == LLC_SNAP_LSAP &&
882 l->llc_control == LLC_UI) {
883 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
884 sizeof(at_org_code)) == 0 &&
885 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
886 m_adj(m, LLC_SNAPFRAMELEN);
887 isr = NETISR_ATALK2;
888 break;
889 }
890 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
891 sizeof(aarp_org_code)) == 0 &&
892 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
893 m_adj(m, LLC_SNAPFRAMELEN);
894 isr = NETISR_AARP;
895 break;
896 }
897 }
898 #endif /* NETATALK */
899 goto discard;
900 }
901 netisr_dispatch(isr, m);
902 return;
903
904 discard:
905 /*
906 * Packet is to be discarded. If netgraph is present,
907 * hand the packet to it for last chance processing;
908 * otherwise dispose of it.
909 */
910 if (IFP2AC(ifp)->ac_netgraph != NULL) {
911 KASSERT(ng_ether_input_orphan_p != NULL,
912 ("ng_ether_input_orphan_p is NULL"));
913 /*
914 * Put back the ethernet header so netgraph has a
915 * consistent view of inbound packets.
916 */
917 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
918 (*ng_ether_input_orphan_p)(ifp, m);
919 return;
920 }
921 m_freem(m);
922 }
923
924 /*
925 * Convert Ethernet address to printable (loggable) representation.
926 * This routine is for compatibility; it's better to just use
927 *
928 * printf("%6D", <pointer to address>, ":");
929 *
930 * since there's no static buffer involved.
931 */
932 char *
933 ether_sprintf(const u_char *ap)
934 {
935 static char etherbuf[18];
936 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
937 return (etherbuf);
938 }
939
940 /*
941 * Perform common duties while attaching to interface list
942 */
943 void
944 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
945 {
946 int i;
947 struct ifaddr *ifa;
948 struct sockaddr_dl *sdl;
949
950 ifp->if_addrlen = ETHER_ADDR_LEN;
951 ifp->if_hdrlen = ETHER_HDR_LEN;
952 if_attach(ifp);
953 ifp->if_mtu = ETHERMTU;
954 ifp->if_output = ether_output;
955 ifp->if_input = ether_input;
956 ifp->if_resolvemulti = ether_resolvemulti;
957 if (ifp->if_baudrate == 0)
958 ifp->if_baudrate = IF_Mbps(10); /* just a default */
959 ifp->if_broadcastaddr = etherbroadcastaddr;
960
961 ifa = ifp->if_addr;
962 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
963 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
964 sdl->sdl_type = IFT_ETHER;
965 sdl->sdl_alen = ifp->if_addrlen;
966 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
967
968 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
969 if (ng_ether_attach_p != NULL)
970 (*ng_ether_attach_p)(ifp);
971
972 /* Announce Ethernet MAC address if non-zero. */
973 for (i = 0; i < ifp->if_addrlen; i++)
974 if (lla[i] != 0)
975 break;
976 if (i != ifp->if_addrlen)
977 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
978 }
979
980 /*
981 * Perform common duties while detaching an Ethernet interface
982 */
983 void
984 ether_ifdetach(struct ifnet *ifp)
985 {
986 if (IFP2AC(ifp)->ac_netgraph != NULL) {
987 KASSERT(ng_ether_detach_p != NULL,
988 ("ng_ether_detach_p is NULL"));
989 (*ng_ether_detach_p)(ifp);
990 }
991
992 bpfdetach(ifp);
993 if_detach(ifp);
994 }
995
996 SYSCTL_DECL(_net_link);
997 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
998 #if defined(INET) || defined(INET6)
999 SYSCTL_VNET_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
1000 &VNET_NAME(ether_ipfw), 0, "Pass ether pkts through firewall");
1001 #endif
1002
1003 #if 0
1004 /*
1005 * This is for reference. We have a table-driven version
1006 * of the little-endian crc32 generator, which is faster
1007 * than the double-loop.
1008 */
1009 uint32_t
1010 ether_crc32_le(const uint8_t *buf, size_t len)
1011 {
1012 size_t i;
1013 uint32_t crc;
1014 int bit;
1015 uint8_t data;
1016
1017 crc = 0xffffffff; /* initial value */
1018
1019 for (i = 0; i < len; i++) {
1020 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1021 carry = (crc ^ data) & 1;
1022 crc >>= 1;
1023 if (carry)
1024 crc = (crc ^ ETHER_CRC_POLY_LE);
1025 }
1026 }
1027
1028 return (crc);
1029 }
1030 #else
1031 uint32_t
1032 ether_crc32_le(const uint8_t *buf, size_t len)
1033 {
1034 static const uint32_t crctab[] = {
1035 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1036 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1037 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1038 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1039 };
1040 size_t i;
1041 uint32_t crc;
1042
1043 crc = 0xffffffff; /* initial value */
1044
1045 for (i = 0; i < len; i++) {
1046 crc ^= buf[i];
1047 crc = (crc >> 4) ^ crctab[crc & 0xf];
1048 crc = (crc >> 4) ^ crctab[crc & 0xf];
1049 }
1050
1051 return (crc);
1052 }
1053 #endif
1054
1055 uint32_t
1056 ether_crc32_be(const uint8_t *buf, size_t len)
1057 {
1058 size_t i;
1059 uint32_t crc, carry;
1060 int bit;
1061 uint8_t data;
1062
1063 crc = 0xffffffff; /* initial value */
1064
1065 for (i = 0; i < len; i++) {
1066 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1067 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1068 crc <<= 1;
1069 if (carry)
1070 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1071 }
1072 }
1073
1074 return (crc);
1075 }
1076
1077 int
1078 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1079 {
1080 struct ifaddr *ifa = (struct ifaddr *) data;
1081 struct ifreq *ifr = (struct ifreq *) data;
1082 int error = 0;
1083
1084 switch (command) {
1085 case SIOCSIFADDR:
1086 ifp->if_flags |= IFF_UP;
1087
1088 switch (ifa->ifa_addr->sa_family) {
1089 #ifdef INET
1090 case AF_INET:
1091 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1092 arp_ifinit(ifp, ifa);
1093 break;
1094 #endif
1095 #ifdef IPX
1096 /*
1097 * XXX - This code is probably wrong
1098 */
1099 case AF_IPX:
1100 {
1101 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1102
1103 if (ipx_nullhost(*ina))
1104 ina->x_host =
1105 *(union ipx_host *)
1106 IF_LLADDR(ifp);
1107 else {
1108 bcopy((caddr_t) ina->x_host.c_host,
1109 (caddr_t) IF_LLADDR(ifp),
1110 ETHER_ADDR_LEN);
1111 }
1112
1113 /*
1114 * Set new address
1115 */
1116 ifp->if_init(ifp->if_softc);
1117 break;
1118 }
1119 #endif
1120 default:
1121 ifp->if_init(ifp->if_softc);
1122 break;
1123 }
1124 break;
1125
1126 case SIOCGIFADDR:
1127 {
1128 struct sockaddr *sa;
1129
1130 sa = (struct sockaddr *) & ifr->ifr_data;
1131 bcopy(IF_LLADDR(ifp),
1132 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1133 }
1134 break;
1135
1136 case SIOCSIFMTU:
1137 /*
1138 * Set the interface MTU.
1139 */
1140 if (ifr->ifr_mtu > ETHERMTU) {
1141 error = EINVAL;
1142 } else {
1143 ifp->if_mtu = ifr->ifr_mtu;
1144 }
1145 break;
1146 default:
1147 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1148 break;
1149 }
1150 return (error);
1151 }
1152
1153 static int
1154 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1155 struct sockaddr *sa)
1156 {
1157 struct sockaddr_dl *sdl;
1158 #ifdef INET
1159 struct sockaddr_in *sin;
1160 #endif
1161 #ifdef INET6
1162 struct sockaddr_in6 *sin6;
1163 #endif
1164 u_char *e_addr;
1165
1166 switch(sa->sa_family) {
1167 case AF_LINK:
1168 /*
1169 * No mapping needed. Just check that it's a valid MC address.
1170 */
1171 sdl = (struct sockaddr_dl *)sa;
1172 e_addr = LLADDR(sdl);
1173 if (!ETHER_IS_MULTICAST(e_addr))
1174 return EADDRNOTAVAIL;
1175 *llsa = 0;
1176 return 0;
1177
1178 #ifdef INET
1179 case AF_INET:
1180 sin = (struct sockaddr_in *)sa;
1181 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1182 return EADDRNOTAVAIL;
1183 sdl = malloc(sizeof *sdl, M_IFMADDR,
1184 M_NOWAIT|M_ZERO);
1185 if (sdl == NULL)
1186 return ENOMEM;
1187 sdl->sdl_len = sizeof *sdl;
1188 sdl->sdl_family = AF_LINK;
1189 sdl->sdl_index = ifp->if_index;
1190 sdl->sdl_type = IFT_ETHER;
1191 sdl->sdl_alen = ETHER_ADDR_LEN;
1192 e_addr = LLADDR(sdl);
1193 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1194 *llsa = (struct sockaddr *)sdl;
1195 return 0;
1196 #endif
1197 #ifdef INET6
1198 case AF_INET6:
1199 sin6 = (struct sockaddr_in6 *)sa;
1200 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1201 /*
1202 * An IP6 address of 0 means listen to all
1203 * of the Ethernet multicast address used for IP6.
1204 * (This is used for multicast routers.)
1205 */
1206 ifp->if_flags |= IFF_ALLMULTI;
1207 *llsa = 0;
1208 return 0;
1209 }
1210 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1211 return EADDRNOTAVAIL;
1212 sdl = malloc(sizeof *sdl, M_IFMADDR,
1213 M_NOWAIT|M_ZERO);
1214 if (sdl == NULL)
1215 return (ENOMEM);
1216 sdl->sdl_len = sizeof *sdl;
1217 sdl->sdl_family = AF_LINK;
1218 sdl->sdl_index = ifp->if_index;
1219 sdl->sdl_type = IFT_ETHER;
1220 sdl->sdl_alen = ETHER_ADDR_LEN;
1221 e_addr = LLADDR(sdl);
1222 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1223 *llsa = (struct sockaddr *)sdl;
1224 return 0;
1225 #endif
1226
1227 default:
1228 /*
1229 * Well, the text isn't quite right, but it's the name
1230 * that counts...
1231 */
1232 return EAFNOSUPPORT;
1233 }
1234 }
1235
1236 static void*
1237 ether_alloc(u_char type, struct ifnet *ifp)
1238 {
1239 struct arpcom *ac;
1240
1241 ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1242 ac->ac_ifp = ifp;
1243
1244 return (ac);
1245 }
1246
1247 static void
1248 ether_free(void *com, u_char type)
1249 {
1250
1251 free(com, M_ARPCOM);
1252 }
1253
1254 static int
1255 ether_modevent(module_t mod, int type, void *data)
1256 {
1257
1258 switch (type) {
1259 case MOD_LOAD:
1260 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1261 break;
1262 case MOD_UNLOAD:
1263 if_deregister_com_alloc(IFT_ETHER);
1264 break;
1265 default:
1266 return EOPNOTSUPP;
1267 }
1268
1269 return (0);
1270 }
1271
1272 static moduledata_t ether_mod = {
1273 "ether",
1274 ether_modevent,
1275 0
1276 };
1277
1278 void
1279 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1280 {
1281 struct ether_vlan_header vlan;
1282 struct mbuf mv, mb;
1283
1284 KASSERT((m->m_flags & M_VLANTAG) != 0,
1285 ("%s: vlan information not present", __func__));
1286 KASSERT(m->m_len >= sizeof(struct ether_header),
1287 ("%s: mbuf not large enough for header", __func__));
1288 bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1289 vlan.evl_proto = vlan.evl_encap_proto;
1290 vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1291 vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1292 m->m_len -= sizeof(struct ether_header);
1293 m->m_data += sizeof(struct ether_header);
1294 /*
1295 * If a data link has been supplied by the caller, then we will need to
1296 * re-create a stack allocated mbuf chain with the following structure:
1297 *
1298 * (1) mbuf #1 will contain the supplied data link
1299 * (2) mbuf #2 will contain the vlan header
1300 * (3) mbuf #3 will contain the original mbuf's packet data
1301 *
1302 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1303 */
1304 if (data != NULL) {
1305 mv.m_next = m;
1306 mv.m_data = (caddr_t)&vlan;
1307 mv.m_len = sizeof(vlan);
1308 mb.m_next = &mv;
1309 mb.m_data = data;
1310 mb.m_len = dlen;
1311 bpf_mtap(bp, &mb);
1312 } else
1313 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1314 m->m_len += sizeof(struct ether_header);
1315 m->m_data -= sizeof(struct ether_header);
1316 }
1317
1318 struct mbuf *
1319 ether_vlanencap(struct mbuf *m, uint16_t tag)
1320 {
1321 struct ether_vlan_header *evl;
1322
1323 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
1324 if (m == NULL)
1325 return (NULL);
1326 /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1327
1328 if (m->m_len < sizeof(*evl)) {
1329 m = m_pullup(m, sizeof(*evl));
1330 if (m == NULL)
1331 return (NULL);
1332 }
1333
1334 /*
1335 * Transform the Ethernet header into an Ethernet header
1336 * with 802.1Q encapsulation.
1337 */
1338 evl = mtod(m, struct ether_vlan_header *);
1339 bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1340 (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1341 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1342 evl->evl_tag = htons(tag);
1343 return (m);
1344 }
1345
1346 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1347 MODULE_VERSION(ether, 1);
Cache object: eb0a7df79d9d79f2f9b747b4816a9e11
|