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