2 * Copyright (c) 2015 Nicira, Inc.
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.
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.
14 #include <linux/kconfig.h>
15 #include <linux/version.h>
17 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
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>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_labels.h>
28 #include <net/netfilter/nf_conntrack_seqadj.h>
29 #include <net/netfilter/nf_conntrack_zones.h>
30 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
32 #ifdef CONFIG_NF_NAT_NEEDED
33 #include <linux/netfilter/nf_nat.h>
34 #include <net/netfilter/nf_nat_core.h>
35 #include <net/netfilter/nf_nat_l3proto.h>
39 #include "conntrack.h"
41 #include "flow_netlink.h"
44 struct ovs_ct_len_tbl {
49 /* Metadata mark for masked write to conntrack mark */
55 /* Metadata label for masked write to conntrack label. */
57 struct ovs_key_ct_labels value;
58 struct ovs_key_ct_labels mask;
62 OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
63 OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
64 OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
67 /* Conntrack action context for execution. */
68 struct ovs_conntrack_info {
69 struct nf_conntrack_helper *helper;
70 struct nf_conntrack_zone zone;
73 u8 nat : 3; /* enum ovs_ct_nat */
74 u8 random_fully_compat : 1; /* bool */
77 struct md_labels labels;
78 #ifdef CONFIG_NF_NAT_NEEDED
79 struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
83 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
85 static u16 key_to_nfproto(const struct sw_flow_key *key)
87 switch (ntohs(key->eth.type)) {
93 return NFPROTO_UNSPEC;
97 /* Map SKB connection state into the values used by flow definition. */
98 static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
100 u8 ct_state = OVS_CS_F_TRACKED;
103 case IP_CT_ESTABLISHED_REPLY:
104 case IP_CT_RELATED_REPLY:
105 ct_state |= OVS_CS_F_REPLY_DIR;
112 case IP_CT_ESTABLISHED:
113 case IP_CT_ESTABLISHED_REPLY:
114 ct_state |= OVS_CS_F_ESTABLISHED;
117 case IP_CT_RELATED_REPLY:
118 ct_state |= OVS_CS_F_RELATED;
121 ct_state |= OVS_CS_F_NEW;
130 static u32 ovs_ct_get_mark(const struct nf_conn *ct)
132 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
133 return ct ? ct->mark : 0;
139 static void ovs_ct_get_labels(const struct nf_conn *ct,
140 struct ovs_key_ct_labels *labels)
142 struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
145 size_t len = cl->words * sizeof(long);
147 if (len > OVS_CT_LABELS_LEN)
148 len = OVS_CT_LABELS_LEN;
149 else if (len < OVS_CT_LABELS_LEN)
150 memset(labels, 0, OVS_CT_LABELS_LEN);
151 memcpy(labels, cl->bits, len);
153 memset(labels, 0, OVS_CT_LABELS_LEN);
157 static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
158 const struct nf_conntrack_zone *zone,
159 const struct nf_conn *ct)
161 key->ct.state = state;
162 key->ct.zone = zone->id;
163 key->ct.mark = ovs_ct_get_mark(ct);
164 ovs_ct_get_labels(ct, &key->ct.labels);
167 /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
168 * previously sent the packet to conntrack via the ct action. If
169 * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
170 * initialized from the connection status.
172 static void ovs_ct_update_key(const struct sk_buff *skb,
173 const struct ovs_conntrack_info *info,
174 struct sw_flow_key *key, bool post_ct,
177 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
178 enum ip_conntrack_info ctinfo;
182 ct = nf_ct_get(skb, &ctinfo);
184 state = ovs_ct_get_state(ctinfo);
185 /* All unconfirmed entries are NEW connections. */
186 if (!nf_ct_is_confirmed(ct))
187 state |= OVS_CS_F_NEW;
188 /* OVS persists the related flag for the duration of the
192 state |= OVS_CS_F_RELATED;
193 if (keep_nat_flags) {
194 state |= key->ct.state & OVS_CS_F_NAT_MASK;
196 if (ct->status & IPS_SRC_NAT)
197 state |= OVS_CS_F_SRC_NAT;
198 if (ct->status & IPS_DST_NAT)
199 state |= OVS_CS_F_DST_NAT;
201 zone = nf_ct_zone(ct);
202 } else if (post_ct) {
203 state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
207 __ovs_ct_update_key(key, state, zone, ct);
210 /* This is called to initialize CT key fields possibly coming in from the local
213 void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
215 ovs_ct_update_key(skb, NULL, key, false, false);
218 int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
220 if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
223 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
224 nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
227 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
228 nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
231 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
232 nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
239 static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
240 u32 ct_mark, u32 mask)
242 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
243 enum ip_conntrack_info ctinfo;
247 /* The connection could be invalid, in which case set_mark is no-op. */
248 ct = nf_ct_get(skb, &ctinfo);
252 new_mark = ct_mark | (ct->mark & ~(mask));
253 if (ct->mark != new_mark) {
255 nf_conntrack_event_cache(IPCT_MARK, ct);
256 key->ct.mark = new_mark;
265 static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
266 const struct ovs_key_ct_labels *labels,
267 const struct ovs_key_ct_labels *mask)
269 enum ip_conntrack_info ctinfo;
270 struct nf_conn_labels *cl;
274 /* The connection could be invalid, in which case set_label is no-op.*/
275 ct = nf_ct_get(skb, &ctinfo);
279 cl = nf_ct_labels_find(ct);
281 nf_ct_labels_ext_add(ct);
282 cl = nf_ct_labels_find(ct);
284 if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
287 err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
288 OVS_CT_LABELS_LEN / sizeof(u32));
292 ovs_ct_get_labels(ct, &key->ct.labels);
296 /* 'skb' should already be pulled to nh_ofs. */
297 static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
299 const struct nf_conntrack_helper *helper;
300 const struct nf_conn_help *help;
301 enum ip_conntrack_info ctinfo;
302 unsigned int protoff;
307 ct = nf_ct_get(skb, &ctinfo);
308 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
311 help = nfct_help(ct);
315 helper = rcu_dereference(help->helper);
321 protoff = ip_hdrlen(skb);
327 nexthdr = ipv6_hdr(skb)->nexthdr;
328 ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
330 if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
331 pr_debug("proto header not found\n");
338 WARN_ONCE(1, "helper invoked on non-IP family!");
342 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
343 /* Linux 4.5 and older depend on skb_dst being set when recalculating
344 * checksums after NAT helper has mangled TCP or UDP packet payload.
345 * This dependency is avoided when skb is CHECKSUM_PARTIAL or when UDP
348 * The dependency is not triggered when the main NAT code updates
349 * checksums after translating the IP header (address, port), so this
350 * fix only needs to be executed on packets that are both being NATted
351 * and that have a helper assigned.
353 if (ct->status & IPS_NAT_MASK && skb->ip_summed != CHECKSUM_PARTIAL) {
354 u8 ipproto = (proto == NFPROTO_IPV4)
355 ? ip_hdr(skb)->protocol : nexthdr;
360 offset = offsetof(struct tcphdr, check);
363 /* Skip if no csum. */
364 if (udp_hdr(skb)->check)
365 offset = offsetof(struct udphdr, check);
369 if (unlikely(!pskb_may_pull(skb, protoff + offset + 2)))
372 skb->csum_start = skb_headroom(skb) + protoff;
373 skb->csum_offset = offset;
374 skb->ip_summed = CHECKSUM_PARTIAL;
378 err = helper->help(skb, protoff, ct, ctinfo);
379 if (err != NF_ACCEPT)
382 /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
383 * FTP with NAT) adusting the TCP payload size when mangling IP
384 * addresses and/or port numbers in the text-based control connection.
386 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
387 !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
392 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
393 * value if 'skb' is freed.
395 static int handle_fragments(struct net *net, struct sw_flow_key *key,
396 u16 zone, struct sk_buff *skb)
398 struct ovs_gso_cb ovs_cb = *OVS_GSO_CB(skb);
401 if (key->eth.type == htons(ETH_P_IP)) {
402 enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
404 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
405 err = ip_defrag(net, skb, user);
409 ovs_cb.dp_cb.mru = IPCB(skb)->frag_max_size;
410 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
411 } else if (key->eth.type == htons(ETH_P_IPV6)) {
412 enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
415 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
416 err = nf_ct_frag6_gather(net, skb, user);
420 key->ip.proto = ipv6_hdr(skb)->nexthdr;
421 ovs_cb.dp_cb.mru = IP6CB(skb)->frag_max_size;
422 #endif /* IP frag support */
425 return -EPFNOSUPPORT;
428 key->ip.frag = OVS_FRAG_TYPE_NONE;
431 *OVS_GSO_CB(skb) = ovs_cb;
436 static struct nf_conntrack_expect *
437 ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
438 u16 proto, const struct sk_buff *skb)
440 struct nf_conntrack_tuple tuple;
442 if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
444 return __nf_ct_expect_find(net, zone, &tuple);
447 /* This replicates logic from nf_conntrack_core.c that is not exported. */
448 static enum ip_conntrack_info
449 ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
451 const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
453 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
454 return IP_CT_ESTABLISHED_REPLY;
455 /* Once we've had two way comms, always ESTABLISHED. */
456 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
457 return IP_CT_ESTABLISHED;
458 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
459 return IP_CT_RELATED;
463 /* Find an existing connection which this packet belongs to without
464 * re-attributing statistics or modifying the connection state. This allows an
465 * skb->nfct lost due to an upcall to be recovered during actions execution.
467 * Must be called with rcu_read_lock.
469 * On success, populates skb->nfct and skb->nfctinfo, and returns the
470 * connection. Returns NULL if there is no existing entry.
472 static struct nf_conn *
473 ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
474 u8 l3num, struct sk_buff *skb)
476 struct nf_conntrack_l3proto *l3proto;
477 struct nf_conntrack_l4proto *l4proto;
478 struct nf_conntrack_tuple tuple;
479 struct nf_conntrack_tuple_hash *h;
480 enum ip_conntrack_info ctinfo;
482 unsigned int dataoff;
485 l3proto = __nf_ct_l3proto_find(l3num);
486 if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
488 pr_debug("ovs_ct_find_existing: Can't get protonum\n");
491 l4proto = __nf_ct_l4proto_find(l3num, protonum);
492 if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
493 protonum, net, &tuple, l3proto, l4proto)) {
494 pr_debug("ovs_ct_find_existing: Can't get tuple\n");
498 /* look for tuple match */
499 h = nf_conntrack_find_get(net, zone, &tuple);
501 return NULL; /* Not found. */
503 ct = nf_ct_tuplehash_to_ctrack(h);
505 ctinfo = ovs_ct_get_info(h);
506 if (ctinfo == IP_CT_NEW) {
507 /* This should not happen. */
508 WARN_ONCE(1, "ovs_ct_find_existing: new packet for %p\n", ct);
510 skb->nfct = &ct->ct_general;
511 skb->nfctinfo = ctinfo;
515 /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
516 static bool skb_nfct_cached(struct net *net,
517 const struct sw_flow_key *key,
518 const struct ovs_conntrack_info *info,
521 enum ip_conntrack_info ctinfo;
524 ct = nf_ct_get(skb, &ctinfo);
525 /* If no ct, check if we have evidence that an existing conntrack entry
526 * might be found for this skb. This happens when we lose a skb->nfct
527 * due to an upcall. If the connection was not confirmed, it is not
528 * cached and needs to be run through conntrack again.
530 if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
531 !(key->ct.state & OVS_CS_F_INVALID) &&
532 key->ct.zone == info->zone.id)
533 ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
536 if (!net_eq(net, read_pnet(&ct->ct_net)))
538 if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
541 struct nf_conn_help *help;
543 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
544 if (help && rcu_access_pointer(help->helper) != info->helper)
551 #ifdef CONFIG_NF_NAT_NEEDED
552 /* Modelled after nf_nat_ipv[46]_fn().
553 * range is only used for new, uninitialized NAT state.
554 * Returns either NF_ACCEPT or NF_DROP.
556 static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
557 enum ip_conntrack_info ctinfo,
558 const struct nf_nat_range *range,
559 enum nf_nat_manip_type maniptype)
561 int hooknum, nh_off, err = NF_ACCEPT;
563 nh_off = skb_network_offset(skb);
564 skb_pull(skb, nh_off);
566 /* See HOOK2MANIP(). */
567 if (maniptype == NF_NAT_MANIP_SRC)
568 hooknum = NF_INET_LOCAL_IN; /* Source NAT */
570 hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
574 case IP_CT_RELATED_REPLY:
575 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
576 skb->protocol == htons(ETH_P_IP) &&
577 ip_hdr(skb)->protocol == IPPROTO_ICMP) {
578 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
582 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
583 skb->protocol == htons(ETH_P_IPV6)) {
585 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
586 int hdrlen = ipv6_skip_exthdr(skb,
587 sizeof(struct ipv6hdr),
588 &nexthdr, &frag_off);
590 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
591 if (!nf_nat_icmpv6_reply_translation(skb, ct,
599 /* Non-ICMP, fall thru to initialize if needed. */
601 /* Seen it before? This can happen for loopback, retrans,
604 if (!nf_nat_initialized(ct, maniptype)) {
605 /* Initialize according to the NAT action. */
606 err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
607 /* Action is set up to establish a new
610 ? nf_nat_setup_info(ct, range, maniptype)
611 : nf_nat_alloc_null_binding(ct, hooknum);
612 if (err != NF_ACCEPT)
617 case IP_CT_ESTABLISHED:
618 case IP_CT_ESTABLISHED_REPLY:
626 err = nf_nat_packet(ct, ctinfo, hooknum, skb);
628 skb_push(skb, nh_off);
633 static void ovs_nat_update_key(struct sw_flow_key *key,
634 const struct sk_buff *skb,
635 enum nf_nat_manip_type maniptype)
637 if (maniptype == NF_NAT_MANIP_SRC) {
640 key->ct.state |= OVS_CS_F_SRC_NAT;
641 if (key->eth.type == htons(ETH_P_IP))
642 key->ipv4.addr.src = ip_hdr(skb)->saddr;
643 else if (key->eth.type == htons(ETH_P_IPV6))
644 memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
645 sizeof(key->ipv6.addr.src));
649 if (key->ip.proto == IPPROTO_UDP)
650 src = udp_hdr(skb)->source;
651 else if (key->ip.proto == IPPROTO_TCP)
652 src = tcp_hdr(skb)->source;
653 else if (key->ip.proto == IPPROTO_SCTP)
654 src = sctp_hdr(skb)->source;
662 key->ct.state |= OVS_CS_F_DST_NAT;
663 if (key->eth.type == htons(ETH_P_IP))
664 key->ipv4.addr.dst = ip_hdr(skb)->daddr;
665 else if (key->eth.type == htons(ETH_P_IPV6))
666 memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
667 sizeof(key->ipv6.addr.dst));
671 if (key->ip.proto == IPPROTO_UDP)
672 dst = udp_hdr(skb)->dest;
673 else if (key->ip.proto == IPPROTO_TCP)
674 dst = tcp_hdr(skb)->dest;
675 else if (key->ip.proto == IPPROTO_SCTP)
676 dst = sctp_hdr(skb)->dest;
684 /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
685 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
686 const struct ovs_conntrack_info *info,
687 struct sk_buff *skb, struct nf_conn *ct,
688 enum ip_conntrack_info ctinfo)
690 enum nf_nat_manip_type maniptype;
693 if (nf_ct_is_untracked(ct)) {
694 /* A NAT action may only be performed on tracked packets. */
698 /* Add NAT extension if not confirmed yet. */
699 if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
700 return NF_ACCEPT; /* Can't NAT. */
702 /* Determine NAT type.
703 * Check if the NAT type can be deduced from the tracked connection.
704 * Make sure new expected connections (IP_CT_RELATED) are NATted only
707 if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
708 ct->status & IPS_NAT_MASK &&
709 (ctinfo != IP_CT_RELATED || info->commit)) {
710 /* NAT an established or related connection like before. */
711 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
712 /* This is the REPLY direction for a connection
713 * for which NAT was applied in the forward
714 * direction. Do the reverse NAT.
716 maniptype = ct->status & IPS_SRC_NAT
717 ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
719 maniptype = ct->status & IPS_SRC_NAT
720 ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
721 } else if (info->nat & OVS_CT_SRC_NAT) {
722 maniptype = NF_NAT_MANIP_SRC;
723 } else if (info->nat & OVS_CT_DST_NAT) {
724 maniptype = NF_NAT_MANIP_DST;
726 return NF_ACCEPT; /* Connection is not NATed. */
728 err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
730 /* Mark NAT done if successful and update the flow key. */
731 if (err == NF_ACCEPT)
732 ovs_nat_update_key(key, skb, maniptype);
736 #else /* !CONFIG_NF_NAT_NEEDED */
737 static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
738 const struct ovs_conntrack_info *info,
739 struct sk_buff *skb, struct nf_conn *ct,
740 enum ip_conntrack_info ctinfo)
746 /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
747 * not done already. Update key with new CT state after passing the packet
749 * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
750 * set to NULL and 0 will be returned.
752 static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
753 const struct ovs_conntrack_info *info,
756 /* If we are recirculating packets to match on conntrack fields and
757 * committing with a separate conntrack action, then we don't need to
758 * actually run the packet through conntrack twice unless it's for a
761 bool cached = skb_nfct_cached(net, key, info, skb);
762 enum ip_conntrack_info ctinfo;
766 struct nf_conn *tmpl = info->ct;
769 /* Associate skb with specified zone. */
772 nf_conntrack_put(skb->nfct);
773 nf_conntrack_get(&tmpl->ct_general);
774 skb->nfct = &tmpl->ct_general;
775 skb->nfctinfo = IP_CT_NEW;
778 /* Repeat if requested, see nf_iterate(). */
780 err = nf_conntrack_in(net, info->family,
781 NF_INET_FORWARD, skb);
782 } while (err == NF_REPEAT);
784 if (err != NF_ACCEPT)
787 /* Clear CT state NAT flags to mark that we have not yet done
788 * NAT after the nf_conntrack_in() call. We can actually clear
789 * the whole state, as it will be re-initialized below.
793 /* Update the key, but keep the NAT flags. */
794 ovs_ct_update_key(skb, info, key, true, true);
797 ct = nf_ct_get(skb, &ctinfo);
799 /* Packets starting a new connection must be NATted before the
800 * helper, so that the helper knows about the NAT. We enforce
801 * this by delaying both NAT and helper calls for unconfirmed
802 * connections until the committing CT action. For later
803 * packets NAT and Helper may be called in either order.
805 * NAT will be done only if the CT action has NAT, and only
806 * once per packet (per zone), as guarded by the NAT bits in
809 if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
810 (nf_ct_is_confirmed(ct) || info->commit) &&
811 ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
815 /* Userspace may decide to perform a ct lookup without a helper
816 * specified followed by a (recirculate and) commit with one.
817 * Therefore, for unconfirmed connections which we will commit,
818 * we need to attach the helper here.
820 if (!nf_ct_is_confirmed(ct) && info->commit &&
821 info->helper && !nfct_help(ct)) {
822 int err = __nf_ct_try_assign_helper(ct, info->ct,
828 /* Call the helper only if:
829 * - nf_conntrack_in() was executed above ("!cached") for a
830 * confirmed connection, or
831 * - When committing an unconfirmed connection.
833 if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
834 ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
842 /* Lookup connection and read fields into key. */
843 static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
844 const struct ovs_conntrack_info *info,
847 struct nf_conntrack_expect *exp;
849 /* If we pass an expected packet through nf_conntrack_in() the
850 * expectation is typically removed, but the packet could still be
851 * lost in upcall processing. To prevent this from happening we
852 * perform an explicit expectation lookup. Expected connections are
853 * always new, and will be passed through conntrack only when they are
854 * committed, as it is OK to remove the expectation at that time.
856 exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
860 /* NOTE: New connections are NATted and Helped only when
861 * committed, so we are not calling into NAT here.
863 state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
864 __ovs_ct_update_key(key, state, &info->zone, exp->master);
866 return __ovs_ct_lookup(net, key, info, skb);
871 static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
875 for (i = 0; i < sizeof(*labels); i++)
876 if (labels->ct_labels[i])
882 /* Lookup connection and confirm if unconfirmed. */
883 static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
884 const struct ovs_conntrack_info *info,
889 err = __ovs_ct_lookup(net, key, info, skb);
893 /* Apply changes before confirming the connection so that the initial
894 * conntrack NEW netlink event carries the values given in the CT
897 if (info->mark.mask) {
898 err = ovs_ct_set_mark(skb, key, info->mark.value,
903 if (labels_nonzero(&info->labels.mask)) {
904 err = ovs_ct_set_labels(skb, key, &info->labels.value,
909 /* This will take care of sending queued events even if the connection
910 * is already confirmed.
912 if (nf_conntrack_confirm(skb) != NF_ACCEPT)
918 /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
919 * value if 'skb' is freed.
921 int ovs_ct_execute(struct net *net, struct sk_buff *skb,
922 struct sw_flow_key *key,
923 const struct ovs_conntrack_info *info)
928 /* The conntrack module expects to be working at L3. */
929 nh_ofs = skb_network_offset(skb);
930 skb_pull(skb, nh_ofs);
932 if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
933 err = handle_fragments(net, key, info->zone.id, skb);
939 err = ovs_ct_commit(net, key, info, skb);
941 err = ovs_ct_lookup(net, key, info, skb);
943 skb_push(skb, nh_ofs);
949 static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
950 const struct sw_flow_key *key, bool log)
952 struct nf_conntrack_helper *helper;
953 struct nf_conn_help *help;
955 helper = nf_conntrack_helper_try_module_get(name, info->family,
958 OVS_NLERR(log, "Unknown helper \"%s\"", name);
962 help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
964 module_put(helper->me);
968 rcu_assign_pointer(help->helper, helper);
969 info->helper = helper;
973 #ifdef CONFIG_NF_NAT_NEEDED
974 static int parse_nat(const struct nlattr *attr,
975 struct ovs_conntrack_info *info, bool log)
979 bool have_ip_max = false;
980 bool have_proto_max = false;
981 bool ip_vers = (info->family == NFPROTO_IPV6);
983 nla_for_each_nested(a, attr, rem) {
984 static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
985 [OVS_NAT_ATTR_SRC] = {0, 0},
986 [OVS_NAT_ATTR_DST] = {0, 0},
987 [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
988 sizeof(struct in6_addr)},
989 [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
990 sizeof(struct in6_addr)},
991 [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
992 [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
993 [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
994 [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
995 [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
997 int type = nla_type(a);
999 if (type > OVS_NAT_ATTR_MAX) {
1001 "Unknown NAT attribute (type=%d, max=%d).\n",
1002 type, OVS_NAT_ATTR_MAX);
1006 if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
1008 "NAT attribute type %d has unexpected length (%d != %d).\n",
1010 ovs_nat_attr_lens[type][ip_vers]);
1015 case OVS_NAT_ATTR_SRC:
1016 case OVS_NAT_ATTR_DST:
1019 "Only one type of NAT may be specified.\n"
1023 info->nat |= OVS_CT_NAT;
1024 info->nat |= ((type == OVS_NAT_ATTR_SRC)
1025 ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
1028 case OVS_NAT_ATTR_IP_MIN:
1029 nla_memcpy(&info->range.min_addr, a,
1030 sizeof(info->range.min_addr));
1031 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1034 case OVS_NAT_ATTR_IP_MAX:
1036 nla_memcpy(&info->range.max_addr, a,
1037 sizeof(info->range.max_addr));
1038 info->range.flags |= NF_NAT_RANGE_MAP_IPS;
1041 case OVS_NAT_ATTR_PROTO_MIN:
1042 info->range.min_proto.all = htons(nla_get_u16(a));
1043 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1046 case OVS_NAT_ATTR_PROTO_MAX:
1047 have_proto_max = true;
1048 info->range.max_proto.all = htons(nla_get_u16(a));
1049 info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
1052 case OVS_NAT_ATTR_PERSISTENT:
1053 info->range.flags |= NF_NAT_RANGE_PERSISTENT;
1056 case OVS_NAT_ATTR_PROTO_HASH:
1057 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1060 case OVS_NAT_ATTR_PROTO_RANDOM:
1061 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1062 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
1064 info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
1065 info->random_fully_compat = true;
1070 OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
1076 OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
1080 /* Do not allow flags if no type is given. */
1081 if (info->range.flags) {
1083 "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
1087 info->nat = OVS_CT_NAT; /* NAT existing connections. */
1088 } else if (!info->commit) {
1090 "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
1094 /* Allow missing IP_MAX. */
1095 if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
1096 memcpy(&info->range.max_addr, &info->range.min_addr,
1097 sizeof(info->range.max_addr));
1099 /* Allow missing PROTO_MAX. */
1100 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1102 info->range.max_proto.all = info->range.min_proto.all;
1108 static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
1109 [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
1110 [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
1111 .maxlen = sizeof(u16) },
1112 [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
1113 .maxlen = sizeof(struct md_mark) },
1114 [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
1115 .maxlen = sizeof(struct md_labels) },
1116 [OVS_CT_ATTR_HELPER] = { .minlen = 1,
1117 .maxlen = NF_CT_HELPER_NAME_LEN },
1118 #ifdef CONFIG_NF_NAT_NEEDED
1119 /* NAT length is checked when parsing the nested attributes. */
1120 [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
1124 static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
1125 const char **helper, bool log)
1130 nla_for_each_nested(a, attr, rem) {
1131 int type = nla_type(a);
1132 int maxlen = ovs_ct_attr_lens[type].maxlen;
1133 int minlen = ovs_ct_attr_lens[type].minlen;
1135 if (type > OVS_CT_ATTR_MAX) {
1137 "Unknown conntrack attr (type=%d, max=%d)",
1138 type, OVS_CT_ATTR_MAX);
1141 if (nla_len(a) < minlen || nla_len(a) > maxlen) {
1143 "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
1144 type, nla_len(a), maxlen);
1149 case OVS_CT_ATTR_COMMIT:
1150 info->commit = true;
1152 #ifdef CONFIG_NF_CONNTRACK_ZONES
1153 case OVS_CT_ATTR_ZONE:
1154 info->zone.id = nla_get_u16(a);
1157 #ifdef CONFIG_NF_CONNTRACK_MARK
1158 case OVS_CT_ATTR_MARK: {
1159 struct md_mark *mark = nla_data(a);
1162 OVS_NLERR(log, "ct_mark mask cannot be 0");
1169 #ifdef CONFIG_NF_CONNTRACK_LABELS
1170 case OVS_CT_ATTR_LABELS: {
1171 struct md_labels *labels = nla_data(a);
1173 if (!labels_nonzero(&labels->mask)) {
1174 OVS_NLERR(log, "ct_labels mask cannot be 0");
1177 info->labels = *labels;
1181 case OVS_CT_ATTR_HELPER:
1182 *helper = nla_data(a);
1183 if (!memchr(*helper, '\0', nla_len(a))) {
1184 OVS_NLERR(log, "Invalid conntrack helper");
1188 #ifdef CONFIG_NF_NAT_NEEDED
1189 case OVS_CT_ATTR_NAT: {
1190 int err = parse_nat(a, info, log);
1198 OVS_NLERR(log, "Unknown conntrack attr (%d)",
1204 #ifdef CONFIG_NF_CONNTRACK_MARK
1205 if (!info->commit && info->mark.mask) {
1207 "Setting conntrack mark requires 'commit' flag.");
1211 #ifdef CONFIG_NF_CONNTRACK_LABELS
1212 if (!info->commit && labels_nonzero(&info->labels.mask)) {
1214 "Setting conntrack labels requires 'commit' flag.");
1219 OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
1226 bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
1228 if (attr == OVS_KEY_ATTR_CT_STATE)
1230 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1231 attr == OVS_KEY_ATTR_CT_ZONE)
1233 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
1234 attr == OVS_KEY_ATTR_CT_MARK)
1236 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1237 attr == OVS_KEY_ATTR_CT_LABELS) {
1238 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1240 return ovs_net->xt_label;
1246 int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
1247 const struct sw_flow_key *key,
1248 struct sw_flow_actions **sfa, bool log)
1250 struct ovs_conntrack_info ct_info;
1251 const char *helper = NULL;
1255 family = key_to_nfproto(key);
1256 if (family == NFPROTO_UNSPEC) {
1257 OVS_NLERR(log, "ct family unspecified");
1261 memset(&ct_info, 0, sizeof(ct_info));
1262 ct_info.family = family;
1264 nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
1265 NF_CT_DEFAULT_ZONE_DIR, 0);
1267 err = parse_ct(attr, &ct_info, &helper, log);
1271 /* Set up template for tracking connections in specific zones. */
1272 ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
1274 OVS_NLERR(log, "Failed to allocate conntrack template");
1278 __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
1279 nf_conntrack_get(&ct_info.ct->ct_general);
1282 err = ovs_ct_add_helper(&ct_info, helper, key, log);
1287 err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
1288 sizeof(ct_info), log);
1294 __ovs_ct_free_action(&ct_info);
1298 #ifdef CONFIG_NF_NAT_NEEDED
1299 static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
1300 struct sk_buff *skb)
1302 struct nlattr *start;
1304 start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
1308 if (info->nat & OVS_CT_SRC_NAT) {
1309 if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
1311 } else if (info->nat & OVS_CT_DST_NAT) {
1312 if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
1318 if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
1319 if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
1320 info->family == NFPROTO_IPV4) {
1321 if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
1322 info->range.min_addr.ip) ||
1323 (info->range.max_addr.ip
1324 != info->range.min_addr.ip &&
1325 (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
1326 info->range.max_addr.ip))))
1328 } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
1329 info->family == NFPROTO_IPV6) {
1330 if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
1331 &info->range.min_addr.in6) ||
1332 (memcmp(&info->range.max_addr.in6,
1333 &info->range.min_addr.in6,
1334 sizeof(info->range.max_addr.in6)) &&
1335 (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
1336 &info->range.max_addr.in6))))
1342 if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
1343 (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
1344 ntohs(info->range.min_proto.all)) ||
1345 (info->range.max_proto.all != info->range.min_proto.all &&
1346 nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
1347 ntohs(info->range.max_proto.all)))))
1350 if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
1351 nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
1353 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
1354 nla_put_flag(skb, info->random_fully_compat
1355 ? OVS_NAT_ATTR_PROTO_RANDOM
1356 : OVS_NAT_ATTR_PROTO_HASH))
1358 #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY
1359 if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
1360 nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
1364 nla_nest_end(skb, start);
1370 int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
1371 struct sk_buff *skb)
1373 struct nlattr *start;
1375 start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
1379 if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
1381 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
1382 nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
1384 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
1385 nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
1388 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
1389 labels_nonzero(&ct_info->labels.mask) &&
1390 nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
1393 if (ct_info->helper) {
1394 if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
1395 ct_info->helper->name))
1398 #ifdef CONFIG_NF_NAT_NEEDED
1399 if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
1402 nla_nest_end(skb, start);
1407 void ovs_ct_free_action(const struct nlattr *a)
1409 struct ovs_conntrack_info *ct_info = nla_data(a);
1411 __ovs_ct_free_action(ct_info);
1414 static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
1416 if (ct_info->helper)
1417 module_put(ct_info->helper->me);
1419 nf_ct_tmpl_free(ct_info->ct);
1422 void ovs_ct_init(struct net *net)
1424 unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
1425 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1427 if (nf_connlabels_get(net, n_bits - 1)) {
1428 ovs_net->xt_label = false;
1429 OVS_NLERR(true, "Failed to set connlabel length");
1431 ovs_net->xt_label = true;
1435 void ovs_ct_exit(struct net *net)
1437 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1439 if (ovs_net->xt_label)
1440 nf_connlabels_put(net);
1443 #endif /* CONFIG_NF_CONNTRACK */