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