conntrack.c 36 KB

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