Remove support for the IPX and NCP protocols, and for NWFS.
[dragonfly.git] / sys / netgraph7 / iface / 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: src/sys/netgraph/ng_iface.c,v 1.48 2008/01/31 08:51:48 mav Exp $
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/random.h>
65 #include <sys/sockio.h>
66 #include <sys/socket.h>
67 #include <sys/syslog.h>
68 #include <sys/libkern.h>
69
70 #include <net/if.h>
71 #include <net/if_types.h>
72 #include <net/ifq_var.h>
73 #include <net/bpf.h>
74 #include <net/netisr.h>
75
76 #include <netinet/in.h>
77
78 #include <netgraph7/ng_message.h>
79 #include <netgraph7/netgraph.h>
80 #include <netgraph7/ng_parse.h>
81 #include <netgraph7/cisco/ng_cisco.h>
82 #include "ng_iface.h"
83
84 #ifdef NG_SEPARATE_MALLOC
85 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
86 #else
87 #define M_NETGRAPH_IFACE M_NETGRAPH
88 #endif
89
90 /* This struct describes one address family */
91 struct iffam {
92         sa_family_t     family;         /* Address family */
93         const char      *hookname;      /* Name for hook */
94 };
95 typedef const struct iffam *iffam_p;
96
97 /* List of address families supported by our interface */
98 static const struct iffam gFamilies[] = {
99         { AF_INET,      NG_IFACE_HOOK_INET      },
100         { AF_INET6,     NG_IFACE_HOOK_INET6     },
101         { AF_ATM,       NG_IFACE_HOOK_ATM       },
102         { AF_NATM,      NG_IFACE_HOOK_NATM      },
103 };
104 #define NUM_FAMILIES            NELEM(gFamilies)
105
106 /* Node private data */
107 struct ng_iface_private {
108         struct  ifnet *ifp;             /* Our interface */
109         int     unit;                   /* Interface unit number */
110         node_p  node;                   /* Our netgraph node */
111         hook_p  hooks[NUM_FAMILIES];    /* Hook for each address family */
112 };
113 typedef struct ng_iface_private *priv_p;
114
115 /* Interface methods */
116 static void     ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *);
117 static int      ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
118                         struct ucred *cr);
119 static int      ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
120                         struct sockaddr *dst, struct rtentry *rt0);
121 static void     ng_iface_bpftap(struct ifnet *ifp,
122                         struct mbuf *m, sa_family_t family);
123 static int      ng_iface_send(struct ifnet *ifp, struct mbuf *m,
124                         sa_family_t sa);
125 #ifdef DEBUG
126 static void     ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
127 #endif
128
129 /* Netgraph methods */
130 static int              ng_iface_mod_event(module_t, int, void *);
131 static ng_constructor_t ng_iface_constructor;
132 static ng_rcvmsg_t      ng_iface_rcvmsg;
133 static ng_shutdown_t    ng_iface_shutdown;
134 static ng_newhook_t     ng_iface_newhook;
135 static ng_rcvdata_t     ng_iface_rcvdata;
136 static ng_disconnect_t  ng_iface_disconnect;
137
138 /* Helper stuff */
139 static iffam_p  get_iffam_from_af(sa_family_t family);
140 static iffam_p  get_iffam_from_hook(priv_p priv, hook_p hook);
141 static iffam_p  get_iffam_from_name(const char *name);
142 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
143
144 /* Parse type for struct ng_cisco_ipaddr */
145 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
146         = NG_CISCO_IPADDR_TYPE_INFO;
147 static const struct ng_parse_type ng_cisco_ipaddr_type = {
148         &ng_parse_struct_type,
149         &ng_cisco_ipaddr_type_fields
150 };
151
152 /* List of commands and how to convert arguments to/from ASCII */
153 static const struct ng_cmdlist ng_iface_cmds[] = {
154         {
155           NGM_IFACE_COOKIE,
156           NGM_IFACE_GET_IFNAME,
157           "getifname",
158           NULL,
159           &ng_parse_string_type
160         },
161         {
162           NGM_IFACE_COOKIE,
163           NGM_IFACE_POINT2POINT,
164           "point2point",
165           NULL,
166           NULL
167         },
168         {
169           NGM_IFACE_COOKIE,
170           NGM_IFACE_BROADCAST,
171           "broadcast",
172           NULL,
173           NULL
174         },
175         {
176           NGM_CISCO_COOKIE,
177           NGM_CISCO_GET_IPADDR,
178           "getipaddr",
179           NULL,
180           &ng_cisco_ipaddr_type
181         },
182         {
183           NGM_IFACE_COOKIE,
184           NGM_IFACE_GET_IFINDEX,
185           "getifindex",
186           NULL,
187           &ng_parse_uint32_type
188         },
189         { 0 }
190 };
191
192 /* Node type descriptor */
193 static struct ng_type typestruct = {
194         .version =      NG_ABI_VERSION,
195         .name =         NG_IFACE_NODE_TYPE,
196         .mod_event =    ng_iface_mod_event,
197         .constructor =  ng_iface_constructor,
198         .rcvmsg =       ng_iface_rcvmsg,
199         .shutdown =     ng_iface_shutdown,
200         .newhook =      ng_iface_newhook,
201         .rcvdata =      ng_iface_rcvdata,
202         .disconnect =   ng_iface_disconnect,
203         .cmdlist =      ng_iface_cmds,
204 };
205 NETGRAPH_INIT(iface, &typestruct);
206
207 /* We keep a bitmap indicating which unit numbers are free.
208    One means the unit number is free, zero means it's taken. */
209 static int      *ng_iface_units = NULL;
210 static int      ng_iface_units_len = 0;
211
212 #define UNITS_BITSPERWORD       (sizeof(*ng_iface_units) * NBBY)
213
214
215 /************************************************************************
216                         HELPER STUFF
217  ************************************************************************/
218
219 /*
220  * Get the family descriptor from the family ID
221  */
222 static __inline iffam_p
223 get_iffam_from_af(sa_family_t family)
224 {
225         iffam_p iffam;
226         int k;
227
228         for (k = 0; k < NUM_FAMILIES; k++) {
229                 iffam = &gFamilies[k];
230                 if (iffam->family == family)
231                         return (iffam);
232         }
233         return (NULL);
234 }
235
236 /*
237  * Get the family descriptor from the hook
238  */
239 static __inline iffam_p
240 get_iffam_from_hook(priv_p priv, hook_p hook)
241 {
242         int k;
243
244         for (k = 0; k < NUM_FAMILIES; k++)
245                 if (priv->hooks[k] == hook)
246                         return (&gFamilies[k]);
247         return (NULL);
248 }
249
250 /*
251  * Get the hook from the iffam descriptor
252  */
253
254 static __inline hook_p *
255 get_hook_from_iffam(priv_p priv, iffam_p iffam)
256 {
257         return (&priv->hooks[iffam - gFamilies]);
258 }
259
260 /*
261  * Get the iffam descriptor from the name
262  */
263 static __inline iffam_p
264 get_iffam_from_name(const char *name)
265 {
266         iffam_p iffam;
267         int k;
268
269         for (k = 0; k < NUM_FAMILIES; k++) {
270                 iffam = &gFamilies[k];
271                 if (!strcmp(iffam->hookname, name))
272                         return (iffam);
273         }
274         return (NULL);
275 }
276
277 /*
278  * Find the first free unit number for a new interface.
279  * Increase the size of the unit bitmap as necessary.
280  */
281 static __inline__ int
282 ng_iface_get_unit(int *unit)
283 {
284         int index, bit;
285
286         for (index = 0; index < ng_iface_units_len
287             && ng_iface_units[index] == 0; index++);
288         if (index == ng_iface_units_len) {              /* extend array */
289                 int i, *newarray, newlen;
290
291                 newlen = (2 * ng_iface_units_len) + 4;
292                 newarray = kmalloc(newlen * sizeof(*ng_iface_units),
293                     M_NETGRAPH_IFACE, M_NOWAIT);
294                 if (newarray == NULL)
295                         return (ENOMEM);
296                 bcopy(ng_iface_units, newarray,
297                     ng_iface_units_len * sizeof(*ng_iface_units));
298                 for (i = ng_iface_units_len; i < newlen; i++)
299                         newarray[i] = ~0;
300                 if (ng_iface_units != NULL)
301                         kfree(ng_iface_units, M_NETGRAPH_IFACE);
302                 ng_iface_units = newarray;
303                 ng_iface_units_len = newlen;
304         }
305         bit = ffs(ng_iface_units[index]) - 1;
306         KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
307             ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
308         ng_iface_units[index] &= ~(1 << bit);
309         *unit = (index * UNITS_BITSPERWORD) + bit;
310         return (0);
311 }
312
313 /*
314  * Free a no longer needed unit number.
315  */
316 static __inline__ void
317 ng_iface_free_unit(int unit)
318 {
319         int index, bit;
320
321         index = unit / UNITS_BITSPERWORD;
322         bit = unit % UNITS_BITSPERWORD;
323         KASSERT(index < ng_iface_units_len,
324             ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
325         KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
326             ("%s: unit=%d is free", __func__, unit));
327         ng_iface_units[index] |= (1 << bit);
328         /*
329          * XXX We could think about reducing the size of ng_iface_units[]
330          * XXX here if the last portion is all ones
331          */
332 }
333
334 /************************************************************************
335                         INTERFACE STUFF
336  ************************************************************************/
337
338 /*
339  * Process an ioctl for the virtual interface
340  */
341 static int
342 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
343     struct ucred *cr)
344 {
345         struct ifreq *const ifr = (struct ifreq *) data;
346         int error = 0;
347
348 #ifdef DEBUG
349         ng_iface_print_ioctl(ifp, command, data);
350 #endif
351         crit_enter();
352         switch (command) {
353
354         /* These two are mostly handled at a higher layer */
355         case SIOCSIFADDR:
356                 ifp->if_flags |= IFF_UP;
357                 ifp->if_flags |= IFF_RUNNING;
358                 ifq_clr_oactive(&ifp->if_snd);
359                 break;
360         case SIOCGIFADDR:
361                 break;
362
363         /* Set flags */
364         case SIOCSIFFLAGS:
365                 /*
366                  * If the interface is marked up and stopped, then start it.
367                  * If it is marked down and running, then stop it.
368                  */
369                 if (ifr->ifr_flags & IFF_UP) {
370                         if (!(ifp->if_flags & IFF_RUNNING)) {
371                                 ifq_clr_oactive(&ifp->if_snd);
372                                 ifp->if_flags |= IFF_RUNNING;
373                         }
374                 } else {
375                         if (ifp->if_flags & IFF_RUNNING) {
376                                 ifp->if_flags &= ~IFF_RUNNING;
377                                 ifq_clr_oactive(&ifp->if_snd);
378                         }
379                 }
380                 break;
381
382         /* Set the interface MTU */
383         case SIOCSIFMTU:
384                 if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
385                     || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
386                         error = EINVAL;
387                 else
388                         ifp->if_mtu = ifr->ifr_mtu;
389                 break;
390
391         /* Stuff that's not supported */
392         case SIOCADDMULTI:
393         case SIOCDELMULTI:
394                 error = 0;
395                 break;
396         case SIOCSIFPHYS:
397                 error = EOPNOTSUPP;
398                 break;
399
400         default:
401                 error = EINVAL;
402                 break;
403         }
404         crit_exit();
405         return (error);
406 }
407
408 /*
409  * This routine is called to deliver a packet out the interface.
410  * We simply look at the address family and relay the packet to
411  * the corresponding hook, if it exists and is connected.
412  */
413
414 static int
415 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
416                 struct sockaddr *dst, struct rtentry *rt0)
417 {
418         uint32_t af;
419         int error;
420
421         /* Check interface flags */
422         if (!((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))) {
423                 m_freem(m);
424                 return (ENETDOWN);
425         }
426
427         /* BPF writes need to be handled specially. */
428         if (dst->sa_family == AF_UNSPEC) {
429                 bcopy(dst->sa_data, &af, sizeof(af));
430                 dst->sa_family = af;
431         }
432
433         /* Berkeley packet filter */
434         ng_iface_bpftap(ifp, m, dst->sa_family);
435
436         if (ifq_is_enabled(&ifp->if_snd)) {
437                 M_PREPEND(m, sizeof(sa_family_t), MB_DONTWAIT);
438                 if (m == NULL) {
439                         IFNET_STAT_INC(ifp, oerrors, 1);
440                         return (ENOBUFS);
441                 }
442                 *(sa_family_t *)m->m_data = dst->sa_family;
443                 error = ifq_dispatch(ifp, m, NULL);
444         } else
445                 error = ng_iface_send(ifp, m, dst->sa_family);
446
447         return (error);
448 }
449
450 /*
451  * Start method is used only when ALTQ is enabled.
452  */
453 static void
454 ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
455 {
456         sa_family_t sa;
457
458         KASSERT(ifq_is_enabled(&ifp->if_snd), ("%s without ALTQ", __func__));
459
460         for(;;) {
461                 struct mbuf *m;
462
463                 m = ifq_dequeue(&ifp->if_snd);
464                 if (m == NULL)
465                         break;
466                 sa = *mtod(m, sa_family_t *);
467                 m_adj(m, sizeof(sa_family_t));
468                 ng_iface_send(ifp, m, sa);
469         }
470 }
471
472 /*
473  * Flash a packet by the BPF (requires prepending 4 byte AF header)
474  * Note the phoney mbuf; this is OK because BPF treats it read-only.
475  */
476 static void
477 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
478 {
479         int32_t family4 = (int32_t)family;
480
481         KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
482
483         if (ifp->if_bpf) {
484                 bpf_gettoken();
485                 if (ifp->if_bpf)
486                         bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4));
487                 bpf_reltoken();
488         }
489 }
490
491 /*
492  * This routine does actual delivery of the packet into the
493  * netgraph(4). It is called from ng_iface_start() and
494  * ng_iface_output().
495  */
496 static int
497 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
498 {
499         const priv_p priv = (priv_p) ifp->if_softc;
500         const iffam_p iffam = get_iffam_from_af(sa);
501         int error;
502         int len;
503
504         /* Check address family to determine hook (if known) */
505         if (iffam == NULL) {
506                 m_freem(m);
507                 log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
508                 return (EAFNOSUPPORT);
509         }
510
511         /* Copy length before the mbuf gets invalidated. */
512         len = m->m_pkthdr.len;
513
514         /* Send packet. If hook is not connected,
515            mbuf will get freed. */
516         NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
517
518         /* Update stats. */
519         if (error == 0) {
520                 IFNET_STAT_INC(ifp, obytes, len);
521                 IFNET_STAT_INC(ifp, opackets, 1);
522         }
523
524         return (error);
525 }
526
527 #ifdef DEBUG
528 /*
529  * Display an ioctl to the virtual interface
530  */
531
532 static void
533 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
534 {
535         char   *str;
536
537         switch (command & IOC_DIRMASK) {
538         case IOC_VOID:
539                 str = "IO";
540                 break;
541         case IOC_OUT:
542                 str = "IOR";
543                 break;
544         case IOC_IN:
545                 str = "IOW";
546                 break;
547         case IOC_INOUT:
548                 str = "IORW";
549                 break;
550         default:
551                 str = "IO??";
552         }
553         log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
554                ifp->if_xname,
555                str,
556                IOCGROUP(command),
557                command & 0xff,
558                IOCPARM_LEN(command));
559 }
560 #endif /* DEBUG */
561
562 /************************************************************************
563                         NETGRAPH NODE STUFF
564  ************************************************************************/
565
566 /*
567  * Constructor for a node
568  */
569 static int
570 ng_iface_constructor(node_p node)
571 {
572         struct ifnet *ifp;
573         priv_p priv;
574         int error;
575
576         /* Allocate node and interface private structures */
577         priv = kmalloc(sizeof(*priv), M_NETGRAPH_IFACE,
578                        M_WAITOK | M_NULLOK | M_ZERO);
579         if (priv == NULL)
580                 return (ENOMEM);
581         ifp = kmalloc(sizeof(*ifp), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO);
582         if (ifp == NULL) {
583                 kfree(priv, M_NETGRAPH_IFACE);
584                 return (ENOMEM);
585         }
586
587         /* Link them together */
588         ifp->if_softc = priv;
589         priv->ifp = ifp;
590
591         /* Get an interface unit number */
592         if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
593                 kfree(ifp, M_NETGRAPH_IFACE);
594                 kfree(priv, M_NETGRAPH_IFACE);
595                 return (error);
596         }
597
598
599         /* Link together node and private info */
600         NG_NODE_SET_PRIVATE(node, priv);
601         priv->node = node;
602
603         /* Initialize interface structure */
604         if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
605         ifp->if_output = ng_iface_output;
606         ifp->if_start = ng_iface_start;
607         ifp->if_ioctl = ng_iface_ioctl;
608         ifp->if_watchdog = NULL;
609         ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
610         ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
611         ifp->if_type = IFT_PROPVIRTUAL;         /* XXX */
612         ifp->if_addrlen = 0;                    /* XXX */
613         ifp->if_hdrlen = 0;                     /* XXX */
614         ifp->if_baudrate = 64000;               /* XXX */
615         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
616         ifq_set_ready(&ifp->if_snd);
617
618         /* Give this node the same name as the interface (if possible) */
619         if (ng_name_node(node, ifp->if_xname) != 0)
620                 log(LOG_WARNING, "%s: can't acquire netgraph name\n",
621                     ifp->if_xname);
622
623         /* Attach the interface */
624         if_attach(ifp, NULL);
625         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
626
627         /* Done */
628         return (0);
629 }
630
631 /*
632  * Give our ok for a hook to be added
633  */
634 static int
635 ng_iface_newhook(node_p node, hook_p hook, const char *name)
636 {
637         const iffam_p iffam = get_iffam_from_name(name);
638         hook_p *hookptr;
639
640         if (iffam == NULL)
641                 return (EPFNOSUPPORT);
642         hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
643         if (*hookptr != NULL)
644                 return (EISCONN);
645         *hookptr = hook;
646         NG_HOOK_HI_STACK(hook);
647         return (0);
648 }
649
650 /*
651  * Receive a control message
652  */
653 static int
654 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
655 {
656         const priv_p priv = NG_NODE_PRIVATE(node);
657         struct ifnet *const ifp = priv->ifp;
658         struct ng_mesg *resp = NULL;
659         int error = 0;
660         struct ng_mesg *msg;
661
662         NGI_GET_MSG(item, msg);
663         switch (msg->header.typecookie) {
664         case NGM_IFACE_COOKIE:
665                 switch (msg->header.cmd) {
666                 case NGM_IFACE_GET_IFNAME:
667                         NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
668                         if (resp == NULL) {
669                                 error = ENOMEM;
670                                 break;
671                         }
672                         strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
673                         break;
674
675                 case NGM_IFACE_POINT2POINT:
676                 case NGM_IFACE_BROADCAST:
677                     {
678
679                         /* Deny request if interface is UP */
680                         if ((ifp->if_flags & IFF_UP) != 0)
681                                 return (EBUSY);
682
683                         /* Change flags */
684                         switch (msg->header.cmd) {
685                         case NGM_IFACE_POINT2POINT:
686                                 ifp->if_flags |= IFF_POINTOPOINT;
687                                 ifp->if_flags &= ~IFF_BROADCAST;
688                                 break;
689                         case NGM_IFACE_BROADCAST:
690                                 ifp->if_flags &= ~IFF_POINTOPOINT;
691                                 ifp->if_flags |= IFF_BROADCAST;
692                                 break;
693                         }
694                         break;
695                     }
696
697                 case NGM_IFACE_GET_IFINDEX:
698                         NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
699                         if (resp == NULL) {
700                                 error = ENOMEM;
701                                 break;
702                         }
703                         *((uint32_t *)resp->data) = priv->ifp->if_index;
704                         break;
705
706                 default:
707                         error = EINVAL;
708                         break;
709                 }
710                 break;
711         case NGM_CISCO_COOKIE:
712                 switch (msg->header.cmd) {
713                 case NGM_CISCO_GET_IPADDR:      /* we understand this too */
714                     {
715                         struct ifaddr_container *ifac;
716
717                         /* Return the first configured IP address */
718                         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
719                                 struct ifaddr *ifa = ifac->ifa;
720                                 struct ng_cisco_ipaddr *ips;
721
722                                 if (ifa->ifa_addr->sa_family != AF_INET)
723                                         continue;
724                                 NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK);
725                                 if (resp == NULL) {
726                                         error = ENOMEM;
727                                         break;
728                                 }
729                                 ips = (struct ng_cisco_ipaddr *)resp->data;
730                                 ips->ipaddr = ((struct sockaddr_in *)
731                                                 ifa->ifa_addr)->sin_addr;
732                                 ips->netmask = ((struct sockaddr_in *)
733                                                 ifa->ifa_netmask)->sin_addr;
734                                 break;
735                         }
736
737                         /* No IP addresses on this interface? */
738                         if (ifac == NULL)
739                                 error = EADDRNOTAVAIL;
740                         break;
741                     }
742                 default:
743                         error = EINVAL;
744                         break;
745                 }
746                 break;
747         case NGM_FLOW_COOKIE:
748                 switch (msg->header.cmd) {
749                 case NGM_LINK_IS_UP:
750                         ifp->if_flags |= IFF_RUNNING;
751                         break;
752                 case NGM_LINK_IS_DOWN:
753                         ifp->if_flags &= ~IFF_RUNNING;
754                         break;
755                 default:
756                         break;
757                 }
758                 break;
759         default:
760                 error = EINVAL;
761                 break;
762         }
763         NG_RESPOND_MSG(error, node, item, resp);
764         NG_FREE_MSG(msg);
765         return (error);
766 }
767
768 /*
769  * Recive data from a hook. Pass the packet to the correct input routine.
770  */
771 static int
772 ng_iface_rcvdata(hook_p hook, item_p item)
773 {
774         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
775         const iffam_p iffam = get_iffam_from_hook(priv, hook);
776         struct ifnet *const ifp = priv->ifp;
777         struct mbuf *m;
778         int isr;
779
780         NGI_GET_M(item, m);
781         NG_FREE_ITEM(item);
782         /* Sanity checks */
783         KASSERT(iffam != NULL, ("%s: iffam", __func__));
784         M_ASSERTPKTHDR(m);
785         if ((ifp->if_flags & IFF_UP) == 0) {
786                 NG_FREE_M(m);
787                 return (ENETDOWN);
788         }
789
790         /* Update interface stats */
791         IFNET_STAT_INC(ifp, ipackets, 1);
792         IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
793
794         /* Note receiving interface */
795         m->m_pkthdr.rcvif = ifp;
796
797         /* Berkeley packet filter */
798         ng_iface_bpftap(ifp, m, iffam->family);
799
800         /* Send packet */
801         switch (iffam->family) {
802 #ifdef INET
803         case AF_INET:
804                 isr = NETISR_IP;
805                 break;
806 #endif
807 #ifdef INET6
808         case AF_INET6:
809                 isr = NETISR_IPV6;
810                 break;
811 #endif
812         default:
813                 m_freem(m);
814                 return (EAFNOSUPPORT);
815         }
816 #if 0
817         /* First chunk of an mbuf contains good junk */
818         if (harvest.point_to_point)
819                 random_harvest(m, 16, 3, 0, RANDOM_NET);
820 #endif
821         netisr_queue(isr, m);
822         return (0);
823 }
824
825 /*
826  * Shutdown and remove the node and its associated interface.
827  */
828 static int
829 ng_iface_shutdown(node_p node)
830 {
831         const priv_p priv = NG_NODE_PRIVATE(node);
832
833         bpfdetach(priv->ifp);
834         if_detach(priv->ifp);
835         kfree(priv->ifp, M_NETGRAPH_IFACE);
836         priv->ifp = NULL;
837         ng_iface_free_unit(priv->unit);
838         kfree(priv, M_NETGRAPH_IFACE);
839         NG_NODE_SET_PRIVATE(node, NULL);
840         NG_NODE_UNREF(node);
841         return (0);
842 }
843
844 /*
845  * Hook disconnection. Note that we do *not* shutdown when all
846  * hooks have been disconnected.
847  */
848 static int
849 ng_iface_disconnect(hook_p hook)
850 {
851         const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
852         const iffam_p iffam = get_iffam_from_hook(priv, hook);
853
854         if (iffam == NULL)
855                 panic(__func__);
856         *get_hook_from_iffam(priv, iffam) = NULL;
857         return (0);
858 }
859
860 /*
861  * Handle loading and unloading for this node type.
862  */
863 static int
864 ng_iface_mod_event(module_t mod, int event, void *data)
865 {
866         int error = 0;
867
868         switch (event) {
869         case MOD_LOAD:
870                 break;
871         case MOD_UNLOAD:
872                 break;
873         default:
874                 error = EOPNOTSUPP;
875                 break;
876         }
877         return (error);
878 }