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, IPX, 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.
55 #include "opt_atalk.h"
57 #include "opt_inet6.h"
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/errno.h>
67 #include <sys/random.h>
68 #include <sys/sockio.h>
69 #include <sys/socket.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
74 #include <net/if_types.h>
76 #include <net/netisr.h>
78 #include <netinet/in.h>
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include <netgraph/ng_iface.h>
84 #include <netgraph/ng_cisco.h>
86 #ifdef NG_SEPARATE_MALLOC
87 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
89 #define M_NETGRAPH_IFACE M_NETGRAPH
92 /* This struct describes one address family */
94 sa_family_t family; /* Address family */
95 const char *hookname; /* Name for hook */
97 typedef const struct iffam *iffam_p;
99 /* List of address families supported by our interface */
100 const static struct iffam gFamilies[] = {
101 { AF_INET, NG_IFACE_HOOK_INET },
102 { AF_INET6, NG_IFACE_HOOK_INET6 },
103 { AF_APPLETALK, NG_IFACE_HOOK_ATALK },
104 { AF_IPX, NG_IFACE_HOOK_IPX },
105 { AF_ATM, NG_IFACE_HOOK_ATM },
106 { AF_NATM, NG_IFACE_HOOK_NATM },
108 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
110 /* Node private data */
111 struct ng_iface_private {
112 struct ifnet *ifp; /* Our interface */
113 int unit; /* Interface unit number */
114 node_p node; /* Our netgraph node */
115 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
117 typedef struct ng_iface_private *priv_p;
119 /* Interface methods */
120 static void ng_iface_start(struct ifnet *ifp);
121 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
122 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
123 struct sockaddr *dst, struct rtentry *rt0);
124 static void ng_iface_bpftap(struct ifnet *ifp,
125 struct mbuf *m, sa_family_t family);
126 static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
129 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
132 /* Netgraph methods */
133 static int ng_iface_mod_event(module_t, int, void *);
134 static ng_constructor_t ng_iface_constructor;
135 static ng_rcvmsg_t ng_iface_rcvmsg;
136 static ng_shutdown_t ng_iface_shutdown;
137 static ng_newhook_t ng_iface_newhook;
138 static ng_rcvdata_t ng_iface_rcvdata;
139 static ng_disconnect_t ng_iface_disconnect;
142 static iffam_p get_iffam_from_af(sa_family_t family);
143 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
144 static iffam_p get_iffam_from_name(const char *name);
145 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
147 /* Parse type for struct ng_cisco_ipaddr */
148 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
149 = NG_CISCO_IPADDR_TYPE_INFO;
150 static const struct ng_parse_type ng_cisco_ipaddr_type = {
151 &ng_parse_struct_type,
152 &ng_cisco_ipaddr_type_fields
155 /* List of commands and how to convert arguments to/from ASCII */
156 static const struct ng_cmdlist ng_iface_cmds[] = {
159 NGM_IFACE_GET_IFNAME,
162 &ng_parse_string_type
166 NGM_IFACE_POINT2POINT,
180 NGM_CISCO_GET_IPADDR,
183 &ng_cisco_ipaddr_type
187 NGM_IFACE_GET_IFINDEX,
190 &ng_parse_uint32_type
195 /* Node type descriptor */
196 static struct ng_type typestruct = {
197 .version = NG_ABI_VERSION,
198 .name = NG_IFACE_NODE_TYPE,
199 .mod_event = ng_iface_mod_event,
200 .constructor = ng_iface_constructor,
201 .rcvmsg = ng_iface_rcvmsg,
202 .shutdown = ng_iface_shutdown,
203 .newhook = ng_iface_newhook,
204 .rcvdata = ng_iface_rcvdata,
205 .disconnect = ng_iface_disconnect,
206 .cmdlist = ng_iface_cmds,
208 NETGRAPH_INIT(iface, &typestruct);
210 static struct unrhdr *ng_iface_unit;
212 /************************************************************************
214 ************************************************************************/
217 * Get the family descriptor from the family ID
219 static __inline iffam_p
220 get_iffam_from_af(sa_family_t family)
225 for (k = 0; k < NUM_FAMILIES; k++) {
226 iffam = &gFamilies[k];
227 if (iffam->family == family)
234 * Get the family descriptor from the hook
236 static __inline iffam_p
237 get_iffam_from_hook(priv_p priv, hook_p hook)
241 for (k = 0; k < NUM_FAMILIES; k++)
242 if (priv->hooks[k] == hook)
243 return (&gFamilies[k]);
248 * Get the hook from the iffam descriptor
251 static __inline hook_p *
252 get_hook_from_iffam(priv_p priv, iffam_p iffam)
254 return (&priv->hooks[iffam - gFamilies]);
258 * Get the iffam descriptor from the name
260 static __inline iffam_p
261 get_iffam_from_name(const char *name)
266 for (k = 0; k < NUM_FAMILIES; k++) {
267 iffam = &gFamilies[k];
268 if (!strcmp(iffam->hookname, name))
274 /************************************************************************
276 ************************************************************************/
279 * Process an ioctl for the virtual interface
282 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
284 struct ifreq *const ifr = (struct ifreq *) data;
288 ng_iface_print_ioctl(ifp, command, data);
293 /* These two are mostly handled at a higher layer */
295 ifp->if_flags |= IFF_UP;
296 ifp->if_drv_flags |= IFF_DRV_RUNNING;
297 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
305 * If the interface is marked up and stopped, then start it.
306 * If it is marked down and running, then stop it.
308 if (ifr->ifr_flags & IFF_UP) {
309 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
310 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
311 ifp->if_drv_flags |= IFF_DRV_RUNNING;
314 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
315 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
320 /* Set the interface MTU */
322 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
323 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
326 ifp->if_mtu = ifr->ifr_mtu;
329 /* Stuff that's not supported */
347 * This routine is called to deliver a packet out the interface.
348 * We simply look at the address family and relay the packet to
349 * the corresponding hook, if it exists and is connected.
353 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
354 struct sockaddr *dst, struct rtentry *rt0)
360 /* Check interface flags */
361 if (!((ifp->if_flags & IFF_UP) &&
362 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
367 /* Protect from deadly infinite recursion. */
368 while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, NULL))) {
369 if (*(struct ifnet **)(mtag + 1) == ifp) {
370 log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
375 mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
381 *(struct ifnet **)(mtag + 1) = ifp;
382 m_tag_prepend(m, mtag);
384 /* BPF writes need to be handled specially. */
385 if (dst->sa_family == AF_UNSPEC) {
386 bcopy(dst->sa_data, &af, sizeof(af));
390 /* Berkeley packet filter */
391 ng_iface_bpftap(ifp, m, dst->sa_family);
393 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
394 M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
396 IFQ_LOCK(&ifp->if_snd);
397 IFQ_INC_DROPS(&ifp->if_snd);
398 IFQ_UNLOCK(&ifp->if_snd);
402 *(sa_family_t *)m->m_data = dst->sa_family;
403 IFQ_HANDOFF(ifp, m, error);
405 error = ng_iface_send(ifp, m, dst->sa_family);
411 * Start method is used only when ALTQ is enabled.
414 ng_iface_start(struct ifnet *ifp)
419 KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
422 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
425 sa = *mtod(m, sa_family_t *);
426 m_adj(m, sizeof(sa_family_t));
427 ng_iface_send(ifp, m, sa);
432 * Flash a packet by the BPF (requires prepending 4 byte AF header)
433 * Note the phoney mbuf; this is OK because BPF treats it read-only.
436 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
438 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
439 if (bpf_peers_present(ifp->if_bpf)) {
440 int32_t family4 = (int32_t)family;
441 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
446 * This routine does actual delivery of the packet into the
447 * netgraph(4). It is called from ng_iface_start() and
451 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
453 const priv_p priv = (priv_p) ifp->if_softc;
454 const iffam_p iffam = get_iffam_from_af(sa);
458 /* Check address family to determine hook (if known) */
461 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
462 return (EAFNOSUPPORT);
465 /* Copy length before the mbuf gets invalidated. */
466 len = m->m_pkthdr.len;
468 /* Send packet. If hook is not connected,
469 mbuf will get freed. */
470 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
474 ifp->if_obytes += len;
483 * Display an ioctl to the virtual interface
487 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
491 switch (command & IOC_DIRMASK) {
507 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
512 IOCPARM_LEN(command));
516 /************************************************************************
518 ************************************************************************/
521 * Constructor for a node
524 ng_iface_constructor(node_p node)
529 /* Allocate node and interface private structures */
530 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
533 ifp = if_alloc(IFT_PROPVIRTUAL);
535 FREE(priv, M_NETGRAPH_IFACE);
539 /* Link them together */
540 ifp->if_softc = priv;
543 /* Get an interface unit number */
544 priv->unit = alloc_unr(ng_iface_unit);
546 /* Link together node and private info */
547 NG_NODE_SET_PRIVATE(node, priv);
550 /* Initialize interface structure */
551 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
552 ifp->if_output = ng_iface_output;
553 ifp->if_start = ng_iface_start;
554 ifp->if_ioctl = ng_iface_ioctl;
555 ifp->if_watchdog = NULL;
556 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
557 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
558 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
559 ifp->if_addrlen = 0; /* XXX */
560 ifp->if_hdrlen = 0; /* XXX */
561 ifp->if_baudrate = 64000; /* XXX */
562 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
563 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
564 IFQ_SET_READY(&ifp->if_snd);
566 /* Give this node the same name as the interface (if possible) */
567 if (ng_name_node(node, ifp->if_xname) != 0)
568 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
571 /* Attach the interface */
573 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
580 * Give our ok for a hook to be added
583 ng_iface_newhook(node_p node, hook_p hook, const char *name)
585 const iffam_p iffam = get_iffam_from_name(name);
589 return (EPFNOSUPPORT);
590 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
591 if (*hookptr != NULL)
594 NG_HOOK_HI_STACK(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:
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_CISCO_COOKIE:
660 switch (msg->header.cmd) {
661 case NGM_CISCO_GET_IPADDR: /* we understand this too */
665 /* Return the first configured IP address */
666 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
667 struct ng_cisco_ipaddr *ips;
669 if (ifa->ifa_addr->sa_family != AF_INET)
671 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
676 ips = (struct ng_cisco_ipaddr *)resp->data;
677 ips->ipaddr = ((struct sockaddr_in *)
678 ifa->ifa_addr)->sin_addr;
679 ips->netmask = ((struct sockaddr_in *)
680 ifa->ifa_netmask)->sin_addr;
684 /* No IP addresses on this interface? */
686 error = EADDRNOTAVAIL;
694 case NGM_FLOW_COOKIE:
695 switch (msg->header.cmd) {
697 ifp->if_drv_flags |= IFF_DRV_RUNNING;
699 case NGM_LINK_IS_DOWN:
700 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
710 NG_RESPOND_MSG(error, node, item, resp);
716 * Recive data from a hook. Pass the packet to the correct input routine.
719 ng_iface_rcvdata(hook_p hook, item_p item)
721 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
722 const iffam_p iffam = get_iffam_from_hook(priv, hook);
723 struct ifnet *const ifp = priv->ifp;
730 KASSERT(iffam != NULL, ("%s: iffam", __func__));
732 if ((ifp->if_flags & IFF_UP) == 0) {
737 /* Update interface stats */
739 ifp->if_ibytes += m->m_pkthdr.len;
741 /* Note receiving interface */
742 m->m_pkthdr.rcvif = ifp;
744 /* Berkeley packet filter */
745 ng_iface_bpftap(ifp, m, iffam->family);
748 switch (iffam->family) {
771 return (EAFNOSUPPORT);
773 /* First chunk of an mbuf contains good junk */
774 if (harvest.point_to_point)
775 random_harvest(m, 16, 3, 0, RANDOM_NET);
776 netisr_dispatch(isr, m);
781 * Shutdown and remove the node and its associated interface.
784 ng_iface_shutdown(node_p node)
786 const priv_p priv = NG_NODE_PRIVATE(node);
788 bpfdetach(priv->ifp);
789 if_detach(priv->ifp);
792 free_unr(ng_iface_unit, priv->unit);
793 FREE(priv, M_NETGRAPH_IFACE);
794 NG_NODE_SET_PRIVATE(node, NULL);
800 * Hook disconnection. Note that we do *not* shutdown when all
801 * hooks have been disconnected.
804 ng_iface_disconnect(hook_p hook)
806 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
807 const iffam_p iffam = get_iffam_from_hook(priv, hook);
811 *get_hook_from_iffam(priv, iffam) = NULL;
816 * Handle loading and unloading for this node type.
819 ng_iface_mod_event(module_t mod, int event, void *data)
825 ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
828 delete_unrhdr(ng_iface_unit);