2015-11-15 22:07:25 -08:00
|
|
|
|
/*
|
2019-02-13 15:34:21 -08:00
|
|
|
|
* Copyright (c) 2015-2019 Nicira, Inc.
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2017-08-06 10:51:14 -07:00
|
|
|
|
#include <ctype.h>
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include <errno.h>
|
2017-11-06 10:32:12 +01:00
|
|
|
|
#include <sys/types.h>
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netinet/icmp6.h>
|
2017-08-06 10:51:14 -07:00
|
|
|
|
#include <string.h>
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
#include "bitmap.h"
|
2017-08-06 10:51:14 -07:00
|
|
|
|
#include "conntrack.h"
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include "conntrack-private.h"
|
2020-04-29 12:25:11 -07:00
|
|
|
|
#include "conntrack-tp.h"
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include "coverage.h"
|
|
|
|
|
#include "csum.h"
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include "ct-dpif.h"
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include "dp-packet.h"
|
|
|
|
|
#include "flow.h"
|
|
|
|
|
#include "netdev.h"
|
|
|
|
|
#include "odp-netlink.h"
|
|
|
|
|
#include "openvswitch/hmap.h"
|
|
|
|
|
#include "openvswitch/vlog.h"
|
|
|
|
|
#include "ovs-rcu.h"
|
2016-05-16 12:59:23 -07:00
|
|
|
|
#include "ovs-thread.h"
|
2017-11-03 13:53:53 +08:00
|
|
|
|
#include "openvswitch/poll-loop.h"
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#include "random.h"
|
|
|
|
|
#include "timeval.h"
|
|
|
|
|
|
|
|
|
|
VLOG_DEFINE_THIS_MODULE(conntrack);
|
|
|
|
|
|
|
|
|
|
COVERAGE_DEFINE(conntrack_full);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
COVERAGE_DEFINE(conntrack_long_cleanup);
|
2021-04-30 19:12:15 +02:00
|
|
|
|
COVERAGE_DEFINE(conntrack_l3csum_err);
|
2020-04-16 12:54:53 -07:00
|
|
|
|
COVERAGE_DEFINE(conntrack_l4csum_err);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
struct conn_lookup_ctx {
|
|
|
|
|
struct conn_key key;
|
|
|
|
|
struct conn *conn;
|
|
|
|
|
uint32_t hash;
|
|
|
|
|
bool reply;
|
2017-07-05 21:32:19 -07:00
|
|
|
|
bool icmp_related;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
};
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
enum ftp_ctl_pkt {
|
|
|
|
|
/* Control packets with address and/or port specifiers. */
|
|
|
|
|
CT_FTP_CTL_INTEREST,
|
|
|
|
|
/* Control packets without address and/or port specifiers. */
|
|
|
|
|
CT_FTP_CTL_OTHER,
|
|
|
|
|
CT_FTP_CTL_INVALID,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum ct_alg_mode {
|
|
|
|
|
CT_FTP_MODE_ACTIVE,
|
|
|
|
|
CT_FTP_MODE_PASSIVE,
|
2017-08-06 10:51:15 -07:00
|
|
|
|
CT_TFTP_MODE,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
};
|
|
|
|
|
|
2017-12-04 08:13:05 -08:00
|
|
|
|
enum ct_alg_ctl_type {
|
|
|
|
|
CT_ALG_CTL_NONE,
|
|
|
|
|
CT_ALG_CTL_FTP,
|
|
|
|
|
CT_ALG_CTL_TFTP,
|
2018-01-09 15:44:55 -08:00
|
|
|
|
/* SIP is not enabled through Openflow and presently only used as
|
|
|
|
|
* an example of an alg that allows a wildcard src ip. */
|
|
|
|
|
CT_ALG_CTL_SIP,
|
2017-12-04 08:13:05 -08:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-03 09:14:17 -08:00
|
|
|
|
struct zone_limit {
|
|
|
|
|
struct hmap_node node;
|
|
|
|
|
struct conntrack_zone_limit czl;
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static bool conn_key_extract(struct conntrack *, struct dp_packet *,
|
2016-05-25 18:10:09 -07:00
|
|
|
|
ovs_be16 dl_type, struct conn_lookup_ctx *,
|
|
|
|
|
uint16_t zone);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static uint32_t conn_key_hash(const struct conn_key *, uint32_t basis);
|
|
|
|
|
static void conn_key_reverse(struct conn_key *);
|
|
|
|
|
static bool valid_new(struct dp_packet *pkt, struct conn_key *);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
static struct conn *new_conn(struct conntrack *ct, struct dp_packet *pkt,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
struct conn_key *, long long now,
|
|
|
|
|
uint32_t tp_id);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
static void delete_conn_cmn(struct conn *);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void delete_conn(struct conn *);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
static void delete_conn_one(struct conn *conn);
|
|
|
|
|
static enum ct_update_res conn_update(struct conntrack *ct, struct conn *conn,
|
|
|
|
|
struct dp_packet *pkt,
|
|
|
|
|
struct conn_lookup_ctx *ctx,
|
2016-05-16 12:59:23 -07:00
|
|
|
|
long long now);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static bool conn_expired(struct conn *, long long now);
|
|
|
|
|
static void set_mark(struct dp_packet *, struct conn *,
|
|
|
|
|
uint32_t val, uint32_t mask);
|
|
|
|
|
static void set_label(struct dp_packet *, struct conn *,
|
|
|
|
|
const struct ovs_key_ct_labels *val,
|
|
|
|
|
const struct ovs_key_ct_labels *mask);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
static void *clean_thread_main(void *f_);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static bool
|
|
|
|
|
nat_select_range_tuple(struct conntrack *ct, const struct conn *conn,
|
|
|
|
|
struct conn *nat_conn);
|
|
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
|
reverse_icmp_type(uint8_t type);
|
|
|
|
|
static uint8_t
|
|
|
|
|
reverse_icmp6_type(uint8_t type);
|
|
|
|
|
static inline bool
|
|
|
|
|
extract_l3_ipv4(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
const char **new_data, bool validate_checksum);
|
|
|
|
|
static inline bool
|
|
|
|
|
extract_l3_ipv6(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
const char **new_data);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static struct alg_exp_node *
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_lookup(struct hmap *alg_expectations, const struct conn_key *key,
|
|
|
|
|
uint32_t basis, bool src_ip_wc);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
repl_ftp_v4_addr(struct dp_packet *pkt, ovs_be32 v4_addr_rep,
|
|
|
|
|
char *ftp_data_v4_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
size_t addr_offset_from_ftp_data_start, size_t addr_size);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
|
|
|
|
static enum ftp_ctl_pkt
|
|
|
|
|
process_ftp_ctl_v4(struct conntrack *ct,
|
|
|
|
|
struct dp_packet *pkt,
|
|
|
|
|
const struct conn *conn_for_expectation,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
ovs_be32 *v4_addr_rep,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
char **ftp_data_v4_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
size_t *addr_offset_from_ftp_data_start,
|
|
|
|
|
size_t *addr_size);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
|
|
|
|
static enum ftp_ctl_pkt
|
|
|
|
|
detect_ftp_ctl_type(const struct conn_lookup_ctx *ctx,
|
|
|
|
|
struct dp_packet *pkt);
|
|
|
|
|
|
2018-01-09 15:44:54 -08:00
|
|
|
|
static void
|
2020-06-17 15:31:09 -07:00
|
|
|
|
expectation_clean(struct conntrack *ct, const struct conn_key *parent_key);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
|
2020-10-12 07:38:38 -04:00
|
|
|
|
static struct ct_l4_proto *l4_protos[UINT8_MAX + 1];
|
2017-12-04 08:13:05 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static void
|
|
|
|
|
handle_ftp_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct dp_packet *pkt, struct conn *ec, long long now,
|
|
|
|
|
enum ftp_ctl_pkt ftp_ctl, bool nat);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2017-08-06 10:51:15 -07:00
|
|
|
|
static void
|
|
|
|
|
handle_tftp_ctl(struct conntrack *ct,
|
2017-12-04 08:13:05 -08:00
|
|
|
|
const struct conn_lookup_ctx *ctx OVS_UNUSED,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct dp_packet *pkt, struct conn *conn_for_expectation,
|
|
|
|
|
long long now OVS_UNUSED, enum ftp_ctl_pkt ftp_ctl OVS_UNUSED,
|
|
|
|
|
bool nat OVS_UNUSED);
|
2017-12-04 08:13:05 -08:00
|
|
|
|
|
|
|
|
|
typedef void (*alg_helper)(struct conntrack *ct,
|
|
|
|
|
const struct conn_lookup_ctx *ctx,
|
|
|
|
|
struct dp_packet *pkt,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn_for_expectation,
|
2017-12-04 08:13:05 -08:00
|
|
|
|
long long now, enum ftp_ctl_pkt ftp_ctl,
|
|
|
|
|
bool nat);
|
|
|
|
|
|
|
|
|
|
static alg_helper alg_helpers[] = {
|
|
|
|
|
[CT_ALG_CTL_NONE] = NULL,
|
|
|
|
|
[CT_ALG_CTL_FTP] = handle_ftp_ctl,
|
|
|
|
|
[CT_ALG_CTL_TFTP] = handle_tftp_ctl,
|
2015-11-15 22:07:25 -08:00
|
|
|
|
};
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
/* The maximum TCP or UDP port number. */
|
|
|
|
|
#define CT_MAX_L4_PORT 65535
|
|
|
|
|
/* String buffer used for parsing FTP string messages.
|
|
|
|
|
* This is sized about twice what is needed to leave some
|
|
|
|
|
* margin of error. */
|
|
|
|
|
#define LARGEST_FTP_MSG_OF_INTEREST 128
|
|
|
|
|
/* FTP port string used in active mode. */
|
|
|
|
|
#define FTP_PORT_CMD "PORT"
|
|
|
|
|
/* FTP pasv string used in passive mode. */
|
|
|
|
|
#define FTP_PASV_REPLY_CODE "227"
|
|
|
|
|
/* Maximum decimal digits for port in FTP command.
|
|
|
|
|
* The port is represented as two 3 digit numbers with the
|
|
|
|
|
* high part a multiple of 256. */
|
|
|
|
|
#define MAX_FTP_PORT_DGTS 3
|
|
|
|
|
|
|
|
|
|
/* FTP extension EPRT string used for active mode. */
|
|
|
|
|
#define FTP_EPRT_CMD "EPRT"
|
|
|
|
|
/* FTP extension EPSV string used for passive mode. */
|
|
|
|
|
#define FTP_EPSV_REPLY "EXTENDED PASSIVE"
|
|
|
|
|
/* Maximum decimal digits for port in FTP extended command. */
|
|
|
|
|
#define MAX_EXT_FTP_PORT_DGTS 5
|
|
|
|
|
/* FTP extended command code for IPv6. */
|
|
|
|
|
#define FTP_AF_V6 '2'
|
|
|
|
|
/* Used to indicate a wildcard L4 source port number for ALGs.
|
|
|
|
|
* This is used for port numbers that we cannot predict in
|
|
|
|
|
* expectations. */
|
|
|
|
|
#define ALG_WC_SRC_PORT 0
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* If the total number of connections goes above this value, no new connections
|
2017-05-30 10:49:27 -07:00
|
|
|
|
* are accepted; this is for CT_CONN_TYPE_DEFAULT connections. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
#define DEFAULT_N_CONN_LIMIT 3000000
|
|
|
|
|
|
2017-08-06 10:51:13 -07:00
|
|
|
|
/* Does a member by member comparison of two conn_keys; this
|
|
|
|
|
* function must be kept in sync with struct conn_key; returns 0
|
|
|
|
|
* if the keys are equal or 1 if the keys are not equal. */
|
|
|
|
|
static int
|
|
|
|
|
conn_key_cmp(const struct conn_key *key1, const struct conn_key *key2)
|
|
|
|
|
{
|
|
|
|
|
if (!memcmp(&key1->src.addr, &key2->src.addr, sizeof key1->src.addr) &&
|
|
|
|
|
!memcmp(&key1->dst.addr, &key2->dst.addr, sizeof key1->dst.addr) &&
|
|
|
|
|
(key1->src.icmp_id == key2->src.icmp_id) &&
|
|
|
|
|
(key1->src.icmp_type == key2->src.icmp_type) &&
|
|
|
|
|
(key1->src.icmp_code == key2->src.icmp_code) &&
|
|
|
|
|
(key1->dst.icmp_id == key2->dst.icmp_id) &&
|
|
|
|
|
(key1->dst.icmp_type == key2->dst.icmp_type) &&
|
|
|
|
|
(key1->dst.icmp_code == key2->dst.icmp_code) &&
|
|
|
|
|
(key1->dl_type == key2->dl_type) &&
|
|
|
|
|
(key1->zone == key2->zone) &&
|
|
|
|
|
(key1->nw_proto == key2->nw_proto)) {
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 20:51:44 -07:00
|
|
|
|
static void
|
2018-01-09 15:44:56 -08:00
|
|
|
|
ct_print_conn_info(const struct conn *c, const char *log_msg,
|
|
|
|
|
enum vlog_level vll, bool force, bool rl_on)
|
2017-09-25 20:51:43 -07:00
|
|
|
|
{
|
|
|
|
|
#define CT_VLOG(RL_ON, LEVEL, ...) \
|
|
|
|
|
do { \
|
|
|
|
|
if (RL_ON) { \
|
|
|
|
|
static struct vlog_rate_limit rl_ = VLOG_RATE_LIMIT_INIT(5, 5); \
|
|
|
|
|
vlog_rate_limit(&this_module, LEVEL, &rl_, __VA_ARGS__); \
|
|
|
|
|
} else { \
|
|
|
|
|
vlog(&this_module, LEVEL, __VA_ARGS__); \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
if (OVS_UNLIKELY(force || vlog_is_enabled(&this_module, vll))) {
|
|
|
|
|
if (c->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
CT_VLOG(rl_on, vll, "%s: src ip "IP_FMT" dst ip "IP_FMT" rev src "
|
|
|
|
|
"ip "IP_FMT" rev dst ip "IP_FMT" src/dst ports "
|
|
|
|
|
"%"PRIu16"/%"PRIu16" rev src/dst ports "
|
|
|
|
|
"%"PRIu16"/%"PRIu16" zone/rev zone "
|
|
|
|
|
"%"PRIu16"/%"PRIu16" nw_proto/rev nw_proto "
|
|
|
|
|
"%"PRIu8"/%"PRIu8, log_msg,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
IP_ARGS(c->key.src.addr.ipv4),
|
|
|
|
|
IP_ARGS(c->key.dst.addr.ipv4),
|
|
|
|
|
IP_ARGS(c->rev_key.src.addr.ipv4),
|
|
|
|
|
IP_ARGS(c->rev_key.dst.addr.ipv4),
|
2017-09-25 20:51:43 -07:00
|
|
|
|
ntohs(c->key.src.port), ntohs(c->key.dst.port),
|
|
|
|
|
ntohs(c->rev_key.src.port), ntohs(c->rev_key.dst.port),
|
|
|
|
|
c->key.zone, c->rev_key.zone, c->key.nw_proto,
|
|
|
|
|
c->rev_key.nw_proto);
|
|
|
|
|
} else {
|
|
|
|
|
char ip6_s[INET6_ADDRSTRLEN];
|
|
|
|
|
inet_ntop(AF_INET6, &c->key.src.addr.ipv6, ip6_s, sizeof ip6_s);
|
|
|
|
|
char ip6_d[INET6_ADDRSTRLEN];
|
|
|
|
|
inet_ntop(AF_INET6, &c->key.dst.addr.ipv6, ip6_d, sizeof ip6_d);
|
|
|
|
|
char ip6_rs[INET6_ADDRSTRLEN];
|
|
|
|
|
inet_ntop(AF_INET6, &c->rev_key.src.addr.ipv6, ip6_rs,
|
|
|
|
|
sizeof ip6_rs);
|
|
|
|
|
char ip6_rd[INET6_ADDRSTRLEN];
|
|
|
|
|
inet_ntop(AF_INET6, &c->rev_key.dst.addr.ipv6, ip6_rd,
|
|
|
|
|
sizeof ip6_rd);
|
|
|
|
|
|
|
|
|
|
CT_VLOG(rl_on, vll, "%s: src ip %s dst ip %s rev src ip %s"
|
|
|
|
|
" rev dst ip %s src/dst ports %"PRIu16"/%"PRIu16
|
|
|
|
|
" rev src/dst ports %"PRIu16"/%"PRIu16" zone/rev zone "
|
|
|
|
|
"%"PRIu16"/%"PRIu16" nw_proto/rev nw_proto "
|
|
|
|
|
"%"PRIu8"/%"PRIu8, log_msg, ip6_s, ip6_d, ip6_rs,
|
|
|
|
|
ip6_rd, ntohs(c->key.src.port), ntohs(c->key.dst.port),
|
|
|
|
|
ntohs(c->rev_key.src.port), ntohs(c->rev_key.dst.port),
|
|
|
|
|
c->key.zone, c->rev_key.zone, c->key.nw_proto,
|
|
|
|
|
c->rev_key.nw_proto);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* Initializes the connection tracker 'ct'. The caller is responsible for
|
|
|
|
|
* calling 'conntrack_destroy()', when the instance is not needed anymore */
|
2019-05-02 21:34:04 -07:00
|
|
|
|
struct conntrack *
|
|
|
|
|
conntrack_init(void)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2020-10-12 07:38:38 -04:00
|
|
|
|
static struct ovsthread_once setup_l4_once = OVSTHREAD_ONCE_INITIALIZER;
|
2019-05-02 21:34:04 -07:00
|
|
|
|
struct conntrack *ct = xzalloc(sizeof *ct);
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_init(&ct->resources_lock);
|
|
|
|
|
ovs_rwlock_wrlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
hmap_init(&ct->alg_expectations);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
hindex_init(&ct->alg_expectation_refs);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_init_adaptive(&ct->ct_lock);
|
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
cmap_init(&ct->conns);
|
|
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(ct->exp_lists); i++) {
|
|
|
|
|
ovs_list_init(&ct->exp_lists[i]);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2019-12-03 09:14:17 -08:00
|
|
|
|
hmap_init(&ct->zone_limits);
|
|
|
|
|
ct->zone_limit_seq = 0;
|
2020-04-29 12:25:11 -07:00
|
|
|
|
timeout_policy_init(ct);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
ct->hash_basis = random_uint32();
|
|
|
|
|
atomic_count_init(&ct->n_conn, 0);
|
|
|
|
|
atomic_init(&ct->n_conn_limit, DEFAULT_N_CONN_LIMIT);
|
2019-09-25 14:09:41 -07:00
|
|
|
|
atomic_init(&ct->tcp_seq_chk, true);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
latch_init(&ct->clean_thread_exit);
|
|
|
|
|
ct->clean_thread = ovs_thread_create("ct_clean", clean_thread_main, ct);
|
2019-02-13 15:34:21 -08:00
|
|
|
|
ct->ipf = ipf_init();
|
2019-05-02 21:34:04 -07:00
|
|
|
|
|
2020-10-12 07:38:38 -04:00
|
|
|
|
/* Initialize the l4 protocols. */
|
|
|
|
|
if (ovsthread_once_start(&setup_l4_once)) {
|
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(l4_protos); i++) {
|
|
|
|
|
l4_protos[i] = &ct_proto_other;
|
|
|
|
|
}
|
|
|
|
|
/* IPPROTO_UDP uses ct_proto_other, so no need to initialize it. */
|
|
|
|
|
l4_protos[IPPROTO_TCP] = &ct_proto_tcp;
|
|
|
|
|
l4_protos[IPPROTO_ICMP] = &ct_proto_icmp4;
|
|
|
|
|
l4_protos[IPPROTO_ICMPV6] = &ct_proto_icmp6;
|
|
|
|
|
|
|
|
|
|
ovsthread_once_done(&setup_l4_once);
|
|
|
|
|
}
|
2019-05-02 21:34:04 -07:00
|
|
|
|
return ct;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 09:14:17 -08:00
|
|
|
|
static uint32_t
|
|
|
|
|
zone_key_hash(int32_t zone, uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
size_t hash = hash_int((OVS_FORCE uint32_t) zone, basis);
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct zone_limit *
|
|
|
|
|
zone_limit_lookup(struct conntrack *ct, int32_t zone)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
uint32_t hash = zone_key_hash(zone, ct->hash_basis);
|
|
|
|
|
struct zone_limit *zl;
|
|
|
|
|
HMAP_FOR_EACH_IN_BUCKET (zl, node, hash, &ct->zone_limits) {
|
|
|
|
|
if (zl->czl.zone == zone) {
|
|
|
|
|
return zl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct zone_limit *
|
|
|
|
|
zone_limit_lookup_or_default(struct conntrack *ct, int32_t zone)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
struct zone_limit *zl = zone_limit_lookup(ct, zone);
|
|
|
|
|
return zl ? zl : zone_limit_lookup(ct, DEFAULT_ZONE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct conntrack_zone_limit
|
|
|
|
|
zone_limit_get(struct conntrack *ct, int32_t zone)
|
|
|
|
|
{
|
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
struct conntrack_zone_limit czl = {DEFAULT_ZONE, 0, 0, 0};
|
|
|
|
|
struct zone_limit *zl = zone_limit_lookup_or_default(ct, zone);
|
|
|
|
|
if (zl) {
|
|
|
|
|
czl = zl->czl;
|
|
|
|
|
}
|
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
|
|
|
|
return czl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
zone_limit_create(struct conntrack *ct, int32_t zone, uint32_t limit)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
if (zone >= DEFAULT_ZONE && zone <= MAX_ZONE) {
|
|
|
|
|
struct zone_limit *zl = xzalloc(sizeof *zl);
|
|
|
|
|
zl->czl.limit = limit;
|
|
|
|
|
zl->czl.zone = zone;
|
|
|
|
|
zl->czl.zone_limit_seq = ct->zone_limit_seq++;
|
|
|
|
|
uint32_t hash = zone_key_hash(zone, ct->hash_basis);
|
|
|
|
|
hmap_insert(&ct->zone_limits, &zl->node, hash);
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
zone_limit_update(struct conntrack *ct, int32_t zone, uint32_t limit)
|
|
|
|
|
{
|
|
|
|
|
int err = 0;
|
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
struct zone_limit *zl = zone_limit_lookup(ct, zone);
|
|
|
|
|
if (zl) {
|
|
|
|
|
zl->czl.limit = limit;
|
|
|
|
|
VLOG_INFO("Changed zone limit of %u for zone %d", limit, zone);
|
|
|
|
|
} else {
|
|
|
|
|
err = zone_limit_create(ct, zone, limit);
|
|
|
|
|
if (!err) {
|
|
|
|
|
VLOG_INFO("Created zone limit of %u for zone %d", limit, zone);
|
|
|
|
|
} else {
|
|
|
|
|
VLOG_WARN("Request to create zone limit for invalid zone %d",
|
|
|
|
|
zone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
zone_limit_clean(struct conntrack *ct, struct zone_limit *zl)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
hmap_remove(&ct->zone_limits, &zl->node);
|
|
|
|
|
free(zl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
zone_limit_delete(struct conntrack *ct, uint16_t zone)
|
|
|
|
|
{
|
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
struct zone_limit *zl = zone_limit_lookup(ct, zone);
|
|
|
|
|
if (zl) {
|
|
|
|
|
zone_limit_clean(ct, zl);
|
|
|
|
|
VLOG_INFO("Deleted zone limit for zone %d", zone);
|
|
|
|
|
} else {
|
|
|
|
|
VLOG_INFO("Attempted delete of non-existent zone limit: zone %d",
|
|
|
|
|
zone);
|
|
|
|
|
}
|
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
static void
|
|
|
|
|
conn_clean_cmn(struct conntrack *ct, struct conn *conn)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
if (conn->alg) {
|
|
|
|
|
expectation_clean(ct, &conn->key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t hash = conn_key_hash(&conn->key, ct->hash_basis);
|
|
|
|
|
cmap_remove(&ct->conns, &conn->cm_node, hash);
|
2019-12-03 09:14:17 -08:00
|
|
|
|
|
|
|
|
|
struct zone_limit *zl = zone_limit_lookup(ct, conn->admit_zone);
|
|
|
|
|
if (zl && zl->czl.zone_limit_seq == conn->zone_limit_seq) {
|
|
|
|
|
zl->czl.count--;
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Must be called with 'conn' of 'conn_type' CT_CONN_TYPE_DEFAULT. Also
|
|
|
|
|
* removes the associated nat 'conn' from the lookup datastructures. */
|
|
|
|
|
static void
|
|
|
|
|
conn_clean(struct conntrack *ct, struct conn *conn)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
ovs_assert(conn->conn_type == CT_CONN_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
|
|
conn_clean_cmn(ct, conn);
|
|
|
|
|
if (conn->nat_conn) {
|
|
|
|
|
uint32_t hash = conn_key_hash(&conn->nat_conn->key, ct->hash_basis);
|
|
|
|
|
cmap_remove(&ct->conns, &conn->nat_conn->cm_node, hash);
|
|
|
|
|
}
|
|
|
|
|
ovs_list_remove(&conn->exp_node);
|
2019-05-28 11:14:42 -07:00
|
|
|
|
conn->cleaned = true;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovsrcu_postpone(delete_conn, conn);
|
|
|
|
|
atomic_count_dec(&ct->n_conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
conn_clean_one(struct conntrack *ct, struct conn *conn)
|
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
|
|
|
|
{
|
|
|
|
|
conn_clean_cmn(ct, conn);
|
|
|
|
|
if (conn->conn_type == CT_CONN_TYPE_DEFAULT) {
|
|
|
|
|
ovs_list_remove(&conn->exp_node);
|
2019-05-28 11:14:42 -07:00
|
|
|
|
conn->cleaned = true;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
atomic_count_dec(&ct->n_conn);
|
|
|
|
|
}
|
|
|
|
|
ovsrcu_postpone(delete_conn_one, conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Destroys the connection tracker 'ct' and frees all the allocated memory.
|
|
|
|
|
* The caller of this function must already have shut down packet input
|
|
|
|
|
* and PMD threads (which would have been quiesced). */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
void
|
|
|
|
|
conntrack_destroy(struct conntrack *ct)
|
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn;
|
2016-05-16 12:59:23 -07:00
|
|
|
|
latch_set(&ct->clean_thread_exit);
|
|
|
|
|
pthread_join(ct->clean_thread, NULL);
|
|
|
|
|
latch_destroy(&ct->clean_thread_exit);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
CMAP_FOR_EACH (conn, cm_node, &ct->conns) {
|
|
|
|
|
conn_clean_one(ct, conn);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
cmap_destroy(&ct->conns);
|
2019-12-03 09:14:17 -08:00
|
|
|
|
|
|
|
|
|
struct zone_limit *zl;
|
|
|
|
|
HMAP_FOR_EACH_POP (zl, node, &ct->zone_limits) {
|
|
|
|
|
free(zl);
|
|
|
|
|
}
|
|
|
|
|
hmap_destroy(&ct->zone_limits);
|
|
|
|
|
|
2020-04-29 12:25:11 -07:00
|
|
|
|
struct timeout_policy *tp;
|
|
|
|
|
HMAP_FOR_EACH_POP (tp, node, &ct->timeout_policies) {
|
|
|
|
|
free(tp);
|
|
|
|
|
}
|
|
|
|
|
hmap_destroy(&ct->timeout_policies);
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
|
|
|
|
ovs_mutex_destroy(&ct->ct_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_wrlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
struct alg_exp_node *alg_exp_node;
|
|
|
|
|
HMAP_FOR_EACH_POP (alg_exp_node, node, &ct->alg_expectations) {
|
|
|
|
|
free(alg_exp_node);
|
|
|
|
|
}
|
|
|
|
|
hmap_destroy(&ct->alg_expectations);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
hindex_destroy(&ct->alg_expectation_refs);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
|
|
|
|
ovs_rwlock_destroy(&ct->resources_lock);
|
|
|
|
|
|
2019-02-13 15:34:21 -08:00
|
|
|
|
ipf_destroy(ct->ipf);
|
2019-05-06 07:37:18 -07:00
|
|
|
|
free(ct);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
conn_key_lookup(struct conntrack *ct, const struct conn_key *key,
|
|
|
|
|
uint32_t hash, long long now, struct conn **conn_out,
|
|
|
|
|
bool *reply)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn;
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
|
|
CMAP_FOR_EACH_WITH_HASH (conn, cm_node, hash, &ct->conns) {
|
|
|
|
|
if (!conn_key_cmp(&conn->key, key) && !conn_expired(conn, now)) {
|
|
|
|
|
found = true;
|
|
|
|
|
if (reply) {
|
|
|
|
|
*reply = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!conn_key_cmp(&conn->rev_key, key) && !conn_expired(conn, now)) {
|
|
|
|
|
found = true;
|
|
|
|
|
if (reply) {
|
|
|
|
|
*reply = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
if (found && conn_out) {
|
|
|
|
|
*conn_out = conn;
|
|
|
|
|
} else if (conn_out) {
|
|
|
|
|
*conn_out = NULL;
|
|
|
|
|
}
|
|
|
|
|
return found;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 11:14:44 -07:00
|
|
|
|
static bool
|
|
|
|
|
conn_lookup(struct conntrack *ct, const struct conn_key *key,
|
|
|
|
|
long long now, struct conn **conn_out, bool *reply)
|
|
|
|
|
{
|
|
|
|
|
uint32_t hash = conn_key_hash(key, ct->hash_basis);
|
|
|
|
|
return conn_key_lookup(ct, key, hash, now, conn_out, reply);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
2017-05-30 10:49:27 -07:00
|
|
|
|
write_ct_md(struct dp_packet *pkt, uint16_t zone, const struct conn *conn,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
const struct conn_key *key, const struct alg_exp_node *alg_exp)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2017-05-30 10:49:27 -07:00
|
|
|
|
pkt->md.ct_state |= CS_TRACKED;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
pkt->md.ct_zone = zone;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
|
|
|
|
if (conn) {
|
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
pkt->md.ct_mark = conn->mark;
|
|
|
|
|
pkt->md.ct_label = conn->label;
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
} else {
|
|
|
|
|
pkt->md.ct_mark = 0;
|
|
|
|
|
pkt->md.ct_label = OVS_U128_ZERO;
|
|
|
|
|
}
|
2017-03-08 17:18:23 -08:00
|
|
|
|
|
|
|
|
|
/* Use the original direction tuple if we have it. */
|
|
|
|
|
if (conn) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (conn->alg_related) {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
key = &conn->parent_key;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
} else {
|
|
|
|
|
key = &conn->key;
|
|
|
|
|
}
|
|
|
|
|
} else if (alg_exp) {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
pkt->md.ct_mark = alg_exp->parent_mark;
|
|
|
|
|
pkt->md.ct_label = alg_exp->parent_label;
|
|
|
|
|
key = &alg_exp->parent_key;
|
2017-03-08 17:18:23 -08:00
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-03-08 17:18:23 -08:00
|
|
|
|
pkt->md.ct_orig_tuple_ipv6 = false;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-03-08 17:18:23 -08:00
|
|
|
|
if (key) {
|
|
|
|
|
if (key->dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
pkt->md.ct_orig_tuple.ipv4 = (struct ovs_key_ct_tuple_ipv4) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
key->src.addr.ipv4,
|
|
|
|
|
key->dst.addr.ipv4,
|
2017-03-08 17:18:23 -08:00
|
|
|
|
key->nw_proto != IPPROTO_ICMP
|
|
|
|
|
? key->src.port : htons(key->src.icmp_type),
|
|
|
|
|
key->nw_proto != IPPROTO_ICMP
|
|
|
|
|
? key->dst.port : htons(key->src.icmp_code),
|
|
|
|
|
key->nw_proto,
|
|
|
|
|
};
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
2017-03-08 17:18:23 -08:00
|
|
|
|
pkt->md.ct_orig_tuple_ipv6 = true;
|
|
|
|
|
pkt->md.ct_orig_tuple.ipv6 = (struct ovs_key_ct_tuple_ipv6) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
key->src.addr.ipv6,
|
|
|
|
|
key->dst.addr.ipv6,
|
2017-03-08 17:18:23 -08:00
|
|
|
|
key->nw_proto != IPPROTO_ICMPV6
|
|
|
|
|
? key->src.port : htons(key->src.icmp_type),
|
|
|
|
|
key->nw_proto != IPPROTO_ICMPV6
|
|
|
|
|
? key->dst.port : htons(key->src.icmp_code),
|
|
|
|
|
key->nw_proto,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
memset(&pkt->md.ct_orig_tuple, 0, sizeof pkt->md.ct_orig_tuple);
|
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
|
get_ip_proto(const struct dp_packet *pkt)
|
|
|
|
|
{
|
|
|
|
|
uint8_t ip_proto;
|
|
|
|
|
struct eth_header *l2 = dp_packet_eth(pkt);
|
|
|
|
|
if (l2->eth_type == htons(ETH_TYPE_IPV6)) {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
ip_proto = nh6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
|
|
|
|
|
} else {
|
|
|
|
|
struct ip_header *l3_hdr = dp_packet_l3(pkt);
|
|
|
|
|
ip_proto = l3_hdr->ip_proto;
|
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return ip_proto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2017-12-04 08:13:05 -08:00
|
|
|
|
is_ftp_ctl(const enum ct_alg_ctl_type ct_alg_ctl)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
2017-12-04 08:13:05 -08:00
|
|
|
|
return ct_alg_ctl == CT_ALG_CTL_FTP;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 08:13:05 -08:00
|
|
|
|
static enum ct_alg_ctl_type
|
2017-12-04 08:13:06 -08:00
|
|
|
|
get_alg_ctl_type(const struct dp_packet *pkt, ovs_be16 tp_src, ovs_be16 tp_dst,
|
|
|
|
|
const char *helper)
|
2017-08-06 10:51:15 -07:00
|
|
|
|
{
|
2017-12-04 08:13:05 -08:00
|
|
|
|
/* CT_IPPORT_FTP/TFTP is used because IPPORT_FTP/TFTP in not defined
|
|
|
|
|
* in OSX, at least in in.h. Since these values will never change, remove
|
2017-08-06 10:51:15 -07:00
|
|
|
|
* the external dependency. */
|
2017-12-04 08:13:05 -08:00
|
|
|
|
enum { CT_IPPORT_FTP = 21 };
|
|
|
|
|
enum { CT_IPPORT_TFTP = 69 };
|
2017-12-04 08:13:06 -08:00
|
|
|
|
uint8_t ip_proto = get_ip_proto(pkt);
|
|
|
|
|
struct udp_header *uh = dp_packet_l4(pkt);
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
ovs_be16 ftp_src_port = htons(CT_IPPORT_FTP);
|
|
|
|
|
ovs_be16 ftp_dst_port = htons(CT_IPPORT_FTP);
|
|
|
|
|
ovs_be16 tftp_dst_port = htons(CT_IPPORT_TFTP);
|
|
|
|
|
|
|
|
|
|
if (OVS_UNLIKELY(tp_dst)) {
|
|
|
|
|
if (helper && !strncmp(helper, "ftp", strlen("ftp"))) {
|
|
|
|
|
ftp_dst_port = tp_dst;
|
|
|
|
|
} else if (helper && !strncmp(helper, "tftp", strlen("tftp"))) {
|
|
|
|
|
tftp_dst_port = tp_dst;
|
|
|
|
|
}
|
|
|
|
|
} else if (OVS_UNLIKELY(tp_src)) {
|
|
|
|
|
if (helper && !strncmp(helper, "ftp", strlen("ftp"))) {
|
|
|
|
|
ftp_src_port = tp_src;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-06 10:51:15 -07:00
|
|
|
|
|
2017-12-04 08:13:06 -08:00
|
|
|
|
if (ip_proto == IPPROTO_UDP && uh->udp_dst == tftp_dst_port) {
|
2017-12-04 08:13:05 -08:00
|
|
|
|
return CT_ALG_CTL_TFTP;
|
|
|
|
|
} else if (ip_proto == IPPROTO_TCP &&
|
2017-12-04 08:13:06 -08:00
|
|
|
|
(th->tcp_src == ftp_src_port || th->tcp_dst == ftp_dst_port)) {
|
2017-12-04 08:13:05 -08:00
|
|
|
|
return CT_ALG_CTL_FTP;
|
|
|
|
|
}
|
|
|
|
|
return CT_ALG_CTL_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:55 -08:00
|
|
|
|
static bool
|
|
|
|
|
alg_src_ip_wc(enum ct_alg_ctl_type alg_ctl_type)
|
|
|
|
|
{
|
|
|
|
|
if (alg_ctl_type == CT_ALG_CTL_SIP) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 08:13:05 -08:00
|
|
|
|
static void
|
|
|
|
|
handle_alg_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx,
|
|
|
|
|
struct dp_packet *pkt, enum ct_alg_ctl_type ct_alg_ctl,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn, long long now, bool nat)
|
2017-12-04 08:13:05 -08:00
|
|
|
|
{
|
|
|
|
|
/* ALG control packet handling with expectation creation. */
|
2017-12-04 08:13:07 -08:00
|
|
|
|
if (OVS_UNLIKELY(alg_helpers[ct_alg_ctl] && conn && conn->alg)) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
alg_helpers[ct_alg_ctl](ct, ctx, pkt, conn, now, CT_FTP_CTL_INTEREST,
|
|
|
|
|
nat);
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2017-12-04 08:13:05 -08:00
|
|
|
|
}
|
2017-08-06 10:51:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static void
|
|
|
|
|
pat_packet(struct dp_packet *pkt, const struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, conn->rev_key.dst.port, th->tcp_dst);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, conn->rev_key.dst.port, uh->udp_dst);
|
|
|
|
|
}
|
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, th->tcp_src, conn->rev_key.src.port);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, uh->udp_src, conn->rev_key.src.port);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nat_packet(struct dp_packet *pkt, const struct conn *conn, bool related)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
pkt->md.ct_state |= CS_SRC_NAT;
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
struct ip_header *nh = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv4_addr(pkt, &nh->ip_src,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->rev_key.dst.addr.ipv4);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
nh6->ip6_src.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->rev_key.dst.addr.ipv6, true);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
if (!related) {
|
|
|
|
|
pat_packet(pkt, conn);
|
|
|
|
|
}
|
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
pkt->md.ct_state |= CS_DST_NAT;
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
struct ip_header *nh = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv4_addr(pkt, &nh->ip_dst,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->rev_key.src.addr.ipv4);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
nh6->ip6_dst.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->rev_key.src.addr.ipv6, true);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
if (!related) {
|
|
|
|
|
pat_packet(pkt, conn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
un_pat_packet(struct dp_packet *pkt, const struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, th->tcp_src, conn->key.src.port);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, uh->udp_src, conn->key.src.port);
|
|
|
|
|
}
|
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, conn->key.dst.port, th->tcp_dst);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, conn->key.dst.port, uh->udp_dst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:28 -07:00
|
|
|
|
static void
|
|
|
|
|
reverse_pat_packet(struct dp_packet *pkt, const struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th_in = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, conn->key.src.port,
|
|
|
|
|
th_in->tcp_dst);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh_in = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, conn->key.src.port,
|
|
|
|
|
uh_in->udp_dst);
|
|
|
|
|
}
|
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
if (conn->key.nw_proto == IPPROTO_TCP) {
|
|
|
|
|
struct tcp_header *th_in = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_tcp_port(pkt, th_in->tcp_src,
|
|
|
|
|
conn->key.dst.port);
|
|
|
|
|
} else if (conn->key.nw_proto == IPPROTO_UDP) {
|
|
|
|
|
struct udp_header *uh_in = dp_packet_l4(pkt);
|
|
|
|
|
packet_set_udp_port(pkt, uh_in->udp_src,
|
|
|
|
|
conn->key.dst.port);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
reverse_nat_packet(struct dp_packet *pkt, const struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
char *tail = dp_packet_tail(pkt);
|
2020-10-26 16:03:19 -03:00
|
|
|
|
uint16_t pad = dp_packet_l2_pad_size(pkt);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
struct conn_key inner_key;
|
|
|
|
|
const char *inner_l4 = NULL;
|
|
|
|
|
uint16_t orig_l3_ofs = pkt->l3_ofs;
|
|
|
|
|
uint16_t orig_l4_ofs = pkt->l4_ofs;
|
|
|
|
|
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
struct ip_header *nh = dp_packet_l3(pkt);
|
|
|
|
|
struct icmp_header *icmp = dp_packet_l4(pkt);
|
|
|
|
|
struct ip_header *inner_l3 = (struct ip_header *) (icmp + 1);
|
2019-08-30 09:13:19 -07:00
|
|
|
|
/* This call is already verified to succeed during the code path from
|
|
|
|
|
* 'conn_key_extract()' which calls 'extract_l4_icmp()'. */
|
2017-08-06 10:51:14 -07:00
|
|
|
|
extract_l3_ipv4(&inner_key, inner_l3, tail - ((char *)inner_l3) - pad,
|
|
|
|
|
&inner_l4, false);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
pkt->l3_ofs += (char *) inner_l3 - (char *) nh;
|
|
|
|
|
pkt->l4_ofs += inner_l4 - (char *) icmp;
|
|
|
|
|
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
packet_set_ipv4_addr(pkt, &inner_l3->ip_src,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->key.src.addr.ipv4);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
packet_set_ipv4_addr(pkt, &inner_l3->ip_dst,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->key.dst.addr.ipv4);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-05-30 10:49:28 -07:00
|
|
|
|
reverse_pat_packet(pkt, conn);
|
|
|
|
|
icmp->icmp_csum = 0;
|
|
|
|
|
icmp->icmp_csum = csum(icmp, tail - (char *) icmp - pad);
|
|
|
|
|
} else {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
2019-10-01 18:18:23 +03:00
|
|
|
|
struct icmp6_data_header *icmp6 = dp_packet_l4(pkt);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
struct ovs_16aligned_ip6_hdr *inner_l3_6 =
|
|
|
|
|
(struct ovs_16aligned_ip6_hdr *) (icmp6 + 1);
|
2019-08-30 09:13:19 -07:00
|
|
|
|
/* This call is already verified to succeed during the code path from
|
|
|
|
|
* 'conn_key_extract()' which calls 'extract_l4_icmp6()'. */
|
2017-05-30 10:49:28 -07:00
|
|
|
|
extract_l3_ipv6(&inner_key, inner_l3_6,
|
|
|
|
|
tail - ((char *)inner_l3_6) - pad,
|
|
|
|
|
&inner_l4);
|
|
|
|
|
pkt->l3_ofs += (char *) inner_l3_6 - (char *) nh6;
|
|
|
|
|
pkt->l4_ofs += inner_l4 - (char *) icmp6;
|
|
|
|
|
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
inner_l3_6->ip6_src.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->key.src.addr.ipv6, true);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
inner_l3_6->ip6_dst.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->key.dst.addr.ipv6, true);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
}
|
|
|
|
|
reverse_pat_packet(pkt, conn);
|
|
|
|
|
icmp6->icmp6_base.icmp6_cksum = 0;
|
2019-02-22 17:17:42 -08:00
|
|
|
|
icmp6->icmp6_base.icmp6_cksum = packet_csum_upperlayer6(nh6, icmp6,
|
|
|
|
|
IPPROTO_ICMPV6, tail - (char *) icmp6 - pad);
|
2017-05-30 10:49:28 -07:00
|
|
|
|
}
|
|
|
|
|
pkt->l3_ofs = orig_l3_ofs;
|
|
|
|
|
pkt->l4_ofs = orig_l4_ofs;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static void
|
|
|
|
|
un_nat_packet(struct dp_packet *pkt, const struct conn *conn,
|
|
|
|
|
bool related)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
pkt->md.ct_state |= CS_DST_NAT;
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
struct ip_header *nh = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv4_addr(pkt, &nh->ip_dst,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->key.src.addr.ipv4);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
nh6->ip6_dst.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->key.src.addr.ipv6, true);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
2017-05-30 10:49:28 -07:00
|
|
|
|
|
|
|
|
|
if (OVS_UNLIKELY(related)) {
|
|
|
|
|
reverse_nat_packet(pkt, conn);
|
|
|
|
|
} else {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
un_pat_packet(pkt, conn);
|
|
|
|
|
}
|
|
|
|
|
} else if (conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
pkt->md.ct_state |= CS_SRC_NAT;
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
struct ip_header *nh = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv4_addr(pkt, &nh->ip_src,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
conn->key.dst.addr.ipv4);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
packet_set_ipv6_addr(pkt, conn->key.nw_proto,
|
|
|
|
|
nh6->ip6_src.be32,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
&conn->key.dst.addr.ipv6, true);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
2017-05-30 10:49:28 -07:00
|
|
|
|
|
|
|
|
|
if (OVS_UNLIKELY(related)) {
|
|
|
|
|
reverse_nat_packet(pkt, conn);
|
|
|
|
|
} else {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
un_pat_packet(pkt, conn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static void
|
2019-05-09 08:15:07 -07:00
|
|
|
|
conn_seq_skew_set(struct conntrack *ct, const struct conn *conn_in,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
long long now, int seq_skew, bool seq_skew_dir)
|
2019-05-09 08:15:07 -07:00
|
|
|
|
OVS_NO_THREAD_SAFETY_ANALYSIS
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn;
|
|
|
|
|
ovs_mutex_unlock(&conn_in->lock);
|
2019-05-28 11:14:44 -07:00
|
|
|
|
conn_lookup(ct, &conn_in->key, now, &conn, NULL);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn_in->lock);
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (conn && seq_skew) {
|
|
|
|
|
conn->seq_skew = seq_skew;
|
|
|
|
|
conn->seq_skew_dir = seq_skew_dir;
|
|
|
|
|
}
|
2019-03-15 15:01:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 08:13:07 -08:00
|
|
|
|
static bool
|
|
|
|
|
ct_verify_helper(const char *helper, enum ct_alg_ctl_type ct_alg_ctl)
|
|
|
|
|
{
|
|
|
|
|
if (ct_alg_ctl == CT_ALG_CTL_NONE) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (helper) {
|
|
|
|
|
if ((ct_alg_ctl == CT_ALG_CTL_FTP) &&
|
|
|
|
|
!strncmp(helper, "ftp", strlen("ftp"))) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if ((ct_alg_ctl == CT_ALG_CTL_TFTP) &&
|
|
|
|
|
!strncmp(helper, "tftp", strlen("tftp"))) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static struct conn *
|
|
|
|
|
conn_not_found(struct conntrack *ct, struct dp_packet *pkt,
|
2017-05-30 10:49:27 -07:00
|
|
|
|
struct conn_lookup_ctx *ctx, bool commit, long long now,
|
|
|
|
|
const struct nat_action_info_t *nat_action_info,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
const char *helper, const struct alg_exp_node *alg_exp,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
enum ct_alg_ctl_type ct_alg_ctl, uint32_t tp_id)
|
2019-05-09 08:15:07 -07:00
|
|
|
|
OVS_REQUIRES(ct->ct_lock)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
struct conn *nc = NULL;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *nat_conn = NULL;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
if (!valid_new(pkt, &ctx->key)) {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
pkt->md.ct_state = CS_INVALID;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return nc;
|
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
pkt->md.ct_state = CS_NEW;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (alg_exp) {
|
|
|
|
|
pkt->md.ct_state |= CS_RELATED;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
if (commit) {
|
2019-12-03 09:14:17 -08:00
|
|
|
|
struct zone_limit *zl = zone_limit_lookup_or_default(ct,
|
|
|
|
|
ctx->key.zone);
|
|
|
|
|
if (zl && zl->czl.count >= zl->czl.limit) {
|
|
|
|
|
return nc;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
unsigned int n_conn_limit;
|
|
|
|
|
atomic_read_relaxed(&ct->n_conn_limit, &n_conn_limit);
|
|
|
|
|
if (atomic_count_get(&ct->n_conn) >= n_conn_limit) {
|
|
|
|
|
COVERAGE_INC(conntrack_full);
|
|
|
|
|
return nc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 12:25:11 -07:00
|
|
|
|
nc = new_conn(ct, pkt, &ctx->key, now, tp_id);
|
2019-03-15 15:01:18 -07:00
|
|
|
|
memcpy(&nc->key, &ctx->key, sizeof nc->key);
|
2019-03-15 15:01:20 -07:00
|
|
|
|
memcpy(&nc->rev_key, &nc->key, sizeof nc->rev_key);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
conn_key_reverse(&nc->rev_key);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-12-04 08:13:07 -08:00
|
|
|
|
if (ct_verify_helper(helper, ct_alg_ctl)) {
|
|
|
|
|
nc->alg = nullable_xstrdup(helper);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (alg_exp) {
|
|
|
|
|
nc->alg_related = true;
|
2020-06-17 15:31:09 -07:00
|
|
|
|
nc->mark = alg_exp->parent_mark;
|
|
|
|
|
nc->label = alg_exp->parent_label;
|
|
|
|
|
nc->parent_key = alg_exp->parent_key;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (nat_action_info) {
|
|
|
|
|
nc->nat_info = xmemdup(nat_action_info, sizeof *nc->nat_info);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
nat_conn = xzalloc(sizeof *nat_conn);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (alg_exp) {
|
2018-01-09 15:44:55 -08:00
|
|
|
|
if (alg_exp->nat_rpl_dst) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
nc->rev_key.dst.addr = alg_exp->alg_nat_repl_addr;
|
|
|
|
|
nc->nat_info->nat_action = NAT_ACTION_SRC;
|
|
|
|
|
} else {
|
|
|
|
|
nc->rev_key.src.addr = alg_exp->alg_nat_repl_addr;
|
|
|
|
|
nc->nat_info->nat_action = NAT_ACTION_DST;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
memcpy(nat_conn, nc, sizeof *nat_conn);
|
|
|
|
|
bool nat_res = nat_select_range_tuple(ct, nc, nat_conn);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (!nat_res) {
|
|
|
|
|
goto nat_res_exhaustion;
|
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
/* Update nc with nat adjustments made to nat_conn by
|
|
|
|
|
* nat_select_range_tuple(). */
|
|
|
|
|
memcpy(nc, nat_conn, sizeof *nc);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
2017-07-05 21:32:19 -07:00
|
|
|
|
nat_packet(pkt, nc, ctx->icmp_related);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
memcpy(&nat_conn->key, &nc->rev_key, sizeof nat_conn->key);
|
|
|
|
|
memcpy(&nat_conn->rev_key, &nc->key, sizeof nat_conn->rev_key);
|
|
|
|
|
nat_conn->conn_type = CT_CONN_TYPE_UN_NAT;
|
|
|
|
|
nat_conn->nat_info = NULL;
|
|
|
|
|
nat_conn->alg = NULL;
|
|
|
|
|
nat_conn->nat_conn = NULL;
|
|
|
|
|
uint32_t nat_hash = conn_key_hash(&nat_conn->key, ct->hash_basis);
|
|
|
|
|
cmap_insert(&ct->conns, &nat_conn->cm_node, nat_hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nc->nat_conn = nat_conn;
|
|
|
|
|
ovs_mutex_init_adaptive(&nc->lock);
|
|
|
|
|
nc->conn_type = CT_CONN_TYPE_DEFAULT;
|
|
|
|
|
cmap_insert(&ct->conns, &nc->cm_node, ctx->hash);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
atomic_count_inc(&ct->n_conn);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ctx->conn = nc; /* For completeness. */
|
2019-12-03 09:14:17 -08:00
|
|
|
|
if (zl) {
|
|
|
|
|
nc->admit_zone = zl->czl.zone;
|
|
|
|
|
nc->zone_limit_seq = zl->czl.zone_limit_seq;
|
|
|
|
|
zl->czl.count++;
|
|
|
|
|
} else {
|
|
|
|
|
nc->admit_zone = INVALID_ZONE;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return nc;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
/* This would be a user error or a DOS attack. A user error is prevented
|
|
|
|
|
* by allocating enough combinations of NAT addresses when combined with
|
|
|
|
|
* ephemeral ports. A DOS attack should be protected against with
|
|
|
|
|
* firewall rules or a separate firewall. Also using zone partitioning
|
|
|
|
|
* can limit DoS impact. */
|
2017-08-06 10:51:14 -07:00
|
|
|
|
nat_res_exhaustion:
|
2019-05-09 08:15:07 -07:00
|
|
|
|
free(nat_conn);
|
|
|
|
|
ovs_list_remove(&nc->exp_node);
|
|
|
|
|
delete_conn_cmn(nc);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
|
|
|
|
|
VLOG_WARN_RL(&rl, "Unable to NAT due to tuple space exhaustion - "
|
|
|
|
|
"if DoS attack, use firewalling and/or zone partitioning.");
|
|
|
|
|
return NULL;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static bool
|
|
|
|
|
conn_update_state(struct conntrack *ct, struct dp_packet *pkt,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn_lookup_ctx *ctx, struct conn *conn,
|
|
|
|
|
long long now)
|
2017-05-30 10:49:27 -07:00
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_assert(conn->conn_type == CT_CONN_TYPE_DEFAULT);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
bool create_new_conn = false;
|
|
|
|
|
|
2017-07-05 21:32:19 -07:00
|
|
|
|
if (ctx->icmp_related) {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
pkt->md.ct_state |= CS_RELATED;
|
|
|
|
|
if (ctx->reply) {
|
|
|
|
|
pkt->md.ct_state |= CS_REPLY_DIR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
if (conn->alg_related) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
pkt->md.ct_state |= CS_RELATED;
|
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
enum ct_update_res res = conn_update(ct, conn, pkt, ctx, now);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
|
|
|
|
switch (res) {
|
|
|
|
|
case CT_UPDATE_VALID:
|
|
|
|
|
pkt->md.ct_state |= CS_ESTABLISHED;
|
|
|
|
|
pkt->md.ct_state &= ~CS_NEW;
|
|
|
|
|
if (ctx->reply) {
|
|
|
|
|
pkt->md.ct_state |= CS_REPLY_DIR;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CT_UPDATE_INVALID:
|
|
|
|
|
pkt->md.ct_state = CS_INVALID;
|
|
|
|
|
break;
|
|
|
|
|
case CT_UPDATE_NEW:
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
2019-05-28 11:14:44 -07:00
|
|
|
|
if (conn_lookup(ct, &conn->key, now, NULL, NULL)) {
|
2019-05-28 11:14:43 -07:00
|
|
|
|
conn_clean(ct, conn);
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
create_new_conn = true;
|
|
|
|
|
break;
|
2019-12-20 09:51:08 -08:00
|
|
|
|
case CT_UPDATE_VALID_NEW:
|
|
|
|
|
pkt->md.ct_state |= CS_NEW;
|
|
|
|
|
break;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
default:
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return create_new_conn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_nat(struct dp_packet *pkt, struct conn *conn,
|
|
|
|
|
uint16_t zone, bool reply, bool related)
|
|
|
|
|
{
|
|
|
|
|
if (conn->nat_info &&
|
|
|
|
|
(!(pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) ||
|
|
|
|
|
(pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT) &&
|
|
|
|
|
zone != pkt->md.ct_zone))) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) {
|
|
|
|
|
pkt->md.ct_state &= ~(CS_SRC_NAT | CS_DST_NAT);
|
|
|
|
|
}
|
|
|
|
|
if (reply) {
|
|
|
|
|
un_nat_packet(pkt, conn, related);
|
|
|
|
|
} else {
|
|
|
|
|
nat_packet(pkt, conn, related);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:29 -07:00
|
|
|
|
static bool
|
|
|
|
|
check_orig_tuple(struct conntrack *ct, struct dp_packet *pkt,
|
|
|
|
|
struct conn_lookup_ctx *ctx_in, long long now,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn **conn,
|
2017-05-30 10:49:29 -07:00
|
|
|
|
const struct nat_action_info_t *nat_action_info)
|
|
|
|
|
{
|
2019-09-23 16:44:33 -07:00
|
|
|
|
if (!(pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) ||
|
|
|
|
|
(ctx_in->key.dl_type == htons(ETH_TYPE_IP) &&
|
2017-05-30 10:49:29 -07:00
|
|
|
|
!pkt->md.ct_orig_tuple.ipv4.ipv4_proto) ||
|
|
|
|
|
(ctx_in->key.dl_type == htons(ETH_TYPE_IPV6) &&
|
|
|
|
|
!pkt->md.ct_orig_tuple.ipv6.ipv6_proto) ||
|
|
|
|
|
nat_action_info) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn_key key;
|
|
|
|
|
memset(&key, 0 , sizeof key);
|
2017-05-30 10:49:29 -07:00
|
|
|
|
|
|
|
|
|
if (ctx_in->key.dl_type == htons(ETH_TYPE_IP)) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.addr.ipv4 = pkt->md.ct_orig_tuple.ipv4.ipv4_src;
|
|
|
|
|
key.dst.addr.ipv4 = pkt->md.ct_orig_tuple.ipv4.ipv4_dst;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
|
|
|
|
|
if (ctx_in->key.nw_proto == IPPROTO_ICMP) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.icmp_id = ctx_in->key.src.icmp_id;
|
|
|
|
|
key.dst.icmp_id = ctx_in->key.dst.icmp_id;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
uint16_t src_port = ntohs(pkt->md.ct_orig_tuple.ipv4.src_port);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.icmp_type = (uint8_t) src_port;
|
|
|
|
|
key.dst.icmp_type = reverse_icmp_type(key.src.icmp_type);
|
2017-05-30 10:49:29 -07:00
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.port = pkt->md.ct_orig_tuple.ipv4.src_port;
|
|
|
|
|
key.dst.port = pkt->md.ct_orig_tuple.ipv4.dst_port;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.nw_proto = pkt->md.ct_orig_tuple.ipv4.ipv4_proto;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.addr.ipv6 = pkt->md.ct_orig_tuple.ipv6.ipv6_src;
|
|
|
|
|
key.dst.addr.ipv6 = pkt->md.ct_orig_tuple.ipv6.ipv6_dst;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
|
|
|
|
|
if (ctx_in->key.nw_proto == IPPROTO_ICMPV6) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.icmp_id = ctx_in->key.src.icmp_id;
|
|
|
|
|
key.dst.icmp_id = ctx_in->key.dst.icmp_id;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
uint16_t src_port = ntohs(pkt->md.ct_orig_tuple.ipv6.src_port);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.icmp_type = (uint8_t) src_port;
|
|
|
|
|
key.dst.icmp_type = reverse_icmp6_type(key.src.icmp_type);
|
2017-05-30 10:49:29 -07:00
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.src.port = pkt->md.ct_orig_tuple.ipv6.src_port;
|
|
|
|
|
key.dst.port = pkt->md.ct_orig_tuple.ipv6.dst_port;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.nw_proto = pkt->md.ct_orig_tuple.ipv6.ipv6_proto;
|
2017-05-30 10:49:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
key.dl_type = ctx_in->key.dl_type;
|
|
|
|
|
key.zone = pkt->md.ct_zone;
|
2019-05-28 11:14:44 -07:00
|
|
|
|
conn_lookup(ct, &key, now, conn, NULL);
|
2017-05-30 10:49:29 -07:00
|
|
|
|
return *conn ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 08:13:05 -08:00
|
|
|
|
static bool
|
|
|
|
|
conn_update_state_alg(struct conntrack *ct, struct dp_packet *pkt,
|
|
|
|
|
struct conn_lookup_ctx *ctx, struct conn *conn,
|
|
|
|
|
const struct nat_action_info_t *nat_action_info,
|
|
|
|
|
enum ct_alg_ctl_type ct_alg_ctl, long long now,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
bool *create_new_conn)
|
2017-12-04 08:13:05 -08:00
|
|
|
|
{
|
|
|
|
|
if (is_ftp_ctl(ct_alg_ctl)) {
|
|
|
|
|
/* Keep sequence tracking in sync with the source of the
|
|
|
|
|
* sequence skew. */
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
2017-12-04 08:13:05 -08:00
|
|
|
|
if (ctx->reply != conn->seq_skew_dir) {
|
|
|
|
|
handle_ftp_ctl(ct, ctx, pkt, conn, now, CT_FTP_CTL_OTHER,
|
|
|
|
|
!!nat_action_info);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
/* conn_update_state locks for unrelated fields, so unlock. */
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
*create_new_conn = conn_update_state(ct, pkt, ctx, conn, now);
|
2017-12-04 08:13:05 -08:00
|
|
|
|
} else {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
/* conn_update_state locks for unrelated fields, so unlock. */
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
*create_new_conn = conn_update_state(ct, pkt, ctx, conn, now);
|
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
2018-07-10 16:41:58 -07:00
|
|
|
|
if (*create_new_conn == false) {
|
|
|
|
|
handle_ftp_ctl(ct, ctx, pkt, conn, now, CT_FTP_CTL_OTHER,
|
|
|
|
|
!!nat_action_info);
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2017-12-04 08:13:05 -08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 09:05:44 -07:00
|
|
|
|
static void
|
|
|
|
|
set_cached_conn(const struct nat_action_info_t *nat_action_info,
|
|
|
|
|
const struct conn_lookup_ctx *ctx, struct conn *conn,
|
|
|
|
|
struct dp_packet *pkt)
|
|
|
|
|
{
|
|
|
|
|
if (OVS_LIKELY(!nat_action_info)) {
|
|
|
|
|
pkt->md.conn = conn;
|
|
|
|
|
pkt->md.reply = ctx->reply;
|
|
|
|
|
pkt->md.icmp_related = ctx->icmp_related;
|
|
|
|
|
} else {
|
|
|
|
|
pkt->md.conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
process_one_fast(uint16_t zone, const uint32_t *setmark,
|
|
|
|
|
const struct ovs_key_ct_labels *setlabel,
|
|
|
|
|
const struct nat_action_info_t *nat_action_info,
|
|
|
|
|
struct conn *conn, struct dp_packet *pkt)
|
|
|
|
|
{
|
|
|
|
|
if (nat_action_info) {
|
|
|
|
|
handle_nat(pkt, conn, zone, pkt->md.reply, pkt->md.icmp_related);
|
|
|
|
|
pkt->md.conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkt->md.ct_zone = zone;
|
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
pkt->md.ct_mark = conn->mark;
|
|
|
|
|
pkt->md.ct_label = conn->label;
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
|
|
|
|
|
if (setmark) {
|
|
|
|
|
set_mark(pkt, conn, setmark[0], setmark[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (setlabel) {
|
|
|
|
|
set_label(pkt, conn, &setlabel[0], &setlabel[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static void
|
2015-11-15 22:07:25 -08:00
|
|
|
|
process_one(struct conntrack *ct, struct dp_packet *pkt,
|
|
|
|
|
struct conn_lookup_ctx *ctx, uint16_t zone,
|
2017-05-30 10:49:27 -07:00
|
|
|
|
bool force, bool commit, long long now, const uint32_t *setmark,
|
|
|
|
|
const struct ovs_key_ct_labels *setlabel,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
const struct nat_action_info_t *nat_action_info,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
ovs_be16 tp_src, ovs_be16 tp_dst, const char *helper,
|
|
|
|
|
uint32_t tp_id)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2020-03-19 20:21:16 +01:00
|
|
|
|
/* Reset ct_state whenever entering a new zone. */
|
|
|
|
|
if (pkt->md.ct_state && pkt->md.ct_zone != zone) {
|
|
|
|
|
pkt->md.ct_state = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
bool create_new_conn = false;
|
|
|
|
|
conn_key_lookup(ct, &ctx->key, ctx->hash, now, &ctx->conn, &ctx->reply);
|
|
|
|
|
struct conn *conn = ctx->conn;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-03-08 17:18:23 -08:00
|
|
|
|
/* Delete found entry if in wrong direction. 'force' implies commit. */
|
2019-03-15 15:01:18 -07:00
|
|
|
|
if (OVS_UNLIKELY(force && ctx->reply && conn)) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
2019-05-28 11:14:44 -07:00
|
|
|
|
if (conn_lookup(ct, &conn->key, now, NULL, NULL)) {
|
2019-05-28 11:14:43 -07:00
|
|
|
|
conn_clean(ct, conn);
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2017-03-08 17:18:23 -08:00
|
|
|
|
conn = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (OVS_LIKELY(conn)) {
|
|
|
|
|
if (conn->conn_type == CT_CONN_TYPE_UN_NAT) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
ctx->reply = true;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *rev_conn = conn; /* Save for debugging. */
|
2019-05-28 11:14:44 -07:00
|
|
|
|
uint32_t hash = conn_key_hash(&conn->rev_key, ct->hash_basis);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
conn_key_lookup(ct, &ctx->key, hash, now, &conn, &ctx->reply);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
if (!conn) {
|
2020-03-19 20:21:16 +01:00
|
|
|
|
pkt->md.ct_state |= CS_INVALID;
|
|
|
|
|
write_ct_md(pkt, zone, NULL, NULL, NULL);
|
2020-06-17 15:31:09 -07:00
|
|
|
|
char *log_msg = xasprintf("Missing parent conn %p", rev_conn);
|
2020-03-17 16:12:21 -07:00
|
|
|
|
ct_print_conn_info(rev_conn, log_msg, VLL_INFO, true, true);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
free(log_msg);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
return;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 08:13:06 -08:00
|
|
|
|
enum ct_alg_ctl_type ct_alg_ctl = get_alg_ctl_type(pkt, tp_src, tp_dst,
|
|
|
|
|
helper);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (OVS_LIKELY(conn)) {
|
2017-12-04 08:13:05 -08:00
|
|
|
|
if (OVS_LIKELY(!conn_update_state_alg(ct, pkt, ctx, conn,
|
|
|
|
|
nat_action_info,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ct_alg_ctl, now,
|
2017-12-04 08:13:05 -08:00
|
|
|
|
&create_new_conn))) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
create_new_conn = conn_update_state(ct, pkt, ctx, conn, now);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (nat_action_info && !create_new_conn) {
|
2017-07-05 21:32:19 -07:00
|
|
|
|
handle_nat(pkt, conn, zone, ctx->reply, ctx->icmp_related);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-09-23 16:44:33 -07:00
|
|
|
|
} else if (check_orig_tuple(ct, pkt, ctx, now, &conn, nat_action_info)) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
create_new_conn = conn_update_state(ct, pkt, ctx, conn, now);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else {
|
2017-07-05 21:32:19 -07:00
|
|
|
|
if (ctx->icmp_related) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
/* An icmp related conn should always be found; no new
|
|
|
|
|
connection is created based on an icmp related packet. */
|
2017-05-30 10:49:27 -07:00
|
|
|
|
pkt->md.ct_state = CS_INVALID;
|
2016-12-19 14:03:58 -08:00
|
|
|
|
} else {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
create_new_conn = true;
|
2016-12-19 14:03:58 -08:00
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
const struct alg_exp_node *alg_exp = NULL;
|
2018-07-10 14:05:50 +03:00
|
|
|
|
struct alg_exp_node alg_exp_entry;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (OVS_UNLIKELY(create_new_conn)) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_rdlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
alg_exp = expectation_lookup(&ct->alg_expectations, &ctx->key,
|
2018-01-09 15:44:55 -08:00
|
|
|
|
ct->hash_basis,
|
|
|
|
|
alg_src_ip_wc(ct_alg_ctl));
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (alg_exp) {
|
2019-02-04 16:02:15 -08:00
|
|
|
|
memcpy(&alg_exp_entry, alg_exp, sizeof alg_exp_entry);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
alg_exp = &alg_exp_entry;
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
2019-05-28 11:14:44 -07:00
|
|
|
|
if (!conn_lookup(ct, &ctx->key, now, NULL, NULL)) {
|
2019-05-28 11:14:43 -07:00
|
|
|
|
conn = conn_not_found(ct, pkt, ctx, commit, now, nat_action_info,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
helper, alg_exp, ct_alg_ctl, tp_id);
|
2019-05-28 11:14:43 -07:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
write_ct_md(pkt, zone, conn, &ctx->key, alg_exp);
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (conn && setmark) {
|
|
|
|
|
set_mark(pkt, conn, setmark[0], setmark[1]);
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (conn && setlabel) {
|
|
|
|
|
set_label(pkt, conn, &setlabel[0], &setlabel[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
handle_alg_ctl(ct, ctx, pkt, ct_alg_ctl, conn, now, !!nat_action_info);
|
2019-08-26 09:05:44 -07:00
|
|
|
|
|
|
|
|
|
set_cached_conn(nat_action_info, ctx, conn, pkt);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sends the packets in '*pkt_batch' through the connection tracker 'ct'. All
|
2019-02-13 15:34:17 -08:00
|
|
|
|
* the packets must have the same 'dl_type' (IPv4 or IPv6) and should have
|
2019-02-13 15:34:21 -08:00
|
|
|
|
* the l3 and and l4 offset properly set. Performs fragment reassembly with
|
|
|
|
|
* the help of ipf_preprocess_conntrack().
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
|
|
|
|
* If 'commit' is true, the packets are allowed to create new entries in the
|
|
|
|
|
* connection tables. 'setmark', if not NULL, should point to a two
|
|
|
|
|
* elements array containing a value and a mask to set the connection mark.
|
|
|
|
|
* 'setlabel' behaves similarly for the connection label.*/
|
|
|
|
|
int
|
|
|
|
|
conntrack_execute(struct conntrack *ct, struct dp_packet_batch *pkt_batch,
|
2017-03-08 17:18:23 -08:00
|
|
|
|
ovs_be16 dl_type, bool force, bool commit, uint16_t zone,
|
2016-05-25 18:10:09 -07:00
|
|
|
|
const uint32_t *setmark,
|
2015-11-15 22:07:25 -08:00
|
|
|
|
const struct ovs_key_ct_labels *setlabel,
|
2017-12-04 08:13:06 -08:00
|
|
|
|
ovs_be16 tp_src, ovs_be16 tp_dst, const char *helper,
|
2017-08-24 22:16:53 -07:00
|
|
|
|
const struct nat_action_info_t *nat_action_info,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
long long now, uint32_t tp_id)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-02-13 15:34:21 -08:00
|
|
|
|
ipf_preprocess_conntrack(ct->ipf, pkt_batch, now, dl_type, zone,
|
|
|
|
|
ct->hash_basis);
|
|
|
|
|
|
2017-09-22 02:04:32 -07:00
|
|
|
|
struct dp_packet *packet;
|
2017-05-30 10:49:26 -07:00
|
|
|
|
struct conn_lookup_ctx ctx;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-02-27 10:41:30 -08:00
|
|
|
|
DP_PACKET_BATCH_FOR_EACH (i, packet, pkt_batch) {
|
2019-08-26 09:05:44 -07:00
|
|
|
|
struct conn *conn = packet->md.conn;
|
|
|
|
|
if (OVS_UNLIKELY(packet->md.ct_state == CS_INVALID)) {
|
|
|
|
|
write_ct_md(packet, zone, NULL, NULL, NULL);
|
|
|
|
|
} else if (conn && conn->key.zone == zone && !force
|
|
|
|
|
&& !get_alg_ctl_type(packet, tp_src, tp_dst, helper)) {
|
|
|
|
|
process_one_fast(zone, setmark, setlabel, nat_action_info,
|
|
|
|
|
conn, packet);
|
|
|
|
|
} else if (OVS_UNLIKELY(!conn_key_extract(ct, packet, dl_type, &ctx,
|
|
|
|
|
zone))) {
|
2017-09-22 02:04:32 -07:00
|
|
|
|
packet->md.ct_state = CS_INVALID;
|
|
|
|
|
write_ct_md(packet, zone, NULL, NULL, NULL);
|
2019-08-26 09:05:44 -07:00
|
|
|
|
} else {
|
|
|
|
|
process_one(ct, packet, &ctx, zone, force, commit, now, setmark,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
setlabel, nat_action_info, tp_src, tp_dst, helper,
|
|
|
|
|
tp_id);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 15:34:21 -08:00
|
|
|
|
ipf_postprocess_conntrack(ct->ipf, pkt_batch, now, dl_type);
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 14:21:51 -05:00
|
|
|
|
void
|
|
|
|
|
conntrack_clear(struct dp_packet *packet)
|
|
|
|
|
{
|
|
|
|
|
/* According to pkt_metadata_init(), ct_state == 0 is enough to make all of
|
|
|
|
|
* the conntrack fields invalid. */
|
|
|
|
|
packet->md.ct_state = 0;
|
2019-08-26 09:05:44 -07:00
|
|
|
|
pkt_metadata_init_conn(&packet->md);
|
2018-01-19 14:21:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
|
|
|
|
set_mark(struct dp_packet *pkt, struct conn *conn, uint32_t val, uint32_t mask)
|
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (conn->alg_related) {
|
|
|
|
|
pkt->md.ct_mark = conn->mark;
|
|
|
|
|
} else {
|
|
|
|
|
pkt->md.ct_mark = val | (pkt->md.ct_mark & ~(mask));
|
|
|
|
|
conn->mark = pkt->md.ct_mark;
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
set_label(struct dp_packet *pkt, struct conn *conn,
|
|
|
|
|
const struct ovs_key_ct_labels *val,
|
|
|
|
|
const struct ovs_key_ct_labels *mask)
|
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (conn->alg_related) {
|
|
|
|
|
pkt->md.ct_label = conn->label;
|
|
|
|
|
} else {
|
|
|
|
|
ovs_u128 v, m;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
memcpy(&v, val, sizeof v);
|
|
|
|
|
memcpy(&m, mask, sizeof m);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
pkt->md.ct_label.u64.lo = v.u64.lo
|
2015-11-15 22:07:25 -08:00
|
|
|
|
| (pkt->md.ct_label.u64.lo & ~(m.u64.lo));
|
2017-08-06 10:51:14 -07:00
|
|
|
|
pkt->md.ct_label.u64.hi = v.u64.hi
|
2015-11-15 22:07:25 -08:00
|
|
|
|
| (pkt->md.ct_label.u64.hi & ~(m.u64.hi));
|
2017-08-06 10:51:14 -07:00
|
|
|
|
conn->label = pkt->md.ct_label;
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2016-05-16 12:59:23 -07:00
|
|
|
|
/* Delete the expired connections from 'ctb', up to 'limit'. Returns the
|
|
|
|
|
* earliest expiration time among the remaining connections in 'ctb'. Returns
|
|
|
|
|
* LLONG_MAX if 'ctb' is empty. The return value might be smaller than 'now',
|
|
|
|
|
* if 'limit' is reached */
|
|
|
|
|
static long long
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ct_sweep(struct conntrack *ct, long long now, size_t limit)
|
2016-05-16 12:59:23 -07:00
|
|
|
|
{
|
|
|
|
|
struct conn *conn, *next;
|
|
|
|
|
long long min_expiration = LLONG_MAX;
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
for (unsigned i = 0; i < N_CT_TM; i++) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
LIST_FOR_EACH_SAFE (conn, next, exp_node, &ct->exp_lists[i]) {
|
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
if (now < conn->expiration || count >= limit) {
|
2019-03-15 15:01:18 -07:00
|
|
|
|
min_expiration = MIN(min_expiration, conn->expiration);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2019-03-15 15:01:18 -07:00
|
|
|
|
if (count >= limit) {
|
|
|
|
|
/* Do not check other lists. */
|
|
|
|
|
COVERAGE_INC(conntrack_long_cleanup);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
goto out;
|
2016-05-16 12:59:23 -07:00
|
|
|
|
}
|
2019-03-15 15:01:18 -07:00
|
|
|
|
break;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
} else {
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
conn_clean(ct, conn);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
}
|
2019-03-15 15:01:18 -07:00
|
|
|
|
count++;
|
2016-05-16 12:59:23 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
VLOG_DBG("conntrack cleanup %"PRIuSIZE" entries in %lld msec", count,
|
|
|
|
|
time_msec() - now);
|
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
return min_expiration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cleans up old connection entries from 'ct'. Returns the time when the
|
|
|
|
|
* next expiration might happen. The return value might be smaller than
|
|
|
|
|
* 'now', meaning that an internal limit has been reached, and some expired
|
|
|
|
|
* connections have not been deleted. */
|
|
|
|
|
static long long
|
|
|
|
|
conntrack_clean(struct conntrack *ct, long long now)
|
|
|
|
|
{
|
|
|
|
|
unsigned int n_conn_limit;
|
|
|
|
|
atomic_read_relaxed(&ct->n_conn_limit, &n_conn_limit);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
size_t clean_max = n_conn_limit > 10 ? n_conn_limit / 10 : 1;
|
|
|
|
|
long long min_exp = ct_sweep(ct, now, clean_max);
|
2020-04-29 12:25:11 -07:00
|
|
|
|
long long next_wakeup = MIN(min_exp, now + CT_DPIF_NETDEV_TP_MIN);
|
2016-05-16 12:59:23 -07:00
|
|
|
|
|
|
|
|
|
return next_wakeup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Cleanup:
|
|
|
|
|
*
|
|
|
|
|
* We must call conntrack_clean() periodically. conntrack_clean() return
|
|
|
|
|
* value gives an hint on when the next cleanup must be done (either because
|
|
|
|
|
* there is an actual connection that expires, or because a new connection
|
|
|
|
|
* might be created with the minimum timeout).
|
|
|
|
|
*
|
|
|
|
|
* The logic below has two goals:
|
|
|
|
|
*
|
2016-07-27 18:32:15 -07:00
|
|
|
|
* - We want to reduce the number of wakeups and batch connection cleanup
|
|
|
|
|
* when the load is not very high. CT_CLEAN_INTERVAL ensures that if we
|
|
|
|
|
* are coping with the current cleanup tasks, then we wait at least
|
|
|
|
|
* 5 seconds to do further cleanup.
|
2016-05-16 12:59:23 -07:00
|
|
|
|
*
|
2019-05-09 08:15:07 -07:00
|
|
|
|
* - We don't want to keep the map locked too long, as we might prevent
|
2016-07-27 18:32:15 -07:00
|
|
|
|
* traffic from flowing. CT_CLEAN_MIN_INTERVAL ensures that if cleanup is
|
2019-05-09 08:15:07 -07:00
|
|
|
|
* behind, there is at least some 200ms blocks of time when the map will be
|
2016-07-27 18:32:15 -07:00
|
|
|
|
* left alone, so the datapath can operate unhindered.
|
2016-05-16 12:59:23 -07:00
|
|
|
|
*/
|
|
|
|
|
#define CT_CLEAN_INTERVAL 5000 /* 5 seconds */
|
|
|
|
|
#define CT_CLEAN_MIN_INTERVAL 200 /* 0.2 seconds */
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
|
clean_thread_main(void *f_)
|
|
|
|
|
{
|
|
|
|
|
struct conntrack *ct = f_;
|
|
|
|
|
|
|
|
|
|
while (!latch_is_set(&ct->clean_thread_exit)) {
|
|
|
|
|
long long next_wake;
|
|
|
|
|
long long now = time_msec();
|
|
|
|
|
next_wake = conntrack_clean(ct, now);
|
|
|
|
|
|
|
|
|
|
if (next_wake < now) {
|
|
|
|
|
poll_timer_wait_until(now + CT_CLEAN_MIN_INTERVAL);
|
|
|
|
|
} else {
|
|
|
|
|
poll_timer_wait_until(MAX(next_wake, now + CT_CLEAN_INTERVAL));
|
|
|
|
|
}
|
|
|
|
|
latch_wait(&ct->clean_thread_exit);
|
|
|
|
|
poll_block();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
/* 'Data' is a pointer to the beginning of the L3 header and 'new_data' is
|
|
|
|
|
* used to store a pointer to the first byte after the L3 header. 'Size' is
|
|
|
|
|
* the size of the packet beyond the data pointer. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static inline bool
|
|
|
|
|
extract_l3_ipv4(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
const char **new_data, bool validate_checksum)
|
|
|
|
|
{
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < IP_HEADER_LEN)) {
|
|
|
|
|
return false;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
const struct ip_header *ip = data;
|
|
|
|
|
size_t ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (OVS_UNLIKELY(ip_len < IP_HEADER_LEN)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < ip_len)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (IP_IS_FRAGMENT(ip->ip_frag_off)) {
|
|
|
|
|
return false;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validate_checksum && csum(data, ip_len) != 0) {
|
2021-04-30 19:12:15 +02:00
|
|
|
|
COVERAGE_INC(conntrack_l3csum_err);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (new_data) {
|
|
|
|
|
*new_data = (char *) data + ip_len;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 13:15:08 -08:00
|
|
|
|
key->src.addr.ipv4 = get_16aligned_be32(&ip->ip_src);
|
|
|
|
|
key->dst.addr.ipv4 = get_16aligned_be32(&ip->ip_dst);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
key->nw_proto = ip->ip_proto;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
/* 'Data' is a pointer to the beginning of the L3 header and 'new_data' is
|
|
|
|
|
* used to store a pointer to the first byte after the L3 header. 'Size' is
|
|
|
|
|
* the size of the packet beyond the data pointer. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static inline bool
|
|
|
|
|
extract_l3_ipv6(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
const char **new_data)
|
|
|
|
|
{
|
|
|
|
|
const struct ovs_16aligned_ip6_hdr *ip6 = data;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
2018-06-28 23:39:47 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < sizeof *ip6)) {
|
|
|
|
|
return false;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = ip6 + 1;
|
|
|
|
|
size -= sizeof *ip6;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
uint8_t nw_proto = ip6->ip6_nxt;
|
|
|
|
|
uint8_t nw_frag = 0;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-02-13 15:34:15 -08:00
|
|
|
|
const struct ovs_16aligned_ip6_frag *frag_hdr;
|
|
|
|
|
if (!parse_ipv6_ext_hdrs(&data, &size, &nw_proto, &nw_frag, &frag_hdr)) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nw_frag) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:57 -08:00
|
|
|
|
if (new_data) {
|
|
|
|
|
*new_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 13:15:08 -08:00
|
|
|
|
memcpy(&key->src.addr.ipv6, &ip6->ip6_src, sizeof key->src.addr);
|
|
|
|
|
memcpy(&key->dst.addr.ipv6, &ip6->ip6_dst, sizeof key->dst.addr);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
key->nw_proto = nw_proto;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
|
checksum_valid(const struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
const void *l3)
|
|
|
|
|
{
|
2021-03-08 00:24:40 +01:00
|
|
|
|
bool valid;
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (key->dl_type == htons(ETH_TYPE_IP)) {
|
2019-02-22 17:17:42 -08:00
|
|
|
|
uint32_t csum = packet_csum_pseudoheader(l3);
|
2021-03-08 00:24:40 +01:00
|
|
|
|
valid = (csum_finish(csum_continue(csum, data, size)) == 0);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (key->dl_type == htons(ETH_TYPE_IPV6)) {
|
2021-03-08 00:24:40 +01:00
|
|
|
|
valid = (packet_csum_upperlayer6(l3, data, key->nw_proto, size) == 0);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else {
|
2021-03-08 00:24:40 +01:00
|
|
|
|
valid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!valid) {
|
2020-04-16 12:54:53 -07:00
|
|
|
|
COVERAGE_INC(conntrack_l4csum_err);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2021-03-08 00:24:40 +01:00
|
|
|
|
|
|
|
|
|
return valid;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
|
check_l4_tcp(const struct conn_key *key, const void *data, size_t size,
|
2017-08-01 18:51:14 -07:00
|
|
|
|
const void *l3, bool validate_checksum)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
const struct tcp_header *tcp = data;
|
2017-03-03 21:16:17 -08:00
|
|
|
|
if (size < sizeof *tcp) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-03-03 21:16:17 -08:00
|
|
|
|
size_t tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (OVS_UNLIKELY(tcp_len < TCP_HEADER_LEN || tcp_len > size)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return validate_checksum ? checksum_valid(key, data, size, l3) : true;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
|
check_l4_udp(const struct conn_key *key, const void *data, size_t size,
|
2017-08-01 18:51:14 -07:00
|
|
|
|
const void *l3, bool validate_checksum)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
const struct udp_header *udp = data;
|
2017-03-03 21:16:17 -08:00
|
|
|
|
if (size < sizeof *udp) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2017-03-03 21:16:17 -08:00
|
|
|
|
size_t udp_len = ntohs(udp->udp_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (OVS_UNLIKELY(udp_len < UDP_HEADER_LEN || udp_len > size)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Validation must be skipped if checksum is 0 on IPv4 packets */
|
|
|
|
|
return (udp->udp_csum == 0 && key->dl_type == htons(ETH_TYPE_IP))
|
2017-08-01 18:51:14 -07:00
|
|
|
|
|| (validate_checksum ? checksum_valid(key, data, size, l3) : true);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
2017-08-01 18:51:14 -07:00
|
|
|
|
check_l4_icmp(const void *data, size_t size, bool validate_checksum)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2020-04-16 12:54:53 -07:00
|
|
|
|
if (validate_checksum && csum(data, size) != 0) {
|
|
|
|
|
COVERAGE_INC(conntrack_l4csum_err);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
|
check_l4_icmp6(const struct conn_key *key, const void *data, size_t size,
|
2017-08-01 18:51:14 -07:00
|
|
|
|
const void *l3, bool validate_checksum)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return validate_checksum ? checksum_valid(key, data, size, l3) : true;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
2019-08-27 16:59:02 -07:00
|
|
|
|
extract_l4_tcp(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
size_t *chk_len)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-08-27 16:59:02 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < (chk_len ? *chk_len : TCP_HEADER_LEN))) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
const struct tcp_header *tcp = data;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
key->src.port = tcp->tcp_src;
|
|
|
|
|
key->dst.port = tcp->tcp_dst;
|
|
|
|
|
|
|
|
|
|
/* Port 0 is invalid */
|
|
|
|
|
return key->src.port && key->dst.port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool
|
2019-08-27 16:59:02 -07:00
|
|
|
|
extract_l4_udp(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
size_t *chk_len)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-08-27 16:59:02 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < (chk_len ? *chk_len : UDP_HEADER_LEN))) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
const struct udp_header *udp = data;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
key->src.port = udp->udp_src;
|
|
|
|
|
key->dst.port = udp->udp_dst;
|
|
|
|
|
|
|
|
|
|
/* Port 0 is invalid */
|
|
|
|
|
return key->src.port && key->dst.port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool extract_l4(struct conn_key *key, const void *data,
|
2017-08-01 18:51:14 -07:00
|
|
|
|
size_t size, bool *related, const void *l3,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
bool validate_checksum, size_t *chk_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2016-05-13 15:04:17 -07:00
|
|
|
|
static uint8_t
|
|
|
|
|
reverse_icmp_type(uint8_t type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
|
|
|
|
case ICMP4_ECHO_REQUEST:
|
|
|
|
|
return ICMP4_ECHO_REPLY;
|
|
|
|
|
case ICMP4_ECHO_REPLY:
|
|
|
|
|
return ICMP4_ECHO_REQUEST;
|
|
|
|
|
|
|
|
|
|
case ICMP4_TIMESTAMP:
|
|
|
|
|
return ICMP4_TIMESTAMPREPLY;
|
|
|
|
|
case ICMP4_TIMESTAMPREPLY:
|
|
|
|
|
return ICMP4_TIMESTAMP;
|
|
|
|
|
|
|
|
|
|
case ICMP4_INFOREQUEST:
|
|
|
|
|
return ICMP4_INFOREPLY;
|
|
|
|
|
case ICMP4_INFOREPLY:
|
|
|
|
|
return ICMP4_INFOREQUEST;
|
|
|
|
|
default:
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* If 'related' is not NULL and the function is processing an ICMP
|
|
|
|
|
* error packet, extract the l3 and l4 fields from the nested header
|
|
|
|
|
* instead and set *related to true. If 'related' is NULL we're
|
|
|
|
|
* already processing a nested header and no such recursion is
|
|
|
|
|
* possible */
|
|
|
|
|
static inline int
|
|
|
|
|
extract_l4_icmp(struct conn_key *key, const void *data, size_t size,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
bool *related, size_t *chk_len)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-08-27 16:59:02 -07:00
|
|
|
|
if (OVS_UNLIKELY(size < (chk_len ? *chk_len : ICMP_HEADER_LEN))) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
const struct icmp_header *icmp = data;
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
switch (icmp->icmp_type) {
|
|
|
|
|
case ICMP4_ECHO_REQUEST:
|
|
|
|
|
case ICMP4_ECHO_REPLY:
|
|
|
|
|
case ICMP4_TIMESTAMP:
|
|
|
|
|
case ICMP4_TIMESTAMPREPLY:
|
|
|
|
|
case ICMP4_INFOREQUEST:
|
|
|
|
|
case ICMP4_INFOREPLY:
|
2016-05-13 15:04:17 -07:00
|
|
|
|
if (icmp->icmp_code != 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* Separate ICMP connection: identified using id */
|
2016-05-13 15:04:17 -07:00
|
|
|
|
key->src.icmp_id = key->dst.icmp_id = icmp->icmp_fields.echo.id;
|
|
|
|
|
key->src.icmp_type = icmp->icmp_type;
|
|
|
|
|
key->dst.icmp_type = reverse_icmp_type(icmp->icmp_type);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
break;
|
|
|
|
|
case ICMP4_DST_UNREACH:
|
|
|
|
|
case ICMP4_TIME_EXCEEDED:
|
|
|
|
|
case ICMP4_PARAM_PROB:
|
|
|
|
|
case ICMP4_SOURCEQUENCH:
|
|
|
|
|
case ICMP4_REDIRECT: {
|
|
|
|
|
/* ICMP packet part of another connection. We should
|
|
|
|
|
* extract the key from embedded packet header */
|
|
|
|
|
struct conn_key inner_key;
|
|
|
|
|
const char *l3 = (const char *) (icmp + 1);
|
|
|
|
|
const char *tail = (const char *) data + size;
|
|
|
|
|
const char *l4;
|
|
|
|
|
|
|
|
|
|
if (!related) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&inner_key, 0, sizeof inner_key);
|
|
|
|
|
inner_key.dl_type = htons(ETH_TYPE_IP);
|
2018-01-09 15:44:56 -08:00
|
|
|
|
bool ok = extract_l3_ipv4(&inner_key, l3, tail - l3, &l4, false);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (!ok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 13:15:08 -08:00
|
|
|
|
if (inner_key.src.addr.ipv4 != key->dst.addr.ipv4) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key->src = inner_key.src;
|
|
|
|
|
key->dst = inner_key.dst;
|
|
|
|
|
key->nw_proto = inner_key.nw_proto;
|
2019-08-27 16:59:02 -07:00
|
|
|
|
size_t check_len = ICMP_ERROR_DATA_L4_LEN;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2019-08-27 16:59:02 -07:00
|
|
|
|
ok = extract_l4(key, l4, tail - l4, NULL, l3, false, &check_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (ok) {
|
|
|
|
|
conn_key_reverse(key);
|
|
|
|
|
*related = true;
|
|
|
|
|
}
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 15:04:17 -07:00
|
|
|
|
static uint8_t
|
|
|
|
|
reverse_icmp6_type(uint8_t type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
|
|
|
|
case ICMP6_ECHO_REQUEST:
|
|
|
|
|
return ICMP6_ECHO_REPLY;
|
|
|
|
|
case ICMP6_ECHO_REPLY:
|
|
|
|
|
return ICMP6_ECHO_REQUEST;
|
|
|
|
|
default:
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* If 'related' is not NULL and the function is processing an ICMP
|
|
|
|
|
* error packet, extract the l3 and l4 fields from the nested header
|
|
|
|
|
* instead and set *related to true. If 'related' is NULL we're
|
|
|
|
|
* already processing a nested header and no such recursion is
|
|
|
|
|
* possible */
|
|
|
|
|
static inline bool
|
|
|
|
|
extract_l4_icmp6(struct conn_key *key, const void *data, size_t size,
|
|
|
|
|
bool *related)
|
|
|
|
|
{
|
|
|
|
|
const struct icmp6_header *icmp6 = data;
|
|
|
|
|
|
|
|
|
|
/* All the messages that we support need at least 4 bytes after
|
|
|
|
|
* the header */
|
|
|
|
|
if (size < sizeof *icmp6 + 4) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (icmp6->icmp6_type) {
|
|
|
|
|
case ICMP6_ECHO_REQUEST:
|
|
|
|
|
case ICMP6_ECHO_REPLY:
|
2016-05-13 15:04:17 -07:00
|
|
|
|
if (icmp6->icmp6_code != 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
/* Separate ICMP connection: identified using id */
|
2016-05-13 15:04:17 -07:00
|
|
|
|
key->src.icmp_id = key->dst.icmp_id = *(ovs_be16 *) (icmp6 + 1);
|
|
|
|
|
key->src.icmp_type = icmp6->icmp6_type;
|
|
|
|
|
key->dst.icmp_type = reverse_icmp6_type(icmp6->icmp6_type);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
break;
|
|
|
|
|
case ICMP6_DST_UNREACH:
|
|
|
|
|
case ICMP6_PACKET_TOO_BIG:
|
|
|
|
|
case ICMP6_TIME_EXCEEDED:
|
|
|
|
|
case ICMP6_PARAM_PROB: {
|
|
|
|
|
/* ICMP packet part of another connection. We should
|
|
|
|
|
* extract the key from embedded packet header */
|
|
|
|
|
struct conn_key inner_key;
|
|
|
|
|
const char *l3 = (const char *) icmp6 + 8;
|
|
|
|
|
const char *tail = (const char *) data + size;
|
|
|
|
|
const char *l4 = NULL;
|
|
|
|
|
|
|
|
|
|
if (!related) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(&inner_key, 0, sizeof inner_key);
|
|
|
|
|
inner_key.dl_type = htons(ETH_TYPE_IPV6);
|
2018-01-09 15:44:56 -08:00
|
|
|
|
bool ok = extract_l3_ipv6(&inner_key, l3, tail - l3, &l4);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (!ok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* pf doesn't do this, but it seems a good idea */
|
2019-02-14 13:15:08 -08:00
|
|
|
|
if (!ipv6_addr_equals(&inner_key.src.addr.ipv6,
|
|
|
|
|
&key->dst.addr.ipv6)) {
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key->src = inner_key.src;
|
|
|
|
|
key->dst = inner_key.dst;
|
|
|
|
|
key->nw_proto = inner_key.nw_proto;
|
|
|
|
|
|
2019-08-27 16:59:02 -07:00
|
|
|
|
ok = extract_l4(key, l4, tail - l4, NULL, l3, false, NULL);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (ok) {
|
|
|
|
|
conn_key_reverse(key);
|
|
|
|
|
*related = true;
|
|
|
|
|
}
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Extract l4 fields into 'key', which must already contain valid l3
|
|
|
|
|
* members.
|
|
|
|
|
*
|
|
|
|
|
* If 'related' is not NULL and an ICMP error packet is being
|
|
|
|
|
* processed, the function will extract the key from the packet nested
|
2017-06-23 13:28:20 +01:00
|
|
|
|
* in the ICMP payload and set '*related' to true.
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
2019-02-04 16:23:07 -08:00
|
|
|
|
* 'size' here is the layer 4 size, which can be a nested size if parsing
|
|
|
|
|
* an ICMP or ICMP6 header.
|
|
|
|
|
*
|
2015-11-15 22:07:25 -08:00
|
|
|
|
* If 'related' is NULL, it means that we're already parsing a header nested
|
2019-08-27 16:59:02 -07:00
|
|
|
|
* in an ICMP error. In this case, we skip the checksum and some length
|
|
|
|
|
* validations. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static inline bool
|
|
|
|
|
extract_l4(struct conn_key *key, const void *data, size_t size, bool *related,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
const void *l3, bool validate_checksum, size_t *chk_len)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
if (key->nw_proto == IPPROTO_TCP) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return (!related || check_l4_tcp(key, data, size, l3,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
validate_checksum))
|
|
|
|
|
&& extract_l4_tcp(key, data, size, chk_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (key->nw_proto == IPPROTO_UDP) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return (!related || check_l4_udp(key, data, size, l3,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
validate_checksum))
|
|
|
|
|
&& extract_l4_udp(key, data, size, chk_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (key->dl_type == htons(ETH_TYPE_IP)
|
|
|
|
|
&& key->nw_proto == IPPROTO_ICMP) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return (!related || check_l4_icmp(data, size, validate_checksum))
|
2019-08-27 16:59:02 -07:00
|
|
|
|
&& extract_l4_icmp(key, data, size, related, chk_len);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (key->dl_type == htons(ETH_TYPE_IPV6)
|
|
|
|
|
&& key->nw_proto == IPPROTO_ICMPV6) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
return (!related || check_l4_icmp6(key, data, size, l3,
|
2019-08-27 16:59:02 -07:00
|
|
|
|
validate_checksum))
|
|
|
|
|
&& extract_l4_icmp6(key, data, size, related);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2020-10-12 07:38:38 -04:00
|
|
|
|
|
|
|
|
|
/* For all other protocols we do not have L4 keys, so keep them zero. */
|
|
|
|
|
return true;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2016-05-25 18:10:09 -07:00
|
|
|
|
conn_key_extract(struct conntrack *ct, struct dp_packet *pkt, ovs_be16 dl_type,
|
2015-11-15 22:07:25 -08:00
|
|
|
|
struct conn_lookup_ctx *ctx, uint16_t zone)
|
|
|
|
|
{
|
2017-04-25 16:29:59 +00:00
|
|
|
|
const struct eth_header *l2 = dp_packet_eth(pkt);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
const struct ip_header *l3 = dp_packet_l3(pkt);
|
|
|
|
|
const char *l4 = dp_packet_l4(pkt);
|
|
|
|
|
|
|
|
|
|
memset(ctx, 0, sizeof *ctx);
|
|
|
|
|
|
|
|
|
|
if (!l2 || !l3 || !l4) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->key.zone = zone;
|
|
|
|
|
|
|
|
|
|
/* XXX In this function we parse the packet (again, it has already
|
|
|
|
|
* gone through miniflow_extract()) for two reasons:
|
|
|
|
|
*
|
|
|
|
|
* 1) To extract the l3 addresses and l4 ports.
|
|
|
|
|
* We already have the l3 and l4 headers' pointers. Extracting
|
|
|
|
|
* the l3 addresses and the l4 ports is really cheap, since they
|
|
|
|
|
* can be found at fixed locations.
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* 2) To extract the l4 type.
|
|
|
|
|
* Extracting the l4 types, for IPv6 can be quite expensive, because
|
|
|
|
|
* it's not at a fixed location.
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
|
|
|
|
* Here's a way to avoid (2) with the help of the datapath.
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* The datapath doesn't keep the packet's extracted flow[1], so
|
2015-11-15 22:07:25 -08:00
|
|
|
|
* using that is not an option. We could use the packet's matching
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* megaflow, but we have to make sure that the l4 type (nw_proto)
|
|
|
|
|
* is unwildcarded. This means either:
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* a) dpif-netdev unwildcards the l4 type when a new flow is installed
|
|
|
|
|
* if the actions contains ct().
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* b) ofproto-dpif-xlate unwildcards the l4 type when translating a ct()
|
|
|
|
|
* action. This is already done in different actions, but it's
|
|
|
|
|
* unnecessary for the kernel.
|
2015-11-15 22:07:25 -08:00
|
|
|
|
*
|
|
|
|
|
* ---
|
2016-05-25 18:10:09 -07:00
|
|
|
|
* [1] The reasons for this are that keeping the flow increases
|
2015-11-15 22:07:25 -08:00
|
|
|
|
* (slightly) the cache footprint and increases computation
|
|
|
|
|
* time as we move the packet around. Most importantly, the flow
|
|
|
|
|
* should be updated by the actions and this can be slow, as
|
|
|
|
|
* we use a sparse representation (miniflow).
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-01-09 15:44:56 -08:00
|
|
|
|
bool ok;
|
2016-05-25 18:10:09 -07:00
|
|
|
|
ctx->key.dl_type = dl_type;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (ctx->key.dl_type == htons(ETH_TYPE_IP)) {
|
2018-01-09 15:44:56 -08:00
|
|
|
|
bool hwol_bad_l3_csum = dp_packet_ip_checksum_bad(pkt);
|
2017-08-01 18:51:14 -07:00
|
|
|
|
if (hwol_bad_l3_csum) {
|
|
|
|
|
ok = false;
|
2021-04-30 19:12:15 +02:00
|
|
|
|
COVERAGE_INC(conntrack_l3csum_err);
|
2017-08-01 18:51:14 -07:00
|
|
|
|
} else {
|
2020-01-17 18:47:55 -03:00
|
|
|
|
bool hwol_good_l3_csum = dp_packet_ip_checksum_valid(pkt)
|
|
|
|
|
|| dp_packet_hwol_is_ipv4(pkt);
|
2017-08-01 18:51:14 -07:00
|
|
|
|
/* Validate the checksum only when hwol is not supported. */
|
2019-02-04 16:23:07 -08:00
|
|
|
|
ok = extract_l3_ipv4(&ctx->key, l3, dp_packet_l3_size(pkt), NULL,
|
2017-08-01 18:51:14 -07:00
|
|
|
|
!hwol_good_l3_csum);
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) {
|
2019-02-04 16:23:07 -08:00
|
|
|
|
ok = extract_l3_ipv6(&ctx->key, l3, dp_packet_l3_size(pkt), NULL);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else {
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ok) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
bool hwol_bad_l4_csum = dp_packet_l4_checksum_bad(pkt);
|
|
|
|
|
if (!hwol_bad_l4_csum) {
|
2020-01-17 18:47:55 -03:00
|
|
|
|
bool hwol_good_l4_csum = dp_packet_l4_checksum_valid(pkt)
|
|
|
|
|
|| dp_packet_hwol_tx_l4_checksum(pkt);
|
2017-08-01 18:51:14 -07:00
|
|
|
|
/* Validate the checksum only when hwol is not supported. */
|
2019-02-04 16:23:07 -08:00
|
|
|
|
if (extract_l4(&ctx->key, l4, dp_packet_l4_size(pkt),
|
2019-08-27 16:59:02 -07:00
|
|
|
|
&ctx->icmp_related, l3, !hwol_good_l4_csum,
|
|
|
|
|
NULL)) {
|
2017-08-01 18:51:14 -07:00
|
|
|
|
ctx->hash = conn_key_hash(&ctx->key, ct->hash_basis);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-03-08 00:24:40 +01:00
|
|
|
|
} else {
|
|
|
|
|
COVERAGE_INC(conntrack_l4csum_err);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-06-09 15:30:43 -07:00
|
|
|
|
|
|
|
|
|
static uint32_t
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ct_addr_hash_add(uint32_t hash, const union ct_addr *addr)
|
2017-06-09 15:30:43 -07:00
|
|
|
|
{
|
|
|
|
|
BUILD_ASSERT_DECL(sizeof *addr % 4 == 0);
|
|
|
|
|
return hash_add_bytes32(hash, (const uint32_t *) addr, sizeof *addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
|
ct_endpoint_hash_add(uint32_t hash, const struct ct_endpoint *ep)
|
|
|
|
|
{
|
|
|
|
|
BUILD_ASSERT_DECL(sizeof *ep % 4 == 0);
|
|
|
|
|
return hash_add_bytes32(hash, (const uint32_t *) ep, sizeof *ep);
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
/* Symmetric */
|
|
|
|
|
static uint32_t
|
|
|
|
|
conn_key_hash(const struct conn_key *key, uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
uint32_t hsrc, hdst, hash;
|
|
|
|
|
hsrc = hdst = basis;
|
2017-06-09 15:30:44 -07:00
|
|
|
|
hsrc = ct_endpoint_hash_add(hsrc, &key->src);
|
|
|
|
|
hdst = ct_endpoint_hash_add(hdst, &key->dst);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
/* Even if source and destination are swapped the hash will be the same. */
|
|
|
|
|
hash = hsrc ^ hdst;
|
|
|
|
|
|
|
|
|
|
/* Hash the rest of the key(L3 and L4 types and zone). */
|
2019-02-14 13:15:07 -08:00
|
|
|
|
return hash_words((uint32_t *) (&key->dst + 1),
|
2015-11-15 22:07:25 -08:00
|
|
|
|
(uint32_t *) (key + 1) - (uint32_t *) (&key->dst + 1),
|
|
|
|
|
hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
conn_key_reverse(struct conn_key *key)
|
|
|
|
|
{
|
2018-01-09 15:44:56 -08:00
|
|
|
|
struct ct_endpoint tmp = key->src;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
key->src = key->dst;
|
|
|
|
|
key->dst = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
static uint32_t
|
2019-02-14 13:15:08 -08:00
|
|
|
|
nat_ipv6_addrs_delta(struct in6_addr *ipv6_min, struct in6_addr *ipv6_max)
|
2017-05-30 10:49:27 -07:00
|
|
|
|
{
|
2019-02-14 13:15:08 -08:00
|
|
|
|
uint8_t *ipv6_min_hi = &ipv6_min->s6_addr[0];
|
|
|
|
|
uint8_t *ipv6_min_lo = &ipv6_min->s6_addr[0] + sizeof(uint64_t);
|
|
|
|
|
uint8_t *ipv6_max_hi = &ipv6_max->s6_addr[0];
|
|
|
|
|
uint8_t *ipv6_max_lo = &ipv6_max->s6_addr[0] + sizeof(uint64_t);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
|
|
|
|
ovs_be64 addr6_64_min_hi;
|
|
|
|
|
ovs_be64 addr6_64_min_lo;
|
|
|
|
|
memcpy(&addr6_64_min_hi, ipv6_min_hi, sizeof addr6_64_min_hi);
|
|
|
|
|
memcpy(&addr6_64_min_lo, ipv6_min_lo, sizeof addr6_64_min_lo);
|
|
|
|
|
|
|
|
|
|
ovs_be64 addr6_64_max_hi;
|
|
|
|
|
ovs_be64 addr6_64_max_lo;
|
|
|
|
|
memcpy(&addr6_64_max_hi, ipv6_max_hi, sizeof addr6_64_max_hi);
|
|
|
|
|
memcpy(&addr6_64_max_lo, ipv6_max_lo, sizeof addr6_64_max_lo);
|
|
|
|
|
|
|
|
|
|
uint64_t diff;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (addr6_64_min_hi == addr6_64_max_hi &&
|
|
|
|
|
ntohll(addr6_64_min_lo) <= ntohll(addr6_64_max_lo)) {
|
|
|
|
|
diff = ntohll(addr6_64_max_lo) - ntohll(addr6_64_min_lo);
|
|
|
|
|
} else if (ntohll(addr6_64_min_hi) + 1 == ntohll(addr6_64_max_hi) &&
|
|
|
|
|
ntohll(addr6_64_min_lo) > ntohll(addr6_64_max_lo)) {
|
|
|
|
|
diff = UINT64_MAX - (ntohll(addr6_64_min_lo) -
|
|
|
|
|
ntohll(addr6_64_max_lo) - 1);
|
|
|
|
|
} else {
|
|
|
|
|
/* Limit address delta supported to 32 bits or 4 billion approximately.
|
|
|
|
|
* Possibly, this should be visible to the user through a datapath
|
|
|
|
|
* support check, however the practical impact is probably nil. */
|
|
|
|
|
diff = 0xfffffffe;
|
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (diff > 0xfffffffe) {
|
|
|
|
|
diff = 0xfffffffe;
|
|
|
|
|
}
|
|
|
|
|
return diff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function must be used in tandem with nat_ipv6_addrs_delta(), which
|
|
|
|
|
* restricts the input parameters. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
2019-02-14 13:15:08 -08:00
|
|
|
|
nat_ipv6_addr_increment(struct in6_addr *ipv6, uint32_t increment)
|
2017-05-30 10:49:27 -07:00
|
|
|
|
{
|
2019-02-14 13:15:08 -08:00
|
|
|
|
uint8_t *ipv6_hi = &ipv6->s6_addr[0];
|
|
|
|
|
uint8_t *ipv6_lo = &ipv6->s6_addr[0] + sizeof(ovs_be64);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
ovs_be64 addr6_64_hi;
|
|
|
|
|
ovs_be64 addr6_64_lo;
|
|
|
|
|
memcpy(&addr6_64_hi, ipv6_hi, sizeof addr6_64_hi);
|
|
|
|
|
memcpy(&addr6_64_lo, ipv6_lo, sizeof addr6_64_lo);
|
|
|
|
|
|
|
|
|
|
if (UINT64_MAX - increment >= ntohll(addr6_64_lo)) {
|
|
|
|
|
addr6_64_lo = htonll(increment + ntohll(addr6_64_lo));
|
|
|
|
|
} else if (addr6_64_hi != OVS_BE64_MAX) {
|
|
|
|
|
addr6_64_hi = htonll(1 + ntohll(addr6_64_hi));
|
|
|
|
|
addr6_64_lo = htonll(increment - (UINT64_MAX -
|
|
|
|
|
ntohll(addr6_64_lo) + 1));
|
|
|
|
|
} else {
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(ipv6_hi, &addr6_64_hi, sizeof addr6_64_hi);
|
|
|
|
|
memcpy(ipv6_lo, &addr6_64_lo, sizeof addr6_64_lo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
|
nat_range_hash(const struct conn *conn, uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
uint32_t hash = basis;
|
|
|
|
|
|
2017-06-09 15:30:43 -07:00
|
|
|
|
hash = ct_addr_hash_add(hash, &conn->nat_info->min_addr);
|
|
|
|
|
hash = ct_addr_hash_add(hash, &conn->nat_info->max_addr);
|
|
|
|
|
hash = hash_add(hash,
|
|
|
|
|
(conn->nat_info->max_port << 16)
|
|
|
|
|
| conn->nat_info->min_port);
|
|
|
|
|
hash = ct_endpoint_hash_add(hash, &conn->key.src);
|
|
|
|
|
hash = ct_endpoint_hash_add(hash, &conn->key.dst);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
hash = hash_add(hash, (OVS_FORCE uint32_t) conn->key.dl_type);
|
|
|
|
|
hash = hash_add(hash, conn->key.nw_proto);
|
|
|
|
|
hash = hash_add(hash, conn->key.zone);
|
2017-06-09 15:30:43 -07:00
|
|
|
|
|
|
|
|
|
/* The purpose of the second parameter is to distinguish hashes of data of
|
|
|
|
|
* different length; our data always has the same length so there is no
|
|
|
|
|
* value in counting. */
|
|
|
|
|
return hash_finish(hash, 0);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
nat_select_range_tuple(struct conntrack *ct, const struct conn *conn,
|
|
|
|
|
struct conn *nat_conn)
|
|
|
|
|
{
|
2017-08-06 10:51:14 -07:00
|
|
|
|
enum { MIN_NAT_EPHEMERAL_PORT = 1024,
|
|
|
|
|
MAX_NAT_EPHEMERAL_PORT = 65535 };
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
|
|
|
|
uint16_t min_port;
|
|
|
|
|
uint16_t max_port;
|
|
|
|
|
uint16_t first_port;
|
|
|
|
|
uint32_t hash = nat_range_hash(conn, ct->hash_basis);
|
|
|
|
|
|
|
|
|
|
if ((conn->nat_info->nat_action & NAT_ACTION_SRC) &&
|
|
|
|
|
(!(conn->nat_info->nat_action & NAT_ACTION_SRC_PORT))) {
|
|
|
|
|
min_port = ntohs(conn->key.src.port);
|
|
|
|
|
max_port = ntohs(conn->key.src.port);
|
|
|
|
|
first_port = min_port;
|
|
|
|
|
} else if ((conn->nat_info->nat_action & NAT_ACTION_DST) &&
|
|
|
|
|
(!(conn->nat_info->nat_action & NAT_ACTION_DST_PORT))) {
|
|
|
|
|
min_port = ntohs(conn->key.dst.port);
|
|
|
|
|
max_port = ntohs(conn->key.dst.port);
|
|
|
|
|
first_port = min_port;
|
|
|
|
|
} else {
|
|
|
|
|
uint16_t deltap = conn->nat_info->max_port - conn->nat_info->min_port;
|
|
|
|
|
uint32_t port_index = hash % (deltap + 1);
|
|
|
|
|
first_port = conn->nat_info->min_port + port_index;
|
|
|
|
|
min_port = conn->nat_info->min_port;
|
|
|
|
|
max_port = conn->nat_info->max_port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t deltaa = 0;
|
|
|
|
|
uint32_t address_index;
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr ct_addr;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
memset(&ct_addr, 0, sizeof ct_addr);
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr max_ct_addr;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
memset(&max_ct_addr, 0, sizeof max_ct_addr);
|
|
|
|
|
max_ct_addr = conn->nat_info->max_addr;
|
|
|
|
|
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
deltaa = ntohl(conn->nat_info->max_addr.ipv4) -
|
|
|
|
|
ntohl(conn->nat_info->min_addr.ipv4);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
address_index = hash % (deltaa + 1);
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ct_addr.ipv4 = htonl(
|
|
|
|
|
ntohl(conn->nat_info->min_addr.ipv4) + address_index);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
deltaa = nat_ipv6_addrs_delta(&conn->nat_info->min_addr.ipv6,
|
|
|
|
|
&conn->nat_info->max_addr.ipv6);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
/* deltaa must be within 32 bits for full hash coverage. A 64 or
|
|
|
|
|
* 128 bit hash is unnecessary and hence not used here. Most code
|
|
|
|
|
* is kept common with V4; nat_ipv6_addrs_delta() will do the
|
|
|
|
|
* enforcement via max_ct_addr. */
|
|
|
|
|
max_ct_addr = conn->nat_info->min_addr;
|
2019-02-14 13:15:08 -08:00
|
|
|
|
nat_ipv6_addr_increment(&max_ct_addr.ipv6, deltaa);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
address_index = hash % (deltaa + 1);
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ct_addr.ipv6 = conn->nat_info->min_addr.ipv6;
|
|
|
|
|
nat_ipv6_addr_increment(&ct_addr.ipv6, address_index);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t port = first_port;
|
|
|
|
|
bool all_ports_tried = false;
|
2019-02-25 15:36:32 -08:00
|
|
|
|
/* For DNAT or for specified port ranges, we don't use ephemeral ports. */
|
|
|
|
|
bool ephemeral_ports_tried
|
|
|
|
|
= conn->nat_info->nat_action & NAT_ACTION_DST ||
|
|
|
|
|
conn->nat_info->nat_action & NAT_ACTION_SRC_PORT
|
|
|
|
|
? true : false;
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr first_addr = ct_addr;
|
2020-10-12 07:38:38 -04:00
|
|
|
|
bool pat_enabled = conn->key.nw_proto == IPPROTO_TCP ||
|
|
|
|
|
conn->key.nw_proto == IPPROTO_UDP;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
if (conn->nat_info->nat_action & NAT_ACTION_SRC) {
|
|
|
|
|
nat_conn->rev_key.dst.addr = ct_addr;
|
2019-06-05 15:35:34 -07:00
|
|
|
|
if (pat_enabled) {
|
|
|
|
|
nat_conn->rev_key.dst.port = htons(port);
|
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
2019-02-25 16:37:50 -08:00
|
|
|
|
nat_conn->rev_key.src.addr = ct_addr;
|
2019-06-05 15:35:34 -07:00
|
|
|
|
if (pat_enabled) {
|
|
|
|
|
nat_conn->rev_key.src.port = htons(port);
|
|
|
|
|
}
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 15:35:34 -07:00
|
|
|
|
bool found = conn_lookup(ct, &nat_conn->rev_key, time_msec(), NULL,
|
|
|
|
|
NULL);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
if (!found) {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
return true;
|
2019-02-25 15:36:31 -08:00
|
|
|
|
} else if (pat_enabled && !all_ports_tried) {
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (min_port == max_port) {
|
|
|
|
|
all_ports_tried = true;
|
|
|
|
|
} else if (port == max_port) {
|
|
|
|
|
port = min_port;
|
|
|
|
|
} else {
|
|
|
|
|
port++;
|
|
|
|
|
}
|
|
|
|
|
if (port == first_port) {
|
|
|
|
|
all_ports_tried = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (memcmp(&ct_addr, &max_ct_addr, sizeof ct_addr)) {
|
|
|
|
|
if (conn->key.dl_type == htons(ETH_TYPE_IP)) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ct_addr.ipv4 = htonl(ntohl(ct_addr.ipv4) + 1);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
} else {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
nat_ipv6_addr_increment(&ct_addr.ipv6, 1);
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ct_addr = conn->nat_info->min_addr;
|
|
|
|
|
}
|
|
|
|
|
if (!memcmp(&ct_addr, &first_addr, sizeof ct_addr)) {
|
2019-02-25 15:36:31 -08:00
|
|
|
|
if (pat_enabled && !ephemeral_ports_tried) {
|
2018-12-17 14:43:11 -08:00
|
|
|
|
ephemeral_ports_tried = true;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
ct_addr = conn->nat_info->min_addr;
|
2018-12-17 14:43:12 -08:00
|
|
|
|
first_addr = ct_addr;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
min_port = MIN_NAT_EPHEMERAL_PORT;
|
|
|
|
|
max_port = MAX_NAT_EPHEMERAL_PORT;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
first_port = min_port;
|
|
|
|
|
port = first_port;
|
|
|
|
|
all_ports_tried = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static enum ct_update_res
|
2019-05-09 08:15:07 -07:00
|
|
|
|
conn_update(struct conntrack *ct, struct conn *conn, struct dp_packet *pkt,
|
|
|
|
|
struct conn_lookup_ctx *ctx, long long now)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
enum ct_update_res update_res =
|
|
|
|
|
l4_protos[conn->key.nw_proto]->conn_update(ct, conn, pkt, ctx->reply,
|
|
|
|
|
now);
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
return update_res;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
conn_expired(struct conn *conn, long long now)
|
|
|
|
|
{
|
2017-05-30 10:49:27 -07:00
|
|
|
|
if (conn->conn_type == CT_CONN_TYPE_DEFAULT) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
bool expired = now >= conn->expiration ? true : false;
|
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
|
|
|
|
return expired;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
valid_new(struct dp_packet *pkt, struct conn_key *key)
|
|
|
|
|
{
|
|
|
|
|
return l4_protos[key->nw_proto]->valid_new(pkt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct conn *
|
2019-05-09 08:15:07 -07:00
|
|
|
|
new_conn(struct conntrack *ct, struct dp_packet *pkt, struct conn_key *key,
|
2020-04-29 12:25:11 -07:00
|
|
|
|
long long now, uint32_t tp_id)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2020-04-29 12:25:11 -07:00
|
|
|
|
return l4_protos[key->nw_proto]->new_conn(ct, pkt, now, tp_id);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2019-05-09 08:15:07 -07:00
|
|
|
|
delete_conn_cmn(struct conn *conn)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
2017-05-30 10:49:27 -07:00
|
|
|
|
free(conn->nat_info);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
free(conn->alg);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
free(conn);
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
delete_conn(struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
ovs_assert(conn->conn_type == CT_CONN_TYPE_DEFAULT);
|
|
|
|
|
ovs_mutex_destroy(&conn->lock);
|
|
|
|
|
free(conn->nat_conn);
|
|
|
|
|
delete_conn_cmn(conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Only used by conn_clean_one(). */
|
|
|
|
|
static void
|
|
|
|
|
delete_conn_one(struct conn *conn)
|
|
|
|
|
{
|
|
|
|
|
if (conn->conn_type == CT_CONN_TYPE_DEFAULT) {
|
|
|
|
|
ovs_mutex_destroy(&conn->lock);
|
|
|
|
|
}
|
|
|
|
|
delete_conn_cmn(conn);
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-02-12 14:02:37 -08:00
|
|
|
|
/* Convert a conntrack address 'a' into an IP address 'b' based on 'dl_type'.
|
|
|
|
|
*
|
|
|
|
|
* Note that 'dl_type' should be either "ETH_TYPE_IP" or "ETH_TYPE_IPv6"
|
|
|
|
|
* in network-byte order. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ct_endpoint_to_ct_dpif_inet_addr(const union ct_addr *a,
|
2015-11-15 22:07:25 -08:00
|
|
|
|
union ct_dpif_inet_addr *b,
|
|
|
|
|
ovs_be16 dl_type)
|
|
|
|
|
{
|
|
|
|
|
if (dl_type == htons(ETH_TYPE_IP)) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
b->ip = a->ipv4;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else if (dl_type == htons(ETH_TYPE_IPV6)){
|
2019-02-14 13:15:08 -08:00
|
|
|
|
b->in6 = a->ipv6;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 14:02:37 -08:00
|
|
|
|
/* Convert an IP address 'a' into a conntrack address 'b' based on 'dl_type'.
|
|
|
|
|
*
|
|
|
|
|
* Note that 'dl_type' should be either "ETH_TYPE_IP" or "ETH_TYPE_IPv6"
|
|
|
|
|
* in network-byte order. */
|
|
|
|
|
static void
|
|
|
|
|
ct_dpif_inet_addr_to_ct_endpoint(const union ct_dpif_inet_addr *a,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr *b, ovs_be16 dl_type)
|
2018-02-12 14:02:37 -08:00
|
|
|
|
{
|
|
|
|
|
if (dl_type == htons(ETH_TYPE_IP)) {
|
2019-02-14 13:15:08 -08:00
|
|
|
|
b->ipv4 = a->ip;
|
2018-02-12 14:02:37 -08:00
|
|
|
|
} else if (dl_type == htons(ETH_TYPE_IPV6)){
|
2019-02-14 13:15:08 -08:00
|
|
|
|
b->ipv6 = a->in6;
|
2018-02-12 14:02:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
|
|
|
|
conn_key_to_tuple(const struct conn_key *key, struct ct_dpif_tuple *tuple)
|
|
|
|
|
{
|
|
|
|
|
if (key->dl_type == htons(ETH_TYPE_IP)) {
|
|
|
|
|
tuple->l3_type = AF_INET;
|
|
|
|
|
} else if (key->dl_type == htons(ETH_TYPE_IPV6)) {
|
|
|
|
|
tuple->l3_type = AF_INET6;
|
|
|
|
|
}
|
|
|
|
|
tuple->ip_proto = key->nw_proto;
|
|
|
|
|
ct_endpoint_to_ct_dpif_inet_addr(&key->src.addr, &tuple->src,
|
|
|
|
|
key->dl_type);
|
|
|
|
|
ct_endpoint_to_ct_dpif_inet_addr(&key->dst.addr, &tuple->dst,
|
|
|
|
|
key->dl_type);
|
|
|
|
|
|
|
|
|
|
if (key->nw_proto == IPPROTO_ICMP || key->nw_proto == IPPROTO_ICMPV6) {
|
2016-05-13 15:04:17 -07:00
|
|
|
|
tuple->icmp_id = key->src.icmp_id;
|
|
|
|
|
tuple->icmp_type = key->src.icmp_type;
|
|
|
|
|
tuple->icmp_code = key->src.icmp_code;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
} else {
|
|
|
|
|
tuple->src_port = key->src.port;
|
|
|
|
|
tuple->dst_port = key->dst.port;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 14:02:37 -08:00
|
|
|
|
static void
|
|
|
|
|
tuple_to_conn_key(const struct ct_dpif_tuple *tuple, uint16_t zone,
|
|
|
|
|
struct conn_key *key)
|
|
|
|
|
{
|
|
|
|
|
if (tuple->l3_type == AF_INET) {
|
|
|
|
|
key->dl_type = htons(ETH_TYPE_IP);
|
|
|
|
|
} else if (tuple->l3_type == AF_INET6) {
|
|
|
|
|
key->dl_type = htons(ETH_TYPE_IPV6);
|
|
|
|
|
}
|
|
|
|
|
key->nw_proto = tuple->ip_proto;
|
|
|
|
|
ct_dpif_inet_addr_to_ct_endpoint(&tuple->src, &key->src.addr,
|
|
|
|
|
key->dl_type);
|
|
|
|
|
ct_dpif_inet_addr_to_ct_endpoint(&tuple->dst, &key->dst.addr,
|
|
|
|
|
key->dl_type);
|
|
|
|
|
|
|
|
|
|
if (tuple->ip_proto == IPPROTO_ICMP || tuple->ip_proto == IPPROTO_ICMPV6) {
|
|
|
|
|
key->src.icmp_id = tuple->icmp_id;
|
|
|
|
|
key->src.icmp_type = tuple->icmp_type;
|
|
|
|
|
key->src.icmp_code = tuple->icmp_code;
|
|
|
|
|
key->dst.icmp_id = tuple->icmp_id;
|
|
|
|
|
key->dst.icmp_type = reverse_icmp_type(tuple->icmp_type);
|
|
|
|
|
key->dst.icmp_code = tuple->icmp_code;
|
|
|
|
|
} else {
|
|
|
|
|
key->src.port = tuple->src_port;
|
|
|
|
|
key->dst.port = tuple->dst_port;
|
|
|
|
|
}
|
|
|
|
|
key->zone = zone;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
static void
|
|
|
|
|
conn_to_ct_dpif_entry(const struct conn *conn, struct ct_dpif_entry *entry,
|
2019-05-20 21:50:28 -07:00
|
|
|
|
long long now)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
memset(entry, 0, sizeof *entry);
|
|
|
|
|
conn_key_to_tuple(&conn->key, &entry->tuple_orig);
|
|
|
|
|
conn_key_to_tuple(&conn->rev_key, &entry->tuple_reply);
|
|
|
|
|
|
|
|
|
|
entry->zone = conn->key.zone;
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&conn->lock);
|
|
|
|
|
entry->mark = conn->mark;
|
2017-05-30 10:49:27 -07:00
|
|
|
|
memcpy(&entry->labels, &conn->label, sizeof entry->labels);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
long long expiration = conn->expiration - now;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
struct ct_l4_proto *class = l4_protos[conn->key.nw_proto];
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (class->conn_get_protoinfo) {
|
|
|
|
|
class->conn_get_protoinfo(conn, &entry->protoinfo);
|
|
|
|
|
}
|
2019-05-20 21:50:28 -07:00
|
|
|
|
ovs_mutex_unlock(&conn->lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2019-05-20 21:50:28 -07:00
|
|
|
|
entry->timeout = (expiration > 0) ? expiration / 1000 : 0;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
|
|
|
|
if (conn->alg) {
|
|
|
|
|
/* Caller is responsible for freeing. */
|
|
|
|
|
entry->helper.name = xstrdup(conn->alg);
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 15:34:21 -08:00
|
|
|
|
struct ipf *
|
|
|
|
|
conntrack_ipf_ctx(struct conntrack *ct)
|
|
|
|
|
{
|
|
|
|
|
return ct->ipf;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
int
|
|
|
|
|
conntrack_dump_start(struct conntrack *ct, struct conntrack_dump *dump,
|
2017-08-01 20:12:03 -07:00
|
|
|
|
const uint16_t *pzone, int *ptot_bkts)
|
2015-11-15 22:07:25 -08:00
|
|
|
|
{
|
|
|
|
|
memset(dump, 0, sizeof(*dump));
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
if (pzone) {
|
|
|
|
|
dump->zone = *pzone;
|
|
|
|
|
dump->filter_zone = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
dump->ct = ct;
|
2019-05-09 08:15:07 -07:00
|
|
|
|
*ptot_bkts = 1; /* Need to clean up the callers. */
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
conntrack_dump_next(struct conntrack_dump *dump, struct ct_dpif_entry *entry)
|
|
|
|
|
{
|
|
|
|
|
struct conntrack *ct = dump->ct;
|
|
|
|
|
long long now = time_msec();
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
for (;;) {
|
|
|
|
|
struct cmap_node *cm_node = cmap_next_position(&ct->conns,
|
|
|
|
|
&dump->cm_pos);
|
|
|
|
|
if (!cm_node) {
|
|
|
|
|
break;
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn;
|
|
|
|
|
INIT_CONTAINER(conn, cm_node, cm_node);
|
|
|
|
|
if ((!dump->filter_zone || conn->key.zone == dump->zone) &&
|
|
|
|
|
(conn->conn_type != CT_CONN_TYPE_UN_NAT)) {
|
2019-05-20 21:50:28 -07:00
|
|
|
|
conn_to_ct_dpif_entry(conn, entry, now);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return EOF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
conntrack_dump_done(struct conntrack_dump *dump OVS_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-11-15 22:07:25 -08:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
conntrack_flush(struct conntrack *ct, const uint16_t *zone)
|
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct conn *conn;
|
|
|
|
|
|
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
|
|
|
|
CMAP_FOR_EACH (conn, cm_node, &ct->conns) {
|
|
|
|
|
if (!zone || *zone == conn->key.zone) {
|
|
|
|
|
conn_clean_one(ct, conn);
|
2015-11-15 22:07:25 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2015-11-15 22:07:25 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2018-02-12 14:02:37 -08:00
|
|
|
|
int
|
|
|
|
|
conntrack_flush_tuple(struct conntrack *ct, const struct ct_dpif_tuple *tuple,
|
|
|
|
|
uint16_t zone)
|
|
|
|
|
{
|
|
|
|
|
int error = 0;
|
2019-05-28 11:14:44 -07:00
|
|
|
|
struct conn_key key;
|
|
|
|
|
struct conn *conn;
|
2018-02-12 14:02:37 -08:00
|
|
|
|
|
2019-05-28 11:14:44 -07:00
|
|
|
|
memset(&key, 0, sizeof(key));
|
|
|
|
|
tuple_to_conn_key(tuple, zone, &key);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_mutex_lock(&ct->ct_lock);
|
2019-05-28 11:14:44 -07:00
|
|
|
|
conn_lookup(ct, &key, time_msec(), &conn, NULL);
|
2018-02-12 14:02:37 -08:00
|
|
|
|
|
2019-05-28 11:14:44 -07:00
|
|
|
|
if (conn && conn->conn_type == CT_CONN_TYPE_DEFAULT) {
|
|
|
|
|
conn_clean(ct, conn);
|
2018-02-12 14:02:37 -08:00
|
|
|
|
} else {
|
2018-12-17 14:43:13 -08:00
|
|
|
|
VLOG_WARN("Must flush tuple using the original pre-NATed tuple");
|
2018-02-12 14:02:37 -08:00
|
|
|
|
error = ENOENT;
|
|
|
|
|
}
|
2019-05-09 08:15:07 -07:00
|
|
|
|
|
|
|
|
|
ovs_mutex_unlock(&ct->ct_lock);
|
2018-02-12 14:02:37 -08:00
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-08 15:18:42 -08:00
|
|
|
|
int
|
|
|
|
|
conntrack_set_maxconns(struct conntrack *ct, uint32_t maxconns)
|
|
|
|
|
{
|
|
|
|
|
atomic_store_relaxed(&ct->n_conn_limit, maxconns);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
conntrack_get_maxconns(struct conntrack *ct, uint32_t *maxconns)
|
|
|
|
|
{
|
|
|
|
|
atomic_read_relaxed(&ct->n_conn_limit, maxconns);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-08 15:18:43 -08:00
|
|
|
|
int
|
|
|
|
|
conntrack_get_nconns(struct conntrack *ct, uint32_t *nconns)
|
|
|
|
|
{
|
|
|
|
|
*nconns = atomic_count_get(&ct->n_conn);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 14:09:41 -07:00
|
|
|
|
int
|
|
|
|
|
conntrack_set_tcp_seq_chk(struct conntrack *ct, bool enabled)
|
|
|
|
|
{
|
|
|
|
|
atomic_store_relaxed(&ct->tcp_seq_chk, enabled);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
conntrack_get_tcp_seq_chk(struct conntrack *ct)
|
|
|
|
|
{
|
|
|
|
|
bool enabled;
|
|
|
|
|
atomic_read_relaxed(&ct->tcp_seq_chk, &enabled);
|
|
|
|
|
return enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
/* This function must be called with the ct->resources read lock taken. */
|
|
|
|
|
static struct alg_exp_node *
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_lookup(struct hmap *alg_expectations, const struct conn_key *key,
|
|
|
|
|
uint32_t basis, bool src_ip_wc)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
2019-02-04 16:02:15 -08:00
|
|
|
|
struct conn_key check_key;
|
|
|
|
|
memcpy(&check_key, key, sizeof check_key);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
check_key.src.port = ALG_WC_SRC_PORT;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2018-01-09 15:44:55 -08:00
|
|
|
|
if (src_ip_wc) {
|
|
|
|
|
memset(&check_key.src.addr, 0, sizeof check_key.src.addr);
|
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
struct alg_exp_node *alg_exp_node;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (alg_exp_node, node,
|
2018-01-09 15:44:56 -08:00
|
|
|
|
conn_key_hash(&check_key, basis),
|
2017-08-06 10:51:14 -07:00
|
|
|
|
alg_expectations) {
|
|
|
|
|
if (!conn_key_cmp(&alg_exp_node->key, &check_key)) {
|
|
|
|
|
return alg_exp_node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:54 -08:00
|
|
|
|
/* This function must be called with the ct->resources write lock taken. */
|
|
|
|
|
static void
|
|
|
|
|
expectation_remove(struct hmap *alg_expectations,
|
|
|
|
|
const struct conn_key *key, uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
struct alg_exp_node *alg_exp_node;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (alg_exp_node, node, conn_key_hash(key, basis),
|
|
|
|
|
alg_expectations) {
|
|
|
|
|
if (!conn_key_cmp(&alg_exp_node->key, key)) {
|
|
|
|
|
hmap_remove(alg_expectations, &alg_exp_node->node);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function must be called with the ct->resources read lock taken. */
|
|
|
|
|
static struct alg_exp_node *
|
|
|
|
|
expectation_ref_lookup_unique(const struct hindex *alg_expectation_refs,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
const struct conn_key *parent_key,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
const struct conn_key *alg_exp_key,
|
|
|
|
|
uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
struct alg_exp_node *alg_exp_node;
|
|
|
|
|
|
|
|
|
|
HINDEX_FOR_EACH_WITH_HASH (alg_exp_node, node_ref,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
conn_key_hash(parent_key, basis),
|
2018-01-09 15:44:54 -08:00
|
|
|
|
alg_expectation_refs) {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
if (!conn_key_cmp(&alg_exp_node->parent_key, parent_key) &&
|
2018-01-09 15:44:54 -08:00
|
|
|
|
!conn_key_cmp(&alg_exp_node->key, alg_exp_key)) {
|
|
|
|
|
return alg_exp_node;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function must be called with the ct->resources write lock taken. */
|
|
|
|
|
static void
|
|
|
|
|
expectation_ref_create(struct hindex *alg_expectation_refs,
|
|
|
|
|
struct alg_exp_node *alg_exp_node,
|
|
|
|
|
uint32_t basis)
|
|
|
|
|
{
|
|
|
|
|
if (!expectation_ref_lookup_unique(alg_expectation_refs,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
&alg_exp_node->parent_key,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
&alg_exp_node->key, basis)) {
|
|
|
|
|
hindex_insert(alg_expectation_refs, &alg_exp_node->node_ref,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
conn_key_hash(&alg_exp_node->parent_key, basis));
|
2018-01-09 15:44:54 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-06-17 15:31:09 -07:00
|
|
|
|
expectation_clean(struct conntrack *ct, const struct conn_key *parent_key)
|
2018-01-09 15:44:54 -08:00
|
|
|
|
{
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_wrlock(&ct->resources_lock);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
|
|
|
|
|
struct alg_exp_node *node, *next;
|
|
|
|
|
HINDEX_FOR_EACH_WITH_HASH_SAFE (node, next, node_ref,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
conn_key_hash(parent_key, ct->hash_basis),
|
2018-01-09 15:44:54 -08:00
|
|
|
|
&ct->alg_expectation_refs) {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
if (!conn_key_cmp(&node->parent_key, parent_key)) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
expectation_remove(&ct->alg_expectations, &node->key,
|
|
|
|
|
ct->hash_basis);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
hindex_remove(&ct->alg_expectation_refs, &node->node_ref);
|
|
|
|
|
free(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
2018-01-09 15:44:54 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static void
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_create(struct conntrack *ct, ovs_be16 dst_port,
|
2020-06-17 15:31:09 -07:00
|
|
|
|
const struct conn *parent_conn, bool reply, bool src_ip_wc,
|
2018-01-09 15:44:55 -08:00
|
|
|
|
bool skip_nat)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr src_addr;
|
|
|
|
|
union ct_addr dst_addr;
|
|
|
|
|
union ct_addr alg_nat_repl_addr;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
struct alg_exp_node *alg_exp_node = xzalloc(sizeof *alg_exp_node);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
|
2018-01-09 15:44:55 -08:00
|
|
|
|
if (reply) {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
src_addr = parent_conn->key.src.addr;
|
|
|
|
|
dst_addr = parent_conn->key.dst.addr;
|
2019-01-15 18:58:16 -08:00
|
|
|
|
alg_exp_node->nat_rpl_dst = true;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
if (skip_nat) {
|
|
|
|
|
alg_nat_repl_addr = dst_addr;
|
2020-06-17 15:31:09 -07:00
|
|
|
|
} else if (parent_conn->nat_info &&
|
|
|
|
|
parent_conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
alg_nat_repl_addr = parent_conn->rev_key.src.addr;
|
2019-01-15 18:58:16 -08:00
|
|
|
|
alg_exp_node->nat_rpl_dst = false;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
} else {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
alg_nat_repl_addr = parent_conn->rev_key.dst.addr;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
src_addr = parent_conn->rev_key.src.addr;
|
|
|
|
|
dst_addr = parent_conn->rev_key.dst.addr;
|
2019-01-15 18:58:16 -08:00
|
|
|
|
alg_exp_node->nat_rpl_dst = false;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
if (skip_nat) {
|
|
|
|
|
alg_nat_repl_addr = src_addr;
|
2020-06-17 15:31:09 -07:00
|
|
|
|
} else if (parent_conn->nat_info &&
|
|
|
|
|
parent_conn->nat_info->nat_action & NAT_ACTION_DST) {
|
|
|
|
|
alg_nat_repl_addr = parent_conn->key.dst.addr;
|
2019-01-15 18:58:16 -08:00
|
|
|
|
alg_exp_node->nat_rpl_dst = true;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
} else {
|
2020-06-17 15:31:09 -07:00
|
|
|
|
alg_nat_repl_addr = parent_conn->key.src.addr;
|
2018-01-09 15:44:55 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (src_ip_wc) {
|
|
|
|
|
memset(&src_addr, 0, sizeof src_addr);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 15:31:09 -07:00
|
|
|
|
alg_exp_node->key.dl_type = parent_conn->key.dl_type;
|
|
|
|
|
alg_exp_node->key.nw_proto = parent_conn->key.nw_proto;
|
|
|
|
|
alg_exp_node->key.zone = parent_conn->key.zone;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
alg_exp_node->key.src.addr = src_addr;
|
|
|
|
|
alg_exp_node->key.dst.addr = dst_addr;
|
|
|
|
|
alg_exp_node->key.src.port = ALG_WC_SRC_PORT;
|
|
|
|
|
alg_exp_node->key.dst.port = dst_port;
|
2020-06-17 15:31:09 -07:00
|
|
|
|
alg_exp_node->parent_mark = parent_conn->mark;
|
|
|
|
|
alg_exp_node->parent_label = parent_conn->label;
|
|
|
|
|
memcpy(&alg_exp_node->parent_key, &parent_conn->key,
|
|
|
|
|
sizeof alg_exp_node->parent_key);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
/* Take the write lock here because it is almost 100%
|
|
|
|
|
* likely that the lookup will fail and
|
|
|
|
|
* expectation_create() will be called below. */
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_wrlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
struct alg_exp_node *alg_exp = expectation_lookup(
|
2018-01-09 15:44:55 -08:00
|
|
|
|
&ct->alg_expectations, &alg_exp_node->key, ct->hash_basis, src_ip_wc);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (alg_exp) {
|
|
|
|
|
free(alg_exp_node);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alg_exp_node->alg_nat_repl_addr = alg_nat_repl_addr;
|
2018-01-09 15:44:54 -08:00
|
|
|
|
hmap_insert(&ct->alg_expectations, &alg_exp_node->node,
|
2018-01-09 15:44:56 -08:00
|
|
|
|
conn_key_hash(&alg_exp_node->key, ct->hash_basis));
|
2018-01-09 15:44:54 -08:00
|
|
|
|
expectation_ref_create(&ct->alg_expectation_refs, alg_exp_node,
|
|
|
|
|
ct->hash_basis);
|
2019-05-09 08:15:07 -07:00
|
|
|
|
ovs_rwlock_unlock(&ct->resources_lock);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
replace_substring(char *substr, uint8_t substr_size,
|
|
|
|
|
uint8_t total_size, char *rep_str,
|
|
|
|
|
uint8_t rep_str_size)
|
|
|
|
|
{
|
|
|
|
|
memmove(substr + rep_str_size, substr + substr_size,
|
|
|
|
|
total_size - substr_size);
|
|
|
|
|
memcpy(substr, rep_str, rep_str_size);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 23:35:40 -08:00
|
|
|
|
static void
|
|
|
|
|
repl_bytes(char *str, char c1, char c2)
|
|
|
|
|
{
|
|
|
|
|
while (*str) {
|
|
|
|
|
if (*str == c1) {
|
|
|
|
|
*str = c2;
|
|
|
|
|
}
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
modify_packet(struct dp_packet *pkt, char *pkt_str, size_t size,
|
|
|
|
|
char *repl_str, size_t repl_size,
|
|
|
|
|
uint32_t orig_used_size)
|
|
|
|
|
{
|
|
|
|
|
replace_substring(pkt_str, size,
|
|
|
|
|
(const char *) dp_packet_tail(pkt) - pkt_str,
|
|
|
|
|
repl_str, repl_size);
|
|
|
|
|
dp_packet_set_size(pkt, orig_used_size + (int) repl_size - (int) size);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
/* Replace IPV4 address in FTP message with NATed address. */
|
|
|
|
|
static int
|
|
|
|
|
repl_ftp_v4_addr(struct dp_packet *pkt, ovs_be32 v4_addr_rep,
|
|
|
|
|
char *ftp_data_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
size_t addr_offset_from_ftp_data_start,
|
|
|
|
|
size_t addr_size OVS_UNUSED)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
|
|
|
|
enum { MAX_FTP_V4_NAT_DELTA = 8 };
|
|
|
|
|
|
|
|
|
|
/* Do conservative check for pathological MTU usage. */
|
|
|
|
|
uint32_t orig_used_size = dp_packet_size(pkt);
|
2019-01-31 23:35:40 -08:00
|
|
|
|
if (orig_used_size + MAX_FTP_V4_NAT_DELTA >
|
|
|
|
|
dp_packet_get_allocated(pkt)) {
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
|
2019-01-31 23:35:40 -08:00
|
|
|
|
VLOG_WARN_RL(&rl, "Unsupported effective MTU %u used with FTP V4",
|
|
|
|
|
dp_packet_get_allocated(pkt));
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 23:35:40 -08:00
|
|
|
|
char v4_addr_str[INET_ADDRSTRLEN] = {0};
|
|
|
|
|
ovs_assert(inet_ntop(AF_INET, &v4_addr_rep, v4_addr_str,
|
|
|
|
|
sizeof v4_addr_str));
|
|
|
|
|
repl_bytes(v4_addr_str, '.', ',');
|
|
|
|
|
modify_packet(pkt, ftp_data_start + addr_offset_from_ftp_data_start,
|
|
|
|
|
addr_size, v4_addr_str, strlen(v4_addr_str),
|
|
|
|
|
orig_used_size);
|
|
|
|
|
return (int) strlen(v4_addr_str) - (int) addr_size;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
skip_non_digits(char *str)
|
|
|
|
|
{
|
|
|
|
|
while (!isdigit(*str) && *str != 0) {
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
terminate_number_str(char *str, uint8_t max_digits)
|
|
|
|
|
{
|
|
|
|
|
uint8_t digits_found = 0;
|
|
|
|
|
while (isdigit(*str) && digits_found <= max_digits) {
|
|
|
|
|
str++;
|
|
|
|
|
digits_found++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*str = 0;
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_ftp_ctl_msg(struct dp_packet *pkt, char *ftp_msg)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
char *tcp_hdr = (char *) th;
|
2021-01-21 14:33:18 -08:00
|
|
|
|
uint32_t tcp_payload_len = dp_packet_get_tcp_payload_length(pkt);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
size_t tcp_payload_of_interest = MIN(tcp_payload_len,
|
|
|
|
|
LARGEST_FTP_MSG_OF_INTEREST);
|
|
|
|
|
size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4;
|
|
|
|
|
|
|
|
|
|
ovs_strlcpy(ftp_msg, tcp_hdr + tcp_hdr_len,
|
|
|
|
|
tcp_payload_of_interest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ftp_ctl_pkt
|
|
|
|
|
detect_ftp_ctl_type(const struct conn_lookup_ctx *ctx,
|
|
|
|
|
struct dp_packet *pkt)
|
|
|
|
|
{
|
|
|
|
|
char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0};
|
|
|
|
|
get_ftp_ctl_msg(pkt, ftp_msg);
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) {
|
|
|
|
|
if (strncasecmp(ftp_msg, FTP_EPRT_CMD, strlen(FTP_EPRT_CMD)) &&
|
|
|
|
|
!strcasestr(ftp_msg, FTP_EPSV_REPLY)) {
|
|
|
|
|
return CT_FTP_CTL_OTHER;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (strncasecmp(ftp_msg, FTP_PORT_CMD, strlen(FTP_PORT_CMD)) &&
|
|
|
|
|
strncasecmp(ftp_msg, FTP_PASV_REPLY_CODE,
|
|
|
|
|
strlen(FTP_PASV_REPLY_CODE))) {
|
|
|
|
|
return CT_FTP_CTL_OTHER;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CT_FTP_CTL_INTEREST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ftp_ctl_pkt
|
|
|
|
|
process_ftp_ctl_v4(struct conntrack *ct,
|
|
|
|
|
struct dp_packet *pkt,
|
|
|
|
|
const struct conn *conn_for_expectation,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
ovs_be32 *v4_addr_rep,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
char **ftp_data_v4_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
size_t *addr_offset_from_ftp_data_start,
|
|
|
|
|
size_t *addr_size)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4;
|
|
|
|
|
char *tcp_hdr = (char *) th;
|
|
|
|
|
*ftp_data_v4_start = tcp_hdr + tcp_hdr_len;
|
|
|
|
|
char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0};
|
|
|
|
|
get_ftp_ctl_msg(pkt, ftp_msg);
|
|
|
|
|
char *ftp = ftp_msg;
|
|
|
|
|
enum ct_alg_mode mode;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-09-25 20:51:45 -07:00
|
|
|
|
if (!strncasecmp(ftp, FTP_PORT_CMD, strlen(FTP_PORT_CMD))) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
ftp = ftp_msg + strlen(FTP_PORT_CMD);
|
|
|
|
|
mode = CT_FTP_MODE_ACTIVE;
|
|
|
|
|
} else {
|
|
|
|
|
ftp = ftp_msg + strlen(FTP_PASV_REPLY_CODE);
|
|
|
|
|
mode = CT_FTP_MODE_PASSIVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find first space. */
|
|
|
|
|
ftp = strchr(ftp, ' ');
|
|
|
|
|
if (!ftp) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Find the first digit, after space. */
|
|
|
|
|
ftp = skip_non_digits(ftp);
|
|
|
|
|
if (*ftp == 0) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *ip_addr_start = ftp;
|
|
|
|
|
*addr_offset_from_ftp_data_start = ip_addr_start - ftp_msg;
|
|
|
|
|
|
2018-01-09 15:44:56 -08:00
|
|
|
|
uint8_t comma_count = 0;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
while (comma_count < 4 && *ftp) {
|
|
|
|
|
if (*ftp == ',') {
|
|
|
|
|
comma_count++;
|
|
|
|
|
if (comma_count == 4) {
|
|
|
|
|
*ftp = 0;
|
|
|
|
|
} else {
|
|
|
|
|
*ftp = '.';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ftp++;
|
|
|
|
|
}
|
|
|
|
|
if (comma_count != 4) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct in_addr ip_addr;
|
|
|
|
|
int rc2 = inet_pton(AF_INET, ip_addr_start, &ip_addr);
|
|
|
|
|
if (rc2 != 1) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 23:35:40 -08:00
|
|
|
|
*addr_size = ftp - ip_addr_start - 1;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
char *save_ftp = ftp;
|
|
|
|
|
ftp = terminate_number_str(ftp, MAX_FTP_PORT_DGTS);
|
|
|
|
|
if (!ftp) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
int value;
|
|
|
|
|
if (!str_to_int(save_ftp, 10, &value)) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This is derived from the L4 port maximum is 65535. */
|
|
|
|
|
if (value > 255) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t port_hs = value;
|
|
|
|
|
port_hs <<= 8;
|
|
|
|
|
|
|
|
|
|
/* Skip over comma. */
|
|
|
|
|
ftp++;
|
|
|
|
|
save_ftp = ftp;
|
|
|
|
|
bool digit_found = false;
|
|
|
|
|
while (isdigit(*ftp)) {
|
|
|
|
|
ftp++;
|
|
|
|
|
digit_found = true;
|
|
|
|
|
}
|
|
|
|
|
if (!digit_found) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
*ftp = 0;
|
|
|
|
|
if (!str_to_int(save_ftp, 10, &value)) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value > 255) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 10:52:54 +08:00
|
|
|
|
port_hs |= value;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
ovs_be16 port = htons(port_hs);
|
|
|
|
|
ovs_be32 conn_ipv4_addr;
|
|
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case CT_FTP_MODE_ACTIVE:
|
2019-02-14 13:15:08 -08:00
|
|
|
|
*v4_addr_rep = conn_for_expectation->rev_key.dst.addr.ipv4;
|
|
|
|
|
conn_ipv4_addr = conn_for_expectation->key.src.addr.ipv4;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
break;
|
|
|
|
|
case CT_FTP_MODE_PASSIVE:
|
2019-02-14 13:15:08 -08:00
|
|
|
|
*v4_addr_rep = conn_for_expectation->key.dst.addr.ipv4;
|
|
|
|
|
conn_ipv4_addr = conn_for_expectation->rev_key.src.addr.ipv4;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
break;
|
2017-08-06 10:51:15 -07:00
|
|
|
|
case CT_TFTP_MODE:
|
2017-08-06 10:51:14 -07:00
|
|
|
|
default:
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ovs_be32 ftp_ipv4_addr;
|
|
|
|
|
ftp_ipv4_addr = ip_addr.s_addr;
|
|
|
|
|
/* Although most servers will block this exploit, there may be some
|
|
|
|
|
* less well managed. */
|
|
|
|
|
if (ftp_ipv4_addr != conn_ipv4_addr && ftp_ipv4_addr != *v4_addr_rep) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_create(ct, port, conn_for_expectation,
|
|
|
|
|
!!(pkt->md.ct_state & CS_REPLY_DIR), false, false);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return CT_FTP_CTL_INTEREST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
skip_ipv6_digits(char *str)
|
|
|
|
|
{
|
|
|
|
|
while (isxdigit(*str) || *str == ':' || *str == '.') {
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ftp_ctl_pkt
|
|
|
|
|
process_ftp_ctl_v6(struct conntrack *ct,
|
|
|
|
|
struct dp_packet *pkt,
|
|
|
|
|
const struct conn *conn_for_expectation,
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr *v6_addr_rep, char **ftp_data_start,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
size_t *addr_offset_from_ftp_data_start,
|
|
|
|
|
size_t *addr_size, enum ct_alg_mode *mode)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
|
|
|
|
size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4;
|
|
|
|
|
char *tcp_hdr = (char *) th;
|
|
|
|
|
char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0};
|
|
|
|
|
get_ftp_ctl_msg(pkt, ftp_msg);
|
|
|
|
|
*ftp_data_start = tcp_hdr + tcp_hdr_len;
|
|
|
|
|
char *ftp = ftp_msg;
|
|
|
|
|
struct in6_addr ip6_addr;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-09-25 20:51:45 -07:00
|
|
|
|
if (!strncasecmp(ftp, FTP_EPRT_CMD, strlen(FTP_EPRT_CMD))) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
ftp = ftp_msg + strlen(FTP_EPRT_CMD);
|
|
|
|
|
ftp = skip_non_digits(ftp);
|
|
|
|
|
if (*ftp != FTP_AF_V6 || isdigit(ftp[1])) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
/* Jump over delimiter. */
|
|
|
|
|
ftp += 2;
|
|
|
|
|
|
|
|
|
|
memset(&ip6_addr, 0, sizeof ip6_addr);
|
2018-01-09 15:44:56 -08:00
|
|
|
|
char *ip_addr_start = ftp;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
*addr_offset_from_ftp_data_start = ip_addr_start - ftp_msg;
|
|
|
|
|
ftp = skip_ipv6_digits(ftp);
|
|
|
|
|
*ftp = 0;
|
|
|
|
|
*addr_size = ftp - ip_addr_start;
|
|
|
|
|
int rc2 = inet_pton(AF_INET6, ip_addr_start, &ip6_addr);
|
|
|
|
|
if (rc2 != 1) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
ftp++;
|
|
|
|
|
*mode = CT_FTP_MODE_ACTIVE;
|
|
|
|
|
} else {
|
|
|
|
|
ftp = ftp_msg + strcspn(ftp_msg, "(");
|
|
|
|
|
ftp = skip_non_digits(ftp);
|
|
|
|
|
if (!isdigit(*ftp)) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Not used for passive mode. */
|
|
|
|
|
*addr_offset_from_ftp_data_start = 0;
|
|
|
|
|
*addr_size = 0;
|
|
|
|
|
|
|
|
|
|
*mode = CT_FTP_MODE_PASSIVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *save_ftp = ftp;
|
|
|
|
|
ftp = terminate_number_str(ftp, MAX_EXT_FTP_PORT_DGTS);
|
|
|
|
|
if (!ftp) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
int value;
|
|
|
|
|
if (!str_to_int(save_ftp, 10, &value)) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
if (value > CT_MAX_L4_PORT) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t port_hs = value;
|
|
|
|
|
ovs_be16 port = htons(port_hs);
|
|
|
|
|
|
|
|
|
|
switch (*mode) {
|
|
|
|
|
case CT_FTP_MODE_ACTIVE:
|
|
|
|
|
*v6_addr_rep = conn_for_expectation->rev_key.dst.addr;
|
|
|
|
|
/* Although most servers will block this exploit, there may be some
|
|
|
|
|
* less well managed. */
|
2019-02-14 13:15:08 -08:00
|
|
|
|
if (memcmp(&ip6_addr, &v6_addr_rep->ipv6, sizeof ip6_addr) &&
|
|
|
|
|
memcmp(&ip6_addr, &conn_for_expectation->key.src.addr.ipv6,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
sizeof ip6_addr)) {
|
|
|
|
|
return CT_FTP_CTL_INVALID;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CT_FTP_MODE_PASSIVE:
|
|
|
|
|
*v6_addr_rep = conn_for_expectation->key.dst.addr;
|
|
|
|
|
break;
|
2017-08-06 10:51:15 -07:00
|
|
|
|
case CT_TFTP_MODE:
|
2017-08-06 10:51:14 -07:00
|
|
|
|
default:
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_create(ct, port, conn_for_expectation,
|
|
|
|
|
!!(pkt->md.ct_state & CS_REPLY_DIR), false, false);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return CT_FTP_CTL_INTEREST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2019-02-14 13:15:08 -08:00
|
|
|
|
repl_ftp_v6_addr(struct dp_packet *pkt, union ct_addr v6_addr_rep,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
char *ftp_data_start,
|
|
|
|
|
size_t addr_offset_from_ftp_data_start,
|
|
|
|
|
size_t addr_size, enum ct_alg_mode mode)
|
|
|
|
|
{
|
|
|
|
|
/* This is slightly bigger than really possible. */
|
|
|
|
|
enum { MAX_FTP_V6_NAT_DELTA = 45 };
|
|
|
|
|
|
|
|
|
|
if (mode == CT_FTP_MODE_PASSIVE) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do conservative check for pathological MTU usage. */
|
|
|
|
|
uint32_t orig_used_size = dp_packet_size(pkt);
|
2019-01-31 23:35:40 -08:00
|
|
|
|
if (orig_used_size + MAX_FTP_V6_NAT_DELTA >
|
|
|
|
|
dp_packet_get_allocated(pkt)) {
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
|
2019-01-31 23:35:40 -08:00
|
|
|
|
VLOG_WARN_RL(&rl, "Unsupported effective MTU %u used with FTP V6",
|
|
|
|
|
dp_packet_get_allocated(pkt));
|
2017-08-06 10:51:14 -07:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 23:35:41 -08:00
|
|
|
|
char v6_addr_str[INET6_ADDRSTRLEN] = {0};
|
2019-02-14 13:15:08 -08:00
|
|
|
|
ovs_assert(inet_ntop(AF_INET6, &v6_addr_rep.ipv6, v6_addr_str,
|
2019-01-31 23:35:41 -08:00
|
|
|
|
sizeof v6_addr_str));
|
2019-01-31 23:35:40 -08:00
|
|
|
|
modify_packet(pkt, ftp_data_start + addr_offset_from_ftp_data_start,
|
|
|
|
|
addr_size, v6_addr_str, strlen(v6_addr_str),
|
|
|
|
|
orig_used_size);
|
|
|
|
|
return (int) strlen(v6_addr_str) - (int) addr_size;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 18:58:17 -08:00
|
|
|
|
/* Increment/decrement a TCP sequence number. */
|
|
|
|
|
static void
|
|
|
|
|
adj_seqnum(ovs_16aligned_be32 *val, int32_t inc)
|
|
|
|
|
{
|
|
|
|
|
put_16aligned_be32(val, htonl(ntohl(get_16aligned_be32(val)) + inc));
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
static void
|
|
|
|
|
handle_ftp_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct dp_packet *pkt, struct conn *ec, long long now,
|
2019-01-15 18:58:15 -08:00
|
|
|
|
enum ftp_ctl_pkt ftp_ctl, bool nat)
|
2017-08-06 10:51:14 -07:00
|
|
|
|
{
|
|
|
|
|
struct ip_header *l3_hdr = dp_packet_l3(pkt);
|
|
|
|
|
ovs_be32 v4_addr_rep = 0;
|
2019-02-14 13:15:08 -08:00
|
|
|
|
union ct_addr v6_addr_rep;
|
2018-12-17 15:00:26 -08:00
|
|
|
|
size_t addr_offset_from_ftp_data_start = 0;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
size_t addr_size = 0;
|
|
|
|
|
char *ftp_data_start;
|
|
|
|
|
enum ct_alg_mode mode = CT_FTP_MODE_ACTIVE;
|
|
|
|
|
|
|
|
|
|
if (detect_ftp_ctl_type(ctx, pkt) != ftp_ctl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt);
|
|
|
|
|
int64_t seq_skew = 0;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2019-01-15 18:58:15 -08:00
|
|
|
|
if (ftp_ctl == CT_FTP_CTL_INTEREST) {
|
2017-08-06 10:51:14 -07:00
|
|
|
|
enum ftp_ctl_pkt rc;
|
|
|
|
|
if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
rc = process_ftp_ctl_v6(ct, pkt, ec,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
&v6_addr_rep, &ftp_data_start,
|
2017-08-06 10:51:14 -07:00
|
|
|
|
&addr_offset_from_ftp_data_start,
|
|
|
|
|
&addr_size, &mode);
|
|
|
|
|
} else {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
rc = process_ftp_ctl_v4(ct, pkt, ec,
|
2018-01-09 15:44:54 -08:00
|
|
|
|
&v4_addr_rep, &ftp_data_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
&addr_offset_from_ftp_data_start,
|
|
|
|
|
&addr_size);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
if (rc == CT_FTP_CTL_INVALID) {
|
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
|
|
|
|
|
VLOG_WARN_RL(&rl, "Invalid FTP control packet format");
|
|
|
|
|
pkt->md.ct_state |= CS_TRACKED | CS_INVALID;
|
|
|
|
|
return;
|
|
|
|
|
} else if (rc == CT_FTP_CTL_INTEREST) {
|
|
|
|
|
uint16_t ip_len;
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
if (nat) {
|
|
|
|
|
seq_skew = repl_ftp_v6_addr(pkt, v6_addr_rep,
|
|
|
|
|
ftp_data_start,
|
|
|
|
|
addr_offset_from_ftp_data_start,
|
|
|
|
|
addr_size, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (seq_skew) {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
ip_len = ntohs(nh6->ip6_ctlun.ip6_un1.ip6_un1_plen) +
|
|
|
|
|
seq_skew;
|
2017-08-06 10:51:14 -07:00
|
|
|
|
nh6->ip6_ctlun.ip6_un1.ip6_un1_plen = htons(ip_len);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
if (nat) {
|
|
|
|
|
seq_skew = repl_ftp_v4_addr(pkt, v4_addr_rep,
|
|
|
|
|
ftp_data_start,
|
2019-01-31 23:35:40 -08:00
|
|
|
|
addr_offset_from_ftp_data_start,
|
|
|
|
|
addr_size);
|
2019-01-15 18:58:15 -08:00
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
if (seq_skew) {
|
2019-01-15 18:58:15 -08:00
|
|
|
|
ip_len = ntohs(l3_hdr->ip_tot_len) + seq_skew;
|
2020-01-17 18:47:55 -03:00
|
|
|
|
if (!dp_packet_hwol_is_ipv4(pkt)) {
|
|
|
|
|
l3_hdr->ip_csum = recalc_csum16(l3_hdr->ip_csum,
|
|
|
|
|
l3_hdr->ip_tot_len,
|
|
|
|
|
htons(ip_len));
|
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
l3_hdr->ip_tot_len = htons(ip_len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
OVS_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tcp_header *th = dp_packet_l4(pkt);
|
2018-01-09 15:44:56 -08:00
|
|
|
|
|
2019-01-15 18:58:15 -08:00
|
|
|
|
if (nat && ec->seq_skew != 0) {
|
2019-01-15 18:58:17 -08:00
|
|
|
|
ctx->reply != ec->seq_skew_dir ?
|
|
|
|
|
adj_seqnum(&th->tcp_ack, -ec->seq_skew) :
|
|
|
|
|
adj_seqnum(&th->tcp_seq, ec->seq_skew);
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
th->tcp_csum = 0;
|
2020-01-17 18:47:55 -03:00
|
|
|
|
if (!dp_packet_hwol_tx_l4_checksum(pkt)) {
|
|
|
|
|
if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) {
|
|
|
|
|
th->tcp_csum = packet_csum_upperlayer6(nh6, th, ctx->key.nw_proto,
|
|
|
|
|
dp_packet_l4_size(pkt));
|
|
|
|
|
} else {
|
|
|
|
|
uint32_t tcp_csum = packet_csum_pseudoheader(l3_hdr);
|
|
|
|
|
th->tcp_csum = csum_finish(
|
|
|
|
|
csum_continue(tcp_csum, th, dp_packet_l4_size(pkt)));
|
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
2019-01-15 18:58:15 -08:00
|
|
|
|
|
|
|
|
|
if (seq_skew) {
|
2019-05-09 08:15:07 -07:00
|
|
|
|
conn_seq_skew_set(ct, ec, now, seq_skew + ec->seq_skew,
|
2019-01-15 18:58:15 -08:00
|
|
|
|
ctx->reply);
|
|
|
|
|
}
|
2017-08-06 10:51:14 -07:00
|
|
|
|
}
|
2017-08-06 10:51:15 -07:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_tftp_ctl(struct conntrack *ct,
|
2017-12-04 08:13:05 -08:00
|
|
|
|
const struct conn_lookup_ctx *ctx OVS_UNUSED,
|
2019-05-09 08:15:07 -07:00
|
|
|
|
struct dp_packet *pkt, struct conn *conn_for_expectation,
|
|
|
|
|
long long now OVS_UNUSED, enum ftp_ctl_pkt ftp_ctl OVS_UNUSED,
|
|
|
|
|
bool nat OVS_UNUSED)
|
2017-08-06 10:51:15 -07:00
|
|
|
|
{
|
2018-01-09 15:44:55 -08:00
|
|
|
|
expectation_create(ct, conn_for_expectation->key.src.port,
|
|
|
|
|
conn_for_expectation,
|
|
|
|
|
!!(pkt->md.ct_state & CS_REPLY_DIR), false, false);
|
2017-08-06 10:51:15 -07:00
|
|
|
|
}
|