conntrack.c 36 KB

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