• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Fundamental constants relating to IP Protocol
3  *
4  * Copyright (C) 1999-2017, Broadcom Corporation
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  *      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 700076 2017-05-17 14:42:22Z $
28  */
29 
30 #ifndef _bcmip_h_
31 #define _bcmip_h_
32 
33 #ifndef _TYPEDEFS_H_
34 #include <typedefs.h>
35 #endif
36 
37 /* This marks the start of a packed structure section. */
38 #include <packed_section_start.h>
39 
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_ADDR_LEN        4    /* IPV4 address length */
78 
79 #define IPV4_ADDR_NULL(a)    ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \
80                   ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0)
81 
82 #define IPV4_ADDR_BCAST(a)    ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \
83                   ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff)
84 
85 #define    IPV4_TOS_DSCP_MASK    0xfc    /* DiffServ codepoint mask */
86 #define    IPV4_TOS_DSCP_SHIFT    2    /* DiffServ codepoint shift */
87 
88 #define    IPV4_TOS(ipv4_body)    (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
89 
90 #define    IPV4_TOS_PREC_MASK    0xe0    /* Historical precedence mask */
91 #define    IPV4_TOS_PREC_SHIFT    5    /* Historical precedence shift */
92 
93 #define IPV4_TOS_LOWDELAY    0x10    /* Lowest delay requested */
94 #define IPV4_TOS_THROUGHPUT    0x8    /* Best throughput requested */
95 #define IPV4_TOS_RELIABILITY    0x4    /* Most reliable delivery requested */
96 
97 #define IPV4_TOS_ROUTINE        0
98 #define IPV4_TOS_PRIORITY       1
99 #define IPV4_TOS_IMMEDIATE      2
100 #define IPV4_TOS_FLASH          3
101 #define IPV4_TOS_FLASHOVERRIDE  4
102 #define IPV4_TOS_CRITICAL       5
103 #define IPV4_TOS_INETWORK_CTRL  6
104 #define IPV4_TOS_NETWORK_CTRL   7
105 
106 #define IPV4_PROT(ipv4_body)    (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
107 
108 #define IPV4_FRAG_RESV        0x8000    /* Reserved */
109 #define IPV4_FRAG_DONT        0x4000    /* Don't fragment */
110 #define IPV4_FRAG_MORE        0x2000    /* More fragments */
111 #define IPV4_FRAG_OFFSET_MASK    0x1fff    /* Fragment offset */
112 
113 #define IPV4_ADDR_STR_LEN    16    /* Max IP address length in string format */
114 
115 /* IPV4 packet formats */
116 BWL_PRE_PACKED_STRUCT struct ipv4_addr {
117     uint8    addr[IPV4_ADDR_LEN];
118 } BWL_POST_PACKED_STRUCT;
119 
120 BWL_PRE_PACKED_STRUCT struct ipv4_hdr {
121     uint8    version_ihl;        /* Version and Internet Header Length */
122     uint8    tos;            /* Type Of Service */
123     uint16    tot_len;        /* Number of bytes in packet (max 65535) */
124     uint16    id;
125     uint16    frag;            /* 3 flag bits and fragment offset */
126     uint8    ttl;            /* Time To Live */
127     uint8    prot;            /* Protocol */
128     uint16    hdr_chksum;        /* IP header checksum */
129     uint8    src_ip[IPV4_ADDR_LEN];    /* Source IP Address */
130     uint8    dst_ip[IPV4_ADDR_LEN];    /* Destination IP Address */
131 } BWL_POST_PACKED_STRUCT;
132 
133 /* IPV6 field offsets */
134 #define IPV6_PAYLOAD_LEN_OFFSET    4    /* payload length offset */
135 #define IPV6_NEXT_HDR_OFFSET    6    /* next header/protocol offset */
136 #define IPV6_HOP_LIMIT_OFFSET    7    /* hop limit offset */
137 #define IPV6_SRC_IP_OFFSET    8    /* src IP addr offset */
138 #define IPV6_DEST_IP_OFFSET    24    /* dst IP addr offset */
139 
140 /* IPV6 field decodes */
141 #define IPV6_TRAFFIC_CLASS(ipv6_body) \
142     (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \
143      ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
144 
145 #define IPV6_FLOW_LABEL(ipv6_body) \
146     (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \
147      (((uint8 *)(ipv6_body))[2] << 8) | \
148      (((uint8 *)(ipv6_body))[3]))
149 
150 #define IPV6_PAYLOAD_LEN(ipv6_body) \
151     ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \
152      ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
153 
154 #define IPV6_NEXT_HDR(ipv6_body) \
155     (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET])
156 
157 #define IPV6_PROT(ipv6_body)    IPV6_NEXT_HDR(ipv6_body)
158 
159 #define IPV6_ADDR_LEN        16    /* IPV6 address length */
160 
161 /* IPV4 TOS or IPV6 Traffic Classifier or 0 */
162 #define IP_TOS46(ip_body) \
163     (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \
164      IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0)
165 
166 #define IP_DSCP46(ip_body) (IP_TOS46(ip_body) >> IPV4_TOS_DSCP_SHIFT);
167 
168 /* IPV4 or IPV6 Protocol Classifier or 0 */
169 #define IP_PROT46(ip_body) \
170     (IP_VER(ip_body) == IP_VER_4 ? IPV4_PROT(ip_body) : \
171      IP_VER(ip_body) == IP_VER_6 ? IPV6_PROT(ip_body) : 0)
172 
173 /* IPV6 extension headers (options) */
174 #define IPV6_EXTHDR_HOP        0
175 #define IPV6_EXTHDR_ROUTING    43
176 #define IPV6_EXTHDR_FRAGMENT    44
177 #define IPV6_EXTHDR_AUTH    51
178 #define IPV6_EXTHDR_NONE    59
179 #define IPV6_EXTHDR_DEST    60
180 
181 #define IPV6_EXTHDR(prot)    (((prot) == IPV6_EXTHDR_HOP) || \
182                              ((prot) == IPV6_EXTHDR_ROUTING) || \
183                              ((prot) == IPV6_EXTHDR_FRAGMENT) || \
184                              ((prot) == IPV6_EXTHDR_AUTH) || \
185                              ((prot) == IPV6_EXTHDR_NONE) || \
186                              ((prot) == IPV6_EXTHDR_DEST))
187 
188 #define IPV6_MIN_HLEN         40
189 
190 #define IPV6_EXTHDR_LEN(eh)    ((((struct ipv6_exthdr *)(eh))->hdrlen + 1) << 3)
191 
192 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr {
193     uint8    nexthdr;
194     uint8    hdrlen;
195 } BWL_POST_PACKED_STRUCT;
196 
197 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr_frag {
198     uint8    nexthdr;
199     uint8    rsvd;
200     uint16    frag_off;
201     uint32    ident;
202 } BWL_POST_PACKED_STRUCT;
203 
204 static INLINE int32
ipv6_exthdr_len(uint8 * h,uint8 * proto)205 ipv6_exthdr_len(uint8 *h, uint8 *proto)
206 {
207     uint16 len = 0, hlen;
208     struct ipv6_exthdr *eh = (struct ipv6_exthdr *)h;
209 
210     while (IPV6_EXTHDR(eh->nexthdr)) {
211         if (eh->nexthdr == IPV6_EXTHDR_NONE)
212             return -1;
213         else if (eh->nexthdr == IPV6_EXTHDR_FRAGMENT)
214             hlen = 8;
215         else if (eh->nexthdr == IPV6_EXTHDR_AUTH)
216             hlen = (eh->hdrlen + 2) << 2;
217         else
218             hlen = IPV6_EXTHDR_LEN(eh);
219 
220         len += hlen;
221         eh = (struct ipv6_exthdr *)(h + len);
222     }
223 
224     *proto = eh->nexthdr;
225     return len;
226 }
227 
228 #define IPV4_ISMULTI(a) (((a) & 0xf0000000) == 0xe0000000)
229 
230 #define IPV4_MCAST_TO_ETHER_MCAST(ipv4, ether) \
231 { \
232     ether[0] = 0x01; \
233     ether[1] = 0x00; \
234     ether[2] = 0x5E; \
235     ether[3] = (ipv4 & 0x7f0000) >> 16; \
236     ether[4] = (ipv4 & 0xff00) >> 8; \
237     ether[5] = (ipv4 & 0xff); \
238 }
239 
240 /* This marks the end of a packed structure section. */
241 #include <packed_section_end.h>
242 
243 #define IPV4_ADDR_STR "%d.%d.%d.%d"
244 #define IPV4_ADDR_TO_STR(addr)    ((uint32)addr & 0xff000000) >> 24, \
245                                 ((uint32)addr & 0x00ff0000) >> 16, \
246                                 ((uint32)addr & 0x0000ff00) >> 8, \
247                                 ((uint32)addr & 0x000000ff)
248 
249 #endif    /* _bcmip_h_ */
250