]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_ethersubr.c
Remove AppleTalk support.
[FreeBSD/FreeBSD.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)if_ethersubr.c      8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_netgraph.h"
36 #include "opt_mbuf_profiling.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mbuf.h>
45 #include <sys/random.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
49 #include <sys/uuid.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/if_bridgevar.h>
62 #include <net/if_vlan_var.h>
63 #include <net/if_llatbl.h>
64 #include <net/pfil.h>
65 #include <net/vnet.h>
66
67 #include <netpfil/pf/pf_mtag.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 <netinet/ip_carp.h>
74 #include <netinet/ip_var.h>
75 #endif
76 #ifdef INET6
77 #include <netinet6/nd6.h>
78 #endif
79
80 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
81 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
82                 const struct sockaddr *dst, short *tp, int *hlen);
83
84 #include <security/mac/mac_framework.h>
85
86 #ifdef CTASSERT
87 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
88 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
89 #endif
90
91 VNET_DEFINE(struct pfil_head, link_pfil_hook);  /* Packet filter hooks */
92
93 /* netgraph node hooks for ng_ether(4) */
94 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
95 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
96 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
97 void    (*ng_ether_attach_p)(struct ifnet *ifp);
98 void    (*ng_ether_detach_p)(struct ifnet *ifp);
99
100 void    (*vlan_input_p)(struct ifnet *, struct mbuf *);
101
102 /* if_bridge(4) support */
103 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 
104 int     (*bridge_output_p)(struct ifnet *, struct mbuf *, 
105                 struct sockaddr *, struct rtentry *);
106 void    (*bridge_dn_p)(struct mbuf *, struct ifnet *);
107
108 /* if_lagg(4) support */
109 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *); 
110
111 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
112                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
113
114 static  int ether_resolvemulti(struct ifnet *, struct sockaddr **,
115                 struct sockaddr *);
116 #ifdef VIMAGE
117 static  void ether_reassign(struct ifnet *, struct vnet *, char *);
118 #endif
119
120 /* XXX: should be in an arp support file, not here */
121 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
122
123 #define ETHER_IS_BROADCAST(addr) \
124         (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
125
126 #define senderr(e) do { error = (e); goto bad;} while (0)
127
128 static void
129 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
130 {
131         int csum_flags = 0;
132
133         if (src->m_pkthdr.csum_flags & CSUM_IP)
134                 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
135         if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
136                 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
137         if (src->m_pkthdr.csum_flags & CSUM_SCTP)
138                 csum_flags |= CSUM_SCTP_VALID;
139         dst->m_pkthdr.csum_flags |= csum_flags;
140         if (csum_flags & CSUM_DATA_VALID)
141                 dst->m_pkthdr.csum_data = 0xffff;
142 }
143
144 /*
145  * Ethernet output routine.
146  * Encapsulate a packet of type family for the local net.
147  * Use trailer local net encapsulation if enough data in first
148  * packet leaves a multiple of 512 bytes of data in remainder.
149  */
150 int
151 ether_output(struct ifnet *ifp, struct mbuf *m,
152         const struct sockaddr *dst, struct route *ro)
153 {
154         short type;
155         int error = 0, hdrcmplt = 0;
156         u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
157         struct llentry *lle = NULL;
158         struct rtentry *rt0 = NULL;
159         struct ether_header *eh;
160         struct pf_mtag *t;
161         int loop_copy = 1;
162         int hlen;       /* link layer header length */
163
164         if (ro != NULL) {
165                 if (!(m->m_flags & (M_BCAST | M_MCAST)))
166                         lle = ro->ro_lle;
167                 rt0 = ro->ro_rt;
168         }
169 #ifdef MAC
170         error = mac_ifnet_check_transmit(ifp, m);
171         if (error)
172                 senderr(error);
173 #endif
174
175         M_PROFILE(m);
176         if (ifp->if_flags & IFF_MONITOR)
177                 senderr(ENETDOWN);
178         if (!((ifp->if_flags & IFF_UP) &&
179             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
180                 senderr(ENETDOWN);
181
182         hlen = ETHER_HDR_LEN;
183         switch (dst->sa_family) {
184 #ifdef INET
185         case AF_INET:
186                 if (lle != NULL && (lle->la_flags & LLE_VALID))
187                         memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
188                 else
189                         error = arpresolve(ifp, rt0, m, dst, edst, &lle);
190                 if (error)
191                         return (error == EWOULDBLOCK ? 0 : error);
192                 type = htons(ETHERTYPE_IP);
193                 break;
194         case AF_ARP:
195         {
196                 struct arphdr *ah;
197                 ah = mtod(m, struct arphdr *);
198                 ah->ar_hrd = htons(ARPHRD_ETHER);
199
200                 loop_copy = 0; /* if this is for us, don't do it */
201
202                 switch(ntohs(ah->ar_op)) {
203                 case ARPOP_REVREQUEST:
204                 case ARPOP_REVREPLY:
205                         type = htons(ETHERTYPE_REVARP);
206                         break;
207                 case ARPOP_REQUEST:
208                 case ARPOP_REPLY:
209                 default:
210                         type = htons(ETHERTYPE_ARP);
211                         break;
212                 }
213
214                 if (m->m_flags & M_BCAST)
215                         bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
216                 else
217                         bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
218
219         }
220         break;
221 #endif
222 #ifdef INET6
223         case AF_INET6:
224                 if (lle != NULL && (lle->la_flags & LLE_VALID))
225                         memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
226                 else
227                         error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
228                 if (error)
229                         return error;
230                 type = htons(ETHERTYPE_IPV6);
231                 break;
232 #endif
233         case pseudo_AF_HDRCMPLT:
234             {
235                 const struct ether_header *eh;
236                 
237                 hdrcmplt = 1;
238                 eh = (const struct ether_header *)dst->sa_data;
239                 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
240                 /* FALLTHROUGH */
241
242         case AF_UNSPEC:
243                 loop_copy = 0; /* if this is for us, don't do it */
244                 eh = (const struct ether_header *)dst->sa_data;
245                 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
246                 type = eh->ether_type;
247                 break;
248             }
249         default:
250                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
251                 senderr(EAFNOSUPPORT);
252         }
253
254         if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
255                 update_mbuf_csumflags(m, m);
256                 return (if_simloop(ifp, m, dst->sa_family, 0));
257         }
258
259         /*
260          * Add local net header.  If no space in first mbuf,
261          * allocate another.
262          */
263         M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
264         if (m == NULL)
265                 senderr(ENOBUFS);
266         eh = mtod(m, struct ether_header *);
267         (void)memcpy(&eh->ether_type, &type,
268                 sizeof(eh->ether_type));
269         (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
270         if (hdrcmplt)
271                 (void)memcpy(eh->ether_shost, esrc,
272                         sizeof(eh->ether_shost));
273         else
274                 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
275                         sizeof(eh->ether_shost));
276
277         /*
278          * If a simplex interface, and the packet is being sent to our
279          * Ethernet address or a broadcast address, loopback a copy.
280          * XXX To make a simplex device behave exactly like a duplex
281          * device, we should copy in the case of sending to our own
282          * ethernet address (thus letting the original actually appear
283          * on the wire). However, we don't do that here for security
284          * reasons and compatibility with the original behavior.
285          */
286         if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
287             ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
288                 if (m->m_flags & M_BCAST) {
289                         struct mbuf *n;
290
291                         /*
292                          * Because if_simloop() modifies the packet, we need a
293                          * writable copy through m_dup() instead of a readonly
294                          * one as m_copy[m] would give us. The alternative would
295                          * be to modify if_simloop() to handle the readonly mbuf,
296                          * but performancewise it is mostly equivalent (trading
297                          * extra data copying vs. extra locking).
298                          *
299                          * XXX This is a local workaround.  A number of less
300                          * often used kernel parts suffer from the same bug.
301                          * See PR kern/105943 for a proposed general solution.
302                          */
303                         if ((n = m_dup(m, M_NOWAIT)) != NULL) {
304                                 update_mbuf_csumflags(m, n);
305                                 (void)if_simloop(ifp, n, dst->sa_family, hlen);
306                         } else
307                                 ifp->if_iqdrops++;
308                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
309                                 ETHER_ADDR_LEN) == 0) {
310                         update_mbuf_csumflags(m, m);
311                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
312                         return (0);     /* XXX */
313                 }
314         }
315
316        /*
317         * Bridges require special output handling.
318         */
319         if (ifp->if_bridge) {
320                 BRIDGE_OUTPUT(ifp, m, error);
321                 return (error);
322         }
323
324 #if defined(INET) || defined(INET6)
325         if (ifp->if_carp &&
326             (error = (*carp_output_p)(ifp, m, dst)))
327                 goto bad;
328 #endif
329
330         /* Handle ng_ether(4) processing, if any */
331         if (IFP2AC(ifp)->ac_netgraph != NULL) {
332                 KASSERT(ng_ether_output_p != NULL,
333                     ("ng_ether_output_p is NULL"));
334                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
335 bad:                    if (m != NULL)
336                                 m_freem(m);
337                         return (error);
338                 }
339                 if (m == NULL)
340                         return (0);
341         }
342
343         /* Continue with link-layer output */
344         return ether_output_frame(ifp, m);
345 }
346
347 /*
348  * Ethernet link layer output routine to send a raw frame to the device.
349  *
350  * This assumes that the 14 byte Ethernet header is present and contiguous
351  * in the first mbuf (if BRIDGE'ing).
352  */
353 int
354 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
355 {
356         int i;
357
358         if (PFIL_HOOKED(&V_link_pfil_hook)) {
359                 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
360
361                 if (i != 0)
362                         return (EACCES);
363
364                 if (m == NULL)
365                         return (0);
366         }
367
368         /*
369          * Queue message on interface, update output statistics if
370          * successful, and start output if interface not yet active.
371          */
372         return ((ifp->if_transmit)(ifp, m));
373 }
374
375 #if defined(INET) || defined(INET6)
376 #endif
377
378 /*
379  * Process a received Ethernet packet; the packet is in the
380  * mbuf chain m with the ethernet header at the front.
381  */
382 static void
383 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
384 {
385         struct ether_header *eh;
386         u_short etype;
387
388         if ((ifp->if_flags & IFF_UP) == 0) {
389                 m_freem(m);
390                 return;
391         }
392 #ifdef DIAGNOSTIC
393         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
394                 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
395                 m_freem(m);
396                 return;
397         }
398 #endif
399         /*
400          * Do consistency checks to verify assumptions
401          * made by code past this point.
402          */
403         if ((m->m_flags & M_PKTHDR) == 0) {
404                 if_printf(ifp, "discard frame w/o packet header\n");
405                 ifp->if_ierrors++;
406                 m_freem(m);
407                 return;
408         }
409         if (m->m_len < ETHER_HDR_LEN) {
410                 /* XXX maybe should pullup? */
411                 if_printf(ifp, "discard frame w/o leading ethernet "
412                                 "header (len %u pkt len %u)\n",
413                                 m->m_len, m->m_pkthdr.len);
414                 ifp->if_ierrors++;
415                 m_freem(m);
416                 return;
417         }
418         eh = mtod(m, struct ether_header *);
419         etype = ntohs(eh->ether_type);
420         if (m->m_pkthdr.rcvif == NULL) {
421                 if_printf(ifp, "discard frame w/o interface pointer\n");
422                 ifp->if_ierrors++;
423                 m_freem(m);
424                 return;
425         }
426 #ifdef DIAGNOSTIC
427         if (m->m_pkthdr.rcvif != ifp) {
428                 if_printf(ifp, "Warning, frame marked as received on %s\n",
429                         m->m_pkthdr.rcvif->if_xname);
430         }
431 #endif
432
433         CURVNET_SET_QUIET(ifp->if_vnet);
434
435         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
436                 if (ETHER_IS_BROADCAST(eh->ether_dhost))
437                         m->m_flags |= M_BCAST;
438                 else
439                         m->m_flags |= M_MCAST;
440                 ifp->if_imcasts++;
441         }
442
443 #ifdef MAC
444         /*
445          * Tag the mbuf with an appropriate MAC label before any other
446          * consumers can get to it.
447          */
448         mac_ifnet_create_mbuf(ifp, m);
449 #endif
450
451         /*
452          * Give bpf a chance at the packet.
453          */
454         ETHER_BPF_MTAP(ifp, m);
455
456         /*
457          * If the CRC is still on the packet, trim it off. We do this once
458          * and once only in case we are re-entered. Nothing else on the
459          * Ethernet receive path expects to see the FCS.
460          */
461         if (m->m_flags & M_HASFCS) {
462                 m_adj(m, -ETHER_CRC_LEN);
463                 m->m_flags &= ~M_HASFCS;
464         }
465
466         if (!(ifp->if_capenable & IFCAP_HWSTATS))
467                 ifp->if_ibytes += m->m_pkthdr.len;
468
469         /* Allow monitor mode to claim this frame, after stats are updated. */
470         if (ifp->if_flags & IFF_MONITOR) {
471                 m_freem(m);
472                 CURVNET_RESTORE();
473                 return;
474         }
475
476         /* Handle input from a lagg(4) port */
477         if (ifp->if_type == IFT_IEEE8023ADLAG) {
478                 KASSERT(lagg_input_p != NULL,
479                     ("%s: if_lagg not loaded!", __func__));
480                 m = (*lagg_input_p)(ifp, m);
481                 if (m != NULL)
482                         ifp = m->m_pkthdr.rcvif;
483                 else {
484                         CURVNET_RESTORE();
485                         return;
486                 }
487         }
488
489         /*
490          * If the hardware did not process an 802.1Q tag, do this now,
491          * to allow 802.1P priority frames to be passed to the main input
492          * path correctly.
493          * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
494          */
495         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
496                 struct ether_vlan_header *evl;
497
498                 if (m->m_len < sizeof(*evl) &&
499                     (m = m_pullup(m, sizeof(*evl))) == NULL) {
500 #ifdef DIAGNOSTIC
501                         if_printf(ifp, "cannot pullup VLAN header\n");
502 #endif
503                         ifp->if_ierrors++;
504                         m_freem(m);
505                         CURVNET_RESTORE();
506                         return;
507                 }
508
509                 evl = mtod(m, struct ether_vlan_header *);
510                 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
511                 m->m_flags |= M_VLANTAG;
512
513                 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
514                     ETHER_HDR_LEN - ETHER_TYPE_LEN);
515                 m_adj(m, ETHER_VLAN_ENCAP_LEN);
516                 eh = mtod(m, struct ether_header *);
517         }
518
519         M_SETFIB(m, ifp->if_fib);
520
521         /* Allow ng_ether(4) to claim this frame. */
522         if (IFP2AC(ifp)->ac_netgraph != NULL) {
523                 KASSERT(ng_ether_input_p != NULL,
524                     ("%s: ng_ether_input_p is NULL", __func__));
525                 m->m_flags &= ~M_PROMISC;
526                 (*ng_ether_input_p)(ifp, &m);
527                 if (m == NULL) {
528                         CURVNET_RESTORE();
529                         return;
530                 }
531                 eh = mtod(m, struct ether_header *);
532         }
533
534         /*
535          * Allow if_bridge(4) to claim this frame.
536          * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
537          * and the frame should be delivered locally.
538          */
539         if (ifp->if_bridge != NULL) {
540                 m->m_flags &= ~M_PROMISC;
541                 BRIDGE_INPUT(ifp, m);
542                 if (m == NULL) {
543                         CURVNET_RESTORE();
544                         return;
545                 }
546                 eh = mtod(m, struct ether_header *);
547         }
548
549 #if defined(INET) || defined(INET6)
550         /*
551          * Clear M_PROMISC on frame so that carp(4) will see it when the
552          * mbuf flows up to Layer 3.
553          * FreeBSD's implementation of carp(4) uses the inprotosw
554          * to dispatch IPPROTO_CARP. carp(4) also allocates its own
555          * Ethernet addresses of the form 00:00:5e:00:01:xx, which
556          * is outside the scope of the M_PROMISC test below.
557          * TODO: Maintain a hash table of ethernet addresses other than
558          * ether_dhost which may be active on this ifp.
559          */
560         if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
561                 m->m_flags &= ~M_PROMISC;
562         } else
563 #endif
564         {
565                 /*
566                  * If the frame received was not for our MAC address, set the
567                  * M_PROMISC flag on the mbuf chain. The frame may need to
568                  * be seen by the rest of the Ethernet input path in case of
569                  * re-entry (e.g. bridge, vlan, netgraph) but should not be
570                  * seen by upper protocol layers.
571                  */
572                 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
573                     bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
574                         m->m_flags |= M_PROMISC;
575         }
576
577         if (harvest.ethernet)
578                 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
579
580         ether_demux(ifp, m);
581         CURVNET_RESTORE();
582 }
583
584 /*
585  * Ethernet input dispatch; by default, direct dispatch here regardless of
586  * global configuration.
587  */
588 static void
589 ether_nh_input(struct mbuf *m)
590 {
591
592         ether_input_internal(m->m_pkthdr.rcvif, m);
593 }
594
595 static struct netisr_handler    ether_nh = {
596         .nh_name = "ether",
597         .nh_handler = ether_nh_input,
598         .nh_proto = NETISR_ETHER,
599         .nh_policy = NETISR_POLICY_SOURCE,
600         .nh_dispatch = NETISR_DISPATCH_DIRECT,
601 };
602
603 static void
604 ether_init(__unused void *arg)
605 {
606
607         netisr_register(&ether_nh);
608 }
609 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
610
611 static void
612 vnet_ether_init(__unused void *arg)
613 {
614         int i;
615
616         /* Initialize packet filter hooks. */
617         V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
618         V_link_pfil_hook.ph_af = AF_LINK;
619         if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
620                 printf("%s: WARNING: unable to register pfil link hook, "
621                         "error %d\n", __func__, i);
622 }
623 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
624     vnet_ether_init, NULL);
625  
626 static void
627 vnet_ether_destroy(__unused void *arg)
628 {
629         int i;
630
631         if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
632                 printf("%s: WARNING: unable to unregister pfil link hook, "
633                         "error %d\n", __func__, i);
634 }
635 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
636     vnet_ether_destroy, NULL);
637
638
639
640 static void
641 ether_input(struct ifnet *ifp, struct mbuf *m)
642 {
643
644         struct mbuf *mn;
645
646         /*
647          * The drivers are allowed to pass in a chain of packets linked with
648          * m_nextpkt. We split them up into separate packets here and pass
649          * them up. This allows the drivers to amortize the receive lock.
650          */
651         while (m) {
652                 mn = m->m_nextpkt;
653                 m->m_nextpkt = NULL;
654
655                 /*
656                  * We will rely on rcvif being set properly in the deferred context,
657                  * so assert it is correct here.
658                  */
659                 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
660                 netisr_dispatch(NETISR_ETHER, m);
661                 m = mn;
662         }
663 }
664
665 /*
666  * Upper layer processing for a received Ethernet packet.
667  */
668 void
669 ether_demux(struct ifnet *ifp, struct mbuf *m)
670 {
671         struct ether_header *eh;
672         int i, isr;
673         u_short ether_type;
674
675         KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
676
677         /* Do not grab PROMISC frames in case we are re-entered. */
678         if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
679                 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
680
681                 if (i != 0 || m == NULL)
682                         return;
683         }
684
685         eh = mtod(m, struct ether_header *);
686         ether_type = ntohs(eh->ether_type);
687
688         /*
689          * If this frame has a VLAN tag other than 0, call vlan_input()
690          * if its module is loaded. Otherwise, drop.
691          */
692         if ((m->m_flags & M_VLANTAG) &&
693             EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
694                 if (ifp->if_vlantrunk == NULL) {
695                         ifp->if_noproto++;
696                         m_freem(m);
697                         return;
698                 }
699                 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
700                     __func__));
701                 /* Clear before possibly re-entering ether_input(). */
702                 m->m_flags &= ~M_PROMISC;
703                 (*vlan_input_p)(ifp, m);
704                 return;
705         }
706
707         /*
708          * Pass promiscuously received frames to the upper layer if the user
709          * requested this by setting IFF_PPROMISC. Otherwise, drop them.
710          */
711         if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
712                 m_freem(m);
713                 return;
714         }
715
716         /*
717          * Reset layer specific mbuf flags to avoid confusing upper layers.
718          * Strip off Ethernet header.
719          */
720         m->m_flags &= ~M_VLANTAG;
721         m_clrprotoflags(m);
722         m_adj(m, ETHER_HDR_LEN);
723
724         /*
725          * Dispatch frame to upper layer.
726          */
727         switch (ether_type) {
728 #ifdef INET
729         case ETHERTYPE_IP:
730                 if ((m = ip_fastforward(m)) == NULL)
731                         return;
732                 isr = NETISR_IP;
733                 break;
734
735         case ETHERTYPE_ARP:
736                 if (ifp->if_flags & IFF_NOARP) {
737                         /* Discard packet if ARP is disabled on interface */
738                         m_freem(m);
739                         return;
740                 }
741                 isr = NETISR_ARP;
742                 break;
743 #endif
744 #ifdef INET6
745         case ETHERTYPE_IPV6:
746                 isr = NETISR_IPV6;
747                 break;
748 #endif
749         default:
750                 goto discard;
751         }
752         netisr_dispatch(isr, m);
753         return;
754
755 discard:
756         /*
757          * Packet is to be discarded.  If netgraph is present,
758          * hand the packet to it for last chance processing;
759          * otherwise dispose of it.
760          */
761         if (IFP2AC(ifp)->ac_netgraph != NULL) {
762                 KASSERT(ng_ether_input_orphan_p != NULL,
763                     ("ng_ether_input_orphan_p is NULL"));
764                 /*
765                  * Put back the ethernet header so netgraph has a
766                  * consistent view of inbound packets.
767                  */
768                 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
769                 (*ng_ether_input_orphan_p)(ifp, m);
770                 return;
771         }
772         m_freem(m);
773 }
774
775 /*
776  * Convert Ethernet address to printable (loggable) representation.
777  * This routine is for compatibility; it's better to just use
778  *
779  *      printf("%6D", <pointer to address>, ":");
780  *
781  * since there's no static buffer involved.
782  */
783 char *
784 ether_sprintf(const u_char *ap)
785 {
786         static char etherbuf[18];
787         snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
788         return (etherbuf);
789 }
790
791 /*
792  * Perform common duties while attaching to interface list
793  */
794 void
795 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
796 {
797         int i;
798         struct ifaddr *ifa;
799         struct sockaddr_dl *sdl;
800
801         ifp->if_addrlen = ETHER_ADDR_LEN;
802         ifp->if_hdrlen = ETHER_HDR_LEN;
803         if_attach(ifp);
804         ifp->if_mtu = ETHERMTU;
805         ifp->if_output = ether_output;
806         ifp->if_input = ether_input;
807         ifp->if_resolvemulti = ether_resolvemulti;
808 #ifdef VIMAGE
809         ifp->if_reassign = ether_reassign;
810 #endif
811         if (ifp->if_baudrate == 0)
812                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
813         ifp->if_broadcastaddr = etherbroadcastaddr;
814
815         ifa = ifp->if_addr;
816         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
817         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
818         sdl->sdl_type = IFT_ETHER;
819         sdl->sdl_alen = ifp->if_addrlen;
820         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
821
822         bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
823         if (ng_ether_attach_p != NULL)
824                 (*ng_ether_attach_p)(ifp);
825
826         /* Announce Ethernet MAC address if non-zero. */
827         for (i = 0; i < ifp->if_addrlen; i++)
828                 if (lla[i] != 0)
829                         break; 
830         if (i != ifp->if_addrlen)
831                 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
832
833         uuid_ether_add(LLADDR(sdl));
834 }
835
836 /*
837  * Perform common duties while detaching an Ethernet interface
838  */
839 void
840 ether_ifdetach(struct ifnet *ifp)
841 {
842         struct sockaddr_dl *sdl;
843
844         sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
845         uuid_ether_del(LLADDR(sdl));
846
847         if (IFP2AC(ifp)->ac_netgraph != NULL) {
848                 KASSERT(ng_ether_detach_p != NULL,
849                     ("ng_ether_detach_p is NULL"));
850                 (*ng_ether_detach_p)(ifp);
851         }
852
853         bpfdetach(ifp);
854         if_detach(ifp);
855 }
856
857 #ifdef VIMAGE
858 void
859 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
860 {
861
862         if (IFP2AC(ifp)->ac_netgraph != NULL) {
863                 KASSERT(ng_ether_detach_p != NULL,
864                     ("ng_ether_detach_p is NULL"));
865                 (*ng_ether_detach_p)(ifp);
866         }
867
868         if (ng_ether_attach_p != NULL) {
869                 CURVNET_SET_QUIET(new_vnet);
870                 (*ng_ether_attach_p)(ifp);
871                 CURVNET_RESTORE();
872         }
873 }
874 #endif
875
876 SYSCTL_DECL(_net_link);
877 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
878
879 #if 0
880 /*
881  * This is for reference.  We have a table-driven version
882  * of the little-endian crc32 generator, which is faster
883  * than the double-loop.
884  */
885 uint32_t
886 ether_crc32_le(const uint8_t *buf, size_t len)
887 {
888         size_t i;
889         uint32_t crc;
890         int bit;
891         uint8_t data;
892
893         crc = 0xffffffff;       /* initial value */
894
895         for (i = 0; i < len; i++) {
896                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
897                         carry = (crc ^ data) & 1;
898                         crc >>= 1;
899                         if (carry)
900                                 crc = (crc ^ ETHER_CRC_POLY_LE);
901                 }
902         }
903
904         return (crc);
905 }
906 #else
907 uint32_t
908 ether_crc32_le(const uint8_t *buf, size_t len)
909 {
910         static const uint32_t crctab[] = {
911                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
912                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
913                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
914                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
915         };
916         size_t i;
917         uint32_t crc;
918
919         crc = 0xffffffff;       /* initial value */
920
921         for (i = 0; i < len; i++) {
922                 crc ^= buf[i];
923                 crc = (crc >> 4) ^ crctab[crc & 0xf];
924                 crc = (crc >> 4) ^ crctab[crc & 0xf];
925         }
926
927         return (crc);
928 }
929 #endif
930
931 uint32_t
932 ether_crc32_be(const uint8_t *buf, size_t len)
933 {
934         size_t i;
935         uint32_t crc, carry;
936         int bit;
937         uint8_t data;
938
939         crc = 0xffffffff;       /* initial value */
940
941         for (i = 0; i < len; i++) {
942                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
943                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
944                         crc <<= 1;
945                         if (carry)
946                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
947                 }
948         }
949
950         return (crc);
951 }
952
953 int
954 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
955 {
956         struct ifaddr *ifa = (struct ifaddr *) data;
957         struct ifreq *ifr = (struct ifreq *) data;
958         int error = 0;
959
960         switch (command) {
961         case SIOCSIFADDR:
962                 ifp->if_flags |= IFF_UP;
963
964                 switch (ifa->ifa_addr->sa_family) {
965 #ifdef INET
966                 case AF_INET:
967                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
968                         arp_ifinit(ifp, ifa);
969                         break;
970 #endif
971                 default:
972                         ifp->if_init(ifp->if_softc);
973                         break;
974                 }
975                 break;
976
977         case SIOCGIFADDR:
978                 {
979                         struct sockaddr *sa;
980
981                         sa = (struct sockaddr *) & ifr->ifr_data;
982                         bcopy(IF_LLADDR(ifp),
983                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
984                 }
985                 break;
986
987         case SIOCSIFMTU:
988                 /*
989                  * Set the interface MTU.
990                  */
991                 if (ifr->ifr_mtu > ETHERMTU) {
992                         error = EINVAL;
993                 } else {
994                         ifp->if_mtu = ifr->ifr_mtu;
995                 }
996                 break;
997         default:
998                 error = EINVAL;                 /* XXX netbsd has ENOTTY??? */
999                 break;
1000         }
1001         return (error);
1002 }
1003
1004 static int
1005 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1006         struct sockaddr *sa)
1007 {
1008         struct sockaddr_dl *sdl;
1009 #ifdef INET
1010         struct sockaddr_in *sin;
1011 #endif
1012 #ifdef INET6
1013         struct sockaddr_in6 *sin6;
1014 #endif
1015         u_char *e_addr;
1016
1017         switch(sa->sa_family) {
1018         case AF_LINK:
1019                 /*
1020                  * No mapping needed. Just check that it's a valid MC address.
1021                  */
1022                 sdl = (struct sockaddr_dl *)sa;
1023                 e_addr = LLADDR(sdl);
1024                 if (!ETHER_IS_MULTICAST(e_addr))
1025                         return EADDRNOTAVAIL;
1026                 *llsa = 0;
1027                 return 0;
1028
1029 #ifdef INET
1030         case AF_INET:
1031                 sin = (struct sockaddr_in *)sa;
1032                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1033                         return EADDRNOTAVAIL;
1034                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1035                 sdl->sdl_alen = ETHER_ADDR_LEN;
1036                 e_addr = LLADDR(sdl);
1037                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1038                 *llsa = (struct sockaddr *)sdl;
1039                 return 0;
1040 #endif
1041 #ifdef INET6
1042         case AF_INET6:
1043                 sin6 = (struct sockaddr_in6 *)sa;
1044                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1045                         /*
1046                          * An IP6 address of 0 means listen to all
1047                          * of the Ethernet multicast address used for IP6.
1048                          * (This is used for multicast routers.)
1049                          */
1050                         ifp->if_flags |= IFF_ALLMULTI;
1051                         *llsa = 0;
1052                         return 0;
1053                 }
1054                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1055                         return EADDRNOTAVAIL;
1056                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1057                 sdl->sdl_alen = ETHER_ADDR_LEN;
1058                 e_addr = LLADDR(sdl);
1059                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1060                 *llsa = (struct sockaddr *)sdl;
1061                 return 0;
1062 #endif
1063
1064         default:
1065                 /*
1066                  * Well, the text isn't quite right, but it's the name
1067                  * that counts...
1068                  */
1069                 return EAFNOSUPPORT;
1070         }
1071 }
1072
1073 static void*
1074 ether_alloc(u_char type, struct ifnet *ifp)
1075 {
1076         struct arpcom   *ac;
1077         
1078         ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1079         ac->ac_ifp = ifp;
1080
1081         return (ac);
1082 }
1083
1084 static void
1085 ether_free(void *com, u_char type)
1086 {
1087
1088         free(com, M_ARPCOM);
1089 }
1090
1091 static int
1092 ether_modevent(module_t mod, int type, void *data)
1093 {
1094
1095         switch (type) {
1096         case MOD_LOAD:
1097                 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1098                 break;
1099         case MOD_UNLOAD:
1100                 if_deregister_com_alloc(IFT_ETHER);
1101                 break;
1102         default:
1103                 return EOPNOTSUPP;
1104         }
1105
1106         return (0);
1107 }
1108
1109 static moduledata_t ether_mod = {
1110         "ether",
1111         ether_modevent,
1112         0
1113 };
1114
1115 void
1116 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1117 {
1118         struct ether_vlan_header vlan;
1119         struct mbuf mv, mb;
1120
1121         KASSERT((m->m_flags & M_VLANTAG) != 0,
1122             ("%s: vlan information not present", __func__));
1123         KASSERT(m->m_len >= sizeof(struct ether_header),
1124             ("%s: mbuf not large enough for header", __func__));
1125         bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1126         vlan.evl_proto = vlan.evl_encap_proto;
1127         vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1128         vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1129         m->m_len -= sizeof(struct ether_header);
1130         m->m_data += sizeof(struct ether_header);
1131         /*
1132          * If a data link has been supplied by the caller, then we will need to
1133          * re-create a stack allocated mbuf chain with the following structure:
1134          *
1135          * (1) mbuf #1 will contain the supplied data link
1136          * (2) mbuf #2 will contain the vlan header
1137          * (3) mbuf #3 will contain the original mbuf's packet data
1138          *
1139          * Otherwise, submit the packet and vlan header via bpf_mtap2().
1140          */
1141         if (data != NULL) {
1142                 mv.m_next = m;
1143                 mv.m_data = (caddr_t)&vlan;
1144                 mv.m_len = sizeof(vlan);
1145                 mb.m_next = &mv;
1146                 mb.m_data = data;
1147                 mb.m_len = dlen;
1148                 bpf_mtap(bp, &mb);
1149         } else
1150                 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1151         m->m_len += sizeof(struct ether_header);
1152         m->m_data -= sizeof(struct ether_header);
1153 }
1154
1155 struct mbuf *
1156 ether_vlanencap(struct mbuf *m, uint16_t tag)
1157 {
1158         struct ether_vlan_header *evl;
1159
1160         M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1161         if (m == NULL)
1162                 return (NULL);
1163         /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1164
1165         if (m->m_len < sizeof(*evl)) {
1166                 m = m_pullup(m, sizeof(*evl));
1167                 if (m == NULL)
1168                         return (NULL);
1169         }
1170
1171         /*
1172          * Transform the Ethernet header into an Ethernet header
1173          * with 802.1Q encapsulation.
1174          */
1175         evl = mtod(m, struct ether_vlan_header *);
1176         bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1177             (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1178         evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1179         evl->evl_tag = htons(tag);
1180         return (m);
1181 }
1182
1183 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1184 MODULE_VERSION(ether, 1);