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>
39 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
43 * This node is also a system networking interface. It has
44 * a hook for each protocol (IP, AppleTalk, etc). Packets
45 * are simply relayed between the interface and the hooks.
47 * Interfaces are named ng0, ng1, etc. New nodes take the
48 * first available interface name.
50 * This node also includes Berkeley packet filter support.
54 #include "opt_inet6.h"
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/errno.h>
59 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/errno.h>
65 #include <sys/random.h>
66 #include <sys/rmlock.h>
67 #include <sys/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
74 #include <net/if_var.h>
75 #include <net/if_private.h>
76 #include <net/if_types.h>
78 #include <net/netisr.h>
79 #include <net/route.h>
82 #include <netinet/in.h>
84 #include <netgraph/ng_message.h>
85 #include <netgraph/netgraph.h>
86 #include <netgraph/ng_parse.h>
87 #include <netgraph/ng_iface.h>
89 #ifdef NG_SEPARATE_MALLOC
90 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
92 #define M_NETGRAPH_IFACE M_NETGRAPH
95 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
96 "Point to point netgraph interface");
97 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
98 #define V_ng_iface_max_nest VNET(ng_iface_max_nest)
99 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
100 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
102 /* This struct describes one address family */
104 sa_family_t family; /* Address family */
105 const char *hookname; /* Name for hook */
107 typedef const struct iffam *iffam_p;
109 /* List of address families supported by our interface */
110 const static struct iffam gFamilies[] = {
111 { AF_INET, NG_IFACE_HOOK_INET },
112 { AF_INET6, NG_IFACE_HOOK_INET6 },
114 #define NUM_FAMILIES nitems(gFamilies)
116 /* Node private data */
117 struct ng_iface_private {
118 struct ifnet *ifp; /* Our interface */
119 int unit; /* Interface unit number */
120 node_p node; /* Our netgraph node */
121 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
122 struct rmlock lock; /* Protect private data changes */
124 typedef struct ng_iface_private *priv_p;
126 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t)
127 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t)
128 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock)
129 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock)
131 /* Interface methods */
132 static void ng_iface_start(struct ifnet *ifp);
133 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
134 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
135 const struct sockaddr *dst, struct route *ro);
136 static void ng_iface_bpftap(struct ifnet *ifp,
137 struct mbuf *m, sa_family_t family);
138 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
141 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
144 /* Netgraph methods */
145 static int ng_iface_mod_event(module_t, int, void *);
146 static ng_constructor_t ng_iface_constructor;
147 static ng_rcvmsg_t ng_iface_rcvmsg;
148 static ng_shutdown_t ng_iface_shutdown;
149 static ng_newhook_t ng_iface_newhook;
150 static ng_rcvdata_t ng_iface_rcvdata;
151 static ng_disconnect_t ng_iface_disconnect;
154 static iffam_p get_iffam_from_af(sa_family_t family);
155 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
156 static iffam_p get_iffam_from_name(const char *name);
157 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
159 /* List of commands and how to convert arguments to/from ASCII */
160 static const struct ng_cmdlist ng_iface_cmds[] = {
163 NGM_IFACE_GET_IFNAME,
166 &ng_parse_string_type
170 NGM_IFACE_POINT2POINT,
184 NGM_IFACE_GET_IFINDEX,
187 &ng_parse_uint32_type
192 /* Node type descriptor */
193 static struct ng_type typestruct = {
194 .version = NG_ABI_VERSION,
195 .name = NG_IFACE_NODE_TYPE,
196 .mod_event = ng_iface_mod_event,
197 .constructor = ng_iface_constructor,
198 .rcvmsg = ng_iface_rcvmsg,
199 .shutdown = ng_iface_shutdown,
200 .newhook = ng_iface_newhook,
201 .rcvdata = ng_iface_rcvdata,
202 .disconnect = ng_iface_disconnect,
203 .cmdlist = ng_iface_cmds,
205 NETGRAPH_INIT(iface, &typestruct);
207 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
208 #define V_ng_iface_unit VNET(ng_iface_unit)
210 /************************************************************************
212 ************************************************************************/
215 * Get the family descriptor from the family ID
217 static __inline iffam_p
218 get_iffam_from_af(sa_family_t family)
223 for (k = 0; k < NUM_FAMILIES; k++) {
224 iffam = &gFamilies[k];
225 if (iffam->family == family)
232 * Get the family descriptor from the hook
234 static __inline iffam_p
235 get_iffam_from_hook(priv_p priv, hook_p hook)
239 for (k = 0; k < NUM_FAMILIES; k++)
240 if (priv->hooks[k] == hook)
241 return (&gFamilies[k]);
246 * Get the hook from the iffam descriptor
249 static __inline hook_p *
250 get_hook_from_iffam(priv_p priv, iffam_p iffam)
252 return (&priv->hooks[iffam - gFamilies]);
256 * Get the iffam descriptor from the name
258 static __inline iffam_p
259 get_iffam_from_name(const char *name)
264 for (k = 0; k < NUM_FAMILIES; k++) {
265 iffam = &gFamilies[k];
266 if (!strcmp(iffam->hookname, name))
272 /************************************************************************
274 ************************************************************************/
277 * Process an ioctl for the virtual interface
280 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
282 struct ifreq *const ifr = (struct ifreq *) data;
286 ng_iface_print_ioctl(ifp, command, data);
289 /* These two are mostly handled at a higher layer */
291 ifp->if_flags |= IFF_UP;
292 ifp->if_drv_flags |= IFF_DRV_RUNNING;
293 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
301 * If the interface is marked up and stopped, then start it.
302 * If it is marked down and running, then stop it.
304 if (ifr->ifr_flags & IFF_UP) {
305 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
306 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
307 ifp->if_drv_flags |= IFF_DRV_RUNNING;
310 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
311 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
316 /* Set the interface MTU */
318 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
319 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
322 ifp->if_mtu = ifr->ifr_mtu;
325 /* Stuff that's not supported */
342 * This routine is called to deliver a packet out the interface.
343 * We simply look at the address family and relay the packet to
344 * the corresponding hook, if it exists and is connected.
348 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
349 const struct sockaddr *dst, struct route *ro)
354 /* Check interface flags */
355 if (!((ifp->if_flags & IFF_UP) &&
356 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
361 /* Protect from deadly infinite recursion. */
362 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
363 V_ng_iface_max_nest);
369 /* BPF writes need to be handled specially. */
370 if (dst->sa_family == AF_UNSPEC)
371 bcopy(dst->sa_data, &af, sizeof(af));
373 af = RO_GET_FAMILY(ro, dst);
375 /* Berkeley packet filter */
376 ng_iface_bpftap(ifp, m, af);
378 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
379 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
381 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
384 *(sa_family_t *)m->m_data = af;
385 error = (ifp->if_transmit)(ifp, m);
387 error = ng_iface_send(ifp, m, af);
393 * Start method is used only when ALTQ is enabled.
396 ng_iface_start(struct ifnet *ifp)
401 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
404 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
407 sa = *mtod(m, sa_family_t *);
408 m_adj(m, sizeof(sa_family_t));
409 ng_iface_send(ifp, m, sa);
414 * Flash a packet by the BPF (requires prepending 4 byte AF header)
415 * Note the phoney mbuf; this is OK because BPF treats it read-only.
418 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
420 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
421 if (bpf_peers_present(ifp->if_bpf)) {
422 int32_t family4 = (int32_t)family;
423 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
428 * This routine does actual delivery of the packet into the
429 * netgraph(4). It is called from ng_iface_start() and
433 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
435 struct rm_priotracker priv_tracker;
436 const priv_p priv = (priv_p) ifp->if_softc;
437 const iffam_p iffam = get_iffam_from_af(sa);
442 /* Check address family to determine hook (if known) */
445 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
446 return (EAFNOSUPPORT);
449 /* Copy length before the mbuf gets invalidated. */
450 len = m->m_pkthdr.len;
452 PRIV_RLOCK(priv, &priv_tracker);
453 hook = *get_hook_from_iffam(priv, iffam);
456 PRIV_RUNLOCK(priv, &priv_tracker);
460 PRIV_RUNLOCK(priv, &priv_tracker);
462 NG_OUTBOUND_THREAD_REF();
463 NG_SEND_DATA_ONLY(error, hook, m);
464 NG_OUTBOUND_THREAD_UNREF();
469 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
470 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
478 * Display an ioctl to the virtual interface
482 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
486 switch (command & IOC_DIRMASK) {
502 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
507 IOCPARM_LEN(command));
511 /************************************************************************
513 ************************************************************************/
516 * Constructor for a node
519 ng_iface_constructor(node_p node)
524 /* Allocate node and interface private structures */
525 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
526 ifp = if_alloc(IFT_PROPVIRTUAL);
528 free(priv, M_NETGRAPH_IFACE);
532 rm_init(&priv->lock, "ng_iface private rmlock");
534 /* Link them together */
535 ifp->if_softc = priv;
538 /* Get an interface unit number */
539 priv->unit = alloc_unr(V_ng_iface_unit);
541 /* Link together node and private info */
542 NG_NODE_SET_PRIVATE(node, priv);
545 /* Initialize interface structure */
546 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
547 ifp->if_output = ng_iface_output;
548 ifp->if_start = ng_iface_start;
549 ifp->if_ioctl = ng_iface_ioctl;
550 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
551 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
552 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
553 ifp->if_addrlen = 0; /* XXX */
554 ifp->if_hdrlen = 0; /* XXX */
555 ifp->if_baudrate = 64000; /* XXX */
556 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
557 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
558 IFQ_SET_READY(&ifp->if_snd);
560 /* Give this node the same name as the interface (if possible) */
561 if (ng_name_node(node, ifp->if_xname) != 0)
562 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
565 /* Attach the interface */
567 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
574 * Give our ok for a hook to be added
577 ng_iface_newhook(node_p node, hook_p hook, const char *name)
579 const iffam_p iffam = get_iffam_from_name(name);
580 const priv_p priv = NG_NODE_PRIVATE(node);
584 return (EPFNOSUPPORT);
586 hookptr = get_hook_from_iffam(priv, iffam);
587 if (*hookptr != NULL) {
592 NG_HOOK_HI_STACK(hook);
593 NG_HOOK_SET_TO_INBOUND(hook);
599 * Receive a control message
602 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
604 const priv_p priv = NG_NODE_PRIVATE(node);
605 struct ifnet *const ifp = priv->ifp;
606 struct ng_mesg *resp = NULL;
610 NGI_GET_MSG(item, msg);
611 switch (msg->header.typecookie) {
612 case NGM_IFACE_COOKIE:
613 switch (msg->header.cmd) {
614 case NGM_IFACE_GET_IFNAME:
615 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
620 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
623 case NGM_IFACE_POINT2POINT:
624 case NGM_IFACE_BROADCAST:
626 /* Deny request if interface is UP */
627 if ((ifp->if_flags & IFF_UP) != 0)
631 switch (msg->header.cmd) {
632 case NGM_IFACE_POINT2POINT:
633 ifp->if_flags |= IFF_POINTOPOINT;
634 ifp->if_flags &= ~IFF_BROADCAST;
636 case NGM_IFACE_BROADCAST:
637 ifp->if_flags &= ~IFF_POINTOPOINT;
638 ifp->if_flags |= IFF_BROADCAST;
644 case NGM_IFACE_GET_IFINDEX:
645 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
650 *((uint32_t *)resp->data) = priv->ifp->if_index;
658 case NGM_FLOW_COOKIE:
659 switch (msg->header.cmd) {
661 if_link_state_change(ifp, LINK_STATE_UP);
663 case NGM_LINK_IS_DOWN:
664 if_link_state_change(ifp, LINK_STATE_DOWN);
674 NG_RESPOND_MSG(error, node, item, resp);
680 * Recive data from a hook. Pass the packet to the correct input routine.
683 ng_iface_rcvdata(hook_p hook, item_p item)
685 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
686 const iffam_p iffam = get_iffam_from_hook(priv, hook);
687 struct ifnet *const ifp = priv->ifp;
688 struct epoch_tracker et;
695 KASSERT(iffam != NULL, ("%s: iffam", __func__));
697 if ((ifp->if_flags & IFF_UP) == 0) {
702 /* Update interface stats */
703 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
704 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
706 /* Note receiving interface */
707 m->m_pkthdr.rcvif = ifp;
709 /* Berkeley packet filter */
710 ng_iface_bpftap(ifp, m, iffam->family);
713 switch (iffam->family) {
726 return (EAFNOSUPPORT);
728 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
729 M_SETFIB(m, ifp->if_fib);
730 CURVNET_SET(ifp->if_vnet);
732 netisr_dispatch(isr, m);
739 * Shutdown and remove the node and its associated interface.
742 ng_iface_shutdown(node_p node)
744 const priv_p priv = NG_NODE_PRIVATE(node);
747 * The ifnet may be in a different vnet than the netgraph node,
748 * hence we have to change the current vnet context here.
750 CURVNET_SET_QUIET(priv->ifp->if_vnet);
751 bpfdetach(priv->ifp);
752 if_detach(priv->ifp);
756 free_unr(V_ng_iface_unit, priv->unit);
757 rm_destroy(&priv->lock);
758 free(priv, M_NETGRAPH_IFACE);
759 NG_NODE_SET_PRIVATE(node, NULL);
765 * Hook disconnection. Note that we do *not* shutdown when all
766 * hooks have been disconnected.
769 ng_iface_disconnect(hook_p hook)
771 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
772 const iffam_p iffam = get_iffam_from_hook(priv, hook);
775 panic("%s", __func__);
777 *get_hook_from_iffam(priv, iffam) = NULL;
783 * Handle loading and unloading for this node type.
786 ng_iface_mod_event(module_t mod, int event, void *data)
802 vnet_ng_iface_init(const void *unused)
805 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
807 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
808 vnet_ng_iface_init, NULL);
811 vnet_ng_iface_uninit(const void *unused)
814 delete_unrhdr(V_ng_iface_unit);
816 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
817 vnet_ng_iface_uninit, NULL);