conntrack.c 20 KB

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