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.55 1999/01/31 08:17:16 julian 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) do { error = (e); goto bad;} while (0)
118 #define IFP2AC(IFP) ((struct arpcom *)IFP)
121 * Ethernet output routine.
122 * Encapsulate a packet of type family for the local net.
123 * Use trailer local net encapsulation if enough data in first
124 * packet leaves a multiple of 512 bytes of data in remainder.
125 * Assumes that ifp is actually pointer to arpcom structure.
128 ether_output(ifp, m0, dst, rt0)
129 register struct ifnet *ifp;
131 struct sockaddr *dst;
137 register struct mbuf *m = m0;
138 register struct rtentry *rt;
139 register struct ether_header *eh;
140 int off, len = m->m_pkthdr.len, loop_copy = 0;
141 int hlen; /* link layer header lenght */
142 struct arpcom *ac = IFP2AC(ifp);
144 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
148 if ((rt->rt_flags & RTF_UP) == 0) {
149 rt0 = rt = rtalloc1(dst, 1, 0UL);
153 senderr(EHOSTUNREACH);
155 if (rt->rt_flags & RTF_GATEWAY) {
156 if (rt->rt_gwroute == 0)
158 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
159 rtfree(rt); rt = rt0;
160 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
162 if ((rt = rt->rt_gwroute) == 0)
163 senderr(EHOSTUNREACH);
166 if (rt->rt_flags & RTF_REJECT)
167 if (rt->rt_rmx.rmx_expire == 0 ||
168 time_second < rt->rt_rmx.rmx_expire)
169 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
171 hlen = ETHER_HDR_LEN;
172 switch (dst->sa_family) {
175 if (!arpresolve(ac, rt, m, dst, edst, rt0))
176 return (0); /* if not yet resolved */
177 off = m->m_pkthdr.len - m->m_len;
178 type = htons(ETHERTYPE_IP);
183 type = htons(ETHERTYPE_IPX);
184 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
185 (caddr_t)edst, sizeof (edst));
191 struct at_ifaddr *aa;
193 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
196 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
199 * In the phase 2 case, need to prepend an mbuf for the llc header.
200 * Since we must preserve the value of m, which is passed to us by
201 * value, we m_copy() the first mbuf, and use it for our llc header.
203 if ( aa->aa_flags & AFA_PHASE2 ) {
206 M_PREPEND(m, sizeof(struct llc), M_WAIT);
207 len += sizeof(struct llc);
208 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
209 llc.llc_control = LLC_UI;
210 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
211 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
212 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
213 type = htons(m->m_pkthdr.len);
214 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
216 type = htons(ETHERTYPE_AT);
225 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
228 case 0x0: /* Novell 802.3 */
229 type = htons( m->m_pkthdr.len);
231 case 0xe0e0: /* Novell 802.2 and Token-Ring */
232 M_PREPEND(m, 3, M_WAIT);
233 type = htons( m->m_pkthdr.len);
234 cp = mtod(m, u_char *);
240 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
241 (caddr_t)edst, sizeof (edst));
243 * XXX if ns_thishost is the same as the node's ethernet
244 * address then just the default code will catch this anyhow.
245 * So I'm not sure if this next clause should be here at all?
248 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
249 m->m_pkthdr.rcvif = ifp;
250 schednetisr(NETISR_NS);
261 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
262 m->m_flags |= M_BCAST;
270 register struct sockaddr_dl *sdl;
272 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
273 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
274 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
276 iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
277 (char *)edst, &snpalen))
278 goto bad; /* Not Resolved */
279 /* If broadcasting on a simplex interface, loopback a copy */
281 m->m_flags |= (M_BCAST|M_MCAST);
282 M_PREPEND(m, 3, M_DONTWAIT);
285 type = htons(m->m_pkthdr.len);
286 l = mtod(m, struct llc *);
287 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
288 l->llc_control = LLC_UI;
292 printf("unoutput: sending pkt to: ");
294 printf("%x ", edst[i] & 0xff);
302 register struct sockaddr_dl *sdl =
303 (struct sockaddr_dl *) rt -> rt_gateway;
305 if (sdl && sdl->sdl_family == AF_LINK
306 && sdl->sdl_alen > 0) {
307 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst));
308 } else goto bad; /* Not a link interface ? Funny ... */
311 type = htons(m->m_pkthdr.len);
315 register struct llc *l = mtod(m, struct llc *);
317 printf("ether_output: sending LLC2 pkt to: ");
319 printf("%x ", edst[i] & 0xff);
320 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
321 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
322 l->llc_control & 0xff);
325 #endif /* LLC_DEBUG */
330 loop_copy = -1; /* if this is for us, don't do it */
331 eh = (struct ether_header *)dst->sa_data;
332 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
333 type = eh->ether_type;
337 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
339 senderr(EAFNOSUPPORT);
343 * Add local net header. If no space in first mbuf,
346 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
349 eh = mtod(m, struct ether_header *);
350 (void)memcpy(&eh->ether_type, &type,
351 sizeof(eh->ether_type));
352 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
353 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
354 sizeof(eh->ether_shost));
357 * If a simplex interface, and the packet is being sent to our
358 * Ethernet address or a broadcast address, loopback a copy.
359 * XXX To make a simplex device behave exactly like a duplex
360 * device, we should copy in the case of sending to our own
361 * ethernet address (thus letting the original actually appear
362 * on the wire). However, we don't do that here for security
363 * reasons and compatibility with the original behavior.
365 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
366 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
367 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
369 (void) if_simloop(ifp, n, dst, hlen);
370 } else if (bcmp(eh->ether_dhost,
371 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
372 (void) if_simloop(ifp, m, dst, hlen);
373 return (0); /* XXX */
378 struct mbuf *m0 = m ;
380 if (m->m_pkthdr.rcvif)
381 m->m_pkthdr.rcvif = NULL ;
382 ifp = bridge_dst_lookup(m);
383 bdg_forward(&m0, ifp);
391 * Queue message on interface, and start output if interface
394 if (IF_QFULL(&ifp->if_snd)) {
395 IF_DROP(&ifp->if_snd);
399 IF_ENQUEUE(&ifp->if_snd, m);
400 if ((ifp->if_flags & IFF_OACTIVE) == 0)
401 (*ifp->if_start)(ifp);
403 ifp->if_obytes += len + sizeof (struct ether_header);
404 if (m->m_flags & M_MCAST)
415 * Process a received Ethernet packet;
416 * the packet is in the mbuf chain m without
417 * the ether header, which is provided separately.
420 ether_input(ifp, eh, m)
422 register struct ether_header *eh;
425 register struct ifqueue *inq;
428 #if defined (ISO) || defined (LLC) || defined(NETATALK)
429 register struct llc *l;
432 if ((ifp->if_flags & IFF_UP) == 0) {
436 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
437 if (eh->ether_dhost[0] & 1) {
438 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
439 sizeof(etherbroadcastaddr)) == 0)
440 m->m_flags |= M_BCAST;
442 m->m_flags |= M_MCAST;
444 if (m->m_flags & (M_BCAST|M_MCAST))
447 ether_type = ntohs(eh->ether_type);
450 if (ether_type == vlan_proto) {
451 if (vlan_input(eh, m) < 0)
452 ifp->if_data.ifi_noproto++;
455 #endif /* NVLAN > 0 */
457 switch (ether_type) {
460 if (ipflow_fastforward(m))
462 schednetisr(NETISR_IP);
467 schednetisr(NETISR_ARP);
473 schednetisr(NETISR_IPX);
478 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
479 schednetisr(NETISR_NS);
486 schednetisr(NETISR_ATALK);
490 /* probably this should be done with a NETISR as well */
491 aarpinput(IFP2AC(ifp), m); /* XXX */
496 checksum = mtod(m, ushort *);
498 if ((ether_type <= ETHERMTU) &&
499 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){
500 if(*checksum == 0xE0E0) {
501 m->m_pkthdr.len -= 3;
505 schednetisr(NETISR_NS);
510 #if defined (ISO) || defined (LLC) || defined(NETATALK)
511 if (ether_type > ETHERMTU)
513 l = mtod(m, struct llc *);
514 switch (l->llc_dsap) {
517 switch (l->llc_control) {
519 if (l->llc_ssap != LLC_SNAP_LSAP)
522 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
523 sizeof(at_org_code)) == 0 &&
524 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
526 m_adj( m, sizeof( struct llc ));
527 schednetisr(NETISR_ATALK);
531 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
532 sizeof(aarp_org_code)) == 0 &&
533 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
534 m_adj( m, sizeof( struct llc ));
535 aarpinput(IFP2AC(ifp), m); /* XXX */
546 switch (l->llc_control) {
548 /* LLC_UI_P forbidden in class 1 service */
549 if ((l->llc_dsap == LLC_ISO_LSAP) &&
550 (l->llc_ssap == LLC_ISO_LSAP)) {
552 if (m->m_pkthdr.len > ether_type)
553 m_adj(m, ether_type - m->m_pkthdr.len);
554 m->m_data += 3; /* XXX */
555 m->m_len -= 3; /* XXX */
556 m->m_pkthdr.len -= 3; /* XXX */
557 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
560 *mtod(m, struct ether_header *) = *eh;
562 printf("clnp packet");
564 schednetisr(NETISR_ISO);
577 l->llc_dsap = l->llc_ssap = 0;
578 /* Fall through to */
583 register struct ether_header *eh2;
585 u_char c = l->llc_dsap;
587 l->llc_dsap = l->llc_ssap;
589 if (m->m_flags & (M_BCAST | M_MCAST))
590 bcopy((caddr_t)ac->ac_enaddr,
591 (caddr_t)eh->ether_dhost, 6);
592 sa.sa_family = AF_UNSPEC;
593 sa.sa_len = sizeof(sa);
594 eh2 = (struct ether_header *)sa.sa_data;
595 for (i = 0; i < 6; i++) {
596 eh2->ether_shost[i] = c = eh->ether_dhost[i];
597 eh2->ether_dhost[i] =
598 eh->ether_dhost[i] = eh->ether_shost[i];
599 eh->ether_shost[i] = c;
601 ifp->if_output(ifp, m, &sa, NULL);
613 if (m->m_pkthdr.len > ether_type)
614 m_adj(m, ether_type - m->m_pkthdr.len);
615 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
618 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
619 eh->ether_dhost, LLC_X25_LSAP, 6,
620 mtod(m, struct sdl_hdr *)))
621 panic("ETHER cons addr failure");
622 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
624 printf("llc packet\n");
625 #endif /* LLC_DEBUG */
626 schednetisr(NETISR_CCITT);
636 #else /* ISO || LLC || NETATALK */
639 #endif /* ISO || LLC || NETATALK */
652 * Perform common duties while attaching to interface list
656 register struct ifnet *ifp;
658 register struct ifaddr *ifa;
659 register struct sockaddr_dl *sdl;
661 ifp->if_type = IFT_ETHER;
664 ifp->if_mtu = ETHERMTU;
665 ifp->if_resolvemulti = ether_resolvemulti;
666 if (ifp->if_baudrate == 0)
667 ifp->if_baudrate = 10000000;
668 ifa = ifnet_addrs[ifp->if_index - 1];
670 printf("ether_ifattach: no lladdr!\n");
673 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
674 sdl->sdl_type = IFT_ETHER;
675 sdl->sdl_alen = ifp->if_addrlen;
676 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
679 #endif /* NETGRAPH */
682 SYSCTL_DECL(_net_link);
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(IFP2AC(ifp), ifa);
708 * XXX - This code is probably wrong
712 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
713 struct arpcom *ac = IFP2AC(ifp);
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 = IFP2AC(ifp);
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(IFP2AC(ifp)->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