conntrack.c 22.2 KB
Newer Older
J
Joe Stringer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2015 Nicira, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 */

#include <linux/module.h>
#include <linux/openvswitch.h>
#include <net/ip.h>
#include <net/netfilter/nf_conntrack_core.h>
18
#include <net/netfilter/nf_conntrack_helper.h>
19
#include <net/netfilter/nf_conntrack_labels.h>
J
Joe Stringer 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32
#include <net/netfilter/nf_conntrack_zones.h>
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>

#include "datapath.h"
#include "conntrack.h"
#include "flow.h"
#include "flow_netlink.h"

struct ovs_ct_len_tbl {
	size_t maxlen;
	size_t minlen;
};

33 34 35 36 37 38
/* Metadata mark for masked write to conntrack mark */
struct md_mark {
	u32 value;
	u32 mask;
};

39
/* Metadata label for masked write to conntrack label. */
J
Joe Stringer 已提交
40 41 42
struct md_labels {
	struct ovs_key_ct_labels value;
	struct ovs_key_ct_labels mask;
43 44
};

J
Joe Stringer 已提交
45 46
/* Conntrack action context for execution. */
struct ovs_conntrack_info {
47
	struct nf_conntrack_helper *helper;
J
Joe Stringer 已提交
48 49
	struct nf_conntrack_zone zone;
	struct nf_conn *ct;
50
	u8 commit : 1;
J
Joe Stringer 已提交
51
	u16 family;
52
	struct md_mark mark;
J
Joe Stringer 已提交
53
	struct md_labels labels;
J
Joe Stringer 已提交
54 55
};

56 57
static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);

J
Joe Stringer 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static u16 key_to_nfproto(const struct sw_flow_key *key)
{
	switch (ntohs(key->eth.type)) {
	case ETH_P_IP:
		return NFPROTO_IPV4;
	case ETH_P_IPV6:
		return NFPROTO_IPV6;
	default:
		return NFPROTO_UNSPEC;
	}
}

/* Map SKB connection state into the values used by flow definition. */
static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
{
	u8 ct_state = OVS_CS_F_TRACKED;

	switch (ctinfo) {
	case IP_CT_ESTABLISHED_REPLY:
	case IP_CT_RELATED_REPLY:
		ct_state |= OVS_CS_F_REPLY_DIR;
		break;
	default:
		break;
	}

	switch (ctinfo) {
	case IP_CT_ESTABLISHED:
	case IP_CT_ESTABLISHED_REPLY:
		ct_state |= OVS_CS_F_ESTABLISHED;
		break;
	case IP_CT_RELATED:
	case IP_CT_RELATED_REPLY:
		ct_state |= OVS_CS_F_RELATED;
		break;
	case IP_CT_NEW:
		ct_state |= OVS_CS_F_NEW;
		break;
	default:
		break;
	}

	return ct_state;
}

103 104 105 106 107 108 109 110 111
static u32 ovs_ct_get_mark(const struct nf_conn *ct)
{
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
	return ct ? ct->mark : 0;
#else
	return 0;
#endif
}

J
Joe Stringer 已提交
112 113
static void ovs_ct_get_labels(const struct nf_conn *ct,
			      struct ovs_key_ct_labels *labels)
114 115 116 117 118 119
{
	struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;

	if (cl) {
		size_t len = cl->words * sizeof(long);

J
Joe Stringer 已提交
120 121 122 123 124
		if (len > OVS_CT_LABELS_LEN)
			len = OVS_CT_LABELS_LEN;
		else if (len < OVS_CT_LABELS_LEN)
			memset(labels, 0, OVS_CT_LABELS_LEN);
		memcpy(labels, cl->bits, len);
125
	} else {
J
Joe Stringer 已提交
126
		memset(labels, 0, OVS_CT_LABELS_LEN);
127 128 129
	}
}

J
Joe Stringer 已提交
130
static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
131 132
				const struct nf_conntrack_zone *zone,
				const struct nf_conn *ct)
J
Joe Stringer 已提交
133 134 135
{
	key->ct.state = state;
	key->ct.zone = zone->id;
136
	key->ct.mark = ovs_ct_get_mark(ct);
J
Joe Stringer 已提交
137
	ovs_ct_get_labels(ct, &key->ct.labels);
J
Joe Stringer 已提交
138 139 140 141 142 143
}

/* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
 * previously sent the packet to conntrack via the ct action.
 */
static void ovs_ct_update_key(const struct sk_buff *skb,
144
			      const struct ovs_conntrack_info *info,
J
Joe Stringer 已提交
145 146 147 148 149 150 151 152 153 154
			      struct sw_flow_key *key, bool post_ct)
{
	const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct;
	u8 state = 0;

	ct = nf_ct_get(skb, &ctinfo);
	if (ct) {
		state = ovs_ct_get_state(ctinfo);
155
		/* All unconfirmed entries are NEW connections. */
156 157
		if (!nf_ct_is_confirmed(ct))
			state |= OVS_CS_F_NEW;
158 159 160
		/* OVS persists the related flag for the duration of the
		 * connection.
		 */
J
Joe Stringer 已提交
161 162 163 164 165
		if (ct->master)
			state |= OVS_CS_F_RELATED;
		zone = nf_ct_zone(ct);
	} else if (post_ct) {
		state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
166 167
		if (info)
			zone = &info->zone;
J
Joe Stringer 已提交
168
	}
169
	__ovs_ct_update_key(key, state, zone, ct);
J
Joe Stringer 已提交
170 171
}

172 173 174
/* This is called to initialize CT key fields possibly coming in from the local
 * stack.
 */
J
Joe Stringer 已提交
175 176
void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
{
177
	ovs_ct_update_key(skb, NULL, key, false);
J
Joe Stringer 已提交
178 179 180 181
}

int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
{
182
	if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
J
Joe Stringer 已提交
183 184 185 186 187 188
		return -EMSGSIZE;

	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
	    nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
		return -EMSGSIZE;

189 190 191 192
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
	    nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
		return -EMSGSIZE;

193
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
J
Joe Stringer 已提交
194 195
	    nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
		    &key->ct.labels))
196 197
		return -EMSGSIZE;

198 199 200 201 202 203
	return 0;
}

static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
			   u32 ct_mark, u32 mask)
{
204
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct;
	u32 new_mark;

	/* The connection could be invalid, in which case set_mark is no-op. */
	ct = nf_ct_get(skb, &ctinfo);
	if (!ct)
		return 0;

	new_mark = ct_mark | (ct->mark & ~(mask));
	if (ct->mark != new_mark) {
		ct->mark = new_mark;
		nf_conntrack_event_cache(IPCT_MARK, ct);
		key->ct.mark = new_mark;
	}

J
Joe Stringer 已提交
221
	return 0;
222 223 224
#else
	return -ENOTSUPP;
#endif
J
Joe Stringer 已提交
225 226
}

J
Joe Stringer 已提交
227 228 229
static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
			     const struct ovs_key_ct_labels *labels,
			     const struct ovs_key_ct_labels *mask)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
{
	enum ip_conntrack_info ctinfo;
	struct nf_conn_labels *cl;
	struct nf_conn *ct;
	int err;

	/* The connection could be invalid, in which case set_label is no-op.*/
	ct = nf_ct_get(skb, &ctinfo);
	if (!ct)
		return 0;

	cl = nf_ct_labels_find(ct);
	if (!cl) {
		nf_ct_labels_ext_add(ct);
		cl = nf_ct_labels_find(ct);
	}
J
Joe Stringer 已提交
246
	if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
247 248
		return -ENOSPC;

J
Joe Stringer 已提交
249 250
	err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
				    OVS_CT_LABELS_LEN / sizeof(u32));
251 252 253
	if (err)
		return err;

J
Joe Stringer 已提交
254
	ovs_ct_get_labels(ct, &key->ct.labels);
255 256 257
	return 0;
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/* 'skb' should already be pulled to nh_ofs. */
static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
{
	const struct nf_conntrack_helper *helper;
	const struct nf_conn_help *help;
	enum ip_conntrack_info ctinfo;
	unsigned int protoff;
	struct nf_conn *ct;

	ct = nf_ct_get(skb, &ctinfo);
	if (!ct || ctinfo == IP_CT_RELATED_REPLY)
		return NF_ACCEPT;

	help = nfct_help(ct);
	if (!help)
		return NF_ACCEPT;

	helper = rcu_dereference(help->helper);
	if (!helper)
		return NF_ACCEPT;

	switch (proto) {
	case NFPROTO_IPV4:
		protoff = ip_hdrlen(skb);
		break;
	case NFPROTO_IPV6: {
		u8 nexthdr = ipv6_hdr(skb)->nexthdr;
		__be16 frag_off;
286
		int ofs;
287

288 289 290
		ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
				       &frag_off);
		if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
291 292 293
			pr_debug("proto header not found\n");
			return NF_ACCEPT;
		}
294
		protoff = ofs;
295 296 297 298 299 300 301 302 303 304
		break;
	}
	default:
		WARN_ONCE(1, "helper invoked on non-IP family!");
		return NF_DROP;
	}

	return helper->help(skb, protoff, ct, ctinfo);
}

305 306 307
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
 * value if 'skb' is freed.
 */
J
Joe Stringer 已提交
308 309 310 311
static int handle_fragments(struct net *net, struct sw_flow_key *key,
			    u16 zone, struct sk_buff *skb)
{
	struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
312
	int err;
J
Joe Stringer 已提交
313 314 315 316 317

	if (key->eth.type == htons(ETH_P_IP)) {
		enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;

		memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
318
		err = ip_defrag(net, skb, user);
J
Joe Stringer 已提交
319 320 321 322 323
		if (err)
			return err;

		ovs_cb.mru = IPCB(skb)->frag_max_size;
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
324
	} else if (key->eth.type == htons(ETH_P_IPV6)) {
J
Joe Stringer 已提交
325 326 327
		enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;

		memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
328 329 330
		err = nf_ct_frag6_gather(net, skb, user);
		if (err)
			return err;
J
Joe Stringer 已提交
331

332
		key->ip.proto = ipv6_hdr(skb)->nexthdr;
J
Joe Stringer 已提交
333 334 335
		ovs_cb.mru = IP6CB(skb)->frag_max_size;
#endif
	} else {
336
		kfree_skb(skb);
J
Joe Stringer 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		return -EPFNOSUPPORT;
	}

	key->ip.frag = OVS_FRAG_TYPE_NONE;
	skb_clear_hash(skb);
	skb->ignore_df = 1;
	*OVS_CB(skb) = ovs_cb;

	return 0;
}

static struct nf_conntrack_expect *
ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
		   u16 proto, const struct sk_buff *skb)
{
	struct nf_conntrack_tuple tuple;

354
	if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
J
Joe Stringer 已提交
355 356 357 358
		return NULL;
	return __nf_ct_expect_find(net, zone, &tuple);
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
/* This replicates logic from nf_conntrack_core.c that is not exported. */
static enum ip_conntrack_info
ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
{
	const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);

	if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
		return IP_CT_ESTABLISHED_REPLY;
	/* Once we've had two way comms, always ESTABLISHED. */
	if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
		return IP_CT_ESTABLISHED;
	if (test_bit(IPS_EXPECTED_BIT, &ct->status))
		return IP_CT_RELATED;
	return IP_CT_NEW;
}

/* Find an existing connection which this packet belongs to without
 * re-attributing statistics or modifying the connection state.  This allows an
 * skb->nfct lost due to an upcall to be recovered during actions execution.
 *
 * Must be called with rcu_read_lock.
 *
 * On success, populates skb->nfct and skb->nfctinfo, and returns the
 * connection.  Returns NULL if there is no existing entry.
 */
static struct nf_conn *
ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
		     u8 l3num, struct sk_buff *skb)
{
	struct nf_conntrack_l3proto *l3proto;
	struct nf_conntrack_l4proto *l4proto;
	struct nf_conntrack_tuple tuple;
	struct nf_conntrack_tuple_hash *h;
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct;
	unsigned int dataoff;
	u8 protonum;

	l3proto = __nf_ct_l3proto_find(l3num);
	if (!l3proto) {
		pr_debug("ovs_ct_find_existing: Can't get l3proto\n");
		return NULL;
	}
	if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
				 &protonum) <= 0) {
		pr_debug("ovs_ct_find_existing: Can't get protonum\n");
		return NULL;
	}
	l4proto = __nf_ct_l4proto_find(l3num, protonum);
	if (!l4proto) {
		pr_debug("ovs_ct_find_existing: Can't get l4proto\n");
		return NULL;
	}
	if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
			     protonum, net, &tuple, l3proto, l4proto)) {
		pr_debug("ovs_ct_find_existing: Can't get tuple\n");
		return NULL;
	}

	/* look for tuple match */
	h = nf_conntrack_find_get(net, zone, &tuple);
	if (!h)
		return NULL;   /* Not found. */

	ct = nf_ct_tuplehash_to_ctrack(h);

	ctinfo = ovs_ct_get_info(h);
	if (ctinfo == IP_CT_NEW) {
		/* This should not happen. */
		WARN_ONCE(1, "ovs_ct_find_existing: new packet for %p\n", ct);
	}
	skb->nfct = &ct->ct_general;
	skb->nfctinfo = ctinfo;
	return ct;
}

J
Joe Stringer 已提交
435
/* Determine whether skb->nfct is equal to the result of conntrack lookup. */
436 437 438 439
static bool skb_nfct_cached(struct net *net,
			    const struct sw_flow_key *key,
			    const struct ovs_conntrack_info *info,
			    struct sk_buff *skb)
J
Joe Stringer 已提交
440 441 442 443 444
{
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct;

	ct = nf_ct_get(skb, &ctinfo);
445 446 447 448 449 450 451 452 453
	/* If no ct, check if we have evidence that an existing conntrack entry
	 * might be found for this skb.  This happens when we lose a skb->nfct
	 * due to an upcall.  If the connection was not confirmed, it is not
	 * cached and needs to be run through conntrack again.
	 */
	if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
	    !(key->ct.state & OVS_CS_F_INVALID) &&
	    key->ct.zone == info->zone.id)
		ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
J
Joe Stringer 已提交
454 455 456 457 458 459
	if (!ct)
		return false;
	if (!net_eq(net, read_pnet(&ct->ct_net)))
		return false;
	if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
		return false;
460 461 462 463 464 465 466
	if (info->helper) {
		struct nf_conn_help *help;

		help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
		if (help && rcu_access_pointer(help->helper) != info->helper)
			return false;
	}
J
Joe Stringer 已提交
467 468 469 470

	return true;
}

471
/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
472 473
 * not done already.  Update key with new CT state after passing the packet
 * through conntrack.
474 475 476
 * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
 * set to NULL and 0 will be returned.
 */
477
static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
J
Joe Stringer 已提交
478 479 480 481 482 483 484 485
			   const struct ovs_conntrack_info *info,
			   struct sk_buff *skb)
{
	/* If we are recirculating packets to match on conntrack fields and
	 * committing with a separate conntrack action,  then we don't need to
	 * actually run the packet through conntrack twice unless it's for a
	 * different zone.
	 */
486 487 488 489 490
	bool cached = skb_nfct_cached(net, key, info, skb);
	enum ip_conntrack_info ctinfo;
	struct nf_conn *ct;

	if (!cached) {
J
Joe Stringer 已提交
491
		struct nf_conn *tmpl = info->ct;
492
		int err;
J
Joe Stringer 已提交
493 494 495 496 497 498 499 500 501 502

		/* Associate skb with specified zone. */
		if (tmpl) {
			if (skb->nfct)
				nf_conntrack_put(skb->nfct);
			nf_conntrack_get(&tmpl->ct_general);
			skb->nfct = &tmpl->ct_general;
			skb->nfctinfo = IP_CT_NEW;
		}

503 504 505 506 507 508 509
		/* Repeat if requested, see nf_iterate(). */
		do {
			err = nf_conntrack_in(net, info->family,
					      NF_INET_PRE_ROUTING, skb);
		} while (err == NF_REPEAT);

		if (err != NF_ACCEPT)
J
Joe Stringer 已提交
510
			return -ENOENT;
511

512
		ovs_ct_update_key(skb, info, key, true);
513
	}
514

515 516 517 518 519 520 521 522 523 524
	/* Call the helper only if:
	 * - nf_conntrack_in() was executed above ("!cached") for a confirmed
	 *   connection, or
	 * - When committing an unconfirmed connection.
	 */
	ct = nf_ct_get(skb, &ctinfo);
	if (ct && (nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
	    ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
		WARN_ONCE(1, "helper rejected packet");
		return -EINVAL;
J
Joe Stringer 已提交
525 526 527 528 529 530 531 532 533 534 535 536
	}

	return 0;
}

/* Lookup connection and read fields into key. */
static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
			 const struct ovs_conntrack_info *info,
			 struct sk_buff *skb)
{
	struct nf_conntrack_expect *exp;

537 538 539 540 541 542 543
	/* If we pass an expected packet through nf_conntrack_in() the
	 * expectation is typically removed, but the packet could still be
	 * lost in upcall processing.  To prevent this from happening we
	 * perform an explicit expectation lookup.  Expected connections are
	 * always new, and will be passed through conntrack only when they are
	 * committed, as it is OK to remove the expectation at that time.
	 */
J
Joe Stringer 已提交
544 545 546 547 548
	exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
	if (exp) {
		u8 state;

		state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
549
		__ovs_ct_update_key(key, state, &info->zone, exp->master);
J
Joe Stringer 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	} else {
		int err;

		err = __ovs_ct_lookup(net, key, info, skb);
		if (err)
			return err;
	}

	return 0;
}

/* Lookup connection and confirm if unconfirmed. */
static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
			 const struct ovs_conntrack_info *info,
			 struct sk_buff *skb)
{
	int err;

	err = __ovs_ct_lookup(net, key, info, skb);
	if (err)
		return err;
571
	/* This is a no-op if the connection has already been confirmed. */
J
Joe Stringer 已提交
572 573 574 575 576 577
	if (nf_conntrack_confirm(skb) != NF_ACCEPT)
		return -EINVAL;

	return 0;
}

J
Joe Stringer 已提交
578
static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
579 580 581
{
	size_t i;

J
Joe Stringer 已提交
582 583
	for (i = 0; i < sizeof(*labels); i++)
		if (labels->ct_labels[i])
584 585 586 587 588
			return true;

	return false;
}

589 590 591
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
 * value if 'skb' is freed.
 */
J
Joe Stringer 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
int ovs_ct_execute(struct net *net, struct sk_buff *skb,
		   struct sw_flow_key *key,
		   const struct ovs_conntrack_info *info)
{
	int nh_ofs;
	int err;

	/* The conntrack module expects to be working at L3. */
	nh_ofs = skb_network_offset(skb);
	skb_pull(skb, nh_ofs);

	if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
		err = handle_fragments(net, key, info->zone.id, skb);
		if (err)
			return err;
	}

609
	if (info->commit)
J
Joe Stringer 已提交
610 611 612
		err = ovs_ct_commit(net, key, info, skb);
	else
		err = ovs_ct_lookup(net, key, info, skb);
613 614
	if (err)
		goto err;
J
Joe Stringer 已提交
615

616
	if (info->mark.mask) {
617 618
		err = ovs_ct_set_mark(skb, key, info->mark.value,
				      info->mark.mask);
619 620 621
		if (err)
			goto err;
	}
J
Joe Stringer 已提交
622 623 624
	if (labels_nonzero(&info->labels.mask))
		err = ovs_ct_set_labels(skb, key, &info->labels.value,
					&info->labels.mask);
625
err:
J
Joe Stringer 已提交
626
	skb_push(skb, nh_ofs);
627 628
	if (err)
		kfree_skb(skb);
J
Joe Stringer 已提交
629 630 631
	return err;
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
			     const struct sw_flow_key *key, bool log)
{
	struct nf_conntrack_helper *helper;
	struct nf_conn_help *help;

	helper = nf_conntrack_helper_try_module_get(name, info->family,
						    key->ip.proto);
	if (!helper) {
		OVS_NLERR(log, "Unknown helper \"%s\"", name);
		return -EINVAL;
	}

	help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
	if (!help) {
		module_put(helper->me);
		return -ENOMEM;
	}

	rcu_assign_pointer(help->helper, helper);
	info->helper = helper;
	return 0;
}

J
Joe Stringer 已提交
656
static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
657
	[OVS_CT_ATTR_COMMIT]	= { .minlen = 0, .maxlen = 0 },
J
Joe Stringer 已提交
658 659
	[OVS_CT_ATTR_ZONE]	= { .minlen = sizeof(u16),
				    .maxlen = sizeof(u16) },
660 661
	[OVS_CT_ATTR_MARK]	= { .minlen = sizeof(struct md_mark),
				    .maxlen = sizeof(struct md_mark) },
J
Joe Stringer 已提交
662 663
	[OVS_CT_ATTR_LABELS]	= { .minlen = sizeof(struct md_labels),
				    .maxlen = sizeof(struct md_labels) },
664 665
	[OVS_CT_ATTR_HELPER]	= { .minlen = 1,
				    .maxlen = NF_CT_HELPER_NAME_LEN }
J
Joe Stringer 已提交
666 667 668
};

static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
669
		    const char **helper, bool log)
J
Joe Stringer 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
{
	struct nlattr *a;
	int rem;

	nla_for_each_nested(a, attr, rem) {
		int type = nla_type(a);
		int maxlen = ovs_ct_attr_lens[type].maxlen;
		int minlen = ovs_ct_attr_lens[type].minlen;

		if (type > OVS_CT_ATTR_MAX) {
			OVS_NLERR(log,
				  "Unknown conntrack attr (type=%d, max=%d)",
				  type, OVS_CT_ATTR_MAX);
			return -EINVAL;
		}
		if (nla_len(a) < minlen || nla_len(a) > maxlen) {
			OVS_NLERR(log,
				  "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
				  type, nla_len(a), maxlen);
			return -EINVAL;
		}

		switch (type) {
693 694
		case OVS_CT_ATTR_COMMIT:
			info->commit = true;
J
Joe Stringer 已提交
695 696 697 698 699
			break;
#ifdef CONFIG_NF_CONNTRACK_ZONES
		case OVS_CT_ATTR_ZONE:
			info->zone.id = nla_get_u16(a);
			break;
700 701 702 703 704
#endif
#ifdef CONFIG_NF_CONNTRACK_MARK
		case OVS_CT_ATTR_MARK: {
			struct md_mark *mark = nla_data(a);

705 706 707 708
			if (!mark->mask) {
				OVS_NLERR(log, "ct_mark mask cannot be 0");
				return -EINVAL;
			}
709 710 711
			info->mark = *mark;
			break;
		}
712 713
#endif
#ifdef CONFIG_NF_CONNTRACK_LABELS
J
Joe Stringer 已提交
714 715
		case OVS_CT_ATTR_LABELS: {
			struct md_labels *labels = nla_data(a);
716

717 718 719 720
			if (!labels_nonzero(&labels->mask)) {
				OVS_NLERR(log, "ct_labels mask cannot be 0");
				return -EINVAL;
			}
J
Joe Stringer 已提交
721
			info->labels = *labels;
722 723
			break;
		}
J
Joe Stringer 已提交
724
#endif
725 726 727 728 729 730 731
		case OVS_CT_ATTR_HELPER:
			*helper = nla_data(a);
			if (!memchr(*helper, '\0', nla_len(a))) {
				OVS_NLERR(log, "Invalid conntrack helper");
				return -EINVAL;
			}
			break;
J
Joe Stringer 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
		default:
			OVS_NLERR(log, "Unknown conntrack attr (%d)",
				  type);
			return -EINVAL;
		}
	}

	if (rem > 0) {
		OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
		return -EINVAL;
	}

	return 0;
}

747
bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
J
Joe Stringer 已提交
748 749 750 751 752 753
{
	if (attr == OVS_KEY_ATTR_CT_STATE)
		return true;
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
	    attr == OVS_KEY_ATTR_CT_ZONE)
		return true;
754 755 756
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
	    attr == OVS_KEY_ATTR_CT_MARK)
		return true;
757
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
J
Joe Stringer 已提交
758
	    attr == OVS_KEY_ATTR_CT_LABELS) {
759 760 761 762
		struct ovs_net *ovs_net = net_generic(net, ovs_net_id);

		return ovs_net->xt_label;
	}
J
Joe Stringer 已提交
763 764 765 766 767 768 769 770 771

	return false;
}

int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
		       const struct sw_flow_key *key,
		       struct sw_flow_actions **sfa,  bool log)
{
	struct ovs_conntrack_info ct_info;
772
	const char *helper = NULL;
J
Joe Stringer 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	u16 family;
	int err;

	family = key_to_nfproto(key);
	if (family == NFPROTO_UNSPEC) {
		OVS_NLERR(log, "ct family unspecified");
		return -EINVAL;
	}

	memset(&ct_info, 0, sizeof(ct_info));
	ct_info.family = family;

	nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
			NF_CT_DEFAULT_ZONE_DIR, 0);

788
	err = parse_ct(attr, &ct_info, &helper, log);
J
Joe Stringer 已提交
789 790 791 792 793 794 795 796 797
	if (err)
		return err;

	/* Set up template for tracking connections in specific zones. */
	ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
	if (!ct_info.ct) {
		OVS_NLERR(log, "Failed to allocate conntrack template");
		return -ENOMEM;
	}
798 799 800 801

	__set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
	nf_conntrack_get(&ct_info.ct->ct_general);

802 803 804 805 806
	if (helper) {
		err = ovs_ct_add_helper(&ct_info, helper, key, log);
		if (err)
			goto err_free_ct;
	}
J
Joe Stringer 已提交
807 808 809 810 811 812 813 814

	err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
				 sizeof(ct_info), log);
	if (err)
		goto err_free_ct;

	return 0;
err_free_ct:
815
	__ovs_ct_free_action(&ct_info);
J
Joe Stringer 已提交
816 817 818 819 820 821 822 823 824 825 826 827
	return err;
}

int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
			  struct sk_buff *skb)
{
	struct nlattr *start;

	start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
	if (!start)
		return -EMSGSIZE;

828
	if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
J
Joe Stringer 已提交
829 830 831 832
		return -EMSGSIZE;
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
	    nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
		return -EMSGSIZE;
833
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
834 835 836
	    nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
		    &ct_info->mark))
		return -EMSGSIZE;
837
	if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
838
	    labels_nonzero(&ct_info->labels.mask) &&
J
Joe Stringer 已提交
839 840
	    nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
		    &ct_info->labels))
841
		return -EMSGSIZE;
842 843 844 845 846
	if (ct_info->helper) {
		if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
				   ct_info->helper->name))
			return -EMSGSIZE;
	}
J
Joe Stringer 已提交
847 848 849 850 851 852 853 854 855 856

	nla_nest_end(skb, start);

	return 0;
}

void ovs_ct_free_action(const struct nlattr *a)
{
	struct ovs_conntrack_info *ct_info = nla_data(a);

857 858 859 860 861
	__ovs_ct_free_action(ct_info);
}

static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
{
862 863
	if (ct_info->helper)
		module_put(ct_info->helper->me);
J
Joe Stringer 已提交
864 865 866
	if (ct_info->ct)
		nf_ct_put(ct_info->ct);
}
867 868 869

void ovs_ct_init(struct net *net)
{
J
Joe Stringer 已提交
870
	unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);

	if (nf_connlabels_get(net, n_bits)) {
		ovs_net->xt_label = false;
		OVS_NLERR(true, "Failed to set connlabel length");
	} else {
		ovs_net->xt_label = true;
	}
}

void ovs_ct_exit(struct net *net)
{
	struct ovs_net *ovs_net = net_generic(net, ovs_net_id);

	if (ovs_net->xt_label)
		nf_connlabels_put(net);
}