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