6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
38 * Author: Archie Cobbs <archie@freebsd.org>
41 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
45 * This node is also a system networking interface. It has
46 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
47 * are simply relayed between the interface and the hooks.
49 * Interfaces are named ng0, ng1, etc. New nodes take the
50 * first available interface name.
52 * This node also includes Berkeley packet filter support.
55 #include "opt_atalk.h"
57 #include "opt_inet6.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/errno.h>
68 #include <sys/random.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/syslog.h>
72 #include <sys/libkern.h>
75 #include <net/if_types.h>
77 #include <net/netisr.h>
78 #include <net/route.h>
81 #include <netinet/in.h>
83 #include <netgraph/ng_message.h>
84 #include <netgraph/netgraph.h>
85 #include <netgraph/ng_parse.h>
86 #include <netgraph/ng_iface.h>
87 #include <netgraph/ng_cisco.h>
89 #ifdef NG_SEPARATE_MALLOC
90 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
92 #define M_NETGRAPH_IFACE M_NETGRAPH
95 /* This struct describes one address family */
97 sa_family_t family; /* Address family */
98 const char *hookname; /* Name for hook */
100 typedef const struct iffam *iffam_p;
102 /* List of address families supported by our interface */
103 const static struct iffam gFamilies[] = {
104 { AF_INET, NG_IFACE_HOOK_INET },
105 { AF_INET6, NG_IFACE_HOOK_INET6 },
106 { AF_APPLETALK, NG_IFACE_HOOK_ATALK },
107 { AF_IPX, NG_IFACE_HOOK_IPX },
108 { AF_ATM, NG_IFACE_HOOK_ATM },
109 { AF_NATM, NG_IFACE_HOOK_NATM },
111 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
113 /* Node private data */
114 struct ng_iface_private {
115 struct ifnet *ifp; /* Our interface */
116 int unit; /* Interface unit number */
117 node_p node; /* Our netgraph node */
118 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
120 typedef struct ng_iface_private *priv_p;
122 /* Interface methods */
123 static void ng_iface_start(struct ifnet *ifp);
124 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
125 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
126 struct sockaddr *dst, struct route *ro);
127 static void ng_iface_bpftap(struct ifnet *ifp,
128 struct mbuf *m, sa_family_t family);
129 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
132 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
135 /* Netgraph methods */
136 static int ng_iface_mod_event(module_t, int, void *);
137 static ng_constructor_t ng_iface_constructor;
138 static ng_rcvmsg_t ng_iface_rcvmsg;
139 static ng_shutdown_t ng_iface_shutdown;
140 static ng_newhook_t ng_iface_newhook;
141 static ng_rcvdata_t ng_iface_rcvdata;
142 static ng_disconnect_t ng_iface_disconnect;
145 static iffam_p get_iffam_from_af(sa_family_t family);
146 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
147 static iffam_p get_iffam_from_name(const char *name);
148 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
150 /* Parse type for struct ng_cisco_ipaddr */
151 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
152 = NG_CISCO_IPADDR_TYPE_INFO;
153 static const struct ng_parse_type ng_cisco_ipaddr_type = {
154 &ng_parse_struct_type,
155 &ng_cisco_ipaddr_type_fields
158 /* List of commands and how to convert arguments to/from ASCII */
159 static const struct ng_cmdlist ng_iface_cmds[] = {
162 NGM_IFACE_GET_IFNAME,
165 &ng_parse_string_type
169 NGM_IFACE_POINT2POINT,
183 NGM_CISCO_GET_IPADDR,
186 &ng_cisco_ipaddr_type
190 NGM_IFACE_GET_IFINDEX,
193 &ng_parse_uint32_type
198 /* Node type descriptor */
199 static struct ng_type typestruct = {
200 .version = NG_ABI_VERSION,
201 .name = NG_IFACE_NODE_TYPE,
202 .mod_event = ng_iface_mod_event,
203 .constructor = ng_iface_constructor,
204 .rcvmsg = ng_iface_rcvmsg,
205 .shutdown = ng_iface_shutdown,
206 .newhook = ng_iface_newhook,
207 .rcvdata = ng_iface_rcvdata,
208 .disconnect = ng_iface_disconnect,
209 .cmdlist = ng_iface_cmds,
211 NETGRAPH_INIT(iface, &typestruct);
213 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
214 #define V_ng_iface_unit VNET(ng_iface_unit)
216 /************************************************************************
218 ************************************************************************/
221 * Get the family descriptor from the family ID
223 static __inline iffam_p
224 get_iffam_from_af(sa_family_t family)
229 for (k = 0; k < NUM_FAMILIES; k++) {
230 iffam = &gFamilies[k];
231 if (iffam->family == family)
238 * Get the family descriptor from the hook
240 static __inline iffam_p
241 get_iffam_from_hook(priv_p priv, hook_p hook)
245 for (k = 0; k < NUM_FAMILIES; k++)
246 if (priv->hooks[k] == hook)
247 return (&gFamilies[k]);
252 * Get the hook from the iffam descriptor
255 static __inline hook_p *
256 get_hook_from_iffam(priv_p priv, iffam_p iffam)
258 return (&priv->hooks[iffam - gFamilies]);
262 * Get the iffam descriptor from the name
264 static __inline iffam_p
265 get_iffam_from_name(const char *name)
270 for (k = 0; k < NUM_FAMILIES; k++) {
271 iffam = &gFamilies[k];
272 if (!strcmp(iffam->hookname, name))
278 /************************************************************************
280 ************************************************************************/
283 * Process an ioctl for the virtual interface
286 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
288 struct ifreq *const ifr = (struct ifreq *) data;
292 ng_iface_print_ioctl(ifp, command, data);
297 /* These two are mostly handled at a higher layer */
299 ifp->if_flags |= IFF_UP;
300 ifp->if_drv_flags |= IFF_DRV_RUNNING;
301 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
309 * If the interface is marked up and stopped, then start it.
310 * If it is marked down and running, then stop it.
312 if (ifr->ifr_flags & IFF_UP) {
313 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
314 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
315 ifp->if_drv_flags |= IFF_DRV_RUNNING;
318 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
319 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
324 /* Set the interface MTU */
326 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
327 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
330 ifp->if_mtu = ifr->ifr_mtu;
333 /* Stuff that's not supported */
351 * This routine is called to deliver a packet out the interface.
352 * We simply look at the address family and relay the packet to
353 * the corresponding hook, if it exists and is connected.
357 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
358 struct sockaddr *dst, struct route *ro)
364 /* Check interface flags */
365 if (!((ifp->if_flags & IFF_UP) &&
366 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
371 /* Protect from deadly infinite recursion. */
373 while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
374 if (*(struct ifnet **)(mtag + 1) == ifp) {
375 log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
380 mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
386 *(struct ifnet **)(mtag + 1) = ifp;
387 m_tag_prepend(m, mtag);
389 /* BPF writes need to be handled specially. */
390 if (dst->sa_family == AF_UNSPEC) {
391 bcopy(dst->sa_data, &af, sizeof(af));
395 /* Berkeley packet filter */
396 ng_iface_bpftap(ifp, m, dst->sa_family);
398 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
399 M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
401 IFQ_LOCK(&ifp->if_snd);
402 IFQ_INC_DROPS(&ifp->if_snd);
403 IFQ_UNLOCK(&ifp->if_snd);
407 *(sa_family_t *)m->m_data = dst->sa_family;
408 error = (ifp->if_transmit)(ifp, m);
410 error = ng_iface_send(ifp, m, dst->sa_family);
416 * Start method is used only when ALTQ is enabled.
419 ng_iface_start(struct ifnet *ifp)
424 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
427 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
430 sa = *mtod(m, sa_family_t *);
431 m_adj(m, sizeof(sa_family_t));
432 ng_iface_send(ifp, m, sa);
437 * Flash a packet by the BPF (requires prepending 4 byte AF header)
438 * Note the phoney mbuf; this is OK because BPF treats it read-only.
441 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
443 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
444 if (bpf_peers_present(ifp->if_bpf)) {
445 int32_t family4 = (int32_t)family;
446 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
451 * This routine does actual delivery of the packet into the
452 * netgraph(4). It is called from ng_iface_start() and
456 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
458 const priv_p priv = (priv_p) ifp->if_softc;
459 const iffam_p iffam = get_iffam_from_af(sa);
463 /* Check address family to determine hook (if known) */
466 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
467 return (EAFNOSUPPORT);
470 /* Copy length before the mbuf gets invalidated. */
471 len = m->m_pkthdr.len;
473 /* Send packet. If hook is not connected, mbuf will get freed. */
474 NG_OUTBOUND_THREAD_REF();
475 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
476 NG_OUTBOUND_THREAD_UNREF();
480 ifp->if_obytes += len;
489 * Display an ioctl to the virtual interface
493 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
497 switch (command & IOC_DIRMASK) {
513 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
518 IOCPARM_LEN(command));
522 /************************************************************************
524 ************************************************************************/
527 * Constructor for a node
530 ng_iface_constructor(node_p node)
535 /* Allocate node and interface private structures */
536 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
539 ifp = if_alloc(IFT_PROPVIRTUAL);
541 free(priv, M_NETGRAPH_IFACE);
545 /* Link them together */
546 ifp->if_softc = priv;
549 /* Get an interface unit number */
550 priv->unit = alloc_unr(V_ng_iface_unit);
552 /* Link together node and private info */
553 NG_NODE_SET_PRIVATE(node, priv);
556 /* Initialize interface structure */
557 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
558 ifp->if_output = ng_iface_output;
559 ifp->if_start = ng_iface_start;
560 ifp->if_ioctl = ng_iface_ioctl;
561 ifp->if_watchdog = NULL;
562 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
563 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
564 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
565 ifp->if_addrlen = 0; /* XXX */
566 ifp->if_hdrlen = 0; /* XXX */
567 ifp->if_baudrate = 64000; /* XXX */
568 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
569 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
570 IFQ_SET_READY(&ifp->if_snd);
572 /* Give this node the same name as the interface (if possible) */
573 if (ng_name_node(node, ifp->if_xname) != 0)
574 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
577 /* Attach the interface */
579 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
586 * Give our ok for a hook to be added
589 ng_iface_newhook(node_p node, hook_p hook, const char *name)
591 const iffam_p iffam = get_iffam_from_name(name);
595 return (EPFNOSUPPORT);
596 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
597 if (*hookptr != NULL)
600 NG_HOOK_HI_STACK(hook);
601 NG_HOOK_SET_TO_INBOUND(hook);
606 * Receive a control message
609 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
611 const priv_p priv = NG_NODE_PRIVATE(node);
612 struct ifnet *const ifp = priv->ifp;
613 struct ng_mesg *resp = NULL;
617 NGI_GET_MSG(item, msg);
618 switch (msg->header.typecookie) {
619 case NGM_IFACE_COOKIE:
620 switch (msg->header.cmd) {
621 case NGM_IFACE_GET_IFNAME:
622 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
627 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
630 case NGM_IFACE_POINT2POINT:
631 case NGM_IFACE_BROADCAST:
634 /* Deny request if interface is UP */
635 if ((ifp->if_flags & IFF_UP) != 0)
639 switch (msg->header.cmd) {
640 case NGM_IFACE_POINT2POINT:
641 ifp->if_flags |= IFF_POINTOPOINT;
642 ifp->if_flags &= ~IFF_BROADCAST;
644 case NGM_IFACE_BROADCAST:
645 ifp->if_flags &= ~IFF_POINTOPOINT;
646 ifp->if_flags |= IFF_BROADCAST;
652 case NGM_IFACE_GET_IFINDEX:
653 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
658 *((uint32_t *)resp->data) = priv->ifp->if_index;
666 case NGM_CISCO_COOKIE:
667 switch (msg->header.cmd) {
668 case NGM_CISCO_GET_IPADDR: /* we understand this too */
672 /* Return the first configured IP address */
674 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
675 struct ng_cisco_ipaddr *ips;
677 if (ifa->ifa_addr->sa_family != AF_INET)
679 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
684 ips = (struct ng_cisco_ipaddr *)resp->data;
685 ips->ipaddr = ((struct sockaddr_in *)
686 ifa->ifa_addr)->sin_addr;
687 ips->netmask = ((struct sockaddr_in *)
688 ifa->ifa_netmask)->sin_addr;
691 if_addr_runlock(ifp);
693 /* No IP addresses on this interface? */
695 error = EADDRNOTAVAIL;
703 case NGM_FLOW_COOKIE:
704 switch (msg->header.cmd) {
706 ifp->if_drv_flags |= IFF_DRV_RUNNING;
708 case NGM_LINK_IS_DOWN:
709 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
719 NG_RESPOND_MSG(error, node, item, resp);
725 * Recive data from a hook. Pass the packet to the correct input routine.
728 ng_iface_rcvdata(hook_p hook, item_p item)
730 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
731 const iffam_p iffam = get_iffam_from_hook(priv, hook);
732 struct ifnet *const ifp = priv->ifp;
739 KASSERT(iffam != NULL, ("%s: iffam", __func__));
741 if ((ifp->if_flags & IFF_UP) == 0) {
746 /* Update interface stats */
748 ifp->if_ibytes += m->m_pkthdr.len;
750 /* Note receiving interface */
751 m->m_pkthdr.rcvif = ifp;
753 /* Berkeley packet filter */
754 ng_iface_bpftap(ifp, m, iffam->family);
757 switch (iffam->family) {
780 return (EAFNOSUPPORT);
782 /* First chunk of an mbuf contains good junk */
783 if (harvest.point_to_point)
784 random_harvest(m, 16, 3, 0, RANDOM_NET);
785 netisr_dispatch(isr, m);
790 * Shutdown and remove the node and its associated interface.
793 ng_iface_shutdown(node_p node)
795 const priv_p priv = NG_NODE_PRIVATE(node);
798 * The ifnet may be in a different vnet than the netgraph node,
799 * hence we have to change the current vnet context here.
801 CURVNET_SET_QUIET(priv->ifp->if_vnet);
802 bpfdetach(priv->ifp);
803 if_detach(priv->ifp);
807 free_unr(V_ng_iface_unit, priv->unit);
808 free(priv, M_NETGRAPH_IFACE);
809 NG_NODE_SET_PRIVATE(node, NULL);
815 * Hook disconnection. Note that we do *not* shutdown when all
816 * hooks have been disconnected.
819 ng_iface_disconnect(hook_p hook)
821 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
822 const iffam_p iffam = get_iffam_from_hook(priv, hook);
826 *get_hook_from_iffam(priv, iffam) = NULL;
831 * Handle loading and unloading for this node type.
834 ng_iface_mod_event(module_t mod, int event, void *data)
850 vnet_ng_iface_init(const void *unused)
853 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
855 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
856 vnet_ng_iface_init, NULL);
859 vnet_ng_iface_uninit(const void *unused)
862 delete_unrhdr(V_ng_iface_unit);
864 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
865 vnet_ng_iface_uninit, NULL);