4 * Copyright (c) 1999-2000, Vitaly V Belekhov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: ng_eiface.c,v 1.14 2000/03/15 12:28:44 vitaly Exp $
30 * $FreeBSD: src/sys/netgraph/ng_eiface.c,v 1.4.2.5 2002/12/17 21:47:48 julian Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
42 #include <sys/serialize.h>
43 #include <sys/thread2.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/netisr.h>
51 #include <netgraph/ng_message.h>
52 #include <netgraph/netgraph.h>
53 #include <netgraph/ng_parse.h>
54 #include "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[] = {
79 /* Node private data */
80 struct ng_eiface_private {
81 struct arpcom arpcom; /* per-interface network data */
82 struct ifnet *ifp; /* This interface */
83 node_p node; /* Our netgraph node */
84 hook_p ether; /* Hook for ethernet stream */
85 struct private *next; /* When hung on the free list */
87 typedef struct ng_eiface_private *priv_p;
89 /* Interface methods */
90 static void ng_eiface_init(void *xsc);
91 static void ng_eiface_start(struct ifnet *ifp, struct ifaltq_subque *);
92 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,
120 ng_eiface_disconnect,
123 NETGRAPH_INIT(eiface, &typestruct);
125 static char ng_eiface_ifname[] = NG_EIFACE_EIFACE_NAME;
126 static int ng_eiface_next_unit;
128 /************************************************************************
130 ************************************************************************/
133 * Process an ioctl for the virtual interface
136 ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
138 struct ifreq *const ifr = (struct ifreq *) data;
142 ng_eiface_print_ioctl(ifp, cmd, data);
147 /* These two are mostly handled at a higher layer */
149 error = ether_ioctl(ifp, cmd, data);
157 * If the interface is marked up and stopped, then start it.
158 * If it is marked down and running, then stop it.
160 if (ifr->ifr_flags & IFF_UP) {
161 if (!(ifp->if_flags & IFF_RUNNING)) {
162 ifq_clr_oactive(&ifp->if_snd);
163 ifp->if_flags |= IFF_RUNNING;
166 if (ifp->if_flags & IFF_RUNNING) {
167 ifp->if_flags &= ~IFF_RUNNING;
168 ifq_clr_oactive(&ifp->if_snd);
173 /* Set the interface MTU */
175 if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX
176 || ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
179 ifp->if_mtu = ifr->ifr_mtu;
182 /* Stuff that's not supported */
200 ng_eiface_init(void *xsc)
203 struct ifnet *ifp = sc->ifp;
207 ifp->if_flags |= IFF_RUNNING;
208 ifq_clr_oactive(&ifp->if_snd);
215 * This routine is called to deliver a packet out the interface.
216 * We simply relay the packet to
217 * the ether hook, if it is connected.
221 ng_eiface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
223 const priv_p priv = (priv_p) ifp->if_softc;
228 /* Check interface flags */
229 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
232 /* Don't do anything if output is active */
233 if(ifq_is_oactive(&ifp->if_snd))
236 ifq_set_oactive(&ifp->if_snd);
239 * Grab a packet to transmit.
241 m = ifq_dequeue(&ifp->if_snd);
243 /* If there's nothing to send, return. */
246 ifq_clr_oactive(&ifp->if_snd);
252 /* Copy length before the mbuf gets invalidated */
253 len = m->m_pkthdr.len;
255 /* Send packet; if hook is not connected, mbuf will get freed. */
256 NG_SEND_DATA(error, priv->ether, m, meta);
260 IFNET_STAT_INC(ifp, obytes, len);
261 IFNET_STAT_INC(ifp, opackets, 1);
264 ifq_clr_oactive(&ifp->if_snd);
271 * Display an ioctl to the virtual interface
275 ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data)
279 switch (cmd & IOC_DIRMASK) {
295 log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
304 /************************************************************************
306 ************************************************************************/
309 * Constructor for a node
312 ng_eiface_constructor(node_p *nodep)
319 /* Allocate node and interface private structures */
320 priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
322 ifp = &(priv->arpcom.ac_if);
324 /* Link them together */
325 ifp->if_softc = priv;
328 /* Call generic node constructor */
329 if ((error = ng_make_node_common(&typestruct, nodep))) {
330 kfree(priv, M_NETGRAPH);
335 /* Link together node and private info */
336 node->private = priv;
339 /* Initialize interface structure */
340 if_initname(ifp, ng_eiface_ifname, ng_eiface_next_unit++);
341 ifp->if_init = ng_eiface_init;
342 ifp->if_start = ng_eiface_start;
343 ifp->if_ioctl = ng_eiface_ioctl;
344 ifp->if_watchdog = NULL;
345 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
346 ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
348 /* Give this node name *
349 bzero(ifname, sizeof(ifname));
350 ksprintf(ifname, "if%s", ifp->if_xname);
351 ng_name_node(node, ifname);
354 /* Attach the interface */
355 ether_ifattach(ifp, priv->arpcom.ac_enaddr, NULL);
362 * Give our ok for a hook to be added
365 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
367 priv_p priv = node->private;
369 if (strcmp(name, NG_EIFACE_HOOK_ETHER))
370 return (EPFNOSUPPORT);
371 if (priv->ether != NULL)
374 hook->private = &priv->ether;
380 * Receive a control message
383 ng_eiface_rcvmsg(node_p node, struct ng_mesg *msg,
384 const char *retaddr, struct ng_mesg **rptr)
386 const priv_p priv = node->private;
387 struct ifnet *const ifp = priv->ifp;
388 struct ng_mesg *resp = NULL;
391 switch (msg->header.typecookie) {
392 case NGM_EIFACE_COOKIE:
393 switch (msg->header.cmd) {
397 struct ng_eiface_par *eaddr;
399 if (msg->header.arglen != sizeof(struct ng_eiface_par))
404 eaddr = (struct ng_eiface_par *)(msg->data);
406 priv->arpcom.ac_enaddr[0] = eaddr->oct0;
407 priv->arpcom.ac_enaddr[1] = eaddr->oct1;
408 priv->arpcom.ac_enaddr[2] = eaddr->oct2;
409 priv->arpcom.ac_enaddr[3] = eaddr->oct3;
410 priv->arpcom.ac_enaddr[4] = eaddr->oct4;
411 priv->arpcom.ac_enaddr[5] = eaddr->oct5;
416 case NGM_EIFACE_GET_IFNAME:
418 struct ng_eiface_ifname *arg;
420 NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
425 arg = (struct ng_eiface_ifname *) resp->data;
426 ksprintf(arg->ngif_name,
427 "%s", ifp->if_xname); /* XXX: strings */
431 case NGM_EIFACE_GET_IFADDRS:
433 struct ifaddr_container *ifac;
437 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
439 /* Determine size of response and allocate it */
441 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
443 buflen += SA_SIZE(ifac->ifa->ifa_addr);
444 NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
452 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
454 struct ifaddr *ifa = ifac->ifa;
455 const int len = SA_SIZE(ifa->ifa_addr);
458 log(LOG_ERR, "%s: len changed?\n",
462 bcopy(ifa->ifa_addr, ptr, len);
482 kfree(resp, M_NETGRAPH);
483 kfree(msg, M_NETGRAPH);
488 * Recive data from a hook. Pass the packet to the ether_input routine.
491 ng_eiface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
493 const priv_p priv = hook->node->private;
494 struct ifnet *const ifp = priv->ifp;
497 /* Meta-data is end its life here... */
501 kprintf("ng_eiface: mbuf is null.\n");
505 if ( !(ifp->if_flags & IFF_UP) )
508 /* Note receiving interface */
509 m->m_pkthdr.rcvif = ifp;
511 /* Update interface stats */
512 IFNET_STAT_INC(ifp, ipackets, 1);
516 ifp->if_input(ifp, m);
523 * Because the BSD networking code doesn't support the removal of
524 * networking interfaces, iface nodes (once created) are persistent.
525 * So this method breaks all connections and marks the interface
526 * down, but does not remove the node.
529 ng_eiface_rmnode(node_p node)
531 const priv_p priv = node->private;
532 struct ifnet *const ifp = priv->ifp;
535 node->flags &= ~NG_INVALID;
536 ifnet_serialize_all(ifp);
537 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
538 ifq_clr_oactive(&ifp->if_snd);
539 ifnet_deserialize_all(ifp);
545 * This is called once we've already connected a new hook to the other node.
546 * It gives us a chance to balk at the last minute.
549 ng_eiface_connect(hook_p hook)
551 /* be really amiable and just say "YUP that's OK by me! " */
559 ng_eiface_disconnect(hook_p hook)
561 const priv_p priv = hook->node->private;