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