5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.7.2.5 2002/07/02 23:44:02 archie Exp $
40 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
44 * This node is also a system networking interface. It has
45 * a hook for each protocol (IP, AppleTalk, etc). Packets
46 * are simply relayed between the interface and the hooks.
48 * Interfaces are named ng0, ng1, etc. New nodes take the
49 * first available interface name.
51 * This node also includes Berkeley packet filter support.
55 #include "opt_inet6.h"
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/errno.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/sockio.h>
64 #include <sys/socket.h>
65 #include <sys/syslog.h>
66 #include <sys/libkern.h>
67 #include <sys/thread2.h>
70 #include <net/if_types.h>
71 #include <net/ifq_var.h>
72 #include <net/netisr.h>
75 #include <netinet/in.h>
77 #include <netgraph/ng_message.h>
78 #include <netgraph/netgraph.h>
79 #include <netgraph/ng_parse.h>
81 #include <netgraph/cisco/ng_cisco.h>
83 /* This struct describes one address family */
85 sa_family_t family; /* Address family */
86 const char *hookname; /* Name for hook */
88 typedef const struct iffam *iffam_p;
90 /* List of address families supported by our interface */
91 static const struct iffam gFamilies[] = {
92 { AF_INET, NG_IFACE_HOOK_INET },
93 { AF_INET6, NG_IFACE_HOOK_INET6 },
94 { AF_ATM, NG_IFACE_HOOK_ATM },
95 { AF_NATM, NG_IFACE_HOOK_NATM },
97 #define NUM_FAMILIES NELEM(gFamilies)
99 /* Node private data */
100 struct ng_iface_private {
101 struct ifnet *ifp; /* Our interface */
102 int unit; /* Interface unit number */
103 node_p node; /* Our netgraph node */
104 hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
106 typedef struct ng_iface_private *priv_p;
108 /* Interface methods */
109 static void ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *);
110 static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
112 static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
113 struct sockaddr *dst, struct rtentry *rt0);
114 static void ng_iface_bpftap(struct ifnet *ifp,
115 struct mbuf *m, sa_family_t family);
117 static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
120 /* Netgraph methods */
121 static ng_constructor_t ng_iface_constructor;
122 static ng_rcvmsg_t ng_iface_rcvmsg;
123 static ng_shutdown_t ng_iface_rmnode;
124 static ng_newhook_t ng_iface_newhook;
125 static ng_rcvdata_t ng_iface_rcvdata;
126 static ng_disconnect_t ng_iface_disconnect;
129 static iffam_p get_iffam_from_af(sa_family_t family);
130 static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
131 static iffam_p get_iffam_from_name(const char *name);
132 static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
134 /* Parse type for struct ng_iface_ifname */
135 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
136 NG_IFACE_IFACE_NAME_MAX + 1
138 static const struct ng_parse_type ng_iface_ifname_type = {
139 &ng_parse_fixedstring_type,
140 &ng_iface_ifname_info
143 /* Parse type for struct ng_cisco_ipaddr */
144 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
145 = NG_CISCO_IPADDR_TYPE_INFO;
146 static const struct ng_parse_type ng_cisco_ipaddr_type = {
147 &ng_parse_struct_type,
148 &ng_cisco_ipaddr_type_fields
151 /* List of commands and how to convert arguments to/from ASCII */
152 static const struct ng_cmdlist ng_iface_cmds[] = {
155 NGM_IFACE_GET_IFNAME,
158 &ng_iface_ifname_type
162 NGM_IFACE_POINT2POINT,
176 NGM_CISCO_GET_IPADDR,
179 &ng_cisco_ipaddr_type
184 /* Node type descriptor */
185 static struct ng_type typestruct = {
189 ng_iface_constructor,
200 NETGRAPH_INIT(iface, &typestruct);
202 /* We keep a bitmap indicating which unit numbers are free.
203 One means the unit number is free, zero means it's taken. */
204 static int *ng_iface_units = NULL;
205 static int ng_iface_units_len = 0;
207 #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
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))
272 * Find the first free unit number for a new interface.
273 * Increase the size of the unit bitmap as necessary.
275 static __inline__ int
276 ng_iface_get_unit(int *unit)
280 for (index = 0; index < ng_iface_units_len
281 && ng_iface_units[index] == 0; index++);
282 if (index == ng_iface_units_len) { /* extend array */
283 int i, *newarray, newlen;
285 newlen = (2 * ng_iface_units_len) + 4;
286 newarray = kmalloc(newlen * sizeof(*ng_iface_units),
287 M_NETGRAPH, M_NOWAIT);
288 if (newarray == NULL)
290 bcopy(ng_iface_units, newarray,
291 ng_iface_units_len * sizeof(*ng_iface_units));
292 for (i = ng_iface_units_len; i < newlen; i++)
294 if (ng_iface_units != NULL)
295 kfree(ng_iface_units, M_NETGRAPH);
296 ng_iface_units = newarray;
297 ng_iface_units_len = newlen;
299 bit = ffs(ng_iface_units[index]) - 1;
300 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
301 ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
302 ng_iface_units[index] &= ~(1 << bit);
303 *unit = (index * UNITS_BITSPERWORD) + bit;
308 * Free a no longer needed unit number.
310 static __inline__ void
311 ng_iface_free_unit(int unit)
315 index = unit / UNITS_BITSPERWORD;
316 bit = unit % UNITS_BITSPERWORD;
317 KASSERT(index < ng_iface_units_len,
318 ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
319 KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
320 ("%s: unit=%d is free", __func__, unit));
321 ng_iface_units[index] |= (1 << bit);
323 * XXX We could think about reducing the size of ng_iface_units[]
324 * XXX here if the last portion is all ones
328 /************************************************************************
330 ************************************************************************/
333 * Process an ioctl for the virtual interface
336 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
339 struct ifreq *const ifr = (struct ifreq *) data;
343 ng_iface_print_ioctl(ifp, command, data);
348 /* These two are mostly handled at a higher layer */
350 ifp->if_flags |= (IFF_UP | IFF_RUNNING);
351 ifq_clr_oactive(&ifp->if_snd);
359 * If the interface is marked up and stopped, then start it.
360 * If it is marked down and running, then stop it.
362 if (ifr->ifr_flags & IFF_UP) {
363 if (!(ifp->if_flags & IFF_RUNNING)) {
364 ifq_clr_oactive(&ifp->if_snd);
365 ifp->if_flags |= IFF_RUNNING;
368 if (ifp->if_flags & IFF_RUNNING) {
369 ifp->if_flags &= ~IFF_RUNNING;
370 ifq_clr_oactive(&ifp->if_snd);
375 /* Set the interface MTU */
377 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
378 || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
381 ifp->if_mtu = ifr->ifr_mtu;
384 /* Stuff that's not supported */
402 * This routine is called to deliver a packet out the interface.
403 * We simply look at the address family and relay the packet to
404 * the corresponding hook, if it exists and is connected.
408 ng_iface_output_serialized(struct ifnet *ifp, struct mbuf *m,
409 struct sockaddr *dst, struct rtentry *rt0)
411 const priv_p priv = (priv_p) ifp->if_softc;
412 const iffam_p iffam = get_iffam_from_af(dst->sa_family);
416 /* Check interface flags */
417 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
422 /* BPF writes need to be handled specially */
423 if (dst->sa_family == AF_UNSPEC) {
424 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
426 dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
429 m->m_pkthdr.len -= 4;
432 /* Berkeley packet filter */
433 ng_iface_bpftap(ifp, m, dst->sa_family);
435 /* Check address family to determine hook (if known) */
438 log(LOG_WARNING, "%s: can't handle af%d\n",
439 ifp->if_xname, (int)dst->sa_family);
440 return (EAFNOSUPPORT);
443 /* Copy length before the mbuf gets invalidated */
444 len = m->m_pkthdr.len;
446 /* Send packet; if hook is not connected, mbuf will get freed. */
447 NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
451 IFNET_STAT_INC(ifp, obytes, len);
452 IFNET_STAT_INC(ifp, opackets, 1);
458 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
459 struct sockaddr *dst, struct rtentry *rt0)
461 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
464 ifsq_serialize_hw(ifsq);
465 error = ng_iface_output_serialized(ifp, m, dst, rt0);
466 ifsq_deserialize_hw(ifsq);
472 * This routine should never be called
476 ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
478 kprintf("%s: %s called?", ifp->if_xname, __func__);
482 * Flash a packet by the BPF (requires prepending 4 byte AF header)
485 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
487 int32_t family4 = (int32_t)family;
489 KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
494 bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
501 * Display an ioctl to the virtual interface
505 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
509 switch (command & IOC_DIRMASK) {
525 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
530 IOCPARM_LEN(command));
534 /************************************************************************
536 ************************************************************************/
539 * Constructor for a node
542 ng_iface_constructor(node_p *nodep)
544 char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
550 /* Allocate node and interface private structures */
551 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
554 ifp = kmalloc(sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
556 kfree(priv, M_NETGRAPH);
560 /* Link them together */
561 ifp->if_softc = priv;
564 /* Get an interface unit number */
565 if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
566 kfree(ifp, M_NETGRAPH);
567 kfree(priv, M_NETGRAPH);
571 /* Call generic node constructor */
572 if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
573 ng_iface_free_unit(priv->unit);
574 kfree(ifp, M_NETGRAPH);
575 kfree(priv, M_NETGRAPH);
580 /* Link together node and private info */
581 node->private = priv;
584 /* Initialize interface structure */
585 if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
586 ifp->if_output = ng_iface_output;
587 ifp->if_start = ng_iface_start;
588 ifp->if_ioctl = ng_iface_ioctl;
589 ifp->if_watchdog = NULL;
590 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
591 ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
592 ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
593 ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
594 ifp->if_addrlen = 0; /* XXX */
595 ifp->if_hdrlen = 0; /* XXX */
596 ifp->if_baudrate = 64000; /* XXX */
598 /* Give this node the same name as the interface (if possible) */
599 bzero(ifname, sizeof(ifname));
600 strlcpy(ifname, ifp->if_xname, sizeof(ifname));
601 if (ng_name_node(node, ifname) != 0)
602 log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
604 /* Attach the interface */
605 if_attach(ifp, NULL);
606 bpfattach(ifp, DLT_NULL, sizeof(u_int));
613 * Give our ok for a hook to be added
616 ng_iface_newhook(node_p node, hook_p hook, const char *name)
618 const iffam_p iffam = get_iffam_from_name(name);
622 return (EPFNOSUPPORT);
623 hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
624 if (*hookptr != NULL)
631 * Receive a control message
634 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
635 const char *retaddr, struct ng_mesg **rptr)
637 const priv_p priv = node->private;
638 struct ifnet *const ifp = priv->ifp;
639 struct ng_mesg *resp = NULL;
642 switch (msg->header.typecookie) {
643 case NGM_IFACE_COOKIE:
644 switch (msg->header.cmd) {
645 case NGM_IFACE_GET_IFNAME:
647 struct ng_iface_ifname *arg;
649 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
654 arg = (struct ng_iface_ifname *)resp->data;
655 strlcpy(arg->ngif_name, ifp->if_xname,
656 sizeof(arg->ngif_name));
660 case NGM_IFACE_POINT2POINT:
661 case NGM_IFACE_BROADCAST:
664 /* Deny request if interface is UP */
665 if ((ifp->if_flags & IFF_UP) != 0)
669 switch (msg->header.cmd) {
670 case NGM_IFACE_POINT2POINT:
671 ifp->if_flags |= IFF_POINTOPOINT;
672 ifp->if_flags &= ~IFF_BROADCAST;
674 case NGM_IFACE_BROADCAST:
675 ifp->if_flags &= ~IFF_POINTOPOINT;
676 ifp->if_flags |= IFF_BROADCAST;
687 case NGM_CISCO_COOKIE:
688 switch (msg->header.cmd) {
689 case NGM_CISCO_GET_IPADDR: /* we understand this too */
691 struct ifaddr_container *ifac;
693 /* Return the first configured IP address */
694 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
696 struct ifaddr *ifa = ifac->ifa;
697 struct ng_cisco_ipaddr *ips;
699 if (ifa->ifa_addr->sa_family != AF_INET)
701 NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
706 ips = (struct ng_cisco_ipaddr *)resp->data;
707 ips->ipaddr = ((struct sockaddr_in *)
708 ifa->ifa_addr)->sin_addr;
709 ips->netmask = ((struct sockaddr_in *)
710 ifa->ifa_netmask)->sin_addr;
714 /* No IP addresses on this interface? */
716 error = EADDRNOTAVAIL;
731 kfree(resp, M_NETGRAPH);
732 kfree(msg, M_NETGRAPH);
737 * Recive data from a hook. Pass the packet to the correct input routine.
740 ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
742 const priv_p priv = hook->node->private;
743 const iffam_p iffam = get_iffam_from_hook(priv, hook);
744 struct ifnet *const ifp = priv->ifp;
748 KASSERT(iffam != NULL, ("%s: iffam", __func__));
749 KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
752 if ((ifp->if_flags & IFF_UP) == 0) {
753 NG_FREE_DATA(m, meta);
757 /* Update interface stats */
758 IFNET_STAT_INC(ifp, ipackets, 1);
759 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
761 /* Note receiving interface */
762 m->m_pkthdr.rcvif = ifp;
764 /* Berkeley packet filter */
765 ng_iface_bpftap(ifp, m, iffam->family);
767 /* Ignore any meta-data */
771 switch (iffam->family) {
784 return (EAFNOSUPPORT);
786 netisr_queue(isr, m);
791 * Shutdown and remove the node and its associated interface.
794 ng_iface_rmnode(node_p node)
796 const priv_p priv = node->private;
800 bpfdetach(priv->ifp);
801 if_detach(priv->ifp);
802 kfree(priv->ifp, M_NETGRAPH);
804 ng_iface_free_unit(priv->unit);
805 kfree(priv, M_NETGRAPH);
806 node->private = NULL;
812 * Hook disconnection. Note that we do *not* shutdown when all
813 * hooks have been disconnected.
816 ng_iface_disconnect(hook_p hook)
818 const priv_p priv = hook->node->private;
819 const iffam_p iffam = get_iffam_from_hook(priv, hook);
823 *get_hook_from_iffam(priv, iffam) = NULL;