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