]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/conntrack.c
compat: Clean up gre_calc_hlen
[mirror_ovs.git] / datapath / conntrack.c
1 /*
2 * Copyright (c) 2015 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 #include <linux/kconfig.h>
15 #include <linux/version.h>
16
17 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
18
19 #include <linux/module.h>
20 #include <linux/openvswitch.h>
21 #include <linux/tcp.h>
22 #include <linux/udp.h>
23 #include <linux/sctp.h>
24 #include <linux/static_key.h>
25 #include <net/ip.h>
26 #include <net/genetlink.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_count.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_labels.h>
31 #include <net/netfilter/nf_conntrack_seqadj.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
34 #include <net/ipv6_frag.h>
35
36 #ifdef CONFIG_NF_NAT_NEEDED
37 #include <linux/netfilter/nf_nat.h>
38 #include <net/netfilter/nf_nat_core.h>
39 #include <net/netfilter/nf_nat_l3proto.h>
40 #endif
41
42 #include "datapath.h"
43 #include "conntrack.h"
44 #include "flow.h"
45 #include "flow_netlink.h"
46 #include "gso.h"
47
48 #ifndef HAVE_NF_NAT_RANGE2
49 #define nf_nat_range2 nf_nat_range
50 #endif
51
52 struct ovs_ct_len_tbl {
53 int maxlen;
54 int minlen;
55 };
56
57 /* Metadata mark for masked write to conntrack mark */
58 struct md_mark {
59 u32 value;
60 u32 mask;
61 };
62
63 /* Metadata label for masked write to conntrack label. */
64 struct md_labels {
65 struct ovs_key_ct_labels value;
66 struct ovs_key_ct_labels mask;
67 };
68
69 enum ovs_ct_nat {
70 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
71 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
72 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
73 };
74
75 /* Conntrack action context for execution. */
76 struct ovs_conntrack_info {
77 struct nf_conntrack_helper *helper;
78 struct nf_conntrack_zone zone;
79 struct nf_conn *ct;
80 u8 commit : 1;
81 u8 nat : 3; /* enum ovs_ct_nat */
82 u8 random_fully_compat : 1; /* bool */
83 u8 force : 1;
84 u8 have_eventmask : 1;
85 u16 family;
86 u32 eventmask; /* Mask of 1 << IPCT_*. */
87 struct md_mark mark;
88 struct md_labels labels;
89 #ifdef CONFIG_NF_NAT_NEEDED
90 struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
91 #endif
92 };
93
94 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
95 #define OVS_CT_LIMIT_UNLIMITED 0
96 #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
97 #define CT_LIMIT_HASH_BUCKETS 512
98 static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
99
100 struct ovs_ct_limit {
101 /* Elements in ovs_ct_limit_info->limits hash table */
102 struct hlist_node hlist_node;
103 struct rcu_head rcu;
104 u16 zone;
105 u32 limit;
106 };
107
108 struct ovs_ct_limit_info {
109 u32 default_limit;
110 struct hlist_head *limits;
111 struct nf_conncount_data *data;
112 };
113
114 static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
115 [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
116 };
117 #endif
118
119 static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
120
121 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
122
123 static u16 key_to_nfproto(const struct sw_flow_key *key)
124 {
125 switch (ntohs(key->eth.type)) {
126 case ETH_P_IP:
127 return NFPROTO_IPV4;
128 case ETH_P_IPV6:
129 return NFPROTO_IPV6;
130 default:
131 return NFPROTO_UNSPEC;
132 }
133 }
134
135 /* Map SKB connection state into the values used by flow definition. */
136 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
137 {
138 u8 ct_state = OVS_CS_F_TRACKED;
139
140 switch (ctinfo) {
141 case IP_CT_ESTABLISHED_REPLY:
142 case IP_CT_RELATED_REPLY:
143 ct_state |= OVS_CS_F_REPLY_DIR;
144 break;
145 default:
146 break;
147 }
148
149 switch (ctinfo) {
150 case IP_CT_ESTABLISHED:
151 case IP_CT_ESTABLISHED_REPLY:
152 ct_state |= OVS_CS_F_ESTABLISHED;
153 break;
154 case IP_CT_RELATED:
155 case IP_CT_RELATED_REPLY:
156 ct_state |= OVS_CS_F_RELATED;
157 break;
158 case IP_CT_NEW:
159 ct_state |= OVS_CS_F_NEW;
160 break;
161 default:
162 break;
163 }
164
165 return ct_state;
166 }
167
168 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
169 {
170 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
171 return ct ? ct->mark : 0;
172 #else
173 return 0;
174 #endif
175 }
176
177 /* Guard against conntrack labels max size shrinking below 128 bits. */
178 #if NF_CT_LABELS_MAX_SIZE < 16
179 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
180 #endif
181
182 static void ovs_ct_get_labels(const struct nf_conn *ct,
183 struct ovs_key_ct_labels *labels)
184 {
185 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
186
187 if (cl)
188 memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
189 else
190 memset(labels, 0, OVS_CT_LABELS_LEN);
191 }
192
193 static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
194 const struct nf_conntrack_tuple *orig,
195 u8 icmp_proto)
196 {
197 key->ct_orig_proto = orig->dst.protonum;
198 if (orig->dst.protonum == icmp_proto) {
199 key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
200 key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
201 } else {
202 key->ct.orig_tp.src = orig->src.u.all;
203 key->ct.orig_tp.dst = orig->dst.u.all;
204 }
205 }
206
207 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
208 const struct nf_conntrack_zone *zone,
209 const struct nf_conn *ct)
210 {
211 key->ct_state = state;
212 key->ct_zone = zone->id;
213 key->ct.mark = ovs_ct_get_mark(ct);
214 ovs_ct_get_labels(ct, &key->ct.labels);
215
216 if (ct) {
217 const struct nf_conntrack_tuple *orig;
218
219 /* Use the master if we have one. */
220 if (ct->master)
221 ct = ct->master;
222 orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
223
224 /* IP version must match with the master connection. */
225 if (key->eth.type == htons(ETH_P_IP) &&
226 nf_ct_l3num(ct) == NFPROTO_IPV4) {
227 key->ipv4.ct_orig.src = orig->src.u3.ip;
228 key->ipv4.ct_orig.dst = orig->dst.u3.ip;
229 __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
230 return;
231 } else if (key->eth.type == htons(ETH_P_IPV6) &&
232 !sw_flow_key_is_nd(key) &&
233 nf_ct_l3num(ct) == NFPROTO_IPV6) {
234 key->ipv6.ct_orig.src = orig->src.u3.in6;
235 key->ipv6.ct_orig.dst = orig->dst.u3.in6;
236 __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
237 return;
238 }
239 }
240 /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
241 * original direction key fields.
242 */
243 key->ct_orig_proto = 0;
244 }
245
246 /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
247 * previously sent the packet to conntrack via the ct action. If
248 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
249 * initialized from the connection status.
250 */
251 static void ovs_ct_update_key(const struct sk_buff *skb,
252 const struct ovs_conntrack_info *info,
253 struct sw_flow_key *key, bool post_ct,
254 bool keep_nat_flags)
255 {
256 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
257 enum ip_conntrack_info ctinfo;
258 struct nf_conn *ct;
259 u8 state = 0;
260
261 ct = nf_ct_get(skb, &ctinfo);
262 if (ct) {
263 state = ovs_ct_get_state(ctinfo);
264 /* All unconfirmed entries are NEW connections. */
265 if (!nf_ct_is_confirmed(ct))
266 state |= OVS_CS_F_NEW;
267 /* OVS persists the related flag for the duration of the
268 * connection.
269 */
270 if (ct->master)
271 state |= OVS_CS_F_RELATED;
272 if (keep_nat_flags) {
273 state |= key->ct_state & OVS_CS_F_NAT_MASK;
274 } else {
275 if (ct->status & IPS_SRC_NAT)
276 state |= OVS_CS_F_SRC_NAT;
277 if (ct->status & IPS_DST_NAT)
278 state |= OVS_CS_F_DST_NAT;
279 }
280 zone = nf_ct_zone(ct);
281 } else if (post_ct) {
282 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
283 if (info)
284 zone = &info->zone;
285 }
286 __ovs_ct_update_key(key, state, zone, ct);
287 }
288
289 /* This is called to initialize CT key fields possibly coming in from the local
290 * stack.
291 */
292 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
293 {
294 ovs_ct_update_key(skb, NULL, key, false, false);
295 }
296
297 #define IN6_ADDR_INITIALIZER(ADDR) \
298 { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
299 (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
300
301 int ovs_ct_put_key(const struct sw_flow_key *swkey,
302 const struct sw_flow_key *output, struct sk_buff *skb)
303 {
304 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
305 return -EMSGSIZE;
306
307 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
308 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
309 return -EMSGSIZE;
310
311 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
312 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
313 return -EMSGSIZE;
314
315 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
316 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
317 &output->ct.labels))
318 return -EMSGSIZE;
319
320 if (swkey->ct_orig_proto) {
321 if (swkey->eth.type == htons(ETH_P_IP)) {
322 struct ovs_key_ct_tuple_ipv4 orig = {
323 output->ipv4.ct_orig.src,
324 output->ipv4.ct_orig.dst,
325 output->ct.orig_tp.src,
326 output->ct.orig_tp.dst,
327 output->ct_orig_proto,
328 };
329 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
330 sizeof(orig), &orig))
331 return -EMSGSIZE;
332 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
333 struct ovs_key_ct_tuple_ipv6 orig = {
334 IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.src),
335 IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.dst),
336 output->ct.orig_tp.src,
337 output->ct.orig_tp.dst,
338 output->ct_orig_proto,
339 };
340 if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
341 sizeof(orig), &orig))
342 return -EMSGSIZE;
343 }
344 }
345
346 return 0;
347 }
348
349 static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
350 u32 ct_mark, u32 mask)
351 {
352 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
353 u32 new_mark;
354
355 new_mark = ct_mark | (ct->mark & ~(mask));
356 if (ct->mark != new_mark) {
357 ct->mark = new_mark;
358 if (nf_ct_is_confirmed(ct))
359 nf_conntrack_event_cache(IPCT_MARK, ct);
360 key->ct.mark = new_mark;
361 }
362
363 return 0;
364 #else
365 return -ENOTSUPP;
366 #endif
367 }
368
369 static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
370 {
371 struct nf_conn_labels *cl;
372
373 cl = nf_ct_labels_find(ct);
374 if (!cl) {
375 nf_ct_labels_ext_add(ct);
376 cl = nf_ct_labels_find(ct);
377 }
378
379 return cl;
380 }
381
382 /* Initialize labels for a new, yet to be committed conntrack entry. Note that
383 * since the new connection is not yet confirmed, and thus no-one else has
384 * access to it's labels, we simply write them over.
385 */
386 static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
387 const struct ovs_key_ct_labels *labels,
388 const struct ovs_key_ct_labels *mask)
389 {
390 struct nf_conn_labels *cl, *master_cl;
391 bool have_mask = labels_nonzero(mask);
392
393 /* Inherit master's labels to the related connection? */
394 master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
395
396 if (!master_cl && !have_mask)
397 return 0; /* Nothing to do. */
398
399 cl = ovs_ct_get_conn_labels(ct);
400 if (!cl)
401 return -ENOSPC;
402
403 /* Inherit the master's labels, if any. Must use memcpy for backport
404 * as struct assignment only copies the length field in older
405 * kernels.
406 */
407 if (master_cl)
408 memcpy(cl->bits, master_cl->bits, OVS_CT_LABELS_LEN);
409
410 if (have_mask) {
411 u32 *dst = (u32 *)cl->bits;
412 int i;
413
414 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
415 dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
416 (labels->ct_labels_32[i]
417 & mask->ct_labels_32[i]);
418 }
419
420 /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
421 * IPCT_LABEL bit is set in the event cache.
422 */
423 nf_conntrack_event_cache(IPCT_LABEL, ct);
424
425 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
426
427 return 0;
428 }
429
430 static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
431 const struct ovs_key_ct_labels *labels,
432 const struct ovs_key_ct_labels *mask)
433 {
434 struct nf_conn_labels *cl;
435 int err;
436
437 cl = ovs_ct_get_conn_labels(ct);
438 if (!cl)
439 return -ENOSPC;
440
441 err = nf_connlabels_replace(ct, labels->ct_labels_32,
442 mask->ct_labels_32,
443 OVS_CT_LABELS_LEN_32);
444 if (err)
445 return err;
446
447 memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
448
449 return 0;
450 }
451
452 /* 'skb' should already be pulled to nh_ofs. */
453 static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
454 {
455 const struct nf_conntrack_helper *helper;
456 const struct nf_conn_help *help;
457 enum ip_conntrack_info ctinfo;
458 unsigned int protoff;
459 struct nf_conn *ct;
460 u8 nexthdr;
461 int err;
462
463 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
464 bool dst_set = false;
465 struct rtable rt = { .rt_flags = 0 };
466 #endif
467
468 ct = nf_ct_get(skb, &ctinfo);
469 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
470 return NF_ACCEPT;
471
472 help = nfct_help(ct);
473 if (!help)
474 return NF_ACCEPT;
475
476 helper = rcu_dereference(help->helper);
477 if (!helper)
478 return NF_ACCEPT;
479
480 switch (proto) {
481 case NFPROTO_IPV4:
482 protoff = ip_hdrlen(skb);
483 break;
484 case NFPROTO_IPV6: {
485 __be16 frag_off;
486 int ofs;
487
488 nexthdr = ipv6_hdr(skb)->nexthdr;
489 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
490 &frag_off);
491 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
492 pr_debug("proto header not found\n");
493 return NF_ACCEPT;
494 }
495 protoff = ofs;
496 break;
497 }
498 default:
499 WARN_ONCE(1, "helper invoked on non-IP family!");
500 return NF_DROP;
501 }
502
503 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
504 /* Linux 4.5 and older depend on skb_dst being set when recalculating
505 * checksums after NAT helper has mangled TCP or UDP packet payload.
506 * skb_dst is cast to a rtable struct and the flags examined.
507 * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod
508 * is carried out in the same way as kernel versions > 4.5
509 */
510 if (ct->status & IPS_NAT_MASK && skb->ip_summed != CHECKSUM_PARTIAL
511 && !skb_dst(skb)) {
512 dst_set = true;
513 skb_dst_set(skb, &rt.dst);
514 }
515 #endif
516 err = helper->help(skb, protoff, ct, ctinfo);
517 if (err != NF_ACCEPT)
518 return err;
519
520 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
521 if (dst_set)
522 skb_dst_set(skb, NULL);
523 #endif
524
525 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
526 * FTP with NAT) adusting the TCP payload size when mangling IP
527 * addresses and/or port numbers in the text-based control connection.
528 */
529 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
530 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
531 return NF_DROP;
532 return NF_ACCEPT;
533 }
534
535 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
536 * value if 'skb' is freed.
537 */
538 static int handle_fragments(struct net *net, struct sw_flow_key *key,
539 u16 zone, struct sk_buff *skb)
540 {
541 struct ovs_gso_cb ovs_cb = *OVS_GSO_CB(skb);
542 int err;
543
544 if (key->eth.type == htons(ETH_P_IP)) {
545 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
546
547 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
548 err = ip_defrag(net, skb, user);
549 if (err)
550 return err;
551
552 ovs_cb.dp_cb.mru = IPCB(skb)->frag_max_size;
553 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
554 } else if (key->eth.type == htons(ETH_P_IPV6)) {
555 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
556
557 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
558 err = nf_ct_frag6_gather(net, skb, user);
559 if (err) {
560 if (err != -EINPROGRESS)
561 kfree_skb(skb);
562 return err;
563 }
564
565 key->ip.proto = ipv6_hdr(skb)->nexthdr;
566 ovs_cb.dp_cb.mru = IP6CB(skb)->frag_max_size;
567 #endif /* IP frag support */
568 } else {
569 kfree_skb(skb);
570 return -EPFNOSUPPORT;
571 }
572
573 key->ip.frag = OVS_FRAG_TYPE_NONE;
574 skb_clear_hash(skb);
575 skb->ignore_df = 1;
576 *OVS_GSO_CB(skb) = ovs_cb;
577
578 return 0;
579 }
580
581 static struct nf_conntrack_expect *
582 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
583 u16 proto, const struct sk_buff *skb)
584 {
585 struct nf_conntrack_tuple tuple;
586 struct nf_conntrack_expect *exp;
587
588 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
589 return NULL;
590
591 exp = __nf_ct_expect_find(net, zone, &tuple);
592 if (exp) {
593 struct nf_conntrack_tuple_hash *h;
594
595 /* Delete existing conntrack entry, if it clashes with the
596 * expectation. This can happen since conntrack ALGs do not
597 * check for clashes between (new) expectations and existing
598 * conntrack entries. nf_conntrack_in() will check the
599 * expectations only if a conntrack entry can not be found,
600 * which can lead to OVS finding the expectation (here) in the
601 * init direction, but which will not be removed by the
602 * nf_conntrack_in() call, if a matching conntrack entry is
603 * found instead. In this case all init direction packets
604 * would be reported as new related packets, while reply
605 * direction packets would be reported as un-related
606 * established packets.
607 */
608 h = nf_conntrack_find_get(net, zone, &tuple);
609 if (h) {
610 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
611
612 nf_ct_delete(ct, 0, 0);
613 nf_conntrack_put(&ct->ct_general);
614 }
615 }
616
617 return exp;
618 }
619
620 /* This replicates logic from nf_conntrack_core.c that is not exported. */
621 static enum ip_conntrack_info
622 ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
623 {
624 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
625
626 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
627 return IP_CT_ESTABLISHED_REPLY;
628 /* Once we've had two way comms, always ESTABLISHED. */
629 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
630 return IP_CT_ESTABLISHED;
631 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
632 return IP_CT_RELATED;
633 return IP_CT_NEW;
634 }
635
636 /* Find an existing connection which this packet belongs to without
637 * re-attributing statistics or modifying the connection state. This allows an
638 * skb->_nfct lost due to an upcall to be recovered during actions execution.
639 *
640 * Must be called with rcu_read_lock.
641 *
642 * On success, populates skb->_nfct and returns the connection. Returns NULL
643 * if there is no existing entry.
644 */
645 static struct nf_conn *
646 ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
647 u8 l3num, struct sk_buff *skb, bool natted)
648 {
649 struct nf_conntrack_tuple tuple;
650 struct nf_conntrack_tuple_hash *h;
651 struct nf_conn *ct;
652
653 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
654 net, &tuple)) {
655 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
656 return NULL;
657 }
658
659 /* Must invert the tuple if skb has been transformed by NAT. */
660 if (natted) {
661 struct nf_conntrack_tuple inverse;
662
663 if (!nf_ct_invert_tuplepr(&inverse, &tuple)) {
664 pr_debug("ovs_ct_find_existing: Inversion failed!\n");
665 return NULL;
666 }
667 tuple = inverse;
668 }
669
670 /* look for tuple match */
671 h = nf_conntrack_find_get(net, zone, &tuple);
672 if (!h)
673 return NULL; /* Not found. */
674
675 ct = nf_ct_tuplehash_to_ctrack(h);
676
677 /* Inverted packet tuple matches the reverse direction conntrack tuple,
678 * select the other tuplehash to get the right 'ctinfo' bits for this
679 * packet.
680 */
681 if (natted)
682 h = &ct->tuplehash[!h->tuple.dst.dir];
683
684 nf_ct_set(skb, ct, ovs_ct_get_info(h));
685 return ct;
686 }
687
688 static
689 struct nf_conn *ovs_ct_executed(struct net *net,
690 const struct sw_flow_key *key,
691 const struct ovs_conntrack_info *info,
692 struct sk_buff *skb,
693 bool *ct_executed)
694 {
695 struct nf_conn *ct = NULL;
696
697 /* If no ct, check if we have evidence that an existing conntrack entry
698 * might be found for this skb. This happens when we lose a skb->_nfct
699 * due to an upcall, or if the direction is being forced. If the
700 * connection was not confirmed, it is not cached and needs to be run
701 * through conntrack again.
702 */
703 *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
704 !(key->ct_state & OVS_CS_F_INVALID) &&
705 (key->ct_zone == info->zone.id);
706
707 if (*ct_executed || (!key->ct_state && info->force)) {
708 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
709 !!(key->ct_state &
710 OVS_CS_F_NAT_MASK));
711 }
712
713 return ct;
714 }
715
716 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
717 static bool skb_nfct_cached(struct net *net,
718 const struct sw_flow_key *key,
719 const struct ovs_conntrack_info *info,
720 struct sk_buff *skb)
721 {
722 enum ip_conntrack_info ctinfo;
723 struct nf_conn *ct;
724 bool ct_executed = true;
725
726 ct = nf_ct_get(skb, &ctinfo);
727 if (!ct)
728 ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
729
730 if (ct)
731 nf_ct_get(skb, &ctinfo);
732 else
733 return false;
734
735 if (!net_eq(net, read_pnet(&ct->ct_net)))
736 return false;
737 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
738 return false;
739 if (info->helper) {
740 struct nf_conn_help *help;
741
742 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
743 if (help && rcu_access_pointer(help->helper) != info->helper)
744 return false;
745 }
746 /* Force conntrack entry direction to the current packet? */
747 if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
748 /* Delete the conntrack entry if confirmed, else just release
749 * the reference.
750 */
751 if (nf_ct_is_confirmed(ct))
752 nf_ct_delete(ct, 0, 0);
753
754 nf_conntrack_put(&ct->ct_general);
755 nf_ct_set(skb, NULL, 0);
756 return false;
757 }
758
759 return ct_executed;
760 }
761
762 #ifdef CONFIG_NF_NAT_NEEDED
763 /* Modelled after nf_nat_ipv[46]_fn().
764 * range is only used for new, uninitialized NAT state.
765 * Returns either NF_ACCEPT or NF_DROP.
766 */
767 static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
768 enum ip_conntrack_info ctinfo,
769 const struct nf_nat_range2 *range,
770 enum nf_nat_manip_type maniptype)
771 {
772 int hooknum, nh_off, err = NF_ACCEPT;
773
774 nh_off = skb_network_offset(skb);
775 skb_pull_rcsum(skb, nh_off);
776
777 /* See HOOK2MANIP(). */
778 if (maniptype == NF_NAT_MANIP_SRC)
779 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
780 else
781 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
782
783 switch (ctinfo) {
784 case IP_CT_RELATED:
785 case IP_CT_RELATED_REPLY:
786 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
787 skb->protocol == htons(ETH_P_IP) &&
788 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
789 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
790 hooknum))
791 err = NF_DROP;
792 goto push;
793 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
794 skb->protocol == htons(ETH_P_IPV6)) {
795 __be16 frag_off;
796 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
797 int hdrlen = ipv6_skip_exthdr(skb,
798 sizeof(struct ipv6hdr),
799 &nexthdr, &frag_off);
800
801 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
802 if (!nf_nat_icmpv6_reply_translation(skb, ct,
803 ctinfo,
804 hooknum,
805 hdrlen))
806 err = NF_DROP;
807 goto push;
808 }
809 }
810 /* Non-ICMP, fall thru to initialize if needed. */
811 case IP_CT_NEW:
812 /* Seen it before? This can happen for loopback, retrans,
813 * or local packets.
814 */
815 if (!nf_nat_initialized(ct, maniptype)) {
816 /* Initialize according to the NAT action. */
817 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
818 /* Action is set up to establish a new
819 * mapping.
820 */
821 ? nf_nat_setup_info(ct, range, maniptype)
822 : nf_nat_alloc_null_binding(ct, hooknum);
823 if (err != NF_ACCEPT)
824 goto push;
825 }
826 break;
827
828 case IP_CT_ESTABLISHED:
829 case IP_CT_ESTABLISHED_REPLY:
830 break;
831
832 default:
833 err = NF_DROP;
834 goto push;
835 }
836
837 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
838 push:
839 skb_push(skb, nh_off);
840 skb_postpush_rcsum(skb, skb->data, nh_off);
841
842 return err;
843 }
844
845 static void ovs_nat_update_key(struct sw_flow_key *key,
846 const struct sk_buff *skb,
847 enum nf_nat_manip_type maniptype)
848 {
849 if (maniptype == NF_NAT_MANIP_SRC) {
850 __be16 src;
851
852 key->ct_state |= OVS_CS_F_SRC_NAT;
853 if (key->eth.type == htons(ETH_P_IP))
854 key->ipv4.addr.src = ip_hdr(skb)->saddr;
855 else if (key->eth.type == htons(ETH_P_IPV6))
856 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
857 sizeof(key->ipv6.addr.src));
858 else
859 return;
860
861 if (key->ip.proto == IPPROTO_UDP)
862 src = udp_hdr(skb)->source;
863 else if (key->ip.proto == IPPROTO_TCP)
864 src = tcp_hdr(skb)->source;
865 else if (key->ip.proto == IPPROTO_SCTP)
866 src = sctp_hdr(skb)->source;
867 else
868 return;
869
870 key->tp.src = src;
871 } else {
872 __be16 dst;
873
874 key->ct_state |= OVS_CS_F_DST_NAT;
875 if (key->eth.type == htons(ETH_P_IP))
876 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
877 else if (key->eth.type == htons(ETH_P_IPV6))
878 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
879 sizeof(key->ipv6.addr.dst));
880 else
881 return;
882
883 if (key->ip.proto == IPPROTO_UDP)
884 dst = udp_hdr(skb)->dest;
885 else if (key->ip.proto == IPPROTO_TCP)
886 dst = tcp_hdr(skb)->dest;
887 else if (key->ip.proto == IPPROTO_SCTP)
888 dst = sctp_hdr(skb)->dest;
889 else
890 return;
891
892 key->tp.dst = dst;
893 }
894 }
895
896 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
897 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
898 const struct ovs_conntrack_info *info,
899 struct sk_buff *skb, struct nf_conn *ct,
900 enum ip_conntrack_info ctinfo)
901 {
902 enum nf_nat_manip_type maniptype;
903 int err;
904
905 #ifdef HAVE_NF_CT_IS_UNTRACKED
906 if (nf_ct_is_untracked(ct)) {
907 /* A NAT action may only be performed on tracked packets. */
908 return NF_ACCEPT;
909 }
910 #endif /* HAVE_NF_CT_IS_UNTRACKED */
911
912 /* Add NAT extension if not confirmed yet. */
913 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
914 return NF_ACCEPT; /* Can't NAT. */
915
916 /* Determine NAT type.
917 * Check if the NAT type can be deduced from the tracked connection.
918 * Make sure new expected connections (IP_CT_RELATED) are NATted only
919 * when committing.
920 */
921 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
922 ct->status & IPS_NAT_MASK &&
923 (ctinfo != IP_CT_RELATED || info->commit)) {
924 /* NAT an established or related connection like before. */
925 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
926 /* This is the REPLY direction for a connection
927 * for which NAT was applied in the forward
928 * direction. Do the reverse NAT.
929 */
930 maniptype = ct->status & IPS_SRC_NAT
931 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
932 else
933 maniptype = ct->status & IPS_SRC_NAT
934 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
935 } else if (info->nat & OVS_CT_SRC_NAT) {
936 maniptype = NF_NAT_MANIP_SRC;
937 } else if (info->nat & OVS_CT_DST_NAT) {
938 maniptype = NF_NAT_MANIP_DST;
939 } else {
940 return NF_ACCEPT; /* Connection is not NATed. */
941 }
942 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
943
944 /* Mark NAT done if successful and update the flow key. */
945 if (err == NF_ACCEPT)
946 ovs_nat_update_key(key, skb, maniptype);
947
948 return err;
949 }
950 #else /* !CONFIG_NF_NAT_NEEDED */
951 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
952 const struct ovs_conntrack_info *info,
953 struct sk_buff *skb, struct nf_conn *ct,
954 enum ip_conntrack_info ctinfo)
955 {
956 return NF_ACCEPT;
957 }
958 #endif
959
960 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
961 * not done already. Update key with new CT state after passing the packet
962 * through conntrack.
963 * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
964 * set to NULL and 0 will be returned.
965 */
966 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
967 const struct ovs_conntrack_info *info,
968 struct sk_buff *skb)
969 {
970 /* If we are recirculating packets to match on conntrack fields and
971 * committing with a separate conntrack action, then we don't need to
972 * actually run the packet through conntrack twice unless it's for a
973 * different zone.
974 */
975 bool cached = skb_nfct_cached(net, key, info, skb);
976 enum ip_conntrack_info ctinfo;
977 struct nf_conn *ct;
978
979 if (!cached) {
980 struct nf_hook_state state = {
981 .hook = NF_INET_PRE_ROUTING,
982 .pf = info->family,
983 .net = net,
984 };
985 struct nf_conn *tmpl = info->ct;
986 int err;
987
988 /* Associate skb with specified zone. */
989 if (tmpl) {
990 if (skb_nfct(skb))
991 nf_conntrack_put(skb_nfct(skb));
992 nf_conntrack_get(&tmpl->ct_general);
993 nf_ct_set(skb, tmpl, IP_CT_NEW);
994 }
995
996 err = nf_conntrack_in(skb, &state);
997 if (err != NF_ACCEPT)
998 return -ENOENT;
999
1000 /* Clear CT state NAT flags to mark that we have not yet done
1001 * NAT after the nf_conntrack_in() call. We can actually clear
1002 * the whole state, as it will be re-initialized below.
1003 */
1004 key->ct_state = 0;
1005
1006 /* Update the key, but keep the NAT flags. */
1007 ovs_ct_update_key(skb, info, key, true, true);
1008 }
1009
1010 ct = nf_ct_get(skb, &ctinfo);
1011 if (ct) {
1012 /* Packets starting a new connection must be NATted before the
1013 * helper, so that the helper knows about the NAT. We enforce
1014 * this by delaying both NAT and helper calls for unconfirmed
1015 * connections until the committing CT action. For later
1016 * packets NAT and Helper may be called in either order.
1017 *
1018 * NAT will be done only if the CT action has NAT, and only
1019 * once per packet (per zone), as guarded by the NAT bits in
1020 * the key->ct_state.
1021 */
1022 if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
1023 (nf_ct_is_confirmed(ct) || info->commit) &&
1024 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
1025 return -EINVAL;
1026 }
1027
1028 /* Userspace may decide to perform a ct lookup without a helper
1029 * specified followed by a (recirculate and) commit with one.
1030 * Therefore, for unconfirmed connections which we will commit,
1031 * we need to attach the helper here.
1032 */
1033 if (!nf_ct_is_confirmed(ct) && info->commit &&
1034 info->helper && !nfct_help(ct)) {
1035 int err = __nf_ct_try_assign_helper(ct, info->ct,
1036 GFP_ATOMIC);
1037 if (err)
1038 return err;
1039 }
1040
1041 /* Call the helper only if:
1042 * - nf_conntrack_in() was executed above ("!cached") for a
1043 * confirmed connection, or
1044 * - When committing an unconfirmed connection.
1045 */
1046 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
1047 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
1048 return -EINVAL;
1049 }
1050 }
1051
1052 return 0;
1053 }
1054
1055 /* Lookup connection and read fields into key. */
1056 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
1057 const struct ovs_conntrack_info *info,
1058 struct sk_buff *skb)
1059 {
1060 struct nf_conntrack_expect *exp;
1061
1062 /* If we pass an expected packet through nf_conntrack_in() the
1063 * expectation is typically removed, but the packet could still be
1064 * lost in upcall processing. To prevent this from happening we
1065 * perform an explicit expectation lookup. Expected connections are
1066 * always new, and will be passed through conntrack only when they are
1067 * committed, as it is OK to remove the expectation at that time.
1068 */
1069 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
1070 if (exp) {
1071 u8 state;
1072
1073 /* NOTE: New connections are NATted and Helped only when
1074 * committed, so we are not calling into NAT here.
1075 */
1076 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
1077 __ovs_ct_update_key(key, state, &info->zone, exp->master);
1078 } else {
1079 struct nf_conn *ct;
1080 int err;
1081
1082 err = __ovs_ct_lookup(net, key, info, skb);
1083 if (err)
1084 return err;
1085
1086 ct = (struct nf_conn *)skb_nfct(skb);
1087 if (ct)
1088 nf_ct_deliver_cached_events(ct);
1089 }
1090
1091 return 0;
1092 }
1093
1094 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
1095 {
1096 size_t i;
1097
1098 for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
1099 if (labels->ct_labels_32[i])
1100 return true;
1101
1102 return false;
1103 }
1104
1105 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1106 static struct hlist_head *ct_limit_hash_bucket(
1107 const struct ovs_ct_limit_info *info, u16 zone)
1108 {
1109 return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
1110 }
1111
1112 /* Call with ovs_mutex */
1113 static void ct_limit_set(const struct ovs_ct_limit_info *info,
1114 struct ovs_ct_limit *new_ct_limit)
1115 {
1116 struct ovs_ct_limit *ct_limit;
1117 struct hlist_head *head;
1118
1119 head = ct_limit_hash_bucket(info, new_ct_limit->zone);
1120 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1121 if (ct_limit->zone == new_ct_limit->zone) {
1122 hlist_replace_rcu(&ct_limit->hlist_node,
1123 &new_ct_limit->hlist_node);
1124 kfree_rcu(ct_limit, rcu);
1125 return;
1126 }
1127 }
1128
1129 hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
1130 }
1131
1132 /* Call with ovs_mutex */
1133 static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
1134 {
1135 struct ovs_ct_limit *ct_limit;
1136 struct hlist_head *head;
1137 struct hlist_node *n;
1138
1139 head = ct_limit_hash_bucket(info, zone);
1140 hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
1141 if (ct_limit->zone == zone) {
1142 hlist_del_rcu(&ct_limit->hlist_node);
1143 kfree_rcu(ct_limit, rcu);
1144 return;
1145 }
1146 }
1147 }
1148
1149 /* Call with RCU read lock */
1150 static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
1151 {
1152 struct ovs_ct_limit *ct_limit;
1153 struct hlist_head *head;
1154
1155 head = ct_limit_hash_bucket(info, zone);
1156 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
1157 if (ct_limit->zone == zone)
1158 return ct_limit->limit;
1159 }
1160
1161 return info->default_limit;
1162 }
1163
1164 static int ovs_ct_check_limit(struct net *net,
1165 const struct ovs_conntrack_info *info,
1166 const struct nf_conntrack_tuple *tuple)
1167 {
1168 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1169 const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
1170 u32 per_zone_limit, connections;
1171 u32 conncount_key;
1172
1173 conncount_key = info->zone.id;
1174
1175 per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
1176 if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
1177 return 0;
1178
1179 connections = nf_conncount_count(net, ct_limit_info->data,
1180 &conncount_key, tuple, &info->zone);
1181 if (connections > per_zone_limit)
1182 return -ENOMEM;
1183
1184 return 0;
1185 }
1186 #endif
1187
1188 /* Lookup connection and confirm if unconfirmed. */
1189 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
1190 const struct ovs_conntrack_info *info,
1191 struct sk_buff *skb)
1192 {
1193 enum ip_conntrack_info ctinfo;
1194 struct nf_conn *ct;
1195 int err;
1196
1197 err = __ovs_ct_lookup(net, key, info, skb);
1198 if (err)
1199 return err;
1200
1201 /* The connection could be invalid, in which case this is a no-op.*/
1202 ct = nf_ct_get(skb, &ctinfo);
1203 if (!ct)
1204 return 0;
1205
1206 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1207 if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
1208 if (!nf_ct_is_confirmed(ct)) {
1209 err = ovs_ct_check_limit(net, info,
1210 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1211 if (err) {
1212 net_warn_ratelimited("openvswitch: zone: %u "
1213 "exceeds conntrack limit\n",
1214 info->zone.id);
1215 return err;
1216 }
1217 }
1218 }
1219 #endif
1220
1221 /* Set the conntrack event mask if given. NEW and DELETE events have
1222 * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
1223 * typically would receive many kinds of updates. Setting the event
1224 * mask allows those events to be filtered. The set event mask will
1225 * remain in effect for the lifetime of the connection unless changed
1226 * by a further CT action with both the commit flag and the eventmask
1227 * option. */
1228 if (info->have_eventmask) {
1229 struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
1230
1231 if (cache)
1232 cache->ctmask = info->eventmask;
1233 }
1234
1235 /* Apply changes before confirming the connection so that the initial
1236 * conntrack NEW netlink event carries the values given in the CT
1237 * action.
1238 */
1239 if (info->mark.mask) {
1240 err = ovs_ct_set_mark(ct, key, info->mark.value,
1241 info->mark.mask);
1242 if (err)
1243 return err;
1244 }
1245 if (!nf_ct_is_confirmed(ct)) {
1246 err = ovs_ct_init_labels(ct, key, &info->labels.value,
1247 &info->labels.mask);
1248 if (err)
1249 return err;
1250 } else if (labels_nonzero(&info->labels.mask)) {
1251 err = ovs_ct_set_labels(ct, key, &info->labels.value,
1252 &info->labels.mask);
1253 if (err)
1254 return err;
1255 }
1256 /* This will take care of sending queued events even if the connection
1257 * is already confirmed.
1258 */
1259 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
1260 return -EINVAL;
1261
1262 return 0;
1263 }
1264
1265 /* Trim the skb to the length specified by the IP/IPv6 header,
1266 * removing any trailing lower-layer padding. This prepares the skb
1267 * for higher-layer processing that assumes skb->len excludes padding
1268 * (such as nf_ip_checksum). The caller needs to pull the skb to the
1269 * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
1270 */
1271 static int ovs_skb_network_trim(struct sk_buff *skb)
1272 {
1273 unsigned int len;
1274 int err;
1275
1276 switch (skb->protocol) {
1277 case htons(ETH_P_IP):
1278 len = ntohs(ip_hdr(skb)->tot_len);
1279 break;
1280 case htons(ETH_P_IPV6):
1281 len = sizeof(struct ipv6hdr)
1282 + ntohs(ipv6_hdr(skb)->payload_len);
1283 break;
1284 default:
1285 len = skb->len;
1286 }
1287
1288 err = pskb_trim_rcsum(skb, len);
1289 if (err)
1290 kfree_skb(skb);
1291
1292 return err;
1293 }
1294
1295 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
1296 * value if 'skb' is freed.
1297 */
1298 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
1299 struct sw_flow_key *key,
1300 const struct ovs_conntrack_info *info)
1301 {
1302 int nh_ofs;
1303 int err;
1304
1305 /* The conntrack module expects to be working at L3. */
1306 nh_ofs = skb_network_offset(skb);
1307 skb_pull_rcsum(skb, nh_ofs);
1308
1309 err = ovs_skb_network_trim(skb);
1310 if (err)
1311 return err;
1312
1313 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
1314 err = handle_fragments(net, key, info->zone.id, skb);
1315 if (err)
1316 return err;
1317 }
1318
1319 if (info->commit)
1320 err = ovs_ct_commit(net, key, info, skb);
1321 else
1322 err = ovs_ct_lookup(net, key, info, skb);
1323
1324 skb_push(skb, nh_ofs);
1325 skb_postpush_rcsum(skb, skb->data, nh_ofs);
1326 if (err)
1327 kfree_skb(skb);
1328 return err;
1329 }
1330
1331 int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
1332 {
1333 if (skb_nfct(skb)) {
1334 nf_conntrack_put(skb_nfct(skb));
1335 #ifdef HAVE_IP_CT_UNTRACKED
1336 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
1337 #else
1338 nf_ct_set(skb, NULL, 0);
1339 #endif
1340 ovs_ct_fill_key(skb, key);
1341 }
1342
1343 return 0;
1344 }
1345
1346 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
1347 const struct sw_flow_key *key, bool log)
1348 {
1349 struct nf_conntrack_helper *helper;
1350 struct nf_conn_help *help;
1351
1352 helper = nf_conntrack_helper_try_module_get(name, info->family,
1353 key->ip.proto);
1354 if (!helper) {
1355 OVS_NLERR(log, "Unknown helper \"%s\"", name);
1356 return -EINVAL;
1357 }
1358
1359 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
1360 if (!help) {
1361 nf_conntrack_helper_put(helper);
1362 return -ENOMEM;
1363 }
1364
1365 rcu_assign_pointer(help->helper, helper);
1366 info->helper = helper;
1367
1368 if (info->nat)
1369 request_module("ip_nat_%s", name);
1370
1371 return 0;
1372 }
1373
1374 #ifdef CONFIG_NF_NAT_NEEDED
1375 static int parse_nat(const struct nlattr *attr,
1376 struct ovs_conntrack_info *info, bool log)
1377 {
1378 struct nlattr *a;
1379 int rem;
1380 bool have_ip_max = false;
1381 bool have_proto_max = false;
1382 bool ip_vers = (info->family == NFPROTO_IPV6);
1383
1384 nla_for_each_nested(a, attr, rem) {
1385 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
1386 [OVS_NAT_ATTR_SRC] = {0, 0},
1387 [OVS_NAT_ATTR_DST] = {0, 0},
1388 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
1389 sizeof(struct in6_addr)},
1390 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
1391 sizeof(struct in6_addr)},
1392 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
1393 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
1394 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
1395 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
1396 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
1397 };
1398 int type = nla_type(a);
1399
1400 if (type > OVS_NAT_ATTR_MAX) {
1401 OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
1402 type, OVS_NAT_ATTR_MAX);
1403 return -EINVAL;
1404 }
1405
1406 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1407 OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
1408 type, nla_len(a),
1409 ovs_nat_attr_lens[type][ip_vers]);
1410 return -EINVAL;
1411 }
1412
1413 switch (type) {
1414 case OVS_NAT_ATTR_SRC:
1415 case OVS_NAT_ATTR_DST:
1416 if (info->nat) {
1417 OVS_NLERR(log, "Only one type of NAT may be specified");
1418 return -ERANGE;
1419 }
1420 info->nat |= OVS_CT_NAT;
1421 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1422 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1423 break;
1424
1425 case OVS_NAT_ATTR_IP_MIN:
1426 nla_memcpy(&info->range.min_addr, a,
1427 sizeof(info->range.min_addr));
1428 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1429 break;
1430
1431 case OVS_NAT_ATTR_IP_MAX:
1432 have_ip_max = true;
1433 nla_memcpy(&info->range.max_addr, a,
1434 sizeof(info->range.max_addr));
1435 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1436 break;
1437
1438 case OVS_NAT_ATTR_PROTO_MIN:
1439 info->range.min_proto.all = htons(nla_get_u16(a));
1440 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1441 break;
1442
1443 case OVS_NAT_ATTR_PROTO_MAX:
1444 have_proto_max = true;
1445 info->range.max_proto.all = htons(nla_get_u16(a));
1446 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1447 break;
1448
1449 case OVS_NAT_ATTR_PERSISTENT:
1450 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1451 break;
1452
1453 case OVS_NAT_ATTR_PROTO_HASH:
1454 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1455 break;
1456
1457 case OVS_NAT_ATTR_PROTO_RANDOM:
1458 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1459 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1460 #else
1461 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1462 info->random_fully_compat = true;
1463 #endif
1464 break;
1465
1466 default:
1467 OVS_NLERR(log, "Unknown nat attribute (%d)", type);
1468 return -EINVAL;
1469 }
1470 }
1471
1472 if (rem > 0) {
1473 OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
1474 return -EINVAL;
1475 }
1476 if (!info->nat) {
1477 /* Do not allow flags if no type is given. */
1478 if (info->range.flags) {
1479 OVS_NLERR(log,
1480 "NAT flags may be given only when NAT range (SRC or DST) is also specified."
1481 );
1482 return -EINVAL;
1483 }
1484 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1485 } else if (!info->commit) {
1486 OVS_NLERR(log,
1487 "NAT attributes may be specified only when CT COMMIT flag is also specified."
1488 );
1489 return -EINVAL;
1490 }
1491 /* Allow missing IP_MAX. */
1492 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1493 memcpy(&info->range.max_addr, &info->range.min_addr,
1494 sizeof(info->range.max_addr));
1495 }
1496 /* Allow missing PROTO_MAX. */
1497 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1498 !have_proto_max) {
1499 info->range.max_proto.all = info->range.min_proto.all;
1500 }
1501 return 0;
1502 }
1503 #endif
1504
1505 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1506 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1507 [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
1508 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1509 .maxlen = sizeof(u16) },
1510 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1511 .maxlen = sizeof(struct md_mark) },
1512 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1513 .maxlen = sizeof(struct md_labels) },
1514 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1515 .maxlen = NF_CT_HELPER_NAME_LEN },
1516 #ifdef CONFIG_NF_NAT_NEEDED
1517 /* NAT length is checked when parsing the nested attributes. */
1518 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1519 #endif
1520 [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
1521 .maxlen = sizeof(u32) },
1522 };
1523
1524 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1525 const char **helper, bool log)
1526 {
1527 struct nlattr *a;
1528 int rem;
1529
1530 nla_for_each_nested(a, attr, rem) {
1531 int type = nla_type(a);
1532 int maxlen;
1533 int minlen;
1534
1535 if (type > OVS_CT_ATTR_MAX) {
1536 OVS_NLERR(log,
1537 "Unknown conntrack attr (type=%d, max=%d)",
1538 type, OVS_CT_ATTR_MAX);
1539 return -EINVAL;
1540 }
1541
1542 maxlen = ovs_ct_attr_lens[type].maxlen;
1543 minlen = ovs_ct_attr_lens[type].minlen;
1544 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1545 OVS_NLERR(log,
1546 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1547 type, nla_len(a), maxlen);
1548 return -EINVAL;
1549 }
1550
1551 switch (type) {
1552 case OVS_CT_ATTR_FORCE_COMMIT:
1553 info->force = true;
1554 /* fall through. */
1555 case OVS_CT_ATTR_COMMIT:
1556 info->commit = true;
1557 break;
1558 #ifdef CONFIG_NF_CONNTRACK_ZONES
1559 case OVS_CT_ATTR_ZONE:
1560 info->zone.id = nla_get_u16(a);
1561 break;
1562 #endif
1563 #ifdef CONFIG_NF_CONNTRACK_MARK
1564 case OVS_CT_ATTR_MARK: {
1565 struct md_mark *mark = nla_data(a);
1566
1567 if (!mark->mask) {
1568 OVS_NLERR(log, "ct_mark mask cannot be 0");
1569 return -EINVAL;
1570 }
1571 info->mark = *mark;
1572 break;
1573 }
1574 #endif
1575 #ifdef CONFIG_NF_CONNTRACK_LABELS
1576 case OVS_CT_ATTR_LABELS: {
1577 struct md_labels *labels = nla_data(a);
1578
1579 if (!labels_nonzero(&labels->mask)) {
1580 OVS_NLERR(log, "ct_labels mask cannot be 0");
1581 return -EINVAL;
1582 }
1583 info->labels = *labels;
1584 break;
1585 }
1586 #endif
1587 case OVS_CT_ATTR_HELPER:
1588 *helper = nla_data(a);
1589 if (!memchr(*helper, '\0', nla_len(a))) {
1590 OVS_NLERR(log, "Invalid conntrack helper");
1591 return -EINVAL;
1592 }
1593 break;
1594 #ifdef CONFIG_NF_NAT_NEEDED
1595 case OVS_CT_ATTR_NAT: {
1596 int err = parse_nat(a, info, log);
1597
1598 if (err)
1599 return err;
1600 break;
1601 }
1602 #endif
1603 case OVS_CT_ATTR_EVENTMASK:
1604 info->have_eventmask = true;
1605 info->eventmask = nla_get_u32(a);
1606 break;
1607
1608 default:
1609 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1610 type);
1611 return -EINVAL;
1612 }
1613 }
1614
1615 #ifdef CONFIG_NF_CONNTRACK_MARK
1616 if (!info->commit && info->mark.mask) {
1617 OVS_NLERR(log,
1618 "Setting conntrack mark requires 'commit' flag.");
1619 return -EINVAL;
1620 }
1621 #endif
1622 #ifdef CONFIG_NF_CONNTRACK_LABELS
1623 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1624 OVS_NLERR(log,
1625 "Setting conntrack labels requires 'commit' flag.");
1626 return -EINVAL;
1627 }
1628 #endif
1629 if (rem > 0) {
1630 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1631 return -EINVAL;
1632 }
1633
1634 return 0;
1635 }
1636
1637 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1638 {
1639 if (attr == OVS_KEY_ATTR_CT_STATE)
1640 return true;
1641 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1642 attr == OVS_KEY_ATTR_CT_ZONE)
1643 return true;
1644 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1645 attr == OVS_KEY_ATTR_CT_MARK)
1646 return true;
1647 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1648 attr == OVS_KEY_ATTR_CT_LABELS) {
1649 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1650
1651 return ovs_net->xt_label;
1652 }
1653
1654 return false;
1655 }
1656
1657 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1658 const struct sw_flow_key *key,
1659 struct sw_flow_actions **sfa, bool log)
1660 {
1661 struct ovs_conntrack_info ct_info;
1662 const char *helper = NULL;
1663 u16 family;
1664 int err;
1665
1666 family = key_to_nfproto(key);
1667 if (family == NFPROTO_UNSPEC) {
1668 OVS_NLERR(log, "ct family unspecified");
1669 return -EINVAL;
1670 }
1671
1672 memset(&ct_info, 0, sizeof(ct_info));
1673 ct_info.family = family;
1674
1675 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1676 NF_CT_DEFAULT_ZONE_DIR, 0);
1677
1678 err = parse_ct(attr, &ct_info, &helper, log);
1679 if (err)
1680 return err;
1681
1682 /* Set up template for tracking connections in specific zones. */
1683 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1684 if (!ct_info.ct) {
1685 OVS_NLERR(log, "Failed to allocate conntrack template");
1686 return -ENOMEM;
1687 }
1688 if (helper) {
1689 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1690 if (err)
1691 goto err_free_ct;
1692 }
1693
1694 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1695 sizeof(ct_info), log);
1696 if (err)
1697 goto err_free_ct;
1698
1699 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1700 nf_conntrack_get(&ct_info.ct->ct_general);
1701 return 0;
1702 err_free_ct:
1703 __ovs_ct_free_action(&ct_info);
1704 return err;
1705 }
1706
1707 #ifdef CONFIG_NF_NAT_NEEDED
1708 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1709 struct sk_buff *skb)
1710 {
1711 struct nlattr *start;
1712
1713 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1714 if (!start)
1715 return false;
1716
1717 if (info->nat & OVS_CT_SRC_NAT) {
1718 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1719 return false;
1720 } else if (info->nat & OVS_CT_DST_NAT) {
1721 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1722 return false;
1723 } else {
1724 goto out;
1725 }
1726
1727 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1728 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1729 info->family == NFPROTO_IPV4) {
1730 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1731 info->range.min_addr.ip) ||
1732 (info->range.max_addr.ip
1733 != info->range.min_addr.ip &&
1734 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1735 info->range.max_addr.ip))))
1736 return false;
1737 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1738 info->family == NFPROTO_IPV6) {
1739 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1740 &info->range.min_addr.in6) ||
1741 (memcmp(&info->range.max_addr.in6,
1742 &info->range.min_addr.in6,
1743 sizeof(info->range.max_addr.in6)) &&
1744 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1745 &info->range.max_addr.in6))))
1746 return false;
1747 } else {
1748 return false;
1749 }
1750 }
1751 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1752 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1753 ntohs(info->range.min_proto.all)) ||
1754 (info->range.max_proto.all != info->range.min_proto.all &&
1755 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1756 ntohs(info->range.max_proto.all)))))
1757 return false;
1758
1759 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1760 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1761 return false;
1762 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1763 nla_put_flag(skb, info->random_fully_compat
1764 ? OVS_NAT_ATTR_PROTO_RANDOM
1765 : OVS_NAT_ATTR_PROTO_HASH))
1766 return false;
1767 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1768 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1769 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1770 return false;
1771 #endif
1772 out:
1773 nla_nest_end(skb, start);
1774
1775 return true;
1776 }
1777 #endif
1778
1779 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1780 struct sk_buff *skb)
1781 {
1782 struct nlattr *start;
1783
1784 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1785 if (!start)
1786 return -EMSGSIZE;
1787
1788 if (ct_info->commit && nla_put_flag(skb, ct_info->force
1789 ? OVS_CT_ATTR_FORCE_COMMIT
1790 : OVS_CT_ATTR_COMMIT))
1791 return -EMSGSIZE;
1792 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1793 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1794 return -EMSGSIZE;
1795 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1796 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1797 &ct_info->mark))
1798 return -EMSGSIZE;
1799 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1800 labels_nonzero(&ct_info->labels.mask) &&
1801 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1802 &ct_info->labels))
1803 return -EMSGSIZE;
1804 if (ct_info->helper) {
1805 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1806 ct_info->helper->name))
1807 return -EMSGSIZE;
1808 }
1809 if (ct_info->have_eventmask &&
1810 nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
1811 return -EMSGSIZE;
1812
1813 #ifdef CONFIG_NF_NAT_NEEDED
1814 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1815 return -EMSGSIZE;
1816 #endif
1817 nla_nest_end(skb, start);
1818
1819 return 0;
1820 }
1821
1822 void ovs_ct_free_action(const struct nlattr *a)
1823 {
1824 struct ovs_conntrack_info *ct_info = nla_data(a);
1825
1826 __ovs_ct_free_action(ct_info);
1827 }
1828
1829 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1830 {
1831 if (ct_info->helper)
1832 nf_conntrack_helper_put(ct_info->helper);
1833 if (ct_info->ct)
1834 nf_ct_tmpl_free(ct_info->ct);
1835 }
1836
1837 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
1838 static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
1839 {
1840 int i, err;
1841
1842 ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
1843 GFP_KERNEL);
1844 if (!ovs_net->ct_limit_info)
1845 return -ENOMEM;
1846
1847 ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
1848 ovs_net->ct_limit_info->limits =
1849 kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
1850 GFP_KERNEL);
1851 if (!ovs_net->ct_limit_info->limits) {
1852 kfree(ovs_net->ct_limit_info);
1853 return -ENOMEM;
1854 }
1855
1856 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
1857 INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
1858
1859 ovs_net->ct_limit_info->data =
1860 nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
1861
1862 if (IS_ERR(ovs_net->ct_limit_info->data)) {
1863 err = PTR_ERR(ovs_net->ct_limit_info->data);
1864 kfree(ovs_net->ct_limit_info->limits);
1865 kfree(ovs_net->ct_limit_info);
1866 pr_err("openvswitch: failed to init nf_conncount %d\n", err);
1867 return err;
1868 }
1869 return 0;
1870 }
1871
1872 static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
1873 {
1874 const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
1875 int i;
1876
1877 nf_conncount_destroy(net, NFPROTO_INET, info->data);
1878 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
1879 struct hlist_head *head = &info->limits[i];
1880 struct ovs_ct_limit *ct_limit;
1881
1882 hlist_for_each_entry_rcu(ct_limit, head, hlist_node)
1883 kfree_rcu(ct_limit, rcu);
1884 }
1885 kfree(ovs_net->ct_limit_info->limits);
1886 kfree(ovs_net->ct_limit_info);
1887 }
1888
1889 static struct sk_buff *
1890 ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
1891 struct ovs_header **ovs_reply_header)
1892 {
1893 struct ovs_header *ovs_header = info->userhdr;
1894 struct sk_buff *skb;
1895
1896 skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1897 if (!skb)
1898 return ERR_PTR(-ENOMEM);
1899
1900 *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
1901 info->snd_seq,
1902 &dp_ct_limit_genl_family, 0, cmd);
1903
1904 if (!*ovs_reply_header) {
1905 nlmsg_free(skb);
1906 return ERR_PTR(-EMSGSIZE);
1907 }
1908 (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
1909
1910 return skb;
1911 }
1912
1913 static bool check_zone_id(int zone_id, u16 *pzone)
1914 {
1915 if (zone_id >= 0 && zone_id <= 65535) {
1916 *pzone = (u16)zone_id;
1917 return true;
1918 }
1919 return false;
1920 }
1921
1922 static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
1923 struct ovs_ct_limit_info *info)
1924 {
1925 struct ovs_zone_limit *zone_limit;
1926 int rem;
1927 u16 zone;
1928
1929 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1930 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1931
1932 while (rem >= sizeof(*zone_limit)) {
1933 if (unlikely(zone_limit->zone_id ==
1934 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1935 ovs_lock();
1936 info->default_limit = zone_limit->limit;
1937 ovs_unlock();
1938 } else if (unlikely(!check_zone_id(
1939 zone_limit->zone_id, &zone))) {
1940 OVS_NLERR(true, "zone id is out of range");
1941 } else {
1942 struct ovs_ct_limit *ct_limit;
1943
1944 ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
1945 if (!ct_limit)
1946 return -ENOMEM;
1947
1948 ct_limit->zone = zone;
1949 ct_limit->limit = zone_limit->limit;
1950
1951 ovs_lock();
1952 ct_limit_set(info, ct_limit);
1953 ovs_unlock();
1954 }
1955 rem -= NLA_ALIGN(sizeof(*zone_limit));
1956 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1957 NLA_ALIGN(sizeof(*zone_limit)));
1958 }
1959
1960 if (rem)
1961 OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
1962
1963 return 0;
1964 }
1965
1966 static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
1967 struct ovs_ct_limit_info *info)
1968 {
1969 struct ovs_zone_limit *zone_limit;
1970 int rem;
1971 u16 zone;
1972
1973 rem = NLA_ALIGN(nla_len(nla_zone_limit));
1974 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
1975
1976 while (rem >= sizeof(*zone_limit)) {
1977 if (unlikely(zone_limit->zone_id ==
1978 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
1979 ovs_lock();
1980 info->default_limit = OVS_CT_LIMIT_DEFAULT;
1981 ovs_unlock();
1982 } else if (unlikely(!check_zone_id(
1983 zone_limit->zone_id, &zone))) {
1984 OVS_NLERR(true, "zone id is out of range");
1985 } else {
1986 ovs_lock();
1987 ct_limit_del(info, zone);
1988 ovs_unlock();
1989 }
1990 rem -= NLA_ALIGN(sizeof(*zone_limit));
1991 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
1992 NLA_ALIGN(sizeof(*zone_limit)));
1993 }
1994
1995 if (rem)
1996 OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
1997
1998 return 0;
1999 }
2000
2001 static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
2002 struct sk_buff *reply)
2003 {
2004 struct ovs_zone_limit zone_limit;
2005 int err;
2006
2007 zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
2008 zone_limit.limit = info->default_limit;
2009 err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2010 if (err)
2011 return err;
2012
2013 return 0;
2014 }
2015
2016 static int __ovs_ct_limit_get_zone_limit(struct net *net,
2017 struct nf_conncount_data *data,
2018 u16 zone_id, u32 limit,
2019 struct sk_buff *reply)
2020 {
2021 struct nf_conntrack_zone ct_zone;
2022 struct ovs_zone_limit zone_limit;
2023 u32 conncount_key = zone_id;
2024
2025 zone_limit.zone_id = zone_id;
2026 zone_limit.limit = limit;
2027 nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
2028
2029 zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
2030 &ct_zone);
2031 return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
2032 }
2033
2034 static int ovs_ct_limit_get_zone_limit(struct net *net,
2035 struct nlattr *nla_zone_limit,
2036 struct ovs_ct_limit_info *info,
2037 struct sk_buff *reply)
2038 {
2039 struct ovs_zone_limit *zone_limit;
2040 int rem, err;
2041 u32 limit;
2042 u16 zone;
2043
2044 rem = NLA_ALIGN(nla_len(nla_zone_limit));
2045 zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
2046
2047 while (rem >= sizeof(*zone_limit)) {
2048 if (unlikely(zone_limit->zone_id ==
2049 OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
2050 err = ovs_ct_limit_get_default_limit(info, reply);
2051 if (err)
2052 return err;
2053 } else if (unlikely(!check_zone_id(zone_limit->zone_id,
2054 &zone))) {
2055 OVS_NLERR(true, "zone id is out of range");
2056 } else {
2057 rcu_read_lock();
2058 limit = ct_limit_get(info, zone);
2059 rcu_read_unlock();
2060
2061 err = __ovs_ct_limit_get_zone_limit(
2062 net, info->data, zone, limit, reply);
2063 if (err)
2064 return err;
2065 }
2066 rem -= NLA_ALIGN(sizeof(*zone_limit));
2067 zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
2068 NLA_ALIGN(sizeof(*zone_limit)));
2069 }
2070
2071 if (rem)
2072 OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
2073
2074 return 0;
2075 }
2076
2077 static int ovs_ct_limit_get_all_zone_limit(struct net *net,
2078 struct ovs_ct_limit_info *info,
2079 struct sk_buff *reply)
2080 {
2081 struct ovs_ct_limit *ct_limit;
2082 struct hlist_head *head;
2083 int i, err = 0;
2084
2085 err = ovs_ct_limit_get_default_limit(info, reply);
2086 if (err)
2087 return err;
2088
2089 rcu_read_lock();
2090 for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
2091 head = &info->limits[i];
2092 hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
2093 err = __ovs_ct_limit_get_zone_limit(net, info->data,
2094 ct_limit->zone, ct_limit->limit, reply);
2095 if (err)
2096 goto exit_err;
2097 }
2098 }
2099
2100 exit_err:
2101 rcu_read_unlock();
2102 return err;
2103 }
2104
2105 static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
2106 {
2107 struct nlattr **a = info->attrs;
2108 struct sk_buff *reply;
2109 struct ovs_header *ovs_reply_header;
2110 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2111 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2112 int err;
2113
2114 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
2115 &ovs_reply_header);
2116 if (IS_ERR(reply))
2117 return PTR_ERR(reply);
2118
2119 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2120 err = -EINVAL;
2121 goto exit_err;
2122 }
2123
2124 err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2125 ct_limit_info);
2126 if (err)
2127 goto exit_err;
2128
2129 static_branch_enable(&ovs_ct_limit_enabled);
2130
2131 genlmsg_end(reply, ovs_reply_header);
2132 return genlmsg_reply(reply, info);
2133
2134 exit_err:
2135 nlmsg_free(reply);
2136 return err;
2137 }
2138
2139 static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
2140 {
2141 struct nlattr **a = info->attrs;
2142 struct sk_buff *reply;
2143 struct ovs_header *ovs_reply_header;
2144 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
2145 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2146 int err;
2147
2148 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
2149 &ovs_reply_header);
2150 if (IS_ERR(reply))
2151 return PTR_ERR(reply);
2152
2153 if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2154 err = -EINVAL;
2155 goto exit_err;
2156 }
2157
2158 err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
2159 ct_limit_info);
2160 if (err)
2161 goto exit_err;
2162
2163 genlmsg_end(reply, ovs_reply_header);
2164 return genlmsg_reply(reply, info);
2165
2166 exit_err:
2167 nlmsg_free(reply);
2168 return err;
2169 }
2170
2171 static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
2172 {
2173 struct nlattr **a = info->attrs;
2174 struct nlattr *nla_reply;
2175 struct sk_buff *reply;
2176 struct ovs_header *ovs_reply_header;
2177 struct net *net = sock_net(skb->sk);
2178 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2179 struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
2180 int err;
2181
2182 reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
2183 &ovs_reply_header);
2184 if (IS_ERR(reply))
2185 return PTR_ERR(reply);
2186
2187 nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
2188
2189 if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
2190 err = ovs_ct_limit_get_zone_limit(
2191 net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
2192 reply);
2193 if (err)
2194 goto exit_err;
2195 } else {
2196 err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
2197 reply);
2198 if (err)
2199 goto exit_err;
2200 }
2201
2202 nla_nest_end(reply, nla_reply);
2203 genlmsg_end(reply, ovs_reply_header);
2204 return genlmsg_reply(reply, info);
2205
2206 exit_err:
2207 nlmsg_free(reply);
2208 return err;
2209 }
2210
2211 static struct genl_ops ct_limit_genl_ops[] = {
2212 { .cmd = OVS_CT_LIMIT_CMD_SET,
2213 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2214 * privilege. */
2215 .policy = ct_limit_policy,
2216 .doit = ovs_ct_limit_cmd_set,
2217 },
2218 { .cmd = OVS_CT_LIMIT_CMD_DEL,
2219 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
2220 * privilege. */
2221 .policy = ct_limit_policy,
2222 .doit = ovs_ct_limit_cmd_del,
2223 },
2224 { .cmd = OVS_CT_LIMIT_CMD_GET,
2225 .flags = 0, /* OK for unprivileged users. */
2226 .policy = ct_limit_policy,
2227 .doit = ovs_ct_limit_cmd_get,
2228 },
2229 };
2230
2231 static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
2232 .name = OVS_CT_LIMIT_MCGROUP,
2233 };
2234
2235 struct genl_family dp_ct_limit_genl_family __ro_after_init = {
2236 .hdrsize = sizeof(struct ovs_header),
2237 .name = OVS_CT_LIMIT_FAMILY,
2238 .version = OVS_CT_LIMIT_VERSION,
2239 .maxattr = OVS_CT_LIMIT_ATTR_MAX,
2240 .netnsok = true,
2241 .parallel_ops = true,
2242 .ops = ct_limit_genl_ops,
2243 .n_ops = ARRAY_SIZE(ct_limit_genl_ops),
2244 .mcgrps = &ovs_ct_limit_multicast_group,
2245 .n_mcgrps = 1,
2246 .module = THIS_MODULE,
2247 };
2248 #endif
2249
2250 int ovs_ct_init(struct net *net)
2251 {
2252 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
2253 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2254
2255 if (nf_connlabels_get(net, n_bits - 1)) {
2256 ovs_net->xt_label = false;
2257 OVS_NLERR(true, "Failed to set connlabel length");
2258 } else {
2259 ovs_net->xt_label = true;
2260 }
2261
2262 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2263 return ovs_ct_limit_init(net, ovs_net);
2264 #else
2265 return 0;
2266 #endif
2267 }
2268
2269 void ovs_ct_exit(struct net *net)
2270 {
2271 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2272
2273 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2274 ovs_ct_limit_exit(net, ovs_net);
2275 #endif
2276
2277 if (ovs_net->xt_label)
2278 nf_connlabels_put(net);
2279 }
2280
2281 #endif /* CONFIG_NF_CONNTRACK */