conntrack.c 18 KB

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