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_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>
88 #ifdef NG_SEPARATE_MALLOC
89 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
91 #define M_NETGRAPH_IFACE M_NETGRAPH
94 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95 "Point to point netgraph interface");
96 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
97 #define V_ng_iface_max_nest VNET(ng_iface_max_nest)
98 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
99 &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
101 /* This struct describes one address family */
103 sa_family_t family; /* Address family */
104 const char *hookname; /* Name for hook */
106 typedef const struct iffam *iffam_p;
108 /* List of address families supported by our interface */
109 const static struct iffam gFamilies[] = {
110 { AF_INET, NG_IFACE_HOOK_INET },
111 { AF_INET6, NG_IFACE_HOOK_INET6 },
113 #define NUM_FAMILIES nitems(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 /* 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_IFACE_GET_IFINDEX,
186 &ng_parse_uint32_type
191 /* Node type descriptor */
192 static struct ng_type typestruct = {
193 .version = NG_ABI_VERSION,
194 .name = NG_IFACE_NODE_TYPE,
195 .mod_event = ng_iface_mod_event,
196 .constructor = ng_iface_constructor,
197 .rcvmsg = ng_iface_rcvmsg,
198 .shutdown = ng_iface_shutdown,
199 .newhook = ng_iface_newhook,
200 .rcvdata = ng_iface_rcvdata,
201 .disconnect = ng_iface_disconnect,
202 .cmdlist = ng_iface_cmds,
204 NETGRAPH_INIT(iface, &typestruct);
206 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
207 #define V_ng_iface_unit VNET(ng_iface_unit)
209 /************************************************************************
211 ************************************************************************/
214 * Get the family descriptor from the family ID
216 static __inline iffam_p
217 get_iffam_from_af(sa_family_t family)
222 for (k = 0; k < NUM_FAMILIES; k++) {
223 iffam = &gFamilies[k];
224 if (iffam->family == family)
231 * Get the family descriptor from the hook
233 static __inline iffam_p
234 get_iffam_from_hook(priv_p priv, hook_p hook)
238 for (k = 0; k < NUM_FAMILIES; k++)
239 if (priv->hooks[k] == hook)
240 return (&gFamilies[k]);
245 * Get the hook from the iffam descriptor
248 static __inline hook_p *
249 get_hook_from_iffam(priv_p priv, iffam_p iffam)
251 return (&priv->hooks[iffam - gFamilies]);
255 * Get the iffam descriptor from the name
257 static __inline iffam_p
258 get_iffam_from_name(const char *name)
263 for (k = 0; k < NUM_FAMILIES; k++) {
264 iffam = &gFamilies[k];
265 if (!strcmp(iffam->hookname, name))
271 /************************************************************************
273 ************************************************************************/
276 * Process an ioctl for the virtual interface
279 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
281 struct ifreq *const ifr = (struct ifreq *) data;
285 ng_iface_print_ioctl(ifp, command, data);
288 /* These two are mostly handled at a higher layer */
290 ifp->if_flags |= IFF_UP;
291 ifp->if_drv_flags |= IFF_DRV_RUNNING;
292 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
300 * If the interface is marked up and stopped, then start it.
301 * If it is marked down and running, then stop it.
303 if (ifr->ifr_flags & IFF_UP) {
304 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
305 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
306 ifp->if_drv_flags |= IFF_DRV_RUNNING;
309 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
310 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
315 /* Set the interface MTU */
317 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
318 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
321 ifp->if_mtu = ifr->ifr_mtu;
324 /* Stuff that's not supported */
341 * This routine is called to deliver a packet out the interface.
342 * We simply look at the address family and relay the packet to
343 * the corresponding hook, if it exists and is connected.
347 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
348 const struct sockaddr *dst, struct route *ro)
353 /* Check interface flags */
354 if (!((ifp->if_flags & IFF_UP) &&
355 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
360 /* Protect from deadly infinite recursion. */
361 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
362 V_ng_iface_max_nest);
368 /* BPF writes need to be handled specially. */
369 if (dst->sa_family == AF_UNSPEC)
370 bcopy(dst->sa_data, &af, sizeof(af));
372 af = RO_GET_FAMILY(ro, dst);
374 /* Berkeley packet filter */
375 ng_iface_bpftap(ifp, m, af);
377 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
378 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
380 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
383 *(sa_family_t *)m->m_data = af;
384 error = (ifp->if_transmit)(ifp, m);
386 error = ng_iface_send(ifp, m, af);
392 * Start method is used only when ALTQ is enabled.
395 ng_iface_start(struct ifnet *ifp)
400 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
403 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
406 sa = *mtod(m, sa_family_t *);
407 m_adj(m, sizeof(sa_family_t));
408 ng_iface_send(ifp, m, sa);
413 * Flash a packet by the BPF (requires prepending 4 byte AF header)
414 * Note the phoney mbuf; this is OK because BPF treats it read-only.
417 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
419 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
420 if (bpf_peers_present(ifp->if_bpf)) {
421 int32_t family4 = (int32_t)family;
422 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
427 * This routine does actual delivery of the packet into the
428 * netgraph(4). It is called from ng_iface_start() and
432 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
434 struct rm_priotracker priv_tracker;
435 const priv_p priv = (priv_p) ifp->if_softc;
436 const iffam_p iffam = get_iffam_from_af(sa);
441 /* Check address family to determine hook (if known) */
444 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
445 return (EAFNOSUPPORT);
448 /* Copy length before the mbuf gets invalidated. */
449 len = m->m_pkthdr.len;
451 PRIV_RLOCK(priv, &priv_tracker);
452 hook = *get_hook_from_iffam(priv, iffam);
455 PRIV_RUNLOCK(priv, &priv_tracker);
459 PRIV_RUNLOCK(priv, &priv_tracker);
461 NG_OUTBOUND_THREAD_REF();
462 NG_SEND_DATA_ONLY(error, hook, m);
463 NG_OUTBOUND_THREAD_UNREF();
468 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
469 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
477 * Display an ioctl to the virtual interface
481 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
485 switch (command & IOC_DIRMASK) {
501 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
506 IOCPARM_LEN(command));
510 /************************************************************************
512 ************************************************************************/
515 * Constructor for a node
518 ng_iface_constructor(node_p node)
523 /* Allocate node and interface private structures */
524 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
525 ifp = if_alloc(IFT_PROPVIRTUAL);
527 free(priv, M_NETGRAPH_IFACE);
531 rm_init(&priv->lock, "ng_iface private rmlock");
533 /* Link them together */
534 ifp->if_softc = priv;
537 /* Get an interface unit number */
538 priv->unit = alloc_unr(V_ng_iface_unit);
540 /* Link together node and private info */
541 NG_NODE_SET_PRIVATE(node, priv);
544 /* Initialize interface structure */
545 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
546 ifp->if_output = ng_iface_output;
547 ifp->if_start = ng_iface_start;
548 ifp->if_ioctl = ng_iface_ioctl;
549 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
550 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
551 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
552 ifp->if_addrlen = 0; /* XXX */
553 ifp->if_hdrlen = 0; /* XXX */
554 ifp->if_baudrate = 64000; /* XXX */
555 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
556 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
557 IFQ_SET_READY(&ifp->if_snd);
559 /* Give this node the same name as the interface (if possible) */
560 if (ng_name_node(node, ifp->if_xname) != 0)
561 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
564 /* Attach the interface */
566 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
573 * Give our ok for a hook to be added
576 ng_iface_newhook(node_p node, hook_p hook, const char *name)
578 const iffam_p iffam = get_iffam_from_name(name);
579 const priv_p priv = NG_NODE_PRIVATE(node);
583 return (EPFNOSUPPORT);
585 hookptr = get_hook_from_iffam(priv, iffam);
586 if (*hookptr != NULL) {
591 NG_HOOK_HI_STACK(hook);
592 NG_HOOK_SET_TO_INBOUND(hook);
598 * Receive a control message
601 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
603 const priv_p priv = NG_NODE_PRIVATE(node);
604 struct ifnet *const ifp = priv->ifp;
605 struct ng_mesg *resp = NULL;
609 NGI_GET_MSG(item, msg);
610 switch (msg->header.typecookie) {
611 case NGM_IFACE_COOKIE:
612 switch (msg->header.cmd) {
613 case NGM_IFACE_GET_IFNAME:
614 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
619 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
622 case NGM_IFACE_POINT2POINT:
623 case NGM_IFACE_BROADCAST:
625 /* Deny request if interface is UP */
626 if ((ifp->if_flags & IFF_UP) != 0)
630 switch (msg->header.cmd) {
631 case NGM_IFACE_POINT2POINT:
632 ifp->if_flags |= IFF_POINTOPOINT;
633 ifp->if_flags &= ~IFF_BROADCAST;
635 case NGM_IFACE_BROADCAST:
636 ifp->if_flags &= ~IFF_POINTOPOINT;
637 ifp->if_flags |= IFF_BROADCAST;
643 case NGM_IFACE_GET_IFINDEX:
644 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
649 *((uint32_t *)resp->data) = priv->ifp->if_index;
657 case NGM_FLOW_COOKIE:
658 switch (msg->header.cmd) {
660 if_link_state_change(ifp, LINK_STATE_UP);
662 case NGM_LINK_IS_DOWN:
663 if_link_state_change(ifp, LINK_STATE_DOWN);
673 NG_RESPOND_MSG(error, node, item, resp);
679 * Recive data from a hook. Pass the packet to the correct input routine.
682 ng_iface_rcvdata(hook_p hook, item_p item)
684 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
685 const iffam_p iffam = get_iffam_from_hook(priv, hook);
686 struct ifnet *const ifp = priv->ifp;
687 struct epoch_tracker et;
694 KASSERT(iffam != NULL, ("%s: iffam", __func__));
696 if ((ifp->if_flags & IFF_UP) == 0) {
701 /* Update interface stats */
702 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
703 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
705 /* Note receiving interface */
706 m->m_pkthdr.rcvif = ifp;
708 /* Berkeley packet filter */
709 ng_iface_bpftap(ifp, m, iffam->family);
712 switch (iffam->family) {
725 return (EAFNOSUPPORT);
727 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
728 M_SETFIB(m, ifp->if_fib);
729 CURVNET_SET(ifp->if_vnet);
731 netisr_dispatch(isr, m);
738 * Shutdown and remove the node and its associated interface.
741 ng_iface_shutdown(node_p node)
743 const priv_p priv = NG_NODE_PRIVATE(node);
746 * The ifnet may be in a different vnet than the netgraph node,
747 * hence we have to change the current vnet context here.
749 CURVNET_SET_QUIET(priv->ifp->if_vnet);
750 bpfdetach(priv->ifp);
751 if_detach(priv->ifp);
755 free_unr(V_ng_iface_unit, priv->unit);
756 rm_destroy(&priv->lock);
757 free(priv, M_NETGRAPH_IFACE);
758 NG_NODE_SET_PRIVATE(node, NULL);
764 * Hook disconnection. Note that we do *not* shutdown when all
765 * hooks have been disconnected.
768 ng_iface_disconnect(hook_p hook)
770 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
771 const iffam_p iffam = get_iffam_from_hook(priv, hook);
774 panic("%s", __func__);
776 *get_hook_from_iffam(priv, iffam) = NULL;
782 * Handle loading and unloading for this node type.
785 ng_iface_mod_event(module_t mod, int event, void *data)
801 vnet_ng_iface_init(const void *unused)
804 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
806 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
807 vnet_ng_iface_init, NULL);
810 vnet_ng_iface_uninit(const void *unused)
813 delete_unrhdr(V_ng_iface_unit);
815 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
816 vnet_ng_iface_uninit, NULL);