conntrack.c 44 KB

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