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
37 #include "opt_atalk.h"
41 #include "opt_netgraph.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 #include <net/if_llc.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
66 #include <netipx/ipx.h>
67 #include <netipx/ipx_if.h>
72 #include <netns/ns_if.h>
74 int ether_outputdebug = 0;
75 int ether_inputdebug = 0;
79 #include <netiso/argo_debug.h>
80 #include <netiso/iso.h>
81 #include <netiso/iso_var.h>
82 #include <netiso/iso_snpac.h>
86 #include <netccitt/dll.h>
87 #include <netccitt/llc_var.h>
90 #if defined(LLC) && defined(CCITT)
91 extern struct ifqueue pkintrq;
95 #include <netatalk/at.h>
96 #include <netatalk/at_var.h>
97 #include <netatalk/at_extern.h>
99 #define llc_snap_org_code llc_un.type_snap.org_code
100 #define llc_snap_ether_type llc_un.type_snap.ether_type
102 extern u_char at_org_code[3];
103 extern u_char aarp_org_code[3];
104 #endif /* NETATALK */
107 #include <net/bridge.h>
112 #include <net/if_vlan_var.h>
113 #endif /* NVLAN > 0 */
115 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
117 u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
118 #define senderr(e) do { error = (e); goto bad;} while (0)
119 #define IFP2AC(IFP) ((struct arpcom *)IFP)
122 #include <netgraph/ng_ether.h>
123 #include <netgraph/ng_message.h>
124 #include <netgraph/netgraph.h>
126 static void ngether_init(void* ignored);
127 static void ngether_send(struct arpcom *ac,
128 struct ether_header *eh, struct mbuf *m);
129 static ng_constructor_t ngether_constructor;
130 static ng_rcvmsg_t ngether_rcvmsg;
131 static ng_shutdown_t ngether_rmnode;
132 static ng_newhook_t ngether_newhook;
133 static ng_connect_t ngether_connect;
134 static ng_rcvdata_t ngether_rcvdata;
135 static ng_disconnect_t ngether_disconnect;
137 static struct ng_type typestruct = {
152 #define AC2NG(AC) ((node_p)((AC)->ac_ng))
153 #define NGEF_DIVERT NGF_TYPE1 /* all packets sent to netgraph */
154 #endif /* NETGRAPH */
157 * Ethernet output routine.
158 * Encapsulate a packet of type family for the local net.
159 * Use trailer local net encapsulation if enough data in first
160 * packet leaves a multiple of 512 bytes of data in remainder.
161 * Assumes that ifp is actually pointer to arpcom structure.
164 ether_output(ifp, m0, dst, rt0)
165 register struct ifnet *ifp;
167 struct sockaddr *dst;
171 int s, error = 0, hdrcmplt = 0;
172 u_char esrc[6], edst[6];
173 register struct mbuf *m = m0;
174 register struct rtentry *rt;
175 register struct ether_header *eh;
176 int off, len = m->m_pkthdr.len, loop_copy = 0;
177 int hlen; /* link layer header lenght */
178 struct arpcom *ac = IFP2AC(ifp);
180 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
184 if ((rt->rt_flags & RTF_UP) == 0) {
185 rt0 = rt = rtalloc1(dst, 1, 0UL);
189 senderr(EHOSTUNREACH);
191 if (rt->rt_flags & RTF_GATEWAY) {
192 if (rt->rt_gwroute == 0)
194 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
195 rtfree(rt); rt = rt0;
196 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
198 if ((rt = rt->rt_gwroute) == 0)
199 senderr(EHOSTUNREACH);
202 if (rt->rt_flags & RTF_REJECT)
203 if (rt->rt_rmx.rmx_expire == 0 ||
204 time_second < rt->rt_rmx.rmx_expire)
205 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
207 hlen = ETHER_HDR_LEN;
208 switch (dst->sa_family) {
211 if (!arpresolve(ac, rt, m, dst, edst, rt0))
212 return (0); /* if not yet resolved */
213 off = m->m_pkthdr.len - m->m_len;
214 type = htons(ETHERTYPE_IP);
219 type = htons(ETHERTYPE_IPX);
220 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
221 (caddr_t)edst, sizeof (edst));
227 struct at_ifaddr *aa;
229 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
232 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
235 * In the phase 2 case, need to prepend an mbuf for the llc header.
236 * Since we must preserve the value of m, which is passed to us by
237 * value, we m_copy() the first mbuf, and use it for our llc header.
239 if ( aa->aa_flags & AFA_PHASE2 ) {
242 M_PREPEND(m, sizeof(struct llc), M_WAIT);
243 len += sizeof(struct llc);
244 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
245 llc.llc_control = LLC_UI;
246 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
247 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
248 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
249 type = htons(m->m_pkthdr.len);
250 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
252 type = htons(ETHERTYPE_AT);
261 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
264 case 0x0: /* Novell 802.3 */
265 type = htons( m->m_pkthdr.len);
267 case 0xe0e0: /* Novell 802.2 and Token-Ring */
268 M_PREPEND(m, 3, M_WAIT);
269 type = htons( m->m_pkthdr.len);
270 cp = mtod(m, u_char *);
276 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
277 (caddr_t)edst, sizeof (edst));
279 * XXX if ns_thishost is the same as the node's ethernet
280 * address then just the default code will catch this anyhow.
281 * So I'm not sure if this next clause should be here at all?
284 if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
285 m->m_pkthdr.rcvif = ifp;
286 schednetisr(NETISR_NS);
297 if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
298 m->m_flags |= M_BCAST;
306 register struct sockaddr_dl *sdl;
308 if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) &&
309 sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) {
310 bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst));
312 iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
313 (char *)edst, &snpalen))
314 goto bad; /* Not Resolved */
315 /* If broadcasting on a simplex interface, loopback a copy */
317 m->m_flags |= (M_BCAST|M_MCAST);
318 M_PREPEND(m, 3, M_DONTWAIT);
321 type = htons(m->m_pkthdr.len);
322 l = mtod(m, struct llc *);
323 l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
324 l->llc_control = LLC_UI;
328 printf("unoutput: sending pkt to: ");
330 printf("%x ", edst[i] & 0xff);
338 register struct sockaddr_dl *sdl =
339 (struct sockaddr_dl *) rt -> rt_gateway;
341 if (sdl && sdl->sdl_family == AF_LINK
342 && sdl->sdl_alen > 0) {
343 bcopy(LLADDR(sdl), (char *)edst, sizeof(edst));
344 } else goto bad; /* Not a link interface ? Funny ... */
347 type = htons(m->m_pkthdr.len);
351 register struct llc *l = mtod(m, struct llc *);
353 printf("ether_output: sending LLC2 pkt to: ");
355 printf("%x ", edst[i] & 0xff);
356 printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n",
357 type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff,
358 l->llc_control & 0xff);
361 #endif /* LLC_DEBUG */
365 case pseudo_AF_HDRCMPLT:
367 eh = (struct ether_header *)dst->sa_data;
368 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
372 loop_copy = -1; /* if this is for us, don't do it */
373 eh = (struct ether_header *)dst->sa_data;
374 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
375 type = eh->ether_type;
379 printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
381 senderr(EAFNOSUPPORT);
385 * Add local net header. If no space in first mbuf,
388 M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
391 eh = mtod(m, struct ether_header *);
392 (void)memcpy(&eh->ether_type, &type,
393 sizeof(eh->ether_type));
394 (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
396 (void)memcpy(eh->ether_shost, esrc,
397 sizeof(eh->ether_shost));
399 (void)memcpy(eh->ether_shost, ac->ac_enaddr,
400 sizeof(eh->ether_shost));
403 * If a simplex interface, and the packet is being sent to our
404 * Ethernet address or a broadcast address, loopback a copy.
405 * XXX To make a simplex device behave exactly like a duplex
406 * device, we should copy in the case of sending to our own
407 * ethernet address (thus letting the original actually appear
408 * on the wire). However, we don't do that here for security
409 * reasons and compatibility with the original behavior.
411 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
412 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
413 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
415 (void) if_simloop(ifp, n, dst, hlen);
416 } else if (bcmp(eh->ether_dhost,
417 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
418 (void) if_simloop(ifp, m, dst, hlen);
419 return (0); /* XXX */
424 struct mbuf *m0 = m ;
426 if (m->m_pkthdr.rcvif)
427 m->m_pkthdr.rcvif = NULL ;
428 ifp = bridge_dst_lookup(m);
429 bdg_forward(&m0, ifp);
437 * Queue message on interface, and start output if interface
440 if (IF_QFULL(&ifp->if_snd)) {
441 IF_DROP(&ifp->if_snd);
445 IF_ENQUEUE(&ifp->if_snd, m);
446 if ((ifp->if_flags & IFF_OACTIVE) == 0)
447 (*ifp->if_start)(ifp);
449 ifp->if_obytes += len + sizeof (struct ether_header);
450 if (m->m_flags & M_MCAST)
461 * Process a received Ethernet packet;
462 * the packet is in the mbuf chain m without
463 * the ether header, which is provided separately.
466 ether_input(ifp, eh, m)
468 register struct ether_header *eh;
471 register struct ifqueue *inq;
474 #if defined (ISO) || defined (LLC) || defined(NETATALK)
475 register struct llc *l;
478 if ((ifp->if_flags & IFF_UP) == 0) {
482 ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
483 if (eh->ether_dhost[0] & 1) {
484 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
485 sizeof(etherbroadcastaddr)) == 0)
486 m->m_flags |= M_BCAST;
488 m->m_flags |= M_MCAST;
490 if (m->m_flags & (M_BCAST|M_MCAST))
493 ether_type = ntohs(eh->ether_type);
497 struct arpcom *ac = IFP2AC(ifp);
498 if (AC2NG(ac) && (AC2NG(ac)->flags & NGEF_DIVERT)) {
499 ngether_send(ac, eh, m);
503 #endif /* NETGRAPH */
506 if (ether_type == vlan_proto) {
507 if (vlan_input(eh, m) < 0)
508 ifp->if_data.ifi_noproto++;
511 #endif /* NVLAN > 0 */
513 switch (ether_type) {
516 if (ipflow_fastforward(m))
518 schednetisr(NETISR_IP);
523 schednetisr(NETISR_ARP);
529 schednetisr(NETISR_IPX);
534 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
535 schednetisr(NETISR_NS);
542 schednetisr(NETISR_ATALK);
546 /* probably this should be done with a NETISR as well */
547 aarpinput(IFP2AC(ifp), m); /* XXX */
552 checksum = mtod(m, ushort *);
554 if ((ether_type <= ETHERMTU) &&
555 ((*checksum == 0xffff) || (*checksum == 0xE0E0))){
556 if(*checksum == 0xE0E0) {
557 m->m_pkthdr.len -= 3;
561 schednetisr(NETISR_NS);
566 #if defined (ISO) || defined (LLC) || defined(NETATALK)
567 if (ether_type > ETHERMTU)
569 l = mtod(m, struct llc *);
570 switch (l->llc_dsap) {
573 switch (l->llc_control) {
575 if (l->llc_ssap != LLC_SNAP_LSAP)
578 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
579 sizeof(at_org_code)) == 0 &&
580 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
582 m_adj( m, sizeof( struct llc ));
583 schednetisr(NETISR_ATALK);
587 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
588 sizeof(aarp_org_code)) == 0 &&
589 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
590 m_adj( m, sizeof( struct llc ));
591 aarpinput(IFP2AC(ifp), m); /* XXX */
602 switch (l->llc_control) {
604 /* LLC_UI_P forbidden in class 1 service */
605 if ((l->llc_dsap == LLC_ISO_LSAP) &&
606 (l->llc_ssap == LLC_ISO_LSAP)) {
608 if (m->m_pkthdr.len > ether_type)
609 m_adj(m, ether_type - m->m_pkthdr.len);
610 m->m_data += 3; /* XXX */
611 m->m_len -= 3; /* XXX */
612 m->m_pkthdr.len -= 3; /* XXX */
613 M_PREPEND(m, sizeof *eh, M_DONTWAIT);
616 *mtod(m, struct ether_header *) = *eh;
618 printf("clnp packet");
620 schednetisr(NETISR_ISO);
633 l->llc_dsap = l->llc_ssap = 0;
634 /* Fall through to */
639 register struct ether_header *eh2;
641 u_char c = l->llc_dsap;
643 l->llc_dsap = l->llc_ssap;
645 if (m->m_flags & (M_BCAST | M_MCAST))
646 bcopy((caddr_t)ac->ac_enaddr,
647 (caddr_t)eh->ether_dhost, 6);
648 sa.sa_family = AF_UNSPEC;
649 sa.sa_len = sizeof(sa);
650 eh2 = (struct ether_header *)sa.sa_data;
651 for (i = 0; i < 6; i++) {
652 eh2->ether_shost[i] = c = eh->ether_dhost[i];
653 eh2->ether_dhost[i] =
654 eh->ether_dhost[i] = eh->ether_shost[i];
655 eh->ether_shost[i] = c;
657 ifp->if_output(ifp, m, &sa, NULL);
669 if (m->m_pkthdr.len > ether_type)
670 m_adj(m, ether_type - m->m_pkthdr.len);
671 M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT);
674 if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP,
675 eh->ether_dhost, LLC_X25_LSAP, 6,
676 mtod(m, struct sdl_hdr *)))
677 panic("ETHER cons addr failure");
678 mtod(m, struct sdl_hdr *)->sdlhdr_len = ether_type;
680 printf("llc packet\n");
681 #endif /* LLC_DEBUG */
682 schednetisr(NETISR_CCITT);
690 ngether_send(IFP2AC(ifp), eh, m);
693 #endif /* NETGRAPH */
696 #else /* ISO || LLC || NETATALK */
698 ngether_send(IFP2AC(ifp), eh, m);
701 #endif /* NETGRAPH */
703 #endif /* ISO || LLC || NETATALK */
716 * Perform common duties while attaching to interface list
720 register struct ifnet *ifp;
722 register struct ifaddr *ifa;
723 register struct sockaddr_dl *sdl;
725 ifp->if_type = IFT_ETHER;
728 ifp->if_mtu = ETHERMTU;
729 ifp->if_resolvemulti = ether_resolvemulti;
730 if (ifp->if_baudrate == 0)
731 ifp->if_baudrate = 10000000;
732 ifa = ifnet_addrs[ifp->if_index - 1];
734 printf("ether_ifattach: no lladdr!\n");
737 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
738 sdl->sdl_type = IFT_ETHER;
739 sdl->sdl_alen = ifp->if_addrlen;
740 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
743 #endif /* NETGRAPH */
746 SYSCTL_DECL(_net_link);
747 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
750 ether_ioctl(ifp, command, data)
755 struct ifaddr *ifa = (struct ifaddr *) data;
756 struct ifreq *ifr = (struct ifreq *) data;
761 ifp->if_flags |= IFF_UP;
763 switch (ifa->ifa_addr->sa_family) {
766 ifp->if_init(ifp->if_softc); /* before arpwhohas */
767 arp_ifinit(IFP2AC(ifp), ifa);
772 * XXX - This code is probably wrong
776 register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
777 struct arpcom *ac = IFP2AC(ifp);
779 if (ipx_nullhost(*ina))
784 bcopy((caddr_t) ina->x_host.c_host,
785 (caddr_t) ac->ac_enaddr,
786 sizeof(ac->ac_enaddr));
792 ifp->if_init(ifp->if_softc);
798 * XXX - This code is probably wrong
802 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
803 struct arpcom *ac = IFP2AC(ifp);
805 if (ns_nullhost(*ina))
807 *(union ns_host *) (ac->ac_enaddr);
809 bcopy((caddr_t) ina->x_host.c_host,
810 (caddr_t) ac->ac_enaddr,
811 sizeof(ac->ac_enaddr));
817 ifp->if_init(ifp->if_softc);
822 ifp->if_init(ifp->if_softc);
831 sa = (struct sockaddr *) & ifr->ifr_data;
832 bcopy(IFP2AC(ifp)->ac_enaddr,
833 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
839 * Set the interface MTU.
841 if (ifr->ifr_mtu > ETHERMTU) {
844 ifp->if_mtu = ifr->ifr_mtu;
852 ether_resolvemulti(ifp, llsa, sa)
854 struct sockaddr **llsa;
857 struct sockaddr_dl *sdl;
858 struct sockaddr_in *sin;
861 switch(sa->sa_family) {
864 * No mapping needed. Just check that it's a valid MC address.
866 sdl = (struct sockaddr_dl *)sa;
867 e_addr = LLADDR(sdl);
868 if ((e_addr[0] & 1) != 1)
869 return EADDRNOTAVAIL;
875 sin = (struct sockaddr_in *)sa;
876 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
877 return EADDRNOTAVAIL;
878 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
880 sdl->sdl_len = sizeof *sdl;
881 sdl->sdl_family = AF_LINK;
882 sdl->sdl_index = ifp->if_index;
883 sdl->sdl_type = IFT_ETHER;
885 sdl->sdl_alen = ETHER_ADDR_LEN;
887 e_addr = LLADDR(sdl);
888 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
889 *llsa = (struct sockaddr *)sdl;
895 * Well, the text isn't quite right, but it's the name
904 /***********************************************************************
905 * This section contains the methods for the Netgraph interface
906 ***********************************************************************/
907 /* It's Ascii-art time!
908 * The ifnet is the first part of the arpcom which must be
909 * the first part of the device's softc.. yuk.
911 * +--------------------------+-----+---------+
912 * | struct ifnet (*ifp) | | |
914 * +--------------------------+ | |
915 * +--|[ac_ng] struct arpcom (*ac) | |
916 * | +--------------------------------+ |
917 * | | struct softc (*ifp->if_softc) (device) |
918 * | +------------------------------------------+
922 * | +----------------------+
923 * | | [private] [flags] |
924 * +------>| struct ng_node |
925 * | [hooks] | ** we only allow one hook
926 * +----------------------+
933 * | [private]|-- *unused*
938 * called during interface attaching
941 ngether_init(void *ifpvoid)
943 struct ifnet *ifp = ifpvoid;
944 struct arpcom *ac = IFP2AC(ifp);
945 static int ngether_done_init;
950 * we have found a node, make sure our 'type' is availabe.
952 if (ngether_done_init == 0) {
953 if (ng_newtype(&typestruct)) {
954 printf("ngether install failed\n");
957 ngether_done_init = 1;
959 if (ng_make_node_common(&typestruct, &node) != 0)
963 sprintf(namebuf, "%s%d", ifp->if_name, ifp->if_unit);
964 ng_name_node(AC2NG(ac), namebuf);
968 * It is not possible or allowable to create a node of this type.
969 * If the hardware exists, it will already have created it.
972 ngether_constructor(node_p *nodep)
978 * Give our ok for a hook to be added...
980 * Allow one hook at a time (rawdata).
981 * It can eiteh rdivert everything or only unclaimed packets.
984 ngether_newhook(node_p node, hook_p hook, const char *name)
987 /* check if there is already a hook */
988 if (LIST_FIRST(&(node->hooks)))
991 * Check for which mode hook we want.
993 if (strcmp(name, NG_ETHER_HOOK_ORPHAN) != 0) {
994 if (strcmp(name, NG_ETHER_HOOK_DIVERT) != 0) {
997 node->flags |= NGEF_DIVERT;
999 node->flags &= ~NGEF_DIVERT;
1005 * incoming messages.
1006 * Just respond to the generic TEXT_STATUS message
1009 ngether_rcvmsg(node_p node,
1010 struct ng_mesg *msg, const char *retaddr, struct ng_mesg **resp)
1015 ifp = node->private;
1016 switch (msg->header.typecookie) {
1017 case NGM_ETHER_COOKIE:
1020 case NGM_GENERIC_COOKIE:
1021 switch(msg->header.cmd) {
1022 case NGM_TEXT_STATUS: {
1025 int resplen = sizeof(struct ng_mesg) + 512;
1026 MALLOC(*resp, struct ng_mesg *, resplen,
1027 M_NETGRAPH, M_NOWAIT);
1028 if (*resp == NULL) {
1032 bzero(*resp, resplen);
1033 arg = (*resp)->data;
1036 * Put in the throughput information.
1038 pos = sprintf(arg, "%ld bytes in, %ld bytes out\n",
1039 ifp->if_ibytes, ifp->if_obytes);
1040 pos += sprintf(arg + pos,
1041 "%ld output errors\n",
1043 pos += sprintf(arg + pos,
1047 (*resp)->header.version = NG_VERSION;
1048 (*resp)->header.arglen = strlen(arg) + 1;
1049 (*resp)->header.token = msg->header.token;
1050 (*resp)->header.typecookie = NGM_ETHER_COOKIE;
1051 (*resp)->header.cmd = msg->header.cmd;
1052 strncpy((*resp)->header.cmdstr, "status",
1065 free(msg, M_NETGRAPH);
1070 * Receive a completed ethernet packet.
1071 * Queue it for output.
1074 ngether_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
1079 struct ether_header *eh;
1081 ifp = hook->node->private;
1083 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1086 * If a simplex interface, and the packet is being sent to our
1087 * Ethernet address or a broadcast address, loopback a copy.
1088 * XXX To make a simplex device behave exactly like a duplex
1089 * device, we should copy in the case of sending to our own
1090 * ethernet address (thus letting the original actually appear
1091 * on the wire). However, we don't do that here for security
1092 * reasons and compatibility with the original behavior.
1094 if (ifp->if_flags & IFF_SIMPLEX) {
1095 eh = mtod(m, struct ether_header *);
1096 if (m->m_flags & M_BCAST) {
1097 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
1099 ng_queue_data(hook, n, meta);
1100 } else if (bcmp(eh->ether_dhost,
1101 eh->ether_shost, ETHER_ADDR_LEN) == 0) {
1102 ng_queue_data(hook, m, meta);
1103 return (0); /* XXX */
1108 * Queue message on interface, and start output if interface
1110 * XXX if we lookead at the priority in the meta data we could
1111 * queue high priority items at the head.
1113 if (IF_QFULL(&ifp->if_snd)) {
1114 IF_DROP(&ifp->if_snd);
1118 IF_ENQUEUE(&ifp->if_snd, m);
1119 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1120 (*ifp->if_start)(ifp);
1122 ifp->if_obytes += m->m_pkthdr.len;
1123 if (m->m_flags & M_MCAST)
1128 NG_FREE_DATA(m, meta);
1133 * pass an mbuf out to the connected hook
1134 * More complicated than just an m_prepend, as it tries to save later nodes
1135 * from needing to do lots of m_pullups.
1138 ngether_send(struct arpcom *ac, struct ether_header *eh, struct mbuf *m)
1141 node_p node = AC2NG(ac);
1142 struct ether_header *eh2;
1144 if (node && LIST_FIRST(&(node->hooks))) {
1146 * Possibly the header is already on the front,
1148 eh2 = mtod(m, struct ether_header *) - 1;
1151 * This is the case so just move the markers back to
1152 * re-include it. We lucked out.
1153 * This allows us to avoid a yucky m_pullup
1154 * in later nodes if it works.
1156 m->m_len += sizeof(*eh);
1157 m->m_data -= sizeof(*eh);
1158 m->m_pkthdr.len += sizeof(*eh);
1161 * Alternatively there may be room even though
1162 * it is stored somewhere else. If so, copy it in.
1163 * This only safe because we KNOW that this packet has
1164 * just been generated by an ethernet card, so there
1165 * are no aliases to the buffer. (unlike in outgoing
1167 * Nearly all ethernet cards will end up producing mbufs
1168 * that fall into these cases. So we are not optimising
1172 if (m->m_flags & M_EXT) {
1173 room = (mtod(m, caddr_t) - m->m_ext.ext_buf);
1174 if (room > m->m_ext.ext_size) /* garbage */
1175 room = 0; /* fail immediatly */
1177 room = (mtod(m, caddr_t) - m->m_pktdat);
1179 if (room > sizeof (*eh)) {
1180 /* we have room, just copy it and adjust */
1181 m->m_len += sizeof(*eh);
1182 m->m_data -= sizeof(*eh);
1183 m->m_pkthdr.len += sizeof(*eh);
1184 bcopy ((caddr_t)eh, (caddr_t)eh2, sizeof(*eh));
1187 * Doing anything more is likely to get more
1188 * expensive than it's worth..
1189 * it's probable that everything else is in one
1190 * big lump. The next node will do an m_pullup()
1191 * for exactly the amount of data it needs and
1192 * hopefully everything after that will not
1193 * need one. So let's just use m_prepend.
1195 m = m_prepend(m, MHLEN, M_DONTWAIT);
1200 ng_queue_data(LIST_FIRST(&(node->hooks)), m, NULL);
1207 * do local shutdown processing..
1208 * This node will refuse to go away, unless the hardware says to..
1209 * don't unref the node, or remove our name. just clear our links up.
1212 ngether_rmnode(node_p node)
1215 node->flags &= ~NG_INVALID; /* bounce back to life */
1219 /* already linked */
1221 ngether_connect(hook_p hook)
1223 /* be really amiable and just say "YUP that's OK by me! " */
1228 * notify on hook disconnection (destruction)
1230 * For this type, removal of the last lins no effect. The interface can run
1232 * Since we have no per-hook information, this is rather simple.
1235 ngether_disconnect(hook_p hook)
1237 hook->node->flags &= ~NGEF_DIVERT;
1240 #endif /* NETGRAPH */
1242 /********************************** END *************************************/