]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
6335d074 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 | 15 | */ |
b9e8b45a | 16 | |
064af421 BP |
17 | #ifndef PACKETS_H |
18 | #define PACKETS_H 1 | |
19 | ||
ab9c78ff | 20 | #include <inttypes.h> |
26233bb4 | 21 | #include <sys/types.h> |
064af421 BP |
22 | #include <stdint.h> |
23 | #include <string.h> | |
24 | #include "compiler.h" | |
b211014d | 25 | #include "openvswitch/geneve.h" |
b2bd6da6 | 26 | #include "openvswitch/packets.h" |
0596e897 | 27 | #include "openvswitch/types.h" |
07659514 | 28 | #include "odp-netlink.h" |
064af421 | 29 | #include "random.h" |
7e36ac42 | 30 | #include "hash.h" |
9558d2a5 | 31 | #include "tun-metadata.h" |
754c1feb | 32 | #include "unaligned.h" |
064af421 BP |
33 | #include "util.h" |
34 | ||
cf62fa4c | 35 | struct dp_packet; |
d31f1109 | 36 | struct ds; |
b9e8b45a | 37 | |
59781952 JR |
38 | /* Purely internal to OVS userspace. These flags should never be exposed to |
39 | * the outside world and so aren't included in the flags mask. */ | |
40 | ||
41 | /* Tunnel information is in userspace datapath format. */ | |
42 | #define FLOW_TNL_F_UDPIF (1 << 4) | |
43 | ||
ffe4c74f JB |
44 | static inline bool ipv6_addr_is_set(const struct in6_addr *addr); |
45 | ||
46 | static inline bool | |
47 | flow_tnl_dst_is_set(const struct flow_tnl *tnl) | |
48 | { | |
49 | return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst); | |
50 | } | |
51 | ||
52 | struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl); | |
53 | struct in6_addr flow_tnl_src(const struct flow_tnl *tnl); | |
54 | ||
59781952 JR |
55 | /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */ |
56 | static inline size_t | |
57 | flow_tnl_size(const struct flow_tnl *src) | |
58 | { | |
ffe4c74f JB |
59 | if (!flow_tnl_dst_is_set(src)) { |
60 | /* Covers ip_dst and ipv6_dst only. */ | |
59781952 JR |
61 | return offsetof(struct flow_tnl, ip_src); |
62 | } | |
63 | if (src->flags & FLOW_TNL_F_UDPIF) { | |
64 | /* Datapath format, cover all options we have. */ | |
65 | return offsetof(struct flow_tnl, metadata.opts) | |
66 | + src->metadata.present.len; | |
67 | } | |
68 | if (!src->metadata.present.map) { | |
69 | /* No TLVs, opts is irrelevant. */ | |
70 | return offsetof(struct flow_tnl, metadata.opts); | |
71 | } | |
72 | /* Have decoded TLVs, opts is relevant. */ | |
73 | return sizeof *src; | |
74 | } | |
75 | ||
76 | /* Copy flow_tnl, but avoid copying unused portions of tun_metadata. Unused | |
77 | * data in 'dst' is NOT cleared, so this must not be used in cases where the | |
78 | * uninitialized portion may be hashed over. */ | |
79 | static inline void | |
80 | flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src) | |
81 | { | |
82 | memcpy(dst, src, flow_tnl_size(src)); | |
83 | } | |
84 | ||
85 | static inline bool | |
86 | flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b) | |
87 | { | |
88 | size_t a_size = flow_tnl_size(a); | |
89 | ||
90 | return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size); | |
91 | } | |
92 | ||
758c456d JR |
93 | /* Datapath packet metadata */ |
94 | struct pkt_metadata { | |
572f732a AZ |
95 | uint32_t recirc_id; /* Recirculation id carried with the |
96 | recirculating packets. 0 for packets | |
97 | received from the wire. */ | |
98 | uint32_t dp_hash; /* hash value computed by the recirculation | |
99 | action. */ | |
758c456d JR |
100 | uint32_t skb_priority; /* Packet priority for QoS. */ |
101 | uint32_t pkt_mark; /* Packet mark. */ | |
07659514 JS |
102 | uint16_t ct_state; /* Connection state. */ |
103 | uint16_t ct_zone; /* Connection zone. */ | |
8e53fe8c | 104 | uint32_t ct_mark; /* Connection mark. */ |
9daf2348 | 105 | ovs_u128 ct_label; /* Connection label. */ |
b5e7e61a | 106 | union flow_in_port in_port; /* Input port. */ |
59781952 JR |
107 | struct flow_tnl tunnel; /* Encapsulating tunnel parameters. Note that |
108 | * if 'ip_dst' == 0, the rest of the fields may | |
109 | * be uninitialized. */ | |
758c456d JR |
110 | }; |
111 | ||
6b241d64 PS |
112 | static inline void |
113 | pkt_metadata_init_tnl(struct pkt_metadata *md) | |
114 | { | |
115 | /* Zero up through the tunnel metadata options. The length and table | |
116 | * are before this and as long as they are empty, the options won't | |
117 | * be looked at. */ | |
118 | memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts)); | |
119 | } | |
120 | ||
35303d71 JG |
121 | static inline void |
122 | pkt_metadata_init(struct pkt_metadata *md, odp_port_t port) | |
123 | { | |
124 | /* It can be expensive to zero out all of the tunnel metadata. However, | |
125 | * we can just zero out ip_dst and the rest of the data will never be | |
126 | * looked at. */ | |
07659514 | 127 | memset(md, 0, offsetof(struct pkt_metadata, in_port)); |
35303d71 | 128 | md->tunnel.ip_dst = 0; |
ffe4c74f | 129 | md->tunnel.ipv6_dst = in6addr_any; |
35303d71 JG |
130 | |
131 | md->in_port.odp_port = port; | |
132 | } | |
b5e7e61a | 133 | |
a90ed026 DDP |
134 | /* This function prefetches the cachelines touched by pkt_metadata_init() |
135 | * For performance reasons the two functions should be kept in sync. */ | |
136 | static inline void | |
137 | pkt_metadata_prefetch_init(struct pkt_metadata *md) | |
138 | { | |
139 | ovs_prefetch_range(md, offsetof(struct pkt_metadata, tunnel.ip_src)); | |
140 | } | |
141 | ||
76343538 BP |
142 | bool dpid_from_string(const char *s, uint64_t *dpidp); |
143 | ||
064af421 BP |
144 | #define ETH_ADDR_LEN 6 |
145 | ||
74ff3298 JR |
146 | static const struct eth_addr eth_addr_broadcast OVS_UNUSED |
147 | = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }; | |
064af421 | 148 | |
74ff3298 JR |
149 | static const struct eth_addr eth_addr_exact OVS_UNUSED |
150 | = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }; | |
eb0b295e | 151 | |
74ff3298 JR |
152 | static const struct eth_addr eth_addr_zero OVS_UNUSED |
153 | = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }; | |
ba186119 | 154 | |
74ff3298 JR |
155 | static const struct eth_addr eth_addr_stp OVS_UNUSED |
156 | = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } }; | |
c25c91fd | 157 | |
74ff3298 JR |
158 | static const struct eth_addr eth_addr_lacp OVS_UNUSED |
159 | = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } }; | |
de8d2ef9 | 160 | |
74ff3298 JR |
161 | static const struct eth_addr eth_addr_bfd OVS_UNUSED |
162 | = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } }; | |
163 | ||
164 | static inline bool eth_addr_is_broadcast(const struct eth_addr a) | |
064af421 | 165 | { |
74ff3298 | 166 | return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff); |
064af421 BP |
167 | } |
168 | ||
74ff3298 | 169 | static inline bool eth_addr_is_multicast(const struct eth_addr a) |
064af421 | 170 | { |
74ff3298 | 171 | return a.ea[0] & 1; |
064af421 | 172 | } |
74ff3298 JR |
173 | |
174 | static inline bool eth_addr_is_local(const struct eth_addr a) | |
064af421 | 175 | { |
70150daf JG |
176 | /* Local if it is either a locally administered address or a Nicira random |
177 | * address. */ | |
74ff3298 JR |
178 | return a.ea[0] & 2 |
179 | || (a.be16[0] == htons(0x0023) | |
180 | && (a.be16[1] & htons(0xff80)) == htons(0x2080)); | |
064af421 | 181 | } |
74ff3298 | 182 | static inline bool eth_addr_is_zero(const struct eth_addr a) |
064af421 | 183 | { |
74ff3298 | 184 | return !(a.be16[0] | a.be16[1] | a.be16[2]); |
064af421 | 185 | } |
3b4d8ad3 | 186 | |
74ff3298 | 187 | static inline int eth_mask_is_exact(const struct eth_addr a) |
3b4d8ad3 | 188 | { |
74ff3298 | 189 | return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff); |
3b4d8ad3 JS |
190 | } |
191 | ||
74ff3298 JR |
192 | static inline int eth_addr_compare_3way(const struct eth_addr a, |
193 | const struct eth_addr b) | |
130f6e5f | 194 | { |
74ff3298 | 195 | return memcmp(&a, &b, sizeof a); |
130f6e5f | 196 | } |
74ff3298 JR |
197 | |
198 | static inline bool eth_addr_equals(const struct eth_addr a, | |
199 | const struct eth_addr b) | |
064af421 | 200 | { |
130f6e5f | 201 | return !eth_addr_compare_3way(a, b); |
064af421 | 202 | } |
74ff3298 JR |
203 | |
204 | static inline bool eth_addr_equal_except(const struct eth_addr a, | |
205 | const struct eth_addr b, | |
206 | const struct eth_addr mask) | |
3b4d8ad3 | 207 | { |
74ff3298 JR |
208 | return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0]) |
209 | || ((a.be16[1] ^ b.be16[1]) & mask.be16[1]) | |
210 | || ((a.be16[2] ^ b.be16[2]) & mask.be16[2])); | |
3b4d8ad3 | 211 | } |
74ff3298 JR |
212 | |
213 | static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea) | |
064af421 | 214 | { |
74ff3298 JR |
215 | return (((uint64_t) ntohs(ea.be16[0]) << 32) |
216 | | ((uint64_t) ntohs(ea.be16[1]) << 16) | |
217 | | ntohs(ea.be16[2])); | |
064af421 | 218 | } |
74ff3298 JR |
219 | |
220 | static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea, | |
7e36ac42 AZ |
221 | uint16_t vlan) |
222 | { | |
223 | return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea)); | |
224 | } | |
74ff3298 JR |
225 | |
226 | static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea) | |
227 | { | |
228 | ea->be16[0] = htons(x >> 32); | |
37ba4764 AC |
229 | ea->be16[1] = htons((x & 0xFFFF0000) >> 16); |
230 | ea->be16[2] = htons(x & 0xFFFF); | |
74ff3298 JR |
231 | } |
232 | ||
233 | static inline struct eth_addr eth_addr_invert(const struct eth_addr src) | |
064af421 | 234 | { |
74ff3298 JR |
235 | struct eth_addr dst; |
236 | ||
237 | for (int i = 0; i < ARRAY_SIZE(src.be16); i++) { | |
238 | dst.be16[i] = ~src.be16[i]; | |
239 | } | |
240 | ||
241 | return dst; | |
064af421 | 242 | } |
74ff3298 JR |
243 | |
244 | static inline void eth_addr_mark_random(struct eth_addr *ea) | |
064af421 | 245 | { |
74ff3298 JR |
246 | ea->ea[0] &= ~1; /* Unicast. */ |
247 | ea->ea[0] |= 2; /* Private. */ | |
064af421 | 248 | } |
74ff3298 JR |
249 | |
250 | static inline void eth_addr_random(struct eth_addr *ea) | |
064af421 | 251 | { |
74ff3298 | 252 | random_bytes((uint8_t *)ea, sizeof *ea); |
064af421 BP |
253 | eth_addr_mark_random(ea); |
254 | } | |
74ff3298 JR |
255 | |
256 | static inline void eth_addr_nicira_random(struct eth_addr *ea) | |
70150daf JG |
257 | { |
258 | eth_addr_random(ea); | |
259 | ||
260 | /* Set the OUI to the Nicira one. */ | |
74ff3298 JR |
261 | ea->ea[0] = 0x00; |
262 | ea->ea[1] = 0x23; | |
263 | ea->ea[2] = 0x20; | |
70150daf JG |
264 | |
265 | /* Set the top bit to indicate random Nicira address. */ | |
74ff3298 | 266 | ea->ea[3] |= 0x80; |
70150daf | 267 | } |
74ff3298 | 268 | static inline uint32_t hash_mac(const struct eth_addr ea, |
7e36ac42 AZ |
269 | const uint16_t vlan, const uint32_t basis) |
270 | { | |
271 | return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis); | |
272 | } | |
064af421 | 273 | |
74ff3298 JR |
274 | bool eth_addr_is_reserved(const struct eth_addr); |
275 | bool eth_addr_from_string(const char *, struct eth_addr *); | |
76343538 | 276 | |
74ff3298 | 277 | void compose_rarp(struct dp_packet *, const struct eth_addr); |
b9e8b45a | 278 | |
cf62fa4c PS |
279 | void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci); |
280 | void eth_pop_vlan(struct dp_packet *); | |
7c66b273 | 281 | |
cf62fa4c | 282 | const char *eth_from_hex(const char *hex, struct dp_packet **packetp); |
74ff3298 JR |
283 | void eth_format_masked(const struct eth_addr ea, |
284 | const struct eth_addr *mask, struct ds *s); | |
e22f1753 | 285 | |
cf62fa4c PS |
286 | void set_mpls_lse(struct dp_packet *, ovs_be32 label); |
287 | void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse); | |
288 | void pop_mpls(struct dp_packet *, ovs_be16 ethtype); | |
b02475c5 | 289 | |
b676167a | 290 | void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl); |
b02475c5 SH |
291 | void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc); |
292 | void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label); | |
293 | void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos); | |
294 | ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos, | |
295 | ovs_be32 label); | |
296 | ||
eaa71334 BP |
297 | /* Example: |
298 | * | |
74ff3298 | 299 | * struct eth_addr mac; |
eaa71334 BP |
300 | * [...] |
301 | * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac)); | |
302 | * | |
303 | */ | |
064af421 BP |
304 | #define ETH_ADDR_FMT \ |
305 | "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8 | |
ca92d173 AC |
306 | #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea) |
307 | #define ETH_ADDR_BYTES_ARGS(EAB) \ | |
308 | (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5] | |
b0d390e5 | 309 | #define ETH_ADDR_STRLEN 17 |
064af421 | 310 | |
eaa71334 BP |
311 | /* Example: |
312 | * | |
313 | * char *string = "1 00:11:22:33:44:55 2"; | |
74ff3298 | 314 | * struct eth_addr mac; |
eaa71334 BP |
315 | * int a, b; |
316 | * | |
c2c28dfd BP |
317 | * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d", |
318 | * &a, ETH_ADDR_SCAN_ARGS(mac), &b)) { | |
eaa71334 BP |
319 | * ... |
320 | * } | |
321 | */ | |
322 | #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8 | |
74ff3298 JR |
323 | #define ETH_ADDR_SCAN_ARGS(EA) \ |
324 | &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5] | |
eaa71334 | 325 | |
064af421 BP |
326 | #define ETH_TYPE_IP 0x0800 |
327 | #define ETH_TYPE_ARP 0x0806 | |
a36de779 | 328 | #define ETH_TYPE_TEB 0x6558 |
3e34fbdd IY |
329 | #define ETH_TYPE_VLAN_8021Q 0x8100 |
330 | #define ETH_TYPE_VLAN ETH_TYPE_VLAN_8021Q | |
331 | #define ETH_TYPE_VLAN_8021AD 0x88a8 | |
d31f1109 | 332 | #define ETH_TYPE_IPV6 0x86dd |
c25c91fd | 333 | #define ETH_TYPE_LACP 0x8809 |
38f7147c | 334 | #define ETH_TYPE_RARP 0x8035 |
4fba171d BP |
335 | #define ETH_TYPE_MPLS 0x8847 |
336 | #define ETH_TYPE_MPLS_MCAST 0x8848 | |
064af421 | 337 | |
b02475c5 SH |
338 | static inline bool eth_type_mpls(ovs_be16 eth_type) |
339 | { | |
340 | return eth_type == htons(ETH_TYPE_MPLS) || | |
341 | eth_type == htons(ETH_TYPE_MPLS_MCAST); | |
342 | } | |
343 | ||
d6943394 TH |
344 | static inline bool eth_type_vlan(ovs_be16 eth_type) |
345 | { | |
346 | return eth_type == htons(ETH_TYPE_VLAN_8021Q) || | |
347 | eth_type == htons(ETH_TYPE_VLAN_8021AD); | |
348 | } | |
349 | ||
350 | ||
36956a7d BP |
351 | /* Minimum value for an Ethernet type. Values below this are IEEE 802.2 frame |
352 | * lengths. */ | |
353 | #define ETH_TYPE_MIN 0x600 | |
354 | ||
064af421 BP |
355 | #define ETH_HEADER_LEN 14 |
356 | #define ETH_PAYLOAD_MIN 46 | |
357 | #define ETH_PAYLOAD_MAX 1500 | |
358 | #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN) | |
359 | #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX) | |
360 | #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX) | |
13b6bae6 | 361 | OVS_PACKED( |
064af421 | 362 | struct eth_header { |
74ff3298 JR |
363 | struct eth_addr eth_dst; |
364 | struct eth_addr eth_src; | |
d5ca4fce | 365 | ovs_be16 eth_type; |
13b6bae6 | 366 | }); |
064af421 BP |
367 | BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header)); |
368 | ||
369 | #define LLC_DSAP_SNAP 0xaa | |
370 | #define LLC_SSAP_SNAP 0xaa | |
371 | #define LLC_CNTL_SNAP 3 | |
372 | ||
373 | #define LLC_HEADER_LEN 3 | |
13b6bae6 | 374 | OVS_PACKED( |
064af421 BP |
375 | struct llc_header { |
376 | uint8_t llc_dsap; | |
377 | uint8_t llc_ssap; | |
378 | uint8_t llc_cntl; | |
13b6bae6 | 379 | }); |
064af421 BP |
380 | BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header)); |
381 | ||
9efd308e DV |
382 | /* LLC field values used for STP frames. */ |
383 | #define STP_LLC_SSAP 0x42 | |
384 | #define STP_LLC_DSAP 0x42 | |
385 | #define STP_LLC_CNTL 0x03 | |
386 | ||
064af421 BP |
387 | #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so |
388 | sizeof(SNAP_ORG_ETHERNET) == 3. */ | |
389 | #define SNAP_HEADER_LEN 5 | |
13b6bae6 | 390 | OVS_PACKED( |
064af421 BP |
391 | struct snap_header { |
392 | uint8_t snap_org[3]; | |
d5ca4fce | 393 | ovs_be16 snap_type; |
13b6bae6 | 394 | }); |
064af421 BP |
395 | BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header)); |
396 | ||
397 | #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN) | |
13b6bae6 | 398 | OVS_PACKED( |
064af421 BP |
399 | struct llc_snap_header { |
400 | struct llc_header llc; | |
401 | struct snap_header snap; | |
13b6bae6 | 402 | }); |
064af421 BP |
403 | BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header)); |
404 | ||
405 | #define VLAN_VID_MASK 0x0fff | |
33ce24ed BP |
406 | #define VLAN_VID_SHIFT 0 |
407 | ||
064af421 | 408 | #define VLAN_PCP_MASK 0xe000 |
33ce24ed | 409 | #define VLAN_PCP_SHIFT 13 |
064af421 | 410 | |
26233bb4 | 411 | #define VLAN_CFI 0x1000 |
e6cc0bab | 412 | #define VLAN_CFI_SHIFT 12 |
26233bb4 BP |
413 | |
414 | /* Given the vlan_tci field from an 802.1Q header, in network byte order, | |
415 | * returns the VLAN ID in host byte order. */ | |
416 | static inline uint16_t | |
d5ca4fce | 417 | vlan_tci_to_vid(ovs_be16 vlan_tci) |
26233bb4 BP |
418 | { |
419 | return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT; | |
420 | } | |
421 | ||
422 | /* Given the vlan_tci field from an 802.1Q header, in network byte order, | |
423 | * returns the priority code point (PCP) in host byte order. */ | |
424 | static inline int | |
d5ca4fce | 425 | vlan_tci_to_pcp(ovs_be16 vlan_tci) |
26233bb4 BP |
426 | { |
427 | return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT; | |
428 | } | |
429 | ||
e6cc0bab AZ |
430 | /* Given the vlan_tci field from an 802.1Q header, in network byte order, |
431 | * returns the Canonical Format Indicator (CFI). */ | |
432 | static inline int | |
433 | vlan_tci_to_cfi(ovs_be16 vlan_tci) | |
434 | { | |
435 | return (vlan_tci & htons(VLAN_CFI)) != 0; | |
436 | } | |
437 | ||
064af421 BP |
438 | #define VLAN_HEADER_LEN 4 |
439 | struct vlan_header { | |
d5ca4fce EJ |
440 | ovs_be16 vlan_tci; /* Lowest 12 bits are VLAN ID. */ |
441 | ovs_be16 vlan_next_type; | |
064af421 BP |
442 | }; |
443 | BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header)); | |
444 | ||
445 | #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN) | |
13b6bae6 | 446 | OVS_PACKED( |
064af421 | 447 | struct vlan_eth_header { |
74ff3298 JR |
448 | struct eth_addr veth_dst; |
449 | struct eth_addr veth_src; | |
d5ca4fce EJ |
450 | ovs_be16 veth_type; /* Always htons(ETH_TYPE_VLAN). */ |
451 | ovs_be16 veth_tci; /* Lowest 12 bits are VLAN ID. */ | |
452 | ovs_be16 veth_next_type; | |
13b6bae6 | 453 | }); |
064af421 BP |
454 | BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header)); |
455 | ||
b02475c5 SH |
456 | /* MPLS related definitions */ |
457 | #define MPLS_TTL_MASK 0x000000ff | |
458 | #define MPLS_TTL_SHIFT 0 | |
459 | ||
460 | #define MPLS_BOS_MASK 0x00000100 | |
461 | #define MPLS_BOS_SHIFT 8 | |
462 | ||
463 | #define MPLS_TC_MASK 0x00000e00 | |
464 | #define MPLS_TC_SHIFT 9 | |
465 | ||
466 | #define MPLS_LABEL_MASK 0xfffff000 | |
467 | #define MPLS_LABEL_SHIFT 12 | |
468 | ||
469 | #define MPLS_HLEN 4 | |
470 | ||
471 | struct mpls_hdr { | |
5fa008d4 | 472 | ovs_16aligned_be32 mpls_lse; |
b02475c5 SH |
473 | }; |
474 | BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr)); | |
475 | ||
476 | /* Given a mpls label stack entry in network byte order | |
477 | * return mpls label in host byte order */ | |
478 | static inline uint32_t | |
479 | mpls_lse_to_label(ovs_be32 mpls_lse) | |
480 | { | |
481 | return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT; | |
482 | } | |
483 | ||
484 | /* Given a mpls label stack entry in network byte order | |
485 | * return mpls tc */ | |
486 | static inline uint8_t | |
487 | mpls_lse_to_tc(ovs_be32 mpls_lse) | |
488 | { | |
489 | return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT; | |
490 | } | |
491 | ||
492 | /* Given a mpls label stack entry in network byte order | |
493 | * return mpls ttl */ | |
494 | static inline uint8_t | |
495 | mpls_lse_to_ttl(ovs_be32 mpls_lse) | |
496 | { | |
497 | return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT; | |
498 | } | |
499 | ||
500 | /* Set TTL in mpls lse. */ | |
501 | static inline void | |
502 | flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl) | |
503 | { | |
504 | *mpls_lse &= ~htonl(MPLS_TTL_MASK); | |
505 | *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT); | |
506 | } | |
507 | ||
508 | /* Given a mpls label stack entry in network byte order | |
509 | * return mpls BoS bit */ | |
510 | static inline uint8_t | |
511 | mpls_lse_to_bos(ovs_be32 mpls_lse) | |
512 | { | |
513 | return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0; | |
514 | } | |
515 | ||
ed36537e | 516 | #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32 |
064af421 | 517 | #define IP_ARGS(ip) \ |
ed36537e BP |
518 | ntohl(ip) >> 24, \ |
519 | (ntohl(ip) >> 16) & 0xff, \ | |
520 | (ntohl(ip) >> 8) & 0xff, \ | |
521 | ntohl(ip) & 0xff | |
064af421 | 522 | |
3bffc610 BP |
523 | /* Example: |
524 | * | |
525 | * char *string = "1 33.44.55.66 2"; | |
526 | * ovs_be32 ip; | |
527 | * int a, b; | |
528 | * | |
c2c28dfd | 529 | * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) { |
3bffc610 BP |
530 | * ... |
531 | * } | |
532 | */ | |
533 | #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8 | |
534 | #define IP_SCAN_ARGS(ip) \ | |
535 | ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]), \ | |
536 | &((uint8_t *) ip)[1], \ | |
537 | &((uint8_t *) ip)[2], \ | |
538 | &((uint8_t *) ip)[3] | |
3bffc610 | 539 | |
0596e897 BP |
540 | /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N |
541 | * high-order 1-bits and 32-N low-order 0-bits. */ | |
542 | static inline bool | |
543 | ip_is_cidr(ovs_be32 netmask) | |
544 | { | |
545 | uint32_t x = ~ntohl(netmask); | |
546 | return !(x & (x + 1)); | |
547 | } | |
b37e6334 BP |
548 | static inline bool |
549 | ip_is_multicast(ovs_be32 ip) | |
550 | { | |
551 | return (ip & htonl(0xf0000000)) == htonl(0xe0000000); | |
552 | } | |
7e598a7d FL |
553 | static inline bool |
554 | ip_is_local_multicast(ovs_be32 ip) | |
555 | { | |
556 | return (ip & htonl(0xffffff00)) == htonl(0xe0000000); | |
557 | } | |
aad29cd1 BP |
558 | int ip_count_cidr_bits(ovs_be32 netmask); |
559 | void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *); | |
2b02db1b | 560 | bool ip_parse(const char *s, ovs_be32 *ip); |
61440451 BP |
561 | char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask) |
562 | OVS_WARN_UNUSED_RESULT; | |
2b02db1b BP |
563 | char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen) |
564 | OVS_WARN_UNUSED_RESULT; | |
7dc88496 NS |
565 | char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask) |
566 | OVS_WARN_UNUSED_RESULT; | |
567 | char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip, | |
568 | unsigned int *plen) | |
569 | OVS_WARN_UNUSED_RESULT; | |
0596e897 | 570 | |
064af421 BP |
571 | #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4) |
572 | #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15) | |
573 | #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl)) | |
574 | ||
6c99379e BP |
575 | #ifndef IPPROTO_SCTP |
576 | #define IPPROTO_SCTP 132 | |
577 | #endif | |
578 | ||
f1193301 | 579 | /* TOS fields. */ |
495fe264 JG |
580 | #define IP_ECN_NOT_ECT 0x0 |
581 | #define IP_ECN_ECT_1 0x01 | |
582 | #define IP_ECN_ECT_0 0x02 | |
583 | #define IP_ECN_CE 0x03 | |
f1193301 BP |
584 | #define IP_ECN_MASK 0x03 |
585 | #define IP_DSCP_MASK 0xfc | |
586 | ||
1bc3f0ed PS |
587 | static inline int |
588 | IP_ECN_is_ce(uint8_t dsfield) | |
589 | { | |
590 | return (dsfield & IP_ECN_MASK) == IP_ECN_CE; | |
591 | } | |
592 | ||
064af421 BP |
593 | #define IP_VERSION 4 |
594 | ||
595 | #define IP_DONT_FRAGMENT 0x4000 /* Don't fragment. */ | |
596 | #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */ | |
597 | #define IP_FRAG_OFF_MASK 0x1fff /* Fragment offset. */ | |
598 | #define IP_IS_FRAGMENT(ip_frag_off) \ | |
599 | ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK)) | |
600 | ||
601 | #define IP_HEADER_LEN 20 | |
602 | struct ip_header { | |
603 | uint8_t ip_ihl_ver; | |
604 | uint8_t ip_tos; | |
d5ca4fce EJ |
605 | ovs_be16 ip_tot_len; |
606 | ovs_be16 ip_id; | |
607 | ovs_be16 ip_frag_off; | |
064af421 BP |
608 | uint8_t ip_ttl; |
609 | uint8_t ip_proto; | |
d5ca4fce | 610 | ovs_be16 ip_csum; |
7c457c33 BP |
611 | ovs_16aligned_be32 ip_src; |
612 | ovs_16aligned_be32 ip_dst; | |
064af421 | 613 | }; |
a36de779 | 614 | |
064af421 BP |
615 | BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header)); |
616 | ||
c4ccff78 | 617 | #define ICMP_HEADER_LEN 8 |
064af421 BP |
618 | struct icmp_header { |
619 | uint8_t icmp_type; | |
620 | uint8_t icmp_code; | |
d5ca4fce | 621 | ovs_be16 icmp_csum; |
c4ccff78 JG |
622 | union { |
623 | struct { | |
624 | ovs_be16 id; | |
625 | ovs_be16 seq; | |
626 | } echo; | |
627 | struct { | |
628 | ovs_be16 empty; | |
629 | ovs_be16 mtu; | |
630 | } frag; | |
7c457c33 | 631 | ovs_16aligned_be32 gateway; |
c4ccff78 | 632 | } icmp_fields; |
064af421 BP |
633 | }; |
634 | BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header)); | |
635 | ||
8e451a96 DB |
636 | #define IGMP_HEADER_LEN 8 |
637 | struct igmp_header { | |
638 | uint8_t igmp_type; | |
639 | uint8_t igmp_code; | |
640 | ovs_be16 igmp_csum; | |
641 | ovs_16aligned_be32 group; | |
642 | }; | |
643 | BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header)); | |
644 | ||
e3102e42 TLSC |
645 | #define IGMPV3_HEADER_LEN 8 |
646 | struct igmpv3_header { | |
647 | uint8_t type; | |
648 | uint8_t rsvr1; | |
649 | ovs_be16 csum; | |
650 | ovs_be16 rsvr2; | |
651 | ovs_be16 ngrp; | |
652 | }; | |
653 | BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header)); | |
654 | ||
655 | #define IGMPV3_RECORD_LEN 8 | |
656 | struct igmpv3_record { | |
657 | uint8_t type; | |
658 | uint8_t aux_len; | |
659 | ovs_be16 nsrcs; | |
660 | ovs_16aligned_be32 maddr; | |
661 | }; | |
662 | BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record)); | |
663 | ||
8e451a96 DB |
664 | #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */ |
665 | #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */ | |
666 | #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */ | |
667 | #define IGMP_HOST_LEAVE_MESSAGE 0x17 | |
668 | #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */ | |
669 | ||
06994f87 TLSC |
670 | /* |
671 | * IGMPv3 and MLDv2 use the same codes. | |
672 | */ | |
e3102e42 TLSC |
673 | #define IGMPV3_MODE_IS_INCLUDE 1 |
674 | #define IGMPV3_MODE_IS_EXCLUDE 2 | |
675 | #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3 | |
676 | #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4 | |
677 | #define IGMPV3_ALLOW_NEW_SOURCES 5 | |
678 | #define IGMPV3_BLOCK_OLD_SOURCES 6 | |
679 | ||
c6bcb685 JS |
680 | #define SCTP_HEADER_LEN 12 |
681 | struct sctp_header { | |
682 | ovs_be16 sctp_src; | |
683 | ovs_be16 sctp_dst; | |
5fa008d4 BP |
684 | ovs_16aligned_be32 sctp_vtag; |
685 | ovs_16aligned_be32 sctp_csum; | |
c6bcb685 JS |
686 | }; |
687 | BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header)); | |
688 | ||
064af421 BP |
689 | #define UDP_HEADER_LEN 8 |
690 | struct udp_header { | |
d5ca4fce EJ |
691 | ovs_be16 udp_src; |
692 | ovs_be16 udp_dst; | |
693 | ovs_be16 udp_len; | |
694 | ovs_be16 udp_csum; | |
064af421 BP |
695 | }; |
696 | BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header)); | |
697 | ||
a66733a8 JR |
698 | #define TCP_FIN 0x001 |
699 | #define TCP_SYN 0x002 | |
700 | #define TCP_RST 0x004 | |
701 | #define TCP_PSH 0x008 | |
702 | #define TCP_ACK 0x010 | |
703 | #define TCP_URG 0x020 | |
704 | #define TCP_ECE 0x040 | |
705 | #define TCP_CWR 0x080 | |
706 | #define TCP_NS 0x100 | |
064af421 | 707 | |
df9b6612 | 708 | #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12))) |
a66733a8 | 709 | #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff) |
419681da | 710 | #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff)) |
d84d4b88 | 711 | #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12) |
064af421 BP |
712 | |
713 | #define TCP_HEADER_LEN 20 | |
714 | struct tcp_header { | |
d5ca4fce EJ |
715 | ovs_be16 tcp_src; |
716 | ovs_be16 tcp_dst; | |
7c457c33 BP |
717 | ovs_16aligned_be32 tcp_seq; |
718 | ovs_16aligned_be32 tcp_ack; | |
d5ca4fce EJ |
719 | ovs_be16 tcp_ctl; |
720 | ovs_be16 tcp_winsz; | |
721 | ovs_be16 tcp_csum; | |
722 | ovs_be16 tcp_urg; | |
064af421 BP |
723 | }; |
724 | BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header)); | |
725 | ||
07659514 | 726 | /* Connection states */ |
aa68cf38 RB |
727 | enum { |
728 | CS_NEW_BIT = 0, | |
729 | CS_ESTABLISHED_BIT = 1, | |
730 | CS_RELATED_BIT = 2, | |
731 | CS_REPLY_DIR_BIT = 3, | |
732 | CS_INVALID_BIT = 4, | |
733 | CS_TRACKED_BIT = 5, | |
734 | CS_SRC_NAT_BIT = 6, | |
735 | CS_DST_NAT_BIT = 7, | |
736 | }; | |
737 | ||
738 | enum { | |
739 | CS_NEW = (1 << CS_NEW_BIT), | |
740 | CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT), | |
741 | CS_RELATED = (1 << CS_RELATED_BIT), | |
742 | CS_REPLY_DIR = (1 << CS_REPLY_DIR_BIT), | |
743 | CS_INVALID = (1 << CS_INVALID_BIT), | |
744 | CS_TRACKED = (1 << CS_TRACKED_BIT), | |
745 | CS_SRC_NAT = (1 << CS_SRC_NAT_BIT), | |
746 | CS_DST_NAT = (1 << CS_DST_NAT_BIT), | |
747 | }; | |
07659514 JS |
748 | |
749 | /* Undefined connection state bits. */ | |
750 | #define CS_SUPPORTED_MASK (CS_NEW | CS_ESTABLISHED | CS_RELATED \ | |
9ac0aada JR |
751 | | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \ |
752 | | CS_SRC_NAT | CS_DST_NAT) | |
07659514 JS |
753 | #define CS_UNSUPPORTED_MASK (~(uint32_t)CS_SUPPORTED_MASK) |
754 | ||
064af421 BP |
755 | #define ARP_HRD_ETHERNET 1 |
756 | #define ARP_PRO_IP 0x0800 | |
757 | #define ARP_OP_REQUEST 1 | |
758 | #define ARP_OP_REPLY 2 | |
7cb57d10 | 759 | #define ARP_OP_RARP 3 |
064af421 BP |
760 | |
761 | #define ARP_ETH_HEADER_LEN 28 | |
762 | struct arp_eth_header { | |
763 | /* Generic members. */ | |
d5ca4fce EJ |
764 | ovs_be16 ar_hrd; /* Hardware type. */ |
765 | ovs_be16 ar_pro; /* Protocol type. */ | |
064af421 BP |
766 | uint8_t ar_hln; /* Hardware address length. */ |
767 | uint8_t ar_pln; /* Protocol address length. */ | |
d5ca4fce | 768 | ovs_be16 ar_op; /* Opcode. */ |
064af421 BP |
769 | |
770 | /* Ethernet+IPv4 specific members. */ | |
74ff3298 JR |
771 | struct eth_addr ar_sha; /* Sender hardware address. */ |
772 | ovs_16aligned_be32 ar_spa; /* Sender protocol address. */ | |
773 | struct eth_addr ar_tha; /* Target hardware address. */ | |
774 | ovs_16aligned_be32 ar_tpa; /* Target protocol address. */ | |
7c457c33 | 775 | }; |
064af421 BP |
776 | BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header)); |
777 | ||
370e373b TLSC |
778 | #define IPV6_HEADER_LEN 40 |
779 | ||
4528f34f BP |
780 | /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on |
781 | * most implementations, this one only requires 16-bit alignment. */ | |
782 | union ovs_16aligned_in6_addr { | |
783 | ovs_be16 be16[8]; | |
784 | ovs_16aligned_be32 be32[4]; | |
785 | }; | |
786 | ||
787 | /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this | |
788 | * one only requires 16-bit alignment. */ | |
789 | struct ovs_16aligned_ip6_hdr { | |
790 | union { | |
791 | struct ovs_16aligned_ip6_hdrctl { | |
792 | ovs_16aligned_be32 ip6_un1_flow; | |
793 | ovs_be16 ip6_un1_plen; | |
794 | uint8_t ip6_un1_nxt; | |
795 | uint8_t ip6_un1_hlim; | |
796 | } ip6_un1; | |
797 | uint8_t ip6_un2_vfc; | |
798 | } ip6_ctlun; | |
799 | union ovs_16aligned_in6_addr ip6_src; | |
800 | union ovs_16aligned_in6_addr ip6_dst; | |
801 | }; | |
802 | ||
803 | /* Like struct in6_frag, but whereas that struct requires 32-bit alignment, | |
804 | * this one only requires 16-bit alignment. */ | |
805 | struct ovs_16aligned_ip6_frag { | |
806 | uint8_t ip6f_nxt; | |
807 | uint8_t ip6f_reserved; | |
808 | ovs_be16 ip6f_offlg; | |
809 | ovs_16aligned_be32 ip6f_ident; | |
810 | }; | |
811 | ||
5abf65d0 JG |
812 | #define ICMP6_HEADER_LEN 4 |
813 | struct icmp6_header { | |
814 | uint8_t icmp6_type; | |
815 | uint8_t icmp6_code; | |
816 | ovs_be16 icmp6_cksum; | |
5abf65d0 JG |
817 | }; |
818 | BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header)); | |
819 | ||
370e373b TLSC |
820 | uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *); |
821 | ||
e60e935b SRCSA |
822 | /* Neighbor Discovery option field. |
823 | * ND options are always a multiple of 8 bytes in size. */ | |
824 | #define ND_OPT_LEN 8 | |
825 | struct ovs_nd_opt { | |
826 | uint8_t nd_opt_type; /* Values defined in icmp6.h */ | |
827 | uint8_t nd_opt_len; /* in units of 8 octets (the size of this struct) */ | |
74ff3298 | 828 | struct eth_addr nd_opt_mac; /* Ethernet address in the case of SLL or TLL options */ |
e60e935b SRCSA |
829 | }; |
830 | BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt)); | |
831 | ||
832 | /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit | |
833 | * alignment, this one only requires 16-bit alignment. */ | |
834 | #define ND_MSG_LEN 24 | |
835 | struct ovs_nd_msg { | |
836 | struct icmp6_header icmph; | |
837 | ovs_16aligned_be32 rco_flags; | |
838 | union ovs_16aligned_in6_addr target; | |
839 | struct ovs_nd_opt options[0]; | |
840 | }; | |
841 | BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg)); | |
842 | ||
06994f87 TLSC |
843 | /* |
844 | * Use the same struct for MLD and MLD2, naming members as the defined fields in | |
845 | * in the corresponding version of the protocol, though they are reserved in the | |
846 | * other one. | |
847 | */ | |
848 | #define MLD_HEADER_LEN 8 | |
849 | struct mld_header { | |
850 | uint8_t type; | |
851 | uint8_t code; | |
852 | ovs_be16 csum; | |
853 | ovs_be16 mrd; | |
854 | ovs_be16 ngrp; | |
855 | }; | |
856 | BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header)); | |
857 | ||
858 | #define MLD2_RECORD_LEN 20 | |
859 | struct mld2_record { | |
860 | uint8_t type; | |
861 | uint8_t aux_len; | |
862 | ovs_be16 nsrcs; | |
863 | union ovs_16aligned_in6_addr maddr; | |
864 | }; | |
865 | BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record)); | |
866 | ||
867 | #define MLD_QUERY 130 | |
868 | #define MLD_REPORT 131 | |
869 | #define MLD_DONE 132 | |
870 | #define MLD2_REPORT 143 | |
871 | ||
fa8223b7 JP |
872 | /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */ |
873 | #define IPV6_LABEL_MASK 0x000fffff | |
874 | ||
3bffc610 BP |
875 | /* Example: |
876 | * | |
877 | * char *string = "1 ::1 2"; | |
878 | * char ipv6_s[IPV6_SCAN_LEN + 1]; | |
879 | * struct in6_addr ipv6; | |
880 | * | |
c2c28dfd | 881 | * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b) |
3bffc610 BP |
882 | * && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) { |
883 | * ... | |
884 | * } | |
885 | */ | |
886 | #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]" | |
887 | #define IPV6_SCAN_LEN 46 | |
888 | ||
d31f1109 JP |
889 | extern const struct in6_addr in6addr_exact; |
890 | #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \ | |
891 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } } | |
892 | ||
06994f87 TLSC |
893 | extern const struct in6_addr in6addr_all_hosts; |
894 | #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \ | |
895 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } } | |
896 | ||
d31f1109 JP |
897 | static inline bool ipv6_addr_equals(const struct in6_addr *a, |
898 | const struct in6_addr *b) | |
899 | { | |
900 | #ifdef IN6_ARE_ADDR_EQUAL | |
901 | return IN6_ARE_ADDR_EQUAL(a, b); | |
902 | #else | |
903 | return !memcmp(a, b, sizeof(*a)); | |
904 | #endif | |
905 | } | |
906 | ||
907 | static inline bool ipv6_mask_is_any(const struct in6_addr *mask) { | |
908 | return ipv6_addr_equals(mask, &in6addr_any); | |
909 | } | |
910 | ||
911 | static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) { | |
912 | return ipv6_addr_equals(mask, &in6addr_exact); | |
913 | } | |
914 | ||
06994f87 TLSC |
915 | static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) { |
916 | return ipv6_addr_equals(addr, &in6addr_all_hosts); | |
917 | } | |
918 | ||
8c46162b JB |
919 | static inline bool ipv6_addr_is_set(const struct in6_addr *addr) { |
920 | return !ipv6_addr_equals(addr, &in6addr_any); | |
921 | } | |
922 | ||
923 | static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) { | |
924 | return ip->s6_addr[0] == 0xff; | |
925 | } | |
926 | ||
12d0ee08 BP |
927 | static inline struct in6_addr |
928 | in6_addr_mapped_ipv4(ovs_be32 ip4) | |
929 | { | |
930 | struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } }; | |
931 | memcpy(&ip6.s6_addr[12], &ip4, 4); | |
932 | return ip6; | |
933 | } | |
934 | ||
754c1feb | 935 | static inline void |
12d0ee08 | 936 | in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4) |
754c1feb | 937 | { |
12d0ee08 | 938 | *ip6 = in6_addr_mapped_ipv4(ip4); |
754c1feb TLSC |
939 | } |
940 | ||
941 | static inline ovs_be32 | |
942 | in6_addr_get_mapped_ipv4(const struct in6_addr *addr) | |
943 | { | |
944 | union ovs_16aligned_in6_addr *taddr = (void *) addr; | |
945 | if (IN6_IS_ADDR_V4MAPPED(addr)) { | |
946 | return get_16aligned_be32(&taddr->be32[3]); | |
947 | } else { | |
948 | return INADDR_ANY; | |
949 | } | |
950 | } | |
951 | ||
c2b878e0 TLSC |
952 | static inline void |
953 | in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6) | |
954 | { | |
955 | union ovs_16aligned_in6_addr *taddr = (void *) addr; | |
956 | memset(taddr->be16, 0, sizeof(taddr->be16)); | |
957 | taddr->be16[0] = htons(0xff02); | |
958 | taddr->be16[5] = htons(0x1); | |
959 | taddr->be16[6] = htons(0xff00); | |
960 | memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3); | |
961 | } | |
962 | ||
685f4dfe NS |
963 | /* |
964 | * Generates ipv6 link local address from the given eth addr | |
965 | * with prefix 'fe80::/64' and stores it in 'lla' | |
966 | */ | |
967 | static inline void | |
968 | in6_generate_lla(struct eth_addr ea, struct in6_addr *lla) | |
969 | { | |
970 | union ovs_16aligned_in6_addr *taddr = (void *) lla; | |
971 | memset(taddr->be16, 0, sizeof(taddr->be16)); | |
972 | taddr->be16[0] = htons(0xfe80); | |
973 | taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]); | |
974 | taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff); | |
975 | taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]); | |
976 | taddr->be16[7] = ea.be16[2]; | |
977 | } | |
978 | ||
c2b878e0 TLSC |
979 | static inline void |
980 | ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6) | |
981 | { | |
982 | eth->ea[0] = 0x33; | |
983 | eth->ea[1] = 0x33; | |
984 | eth->ea[2] = ip6->s6_addr[12]; | |
985 | eth->ea[3] = ip6->s6_addr[13]; | |
986 | eth->ea[4] = ip6->s6_addr[14]; | |
987 | eth->ea[5] = ip6->s6_addr[15]; | |
988 | } | |
989 | ||
e8c16d83 SH |
990 | static inline bool dl_type_is_ip_any(ovs_be16 dl_type) |
991 | { | |
992 | return dl_type == htons(ETH_TYPE_IP) | |
993 | || dl_type == htons(ETH_TYPE_IPV6); | |
994 | } | |
995 | ||
a36de779 | 996 | /* Tunnel header */ |
e5a1caee | 997 | |
a36de779 PS |
998 | /* GRE protocol header */ |
999 | struct gre_base_hdr { | |
1000 | ovs_be16 flags; | |
1001 | ovs_be16 protocol; | |
1002 | }; | |
1003 | ||
1004 | #define GRE_CSUM 0x8000 | |
1005 | #define GRE_ROUTING 0x4000 | |
1006 | #define GRE_KEY 0x2000 | |
1007 | #define GRE_SEQ 0x1000 | |
1008 | #define GRE_STRICT 0x0800 | |
1009 | #define GRE_REC 0x0700 | |
1010 | #define GRE_FLAGS 0x00F8 | |
1011 | #define GRE_VERSION 0x0007 | |
1012 | ||
1013 | /* VXLAN protocol header */ | |
1014 | struct vxlanhdr { | |
1015 | ovs_16aligned_be32 vx_flags; | |
1016 | ovs_16aligned_be32 vx_vni; | |
1017 | }; | |
1018 | ||
1019 | #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ | |
1020 | ||
ac6d120f | 1021 | void ipv6_format_addr(const struct in6_addr *addr, struct ds *); |
9ac0aada JR |
1022 | void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *, |
1023 | bool bracket); | |
ac6d120f JP |
1024 | void ipv6_format_mapped(const struct in6_addr *addr, struct ds *); |
1025 | void ipv6_format_masked(const struct in6_addr *addr, | |
1026 | const struct in6_addr *mask, struct ds *); | |
bed610e8 | 1027 | const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr); |
d31f1109 JP |
1028 | struct in6_addr ipv6_addr_bitand(const struct in6_addr *src, |
1029 | const struct in6_addr *mask); | |
1030 | struct in6_addr ipv6_create_mask(int mask); | |
1031 | int ipv6_count_cidr_bits(const struct in6_addr *netmask); | |
1032 | bool ipv6_is_cidr(const struct in6_addr *netmask); | |
2b02db1b BP |
1033 | |
1034 | bool ipv6_parse(const char *s, struct in6_addr *ip); | |
fac5bd3c JP |
1035 | char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6, |
1036 | struct in6_addr *mask); | |
2b02db1b BP |
1037 | char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen) |
1038 | OVS_WARN_UNUSED_RESULT; | |
7dc88496 NS |
1039 | char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6, |
1040 | struct in6_addr *mask); | |
1041 | char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip, | |
1042 | unsigned int *plen) | |
1043 | OVS_WARN_UNUSED_RESULT; | |
d31f1109 | 1044 | |
74ff3298 JR |
1045 | void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst, |
1046 | const struct eth_addr eth_src, uint16_t eth_type, | |
5de1bb5c | 1047 | size_t size); |
74ff3298 JR |
1048 | void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst, |
1049 | const struct eth_addr eth_src, | |
5de1bb5c | 1050 | unsigned int oui, uint16_t snap_type, size_t size); |
cf62fa4c | 1051 | void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos, |
c97664b3 | 1052 | uint8_t ttl); |
31a9a584 | 1053 | void packet_set_ipv6(struct dp_packet *, const ovs_be32 src[4], |
bc7a5acd | 1054 | const ovs_be32 dst[4], uint8_t tc, |
038341d1 | 1055 | ovs_be32 fl, uint8_t hlmit); |
cf62fa4c PS |
1056 | void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst); |
1057 | void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst); | |
1058 | void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst); | |
b8786b18 | 1059 | void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code); |
cf62fa4c | 1060 | void packet_set_nd(struct dp_packet *, const ovs_be32 target[4], |
74ff3298 | 1061 | const struct eth_addr sll, const struct eth_addr tll); |
40f78b38 | 1062 | |
a66733a8 | 1063 | void packet_format_tcp_flags(struct ds *, uint16_t); |
61bf6666 | 1064 | const char *packet_tcp_flag_to_string(uint32_t flag); |
6335d074 | 1065 | void compose_arp__(struct dp_packet *); |
eb0b295e | 1066 | void compose_arp(struct dp_packet *, uint16_t arp_op, |
74ff3298 JR |
1067 | const struct eth_addr arp_sha, |
1068 | const struct eth_addr arp_tha, bool broadcast, | |
eb0b295e | 1069 | ovs_be32 arp_spa, ovs_be32 arp_tpa); |
c2b878e0 TLSC |
1070 | void compose_nd(struct dp_packet *, const struct eth_addr eth_src, |
1071 | struct in6_addr *, struct in6_addr *); | |
e75451fe ZKL |
1072 | void compose_na(struct dp_packet *, |
1073 | const struct eth_addr eth_src, const struct eth_addr eth_dst, | |
1074 | const ovs_be32 ipv6_src[4], const ovs_be32 ipv6_dst[4], | |
1075 | ovs_be32 rco_flags); | |
0292a0c9 | 1076 | uint32_t packet_csum_pseudoheader(const struct ip_header *); |
1bc3f0ed | 1077 | void IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6); |
12113c39 | 1078 | |
064af421 | 1079 | #endif /* packets.h */ |