]> git.proxmox.com Git - mirror_ovs.git/blame - lib/packets.c
dpctl: add examples to the manpage.
[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,
932c96b7
JR
963 ovs_16aligned_be32 addr[4],
964 const struct in6_addr *new_addr)
bc7a5acd 965{
cf62fa4c 966 size_t l4_size = dp_packet_l4_size(packet);
5a51b2cd
JR
967
968 if (proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
cf62fa4c 969 struct tcp_header *th = dp_packet_l4(packet);
bc7a5acd
AA
970
971 th->tcp_csum = recalc_csum128(th->tcp_csum, addr, new_addr);
5a51b2cd 972 } else if (proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
cf62fa4c 973 struct udp_header *uh = dp_packet_l4(packet);
bc7a5acd
AA
974
975 if (uh->udp_csum) {
976 uh->udp_csum = recalc_csum128(uh->udp_csum, addr, new_addr);
977 if (!uh->udp_csum) {
978 uh->udp_csum = htons(0xffff);
979 }
980 }
5abf65d0
JG
981 } else if (proto == IPPROTO_ICMPV6 &&
982 l4_size >= sizeof(struct icmp6_header)) {
cf62fa4c 983 struct icmp6_header *icmp = dp_packet_l4(packet);
00894212
JG
984
985 icmp->icmp6_cksum = recalc_csum128(icmp->icmp6_cksum, addr, new_addr);
bc7a5acd
AA
986 }
987}
988
0e29d884 989void
cf62fa4c 990packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto,
932c96b7
JR
991 ovs_16aligned_be32 addr[4],
992 const struct in6_addr *new_addr,
bc7a5acd
AA
993 bool recalculate_csum)
994{
995 if (recalculate_csum) {
4528f34f 996 packet_update_csum128(packet, proto, addr, new_addr);
bc7a5acd 997 }
4068403a 998 memcpy(addr, new_addr, sizeof(ovs_be32[4]));
bc7a5acd
AA
999}
1000
1001static void
4528f34f 1002packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key)
bc7a5acd 1003{
4528f34f
BP
1004 ovs_be32 old_label = get_16aligned_be32(flow_label);
1005 ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
1006 put_16aligned_be32(flow_label, new_label);
bc7a5acd
AA
1007}
1008
1009static void
4528f34f 1010packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc)
bc7a5acd 1011{
4528f34f
BP
1012 ovs_be32 old_label = get_16aligned_be32(flow_label);
1013 ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
1014 put_16aligned_be32(flow_label, new_label);
bc7a5acd
AA
1015}
1016
c97664b3
EJ
1017/* Modifies the IPv4 header fields of 'packet' to be consistent with 'src',
1018 * 'dst', 'tos', and 'ttl'. Updates 'packet''s L4 checksums as appropriate.
1019 * 'packet' must contain a valid IPv4 packet with correctly populated l[347]
1020 * markers. */
1021void
cf62fa4c 1022packet_set_ipv4(struct dp_packet *packet, ovs_be32 src, ovs_be32 dst,
c97664b3
EJ
1023 uint8_t tos, uint8_t ttl)
1024{
cf62fa4c 1025 struct ip_header *nh = dp_packet_l3(packet);
c97664b3 1026
7c457c33 1027 if (get_16aligned_be32(&nh->ip_src) != src) {
c97664b3
EJ
1028 packet_set_ipv4_addr(packet, &nh->ip_src, src);
1029 }
1030
7c457c33 1031 if (get_16aligned_be32(&nh->ip_dst) != dst) {
c97664b3
EJ
1032 packet_set_ipv4_addr(packet, &nh->ip_dst, dst);
1033 }
1034
1035 if (nh->ip_tos != tos) {
1036 uint8_t *field = &nh->ip_tos;
1037
1038 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t) *field),
1039 htons((uint16_t) tos));
1040 *field = tos;
1041 }
1042
1043 if (nh->ip_ttl != ttl) {
1044 uint8_t *field = &nh->ip_ttl;
1045
1046 nh->ip_csum = recalc_csum16(nh->ip_csum, htons(*field << 8),
1047 htons(ttl << 8));
1048 *field = ttl;
1049 }
1050}
1051
bc7a5acd
AA
1052/* Modifies the IPv6 header fields of 'packet' to be consistent with 'src',
1053 * 'dst', 'traffic class', and 'next hop'. Updates 'packet''s L4 checksums as
1054 * appropriate. 'packet' must contain a valid IPv6 packet with correctly
437d0d22 1055 * populated l[34] offsets. */
bc7a5acd 1056void
932c96b7
JR
1057packet_set_ipv6(struct dp_packet *packet, const struct in6_addr *src,
1058 const struct in6_addr *dst, uint8_t key_tc, ovs_be32 key_fl,
bc7a5acd
AA
1059 uint8_t key_hl)
1060{
cf62fa4c 1061 struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet);
31a9a584
SH
1062 uint8_t proto = 0;
1063 bool rh_present;
1064
1065 rh_present = packet_rh_present(packet, &proto);
bc7a5acd
AA
1066
1067 if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) {
4528f34f 1068 packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32, src, true);
bc7a5acd
AA
1069 }
1070
1071 if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) {
4528f34f 1072 packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst,
31a9a584 1073 !rh_present);
bc7a5acd
AA
1074 }
1075
1076 packet_set_ipv6_tc(&nh->ip6_flow, key_tc);
bc7a5acd 1077 packet_set_ipv6_flow_label(&nh->ip6_flow, key_fl);
bc7a5acd
AA
1078 nh->ip6_hlim = key_hl;
1079}
1080
c97664b3
EJ
1081static void
1082packet_set_port(ovs_be16 *port, ovs_be16 new_port, ovs_be16 *csum)
1083{
1084 if (*port != new_port) {
1085 *csum = recalc_csum16(*csum, *port, new_port);
1086 *port = new_port;
1087 }
1088}
1089
1090/* Sets the TCP source and destination port ('src' and 'dst' respectively) of
1091 * the TCP header contained in 'packet'. 'packet' must be a valid TCP packet
437d0d22 1092 * with its l4 offset properly populated. */
c97664b3 1093void
cf62fa4c 1094packet_set_tcp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
c97664b3 1095{
cf62fa4c 1096 struct tcp_header *th = dp_packet_l4(packet);
c97664b3
EJ
1097
1098 packet_set_port(&th->tcp_src, src, &th->tcp_csum);
1099 packet_set_port(&th->tcp_dst, dst, &th->tcp_csum);
1100}
1101
1102/* Sets the UDP source and destination port ('src' and 'dst' respectively) of
1103 * the UDP header contained in 'packet'. 'packet' must be a valid UDP packet
437d0d22 1104 * with its l4 offset properly populated. */
c97664b3 1105void
cf62fa4c 1106packet_set_udp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
c97664b3 1107{
cf62fa4c 1108 struct udp_header *uh = dp_packet_l4(packet);
c97664b3
EJ
1109
1110 if (uh->udp_csum) {
1111 packet_set_port(&uh->udp_src, src, &uh->udp_csum);
1112 packet_set_port(&uh->udp_dst, dst, &uh->udp_csum);
1113
1114 if (!uh->udp_csum) {
1115 uh->udp_csum = htons(0xffff);
1116 }
1117 } else {
1118 uh->udp_src = src;
1119 uh->udp_dst = dst;
1120 }
1121}
12113c39 1122
c6bcb685
JS
1123/* Sets the SCTP source and destination port ('src' and 'dst' respectively) of
1124 * the SCTP header contained in 'packet'. 'packet' must be a valid SCTP packet
437d0d22 1125 * with its l4 offset properly populated. */
c6bcb685 1126void
cf62fa4c 1127packet_set_sctp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
c6bcb685 1128{
cf62fa4c 1129 struct sctp_header *sh = dp_packet_l4(packet);
c6bcb685 1130 ovs_be32 old_csum, old_correct_csum, new_csum;
cf62fa4c 1131 uint16_t tp_len = dp_packet_l4_size(packet);
c6bcb685 1132
5fa008d4
BP
1133 old_csum = get_16aligned_be32(&sh->sctp_csum);
1134 put_16aligned_be32(&sh->sctp_csum, 0);
437d0d22 1135 old_correct_csum = crc32c((void *)sh, tp_len);
c6bcb685
JS
1136
1137 sh->sctp_src = src;
1138 sh->sctp_dst = dst;
1139
437d0d22 1140 new_csum = crc32c((void *)sh, tp_len);
5fa008d4 1141 put_16aligned_be32(&sh->sctp_csum, old_csum ^ old_correct_csum ^ new_csum);
c6bcb685
JS
1142}
1143
b8786b18
JP
1144/* Sets the ICMP type and code of the ICMP header contained in 'packet'.
1145 * 'packet' must be a valid ICMP packet with its l4 offset properly
1146 * populated. */
1147void
1148packet_set_icmp(struct dp_packet *packet, uint8_t type, uint8_t code)
1149{
1150 struct icmp_header *ih = dp_packet_l4(packet);
1151 ovs_be16 orig_tc = htons(ih->icmp_type << 8 | ih->icmp_code);
1152 ovs_be16 new_tc = htons(type << 8 | code);
1153
1154 if (orig_tc != new_tc) {
1155 ih->icmp_type = type;
1156 ih->icmp_code = code;
1157
1158 ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_tc, new_tc);
1159 }
1160}
1161
e60e935b 1162void
932c96b7 1163packet_set_nd(struct dp_packet *packet, const struct in6_addr *target,
c4bee4cb
PS
1164 const struct eth_addr sll, const struct eth_addr tll)
1165{
e60e935b
SRCSA
1166 struct ovs_nd_msg *ns;
1167 struct ovs_nd_opt *nd_opt;
cf62fa4c 1168 int bytes_remain = dp_packet_l4_size(packet);
e60e935b
SRCSA
1169
1170 if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) {
1171 return;
1172 }
1173
cf62fa4c 1174 ns = dp_packet_l4(packet);
e60e935b
SRCSA
1175 nd_opt = &ns->options[0];
1176 bytes_remain -= sizeof(*ns);
1177
1178 if (memcmp(&ns->target, target, sizeof(ovs_be32[4]))) {
932c96b7
JR
1179 packet_set_ipv6_addr(packet, IPPROTO_ICMPV6, ns->target.be32, target,
1180 true);
e60e935b
SRCSA
1181 }
1182
1183 while (bytes_remain >= ND_OPT_LEN && nd_opt->nd_opt_len != 0) {
1184 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
1185 && nd_opt->nd_opt_len == 1) {
74ff3298 1186 if (!eth_addr_equals(nd_opt->nd_opt_mac, sll)) {
e60e935b
SRCSA
1187 ovs_be16 *csum = &(ns->icmph.icmp6_cksum);
1188
74ff3298
JR
1189 *csum = recalc_csum48(*csum, nd_opt->nd_opt_mac, sll);
1190 nd_opt->nd_opt_mac = sll;
e60e935b
SRCSA
1191 }
1192
1193 /* A packet can only contain one SLL or TLL option */
1194 break;
1195 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
1196 && nd_opt->nd_opt_len == 1) {
74ff3298 1197 if (!eth_addr_equals(nd_opt->nd_opt_mac, tll)) {
e60e935b
SRCSA
1198 ovs_be16 *csum = &(ns->icmph.icmp6_cksum);
1199
74ff3298
JR
1200 *csum = recalc_csum48(*csum, nd_opt->nd_opt_mac, tll);
1201 nd_opt->nd_opt_mac = tll;
e60e935b
SRCSA
1202 }
1203
1204 /* A packet can only contain one SLL or TLL option */
1205 break;
1206 }
1207
1208 nd_opt += nd_opt->nd_opt_len;
1209 bytes_remain -= nd_opt->nd_opt_len * ND_OPT_LEN;
1210 }
1211}
1212
61bf6666
JR
1213const char *
1214packet_tcp_flag_to_string(uint32_t flag)
1215{
1216 switch (flag) {
1217 case TCP_FIN:
1218 return "fin";
1219 case TCP_SYN:
1220 return "syn";
1221 case TCP_RST:
1222 return "rst";
1223 case TCP_PSH:
1224 return "psh";
1225 case TCP_ACK:
1226 return "ack";
1227 case TCP_URG:
1228 return "urg";
1229 case TCP_ECE:
1230 return "ece";
1231 case TCP_CWR:
1232 return "cwr";
1233 case TCP_NS:
1234 return "ns";
1235 case 0x200:
1236 return "[200]";
1237 case 0x400:
1238 return "[400]";
1239 case 0x800:
1240 return "[800]";
1241 default:
1242 return NULL;
1243 }
1244}
1245
7393104d 1246/* Appends a string representation of the TCP flags value 'tcp_flags'
f41b5b3b 1247 * (e.g. from struct flow.tcp_flags or obtained via TCP_FLAGS) to 's', in the
7393104d
BP
1248 * format used by tcpdump. */
1249void
a66733a8 1250packet_format_tcp_flags(struct ds *s, uint16_t tcp_flags)
7393104d
BP
1251{
1252 if (!tcp_flags) {
1253 ds_put_cstr(s, "none");
1254 return;
1255 }
1256
1257 if (tcp_flags & TCP_SYN) {
1258 ds_put_char(s, 'S');
1259 }
1260 if (tcp_flags & TCP_FIN) {
1261 ds_put_char(s, 'F');
1262 }
1263 if (tcp_flags & TCP_PSH) {
1264 ds_put_char(s, 'P');
1265 }
1266 if (tcp_flags & TCP_RST) {
1267 ds_put_char(s, 'R');
1268 }
1269 if (tcp_flags & TCP_URG) {
1270 ds_put_char(s, 'U');
1271 }
1272 if (tcp_flags & TCP_ACK) {
1273 ds_put_char(s, '.');
1274 }
a66733a8
JR
1275 if (tcp_flags & TCP_ECE) {
1276 ds_put_cstr(s, "E");
7393104d 1277 }
a66733a8
JR
1278 if (tcp_flags & TCP_CWR) {
1279 ds_put_cstr(s, "C");
1280 }
1281 if (tcp_flags & TCP_NS) {
1282 ds_put_cstr(s, "N");
1283 }
1284 if (tcp_flags & 0x200) {
1285 ds_put_cstr(s, "[200]");
1286 }
1287 if (tcp_flags & 0x400) {
1288 ds_put_cstr(s, "[400]");
1289 }
1290 if (tcp_flags & 0x800) {
1291 ds_put_cstr(s, "[800]");
7393104d
BP
1292 }
1293}
a36de779
PS
1294
1295#define ARP_PACKET_SIZE (2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + \
1296 ARP_ETH_HEADER_LEN)
1297
eb0b295e
BP
1298/* Clears 'b' and replaces its contents by an ARP frame with the specified
1299 * 'arp_op', 'arp_sha', 'arp_tha', 'arp_spa', and 'arp_tpa'. The outer
1300 * Ethernet frame is initialized with Ethernet source 'arp_sha' and destination
1301 * 'arp_tha', except that destination ff:ff:ff:ff:ff:ff is used instead if
6335d074 1302 * 'broadcast' is true. Points the L3 header to the ARP header. */
a36de779 1303void
eb0b295e 1304compose_arp(struct dp_packet *b, uint16_t arp_op,
74ff3298
JR
1305 const struct eth_addr arp_sha, const struct eth_addr arp_tha,
1306 bool broadcast, ovs_be32 arp_spa, ovs_be32 arp_tpa)
a36de779 1307{
6335d074
BP
1308 compose_arp__(b);
1309
1310 struct eth_header *eth = dp_packet_l2(b);
1311 eth->eth_dst = broadcast ? eth_addr_broadcast : arp_tha;
1312 eth->eth_src = arp_sha;
1313
1314 struct arp_eth_header *arp = dp_packet_l3(b);
1315 arp->ar_op = htons(arp_op);
1316 arp->ar_sha = arp_sha;
1317 arp->ar_tha = arp_tha;
1318 put_16aligned_be32(&arp->ar_spa, arp_spa);
1319 put_16aligned_be32(&arp->ar_tpa, arp_tpa);
1320}
a36de779 1321
6335d074
BP
1322/* Clears 'b' and replaces its contents by an ARP frame. Sets the fields in
1323 * the Ethernet and ARP headers that are fixed for ARP frames to those fixed
1324 * values, and zeroes the other fields. Points the L3 header to the ARP
1325 * header. */
1326void
1327compose_arp__(struct dp_packet *b)
1328{
cf62fa4c
PS
1329 dp_packet_clear(b);
1330 dp_packet_prealloc_tailroom(b, ARP_PACKET_SIZE);
1331 dp_packet_reserve(b, 2 + VLAN_HEADER_LEN);
a36de779 1332
6335d074 1333 struct eth_header *eth = dp_packet_put_zeros(b, sizeof *eth);
a36de779
PS
1334 eth->eth_type = htons(ETH_TYPE_ARP);
1335
6335d074 1336 struct arp_eth_header *arp = dp_packet_put_zeros(b, sizeof *arp);
a36de779
PS
1337 arp->ar_hrd = htons(ARP_HRD_ETHERNET);
1338 arp->ar_pro = htons(ARP_PRO_IP);
1339 arp->ar_hln = sizeof arp->ar_sha;
1340 arp->ar_pln = sizeof arp->ar_spa;
a36de779 1341
82eb5b0a 1342 dp_packet_reset_offsets(b);
cf62fa4c 1343 dp_packet_set_l3(b, arp);
a36de779 1344}
0292a0c9 1345
16187903 1346/* This function expects packet with ethernet header with correct
c4bee4cb
PS
1347 * l3 pointer set. */
1348static void *
16187903
JP
1349compose_ipv6(struct dp_packet *packet, uint8_t proto,
1350 const struct in6_addr *src, const struct in6_addr *dst,
1351 uint8_t key_tc, ovs_be32 key_fl, uint8_t key_hl, int size)
c4bee4cb
PS
1352{
1353 struct ip6_hdr *nh;
1354 void *data;
1355
1356 nh = dp_packet_l3(packet);
1357 nh->ip6_vfc = 0x60;
1358 nh->ip6_nxt = proto;
1359 nh->ip6_plen = htons(size);
1360 data = dp_packet_put_zeros(packet, size);
1361 dp_packet_set_l4(packet, data);
932c96b7 1362 packet_set_ipv6(packet, src, dst, key_tc, key_fl, key_hl);
c4bee4cb
PS
1363 return data;
1364}
1365
16187903 1366/* Compose an IPv6 Neighbor Discovery Neighbor Solicitation message. */
c2b878e0 1367void
16187903
JP
1368compose_nd_ns(struct dp_packet *b, const struct eth_addr eth_src,
1369 const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst)
c2b878e0
TLSC
1370{
1371 struct in6_addr sn_addr;
1372 struct eth_addr eth_dst;
1373 struct ovs_nd_msg *ns;
1374 struct ovs_nd_opt *nd_opt;
c4bee4cb 1375 uint32_t icmp_csum;
c2b878e0
TLSC
1376
1377 in6_addr_solicited_node(&sn_addr, ipv6_dst);
1378 ipv6_multicast_to_ethernet(&eth_dst, &sn_addr);
1379
c4bee4cb 1380 eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
16187903
JP
1381 ns = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, &sn_addr,
1382 0, 0, 255, ND_MSG_LEN + ND_OPT_LEN);
c2b878e0
TLSC
1383
1384 ns->icmph.icmp6_type = ND_NEIGHBOR_SOLICIT;
1385 ns->icmph.icmp6_code = 0;
29d5e9a7 1386 put_16aligned_be32(&ns->rso_flags, htonl(0));
c2b878e0 1387
c4bee4cb 1388 nd_opt = &ns->options[0];
c2b878e0 1389 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
c4bee4cb
PS
1390 nd_opt->nd_opt_len = 1;
1391
932c96b7 1392 packet_set_nd(b, ipv6_dst, eth_src, eth_addr_zero);
16187903 1393
c4bee4cb
PS
1394 ns->icmph.icmp6_cksum = 0;
1395 icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
1396 ns->icmph.icmp6_cksum = csum_finish(csum_continue(icmp_csum, ns,
1397 ND_MSG_LEN + ND_OPT_LEN));
c2b878e0
TLSC
1398}
1399
16187903 1400/* Compose an IPv6 Neighbor Discovery Neighbor Advertisement message. */
e75451fe 1401void
16187903
JP
1402compose_nd_na(struct dp_packet *b,
1403 const struct eth_addr eth_src, const struct eth_addr eth_dst,
1404 const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst,
1405 ovs_be32 rso_flags)
e75451fe
ZKL
1406{
1407 struct ovs_nd_msg *na;
1408 struct ovs_nd_opt *nd_opt;
1409 uint32_t icmp_csum;
1410
1411 eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
16187903
JP
1412 na = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst,
1413 0, 0, 255, ND_MSG_LEN + ND_OPT_LEN);
e75451fe
ZKL
1414
1415 na->icmph.icmp6_type = ND_NEIGHBOR_ADVERT;
1416 na->icmph.icmp6_code = 0;
29d5e9a7 1417 put_16aligned_be32(&na->rso_flags, rso_flags);
e75451fe
ZKL
1418
1419 nd_opt = &na->options[0];
1420 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1421 nd_opt->nd_opt_len = 1;
1422
932c96b7 1423 packet_set_nd(b, ipv6_src, eth_addr_zero, eth_src);
16187903 1424
e75451fe
ZKL
1425 na->icmph.icmp6_cksum = 0;
1426 icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
1427 na->icmph.icmp6_cksum = csum_finish(csum_continue(icmp_csum, na,
1428 ND_MSG_LEN + ND_OPT_LEN));
1429}
1430
0292a0c9
JG
1431uint32_t
1432packet_csum_pseudoheader(const struct ip_header *ip)
1433{
1434 uint32_t partial = 0;
1435
1436 partial = csum_add32(partial, get_16aligned_be32(&ip->ip_src));
1437 partial = csum_add32(partial, get_16aligned_be32(&ip->ip_dst));
1438 partial = csum_add16(partial, htons(ip->ip_proto));
1439 partial = csum_add16(partial, htons(ntohs(ip->ip_tot_len) -
1440 IP_IHL(ip->ip_ihl_ver) * 4));
1441
1442 return partial;
1443}
07659514 1444
370e373b
TLSC
1445#ifndef __CHECKER__
1446uint32_t
1447packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *ip6)
1448{
1449 uint32_t partial = 0;
1450
cfa354cb
BP
1451 partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src);
1452 partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst);
c4bee4cb 1453 partial = csum_add16(partial, htons(ip6->ip6_nxt));
370e373b 1454 partial = csum_add16(partial, ip6->ip6_plen);
370e373b
TLSC
1455
1456 return partial;
1457}
46445c63
EC
1458
1459/* Calculate the IPv6 upper layer checksum according to RFC2460. We pass the
1460 ip6_nxt and ip6_plen values, so it will also work if extension headers
1461 are present. */
1462uint16_t
1463packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *ip6,
1464 const void *data, uint8_t l4_protocol,
1465 uint16_t l4_size)
1466{
1467 uint32_t partial = 0;
1468
1469 partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src);
1470 partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst);
1471 partial = csum_add16(partial, htons(l4_protocol));
1472 partial = csum_add16(partial, htons(l4_size));
1473
1474 partial = csum_continue(partial, data, l4_size);
1475
1476 return csum_finish(partial);
1477}
370e373b 1478#endif
1bc3f0ed
PS
1479
1480void
1481IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6)
1482{
1483 if (is_ipv6) {
1484 ovs_16aligned_be32 *ip6 = dp_packet_l3(pkt);
1485
1486 put_16aligned_be32(ip6, get_16aligned_be32(ip6) |
1487 htonl(IP_ECN_CE << 20));
1488 } else {
1489 struct ip_header *nh = dp_packet_l3(pkt);
1490 uint8_t tos = nh->ip_tos;
1491
1492 tos |= IP_ECN_CE;
1493 if (nh->ip_tos != tos) {
1494 nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_tos),
1495 htons((uint16_t) tos));
1496 nh->ip_tos = tos;
1497 }
1498 }
1499}