1 /*
2 * Fundamental constants relating to IP Protocol
3 *
4 * Copyright (C) 2020, Broadcom.
5 *
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
11 *
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
19 *
20 *
21 * <<Broadcom-WL-IPTag/Dual:>>
22 */
23
24 #ifndef _bcmip_h_
25 #define _bcmip_h_
26
27 #ifndef _TYPEDEFS_H_
28 #include <typedefs.h>
29 #endif
30
31 /* This marks the start of a packed structure section. */
32 #include <packed_section_start.h>
33
34 /* IPV4 and IPV6 common */
35 #define IP_VER_OFFSET 0x0 /* offset to version field */
36 #define IP_VER_MASK 0xf0 /* version mask */
37 #define IP_VER_SHIFT 4 /* version shift */
38 #define IP_VER_4 4 /* version number for IPV4 */
39 #define IP_VER_6 6 /* version number for IPV6 */
40
41 #define IP_VER(ip_body) \
42 ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT)
43
44 #define IP_PROT_ICMP 0x1 /* ICMP protocol */
45 #define IP_PROT_IGMP 0x2 /* IGMP protocol */
46 #define IP_PROT_TCP 0x6 /* TCP protocol */
47 #define IP_PROT_UDP 0x11 /* UDP protocol type */
48 #define IP_PROT_GRE 0x2f /* GRE protocol type */
49 #define IP_PROT_ICMP6 0x3a /* ICMPv6 protocol type */
50
51 /* IPV4 field offsets */
52 #define IPV4_VER_HL_OFFSET 0 /* version and ihl byte offset */
53 #define IPV4_TOS_OFFSET 1 /* type of service offset */
54 #define IPV4_PKTLEN_OFFSET 2 /* packet length offset */
55 #define IPV4_PKTFLAG_OFFSET 6 /* more-frag,dont-frag flag offset */
56 #define IPV4_PROT_OFFSET 9 /* protocol type offset */
57 #define IPV4_CHKSUM_OFFSET 10 /* IP header checksum offset */
58 #define IPV4_SRC_IP_OFFSET 12 /* src IP addr offset */
59 #define IPV4_DEST_IP_OFFSET 16 /* dest IP addr offset */
60 #define IPV4_OPTIONS_OFFSET 20 /* IP options offset */
61 #define IPV4_MIN_HEADER_LEN 20 /* Minimum size for an IP header (no options) */
62
63 /* IPV4 field decodes */
64 #define IPV4_VER_MASK 0xf0 /* IPV4 version mask */
65 #define IPV4_VER_SHIFT 4 /* IPV4 version shift */
66
67 #define IPV4_HLEN_MASK 0x0f /* IPV4 header length mask */
68 #define IPV4_HLEN(ipv4_body) (4 * (((uint8 *)(ipv4_body))[IPV4_VER_HL_OFFSET] & IPV4_HLEN_MASK))
69
70 #define IPV4_HLEN_MIN (4 * 5) /* IPV4 header minimum length */
71
72 #define IPV4_ADDR_LEN 4 /* IPV4 address length */
73
74 #define IPV4_ADDR_NULL(a) ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \
75 ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0)
76
77 #define IPV4_ADDR_BCAST(a) ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \
78 ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff)
79
80 #define IPV4_TOS_DSCP_MASK 0xfc /* DiffServ codepoint mask */
81 #define IPV4_TOS_DSCP_SHIFT 2 /* DiffServ codepoint shift */
82
83 #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
84
85 #define IPV4_TOS_PREC_MASK 0xe0 /* Historical precedence mask */
86 #define IPV4_TOS_PREC_SHIFT 5 /* Historical precedence shift */
87
88 #define IPV4_TOS_LOWDELAY 0x10 /* Lowest delay requested */
89 #define IPV4_TOS_THROUGHPUT 0x8 /* Best throughput requested */
90 #define IPV4_TOS_RELIABILITY 0x4 /* Most reliable delivery requested */
91
92 #define IPV4_TOS_ROUTINE 0
93 #define IPV4_TOS_PRIORITY 1
94 #define IPV4_TOS_IMMEDIATE 2
95 #define IPV4_TOS_FLASH 3
96 #define IPV4_TOS_FLASHOVERRIDE 4
97 #define IPV4_TOS_CRITICAL 5
98 #define IPV4_TOS_INETWORK_CTRL 6
99 #define IPV4_TOS_NETWORK_CTRL 7
100
101 #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
102
103 #define IPV4_FRAG_RESV 0x8000 /* Reserved */
104 #define IPV4_FRAG_DONT 0x4000 /* Don't fragment */
105 #define IPV4_FRAG_MORE 0x2000 /* More fragments */
106 #define IPV4_FRAG_OFFSET_MASK 0x1fff /* Fragment offset */
107
108 #define IPV4_ADDR_STR_LEN 16 /* Max IP address length in string format */
109
110 /* IPV4 packet formats */
111 BWL_PRE_PACKED_STRUCT struct ipv4_addr {
112 uint8 addr[IPV4_ADDR_LEN];
113 } BWL_POST_PACKED_STRUCT;
114
115 BWL_PRE_PACKED_STRUCT struct ipv4_hdr {
116 uint8 version_ihl; /* Version and Internet Header Length */
117 uint8 tos; /* Type Of Service */
118 uint16 tot_len; /* Number of bytes in packet (max 65535) */
119 uint16 id;
120 uint16 frag; /* 3 flag bits and fragment offset */
121 uint8 ttl; /* Time To Live */
122 uint8 prot; /* Protocol */
123 uint16 hdr_chksum; /* IP header checksum */
124 uint8 src_ip[IPV4_ADDR_LEN]; /* Source IP Address */
125 uint8 dst_ip[IPV4_ADDR_LEN]; /* Destination IP Address */
126 } BWL_POST_PACKED_STRUCT;
127
128 /* IPV6 field offsets */
129 #define IPV6_PAYLOAD_LEN_OFFSET 4 /* payload length offset */
130 #define IPV6_NEXT_HDR_OFFSET 6 /* next header/protocol offset */
131 #define IPV6_HOP_LIMIT_OFFSET 7 /* hop limit offset */
132 #define IPV6_SRC_IP_OFFSET 8 /* src IP addr offset */
133 #define IPV6_DEST_IP_OFFSET 24 /* dst IP addr offset */
134
135 /* IPV6 field decodes */
136 #define IPV6_TRAFFIC_CLASS(ipv6_body) \
137 (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \
138 ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
139
140 #define IPV6_FLOW_LABEL(ipv6_body) \
141 (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \
142 (((uint8 *)(ipv6_body))[2] << 8) | \
143 (((uint8 *)(ipv6_body))[3]))
144
145 #define IPV6_PAYLOAD_LEN(ipv6_body) \
146 ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \
147 ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
148
149 #define IPV6_NEXT_HDR(ipv6_body) \
150 (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET])
151
152 #define IPV6_PROT(ipv6_body) IPV6_NEXT_HDR(ipv6_body)
153
154 #define IPV6_ADDR_LEN 16 /* IPV6 address length */
155
156 /* IPV4 TOS or IPV6 Traffic Classifier or 0 */
157 #define IP_TOS46(ip_body) \
158 (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \
159 IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0)
160
161 #define IP_DSCP46(ip_body) (IP_TOS46(ip_body) >> IPV4_TOS_DSCP_SHIFT);
162
163 /* IPV4 or IPV6 Protocol Classifier or 0 */
164 #define IP_PROT46(ip_body) \
165 (IP_VER(ip_body) == IP_VER_4 ? IPV4_PROT(ip_body) : \
166 IP_VER(ip_body) == IP_VER_6 ? IPV6_PROT(ip_body) : 0)
167
168 /* IPV6 extension headers (options) */
169 #define IPV6_EXTHDR_HOP 0
170 #define IPV6_EXTHDR_ROUTING 43
171 #define IPV6_EXTHDR_FRAGMENT 44
172 #define IPV6_EXTHDR_AUTH 51
173 #define IPV6_EXTHDR_NONE 59
174 #define IPV6_EXTHDR_DEST 60
175
176 #define IPV6_EXTHDR(prot) (((prot) == IPV6_EXTHDR_HOP) || \
177 ((prot) == IPV6_EXTHDR_ROUTING) || \
178 ((prot) == IPV6_EXTHDR_FRAGMENT) || \
179 ((prot) == IPV6_EXTHDR_AUTH) || \
180 ((prot) == IPV6_EXTHDR_NONE) || \
181 ((prot) == IPV6_EXTHDR_DEST))
182
183 #define IPV6_MIN_HLEN 40
184
185 #define IPV6_EXTHDR_LEN(eh) ((((struct ipv6_exthdr *)(eh))->hdrlen + 1) << 3)
186
187 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr {
188 uint8 nexthdr;
189 uint8 hdrlen;
190 } BWL_POST_PACKED_STRUCT;
191
192 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr_frag {
193 uint8 nexthdr;
194 uint8 rsvd;
195 uint16 frag_off;
196 uint32 ident;
197 } BWL_POST_PACKED_STRUCT;
198
199 /* deprecated and replaced by ipv6_exthdr_len_check */
200 static INLINE int32
ipv6_exthdr_len(uint8 * h,uint8 * proto)201 ipv6_exthdr_len(uint8 *h, uint8 *proto)
202 {
203 uint16 len = 0, hlen;
204 struct ipv6_exthdr *eh = (struct ipv6_exthdr *)h;
205
206 while (IPV6_EXTHDR(eh->nexthdr)) {
207 if (eh->nexthdr == IPV6_EXTHDR_NONE)
208 return -1;
209 else if (eh->nexthdr == IPV6_EXTHDR_FRAGMENT)
210 hlen = 8U;
211 else if (eh->nexthdr == IPV6_EXTHDR_AUTH)
212 hlen = (uint16)((eh->hdrlen + 2U) << 2U);
213 else
214 hlen = (uint16)IPV6_EXTHDR_LEN(eh);
215
216 len += hlen;
217 eh = (struct ipv6_exthdr *)(h + len);
218 }
219
220 *proto = eh->nexthdr;
221 return len;
222 }
223
224 /* determine length of exthdr with length checking */
225 static INLINE int32
ipv6_exthdr_len_check(uint8 * h,uint16 plen,uint8 * proto)226 ipv6_exthdr_len_check(uint8 *h, uint16 plen, uint8 *proto)
227 {
228 uint16 len = 0, hlen;
229 struct ipv6_exthdr *eh = (struct ipv6_exthdr *)h;
230
231 /* must have at least one exthdr */
232 if (plen < sizeof(struct ipv6_exthdr)) {
233 return -1;
234 }
235
236 /* length check before accessing next exthdr */
237 while ((plen >= len + sizeof(struct ipv6_exthdr)) && IPV6_EXTHDR(eh->nexthdr)) {
238 if (eh->nexthdr == IPV6_EXTHDR_NONE) {
239 return -1;
240 } else if (eh->nexthdr == IPV6_EXTHDR_FRAGMENT) {
241 hlen = 8U;
242 } else if (eh->nexthdr == IPV6_EXTHDR_AUTH) {
243 hlen = (uint16)((eh->hdrlen + 2U) << 2U);
244 } else {
245 hlen = (uint16)IPV6_EXTHDR_LEN(eh);
246 }
247
248 /* check exthdr length */
249 if (plen < len + hlen) {
250 /* invalid exthdr */
251 return -1;
252 }
253 len += hlen;
254 eh = (struct ipv6_exthdr *)(h + len);
255 }
256
257 /* length check before accessing next exthdr */
258 if (plen >= len + sizeof(struct ipv6_exthdr)) {
259 *proto = eh->nexthdr;
260 } else {
261 *proto = 0;
262 }
263 return len;
264 }
265 #define IPV4_ISMULTI(a) (((a) & 0xf0000000) == 0xe0000000)
266
267 #define IPV4_MCAST_TO_ETHER_MCAST(ipv4, ether) \
268 { \
269 ether[0] = 0x01; \
270 ether[1] = 0x00; \
271 ether[2] = 0x5E; \
272 ether[3] = (ipv4 & 0x7f0000) >> 16; \
273 ether[4] = (ipv4 & 0xff00) >> 8; \
274 ether[5] = (ipv4 & 0xff); \
275 }
276
277 /* This marks the end of a packed structure section. */
278 #include <packed_section_end.h>
279
280 #define IPV4_ADDR_STR "%d.%d.%d.%d"
281 #define IPV4_ADDR_TO_STR(addr) ((uint32)addr & 0xff000000) >> 24, \
282 ((uint32)addr & 0x00ff0000) >> 16, \
283 ((uint32)addr & 0x0000ff00) >> 8, \
284 ((uint32)addr & 0x000000ff)
285
286 #endif /* _bcmip_h_ */
287