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