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