3 * Copyright (c) 1999-2001, Vitaly V Belekhov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
38 #include <sys/errno.h>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/netisr.h>
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 #include <netgraph/ng_parse.h>
54 #include <netgraph/ng_eiface.h>
57 #include <net/ethernet.h>
58 #include <net/if_arp.h>
60 static const struct ng_parse_struct_field ng_eiface_par_fields[]
61 = NG_EIFACE_PAR_FIELDS;
63 static const struct ng_parse_type ng_eiface_par_type = {
64 &ng_parse_struct_type,
68 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
80 /* Node private data */
81 struct ng_eiface_private {
82 struct ifnet *ifp; /* This interface */
83 int unit; /* Interface unit number */
84 struct arpcom arpcom; /* per-interface network data */
85 node_p node; /* Our netgraph node */
86 hook_p ether; /* Hook for ethernet stream */
88 typedef struct ng_eiface_private *priv_p;
90 /* Interface methods */
91 static void ng_eiface_init(void *xsc);
92 static void ng_eiface_start(struct ifnet *ifp);
93 static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
95 static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
98 /* Netgraph methods */
99 static ng_constructor_t ng_eiface_constructor;
100 static ng_rcvmsg_t ng_eiface_rcvmsg;
101 static ng_shutdown_t ng_eiface_rmnode;
102 static ng_newhook_t ng_eiface_newhook;
103 static ng_rcvdata_t ng_eiface_rcvdata;
104 static ng_connect_t ng_eiface_connect;
105 static ng_disconnect_t ng_eiface_disconnect;
107 /* Node type descriptor */
108 static struct ng_type typestruct = {
112 ng_eiface_constructor,
119 ng_eiface_disconnect,
122 NETGRAPH_INIT(eiface, &typestruct);
124 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME;
126 /* We keep a bitmap indicating which unit numbers are free.
127 One means the unit number is free, zero means it's taken. */
128 static int *ng_eiface_units = NULL;
129 static int ng_eiface_units_len = 0;
130 static int ng_units_in_use = 0;
132 #define UNITS_BITSPERWORD (sizeof(*ng_eiface_units) * NBBY)
135 /************************************************************************
137 ************************************************************************/
139 * Find the first free unit number for a new interface.
140 * Increase the size of the unit bitmap as necessary.
142 static __inline__ int
143 ng_eiface_get_unit(int *unit)
147 for (index = 0; index < ng_eiface_units_len
148 && ng_eiface_units[index] == 0; index++);
149 if (index == ng_eiface_units_len) { /* extend array */
150 int i, *newarray, newlen;
152 newlen = (2 * ng_eiface_units_len) + 4;
153 MALLOC(newarray, int *, newlen * sizeof(*ng_eiface_units),
154 M_NETGRAPH, M_NOWAIT);
155 if (newarray == NULL)
157 bcopy(ng_eiface_units, newarray,
158 ng_eiface_units_len * sizeof(*ng_eiface_units));
159 for (i = ng_eiface_units_len; i < newlen; i++)
161 if (ng_eiface_units != NULL)
162 FREE(ng_eiface_units, M_NETGRAPH);
163 ng_eiface_units = newarray;
164 ng_eiface_units_len = newlen;
166 bit = ffs(ng_eiface_units[index]) - 1;
167 KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
168 ("%s: word=%d bit=%d", __func__, ng_eiface_units[index], bit));
169 ng_eiface_units[index] &= ~(1 << bit);
170 *unit = (index * UNITS_BITSPERWORD) + bit;
176 * Free a no longer needed unit number.
178 static __inline__ void
179 ng_eiface_free_unit(int unit)
183 index = unit / UNITS_BITSPERWORD;
184 bit = unit % UNITS_BITSPERWORD;
185 KASSERT(index < ng_eiface_units_len,
186 ("%s: unit=%d len=%d", __func__, unit, ng_eiface_units_len));
187 KASSERT((ng_eiface_units[index] & (1 << bit)) == 0,
188 ("%s: unit=%d is free", __func__, unit));
189 ng_eiface_units[index] |= (1 << bit);
191 * XXX We could think about reducing the size of ng_eiface_units[]
192 * XXX here if the last portion is all ones
193 * XXX At least free it if no more units.
194 * Needed if we are to eventually be able to unload.
197 if (ng_units_in_use == 0) { /* XXX make SMP safe */
198 FREE(ng_eiface_units, M_NETGRAPH);
199 ng_eiface_units_len = 0;
200 ng_eiface_units = NULL;
204 /************************************************************************
206 ************************************************************************/
209 * Process an ioctl for the virtual interface
212 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
214 struct ifreq *const ifr = (struct ifreq *)data;
218 ng_eiface_print_ioctl(ifp, command, data);
223 /* These two are mostly handled at a higher layer */
225 error = ether_ioctl(ifp, command, data);
233 * If the interface is marked up and stopped, then
234 * start it. If it is marked down and running,
237 if (ifr->ifr_flags & IFF_UP) {
238 if (!(ifp->if_flags & IFF_RUNNING)) {
239 ifp->if_flags &= ~(IFF_OACTIVE);
240 ifp->if_flags |= IFF_RUNNING;
243 if (ifp->if_flags & IFF_RUNNING)
245 &= ~(IFF_RUNNING | IFF_OACTIVE);
249 /* Set the interface MTU */
251 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX
252 || ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
255 ifp->if_mtu = ifr->ifr_mtu;
258 /* Stuff that's not supported */
276 ng_eiface_init(void *xsc)
279 struct ifnet *ifp = sc->ifp;
284 ifp->if_flags |= IFF_RUNNING;
285 ifp->if_flags &= ~IFF_OACTIVE;
291 * We simply relay the packet to the ether hook, if it is connected.
292 * We have been throughthe netgraph locking an are guaranteed to
293 * be the only code running in this node at this time.
296 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
298 struct ifnet *ifp = arg1;
299 const priv_p priv = (priv_p) ifp->if_softc;
303 /* Check interface flags */
304 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
307 /* Don't do anything if output is active */
308 if (ifp->if_flags & IFF_OACTIVE)
311 ifp->if_flags |= IFF_OACTIVE;
314 * Grab a packet to transmit.
316 IF_DEQUEUE(&ifp->if_snd, m);
318 /* If there's nothing to send, return. */
320 ifp->if_flags &= ~IFF_OACTIVE;
324 /* Berkeley packet filter
325 * Pass packet to bpf if there is a listener.
326 * XXX is this safe? locking?
331 /* Copy length before the mbuf gets invalidated */
332 len = m->m_pkthdr.len;
335 * Send packet; if hook is not connected, mbuf will get
338 NG_SEND_DATA_ONLY(error, priv->ether, m);
342 ifp->if_obytes += len;
345 ifp->if_flags &= ~IFF_OACTIVE;
350 * This routine is called to deliver a packet out the interface.
351 * We simply queue the netgraph version to be called when netgraph locking
352 * allows it to happen.
353 * Until we know what the rest of the networking code is doing for
354 * locking, we don't know how we will interact with it.
355 * Take comfort from the fact that the ifnet struct is part of our
356 * private info and can't go away while we are queued.
357 * [Though we don't know it is still there now....]
358 * it is possible we don't gain anything from this because
359 * we would like to get the mbuf and queue it as data
360 * somehow, but we can't and if we did would we solve anything?
363 ng_eiface_start(struct ifnet *ifp)
366 const priv_p priv = (priv_p) ifp->if_softc;
368 ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
373 * Display an ioctl to the virtual interface
377 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data){
380 switch (command & IOC_DIRMASK)
397 log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
398 ifp->if_name, ifp->if_unit,
402 IOCPARM_LEN(command));
406 /************************************************************************
408 ************************************************************************/
411 * Constructor for a node
414 ng_eiface_constructor(node_p node)
420 /* Allocate node and interface private structures */
421 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_WAITOK);
425 bzero(priv, sizeof(*priv));
427 ifp = &(priv->arpcom.ac_if);
429 /* Link them together */
430 ifp->if_softc = priv;
433 /* Get an interface unit number */
434 if ((error = ng_eiface_get_unit(&priv->unit)) != 0) {
435 FREE(priv, M_NETGRAPH);
439 /* Link together node and private info */
440 NG_NODE_SET_PRIVATE(node, priv);
443 /* Initialize interface structure */
444 ifp->if_name = ng_eiface_ifname;
445 ifp->if_unit = priv->unit;
446 ifp->if_init = ng_eiface_init;
447 ifp->if_output = ether_output;
448 ifp->if_start = ng_eiface_start;
449 ifp->if_ioctl = ng_eiface_ioctl;
450 ifp->if_watchdog = NULL;
451 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
452 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
455 * Give this node name * bzero(ifname, sizeof(ifname));
456 * sprintf(ifname, "if%s%d", ifp->if_name, ifp->if_unit); (void)
457 * ng_name_node(node, ifname);
460 /* Attach the interface */
461 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
468 * Give our ok for a hook to be added
471 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
473 priv_p priv = NG_NODE_PRIVATE(node);
475 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
476 return (EPFNOSUPPORT);
477 if (priv->ether != NULL)
480 NG_HOOK_SET_PRIVATE(hook, &priv->ether);
486 * Receive a control message
489 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
491 priv_p priv = NG_NODE_PRIVATE(node);
492 struct ifnet *const ifp = priv->ifp;
493 struct ng_mesg *resp = NULL;
497 NGI_GET_MSG(item, msg);
498 switch (msg->header.typecookie) {
499 case NGM_EIFACE_COOKIE:
500 switch (msg->header.cmd) {
503 struct ng_eiface_par *eaddr;
505 struct sockaddr_dl *sdl;
507 if (msg->header.arglen != sizeof(struct ng_eiface_par)){
511 eaddr = (struct ng_eiface_par *)(msg->data);
513 priv->arpcom.ac_enaddr[0] = eaddr->oct0;
514 priv->arpcom.ac_enaddr[1] = eaddr->oct1;
515 priv->arpcom.ac_enaddr[2] = eaddr->oct2;
516 priv->arpcom.ac_enaddr[3] = eaddr->oct3;
517 priv->arpcom.ac_enaddr[4] = eaddr->oct4;
518 priv->arpcom.ac_enaddr[5] = eaddr->oct5;
520 /* And put it in the ifaddr list */
521 #define IFP2AC(IFP) ((struct arpcom *)IFP)
522 TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) {
523 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
524 if (sdl->sdl_type == IFT_ETHER) {
525 bcopy((IFP2AC(ifp))->ac_enaddr,
526 LLADDR(sdl), ifp->if_addrlen);
533 case NGM_EIFACE_GET_IFNAME:
535 struct ng_eiface_ifname *arg;
537 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
542 arg = (struct ng_eiface_ifname *)resp->data;
543 sprintf(arg->ngif_name,
544 "%s%d", ifp->if_name, ifp->if_unit);
548 case NGM_EIFACE_GET_IFADDRS:
554 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
556 /* Determine size of response and allocate it */
558 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
559 buflen += SA_SIZE(ifa->ifa_addr);
560 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
567 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
568 const int len = SA_SIZE(ifa->ifa_addr);
571 log(LOG_ERR, "%s%d: len changed?\n",
572 ifp->if_name, ifp->if_unit);
575 bcopy(ifa->ifa_addr, ptr, len);
586 } /* end of inner switch() */
592 NG_RESPOND_MSG(error, node, item, resp);
598 * Recive data from a hook. Pass the packet to the ether_input routine.
601 ng_eiface_rcvdata(hook_p hook, item_p item)
603 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
604 struct ifnet *const ifp = priv->ifp;
606 struct ether_header *eh;
611 /* Meta-data ends its life here... */
616 printf("ng_eiface: mbuf is null.\n");
619 if (!(ifp->if_flags & IFF_UP)) {
623 /* Note receiving interface */
624 m->m_pkthdr.rcvif = ifp;
626 /* Update interface stats */
629 eh = mtod(m, struct ether_header *);
630 ether_type = ntohs(eh->ether_type);
633 m->m_pkthdr.len -= sizeof(*eh);
634 m->m_len -= sizeof(*eh);
636 m->m_data += sizeof(*eh);
638 if (ether_type == ETHERTYPE_ARP) {
639 m->m_len = m->m_next->m_len;
640 m->m_data = m->m_next->m_data;
645 ether_input(ifp, eh, m);
655 ng_eiface_rmnode(node_p node)
657 priv_p priv = NG_NODE_PRIVATE(node);
658 struct ifnet *const ifp = priv->ifp;
660 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
661 ng_eiface_free_unit(priv->unit);
662 FREE(priv, M_NETGRAPH);
663 NG_NODE_SET_PRIVATE(node, NULL);
670 * This is called once we've already connected a new hook to the other node.
671 * It gives us a chance to balk at the last minute.
674 ng_eiface_connect(hook_p hook)
676 /* be really amiable and just say "YUP that's OK by me! " */
684 ng_eiface_disconnect(hook_p hook)
686 priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));