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