conntrack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright (c) 2015 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/openvswitch.h>
  15. #include <net/ip.h>
  16. #include <net/netfilter/nf_conntrack_core.h>
  17. #include <net/netfilter/nf_conntrack_helper.h>
  18. #include <net/netfilter/nf_conntrack_labels.h>
  19. #include <net/netfilter/nf_conntrack_zones.h>
  20. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  21. #include "datapath.h"
  22. #include "conntrack.h"
  23. #include "flow.h"
  24. #include "flow_netlink.h"
  25. struct ovs_ct_len_tbl {
  26. size_t maxlen;
  27. size_t minlen;
  28. };
  29. /* Metadata mark for masked write to conntrack mark */
  30. struct md_mark {
  31. u32 value;
  32. u32 mask;
  33. };
  34. /* Metadata label for masked write to conntrack label. */
  35. struct md_label {
  36. struct ovs_key_ct_label value;
  37. struct ovs_key_ct_label mask;
  38. };
  39. /* Conntrack action context for execution. */
  40. struct ovs_conntrack_info {
  41. struct nf_conntrack_helper *helper;
  42. struct nf_conntrack_zone zone;
  43. struct nf_conn *ct;
  44. u32 flags;
  45. u16 family;
  46. struct md_mark mark;
  47. struct md_label label;
  48. };
  49. static u16 key_to_nfproto(const struct sw_flow_key *key)
  50. {
  51. switch (ntohs(key->eth.type)) {
  52. case ETH_P_IP:
  53. return NFPROTO_IPV4;
  54. case ETH_P_IPV6:
  55. return NFPROTO_IPV6;
  56. default:
  57. return NFPROTO_UNSPEC;
  58. }
  59. }
  60. /* Map SKB connection state into the values used by flow definition. */
  61. static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
  62. {
  63. u8 ct_state = OVS_CS_F_TRACKED;
  64. switch (ctinfo) {
  65. case IP_CT_ESTABLISHED_REPLY:
  66. case IP_CT_RELATED_REPLY:
  67. case IP_CT_NEW_REPLY:
  68. ct_state |= OVS_CS_F_REPLY_DIR;
  69. break;
  70. default:
  71. break;
  72. }
  73. switch (ctinfo) {
  74. case IP_CT_ESTABLISHED:
  75. case IP_CT_ESTABLISHED_REPLY:
  76. ct_state |= OVS_CS_F_ESTABLISHED;
  77. break;
  78. case IP_CT_RELATED:
  79. case IP_CT_RELATED_REPLY:
  80. ct_state |= OVS_CS_F_RELATED;
  81. break;
  82. case IP_CT_NEW:
  83. case IP_CT_NEW_REPLY:
  84. ct_state |= OVS_CS_F_NEW;
  85. break;
  86. default:
  87. break;
  88. }
  89. return ct_state;
  90. }
  91. static u32 ovs_ct_get_mark(const struct nf_conn *ct)
  92. {
  93. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  94. return ct ? ct->mark : 0;
  95. #else
  96. return 0;
  97. #endif
  98. }
  99. static void ovs_ct_get_label(const struct nf_conn *ct,
  100. struct ovs_key_ct_label *label)
  101. {
  102. struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
  103. if (cl) {
  104. size_t len = cl->words * sizeof(long);
  105. if (len > OVS_CT_LABEL_LEN)
  106. len = OVS_CT_LABEL_LEN;
  107. else if (len < OVS_CT_LABEL_LEN)
  108. memset(label, 0, OVS_CT_LABEL_LEN);
  109. memcpy(label, cl->bits, len);
  110. } else {
  111. memset(label, 0, OVS_CT_LABEL_LEN);
  112. }
  113. }
  114. static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
  115. const struct nf_conntrack_zone *zone,
  116. const struct nf_conn *ct)
  117. {
  118. key->ct.state = state;
  119. key->ct.zone = zone->id;
  120. key->ct.mark = ovs_ct_get_mark(ct);
  121. ovs_ct_get_label(ct, &key->ct.label);
  122. }
  123. /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
  124. * previously sent the packet to conntrack via the ct action.
  125. */
  126. static void ovs_ct_update_key(const struct sk_buff *skb,
  127. struct sw_flow_key *key, bool post_ct)
  128. {
  129. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  130. enum ip_conntrack_info ctinfo;
  131. struct nf_conn *ct;
  132. u8 state = 0;
  133. ct = nf_ct_get(skb, &ctinfo);
  134. if (ct) {
  135. state = ovs_ct_get_state(ctinfo);
  136. if (ct->master)
  137. state |= OVS_CS_F_RELATED;
  138. zone = nf_ct_zone(ct);
  139. } else if (post_ct) {
  140. state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
  141. }
  142. __ovs_ct_update_key(key, state, zone, ct);
  143. }
  144. void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
  145. {
  146. ovs_ct_update_key(skb, key, false);
  147. }
  148. int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
  149. {
  150. if (nla_put_u8(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
  151. return -EMSGSIZE;
  152. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  153. nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
  154. return -EMSGSIZE;
  155. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  156. nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
  157. return -EMSGSIZE;
  158. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  159. nla_put(skb, OVS_KEY_ATTR_CT_LABEL, sizeof(key->ct.label),
  160. &key->ct.label))
  161. return -EMSGSIZE;
  162. return 0;
  163. }
  164. static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
  165. u32 ct_mark, u32 mask)
  166. {
  167. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  168. enum ip_conntrack_info ctinfo;
  169. struct nf_conn *ct;
  170. u32 new_mark;
  171. /* The connection could be invalid, in which case set_mark is no-op. */
  172. ct = nf_ct_get(skb, &ctinfo);
  173. if (!ct)
  174. return 0;
  175. new_mark = ct_mark | (ct->mark & ~(mask));
  176. if (ct->mark != new_mark) {
  177. ct->mark = new_mark;
  178. nf_conntrack_event_cache(IPCT_MARK, ct);
  179. key->ct.mark = new_mark;
  180. }
  181. return 0;
  182. #else
  183. return -ENOTSUPP;
  184. #endif
  185. }
  186. static int ovs_ct_set_label(struct sk_buff *skb, struct sw_flow_key *key,
  187. const struct ovs_key_ct_label *label,
  188. const struct ovs_key_ct_label *mask)
  189. {
  190. enum ip_conntrack_info ctinfo;
  191. struct nf_conn_labels *cl;
  192. struct nf_conn *ct;
  193. int err;
  194. if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS))
  195. return -ENOTSUPP;
  196. /* The connection could be invalid, in which case set_label is no-op.*/
  197. ct = nf_ct_get(skb, &ctinfo);
  198. if (!ct)
  199. return 0;
  200. cl = nf_ct_labels_find(ct);
  201. if (!cl) {
  202. nf_ct_labels_ext_add(ct);
  203. cl = nf_ct_labels_find(ct);
  204. }
  205. if (!cl || cl->words * sizeof(long) < OVS_CT_LABEL_LEN)
  206. return -ENOSPC;
  207. err = nf_connlabels_replace(ct, (u32 *)label, (u32 *)mask,
  208. OVS_CT_LABEL_LEN / sizeof(u32));
  209. if (err)
  210. return err;
  211. ovs_ct_get_label(ct, &key->ct.label);
  212. return 0;
  213. }
  214. /* 'skb' should already be pulled to nh_ofs. */
  215. static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
  216. {
  217. const struct nf_conntrack_helper *helper;
  218. const struct nf_conn_help *help;
  219. enum ip_conntrack_info ctinfo;
  220. unsigned int protoff;
  221. struct nf_conn *ct;
  222. ct = nf_ct_get(skb, &ctinfo);
  223. if (!ct || ctinfo == IP_CT_RELATED_REPLY)
  224. return NF_ACCEPT;
  225. help = nfct_help(ct);
  226. if (!help)
  227. return NF_ACCEPT;
  228. helper = rcu_dereference(help->helper);
  229. if (!helper)
  230. return NF_ACCEPT;
  231. switch (proto) {
  232. case NFPROTO_IPV4:
  233. protoff = ip_hdrlen(skb);
  234. break;
  235. case NFPROTO_IPV6: {
  236. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  237. __be16 frag_off;
  238. protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
  239. &nexthdr, &frag_off);
  240. if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
  241. pr_debug("proto header not found\n");
  242. return NF_ACCEPT;
  243. }
  244. break;
  245. }
  246. default:
  247. WARN_ONCE(1, "helper invoked on non-IP family!");
  248. return NF_DROP;
  249. }
  250. return helper->help(skb, protoff, ct, ctinfo);
  251. }
  252. static int handle_fragments(struct net *net, struct sw_flow_key *key,
  253. u16 zone, struct sk_buff *skb)
  254. {
  255. struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
  256. if (key->eth.type == htons(ETH_P_IP)) {
  257. enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
  258. int err;
  259. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  260. err = ip_defrag(skb, user);
  261. if (err)
  262. return err;
  263. ovs_cb.mru = IPCB(skb)->frag_max_size;
  264. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  265. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
  266. enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
  267. struct sk_buff *reasm;
  268. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  269. reasm = nf_ct_frag6_gather(skb, user);
  270. if (!reasm)
  271. return -EINPROGRESS;
  272. if (skb == reasm)
  273. return -EINVAL;
  274. key->ip.proto = ipv6_hdr(reasm)->nexthdr;
  275. skb_morph(skb, reasm);
  276. consume_skb(reasm);
  277. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  278. #else
  279. return -EPFNOSUPPORT;
  280. #endif
  281. } else {
  282. return -EPFNOSUPPORT;
  283. }
  284. key->ip.frag = OVS_FRAG_TYPE_NONE;
  285. skb_clear_hash(skb);
  286. skb->ignore_df = 1;
  287. *OVS_CB(skb) = ovs_cb;
  288. return 0;
  289. }
  290. static struct nf_conntrack_expect *
  291. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  292. u16 proto, const struct sk_buff *skb)
  293. {
  294. struct nf_conntrack_tuple tuple;
  295. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
  296. return NULL;
  297. return __nf_ct_expect_find(net, zone, &tuple);
  298. }
  299. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  300. static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
  301. const struct ovs_conntrack_info *info)
  302. {
  303. enum ip_conntrack_info ctinfo;
  304. struct nf_conn *ct;
  305. ct = nf_ct_get(skb, &ctinfo);
  306. if (!ct)
  307. return false;
  308. if (!net_eq(net, read_pnet(&ct->ct_net)))
  309. return false;
  310. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  311. return false;
  312. if (info->helper) {
  313. struct nf_conn_help *help;
  314. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  315. if (help && rcu_access_pointer(help->helper) != info->helper)
  316. return false;
  317. }
  318. return true;
  319. }
  320. static int __ovs_ct_lookup(struct net *net, const struct sw_flow_key *key,
  321. const struct ovs_conntrack_info *info,
  322. struct sk_buff *skb)
  323. {
  324. /* If we are recirculating packets to match on conntrack fields and
  325. * committing with a separate conntrack action, then we don't need to
  326. * actually run the packet through conntrack twice unless it's for a
  327. * different zone.
  328. */
  329. if (!skb_nfct_cached(net, skb, info)) {
  330. struct nf_conn *tmpl = info->ct;
  331. /* Associate skb with specified zone. */
  332. if (tmpl) {
  333. if (skb->nfct)
  334. nf_conntrack_put(skb->nfct);
  335. nf_conntrack_get(&tmpl->ct_general);
  336. skb->nfct = &tmpl->ct_general;
  337. skb->nfctinfo = IP_CT_NEW;
  338. }
  339. if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
  340. skb) != NF_ACCEPT)
  341. return -ENOENT;
  342. if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  343. WARN_ONCE(1, "helper rejected packet");
  344. return -EINVAL;
  345. }
  346. }
  347. return 0;
  348. }
  349. /* Lookup connection and read fields into key. */
  350. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  351. const struct ovs_conntrack_info *info,
  352. struct sk_buff *skb)
  353. {
  354. struct nf_conntrack_expect *exp;
  355. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  356. if (exp) {
  357. u8 state;
  358. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  359. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  360. } else {
  361. int err;
  362. err = __ovs_ct_lookup(net, key, info, skb);
  363. if (err)
  364. return err;
  365. ovs_ct_update_key(skb, key, true);
  366. }
  367. return 0;
  368. }
  369. /* Lookup connection and confirm if unconfirmed. */
  370. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  371. const struct ovs_conntrack_info *info,
  372. struct sk_buff *skb)
  373. {
  374. u8 state;
  375. int err;
  376. state = key->ct.state;
  377. if (key->ct.zone == info->zone.id &&
  378. ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
  379. /* Previous lookup has shown that this connection is already
  380. * tracked and committed. Skip committing.
  381. */
  382. return 0;
  383. }
  384. err = __ovs_ct_lookup(net, key, info, skb);
  385. if (err)
  386. return err;
  387. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  388. return -EINVAL;
  389. ovs_ct_update_key(skb, key, true);
  390. return 0;
  391. }
  392. static bool label_nonzero(const struct ovs_key_ct_label *label)
  393. {
  394. size_t i;
  395. for (i = 0; i < sizeof(*label); i++)
  396. if (label->ct_label[i])
  397. return true;
  398. return false;
  399. }
  400. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  401. struct sw_flow_key *key,
  402. const struct ovs_conntrack_info *info)
  403. {
  404. int nh_ofs;
  405. int err;
  406. /* The conntrack module expects to be working at L3. */
  407. nh_ofs = skb_network_offset(skb);
  408. skb_pull(skb, nh_ofs);
  409. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  410. err = handle_fragments(net, key, info->zone.id, skb);
  411. if (err)
  412. return err;
  413. }
  414. if (info->flags & OVS_CT_F_COMMIT)
  415. err = ovs_ct_commit(net, key, info, skb);
  416. else
  417. err = ovs_ct_lookup(net, key, info, skb);
  418. if (err)
  419. goto err;
  420. if (info->mark.mask) {
  421. err = ovs_ct_set_mark(skb, key, info->mark.value,
  422. info->mark.mask);
  423. if (err)
  424. goto err;
  425. }
  426. if (label_nonzero(&info->label.mask))
  427. err = ovs_ct_set_label(skb, key, &info->label.value,
  428. &info->label.mask);
  429. err:
  430. skb_push(skb, nh_ofs);
  431. return err;
  432. }
  433. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  434. const struct sw_flow_key *key, bool log)
  435. {
  436. struct nf_conntrack_helper *helper;
  437. struct nf_conn_help *help;
  438. helper = nf_conntrack_helper_try_module_get(name, info->family,
  439. key->ip.proto);
  440. if (!helper) {
  441. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  442. return -EINVAL;
  443. }
  444. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  445. if (!help) {
  446. module_put(helper->me);
  447. return -ENOMEM;
  448. }
  449. rcu_assign_pointer(help->helper, helper);
  450. info->helper = helper;
  451. return 0;
  452. }
  453. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  454. [OVS_CT_ATTR_FLAGS] = { .minlen = sizeof(u32),
  455. .maxlen = sizeof(u32) },
  456. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  457. .maxlen = sizeof(u16) },
  458. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  459. .maxlen = sizeof(struct md_mark) },
  460. [OVS_CT_ATTR_LABEL] = { .minlen = sizeof(struct md_label),
  461. .maxlen = sizeof(struct md_label) },
  462. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  463. .maxlen = NF_CT_HELPER_NAME_LEN }
  464. };
  465. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  466. const char **helper, bool log)
  467. {
  468. struct nlattr *a;
  469. int rem;
  470. nla_for_each_nested(a, attr, rem) {
  471. int type = nla_type(a);
  472. int maxlen = ovs_ct_attr_lens[type].maxlen;
  473. int minlen = ovs_ct_attr_lens[type].minlen;
  474. if (type > OVS_CT_ATTR_MAX) {
  475. OVS_NLERR(log,
  476. "Unknown conntrack attr (type=%d, max=%d)",
  477. type, OVS_CT_ATTR_MAX);
  478. return -EINVAL;
  479. }
  480. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  481. OVS_NLERR(log,
  482. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  483. type, nla_len(a), maxlen);
  484. return -EINVAL;
  485. }
  486. switch (type) {
  487. case OVS_CT_ATTR_FLAGS:
  488. info->flags = nla_get_u32(a);
  489. break;
  490. #ifdef CONFIG_NF_CONNTRACK_ZONES
  491. case OVS_CT_ATTR_ZONE:
  492. info->zone.id = nla_get_u16(a);
  493. break;
  494. #endif
  495. #ifdef CONFIG_NF_CONNTRACK_MARK
  496. case OVS_CT_ATTR_MARK: {
  497. struct md_mark *mark = nla_data(a);
  498. info->mark = *mark;
  499. break;
  500. }
  501. #endif
  502. #ifdef CONFIG_NF_CONNTRACK_LABELS
  503. case OVS_CT_ATTR_LABEL: {
  504. struct md_label *label = nla_data(a);
  505. info->label = *label;
  506. break;
  507. }
  508. #endif
  509. case OVS_CT_ATTR_HELPER:
  510. *helper = nla_data(a);
  511. if (!memchr(*helper, '\0', nla_len(a))) {
  512. OVS_NLERR(log, "Invalid conntrack helper");
  513. return -EINVAL;
  514. }
  515. break;
  516. default:
  517. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  518. type);
  519. return -EINVAL;
  520. }
  521. }
  522. if (rem > 0) {
  523. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  524. return -EINVAL;
  525. }
  526. return 0;
  527. }
  528. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  529. {
  530. if (attr == OVS_KEY_ATTR_CT_STATE)
  531. return true;
  532. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  533. attr == OVS_KEY_ATTR_CT_ZONE)
  534. return true;
  535. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  536. attr == OVS_KEY_ATTR_CT_MARK)
  537. return true;
  538. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  539. attr == OVS_KEY_ATTR_CT_LABEL) {
  540. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  541. return ovs_net->xt_label;
  542. }
  543. return false;
  544. }
  545. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  546. const struct sw_flow_key *key,
  547. struct sw_flow_actions **sfa, bool log)
  548. {
  549. struct ovs_conntrack_info ct_info;
  550. const char *helper = NULL;
  551. u16 family;
  552. int err;
  553. family = key_to_nfproto(key);
  554. if (family == NFPROTO_UNSPEC) {
  555. OVS_NLERR(log, "ct family unspecified");
  556. return -EINVAL;
  557. }
  558. memset(&ct_info, 0, sizeof(ct_info));
  559. ct_info.family = family;
  560. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  561. NF_CT_DEFAULT_ZONE_DIR, 0);
  562. err = parse_ct(attr, &ct_info, &helper, log);
  563. if (err)
  564. return err;
  565. /* Set up template for tracking connections in specific zones. */
  566. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  567. if (!ct_info.ct) {
  568. OVS_NLERR(log, "Failed to allocate conntrack template");
  569. return -ENOMEM;
  570. }
  571. if (helper) {
  572. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  573. if (err)
  574. goto err_free_ct;
  575. }
  576. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  577. sizeof(ct_info), log);
  578. if (err)
  579. goto err_free_ct;
  580. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  581. nf_conntrack_get(&ct_info.ct->ct_general);
  582. return 0;
  583. err_free_ct:
  584. nf_conntrack_free(ct_info.ct);
  585. return err;
  586. }
  587. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  588. struct sk_buff *skb)
  589. {
  590. struct nlattr *start;
  591. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  592. if (!start)
  593. return -EMSGSIZE;
  594. if (nla_put_u32(skb, OVS_CT_ATTR_FLAGS, ct_info->flags))
  595. return -EMSGSIZE;
  596. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  597. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  598. return -EMSGSIZE;
  599. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  600. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  601. &ct_info->mark))
  602. return -EMSGSIZE;
  603. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  604. nla_put(skb, OVS_CT_ATTR_LABEL, sizeof(ct_info->label),
  605. &ct_info->label))
  606. return -EMSGSIZE;
  607. if (ct_info->helper) {
  608. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  609. ct_info->helper->name))
  610. return -EMSGSIZE;
  611. }
  612. nla_nest_end(skb, start);
  613. return 0;
  614. }
  615. void ovs_ct_free_action(const struct nlattr *a)
  616. {
  617. struct ovs_conntrack_info *ct_info = nla_data(a);
  618. if (ct_info->helper)
  619. module_put(ct_info->helper->me);
  620. if (ct_info->ct)
  621. nf_ct_put(ct_info->ct);
  622. }
  623. void ovs_ct_init(struct net *net)
  624. {
  625. unsigned int n_bits = sizeof(struct ovs_key_ct_label) * BITS_PER_BYTE;
  626. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  627. if (nf_connlabels_get(net, n_bits)) {
  628. ovs_net->xt_label = false;
  629. OVS_NLERR(true, "Failed to set connlabel length");
  630. } else {
  631. ovs_net->xt_label = true;
  632. }
  633. }
  634. void ovs_ct_exit(struct net *net)
  635. {
  636. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  637. if (ovs_net->xt_label)
  638. nf_connlabels_put(net);
  639. }