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