conntrack.c 37 KB

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