• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * IPv4 protocol definitions
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 #ifndef LWIP_HDR_PROT_IP4_H
38 #define LWIP_HDR_PROT_IP4_H
39 
40 #include "lwip/arch.h"
41 #include "lwip/ip4_addr.h"
42 #if LWIP_LITEOS_COMPAT
43 #include <netinet/ip.h>
44 #endif
45 
46 #if defined (__cplusplus) && __cplusplus
47 extern "C" {
48 #endif
49 
50 /* This is the packed version of ip4_addr_t,
51     used in network headers that are itself packed */
52 #ifdef PACK_STRUCT_USE_INCLUDES
53 #  include "arch/bpstruct.h"
54 #endif
55 PACK_STRUCT_BEGIN
56 struct ip4_addr_packed {
57   PACK_STRUCT_FIELD(u32_t addr);
58 } PACK_STRUCT_STRUCT;
59 PACK_STRUCT_END
60 #ifdef PACK_STRUCT_USE_INCLUDES
61 #  include "arch/epstruct.h"
62 #endif
63 
64 typedef struct ip4_addr_packed ip4_addr_p_t;
65 
66 /* Size of the IPv4 header. Same as 'sizeof(struct ip_hdr)'. */
67 #define IP_HLEN 20
68 
69 /* Maximum size of the IPv4 header with options. */
70 #define IP_HLEN_MAX 60
71 
72 #ifndef IP_RF
73 #define IP_RF 0x8000U        /* reserved fragment flag */
74 #endif
75 #ifndef IP_DF
76 #define IP_DF 0x4000U        /* don't fragment flag */
77 #endif
78 #ifndef IP_MF
79 #define IP_MF 0x2000U        /* more fragments flag */
80 #endif
81 #ifndef IP_OFFMASK
82 #define IP_OFFMASK 0x1fffU   /* mask for fragmenting bits */
83 #endif
84 
85 #ifdef PACK_STRUCT_USE_INCLUDES
86 #  include "arch/bpstruct.h"
87 #endif
88 PACK_STRUCT_BEGIN
89 /* The IPv4 header */
90 struct ip_hdr {
91   /* version / header length */
92   PACK_STRUCT_FLD_8(u8_t _v_hl);
93   /* type of service */
94   PACK_STRUCT_FLD_8(u8_t _tos);
95   /* total length */
96   PACK_STRUCT_FIELD(u16_t _len);
97   /* identification */
98   PACK_STRUCT_FIELD(u16_t _id);
99   /* fragment offset field */
100   PACK_STRUCT_FIELD(u16_t _offset);
101   /* time to live */
102   PACK_STRUCT_FLD_8(u8_t _ttl);
103   /* protocol*/
104   PACK_STRUCT_FLD_8(u8_t _proto);
105   /* checksum */
106   PACK_STRUCT_FIELD(u16_t _chksum);
107   /* source and destination IP addresses */
108   PACK_STRUCT_FLD_S(ip4_addr_p_t src);
109   PACK_STRUCT_FLD_S(ip4_addr_p_t dest);
110 } PACK_STRUCT_STRUCT;
111 PACK_STRUCT_END
112 #ifdef PACK_STRUCT_USE_INCLUDES
113 #  include "arch/epstruct.h"
114 #endif
115 
116 /* Macros to get struct ip_hdr fields: */
117 #define IPH_V(hdr)  ((hdr)->_v_hl >> 4)
118 #define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f)
119 #define IPH_TOS(hdr) ((hdr)->_tos)
120 #define IPH_LEN(hdr) ((hdr)->_len)
121 #define IPH_ID(hdr) ((hdr)->_id)
122 #define IPH_OFFSET(hdr) ((hdr)->_offset)
123 #define IPH_TTL(hdr) ((hdr)->_ttl)
124 #define IPH_PROTO(hdr) ((hdr)->_proto)
125 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
126 
127 /* Macros to set struct ip_hdr fields: */
128 #define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = (u8_t)((((v) << 4) | (hl)))
129 #define IPH_TOS_SET(hdr, tos) (hdr)->_tos = (tos)
130 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
131 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
132 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
133 #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl = (u8_t)(ttl)
134 #define IPH_PROTO_SET(hdr, proto) (hdr)->_proto = (u8_t)(proto)
135 #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
136 
137 #if defined (__cplusplus) && __cplusplus
138 }
139 #endif
140 
141 #endif /* LWIP_HDR_PROT_IP4_H */
142