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@whistle.com> 38 * 39 * $FreeBSD$ 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 { \ 72 { "version", &ng_parse_int8_type }, \ 73 { "spare", &ng_parse_int8_type }, \ 74 { "arglen", &ng_parse_int16_type }, \ 75 { "flags", &ng_parse_int32_type }, \ 76 { "token", &ng_parse_int32_type }, \ 77 { "typecookie", &ng_parse_int32_type }, \ 78 { "cmd", &ng_parse_int32_type }, \ 79 { "cmdstr", &ng_parse_cmdbuf_type }, \ 80 { "data", (dtype) }, \ 81 { NULL }, \ 82 } \ 83 } 84 85 /* Negraph type binary compatibility field */ 86 #define NG_VERSION 2 87 88 /* Flags field flags */ 89 #define NGF_ORIG 0x0000 /* the msg is the original request */ 90 #define NGF_RESP 0x0001 /* the message is a response */ 91 92 /* Type of a unique node ID */ 93 #define ng_ID_t unsigned int 94 95 /* 96 * Here we describe the "generic" messages that all nodes inherently 97 * understand. With the exception of NGM_TEXT_STATUS, these are handled 98 * automatically by the base netgraph code. 99 */ 100 101 /* Generic message type cookie */ 102 #define NGM_GENERIC_COOKIE 851672668 103 104 /* Generic messages defined for this type cookie */ 105 #define NGM_SHUTDOWN 1 /* shut down node */ 106 #define NGM_MKPEER 2 /* create and attach a peer node */ 107 #define NGM_CONNECT 3 /* connect two nodes */ 108 #define NGM_NAME 4 /* give a node a name */ 109 #define NGM_RMHOOK 5 /* break a connection btw. two nodes */ 110 #define NGM_NODEINFO 6 /* get nodeinfo for the target */ 111 #define NGM_LISTHOOKS 7 /* get list of hooks on node */ 112 #define NGM_LISTNAMES 8 /* list all globally named nodes */ 113 #define NGM_LISTNODES 9 /* list all nodes, named and unnamed */ 114 #define NGM_LISTTYPES 10 /* list all installed node types */ 115 #define NGM_TEXT_STATUS 11 /* (optional) get text status report */ 116 #define NGM_BINARY2ASCII 12 /* convert struct ng_mesg to ascii */ 117 #define NGM_ASCII2BINARY 13 /* convert ascii to struct ng_mesg */ 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 { \ 129 { "type", &ng_parse_typebuf_type }, \ 130 { "ourhook", &ng_parse_hookbuf_type }, \ 131 { "peerhook", &ng_parse_hookbuf_type }, \ 132 { NULL }, \ 133 } \ 134 } 135 136 /* Structure used for NGM_CONNECT */ 137 struct ngm_connect { 138 char path[NG_PATHLEN + 1]; /* peer path */ 139 char ourhook[NG_HOOKLEN + 1]; /* hook name */ 140 char peerhook[NG_HOOKLEN + 1]; /* peer hook name */ 141 }; 142 143 /* Keep this in sync with the above structure definition */ 144 #define NG_GENERIC_CONNECT_INFO() { \ 145 { \ 146 { "path", &ng_parse_pathbuf_type }, \ 147 { "ourhook", &ng_parse_hookbuf_type }, \ 148 { "peerhook", &ng_parse_hookbuf_type }, \ 149 { NULL }, \ 150 } \ 151 } 152 153 /* Structure used for NGM_NAME */ 154 struct ngm_name { 155 char name[NG_NODELEN + 1]; /* node name */ 156 }; 157 158 /* Keep this in sync with the above structure definition */ 159 #define NG_GENERIC_NAME_INFO() { \ 160 { \ 161 { "name", &ng_parse_nodebuf_type }, \ 162 { NULL }, \ 163 } \ 164 } 165 166 /* Structure used for NGM_RMHOOK */ 167 struct ngm_rmhook { 168 char ourhook[NG_HOOKLEN + 1]; /* hook name */ 169 }; 170 171 /* Keep this in sync with the above structure definition */ 172 #define NG_GENERIC_RMHOOK_INFO() { \ 173 { \ 174 { "hook", &ng_parse_hookbuf_type }, \ 175 { NULL }, \ 176 } \ 177 } 178 179 /* Structure used for NGM_NODEINFO */ 180 struct nodeinfo { 181 char name[NG_NODELEN + 1]; /* node name (if any) */ 182 char type[NG_TYPELEN + 1]; /* peer type */ 183 ng_ID_t id; /* unique identifier */ 184 u_int32_t hooks; /* number of active hooks */ 185 }; 186 187 /* Keep this in sync with the above structure definition */ 188 #define NG_GENERIC_NODEINFO_INFO() { \ 189 { \ 190 { "name", &ng_parse_nodebuf_type }, \ 191 { "type", &ng_parse_typebuf_type }, \ 192 { "id", &ng_parse_int32_type }, \ 193 { "hooks", &ng_parse_int32_type }, \ 194 { NULL }, \ 195 } \ 196 } 197 198 /* Structure used for NGM_LISTHOOKS */ 199 struct linkinfo { 200 char ourhook[NG_HOOKLEN + 1]; /* hook name */ 201 char peerhook[NG_HOOKLEN + 1]; /* peer hook */ 202 struct nodeinfo nodeinfo; 203 }; 204 205 /* Keep this in sync with the above structure definition */ 206 #define NG_GENERIC_LINKINFO_INFO(nitype) { \ 207 { \ 208 { "ourhook", &ng_parse_hookbuf_type }, \ 209 { "peerhook", &ng_parse_hookbuf_type }, \ 210 { "nodeinfo", (nitype) }, \ 211 { NULL }, \ 212 } \ 213 } 214 215 struct hooklist { 216 struct nodeinfo nodeinfo; /* node information */ 217 struct linkinfo link[0]; /* info about each hook */ 218 }; 219 220 /* Keep this in sync with the above structure definition */ 221 #define NG_GENERIC_HOOKLIST_INFO(nitype,litype) { \ 222 { \ 223 { "nodeinfo", (nitype) }, \ 224 { "linkinfo", (litype) }, \ 225 { NULL }, \ 226 } \ 227 } 228 229 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES */ 230 struct namelist { 231 u_int32_t numnames; 232 struct nodeinfo nodeinfo[0]; 233 }; 234 235 /* Keep this in sync with the above structure definition */ 236 #define NG_GENERIC_LISTNODES_INFO(niarraytype) { \ 237 { \ 238 { "numnames", &ng_parse_int32_type }, \ 239 { "nodeinfo", (niarraytype) }, \ 240 { NULL }, \ 241 } \ 242 } 243 244 /* Structure used for NGM_LISTTYPES */ 245 struct typeinfo { 246 char typename[NG_TYPELEN + 1]; /* name of type */ 247 u_int32_t numnodes; /* number alive */ 248 }; 249 250 /* Keep this in sync with the above structure definition */ 251 #define NG_GENERIC_TYPEINFO_INFO() { \ 252 { \ 253 { "typename", &ng_parse_typebuf_type }, \ 254 { "typeinfo", &ng_parse_int32_type }, \ 255 { NULL }, \ 256 } \ 257 } 258 259 struct typelist { 260 u_int32_t numtypes; 261 struct typeinfo typeinfo[0]; 262 }; 263 264 /* Keep this in sync with the above structure definition */ 265 #define NG_GENERIC_TYPELIST_INFO(tiarraytype) { \ 266 { \ 267 { "numtypes", &ng_parse_int32_type }, \ 268 { "typeinfo", (tiarraytype) }, \ 269 { NULL }, \ 270 } \ 271 } 272 273 /* 274 * For netgraph nodes that are somehow associated with file descriptors 275 * (e.g., a device that has a /dev entry and is also a netgraph node), 276 * we define a generic ioctl for requesting the corresponding nodeinfo 277 * structure and for assigning a name (if there isn't one already). 278 * 279 * For these to you need to also #include <sys/ioccom.h>. 280 */ 281 282 #define NGIOCGINFO _IOR('N', 40, struct nodeinfo) /* get node info */ 283 #define NGIOCSETNAME _IOW('N', 41, struct ngm_name) /* set node name */ 284 285 #ifdef _KERNEL 286 /* 287 * Allocate and initialize a netgraph message "msg" with "len" 288 * extra bytes of argument. Sets "msg" to NULL if fails. 289 * Does not initialize token. 290 */ 291 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how) \ 292 do { \ 293 MALLOC((msg), struct ng_mesg *, sizeof(struct ng_mesg) \ 294 + (len), M_NETGRAPH, (how)); \ 295 if ((msg) == NULL) \ 296 break; \ 297 bzero((msg), sizeof(struct ng_mesg) + (len)); \ 298 (msg)->header.version = NG_VERSION; \ 299 (msg)->header.typecookie = (cookie); \ 300 (msg)->header.cmd = (cmdid); \ 301 (msg)->header.arglen = (len); \ 302 strncpy((msg)->header.cmdstr, #cmdid, \ 303 sizeof((msg)->header.cmdstr) - 1); \ 304 } while (0) 305 306 /* 307 * Allocate and initialize a response "rsp" to a message "msg" 308 * with "len" extra bytes of argument. Sets "rsp" to NULL if fails. 309 */ 310 #define NG_MKRESPONSE(rsp, msg, len, how) \ 311 do { \ 312 MALLOC((rsp), struct ng_mesg *, sizeof(struct ng_mesg) \ 313 + (len), M_NETGRAPH, (how)); \ 314 if ((rsp) == NULL) \ 315 break; \ 316 bzero((rsp), sizeof(struct ng_mesg) + (len)); \ 317 (rsp)->header.version = NG_VERSION; \ 318 (rsp)->header.arglen = (len); \ 319 (rsp)->header.token = (msg)->header.token; \ 320 (rsp)->header.typecookie = (msg)->header.typecookie; \ 321 (rsp)->header.cmd = (msg)->header.cmd; \ 322 bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr, \ 323 sizeof((rsp)->header.cmdstr)); \ 324 (rsp)->header.flags |= NGF_RESP; \ 325 } while (0) 326 #endif /* _KERNEL */ 327 328 #endif /* _NETGRAPH_NG_MESSAGE_H_ */ 329 330