• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Fundamental constants relating to IP Protocol
3  *
4  * Copyright (C) 1999-2019, 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
16  * of the license of that module.  An independent module is a module which is
17  * not derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  *
25  * <<Broadcom-WL-IPTag/Open:>>
26  *
27  * $Id: bcmip.h 785436 2018-10-18 17:54:25Z $
28  */
29 
30 #ifndef _bcmip_h_
31 #define _bcmip_h_
32 
33 #ifndef _TYPEDEFS_H_
34 #include <typedefs.h>
35 #endif // endif
36 
37 /* This marks the start of a packed structure section. */
38 #include <packed_section_start.h>
39 
40 /* IPV4 and IPV6 common */
41 #define IP_VER_OFFSET 0x0 /* offset to version field */
42 #define IP_VER_MASK 0xf0  /* version mask */
43 #define IP_VER_SHIFT 4    /* version shift */
44 #define IP_VER_4 4        /* version number for IPV4 */
45 #define IP_VER_6 6        /* version number for IPV6 */
46 
47 #define IP_VER(ip_body)                                                        \
48     ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT)
49 
50 #define IP_PROT_ICMP 0x1   /* ICMP protocol */
51 #define IP_PROT_IGMP 0x2   /* IGMP protocol */
52 #define IP_PROT_TCP 0x6    /* TCP protocol */
53 #define IP_PROT_UDP 0x11   /* UDP protocol type */
54 #define IP_PROT_GRE 0x2f   /* GRE protocol type */
55 #define IP_PROT_ICMP6 0x3a /* ICMPv6 protocol type */
56 
57 /* IPV4 field offsets */
58 #define IPV4_VER_HL_OFFSET 0   /* version and ihl byte offset */
59 #define IPV4_TOS_OFFSET 1      /* type of service offset */
60 #define IPV4_PKTLEN_OFFSET 2   /* packet length offset */
61 #define IPV4_PKTFLAG_OFFSET 6  /* more-frag,dont-frag flag offset */
62 #define IPV4_PROT_OFFSET 9     /* protocol type offset */
63 #define IPV4_CHKSUM_OFFSET 10  /* IP header checksum offset */
64 #define IPV4_SRC_IP_OFFSET 12  /* src IP addr offset */
65 #define IPV4_DEST_IP_OFFSET 16 /* dest IP addr offset */
66 #define IPV4_OPTIONS_OFFSET 20 /* IP options offset */
67 #define IPV4_MIN_HEADER_LEN 20 /* Minimum size for an IP header (no options)   \
68                                 */
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)                                                   \
76     (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)                                                      \
83     ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | ((uint8 *)(a))[2] |              \
84       ((uint8 *)(a))[3]) == 0)
85 
86 #define IPV4_ADDR_BCAST(a)                                                     \
87     ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & ((uint8 *)(a))[2] &              \
88       ((uint8 *)(a))[3]) == 0xff)
89 
90 #define IPV4_TOS_DSCP_MASK 0xfc /* DiffServ codepoint mask */
91 #define IPV4_TOS_DSCP_SHIFT 2   /* DiffServ codepoint shift */
92 
93 #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
94 
95 #define IPV4_TOS_PREC_MASK 0xe0 /* Historical precedence mask */
96 #define IPV4_TOS_PREC_SHIFT 5   /* Historical precedence shift */
97 
98 #define IPV4_TOS_LOWDELAY 0x10   /* Lowest delay requested */
99 #define IPV4_TOS_THROUGHPUT 0x8  /* Best throughput requested */
100 #define IPV4_TOS_RELIABILITY 0x4 /* Most reliable delivery requested */
101 
102 #define IPV4_TOS_ROUTINE 0
103 #define IPV4_TOS_PRIORITY 1
104 #define IPV4_TOS_IMMEDIATE 2
105 #define IPV4_TOS_FLASH 3
106 #define IPV4_TOS_FLASHOVERRIDE 4
107 #define IPV4_TOS_CRITICAL 5
108 #define IPV4_TOS_INETWORK_CTRL 6
109 #define IPV4_TOS_NETWORK_CTRL 7
110 
111 #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
112 
113 #define IPV4_FRAG_RESV 0x8000        /* Reserved */
114 #define IPV4_FRAG_DONT 0x4000        /* Don't fragment */
115 #define IPV4_FRAG_MORE 0x2000        /* More fragments */
116 #define IPV4_FRAG_OFFSET_MASK 0x1fff /* Fragment offset */
117 
118 #define IPV4_ADDR_STR_LEN 16 /* Max IP address length in string format */
119 
120 /* IPV4 packet formats */
121 BWL_PRE_PACKED_STRUCT struct ipv4_addr {
122     uint8 addr[IPV4_ADDR_LEN];
123 } BWL_POST_PACKED_STRUCT;
124 
125 BWL_PRE_PACKED_STRUCT struct ipv4_hdr {
126     uint8 version_ihl; /* Version and Internet Header Length */
127     uint8 tos;         /* Type Of Service */
128     uint16 tot_len;    /* Number of bytes in packet (max 65535) */
129     uint16 id;
130     uint16 frag;                 /* 3 flag bits and fragment offset */
131     uint8 ttl;                   /* Time To Live */
132     uint8 prot;                  /* Protocol */
133     uint16 hdr_chksum;           /* IP header checksum */
134     uint8 src_ip[IPV4_ADDR_LEN]; /* Source IP Address */
135     uint8 dst_ip[IPV4_ADDR_LEN]; /* Destination IP Address */
136 } BWL_POST_PACKED_STRUCT;
137 
138 /* IPV6 field offsets */
139 #define IPV6_PAYLOAD_LEN_OFFSET 4 /* payload length offset */
140 #define IPV6_NEXT_HDR_OFFSET 6    /* next header/protocol offset */
141 #define IPV6_HOP_LIMIT_OFFSET 7   /* hop limit offset */
142 #define IPV6_SRC_IP_OFFSET 8      /* src IP addr offset */
143 #define IPV6_DEST_IP_OFFSET 24    /* dst IP addr offset */
144 
145 /* IPV6 field decodes */
146 #define IPV6_TRAFFIC_CLASS(ipv6_body)                                          \
147     (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) |                               \
148      ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
149 
150 #define IPV6_FLOW_LABEL(ipv6_body)                                             \
151     (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) |                              \
152      (((uint8 *)(ipv6_body))[2] << 8) | (((uint8 *)(ipv6_body))[3]))
153 
154 #define IPV6_PAYLOAD_LEN(ipv6_body)                                            \
155     ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) |              \
156      ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
157 
158 #define IPV6_NEXT_HDR(ipv6_body) (((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)               \
168                                    : 0)
169 
170 #define IP_DSCP46(ip_body) (IP_TOS46(ip_body) >> IPV4_TOS_DSCP_SHIFT);
171 
172 /* IPV4 or IPV6 Protocol Classifier or 0 */
173 #define IP_PROT46(ip_body)                                                     \
174     (IP_VER(ip_body) == IP_VER_4   ? IPV4_PROT(ip_body)                        \
175      : IP_VER(ip_body) == IP_VER_6 ? IPV6_PROT(ip_body)                        \
176                                    : 0)
177 
178 /* IPV6 extension headers (options) */
179 #define IPV6_EXTHDR_HOP 0
180 #define IPV6_EXTHDR_ROUTING 43
181 #define IPV6_EXTHDR_FRAGMENT 44
182 #define IPV6_EXTHDR_AUTH 51
183 #define IPV6_EXTHDR_NONE 59
184 #define IPV6_EXTHDR_DEST 60
185 
186 #define IPV6_EXTHDR(prot)                                                      \
187     (((prot) == IPV6_EXTHDR_HOP) || ((prot) == IPV6_EXTHDR_ROUTING) ||         \
188      ((prot) == IPV6_EXTHDR_FRAGMENT) || ((prot) == IPV6_EXTHDR_AUTH) ||       \
189      ((prot) == IPV6_EXTHDR_NONE) || ((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 
ipv6_exthdr_len(uint8 * h,uint8 * proto)207 static INLINE int32 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 
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)                                                 \
248     ((uint32)addr & 0xff000000) >> 24, ((uint32)addr & 0x00ff0000) >> 16,      \
249         ((uint32)addr & 0x0000ff00) >> 8, ((uint32)addr & 0x000000ff)
250 
251 #endif /* _bcmip_h_ */
252