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