Clean up routing code before I parallelize it.
[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 $
2e9572df 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.20 2004/12/14 18:46:08 hsu 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"
42#include "opt_bdg.h"
43#include "opt_netgraph.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52#include <sys/sysctl.h>
53
54#include <net/if.h>
55#include <net/netisr.h>
56#include <net/route.h>
57#include <net/if_llc.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/bpf.h>
61#include <net/ethernet.h>
1f2de5d4 62#include <net/bridge/bridge.h>
984263bc
MD
63
64#if defined(INET) || defined(INET6)
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#include <netinet/if_ether.h>
1f2de5d4
MD
68#include <net/ipfw/ip_fw.h>
69#include <net/dummynet/ip_dummynet.h>
984263bc
MD
70#endif
71#ifdef INET6
72#include <netinet6/nd6.h>
73#endif
74
75#ifdef IPX
d2438d69
MD
76#include <netproto/ipx/ipx.h>
77#include <netproto/ipx/ipx_if.h>
984263bc
MD
78int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
79int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
80 struct sockaddr *dst, short *tp, int *hlen);
81#endif
82
83#ifdef NS
84#include <netns/ns.h>
85#include <netns/ns_if.h>
86ushort ns_nettype;
87int ether_outputdebug = 0;
88int ether_inputdebug = 0;
89#endif
90
91#ifdef NETATALK
d2438d69
MD
92#include <netproto/atalk/at.h>
93#include <netproto/atalk/at_var.h>
94#include <netproto/atalk/at_extern.h>
984263bc
MD
95
96#define llc_snap_org_code llc_un.type_snap.org_code
97#define llc_snap_ether_type llc_un.type_snap.ether_type
98
99extern u_char at_org_code[3];
100extern u_char aarp_org_code[3];
101#endif /* NETATALK */
102
103/* netgraph node hooks for ng_ether(4) */
104void (*ng_ether_input_p)(struct ifnet *ifp,
105 struct mbuf **mp, struct ether_header *eh);
106void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
107 struct mbuf *m, struct ether_header *eh);
108int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
109void (*ng_ether_attach_p)(struct ifnet *ifp);
110void (*ng_ether_detach_p)(struct ifnet *ifp);
111
112int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
3013ac0e
JS
113int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
114
115static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116 struct rtentry *);
984263bc
MD
117
118/* bridge support */
119int do_bridge;
120bridge_in_t *bridge_in_ptr;
121bdg_forward_t *bdg_forward_ptr;
122bdgtakeifaces_t *bdgtakeifaces_ptr;
123struct bdg_softc *ifp2sc;
124
125static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
126 struct sockaddr *);
c401f0fd
JS
127const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
129};
130
984263bc
MD
131#define senderr(e) do { error = (e); goto bad;} while (0)
132#define IFP2AC(IFP) ((struct arpcom *)IFP)
133
134int
135ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 struct ip_fw **rule, struct ether_header *eh, int shared);
137static int ether_ipfw;
138
139/*
140 * Ethernet output routine.
141 * Encapsulate a packet of type family for the local net.
142 * Use trailer local net encapsulation if enough data in first
143 * packet leaves a multiple of 512 bytes of data in remainder.
144 * Assumes that ifp is actually pointer to arpcom structure.
145 */
3013ac0e
JS
146static int
147ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
148 struct rtentry *rt0)
984263bc
MD
149{
150 short type;
151 int error = 0, hdrcmplt = 0;
152 u_char esrc[6], edst[6];
82ed7fc2
RG
153 struct rtentry *rt;
154 struct ether_header *eh;
984263bc
MD
155 int loop_copy = 0;
156 int hlen; /* link layer header lenght */
157 struct arpcom *ac = IFP2AC(ifp);
158
159 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
160 senderr(ENETDOWN);
161 rt = rt0;
162 if (rt) {
163 if ((rt->rt_flags & RTF_UP) == 0) {
164 rt0 = rt = rtalloc1(dst, 1, 0UL);
165 if (rt0)
166 rt->rt_refcnt--;
167 else
168 senderr(EHOSTUNREACH);
169 }
170 if (rt->rt_flags & RTF_GATEWAY) {
171 if (rt->rt_gwroute == 0)
172 goto lookup;
173 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
174 rtfree(rt); rt = rt0;
175 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
176 0UL);
177 if ((rt = rt->rt_gwroute) == 0)
178 senderr(EHOSTUNREACH);
179 }
180 }
181 if (rt->rt_flags & RTF_REJECT)
182 if (rt->rt_rmx.rmx_expire == 0 ||
183 time_second < rt->rt_rmx.rmx_expire)
184 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
185 }
186 hlen = ETHER_HDR_LEN;
187 switch (dst->sa_family) {
188#ifdef INET
189 case AF_INET:
190 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
191 return (0); /* if not yet resolved */
192 type = htons(ETHERTYPE_IP);
193 break;
194#endif
195#ifdef INET6
196 case AF_INET6:
197 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
198 /* Something bad happened */
199 return(0);
200 }
201 type = htons(ETHERTYPE_IPV6);
202 break;
203#endif
204#ifdef IPX
205 case AF_IPX:
206 if (ef_outputp) {
207 error = ef_outputp(ifp, &m, dst, &type, &hlen);
208 if (error)
209 goto bad;
210 } else
211 type = htons(ETHERTYPE_IPX);
212 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
213 (caddr_t)edst, sizeof (edst));
214 break;
215#endif
216#ifdef NETATALK
217 case AF_APPLETALK:
218 {
219 struct at_ifaddr *aa;
220
221 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
222 goto bad;
223 }
224 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
225 return (0);
226 /*
227 * In the phase 2 case, need to prepend an mbuf for the llc header.
228 * Since we must preserve the value of m, which is passed to us by
229 * value, we m_copy() the first mbuf, and use it for our llc header.
230 */
231 if ( aa->aa_flags & AFA_PHASE2 ) {
232 struct llc llc;
233
74f1caca 234 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
984263bc
MD
235 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
236 llc.llc_control = LLC_UI;
237 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
238 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
239 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
240 type = htons(m->m_pkthdr.len);
241 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
242 } else {
243 type = htons(ETHERTYPE_AT);
244 }
245 break;
246 }
247#endif /* NETATALK */
248#ifdef NS
249 case AF_NS:
250 switch(ns_nettype){
251 default:
252 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
253 type = 0x8137;
254 break;
255 case 0x0: /* Novell 802.3 */
256 type = htons( m->m_pkthdr.len);
257 break;
258 case 0xe0e0: /* Novell 802.2 and Token-Ring */
74f1caca 259 M_PREPEND(m, 3, MB_WAIT);
984263bc
MD
260 type = htons( m->m_pkthdr.len);
261 cp = mtod(m, u_char *);
262 *cp++ = 0xE0;
263 *cp++ = 0xE0;
264 *cp++ = 0x03;
265 break;
266 }
267 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
268 (caddr_t)edst, sizeof (edst));
269 /*
270 * XXX if ns_thishost is the same as the node's ethernet
271 * address then just the default code will catch this anyhow.
272 * So I'm not sure if this next clause should be here at all?
273 * [JRE]
274 */
275 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
276 m->m_pkthdr.rcvif = ifp;
8bde602d 277 netisr_dispatch(NETISR_NS, m);
984263bc
MD
278 return (error);
279 }
280 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
281 m->m_flags |= M_BCAST;
282 }
283 break;
284#endif /* NS */
285
286 case pseudo_AF_HDRCMPLT:
287 hdrcmplt = 1;
288 eh = (struct ether_header *)dst->sa_data;
289 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
290 /* FALLTHROUGH */
291
292 case AF_UNSPEC:
293 loop_copy = -1; /* if this is for us, don't do it */
294 eh = (struct ether_header *)dst->sa_data;
295 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
296 type = eh->ether_type;
297 break;
298
299 default:
3e4a09e7 300 printf("%s: can't handle af%d\n", ifp->if_xname,
984263bc
MD
301 dst->sa_family);
302 senderr(EAFNOSUPPORT);
303 }
304
305 /*
306 * Add local net header. If no space in first mbuf,
307 * allocate another.
308 */
74f1caca 309 M_PREPEND(m, sizeof (struct ether_header), MB_DONTWAIT);
984263bc
MD
310 if (m == 0)
311 senderr(ENOBUFS);
312 eh = mtod(m, struct ether_header *);
313 (void)memcpy(&eh->ether_type, &type,
314 sizeof(eh->ether_type));
315 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
316 if (hdrcmplt)
317 (void)memcpy(eh->ether_shost, esrc,
318 sizeof(eh->ether_shost));
319 else
320 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
321 sizeof(eh->ether_shost));
322
323 /*
324 * If a simplex interface, and the packet is being sent to our
325 * Ethernet address or a broadcast address, loopback a copy.
326 * XXX To make a simplex device behave exactly like a duplex
327 * device, we should copy in the case of sending to our own
328 * ethernet address (thus letting the original actually appear
329 * on the wire). However, we don't do that here for security
330 * reasons and compatibility with the original behavior.
331 */
332 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
333 int csum_flags = 0;
334
335 if (m->m_pkthdr.csum_flags & CSUM_IP)
336 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
337 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
338 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
339 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
340 struct mbuf *n;
341
342 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
343 n->m_pkthdr.csum_flags |= csum_flags;
344 if (csum_flags & CSUM_DATA_VALID)
345 n->m_pkthdr.csum_data = 0xffff;
346 (void)if_simloop(ifp, n, dst->sa_family, hlen);
347 } else
348 ifp->if_iqdrops++;
349 } else if (bcmp(eh->ether_dhost,
350 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
351 m->m_pkthdr.csum_flags |= csum_flags;
352 if (csum_flags & CSUM_DATA_VALID)
353 m->m_pkthdr.csum_data = 0xffff;
354 (void) if_simloop(ifp, m, dst->sa_family, hlen);
355 return (0); /* XXX */
356 }
357 }
358
359 /* Handle ng_ether(4) processing, if any */
360 if (ng_ether_output_p != NULL) {
361 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
362bad: if (m != NULL)
363 m_freem(m);
364 return (error);
365 }
366 if (m == NULL)
367 return (0);
368 }
369
370 /* Continue with link-layer output */
371 return ether_output_frame(ifp, m);
372}
373
374/*
375 * Ethernet link layer output routine to send a raw frame to the device.
376 *
377 * This assumes that the 14 byte Ethernet header is present and contiguous
378 * in the first mbuf (if BRIDGE'ing).
379 */
380int
381ether_output_frame(ifp, m)
382 struct ifnet *ifp;
383 struct mbuf *m;
384{
385 int error = 0;
386 int s;
387 struct ip_fw *rule = NULL;
388
389 /* Extract info from dummynet tag, ignore others */
390 for (; m->m_type == MT_TAG; m = m->m_next)
391 if (m->m_flags == PACKET_TAG_DUMMYNET)
392 rule = ((struct dn_pkt *)m)->rule;
393
394 if (rule) /* packet was already bridged */
395 goto no_bridge;
396
397 if (BDG_ACTIVE(ifp) ) {
398 struct ether_header *eh; /* a ptr suffices */
399
400 m->m_pkthdr.rcvif = NULL;
401 eh = mtod(m, struct ether_header *);
402 m_adj(m, ETHER_HDR_LEN);
403 m = bdg_forward_ptr(m, eh, ifp);
404 if (m != NULL)
405 m_freem(m);
406 return (0);
407 }
408
409no_bridge:
410 s = splimp();
411 if (IPFW_LOADED && ether_ipfw != 0) {
412 struct ether_header save_eh, *eh;
413
414 eh = mtod(m, struct ether_header *);
415 save_eh = *eh;
416 m_adj(m, ETHER_HDR_LEN);
417 if (ether_ipfw_chk(&m, ifp, &rule, eh, 0) == 0) {
418 if (m) {
419 m_freem(m);
420 return ENOBUFS; /* pkt dropped */
421 } else
422 return 0; /* consumed e.g. in a pipe */
423 }
424 /* packet was ok, restore the ethernet header */
425 if ( (void *)(eh + 1) == (void *)m->m_data) {
426 m->m_data -= ETHER_HDR_LEN ;
427 m->m_len += ETHER_HDR_LEN ;
428 m->m_pkthdr.len += ETHER_HDR_LEN ;
429 } else {
74f1caca 430 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
984263bc
MD
431 if (m == NULL) /* nope... */
432 return ENOBUFS;
433 bcopy(&save_eh, mtod(m, struct ether_header *),
434 ETHER_HDR_LEN);
435 }
436 }
437
438 /*
439 * Queue message on interface, update output statistics if
440 * successful, and start output if interface not yet active.
441 */
442 if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
443 error = ENOBUFS;
444 splx(s);
445 return (error);
446}
447
448/*
449 * ipfw processing for ethernet packets (in and out).
450 * The second parameter is NULL from ether_demux, and ifp from
451 * ether_output_frame. This section of code could be used from
452 * bridge.c as well as long as we use some extra info
453 * to distinguish that case from ether_output_frame();
454 */
455int
456ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
457 struct ip_fw **rule, struct ether_header *eh, int shared)
458{
459 struct ether_header save_eh = *eh; /* might be a ptr in m */
460 int i;
461 struct ip_fw_args args;
462
463 if (*rule != NULL && fw_one_pass)
464 return 1; /* dummynet packet, already partially processed */
465
466 /*
467 * I need some amt of data to be contiguous, and in case others need
468 * the packet (shared==1) also better be in the first mbuf.
469 */
470 i = min( (*m0)->m_pkthdr.len, max_protohdr);
471 if ( shared || (*m0)->m_len < i) {
472 *m0 = m_pullup(*m0, i);
473 if (*m0 == NULL)
474 return 0;
475 }
476
477 args.m = *m0; /* the packet we are looking at */
478 args.oif = dst; /* destination, if any */
479 args.divert_rule = 0; /* we do not support divert yet */
480 args.rule = *rule; /* matching rule to restart */
481 args.next_hop = NULL; /* we do not support forward yet */
482 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
483 i = ip_fw_chk_ptr(&args);
484 *m0 = args.m;
485 *rule = args.rule;
486
487 if ( (i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
488 return 0;
489
490 if (i == 0) /* a PASS rule. */
491 return 1;
492
493 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
494 /*
495 * Pass the pkt to dummynet, which consumes it.
496 * If shared, make a copy and keep the original.
497 */
498 struct mbuf *m ;
499
500 if (shared) {
74f1caca 501 m = m_copypacket(*m0, MB_DONTWAIT);
984263bc
MD
502 if (m == NULL)
503 return 0;
504 } else {
505 m = *m0 ; /* pass the original to dummynet */
506 *m0 = NULL ; /* and nothing back to the caller */
507 }
508 /*
509 * Prepend the header, optimize for the common case of
510 * eh pointing into the mbuf.
511 */
512 if ( (void *)(eh + 1) == (void *)m->m_data) {
513 m->m_data -= ETHER_HDR_LEN ;
514 m->m_len += ETHER_HDR_LEN ;
515 m->m_pkthdr.len += ETHER_HDR_LEN ;
516 } else {
74f1caca 517 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
984263bc
MD
518 if (m == NULL) /* nope... */
519 return 0;
520 bcopy(&save_eh, mtod(m, struct ether_header *),
521 ETHER_HDR_LEN);
522 }
523 ip_dn_io_ptr(m, (i & 0xffff),
524 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
525 return 0;
526 }
527 /*
528 * XXX at some point add support for divert/forward actions.
529 * If none of the above matches, we have to drop the pkt.
530 */
531 return 0;
532}
533
3013ac0e
JS
534/*
535 * XXX merge this function with ether_input.
536 */
537static void
538ether_input_internal(struct ifnet *ifp, struct mbuf *m)
539{
540 ether_input(ifp, NULL, m);
541}
542
984263bc
MD
543/*
544 * Process a received Ethernet packet. We have two different interfaces:
545 * one (conventional) assumes the packet in the mbuf, with the ethernet
546 * header provided separately in *eh. The second one (new) has everything
547 * in the mbuf, and we can tell it because eh == NULL.
548 * The caller MUST MAKE SURE that there are at least
549 * sizeof(struct ether_header) bytes in the first mbuf.
550 *
551 * This allows us to concentrate in one place a bunch of code which
552 * is replicated in all device drivers. Also, many functions called
553 * from ether_input() try to put the eh back into the mbuf, so we
554 * can later propagate the 'contiguous packet' interface to them,
555 * and handle the old interface just here.
556 *
557 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
558 * cluster, right before m_data. So be very careful when working on m,
559 * as you could destroy *eh !!
560 *
561 * First we perform any link layer operations, then continue
562 * to the upper layers with ether_demux().
563 */
564void
565ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
566{
567 struct ether_header save_eh;
568
569 if (eh == NULL) {
570 if (m->m_len < sizeof(struct ether_header)) {
571 /* XXX error in the caller. */
572 m_freem(m);
573 return;
574 }
575 m->m_pkthdr.rcvif = ifp;
576 eh = mtod(m, struct ether_header *);
3013ac0e
JS
577 m_adj(m, sizeof(struct ether_header));
578 /* XXX */
579 /* m->m_pkthdr.len = m->m_len; */
984263bc
MD
580 }
581
582 /* Check for a BPF tap */
583 if (ifp->if_bpf != NULL) {
584 struct m_hdr mh;
585
586 /* This kludge is OK; BPF treats the "mbuf" as read-only */
587 mh.mh_next = m;
588 mh.mh_data = (char *)eh;
589 mh.mh_len = ETHER_HDR_LEN;
590 bpf_mtap(ifp, (struct mbuf *)&mh);
591 }
592
593 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
594
595 /* Handle ng_ether(4) processing, if any */
596 if (ng_ether_input_p != NULL) {
597 (*ng_ether_input_p)(ifp, &m, eh);
598 if (m == NULL)
599 return;
600 }
601
602 /* Check for bridging mode */
603 if (BDG_ACTIVE(ifp) ) {
604 struct ifnet *bif;
605
606 /* Check with bridging code */
607 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
608 m_freem(m);
609 return;
610 }
611 if (bif != BDG_LOCAL) {
984263bc
MD
612 save_eh = *eh ; /* because it might change */
613 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
614 /*
615 * Do not continue if bdg_forward_ptr() processed our
616 * packet (and cleared the mbuf pointer m) or if
617 * it dropped (m_free'd) the packet itself.
618 */
619 if (m == NULL) {
620 if (bif == BDG_BCAST || bif == BDG_MCAST)
621 printf("bdg_forward drop MULTICAST PKT\n");
622 return;
623 }
624 eh = &save_eh ;
625 }
626 if (bif == BDG_LOCAL
627 || bif == BDG_BCAST
628 || bif == BDG_MCAST)
629 goto recvLocal; /* receive locally */
630
631 /* If not local and not multicast, just drop it */
632 if (m != NULL)
633 m_freem(m);
634 return;
635 }
636
637recvLocal:
638 /* Continue with upper layer processing */
639 ether_demux(ifp, eh, m);
640}
641
642/*
643 * Upper layer processing for a received Ethernet packet.
644 */
645void
646ether_demux(ifp, eh, m)
647 struct ifnet *ifp;
648 struct ether_header *eh;
649 struct mbuf *m;
650{
8bde602d 651 int isr;
984263bc
MD
652 u_short ether_type;
653#if defined(NETATALK)
82ed7fc2 654 struct llc *l;
984263bc
MD
655#endif
656 struct ip_fw *rule = NULL;
657
658 /* Extract info from dummynet tag, ignore others */
659 for (;m->m_type == MT_TAG; m = m->m_next)
660 if (m->m_flags == PACKET_TAG_DUMMYNET) {
661 rule = ((struct dn_pkt *)m)->rule;
662 ifp = m->m_next->m_pkthdr.rcvif;
663 }
664
665 if (rule) /* packet was already bridged */
666 goto post_stats;
667
668 if (! (BDG_ACTIVE(ifp) ) )
669 /* Discard packet if upper layers shouldn't see it because it was
670 unicast to a different Ethernet address. If the driver is working
671 properly, then this situation can only happen when the interface
672 is in promiscuous mode. */
673 if ((ifp->if_flags & IFF_PROMISC) != 0
674 && (eh->ether_dhost[0] & 1) == 0
675 && bcmp(eh->ether_dhost,
676 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
46f25451 677 && (ifp->if_flags & IFF_PPROMISC) == 0) {
984263bc
MD
678 m_freem(m);
679 return;
680 }
681
682 /* Discard packet if interface is not up */
683 if ((ifp->if_flags & IFF_UP) == 0) {
684 m_freem(m);
685 return;
686 }
687 if (eh->ether_dhost[0] & 1) {
c401f0fd
JS
688 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
689 ifp->if_addrlen) == 0)
984263bc
MD
690 m->m_flags |= M_BCAST;
691 else
692 m->m_flags |= M_MCAST;
693 }
694 if (m->m_flags & (M_BCAST|M_MCAST))
695 ifp->if_imcasts++;
696
697post_stats:
698 if (IPFW_LOADED && ether_ipfw != 0) {
699 if (ether_ipfw_chk(&m, NULL, &rule, eh, 0 ) == 0) {
700 if (m)
701 m_freem(m);
702 return;
703 }
704 }
705
706 ether_type = ntohs(eh->ether_type);
707
708 switch (ether_type) {
709#ifdef INET
710 case ETHERTYPE_IP:
711 if (ipflow_fastforward(m))
712 return;
8bde602d 713 isr = NETISR_IP;
984263bc
MD
714 break;
715
716 case ETHERTYPE_ARP:
717 if (ifp->if_flags & IFF_NOARP) {
718 /* Discard packet if ARP is disabled on interface */
719 m_freem(m);
720 return;
721 }
8bde602d 722 isr = NETISR_ARP;
984263bc
MD
723 break;
724#endif
725#ifdef IPX
726 case ETHERTYPE_IPX:
727 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
728 return;
8bde602d 729 isr = NETISR_IPX;
984263bc
MD
730 break;
731#endif
732#ifdef INET6
733 case ETHERTYPE_IPV6:
8bde602d 734 isr = NETISR_IPV6;
984263bc
MD
735 break;
736#endif
737#ifdef NS
738 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
8bde602d 739 isr = NETISR_NS;
984263bc
MD
740 break;
741
742#endif /* NS */
743#ifdef NETATALK
744 case ETHERTYPE_AT:
8bde602d 745 isr = NETISR_ATALK1;
984263bc
MD
746 break;
747 case ETHERTYPE_AARP:
8bde602d
JH
748 isr = NETISR_AARP;
749 break;
984263bc
MD
750#endif /* NETATALK */
751 case ETHERTYPE_VLAN:
752 /* XXX lock ? */
753 if (vlan_input_p != NULL)
754 (*vlan_input_p)(eh, m);
755 else {
756 m->m_pkthdr.rcvif->if_noproto++;
757 m_freem(m);
758 }
759 /* XXX unlock ? */
760 return;
761 default:
762#ifdef IPX
763 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
764 return;
765#endif /* IPX */
766#ifdef NS
767 checksum = mtod(m, ushort *);
768 /* Novell 802.3 */
769 if ((ether_type <= ETHERMTU) &&
8bde602d
JH
770 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
771 if (*checksum == 0xE0E0) {
984263bc
MD
772 m->m_pkthdr.len -= 3;
773 m->m_len -= 3;
774 m->m_data += 3;
775 }
8bde602d
JH
776 isr = NETISR_NS;
777 break;
984263bc
MD
778 }
779#endif /* NS */
8bde602d 780#ifdef NETATALK
984263bc
MD
781 if (ether_type > ETHERMTU)
782 goto dropanyway;
783 l = mtod(m, struct llc *);
8bde602d
JH
784 if (l->llc_dsap == LLC_SNAP_LSAP &&
785 l->llc_ssap == LLC_SNAP_LSAP &&
786 l->llc_control == LLC_UI) {
2e9572df 787 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
8bde602d
JH
788 sizeof(at_org_code)) == 0 &&
789 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
790 m_adj(m, sizeof(struct llc));
791 isr = NETISR_ATALK2;
792 break;
984263bc 793 }
2e9572df 794 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
8bde602d
JH
795 sizeof(aarp_org_code)) == 0 &&
796 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
797 m_adj(m, sizeof(struct llc));
798 isr = NETISR_AARP;
799 break;
984263bc 800 }
984263bc 801 }
8bde602d
JH
802dropanyway:
803#endif /* NETATALK */
984263bc
MD
804 if (ng_ether_input_orphan_p != NULL)
805 (*ng_ether_input_orphan_p)(ifp, m, eh);
806 else
807 m_freem(m);
808 return;
984263bc 809 }
8bde602d 810 netisr_dispatch(isr, m);
984263bc
MD
811}
812
813/*
814 * Perform common duties while attaching to interface list
815 */
c0f6c904 816
984263bc 817void
0a8b5977 818ether_ifattach(struct ifnet *ifp, uint8_t *lla)
c0f6c904
JS
819{
820 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header));
821}
822
823void
824ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen)
984263bc 825{
82ed7fc2
RG
826 struct ifaddr *ifa;
827 struct sockaddr_dl *sdl;
984263bc 828
3013ac0e
JS
829 ifp->if_output = ether_output;
830 ifp->if_input = ether_input_internal;
984263bc 831 ifp->if_type = IFT_ETHER;
c401f0fd
JS
832 ifp->if_addrlen = ETHER_ADDR_LEN;
833 ifp->if_broadcastaddr = etherbroadcastaddr;
984263bc
MD
834 ifp->if_hdrlen = 14;
835 if_attach(ifp);
836 ifp->if_mtu = ETHERMTU;
837 ifp->if_resolvemulti = ether_resolvemulti;
838 if (ifp->if_baudrate == 0)
839 ifp->if_baudrate = 10000000;
840 ifa = ifnet_addrs[ifp->if_index - 1];
841 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
842 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
843 sdl->sdl_type = IFT_ETHER;
844 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 845 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
846 /*
847 * XXX Keep the current drivers happy.
848 * XXX Remove once all drivers have been cleaned up
849 */
850 if (lla != IFP2AC(ifp)->ac_enaddr)
851 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 852 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
853 if (ng_ether_attach_p != NULL)
854 (*ng_ether_attach_p)(ifp);
855 if (BDG_LOADED)
856 bdgtakeifaces_ptr();
267caeeb
JS
857
858 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
859}
860
861/*
862 * Perform common duties while detaching an Ethernet interface
863 */
864void
0a8b5977 865ether_ifdetach(struct ifnet *ifp)
984263bc 866{
45b8be9e
JS
867 int s;
868
869 s = splnet();
870 if_down(ifp);
871 splx(s);
872
984263bc
MD
873 if (ng_ether_detach_p != NULL)
874 (*ng_ether_detach_p)(ifp);
0a8b5977 875 bpfdetach(ifp);
984263bc
MD
876 if_detach(ifp);
877 if (BDG_LOADED)
878 bdgtakeifaces_ptr();
879}
880
881SYSCTL_DECL(_net_link);
882SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
883SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
884 &ether_ipfw,0,"Pass ether pkts through firewall");
885
886int
887ether_ioctl(ifp, command, data)
888 struct ifnet *ifp;
889 int command;
890 caddr_t data;
891{
892 struct ifaddr *ifa = (struct ifaddr *) data;
893 struct ifreq *ifr = (struct ifreq *) data;
894 int error = 0;
895
896 switch (command) {
897 case SIOCSIFADDR:
898 ifp->if_flags |= IFF_UP;
899
900 switch (ifa->ifa_addr->sa_family) {
901#ifdef INET
902 case AF_INET:
903 ifp->if_init(ifp->if_softc); /* before arpwhohas */
904 arp_ifinit(ifp, ifa);
905 break;
906#endif
907#ifdef IPX
908 /*
909 * XXX - This code is probably wrong
910 */
911 case AF_IPX:
912 {
82ed7fc2 913 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
984263bc
MD
914 struct arpcom *ac = IFP2AC(ifp);
915
916 if (ipx_nullhost(*ina))
917 ina->x_host =
918 *(union ipx_host *)
919 ac->ac_enaddr;
920 else {
921 bcopy((caddr_t) ina->x_host.c_host,
922 (caddr_t) ac->ac_enaddr,
923 sizeof(ac->ac_enaddr));
924 }
925
926 /*
927 * Set new address
928 */
929 ifp->if_init(ifp->if_softc);
930 break;
931 }
932#endif
933#ifdef NS
934 /*
935 * XXX - This code is probably wrong
936 */
937 case AF_NS:
938 {
82ed7fc2 939 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984263bc
MD
940 struct arpcom *ac = IFP2AC(ifp);
941
942 if (ns_nullhost(*ina))
943 ina->x_host =
944 *(union ns_host *) (ac->ac_enaddr);
945 else {
946 bcopy((caddr_t) ina->x_host.c_host,
947 (caddr_t) ac->ac_enaddr,
948 sizeof(ac->ac_enaddr));
949 }
950
951 /*
952 * Set new address
953 */
954 ifp->if_init(ifp->if_softc);
955 break;
956 }
957#endif
958 default:
959 ifp->if_init(ifp->if_softc);
960 break;
961 }
962 break;
963
964 case SIOCGIFADDR:
965 {
966 struct sockaddr *sa;
967
968 sa = (struct sockaddr *) & ifr->ifr_data;
969 bcopy(IFP2AC(ifp)->ac_enaddr,
970 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
971 }
972 break;
973
974 case SIOCSIFMTU:
975 /*
976 * Set the interface MTU.
977 */
978 if (ifr->ifr_mtu > ETHERMTU) {
979 error = EINVAL;
980 } else {
981 ifp->if_mtu = ifr->ifr_mtu;
982 }
983 break;
c2d9fd91
JS
984 default:
985 error = EINVAL;
986 break;
984263bc
MD
987 }
988 return (error);
989}
990
991int
992ether_resolvemulti(ifp, llsa, sa)
993 struct ifnet *ifp;
994 struct sockaddr **llsa;
995 struct sockaddr *sa;
996{
997 struct sockaddr_dl *sdl;
998 struct sockaddr_in *sin;
999#ifdef INET6
1000 struct sockaddr_in6 *sin6;
1001#endif
1002 u_char *e_addr;
1003
1004 switch(sa->sa_family) {
1005 case AF_LINK:
1006 /*
1007 * No mapping needed. Just check that it's a valid MC address.
1008 */
1009 sdl = (struct sockaddr_dl *)sa;
1010 e_addr = LLADDR(sdl);
1011 if ((e_addr[0] & 1) != 1)
1012 return EADDRNOTAVAIL;
1013 *llsa = 0;
1014 return 0;
1015
1016#ifdef INET
1017 case AF_INET:
1018 sin = (struct sockaddr_in *)sa;
1019 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1020 return EADDRNOTAVAIL;
1021 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1022 M_WAITOK|M_ZERO);
1023 sdl->sdl_len = sizeof *sdl;
1024 sdl->sdl_family = AF_LINK;
1025 sdl->sdl_index = ifp->if_index;
1026 sdl->sdl_type = IFT_ETHER;
1027 sdl->sdl_alen = ETHER_ADDR_LEN;
1028 e_addr = LLADDR(sdl);
1029 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1030 *llsa = (struct sockaddr *)sdl;
1031 return 0;
1032#endif
1033#ifdef INET6
1034 case AF_INET6:
1035 sin6 = (struct sockaddr_in6 *)sa;
1036 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1037 /*
1038 * An IP6 address of 0 means listen to all
1039 * of the Ethernet multicast address used for IP6.
1040 * (This is used for multicast routers.)
1041 */
1042 ifp->if_flags |= IFF_ALLMULTI;
1043 *llsa = 0;
1044 return 0;
1045 }
1046 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1047 return EADDRNOTAVAIL;
1048 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1049 M_WAITOK|M_ZERO);
1050 sdl->sdl_len = sizeof *sdl;
1051 sdl->sdl_family = AF_LINK;
1052 sdl->sdl_index = ifp->if_index;
1053 sdl->sdl_type = IFT_ETHER;
1054 sdl->sdl_alen = ETHER_ADDR_LEN;
1055 e_addr = LLADDR(sdl);
1056 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1057 *llsa = (struct sockaddr *)sdl;
1058 return 0;
1059#endif
1060
1061 default:
1062 /*
1063 * Well, the text isn't quite right, but it's the name
1064 * that counts...
1065 */
1066 return EAFNOSUPPORT;
1067 }
1068}
d6018c31
JS
1069
1070#if 0
1071/*
1072 * This is for reference. We have a table-driven version
1073 * of the little-endian crc32 generator, which is faster
1074 * than the double-loop.
1075 */
1076uint32_t
1077ether_crc32_le(const uint8_t *buf, size_t len)
1078{
1079 uint32_t c, crc, carry;
1080 size_t i, j;
1081
1082 crc = 0xffffffffU; /* initial value */
1083
1084 for (i = 0; i < len; i++) {
1085 c = buf[i];
1086 for (j = 0; j < 8; j++) {
1087 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1088 crc >>= 1;
1089 c >>= 1;
1090 if (carry)
1091 crc = (crc ^ ETHER_CRC_POLY_LE);
1092 }
1093 }
1094
1095 return(crc);
1096}
1097#else
1098uint32_t
1099ether_crc32_le(const uint8_t *buf, size_t len)
1100{
1101 static const uint32_t crctab[] = {
1102 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1103 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1104 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1105 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1106 };
1107 uint32_t crc;
1108 size_t i;
1109
1110 crc = 0xffffffffU; /* initial value */
1111
1112 for (i = 0; i < len; i++) {
1113 crc ^= buf[i];
1114 crc = (crc >> 4) ^ crctab[crc & 0xf];
1115 crc = (crc >> 4) ^ crctab[crc & 0xf];
1116 }
1117
1118 return(crc);
1119}
1120#endif
1121
1122uint32_t
1123ether_crc32_be(const uint8_t *buf, size_t len)
1124{
1125 uint32_t c, crc, carry;
1126 size_t i, j;
1127
1128 crc = 0xffffffffU; /* initial value */
1129
1130 for (i = 0; i < len; i++) {
1131 c = buf[i];
1132 for (j = 0; j < 8; j++) {
1133 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1134 crc <<= 1;
1135 c >>= 1;
1136 if (carry)
1137 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1138 }
1139 }
1140
1141 return(crc);
1142}