VirtualBox

source: vbox/trunk/include/iprt/net.h@ 55969

Last change on this file since 55969 was 52543, checked in by vboxsync, 10 years ago

IntNet: follow up to r85262 - refactor rewriting of link-level address
option in IPv6 Neighbor Discovery datagrams when bridging to wireless.

Ok bird@

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.4 KB
Line 
1/** @file
2 * IPRT - Network Protocols.
3 */
4
5/*
6 * Copyright (C) 2008-2013 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_net_h
27#define ___iprt_net_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/assert.h>
32
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_net RTNet - Network Protocols
37 * @ingroup grp_rt
38 * @{
39 */
40
41/**
42 * Converts an stringified Ethernet MAC address into the RTMAC representation.
43 *
44 * @todo This should be move to some generic part of the runtime.
45 *
46 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
47 * failure.
48 *
49 * @param pszValue The value to convert.
50 * @param pAddr Where to store the result.
51 */
52RTDECL(int) RTNetStrToMacAddr(const char *pszAddr, PRTMAC pMacAddr);
53
54/**
55 * IPv4 address.
56 */
57typedef RTUINT32U RTNETADDRIPV4;
58AssertCompileSize(RTNETADDRIPV4, 4);
59/** Pointer to a IPv4 address. */
60typedef RTNETADDRIPV4 *PRTNETADDRIPV4;
61/** Pointer to a const IPv4 address. */
62typedef RTNETADDRIPV4 const *PCRTNETADDRIPV4;
63
64/**
65 * Tests if the given string is an IPv4 address.
66 *
67 * @returns boolean.
68 * @param pcszAddr String which may be an IPv4 address.
69 */
70RTDECL(bool) RTNetIsIPv4AddrStr(const char *pcszAddr);
71
72/**
73 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
74 *
75 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
76 * failure.
77 *
78 * @param pcszAddr The value to convert.
79 * @param ppszNext Where to store the pointer to the first char
80 * following the address. (Optional)
81 * @param pAddr Where to store the result.
82 */
83RTDECL(int) RTNetStrToIPv4AddrEx(const char *pcszAddr, PRTNETADDRIPV4 pAddr, char **ppszNext);
84
85/**
86 * Parses dotted-decimal IPv4 address into RTNETADDRIPV4 representation.
87 * Leading and trailing whitespace is ignored.
88 *
89 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
90 * failure.
91 *
92 * @param pcszAddr The value to convert.
93 * @param pAddr Where to store the result.
94 */
95RTDECL(int) RTNetStrToIPv4Addr(const char *pcszAddr, PRTNETADDRIPV4 pAddr);
96
97/**
98 * IPv6 address.
99 */
100typedef RTUINT128U RTNETADDRIPV6;
101AssertCompileSize(RTNETADDRIPV6, 16);
102/** Pointer to a IPv6 address. */
103typedef RTNETADDRIPV6 *PRTNETADDRIPV6;
104/** Pointer to a const IPv6 address. */
105typedef RTNETADDRIPV6 const *PCRTNETADDRIPV6;
106
107/**
108 * Tests if the given string is a valid IPv6 address.
109 *
110 * @returns @c true if it is, @c false if not.
111 * @param pszAddress String which may be an IPv6 address.
112 */
113RTDECL(bool) RTNetIsIPv6AddrStr(const char *pszAddress);
114
115/**
116 * Parses IPv6 address into RTNETADDRIPV6 representation.
117 *
118 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
119 * failure.
120 *
121 * @param pcszAddr The value to convert.
122 * @param ppszNext Where to store the pointer to the first char
123 * following the address. (Optional)
124 * @param pAddr Where to store the result.
125 */
126RTDECL(int) RTNetStrToIPv6AddrEx(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszNext);
127
128/**
129 * Parses IPv6 address into RTNETADDRIPV6 representation.
130 * Leading and trailing whitespace is ignored.
131 *
132 * @returns VINF_SUCCESS on success, VERR_INVALID_PARAMETER on
133 * failure.
134 *
135 * @param pcszAddr The value to convert.
136 * @param ppszZone Where to store the pointer to the first char
137 * of the zone id. NULL is stored if there is
138 * no zone id.
139 * @param pAddr Where to store the result.
140 */
141RTDECL(int) RTNetStrToIPv6Addr(const char *pcszAddr, PRTNETADDRIPV6 pAddr, char **ppszZone);
142
143/**
144 * IPX address.
145 */
146#pragma pack(1)
147typedef struct RTNETADDRIPX
148{
149 /** The network ID. */
150 uint32_t Network;
151 /** The node ID. (Defaults to the MAC address apparently.) */
152 RTMAC Node;
153} RTNETADDRIPX;
154#pragma pack()
155AssertCompileSize(RTNETADDRIPX, 4+6);
156/** Pointer to an IPX address. */
157typedef RTNETADDRIPX *PRTNETADDRIPX;
158/** Pointer to a const IPX address. */
159typedef RTNETADDRIPX const *PCRTNETADDRIPX;
160
161/**
162 * Network address union.
163 *
164 * @remarks The size of this structure may change in the future.
165 */
166typedef union RTNETADDRU
167{
168 /** 64-bit view. */
169 uint64_t au64[2];
170 /** 32-bit view. */
171 uint32_t au32[4];
172 /** 16-bit view. */
173 uint16_t au16[8];
174 /** 8-bit view. */
175 uint8_t au8[16];
176 /** IPv4 view. */
177 RTNETADDRIPV4 IPv4;
178#ifndef IPv6 /* Work around X11 and RDP defining IPv6 to 1. */
179 /** IPv6 view. */
180 RTNETADDRIPV6 IPv6;
181#endif
182 /** IPX view. */
183 RTNETADDRIPX Ipx;
184 /** MAC address view. */
185 RTMAC Mac;
186} RTNETADDRU;
187AssertCompileSize(RTNETADDRU, 16);
188/** Pointer to an address union. */
189typedef RTNETADDRU *PRTNETADDRU;
190/** Pointer to a const address union. */
191typedef RTNETADDRU const *PCRTNETADDRU;
192
193/**
194 * Network address type.
195 *
196 * @remarks The value assignments may change in the future.
197 */
198typedef enum RTNETADDRTYPE
199{
200 /** The invalid 0 entry. */
201 RTNETADDRTYPE_INVALID = 0,
202 /** IP version 4. */
203 RTNETADDRTYPE_IPV4,
204 /** IP version 6. */
205 RTNETADDRTYPE_IPV6,
206 /** IPX. */
207 RTNETADDRTYPE_IPX,
208 /** MAC address. */
209 RTNETADDRTYPE_MAC,
210 /** The end of the valid values. */
211 RTNETADDRTYPE_END,
212 /** The usual 32-bit hack. */
213 RTNETADDRTYPE_32_BIT_HACK = 0x7fffffff
214} RTNETADDRTYPE;
215/** Pointer to a network address type. */
216typedef RTNETADDRTYPE *PRTNETADDRTYPE;
217/** Pointer to a const network address type. */
218typedef RTNETADDRTYPE const *PCRTNETADDRTYPE;
219
220/**
221 * Network address.
222 *
223 * @remarks The size and type values may change.
224 */
225typedef struct RTNETADDR
226{
227 /** The address union. */
228 RTNETADDRU uAddr;
229 /** Indicates which view of @a u that is valid. */
230 RTNETADDRTYPE enmType;
231 /** The port number for IPv4 and IPv6 addresses. This is set to
232 * RTNETADDR_NA_PORT if not applicable. */
233 uint32_t uPort;
234} RTNETADDR;
235/** Pointer to a network address. */
236typedef RTNETADDR *PRTNETADDR;
237/** Pointer to a const network address. */
238typedef RTNETADDR const *PCRTNETADDR;
239
240/** The not applicable value of RTNETADDR::uPort value use to inid. */
241#define RTNETADDR_PORT_NA UINT32_MAX
242
243/**
244 * Ethernet header.
245 */
246#pragma pack(1)
247typedef struct RTNETETHERHDR
248{
249 RTMAC DstMac;
250 RTMAC SrcMac;
251 /** Ethernet frame type or frame size, depending on the kind of ethernet.
252 * This is big endian on the wire. */
253 uint16_t EtherType;
254} RTNETETHERHDR;
255#pragma pack()
256AssertCompileSize(RTNETETHERHDR, 14);
257/** Pointer to an ethernet header. */
258typedef RTNETETHERHDR *PRTNETETHERHDR;
259/** Pointer to a const ethernet header. */
260typedef RTNETETHERHDR const *PCRTNETETHERHDR;
261
262/** @name EtherType (RTNETETHERHDR::EtherType)
263 * @{ */
264#define RTNET_ETHERTYPE_IPV4 UINT16_C(0x0800)
265#define RTNET_ETHERTYPE_ARP UINT16_C(0x0806)
266#define RTNET_ETHERTYPE_IPV6 UINT16_C(0x86dd)
267#define RTNET_ETHERTYPE_VLAN UINT16_C(0x8100)
268#define RTNET_ETHERTYPE_IPX_1 UINT16_C(0x8037)
269#define RTNET_ETHERTYPE_IPX_2 UINT16_C(0x8137)
270#define RTNET_ETHERTYPE_IPX_3 UINT16_C(0x8138)
271/** @} */
272
273
274/**
275 * IPv4 header.
276 * All is bigendian on the wire.
277 */
278#pragma pack(1)
279typedef struct RTNETIPV4
280{
281#ifdef RT_BIG_ENDIAN
282 unsigned int ip_v : 4;
283 unsigned int ip_hl : 4;
284 unsigned int ip_tos : 8;
285 unsigned int ip_len : 16;
286#else
287 /** 00:0 - Header length given as a 32-bit word count. */
288 unsigned int ip_hl : 4;
289 /** 00:4 - Header version. */
290 unsigned int ip_v : 4;
291 /** 01 - Type of service. */
292 unsigned int ip_tos : 8;
293 /** 02 - Total length (header + data). */
294 unsigned int ip_len : 16;
295#endif
296 /** 04 - Packet idenficiation. */
297 uint16_t ip_id;
298 /** 06 - Offset if fragmented. */
299 uint16_t ip_off;
300 /** 08 - Time to live. */
301 uint8_t ip_ttl;
302 /** 09 - Protocol. */
303 uint8_t ip_p;
304 /** 0a - Header check sum. */
305 uint16_t ip_sum;
306 /** 0c - Source address. */
307 RTNETADDRIPV4 ip_src;
308 /** 10 - Destination address. */
309 RTNETADDRIPV4 ip_dst;
310 /** 14 - Options (optional). */
311 uint32_t ip_options[1];
312} RTNETIPV4;
313#pragma pack()
314AssertCompileSize(RTNETIPV4, 6 * 4);
315/** Pointer to a IPv4 header. */
316typedef RTNETIPV4 *PRTNETIPV4;
317/** Pointer to a const IPv4 header. */
318typedef RTNETIPV4 const *PCRTNETIPV4;
319
320/** The minimum IPv4 header length (in bytes).
321 * Up to and including RTNETIPV4::ip_dst. */
322#define RTNETIPV4_MIN_LEN (20)
323
324
325/** @name IPv4 Protocol Numbers
326 * @{ */
327/** IPv4: ICMP */
328#define RTNETIPV4_PROT_ICMP (1)
329/** IPv4: TCP */
330#define RTNETIPV4_PROT_TCP (6)
331/** IPv4: UDP */
332#define RTNETIPV4_PROT_UDP (17)
333/** @} */
334
335/** @name Common IPv4 Port Assignments
336 * @{
337 */
338/** Boostrap Protocol / DHCP) Server. */
339#define RTNETIPV4_PORT_BOOTPS (67)
340/** Boostrap Protocol / DHCP) Client. */
341#define RTNETIPV4_PORT_BOOTPC (68)
342/** @} */
343
344/** @name IPv4 Flags
345 * @{ */
346/** IPv4: Don't fragment */
347#define RTNETIPV4_FLAGS_DF (0x4000)
348/** IPv4: More fragments */
349#define RTNETIPV4_FLAGS_MF (0x2000)
350/** @} */
351
352RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr);
353RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax, bool fChecksum);
354RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr);
355RTDECL(uint32_t) RTNetIPv4PseudoChecksumBits(RTNETADDRIPV4 SrcAddr, RTNETADDRIPV4 DstAddr, uint8_t bProtocol, uint16_t cbPkt);
356RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd);
357RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t u32Sum);
358
359
360/**
361 * IPv6 header.
362 * All is bigendian on the wire.
363 */
364#pragma pack(1)
365typedef struct RTNETIPV6
366{
367 /** Version (4 bits), Traffic Class (8 bits) and Flow Lable (20 bits).
368 * @todo this is probably mislabeled - ip6_flow vs. ip6_vfc, fix later. */
369 uint32_t ip6_vfc;
370 /** 04 - Payload length, including extension headers. */
371 uint16_t ip6_plen;
372 /** 06 - Next header type (RTNETIPV4_PROT_XXX). */
373 uint8_t ip6_nxt;
374 /** 07 - Hop limit. */
375 uint8_t ip6_hlim;
376 /** xx - Source address. */
377 RTNETADDRIPV6 ip6_src;
378 /** xx - Destination address. */
379 RTNETADDRIPV6 ip6_dst;
380} RTNETIPV6;
381#pragma pack()
382AssertCompileSize(RTNETIPV6, 8 + 16 + 16);
383/** Pointer to a IPv6 header. */
384typedef RTNETIPV6 *PRTNETIPV6;
385/** Pointer to a const IPv6 header. */
386typedef RTNETIPV6 const *PCRTNETIPV6;
387
388/** The minimum IPv6 header length (in bytes).
389 * Up to and including RTNETIPV6::ip6_dst. */
390#define RTNETIPV6_MIN_LEN (40)
391#define RTNETIPV6_ICMPV6_ND_WITH_LLA_OPT_MIN_LEN (32)
392
393RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr);
394RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt);
395RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
396 uint8_t bProtocol, uint16_t cbPkt);
397
398
399/**
400 * UDP header.
401 */
402#pragma pack(1)
403typedef struct RTNETUDP
404{
405 /** The source port. */
406 uint16_t uh_sport;
407 /** The destination port. */
408 uint16_t uh_dport;
409 /** The length of the UDP header and associated data. */
410 uint16_t uh_ulen;
411 /** The checksum of the pseudo header, the UDP header and the data. */
412 uint16_t uh_sum;
413} RTNETUDP;
414#pragma pack()
415AssertCompileSize(RTNETUDP, 8);
416/** Pointer to an UDP header. */
417typedef RTNETUDP *PRTNETUDP;
418/** Pointer to a const UDP header. */
419typedef RTNETUDP const *PCRTNETUDP;
420
421/** The minimum UDP packet length (in bytes). (RTNETUDP::uh_ulen) */
422#define RTNETUDP_MIN_LEN (8)
423
424RTDECL(uint16_t) RTNetUDPChecksum(uint32_t u32Sum, PCRTNETUDP pUdpHdr);
425RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum);
426RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData);
427RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax);
428RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax, bool fChecksum);
429
430
431/**
432 * IPv4 BOOTP / DHCP packet.
433 */
434#pragma pack(1)
435typedef struct RTNETBOOTP
436{
437 /** 00 - The packet opcode (RTNETBOOTP_OP_*). */
438 uint8_t bp_op;
439 /** 01 - Hardware address type. Same as RTNETARPHDR::ar_htype. */
440 uint8_t bp_htype;
441 /** 02 - Hardware address length. */
442 uint8_t bp_hlen;
443 /** 03 - Gateway hops. */
444 uint8_t bp_hops;
445 /** 04 - Transaction ID. */
446 uint32_t bp_xid;
447 /** 08 - Seconds since boot started. */
448 uint16_t bp_secs;
449 /** 0a - Unused (BOOTP) / Flags (DHCP) (RTNET_DHCP_FLAGS_*). */
450 uint16_t bp_flags;
451 /** 0c - Client IPv4 address. */
452 RTNETADDRIPV4 bp_ciaddr;
453 /** 10 - Your IPv4 address. */
454 RTNETADDRIPV4 bp_yiaddr;
455 /** 14 - Server IPv4 address. */
456 RTNETADDRIPV4 bp_siaddr;
457 /** 18 - Gateway IPv4 address. */
458 RTNETADDRIPV4 bp_giaddr;
459 /** 1c - Client hardware address. */
460 union
461 {
462 uint8_t au8[16];
463 RTMAC Mac;
464 } bp_chaddr;
465 /** 2c - Server name. */
466 uint8_t bp_sname[64];
467 /** 6c - File name / more DHCP options. */
468 uint8_t bp_file[128];
469 /** ec - Vendor specific area (BOOTP) / Options (DHCP).
470 * @remark This is really 312 bytes in the DHCP version. */
471 union
472 {
473 uint8_t au8[128];
474 struct DHCP
475 {
476 /** ec - The DHCP cookie (RTNET_DHCP_COOKIE). */
477 uint32_t dhcp_cookie;
478 /** f0 - The DHCP options. */
479 uint8_t dhcp_opts[124];
480 } Dhcp;
481 } bp_vend;
482
483} RTNETBOOTP;
484#pragma pack()
485AssertCompileSize(RTNETBOOTP, 0xec + 128);
486/** Pointer to a BOOTP / DHCP packet. */
487typedef RTNETBOOTP *PRTNETBOOTP;
488/** Pointer to a const BOOTP / DHCP packet. */
489typedef RTNETBOOTP const *PCRTNETBOOTP;
490
491/** Minimum BOOTP packet length. For quick validation, no standard thing really. */
492#define RTNETBOOTP_MIN_LEN 0xec
493/** Minimum DHCP packet length. For quick validation, no standard thing really. */
494#define RTNETBOOTP_DHCP_MIN_LEN 0xf1
495
496/** The normal size of the a DHCP packet (i.e. a RTNETBOOTP).
497 * Same as RTNET_DHCP_OPT_SIZE, just expressed differently. */
498#define RTNET_DHCP_NORMAL_SIZE (0xec + 4 + RTNET_DHCP_OPT_SIZE)
499/** The normal size of RTNETBOOTP::bp_vend::Dhcp::dhcp_opts. */
500#define RTNET_DHCP_OPT_SIZE (312 - 4)
501
502/** @name BOOTP packet opcode values
503 * @{ */
504#define RTNETBOOTP_OP_REQUEST 1
505#define RTNETBOOTP_OP_REPLY 2
506/** @} */
507
508/** @name DHCP flags (RTNETBOOTP::bp_flags)
509 * @{ */
510#define RTNET_DHCP_FLAGS_NO_BROADCAST UINT16_C(0x8000) /** @todo check test!!! */
511/** @} */
512
513/** The DHCP cookie (network endian). */
514#define RTNET_DHCP_COOKIE UINT32_C(0x63825363)
515
516/**
517 * An IPv4 DHCP option header.
518 */
519typedef struct RTNETDHCPOPT
520{
521 /** 00 - The DHCP option. */
522 uint8_t dhcp_opt;
523 /** 01 - The data length (excluding this header). */
524 uint8_t dhcp_len;
525 /* 02 - The option data follows here, optional and of variable length. */
526} RTNETDHCPOPT;
527AssertCompileSize(RTNETDHCPOPT, 2);
528/** Pointer to a DHCP option header. */
529typedef RTNETDHCPOPT *PRTNETDHCPOPT;
530/** Pointer to a const DHCP option header. */
531typedef RTNETDHCPOPT const *PCRTNETDHCPOPT;
532
533/** @name DHCP options
534 * @{ */
535/** 1 byte padding, this has no dhcp_len field. */
536#define RTNET_DHCP_OPT_PAD 0
537
538/** The subnet mask. */
539#define RTNET_DHCP_OPT_SUBNET_MASK 1
540/** The time offset. */
541#define RTNET_DHCP_OPT_TIME_OFFSET 2
542/** The routers for the subnet. */
543#define RTNET_DHCP_OPT_ROUTERS 3
544/** Domain Name Server. */
545#define RTNET_DHCP_OPT_DNS 6
546/** Host name. */
547#define RTNET_DHCP_OPT_HOST_NAME 12
548/** Domain name. */
549#define RTNET_DHCP_OPT_DOMAIN_NAME 15
550
551/** The requested address. */
552#define RTNET_DHCP_OPT_REQ_ADDR 50
553/** The lease time in seconds. */
554#define RTNET_DHCP_OPT_LEASE_TIME 51
555/** Option overload.
556 * Indicates that the bp_file and/or bp_sname holds contains DHCP options. */
557#define RTNET_DHCP_OPT_OPTION_OVERLOAD 52
558/** Have a 8-bit message type value as data, see RTNET_DHCP_MT_*. */
559#define RTNET_DHCP_OPT_MSG_TYPE 53
560/** Server ID. */
561#define RTNET_DHCP_OPT_SERVER_ID 54
562/** Parameter request list. */
563#define RTNET_DHCP_OPT_PARAM_REQ_LIST 55
564/** The maximum DHCP message size a client is willing to accept. */
565#define RTNET_DHCP_OPT_MAX_DHCP_MSG_SIZE 57
566/** Client ID. */
567#define RTNET_DHCP_OPT_CLIENT_ID 61
568/** TFTP server name. */
569#define RTNET_DHCP_OPT_TFTP_SERVER_NAME 66
570/** Bootfile name. */
571#define RTNET_DHCP_OPT_BOOTFILE_NAME 67
572
573/** Marks the end of the DHCP options, this has no dhcp_len field. */
574#define RTNET_DHCP_OPT_END 255
575/** @} */
576
577/** @name DHCP Message Types (option 53)
578 * @{ */
579#define RTNET_DHCP_MT_DISCOVER 1
580#define RTNET_DHCP_MT_OFFER 2
581#define RTNET_DHCP_MT_REQUEST 3
582#define RTNET_DHCP_MT_DECLINE 4
583#define RTNET_DHCP_MT_ACK 5
584#define RTNET_DHCP_MT_NAC 6
585#define RTNET_DHCP_MT_RELEASE 7
586#define RTNET_DHCP_MT_INFORM 8
587/** @} */
588
589/** @name DHCP Flags
590 * @{ */
591#define RTNET_DHCP_FLAG_BROADCAST 0x8000
592/** @} */
593
594RTDECL(bool) RTNetIPv4IsDHCPValid(PCRTNETUDP pUdpHdr, PCRTNETBOOTP pDhcp, size_t cbDhcp, uint8_t *pMsgType);
595
596
597/**
598 * IPv4 DHCP packet.
599 * @deprecated Use RTNETBOOTP.
600 */
601#pragma pack(1)
602typedef struct RTNETDHCP
603{
604 /** 00 - The packet opcode. */
605 uint8_t Op;
606 /** Hardware address type. */
607 uint8_t HType;
608 /** Hardware address length. */
609 uint8_t HLen;
610 uint8_t Hops;
611 uint32_t XID;
612 uint16_t Secs;
613 uint16_t Flags;
614 /** Client IPv4 address. */
615 RTNETADDRIPV4 CIAddr;
616 /** Your IPv4 address. */
617 RTNETADDRIPV4 YIAddr;
618 /** Server IPv4 address. */
619 RTNETADDRIPV4 SIAddr;
620 /** Gateway IPv4 address. */
621 RTNETADDRIPV4 GIAddr;
622 /** Client hardware address. */
623 uint8_t CHAddr[16];
624 /** Server name. */
625 uint8_t SName[64];
626 uint8_t File[128];
627 uint8_t abMagic[4];
628 uint8_t DhcpOpt;
629 uint8_t DhcpLen; /* 1 */
630 uint8_t DhcpReq;
631 uint8_t abOptions[57];
632} RTNETDHCP;
633#pragma pack()
634/** @todo AssertCompileSize(RTNETDHCP, ); */
635/** Pointer to a DHCP packet. */
636typedef RTNETDHCP *PRTNETDHCP;
637/** Pointer to a const DHCP packet. */
638typedef RTNETDHCP const *PCRTNETDHCP;
639
640
641/**
642 * TCP packet.
643 */
644#pragma pack(1)
645typedef struct RTNETTCP
646{
647 /** 00 - The source port. */
648 uint16_t th_sport;
649 /** 02 - The destination port. */
650 uint16_t th_dport;
651 /** 04 - The sequence number. */
652 uint32_t th_seq;
653 /** 08 - The acknowledgement number. */
654 uint32_t th_ack;
655#ifdef RT_BIG_ENDIAN
656 unsigned int th_win : 16;
657 unsigned int th_flags : 8;
658 unsigned int th_off : 4;
659 unsigned int th_x2 : 4;
660#else
661 /** 0c:0 - Reserved. */
662 unsigned int th_x2 : 4;
663 /** 0c:4 - The data offset given as a dword count from the start of this header. */
664 unsigned int th_off : 4;
665 /** 0d - flags. */
666 unsigned int th_flags : 8;
667 /** 0e - The window. */
668 unsigned int th_win : 16;
669#endif
670 /** 10 - The checksum of the pseudo header, the TCP header and the data. */
671 uint16_t th_sum;
672 /** 12 - The urgent pointer. */
673 uint16_t th_urp;
674 /* (options follows here and then the data (aka text).) */
675} RTNETTCP;
676#pragma pack()
677AssertCompileSize(RTNETTCP, 20);
678/** Pointer to a TCP packet. */
679typedef RTNETTCP *PRTNETTCP;
680/** Pointer to a const TCP packet. */
681typedef RTNETTCP const *PCRTNETTCP;
682
683/** The minimum TCP header length (in bytes). (RTNETTCP::th_off * 4) */
684#define RTNETTCP_MIN_LEN (20)
685
686/** @name TCP flags (RTNETTCP::th_flags)
687 * @{ */
688#define RTNETTCP_F_FIN 0x01
689#define RTNETTCP_F_SYN 0x02
690#define RTNETTCP_F_RST 0x04
691#define RTNETTCP_F_PSH 0x08
692#define RTNETTCP_F_ACK 0x10
693#define RTNETTCP_F_URG 0x20
694#define RTNETTCP_F_ECE 0x40
695#define RTNETTCP_F_CWR 0x80
696/** @} */
697
698RTDECL(uint16_t) RTNetTCPChecksum(uint32_t u32Sum, PCRTNETTCP pTcpHdr, void const *pvData, size_t cbData);
699RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum);
700RTDECL(uint16_t) RTNetIPv4TCPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, void const *pvData);
701RTDECL(bool) RTNetIPv4IsTCPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, size_t cbPktMax);
702RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData,
703 size_t cbPktMax, bool fChecksum);
704
705
706/**
707 * IPv4 ICMP packet header.
708 */
709#pragma pack(1)
710typedef struct RTNETICMPV4HDR
711{
712 /** 00 - The ICMP message type. */
713 uint8_t icmp_type;
714 /** 01 - Type specific code that further qualifies the message. */
715 uint8_t icmp_code;
716 /** 02 - Checksum of the ICMP message. */
717 uint16_t icmp_cksum;
718} RTNETICMPV4HDR;
719#pragma pack()
720AssertCompileSize(RTNETICMPV4HDR, 4);
721/** Pointer to an ICMP packet header. */
722typedef RTNETICMPV4HDR *PRTNETICMPV4HDR;
723/** Pointer to a const ICMP packet header. */
724typedef RTNETICMPV4HDR const *PCRTNETICMPV4HDR;
725
726/** @name ICMP (v4) message types.
727 * @{ */
728#define RTNETICMPV4_TYPE_ECHO_REPLY 0
729#define RTNETICMPV4_TYPE_ECHO_REQUEST 8
730#define RTNETICMPV4_TYPE_TRACEROUTE 30
731/** @} */
732
733/**
734 * IPv4 ICMP ECHO Reply & Request packet.
735 */
736#pragma pack(1)
737typedef struct RTNETICMPV4ECHO
738{
739 /** 00 - The ICMP header. */
740 RTNETICMPV4HDR Hdr;
741 /** 04 - The identifier to help the requestor match up the reply.
742 * Can be 0. Typically fixed value. */
743 uint16_t icmp_id;
744 /** 06 - The sequence number to help the requestor match up the reply.
745 * Can be 0. Typically incrementing between requests. */
746 uint16_t icmp_seq;
747 /** 08 - Variable length data that is to be returned unmodified in the reply. */
748 uint8_t icmp_data[1];
749} RTNETICMPV4ECHO;
750#pragma pack()
751AssertCompileSize(RTNETICMPV4ECHO, 9);
752/** Pointer to an ICMP ECHO packet. */
753typedef RTNETICMPV4ECHO *PRTNETICMPV4ECHO;
754/** Pointer to a const ICMP ECHO packet. */
755typedef RTNETICMPV4ECHO const *PCRTNETICMPV4ECHO;
756
757/**
758 * IPv4 ICMP TRACEROUTE packet.
759 * This is an reply to an IP packet with the traceroute option set.
760 */
761#pragma pack(1)
762typedef struct RTNETICMPV4TRACEROUTE
763{
764 /** 00 - The ICMP header. */
765 RTNETICMPV4HDR Hdr;
766 /** 04 - Identifier copied from the traceroute option's ID number. */
767 uint16_t icmp_id;
768 /** 06 - Unused. (Possibly an icmp_seq?) */
769 uint16_t icmp_void;
770 /** 08 - Outbound hop count. From the IP packet causing this message. */
771 uint16_t icmp_ohc;
772 /** 0a - Return hop count. From the IP packet causing this message. */
773 uint16_t icmp_rhc;
774 /** 0c - Output link speed, 0 if not known. */
775 uint32_t icmp_speed;
776 /** 10 - Output link MTU, 0 if not known. */
777 uint32_t icmp_mtu;
778} RTNETICMPV4TRACEROUTE;
779#pragma pack()
780AssertCompileSize(RTNETICMPV4TRACEROUTE, 20);
781/** Pointer to an ICMP TRACEROUTE packet. */
782typedef RTNETICMPV4TRACEROUTE *PRTNETICMPV4TRACEROUTE;
783/** Pointer to a const ICMP TRACEROUTE packet. */
784typedef RTNETICMPV4TRACEROUTE const *PCRTNETICMPV4TRACEROUTE;
785
786/** @todo add more ICMPv4 as needed. */
787
788/**
789 * IPv4 ICMP union packet.
790 */
791typedef union RTNETICMPV4
792{
793 RTNETICMPV4HDR Hdr;
794 RTNETICMPV4ECHO Echo;
795 RTNETICMPV4TRACEROUTE Traceroute;
796} RTNETICMPV4;
797/** Pointer to an ICMP union packet. */
798typedef RTNETICMPV4 *PRTNETICMPV4;
799/** Pointer to a const ICMP union packet. */
800typedef RTNETICMPV4 const *PCRTNETICMPV4;
801
802
803/**
804 * IPv6 ICMP packet header.
805 */
806#pragma pack(1)
807typedef struct RTNETICMPV6HDR
808{
809 /** 00 - The ICMPv6 message type. */
810 uint8_t icmp6_type;
811 /** 01 - Type specific code that further qualifies the message. */
812 uint8_t icmp6_code;
813 /** 02 - Checksum of the ICMPv6 message. */
814 uint16_t icmp6_cksum;
815} RTNETICMPV6HDR;
816#pragma pack()
817AssertCompileSize(RTNETICMPV6HDR, 4);
818/** Pointer to an ICMPv6 packet header. */
819typedef RTNETICMPV6HDR *PRTNETICMPV6HDR;
820/** Pointer to a const ICMP packet header. */
821typedef RTNETICMPV6HDR const *PCRTNETICMPV6HDR;
822
823#define RTNETIPV6_PROT_ICMPV6 (58)
824
825/** @name Internet Control Message Protocol version 6 (ICMPv6) message types.
826 * @{ */
827#define RTNETIPV6_ICMP_TYPE_RS 133
828#define RTNETIPV6_ICMP_TYPE_RA 134
829#define RTNETIPV6_ICMP_TYPE_NS 135
830#define RTNETIPV6_ICMP_TYPE_NA 136
831#define RTNETIPV6_ICMP_TYPE_RDR 137
832/** @} */
833
834/** @name Neighbor Discovery option types
835 * @{ */
836#define RTNETIPV6_ICMP_ND_SLLA_OPT (1)
837#define RTNETIPV6_ICMP_ND_TLLA_OPT (2)
838/** @} */
839
840/** ICMPv6 ND Source/Target Link Layer Address option */
841#pragma pack(1)
842typedef struct RTNETNDP_LLA_OPT
843{
844 uint8_t type;
845 uint8_t len;
846 RTMAC lla;
847} RTNETNDP_LLA_OPT;
848#pragma pack()
849
850AssertCompileSize(RTNETNDP_LLA_OPT, 1+1+6);
851
852typedef RTNETNDP_LLA_OPT *PRTNETNDP_LLA_OPT;
853typedef RTNETNDP_LLA_OPT const *PCRTNETNDP_LLA_OPT;
854
855/** ICMPv6 ND Neighbor Sollicitation */
856#pragma pack(1)
857typedef struct RTNETNDP
858{
859 /** 00 - The ICMPv6 header. */
860 RTNETICMPV6HDR Hdr;
861 /** 04 - reserved */
862 uint32_t reserved;
863 /** 08 - target address */
864 RTNETADDRIPV6 target_address;
865} RTNETNDP;
866#pragma pack()
867AssertCompileSize(RTNETNDP, 4+4+16);
868/** Pointer to a NDP ND packet. */
869typedef RTNETNDP *PRTNETNDP;
870/** Pointer to a const NDP NS packet. */
871typedef RTNETNDP const *PCRTNETNDP;
872
873
874/**
875 * Ethernet ARP header.
876 */
877#pragma pack(1)
878typedef struct RTNETARPHDR
879{
880 /** The hardware type. */
881 uint16_t ar_htype;
882 /** The protocol type (ethertype). */
883 uint16_t ar_ptype;
884 /** The hardware address length. */
885 uint8_t ar_hlen;
886 /** The protocol address length. */
887 uint8_t ar_plen;
888 /** The operation. */
889 uint16_t ar_oper;
890} RTNETARPHDR;
891#pragma pack()
892AssertCompileSize(RTNETARPHDR, 8);
893/** Pointer to an ethernet ARP header. */
894typedef RTNETARPHDR *PRTNETARPHDR;
895/** Pointer to a const ethernet ARP header. */
896typedef RTNETARPHDR const *PCRTNETARPHDR;
897
898/** ARP hardware type - ethernet. */
899#define RTNET_ARP_ETHER UINT16_C(1)
900
901/** @name ARP operations
902 * @{ */
903#define RTNET_ARPOP_REQUEST UINT16_C(1) /**< Request hardware address given a protocol address (ARP). */
904#define RTNET_ARPOP_REPLY UINT16_C(2)
905#define RTNET_ARPOP_REVREQUEST UINT16_C(3) /**< Request protocol address given a hardware address (RARP). */
906#define RTNET_ARPOP_REVREPLY UINT16_C(4)
907#define RTNET_ARPOP_INVREQUEST UINT16_C(8) /**< Inverse ARP. */
908#define RTNET_ARPOP_INVREPLY UINT16_C(9)
909/** Check if an ARP operation is a request or not. */
910#define RTNET_ARPOP_IS_REQUEST(Op) ((Op) & 1)
911/** Check if an ARP operation is a reply or not. */
912#define RTNET_ARPOP_IS_REPLY(Op) (!RTNET_ARPOP_IS_REQUEST(Op))
913/** @} */
914
915
916/**
917 * Ethernet IPv4 + 6-byte MAC ARP request packet.
918 */
919#pragma pack(1)
920typedef struct RTNETARPIPV4
921{
922 /** ARP header. */
923 RTNETARPHDR Hdr;
924 /** The sender hardware address. */
925 RTMAC ar_sha;
926 /** The sender protocol address. */
927 RTNETADDRIPV4 ar_spa;
928 /** The target hardware address. */
929 RTMAC ar_tha;
930 /** The target protocol address. */
931 RTNETADDRIPV4 ar_tpa;
932} RTNETARPIPV4;
933#pragma pack()
934AssertCompileSize(RTNETARPIPV4, 8+6+4+6+4);
935/** Pointer to an ethernet IPv4+MAC ARP request packet. */
936typedef RTNETARPIPV4 *PRTNETARPIPV4;
937/** Pointer to a const ethernet IPv4+MAC ARP request packet. */
938typedef RTNETARPIPV4 const *PCRTNETARPIPV4;
939
940
941/** @} */
942
943RT_C_DECLS_END
944
945#endif
946
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette