2019-05-29 14:18:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-08-26 18:31:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Nicira, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/openvswitch.h>
|
2016-03-10 18:54:23 +00:00
|
|
|
#include <linux/tcp.h>
|
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <linux/sctp.h>
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#include <linux/static_key.h>
|
2023-02-08 13:31:53 +00:00
|
|
|
#include <linux/string_helpers.h>
|
2015-08-26 18:31:48 +00:00
|
|
|
#include <net/ip.h>
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#include <net/genetlink.h>
|
2015-08-26 18:31:48 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_count.h>
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_helper.h>
|
2015-08-26 18:31:52 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_labels.h>
|
2016-03-10 18:54:23 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_seqadj.h>
|
2019-03-26 18:31:14 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_timeout.h>
|
2015-08-26 18:31:48 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_zones.h>
|
|
|
|
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
|
2018-07-13 23:14:01 +00:00
|
|
|
#include <net/ipv6_frag.h>
|
2015-08-26 18:31:48 +00:00
|
|
|
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2019-02-19 16:38:27 +00:00
|
|
|
#include <net/netfilter/nf_nat.h>
|
2016-03-10 18:54:23 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-03 11:44:51 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_act_ct.h>
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
#include "datapath.h"
|
2023-08-11 14:12:52 +00:00
|
|
|
#include "drop.h"
|
2015-08-26 18:31:48 +00:00
|
|
|
#include "conntrack.h"
|
|
|
|
#include "flow.h"
|
|
|
|
#include "flow_netlink.h"
|
|
|
|
|
|
|
|
struct ovs_ct_len_tbl {
|
2016-03-10 18:54:23 +00:00
|
|
|
int maxlen;
|
|
|
|
int minlen;
|
2015-08-26 18:31:48 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 18:31:49 +00:00
|
|
|
/* Metadata mark for masked write to conntrack mark */
|
|
|
|
struct md_mark {
|
|
|
|
u32 value;
|
|
|
|
u32 mask;
|
|
|
|
};
|
|
|
|
|
2015-08-26 18:31:52 +00:00
|
|
|
/* Metadata label for masked write to conntrack label. */
|
2015-10-01 22:00:37 +00:00
|
|
|
struct md_labels {
|
|
|
|
struct ovs_key_ct_labels value;
|
|
|
|
struct ovs_key_ct_labels mask;
|
2015-08-26 18:31:52 +00:00
|
|
|
};
|
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
enum ovs_ct_nat {
|
|
|
|
OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
|
|
|
|
OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
|
|
|
|
OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
|
|
|
|
};
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
/* Conntrack action context for execution. */
|
|
|
|
struct ovs_conntrack_info {
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
struct nf_conntrack_helper *helper;
|
2015-08-26 18:31:48 +00:00
|
|
|
struct nf_conntrack_zone zone;
|
|
|
|
struct nf_conn *ct;
|
2015-10-06 18:00:01 +00:00
|
|
|
u8 commit : 1;
|
2016-03-10 18:54:23 +00:00
|
|
|
u8 nat : 3; /* enum ovs_ct_nat */
|
2017-02-09 19:22:00 +00:00
|
|
|
u8 force : 1;
|
2017-04-21 23:48:06 +00:00
|
|
|
u8 have_eventmask : 1;
|
2015-08-26 18:31:48 +00:00
|
|
|
u16 family;
|
2017-04-21 23:48:06 +00:00
|
|
|
u32 eventmask; /* Mask of 1 << IPCT_*. */
|
2015-08-26 18:31:49 +00:00
|
|
|
struct md_mark mark;
|
2015-10-01 22:00:37 +00:00
|
|
|
struct md_labels labels;
|
2019-03-26 18:31:14 +00:00
|
|
|
char timeout[CTNL_TIMEOUT_NAME_MAX];
|
2019-08-22 20:17:50 +00:00
|
|
|
struct nf_ct_timeout *nf_ct_timeout;
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2018-04-04 13:38:22 +00:00
|
|
|
struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
|
2016-03-10 18:54:23 +00:00
|
|
|
#endif
|
2015-08-26 18:31:48 +00:00
|
|
|
};
|
|
|
|
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
#define OVS_CT_LIMIT_UNLIMITED 0
|
|
|
|
#define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
|
|
|
|
#define CT_LIMIT_HASH_BUCKETS 512
|
|
|
|
static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
|
|
|
|
|
|
|
|
struct ovs_ct_limit {
|
|
|
|
/* Elements in ovs_ct_limit_info->limits hash table */
|
|
|
|
struct hlist_node hlist_node;
|
|
|
|
struct rcu_head rcu;
|
|
|
|
u16 zone;
|
|
|
|
u32 limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ovs_ct_limit_info {
|
|
|
|
u32 default_limit;
|
|
|
|
struct hlist_head *limits;
|
|
|
|
struct nf_conncount_data *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
|
|
|
|
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-02-09 19:21:58 +00:00
|
|
|
static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
|
|
|
|
|
2015-12-09 22:07:39 +00:00
|
|
|
static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
static u16 key_to_nfproto(const struct sw_flow_key *key)
|
|
|
|
{
|
|
|
|
switch (ntohs(key->eth.type)) {
|
|
|
|
case ETH_P_IP:
|
|
|
|
return NFPROTO_IPV4;
|
|
|
|
case ETH_P_IPV6:
|
|
|
|
return NFPROTO_IPV6;
|
|
|
|
default:
|
|
|
|
return NFPROTO_UNSPEC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map SKB connection state into the values used by flow definition. */
|
|
|
|
static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
|
|
|
|
{
|
|
|
|
u8 ct_state = OVS_CS_F_TRACKED;
|
|
|
|
|
|
|
|
switch (ctinfo) {
|
|
|
|
case IP_CT_ESTABLISHED_REPLY:
|
|
|
|
case IP_CT_RELATED_REPLY:
|
|
|
|
ct_state |= OVS_CS_F_REPLY_DIR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ctinfo) {
|
|
|
|
case IP_CT_ESTABLISHED:
|
|
|
|
case IP_CT_ESTABLISHED_REPLY:
|
|
|
|
ct_state |= OVS_CS_F_ESTABLISHED;
|
|
|
|
break;
|
|
|
|
case IP_CT_RELATED:
|
|
|
|
case IP_CT_RELATED_REPLY:
|
|
|
|
ct_state |= OVS_CS_F_RELATED;
|
|
|
|
break;
|
|
|
|
case IP_CT_NEW:
|
|
|
|
ct_state |= OVS_CS_F_NEW;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ct_state;
|
|
|
|
}
|
|
|
|
|
2015-08-29 02:22:11 +00:00
|
|
|
static u32 ovs_ct_get_mark(const struct nf_conn *ct)
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
|
2022-11-09 19:39:07 +00:00
|
|
|
return ct ? READ_ONCE(ct->mark) : 0;
|
2015-08-29 02:22:11 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:56 +00:00
|
|
|
/* Guard against conntrack labels max size shrinking below 128 bits. */
|
|
|
|
#if NF_CT_LABELS_MAX_SIZE < 16
|
|
|
|
#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
|
|
|
|
#endif
|
|
|
|
|
2015-10-01 22:00:37 +00:00
|
|
|
static void ovs_ct_get_labels(const struct nf_conn *ct,
|
|
|
|
struct ovs_key_ct_labels *labels)
|
2015-08-26 18:31:52 +00:00
|
|
|
{
|
|
|
|
struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
|
|
|
|
|
2017-02-09 19:21:56 +00:00
|
|
|
if (cl)
|
|
|
|
memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
|
|
|
|
else
|
2015-10-01 22:00:37 +00:00
|
|
|
memset(labels, 0, OVS_CT_LABELS_LEN);
|
2015-08-26 18:31:52 +00:00
|
|
|
}
|
|
|
|
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
|
|
|
|
const struct nf_conntrack_tuple *orig,
|
|
|
|
u8 icmp_proto)
|
|
|
|
{
|
2017-02-09 19:22:01 +00:00
|
|
|
key->ct_orig_proto = orig->dst.protonum;
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
if (orig->dst.protonum == icmp_proto) {
|
|
|
|
key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
|
|
|
|
key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
|
|
|
|
} else {
|
|
|
|
key->ct.orig_tp.src = orig->src.u.all;
|
|
|
|
key->ct.orig_tp.dst = orig->dst.u.all;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
|
2015-08-26 18:31:49 +00:00
|
|
|
const struct nf_conntrack_zone *zone,
|
|
|
|
const struct nf_conn *ct)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
2017-02-09 19:22:01 +00:00
|
|
|
key->ct_state = state;
|
|
|
|
key->ct_zone = zone->id;
|
2015-08-29 02:22:11 +00:00
|
|
|
key->ct.mark = ovs_ct_get_mark(ct);
|
2015-10-01 22:00:37 +00:00
|
|
|
ovs_ct_get_labels(ct, &key->ct.labels);
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
|
|
|
|
if (ct) {
|
|
|
|
const struct nf_conntrack_tuple *orig;
|
|
|
|
|
|
|
|
/* Use the master if we have one. */
|
|
|
|
if (ct->master)
|
|
|
|
ct = ct->master;
|
|
|
|
orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
|
|
|
|
|
|
|
|
/* IP version must match with the master connection. */
|
|
|
|
if (key->eth.type == htons(ETH_P_IP) &&
|
|
|
|
nf_ct_l3num(ct) == NFPROTO_IPV4) {
|
|
|
|
key->ipv4.ct_orig.src = orig->src.u3.ip;
|
|
|
|
key->ipv4.ct_orig.dst = orig->dst.u3.ip;
|
|
|
|
__ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
|
|
|
|
return;
|
|
|
|
} else if (key->eth.type == htons(ETH_P_IPV6) &&
|
|
|
|
!sw_flow_key_is_nd(key) &&
|
|
|
|
nf_ct_l3num(ct) == NFPROTO_IPV6) {
|
|
|
|
key->ipv6.ct_orig.src = orig->src.u3.in6;
|
|
|
|
key->ipv6.ct_orig.dst = orig->dst.u3.in6;
|
|
|
|
__ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 19:22:01 +00:00
|
|
|
/* Clear 'ct_orig_proto' to mark the non-existence of conntrack
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
* original direction key fields.
|
|
|
|
*/
|
2017-02-09 19:22:01 +00:00
|
|
|
key->ct_orig_proto = 0;
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:52 +00:00
|
|
|
/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
|
2016-03-10 18:54:23 +00:00
|
|
|
* previously sent the packet to conntrack via the ct action. If
|
|
|
|
* 'keep_nat_flags' is true, the existing NAT flags retained, else they are
|
|
|
|
* initialized from the connection status.
|
2015-08-26 18:31:48 +00:00
|
|
|
*/
|
|
|
|
static void ovs_ct_update_key(const struct sk_buff *skb,
|
2015-12-09 22:07:40 +00:00
|
|
|
const struct ovs_conntrack_info *info,
|
2016-03-10 18:54:23 +00:00
|
|
|
struct sw_flow_key *key, bool post_ct,
|
|
|
|
bool keep_nat_flags)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
|
|
|
const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
|
|
|
u8 state = 0;
|
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
if (ct) {
|
|
|
|
state = ovs_ct_get_state(ctinfo);
|
2016-03-10 18:54:18 +00:00
|
|
|
/* All unconfirmed entries are NEW connections. */
|
2015-10-20 02:18:59 +00:00
|
|
|
if (!nf_ct_is_confirmed(ct))
|
|
|
|
state |= OVS_CS_F_NEW;
|
2016-03-10 18:54:18 +00:00
|
|
|
/* OVS persists the related flag for the duration of the
|
|
|
|
* connection.
|
|
|
|
*/
|
2015-08-26 18:31:48 +00:00
|
|
|
if (ct->master)
|
|
|
|
state |= OVS_CS_F_RELATED;
|
2016-03-10 18:54:23 +00:00
|
|
|
if (keep_nat_flags) {
|
2017-02-09 19:22:01 +00:00
|
|
|
state |= key->ct_state & OVS_CS_F_NAT_MASK;
|
2016-03-10 18:54:23 +00:00
|
|
|
} else {
|
|
|
|
if (ct->status & IPS_SRC_NAT)
|
|
|
|
state |= OVS_CS_F_SRC_NAT;
|
|
|
|
if (ct->status & IPS_DST_NAT)
|
|
|
|
state |= OVS_CS_F_DST_NAT;
|
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
zone = nf_ct_zone(ct);
|
|
|
|
} else if (post_ct) {
|
|
|
|
state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
|
2015-12-09 22:07:40 +00:00
|
|
|
if (info)
|
|
|
|
zone = &info->zone;
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
2015-08-26 18:31:49 +00:00
|
|
|
__ovs_ct_update_key(key, state, zone, ct);
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:54:18 +00:00
|
|
|
/* This is called to initialize CT key fields possibly coming in from the local
|
|
|
|
* stack.
|
|
|
|
*/
|
2021-03-16 08:33:54 +00:00
|
|
|
void ovs_ct_fill_key(const struct sk_buff *skb,
|
|
|
|
struct sw_flow_key *key,
|
|
|
|
bool post_ct)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
2021-03-16 08:33:54 +00:00
|
|
|
ovs_ct_update_key(skb, NULL, key, post_ct, false);
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
int ovs_ct_put_key(const struct sw_flow_key *swkey,
|
|
|
|
const struct sw_flow_key *output, struct sk_buff *skb)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
2017-02-09 19:22:01 +00:00
|
|
|
if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
|
2015-08-26 18:31:48 +00:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
|
2017-02-09 19:22:01 +00:00
|
|
|
nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
|
2015-08-26 18:31:48 +00:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2015-08-26 18:31:49 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
|
2015-08-26 18:31:49 +00:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2015-08-28 08:39:56 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
|
|
|
|
&output->ct.labels))
|
2015-08-26 18:31:52 +00:00
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2017-02-09 19:22:01 +00:00
|
|
|
if (swkey->ct_orig_proto) {
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
if (swkey->eth.type == htons(ETH_P_IP)) {
|
2020-07-31 04:48:38 +00:00
|
|
|
struct ovs_key_ct_tuple_ipv4 orig;
|
|
|
|
|
|
|
|
memset(&orig, 0, sizeof(orig));
|
|
|
|
orig.ipv4_src = output->ipv4.ct_orig.src;
|
|
|
|
orig.ipv4_dst = output->ipv4.ct_orig.dst;
|
|
|
|
orig.src_port = output->ct.orig_tp.src;
|
|
|
|
orig.dst_port = output->ct.orig_tp.dst;
|
|
|
|
orig.ipv4_proto = output->ct_orig_proto;
|
|
|
|
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
|
|
|
|
sizeof(orig), &orig))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
} else if (swkey->eth.type == htons(ETH_P_IPV6)) {
|
2020-07-31 04:48:38 +00:00
|
|
|
struct ovs_key_ct_tuple_ipv6 orig;
|
|
|
|
|
|
|
|
memset(&orig, 0, sizeof(orig));
|
|
|
|
memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32,
|
|
|
|
sizeof(orig.ipv6_src));
|
|
|
|
memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32,
|
|
|
|
sizeof(orig.ipv6_dst));
|
|
|
|
orig.src_port = output->ct.orig_tp.src;
|
|
|
|
orig.dst_port = output->ct.orig_tp.dst;
|
|
|
|
orig.ipv6_proto = output->ct_orig_proto;
|
|
|
|
|
openvswitch: Add original direction conntrack tuple to sw_flow_key.
Add the fields of the conntrack original direction 5-tuple to struct
sw_flow_key. The new fields are initially marked as non-existent, and
are populated whenever a conntrack action is executed and either finds
or generates a conntrack entry. This means that these fields exist
for all packets that were not rejected by conntrack as untrackable.
The original tuple fields in the sw_flow_key are filled from the
original direction tuple of the conntrack entry relating to the
current packet, or from the original direction tuple of the master
conntrack entry, if the current conntrack entry has a master.
Generally, expected connections of connections having an assigned
helper (e.g., FTP), have a master conntrack entry.
The main purpose of the new conntrack original tuple fields is to
allow matching on them for policy decision purposes, with the premise
that the admissibility of tracked connections reply packets (as well
as original direction packets), and both direction packets of any
related connections may be based on ACL rules applying to the master
connection's original direction 5-tuple. This also makes it easier to
make policy decisions when the actual packet headers might have been
transformed by NAT, as the original direction 5-tuple represents the
packet headers before any such transformation.
When using the original direction 5-tuple the admissibility of return
and/or related packets need not be based on the mere existence of a
conntrack entry, allowing separation of admission policy from the
established conntrack state. While existence of a conntrack entry is
required for admission of the return or related packets, policy
changes can render connections that were initially admitted to be
rejected or dropped afterwards. If the admission of the return and
related packets was based on mere conntrack state (e.g., connection
being in an established state), a policy change that would make the
connection rejected or dropped would need to find and delete all
conntrack entries affected by such a change. When using the original
direction 5-tuple matching the affected conntrack entries can be
allowed to time out instead, as the established state of the
connection would not need to be the basis for packet admission any
more.
It should be noted that the directionality of related connections may
be the same or different than that of the master connection, and
neither the original direction 5-tuple nor the conntrack state bits
carry this information. If needed, the directionality of the master
connection can be stored in master's conntrack mark or labels, which
are automatically inherited by the expected related connections.
The fact that neither ARP nor ND packets are trackable by conntrack
allows mutual exclusion between ARP/ND and the new conntrack original
tuple fields. Hence, the IP addresses are overlaid in union with ARP
and ND fields. This allows the sw_flow_key to not grow much due to
this patch, but it also means that we must be careful to never use the
new key fields with ARP or ND packets. ARP is easy to distinguish and
keep mutually exclusive based on the ethernet type, but ND being an
ICMPv6 protocol requires a bit more attention.
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
Acked-by: Joe Stringer <joe@ovn.org>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-02-09 19:21:59 +00:00
|
|
|
if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
|
|
|
|
sizeof(orig), &orig))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:31:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:57 +00:00
|
|
|
static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
|
2015-08-26 18:31:49 +00:00
|
|
|
u32 ct_mark, u32 mask)
|
|
|
|
{
|
2015-08-29 02:22:11 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
|
2015-08-26 18:31:49 +00:00
|
|
|
u32 new_mark;
|
|
|
|
|
2022-11-09 19:39:07 +00:00
|
|
|
new_mark = ct_mark | (READ_ONCE(ct->mark) & ~(mask));
|
|
|
|
if (READ_ONCE(ct->mark) != new_mark) {
|
|
|
|
WRITE_ONCE(ct->mark, new_mark);
|
2017-02-09 19:21:54 +00:00
|
|
|
if (nf_ct_is_confirmed(ct))
|
|
|
|
nf_conntrack_event_cache(IPCT_MARK, ct);
|
2015-08-26 18:31:49 +00:00
|
|
|
key->ct.mark = new_mark;
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
return 0;
|
2015-08-29 02:22:11 +00:00
|
|
|
#else
|
|
|
|
return -ENOTSUPP;
|
|
|
|
#endif
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:57 +00:00
|
|
|
static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
|
2015-08-26 18:31:52 +00:00
|
|
|
{
|
|
|
|
struct nf_conn_labels *cl;
|
|
|
|
|
|
|
|
cl = nf_ct_labels_find(ct);
|
|
|
|
if (!cl) {
|
|
|
|
nf_ct_labels_ext_add(ct);
|
|
|
|
cl = nf_ct_labels_find(ct);
|
|
|
|
}
|
2017-02-09 19:21:57 +00:00
|
|
|
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize labels for a new, yet to be committed conntrack entry. Note that
|
|
|
|
* since the new connection is not yet confirmed, and thus no-one else has
|
2017-02-18 02:11:58 +00:00
|
|
|
* access to it's labels, we simply write them over.
|
2017-02-09 19:21:57 +00:00
|
|
|
*/
|
|
|
|
static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
|
|
|
|
const struct ovs_key_ct_labels *labels,
|
|
|
|
const struct ovs_key_ct_labels *mask)
|
|
|
|
{
|
2017-02-09 19:21:58 +00:00
|
|
|
struct nf_conn_labels *cl, *master_cl;
|
|
|
|
bool have_mask = labels_nonzero(mask);
|
|
|
|
|
|
|
|
/* Inherit master's labels to the related connection? */
|
|
|
|
master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
|
|
|
|
|
|
|
|
if (!master_cl && !have_mask)
|
|
|
|
return 0; /* Nothing to do. */
|
2017-02-09 19:21:57 +00:00
|
|
|
|
|
|
|
cl = ovs_ct_get_conn_labels(ct);
|
2017-02-09 19:21:56 +00:00
|
|
|
if (!cl)
|
2015-08-26 18:31:52 +00:00
|
|
|
return -ENOSPC;
|
|
|
|
|
2017-02-09 19:21:58 +00:00
|
|
|
/* Inherit the master's labels, if any. */
|
|
|
|
if (master_cl)
|
|
|
|
*cl = *master_cl;
|
|
|
|
|
|
|
|
if (have_mask) {
|
|
|
|
u32 *dst = (u32 *)cl->bits;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
|
|
|
|
dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
|
|
|
|
(labels->ct_labels_32[i]
|
|
|
|
& mask->ct_labels_32[i]);
|
|
|
|
}
|
2017-02-09 19:21:54 +00:00
|
|
|
|
2017-02-18 02:11:58 +00:00
|
|
|
/* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
|
2017-04-21 23:48:05 +00:00
|
|
|
* IPCT_LABEL bit is set in the event cache.
|
2017-02-18 02:11:58 +00:00
|
|
|
*/
|
|
|
|
nf_conntrack_event_cache(IPCT_LABEL, ct);
|
|
|
|
|
2017-02-09 19:21:57 +00:00
|
|
|
memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
|
|
|
|
const struct ovs_key_ct_labels *labels,
|
|
|
|
const struct ovs_key_ct_labels *mask)
|
|
|
|
{
|
|
|
|
struct nf_conn_labels *cl;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
cl = ovs_ct_get_conn_labels(ct);
|
|
|
|
if (!cl)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
|
|
err = nf_connlabels_replace(ct, labels->ct_labels_32,
|
|
|
|
mask->ct_labels_32,
|
|
|
|
OVS_CT_LABELS_LEN_32);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
|
2015-08-26 18:31:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:52:08 +00:00
|
|
|
static int ovs_ct_handle_fragments(struct net *net, struct sw_flow_key *key,
|
|
|
|
u16 zone, int family, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
|
|
|
|
int err;
|
|
|
|
|
2023-02-07 22:52:10 +00:00
|
|
|
err = nf_ct_handle_fragments(net, skb, zone, family, &key->ip.proto, &ovs_cb.mru);
|
2023-02-07 22:52:08 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2019-08-27 14:58:09 +00:00
|
|
|
/* The key extracted from the fragment that completed this datagram
|
|
|
|
* likely didn't have an L4 header, so regenerate it.
|
|
|
|
*/
|
|
|
|
ovs_flow_key_update_l3l4(skb, key);
|
2015-08-26 18:31:48 +00:00
|
|
|
key->ip.frag = OVS_FRAG_TYPE_NONE;
|
|
|
|
*OVS_CB(skb) = ovs_cb;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:54:20 +00:00
|
|
|
/* This replicates logic from nf_conntrack_core.c that is not exported. */
|
|
|
|
static enum ip_conntrack_info
|
|
|
|
ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
|
|
|
|
{
|
|
|
|
const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
|
|
|
|
|
|
|
|
if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
|
|
|
|
return IP_CT_ESTABLISHED_REPLY;
|
|
|
|
/* Once we've had two way comms, always ESTABLISHED. */
|
|
|
|
if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
|
|
|
|
return IP_CT_ESTABLISHED;
|
|
|
|
if (test_bit(IPS_EXPECTED_BIT, &ct->status))
|
|
|
|
return IP_CT_RELATED;
|
|
|
|
return IP_CT_NEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find an existing connection which this packet belongs to without
|
|
|
|
* re-attributing statistics or modifying the connection state. This allows an
|
2017-02-09 19:21:52 +00:00
|
|
|
* skb->_nfct lost due to an upcall to be recovered during actions execution.
|
2016-03-10 18:54:20 +00:00
|
|
|
*
|
|
|
|
* Must be called with rcu_read_lock.
|
|
|
|
*
|
2017-02-09 19:21:52 +00:00
|
|
|
* On success, populates skb->_nfct and returns the connection. Returns NULL
|
|
|
|
* if there is no existing entry.
|
2016-03-10 18:54:20 +00:00
|
|
|
*/
|
|
|
|
static struct nf_conn *
|
|
|
|
ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
|
2017-02-09 19:21:53 +00:00
|
|
|
u8 l3num, struct sk_buff *skb, bool natted)
|
2016-03-10 18:54:20 +00:00
|
|
|
{
|
|
|
|
struct nf_conntrack_tuple tuple;
|
|
|
|
struct nf_conntrack_tuple_hash *h;
|
|
|
|
struct nf_conn *ct;
|
|
|
|
|
2018-06-25 15:55:32 +00:00
|
|
|
if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
|
|
|
|
net, &tuple)) {
|
2016-03-10 18:54:20 +00:00
|
|
|
pr_debug("ovs_ct_find_existing: Can't get tuple\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:53 +00:00
|
|
|
/* Must invert the tuple if skb has been transformed by NAT. */
|
|
|
|
if (natted) {
|
|
|
|
struct nf_conntrack_tuple inverse;
|
|
|
|
|
2019-01-15 21:03:42 +00:00
|
|
|
if (!nf_ct_invert_tuple(&inverse, &tuple)) {
|
2017-02-09 19:21:53 +00:00
|
|
|
pr_debug("ovs_ct_find_existing: Inversion failed!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tuple = inverse;
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:54:20 +00:00
|
|
|
/* look for tuple match */
|
|
|
|
h = nf_conntrack_find_get(net, zone, &tuple);
|
|
|
|
if (!h)
|
|
|
|
return NULL; /* Not found. */
|
|
|
|
|
|
|
|
ct = nf_ct_tuplehash_to_ctrack(h);
|
|
|
|
|
2017-02-09 19:21:53 +00:00
|
|
|
/* Inverted packet tuple matches the reverse direction conntrack tuple,
|
|
|
|
* select the other tuplehash to get the right 'ctinfo' bits for this
|
|
|
|
* packet.
|
|
|
|
*/
|
|
|
|
if (natted)
|
|
|
|
h = &ct->tuplehash[!h->tuple.dst.dir];
|
|
|
|
|
2017-01-23 17:21:57 +00:00
|
|
|
nf_ct_set(skb, ct, ovs_ct_get_info(h));
|
2016-03-10 18:54:20 +00:00
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:42:49 +00:00
|
|
|
static
|
|
|
|
struct nf_conn *ovs_ct_executed(struct net *net,
|
|
|
|
const struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb,
|
|
|
|
bool *ct_executed)
|
|
|
|
{
|
|
|
|
struct nf_conn *ct = NULL;
|
|
|
|
|
|
|
|
/* If no ct, check if we have evidence that an existing conntrack entry
|
|
|
|
* might be found for this skb. This happens when we lose a skb->_nfct
|
|
|
|
* due to an upcall, or if the direction is being forced. If the
|
|
|
|
* connection was not confirmed, it is not cached and needs to be run
|
|
|
|
* through conntrack again.
|
|
|
|
*/
|
|
|
|
*ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
|
|
|
|
!(key->ct_state & OVS_CS_F_INVALID) &&
|
|
|
|
(key->ct_zone == info->zone.id);
|
|
|
|
|
|
|
|
if (*ct_executed || (!key->ct_state && info->force)) {
|
|
|
|
ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
|
|
|
|
!!(key->ct_state &
|
|
|
|
OVS_CS_F_NAT_MASK));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:21:52 +00:00
|
|
|
/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
|
2016-03-10 18:54:20 +00:00
|
|
|
static bool skb_nfct_cached(struct net *net,
|
|
|
|
const struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
2017-07-14 19:42:49 +00:00
|
|
|
bool ct_executed = true;
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
if (!ct)
|
2017-07-14 19:42:49 +00:00
|
|
|
ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
|
|
|
|
|
|
|
|
if (ct)
|
|
|
|
nf_ct_get(skb, &ctinfo);
|
|
|
|
else
|
2015-08-26 18:31:48 +00:00
|
|
|
return false;
|
2017-07-14 19:42:49 +00:00
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
if (!net_eq(net, read_pnet(&ct->ct_net)))
|
|
|
|
return false;
|
|
|
|
if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
|
|
|
|
return false;
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
if (info->helper) {
|
|
|
|
struct nf_conn_help *help;
|
|
|
|
|
|
|
|
help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
|
|
|
|
if (help && rcu_access_pointer(help->helper) != info->helper)
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-22 20:17:50 +00:00
|
|
|
if (info->nf_ct_timeout) {
|
|
|
|
struct nf_conn_timeout *timeout_ext;
|
|
|
|
|
|
|
|
timeout_ext = nf_ct_timeout_find(ct);
|
|
|
|
if (!timeout_ext || info->nf_ct_timeout !=
|
|
|
|
rcu_dereference(timeout_ext->timeout))
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-09 19:22:00 +00:00
|
|
|
/* Force conntrack entry direction to the current packet? */
|
|
|
|
if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
|
|
|
|
/* Delete the conntrack entry if confirmed, else just release
|
|
|
|
* the reference.
|
|
|
|
*/
|
|
|
|
if (nf_ct_is_confirmed(ct))
|
|
|
|
nf_ct_delete(ct, 0, 0);
|
2017-03-28 18:25:26 +00:00
|
|
|
|
2022-01-07 04:03:26 +00:00
|
|
|
nf_ct_put(ct);
|
2017-02-09 19:22:00 +00:00
|
|
|
nf_ct_set(skb, NULL, 0);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
|
2017-07-14 19:42:49 +00:00
|
|
|
return ct_executed;
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2022-03-18 12:43:19 +00:00
|
|
|
static void ovs_nat_update_key(struct sw_flow_key *key,
|
|
|
|
const struct sk_buff *skb,
|
|
|
|
enum nf_nat_manip_type maniptype)
|
|
|
|
{
|
|
|
|
if (maniptype == NF_NAT_MANIP_SRC) {
|
|
|
|
__be16 src;
|
|
|
|
|
|
|
|
key->ct_state |= OVS_CS_F_SRC_NAT;
|
|
|
|
if (key->eth.type == htons(ETH_P_IP))
|
|
|
|
key->ipv4.addr.src = ip_hdr(skb)->saddr;
|
|
|
|
else if (key->eth.type == htons(ETH_P_IPV6))
|
|
|
|
memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
|
|
|
|
sizeof(key->ipv6.addr.src));
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key->ip.proto == IPPROTO_UDP)
|
|
|
|
src = udp_hdr(skb)->source;
|
|
|
|
else if (key->ip.proto == IPPROTO_TCP)
|
|
|
|
src = tcp_hdr(skb)->source;
|
|
|
|
else if (key->ip.proto == IPPROTO_SCTP)
|
|
|
|
src = sctp_hdr(skb)->source;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
key->tp.src = src;
|
|
|
|
} else {
|
|
|
|
__be16 dst;
|
|
|
|
|
|
|
|
key->ct_state |= OVS_CS_F_DST_NAT;
|
|
|
|
if (key->eth.type == htons(ETH_P_IP))
|
|
|
|
key->ipv4.addr.dst = ip_hdr(skb)->daddr;
|
|
|
|
else if (key->eth.type == htons(ETH_P_IPV6))
|
|
|
|
memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
|
|
|
|
sizeof(key->ipv6.addr.dst));
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (key->ip.proto == IPPROTO_UDP)
|
|
|
|
dst = udp_hdr(skb)->dest;
|
|
|
|
else if (key->ip.proto == IPPROTO_TCP)
|
|
|
|
dst = tcp_hdr(skb)->dest;
|
|
|
|
else if (key->ip.proto == IPPROTO_SCTP)
|
|
|
|
dst = sctp_hdr(skb)->dest;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
key->tp.dst = dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
|
|
|
|
static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb, struct nf_conn *ct,
|
|
|
|
enum ip_conntrack_info ctinfo)
|
|
|
|
{
|
2022-12-08 16:56:12 +00:00
|
|
|
int err, action = 0;
|
2016-03-10 18:54:23 +00:00
|
|
|
|
2022-12-08 16:56:09 +00:00
|
|
|
if (!(info->nat & OVS_CT_NAT))
|
|
|
|
return NF_ACCEPT;
|
2022-12-08 16:56:12 +00:00
|
|
|
if (info->nat & OVS_CT_SRC_NAT)
|
|
|
|
action |= BIT(NF_NAT_MANIP_SRC);
|
|
|
|
if (info->nat & OVS_CT_DST_NAT)
|
|
|
|
action |= BIT(NF_NAT_MANIP_DST);
|
2022-12-08 16:56:09 +00:00
|
|
|
|
2022-12-08 16:56:12 +00:00
|
|
|
err = nf_ct_nat(skb, ct, ctinfo, &action, &info->range, info->commit);
|
2016-03-10 18:54:23 +00:00
|
|
|
|
2022-12-08 16:56:12 +00:00
|
|
|
if (action & BIT(NF_NAT_MANIP_SRC))
|
|
|
|
ovs_nat_update_key(key, skb, NF_NAT_MANIP_SRC);
|
|
|
|
if (action & BIT(NF_NAT_MANIP_DST))
|
|
|
|
ovs_nat_update_key(key, skb, NF_NAT_MANIP_DST);
|
2019-12-03 21:34:13 +00:00
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
return err;
|
|
|
|
}
|
2019-03-27 08:22:26 +00:00
|
|
|
#else /* !CONFIG_NF_NAT */
|
2016-03-10 18:54:23 +00:00
|
|
|
static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb, struct nf_conn *ct,
|
|
|
|
enum ip_conntrack_info ctinfo)
|
|
|
|
{
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-10 18:54:18 +00:00
|
|
|
/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
|
2016-03-10 18:54:19 +00:00
|
|
|
* not done already. Update key with new CT state after passing the packet
|
|
|
|
* through conntrack.
|
2017-02-09 19:21:52 +00:00
|
|
|
* Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
|
2016-03-10 18:54:18 +00:00
|
|
|
* set to NULL and 0 will be returned.
|
|
|
|
*/
|
2015-10-20 02:18:59 +00:00
|
|
|
static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
|
2015-08-26 18:31:48 +00:00
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
/* If we are recirculating packets to match on conntrack fields and
|
|
|
|
* committing with a separate conntrack action, then we don't need to
|
|
|
|
* actually run the packet through conntrack twice unless it's for a
|
|
|
|
* different zone.
|
|
|
|
*/
|
2016-03-10 18:54:22 +00:00
|
|
|
bool cached = skb_nfct_cached(net, key, info, skb);
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
|
|
|
|
|
|
|
if (!cached) {
|
2018-09-12 13:19:07 +00:00
|
|
|
struct nf_hook_state state = {
|
|
|
|
.hook = NF_INET_PRE_ROUTING,
|
|
|
|
.pf = info->family,
|
|
|
|
.net = net,
|
|
|
|
};
|
2015-08-26 18:31:48 +00:00
|
|
|
struct nf_conn *tmpl = info->ct;
|
2016-03-10 18:54:21 +00:00
|
|
|
int err;
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
/* Associate skb with specified zone. */
|
|
|
|
if (tmpl) {
|
2022-01-07 04:03:26 +00:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
nf_ct_put(ct);
|
2015-08-26 18:31:48 +00:00
|
|
|
nf_conntrack_get(&tmpl->ct_general);
|
2017-01-23 17:21:57 +00:00
|
|
|
nf_ct_set(skb, tmpl, IP_CT_NEW);
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:19:07 +00:00
|
|
|
err = nf_conntrack_in(skb, &state);
|
2016-03-10 18:54:21 +00:00
|
|
|
if (err != NF_ACCEPT)
|
2015-08-26 18:31:48 +00:00
|
|
|
return -ENOENT;
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
/* Clear CT state NAT flags to mark that we have not yet done
|
|
|
|
* NAT after the nf_conntrack_in() call. We can actually clear
|
|
|
|
* the whole state, as it will be re-initialized below.
|
|
|
|
*/
|
2017-02-09 19:22:01 +00:00
|
|
|
key->ct_state = 0;
|
2016-03-10 18:54:23 +00:00
|
|
|
|
|
|
|
/* Update the key, but keep the NAT flags. */
|
|
|
|
ovs_ct_update_key(skb, info, key, true, true);
|
2016-03-10 18:54:22 +00:00
|
|
|
}
|
2016-03-10 18:54:19 +00:00
|
|
|
|
2016-03-10 18:54:22 +00:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2016-03-10 18:54:23 +00:00
|
|
|
if (ct) {
|
2019-10-04 16:26:44 +00:00
|
|
|
bool add_helper = false;
|
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
/* Packets starting a new connection must be NATted before the
|
|
|
|
* helper, so that the helper knows about the NAT. We enforce
|
|
|
|
* this by delaying both NAT and helper calls for unconfirmed
|
|
|
|
* connections until the committing CT action. For later
|
|
|
|
* packets NAT and Helper may be called in either order.
|
|
|
|
*
|
|
|
|
* NAT will be done only if the CT action has NAT, and only
|
|
|
|
* once per packet (per zone), as guarded by the NAT bits in
|
2017-02-09 19:22:01 +00:00
|
|
|
* the key->ct_state.
|
2016-03-10 18:54:23 +00:00
|
|
|
*/
|
2017-02-09 19:22:01 +00:00
|
|
|
if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
|
2016-03-10 18:54:23 +00:00
|
|
|
(nf_ct_is_confirmed(ct) || info->commit) &&
|
|
|
|
ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-05-11 17:29:26 +00:00
|
|
|
/* Userspace may decide to perform a ct lookup without a helper
|
2019-10-04 16:26:44 +00:00
|
|
|
* specified followed by a (recirculate and) commit with one,
|
|
|
|
* or attach a helper in a later commit. Therefore, for
|
|
|
|
* connections which we will commit, we may need to attach
|
|
|
|
* the helper here.
|
2016-05-11 17:29:26 +00:00
|
|
|
*/
|
openvswitch: add nf_ct_is_confirmed check before assigning the helper
A WARN_ON call trace would be triggered when 'ct(commit, alg=helper)'
applies on a confirmed connection:
WARNING: CPU: 0 PID: 1251 at net/netfilter/nf_conntrack_extend.c:98
RIP: 0010:nf_ct_ext_add+0x12d/0x150 [nf_conntrack]
Call Trace:
<TASK>
nf_ct_helper_ext_add+0x12/0x60 [nf_conntrack]
__nf_ct_try_assign_helper+0xc4/0x160 [nf_conntrack]
__ovs_ct_lookup+0x72e/0x780 [openvswitch]
ovs_ct_execute+0x1d8/0x920 [openvswitch]
do_execute_actions+0x4e6/0xb60 [openvswitch]
ovs_execute_actions+0x60/0x140 [openvswitch]
ovs_packet_cmd_execute+0x2ad/0x310 [openvswitch]
genl_family_rcv_msg_doit.isra.15+0x113/0x150
genl_rcv_msg+0xef/0x1f0
which can be reproduced with these OVS flows:
table=0, in_port=veth1,tcp,tcp_dst=2121,ct_state=-trk
actions=ct(commit, table=1)
table=1, in_port=veth1,tcp,tcp_dst=2121,ct_state=+trk+new
actions=ct(commit, alg=ftp),normal
The issue was introduced by commit 248d45f1e193 ("openvswitch: Allow
attaching helper in later commit") where it somehow removed the check
of nf_ct_is_confirmed before asigning the helper. This patch is to fix
it by bringing it back.
Fixes: 248d45f1e193 ("openvswitch: Allow attaching helper in later commit")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Xin Long <lucien.xin@gmail.com>
Acked-by: Aaron Conole <aconole@redhat.com>
Tested-by: Aaron Conole <aconole@redhat.com>
Link: https://lore.kernel.org/r/c5c9092a22a2194650222bffaf786902613deb16.1665085502.git.lucien.xin@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2022-10-06 19:45:02 +00:00
|
|
|
if (!nf_ct_is_confirmed(ct) && info->commit &&
|
|
|
|
info->helper && !nfct_help(ct)) {
|
2016-05-11 17:29:26 +00:00
|
|
|
int err = __nf_ct_try_assign_helper(ct, info->ct,
|
|
|
|
GFP_ATOMIC);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2019-10-04 16:26:44 +00:00
|
|
|
add_helper = true;
|
2019-03-25 18:58:31 +00:00
|
|
|
|
|
|
|
/* helper installed, add seqadj if NAT is required */
|
|
|
|
if (info->nat && !nfct_seqadj(ct)) {
|
|
|
|
if (!nfct_seqadj_ext_add(ct))
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-05-11 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:54:23 +00:00
|
|
|
/* Call the helper only if:
|
2019-10-04 16:26:44 +00:00
|
|
|
* - nf_conntrack_in() was executed above ("!cached") or a
|
|
|
|
* helper was just attached ("add_helper") for a confirmed
|
|
|
|
* connection, or
|
2016-03-10 18:54:23 +00:00
|
|
|
* - When committing an unconfirmed connection.
|
|
|
|
*/
|
2019-10-04 16:26:44 +00:00
|
|
|
if ((nf_ct_is_confirmed(ct) ? !cached || add_helper :
|
|
|
|
info->commit) &&
|
2022-11-06 20:34:14 +00:00
|
|
|
nf_ct_helper(skb, ct, ctinfo, info->family) != NF_ACCEPT) {
|
2016-03-10 18:54:23 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-11-16 13:01:26 +00:00
|
|
|
|
|
|
|
if (nf_ct_protonum(ct) == IPPROTO_TCP &&
|
|
|
|
nf_ct_is_confirmed(ct) && nf_conntrack_tcp_established(ct)) {
|
|
|
|
/* Be liberal for tcp packets so that out-of-window
|
|
|
|
* packets are not marked invalid.
|
|
|
|
*/
|
|
|
|
nf_ct_set_tcp_be_liberal(ct);
|
|
|
|
}
|
2022-01-03 11:44:51 +00:00
|
|
|
|
|
|
|
nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup connection and read fields into key. */
|
|
|
|
static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
2023-07-16 21:09:19 +00:00
|
|
|
struct nf_conn *ct;
|
|
|
|
int err;
|
2016-06-28 15:22:26 +00:00
|
|
|
|
2023-07-16 21:09:19 +00:00
|
|
|
err = __ovs_ct_lookup(net, key, info, skb);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2016-06-28 15:22:26 +00:00
|
|
|
|
2023-07-16 21:09:19 +00:00
|
|
|
ct = (struct nf_conn *)skb_nfct(skb);
|
|
|
|
if (ct)
|
|
|
|
nf_ct_deliver_cached_events(ct);
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-01 22:00:37 +00:00
|
|
|
static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
|
2015-08-26 18:31:52 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2017-02-09 19:21:55 +00:00
|
|
|
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
|
|
|
|
if (labels->ct_labels_32[i])
|
2015-08-26 18:31:52 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
static struct hlist_head *ct_limit_hash_bucket(
|
|
|
|
const struct ovs_ct_limit_info *info, u16 zone)
|
|
|
|
{
|
|
|
|
return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with ovs_mutex */
|
|
|
|
static void ct_limit_set(const struct ovs_ct_limit_info *info,
|
|
|
|
struct ovs_ct_limit *new_ct_limit)
|
|
|
|
{
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
struct hlist_head *head;
|
|
|
|
|
|
|
|
head = ct_limit_hash_bucket(info, new_ct_limit->zone);
|
|
|
|
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
|
|
|
|
if (ct_limit->zone == new_ct_limit->zone) {
|
|
|
|
hlist_replace_rcu(&ct_limit->hlist_node,
|
|
|
|
&new_ct_limit->hlist_node);
|
|
|
|
kfree_rcu(ct_limit, rcu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with ovs_mutex */
|
|
|
|
static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
|
|
|
|
{
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct hlist_node *n;
|
|
|
|
|
|
|
|
head = ct_limit_hash_bucket(info, zone);
|
|
|
|
hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
|
|
|
|
if (ct_limit->zone == zone) {
|
|
|
|
hlist_del_rcu(&ct_limit->hlist_node);
|
|
|
|
kfree_rcu(ct_limit, rcu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call with RCU read lock */
|
|
|
|
static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
|
|
|
|
{
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
struct hlist_head *head;
|
|
|
|
|
|
|
|
head = ct_limit_hash_bucket(info, zone);
|
|
|
|
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
|
|
|
|
if (ct_limit->zone == zone)
|
|
|
|
return ct_limit->limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info->default_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_check_limit(struct net *net,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
const struct nf_conntrack_tuple *tuple)
|
|
|
|
{
|
|
|
|
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
|
|
|
|
u32 per_zone_limit, connections;
|
|
|
|
u32 conncount_key;
|
|
|
|
|
|
|
|
conncount_key = info->zone.id;
|
|
|
|
|
|
|
|
per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
|
|
|
|
if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
connections = nf_conncount_count(net, ct_limit_info->data,
|
|
|
|
&conncount_key, tuple, &info->zone);
|
|
|
|
if (connections > per_zone_limit)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-21 21:59:38 +00:00
|
|
|
/* Lookup connection and confirm if unconfirmed. */
|
|
|
|
static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
2017-02-09 19:21:57 +00:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
2016-06-21 21:59:38 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = __ovs_ct_lookup(net, key, info, skb);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2017-02-09 19:21:57 +00:00
|
|
|
/* The connection could be invalid, in which case this is a no-op.*/
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
if (!ct)
|
|
|
|
return 0;
|
|
|
|
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
|
|
|
|
if (!nf_ct_is_confirmed(ct)) {
|
|
|
|
err = ovs_ct_check_limit(net, info,
|
|
|
|
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
|
|
|
|
if (err) {
|
|
|
|
net_warn_ratelimited("openvswitch: zone: %u "
|
2018-11-27 14:37:17 +00:00
|
|
|
"exceeds conntrack limit\n",
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
info->zone.id);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-21 23:48:06 +00:00
|
|
|
/* Set the conntrack event mask if given. NEW and DELETE events have
|
|
|
|
* their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
|
|
|
|
* typically would receive many kinds of updates. Setting the event
|
|
|
|
* mask allows those events to be filtered. The set event mask will
|
|
|
|
* remain in effect for the lifetime of the connection unless changed
|
|
|
|
* by a further CT action with both the commit flag and the eventmask
|
|
|
|
* option. */
|
|
|
|
if (info->have_eventmask) {
|
|
|
|
struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
|
|
|
|
|
|
|
|
if (cache)
|
|
|
|
cache->ctmask = info->eventmask;
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:59:38 +00:00
|
|
|
/* Apply changes before confirming the connection so that the initial
|
|
|
|
* conntrack NEW netlink event carries the values given in the CT
|
|
|
|
* action.
|
|
|
|
*/
|
|
|
|
if (info->mark.mask) {
|
2017-02-09 19:21:57 +00:00
|
|
|
err = ovs_ct_set_mark(ct, key, info->mark.value,
|
2016-06-21 21:59:38 +00:00
|
|
|
info->mark.mask);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2017-02-09 19:21:58 +00:00
|
|
|
if (!nf_ct_is_confirmed(ct)) {
|
|
|
|
err = ovs_ct_init_labels(ct, key, &info->labels.value,
|
|
|
|
&info->labels.mask);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2022-01-03 11:44:51 +00:00
|
|
|
|
|
|
|
nf_conn_act_ct_ext_add(ct);
|
2018-11-02 15:36:55 +00:00
|
|
|
} else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
|
|
|
|
labels_nonzero(&info->labels.mask)) {
|
2017-02-09 19:21:58 +00:00
|
|
|
err = ovs_ct_set_labels(ct, key, &info->labels.value,
|
|
|
|
&info->labels.mask);
|
2016-06-21 21:59:38 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
/* This will take care of sending queued events even if the connection
|
|
|
|
* is already confirmed.
|
|
|
|
*/
|
|
|
|
if (nf_conntrack_confirm(skb) != NF_ACCEPT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:21:48 +00:00
|
|
|
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
|
|
|
|
* value if 'skb' is freed.
|
|
|
|
*/
|
2015-08-26 18:31:48 +00:00
|
|
|
int ovs_ct_execute(struct net *net, struct sk_buff *skb,
|
|
|
|
struct sw_flow_key *key,
|
|
|
|
const struct ovs_conntrack_info *info)
|
|
|
|
{
|
|
|
|
int nh_ofs;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* The conntrack module expects to be working at L3. */
|
|
|
|
nh_ofs = skb_network_offset(skb);
|
2017-01-13 00:33:18 +00:00
|
|
|
skb_pull_rcsum(skb, nh_ofs);
|
2015-08-26 18:31:48 +00:00
|
|
|
|
2023-02-07 22:52:07 +00:00
|
|
|
err = nf_ct_skb_network_trim(skb, info->family);
|
|
|
|
if (err) {
|
|
|
|
kfree_skb(skb);
|
openvswitch: Remove padding from packet before L3+ conntrack processing
IPv4 and IPv6 packets may arrive with lower-layer padding that is not
included in the L3 length. For example, a short IPv4 packet may have
up to 6 bytes of padding following the IP payload when received on an
Ethernet device with a minimum packet length of 64 bytes.
Higher-layer processing functions in netfilter (e.g. nf_ip_checksum(),
and help() in nf_conntrack_ftp) assume skb->len reflects the length of
the L3 header and payload, rather than referring back to
ip_hdr->tot_len or ipv6_hdr->payload_len, and get confused by
lower-layer padding.
In the normal IPv4 receive path, ip_rcv() trims the packet to
ip_hdr->tot_len before invoking netfilter hooks. In the IPv6 receive
path, ip6_rcv() does the same using ipv6_hdr->payload_len. Similarly
in the br_netfilter receive path, br_validate_ipv4() and
br_validate_ipv6() trim the packet to the L3 length before invoking
netfilter hooks.
Currently in the OVS conntrack receive path, ovs_ct_execute() pulls
the skb to the L3 header but does not trim it to the L3 length before
calling nf_conntrack_in(NF_INET_PRE_ROUTING). When
nf_conntrack_proto_tcp encounters a packet with lower-layer padding,
nf_ip_checksum() fails causing a "nf_ct_tcp: bad TCP checksum" log
message. While extra zero bytes don't affect the checksum, the length
in the IP pseudoheader does. That length is based on skb->len, and
without trimming, it doesn't match the length the sender used when
computing the checksum.
In ovs_ct_execute(), trim the skb to the L3 length before higher-layer
processing.
Signed-off-by: Ed Swierk <eswierk@skyportsystems.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-02-01 02:48:02 +00:00
|
|
|
return err;
|
2023-02-07 22:52:07 +00:00
|
|
|
}
|
openvswitch: Remove padding from packet before L3+ conntrack processing
IPv4 and IPv6 packets may arrive with lower-layer padding that is not
included in the L3 length. For example, a short IPv4 packet may have
up to 6 bytes of padding following the IP payload when received on an
Ethernet device with a minimum packet length of 64 bytes.
Higher-layer processing functions in netfilter (e.g. nf_ip_checksum(),
and help() in nf_conntrack_ftp) assume skb->len reflects the length of
the L3 header and payload, rather than referring back to
ip_hdr->tot_len or ipv6_hdr->payload_len, and get confused by
lower-layer padding.
In the normal IPv4 receive path, ip_rcv() trims the packet to
ip_hdr->tot_len before invoking netfilter hooks. In the IPv6 receive
path, ip6_rcv() does the same using ipv6_hdr->payload_len. Similarly
in the br_netfilter receive path, br_validate_ipv4() and
br_validate_ipv6() trim the packet to the L3 length before invoking
netfilter hooks.
Currently in the OVS conntrack receive path, ovs_ct_execute() pulls
the skb to the L3 header but does not trim it to the L3 length before
calling nf_conntrack_in(NF_INET_PRE_ROUTING). When
nf_conntrack_proto_tcp encounters a packet with lower-layer padding,
nf_ip_checksum() fails causing a "nf_ct_tcp: bad TCP checksum" log
message. While extra zero bytes don't affect the checksum, the length
in the IP pseudoheader does. That length is based on skb->len, and
without trimming, it doesn't match the length the sender used when
computing the checksum.
In ovs_ct_execute(), trim the skb to the L3 length before higher-layer
processing.
Signed-off-by: Ed Swierk <eswierk@skyportsystems.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-02-01 02:48:02 +00:00
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
|
2023-02-07 22:52:08 +00:00
|
|
|
err = ovs_ct_handle_fragments(net, key, info->zone.id,
|
|
|
|
info->family, skb);
|
2015-08-26 18:31:48 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:00:01 +00:00
|
|
|
if (info->commit)
|
2016-06-21 21:59:38 +00:00
|
|
|
err = ovs_ct_commit(net, key, info, skb);
|
2015-08-26 18:31:48 +00:00
|
|
|
else
|
|
|
|
err = ovs_ct_lookup(net, key, info, skb);
|
|
|
|
|
2021-04-04 07:11:03 +00:00
|
|
|
skb_push_rcsum(skb, nh_ofs);
|
2015-10-26 03:21:48 +00:00
|
|
|
if (err)
|
2023-08-11 14:12:52 +00:00
|
|
|
ovs_kfree_skb_reason(skb, OVS_DROP_CONNTRACK);
|
2015-08-26 18:31:48 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:54:44 +00:00
|
|
|
int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
|
|
|
|
{
|
2022-01-07 04:03:26 +00:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
|
|
|
|
nf_ct_put(ct);
|
2021-05-08 02:27:07 +00:00
|
|
|
nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
|
net: openvswitch: fix misuse of the cached connection on tuple changes
If packet headers changed, the cached nfct is no longer relevant
for the packet and attempt to re-use it leads to the incorrect packet
classification.
This issue is causing broken connectivity in OpenStack deployments
with OVS/OVN due to hairpin traffic being unexpectedly dropped.
The setup has datapath flows with several conntrack actions and tuple
changes between them:
actions:ct(commit,zone=8,mark=0/0x1,nat(src)),
set(eth(src=00:00:00:00:00:01,dst=00:00:00:00:00:06)),
set(ipv4(src=172.18.2.10,dst=192.168.100.6,ttl=62)),
ct(zone=8),recirc(0x4)
After the first ct() action the packet headers are almost fully
re-written. The next ct() tries to re-use the existing nfct entry
and marks the packet as invalid, so it gets dropped later in the
pipeline.
Clearing the cached conntrack entry whenever packet tuple is changed
to avoid the issue.
The flow key should not be cleared though, because we should still
be able to match on the ct_state if the recirculation happens after
the tuple change but before the next ct() action.
Cc: stable@vger.kernel.org
Fixes: 7f8a436eaa2c ("openvswitch: Add conntrack action")
Reported-by: Frode Nordahl <frode.nordahl@canonical.com>
Link: https://mail.openvswitch.org/pipermail/ovs-discuss/2022-May/051829.html
Link: https://bugs.launchpad.net/ubuntu/+source/ovn/+bug/1967856
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Link: https://lore.kernel.org/r/20220606221140.488984-1-i.maximets@ovn.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2022-06-06 22:11:40 +00:00
|
|
|
|
|
|
|
if (key)
|
|
|
|
ovs_ct_fill_key(skb, key, false);
|
2017-10-10 20:54:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2016-03-10 18:54:23 +00:00
|
|
|
static int parse_nat(const struct nlattr *attr,
|
|
|
|
struct ovs_conntrack_info *info, bool log)
|
|
|
|
{
|
|
|
|
struct nlattr *a;
|
|
|
|
int rem;
|
|
|
|
bool have_ip_max = false;
|
|
|
|
bool have_proto_max = false;
|
|
|
|
bool ip_vers = (info->family == NFPROTO_IPV6);
|
|
|
|
|
|
|
|
nla_for_each_nested(a, attr, rem) {
|
|
|
|
static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
|
|
|
|
[OVS_NAT_ATTR_SRC] = {0, 0},
|
|
|
|
[OVS_NAT_ATTR_DST] = {0, 0},
|
|
|
|
[OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
|
|
|
|
sizeof(struct in6_addr)},
|
|
|
|
[OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
|
|
|
|
sizeof(struct in6_addr)},
|
|
|
|
[OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
|
|
|
|
[OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
|
|
|
|
[OVS_NAT_ATTR_PERSISTENT] = {0, 0},
|
|
|
|
[OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
|
|
|
|
[OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
|
|
|
|
};
|
|
|
|
int type = nla_type(a);
|
|
|
|
|
|
|
|
if (type > OVS_NAT_ATTR_MAX) {
|
2017-08-11 11:26:26 +00:00
|
|
|
OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
|
2016-03-10 18:54:23 +00:00
|
|
|
type, OVS_NAT_ATTR_MAX);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
|
2017-08-11 11:26:26 +00:00
|
|
|
OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
|
2016-03-10 18:54:23 +00:00
|
|
|
type, nla_len(a),
|
|
|
|
ovs_nat_attr_lens[type][ip_vers]);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case OVS_NAT_ATTR_SRC:
|
|
|
|
case OVS_NAT_ATTR_DST:
|
|
|
|
if (info->nat) {
|
2017-08-11 11:26:26 +00:00
|
|
|
OVS_NLERR(log, "Only one type of NAT may be specified");
|
2016-03-10 18:54:23 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
info->nat |= OVS_CT_NAT;
|
|
|
|
info->nat |= ((type == OVS_NAT_ATTR_SRC)
|
|
|
|
? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_IP_MIN:
|
2016-03-28 10:08:59 +00:00
|
|
|
nla_memcpy(&info->range.min_addr, a,
|
|
|
|
sizeof(info->range.min_addr));
|
2016-03-10 18:54:23 +00:00
|
|
|
info->range.flags |= NF_NAT_RANGE_MAP_IPS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_IP_MAX:
|
|
|
|
have_ip_max = true;
|
|
|
|
nla_memcpy(&info->range.max_addr, a,
|
|
|
|
sizeof(info->range.max_addr));
|
|
|
|
info->range.flags |= NF_NAT_RANGE_MAP_IPS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_PROTO_MIN:
|
|
|
|
info->range.min_proto.all = htons(nla_get_u16(a));
|
|
|
|
info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_PROTO_MAX:
|
|
|
|
have_proto_max = true;
|
|
|
|
info->range.max_proto.all = htons(nla_get_u16(a));
|
|
|
|
info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_PERSISTENT:
|
|
|
|
info->range.flags |= NF_NAT_RANGE_PERSISTENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_PROTO_HASH:
|
|
|
|
info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OVS_NAT_ATTR_PROTO_RANDOM:
|
|
|
|
info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-11 11:26:26 +00:00
|
|
|
OVS_NLERR(log, "Unknown nat attribute (%d)", type);
|
2016-03-10 18:54:23 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rem > 0) {
|
2017-08-11 11:26:26 +00:00
|
|
|
OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
|
2016-03-10 18:54:23 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!info->nat) {
|
|
|
|
/* Do not allow flags if no type is given. */
|
|
|
|
if (info->range.flags) {
|
|
|
|
OVS_NLERR(log,
|
2017-12-27 14:51:38 +00:00
|
|
|
"NAT flags may be given only when NAT range (SRC or DST) is also specified."
|
2016-03-10 18:54:23 +00:00
|
|
|
);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
info->nat = OVS_CT_NAT; /* NAT existing connections. */
|
|
|
|
} else if (!info->commit) {
|
|
|
|
OVS_NLERR(log,
|
2017-12-27 14:51:38 +00:00
|
|
|
"NAT attributes may be specified only when CT COMMIT flag is also specified."
|
2016-03-10 18:54:23 +00:00
|
|
|
);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
/* Allow missing IP_MAX. */
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
|
|
|
|
memcpy(&info->range.max_addr, &info->range.min_addr,
|
|
|
|
sizeof(info->range.max_addr));
|
|
|
|
}
|
|
|
|
/* Allow missing PROTO_MAX. */
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
|
|
|
|
!have_proto_max) {
|
|
|
|
info->range.max_proto.all = info->range.min_proto.all;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
|
2015-10-06 18:00:01 +00:00
|
|
|
[OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
|
2017-02-09 19:22:00 +00:00
|
|
|
[OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
|
2015-08-26 18:31:48 +00:00
|
|
|
[OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
|
|
|
|
.maxlen = sizeof(u16) },
|
2015-08-26 18:31:49 +00:00
|
|
|
[OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
|
|
|
|
.maxlen = sizeof(struct md_mark) },
|
2015-10-01 22:00:37 +00:00
|
|
|
[OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
|
|
|
|
.maxlen = sizeof(struct md_labels) },
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
[OVS_CT_ATTR_HELPER] = { .minlen = 1,
|
2016-03-10 18:54:23 +00:00
|
|
|
.maxlen = NF_CT_HELPER_NAME_LEN },
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2016-03-10 18:54:23 +00:00
|
|
|
/* NAT length is checked when parsing the nested attributes. */
|
|
|
|
[OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
|
|
|
|
#endif
|
2017-04-21 23:48:06 +00:00
|
|
|
[OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
|
|
|
|
.maxlen = sizeof(u32) },
|
2019-03-26 18:31:14 +00:00
|
|
|
[OVS_CT_ATTR_TIMEOUT] = { .minlen = 1,
|
|
|
|
.maxlen = CTNL_TIMEOUT_NAME_MAX },
|
2015-08-26 18:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
const char **helper, bool log)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
|
|
|
struct nlattr *a;
|
|
|
|
int rem;
|
|
|
|
|
|
|
|
nla_for_each_nested(a, attr, rem) {
|
|
|
|
int type = nla_type(a);
|
2017-07-23 09:52:23 +00:00
|
|
|
int maxlen;
|
|
|
|
int minlen;
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
if (type > OVS_CT_ATTR_MAX) {
|
|
|
|
OVS_NLERR(log,
|
|
|
|
"Unknown conntrack attr (type=%d, max=%d)",
|
|
|
|
type, OVS_CT_ATTR_MAX);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-07-23 09:52:23 +00:00
|
|
|
|
|
|
|
maxlen = ovs_ct_attr_lens[type].maxlen;
|
|
|
|
minlen = ovs_ct_attr_lens[type].minlen;
|
2015-08-26 18:31:48 +00:00
|
|
|
if (nla_len(a) < minlen || nla_len(a) > maxlen) {
|
|
|
|
OVS_NLERR(log,
|
|
|
|
"Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
|
|
|
|
type, nla_len(a), maxlen);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
2017-02-09 19:22:00 +00:00
|
|
|
case OVS_CT_ATTR_FORCE_COMMIT:
|
|
|
|
info->force = true;
|
2020-08-23 22:36:59 +00:00
|
|
|
fallthrough;
|
2015-10-06 18:00:01 +00:00
|
|
|
case OVS_CT_ATTR_COMMIT:
|
|
|
|
info->commit = true;
|
2015-08-26 18:31:48 +00:00
|
|
|
break;
|
|
|
|
#ifdef CONFIG_NF_CONNTRACK_ZONES
|
|
|
|
case OVS_CT_ATTR_ZONE:
|
|
|
|
info->zone.id = nla_get_u16(a);
|
|
|
|
break;
|
2015-08-26 18:31:49 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NF_CONNTRACK_MARK
|
|
|
|
case OVS_CT_ATTR_MARK: {
|
|
|
|
struct md_mark *mark = nla_data(a);
|
|
|
|
|
2015-10-20 02:19:00 +00:00
|
|
|
if (!mark->mask) {
|
|
|
|
OVS_NLERR(log, "ct_mark mask cannot be 0");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-08-26 18:31:49 +00:00
|
|
|
info->mark = *mark;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-26 18:31:52 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NF_CONNTRACK_LABELS
|
2015-10-01 22:00:37 +00:00
|
|
|
case OVS_CT_ATTR_LABELS: {
|
|
|
|
struct md_labels *labels = nla_data(a);
|
2015-08-26 18:31:52 +00:00
|
|
|
|
2015-10-20 02:19:00 +00:00
|
|
|
if (!labels_nonzero(&labels->mask)) {
|
|
|
|
OVS_NLERR(log, "ct_labels mask cannot be 0");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-10-01 22:00:37 +00:00
|
|
|
info->labels = *labels;
|
2015-08-26 18:31:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
#endif
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
case OVS_CT_ATTR_HELPER:
|
|
|
|
*helper = nla_data(a);
|
2023-02-08 13:31:53 +00:00
|
|
|
if (!string_is_terminated(*helper, nla_len(a))) {
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
OVS_NLERR(log, "Invalid conntrack helper");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2016-03-10 18:54:23 +00:00
|
|
|
case OVS_CT_ATTR_NAT: {
|
|
|
|
int err = parse_nat(a, info, log);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-21 23:48:06 +00:00
|
|
|
case OVS_CT_ATTR_EVENTMASK:
|
|
|
|
info->have_eventmask = true;
|
|
|
|
info->eventmask = nla_get_u32(a);
|
|
|
|
break;
|
2019-03-26 18:31:14 +00:00
|
|
|
#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
|
|
|
|
case OVS_CT_ATTR_TIMEOUT:
|
|
|
|
memcpy(info->timeout, nla_data(a), nla_len(a));
|
2023-02-08 13:31:53 +00:00
|
|
|
if (!string_is_terminated(info->timeout, nla_len(a))) {
|
2019-08-22 00:16:10 +00:00
|
|
|
OVS_NLERR(log, "Invalid conntrack timeout");
|
2019-03-26 18:31:14 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2017-04-21 23:48:06 +00:00
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
default:
|
|
|
|
OVS_NLERR(log, "Unknown conntrack attr (%d)",
|
|
|
|
type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:59:38 +00:00
|
|
|
#ifdef CONFIG_NF_CONNTRACK_MARK
|
|
|
|
if (!info->commit && info->mark.mask) {
|
|
|
|
OVS_NLERR(log,
|
|
|
|
"Setting conntrack mark requires 'commit' flag.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NF_CONNTRACK_LABELS
|
|
|
|
if (!info->commit && labels_nonzero(&info->labels.mask)) {
|
|
|
|
OVS_NLERR(log,
|
|
|
|
"Setting conntrack labels requires 'commit' flag.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-26 18:31:48 +00:00
|
|
|
if (rem > 0) {
|
|
|
|
OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:31:52 +00:00
|
|
|
bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
|
2015-08-26 18:31:48 +00:00
|
|
|
{
|
|
|
|
if (attr == OVS_KEY_ATTR_CT_STATE)
|
|
|
|
return true;
|
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
|
|
|
|
attr == OVS_KEY_ATTR_CT_ZONE)
|
|
|
|
return true;
|
2015-08-26 18:31:49 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
|
|
|
|
attr == OVS_KEY_ATTR_CT_MARK)
|
|
|
|
return true;
|
2015-08-26 18:31:52 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
|
2015-10-01 22:00:37 +00:00
|
|
|
attr == OVS_KEY_ATTR_CT_LABELS) {
|
2015-08-26 18:31:52 +00:00
|
|
|
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
|
|
|
|
return ovs_net->xt_label;
|
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
|
|
|
|
const struct sw_flow_key *key,
|
|
|
|
struct sw_flow_actions **sfa, bool log)
|
|
|
|
{
|
|
|
|
struct ovs_conntrack_info ct_info;
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
const char *helper = NULL;
|
2015-08-26 18:31:48 +00:00
|
|
|
u16 family;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
family = key_to_nfproto(key);
|
|
|
|
if (family == NFPROTO_UNSPEC) {
|
|
|
|
OVS_NLERR(log, "ct family unspecified");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ct_info, 0, sizeof(ct_info));
|
|
|
|
ct_info.family = family;
|
|
|
|
|
|
|
|
nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
|
|
|
|
NF_CT_DEFAULT_ZONE_DIR, 0);
|
|
|
|
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
err = parse_ct(attr, &ct_info, &helper, log);
|
2015-08-26 18:31:48 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Set up template for tracking connections in specific zones. */
|
|
|
|
ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
|
|
|
|
if (!ct_info.ct) {
|
|
|
|
OVS_NLERR(log, "Failed to allocate conntrack template");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2019-03-26 18:31:14 +00:00
|
|
|
|
|
|
|
if (ct_info.timeout[0]) {
|
|
|
|
if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto,
|
|
|
|
ct_info.timeout))
|
|
|
|
pr_info_ratelimited("Failed to associated timeout "
|
|
|
|
"policy `%s'\n", ct_info.timeout);
|
2019-08-22 20:17:50 +00:00
|
|
|
else
|
|
|
|
ct_info.nf_ct_timeout = rcu_dereference(
|
|
|
|
nf_ct_timeout_find(ct_info.ct)->timeout);
|
|
|
|
|
2019-03-26 18:31:14 +00:00
|
|
|
}
|
|
|
|
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
if (helper) {
|
2022-11-06 20:34:15 +00:00
|
|
|
err = nf_ct_add_helper(ct_info.ct, helper, ct_info.family,
|
|
|
|
key->ip.proto, ct_info.nat, &ct_info.helper);
|
|
|
|
if (err) {
|
|
|
|
OVS_NLERR(log, "Failed to add %s helper %d", helper, err);
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
goto err_free_ct;
|
2022-11-06 20:34:15 +00:00
|
|
|
}
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
|
|
|
|
err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
|
|
|
|
sizeof(ct_info), log);
|
|
|
|
if (err)
|
|
|
|
goto err_free_ct;
|
|
|
|
|
2023-07-16 21:09:19 +00:00
|
|
|
if (ct_info.commit)
|
|
|
|
__set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
|
2015-08-26 18:31:48 +00:00
|
|
|
return 0;
|
|
|
|
err_free_ct:
|
2015-12-09 22:07:39 +00:00
|
|
|
__ovs_ct_free_action(&ct_info);
|
2015-08-26 18:31:48 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2016-03-10 18:54:23 +00:00
|
|
|
static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nlattr *start;
|
|
|
|
|
2019-04-26 09:13:06 +00:00
|
|
|
start = nla_nest_start_noflag(skb, OVS_CT_ATTR_NAT);
|
2016-03-10 18:54:23 +00:00
|
|
|
if (!start)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->nat & OVS_CT_SRC_NAT) {
|
|
|
|
if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
|
|
|
|
return false;
|
|
|
|
} else if (info->nat & OVS_CT_DST_NAT) {
|
|
|
|
if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
|
2019-02-19 16:38:21 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_NAT) &&
|
2016-03-18 13:33:45 +00:00
|
|
|
info->family == NFPROTO_IPV4) {
|
2016-03-10 18:54:23 +00:00
|
|
|
if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
|
|
|
|
info->range.min_addr.ip) ||
|
|
|
|
(info->range.max_addr.ip
|
|
|
|
!= info->range.min_addr.ip &&
|
|
|
|
(nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
|
|
|
|
info->range.max_addr.ip))))
|
|
|
|
return false;
|
2019-02-19 16:38:21 +00:00
|
|
|
} else if (IS_ENABLED(CONFIG_IPV6) &&
|
2016-03-18 13:33:45 +00:00
|
|
|
info->family == NFPROTO_IPV6) {
|
2016-03-10 18:54:23 +00:00
|
|
|
if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
|
|
|
|
&info->range.min_addr.in6) ||
|
|
|
|
(memcmp(&info->range.max_addr.in6,
|
|
|
|
&info->range.min_addr.in6,
|
|
|
|
sizeof(info->range.max_addr.in6)) &&
|
|
|
|
(nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
|
|
|
|
&info->range.max_addr.in6))))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
|
|
|
|
(nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
|
|
|
|
ntohs(info->range.min_proto.all)) ||
|
|
|
|
(info->range.max_proto.all != info->range.min_proto.all &&
|
|
|
|
nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
|
|
|
|
ntohs(info->range.max_proto.all)))))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
|
|
|
|
nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
|
|
|
|
return false;
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
|
|
|
|
nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
|
|
|
|
return false;
|
|
|
|
if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
|
|
|
|
nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
|
|
|
|
return false;
|
|
|
|
out:
|
|
|
|
nla_nest_end(skb, start);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-26 18:31:48 +00:00
|
|
|
int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nlattr *start;
|
|
|
|
|
2019-04-26 09:13:06 +00:00
|
|
|
start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CT);
|
2015-08-26 18:31:48 +00:00
|
|
|
if (!start)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2017-02-09 19:22:00 +00:00
|
|
|
if (ct_info->commit && nla_put_flag(skb, ct_info->force
|
|
|
|
? OVS_CT_ATTR_FORCE_COMMIT
|
|
|
|
: OVS_CT_ATTR_COMMIT))
|
2015-08-26 18:31:48 +00:00
|
|
|
return -EMSGSIZE;
|
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
|
|
|
|
nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
|
|
|
|
return -EMSGSIZE;
|
2015-10-20 02:19:00 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
|
2015-08-26 18:31:49 +00:00
|
|
|
nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
|
|
|
|
&ct_info->mark))
|
|
|
|
return -EMSGSIZE;
|
2015-08-26 18:31:52 +00:00
|
|
|
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
|
2015-10-20 02:19:00 +00:00
|
|
|
labels_nonzero(&ct_info->labels.mask) &&
|
2015-10-01 22:00:37 +00:00
|
|
|
nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
|
|
|
|
&ct_info->labels))
|
2015-08-26 18:31:52 +00:00
|
|
|
return -EMSGSIZE;
|
openvswitch: Allow attaching helpers to ct action
Add support for using conntrack helpers to assist protocol detection.
The new OVS_CT_ATTR_HELPER attribute of the CT action specifies a helper
to be used for this connection. If no helper is specified, then helpers
will be automatically applied as per the sysctl configuration of
net.netfilter.nf_conntrack_helper.
The helper may be specified as part of the conntrack action, eg:
ct(helper=ftp). Initial packets for related connections should be
committed to allow later packets for the flow to be considered
established.
Example ovs-ofctl flows allowing FTP connections from ports 1->2:
in_port=1,tcp,action=ct(helper=ftp,commit),2
in_port=2,tcp,ct_state=-trk,action=ct(recirc)
in_port=2,tcp,ct_state=+trk-new+est,action=1
in_port=2,tcp,ct_state=+trk+rel,action=1
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Acked-by: Thomas Graf <tgraf@suug.ch>
Acked-by: Pravin B Shelar <pshelar@nicira.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2015-08-26 18:31:53 +00:00
|
|
|
if (ct_info->helper) {
|
|
|
|
if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
|
|
|
|
ct_info->helper->name))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
2017-04-21 23:48:06 +00:00
|
|
|
if (ct_info->have_eventmask &&
|
|
|
|
nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
|
|
|
|
return -EMSGSIZE;
|
2019-03-26 18:31:14 +00:00
|
|
|
if (ct_info->timeout[0]) {
|
|
|
|
if (nla_put_string(skb, OVS_CT_ATTR_TIMEOUT, ct_info->timeout))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
2017-04-21 23:48:06 +00:00
|
|
|
|
2019-03-27 08:22:26 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2016-03-10 18:54:23 +00:00
|
|
|
if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
|
|
|
|
return -EMSGSIZE;
|
|
|
|
#endif
|
2015-08-26 18:31:48 +00:00
|
|
|
nla_nest_end(skb, start);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ovs_ct_free_action(const struct nlattr *a)
|
|
|
|
{
|
|
|
|
struct ovs_conntrack_info *ct_info = nla_data(a);
|
|
|
|
|
2015-12-09 22:07:39 +00:00
|
|
|
__ovs_ct_free_action(ct_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
|
|
|
|
{
|
2019-04-17 14:46:17 +00:00
|
|
|
if (ct_info->helper) {
|
2019-05-08 06:52:32 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_NAT)
|
2019-04-17 14:46:17 +00:00
|
|
|
if (ct_info->nat)
|
|
|
|
nf_nat_helper_put(ct_info->helper);
|
|
|
|
#endif
|
2017-05-07 14:01:55 +00:00
|
|
|
nf_conntrack_helper_put(ct_info->helper);
|
2019-04-17 14:46:17 +00:00
|
|
|
}
|
2019-03-26 18:31:14 +00:00
|
|
|
if (ct_info->ct) {
|
|
|
|
if (ct_info->timeout[0])
|
|
|
|
nf_ct_destroy_timeout(ct_info->ct);
|
2019-04-02 06:53:14 +00:00
|
|
|
nf_ct_tmpl_free(ct_info->ct);
|
2019-03-26 18:31:14 +00:00
|
|
|
}
|
2015-08-26 18:31:48 +00:00
|
|
|
}
|
2015-08-26 18:31:52 +00:00
|
|
|
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
|
|
|
|
{
|
|
|
|
int i, err;
|
|
|
|
|
|
|
|
ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!ovs_net->ct_limit_info)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
|
|
|
|
ovs_net->ct_limit_info->limits =
|
|
|
|
kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!ovs_net->ct_limit_info->limits) {
|
|
|
|
kfree(ovs_net->ct_limit_info);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
|
|
|
|
INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
|
|
|
|
|
|
|
|
ovs_net->ct_limit_info->data =
|
|
|
|
nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
|
|
|
|
|
|
|
|
if (IS_ERR(ovs_net->ct_limit_info->data)) {
|
|
|
|
err = PTR_ERR(ovs_net->ct_limit_info->data);
|
|
|
|
kfree(ovs_net->ct_limit_info->limits);
|
|
|
|
kfree(ovs_net->ct_limit_info);
|
|
|
|
pr_err("openvswitch: failed to init nf_conncount %d\n", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
|
|
|
|
{
|
|
|
|
const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
nf_conncount_destroy(net, NFPROTO_INET, info->data);
|
|
|
|
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
|
|
|
|
struct hlist_head *head = &info->limits[i];
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
|
2020-04-16 18:57:31 +00:00
|
|
|
hlist_for_each_entry_rcu(ct_limit, head, hlist_node,
|
|
|
|
lockdep_ovsl_is_held())
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
kfree_rcu(ct_limit, rcu);
|
|
|
|
}
|
2020-09-18 07:14:30 +00:00
|
|
|
kfree(info->limits);
|
|
|
|
kfree(info);
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *
|
|
|
|
ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
|
|
|
|
struct ovs_header **ovs_reply_header)
|
|
|
|
{
|
2023-08-14 21:47:16 +00:00
|
|
|
struct ovs_header *ovs_header = genl_info_userhdr(info);
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
|
|
|
|
if (!skb)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
*ovs_reply_header = genlmsg_put(skb, info->snd_portid,
|
|
|
|
info->snd_seq,
|
|
|
|
&dp_ct_limit_genl_family, 0, cmd);
|
|
|
|
|
|
|
|
if (!*ovs_reply_header) {
|
|
|
|
nlmsg_free(skb);
|
|
|
|
return ERR_PTR(-EMSGSIZE);
|
|
|
|
}
|
|
|
|
(*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_zone_id(int zone_id, u16 *pzone)
|
|
|
|
{
|
|
|
|
if (zone_id >= 0 && zone_id <= 65535) {
|
|
|
|
*pzone = (u16)zone_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
|
|
|
|
struct ovs_ct_limit_info *info)
|
|
|
|
{
|
|
|
|
struct ovs_zone_limit *zone_limit;
|
|
|
|
int rem;
|
|
|
|
u16 zone;
|
|
|
|
|
|
|
|
rem = NLA_ALIGN(nla_len(nla_zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
|
|
|
|
|
|
|
|
while (rem >= sizeof(*zone_limit)) {
|
|
|
|
if (unlikely(zone_limit->zone_id ==
|
|
|
|
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
|
|
|
|
ovs_lock();
|
|
|
|
info->default_limit = zone_limit->limit;
|
|
|
|
ovs_unlock();
|
|
|
|
} else if (unlikely(!check_zone_id(
|
|
|
|
zone_limit->zone_id, &zone))) {
|
|
|
|
OVS_NLERR(true, "zone id is out of range");
|
|
|
|
} else {
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
|
2022-09-23 13:38:20 +00:00
|
|
|
ct_limit = kmalloc(sizeof(*ct_limit),
|
|
|
|
GFP_KERNEL_ACCOUNT);
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
if (!ct_limit)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ct_limit->zone = zone;
|
|
|
|
ct_limit->limit = zone_limit->limit;
|
|
|
|
|
|
|
|
ovs_lock();
|
|
|
|
ct_limit_set(info, ct_limit);
|
|
|
|
ovs_unlock();
|
|
|
|
}
|
|
|
|
rem -= NLA_ALIGN(sizeof(*zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
|
|
|
|
NLA_ALIGN(sizeof(*zone_limit)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rem)
|
|
|
|
OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
|
|
|
|
struct ovs_ct_limit_info *info)
|
|
|
|
{
|
|
|
|
struct ovs_zone_limit *zone_limit;
|
|
|
|
int rem;
|
|
|
|
u16 zone;
|
|
|
|
|
|
|
|
rem = NLA_ALIGN(nla_len(nla_zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
|
|
|
|
|
|
|
|
while (rem >= sizeof(*zone_limit)) {
|
|
|
|
if (unlikely(zone_limit->zone_id ==
|
|
|
|
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
|
|
|
|
ovs_lock();
|
|
|
|
info->default_limit = OVS_CT_LIMIT_DEFAULT;
|
|
|
|
ovs_unlock();
|
|
|
|
} else if (unlikely(!check_zone_id(
|
|
|
|
zone_limit->zone_id, &zone))) {
|
|
|
|
OVS_NLERR(true, "zone id is out of range");
|
|
|
|
} else {
|
|
|
|
ovs_lock();
|
|
|
|
ct_limit_del(info, zone);
|
|
|
|
ovs_unlock();
|
|
|
|
}
|
|
|
|
rem -= NLA_ALIGN(sizeof(*zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
|
|
|
|
NLA_ALIGN(sizeof(*zone_limit)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rem)
|
|
|
|
OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
|
|
|
|
struct sk_buff *reply)
|
|
|
|
{
|
2021-04-04 17:50:31 +00:00
|
|
|
struct ovs_zone_limit zone_limit = {
|
|
|
|
.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE,
|
|
|
|
.limit = info->default_limit,
|
|
|
|
};
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
|
2020-12-08 12:13:53 +00:00
|
|
|
return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __ovs_ct_limit_get_zone_limit(struct net *net,
|
|
|
|
struct nf_conncount_data *data,
|
|
|
|
u16 zone_id, u32 limit,
|
|
|
|
struct sk_buff *reply)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_zone ct_zone;
|
|
|
|
struct ovs_zone_limit zone_limit;
|
|
|
|
u32 conncount_key = zone_id;
|
|
|
|
|
|
|
|
zone_limit.zone_id = zone_id;
|
|
|
|
zone_limit.limit = limit;
|
|
|
|
nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
|
|
|
|
|
|
|
|
zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
|
|
|
|
&ct_zone);
|
|
|
|
return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_get_zone_limit(struct net *net,
|
|
|
|
struct nlattr *nla_zone_limit,
|
|
|
|
struct ovs_ct_limit_info *info,
|
|
|
|
struct sk_buff *reply)
|
|
|
|
{
|
|
|
|
struct ovs_zone_limit *zone_limit;
|
|
|
|
int rem, err;
|
|
|
|
u32 limit;
|
|
|
|
u16 zone;
|
|
|
|
|
|
|
|
rem = NLA_ALIGN(nla_len(nla_zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
|
|
|
|
|
|
|
|
while (rem >= sizeof(*zone_limit)) {
|
|
|
|
if (unlikely(zone_limit->zone_id ==
|
|
|
|
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
|
|
|
|
err = ovs_ct_limit_get_default_limit(info, reply);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
} else if (unlikely(!check_zone_id(zone_limit->zone_id,
|
|
|
|
&zone))) {
|
|
|
|
OVS_NLERR(true, "zone id is out of range");
|
|
|
|
} else {
|
|
|
|
rcu_read_lock();
|
|
|
|
limit = ct_limit_get(info, zone);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
err = __ovs_ct_limit_get_zone_limit(
|
|
|
|
net, info->data, zone, limit, reply);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
rem -= NLA_ALIGN(sizeof(*zone_limit));
|
|
|
|
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
|
|
|
|
NLA_ALIGN(sizeof(*zone_limit)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rem)
|
|
|
|
OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_get_all_zone_limit(struct net *net,
|
|
|
|
struct ovs_ct_limit_info *info,
|
|
|
|
struct sk_buff *reply)
|
|
|
|
{
|
|
|
|
struct ovs_ct_limit *ct_limit;
|
|
|
|
struct hlist_head *head;
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
err = ovs_ct_limit_get_default_limit(info, reply);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
|
|
|
|
head = &info->limits[i];
|
|
|
|
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
|
|
|
|
err = __ovs_ct_limit_get_zone_limit(net, info->data,
|
|
|
|
ct_limit->zone, ct_limit->limit, reply);
|
|
|
|
if (err)
|
|
|
|
goto exit_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_err:
|
|
|
|
rcu_read_unlock();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct sk_buff *reply;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
|
|
|
struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
|
|
|
|
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply))
|
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
|
|
|
if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto exit_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
|
|
|
|
ct_limit_info);
|
|
|
|
if (err)
|
|
|
|
goto exit_err;
|
|
|
|
|
|
|
|
static_branch_enable(&ovs_ct_limit_enabled);
|
|
|
|
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_err:
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct sk_buff *reply;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
|
|
|
struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
|
|
|
|
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply))
|
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
|
|
|
if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto exit_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
|
|
|
|
ct_limit_info);
|
|
|
|
if (err)
|
|
|
|
goto exit_err;
|
|
|
|
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_err:
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct nlattr **a = info->attrs;
|
|
|
|
struct nlattr *nla_reply;
|
|
|
|
struct sk_buff *reply;
|
|
|
|
struct ovs_header *ovs_reply_header;
|
|
|
|
struct net *net = sock_net(skb->sk);
|
|
|
|
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
|
|
|
|
&ovs_reply_header);
|
|
|
|
if (IS_ERR(reply))
|
|
|
|
return PTR_ERR(reply);
|
|
|
|
|
2019-04-26 09:13:06 +00:00
|
|
|
nla_reply = nla_nest_start_noflag(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
|
2019-05-01 13:41:58 +00:00
|
|
|
if (!nla_reply) {
|
|
|
|
err = -EMSGSIZE;
|
|
|
|
goto exit_err;
|
|
|
|
}
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
|
|
|
|
if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
|
|
|
|
err = ovs_ct_limit_get_zone_limit(
|
|
|
|
net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
|
|
|
|
reply);
|
|
|
|
if (err)
|
|
|
|
goto exit_err;
|
|
|
|
} else {
|
|
|
|
err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
|
|
|
|
reply);
|
|
|
|
if (err)
|
|
|
|
goto exit_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
nla_nest_end(reply, nla_reply);
|
|
|
|
genlmsg_end(reply, ovs_reply_header);
|
|
|
|
return genlmsg_reply(reply, info);
|
|
|
|
|
|
|
|
exit_err:
|
|
|
|
nlmsg_free(reply);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:44:17 +00:00
|
|
|
static const struct genl_small_ops ct_limit_genl_ops[] = {
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
{ .cmd = OVS_CT_LIMIT_CMD_SET,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2022-09-23 13:38:20 +00:00
|
|
|
.flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN
|
|
|
|
* privilege.
|
|
|
|
*/
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
.doit = ovs_ct_limit_cmd_set,
|
|
|
|
},
|
|
|
|
{ .cmd = OVS_CT_LIMIT_CMD_DEL,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2022-09-23 13:38:20 +00:00
|
|
|
.flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN
|
|
|
|
* privilege.
|
|
|
|
*/
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
.doit = ovs_ct_limit_cmd_del,
|
|
|
|
},
|
|
|
|
{ .cmd = OVS_CT_LIMIT_CMD_GET,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
.flags = 0, /* OK for unprivileged users. */
|
|
|
|
.doit = ovs_ct_limit_cmd_get,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
|
|
|
|
.name = OVS_CT_LIMIT_MCGROUP,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct genl_family dp_ct_limit_genl_family __ro_after_init = {
|
|
|
|
.hdrsize = sizeof(struct ovs_header),
|
|
|
|
.name = OVS_CT_LIMIT_FAMILY,
|
|
|
|
.version = OVS_CT_LIMIT_VERSION,
|
|
|
|
.maxattr = OVS_CT_LIMIT_ATTR_MAX,
|
genetlink: make policy common to family
Since maxattr is common, the policy can't really differ sanely,
so make it common as well.
The only user that did in fact manage to make a non-common policy
is taskstats, which has to be really careful about it (since it's
still using a common maxattr!). This is no longer supported, but
we can fake it using pre_doit.
This reduces the size of e.g. nl80211.o (which has lots of commands):
text data bss dec hex filename
398745 14323 2240 415308 6564c net/wireless/nl80211.o (before)
397913 14331 2240 414484 65314 net/wireless/nl80211.o (after)
--------------------------------
-832 +8 0 -824
Which is obviously just 8 bytes for each command, and an added 8
bytes for the new policy pointer. I'm not sure why the ops list is
counted as .text though.
Most of the code transformations were done using the following spatch:
@ops@
identifier OPS;
expression POLICY;
@@
struct genl_ops OPS[] = {
...,
{
- .policy = POLICY,
},
...
};
@@
identifier ops.OPS;
expression ops.POLICY;
identifier fam;
expression M;
@@
struct genl_family fam = {
.ops = OPS,
.maxattr = M,
+ .policy = POLICY,
...
};
This also gets rid of devlink_nl_cmd_region_read_dumpit() accessing
the cb->data as ops, which we want to change in a later genl patch.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-03-21 21:51:02 +00:00
|
|
|
.policy = ct_limit_policy,
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
.netnsok = true,
|
|
|
|
.parallel_ops = true,
|
2020-10-02 21:49:54 +00:00
|
|
|
.small_ops = ct_limit_genl_ops,
|
|
|
|
.n_small_ops = ARRAY_SIZE(ct_limit_genl_ops),
|
2022-08-25 00:18:30 +00:00
|
|
|
.resv_start_op = OVS_CT_LIMIT_CMD_GET + 1,
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
.mcgrps = &ovs_ct_limit_multicast_group,
|
|
|
|
.n_mcgrps = 1,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int ovs_ct_init(struct net *net)
|
2015-08-26 18:31:52 +00:00
|
|
|
{
|
2015-10-01 22:00:37 +00:00
|
|
|
unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
|
2015-08-26 18:31:52 +00:00
|
|
|
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
|
2016-04-12 16:14:25 +00:00
|
|
|
if (nf_connlabels_get(net, n_bits - 1)) {
|
2015-08-26 18:31:52 +00:00
|
|
|
ovs_net->xt_label = false;
|
|
|
|
OVS_NLERR(true, "Failed to set connlabel length");
|
|
|
|
} else {
|
|
|
|
ovs_net->xt_label = true;
|
|
|
|
}
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
return ovs_ct_limit_init(net, ovs_net);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2015-08-26 18:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ovs_ct_exit(struct net *net)
|
|
|
|
{
|
|
|
|
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
|
|
|
|
|
openvswitch: Support conntrack zone limit
Currently, nf_conntrack_max is used to limit the maximum number of
conntrack entries in the conntrack table for every network namespace.
For the VMs and containers that reside in the same namespace,
they share the same conntrack table, and the total # of conntrack entries
for all the VMs and containers are limited by nf_conntrack_max. In this
case, if one of the VM/container abuses the usage the conntrack entries,
it blocks the others from committing valid conntrack entries into the
conntrack table. Even if we can possibly put the VM in different network
namespace, the current nf_conntrack_max configuration is kind of rigid
that we cannot limit different VM/container to have different # conntrack
entries.
To address the aforementioned issue, this patch proposes to have a
fine-grained mechanism that could further limit the # of conntrack entries
per-zone. For example, we can designate different zone to different VM,
and set conntrack limit to each zone. By providing this isolation, a
mis-behaved VM only consumes the conntrack entries in its own zone, and
it will not influence other well-behaved VMs. Moreover, the users can
set various conntrack limit to different zone based on their preference.
The proposed implementation utilizes Netfilter's nf_conncount backend
to count the number of connections in a particular zone. If the number of
connection is above a configured limitation, ovs will return ENOMEM to the
userspace. If userspace does not configure the zone limit, the limit
defaults to zero that is no limitation, which is backward compatible to
the behavior without this patch.
The following high leve APIs are provided to the userspace:
- OVS_CT_LIMIT_CMD_SET:
* set default connection limit for all zones
* set the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_DEL:
* remove the connection limit for a particular zone
- OVS_CT_LIMIT_CMD_GET:
* get the default connection limit for all zones
* get the connection limit for a particular zone
Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com>
Acked-by: Pravin B Shelar <pshelar@ovn.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-05-25 00:56:43 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
|
|
|
|
ovs_ct_limit_exit(net, ovs_net);
|
|
|
|
#endif
|
|
|
|
|
2015-08-26 18:31:52 +00:00
|
|
|
if (ovs_net->xt_label)
|
|
|
|
nf_connlabels_put(net);
|
|
|
|
}
|