conntrack.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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 <linux/tcp.h>
  16. #include <linux/udp.h>
  17. #include <linux/sctp.h>
  18. #include <net/ip.h>
  19. #include <net/netfilter/nf_conntrack_core.h>
  20. #include <net/netfilter/nf_conntrack_helper.h>
  21. #include <net/netfilter/nf_conntrack_labels.h>
  22. #include <net/netfilter/nf_conntrack_seqadj.h>
  23. #include <net/netfilter/nf_conntrack_zones.h>
  24. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  25. #ifdef CONFIG_NF_NAT_NEEDED
  26. #include <linux/netfilter/nf_nat.h>
  27. #include <net/netfilter/nf_nat_core.h>
  28. #include <net/netfilter/nf_nat_l3proto.h>
  29. #endif
  30. #include "datapath.h"
  31. #include "conntrack.h"
  32. #include "flow.h"
  33. #include "flow_netlink.h"
  34. struct ovs_ct_len_tbl {
  35. int maxlen;
  36. int minlen;
  37. };
  38. /* Metadata mark for masked write to conntrack mark */
  39. struct md_mark {
  40. u32 value;
  41. u32 mask;
  42. };
  43. /* Metadata label for masked write to conntrack label. */
  44. struct md_labels {
  45. struct ovs_key_ct_labels value;
  46. struct ovs_key_ct_labels mask;
  47. };
  48. enum ovs_ct_nat {
  49. OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
  50. OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
  51. OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
  52. };
  53. /* Conntrack action context for execution. */
  54. struct ovs_conntrack_info {
  55. struct nf_conntrack_helper *helper;
  56. struct nf_conntrack_zone zone;
  57. struct nf_conn *ct;
  58. u8 commit : 1;
  59. u8 nat : 3; /* enum ovs_ct_nat */
  60. u16 family;
  61. struct md_mark mark;
  62. struct md_labels labels;
  63. #ifdef CONFIG_NF_NAT_NEEDED
  64. struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
  65. #endif
  66. };
  67. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
  68. static u16 key_to_nfproto(const struct sw_flow_key *key)
  69. {
  70. switch (ntohs(key->eth.type)) {
  71. case ETH_P_IP:
  72. return NFPROTO_IPV4;
  73. case ETH_P_IPV6:
  74. return NFPROTO_IPV6;
  75. default:
  76. return NFPROTO_UNSPEC;
  77. }
  78. }
  79. /* Map SKB connection state into the values used by flow definition. */
  80. static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
  81. {
  82. u8 ct_state = OVS_CS_F_TRACKED;
  83. switch (ctinfo) {
  84. case IP_CT_ESTABLISHED_REPLY:
  85. case IP_CT_RELATED_REPLY:
  86. ct_state |= OVS_CS_F_REPLY_DIR;
  87. break;
  88. default:
  89. break;
  90. }
  91. switch (ctinfo) {
  92. case IP_CT_ESTABLISHED:
  93. case IP_CT_ESTABLISHED_REPLY:
  94. ct_state |= OVS_CS_F_ESTABLISHED;
  95. break;
  96. case IP_CT_RELATED:
  97. case IP_CT_RELATED_REPLY:
  98. ct_state |= OVS_CS_F_RELATED;
  99. break;
  100. case IP_CT_NEW:
  101. ct_state |= OVS_CS_F_NEW;
  102. break;
  103. default:
  104. break;
  105. }
  106. return ct_state;
  107. }
  108. static u32 ovs_ct_get_mark(const struct nf_conn *ct)
  109. {
  110. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  111. return ct ? ct->mark : 0;
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. static void ovs_ct_get_labels(const struct nf_conn *ct,
  117. struct ovs_key_ct_labels *labels)
  118. {
  119. struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
  120. if (cl) {
  121. size_t len = cl->words * sizeof(long);
  122. if (len > OVS_CT_LABELS_LEN)
  123. len = OVS_CT_LABELS_LEN;
  124. else if (len < OVS_CT_LABELS_LEN)
  125. memset(labels, 0, OVS_CT_LABELS_LEN);
  126. memcpy(labels, cl->bits, len);
  127. } else {
  128. memset(labels, 0, OVS_CT_LABELS_LEN);
  129. }
  130. }
  131. static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
  132. const struct nf_conntrack_zone *zone,
  133. const struct nf_conn *ct)
  134. {
  135. key->ct.state = state;
  136. key->ct.zone = zone->id;
  137. key->ct.mark = ovs_ct_get_mark(ct);
  138. ovs_ct_get_labels(ct, &key->ct.labels);
  139. }
  140. /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
  141. * previously sent the packet to conntrack via the ct action. If
  142. * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
  143. * initialized from the connection status.
  144. */
  145. static void ovs_ct_update_key(const struct sk_buff *skb,
  146. const struct ovs_conntrack_info *info,
  147. struct sw_flow_key *key, bool post_ct,
  148. bool keep_nat_flags)
  149. {
  150. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  151. enum ip_conntrack_info ctinfo;
  152. struct nf_conn *ct;
  153. u8 state = 0;
  154. ct = nf_ct_get(skb, &ctinfo);
  155. if (ct) {
  156. state = ovs_ct_get_state(ctinfo);
  157. /* All unconfirmed entries are NEW connections. */
  158. if (!nf_ct_is_confirmed(ct))
  159. state |= OVS_CS_F_NEW;
  160. /* OVS persists the related flag for the duration of the
  161. * connection.
  162. */
  163. if (ct->master)
  164. state |= OVS_CS_F_RELATED;
  165. if (keep_nat_flags) {
  166. state |= key->ct.state & OVS_CS_F_NAT_MASK;
  167. } else {
  168. if (ct->status & IPS_SRC_NAT)
  169. state |= OVS_CS_F_SRC_NAT;
  170. if (ct->status & IPS_DST_NAT)
  171. state |= OVS_CS_F_DST_NAT;
  172. }
  173. zone = nf_ct_zone(ct);
  174. } else if (post_ct) {
  175. state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
  176. if (info)
  177. zone = &info->zone;
  178. }
  179. __ovs_ct_update_key(key, state, zone, ct);
  180. }
  181. /* This is called to initialize CT key fields possibly coming in from the local
  182. * stack.
  183. */
  184. void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
  185. {
  186. ovs_ct_update_key(skb, NULL, key, false, false);
  187. }
  188. int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
  189. {
  190. if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
  191. return -EMSGSIZE;
  192. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  193. nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
  194. return -EMSGSIZE;
  195. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  196. nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
  197. return -EMSGSIZE;
  198. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  199. nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
  200. &key->ct.labels))
  201. return -EMSGSIZE;
  202. return 0;
  203. }
  204. static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
  205. u32 ct_mark, u32 mask)
  206. {
  207. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  208. enum ip_conntrack_info ctinfo;
  209. struct nf_conn *ct;
  210. u32 new_mark;
  211. /* The connection could be invalid, in which case set_mark is no-op. */
  212. ct = nf_ct_get(skb, &ctinfo);
  213. if (!ct)
  214. return 0;
  215. new_mark = ct_mark | (ct->mark & ~(mask));
  216. if (ct->mark != new_mark) {
  217. ct->mark = new_mark;
  218. nf_conntrack_event_cache(IPCT_MARK, ct);
  219. key->ct.mark = new_mark;
  220. }
  221. return 0;
  222. #else
  223. return -ENOTSUPP;
  224. #endif
  225. }
  226. static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
  227. const struct ovs_key_ct_labels *labels,
  228. const struct ovs_key_ct_labels *mask)
  229. {
  230. enum ip_conntrack_info ctinfo;
  231. struct nf_conn_labels *cl;
  232. struct nf_conn *ct;
  233. int err;
  234. /* The connection could be invalid, in which case set_label is no-op.*/
  235. ct = nf_ct_get(skb, &ctinfo);
  236. if (!ct)
  237. return 0;
  238. cl = nf_ct_labels_find(ct);
  239. if (!cl) {
  240. nf_ct_labels_ext_add(ct);
  241. cl = nf_ct_labels_find(ct);
  242. }
  243. if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN)
  244. return -ENOSPC;
  245. err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
  246. OVS_CT_LABELS_LEN / sizeof(u32));
  247. if (err)
  248. return err;
  249. ovs_ct_get_labels(ct, &key->ct.labels);
  250. return 0;
  251. }
  252. /* 'skb' should already be pulled to nh_ofs. */
  253. static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
  254. {
  255. const struct nf_conntrack_helper *helper;
  256. const struct nf_conn_help *help;
  257. enum ip_conntrack_info ctinfo;
  258. unsigned int protoff;
  259. struct nf_conn *ct;
  260. int err;
  261. ct = nf_ct_get(skb, &ctinfo);
  262. if (!ct || ctinfo == IP_CT_RELATED_REPLY)
  263. return NF_ACCEPT;
  264. help = nfct_help(ct);
  265. if (!help)
  266. return NF_ACCEPT;
  267. helper = rcu_dereference(help->helper);
  268. if (!helper)
  269. return NF_ACCEPT;
  270. switch (proto) {
  271. case NFPROTO_IPV4:
  272. protoff = ip_hdrlen(skb);
  273. break;
  274. case NFPROTO_IPV6: {
  275. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  276. __be16 frag_off;
  277. int ofs;
  278. ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
  279. &frag_off);
  280. if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
  281. pr_debug("proto header not found\n");
  282. return NF_ACCEPT;
  283. }
  284. protoff = ofs;
  285. break;
  286. }
  287. default:
  288. WARN_ONCE(1, "helper invoked on non-IP family!");
  289. return NF_DROP;
  290. }
  291. err = helper->help(skb, protoff, ct, ctinfo);
  292. if (err != NF_ACCEPT)
  293. return err;
  294. /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
  295. * FTP with NAT) adusting the TCP payload size when mangling IP
  296. * addresses and/or port numbers in the text-based control connection.
  297. */
  298. if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
  299. !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
  300. return NF_DROP;
  301. return NF_ACCEPT;
  302. }
  303. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  304. * value if 'skb' is freed.
  305. */
  306. static int handle_fragments(struct net *net, struct sw_flow_key *key,
  307. u16 zone, struct sk_buff *skb)
  308. {
  309. struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
  310. int err;
  311. if (key->eth.type == htons(ETH_P_IP)) {
  312. enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
  313. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  314. err = ip_defrag(net, skb, user);
  315. if (err)
  316. return err;
  317. ovs_cb.mru = IPCB(skb)->frag_max_size;
  318. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
  319. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  320. enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
  321. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  322. err = nf_ct_frag6_gather(net, skb, user);
  323. if (err)
  324. return err;
  325. key->ip.proto = ipv6_hdr(skb)->nexthdr;
  326. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  327. #endif
  328. } else {
  329. kfree_skb(skb);
  330. return -EPFNOSUPPORT;
  331. }
  332. key->ip.frag = OVS_FRAG_TYPE_NONE;
  333. skb_clear_hash(skb);
  334. skb->ignore_df = 1;
  335. *OVS_CB(skb) = ovs_cb;
  336. return 0;
  337. }
  338. static struct nf_conntrack_expect *
  339. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  340. u16 proto, const struct sk_buff *skb)
  341. {
  342. struct nf_conntrack_tuple tuple;
  343. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
  344. return NULL;
  345. return __nf_ct_expect_find(net, zone, &tuple);
  346. }
  347. /* This replicates logic from nf_conntrack_core.c that is not exported. */
  348. static enum ip_conntrack_info
  349. ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
  350. {
  351. const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
  352. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  353. return IP_CT_ESTABLISHED_REPLY;
  354. /* Once we've had two way comms, always ESTABLISHED. */
  355. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
  356. return IP_CT_ESTABLISHED;
  357. if (test_bit(IPS_EXPECTED_BIT, &ct->status))
  358. return IP_CT_RELATED;
  359. return IP_CT_NEW;
  360. }
  361. /* Find an existing connection which this packet belongs to without
  362. * re-attributing statistics or modifying the connection state. This allows an
  363. * skb->nfct lost due to an upcall to be recovered during actions execution.
  364. *
  365. * Must be called with rcu_read_lock.
  366. *
  367. * On success, populates skb->nfct and skb->nfctinfo, and returns the
  368. * connection. Returns NULL if there is no existing entry.
  369. */
  370. static struct nf_conn *
  371. ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
  372. u8 l3num, struct sk_buff *skb)
  373. {
  374. struct nf_conntrack_l3proto *l3proto;
  375. struct nf_conntrack_l4proto *l4proto;
  376. struct nf_conntrack_tuple tuple;
  377. struct nf_conntrack_tuple_hash *h;
  378. enum ip_conntrack_info ctinfo;
  379. struct nf_conn *ct;
  380. unsigned int dataoff;
  381. u8 protonum;
  382. l3proto = __nf_ct_l3proto_find(l3num);
  383. if (!l3proto) {
  384. pr_debug("ovs_ct_find_existing: Can't get l3proto\n");
  385. return NULL;
  386. }
  387. if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
  388. &protonum) <= 0) {
  389. pr_debug("ovs_ct_find_existing: Can't get protonum\n");
  390. return NULL;
  391. }
  392. l4proto = __nf_ct_l4proto_find(l3num, protonum);
  393. if (!l4proto) {
  394. pr_debug("ovs_ct_find_existing: Can't get l4proto\n");
  395. return NULL;
  396. }
  397. if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
  398. protonum, net, &tuple, l3proto, l4proto)) {
  399. pr_debug("ovs_ct_find_existing: Can't get tuple\n");
  400. return NULL;
  401. }
  402. /* look for tuple match */
  403. h = nf_conntrack_find_get(net, zone, &tuple);
  404. if (!h)
  405. return NULL; /* Not found. */
  406. ct = nf_ct_tuplehash_to_ctrack(h);
  407. ctinfo = ovs_ct_get_info(h);
  408. if (ctinfo == IP_CT_NEW) {
  409. /* This should not happen. */
  410. WARN_ONCE(1, "ovs_ct_find_existing: new packet for %p\n", ct);
  411. }
  412. skb->nfct = &ct->ct_general;
  413. skb->nfctinfo = ctinfo;
  414. return ct;
  415. }
  416. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  417. static bool skb_nfct_cached(struct net *net,
  418. const struct sw_flow_key *key,
  419. const struct ovs_conntrack_info *info,
  420. struct sk_buff *skb)
  421. {
  422. enum ip_conntrack_info ctinfo;
  423. struct nf_conn *ct;
  424. ct = nf_ct_get(skb, &ctinfo);
  425. /* If no ct, check if we have evidence that an existing conntrack entry
  426. * might be found for this skb. This happens when we lose a skb->nfct
  427. * due to an upcall. If the connection was not confirmed, it is not
  428. * cached and needs to be run through conntrack again.
  429. */
  430. if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
  431. !(key->ct.state & OVS_CS_F_INVALID) &&
  432. key->ct.zone == info->zone.id)
  433. ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
  434. if (!ct)
  435. return false;
  436. if (!net_eq(net, read_pnet(&ct->ct_net)))
  437. return false;
  438. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  439. return false;
  440. if (info->helper) {
  441. struct nf_conn_help *help;
  442. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  443. if (help && rcu_access_pointer(help->helper) != info->helper)
  444. return false;
  445. }
  446. return true;
  447. }
  448. #ifdef CONFIG_NF_NAT_NEEDED
  449. /* Modelled after nf_nat_ipv[46]_fn().
  450. * range is only used for new, uninitialized NAT state.
  451. * Returns either NF_ACCEPT or NF_DROP.
  452. */
  453. static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
  454. enum ip_conntrack_info ctinfo,
  455. const struct nf_nat_range *range,
  456. enum nf_nat_manip_type maniptype)
  457. {
  458. int hooknum, nh_off, err = NF_ACCEPT;
  459. nh_off = skb_network_offset(skb);
  460. skb_pull(skb, nh_off);
  461. /* See HOOK2MANIP(). */
  462. if (maniptype == NF_NAT_MANIP_SRC)
  463. hooknum = NF_INET_LOCAL_IN; /* Source NAT */
  464. else
  465. hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
  466. switch (ctinfo) {
  467. case IP_CT_RELATED:
  468. case IP_CT_RELATED_REPLY:
  469. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  470. skb->protocol == htons(ETH_P_IP) &&
  471. ip_hdr(skb)->protocol == IPPROTO_ICMP) {
  472. if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
  473. hooknum))
  474. err = NF_DROP;
  475. goto push;
  476. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  477. skb->protocol == htons(ETH_P_IPV6)) {
  478. __be16 frag_off;
  479. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  480. int hdrlen = ipv6_skip_exthdr(skb,
  481. sizeof(struct ipv6hdr),
  482. &nexthdr, &frag_off);
  483. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  484. if (!nf_nat_icmpv6_reply_translation(skb, ct,
  485. ctinfo,
  486. hooknum,
  487. hdrlen))
  488. err = NF_DROP;
  489. goto push;
  490. }
  491. }
  492. /* Non-ICMP, fall thru to initialize if needed. */
  493. case IP_CT_NEW:
  494. /* Seen it before? This can happen for loopback, retrans,
  495. * or local packets.
  496. */
  497. if (!nf_nat_initialized(ct, maniptype)) {
  498. /* Initialize according to the NAT action. */
  499. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  500. /* Action is set up to establish a new
  501. * mapping.
  502. */
  503. ? nf_nat_setup_info(ct, range, maniptype)
  504. : nf_nat_alloc_null_binding(ct, hooknum);
  505. if (err != NF_ACCEPT)
  506. goto push;
  507. }
  508. break;
  509. case IP_CT_ESTABLISHED:
  510. case IP_CT_ESTABLISHED_REPLY:
  511. break;
  512. default:
  513. err = NF_DROP;
  514. goto push;
  515. }
  516. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  517. push:
  518. skb_push(skb, nh_off);
  519. return err;
  520. }
  521. static void ovs_nat_update_key(struct sw_flow_key *key,
  522. const struct sk_buff *skb,
  523. enum nf_nat_manip_type maniptype)
  524. {
  525. if (maniptype == NF_NAT_MANIP_SRC) {
  526. __be16 src;
  527. key->ct.state |= OVS_CS_F_SRC_NAT;
  528. if (key->eth.type == htons(ETH_P_IP))
  529. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  530. else if (key->eth.type == htons(ETH_P_IPV6))
  531. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  532. sizeof(key->ipv6.addr.src));
  533. else
  534. return;
  535. if (key->ip.proto == IPPROTO_UDP)
  536. src = udp_hdr(skb)->source;
  537. else if (key->ip.proto == IPPROTO_TCP)
  538. src = tcp_hdr(skb)->source;
  539. else if (key->ip.proto == IPPROTO_SCTP)
  540. src = sctp_hdr(skb)->source;
  541. else
  542. return;
  543. key->tp.src = src;
  544. } else {
  545. __be16 dst;
  546. key->ct.state |= OVS_CS_F_DST_NAT;
  547. if (key->eth.type == htons(ETH_P_IP))
  548. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  549. else if (key->eth.type == htons(ETH_P_IPV6))
  550. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  551. sizeof(key->ipv6.addr.dst));
  552. else
  553. return;
  554. if (key->ip.proto == IPPROTO_UDP)
  555. dst = udp_hdr(skb)->dest;
  556. else if (key->ip.proto == IPPROTO_TCP)
  557. dst = tcp_hdr(skb)->dest;
  558. else if (key->ip.proto == IPPROTO_SCTP)
  559. dst = sctp_hdr(skb)->dest;
  560. else
  561. return;
  562. key->tp.dst = dst;
  563. }
  564. }
  565. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  566. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  567. const struct ovs_conntrack_info *info,
  568. struct sk_buff *skb, struct nf_conn *ct,
  569. enum ip_conntrack_info ctinfo)
  570. {
  571. enum nf_nat_manip_type maniptype;
  572. int err;
  573. if (nf_ct_is_untracked(ct)) {
  574. /* A NAT action may only be performed on tracked packets. */
  575. return NF_ACCEPT;
  576. }
  577. /* Add NAT extension if not confirmed yet. */
  578. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  579. return NF_ACCEPT; /* Can't NAT. */
  580. /* Determine NAT type.
  581. * Check if the NAT type can be deduced from the tracked connection.
  582. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  583. * when committing.
  584. */
  585. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  586. ct->status & IPS_NAT_MASK &&
  587. (ctinfo != IP_CT_RELATED || info->commit)) {
  588. /* NAT an established or related connection like before. */
  589. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  590. /* This is the REPLY direction for a connection
  591. * for which NAT was applied in the forward
  592. * direction. Do the reverse NAT.
  593. */
  594. maniptype = ct->status & IPS_SRC_NAT
  595. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  596. else
  597. maniptype = ct->status & IPS_SRC_NAT
  598. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  599. } else if (info->nat & OVS_CT_SRC_NAT) {
  600. maniptype = NF_NAT_MANIP_SRC;
  601. } else if (info->nat & OVS_CT_DST_NAT) {
  602. maniptype = NF_NAT_MANIP_DST;
  603. } else {
  604. return NF_ACCEPT; /* Connection is not NATed. */
  605. }
  606. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  607. /* Mark NAT done if successful and update the flow key. */
  608. if (err == NF_ACCEPT)
  609. ovs_nat_update_key(key, skb, maniptype);
  610. return err;
  611. }
  612. #else /* !CONFIG_NF_NAT_NEEDED */
  613. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  614. const struct ovs_conntrack_info *info,
  615. struct sk_buff *skb, struct nf_conn *ct,
  616. enum ip_conntrack_info ctinfo)
  617. {
  618. return NF_ACCEPT;
  619. }
  620. #endif
  621. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  622. * not done already. Update key with new CT state after passing the packet
  623. * through conntrack.
  624. * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
  625. * set to NULL and 0 will be returned.
  626. */
  627. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  628. const struct ovs_conntrack_info *info,
  629. struct sk_buff *skb)
  630. {
  631. /* If we are recirculating packets to match on conntrack fields and
  632. * committing with a separate conntrack action, then we don't need to
  633. * actually run the packet through conntrack twice unless it's for a
  634. * different zone.
  635. */
  636. bool cached = skb_nfct_cached(net, key, info, skb);
  637. enum ip_conntrack_info ctinfo;
  638. struct nf_conn *ct;
  639. if (!cached) {
  640. struct nf_conn *tmpl = info->ct;
  641. int err;
  642. /* Associate skb with specified zone. */
  643. if (tmpl) {
  644. if (skb->nfct)
  645. nf_conntrack_put(skb->nfct);
  646. nf_conntrack_get(&tmpl->ct_general);
  647. skb->nfct = &tmpl->ct_general;
  648. skb->nfctinfo = IP_CT_NEW;
  649. }
  650. /* Repeat if requested, see nf_iterate(). */
  651. do {
  652. err = nf_conntrack_in(net, info->family,
  653. NF_INET_PRE_ROUTING, skb);
  654. } while (err == NF_REPEAT);
  655. if (err != NF_ACCEPT)
  656. return -ENOENT;
  657. /* Clear CT state NAT flags to mark that we have not yet done
  658. * NAT after the nf_conntrack_in() call. We can actually clear
  659. * the whole state, as it will be re-initialized below.
  660. */
  661. key->ct.state = 0;
  662. /* Update the key, but keep the NAT flags. */
  663. ovs_ct_update_key(skb, info, key, true, true);
  664. }
  665. ct = nf_ct_get(skb, &ctinfo);
  666. if (ct) {
  667. /* Packets starting a new connection must be NATted before the
  668. * helper, so that the helper knows about the NAT. We enforce
  669. * this by delaying both NAT and helper calls for unconfirmed
  670. * connections until the committing CT action. For later
  671. * packets NAT and Helper may be called in either order.
  672. *
  673. * NAT will be done only if the CT action has NAT, and only
  674. * once per packet (per zone), as guarded by the NAT bits in
  675. * the key->ct.state.
  676. */
  677. if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
  678. (nf_ct_is_confirmed(ct) || info->commit) &&
  679. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  680. return -EINVAL;
  681. }
  682. /* Call the helper only if:
  683. * - nf_conntrack_in() was executed above ("!cached") for a
  684. * confirmed connection, or
  685. * - When committing an unconfirmed connection.
  686. */
  687. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  688. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  689. return -EINVAL;
  690. }
  691. }
  692. return 0;
  693. }
  694. /* Lookup connection and read fields into key. */
  695. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  696. const struct ovs_conntrack_info *info,
  697. struct sk_buff *skb)
  698. {
  699. struct nf_conntrack_expect *exp;
  700. /* If we pass an expected packet through nf_conntrack_in() the
  701. * expectation is typically removed, but the packet could still be
  702. * lost in upcall processing. To prevent this from happening we
  703. * perform an explicit expectation lookup. Expected connections are
  704. * always new, and will be passed through conntrack only when they are
  705. * committed, as it is OK to remove the expectation at that time.
  706. */
  707. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  708. if (exp) {
  709. u8 state;
  710. /* NOTE: New connections are NATted and Helped only when
  711. * committed, so we are not calling into NAT here.
  712. */
  713. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  714. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  715. } else
  716. return __ovs_ct_lookup(net, key, info, skb);
  717. return 0;
  718. }
  719. /* Lookup connection and confirm if unconfirmed. */
  720. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  721. const struct ovs_conntrack_info *info,
  722. struct sk_buff *skb)
  723. {
  724. int err;
  725. err = __ovs_ct_lookup(net, key, info, skb);
  726. if (err)
  727. return err;
  728. /* This is a no-op if the connection has already been confirmed. */
  729. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  730. return -EINVAL;
  731. return 0;
  732. }
  733. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  734. {
  735. size_t i;
  736. for (i = 0; i < sizeof(*labels); i++)
  737. if (labels->ct_labels[i])
  738. return true;
  739. return false;
  740. }
  741. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  742. * value if 'skb' is freed.
  743. */
  744. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  745. struct sw_flow_key *key,
  746. const struct ovs_conntrack_info *info)
  747. {
  748. int nh_ofs;
  749. int err;
  750. /* The conntrack module expects to be working at L3. */
  751. nh_ofs = skb_network_offset(skb);
  752. skb_pull(skb, nh_ofs);
  753. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  754. err = handle_fragments(net, key, info->zone.id, skb);
  755. if (err)
  756. return err;
  757. }
  758. if (info->commit)
  759. err = ovs_ct_commit(net, key, info, skb);
  760. else
  761. err = ovs_ct_lookup(net, key, info, skb);
  762. if (err)
  763. goto err;
  764. if (info->mark.mask) {
  765. err = ovs_ct_set_mark(skb, key, info->mark.value,
  766. info->mark.mask);
  767. if (err)
  768. goto err;
  769. }
  770. if (labels_nonzero(&info->labels.mask))
  771. err = ovs_ct_set_labels(skb, key, &info->labels.value,
  772. &info->labels.mask);
  773. err:
  774. skb_push(skb, nh_ofs);
  775. if (err)
  776. kfree_skb(skb);
  777. return err;
  778. }
  779. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  780. const struct sw_flow_key *key, bool log)
  781. {
  782. struct nf_conntrack_helper *helper;
  783. struct nf_conn_help *help;
  784. helper = nf_conntrack_helper_try_module_get(name, info->family,
  785. key->ip.proto);
  786. if (!helper) {
  787. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  788. return -EINVAL;
  789. }
  790. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  791. if (!help) {
  792. module_put(helper->me);
  793. return -ENOMEM;
  794. }
  795. rcu_assign_pointer(help->helper, helper);
  796. info->helper = helper;
  797. return 0;
  798. }
  799. #ifdef CONFIG_NF_NAT_NEEDED
  800. static int parse_nat(const struct nlattr *attr,
  801. struct ovs_conntrack_info *info, bool log)
  802. {
  803. struct nlattr *a;
  804. int rem;
  805. bool have_ip_max = false;
  806. bool have_proto_max = false;
  807. bool ip_vers = (info->family == NFPROTO_IPV6);
  808. nla_for_each_nested(a, attr, rem) {
  809. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  810. [OVS_NAT_ATTR_SRC] = {0, 0},
  811. [OVS_NAT_ATTR_DST] = {0, 0},
  812. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  813. sizeof(struct in6_addr)},
  814. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  815. sizeof(struct in6_addr)},
  816. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  817. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  818. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  819. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  820. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  821. };
  822. int type = nla_type(a);
  823. if (type > OVS_NAT_ATTR_MAX) {
  824. OVS_NLERR(log,
  825. "Unknown NAT attribute (type=%d, max=%d).\n",
  826. type, OVS_NAT_ATTR_MAX);
  827. return -EINVAL;
  828. }
  829. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  830. OVS_NLERR(log,
  831. "NAT attribute type %d has unexpected length (%d != %d).\n",
  832. type, nla_len(a),
  833. ovs_nat_attr_lens[type][ip_vers]);
  834. return -EINVAL;
  835. }
  836. switch (type) {
  837. case OVS_NAT_ATTR_SRC:
  838. case OVS_NAT_ATTR_DST:
  839. if (info->nat) {
  840. OVS_NLERR(log,
  841. "Only one type of NAT may be specified.\n"
  842. );
  843. return -ERANGE;
  844. }
  845. info->nat |= OVS_CT_NAT;
  846. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  847. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  848. break;
  849. case OVS_NAT_ATTR_IP_MIN:
  850. nla_memcpy(&info->range.min_addr, a, nla_len(a));
  851. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  852. break;
  853. case OVS_NAT_ATTR_IP_MAX:
  854. have_ip_max = true;
  855. nla_memcpy(&info->range.max_addr, a,
  856. sizeof(info->range.max_addr));
  857. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  858. break;
  859. case OVS_NAT_ATTR_PROTO_MIN:
  860. info->range.min_proto.all = htons(nla_get_u16(a));
  861. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  862. break;
  863. case OVS_NAT_ATTR_PROTO_MAX:
  864. have_proto_max = true;
  865. info->range.max_proto.all = htons(nla_get_u16(a));
  866. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  867. break;
  868. case OVS_NAT_ATTR_PERSISTENT:
  869. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  870. break;
  871. case OVS_NAT_ATTR_PROTO_HASH:
  872. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  873. break;
  874. case OVS_NAT_ATTR_PROTO_RANDOM:
  875. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  876. break;
  877. default:
  878. OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
  879. return -EINVAL;
  880. }
  881. }
  882. if (rem > 0) {
  883. OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
  884. return -EINVAL;
  885. }
  886. if (!info->nat) {
  887. /* Do not allow flags if no type is given. */
  888. if (info->range.flags) {
  889. OVS_NLERR(log,
  890. "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
  891. );
  892. return -EINVAL;
  893. }
  894. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  895. } else if (!info->commit) {
  896. OVS_NLERR(log,
  897. "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
  898. );
  899. return -EINVAL;
  900. }
  901. /* Allow missing IP_MAX. */
  902. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  903. memcpy(&info->range.max_addr, &info->range.min_addr,
  904. sizeof(info->range.max_addr));
  905. }
  906. /* Allow missing PROTO_MAX. */
  907. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  908. !have_proto_max) {
  909. info->range.max_proto.all = info->range.min_proto.all;
  910. }
  911. return 0;
  912. }
  913. #endif
  914. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  915. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  916. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  917. .maxlen = sizeof(u16) },
  918. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  919. .maxlen = sizeof(struct md_mark) },
  920. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  921. .maxlen = sizeof(struct md_labels) },
  922. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  923. .maxlen = NF_CT_HELPER_NAME_LEN },
  924. #ifdef CONFIG_NF_NAT_NEEDED
  925. /* NAT length is checked when parsing the nested attributes. */
  926. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  927. #endif
  928. };
  929. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  930. const char **helper, bool log)
  931. {
  932. struct nlattr *a;
  933. int rem;
  934. nla_for_each_nested(a, attr, rem) {
  935. int type = nla_type(a);
  936. int maxlen = ovs_ct_attr_lens[type].maxlen;
  937. int minlen = ovs_ct_attr_lens[type].minlen;
  938. if (type > OVS_CT_ATTR_MAX) {
  939. OVS_NLERR(log,
  940. "Unknown conntrack attr (type=%d, max=%d)",
  941. type, OVS_CT_ATTR_MAX);
  942. return -EINVAL;
  943. }
  944. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  945. OVS_NLERR(log,
  946. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  947. type, nla_len(a), maxlen);
  948. return -EINVAL;
  949. }
  950. switch (type) {
  951. case OVS_CT_ATTR_COMMIT:
  952. info->commit = true;
  953. break;
  954. #ifdef CONFIG_NF_CONNTRACK_ZONES
  955. case OVS_CT_ATTR_ZONE:
  956. info->zone.id = nla_get_u16(a);
  957. break;
  958. #endif
  959. #ifdef CONFIG_NF_CONNTRACK_MARK
  960. case OVS_CT_ATTR_MARK: {
  961. struct md_mark *mark = nla_data(a);
  962. if (!mark->mask) {
  963. OVS_NLERR(log, "ct_mark mask cannot be 0");
  964. return -EINVAL;
  965. }
  966. info->mark = *mark;
  967. break;
  968. }
  969. #endif
  970. #ifdef CONFIG_NF_CONNTRACK_LABELS
  971. case OVS_CT_ATTR_LABELS: {
  972. struct md_labels *labels = nla_data(a);
  973. if (!labels_nonzero(&labels->mask)) {
  974. OVS_NLERR(log, "ct_labels mask cannot be 0");
  975. return -EINVAL;
  976. }
  977. info->labels = *labels;
  978. break;
  979. }
  980. #endif
  981. case OVS_CT_ATTR_HELPER:
  982. *helper = nla_data(a);
  983. if (!memchr(*helper, '\0', nla_len(a))) {
  984. OVS_NLERR(log, "Invalid conntrack helper");
  985. return -EINVAL;
  986. }
  987. break;
  988. #ifdef CONFIG_NF_NAT_NEEDED
  989. case OVS_CT_ATTR_NAT: {
  990. int err = parse_nat(a, info, log);
  991. if (err)
  992. return err;
  993. break;
  994. }
  995. #endif
  996. default:
  997. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  998. type);
  999. return -EINVAL;
  1000. }
  1001. }
  1002. if (rem > 0) {
  1003. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1004. return -EINVAL;
  1005. }
  1006. return 0;
  1007. }
  1008. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1009. {
  1010. if (attr == OVS_KEY_ATTR_CT_STATE)
  1011. return true;
  1012. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1013. attr == OVS_KEY_ATTR_CT_ZONE)
  1014. return true;
  1015. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1016. attr == OVS_KEY_ATTR_CT_MARK)
  1017. return true;
  1018. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1019. attr == OVS_KEY_ATTR_CT_LABELS) {
  1020. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1021. return ovs_net->xt_label;
  1022. }
  1023. return false;
  1024. }
  1025. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1026. const struct sw_flow_key *key,
  1027. struct sw_flow_actions **sfa, bool log)
  1028. {
  1029. struct ovs_conntrack_info ct_info;
  1030. const char *helper = NULL;
  1031. u16 family;
  1032. int err;
  1033. family = key_to_nfproto(key);
  1034. if (family == NFPROTO_UNSPEC) {
  1035. OVS_NLERR(log, "ct family unspecified");
  1036. return -EINVAL;
  1037. }
  1038. memset(&ct_info, 0, sizeof(ct_info));
  1039. ct_info.family = family;
  1040. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1041. NF_CT_DEFAULT_ZONE_DIR, 0);
  1042. err = parse_ct(attr, &ct_info, &helper, log);
  1043. if (err)
  1044. return err;
  1045. /* Set up template for tracking connections in specific zones. */
  1046. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1047. if (!ct_info.ct) {
  1048. OVS_NLERR(log, "Failed to allocate conntrack template");
  1049. return -ENOMEM;
  1050. }
  1051. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1052. nf_conntrack_get(&ct_info.ct->ct_general);
  1053. if (helper) {
  1054. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1055. if (err)
  1056. goto err_free_ct;
  1057. }
  1058. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1059. sizeof(ct_info), log);
  1060. if (err)
  1061. goto err_free_ct;
  1062. return 0;
  1063. err_free_ct:
  1064. __ovs_ct_free_action(&ct_info);
  1065. return err;
  1066. }
  1067. #ifdef CONFIG_NF_NAT_NEEDED
  1068. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1069. struct sk_buff *skb)
  1070. {
  1071. struct nlattr *start;
  1072. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1073. if (!start)
  1074. return false;
  1075. if (info->nat & OVS_CT_SRC_NAT) {
  1076. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1077. return false;
  1078. } else if (info->nat & OVS_CT_DST_NAT) {
  1079. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1080. return false;
  1081. } else {
  1082. goto out;
  1083. }
  1084. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1085. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1086. info->family == NFPROTO_IPV4) {
  1087. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1088. info->range.min_addr.ip) ||
  1089. (info->range.max_addr.ip
  1090. != info->range.min_addr.ip &&
  1091. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1092. info->range.max_addr.ip))))
  1093. return false;
  1094. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1095. info->family == NFPROTO_IPV6) {
  1096. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1097. &info->range.min_addr.in6) ||
  1098. (memcmp(&info->range.max_addr.in6,
  1099. &info->range.min_addr.in6,
  1100. sizeof(info->range.max_addr.in6)) &&
  1101. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1102. &info->range.max_addr.in6))))
  1103. return false;
  1104. } else {
  1105. return false;
  1106. }
  1107. }
  1108. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1109. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1110. ntohs(info->range.min_proto.all)) ||
  1111. (info->range.max_proto.all != info->range.min_proto.all &&
  1112. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1113. ntohs(info->range.max_proto.all)))))
  1114. return false;
  1115. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1116. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1117. return false;
  1118. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1119. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1120. return false;
  1121. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1122. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1123. return false;
  1124. out:
  1125. nla_nest_end(skb, start);
  1126. return true;
  1127. }
  1128. #endif
  1129. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1130. struct sk_buff *skb)
  1131. {
  1132. struct nlattr *start;
  1133. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1134. if (!start)
  1135. return -EMSGSIZE;
  1136. if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
  1137. return -EMSGSIZE;
  1138. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1139. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1140. return -EMSGSIZE;
  1141. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1142. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1143. &ct_info->mark))
  1144. return -EMSGSIZE;
  1145. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1146. labels_nonzero(&ct_info->labels.mask) &&
  1147. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1148. &ct_info->labels))
  1149. return -EMSGSIZE;
  1150. if (ct_info->helper) {
  1151. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1152. ct_info->helper->name))
  1153. return -EMSGSIZE;
  1154. }
  1155. #ifdef CONFIG_NF_NAT_NEEDED
  1156. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1157. return -EMSGSIZE;
  1158. #endif
  1159. nla_nest_end(skb, start);
  1160. return 0;
  1161. }
  1162. void ovs_ct_free_action(const struct nlattr *a)
  1163. {
  1164. struct ovs_conntrack_info *ct_info = nla_data(a);
  1165. __ovs_ct_free_action(ct_info);
  1166. }
  1167. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1168. {
  1169. if (ct_info->helper)
  1170. module_put(ct_info->helper->me);
  1171. if (ct_info->ct)
  1172. nf_ct_put(ct_info->ct);
  1173. }
  1174. void ovs_ct_init(struct net *net)
  1175. {
  1176. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1177. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1178. if (nf_connlabels_get(net, n_bits)) {
  1179. ovs_net->xt_label = false;
  1180. OVS_NLERR(true, "Failed to set connlabel length");
  1181. } else {
  1182. ovs_net->xt_label = true;
  1183. }
  1184. }
  1185. void ovs_ct_exit(struct net *net)
  1186. {
  1187. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1188. if (ovs_net->xt_label)
  1189. nf_connlabels_put(net);
  1190. }