conntrack.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /*
  2. * Copyright (c) 2015 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/openvswitch.h>
  15. #include <linux/tcp.h>
  16. #include <linux/udp.h>
  17. #include <linux/sctp.h>
  18. #include <net/ip.h>
  19. #include <net/netfilter/nf_conntrack_core.h>
  20. #include <net/netfilter/nf_conntrack_helper.h>
  21. #include <net/netfilter/nf_conntrack_labels.h>
  22. #include <net/netfilter/nf_conntrack_seqadj.h>
  23. #include <net/netfilter/nf_conntrack_zones.h>
  24. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  25. #ifdef CONFIG_NF_NAT_NEEDED
  26. #include <linux/netfilter/nf_nat.h>
  27. #include <net/netfilter/nf_nat_core.h>
  28. #include <net/netfilter/nf_nat_l3proto.h>
  29. #endif
  30. #include "datapath.h"
  31. #include "conntrack.h"
  32. #include "flow.h"
  33. #include "flow_netlink.h"
  34. struct ovs_ct_len_tbl {
  35. int maxlen;
  36. int minlen;
  37. };
  38. /* Metadata mark for masked write to conntrack mark */
  39. struct md_mark {
  40. u32 value;
  41. u32 mask;
  42. };
  43. /* Metadata label for masked write to conntrack label. */
  44. struct md_labels {
  45. struct ovs_key_ct_labels value;
  46. struct ovs_key_ct_labels mask;
  47. };
  48. enum ovs_ct_nat {
  49. OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
  50. OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
  51. OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
  52. };
  53. /* Conntrack action context for execution. */
  54. struct ovs_conntrack_info {
  55. struct nf_conntrack_helper *helper;
  56. struct nf_conntrack_zone zone;
  57. struct nf_conn *ct;
  58. u8 commit : 1;
  59. u8 nat : 3; /* enum ovs_ct_nat */
  60. u16 family;
  61. struct md_mark mark;
  62. struct md_labels labels;
  63. #ifdef CONFIG_NF_NAT_NEEDED
  64. struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */
  65. #endif
  66. };
  67. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
  68. static u16 key_to_nfproto(const struct sw_flow_key *key)
  69. {
  70. switch (ntohs(key->eth.type)) {
  71. case ETH_P_IP:
  72. return NFPROTO_IPV4;
  73. case ETH_P_IPV6:
  74. return NFPROTO_IPV6;
  75. default:
  76. return NFPROTO_UNSPEC;
  77. }
  78. }
  79. /* Map SKB connection state into the values used by flow definition. */
  80. static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
  81. {
  82. u8 ct_state = OVS_CS_F_TRACKED;
  83. switch (ctinfo) {
  84. case IP_CT_ESTABLISHED_REPLY:
  85. case IP_CT_RELATED_REPLY:
  86. ct_state |= OVS_CS_F_REPLY_DIR;
  87. break;
  88. default:
  89. break;
  90. }
  91. switch (ctinfo) {
  92. case IP_CT_ESTABLISHED:
  93. case IP_CT_ESTABLISHED_REPLY:
  94. ct_state |= OVS_CS_F_ESTABLISHED;
  95. break;
  96. case IP_CT_RELATED:
  97. case IP_CT_RELATED_REPLY:
  98. ct_state |= OVS_CS_F_RELATED;
  99. break;
  100. case IP_CT_NEW:
  101. ct_state |= OVS_CS_F_NEW;
  102. break;
  103. default:
  104. break;
  105. }
  106. return ct_state;
  107. }
  108. static u32 ovs_ct_get_mark(const struct nf_conn *ct)
  109. {
  110. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  111. return ct ? ct->mark : 0;
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. static void ovs_ct_get_labels(const struct nf_conn *ct,
  117. struct ovs_key_ct_labels *labels)
  118. {
  119. struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
  120. if (cl) {
  121. size_t len = sizeof(cl->bits);
  122. if (len > OVS_CT_LABELS_LEN)
  123. len = OVS_CT_LABELS_LEN;
  124. else if (len < OVS_CT_LABELS_LEN)
  125. memset(labels, 0, OVS_CT_LABELS_LEN);
  126. memcpy(labels, cl->bits, len);
  127. } else {
  128. memset(labels, 0, OVS_CT_LABELS_LEN);
  129. }
  130. }
  131. static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
  132. const struct nf_conntrack_zone *zone,
  133. const struct nf_conn *ct)
  134. {
  135. key->ct.state = state;
  136. key->ct.zone = zone->id;
  137. key->ct.mark = ovs_ct_get_mark(ct);
  138. ovs_ct_get_labels(ct, &key->ct.labels);
  139. }
  140. /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has
  141. * previously sent the packet to conntrack via the ct action. If
  142. * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
  143. * initialized from the connection status.
  144. */
  145. static void ovs_ct_update_key(const struct sk_buff *skb,
  146. const struct ovs_conntrack_info *info,
  147. struct sw_flow_key *key, bool post_ct,
  148. bool keep_nat_flags)
  149. {
  150. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  151. enum ip_conntrack_info ctinfo;
  152. struct nf_conn *ct;
  153. u8 state = 0;
  154. ct = nf_ct_get(skb, &ctinfo);
  155. if (ct) {
  156. state = ovs_ct_get_state(ctinfo);
  157. /* All unconfirmed entries are NEW connections. */
  158. if (!nf_ct_is_confirmed(ct))
  159. state |= OVS_CS_F_NEW;
  160. /* OVS persists the related flag for the duration of the
  161. * connection.
  162. */
  163. if (ct->master)
  164. state |= OVS_CS_F_RELATED;
  165. if (keep_nat_flags) {
  166. state |= key->ct.state & OVS_CS_F_NAT_MASK;
  167. } else {
  168. if (ct->status & IPS_SRC_NAT)
  169. state |= OVS_CS_F_SRC_NAT;
  170. if (ct->status & IPS_DST_NAT)
  171. state |= OVS_CS_F_DST_NAT;
  172. }
  173. zone = nf_ct_zone(ct);
  174. } else if (post_ct) {
  175. state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
  176. if (info)
  177. zone = &info->zone;
  178. }
  179. __ovs_ct_update_key(key, state, zone, ct);
  180. }
  181. /* This is called to initialize CT key fields possibly coming in from the local
  182. * stack.
  183. */
  184. void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
  185. {
  186. ovs_ct_update_key(skb, NULL, key, false, false);
  187. }
  188. int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb)
  189. {
  190. if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state))
  191. return -EMSGSIZE;
  192. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  193. nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone))
  194. return -EMSGSIZE;
  195. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  196. nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark))
  197. return -EMSGSIZE;
  198. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  199. nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels),
  200. &key->ct.labels))
  201. return -EMSGSIZE;
  202. return 0;
  203. }
  204. static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key,
  205. u32 ct_mark, u32 mask)
  206. {
  207. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  208. enum ip_conntrack_info ctinfo;
  209. struct nf_conn *ct;
  210. u32 new_mark;
  211. /* The connection could be invalid, in which case set_mark is no-op. */
  212. ct = nf_ct_get(skb, &ctinfo);
  213. if (!ct)
  214. return 0;
  215. new_mark = ct_mark | (ct->mark & ~(mask));
  216. if (ct->mark != new_mark) {
  217. ct->mark = new_mark;
  218. nf_conntrack_event_cache(IPCT_MARK, ct);
  219. key->ct.mark = new_mark;
  220. }
  221. return 0;
  222. #else
  223. return -ENOTSUPP;
  224. #endif
  225. }
  226. static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key,
  227. const struct ovs_key_ct_labels *labels,
  228. const struct ovs_key_ct_labels *mask)
  229. {
  230. enum ip_conntrack_info ctinfo;
  231. struct nf_conn_labels *cl;
  232. struct nf_conn *ct;
  233. int err;
  234. /* The connection could be invalid, in which case set_label is no-op.*/
  235. ct = nf_ct_get(skb, &ctinfo);
  236. if (!ct)
  237. return 0;
  238. cl = nf_ct_labels_find(ct);
  239. if (!cl) {
  240. nf_ct_labels_ext_add(ct);
  241. cl = nf_ct_labels_find(ct);
  242. }
  243. if (!cl || sizeof(cl->bits) < OVS_CT_LABELS_LEN)
  244. return -ENOSPC;
  245. err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask,
  246. OVS_CT_LABELS_LEN / sizeof(u32));
  247. if (err)
  248. return err;
  249. ovs_ct_get_labels(ct, &key->ct.labels);
  250. return 0;
  251. }
  252. /* 'skb' should already be pulled to nh_ofs. */
  253. static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
  254. {
  255. const struct nf_conntrack_helper *helper;
  256. const struct nf_conn_help *help;
  257. enum ip_conntrack_info ctinfo;
  258. unsigned int protoff;
  259. struct nf_conn *ct;
  260. int err;
  261. ct = nf_ct_get(skb, &ctinfo);
  262. if (!ct || ctinfo == IP_CT_RELATED_REPLY)
  263. return NF_ACCEPT;
  264. help = nfct_help(ct);
  265. if (!help)
  266. return NF_ACCEPT;
  267. helper = rcu_dereference(help->helper);
  268. if (!helper)
  269. return NF_ACCEPT;
  270. switch (proto) {
  271. case NFPROTO_IPV4:
  272. protoff = ip_hdrlen(skb);
  273. break;
  274. case NFPROTO_IPV6: {
  275. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  276. __be16 frag_off;
  277. int ofs;
  278. ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
  279. &frag_off);
  280. if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
  281. pr_debug("proto header not found\n");
  282. return NF_ACCEPT;
  283. }
  284. protoff = ofs;
  285. break;
  286. }
  287. default:
  288. WARN_ONCE(1, "helper invoked on non-IP family!");
  289. return NF_DROP;
  290. }
  291. err = helper->help(skb, protoff, ct, ctinfo);
  292. if (err != NF_ACCEPT)
  293. return err;
  294. /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
  295. * FTP with NAT) adusting the TCP payload size when mangling IP
  296. * addresses and/or port numbers in the text-based control connection.
  297. */
  298. if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
  299. !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
  300. return NF_DROP;
  301. return NF_ACCEPT;
  302. }
  303. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  304. * value if 'skb' is freed.
  305. */
  306. static int handle_fragments(struct net *net, struct sw_flow_key *key,
  307. u16 zone, struct sk_buff *skb)
  308. {
  309. struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
  310. int err;
  311. if (key->eth.type == htons(ETH_P_IP)) {
  312. enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
  313. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  314. err = ip_defrag(net, skb, user);
  315. if (err)
  316. return err;
  317. ovs_cb.mru = IPCB(skb)->frag_max_size;
  318. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
  319. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  320. enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
  321. 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. struct nf_conn *ct;
  380. unsigned int dataoff;
  381. u8 protonum;
  382. l3proto = __nf_ct_l3proto_find(l3num);
  383. if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
  384. &protonum) <= 0) {
  385. pr_debug("ovs_ct_find_existing: Can't get protonum\n");
  386. return NULL;
  387. }
  388. l4proto = __nf_ct_l4proto_find(l3num, protonum);
  389. if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
  390. protonum, net, &tuple, l3proto, l4proto)) {
  391. pr_debug("ovs_ct_find_existing: Can't get tuple\n");
  392. return NULL;
  393. }
  394. /* look for tuple match */
  395. h = nf_conntrack_find_get(net, zone, &tuple);
  396. if (!h)
  397. return NULL; /* Not found. */
  398. ct = nf_ct_tuplehash_to_ctrack(h);
  399. skb->nfct = &ct->ct_general;
  400. skb->nfctinfo = ovs_ct_get_info(h);
  401. return ct;
  402. }
  403. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  404. static bool skb_nfct_cached(struct net *net,
  405. const struct sw_flow_key *key,
  406. const struct ovs_conntrack_info *info,
  407. struct sk_buff *skb)
  408. {
  409. enum ip_conntrack_info ctinfo;
  410. struct nf_conn *ct;
  411. ct = nf_ct_get(skb, &ctinfo);
  412. /* If no ct, check if we have evidence that an existing conntrack entry
  413. * might be found for this skb. This happens when we lose a skb->nfct
  414. * due to an upcall. If the connection was not confirmed, it is not
  415. * cached and needs to be run through conntrack again.
  416. */
  417. if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
  418. !(key->ct.state & OVS_CS_F_INVALID) &&
  419. key->ct.zone == info->zone.id)
  420. ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
  421. if (!ct)
  422. return false;
  423. if (!net_eq(net, read_pnet(&ct->ct_net)))
  424. return false;
  425. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  426. return false;
  427. if (info->helper) {
  428. struct nf_conn_help *help;
  429. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  430. if (help && rcu_access_pointer(help->helper) != info->helper)
  431. return false;
  432. }
  433. return true;
  434. }
  435. #ifdef CONFIG_NF_NAT_NEEDED
  436. /* Modelled after nf_nat_ipv[46]_fn().
  437. * range is only used for new, uninitialized NAT state.
  438. * Returns either NF_ACCEPT or NF_DROP.
  439. */
  440. static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
  441. enum ip_conntrack_info ctinfo,
  442. const struct nf_nat_range *range,
  443. enum nf_nat_manip_type maniptype)
  444. {
  445. int hooknum, nh_off, err = NF_ACCEPT;
  446. nh_off = skb_network_offset(skb);
  447. skb_pull(skb, nh_off);
  448. /* See HOOK2MANIP(). */
  449. if (maniptype == NF_NAT_MANIP_SRC)
  450. hooknum = NF_INET_LOCAL_IN; /* Source NAT */
  451. else
  452. hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
  453. switch (ctinfo) {
  454. case IP_CT_RELATED:
  455. case IP_CT_RELATED_REPLY:
  456. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  457. skb->protocol == htons(ETH_P_IP) &&
  458. ip_hdr(skb)->protocol == IPPROTO_ICMP) {
  459. if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
  460. hooknum))
  461. err = NF_DROP;
  462. goto push;
  463. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  464. skb->protocol == htons(ETH_P_IPV6)) {
  465. __be16 frag_off;
  466. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  467. int hdrlen = ipv6_skip_exthdr(skb,
  468. sizeof(struct ipv6hdr),
  469. &nexthdr, &frag_off);
  470. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  471. if (!nf_nat_icmpv6_reply_translation(skb, ct,
  472. ctinfo,
  473. hooknum,
  474. hdrlen))
  475. err = NF_DROP;
  476. goto push;
  477. }
  478. }
  479. /* Non-ICMP, fall thru to initialize if needed. */
  480. case IP_CT_NEW:
  481. /* Seen it before? This can happen for loopback, retrans,
  482. * or local packets.
  483. */
  484. if (!nf_nat_initialized(ct, maniptype)) {
  485. /* Initialize according to the NAT action. */
  486. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  487. /* Action is set up to establish a new
  488. * mapping.
  489. */
  490. ? nf_nat_setup_info(ct, range, maniptype)
  491. : nf_nat_alloc_null_binding(ct, hooknum);
  492. if (err != NF_ACCEPT)
  493. goto push;
  494. }
  495. break;
  496. case IP_CT_ESTABLISHED:
  497. case IP_CT_ESTABLISHED_REPLY:
  498. break;
  499. default:
  500. err = NF_DROP;
  501. goto push;
  502. }
  503. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  504. push:
  505. skb_push(skb, nh_off);
  506. return err;
  507. }
  508. static void ovs_nat_update_key(struct sw_flow_key *key,
  509. const struct sk_buff *skb,
  510. enum nf_nat_manip_type maniptype)
  511. {
  512. if (maniptype == NF_NAT_MANIP_SRC) {
  513. __be16 src;
  514. key->ct.state |= OVS_CS_F_SRC_NAT;
  515. if (key->eth.type == htons(ETH_P_IP))
  516. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  517. else if (key->eth.type == htons(ETH_P_IPV6))
  518. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  519. sizeof(key->ipv6.addr.src));
  520. else
  521. return;
  522. if (key->ip.proto == IPPROTO_UDP)
  523. src = udp_hdr(skb)->source;
  524. else if (key->ip.proto == IPPROTO_TCP)
  525. src = tcp_hdr(skb)->source;
  526. else if (key->ip.proto == IPPROTO_SCTP)
  527. src = sctp_hdr(skb)->source;
  528. else
  529. return;
  530. key->tp.src = src;
  531. } else {
  532. __be16 dst;
  533. key->ct.state |= OVS_CS_F_DST_NAT;
  534. if (key->eth.type == htons(ETH_P_IP))
  535. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  536. else if (key->eth.type == htons(ETH_P_IPV6))
  537. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  538. sizeof(key->ipv6.addr.dst));
  539. else
  540. return;
  541. if (key->ip.proto == IPPROTO_UDP)
  542. dst = udp_hdr(skb)->dest;
  543. else if (key->ip.proto == IPPROTO_TCP)
  544. dst = tcp_hdr(skb)->dest;
  545. else if (key->ip.proto == IPPROTO_SCTP)
  546. dst = sctp_hdr(skb)->dest;
  547. else
  548. return;
  549. key->tp.dst = dst;
  550. }
  551. }
  552. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  553. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  554. const struct ovs_conntrack_info *info,
  555. struct sk_buff *skb, struct nf_conn *ct,
  556. enum ip_conntrack_info ctinfo)
  557. {
  558. enum nf_nat_manip_type maniptype;
  559. int err;
  560. if (nf_ct_is_untracked(ct)) {
  561. /* A NAT action may only be performed on tracked packets. */
  562. return NF_ACCEPT;
  563. }
  564. /* Add NAT extension if not confirmed yet. */
  565. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  566. return NF_ACCEPT; /* Can't NAT. */
  567. /* Determine NAT type.
  568. * Check if the NAT type can be deduced from the tracked connection.
  569. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  570. * when committing.
  571. */
  572. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  573. ct->status & IPS_NAT_MASK &&
  574. (ctinfo != IP_CT_RELATED || info->commit)) {
  575. /* NAT an established or related connection like before. */
  576. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  577. /* This is the REPLY direction for a connection
  578. * for which NAT was applied in the forward
  579. * direction. Do the reverse NAT.
  580. */
  581. maniptype = ct->status & IPS_SRC_NAT
  582. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  583. else
  584. maniptype = ct->status & IPS_SRC_NAT
  585. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  586. } else if (info->nat & OVS_CT_SRC_NAT) {
  587. maniptype = NF_NAT_MANIP_SRC;
  588. } else if (info->nat & OVS_CT_DST_NAT) {
  589. maniptype = NF_NAT_MANIP_DST;
  590. } else {
  591. return NF_ACCEPT; /* Connection is not NATed. */
  592. }
  593. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  594. /* Mark NAT done if successful and update the flow key. */
  595. if (err == NF_ACCEPT)
  596. ovs_nat_update_key(key, skb, maniptype);
  597. return err;
  598. }
  599. #else /* !CONFIG_NF_NAT_NEEDED */
  600. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  601. const struct ovs_conntrack_info *info,
  602. struct sk_buff *skb, struct nf_conn *ct,
  603. enum ip_conntrack_info ctinfo)
  604. {
  605. return NF_ACCEPT;
  606. }
  607. #endif
  608. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  609. * not done already. Update key with new CT state after passing the packet
  610. * through conntrack.
  611. * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
  612. * set to NULL and 0 will be returned.
  613. */
  614. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  615. const struct ovs_conntrack_info *info,
  616. struct sk_buff *skb)
  617. {
  618. /* If we are recirculating packets to match on conntrack fields and
  619. * committing with a separate conntrack action, then we don't need to
  620. * actually run the packet through conntrack twice unless it's for a
  621. * different zone.
  622. */
  623. bool cached = skb_nfct_cached(net, key, info, skb);
  624. enum ip_conntrack_info ctinfo;
  625. struct nf_conn *ct;
  626. if (!cached) {
  627. struct nf_conn *tmpl = info->ct;
  628. int err;
  629. /* Associate skb with specified zone. */
  630. if (tmpl) {
  631. if (skb->nfct)
  632. nf_conntrack_put(skb->nfct);
  633. nf_conntrack_get(&tmpl->ct_general);
  634. skb->nfct = &tmpl->ct_general;
  635. skb->nfctinfo = IP_CT_NEW;
  636. }
  637. /* Repeat if requested, see nf_iterate(). */
  638. do {
  639. err = nf_conntrack_in(net, info->family,
  640. NF_INET_PRE_ROUTING, skb);
  641. } while (err == NF_REPEAT);
  642. if (err != NF_ACCEPT)
  643. return -ENOENT;
  644. /* Clear CT state NAT flags to mark that we have not yet done
  645. * NAT after the nf_conntrack_in() call. We can actually clear
  646. * the whole state, as it will be re-initialized below.
  647. */
  648. key->ct.state = 0;
  649. /* Update the key, but keep the NAT flags. */
  650. ovs_ct_update_key(skb, info, key, true, true);
  651. }
  652. ct = nf_ct_get(skb, &ctinfo);
  653. if (ct) {
  654. /* Packets starting a new connection must be NATted before the
  655. * helper, so that the helper knows about the NAT. We enforce
  656. * this by delaying both NAT and helper calls for unconfirmed
  657. * connections until the committing CT action. For later
  658. * packets NAT and Helper may be called in either order.
  659. *
  660. * NAT will be done only if the CT action has NAT, and only
  661. * once per packet (per zone), as guarded by the NAT bits in
  662. * the key->ct.state.
  663. */
  664. if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
  665. (nf_ct_is_confirmed(ct) || info->commit) &&
  666. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  667. return -EINVAL;
  668. }
  669. /* Userspace may decide to perform a ct lookup without a helper
  670. * specified followed by a (recirculate and) commit with one.
  671. * Therefore, for unconfirmed connections which we will commit,
  672. * we need to attach the helper here.
  673. */
  674. if (!nf_ct_is_confirmed(ct) && info->commit &&
  675. info->helper && !nfct_help(ct)) {
  676. int err = __nf_ct_try_assign_helper(ct, info->ct,
  677. GFP_ATOMIC);
  678. if (err)
  679. return err;
  680. }
  681. /* Call the helper only if:
  682. * - nf_conntrack_in() was executed above ("!cached") for a
  683. * confirmed connection, or
  684. * - When committing an unconfirmed connection.
  685. */
  686. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  687. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  688. return -EINVAL;
  689. }
  690. }
  691. return 0;
  692. }
  693. /* Lookup connection and read fields into key. */
  694. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  695. const struct ovs_conntrack_info *info,
  696. struct sk_buff *skb)
  697. {
  698. struct nf_conntrack_expect *exp;
  699. /* If we pass an expected packet through nf_conntrack_in() the
  700. * expectation is typically removed, but the packet could still be
  701. * lost in upcall processing. To prevent this from happening we
  702. * perform an explicit expectation lookup. Expected connections are
  703. * always new, and will be passed through conntrack only when they are
  704. * committed, as it is OK to remove the expectation at that time.
  705. */
  706. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  707. if (exp) {
  708. u8 state;
  709. /* NOTE: New connections are NATted and Helped only when
  710. * committed, so we are not calling into NAT here.
  711. */
  712. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  713. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  714. } else {
  715. struct nf_conn *ct;
  716. int err;
  717. err = __ovs_ct_lookup(net, key, info, skb);
  718. if (err)
  719. return err;
  720. ct = (struct nf_conn *)skb->nfct;
  721. if (ct)
  722. nf_ct_deliver_cached_events(ct);
  723. }
  724. return 0;
  725. }
  726. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  727. {
  728. size_t i;
  729. for (i = 0; i < sizeof(*labels); i++)
  730. if (labels->ct_labels[i])
  731. return true;
  732. return false;
  733. }
  734. /* Lookup connection and confirm if unconfirmed. */
  735. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  736. const struct ovs_conntrack_info *info,
  737. struct sk_buff *skb)
  738. {
  739. int err;
  740. err = __ovs_ct_lookup(net, key, info, skb);
  741. if (err)
  742. return err;
  743. /* Apply changes before confirming the connection so that the initial
  744. * conntrack NEW netlink event carries the values given in the CT
  745. * action.
  746. */
  747. if (info->mark.mask) {
  748. err = ovs_ct_set_mark(skb, key, info->mark.value,
  749. info->mark.mask);
  750. if (err)
  751. return err;
  752. }
  753. if (labels_nonzero(&info->labels.mask)) {
  754. err = ovs_ct_set_labels(skb, key, &info->labels.value,
  755. &info->labels.mask);
  756. if (err)
  757. return err;
  758. }
  759. /* This will take care of sending queued events even if the connection
  760. * is already confirmed.
  761. */
  762. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  763. return -EINVAL;
  764. return 0;
  765. }
  766. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  767. * value if 'skb' is freed.
  768. */
  769. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  770. struct sw_flow_key *key,
  771. const struct ovs_conntrack_info *info)
  772. {
  773. int nh_ofs;
  774. int err;
  775. /* The conntrack module expects to be working at L3. */
  776. nh_ofs = skb_network_offset(skb);
  777. skb_pull(skb, nh_ofs);
  778. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  779. err = handle_fragments(net, key, info->zone.id, skb);
  780. if (err)
  781. return err;
  782. }
  783. if (info->commit)
  784. err = ovs_ct_commit(net, key, info, skb);
  785. else
  786. err = ovs_ct_lookup(net, key, info, skb);
  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. #ifdef CONFIG_NF_CONNTRACK_MARK
  1017. if (!info->commit && info->mark.mask) {
  1018. OVS_NLERR(log,
  1019. "Setting conntrack mark requires 'commit' flag.");
  1020. return -EINVAL;
  1021. }
  1022. #endif
  1023. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1024. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1025. OVS_NLERR(log,
  1026. "Setting conntrack labels requires 'commit' flag.");
  1027. return -EINVAL;
  1028. }
  1029. #endif
  1030. if (rem > 0) {
  1031. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1032. return -EINVAL;
  1033. }
  1034. return 0;
  1035. }
  1036. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1037. {
  1038. if (attr == OVS_KEY_ATTR_CT_STATE)
  1039. return true;
  1040. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1041. attr == OVS_KEY_ATTR_CT_ZONE)
  1042. return true;
  1043. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1044. attr == OVS_KEY_ATTR_CT_MARK)
  1045. return true;
  1046. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1047. attr == OVS_KEY_ATTR_CT_LABELS) {
  1048. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1049. return ovs_net->xt_label;
  1050. }
  1051. return false;
  1052. }
  1053. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1054. const struct sw_flow_key *key,
  1055. struct sw_flow_actions **sfa, bool log)
  1056. {
  1057. struct ovs_conntrack_info ct_info;
  1058. const char *helper = NULL;
  1059. u16 family;
  1060. int err;
  1061. family = key_to_nfproto(key);
  1062. if (family == NFPROTO_UNSPEC) {
  1063. OVS_NLERR(log, "ct family unspecified");
  1064. return -EINVAL;
  1065. }
  1066. memset(&ct_info, 0, sizeof(ct_info));
  1067. ct_info.family = family;
  1068. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1069. NF_CT_DEFAULT_ZONE_DIR, 0);
  1070. err = parse_ct(attr, &ct_info, &helper, log);
  1071. if (err)
  1072. return err;
  1073. /* Set up template for tracking connections in specific zones. */
  1074. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1075. if (!ct_info.ct) {
  1076. OVS_NLERR(log, "Failed to allocate conntrack template");
  1077. return -ENOMEM;
  1078. }
  1079. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1080. nf_conntrack_get(&ct_info.ct->ct_general);
  1081. if (helper) {
  1082. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1083. if (err)
  1084. goto err_free_ct;
  1085. }
  1086. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1087. sizeof(ct_info), log);
  1088. if (err)
  1089. goto err_free_ct;
  1090. return 0;
  1091. err_free_ct:
  1092. __ovs_ct_free_action(&ct_info);
  1093. return err;
  1094. }
  1095. #ifdef CONFIG_NF_NAT_NEEDED
  1096. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1097. struct sk_buff *skb)
  1098. {
  1099. struct nlattr *start;
  1100. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1101. if (!start)
  1102. return false;
  1103. if (info->nat & OVS_CT_SRC_NAT) {
  1104. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1105. return false;
  1106. } else if (info->nat & OVS_CT_DST_NAT) {
  1107. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1108. return false;
  1109. } else {
  1110. goto out;
  1111. }
  1112. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1113. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1114. info->family == NFPROTO_IPV4) {
  1115. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1116. info->range.min_addr.ip) ||
  1117. (info->range.max_addr.ip
  1118. != info->range.min_addr.ip &&
  1119. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1120. info->range.max_addr.ip))))
  1121. return false;
  1122. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1123. info->family == NFPROTO_IPV6) {
  1124. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1125. &info->range.min_addr.in6) ||
  1126. (memcmp(&info->range.max_addr.in6,
  1127. &info->range.min_addr.in6,
  1128. sizeof(info->range.max_addr.in6)) &&
  1129. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1130. &info->range.max_addr.in6))))
  1131. return false;
  1132. } else {
  1133. return false;
  1134. }
  1135. }
  1136. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1137. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1138. ntohs(info->range.min_proto.all)) ||
  1139. (info->range.max_proto.all != info->range.min_proto.all &&
  1140. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1141. ntohs(info->range.max_proto.all)))))
  1142. return false;
  1143. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1144. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1145. return false;
  1146. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1147. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1148. return false;
  1149. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1150. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1151. return false;
  1152. out:
  1153. nla_nest_end(skb, start);
  1154. return true;
  1155. }
  1156. #endif
  1157. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1158. struct sk_buff *skb)
  1159. {
  1160. struct nlattr *start;
  1161. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1162. if (!start)
  1163. return -EMSGSIZE;
  1164. if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
  1165. return -EMSGSIZE;
  1166. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1167. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1168. return -EMSGSIZE;
  1169. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1170. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1171. &ct_info->mark))
  1172. return -EMSGSIZE;
  1173. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1174. labels_nonzero(&ct_info->labels.mask) &&
  1175. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1176. &ct_info->labels))
  1177. return -EMSGSIZE;
  1178. if (ct_info->helper) {
  1179. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1180. ct_info->helper->name))
  1181. return -EMSGSIZE;
  1182. }
  1183. #ifdef CONFIG_NF_NAT_NEEDED
  1184. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1185. return -EMSGSIZE;
  1186. #endif
  1187. nla_nest_end(skb, start);
  1188. return 0;
  1189. }
  1190. void ovs_ct_free_action(const struct nlattr *a)
  1191. {
  1192. struct ovs_conntrack_info *ct_info = nla_data(a);
  1193. __ovs_ct_free_action(ct_info);
  1194. }
  1195. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1196. {
  1197. if (ct_info->helper)
  1198. module_put(ct_info->helper->me);
  1199. if (ct_info->ct)
  1200. nf_ct_put(ct_info->ct);
  1201. }
  1202. void ovs_ct_init(struct net *net)
  1203. {
  1204. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1205. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1206. if (nf_connlabels_get(net, n_bits - 1)) {
  1207. ovs_net->xt_label = false;
  1208. OVS_NLERR(true, "Failed to set connlabel length");
  1209. } else {
  1210. ovs_net->xt_label = true;
  1211. }
  1212. }
  1213. void ovs_ct_exit(struct net *net)
  1214. {
  1215. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1216. if (ovs_net->xt_label)
  1217. nf_connlabels_put(net);
  1218. }