]> The Tcpdump Group git mirrors - libpcap/blob - rpcap-protocol.h
Further emphasize "don't change these structure layouts".
[libpcap] / rpcap-protocol.h
1 /*
2 * Copyright (c) 2002 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2008 CACE Technologies, Davis (California)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #ifndef __RPCAP_PROTOCOL_H__
35 #define __RPCAP_PROTOCOL_H__
36
37 #define RPCAP_DEFAULT_NETPORT "2002" /* Default port on which the RPCAP daemon is waiting for connections. */
38 /* Default port on which the client workstation is waiting for connections in case of active mode. */
39 #define RPCAP_DEFAULT_NETPORT_ACTIVE "2003"
40 #define RPCAP_DEFAULT_NETADDR "" /* Default network address on which the RPCAP daemon binds to. */
41
42 /*
43 * Minimum and maximum supported versions of the protocol.
44 *
45 * If new message types are added, the protocol version MUST be changed,
46 * so that a client knows, from the negotiated protocol version, what
47 * messages can be sent to the server.
48 *
49 * If the format of an existing message type is changed, the protocol
50 * version MUST be changed, so that each side knows, from the negotiated
51 * protocol version, what format should be used.
52 *
53 * The RPCAP_MSG_ERROR format MUST not change, as it's used to, among
54 * other things, report "incorrect version number" errors, where, if
55 * the format changed, the sender of the message might not know what
56 * versions the recipient would understand, or might know a version
57 * they support (the version number they sent) but might not know
58 * the format of the message in that version.
59 *
60 * Other message versions SHOULD not change, as that would complicate
61 * the process of interpreting the message, making it version-dependent.
62 * Introducing a new message with a new format is preferable.
63 *
64 * Version negotiation is done as part of the authentication process:
65 *
66 * The client sends an authentication request, with the version number
67 * in the request being the maximum version it supports.
68 *
69 * If the server supports that version, it attempts to authenticate the
70 * client, and replies as appropriate, with the version number in the
71 * reply being that version.
72 *
73 * If the server doesn't support that version because it's too large,
74 * it replies with a RPCAP_MSG_ERROR message, with the maximum version
75 * they support as the version number in the reply, and with the error
76 * code being PCAP_ERR_WRONGVER.
77 *
78 * If the server doesn't support that version because it's too small,
79 * it replies with a RPCAP_MSG_ERROR message, with that version as
80 * the version number in the reply, and with the error code being
81 * PCAP_ERR_WRONGVER.
82 *
83 * If the client supports that version, it retries the authentication
84 * with that version and, if that fails for any reason, including
85 * PCAP_ERR_WRONGVER, fails. Otherwise, it fails, telling its caller
86 * that there's no version that both support.
87 *
88 * This requires that the set of versions supported by a client or
89 * server be a range of integers, with no gaps. Thus:
90 *
91 * the client's version set is [Cmin, Cmax], with Cmin <= Cmax;
92 *
93 * the server's version set is [Smin, Smax], with Smin <= Smax;
94 *
95 * the client sends Cmax as the version number in the initial
96 * authentication request;
97 *
98 * if the server doesn't support the version sent by the client,
99 * either Smax < Cmax or Smin > Cmax (because the client sent Cmax
100 * to the server, and the server doesn't support it);
101 *
102 * if Smax < Cmax:
103 *
104 * the server sends Smax as the version number in the RPCAP_MSG_ERROR/
105 * PCAP_ERR_WRONGVER message - the client will accept this because
106 * Cmax != 0, as these numbers are unsigned, and this means that
107 * this isn't an old client that rejects all messages with a non-zero
108 * version number, it's a new client that accepts RPCAP_MSG_ERROR
109 * messages no matter what the version is;
110 *
111 * if Smax >= Cmin, both the client and the server can use it, and
112 * the client retries with Smax;
113 *
114 * if Smax < Cmin, there is no version the client and server can
115 * both support.
116 *
117 * if Smin > Cmax:
118 *
119 * the server sends Cmax as the version number in the RPCAP_MSG_ERROR/
120 * PCAP_ERR_WRONGVER message - the client will accept this because
121 * Cmax is a valid client version number.
122 *
123 * the client will retry with Cmax, get the same version failure,
124 * and report that there is no version the client and server can
125 * both support (as the version sets are disjoint).
126 *
127 * Old negotiation-unaware clients just send version 0 and, if they
128 * get back PCAP_ERR_WRONGVER, treat it as a fatal error. This
129 * means they'll fail to talk to any server that can't handle
130 * version 0, which is the appropriate thing to do, as they can
131 * only use version 0.
132 *
133 * Old negotiation-unaware servers fail if they get a version other
134 * than 0, sending back PCAP_ERR_WRONGVER with version 0, which is
135 * the only version, and thus both the minimum and maximum version,
136 * they support. The client will either fail if it doesn't support
137 * version 0, or will retry with version 0 and succeed, so it will
138 * fail with servers that can't handle version 0 or will negotiate
139 * version 0 with servers that can handle version 0.
140 */
141 #define RPCAP_MIN_VERSION 0
142 #define RPCAP_MAX_VERSION 0
143
144 /*
145 * So that the server can distinguish between a non-TLS rpcapd
146 * message, the first byte of which is the version number, and
147 * a TLS client hello, the first byte of which is 22, we don't
148 * allow 22 as an rpcap version number.
149 */
150 #define RPCAP_UNUSED_VERSION 22
151
152 /*
153 * Make sure nobody makes the mistake of making the "must not use" version
154 * the minimum or maximum version; we *must* allow at least one version
155 * other than that version.
156 */
157 #if RPCAP_MIN_VERSION == RPCAP_UNUSED_VERSION
158 #error Minimum protocol version is a version that must not be used
159 #elif RPCAP_MAX_VERSION == RPCAP_UNUSED_VERSION
160 #error Maximum protocol version is a version that must not be used
161 #endif
162
163 /*
164 * Version numbers are unsigned, so if RPCAP_MIN_VERSION is 0, they
165 * are >= the minimum version, by definition; don't check against
166 * RPCAP_MIN_VERSION, as you may get compiler warnings that the
167 * comparison will always succeed.
168 *
169 * We *never* allow version RPCAP_UNUSED_VERSION, even if it's
170 * otherwise in the allowed range.
171 */
172 #if RPCAP_MIN_VERSION == 0
173 #define RPCAP_VERSION_IS_SUPPORTED(v) \
174 ((v) <= RPCAP_MAX_VERSION && (v) != RPCAP_UNUSED_VERSION)
175 #else
176 #define RPCAP_VERSION_IS_SUPPORTED(v) \
177 ((v) >= RPCAP_MIN_VERSION && (v) <= RPCAP_MAX_VERSION && \
178 (v) != RPCAP_UNUSED_VERSION)
179 #endif
180
181 /*
182 * Separators used for the host list.
183 *
184 * It is used:
185 * - by the rpcapd daemon, when you types a list of allowed connecting hosts
186 * - by the rpcap client in active mode, when the client waits for incoming
187 * connections from other hosts
188 */
189 #define RPCAP_HOSTLIST_SEP " ,;\n\r"
190
191 /*********************************************************
192 * *
193 * Protocol messages formats *
194 * *
195 *********************************************************/
196 /*
197 * WARNING: This file defines some structures that are used to transfer
198 * data on the network.
199 * Note that your compiler MUST not insert padding into these structures
200 * for better alignment.
201 * These structures have been created in order to be correctly aligned to
202 * a 32-bit boundary, but be careful in any case.
203 *
204 * The layout of these structures MUST not be changed. If a packet
205 * format is different in different versions of the protocol, versions
206 * of the structure should be provided for all the different versions or
207 * version ranges (if more than one version of the protocol has the same
208 * layout) that we support.
209 */
210
211 /*
212 * WARNING: These typedefs MUST be of a specific size.
213 * You might have to change them on your platform.
214 *
215 * XXX - use the C99 types? Microsoft's newer versions of Visual Studio
216 * support them.
217 */
218 typedef unsigned char uint8; /* 8-bit unsigned integer */
219 typedef unsigned short uint16; /* 16-bit unsigned integer */
220 typedef unsigned int uint32; /* 32-bit unsigned integer */
221 typedef int int32; /* 32-bit signed integer */
222
223 /* Common header for all the RPCAP messages */
224 struct rpcap_header
225 {
226 uint8 ver; /* RPCAP version number */
227 uint8 type; /* RPCAP message type (error, findalldevs, ...) */
228 uint16 value; /* Message-dependent value (not always used) */
229 uint32 plen; /* Length of the payload of this RPCAP message */
230 };
231
232 /* Format of the message for the interface description (findalldevs command) */
233 struct rpcap_findalldevs_if
234 {
235 uint16 namelen; /* Length of the interface name */
236 uint16 desclen; /* Length of the interface description */
237 uint32 flags; /* Interface flags */
238 uint16 naddr; /* Number of addresses */
239 uint16 dummy; /* Must be zero */
240 };
241
242 /*
243 * Format of an address as sent over the wire.
244 *
245 * Do *NOT* use struct sockaddr_storage, as the layout for that is
246 * machine-dependent.
247 *
248 * RFC 2553 gives two sample layouts, both of which are 128 bytes long,
249 * both of which are aligned on an 8-byte boundary, and both of which
250 * have 2 bytes before the address data.
251 *
252 * However, one has a 2-byte address family value at the beginning
253 * and the other has a 1-byte address length value and a 1-byte
254 * address family value; this reflects the fact that the original
255 * BSD sockaddr structure had a 2-byte address family value, which
256 * was later changed to a 1-byte address length value and a 1-byte
257 * address family value, when support for variable-length OSI
258 * network-layer addresses was added.
259 *
260 * Furthermore, Solaris's struct sockaddr_storage is 256 bytes
261 * long.
262 *
263 * This structure is supposed to be aligned on an 8-byte boundary;
264 * the message header is 8 bytes long, so we don't have to do
265 * anything to ensure it's aligned on that boundary within a packet,
266 * so we just define it as 128 bytes long, with a 2-byte address
267 * family. (We only support IPv4 and IPv6 addresses, which are fixed-
268 * length.) That way, it's the same size as sockaddr_storage on
269 * Windows, and it'll look like what an older Windows client will
270 * expect.
271 *
272 * In addition, do *NOT* use the host's AF_ value for an address,
273 * as the value for AF_INET6 is machine-dependent. We use the
274 * Windows value, so it'll look like what an older Windows client
275 * will expect.
276 *
277 * (The Windows client is the only one that has been distributed
278 * as a standard part of *pcap; UN*X clients are probably built
279 * from source by the user or administrator, so they're in a
280 * better position to upgrade an old client. Therefore, we
281 * try to make what goes over the wire look like what comes
282 * from a Windows server.)
283 */
284 struct rpcap_sockaddr
285 {
286 uint16 family; /* Address family */
287 char data[128-2]; /* Data */
288 };
289
290 /*
291 * Format of an IPv4 address as sent over the wire.
292 */
293 #define RPCAP_AF_INET 2 /* Value on all OSes */
294 struct rpcap_sockaddr_in
295 {
296 uint16 family; /* Address family */
297 uint16 port; /* Port number */
298 uint32 addr; /* IPv4 address */
299 uint8 zero[8]; /* Padding */
300 };
301
302 /*
303 * Format of an IPv6 address as sent over the wire.
304 */
305 #define RPCAP_AF_INET6 23 /* Value on Windows */
306 struct rpcap_sockaddr_in6
307 {
308 uint16 family; /* Address family */
309 uint16 port; /* Port number */
310 uint32 flowinfo; /* IPv6 flow information */
311 uint8 addr[16]; /* IPv6 address */
312 uint32 scope_id; /* Scope zone index */
313 };
314
315 /* Format of the message for the address listing (findalldevs command) */
316 struct rpcap_findalldevs_ifaddr
317 {
318 struct rpcap_sockaddr addr; /* Network address */
319 struct rpcap_sockaddr netmask; /* Netmask for that address */
320 struct rpcap_sockaddr broadaddr; /* Broadcast address for that address */
321 struct rpcap_sockaddr dstaddr; /* P2P destination address for that address */
322 };
323
324 /*
325 * \brief Format of the message of the connection opening reply (open command).
326 *
327 * This structure transfers over the network some of the values useful on the client side.
328 */
329 struct rpcap_openreply
330 {
331 int32 linktype; /* Link type */
332 int32 tzoff; /* Timezone offset - not used by newer clients */
333 };
334
335 /* Format of the message that starts a remote capture (startcap command) */
336 struct rpcap_startcapreq
337 {
338 uint32 snaplen; /* Length of the snapshot (number of bytes to capture for each packet) */
339 uint32 read_timeout; /* Read timeout in milliseconds */
340 uint16 flags; /* Flags (see RPCAP_STARTCAPREQ_FLAG_xxx) */
341 uint16 portdata; /* Network port on which the client is waiting at (if 'serveropen') */
342 };
343
344 /* Format of the reply message that devoted to start a remote capture (startcap reply command) */
345 struct rpcap_startcapreply
346 {
347 int32 bufsize; /* Size of the user buffer allocated by WinPcap; it can be different from the one we chose */
348 uint16 portdata; /* Network port on which the server is waiting at (passive mode only) */
349 uint16 dummy; /* Must be zero */
350 };
351
352 /*
353 * \brief Format of the header which encapsulates captured packets when transmitted on the network.
354 *
355 * This message requires the general header as well, since we want to be able to exchange
356 * more information across the network in the future (for example statistics, and kind like that).
357 */
358 struct rpcap_pkthdr
359 {
360 /*
361 * This protocol needs to be updated with a new version before
362 * 2038-01-19 03:14:07 UTC.
363 */
364 uint32 timestamp_sec; /* 'struct timeval' compatible, it represents the 'tv_sec' field */
365 uint32 timestamp_usec; /* 'struct timeval' compatible, it represents the 'tv_usec' field */
366 uint32 caplen; /* Length of portion present in the capture */
367 uint32 len; /* Real length this packet (off wire) */
368 uint32 npkt; /* Ordinal number of the packet (i.e. the first one captured has '1', the second one '2', etc) */
369 };
370
371 /* General header used for the pcap_setfilter() command; keeps just the number of BPF instructions */
372 struct rpcap_filter
373 {
374 uint16 filtertype; /* type of the filter transferred (BPF instructions, ...) */
375 uint16 dummy; /* Must be zero */
376 uint32 nitems; /* Number of items contained into the filter (e.g. BPF instructions for BPF filters) */
377 };
378
379 /* Structure that keeps a single BPF instuction; it is repeated 'ninsn' times according to the 'rpcap_filterbpf' header */
380 struct rpcap_filterbpf_insn
381 {
382 uint16 code; /* opcode of the instruction */
383 uint8 jt; /* relative offset to jump to in case of 'true' */
384 uint8 jf; /* relative offset to jump to in case of 'false' */
385 int32 k; /* instruction-dependent value */
386 };
387
388 /* Structure that keeps the data required for the authentication on the remote host */
389 struct rpcap_auth
390 {
391 uint16 type; /* Authentication type */
392 uint16 dummy; /* Must be zero */
393 uint16 slen1; /* Length of the first authentication item (e.g. username) */
394 uint16 slen2; /* Length of the second authentication item (e.g. password) */
395 };
396
397 /* Structure that keeps the statistics about the number of packets captured, dropped, etc. */
398 struct rpcap_stats
399 {
400 uint32 ifrecv; /* Packets received by the kernel filter (i.e. pcap_stats.ps_recv) */
401 uint32 ifdrop; /* Packets dropped by the network interface (e.g. not enough buffers) (i.e. pcap_stats.ps_ifdrop) */
402 uint32 krnldrop; /* Packets dropped by the kernel filter (i.e. pcap_stats.ps_drop) */
403 uint32 svrcapt; /* Packets captured by the RPCAP daemon and sent on the network */
404 };
405
406 /* Structure that is needed to set sampling parameters */
407 struct rpcap_sampling
408 {
409 uint8 method; /* Sampling method */
410 uint8 dummy1; /* Must be zero */
411 uint16 dummy2; /* Must be zero */
412 uint32 value; /* Parameter related to the sampling method */
413 };
414
415 /*
416 * Messages field coding.
417 *
418 * These values are used in messages sent over the network, and MUST
419 * not be changed.
420 */
421 #define RPCAP_MSG_IS_REPLY 0x080 /* Flag indicating a reply */
422
423 #define RPCAP_MSG_ERROR 1 /* Message that keeps an error notification */
424 #define RPCAP_MSG_FINDALLIF_REQ 2 /* Request to list all the remote interfaces */
425 #define RPCAP_MSG_OPEN_REQ 3 /* Request to open a remote device */
426 #define RPCAP_MSG_STARTCAP_REQ 4 /* Request to start a capture on a remote device */
427 #define RPCAP_MSG_UPDATEFILTER_REQ 5 /* Send a compiled filter into the remote device */
428 #define RPCAP_MSG_CLOSE 6 /* Close the connection with the remote peer */
429 #define RPCAP_MSG_PACKET 7 /* This is a 'data' message, which carries a network packet */
430 #define RPCAP_MSG_AUTH_REQ 8 /* Message that keeps the authentication parameters */
431 #define RPCAP_MSG_STATS_REQ 9 /* It requires to have network statistics */
432 #define RPCAP_MSG_ENDCAP_REQ 10 /* Stops the current capture, keeping the device open */
433 #define RPCAP_MSG_SETSAMPLING_REQ 11 /* Set sampling parameters */
434
435 #define RPCAP_MSG_FINDALLIF_REPLY (RPCAP_MSG_FINDALLIF_REQ | RPCAP_MSG_IS_REPLY) /* Keeps the list of all the remote interfaces */
436 #define RPCAP_MSG_OPEN_REPLY (RPCAP_MSG_OPEN_REQ | RPCAP_MSG_IS_REPLY) /* The remote device has been opened correctly */
437 #define RPCAP_MSG_STARTCAP_REPLY (RPCAP_MSG_STARTCAP_REQ | RPCAP_MSG_IS_REPLY) /* The capture is starting correctly */
438 #define RPCAP_MSG_UPDATEFILTER_REPLY (RPCAP_MSG_UPDATEFILTER_REQ | RPCAP_MSG_IS_REPLY) /* The filter has been applied correctly on the remote device */
439 #define RPCAP_MSG_AUTH_REPLY (RPCAP_MSG_AUTH_REQ | RPCAP_MSG_IS_REPLY) /* Sends a message that says 'ok, authorization successful' */
440 #define RPCAP_MSG_STATS_REPLY (RPCAP_MSG_STATS_REQ | RPCAP_MSG_IS_REPLY) /* Message that keeps the network statistics */
441 #define RPCAP_MSG_ENDCAP_REPLY (RPCAP_MSG_ENDCAP_REQ | RPCAP_MSG_IS_REPLY) /* Confirms that the capture stopped successfully */
442 #define RPCAP_MSG_SETSAMPLING_REPLY (RPCAP_MSG_SETSAMPLING_REQ | RPCAP_MSG_IS_REPLY) /* Confirms that the capture stopped successfully */
443
444 #define RPCAP_STARTCAPREQ_FLAG_PROMISC 0x00000001 /* Enables promiscuous mode (default: disabled) */
445 #define RPCAP_STARTCAPREQ_FLAG_DGRAM 0x00000002 /* Use a datagram (i.e. UDP) connection for the data stream (default: use TCP)*/
446 #define RPCAP_STARTCAPREQ_FLAG_SERVEROPEN 0x00000004 /* The server has to open the data connection toward the client */
447 #define RPCAP_STARTCAPREQ_FLAG_INBOUND 0x00000008 /* Capture only inbound packets (take care: the flag has no effect with promiscuous enabled) */
448 #define RPCAP_STARTCAPREQ_FLAG_OUTBOUND 0x00000010 /* Capture only outbound packets (take care: the flag has no effect with promiscuous enabled) */
449
450 #define RPCAP_UPDATEFILTER_BPF 1 /* This code tells us that the filter is encoded with the BPF/NPF syntax */
451
452 /*
453 * Network error codes.
454 *
455 * These values are used in messages sent over the network, and MUST
456 * not be changed.
457 */
458 #define PCAP_ERR_NETW 1 /* Network error */
459 #define PCAP_ERR_INITTIMEOUT 2 /* The RPCAP initial timeout has expired */
460 #define PCAP_ERR_AUTH 3 /* Generic authentication error */
461 #define PCAP_ERR_FINDALLIF 4 /* Generic findalldevs error */
462 #define PCAP_ERR_NOREMOTEIF 5 /* The findalldevs was ok, but the remote end had no interfaces to list */
463 #define PCAP_ERR_OPEN 6 /* Generic pcap_open error */
464 #define PCAP_ERR_UPDATEFILTER 7 /* Generic updatefilter error */
465 #define PCAP_ERR_GETSTATS 8 /* Generic pcap_stats error */
466 #define PCAP_ERR_READEX 9 /* Generic pcap_next_ex error */
467 #define PCAP_ERR_HOSTNOAUTH 10 /* The host is not authorized to connect to this server */
468 #define PCAP_ERR_REMOTEACCEPT 11 /* Generic pcap_remoteaccept error */
469 #define PCAP_ERR_STARTCAPTURE 12 /* Generic pcap_startcapture error */
470 #define PCAP_ERR_ENDCAPTURE 13 /* Generic pcap_endcapture error */
471 #define PCAP_ERR_RUNTIMETIMEOUT 14 /* The RPCAP run-time timeout has expired */
472 #define PCAP_ERR_SETSAMPLING 15 /* Error during the settings of sampling parameters */
473 #define PCAP_ERR_WRONGMSG 16 /* The other end endpoint sent a message which has not been recognized */
474 #define PCAP_ERR_WRONGVER 17 /* The other end endpoint has a version number that is not compatible with our */
475
476 /*
477 * \brief Buffer used by socket functions to send-receive packets.
478 * In case you plan to have messages larger than this value, you have to increase it.
479 */
480 #define RPCAP_NETBUF_SIZE 64000
481
482 /*********************************************************
483 * *
484 * Routines used by the rpcap client and rpcap daemon *
485 * *
486 *********************************************************/
487
488 #include "sockutils.h"
489 #include "sslutils.h"
490
491 extern void rpcap_createhdr(struct rpcap_header *header, uint8 ver, uint8 type, uint16 value, uint32 length);
492 extern const char *rpcap_msg_type_string(uint8 type);
493 extern int rpcap_senderror(SOCKET sock, SSL *ssl, uint8 ver, uint16 errcode, const char *error, char *errbuf);
494
495 #endif