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/syslog.h>
72 #include <sys/libkern.h>
75 #include <net/if_var.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 /* This struct describes one address family */
97 sa_family_t family; /* Address family */
98 const char *hookname; /* Name for hook */
100 typedef const struct iffam *iffam_p;
102 /* List of address families supported by our interface */
103 const static struct iffam gFamilies[] = {
104 { AF_INET, NG_IFACE_HOOK_INET },
105 { AF_INET6, NG_IFACE_HOOK_INET6 },
106 { AF_ATM, NG_IFACE_HOOK_ATM },
107 { AF_NATM, NG_IFACE_HOOK_NATM },
109 #define NUM_FAMILIES nitems(gFamilies)
111 /* Node private data */
112 struct ng_iface_private {
113 struct ifnet *ifp; /* Our interface */
114 int unit; /* Interface unit number */
115 node_p node; /* Our netgraph node */
116 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
117 struct rmlock lock; /* Protect private data changes */
119 typedef struct ng_iface_private *priv_p;
121 #define PRIV_RLOCK(priv, t) rm_rlock(&priv->lock, t)
122 #define PRIV_RUNLOCK(priv, t) rm_runlock(&priv->lock, t)
123 #define PRIV_WLOCK(priv) rm_wlock(&priv->lock)
124 #define PRIV_WUNLOCK(priv) rm_wunlock(&priv->lock)
126 /* Interface methods */
127 static void ng_iface_start(struct ifnet *ifp);
128 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
129 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
130 const struct sockaddr *dst, struct route *ro);
131 static void ng_iface_bpftap(struct ifnet *ifp,
132 struct mbuf *m, sa_family_t family);
133 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
136 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
139 /* Netgraph methods */
140 static int ng_iface_mod_event(module_t, int, void *);
141 static ng_constructor_t ng_iface_constructor;
142 static ng_rcvmsg_t ng_iface_rcvmsg;
143 static ng_shutdown_t ng_iface_shutdown;
144 static ng_newhook_t ng_iface_newhook;
145 static ng_rcvdata_t ng_iface_rcvdata;
146 static ng_disconnect_t ng_iface_disconnect;
149 static iffam_p get_iffam_from_af(sa_family_t family);
150 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
151 static iffam_p get_iffam_from_name(const char *name);
152 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
154 /* List of commands and how to convert arguments to/from ASCII */
155 static const struct ng_cmdlist ng_iface_cmds[] = {
158 NGM_IFACE_GET_IFNAME,
161 &ng_parse_string_type
165 NGM_IFACE_POINT2POINT,
179 NGM_IFACE_GET_IFINDEX,
182 &ng_parse_uint32_type
187 /* Node type descriptor */
188 static struct ng_type typestruct = {
189 .version = NG_ABI_VERSION,
190 .name = NG_IFACE_NODE_TYPE,
191 .mod_event = ng_iface_mod_event,
192 .constructor = ng_iface_constructor,
193 .rcvmsg = ng_iface_rcvmsg,
194 .shutdown = ng_iface_shutdown,
195 .newhook = ng_iface_newhook,
196 .rcvdata = ng_iface_rcvdata,
197 .disconnect = ng_iface_disconnect,
198 .cmdlist = ng_iface_cmds,
200 NETGRAPH_INIT(iface, &typestruct);
202 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
203 #define V_ng_iface_unit VNET(ng_iface_unit)
205 /************************************************************************
207 ************************************************************************/
210 * Get the family descriptor from the family ID
212 static __inline iffam_p
213 get_iffam_from_af(sa_family_t family)
218 for (k = 0; k < NUM_FAMILIES; k++) {
219 iffam = &gFamilies[k];
220 if (iffam->family == family)
227 * Get the family descriptor from the hook
229 static __inline iffam_p
230 get_iffam_from_hook(priv_p priv, hook_p hook)
234 for (k = 0; k < NUM_FAMILIES; k++)
235 if (priv->hooks[k] == hook)
236 return (&gFamilies[k]);
241 * Get the hook from the iffam descriptor
244 static __inline hook_p *
245 get_hook_from_iffam(priv_p priv, iffam_p iffam)
247 return (&priv->hooks[iffam - gFamilies]);
251 * Get the iffam descriptor from the name
253 static __inline iffam_p
254 get_iffam_from_name(const char *name)
259 for (k = 0; k < NUM_FAMILIES; k++) {
260 iffam = &gFamilies[k];
261 if (!strcmp(iffam->hookname, name))
267 /************************************************************************
269 ************************************************************************/
272 * Process an ioctl for the virtual interface
275 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
277 struct ifreq *const ifr = (struct ifreq *) data;
281 ng_iface_print_ioctl(ifp, command, data);
285 /* These two are mostly handled at a higher layer */
287 ifp->if_flags |= IFF_UP;
288 ifp->if_drv_flags |= IFF_DRV_RUNNING;
289 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
297 * If the interface is marked up and stopped, then start it.
298 * If it is marked down and running, then stop it.
300 if (ifr->ifr_flags & IFF_UP) {
301 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
302 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
303 ifp->if_drv_flags |= IFF_DRV_RUNNING;
306 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
307 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
312 /* Set the interface MTU */
314 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
315 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
318 ifp->if_mtu = ifr->ifr_mtu;
321 /* Stuff that's not supported */
338 * This routine is called to deliver a packet out the interface.
339 * We simply look at the address family and relay the packet to
340 * the corresponding hook, if it exists and is connected.
344 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
345 const struct sockaddr *dst, struct route *ro)
350 /* Check interface flags */
351 if (!((ifp->if_flags & IFF_UP) &&
352 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
357 /* Protect from deadly infinite recursion. */
358 error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE, 1);
364 /* BPF writes need to be handled specially. */
365 if (dst->sa_family == AF_UNSPEC)
366 bcopy(dst->sa_data, &af, sizeof(af));
370 /* Berkeley packet filter */
371 ng_iface_bpftap(ifp, m, af);
373 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
374 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
376 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
379 *(sa_family_t *)m->m_data = af;
380 error = (ifp->if_transmit)(ifp, m);
382 error = ng_iface_send(ifp, m, af);
388 * Start method is used only when ALTQ is enabled.
391 ng_iface_start(struct ifnet *ifp)
396 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
399 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
402 sa = *mtod(m, sa_family_t *);
403 m_adj(m, sizeof(sa_family_t));
404 ng_iface_send(ifp, m, sa);
409 * Flash a packet by the BPF (requires prepending 4 byte AF header)
410 * Note the phoney mbuf; this is OK because BPF treats it read-only.
413 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
415 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
416 if (bpf_peers_present(ifp->if_bpf)) {
417 int32_t family4 = (int32_t)family;
418 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
423 * This routine does actual delivery of the packet into the
424 * netgraph(4). It is called from ng_iface_start() and
428 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
430 struct rm_priotracker priv_tracker;
431 const priv_p priv = (priv_p) ifp->if_softc;
432 const iffam_p iffam = get_iffam_from_af(sa);
437 /* Check address family to determine hook (if known) */
440 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
441 return (EAFNOSUPPORT);
444 /* Copy length before the mbuf gets invalidated. */
445 len = m->m_pkthdr.len;
447 PRIV_RLOCK(priv, &priv_tracker);
448 hook = *get_hook_from_iffam(priv, iffam);
451 PRIV_RUNLOCK(priv, &priv_tracker);
455 PRIV_RUNLOCK(priv, &priv_tracker);
457 NG_OUTBOUND_THREAD_REF();
458 NG_SEND_DATA_ONLY(error, hook, m);
459 NG_OUTBOUND_THREAD_UNREF();
464 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
465 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
473 * Display an ioctl to the virtual interface
477 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
481 switch (command & IOC_DIRMASK) {
497 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
502 IOCPARM_LEN(command));
506 /************************************************************************
508 ************************************************************************/
511 * Constructor for a node
514 ng_iface_constructor(node_p node)
519 /* Allocate node and interface private structures */
520 priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
521 ifp = if_alloc(IFT_PROPVIRTUAL);
523 free(priv, M_NETGRAPH_IFACE);
527 rm_init(&priv->lock, "ng_iface private rmlock");
529 /* Link them together */
530 ifp->if_softc = priv;
533 /* Get an interface unit number */
534 priv->unit = alloc_unr(V_ng_iface_unit);
536 /* Link together node and private info */
537 NG_NODE_SET_PRIVATE(node, priv);
540 /* Initialize interface structure */
541 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
542 ifp->if_output = ng_iface_output;
543 ifp->if_start = ng_iface_start;
544 ifp->if_ioctl = ng_iface_ioctl;
545 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
546 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
547 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
548 ifp->if_addrlen = 0; /* XXX */
549 ifp->if_hdrlen = 0; /* XXX */
550 ifp->if_baudrate = 64000; /* XXX */
551 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
552 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
553 IFQ_SET_READY(&ifp->if_snd);
555 /* Give this node the same name as the interface (if possible) */
556 if (ng_name_node(node, ifp->if_xname) != 0)
557 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
560 /* Attach the interface */
562 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
569 * Give our ok for a hook to be added
572 ng_iface_newhook(node_p node, hook_p hook, const char *name)
574 const iffam_p iffam = get_iffam_from_name(name);
575 const priv_p priv = NG_NODE_PRIVATE(node);
579 return (EPFNOSUPPORT);
581 hookptr = get_hook_from_iffam(priv, iffam);
582 if (*hookptr != NULL) {
587 NG_HOOK_HI_STACK(hook);
588 NG_HOOK_SET_TO_INBOUND(hook);
594 * Receive a control message
597 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
599 const priv_p priv = NG_NODE_PRIVATE(node);
600 struct ifnet *const ifp = priv->ifp;
601 struct ng_mesg *resp = NULL;
605 NGI_GET_MSG(item, msg);
606 switch (msg->header.typecookie) {
607 case NGM_IFACE_COOKIE:
608 switch (msg->header.cmd) {
609 case NGM_IFACE_GET_IFNAME:
610 NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
615 strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
618 case NGM_IFACE_POINT2POINT:
619 case NGM_IFACE_BROADCAST:
622 /* Deny request if interface is UP */
623 if ((ifp->if_flags & IFF_UP) != 0)
627 switch (msg->header.cmd) {
628 case NGM_IFACE_POINT2POINT:
629 ifp->if_flags |= IFF_POINTOPOINT;
630 ifp->if_flags &= ~IFF_BROADCAST;
632 case NGM_IFACE_BROADCAST:
633 ifp->if_flags &= ~IFF_POINTOPOINT;
634 ifp->if_flags |= IFF_BROADCAST;
640 case NGM_IFACE_GET_IFINDEX:
641 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
646 *((uint32_t *)resp->data) = priv->ifp->if_index;
654 case NGM_FLOW_COOKIE:
655 switch (msg->header.cmd) {
657 if_link_state_change(ifp, LINK_STATE_UP);
659 case NGM_LINK_IS_DOWN:
660 if_link_state_change(ifp, LINK_STATE_DOWN);
670 NG_RESPOND_MSG(error, node, item, resp);
676 * Recive data from a hook. Pass the packet to the correct input routine.
679 ng_iface_rcvdata(hook_p hook, item_p item)
681 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
682 const iffam_p iffam = get_iffam_from_hook(priv, hook);
683 struct ifnet *const ifp = priv->ifp;
690 KASSERT(iffam != NULL, ("%s: iffam", __func__));
692 if ((ifp->if_flags & IFF_UP) == 0) {
697 /* Update interface stats */
698 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
699 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
701 /* Note receiving interface */
702 m->m_pkthdr.rcvif = ifp;
704 /* Berkeley packet filter */
705 ng_iface_bpftap(ifp, m, iffam->family);
708 switch (iffam->family) {
721 return (EAFNOSUPPORT);
723 random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
724 M_SETFIB(m, ifp->if_fib);
725 netisr_dispatch(isr, m);
730 * Shutdown and remove the node and its associated interface.
733 ng_iface_shutdown(node_p node)
735 const priv_p priv = NG_NODE_PRIVATE(node);
738 * The ifnet may be in a different vnet than the netgraph node,
739 * hence we have to change the current vnet context here.
741 CURVNET_SET_QUIET(priv->ifp->if_vnet);
742 bpfdetach(priv->ifp);
743 if_detach(priv->ifp);
747 free_unr(V_ng_iface_unit, priv->unit);
748 rm_destroy(&priv->lock);
749 free(priv, M_NETGRAPH_IFACE);
750 NG_NODE_SET_PRIVATE(node, NULL);
756 * Hook disconnection. Note that we do *not* shutdown when all
757 * hooks have been disconnected.
760 ng_iface_disconnect(hook_p hook)
762 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
763 const iffam_p iffam = get_iffam_from_hook(priv, hook);
766 panic("%s", __func__);
768 *get_hook_from_iffam(priv, iffam) = NULL;
774 * Handle loading and unloading for this node type.
777 ng_iface_mod_event(module_t mod, int event, void *data)
793 vnet_ng_iface_init(const void *unused)
796 V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
798 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
799 vnet_ng_iface_init, NULL);
802 vnet_ng_iface_uninit(const void *unused)
805 delete_unrhdr(V_ng_iface_unit);
807 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
808 vnet_ng_iface_uninit, NULL);