Modify ktr(4) to be typesafe
[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 $
35 */
36
984263bc
MD
37#include "opt_inet.h"
38#include "opt_inet6.h"
39#include "opt_ipx.h"
9b42cabe 40#include "opt_mpls.h"
984263bc 41#include "opt_netgraph.h"
0d16ba1d 42#include "opt_carp.h"
7c7f9646 43#include "opt_rss.h"
984263bc
MD
44
45#include <sys/param.h>
46#include <sys/systm.h>
68b67450 47#include <sys/globaldata.h>
984263bc 48#include <sys/kernel.h>
f3e0b5f0 49#include <sys/ktr.h>
8b3db995 50#include <sys/lock.h>
984263bc
MD
51#include <sys/malloc.h>
52#include <sys/mbuf.h>
68b67450 53#include <sys/msgport.h>
984263bc
MD
54#include <sys/socket.h>
55#include <sys/sockio.h>
56#include <sys/sysctl.h>
68b67450 57#include <sys/thread.h>
684a93c4 58
68b67450 59#include <sys/thread2.h>
684a93c4 60#include <sys/mplock2.h>
984263bc
MD
61
62#include <net/if.h>
63#include <net/netisr.h>
64#include <net/route.h>
65#include <net/if_llc.h>
66#include <net/if_dl.h>
67#include <net/if_types.h>
4d723e5a 68#include <net/ifq_var.h>
984263bc
MD
69#include <net/bpf.h>
70#include <net/ethernet.h>
e6b5847c 71#include <net/vlan/if_vlan_ether.h>
29bc1092 72#include <net/netmsg2.h>
984263bc
MD
73
74#if defined(INET) || defined(INET6)
75#include <netinet/in.h>
8697599b 76#include <netinet/ip_var.h>
984263bc 77#include <netinet/if_ether.h>
4639df5f 78#include <netinet/ip_flow.h>
1f2de5d4
MD
79#include <net/ipfw/ip_fw.h>
80#include <net/dummynet/ip_dummynet.h>
984263bc
MD
81#endif
82#ifdef INET6
83#include <netinet6/nd6.h>
84#endif
85
0d16ba1d
MD
86#ifdef CARP
87#include <netinet/ip_carp.h>
88#endif
89
984263bc 90#ifdef IPX
d2438d69
MD
91#include <netproto/ipx/ipx.h>
92#include <netproto/ipx/ipx_if.h>
32211831 93int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
f23061d4
JH
94int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
95 short *tp, int *hlen);
984263bc
MD
96#endif
97
9b42cabe
NA
98#ifdef MPLS
99#include <netproto/mpls/mpls.h>
100#endif
101
984263bc 102/* netgraph node hooks for ng_ether(4) */
601fa0f9 103void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
984263bc 104void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
36f48e5e 105 struct mbuf *m, const struct ether_header *eh);
984263bc
MD
106int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
107void (*ng_ether_attach_p)(struct ifnet *ifp);
108void (*ng_ether_detach_p)(struct ifnet *ifp);
109
50098e2e 110void (*vlan_input_p)(struct mbuf *);
3013ac0e 111
5fe66e68
JH
112static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
113 struct rtentry *);
9b77ea6e
SZ
114static void ether_restore_header(struct mbuf **, const struct ether_header *,
115 const struct ether_header *);
0e805566 116static int ether_characterize(struct mbuf **);
984263bc 117
f23061d4 118/*
a8d45119 119 * if_bridge support
f23061d4 120 */
db37145f 121struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
eb366364 122int (*bridge_output_p)(struct ifnet *, struct mbuf *);
db37145f 123void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
70d9a675 124struct ifnet *(*bridge_interface_p)(void *if_bridge);
db37145f 125
5fe66e68
JH
126static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
127 struct sockaddr *);
128
129const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
c401f0fd
JS
130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
131};
132
5fe66e68 133#define gotoerr(e) do { error = (e); goto bad; } while (0)
f23061d4 134#define IFP2AC(ifp) ((struct arpcom *)(ifp))
984263bc 135
5fe66e68 136static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
7c5bb821 137 struct ip_fw **rule,
90ca9293 138 const struct ether_header *eh);
5fe66e68 139
984263bc 140static int ether_ipfw;
83c08199
SZ
141static u_long ether_restore_hdr;
142static u_long ether_prepend_hdr;
ebe4c2ae 143static u_long ether_input_wronghash;
70d9a675 144static int ether_debug;
9b77ea6e 145
7c7f9646 146#ifdef RSS_DEBUG
83c08199
SZ
147static u_long ether_pktinfo_try;
148static u_long ether_pktinfo_hit;
149static u_long ether_rss_nopi;
150static u_long ether_rss_nohash;
ebe4c2ae 151static u_long ether_input_requeue;
7c7f9646
SZ
152#endif
153
5fe66e68
JH
154SYSCTL_DECL(_net_link);
155SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
70d9a675 156SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
83c08199 157 &ether_debug, 0, "Ether debug");
5fe66e68 158SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
83c08199
SZ
159 &ether_ipfw, 0, "Pass ether pkts through firewall");
160SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
161 &ether_restore_hdr, 0, "# of ether header restoration");
162SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
163 &ether_prepend_hdr, 0,
164 "# of ether header restoration which prepends mbuf");
ebe4c2ae
SZ
165SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
166 &ether_input_wronghash, 0, "# of input packets with wrong hash");
7c7f9646 167#ifdef RSS_DEBUG
83c08199
SZ
168SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
169 &ether_rss_nopi, 0, "# of packets do not have pktinfo");
170SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
171 &ether_rss_nohash, 0, "# of packets do not have hash");
172SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
173 &ether_pktinfo_try, 0,
174 "# of tries to find packets' msgport using pktinfo");
175SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
176 &ether_pktinfo_hit, 0,
177 "# of packets whose msgport are found using pktinfo");
ebe4c2ae
SZ
178SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
179 &ether_input_requeue, 0, "# of input packets gets requeued");
7c7f9646 180#endif
984263bc 181
f3e0b5f0 182#define ETHER_KTR_STR "ifp=%p"
5bf48697 183#define ETHER_KTR_ARGS struct ifnet *ifp
f3e0b5f0
SZ
184#ifndef KTR_ETHERNET
185#define KTR_ETHERNET KTR_ALL
186#endif
187KTR_INFO_MASTER(ether);
5bf48697
AE
188KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
189KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
190KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
191KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
f3e0b5f0
SZ
192#define logether(name, arg) KTR_LOG(ether_ ## name, arg)
193
984263bc
MD
194/*
195 * Ethernet output routine.
196 * Encapsulate a packet of type family for the local net.
197 * Use trailer local net encapsulation if enough data in first
198 * packet leaves a multiple of 512 bytes of data in remainder.
199 * Assumes that ifp is actually pointer to arpcom structure.
200 */
3013ac0e
JS
201static int
202ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 203 struct rtentry *rt)
984263bc 204{
f23061d4
JH
205 struct ether_header *eh, *deh;
206 u_char *edst;
984263bc 207 int loop_copy = 0;
f23061d4 208 int hlen = ETHER_HDR_LEN; /* link layer header length */
984263bc 209 struct arpcom *ac = IFP2AC(ifp);
f23061d4 210 int error;
984263bc 211
2c9effcf 212 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
57dff79c 213
3a593c54
MD
214 if (ifp->if_flags & IFF_MONITOR)
215 gotoerr(ENETDOWN);
f23061d4
JH
216 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
217 gotoerr(ENETDOWN);
218
219 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
220 if (m == NULL)
5fe66e68 221 return (ENOBUFS);
f23061d4
JH
222 eh = mtod(m, struct ether_header *);
223 edst = eh->ether_dhost;
224
5fe66e68
JH
225 /*
226 * Fill in the destination ethernet address and frame type.
227 */
984263bc
MD
228 switch (dst->sa_family) {
229#ifdef INET
230 case AF_INET:
f23061d4 231 if (!arpresolve(ifp, rt, m, dst, edst))
984263bc 232 return (0); /* if not yet resolved */
cb8d752c
NA
233#ifdef MPLS
234 if (m->m_flags & M_MPLSLABELED)
235 eh->ether_type = htons(ETHERTYPE_MPLS);
236 else
237#endif
238 eh->ether_type = htons(ETHERTYPE_IP);
984263bc
MD
239 break;
240#endif
241#ifdef INET6
242 case AF_INET6:
f23061d4 243 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
5fe66e68 244 return (0); /* Something bad happenned. */
f23061d4 245 eh->ether_type = htons(ETHERTYPE_IPV6);
984263bc
MD
246 break;
247#endif
248#ifdef IPX
249 case AF_IPX:
f23061d4 250 if (ef_outputp != NULL) {
ff54734e
SZ
251 /*
252 * Hold BGL and recheck ef_outputp
253 */
254 get_mplock();
255 if (ef_outputp != NULL) {
256 error = ef_outputp(ifp, &m, dst,
257 &eh->ether_type, &hlen);
258 rel_mplock();
259 if (error)
260 goto bad;
261 else
262 break;
263 }
264 rel_mplock();
f23061d4 265 }
ff54734e
SZ
266 eh->ether_type = htons(ETHERTYPE_IPX);
267 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
268 edst, ETHER_ADDR_LEN);
984263bc
MD
269 break;
270#endif
984263bc 271 case pseudo_AF_HDRCMPLT:
984263bc
MD
272 case AF_UNSPEC:
273 loop_copy = -1; /* if this is for us, don't do it */
f23061d4
JH
274 deh = (struct ether_header *)dst->sa_data;
275 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
276 eh->ether_type = deh->ether_type;
984263bc
MD
277 break;
278
279 default:
8f0777ca 280 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
f23061d4 281 gotoerr(EAFNOSUPPORT);
984263bc
MD
282 }
283
f23061d4
JH
284 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
285 memcpy(eh->ether_shost,
286 ((struct ether_header *)dst->sa_data)->ether_shost,
287 ETHER_ADDR_LEN);
984263bc 288 else
f23061d4 289 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
984263bc
MD
290
291 /*
db37145f
SS
292 * Bridges require special output handling.
293 */
294 if (ifp->if_bridge) {
8f0777ca
SZ
295 KASSERT(bridge_output_p != NULL,
296 ("%s: if_bridge not loaded!", __func__));
ad8c8b44 297 return bridge_output_p(ifp, m);
db37145f
SS
298 }
299
300 /*
984263bc
MD
301 * If a simplex interface, and the packet is being sent to our
302 * Ethernet address or a broadcast address, loopback a copy.
303 * XXX To make a simplex device behave exactly like a duplex
304 * device, we should copy in the case of sending to our own
305 * ethernet address (thus letting the original actually appear
306 * on the wire). However, we don't do that here for security
307 * reasons and compatibility with the original behavior.
308 */
309 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
310 int csum_flags = 0;
311
312 if (m->m_pkthdr.csum_flags & CSUM_IP)
f23061d4 313 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
984263bc 314 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
f23061d4 315 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
984263bc
MD
316 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
317 struct mbuf *n;
318
f23061d4 319 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
984263bc
MD
320 n->m_pkthdr.csum_flags |= csum_flags;
321 if (csum_flags & CSUM_DATA_VALID)
322 n->m_pkthdr.csum_data = 0xffff;
f23061d4 323 if_simloop(ifp, n, dst->sa_family, hlen);
984263bc
MD
324 } else
325 ifp->if_iqdrops++;
f23061d4
JH
326 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
327 ETHER_ADDR_LEN) == 0) {
984263bc
MD
328 m->m_pkthdr.csum_flags |= csum_flags;
329 if (csum_flags & CSUM_DATA_VALID)
330 m->m_pkthdr.csum_data = 0xffff;
f23061d4 331 if_simloop(ifp, m, dst->sa_family, hlen);
984263bc
MD
332 return (0); /* XXX */
333 }
334 }
335
0d16ba1d 336#ifdef CARP
ff54734e
SZ
337 if (ifp->if_carp) {
338 /*
339 * Hold BGL and recheck ifp->if_carp
340 */
341 get_mplock();
342 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
343 rel_mplock();
344 goto bad;
345 }
346 rel_mplock();
347 }
0d16ba1d
MD
348#endif
349
350
984263bc
MD
351 /* Handle ng_ether(4) processing, if any */
352 if (ng_ether_output_p != NULL) {
ff54734e
SZ
353 /*
354 * Hold BGL and recheck ng_ether_output_p
355 */
356 get_mplock();
357 if (ng_ether_output_p != NULL) {
358 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
359 rel_mplock();
360 goto bad;
361 }
362 if (m == NULL) {
363 rel_mplock();
364 return (0);
365 }
366 }
367 rel_mplock();
984263bc
MD
368 }
369
370 /* Continue with link-layer output */
371 return ether_output_frame(ifp, m);
0c3c561c
JH
372
373bad:
374 m_freem(m);
375 return (error);
984263bc
MD
376}
377
378/*
70d9a675
MD
379 * Returns the bridge interface an ifp is associated
380 * with.
381 *
382 * Only call if ifp->if_bridge != NULL.
383 */
384struct ifnet *
385ether_bridge_interface(struct ifnet *ifp)
386{
387 if (bridge_interface_p)
388 return(bridge_interface_p(ifp->if_bridge));
389 return (ifp);
390}
391
392/*
984263bc
MD
393 * Ethernet link layer output routine to send a raw frame to the device.
394 *
395 * This assumes that the 14 byte Ethernet header is present and contiguous
a8d45119 396 * in the first mbuf.
984263bc
MD
397 */
398int
f23061d4 399ether_output_frame(struct ifnet *ifp, struct mbuf *m)
984263bc 400{
f23061d4 401 struct ip_fw *rule = NULL;
984263bc 402 int error = 0;
4d723e5a 403 struct altq_pktattr pktattr;
984263bc 404
2c9effcf 405 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
57dff79c 406
eb241549
SZ
407 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
408 struct m_tag *mtag;
409
410 /* Extract info from dummynet tag */
411 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
412 KKASSERT(mtag != NULL);
84a3e25a 413 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
eb241549 414 KKASSERT(rule != NULL);
4c7020ad
SZ
415
416 m_tag_delete(m, mtag);
eb241549 417 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
5fe66e68 418 }
984263bc 419
4d723e5a
JS
420 if (ifq_is_enabled(&ifp->if_snd))
421 altq_etherclassify(&ifp->if_snd, m, &pktattr);
4986965b 422 crit_enter();
984263bc
MD
423 if (IPFW_LOADED && ether_ipfw != 0) {
424 struct ether_header save_eh, *eh;
425
426 eh = mtod(m, struct ether_header *);
427 save_eh = *eh;
428 m_adj(m, ETHER_HDR_LEN);
90ca9293 429 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
4986965b 430 crit_exit();
5fe66e68 431 if (m != NULL) {
984263bc 432 m_freem(m);
f23061d4 433 return ENOBUFS; /* pkt dropped */
984263bc
MD
434 } else
435 return 0; /* consumed e.g. in a pipe */
436 }
9b77ea6e 437
984263bc 438 /* packet was ok, restore the ethernet header */
9b77ea6e
SZ
439 ether_restore_header(&m, eh, &save_eh);
440 if (m == NULL) {
441 crit_exit();
442 return ENOBUFS;
984263bc
MD
443 }
444 }
78195a76 445 crit_exit();
984263bc
MD
446
447 /*
448 * Queue message on interface, update output statistics if
449 * successful, and start output if interface not yet active.
450 */
9db4b353 451 error = ifq_dispatch(ifp, m, &pktattr);
984263bc
MD
452 return (error);
453}
454
455/*
456 * ipfw processing for ethernet packets (in and out).
5fe66e68 457 * The second parameter is NULL from ether_demux(), and ifp from
a8d45119 458 * ether_output_frame().
984263bc 459 */
5fe66e68 460static boolean_t
90ca9293
SZ
461ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
462 const struct ether_header *eh)
984263bc 463{
29b27cb7 464 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
984263bc 465 struct ip_fw_args args;
e5ecc832 466 struct m_tag *mtag;
29b27cb7 467 struct mbuf *m;
f23061d4 468 int i;
984263bc
MD
469
470 if (*rule != NULL && fw_one_pass)
5fe66e68 471 return TRUE; /* dummynet packet, already partially processed */
984263bc
MD
472
473 /*
90ca9293 474 * I need some amount of data to be contiguous.
984263bc 475 */
f23061d4 476 i = min((*m0)->m_pkthdr.len, max_protohdr);
90ca9293 477 if ((*m0)->m_len < i) {
984263bc
MD
478 *m0 = m_pullup(*m0, i);
479 if (*m0 == NULL)
5fe66e68 480 return FALSE;
984263bc
MD
481 }
482
5de23090
SZ
483 /*
484 * Clean up tags
485 */
e5ecc832
JS
486 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
487 m_tag_delete(*m0, mtag);
5de23090
SZ
488 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
489 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
490 KKASSERT(mtag != NULL);
491 m_tag_delete(*m0, mtag);
492 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
493 }
494
495 args.m = *m0; /* the packet we are looking at */
496 args.oif = dst; /* destination, if any */
984263bc 497 args.rule = *rule; /* matching rule to restart */
984263bc
MD
498 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
499 i = ip_fw_chk_ptr(&args);
500 *m0 = args.m;
501 *rule = args.rule;
502
29b27cb7 503 if (*m0 == NULL)
5fe66e68 504 return FALSE;
984263bc 505
29b27cb7
SZ
506 switch (i) {
507 case IP_FW_PASS:
5fe66e68 508 return TRUE;
984263bc 509
29b27cb7
SZ
510 case IP_FW_DIVERT:
511 case IP_FW_TEE:
512 case IP_FW_DENY:
984263bc 513 /*
29b27cb7
SZ
514 * XXX at some point add support for divert/forward actions.
515 * If none of the above matches, we have to drop the pkt.
984263bc 516 */
29b27cb7 517 return FALSE;
984263bc 518
29b27cb7
SZ
519 case IP_FW_DUMMYNET:
520 /*
521 * Pass the pkt to dummynet, which consumes it.
522 */
90ca9293
SZ
523 m = *m0; /* pass the original to dummynet */
524 *m0 = NULL; /* and nothing back to the caller */
9b77ea6e
SZ
525
526 ether_restore_header(&m, eh, &save_eh);
527 if (m == NULL)
528 return FALSE;
529
29b27cb7
SZ
530 ip_fw_dn_io_ptr(m, args.cookie,
531 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
e4d4f9c3 532 ip_dn_queue(m);
5fe66e68 533 return FALSE;
29b27cb7
SZ
534
535 default:
536 panic("unknown ipfw return value: %d\n", i);
984263bc 537 }
984263bc
MD
538}
539
4853cd0f 540static void
68b67450
SZ
541ether_input(struct ifnet *ifp, struct mbuf *m)
542{
2eb0d069 543 ether_input_chain(ifp, m, NULL, NULL);
984263bc
MD
544}
545
546/*
547 * Perform common duties while attaching to interface list
548 */
549void
78195a76 550ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
984263bc 551{
78195a76
MD
552 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
553 serializer);
c0f6c904
JS
554}
555
556void
78195a76
MD
557ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
558 lwkt_serialize_t serializer)
c0f6c904 559{
82ed7fc2 560 struct sockaddr_dl *sdl;
984263bc
MD
561
562 ifp->if_type = IFT_ETHER;
c401f0fd 563 ifp->if_addrlen = ETHER_ADDR_LEN;
5fe66e68 564 ifp->if_hdrlen = ETHER_HDR_LEN;
78195a76 565 if_attach(ifp, serializer);
984263bc 566 ifp->if_mtu = ETHERMTU;
984263bc 567 if (ifp->if_baudrate == 0)
f23061d4 568 ifp->if_baudrate = 10000000;
5fe66e68 569 ifp->if_output = ether_output;
0b076e92 570 ifp->if_input = ether_input;
5fe66e68
JH
571 ifp->if_resolvemulti = ether_resolvemulti;
572 ifp->if_broadcastaddr = etherbroadcastaddr;
f2682cb9 573 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
574 sdl->sdl_type = IFT_ETHER;
575 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 576 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
577 /*
578 * XXX Keep the current drivers happy.
579 * XXX Remove once all drivers have been cleaned up
580 */
581 if (lla != IFP2AC(ifp)->ac_enaddr)
582 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 583 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
584 if (ng_ether_attach_p != NULL)
585 (*ng_ether_attach_p)(ifp);
267caeeb
JS
586
587 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
588}
589
590/*
591 * Perform common duties while detaching an Ethernet interface
592 */
593void
0a8b5977 594ether_ifdetach(struct ifnet *ifp)
984263bc 595{
45b8be9e 596 if_down(ifp);
45b8be9e 597
984263bc
MD
598 if (ng_ether_detach_p != NULL)
599 (*ng_ether_detach_p)(ifp);
0a8b5977 600 bpfdetach(ifp);
984263bc 601 if_detach(ifp);
984263bc
MD
602}
603
984263bc 604int
fecfec53 605ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
984263bc
MD
606{
607 struct ifaddr *ifa = (struct ifaddr *) data;
608 struct ifreq *ifr = (struct ifreq *) data;
609 int error = 0;
610
cbf2eda6
SZ
611#define IF_INIT(ifp) \
612do { \
613 if (((ifp)->if_flags & IFF_UP) == 0) { \
614 (ifp)->if_flags |= IFF_UP; \
615 (ifp)->if_init((ifp)->if_softc); \
616 } \
617} while (0)
618
2c9effcf 619 ASSERT_IFNET_SERIALIZED_ALL(ifp);
78195a76 620
984263bc
MD
621 switch (command) {
622 case SIOCSIFADDR:
984263bc
MD
623 switch (ifa->ifa_addr->sa_family) {
624#ifdef INET
625 case AF_INET:
cbf2eda6 626 IF_INIT(ifp); /* before arpwhohas */
984263bc
MD
627 arp_ifinit(ifp, ifa);
628 break;
629#endif
630#ifdef IPX
631 /*
632 * XXX - This code is probably wrong
633 */
634 case AF_IPX:
635 {
f23061d4 636 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
984263bc
MD
637 struct arpcom *ac = IFP2AC(ifp);
638
639 if (ipx_nullhost(*ina))
f23061d4
JH
640 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
641 else
642 bcopy(ina->x_host.c_host, ac->ac_enaddr,
643 sizeof ac->ac_enaddr);
984263bc 644
cbf2eda6 645 IF_INIT(ifp); /* Set new address. */
984263bc
MD
646 break;
647 }
648#endif
984263bc 649 default:
cbf2eda6 650 IF_INIT(ifp);
984263bc
MD
651 break;
652 }
653 break;
654
655 case SIOCGIFADDR:
f23061d4
JH
656 bcopy(IFP2AC(ifp)->ac_enaddr,
657 ((struct sockaddr *)ifr->ifr_data)->sa_data,
658 ETHER_ADDR_LEN);
984263bc
MD
659 break;
660
661 case SIOCSIFMTU:
662 /*
663 * Set the interface MTU.
664 */
665 if (ifr->ifr_mtu > ETHERMTU) {
666 error = EINVAL;
667 } else {
668 ifp->if_mtu = ifr->ifr_mtu;
669 }
670 break;
c2d9fd91
JS
671 default:
672 error = EINVAL;
673 break;
984263bc
MD
674 }
675 return (error);
cbf2eda6
SZ
676
677#undef IF_INIT
984263bc
MD
678}
679
680int
f23061d4
JH
681ether_resolvemulti(
682 struct ifnet *ifp,
683 struct sockaddr **llsa,
684 struct sockaddr *sa)
984263bc
MD
685{
686 struct sockaddr_dl *sdl;
687 struct sockaddr_in *sin;
688#ifdef INET6
689 struct sockaddr_in6 *sin6;
690#endif
691 u_char *e_addr;
692
693 switch(sa->sa_family) {
694 case AF_LINK:
695 /*
696 * No mapping needed. Just check that it's a valid MC address.
697 */
698 sdl = (struct sockaddr_dl *)sa;
699 e_addr = LLADDR(sdl);
700 if ((e_addr[0] & 1) != 1)
701 return EADDRNOTAVAIL;
702 *llsa = 0;
703 return 0;
704
705#ifdef INET
706 case AF_INET:
707 sin = (struct sockaddr_in *)sa;
708 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
709 return EADDRNOTAVAIL;
884717e1 710 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
984263bc
MD
711 sdl->sdl_len = sizeof *sdl;
712 sdl->sdl_family = AF_LINK;
713 sdl->sdl_index = ifp->if_index;
714 sdl->sdl_type = IFT_ETHER;
715 sdl->sdl_alen = ETHER_ADDR_LEN;
716 e_addr = LLADDR(sdl);
717 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
718 *llsa = (struct sockaddr *)sdl;
719 return 0;
720#endif
721#ifdef INET6
722 case AF_INET6:
723 sin6 = (struct sockaddr_in6 *)sa;
724 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
725 /*
726 * An IP6 address of 0 means listen to all
727 * of the Ethernet multicast address used for IP6.
728 * (This is used for multicast routers.)
729 */
730 ifp->if_flags |= IFF_ALLMULTI;
731 *llsa = 0;
732 return 0;
733 }
734 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
735 return EADDRNOTAVAIL;
884717e1 736 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
984263bc
MD
737 sdl->sdl_len = sizeof *sdl;
738 sdl->sdl_family = AF_LINK;
739 sdl->sdl_index = ifp->if_index;
740 sdl->sdl_type = IFT_ETHER;
741 sdl->sdl_alen = ETHER_ADDR_LEN;
742 e_addr = LLADDR(sdl);
743 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
744 *llsa = (struct sockaddr *)sdl;
745 return 0;
746#endif
747
748 default:
749 /*
750 * Well, the text isn't quite right, but it's the name
751 * that counts...
752 */
753 return EAFNOSUPPORT;
754 }
755}
d6018c31
JS
756
757#if 0
758/*
759 * This is for reference. We have a table-driven version
760 * of the little-endian crc32 generator, which is faster
761 * than the double-loop.
762 */
763uint32_t
764ether_crc32_le(const uint8_t *buf, size_t len)
765{
766 uint32_t c, crc, carry;
767 size_t i, j;
768
769 crc = 0xffffffffU; /* initial value */
770
771 for (i = 0; i < len; i++) {
772 c = buf[i];
773 for (j = 0; j < 8; j++) {
774 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
775 crc >>= 1;
776 c >>= 1;
777 if (carry)
778 crc = (crc ^ ETHER_CRC_POLY_LE);
779 }
780 }
781
f23061d4 782 return (crc);
d6018c31
JS
783}
784#else
785uint32_t
786ether_crc32_le(const uint8_t *buf, size_t len)
787{
788 static const uint32_t crctab[] = {
789 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
790 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
791 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
792 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
793 };
794 uint32_t crc;
795 size_t i;
796
797 crc = 0xffffffffU; /* initial value */
798
799 for (i = 0; i < len; i++) {
800 crc ^= buf[i];
801 crc = (crc >> 4) ^ crctab[crc & 0xf];
802 crc = (crc >> 4) ^ crctab[crc & 0xf];
803 }
804
f23061d4 805 return (crc);
d6018c31
JS
806}
807#endif
808
809uint32_t
810ether_crc32_be(const uint8_t *buf, size_t len)
811{
812 uint32_t c, crc, carry;
813 size_t i, j;
814
815 crc = 0xffffffffU; /* initial value */
816
817 for (i = 0; i < len; i++) {
818 c = buf[i];
819 for (j = 0; j < 8; j++) {
820 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
821 crc <<= 1;
822 c >>= 1;
823 if (carry)
824 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
825 }
826 }
827
f23061d4 828 return (crc);
d6018c31 829}
4d723e5a 830
4d723e5a
JS
831/*
832 * find the size of ethernet header, and call classifier
833 */
834void
835altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
836 struct altq_pktattr *pktattr)
837{
838 struct ether_header *eh;
839 uint16_t ether_type;
840 int hlen, af, hdrsize;
841 caddr_t hdr;
842
843 hlen = sizeof(struct ether_header);
844 eh = mtod(m, struct ether_header *);
845
846 ether_type = ntohs(eh->ether_type);
847 if (ether_type < ETHERMTU) {
848 /* ick! LLC/SNAP */
849 struct llc *llc = (struct llc *)(eh + 1);
850 hlen += 8;
851
852 if (m->m_len < hlen ||
853 llc->llc_dsap != LLC_SNAP_LSAP ||
854 llc->llc_ssap != LLC_SNAP_LSAP ||
855 llc->llc_control != LLC_UI)
856 goto bad; /* not snap! */
857
858 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
859 }
860
861 if (ether_type == ETHERTYPE_IP) {
862 af = AF_INET;
863 hdrsize = 20; /* sizeof(struct ip) */
864#ifdef INET6
865 } else if (ether_type == ETHERTYPE_IPV6) {
866 af = AF_INET6;
867 hdrsize = 40; /* sizeof(struct ip6_hdr) */
868#endif
869 } else
870 goto bad;
871
872 while (m->m_len <= hlen) {
873 hlen -= m->m_len;
874 m = m->m_next;
875 }
876 hdr = m->m_data + hlen;
877 if (m->m_len < hlen + hdrsize) {
878 /*
879 * ip header is not in a single mbuf. this should not
880 * happen in the current code.
881 * (todo: use m_pulldown in the future)
882 */
883 goto bad;
884 }
885 m->m_data += hlen;
886 m->m_len -= hlen;
887 ifq_classify(ifq, m, af, pktattr);
888 m->m_data -= hlen;
889 m->m_len += hlen;
890
891 return;
892
893bad:
894 pktattr->pattr_class = NULL;
895 pktattr->pattr_hdr = NULL;
896 pktattr->pattr_af = AF_UNSPEC;
897}
9b77ea6e
SZ
898
899static void
900ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
901 const struct ether_header *save_eh)
902{
903 struct mbuf *m = *m0;
904
905 ether_restore_hdr++;
906
907 /*
908 * Prepend the header, optimize for the common case of
909 * eh pointing into the mbuf.
910 */
911 if ((const void *)(eh + 1) == (void *)m->m_data) {
912 m->m_data -= ETHER_HDR_LEN;
913 m->m_len += ETHER_HDR_LEN;
914 m->m_pkthdr.len += ETHER_HDR_LEN;
915 } else {
916 ether_prepend_hdr++;
917
918 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
919 if (m != NULL) {
3d26a382 920 bcopy(save_eh, mtod(m, struct ether_header *),
9b77ea6e
SZ
921 ETHER_HDR_LEN);
922 }
923 }
924 *m0 = m;
925}
68b67450 926
68b67450
SZ
927static void
928ether_input_ipifunc(void *arg)
929{
930 struct mbuf *m, *next;
c3c96e44 931 lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid);
68b67450
SZ
932
933 m = arg;
934 do {
935 next = m->m_nextpkt;
936 m->m_nextpkt = NULL;
002c1265 937 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg);
68b67450
SZ
938 m = next;
939 } while (m != NULL);
940}
941
942void
943ether_input_dispatch(struct mbuf_chain *chain)
944{
945#ifdef SMP
946 int i;
947
f3e0b5f0 948 logether(disp_beg, NULL);
68b67450
SZ
949 for (i = 0; i < ncpus; ++i) {
950 if (chain[i].mc_head != NULL) {
951 lwkt_send_ipiq(globaldata_find(i),
c3c96e44 952 ether_input_ipifunc, chain[i].mc_head);
68b67450
SZ
953 }
954 }
955#else
f3e0b5f0 956 logether(disp_beg, NULL);
b358f907
SZ
957 if (chain->mc_head != NULL)
958 ether_input_ipifunc(chain->mc_head);
68b67450 959#endif
f3e0b5f0 960 logether(disp_end, NULL);
68b67450
SZ
961}
962
57ccf5a2
SZ
963void
964ether_input_chain_init(struct mbuf_chain *chain)
965{
966#ifdef SMP
967 int i;
968
969 for (i = 0; i < ncpus; ++i)
970 chain[i].mc_head = chain[i].mc_tail = NULL;
971#else
972 chain->mc_head = chain->mc_tail = NULL;
973#endif
974}
975
5f32d321
SZ
976/*
977 * Upper layer processing for a received Ethernet packet.
978 */
297c8124 979void
29bc1092
SZ
980ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
981{
982 struct ether_header *eh;
21b74198 983 int isr, discard = 0;
29bc1092
SZ
984 u_short ether_type;
985 struct ip_fw *rule = NULL;
29bc1092
SZ
986
987 M_ASSERTPKTHDR(m);
988 KASSERT(m->m_len >= ETHER_HDR_LEN,
989 ("ether header is no contiguous!\n"));
990
991 eh = mtod(m, struct ether_header *);
992
eb241549
SZ
993 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
994 struct m_tag *mtag;
995
996 /* Extract info from dummynet tag */
997 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
998 KKASSERT(mtag != NULL);
29bc1092 999 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
eb241549
SZ
1000 KKASSERT(rule != NULL);
1001
29bc1092 1002 m_tag_delete(m, mtag);
eb241549
SZ
1003 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1004
1005 /* packet is passing the second time */
29bc1092 1006 goto post_stats;
eb241549 1007 }
29bc1092
SZ
1008
1009#ifdef CARP
1010 /*
1011 * XXX: Okay, we need to call carp_forus() and - if it is for
1012 * us jump over code that does the normal check
1013 * "ac_enaddr == ether_dhost". The check sequence is a bit
1014 * different from OpenBSD, so we jump over as few code as
1015 * possible, to catch _all_ sanity checks. This needs
1016 * evaluation, to see if the carp ether_dhost values break any
1017 * of these checks!
1018 */
c6690c74
SZ
1019 if (ifp->if_carp) {
1020 /*
1021 * Hold BGL and recheck ifp->if_carp
1022 */
1023 get_mplock();
1024 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1025 rel_mplock();
1026 goto post_stats;
1027 }
1028 rel_mplock();
1029 }
29bc1092
SZ
1030#endif
1031
1032 /*
469c71d1
SZ
1033 * We got a packet which was unicast to a different Ethernet
1034 * address. If the driver is working properly, then this
1035 * situation can only happen when the interface is in
1036 * promiscuous mode. We defer the packet discarding until the
1037 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1038 * could work.
29bc1092
SZ
1039 */
1040 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
d1859e78 1041 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
70d9a675
MD
1042 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1043 if (ether_debug & 1) {
1044 kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1045 "%02x:%02x:%02x:%02x:%02x:%02x "
1046 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1047 eh->ether_dhost[0],
1048 eh->ether_dhost[1],
1049 eh->ether_dhost[2],
1050 eh->ether_dhost[3],
1051 eh->ether_dhost[4],
1052 eh->ether_dhost[5],
1053 eh->ether_shost[0],
1054 eh->ether_shost[1],
1055 eh->ether_shost[2],
1056 eh->ether_shost[3],
1057 eh->ether_shost[4],
1058 eh->ether_shost[5],
1059 eh->ether_type,
1060 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1061 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1062 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1063 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1064 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1065 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1066 );
1067 }
1068 if ((ether_debug & 2) == 0)
1069 discard = 1;
1070 }
29bc1092
SZ
1071
1072post_stats:
469c71d1 1073 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
29bc1092
SZ
1074 struct ether_header save_eh = *eh;
1075
1076 /* XXX old crufty stuff, needs to be removed */
1077 m_adj(m, sizeof(struct ether_header));
1078
1079 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1080 m_freem(m);
1081 return;
1082 }
1083
1084 ether_restore_header(&m, eh, &save_eh);
1085 if (m == NULL)
1086 return;
1087 eh = mtod(m, struct ether_header *);
1088 }
1089
1090 ether_type = ntohs(eh->ether_type);
1091 KKASSERT(ether_type != ETHERTYPE_VLAN);
1092
1093 if (m->m_flags & M_VLANTAG) {
b327296f
SZ
1094 void (*vlan_input_func)(struct mbuf *);
1095
1096 vlan_input_func = vlan_input_p;
1097 if (vlan_input_func != NULL) {
1098 vlan_input_func(m);
29bc1092
SZ
1099 } else {
1100 m->m_pkthdr.rcvif->if_noproto++;
1101 m_freem(m);
1102 }
29bc1092
SZ
1103 return;
1104 }
1105
4d895293 1106 /*
469c71d1
SZ
1107 * If we have been asked to discard this packet
1108 * (e.g. not for us), drop it before entering
1109 * the upper layer.
1110 */
1111 if (discard) {
1112 m_freem(m);
1113 return;
1114 }
1115
1116 /*
4d895293
SZ
1117 * Clear protocol specific flags,
1118 * before entering the upper layer.
1119 */
da1604af 1120 m->m_flags &= ~M_ETHER_FLAGS;
4d895293
SZ
1121
1122 /* Strip ethernet header. */
29bc1092 1123 m_adj(m, sizeof(struct ether_header));
4d895293 1124
29bc1092
SZ
1125 switch (ether_type) {
1126#ifdef INET
1127 case ETHERTYPE_IP:
8697599b 1128 if ((m->m_flags & M_LENCHECKED) == 0) {
c3c96e44 1129 if (!ip_lengthcheck(&m, 0))
8697599b
SZ
1130 return;
1131 }
297c8124 1132 if (ipflow_fastforward(m))
29bc1092 1133 return;
29bc1092
SZ
1134 isr = NETISR_IP;
1135 break;
1136
1137 case ETHERTYPE_ARP:
1138 if (ifp->if_flags & IFF_NOARP) {
1139 /* Discard packet if ARP is disabled on interface */
1140 m_freem(m);
1141 return;
1142 }
1143 isr = NETISR_ARP;
1144 break;
1145#endif
1146
1147#ifdef INET6
1148 case ETHERTYPE_IPV6:
1149 isr = NETISR_IPV6;
1150 break;
1151#endif
1152
1153#ifdef IPX
1154 case ETHERTYPE_IPX:
c6690c74
SZ
1155 if (ef_inputp) {
1156 /*
1157 * Hold BGL and recheck ef_inputp
1158 */
1159 get_mplock();
1160 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1161 rel_mplock();
1162 return;
1163 }
1164 rel_mplock();
1165 }
29bc1092
SZ
1166 isr = NETISR_IPX;
1167 break;
1168#endif
1169
a020e9d5
SZ
1170#ifdef MPLS
1171 case ETHERTYPE_MPLS:
1172 case ETHERTYPE_MPLS_MCAST:
50098e2e 1173 /* Should have been set by ether_input_chain(). */
cb8d752c 1174 KKASSERT(m->m_flags & M_MPLSLABELED);
a020e9d5
SZ
1175 isr = NETISR_MPLS;
1176 break;
1177#endif
1178
29bc1092
SZ
1179 default:
1180 /*
1181 * The accurate msgport is not determined before
ebe4c2ae 1182 * we reach here, so recharacterize packet.
29bc1092 1183 */
ebe4c2ae 1184 m->m_flags &= ~M_HASH;
29bc1092 1185#ifdef IPX
c6690c74
SZ
1186 if (ef_inputp) {
1187 /*
1188 * Hold BGL and recheck ef_inputp
1189 */
1190 get_mplock();
1191 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1192 rel_mplock();
1193 return;
1194 }
1195 rel_mplock();
1196 }
29bc1092 1197#endif
c6690c74
SZ
1198 if (ng_ether_input_orphan_p != NULL) {
1199 /*
1200 * Hold BGL and recheck ng_ether_input_orphan_p
1201 */
1202 get_mplock();
1203 if (ng_ether_input_orphan_p != NULL) {
1204 ng_ether_input_orphan_p(ifp, m, eh);
1205 rel_mplock();
1206 return;
1207 }
1208 rel_mplock();
1209 }
1210 m_freem(m);
29bc1092
SZ
1211 return;
1212 }
1213
ebe4c2ae
SZ
1214 if (m->m_flags & M_HASH) {
1215 if (&curthread->td_msgport == cpu_portfn(m->m_pkthdr.hash)) {
1216 netisr_handle(isr, m);
1217 return;
1218 } else {
1219 /*
1220 * XXX Something is wrong,
1221 * we probably should panic here!
1222 */
1223 m->m_flags &= ~M_HASH;
1224 ether_input_wronghash++;
1225 }
1226 }
1227#ifdef RSS_DEBUG
1228 ether_input_requeue++;
1229#endif
c3c96e44 1230 netisr_queue(isr, m);
29bc1092
SZ
1231}
1232
5f32d321
SZ
1233/*
1234 * First we perform any link layer operations, then continue to the
1235 * upper layers with ether_demux_oncpu().
1236 */
c3c96e44 1237static void
29bc1092
SZ
1238ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1239{
160af078
SZ
1240 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1241 /*
1242 * Receiving interface's flags are changed, when this
1243 * packet is waiting for processing; discard it.
1244 */
1245 m_freem(m);
1246 return;
1247 }
1248
29bc1092
SZ
1249 /*
1250 * Tap the packet off here for a bridge. bridge_input()
1251 * will return NULL if it has consumed the packet, otherwise
1252 * it gets processed as normal. Note that bridge_input()
1253 * will always return the original packet if we need to
1254 * process it locally.
1255 */
1256 if (ifp->if_bridge) {
1257 KASSERT(bridge_input_p != NULL,
1258 ("%s: if_bridge not loaded!", __func__));
1259
0899cb3e
SZ
1260 if(m->m_flags & M_ETHER_BRIDGED) {
1261 m->m_flags &= ~M_ETHER_BRIDGED;
29bc1092 1262 } else {
29bc1092
SZ
1263 m = bridge_input_p(ifp, m);
1264 if (m == NULL)
1265 return;
1266
1267 KASSERT(ifp == m->m_pkthdr.rcvif,
1268 ("bridge_input_p changed rcvif\n"));
1269 }
1270 }
1271
1272 /* Handle ng_ether(4) processing, if any */
1273 if (ng_ether_input_p != NULL) {
c6690c74
SZ
1274 /*
1275 * Hold BGL and recheck ng_ether_input_p
1276 */
1277 get_mplock();
1278 if (ng_ether_input_p != NULL)
1279 ng_ether_input_p(ifp, &m);
1280 rel_mplock();
1281
29bc1092
SZ
1282 if (m == NULL)
1283 return;
1284 }
1285
1286 /* Continue with upper layer processing */
1287 ether_demux_oncpu(ifp, m);
1288}
1289
b9ed4403
SZ
1290/*
1291 * Perform certain functions of ether_input_chain():
1292 * - Test IFF_UP
1293 * - Update statistics
1294 * - Run bpf(4) tap if requested
1295 * Then pass the packet to ether_input_oncpu().
1296 *
1297 * This function should be used by pseudo interface (e.g. vlan(4)),
1298 * when it tries to claim that the packet is received by it.
4ee4f753
MD
1299 *
1300 * REINPUT_KEEPRCVIF
1301 * REINPUT_RUNBPF
b9ed4403
SZ
1302 */
1303void
4ee4f753 1304ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
b9ed4403
SZ
1305{
1306 /* Discard packet if interface is not up */
1307 if (!(ifp->if_flags & IFF_UP)) {
1308 m_freem(m);
1309 return;
1310 }
1311
4ee4f753
MD
1312 /*
1313 * Change receiving interface. The bridge will often pass a flag to
1314 * ask that this not be done so ARPs get applied to the correct
1315 * side.
1316 */
1317 if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1318 m->m_pkthdr.rcvif == NULL) {
1319 m->m_pkthdr.rcvif = ifp;
1320 }
b9ed4403
SZ
1321
1322 /* Update statistics */
1323 ifp->if_ipackets++;
1324 ifp->if_ibytes += m->m_pkthdr.len;
1325 if (m->m_flags & (M_MCAST | M_BCAST))
1326 ifp->if_imcasts++;
1327
4ee4f753 1328 if (reinput_flags & REINPUT_RUNBPF)
b9ed4403
SZ
1329 BPF_MTAP(ifp, m);
1330
1331 ether_input_oncpu(ifp, m);
1332}
1333
057441be
SZ
1334static __inline boolean_t
1335ether_vlancheck(struct mbuf **m0)
1336{
1337 struct mbuf *m = *m0;
1338 struct ether_header *eh;
1339 uint16_t ether_type;
1340
1341 eh = mtod(m, struct ether_header *);
1342 ether_type = ntohs(eh->ether_type);
1343
1344 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1345 /*
1346 * Extract vlan tag if hardware does not do it for us
1347 */
1348 vlan_ether_decap(&m);
1349 if (m == NULL)
1350 goto failed;
1351
1352 eh = mtod(m, struct ether_header *);
1353 ether_type = ntohs(eh->ether_type);
1354 }
1355
1356 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1357 /*
1358 * To prevent possible dangerous recursion,
1359 * we don't do vlan-in-vlan
1360 */
1361 m->m_pkthdr.rcvif->if_noproto++;
1362 goto failed;
1363 }
1364 KKASSERT(ether_type != ETHERTYPE_VLAN);
1365
da1604af 1366 m->m_flags |= M_ETHER_VLANCHECKED;
057441be
SZ
1367 *m0 = m;
1368 return TRUE;
1369failed:
1370 if (m != NULL)
1371 m_freem(m);
1372 *m0 = NULL;
1373 return FALSE;
1374}
1375
29bc1092 1376static void
002c1265 1377ether_input_handler(netmsg_t nmsg)
29bc1092 1378{
002c1265 1379 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
828c9923 1380 struct ether_header *eh;
29bc1092
SZ
1381 struct ifnet *ifp;
1382 struct mbuf *m;
1383
1384 m = nmp->nm_packet;
1385 M_ASSERTPKTHDR(m);
1386 ifp = m->m_pkthdr.rcvif;
1387
828c9923
SZ
1388 eh = mtod(m, struct ether_header *);
1389 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1390 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1391 ifp->if_addrlen) == 0)
1392 m->m_flags |= M_BCAST;
1393 else
1394 m->m_flags |= M_MCAST;
1395 ifp->if_imcasts++;
1396 }
1397
da1604af
SZ
1398 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1399 if (!ether_vlancheck(&m)) {
1400 KKASSERT(m == NULL);
1401 return;
1402 }
1403 }
1404
29bc1092
SZ
1405 ether_input_oncpu(ifp, m);
1406}
1407
c3c96e44
MD
1408/*
1409 * Send the packet to the target msgport or queue it into 'chain'.
21b74198
MD
1410 *
1411 * At this point the packet had better be characterized (M_HASH set),
1412 * so we know which cpu to send it to.
c3c96e44
MD
1413 */
1414static void
1415ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain)
29bc1092
SZ
1416{
1417 struct netmsg_packet *pmsg;
1418
c3c96e44 1419 KKASSERT(m->m_flags & M_HASH);
29bc1092 1420 pmsg = &m->m_hdr.mh_netmsg;
002c1265 1421 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
c3c96e44 1422 0, ether_input_handler);
29bc1092 1423 pmsg->nm_packet = m;
002c1265 1424 pmsg->base.lmsg.u.ms_result = isr;
74f66604
SZ
1425
1426 if (chain != NULL) {
c3c96e44 1427 int cpuid = m->m_pkthdr.hash;
74f66604 1428 struct mbuf_chain *c;
74f66604
SZ
1429
1430 c = &chain[cpuid];
1431 if (c->mc_head == NULL) {
1432 c->mc_head = c->mc_tail = m;
1433 } else {
1434 c->mc_tail->m_nextpkt = m;
1435 c->mc_tail = m;
1436 }
1437 m->m_nextpkt = NULL;
1438 } else {
002c1265 1439 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
74f66604
SZ
1440 }
1441}
1442
62f35c44
SZ
1443/*
1444 * Process a received Ethernet packet.
1445 *
1446 * The ethernet header is assumed to be in the mbuf so the caller
1447 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1448 * bytes in the first mbuf.
1449 *
62f35c44 1450 * - If 'chain' is NULL, this ether frame is sent to the target msgport
50098e2e 1451 * immediately. This situation happens when ether_input_chain is
62f35c44 1452 * accessed through ifnet.if_input.
c3c96e44 1453 *
62f35c44
SZ
1454 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1455 * bucket indexed by the target msgport's cpuid and the target msgport
50098e2e 1456 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
62f35c44
SZ
1457 * must initialize 'chain' by calling ether_input_chain_init().
1458 * ether_input_dispatch must be called later to send ether frames
1459 * queued on 'chain' to their target msgport.
1460 */
29bc1092 1461void
2eb0d069
SZ
1462ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1463 struct mbuf_chain *chain)
29bc1092 1464{
29bc1092
SZ
1465 int isr;
1466
29bc1092
SZ
1467 M_ASSERTPKTHDR(m);
1468
1469 /* Discard packet if interface is not up */
1470 if (!(ifp->if_flags & IFF_UP)) {
1471 m_freem(m);
1472 return;
1473 }
1474
1475 if (m->m_len < sizeof(struct ether_header)) {
1476 /* XXX error in the caller. */
1477 m_freem(m);
1478 return;
1479 }
29bc1092
SZ
1480
1481 m->m_pkthdr.rcvif = ifp;
1482
f3e0b5f0
SZ
1483 logether(chain_beg, ifp);
1484
29bc1092
SZ
1485 ETHER_BPF_MTAP(ifp, m);
1486
1487 ifp->if_ibytes += m->m_pkthdr.len;
1488
1489 if (ifp->if_flags & IFF_MONITOR) {
0e805566
SZ
1490 struct ether_header *eh;
1491
828c9923
SZ
1492 eh = mtod(m, struct ether_header *);
1493 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1494 ifp->if_imcasts++;
1495
29bc1092
SZ
1496 /*
1497 * Interface marked for monitoring; discard packet.
1498 */
b5a65047 1499 m_freem(m);
f3e0b5f0
SZ
1500
1501 logether(chain_end, ifp);
b5a65047 1502 return;
29bc1092
SZ
1503 }
1504
c3c96e44
MD
1505 /*
1506 * If the packet has been characterized (pi->pi_netisr / M_HASH)
1507 * we can dispatch it immediately without further inspection.
1508 */
2eb0d069 1509 if (pi != NULL && (m->m_flags & M_HASH)) {
7c7f9646
SZ
1510#ifdef RSS_DEBUG
1511 ether_pktinfo_try++;
1512#endif
e6f77b88
SZ
1513 netisr_hashcheck(pi->pi_netisr, m, pi);
1514 if (m->m_flags & M_HASH) {
1515 ether_dispatch(pi->pi_netisr, m, chain);
7c7f9646 1516#ifdef RSS_DEBUG
e6f77b88 1517 ether_pktinfo_hit++;
7c7f9646 1518#endif
e6f77b88
SZ
1519 logether(chain_end, ifp);
1520 return;
1521 }
2eb0d069 1522 }
7c7f9646
SZ
1523#ifdef RSS_DEBUG
1524 else if (ifp->if_capenable & IFCAP_RSS) {
1525 if (pi == NULL)
1526 ether_rss_nopi++;
1527 else
1528 ether_rss_nohash++;
1529 }
1530#endif
2eb0d069
SZ
1531
1532 /*
1533 * Packet hash will be recalculated by software,
1534 * so clear the M_HASH flag set by the driver;
1535 * the hash value calculated by the hardware may
1536 * not be exactly what we want.
1537 */
1538 m->m_flags &= ~M_HASH;
1539
057441be 1540 if (!ether_vlancheck(&m)) {
057441be 1541 KKASSERT(m == NULL);
a4923720 1542 logether(chain_end, ifp);
29bc1092
SZ
1543 return;
1544 }
0e805566
SZ
1545
1546 isr = ether_characterize(&m);
1547 if (m == NULL) {
1548 logether(chain_end, ifp);
1549 return;
1550 }
1551
1552 /*
1553 * Finally dispatch it
1554 */
1555 ether_dispatch(isr, m, chain);
1556
1557 logether(chain_end, ifp);
1558}
1559
1560static int
1561ether_characterize(struct mbuf **m0)
1562{
1563 struct mbuf *m = *m0;
1564 struct ether_header *eh;
1565 uint16_t ether_type;
1566 int isr;
1567
057441be
SZ
1568 eh = mtod(m, struct ether_header *);
1569 ether_type = ntohs(eh->ether_type);
29bc1092
SZ
1570
1571 /*
1572 * Map ether type to netisr id.
1573 */
1574 switch (ether_type) {
1575#ifdef INET
1576 case ETHERTYPE_IP:
1577 isr = NETISR_IP;
1578 break;
1579
1580 case ETHERTYPE_ARP:
1581 isr = NETISR_ARP;
1582 break;
1583#endif
1584
1585#ifdef INET6
1586 case ETHERTYPE_IPV6:
1587 isr = NETISR_IPV6;
1588 break;
1589#endif
1590
1591#ifdef IPX
1592 case ETHERTYPE_IPX:
1593 isr = NETISR_IPX;
1594 break;
1595#endif
1596
a020e9d5
SZ
1597#ifdef MPLS
1598 case ETHERTYPE_MPLS:
1599 case ETHERTYPE_MPLS_MCAST:
cb8d752c 1600 m->m_flags |= M_MPLSLABELED;
a020e9d5
SZ
1601 isr = NETISR_MPLS;
1602 break;
1603#endif
1604
29bc1092
SZ
1605 default:
1606 /*
1607 * NETISR_MAX is an invalid value; it is chosen to let
0e805566
SZ
1608 * netisr_characterize() know that we have no clear
1609 * idea where this packet should go.
29bc1092
SZ
1610 */
1611 isr = NETISR_MAX;
1612 break;
1613 }
1614
1615 /*
c3c96e44
MD
1616 * Ask the isr to characterize the packet since we couldn't.
1617 * This is an attempt to optimally get us onto the correct protocol
1618 * thread.
29bc1092 1619 */
c3c96e44 1620 netisr_characterize(isr, &m, sizeof(struct ether_header));
29bc1092 1621
0e805566
SZ
1622 *m0 = m;
1623 return isr;
29bc1092 1624}
09c280ec 1625
48242f47
SZ
1626static void
1627ether_demux_handler(netmsg_t nmsg)
1628{
1629 struct netmsg_packet *nmp = &nmsg->packet; /* actual size */
1630 struct ifnet *ifp;
1631 struct mbuf *m;
1632
1633 m = nmp->nm_packet;
1634 M_ASSERTPKTHDR(m);
1635 ifp = m->m_pkthdr.rcvif;
1636
1637 ether_demux_oncpu(ifp, m);
1638}
1639
1640void
1641ether_demux(struct mbuf *m)
1642{
1643 struct netmsg_packet *pmsg;
1644 int isr;
1645
1646 isr = ether_characterize(&m);
1647 if (m == NULL)
1648 return;
1649
1650 KKASSERT(m->m_flags & M_HASH);
1651 pmsg = &m->m_hdr.mh_netmsg;
1652 netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1653 0, ether_demux_handler);
1654 pmsg->nm_packet = m;
1655 pmsg->base.lmsg.u.ms_result = isr;
1656
1657 lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg);
1658}
1659
09c280ec 1660MODULE_VERSION(ether, 1);