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