HAMMER 40B/Many: Inode/link-count sequencer cleanup pass.
[dragonfly.git] / sys / net / if_ethersubr.c
CommitLineData
984263bc
MD
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
90ca9293 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.57 2008/03/20 14:08:45 sephe Exp $
984263bc
MD
36 */
37
38#include "opt_atalk.h"
39#include "opt_inet.h"
40#include "opt_inet6.h"
41#include "opt_ipx.h"
984263bc 42#include "opt_netgraph.h"
0d16ba1d 43#include "opt_carp.h"
984263bc
MD
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.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/netisr.h>
56#include <net/route.h>
57#include <net/if_llc.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
4d723e5a 60#include <net/ifq_var.h>
984263bc
MD
61#include <net/bpf.h>
62#include <net/ethernet.h>
984263bc
MD
63
64#if defined(INET) || defined(INET6)
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#include <netinet/if_ether.h>
1f2de5d4
MD
68#include <net/ipfw/ip_fw.h>
69#include <net/dummynet/ip_dummynet.h>
984263bc
MD
70#endif
71#ifdef INET6
72#include <netinet6/nd6.h>
73#endif
74
0d16ba1d
MD
75#ifdef CARP
76#include <netinet/ip_carp.h>
77#endif
78
984263bc 79#ifdef IPX
d2438d69
MD
80#include <netproto/ipx/ipx.h>
81#include <netproto/ipx/ipx_if.h>
32211831 82int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
f23061d4
JH
83int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
84 short *tp, int *hlen);
984263bc
MD
85#endif
86
87#ifdef NS
88#include <netns/ns.h>
89#include <netns/ns_if.h>
90ushort ns_nettype;
91int ether_outputdebug = 0;
92int ether_inputdebug = 0;
93#endif
94
95#ifdef NETATALK
d2438d69
MD
96#include <netproto/atalk/at.h>
97#include <netproto/atalk/at_var.h>
98#include <netproto/atalk/at_extern.h>
984263bc 99
f23061d4
JH
100#define llc_snap_org_code llc_un.type_snap.org_code
101#define llc_snap_ether_type llc_un.type_snap.ether_type
984263bc
MD
102
103extern u_char at_org_code[3];
104extern u_char aarp_org_code[3];
105#endif /* NETATALK */
106
107/* netgraph node hooks for ng_ether(4) */
601fa0f9 108void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
984263bc 109void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
36f48e5e 110 struct mbuf *m, const struct ether_header *eh);
984263bc
MD
111int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
112void (*ng_ether_attach_p)(struct ifnet *ifp);
113void (*ng_ether_detach_p)(struct ifnet *ifp);
114
9801c957 115int (*vlan_input_p)(const struct ether_header *eh, struct mbuf *m);
3013ac0e
JS
116int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
117
5fe66e68
JH
118static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
119 struct rtentry *);
9b77ea6e
SZ
120static void ether_restore_header(struct mbuf **, const struct ether_header *,
121 const struct ether_header *);
984263bc 122
f23061d4 123/*
a8d45119 124 * if_bridge support
f23061d4 125 */
db37145f
SS
126struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
127int (*bridge_output_p)(struct ifnet *, struct mbuf *,
128 struct sockaddr *, struct rtentry *);
129void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
130
5fe66e68
JH
131static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
132 struct sockaddr *);
133
134const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
c401f0fd
JS
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
136};
137
5fe66e68 138#define gotoerr(e) do { error = (e); goto bad; } while (0)
f23061d4 139#define IFP2AC(ifp) ((struct arpcom *)(ifp))
984263bc 140
5fe66e68 141static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
7c5bb821 142 struct ip_fw **rule,
90ca9293 143 const struct ether_header *eh);
5fe66e68 144
984263bc 145static int ether_ipfw;
9b77ea6e
SZ
146static u_int ether_restore_hdr;
147static u_int ether_prepend_hdr;
148
5fe66e68
JH
149SYSCTL_DECL(_net_link);
150SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
151SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
152 &ether_ipfw, 0, "Pass ether pkts through firewall");
9b77ea6e
SZ
153SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
154 &ether_restore_hdr, 0, "# of ether header restoration");
155SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
156 &ether_prepend_hdr, 0,
157 "# of ether header restoration which prepends mbuf");
984263bc
MD
158
159/*
160 * Ethernet output routine.
161 * Encapsulate a packet of type family for the local net.
162 * Use trailer local net encapsulation if enough data in first
163 * packet leaves a multiple of 512 bytes of data in remainder.
164 * Assumes that ifp is actually pointer to arpcom structure.
165 */
3013ac0e
JS
166static int
167ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 168 struct rtentry *rt)
984263bc 169{
f23061d4
JH
170 struct ether_header *eh, *deh;
171 u_char *edst;
984263bc 172 int loop_copy = 0;
f23061d4 173 int hlen = ETHER_HDR_LEN; /* link layer header length */
984263bc 174 struct arpcom *ac = IFP2AC(ifp);
f23061d4 175 int error;
984263bc 176
78195a76
MD
177 ASSERT_SERIALIZED(ifp->if_serializer);
178
3a593c54
MD
179 if (ifp->if_flags & IFF_MONITOR)
180 gotoerr(ENETDOWN);
f23061d4
JH
181 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
182 gotoerr(ENETDOWN);
183
184 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
185 if (m == NULL)
5fe66e68 186 return (ENOBUFS);
f23061d4
JH
187 eh = mtod(m, struct ether_header *);
188 edst = eh->ether_dhost;
189
5fe66e68
JH
190 /*
191 * Fill in the destination ethernet address and frame type.
192 */
984263bc
MD
193 switch (dst->sa_family) {
194#ifdef INET
195 case AF_INET:
f23061d4 196 if (!arpresolve(ifp, rt, m, dst, edst))
984263bc 197 return (0); /* if not yet resolved */
f23061d4 198 eh->ether_type = htons(ETHERTYPE_IP);
984263bc
MD
199 break;
200#endif
201#ifdef INET6
202 case AF_INET6:
f23061d4 203 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
5fe66e68 204 return (0); /* Something bad happenned. */
f23061d4 205 eh->ether_type = htons(ETHERTYPE_IPV6);
984263bc
MD
206 break;
207#endif
208#ifdef IPX
209 case AF_IPX:
f23061d4
JH
210 if (ef_outputp != NULL) {
211 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
212 &hlen);
213 if (error)
214 goto bad;
215 } else {
216 eh->ether_type = htons(ETHERTYPE_IPX);
217 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
218 edst, ETHER_ADDR_LEN);
219 }
984263bc
MD
220 break;
221#endif
222#ifdef NETATALK
f23061d4
JH
223 case AF_APPLETALK: {
224 struct at_ifaddr *aa;
225
226 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
227 error = 0; /* XXX */
228 goto bad;
229 }
230 /*
231 * In the phase 2 case, need to prepend an mbuf for
232 * the llc header. Since we must preserve the value
233 * of m, which is passed to us by value, we m_copy()
234 * the first mbuf, and use it for our llc header.
235 */
236 if (aa->aa_flags & AFA_PHASE2) {
237 struct llc llc;
238
239 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
5fe66e68
JH
240 eh = mtod(m, struct ether_header *);
241 edst = eh->ether_dhost;
f23061d4
JH
242 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
243 llc.llc_control = LLC_UI;
244 bcopy(at_org_code, llc.llc_snap_org_code,
245 sizeof at_org_code);
246 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
5fe66e68
JH
247 bcopy(&llc,
248 mtod(m, caddr_t) + sizeof(struct ether_header),
249 sizeof(struct llc));
f23061d4
JH
250 eh->ether_type = htons(m->m_pkthdr.len);
251 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
252 } else {
253 eh->ether_type = htons(ETHERTYPE_AT);
254 }
255 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
256 return (0);
257 break;
984263bc 258 }
5fe66e68 259#endif
984263bc
MD
260#ifdef NS
261 case AF_NS:
f23061d4 262 switch(ns_nettype) {
984263bc 263 default:
f23061d4
JH
264 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
265 eh->ether_type = 0x8137;
984263bc 266 break;
f23061d4
JH
267 case 0x0: /* Novell 802.3 */
268 eh->ether_type = htons(m->m_pkthdr.len);
984263bc 269 break;
f23061d4
JH
270 case 0xe0e0: /* Novell 802.2 and Token-Ring */
271 M_PREPEND(m, 3, MB_DONTWAIT);
5fe66e68
JH
272 eh = mtod(m, struct ether_header *);
273 edst = eh->ether_dhost;
f23061d4 274 eh->ether_type = htons(m->m_pkthdr.len);
5fe66e68 275 cp = mtod(m, u_char *) + sizeof(struct ether_header);
984263bc
MD
276 *cp++ = 0xE0;
277 *cp++ = 0xE0;
278 *cp++ = 0x03;
279 break;
280 }
f23061d4
JH
281 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
282 ETHER_ADDR_LEN);
984263bc
MD
283 /*
284 * XXX if ns_thishost is the same as the node's ethernet
285 * address then just the default code will catch this anyhow.
286 * So I'm not sure if this next clause should be here at all?
287 * [JRE]
288 */
f23061d4 289 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
984263bc 290 m->m_pkthdr.rcvif = ifp;
8bde602d 291 netisr_dispatch(NETISR_NS, m);
984263bc
MD
292 return (error);
293 }
f23061d4 294 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
984263bc 295 m->m_flags |= M_BCAST;
984263bc 296 break;
5fe66e68 297#endif
984263bc 298 case pseudo_AF_HDRCMPLT:
984263bc
MD
299 case AF_UNSPEC:
300 loop_copy = -1; /* if this is for us, don't do it */
f23061d4
JH
301 deh = (struct ether_header *)dst->sa_data;
302 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
303 eh->ether_type = deh->ether_type;
984263bc
MD
304 break;
305
306 default:
8f0777ca 307 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
f23061d4 308 gotoerr(EAFNOSUPPORT);
984263bc
MD
309 }
310
f23061d4
JH
311 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
312 memcpy(eh->ether_shost,
313 ((struct ether_header *)dst->sa_data)->ether_shost,
314 ETHER_ADDR_LEN);
984263bc 315 else
f23061d4 316 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
984263bc 317
db37145f
SS
318 /*
319 * Bridges require special output handling.
320 */
321 if (ifp->if_bridge) {
8f0777ca
SZ
322 KASSERT(bridge_output_p != NULL,
323 ("%s: if_bridge not loaded!", __func__));
db37145f
SS
324 return ((*bridge_output_p)(ifp, m, NULL, NULL));
325 }
326
984263bc
MD
327 /*
328 * If a simplex interface, and the packet is being sent to our
329 * Ethernet address or a broadcast address, loopback a copy.
330 * XXX To make a simplex device behave exactly like a duplex
331 * device, we should copy in the case of sending to our own
332 * ethernet address (thus letting the original actually appear
333 * on the wire). However, we don't do that here for security
334 * reasons and compatibility with the original behavior.
335 */
336 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
337 int csum_flags = 0;
338
339 if (m->m_pkthdr.csum_flags & CSUM_IP)
f23061d4 340 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
984263bc 341 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
f23061d4 342 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
984263bc
MD
343 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
344 struct mbuf *n;
345
f23061d4 346 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
984263bc
MD
347 n->m_pkthdr.csum_flags |= csum_flags;
348 if (csum_flags & CSUM_DATA_VALID)
349 n->m_pkthdr.csum_data = 0xffff;
f23061d4 350 if_simloop(ifp, n, dst->sa_family, hlen);
984263bc
MD
351 } else
352 ifp->if_iqdrops++;
f23061d4
JH
353 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
354 ETHER_ADDR_LEN) == 0) {
984263bc
MD
355 m->m_pkthdr.csum_flags |= csum_flags;
356 if (csum_flags & CSUM_DATA_VALID)
357 m->m_pkthdr.csum_data = 0xffff;
f23061d4 358 if_simloop(ifp, m, dst->sa_family, hlen);
984263bc
MD
359 return (0); /* XXX */
360 }
361 }
362
0d16ba1d
MD
363#ifdef CARP
364 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
365 goto bad;
366#endif
367
368
984263bc
MD
369 /* Handle ng_ether(4) processing, if any */
370 if (ng_ether_output_p != NULL) {
0c3c561c
JH
371 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
372 goto bad;
984263bc
MD
373 if (m == NULL)
374 return (0);
375 }
376
377 /* Continue with link-layer output */
378 return ether_output_frame(ifp, m);
0c3c561c
JH
379
380bad:
381 m_freem(m);
382 return (error);
984263bc
MD
383}
384
385/*
386 * Ethernet link layer output routine to send a raw frame to the device.
387 *
388 * This assumes that the 14 byte Ethernet header is present and contiguous
a8d45119 389 * in the first mbuf.
984263bc
MD
390 */
391int
f23061d4 392ether_output_frame(struct ifnet *ifp, struct mbuf *m)
984263bc 393{
f23061d4 394 struct ip_fw *rule = NULL;
984263bc 395 int error = 0;
4d723e5a 396 struct altq_pktattr pktattr;
4c7020ad 397 struct m_tag *mtag;
984263bc 398
78195a76
MD
399 ASSERT_SERIALIZED(ifp->if_serializer);
400
4c7020ad
SZ
401 /* Extract info from dummynet tag */
402 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
403 if (mtag != NULL) {
84a3e25a 404 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4c7020ad
SZ
405
406 m_tag_delete(m, mtag);
407 mtag = NULL;
5fe66e68 408 }
984263bc 409
4d723e5a
JS
410 if (ifq_is_enabled(&ifp->if_snd))
411 altq_etherclassify(&ifp->if_snd, m, &pktattr);
4986965b 412 crit_enter();
984263bc
MD
413 if (IPFW_LOADED && ether_ipfw != 0) {
414 struct ether_header save_eh, *eh;
415
416 eh = mtod(m, struct ether_header *);
417 save_eh = *eh;
418 m_adj(m, ETHER_HDR_LEN);
90ca9293 419 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
4986965b 420 crit_exit();
5fe66e68 421 if (m != NULL) {
984263bc 422 m_freem(m);
f23061d4 423 return ENOBUFS; /* pkt dropped */
984263bc
MD
424 } else
425 return 0; /* consumed e.g. in a pipe */
426 }
9b77ea6e 427
984263bc 428 /* packet was ok, restore the ethernet header */
9b77ea6e
SZ
429 ether_restore_header(&m, eh, &save_eh);
430 if (m == NULL) {
431 crit_exit();
432 return ENOBUFS;
984263bc
MD
433 }
434 }
78195a76 435 crit_exit();
984263bc
MD
436
437 /*
438 * Queue message on interface, update output statistics if
439 * successful, and start output if interface not yet active.
440 */
4d723e5a 441 error = ifq_handoff(ifp, m, &pktattr);
984263bc
MD
442 return (error);
443}
444
445/*
446 * ipfw processing for ethernet packets (in and out).
5fe66e68 447 * The second parameter is NULL from ether_demux(), and ifp from
a8d45119 448 * ether_output_frame().
984263bc 449 */
5fe66e68 450static boolean_t
90ca9293
SZ
451ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
452 const struct ether_header *eh)
984263bc
MD
453{
454 struct ether_header save_eh = *eh; /* might be a ptr in m */
984263bc 455 struct ip_fw_args args;
e5ecc832 456 struct m_tag *mtag;
f23061d4 457 int i;
984263bc
MD
458
459 if (*rule != NULL && fw_one_pass)
5fe66e68 460 return TRUE; /* dummynet packet, already partially processed */
984263bc
MD
461
462 /*
90ca9293 463 * I need some amount of data to be contiguous.
984263bc 464 */
f23061d4 465 i = min((*m0)->m_pkthdr.len, max_protohdr);
90ca9293 466 if ((*m0)->m_len < i) {
984263bc
MD
467 *m0 = m_pullup(*m0, i);
468 if (*m0 == NULL)
5fe66e68 469 return FALSE;
984263bc
MD
470 }
471
472 args.m = *m0; /* the packet we are looking at */
473 args.oif = dst; /* destination, if any */
e5ecc832
JS
474 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
475 m_tag_delete(*m0, mtag);
984263bc
MD
476 args.rule = *rule; /* matching rule to restart */
477 args.next_hop = NULL; /* we do not support forward yet */
478 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
479 i = ip_fw_chk_ptr(&args);
480 *m0 = args.m;
481 *rule = args.rule;
482
5fe66e68
JH
483 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
484 return FALSE;
984263bc 485
5fe66e68
JH
486 if (i == 0) /* a PASS rule. */
487 return TRUE;
984263bc 488
84a3e25a 489 if (i & IP_FW_PORT_DYNT_FLAG) {
984263bc
MD
490 /*
491 * Pass the pkt to dummynet, which consumes it.
984263bc 492 */
90ca9293 493 struct mbuf *m;
984263bc 494
90ca9293
SZ
495 m = *m0; /* pass the original to dummynet */
496 *m0 = NULL; /* and nothing back to the caller */
9b77ea6e
SZ
497
498 ether_restore_header(&m, eh, &save_eh);
499 if (m == NULL)
500 return FALSE;
501
84a3e25a
SZ
502 ip_fw_dn_io_ptr(m, (i & 0xffff),
503 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
5fe66e68 504 return FALSE;
984263bc
MD
505 }
506 /*
507 * XXX at some point add support for divert/forward actions.
508 * If none of the above matches, we have to drop the pkt.
509 */
5fe66e68 510 return FALSE;
984263bc
MD
511}
512
3013ac0e 513/*
0b076e92
SZ
514 * Process a received Ethernet packet.
515 *
516 * The ethernet header is assumed to be in the mbuf so the caller
517 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
518 * bytes in the first mbuf.
984263bc
MD
519 *
520 * This allows us to concentrate in one place a bunch of code which
521 * is replicated in all device drivers. Also, many functions called
522 * from ether_input() try to put the eh back into the mbuf, so we
0b076e92 523 * can later propagate the 'contiguous packet' interface to them.
984263bc 524 *
0b076e92 525 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
984263bc
MD
526 * cluster, right before m_data. So be very careful when working on m,
527 * as you could destroy *eh !!
528 *
0b076e92
SZ
529 * First we perform any link layer operations, then continue to the
530 * upper layers with ether_demux().
984263bc
MD
531 */
532void
0b076e92 533ether_input(struct ifnet *ifp, struct mbuf *m)
984263bc 534{
0b076e92 535 struct ether_header *eh;
78195a76 536
0b076e92 537 ASSERT_SERIALIZED(ifp->if_serializer);
e7790e64
SZ
538 M_ASSERTPKTHDR(m);
539
540 /* Discard packet if interface is not up */
541 if (!(ifp->if_flags & IFF_UP)) {
542 m_freem(m);
543 return;
544 }
984263bc 545
db37145f
SS
546 if (m->m_len < sizeof(struct ether_header)) {
547 /* XXX error in the caller. */
548 m_freem(m);
549 return;
550 }
e7790e64
SZ
551 eh = mtod(m, struct ether_header *);
552
db37145f 553 m->m_pkthdr.rcvif = ifp;
db37145f 554
e7790e64
SZ
555 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
556 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
557 ifp->if_addrlen) == 0)
558 m->m_flags |= M_BCAST;
559 else
560 m->m_flags |= M_MCAST;
561 ifp->if_imcasts++;
562 }
563
db37145f
SS
564 BPF_MTAP(ifp, m);
565
566 ifp->if_ibytes += m->m_pkthdr.len;
8f0777ca 567
3a593c54
MD
568 if (ifp->if_flags & IFF_MONITOR) {
569 /*
570 * Interface marked for monitoring; discard packet.
571 */
572 m_freem(m);
573 return;
574 }
db37145f
SS
575
576 /*
577 * Tap the packet off here for a bridge. bridge_input()
578 * will return NULL if it has consumed the packet, otherwise
579 * it gets processed as normal. Note that bridge_input()
580 * will always return the original packet if we need to
581 * process it locally.
582 */
583 if (ifp->if_bridge) {
8f0777ca
SZ
584 KASSERT(bridge_input_p != NULL,
585 ("%s: if_bridge not loaded!", __func__));
db37145f
SS
586
587 if(m->m_flags & M_PROTO1) {
588 m->m_flags &= ~M_PROTO1;
589 } else {
590 /* clear M_PROMISC, in case the packets comes from a vlan */
591 /* m->m_flags &= ~M_PROMISC; */
592 lwkt_serialize_exit(ifp->if_serializer);
593 m = (*bridge_input_p)(ifp, m);
594 lwkt_serialize_enter(ifp->if_serializer);
595 if (m == NULL)
596 return;
597
3ce67233
SZ
598 KASSERT(ifp == m->m_pkthdr.rcvif,
599 ("bridge_input_p changed rcvif\n"));
e7790e64
SZ
600
601 /* 'm' may be changed by bridge_input_p() */
602 eh = mtod(m, struct ether_header *);
db37145f
SS
603 }
604 }
984263bc 605
984263bc
MD
606 /* Handle ng_ether(4) processing, if any */
607 if (ng_ether_input_p != NULL) {
601fa0f9 608 ng_ether_input_p(ifp, &m);
984263bc
MD
609 if (m == NULL)
610 return;
601fa0f9
SZ
611
612 /* 'm' may be changed by ng_ether_input_p() */
613 eh = mtod(m, struct ether_header *);
984263bc
MD
614 }
615
984263bc 616 /* Continue with upper layer processing */
d781b904 617 ether_demux(ifp, m);
984263bc
MD
618}
619
620/*
621 * Upper layer processing for a received Ethernet packet.
622 */
623void
d781b904 624ether_demux(struct ifnet *ifp, struct mbuf *m)
984263bc 625{
d781b904 626 struct ether_header save_eh, *eh;
8bde602d 627 int isr;
984263bc 628 u_short ether_type;
5fe66e68 629 struct ip_fw *rule = NULL;
4c7020ad 630 struct m_tag *mtag;
5fe66e68 631#ifdef NETATALK
82ed7fc2 632 struct llc *l;
984263bc 633#endif
984263bc 634
d781b904
SZ
635 M_ASSERTPKTHDR(m);
636 KASSERT(m->m_len >= ETHER_HDR_LEN,
637 ("ether header is no contiguous!\n"));
638
639 eh = mtod(m, struct ether_header *);
640 save_eh = *eh;
641
642 /* XXX old crufty stuff, needs to be removed */
643 m_adj(m, sizeof(struct ether_header));
aefa0143 644
4c7020ad
SZ
645 /* Extract info from dummynet tag */
646 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
647 if (mtag != NULL) {
84a3e25a 648 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4c7020ad
SZ
649 KKASSERT(ifp == NULL);
650 ifp = m->m_pkthdr.rcvif;
651
652 m_tag_delete(m, mtag);
653 mtag = NULL;
5fe66e68 654 }
a8d45119 655 if (rule) /* packet is passing the second time */
984263bc
MD
656 goto post_stats;
657
0d16ba1d
MD
658#ifdef CARP
659 /*
660 * XXX: Okay, we need to call carp_forus() and - if it is for
661 * us jump over code that does the normal check
662 * "ac_enaddr == ether_dhost". The check sequence is a bit
663 * different from OpenBSD, so we jump over as few code as
664 * possible, to catch _all_ sanity checks. This needs
665 * evaluation, to see if the carp ether_dhost values break any
666 * of these checks!
667 */
d781b904 668 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
e7790e64 669 goto post_stats;
0d16ba1d
MD
670#endif
671
f23061d4
JH
672 /*
673 * Discard packet if upper layers shouldn't see it because
674 * it was unicast to a different Ethernet address. If the
675 * driver is working properly, then this situation can only
676 * happen when the interface is in promiscuous mode.
677 */
a8d45119 678 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
d781b904
SZ
679 (eh->ether_dhost[0] & 1) == 0 &&
680 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
984263bc
MD
681 m_freem(m);
682 return;
683 }
0d16ba1d 684
984263bc
MD
685post_stats:
686 if (IPFW_LOADED && ether_ipfw != 0) {
90ca9293 687 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
0c3c561c 688 m_freem(m);
984263bc
MD
689 return;
690 }
691 }
d781b904 692 eh = NULL; /* catch any further usage */
984263bc 693
d781b904 694 ether_type = ntohs(save_eh.ether_type);
984263bc
MD
695
696 switch (ether_type) {
697#ifdef INET
698 case ETHERTYPE_IP:
78195a76 699 if (ipflow_fastforward(m, ifp->if_serializer))
984263bc 700 return;
8bde602d 701 isr = NETISR_IP;
984263bc
MD
702 break;
703
704 case ETHERTYPE_ARP:
705 if (ifp->if_flags & IFF_NOARP) {
706 /* Discard packet if ARP is disabled on interface */
707 m_freem(m);
708 return;
709 }
8bde602d 710 isr = NETISR_ARP;
984263bc
MD
711 break;
712#endif
5fe66e68
JH
713
714#ifdef INET6
715 case ETHERTYPE_IPV6:
716 isr = NETISR_IPV6;
717 break;
718#endif
719
984263bc
MD
720#ifdef IPX
721 case ETHERTYPE_IPX:
d781b904 722 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
984263bc 723 return;
8bde602d 724 isr = NETISR_IPX;
984263bc
MD
725 break;
726#endif
5fe66e68 727
984263bc
MD
728#ifdef NS
729 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
8bde602d 730 isr = NETISR_NS;
984263bc
MD
731 break;
732
5fe66e68
JH
733#endif
734
984263bc 735#ifdef NETATALK
f23061d4 736 case ETHERTYPE_AT:
8bde602d 737 isr = NETISR_ATALK1;
f23061d4
JH
738 break;
739 case ETHERTYPE_AARP:
8bde602d 740 isr = NETISR_AARP;
f23061d4 741 break;
5fe66e68
JH
742#endif
743
984263bc 744 case ETHERTYPE_VLAN:
d781b904
SZ
745 if (vlan_input_p != NULL) {
746 (*vlan_input_p)(&save_eh, m);
747 } else {
984263bc
MD
748 m->m_pkthdr.rcvif->if_noproto++;
749 m_freem(m);
750 }
984263bc 751 return;
5fe66e68 752
984263bc
MD
753 default:
754#ifdef IPX
d781b904 755 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
984263bc 756 return;
5fe66e68 757#endif
984263bc
MD
758#ifdef NS
759 checksum = mtod(m, ushort *);
760 /* Novell 802.3 */
761 if ((ether_type <= ETHERMTU) &&
8bde602d
JH
762 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
763 if (*checksum == 0xE0E0) {
984263bc
MD
764 m->m_pkthdr.len -= 3;
765 m->m_len -= 3;
766 m->m_data += 3;
767 }
8bde602d
JH
768 isr = NETISR_NS;
769 break;
984263bc 770 }
5fe66e68 771#endif
8bde602d 772#ifdef NETATALK
984263bc
MD
773 if (ether_type > ETHERMTU)
774 goto dropanyway;
775 l = mtod(m, struct llc *);
8bde602d
JH
776 if (l->llc_dsap == LLC_SNAP_LSAP &&
777 l->llc_ssap == LLC_SNAP_LSAP &&
778 l->llc_control == LLC_UI) {
f23061d4 779 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
0c3c561c 780 sizeof at_org_code) == 0 &&
8bde602d
JH
781 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
782 m_adj(m, sizeof(struct llc));
783 isr = NETISR_ATALK2;
784 break;
984263bc 785 }
2e9572df 786 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
0c3c561c 787 sizeof aarp_org_code) == 0 &&
8bde602d
JH
788 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
789 m_adj(m, sizeof(struct llc));
790 isr = NETISR_AARP;
791 break;
984263bc 792 }
984263bc 793 }
8bde602d 794dropanyway:
5fe66e68 795#endif
984263bc 796 if (ng_ether_input_orphan_p != NULL)
d781b904 797 (*ng_ether_input_orphan_p)(ifp, m, &save_eh);
984263bc
MD
798 else
799 m_freem(m);
800 return;
984263bc 801 }
8bde602d 802 netisr_dispatch(isr, m);
984263bc
MD
803}
804
805/*
806 * Perform common duties while attaching to interface list
807 */
c0f6c904 808
984263bc 809void
78195a76 810ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
c0f6c904 811{
78195a76
MD
812 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
813 serializer);
c0f6c904
JS
814}
815
816void
78195a76
MD
817ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
818 lwkt_serialize_t serializer)
984263bc 819{
82ed7fc2 820 struct sockaddr_dl *sdl;
984263bc
MD
821
822 ifp->if_type = IFT_ETHER;
c401f0fd 823 ifp->if_addrlen = ETHER_ADDR_LEN;
5fe66e68 824 ifp->if_hdrlen = ETHER_HDR_LEN;
78195a76 825 if_attach(ifp, serializer);
984263bc 826 ifp->if_mtu = ETHERMTU;
984263bc 827 if (ifp->if_baudrate == 0)
f23061d4 828 ifp->if_baudrate = 10000000;
5fe66e68 829 ifp->if_output = ether_output;
0b076e92 830 ifp->if_input = ether_input;
5fe66e68
JH
831 ifp->if_resolvemulti = ether_resolvemulti;
832 ifp->if_broadcastaddr = etherbroadcastaddr;
f2682cb9 833 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
834 sdl->sdl_type = IFT_ETHER;
835 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 836 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
837 /*
838 * XXX Keep the current drivers happy.
839 * XXX Remove once all drivers have been cleaned up
840 */
841 if (lla != IFP2AC(ifp)->ac_enaddr)
842 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 843 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
844 if (ng_ether_attach_p != NULL)
845 (*ng_ether_attach_p)(ifp);
267caeeb
JS
846
847 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
848}
849
850/*
851 * Perform common duties while detaching an Ethernet interface
852 */
853void
0a8b5977 854ether_ifdetach(struct ifnet *ifp)
984263bc 855{
45b8be9e 856 if_down(ifp);
45b8be9e 857
984263bc
MD
858 if (ng_ether_detach_p != NULL)
859 (*ng_ether_detach_p)(ifp);
0a8b5977 860 bpfdetach(ifp);
984263bc 861 if_detach(ifp);
984263bc
MD
862}
863
984263bc 864int
f23061d4 865ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
984263bc
MD
866{
867 struct ifaddr *ifa = (struct ifaddr *) data;
868 struct ifreq *ifr = (struct ifreq *) data;
869 int error = 0;
870
cbf2eda6
SZ
871#define IF_INIT(ifp) \
872do { \
873 if (((ifp)->if_flags & IFF_UP) == 0) { \
874 (ifp)->if_flags |= IFF_UP; \
875 (ifp)->if_init((ifp)->if_softc); \
876 } \
877} while (0)
878
78195a76
MD
879 ASSERT_SERIALIZED(ifp->if_serializer);
880
984263bc
MD
881 switch (command) {
882 case SIOCSIFADDR:
984263bc
MD
883 switch (ifa->ifa_addr->sa_family) {
884#ifdef INET
885 case AF_INET:
cbf2eda6 886 IF_INIT(ifp); /* before arpwhohas */
984263bc
MD
887 arp_ifinit(ifp, ifa);
888 break;
889#endif
890#ifdef IPX
891 /*
892 * XXX - This code is probably wrong
893 */
894 case AF_IPX:
895 {
f23061d4 896 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
984263bc
MD
897 struct arpcom *ac = IFP2AC(ifp);
898
899 if (ipx_nullhost(*ina))
f23061d4
JH
900 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
901 else
902 bcopy(ina->x_host.c_host, ac->ac_enaddr,
903 sizeof ac->ac_enaddr);
984263bc 904
cbf2eda6 905 IF_INIT(ifp); /* Set new address. */
984263bc
MD
906 break;
907 }
908#endif
909#ifdef NS
910 /*
911 * XXX - This code is probably wrong
912 */
913 case AF_NS:
914 {
82ed7fc2 915 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984263bc
MD
916 struct arpcom *ac = IFP2AC(ifp);
917
918 if (ns_nullhost(*ina))
f23061d4
JH
919 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
920 else
921 bcopy(ina->x_host.c_host, ac->ac_enaddr,
922 sizeof ac->ac_enaddr);
984263bc
MD
923
924 /*
925 * Set new address
926 */
cbf2eda6 927 IF_INIT(ifp);
984263bc
MD
928 break;
929 }
930#endif
931 default:
cbf2eda6 932 IF_INIT(ifp);
984263bc
MD
933 break;
934 }
935 break;
936
937 case SIOCGIFADDR:
f23061d4
JH
938 bcopy(IFP2AC(ifp)->ac_enaddr,
939 ((struct sockaddr *)ifr->ifr_data)->sa_data,
940 ETHER_ADDR_LEN);
984263bc
MD
941 break;
942
943 case SIOCSIFMTU:
944 /*
945 * Set the interface MTU.
946 */
947 if (ifr->ifr_mtu > ETHERMTU) {
948 error = EINVAL;
949 } else {
950 ifp->if_mtu = ifr->ifr_mtu;
951 }
952 break;
c2d9fd91
JS
953 default:
954 error = EINVAL;
955 break;
984263bc
MD
956 }
957 return (error);
cbf2eda6
SZ
958
959#undef IF_INIT
984263bc
MD
960}
961
962int
f23061d4
JH
963ether_resolvemulti(
964 struct ifnet *ifp,
965 struct sockaddr **llsa,
966 struct sockaddr *sa)
984263bc
MD
967{
968 struct sockaddr_dl *sdl;
969 struct sockaddr_in *sin;
970#ifdef INET6
971 struct sockaddr_in6 *sin6;
972#endif
973 u_char *e_addr;
974
975 switch(sa->sa_family) {
976 case AF_LINK:
977 /*
978 * No mapping needed. Just check that it's a valid MC address.
979 */
980 sdl = (struct sockaddr_dl *)sa;
981 e_addr = LLADDR(sdl);
982 if ((e_addr[0] & 1) != 1)
983 return EADDRNOTAVAIL;
984 *llsa = 0;
985 return 0;
986
987#ifdef INET
988 case AF_INET:
989 sin = (struct sockaddr_in *)sa;
990 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
991 return EADDRNOTAVAIL;
992 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 993 M_WAITOK | M_ZERO);
984263bc
MD
994 sdl->sdl_len = sizeof *sdl;
995 sdl->sdl_family = AF_LINK;
996 sdl->sdl_index = ifp->if_index;
997 sdl->sdl_type = IFT_ETHER;
998 sdl->sdl_alen = ETHER_ADDR_LEN;
999 e_addr = LLADDR(sdl);
1000 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1001 *llsa = (struct sockaddr *)sdl;
1002 return 0;
1003#endif
1004#ifdef INET6
1005 case AF_INET6:
1006 sin6 = (struct sockaddr_in6 *)sa;
1007 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1008 /*
1009 * An IP6 address of 0 means listen to all
1010 * of the Ethernet multicast address used for IP6.
1011 * (This is used for multicast routers.)
1012 */
1013 ifp->if_flags |= IFF_ALLMULTI;
1014 *llsa = 0;
1015 return 0;
1016 }
1017 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1018 return EADDRNOTAVAIL;
1019 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 1020 M_WAITOK | M_ZERO);
984263bc
MD
1021 sdl->sdl_len = sizeof *sdl;
1022 sdl->sdl_family = AF_LINK;
1023 sdl->sdl_index = ifp->if_index;
1024 sdl->sdl_type = IFT_ETHER;
1025 sdl->sdl_alen = ETHER_ADDR_LEN;
1026 e_addr = LLADDR(sdl);
1027 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1028 *llsa = (struct sockaddr *)sdl;
1029 return 0;
1030#endif
1031
1032 default:
1033 /*
1034 * Well, the text isn't quite right, but it's the name
1035 * that counts...
1036 */
1037 return EAFNOSUPPORT;
1038 }
1039}
d6018c31
JS
1040
1041#if 0
1042/*
1043 * This is for reference. We have a table-driven version
1044 * of the little-endian crc32 generator, which is faster
1045 * than the double-loop.
1046 */
1047uint32_t
1048ether_crc32_le(const uint8_t *buf, size_t len)
1049{
1050 uint32_t c, crc, carry;
1051 size_t i, j;
1052
1053 crc = 0xffffffffU; /* initial value */
1054
1055 for (i = 0; i < len; i++) {
1056 c = buf[i];
1057 for (j = 0; j < 8; j++) {
1058 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1059 crc >>= 1;
1060 c >>= 1;
1061 if (carry)
1062 crc = (crc ^ ETHER_CRC_POLY_LE);
1063 }
1064 }
1065
f23061d4 1066 return (crc);
d6018c31
JS
1067}
1068#else
1069uint32_t
1070ether_crc32_le(const uint8_t *buf, size_t len)
1071{
1072 static const uint32_t crctab[] = {
1073 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1074 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1075 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1076 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1077 };
1078 uint32_t crc;
1079 size_t i;
1080
1081 crc = 0xffffffffU; /* initial value */
1082
1083 for (i = 0; i < len; i++) {
1084 crc ^= buf[i];
1085 crc = (crc >> 4) ^ crctab[crc & 0xf];
1086 crc = (crc >> 4) ^ crctab[crc & 0xf];
1087 }
1088
f23061d4 1089 return (crc);
d6018c31
JS
1090}
1091#endif
1092
1093uint32_t
1094ether_crc32_be(const uint8_t *buf, size_t len)
1095{
1096 uint32_t c, crc, carry;
1097 size_t i, j;
1098
1099 crc = 0xffffffffU; /* initial value */
1100
1101 for (i = 0; i < len; i++) {
1102 c = buf[i];
1103 for (j = 0; j < 8; j++) {
1104 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1105 crc <<= 1;
1106 c >>= 1;
1107 if (carry)
1108 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1109 }
1110 }
1111
f23061d4 1112 return (crc);
d6018c31 1113}
4d723e5a 1114
4d723e5a
JS
1115/*
1116 * find the size of ethernet header, and call classifier
1117 */
1118void
1119altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1120 struct altq_pktattr *pktattr)
1121{
1122 struct ether_header *eh;
1123 uint16_t ether_type;
1124 int hlen, af, hdrsize;
1125 caddr_t hdr;
1126
1127 hlen = sizeof(struct ether_header);
1128 eh = mtod(m, struct ether_header *);
1129
1130 ether_type = ntohs(eh->ether_type);
1131 if (ether_type < ETHERMTU) {
1132 /* ick! LLC/SNAP */
1133 struct llc *llc = (struct llc *)(eh + 1);
1134 hlen += 8;
1135
1136 if (m->m_len < hlen ||
1137 llc->llc_dsap != LLC_SNAP_LSAP ||
1138 llc->llc_ssap != LLC_SNAP_LSAP ||
1139 llc->llc_control != LLC_UI)
1140 goto bad; /* not snap! */
1141
1142 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1143 }
1144
1145 if (ether_type == ETHERTYPE_IP) {
1146 af = AF_INET;
1147 hdrsize = 20; /* sizeof(struct ip) */
1148#ifdef INET6
1149 } else if (ether_type == ETHERTYPE_IPV6) {
1150 af = AF_INET6;
1151 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1152#endif
1153 } else
1154 goto bad;
1155
1156 while (m->m_len <= hlen) {
1157 hlen -= m->m_len;
1158 m = m->m_next;
1159 }
1160 hdr = m->m_data + hlen;
1161 if (m->m_len < hlen + hdrsize) {
1162 /*
1163 * ip header is not in a single mbuf. this should not
1164 * happen in the current code.
1165 * (todo: use m_pulldown in the future)
1166 */
1167 goto bad;
1168 }
1169 m->m_data += hlen;
1170 m->m_len -= hlen;
1171 ifq_classify(ifq, m, af, pktattr);
1172 m->m_data -= hlen;
1173 m->m_len += hlen;
1174
1175 return;
1176
1177bad:
1178 pktattr->pattr_class = NULL;
1179 pktattr->pattr_hdr = NULL;
1180 pktattr->pattr_af = AF_UNSPEC;
1181}
9b77ea6e
SZ
1182
1183static void
1184ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1185 const struct ether_header *save_eh)
1186{
1187 struct mbuf *m = *m0;
1188
1189 ether_restore_hdr++;
1190
1191 /*
1192 * Prepend the header, optimize for the common case of
1193 * eh pointing into the mbuf.
1194 */
1195 if ((const void *)(eh + 1) == (void *)m->m_data) {
1196 m->m_data -= ETHER_HDR_LEN;
1197 m->m_len += ETHER_HDR_LEN;
1198 m->m_pkthdr.len += ETHER_HDR_LEN;
1199 } else {
1200 ether_prepend_hdr++;
1201
1202 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1203 if (m != NULL) {
3d26a382 1204 bcopy(save_eh, mtod(m, struct ether_header *),
9b77ea6e
SZ
1205 ETHER_HDR_LEN);
1206 }
1207 }
1208 *m0 = m;
1209}