/src/openvswitch/lib/packets.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
3 | | * |
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: |
7 | | * |
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. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | #include "packets.h" |
19 | | #include <sys/types.h> |
20 | | #include <netinet/in.h> |
21 | | #include <arpa/inet.h> |
22 | | #include <sys/socket.h> |
23 | | #include <netinet/ip6.h> |
24 | | #include <netinet/icmp6.h> |
25 | | #include <stdlib.h> |
26 | | #include <netdb.h> |
27 | | #include "byte-order.h" |
28 | | #include "csum.h" |
29 | | #include "crc32c.h" |
30 | | #include "flow.h" |
31 | | #include "openvswitch/hmap.h" |
32 | | #include "openvswitch/dynamic-string.h" |
33 | | #include "ovs-thread.h" |
34 | | #include "odp-util.h" |
35 | | #include "dp-packet.h" |
36 | | #include "unaligned.h" |
37 | | |
38 | | const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT; |
39 | | const struct in6_addr in6addr_all_hosts = IN6ADDR_ALL_HOSTS_INIT; |
40 | | const struct in6_addr in6addr_all_routers = IN6ADDR_ALL_ROUTERS_INIT; |
41 | | |
42 | | struct in6_addr |
43 | | flow_tnl_dst(const struct flow_tnl *tnl) |
44 | 0 | { |
45 | 0 | return tnl->ip_dst ? in6_addr_mapped_ipv4(tnl->ip_dst) : tnl->ipv6_dst; |
46 | 0 | } |
47 | | |
48 | | struct in6_addr |
49 | | flow_tnl_src(const struct flow_tnl *tnl) |
50 | 0 | { |
51 | 0 | return tnl->ip_src ? in6_addr_mapped_ipv4(tnl->ip_src) : tnl->ipv6_src; |
52 | 0 | } |
53 | | |
54 | | /* Returns true if 's' consists entirely of hex digits, false otherwise. */ |
55 | | static bool |
56 | | is_all_hex(const char *s) |
57 | 0 | { |
58 | 0 | return s[strspn(s, "0123456789abcdefABCDEF")] == '\0'; |
59 | 0 | } |
60 | | |
61 | | /* Parses 's' as a 16-digit hexadecimal number representing a datapath ID. On |
62 | | * success stores the dpid into '*dpidp' and returns true, on failure stores 0 |
63 | | * into '*dpidp' and returns false. |
64 | | * |
65 | | * Rejects an all-zeros dpid as invalid. */ |
66 | | bool |
67 | | dpid_from_string(const char *s, uint64_t *dpidp) |
68 | 0 | { |
69 | 0 | size_t len = strlen(s); |
70 | 0 | *dpidp = ((len == 16 && is_all_hex(s)) |
71 | 0 | || (len <= 18 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') |
72 | 0 | && is_all_hex(s + 2)) |
73 | 0 | ? strtoull(s, NULL, 16) |
74 | 0 | : 0); |
75 | 0 | return *dpidp != 0; |
76 | 0 | } |
77 | | |
78 | | uint64_t |
79 | | eth_addr_to_uint64(const struct eth_addr ea) |
80 | 0 | { |
81 | 0 | return (((uint64_t) ntohs(ea.be16[0]) << 32) |
82 | 0 | | ((uint64_t) ntohs(ea.be16[1]) << 16) |
83 | 0 | | ntohs(ea.be16[2])); |
84 | 0 | } |
85 | | |
86 | | void |
87 | | eth_addr_from_uint64(uint64_t x, struct eth_addr *ea) |
88 | 0 | { |
89 | 0 | ea->be16[0] = htons(x >> 32); |
90 | 0 | ea->be16[1] = htons((x & 0xFFFF0000) >> 16); |
91 | 0 | ea->be16[2] = htons(x & 0xFFFF); |
92 | 0 | } |
93 | | |
94 | | void |
95 | | eth_addr_mark_random(struct eth_addr *ea) |
96 | 0 | { |
97 | 0 | ea->ea[0] &= ~1; /* Unicast. */ |
98 | 0 | ea->ea[0] |= 2; /* Private. */ |
99 | 0 | } |
100 | | |
101 | | /* Returns true if 'ea' is a reserved address, that a bridge must never |
102 | | * forward, false otherwise. |
103 | | * |
104 | | * If you change this function's behavior, please update corresponding |
105 | | * documentation in vswitch.xml at the same time. */ |
106 | | bool |
107 | | eth_addr_is_reserved(const struct eth_addr ea) |
108 | 0 | { |
109 | 0 | struct eth_addr_node { |
110 | 0 | struct hmap_node hmap_node; |
111 | 0 | const uint64_t ea64; |
112 | 0 | }; |
113 | |
|
114 | 0 | static struct eth_addr_node nodes[] = { |
115 | | /* STP, IEEE pause frames, and other reserved protocols. */ |
116 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000000ULL }, |
117 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000001ULL }, |
118 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000002ULL }, |
119 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000003ULL }, |
120 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000004ULL }, |
121 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000005ULL }, |
122 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000006ULL }, |
123 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000007ULL }, |
124 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000008ULL }, |
125 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000009ULL }, |
126 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000aULL }, |
127 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000bULL }, |
128 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000cULL }, |
129 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000dULL }, |
130 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000eULL }, |
131 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000fULL }, |
132 | | |
133 | | /* Extreme protocols. */ |
134 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000000ULL }, /* EDP. */ |
135 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000004ULL }, /* EAPS. */ |
136 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000006ULL }, /* EAPS. */ |
137 | | |
138 | | /* Cisco protocols. */ |
139 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000c000000ULL }, /* ISL. */ |
140 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccccULL }, /* PAgP, UDLD, CDP, |
141 | | * DTP, VTP. */ |
142 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000ccccccdULL }, /* PVST+. */ |
143 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000ccdcdcdULL }, /* STP Uplink Fast, |
144 | | * FlexLink. */ |
145 | | |
146 | | /* Cisco CFM. */ |
147 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc0ULL }, |
148 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc1ULL }, |
149 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc2ULL }, |
150 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc3ULL }, |
151 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc4ULL }, |
152 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc5ULL }, |
153 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc6ULL }, |
154 | 0 | { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc7ULL }, |
155 | 0 | }; |
156 | |
|
157 | 0 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
158 | 0 | struct eth_addr_node *node; |
159 | 0 | static struct hmap addrs; |
160 | 0 | uint64_t ea64; |
161 | |
|
162 | 0 | if (ovsthread_once_start(&once)) { |
163 | 0 | hmap_init(&addrs); |
164 | 0 | for (node = nodes; node < &nodes[ARRAY_SIZE(nodes)]; node++) { |
165 | 0 | hmap_insert(&addrs, &node->hmap_node, hash_uint64(node->ea64)); |
166 | 0 | } |
167 | 0 | ovsthread_once_done(&once); |
168 | 0 | } |
169 | |
|
170 | 0 | ea64 = eth_addr_to_uint64(ea); |
171 | 0 | HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_uint64(ea64), &addrs) { |
172 | 0 | if (node->ea64 == ea64) { |
173 | 0 | return true; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | return false; |
177 | 0 | } |
178 | | |
179 | | /* Attempts to parse 's' as an Ethernet address. If successful, stores the |
180 | | * address in 'ea' and returns true, otherwise zeros 'ea' and returns |
181 | | * false. This function checks trailing characters. */ |
182 | | bool |
183 | | eth_addr_from_string(const char *s, struct eth_addr *ea) |
184 | 0 | { |
185 | 0 | int n = 0; |
186 | 0 | if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*ea), &n) |
187 | 0 | && !s[n]) { |
188 | 0 | return true; |
189 | 0 | } else { |
190 | 0 | *ea = eth_addr_zero; |
191 | 0 | return false; |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | | /* Fills 'b' with a Reverse ARP packet with Ethernet source address 'eth_src'. |
196 | | * This function is used by Open vSwitch to compose packets in cases where |
197 | | * context is important but content doesn't (or shouldn't) matter. |
198 | | * |
199 | | * The returned packet has enough headroom to insert an 802.1Q VLAN header if |
200 | | * desired. */ |
201 | | void |
202 | | compose_rarp(struct dp_packet *b, const struct eth_addr eth_src) |
203 | 0 | { |
204 | 0 | struct eth_header *eth; |
205 | 0 | struct arp_eth_header *arp; |
206 | |
|
207 | 0 | dp_packet_clear(b); |
208 | 0 | dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN |
209 | 0 | + ARP_ETH_HEADER_LEN); |
210 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
211 | 0 | eth = dp_packet_put_uninit(b, sizeof *eth); |
212 | 0 | eth->eth_dst = eth_addr_broadcast; |
213 | 0 | eth->eth_src = eth_src; |
214 | 0 | eth->eth_type = htons(ETH_TYPE_RARP); |
215 | |
|
216 | 0 | arp = dp_packet_put_uninit(b, sizeof *arp); |
217 | 0 | arp->ar_hrd = htons(ARP_HRD_ETHERNET); |
218 | 0 | arp->ar_pro = htons(ARP_PRO_IP); |
219 | 0 | arp->ar_hln = sizeof arp->ar_sha; |
220 | 0 | arp->ar_pln = sizeof arp->ar_spa; |
221 | 0 | arp->ar_op = htons(ARP_OP_RARP); |
222 | 0 | arp->ar_sha = eth_src; |
223 | 0 | put_16aligned_be32(&arp->ar_spa, htonl(0)); |
224 | 0 | arp->ar_tha = eth_src; |
225 | 0 | put_16aligned_be32(&arp->ar_tpa, htonl(0)); |
226 | |
|
227 | 0 | dp_packet_set_l3(b, arp); |
228 | 0 | b->packet_type = htonl(PT_ETH); |
229 | 0 | } |
230 | | |
231 | | /* Insert VLAN header according to given TCI. Packet passed must be Ethernet |
232 | | * packet. Ignores the CFI bit of 'tci' using 0 instead. |
233 | | * |
234 | | * Also adjusts the layer offsets accordingly. */ |
235 | | void |
236 | | eth_push_vlan(struct dp_packet *packet, ovs_be16 tpid, ovs_be16 tci) |
237 | 0 | { |
238 | 0 | struct vlan_eth_header *veh; |
239 | | |
240 | | /* Insert new 802.1Q header. */ |
241 | 0 | veh = dp_packet_resize_l2(packet, VLAN_HEADER_LEN); |
242 | 0 | memmove(veh, (char *)veh + VLAN_HEADER_LEN, 2 * ETH_ADDR_LEN); |
243 | 0 | veh->veth_type = tpid; |
244 | 0 | veh->veth_tci = tci & htons(~VLAN_CFI); |
245 | 0 | } |
246 | | |
247 | | /* Removes outermost VLAN header (if any is present) from 'packet'. |
248 | | * |
249 | | * 'packet->l2_5' should initially point to 'packet''s outer-most VLAN header |
250 | | * or may be NULL if there are no VLAN headers. */ |
251 | | void |
252 | | eth_pop_vlan(struct dp_packet *packet) |
253 | 0 | { |
254 | 0 | struct vlan_eth_header *veh = dp_packet_eth(packet); |
255 | |
|
256 | 0 | if (veh && dp_packet_size(packet) >= sizeof *veh |
257 | 0 | && eth_type_vlan(veh->veth_type)) { |
258 | |
|
259 | 0 | memmove((char *)veh + VLAN_HEADER_LEN, veh, 2 * ETH_ADDR_LEN); |
260 | 0 | dp_packet_resize_l2(packet, -VLAN_HEADER_LEN); |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | | /* Push Ethernet header onto 'packet' assuming it is layer 3 */ |
265 | | void |
266 | | push_eth(struct dp_packet *packet, const struct eth_addr *dst, |
267 | | const struct eth_addr *src) |
268 | 0 | { |
269 | 0 | struct eth_header *eh; |
270 | |
|
271 | 0 | ovs_assert(!dp_packet_is_eth(packet)); |
272 | 0 | eh = dp_packet_resize_l2(packet, ETH_HEADER_LEN); |
273 | 0 | eh->eth_dst = *dst; |
274 | 0 | eh->eth_src = *src; |
275 | 0 | eh->eth_type = pt_ns_type_be(packet->packet_type); |
276 | 0 | packet->packet_type = htonl(PT_ETH); |
277 | 0 | } |
278 | | |
279 | | /* Removes Ethernet header, including VLAN header, from 'packet'. |
280 | | * |
281 | | * Previous to calling this function, 'ofpbuf_l3(packet)' must not be NULL */ |
282 | | void |
283 | | pop_eth(struct dp_packet *packet) |
284 | 0 | { |
285 | 0 | char *l2_5 = dp_packet_l2_5(packet); |
286 | 0 | char *l3 = dp_packet_l3(packet); |
287 | 0 | ovs_be16 ethertype; |
288 | 0 | int increment; |
289 | |
|
290 | 0 | ovs_assert(dp_packet_is_eth(packet)); |
291 | 0 | ovs_assert(l3 != NULL); |
292 | |
|
293 | 0 | if (l2_5) { |
294 | 0 | increment = packet->l2_5_ofs; |
295 | 0 | ethertype = *(ALIGNED_CAST(ovs_be16 *, (l2_5 - 2))); |
296 | 0 | } else { |
297 | 0 | increment = packet->l3_ofs; |
298 | 0 | ethertype = *(ALIGNED_CAST(ovs_be16 *, (l3 - 2))); |
299 | 0 | } |
300 | |
|
301 | 0 | dp_packet_resize_l2(packet, -increment); |
302 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, ntohs(ethertype)); |
303 | 0 | } |
304 | | |
305 | | /* Set ethertype of the packet. */ |
306 | | static void |
307 | | set_ethertype(struct dp_packet *packet, ovs_be16 eth_type) |
308 | 0 | { |
309 | 0 | struct eth_header *eh = dp_packet_eth(packet); |
310 | |
|
311 | 0 | if (!eh) { |
312 | 0 | return; |
313 | 0 | } |
314 | | |
315 | 0 | if (eth_type_vlan(eh->eth_type)) { |
316 | 0 | ovs_be16 *p; |
317 | 0 | char *l2_5 = dp_packet_l2_5(packet); |
318 | |
|
319 | 0 | p = ALIGNED_CAST(ovs_be16 *, |
320 | 0 | (l2_5 ? l2_5 : (char *)dp_packet_l3(packet)) - 2); |
321 | 0 | *p = eth_type; |
322 | 0 | } else { |
323 | 0 | eh->eth_type = eth_type; |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | static bool is_mpls(struct dp_packet *packet) |
328 | 0 | { |
329 | 0 | return packet->l2_5_ofs != UINT16_MAX; |
330 | 0 | } |
331 | | |
332 | | /* Set time to live (TTL) of an MPLS label stack entry (LSE). */ |
333 | | void |
334 | | set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl) |
335 | 0 | { |
336 | 0 | *lse &= ~htonl(MPLS_TTL_MASK); |
337 | 0 | *lse |= htonl((ttl << MPLS_TTL_SHIFT) & MPLS_TTL_MASK); |
338 | 0 | } |
339 | | |
340 | | /* Set traffic class (TC) of an MPLS label stack entry (LSE). */ |
341 | | void |
342 | | set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc) |
343 | 0 | { |
344 | 0 | *lse &= ~htonl(MPLS_TC_MASK); |
345 | 0 | *lse |= htonl((tc << MPLS_TC_SHIFT) & MPLS_TC_MASK); |
346 | 0 | } |
347 | | |
348 | | /* Set label of an MPLS label stack entry (LSE). */ |
349 | | void |
350 | | set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label) |
351 | 0 | { |
352 | 0 | *lse &= ~htonl(MPLS_LABEL_MASK); |
353 | 0 | *lse |= htonl((ntohl(label) << MPLS_LABEL_SHIFT) & MPLS_LABEL_MASK); |
354 | 0 | } |
355 | | |
356 | | /* Set bottom of stack (BoS) bit of an MPLS label stack entry (LSE). */ |
357 | | void |
358 | | set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos) |
359 | 0 | { |
360 | 0 | *lse &= ~htonl(MPLS_BOS_MASK); |
361 | 0 | *lse |= htonl((bos << MPLS_BOS_SHIFT) & MPLS_BOS_MASK); |
362 | 0 | } |
363 | | |
364 | | /* Compose an MPLS label stack entry (LSE) from its components: |
365 | | * label, traffic class (TC), time to live (TTL) and |
366 | | * bottom of stack (BoS) bit. */ |
367 | | ovs_be32 |
368 | | set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos, ovs_be32 label) |
369 | 0 | { |
370 | 0 | ovs_be32 lse = htonl(0); |
371 | 0 | set_mpls_lse_ttl(&lse, ttl); |
372 | 0 | set_mpls_lse_tc(&lse, tc); |
373 | 0 | set_mpls_lse_bos(&lse, bos); |
374 | 0 | set_mpls_lse_label(&lse, label); |
375 | 0 | return lse; |
376 | 0 | } |
377 | | |
378 | | /* Set MPLS label stack entry to outermost MPLS header.*/ |
379 | | void |
380 | | set_mpls_lse(struct dp_packet *packet, ovs_be32 mpls_lse) |
381 | 0 | { |
382 | | /* Packet type should be MPLS to set label stack entry. */ |
383 | 0 | if (is_mpls(packet)) { |
384 | 0 | struct mpls_hdr *mh = dp_packet_l2_5(packet); |
385 | | |
386 | | /* Update mpls label stack entry. */ |
387 | 0 | put_16aligned_be32(&mh->mpls_lse, mpls_lse); |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | /* Push MPLS label stack entry 'lse' onto 'packet' as the outermost MPLS |
392 | | * header. If 'packet' does not already have any MPLS labels, then its |
393 | | * Ethertype is changed to 'ethtype' (which must be an MPLS Ethertype). */ |
394 | | void |
395 | | push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse) |
396 | 0 | { |
397 | 0 | char * header; |
398 | 0 | size_t len; |
399 | |
|
400 | 0 | if (!eth_type_mpls(ethtype)) { |
401 | 0 | return; |
402 | 0 | } |
403 | | |
404 | 0 | if (!is_mpls(packet)) { |
405 | | /* Set MPLS label stack offset. */ |
406 | 0 | packet->l2_5_ofs = packet->l3_ofs; |
407 | 0 | } |
408 | |
|
409 | 0 | set_ethertype(packet, ethtype); |
410 | | |
411 | | /* Push new MPLS shim header onto packet. */ |
412 | 0 | len = packet->l2_5_ofs; |
413 | 0 | header = dp_packet_resize_l2_5(packet, MPLS_HLEN); |
414 | 0 | memmove(header, header + MPLS_HLEN, len); |
415 | 0 | memcpy(header + len, &lse, sizeof lse); |
416 | |
|
417 | 0 | pkt_metadata_init_conn(&packet->md); |
418 | 0 | } |
419 | | |
420 | | void |
421 | | add_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse, |
422 | | bool l3_encap) |
423 | 0 | { |
424 | 0 | if (!eth_type_mpls(ethtype)) { |
425 | 0 | return; |
426 | 0 | } |
427 | | |
428 | 0 | if (!l3_encap) { |
429 | 0 | struct mpls_hdr *header = dp_packet_resize_l2(packet, MPLS_HLEN); |
430 | |
|
431 | 0 | put_16aligned_be32(&header->mpls_lse, lse); |
432 | 0 | packet->l2_5_ofs = 0; |
433 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, |
434 | 0 | ntohs(ethtype)); |
435 | 0 | } else { |
436 | 0 | size_t len; |
437 | 0 | char *header; |
438 | |
|
439 | 0 | if (!is_mpls(packet)) { |
440 | | /* Set MPLS label stack offset. */ |
441 | 0 | packet->l2_5_ofs = packet->l3_ofs; |
442 | 0 | } |
443 | 0 | set_ethertype(packet, ethtype); |
444 | | |
445 | | /* Push new MPLS shim header onto packet. */ |
446 | 0 | len = packet->l2_5_ofs; |
447 | 0 | header = dp_packet_resize_l2_5(packet, MPLS_HLEN); |
448 | 0 | memmove(header, header + MPLS_HLEN, len); |
449 | 0 | memcpy(header + len, &lse, sizeof lse); |
450 | 0 | } |
451 | 0 | pkt_metadata_init_conn(&packet->md); |
452 | 0 | } |
453 | | |
454 | | /* If 'packet' is an MPLS packet, removes its outermost MPLS label stack entry. |
455 | | * If the label that was removed was the only MPLS label, changes 'packet''s |
456 | | * Ethertype to 'ethtype' (which ordinarily should not be an MPLS |
457 | | * Ethertype). */ |
458 | | void |
459 | | pop_mpls(struct dp_packet *packet, ovs_be16 ethtype) |
460 | 0 | { |
461 | 0 | if (is_mpls(packet)) { |
462 | 0 | struct mpls_hdr *mh = dp_packet_l2_5(packet); |
463 | 0 | size_t len = packet->l2_5_ofs; |
464 | |
|
465 | 0 | set_ethertype(packet, ethtype); |
466 | 0 | if (get_16aligned_be32(&mh->mpls_lse) & htonl(MPLS_BOS_MASK)) { |
467 | 0 | dp_packet_set_l2_5(packet, NULL); |
468 | 0 | } |
469 | | /* Shift the l2 header forward. */ |
470 | 0 | memmove((char*)dp_packet_data(packet) + MPLS_HLEN, dp_packet_data(packet), len); |
471 | 0 | dp_packet_resize_l2_5(packet, -MPLS_HLEN); |
472 | | |
473 | | /* Invalidate offload flags as they are not valid after |
474 | | * decapsulation of MPLS header. */ |
475 | 0 | dp_packet_reset_offload(packet); |
476 | | |
477 | | /* packet_type must be reset for the MPLS packets with no l2 header */ |
478 | 0 | if (!len) { |
479 | 0 | if (ethtype == htons(ETH_TYPE_TEB)) { |
480 | | /* The inner packet must be classified as ethernet if the |
481 | | * ethtype is ETH_TYPE_TEB. */ |
482 | 0 | packet->packet_type = htonl(PT_ETH); |
483 | 0 | } else { |
484 | 0 | packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, |
485 | 0 | ntohs(ethtype)); |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | | void |
492 | | push_nsh(struct dp_packet *packet, const struct nsh_hdr *nsh_hdr_src) |
493 | 0 | { |
494 | 0 | struct nsh_hdr *nsh; |
495 | 0 | size_t length = nsh_hdr_len(nsh_hdr_src); |
496 | 0 | uint8_t next_proto; |
497 | |
|
498 | 0 | switch (ntohl(packet->packet_type)) { |
499 | 0 | case PT_ETH: |
500 | 0 | next_proto = NSH_P_ETHERNET; |
501 | 0 | break; |
502 | 0 | case PT_IPV4: |
503 | 0 | next_proto = NSH_P_IPV4; |
504 | 0 | break; |
505 | 0 | case PT_IPV6: |
506 | 0 | next_proto = NSH_P_IPV6; |
507 | 0 | break; |
508 | 0 | case PT_NSH: |
509 | 0 | next_proto = NSH_P_NSH; |
510 | 0 | break; |
511 | 0 | default: |
512 | 0 | OVS_NOT_REACHED(); |
513 | 0 | } |
514 | | |
515 | 0 | nsh = (struct nsh_hdr *) dp_packet_resize_l2(packet, length); |
516 | 0 | memcpy(nsh, nsh_hdr_src, length); |
517 | 0 | nsh->next_proto = next_proto; |
518 | 0 | packet->packet_type = htonl(PT_NSH); |
519 | 0 | dp_packet_reset_offsets(packet); |
520 | 0 | packet->l3_ofs = 0; |
521 | 0 | } |
522 | | |
523 | | bool |
524 | | pop_nsh(struct dp_packet *packet) |
525 | 0 | { |
526 | 0 | struct nsh_hdr *nsh = (struct nsh_hdr *) dp_packet_l3(packet); |
527 | 0 | size_t length; |
528 | 0 | uint32_t next_pt; |
529 | |
|
530 | 0 | if (packet->packet_type == htonl(PT_NSH) && nsh) { |
531 | 0 | switch (nsh->next_proto) { |
532 | 0 | case NSH_P_ETHERNET: |
533 | 0 | next_pt = PT_ETH; |
534 | 0 | break; |
535 | 0 | case NSH_P_IPV4: |
536 | 0 | next_pt = PT_IPV4; |
537 | 0 | break; |
538 | 0 | case NSH_P_IPV6: |
539 | 0 | next_pt = PT_IPV6; |
540 | 0 | break; |
541 | 0 | case NSH_P_NSH: |
542 | 0 | next_pt = PT_NSH; |
543 | 0 | break; |
544 | 0 | default: |
545 | | /* Unknown inner packet type. Drop packet. */ |
546 | 0 | return false; |
547 | 0 | } |
548 | | |
549 | 0 | length = nsh_hdr_len(nsh); |
550 | 0 | dp_packet_reset_packet(packet, length); |
551 | 0 | packet->packet_type = htonl(next_pt); |
552 | | /* Packet must be recirculated for further processing. */ |
553 | 0 | } |
554 | 0 | return true; |
555 | 0 | } |
556 | | |
557 | | /* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'. The |
558 | | * caller must free '*packetp'. On success, returns NULL. On failure, returns |
559 | | * an error message and stores NULL in '*packetp'. |
560 | | * |
561 | | * Aligns the L3 header of '*packetp' on a 32-bit boundary. */ |
562 | | const char * |
563 | | eth_from_hex(const char *hex, struct dp_packet **packetp) |
564 | 0 | { |
565 | 0 | struct dp_packet *packet; |
566 | | |
567 | | /* Use 2 bytes of headroom to 32-bit align the L3 header. */ |
568 | 0 | packet = *packetp = dp_packet_new_with_headroom(strlen(hex) / 2, 2); |
569 | |
|
570 | 0 | if (dp_packet_put_hex(packet, hex, NULL)[0] != '\0') { |
571 | 0 | dp_packet_delete(packet); |
572 | 0 | *packetp = NULL; |
573 | 0 | return "Trailing garbage in packet data"; |
574 | 0 | } |
575 | | |
576 | 0 | if (dp_packet_size(packet) < ETH_HEADER_LEN) { |
577 | 0 | dp_packet_delete(packet); |
578 | 0 | *packetp = NULL; |
579 | 0 | return "Packet data too short for Ethernet"; |
580 | 0 | } |
581 | | |
582 | 0 | return NULL; |
583 | 0 | } |
584 | | |
585 | | void |
586 | | eth_format_masked(const struct eth_addr eth, |
587 | | const struct eth_addr *mask, struct ds *s) |
588 | 8.14k | { |
589 | 8.14k | ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth)); |
590 | 8.14k | if (mask && !eth_mask_is_exact(*mask)) { |
591 | 0 | ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(*mask)); |
592 | 0 | } |
593 | 8.14k | } |
594 | | |
595 | | void |
596 | | in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6) |
597 | 0 | { |
598 | 0 | union ovs_16aligned_in6_addr *taddr = |
599 | 0 | (union ovs_16aligned_in6_addr *) addr; |
600 | 0 | memset(taddr->be16, 0, sizeof(taddr->be16)); |
601 | 0 | taddr->be16[0] = htons(0xff02); |
602 | 0 | taddr->be16[5] = htons(0x1); |
603 | 0 | taddr->be16[6] = htons(0xff00); |
604 | 0 | memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3); |
605 | 0 | } |
606 | | |
607 | | /* |
608 | | * Generates ipv6 EUI64 address from the given eth addr |
609 | | * and prefix and stores it in 'lla' |
610 | | */ |
611 | | void |
612 | | in6_generate_eui64(struct eth_addr ea, const struct in6_addr *prefix, |
613 | | struct in6_addr *lla) |
614 | 0 | { |
615 | 0 | union ovs_16aligned_in6_addr *taddr = |
616 | 0 | (union ovs_16aligned_in6_addr *) lla; |
617 | 0 | union ovs_16aligned_in6_addr *prefix_taddr = |
618 | 0 | (union ovs_16aligned_in6_addr *) prefix; |
619 | 0 | taddr->be16[0] = prefix_taddr->be16[0]; |
620 | 0 | taddr->be16[1] = prefix_taddr->be16[1]; |
621 | 0 | taddr->be16[2] = prefix_taddr->be16[2]; |
622 | 0 | taddr->be16[3] = prefix_taddr->be16[3]; |
623 | 0 | taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]); |
624 | 0 | taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff); |
625 | 0 | taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]); |
626 | 0 | taddr->be16[7] = ea.be16[2]; |
627 | 0 | } |
628 | | |
629 | | /* Generates ipv6 link local address from the given eth addr |
630 | | * with prefix 'fe80::/64' and stores it in 'lla'. */ |
631 | | void |
632 | | in6_generate_lla(struct eth_addr ea, struct in6_addr *lla) |
633 | 0 | { |
634 | 0 | union ovs_16aligned_in6_addr *taddr = |
635 | 0 | (union ovs_16aligned_in6_addr *) lla; |
636 | 0 | memset(taddr->be16, 0, sizeof(taddr->be16)); |
637 | 0 | taddr->be16[0] = htons(0xfe80); |
638 | 0 | taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]); |
639 | 0 | taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff); |
640 | 0 | taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]); |
641 | 0 | taddr->be16[7] = ea.be16[2]; |
642 | 0 | } |
643 | | |
644 | | /* Returns true if 'addr' is a link local address. Otherwise, false. */ |
645 | | bool |
646 | | in6_is_lla(struct in6_addr *addr) |
647 | 0 | { |
648 | 0 | #ifdef s6_addr32 |
649 | 0 | return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]); |
650 | | #else |
651 | | return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 && |
652 | | !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] | |
653 | | addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]); |
654 | | #endif |
655 | 0 | } |
656 | | |
657 | | void |
658 | | ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6) |
659 | 0 | { |
660 | 0 | eth->ea[0] = 0x33; |
661 | 0 | eth->ea[1] = 0x33; |
662 | 0 | eth->ea[2] = ip6->s6_addr[12]; |
663 | 0 | eth->ea[3] = ip6->s6_addr[13]; |
664 | 0 | eth->ea[4] = ip6->s6_addr[14]; |
665 | 0 | eth->ea[5] = ip6->s6_addr[15]; |
666 | 0 | } |
667 | | |
668 | | /* Given the IP netmask 'netmask', returns the number of bits of the IP address |
669 | | * that it specifies, that is, the number of 1-bits in 'netmask'. |
670 | | * |
671 | | * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will |
672 | | * still be in the valid range but isn't otherwise meaningful. */ |
673 | | int |
674 | | ip_count_cidr_bits(ovs_be32 netmask) |
675 | 3.89k | { |
676 | 3.89k | return 32 - ctz32(ntohl(netmask)); |
677 | 3.89k | } |
678 | | |
679 | | void |
680 | | ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s) |
681 | 4.62k | { |
682 | 4.62k | ds_put_format(s, IP_FMT, IP_ARGS(ip)); |
683 | 4.62k | if (mask != OVS_BE32_MAX) { |
684 | 0 | if (ip_is_cidr(mask)) { |
685 | 0 | ds_put_format(s, "/%d", ip_count_cidr_bits(mask)); |
686 | 0 | } else { |
687 | 0 | ds_put_format(s, "/"IP_FMT, IP_ARGS(mask)); |
688 | 0 | } |
689 | 0 | } |
690 | 4.62k | } |
691 | | |
692 | | /* Parses string 's', which must be an IP address. Stores the IP address into |
693 | | * '*ip'. Returns true if successful, otherwise false. */ |
694 | | bool |
695 | | ip_parse(const char *s, ovs_be32 *ip) |
696 | 0 | { |
697 | 0 | return inet_pton(AF_INET, s, ip) == 1; |
698 | 0 | } |
699 | | |
700 | | /* Parses string 's', which must be an IP address with a port number |
701 | | * with ":" as a separator (e.g.: 192.168.1.2:80). |
702 | | * Stores the IP address into '*ip' and port number to '*port'. |
703 | | * |
704 | | * Returns NULL if successful, otherwise an error message that the caller must |
705 | | * free(). */ |
706 | | char * OVS_WARN_UNUSED_RESULT |
707 | | ip_parse_port(const char *s, ovs_be32 *ip, ovs_be16 *port) |
708 | 0 | { |
709 | 0 | int n = 0; |
710 | 0 | if (ovs_scan(s, IP_PORT_SCAN_FMT"%n", IP_PORT_SCAN_ARGS(ip, port), &n) |
711 | 0 | && !s[n]) { |
712 | 0 | return NULL; |
713 | 0 | } |
714 | | |
715 | 0 | return xasprintf("%s: invalid IP address or port number", s); |
716 | 0 | } |
717 | | |
718 | | /* Parses string 's', which must be an IP address with an optional netmask or |
719 | | * CIDR prefix length. Stores the IP address into '*ip', netmask into '*mask', |
720 | | * (255.255.255.255, if 's' lacks a netmask), and number of scanned characters |
721 | | * into '*n'. |
722 | | * |
723 | | * Returns NULL if successful, otherwise an error message that the caller must |
724 | | * free(). */ |
725 | | char * OVS_WARN_UNUSED_RESULT |
726 | | ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, |
727 | | ovs_be32 *mask) |
728 | 0 | { |
729 | 0 | int prefix; |
730 | |
|
731 | 0 | if (ovs_scan_len(s, n, IP_SCAN_FMT"/"IP_SCAN_FMT, |
732 | 0 | IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) { |
733 | | /* OK. */ |
734 | 0 | } else if (ovs_scan_len(s, n, IP_SCAN_FMT"/%d", |
735 | 0 | IP_SCAN_ARGS(ip), &prefix)) { |
736 | 0 | if (prefix < 0 || prefix > 32) { |
737 | 0 | return xasprintf("%s: IPv4 network prefix bits not between 0 and " |
738 | 0 | "32, inclusive", s); |
739 | 0 | } |
740 | 0 | *mask = be32_prefix_mask(prefix); |
741 | 0 | } else if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) { |
742 | 0 | *mask = OVS_BE32_MAX; |
743 | 0 | } else { |
744 | 0 | return xasprintf("%s: invalid IP address", s); |
745 | 0 | } |
746 | 0 | return NULL; |
747 | 0 | } |
748 | | |
749 | | /* This function is similar to ip_parse_masked_len(), but doesn't return the |
750 | | * number of scanned characters and expects 's' to end after the ip/(optional) |
751 | | * mask. |
752 | | * |
753 | | * Returns NULL if successful, otherwise an error message that the caller must |
754 | | * free(). */ |
755 | | char * OVS_WARN_UNUSED_RESULT |
756 | | ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask) |
757 | 0 | { |
758 | 0 | int n = 0; |
759 | |
|
760 | 0 | char *error = ip_parse_masked_len(s, &n, ip, mask); |
761 | 0 | if (!error && s[n]) { |
762 | 0 | return xasprintf("%s: invalid IP address", s); |
763 | 0 | } |
764 | 0 | return error; |
765 | 0 | } |
766 | | |
767 | | /* Similar to ip_parse_masked_len(), but the mask, if present, must be a CIDR |
768 | | * mask and is returned as a prefix len in '*plen'. */ |
769 | | char * OVS_WARN_UNUSED_RESULT |
770 | | ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip, unsigned int *plen) |
771 | 0 | { |
772 | 0 | ovs_be32 mask; |
773 | 0 | char *error; |
774 | |
|
775 | 0 | error = ip_parse_masked_len(s, n, ip, &mask); |
776 | 0 | if (error) { |
777 | 0 | return error; |
778 | 0 | } |
779 | | |
780 | 0 | if (!ip_is_cidr(mask)) { |
781 | 0 | return xasprintf("%s: CIDR network required", s); |
782 | 0 | } |
783 | 0 | *plen = ip_count_cidr_bits(mask); |
784 | 0 | return NULL; |
785 | 0 | } |
786 | | |
787 | | /* Similar to ip_parse_cidr_len(), but doesn't return the number of scanned |
788 | | * characters and expects 's' to be NULL terminated at the end of the |
789 | | * ip/(optional) cidr. */ |
790 | | char * OVS_WARN_UNUSED_RESULT |
791 | | ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen) |
792 | 0 | { |
793 | 0 | int n = 0; |
794 | |
|
795 | 0 | char *error = ip_parse_cidr_len(s, &n, ip, plen); |
796 | 0 | if (!error && s[n]) { |
797 | 0 | return xasprintf("%s: invalid IP address", s); |
798 | 0 | } |
799 | 0 | return error; |
800 | 0 | } |
801 | | |
802 | | /* Parses string 's', which must be an IPv6 address. Stores the IPv6 address |
803 | | * into '*ip'. Returns true if successful, otherwise false. */ |
804 | | bool |
805 | | ipv6_parse(const char *s, struct in6_addr *ip) |
806 | 0 | { |
807 | 0 | return inet_pton(AF_INET6, s, ip) == 1; |
808 | 0 | } |
809 | | |
810 | | /* Parses string 's', which must be an IPv6 address with an optional netmask or |
811 | | * CIDR prefix length. Stores the IPv6 address into '*ip' and the netmask into |
812 | | * '*mask' (if 's' does not contain a netmask, all-one-bits is assumed), and |
813 | | * number of scanned characters into '*n'. |
814 | | * |
815 | | * Returns NULL if successful, otherwise an error message that the caller must |
816 | | * free(). */ |
817 | | char * OVS_WARN_UNUSED_RESULT |
818 | | ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ip, |
819 | | struct in6_addr *mask) |
820 | 0 | { |
821 | 0 | char ipv6_s[IPV6_SCAN_LEN + 1]; |
822 | 0 | int prefix; |
823 | |
|
824 | 0 | if (ovs_scan_len(s, n, " "IPV6_SCAN_FMT, ipv6_s) |
825 | 0 | && ipv6_parse(ipv6_s, ip)) { |
826 | 0 | if (ovs_scan_len(s, n, "/%d", &prefix)) { |
827 | 0 | if (prefix < 0 || prefix > 128) { |
828 | 0 | return xasprintf("%s: IPv6 network prefix bits not between 0 " |
829 | 0 | "and 128, inclusive", s); |
830 | 0 | } |
831 | 0 | *mask = ipv6_create_mask(prefix); |
832 | 0 | } else if (ovs_scan_len(s, n, "/"IPV6_SCAN_FMT, ipv6_s)) { |
833 | 0 | if (!ipv6_parse(ipv6_s, mask)) { |
834 | 0 | return xasprintf("%s: Invalid IPv6 mask", s); |
835 | 0 | } |
836 | | /* OK. */ |
837 | 0 | } else { |
838 | | /* OK. No mask. */ |
839 | 0 | *mask = in6addr_exact; |
840 | 0 | } |
841 | 0 | return NULL; |
842 | 0 | } |
843 | 0 | return xasprintf("%s: invalid IPv6 address", s); |
844 | 0 | } |
845 | | |
846 | | /* This function is similar to ipv6_parse_masked_len(), but doesn't return the |
847 | | * number of scanned characters and expects 's' to end following the |
848 | | * ipv6/(optional) mask. */ |
849 | | char * OVS_WARN_UNUSED_RESULT |
850 | | ipv6_parse_masked(const char *s, struct in6_addr *ip, struct in6_addr *mask) |
851 | 0 | { |
852 | 0 | int n = 0; |
853 | |
|
854 | 0 | char *error = ipv6_parse_masked_len(s, &n, ip, mask); |
855 | 0 | if (!error && s[n]) { |
856 | 0 | return xasprintf("%s: invalid IPv6 address", s); |
857 | 0 | } |
858 | 0 | return error; |
859 | 0 | } |
860 | | |
861 | | /* Similar to ipv6_parse_masked_len(), but the mask, if present, must be a CIDR |
862 | | * mask and is returned as a prefix length in '*plen'. */ |
863 | | char * OVS_WARN_UNUSED_RESULT |
864 | | ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip, |
865 | | unsigned int *plen) |
866 | 0 | { |
867 | 0 | struct in6_addr mask; |
868 | 0 | char *error; |
869 | |
|
870 | 0 | error = ipv6_parse_masked_len(s, n, ip, &mask); |
871 | 0 | if (error) { |
872 | 0 | return error; |
873 | 0 | } |
874 | | |
875 | 0 | if (!ipv6_is_cidr(&mask)) { |
876 | 0 | return xasprintf("%s: IPv6 CIDR network required", s); |
877 | 0 | } |
878 | 0 | *plen = ipv6_count_cidr_bits(&mask); |
879 | 0 | return NULL; |
880 | 0 | } |
881 | | |
882 | | /* Similar to ipv6_parse_cidr_len(), but doesn't return the number of scanned |
883 | | * characters and expects 's' to end after the ipv6/(optional) cidr. */ |
884 | | char * OVS_WARN_UNUSED_RESULT |
885 | | ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen) |
886 | 0 | { |
887 | 0 | int n = 0; |
888 | |
|
889 | 0 | char *error = ipv6_parse_cidr_len(s, &n, ip, plen); |
890 | 0 | if (!error && s[n]) { |
891 | 0 | return xasprintf("%s: invalid IPv6 address", s); |
892 | 0 | } |
893 | 0 | return error; |
894 | 0 | } |
895 | | |
896 | | /* Stores the string representation of the IPv6 address 'addr' into the |
897 | | * character array 'addr_str', which must be at least INET6_ADDRSTRLEN |
898 | | * bytes long. */ |
899 | | void |
900 | | ipv6_format_addr(const struct in6_addr *addr, struct ds *s) |
901 | 4.03k | { |
902 | 4.03k | char *dst; |
903 | | |
904 | 4.03k | ds_reserve(s, s->length + INET6_ADDRSTRLEN); |
905 | | |
906 | 4.03k | dst = s->string + s->length; |
907 | 4.03k | inet_ntop(AF_INET6, addr, dst, INET6_ADDRSTRLEN); |
908 | 4.03k | s->length += strlen(dst); |
909 | 4.03k | } |
910 | | |
911 | | /* Same as print_ipv6_addr, but optionally encloses the address in square |
912 | | * brackets. */ |
913 | | void |
914 | | ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *s, |
915 | | bool bracket) |
916 | 0 | { |
917 | 0 | if (bracket) { |
918 | 0 | ds_put_char(s, '['); |
919 | 0 | } |
920 | 0 | ipv6_format_addr(addr, s); |
921 | 0 | if (bracket) { |
922 | 0 | ds_put_char(s, ']'); |
923 | 0 | } |
924 | 0 | } |
925 | | |
926 | | void |
927 | | ipv6_format_mapped(const struct in6_addr *addr, struct ds *s) |
928 | 0 | { |
929 | 0 | if (IN6_IS_ADDR_V4MAPPED(addr)) { |
930 | 0 | ds_put_format(s, IP_FMT, addr->s6_addr[12], addr->s6_addr[13], |
931 | 0 | addr->s6_addr[14], addr->s6_addr[15]); |
932 | 0 | } else { |
933 | 0 | ipv6_format_addr(addr, s); |
934 | 0 | } |
935 | 0 | } |
936 | | |
937 | | void |
938 | | ipv6_format_masked(const struct in6_addr *addr, const struct in6_addr *mask, |
939 | | struct ds *s) |
940 | 4.03k | { |
941 | 4.03k | ipv6_format_addr(addr, s); |
942 | 4.03k | if (mask && !ipv6_mask_is_exact(mask)) { |
943 | 0 | if (ipv6_is_cidr(mask)) { |
944 | 0 | int cidr_bits = ipv6_count_cidr_bits(mask); |
945 | 0 | ds_put_format(s, "/%d", cidr_bits); |
946 | 0 | } else { |
947 | 0 | ds_put_char(s, '/'); |
948 | 0 | ipv6_format_addr(mask, s); |
949 | 0 | } |
950 | 0 | } |
951 | 4.03k | } |
952 | | |
953 | | /* Stores the string representation of the IPv6 address 'addr' into the |
954 | | * character array 'addr_str', which must be at least INET6_ADDRSTRLEN |
955 | | * bytes long. If addr is IPv4-mapped, store an IPv4 dotted-decimal string. */ |
956 | | const char * |
957 | | ipv6_string_mapped(char *addr_str, const struct in6_addr *addr) |
958 | 0 | { |
959 | 0 | ovs_be32 ip; |
960 | 0 | ip = in6_addr_get_mapped_ipv4(addr); |
961 | 0 | if (ip) { |
962 | 0 | return inet_ntop(AF_INET, &ip, addr_str, INET6_ADDRSTRLEN); |
963 | 0 | } else { |
964 | 0 | return inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN); |
965 | 0 | } |
966 | 0 | } |
967 | | |
968 | | #ifdef s6_addr32 |
969 | 0 | #define s6_addrX s6_addr32 |
970 | 0 | #define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 4; VAR++) |
971 | | #else |
972 | | #define s6_addrX s6_addr |
973 | | #define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 16; VAR++) |
974 | | #endif |
975 | | |
976 | | struct in6_addr |
977 | | ipv6_addr_bitand(const struct in6_addr *a, const struct in6_addr *b) |
978 | 0 | { |
979 | 0 | struct in6_addr dst; |
980 | 0 | IPV6_FOR_EACH (i) { |
981 | 0 | dst.s6_addrX[i] = a->s6_addrX[i] & b->s6_addrX[i]; |
982 | 0 | } |
983 | 0 | return dst; |
984 | 0 | } |
985 | | |
986 | | struct in6_addr |
987 | | ipv6_addr_bitxor(const struct in6_addr *a, const struct in6_addr *b) |
988 | 0 | { |
989 | 0 | struct in6_addr dst; |
990 | 0 | IPV6_FOR_EACH (i) { |
991 | 0 | dst.s6_addrX[i] = a->s6_addrX[i] ^ b->s6_addrX[i]; |
992 | 0 | } |
993 | 0 | return dst; |
994 | 0 | } |
995 | | |
996 | | bool |
997 | | ipv6_is_zero(const struct in6_addr *a) |
998 | 0 | { |
999 | 0 | IPV6_FOR_EACH (i) { |
1000 | 0 | if (a->s6_addrX[i]) { |
1001 | 0 | return false; |
1002 | 0 | } |
1003 | 0 | } |
1004 | 0 | return true; |
1005 | 0 | } |
1006 | | |
1007 | | /* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N |
1008 | | * low-order 0-bits. */ |
1009 | | struct in6_addr |
1010 | | ipv6_create_mask(int mask) |
1011 | 0 | { |
1012 | 0 | struct in6_addr netmask; |
1013 | 0 | uint8_t *netmaskp = &netmask.s6_addr[0]; |
1014 | |
|
1015 | 0 | memset(&netmask, 0, sizeof netmask); |
1016 | 0 | while (mask > 8) { |
1017 | 0 | *netmaskp = 0xff; |
1018 | 0 | netmaskp++; |
1019 | 0 | mask -= 8; |
1020 | 0 | } |
1021 | |
|
1022 | 0 | if (mask) { |
1023 | 0 | *netmaskp = 0xff << (8 - mask); |
1024 | 0 | } |
1025 | |
|
1026 | 0 | return netmask; |
1027 | 0 | } |
1028 | | |
1029 | | /* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6 |
1030 | | * address that it specifies, that is, the number of 1-bits in 'netmask'. |
1031 | | * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()). |
1032 | | * |
1033 | | * If 'netmask' is not a CIDR netmask (see ipv6_is_cidr()), the return value |
1034 | | * will still be in the valid range but isn't otherwise meaningful. */ |
1035 | | int |
1036 | | ipv6_count_cidr_bits(const struct in6_addr *netmask) |
1037 | 0 | { |
1038 | 0 | int i; |
1039 | 0 | int count = 0; |
1040 | 0 | const uint8_t *netmaskp = &netmask->s6_addr[0]; |
1041 | |
|
1042 | 0 | for (i=0; i<16; i++) { |
1043 | 0 | if (netmaskp[i] == 0xff) { |
1044 | 0 | count += 8; |
1045 | 0 | } else { |
1046 | 0 | uint8_t nm; |
1047 | |
|
1048 | 0 | for(nm = netmaskp[i]; nm; nm <<= 1) { |
1049 | 0 | count++; |
1050 | 0 | } |
1051 | 0 | break; |
1052 | 0 | } |
1053 | |
|
1054 | 0 | } |
1055 | |
|
1056 | 0 | return count; |
1057 | 0 | } |
1058 | | |
1059 | | /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N |
1060 | | * high-order 1-bits and 128-N low-order 0-bits. */ |
1061 | | bool |
1062 | | ipv6_is_cidr(const struct in6_addr *netmask) |
1063 | 0 | { |
1064 | 0 | const uint8_t *netmaskp = &netmask->s6_addr[0]; |
1065 | 0 | int i; |
1066 | |
|
1067 | 0 | for (i=0; i<16; i++) { |
1068 | 0 | if (netmaskp[i] != 0xff) { |
1069 | 0 | uint8_t x = ~netmaskp[i]; |
1070 | 0 | if (x & (x + 1)) { |
1071 | 0 | return false; |
1072 | 0 | } |
1073 | 0 | while (++i < 16) { |
1074 | 0 | if (netmaskp[i]) { |
1075 | 0 | return false; |
1076 | 0 | } |
1077 | 0 | } |
1078 | 0 | } |
1079 | 0 | } |
1080 | | |
1081 | 0 | return true; |
1082 | 0 | } |
1083 | | |
1084 | | /* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst', |
1085 | | * 'eth_src' and 'eth_type' parameters. A payload of 'size' bytes is allocated |
1086 | | * in 'b' and returned. This payload may be populated with appropriate |
1087 | | * information by the caller. Sets 'b''s 'frame' pointer and 'l3' offset to |
1088 | | * the Ethernet header and payload respectively. Aligns b->l3 on a 32-bit |
1089 | | * boundary. |
1090 | | * |
1091 | | * The returned packet has enough headroom to insert an 802.1Q VLAN header if |
1092 | | * desired. */ |
1093 | | void * |
1094 | | eth_compose(struct dp_packet *b, const struct eth_addr eth_dst, |
1095 | | const struct eth_addr eth_src, uint16_t eth_type, |
1096 | | size_t size) |
1097 | 0 | { |
1098 | 0 | void *data; |
1099 | 0 | struct eth_header *eth; |
1100 | | |
1101 | |
|
1102 | 0 | dp_packet_clear(b); |
1103 | | |
1104 | | /* The magic 2 here ensures that the L3 header (when it is added later) |
1105 | | * will be 32-bit aligned. */ |
1106 | 0 | dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + size); |
1107 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
1108 | 0 | eth = dp_packet_put_uninit(b, ETH_HEADER_LEN); |
1109 | 0 | data = dp_packet_put_zeros(b, size); |
1110 | |
|
1111 | 0 | eth->eth_dst = eth_dst; |
1112 | 0 | eth->eth_src = eth_src; |
1113 | 0 | eth->eth_type = htons(eth_type); |
1114 | |
|
1115 | 0 | b->packet_type = htonl(PT_ETH); |
1116 | 0 | dp_packet_set_l3(b, data); |
1117 | |
|
1118 | 0 | return data; |
1119 | 0 | } |
1120 | | |
1121 | | void |
1122 | | packet_set_ipv4_addr(struct dp_packet *packet, |
1123 | | ovs_16aligned_be32 *addr, ovs_be32 new_addr) |
1124 | 0 | { |
1125 | 0 | struct ip_header *nh = dp_packet_l3(packet); |
1126 | 0 | ovs_be32 old_addr = get_16aligned_be32(addr); |
1127 | 0 | size_t l4_size = dp_packet_l4_size(packet); |
1128 | |
|
1129 | 0 | pkt_metadata_init_conn(&packet->md); |
1130 | |
|
1131 | 0 | if (nh->ip_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) { |
1132 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1133 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1134 | 0 | } else { |
1135 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1136 | 0 | th->tcp_csum = recalc_csum32(th->tcp_csum, old_addr, new_addr); |
1137 | 0 | } |
1138 | 0 | } else if (nh->ip_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN ) { |
1139 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1140 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1141 | 0 | } else { |
1142 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1143 | 0 | if (uh->udp_csum) { |
1144 | 0 | uh->udp_csum = recalc_csum32(uh->udp_csum, old_addr, new_addr); |
1145 | 0 | if (!uh->udp_csum) { |
1146 | 0 | uh->udp_csum = htons(0xffff); |
1147 | 0 | } |
1148 | 0 | } |
1149 | 0 | } |
1150 | 0 | } |
1151 | |
|
1152 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1153 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1154 | 0 | } else { |
1155 | 0 | nh->ip_csum = recalc_csum32(nh->ip_csum, old_addr, new_addr); |
1156 | 0 | } |
1157 | 0 | put_16aligned_be32(addr, new_addr); |
1158 | 0 | } |
1159 | | |
1160 | | /* Returns true, if packet contains at least one routing header where |
1161 | | * segements_left > 0. |
1162 | | * |
1163 | | * This function assumes that L3 and L4 offsets are set in the packet. */ |
1164 | | bool |
1165 | | packet_rh_present(struct dp_packet *packet, uint8_t *nexthdr, bool *first_frag) |
1166 | 0 | { |
1167 | 0 | const struct ovs_16aligned_ip6_hdr *nh; |
1168 | 0 | size_t len; |
1169 | 0 | size_t remaining; |
1170 | 0 | uint8_t *data = dp_packet_l3(packet); |
1171 | |
|
1172 | 0 | remaining = packet->l4_ofs - packet->l3_ofs; |
1173 | 0 | if (remaining < sizeof *nh) { |
1174 | 0 | return false; |
1175 | 0 | } |
1176 | 0 | nh = ALIGNED_CAST(struct ovs_16aligned_ip6_hdr *, data); |
1177 | 0 | data += sizeof *nh; |
1178 | 0 | remaining -= sizeof *nh; |
1179 | 0 | *nexthdr = nh->ip6_nxt; |
1180 | |
|
1181 | 0 | while (1) { |
1182 | 0 | if ((*nexthdr != IPPROTO_HOPOPTS) |
1183 | 0 | && (*nexthdr != IPPROTO_ROUTING) |
1184 | 0 | && (*nexthdr != IPPROTO_DSTOPTS) |
1185 | 0 | && (*nexthdr != IPPROTO_AH) |
1186 | 0 | && (*nexthdr != IPPROTO_FRAGMENT)) { |
1187 | | /* It's either a terminal header (e.g., TCP, UDP) or one we |
1188 | | * don't understand. In either case, we're done with the |
1189 | | * packet, so use it to fill in 'nw_proto'. */ |
1190 | 0 | break; |
1191 | 0 | } |
1192 | | |
1193 | | /* We only verify that at least 8 bytes of the next header are |
1194 | | * available, but many of these headers are longer. Ensure that |
1195 | | * accesses within the extension header are within those first 8 |
1196 | | * bytes. All extension headers are required to be at least 8 |
1197 | | * bytes. */ |
1198 | 0 | if (remaining < 8) { |
1199 | 0 | return false; |
1200 | 0 | } |
1201 | | |
1202 | 0 | if (*nexthdr == IPPROTO_AH) { |
1203 | | /* A standard AH definition isn't available, but the fields |
1204 | | * we care about are in the same location as the generic |
1205 | | * option header--only the header length is calculated |
1206 | | * differently. */ |
1207 | 0 | const struct ip6_ext *ext_hdr = (struct ip6_ext *)data; |
1208 | |
|
1209 | 0 | *nexthdr = ext_hdr->ip6e_nxt; |
1210 | 0 | len = (ext_hdr->ip6e_len + 2) * 4; |
1211 | 0 | } else if (*nexthdr == IPPROTO_FRAGMENT) { |
1212 | 0 | const struct ovs_16aligned_ip6_frag *frag_hdr |
1213 | 0 | = ALIGNED_CAST(struct ovs_16aligned_ip6_frag *, data); |
1214 | |
|
1215 | 0 | *first_frag = !(frag_hdr->ip6f_offlg & IP6F_OFF_MASK) && |
1216 | 0 | (frag_hdr->ip6f_offlg & IP6F_MORE_FRAG); |
1217 | 0 | *nexthdr = frag_hdr->ip6f_nxt; |
1218 | 0 | len = sizeof *frag_hdr; |
1219 | 0 | } else if (*nexthdr == IPPROTO_ROUTING) { |
1220 | 0 | const struct ip6_rthdr *rh = (struct ip6_rthdr *)data; |
1221 | |
|
1222 | 0 | if (rh->ip6r_segleft > 0) { |
1223 | 0 | return true; |
1224 | 0 | } |
1225 | | |
1226 | 0 | *nexthdr = rh->ip6r_nxt; |
1227 | 0 | len = (rh->ip6r_len + 1) * 8; |
1228 | 0 | } else { |
1229 | 0 | const struct ip6_ext *ext_hdr = (struct ip6_ext *)data; |
1230 | |
|
1231 | 0 | *nexthdr = ext_hdr->ip6e_nxt; |
1232 | 0 | len = (ext_hdr->ip6e_len + 1) * 8; |
1233 | 0 | } |
1234 | | |
1235 | 0 | if (remaining < len) { |
1236 | 0 | return false; |
1237 | 0 | } |
1238 | 0 | remaining -= len; |
1239 | 0 | data += len; |
1240 | 0 | } |
1241 | | |
1242 | 0 | return false; |
1243 | 0 | } |
1244 | | |
1245 | | static void |
1246 | | packet_update_csum128(struct dp_packet *packet, uint8_t proto, |
1247 | | ovs_16aligned_be32 addr[4], |
1248 | | const struct in6_addr *new_addr) |
1249 | 0 | { |
1250 | 0 | size_t l4_size = dp_packet_l4_size(packet); |
1251 | |
|
1252 | 0 | if (proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) { |
1253 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1254 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1255 | 0 | } else { |
1256 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1257 | |
|
1258 | 0 | th->tcp_csum = recalc_csum128(th->tcp_csum, addr, new_addr); |
1259 | 0 | } |
1260 | 0 | } else if (proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) { |
1261 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1262 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1263 | 0 | } else { |
1264 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1265 | |
|
1266 | 0 | if (uh->udp_csum) { |
1267 | 0 | uh->udp_csum = recalc_csum128(uh->udp_csum, addr, new_addr); |
1268 | 0 | if (!uh->udp_csum) { |
1269 | 0 | uh->udp_csum = htons(0xffff); |
1270 | 0 | } |
1271 | 0 | } |
1272 | 0 | } |
1273 | 0 | } else if (proto == IPPROTO_ICMPV6 && |
1274 | 0 | l4_size >= sizeof(struct icmp6_header)) { |
1275 | 0 | struct icmp6_header *icmp = dp_packet_l4(packet); |
1276 | |
|
1277 | 0 | icmp->icmp6_cksum = recalc_csum128(icmp->icmp6_cksum, addr, new_addr); |
1278 | 0 | } |
1279 | 0 | } |
1280 | | |
1281 | | void |
1282 | | packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto, |
1283 | | ovs_16aligned_be32 addr[4], |
1284 | | const struct in6_addr *new_addr, |
1285 | | bool recalculate_csum) |
1286 | 0 | { |
1287 | 0 | if (recalculate_csum) { |
1288 | 0 | packet_update_csum128(packet, proto, addr, new_addr); |
1289 | 0 | } |
1290 | 0 | memcpy(addr, new_addr, sizeof(ovs_be32[4])); |
1291 | 0 | pkt_metadata_init_conn(&packet->md); |
1292 | 0 | } |
1293 | | |
1294 | | void |
1295 | | packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key) |
1296 | 0 | { |
1297 | 0 | ovs_be32 old_label = get_16aligned_be32(flow_label); |
1298 | 0 | ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key; |
1299 | 0 | put_16aligned_be32(flow_label, new_label); |
1300 | 0 | } |
1301 | | |
1302 | | void |
1303 | | packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc) |
1304 | 0 | { |
1305 | 0 | ovs_be32 old_label = get_16aligned_be32(flow_label); |
1306 | 0 | ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20); |
1307 | 0 | put_16aligned_be32(flow_label, new_label); |
1308 | 0 | } |
1309 | | |
1310 | | /* Modifies the IPv4 header fields of 'packet' to be consistent with 'src', |
1311 | | * 'dst', 'tos', and 'ttl'. Updates 'packet''s L4 checksums as appropriate. |
1312 | | * 'packet' must contain a valid IPv4 packet with correctly populated l[347] |
1313 | | * markers. */ |
1314 | | void |
1315 | | packet_set_ipv4(struct dp_packet *packet, ovs_be32 src, ovs_be32 dst, |
1316 | | uint8_t tos, uint8_t ttl) |
1317 | 0 | { |
1318 | 0 | struct ip_header *nh = dp_packet_l3(packet); |
1319 | |
|
1320 | 0 | if (get_16aligned_be32(&nh->ip_src) != src) { |
1321 | 0 | packet_set_ipv4_addr(packet, &nh->ip_src, src); |
1322 | 0 | } |
1323 | |
|
1324 | 0 | if (get_16aligned_be32(&nh->ip_dst) != dst) { |
1325 | 0 | packet_set_ipv4_addr(packet, &nh->ip_dst, dst); |
1326 | 0 | } |
1327 | |
|
1328 | 0 | if (nh->ip_tos != tos) { |
1329 | 0 | uint8_t *field = &nh->ip_tos; |
1330 | |
|
1331 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1332 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1333 | 0 | } else { |
1334 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t) *field), |
1335 | 0 | htons((uint16_t) tos)); |
1336 | 0 | } |
1337 | |
|
1338 | 0 | *field = tos; |
1339 | 0 | } |
1340 | |
|
1341 | 0 | if (nh->ip_ttl != ttl) { |
1342 | 0 | uint8_t *field = &nh->ip_ttl; |
1343 | |
|
1344 | 0 | if (dp_packet_ip_checksum_valid(packet)) { |
1345 | 0 | dp_packet_ip_checksum_set_partial(packet); |
1346 | 0 | } else { |
1347 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons(*field << 8), |
1348 | 0 | htons(ttl << 8)); |
1349 | 0 | } |
1350 | |
|
1351 | 0 | *field = ttl; |
1352 | 0 | } |
1353 | 0 | } |
1354 | | |
1355 | | /* Modifies the IPv6 header fields of 'packet' to be consistent with 'src', |
1356 | | * 'dst', 'traffic class', and 'next hop'. Updates 'packet''s L4 checksums as |
1357 | | * appropriate. 'packet' must contain a valid IPv6 packet with correctly |
1358 | | * populated l[34] offsets. */ |
1359 | | void |
1360 | | packet_set_ipv6(struct dp_packet *packet, const struct in6_addr *src, |
1361 | | const struct in6_addr *dst, uint8_t key_tc, ovs_be32 key_fl, |
1362 | | uint8_t key_hl) |
1363 | 0 | { |
1364 | 0 | struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet); |
1365 | 0 | bool recalc_csum = true; |
1366 | 0 | uint8_t proto = 0; |
1367 | 0 | bool rh_present; |
1368 | |
|
1369 | 0 | rh_present = packet_rh_present(packet, &proto, &recalc_csum); |
1370 | |
|
1371 | 0 | if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) { |
1372 | 0 | packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32, |
1373 | 0 | src, recalc_csum); |
1374 | 0 | } |
1375 | |
|
1376 | 0 | if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) { |
1377 | 0 | packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst, |
1378 | 0 | !rh_present && recalc_csum); |
1379 | 0 | } |
1380 | |
|
1381 | 0 | packet_set_ipv6_tc(&nh->ip6_flow, key_tc); |
1382 | 0 | packet_set_ipv6_flow_label(&nh->ip6_flow, key_fl); |
1383 | 0 | nh->ip6_hlim = key_hl; |
1384 | 0 | } |
1385 | | |
1386 | | static void |
1387 | | packet_set_port(ovs_be16 *port, ovs_be16 new_port, ovs_be16 *csum) |
1388 | 0 | { |
1389 | 0 | if (*port != new_port) { |
1390 | 0 | if (csum) { |
1391 | 0 | *csum = recalc_csum16(*csum, *port, new_port); |
1392 | 0 | } |
1393 | 0 | *port = new_port; |
1394 | 0 | } |
1395 | 0 | } |
1396 | | |
1397 | | /* Sets the TCP source and destination port ('src' and 'dst' respectively) of |
1398 | | * the TCP header contained in 'packet'. 'packet' must be a valid TCP packet |
1399 | | * with its l4 offset properly populated. */ |
1400 | | void |
1401 | | packet_set_tcp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1402 | 0 | { |
1403 | 0 | struct tcp_header *th = dp_packet_l4(packet); |
1404 | 0 | ovs_be16 *csum = NULL; |
1405 | |
|
1406 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1407 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1408 | 0 | } else { |
1409 | 0 | csum = &th->tcp_csum; |
1410 | 0 | } |
1411 | |
|
1412 | 0 | packet_set_port(&th->tcp_src, src, csum); |
1413 | 0 | packet_set_port(&th->tcp_dst, dst, csum); |
1414 | 0 | pkt_metadata_init_conn(&packet->md); |
1415 | 0 | } |
1416 | | |
1417 | | /* Sets the UDP source and destination port ('src' and 'dst' respectively) of |
1418 | | * the UDP header contained in 'packet'. 'packet' must be a valid UDP packet |
1419 | | * with its l4 offset properly populated. */ |
1420 | | void |
1421 | | packet_set_udp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1422 | 0 | { |
1423 | 0 | struct udp_header *uh = dp_packet_l4(packet); |
1424 | |
|
1425 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1426 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1427 | 0 | packet_set_port(&uh->udp_src, src, NULL); |
1428 | 0 | packet_set_port(&uh->udp_dst, dst, NULL); |
1429 | 0 | } else { |
1430 | 0 | ovs_be16 *csum = uh->udp_csum ? &uh->udp_csum : NULL; |
1431 | |
|
1432 | 0 | packet_set_port(&uh->udp_src, src, csum); |
1433 | 0 | packet_set_port(&uh->udp_dst, dst, csum); |
1434 | |
|
1435 | 0 | if (csum && !uh->udp_csum) { |
1436 | 0 | uh->udp_csum = htons(0xffff); |
1437 | 0 | } |
1438 | 0 | } |
1439 | |
|
1440 | 0 | pkt_metadata_init_conn(&packet->md); |
1441 | 0 | } |
1442 | | |
1443 | | /* Sets the SCTP source and destination port ('src' and 'dst' respectively) of |
1444 | | * the SCTP header contained in 'packet'. 'packet' must be a valid SCTP packet |
1445 | | * with its l4 offset properly populated. */ |
1446 | | void |
1447 | | packet_set_sctp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst) |
1448 | 0 | { |
1449 | 0 | struct sctp_header *sh = dp_packet_l4(packet); |
1450 | |
|
1451 | 0 | if (dp_packet_l4_checksum_valid(packet)) { |
1452 | 0 | dp_packet_l4_checksum_set_partial(packet); |
1453 | 0 | sh->sctp_src = src; |
1454 | 0 | sh->sctp_dst = dst; |
1455 | 0 | } else { |
1456 | 0 | ovs_be32 old_csum, old_correct_csum, new_csum; |
1457 | 0 | uint16_t tp_len = dp_packet_l4_size(packet); |
1458 | |
|
1459 | 0 | old_csum = get_16aligned_be32(&sh->sctp_csum); |
1460 | 0 | put_16aligned_be32(&sh->sctp_csum, 0); |
1461 | 0 | old_correct_csum = crc32c((void *) sh, tp_len); |
1462 | |
|
1463 | 0 | sh->sctp_src = src; |
1464 | 0 | sh->sctp_dst = dst; |
1465 | |
|
1466 | 0 | new_csum = crc32c((void *) sh, tp_len); |
1467 | 0 | put_16aligned_be32(&sh->sctp_csum, old_csum ^ old_correct_csum |
1468 | 0 | ^ new_csum); |
1469 | 0 | } |
1470 | |
|
1471 | 0 | pkt_metadata_init_conn(&packet->md); |
1472 | 0 | } |
1473 | | |
1474 | | /* Sets the ICMP type and code of the ICMP header contained in 'packet'. |
1475 | | * 'packet' must be a valid ICMP packet with its l4 offset properly |
1476 | | * populated. */ |
1477 | | void |
1478 | | packet_set_icmp(struct dp_packet *packet, uint8_t type, uint8_t code) |
1479 | 0 | { |
1480 | 0 | struct icmp_header *ih = dp_packet_l4(packet); |
1481 | 0 | ovs_be16 orig_tc = htons(ih->icmp_type << 8 | ih->icmp_code); |
1482 | 0 | ovs_be16 new_tc = htons(type << 8 | code); |
1483 | |
|
1484 | 0 | if (orig_tc != new_tc) { |
1485 | 0 | ih->icmp_type = type; |
1486 | 0 | ih->icmp_code = code; |
1487 | |
|
1488 | 0 | ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_tc, new_tc); |
1489 | 0 | } |
1490 | 0 | pkt_metadata_init_conn(&packet->md); |
1491 | 0 | } |
1492 | | |
1493 | | /* Sets the IGMP type to IGMP_HOST_MEMBERSHIP_QUERY and populates the |
1494 | | * v3 query header fields in 'packet'. 'packet' must be a valid IGMPv3 |
1495 | | * query packet with its l4 offset properly populated. |
1496 | | */ |
1497 | | void |
1498 | | packet_set_igmp3_query(struct dp_packet *packet, uint8_t max_resp, |
1499 | | ovs_be32 group, bool srs, uint8_t qrv, uint8_t qqic) |
1500 | 0 | { |
1501 | 0 | struct igmpv3_query_header *igh = dp_packet_l4(packet); |
1502 | 0 | ovs_be16 orig_type_max_resp = |
1503 | 0 | htons(igh->type << 8 | igh->max_resp); |
1504 | 0 | ovs_be16 new_type_max_resp = |
1505 | 0 | htons(IGMP_HOST_MEMBERSHIP_QUERY << 8 | max_resp); |
1506 | |
|
1507 | 0 | if (orig_type_max_resp != new_type_max_resp) { |
1508 | 0 | igh->type = IGMP_HOST_MEMBERSHIP_QUERY; |
1509 | 0 | igh->max_resp = max_resp; |
1510 | 0 | igh->csum = recalc_csum16(igh->csum, orig_type_max_resp, |
1511 | 0 | new_type_max_resp); |
1512 | 0 | } |
1513 | |
|
1514 | 0 | ovs_be32 old_group = get_16aligned_be32(&igh->group); |
1515 | |
|
1516 | 0 | if (old_group != group) { |
1517 | 0 | put_16aligned_be32(&igh->group, group); |
1518 | 0 | igh->csum = recalc_csum32(igh->csum, old_group, group); |
1519 | 0 | } |
1520 | | |
1521 | | /* See RFC 3376 4.1.6. */ |
1522 | 0 | if (qrv > 7) { |
1523 | 0 | qrv = 0; |
1524 | 0 | } |
1525 | |
|
1526 | 0 | ovs_be16 orig_srs_qrv_qqic = htons(igh->srs_qrv << 8 | igh->qqic); |
1527 | 0 | ovs_be16 new_srs_qrv_qqic = htons(srs << 11 | qrv << 8 | qqic); |
1528 | |
|
1529 | 0 | if (orig_srs_qrv_qqic != new_srs_qrv_qqic) { |
1530 | 0 | igh->srs_qrv = (srs << 3 | qrv); |
1531 | 0 | igh->qqic = qqic; |
1532 | 0 | igh->csum = recalc_csum16(igh->csum, orig_srs_qrv_qqic, |
1533 | 0 | new_srs_qrv_qqic); |
1534 | 0 | } |
1535 | 0 | } |
1536 | | |
1537 | | void |
1538 | | packet_set_nd_ext(struct dp_packet *packet, const ovs_16aligned_be32 rso_flags, |
1539 | | const uint8_t opt_type) |
1540 | 0 | { |
1541 | 0 | struct ovs_nd_msg *ns; |
1542 | 0 | struct ovs_nd_lla_opt *opt; |
1543 | 0 | int bytes_remain = dp_packet_l4_size(packet); |
1544 | 0 | struct ovs_16aligned_ip6_hdr * nh = dp_packet_l3(packet); |
1545 | 0 | uint32_t pseudo_hdr_csum = 0; |
1546 | |
|
1547 | 0 | if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) { |
1548 | 0 | return; |
1549 | 0 | } |
1550 | | |
1551 | 0 | if (nh) { |
1552 | 0 | pseudo_hdr_csum = packet_csum_pseudoheader6(nh); |
1553 | 0 | } |
1554 | |
|
1555 | 0 | ns = dp_packet_l4(packet); |
1556 | 0 | opt = &ns->options[0]; |
1557 | | |
1558 | | /* set RSO flags and option type */ |
1559 | 0 | ns->rso_flags = rso_flags; |
1560 | 0 | opt->type = opt_type; |
1561 | | |
1562 | | /* recalculate checksum */ |
1563 | 0 | ovs_be16 *csum_value = &(ns->icmph.icmp6_cksum); |
1564 | 0 | *csum_value = 0; |
1565 | 0 | *csum_value = csum_finish(csum_continue(pseudo_hdr_csum, |
1566 | 0 | &(ns->icmph), bytes_remain)); |
1567 | |
|
1568 | 0 | } |
1569 | | |
1570 | | void |
1571 | | packet_set_nd(struct dp_packet *packet, const struct in6_addr *target, |
1572 | | const struct eth_addr sll, const struct eth_addr tll) |
1573 | 0 | { |
1574 | 0 | struct ovs_nd_msg *ns; |
1575 | 0 | struct ovs_nd_lla_opt *opt; |
1576 | 0 | int bytes_remain = dp_packet_l4_size(packet); |
1577 | |
|
1578 | 0 | if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) { |
1579 | 0 | return; |
1580 | 0 | } |
1581 | | |
1582 | 0 | ns = dp_packet_l4(packet); |
1583 | 0 | opt = &ns->options[0]; |
1584 | 0 | bytes_remain -= sizeof(*ns); |
1585 | |
|
1586 | 0 | if (memcmp(&ns->target, target, sizeof(ovs_be32[4]))) { |
1587 | 0 | packet_set_ipv6_addr(packet, IPPROTO_ICMPV6, ns->target.be32, target, |
1588 | 0 | true); |
1589 | 0 | } |
1590 | |
|
1591 | 0 | while (bytes_remain >= ND_LLA_OPT_LEN && opt->len != 0) { |
1592 | 0 | if (opt->type == ND_OPT_SOURCE_LINKADDR && opt->len == 1) { |
1593 | 0 | if (!eth_addr_equals(opt->mac, sll)) { |
1594 | 0 | ovs_be16 *csum = &(ns->icmph.icmp6_cksum); |
1595 | |
|
1596 | 0 | *csum = recalc_csum48(*csum, opt->mac, sll); |
1597 | 0 | opt->mac = sll; |
1598 | 0 | } |
1599 | | |
1600 | | /* A packet can only contain one SLL or TLL option */ |
1601 | 0 | break; |
1602 | 0 | } else if (opt->type == ND_OPT_TARGET_LINKADDR && opt->len == 1) { |
1603 | 0 | if (!eth_addr_equals(opt->mac, tll)) { |
1604 | 0 | ovs_be16 *csum = &(ns->icmph.icmp6_cksum); |
1605 | |
|
1606 | 0 | *csum = recalc_csum48(*csum, opt->mac, tll); |
1607 | 0 | opt->mac = tll; |
1608 | 0 | } |
1609 | | |
1610 | | /* A packet can only contain one SLL or TLL option */ |
1611 | 0 | break; |
1612 | 0 | } |
1613 | | |
1614 | 0 | opt += opt->len; |
1615 | 0 | bytes_remain -= opt->len * ND_LLA_OPT_LEN; |
1616 | 0 | } |
1617 | 0 | } |
1618 | | |
1619 | | const char * |
1620 | | packet_tcp_flag_to_string(uint32_t flag) |
1621 | 3.17k | { |
1622 | 3.17k | switch (flag) { |
1623 | 332 | case TCP_FIN: |
1624 | 332 | return "fin"; |
1625 | 276 | case TCP_SYN: |
1626 | 276 | return "syn"; |
1627 | 272 | case TCP_RST: |
1628 | 272 | return "rst"; |
1629 | 216 | case TCP_PSH: |
1630 | 216 | return "psh"; |
1631 | 356 | case TCP_ACK: |
1632 | 356 | return "ack"; |
1633 | 184 | case TCP_URG: |
1634 | 184 | return "urg"; |
1635 | 374 | case TCP_ECE: |
1636 | 374 | return "ece"; |
1637 | 148 | case TCP_CWR: |
1638 | 148 | return "cwr"; |
1639 | 308 | case TCP_NS: |
1640 | 308 | return "ns"; |
1641 | 232 | case 0x200: |
1642 | 232 | return "[200]"; |
1643 | 312 | case 0x400: |
1644 | 312 | return "[400]"; |
1645 | 164 | case 0x800: |
1646 | 164 | return "[800]"; |
1647 | 0 | default: |
1648 | 0 | return NULL; |
1649 | 3.17k | } |
1650 | 3.17k | } |
1651 | | |
1652 | | /* Appends a string representation of the TCP flags value 'tcp_flags' |
1653 | | * (e.g. from struct flow.tcp_flags or obtained via TCP_FLAGS) to 's', in the |
1654 | | * format used by tcpdump. */ |
1655 | | void |
1656 | | packet_format_tcp_flags(struct ds *s, uint16_t tcp_flags) |
1657 | 0 | { |
1658 | 0 | if (!tcp_flags) { |
1659 | 0 | ds_put_cstr(s, "none"); |
1660 | 0 | return; |
1661 | 0 | } |
1662 | | |
1663 | 0 | if (tcp_flags & TCP_SYN) { |
1664 | 0 | ds_put_char(s, 'S'); |
1665 | 0 | } |
1666 | 0 | if (tcp_flags & TCP_FIN) { |
1667 | 0 | ds_put_char(s, 'F'); |
1668 | 0 | } |
1669 | 0 | if (tcp_flags & TCP_PSH) { |
1670 | 0 | ds_put_char(s, 'P'); |
1671 | 0 | } |
1672 | 0 | if (tcp_flags & TCP_RST) { |
1673 | 0 | ds_put_char(s, 'R'); |
1674 | 0 | } |
1675 | 0 | if (tcp_flags & TCP_URG) { |
1676 | 0 | ds_put_char(s, 'U'); |
1677 | 0 | } |
1678 | 0 | if (tcp_flags & TCP_ACK) { |
1679 | 0 | ds_put_char(s, '.'); |
1680 | 0 | } |
1681 | 0 | if (tcp_flags & TCP_ECE) { |
1682 | 0 | ds_put_cstr(s, "E"); |
1683 | 0 | } |
1684 | 0 | if (tcp_flags & TCP_CWR) { |
1685 | 0 | ds_put_cstr(s, "C"); |
1686 | 0 | } |
1687 | 0 | if (tcp_flags & TCP_NS) { |
1688 | 0 | ds_put_cstr(s, "N"); |
1689 | 0 | } |
1690 | 0 | if (tcp_flags & 0x200) { |
1691 | 0 | ds_put_cstr(s, "[200]"); |
1692 | 0 | } |
1693 | 0 | if (tcp_flags & 0x400) { |
1694 | 0 | ds_put_cstr(s, "[400]"); |
1695 | 0 | } |
1696 | 0 | if (tcp_flags & 0x800) { |
1697 | 0 | ds_put_cstr(s, "[800]"); |
1698 | 0 | } |
1699 | 0 | } |
1700 | | |
1701 | 0 | #define ARP_PACKET_SIZE (2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + \ |
1702 | 0 | ARP_ETH_HEADER_LEN) |
1703 | | |
1704 | | /* Clears 'b' and replaces its contents by an ARP frame with the specified |
1705 | | * 'arp_op', 'arp_sha', 'arp_tha', 'arp_spa', and 'arp_tpa'. The outer |
1706 | | * Ethernet frame is initialized with Ethernet source 'arp_sha' and destination |
1707 | | * 'arp_tha', except that destination ff:ff:ff:ff:ff:ff is used instead if |
1708 | | * 'broadcast' is true. Points the L3 header to the ARP header. */ |
1709 | | void |
1710 | | compose_arp(struct dp_packet *b, uint16_t arp_op, |
1711 | | const struct eth_addr arp_sha, const struct eth_addr arp_tha, |
1712 | | bool broadcast, ovs_be32 arp_spa, ovs_be32 arp_tpa) |
1713 | 0 | { |
1714 | 0 | compose_arp__(b); |
1715 | |
|
1716 | 0 | struct eth_header *eth = dp_packet_eth(b); |
1717 | 0 | eth->eth_dst = broadcast ? eth_addr_broadcast : arp_tha; |
1718 | 0 | eth->eth_src = arp_sha; |
1719 | |
|
1720 | 0 | struct arp_eth_header *arp = dp_packet_l3(b); |
1721 | 0 | arp->ar_op = htons(arp_op); |
1722 | 0 | arp->ar_sha = arp_sha; |
1723 | 0 | arp->ar_tha = arp_tha; |
1724 | 0 | put_16aligned_be32(&arp->ar_spa, arp_spa); |
1725 | 0 | put_16aligned_be32(&arp->ar_tpa, arp_tpa); |
1726 | 0 | } |
1727 | | |
1728 | | /* Clears 'b' and replaces its contents by an ARP frame. Sets the fields in |
1729 | | * the Ethernet and ARP headers that are fixed for ARP frames to those fixed |
1730 | | * values, and zeroes the other fields. Points the L3 header to the ARP |
1731 | | * header. */ |
1732 | | void |
1733 | | compose_arp__(struct dp_packet *b) |
1734 | 0 | { |
1735 | 0 | dp_packet_clear(b); |
1736 | 0 | dp_packet_prealloc_tailroom(b, ARP_PACKET_SIZE); |
1737 | 0 | dp_packet_reserve(b, 2 + VLAN_HEADER_LEN); |
1738 | |
|
1739 | 0 | struct eth_header *eth = dp_packet_put_zeros(b, sizeof *eth); |
1740 | 0 | eth->eth_type = htons(ETH_TYPE_ARP); |
1741 | |
|
1742 | 0 | struct arp_eth_header *arp = dp_packet_put_zeros(b, sizeof *arp); |
1743 | 0 | arp->ar_hrd = htons(ARP_HRD_ETHERNET); |
1744 | 0 | arp->ar_pro = htons(ARP_PRO_IP); |
1745 | 0 | arp->ar_hln = sizeof arp->ar_sha; |
1746 | 0 | arp->ar_pln = sizeof arp->ar_spa; |
1747 | |
|
1748 | 0 | dp_packet_set_l3(b, arp); |
1749 | |
|
1750 | 0 | b->packet_type = htonl(PT_ETH); |
1751 | 0 | } |
1752 | | |
1753 | | /* This function expects packet with ethernet header with correct |
1754 | | * l3 pointer set. */ |
1755 | | void * |
1756 | | compose_ipv6(struct dp_packet *packet, uint8_t proto, |
1757 | | const struct in6_addr *src, const struct in6_addr *dst, |
1758 | | uint8_t key_tc, ovs_be32 key_fl, uint8_t key_hl, int size) |
1759 | 0 | { |
1760 | 0 | struct ovs_16aligned_ip6_hdr *nh; |
1761 | 0 | void *data; |
1762 | |
|
1763 | 0 | nh = dp_packet_l3(packet); |
1764 | 0 | nh->ip6_vfc = 0x60; |
1765 | 0 | nh->ip6_nxt = proto; |
1766 | 0 | nh->ip6_plen = htons(size); |
1767 | 0 | data = dp_packet_put_zeros(packet, size); |
1768 | 0 | dp_packet_set_l4(packet, data); |
1769 | 0 | packet_set_ipv6(packet, src, dst, key_tc, key_fl, key_hl); |
1770 | 0 | return data; |
1771 | 0 | } |
1772 | | |
1773 | | /* Compose an IPv6 Neighbor Discovery Neighbor Solicitation message. */ |
1774 | | void |
1775 | | compose_nd_ns(struct dp_packet *b, const struct eth_addr eth_src, |
1776 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst) |
1777 | 0 | { |
1778 | 0 | struct in6_addr sn_addr; |
1779 | 0 | struct eth_addr eth_dst; |
1780 | 0 | struct ovs_nd_msg *ns; |
1781 | 0 | struct ovs_nd_lla_opt *lla_opt; |
1782 | 0 | uint32_t icmp_csum; |
1783 | |
|
1784 | 0 | in6_addr_solicited_node(&sn_addr, ipv6_dst); |
1785 | 0 | ipv6_multicast_to_ethernet(ð_dst, &sn_addr); |
1786 | |
|
1787 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1788 | 0 | ns = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, &sn_addr, |
1789 | 0 | 0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN); |
1790 | |
|
1791 | 0 | ns->icmph.icmp6_type = ND_NEIGHBOR_SOLICIT; |
1792 | 0 | ns->icmph.icmp6_code = 0; |
1793 | 0 | put_16aligned_be32(&ns->rso_flags, htonl(0)); |
1794 | |
|
1795 | 0 | lla_opt = &ns->options[0]; |
1796 | 0 | lla_opt->type = ND_OPT_SOURCE_LINKADDR; |
1797 | 0 | lla_opt->len = 1; |
1798 | |
|
1799 | 0 | packet_set_nd(b, ipv6_dst, eth_src, eth_addr_zero); |
1800 | |
|
1801 | 0 | ns->icmph.icmp6_cksum = 0; |
1802 | 0 | icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1803 | 0 | ns->icmph.icmp6_cksum = csum_finish( |
1804 | 0 | csum_continue(icmp_csum, ns, ND_MSG_LEN + ND_LLA_OPT_LEN)); |
1805 | 0 | } |
1806 | | |
1807 | | /* Compose an IPv6 Neighbor Discovery Neighbor Advertisement message. */ |
1808 | | void |
1809 | | compose_nd_na(struct dp_packet *b, |
1810 | | const struct eth_addr eth_src, const struct eth_addr eth_dst, |
1811 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst, |
1812 | | ovs_be32 rso_flags) |
1813 | 0 | { |
1814 | 0 | struct ovs_nd_msg *na; |
1815 | 0 | struct ovs_nd_lla_opt *lla_opt; |
1816 | 0 | uint32_t icmp_csum; |
1817 | |
|
1818 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1819 | 0 | na = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, |
1820 | 0 | 0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN); |
1821 | |
|
1822 | 0 | na->icmph.icmp6_type = ND_NEIGHBOR_ADVERT; |
1823 | 0 | na->icmph.icmp6_code = 0; |
1824 | 0 | put_16aligned_be32(&na->rso_flags, rso_flags); |
1825 | |
|
1826 | 0 | lla_opt = &na->options[0]; |
1827 | 0 | lla_opt->type = ND_OPT_TARGET_LINKADDR; |
1828 | 0 | lla_opt->len = 1; |
1829 | |
|
1830 | 0 | packet_set_nd(b, ipv6_src, eth_addr_zero, eth_src); |
1831 | |
|
1832 | 0 | na->icmph.icmp6_cksum = 0; |
1833 | 0 | icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1834 | 0 | na->icmph.icmp6_cksum = csum_finish(csum_continue( |
1835 | 0 | icmp_csum, na, ND_MSG_LEN + ND_LLA_OPT_LEN)); |
1836 | 0 | } |
1837 | | |
1838 | | /* Compose an IPv6 Neighbor Discovery Router Advertisement message with |
1839 | | * Source Link-layer Address Option and MTU Option. |
1840 | | * Caller can call packet_put_ra_prefix_opt to append Prefix Information |
1841 | | * Options to composed messags in 'b'. */ |
1842 | | void |
1843 | | compose_nd_ra(struct dp_packet *b, |
1844 | | const struct eth_addr eth_src, const struct eth_addr eth_dst, |
1845 | | const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst, |
1846 | | uint8_t cur_hop_limit, uint8_t mo_flags, |
1847 | | ovs_be16 router_lt, ovs_be32 reachable_time, |
1848 | | ovs_be32 retrans_timer, uint32_t mtu) |
1849 | 0 | { |
1850 | | /* Don't compose Router Advertisement packet with MTU Option if mtu |
1851 | | * value is 0. */ |
1852 | 0 | bool with_mtu = mtu != 0; |
1853 | 0 | size_t mtu_opt_len = with_mtu ? ND_MTU_OPT_LEN : 0; |
1854 | |
|
1855 | 0 | eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); |
1856 | |
|
1857 | 0 | struct ovs_ra_msg *ra = compose_ipv6( |
1858 | 0 | b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, 0, 0, 255, |
1859 | 0 | RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len); |
1860 | 0 | ra->icmph.icmp6_type = ND_ROUTER_ADVERT; |
1861 | 0 | ra->icmph.icmp6_code = 0; |
1862 | 0 | ra->cur_hop_limit = cur_hop_limit; |
1863 | 0 | ra->mo_flags = mo_flags; |
1864 | 0 | ra->router_lifetime = router_lt; |
1865 | 0 | ra->reachable_time = reachable_time; |
1866 | 0 | ra->retrans_timer = retrans_timer; |
1867 | |
|
1868 | 0 | struct ovs_nd_lla_opt *lla_opt = ra->options; |
1869 | 0 | lla_opt->type = ND_OPT_SOURCE_LINKADDR; |
1870 | 0 | lla_opt->len = 1; |
1871 | 0 | lla_opt->mac = eth_src; |
1872 | |
|
1873 | 0 | if (with_mtu) { |
1874 | | /* ovs_nd_mtu_opt has the same size with ovs_nd_lla_opt. */ |
1875 | 0 | struct ovs_nd_mtu_opt *mtu_opt |
1876 | 0 | = (struct ovs_nd_mtu_opt *)(lla_opt + 1); |
1877 | 0 | mtu_opt->type = ND_OPT_MTU; |
1878 | 0 | mtu_opt->len = 1; |
1879 | 0 | mtu_opt->reserved = 0; |
1880 | 0 | put_16aligned_be32(&mtu_opt->mtu, htonl(mtu)); |
1881 | 0 | } |
1882 | |
|
1883 | 0 | ra->icmph.icmp6_cksum = 0; |
1884 | 0 | uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1885 | 0 | ra->icmph.icmp6_cksum = csum_finish(csum_continue( |
1886 | 0 | icmp_csum, ra, RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len)); |
1887 | 0 | } |
1888 | | |
1889 | | /* Append an IPv6 Neighbor Discovery Prefix Information option to a |
1890 | | * Router Advertisement message. */ |
1891 | | void |
1892 | | packet_put_ra_prefix_opt(struct dp_packet *b, |
1893 | | uint8_t plen, uint8_t la_flags, |
1894 | | ovs_be32 valid_lifetime, ovs_be32 preferred_lifetime, |
1895 | | const ovs_be128 prefix) |
1896 | 0 | { |
1897 | 0 | size_t prev_l4_size = dp_packet_l4_size(b); |
1898 | 0 | struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(b); |
1899 | 0 | nh->ip6_plen = htons(prev_l4_size + ND_PREFIX_OPT_LEN); |
1900 | |
|
1901 | 0 | struct ovs_nd_prefix_opt *prefix_opt = |
1902 | 0 | dp_packet_put_uninit(b, sizeof *prefix_opt); |
1903 | 0 | prefix_opt->type = ND_OPT_PREFIX_INFORMATION; |
1904 | 0 | prefix_opt->len = 4; |
1905 | 0 | prefix_opt->prefix_len = plen; |
1906 | 0 | prefix_opt->la_flags = la_flags; |
1907 | 0 | put_16aligned_be32(&prefix_opt->valid_lifetime, valid_lifetime); |
1908 | 0 | put_16aligned_be32(&prefix_opt->preferred_lifetime, preferred_lifetime); |
1909 | 0 | put_16aligned_be32(&prefix_opt->reserved, 0); |
1910 | 0 | memcpy(prefix_opt->prefix.be32, prefix.be32, sizeof(ovs_be32[4])); |
1911 | |
|
1912 | 0 | struct ovs_ra_msg *ra = dp_packet_l4(b); |
1913 | 0 | ra->icmph.icmp6_cksum = 0; |
1914 | 0 | uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); |
1915 | 0 | ra->icmph.icmp6_cksum = csum_finish(csum_continue( |
1916 | 0 | icmp_csum, ra, prev_l4_size + ND_PREFIX_OPT_LEN)); |
1917 | 0 | } |
1918 | | |
1919 | | uint32_t |
1920 | | packet_csum_pseudoheader(const struct ip_header *ip) |
1921 | 0 | { |
1922 | 0 | uint32_t partial = 0; |
1923 | |
|
1924 | 0 | partial = csum_add32(partial, get_16aligned_be32(&ip->ip_src)); |
1925 | 0 | partial = csum_add32(partial, get_16aligned_be32(&ip->ip_dst)); |
1926 | 0 | partial = csum_add16(partial, htons(ip->ip_proto)); |
1927 | 0 | partial = csum_add16(partial, htons(ntohs(ip->ip_tot_len) - |
1928 | 0 | IP_IHL(ip->ip_ihl_ver) * 4)); |
1929 | |
|
1930 | 0 | return partial; |
1931 | 0 | } |
1932 | | |
1933 | | #ifndef __CHECKER__ |
1934 | | uint32_t |
1935 | | packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *ip6) |
1936 | 0 | { |
1937 | 0 | uint32_t partial = 0; |
1938 | |
|
1939 | 0 | partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src); |
1940 | 0 | partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst); |
1941 | 0 | partial = csum_add16(partial, htons(ip6->ip6_nxt)); |
1942 | 0 | partial = csum_add16(partial, ip6->ip6_plen); |
1943 | |
|
1944 | 0 | return partial; |
1945 | 0 | } |
1946 | | |
1947 | | /* Calculate the IPv6 upper layer checksum according to RFC2460. We pass the |
1948 | | ip6_nxt and ip6_plen values, so it will also work if extension headers |
1949 | | are present. */ |
1950 | | ovs_be16 |
1951 | | packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *ip6, |
1952 | | const void *data, uint8_t l4_protocol, |
1953 | | uint16_t l4_size) |
1954 | 0 | { |
1955 | 0 | uint32_t partial = 0; |
1956 | |
|
1957 | 0 | partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src); |
1958 | 0 | partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst); |
1959 | 0 | partial = csum_add16(partial, htons(l4_protocol)); |
1960 | 0 | partial = csum_add16(partial, htons(l4_size)); |
1961 | |
|
1962 | 0 | partial = csum_continue(partial, data, l4_size); |
1963 | |
|
1964 | 0 | return csum_finish(partial); |
1965 | 0 | } |
1966 | | #endif |
1967 | | |
1968 | | void |
1969 | | IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6) |
1970 | 0 | { |
1971 | 0 | if (is_ipv6) { |
1972 | 0 | ovs_16aligned_be32 *ip6 = dp_packet_l3(pkt); |
1973 | |
|
1974 | 0 | put_16aligned_be32(ip6, get_16aligned_be32(ip6) | |
1975 | 0 | htonl(IP_ECN_CE << 20)); |
1976 | 0 | } else { |
1977 | 0 | struct ip_header *nh = dp_packet_l3(pkt); |
1978 | 0 | uint8_t tos = nh->ip_tos; |
1979 | |
|
1980 | 0 | tos |= IP_ECN_CE; |
1981 | 0 | if (nh->ip_tos != tos) { |
1982 | 0 | if (dp_packet_ip_checksum_valid(pkt)) { |
1983 | 0 | dp_packet_ip_checksum_set_partial(pkt); |
1984 | 0 | } else { |
1985 | 0 | nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_tos), |
1986 | 0 | htons((uint16_t) tos)); |
1987 | 0 | } |
1988 | |
|
1989 | 0 | nh->ip_tos = tos; |
1990 | 0 | } |
1991 | 0 | } |
1992 | 0 | } |
1993 | | |
1994 | | /* Set TCP checksum field in packet 'p' with complete checksum. |
1995 | | * The packet must have the L3 and L4 offsets. */ |
1996 | | void |
1997 | | packet_tcp_complete_csum(struct dp_packet *p, bool inner) |
1998 | 0 | { |
1999 | 0 | struct tcp_header *tcp; |
2000 | 0 | size_t tcp_sz; |
2001 | 0 | void *ip_hdr; |
2002 | |
|
2003 | 0 | if (inner) { |
2004 | 0 | tcp = dp_packet_inner_l4(p); |
2005 | 0 | ip_hdr = dp_packet_inner_l3(p); |
2006 | 0 | tcp_sz = dp_packet_inner_l4_size(p); |
2007 | 0 | } else { |
2008 | 0 | tcp = dp_packet_l4(p); |
2009 | 0 | ip_hdr = dp_packet_l3(p); |
2010 | 0 | tcp_sz = dp_packet_l4_size(p); |
2011 | 0 | } |
2012 | |
|
2013 | 0 | ovs_assert(tcp); |
2014 | 0 | ovs_assert(ip_hdr); |
2015 | |
|
2016 | 0 | tcp->tcp_csum = 0; |
2017 | 0 | if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) { |
2018 | 0 | struct ip_header *ip = ip_hdr; |
2019 | |
|
2020 | 0 | tcp->tcp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip), |
2021 | 0 | tcp, tcp_sz)); |
2022 | 0 | } else { |
2023 | 0 | struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr; |
2024 | |
|
2025 | 0 | tcp->tcp_csum = packet_csum_upperlayer6(ip6, tcp, ip6->ip6_nxt, |
2026 | 0 | tcp_sz); |
2027 | 0 | } |
2028 | |
|
2029 | 0 | if (inner) { |
2030 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2031 | 0 | } else { |
2032 | 0 | dp_packet_l4_checksum_set_good(p); |
2033 | 0 | } |
2034 | 0 | } |
2035 | | |
2036 | | /* Set UDP checksum field in packet 'p' with complete checksum. |
2037 | | * The packet must have the L3 and L4 offsets. */ |
2038 | | void |
2039 | | packet_udp_complete_csum(struct dp_packet *p, bool inner) |
2040 | 0 | { |
2041 | 0 | struct udp_header *udp; |
2042 | 0 | size_t udp_sz; |
2043 | 0 | void *ip_hdr; |
2044 | |
|
2045 | 0 | if (inner) { |
2046 | 0 | udp = dp_packet_inner_l4(p); |
2047 | 0 | ip_hdr = dp_packet_inner_l3(p); |
2048 | 0 | udp_sz = dp_packet_inner_l4_size(p); |
2049 | 0 | } else { |
2050 | 0 | udp = dp_packet_l4(p); |
2051 | 0 | ip_hdr = dp_packet_l3(p); |
2052 | 0 | udp_sz = dp_packet_l4_size(p); |
2053 | 0 | } |
2054 | |
|
2055 | 0 | ovs_assert(udp); |
2056 | 0 | ovs_assert(ip_hdr); |
2057 | | |
2058 | | /* Skip csum calculation if the udp_csum is zero. */ |
2059 | 0 | if (!udp->udp_csum) { |
2060 | 0 | goto out; |
2061 | 0 | } |
2062 | | |
2063 | 0 | udp->udp_csum = 0; |
2064 | 0 | if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) { |
2065 | 0 | struct ip_header *ip = ip_hdr; |
2066 | |
|
2067 | 0 | udp->udp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip), |
2068 | 0 | udp, udp_sz)); |
2069 | 0 | } else { |
2070 | 0 | struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr; |
2071 | |
|
2072 | 0 | udp->udp_csum = packet_csum_upperlayer6(ip6, udp, ip6->ip6_nxt, |
2073 | 0 | udp_sz); |
2074 | 0 | } |
2075 | |
|
2076 | 0 | if (!udp->udp_csum) { |
2077 | 0 | udp->udp_csum = htons(0xffff); |
2078 | 0 | } |
2079 | |
|
2080 | 0 | out: |
2081 | 0 | if (inner) { |
2082 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2083 | 0 | } else { |
2084 | 0 | dp_packet_l4_checksum_set_good(p); |
2085 | 0 | } |
2086 | 0 | } |
2087 | | |
2088 | | /* Set SCTP checksum field in packet 'p' with complete checksum. |
2089 | | * The packet must have the L3 and L4 offsets. */ |
2090 | | void |
2091 | | packet_sctp_complete_csum(struct dp_packet *p, bool inner) |
2092 | 0 | { |
2093 | 0 | struct sctp_header *sh; |
2094 | 0 | uint16_t tp_len; |
2095 | 0 | ovs_be32 csum; |
2096 | |
|
2097 | 0 | if (inner) { |
2098 | 0 | sh = dp_packet_inner_l4(p); |
2099 | 0 | tp_len = dp_packet_inner_l4_size(p); |
2100 | 0 | } else { |
2101 | 0 | sh = dp_packet_l4(p); |
2102 | 0 | tp_len = dp_packet_l4_size(p); |
2103 | 0 | } |
2104 | |
|
2105 | 0 | ovs_assert(sh); |
2106 | |
|
2107 | 0 | put_16aligned_be32(&sh->sctp_csum, 0); |
2108 | 0 | csum = crc32c((void *) sh, tp_len); |
2109 | 0 | put_16aligned_be32(&sh->sctp_csum, csum); |
2110 | |
|
2111 | 0 | if (inner) { |
2112 | 0 | dp_packet_inner_l4_checksum_set_good(p); |
2113 | 0 | } else { |
2114 | 0 | dp_packet_l4_checksum_set_good(p); |
2115 | 0 | } |
2116 | 0 | } |