4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * 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, IPX, 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.
53 #include "opt_atalk.h"
55 #include "opt_inet6.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
64 #include <sys/errno.h>
65 #include <sys/random.h>
66 #include <sys/sockio.h>
67 #include <sys/socket.h>
68 #include <sys/syslog.h>
69 #include <sys/libkern.h>
72 #include <net/if_types.h>
74 #include <net/netisr.h>
76 #include <netinet/in.h>
78 #include <netgraph/ng_message.h>
79 #include <netgraph/netgraph.h>
80 #include <netgraph/ng_parse.h>
81 #include <netgraph/ng_iface.h>
82 #include <netgraph/ng_cisco.h>
84 #ifdef NG_SEPARATE_MALLOC
85 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
87 #define M_NETGRAPH_IFACE M_NETGRAPH
90 /* This struct describes one address family */
92 sa_family_t family; /* Address family */
93 const char *hookname; /* Name for hook */
95 typedef const struct iffam *iffam_p;
97 /* List of address families supported by our interface */
98 const static struct iffam gFamilies[] = {
99 { AF_INET, NG_IFACE_HOOK_INET },
100 { AF_INET6, NG_IFACE_HOOK_INET6 },
101 { AF_APPLETALK, NG_IFACE_HOOK_ATALK },
102 { AF_IPX, NG_IFACE_HOOK_IPX },
103 { AF_ATM, NG_IFACE_HOOK_ATM },
104 { AF_NATM, NG_IFACE_HOOK_NATM },
106 #define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
108 /* Node private data */
109 struct ng_iface_private {
110 struct ifnet *ifp; /* Our interface */
111 int unit; /* Interface unit number */
112 node_p node; /* Our netgraph node */
113 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
115 typedef struct ng_iface_private *priv_p;
117 /* Interface methods */
118 static void ng_iface_start(struct ifnet *ifp);
119 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
120 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
121 struct sockaddr *dst, struct rtentry *rt0);
122 static void ng_iface_bpftap(struct ifnet *ifp,
123 struct mbuf *m, sa_family_t family);
125 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
128 /* Netgraph methods */
129 static ng_constructor_t ng_iface_constructor;
130 static ng_rcvmsg_t ng_iface_rcvmsg;
131 static ng_shutdown_t ng_iface_shutdown;
132 static ng_newhook_t ng_iface_newhook;
133 static ng_rcvdata_t ng_iface_rcvdata;
134 static ng_disconnect_t ng_iface_disconnect;
137 static iffam_p get_iffam_from_af(sa_family_t family);
138 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
139 static iffam_p get_iffam_from_name(const char *name);
140 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
142 /* Parse type for struct ng_iface_ifname */
143 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
144 NG_IFACE_IFACE_NAME_MAX + 1
146 static const struct ng_parse_type ng_iface_ifname_type = {
147 &ng_parse_fixedstring_type,
148 &ng_iface_ifname_info
151 /* Parse type for struct ng_cisco_ipaddr */
152 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
153 = NG_CISCO_IPADDR_TYPE_INFO;
154 static const struct ng_parse_type ng_cisco_ipaddr_type = {
155 &ng_parse_struct_type,
156 &ng_cisco_ipaddr_type_fields
159 /* List of commands and how to convert arguments to/from ASCII */
160 static const struct ng_cmdlist ng_iface_cmds[] = {
163 NGM_IFACE_GET_IFNAME,
166 &ng_iface_ifname_type
170 NGM_IFACE_POINT2POINT,
184 NGM_CISCO_GET_IPADDR,
187 &ng_cisco_ipaddr_type
191 NGM_IFACE_GET_IFINDEX,
194 &ng_parse_uint32_type
199 /* Node type descriptor */
200 static struct ng_type typestruct = {
201 .version = NG_ABI_VERSION,
202 .name = NG_IFACE_NODE_TYPE,
203 .constructor = ng_iface_constructor,
204 .rcvmsg = ng_iface_rcvmsg,
205 .shutdown = ng_iface_shutdown,
206 .newhook = ng_iface_newhook,
207 .rcvdata = ng_iface_rcvdata,
208 .disconnect = ng_iface_disconnect,
209 .cmdlist = ng_iface_cmds,
211 NETGRAPH_INIT(iface, &typestruct);
213 /* We keep a bitmap indicating which unit numbers are free.
214 One means the unit number is free, zero means it's taken. */
215 static int *ng_iface_units = NULL;
216 static int ng_iface_units_len = 0;
217 static int ng_units_in_use = 0;
219 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
221 /************************************************************************
223 ************************************************************************/
226 * Get the family descriptor from the family ID
228 static __inline__ iffam_p
229 get_iffam_from_af(sa_family_t family)
234 for (k = 0; k < NUM_FAMILIES; k++) {
235 iffam = &gFamilies[k];
236 if (iffam->family == family)
243 * Get the family descriptor from the hook
245 static __inline__ iffam_p
246 get_iffam_from_hook(priv_p priv, hook_p hook)
250 for (k = 0; k < NUM_FAMILIES; k++)
251 if (priv->hooks[k] == hook)
252 return (&gFamilies[k]);
257 * Get the hook from the iffam descriptor
260 static __inline__ hook_p *
261 get_hook_from_iffam(priv_p priv, iffam_p iffam)
263 return (&priv->hooks[iffam - gFamilies]);
267 * Get the iffam descriptor from the name
269 static __inline__ iffam_p
270 get_iffam_from_name(const char *name)
275 for (k = 0; k < NUM_FAMILIES; k++) {
276 iffam = &gFamilies[k];
277 if (!strcmp(iffam->hookname, name))
284 * Find the first free unit number for a new interface.
285 * Increase the size of the unit bitmap as necessary.
287 static __inline__ int
288 ng_iface_get_unit(int *unit)
292 for (index = 0; index < ng_iface_units_len
293 && ng_iface_units[index] == 0; index++);
294 if (index == ng_iface_units_len) { /* extend array */
295 int i, *newarray, newlen;
297 newlen = (2 * ng_iface_units_len) + 4;
298 MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
299 M_NETGRAPH_IFACE, M_NOWAIT);
300 if (newarray == NULL)
302 bcopy(ng_iface_units, newarray,
303 ng_iface_units_len * sizeof(*ng_iface_units));
304 for (i = ng_iface_units_len; i < newlen; i++)
306 if (ng_iface_units != NULL)
307 FREE(ng_iface_units, M_NETGRAPH_IFACE);
308 ng_iface_units = newarray;
309 ng_iface_units_len = newlen;
311 bit = ffs(ng_iface_units[index]) - 1;
312 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
313 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
314 ng_iface_units[index] &= ~(1 << bit);
315 *unit = (index * UNITS_BITSPERWORD) + bit;
321 * Free a no longer needed unit number.
323 static __inline__ void
324 ng_iface_free_unit(int unit)
328 index = unit / UNITS_BITSPERWORD;
329 bit = unit % UNITS_BITSPERWORD;
330 KASSERT(index < ng_iface_units_len,
331 ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
332 KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
333 ("%s: unit=%d is free", __func__, unit));
334 ng_iface_units[index] |= (1 << bit);
336 * XXX We could think about reducing the size of ng_iface_units[]
337 * XXX here if the last portion is all ones
338 * XXX At least free it if no more units.
339 * Needed if we are to eventually be able to unload.
342 if (ng_units_in_use == 0) { /* XXX make SMP safe */
343 FREE(ng_iface_units, M_NETGRAPH_IFACE);
344 ng_iface_units_len = 0;
345 ng_iface_units = NULL;
349 /************************************************************************
351 ************************************************************************/
354 * Process an ioctl for the virtual interface
357 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
359 struct ifreq *const ifr = (struct ifreq *) data;
363 ng_iface_print_ioctl(ifp, command, data);
368 /* These two are mostly handled at a higher layer */
370 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
371 ifp->if_flags &= ~(IFF_OACTIVE);
379 * If the interface is marked up and stopped, then start it.
380 * If it is marked down and running, then stop it.
382 if (ifr->ifr_flags & IFF_UP) {
383 if (!(ifp->if_flags & IFF_RUNNING)) {
384 ifp->if_flags &= ~(IFF_OACTIVE);
385 ifp->if_flags |= IFF_RUNNING;
388 if (ifp->if_flags & IFF_RUNNING)
389 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
393 /* Set the interface MTU */
395 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
396 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
399 ifp->if_mtu = ifr->ifr_mtu;
402 /* Stuff that's not supported */
420 * This routine is called to deliver a packet out the interface.
421 * We simply look at the address family and relay the packet to
422 * the corresponding hook, if it exists and is connected.
426 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
427 struct sockaddr *dst, struct rtentry *rt0)
429 const priv_p priv = (priv_p) ifp->if_softc;
430 const iffam_p iffam = get_iffam_from_af(dst->sa_family);
433 /* Check interface flags */
434 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
439 /* BPF writes need to be handled specially */
440 if (dst->sa_family == AF_UNSPEC) {
441 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
443 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
446 m->m_pkthdr.len -= 4;
449 /* Berkeley packet filter */
450 ng_iface_bpftap(ifp, m, dst->sa_family);
452 /* Check address family to determine hook (if known) */
455 log(LOG_WARNING, "%s: can't handle af%d\n",
456 ifp->if_xname, (int)dst->sa_family);
457 return (EAFNOSUPPORT);
460 /* Copy length before the mbuf gets invalidated */
461 len = m->m_pkthdr.len;
463 /* Send packet; if hook is not connected, mbuf will get freed. */
464 NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
468 ifp->if_obytes += len;
475 * This routine should never be called
479 ng_iface_start(struct ifnet *ifp)
481 if_printf(ifp, "%s called?", __func__);
485 * Flash a packet by the BPF (requires prepending 4 byte AF header)
486 * Note the phoney mbuf; this is OK because BPF treats it read-only.
489 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
491 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
492 if (ifp->if_bpf != NULL) {
493 int32_t family4 = (int32_t)family;
494 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
500 * Display an ioctl to the virtual interface
504 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
508 switch (command & IOC_DIRMASK) {
524 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
529 IOCPARM_LEN(command));
533 /************************************************************************
535 ************************************************************************/
538 * Constructor for a node
541 ng_iface_constructor(node_p node)
543 char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
548 /* Allocate node and interface private structures */
549 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
552 MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
554 FREE(priv, M_NETGRAPH_IFACE);
558 /* Link them together */
559 ifp->if_softc = priv;
562 /* Get an interface unit number */
563 if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
564 FREE(ifp, M_NETGRAPH_IFACE);
565 FREE(priv, M_NETGRAPH_IFACE);
569 /* Link together node and private info */
570 NG_NODE_SET_PRIVATE(node, priv);
573 /* Initialize interface structure */
574 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
575 ifp->if_output = ng_iface_output;
576 ifp->if_start = ng_iface_start;
577 ifp->if_ioctl = ng_iface_ioctl;
578 ifp->if_watchdog = NULL;
579 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
580 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
581 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
582 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
583 ifp->if_addrlen = 0; /* XXX */
584 ifp->if_hdrlen = 0; /* XXX */
585 ifp->if_baudrate = 64000; /* XXX */
586 TAILQ_INIT(&ifp->if_addrhead);
588 /* Give this node the same name as the interface (if possible) */
589 bzero(ifname, sizeof(ifname));
590 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
591 if (ng_name_node(node, ifname) != 0)
592 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
594 /* Attach the interface */
596 bpfattach(ifp, DLT_NULL, sizeof(u_int));
603 * Give our ok for a hook to be added
606 ng_iface_newhook(node_p node, hook_p hook, const char *name)
608 const iffam_p iffam = get_iffam_from_name(name);
612 return (EPFNOSUPPORT);
613 hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
614 if (*hookptr != NULL)
621 * Receive a control message
624 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
626 const priv_p priv = NG_NODE_PRIVATE(node);
627 struct ifnet *const ifp = priv->ifp;
628 struct ng_mesg *resp = NULL;
632 NGI_GET_MSG(item, msg);
633 switch (msg->header.typecookie) {
634 case NGM_IFACE_COOKIE:
635 switch (msg->header.cmd) {
636 case NGM_IFACE_GET_IFNAME:
638 struct ng_iface_ifname *arg;
640 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
645 arg = (struct ng_iface_ifname *)resp->data;
646 strlcpy(arg->ngif_name, ifp->if_xname,
647 sizeof(arg->ngif_name));
651 case NGM_IFACE_POINT2POINT:
652 case NGM_IFACE_BROADCAST:
655 /* Deny request if interface is UP */
656 if ((ifp->if_flags & IFF_UP) != 0)
660 switch (msg->header.cmd) {
661 case NGM_IFACE_POINT2POINT:
662 ifp->if_flags |= IFF_POINTOPOINT;
663 ifp->if_flags &= ~IFF_BROADCAST;
665 case NGM_IFACE_BROADCAST:
666 ifp->if_flags &= ~IFF_POINTOPOINT;
667 ifp->if_flags |= IFF_BROADCAST;
673 case NGM_IFACE_GET_IFINDEX:
674 NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
679 *((uint32_t *)resp->data) = priv->ifp->if_index;
687 case NGM_CISCO_COOKIE:
688 switch (msg->header.cmd) {
689 case NGM_CISCO_GET_IPADDR: /* we understand this too */
693 /* Return the first configured IP address */
694 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
695 struct ng_cisco_ipaddr *ips;
697 if (ifa->ifa_addr->sa_family != AF_INET)
699 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
704 ips = (struct ng_cisco_ipaddr *)resp->data;
705 ips->ipaddr = ((struct sockaddr_in *)
706 ifa->ifa_addr)->sin_addr;
707 ips->netmask = ((struct sockaddr_in *)
708 ifa->ifa_netmask)->sin_addr;
712 /* No IP addresses on this interface? */
714 error = EADDRNOTAVAIL;
726 NG_RESPOND_MSG(error, node, item, resp);
732 * Recive data from a hook. Pass the packet to the correct input routine.
735 ng_iface_rcvdata(hook_p hook, item_p item)
737 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
738 const iffam_p iffam = get_iffam_from_hook(priv, hook);
739 struct ifnet *const ifp = priv->ifp;
746 KASSERT(iffam != NULL, ("%s: iffam", __func__));
748 if ((ifp->if_flags & IFF_UP) == 0) {
753 /* Update interface stats */
755 ifp->if_ibytes += m->m_pkthdr.len;
757 /* Note receiving interface */
758 m->m_pkthdr.rcvif = ifp;
760 /* Berkeley packet filter */
761 ng_iface_bpftap(ifp, m, iffam->family);
764 switch (iffam->family) {
787 return (EAFNOSUPPORT);
789 /* First chunk of an mbuf contains good junk */
790 if (harvest.point_to_point)
791 random_harvest(m, 16, 3, 0, RANDOM_NET);
792 netisr_dispatch(isr, m);
797 * Shutdown and remove the node and its associated interface.
800 ng_iface_shutdown(node_p node)
802 const priv_p priv = NG_NODE_PRIVATE(node);
804 bpfdetach(priv->ifp);
805 if_detach(priv->ifp);
806 FREE(priv->ifp, M_NETGRAPH_IFACE);
808 ng_iface_free_unit(priv->unit);
809 FREE(priv, M_NETGRAPH_IFACE);
810 NG_NODE_SET_PRIVATE(node, NULL);
816 * Hook disconnection. Note that we do *not* shutdown when all
817 * hooks have been disconnected.
820 ng_iface_disconnect(hook_p hook)
822 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
823 const iffam_p iffam = get_iffam_from_hook(priv, hook);
827 *get_hook_from_iffam(priv, iffam) = NULL;