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