conntrack.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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 = sizeof(cl->bits);
  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 || sizeof(cl->bits) < 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. skb_orphan(skb);
  322. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  323. err = nf_ct_frag6_gather(net, skb, user);
  324. if (err) {
  325. if (err != -EINPROGRESS)
  326. kfree_skb(skb);
  327. return err;
  328. }
  329. key->ip.proto = ipv6_hdr(skb)->nexthdr;
  330. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  331. #endif
  332. } else {
  333. kfree_skb(skb);
  334. return -EPFNOSUPPORT;
  335. }
  336. key->ip.frag = OVS_FRAG_TYPE_NONE;
  337. skb_clear_hash(skb);
  338. skb->ignore_df = 1;
  339. *OVS_CB(skb) = ovs_cb;
  340. return 0;
  341. }
  342. static struct nf_conntrack_expect *
  343. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  344. u16 proto, const struct sk_buff *skb)
  345. {
  346. struct nf_conntrack_tuple tuple;
  347. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
  348. return NULL;
  349. return __nf_ct_expect_find(net, zone, &tuple);
  350. }
  351. /* This replicates logic from nf_conntrack_core.c that is not exported. */
  352. static enum ip_conntrack_info
  353. ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
  354. {
  355. const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
  356. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  357. return IP_CT_ESTABLISHED_REPLY;
  358. /* Once we've had two way comms, always ESTABLISHED. */
  359. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
  360. return IP_CT_ESTABLISHED;
  361. if (test_bit(IPS_EXPECTED_BIT, &ct->status))
  362. return IP_CT_RELATED;
  363. return IP_CT_NEW;
  364. }
  365. /* Find an existing connection which this packet belongs to without
  366. * re-attributing statistics or modifying the connection state. This allows an
  367. * skb->nfct lost due to an upcall to be recovered during actions execution.
  368. *
  369. * Must be called with rcu_read_lock.
  370. *
  371. * On success, populates skb->nfct and skb->nfctinfo, and returns the
  372. * connection. Returns NULL if there is no existing entry.
  373. */
  374. static struct nf_conn *
  375. ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
  376. u8 l3num, struct sk_buff *skb)
  377. {
  378. struct nf_conntrack_l3proto *l3proto;
  379. struct nf_conntrack_l4proto *l4proto;
  380. struct nf_conntrack_tuple tuple;
  381. struct nf_conntrack_tuple_hash *h;
  382. struct nf_conn *ct;
  383. unsigned int dataoff;
  384. u8 protonum;
  385. l3proto = __nf_ct_l3proto_find(l3num);
  386. if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
  387. &protonum) <= 0) {
  388. pr_debug("ovs_ct_find_existing: Can't get protonum\n");
  389. return NULL;
  390. }
  391. l4proto = __nf_ct_l4proto_find(l3num, protonum);
  392. if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
  393. protonum, net, &tuple, l3proto, l4proto)) {
  394. pr_debug("ovs_ct_find_existing: Can't get tuple\n");
  395. return NULL;
  396. }
  397. /* look for tuple match */
  398. h = nf_conntrack_find_get(net, zone, &tuple);
  399. if (!h)
  400. return NULL; /* Not found. */
  401. ct = nf_ct_tuplehash_to_ctrack(h);
  402. skb->nfct = &ct->ct_general;
  403. skb->nfctinfo = ovs_ct_get_info(h);
  404. return ct;
  405. }
  406. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  407. static bool skb_nfct_cached(struct net *net,
  408. const struct sw_flow_key *key,
  409. const struct ovs_conntrack_info *info,
  410. struct sk_buff *skb)
  411. {
  412. enum ip_conntrack_info ctinfo;
  413. struct nf_conn *ct;
  414. ct = nf_ct_get(skb, &ctinfo);
  415. /* If no ct, check if we have evidence that an existing conntrack entry
  416. * might be found for this skb. This happens when we lose a skb->nfct
  417. * due to an upcall. If the connection was not confirmed, it is not
  418. * cached and needs to be run through conntrack again.
  419. */
  420. if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
  421. !(key->ct.state & OVS_CS_F_INVALID) &&
  422. key->ct.zone == info->zone.id)
  423. ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
  424. if (!ct)
  425. return false;
  426. if (!net_eq(net, read_pnet(&ct->ct_net)))
  427. return false;
  428. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  429. return false;
  430. if (info->helper) {
  431. struct nf_conn_help *help;
  432. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  433. if (help && rcu_access_pointer(help->helper) != info->helper)
  434. return false;
  435. }
  436. return true;
  437. }
  438. #ifdef CONFIG_NF_NAT_NEEDED
  439. /* Modelled after nf_nat_ipv[46]_fn().
  440. * range is only used for new, uninitialized NAT state.
  441. * Returns either NF_ACCEPT or NF_DROP.
  442. */
  443. static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
  444. enum ip_conntrack_info ctinfo,
  445. const struct nf_nat_range *range,
  446. enum nf_nat_manip_type maniptype)
  447. {
  448. int hooknum, nh_off, err = NF_ACCEPT;
  449. nh_off = skb_network_offset(skb);
  450. skb_pull_rcsum(skb, nh_off);
  451. /* See HOOK2MANIP(). */
  452. if (maniptype == NF_NAT_MANIP_SRC)
  453. hooknum = NF_INET_LOCAL_IN; /* Source NAT */
  454. else
  455. hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
  456. switch (ctinfo) {
  457. case IP_CT_RELATED:
  458. case IP_CT_RELATED_REPLY:
  459. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  460. skb->protocol == htons(ETH_P_IP) &&
  461. ip_hdr(skb)->protocol == IPPROTO_ICMP) {
  462. if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
  463. hooknum))
  464. err = NF_DROP;
  465. goto push;
  466. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  467. skb->protocol == htons(ETH_P_IPV6)) {
  468. __be16 frag_off;
  469. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  470. int hdrlen = ipv6_skip_exthdr(skb,
  471. sizeof(struct ipv6hdr),
  472. &nexthdr, &frag_off);
  473. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  474. if (!nf_nat_icmpv6_reply_translation(skb, ct,
  475. ctinfo,
  476. hooknum,
  477. hdrlen))
  478. err = NF_DROP;
  479. goto push;
  480. }
  481. }
  482. /* Non-ICMP, fall thru to initialize if needed. */
  483. case IP_CT_NEW:
  484. /* Seen it before? This can happen for loopback, retrans,
  485. * or local packets.
  486. */
  487. if (!nf_nat_initialized(ct, maniptype)) {
  488. /* Initialize according to the NAT action. */
  489. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  490. /* Action is set up to establish a new
  491. * mapping.
  492. */
  493. ? nf_nat_setup_info(ct, range, maniptype)
  494. : nf_nat_alloc_null_binding(ct, hooknum);
  495. if (err != NF_ACCEPT)
  496. goto push;
  497. }
  498. break;
  499. case IP_CT_ESTABLISHED:
  500. case IP_CT_ESTABLISHED_REPLY:
  501. break;
  502. default:
  503. err = NF_DROP;
  504. goto push;
  505. }
  506. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  507. push:
  508. skb_push(skb, nh_off);
  509. skb_postpush_rcsum(skb, skb->data, nh_off);
  510. return err;
  511. }
  512. static void ovs_nat_update_key(struct sw_flow_key *key,
  513. const struct sk_buff *skb,
  514. enum nf_nat_manip_type maniptype)
  515. {
  516. if (maniptype == NF_NAT_MANIP_SRC) {
  517. __be16 src;
  518. key->ct.state |= OVS_CS_F_SRC_NAT;
  519. if (key->eth.type == htons(ETH_P_IP))
  520. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  521. else if (key->eth.type == htons(ETH_P_IPV6))
  522. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  523. sizeof(key->ipv6.addr.src));
  524. else
  525. return;
  526. if (key->ip.proto == IPPROTO_UDP)
  527. src = udp_hdr(skb)->source;
  528. else if (key->ip.proto == IPPROTO_TCP)
  529. src = tcp_hdr(skb)->source;
  530. else if (key->ip.proto == IPPROTO_SCTP)
  531. src = sctp_hdr(skb)->source;
  532. else
  533. return;
  534. key->tp.src = src;
  535. } else {
  536. __be16 dst;
  537. key->ct.state |= OVS_CS_F_DST_NAT;
  538. if (key->eth.type == htons(ETH_P_IP))
  539. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  540. else if (key->eth.type == htons(ETH_P_IPV6))
  541. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  542. sizeof(key->ipv6.addr.dst));
  543. else
  544. return;
  545. if (key->ip.proto == IPPROTO_UDP)
  546. dst = udp_hdr(skb)->dest;
  547. else if (key->ip.proto == IPPROTO_TCP)
  548. dst = tcp_hdr(skb)->dest;
  549. else if (key->ip.proto == IPPROTO_SCTP)
  550. dst = sctp_hdr(skb)->dest;
  551. else
  552. return;
  553. key->tp.dst = dst;
  554. }
  555. }
  556. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  557. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  558. const struct ovs_conntrack_info *info,
  559. struct sk_buff *skb, struct nf_conn *ct,
  560. enum ip_conntrack_info ctinfo)
  561. {
  562. enum nf_nat_manip_type maniptype;
  563. int err;
  564. if (nf_ct_is_untracked(ct)) {
  565. /* A NAT action may only be performed on tracked packets. */
  566. return NF_ACCEPT;
  567. }
  568. /* Add NAT extension if not confirmed yet. */
  569. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  570. return NF_ACCEPT; /* Can't NAT. */
  571. /* Determine NAT type.
  572. * Check if the NAT type can be deduced from the tracked connection.
  573. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  574. * when committing.
  575. */
  576. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  577. ct->status & IPS_NAT_MASK &&
  578. (ctinfo != IP_CT_RELATED || info->commit)) {
  579. /* NAT an established or related connection like before. */
  580. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  581. /* This is the REPLY direction for a connection
  582. * for which NAT was applied in the forward
  583. * direction. Do the reverse NAT.
  584. */
  585. maniptype = ct->status & IPS_SRC_NAT
  586. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  587. else
  588. maniptype = ct->status & IPS_SRC_NAT
  589. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  590. } else if (info->nat & OVS_CT_SRC_NAT) {
  591. maniptype = NF_NAT_MANIP_SRC;
  592. } else if (info->nat & OVS_CT_DST_NAT) {
  593. maniptype = NF_NAT_MANIP_DST;
  594. } else {
  595. return NF_ACCEPT; /* Connection is not NATed. */
  596. }
  597. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  598. /* Mark NAT done if successful and update the flow key. */
  599. if (err == NF_ACCEPT)
  600. ovs_nat_update_key(key, skb, maniptype);
  601. return err;
  602. }
  603. #else /* !CONFIG_NF_NAT_NEEDED */
  604. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  605. const struct ovs_conntrack_info *info,
  606. struct sk_buff *skb, struct nf_conn *ct,
  607. enum ip_conntrack_info ctinfo)
  608. {
  609. return NF_ACCEPT;
  610. }
  611. #endif
  612. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  613. * not done already. Update key with new CT state after passing the packet
  614. * through conntrack.
  615. * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
  616. * set to NULL and 0 will be returned.
  617. */
  618. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  619. const struct ovs_conntrack_info *info,
  620. struct sk_buff *skb)
  621. {
  622. /* If we are recirculating packets to match on conntrack fields and
  623. * committing with a separate conntrack action, then we don't need to
  624. * actually run the packet through conntrack twice unless it's for a
  625. * different zone.
  626. */
  627. bool cached = skb_nfct_cached(net, key, info, skb);
  628. enum ip_conntrack_info ctinfo;
  629. struct nf_conn *ct;
  630. if (!cached) {
  631. struct nf_conn *tmpl = info->ct;
  632. int err;
  633. /* Associate skb with specified zone. */
  634. if (tmpl) {
  635. if (skb->nfct)
  636. nf_conntrack_put(skb->nfct);
  637. nf_conntrack_get(&tmpl->ct_general);
  638. skb->nfct = &tmpl->ct_general;
  639. skb->nfctinfo = IP_CT_NEW;
  640. }
  641. err = nf_conntrack_in(net, info->family,
  642. NF_INET_PRE_ROUTING, skb);
  643. if (err != NF_ACCEPT)
  644. return -ENOENT;
  645. /* Clear CT state NAT flags to mark that we have not yet done
  646. * NAT after the nf_conntrack_in() call. We can actually clear
  647. * the whole state, as it will be re-initialized below.
  648. */
  649. key->ct.state = 0;
  650. /* Update the key, but keep the NAT flags. */
  651. ovs_ct_update_key(skb, info, key, true, true);
  652. }
  653. ct = nf_ct_get(skb, &ctinfo);
  654. if (ct) {
  655. /* Packets starting a new connection must be NATted before the
  656. * helper, so that the helper knows about the NAT. We enforce
  657. * this by delaying both NAT and helper calls for unconfirmed
  658. * connections until the committing CT action. For later
  659. * packets NAT and Helper may be called in either order.
  660. *
  661. * NAT will be done only if the CT action has NAT, and only
  662. * once per packet (per zone), as guarded by the NAT bits in
  663. * the key->ct.state.
  664. */
  665. if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
  666. (nf_ct_is_confirmed(ct) || info->commit) &&
  667. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  668. return -EINVAL;
  669. }
  670. /* Userspace may decide to perform a ct lookup without a helper
  671. * specified followed by a (recirculate and) commit with one.
  672. * Therefore, for unconfirmed connections which we will commit,
  673. * we need to attach the helper here.
  674. */
  675. if (!nf_ct_is_confirmed(ct) && info->commit &&
  676. info->helper && !nfct_help(ct)) {
  677. int err = __nf_ct_try_assign_helper(ct, info->ct,
  678. GFP_ATOMIC);
  679. if (err)
  680. return err;
  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. struct nf_conn *ct;
  717. int err;
  718. err = __ovs_ct_lookup(net, key, info, skb);
  719. if (err)
  720. return err;
  721. ct = (struct nf_conn *)skb->nfct;
  722. if (ct)
  723. nf_ct_deliver_cached_events(ct);
  724. }
  725. return 0;
  726. }
  727. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  728. {
  729. size_t i;
  730. for (i = 0; i < sizeof(*labels); i++)
  731. if (labels->ct_labels[i])
  732. return true;
  733. return false;
  734. }
  735. /* Lookup connection and confirm if unconfirmed. */
  736. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  737. const struct ovs_conntrack_info *info,
  738. struct sk_buff *skb)
  739. {
  740. int err;
  741. err = __ovs_ct_lookup(net, key, info, skb);
  742. if (err)
  743. return err;
  744. /* Apply changes before confirming the connection so that the initial
  745. * conntrack NEW netlink event carries the values given in the CT
  746. * action.
  747. */
  748. if (info->mark.mask) {
  749. err = ovs_ct_set_mark(skb, key, info->mark.value,
  750. info->mark.mask);
  751. if (err)
  752. return err;
  753. }
  754. if (labels_nonzero(&info->labels.mask)) {
  755. err = ovs_ct_set_labels(skb, key, &info->labels.value,
  756. &info->labels.mask);
  757. if (err)
  758. return err;
  759. }
  760. /* This will take care of sending queued events even if the connection
  761. * is already confirmed.
  762. */
  763. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  764. return -EINVAL;
  765. return 0;
  766. }
  767. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  768. * value if 'skb' is freed.
  769. */
  770. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  771. struct sw_flow_key *key,
  772. const struct ovs_conntrack_info *info)
  773. {
  774. int nh_ofs;
  775. int err;
  776. /* The conntrack module expects to be working at L3. */
  777. nh_ofs = skb_network_offset(skb);
  778. skb_pull_rcsum(skb, nh_ofs);
  779. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  780. err = handle_fragments(net, key, info->zone.id, skb);
  781. if (err)
  782. return err;
  783. }
  784. if (info->commit)
  785. err = ovs_ct_commit(net, key, info, skb);
  786. else
  787. err = ovs_ct_lookup(net, key, info, skb);
  788. skb_push(skb, nh_ofs);
  789. skb_postpush_rcsum(skb, skb->data, nh_ofs);
  790. if (err)
  791. kfree_skb(skb);
  792. return err;
  793. }
  794. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  795. const struct sw_flow_key *key, bool log)
  796. {
  797. struct nf_conntrack_helper *helper;
  798. struct nf_conn_help *help;
  799. helper = nf_conntrack_helper_try_module_get(name, info->family,
  800. key->ip.proto);
  801. if (!helper) {
  802. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  803. return -EINVAL;
  804. }
  805. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  806. if (!help) {
  807. module_put(helper->me);
  808. return -ENOMEM;
  809. }
  810. rcu_assign_pointer(help->helper, helper);
  811. info->helper = helper;
  812. return 0;
  813. }
  814. #ifdef CONFIG_NF_NAT_NEEDED
  815. static int parse_nat(const struct nlattr *attr,
  816. struct ovs_conntrack_info *info, bool log)
  817. {
  818. struct nlattr *a;
  819. int rem;
  820. bool have_ip_max = false;
  821. bool have_proto_max = false;
  822. bool ip_vers = (info->family == NFPROTO_IPV6);
  823. nla_for_each_nested(a, attr, rem) {
  824. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  825. [OVS_NAT_ATTR_SRC] = {0, 0},
  826. [OVS_NAT_ATTR_DST] = {0, 0},
  827. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  828. sizeof(struct in6_addr)},
  829. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  830. sizeof(struct in6_addr)},
  831. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  832. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  833. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  834. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  835. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  836. };
  837. int type = nla_type(a);
  838. if (type > OVS_NAT_ATTR_MAX) {
  839. OVS_NLERR(log,
  840. "Unknown NAT attribute (type=%d, max=%d).\n",
  841. type, OVS_NAT_ATTR_MAX);
  842. return -EINVAL;
  843. }
  844. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  845. OVS_NLERR(log,
  846. "NAT attribute type %d has unexpected length (%d != %d).\n",
  847. type, nla_len(a),
  848. ovs_nat_attr_lens[type][ip_vers]);
  849. return -EINVAL;
  850. }
  851. switch (type) {
  852. case OVS_NAT_ATTR_SRC:
  853. case OVS_NAT_ATTR_DST:
  854. if (info->nat) {
  855. OVS_NLERR(log,
  856. "Only one type of NAT may be specified.\n"
  857. );
  858. return -ERANGE;
  859. }
  860. info->nat |= OVS_CT_NAT;
  861. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  862. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  863. break;
  864. case OVS_NAT_ATTR_IP_MIN:
  865. nla_memcpy(&info->range.min_addr, a,
  866. sizeof(info->range.min_addr));
  867. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  868. break;
  869. case OVS_NAT_ATTR_IP_MAX:
  870. have_ip_max = true;
  871. nla_memcpy(&info->range.max_addr, a,
  872. sizeof(info->range.max_addr));
  873. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  874. break;
  875. case OVS_NAT_ATTR_PROTO_MIN:
  876. info->range.min_proto.all = htons(nla_get_u16(a));
  877. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  878. break;
  879. case OVS_NAT_ATTR_PROTO_MAX:
  880. have_proto_max = true;
  881. info->range.max_proto.all = htons(nla_get_u16(a));
  882. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  883. break;
  884. case OVS_NAT_ATTR_PERSISTENT:
  885. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  886. break;
  887. case OVS_NAT_ATTR_PROTO_HASH:
  888. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  889. break;
  890. case OVS_NAT_ATTR_PROTO_RANDOM:
  891. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  892. break;
  893. default:
  894. OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
  895. return -EINVAL;
  896. }
  897. }
  898. if (rem > 0) {
  899. OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
  900. return -EINVAL;
  901. }
  902. if (!info->nat) {
  903. /* Do not allow flags if no type is given. */
  904. if (info->range.flags) {
  905. OVS_NLERR(log,
  906. "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
  907. );
  908. return -EINVAL;
  909. }
  910. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  911. } else if (!info->commit) {
  912. OVS_NLERR(log,
  913. "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
  914. );
  915. return -EINVAL;
  916. }
  917. /* Allow missing IP_MAX. */
  918. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  919. memcpy(&info->range.max_addr, &info->range.min_addr,
  920. sizeof(info->range.max_addr));
  921. }
  922. /* Allow missing PROTO_MAX. */
  923. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  924. !have_proto_max) {
  925. info->range.max_proto.all = info->range.min_proto.all;
  926. }
  927. return 0;
  928. }
  929. #endif
  930. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  931. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  932. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  933. .maxlen = sizeof(u16) },
  934. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  935. .maxlen = sizeof(struct md_mark) },
  936. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  937. .maxlen = sizeof(struct md_labels) },
  938. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  939. .maxlen = NF_CT_HELPER_NAME_LEN },
  940. #ifdef CONFIG_NF_NAT_NEEDED
  941. /* NAT length is checked when parsing the nested attributes. */
  942. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  943. #endif
  944. };
  945. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  946. const char **helper, bool log)
  947. {
  948. struct nlattr *a;
  949. int rem;
  950. nla_for_each_nested(a, attr, rem) {
  951. int type = nla_type(a);
  952. int maxlen = ovs_ct_attr_lens[type].maxlen;
  953. int minlen = ovs_ct_attr_lens[type].minlen;
  954. if (type > OVS_CT_ATTR_MAX) {
  955. OVS_NLERR(log,
  956. "Unknown conntrack attr (type=%d, max=%d)",
  957. type, OVS_CT_ATTR_MAX);
  958. return -EINVAL;
  959. }
  960. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  961. OVS_NLERR(log,
  962. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  963. type, nla_len(a), maxlen);
  964. return -EINVAL;
  965. }
  966. switch (type) {
  967. case OVS_CT_ATTR_COMMIT:
  968. info->commit = true;
  969. break;
  970. #ifdef CONFIG_NF_CONNTRACK_ZONES
  971. case OVS_CT_ATTR_ZONE:
  972. info->zone.id = nla_get_u16(a);
  973. break;
  974. #endif
  975. #ifdef CONFIG_NF_CONNTRACK_MARK
  976. case OVS_CT_ATTR_MARK: {
  977. struct md_mark *mark = nla_data(a);
  978. if (!mark->mask) {
  979. OVS_NLERR(log, "ct_mark mask cannot be 0");
  980. return -EINVAL;
  981. }
  982. info->mark = *mark;
  983. break;
  984. }
  985. #endif
  986. #ifdef CONFIG_NF_CONNTRACK_LABELS
  987. case OVS_CT_ATTR_LABELS: {
  988. struct md_labels *labels = nla_data(a);
  989. if (!labels_nonzero(&labels->mask)) {
  990. OVS_NLERR(log, "ct_labels mask cannot be 0");
  991. return -EINVAL;
  992. }
  993. info->labels = *labels;
  994. break;
  995. }
  996. #endif
  997. case OVS_CT_ATTR_HELPER:
  998. *helper = nla_data(a);
  999. if (!memchr(*helper, '\0', nla_len(a))) {
  1000. OVS_NLERR(log, "Invalid conntrack helper");
  1001. return -EINVAL;
  1002. }
  1003. break;
  1004. #ifdef CONFIG_NF_NAT_NEEDED
  1005. case OVS_CT_ATTR_NAT: {
  1006. int err = parse_nat(a, info, log);
  1007. if (err)
  1008. return err;
  1009. break;
  1010. }
  1011. #endif
  1012. default:
  1013. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  1014. type);
  1015. return -EINVAL;
  1016. }
  1017. }
  1018. #ifdef CONFIG_NF_CONNTRACK_MARK
  1019. if (!info->commit && info->mark.mask) {
  1020. OVS_NLERR(log,
  1021. "Setting conntrack mark requires 'commit' flag.");
  1022. return -EINVAL;
  1023. }
  1024. #endif
  1025. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1026. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1027. OVS_NLERR(log,
  1028. "Setting conntrack labels requires 'commit' flag.");
  1029. return -EINVAL;
  1030. }
  1031. #endif
  1032. if (rem > 0) {
  1033. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1034. return -EINVAL;
  1035. }
  1036. return 0;
  1037. }
  1038. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1039. {
  1040. if (attr == OVS_KEY_ATTR_CT_STATE)
  1041. return true;
  1042. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1043. attr == OVS_KEY_ATTR_CT_ZONE)
  1044. return true;
  1045. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1046. attr == OVS_KEY_ATTR_CT_MARK)
  1047. return true;
  1048. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1049. attr == OVS_KEY_ATTR_CT_LABELS) {
  1050. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1051. return ovs_net->xt_label;
  1052. }
  1053. return false;
  1054. }
  1055. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1056. const struct sw_flow_key *key,
  1057. struct sw_flow_actions **sfa, bool log)
  1058. {
  1059. struct ovs_conntrack_info ct_info;
  1060. const char *helper = NULL;
  1061. u16 family;
  1062. int err;
  1063. family = key_to_nfproto(key);
  1064. if (family == NFPROTO_UNSPEC) {
  1065. OVS_NLERR(log, "ct family unspecified");
  1066. return -EINVAL;
  1067. }
  1068. memset(&ct_info, 0, sizeof(ct_info));
  1069. ct_info.family = family;
  1070. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1071. NF_CT_DEFAULT_ZONE_DIR, 0);
  1072. err = parse_ct(attr, &ct_info, &helper, log);
  1073. if (err)
  1074. return err;
  1075. /* Set up template for tracking connections in specific zones. */
  1076. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1077. if (!ct_info.ct) {
  1078. OVS_NLERR(log, "Failed to allocate conntrack template");
  1079. return -ENOMEM;
  1080. }
  1081. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1082. nf_conntrack_get(&ct_info.ct->ct_general);
  1083. if (helper) {
  1084. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1085. if (err)
  1086. goto err_free_ct;
  1087. }
  1088. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1089. sizeof(ct_info), log);
  1090. if (err)
  1091. goto err_free_ct;
  1092. return 0;
  1093. err_free_ct:
  1094. __ovs_ct_free_action(&ct_info);
  1095. return err;
  1096. }
  1097. #ifdef CONFIG_NF_NAT_NEEDED
  1098. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1099. struct sk_buff *skb)
  1100. {
  1101. struct nlattr *start;
  1102. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1103. if (!start)
  1104. return false;
  1105. if (info->nat & OVS_CT_SRC_NAT) {
  1106. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1107. return false;
  1108. } else if (info->nat & OVS_CT_DST_NAT) {
  1109. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1110. return false;
  1111. } else {
  1112. goto out;
  1113. }
  1114. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1115. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1116. info->family == NFPROTO_IPV4) {
  1117. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1118. info->range.min_addr.ip) ||
  1119. (info->range.max_addr.ip
  1120. != info->range.min_addr.ip &&
  1121. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1122. info->range.max_addr.ip))))
  1123. return false;
  1124. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1125. info->family == NFPROTO_IPV6) {
  1126. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1127. &info->range.min_addr.in6) ||
  1128. (memcmp(&info->range.max_addr.in6,
  1129. &info->range.min_addr.in6,
  1130. sizeof(info->range.max_addr.in6)) &&
  1131. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1132. &info->range.max_addr.in6))))
  1133. return false;
  1134. } else {
  1135. return false;
  1136. }
  1137. }
  1138. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1139. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1140. ntohs(info->range.min_proto.all)) ||
  1141. (info->range.max_proto.all != info->range.min_proto.all &&
  1142. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1143. ntohs(info->range.max_proto.all)))))
  1144. return false;
  1145. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1146. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1147. return false;
  1148. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1149. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1150. return false;
  1151. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1152. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1153. return false;
  1154. out:
  1155. nla_nest_end(skb, start);
  1156. return true;
  1157. }
  1158. #endif
  1159. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1160. struct sk_buff *skb)
  1161. {
  1162. struct nlattr *start;
  1163. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1164. if (!start)
  1165. return -EMSGSIZE;
  1166. if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
  1167. return -EMSGSIZE;
  1168. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1169. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1170. return -EMSGSIZE;
  1171. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1172. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1173. &ct_info->mark))
  1174. return -EMSGSIZE;
  1175. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1176. labels_nonzero(&ct_info->labels.mask) &&
  1177. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1178. &ct_info->labels))
  1179. return -EMSGSIZE;
  1180. if (ct_info->helper) {
  1181. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1182. ct_info->helper->name))
  1183. return -EMSGSIZE;
  1184. }
  1185. #ifdef CONFIG_NF_NAT_NEEDED
  1186. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1187. return -EMSGSIZE;
  1188. #endif
  1189. nla_nest_end(skb, start);
  1190. return 0;
  1191. }
  1192. void ovs_ct_free_action(const struct nlattr *a)
  1193. {
  1194. struct ovs_conntrack_info *ct_info = nla_data(a);
  1195. __ovs_ct_free_action(ct_info);
  1196. }
  1197. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1198. {
  1199. if (ct_info->helper)
  1200. module_put(ct_info->helper->me);
  1201. if (ct_info->ct)
  1202. nf_ct_tmpl_free(ct_info->ct);
  1203. }
  1204. void ovs_ct_init(struct net *net)
  1205. {
  1206. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1207. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1208. if (nf_connlabels_get(net, n_bits - 1)) {
  1209. ovs_net->xt_label = false;
  1210. OVS_NLERR(true, "Failed to set connlabel length");
  1211. } else {
  1212. ovs_net->xt_label = true;
  1213. }
  1214. }
  1215. void ovs_ct_exit(struct net *net)
  1216. {
  1217. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1218. if (ovs_net->xt_label)
  1219. nf_connlabels_put(net);
  1220. }