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