conntrack.c 38 KB

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