mikroSDK Reference Manual
ipv4.h
Go to the documentation of this file.
1
31#ifndef _IPV4_H
32#define _IPV4_H
33
34//Forward declaration of structures
35struct _Ipv4Header;
36#define Ipv4Header struct _Ipv4Header
37
39#define Ipv4PseudoHeader struct _Ipv4PseudoHeader
40
41//Dependencies
42#include "core/net.h"
43#include "core/ethernet.h"
44#include "ipv4/ipv4_frag.h"
45
46//IPv4 support
47#ifndef IPV4_SUPPORT
48 #define IPV4_SUPPORT ENABLED
49#elif (IPV4_SUPPORT != ENABLED && IPV4_SUPPORT != DISABLED)
50 #error IPV4_SUPPORT parameter is not valid
51#endif
52
53//IPsec support
54#ifndef IPV4_IPSEC_SUPPORT
55 #define IPV4_IPSEC_SUPPORT DISABLED
56#elif (IPV4_IPSEC_SUPPORT != ENABLED && IPV4_IPSEC_SUPPORT != DISABLED)
57 #error IPV4_IPSEC_SUPPORT parameter is not valid
58#endif
59
60//Default IPv4 time-to-live value
61#ifndef IPV4_DEFAULT_TTL
62 #define IPV4_DEFAULT_TTL 64
63#elif (IPV4_DEFAULT_TTL < 1)
64 #error IPV4_DEFAULT_TTL parameter is not valid
65#endif
66
67//Maximum number of IPv4 addresses
68#ifndef IPV4_ADDR_LIST_SIZE
69 #define IPV4_ADDR_LIST_SIZE 1
70#elif (IPV4_ADDR_LIST_SIZE < 1)
71 #error IPV4_ADDR_LIST_SIZE parameter is not valid
72#endif
73
74//Maximum number of DNS servers
75#ifndef IPV4_DNS_SERVER_LIST_SIZE
76 #define IPV4_DNS_SERVER_LIST_SIZE 2
77#elif (IPV4_DNS_SERVER_LIST_SIZE < 1)
78 #error IPV4_DNS_SERVER_LIST_SIZE parameter is not valid
79#endif
80
81//Size of the IPv4 multicast filter
82#ifndef IPV4_MULTICAST_FILTER_SIZE
83 #define IPV4_MULTICAST_FILTER_SIZE 4
84#elif (IPV4_MULTICAST_FILTER_SIZE < 1)
85 #error IPV4_MULTICAST_FILTER_SIZE parameter is not valid
86#endif
87
88//Version number for IPv4
89#define IPV4_VERSION 4
90//Minimum MTU
91#define IPV4_MINIMUM_MTU 68
92//Default MTU
93#define IPV4_DEFAULT_MTU 576
94//Minimum header length
95#define IPV4_MIN_HEADER_LENGTH 20
96//Maximum header length
97#define IPV4_MAX_HEADER_LENGTH 60
98
99//Shortcut to data field
100#define IPV4_DATA(packet) PTR_OFFSET(packet, packet->headerLength * 4)
101
102//Macro used for defining an IPv4 address
103#ifdef _CPU_BIG_ENDIAN
104 #define IPV4_ADDR(a, b, c, d) (((uint32_t) (a) << 24) | ((b) << 16) | ((c) << 8) | (d))
105#else
106 #define IPV4_ADDR(a, b, c, d) ((a) | ((b) << 8) | ((c) << 16) | ((uint32_t) (d) << 24))
107#endif
108
109//Unspecified IPv4 address
110#define IPV4_UNSPECIFIED_ADDR IPV4_ADDR(0, 0, 0, 0)
111//Broadcast IPV4 address
112#define IPV4_BROADCAST_ADDR IPV4_ADDR(255, 255, 255, 255)
113
114//Loopback IPv4 address
115#define IPV4_LOOPBACK_ADDR IPV4_ADDR(127, 0, 0, 1)
116#define IPV4_LOOPBACK_PREFIX IPV4_ADDR(127, 0, 0, 0)
117#define IPV4_LOOPBACK_MASK IPV4_ADDR(255, 0, 0, 0)
118
119//Link-local addresses
120#define IPV4_LINK_LOCAL_PREFIX IPV4_ADDR(169, 254, 0, 0)
121#define IPV4_LINK_LOCAL_MASK IPV4_ADDR(255, 255, 0, 0)
122
123//Multicast addresses
124#define IPV4_MULTICAST_PREFIX IPV4_ADDR(224, 0, 0, 0)
125#define IPV4_MULTICAST_MASK IPV4_ADDR(240, 0, 0, 0)
126
127//Local Network Control Block (RFC 5771)
128#define IPV4_MULTICAST_LNCB_PREFIX IPV4_ADDR(224, 0, 0, 0)
129#define IPV4_MULTICAST_LNCB_MASK IPV4_ADDR(255, 255, 255, 0)
130
131//Internetwork Control Block (RFC 5771)
132#define IPV4_MULTICAST_INCB_PREFIX IPV4_ADDR(224, 0, 1, 0)
133#define IPV4_MULTICAST_INCB_MASK IPV4_ADDR(255, 255, 255, 0)
134
135//IPv4 address classes
136#define IPV4_CLASS_A_ADDR IPV4_ADDR(0, 0, 0, 0)
137#define IPV4_CLASS_A_MASK IPV4_ADDR(128, 0, 0, 0)
138#define IPV4_CLASS_B_ADDR IPV4_ADDR(128, 0, 0, 0)
139#define IPV4_CLASS_B_MASK IPV4_ADDR(192, 0, 0, 0)
140#define IPV4_CLASS_C_ADDR IPV4_ADDR(192, 0, 0, 0)
141#define IPV4_CLASS_C_MASK IPV4_ADDR(224, 0, 0, 0)
142#define IPV4_CLASS_D_ADDR IPV4_ADDR(224, 0, 0, 0)
143#define IPV4_CLASS_D_MASK IPV4_ADDR(240, 0, 0, 0)
144#define IPV4_CLASS_E_ADDR IPV4_ADDR(240, 0, 0, 0)
145#define IPV4_CLASS_E_MASK IPV4_ADDR(240, 0, 0, 0)
146
147//Copy IPv4 address
148#define ipv4CopyAddr(destIpAddr, srcIpAddr) \
149 osMemcpy(destIpAddr, srcIpAddr, sizeof(Ipv4Addr))
150
151//Compare IPv4 addresses
152#define ipv4CompAddr(ipAddr1, ipAddr2) \
153 (!osMemcmp(ipAddr1, ipAddr2, sizeof(Ipv4Addr)))
154
155//Determine whether an IPv4 address belongs to the subnet
156#define ipv4IsOnSubnet(entry, ipAddr) \
157 (((ipAddr) & (entry)->subnetMask) == ((entry)->addr & (entry)->subnetMask))
158
159//Determine whether an IPv4 address is a loopback address
160#define ipv4IsLoopbackAddr(ipAddr) \
161 (((ipAddr) & IPV4_LOOPBACK_MASK) == IPV4_LOOPBACK_PREFIX)
162
163//Determine whether an IPv4 address is a link-local address
164#define ipv4IsLinkLocalAddr(ipAddr) \
165 (((ipAddr) & IPV4_LINK_LOCAL_MASK) == IPV4_LINK_LOCAL_PREFIX)
166
167//Determine whether an IPv4 address is a multicast address
168#define ipv4IsMulticastAddr(ipAddr) \
169 (((ipAddr) & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_PREFIX)
170
171//C++ guard
172#ifdef __cplusplus
173extern "C" {
174#endif
175
176
181typedef enum
182{
183 IPV4_ADDR_SCOPE_INTERFACE_LOCAL = 1,
184 IPV4_ADDR_SCOPE_LINK_LOCAL = 2,
185 IPV4_ADDR_SCOPE_GLOBAL = 3
187
188
199
200
205typedef enum
206{
207 IPV4_FLAG_RES = 0x8000,
208 IPV4_FLAG_DF = 0x4000,
209 IPV4_FLAG_MF = 0x2000,
210 IPV4_OFFSET_MASK = 0x1FFF
212
213
218typedef enum
219{
220 IPV4_PROTOCOL_ICMP = 1,
221 IPV4_PROTOCOL_IGMP = 2,
222 IPV4_PROTOCOL_TCP = 6,
223 IPV4_PROTOCOL_UDP = 17,
224 IPV4_PROTOCOL_ESP = 50,
225 IPV4_PROTOCOL_AH = 51
227
228
261
262
267typedef uint32_t Ipv4Addr;
268
269
270//CodeWarrior or Win32 compiler?
271#if defined(__CWCC__) || defined(_WIN32)
272 #pragma pack(push, 1)
273#endif
274
275
281{
282#if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
283 uint8_t version : 4; //0
284 uint8_t headerLength : 4;
285#else
286 uint8_t headerLength : 4; //0
287 uint8_t version : 4;
288#endif
289 uint8_t typeOfService; //1
290 uint16_t totalLength; //2-3
291 uint16_t identification; //4-5
292 uint16_t fragmentOffset; //6-7
293 uint8_t timeToLive; //8
294 uint8_t protocol; //9
295 uint16_t headerChecksum; //10-11
296 Ipv4Addr srcAddr; //12-15
297 Ipv4Addr destAddr; //16-19
298 uint8_t options[]; //20
299};
300
301
307{
308 Ipv4Addr srcAddr; //0-3
309 Ipv4Addr destAddr; //4-7
310 uint8_t reserved; //8
311 uint8_t protocol; //9
312 uint16_t length; //10-11
313};
314
315
321{
322 uint8_t type; //0
323 uint8_t length; //1
324 uint8_t value[]; //2
325} Ipv4Option;
326
327
328//CodeWarrior or Win32 compiler?
329#if defined(__CWCC__) || defined(_WIN32)
330 #pragma pack(pop)
331#endif
332
333
346
347
352typedef struct
353{
355 uint_t refCount;
356 uint_t state;
357 bool_t flag;
360
361
366typedef struct
367{
368 size_t linkMtu;
369 bool_t isRouter;
370 uint8_t defaultTtl;
373 uint16_t identification;
374 Ipv4AddrEntry addrList[IPV4_ADDR_LIST_SIZE];
375 Ipv4Addr dnsServerList[IPV4_DNS_SERVER_LIST_SIZE];
376 Ipv4FilterEntry multicastFilter[IPV4_MULTICAST_FILTER_SIZE];
377#if (IPV4_FRAG_SUPPORT == ENABLED)
378 Ipv4FragDesc fragQueue[IPV4_MAX_FRAG_DATAGRAMS];
379#endif
381
382
383//IPv4 related functions
384error_t ipv4Init(NetInterface *interface);
385
386error_t ipv4SetDefaultTtl(NetInterface *interface, uint8_t ttl);
387
388error_t ipv4SetHostAddr(NetInterface *interface, Ipv4Addr addr);
389error_t ipv4SetHostAddrEx(NetInterface *interface, uint_t index, Ipv4Addr addr);
390error_t ipv4GetHostAddr(NetInterface *interface, Ipv4Addr *addr);
391error_t ipv4GetHostAddrEx(NetInterface *interface, uint_t index, Ipv4Addr *addr);
392
393error_t ipv4SetSubnetMask(NetInterface *interface, Ipv4Addr mask);
394error_t ipv4SetSubnetMaskEx(NetInterface *interface, uint_t index, Ipv4Addr mask);
395error_t ipv4GetSubnetMask(NetInterface *interface, Ipv4Addr *mask);
396error_t ipv4GetSubnetMaskEx(NetInterface *interface, uint_t index, Ipv4Addr *mask);
397
398error_t ipv4SetDefaultGateway(NetInterface *interface, Ipv4Addr addr);
399
400error_t ipv4SetDefaultGatewayEx(NetInterface *interface, uint_t index,
401 Ipv4Addr addr);
402
403error_t ipv4GetDefaultGateway(NetInterface *interface, Ipv4Addr *addr);
404
405error_t ipv4GetDefaultGatewayEx(NetInterface *interface, uint_t index,
406 Ipv4Addr *addr);
407
408error_t ipv4SetDnsServer(NetInterface *interface, uint_t index, Ipv4Addr addr);
409error_t ipv4GetDnsServer(NetInterface *interface, uint_t index, Ipv4Addr *addr);
410
411void ipv4LinkChangeEvent(NetInterface *interface);
412
413void ipv4ProcessPacket(NetInterface *interface, Ipv4Header *packet,
414 size_t length, NetRxAncillary *ancillary);
415
416void ipv4ProcessDatagram(NetInterface *interface, const NetBuffer *buffer,
417 size_t offset, NetRxAncillary *ancillary);
418
419error_t ipv4SendDatagram(NetInterface *interface,
420 const Ipv4PseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset,
421 NetTxAncillary *ancillary);
422
423error_t ipv4SendPacket(NetInterface *interface,
424 const Ipv4PseudoHeader *pseudoHeader, uint16_t fragId, size_t fragOffset,
425 NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
426
427error_t ipv4JoinMulticastGroup(NetInterface *interface, Ipv4Addr groupAddr);
428error_t ipv4LeaveMulticastGroup(NetInterface *interface, Ipv4Addr groupAddr);
429
430error_t ipv4StringToAddr(const char_t *str, Ipv4Addr *ipAddr);
431char_t *ipv4AddrToString(Ipv4Addr ipAddr, char_t *str);
432
433void ipv4DumpHeader(const Ipv4Header *ipHeader);
434
435//C++ guard
436#ifdef __cplusplus
437}
438#endif
439
440#endif
error_t
Error codes.
Definition error.h:43
Ethernet.
Ipv4Protocol
IPv4 protocol field.
Definition ipv4.h:219
__packed_struct _Ipv4PseudoHeader
IPv4 pseudo header.
Definition ipv4.h:307
uint32_t Ipv4Addr
IPv4 network address.
Definition ipv4.h:267
Ipv4AddrScope
IPv4 address scopes.
Definition ipv4.h:182
Ipv4AddrState
IPv4 address state.
Definition ipv4.h:194
@ IPV4_ADDR_STATE_INVALID
An address that is not assigned to any interface.
Definition ipv4.h:195
@ IPV4_ADDR_STATE_VALID
An address assigned to an interface whose use is unrestricted.
Definition ipv4.h:197
@ IPV4_ADDR_STATE_TENTATIVE
An address whose uniqueness on a link is being verified.
Definition ipv4.h:196
Ipv4FragmentOffset
IPv4 fragment offset field.
Definition ipv4.h:206
__packed_struct _Ipv4Header
IPv4 header.
Definition ipv4.h:281
typedef __packed_struct
IPv4 option.
Definition ipv4.h:321
Ipv4OptionType
IPv4 option types.
Definition ipv4.h:234
@ IPV4_OPTION_MTUP
MTU Probe.
Definition ipv4.h:239
@ IPV4_OPTION_QS
Quick-Start.
Definition ipv4.h:242
@ IPV4_OPTION_NOP
No Operation.
Definition ipv4.h:236
@ IPV4_OPTION_DPS
Dynamic Packet State.
Definition ipv4.h:257
@ IPV4_OPTION_EIP
Extended Internet Protocol.
Definition ipv4.h:253
@ IPV4_OPTION_ENCODE
Experimental IP encryption.
Definition ipv4.h:241
@ IPV4_OPTION_SDB
Selective Directed Broadcast.
Definition ipv4.h:256
@ IPV4_OPTION_TS
Time Stamp.
Definition ipv4.h:243
@ IPV4_OPTION_CIPSO
Commercial Security.
Definition ipv4.h:248
@ IPV4_OPTION_LSR
Loose Source Route.
Definition ipv4.h:246
@ IPV4_OPTION_RTRALT
Router Alert.
Definition ipv4.h:255
@ IPV4_OPTION_ADDEXT
Address Extension.
Definition ipv4.h:254
@ IPV4_OPTION_SEC
Security.
Definition ipv4.h:245
@ IPV4_OPTION_UMP
Upstream Multicast Packet.
Definition ipv4.h:258
@ IPV4_OPTION_IMITD
IMI Traffic Descriptor.
Definition ipv4.h:252
@ IPV4_OPTION_ESEC
Extended Security.
Definition ipv4.h:247
@ IPV4_OPTION_VISA
Experimental Access Control.
Definition ipv4.h:251
@ IPV4_OPTION_ZSU
Experimental Measurement.
Definition ipv4.h:238
@ IPV4_OPTION_FINN
Experimental Flow Control.
Definition ipv4.h:259
@ IPV4_OPTION_EEOL
End of Options List.
Definition ipv4.h:235
@ IPV4_OPTION_SSR
Strict Source Route.
Definition ipv4.h:250
@ IPV4_OPTION_TR
Traceroute.
Definition ipv4.h:244
@ IPV4_OPTION_SID
Stream ID.
Definition ipv4.h:249
@ IPV4_OPTION_MTUR
MTU Reply.
Definition ipv4.h:240
@ IPV4_OPTION_RR
Record Route.
Definition ipv4.h:237
IPv4 fragmentation and reassembly.
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
IPv4 address entry.
Definition ipv4.h:339
Ipv4AddrState state
IPv4 address state.
Definition ipv4.h:341
Ipv4Addr addr
IPv4 address.
Definition ipv4.h:340
Ipv4Addr defaultGateway
Default gateway.
Definition ipv4.h:344
bool_t conflict
Address conflict detected.
Definition ipv4.h:342
Ipv4Addr subnetMask
Subnet mask.
Definition ipv4.h:343
IPv4 context.
Definition ipv4.h:367
uint8_t defaultTtl
Default time-to-live value.
Definition ipv4.h:370
bool_t enableBroadcastEchoReq
Support for broadcast ICMP Echo Request messages.
Definition ipv4.h:372
uint16_t identification
IPv4 fragment identification field.
Definition ipv4.h:373
size_t linkMtu
Maximum transmission unit.
Definition ipv4.h:368
bool_t enableEchoReq
Support for ICMP Echo Request messages.
Definition ipv4.h:371
bool_t isRouter
A flag indicating whether routing is enabled on this interface.
Definition ipv4.h:369
IPv4 multicast filter entry.
Definition ipv4.h:353
bool_t flag
IGMP flag.
Definition ipv4.h:357
uint_t refCount
Reference count for the current entry.
Definition ipv4.h:355
Ipv4Addr addr
Multicast address.
Definition ipv4.h:354
systime_t timer
Delay timer.
Definition ipv4.h:358
uint_t state
IGMP host state.
Definition ipv4.h:356
Fragmented packet descriptor.
Definition ipv4_frag.h:131
Structure describing a buffer that spans multiple chunks.
Definition net_mem.h:89