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