conntrack.c 18 KB

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