Coverage Report

Created: 2026-04-12 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/packets.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
#include "packets.h"
19
#include <sys/types.h>
20
#include <netinet/in.h>
21
#include <arpa/inet.h>
22
#include <sys/socket.h>
23
#include <netinet/ip6.h>
24
#include <netinet/icmp6.h>
25
#include <stdlib.h>
26
#include <netdb.h>
27
#include "byte-order.h"
28
#include "csum.h"
29
#include "crc32c.h"
30
#include "flow.h"
31
#include "openvswitch/hmap.h"
32
#include "openvswitch/dynamic-string.h"
33
#include "ovs-thread.h"
34
#include "odp-util.h"
35
#include "dp-packet.h"
36
#include "dp-packet-gso.h"
37
#include "unaligned.h"
38
39
const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT;
40
const struct in6_addr in6addr_all_hosts = IN6ADDR_ALL_HOSTS_INIT;
41
const struct in6_addr in6addr_all_routers = IN6ADDR_ALL_ROUTERS_INIT;
42
const struct in6_addr in6addr_v4mapped_any = IN6ADDR_V4MAPPED_ANY_INIT;
43
44
struct in6_addr
45
flow_tnl_dst(const struct flow_tnl *tnl)
46
0
{
47
0
    return tnl->ip_dst ? in6_addr_mapped_ipv4(tnl->ip_dst) : tnl->ipv6_dst;
48
0
}
49
50
struct in6_addr
51
flow_tnl_src(const struct flow_tnl *tnl)
52
0
{
53
0
    return tnl->ip_src ? in6_addr_mapped_ipv4(tnl->ip_src) : tnl->ipv6_src;
54
0
}
55
56
/* Returns true if 's' consists entirely of hex digits, false otherwise. */
57
static bool
58
is_all_hex(const char *s)
59
0
{
60
0
    return s[strspn(s, "0123456789abcdefABCDEF")] == '\0';
61
0
}
62
63
/* Parses 's' as a 16-digit hexadecimal number representing a datapath ID.  On
64
 * success stores the dpid into '*dpidp' and returns true, on failure stores 0
65
 * into '*dpidp' and returns false.
66
 *
67
 * Rejects an all-zeros dpid as invalid. */
68
bool
69
dpid_from_string(const char *s, uint64_t *dpidp)
70
0
{
71
0
    size_t len = strlen(s);
72
0
    *dpidp = ((len == 16 && is_all_hex(s))
73
0
              || (len <= 18 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')
74
0
                  && is_all_hex(s + 2))
75
0
              ? strtoull(s, NULL, 16)
76
0
              : 0);
77
0
    return *dpidp != 0;
78
0
}
79
80
uint64_t
81
eth_addr_to_uint64(const struct eth_addr ea)
82
0
{
83
0
    return (((uint64_t) ntohs(ea.be16[0]) << 32)
84
0
            | ((uint64_t) ntohs(ea.be16[1]) << 16)
85
0
            | ntohs(ea.be16[2]));
86
0
}
87
88
void
89
eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
90
0
{
91
0
    ea->be16[0] = htons(x >> 32);
92
0
    ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
93
0
    ea->be16[2] = htons(x & 0xFFFF);
94
0
}
95
96
void
97
eth_addr_mark_random(struct eth_addr *ea)
98
0
{
99
0
    ea->ea[0] &= ~1;                /* Unicast. */
100
0
    ea->ea[0] |= 2;                 /* Private. */
101
0
}
102
103
/* Returns true if 'ea' is a reserved address, that a bridge must never
104
 * forward, false otherwise.
105
 *
106
 * If you change this function's behavior, please update corresponding
107
 * documentation in vswitch.xml at the same time. */
108
bool
109
eth_addr_is_reserved(const struct eth_addr ea)
110
0
{
111
0
    struct eth_addr_node {
112
0
        struct hmap_node hmap_node;
113
0
        const uint64_t ea64;
114
0
    };
115
116
0
    static struct eth_addr_node nodes[] = {
117
        /* STP, IEEE pause frames, and other reserved protocols. */
118
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000000ULL },
119
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000001ULL },
120
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000002ULL },
121
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000003ULL },
122
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000004ULL },
123
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000005ULL },
124
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000006ULL },
125
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000007ULL },
126
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000008ULL },
127
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c2000009ULL },
128
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000aULL },
129
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000bULL },
130
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000cULL },
131
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000dULL },
132
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000eULL },
133
0
        { HMAP_NODE_NULL_INITIALIZER, 0x0180c200000fULL },
134
135
        /* Extreme protocols. */
136
0
        { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000000ULL }, /* EDP. */
137
0
        { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000004ULL }, /* EAPS. */
138
0
        { HMAP_NODE_NULL_INITIALIZER, 0x00e02b000006ULL }, /* EAPS. */
139
140
        /* Cisco protocols. */
141
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000c000000ULL }, /* ISL. */
142
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccccULL }, /* PAgP, UDLD, CDP,
143
                                                            * DTP, VTP. */
144
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000ccccccdULL }, /* PVST+. */
145
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000ccdcdcdULL }, /* STP Uplink Fast,
146
                                                            * FlexLink. */
147
148
        /* Cisco CFM. */
149
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc0ULL },
150
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc1ULL },
151
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc2ULL },
152
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc3ULL },
153
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc4ULL },
154
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc5ULL },
155
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc6ULL },
156
0
        { HMAP_NODE_NULL_INITIALIZER, 0x01000cccccc7ULL },
157
0
    };
158
159
0
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
160
0
    struct eth_addr_node *node;
161
0
    static struct hmap addrs;
162
0
    uint64_t ea64;
163
164
0
    if (ovsthread_once_start(&once)) {
165
0
        hmap_init(&addrs);
166
0
        for (node = nodes; node < &nodes[ARRAY_SIZE(nodes)]; node++) {
167
0
            hmap_insert(&addrs, &node->hmap_node, hash_uint64(node->ea64));
168
0
        }
169
0
        ovsthread_once_done(&once);
170
0
    }
171
172
0
    ea64 = eth_addr_to_uint64(ea);
173
0
    HMAP_FOR_EACH_IN_BUCKET (node, hmap_node, hash_uint64(ea64), &addrs) {
174
0
        if (node->ea64 == ea64) {
175
0
            return true;
176
0
        }
177
0
    }
178
0
    return false;
179
0
}
180
181
/* Attempts to parse 's' as an Ethernet address.  If successful, stores the
182
 * address in 'ea' and returns true, otherwise zeros 'ea' and returns
183
 * false.  This function checks trailing characters. */
184
bool
185
eth_addr_from_string(const char *s, struct eth_addr *ea)
186
0
{
187
0
    int n = 0;
188
0
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*ea), &n)
189
0
        && !s[n]) {
190
0
        return true;
191
0
    } else {
192
0
        *ea = eth_addr_zero;
193
0
        return false;
194
0
    }
195
0
}
196
197
/* Fills 'b' with a Reverse ARP packet with Ethernet source address 'eth_src'.
198
 * This function is used by Open vSwitch to compose packets in cases where
199
 * context is important but content doesn't (or shouldn't) matter.
200
 *
201
 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
202
 * desired. */
203
void
204
compose_rarp(struct dp_packet *b, const struct eth_addr eth_src)
205
0
{
206
0
    struct eth_header *eth;
207
0
    struct arp_eth_header *arp;
208
209
0
    dp_packet_clear(b);
210
0
    dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN
211
0
                             + ARP_ETH_HEADER_LEN);
212
0
    dp_packet_reserve(b, 2 + VLAN_HEADER_LEN);
213
0
    eth = dp_packet_put_uninit(b, sizeof *eth);
214
0
    eth->eth_dst = eth_addr_broadcast;
215
0
    eth->eth_src = eth_src;
216
0
    eth->eth_type = htons(ETH_TYPE_RARP);
217
218
0
    arp = dp_packet_put_uninit(b, sizeof *arp);
219
0
    arp->ar_hrd = htons(ARP_HRD_ETHERNET);
220
0
    arp->ar_pro = htons(ARP_PRO_IP);
221
0
    arp->ar_hln = sizeof arp->ar_sha;
222
0
    arp->ar_pln = sizeof arp->ar_spa;
223
0
    arp->ar_op = htons(ARP_OP_RARP);
224
0
    arp->ar_sha = eth_src;
225
0
    put_16aligned_be32(&arp->ar_spa, htonl(0));
226
0
    arp->ar_tha = eth_src;
227
0
    put_16aligned_be32(&arp->ar_tpa, htonl(0));
228
229
0
    dp_packet_set_l3(b, arp);
230
0
    b->packet_type = htonl(PT_ETH);
231
0
}
232
233
/* Insert VLAN header according to given TCI. Packet passed must be Ethernet
234
 * packet.  Ignores the CFI bit of 'tci' using 0 instead.
235
 *
236
 * Also adjusts the layer offsets accordingly. */
237
void
238
eth_push_vlan(struct dp_packet *packet, ovs_be16 tpid, ovs_be16 tci)
239
0
{
240
0
    struct vlan_eth_header *veh;
241
242
    /* Insert new 802.1Q header. */
243
0
    veh = dp_packet_resize_l2(packet, VLAN_HEADER_LEN);
244
0
    memmove(veh, (char *)veh + VLAN_HEADER_LEN, 2 * ETH_ADDR_LEN);
245
0
    veh->veth_type = tpid;
246
0
    veh->veth_tci = tci & htons(~VLAN_CFI);
247
0
}
248
249
/* Removes outermost VLAN header (if any is present) from 'packet'.
250
 *
251
 * 'packet->l2_5' should initially point to 'packet''s outer-most VLAN header
252
 * or may be NULL if there are no VLAN headers. */
253
void
254
eth_pop_vlan(struct dp_packet *packet)
255
0
{
256
0
    struct vlan_eth_header *veh = dp_packet_eth(packet);
257
258
0
    if (veh && dp_packet_size(packet) >= sizeof *veh
259
0
        && eth_type_vlan(veh->veth_type)) {
260
261
0
        memmove((char *)veh + VLAN_HEADER_LEN, veh, 2 * ETH_ADDR_LEN);
262
0
        dp_packet_resize_l2(packet, -VLAN_HEADER_LEN);
263
0
    }
264
0
}
265
266
/* Push Ethernet header onto 'packet' assuming it is layer 3 */
267
void
268
push_eth(struct dp_packet *packet, const struct eth_addr *dst,
269
         const struct eth_addr *src)
270
0
{
271
0
    struct eth_header *eh;
272
273
0
    ovs_assert(!dp_packet_is_eth(packet));
274
0
    eh = dp_packet_resize_l2(packet, ETH_HEADER_LEN);
275
0
    eh->eth_dst = *dst;
276
0
    eh->eth_src = *src;
277
0
    eh->eth_type = pt_ns_type_be(packet->packet_type);
278
0
    packet->packet_type = htonl(PT_ETH);
279
0
}
280
281
/* Removes Ethernet header, including VLAN header, from 'packet'.
282
 *
283
 * Previous to calling this function, 'ofpbuf_l3(packet)' must not be NULL */
284
void
285
pop_eth(struct dp_packet *packet)
286
0
{
287
0
    char *l2_5 = dp_packet_l2_5(packet);
288
0
    char *l3 = dp_packet_l3(packet);
289
0
    ovs_be16 ethertype;
290
0
    int increment;
291
292
0
    ovs_assert(dp_packet_is_eth(packet));
293
0
    ovs_assert(l3 != NULL);
294
295
0
    if (l2_5) {
296
0
        increment = packet->l2_5_ofs;
297
0
        ethertype = *(ALIGNED_CAST(ovs_be16 *, (l2_5 - 2)));
298
0
    } else {
299
0
        increment = packet->l3_ofs;
300
0
        ethertype = *(ALIGNED_CAST(ovs_be16 *, (l3 - 2)));
301
0
    }
302
303
0
    dp_packet_resize_l2(packet, -increment);
304
0
    packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE, ntohs(ethertype));
305
0
}
306
307
/* Set ethertype of the packet. */
308
static void
309
set_ethertype(struct dp_packet *packet, ovs_be16 eth_type)
310
0
{
311
0
    struct eth_header *eh = dp_packet_eth(packet);
312
313
0
    if (!eh) {
314
0
        return;
315
0
    }
316
317
0
    if (eth_type_vlan(eh->eth_type)) {
318
0
        ovs_be16 *p;
319
0
        char *l2_5 = dp_packet_l2_5(packet);
320
321
0
        p = ALIGNED_CAST(ovs_be16 *,
322
0
                         (l2_5 ? l2_5 : (char *)dp_packet_l3(packet)) - 2);
323
0
        *p = eth_type;
324
0
    } else {
325
0
        eh->eth_type = eth_type;
326
0
    }
327
0
}
328
329
static bool is_mpls(struct dp_packet *packet)
330
0
{
331
0
    return packet->l2_5_ofs != UINT16_MAX;
332
0
}
333
334
/* Set time to live (TTL) of an MPLS label stack entry (LSE). */
335
void
336
set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl)
337
146
{
338
146
    *lse &= ~htonl(MPLS_TTL_MASK);
339
146
    *lse |= htonl((ttl << MPLS_TTL_SHIFT) & MPLS_TTL_MASK);
340
146
}
341
342
/* Set traffic class (TC) of an MPLS label stack entry (LSE). */
343
void
344
set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc)
345
381
{
346
381
    *lse &= ~htonl(MPLS_TC_MASK);
347
381
    *lse |= htonl((tc << MPLS_TC_SHIFT) & MPLS_TC_MASK);
348
381
}
349
350
/* Set label of an MPLS label stack entry (LSE). */
351
void
352
set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label)
353
481
{
354
481
    *lse &= ~htonl(MPLS_LABEL_MASK);
355
481
    *lse |= htonl((ntohl(label) << MPLS_LABEL_SHIFT) & MPLS_LABEL_MASK);
356
481
}
357
358
/* Set bottom of stack (BoS) bit of an MPLS label stack entry (LSE). */
359
void
360
set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos)
361
200
{
362
200
    *lse &= ~htonl(MPLS_BOS_MASK);
363
200
    *lse |= htonl((bos << MPLS_BOS_SHIFT) & MPLS_BOS_MASK);
364
200
}
365
366
/* Compose an MPLS label stack entry (LSE) from its components:
367
 * label, traffic class (TC), time to live (TTL) and
368
 * bottom of stack (BoS) bit. */
369
ovs_be32
370
set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos, ovs_be32 label)
371
0
{
372
0
    ovs_be32 lse = htonl(0);
373
0
    set_mpls_lse_ttl(&lse, ttl);
374
0
    set_mpls_lse_tc(&lse, tc);
375
0
    set_mpls_lse_bos(&lse, bos);
376
0
    set_mpls_lse_label(&lse, label);
377
0
    return lse;
378
0
}
379
380
/* Set MPLS label stack entry to outermost MPLS header.*/
381
void
382
set_mpls_lse(struct dp_packet *packet, ovs_be32 mpls_lse)
383
0
{
384
    /* Packet type should be MPLS to set label stack entry. */
385
0
    if (is_mpls(packet)) {
386
0
        struct mpls_hdr *mh = dp_packet_l2_5(packet);
387
388
        /* Update mpls label stack entry. */
389
0
        put_16aligned_be32(&mh->mpls_lse, mpls_lse);
390
0
    }
391
0
}
392
393
/* Push MPLS label stack entry 'lse' onto 'packet' as the outermost MPLS
394
 * header.  If 'packet' does not already have any MPLS labels, then its
395
 * Ethertype is changed to 'ethtype' (which must be an MPLS Ethertype). */
396
void
397
push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse)
398
0
{
399
0
    char * header;
400
0
    size_t len;
401
402
0
    if (!eth_type_mpls(ethtype)) {
403
0
        return;
404
0
    }
405
406
0
    if (!is_mpls(packet)) {
407
        /* Set MPLS label stack offset. */
408
0
        packet->l2_5_ofs = packet->l3_ofs;
409
0
    }
410
411
0
    set_ethertype(packet, ethtype);
412
413
    /* Push new MPLS shim header onto packet. */
414
0
    len = packet->l2_5_ofs;
415
0
    header = dp_packet_resize_l2_5(packet, MPLS_HLEN);
416
0
    memmove(header, header + MPLS_HLEN, len);
417
0
    memcpy(header + len, &lse, sizeof lse);
418
419
0
    pkt_metadata_init_conn(&packet->md);
420
0
}
421
422
void
423
add_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse,
424
         bool l3_encap)
425
0
{
426
0
    if (!eth_type_mpls(ethtype)) {
427
0
        return;
428
0
    }
429
430
0
    if (!l3_encap) {
431
0
        struct mpls_hdr *header = dp_packet_resize_l2(packet, MPLS_HLEN);
432
433
0
        put_16aligned_be32(&header->mpls_lse, lse);
434
0
        packet->l2_5_ofs = 0;
435
0
        packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
436
0
                                             ntohs(ethtype));
437
0
    } else {
438
0
        size_t len;
439
0
        char *header;
440
441
0
        if (!is_mpls(packet)) {
442
            /* Set MPLS label stack offset. */
443
0
            packet->l2_5_ofs = packet->l3_ofs;
444
0
        }
445
0
        set_ethertype(packet, ethtype);
446
447
        /* Push new MPLS shim header onto packet. */
448
0
        len = packet->l2_5_ofs;
449
0
        header = dp_packet_resize_l2_5(packet, MPLS_HLEN);
450
0
        memmove(header, header + MPLS_HLEN, len);
451
0
        memcpy(header + len, &lse, sizeof lse);
452
0
    }
453
0
    pkt_metadata_init_conn(&packet->md);
454
0
}
455
456
/* If 'packet' is an MPLS packet, removes its outermost MPLS label stack entry.
457
 * If the label that was removed was the only MPLS label, changes 'packet''s
458
 * Ethertype to 'ethtype' (which ordinarily should not be an MPLS
459
 * Ethertype). */
460
void
461
pop_mpls(struct dp_packet *packet, ovs_be16 ethtype)
462
0
{
463
0
    if (is_mpls(packet)) {
464
0
        struct mpls_hdr *mh = dp_packet_l2_5(packet);
465
0
        size_t len = packet->l2_5_ofs;
466
467
0
        set_ethertype(packet, ethtype);
468
0
        if (get_16aligned_be32(&mh->mpls_lse) & htonl(MPLS_BOS_MASK)) {
469
0
            dp_packet_set_l2_5(packet, NULL);
470
0
        }
471
        /* Shift the l2 header forward. */
472
0
        memmove((char*)dp_packet_data(packet) + MPLS_HLEN, dp_packet_data(packet), len);
473
0
        dp_packet_resize_l2_5(packet, -MPLS_HLEN);
474
475
        /* Invalidate offload flags as they are not valid after
476
         * decapsulation of MPLS header. */
477
0
        dp_packet_reset_offload(packet);
478
479
        /* packet_type must be reset for the MPLS packets with no l2 header */
480
0
        if (!len) {
481
0
            if (ethtype == htons(ETH_TYPE_TEB)) {
482
                /* The inner packet must be classified as ethernet if the
483
                 * ethtype is ETH_TYPE_TEB. */
484
0
                packet->packet_type = htonl(PT_ETH);
485
0
            } else {
486
0
                packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
487
0
                                                     ntohs(ethtype));
488
0
            }
489
0
        }
490
0
    }
491
0
}
492
493
void
494
push_nsh(struct dp_packet *packet, const struct nsh_hdr *nsh_hdr_src)
495
0
{
496
0
    struct nsh_hdr *nsh;
497
0
    size_t length = nsh_hdr_len(nsh_hdr_src);
498
0
    uint8_t next_proto;
499
500
0
    switch (ntohl(packet->packet_type)) {
501
0
        case PT_ETH:
502
0
            next_proto = NSH_P_ETHERNET;
503
0
            break;
504
0
        case PT_IPV4:
505
0
            next_proto = NSH_P_IPV4;
506
0
            break;
507
0
        case PT_IPV6:
508
0
            next_proto = NSH_P_IPV6;
509
0
            break;
510
0
        case PT_NSH:
511
0
            next_proto = NSH_P_NSH;
512
0
            break;
513
0
        default:
514
0
            OVS_NOT_REACHED();
515
0
    }
516
517
0
    nsh = (struct nsh_hdr *) dp_packet_resize_l2(packet, length);
518
0
    memcpy(nsh, nsh_hdr_src, length);
519
0
    nsh->next_proto = next_proto;
520
0
    packet->packet_type = htonl(PT_NSH);
521
0
    dp_packet_reset_offsets(packet);
522
0
    packet->l3_ofs = 0;
523
0
}
524
525
bool
526
pop_nsh(struct dp_packet *packet)
527
0
{
528
0
    struct nsh_hdr *nsh = (struct nsh_hdr *) dp_packet_l3(packet);
529
0
    size_t length;
530
0
    uint32_t next_pt;
531
532
0
    if (packet->packet_type == htonl(PT_NSH) && nsh) {
533
0
        switch (nsh->next_proto) {
534
0
            case NSH_P_ETHERNET:
535
0
                next_pt = PT_ETH;
536
0
                break;
537
0
            case NSH_P_IPV4:
538
0
                next_pt = PT_IPV4;
539
0
                break;
540
0
            case NSH_P_IPV6:
541
0
                next_pt = PT_IPV6;
542
0
                break;
543
0
            case NSH_P_NSH:
544
0
                next_pt = PT_NSH;
545
0
                break;
546
0
            default:
547
                /* Unknown inner packet type. Drop packet. */
548
0
                return false;
549
0
        }
550
551
0
        length = nsh_hdr_len(nsh);
552
0
        dp_packet_reset_packet(packet, length);
553
0
        packet->packet_type = htonl(next_pt);
554
        /* Packet must be recirculated for further processing. */
555
0
    }
556
0
    return true;
557
0
}
558
559
/* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'.  The
560
 * caller must free '*packetp'.  On success, returns NULL.  On failure, returns
561
 * an error message and stores NULL in '*packetp'.
562
 *
563
 * Aligns the L3 header of '*packetp' on a 32-bit boundary. */
564
const char *
565
eth_from_hex(const char *hex, struct dp_packet **packetp)
566
0
{
567
0
    struct dp_packet *packet;
568
569
    /* Use 2 bytes of headroom to 32-bit align the L3 header. */
570
0
    packet = *packetp = dp_packet_new_with_headroom(strlen(hex) / 2, 2);
571
572
0
    if (dp_packet_put_hex(packet, hex, NULL)[0] != '\0') {
573
0
        dp_packet_delete(packet);
574
0
        *packetp = NULL;
575
0
        return "Trailing garbage in packet data";
576
0
    }
577
578
0
    if (dp_packet_size(packet) < ETH_HEADER_LEN) {
579
0
        dp_packet_delete(packet);
580
0
        *packetp = NULL;
581
0
        return "Packet data too short for Ethernet";
582
0
    }
583
584
0
    return NULL;
585
0
}
586
587
void
588
eth_format_masked(const struct eth_addr eth,
589
                  const struct eth_addr *mask, struct ds *s)
590
119k
{
591
119k
    ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth));
592
119k
    if (mask && !eth_mask_is_exact(*mask)) {
593
3.08k
        ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(*mask));
594
3.08k
    }
595
119k
}
596
597
void
598
in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
599
0
{
600
0
    union ovs_16aligned_in6_addr *taddr =
601
0
        (union ovs_16aligned_in6_addr *) addr;
602
0
    memset(taddr->be16, 0, sizeof(taddr->be16));
603
0
    taddr->be16[0] = htons(0xff02);
604
0
    taddr->be16[5] = htons(0x1);
605
0
    taddr->be16[6] = htons(0xff00);
606
0
    memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
607
0
}
608
609
/*
610
 * Generates ipv6 EUI64 address from the given eth addr
611
 * and prefix and stores it in 'lla'
612
 */
613
void
614
in6_generate_eui64(struct eth_addr ea, const struct in6_addr *prefix,
615
                   struct in6_addr *lla)
616
0
{
617
0
    union ovs_16aligned_in6_addr *taddr =
618
0
        (union ovs_16aligned_in6_addr *) lla;
619
0
    union ovs_16aligned_in6_addr *prefix_taddr =
620
0
        (union ovs_16aligned_in6_addr *) prefix;
621
0
    taddr->be16[0] = prefix_taddr->be16[0];
622
0
    taddr->be16[1] = prefix_taddr->be16[1];
623
0
    taddr->be16[2] = prefix_taddr->be16[2];
624
0
    taddr->be16[3] = prefix_taddr->be16[3];
625
0
    taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
626
0
    taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
627
0
    taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
628
0
    taddr->be16[7] = ea.be16[2];
629
0
}
630
631
/* Generates ipv6 link local address from the given eth addr
632
 * with prefix 'fe80::/64' and stores it in 'lla'. */
633
void
634
in6_generate_lla(struct eth_addr ea, struct in6_addr *lla)
635
0
{
636
0
    union ovs_16aligned_in6_addr *taddr =
637
0
        (union ovs_16aligned_in6_addr *) lla;
638
0
    memset(taddr->be16, 0, sizeof(taddr->be16));
639
0
    taddr->be16[0] = htons(0xfe80);
640
0
    taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
641
0
    taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
642
0
    taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
643
0
    taddr->be16[7] = ea.be16[2];
644
0
}
645
646
/* Returns true if 'addr' is a link local address.  Otherwise, false. */
647
bool
648
in6_is_lla(struct in6_addr *addr)
649
0
{
650
0
#ifdef s6_addr32
651
0
    return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]);
652
#else
653
    return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 &&
654
         !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] |
655
           addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]);
656
#endif
657
0
}
658
659
void
660
ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
661
0
{
662
0
    eth->ea[0] = 0x33;
663
0
    eth->ea[1] = 0x33;
664
0
    eth->ea[2] = ip6->s6_addr[12];
665
0
    eth->ea[3] = ip6->s6_addr[13];
666
0
    eth->ea[4] = ip6->s6_addr[14];
667
0
    eth->ea[5] = ip6->s6_addr[15];
668
0
}
669
670
/* Given the IP netmask 'netmask', returns the number of bits of the IP address
671
 * that it specifies, that is, the number of 1-bits in 'netmask'.
672
 *
673
 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
674
 * still be in the valid range but isn't otherwise meaningful. */
675
int
676
ip_count_cidr_bits(ovs_be32 netmask)
677
5.77k
{
678
5.77k
    return 32 - ctz32(ntohl(netmask));
679
5.77k
}
680
681
void
682
ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
683
16.7k
{
684
16.7k
    ds_put_format(s, IP_FMT, IP_ARGS(ip));
685
16.7k
    if (mask != OVS_BE32_MAX) {
686
6.55k
        if (ip_is_cidr(mask)) {
687
5.77k
            ds_put_format(s, "/%d", ip_count_cidr_bits(mask));
688
5.77k
        } else {
689
783
            ds_put_format(s, "/"IP_FMT, IP_ARGS(mask));
690
783
        }
691
6.55k
    }
692
16.7k
}
693
694
/* Parses string 's', which must be an IP address.  Stores the IP address into
695
 * '*ip'.  Returns true if successful, otherwise false. */
696
bool
697
ip_parse(const char *s, ovs_be32 *ip)
698
0
{
699
0
    return inet_pton(AF_INET, s, ip) == 1;
700
0
}
701
702
/* Parses string 's', which must be an IP address with a port number
703
 * with ":" as a separator (e.g.: 192.168.1.2:80).
704
 * Stores the IP address into '*ip' and port number to '*port'.
705
 *
706
 * Returns NULL if successful, otherwise an error message that the caller must
707
 * free(). */
708
char * OVS_WARN_UNUSED_RESULT
709
ip_parse_port(const char *s, ovs_be32 *ip, ovs_be16 *port)
710
0
{
711
0
    int n = 0;
712
0
    if (ovs_scan(s, IP_PORT_SCAN_FMT"%n", IP_PORT_SCAN_ARGS(ip, port), &n)
713
0
        && !s[n]) {
714
0
        return NULL;
715
0
    }
716
717
0
    return xasprintf("%s: invalid IP address or port number", s);
718
0
}
719
720
/* Parses string 's', which must be an IP address with an optional netmask or
721
 * CIDR prefix length.  Stores the IP address into '*ip', netmask into '*mask',
722
 * (255.255.255.255, if 's' lacks a netmask), and number of scanned characters
723
 * into '*n'.
724
 *
725
 * Returns NULL if successful, otherwise an error message that the caller must
726
 * free(). */
727
char * OVS_WARN_UNUSED_RESULT
728
ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip,
729
                    ovs_be32 *mask)
730
0
{
731
0
    int prefix;
732
733
0
    if (ovs_scan_len(s, n, IP_SCAN_FMT"/"IP_SCAN_FMT,
734
0
                 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
735
        /* OK. */
736
0
    } else if (ovs_scan_len(s, n, IP_SCAN_FMT"/%d",
737
0
                            IP_SCAN_ARGS(ip), &prefix)) {
738
0
        if (prefix < 0 || prefix > 32) {
739
0
            return xasprintf("%s: IPv4 network prefix bits not between 0 and "
740
0
                              "32, inclusive", s);
741
0
        }
742
0
        *mask = be32_prefix_mask(prefix);
743
0
    } else if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
744
0
        *mask = OVS_BE32_MAX;
745
0
    } else {
746
0
        return xasprintf("%s: invalid IP address", s);
747
0
    }
748
0
    return NULL;
749
0
}
750
751
/* This function is similar to ip_parse_masked_len(), but doesn't return the
752
 * number of scanned characters and expects 's' to end after the ip/(optional)
753
 * mask.
754
 *
755
 * Returns NULL if successful, otherwise an error message that the caller must
756
 * free(). */
757
char * OVS_WARN_UNUSED_RESULT
758
ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
759
0
{
760
0
    int n = 0;
761
762
0
    char *error = ip_parse_masked_len(s, &n, ip, mask);
763
0
    if (!error && s[n]) {
764
0
        return xasprintf("%s: invalid IP address", s);
765
0
    }
766
0
    return error;
767
0
}
768
769
/* Similar to ip_parse_masked_len(), but the mask, if present, must be a CIDR
770
 * mask and is returned as a prefix len in '*plen'. */
771
char * OVS_WARN_UNUSED_RESULT
772
ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip, unsigned int *plen)
773
0
{
774
0
    ovs_be32 mask;
775
0
    char *error;
776
777
0
    error = ip_parse_masked_len(s, n, ip, &mask);
778
0
    if (error) {
779
0
        return error;
780
0
    }
781
782
0
    if (!ip_is_cidr(mask)) {
783
0
        return xasprintf("%s: CIDR network required", s);
784
0
    }
785
0
    *plen = ip_count_cidr_bits(mask);
786
0
    return NULL;
787
0
}
788
789
/* Similar to ip_parse_cidr_len(), but doesn't return the number of scanned
790
 * characters and expects 's' to be NULL terminated at the end of the
791
 * ip/(optional) cidr. */
792
char * OVS_WARN_UNUSED_RESULT
793
ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
794
0
{
795
0
    int n = 0;
796
797
0
    char *error = ip_parse_cidr_len(s, &n, ip, plen);
798
0
    if (!error && s[n]) {
799
0
        return xasprintf("%s: invalid IP address", s);
800
0
    }
801
0
    return error;
802
0
}
803
804
/* Parses string 's', which must be an IPv6 address.  Stores the IPv6 address
805
 * into '*ip'.  Returns true if successful, otherwise false. */
806
bool
807
ipv6_parse(const char *s, struct in6_addr *ip)
808
0
{
809
0
    return inet_pton(AF_INET6, s, ip) == 1;
810
0
}
811
812
/* Parses string 's', which must be an IPv6 address with an optional netmask or
813
 * CIDR prefix length.  Stores the IPv6 address into '*ip' and the netmask into
814
 * '*mask' (if 's' does not contain a netmask, all-one-bits is assumed), and
815
 * number of scanned characters into '*n'.
816
 *
817
 * Returns NULL if successful, otherwise an error message that the caller must
818
 * free(). */
819
char * OVS_WARN_UNUSED_RESULT
820
ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ip,
821
                      struct in6_addr *mask)
822
0
{
823
0
    char ipv6_s[IPV6_SCAN_LEN + 1];
824
0
    int prefix;
825
826
0
    if (ovs_scan_len(s, n, " "IPV6_SCAN_FMT, ipv6_s)
827
0
        && ipv6_parse(ipv6_s, ip)) {
828
0
        if (ovs_scan_len(s, n, "/%d", &prefix)) {
829
0
            if (prefix < 0 || prefix > 128) {
830
0
                return xasprintf("%s: IPv6 network prefix bits not between 0 "
831
0
                                 "and 128, inclusive", s);
832
0
            }
833
0
            *mask = ipv6_create_mask(prefix);
834
0
        } else if (ovs_scan_len(s, n, "/"IPV6_SCAN_FMT, ipv6_s)) {
835
0
             if (!ipv6_parse(ipv6_s, mask)) {
836
0
                 return xasprintf("%s: Invalid IPv6 mask", s);
837
0
             }
838
            /* OK. */
839
0
        } else {
840
            /* OK. No mask. */
841
0
            *mask = in6addr_exact;
842
0
        }
843
0
        return NULL;
844
0
    }
845
0
    return xasprintf("%s: invalid IPv6 address", s);
846
0
}
847
848
/* This function is similar to ipv6_parse_masked_len(), but doesn't return the
849
 * number of scanned characters and expects 's' to end following the
850
 * ipv6/(optional) mask. */
851
char * OVS_WARN_UNUSED_RESULT
852
ipv6_parse_masked(const char *s, struct in6_addr *ip, struct in6_addr *mask)
853
0
{
854
0
    int n = 0;
855
856
0
    char *error = ipv6_parse_masked_len(s, &n, ip, mask);
857
0
    if (!error && s[n]) {
858
0
        return xasprintf("%s: invalid IPv6 address", s);
859
0
    }
860
0
    return error;
861
0
}
862
863
/* Similar to ipv6_parse_masked_len(), but the mask, if present, must be a CIDR
864
 * mask and is returned as a prefix length in '*plen'. */
865
char * OVS_WARN_UNUSED_RESULT
866
ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
867
                    unsigned int *plen)
868
0
{
869
0
    struct in6_addr mask;
870
0
    char *error;
871
872
0
    error = ipv6_parse_masked_len(s, n, ip, &mask);
873
0
    if (error) {
874
0
        return error;
875
0
    }
876
877
0
    if (!ipv6_is_cidr(&mask)) {
878
0
        return xasprintf("%s: IPv6 CIDR network required", s);
879
0
    }
880
0
    *plen = ipv6_count_cidr_bits(&mask);
881
0
    return NULL;
882
0
}
883
884
/* Similar to ipv6_parse_cidr_len(), but doesn't return the number of scanned
885
 * characters and expects 's' to end after the ipv6/(optional) cidr. */
886
char * OVS_WARN_UNUSED_RESULT
887
ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
888
0
{
889
0
    int n = 0;
890
891
0
    char *error = ipv6_parse_cidr_len(s, &n, ip, plen);
892
0
    if (!error && s[n]) {
893
0
        return xasprintf("%s: invalid IPv6 address", s);
894
0
    }
895
0
    return error;
896
0
}
897
898
/* Stores the string representation of the IPv6 address 'addr' into the
899
 * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
900
 * bytes long. */
901
void
902
ipv6_format_addr(const struct in6_addr *addr, struct ds *s)
903
44.4k
{
904
44.4k
    char *dst;
905
906
44.4k
    ds_reserve(s, s->length + INET6_ADDRSTRLEN);
907
908
44.4k
    dst = s->string + s->length;
909
44.4k
    inet_ntop(AF_INET6, addr, dst, INET6_ADDRSTRLEN);
910
44.4k
    s->length += strlen(dst);
911
44.4k
}
912
913
/* Same as print_ipv6_addr, but optionally encloses the address in square
914
 * brackets. */
915
void
916
ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *s,
917
                         bool bracket)
918
0
{
919
0
    if (bracket) {
920
0
        ds_put_char(s, '[');
921
0
    }
922
0
    ipv6_format_addr(addr, s);
923
0
    if (bracket) {
924
0
        ds_put_char(s, ']');
925
0
    }
926
0
}
927
928
void
929
ipv6_format_mapped(const struct in6_addr *addr, struct ds *s)
930
19.2k
{
931
19.2k
    if (IN6_IS_ADDR_V4MAPPED(addr)) {
932
455
        ds_put_format(s, IP_FMT, addr->s6_addr[12], addr->s6_addr[13],
933
455
                                 addr->s6_addr[14], addr->s6_addr[15]);
934
18.7k
    } else {
935
18.7k
        ipv6_format_addr(addr, s);
936
18.7k
    }
937
19.2k
}
938
939
void
940
ipv6_format_masked(const struct in6_addr *addr, const struct in6_addr *mask,
941
                   struct ds *s)
942
25.2k
{
943
25.2k
    ipv6_format_addr(addr, s);
944
25.2k
    if (mask && !ipv6_mask_is_exact(mask)) {
945
695
        if (ipv6_is_cidr(mask)) {
946
273
            int cidr_bits = ipv6_count_cidr_bits(mask);
947
273
            ds_put_format(s, "/%d", cidr_bits);
948
422
        } else {
949
422
            ds_put_char(s, '/');
950
422
            ipv6_format_addr(mask, s);
951
422
        }
952
695
    }
953
25.2k
}
954
955
/* Stores the string representation of the IPv6 address 'addr' into the
956
 * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
957
 * bytes long. If addr is IPv4-mapped, store an IPv4 dotted-decimal string. */
958
const char *
959
ipv6_string_mapped(char *addr_str, const struct in6_addr *addr)
960
0
{
961
0
    ovs_be32 ip;
962
0
    ip = in6_addr_get_mapped_ipv4(addr);
963
0
    if (ip) {
964
0
        return inet_ntop(AF_INET, &ip, addr_str, INET6_ADDRSTRLEN);
965
0
    } else {
966
0
        return inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
967
0
    }
968
0
}
969
970
#ifdef s6_addr32
971
68.7k
#define s6_addrX s6_addr32
972
41.9k
#define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 4; VAR++)
973
#else
974
#define s6_addrX s6_addr
975
#define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 16; VAR++)
976
#endif
977
978
struct in6_addr
979
ipv6_addr_bitand(const struct in6_addr *a, const struct in6_addr *b)
980
3.88k
{
981
3.88k
   struct in6_addr dst;
982
15.5k
   IPV6_FOR_EACH (i) {
983
15.5k
       dst.s6_addrX[i] = a->s6_addrX[i] & b->s6_addrX[i];
984
15.5k
   }
985
3.88k
   return dst;
986
3.88k
}
987
988
struct in6_addr
989
ipv6_addr_bitxor(const struct in6_addr *a, const struct in6_addr *b)
990
0
{
991
0
   struct in6_addr dst;
992
0
   IPV6_FOR_EACH (i) {
993
0
       dst.s6_addrX[i] = a->s6_addrX[i] ^ b->s6_addrX[i];
994
0
   }
995
0
   return dst;
996
0
}
997
998
bool
999
ipv6_is_zero(const struct in6_addr *a)
1000
11.8k
{
1001
22.1k
   IPV6_FOR_EACH (i) {
1002
22.1k
       if (a->s6_addrX[i]) {
1003
11.4k
           return false;
1004
11.4k
       }
1005
22.1k
   }
1006
359
   return true;
1007
11.8k
}
1008
1009
/* Returns an in6_addr consisting of 'mask' high-order 1-bits and 128-N
1010
 * low-order 0-bits. */
1011
struct in6_addr
1012
ipv6_create_mask(int mask)
1013
0
{
1014
0
    struct in6_addr netmask;
1015
0
    uint8_t *netmaskp = &netmask.s6_addr[0];
1016
1017
0
    memset(&netmask, 0, sizeof netmask);
1018
0
    while (mask > 8) {
1019
0
        *netmaskp = 0xff;
1020
0
        netmaskp++;
1021
0
        mask -= 8;
1022
0
    }
1023
1024
0
    if (mask) {
1025
0
        *netmaskp = 0xff << (8 - mask);
1026
0
    }
1027
1028
0
    return netmask;
1029
0
}
1030
1031
/* Given the IPv6 netmask 'netmask', returns the number of bits of the IPv6
1032
 * address that it specifies, that is, the number of 1-bits in 'netmask'.
1033
 * 'netmask' must be a CIDR netmask (see ipv6_is_cidr()).
1034
 *
1035
 * If 'netmask' is not a CIDR netmask (see ipv6_is_cidr()), the return value
1036
 * will still be in the valid range but isn't otherwise meaningful. */
1037
int
1038
ipv6_count_cidr_bits(const struct in6_addr *netmask)
1039
273
{
1040
273
    int i;
1041
273
    int count = 0;
1042
273
    const uint8_t *netmaskp = &netmask->s6_addr[0];
1043
1044
2.68k
    for (i=0; i<16; i++) {
1045
2.68k
        if (netmaskp[i] == 0xff) {
1046
2.41k
            count += 8;
1047
2.41k
        } else {
1048
273
            uint8_t nm;
1049
1050
1.18k
            for(nm = netmaskp[i]; nm; nm <<= 1) {
1051
913
                count++;
1052
913
            }
1053
273
            break;
1054
273
        }
1055
1056
2.68k
    }
1057
1058
273
    return count;
1059
273
}
1060
1061
/* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
1062
 * high-order 1-bits and 128-N low-order 0-bits. */
1063
bool
1064
ipv6_is_cidr(const struct in6_addr *netmask)
1065
695
{
1066
695
    const uint8_t *netmaskp = &netmask->s6_addr[0];
1067
695
    int i;
1068
1069
4.43k
    for (i=0; i<16; i++) {
1070
4.16k
        if (netmaskp[i] != 0xff) {
1071
695
            uint8_t x = ~netmaskp[i];
1072
695
            if (x & (x + 1)) {
1073
182
                return false;
1074
182
            }
1075
3.57k
            while (++i < 16) {
1076
3.29k
                if (netmaskp[i]) {
1077
240
                    return false;
1078
240
                }
1079
3.29k
            }
1080
513
        }
1081
4.16k
    }
1082
1083
273
    return true;
1084
695
}
1085
1086
bool
1087
ipv6_addr_equals_masked(const struct in6_addr *a, const struct in6_addr *b,
1088
                        int plen)
1089
0
{
1090
0
    struct in6_addr mask;
1091
0
    struct in6_addr ma;
1092
0
    struct in6_addr mb;
1093
1094
0
    if (plen == 128) {
1095
0
        return ipv6_addr_equals(a, b);
1096
0
    }
1097
1098
0
    mask = ipv6_create_mask(plen);
1099
0
    ma = ipv6_addr_bitand(a, &mask);
1100
0
    mb = ipv6_addr_bitand(b, &mask);
1101
1102
0
    return ipv6_addr_equals(&ma, &mb);
1103
0
}
1104
1105
/* Populates 'b' with an Ethernet II packet headed with the given 'eth_dst',
1106
 * 'eth_src' and 'eth_type' parameters.  A payload of 'size' bytes is allocated
1107
 * in 'b' and returned.  This payload may be populated with appropriate
1108
 * information by the caller.  Sets 'b''s 'frame' pointer and 'l3' offset to
1109
 * the Ethernet header and payload respectively.  Aligns b->l3 on a 32-bit
1110
 * boundary.
1111
 *
1112
 * The returned packet has enough headroom to insert an 802.1Q VLAN header if
1113
 * desired. */
1114
void *
1115
eth_compose(struct dp_packet *b, const struct eth_addr eth_dst,
1116
            const struct eth_addr eth_src, uint16_t eth_type,
1117
            size_t size)
1118
0
{
1119
0
    void *data;
1120
0
    struct eth_header *eth;
1121
1122
1123
0
    dp_packet_clear(b);
1124
1125
    /* The magic 2 here ensures that the L3 header (when it is added later)
1126
     * will be 32-bit aligned. */
1127
0
    dp_packet_prealloc_tailroom(b, 2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + size);
1128
0
    dp_packet_reserve(b, 2 + VLAN_HEADER_LEN);
1129
0
    eth = dp_packet_put_uninit(b, ETH_HEADER_LEN);
1130
0
    data = dp_packet_put_zeros(b, size);
1131
1132
0
    eth->eth_dst = eth_dst;
1133
0
    eth->eth_src = eth_src;
1134
0
    eth->eth_type = htons(eth_type);
1135
1136
0
    b->packet_type = htonl(PT_ETH);
1137
0
    dp_packet_set_l3(b, data);
1138
1139
0
    return data;
1140
0
}
1141
1142
void
1143
packet_set_ipv4_addr(struct dp_packet *packet,
1144
                     ovs_16aligned_be32 *addr, ovs_be32 new_addr)
1145
0
{
1146
0
    struct ip_header *nh = dp_packet_l3(packet);
1147
0
    ovs_be32 old_addr = get_16aligned_be32(addr);
1148
0
    size_t l4_size = dp_packet_l4_size(packet);
1149
1150
0
    pkt_metadata_init_conn(&packet->md);
1151
1152
0
    if (nh->ip_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
1153
0
        if (dp_packet_l4_checksum_valid(packet)) {
1154
0
            dp_packet_l4_checksum_set_partial(packet);
1155
0
        } else {
1156
0
            struct tcp_header *th = dp_packet_l4(packet);
1157
0
            th->tcp_csum = recalc_csum32(th->tcp_csum, old_addr, new_addr);
1158
0
        }
1159
0
    } else if (nh->ip_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN ) {
1160
0
        if (dp_packet_l4_checksum_valid(packet)) {
1161
0
            dp_packet_l4_checksum_set_partial(packet);
1162
0
        } else {
1163
0
            struct udp_header *uh = dp_packet_l4(packet);
1164
0
            if (uh->udp_csum) {
1165
0
                uh->udp_csum = recalc_csum32(uh->udp_csum, old_addr, new_addr);
1166
0
                if (!uh->udp_csum) {
1167
0
                    uh->udp_csum = htons(0xffff);
1168
0
                }
1169
0
            }
1170
0
        }
1171
0
    }
1172
1173
0
    if (dp_packet_ip_checksum_valid(packet)) {
1174
0
        dp_packet_ip_checksum_set_partial(packet);
1175
0
    } else {
1176
0
        nh->ip_csum = recalc_csum32(nh->ip_csum, old_addr, new_addr);
1177
0
    }
1178
0
    put_16aligned_be32(addr, new_addr);
1179
0
}
1180
1181
/* Returns true, if packet contains at least one routing header where
1182
 * segements_left > 0.
1183
 *
1184
 * This function assumes that L3 and L4 offsets are set in the packet. */
1185
bool
1186
packet_rh_present(struct dp_packet *packet, uint8_t *nexthdr, bool *first_frag)
1187
0
{
1188
0
    const struct ovs_16aligned_ip6_hdr *nh;
1189
0
    size_t len;
1190
0
    size_t remaining;
1191
0
    uint8_t *data = dp_packet_l3(packet);
1192
1193
0
    remaining = packet->l4_ofs - packet->l3_ofs;
1194
0
    if (remaining < sizeof *nh) {
1195
0
        return false;
1196
0
    }
1197
0
    nh = ALIGNED_CAST(struct ovs_16aligned_ip6_hdr *, data);
1198
0
    data += sizeof *nh;
1199
0
    remaining -= sizeof *nh;
1200
0
    *nexthdr = nh->ip6_nxt;
1201
1202
0
    while (1) {
1203
0
        if ((*nexthdr != IPPROTO_HOPOPTS)
1204
0
                && (*nexthdr != IPPROTO_ROUTING)
1205
0
                && (*nexthdr != IPPROTO_DSTOPTS)
1206
0
                && (*nexthdr != IPPROTO_AH)
1207
0
                && (*nexthdr != IPPROTO_FRAGMENT)) {
1208
            /* It's either a terminal header (e.g., TCP, UDP) or one we
1209
             * don't understand.  In either case, we're done with the
1210
             * packet, so use it to fill in 'nw_proto'. */
1211
0
            break;
1212
0
        }
1213
1214
        /* We only verify that at least 8 bytes of the next header are
1215
         * available, but many of these headers are longer.  Ensure that
1216
         * accesses within the extension header are within those first 8
1217
         * bytes. All extension headers are required to be at least 8
1218
         * bytes. */
1219
0
        if (remaining < 8) {
1220
0
            return false;
1221
0
        }
1222
1223
0
        if (*nexthdr == IPPROTO_AH) {
1224
            /* A standard AH definition isn't available, but the fields
1225
             * we care about are in the same location as the generic
1226
             * option header--only the header length is calculated
1227
             * differently. */
1228
0
            const struct ip6_ext *ext_hdr = (struct ip6_ext *)data;
1229
1230
0
            *nexthdr = ext_hdr->ip6e_nxt;
1231
0
            len = (ext_hdr->ip6e_len + 2) * 4;
1232
0
        } else if (*nexthdr == IPPROTO_FRAGMENT) {
1233
0
            const struct ovs_16aligned_ip6_frag *frag_hdr
1234
0
                = ALIGNED_CAST(struct ovs_16aligned_ip6_frag *, data);
1235
1236
0
            *first_frag = !(frag_hdr->ip6f_offlg & IP6F_OFF_MASK) &&
1237
0
                           (frag_hdr->ip6f_offlg & IP6F_MORE_FRAG);
1238
0
            *nexthdr = frag_hdr->ip6f_nxt;
1239
0
            len = sizeof *frag_hdr;
1240
0
        } else if (*nexthdr == IPPROTO_ROUTING) {
1241
0
            const struct ip6_rthdr *rh = (struct ip6_rthdr *)data;
1242
1243
0
            if (rh->ip6r_segleft > 0) {
1244
0
                return true;
1245
0
            }
1246
1247
0
            *nexthdr = rh->ip6r_nxt;
1248
0
            len = (rh->ip6r_len + 1) * 8;
1249
0
        } else {
1250
0
            const struct ip6_ext *ext_hdr = (struct ip6_ext *)data;
1251
1252
0
            *nexthdr = ext_hdr->ip6e_nxt;
1253
0
            len = (ext_hdr->ip6e_len + 1) * 8;
1254
0
        }
1255
1256
0
        if (remaining < len) {
1257
0
            return false;
1258
0
        }
1259
0
        remaining -= len;
1260
0
        data += len;
1261
0
    }
1262
1263
0
    return false;
1264
0
}
1265
1266
static void
1267
packet_update_csum128(struct dp_packet *packet, uint8_t proto,
1268
                      ovs_16aligned_be32 addr[4],
1269
                      const struct in6_addr *new_addr)
1270
0
{
1271
0
    size_t l4_size = dp_packet_l4_size(packet);
1272
1273
0
    if (proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
1274
0
        if (dp_packet_l4_checksum_valid(packet)) {
1275
0
            dp_packet_l4_checksum_set_partial(packet);
1276
0
        } else {
1277
0
            struct tcp_header *th = dp_packet_l4(packet);
1278
1279
0
            th->tcp_csum = recalc_csum128(th->tcp_csum, addr, new_addr);
1280
0
        }
1281
0
    } else if (proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
1282
0
        if (dp_packet_l4_checksum_valid(packet)) {
1283
0
            dp_packet_l4_checksum_set_partial(packet);
1284
0
        } else {
1285
0
            struct udp_header *uh = dp_packet_l4(packet);
1286
1287
0
            if (uh->udp_csum) {
1288
0
                uh->udp_csum = recalc_csum128(uh->udp_csum, addr, new_addr);
1289
0
                if (!uh->udp_csum) {
1290
0
                    uh->udp_csum = htons(0xffff);
1291
0
                }
1292
0
            }
1293
0
        }
1294
0
    } else if (proto == IPPROTO_ICMPV6 &&
1295
0
               l4_size >= sizeof(struct icmp6_header)) {
1296
0
        struct icmp6_header *icmp = dp_packet_l4(packet);
1297
1298
0
        icmp->icmp6_cksum = recalc_csum128(icmp->icmp6_cksum, addr, new_addr);
1299
0
    }
1300
0
}
1301
1302
void
1303
packet_set_ipv6_addr(struct dp_packet *packet, uint8_t proto,
1304
                     ovs_16aligned_be32 addr[4],
1305
                     const struct in6_addr *new_addr,
1306
                     bool recalculate_csum)
1307
0
{
1308
0
    if (recalculate_csum) {
1309
0
        packet_update_csum128(packet, proto, addr, new_addr);
1310
0
    }
1311
0
    memcpy(addr, new_addr, sizeof(ovs_be32[4]));
1312
0
    pkt_metadata_init_conn(&packet->md);
1313
0
}
1314
1315
void
1316
packet_set_ipv6_flow_label(ovs_16aligned_be32 *flow_label, ovs_be32 flow_key)
1317
0
{
1318
0
    ovs_be32 old_label = get_16aligned_be32(flow_label);
1319
0
    ovs_be32 new_label = (old_label & htonl(~IPV6_LABEL_MASK)) | flow_key;
1320
0
    put_16aligned_be32(flow_label, new_label);
1321
0
}
1322
1323
void
1324
packet_set_ipv6_tc(ovs_16aligned_be32 *flow_label, uint8_t tc)
1325
0
{
1326
0
    ovs_be32 old_label = get_16aligned_be32(flow_label);
1327
0
    ovs_be32 new_label = (old_label & htonl(0xF00FFFFF)) | htonl(tc << 20);
1328
0
    put_16aligned_be32(flow_label, new_label);
1329
0
}
1330
1331
/* Modifies the IPv4 header fields of 'packet' to be consistent with 'src',
1332
 * 'dst', 'tos', and 'ttl'.  Updates 'packet''s L4 checksums as appropriate.
1333
 * 'packet' must contain a valid IPv4 packet with correctly populated l[347]
1334
 * markers. */
1335
void
1336
packet_set_ipv4(struct dp_packet *packet, ovs_be32 src, ovs_be32 dst,
1337
                uint8_t tos, uint8_t ttl)
1338
0
{
1339
0
    struct ip_header *nh = dp_packet_l3(packet);
1340
1341
0
    if (get_16aligned_be32(&nh->ip_src) != src) {
1342
0
        packet_set_ipv4_addr(packet, &nh->ip_src, src);
1343
0
    }
1344
1345
0
    if (get_16aligned_be32(&nh->ip_dst) != dst) {
1346
0
        packet_set_ipv4_addr(packet, &nh->ip_dst, dst);
1347
0
    }
1348
1349
0
    if (nh->ip_tos != tos) {
1350
0
        uint8_t *field = &nh->ip_tos;
1351
1352
0
        if (dp_packet_ip_checksum_valid(packet)) {
1353
0
            dp_packet_ip_checksum_set_partial(packet);
1354
0
        } else {
1355
0
            nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t) *field),
1356
0
                                        htons((uint16_t) tos));
1357
0
        }
1358
1359
0
        *field = tos;
1360
0
    }
1361
1362
0
    if (nh->ip_ttl != ttl) {
1363
0
        uint8_t *field = &nh->ip_ttl;
1364
1365
0
        if (dp_packet_ip_checksum_valid(packet)) {
1366
0
            dp_packet_ip_checksum_set_partial(packet);
1367
0
        } else {
1368
0
            nh->ip_csum = recalc_csum16(nh->ip_csum, htons(*field << 8),
1369
0
                                        htons(ttl << 8));
1370
0
        }
1371
1372
0
        *field = ttl;
1373
0
    }
1374
0
}
1375
1376
/* Modifies the IPv6 header fields of 'packet' to be consistent with 'src',
1377
 * 'dst', 'traffic class', and 'next hop'.  Updates 'packet''s L4 checksums as
1378
 * appropriate. 'packet' must contain a valid IPv6 packet with correctly
1379
 * populated l[34] offsets. */
1380
void
1381
packet_set_ipv6(struct dp_packet *packet, const struct in6_addr *src,
1382
                const struct in6_addr *dst, uint8_t key_tc, ovs_be32 key_fl,
1383
                uint8_t key_hl)
1384
0
{
1385
0
    struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet);
1386
0
    bool recalc_csum = true;
1387
0
    uint8_t proto = 0;
1388
0
    bool rh_present;
1389
1390
0
    rh_present = packet_rh_present(packet, &proto, &recalc_csum);
1391
1392
0
    if (memcmp(&nh->ip6_src, src, sizeof(ovs_be32[4]))) {
1393
0
        packet_set_ipv6_addr(packet, proto, nh->ip6_src.be32,
1394
0
                             src, recalc_csum);
1395
0
    }
1396
1397
0
    if (memcmp(&nh->ip6_dst, dst, sizeof(ovs_be32[4]))) {
1398
0
        packet_set_ipv6_addr(packet, proto, nh->ip6_dst.be32, dst,
1399
0
                             !rh_present && recalc_csum);
1400
0
    }
1401
1402
0
    packet_set_ipv6_tc(&nh->ip6_flow, key_tc);
1403
0
    packet_set_ipv6_flow_label(&nh->ip6_flow, key_fl);
1404
0
    nh->ip6_hlim = key_hl;
1405
0
}
1406
1407
static void
1408
packet_set_port(ovs_be16 *port, ovs_be16 new_port, ovs_be16 *csum)
1409
0
{
1410
0
    if (*port != new_port) {
1411
0
        if (csum) {
1412
0
            *csum = recalc_csum16(*csum, *port, new_port);
1413
0
        }
1414
0
        *port = new_port;
1415
0
    }
1416
0
}
1417
1418
/* Sets the TCP source and destination port ('src' and 'dst' respectively) of
1419
 * the TCP header contained in 'packet'.  'packet' must be a valid TCP packet
1420
 * with its l4 offset properly populated. */
1421
void
1422
packet_set_tcp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
1423
0
{
1424
0
    struct tcp_header *th = dp_packet_l4(packet);
1425
0
    ovs_be16 *csum = NULL;
1426
1427
0
    if (dp_packet_l4_checksum_valid(packet)) {
1428
0
        dp_packet_l4_checksum_set_partial(packet);
1429
0
    } else {
1430
0
        csum = &th->tcp_csum;
1431
0
    }
1432
1433
0
    packet_set_port(&th->tcp_src, src, csum);
1434
0
    packet_set_port(&th->tcp_dst, dst, csum);
1435
0
    pkt_metadata_init_conn(&packet->md);
1436
0
}
1437
1438
/* Sets the UDP source and destination port ('src' and 'dst' respectively) of
1439
 * the UDP header contained in 'packet'.  'packet' must be a valid UDP packet
1440
 * with its l4 offset properly populated. */
1441
void
1442
packet_set_udp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
1443
0
{
1444
0
    struct udp_header *uh = dp_packet_l4(packet);
1445
1446
0
    if (dp_packet_l4_checksum_valid(packet)) {
1447
0
        dp_packet_l4_checksum_set_partial(packet);
1448
0
        packet_set_port(&uh->udp_src, src, NULL);
1449
0
        packet_set_port(&uh->udp_dst, dst, NULL);
1450
0
    } else {
1451
0
        ovs_be16 *csum = uh->udp_csum ? &uh->udp_csum : NULL;
1452
1453
0
        packet_set_port(&uh->udp_src, src, csum);
1454
0
        packet_set_port(&uh->udp_dst, dst, csum);
1455
1456
0
        if (csum && !uh->udp_csum) {
1457
0
            uh->udp_csum = htons(0xffff);
1458
0
        }
1459
0
    }
1460
1461
0
    pkt_metadata_init_conn(&packet->md);
1462
0
}
1463
1464
/* Sets the SCTP source and destination port ('src' and 'dst' respectively) of
1465
 * the SCTP header contained in 'packet'.  'packet' must be a valid SCTP packet
1466
 * with its l4 offset properly populated. */
1467
void
1468
packet_set_sctp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
1469
0
{
1470
0
    struct sctp_header *sh = dp_packet_l4(packet);
1471
1472
0
    if (dp_packet_l4_checksum_valid(packet)) {
1473
0
        dp_packet_l4_checksum_set_partial(packet);
1474
0
        sh->sctp_src = src;
1475
0
        sh->sctp_dst = dst;
1476
0
    } else {
1477
0
        ovs_be32 old_csum, old_correct_csum, new_csum;
1478
0
        uint16_t tp_len = dp_packet_l4_size(packet);
1479
1480
0
        old_csum = get_16aligned_be32(&sh->sctp_csum);
1481
0
        put_16aligned_be32(&sh->sctp_csum, 0);
1482
0
        old_correct_csum = crc32c((void *) sh, tp_len);
1483
1484
0
        sh->sctp_src = src;
1485
0
        sh->sctp_dst = dst;
1486
1487
0
        new_csum = crc32c((void *) sh, tp_len);
1488
0
        put_16aligned_be32(&sh->sctp_csum, old_csum ^ old_correct_csum
1489
0
                           ^ new_csum);
1490
0
    }
1491
1492
0
    pkt_metadata_init_conn(&packet->md);
1493
0
}
1494
1495
/* Sets the ICMP type and code of the ICMP header contained in 'packet'.
1496
 * 'packet' must be a valid ICMP packet with its l4 offset properly
1497
 * populated. */
1498
void
1499
packet_set_icmp(struct dp_packet *packet, uint8_t type, uint8_t code)
1500
0
{
1501
0
    struct icmp_header *ih = dp_packet_l4(packet);
1502
0
    ovs_be16 orig_tc = htons(ih->icmp_type << 8 | ih->icmp_code);
1503
0
    ovs_be16 new_tc = htons(type << 8 | code);
1504
1505
0
    if (orig_tc != new_tc) {
1506
0
        ih->icmp_type = type;
1507
0
        ih->icmp_code = code;
1508
1509
0
        ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_tc, new_tc);
1510
0
    }
1511
0
    pkt_metadata_init_conn(&packet->md);
1512
0
}
1513
1514
/* Sets the ICMP id of the ICMP header contained in 'packet'.
1515
 * 'packet' must be a valid ICMP packet with its l4 offset properly
1516
 * populated. */
1517
void
1518
packet_set_icmp_id(struct dp_packet *packet, ovs_be16 icmp_id)
1519
0
{
1520
0
    struct icmp_header *ih = dp_packet_l4(packet);
1521
1522
0
    if (!ih || dp_packet_l4_size(packet) < ICMP_HEADER_LEN) {
1523
0
        return;
1524
0
    }
1525
1526
0
    ovs_be16 orig_ic = ih->icmp_fields.echo.id;
1527
1528
0
    if (icmp_id != orig_ic) {
1529
0
        ih->icmp_fields.echo.id = icmp_id;
1530
0
        ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_ic, icmp_id);
1531
0
    }
1532
1533
0
    pkt_metadata_init_conn(&packet->md);
1534
0
}
1535
1536
uint8_t
1537
packet_get_icmp_type(const struct dp_packet *packet)
1538
0
{
1539
0
    struct icmp_header *ih = dp_packet_l4(packet);
1540
1541
0
    if (!ih || dp_packet_l4_size(packet) < ICMP_HEADER_LEN) {
1542
0
        return 0;
1543
0
    }
1544
1545
0
    return ih->icmp_type;
1546
0
}
1547
1548
uint8_t
1549
packet_get_ip_proto(const struct dp_packet *packet)
1550
0
{
1551
0
    struct eth_header *l2 = dp_packet_eth(packet);
1552
0
    uint8_t ip_proto;
1553
1554
0
    if (l2->eth_type == htons(ETH_TYPE_IPV6)) {
1555
0
        struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(packet);
1556
0
        ip_proto = nh6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
1557
0
    } else {
1558
0
        struct ip_header *l3_hdr = dp_packet_l3(packet);
1559
0
        ip_proto = l3_hdr->ip_proto;
1560
0
    }
1561
1562
0
    return ip_proto;
1563
0
}
1564
1565
bool
1566
packet_is_icmpv4_info_message(const struct dp_packet *packet)
1567
0
{
1568
0
    uint8_t ip_proto, icmp_type;
1569
1570
0
    ip_proto = packet_get_ip_proto(packet);
1571
0
    if (ip_proto != IPPROTO_ICMP) {
1572
0
        return false;
1573
0
    }
1574
1575
0
    icmp_type = packet_get_icmp_type(packet);
1576
0
    if (icmp_type == ICMP4_ECHO_REQUEST ||
1577
0
        icmp_type == ICMP4_ECHO_REPLY ||
1578
0
        icmp_type == ICMP4_TIMESTAMP ||
1579
0
        icmp_type == ICMP4_TIMESTAMPREPLY ||
1580
0
        icmp_type == ICMP4_INFOREQUEST ||
1581
0
        icmp_type == ICMP4_INFOREPLY) {
1582
0
        return true;
1583
0
    }
1584
1585
0
    return false;
1586
0
}
1587
1588
/* Sets the IGMP type to IGMP_HOST_MEMBERSHIP_QUERY and populates the
1589
 * v3 query header fields in 'packet'. 'packet' must be a valid IGMPv3
1590
 * query packet with its l4 offset properly populated.
1591
 */
1592
void
1593
packet_set_igmp3_query(struct dp_packet *packet, uint8_t max_resp,
1594
                       ovs_be32 group, bool srs, uint8_t qrv, uint8_t qqic)
1595
0
{
1596
0
    struct igmpv3_query_header *igh = dp_packet_l4(packet);
1597
0
    ovs_be16 orig_type_max_resp =
1598
0
        htons(igh->type << 8 | igh->max_resp);
1599
0
    ovs_be16 new_type_max_resp =
1600
0
        htons(IGMP_HOST_MEMBERSHIP_QUERY << 8 | max_resp);
1601
1602
0
    if (orig_type_max_resp != new_type_max_resp) {
1603
0
        igh->type = IGMP_HOST_MEMBERSHIP_QUERY;
1604
0
        igh->max_resp = max_resp;
1605
0
        igh->csum = recalc_csum16(igh->csum, orig_type_max_resp,
1606
0
                                  new_type_max_resp);
1607
0
    }
1608
1609
0
    ovs_be32 old_group = get_16aligned_be32(&igh->group);
1610
1611
0
    if (old_group != group) {
1612
0
        put_16aligned_be32(&igh->group, group);
1613
0
        igh->csum = recalc_csum32(igh->csum, old_group, group);
1614
0
    }
1615
1616
    /* See RFC 3376 4.1.6. */
1617
0
    if (qrv > 7) {
1618
0
        qrv = 0;
1619
0
    }
1620
1621
0
    ovs_be16 orig_srs_qrv_qqic = htons(igh->srs_qrv << 8 | igh->qqic);
1622
0
    ovs_be16 new_srs_qrv_qqic = htons(srs << 11 | qrv << 8 | qqic);
1623
1624
0
    if (orig_srs_qrv_qqic != new_srs_qrv_qqic) {
1625
0
        igh->srs_qrv = (srs << 3 | qrv);
1626
0
        igh->qqic = qqic;
1627
0
        igh->csum = recalc_csum16(igh->csum, orig_srs_qrv_qqic,
1628
0
                                  new_srs_qrv_qqic);
1629
0
    }
1630
0
}
1631
1632
void
1633
packet_set_nd_ext(struct dp_packet *packet, const ovs_16aligned_be32 rso_flags,
1634
                  const uint8_t opt_type)
1635
0
{
1636
0
    struct ovs_nd_msg *ns;
1637
0
    struct ovs_nd_lla_opt *opt;
1638
0
    int bytes_remain = dp_packet_l4_size(packet);
1639
0
    struct ovs_16aligned_ip6_hdr * nh = dp_packet_l3(packet);
1640
0
    uint32_t pseudo_hdr_csum = 0;
1641
1642
0
    if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) {
1643
0
        return;
1644
0
    }
1645
1646
0
    if (nh) {
1647
0
        pseudo_hdr_csum = packet_csum_pseudoheader6(nh);
1648
0
    }
1649
1650
0
    ns = dp_packet_l4(packet);
1651
0
    opt = &ns->options[0];
1652
1653
    /* set RSO flags and option type */
1654
0
    ns->rso_flags = rso_flags;
1655
0
    opt->type = opt_type;
1656
1657
    /* recalculate checksum */
1658
0
    ovs_be16 *csum_value = &(ns->icmph.icmp6_cksum);
1659
0
    *csum_value = 0;
1660
0
    *csum_value = csum_finish(csum_continue(pseudo_hdr_csum,
1661
0
                              &(ns->icmph), bytes_remain));
1662
1663
0
}
1664
1665
void
1666
packet_set_nd(struct dp_packet *packet, const struct in6_addr *target,
1667
              const struct eth_addr sll, const struct eth_addr tll)
1668
0
{
1669
0
    struct ovs_nd_msg *ns;
1670
0
    struct ovs_nd_lla_opt *opt;
1671
0
    int bytes_remain = dp_packet_l4_size(packet);
1672
1673
0
    if (OVS_UNLIKELY(bytes_remain < sizeof(*ns))) {
1674
0
        return;
1675
0
    }
1676
1677
0
    ns = dp_packet_l4(packet);
1678
0
    opt = &ns->options[0];
1679
0
    bytes_remain -= sizeof(*ns);
1680
1681
0
    if (memcmp(&ns->target, target, sizeof(ovs_be32[4]))) {
1682
0
        packet_set_ipv6_addr(packet, IPPROTO_ICMPV6, ns->target.be32, target,
1683
0
                             true);
1684
0
    }
1685
1686
0
    while (bytes_remain >= ND_LLA_OPT_LEN && opt->len != 0
1687
0
           && bytes_remain >= (opt->len * ND_LLA_OPT_LEN)) {
1688
0
        if (opt->type == ND_OPT_SOURCE_LINKADDR && opt->len == 1) {
1689
0
            if (!eth_addr_equals(opt->mac, sll)) {
1690
0
                ovs_be16 *csum = &(ns->icmph.icmp6_cksum);
1691
1692
0
                *csum = recalc_csum48(*csum, opt->mac, sll);
1693
0
                opt->mac = sll;
1694
0
            }
1695
1696
            /* A packet can only contain one SLL or TLL option */
1697
0
            break;
1698
0
        } else if (opt->type == ND_OPT_TARGET_LINKADDR && opt->len == 1) {
1699
0
            if (!eth_addr_equals(opt->mac, tll)) {
1700
0
                ovs_be16 *csum = &(ns->icmph.icmp6_cksum);
1701
1702
0
                *csum = recalc_csum48(*csum, opt->mac, tll);
1703
0
                opt->mac = tll;
1704
0
            }
1705
1706
            /* A packet can only contain one SLL or TLL option */
1707
0
            break;
1708
0
        }
1709
1710
0
        opt += opt->len;
1711
0
        bytes_remain -= opt->len * ND_LLA_OPT_LEN;
1712
0
    }
1713
0
}
1714
1715
const char *
1716
packet_tcp_flag_to_string(uint32_t flag)
1717
35.2k
{
1718
35.2k
    switch (flag) {
1719
2.48k
    case TCP_FIN:
1720
2.48k
        return "fin";
1721
1.65k
    case TCP_SYN:
1722
1.65k
        return "syn";
1723
4.09k
    case TCP_RST:
1724
4.09k
        return "rst";
1725
1.66k
    case TCP_PSH:
1726
1.66k
        return "psh";
1727
2.80k
    case TCP_ACK:
1728
2.80k
        return "ack";
1729
3.67k
    case TCP_URG:
1730
3.67k
        return "urg";
1731
2.54k
    case TCP_ECE:
1732
2.54k
        return "ece";
1733
1.45k
    case TCP_CWR:
1734
1.45k
        return "cwr";
1735
4.51k
    case TCP_NS:
1736
4.51k
        return "ns";
1737
1.85k
    case 0x200:
1738
1.85k
        return "[200]";
1739
4.47k
    case 0x400:
1740
4.47k
        return "[400]";
1741
2.99k
    case 0x800:
1742
2.99k
        return "[800]";
1743
1.03k
    default:
1744
1.03k
        return NULL;
1745
35.2k
    }
1746
35.2k
}
1747
1748
/* Appends a string representation of the TCP flags value 'tcp_flags'
1749
 * (e.g. from struct flow.tcp_flags or obtained via TCP_FLAGS) to 's', in the
1750
 * format used by tcpdump. */
1751
void
1752
packet_format_tcp_flags(struct ds *s, uint16_t tcp_flags)
1753
0
{
1754
0
    if (!tcp_flags) {
1755
0
        ds_put_cstr(s, "none");
1756
0
        return;
1757
0
    }
1758
1759
0
    if (tcp_flags & TCP_SYN) {
1760
0
        ds_put_char(s, 'S');
1761
0
    }
1762
0
    if (tcp_flags & TCP_FIN) {
1763
0
        ds_put_char(s, 'F');
1764
0
    }
1765
0
    if (tcp_flags & TCP_PSH) {
1766
0
        ds_put_char(s, 'P');
1767
0
    }
1768
0
    if (tcp_flags & TCP_RST) {
1769
0
        ds_put_char(s, 'R');
1770
0
    }
1771
0
    if (tcp_flags & TCP_URG) {
1772
0
        ds_put_char(s, 'U');
1773
0
    }
1774
0
    if (tcp_flags & TCP_ACK) {
1775
0
        ds_put_char(s, '.');
1776
0
    }
1777
0
    if (tcp_flags & TCP_ECE) {
1778
0
        ds_put_cstr(s, "E");
1779
0
    }
1780
0
    if (tcp_flags & TCP_CWR) {
1781
0
        ds_put_cstr(s, "C");
1782
0
    }
1783
0
    if (tcp_flags & TCP_NS) {
1784
0
        ds_put_cstr(s, "N");
1785
0
    }
1786
0
    if (tcp_flags & 0x200) {
1787
0
        ds_put_cstr(s, "[200]");
1788
0
    }
1789
0
    if (tcp_flags & 0x400) {
1790
0
        ds_put_cstr(s, "[400]");
1791
0
    }
1792
0
    if (tcp_flags & 0x800) {
1793
0
        ds_put_cstr(s, "[800]");
1794
0
    }
1795
0
}
1796
1797
0
#define ARP_PACKET_SIZE  (2 + ETH_HEADER_LEN + VLAN_HEADER_LEN + \
1798
0
                          ARP_ETH_HEADER_LEN)
1799
1800
/* Clears 'b' and replaces its contents by an ARP frame with the specified
1801
 * 'arp_op', 'arp_sha', 'arp_tha', 'arp_spa', and 'arp_tpa'.  The outer
1802
 * Ethernet frame is initialized with Ethernet source 'arp_sha' and destination
1803
 * 'arp_tha', except that destination ff:ff:ff:ff:ff:ff is used instead if
1804
 * 'broadcast' is true.  Points the L3 header to the ARP header. */
1805
void
1806
compose_arp(struct dp_packet *b, uint16_t arp_op,
1807
            const struct eth_addr arp_sha, const struct eth_addr arp_tha,
1808
            bool broadcast, ovs_be32 arp_spa, ovs_be32 arp_tpa)
1809
0
{
1810
0
    compose_arp__(b);
1811
1812
0
    struct eth_header *eth = dp_packet_eth(b);
1813
0
    eth->eth_dst = broadcast ? eth_addr_broadcast : arp_tha;
1814
0
    eth->eth_src = arp_sha;
1815
1816
0
    struct arp_eth_header *arp = dp_packet_l3(b);
1817
0
    arp->ar_op = htons(arp_op);
1818
0
    arp->ar_sha = arp_sha;
1819
0
    arp->ar_tha = arp_tha;
1820
0
    put_16aligned_be32(&arp->ar_spa, arp_spa);
1821
0
    put_16aligned_be32(&arp->ar_tpa, arp_tpa);
1822
0
}
1823
1824
/* Clears 'b' and replaces its contents by an ARP frame.  Sets the fields in
1825
 * the Ethernet and ARP headers that are fixed for ARP frames to those fixed
1826
 * values, and zeroes the other fields.  Points the L3 header to the ARP
1827
 * header. */
1828
void
1829
compose_arp__(struct dp_packet *b)
1830
0
{
1831
0
    dp_packet_clear(b);
1832
0
    dp_packet_prealloc_tailroom(b, ARP_PACKET_SIZE);
1833
0
    dp_packet_reserve(b, 2 + VLAN_HEADER_LEN);
1834
1835
0
    struct eth_header *eth = dp_packet_put_zeros(b, sizeof *eth);
1836
0
    eth->eth_type = htons(ETH_TYPE_ARP);
1837
1838
0
    struct arp_eth_header *arp = dp_packet_put_zeros(b, sizeof *arp);
1839
0
    arp->ar_hrd = htons(ARP_HRD_ETHERNET);
1840
0
    arp->ar_pro = htons(ARP_PRO_IP);
1841
0
    arp->ar_hln = sizeof arp->ar_sha;
1842
0
    arp->ar_pln = sizeof arp->ar_spa;
1843
1844
0
    dp_packet_set_l3(b, arp);
1845
1846
0
    b->packet_type = htonl(PT_ETH);
1847
0
}
1848
1849
/* This function expects packet with ethernet header with correct
1850
 * l3 pointer set. */
1851
void *
1852
compose_ipv6(struct dp_packet *packet, uint8_t proto,
1853
             const struct in6_addr *src, const struct in6_addr *dst,
1854
             uint8_t key_tc, ovs_be32 key_fl, uint8_t key_hl, int size)
1855
0
{
1856
0
    struct ovs_16aligned_ip6_hdr *nh;
1857
0
    void *data;
1858
1859
0
    nh = dp_packet_l3(packet);
1860
0
    nh->ip6_vfc = 0x60;
1861
0
    nh->ip6_nxt = proto;
1862
0
    nh->ip6_plen = htons(size);
1863
0
    data = dp_packet_put_zeros(packet, size);
1864
0
    dp_packet_set_l4(packet, data);
1865
0
    packet_set_ipv6(packet, src, dst, key_tc, key_fl, key_hl);
1866
0
    return data;
1867
0
}
1868
1869
/* Compose an IPv6 Neighbor Discovery Neighbor Solicitation message. */
1870
void
1871
compose_nd_ns(struct dp_packet *b, const struct eth_addr eth_src,
1872
              const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst)
1873
0
{
1874
0
    struct in6_addr sn_addr;
1875
0
    struct eth_addr eth_dst;
1876
0
    struct ovs_nd_msg *ns;
1877
0
    struct ovs_nd_lla_opt *lla_opt;
1878
0
    uint32_t icmp_csum;
1879
1880
0
    in6_addr_solicited_node(&sn_addr, ipv6_dst);
1881
0
    ipv6_multicast_to_ethernet(&eth_dst, &sn_addr);
1882
1883
0
    eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
1884
0
    ns = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, &sn_addr,
1885
0
                      0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN);
1886
1887
0
    ns->icmph.icmp6_type = ND_NEIGHBOR_SOLICIT;
1888
0
    ns->icmph.icmp6_code = 0;
1889
0
    put_16aligned_be32(&ns->rso_flags, htonl(0));
1890
1891
0
    lla_opt = &ns->options[0];
1892
0
    lla_opt->type = ND_OPT_SOURCE_LINKADDR;
1893
0
    lla_opt->len = 1;
1894
1895
0
    packet_set_nd(b, ipv6_dst, eth_src, eth_addr_zero);
1896
1897
0
    ns->icmph.icmp6_cksum = 0;
1898
0
    icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
1899
0
    ns->icmph.icmp6_cksum = csum_finish(
1900
0
        csum_continue(icmp_csum, ns, ND_MSG_LEN + ND_LLA_OPT_LEN));
1901
0
}
1902
1903
/* Compose an IPv6 Neighbor Discovery Neighbor Advertisement message. */
1904
void
1905
compose_nd_na(struct dp_packet *b,
1906
              const struct eth_addr eth_src, const struct eth_addr eth_dst,
1907
              const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst,
1908
              ovs_be32 rso_flags)
1909
0
{
1910
0
    struct ovs_nd_msg *na;
1911
0
    struct ovs_nd_lla_opt *lla_opt;
1912
0
    uint32_t icmp_csum;
1913
1914
0
    eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
1915
0
    na = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst,
1916
0
                      0, 0, 255, ND_MSG_LEN + ND_LLA_OPT_LEN);
1917
1918
0
    na->icmph.icmp6_type = ND_NEIGHBOR_ADVERT;
1919
0
    na->icmph.icmp6_code = 0;
1920
0
    put_16aligned_be32(&na->rso_flags, rso_flags);
1921
1922
0
    lla_opt = &na->options[0];
1923
0
    lla_opt->type = ND_OPT_TARGET_LINKADDR;
1924
0
    lla_opt->len = 1;
1925
1926
0
    packet_set_nd(b, ipv6_src, eth_addr_zero, eth_src);
1927
1928
0
    na->icmph.icmp6_cksum = 0;
1929
0
    icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
1930
0
    na->icmph.icmp6_cksum = csum_finish(csum_continue(
1931
0
        icmp_csum, na, ND_MSG_LEN + ND_LLA_OPT_LEN));
1932
0
}
1933
1934
/* Compose an IPv6 Neighbor Discovery Router Advertisement message with
1935
 * Source Link-layer Address Option and MTU Option.
1936
 * Caller can call packet_put_ra_prefix_opt to append Prefix Information
1937
 * Options to composed messags in 'b'. */
1938
void
1939
compose_nd_ra(struct dp_packet *b,
1940
              const struct eth_addr eth_src, const struct eth_addr eth_dst,
1941
              const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst,
1942
              uint8_t cur_hop_limit, uint8_t mo_flags,
1943
              ovs_be16 router_lt, ovs_be32 reachable_time,
1944
              ovs_be32 retrans_timer, uint32_t mtu)
1945
0
{
1946
    /* Don't compose Router Advertisement packet with MTU Option if mtu
1947
     * value is 0. */
1948
0
    bool with_mtu = mtu != 0;
1949
0
    size_t mtu_opt_len = with_mtu ? ND_MTU_OPT_LEN : 0;
1950
1951
0
    eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN);
1952
1953
0
    struct ovs_ra_msg *ra = compose_ipv6(
1954
0
        b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, 0, 0, 255,
1955
0
        RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len);
1956
0
    ra->icmph.icmp6_type = ND_ROUTER_ADVERT;
1957
0
    ra->icmph.icmp6_code = 0;
1958
0
    ra->cur_hop_limit = cur_hop_limit;
1959
0
    ra->mo_flags = mo_flags;
1960
0
    ra->router_lifetime = router_lt;
1961
0
    ra->reachable_time = reachable_time;
1962
0
    ra->retrans_timer = retrans_timer;
1963
1964
0
    struct ovs_nd_lla_opt *lla_opt = ra->options;
1965
0
    lla_opt->type = ND_OPT_SOURCE_LINKADDR;
1966
0
    lla_opt->len = 1;
1967
0
    lla_opt->mac = eth_src;
1968
1969
0
    if (with_mtu) {
1970
        /* ovs_nd_mtu_opt has the same size with ovs_nd_lla_opt. */
1971
0
        struct ovs_nd_mtu_opt *mtu_opt
1972
0
            = (struct ovs_nd_mtu_opt *)(lla_opt + 1);
1973
0
        mtu_opt->type = ND_OPT_MTU;
1974
0
        mtu_opt->len = 1;
1975
0
        mtu_opt->reserved = 0;
1976
0
        put_16aligned_be32(&mtu_opt->mtu, htonl(mtu));
1977
0
    }
1978
1979
0
    ra->icmph.icmp6_cksum = 0;
1980
0
    uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
1981
0
    ra->icmph.icmp6_cksum = csum_finish(csum_continue(
1982
0
        icmp_csum, ra, RA_MSG_LEN + ND_LLA_OPT_LEN + mtu_opt_len));
1983
0
}
1984
1985
/* Append an IPv6 Neighbor Discovery Prefix Information option to a
1986
 * Router Advertisement message. */
1987
void
1988
packet_put_ra_prefix_opt(struct dp_packet *b,
1989
                         uint8_t plen, uint8_t la_flags,
1990
                         ovs_be32 valid_lifetime, ovs_be32 preferred_lifetime,
1991
                         const ovs_be128 prefix)
1992
0
{
1993
0
    size_t prev_l4_size = dp_packet_l4_size(b);
1994
0
    struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(b);
1995
0
    nh->ip6_plen = htons(prev_l4_size + ND_PREFIX_OPT_LEN);
1996
1997
0
    struct ovs_nd_prefix_opt *prefix_opt =
1998
0
        dp_packet_put_uninit(b, sizeof *prefix_opt);
1999
0
    prefix_opt->type = ND_OPT_PREFIX_INFORMATION;
2000
0
    prefix_opt->len = 4;
2001
0
    prefix_opt->prefix_len = plen;
2002
0
    prefix_opt->la_flags = la_flags;
2003
0
    put_16aligned_be32(&prefix_opt->valid_lifetime, valid_lifetime);
2004
0
    put_16aligned_be32(&prefix_opt->preferred_lifetime, preferred_lifetime);
2005
0
    put_16aligned_be32(&prefix_opt->reserved, 0);
2006
0
    memcpy(prefix_opt->prefix.be32, prefix.be32, sizeof(ovs_be32[4]));
2007
2008
0
    struct ovs_ra_msg *ra = dp_packet_l4(b);
2009
0
    ra->icmph.icmp6_cksum = 0;
2010
0
    uint32_t icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b));
2011
0
    ra->icmph.icmp6_cksum = csum_finish(csum_continue(
2012
0
        icmp_csum, ra, prev_l4_size + ND_PREFIX_OPT_LEN));
2013
0
}
2014
2015
uint32_t
2016
packet_csum_pseudoheader(const struct ip_header *ip)
2017
0
{
2018
0
    uint32_t partial = 0;
2019
2020
0
    partial = csum_add32(partial, get_16aligned_be32(&ip->ip_src));
2021
0
    partial = csum_add32(partial, get_16aligned_be32(&ip->ip_dst));
2022
0
    partial = csum_add16(partial, htons(ip->ip_proto));
2023
0
    partial = csum_add16(partial, htons(ntohs(ip->ip_tot_len) -
2024
0
                                        IP_IHL(ip->ip_ihl_ver) * 4));
2025
2026
0
    return partial;
2027
0
}
2028
2029
#ifndef __CHECKER__
2030
uint32_t
2031
packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *ip6)
2032
0
{
2033
0
    uint32_t partial = 0;
2034
2035
0
    partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src);
2036
0
    partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst);
2037
0
    partial = csum_add16(partial, htons(ip6->ip6_nxt));
2038
0
    partial = csum_add16(partial, ip6->ip6_plen);
2039
2040
0
    return partial;
2041
0
}
2042
2043
/* Calculate the IPv6 upper layer checksum according to RFC2460. We pass the
2044
   ip6_nxt and ip6_plen values, so it will also work if extension headers
2045
   are present. */
2046
ovs_be16
2047
packet_csum_upperlayer6(const struct ovs_16aligned_ip6_hdr *ip6,
2048
                        const void *data, uint8_t l4_protocol,
2049
                        uint16_t l4_size)
2050
0
{
2051
0
    uint32_t partial = 0;
2052
2053
0
    partial = csum_continue(partial, &ip6->ip6_src, sizeof ip6->ip6_src);
2054
0
    partial = csum_continue(partial, &ip6->ip6_dst, sizeof ip6->ip6_dst);
2055
0
    partial = csum_add16(partial, htons(l4_protocol));
2056
0
    partial = csum_add16(partial, htons(l4_size));
2057
2058
0
    partial = csum_continue(partial, data, l4_size);
2059
2060
0
    return csum_finish(partial);
2061
0
}
2062
#endif
2063
2064
void
2065
IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6)
2066
0
{
2067
0
    if (is_ipv6) {
2068
0
        ovs_16aligned_be32 *ip6 = dp_packet_l3(pkt);
2069
2070
0
        put_16aligned_be32(ip6, get_16aligned_be32(ip6) |
2071
0
                                htonl(IP_ECN_CE << 20));
2072
0
    } else {
2073
0
        struct ip_header *nh = dp_packet_l3(pkt);
2074
0
        uint8_t tos = nh->ip_tos;
2075
2076
0
        tos |= IP_ECN_CE;
2077
0
        if (nh->ip_tos != tos) {
2078
0
            if (dp_packet_ip_checksum_valid(pkt)) {
2079
0
                dp_packet_ip_checksum_set_partial(pkt);
2080
0
            } else {
2081
0
                nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_tos),
2082
0
                                            htons((uint16_t) tos));
2083
0
            }
2084
2085
0
            nh->ip_tos = tos;
2086
0
        }
2087
0
    }
2088
0
}
2089
2090
/* Set TCP checksum field in packet 'p' with complete checksum.
2091
 * The packet must have the L3 and L4 offsets. */
2092
void
2093
packet_tcp_complete_csum(struct dp_packet *p, bool inner)
2094
0
{
2095
0
    struct tcp_header *tcp;
2096
0
    size_t tcp_sz;
2097
0
    void *ip_hdr;
2098
2099
0
    if (inner) {
2100
0
        tcp = dp_packet_inner_l4(p);
2101
0
        ip_hdr = dp_packet_inner_l3(p);
2102
0
        tcp_sz = dp_packet_inner_l4_size(p);
2103
0
    } else {
2104
0
        tcp = dp_packet_l4(p);
2105
0
        ip_hdr = dp_packet_l3(p);
2106
0
        tcp_sz = dp_packet_l4_size(p);
2107
0
    }
2108
2109
0
    ovs_assert(tcp);
2110
0
    ovs_assert(ip_hdr);
2111
2112
0
    tcp->tcp_csum = 0;
2113
0
    if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) {
2114
0
        struct ip_header *ip = ip_hdr;
2115
2116
0
        tcp->tcp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip),
2117
0
                                                  tcp, tcp_sz));
2118
0
    } else {
2119
0
        struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr;
2120
2121
0
        tcp->tcp_csum = packet_csum_upperlayer6(ip6, tcp, ip6->ip6_nxt,
2122
0
                                                tcp_sz);
2123
0
    }
2124
2125
0
    if (inner) {
2126
0
        dp_packet_inner_l4_checksum_set_good(p);
2127
0
    } else {
2128
0
        dp_packet_l4_checksum_set_good(p);
2129
0
    }
2130
0
}
2131
2132
/* Set UDP checksum field in packet 'p' with complete checksum.
2133
 * The packet must have the L3 and L4 offsets. */
2134
void
2135
packet_udp_complete_csum(struct dp_packet *p, bool inner)
2136
0
{
2137
0
    struct udp_header *udp;
2138
0
    size_t udp_sz;
2139
0
    void *ip_hdr;
2140
2141
0
    if (inner) {
2142
0
        udp = dp_packet_inner_l4(p);
2143
0
        ip_hdr = dp_packet_inner_l3(p);
2144
0
        udp_sz = dp_packet_inner_l4_size(p);
2145
0
    } else {
2146
0
        udp = dp_packet_l4(p);
2147
0
        ip_hdr = dp_packet_l3(p);
2148
0
        udp_sz = dp_packet_l4_size(p);
2149
0
    }
2150
2151
0
    ovs_assert(udp);
2152
0
    ovs_assert(ip_hdr);
2153
2154
    /* Skip csum calculation if the udp_csum is zero. */
2155
0
    if (!udp->udp_csum) {
2156
0
        goto out;
2157
0
    }
2158
2159
0
    udp->udp_csum = 0;
2160
0
    if (IP_VER(((const struct ip_header *) ip_hdr)->ip_ihl_ver) == 4) {
2161
0
        struct ip_header *ip = ip_hdr;
2162
2163
0
        udp->udp_csum = csum_finish(csum_continue(packet_csum_pseudoheader(ip),
2164
0
                                                  udp, udp_sz));
2165
0
    } else {
2166
0
        struct ovs_16aligned_ip6_hdr *ip6 = ip_hdr;
2167
2168
0
        udp->udp_csum = packet_csum_upperlayer6(ip6, udp, ip6->ip6_nxt,
2169
0
                                                udp_sz);
2170
0
    }
2171
2172
0
    if (!udp->udp_csum) {
2173
0
        udp->udp_csum = htons(0xffff);
2174
0
    }
2175
2176
0
out:
2177
0
    if (inner) {
2178
0
        dp_packet_inner_l4_checksum_set_good(p);
2179
0
    } else {
2180
0
        dp_packet_l4_checksum_set_good(p);
2181
0
    }
2182
0
}
2183
2184
/* This helper computes a "constant" UDP checksum without looking at the
2185
 * L4 payload.
2186
 *
2187
 * This is possible when L4 is either TCP or UDP: the L4 payload checksum
2188
 * is either computed in SW or in HW later, but its contribution to the
2189
 * outer checksum is cancelled by the L4 payload being part of the global
2190
 * packet sum. */
2191
bool
2192
packet_udp_tunnel_csum(struct dp_packet *p)
2193
0
{
2194
0
    struct ip_header *inner_ip;
2195
0
    const void *inner_l4_data;
2196
0
    char *after_inner_l4_csum;
2197
0
    size_t inner_l4_csum_off;
2198
0
    struct udp_header *udp;
2199
0
    ovs_be16 inner_l4_csum;
2200
0
    uint32_t partial_csum;
2201
0
    struct ip_header *ip;
2202
0
    uint32_t inner_csum;
2203
0
    uint16_t tso_segsz;
2204
0
    bool inner_ipv4;
2205
0
    void *inner_l4;
2206
2207
0
    inner_ip = dp_packet_inner_l3(p);
2208
0
    inner_l4 = dp_packet_inner_l4(p);
2209
0
    ip = dp_packet_l3(p);
2210
0
    udp = dp_packet_l4(p);
2211
2212
0
    if (dp_packet_inner_l4_proto_tcp(p)) {
2213
0
        inner_l4_csum_off = offsetof(struct tcp_header, tcp_csum);
2214
0
        inner_l4_data = dp_packet_get_inner_tcp_payload(p);
2215
0
        if (!inner_l4_data) {
2216
            /* Malformed packet. */
2217
0
            return false;
2218
0
        }
2219
0
    } else if (dp_packet_inner_l4_proto_udp(p)) {
2220
0
        inner_l4_csum_off = offsetof(struct udp_header, udp_csum);
2221
0
        inner_l4_data = (char *) inner_l4 + sizeof (struct udp_header);
2222
0
        if (((struct udp_header *) inner_l4)->udp_csum == 0) {
2223
            /* There is no nested checksum.
2224
             * No choice but compute a full checksum. */
2225
0
            return false;
2226
0
        }
2227
0
    } else {
2228
        /* This optimisation applies only to inner TCP/UDP. */
2229
0
        return false;
2230
0
    }
2231
2232
0
    if (!dp_packet_inner_l4_checksum_valid(p)) {
2233
        /* We have no idea about the contribution of the payload data
2234
         * and what the L4 checksum put in the packet data looks like.
2235
         * Simpler is to let a full checksum happen. */
2236
0
        return false;
2237
0
    }
2238
2239
0
    inner_ipv4 = IP_VER(inner_ip->ip_ihl_ver) == 4;
2240
0
    if (inner_ipv4) {
2241
0
        inner_csum = packet_csum_pseudoheader(inner_ip);
2242
0
    } else {
2243
0
        struct ovs_16aligned_ip6_hdr *inner_ip6 = dp_packet_inner_l3(p);
2244
2245
0
        inner_csum = packet_csum_pseudoheader6(inner_ip6);
2246
0
    }
2247
2248
0
    inner_csum = csum_continue(inner_csum, inner_l4, inner_l4_csum_off);
2249
0
    after_inner_l4_csum = (char *) inner_l4 + inner_l4_csum_off + 2;
2250
0
    inner_l4_csum = csum_finish(csum_continue(inner_csum, after_inner_l4_csum,
2251
0
        (char *) inner_l4_data - after_inner_l4_csum));
2252
    /* Important: for inner UDP, a null inner_l4_csum here should in theory be
2253
     * replaced with 0xffff.  However, since the only use of inner_l4_csum is
2254
     * for the final outer checksum with a csum_add16() below, we can skip this
2255
     * entirely because adding 0xffff will have the same effect as adding 0x0
2256
     * after reducing in csum_finish. */
2257
2258
0
    udp->udp_csum = 0;
2259
0
    if (IP_VER(ip->ip_ihl_ver) == 4) {
2260
0
        partial_csum = packet_csum_pseudoheader(ip);
2261
0
    } else {
2262
0
        struct ovs_16aligned_ip6_hdr *ip6 = dp_packet_l3(p);
2263
2264
0
        partial_csum = packet_csum_pseudoheader6(ip6);
2265
0
    }
2266
2267
0
    partial_csum = csum_continue(partial_csum, udp,
2268
0
        (char *) inner_ip - (char *) udp);
2269
0
    if (!inner_ipv4 || !dp_packet_inner_ip_checksum_valid(p)) {
2270
        /* IPv6 has no checksum, so for inner IPv6, we need to sum the header.
2271
         *
2272
         * In IPv4 case, if inner checksum is already good or HW offload
2273
         * has been requested, the (final) sum of the IPv4 header will be 0.
2274
         * Otherwise, we need to sum the header like for IPv6. */
2275
0
        partial_csum = csum_continue(partial_csum, inner_ip,
2276
0
            (char *) inner_l4 - (char *) inner_ip);
2277
0
    }
2278
0
    partial_csum = csum_continue(partial_csum, inner_l4, inner_l4_csum_off);
2279
0
    partial_csum = csum_add16(partial_csum, inner_l4_csum);
2280
0
    partial_csum = csum_continue(partial_csum, after_inner_l4_csum,
2281
0
        (char *) inner_l4_data - after_inner_l4_csum);
2282
0
    udp->udp_csum = csum_finish(partial_csum);
2283
0
    tso_segsz = dp_packet_get_tso_segsz(p);
2284
0
    if (tso_segsz) {
2285
0
        uint16_t payload_len = dp_packet_get_inner_tcp_payload_length(p);
2286
2287
0
        ovs_assert(payload_len == tso_segsz * dp_packet_gso_nr_segs(p));
2288
2289
        /* The pseudo header used in the outer UDP checksum is dependent on
2290
         * the ip_tot_len / ip6_plen which was a reflection of the TSO frame
2291
         * size. The segmented packet will be shorter. */
2292
0
        udp->udp_csum = recalc_csum16(udp->udp_csum, htons(payload_len),
2293
0
                                      htons(tso_segsz));
2294
2295
        /* When segmenting the packet, various headers get updated:
2296
         * - inner L3
2297
         *   - for IPv4, ip_tot_len is updated, BUT it is not affecting the
2298
         *     outer UDP checksum because the IPv4 header itself contains
2299
         *     a checksum that compensates for this update,
2300
         *   - for IPv6, ip6_plen is updated, and this must be considered,
2301
         * - inner L4
2302
         *   - inner pseudo header used in the TCP checksum is dependent on
2303
         *     the inner ip_tot_len / ip6_plen,
2304
         *   - TCP seq number is updated,
2305
         *   - the HW may change some TCP flags (think PSH/FIN),
2306
         *   BUT the TCP checksum will compensate for those updates,
2307
         *
2308
         * Summary: we only care about the inner IPv6 header update.
2309
         */
2310
0
        if (IP_VER(inner_ip->ip_ihl_ver) != 4) {
2311
0
            udp->udp_csum = recalc_csum16(udp->udp_csum, htons(payload_len),
2312
0
                                          htons(tso_segsz));
2313
0
        }
2314
0
    }
2315
0
    if (!udp->udp_csum) {
2316
0
        udp->udp_csum = htons(0xffff);
2317
0
    }
2318
0
    dp_packet_l4_checksum_set_good(p);
2319
2320
0
    return true;
2321
0
}
2322
2323
/* Set SCTP checksum field in packet 'p' with complete checksum.
2324
 * The packet must have the L3 and L4 offsets. */
2325
void
2326
packet_sctp_complete_csum(struct dp_packet *p, bool inner)
2327
0
{
2328
0
    struct sctp_header *sh;
2329
0
    uint16_t tp_len;
2330
0
    ovs_be32 csum;
2331
2332
0
    if (inner) {
2333
0
        sh = dp_packet_inner_l4(p);
2334
0
        tp_len = dp_packet_inner_l4_size(p);
2335
0
    } else {
2336
0
        sh = dp_packet_l4(p);
2337
0
        tp_len = dp_packet_l4_size(p);
2338
0
    }
2339
2340
0
    ovs_assert(sh);
2341
2342
0
    put_16aligned_be32(&sh->sctp_csum, 0);
2343
0
    csum = crc32c((void *) sh, tp_len);
2344
0
    put_16aligned_be32(&sh->sctp_csum, csum);
2345
2346
0
    if (inner) {
2347
0
        dp_packet_inner_l4_checksum_set_good(p);
2348
0
    } else {
2349
0
        dp_packet_l4_checksum_set_good(p);
2350
0
    }
2351
0
}