conntrack.c 41 KB

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