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