xref: /freebsd/sys/netgraph/ng_message.h (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 #define NG_VERSION 1
69 #define NGF_ORIG	0x0000		/* the msg is the original request */
70 #define NGF_RESP	0x0001		/* the message is a response */
71 
72 /*
73  * Here we describe the "generic" messages that all nodes inherently
74  * understand. With the exception of NGM_TEXT_STATUS, these are handled
75  * automatically by the base netgraph code.
76  */
77 
78 /* Generic message type cookie */
79 #define NGM_GENERIC_COOKIE 851672668
80 
81 /* Generic messages defined for this type cookie */
82 #define	NGM_SHUTDOWN	0x0001 	/* no args */
83 #define NGM_MKPEER	0x0002
84 #define NGM_CONNECT	0x0003
85 #define NGM_NAME	0x0004
86 #define NGM_RMHOOK	0x0005
87 #define	NGM_NODEINFO	0x0006	/* get nodeinfo for the target */
88 #define	NGM_LISTHOOKS	0x0007	/* get nodeinfo for the target + hook info */
89 #define	NGM_LISTNAMES	0x0008	/* list all globally named nodes */
90 #define	NGM_LISTNODES	0x0009	/* list all nodes, named and unnamed */
91 #define	NGM_LISTTYPES	0x000a	/* list all installed node types */
92 #define	NGM_TEXT_STATUS	0x000b	/* (optional) returns human readable status */
93 
94 /*
95  * Args sections for generic NG commands. All strings are NUL-terminated.
96  */
97 struct ngm_mkpeer {
98 	char	type[NG_TYPELEN + 1];			/* peer type */
99 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
100 	char	peerhook[NG_HOOKLEN + 1];		/* peer hook name */
101 };
102 
103 struct ngm_connect {
104 	char	path[NG_PATHLEN + 1];			/* peer path */
105 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
106 	char	peerhook[NG_HOOKLEN + 1];		/* peer hook name */
107 };
108 
109 struct ngm_name {
110 	char	name[NG_NODELEN + 1];			/* node name */
111 };
112 
113 struct ngm_rmhook {
114 	char	ourhook[NG_HOOKLEN + 1];		/* hook name */
115 };
116 
117 /* Structures used in response to NGM_NODEINFO and NGM_LISTHOOKS */
118 struct nodeinfo {
119 	char		name[NG_NODELEN + 1];	/* node name (if any) */
120         char    	type[NG_TYPELEN + 1];   /* peer type */
121 	u_int32_t	id;			/* unique identifier */
122 	u_int32_t	hooks;			/* number of active hooks */
123 };
124 
125 struct linkinfo {
126 	char		ourhook[NG_HOOKLEN + 1];	/* hook name */
127 	char		peerhook[NG_HOOKLEN + 1];	/* peer hook */
128 	struct nodeinfo	nodeinfo;
129 };
130 
131 struct hooklist {
132 	struct nodeinfo nodeinfo;		/* node information */
133 	struct linkinfo link[0];		/* info about each hook */
134 };
135 
136 /* Structure used for NGM_LISTNAMES/NGM_LISTNODES (not node specific) */
137 struct namelist {
138 	u_int32_t	numnames;
139 	struct nodeinfo	nodeinfo[0];
140 };
141 
142 /* Structures used for NGM_LISTTYPES (not node specific) */
143 struct typeinfo {
144 	char		typename[NG_TYPELEN + 1];	/* name of type */
145 	u_int32_t	numnodes;			/* number alive */
146 };
147 
148 struct typelist {
149 	u_int32_t	numtypes;
150 	struct typeinfo	typeinfo[0];
151 };
152 
153 /*
154  * For netgraph nodes that are somehow associated with file descriptors
155  * (e.g., a device that has a /dev entry and is also a netgraph node),
156  * we define a generic ioctl for requesting the corresponding nodeinfo
157  * structure and for assigning a name (if there isn't one already).
158  *
159  * For these to you need to also #include <sys/ioccom.h>.
160  */
161 
162 #define NGIOCGINFO	_IOR('N', 40, struct nodeinfo)	/* get node info */
163 #define NGIOCSETNAME	_IOW('N', 41, struct ngm_name)	/* set node name */
164 
165 #ifdef KERNEL
166 /*
167  * Allocate and initialize a netgraph message "msg" with "len"
168  * extra bytes of argument. Sets "msg" to NULL if fails.
169  * Does not initialize token.
170  */
171 #define NG_MKMESSAGE(msg, cookie, cmdid, len, how)			\
172 	do {								\
173 	  MALLOC((msg), struct ng_mesg *, sizeof(struct ng_mesg)	\
174 	    + (len), M_NETGRAPH, (how));				\
175 	  if ((msg) == NULL)						\
176 	    break;							\
177 	  bzero((msg), sizeof(struct ng_mesg) + (len));			\
178 	  (msg)->header.version = NG_VERSION;				\
179 	  (msg)->header.typecookie = (cookie);				\
180 	  (msg)->header.cmd = (cmdid);					\
181 	  (msg)->header.arglen = (len);					\
182 	  strncpy((msg)->header.cmdstr, #cmdid,				\
183 	    sizeof((msg)->header.cmdstr) - 1);				\
184 	} while (0)
185 
186 /*
187  * Allocate and initialize a response "rsp" to a message "msg"
188  * with "len" extra bytes of argument. Sets "rsp" to NULL if fails.
189  */
190 #define NG_MKRESPONSE(rsp, msg, len, how)				\
191 	do {								\
192 	  MALLOC((rsp), struct ng_mesg *, sizeof(struct ng_mesg)	\
193 	    + (len), M_NETGRAPH, (how));				\
194 	  if ((rsp) == NULL)						\
195 	    break;							\
196 	  bzero((rsp), sizeof(struct ng_mesg) + (len));			\
197 	  (rsp)->header.version = NG_VERSION;				\
198 	  (rsp)->header.arglen = (len);					\
199 	  (rsp)->header.token = (msg)->header.token;			\
200 	  (rsp)->header.typecookie = (msg)->header.typecookie;		\
201 	  (rsp)->header.cmd = (msg)->header.cmd;			\
202 	  bcopy((msg)->header.cmdstr, (rsp)->header.cmdstr,		\
203 	    sizeof((rsp)->header.cmdstr));				\
204 	  (rsp)->header.flags |= NGF_RESP;				\
205 	} while (0)
206 #endif /* KERNEL */
207 
208 #endif /* _NETGRAPH_NG_MESSAGE_H_ */
209 
210