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