xref: /freebsd/sys/netgraph/ng_eiface.c (revision aa3860851b9f6a6002d135b1cac7736e0995eedc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  *
5  * Copyright (c) 1999-2001, Vitaly V Belekhov
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/eventhandler.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/sockio.h>
41 #include <sys/socket.h>
42 #include <sys/syslog.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_media.h>
47 #include <net/if_private.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 #include <net/vnet.h>
52 
53 #include <netgraph/ng_message.h>
54 #include <netgraph/netgraph.h>
55 #include <netgraph/ng_parse.h>
56 #include <netgraph/ng_eiface.h>
57 
58 #include <net/bpf.h>
59 #include <net/ethernet.h>
60 #include <net/if_arp.h>
61 
62 static const struct ng_cmdlist ng_eiface_cmdlist[] = {
63 	{
64 	  NGM_EIFACE_COOKIE,
65 	  NGM_EIFACE_GET_IFNAME,
66 	  "getifname",
67 	  NULL,
68 	  &ng_parse_string_type
69 	},
70 	{
71 	  NGM_EIFACE_COOKIE,
72 	  NGM_EIFACE_SET,
73 	  "set",
74 	  &ng_parse_enaddr_type,
75 	  NULL
76 	},
77 	{ 0 }
78 };
79 
80 /* Node private data */
81 struct ng_eiface_private {
82 	struct ifnet	*ifp;		/* per-interface network data */
83 	struct ifmedia	media;		/* (fake) media information */
84 	int		link_status;	/* fake */
85 	int		unit;		/* Interface unit number */
86 	node_p		node;		/* Our netgraph node */
87 	hook_p		ether;		/* Hook for ethernet stream */
88 };
89 typedef struct ng_eiface_private *priv_p;
90 
91 /* Interface methods */
92 static void	ng_eiface_init(void *xsc);
93 static void	ng_eiface_start(struct ifnet *ifp);
94 static int	ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
95 #ifdef DEBUG
96 static void	ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
97 #endif
98 
99 /* Netgraph methods */
100 static int		ng_eiface_mod_event(module_t, int, void *);
101 static ng_constructor_t	ng_eiface_constructor;
102 static ng_rcvmsg_t	ng_eiface_rcvmsg;
103 static ng_shutdown_t	ng_eiface_rmnode;
104 static ng_newhook_t	ng_eiface_newhook;
105 static ng_rcvdata_t	ng_eiface_rcvdata;
106 static ng_disconnect_t	ng_eiface_disconnect;
107 
108 /* Node type descriptor */
109 static struct ng_type typestruct = {
110 	.version =	NG_ABI_VERSION,
111 	.name =		NG_EIFACE_NODE_TYPE,
112 	.mod_event =	ng_eiface_mod_event,
113 	.constructor =	ng_eiface_constructor,
114 	.rcvmsg =	ng_eiface_rcvmsg,
115 	.shutdown =	ng_eiface_rmnode,
116 	.newhook =	ng_eiface_newhook,
117 	.rcvdata =	ng_eiface_rcvdata,
118 	.disconnect =	ng_eiface_disconnect,
119 	.cmdlist =	ng_eiface_cmdlist
120 };
121 NETGRAPH_INIT(eiface, &typestruct);
122 
123 VNET_DEFINE_STATIC(struct unrhdr *, ng_eiface_unit);
124 #define	V_ng_eiface_unit		VNET(ng_eiface_unit)
125 
126 /************************************************************************
127 			INTERFACE STUFF
128  ************************************************************************/
129 
130 /*
131  * Process an ioctl for the virtual interface
132  */
133 static int
ng_eiface_ioctl(struct ifnet * ifp,u_long command,caddr_t data)134 ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
135 {
136 	const priv_p priv = (priv_p)ifp->if_softc;
137 	struct ifreq *const ifr = (struct ifreq *)data;
138 	int error = 0;
139 
140 #ifdef DEBUG
141 	ng_eiface_print_ioctl(ifp, command, data);
142 #endif
143 	switch (command) {
144 	/* These two are mostly handled at a higher layer */
145 	case SIOCSIFADDR:
146 		error = ether_ioctl(ifp, command, data);
147 		break;
148 	case SIOCGIFADDR:
149 		break;
150 
151 	/* Set flags */
152 	case SIOCSIFFLAGS:
153 		/*
154 		 * If the interface is marked up and stopped, then start it.
155 		 * If it is marked down and running, then stop it.
156 		 */
157 		if (ifp->if_flags & IFF_UP) {
158 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
159 				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
160 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
161 			}
162 		} else {
163 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
164 				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
165 				    IFF_DRV_OACTIVE);
166 		}
167 		break;
168 
169 	/* Set the interface MTU */
170 	case SIOCSIFMTU:
171 		if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
172 		    ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
173 			error = EINVAL;
174 		else
175 			ifp->if_mtu = ifr->ifr_mtu;
176 		break;
177 
178 	/* (Fake) media type manipulation */
179 	case SIOCSIFMEDIA:
180 	case SIOCGIFMEDIA:
181 		error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
182 		break;
183 
184 	/* Stuff that's not supported */
185 	case SIOCADDMULTI:
186 	case SIOCDELMULTI:
187 		error = 0;
188 		break;
189 	case SIOCSIFPHYS:
190 		error = EOPNOTSUPP;
191 		break;
192 
193 	default:
194 		error = EINVAL;
195 		break;
196 	}
197 	return (error);
198 }
199 
200 static void
ng_eiface_init(void * xsc)201 ng_eiface_init(void *xsc)
202 {
203 	priv_p sc = xsc;
204 	struct ifnet *ifp = sc->ifp;
205 
206 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
207 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
208 }
209 
210 /*
211  * We simply relay the packet to the "ether" hook, if it is connected.
212  * We have been through the netgraph locking and are guaranteed to
213  * be the only code running in this node at this time.
214  */
215 static void
ng_eiface_start2(node_p node,hook_p hook,void * arg1,int arg2)216 ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
217 {
218 	struct ifnet *ifp = arg1;
219 	const priv_p priv = (priv_p)ifp->if_softc;
220 	int error = 0;
221 	struct mbuf *m;
222 
223 	/* Check interface flags */
224 
225 	if (!((ifp->if_flags & IFF_UP) &&
226 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
227 		return;
228 
229 	for (;;) {
230 		/*
231 		 * Grab a packet to transmit.
232 		 */
233 		IF_DEQUEUE(&ifp->if_snd, m);
234 
235 		/* If there's nothing to send, break. */
236 		if (m == NULL)
237 			break;
238 
239 		/* Peel the mbuf off any stale tags */
240 		m_tag_delete_chain(m, NULL);
241 
242 		/*
243 		 * Berkeley packet filter.
244 		 * Pass packet to bpf if there is a listener.
245 		 * XXX is this safe? locking?
246 		 */
247 		BPF_MTAP(ifp, m);
248 
249 		if (ifp->if_flags & IFF_MONITOR) {
250 			if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
251 			m_freem(m);
252 			continue;
253 		}
254 
255 		/*
256 		 * Send packet; if hook is not connected, mbuf will get
257 		 * freed.
258 		 */
259 		NG_OUTBOUND_THREAD_REF();
260 		CURVNET_SET_QUIET(priv->node->nd_vnet);
261 		NG_SEND_DATA_ONLY(error, priv->ether, m);
262 		CURVNET_RESTORE();
263 		NG_OUTBOUND_THREAD_UNREF();
264 
265 		/* Update stats */
266 		if (error == 0)
267 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
268 		else
269 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
270 	}
271 
272 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
273 
274 	return;
275 }
276 
277 /*
278  * This routine is called to deliver a packet out the interface.
279  * We simply queue the netgraph version to be called when netgraph locking
280  * allows it to happen.
281  * Until we know what the rest of the networking code is doing for
282  * locking, we don't know how we will interact with it.
283  * Take comfort from the fact that the ifnet struct is part of our
284  * private info and can't go away while we are queued.
285  * [Though we don't know it is still there now....]
286  * it is possible we don't gain anything from this because
287  * we would like to get the mbuf and queue it as data
288  * somehow, but we can't and if we did would we solve anything?
289  */
290 static void
ng_eiface_start(struct ifnet * ifp)291 ng_eiface_start(struct ifnet *ifp)
292 {
293 	const priv_p priv = (priv_p)ifp->if_softc;
294 
295 	/* Don't do anything if output is active */
296 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
297 		return;
298 
299 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
300 
301 	if (ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0) != 0)
302 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
303 }
304 
305 #ifdef DEBUG
306 /*
307  * Display an ioctl to the virtual interface
308  */
309 
310 static void
ng_eiface_print_ioctl(struct ifnet * ifp,int command,caddr_t data)311 ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
312 {
313 	char *str;
314 
315 	switch (command & IOC_DIRMASK) {
316 	case IOC_VOID:
317 		str = "IO";
318 		break;
319 	case IOC_OUT:
320 		str = "IOR";
321 		break;
322 	case IOC_IN:
323 		str = "IOW";
324 		break;
325 	case IOC_INOUT:
326 		str = "IORW";
327 		break;
328 	default:
329 		str = "IO??";
330 	}
331 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
332 	    ifp->if_xname,
333 	    str,
334 	    IOCGROUP(command),
335 	    command & 0xff,
336 	    IOCPARM_LEN(command));
337 }
338 #endif /* DEBUG */
339 
340 /*
341  * ifmedia stuff
342  */
343 static int
ng_eiface_mediachange(struct ifnet * ifp)344 ng_eiface_mediachange(struct ifnet *ifp)
345 {
346 	const priv_p priv = (priv_p)ifp->if_softc;
347 	struct ifmedia *ifm = &priv->media;
348 
349 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
350 		return (EINVAL);
351 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
352 		ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T);
353 	else
354 		ifp->if_baudrate = ifmedia_baudrate(ifm->ifm_media);
355 
356 	return (0);
357 }
358 
359 static void
ng_eiface_mediastatus(struct ifnet * ifp,struct ifmediareq * ifmr)360 ng_eiface_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
361 {
362 	const priv_p priv = (priv_p)ifp->if_softc;
363 	struct ifmedia *ifm = &priv->media;
364 
365 	if (ifm->ifm_cur->ifm_media == (IFM_ETHER | IFM_AUTO) &&
366 	    (priv->link_status & IFM_ACTIVE))
367 		ifmr->ifm_active = IFM_ETHER | IFM_1000_T | IFM_FDX;
368 	else
369 		ifmr->ifm_active = ifm->ifm_cur->ifm_media;
370 	ifmr->ifm_status = priv->link_status;
371 
372 	return;
373 }
374 
375 /************************************************************************
376 			NETGRAPH NODE STUFF
377  ************************************************************************/
378 
379 /*
380  * Constructor for a node
381  */
382 static int
ng_eiface_constructor(node_p node)383 ng_eiface_constructor(node_p node)
384 {
385 	struct ifnet *ifp;
386 	priv_p priv;
387 	struct ether_addr eaddr;
388 
389 	/* Allocate node and interface private structures */
390 	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
391 	ifp = priv->ifp = if_alloc(IFT_ETHER);
392 
393 	/* Link them together */
394 	ifp->if_softc = priv;
395 
396 	/* Get an interface unit number */
397 	priv->unit = alloc_unr(V_ng_eiface_unit);
398 
399 	/* Link together node and private info */
400 	NG_NODE_SET_PRIVATE(node, priv);
401 	priv->node = node;
402 
403 	/* Initialize interface structure */
404 	if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
405 	ifp->if_init = ng_eiface_init;
406 	ifp->if_output = ether_output;
407 	ifp->if_start = ng_eiface_start;
408 	ifp->if_ioctl = ng_eiface_ioctl;
409 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
410 	ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
411 	ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
412 	ifp->if_capenable = IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
413 	ifmedia_init(&priv->media, 0, ng_eiface_mediachange,
414 	    ng_eiface_mediastatus);
415 	ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T, 0, NULL);
416 	ifmedia_add(&priv->media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
417 	ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX, 0, NULL);
418 	ifmedia_add(&priv->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
419 	ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T, 0, NULL);
420 	ifmedia_add(&priv->media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
421 	ifmedia_add(&priv->media, IFM_ETHER | IFM_10G_T | IFM_FDX, 0, NULL);
422 	ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
423 	ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO);
424 	priv->link_status = IFM_AVALID;
425 
426 	/* Give this node the same name as the interface (if possible) */
427 	if (ng_name_node(node, ifp->if_xname) != 0)
428 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
429 		    ifp->if_xname);
430 
431 	/* Attach the interface */
432 	ether_gen_addr(ifp, &eaddr);
433 	ether_ifattach(ifp, eaddr.octet);
434 	ifp->if_baudrate = ifmedia_baudrate(IFM_ETHER | IFM_1000_T);
435 
436 	/* Done */
437 	return (0);
438 }
439 
440 /*
441  * Give our ok for a hook to be added
442  */
443 static int
ng_eiface_newhook(node_p node,hook_p hook,const char * name)444 ng_eiface_newhook(node_p node, hook_p hook, const char *name)
445 {
446 	priv_p priv = NG_NODE_PRIVATE(node);
447 	struct ifnet *ifp = priv->ifp;
448 
449 	if (strcmp(name, NG_EIFACE_HOOK_ETHER))
450 		return (EPFNOSUPPORT);
451 	if (priv->ether != NULL)
452 		return (EISCONN);
453 	priv->ether = hook;
454 	NG_HOOK_SET_PRIVATE(hook, &priv->ether);
455 	NG_HOOK_SET_TO_INBOUND(hook);
456 
457 	priv->link_status |= IFM_ACTIVE;
458 	CURVNET_SET_QUIET(ifp->if_vnet);
459 	if_link_state_change(ifp, LINK_STATE_UP);
460 	CURVNET_RESTORE();
461 
462 	return (0);
463 }
464 
465 /*
466  * Receive a control message
467  */
468 static int
ng_eiface_rcvmsg(node_p node,item_p item,hook_p lasthook)469 ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
470 {
471 	const priv_p priv = NG_NODE_PRIVATE(node);
472 	struct ifnet *const ifp = priv->ifp;
473 	struct ng_mesg *resp = NULL;
474 	int error = 0;
475 	struct ng_mesg *msg;
476 
477 	NGI_GET_MSG(item, msg);
478 	switch (msg->header.typecookie) {
479 	case NGM_EIFACE_COOKIE:
480 		switch (msg->header.cmd) {
481 		case NGM_EIFACE_SET:
482 		    {
483 			if (msg->header.arglen != ETHER_ADDR_LEN) {
484 				error = EINVAL;
485 				break;
486 			}
487 			error = if_setlladdr(priv->ifp,
488 			    (u_char *)msg->data, ETHER_ADDR_LEN);
489 			break;
490 		    }
491 
492 		case NGM_EIFACE_GET_IFNAME:
493 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
494 			if (resp == NULL) {
495 				error = ENOMEM;
496 				break;
497 			}
498 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
499 			break;
500 
501 		case NGM_EIFACE_GET_IFADDRS:
502 		    {
503 			struct epoch_tracker et;
504 			struct ifaddr *ifa;
505 			caddr_t ptr;
506 			int buflen;
507 
508 			/* Determine size of response and allocate it */
509 			buflen = 0;
510 			NET_EPOCH_ENTER(et);
511 			CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
512 				buflen += SA_SIZE(ifa->ifa_addr);
513 			NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
514 			if (resp == NULL) {
515 				NET_EPOCH_EXIT(et);
516 				error = ENOMEM;
517 				break;
518 			}
519 
520 			/* Add addresses */
521 			ptr = resp->data;
522 			CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
523 				const int len = SA_SIZE(ifa->ifa_addr);
524 
525 				if (buflen < len) {
526 					log(LOG_ERR, "%s: len changed?\n",
527 					    ifp->if_xname);
528 					break;
529 				}
530 				bcopy(ifa->ifa_addr, ptr, len);
531 				ptr += len;
532 				buflen -= len;
533 			}
534 			NET_EPOCH_EXIT(et);
535 			break;
536 		    }
537 
538 		default:
539 			error = EINVAL;
540 			break;
541 		} /* end of inner switch() */
542 		break;
543 	case NGM_FLOW_COOKIE:
544 		CURVNET_SET_QUIET(ifp->if_vnet);
545 		switch (msg->header.cmd) {
546 		case NGM_LINK_IS_UP:
547 			priv->link_status |= IFM_ACTIVE;
548 			if_link_state_change(ifp, LINK_STATE_UP);
549 			break;
550 		case NGM_LINK_IS_DOWN:
551 			priv->link_status &= ~IFM_ACTIVE;
552 			if_link_state_change(ifp, LINK_STATE_DOWN);
553 			break;
554 		default:
555 			break;
556 		}
557 		CURVNET_RESTORE();
558 		break;
559 	default:
560 		error = EINVAL;
561 		break;
562 	}
563 	NG_RESPOND_MSG(error, node, item, resp);
564 	NG_FREE_MSG(msg);
565 	return (error);
566 }
567 
568 /*
569  * Receive data from a hook. Pass the packet to the ether_input routine.
570  */
571 static int
ng_eiface_rcvdata(hook_p hook,item_p item)572 ng_eiface_rcvdata(hook_p hook, item_p item)
573 {
574 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
575 	struct ifnet *const ifp = priv->ifp;
576 	struct mbuf *m;
577 
578 	NGI_GET_M(item, m);
579 	NG_FREE_ITEM(item);
580 
581 	if (!((ifp->if_flags & IFF_UP) &&
582 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
583 		NG_FREE_M(m);
584 		return (ENETDOWN);
585 	}
586 
587 	if (m->m_len < ETHER_HDR_LEN) {
588 		m = m_pullup(m, ETHER_HDR_LEN);
589 		if (m == NULL)
590 			return (EINVAL);
591 	}
592 
593 	/* Note receiving interface */
594 	m->m_pkthdr.rcvif = ifp;
595 
596 	/* Update interface stats */
597 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
598 
599 	(*ifp->if_input)(ifp, m);
600 
601 	/* Done */
602 	return (0);
603 }
604 
605 /*
606  * Shutdown processing.
607  */
608 static int
ng_eiface_rmnode(node_p node)609 ng_eiface_rmnode(node_p node)
610 {
611 	const priv_p priv = NG_NODE_PRIVATE(node);
612 	struct ifnet *const ifp = priv->ifp;
613 
614 	/*
615 	 * the ifnet may be in a different vnet than the netgraph node,
616 	 * hence we have to change the current vnet context here.
617 	 */
618 	CURVNET_SET_QUIET(ifp->if_vnet);
619 	ether_ifdetach(ifp);
620 	ifmedia_removeall(&priv->media);
621 	if_free(ifp);
622 	CURVNET_RESTORE();
623 	free_unr(V_ng_eiface_unit, priv->unit);
624 	free(priv, M_NETGRAPH);
625 	NG_NODE_SET_PRIVATE(node, NULL);
626 	NG_NODE_UNREF(node);
627 	return (0);
628 }
629 
630 /*
631  * Hook disconnection
632  */
633 static int
ng_eiface_disconnect(hook_p hook)634 ng_eiface_disconnect(hook_p hook)
635 {
636 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
637 
638 	priv->ether = NULL;
639 	priv->link_status &= ~IFM_ACTIVE;
640 	CURVNET_SET_QUIET(priv->ifp->if_vnet);
641 	if_link_state_change(priv->ifp, LINK_STATE_DOWN);
642 	CURVNET_RESTORE();
643 	return (0);
644 }
645 
646 /*
647  * Handle loading and unloading for this node type.
648  */
649 static int
ng_eiface_mod_event(module_t mod,int event,void * data)650 ng_eiface_mod_event(module_t mod, int event, void *data)
651 {
652 	int error = 0;
653 
654 	switch (event) {
655 	case MOD_LOAD:
656 	case MOD_UNLOAD:
657 		break;
658 	default:
659 		error = EOPNOTSUPP;
660 		break;
661 	}
662 	return (error);
663 }
664 
665 static void
vnet_ng_eiface_init(const void * unused)666 vnet_ng_eiface_init(const void *unused)
667 {
668 
669 	V_ng_eiface_unit = new_unrhdr(0, 0xffff, NULL);
670 }
671 VNET_SYSINIT(vnet_ng_eiface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
672     vnet_ng_eiface_init, NULL);
673 
674 static void
vnet_ng_eiface_uninit(const void * unused)675 vnet_ng_eiface_uninit(const void *unused)
676 {
677 
678 	delete_unrhdr(V_ng_eiface_unit);
679 }
680 VNET_SYSUNINIT(vnet_ng_eiface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
681    vnet_ng_eiface_uninit, NULL);
682