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