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