xref: /freebsd/sys/netgraph/netgraph.h (revision 4a558355e5f3b4521cef56a6b705fa84be41dfa0)
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 int	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 void ng_ref_node(node_p node);
400 static __inline void _ng_node_ref(node_p node, char *file, int line);
401 static __inline int _ng_node_unref(node_p node, char *file, int line);
402 static __inline void _ng_node_set_private(node_p node, void * val,
403 							char *file, int line);
404 static __inline void * _ng_node_private(node_p node, char *file, int line);
405 static __inline int _ng_node_is_valid(node_p node, char *file, int line);
406 static __inline int _ng_node_not_valid(node_p node, char *file, int line);
407 static __inline int _ng_node_numhooks(node_p node, char *file, int line);
408 static __inline void _ng_node_force_writer(node_p node, char *file, int line);
409 static __inline hook_p _ng_node_foreach_hook(node_p node,
410 			ng_fn_eachhook *fn, void *arg, char *file, int line);
411 
412 static void __inline
413 _chknode(node_p node, char *file, int line)
414 {
415 	if (node->nd_magic != ND_MAGIC) {
416 		printf("Accessing freed node ");
417 		dumpnode(node, file, line);
418 	}
419 	node->lastline = line;
420 	node->lastfile = file;
421 }
422 
423 static __inline char *
424 _ng_node_name(node_p node, char *file, int line)
425 {
426 	_chknode(node, file, line);
427 	return(_NG_NODE_NAME(node));
428 }
429 
430 static __inline int
431 _ng_node_has_name(node_p node, char *file, int line)
432 {
433 	_chknode(node, file, line);
434 	return(_NG_NODE_HAS_NAME(node));
435 }
436 
437 static __inline ng_ID_t
438 _ng_node_id(node_p node, char *file, int line)
439 {
440 	_chknode(node, file, line);
441 	return(_NG_NODE_ID(node));
442 }
443 
444 static __inline void
445 _ng_node_ref(node_p node, char *file, int line)
446 {
447 	_chknode(node, file, line);
448 	/*_NG_NODE_REF(node);*/
449 	ng_ref_node(node);
450 }
451 
452 static __inline int
453 _ng_node_unref(node_p node, char *file, int line)
454 {
455 	_chknode(node, file, line);
456 	return (_NG_NODE_UNREF(node));
457 }
458 
459 static __inline void
460 _ng_node_set_private(node_p node, void * val, char *file, int line)
461 {
462 	_chknode(node, file, line);
463 	_NG_NODE_SET_PRIVATE(node, val);
464 }
465 
466 static __inline void *
467 _ng_node_private(node_p node, char *file, int line)
468 {
469 	_chknode(node, file, line);
470 	return (_NG_NODE_PRIVATE(node));
471 }
472 
473 static __inline int
474 _ng_node_is_valid(node_p node, char *file, int line)
475 {
476 	_chknode(node, file, line);
477 	return(_NG_NODE_IS_VALID(node));
478 }
479 
480 static __inline int
481 _ng_node_not_valid(node_p node, char *file, int line)
482 {
483 	_chknode(node, file, line);
484 	return(_NG_NODE_NOT_VALID(node));
485 }
486 
487 static __inline int
488 _ng_node_numhooks(node_p node, char *file, int line)
489 {
490 	_chknode(node, file, line);
491 	return(_NG_NODE_NUMHOOKS(node));
492 }
493 
494 static __inline void
495 _ng_node_force_writer(node_p node, char *file, int line)
496 {
497 	_chknode(node, file, line);
498 	_NG_NODE_FORCE_WRITER(node);
499 }
500 
501 static __inline void
502 _ng_node_really_die(node_p node, char *file, int line)
503 {
504 	_chknode(node, file, line);
505 	_NG_NODE_REALLY_DIE(node);
506 }
507 
508 static __inline hook_p
509 _ng_node_foreach_hook(node_p node, ng_fn_eachhook *fn, void *arg,
510 						char *file, int line)
511 {
512 	hook_p hook;
513 	_chknode(node, file, line);
514 	_NG_NODE_FOREACH_HOOK(node, fn, arg, hook);
515 	return (hook);
516 }
517 
518 #define NG_NODE_NAME(node)		_ng_node_name(node, _NN_)
519 #define NG_NODE_HAS_NAME(node)		_ng_node_has_name(node, _NN_)
520 #define NG_NODE_ID(node)		_ng_node_id(node, _NN_)
521 #define NG_NODE_REF(node)		_ng_node_ref(node, _NN_)
522 #define	NG_NODE_UNREF(node)		_ng_node_unref(node, _NN_)
523 #define	NG_NODE_SET_PRIVATE(node, val)	_ng_node_set_private(node, val, _NN_)
524 #define	NG_NODE_PRIVATE(node)		_ng_node_private(node, _NN_)
525 #define NG_NODE_IS_VALID(node)		_ng_node_is_valid(node, _NN_)
526 #define NG_NODE_NOT_VALID(node)		_ng_node_not_valid(node, _NN_)
527 #define NG_NODE_FORCE_WRITER(node) 	_ng_node_force_writer(node, _NN_)
528 #define NG_NODE_REALLY_DIE(node) 	_ng_node_really_die(node, _NN_)
529 #define NG_NODE_NUMHOOKS(node)		_ng_node_numhooks(node, _NN_)
530 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)			      \
531 	do {								      \
532 		rethook = _ng_node_foreach_hook(node, fn, (void *)arg, _NN_); \
533 	} while (0)
534 
535 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
536 
537 #define NG_NODE_NAME(node)		_NG_NODE_NAME(node)
538 #define NG_NODE_HAS_NAME(node)		_NG_NODE_HAS_NAME(node)
539 #define NG_NODE_ID(node)		_NG_NODE_ID(node)
540 #define	NG_NODE_REF(node)		_NG_NODE_REF(node)
541 #define	NG_NODE_UNREF(node)		_NG_NODE_UNREF(node)
542 #define	NG_NODE_SET_PRIVATE(node, val)	_NG_NODE_SET_PRIVATE(node, val)
543 #define	NG_NODE_PRIVATE(node)		_NG_NODE_PRIVATE(node)
544 #define NG_NODE_IS_VALID(node)		_NG_NODE_IS_VALID(node)
545 #define NG_NODE_NOT_VALID(node)		_NG_NODE_NOT_VALID(node)
546 #define NG_NODE_FORCE_WRITER(node) 	_NG_NODE_FORCE_WRITER(node)
547 #define NG_NODE_REALLY_DIE(node) 	_NG_NODE_REALLY_DIE(node)
548 #define NG_NODE_NUMHOOKS(node)		_NG_NODE_NUMHOOKS(node)
549 #define NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)			\
550 		_NG_NODE_FOREACH_HOOK(node, fn, arg, rethook)
551 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
552 
553 /***********************************************************************
554  ***************** Meta Data Structures and Methods ********************
555  ***********************************************************************
556  *
557  * The structure that holds meta_data about a data packet (e.g. priority)
558  * Nodes might add or subtract options as needed if there is room.
559  * They might reallocate the struct to make more room if they need to.
560  * Meta-data is still experimental.
561  */
562 struct meta_field_header {
563 	u_long	cookie;		/* cookie for the field. Skip fields you don't
564 				 * know about (same cookie as in messgaes) */
565 	u_short type;		/* field ID */
566 	u_short len;		/* total len of this field including extra
567 				 * data */
568 	char	data[0];	/* data starts here */
569 };
570 
571 /* To zero out an option 'in place' set it's cookie to this */
572 #define NGM_INVALID_COOKIE	865455152
573 
574 /* This part of the metadata is always present if the pointer is non NULL */
575 struct ng_meta {
576 	char	priority;	/* -ve is less priority,  0 is default */
577 	char	discardability; /* higher is less valuable.. discard first */
578 	u_short allocated_len;	/* amount malloc'd */
579 	u_short used_len;	/* sum of all fields, options etc. */
580 	u_short flags;		/* see below.. generic flags */
581 	struct meta_field_header options[0];	/* add as (if) needed */
582 };
583 typedef struct ng_meta *meta_p;
584 
585 /* Flags for meta-data */
586 #define NGMF_TEST	0x01	/* discard at the last moment before sending */
587 #define NGMF_TRACE	0x02	/* trace when handing this data to a node */
588 
589 /***********************************************************************
590  ************* Node Queue and Item Structures and Methods **************
591  ***********************************************************************
592  *
593  */
594 typedef	void	ng_item_fn(node_p node, hook_p hook, void *arg1, int arg2);
595 struct ng_item {
596 	u_long	el_flags;
597 	item_p	el_next;
598 	node_p	el_dest; /* The node it will be applied against (or NULL) */
599 	hook_p	el_hook; /* Entering hook. Optional in Control messages */
600 	union {
601 		struct {
602 			struct mbuf	*da_m;
603 			meta_p		da_meta;
604 		} data;
605 		struct {
606 			struct ng_mesg	*msg_msg;
607 			ng_ID_t		msg_retaddr;
608 		} msg;
609 		struct {
610 			ng_item_fn	*fn_fn;
611 			void 		*fn_arg1;
612 			int		fn_arg2;
613 		} fn;
614 	} body;
615 #ifdef	NETGRAPH_DEBUG /*----------------------------------------------*/
616 	char *lastfile;
617 	int  lastline;
618 	TAILQ_ENTRY(ng_item)	  all;		/* all existing items */
619 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
620 };
621 
622 #define NGQF_TYPE	0x03		/* MASK of content definition */
623 #define NGQF_MESG	0x00		/* the queue element is a message */
624 #define NGQF_DATA	0x01		/* the queue element is data */
625 #define NGQF_FN		0x02		/* the queue element is a function */
626 #define NGQF_UNDEF	0x03		/* UNDEFINED */
627 
628 #define NGQF_RW		0x04		/* MASK for queue entry read/write */
629 #define NGQF_READER	0x04		/* queued as a reader */
630 #define NGQF_WRITER	0x00		/* queued as a writer */
631 
632 #define NGQF_FREE	0x08
633 
634 /*
635  * Get the mbuf (etc) out of an item.
636  * Sets the value in the item to NULL in case we need to call NG_FREE_ITEM()
637  * with it, (to avoid freeing the things twice).
638  * If you don't want to zero out the item then realise that the
639  * item still owns it.
640  * Retaddr is different. There are no references on that. It's just a number.
641  * The debug versions must be either all used everywhere or not at all.
642  */
643 
644 #define _NGI_M(i) ((i)->body.data.da_m)
645 #define _NGI_META(i) ((i)->body.data.da_meta)
646 #define _NGI_MSG(i) ((i)->body.msg.msg_msg)
647 #define _NGI_RETADDR(i) ((i)->body.msg.msg_retaddr)
648 #define	_NGI_FN(i) ((i)->body.fn.fn_fn)
649 #define	_NGI_ARG1(i) ((i)->body.fn.fn_arg1)
650 #define	_NGI_ARG2(i) ((i)->body.fn.fn_arg2)
651 #define	_NGI_NODE(i) ((i)->el_dest)
652 #define	_NGI_HOOK(i) ((i)->el_hook)
653 #define	_NGI_SET_HOOK(i,h) do { _NGI_HOOK(i) = h; h = NULL;} while (0)
654 #define	_NGI_CLR_HOOK(i)   do {						\
655 		hook_p hook = _NGI_HOOK(i);				\
656 		if (hook) {						\
657 			_NG_HOOK_UNREF(hook);				\
658 			_NGI_HOOK(i) = NULL;				\
659 		}							\
660 	} while (0)
661 #define	_NGI_SET_NODE(i,n) do { _NGI_NODE(i) = n; n = NULL;} while (0)
662 #define	_NGI_CLR_NODE(i)   do {						\
663 		node_p node = _NGI_NODE(i);				\
664 		if (node) {						\
665 			_NG_NODE_UNREF(node);				\
666 			_NGI_NODE(i) = NULL;				\
667 		}							\
668 	} while (0)
669 
670 #ifdef NETGRAPH_DEBUG /*----------------------------------------------*/
671 void				dumpitem(item_p item, char *file, int line);
672 static __inline void		_ngi_check(item_p item, char *file, int line) ;
673 static __inline struct mbuf **	_ngi_m(item_p item, char *file, int line) ;
674 static __inline meta_p *	_ngi_meta(item_p item, char *file, int line) ;
675 static __inline ng_ID_t *	_ngi_retaddr(item_p item, char *file, int line);
676 static __inline struct ng_mesg ** _ngi_msg(item_p item, char *file, int line) ;
677 static __inline ng_item_fn **	_ngi_fn(item_p item, char *file, int line) ;
678 static __inline void **		_ngi_arg1(item_p item, char *file, int line) ;
679 static __inline int *		_ngi_arg2(item_p item, char *file, int line) ;
680 static __inline node_p		_ngi_node(item_p item, char *file, int line);
681 static __inline hook_p		_ngi_hook(item_p item, char *file, int line);
682 
683 static __inline void
684 _ngi_check(item_p item, char *file, int line)
685 {
686 	if (item->el_flags & NGQF_FREE) {
687 		dumpitem(item, file, line);
688 		panic ("free item!");
689 	}
690 	(item)->lastline = line;
691 	(item)->lastfile = file;
692 }
693 
694 static __inline struct mbuf **
695 _ngi_m(item_p item, char *file, int line)
696 {
697 	_ngi_check(item, file, line);
698 	return (&_NGI_M(item));
699 }
700 
701 static __inline meta_p *
702 _ngi_meta(item_p item, char *file, int line)
703 {
704 	_ngi_check(item, file, line);
705 	return (&_NGI_META(item));
706 }
707 
708 static __inline struct ng_mesg **
709 _ngi_msg(item_p item, char *file, int line)
710 {
711 	_ngi_check(item, file, line);
712 	return (&_NGI_MSG(item));
713 }
714 
715 static __inline ng_ID_t *
716 _ngi_retaddr(item_p item, char *file, int line)
717 {
718 	_ngi_check(item, file, line);
719 	return (&_NGI_RETADDR(item));
720 }
721 
722 static __inline ng_item_fn **
723 _ngi_fn(item_p item, char *file, int line)
724 {
725 	_ngi_check(item, file, line);
726 	return (&_NGI_FN(item));
727 }
728 
729 static __inline void **
730 _ngi_arg1(item_p item, char *file, int line)
731 {
732 	_ngi_check(item, file, line);
733 	return (&_NGI_ARG1(item));
734 }
735 
736 static __inline int *
737 _ngi_arg2(item_p item, char *file, int line)
738 {
739 	_ngi_check(item, file, line);
740 	return (&_NGI_ARG2(item));
741 }
742 
743 static __inline node_p
744 _ngi_node(item_p item, char *file, int line)
745 {
746 	_ngi_check(item, file, line);
747 	return (_NGI_NODE(item));
748 }
749 
750 static __inline hook_p
751 _ngi_hook(item_p item, char *file, int line)
752 {
753 	_ngi_check(item, file, line);
754 	return (_NGI_HOOK(item));
755 }
756 
757 #define NGI_M(i)	(*_ngi_m(i, _NN_))
758 #define NGI_META(i)	(*_ngi_meta(i, _NN_))
759 #define NGI_MSG(i)	(*_ngi_msg(i, _NN_))
760 #define NGI_RETADDR(i)	(*_ngi_retaddr(i, _NN_))
761 #define NGI_FN(i)	(*_ngi_fn(i, _NN_))
762 #define NGI_ARG1(i)	(*_ngi_arg1(i, _NN_))
763 #define NGI_ARG2(i)	(*_ngi_arg2(i, _NN_))
764 #define NGI_HOOK(i)	_ngi_hook(i, _NN_)
765 #define NGI_NODE(i)	_ngi_node(i, _NN_)
766 #define	NGI_SET_HOOK(i,h)						\
767 	do { _ngi_check(i, _NN_); _NGI_SET_HOOK(i, h); } while (0)
768 #define	NGI_CLR_HOOK(i)							\
769 	do { _ngi_check(i, _NN_); _NGI_CLR_HOOK(i); } while (0)
770 #define	NGI_SET_NODE(i,n)						\
771 	do { _ngi_check(i, _NN_); _NGI_SET_NODE(i, n); } while (0)
772 #define	NGI_CLR_NODE(i)							\
773 	do { _ngi_check(i, _NN_); _NGI_CLR_NODE(i); } while (0)
774 
775 #define NG_FREE_ITEM(item)						\
776 	do {								\
777 		_ngi_check(item, _NN_);					\
778 		ng_free_item((item));					\
779 	} while (0)
780 
781 #define	SAVE_LINE(item)							\
782 	do {								\
783 		(item)->lastline = __LINE__;				\
784 		(item)->lastfile = __FILE__;				\
785 	} while (0)
786 
787 #else	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
788 
789 #define NGI_M(i)	_NGI_M(i)
790 #define NGI_META(i)	_NGI_META(i)
791 #define NGI_MSG(i)	_NGI_MSG(i)
792 #define NGI_RETADDR(i)	_NGI_RETADDR(i)
793 #define NGI_FN(i)	_NGI_FN(i)
794 #define NGI_ARG1(i)	_NGI_ARG1(i)
795 #define NGI_ARG2(i)	_NGI_ARG2(i)
796 #define	NGI_NODE(i)	_NGI_NODE(i)
797 #define	NGI_HOOK(i)	_NGI_HOOK(i)
798 #define	NGI_SET_HOOK(i,h) _NGI_SET_HOOK(i,h)
799 #define	NGI_CLR_HOOK(i)	  _NGI_CLR_HOOK(i)
800 #define	NGI_SET_NODE(i,n) _NGI_SET_NODE(i,n)
801 #define	NGI_CLR_NODE(i)	  _NGI_CLR_NODE(i)
802 
803 #define	NG_FREE_ITEM(item)	ng_free_item((item))
804 #define	SAVE_LINE(item)		do {} while (0)
805 
806 #endif	/* NETGRAPH_DEBUG */ /*----------------------------------------------*/
807 
808 #define NGI_GET_M(i,m)							\
809 	do {								\
810 		(m) = NGI_M(i);						\
811 		_NGI_M(i) = NULL;					\
812 	} while (0)
813 
814 #define NGI_GET_META(i,m)						\
815 	do {								\
816 		(m) = NGI_META(i);					\
817 		_NGI_META(i) = NULL;					\
818 	} while (0)
819 
820 #define NGI_GET_MSG(i,m)						\
821 	do {								\
822 		(m) = NGI_MSG(i);					\
823 		_NGI_MSG(i) = NULL;					\
824 	} while (0)
825 
826 #define NGI_GET_NODE(i,n)	/* YOU NOW HAVE THE REFERENCE */	\
827 	do {								\
828 		(n) = NGI_NODE(i);					\
829 		_NGI_NODE(i) = NULL;					\
830 	} while (0)
831 
832 #define NGI_GET_HOOK(i,h)						\
833 	do {								\
834 		(h) = NGI_HOOK(i);					\
835 		_NGI_HOOK(i) = NULL;					\
836 	} while (0)
837 
838 
839 /**********************************************************************
840 * Data macros.  Send, manipulate and free.
841 **********************************************************************/
842 /*
843  * Assuming the data is already ok, just set the new address and send
844  */
845 #define NG_FWD_ITEM_HOOK(error, item, hook)				\
846 	do {								\
847 		(error) =						\
848 		    ng_address_hook(NULL, (item), (hook), NULL);	\
849 		if (error == 0) {					\
850 			SAVE_LINE(item);				\
851 			(error) = ng_snd_item((item), 0);		\
852 		}							\
853 		(item) = NULL;						\
854 	} while (0)
855 
856 /*
857  * Forward a data packet with no new meta-data.
858  * old metadata is passed along without change.
859  * Mbuf pointer is updated to new value. We presume you dealt with the
860  * old one when you update it to the new one (or it maybe the old one).
861  * We got a packet and possibly had to modify the mbuf.
862  * You should probably use NGI_GET_M() if you are going to use this too
863  */
864 #define NG_FWD_NEW_DATA(error, item, hook, m)				\
865 	do {								\
866 		NGI_M(item) = (m);					\
867 		(m) = NULL;						\
868 		NG_FWD_ITEM_HOOK(error, item, hook);			\
869 	} while (0)
870 
871 /* Send a previously unpackaged mbuf when we have no metadata to send */
872 #define NG_SEND_DATA_ONLY(error, hook, m)				\
873 	do {								\
874 		item_p item;						\
875 		if ((item = ng_package_data((m), NULL))) {		\
876 			NG_FWD_ITEM_HOOK(error, item, hook);		\
877 		} else {						\
878 			(error) = ENOMEM;				\
879 		}							\
880 		(m) = NULL;						\
881 	} while (0)
882 
883 /* Send previously unpackeged data and metadata. */
884 #define NG_SEND_DATA(error, hook, m, meta)				\
885 	do {								\
886 		item_p item;						\
887 		if ((item = ng_package_data((m), (meta)))) {		\
888 			NG_FWD_ITEM_HOOK(error, item, hook);		\
889 		} else {						\
890 			(error) = ENOMEM;				\
891 		}							\
892 		(m) = NULL;						\
893 		(meta) = NULL;						\
894 	} while (0)
895 
896 #define NG_FREE_MSG(msg)						\
897 	do {								\
898 		if ((msg)) {						\
899 			FREE((msg), M_NETGRAPH_MSG);			\
900 			(msg) = NULL;					\
901 		}	 						\
902 	} while (0)
903 
904 #define NG_FREE_META(meta)						\
905 	do {								\
906 		if ((meta)) {						\
907 			FREE((meta), M_NETGRAPH_META);			\
908 			(meta) = NULL;					\
909 		}	 						\
910 	} while (0)
911 
912 #define NG_FREE_M(m)							\
913 	do {								\
914 		if ((m)) {						\
915 			m_freem((m));					\
916 			(m) = NULL;					\
917 		}							\
918 	} while (0)
919 
920 /*****************************************
921 * Message macros
922 *****************************************/
923 
924 #define NG_SEND_MSG_HOOK(error, here, msg, hook, retaddr)		\
925 	do {								\
926 		item_p item;						\
927 		if ((item = ng_package_msg(msg)) == NULL) {		\
928 			(msg) = NULL;					\
929 			(error) = ENOMEM;				\
930 			break;						\
931 		}							\
932 		if (((error) = ng_address_hook((here), (item),		\
933 					(hook), (retaddr))) == 0) {	\
934 			SAVE_LINE(item);				\
935 			(error) = ng_snd_item((item), 0);		\
936 		}							\
937 		(msg) = NULL;						\
938 	} while (0)
939 
940 #define NG_SEND_MSG_PATH(error, here, msg, path, retaddr)		\
941 	do {								\
942 		item_p item;						\
943 		if ((item = ng_package_msg(msg)) == NULL) {		\
944 			(msg) = NULL;					\
945 			(error) = ENOMEM;				\
946 			break;						\
947 		}							\
948 		if (((error) = ng_address_path((here), (item),		\
949 					(path), (retaddr))) == 0) {	\
950 			SAVE_LINE(item);				\
951 			(error) = ng_snd_item((item), 0);		\
952 		}							\
953 		(msg) = NULL;						\
954 	} while (0)
955 
956 #define NG_SEND_MSG_ID(error, here, msg, ID, retaddr)			\
957 	do {								\
958 		item_p item;						\
959 		if ((item = ng_package_msg(msg)) == NULL) {		\
960 			(msg) = NULL;					\
961 			(error) = ENOMEM;				\
962 			break;						\
963 		}							\
964 		if (((error) = ng_address_ID((here), (item),		\
965 					(ID), (retaddr))) == 0) {	\
966 			SAVE_LINE(item);				\
967 			(error) = ng_snd_item((item), 0);		\
968 		}							\
969 		(msg) = NULL;						\
970 	} while (0)
971 
972 /*
973  * Redirect the message to the next hop using the given hook.
974  * ng_retarget_msg() frees the item if there is an error
975  * and returns an error code.  It returns 0 on success.
976  */
977 #define NG_FWD_MSG_HOOK(error, here, item, hook, retaddr)		\
978 	do {								\
979 		if (((error) = ng_address_hook((here), (item),		\
980 					(hook), (retaddr))) == 0) {	\
981 			SAVE_LINE(item);				\
982 			(error) = ng_snd_item((item), 0);		\
983 		}							\
984 		(item) = NULL;						\
985 	} while (0)
986 
987 /*
988  * Send a queue item back to it's originator with a response message.
989  * Assume original message was removed and freed separatly.
990  */
991 #define NG_RESPOND_MSG(error, here, item, resp)				\
992 	do {								\
993 		if (resp) {						\
994 			ng_ID_t dest = NGI_RETADDR(item);		\
995 			NGI_RETADDR(item) = NULL;			\
996 			NGI_MSG(item) = resp;				\
997 			if ((ng_address_ID((here), (item),		\
998 					dest, NULL )) == 0) {		\
999 				SAVE_LINE(item);			\
1000 				(error) = ng_snd_item((item), 1);	\
1001 			} else {					\
1002 				(error) = EINVAL;			\
1003 			}						\
1004 		} else {						\
1005 			NG_FREE_ITEM(item);				\
1006 		}							\
1007 		(item) = NULL;						\
1008 	} while (0)
1009 
1010 
1011 /***********************************************************************
1012  ******** Structures Definitions and Macros for defining a node  *******
1013  ***********************************************************************
1014  *
1015  * Here we define the structures needed to actually define a new node
1016  * type.
1017  */
1018 
1019 /*
1020  * Command list -- each node type specifies the command that it knows
1021  * how to convert between ASCII and binary using an array of these.
1022  * The last element in the array must be a terminator with cookie=0.
1023  */
1024 
1025 struct ng_cmdlist {
1026 	u_int32_t			cookie;		/* command typecookie */
1027 	int				cmd;		/* command number */
1028 	const char			*name;		/* command name */
1029 	const struct ng_parse_type	*mesgType;	/* args if !NGF_RESP */
1030 	const struct ng_parse_type	*respType;	/* args if NGF_RESP */
1031 };
1032 
1033 /*
1034  * Structure of a node type
1035  * If data is sent to the "rcvdata()" entrypoint then the system
1036  * may decide to defer it until later by queing it with the normal netgraph
1037  * input queuing system.  This is decidde by the HK_QUEUE flag being set in
1038  * the flags word of the peer (receiving) hook. The dequeuing mechanism will
1039  * ensure it is not requeued again.
1040  * Note the input queueing system is to allow modules
1041  * to 'release the stack' or to pass data across spl layers.
1042  * The data will be redelivered as soon as the NETISR code runs
1043  * which may be almost immediatly.  A node may also do it's own queueing
1044  * for other reasons (e.g. device output queuing).
1045  */
1046 struct ng_type {
1047 
1048 	u_int32_t	version; 	/* must equal NG_API_VERSION */
1049 	const char	*name;		/* Unique type name */
1050 	modeventhand_t	mod_event;	/* Module event handler (optional) */
1051 	ng_constructor_t *constructor;	/* Node constructor */
1052 	ng_rcvmsg_t	*rcvmsg;	/* control messages come here */
1053 	ng_shutdown_t	*shutdown;	/* reset, and free resources */
1054 	ng_newhook_t	*newhook;	/* first notification of new hook */
1055 	ng_findhook_t	*findhook;	/* only if you have lots of hooks */
1056 	ng_connect_t	*connect;	/* final notification of new hook */
1057 	ng_rcvdata_t	*rcvdata;	/* data comes here */
1058 	ng_disconnect_t	*disconnect;	/* notify on disconnect */
1059 
1060 	const struct	ng_cmdlist *cmdlist;	/* commands we can convert */
1061 
1062 	/* R/W data private to the base netgraph code DON'T TOUCH! */
1063 	LIST_ENTRY(ng_type) types;		/* linked list of all types */
1064 	int		    refs;		/* number of instances */
1065 };
1066 
1067 /*
1068  * Use the NETGRAPH_INIT() macro to link a node type into the
1069  * netgraph system. This works for types compiled into the kernel
1070  * as well as KLD modules. The first argument should be the type
1071  * name (eg, echo) and the second a pointer to the type struct.
1072  *
1073  * If a different link time is desired, e.g., a device driver that
1074  * needs to install its netgraph type before probing, use the
1075  * NETGRAPH_INIT_ORDERED() macro instead. Deivce drivers probably
1076  * want to use SI_SUB_DRIVERS instead of SI_SUB_PSEUDO.
1077  */
1078 
1079 #define NETGRAPH_INIT_ORDERED(typename, typestructp, sub, order)	\
1080 static moduledata_t ng_##typename##_mod = {				\
1081 	"ng_" #typename,						\
1082 	ng_mod_event,							\
1083 	(typestructp)							\
1084 };									\
1085 DECLARE_MODULE(ng_##typename, ng_##typename##_mod, sub, order);		\
1086 MODULE_DEPEND(ng_##typename, netgraph,	NG_ABI_VERSION,			\
1087 					NG_ABI_VERSION,			\
1088 					NG_ABI_VERSION)
1089 
1090 #define NETGRAPH_INIT(tn, tp)						\
1091 	NETGRAPH_INIT_ORDERED(tn, tp, SI_SUB_PSEUDO, SI_ORDER_ANY)
1092 
1093 /* Special malloc() type for netgraph structs and ctrl messages */
1094 /* Only these two types should be visible to nodes */
1095 MALLOC_DECLARE(M_NETGRAPH);
1096 MALLOC_DECLARE(M_NETGRAPH_MSG);
1097 MALLOC_DECLARE(M_NETGRAPH_META);
1098 
1099 /* declare the base of the netgraph sysclt hierarchy */
1100 /* but only if this file cares about sysctls */
1101 #ifdef	SYSCTL_DECL
1102 SYSCTL_DECL(_net_graph);
1103 #endif
1104 
1105 /*
1106  * Methods that the nodes can use.
1107  * Many of these methods should usually NOT be used directly but via
1108  * Macros above.
1109  */
1110 int	ng_address_ID(node_p here, item_p item, ng_ID_t ID, ng_ID_t retaddr);
1111 int	ng_address_hook(node_p here, item_p item, hook_p hook, ng_ID_t retaddr);
1112 int	ng_address_path(node_p here, item_p item, char *address, ng_ID_t raddr);
1113 meta_p	ng_copy_meta(meta_p meta);
1114 hook_p	ng_findhook(node_p node, const char *name);
1115 int	ng_make_node_common(struct ng_type *typep, node_p *nodep);
1116 int	ng_name_node(node_p node, const char *name);
1117 int	ng_newtype(struct ng_type *tp);
1118 ng_ID_t ng_node2ID(node_p node);
1119 item_p	ng_package_data(struct mbuf *m, meta_p meta);
1120 item_p	ng_package_msg(struct ng_mesg *msg);
1121 item_p	ng_package_msg_self(node_p here, hook_p hook, struct ng_mesg *msg);
1122 void	ng_replace_retaddr(node_p here, item_p item, ng_ID_t retaddr);
1123 int	ng_rmhook_self(hook_p hook);	/* if a node wants to kill a hook */
1124 int	ng_rmnode_self(node_p here);	/* if a node wants to suicide */
1125 int	ng_snd_item(item_p item, int queue);
1126 int 	ng_send_fn(node_p node, hook_p hook, ng_item_fn *fn,
1127 	void *arg1, int arg2);
1128 
1129 /*
1130  * prototypes the user should DEFINITLY not use directly
1131  */
1132 void	ng_free_item(item_p item); /* Use NG_FREE_ITEM instead */
1133 int	ng_mod_event(module_t mod, int what, void *arg);
1134 
1135 #endif /* _NETGRAPH_NETGRAPH_H_ */
1136 
1137