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