kernel - TMPFS - Stabilization pass, fix VM object leak, msync
[dragonfly.git] / sys / netgraph / ng_message.h
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  * $DragonFly: src/sys/netgraph/ng_message.h,v 1.6 2008/01/05 14:02:38 swildner Exp $
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 #ifndef _SYS_TYPES_H_
48 #include <sys/types.h>
49 #endif
50 #ifndef _SYS_IOCCOM_H_
51 #include <sys/ioccom.h>
52 #endif
53
54 /* ASCII string size limits */
55 #define NG_TYPESIZ      32      /* max type name len (including null) */
56 #define NG_HOOKSIZ      32      /* max hook name len (including null) */
57 #define NG_NODESIZ      32      /* max node name len (including null) */
58 #define NG_PATHSIZ      512     /* max path len (including null) */
59 #define NG_CMDSTRSIZ    32      /* max command string (including null) */
60 #define NG_TEXTRESPONSE 1024    /* allow this length for a text response */
61
62 /* A netgraph message */
63 struct ng_mesg {
64         struct  ng_msghdr {
65                 u_char          version;                /* must == NG_VERSION */
66                 u_char          spare;                  /* pad to 2 bytes */
67                 u_int16_t       arglen;                 /* length of data */
68                 u_int32_t       flags;                  /* message status */
69                 u_int32_t       token;                  /* match with reply */
70                 u_int32_t       typecookie;             /* node's type cookie */
71                 u_int32_t       cmd;                    /* command identifier */
72                 u_char          cmdstr[NG_CMDSTRSIZ];   /* cmd string */
73         } header;
74         char    data[0];                /* placeholder for actual data */
75 };
76
77 /* Keep this in sync with the above structure definition */
78 #define NG_GENERIC_NG_MESG_INFO(dtype)  {                       \
79           { "version",          &ng_parse_uint8_type    },      \
80           { "spare",            &ng_parse_uint8_type    },      \
81           { "arglen",           &ng_parse_uint16_type   },      \
82           { "flags",            &ng_parse_hint32_type   },      \
83           { "token",            &ng_parse_uint32_type   },      \
84           { "typecookie",       &ng_parse_uint32_type   },      \
85           { "cmd",              &ng_parse_uint32_type   },      \
86           { "cmdstr",           &ng_parse_cmdbuf_type   },      \
87           { "data",             (dtype)                 },      \
88           { NULL }                                              \
89 }
90
91 /* Negraph type binary compatibility field */
92 #define NG_VERSION      2
93
94 /* Flags field flags */
95 #define NGF_ORIG        0x0000          /* the msg is the original request */
96 #define NGF_RESP        0x0001          /* the message is a response */
97
98 /* Type of a unique node ID */
99 #define ng_ID_t unsigned int
100
101 /*
102  * Here we describe the "generic" messages that all nodes inherently
103  * understand. With the exception of NGM_TEXT_STATUS, these are handled
104  * automatically by the base netgraph code.
105  */
106
107 /* Generic message type cookie */
108 #define NGM_GENERIC_COOKIE      851672668
109
110 /* Generic messages defined for this type cookie */
111 #define NGM_SHUTDOWN            1       /* shut down node */
112 #define NGM_MKPEER              2       /* create and attach a peer node */
113 #define NGM_CONNECT             3       /* connect two nodes */
114 #define NGM_NAME                4       /* give a node a name */
115 #define NGM_RMHOOK              5       /* break a connection btw. two nodes */
116 #define NGM_NODEINFO            6       /* get nodeinfo for the target */
117 #define NGM_LISTHOOKS           7       /* get list of hooks on node */
118 #define NGM_LISTNAMES           8       /* list all globally named nodes */
119 #define NGM_LISTNODES           9       /* list all nodes, named and unnamed */
120 #define NGM_LISTTYPES           10      /* list all installed node types */
121 #define NGM_TEXT_STATUS         11      /* (optional) get text status report */
122 #define NGM_BINARY2ASCII        12      /* convert struct ng_mesg to ascii */
123 #define NGM_ASCII2BINARY        13      /* convert ascii to struct ng_mesg */
124 #define NGM_TEXT_CONFIG         14      /* (optional) get/set text config */
125
126 /* Structure used for NGM_MKPEER */
127 struct ngm_mkpeer {
128         char    type[NG_TYPESIZ];               /* peer type */
129         char    ourhook[NG_HOOKSIZ];            /* hook name */
130         char    peerhook[NG_HOOKSIZ];           /* peer hook name */
131 };
132
133 /* Keep this in sync with the above structure definition */
134 #define NG_GENERIC_MKPEER_INFO()        {                       \
135           { "type",             &ng_parse_typebuf_type  },      \
136           { "ourhook",          &ng_parse_hookbuf_type  },      \
137           { "peerhook",         &ng_parse_hookbuf_type  },      \
138           { NULL }                                              \
139 }
140
141 /* Structure used for NGM_CONNECT */
142 struct ngm_connect {
143         char    path[NG_PATHSIZ];               /* peer path */
144         char    ourhook[NG_HOOKSIZ];            /* hook name */
145         char    peerhook[NG_HOOKSIZ];           /* peer hook name */
146 };
147
148 /* Keep this in sync with the above structure definition */
149 #define NG_GENERIC_CONNECT_INFO()       {                       \
150           { "path",             &ng_parse_pathbuf_type  },      \
151           { "ourhook",          &ng_parse_hookbuf_type  },      \
152           { "peerhook",         &ng_parse_hookbuf_type  },      \
153           { NULL }                                              \
154 }
155
156 /* Structure used for NGM_NAME */
157 struct ngm_name {
158         char    name[NG_NODESIZ];                       /* node name */
159 };
160
161 /* Keep this in sync with the above structure definition */
162 #define NG_GENERIC_NAME_INFO()  {                               \
163           { "name",             &ng_parse_nodebuf_type  },      \
164           { NULL }                                              \
165 }
166
167 /* Structure used for NGM_RMHOOK */
168 struct ngm_rmhook {
169         char    ourhook[NG_HOOKSIZ];            /* hook name */
170 };
171
172 /* Keep this in sync with the above structure definition */
173 #define NG_GENERIC_RMHOOK_INFO()        {                       \
174           { "hook",             &ng_parse_hookbuf_type  },      \
175           { NULL }                                              \
176 }
177
178 /* Structure used for NGM_NODEINFO */
179 struct nodeinfo {
180         char            name[NG_NODESIZ];       /* node name (if any) */
181         char            type[NG_TYPESIZ];       /* peer type */
182         ng_ID_t         id;                     /* unique identifier */
183         u_int32_t       hooks;                  /* number of active hooks */
184 };
185
186 /* Keep this in sync with the above structure definition */
187 #define NG_GENERIC_NODEINFO_INFO()      {                       \
188           { "name",             &ng_parse_nodebuf_type  },      \
189           { "type",             &ng_parse_typebuf_type  },      \
190           { "id",               &ng_parse_hint32_type   },      \
191           { "hooks",            &ng_parse_uint32_type   },      \
192           { NULL }                                              \
193 }
194
195 /* Structure used for NGM_LISTHOOKS */
196 struct linkinfo {
197         char            ourhook[NG_HOOKSIZ];    /* hook name */
198         char            peerhook[NG_HOOKSIZ];   /* peer hook */
199         struct nodeinfo nodeinfo;
200 };
201
202 /* Keep this in sync with the above structure definition */
203 #define NG_GENERIC_LINKINFO_INFO(nitype)        {               \
204           { "ourhook",          &ng_parse_hookbuf_type  },      \
205           { "peerhook",         &ng_parse_hookbuf_type  },      \
206           { "nodeinfo",         (nitype)                },      \
207           { NULL }                                              \
208 }
209
210 struct hooklist {
211         struct nodeinfo nodeinfo;               /* node information */
212         struct linkinfo link[0];                /* info about each hook */
213 };
214
215 /* Keep this in sync with the above structure definition */
216 #define NG_GENERIC_HOOKLIST_INFO(nitype,litype) {               \
217           { "nodeinfo",         (nitype)                },      \
218           { "linkinfo",         (litype)                },      \
219           { NULL }                                              \
220 }
221
222 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES */
223 struct namelist {
224         u_int32_t       numnames;
225         struct nodeinfo nodeinfo[0];
226 };
227
228 /* Keep this in sync with the above structure definition */
229 #define NG_GENERIC_LISTNODES_INFO(niarraytype)  {               \
230           { "numnames",         &ng_parse_uint32_type   },      \
231           { "nodeinfo",         (niarraytype)           },      \
232           { NULL }                                              \
233 }
234
235 /* Structure used for NGM_LISTTYPES */
236 struct typeinfo {
237         char            type_name[NG_TYPESIZ];          /* name of type */
238         u_int32_t       numnodes;                       /* number alive */
239 };
240
241 /* Keep this in sync with the above structure definition */
242 #define NG_GENERIC_TYPEINFO_INFO()              {               \
243           { "typename",         &ng_parse_typebuf_type  },      \
244           { "numnodes",         &ng_parse_uint32_type   },      \
245           { NULL }                                              \
246 }
247
248 struct typelist {
249         u_int32_t       numtypes;
250         struct typeinfo typeinfo[0];
251 };
252
253 /* Keep this in sync with the above structure definition */
254 #define NG_GENERIC_TYPELIST_INFO(tiarraytype)   {               \
255           { "numtypes",         &ng_parse_uint32_type   },      \
256           { "typeinfo",         (tiarraytype)           },      \
257           { NULL }                                              \
258 }
259
260 /*
261  * For netgraph nodes that are somehow associated with file descriptors
262  * (e.g., a device that has a /dev entry and is also a netgraph node),
263  * we define a generic ioctl for requesting the corresponding nodeinfo
264  * structure and for assigning a name (if there isn't one already).
265  */
266
267 #define NGIOCGINFO      _IOR('N', 40, struct nodeinfo)  /* get node info */
268 #define NGIOCSETNAME    _IOW('N', 41, struct ngm_name)  /* set node name */
269
270 #ifdef _KERNEL
271 /*
272  * Allocate and initialize a netgraph message "msg" with "len"
273  * extra bytes of argument. Sets "msg" to NULL if fails.
274  * Does not initialize token.
275  */
276 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how)                      \
277         do {                                                            \
278           MALLOC((msg), struct ng_mesg *, sizeof(struct ng_mesg)        \
279             + (len), M_NETGRAPH, (how) | M_ZERO);                       \
280           if ((msg) == NULL)                                            \
281             break;                                                      \
282           (msg)->header.version = NG_VERSION;                           \
283           (msg)->header.typecookie = (cookie);                          \
284           (msg)->header.cmd = (cmdid);                                  \
285           (msg)->header.arglen = (len);                                 \
286           strncpy((msg)->header.cmdstr, #cmdid,                         \
287             sizeof((msg)->header.cmdstr) - 1);                          \
288         } while (0)
289
290 /*
291  * Allocate and initialize a response "rsp" to a message "msg"
292  * with "len" extra bytes of argument. Sets "rsp" to NULL if fails.
293  */
294 #define NG_MKRESPONSE(rsp, msg, len, how)                               \
295         do {                                                            \
296           MALLOC((rsp), struct ng_mesg *, sizeof(struct ng_mesg)        \
297             + (len), M_NETGRAPH, (how) | M_ZERO);                       \
298           if ((rsp) == NULL)                                            \
299             break;                                                      \
300           (rsp)->header.version = NG_VERSION;                           \
301           (rsp)->header.arglen = (len);                                 \
302           (rsp)->header.token = (msg)->header.token;                    \
303           (rsp)->header.typecookie = (msg)->header.typecookie;          \
304           (rsp)->header.cmd = (msg)->header.cmd;                        \
305           bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr,             \
306             sizeof((rsp)->header.cmdstr));                              \
307           (rsp)->header.flags |= NGF_RESP;                              \
308         } while (0)
309 #endif /* _KERNEL */
310
311 #endif /* _NETGRAPH_NG_MESSAGE_H_ */
312