2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $Id: if_ethersubr.c,v 1.53 1998/12/14 17:58:05 luigi Exp $
37 #include "opt_atalk.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <net/if_llc.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_ether.h>
65 #include <netipx/ipx.h>
66 #include <netipx/ipx_if.h>
71 #include <netns/ns_if.h>
73 int ether_outputdebug = 0;
74 int ether_inputdebug = 0;
78 #include <netiso/argo_debug.h>
79 #include <netiso/iso.h>
80 #include <netiso/iso_var.h>
81 #include <netiso/iso_snpac.h>
85 #include <netccitt/dll.h>
86 #include <netccitt/llc_var.h>
89 #if defined(LLC) && defined(CCITT)
90 extern struct ifqueue pkintrq;
94 #include <netatalk/at.h>
95 #include <netatalk/at_var.h>
96 #include <netatalk/at_extern.h>
98 #define llc_snap_org_code llc_un.type_snap.org_code
99 #define llc_snap_ether_type llc_un.type_snap.ether_type
101 extern u_char at_org_code[3];
102 extern u_char aarp_org_code[3];
103 #endif /* NETATALK */
106 #include <net/bridge.h>
111 #include <net/if_vlan_var.h>
112 #endif /* NVLAN > 0 */
114 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
116 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
117 #define senderr(e) { error = (e); goto bad;}
120 * Ethernet output routine.
121 * Encapsulate a packet of type family for the local net.
122 * Use trailer local net encapsulation if enough data in first
123 * packet leaves a multiple of 512 bytes of data in remainder.
124 * Assumes that ifp is actually pointer to arpcom structure.
127 ether_output(ifp, m0, dst, rt0)
128 register struct ifnet *ifp;
130 struct sockaddr *dst;
136 register struct mbuf *m = m0;
137 register struct rtentry *rt;
138 register struct ether_header *eh;
139 int off, len = m->m_pkthdr.len, loop_copy = 0;
140 int hlen; /* link layer header lenght */
141 struct arpcom *ac = (struct arpcom *)ifp;
143 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
147 if ((rt->rt_flags & RTF_UP) == 0) {
148 rt0 = rt = rtalloc1(dst, 1, 0UL);
152 senderr(EHOSTUNREACH);
154 if (rt->rt_flags & RTF_GATEWAY) {
155 if (rt->rt_gwroute == 0)
157 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
158 rtfree(rt); rt = rt0;
159 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
161 if ((rt = rt->rt_gwroute) == 0)
162 senderr(EHOSTUNREACH);
165 if (rt->rt_flags & RTF_REJECT)
166 if (rt->rt_rmx.rmx_expire == 0 ||
167 time_second < rt->rt_rmx.rmx_expire)
168 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
170 hlen = ETHER_HDR_LEN;
171 switch (dst->sa_family) {
174 if (!arpresolve(ac, rt, m, dst, edst, rt0))
175 return (0); /* if not yet resolved */
176 off = m->m_pkthdr.len - m->m_len;
177 type = htons(ETHERTYPE_IP);
182 type = htons(ETHERTYPE_IPX);
183 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
184 (caddr_t)edst, sizeof (edst));
190 struct at_ifaddr *aa;
192 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
195 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
198 * In the phase 2 case, need to prepend an mbuf for the llc header.
199 * Since we must preserve the value of m, which is passed to us by
200 * value, we m_copy() the first mbuf, and use it for our llc header.
202 if ( aa->aa_flags & AFA_PHASE2 ) {
205 M_PREPEND(m, sizeof(struct llc), M_WAIT);
206 len += sizeof(struct llc);
207 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
208 llc.llc_control = LLC_UI;
209 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
210 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
211 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
212 type = htons(m->m_pkthdr.len);
213 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
215 type = htons(ETHERTYPE_AT);
224 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
227 case 0x0: /* Novell 802.3 */
228 type = htons( m->m_pkthdr.len);
230 case 0xe0e0: /* Novell 802.2 and Token-Ring */
231 M_PREPEND(m, 3, M_WAIT);
232 type = htons( m->m_pkthdr.len);
233 cp = mtod(m, u_char *);
239 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
240 (caddr_t)edst, sizeof (edst));
241 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
242 m->m_pkthdr.rcvif = ifp;
243 schednetisr(NETISR_NS);
254 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
255 m2 = m_copy(m, 0, (int)M_COPYALL);
256 m2->m_pkthdr.rcvif = ifp;
257 schednetisr(NETISR_NS);
273 register struct sockaddr_dl *sdl;
275 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
276 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
277 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
279 iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
280 (char *)edst, &snpalen))
281 goto bad; /* Not Resolved */
282 /* If broadcasting on a simplex interface, loopback a copy */
284 m->m_flags |= (M_BCAST|M_MCAST);
285 M_PREPEND(m, 3, M_DONTWAIT);
288 type = htons(m->m_pkthdr.len);
289 l = mtod(m, struct llc *);
290 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
291 l->llc_control = LLC_UI;
295 printf("unoutput: sending pkt to: ");
297 printf("%x ", edst[i] & 0xff);
305 register struct sockaddr_dl *sdl =
306 (struct sockaddr_dl *) rt -> rt_gateway;
308 if (sdl && sdl->sdl_family == AF_LINK
309 && sdl->sdl_alen > 0) {
310 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst));
311 } else goto bad; /* Not a link interface ? Funny ... */
314 type = htons(m->m_pkthdr.len);
318 register struct llc *l = mtod(m, struct llc *);
320 printf("ether_output: sending LLC2 pkt to: ");
322 printf("%x ", edst[i] & 0xff);
323 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
324 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
325 l->llc_control & 0xff);
328 #endif /* LLC_DEBUG */
333 loop_copy = -1; /* if this is for us, don't do it */
334 eh = (struct ether_header *)dst->sa_data;
335 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
336 type = eh->ether_type;
340 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
342 senderr(EAFNOSUPPORT);
346 * Add local net header. If no space in first mbuf,
349 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
352 eh = mtod(m, struct ether_header *);
353 (void)memcpy(&eh->ether_type, &type,
354 sizeof(eh->ether_type));
355 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
356 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
357 sizeof(eh->ether_shost));
360 * If a simplex interface, and the packet is being sent to our
361 * Ethernet address or a broadcast address, loopback a copy.
362 * XXX To make a simplex device behave exactly like a duplex
363 * device, we should copy in the case of sending to our own
364 * ethernet address (thus letting the original actually appear
365 * on the wire). However, we don't do that here for security
366 * reasons and compatibility with the original behavior.
368 if ((ifp->if_flags & IFF_SIMPLEX) &&
370 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
371 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
373 (void) if_simloop(ifp, n, dst, hlen);
374 } else if (bcmp(eh->ether_dhost,
375 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
376 (void) if_simloop(ifp, m, dst, hlen);
377 return (0); /* XXX */
382 struct mbuf *m0 = m ;
384 if (m->m_pkthdr.rcvif)
385 m->m_pkthdr.rcvif = NULL ;
386 ifp = bridge_dst_lookup(m);
387 bdg_forward(&m0, ifp);
395 * Queue message on interface, and start output if interface
398 if (IF_QFULL(&ifp->if_snd)) {
399 IF_DROP(&ifp->if_snd);
403 IF_ENQUEUE(&ifp->if_snd, m);
404 if ((ifp->if_flags & IFF_OACTIVE) == 0)
405 (*ifp->if_start)(ifp);
407 ifp->if_obytes += len + sizeof (struct ether_header);
408 if (m->m_flags & M_MCAST)
419 * Process a received Ethernet packet;
420 * the packet is in the mbuf chain m without
421 * the ether header, which is provided separately.
424 ether_input(ifp, eh, m)
426 register struct ether_header *eh;
429 register struct ifqueue *inq;
432 #if defined (ISO) || defined (LLC) || defined(NETATALK)
433 register struct llc *l;
436 if ((ifp->if_flags & IFF_UP) == 0) {
440 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
441 if (eh->ether_dhost[0] & 1) {
442 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
443 sizeof(etherbroadcastaddr)) == 0)
444 m->m_flags |= M_BCAST;
446 m->m_flags |= M_MCAST;
448 if (m->m_flags & (M_BCAST|M_MCAST))
451 ether_type = ntohs(eh->ether_type);
454 if (ether_type == vlan_proto) {
455 if (vlan_input(eh, m) < 0)
456 ifp->if_data.ifi_noproto++;
459 #endif /* NVLAN > 0 */
461 switch (ether_type) {
464 if (ipflow_fastforward(m))
466 schednetisr(NETISR_IP);
471 schednetisr(NETISR_ARP);
477 schednetisr(NETISR_IPX);
482 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
483 schednetisr(NETISR_NS);
490 schednetisr(NETISR_ATALK);
494 /* probably this should be done with a NETISR as well */
495 aarpinput((struct arpcom *)ifp, m); /* XXX */
500 checksum = mtod(m, ushort *);
502 if ((ether_type <= ETHERMTU) &&
503 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){
504 if(*checksum == 0xE0E0) {
505 m->m_pkthdr.len -= 3;
509 schednetisr(NETISR_NS);
514 #if defined (ISO) || defined (LLC) || defined(NETATALK)
515 if (ether_type > ETHERMTU)
517 l = mtod(m, struct llc *);
518 switch (l->llc_dsap) {
521 switch (l->llc_control) {
523 if (l->llc_ssap != LLC_SNAP_LSAP)
526 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
527 sizeof(at_org_code)) == 0 &&
528 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
530 m_adj( m, sizeof( struct llc ));
531 schednetisr(NETISR_ATALK);
535 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
536 sizeof(aarp_org_code)) == 0 &&
537 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
538 m_adj( m, sizeof( struct llc ));
539 aarpinput((struct arpcom *)ifp, m); /* XXX */
550 switch (l->llc_control) {
552 /* LLC_UI_P forbidden in class 1 service */
553 if ((l->llc_dsap == LLC_ISO_LSAP) &&
554 (l->llc_ssap == LLC_ISO_LSAP)) {
556 if (m->m_pkthdr.len > ether_type)
557 m_adj(m, ether_type - m->m_pkthdr.len);
558 m->m_data += 3; /* XXX */
559 m->m_len -= 3; /* XXX */
560 m->m_pkthdr.len -= 3; /* XXX */
561 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
564 *mtod(m, struct ether_header *) = *eh;
566 printf("clnp packet");
568 schednetisr(NETISR_ISO);
581 l->llc_dsap = l->llc_ssap = 0;
582 /* Fall through to */
587 register struct ether_header *eh2;
589 u_char c = l->llc_dsap;
591 l->llc_dsap = l->llc_ssap;
593 if (m->m_flags & (M_BCAST | M_MCAST))
594 bcopy((caddr_t)ac->ac_enaddr,
595 (caddr_t)eh->ether_dhost, 6);
596 sa.sa_family = AF_UNSPEC;
597 sa.sa_len = sizeof(sa);
598 eh2 = (struct ether_header *)sa.sa_data;
599 for (i = 0; i < 6; i++) {
600 eh2->ether_shost[i] = c = eh->ether_dhost[i];
601 eh2->ether_dhost[i] =
602 eh->ether_dhost[i] = eh->ether_shost[i];
603 eh->ether_shost[i] = c;
605 ifp->if_output(ifp, m, &sa, NULL);
617 if (m->m_pkthdr.len > ether_type)
618 m_adj(m, ether_type - m->m_pkthdr.len);
619 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
622 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
623 eh->ether_dhost, LLC_X25_LSAP, 6,
624 mtod(m, struct sdl_hdr *)))
625 panic("ETHER cons addr failure");
626 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
628 printf("llc packet\n");
629 #endif /* LLC_DEBUG */
630 schednetisr(NETISR_CCITT);
640 #else /* ISO || LLC || NETATALK */
643 #endif /* ISO || LLC || NETATALK */
656 * Perform common duties while attaching to interface list
660 register struct ifnet *ifp;
662 register struct ifaddr *ifa;
663 register struct sockaddr_dl *sdl;
665 ifp->if_type = IFT_ETHER;
668 ifp->if_mtu = ETHERMTU;
669 ifp->if_resolvemulti = ether_resolvemulti;
670 if (ifp->if_baudrate == 0)
671 ifp->if_baudrate = 10000000;
672 ifa = ifnet_addrs[ifp->if_index - 1];
674 printf("ether_ifattach: no lladdr!\n");
677 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
678 sdl->sdl_type = IFT_ETHER;
679 sdl->sdl_alen = ifp->if_addrlen;
680 bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
683 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
686 ether_ioctl(ifp, command, data)
691 struct ifaddr *ifa = (struct ifaddr *) data;
692 struct ifreq *ifr = (struct ifreq *) data;
697 ifp->if_flags |= IFF_UP;
699 switch (ifa->ifa_addr->sa_family) {
702 ifp->if_init(ifp->if_softc); /* before arpwhohas */
703 arp_ifinit((struct arpcom *)ifp, ifa);
708 * XXX - This code is probably wrong
712 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
713 struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
715 if (ipx_nullhost(*ina))
720 bcopy((caddr_t) ina->x_host.c_host,
721 (caddr_t) ac->ac_enaddr,
722 sizeof(ac->ac_enaddr));
728 ifp->if_init(ifp->if_softc);
734 * XXX - This code is probably wrong
738 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
739 struct arpcom *ac = (struct arpcom *) (ifp->if_softc);
741 if (ns_nullhost(*ina))
743 *(union ns_host *) (ac->ac_enaddr);
745 bcopy((caddr_t) ina->x_host.c_host,
746 (caddr_t) ac->ac_enaddr,
747 sizeof(ac->ac_enaddr));
753 ifp->if_init(ifp->if_softc);
758 ifp->if_init(ifp->if_softc);
767 sa = (struct sockaddr *) & ifr->ifr_data;
768 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr,
769 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
775 * Set the interface MTU.
777 if (ifr->ifr_mtu > ETHERMTU) {
780 ifp->if_mtu = ifr->ifr_mtu;
788 ether_resolvemulti(ifp, llsa, sa)
790 struct sockaddr **llsa;
793 struct sockaddr_dl *sdl;
794 struct sockaddr_in *sin;
797 switch(sa->sa_family) {
800 * No mapping needed. Just check that it's a valid MC address.
802 sdl = (struct sockaddr_dl *)sa;
803 e_addr = LLADDR(sdl);
804 if ((e_addr[0] & 1) != 1)
805 return EADDRNOTAVAIL;
811 sin = (struct sockaddr_in *)sa;
812 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
813 return EADDRNOTAVAIL;
814 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
816 sdl->sdl_len = sizeof *sdl;
817 sdl->sdl_family = AF_LINK;
818 sdl->sdl_index = ifp->if_index;
819 sdl->sdl_type = IFT_ETHER;
821 sdl->sdl_alen = ETHER_ADDR_LEN;
823 e_addr = LLADDR(sdl);
824 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
825 *llsa = (struct sockaddr *)sdl;
831 * Well, the text isn't quite right, but it's the name