xref: /dragonfly/sys/netgraph7/netgraph.h (revision fb151170)
1 /*
2  * netgraph.h
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: Julian Elischer <julian@freebsd.org>
39  *
40  * $FreeBSD: src/sys/netgraph/netgraph.h,v 1.74 2008/06/24 18:49:49 gnn Exp $
41  * $DragonFly: src/sys/netgraph7/netgraph.h,v 1.2 2008/06/26 23:05:35 dillon Exp $
42  * $Whistle: netgraph.h,v 1.29 1999/11/01 07:56:13 julian Exp $
43  */
44 
45 #ifndef _NETGRAPH_NETGRAPH_H_
46 #define _NETGRAPH_NETGRAPH_H_
47 
48 #ifndef _KERNEL
49 #error "This file should not be included in user level programs"
50 #endif
51 
52 #include <sys/queue.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/mutex2.h>
56 #include "dragonfly.h"
57 
58 #ifdef HAVE_KERNEL_OPTION_HEADERS
59 #include "opt_netgraph.h"
60 #endif
61 
62 /* debugging options */
63 #define NG_SEPARATE_MALLOC	/* make modules use their own malloc types */
64 
65 /*
66  * This defines the in-kernel binary interface version.
67  * It is possible to change this but leave the external message
68  * API the same. Each type also has it's own cookies for versioning as well.
69  * Change it for NETGRAPH_DEBUG version so we cannot mix debug and non debug
70  * modules.
71  */
72 #define _NG_ABI_VERSION 12
73 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
74 #define NG_ABI_VERSION	(_NG_ABI_VERSION + 0x10000)
75 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
76 #define NG_ABI_VERSION	_NG_ABI_VERSION
77 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
78 
79 
80 /*
81  * Forward references for the basic structures so we can
82  * define the typedefs and use them in the structures themselves.
83  */
84 struct ng_hook ;
85 struct ng_node ;
86 struct ng_item ;
87 typedef	struct ng_item *item_p;
88 typedef struct ng_node *node_p;
89 typedef struct ng_hook *hook_p;
90 
91 /* node method definitions */
92 typedef	int	ng_constructor_t(node_p node);
93 typedef	int	ng_close_t(node_p node);
94 typedef	int	ng_shutdown_t(node_p node);
95 typedef	int	ng_newhook_t(node_p node, hook_p hook, const char *name);
96 typedef	hook_p	ng_findhook_t(node_p node, const char *name);
97 typedef	int	ng_connect_t(hook_p hook);
98 typedef	int	ng_rcvmsg_t(node_p node, item_p item, hook_p lasthook);
99 typedef	int	ng_rcvdata_t(hook_p hook, item_p item);
100 typedef	int	ng_disconnect_t(hook_p hook);
101 typedef	int	ng_rcvitem (node_p node, hook_p hook, item_p item);
102 
103 /***********************************************************************
104  ***************** Hook Structure and Methods **************************
105  ***********************************************************************
106  *
107  * Structure of a hook
108  */
109 struct ng_hook {
110 	char	hk_name[NG_HOOKSIZ];	/* what this node knows this link as */
111 	void   *hk_private;		/* node dependant ID for this hook */
112 	int	hk_flags;		/* info about this hook/link */
113 	int	hk_type;		/* tbd: hook data link type */
114 	struct	ng_hook *hk_peer;	/* the other end of this link */
115 	struct	ng_node *hk_node;	/* The node this hook is attached to */
116 	LIST_ENTRY(ng_hook) hk_hooks;	/* linked list of all hooks on node */
117 	ng_rcvmsg_t	*hk_rcvmsg;	/* control messages come here */
118 	ng_rcvdata_t	*hk_rcvdata;	/* data comes here */
119 	int	hk_refs;		/* dont actually free this till 0 */
120 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
121 #define HK_MAGIC 0x78573011
122 	int	hk_magic;
123 	char	*lastfile;
124 	int	lastline;
125 	SLIST_ENTRY(ng_hook)	  hk_all;		/* all existing items */
126 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
127 };
128 /* Flags for a hook */
129 #define HK_INVALID		0x0001	/* don't trust it! */
130 #define HK_QUEUE		0x0002	/* queue for later delivery */
131 #define HK_FORCE_WRITER		0x0004	/* Incoming data queued as a writer */
132 #define HK_DEAD			0x0008	/* This is the dead hook.. don't free */
133 #define HK_HI_STACK		0x0010	/* Hook has hi stack usage */
134 
135 /*
136  * Public Methods for hook
137  * If you can't do it with these you probably shouldn;t be doing it.
138  */
139 void ng_unref_hook(hook_p hook); /* don't move this */
140 #define	_NG_HOOK_REF(hook)	atomic_add_int(&(hook)->hk_refs, 1)
141 #define _NG_HOOK_NAME(hook)	((hook)->hk_name)
142 #define _NG_HOOK_UNREF(hook)	ng_unref_hook(hook)
143 #define	_NG_HOOK_SET_PRIVATE(hook, val)	do {(hook)->hk_private = val;} while (0)
144 #define	_NG_HOOK_SET_RCVMSG(hook, val)	do {(hook)->hk_rcvmsg = val;} while (0)
145 #define	_NG_HOOK_SET_RCVDATA(hook, val)	do {(hook)->hk_rcvdata = val;} while (0)
146 #define	_NG_HOOK_PRIVATE(hook)	((hook)->hk_private)
147 #define _NG_HOOK_NOT_VALID(hook)	((hook)->hk_flags & HK_INVALID)
148 #define _NG_HOOK_IS_VALID(hook)	(!((hook)->hk_flags & HK_INVALID))
149 #define _NG_HOOK_NODE(hook)	((hook)->hk_node) /* only rvalue! */
150 #define _NG_HOOK_PEER(hook)	((hook)->hk_peer) /* only rvalue! */
151 #define _NG_HOOK_FORCE_WRITER(hook)				\
152 		do { hook->hk_flags |= HK_FORCE_WRITER; } while (0)
153 #define _NG_HOOK_FORCE_QUEUE(hook) do { hook->hk_flags |= HK_QUEUE; } while (0)
154 #define _NG_HOOK_HI_STACK(hook) do { hook->hk_flags |= HK_HI_STACK; } while (0)
155 
156 /* Some shortcuts */
157 #define NG_PEER_NODE(hook)	NG_HOOK_NODE(NG_HOOK_PEER(hook))
158 #define NG_PEER_HOOK_NAME(hook)	NG_HOOK_NAME(NG_HOOK_PEER(hook))
159 #define NG_PEER_NODE_NAME(hook)	NG_NODE_NAME(NG_PEER_NODE(hook))
160 
161 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
162 #define _NN_ __FILE__,__LINE__
163 void	dumphook (hook_p hook, char *file, int line);
164 static __inline void	_chkhook(hook_p hook, char *file, int line);
165 static __inline void	_ng_hook_ref(hook_p hook, char * file, int line);
166 static __inline char *	_ng_hook_name(hook_p hook, char * file, int line);
167 static __inline void	_ng_hook_unref(hook_p hook, char * file, int line);
168 static __inline void	_ng_hook_set_private(hook_p hook,
169 				void * val, char * file, int line);
170 static __inline void	_ng_hook_set_rcvmsg(hook_p hook,
171 				ng_rcvmsg_t *val, char * file, int line);
172 static __inline void	_ng_hook_set_rcvdata(hook_p hook,
173 				ng_rcvdata_t *val, char * file, int line);
174 static __inline void *	_ng_hook_private(hook_p hook, char * file, int line);
175 static __inline int	_ng_hook_not_valid(hook_p hook, char * file, int line);
176 static __inline int	_ng_hook_is_valid(hook_p hook, char * file, int line);
177 static __inline node_p	_ng_hook_node(hook_p hook, char * file, int line);
178 static __inline hook_p	_ng_hook_peer(hook_p hook, char * file, int line);
179 static __inline void	_ng_hook_force_writer(hook_p hook, char * file,
180 					int line);
181 static __inline void	_ng_hook_force_queue(hook_p hook, char * file, int line);
182 
183 static __inline void
184 _chkhook(hook_p hook, char *file, int line)
185 {
186 	if (hook->hk_magic != HK_MAGIC) {
187 		printf("Accessing freed hook ");
188 		dumphook(hook, file, line);
189 	}
190 	hook->lastline = line;
191 	hook->lastfile = file;
192 }
193 
194 static __inline void
195 _ng_hook_ref(hook_p hook, char * file, int line)
196 {
197 	_chkhook(hook, file, line);
198 	_NG_HOOK_REF(hook);
199 }
200 
201 static __inline char *
202 _ng_hook_name(hook_p hook, char * file, int line)
203 {
204 	_chkhook(hook, file, line);
205 	return (_NG_HOOK_NAME(hook));
206 }
207 
208 static __inline void
209 _ng_hook_unref(hook_p hook, char * file, int line)
210 {
211 	_chkhook(hook, file, line);
212 	_NG_HOOK_UNREF(hook);
213 }
214 
215 static __inline void
216 _ng_hook_set_private(hook_p hook, void *val, char * file, int line)
217 {
218 	_chkhook(hook, file, line);
219 	_NG_HOOK_SET_PRIVATE(hook, val);
220 }
221 
222 static __inline void
223 _ng_hook_set_rcvmsg(hook_p hook, ng_rcvmsg_t *val, char * file, int line)
224 {
225 	_chkhook(hook, file, line);
226 	_NG_HOOK_SET_RCVMSG(hook, val);
227 }
228 
229 static __inline void
230 _ng_hook_set_rcvdata(hook_p hook, ng_rcvdata_t *val, char * file, int line)
231 {
232 	_chkhook(hook, file, line);
233 	_NG_HOOK_SET_RCVDATA(hook, val);
234 }
235 
236 static __inline void *
237 _ng_hook_private(hook_p hook, char * file, int line)
238 {
239 	_chkhook(hook, file, line);
240 	return (_NG_HOOK_PRIVATE(hook));
241 }
242 
243 static __inline int
244 _ng_hook_not_valid(hook_p hook, char * file, int line)
245 {
246 	_chkhook(hook, file, line);
247 	return (_NG_HOOK_NOT_VALID(hook));
248 }
249 
250 static __inline int
251 _ng_hook_is_valid(hook_p hook, char * file, int line)
252 {
253 	_chkhook(hook, file, line);
254 	return (_NG_HOOK_IS_VALID(hook));
255 }
256 
257 static __inline node_p
258 _ng_hook_node(hook_p hook, char * file, int line)
259 {
260 	_chkhook(hook, file, line);
261 	return (_NG_HOOK_NODE(hook));
262 }
263 
264 static __inline hook_p
265 _ng_hook_peer(hook_p hook, char * file, int line)
266 {
267 	_chkhook(hook, file, line);
268 	return (_NG_HOOK_PEER(hook));
269 }
270 
271 static __inline void
272 _ng_hook_force_writer(hook_p hook, char * file, int line)
273 {
274 	_chkhook(hook, file, line);
275 	_NG_HOOK_FORCE_WRITER(hook);
276 }
277 
278 static __inline void
279 _ng_hook_force_queue(hook_p hook, char * file, int line)
280 {
281 	_chkhook(hook, file, line);
282 	_NG_HOOK_FORCE_QUEUE(hook);
283 }
284 
285 static __inline void
286 _ng_hook_hi_stack(hook_p hook, char * file, int line)
287 {
288 	_chkhook(hook, file, line);
289 	_NG_HOOK_HI_STACK(hook);
290 }
291 
292 
293 #define	NG_HOOK_REF(hook)		_ng_hook_ref(hook, _NN_)
294 #define NG_HOOK_NAME(hook)		_ng_hook_name(hook, _NN_)
295 #define NG_HOOK_UNREF(hook)		_ng_hook_unref(hook, _NN_)
296 #define	NG_HOOK_SET_PRIVATE(hook, val)	_ng_hook_set_private(hook, val, _NN_)
297 #define	NG_HOOK_SET_RCVMSG(hook, val)	_ng_hook_set_rcvmsg(hook, val, _NN_)
298 #define	NG_HOOK_SET_RCVDATA(hook, val)	_ng_hook_set_rcvdata(hook, val, _NN_)
299 #define	NG_HOOK_PRIVATE(hook)		_ng_hook_private(hook, _NN_)
300 #define NG_HOOK_NOT_VALID(hook)		_ng_hook_not_valid(hook, _NN_)
301 #define NG_HOOK_IS_VALID(hook)		_ng_hook_is_valid(hook, _NN_)
302 #define NG_HOOK_NODE(hook)		_ng_hook_node(hook, _NN_)
303 #define NG_HOOK_PEER(hook)		_ng_hook_peer(hook, _NN_)
304 #define NG_HOOK_FORCE_WRITER(hook)	_ng_hook_force_writer(hook, _NN_)
305 #define NG_HOOK_FORCE_QUEUE(hook)	_ng_hook_force_queue(hook, _NN_)
306 #define NG_HOOK_HI_STACK(hook)		_ng_hook_hi_stack(hook, _NN_)
307 
308 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
309 
310 #define	NG_HOOK_REF(hook)		_NG_HOOK_REF(hook)
311 #define NG_HOOK_NAME(hook)		_NG_HOOK_NAME(hook)
312 #define NG_HOOK_UNREF(hook)		_NG_HOOK_UNREF(hook)
313 #define	NG_HOOK_SET_PRIVATE(hook, val)	_NG_HOOK_SET_PRIVATE(hook, val)
314 #define	NG_HOOK_SET_RCVMSG(hook, val)	_NG_HOOK_SET_RCVMSG(hook, val)
315 #define	NG_HOOK_SET_RCVDATA(hook, val)	_NG_HOOK_SET_RCVDATA(hook, val)
316 #define	NG_HOOK_PRIVATE(hook)		_NG_HOOK_PRIVATE(hook)
317 #define NG_HOOK_NOT_VALID(hook)		_NG_HOOK_NOT_VALID(hook)
318 #define NG_HOOK_IS_VALID(hook)		_NG_HOOK_IS_VALID(hook)
319 #define NG_HOOK_NODE(hook)		_NG_HOOK_NODE(hook)
320 #define NG_HOOK_PEER(hook)		_NG_HOOK_PEER(hook)
321 #define NG_HOOK_FORCE_WRITER(hook)	_NG_HOOK_FORCE_WRITER(hook)
322 #define NG_HOOK_FORCE_QUEUE(hook)	_NG_HOOK_FORCE_QUEUE(hook)
323 #define NG_HOOK_HI_STACK(hook)		_NG_HOOK_HI_STACK(hook)
324 
325 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
326 
327 /***********************************************************************
328  ***************** Node Structure and Methods **************************
329  ***********************************************************************
330  * Structure of a node
331  * including the eembedded queue structure.
332  *
333  * The structure for queueing Netgraph request items
334  * embedded in the node structure
335  */
336 struct ng_queue {
337 	u_int		q_flags;	/* Current r/w/q lock flags */
338 	u_int		q_flags2;	/* Other queue flags */
339 	struct mtx	q_mtx;
340 	STAILQ_ENTRY(ng_node)	q_work;	/* nodes with work to do */
341 	STAILQ_HEAD(, ng_item)	queue;	/* actually items queue */
342 };
343 
344 struct ng_node {
345 	char	nd_name[NG_NODESIZ];	/* optional globally unique name */
346 	struct	ng_type *nd_type;	/* the installed 'type' */
347 	int	nd_flags;		/* see below for bit definitions */
348 	int	nd_numhooks;		/* number of hooks */
349 	void   *nd_private;		/* node type dependant node ID */
350 	ng_ID_t	nd_ID;			/* Unique per node */
351 	LIST_HEAD(hooks, ng_hook) nd_hooks;	/* linked list of node hooks */
352 	LIST_ENTRY(ng_node)	  nd_nodes;	/* linked list of all nodes */
353 	LIST_ENTRY(ng_node)	  nd_idnodes;	/* ID hash collision list */
354 	struct	ng_queue	  nd_input_queue; /* input queue for locking */
355 	int	nd_refs;		/* # of references to this node */
356 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
357 #define ND_MAGIC 0x59264837
358 	int	nd_magic;
359 	char	*lastfile;
360 	int	lastline;
361 	SLIST_ENTRY(ng_node)	  nd_all;	/* all existing nodes */
362 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
363 };
364 
365 /* Flags for a node */
366 #define NGF_INVALID	0x00000001	/* free when refs go to 0 */
367 #define NG_INVALID	NGF_INVALID	/* compat for old code */
368 #define NGF_FORCE_WRITER	0x00000004	/* Never multithread this node */
369 #define NG_FORCE_WRITER	NGF_FORCE_WRITER /* compat for old code */
370 #define NGF_CLOSING	0x00000008	/* ng_rmnode() at work */
371 #define NG_CLOSING	NGF_CLOSING	/* compat for old code */
372 #define NGF_REALLY_DIE	0x00000010	/* "persistent" node is unloading */
373 #define NG_REALLY_DIE	NGF_REALLY_DIE	/* compat for old code */
374 #define NGF_HI_STACK	0x00000020	/* node has hi stack usage */
375 #define NGF_TYPE1	0x10000000	/* reserved for type specific storage */
376 #define NGF_TYPE2	0x20000000	/* reserved for type specific storage */
377 #define NGF_TYPE3	0x40000000	/* reserved for type specific storage */
378 #define NGF_TYPE4	0x80000000	/* reserved for type specific storage */
379 
380 /*
381  * Public methods for nodes.
382  * If you can't do it with these you probably shouldn't be doing it.
383  */
384 int	ng_unref_node(node_p node); /* don't move this */
385 #define _NG_NODE_NAME(node)	((node)->nd_name + 0)
386 #define _NG_NODE_HAS_NAME(node)	((node)->nd_name[0] + 0)
387 #define _NG_NODE_ID(node)	((node)->nd_ID + 0)
388 #define	_NG_NODE_REF(node)	atomic_add_int(&(node)->nd_refs, 1)
389 #define	_NG_NODE_UNREF(node)	ng_unref_node(node)
390 #define	_NG_NODE_SET_PRIVATE(node, val)	do {(node)->nd_private = val;} while (0)
391 #define	_NG_NODE_PRIVATE(node)	((node)->nd_private)
392 #define _NG_NODE_IS_VALID(node)	(!((node)->nd_flags & NGF_INVALID))
393 #define _NG_NODE_NOT_VALID(node)	((node)->nd_flags & NGF_INVALID)
394 #define _NG_NODE_NUMHOOKS(node)	((node)->nd_numhooks + 0) /* rvalue */
395 #define _NG_NODE_FORCE_WRITER(node)					\
396 	do{ node->nd_flags |= NGF_FORCE_WRITER; }while (0)
397 #define _NG_NODE_HI_STACK(node)						\
398 	do{ node->nd_flags |= NGF_HI_STACK; }while (0)
399 #define _NG_NODE_REALLY_DIE(node)					\
400 	do{ node->nd_flags |= (NGF_REALLY_DIE|NGF_INVALID); }while (0)
401 #define _NG_NODE_REVIVE(node) \
402 	do { node->nd_flags &= ~NGF_INVALID; } while (0)
403 /*
404  * The hook iterator.
405  * This macro will call a function of type ng_fn_eachhook for each
406  * hook attached to the node. If the function returns 0, then the
407  * iterator will stop and return a pointer to the hook that returned 0.
408  */
409 typedef	int	ng_fn_eachhook(hook_p hook, void* arg);
410 #define _NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)			\
411 	do {								\
412 		hook_p _hook;						\
413 		(rethook) = NULL;					\
414 		LIST_FOREACH(_hook, &((node)->nd_hooks), hk_hooks) {	\
415 			if ((fn)(_hook, arg) == 0) {			\
416 				(rethook) = _hook;			\
417 				break;					\
418 			}						\
419 		}							\
420 	} while (0)
421 
422 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
423 void	dumpnode(node_p node, char *file, int line);
424 static __inline void _chknode(node_p node, char *file, int line);
425 static __inline char * _ng_node_name(node_p node, char *file, int line);
426 static __inline int _ng_node_has_name(node_p node, char *file, int line);
427 static __inline ng_ID_t _ng_node_id(node_p node, char *file, int line);
428 static __inline void _ng_node_ref(node_p node, char *file, int line);
429 static __inline int _ng_node_unref(node_p node, char *file, int line);
430 static __inline void _ng_node_set_private(node_p node, void * val,
431 							char *file, int line);
432 static __inline void * _ng_node_private(node_p node, char *file, int line);
433 static __inline int _ng_node_is_valid(node_p node, char *file, int line);
434 static __inline int _ng_node_not_valid(node_p node, char *file, int line);
435 static __inline int _ng_node_numhooks(node_p node, char *file, int line);
436 static __inline void _ng_node_force_writer(node_p node, char *file, int line);
437 static __inline hook_p _ng_node_foreach_hook(node_p node,
438 			ng_fn_eachhook *fn, void *arg, char *file, int line);
439 static __inline void _ng_node_revive(node_p node, char *file, int line);
440 
441 static __inline void
442 _chknode(node_p node, char *file, int line)
443 {
444 	if (node->nd_magic != ND_MAGIC) {
445 		printf("Accessing freed node ");
446 		dumpnode(node, file, line);
447 	}
448 	node->lastline = line;
449 	node->lastfile = file;
450 }
451 
452 static __inline char *
453 _ng_node_name(node_p node, char *file, int line)
454 {
455 	_chknode(node, file, line);
456 	return(_NG_NODE_NAME(node));
457 }
458 
459 static __inline int
460 _ng_node_has_name(node_p node, char *file, int line)
461 {
462 	_chknode(node, file, line);
463 	return(_NG_NODE_HAS_NAME(node));
464 }
465 
466 static __inline ng_ID_t
467 _ng_node_id(node_p node, char *file, int line)
468 {
469 	_chknode(node, file, line);
470 	return(_NG_NODE_ID(node));
471 }
472 
473 static __inline void
474 _ng_node_ref(node_p node, char *file, int line)
475 {
476 	_chknode(node, file, line);
477 	_NG_NODE_REF(node);
478 }
479 
480 static __inline int
481 _ng_node_unref(node_p node, char *file, int line)
482 {
483 	_chknode(node, file, line);
484 	return (_NG_NODE_UNREF(node));
485 }
486 
487 static __inline void
488 _ng_node_set_private(node_p node, void * val, char *file, int line)
489 {
490 	_chknode(node, file, line);
491 	_NG_NODE_SET_PRIVATE(node, val);
492 }
493 
494 static __inline void *
495 _ng_node_private(node_p node, char *file, int line)
496 {
497 	_chknode(node, file, line);
498 	return (_NG_NODE_PRIVATE(node));
499 }
500 
501 static __inline int
502 _ng_node_is_valid(node_p node, char *file, int line)
503 {
504 	_chknode(node, file, line);
505 	return(_NG_NODE_IS_VALID(node));
506 }
507 
508 static __inline int
509 _ng_node_not_valid(node_p node, char *file, int line)
510 {
511 	_chknode(node, file, line);
512 	return(_NG_NODE_NOT_VALID(node));
513 }
514 
515 static __inline int
516 _ng_node_numhooks(node_p node, char *file, int line)
517 {
518 	_chknode(node, file, line);
519 	return(_NG_NODE_NUMHOOKS(node));
520 }
521 
522 static __inline void
523 _ng_node_force_writer(node_p node, char *file, int line)
524 {
525 	_chknode(node, file, line);
526 	_NG_NODE_FORCE_WRITER(node);
527 }
528 
529 static __inline void
530 _ng_node_hi_stack(node_p node, char *file, int line)
531 {
532 	_chknode(node, file, line);
533 	_NG_NODE_HI_STACK(node);
534 }
535 
536 static __inline void
537 _ng_node_really_die(node_p node, char *file, int line)
538 {
539 	_chknode(node, file, line);
540 	_NG_NODE_REALLY_DIE(node);
541 }
542 
543 static __inline void
544 _ng_node_revive(node_p node, char *file, int line)
545 {
546 	_chknode(node, file, line);
547 	_NG_NODE_REVIVE(node);
548 }
549 
550 static __inline hook_p
551 _ng_node_foreach_hook(node_p node, ng_fn_eachhook *fn, void *arg,
552 						char *file, int line)
553 {
554 	hook_p hook;
555 	_chknode(node, file, line);
556 	_NG_NODE_FOREACH_HOOK(node, fn, arg, hook);
557 	return (hook);
558 }
559 
560 #define NG_NODE_NAME(node)		_ng_node_name(node, _NN_)
561 #define NG_NODE_HAS_NAME(node)		_ng_node_has_name(node, _NN_)
562 #define NG_NODE_ID(node)		_ng_node_id(node, _NN_)
563 #define NG_NODE_REF(node)		_ng_node_ref(node, _NN_)
564 #define	NG_NODE_UNREF(node)		_ng_node_unref(node, _NN_)
565 #define	NG_NODE_SET_PRIVATE(node, val)	_ng_node_set_private(node, val, _NN_)
566 #define	NG_NODE_PRIVATE(node)		_ng_node_private(node, _NN_)
567 #define NG_NODE_IS_VALID(node)		_ng_node_is_valid(node, _NN_)
568 #define NG_NODE_NOT_VALID(node)		_ng_node_not_valid(node, _NN_)
569 #define NG_NODE_FORCE_WRITER(node) 	_ng_node_force_writer(node, _NN_)
570 #define NG_NODE_HI_STACK(node) 		_ng_node_hi_stack(node, _NN_)
571 #define NG_NODE_REALLY_DIE(node) 	_ng_node_really_die(node, _NN_)
572 #define NG_NODE_NUMHOOKS(node)		_ng_node_numhooks(node, _NN_)
573 #define NG_NODE_REVIVE(node)		_ng_node_revive(node, _NN_)
574 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)			      \
575 	do {								      \
576 		rethook = _ng_node_foreach_hook(node, fn, (void *)arg, _NN_); \
577 	} while (0)
578 
579 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
580 
581 #define NG_NODE_NAME(node)		_NG_NODE_NAME(node)
582 #define NG_NODE_HAS_NAME(node)		_NG_NODE_HAS_NAME(node)
583 #define NG_NODE_ID(node)		_NG_NODE_ID(node)
584 #define	NG_NODE_REF(node)		_NG_NODE_REF(node)
585 #define	NG_NODE_UNREF(node)		_NG_NODE_UNREF(node)
586 #define	NG_NODE_SET_PRIVATE(node, val)	_NG_NODE_SET_PRIVATE(node, val)
587 #define	NG_NODE_PRIVATE(node)		_NG_NODE_PRIVATE(node)
588 #define NG_NODE_IS_VALID(node)		_NG_NODE_IS_VALID(node)
589 #define NG_NODE_NOT_VALID(node)		_NG_NODE_NOT_VALID(node)
590 #define NG_NODE_FORCE_WRITER(node) 	_NG_NODE_FORCE_WRITER(node)
591 #define NG_NODE_HI_STACK(node) 		_NG_NODE_HI_STACK(node)
592 #define NG_NODE_REALLY_DIE(node) 	_NG_NODE_REALLY_DIE(node)
593 #define NG_NODE_NUMHOOKS(node)		_NG_NODE_NUMHOOKS(node)
594 #define NG_NODE_REVIVE(node)		_NG_NODE_REVIVE(node)
595 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)			\
596 		_NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)
597 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
598 
599 /***********************************************************************
600  ************* Node Queue and Item Structures and Methods **************
601  ***********************************************************************
602  *
603  */
604 typedef	void	ng_item_fn(node_p node, hook_p hook, void *arg1, int arg2);
605 typedef	int	ng_item_fn2(node_p node, struct ng_item *item, hook_p hook);
606 typedef	void	ng_apply_t(void *context, int error);
607 struct ng_apply_info {
608 	ng_apply_t	*apply;
609 	void		*context;
610 	int		refs;
611 	int		error;
612 };
613 struct ng_item {
614 	u_long	el_flags;
615 	STAILQ_ENTRY(ng_item)	el_next;
616 	node_p	el_dest; /* The node it will be applied against (or NULL) */
617 	hook_p	el_hook; /* Entering hook. Optional in Control messages */
618 	union {
619 		struct mbuf	*da_m;
620 		struct {
621 			struct ng_mesg	*msg_msg;
622 			ng_ID_t		msg_retaddr;
623 		} msg;
624 		struct {
625 			union {
626 				ng_item_fn	*fn_fn;
627 				ng_item_fn2	*fn_fn2;
628 			} fn_fn;
629 			void 		*fn_arg1;
630 			int		fn_arg2;
631 		} fn;
632 	} body;
633 	/*
634 	 * Optional callback called when item is being applied,
635 	 * and its context.
636 	 */
637 	struct ng_apply_info	*apply;
638 	u_int	depth;
639 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
640 	char *lastfile;
641 	int  lastline;
642 	TAILQ_ENTRY(ng_item)	  all;		/* all existing items */
643 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
644 };
645 
646 #define NGQF_TYPE	0x03		/* MASK of content definition */
647 #define NGQF_MESG	0x00		/* the queue element is a message */
648 #define NGQF_DATA	0x01		/* the queue element is data */
649 #define NGQF_FN		0x02		/* the queue element is a function */
650 #define NGQF_FN2	0x03		/* the queue element is a new function */
651 
652 #define NGQF_RW		0x04		/* MASK for wanted queue mode */
653 #define NGQF_READER	0x04		/* wants to be a reader */
654 #define NGQF_WRITER	0x00		/* wants to be a writer */
655 
656 #define NGQF_QMODE	0x08		/* MASK for how it was queued */
657 #define NGQF_QREADER	0x08		/* was queued as a reader */
658 #define NGQF_QWRITER	0x00		/* was queued as a writer */
659 
660 /*
661  * Get the mbuf (etc) out of an item.
662  * Sets the value in the item to NULL in case we need to call NG_FREE_ITEM()
663  * with it, (to avoid freeing the things twice).
664  * If you don't want to zero out the item then realise that the
665  * item still owns it.
666  * Retaddr is different. There are no references on that. It's just a number.
667  * The debug versions must be either all used everywhere or not at all.
668  */
669 
670 #define _NGI_M(i) ((i)->body.da_m)
671 #define _NGI_MSG(i) ((i)->body.msg.msg_msg)
672 #define _NGI_RETADDR(i) ((i)->body.msg.msg_retaddr)
673 #define	_NGI_FN(i) ((i)->body.fn.fn_fn.fn_fn)
674 #define	_NGI_FN2(i) ((i)->body.fn.fn_fn.fn_fn2)
675 #define	_NGI_ARG1(i) ((i)->body.fn.fn_arg1)
676 #define	_NGI_ARG2(i) ((i)->body.fn.fn_arg2)
677 #define	_NGI_NODE(i) ((i)->el_dest)
678 #define	_NGI_HOOK(i) ((i)->el_hook)
679 #define	_NGI_SET_HOOK(i,h) do { _NGI_HOOK(i) = h; h = NULL;} while (0)
680 #define	_NGI_CLR_HOOK(i)   do {						\
681 		hook_p _hook = _NGI_HOOK(i);				\
682 		if (_hook) {						\
683 			_NG_HOOK_UNREF(_hook);				\
684 			_NGI_HOOK(i) = NULL;				\
685 		}							\
686 	} while (0)
687 #define	_NGI_SET_NODE(i,n) do { _NGI_NODE(i) = n; n = NULL;} while (0)
688 #define	_NGI_CLR_NODE(i)   do {						\
689 		node_p _node = _NGI_NODE(i);				\
690 		if (_node) {						\
691 			_NG_NODE_UNREF(_node);				\
692 			_NGI_NODE(i) = NULL;				\
693 		}							\
694 	} while (0)
695 
696 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
697 void				dumpitem(item_p item, char *file, int line);
698 static __inline void		_ngi_check(item_p item, char *file, int line) ;
699 static __inline struct mbuf **	_ngi_m(item_p item, char *file, int line) ;
700 static __inline ng_ID_t *	_ngi_retaddr(item_p item, char *file, int line);
701 static __inline struct ng_mesg ** _ngi_msg(item_p item, char *file, int line) ;
702 static __inline ng_item_fn **	_ngi_fn(item_p item, char *file, int line) ;
703 static __inline ng_item_fn2 **	_ngi_fn2(item_p item, char *file, int line) ;
704 static __inline void **		_ngi_arg1(item_p item, char *file, int line) ;
705 static __inline int *		_ngi_arg2(item_p item, char *file, int line) ;
706 static __inline node_p		_ngi_node(item_p item, char *file, int line);
707 static __inline hook_p		_ngi_hook(item_p item, char *file, int line);
708 
709 static __inline void
710 _ngi_check(item_p item, char *file, int line)
711 {
712 	(item)->lastline = line;
713 	(item)->lastfile = file;
714 }
715 
716 static __inline struct mbuf **
717 _ngi_m(item_p item, char *file, int line)
718 {
719 	_ngi_check(item, file, line);
720 	return (&_NGI_M(item));
721 }
722 
723 static __inline struct ng_mesg **
724 _ngi_msg(item_p item, char *file, int line)
725 {
726 	_ngi_check(item, file, line);
727 	return (&_NGI_MSG(item));
728 }
729 
730 static __inline ng_ID_t *
731 _ngi_retaddr(item_p item, char *file, int line)
732 {
733 	_ngi_check(item, file, line);
734 	return (&_NGI_RETADDR(item));
735 }
736 
737 static __inline ng_item_fn **
738 _ngi_fn(item_p item, char *file, int line)
739 {
740 	_ngi_check(item, file, line);
741 	return (&_NGI_FN(item));
742 }
743 
744 static __inline ng_item_fn2 **
745 _ngi_fn2(item_p item, char *file, int line)
746 {
747 	_ngi_check(item, file, line);
748 	return (&_NGI_FN2(item));
749 }
750 
751 static __inline void **
752 _ngi_arg1(item_p item, char *file, int line)
753 {
754 	_ngi_check(item, file, line);
755 	return (&_NGI_ARG1(item));
756 }
757 
758 static __inline int *
759 _ngi_arg2(item_p item, char *file, int line)
760 {
761 	_ngi_check(item, file, line);
762 	return (&_NGI_ARG2(item));
763 }
764 
765 static __inline node_p
766 _ngi_node(item_p item, char *file, int line)
767 {
768 	_ngi_check(item, file, line);
769 	return (_NGI_NODE(item));
770 }
771 
772 static __inline hook_p
773 _ngi_hook(item_p item, char *file, int line)
774 {
775 	_ngi_check(item, file, line);
776 	return (_NGI_HOOK(item));
777 }
778 
779 #define NGI_M(i)	(*_ngi_m(i, _NN_))
780 #define NGI_MSG(i)	(*_ngi_msg(i, _NN_))
781 #define NGI_RETADDR(i)	(*_ngi_retaddr(i, _NN_))
782 #define NGI_FN(i)	(*_ngi_fn(i, _NN_))
783 #define NGI_FN2(i)	(*_ngi_fn2(i, _NN_))
784 #define NGI_ARG1(i)	(*_ngi_arg1(i, _NN_))
785 #define NGI_ARG2(i)	(*_ngi_arg2(i, _NN_))
786 #define NGI_HOOK(i)	_ngi_hook(i, _NN_)
787 #define NGI_NODE(i)	_ngi_node(i, _NN_)
788 #define	NGI_SET_HOOK(i,h)						\
789 	do { _ngi_check(i, _NN_); _NGI_SET_HOOK(i, h); } while (0)
790 #define	NGI_CLR_HOOK(i)							\
791 	do { _ngi_check(i, _NN_); _NGI_CLR_HOOK(i); } while (0)
792 #define	NGI_SET_NODE(i,n)						\
793 	do { _ngi_check(i, _NN_); _NGI_SET_NODE(i, n); } while (0)
794 #define	NGI_CLR_NODE(i)							\
795 	do { _ngi_check(i, _NN_); _NGI_CLR_NODE(i); } while (0)
796 
797 #define NG_FREE_ITEM(item)						\
798 	do {								\
799 		_ngi_check(item, _NN_);					\
800 		ng_free_item((item));					\
801 	} while (0)
802 
803 #define	SAVE_LINE(item)							\
804 	do {								\
805 		(item)->lastline = __LINE__;				\
806 		(item)->lastfile = __FILE__;				\
807 	} while (0)
808 
809 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
810 
811 #define NGI_M(i)	_NGI_M(i)
812 #define NGI_MSG(i)	_NGI_MSG(i)
813 #define NGI_RETADDR(i)	_NGI_RETADDR(i)
814 #define NGI_FN(i)	_NGI_FN(i)
815 #define NGI_FN2(i)	_NGI_FN2(i)
816 #define NGI_ARG1(i)	_NGI_ARG1(i)
817 #define NGI_ARG2(i)	_NGI_ARG2(i)
818 #define	NGI_NODE(i)	_NGI_NODE(i)
819 #define	NGI_HOOK(i)	_NGI_HOOK(i)
820 #define	NGI_SET_HOOK(i,h) _NGI_SET_HOOK(i,h)
821 #define	NGI_CLR_HOOK(i)	  _NGI_CLR_HOOK(i)
822 #define	NGI_SET_NODE(i,n) _NGI_SET_NODE(i,n)
823 #define	NGI_CLR_NODE(i)	  _NGI_CLR_NODE(i)
824 
825 #define	NG_FREE_ITEM(item)	ng_free_item((item))
826 #define	SAVE_LINE(item)		do {} while (0)
827 
828 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
829 
830 #define NGI_GET_M(i,m)							\
831 	do {								\
832 		(m) = NGI_M(i);						\
833 		_NGI_M(i) = NULL;					\
834 	} while (0)
835 
836 #define NGI_GET_MSG(i,m)						\
837 	do {								\
838 		(m) = NGI_MSG(i);					\
839 		_NGI_MSG(i) = NULL;					\
840 	} while (0)
841 
842 #define NGI_GET_NODE(i,n)	/* YOU NOW HAVE THE REFERENCE */	\
843 	do {								\
844 		(n) = NGI_NODE(i);					\
845 		_NGI_NODE(i) = NULL;					\
846 	} while (0)
847 
848 #define NGI_GET_HOOK(i,h)						\
849 	do {								\
850 		(h) = NGI_HOOK(i);					\
851 		_NGI_HOOK(i) = NULL;					\
852 	} while (0)
853 
854 #define NGI_SET_WRITER(i)	((i)->el_flags &= ~NGQF_QMODE)
855 #define NGI_SET_READER(i)	((i)->el_flags |= NGQF_QREADER)
856 
857 #define NGI_QUEUED_READER(i)	((i)->el_flags & NGQF_QREADER)
858 #define NGI_QUEUED_WRITER(i)	(((i)->el_flags & NGQF_QMODE) == NGQF_QWRITER)
859 
860 /**********************************************************************
861 * Data macros.  Send, manipulate and free.
862 **********************************************************************/
863 /*
864  * Assuming the data is already ok, just set the new address and send
865  */
866 #define NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, flags)		\
867 	do {								\
868 		(error) =						\
869 		    ng_address_hook(NULL, (item), (hook), NG_NOFLAGS);	\
870 		if (error == 0) {					\
871 			SAVE_LINE(item);				\
872 			(error) = ng_snd_item((item), (flags));		\
873 		}							\
874 		(item) = NULL;						\
875 	} while (0)
876 #define	NG_FWD_ITEM_HOOK(error, item, hook)	\
877 		NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, NG_NOFLAGS)
878 
879 /*
880  * Forward a data packet. Mbuf pointer is updated to new value. We
881  * presume you dealt with the old one when you update it to the new one
882  * (or it maybe the old one). We got a packet and possibly had to modify
883  * the mbuf. You should probably use NGI_GET_M() if you are going to use
884  * this too.
885  */
886 #define NG_FWD_NEW_DATA_FLAGS(error, item, hook, m, flags)		\
887 	do {								\
888 		NGI_M(item) = (m);					\
889 		(m) = NULL;						\
890 		NG_FWD_ITEM_HOOK_FLAGS(error, item, hook, flags);	\
891 	} while (0)
892 #define	NG_FWD_NEW_DATA(error, item, hook, m)	\
893 		NG_FWD_NEW_DATA_FLAGS(error, item, hook, m, NG_NOFLAGS)
894 
895 /* Send a previously unpackaged mbuf. XXX: This should be called
896  * NG_SEND_DATA in future, but this name is kept for compatibility
897  * reasons.
898  */
899 #define NG_SEND_DATA_FLAGS(error, hook, m, flags)			\
900 	do {								\
901 		item_p _item;						\
902 		if ((_item = ng_package_data((m), flags))) {		\
903 			NG_FWD_ITEM_HOOK_FLAGS(error, _item, hook, flags);\
904 		} else {						\
905 			(error) = ENOMEM;				\
906 		}							\
907 		(m) = NULL;						\
908 	} while (0)
909 
910 #define NG_SEND_DATA_ONLY(error, hook, m)	\
911 		NG_SEND_DATA_FLAGS(error, hook, m, NG_NOFLAGS)
912 /* NG_SEND_DATA() compat for meta-data times */
913 #define	NG_SEND_DATA(error, hook, m, x)	\
914 		NG_SEND_DATA_FLAGS(error, hook, m, NG_NOFLAGS)
915 
916 #define NG_FREE_MSG(msg)						\
917 	do {								\
918 		if ((msg)) {						\
919 			kfree((msg), M_NETGRAPH_MSG);			\
920 			(msg) = NULL;					\
921 		}	 						\
922 	} while (0)
923 
924 #define NG_FREE_M(m)							\
925 	do {								\
926 		if ((m)) {						\
927 			m_freem((m));					\
928 			(m) = NULL;					\
929 		}							\
930 	} while (0)
931 
932 /*****************************************
933 * Message macros
934 *****************************************/
935 
936 #define NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr)		\
937 	do {								\
938 		item_p _item;						\
939 		if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
940 			(msg) = NULL;					\
941 			(error) = ENOMEM;				\
942 			break;						\
943 		}							\
944 		if (((error) = ng_address_hook((here), (_item),		\
945 					(hook), (retaddr))) == 0) {	\
946 			SAVE_LINE(_item);				\
947 			(error) = ng_snd_item((_item), 0);		\
948 		}							\
949 		(msg) = NULL;						\
950 	} while (0)
951 
952 #define NG_SEND_MSG_PATH(error, here, msg, path, retaddr)		\
953 	do {								\
954 		item_p _item;						\
955 		if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
956 			(msg) = NULL;					\
957 			(error) = ENOMEM;				\
958 			break;						\
959 		}							\
960 		if (((error) = ng_address_path((here), (_item),		\
961 					(path), (retaddr))) == 0) {	\
962 			SAVE_LINE(_item);				\
963 			(error) = ng_snd_item((_item), 0);		\
964 		}							\
965 		(msg) = NULL;						\
966 	} while (0)
967 
968 #define NG_SEND_MSG_ID(error, here, msg, ID, retaddr)			\
969 	do {								\
970 		item_p _item;						\
971 		if ((_item = ng_package_msg(msg, NG_NOFLAGS)) == NULL) {\
972 			(msg) = NULL;					\
973 			(error) = ENOMEM;				\
974 			break;						\
975 		}							\
976 		if (((error) = ng_address_ID((here), (_item),		\
977 					(ID), (retaddr))) == 0) {	\
978 			SAVE_LINE(_item);				\
979 			(error) = ng_snd_item((_item), 0);		\
980 		}							\
981 		(msg) = NULL;						\
982 	} while (0)
983 
984 /*
985  * Redirect the message to the next hop using the given hook.
986  * ng_retarget_msg() frees the item if there is an error
987  * and returns an error code.  It returns 0 on success.
988  */
989 #define NG_FWD_MSG_HOOK(error, here, item, hook, retaddr)		\
990 	do {								\
991 		if (((error) = ng_address_hook((here), (item),		\
992 					(hook), (retaddr))) == 0) {	\
993 			SAVE_LINE(item);				\
994 			(error) = ng_snd_item((item), 0);		\
995 		}							\
996 		(item) = NULL;						\
997 	} while (0)
998 
999 /*
1000  * Send a queue item back to it's originator with a response message.
1001  * Assume original message was removed and freed separatly.
1002  */
1003 #define NG_RESPOND_MSG(error, here, item, resp)				\
1004 	do {								\
1005 		if (resp) {						\
1006 			ng_ID_t _dest = NGI_RETADDR(item);		\
1007 			NGI_RETADDR(item) = 0;				\
1008 			NGI_MSG(item) = resp;				\
1009 			if ((error = ng_address_ID((here), (item),	\
1010 					_dest, 0)) == 0) {		\
1011 				SAVE_LINE(item);			\
1012 				(error) = ng_snd_item((item), NG_QUEUE);\
1013 			}						\
1014 		} else							\
1015 			NG_FREE_ITEM(item);				\
1016 		(item) = NULL;						\
1017 	} while (0)
1018 
1019 
1020 /***********************************************************************
1021  ******** Structures Definitions and Macros for defining a node  *******
1022  ***********************************************************************
1023  *
1024  * Here we define the structures needed to actually define a new node
1025  * type.
1026  */
1027 
1028 /*
1029  * Command list -- each node type specifies the command that it knows
1030  * how to convert between ASCII and binary using an array of these.
1031  * The last element in the array must be a terminator with cookie=0.
1032  */
1033 
1034 struct ng_cmdlist {
1035 	u_int32_t			cookie;		/* command typecookie */
1036 	int				cmd;		/* command number */
1037 	const char			*name;		/* command name */
1038 	const struct ng_parse_type	*mesgType;	/* args if !NGF_RESP */
1039 	const struct ng_parse_type	*respType;	/* args if NGF_RESP */
1040 };
1041 
1042 /*
1043  * Structure of a node type
1044  * If data is sent to the "rcvdata()" entrypoint then the system
1045  * may decide to defer it until later by queing it with the normal netgraph
1046  * input queuing system.  This is decidde by the HK_QUEUE flag being set in
1047  * the flags word of the peer (receiving) hook. The dequeuing mechanism will
1048  * ensure it is not requeued again.
1049  * Note the input queueing system is to allow modules
1050  * to 'release the stack' or to pass data across spl layers.
1051  * The data will be redelivered as soon as the NETISR code runs
1052  * which may be almost immediatly.  A node may also do it's own queueing
1053  * for other reasons (e.g. device output queuing).
1054  */
1055 struct ng_type {
1056 
1057 	u_int32_t	version; 	/* must equal NG_API_VERSION */
1058 	const char	*name;		/* Unique type name */
1059 	modeventhand_t	mod_event;	/* Module event handler (optional) */
1060 	ng_constructor_t *constructor;	/* Node constructor */
1061 	ng_rcvmsg_t	*rcvmsg;	/* control messages come here */
1062 	ng_close_t	*close;		/* warn about forthcoming shutdown */
1063 	ng_shutdown_t	*shutdown;	/* reset, and free resources */
1064 	ng_newhook_t	*newhook;	/* first notification of new hook */
1065 	ng_findhook_t	*findhook;	/* only if you have lots of hooks */
1066 	ng_connect_t	*connect;	/* final notification of new hook */
1067 	ng_rcvdata_t	*rcvdata;	/* data comes here */
1068 	ng_disconnect_t	*disconnect;	/* notify on disconnect */
1069 
1070 	const struct	ng_cmdlist *cmdlist;	/* commands we can convert */
1071 
1072 	/* R/W data private to the base netgraph code DON'T TOUCH! */
1073 	LIST_ENTRY(ng_type) types;		/* linked list of all types */
1074 	int		    refs;		/* number of instances */
1075 };
1076 
1077 /*
1078  * Use the NETGRAPH_INIT() macro to link a node type into the
1079  * netgraph system. This works for types compiled into the kernel
1080  * as well as KLD modules. The first argument should be the type
1081  * name (eg, echo) and the second a pointer to the type struct.
1082  *
1083  * If a different link time is desired, e.g., a device driver that
1084  * needs to install its netgraph type before probing, use the
1085  * NETGRAPH_INIT_ORDERED() macro instead.  Device drivers probably
1086  * want to use SI_SUB_DRIVERS/SI_ORDER_FIRST.
1087  */
1088 
1089 #define NETGRAPH_INIT_ORDERED(typename, typestructp, sub, order)	\
1090 static moduledata_t ng_##typename##_mod = {				\
1091 	"ng_" #typename,						\
1092 	ng_mod_event,							\
1093 	(typestructp)							\
1094 };									\
1095 DECLARE_MODULE(ng_##typename, ng_##typename##_mod, sub, order);		\
1096 MODULE_DEPEND(ng_##typename, netgraph,	NG_ABI_VERSION,			\
1097 					NG_ABI_VERSION,			\
1098 					NG_ABI_VERSION)
1099 
1100 #define NETGRAPH_INIT(tn, tp)						\
1101 	NETGRAPH_INIT_ORDERED(tn, tp, SI_SUB_PSEUDO, SI_ORDER_ANY)
1102 
1103 /* Special malloc() type for netgraph structs and ctrl messages */
1104 /* Only these two types should be visible to nodes */
1105 MALLOC_DECLARE(M_NETGRAPH);
1106 MALLOC_DECLARE(M_NETGRAPH_MSG);
1107 
1108 /* declare the base of the netgraph sysclt hierarchy */
1109 /* but only if this file cares about sysctls */
1110 #ifdef	SYSCTL_DECL
1111 SYSCTL_DECL(_net_graph);
1112 #endif
1113 
1114 /*
1115  * Methods that the nodes can use.
1116  * Many of these methods should usually NOT be used directly but via
1117  * Macros above.
1118  */
1119 int	ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr);
1120 int	ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr);
1121 int	ng_address_path(node_p here, item_p item, char *address, ng_ID_t raddr);
1122 int	ng_bypass(hook_p hook1, hook_p hook2);
1123 hook_p	ng_findhook(node_p node, const char *name);
1124 struct	ng_type *ng_findtype(const char *type);
1125 int	ng_make_node_common(struct ng_type *typep, node_p *nodep);
1126 int	ng_name_node(node_p node, const char *name);
1127 int	ng_newtype(struct ng_type *tp);
1128 ng_ID_t ng_node2ID(node_p node);
1129 item_p	ng_package_data(struct mbuf *m, int flags);
1130 item_p	ng_package_msg(struct ng_mesg *msg, int flags);
1131 item_p	ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg);
1132 void	ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr);
1133 int	ng_rmhook_self(hook_p hook);	/* if a node wants to kill a hook */
1134 int	ng_rmnode_self(node_p here);	/* if a node wants to suicide */
1135 int	ng_rmtype(struct ng_type *tp);
1136 int	ng_snd_item(item_p item, int queue);
1137 int 	ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn, void *arg1,
1138 	int arg2);
1139 int 	ng_send_fn1(node_p node, hook_p hook, ng_item_fn *fn, void *arg1,
1140 	int arg2, int flags);
1141 int 	ng_send_fn2(node_p node, hook_p hook, item_p pitem, ng_item_fn2 *fn,
1142 	void *arg1, int arg2, int flags);
1143 int	ng_uncallout(struct callout *c, node_p node);
1144 int	ng_callout(struct callout *c, node_p node, hook_p hook, int ticks,
1145 	    ng_item_fn *fn, void * arg1, int arg2);
1146 #define	ng_callout_init(c)	callout_init(c)
1147 
1148 /* Flags for netgraph functions. */
1149 #define	NG_NOFLAGS	0x00000000	/* no special options */
1150 #define	NG_QUEUE	0x00000001	/* enqueue item, don't dispatch */
1151 #define	NG_WAITOK	0x00000002	/* use M_WAITOK, etc. */
1152 /* XXXGL: NG_PROGRESS unused since ng_base.c rev. 1.136. Should be deleted? */
1153 #define	NG_PROGRESS	0x00000004	/* return EINPROGRESS if queued */
1154 #define	NG_REUSE_ITEM	0x00000008	/* supplied item should be reused */
1155 
1156 /*
1157  * prototypes the user should DEFINITELY not use directly
1158  */
1159 void	ng_free_item(item_p item); /* Use NG_FREE_ITEM instead */
1160 int	ng_mod_event(module_t mod, int what, void *arg);
1161 
1162 /*
1163  * Tag definitions and constants
1164  */
1165 
1166 #define	NG_TAG_PRIO	1
1167 
1168 struct ng_tag_prio {
1169 	struct m_tag	tag;
1170 	char	priority;
1171 	char	discardability;
1172 };
1173 
1174 #define	NG_PRIO_CUTOFF		32
1175 #define	NG_PRIO_LINKSTATE	64
1176 
1177 /* Macros and declarations to keep compatibility with metadata, which
1178  * is obsoleted now. To be deleted.
1179  */
1180 typedef void *meta_p;
1181 #define _NGI_META(i)	NULL
1182 #define NGI_META(i)	NULL
1183 #define NG_FREE_META(meta)
1184 #define NGI_GET_META(i,m)
1185 #define	ng_copy_meta(meta) NULL
1186 
1187 #endif /* _NETGRAPH_NETGRAPH_H_ */
1188