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>
65 #include <sys/malloc.h>
67 #include <sys/errno.h>
69 #include <sys/random.h>
70 #include <sys/rmlock.h>
71 #include <sys/sockio.h>
72 #include <sys/socket.h>
73 #include <sys/syslog.h>
74 #include <sys/libkern.h>
77 #include <net/if_types.h>
79 #include <net/netisr.h>
80 #include <net/route.h>
83 #include <netinet/in.h>
85 #include <netgraph/ng_message.h>
86 #include <netgraph/netgraph.h>
87 #include <netgraph/ng_parse.h>
88 #include <netgraph/ng_iface.h>
89 #include <netgraph/ng_cisco.h>
91 #ifdef NG_SEPARATE_MALLOC
92 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
94 #define M_NETGRAPH_IFACE M_NETGRAPH
97 /* This struct describes one address family */
99 sa_family_t family; /* Address family */
100 const char *hookname; /* Name for hook */
102 typedef const struct iffam *iffam_p;
104 /* List of address families supported by our interface */
105 const static struct iffam gFamilies[] = {
106 { AF_INET, NG_IFACE_HOOK_INET },
107 { AF_INET6, NG_IFACE_HOOK_INET6 },
108 { AF_APPLETALK, NG_IFACE_HOOK_ATALK },
109 { AF_IPX, NG_IFACE_HOOK_IPX },
110 { AF_ATM, NG_IFACE_HOOK_ATM },
111 { AF_NATM, NG_IFACE_HOOK_NATM },
113 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
115 /* Node private data */
116 struct ng_iface_private {
117 struct ifnet *ifp; /* Our interface */
118 int unit; /* Interface unit number */
119 node_p node; /* Our netgraph node */
120 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
121 struct rmlock lock; /* Protect private data changes */
123 typedef struct ng_iface_private *priv_p;
125 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t)
126 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t)
127 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock)
128 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock)
130 /* Interface methods */
131 static void ng_iface_start(struct ifnet *ifp);
132 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
133 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
134 const struct sockaddr *dst, struct route *ro);
135 static void ng_iface_bpftap(struct ifnet *ifp,
136 struct mbuf *m, sa_family_t family);
137 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
140 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
143 /* Netgraph methods */
144 static int ng_iface_mod_event(module_t, int, void *);
145 static ng_constructor_t ng_iface_constructor;
146 static ng_rcvmsg_t ng_iface_rcvmsg;
147 static ng_shutdown_t ng_iface_shutdown;
148 static ng_newhook_t ng_iface_newhook;
149 static ng_rcvdata_t ng_iface_rcvdata;
150 static ng_disconnect_t ng_iface_disconnect;
153 static iffam_p get_iffam_from_af(sa_family_t family);
154 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
155 static iffam_p get_iffam_from_name(const char *name);
156 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
158 /* Parse type for struct ng_cisco_ipaddr */
159 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
160 = NG_CISCO_IPADDR_TYPE_INFO;
161 static const struct ng_parse_type ng_cisco_ipaddr_type = {
162 &ng_parse_struct_type,
163 &ng_cisco_ipaddr_type_fields
166 /* List of commands and how to convert arguments to/from ASCII */
167 static const struct ng_cmdlist ng_iface_cmds[] = {
170 NGM_IFACE_GET_IFNAME,
173 &ng_parse_string_type
177 NGM_IFACE_POINT2POINT,
191 NGM_CISCO_GET_IPADDR,
194 &ng_cisco_ipaddr_type
198 NGM_IFACE_GET_IFINDEX,
201 &ng_parse_uint32_type
206 /* Node type descriptor */
207 static struct ng_type typestruct = {
208 .version = NG_ABI_VERSION,
209 .name = NG_IFACE_NODE_TYPE,
210 .mod_event = ng_iface_mod_event,
211 .constructor = ng_iface_constructor,
212 .rcvmsg = ng_iface_rcvmsg,
213 .shutdown = ng_iface_shutdown,
214 .newhook = ng_iface_newhook,
215 .rcvdata = ng_iface_rcvdata,
216 .disconnect = ng_iface_disconnect,
217 .cmdlist = ng_iface_cmds,
219 NETGRAPH_INIT(iface, &typestruct);
221 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
222 #define V_ng_iface_unit VNET(ng_iface_unit)
224 /************************************************************************
226 ************************************************************************/
229 * Get the family descriptor from the family ID
231 static __inline iffam_p
232 get_iffam_from_af(sa_family_t family)
237 for (k = 0; k < NUM_FAMILIES; k++) {
238 iffam = &gFamilies[k];
239 if (iffam->family == family)
246 * Get the family descriptor from the hook
248 static __inline iffam_p
249 get_iffam_from_hook(priv_p priv, hook_p hook)
253 for (k = 0; k < NUM_FAMILIES; k++)
254 if (priv->hooks[k] == hook)
255 return (&gFamilies[k]);
260 * Get the hook from the iffam descriptor
263 static __inline hook_p *
264 get_hook_from_iffam(priv_p priv, iffam_p iffam)
266 return (&priv->hooks[iffam - gFamilies]);
270 * Get the iffam descriptor from the name
272 static __inline iffam_p
273 get_iffam_from_name(const char *name)
278 for (k = 0; k < NUM_FAMILIES; k++) {
279 iffam = &gFamilies[k];
280 if (!strcmp(iffam->hookname, name))
286 /************************************************************************
288 ************************************************************************/
291 * Process an ioctl for the virtual interface
294 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
296 struct ifreq *const ifr = (struct ifreq *) data;
300 ng_iface_print_ioctl(ifp, command, data);
304 /* These two are mostly handled at a higher layer */
306 ifp->if_flags |= IFF_UP;
307 ifp->if_drv_flags |= IFF_DRV_RUNNING;
308 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
316 * If the interface is marked up and stopped, then start it.
317 * If it is marked down and running, then stop it.
319 if (ifr->ifr_flags & IFF_UP) {
320 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
321 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
322 ifp->if_drv_flags |= IFF_DRV_RUNNING;
325 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
326 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
331 /* Set the interface MTU */
333 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
334 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
337 ifp->if_mtu = ifr->ifr_mtu;
340 /* Stuff that's not supported */
357 * This routine is called to deliver a packet out the interface.
358 * We simply look at the address family and relay the packet to
359 * the corresponding hook, if it exists and is connected.
363 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
364 const struct sockaddr *dst, struct route *ro)
370 /* Check interface flags */
371 if (!((ifp->if_flags & IFF_UP) &&
372 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
377 /* Protect from deadly infinite recursion. */
379 while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
380 if (*(struct ifnet **)(mtag + 1) == ifp) {
381 log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
386 mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
392 *(struct ifnet **)(mtag + 1) = ifp;
393 m_tag_prepend(m, mtag);
395 /* BPF writes need to be handled specially. */
396 if (dst->sa_family == AF_UNSPEC)
397 bcopy(dst->sa_data, &af, sizeof(af));
401 /* Berkeley packet filter */
402 ng_iface_bpftap(ifp, m, af);
404 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
405 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
407 IFQ_LOCK(&ifp->if_snd);
408 IFQ_INC_DROPS(&ifp->if_snd);
409 IFQ_UNLOCK(&ifp->if_snd);
413 *(sa_family_t *)m->m_data = af;
414 error = (ifp->if_transmit)(ifp, m);
416 error = ng_iface_send(ifp, m, af);
422 * Start method is used only when ALTQ is enabled.
425 ng_iface_start(struct ifnet *ifp)
430 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
433 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
436 sa = *mtod(m, sa_family_t *);
437 m_adj(m, sizeof(sa_family_t));
438 ng_iface_send(ifp, m, sa);
443 * Flash a packet by the BPF (requires prepending 4 byte AF header)
444 * Note the phoney mbuf; this is OK because BPF treats it read-only.
447 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
449 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
450 if (bpf_peers_present(ifp->if_bpf)) {
451 int32_t family4 = (int32_t)family;
452 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
457 * This routine does actual delivery of the packet into the
458 * netgraph(4). It is called from ng_iface_start() and
462 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
464 struct rm_priotracker priv_tracker;
465 const priv_p priv = (priv_p) ifp->if_softc;
466 const iffam_p iffam = get_iffam_from_af(sa);
471 /* Check address family to determine hook (if known) */
474 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
475 return (EAFNOSUPPORT);
478 /* Copy length before the mbuf gets invalidated. */
479 len = m->m_pkthdr.len;
481 PRIV_RLOCK(priv, &priv_tracker);
482 hook = *get_hook_from_iffam(priv, iffam);
485 PRIV_RUNLOCK(priv, &priv_tracker);
489 PRIV_RUNLOCK(priv, &priv_tracker);
491 NG_OUTBOUND_THREAD_REF();
492 NG_SEND_DATA_ONLY(error, hook, m);
493 NG_OUTBOUND_THREAD_UNREF();
498 ifp->if_obytes += len;
507 * Display an ioctl to the virtual interface
511 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
515 switch (command & IOC_DIRMASK) {
531 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
536 IOCPARM_LEN(command));
540 /************************************************************************
542 ************************************************************************/
545 * Constructor for a node
548 ng_iface_constructor(node_p node)
553 /* Allocate node and interface private structures */
554 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
555 ifp = if_alloc(IFT_PROPVIRTUAL);
557 free(priv, M_NETGRAPH_IFACE);
561 rm_init(&priv->lock, "ng_iface private rmlock");
563 /* Link them together */
564 ifp->if_softc = priv;
567 /* Get an interface unit number */
568 priv->unit = alloc_unr(V_ng_iface_unit);
570 /* Link together node and private info */
571 NG_NODE_SET_PRIVATE(node, priv);
574 /* Initialize interface structure */
575 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
576 ifp->if_output = ng_iface_output;
577 ifp->if_start = ng_iface_start;
578 ifp->if_ioctl = ng_iface_ioctl;
579 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
580 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
581 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
582 ifp->if_addrlen = 0; /* XXX */
583 ifp->if_hdrlen = 0; /* XXX */
584 ifp->if_baudrate = 64000; /* XXX */
585 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
586 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
587 IFQ_SET_READY(&ifp->if_snd);
589 /* Give this node the same name as the interface (if possible) */
590 if (ng_name_node(node, ifp->if_xname) != 0)
591 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
594 /* Attach the interface */
596 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
603 * Give our ok for a hook to be added
606 ng_iface_newhook(node_p node, hook_p hook, const char *name)
608 const iffam_p iffam = get_iffam_from_name(name);
609 const priv_p priv = NG_NODE_PRIVATE(node);
613 return (EPFNOSUPPORT);
615 hookptr = get_hook_from_iffam(priv, iffam);
616 if (*hookptr != NULL) {
621 NG_HOOK_HI_STACK(hook);
622 NG_HOOK_SET_TO_INBOUND(hook);
628 * Receive a control message
631 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
633 const priv_p priv = NG_NODE_PRIVATE(node);
634 struct ifnet *const ifp = priv->ifp;
635 struct ng_mesg *resp = NULL;
639 NGI_GET_MSG(item, msg);
640 switch (msg->header.typecookie) {
641 case NGM_IFACE_COOKIE:
642 switch (msg->header.cmd) {
643 case NGM_IFACE_GET_IFNAME:
644 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
649 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
652 case NGM_IFACE_POINT2POINT:
653 case NGM_IFACE_BROADCAST:
656 /* Deny request if interface is UP */
657 if ((ifp->if_flags & IFF_UP) != 0)
661 switch (msg->header.cmd) {
662 case NGM_IFACE_POINT2POINT:
663 ifp->if_flags |= IFF_POINTOPOINT;
664 ifp->if_flags &= ~IFF_BROADCAST;
666 case NGM_IFACE_BROADCAST:
667 ifp->if_flags &= ~IFF_POINTOPOINT;
668 ifp->if_flags |= IFF_BROADCAST;
674 case NGM_IFACE_GET_IFINDEX:
675 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
680 *((uint32_t *)resp->data) = priv->ifp->if_index;
688 case NGM_CISCO_COOKIE:
689 switch (msg->header.cmd) {
690 case NGM_CISCO_GET_IPADDR: /* we understand this too */
694 /* Return the first configured IP address */
696 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
697 struct ng_cisco_ipaddr *ips;
699 if (ifa->ifa_addr->sa_family != AF_INET)
701 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
706 ips = (struct ng_cisco_ipaddr *)resp->data;
707 ips->ipaddr = ((struct sockaddr_in *)
708 ifa->ifa_addr)->sin_addr;
709 ips->netmask = ((struct sockaddr_in *)
710 ifa->ifa_netmask)->sin_addr;
713 if_addr_runlock(ifp);
715 /* No IP addresses on this interface? */
717 error = EADDRNOTAVAIL;
725 case NGM_FLOW_COOKIE:
726 switch (msg->header.cmd) {
728 ifp->if_drv_flags |= IFF_DRV_RUNNING;
730 case NGM_LINK_IS_DOWN:
731 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
741 NG_RESPOND_MSG(error, node, item, resp);
747 * Recive data from a hook. Pass the packet to the correct input routine.
750 ng_iface_rcvdata(hook_p hook, item_p item)
752 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
753 const iffam_p iffam = get_iffam_from_hook(priv, hook);
754 struct ifnet *const ifp = priv->ifp;
761 KASSERT(iffam != NULL, ("%s: iffam", __func__));
763 if ((ifp->if_flags & IFF_UP) == 0) {
768 /* Update interface stats */
770 ifp->if_ibytes += m->m_pkthdr.len;
772 /* Note receiving interface */
773 m->m_pkthdr.rcvif = ifp;
775 /* Berkeley packet filter */
776 ng_iface_bpftap(ifp, m, iffam->family);
779 switch (iffam->family) {
802 return (EAFNOSUPPORT);
804 if (harvest.point_to_point)
805 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG);
806 M_SETFIB(m, ifp->if_fib);
807 netisr_dispatch(isr, m);
812 * Shutdown and remove the node and its associated interface.
815 ng_iface_shutdown(node_p node)
817 const priv_p priv = NG_NODE_PRIVATE(node);
820 * The ifnet may be in a different vnet than the netgraph node,
821 * hence we have to change the current vnet context here.
823 CURVNET_SET_QUIET(priv->ifp->if_vnet);
824 bpfdetach(priv->ifp);
825 if_detach(priv->ifp);
829 free_unr(V_ng_iface_unit, priv->unit);
830 rm_destroy(&priv->lock);
831 free(priv, M_NETGRAPH_IFACE);
832 NG_NODE_SET_PRIVATE(node, NULL);
838 * Hook disconnection. Note that we do *not* shutdown when all
839 * hooks have been disconnected.
842 ng_iface_disconnect(hook_p hook)
844 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
845 const iffam_p iffam = get_iffam_from_hook(priv, hook);
848 panic("%s", __func__);
850 *get_hook_from_iffam(priv, iffam) = NULL;
856 * Handle loading and unloading for this node type.
859 ng_iface_mod_event(module_t mod, int event, void *data)
875 vnet_ng_iface_init(const void *unused)
878 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
880 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
881 vnet_ng_iface_init, NULL);
884 vnet_ng_iface_uninit(const void *unused)
887 delete_unrhdr(V_ng_iface_unit);
889 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
890 vnet_ng_iface_uninit, NULL);