1 /*
2  * ng_message.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 <[email protected]>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_message.h,v 1.12 1999/01/25 01:17:44 archie Exp $
42  */
43 
44 #ifndef _NETGRAPH_NG_MESSAGE_H_
45 #define _NETGRAPH_NG_MESSAGE_H_
46 
47 #include <stdint.h>
48 
49 /* ASCII string size limits */
50 #define	NG_TYPESIZ	32	/* max type name len (including null) */
51 #define	NG_HOOKSIZ	32	/* max hook name len (including null) */
52 #define	NG_NODESIZ	32	/* max node name len (including null) */
53 #define	NG_PATHSIZ	512	/* max path len (including null) */
54 #define	NG_CMDSTRSIZ	32	/* max command string (including null) */
55 
56 #define NG_TEXTRESPONSE 1024	/* allow this length for a text response */
57 
58 /* A netgraph message */
59 struct ng_mesg {
60 	struct	ng_msghdr {
61 		u_char		version;		/*  == NGM_VERSION */
62 		u_char		spare;			/* pad to 4 bytes */
63 		u_int16_t	spare2;
64 		u_int32_t	arglen;			/* length of data */
65 		u_int32_t	cmd;			/* command identifier */
66 		u_int32_t	flags;			/* message status */
67 		u_int32_t	token;			/* match with reply */
68 		u_int32_t	typecookie;		/* node's type cookie */
69 		u_char		cmdstr[NG_CMDSTRSIZ];	/* cmd string + \0 */
70 	} header;
71 	char	data[];			/* placeholder for actual data */
72 };
73 
74 /* This command is guaranteed to not alter data (or'd into the command). */
75 #define NGM_READONLY	0x10000000
76 /* This command is guaranteed to have a reply (or'd into the command). */
77 #define NGM_HASREPLY	0x20000000
78 
79 /* Keep this in sync with the above structure definition */
80 #define NG_GENERIC_NG_MESG_INFO(dtype)	{			\
81 	  { "version",		&ng_parse_uint8_type	},	\
82 	  { "spare",		&ng_parse_uint8_type	},	\
83 	  { "spare2",		&ng_parse_uint16_type	},	\
84 	  { "arglen",		&ng_parse_uint32_type	},	\
85 	  { "cmd",		&ng_parse_uint32_type	},	\
86 	  { "flags",		&ng_parse_hint32_type	},	\
87 	  { "token",		&ng_parse_uint32_type	},	\
88 	  { "typecookie",	&ng_parse_uint32_type	},	\
89 	  { "cmdstr",		&ng_parse_cmdbuf_type	},	\
90 	  { "data",		(dtype)			},	\
91 	  { NULL }						\
92 }
93 
94 /*
95  * Netgraph message header compatibility field
96  * Interfaces within the kernel are defined by a different
97  * value (see NG_ABI_VERSION in netgraph.h)
98  */
99 #define NG_VERSION	8
100 
101 /* Flags field flags */
102 #define NGF_ORIG	0x00000000	/* the msg is the original request */
103 #define NGF_RESP	0x00000001	/* the message is a response */
104 
105 /* Type of a unique node ID. */
106 #define ng_ID_t uint32_t
107 
108 /*
109  * Here we describe the "generic" messages that all nodes inherently
110  * understand. With the exception of NGM_TEXT_STATUS, these are handled
111  * automatically by the base netgraph code.
112  */
113 
114 /* Generic message type cookie */
115 #define NGM_GENERIC_COOKIE	1137070366
116 
117 /* Generic messages defined for this type cookie. */
118 enum {
119 	NGM_SHUTDOWN	= 1,	/* Shut down node. */
120 	NGM_MKPEER	= 2,	/* Create and attach a peer node. */
121 	NGM_CONNECT	= 3,	/* Connect two nodes. */
122 	NGM_NAME	= 4,	/* Give a node a name. */
123 	NGM_RMHOOK	= 5,	/* Break a connection between two nodes. */
124 
125 	/* Get nodeinfo for target. */
126 	NGM_NODEINFO	= (6|NGM_READONLY|NGM_HASREPLY),
127 	/* Get list of hooks on node. */
128 	NGM_LISTHOOKS	= (7|NGM_READONLY|NGM_HASREPLY),
129 	/* List globally named nodes. */
130 	NGM_LISTNAMES	= (8|NGM_READONLY|NGM_HASREPLY),
131 	/* List all nodes. */
132 	NGM_LISTNODES	= (9|NGM_READONLY|NGM_HASREPLY),
133 	/* List installed node types. */
134 	NGM_LISTTYPES	= (10|NGM_READONLY|NGM_HASREPLY),
135 	/* (optional) Get text status. */
136 	NGM_TEXT_STATUS	= (11|NGM_READONLY|NGM_HASREPLY),
137 	/* Convert struct ng_mesg to ASCII. */
138 	NGM_BINARY2ASCII= (12|NGM_READONLY|NGM_HASREPLY),
139 	/* Convert ASCII to struct ng_mesg. */
140 	NGM_ASCII2BINARY= (13|NGM_READONLY|NGM_HASREPLY),
141 	/* (optional) Get/set text config. */
142 	NGM_TEXT_CONFIG	= 14,
143 };
144 
145 /*
146  * Flow control and intra node control messages.
147  * These are routed between nodes to allow flow control and to allow
148  * events to be passed around the graph.
149  * There will be some form of default handling for these but I
150  * do not yet know what it is..
151  */
152 
153 /* Generic message type cookie */
154 #define NGM_FLOW_COOKIE	851672669 /* temp for debugging */
155 
156 /* Upstream messages */
157 #define NGM_LINK_IS_UP		32	/* e.g. carrier found - no data */
158 #define NGM_LINK_IS_DOWN	33	/* carrier lost, includes queue state */
159 #define NGM_HIGH_WATER_PASSED	34	/* includes queue state */
160 #define NGM_LOW_WATER_PASSED	35	/* includes queue state */
161 #define NGM_SYNC_QUEUE_STATE	36	/* sync response from sending packet */
162 
163 /* Downstream messages */
164 #define NGM_DROP_LINK		41	/* drop DTR, etc. - stay in the graph */
165 #define NGM_RAISE_LINK		42	/* if you previously dropped it */
166 #define NGM_FLUSH_QUEUE		43	/* no data */
167 #define NGM_GET_BANDWIDTH	(44|NGM_READONLY)	/* either real or measured */
168 #define NGM_SET_XMIT_Q_LIMITS	45	/* includes queue state */
169 #define NGM_GET_XMIT_Q_LIMITS	(46|NGM_READONLY)	/* returns queue state */
170 #define NGM_MICROMANAGE		47	/* We want sync. queue state
171 						reply for each packet sent */
172 #define NGM_SET_FLOW_MANAGER	48	/* send flow control here */
173 /* Structure used for NGM_MKPEER */
174 struct ngm_mkpeer {
175 	char	type[NG_TYPESIZ];		/* peer type */
176 	char	ourhook[NG_HOOKSIZ];		/* hook name */
177 	char	peerhook[NG_HOOKSIZ];		/* peer hook name */
178 };
179 
180 /* Keep this in sync with the above structure definition */
181 #define NG_GENERIC_MKPEER_INFO()	{			\
182 	  { "type",		&ng_parse_typebuf_type	},	\
183 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
184 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
185 	  { NULL }						\
186 }
187 
188 /* Structure used for NGM_CONNECT */
189 struct ngm_connect {
190 	char	path[NG_PATHSIZ];		/* peer path */
191 	char	ourhook[NG_HOOKSIZ];		/* hook name */
192 	char	peerhook[NG_HOOKSIZ];		/* peer hook name */
193 };
194 
195 /* Keep this in sync with the above structure definition */
196 #define NG_GENERIC_CONNECT_INFO()	{			\
197 	  { "path",		&ng_parse_pathbuf_type	},	\
198 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
199 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
200 	  { NULL }						\
201 }
202 
203 /* Structure used for NGM_NAME */
204 struct ngm_name {
205 	char	name[NG_NODESIZ];			/* node name */
206 };
207 
208 /* Keep this in sync with the above structure definition */
209 #define NG_GENERIC_NAME_INFO()	{				\
210 	  { "name",		&ng_parse_nodebuf_type	},	\
211 	  { NULL }						\
212 }
213 
214 /* Structure used for NGM_RMHOOK */
215 struct ngm_rmhook {
216 	char	ourhook[NG_HOOKSIZ];		/* hook name */
217 };
218 
219 /* Keep this in sync with the above structure definition */
220 #define NG_GENERIC_RMHOOK_INFO()	{			\
221 	  { "hook",		&ng_parse_hookbuf_type	},	\
222 	  { NULL }						\
223 }
224 
225 /* Structure used for NGM_NODEINFO */
226 struct nodeinfo {
227 	char		name[NG_NODESIZ];	/* node name (if any) */
228         char    	type[NG_TYPESIZ];	/* peer type */
229 	ng_ID_t		id;			/* unique identifier */
230 	u_int32_t	hooks;			/* number of active hooks */
231 };
232 
233 /* Keep this in sync with the above structure definition */
234 #define NG_GENERIC_NODEINFO_INFO()	{			\
235 	  { "name",		&ng_parse_nodebuf_type	},	\
236 	  { "type",		&ng_parse_typebuf_type	},	\
237 	  { "id",		&ng_parse_hint32_type	},	\
238 	  { "hooks",		&ng_parse_uint32_type	},	\
239 	  { NULL }						\
240 }
241 
242 /* Structure used for NGM_LISTHOOKS */
243 struct linkinfo {
244 	char		ourhook[NG_HOOKSIZ];	/* hook name */
245 	char		peerhook[NG_HOOKSIZ];	/* peer hook */
246 	struct nodeinfo	nodeinfo;
247 };
248 
249 /* Keep this in sync with the above structure definition */
250 #define NG_GENERIC_LINKINFO_INFO(nitype)	{		\
251 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
252 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
253 	  { "nodeinfo",		(nitype)		},	\
254 	  { NULL }						\
255 }
256 
257 struct hooklist {
258 	struct nodeinfo nodeinfo;		/* node information */
259 	struct linkinfo link[];			/* info about each hook */
260 };
261 
262 /* Keep this in sync with the above structure definition */
263 #define NG_GENERIC_HOOKLIST_INFO(nitype,litype)	{		\
264 	  { "nodeinfo",		(nitype)		},	\
265 	  { "linkinfo",		(litype)		},	\
266 	  { NULL }						\
267 }
268 
269 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES */
270 struct namelist {
271 	u_int32_t	numnames;
272 	struct nodeinfo	nodeinfo[];
273 };
274 
275 /* Keep this in sync with the above structure definition */
276 #define NG_GENERIC_LISTNODES_INFO(niarraytype)	{		\
277 	  { "numnames",		&ng_parse_uint32_type	},	\
278 	  { "nodeinfo",		(niarraytype)		},	\
279 	  { NULL }						\
280 }
281 
282 /* Structure used for NGM_LISTTYPES */
283 struct typeinfo {
284 	char		type_name[NG_TYPESIZ];	/* name of type */
285 	u_int32_t	numnodes;		/* number alive */
286 };
287 
288 /* Keep this in sync with the above structure definition */
289 #define NG_GENERIC_TYPEINFO_INFO()		{		\
290 	  { "typename",		&ng_parse_typebuf_type	},	\
291 	  { "numnodes",		&ng_parse_uint32_type	},	\
292 	  { NULL }						\
293 }
294 
295 struct typelist {
296 	u_int32_t	numtypes;
297 	struct typeinfo	typeinfo[];
298 };
299 
300 /* Keep this in sync with the above structure definition */
301 #define NG_GENERIC_TYPELIST_INFO(tiarraytype)	{		\
302 	  { "numtypes",		&ng_parse_uint32_type	},	\
303 	  { "typeinfo",		(tiarraytype)		},	\
304 	  { NULL }						\
305 }
306 
307 struct ngm_bandwidth {
308 	u_int64_t	nominal_in;
309 	u_int64_t	seen_in;
310 	u_int64_t	nominal_out;
311 	u_int64_t	seen_out;
312 };
313 
314 /* Keep this in sync with the above structure definition */
315 #define NG_GENERIC_BANDWIDTH_INFO()	{			\
316 	  { "nominal_in",	&ng_parse_uint64_type	},	\
317 	  { "seen_in",		&ng_parse_uint64_type	},	\
318 	  { "nominal_out",	&ng_parse_uint64_type	},	\
319 	  { "seen_out",		&ng_parse_uint64_type	},	\
320 	  { NULL }						\
321 }
322 
323 /*
324  * Information about a node's 'output' queue.
325  * This is NOT the netgraph input queueing mechanism,
326  * but rather any queue the node may implement internally
327  * This has to consider ALTQ if we are to work with it.
328  * As far as I can see, ALTQ counts PACKETS, not bytes.
329  * If ALTQ has several queues and one has passed a watermark
330  * we should have the priority of that queue be real (and not -1)
331  * XXX ALTQ stuff is just an idea.....
332  */
333 struct ngm_queue_state {
334 	u_int queue_priority; /* maybe only low-pri is full. -1 = all*/
335 	u_int	max_queuelen_bytes;
336 	u_int	max_queuelen_packets;
337 	u_int	low_watermark;
338 	u_int	high_watermark;
339 	u_int	current;
340 };
341 
342 /* Keep this in sync with the above structure definition */
343 #define NG_GENERIC_QUEUE_INFO()	{				\
344 	  { "max_queuelen_bytes", &ng_parse_uint_type	},	\
345 	  { "max_queuelen_packets", &ng_parse_uint_type	},	\
346 	  { "high_watermark",	&ng_parse_uint_type	},	\
347 	  { "low_watermark",	&ng_parse_uint_type	},	\
348 	  { "current",		&ng_parse_uint_type	},	\
349 	  { NULL }						\
350 }
351 
352 /* Tell a node who to send async flow control info to. */
353 struct flow_manager {
354 	ng_ID_t		id;			/* unique identifier */
355 };
356 
357 /* Keep this in sync with the above structure definition */
358 #define NG_GENERIC_FLOW_MANAGER_INFO()	{			\
359 	  { "id",		&ng_parse_hint32_type	},	\
360 	  { NULL }						\
361 }
362 
363 
364 /*
365  * For netgraph nodes that are somehow associated with file descriptors
366  * (e.g., a device that has a /dev entry and is also a netgraph node),
367  * we define a generic ioctl for requesting the corresponding nodeinfo
368  * structure and for assigning a name (if there isn't one already).
369  *
370  * For these to you need to also #include <sys/ioccom.h>.
371  */
372 
373 #define NGIOCGINFO	_IOR('N', 40, struct nodeinfo)	/* get node info */
374 #define NGIOCSETNAME	_IOW('N', 41, struct ngm_name)	/* set node name */
375 
376 #ifdef _KERNEL
377 /*
378  * Allocate and initialize a netgraph message "msg" with "len"
379  * extra bytes of argument. Sets "msg" to NULL if fails.
380  * Does not initialize token.
381  */
382 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how)			\
383 	do {								\
384 	  (msg) = malloc(sizeof(struct ng_mesg)				\
385 	    + (len), M_NETGRAPH_MSG, (how) | M_ZERO);			\
386 	  if ((msg) == NULL)						\
387 	    break;							\
388 	  (msg)->header.version = NG_VERSION;				\
389 	  (msg)->header.typecookie = (cookie);				\
390 	  (msg)->header.cmd = (cmdid);					\
391 	  (msg)->header.arglen = (len);					\
392 	  strncpy((msg)->header.cmdstr, #cmdid,				\
393 	    sizeof((msg)->header.cmdstr) - 1);				\
394 	} while (0)
395 
396 /*
397  * Allocate and initialize a response "rsp" to a message "msg"
398  * with "len" extra bytes of argument. Sets "rsp" to NULL if fails.
399  */
400 #define NG_MKRESPONSE(rsp, msg, len, how)				\
401 	do {								\
402 	  (rsp) = malloc(sizeof(struct ng_mesg)				\
403 	    + (len), M_NETGRAPH_MSG, (how) | M_ZERO);			\
404 	  if ((rsp) == NULL)						\
405 	    break;							\
406 	  (rsp)->header.version = NG_VERSION;				\
407 	  (rsp)->header.arglen = (len);					\
408 	  (rsp)->header.token = (msg)->header.token;			\
409 	  (rsp)->header.typecookie = (msg)->header.typecookie;		\
410 	  (rsp)->header.cmd = (msg)->header.cmd;			\
411 	  bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr,		\
412 	    sizeof((rsp)->header.cmdstr));				\
413 	  (rsp)->header.flags |= NGF_RESP;				\
414 	} while (0)
415 
416 /*
417  * Make a copy of message. Sets "copy" to NULL if fails.
418  */
419 #define	NG_COPYMESSAGE(copy, msg, how)					\
420 	do {								\
421 	  (copy) = malloc(sizeof(struct ng_mesg)			\
422 	    + (msg)->header.arglen, M_NETGRAPH_MSG, (how) | M_ZERO);	\
423 	  if ((copy) == NULL)						\
424 	    break;							\
425 	  (copy)->header.version = NG_VERSION;				\
426 	  (copy)->header.arglen = (msg)->header.arglen;			\
427 	  (copy)->header.token = (msg)->header.token;			\
428 	  (copy)->header.typecookie = (msg)->header.typecookie;		\
429 	  (copy)->header.cmd = (msg)->header.cmd;			\
430 	  (copy)->header.flags = (msg)->header.flags;			\
431 	  bcopy((msg)->header.cmdstr, (copy)->header.cmdstr,		\
432 	    sizeof((copy)->header.cmdstr));				\
433 	  if ((msg)->header.arglen > 0)					\
434 	    bcopy((msg)->data, (copy)->data, (msg)->header.arglen);	\
435 	} while (0)
436 
437 #endif /* _KERNEL */
438 
439 #endif /* _NETGRAPH_NG_MESSAGE_H_ */
440