1d473c9d5SGleb Smirnoff /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni *
4d473c9d5SGleb Smirnoff * Copyright (c) 2006 Vadim Goncharov <vadimnuclight@tpu.ru>
5d473c9d5SGleb Smirnoff * All rights reserved.
6d473c9d5SGleb Smirnoff *
7d473c9d5SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
8d473c9d5SGleb Smirnoff * modification, are permitted provided that the following conditions
9d473c9d5SGleb Smirnoff * are met:
10d473c9d5SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
11d473c9d5SGleb Smirnoff * notice unmodified, this list of conditions, and the following
12d473c9d5SGleb Smirnoff * disclaimer.
13d473c9d5SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
14d473c9d5SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
15d473c9d5SGleb Smirnoff * documentation and/or other materials provided with the distribution.
16d473c9d5SGleb Smirnoff *
17d473c9d5SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18d473c9d5SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19d473c9d5SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20d473c9d5SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21d473c9d5SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22d473c9d5SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23d473c9d5SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24d473c9d5SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25d473c9d5SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26d473c9d5SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27d473c9d5SGleb Smirnoff * SUCH DAMAGE.
28d473c9d5SGleb Smirnoff *
29d473c9d5SGleb Smirnoff * Portions Copyright (c) 1999 Whistle Communications, Inc.
30d473c9d5SGleb Smirnoff * (ng_bpf by Archie Cobbs <archie@freebsd.org>)
31d473c9d5SGleb Smirnoff */
32d473c9d5SGleb Smirnoff
33d473c9d5SGleb Smirnoff /*
34d473c9d5SGleb Smirnoff * TAG NETGRAPH NODE TYPE
35d473c9d5SGleb Smirnoff *
36d473c9d5SGleb Smirnoff * This node type accepts an arbitrary number of hooks. Each hook can be
37d473c9d5SGleb Smirnoff * configured for an mbuf_tags(9) definition and two hook names: a hook
38d473c9d5SGleb Smirnoff * for matched packets, and a hook for packets, that didn't match. Incoming
39d473c9d5SGleb Smirnoff * packets are examined for configured tag, matched packets are delivered
40d473c9d5SGleb Smirnoff * out via first hook, and not matched out via second. If corresponding hook
41d473c9d5SGleb Smirnoff * is not configured, packets are dropped.
42d473c9d5SGleb Smirnoff *
43d473c9d5SGleb Smirnoff * A hook can also have an outgoing tag definition configured, so that
44d473c9d5SGleb Smirnoff * all packets leaving the hook will be unconditionally appended with newly
45d473c9d5SGleb Smirnoff * allocated tag.
46d473c9d5SGleb Smirnoff *
47d473c9d5SGleb Smirnoff * Both hooks can be set to null tag definitions (that is, with zeroed
48d473c9d5SGleb Smirnoff * fields), so that packet tags are unmodified on output or all packets
49d473c9d5SGleb Smirnoff * are unconditionally forwarded to non-matching hook on input. There is
50d473c9d5SGleb Smirnoff * also a possibility to replace tags by specifying strip flag on input
51d473c9d5SGleb Smirnoff * and replacing tag on corresponding output tag (or simply remove tag if
52d473c9d5SGleb Smirnoff * no tag specified on output).
53d473c9d5SGleb Smirnoff *
54d473c9d5SGleb Smirnoff * If compiled with NG_TAG_DEBUG, each hook also keeps statistics about
55d473c9d5SGleb Smirnoff * how many packets have matched, etc.
56d473c9d5SGleb Smirnoff */
57d473c9d5SGleb Smirnoff
58d473c9d5SGleb Smirnoff #include <sys/param.h>
59d473c9d5SGleb Smirnoff #include <sys/systm.h>
60d473c9d5SGleb Smirnoff #include <sys/errno.h>
61d473c9d5SGleb Smirnoff #include <sys/kernel.h>
62d473c9d5SGleb Smirnoff #include <sys/malloc.h>
63d473c9d5SGleb Smirnoff #include <sys/mbuf.h>
64d473c9d5SGleb Smirnoff #include <sys/stddef.h>
65d473c9d5SGleb Smirnoff
66d473c9d5SGleb Smirnoff #include <netgraph/ng_message.h>
67d473c9d5SGleb Smirnoff #include <netgraph/netgraph.h>
68d473c9d5SGleb Smirnoff #include <netgraph/ng_parse.h>
69d473c9d5SGleb Smirnoff #include <netgraph/ng_tag.h>
70d473c9d5SGleb Smirnoff
71d473c9d5SGleb Smirnoff #ifdef NG_SEPARATE_MALLOC
72d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_TAG, "netgraph_tag", "netgraph tag node");
73d473c9d5SGleb Smirnoff #else
74d473c9d5SGleb Smirnoff #define M_NETGRAPH_TAG M_NETGRAPH
75d473c9d5SGleb Smirnoff #endif
76d473c9d5SGleb Smirnoff
77d473c9d5SGleb Smirnoff #define ERROUT(x) do { error = (x); goto done; } while (0)
78d473c9d5SGleb Smirnoff
79d473c9d5SGleb Smirnoff /*
80d473c9d5SGleb Smirnoff * Per hook private info.
81d473c9d5SGleb Smirnoff *
82d473c9d5SGleb Smirnoff * We've separated API and ABI here, to make easier changes in this node,
83d473c9d5SGleb Smirnoff * if needed. If you want to change representation, please do not break API.
84d473c9d5SGleb Smirnoff * We still keep API structures in memory to simplify access to them for
85d473c9d5SGleb Smirnoff * GET* messages, but most of data is accessed in internal representation
86d473c9d5SGleb Smirnoff * only. The reason for this is to speed things up - if data will be
87d473c9d5SGleb Smirnoff * accessed from API structures, there would be double pointer dereferencing
88d473c9d5SGleb Smirnoff * in the code, which almost necessarily leads to CPU cache misses and
89d473c9d5SGleb Smirnoff * reloads.
90d473c9d5SGleb Smirnoff *
91d473c9d5SGleb Smirnoff * We also do another optimization by using resolved pointers to
92d473c9d5SGleb Smirnoff * destination hooks instead of expensive ng_findhook().
93d473c9d5SGleb Smirnoff */
94d473c9d5SGleb Smirnoff struct ng_tag_hookinfo {
95d473c9d5SGleb Smirnoff hook_p hi_match; /* matching hook pointer */
96d473c9d5SGleb Smirnoff hook_p hi_nonmatch; /* non-matching hook pointer */
97d473c9d5SGleb Smirnoff uint32_t in_tag_cookie;
98d473c9d5SGleb Smirnoff uint32_t out_tag_cookie;
99d473c9d5SGleb Smirnoff uint16_t in_tag_id;
100d473c9d5SGleb Smirnoff uint16_t in_tag_len;
101d473c9d5SGleb Smirnoff uint16_t out_tag_id;
102d473c9d5SGleb Smirnoff uint16_t out_tag_len;
103d473c9d5SGleb Smirnoff uint8_t strip;
104d473c9d5SGleb Smirnoff void *in_tag_data;
105d473c9d5SGleb Smirnoff void *out_tag_data;
106d473c9d5SGleb Smirnoff struct ng_tag_hookin *in;
107d473c9d5SGleb Smirnoff struct ng_tag_hookout *out;
108d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
109d473c9d5SGleb Smirnoff struct ng_tag_hookstat stats;
110d473c9d5SGleb Smirnoff #endif
111d473c9d5SGleb Smirnoff };
112d473c9d5SGleb Smirnoff typedef struct ng_tag_hookinfo *hinfo_p;
113d473c9d5SGleb Smirnoff
114d473c9d5SGleb Smirnoff /* Netgraph methods. */
115d473c9d5SGleb Smirnoff static ng_constructor_t ng_tag_constructor;
116d473c9d5SGleb Smirnoff static ng_rcvmsg_t ng_tag_rcvmsg;
117d473c9d5SGleb Smirnoff static ng_shutdown_t ng_tag_shutdown;
118d473c9d5SGleb Smirnoff static ng_newhook_t ng_tag_newhook;
119d473c9d5SGleb Smirnoff static ng_rcvdata_t ng_tag_rcvdata;
120d473c9d5SGleb Smirnoff static ng_disconnect_t ng_tag_disconnect;
121d473c9d5SGleb Smirnoff
122d473c9d5SGleb Smirnoff /* Internal helper functions. */
123d473c9d5SGleb Smirnoff static int ng_tag_setdata_in(hook_p hook, const struct ng_tag_hookin *hp);
124d473c9d5SGleb Smirnoff static int ng_tag_setdata_out(hook_p hook, const struct ng_tag_hookout *hp);
125d473c9d5SGleb Smirnoff
126d473c9d5SGleb Smirnoff /* Parse types for the field 'tag_data' in structs ng_tag_hookin and out. */
127d473c9d5SGleb Smirnoff static int
ng_tag_hookinary_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)128d473c9d5SGleb Smirnoff ng_tag_hookinary_getLength(const struct ng_parse_type *type,
129d473c9d5SGleb Smirnoff const u_char *start, const u_char *buf)
130d473c9d5SGleb Smirnoff {
131d473c9d5SGleb Smirnoff const struct ng_tag_hookin *hp;
132d473c9d5SGleb Smirnoff
133d473c9d5SGleb Smirnoff hp = (const struct ng_tag_hookin *)
134d473c9d5SGleb Smirnoff (buf - offsetof(struct ng_tag_hookin, tag_data));
135d473c9d5SGleb Smirnoff return (hp->tag_len);
136d473c9d5SGleb Smirnoff }
137d473c9d5SGleb Smirnoff
138d473c9d5SGleb Smirnoff static int
ng_tag_hookoutary_getLength(const struct ng_parse_type * type,const u_char * start,const u_char * buf)139d473c9d5SGleb Smirnoff ng_tag_hookoutary_getLength(const struct ng_parse_type *type,
140d473c9d5SGleb Smirnoff const u_char *start, const u_char *buf)
141d473c9d5SGleb Smirnoff {
142d473c9d5SGleb Smirnoff const struct ng_tag_hookout *hp;
143d473c9d5SGleb Smirnoff
144d473c9d5SGleb Smirnoff hp = (const struct ng_tag_hookout *)
145d473c9d5SGleb Smirnoff (buf - offsetof(struct ng_tag_hookout, tag_data));
146d473c9d5SGleb Smirnoff return (hp->tag_len);
147d473c9d5SGleb Smirnoff }
148d473c9d5SGleb Smirnoff
149d473c9d5SGleb Smirnoff static const struct ng_parse_type ng_tag_hookinary_type = {
150d473c9d5SGleb Smirnoff &ng_parse_bytearray_type,
151d473c9d5SGleb Smirnoff &ng_tag_hookinary_getLength
152d473c9d5SGleb Smirnoff };
153d473c9d5SGleb Smirnoff
154d473c9d5SGleb Smirnoff static const struct ng_parse_type ng_tag_hookoutary_type = {
155d473c9d5SGleb Smirnoff &ng_parse_bytearray_type,
156d473c9d5SGleb Smirnoff &ng_tag_hookoutary_getLength
157d473c9d5SGleb Smirnoff };
158d473c9d5SGleb Smirnoff
159d473c9d5SGleb Smirnoff /* Parse type for struct ng_tag_hookin. */
160d473c9d5SGleb Smirnoff static const struct ng_parse_struct_field ng_tag_hookin_type_fields[]
161d473c9d5SGleb Smirnoff = NG_TAG_HOOKIN_TYPE_INFO(&ng_tag_hookinary_type);
162d473c9d5SGleb Smirnoff static const struct ng_parse_type ng_tag_hookin_type = {
163d473c9d5SGleb Smirnoff &ng_parse_struct_type,
164d473c9d5SGleb Smirnoff &ng_tag_hookin_type_fields
165d473c9d5SGleb Smirnoff };
166d473c9d5SGleb Smirnoff
167d473c9d5SGleb Smirnoff /* Parse type for struct ng_tag_hookout. */
168d473c9d5SGleb Smirnoff static const struct ng_parse_struct_field ng_tag_hookout_type_fields[]
169d473c9d5SGleb Smirnoff = NG_TAG_HOOKOUT_TYPE_INFO(&ng_tag_hookoutary_type);
170d473c9d5SGleb Smirnoff static const struct ng_parse_type ng_tag_hookout_type = {
171d473c9d5SGleb Smirnoff &ng_parse_struct_type,
172d473c9d5SGleb Smirnoff &ng_tag_hookout_type_fields
173d473c9d5SGleb Smirnoff };
174d473c9d5SGleb Smirnoff
175d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
176d473c9d5SGleb Smirnoff /* Parse type for struct ng_tag_hookstat. */
177d473c9d5SGleb Smirnoff static const struct ng_parse_struct_field ng_tag_hookstat_type_fields[]
178d473c9d5SGleb Smirnoff = NG_TAG_HOOKSTAT_TYPE_INFO;
179d473c9d5SGleb Smirnoff static const struct ng_parse_type ng_tag_hookstat_type = {
180d473c9d5SGleb Smirnoff &ng_parse_struct_type,
181d473c9d5SGleb Smirnoff &ng_tag_hookstat_type_fields
182d473c9d5SGleb Smirnoff };
183d473c9d5SGleb Smirnoff #endif
184d473c9d5SGleb Smirnoff
185d473c9d5SGleb Smirnoff /* List of commands and how to convert arguments to/from ASCII. */
186d473c9d5SGleb Smirnoff static const struct ng_cmdlist ng_tag_cmdlist[] = {
187d473c9d5SGleb Smirnoff {
188d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
189d473c9d5SGleb Smirnoff NGM_TAG_SET_HOOKIN,
190d473c9d5SGleb Smirnoff "sethookin",
191d473c9d5SGleb Smirnoff &ng_tag_hookin_type,
192d473c9d5SGleb Smirnoff NULL
193d473c9d5SGleb Smirnoff },
194d473c9d5SGleb Smirnoff {
195d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
196d473c9d5SGleb Smirnoff NGM_TAG_GET_HOOKIN,
197d473c9d5SGleb Smirnoff "gethookin",
198d473c9d5SGleb Smirnoff &ng_parse_hookbuf_type,
199d473c9d5SGleb Smirnoff &ng_tag_hookin_type
200d473c9d5SGleb Smirnoff },
201d473c9d5SGleb Smirnoff {
202d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
203d473c9d5SGleb Smirnoff NGM_TAG_SET_HOOKOUT,
204d473c9d5SGleb Smirnoff "sethookout",
205d473c9d5SGleb Smirnoff &ng_tag_hookout_type,
206d473c9d5SGleb Smirnoff NULL
207d473c9d5SGleb Smirnoff },
208d473c9d5SGleb Smirnoff {
209d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
210d473c9d5SGleb Smirnoff NGM_TAG_GET_HOOKOUT,
211d473c9d5SGleb Smirnoff "gethookout",
212d473c9d5SGleb Smirnoff &ng_parse_hookbuf_type,
213d473c9d5SGleb Smirnoff &ng_tag_hookout_type
214d473c9d5SGleb Smirnoff },
215d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
216d473c9d5SGleb Smirnoff {
217d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
218d473c9d5SGleb Smirnoff NGM_TAG_GET_STATS,
219d473c9d5SGleb Smirnoff "getstats",
220d473c9d5SGleb Smirnoff &ng_parse_hookbuf_type,
221d473c9d5SGleb Smirnoff &ng_tag_hookstat_type
222d473c9d5SGleb Smirnoff },
223d473c9d5SGleb Smirnoff {
224d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
225d473c9d5SGleb Smirnoff NGM_TAG_CLR_STATS,
226d473c9d5SGleb Smirnoff "clrstats",
227d473c9d5SGleb Smirnoff &ng_parse_hookbuf_type,
228d473c9d5SGleb Smirnoff NULL
229d473c9d5SGleb Smirnoff },
230d473c9d5SGleb Smirnoff {
231d473c9d5SGleb Smirnoff NGM_TAG_COOKIE,
232d473c9d5SGleb Smirnoff NGM_TAG_GETCLR_STATS,
233d473c9d5SGleb Smirnoff "getclrstats",
234d473c9d5SGleb Smirnoff &ng_parse_hookbuf_type,
235d473c9d5SGleb Smirnoff &ng_tag_hookstat_type
236d473c9d5SGleb Smirnoff },
237d473c9d5SGleb Smirnoff #endif
238d473c9d5SGleb Smirnoff { 0 }
239d473c9d5SGleb Smirnoff };
240d473c9d5SGleb Smirnoff
241d473c9d5SGleb Smirnoff /* Netgraph type descriptor. */
242d473c9d5SGleb Smirnoff static struct ng_type typestruct = {
243d473c9d5SGleb Smirnoff .version = NG_ABI_VERSION,
244d473c9d5SGleb Smirnoff .name = NG_TAG_NODE_TYPE,
245d473c9d5SGleb Smirnoff .constructor = ng_tag_constructor,
246d473c9d5SGleb Smirnoff .rcvmsg = ng_tag_rcvmsg,
247d473c9d5SGleb Smirnoff .shutdown = ng_tag_shutdown,
248d473c9d5SGleb Smirnoff .newhook = ng_tag_newhook,
249d473c9d5SGleb Smirnoff .rcvdata = ng_tag_rcvdata,
250d473c9d5SGleb Smirnoff .disconnect = ng_tag_disconnect,
251d473c9d5SGleb Smirnoff .cmdlist = ng_tag_cmdlist,
252d473c9d5SGleb Smirnoff };
253d473c9d5SGleb Smirnoff NETGRAPH_INIT(tag, &typestruct);
254d473c9d5SGleb Smirnoff
255d473c9d5SGleb Smirnoff /*
256d473c9d5SGleb Smirnoff * This are default API structures (initialized to zeroes) which are
257d473c9d5SGleb Smirnoff * returned in response to GET* messages when no configuration was made.
258d473c9d5SGleb Smirnoff * One could ask why to have this structures at all when we have
259d473c9d5SGleb Smirnoff * ng_tag_hookinfo initialized to zero and don't need in and out structures
260d473c9d5SGleb Smirnoff * at all to operate. Unfortunatelly, we have to return thisHook field
2617037ebe6SGordon Bergling * in response to messages so the fastest and simplest way is to have
262d473c9d5SGleb Smirnoff * this default structures and initialize thisHook once at hook creation
263d473c9d5SGleb Smirnoff * rather than to do it on every response.
264d473c9d5SGleb Smirnoff */
265d473c9d5SGleb Smirnoff
266d473c9d5SGleb Smirnoff /* Default tag values for a hook that matches nothing. */
267d473c9d5SGleb Smirnoff static const struct ng_tag_hookin ng_tag_default_in = {
268d473c9d5SGleb Smirnoff { '\0' }, /* to be filled in at hook creation time */
269d473c9d5SGleb Smirnoff { '\0' },
270d473c9d5SGleb Smirnoff { '\0' },
271d473c9d5SGleb Smirnoff 0,
272d473c9d5SGleb Smirnoff 0,
273d473c9d5SGleb Smirnoff 0,
274d473c9d5SGleb Smirnoff 0
275d473c9d5SGleb Smirnoff };
276d473c9d5SGleb Smirnoff
277d473c9d5SGleb Smirnoff /* Default tag values for a hook that adds nothing */
278d473c9d5SGleb Smirnoff static const struct ng_tag_hookout ng_tag_default_out = {
279d473c9d5SGleb Smirnoff { '\0' }, /* to be filled in at hook creation time */
280d473c9d5SGleb Smirnoff 0,
281d473c9d5SGleb Smirnoff 0,
282d473c9d5SGleb Smirnoff 0
283d473c9d5SGleb Smirnoff };
284d473c9d5SGleb Smirnoff
285d473c9d5SGleb Smirnoff /*
286d473c9d5SGleb Smirnoff * Node constructor.
287d473c9d5SGleb Smirnoff *
288d473c9d5SGleb Smirnoff * We don't keep any per-node private data - we do it on per-hook basis.
289d473c9d5SGleb Smirnoff */
290d473c9d5SGleb Smirnoff static int
ng_tag_constructor(node_p node)291d473c9d5SGleb Smirnoff ng_tag_constructor(node_p node)
292d473c9d5SGleb Smirnoff {
293d473c9d5SGleb Smirnoff return (0);
294d473c9d5SGleb Smirnoff }
295d473c9d5SGleb Smirnoff
296d473c9d5SGleb Smirnoff /*
297d473c9d5SGleb Smirnoff * Add a hook.
298d473c9d5SGleb Smirnoff */
299d473c9d5SGleb Smirnoff static int
ng_tag_newhook(node_p node,hook_p hook,const char * name)300d473c9d5SGleb Smirnoff ng_tag_newhook(node_p node, hook_p hook, const char *name)
301d473c9d5SGleb Smirnoff {
302d473c9d5SGleb Smirnoff hinfo_p hip;
303d473c9d5SGleb Smirnoff int error;
304d473c9d5SGleb Smirnoff
305d473c9d5SGleb Smirnoff /* Create hook private structure. */
306bdc99a49SGleb Smirnoff hip = malloc(sizeof(*hip), M_NETGRAPH_TAG, M_NOWAIT | M_ZERO);
307bdc99a49SGleb Smirnoff if (hip == NULL)
308bdc99a49SGleb Smirnoff return (ENOMEM);
309d473c9d5SGleb Smirnoff NG_HOOK_SET_PRIVATE(hook, hip);
310d473c9d5SGleb Smirnoff
311d473c9d5SGleb Smirnoff /*
312d473c9d5SGleb Smirnoff * After M_ZERO both in and out hook pointers are set to NULL,
313d473c9d5SGleb Smirnoff * as well as all members and pointers to in and out API
314d473c9d5SGleb Smirnoff * structures, so we need to set explicitly only thisHook field
315d473c9d5SGleb Smirnoff * in that structures (after allocating them, of course).
316d473c9d5SGleb Smirnoff */
317d473c9d5SGleb Smirnoff
318d473c9d5SGleb Smirnoff /* Attach the default IN data. */
319d473c9d5SGleb Smirnoff if ((error = ng_tag_setdata_in(hook, &ng_tag_default_in)) != 0) {
3201ede983cSDag-Erling Smørgrav free(hip, M_NETGRAPH_TAG);
321d473c9d5SGleb Smirnoff return (error);
322d473c9d5SGleb Smirnoff }
323d473c9d5SGleb Smirnoff
324d473c9d5SGleb Smirnoff /* Attach the default OUT data. */
325d473c9d5SGleb Smirnoff if ((error = ng_tag_setdata_out(hook, &ng_tag_default_out)) != 0) {
3261ede983cSDag-Erling Smørgrav free(hip, M_NETGRAPH_TAG);
327d473c9d5SGleb Smirnoff return (error);
328d473c9d5SGleb Smirnoff }
329d473c9d5SGleb Smirnoff
330d473c9d5SGleb Smirnoff /*
331d473c9d5SGleb Smirnoff * Set hook name. This is done only once at hook creation time
332d473c9d5SGleb Smirnoff * since hook name can't change, rather than to do it on every
333d473c9d5SGleb Smirnoff * response to messages requesting API structures with data who
334d473c9d5SGleb Smirnoff * we are etc.
335d473c9d5SGleb Smirnoff */
336d473c9d5SGleb Smirnoff strncpy(hip->in->thisHook, name, sizeof(hip->in->thisHook) - 1);
337d473c9d5SGleb Smirnoff hip->in->thisHook[sizeof(hip->in->thisHook) - 1] = '\0';
338d473c9d5SGleb Smirnoff strncpy(hip->out->thisHook, name, sizeof(hip->out->thisHook) - 1);
339d473c9d5SGleb Smirnoff hip->out->thisHook[sizeof(hip->out->thisHook) - 1] = '\0';
340d473c9d5SGleb Smirnoff return (0);
341d473c9d5SGleb Smirnoff }
342d473c9d5SGleb Smirnoff
343d473c9d5SGleb Smirnoff /*
344d473c9d5SGleb Smirnoff * Receive a control message.
345d473c9d5SGleb Smirnoff */
346d473c9d5SGleb Smirnoff static int
ng_tag_rcvmsg(node_p node,item_p item,hook_p lasthook)347d473c9d5SGleb Smirnoff ng_tag_rcvmsg(node_p node, item_p item, hook_p lasthook)
348d473c9d5SGleb Smirnoff {
349d473c9d5SGleb Smirnoff struct ng_mesg *msg;
350d473c9d5SGleb Smirnoff struct ng_mesg *resp = NULL;
351d473c9d5SGleb Smirnoff int error = 0;
352d473c9d5SGleb Smirnoff
353d473c9d5SGleb Smirnoff NGI_GET_MSG(item, msg);
354d473c9d5SGleb Smirnoff switch (msg->header.typecookie) {
355d473c9d5SGleb Smirnoff case NGM_TAG_COOKIE:
356d473c9d5SGleb Smirnoff switch (msg->header.cmd) {
357d473c9d5SGleb Smirnoff case NGM_TAG_SET_HOOKIN:
358d473c9d5SGleb Smirnoff {
359d473c9d5SGleb Smirnoff struct ng_tag_hookin *const
360d473c9d5SGleb Smirnoff hp = (struct ng_tag_hookin *)msg->data;
361d473c9d5SGleb Smirnoff hook_p hook;
362d473c9d5SGleb Smirnoff
363d473c9d5SGleb Smirnoff /* Sanity check. */
3647c7c231cSLutz Donnerhacke if (msg->header.arglen < sizeof(*hp) ||
3657c7c231cSLutz Donnerhacke msg->header.arglen < NG_TAG_HOOKIN_SIZE(hp->tag_len))
366d473c9d5SGleb Smirnoff ERROUT(EINVAL);
367d473c9d5SGleb Smirnoff
368d473c9d5SGleb Smirnoff /* Find hook. */
369d473c9d5SGleb Smirnoff if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
370d473c9d5SGleb Smirnoff ERROUT(ENOENT);
371d473c9d5SGleb Smirnoff
372d473c9d5SGleb Smirnoff /* Set new tag values. */
373d473c9d5SGleb Smirnoff if ((error = ng_tag_setdata_in(hook, hp)) != 0)
374d473c9d5SGleb Smirnoff ERROUT(error);
375d473c9d5SGleb Smirnoff break;
376d473c9d5SGleb Smirnoff }
377d473c9d5SGleb Smirnoff
378d473c9d5SGleb Smirnoff case NGM_TAG_SET_HOOKOUT:
379d473c9d5SGleb Smirnoff {
380d473c9d5SGleb Smirnoff struct ng_tag_hookout *const
381d473c9d5SGleb Smirnoff hp = (struct ng_tag_hookout *)msg->data;
382d473c9d5SGleb Smirnoff hook_p hook;
383d473c9d5SGleb Smirnoff
384d473c9d5SGleb Smirnoff /* Sanity check. */
3857c7c231cSLutz Donnerhacke if (msg->header.arglen < sizeof(*hp) ||
3867c7c231cSLutz Donnerhacke msg->header.arglen < NG_TAG_HOOKOUT_SIZE(hp->tag_len))
387d473c9d5SGleb Smirnoff ERROUT(EINVAL);
388d473c9d5SGleb Smirnoff
389d473c9d5SGleb Smirnoff /* Find hook. */
390d473c9d5SGleb Smirnoff if ((hook = ng_findhook(node, hp->thisHook)) == NULL)
391d473c9d5SGleb Smirnoff ERROUT(ENOENT);
392d473c9d5SGleb Smirnoff
393d473c9d5SGleb Smirnoff /* Set new tag values. */
394d473c9d5SGleb Smirnoff if ((error = ng_tag_setdata_out(hook, hp)) != 0)
395d473c9d5SGleb Smirnoff ERROUT(error);
396d473c9d5SGleb Smirnoff break;
397d473c9d5SGleb Smirnoff }
398d473c9d5SGleb Smirnoff
399d473c9d5SGleb Smirnoff case NGM_TAG_GET_HOOKIN:
400d473c9d5SGleb Smirnoff {
401d473c9d5SGleb Smirnoff struct ng_tag_hookin *hp;
402d473c9d5SGleb Smirnoff hook_p hook;
403d473c9d5SGleb Smirnoff
404d473c9d5SGleb Smirnoff /* Sanity check. */
405d473c9d5SGleb Smirnoff if (msg->header.arglen == 0)
406d473c9d5SGleb Smirnoff ERROUT(EINVAL);
407d473c9d5SGleb Smirnoff msg->data[msg->header.arglen - 1] = '\0';
408d473c9d5SGleb Smirnoff
409d473c9d5SGleb Smirnoff /* Find hook. */
410d473c9d5SGleb Smirnoff if ((hook = ng_findhook(node, msg->data)) == NULL)
411d473c9d5SGleb Smirnoff ERROUT(ENOENT);
412d473c9d5SGleb Smirnoff
413d473c9d5SGleb Smirnoff /* Build response. */
414d473c9d5SGleb Smirnoff hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->in;
415d473c9d5SGleb Smirnoff NG_MKRESPONSE(resp, msg,
416d473c9d5SGleb Smirnoff NG_TAG_HOOKIN_SIZE(hp->tag_len), M_WAITOK);
417d473c9d5SGleb Smirnoff /* M_WAITOK can't return NULL. */
418d473c9d5SGleb Smirnoff bcopy(hp, resp->data,
419d473c9d5SGleb Smirnoff NG_TAG_HOOKIN_SIZE(hp->tag_len));
420d473c9d5SGleb Smirnoff break;
421d473c9d5SGleb Smirnoff }
422d473c9d5SGleb Smirnoff
423d473c9d5SGleb Smirnoff case NGM_TAG_GET_HOOKOUT:
424d473c9d5SGleb Smirnoff {
425d473c9d5SGleb Smirnoff struct ng_tag_hookout *hp;
426d473c9d5SGleb Smirnoff hook_p hook;
427d473c9d5SGleb Smirnoff
428d473c9d5SGleb Smirnoff /* Sanity check. */
429d473c9d5SGleb Smirnoff if (msg->header.arglen == 0)
430d473c9d5SGleb Smirnoff ERROUT(EINVAL);
431d473c9d5SGleb Smirnoff msg->data[msg->header.arglen - 1] = '\0';
432d473c9d5SGleb Smirnoff
433d473c9d5SGleb Smirnoff /* Find hook. */
434d473c9d5SGleb Smirnoff if ((hook = ng_findhook(node, msg->data)) == NULL)
435d473c9d5SGleb Smirnoff ERROUT(ENOENT);
436d473c9d5SGleb Smirnoff
437d473c9d5SGleb Smirnoff /* Build response. */
438d473c9d5SGleb Smirnoff hp = ((hinfo_p)NG_HOOK_PRIVATE(hook))->out;
439d473c9d5SGleb Smirnoff NG_MKRESPONSE(resp, msg,
440d473c9d5SGleb Smirnoff NG_TAG_HOOKOUT_SIZE(hp->tag_len), M_WAITOK);
441d473c9d5SGleb Smirnoff /* M_WAITOK can't return NULL. */
442d473c9d5SGleb Smirnoff bcopy(hp, resp->data,
443d473c9d5SGleb Smirnoff NG_TAG_HOOKOUT_SIZE(hp->tag_len));
444d473c9d5SGleb Smirnoff break;
445d473c9d5SGleb Smirnoff }
446d473c9d5SGleb Smirnoff
447d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
448d473c9d5SGleb Smirnoff case NGM_TAG_GET_STATS:
449d473c9d5SGleb Smirnoff case NGM_TAG_CLR_STATS:
450d473c9d5SGleb Smirnoff case NGM_TAG_GETCLR_STATS:
451d473c9d5SGleb Smirnoff {
452d473c9d5SGleb Smirnoff struct ng_tag_hookstat *stats;
453d473c9d5SGleb Smirnoff hook_p hook;
454d473c9d5SGleb Smirnoff
455d473c9d5SGleb Smirnoff /* Sanity check. */
456d473c9d5SGleb Smirnoff if (msg->header.arglen == 0)
457d473c9d5SGleb Smirnoff ERROUT(EINVAL);
458d473c9d5SGleb Smirnoff msg->data[msg->header.arglen - 1] = '\0';
459d473c9d5SGleb Smirnoff
460d473c9d5SGleb Smirnoff /* Find hook. */
461d473c9d5SGleb Smirnoff if ((hook = ng_findhook(node, msg->data)) == NULL)
462d473c9d5SGleb Smirnoff ERROUT(ENOENT);
463d473c9d5SGleb Smirnoff stats = &((hinfo_p)NG_HOOK_PRIVATE(hook))->stats;
464d473c9d5SGleb Smirnoff
465d473c9d5SGleb Smirnoff /* Build response (if desired). */
466d473c9d5SGleb Smirnoff if (msg->header.cmd != NGM_TAG_CLR_STATS) {
467d473c9d5SGleb Smirnoff NG_MKRESPONSE(resp,
468d473c9d5SGleb Smirnoff msg, sizeof(*stats), M_WAITOK);
469d473c9d5SGleb Smirnoff /* M_WAITOK can't return NULL. */
470d473c9d5SGleb Smirnoff bcopy(stats, resp->data, sizeof(*stats));
471d473c9d5SGleb Smirnoff }
472d473c9d5SGleb Smirnoff
473d473c9d5SGleb Smirnoff /* Clear stats (if desired). */
474d473c9d5SGleb Smirnoff if (msg->header.cmd != NGM_TAG_GET_STATS)
475d473c9d5SGleb Smirnoff bzero(stats, sizeof(*stats));
476d473c9d5SGleb Smirnoff break;
477d473c9d5SGleb Smirnoff }
478d473c9d5SGleb Smirnoff #endif /* NG_TAG_DEBUG */
479d473c9d5SGleb Smirnoff
480d473c9d5SGleb Smirnoff default:
481d473c9d5SGleb Smirnoff error = EINVAL;
482d473c9d5SGleb Smirnoff break;
483d473c9d5SGleb Smirnoff }
484d473c9d5SGleb Smirnoff break;
485d473c9d5SGleb Smirnoff default:
486d473c9d5SGleb Smirnoff error = EINVAL;
487d473c9d5SGleb Smirnoff break;
488d473c9d5SGleb Smirnoff }
489d473c9d5SGleb Smirnoff done:
490d473c9d5SGleb Smirnoff NG_RESPOND_MSG(error, node, item, resp);
491d473c9d5SGleb Smirnoff NG_FREE_MSG(msg);
492d473c9d5SGleb Smirnoff return (error);
493d473c9d5SGleb Smirnoff }
494d473c9d5SGleb Smirnoff
495d473c9d5SGleb Smirnoff /*
496d473c9d5SGleb Smirnoff * Receive data on a hook.
497d473c9d5SGleb Smirnoff *
498d473c9d5SGleb Smirnoff * Apply the filter, and then drop or forward packet as appropriate.
499d473c9d5SGleb Smirnoff */
500d473c9d5SGleb Smirnoff static int
ng_tag_rcvdata(hook_p hook,item_p item)501d473c9d5SGleb Smirnoff ng_tag_rcvdata(hook_p hook, item_p item)
502d473c9d5SGleb Smirnoff {
503d473c9d5SGleb Smirnoff struct mbuf *m;
504d473c9d5SGleb Smirnoff struct m_tag *tag = NULL;
505d473c9d5SGleb Smirnoff const hinfo_p hip = NG_HOOK_PRIVATE(hook);
506d473c9d5SGleb Smirnoff uint16_t type, tag_len;
507d473c9d5SGleb Smirnoff uint32_t cookie;
508d473c9d5SGleb Smirnoff hinfo_p dhip;
509d473c9d5SGleb Smirnoff hook_p dest;
510f44692ceSJohn Baldwin #ifdef NG_TAG_DEBUG
511d473c9d5SGleb Smirnoff int totlen;
512f44692ceSJohn Baldwin #endif
513d473c9d5SGleb Smirnoff int found = 0, error = 0;
514d473c9d5SGleb Smirnoff
515d473c9d5SGleb Smirnoff m = NGI_M(item); /* 'item' still owns it.. we are peeking */
516f44692ceSJohn Baldwin #ifdef NG_TAG_DEBUG
517d473c9d5SGleb Smirnoff totlen = m->m_pkthdr.len;
518d473c9d5SGleb Smirnoff
519d473c9d5SGleb Smirnoff hip->stats.recvFrames++;
520d473c9d5SGleb Smirnoff hip->stats.recvOctets += totlen;
521d473c9d5SGleb Smirnoff #endif
522d473c9d5SGleb Smirnoff
523d473c9d5SGleb Smirnoff /* Looking up incoming tag. */
524d473c9d5SGleb Smirnoff cookie = hip->in_tag_cookie;
525d473c9d5SGleb Smirnoff type = hip->in_tag_id;
526d473c9d5SGleb Smirnoff tag_len = hip->in_tag_len;
527d473c9d5SGleb Smirnoff
528d473c9d5SGleb Smirnoff /*
529d473c9d5SGleb Smirnoff * We treat case of all zeroes specially (that is, cookie and
530d473c9d5SGleb Smirnoff * type are equal to zero), as we assume that such tag
531d473c9d5SGleb Smirnoff * can never occur in the wild. So we don't waste time trying
532d473c9d5SGleb Smirnoff * to find such tag (for example, these are zeroes after hook
533d473c9d5SGleb Smirnoff * creation in default structures).
534d473c9d5SGleb Smirnoff */
535d473c9d5SGleb Smirnoff if ((cookie != 0) || (type != 0)) {
536d473c9d5SGleb Smirnoff tag = m_tag_locate(m, cookie, type, NULL);
537d473c9d5SGleb Smirnoff while (tag != NULL) {
538d473c9d5SGleb Smirnoff if (memcmp((void *)(tag + 1),
539d473c9d5SGleb Smirnoff hip->in_tag_data, tag_len) == 0) {
540d473c9d5SGleb Smirnoff found = 1;
541d473c9d5SGleb Smirnoff break;
542d473c9d5SGleb Smirnoff }
543d473c9d5SGleb Smirnoff tag = m_tag_locate(m, cookie, type, tag);
544d473c9d5SGleb Smirnoff }
545d473c9d5SGleb Smirnoff }
546d473c9d5SGleb Smirnoff
547d473c9d5SGleb Smirnoff /* See if we got a match and find destination hook. */
548d473c9d5SGleb Smirnoff if (found) {
549d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
550d473c9d5SGleb Smirnoff hip->stats.recvMatchFrames++;
551d473c9d5SGleb Smirnoff hip->stats.recvMatchOctets += totlen;
552d473c9d5SGleb Smirnoff #endif
553d473c9d5SGleb Smirnoff if (hip->strip)
554d473c9d5SGleb Smirnoff m_tag_delete(m, tag);
555d473c9d5SGleb Smirnoff dest = hip->hi_match;
556d473c9d5SGleb Smirnoff } else
557d473c9d5SGleb Smirnoff dest = hip->hi_nonmatch;
558d473c9d5SGleb Smirnoff if (dest == NULL) {
559d473c9d5SGleb Smirnoff NG_FREE_ITEM(item);
560d473c9d5SGleb Smirnoff return (0);
561d473c9d5SGleb Smirnoff }
562d473c9d5SGleb Smirnoff
563d473c9d5SGleb Smirnoff /* Deliver frame out destination hook. */
564d473c9d5SGleb Smirnoff dhip = NG_HOOK_PRIVATE(dest);
565d473c9d5SGleb Smirnoff
566d473c9d5SGleb Smirnoff #ifdef NG_TAG_DEBUG
567d473c9d5SGleb Smirnoff dhip->stats.xmitOctets += totlen;
568d473c9d5SGleb Smirnoff dhip->stats.xmitFrames++;
569d473c9d5SGleb Smirnoff #endif
570d473c9d5SGleb Smirnoff
571d473c9d5SGleb Smirnoff cookie = dhip->out_tag_cookie;
572d473c9d5SGleb Smirnoff type = dhip->out_tag_id;
573d473c9d5SGleb Smirnoff tag_len = dhip->out_tag_len;
574d473c9d5SGleb Smirnoff
575d473c9d5SGleb Smirnoff if ((cookie != 0) || (type != 0)) {
576d473c9d5SGleb Smirnoff tag = m_tag_alloc(cookie, type, tag_len, M_NOWAIT);
577d473c9d5SGleb Smirnoff /* XXX may be free the mbuf if tag allocation failed? */
578d473c9d5SGleb Smirnoff if (tag != NULL) {
579d473c9d5SGleb Smirnoff if (tag_len != 0) {
580d473c9d5SGleb Smirnoff /* copy tag data to its place */
581d473c9d5SGleb Smirnoff memcpy((void *)(tag + 1),
582d473c9d5SGleb Smirnoff dhip->out_tag_data, tag_len);
583d473c9d5SGleb Smirnoff }
584d473c9d5SGleb Smirnoff m_tag_prepend(m, tag);
585d473c9d5SGleb Smirnoff }
586d473c9d5SGleb Smirnoff }
587d473c9d5SGleb Smirnoff
588d473c9d5SGleb Smirnoff NG_FWD_ITEM_HOOK(error, item, dest);
589d473c9d5SGleb Smirnoff return (error);
590d473c9d5SGleb Smirnoff }
591d473c9d5SGleb Smirnoff
592d473c9d5SGleb Smirnoff /*
593d473c9d5SGleb Smirnoff * Shutdown processing.
594d473c9d5SGleb Smirnoff */
595d473c9d5SGleb Smirnoff static int
ng_tag_shutdown(node_p node)596d473c9d5SGleb Smirnoff ng_tag_shutdown(node_p node)
597d473c9d5SGleb Smirnoff {
598d473c9d5SGleb Smirnoff NG_NODE_UNREF(node);
599d473c9d5SGleb Smirnoff return (0);
600d473c9d5SGleb Smirnoff }
601d473c9d5SGleb Smirnoff
602d473c9d5SGleb Smirnoff /*
603d473c9d5SGleb Smirnoff * Hook disconnection.
604d473c9d5SGleb Smirnoff *
605d473c9d5SGleb Smirnoff * We must check all hooks, since they may reference this one.
606d473c9d5SGleb Smirnoff */
607d473c9d5SGleb Smirnoff static int
ng_tag_disconnect(hook_p hook)608d473c9d5SGleb Smirnoff ng_tag_disconnect(hook_p hook)
609d473c9d5SGleb Smirnoff {
610d473c9d5SGleb Smirnoff const hinfo_p hip = NG_HOOK_PRIVATE(hook);
611d473c9d5SGleb Smirnoff node_p node = NG_HOOK_NODE(hook);
612d473c9d5SGleb Smirnoff hook_p hook2;
613d473c9d5SGleb Smirnoff
614d473c9d5SGleb Smirnoff KASSERT(hip != NULL, ("%s: null info", __func__));
615d473c9d5SGleb Smirnoff
616d473c9d5SGleb Smirnoff LIST_FOREACH(hook2, &node->nd_hooks, hk_hooks) {
617d473c9d5SGleb Smirnoff hinfo_p priv = NG_HOOK_PRIVATE(hook2);
618d473c9d5SGleb Smirnoff
619d473c9d5SGleb Smirnoff if (priv->hi_match == hook)
620d473c9d5SGleb Smirnoff priv->hi_match = NULL;
621d473c9d5SGleb Smirnoff if (priv->hi_nonmatch == hook)
622d473c9d5SGleb Smirnoff priv->hi_nonmatch = NULL;
623d473c9d5SGleb Smirnoff }
624d473c9d5SGleb Smirnoff
6251ede983cSDag-Erling Smørgrav free(hip->in, M_NETGRAPH_TAG);
6261ede983cSDag-Erling Smørgrav free(hip->out, M_NETGRAPH_TAG);
6271ede983cSDag-Erling Smørgrav free(hip, M_NETGRAPH_TAG);
628d473c9d5SGleb Smirnoff NG_HOOK_SET_PRIVATE(hook, NULL); /* for good measure */
629d473c9d5SGleb Smirnoff if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
630d473c9d5SGleb Smirnoff (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
631d473c9d5SGleb Smirnoff ng_rmnode_self(NG_HOOK_NODE(hook));
632d473c9d5SGleb Smirnoff }
633d473c9d5SGleb Smirnoff return (0);
634d473c9d5SGleb Smirnoff }
635d473c9d5SGleb Smirnoff
636d473c9d5SGleb Smirnoff /************************************************************************
637d473c9d5SGleb Smirnoff HELPER STUFF
638d473c9d5SGleb Smirnoff ************************************************************************/
639d473c9d5SGleb Smirnoff
640d473c9d5SGleb Smirnoff /*
641d473c9d5SGleb Smirnoff * Set the IN tag values associated with a hook.
642d473c9d5SGleb Smirnoff */
643d473c9d5SGleb Smirnoff static int
ng_tag_setdata_in(hook_p hook,const struct ng_tag_hookin * hp0)644d473c9d5SGleb Smirnoff ng_tag_setdata_in(hook_p hook, const struct ng_tag_hookin *hp0)
645d473c9d5SGleb Smirnoff {
646d473c9d5SGleb Smirnoff const hinfo_p hip = NG_HOOK_PRIVATE(hook);
647d473c9d5SGleb Smirnoff struct ng_tag_hookin *hp;
648d473c9d5SGleb Smirnoff int size;
649d473c9d5SGleb Smirnoff
650d473c9d5SGleb Smirnoff /* Make a copy of the tag values and data. */
651d473c9d5SGleb Smirnoff size = NG_TAG_HOOKIN_SIZE(hp0->tag_len);
6521ede983cSDag-Erling Smørgrav hp = malloc(size, M_NETGRAPH_TAG, M_WAITOK);
653d473c9d5SGleb Smirnoff /* M_WAITOK can't return NULL. */
654d473c9d5SGleb Smirnoff bcopy(hp0, hp, size);
655d473c9d5SGleb Smirnoff
656d473c9d5SGleb Smirnoff /* Free previous tag, if any, and assign new one. */
657d473c9d5SGleb Smirnoff if (hip->in != NULL)
6581ede983cSDag-Erling Smørgrav free(hip->in, M_NETGRAPH_TAG);
659d473c9d5SGleb Smirnoff hip->in = hp;
660d473c9d5SGleb Smirnoff
661d473c9d5SGleb Smirnoff /*
662d473c9d5SGleb Smirnoff * Resolve hook names to pointers.
663d473c9d5SGleb Smirnoff *
664d473c9d5SGleb Smirnoff * As ng_findhook() is expensive operation to do it on every packet
665d473c9d5SGleb Smirnoff * after tag matching check, we do it here and use resolved pointers
666d473c9d5SGleb Smirnoff * where appropriate.
667d473c9d5SGleb Smirnoff *
668d473c9d5SGleb Smirnoff * XXX The drawback is that user can configure a hook to use
669d473c9d5SGleb Smirnoff * ifMatch/ifNotMatch hooks that do not yet exist and will be added
670d473c9d5SGleb Smirnoff * by user later, so that resolved pointers will be NULL even
671d473c9d5SGleb Smirnoff * if the hook already exists, causing node to drop packets and
672d473c9d5SGleb Smirnoff * user to report bugs. We could do check for this situation on
673d473c9d5SGleb Smirnoff * every hook creation with pointers correction, but that involves
674d473c9d5SGleb Smirnoff * re-resolving for all pointers in all hooks, up to O(n^2) operations,
675d473c9d5SGleb Smirnoff * so we better document this in man page for user not to do
676d473c9d5SGleb Smirnoff * configuration before creating all hooks.
677d473c9d5SGleb Smirnoff */
678d473c9d5SGleb Smirnoff hip->hi_match = ng_findhook(NG_HOOK_NODE(hook), hip->in->ifMatch);
679d473c9d5SGleb Smirnoff hip->hi_nonmatch = ng_findhook(NG_HOOK_NODE(hook), hip->in->ifNotMatch);
680d473c9d5SGleb Smirnoff
681d473c9d5SGleb Smirnoff /* Fill internal values from API structures. */
682d473c9d5SGleb Smirnoff hip->in_tag_cookie = hip->in->tag_cookie;
683d473c9d5SGleb Smirnoff hip->in_tag_id = hip->in->tag_id;
684d473c9d5SGleb Smirnoff hip->in_tag_len = hip->in->tag_len;
685d473c9d5SGleb Smirnoff hip->strip = hip->in->strip;
686d473c9d5SGleb Smirnoff hip->in_tag_data = (void*)(hip->in->tag_data);
687d473c9d5SGleb Smirnoff return (0);
688d473c9d5SGleb Smirnoff }
689d473c9d5SGleb Smirnoff
690d473c9d5SGleb Smirnoff /*
691d473c9d5SGleb Smirnoff * Set the OUT tag values associated with a hook.
692d473c9d5SGleb Smirnoff */
693d473c9d5SGleb Smirnoff static int
ng_tag_setdata_out(hook_p hook,const struct ng_tag_hookout * hp0)694d473c9d5SGleb Smirnoff ng_tag_setdata_out(hook_p hook, const struct ng_tag_hookout *hp0)
695d473c9d5SGleb Smirnoff {
696d473c9d5SGleb Smirnoff const hinfo_p hip = NG_HOOK_PRIVATE(hook);
697d473c9d5SGleb Smirnoff struct ng_tag_hookout *hp;
698d473c9d5SGleb Smirnoff int size;
699d473c9d5SGleb Smirnoff
700d473c9d5SGleb Smirnoff /* Make a copy of the tag values and data. */
701d473c9d5SGleb Smirnoff size = NG_TAG_HOOKOUT_SIZE(hp0->tag_len);
7021ede983cSDag-Erling Smørgrav hp = malloc(size, M_NETGRAPH_TAG, M_WAITOK);
703d473c9d5SGleb Smirnoff /* M_WAITOK can't return NULL. */
704d473c9d5SGleb Smirnoff bcopy(hp0, hp, size);
705d473c9d5SGleb Smirnoff
706d473c9d5SGleb Smirnoff /* Free previous tag, if any, and assign new one. */
707d473c9d5SGleb Smirnoff if (hip->out != NULL)
7081ede983cSDag-Erling Smørgrav free(hip->out, M_NETGRAPH_TAG);
709d473c9d5SGleb Smirnoff hip->out = hp;
710d473c9d5SGleb Smirnoff
711d473c9d5SGleb Smirnoff /* Fill internal values from API structures. */
712d473c9d5SGleb Smirnoff hip->out_tag_cookie = hip->out->tag_cookie;
713d473c9d5SGleb Smirnoff hip->out_tag_id = hip->out->tag_id;
714d473c9d5SGleb Smirnoff hip->out_tag_len = hip->out->tag_len;
715d473c9d5SGleb Smirnoff hip->out_tag_data = (void*)(hip->out->tag_data);
716d473c9d5SGleb Smirnoff return (0);
717d473c9d5SGleb Smirnoff }
718