conntrack.c 19 KB

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