conntrack.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. int ofs;
  239. ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
  240. &frag_off);
  241. if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
  242. pr_debug("proto header not found\n");
  243. return NF_ACCEPT;
  244. }
  245. protoff = ofs;
  246. break;
  247. }
  248. default:
  249. WARN_ONCE(1, "helper invoked on non-IP family!");
  250. return NF_DROP;
  251. }
  252. return helper->help(skb, protoff, ct, ctinfo);
  253. }
  254. static int handle_fragments(struct net *net, struct sw_flow_key *key,
  255. u16 zone, struct sk_buff *skb)
  256. {
  257. struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
  258. if (key->eth.type == htons(ETH_P_IP)) {
  259. enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
  260. int err;
  261. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  262. err = ip_defrag(skb, user);
  263. if (err)
  264. return err;
  265. ovs_cb.mru = IPCB(skb)->frag_max_size;
  266. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  267. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
  268. enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
  269. struct sk_buff *reasm;
  270. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  271. reasm = nf_ct_frag6_gather(skb, user);
  272. if (!reasm)
  273. return -EINPROGRESS;
  274. if (skb == reasm)
  275. return -EINVAL;
  276. key->ip.proto = ipv6_hdr(reasm)->nexthdr;
  277. skb_morph(skb, reasm);
  278. consume_skb(reasm);
  279. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  280. #else
  281. return -EPFNOSUPPORT;
  282. #endif
  283. } else {
  284. return -EPFNOSUPPORT;
  285. }
  286. key->ip.frag = OVS_FRAG_TYPE_NONE;
  287. skb_clear_hash(skb);
  288. skb->ignore_df = 1;
  289. *OVS_CB(skb) = ovs_cb;
  290. return 0;
  291. }
  292. static struct nf_conntrack_expect *
  293. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  294. u16 proto, const struct sk_buff *skb)
  295. {
  296. struct nf_conntrack_tuple tuple;
  297. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, &tuple))
  298. return NULL;
  299. return __nf_ct_expect_find(net, zone, &tuple);
  300. }
  301. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  302. static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb,
  303. const struct ovs_conntrack_info *info)
  304. {
  305. enum ip_conntrack_info ctinfo;
  306. struct nf_conn *ct;
  307. ct = nf_ct_get(skb, &ctinfo);
  308. if (!ct)
  309. return false;
  310. if (!net_eq(net, read_pnet(&ct->ct_net)))
  311. return false;
  312. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  313. return false;
  314. if (info->helper) {
  315. struct nf_conn_help *help;
  316. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  317. if (help && rcu_access_pointer(help->helper) != info->helper)
  318. return false;
  319. }
  320. return true;
  321. }
  322. static int __ovs_ct_lookup(struct net *net, const struct sw_flow_key *key,
  323. const struct ovs_conntrack_info *info,
  324. struct sk_buff *skb)
  325. {
  326. /* If we are recirculating packets to match on conntrack fields and
  327. * committing with a separate conntrack action, then we don't need to
  328. * actually run the packet through conntrack twice unless it's for a
  329. * different zone.
  330. */
  331. if (!skb_nfct_cached(net, skb, info)) {
  332. struct nf_conn *tmpl = info->ct;
  333. /* Associate skb with specified zone. */
  334. if (tmpl) {
  335. if (skb->nfct)
  336. nf_conntrack_put(skb->nfct);
  337. nf_conntrack_get(&tmpl->ct_general);
  338. skb->nfct = &tmpl->ct_general;
  339. skb->nfctinfo = IP_CT_NEW;
  340. }
  341. if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING,
  342. skb) != NF_ACCEPT)
  343. return -ENOENT;
  344. if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  345. WARN_ONCE(1, "helper rejected packet");
  346. return -EINVAL;
  347. }
  348. }
  349. return 0;
  350. }
  351. /* Lookup connection and read fields into key. */
  352. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  353. const struct ovs_conntrack_info *info,
  354. struct sk_buff *skb)
  355. {
  356. struct nf_conntrack_expect *exp;
  357. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  358. if (exp) {
  359. u8 state;
  360. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  361. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  362. } else {
  363. int err;
  364. err = __ovs_ct_lookup(net, key, info, skb);
  365. if (err)
  366. return err;
  367. ovs_ct_update_key(skb, key, true);
  368. }
  369. return 0;
  370. }
  371. /* Lookup connection and confirm if unconfirmed. */
  372. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  373. const struct ovs_conntrack_info *info,
  374. struct sk_buff *skb)
  375. {
  376. u8 state;
  377. int err;
  378. state = key->ct.state;
  379. if (key->ct.zone == info->zone.id &&
  380. ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) {
  381. /* Previous lookup has shown that this connection is already
  382. * tracked and committed. Skip committing.
  383. */
  384. return 0;
  385. }
  386. err = __ovs_ct_lookup(net, key, info, skb);
  387. if (err)
  388. return err;
  389. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  390. return -EINVAL;
  391. ovs_ct_update_key(skb, key, true);
  392. return 0;
  393. }
  394. static bool label_nonzero(const struct ovs_key_ct_label *label)
  395. {
  396. size_t i;
  397. for (i = 0; i < sizeof(*label); i++)
  398. if (label->ct_label[i])
  399. return true;
  400. return false;
  401. }
  402. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  403. struct sw_flow_key *key,
  404. const struct ovs_conntrack_info *info)
  405. {
  406. int nh_ofs;
  407. int err;
  408. /* The conntrack module expects to be working at L3. */
  409. nh_ofs = skb_network_offset(skb);
  410. skb_pull(skb, nh_ofs);
  411. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  412. err = handle_fragments(net, key, info->zone.id, skb);
  413. if (err)
  414. return err;
  415. }
  416. if (info->flags & OVS_CT_F_COMMIT)
  417. err = ovs_ct_commit(net, key, info, skb);
  418. else
  419. err = ovs_ct_lookup(net, key, info, skb);
  420. if (err)
  421. goto err;
  422. if (info->mark.mask) {
  423. err = ovs_ct_set_mark(skb, key, info->mark.value,
  424. info->mark.mask);
  425. if (err)
  426. goto err;
  427. }
  428. if (label_nonzero(&info->label.mask))
  429. err = ovs_ct_set_label(skb, key, &info->label.value,
  430. &info->label.mask);
  431. err:
  432. skb_push(skb, nh_ofs);
  433. return err;
  434. }
  435. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  436. const struct sw_flow_key *key, bool log)
  437. {
  438. struct nf_conntrack_helper *helper;
  439. struct nf_conn_help *help;
  440. helper = nf_conntrack_helper_try_module_get(name, info->family,
  441. key->ip.proto);
  442. if (!helper) {
  443. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  444. return -EINVAL;
  445. }
  446. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  447. if (!help) {
  448. module_put(helper->me);
  449. return -ENOMEM;
  450. }
  451. rcu_assign_pointer(help->helper, helper);
  452. info->helper = helper;
  453. return 0;
  454. }
  455. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  456. [OVS_CT_ATTR_FLAGS] = { .minlen = sizeof(u32),
  457. .maxlen = sizeof(u32) },
  458. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  459. .maxlen = sizeof(u16) },
  460. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  461. .maxlen = sizeof(struct md_mark) },
  462. [OVS_CT_ATTR_LABEL] = { .minlen = sizeof(struct md_label),
  463. .maxlen = sizeof(struct md_label) },
  464. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  465. .maxlen = NF_CT_HELPER_NAME_LEN }
  466. };
  467. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  468. const char **helper, bool log)
  469. {
  470. struct nlattr *a;
  471. int rem;
  472. nla_for_each_nested(a, attr, rem) {
  473. int type = nla_type(a);
  474. int maxlen = ovs_ct_attr_lens[type].maxlen;
  475. int minlen = ovs_ct_attr_lens[type].minlen;
  476. if (type > OVS_CT_ATTR_MAX) {
  477. OVS_NLERR(log,
  478. "Unknown conntrack attr (type=%d, max=%d)",
  479. type, OVS_CT_ATTR_MAX);
  480. return -EINVAL;
  481. }
  482. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  483. OVS_NLERR(log,
  484. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  485. type, nla_len(a), maxlen);
  486. return -EINVAL;
  487. }
  488. switch (type) {
  489. case OVS_CT_ATTR_FLAGS:
  490. info->flags = nla_get_u32(a);
  491. break;
  492. #ifdef CONFIG_NF_CONNTRACK_ZONES
  493. case OVS_CT_ATTR_ZONE:
  494. info->zone.id = nla_get_u16(a);
  495. break;
  496. #endif
  497. #ifdef CONFIG_NF_CONNTRACK_MARK
  498. case OVS_CT_ATTR_MARK: {
  499. struct md_mark *mark = nla_data(a);
  500. info->mark = *mark;
  501. break;
  502. }
  503. #endif
  504. #ifdef CONFIG_NF_CONNTRACK_LABELS
  505. case OVS_CT_ATTR_LABEL: {
  506. struct md_label *label = nla_data(a);
  507. info->label = *label;
  508. break;
  509. }
  510. #endif
  511. case OVS_CT_ATTR_HELPER:
  512. *helper = nla_data(a);
  513. if (!memchr(*helper, '\0', nla_len(a))) {
  514. OVS_NLERR(log, "Invalid conntrack helper");
  515. return -EINVAL;
  516. }
  517. break;
  518. default:
  519. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  520. type);
  521. return -EINVAL;
  522. }
  523. }
  524. if (rem > 0) {
  525. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  526. return -EINVAL;
  527. }
  528. return 0;
  529. }
  530. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  531. {
  532. if (attr == OVS_KEY_ATTR_CT_STATE)
  533. return true;
  534. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  535. attr == OVS_KEY_ATTR_CT_ZONE)
  536. return true;
  537. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  538. attr == OVS_KEY_ATTR_CT_MARK)
  539. return true;
  540. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  541. attr == OVS_KEY_ATTR_CT_LABEL) {
  542. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  543. return ovs_net->xt_label;
  544. }
  545. return false;
  546. }
  547. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  548. const struct sw_flow_key *key,
  549. struct sw_flow_actions **sfa, bool log)
  550. {
  551. struct ovs_conntrack_info ct_info;
  552. const char *helper = NULL;
  553. u16 family;
  554. int err;
  555. family = key_to_nfproto(key);
  556. if (family == NFPROTO_UNSPEC) {
  557. OVS_NLERR(log, "ct family unspecified");
  558. return -EINVAL;
  559. }
  560. memset(&ct_info, 0, sizeof(ct_info));
  561. ct_info.family = family;
  562. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  563. NF_CT_DEFAULT_ZONE_DIR, 0);
  564. err = parse_ct(attr, &ct_info, &helper, log);
  565. if (err)
  566. return err;
  567. /* Set up template for tracking connections in specific zones. */
  568. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  569. if (!ct_info.ct) {
  570. OVS_NLERR(log, "Failed to allocate conntrack template");
  571. return -ENOMEM;
  572. }
  573. if (helper) {
  574. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  575. if (err)
  576. goto err_free_ct;
  577. }
  578. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  579. sizeof(ct_info), log);
  580. if (err)
  581. goto err_free_ct;
  582. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  583. nf_conntrack_get(&ct_info.ct->ct_general);
  584. return 0;
  585. err_free_ct:
  586. nf_conntrack_free(ct_info.ct);
  587. return err;
  588. }
  589. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  590. struct sk_buff *skb)
  591. {
  592. struct nlattr *start;
  593. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  594. if (!start)
  595. return -EMSGSIZE;
  596. if (nla_put_u32(skb, OVS_CT_ATTR_FLAGS, ct_info->flags))
  597. return -EMSGSIZE;
  598. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  599. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  600. return -EMSGSIZE;
  601. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  602. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  603. &ct_info->mark))
  604. return -EMSGSIZE;
  605. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  606. nla_put(skb, OVS_CT_ATTR_LABEL, sizeof(ct_info->label),
  607. &ct_info->label))
  608. return -EMSGSIZE;
  609. if (ct_info->helper) {
  610. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  611. ct_info->helper->name))
  612. return -EMSGSIZE;
  613. }
  614. nla_nest_end(skb, start);
  615. return 0;
  616. }
  617. void ovs_ct_free_action(const struct nlattr *a)
  618. {
  619. struct ovs_conntrack_info *ct_info = nla_data(a);
  620. if (ct_info->helper)
  621. module_put(ct_info->helper->me);
  622. if (ct_info->ct)
  623. nf_ct_put(ct_info->ct);
  624. }
  625. void ovs_ct_init(struct net *net)
  626. {
  627. unsigned int n_bits = sizeof(struct ovs_key_ct_label) * BITS_PER_BYTE;
  628. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  629. if (nf_connlabels_get(net, n_bits)) {
  630. ovs_net->xt_label = false;
  631. OVS_NLERR(true, "Failed to set connlabel length");
  632. } else {
  633. ovs_net->xt_label = true;
  634. }
  635. }
  636. void ovs_ct_exit(struct net *net)
  637. {
  638. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  639. if (ovs_net->xt_label)
  640. nf_connlabels_put(net);
  641. }