FreeBSD kernel netgraph code
ng_split.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999-2000, Vitaly V Belekhov
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 *
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/errno.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
39#include <sys/errno.h>
40#include <sys/sockio.h>
41#include <sys/socket.h>
42#include <sys/syslog.h>
43
44#include <netgraph/ng_message.h>
45#include <netgraph/netgraph.h>
46#include <netgraph/ng_split.h>
47
48/* Netgraph methods */
54
55/* Node type descriptor */
56static struct ng_type typestruct = {
58 .name = NG_SPLIT_NODE_TYPE,
59 .constructor = ng_split_constructor,
60 .shutdown = ng_split_shutdown,
61 .newhook = ng_split_newhook,
62 .rcvdata = ng_split_rcvdata,
63 .disconnect = ng_split_disconnect,
64};
66
67/* Node private data */
72 node_p node; /* Our netgraph node */
73};
74typedef struct ng_split_private *priv_p;
75
76/************************************************************************
77 NETGRAPH NODE STUFF
78 ************************************************************************/
79
80/*
81 * Constructor for a node
82 */
83static int
85{
87
88 /* Allocate node */
89 priv = malloc(sizeof(*priv), M_NETGRAPH, M_ZERO | M_WAITOK);
90
91 /* Link together node and private info */
93 priv->node = node;
94
95 /* Done */
96 return (0);
97}
98
99/*
100 * Give our ok for a hook to be added
101 */
102static int
104{
106 hook_p *localhook;
107
108 if (strcmp(name, NG_SPLIT_HOOK_MIXED) == 0) {
109 localhook = &priv->mixed;
110 } else if (strcmp(name, NG_SPLIT_HOOK_IN) == 0) {
111 localhook = &priv->in;
112 } else if (strcmp(name, NG_SPLIT_HOOK_OUT) == 0) {
113 localhook = &priv->out;
114 } else
115 return (EINVAL);
116
117 if (*localhook != NULL)
118 return (EISCONN);
119 *localhook = hook;
120 NG_HOOK_SET_PRIVATE(hook, localhook);
121
122 return (0);
123}
124
125/*
126 * Recive data from a hook.
127 */
128static int
130{
132 int error = 0;
133
134 if (hook == priv->out) {
135 printf("ng_split: got packet from out hook!\n");
136 NG_FREE_ITEM(item);
137 error = EINVAL;
138 } else if ((hook == priv->in) && (priv->mixed != NULL)) {
139 NG_FWD_ITEM_HOOK(error, item, priv->mixed);
140 } else if ((hook == priv->mixed) && (priv->out != NULL)) {
141 NG_FWD_ITEM_HOOK(error, item, priv->out);
142 }
143
144 if (item)
145 NG_FREE_ITEM(item);
146
147 return (error);
148}
149
150static int
152{
154
157 free(priv, M_NETGRAPH);
158
159 return (0);
160}
161
162/*
163 * Hook disconnection
164 */
165static int
167{
168 hook_p *localhook = NG_HOOK_PRIVATE(hook);
169
170 KASSERT(localhook != NULL, ("%s: null info", __func__));
171 *localhook = NULL;
172 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
173 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
175 }
176
177 return (0);
178}
#define NG_HOOK_NODE(hook)
Definition: netgraph.h:339
#define NG_FWD_ITEM_HOOK(error, item, hook)
Definition: netgraph.h:898
int ng_disconnect_t(hook_p hook)
Definition: netgraph.h:107
#define NG_NODE_SET_PRIVATE(node, val)
Definition: netgraph.h:608
#define NG_NODE_IS_VALID(node)
Definition: netgraph.h:610
#define NG_NODE_UNREF(node)
Definition: netgraph.h:607
#define NG_HOOK_SET_PRIVATE(hook, val)
Definition: netgraph.h:333
int ng_rmnode_self(node_p here)
Definition: ng_base.c:1609
int ng_rcvdata_t(hook_p hook, item_p item)
Definition: netgraph.h:106
int ng_shutdown_t(node_p node)
Definition: netgraph.h:101
#define NG_FREE_ITEM(item)
Definition: netgraph.h:847
int ng_constructor_t(node_p node)
Definition: netgraph.h:99
#define NG_ABI_VERSION
Definition: netgraph.h:77
#define NG_NODE_NUMHOOKS(node)
Definition: netgraph.h:615
#define NG_NODE_PRIVATE(node)
Definition: netgraph.h:609
int ng_newhook_t(node_p node, hook_p hook, const char *name)
Definition: netgraph.h:102
#define NG_HOOK_PRIVATE(hook)
Definition: netgraph.h:336
char *const name
Definition: ng_ppp.c:261
static ng_disconnect_t ng_split_disconnect
Definition: ng_split.c:53
static ng_shutdown_t ng_split_shutdown
Definition: ng_split.c:50
static ng_rcvdata_t ng_split_rcvdata
Definition: ng_split.c:52
static struct ng_type typestruct
Definition: ng_split.c:56
static ng_newhook_t ng_split_newhook
Definition: ng_split.c:51
NETGRAPH_INIT(ng_split, &typestruct)
struct ng_split_private * priv_p
Definition: ng_split.c:74
static ng_constructor_t ng_split_constructor
Definition: ng_split.c:49
#define NG_SPLIT_HOOK_IN
Definition: ng_split.h:43
#define NG_SPLIT_HOOK_MIXED
Definition: ng_split.h:41
#define NG_SPLIT_HOOK_OUT
Definition: ng_split.h:42
#define NG_SPLIT_NODE_TYPE
Definition: ng_split.h:37
hook_p mixed
Definition: ng_split.c:71
u_int32_t version
Definition: netgraph.h:1077
Definition: ng_sscfu.c:66