1 /*
2 * Copyright (C) 1999-2013, Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Fundamental constants relating to IP Protocol
17 *
18 * $Id: bcmip.h 384540 2013-02-12 04:28:58Z $
19 */
20
21 #ifndef _bcmip_h_
22 #define _bcmip_h_
23
24 #ifndef _TYPEDEFS_H_
25 #include <typedefs.h>
26 #endif
27
28 /* This marks the start of a packed structure section. */
29 #include <packed_section_start.h>
30
31
32 /* IPV4 and IPV6 common */
33 #define IP_VER_OFFSET 0x0 /* offset to version field */
34 #define IP_VER_MASK 0xf0 /* version mask */
35 #define IP_VER_SHIFT 4 /* version shift */
36 #define IP_VER_4 4 /* version number for IPV4 */
37 #define IP_VER_6 6 /* version number for IPV6 */
38
39 #define IP_VER(ip_body) \
40 ((((uint8 *)(ip_body))[IP_VER_OFFSET] & IP_VER_MASK) >> IP_VER_SHIFT)
41
42 #define IP_PROT_ICMP 0x1 /* ICMP protocol */
43 #define IP_PROT_IGMP 0x2 /* IGMP protocol */
44 #define IP_PROT_TCP 0x6 /* TCP protocol */
45 #define IP_PROT_UDP 0x11 /* UDP protocol type */
46 #define IP_PROT_ICMP6 0x3a /* ICMPv6 protocol type */
47
48 /* IPV4 field offsets */
49 #define IPV4_VER_HL_OFFSET 0 /* version and ihl byte offset */
50 #define IPV4_TOS_OFFSET 1 /* type of service offset */
51 #define IPV4_PKTLEN_OFFSET 2 /* packet length offset */
52 #define IPV4_PKTFLAG_OFFSET 6 /* more-frag,dont-frag flag offset */
53 #define IPV4_PROT_OFFSET 9 /* protocol type offset */
54 #define IPV4_CHKSUM_OFFSET 10 /* IP header checksum offset */
55 #define IPV4_SRC_IP_OFFSET 12 /* src IP addr offset */
56 #define IPV4_DEST_IP_OFFSET 16 /* dest IP addr offset */
57 #define IPV4_OPTIONS_OFFSET 20 /* IP options offset */
58 #define IPV4_MIN_HEADER_LEN 20 /* Minimum size for an IP header (no options) */
59
60 /* IPV4 field decodes */
61 #define IPV4_VER_MASK 0xf0 /* IPV4 version mask */
62 #define IPV4_VER_SHIFT 4 /* IPV4 version shift */
63
64 #define IPV4_HLEN_MASK 0x0f /* IPV4 header length mask */
65 #define IPV4_HLEN(ipv4_body) (4 * (((uint8 *)(ipv4_body))[IPV4_VER_HL_OFFSET] & IPV4_HLEN_MASK))
66
67 #define IPV4_ADDR_LEN 4 /* IPV4 address length */
68
69 #define IPV4_ADDR_NULL(a) ((((uint8 *)(a))[0] | ((uint8 *)(a))[1] | \
70 ((uint8 *)(a))[2] | ((uint8 *)(a))[3]) == 0)
71
72 #define IPV4_ADDR_BCAST(a) ((((uint8 *)(a))[0] & ((uint8 *)(a))[1] & \
73 ((uint8 *)(a))[2] & ((uint8 *)(a))[3]) == 0xff)
74
75 #define IPV4_TOS_DSCP_MASK 0xfc /* DiffServ codepoint mask */
76 #define IPV4_TOS_DSCP_SHIFT 2 /* DiffServ codepoint shift */
77
78 #define IPV4_TOS(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_TOS_OFFSET])
79
80 #define IPV4_TOS_PREC_MASK 0xe0 /* Historical precedence mask */
81 #define IPV4_TOS_PREC_SHIFT 5 /* Historical precedence shift */
82
83 #define IPV4_TOS_LOWDELAY 0x10 /* Lowest delay requested */
84 #define IPV4_TOS_THROUGHPUT 0x8 /* Best throughput requested */
85 #define IPV4_TOS_RELIABILITY 0x4 /* Most reliable delivery requested */
86
87 #define IPV4_PROT(ipv4_body) (((uint8 *)(ipv4_body))[IPV4_PROT_OFFSET])
88
89 #define IPV4_FRAG_RESV 0x8000 /* Reserved */
90 #define IPV4_FRAG_DONT 0x4000 /* Don't fragment */
91 #define IPV4_FRAG_MORE 0x2000 /* More fragments */
92 #define IPV4_FRAG_OFFSET_MASK 0x1fff /* Fragment offset */
93
94 #define IPV4_ADDR_STR_LEN 16 /* Max IP address length in string format */
95
96 /* IPV4 packet formats */
97 BWL_PRE_PACKED_STRUCT struct ipv4_addr {
98 uint8 addr[IPV4_ADDR_LEN];
99 } BWL_POST_PACKED_STRUCT;
100
101 BWL_PRE_PACKED_STRUCT struct ipv4_hdr {
102 uint8 version_ihl; /* Version and Internet Header Length */
103 uint8 tos; /* Type Of Service */
104 uint16 tot_len; /* Number of bytes in packet (max 65535) */
105 uint16 id;
106 uint16 frag; /* 3 flag bits and fragment offset */
107 uint8 ttl; /* Time To Live */
108 uint8 prot; /* Protocol */
109 uint16 hdr_chksum; /* IP header checksum */
110 uint8 src_ip[IPV4_ADDR_LEN]; /* Source IP Address */
111 uint8 dst_ip[IPV4_ADDR_LEN]; /* Destination IP Address */
112 } BWL_POST_PACKED_STRUCT;
113
114 /* IPV6 field offsets */
115 #define IPV6_PAYLOAD_LEN_OFFSET 4 /* payload length offset */
116 #define IPV6_NEXT_HDR_OFFSET 6 /* next header/protocol offset */
117 #define IPV6_HOP_LIMIT_OFFSET 7 /* hop limit offset */
118 #define IPV6_SRC_IP_OFFSET 8 /* src IP addr offset */
119 #define IPV6_DEST_IP_OFFSET 24 /* dst IP addr offset */
120
121 /* IPV6 field decodes */
122 #define IPV6_TRAFFIC_CLASS(ipv6_body) \
123 (((((uint8 *)(ipv6_body))[0] & 0x0f) << 4) | \
124 ((((uint8 *)(ipv6_body))[1] & 0xf0) >> 4))
125
126 #define IPV6_FLOW_LABEL(ipv6_body) \
127 (((((uint8 *)(ipv6_body))[1] & 0x0f) << 16) | \
128 (((uint8 *)(ipv6_body))[2] << 8) | \
129 (((uint8 *)(ipv6_body))[3]))
130
131 #define IPV6_PAYLOAD_LEN(ipv6_body) \
132 ((((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 0] << 8) | \
133 ((uint8 *)(ipv6_body))[IPV6_PAYLOAD_LEN_OFFSET + 1])
134
135 #define IPV6_NEXT_HDR(ipv6_body) \
136 (((uint8 *)(ipv6_body))[IPV6_NEXT_HDR_OFFSET])
137
138 #define IPV6_PROT(ipv6_body) IPV6_NEXT_HDR(ipv6_body)
139
140 #define IPV6_ADDR_LEN 16 /* IPV6 address length */
141
142 /* IPV4 TOS or IPV6 Traffic Classifier or 0 */
143 #define IP_TOS46(ip_body) \
144 (IP_VER(ip_body) == IP_VER_4 ? IPV4_TOS(ip_body) : \
145 IP_VER(ip_body) == IP_VER_6 ? IPV6_TRAFFIC_CLASS(ip_body) : 0)
146
147 /* IPV6 extension headers (options) */
148 #define IPV6_EXTHDR_HOP 0
149 #define IPV6_EXTHDR_ROUTING 43
150 #define IPV6_EXTHDR_FRAGMENT 44
151 #define IPV6_EXTHDR_AUTH 51
152 #define IPV6_EXTHDR_NONE 59
153 #define IPV6_EXTHDR_DEST 60
154
155 #define IPV6_EXTHDR(prot) (((prot) == IPV6_EXTHDR_HOP) || \
156 ((prot) == IPV6_EXTHDR_ROUTING) || \
157 ((prot) == IPV6_EXTHDR_FRAGMENT) || \
158 ((prot) == IPV6_EXTHDR_AUTH) || \
159 ((prot) == IPV6_EXTHDR_NONE) || \
160 ((prot) == IPV6_EXTHDR_DEST))
161
162 #define IPV6_MIN_HLEN 40
163
164 #define IPV6_EXTHDR_LEN(eh) ((((struct ipv6_exthdr *)(eh))->hdrlen + 1) << 3)
165
166 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr {
167 uint8 nexthdr;
168 uint8 hdrlen;
169 } BWL_POST_PACKED_STRUCT;
170
171 BWL_PRE_PACKED_STRUCT struct ipv6_exthdr_frag {
172 uint8 nexthdr;
173 uint8 rsvd;
174 uint16 frag_off;
175 uint32 ident;
176 } BWL_POST_PACKED_STRUCT;
177
178 static INLINE int32
ipv6_exthdr_len(uint8 * h,uint8 * proto)179 ipv6_exthdr_len(uint8 *h, uint8 *proto)
180 {
181 uint16 len = 0, hlen;
182 struct ipv6_exthdr *eh = (struct ipv6_exthdr *)h;
183
184 while (IPV6_EXTHDR(eh->nexthdr)) {
185 if (eh->nexthdr == IPV6_EXTHDR_NONE)
186 return -1;
187 else if (eh->nexthdr == IPV6_EXTHDR_FRAGMENT)
188 hlen = 8;
189 else if (eh->nexthdr == IPV6_EXTHDR_AUTH)
190 hlen = (eh->hdrlen + 2) << 2;
191 else
192 hlen = IPV6_EXTHDR_LEN(eh);
193
194 len += hlen;
195 eh = (struct ipv6_exthdr *)(h + len);
196 }
197
198 *proto = eh->nexthdr;
199 return len;
200 }
201
202 #define IPV4_ISMULTI(a) (((a) & 0xf0000000) == 0xe0000000)
203
204 /* This marks the end of a packed structure section. */
205 #include <packed_section_end.h>
206
207 #endif /* _bcmip_h_ */
208