]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netgraph/ng_iface.c
libc/libc/rpc: refactor some global variables
[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  * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
40  */
41
42 /*
43  * This node is also a system networking interface. It has
44  * a hook for each protocol (IP, AppleTalk, etc). Packets
45  * are simply relayed between the interface and the hooks.
46  *
47  * Interfaces are named ng0, ng1, etc.  New nodes take the
48  * first available interface name.
49  *
50  * This node also includes Berkeley packet filter support.
51  */
52
53 #include "opt_inet.h"
54 #include "opt_inet6.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/errno.h>
59 #include <sys/kernel.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/errno.h>
64 #include <sys/proc.h>
65 #include <sys/random.h>
66 #include <sys/rmlock.h>
67 #include <sys/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
72
73 #include <net/if.h>
74 #include <net/if_var.h>
75 #include <net/if_types.h>
76 #include <net/bpf.h>
77 #include <net/netisr.h>
78 #include <net/route.h>
79 #include <net/vnet.h>
80
81 #include <netinet/in.h>
82
83 #include <netgraph/ng_message.h>
84 #include <netgraph/netgraph.h>
85 #include <netgraph/ng_parse.h>
86 #include <netgraph/ng_iface.h>
87
88 #ifdef NG_SEPARATE_MALLOC
89 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
90 #else
91 #define M_NETGRAPH_IFACE M_NETGRAPH
92 #endif
93
94 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
95     "Point to point netgraph interface");
96 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
97 #define V_ng_iface_max_nest     VNET(ng_iface_max_nest)
98 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
99     &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
100
101 /* This struct describes one address family */
102 struct iffam {
103         sa_family_t     family;         /* Address family */
104         const char      *hookname;      /* Name for hook */
105 };
106 typedef const struct iffam *iffam_p;
107
108 /* List of address families supported by our interface */
109 const static struct iffam gFamilies[] = {
110         { AF_INET,      NG_IFACE_HOOK_INET      },
111         { AF_INET6,     NG_IFACE_HOOK_INET6     },
112 };
113 #define NUM_FAMILIES            nitems(gFamilies)
114
115 /* Node private data */
116 struct ng_iface_private {
117         struct  ifnet *ifp;             /* Our interface */
118         int     unit;                   /* Interface unit number */
119         node_p  node;                   /* Our netgraph node */
120         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
121         struct rmlock   lock;           /* Protect private data changes */
122 };
123 typedef struct ng_iface_private *priv_p;
124
125 #define PRIV_RLOCK(priv, t)     rm_rlock(&priv->lock, t)
126 #define PRIV_RUNLOCK(priv, t)   rm_runlock(&priv->lock, t)
127 #define PRIV_WLOCK(priv)        rm_wlock(&priv->lock)
128 #define PRIV_WUNLOCK(priv)      rm_wunlock(&priv->lock)
129
130 /* Interface methods */
131 static void     ng_iface_start(struct ifnet *ifp);
132 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
133 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
134                         const struct sockaddr *dst, struct route *ro);
135 static void     ng_iface_bpftap(struct ifnet *ifp,
136                         struct mbuf *m, sa_family_t family);
137 static int      ng_iface_send(struct ifnet *ifp, struct mbuf *m,
138                         sa_family_t sa);
139 #ifdef DEBUG
140 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
141 #endif
142
143 /* Netgraph methods */
144 static int              ng_iface_mod_event(module_t, int, void *);
145 static ng_constructor_t ng_iface_constructor;
146 static ng_rcvmsg_t      ng_iface_rcvmsg;
147 static ng_shutdown_t    ng_iface_shutdown;
148 static ng_newhook_t     ng_iface_newhook;
149 static ng_rcvdata_t     ng_iface_rcvdata;
150 static ng_disconnect_t  ng_iface_disconnect;
151
152 /* Helper stuff */
153 static iffam_p  get_iffam_from_af(sa_family_t family);
154 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
155 static iffam_p  get_iffam_from_name(const char *name);
156 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
157
158 /* List of commands and how to convert arguments to/from ASCII */
159 static const struct ng_cmdlist ng_iface_cmds[] = {
160         {
161           NGM_IFACE_COOKIE,
162           NGM_IFACE_GET_IFNAME,
163           "getifname",
164           NULL,
165           &ng_parse_string_type
166         },
167         {
168           NGM_IFACE_COOKIE,
169           NGM_IFACE_POINT2POINT,
170           "point2point",
171           NULL,
172           NULL
173         },
174         {
175           NGM_IFACE_COOKIE,
176           NGM_IFACE_BROADCAST,
177           "broadcast",
178           NULL,
179           NULL
180         },
181         {
182           NGM_IFACE_COOKIE,
183           NGM_IFACE_GET_IFINDEX,
184           "getifindex",
185           NULL,
186           &ng_parse_uint32_type
187         },
188         { 0 }
189 };
190
191 /* Node type descriptor */
192 static struct ng_type typestruct = {
193         .version =      NG_ABI_VERSION,
194         .name =         NG_IFACE_NODE_TYPE,
195         .mod_event =    ng_iface_mod_event,
196         .constructor =  ng_iface_constructor,
197         .rcvmsg =       ng_iface_rcvmsg,
198         .shutdown =     ng_iface_shutdown,
199         .newhook =      ng_iface_newhook,
200         .rcvdata =      ng_iface_rcvdata,
201         .disconnect =   ng_iface_disconnect,
202         .cmdlist =      ng_iface_cmds,
203 };
204 NETGRAPH_INIT(iface, &typestruct);
205
206 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
207 #define V_ng_iface_unit                 VNET(ng_iface_unit)
208
209 /************************************************************************
210                         HELPER STUFF
211  ************************************************************************/
212
213 /*
214  * Get the family descriptor from the family ID
215  */
216 static __inline iffam_p
217 get_iffam_from_af(sa_family_t family)
218 {
219         iffam_p iffam;
220         int k;
221
222         for (k = 0; k < NUM_FAMILIES; k++) {
223                 iffam = &gFamilies[k];
224                 if (iffam->family == family)
225                         return (iffam);
226         }
227         return (NULL);
228 }
229
230 /*
231  * Get the family descriptor from the hook
232  */
233 static __inline iffam_p
234 get_iffam_from_hook(priv_p priv, hook_p hook)
235 {
236         int k;
237
238         for (k = 0; k < NUM_FAMILIES; k++)
239                 if (priv->hooks[k] == hook)
240                         return (&gFamilies[k]);
241         return (NULL);
242 }
243
244 /*
245  * Get the hook from the iffam descriptor
246  */
247
248 static __inline hook_p *
249 get_hook_from_iffam(priv_p priv, iffam_p iffam)
250 {
251         return (&priv->hooks[iffam - gFamilies]);
252 }
253
254 /*
255  * Get the iffam descriptor from the name
256  */
257 static __inline iffam_p
258 get_iffam_from_name(const char *name)
259 {
260         iffam_p iffam;
261         int k;
262
263         for (k = 0; k < NUM_FAMILIES; k++) {
264                 iffam = &gFamilies[k];
265                 if (!strcmp(iffam->hookname, name))
266                         return (iffam);
267         }
268         return (NULL);
269 }
270
271 /************************************************************************
272                         INTERFACE STUFF
273  ************************************************************************/
274
275 /*
276  * Process an ioctl for the virtual interface
277  */
278 static int
279 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
280 {
281         struct ifreq *const ifr = (struct ifreq *) data;
282         int error = 0;
283
284 #ifdef DEBUG
285         ng_iface_print_ioctl(ifp, command, data);
286 #endif
287         switch (command) {
288         /* These two are mostly handled at a higher layer */
289         case SIOCSIFADDR:
290                 ifp->if_flags |= IFF_UP;
291                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
292                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
293                 break;
294         case SIOCGIFADDR:
295                 break;
296
297         /* Set flags */
298         case SIOCSIFFLAGS:
299                 /*
300                  * If the interface is marked up and stopped, then start it.
301                  * If it is marked down and running, then stop it.
302                  */
303                 if (ifr->ifr_flags & IFF_UP) {
304                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
305                                 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
306                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
307                         }
308                 } else {
309                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
310                                 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
311                                     IFF_DRV_OACTIVE);
312                 }
313                 break;
314
315         /* Set the interface MTU */
316         case SIOCSIFMTU:
317                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
318                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
319                         error = EINVAL;
320                 else
321                         ifp->if_mtu = ifr->ifr_mtu;
322                 break;
323
324         /* Stuff that's not supported */
325         case SIOCADDMULTI:
326         case SIOCDELMULTI:
327                 error = 0;
328                 break;
329         case SIOCSIFPHYS:
330                 error = EOPNOTSUPP;
331                 break;
332
333         default:
334                 error = EINVAL;
335                 break;
336         }
337         return (error);
338 }
339
340 /*
341  * This routine is called to deliver a packet out the interface.
342  * We simply look at the address family and relay the packet to
343  * the corresponding hook, if it exists and is connected.
344  */
345
346 static int
347 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
348         const struct sockaddr *dst, struct route *ro)
349 {
350         uint32_t af;
351         int error;
352
353         /* Check interface flags */
354         if (!((ifp->if_flags & IFF_UP) &&
355             (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
356                 m_freem(m);
357                 return (ENETDOWN);
358         }
359
360         /* Protect from deadly infinite recursion. */
361         error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
362             V_ng_iface_max_nest);
363         if (error) {
364                 m_freem(m);
365                 return (error);
366         }
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 = RO_GET_FAMILY(ro, dst);
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         struct rm_priotracker priv_tracker;
435         const priv_p priv = (priv_p) ifp->if_softc;
436         const iffam_p iffam = get_iffam_from_af(sa);
437         hook_p hook;
438         int error;
439         int len;
440
441         /* Check address family to determine hook (if known) */
442         if (iffam == NULL) {
443                 m_freem(m);
444                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
445                 return (EAFNOSUPPORT);
446         }
447
448         /* Copy length before the mbuf gets invalidated. */
449         len = m->m_pkthdr.len;
450
451         PRIV_RLOCK(priv, &priv_tracker);
452         hook = *get_hook_from_iffam(priv, iffam);
453         if (hook == NULL) {
454                 NG_FREE_M(m);
455                 PRIV_RUNLOCK(priv, &priv_tracker);
456                 return ENETDOWN;
457         }
458         NG_HOOK_REF(hook);
459         PRIV_RUNLOCK(priv, &priv_tracker);
460
461         NG_OUTBOUND_THREAD_REF();
462         NG_SEND_DATA_ONLY(error, hook, m);
463         NG_OUTBOUND_THREAD_UNREF();
464         NG_HOOK_UNREF(hook);
465
466         /* Update stats. */
467         if (error == 0) {
468                 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
469                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
470         }
471
472         return (error);
473 }
474
475 #ifdef DEBUG
476 /*
477  * Display an ioctl to the virtual interface
478  */
479
480 static void
481 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
482 {
483         char   *str;
484
485         switch (command & IOC_DIRMASK) {
486         case IOC_VOID:
487                 str = "IO";
488                 break;
489         case IOC_OUT:
490                 str = "IOR";
491                 break;
492         case IOC_IN:
493                 str = "IOW";
494                 break;
495         case IOC_INOUT:
496                 str = "IORW";
497                 break;
498         default:
499                 str = "IO??";
500         }
501         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
502                ifp->if_xname,
503                str,
504                IOCGROUP(command),
505                command & 0xff,
506                IOCPARM_LEN(command));
507 }
508 #endif /* DEBUG */
509
510 /************************************************************************
511                         NETGRAPH NODE STUFF
512  ************************************************************************/
513
514 /*
515  * Constructor for a node
516  */
517 static int
518 ng_iface_constructor(node_p node)
519 {
520         struct ifnet *ifp;
521         priv_p priv;
522
523         /* Allocate node and interface private structures */
524         priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
525         ifp = if_alloc(IFT_PROPVIRTUAL);
526         if (ifp == NULL) {
527                 free(priv, M_NETGRAPH_IFACE);
528                 return (ENOMEM);
529         }
530
531         rm_init(&priv->lock, "ng_iface private rmlock");
532
533         /* Link them together */
534         ifp->if_softc = priv;
535         priv->ifp = ifp;
536
537         /* Get an interface unit number */
538         priv->unit = alloc_unr(V_ng_iface_unit);
539
540         /* Link together node and private info */
541         NG_NODE_SET_PRIVATE(node, priv);
542         priv->node = node;
543
544         /* Initialize interface structure */
545         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
546         ifp->if_output = ng_iface_output;
547         ifp->if_start = ng_iface_start;
548         ifp->if_ioctl = ng_iface_ioctl;
549         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
550         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
551         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
552         ifp->if_addrlen = 0;                    /* XXX */
553         ifp->if_hdrlen = 0;                     /* XXX */
554         ifp->if_baudrate = 64000;               /* XXX */
555         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
556         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
557         IFQ_SET_READY(&ifp->if_snd);
558
559         /* Give this node the same name as the interface (if possible) */
560         if (ng_name_node(node, ifp->if_xname) != 0)
561                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
562                     ifp->if_xname);
563
564         /* Attach the interface */
565         if_attach(ifp);
566         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
567
568         /* Done */
569         return (0);
570 }
571
572 /*
573  * Give our ok for a hook to be added
574  */
575 static int
576 ng_iface_newhook(node_p node, hook_p hook, const char *name)
577 {
578         const iffam_p iffam = get_iffam_from_name(name);
579         const priv_p priv = NG_NODE_PRIVATE(node);
580         hook_p *hookptr;
581
582         if (iffam == NULL)
583                 return (EPFNOSUPPORT);
584         PRIV_WLOCK(priv);
585         hookptr = get_hook_from_iffam(priv, iffam);
586         if (*hookptr != NULL) {
587                 PRIV_WUNLOCK(priv);
588                 return (EISCONN);
589         }
590         *hookptr = hook;
591         NG_HOOK_HI_STACK(hook);
592         NG_HOOK_SET_TO_INBOUND(hook);
593         PRIV_WUNLOCK(priv);
594         return (0);
595 }
596
597 /*
598  * Receive a control message
599  */
600 static int
601 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
602 {
603         const priv_p priv = NG_NODE_PRIVATE(node);
604         struct ifnet *const ifp = priv->ifp;
605         struct ng_mesg *resp = NULL;
606         int error = 0;
607         struct ng_mesg *msg;
608
609         NGI_GET_MSG(item, msg);
610         switch (msg->header.typecookie) {
611         case NGM_IFACE_COOKIE:
612                 switch (msg->header.cmd) {
613                 case NGM_IFACE_GET_IFNAME:
614                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
615                         if (resp == NULL) {
616                                 error = ENOMEM;
617                                 break;
618                         }
619                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
620                         break;
621
622                 case NGM_IFACE_POINT2POINT:
623                 case NGM_IFACE_BROADCAST:
624                     {
625                         /* Deny request if interface is UP */
626                         if ((ifp->if_flags & IFF_UP) != 0)
627                                 return (EBUSY);
628
629                         /* Change flags */
630                         switch (msg->header.cmd) {
631                         case NGM_IFACE_POINT2POINT:
632                                 ifp->if_flags |= IFF_POINTOPOINT;
633                                 ifp->if_flags &= ~IFF_BROADCAST;
634                                 break;
635                         case NGM_IFACE_BROADCAST:
636                                 ifp->if_flags &= ~IFF_POINTOPOINT;
637                                 ifp->if_flags |= IFF_BROADCAST;
638                                 break;
639                         }
640                         break;
641                     }
642
643                 case NGM_IFACE_GET_IFINDEX:
644                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
645                         if (resp == NULL) {
646                                 error = ENOMEM;
647                                 break;
648                         }
649                         *((uint32_t *)resp->data) = priv->ifp->if_index;
650                         break;
651
652                 default:
653                         error = EINVAL;
654                         break;
655                 }
656                 break;
657         case NGM_FLOW_COOKIE:
658                 switch (msg->header.cmd) {
659                 case NGM_LINK_IS_UP:
660                         if_link_state_change(ifp, LINK_STATE_UP);
661                         break;
662                 case NGM_LINK_IS_DOWN:
663                         if_link_state_change(ifp, LINK_STATE_DOWN);
664                         break;
665                 default:
666                         break;
667                 }
668                 break;
669         default:
670                 error = EINVAL;
671                 break;
672         }
673         NG_RESPOND_MSG(error, node, item, resp);
674         NG_FREE_MSG(msg);
675         return (error);
676 }
677
678 /*
679  * Recive data from a hook. Pass the packet to the correct input routine.
680  */
681 static int
682 ng_iface_rcvdata(hook_p hook, item_p item)
683 {
684         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
685         const iffam_p iffam = get_iffam_from_hook(priv, hook);
686         struct ifnet *const ifp = priv->ifp;
687         struct epoch_tracker et;
688         struct mbuf *m;
689         int isr;
690
691         NGI_GET_M(item, m);
692         NG_FREE_ITEM(item);
693         /* Sanity checks */
694         KASSERT(iffam != NULL, ("%s: iffam", __func__));
695         M_ASSERTPKTHDR(m);
696         if ((ifp->if_flags & IFF_UP) == 0) {
697                 NG_FREE_M(m);
698                 return (ENETDOWN);
699         }
700
701         /* Update interface stats */
702         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
703         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
704
705         /* Note receiving interface */
706         m->m_pkthdr.rcvif = ifp;
707
708         /* Berkeley packet filter */
709         ng_iface_bpftap(ifp, m, iffam->family);
710
711         /* Send packet */
712         switch (iffam->family) {
713 #ifdef INET
714         case AF_INET:
715                 isr = NETISR_IP;
716                 break;
717 #endif
718 #ifdef INET6
719         case AF_INET6:
720                 isr = NETISR_IPV6;
721                 break;
722 #endif
723         default:
724                 m_freem(m);
725                 return (EAFNOSUPPORT);
726         }
727         random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
728         M_SETFIB(m, ifp->if_fib);
729         CURVNET_SET(ifp->if_vnet);
730         NET_EPOCH_ENTER(et);
731         netisr_dispatch(isr, m);
732         NET_EPOCH_EXIT(et);
733         CURVNET_RESTORE();
734         return (0);
735 }
736
737 /*
738  * Shutdown and remove the node and its associated interface.
739  */
740 static int
741 ng_iface_shutdown(node_p node)
742 {
743         const priv_p priv = NG_NODE_PRIVATE(node);
744
745         /*
746          * The ifnet may be in a different vnet than the netgraph node, 
747          * hence we have to change the current vnet context here.
748          */
749         CURVNET_SET_QUIET(priv->ifp->if_vnet);
750         bpfdetach(priv->ifp);
751         if_detach(priv->ifp);
752         if_free(priv->ifp);
753         CURVNET_RESTORE();
754         priv->ifp = NULL;
755         free_unr(V_ng_iface_unit, priv->unit);
756         rm_destroy(&priv->lock);
757         free(priv, M_NETGRAPH_IFACE);
758         NG_NODE_SET_PRIVATE(node, NULL);
759         NG_NODE_UNREF(node);
760         return (0);
761 }
762
763 /*
764  * Hook disconnection. Note that we do *not* shutdown when all
765  * hooks have been disconnected.
766  */
767 static int
768 ng_iface_disconnect(hook_p hook)
769 {
770         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
771         const iffam_p iffam = get_iffam_from_hook(priv, hook);
772
773         if (iffam == NULL)
774                 panic("%s", __func__);
775         PRIV_WLOCK(priv);
776         *get_hook_from_iffam(priv, iffam) = NULL;
777         PRIV_WUNLOCK(priv);
778         return (0);
779 }
780
781 /*
782  * Handle loading and unloading for this node type.
783  */
784 static int
785 ng_iface_mod_event(module_t mod, int event, void *data)
786 {
787         int error = 0;
788
789         switch (event) {
790         case MOD_LOAD:
791         case MOD_UNLOAD:
792                 break;
793         default:
794                 error = EOPNOTSUPP;
795                 break;
796         }
797         return (error);
798 }
799
800 static void
801 vnet_ng_iface_init(const void *unused)
802 {
803
804         V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
805 }
806 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
807     vnet_ng_iface_init, NULL);
808
809 static void
810 vnet_ng_iface_uninit(const void *unused)
811 {
812
813         delete_unrhdr(V_ng_iface_unit);
814 }
815 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
816     vnet_ng_iface_uninit, NULL);