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