xref: /dragonfly/sys/netgraph/ng_message.h (revision 984263bc)
1 
2 /*
3  * ng_message.h
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Julian Elischer <julian@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_message.h,v 1.4.2.5 2002/07/02 23:44:02 archie Exp $
40  * $Whistle: ng_message.h,v 1.12 1999/01/25 01:17:44 archie Exp $
41  */
42 
43 #ifndef _NETGRAPH_NG_MESSAGE_H_
44 #define _NETGRAPH_NG_MESSAGE_H_ 1
45 
46 /* ASCII string size limits */
47 #define NG_TYPELEN	15	/* max type name len (16 with null) */
48 #define NG_HOOKLEN	15	/* max hook name len (16 with null) */
49 #define NG_NODELEN	15	/* max node name len (16 with null) */
50 #define NG_PATHLEN	511	/* max path len     (512 with null) */
51 #define NG_CMDSTRLEN	15	/* max command string (16 with null) */
52 #define NG_TEXTRESPONSE 1024	/* allow this length for a text response */
53 
54 /* A netgraph message */
55 struct ng_mesg {
56 	struct	ng_msghdr {
57 		u_char		version;		/* must == NG_VERSION */
58 		u_char		spare;			/* pad to 2 bytes */
59 		u_int16_t	arglen;			/* length of data */
60 		u_int32_t	flags;			/* message status */
61 		u_int32_t	token;			/* match with reply */
62 		u_int32_t	typecookie;		/* node's type cookie */
63 		u_int32_t	cmd;			/* command identifier */
64 		u_char		cmdstr[NG_CMDSTRLEN+1];	/* cmd string + \0 */
65 	} header;
66 	char	data[0];		/* placeholder for actual data */
67 };
68 
69 /* Keep this in sync with the above structure definition */
70 #define NG_GENERIC_NG_MESG_INFO(dtype)	{			\
71 	  { "version",		&ng_parse_uint8_type	},	\
72 	  { "spare",		&ng_parse_uint8_type	},	\
73 	  { "arglen",		&ng_parse_uint16_type	},	\
74 	  { "flags",		&ng_parse_hint32_type	},	\
75 	  { "token",		&ng_parse_uint32_type	},	\
76 	  { "typecookie",	&ng_parse_uint32_type	},	\
77 	  { "cmd",		&ng_parse_uint32_type	},	\
78 	  { "cmdstr",		&ng_parse_cmdbuf_type	},	\
79 	  { "data",		(dtype)			},	\
80 	  { NULL }						\
81 }
82 
83 /* Negraph type binary compatibility field */
84 #define NG_VERSION	2
85 
86 /* Flags field flags */
87 #define NGF_ORIG	0x0000		/* the msg is the original request */
88 #define NGF_RESP	0x0001		/* the message is a response */
89 
90 /* Type of a unique node ID */
91 #define ng_ID_t unsigned int
92 
93 /*
94  * Here we describe the "generic" messages that all nodes inherently
95  * understand. With the exception of NGM_TEXT_STATUS, these are handled
96  * automatically by the base netgraph code.
97  */
98 
99 /* Generic message type cookie */
100 #define NGM_GENERIC_COOKIE	851672668
101 
102 /* Generic messages defined for this type cookie */
103 #define	NGM_SHUTDOWN		1	/* shut down node */
104 #define NGM_MKPEER		2	/* create and attach a peer node */
105 #define NGM_CONNECT		3	/* connect two nodes */
106 #define NGM_NAME		4	/* give a node a name */
107 #define NGM_RMHOOK		5	/* break a connection btw. two nodes */
108 #define	NGM_NODEINFO		6	/* get nodeinfo for the target */
109 #define	NGM_LISTHOOKS		7	/* get list of hooks on node */
110 #define	NGM_LISTNAMES		8	/* list all globally named nodes */
111 #define	NGM_LISTNODES		9	/* list all nodes, named and unnamed */
112 #define	NGM_LISTTYPES		10	/* list all installed node types */
113 #define	NGM_TEXT_STATUS		11	/* (optional) get text status report */
114 #define	NGM_BINARY2ASCII	12	/* convert struct ng_mesg to ascii */
115 #define	NGM_ASCII2BINARY	13	/* convert ascii to struct ng_mesg */
116 #define	NGM_TEXT_CONFIG		14	/* (optional) get/set text config */
117 
118 /* Structure used for NGM_MKPEER */
119 struct ngm_mkpeer {
120 	char	type[NG_TYPELEN + 1];			/* peer type */
121 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
122 	char	peerhook[NG_HOOKLEN + 1];		/* peer hook name */
123 };
124 
125 /* Keep this in sync with the above structure definition */
126 #define NG_GENERIC_MKPEER_INFO()	{			\
127 	  { "type",		&ng_parse_typebuf_type	},	\
128 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
129 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
130 	  { NULL }						\
131 }
132 
133 /* Structure used for NGM_CONNECT */
134 struct ngm_connect {
135 	char	path[NG_PATHLEN + 1];			/* peer path */
136 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
137 	char	peerhook[NG_HOOKLEN + 1];		/* peer hook name */
138 };
139 
140 /* Keep this in sync with the above structure definition */
141 #define NG_GENERIC_CONNECT_INFO()	{			\
142 	  { "path",		&ng_parse_pathbuf_type	},	\
143 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
144 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
145 	  { NULL }						\
146 }
147 
148 /* Structure used for NGM_NAME */
149 struct ngm_name {
150 	char	name[NG_NODELEN + 1];			/* node name */
151 };
152 
153 /* Keep this in sync with the above structure definition */
154 #define NG_GENERIC_NAME_INFO()	{				\
155 	  { "name",		&ng_parse_nodebuf_type	},	\
156 	  { NULL }						\
157 }
158 
159 /* Structure used for NGM_RMHOOK */
160 struct ngm_rmhook {
161 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
162 };
163 
164 /* Keep this in sync with the above structure definition */
165 #define NG_GENERIC_RMHOOK_INFO()	{			\
166 	  { "hook",		&ng_parse_hookbuf_type	},	\
167 	  { NULL }						\
168 }
169 
170 /* Structure used for NGM_NODEINFO */
171 struct nodeinfo {
172 	char		name[NG_NODELEN + 1];	/* node name (if any) */
173         char    	type[NG_TYPELEN + 1];   /* peer type */
174 	ng_ID_t		id;			/* unique identifier */
175 	u_int32_t	hooks;			/* number of active hooks */
176 };
177 
178 /* Keep this in sync with the above structure definition */
179 #define NG_GENERIC_NODEINFO_INFO()	{			\
180 	  { "name",		&ng_parse_nodebuf_type	},	\
181 	  { "type",		&ng_parse_typebuf_type	},	\
182 	  { "id",		&ng_parse_hint32_type	},	\
183 	  { "hooks",		&ng_parse_uint32_type	},	\
184 	  { NULL }						\
185 }
186 
187 /* Structure used for NGM_LISTHOOKS */
188 struct linkinfo {
189 	char		ourhook[NG_HOOKLEN + 1];	/* hook name */
190 	char		peerhook[NG_HOOKLEN + 1];	/* peer hook */
191 	struct nodeinfo	nodeinfo;
192 };
193 
194 /* Keep this in sync with the above structure definition */
195 #define NG_GENERIC_LINKINFO_INFO(nitype)	{		\
196 	  { "ourhook",		&ng_parse_hookbuf_type	},	\
197 	  { "peerhook",		&ng_parse_hookbuf_type	},	\
198 	  { "nodeinfo",		(nitype)		},	\
199 	  { NULL }						\
200 }
201 
202 struct hooklist {
203 	struct nodeinfo nodeinfo;		/* node information */
204 	struct linkinfo link[0];		/* info about each hook */
205 };
206 
207 /* Keep this in sync with the above structure definition */
208 #define NG_GENERIC_HOOKLIST_INFO(nitype,litype)	{		\
209 	  { "nodeinfo",		(nitype)		},	\
210 	  { "linkinfo",		(litype)		},	\
211 	  { NULL }						\
212 }
213 
214 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES */
215 struct namelist {
216 	u_int32_t	numnames;
217 	struct nodeinfo	nodeinfo[0];
218 };
219 
220 /* Keep this in sync with the above structure definition */
221 #define NG_GENERIC_LISTNODES_INFO(niarraytype)	{		\
222 	  { "numnames",		&ng_parse_uint32_type	},	\
223 	  { "nodeinfo",		(niarraytype)		},	\
224 	  { NULL }						\
225 }
226 
227 /* Structure used for NGM_LISTTYPES */
228 struct typeinfo {
229 	char		type_name[NG_TYPELEN + 1];	/* name of type */
230 	u_int32_t	numnodes;			/* number alive */
231 };
232 
233 /* Keep this in sync with the above structure definition */
234 #define NG_GENERIC_TYPEINFO_INFO()		{		\
235 	  { "typename",		&ng_parse_typebuf_type	},	\
236 	  { "numnodes",		&ng_parse_uint32_type	},	\
237 	  { NULL }						\
238 }
239 
240 struct typelist {
241 	u_int32_t	numtypes;
242 	struct typeinfo	typeinfo[0];
243 };
244 
245 /* Keep this in sync with the above structure definition */
246 #define NG_GENERIC_TYPELIST_INFO(tiarraytype)	{		\
247 	  { "numtypes",		&ng_parse_uint32_type	},	\
248 	  { "typeinfo",		(tiarraytype)		},	\
249 	  { NULL }						\
250 }
251 
252 /*
253  * For netgraph nodes that are somehow associated with file descriptors
254  * (e.g., a device that has a /dev entry and is also a netgraph node),
255  * we define a generic ioctl for requesting the corresponding nodeinfo
256  * structure and for assigning a name (if there isn't one already).
257  *
258  * For these to you need to also #include <sys/ioccom.h>.
259  */
260 
261 #define NGIOCGINFO	_IOR('N', 40, struct nodeinfo)	/* get node info */
262 #define NGIOCSETNAME	_IOW('N', 41, struct ngm_name)	/* set node name */
263 
264 #ifdef _KERNEL
265 /*
266  * Allocate and initialize a netgraph message "msg" with "len"
267  * extra bytes of argument. Sets "msg" to NULL if fails.
268  * Does not initialize token.
269  */
270 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how)			\
271 	do {								\
272 	  MALLOC((msg), struct ng_mesg *, sizeof(struct ng_mesg)	\
273 	    + (len), M_NETGRAPH, (how));				\
274 	  if ((msg) == NULL)						\
275 	    break;							\
276 	  bzero((msg), sizeof(struct ng_mesg) + (len));			\
277 	  (msg)->header.version = NG_VERSION;				\
278 	  (msg)->header.typecookie = (cookie);				\
279 	  (msg)->header.cmd = (cmdid);					\
280 	  (msg)->header.arglen = (len);					\
281 	  strncpy((msg)->header.cmdstr, #cmdid,				\
282 	    sizeof((msg)->header.cmdstr) - 1);				\
283 	} while (0)
284 
285 /*
286  * Allocate and initialize a response "rsp" to a message "msg"
287  * with "len" extra bytes of argument. Sets "rsp" to NULL if fails.
288  */
289 #define NG_MKRESPONSE(rsp, msg, len, how)				\
290 	do {								\
291 	  MALLOC((rsp), struct ng_mesg *, sizeof(struct ng_mesg)	\
292 	    + (len), M_NETGRAPH, (how));				\
293 	  if ((rsp) == NULL)						\
294 	    break;							\
295 	  bzero((rsp), sizeof(struct ng_mesg) + (len));			\
296 	  (rsp)->header.version = NG_VERSION;				\
297 	  (rsp)->header.arglen = (len);					\
298 	  (rsp)->header.token = (msg)->header.token;			\
299 	  (rsp)->header.typecookie = (msg)->header.typecookie;		\
300 	  (rsp)->header.cmd = (msg)->header.cmd;			\
301 	  bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr,		\
302 	    sizeof((rsp)->header.cmdstr));				\
303 	  (rsp)->header.flags |= NGF_RESP;				\
304 	} while (0)
305 #endif /* _KERNEL */
306 
307 #endif /* _NETGRAPH_NG_MESSAGE_H_ */
308 
309