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 $
31 * $DragonFly: src/sys/netgraph/eiface/ng_eiface.c,v 1.17 2008/03/07 11:34:20 sephe Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/errno.h>
41 #include <sys/sockio.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
44 #include <sys/serialize.h>
45 #include <sys/thread2.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
52 #include <netgraph/ng_message.h>
53 #include <netgraph/netgraph.h>
54 #include <netgraph/ng_parse.h>
55 #include "ng_eiface.h"
58 #include <net/ethernet.h>
59 #include <net/if_arp.h>
61 static const struct ng_parse_struct_field ng_eiface_par_fields
[]
62 = NG_EIFACE_PAR_FIELDS
;
64 static const struct ng_parse_type ng_eiface_par_type
= {
65 &ng_parse_struct_type
,
69 static const struct ng_cmdlist ng_eiface_cmdlist
[] = {
80 /* Node private data */
81 struct ng_eiface_private
{
82 struct arpcom arpcom
; /* per-interface network data */
83 struct ifnet
*ifp
; /* This interface */
84 node_p node
; /* Our netgraph node */
85 hook_p ether
; /* Hook for ethernet stream */
86 struct private *next
; /* When hung on the free list */
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
,
96 static void ng_eiface_print_ioctl(struct ifnet
*ifp
, int cmd
, caddr_t data
);
99 /* Netgraph methods */
100 static ng_constructor_t ng_eiface_constructor
;
101 static ng_rcvmsg_t ng_eiface_rcvmsg
;
102 static ng_shutdown_t ng_eiface_rmnode
;
103 static ng_newhook_t ng_eiface_newhook
;
104 static ng_rcvdata_t ng_eiface_rcvdata
;
105 static ng_connect_t ng_eiface_connect
;
106 static ng_disconnect_t ng_eiface_disconnect
;
108 /* Node type descriptor */
109 static struct ng_type typestruct
= {
113 ng_eiface_constructor
,
121 ng_eiface_disconnect
,
124 NETGRAPH_INIT(eiface
, &typestruct
);
126 static char ng_eiface_ifname
[] = NG_EIFACE_EIFACE_NAME
;
127 static int ng_eiface_next_unit
;
129 /************************************************************************
131 ************************************************************************/
134 * Process an ioctl for the virtual interface
137 ng_eiface_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
139 struct ifreq
*const ifr
= (struct ifreq
*) data
;
143 ng_eiface_print_ioctl(ifp
, cmd
, data
);
148 /* These two are mostly handled at a higher layer */
150 error
= ether_ioctl(ifp
, cmd
, data
);
158 * If the interface is marked up and stopped, then start it.
159 * If it is marked down and running, then stop it.
161 if (ifr
->ifr_flags
& IFF_UP
) {
162 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
163 ifp
->if_flags
&= ~(IFF_OACTIVE
);
164 ifp
->if_flags
|= IFF_RUNNING
;
167 if (ifp
->if_flags
& IFF_RUNNING
)
168 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
172 /* Set the interface MTU */
174 if (ifr
->ifr_mtu
> NG_EIFACE_MTU_MAX
175 || ifr
->ifr_mtu
< NG_EIFACE_MTU_MIN
)
178 ifp
->if_mtu
= ifr
->ifr_mtu
;
181 /* Stuff that's not supported */
199 ng_eiface_init(void *xsc
)
202 struct ifnet
*ifp
= sc
->ifp
;
206 ifp
->if_flags
|= IFF_RUNNING
;
207 ifp
->if_flags
&= ~IFF_OACTIVE
;
214 * This routine is called to deliver a packet out the interface.
215 * We simply relay the packet to
216 * the ether hook, if it is connected.
220 ng_eiface_start(struct ifnet
*ifp
)
222 const priv_p priv
= (priv_p
) ifp
->if_softc
;
227 /* Check interface flags */
228 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
231 /* Don't do anything if output is active */
232 if( ifp
->if_flags
& IFF_OACTIVE
)
235 ifp
->if_flags
|= IFF_OACTIVE
;
238 * Grab a packet to transmit.
240 IF_DEQUEUE(&ifp
->if_snd
, m
);
242 /* If there's nothing to send, return. */
245 ifp
->if_flags
&= ~IFF_OACTIVE
;
251 /* Copy length before the mbuf gets invalidated */
252 len
= m
->m_pkthdr
.len
;
254 /* Send packet; if hook is not connected, mbuf will get freed. */
255 NG_SEND_DATA(error
, priv
->ether
, m
, meta
);
259 ifp
->if_obytes
+= len
;
263 ifp
->if_flags
&= ~IFF_OACTIVE
;
270 * Display an ioctl to the virtual interface
274 ng_eiface_print_ioctl(struct ifnet
*ifp
, int cmd
, caddr_t data
)
278 switch (cmd
& IOC_DIRMASK
) {
294 log(LOG_DEBUG
, "%s: %s('%c', %d, char[%d])\n",
303 /************************************************************************
305 ************************************************************************/
308 * Constructor for a node
311 ng_eiface_constructor(node_p
*nodep
)
318 /* Allocate node and interface private structures */
319 MALLOC(priv
, priv_p
, sizeof(*priv
), M_NETGRAPH
, M_WAITOK
| M_ZERO
);
321 ifp
= &(priv
->arpcom
.ac_if
);
323 /* Link them together */
324 ifp
->if_softc
= priv
;
327 /* Call generic node constructor */
328 if ((error
= ng_make_node_common(&typestruct
, nodep
))) {
329 FREE(priv
, M_NETGRAPH
);
334 /* Link together node and private info */
335 node
->private = priv
;
338 /* Initialize interface structure */
339 if_initname(ifp
, ng_eiface_ifname
, ng_eiface_next_unit
++);
340 ifp
->if_init
= ng_eiface_init
;
341 ifp
->if_start
= ng_eiface_start
;
342 ifp
->if_ioctl
= ng_eiface_ioctl
;
343 ifp
->if_watchdog
= NULL
;
344 ifp
->if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
345 ifp
->if_flags
= (IFF_SIMPLEX
| IFF_BROADCAST
| IFF_MULTICAST
);
347 /* Give this node name *
348 bzero(ifname, sizeof(ifname));
349 ksprintf(ifname, "if%s", ifp->if_xname);
350 ng_name_node(node, ifname);
353 /* Attach the interface */
354 ether_ifattach(ifp
, priv
->arpcom
.ac_enaddr
, NULL
);
361 * Give our ok for a hook to be added
364 ng_eiface_newhook(node_p node
, hook_p hook
, const char *name
)
366 priv_p priv
= node
->private;
368 if (strcmp(name
, NG_EIFACE_HOOK_ETHER
))
369 return (EPFNOSUPPORT
);
370 if (priv
->ether
!= NULL
)
373 hook
->private = &priv
->ether
;
379 * Receive a control message
382 ng_eiface_rcvmsg(node_p node
, struct ng_mesg
*msg
,
383 const char *retaddr
, struct ng_mesg
**rptr
)
385 const priv_p priv
= node
->private;
386 struct ifnet
*const ifp
= priv
->ifp
;
387 struct ng_mesg
*resp
= NULL
;
390 switch (msg
->header
.typecookie
) {
391 case NGM_EIFACE_COOKIE
:
392 switch (msg
->header
.cmd
) {
396 struct ng_eiface_par
*eaddr
;
398 if (msg
->header
.arglen
!= sizeof(struct ng_eiface_par
))
403 eaddr
= (struct ng_eiface_par
*)(msg
->data
);
405 priv
->arpcom
.ac_enaddr
[0] = eaddr
->oct0
;
406 priv
->arpcom
.ac_enaddr
[1] = eaddr
->oct1
;
407 priv
->arpcom
.ac_enaddr
[2] = eaddr
->oct2
;
408 priv
->arpcom
.ac_enaddr
[3] = eaddr
->oct3
;
409 priv
->arpcom
.ac_enaddr
[4] = eaddr
->oct4
;
410 priv
->arpcom
.ac_enaddr
[5] = eaddr
->oct5
;
415 case NGM_EIFACE_GET_IFNAME
:
417 struct ng_eiface_ifname
*arg
;
419 NG_MKRESPONSE(resp
, msg
, sizeof(*arg
), M_NOWAIT
);
424 arg
= (struct ng_eiface_ifname
*) resp
->data
;
425 ksprintf(arg
->ngif_name
,
426 "%s", ifp
->if_xname
); /* XXX: strings */
430 case NGM_EIFACE_GET_IFADDRS
:
432 struct ifaddr_container
*ifac
;
436 #define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
438 /* Determine size of response and allocate it */
440 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
442 buflen
+= SA_SIZE(ifac
->ifa
->ifa_addr
);
443 NG_MKRESPONSE(resp
, msg
, buflen
, M_NOWAIT
);
451 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
],
453 struct ifaddr
*ifa
= ifac
->ifa
;
454 const int len
= SA_SIZE(ifa
->ifa_addr
);
457 log(LOG_ERR
, "%s: len changed?\n",
461 bcopy(ifa
->ifa_addr
, ptr
, len
);
481 FREE(resp
, M_NETGRAPH
);
482 FREE(msg
, M_NETGRAPH
);
487 * Recive data from a hook. Pass the packet to the ether_input routine.
490 ng_eiface_rcvdata(hook_p hook
, struct mbuf
*m
, meta_p meta
)
492 const priv_p priv
= hook
->node
->private;
493 struct ifnet
*const ifp
= priv
->ifp
;
496 /* Meta-data is end its life here... */
501 kprintf("ng_eiface: mbuf is null.\n");
505 lwkt_serialize_enter(ifp
->if_serializer
);
506 if ( !(ifp
->if_flags
& IFF_UP
) ) {
507 lwkt_serialize_exit(ifp
->if_serializer
);
511 /* Note receiving interface */
512 m
->m_pkthdr
.rcvif
= ifp
;
514 /* Update interface stats */
519 ifp
->if_input(ifp
, m
);
520 lwkt_serialize_exit(ifp
->if_serializer
);
527 * Because the BSD networking code doesn't support the removal of
528 * networking interfaces, iface nodes (once created) are persistent.
529 * So this method breaks all connections and marks the interface
530 * down, but does not remove the node.
533 ng_eiface_rmnode(node_p node
)
535 const priv_p priv
= node
->private;
536 struct ifnet
*const ifp
= priv
->ifp
;
539 node
->flags
&= ~NG_INVALID
;
540 lwkt_serialize_enter(ifp
->if_serializer
);
541 ifp
->if_flags
&= ~(IFF_UP
| IFF_RUNNING
| IFF_OACTIVE
);
542 lwkt_serialize_exit(ifp
->if_serializer
);
548 * This is called once we've already connected a new hook to the other node.
549 * It gives us a chance to balk at the last minute.
552 ng_eiface_connect(hook_p hook
)
554 /* be really amiable and just say "YUP that's OK by me! " */
562 ng_eiface_disconnect(hook_p hook
)
564 const priv_p priv
= hook
->node
->private;