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