~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to datapath/flow.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2007-2011 Nicira, Inc.
 
2
 * Copyright (c) 2007-2013 Nicira, Inc.
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or
5
5
 * modify it under the terms of version 2 of the GNU General Public
47
47
 
48
48
static struct kmem_cache *flow_cache;
49
49
 
 
50
static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
 
51
                struct sw_flow_key_range *range, u8 val);
 
52
 
 
53
static void update_range__(struct sw_flow_match *match,
 
54
                          size_t offset, size_t size, bool is_mask)
 
55
{
 
56
        struct sw_flow_key_range *range = NULL;
 
57
        size_t start = offset;
 
58
        size_t end = offset + size;
 
59
 
 
60
        if (!is_mask)
 
61
                range = &match->range;
 
62
        else if (match->mask)
 
63
                range = &match->mask->range;
 
64
 
 
65
        if (!range)
 
66
                return;
 
67
 
 
68
        if (range->start == range->end) {
 
69
                range->start = start;
 
70
                range->end = end;
 
71
                return;
 
72
        }
 
73
 
 
74
        if (range->start > start)
 
75
                range->start = start;
 
76
 
 
77
        if (range->end < end)
 
78
                range->end = end;
 
79
}
 
80
 
 
81
#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
 
82
        do { \
 
83
                update_range__(match, offsetof(struct sw_flow_key, field),  \
 
84
                                     sizeof((match)->key->field), is_mask); \
 
85
                if (is_mask) {                                              \
 
86
                        if ((match)->mask)                                  \
 
87
                                (match)->mask->key.field = value;           \
 
88
                } else {                                                    \
 
89
                        (match)->key->field = value;                        \
 
90
                }                                                           \
 
91
        } while (0)
 
92
 
 
93
#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
 
94
        do { \
 
95
                update_range__(match, offsetof(struct sw_flow_key, field),  \
 
96
                                len, is_mask);                              \
 
97
                if (is_mask) {                                              \
 
98
                        if ((match)->mask)                                  \
 
99
                                memcpy(&(match)->mask->key.field, value_p, len);\
 
100
                } else {                                                    \
 
101
                        memcpy(&(match)->key->field, value_p, len);         \
 
102
                }                                                           \
 
103
        } while (0)
 
104
 
 
105
void ovs_match_init(struct sw_flow_match *match,
 
106
                    struct sw_flow_key *key,
 
107
                    struct sw_flow_mask *mask)
 
108
{
 
109
        memset(match, 0, sizeof(*match));
 
110
        match->key = key;
 
111
        match->mask = mask;
 
112
 
 
113
        memset(key, 0, sizeof(*key));
 
114
 
 
115
        if (mask) {
 
116
                memset(&mask->key, 0, sizeof(mask->key));
 
117
                mask->range.start = mask->range.end = 0;
 
118
        }
 
119
}
 
120
 
 
121
static bool ovs_match_validate(const struct sw_flow_match *match,
 
122
                u64 key_attrs, u64 mask_attrs)
 
123
{
 
124
        u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
 
125
        u64 mask_allowed = key_attrs;  /* At most allow all key attributes */
 
126
 
 
127
        /* The following mask attributes allowed only if they
 
128
         * pass the validation tests. */
 
129
        mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
 
130
                        | (1ULL << OVS_KEY_ATTR_IPV6)
 
131
                        | (1ULL << OVS_KEY_ATTR_TCP)
 
132
                        | (1ULL << OVS_KEY_ATTR_UDP)
 
133
                        | (1ULL << OVS_KEY_ATTR_ICMP)
 
134
                        | (1ULL << OVS_KEY_ATTR_ICMPV6)
 
135
                        | (1ULL << OVS_KEY_ATTR_ARP)
 
136
                        | (1ULL << OVS_KEY_ATTR_ND));
 
137
 
 
138
        /* Always allowed mask fields. */
 
139
        mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
 
140
                       | (1ULL << OVS_KEY_ATTR_IN_PORT)
 
141
                       | (11ULL << OVS_KEY_ATTR_ETHERTYPE));
 
142
 
 
143
        /* Check key attributes. */
 
144
        if (match->key->eth.type == htons(ETH_P_ARP)
 
145
                        || match->key->eth.type == htons(ETH_P_RARP)) {
 
146
                key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
 
147
                if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
 
148
                        mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
 
149
        }
 
150
 
 
151
        if (match->key->eth.type == htons(ETH_P_IP)) {
 
152
                key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
 
153
                if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
 
154
                        mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
 
155
 
 
156
                if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
 
157
                        if (match->key->ip.proto == IPPROTO_UDP) {
 
158
                                key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
 
159
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
160
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
 
161
                        }
 
162
 
 
163
                        if (match->key->ip.proto == IPPROTO_TCP) {
 
164
                                key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
 
165
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
166
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
 
167
                        }
 
168
 
 
169
                        if (match->key->ip.proto == IPPROTO_ICMP) {
 
170
                                key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
 
171
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
172
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
 
173
                        }
 
174
                }
 
175
        }
 
176
 
 
177
        if (match->key->eth.type == htons(ETH_P_IPV6)) {
 
178
                key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
 
179
                if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
 
180
                        mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
 
181
 
 
182
                if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
 
183
                        if (match->key->ip.proto == IPPROTO_UDP) {
 
184
                                key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
 
185
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
186
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
 
187
                        }
 
188
 
 
189
                        if (match->key->ip.proto == IPPROTO_TCP) {
 
190
                                key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
 
191
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
192
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
 
193
                        }
 
194
 
 
195
                        if (match->key->ip.proto == IPPROTO_ICMPV6) {
 
196
                                key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
 
197
                                if (match->mask && (match->mask->key.ip.proto == 0xff))
 
198
                                        mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
 
199
 
 
200
                                if (match->key->ipv6.tp.src ==
 
201
                                                htons(NDISC_NEIGHBOUR_SOLICITATION) ||
 
202
                                    match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
 
203
                                        key_expected |= 1ULL << OVS_KEY_ATTR_ND;
 
204
                                        if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
 
205
                                                mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
 
206
                                }
 
207
                        }
 
208
                }
 
209
        }
 
210
 
 
211
        if ((key_attrs & key_expected) != key_expected) {
 
212
                /* Key attributes check failed. */
 
213
                OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
 
214
                                key_attrs, key_expected);
 
215
                return false;
 
216
        }
 
217
 
 
218
        if ((mask_attrs & mask_allowed) != mask_attrs) {
 
219
                /* Mask attributes check failed. */
 
220
                OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
 
221
                                mask_attrs, mask_allowed);
 
222
                return false;
 
223
        }
 
224
 
 
225
        return true;
 
226
}
 
227
 
50
228
static int check_header(struct sk_buff *skb, int len)
51
229
{
52
230
        if (unlikely(skb->len < len))
122
300
        return cur_ms - idle_ms;
123
301
}
124
302
 
125
 
#define SW_FLOW_KEY_OFFSET(field)               \
126
 
        (offsetof(struct sw_flow_key, field) +  \
127
 
         FIELD_SIZEOF(struct sw_flow_key, field))
128
 
 
129
 
static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
130
 
                         int *key_lenp)
 
303
static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
131
304
{
132
305
        unsigned int nh_ofs = skb_network_offset(skb);
133
306
        unsigned int nh_len;
137
310
        __be16 frag_off;
138
311
        int err;
139
312
 
140
 
        *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
141
 
 
142
313
        err = check_header(skb, nh_ofs + sizeof(*nh));
143
314
        if (unlikely(err))
144
315
                return err;
177
348
                                  sizeof(struct icmp6hdr));
178
349
}
179
350
 
 
351
void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
 
352
                       const struct sw_flow_mask *mask)
 
353
{
 
354
        u8 *m = (u8 *)&mask->key + mask->range.start;
 
355
        u8 *s = (u8 *)src + mask->range.start;
 
356
        u8 *d = (u8 *)dst + mask->range.start;
 
357
        int i;
 
358
 
 
359
        memset(dst, 0, sizeof(*dst));
 
360
        for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
 
361
                *d = *s & *m;
 
362
                d++, s++, m++;
 
363
        }
 
364
}
 
365
 
180
366
#define TCP_FLAGS_OFFSET 13
181
367
#define TCP_FLAG_MASK 0x3f
182
368
 
225
411
 
226
412
        spin_lock_init(&flow->lock);
227
413
        flow->sf_acts = NULL;
 
414
        flow->mask = NULL;
228
415
 
229
416
        return flow;
230
417
}
264
451
        flex_array_free(buckets);
265
452
}
266
453
 
267
 
struct flow_table *ovs_flow_tbl_alloc(int new_size)
 
454
static struct flow_table *__flow_tbl_alloc(int new_size)
268
455
{
269
456
        struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
270
457
 
282
469
        table->node_ver = 0;
283
470
        table->keep_flows = false;
284
471
        get_random_bytes(&table->hash_seed, sizeof(u32));
 
472
        table->mask_list = NULL;
285
473
 
286
474
        return table;
287
475
}
288
476
 
289
 
void ovs_flow_tbl_destroy(struct flow_table *table)
 
477
static void __flow_tbl_destroy(struct flow_table *table)
290
478
{
291
479
        int i;
292
480
 
293
 
        if (!table)
294
 
                return;
295
 
 
296
481
        if (table->keep_flows)
297
482
                goto skip_flows;
298
483
 
299
484
        for (i = 0; i < table->n_buckets; i++) {
300
485
                struct sw_flow *flow;
301
486
                struct hlist_head *head = flex_array_get(table->buckets, i);
302
 
                struct hlist_node *node, *n;
 
487
                struct hlist_node *n;
303
488
                int ver = table->node_ver;
304
489
 
305
 
                hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
 
490
                hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
306
491
                        hlist_del_rcu(&flow->hash_node[ver]);
307
 
                        ovs_flow_free(flow);
 
492
                        ovs_flow_free(flow, false);
308
493
                }
309
494
        }
310
495
 
 
496
        BUG_ON(!list_empty(table->mask_list));
 
497
        kfree(table->mask_list);
 
498
 
311
499
skip_flows:
312
500
        free_buckets(table->buckets);
313
501
        kfree(table);
314
502
}
315
503
 
 
504
struct flow_table *ovs_flow_tbl_alloc(int new_size)
 
505
{
 
506
        struct flow_table *table = __flow_tbl_alloc(new_size);
 
507
 
 
508
        if (!table)
 
509
                return NULL;
 
510
 
 
511
        table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
 
512
        if (!table->mask_list) {
 
513
                table->keep_flows = true;
 
514
                __flow_tbl_destroy(table);
 
515
                return NULL;
 
516
        }
 
517
        INIT_LIST_HEAD(table->mask_list);
 
518
 
 
519
        return table;
 
520
}
 
521
 
316
522
static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
317
523
{
318
524
        struct flow_table *table = container_of(rcu, struct flow_table, rcu);
319
525
 
320
 
        ovs_flow_tbl_destroy(table);
 
526
        __flow_tbl_destroy(table);
321
527
}
322
528
 
323
 
void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
 
529
void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
324
530
{
325
531
        if (!table)
326
532
                return;
327
533
 
328
 
        call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
 
534
        if (deferred)
 
535
                call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
 
536
        else
 
537
                __flow_tbl_destroy(table);
329
538
}
330
539
 
331
 
struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
 
540
struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
332
541
{
333
542
        struct sw_flow *flow;
334
543
        struct hlist_head *head;
335
 
        struct hlist_node *n;
336
544
        int ver;
337
545
        int i;
338
546
 
340
548
        while (*bucket < table->n_buckets) {
341
549
                i = 0;
342
550
                head = flex_array_get(table->buckets, *bucket);
343
 
                hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
 
551
                hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
344
552
                        if (i < *last) {
345
553
                                i++;
346
554
                                continue;
355
563
        return NULL;
356
564
}
357
565
 
358
 
static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
 
566
static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
359
567
{
360
568
        struct hlist_head *head;
 
569
 
361
570
        head = find_bucket(table, flow->hash);
362
571
        hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
 
572
 
363
573
        table->count++;
364
574
}
365
575
 
375
585
        for (i = 0; i < old->n_buckets; i++) {
376
586
                struct sw_flow *flow;
377
587
                struct hlist_head *head;
378
 
                struct hlist_node *n;
379
588
 
380
589
                head = flex_array_get(old->buckets, i);
381
590
 
382
 
                hlist_for_each_entry(flow, n, head, hash_node[old_ver])
383
 
                        __flow_tbl_insert(new, flow);
 
591
                hlist_for_each_entry(flow, head, hash_node[old_ver])
 
592
                        __tbl_insert(new, flow);
384
593
        }
 
594
 
 
595
        new->mask_list = old->mask_list;
385
596
        old->keep_flows = true;
386
597
}
387
598
 
389
600
{
390
601
        struct flow_table *new_table;
391
602
 
392
 
        new_table = ovs_flow_tbl_alloc(n_buckets);
 
603
        new_table = __flow_tbl_alloc(n_buckets);
393
604
        if (!new_table)
394
605
                return ERR_PTR(-ENOMEM);
395
606
 
408
619
        return __flow_tbl_rehash(table, table->n_buckets * 2);
409
620
}
410
621
 
411
 
void ovs_flow_free(struct sw_flow *flow)
 
622
static void __flow_free(struct sw_flow *flow)
412
623
{
413
 
        if (unlikely(!flow))
414
 
                return;
415
 
 
416
624
        kfree((struct sf_flow_acts __force *)flow->sf_acts);
417
625
        kmem_cache_free(flow_cache, flow);
418
626
}
419
627
 
420
 
/* RCU callback used by ovs_flow_deferred_free. */
421
628
static void rcu_free_flow_callback(struct rcu_head *rcu)
422
629
{
423
630
        struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
424
631
 
425
 
        ovs_flow_free(flow);
 
632
        __flow_free(flow);
426
633
}
427
634
 
428
 
/* Schedules 'flow' to be freed after the next RCU grace period.
429
 
 * The caller must hold rcu_read_lock for this to be sensible. */
430
 
void ovs_flow_deferred_free(struct sw_flow *flow)
 
635
void ovs_flow_free(struct sw_flow *flow, bool deferred)
431
636
{
432
 
        call_rcu(&flow->rcu, rcu_free_flow_callback);
 
637
        if (!flow)
 
638
                return;
 
639
 
 
640
        ovs_sw_flow_mask_del_ref(flow->mask, deferred);
 
641
 
 
642
        if (deferred)
 
643
                call_rcu(&flow->rcu, rcu_free_flow_callback);
 
644
        else
 
645
                __flow_free(flow);
433
646
}
434
647
 
435
648
/* RCU callback used by ovs_flow_deferred_free_acts. */
484
697
        proto = *(__be16 *) skb->data;
485
698
        __skb_pull(skb, sizeof(__be16));
486
699
 
487
 
        if (ntohs(proto) >= 1536)
 
700
        if (ntohs(proto) >= ETH_P_802_3_MIN)
488
701
                return proto;
489
702
 
490
703
        if (skb->len < sizeof(struct llc_snap_hdr))
501
714
 
502
715
        __skb_pull(skb, sizeof(struct llc_snap_hdr));
503
716
 
504
 
        if (ntohs(llc->ethertype) >= 1536)
 
717
        if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
505
718
                return llc->ethertype;
506
719
 
507
720
        return htons(ETH_P_802_2);
508
721
}
509
722
 
510
723
static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
511
 
                        int *key_lenp, int nh_len)
 
724
                        int nh_len)
512
725
{
513
726
        struct icmp6hdr *icmp = icmp6_hdr(skb);
514
 
        int error = 0;
515
 
        int key_len;
516
727
 
517
728
        /* The ICMPv6 type and code fields use the 16-bit transport port
518
729
         * fields, so we need to store them in 16-bit network byte order.
519
730
         */
520
731
        key->ipv6.tp.src = htons(icmp->icmp6_type);
521
732
        key->ipv6.tp.dst = htons(icmp->icmp6_code);
522
 
        key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
523
733
 
524
734
        if (icmp->icmp6_code == 0 &&
525
735
            (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
528
738
                struct nd_msg *nd;
529
739
                int offset;
530
740
 
531
 
                key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
532
 
 
533
741
                /* In order to process neighbor discovery options, we need the
534
742
                 * entire packet.
535
743
                 */
536
744
                if (unlikely(icmp_len < sizeof(*nd)))
537
 
                        goto out;
538
 
                if (unlikely(skb_linearize(skb))) {
539
 
                        error = -ENOMEM;
540
 
                        goto out;
541
 
                }
 
745
                        return 0;
 
746
 
 
747
                if (unlikely(skb_linearize(skb)))
 
748
                        return -ENOMEM;
542
749
 
543
750
                nd = (struct nd_msg *)skb_transport_header(skb);
544
751
                key->ipv6.nd.target = nd->target;
545
 
                key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
546
752
 
547
753
                icmp_len -= sizeof(*nd);
548
754
                offset = 0;
552
758
                        int opt_len = nd_opt->nd_opt_len * 8;
553
759
 
554
760
                        if (unlikely(!opt_len || opt_len > icmp_len))
555
 
                                goto invalid;
 
761
                                return 0;
556
762
 
557
763
                        /* Store the link layer address if the appropriate
558
764
                         * option is provided.  It is considered an error if
577
783
                }
578
784
        }
579
785
 
580
 
        goto out;
 
786
        return 0;
581
787
 
582
788
invalid:
583
789
        memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
584
790
        memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
585
791
        memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
586
792
 
587
 
out:
588
 
        *key_lenp = key_len;
589
 
        return error;
 
793
        return 0;
590
794
}
591
795
 
592
796
/**
608
812
 *    - skb->network_header: just past the Ethernet header, or just past the
609
813
 *      VLAN header, to the first byte of the Ethernet payload.
610
814
 *
611
 
 *    - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
 
815
 *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
612
816
 *      on output, then just past the IP header, if one is present and
613
817
 *      of a correct length, otherwise the same as skb->network_header.
614
 
 *      For other key->dl_type values it is left untouched.
 
818
 *      For other key->eth.type values it is left untouched.
615
819
 */
616
 
int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
617
 
                 int *key_lenp)
 
820
int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
618
821
{
619
 
        int error = 0;
620
 
        int key_len = SW_FLOW_KEY_OFFSET(eth);
 
822
        int error;
621
823
        struct ethhdr *eth;
622
824
 
623
825
        memset(key, 0, sizeof(*key));
638
840
        memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
639
841
 
640
842
        __skb_pull(skb, 2 * ETH_ALEN);
 
843
        /* We are going to push all headers that we pull, so no need to
 
844
         * update skb->csum here. */
641
845
 
642
846
        if (vlan_tx_tag_present(skb))
643
847
                key->eth.tci = htons(vlan_get_tci(skb));
657
861
                struct iphdr *nh;
658
862
                __be16 offset;
659
863
 
660
 
                key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
661
 
 
662
864
                error = check_iphdr(skb);
663
865
                if (unlikely(error)) {
664
866
                        if (error == -EINVAL) {
665
867
                                skb->transport_header = skb->network_header;
666
868
                                error = 0;
667
869
                        }
668
 
                        goto out;
 
870
                        return error;
669
871
                }
670
872
 
671
873
                nh = ip_hdr(skb);
679
881
                offset = nh->frag_off & htons(IP_OFFSET);
680
882
                if (offset) {
681
883
                        key->ip.frag = OVS_FRAG_TYPE_LATER;
682
 
                        goto out;
 
884
                        return 0;
683
885
                }
684
886
                if (nh->frag_off & htons(IP_MF) ||
685
887
                         skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
687
889
 
688
890
                /* Transport layer. */
689
891
                if (key->ip.proto == IPPROTO_TCP) {
690
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
691
892
                        if (tcphdr_ok(skb)) {
692
893
                                struct tcphdr *tcp = tcp_hdr(skb);
693
894
                                key->ipv4.tp.src = tcp->source;
694
895
                                key->ipv4.tp.dst = tcp->dest;
695
896
                        }
696
897
                } else if (key->ip.proto == IPPROTO_UDP) {
697
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
698
898
                        if (udphdr_ok(skb)) {
699
899
                                struct udphdr *udp = udp_hdr(skb);
700
900
                                key->ipv4.tp.src = udp->source;
701
901
                                key->ipv4.tp.dst = udp->dest;
702
902
                        }
703
903
                } else if (key->ip.proto == IPPROTO_ICMP) {
704
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
705
904
                        if (icmphdr_ok(skb)) {
706
905
                                struct icmphdr *icmp = icmp_hdr(skb);
707
906
                                /* The ICMP type and code fields use the 16-bit
730
929
                        memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
731
930
                        memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
732
931
                        memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
733
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
734
932
                }
735
933
        } else if (key->eth.type == htons(ETH_P_IPV6)) {
736
934
                int nh_len;             /* IPv6 Header + Extensions */
737
935
 
738
 
                nh_len = parse_ipv6hdr(skb, key, &key_len);
 
936
                nh_len = parse_ipv6hdr(skb, key);
739
937
                if (unlikely(nh_len < 0)) {
740
 
                        if (nh_len == -EINVAL)
 
938
                        if (nh_len == -EINVAL) {
741
939
                                skb->transport_header = skb->network_header;
742
 
                        else
 
940
                                error = 0;
 
941
                        } else {
743
942
                                error = nh_len;
744
 
                        goto out;
 
943
                        }
 
944
                        return error;
745
945
                }
746
946
 
747
947
                if (key->ip.frag == OVS_FRAG_TYPE_LATER)
748
 
                        goto out;
 
948
                        return 0;
749
949
                if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
750
950
                        key->ip.frag = OVS_FRAG_TYPE_FIRST;
751
951
 
752
952
                /* Transport layer. */
753
953
                if (key->ip.proto == NEXTHDR_TCP) {
754
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
755
954
                        if (tcphdr_ok(skb)) {
756
955
                                struct tcphdr *tcp = tcp_hdr(skb);
757
956
                                key->ipv6.tp.src = tcp->source;
758
957
                                key->ipv6.tp.dst = tcp->dest;
759
958
                        }
760
959
                } else if (key->ip.proto == NEXTHDR_UDP) {
761
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
762
960
                        if (udphdr_ok(skb)) {
763
961
                                struct udphdr *udp = udp_hdr(skb);
764
962
                                key->ipv6.tp.src = udp->source;
765
963
                                key->ipv6.tp.dst = udp->dest;
766
964
                        }
767
965
                } else if (key->ip.proto == NEXTHDR_ICMP) {
768
 
                        key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
769
966
                        if (icmp6hdr_ok(skb)) {
770
 
                                error = parse_icmpv6(skb, key, &key_len, nh_len);
771
 
                                if (error < 0)
772
 
                                        goto out;
 
967
                                error = parse_icmpv6(skb, key, nh_len);
 
968
                                if (error)
 
969
                                        return error;
773
970
                        }
774
971
                }
775
972
        }
776
973
 
777
 
out:
778
 
        *key_lenp = key_len;
779
 
        return error;
 
974
        return 0;
780
975
}
781
976
 
782
977
static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
785
980
                      DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
786
981
}
787
982
 
788
 
static int flow_key_start(struct sw_flow_key *key)
 
983
static int flow_key_start(const struct sw_flow_key *key)
789
984
{
790
985
        if (key->tun_key.ipv4_dst)
791
986
                return 0;
793
988
                return offsetof(struct sw_flow_key, phy);
794
989
}
795
990
 
796
 
struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
797
 
                                struct sw_flow_key *key, int key_len)
798
 
{
799
 
        struct sw_flow *flow;
800
 
        struct hlist_node *n;
801
 
        struct hlist_head *head;
802
 
        u8 *_key;
 
991
static bool __cmp_key(const struct sw_flow_key *key1,
 
992
                const struct sw_flow_key *key2,  int key_start, int key_len)
 
993
{
 
994
        return !memcmp((u8 *)key1 + key_start,
 
995
                        (u8 *)key2 + key_start, (key_len - key_start));
 
996
}
 
997
 
 
998
static bool __flow_cmp_key(const struct sw_flow *flow,
 
999
                const struct sw_flow_key *key, int key_start, int key_len)
 
1000
{
 
1001
        return __cmp_key(&flow->key, key, key_start, key_len);
 
1002
}
 
1003
 
 
1004
static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
 
1005
                  const struct sw_flow_key *key, int key_start, int key_len)
 
1006
{
 
1007
        return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
 
1008
}
 
1009
 
 
1010
bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
 
1011
                const struct sw_flow_key *key, int key_len)
 
1012
{
803
1013
        int key_start;
 
1014
        key_start = flow_key_start(key);
 
1015
 
 
1016
        return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
 
1017
 
 
1018
}
 
1019
 
 
1020
struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
 
1021
                                       struct sw_flow_match *match)
 
1022
{
 
1023
        struct sw_flow_key *unmasked = match->key;
 
1024
        int key_len = match->range.end;
 
1025
        struct sw_flow *flow;
 
1026
 
 
1027
        flow = ovs_flow_lookup(table, unmasked);
 
1028
        if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
 
1029
                flow = NULL;
 
1030
 
 
1031
        return flow;
 
1032
}
 
1033
 
 
1034
static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
 
1035
                                    const struct sw_flow_key *flow_key,
 
1036
                                    struct sw_flow_mask *mask)
 
1037
{
 
1038
        struct sw_flow *flow;
 
1039
        struct hlist_head *head;
 
1040
        int key_start = mask->range.start;
 
1041
        int key_len = mask->range.end;
804
1042
        u32 hash;
805
 
 
806
 
        key_start = flow_key_start(key);
807
 
        hash = ovs_flow_hash(key, key_start, key_len);
808
 
 
809
 
        _key = (u8 *) key + key_start;
 
1043
        struct sw_flow_key masked_key;
 
1044
 
 
1045
        ovs_flow_key_mask(&masked_key, flow_key, mask);
 
1046
        hash = ovs_flow_hash(&masked_key, key_start, key_len);
810
1047
        head = find_bucket(table, hash);
811
 
        hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
812
 
 
813
 
                if (flow->hash == hash &&
814
 
                    !memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) {
 
1048
        hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
 
1049
                if (flow->mask == mask &&
 
1050
                    __flow_cmp_key(flow, &masked_key, key_start, key_len))
815
1051
                        return flow;
816
 
                }
817
1052
        }
818
1053
        return NULL;
819
1054
}
820
1055
 
821
 
void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
822
 
                         struct sw_flow_key *key, int key_len)
823
 
{
824
 
        flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len);
825
 
        memcpy(&flow->key, key, sizeof(flow->key));
826
 
        __flow_tbl_insert(table, flow);
827
 
}
828
 
 
829
 
void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
830
 
{
 
1056
struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
 
1057
                                const struct sw_flow_key *key)
 
1058
{
 
1059
        struct sw_flow *flow = NULL;
 
1060
        struct sw_flow_mask *mask;
 
1061
 
 
1062
        list_for_each_entry_rcu(mask, tbl->mask_list, list) {
 
1063
                flow = ovs_masked_flow_lookup(tbl, key, mask);
 
1064
                if (flow)  /* Found */
 
1065
                        break;
 
1066
        }
 
1067
 
 
1068
        return flow;
 
1069
}
 
1070
 
 
1071
 
 
1072
void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
 
1073
{
 
1074
        flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
 
1075
                        flow->mask->range.end);
 
1076
        __tbl_insert(table, flow);
 
1077
}
 
1078
 
 
1079
void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
 
1080
{
 
1081
        BUG_ON(table->count == 0);
831
1082
        hlist_del_rcu(&flow->hash_node[table->node_ver]);
832
1083
        table->count--;
833
 
        BUG_ON(table->count < 0);
834
1084
}
835
1085
 
836
1086
/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
853
1103
        [OVS_KEY_ATTR_TUNNEL] = -1,
854
1104
};
855
1105
 
856
 
static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
857
 
                                  const struct nlattr *a[], u64 *attrs)
858
 
{
859
 
        const struct ovs_key_icmp *icmp_key;
860
 
        const struct ovs_key_tcp *tcp_key;
861
 
        const struct ovs_key_udp *udp_key;
862
 
 
863
 
        switch (swkey->ip.proto) {
864
 
        case IPPROTO_TCP:
865
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
866
 
                        return -EINVAL;
867
 
                *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
868
 
 
869
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
870
 
                tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
871
 
                swkey->ipv4.tp.src = tcp_key->tcp_src;
872
 
                swkey->ipv4.tp.dst = tcp_key->tcp_dst;
873
 
                break;
874
 
 
875
 
        case IPPROTO_UDP:
876
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
877
 
                        return -EINVAL;
878
 
                *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
879
 
 
880
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
881
 
                udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
882
 
                swkey->ipv4.tp.src = udp_key->udp_src;
883
 
                swkey->ipv4.tp.dst = udp_key->udp_dst;
884
 
                break;
885
 
 
886
 
        case IPPROTO_ICMP:
887
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
888
 
                        return -EINVAL;
889
 
                *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
890
 
 
891
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
892
 
                icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
893
 
                swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
894
 
                swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
895
 
                break;
896
 
        }
897
 
 
898
 
        return 0;
899
 
}
900
 
 
901
 
static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
902
 
                                  const struct nlattr *a[], u64 *attrs)
903
 
{
904
 
        const struct ovs_key_icmpv6 *icmpv6_key;
905
 
        const struct ovs_key_tcp *tcp_key;
906
 
        const struct ovs_key_udp *udp_key;
907
 
 
908
 
        switch (swkey->ip.proto) {
909
 
        case IPPROTO_TCP:
910
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
911
 
                        return -EINVAL;
912
 
                *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
913
 
 
914
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
915
 
                tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
916
 
                swkey->ipv6.tp.src = tcp_key->tcp_src;
917
 
                swkey->ipv6.tp.dst = tcp_key->tcp_dst;
918
 
                break;
919
 
 
920
 
        case IPPROTO_UDP:
921
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
922
 
                        return -EINVAL;
923
 
                *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
924
 
 
925
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
926
 
                udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
927
 
                swkey->ipv6.tp.src = udp_key->udp_src;
928
 
                swkey->ipv6.tp.dst = udp_key->udp_dst;
929
 
                break;
930
 
 
931
 
        case IPPROTO_ICMPV6:
932
 
                if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
933
 
                        return -EINVAL;
934
 
                *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
935
 
 
936
 
                *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
937
 
                icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
938
 
                swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
939
 
                swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
940
 
 
941
 
                if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
942
 
                    swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
943
 
                        const struct ovs_key_nd *nd_key;
944
 
 
945
 
                        if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
946
 
                                return -EINVAL;
947
 
                        *attrs &= ~(1 << OVS_KEY_ATTR_ND);
948
 
 
949
 
                        *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
950
 
                        nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
951
 
                        memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
952
 
                               sizeof(swkey->ipv6.nd.target));
953
 
                        memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
954
 
                        memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
955
 
                }
956
 
                break;
957
 
        }
958
 
 
959
 
        return 0;
960
 
}
961
 
 
962
 
static int parse_flow_nlattrs(const struct nlattr *attr,
963
 
                              const struct nlattr *a[], u64 *attrsp)
 
1106
static bool is_all_zero(const u8 *fp, size_t size)
 
1107
{
 
1108
        int i;
 
1109
 
 
1110
        if (!fp)
 
1111
                return false;
 
1112
 
 
1113
        for (i = 0; i < size; i++)
 
1114
                if (fp[i])
 
1115
                        return false;
 
1116
 
 
1117
        return true;
 
1118
}
 
1119
 
 
1120
static int __parse_flow_nlattrs(const struct nlattr *attr,
 
1121
                              const struct nlattr *a[],
 
1122
                              u64 *attrsp, bool nz)
964
1123
{
965
1124
        const struct nlattr *nla;
966
1125
        u64 attrs;
967
1126
        int rem;
968
1127
 
969
 
        attrs = 0;
 
1128
        attrs = *attrsp;
970
1129
        nla_for_each_nested(nla, attr, rem) {
971
1130
                u16 type = nla_type(nla);
972
1131
                int expected_len;
973
1132
 
974
 
                if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
 
1133
                if (type > OVS_KEY_ATTR_MAX) {
 
1134
                        OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
 
1135
                                  type, OVS_KEY_ATTR_MAX);
 
1136
                }
 
1137
 
 
1138
                if (attrs & (1ULL << type)) {
 
1139
                        OVS_NLERR("Duplicate key attribute (type %d).\n", type);
975
1140
                        return -EINVAL;
 
1141
                }
976
1142
 
977
1143
                expected_len = ovs_key_lens[type];
978
 
                if (nla_len(nla) != expected_len && expected_len != -1)
 
1144
                if (nla_len(nla) != expected_len && expected_len != -1) {
 
1145
                        OVS_NLERR("Key attribute has unexpected length (type=%d"
 
1146
                                  ", length=%d, expected=%d).\n", type,
 
1147
                                  nla_len(nla), expected_len);
979
1148
                        return -EINVAL;
 
1149
                }
980
1150
 
981
 
                attrs |= 1ULL << type;
982
 
                a[type] = nla;
 
1151
                if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
 
1152
                        attrs |= 1ULL << type;
 
1153
                        a[type] = nla;
 
1154
                }
983
1155
        }
984
 
        if (rem)
 
1156
        if (rem) {
 
1157
                OVS_NLERR("Message has %d unknown bytes.\n", rem);
985
1158
                return -EINVAL;
 
1159
        }
986
1160
 
987
1161
        *attrsp = attrs;
988
1162
        return 0;
989
1163
}
990
1164
 
 
1165
static int parse_flow_mask_nlattrs(const struct nlattr *attr,
 
1166
                              const struct nlattr *a[], u64 *attrsp)
 
1167
{
 
1168
        return __parse_flow_nlattrs(attr, a, attrsp, true);
 
1169
}
 
1170
 
 
1171
static int parse_flow_nlattrs(const struct nlattr *attr,
 
1172
                              const struct nlattr *a[], u64 *attrsp)
 
1173
{
 
1174
        return __parse_flow_nlattrs(attr, a, attrsp, false);
 
1175
}
 
1176
 
991
1177
int ipv4_tun_from_nlattr(const struct nlattr *attr,
992
 
                         struct ovs_key_ipv4_tunnel *tun_key)
 
1178
                         struct sw_flow_match *match, bool is_mask)
993
1179
{
994
1180
        struct nlattr *a;
995
1181
        int rem;
996
1182
        bool ttl = false;
997
 
 
998
 
        memset(tun_key, 0, sizeof(*tun_key));
 
1183
        __be16 tun_flags = 0;
999
1184
 
1000
1185
        nla_for_each_nested(a, attr, rem) {
1001
1186
                int type = nla_type(a);
1009
1194
                        [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1010
1195
                };
1011
1196
 
1012
 
                if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
1013
 
                        ovs_tunnel_key_lens[type] != nla_len(a))
1014
 
                        return -EINVAL;
 
1197
                if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
 
1198
                        OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
 
1199
                        type, OVS_TUNNEL_KEY_ATTR_MAX);
 
1200
                        return -EINVAL;
 
1201
                }
 
1202
 
 
1203
                if (ovs_tunnel_key_lens[type] != nla_len(a)) {
 
1204
                        OVS_NLERR("IPv4 tunnel attribute type has unexpected "
 
1205
                                  " legnth (type=%d, length=%d, expected=%d).\n",
 
1206
                                  type, nla_len(a), ovs_tunnel_key_lens[type]);
 
1207
                        return -EINVAL;
 
1208
                }
1015
1209
 
1016
1210
                switch (type) {
1017
1211
                case OVS_TUNNEL_KEY_ATTR_ID:
1018
 
                        tun_key->tun_id = nla_get_be64(a);
1019
 
                        tun_key->tun_flags |= OVS_TNL_F_KEY;
 
1212
                        SW_FLOW_KEY_PUT(match, tun_key.tun_id,
 
1213
                                        nla_get_be64(a), is_mask);
 
1214
                        tun_flags |= TUNNEL_KEY;
1020
1215
                        break;
1021
1216
                case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1022
 
                        tun_key->ipv4_src = nla_get_be32(a);
 
1217
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
 
1218
                                        nla_get_be32(a), is_mask);
1023
1219
                        break;
1024
1220
                case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1025
 
                        tun_key->ipv4_dst = nla_get_be32(a);
 
1221
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
 
1222
                                        nla_get_be32(a), is_mask);
1026
1223
                        break;
1027
1224
                case OVS_TUNNEL_KEY_ATTR_TOS:
1028
 
                        tun_key->ipv4_tos = nla_get_u8(a);
 
1225
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
 
1226
                                        nla_get_u8(a), is_mask);
1029
1227
                        break;
1030
1228
                case OVS_TUNNEL_KEY_ATTR_TTL:
1031
 
                        tun_key->ipv4_ttl = nla_get_u8(a);
 
1229
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
 
1230
                                        nla_get_u8(a), is_mask);
1032
1231
                        ttl = true;
1033
1232
                        break;
1034
1233
                case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1035
 
                        tun_key->tun_flags |= OVS_TNL_F_DONT_FRAGMENT;
 
1234
                        tun_flags |= TUNNEL_DONT_FRAGMENT;
1036
1235
                        break;
1037
1236
                case OVS_TUNNEL_KEY_ATTR_CSUM:
1038
 
                        tun_key->tun_flags |= OVS_TNL_F_CSUM;
 
1237
                        tun_flags |= TUNNEL_CSUM;
1039
1238
                        break;
1040
1239
                default:
1041
1240
                        return -EINVAL;
1042
 
 
1043
 
                }
1044
 
        }
1045
 
        if (rem > 0)
1046
 
                return -EINVAL;
1047
 
 
1048
 
        if (!tun_key->ipv4_dst)
1049
 
                return -EINVAL;
1050
 
 
1051
 
        if (!ttl)
1052
 
                return -EINVAL;
 
1241
                }
 
1242
        }
 
1243
 
 
1244
        SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
 
1245
 
 
1246
        if (rem > 0) {
 
1247
                OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
 
1248
                return -EINVAL;
 
1249
        }
 
1250
 
 
1251
        if (!is_mask) {
 
1252
                if (!match->key->tun_key.ipv4_dst) {
 
1253
                        OVS_NLERR("IPv4 tunnel destination address is zero.\n");
 
1254
                        return -EINVAL;
 
1255
                }
 
1256
 
 
1257
                if (!ttl) {
 
1258
                        OVS_NLERR("IPv4 tunnel TTL not specified.\n");
 
1259
                        return -EINVAL;
 
1260
                }
 
1261
        }
1053
1262
 
1054
1263
        return 0;
1055
1264
}
1056
1265
 
1057
1266
int ipv4_tun_to_nlattr(struct sk_buff *skb,
1058
 
                        const struct ovs_key_ipv4_tunnel *tun_key)
 
1267
                        const struct ovs_key_ipv4_tunnel *tun_key,
 
1268
                        const struct ovs_key_ipv4_tunnel *output)
1059
1269
{
1060
1270
        struct nlattr *nla;
1061
1271
 
1063
1273
        if (!nla)
1064
1274
                return -EMSGSIZE;
1065
1275
 
1066
 
        if (tun_key->tun_flags & OVS_TNL_F_KEY &&
1067
 
            nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id))
1068
 
                return -EMSGSIZE;
1069
 
        if (tun_key->ipv4_src &&
1070
 
            nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src))
1071
 
                return -EMSGSIZE;
1072
 
        if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst))
1073
 
                return -EMSGSIZE;
1074
 
        if (tun_key->ipv4_tos &&
1075
 
            nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos))
1076
 
                return -EMSGSIZE;
1077
 
        if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl))
1078
 
                return -EMSGSIZE;
1079
 
        if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) &&
 
1276
        if (output->tun_flags & TUNNEL_KEY &&
 
1277
            nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
 
1278
                return -EMSGSIZE;
 
1279
        if (output->ipv4_src &&
 
1280
                nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
 
1281
                return -EMSGSIZE;
 
1282
        if (output->ipv4_dst &&
 
1283
                nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
 
1284
                return -EMSGSIZE;
 
1285
        if (output->ipv4_tos &&
 
1286
                nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
 
1287
                return -EMSGSIZE;
 
1288
        if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
 
1289
                return -EMSGSIZE;
 
1290
        if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1080
1291
                nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1081
1292
                return -EMSGSIZE;
1082
 
        if ((tun_key->tun_flags & OVS_TNL_F_CSUM) &&
 
1293
        if ((output->tun_flags & TUNNEL_CSUM) &&
1083
1294
                nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1084
1295
                return -EMSGSIZE;
1085
1296
 
1087
1298
        return 0;
1088
1299
}
1089
1300
 
1090
 
/**
1091
 
 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1092
 
 * @swkey: receives the extracted flow key.
1093
 
 * @key_lenp: number of bytes used in @swkey.
1094
 
 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1095
 
 * sequence.
1096
 
 */
1097
 
int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1098
 
                      const struct nlattr *attr)
 
1301
 
 
1302
static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
 
1303
                const struct nlattr **a, bool is_mask)
1099
1304
{
1100
 
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1101
 
        const struct ovs_key_ethernet *eth_key;
1102
 
        int key_len;
1103
 
        u64 attrs;
1104
 
        int err;
1105
 
 
1106
 
        memset(swkey, 0, sizeof(struct sw_flow_key));
1107
 
        key_len = SW_FLOW_KEY_OFFSET(eth);
1108
 
 
1109
 
        err = parse_flow_nlattrs(attr, a, &attrs);
1110
 
        if (err)
1111
 
                return err;
1112
 
 
1113
 
        /* Metadata attributes. */
1114
 
        if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1115
 
                swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1116
 
                attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
 
1305
        if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
 
1306
                SW_FLOW_KEY_PUT(match, phy.priority,
 
1307
                          nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
 
1308
                *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1117
1309
        }
1118
 
        if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
 
1310
 
 
1311
        if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1119
1312
                u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1120
 
                if (in_port >= DP_MAX_PORTS)
 
1313
 
 
1314
                if (is_mask)
 
1315
                        in_port = 0xffffffff; /* Always exact match in_port. */
 
1316
                else if (in_port >= DP_MAX_PORTS)
1121
1317
                        return -EINVAL;
1122
 
                swkey->phy.in_port = in_port;
1123
 
                attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1124
 
        } else {
1125
 
                swkey->phy.in_port = DP_MAX_PORTS;
 
1318
 
 
1319
                SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
 
1320
                *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
 
1321
        } else if (!is_mask) {
 
1322
                SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1126
1323
        }
1127
 
        if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
 
1324
 
 
1325
        if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1128
1326
                uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1129
1327
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1130
 
                if (mark != 0)
 
1328
                if (!is_mask && mark != 0) {
 
1329
                        OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1131
1330
                        return -EINVAL;
 
1331
                }
1132
1332
#endif
1133
 
                swkey->phy.skb_mark = mark;
1134
 
                attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1135
 
        }
1136
 
 
1137
 
        if (attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1138
 
                err = ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
1139
 
                if (err)
1140
 
                        return err;
1141
 
 
1142
 
                attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1143
 
        }
1144
 
 
1145
 
        /* Data attributes. */
1146
 
        if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1147
 
                return -EINVAL;
1148
 
        attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1149
 
 
1150
 
        eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1151
 
        memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1152
 
        memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1153
 
 
1154
 
        if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1155
 
            nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1156
 
                const struct nlattr *encap;
 
1333
                SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
 
1334
                *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
 
1335
        }
 
1336
        if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
 
1337
                if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
 
1338
                                        is_mask))
 
1339
                        return -EINVAL;
 
1340
                *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
 
1341
        }
 
1342
        return 0;
 
1343
}
 
1344
 
 
1345
static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
 
1346
                const struct nlattr **a, bool is_mask)
 
1347
{
 
1348
        int err;
 
1349
        u64 orig_attrs = attrs;
 
1350
 
 
1351
        err = metadata_from_nlattrs(match, &attrs, a, is_mask);
 
1352
        if (err)
 
1353
                return err;
 
1354
 
 
1355
        if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
 
1356
                const struct ovs_key_ethernet *eth_key;
 
1357
 
 
1358
                eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
 
1359
                SW_FLOW_KEY_MEMCPY(match, eth.src,
 
1360
                                eth_key->eth_src, ETH_ALEN, is_mask);
 
1361
                SW_FLOW_KEY_MEMCPY(match, eth.dst,
 
1362
                                eth_key->eth_dst, ETH_ALEN, is_mask);
 
1363
                attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
 
1364
        }
 
1365
 
 
1366
        if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1157
1367
                __be16 tci;
1158
1368
 
1159
 
                if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1160
 
                              (1 << OVS_KEY_ATTR_ETHERTYPE) |
1161
 
                              (1 << OVS_KEY_ATTR_ENCAP)))
1162
 
                        return -EINVAL;
1163
 
 
1164
 
                encap = a[OVS_KEY_ATTR_ENCAP];
1165
1369
                tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1166
 
                if (tci & htons(VLAN_TAG_PRESENT)) {
1167
 
                        swkey->eth.tci = tci;
1168
 
 
1169
 
                        err = parse_flow_nlattrs(encap, a, &attrs);
1170
 
                        if (err)
1171
 
                                return err;
1172
 
                } else if (!tci) {
1173
 
                        /* Corner case for truncated 802.1Q header. */
1174
 
                        if (nla_len(encap))
1175
 
                                return -EINVAL;
1176
 
 
1177
 
                        swkey->eth.type = htons(ETH_P_8021Q);
1178
 
                        *key_lenp = key_len;
1179
 
                        return 0;
1180
 
                } else {
1181
 
                        return -EINVAL;
1182
 
                }
1183
 
        }
1184
 
 
1185
 
        if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1186
 
                swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1187
 
                if (ntohs(swkey->eth.type) < 1536)
1188
 
                        return -EINVAL;
1189
 
                attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1190
 
        } else {
1191
 
                swkey->eth.type = htons(ETH_P_802_2);
1192
 
        }
1193
 
 
1194
 
        if (swkey->eth.type == htons(ETH_P_IP)) {
 
1370
                if (!(tci & htons(VLAN_TAG_PRESENT))) {
 
1371
                        if (is_mask)
 
1372
                                OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
 
1373
                        else
 
1374
                                OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
 
1375
 
 
1376
                        return -EINVAL;
 
1377
                }
 
1378
 
 
1379
                SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
 
1380
                attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
 
1381
        } else if (!is_mask)
 
1382
                SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
 
1383
 
 
1384
        if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
 
1385
                __be16 eth_type;
 
1386
 
 
1387
                eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
 
1388
                if (is_mask) {
 
1389
                        /* Always exact match EtherType. */
 
1390
                        eth_type = htons(0xffff);
 
1391
                } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
 
1392
                        OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
 
1393
                                        ntohs(eth_type), ETH_P_802_3_MIN);
 
1394
                        return -EINVAL;
 
1395
                }
 
1396
 
 
1397
                SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
 
1398
                attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
 
1399
        } else if (!is_mask) {
 
1400
                SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
 
1401
        }
 
1402
 
 
1403
        if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1195
1404
                const struct ovs_key_ipv4 *ipv4_key;
1196
1405
 
1197
 
                if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1198
 
                        return -EINVAL;
1199
 
                attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1200
 
 
1201
 
                key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1202
1406
                ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1203
 
                if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
 
1407
                if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
 
1408
                        OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
 
1409
                                ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1204
1410
                        return -EINVAL;
1205
 
                swkey->ip.proto = ipv4_key->ipv4_proto;
1206
 
                swkey->ip.tos = ipv4_key->ipv4_tos;
1207
 
                swkey->ip.ttl = ipv4_key->ipv4_ttl;
1208
 
                swkey->ip.frag = ipv4_key->ipv4_frag;
1209
 
                swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1210
 
                swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
 
1411
                }
 
1412
                SW_FLOW_KEY_PUT(match, ip.proto,
 
1413
                                ipv4_key->ipv4_proto, is_mask);
 
1414
                SW_FLOW_KEY_PUT(match, ip.tos,
 
1415
                                ipv4_key->ipv4_tos, is_mask);
 
1416
                SW_FLOW_KEY_PUT(match, ip.ttl,
 
1417
                                ipv4_key->ipv4_ttl, is_mask);
 
1418
                SW_FLOW_KEY_PUT(match, ip.frag,
 
1419
                                ipv4_key->ipv4_frag, is_mask);
 
1420
                SW_FLOW_KEY_PUT(match, ipv4.addr.src,
 
1421
                                ipv4_key->ipv4_src, is_mask);
 
1422
                SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
 
1423
                                ipv4_key->ipv4_dst, is_mask);
 
1424
                attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
 
1425
        }
1211
1426
 
1212
 
                if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1213
 
                        err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1214
 
                        if (err)
1215
 
                                return err;
1216
 
                }
1217
 
        } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
 
1427
        if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1218
1428
                const struct ovs_key_ipv6 *ipv6_key;
1219
1429
 
1220
 
                if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1221
 
                        return -EINVAL;
1222
 
                attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1223
 
 
1224
 
                key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1225
1430
                ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1226
 
                if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
 
1431
                if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
 
1432
                        OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
 
1433
                                ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1227
1434
                        return -EINVAL;
1228
 
                swkey->ipv6.label = ipv6_key->ipv6_label;
1229
 
                swkey->ip.proto = ipv6_key->ipv6_proto;
1230
 
                swkey->ip.tos = ipv6_key->ipv6_tclass;
1231
 
                swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1232
 
                swkey->ip.frag = ipv6_key->ipv6_frag;
1233
 
                memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1234
 
                       sizeof(swkey->ipv6.addr.src));
1235
 
                memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1236
 
                       sizeof(swkey->ipv6.addr.dst));
1237
 
 
1238
 
                if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1239
 
                        err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1240
 
                        if (err)
1241
 
                                return err;
1242
1435
                }
1243
 
        } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1244
 
                   swkey->eth.type == htons(ETH_P_RARP)) {
 
1436
                SW_FLOW_KEY_PUT(match, ipv6.label,
 
1437
                                ipv6_key->ipv6_label, is_mask);
 
1438
                SW_FLOW_KEY_PUT(match, ip.proto,
 
1439
                                ipv6_key->ipv6_proto, is_mask);
 
1440
                SW_FLOW_KEY_PUT(match, ip.tos,
 
1441
                                ipv6_key->ipv6_tclass, is_mask);
 
1442
                SW_FLOW_KEY_PUT(match, ip.ttl,
 
1443
                                ipv6_key->ipv6_hlimit, is_mask);
 
1444
                SW_FLOW_KEY_PUT(match, ip.frag,
 
1445
                                ipv6_key->ipv6_frag, is_mask);
 
1446
                SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
 
1447
                                ipv6_key->ipv6_src,
 
1448
                                sizeof(match->key->ipv6.addr.src),
 
1449
                                is_mask);
 
1450
                SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
 
1451
                                ipv6_key->ipv6_dst,
 
1452
                                sizeof(match->key->ipv6.addr.dst),
 
1453
                                is_mask);
 
1454
 
 
1455
                attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
 
1456
        }
 
1457
 
 
1458
        if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1245
1459
                const struct ovs_key_arp *arp_key;
1246
1460
 
1247
 
                if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1248
 
                        return -EINVAL;
1249
 
                attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1250
 
 
1251
 
                key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1252
1461
                arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1253
 
                swkey->ipv4.addr.src = arp_key->arp_sip;
1254
 
                swkey->ipv4.addr.dst = arp_key->arp_tip;
1255
 
                if (arp_key->arp_op & htons(0xff00))
 
1462
                if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
 
1463
                        OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
 
1464
                                  arp_key->arp_op);
1256
1465
                        return -EINVAL;
1257
 
                swkey->ip.proto = ntohs(arp_key->arp_op);
1258
 
                memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1259
 
                memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1260
 
        }
1261
 
 
1262
 
        if (attrs)
1263
 
                return -EINVAL;
1264
 
        *key_lenp = key_len;
 
1466
                }
 
1467
 
 
1468
                SW_FLOW_KEY_PUT(match, ipv4.addr.src,
 
1469
                                arp_key->arp_sip, is_mask);
 
1470
                SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
 
1471
                        arp_key->arp_tip, is_mask);
 
1472
                SW_FLOW_KEY_PUT(match, ip.proto,
 
1473
                                ntohs(arp_key->arp_op), is_mask);
 
1474
                SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
 
1475
                                arp_key->arp_sha, ETH_ALEN, is_mask);
 
1476
                SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
 
1477
                                arp_key->arp_tha, ETH_ALEN, is_mask);
 
1478
 
 
1479
                attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
 
1480
        }
 
1481
 
 
1482
        if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
 
1483
                const struct ovs_key_tcp *tcp_key;
 
1484
 
 
1485
                tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
 
1486
                if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
 
1487
                        SW_FLOW_KEY_PUT(match, ipv4.tp.src,
 
1488
                                        tcp_key->tcp_src, is_mask);
 
1489
                        SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
 
1490
                                        tcp_key->tcp_dst, is_mask);
 
1491
                } else {
 
1492
                        SW_FLOW_KEY_PUT(match, ipv6.tp.src,
 
1493
                                        tcp_key->tcp_src, is_mask);
 
1494
                        SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
 
1495
                                        tcp_key->tcp_dst, is_mask);
 
1496
                }
 
1497
                attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
 
1498
        }
 
1499
 
 
1500
        if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
 
1501
                const struct ovs_key_udp *udp_key;
 
1502
 
 
1503
                udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
 
1504
                if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
 
1505
                        SW_FLOW_KEY_PUT(match, ipv4.tp.src,
 
1506
                                        udp_key->udp_src, is_mask);
 
1507
                        SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
 
1508
                                        udp_key->udp_dst, is_mask);
 
1509
                } else {
 
1510
                        SW_FLOW_KEY_PUT(match, ipv6.tp.src,
 
1511
                                        udp_key->udp_src, is_mask);
 
1512
                        SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
 
1513
                                        udp_key->udp_dst, is_mask);
 
1514
                }
 
1515
                attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
 
1516
        }
 
1517
 
 
1518
        if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
 
1519
                const struct ovs_key_icmp *icmp_key;
 
1520
 
 
1521
                icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
 
1522
                SW_FLOW_KEY_PUT(match, ipv4.tp.src,
 
1523
                                htons(icmp_key->icmp_type), is_mask);
 
1524
                SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
 
1525
                                htons(icmp_key->icmp_code), is_mask);
 
1526
                attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
 
1527
        }
 
1528
 
 
1529
        if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
 
1530
                const struct ovs_key_icmpv6 *icmpv6_key;
 
1531
 
 
1532
                icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
 
1533
                SW_FLOW_KEY_PUT(match, ipv6.tp.src,
 
1534
                                htons(icmpv6_key->icmpv6_type), is_mask);
 
1535
                SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
 
1536
                                htons(icmpv6_key->icmpv6_code), is_mask);
 
1537
                attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
 
1538
        }
 
1539
 
 
1540
        if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
 
1541
                const struct ovs_key_nd *nd_key;
 
1542
 
 
1543
                nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
 
1544
                SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
 
1545
                        nd_key->nd_target,
 
1546
                        sizeof(match->key->ipv6.nd.target),
 
1547
                        is_mask);
 
1548
                SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
 
1549
                        nd_key->nd_sll, ETH_ALEN, is_mask);
 
1550
                SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
 
1551
                                nd_key->nd_tll, ETH_ALEN, is_mask);
 
1552
                attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
 
1553
        }
 
1554
 
 
1555
        if (attrs != 0)
 
1556
                return -EINVAL;
 
1557
 
 
1558
        return 0;
 
1559
}
 
1560
 
 
1561
/**
 
1562
 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
 
1563
 * mask. In case the 'mask' is NULL, the flow is treated as exact match
 
1564
 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
 
1565
 * does not include any don't care bit.
 
1566
 * @match: receives the extracted flow match information.
 
1567
 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
 
1568
 * sequence. The fields should of the packet that triggered the creation
 
1569
 * of this flow.
 
1570
 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
 
1571
 * attribute specifies the mask field of the wildcarded flow.
 
1572
 */
 
1573
int ovs_match_from_nlattrs(struct sw_flow_match *match,
 
1574
                           const struct nlattr *key,
 
1575
                           const struct nlattr *mask)
 
1576
{
 
1577
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
 
1578
        const struct nlattr *encap;
 
1579
        u64 key_attrs = 0;
 
1580
        u64 mask_attrs = 0;
 
1581
        bool encap_valid = false;
 
1582
        int err;
 
1583
 
 
1584
        err = parse_flow_nlattrs(key, a, &key_attrs);
 
1585
        if (err)
 
1586
                return err;
 
1587
 
 
1588
        if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
 
1589
                encap = a[OVS_KEY_ATTR_ENCAP];
 
1590
                key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
 
1591
                if (nla_len(encap)) {
 
1592
                        __be16 eth_type = 0; /* ETH_P_8021Q */
 
1593
 
 
1594
                        if (a[OVS_KEY_ATTR_ETHERTYPE])
 
1595
                                eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
 
1596
 
 
1597
                        if  ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
 
1598
                                encap_valid = true;
 
1599
                                key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
 
1600
                                err = parse_flow_nlattrs(encap, a, &key_attrs);
 
1601
                        } else {
 
1602
                                OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
 
1603
                                err = -EINVAL;
 
1604
                        }
 
1605
 
 
1606
                        if (err)
 
1607
                                return err;
 
1608
                }
 
1609
        }
 
1610
 
 
1611
        err = ovs_key_from_nlattrs(match, key_attrs, a, false);
 
1612
        if (err)
 
1613
                return err;
 
1614
 
 
1615
        if (mask) {
 
1616
                err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
 
1617
                if (err)
 
1618
                        return err;
 
1619
 
 
1620
                if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
 
1621
                        __be16 eth_type = 0;
 
1622
 
 
1623
                        mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
 
1624
                        if (a[OVS_KEY_ATTR_ETHERTYPE])
 
1625
                                eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
 
1626
                        if (eth_type == htons(0xffff)) {
 
1627
                                mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
 
1628
                                encap = a[OVS_KEY_ATTR_ENCAP];
 
1629
                                err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
 
1630
                        } else {
 
1631
                                OVS_NLERR("VLAN frames must have an exact match"
 
1632
                                         " on the TPID (mask=%x).\n",
 
1633
                                         ntohs(eth_type));
 
1634
                                err = -EINVAL;
 
1635
                        }
 
1636
 
 
1637
                        if (err)
 
1638
                                return err;
 
1639
                }
 
1640
 
 
1641
                err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
 
1642
                if (err)
 
1643
                        return err;
 
1644
        } else {
 
1645
                /* Populate exact match flow's key mask. */
 
1646
                if (match->mask)
 
1647
                        ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
 
1648
        }
 
1649
 
 
1650
        if (!ovs_match_validate(match, key_attrs, mask_attrs))
 
1651
                return -EINVAL;
1265
1652
 
1266
1653
        return 0;
1267
1654
}
1268
1655
 
1269
1656
/**
1270
1657
 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1271
 
 * @in_port: receives the extracted input port.
1272
 
 * @tun_id: receives the extracted tunnel ID.
1273
 
 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
 
1658
 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
 
1659
 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1274
1660
 * sequence.
1275
1661
 *
1276
1662
 * This parses a series of Netlink attributes that form a flow key, which must
1279
1665
 * extracted from the packet itself.
1280
1666
 */
1281
1667
 
1282
 
int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr)
 
1668
int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
 
1669
                const struct nlattr *attr)
1283
1670
{
1284
1671
        struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1285
 
        const struct nlattr *nla;
1286
 
        int rem;
 
1672
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
 
1673
        u64 attrs = 0;
 
1674
        int err;
 
1675
        struct sw_flow_match match;
1287
1676
 
1288
1677
        flow->key.phy.in_port = DP_MAX_PORTS;
1289
1678
        flow->key.phy.priority = 0;
1290
1679
        flow->key.phy.skb_mark = 0;
1291
1680
        memset(tun_key, 0, sizeof(flow->key.tun_key));
1292
1681
 
1293
 
        nla_for_each_nested(nla, attr, rem) {
1294
 
                int type = nla_type(nla);
1295
 
 
1296
 
                if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1297
 
                        int err;
1298
 
 
1299
 
                        if (nla_len(nla) != ovs_key_lens[type])
1300
 
                                return -EINVAL;
1301
 
 
1302
 
                        switch (type) {
1303
 
                        case OVS_KEY_ATTR_PRIORITY:
1304
 
                                flow->key.phy.priority = nla_get_u32(nla);
1305
 
                                break;
1306
 
 
1307
 
                        case OVS_KEY_ATTR_TUNNEL:
1308
 
                                err = ipv4_tun_from_nlattr(nla, tun_key);
1309
 
                                if (err)
1310
 
                                        return err;
1311
 
                                break;
1312
 
 
1313
 
                        case OVS_KEY_ATTR_IN_PORT:
1314
 
                                if (nla_get_u32(nla) >= DP_MAX_PORTS)
1315
 
                                        return -EINVAL;
1316
 
                                flow->key.phy.in_port = nla_get_u32(nla);
1317
 
                                break;
1318
 
 
1319
 
                        case OVS_KEY_ATTR_SKB_MARK:
1320
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1321
 
                                if (nla_get_u32(nla) != 0)
1322
 
                                        return -EINVAL;
1323
 
#endif
1324
 
                                flow->key.phy.skb_mark = nla_get_u32(nla);
1325
 
                                break;
1326
 
                        }
1327
 
                }
1328
 
        }
1329
 
        if (rem)
 
1682
        err = parse_flow_nlattrs(attr, a, &attrs);
 
1683
        if (err)
1330
1684
                return -EINVAL;
1331
1685
 
1332
 
        flow->hash = ovs_flow_hash(&flow->key,
1333
 
                                   flow_key_start(&flow->key), key_len);
 
1686
        memset(&match, 0, sizeof(match));
 
1687
        match.key = &flow->key;
 
1688
 
 
1689
        err = metadata_from_nlattrs(&match, &attrs, a, false);
 
1690
        if (err)
 
1691
                return err;
1334
1692
 
1335
1693
        return 0;
1336
1694
}
1337
1695
 
1338
 
int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
 
1696
int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
 
1697
                const struct sw_flow_key *output, struct sk_buff *skb)
1339
1698
{
1340
1699
        struct ovs_key_ethernet *eth_key;
1341
1700
        struct nlattr *nla, *encap;
1342
 
 
1343
 
        if (swkey->phy.priority &&
1344
 
            nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1345
 
                goto nla_put_failure;
1346
 
 
1347
 
        if (swkey->tun_key.ipv4_dst &&
1348
 
            ipv4_tun_to_nlattr(skb, &swkey->tun_key))
1349
 
                goto nla_put_failure;
1350
 
 
1351
 
        if (swkey->phy.in_port != DP_MAX_PORTS &&
1352
 
            nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1353
 
                goto nla_put_failure;
1354
 
 
1355
 
        if (swkey->phy.skb_mark &&
1356
 
            nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
 
1701
        bool is_mask = (swkey != output);
 
1702
 
 
1703
        if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
 
1704
                goto nla_put_failure;
 
1705
 
 
1706
        if ((swkey->tun_key.ipv4_dst || is_mask) &&
 
1707
            ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
 
1708
                goto nla_put_failure;
 
1709
 
 
1710
        if (swkey->phy.in_port == DP_MAX_PORTS) {
 
1711
                if (is_mask && (output->phy.in_port == 0xffff))
 
1712
                        if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
 
1713
                                goto nla_put_failure;
 
1714
        } else {
 
1715
                u16 upper_u16;
 
1716
                upper_u16 = !is_mask ? 0 : 0xffff;
 
1717
 
 
1718
                if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
 
1719
                                (upper_u16 << 16) | output->phy.in_port))
 
1720
                        goto nla_put_failure;
 
1721
        }
 
1722
 
 
1723
        if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1357
1724
                goto nla_put_failure;
1358
1725
 
1359
1726
        nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1360
1727
        if (!nla)
1361
1728
                goto nla_put_failure;
 
1729
 
1362
1730
        eth_key = nla_data(nla);
1363
 
        memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1364
 
        memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
 
1731
        memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
 
1732
        memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1365
1733
 
1366
1734
        if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1367
 
                if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1368
 
                    nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
 
1735
                __be16 eth_type;
 
1736
                eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
 
1737
                if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
 
1738
                    nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1369
1739
                        goto nla_put_failure;
1370
1740
                encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1371
1741
                if (!swkey->eth.tci)
1372
1742
                        goto unencap;
1373
 
        } else {
 
1743
        } else
1374
1744
                encap = NULL;
1375
 
        }
1376
1745
 
1377
 
        if (swkey->eth.type == htons(ETH_P_802_2))
 
1746
        if (swkey->eth.type == htons(ETH_P_802_2)) {
 
1747
                /*
 
1748
                 * Ethertype 802.2 is represented in the netlink with omitted
 
1749
                 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
 
1750
                 * 0xffff in the mask attribute.  Ethertype can also
 
1751
                 * be wildcarded.
 
1752
                 */
 
1753
                if (is_mask && output->eth.type)
 
1754
                        if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
 
1755
                                                output->eth.type))
 
1756
                                goto nla_put_failure;
1378
1757
                goto unencap;
 
1758
        }
1379
1759
 
1380
 
        if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
 
1760
        if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1381
1761
                goto nla_put_failure;
1382
1762
 
1383
1763
        if (swkey->eth.type == htons(ETH_P_IP)) {
1387
1767
                if (!nla)
1388
1768
                        goto nla_put_failure;
1389
1769
                ipv4_key = nla_data(nla);
1390
 
                ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1391
 
                ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1392
 
                ipv4_key->ipv4_proto = swkey->ip.proto;
1393
 
                ipv4_key->ipv4_tos = swkey->ip.tos;
1394
 
                ipv4_key->ipv4_ttl = swkey->ip.ttl;
1395
 
                ipv4_key->ipv4_frag = swkey->ip.frag;
 
1770
                ipv4_key->ipv4_src = output->ipv4.addr.src;
 
1771
                ipv4_key->ipv4_dst = output->ipv4.addr.dst;
 
1772
                ipv4_key->ipv4_proto = output->ip.proto;
 
1773
                ipv4_key->ipv4_tos = output->ip.tos;
 
1774
                ipv4_key->ipv4_ttl = output->ip.ttl;
 
1775
                ipv4_key->ipv4_frag = output->ip.frag;
1396
1776
        } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1397
1777
                struct ovs_key_ipv6 *ipv6_key;
1398
1778
 
1400
1780
                if (!nla)
1401
1781
                        goto nla_put_failure;
1402
1782
                ipv6_key = nla_data(nla);
1403
 
                memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
 
1783
                memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1404
1784
                                sizeof(ipv6_key->ipv6_src));
1405
 
                memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
 
1785
                memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1406
1786
                                sizeof(ipv6_key->ipv6_dst));
1407
 
                ipv6_key->ipv6_label = swkey->ipv6.label;
1408
 
                ipv6_key->ipv6_proto = swkey->ip.proto;
1409
 
                ipv6_key->ipv6_tclass = swkey->ip.tos;
1410
 
                ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1411
 
                ipv6_key->ipv6_frag = swkey->ip.frag;
 
1787
                ipv6_key->ipv6_label = output->ipv6.label;
 
1788
                ipv6_key->ipv6_proto = output->ip.proto;
 
1789
                ipv6_key->ipv6_tclass = output->ip.tos;
 
1790
                ipv6_key->ipv6_hlimit = output->ip.ttl;
 
1791
                ipv6_key->ipv6_frag = output->ip.frag;
1412
1792
        } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1413
1793
                   swkey->eth.type == htons(ETH_P_RARP)) {
1414
1794
                struct ovs_key_arp *arp_key;
1418
1798
                        goto nla_put_failure;
1419
1799
                arp_key = nla_data(nla);
1420
1800
                memset(arp_key, 0, sizeof(struct ovs_key_arp));
1421
 
                arp_key->arp_sip = swkey->ipv4.addr.src;
1422
 
                arp_key->arp_tip = swkey->ipv4.addr.dst;
1423
 
                arp_key->arp_op = htons(swkey->ip.proto);
1424
 
                memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1425
 
                memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
 
1801
                arp_key->arp_sip = output->ipv4.addr.src;
 
1802
                arp_key->arp_tip = output->ipv4.addr.dst;
 
1803
                arp_key->arp_op = htons(output->ip.proto);
 
1804
                memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
 
1805
                memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1426
1806
        }
1427
1807
 
1428
1808
        if ((swkey->eth.type == htons(ETH_P_IP) ||
1437
1817
                                goto nla_put_failure;
1438
1818
                        tcp_key = nla_data(nla);
1439
1819
                        if (swkey->eth.type == htons(ETH_P_IP)) {
1440
 
                                tcp_key->tcp_src = swkey->ipv4.tp.src;
1441
 
                                tcp_key->tcp_dst = swkey->ipv4.tp.dst;
 
1820
                                tcp_key->tcp_src = output->ipv4.tp.src;
 
1821
                                tcp_key->tcp_dst = output->ipv4.tp.dst;
1442
1822
                        } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1443
 
                                tcp_key->tcp_src = swkey->ipv6.tp.src;
1444
 
                                tcp_key->tcp_dst = swkey->ipv6.tp.dst;
 
1823
                                tcp_key->tcp_src = output->ipv6.tp.src;
 
1824
                                tcp_key->tcp_dst = output->ipv6.tp.dst;
1445
1825
                        }
1446
1826
                } else if (swkey->ip.proto == IPPROTO_UDP) {
1447
1827
                        struct ovs_key_udp *udp_key;
1451
1831
                                goto nla_put_failure;
1452
1832
                        udp_key = nla_data(nla);
1453
1833
                        if (swkey->eth.type == htons(ETH_P_IP)) {
1454
 
                                udp_key->udp_src = swkey->ipv4.tp.src;
1455
 
                                udp_key->udp_dst = swkey->ipv4.tp.dst;
 
1834
                                udp_key->udp_src = output->ipv4.tp.src;
 
1835
                                udp_key->udp_dst = output->ipv4.tp.dst;
1456
1836
                        } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1457
 
                                udp_key->udp_src = swkey->ipv6.tp.src;
1458
 
                                udp_key->udp_dst = swkey->ipv6.tp.dst;
 
1837
                                udp_key->udp_src = output->ipv6.tp.src;
 
1838
                                udp_key->udp_dst = output->ipv6.tp.dst;
1459
1839
                        }
1460
1840
                } else if (swkey->eth.type == htons(ETH_P_IP) &&
1461
1841
                           swkey->ip.proto == IPPROTO_ICMP) {
1465
1845
                        if (!nla)
1466
1846
                                goto nla_put_failure;
1467
1847
                        icmp_key = nla_data(nla);
1468
 
                        icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1469
 
                        icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
 
1848
                        icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
 
1849
                        icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1470
1850
                } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1471
1851
                           swkey->ip.proto == IPPROTO_ICMPV6) {
1472
1852
                        struct ovs_key_icmpv6 *icmpv6_key;
1476
1856
                        if (!nla)
1477
1857
                                goto nla_put_failure;
1478
1858
                        icmpv6_key = nla_data(nla);
1479
 
                        icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1480
 
                        icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
 
1859
                        icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
 
1860
                        icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1481
1861
 
1482
1862
                        if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1483
1863
                            icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1487
1867
                                if (!nla)
1488
1868
                                        goto nla_put_failure;
1489
1869
                                nd_key = nla_data(nla);
1490
 
                                memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
 
1870
                                memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1491
1871
                                                        sizeof(nd_key->nd_target));
1492
 
                                memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1493
 
                                memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
 
1872
                                memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
 
1873
                                memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1494
1874
                        }
1495
1875
                }
1496
1876
        }
1522
1902
{
1523
1903
        kmem_cache_destroy(flow_cache);
1524
1904
}
 
1905
 
 
1906
struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
 
1907
{
 
1908
        struct sw_flow_mask *mask;
 
1909
 
 
1910
        mask = kmalloc(sizeof(*mask), GFP_KERNEL);
 
1911
        if (mask)
 
1912
                mask->ref_count = 0;
 
1913
 
 
1914
        return mask;
 
1915
}
 
1916
 
 
1917
void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
 
1918
{
 
1919
        mask->ref_count++;
 
1920
}
 
1921
 
 
1922
static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
 
1923
{
 
1924
        struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
 
1925
 
 
1926
        kfree(mask);
 
1927
}
 
1928
 
 
1929
void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
 
1930
{
 
1931
        if (!mask)
 
1932
                return;
 
1933
 
 
1934
        BUG_ON(!mask->ref_count);
 
1935
        mask->ref_count--;
 
1936
 
 
1937
        if (!mask->ref_count) {
 
1938
                list_del_rcu(&mask->list);
 
1939
                if (deferred)
 
1940
                        call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
 
1941
                else
 
1942
                        kfree(mask);
 
1943
        }
 
1944
}
 
1945
 
 
1946
static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
 
1947
                const struct sw_flow_mask *b)
 
1948
{
 
1949
        u8 *a_ = (u8 *)&a->key + a->range.start;
 
1950
        u8 *b_ = (u8 *)&b->key + b->range.start;
 
1951
 
 
1952
        return  (a->range.end == b->range.end)
 
1953
                && (a->range.start == b->range.start)
 
1954
                && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
 
1955
}
 
1956
 
 
1957
struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
 
1958
                                           const struct sw_flow_mask *mask)
 
1959
{
 
1960
        struct list_head *ml;
 
1961
 
 
1962
        list_for_each(ml, tbl->mask_list) {
 
1963
                struct sw_flow_mask *m;
 
1964
                m = container_of(ml, struct sw_flow_mask, list);
 
1965
                if (ovs_sw_flow_mask_equal(mask, m))
 
1966
                        return m;
 
1967
        }
 
1968
 
 
1969
        return NULL;
 
1970
}
 
1971
 
 
1972
/**
 
1973
 * add a new mask into the mask list.
 
1974
 * The caller needs to make sure that 'mask' is not the same
 
1975
 * as any masks that are already on the list.
 
1976
 */
 
1977
void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
 
1978
{
 
1979
        list_add_rcu(&mask->list, tbl->mask_list);
 
1980
}
 
1981
 
 
1982
/**
 
1983
 * Set 'range' fields in the mask to the value of 'val'.
 
1984
 */
 
1985
static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
 
1986
                struct sw_flow_key_range *range, u8 val)
 
1987
{
 
1988
        u8 *m = (u8 *)&mask->key + range->start;
 
1989
 
 
1990
        mask->range = *range;
 
1991
        memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
 
1992
}