]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_iface.c
Upgrade to OpenSSH 7.5p1.
[FreeBSD/FreeBSD.git] / sys / netgraph / ng_iface.c
1 /*
2  * ng_iface.c
3  */
4
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  * 
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  * 
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
42  */
43
44 /*
45  * This node is also a system networking interface. It has
46  * a hook for each protocol (IP, AppleTalk, etc). Packets
47  * are simply relayed between the interface and the hooks.
48  *
49  * Interfaces are named ng0, ng1, etc.  New nodes take the
50  * first available interface name.
51  *
52  * This node also includes Berkeley packet filter support.
53  */
54
55 #include "opt_inet.h"
56 #include "opt_inet6.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/errno.h>
65 #include <sys/proc.h>
66 #include <sys/random.h>
67 #include <sys/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <sys/libkern.h>
71
72 #include <net/if.h>
73 #include <net/if_var.h>
74 #include <net/if_types.h>
75 #include <net/bpf.h>
76 #include <net/netisr.h>
77 #include <net/route.h>
78 #include <net/vnet.h>
79
80 #include <netinet/in.h>
81
82 #include <netgraph/ng_message.h>
83 #include <netgraph/netgraph.h>
84 #include <netgraph/ng_parse.h>
85 #include <netgraph/ng_iface.h>
86
87 #ifdef NG_SEPARATE_MALLOC
88 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
89 #else
90 #define M_NETGRAPH_IFACE M_NETGRAPH
91 #endif
92
93 /* This struct describes one address family */
94 struct iffam {
95         sa_family_t     family;         /* Address family */
96         const char      *hookname;      /* Name for hook */
97 };
98 typedef const struct iffam *iffam_p;
99
100 /* List of address families supported by our interface */
101 const static struct iffam gFamilies[] = {
102         { AF_INET,      NG_IFACE_HOOK_INET      },
103         { AF_INET6,     NG_IFACE_HOOK_INET6     },
104         { AF_ATM,       NG_IFACE_HOOK_ATM       },
105         { AF_NATM,      NG_IFACE_HOOK_NATM      },
106 };
107 #define NUM_FAMILIES            nitems(gFamilies)
108
109 /* Node private data */
110 struct ng_iface_private {
111         struct  ifnet *ifp;             /* Our interface */
112         int     unit;                   /* Interface unit number */
113         node_p  node;                   /* Our netgraph node */
114         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
115 };
116 typedef struct ng_iface_private *priv_p;
117
118 /* Interface methods */
119 static void     ng_iface_start(struct ifnet *ifp);
120 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
121 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
122                         const struct sockaddr *dst, struct route *ro);
123 static void     ng_iface_bpftap(struct ifnet *ifp,
124                         struct mbuf *m, sa_family_t family);
125 static int      ng_iface_send(struct ifnet *ifp, struct mbuf *m,
126                         sa_family_t sa);
127 #ifdef DEBUG
128 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
129 #endif
130
131 /* Netgraph methods */
132 static int              ng_iface_mod_event(module_t, int, void *);
133 static ng_constructor_t ng_iface_constructor;
134 static ng_rcvmsg_t      ng_iface_rcvmsg;
135 static ng_shutdown_t    ng_iface_shutdown;
136 static ng_newhook_t     ng_iface_newhook;
137 static ng_rcvdata_t     ng_iface_rcvdata;
138 static ng_disconnect_t  ng_iface_disconnect;
139
140 /* Helper stuff */
141 static iffam_p  get_iffam_from_af(sa_family_t family);
142 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
143 static iffam_p  get_iffam_from_name(const char *name);
144 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
145
146 /* List of commands and how to convert arguments to/from ASCII */
147 static const struct ng_cmdlist ng_iface_cmds[] = {
148         {
149           NGM_IFACE_COOKIE,
150           NGM_IFACE_GET_IFNAME,
151           "getifname",
152           NULL,
153           &ng_parse_string_type
154         },
155         {
156           NGM_IFACE_COOKIE,
157           NGM_IFACE_POINT2POINT,
158           "point2point",
159           NULL,
160           NULL
161         },
162         {
163           NGM_IFACE_COOKIE,
164           NGM_IFACE_BROADCAST,
165           "broadcast",
166           NULL,
167           NULL
168         },
169         {
170           NGM_IFACE_COOKIE,
171           NGM_IFACE_GET_IFINDEX,
172           "getifindex",
173           NULL,
174           &ng_parse_uint32_type
175         },
176         { 0 }
177 };
178
179 /* Node type descriptor */
180 static struct ng_type typestruct = {
181         .version =      NG_ABI_VERSION,
182         .name =         NG_IFACE_NODE_TYPE,
183         .mod_event =    ng_iface_mod_event,
184         .constructor =  ng_iface_constructor,
185         .rcvmsg =       ng_iface_rcvmsg,
186         .shutdown =     ng_iface_shutdown,
187         .newhook =      ng_iface_newhook,
188         .rcvdata =      ng_iface_rcvdata,
189         .disconnect =   ng_iface_disconnect,
190         .cmdlist =      ng_iface_cmds,
191 };
192 NETGRAPH_INIT(iface, &typestruct);
193
194 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
195 #define V_ng_iface_unit                 VNET(ng_iface_unit)
196
197 /************************************************************************
198                         HELPER STUFF
199  ************************************************************************/
200
201 /*
202  * Get the family descriptor from the family ID
203  */
204 static __inline iffam_p
205 get_iffam_from_af(sa_family_t family)
206 {
207         iffam_p iffam;
208         int k;
209
210         for (k = 0; k < NUM_FAMILIES; k++) {
211                 iffam = &gFamilies[k];
212                 if (iffam->family == family)
213                         return (iffam);
214         }
215         return (NULL);
216 }
217
218 /*
219  * Get the family descriptor from the hook
220  */
221 static __inline iffam_p
222 get_iffam_from_hook(priv_p priv, hook_p hook)
223 {
224         int k;
225
226         for (k = 0; k < NUM_FAMILIES; k++)
227                 if (priv->hooks[k] == hook)
228                         return (&gFamilies[k]);
229         return (NULL);
230 }
231
232 /*
233  * Get the hook from the iffam descriptor
234  */
235
236 static __inline hook_p *
237 get_hook_from_iffam(priv_p priv, iffam_p iffam)
238 {
239         return (&priv->hooks[iffam - gFamilies]);
240 }
241
242 /*
243  * Get the iffam descriptor from the name
244  */
245 static __inline iffam_p
246 get_iffam_from_name(const char *name)
247 {
248         iffam_p iffam;
249         int k;
250
251         for (k = 0; k < NUM_FAMILIES; k++) {
252                 iffam = &gFamilies[k];
253                 if (!strcmp(iffam->hookname, name))
254                         return (iffam);
255         }
256         return (NULL);
257 }
258
259 /************************************************************************
260                         INTERFACE STUFF
261  ************************************************************************/
262
263 /*
264  * Process an ioctl for the virtual interface
265  */
266 static int
267 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
268 {
269         struct ifreq *const ifr = (struct ifreq *) data;
270         int error = 0;
271
272 #ifdef DEBUG
273         ng_iface_print_ioctl(ifp, command, data);
274 #endif
275         switch (command) {
276
277         /* These two are mostly handled at a higher layer */
278         case SIOCSIFADDR:
279                 ifp->if_flags |= IFF_UP;
280                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
281                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
282                 break;
283         case SIOCGIFADDR:
284                 break;
285
286         /* Set flags */
287         case SIOCSIFFLAGS:
288                 /*
289                  * If the interface is marked up and stopped, then start it.
290                  * If it is marked down and running, then stop it.
291                  */
292                 if (ifr->ifr_flags & IFF_UP) {
293                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
294                                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
295                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
296                         }
297                 } else {
298                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
299                                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
300                                     IFF_DRV_OACTIVE);
301                 }
302                 break;
303
304         /* Set the interface MTU */
305         case SIOCSIFMTU:
306                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
307                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
308                         error = EINVAL;
309                 else
310                         ifp->if_mtu = ifr->ifr_mtu;
311                 break;
312
313         /* Stuff that's not supported */
314         case SIOCADDMULTI:
315         case SIOCDELMULTI:
316                 error = 0;
317                 break;
318         case SIOCSIFPHYS:
319                 error = EOPNOTSUPP;
320                 break;
321
322         default:
323                 error = EINVAL;
324                 break;
325         }
326         return (error);
327 }
328
329 /*
330  * This routine is called to deliver a packet out the interface.
331  * We simply look at the address family and relay the packet to
332  * the corresponding hook, if it exists and is connected.
333  */
334
335 static int
336 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
337         const struct sockaddr *dst, struct route *ro)
338 {
339         struct m_tag *mtag;
340         uint32_t af;
341         int error;
342
343         /* Check interface flags */
344         if (!((ifp->if_flags & IFF_UP) &&
345             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
346                 m_freem(m);
347                 return (ENETDOWN);
348         }
349
350         /* Protect from deadly infinite recursion. */
351         mtag = NULL;
352         while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
353                 if (*(struct ifnet **)(mtag + 1) == ifp) {
354                         log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
355                         m_freem(m);
356                         return (EDEADLK);
357                 }
358         }
359         mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
360             M_NOWAIT);
361         if (mtag == NULL) {
362                 m_freem(m);
363                 return (ENOMEM);
364         }
365         *(struct ifnet **)(mtag + 1) = ifp;
366         m_tag_prepend(m, mtag);
367
368         /* BPF writes need to be handled specially. */
369         if (dst->sa_family == AF_UNSPEC)
370                 bcopy(dst->sa_data, &af, sizeof(af));
371         else
372                 af = dst->sa_family;
373
374         /* Berkeley packet filter */
375         ng_iface_bpftap(ifp, m, af);
376
377         if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
378                 M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
379                 if (m == NULL) {
380                         if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
381                         return (ENOBUFS);
382                 }
383                 *(sa_family_t *)m->m_data = af;
384                 error = (ifp->if_transmit)(ifp, m);
385         } else
386                 error = ng_iface_send(ifp, m, af);
387
388         return (error);
389 }
390
391 /*
392  * Start method is used only when ALTQ is enabled.
393  */
394 static void
395 ng_iface_start(struct ifnet *ifp)
396 {
397         struct mbuf *m;
398         sa_family_t sa;
399
400         KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
401
402         for(;;) {
403                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
404                 if (m == NULL)
405                         break;
406                 sa = *mtod(m, sa_family_t *);
407                 m_adj(m, sizeof(sa_family_t));
408                 ng_iface_send(ifp, m, sa);
409         }
410 }
411
412 /*
413  * Flash a packet by the BPF (requires prepending 4 byte AF header)
414  * Note the phoney mbuf; this is OK because BPF treats it read-only.
415  */
416 static void
417 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
418 {
419         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
420         if (bpf_peers_present(ifp->if_bpf)) {
421                 int32_t family4 = (int32_t)family;
422                 bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
423         }
424 }
425
426 /*
427  * This routine does actual delivery of the packet into the
428  * netgraph(4). It is called from ng_iface_start() and
429  * ng_iface_output().
430  */
431 static int
432 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
433 {
434         const priv_p priv = (priv_p) ifp->if_softc;
435         const iffam_p iffam = get_iffam_from_af(sa);
436         int error;
437         int len;
438
439         /* Check address family to determine hook (if known) */
440         if (iffam == NULL) {
441                 m_freem(m);
442                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
443                 return (EAFNOSUPPORT);
444         }
445
446         /* Copy length before the mbuf gets invalidated. */
447         len = m->m_pkthdr.len;
448
449         /* Send packet. If hook is not connected, mbuf will get freed. */
450         NG_OUTBOUND_THREAD_REF();
451         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
452         NG_OUTBOUND_THREAD_UNREF();
453
454         /* Update stats. */
455         if (error == 0) {
456                 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
457                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
458         }
459
460         return (error);
461 }
462
463 #ifdef DEBUG
464 /*
465  * Display an ioctl to the virtual interface
466  */
467
468 static void
469 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
470 {
471         char   *str;
472
473         switch (command & IOC_DIRMASK) {
474         case IOC_VOID:
475                 str = "IO";
476                 break;
477         case IOC_OUT:
478                 str = "IOR";
479                 break;
480         case IOC_IN:
481                 str = "IOW";
482                 break;
483         case IOC_INOUT:
484                 str = "IORW";
485                 break;
486         default:
487                 str = "IO??";
488         }
489         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
490                ifp->if_xname,
491                str,
492                IOCGROUP(command),
493                command & 0xff,
494                IOCPARM_LEN(command));
495 }
496 #endif /* DEBUG */
497
498 /************************************************************************
499                         NETGRAPH NODE STUFF
500  ************************************************************************/
501
502 /*
503  * Constructor for a node
504  */
505 static int
506 ng_iface_constructor(node_p node)
507 {
508         struct ifnet *ifp;
509         priv_p priv;
510
511         /* Allocate node and interface private structures */
512         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
513         ifp = if_alloc(IFT_PROPVIRTUAL);
514         if (ifp == NULL) {
515                 free(priv, M_NETGRAPH_IFACE);
516                 return (ENOMEM);
517         }
518
519         /* Link them together */
520         ifp->if_softc = priv;
521         priv->ifp = ifp;
522
523         /* Get an interface unit number */
524         priv->unit = alloc_unr(V_ng_iface_unit);
525
526         /* Link together node and private info */
527         NG_NODE_SET_PRIVATE(node, priv);
528         priv->node = node;
529
530         /* Initialize interface structure */
531         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
532         ifp->if_output = ng_iface_output;
533         ifp->if_start = ng_iface_start;
534         ifp->if_ioctl = ng_iface_ioctl;
535         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
536         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
537         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
538         ifp->if_addrlen = 0;                    /* XXX */
539         ifp->if_hdrlen = 0;                     /* XXX */
540         ifp->if_baudrate = 64000;               /* XXX */
541         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
542         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
543         IFQ_SET_READY(&ifp->if_snd);
544
545         /* Give this node the same name as the interface (if possible) */
546         if (ng_name_node(node, ifp->if_xname) != 0)
547                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
548                     ifp->if_xname);
549
550         /* Attach the interface */
551         if_attach(ifp);
552         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
553
554         /* Done */
555         return (0);
556 }
557
558 /*
559  * Give our ok for a hook to be added
560  */
561 static int
562 ng_iface_newhook(node_p node, hook_p hook, const char *name)
563 {
564         const iffam_p iffam = get_iffam_from_name(name);
565         hook_p *hookptr;
566
567         if (iffam == NULL)
568                 return (EPFNOSUPPORT);
569         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
570         if (*hookptr != NULL)
571                 return (EISCONN);
572         *hookptr = hook;
573         NG_HOOK_HI_STACK(hook);
574         NG_HOOK_SET_TO_INBOUND(hook);
575         return (0);
576 }
577
578 /*
579  * Receive a control message
580  */
581 static int
582 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
583 {
584         const priv_p priv = NG_NODE_PRIVATE(node);
585         struct ifnet *const ifp = priv->ifp;
586         struct ng_mesg *resp = NULL;
587         int error = 0;
588         struct ng_mesg *msg;
589
590         NGI_GET_MSG(item, msg);
591         switch (msg->header.typecookie) {
592         case NGM_IFACE_COOKIE:
593                 switch (msg->header.cmd) {
594                 case NGM_IFACE_GET_IFNAME:
595                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
596                         if (resp == NULL) {
597                                 error = ENOMEM;
598                                 break;
599                         }
600                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
601                         break;
602
603                 case NGM_IFACE_POINT2POINT:
604                 case NGM_IFACE_BROADCAST:
605                     {
606
607                         /* Deny request if interface is UP */
608                         if ((ifp->if_flags & IFF_UP) != 0)
609                                 return (EBUSY);
610
611                         /* Change flags */
612                         switch (msg->header.cmd) {
613                         case NGM_IFACE_POINT2POINT:
614                                 ifp->if_flags |= IFF_POINTOPOINT;
615                                 ifp->if_flags &= ~IFF_BROADCAST;
616                                 break;
617                         case NGM_IFACE_BROADCAST:
618                                 ifp->if_flags &= ~IFF_POINTOPOINT;
619                                 ifp->if_flags |= IFF_BROADCAST;
620                                 break;
621                         }
622                         break;
623                     }
624
625                 case NGM_IFACE_GET_IFINDEX:
626                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
627                         if (resp == NULL) {
628                                 error = ENOMEM;
629                                 break;
630                         }
631                         *((uint32_t *)resp->data) = priv->ifp->if_index;
632                         break;
633
634                 default:
635                         error = EINVAL;
636                         break;
637                 }
638                 break;
639         case NGM_FLOW_COOKIE:
640                 switch (msg->header.cmd) {
641                 case NGM_LINK_IS_UP:
642                         if_link_state_change(ifp, LINK_STATE_UP);
643                         break;
644                 case NGM_LINK_IS_DOWN:
645                         if_link_state_change(ifp, LINK_STATE_DOWN);
646                         break;
647                 default:
648                         break;
649                 }
650                 break;
651         default:
652                 error = EINVAL;
653                 break;
654         }
655         NG_RESPOND_MSG(error, node, item, resp);
656         NG_FREE_MSG(msg);
657         return (error);
658 }
659
660 /*
661  * Recive data from a hook. Pass the packet to the correct input routine.
662  */
663 static int
664 ng_iface_rcvdata(hook_p hook, item_p item)
665 {
666         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
667         const iffam_p iffam = get_iffam_from_hook(priv, hook);
668         struct ifnet *const ifp = priv->ifp;
669         struct mbuf *m;
670         int isr;
671
672         NGI_GET_M(item, m);
673         NG_FREE_ITEM(item);
674         /* Sanity checks */
675         KASSERT(iffam != NULL, ("%s: iffam", __func__));
676         M_ASSERTPKTHDR(m);
677         if ((ifp->if_flags & IFF_UP) == 0) {
678                 NG_FREE_M(m);
679                 return (ENETDOWN);
680         }
681
682         /* Update interface stats */
683         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
684         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
685
686         /* Note receiving interface */
687         m->m_pkthdr.rcvif = ifp;
688
689         /* Berkeley packet filter */
690         ng_iface_bpftap(ifp, m, iffam->family);
691
692         /* Send packet */
693         switch (iffam->family) {
694 #ifdef INET
695         case AF_INET:
696                 isr = NETISR_IP;
697                 break;
698 #endif
699 #ifdef INET6
700         case AF_INET6:
701                 isr = NETISR_IPV6;
702                 break;
703 #endif
704         default:
705                 m_freem(m);
706                 return (EAFNOSUPPORT);
707         }
708         random_harvest_queue(m, sizeof(*m), 2, RANDOM_NET_NG);
709         M_SETFIB(m, ifp->if_fib);
710         netisr_dispatch(isr, m);
711         return (0);
712 }
713
714 /*
715  * Shutdown and remove the node and its associated interface.
716  */
717 static int
718 ng_iface_shutdown(node_p node)
719 {
720         const priv_p priv = NG_NODE_PRIVATE(node);
721
722         /*
723          * The ifnet may be in a different vnet than the netgraph node, 
724          * hence we have to change the current vnet context here.
725          */
726         CURVNET_SET_QUIET(priv->ifp->if_vnet);
727         bpfdetach(priv->ifp);
728         if_detach(priv->ifp);
729         if_free(priv->ifp);
730         CURVNET_RESTORE();
731         priv->ifp = NULL;
732         free_unr(V_ng_iface_unit, priv->unit);
733         free(priv, M_NETGRAPH_IFACE);
734         NG_NODE_SET_PRIVATE(node, NULL);
735         NG_NODE_UNREF(node);
736         return (0);
737 }
738
739 /*
740  * Hook disconnection. Note that we do *not* shutdown when all
741  * hooks have been disconnected.
742  */
743 static int
744 ng_iface_disconnect(hook_p hook)
745 {
746         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
747         const iffam_p iffam = get_iffam_from_hook(priv, hook);
748
749         if (iffam == NULL)
750                 panic("%s", __func__);
751         *get_hook_from_iffam(priv, iffam) = NULL;
752         return (0);
753 }
754
755 /*
756  * Handle loading and unloading for this node type.
757  */
758 static int
759 ng_iface_mod_event(module_t mod, int event, void *data)
760 {
761         int error = 0;
762
763         switch (event) {
764         case MOD_LOAD:
765         case MOD_UNLOAD:
766                 break;
767         default:
768                 error = EOPNOTSUPP;
769                 break;
770         }
771         return (error);
772 }
773
774 static void
775 vnet_ng_iface_init(const void *unused)
776 {
777
778         V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
779 }
780 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
781     vnet_ng_iface_init, NULL);
782
783 static void
784 vnet_ng_iface_uninit(const void *unused)
785 {
786
787         delete_unrhdr(V_ng_iface_unit);
788 }
789 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
790     vnet_ng_iface_uninit, NULL);