]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_ethersubr.c
Missed this file for select SMP fixes associated with rev 1.93 of
[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  * 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$
35  */
36
37 #include "opt_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipx.h"
41 #include "opt_bdg.h"
42 #include "opt_netgraph.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/random.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>
62 #include <net/bridge.h>
63
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #endif
69 #ifdef INET6
70 #include <netinet6/nd6.h>
71 #endif
72
73 #ifdef IPX
74 #include <netipx/ipx.h>
75 #include <netipx/ipx_if.h>
76 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
77 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
78                 struct sockaddr *dst, short *tp, int *hlen);
79 #endif
80
81 #ifdef NS
82 #include <netns/ns.h>
83 #include <netns/ns_if.h>
84 ushort ns_nettype;
85 int ether_outputdebug = 0;
86 int ether_inputdebug = 0;
87 #endif
88
89 #ifdef NETATALK
90 #include <netatalk/at.h>
91 #include <netatalk/at_var.h>
92 #include <netatalk/at_extern.h>
93
94 #define llc_snap_org_code llc_un.type_snap.org_code
95 #define llc_snap_ether_type llc_un.type_snap.ether_type
96
97 extern u_char   at_org_code[3];
98 extern u_char   aarp_org_code[3];
99 #endif /* NETATALK */
100
101 /* netgraph node hooks for ng_ether(4) */
102 void    (*ng_ether_input_p)(struct ifnet *ifp,
103                 struct mbuf **mp, struct ether_header *eh);
104 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp,
105                 struct mbuf *m, struct ether_header *eh);
106 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
107 void    (*ng_ether_attach_p)(struct ifnet *ifp);
108 void    (*ng_ether_detach_p)(struct ifnet *ifp);
109
110 int     (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
111 int     (*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
112                 u_int16_t t);
113
114 /* bridge support */
115 int do_bridge;
116 bridge_in_t *bridge_in_ptr;
117 bdg_forward_t *bdg_forward_ptr;
118 bdgtakeifaces_t *bdgtakeifaces_ptr;
119 struct bdg_softc *ifp2sc;
120
121 static  int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
122                                     struct sockaddr *));
123 u_char  etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
124 #define senderr(e) do { error = (e); goto bad;} while (0)
125 #define IFP2AC(IFP) ((struct arpcom *)IFP)
126
127 /*
128  * Ethernet output routine.
129  * Encapsulate a packet of type family for the local net.
130  * Use trailer local net encapsulation if enough data in first
131  * packet leaves a multiple of 512 bytes of data in remainder.
132  * Assumes that ifp is actually pointer to arpcom structure.
133  */
134 int
135 ether_output(ifp, m, dst, rt0)
136         register struct ifnet *ifp;
137         struct mbuf *m;
138         struct sockaddr *dst;
139         struct rtentry *rt0;
140 {
141         short type;
142         int error = 0, hdrcmplt = 0;
143         u_char esrc[6], edst[6];
144         register struct rtentry *rt;
145         register struct ether_header *eh;
146         int off, loop_copy = 0;
147         int hlen;       /* link layer header lenght */
148         struct arpcom *ac = IFP2AC(ifp);
149
150         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
151                 senderr(ENETDOWN);
152         rt = rt0;
153         if (rt) {
154                 if ((rt->rt_flags & RTF_UP) == 0) {
155                         rt0 = rt = rtalloc1(dst, 1, 0UL);
156                         if (rt0)
157                                 rt->rt_refcnt--;
158                         else
159                                 senderr(EHOSTUNREACH);
160                 }
161                 if (rt->rt_flags & RTF_GATEWAY) {
162                         if (rt->rt_gwroute == 0)
163                                 goto lookup;
164                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
165                                 rtfree(rt); rt = rt0;
166                         lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
167                                                           0UL);
168                                 if ((rt = rt->rt_gwroute) == 0)
169                                         senderr(EHOSTUNREACH);
170                         }
171                 }
172                 if (rt->rt_flags & RTF_REJECT)
173                         if (rt->rt_rmx.rmx_expire == 0 ||
174                             time_second < rt->rt_rmx.rmx_expire)
175                                 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
176         }
177         hlen = ETHER_HDR_LEN;
178         switch (dst->sa_family) {
179 #ifdef INET
180         case AF_INET:
181                 if (!arpresolve(ifp, rt, m, dst, edst, rt0))
182                         return (0);     /* if not yet resolved */
183                 off = m->m_pkthdr.len - m->m_len;
184                 type = htons(ETHERTYPE_IP);
185                 break;
186 #endif
187 #ifdef INET6
188         case AF_INET6:
189                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
190                         /* Something bad happened */
191                         return(0);
192                 }
193                 off = m->m_pkthdr.len - m->m_len;
194                 type = htons(ETHERTYPE_IPV6);
195                 break;
196 #endif
197 #ifdef IPX
198         case AF_IPX:
199                 if (ef_outputp) {
200                     error = ef_outputp(ifp, &m, dst, &type, &hlen);
201                     if (error)
202                         goto bad;
203                 } else
204                     type = htons(ETHERTYPE_IPX);
205                 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
206                     (caddr_t)edst, sizeof (edst));
207                 break;
208 #endif
209 #ifdef NETATALK
210         case AF_APPLETALK:
211           {
212             struct at_ifaddr *aa;
213
214             if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
215                     goto bad;
216             }
217             if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
218                     return (0);
219             /*
220              * In the phase 2 case, need to prepend an mbuf for the llc header.
221              * Since we must preserve the value of m, which is passed to us by
222              * value, we m_copy() the first mbuf, and use it for our llc header.
223              */
224             if ( aa->aa_flags & AFA_PHASE2 ) {
225                 struct llc llc;
226
227                 M_PREPEND(m, sizeof(struct llc), M_TRYWAIT);
228                 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
229                 llc.llc_control = LLC_UI;
230                 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
231                 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
232                 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
233                 type = htons(m->m_pkthdr.len);
234                 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
235             } else {
236                 type = htons(ETHERTYPE_AT);
237             }
238             break;
239           }
240 #endif /* NETATALK */
241 #ifdef NS
242         case AF_NS:
243                 switch(ns_nettype){
244                 default:
245                 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
246                         type = 0x8137;
247                         break;
248                 case 0x0: /* Novell 802.3 */
249                         type = htons( m->m_pkthdr.len);
250                         break;
251                 case 0xe0e0: /* Novell 802.2 and Token-Ring */
252                         M_PREPEND(m, 3, M_TRYWAIT);
253                         type = htons( m->m_pkthdr.len);
254                         cp = mtod(m, u_char *);
255                         *cp++ = 0xE0;
256                         *cp++ = 0xE0;
257                         *cp++ = 0x03;
258                         break;
259                 }
260                 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
261                     (caddr_t)edst, sizeof (edst));
262                 /*
263                  * XXX if ns_thishost is the same as the node's ethernet
264                  * address then just the default code will catch this anyhow.
265                  * So I'm not sure if this next clause should be here at all?
266                  * [JRE]
267                  */
268                 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
269                         m->m_pkthdr.rcvif = ifp;
270                         inq = &nsintrq;
271                         if (IF_HANDOFF(inq, m, NULL))
272                                 schednetisr(NETISR_NS);
273                         return (error);
274                 }
275                 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
276                         m->m_flags |= M_BCAST;
277                 }
278                 break;
279 #endif /* NS */
280
281         case pseudo_AF_HDRCMPLT:
282                 hdrcmplt = 1;
283                 eh = (struct ether_header *)dst->sa_data;
284                 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
285                 /* FALLTHROUGH */
286
287         case AF_UNSPEC:
288                 loop_copy = -1; /* if this is for us, don't do it */
289                 eh = (struct ether_header *)dst->sa_data;
290                 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
291                 type = eh->ether_type;
292                 break;
293
294         default:
295                 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
296                         dst->sa_family);
297                 senderr(EAFNOSUPPORT);
298         }
299
300         /*
301          * Add local net header.  If no space in first mbuf,
302          * allocate another.
303          */
304         M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
305         if (m == 0)
306                 senderr(ENOBUFS);
307         eh = mtod(m, struct ether_header *);
308         (void)memcpy(&eh->ether_type, &type,
309                 sizeof(eh->ether_type));
310         (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
311         if (hdrcmplt)
312                 (void)memcpy(eh->ether_shost, esrc,
313                         sizeof(eh->ether_shost));
314         else
315                 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
316                         sizeof(eh->ether_shost));
317
318         /*
319          * If a simplex interface, and the packet is being sent to our
320          * Ethernet address or a broadcast address, loopback a copy.
321          * XXX To make a simplex device behave exactly like a duplex
322          * device, we should copy in the case of sending to our own
323          * ethernet address (thus letting the original actually appear
324          * on the wire). However, we don't do that here for security
325          * reasons and compatibility with the original behavior.
326          */
327         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
328                 int csum_flags = 0;
329
330                 if (m->m_pkthdr.csum_flags & CSUM_IP)
331                         csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
332                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
333                         csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
334                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
335                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
336
337                         n->m_pkthdr.csum_flags |= csum_flags;
338                         if (csum_flags & CSUM_DATA_VALID)
339                                 n->m_pkthdr.csum_data = 0xffff;
340
341                         (void) if_simloop(ifp, n, dst->sa_family, hlen);
342                 } else if (bcmp(eh->ether_dhost,
343                     eh->ether_shost, ETHER_ADDR_LEN) == 0) {
344                         m->m_pkthdr.csum_flags |= csum_flags;
345                         if (csum_flags & CSUM_DATA_VALID)
346                                 m->m_pkthdr.csum_data = 0xffff;
347                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
348                         return (0);     /* XXX */
349                 }
350         }
351
352         /* Handle ng_ether(4) processing, if any */
353         if (ng_ether_output_p != NULL) {
354                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
355 bad:                    if (m != NULL)
356                                 m_freem(m);
357                         return (error);
358                 }
359                 if (m == NULL)
360                         return (0);
361         }
362
363         /* Continue with link-layer output */
364         return ether_output_frame(ifp, m);
365 }
366
367 /*
368  * Ethernet link layer output routine to send a raw frame to the device.
369  *
370  * This assumes that the 14 byte Ethernet header is present and contiguous
371  * in the first mbuf (if BRIDGE'ing).
372  */
373 int
374 ether_output_frame(ifp, m)
375         struct ifnet *ifp;
376         struct mbuf *m;
377 {
378         int error = 0;
379
380         if (BDG_ACTIVE(ifp) ) {
381                 struct ether_header *eh; /* a ptr suffices */
382
383                 m->m_pkthdr.rcvif = NULL;
384                 eh = mtod(m, struct ether_header *);
385                 m_adj(m, ETHER_HDR_LEN);
386                 m = bdg_forward_ptr(m, eh, ifp);
387                 if (m != NULL)
388                         m_freem(m);
389                 return (0);
390         }
391
392         /*
393          * Queue message on interface, update output statistics if
394          * successful, and start output if interface not yet active.
395          */
396         if (! IF_HANDOFF(&ifp->if_snd, m, ifp))
397                 return (ENOBUFS);
398         return (error);
399 }
400
401 /*
402  * Process a received Ethernet packet;
403  * the packet is in the mbuf chain m without
404  * the ether header, which is provided separately.
405  *
406  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
407  * cluster, right before m_data. So be very careful when working on m,
408  * as you could destroy *eh !!
409  * A (probably) more convenient and efficient interface to ether_input
410  * is to have the whole packet (with the ethernet header) into the mbuf:
411  * modules which do not need the ethernet header can easily drop it, while
412  * others (most noticeably bridge and ng_ether) do not need to do additional
413  * work to put the ethernet header back into the mbuf.
414  *
415  * First we perform any link layer operations, then continue
416  * to the upper layers with ether_demux().
417  */
418 void
419 ether_input(ifp, eh, m)
420         struct ifnet *ifp;
421         struct ether_header *eh;
422         struct mbuf *m;
423 {
424         struct ether_header save_eh;
425
426         /* Check for a BPF tap */
427         if (ifp->if_bpf != NULL) {
428                 struct m_hdr mh;
429
430                 /* This kludge is OK; BPF treats the "mbuf" as read-only */
431                 mh.mh_next = m;
432                 mh.mh_data = (char *)eh;
433                 mh.mh_len = ETHER_HDR_LEN;
434                 bpf_mtap(ifp, (struct mbuf *)&mh);
435         }
436
437         ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
438
439         /* Handle ng_ether(4) processing, if any */
440         if (ng_ether_input_p != NULL) {
441                 (*ng_ether_input_p)(ifp, &m, eh);
442                 if (m == NULL)
443                         return;
444         }
445
446         /* Check for bridging mode */
447         if (BDG_ACTIVE(ifp) ) {
448                 struct ifnet *bif;
449
450                 /* Check with bridging code */
451                 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
452                         m_freem(m);
453                         return;
454                 }
455                 if (bif != BDG_LOCAL) {
456                         struct mbuf *oldm = m ;
457
458                         save_eh = *eh ; /* because it might change */
459                         m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
460                         /*
461                          * Do not continue if bdg_forward_ptr() processed our
462                          * packet (and cleared the mbuf pointer m) or if
463                          * it dropped (m_free'd) the packet itself.
464                          */
465                         if (m == NULL) {
466                             if (bif == BDG_BCAST || bif == BDG_MCAST)
467                                 printf("bdg_forward drop MULTICAST PKT\n");
468                             return;
469                         }
470                         if (m != oldm) /* m changed! */
471                             eh = &save_eh ;
472                 }
473                 if (bif == BDG_LOCAL
474                     || bif == BDG_BCAST
475                     || bif == BDG_MCAST)
476                         goto recvLocal;                 /* receive locally */
477
478                 /* If not local and not multicast, just drop it */
479                 if (m != NULL)
480                         m_freem(m);
481                 return;
482        }
483
484 recvLocal:
485         /* Continue with upper layer processing */
486         ether_demux(ifp, eh, m);
487         /* First chunk of an mbuf contains good junk */
488         if (harvest.ethernet)
489                 random_harvest(m, 16, 3, 0, RANDOM_NET);
490 }
491
492 /*
493  * Upper layer processing for a received Ethernet packet.
494  */
495 void
496 ether_demux(ifp, eh, m)
497         struct ifnet *ifp;
498         struct ether_header *eh;
499         struct mbuf *m;
500 {
501         struct ifqueue *inq;
502         u_short ether_type;
503 #if defined(NETATALK)
504         register struct llc *l;
505 #endif
506
507     if (! (BDG_ACTIVE(ifp) ) )
508         /* Discard packet if upper layers shouldn't see it because it was
509            unicast to a different Ethernet address. If the driver is working
510            properly, then this situation can only happen when the interface
511            is in promiscuous mode. */
512         if ((ifp->if_flags & IFF_PROMISC) != 0
513             && (eh->ether_dhost[0] & 1) == 0
514             && bcmp(eh->ether_dhost,
515               IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0) {
516                 m_freem(m);
517                 return;
518         }
519
520         /* Discard packet if interface is not up */
521         if ((ifp->if_flags & IFF_UP) == 0) {
522                 m_freem(m);
523                 return;
524         }
525         if (eh->ether_dhost[0] & 1) {
526                 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
527                          sizeof(etherbroadcastaddr)) == 0)
528                         m->m_flags |= M_BCAST;
529                 else
530                         m->m_flags |= M_MCAST;
531         }
532         if (m->m_flags & (M_BCAST|M_MCAST))
533                 ifp->if_imcasts++;
534
535         ether_type = ntohs(eh->ether_type);
536
537         switch (ether_type) {
538 #ifdef INET
539         case ETHERTYPE_IP:
540                 if (ipflow_fastforward(m))
541                         return;
542                 schednetisr(NETISR_IP);
543                 inq = &ipintrq;
544                 break;
545
546         case ETHERTYPE_ARP:
547                 if (ifp->if_flags & IFF_NOARP) {
548                         /* Discard packet if ARP is disabled on interface */
549                         m_freem(m);
550                         return;
551                 }
552                 schednetisr(NETISR_ARP);
553                 inq = &arpintrq;
554                 break;
555 #endif
556 #ifdef IPX
557         case ETHERTYPE_IPX:
558                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
559                         return;
560                 schednetisr(NETISR_IPX);
561                 inq = &ipxintrq;
562                 break;
563 #endif
564 #ifdef INET6
565         case ETHERTYPE_IPV6:
566                 schednetisr(NETISR_IPV6);
567                 inq = &ip6intrq;
568                 break;
569 #endif
570 #ifdef NS
571         case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
572                 schednetisr(NETISR_NS);
573                 inq = &nsintrq;
574                 break;
575
576 #endif /* NS */
577 #ifdef NETATALK
578         case ETHERTYPE_AT:
579                 schednetisr(NETISR_ATALK);
580                 inq = &atintrq1;
581                 break;
582         case ETHERTYPE_AARP:
583                 /* probably this should be done with a NETISR as well */
584                 aarpinput(IFP2AC(ifp), m); /* XXX */
585                 return;
586 #endif /* NETATALK */
587         case ETHERTYPE_VLAN:
588                 VLAN_INPUT(eh, m);
589                 return;
590         default:
591 #ifdef IPX
592                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
593                         return;
594 #endif /* IPX */
595 #ifdef NS
596                 checksum = mtod(m, ushort *);
597                 /* Novell 802.3 */
598                 if ((ether_type <= ETHERMTU) &&
599                         ((*checksum == 0xffff) || (*checksum == 0xE0E0))){
600                         if(*checksum == 0xE0E0) {
601                                 m->m_pkthdr.len -= 3;
602                                 m->m_len -= 3;
603                                 m->m_data += 3;
604                         }
605                                 schednetisr(NETISR_NS);
606                                 inq = &nsintrq;
607                                 break;
608                 }
609 #endif /* NS */
610 #if defined(NETATALK)
611                 if (ether_type > ETHERMTU)
612                         goto dropanyway;
613                 l = mtod(m, struct llc *);
614                 switch (l->llc_dsap) {
615                 case LLC_SNAP_LSAP:
616                     switch (l->llc_control) {
617                     case LLC_UI:
618                         if (l->llc_ssap != LLC_SNAP_LSAP)
619                             goto dropanyway;
620         
621                         if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
622                                    sizeof(at_org_code)) == 0 &&
623                              ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
624                             inq = &atintrq2;
625                             m_adj( m, sizeof( struct llc ));
626                             schednetisr(NETISR_ATALK);
627                             break;
628                         }
629
630                         if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
631                                    sizeof(aarp_org_code)) == 0 &&
632                              ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
633                             m_adj( m, sizeof( struct llc ));
634                             aarpinput(IFP2AC(ifp), m); /* XXX */
635                             return;
636                         }
637                 
638                     default:
639                         goto dropanyway;
640                     }
641                     break;
642                 dropanyway:
643                 default:
644                         if (ng_ether_input_orphan_p != NULL)
645                                 (*ng_ether_input_orphan_p)(ifp, m, eh);
646                         else
647                                 m_freem(m);
648                         return;
649                 }
650 #else /* NETATALK */
651                 if (ng_ether_input_orphan_p != NULL)
652                         (*ng_ether_input_orphan_p)(ifp, m, eh);
653                 else
654                         m_freem(m);
655                 return;
656 #endif /* NETATALK */
657         }
658
659         (void) IF_HANDOFF(inq, m, NULL);
660 }
661
662 /*
663  * Perform common duties while attaching to interface list
664  */
665 void
666 ether_ifattach(ifp, bpf)
667         register struct ifnet *ifp;
668         int bpf;
669 {
670         register struct ifaddr *ifa;
671         register struct sockaddr_dl *sdl;
672
673         ifp->if_type = IFT_ETHER;
674         ifp->if_addrlen = 6;
675         ifp->if_hdrlen = 14;
676         if_attach(ifp);
677         ifp->if_mtu = ETHERMTU;
678         ifp->if_resolvemulti = ether_resolvemulti;
679         if (ifp->if_baudrate == 0)
680             ifp->if_baudrate = 10000000;
681         ifp->if_broadcastaddr = etherbroadcastaddr;
682         ifa = ifaddr_byindex(ifp->if_index);
683         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
684         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
685         sdl->sdl_type = IFT_ETHER;
686         sdl->sdl_alen = ifp->if_addrlen;
687         bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
688         if (bpf)
689                 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
690         if (ng_ether_attach_p != NULL)
691                 (*ng_ether_attach_p)(ifp);
692         if (BDG_LOADED)
693                 bdgtakeifaces_ptr();
694 }
695
696 /*
697  * Perform common duties while detaching an Ethernet interface
698  */
699 void
700 ether_ifdetach(ifp, bpf)
701         struct ifnet *ifp;
702         int bpf;
703 {
704         if (ng_ether_detach_p != NULL)
705                 (*ng_ether_detach_p)(ifp);
706         if (bpf)
707                 bpfdetach(ifp);
708         if_detach(ifp);
709         if (BDG_LOADED)
710                 bdgtakeifaces_ptr();
711 }
712
713 SYSCTL_DECL(_net_link);
714 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
715
716 int
717 ether_ioctl(ifp, command, data)
718         struct ifnet *ifp;
719         int command;
720         caddr_t data;
721 {
722         struct ifaddr *ifa = (struct ifaddr *) data;
723         struct ifreq *ifr = (struct ifreq *) data;
724         int error = 0;
725
726         switch (command) {
727         case SIOCSIFADDR:
728                 ifp->if_flags |= IFF_UP;
729
730                 switch (ifa->ifa_addr->sa_family) {
731 #ifdef INET
732                 case AF_INET:
733                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
734                         arp_ifinit(ifp, ifa);
735                         break;
736 #endif
737 #ifdef IPX
738                 /*
739                  * XXX - This code is probably wrong
740                  */
741                 case AF_IPX:
742                         {
743                         register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
744                         struct arpcom *ac = IFP2AC(ifp);
745
746                         if (ipx_nullhost(*ina))
747                                 ina->x_host =
748                                     *(union ipx_host *)
749                                     ac->ac_enaddr;
750                         else {
751                                 bcopy((caddr_t) ina->x_host.c_host,
752                                       (caddr_t) ac->ac_enaddr,
753                                       sizeof(ac->ac_enaddr));
754                         }
755
756                         /*
757                          * Set new address
758                          */
759                         ifp->if_init(ifp->if_softc);
760                         break;
761                         }
762 #endif
763 #ifdef NS
764                 /*
765                  * XXX - This code is probably wrong
766                  */
767                 case AF_NS:
768                 {
769                         register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
770                         struct arpcom *ac = IFP2AC(ifp);
771
772                         if (ns_nullhost(*ina))
773                                 ina->x_host =
774                                     *(union ns_host *) (ac->ac_enaddr);
775                         else {
776                                 bcopy((caddr_t) ina->x_host.c_host,
777                                       (caddr_t) ac->ac_enaddr,
778                                       sizeof(ac->ac_enaddr));
779                         }
780
781                         /*
782                          * Set new address
783                          */
784                         ifp->if_init(ifp->if_softc);
785                         break;
786                 }
787 #endif
788                 default:
789                         ifp->if_init(ifp->if_softc);
790                         break;
791                 }
792                 break;
793
794         case SIOCGIFADDR:
795                 {
796                         struct sockaddr *sa;
797
798                         sa = (struct sockaddr *) & ifr->ifr_data;
799                         bcopy(IFP2AC(ifp)->ac_enaddr,
800                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
801                 }
802                 break;
803
804         case SIOCSIFMTU:
805                 /*
806                  * Set the interface MTU.
807                  */
808                 if (ifr->ifr_mtu > ETHERMTU) {
809                         error = EINVAL;
810                 } else {
811                         ifp->if_mtu = ifr->ifr_mtu;
812                 }
813                 break;
814         }
815         return (error);
816 }
817
818 int
819 ether_resolvemulti(ifp, llsa, sa)
820         struct ifnet *ifp;
821         struct sockaddr **llsa;
822         struct sockaddr *sa;
823 {
824         struct sockaddr_dl *sdl;
825         struct sockaddr_in *sin;
826 #ifdef INET6
827         struct sockaddr_in6 *sin6;
828 #endif
829         u_char *e_addr;
830
831         switch(sa->sa_family) {
832         case AF_LINK:
833                 /*
834                  * No mapping needed. Just check that it's a valid MC address.
835                  */
836                 sdl = (struct sockaddr_dl *)sa;
837                 e_addr = LLADDR(sdl);
838                 if ((e_addr[0] & 1) != 1)
839                         return EADDRNOTAVAIL;
840                 *llsa = 0;
841                 return 0;
842
843 #ifdef INET
844         case AF_INET:
845                 sin = (struct sockaddr_in *)sa;
846                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
847                         return EADDRNOTAVAIL;
848                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
849                        M_WAITOK|M_ZERO);
850                 sdl->sdl_len = sizeof *sdl;
851                 sdl->sdl_family = AF_LINK;
852                 sdl->sdl_index = ifp->if_index;
853                 sdl->sdl_type = IFT_ETHER;
854                 sdl->sdl_alen = ETHER_ADDR_LEN;
855                 e_addr = LLADDR(sdl);
856                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
857                 *llsa = (struct sockaddr *)sdl;
858                 return 0;
859 #endif
860 #ifdef INET6
861         case AF_INET6:
862                 sin6 = (struct sockaddr_in6 *)sa;
863                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
864                         /*
865                          * An IP6 address of 0 means listen to all
866                          * of the Ethernet multicast address used for IP6.
867                          * (This is used for multicast routers.)
868                          */
869                         ifp->if_flags |= IFF_ALLMULTI;
870                         *llsa = 0;
871                         return 0;
872                 }
873                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
874                         return EADDRNOTAVAIL;
875                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
876                        M_WAITOK|M_ZERO);
877                 sdl->sdl_len = sizeof *sdl;
878                 sdl->sdl_family = AF_LINK;
879                 sdl->sdl_index = ifp->if_index;
880                 sdl->sdl_type = IFT_ETHER;
881                 sdl->sdl_alen = ETHER_ADDR_LEN;
882                 e_addr = LLADDR(sdl);
883                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
884                 *llsa = (struct sockaddr *)sdl;
885                 return 0;
886 #endif
887
888         default:
889                 /*
890                  * Well, the text isn't quite right, but it's the name
891                  * that counts...
892                  */
893                 return EAFNOSUPPORT;
894         }
895 }
896