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, 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.
56 #include "opt_inet6.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/errno.h>
67 #include <sys/random.h>
68 #include <sys/rmlock.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/syslog.h>
73 #include <sys/libkern.h>
76 #include <net/if_var.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>
90 #ifdef NG_SEPARATE_MALLOC
91 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
93 #define M_NETGRAPH_IFACE M_NETGRAPH
96 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
97 "Point to point netgraph interface");
98 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
99 #define V_ng_iface_max_nest VNET(ng_iface_max_nest)
100 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
101 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
103 /* This struct describes one address family */
105 sa_family_t family; /* Address family */
106 const char *hookname; /* Name for hook */
108 typedef const struct iffam *iffam_p;
110 /* List of address families supported by our interface */
111 const static struct iffam gFamilies[] = {
112 { AF_INET, NG_IFACE_HOOK_INET },
113 { AF_INET6, NG_IFACE_HOOK_INET6 },
115 #define NUM_FAMILIES nitems(gFamilies)
117 /* Node private data */
118 struct ng_iface_private {
119 struct ifnet *ifp; /* Our interface */
120 int unit; /* Interface unit number */
121 node_p node; /* Our netgraph node */
122 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
123 struct rmlock lock; /* Protect private data changes */
125 typedef struct ng_iface_private *priv_p;
127 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t)
128 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t)
129 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock)
130 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock)
132 /* Interface methods */
133 static void ng_iface_start(struct ifnet *ifp);
134 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
135 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
136 const struct sockaddr *dst, struct route *ro);
137 static void ng_iface_bpftap(struct ifnet *ifp,
138 struct mbuf *m, sa_family_t family);
139 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
142 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
145 /* Netgraph methods */
146 static int ng_iface_mod_event(module_t, int, void *);
147 static ng_constructor_t ng_iface_constructor;
148 static ng_rcvmsg_t ng_iface_rcvmsg;
149 static ng_shutdown_t ng_iface_shutdown;
150 static ng_newhook_t ng_iface_newhook;
151 static ng_rcvdata_t ng_iface_rcvdata;
152 static ng_disconnect_t ng_iface_disconnect;
155 static iffam_p get_iffam_from_af(sa_family_t family);
156 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
157 static iffam_p get_iffam_from_name(const char *name);
158 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
160 /* List of commands and how to convert arguments to/from ASCII */
161 static const struct ng_cmdlist ng_iface_cmds[] = {
164 NGM_IFACE_GET_IFNAME,
167 &ng_parse_string_type
171 NGM_IFACE_POINT2POINT,
185 NGM_IFACE_GET_IFINDEX,
188 &ng_parse_uint32_type
193 /* Node type descriptor */
194 static struct ng_type typestruct = {
195 .version = NG_ABI_VERSION,
196 .name = NG_IFACE_NODE_TYPE,
197 .mod_event = ng_iface_mod_event,
198 .constructor = ng_iface_constructor,
199 .rcvmsg = ng_iface_rcvmsg,
200 .shutdown = ng_iface_shutdown,
201 .newhook = ng_iface_newhook,
202 .rcvdata = ng_iface_rcvdata,
203 .disconnect = ng_iface_disconnect,
204 .cmdlist = ng_iface_cmds,
206 NETGRAPH_INIT(iface, &typestruct);
208 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
209 #define V_ng_iface_unit VNET(ng_iface_unit)
211 /************************************************************************
213 ************************************************************************/
216 * Get the family descriptor from the family ID
218 static __inline iffam_p
219 get_iffam_from_af(sa_family_t family)
224 for (k = 0; k < NUM_FAMILIES; k++) {
225 iffam = &gFamilies[k];
226 if (iffam->family == family)
233 * Get the family descriptor from the hook
235 static __inline iffam_p
236 get_iffam_from_hook(priv_p priv, hook_p hook)
240 for (k = 0; k < NUM_FAMILIES; k++)
241 if (priv->hooks[k] == hook)
242 return (&gFamilies[k]);
247 * Get the hook from the iffam descriptor
250 static __inline hook_p *
251 get_hook_from_iffam(priv_p priv, iffam_p iffam)
253 return (&priv->hooks[iffam - gFamilies]);
257 * Get the iffam descriptor from the name
259 static __inline iffam_p
260 get_iffam_from_name(const char *name)
265 for (k = 0; k < NUM_FAMILIES; k++) {
266 iffam = &gFamilies[k];
267 if (!strcmp(iffam->hookname, name))
273 /************************************************************************
275 ************************************************************************/
278 * Process an ioctl for the virtual interface
281 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
283 struct ifreq *const ifr = (struct ifreq *) data;
287 ng_iface_print_ioctl(ifp, command, data);
290 /* These two are mostly handled at a higher layer */
292 ifp->if_flags |= IFF_UP;
293 ifp->if_drv_flags |= IFF_DRV_RUNNING;
294 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
302 * If the interface is marked up and stopped, then start it.
303 * If it is marked down and running, then stop it.
305 if (ifr->ifr_flags & IFF_UP) {
306 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
307 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
308 ifp->if_drv_flags |= IFF_DRV_RUNNING;
311 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
312 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
317 /* Set the interface MTU */
319 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
320 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
323 ifp->if_mtu = ifr->ifr_mtu;
326 /* Stuff that's not supported */
343 * This routine is called to deliver a packet out the interface.
344 * We simply look at the address family and relay the packet to
345 * the corresponding hook, if it exists and is connected.
349 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
350 const struct sockaddr *dst, struct route *ro)
355 /* Check interface flags */
356 if (!((ifp->if_flags & IFF_UP) &&
357 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
362 /* Protect from deadly infinite recursion. */
363 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
364 V_ng_iface_max_nest);
370 /* BPF writes need to be handled specially. */
371 if (dst->sa_family == AF_UNSPEC)
372 bcopy(dst->sa_data, &af, sizeof(af));
376 /* Berkeley packet filter */
377 ng_iface_bpftap(ifp, m, af);
379 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
380 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
382 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
385 *(sa_family_t *)m->m_data = af;
386 error = (ifp->if_transmit)(ifp, m);
388 error = ng_iface_send(ifp, m, af);
394 * Start method is used only when ALTQ is enabled.
397 ng_iface_start(struct ifnet *ifp)
402 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
405 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
408 sa = *mtod(m, sa_family_t *);
409 m_adj(m, sizeof(sa_family_t));
410 ng_iface_send(ifp, m, sa);
415 * Flash a packet by the BPF (requires prepending 4 byte AF header)
416 * Note the phoney mbuf; this is OK because BPF treats it read-only.
419 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
421 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
422 if (bpf_peers_present(ifp->if_bpf)) {
423 int32_t family4 = (int32_t)family;
424 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
429 * This routine does actual delivery of the packet into the
430 * netgraph(4). It is called from ng_iface_start() and
434 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
436 struct rm_priotracker priv_tracker;
437 const priv_p priv = (priv_p) ifp->if_softc;
438 const iffam_p iffam = get_iffam_from_af(sa);
443 /* Check address family to determine hook (if known) */
446 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
447 return (EAFNOSUPPORT);
450 /* Copy length before the mbuf gets invalidated. */
451 len = m->m_pkthdr.len;
453 PRIV_RLOCK(priv, &priv_tracker);
454 hook = *get_hook_from_iffam(priv, iffam);
457 PRIV_RUNLOCK(priv, &priv_tracker);
461 PRIV_RUNLOCK(priv, &priv_tracker);
463 NG_OUTBOUND_THREAD_REF();
464 NG_SEND_DATA_ONLY(error, hook, m);
465 NG_OUTBOUND_THREAD_UNREF();
470 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
471 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
479 * Display an ioctl to the virtual interface
483 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
487 switch (command & IOC_DIRMASK) {
503 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
508 IOCPARM_LEN(command));
512 /************************************************************************
514 ************************************************************************/
517 * Constructor for a node
520 ng_iface_constructor(node_p node)
525 /* Allocate node and interface private structures */
526 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
527 ifp = if_alloc(IFT_PROPVIRTUAL);
529 free(priv, M_NETGRAPH_IFACE);
533 rm_init(&priv->lock, "ng_iface private rmlock");
535 /* Link them together */
536 ifp->if_softc = priv;
539 /* Get an interface unit number */
540 priv->unit = alloc_unr(V_ng_iface_unit);
542 /* Link together node and private info */
543 NG_NODE_SET_PRIVATE(node, priv);
546 /* Initialize interface structure */
547 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
548 ifp->if_output = ng_iface_output;
549 ifp->if_start = ng_iface_start;
550 ifp->if_ioctl = ng_iface_ioctl;
551 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
552 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
553 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
554 ifp->if_addrlen = 0; /* XXX */
555 ifp->if_hdrlen = 0; /* XXX */
556 ifp->if_baudrate = 64000; /* XXX */
557 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
558 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
559 IFQ_SET_READY(&ifp->if_snd);
561 /* Give this node the same name as the interface (if possible) */
562 if (ng_name_node(node, ifp->if_xname) != 0)
563 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
566 /* Attach the interface */
568 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
575 * Give our ok for a hook to be added
578 ng_iface_newhook(node_p node, hook_p hook, const char *name)
580 const iffam_p iffam = get_iffam_from_name(name);
581 const priv_p priv = NG_NODE_PRIVATE(node);
585 return (EPFNOSUPPORT);
587 hookptr = get_hook_from_iffam(priv, iffam);
588 if (*hookptr != NULL) {
593 NG_HOOK_HI_STACK(hook);
594 NG_HOOK_SET_TO_INBOUND(hook);
600 * Receive a control message
603 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
605 const priv_p priv = NG_NODE_PRIVATE(node);
606 struct ifnet *const ifp = priv->ifp;
607 struct ng_mesg *resp = NULL;
611 NGI_GET_MSG(item, msg);
612 switch (msg->header.typecookie) {
613 case NGM_IFACE_COOKIE:
614 switch (msg->header.cmd) {
615 case NGM_IFACE_GET_IFNAME:
616 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
621 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
624 case NGM_IFACE_POINT2POINT:
625 case NGM_IFACE_BROADCAST:
627 /* Deny request if interface is UP */
628 if ((ifp->if_flags & IFF_UP) != 0)
632 switch (msg->header.cmd) {
633 case NGM_IFACE_POINT2POINT:
634 ifp->if_flags |= IFF_POINTOPOINT;
635 ifp->if_flags &= ~IFF_BROADCAST;
637 case NGM_IFACE_BROADCAST:
638 ifp->if_flags &= ~IFF_POINTOPOINT;
639 ifp->if_flags |= IFF_BROADCAST;
645 case NGM_IFACE_GET_IFINDEX:
646 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
651 *((uint32_t *)resp->data) = priv->ifp->if_index;
659 case NGM_FLOW_COOKIE:
660 switch (msg->header.cmd) {
662 if_link_state_change(ifp, LINK_STATE_UP);
664 case NGM_LINK_IS_DOWN:
665 if_link_state_change(ifp, LINK_STATE_DOWN);
675 NG_RESPOND_MSG(error, node, item, resp);
681 * Recive data from a hook. Pass the packet to the correct input routine.
684 ng_iface_rcvdata(hook_p hook, item_p item)
686 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
687 const iffam_p iffam = get_iffam_from_hook(priv, hook);
688 struct ifnet *const ifp = priv->ifp;
689 struct epoch_tracker et;
696 KASSERT(iffam != NULL, ("%s: iffam", __func__));
698 if ((ifp->if_flags & IFF_UP) == 0) {
703 /* Update interface stats */
704 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
705 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
707 /* Note receiving interface */
708 m->m_pkthdr.rcvif = ifp;
710 /* Berkeley packet filter */
711 ng_iface_bpftap(ifp, m, iffam->family);
714 switch (iffam->family) {
727 return (EAFNOSUPPORT);
729 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
730 M_SETFIB(m, ifp->if_fib);
731 CURVNET_SET(ifp->if_vnet);
733 netisr_dispatch(isr, m);
740 * Shutdown and remove the node and its associated interface.
743 ng_iface_shutdown(node_p node)
745 const priv_p priv = NG_NODE_PRIVATE(node);
748 * The ifnet may be in a different vnet than the netgraph node,
749 * hence we have to change the current vnet context here.
751 CURVNET_SET_QUIET(priv->ifp->if_vnet);
752 bpfdetach(priv->ifp);
753 if_detach(priv->ifp);
757 free_unr(V_ng_iface_unit, priv->unit);
758 rm_destroy(&priv->lock);
759 free(priv, M_NETGRAPH_IFACE);
760 NG_NODE_SET_PRIVATE(node, NULL);
766 * Hook disconnection. Note that we do *not* shutdown when all
767 * hooks have been disconnected.
770 ng_iface_disconnect(hook_p hook)
772 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
773 const iffam_p iffam = get_iffam_from_hook(priv, hook);
776 panic("%s", __func__);
778 *get_hook_from_iffam(priv, iffam) = NULL;
784 * Handle loading and unloading for this node type.
787 ng_iface_mod_event(module_t mod, int event, void *data)
803 vnet_ng_iface_init(const void *unused)
806 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
808 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
809 vnet_ng_iface_init, NULL);
812 vnet_ng_iface_uninit(const void *unused)
815 delete_unrhdr(V_ng_iface_unit);
817 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
818 vnet_ng_iface_uninit, NULL);