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);
291 /* These two are mostly handled at a higher layer */
293 ifp->if_flags |= IFF_UP;
294 ifp->if_drv_flags |= IFF_DRV_RUNNING;
295 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
303 * If the interface is marked up and stopped, then start it.
304 * If it is marked down and running, then stop it.
306 if (ifr->ifr_flags & IFF_UP) {
307 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
308 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
309 ifp->if_drv_flags |= IFF_DRV_RUNNING;
312 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
313 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
318 /* Set the interface MTU */
320 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
321 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
324 ifp->if_mtu = ifr->ifr_mtu;
327 /* Stuff that's not supported */
344 * This routine is called to deliver a packet out the interface.
345 * We simply look at the address family and relay the packet to
346 * the corresponding hook, if it exists and is connected.
350 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
351 const struct sockaddr *dst, struct route *ro)
356 /* Check interface flags */
357 if (!((ifp->if_flags & IFF_UP) &&
358 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
363 /* Protect from deadly infinite recursion. */
364 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
365 V_ng_iface_max_nest);
371 /* BPF writes need to be handled specially. */
372 if (dst->sa_family == AF_UNSPEC)
373 bcopy(dst->sa_data, &af, sizeof(af));
377 /* Berkeley packet filter */
378 ng_iface_bpftap(ifp, m, af);
380 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
381 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
383 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
386 *(sa_family_t *)m->m_data = af;
387 error = (ifp->if_transmit)(ifp, m);
389 error = ng_iface_send(ifp, m, af);
395 * Start method is used only when ALTQ is enabled.
398 ng_iface_start(struct ifnet *ifp)
403 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
406 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
409 sa = *mtod(m, sa_family_t *);
410 m_adj(m, sizeof(sa_family_t));
411 ng_iface_send(ifp, m, sa);
416 * Flash a packet by the BPF (requires prepending 4 byte AF header)
417 * Note the phoney mbuf; this is OK because BPF treats it read-only.
420 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
422 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
423 if (bpf_peers_present(ifp->if_bpf)) {
424 int32_t family4 = (int32_t)family;
425 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
430 * This routine does actual delivery of the packet into the
431 * netgraph(4). It is called from ng_iface_start() and
435 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
437 struct rm_priotracker priv_tracker;
438 const priv_p priv = (priv_p) ifp->if_softc;
439 const iffam_p iffam = get_iffam_from_af(sa);
444 /* Check address family to determine hook (if known) */
447 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
448 return (EAFNOSUPPORT);
451 /* Copy length before the mbuf gets invalidated. */
452 len = m->m_pkthdr.len;
454 PRIV_RLOCK(priv, &priv_tracker);
455 hook = *get_hook_from_iffam(priv, iffam);
458 PRIV_RUNLOCK(priv, &priv_tracker);
462 PRIV_RUNLOCK(priv, &priv_tracker);
464 NG_OUTBOUND_THREAD_REF();
465 NG_SEND_DATA_ONLY(error, hook, m);
466 NG_OUTBOUND_THREAD_UNREF();
471 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
472 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
480 * Display an ioctl to the virtual interface
484 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
488 switch (command & IOC_DIRMASK) {
504 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
509 IOCPARM_LEN(command));
513 /************************************************************************
515 ************************************************************************/
518 * Constructor for a node
521 ng_iface_constructor(node_p node)
526 /* Allocate node and interface private structures */
527 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
528 ifp = if_alloc(IFT_PROPVIRTUAL);
530 free(priv, M_NETGRAPH_IFACE);
534 rm_init(&priv->lock, "ng_iface private rmlock");
536 /* Link them together */
537 ifp->if_softc = priv;
540 /* Get an interface unit number */
541 priv->unit = alloc_unr(V_ng_iface_unit);
543 /* Link together node and private info */
544 NG_NODE_SET_PRIVATE(node, priv);
547 /* Initialize interface structure */
548 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
549 ifp->if_output = ng_iface_output;
550 ifp->if_start = ng_iface_start;
551 ifp->if_ioctl = ng_iface_ioctl;
552 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
553 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
554 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
555 ifp->if_addrlen = 0; /* XXX */
556 ifp->if_hdrlen = 0; /* XXX */
557 ifp->if_baudrate = 64000; /* XXX */
558 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
559 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
560 IFQ_SET_READY(&ifp->if_snd);
562 /* Give this node the same name as the interface (if possible) */
563 if (ng_name_node(node, ifp->if_xname) != 0)
564 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
567 /* Attach the interface */
569 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
576 * Give our ok for a hook to be added
579 ng_iface_newhook(node_p node, hook_p hook, const char *name)
581 const iffam_p iffam = get_iffam_from_name(name);
582 const priv_p priv = NG_NODE_PRIVATE(node);
586 return (EPFNOSUPPORT);
588 hookptr = get_hook_from_iffam(priv, iffam);
589 if (*hookptr != NULL) {
594 NG_HOOK_HI_STACK(hook);
595 NG_HOOK_SET_TO_INBOUND(hook);
601 * Receive a control message
604 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
606 const priv_p priv = NG_NODE_PRIVATE(node);
607 struct ifnet *const ifp = priv->ifp;
608 struct ng_mesg *resp = NULL;
612 NGI_GET_MSG(item, msg);
613 switch (msg->header.typecookie) {
614 case NGM_IFACE_COOKIE:
615 switch (msg->header.cmd) {
616 case NGM_IFACE_GET_IFNAME:
617 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
622 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
625 case NGM_IFACE_POINT2POINT:
626 case NGM_IFACE_BROADCAST:
629 /* Deny request if interface is UP */
630 if ((ifp->if_flags & IFF_UP) != 0)
634 switch (msg->header.cmd) {
635 case NGM_IFACE_POINT2POINT:
636 ifp->if_flags |= IFF_POINTOPOINT;
637 ifp->if_flags &= ~IFF_BROADCAST;
639 case NGM_IFACE_BROADCAST:
640 ifp->if_flags &= ~IFF_POINTOPOINT;
641 ifp->if_flags |= IFF_BROADCAST;
647 case NGM_IFACE_GET_IFINDEX:
648 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
653 *((uint32_t *)resp->data) = priv->ifp->if_index;
661 case NGM_FLOW_COOKIE:
662 switch (msg->header.cmd) {
664 if_link_state_change(ifp, LINK_STATE_UP);
666 case NGM_LINK_IS_DOWN:
667 if_link_state_change(ifp, LINK_STATE_DOWN);
677 NG_RESPOND_MSG(error, node, item, resp);
683 * Recive data from a hook. Pass the packet to the correct input routine.
686 ng_iface_rcvdata(hook_p hook, item_p item)
688 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
689 const iffam_p iffam = get_iffam_from_hook(priv, hook);
690 struct ifnet *const ifp = priv->ifp;
691 struct epoch_tracker et;
698 KASSERT(iffam != NULL, ("%s: iffam", __func__));
700 if ((ifp->if_flags & IFF_UP) == 0) {
705 /* Update interface stats */
706 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
707 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
709 /* Note receiving interface */
710 m->m_pkthdr.rcvif = ifp;
712 /* Berkeley packet filter */
713 ng_iface_bpftap(ifp, m, iffam->family);
716 switch (iffam->family) {
729 return (EAFNOSUPPORT);
731 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
732 M_SETFIB(m, ifp->if_fib);
733 CURVNET_SET(ifp->if_vnet);
735 netisr_dispatch(isr, m);
742 * Shutdown and remove the node and its associated interface.
745 ng_iface_shutdown(node_p node)
747 const priv_p priv = NG_NODE_PRIVATE(node);
750 * The ifnet may be in a different vnet than the netgraph node,
751 * hence we have to change the current vnet context here.
753 CURVNET_SET_QUIET(priv->ifp->if_vnet);
754 bpfdetach(priv->ifp);
755 if_detach(priv->ifp);
759 free_unr(V_ng_iface_unit, priv->unit);
760 rm_destroy(&priv->lock);
761 free(priv, M_NETGRAPH_IFACE);
762 NG_NODE_SET_PRIVATE(node, NULL);
768 * Hook disconnection. Note that we do *not* shutdown when all
769 * hooks have been disconnected.
772 ng_iface_disconnect(hook_p hook)
774 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
775 const iffam_p iffam = get_iffam_from_hook(priv, hook);
778 panic("%s", __func__);
780 *get_hook_from_iffam(priv, iffam) = NULL;
786 * Handle loading and unloading for this node type.
789 ng_iface_mod_event(module_t mod, int event, void *data)
805 vnet_ng_iface_init(const void *unused)
808 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
810 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
811 vnet_ng_iface_init, NULL);
814 vnet_ng_iface_uninit(const void *unused)
817 delete_unrhdr(V_ng_iface_unit);
819 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
820 vnet_ng_iface_uninit, NULL);