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