conntrack.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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. const struct nf_conntrack_l3proto *l3proto;
  503. const 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. /* fall through */
  653. case IP_CT_NEW:
  654. /* Seen it before? This can happen for loopback, retrans,
  655. * or local packets.
  656. */
  657. if (!nf_nat_initialized(ct, maniptype)) {
  658. /* Initialize according to the NAT action. */
  659. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  660. /* Action is set up to establish a new
  661. * mapping.
  662. */
  663. ? nf_nat_setup_info(ct, range, maniptype)
  664. : nf_nat_alloc_null_binding(ct, hooknum);
  665. if (err != NF_ACCEPT)
  666. goto push;
  667. }
  668. break;
  669. case IP_CT_ESTABLISHED:
  670. case IP_CT_ESTABLISHED_REPLY:
  671. break;
  672. default:
  673. err = NF_DROP;
  674. goto push;
  675. }
  676. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  677. push:
  678. skb_push(skb, nh_off);
  679. skb_postpush_rcsum(skb, skb->data, nh_off);
  680. return err;
  681. }
  682. static void ovs_nat_update_key(struct sw_flow_key *key,
  683. const struct sk_buff *skb,
  684. enum nf_nat_manip_type maniptype)
  685. {
  686. if (maniptype == NF_NAT_MANIP_SRC) {
  687. __be16 src;
  688. key->ct_state |= OVS_CS_F_SRC_NAT;
  689. if (key->eth.type == htons(ETH_P_IP))
  690. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  691. else if (key->eth.type == htons(ETH_P_IPV6))
  692. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  693. sizeof(key->ipv6.addr.src));
  694. else
  695. return;
  696. if (key->ip.proto == IPPROTO_UDP)
  697. src = udp_hdr(skb)->source;
  698. else if (key->ip.proto == IPPROTO_TCP)
  699. src = tcp_hdr(skb)->source;
  700. else if (key->ip.proto == IPPROTO_SCTP)
  701. src = sctp_hdr(skb)->source;
  702. else
  703. return;
  704. key->tp.src = src;
  705. } else {
  706. __be16 dst;
  707. key->ct_state |= OVS_CS_F_DST_NAT;
  708. if (key->eth.type == htons(ETH_P_IP))
  709. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  710. else if (key->eth.type == htons(ETH_P_IPV6))
  711. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  712. sizeof(key->ipv6.addr.dst));
  713. else
  714. return;
  715. if (key->ip.proto == IPPROTO_UDP)
  716. dst = udp_hdr(skb)->dest;
  717. else if (key->ip.proto == IPPROTO_TCP)
  718. dst = tcp_hdr(skb)->dest;
  719. else if (key->ip.proto == IPPROTO_SCTP)
  720. dst = sctp_hdr(skb)->dest;
  721. else
  722. return;
  723. key->tp.dst = dst;
  724. }
  725. }
  726. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  727. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  728. const struct ovs_conntrack_info *info,
  729. struct sk_buff *skb, struct nf_conn *ct,
  730. enum ip_conntrack_info ctinfo)
  731. {
  732. enum nf_nat_manip_type maniptype;
  733. int err;
  734. /* Add NAT extension if not confirmed yet. */
  735. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  736. return NF_ACCEPT; /* Can't NAT. */
  737. /* Determine NAT type.
  738. * Check if the NAT type can be deduced from the tracked connection.
  739. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  740. * when committing.
  741. */
  742. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  743. ct->status & IPS_NAT_MASK &&
  744. (ctinfo != IP_CT_RELATED || info->commit)) {
  745. /* NAT an established or related connection like before. */
  746. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  747. /* This is the REPLY direction for a connection
  748. * for which NAT was applied in the forward
  749. * direction. Do the reverse NAT.
  750. */
  751. maniptype = ct->status & IPS_SRC_NAT
  752. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  753. else
  754. maniptype = ct->status & IPS_SRC_NAT
  755. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  756. } else if (info->nat & OVS_CT_SRC_NAT) {
  757. maniptype = NF_NAT_MANIP_SRC;
  758. } else if (info->nat & OVS_CT_DST_NAT) {
  759. maniptype = NF_NAT_MANIP_DST;
  760. } else {
  761. return NF_ACCEPT; /* Connection is not NATed. */
  762. }
  763. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  764. /* Mark NAT done if successful and update the flow key. */
  765. if (err == NF_ACCEPT)
  766. ovs_nat_update_key(key, skb, maniptype);
  767. return err;
  768. }
  769. #else /* !CONFIG_NF_NAT_NEEDED */
  770. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  771. const struct ovs_conntrack_info *info,
  772. struct sk_buff *skb, struct nf_conn *ct,
  773. enum ip_conntrack_info ctinfo)
  774. {
  775. return NF_ACCEPT;
  776. }
  777. #endif
  778. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  779. * not done already. Update key with new CT state after passing the packet
  780. * through conntrack.
  781. * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
  782. * set to NULL and 0 will be returned.
  783. */
  784. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  785. const struct ovs_conntrack_info *info,
  786. struct sk_buff *skb)
  787. {
  788. /* If we are recirculating packets to match on conntrack fields and
  789. * committing with a separate conntrack action, then we don't need to
  790. * actually run the packet through conntrack twice unless it's for a
  791. * different zone.
  792. */
  793. bool cached = skb_nfct_cached(net, key, info, skb);
  794. enum ip_conntrack_info ctinfo;
  795. struct nf_conn *ct;
  796. if (!cached) {
  797. struct nf_conn *tmpl = info->ct;
  798. int err;
  799. /* Associate skb with specified zone. */
  800. if (tmpl) {
  801. if (skb_nfct(skb))
  802. nf_conntrack_put(skb_nfct(skb));
  803. nf_conntrack_get(&tmpl->ct_general);
  804. nf_ct_set(skb, tmpl, IP_CT_NEW);
  805. }
  806. err = nf_conntrack_in(net, info->family,
  807. NF_INET_PRE_ROUTING, skb);
  808. if (err != NF_ACCEPT)
  809. return -ENOENT;
  810. /* Clear CT state NAT flags to mark that we have not yet done
  811. * NAT after the nf_conntrack_in() call. We can actually clear
  812. * the whole state, as it will be re-initialized below.
  813. */
  814. key->ct_state = 0;
  815. /* Update the key, but keep the NAT flags. */
  816. ovs_ct_update_key(skb, info, key, true, true);
  817. }
  818. ct = nf_ct_get(skb, &ctinfo);
  819. if (ct) {
  820. /* Packets starting a new connection must be NATted before the
  821. * helper, so that the helper knows about the NAT. We enforce
  822. * this by delaying both NAT and helper calls for unconfirmed
  823. * connections until the committing CT action. For later
  824. * packets NAT and Helper may be called in either order.
  825. *
  826. * NAT will be done only if the CT action has NAT, and only
  827. * once per packet (per zone), as guarded by the NAT bits in
  828. * the key->ct_state.
  829. */
  830. if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
  831. (nf_ct_is_confirmed(ct) || info->commit) &&
  832. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  833. return -EINVAL;
  834. }
  835. /* Userspace may decide to perform a ct lookup without a helper
  836. * specified followed by a (recirculate and) commit with one.
  837. * Therefore, for unconfirmed connections which we will commit,
  838. * we need to attach the helper here.
  839. */
  840. if (!nf_ct_is_confirmed(ct) && info->commit &&
  841. info->helper && !nfct_help(ct)) {
  842. int err = __nf_ct_try_assign_helper(ct, info->ct,
  843. GFP_ATOMIC);
  844. if (err)
  845. return err;
  846. }
  847. /* Call the helper only if:
  848. * - nf_conntrack_in() was executed above ("!cached") for a
  849. * confirmed connection, or
  850. * - When committing an unconfirmed connection.
  851. */
  852. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  853. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  854. return -EINVAL;
  855. }
  856. }
  857. return 0;
  858. }
  859. /* Lookup connection and read fields into key. */
  860. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  861. const struct ovs_conntrack_info *info,
  862. struct sk_buff *skb)
  863. {
  864. struct nf_conntrack_expect *exp;
  865. /* If we pass an expected packet through nf_conntrack_in() the
  866. * expectation is typically removed, but the packet could still be
  867. * lost in upcall processing. To prevent this from happening we
  868. * perform an explicit expectation lookup. Expected connections are
  869. * always new, and will be passed through conntrack only when they are
  870. * committed, as it is OK to remove the expectation at that time.
  871. */
  872. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  873. if (exp) {
  874. u8 state;
  875. /* NOTE: New connections are NATted and Helped only when
  876. * committed, so we are not calling into NAT here.
  877. */
  878. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  879. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  880. } else {
  881. struct nf_conn *ct;
  882. int err;
  883. err = __ovs_ct_lookup(net, key, info, skb);
  884. if (err)
  885. return err;
  886. ct = (struct nf_conn *)skb_nfct(skb);
  887. if (ct)
  888. nf_ct_deliver_cached_events(ct);
  889. }
  890. return 0;
  891. }
  892. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  893. {
  894. size_t i;
  895. for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
  896. if (labels->ct_labels_32[i])
  897. return true;
  898. return false;
  899. }
  900. /* Lookup connection and confirm if unconfirmed. */
  901. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  902. const struct ovs_conntrack_info *info,
  903. struct sk_buff *skb)
  904. {
  905. enum ip_conntrack_info ctinfo;
  906. struct nf_conn *ct;
  907. int err;
  908. err = __ovs_ct_lookup(net, key, info, skb);
  909. if (err)
  910. return err;
  911. /* The connection could be invalid, in which case this is a no-op.*/
  912. ct = nf_ct_get(skb, &ctinfo);
  913. if (!ct)
  914. return 0;
  915. /* Set the conntrack event mask if given. NEW and DELETE events have
  916. * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
  917. * typically would receive many kinds of updates. Setting the event
  918. * mask allows those events to be filtered. The set event mask will
  919. * remain in effect for the lifetime of the connection unless changed
  920. * by a further CT action with both the commit flag and the eventmask
  921. * option. */
  922. if (info->have_eventmask) {
  923. struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
  924. if (cache)
  925. cache->ctmask = info->eventmask;
  926. }
  927. /* Apply changes before confirming the connection so that the initial
  928. * conntrack NEW netlink event carries the values given in the CT
  929. * action.
  930. */
  931. if (info->mark.mask) {
  932. err = ovs_ct_set_mark(ct, key, info->mark.value,
  933. info->mark.mask);
  934. if (err)
  935. return err;
  936. }
  937. if (!nf_ct_is_confirmed(ct)) {
  938. err = ovs_ct_init_labels(ct, key, &info->labels.value,
  939. &info->labels.mask);
  940. if (err)
  941. return err;
  942. } else if (labels_nonzero(&info->labels.mask)) {
  943. err = ovs_ct_set_labels(ct, key, &info->labels.value,
  944. &info->labels.mask);
  945. if (err)
  946. return err;
  947. }
  948. /* This will take care of sending queued events even if the connection
  949. * is already confirmed.
  950. */
  951. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  952. return -EINVAL;
  953. return 0;
  954. }
  955. /* Trim the skb to the length specified by the IP/IPv6 header,
  956. * removing any trailing lower-layer padding. This prepares the skb
  957. * for higher-layer processing that assumes skb->len excludes padding
  958. * (such as nf_ip_checksum). The caller needs to pull the skb to the
  959. * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
  960. */
  961. static int ovs_skb_network_trim(struct sk_buff *skb)
  962. {
  963. unsigned int len;
  964. int err;
  965. switch (skb->protocol) {
  966. case htons(ETH_P_IP):
  967. len = ntohs(ip_hdr(skb)->tot_len);
  968. break;
  969. case htons(ETH_P_IPV6):
  970. len = sizeof(struct ipv6hdr)
  971. + ntohs(ipv6_hdr(skb)->payload_len);
  972. break;
  973. default:
  974. len = skb->len;
  975. }
  976. err = pskb_trim_rcsum(skb, len);
  977. if (err)
  978. kfree_skb(skb);
  979. return err;
  980. }
  981. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  982. * value if 'skb' is freed.
  983. */
  984. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  985. struct sw_flow_key *key,
  986. const struct ovs_conntrack_info *info)
  987. {
  988. int nh_ofs;
  989. int err;
  990. /* The conntrack module expects to be working at L3. */
  991. nh_ofs = skb_network_offset(skb);
  992. skb_pull_rcsum(skb, nh_ofs);
  993. err = ovs_skb_network_trim(skb);
  994. if (err)
  995. return err;
  996. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  997. err = handle_fragments(net, key, info->zone.id, skb);
  998. if (err)
  999. return err;
  1000. }
  1001. if (info->commit)
  1002. err = ovs_ct_commit(net, key, info, skb);
  1003. else
  1004. err = ovs_ct_lookup(net, key, info, skb);
  1005. skb_push(skb, nh_ofs);
  1006. skb_postpush_rcsum(skb, skb->data, nh_ofs);
  1007. if (err)
  1008. kfree_skb(skb);
  1009. return err;
  1010. }
  1011. int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
  1012. {
  1013. if (skb_nfct(skb)) {
  1014. nf_conntrack_put(skb_nfct(skb));
  1015. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  1016. ovs_ct_fill_key(skb, key);
  1017. }
  1018. return 0;
  1019. }
  1020. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  1021. const struct sw_flow_key *key, bool log)
  1022. {
  1023. struct nf_conntrack_helper *helper;
  1024. struct nf_conn_help *help;
  1025. helper = nf_conntrack_helper_try_module_get(name, info->family,
  1026. key->ip.proto);
  1027. if (!helper) {
  1028. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  1029. return -EINVAL;
  1030. }
  1031. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  1032. if (!help) {
  1033. nf_conntrack_helper_put(helper);
  1034. return -ENOMEM;
  1035. }
  1036. rcu_assign_pointer(help->helper, helper);
  1037. info->helper = helper;
  1038. return 0;
  1039. }
  1040. #ifdef CONFIG_NF_NAT_NEEDED
  1041. static int parse_nat(const struct nlattr *attr,
  1042. struct ovs_conntrack_info *info, bool log)
  1043. {
  1044. struct nlattr *a;
  1045. int rem;
  1046. bool have_ip_max = false;
  1047. bool have_proto_max = false;
  1048. bool ip_vers = (info->family == NFPROTO_IPV6);
  1049. nla_for_each_nested(a, attr, rem) {
  1050. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  1051. [OVS_NAT_ATTR_SRC] = {0, 0},
  1052. [OVS_NAT_ATTR_DST] = {0, 0},
  1053. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  1054. sizeof(struct in6_addr)},
  1055. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  1056. sizeof(struct in6_addr)},
  1057. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  1058. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  1059. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  1060. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  1061. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  1062. };
  1063. int type = nla_type(a);
  1064. if (type > OVS_NAT_ATTR_MAX) {
  1065. OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
  1066. type, OVS_NAT_ATTR_MAX);
  1067. return -EINVAL;
  1068. }
  1069. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  1070. OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
  1071. type, nla_len(a),
  1072. ovs_nat_attr_lens[type][ip_vers]);
  1073. return -EINVAL;
  1074. }
  1075. switch (type) {
  1076. case OVS_NAT_ATTR_SRC:
  1077. case OVS_NAT_ATTR_DST:
  1078. if (info->nat) {
  1079. OVS_NLERR(log, "Only one type of NAT may be specified");
  1080. return -ERANGE;
  1081. }
  1082. info->nat |= OVS_CT_NAT;
  1083. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  1084. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  1085. break;
  1086. case OVS_NAT_ATTR_IP_MIN:
  1087. nla_memcpy(&info->range.min_addr, a,
  1088. sizeof(info->range.min_addr));
  1089. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1090. break;
  1091. case OVS_NAT_ATTR_IP_MAX:
  1092. have_ip_max = true;
  1093. nla_memcpy(&info->range.max_addr, a,
  1094. sizeof(info->range.max_addr));
  1095. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1096. break;
  1097. case OVS_NAT_ATTR_PROTO_MIN:
  1098. info->range.min_proto.all = htons(nla_get_u16(a));
  1099. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1100. break;
  1101. case OVS_NAT_ATTR_PROTO_MAX:
  1102. have_proto_max = true;
  1103. info->range.max_proto.all = htons(nla_get_u16(a));
  1104. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1105. break;
  1106. case OVS_NAT_ATTR_PERSISTENT:
  1107. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  1108. break;
  1109. case OVS_NAT_ATTR_PROTO_HASH:
  1110. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  1111. break;
  1112. case OVS_NAT_ATTR_PROTO_RANDOM:
  1113. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  1114. break;
  1115. default:
  1116. OVS_NLERR(log, "Unknown nat attribute (%d)", type);
  1117. return -EINVAL;
  1118. }
  1119. }
  1120. if (rem > 0) {
  1121. OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
  1122. return -EINVAL;
  1123. }
  1124. if (!info->nat) {
  1125. /* Do not allow flags if no type is given. */
  1126. if (info->range.flags) {
  1127. OVS_NLERR(log,
  1128. "NAT flags may be given only when NAT range (SRC or DST) is also specified."
  1129. );
  1130. return -EINVAL;
  1131. }
  1132. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  1133. } else if (!info->commit) {
  1134. OVS_NLERR(log,
  1135. "NAT attributes may be specified only when CT COMMIT flag is also specified."
  1136. );
  1137. return -EINVAL;
  1138. }
  1139. /* Allow missing IP_MAX. */
  1140. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  1141. memcpy(&info->range.max_addr, &info->range.min_addr,
  1142. sizeof(info->range.max_addr));
  1143. }
  1144. /* Allow missing PROTO_MAX. */
  1145. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1146. !have_proto_max) {
  1147. info->range.max_proto.all = info->range.min_proto.all;
  1148. }
  1149. return 0;
  1150. }
  1151. #endif
  1152. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  1153. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1154. [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1155. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  1156. .maxlen = sizeof(u16) },
  1157. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  1158. .maxlen = sizeof(struct md_mark) },
  1159. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  1160. .maxlen = sizeof(struct md_labels) },
  1161. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  1162. .maxlen = NF_CT_HELPER_NAME_LEN },
  1163. #ifdef CONFIG_NF_NAT_NEEDED
  1164. /* NAT length is checked when parsing the nested attributes. */
  1165. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  1166. #endif
  1167. [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
  1168. .maxlen = sizeof(u32) },
  1169. };
  1170. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  1171. const char **helper, bool log)
  1172. {
  1173. struct nlattr *a;
  1174. int rem;
  1175. nla_for_each_nested(a, attr, rem) {
  1176. int type = nla_type(a);
  1177. int maxlen;
  1178. int minlen;
  1179. if (type > OVS_CT_ATTR_MAX) {
  1180. OVS_NLERR(log,
  1181. "Unknown conntrack attr (type=%d, max=%d)",
  1182. type, OVS_CT_ATTR_MAX);
  1183. return -EINVAL;
  1184. }
  1185. maxlen = ovs_ct_attr_lens[type].maxlen;
  1186. minlen = ovs_ct_attr_lens[type].minlen;
  1187. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  1188. OVS_NLERR(log,
  1189. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  1190. type, nla_len(a), maxlen);
  1191. return -EINVAL;
  1192. }
  1193. switch (type) {
  1194. case OVS_CT_ATTR_FORCE_COMMIT:
  1195. info->force = true;
  1196. /* fall through. */
  1197. case OVS_CT_ATTR_COMMIT:
  1198. info->commit = true;
  1199. break;
  1200. #ifdef CONFIG_NF_CONNTRACK_ZONES
  1201. case OVS_CT_ATTR_ZONE:
  1202. info->zone.id = nla_get_u16(a);
  1203. break;
  1204. #endif
  1205. #ifdef CONFIG_NF_CONNTRACK_MARK
  1206. case OVS_CT_ATTR_MARK: {
  1207. struct md_mark *mark = nla_data(a);
  1208. if (!mark->mask) {
  1209. OVS_NLERR(log, "ct_mark mask cannot be 0");
  1210. return -EINVAL;
  1211. }
  1212. info->mark = *mark;
  1213. break;
  1214. }
  1215. #endif
  1216. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1217. case OVS_CT_ATTR_LABELS: {
  1218. struct md_labels *labels = nla_data(a);
  1219. if (!labels_nonzero(&labels->mask)) {
  1220. OVS_NLERR(log, "ct_labels mask cannot be 0");
  1221. return -EINVAL;
  1222. }
  1223. info->labels = *labels;
  1224. break;
  1225. }
  1226. #endif
  1227. case OVS_CT_ATTR_HELPER:
  1228. *helper = nla_data(a);
  1229. if (!memchr(*helper, '\0', nla_len(a))) {
  1230. OVS_NLERR(log, "Invalid conntrack helper");
  1231. return -EINVAL;
  1232. }
  1233. break;
  1234. #ifdef CONFIG_NF_NAT_NEEDED
  1235. case OVS_CT_ATTR_NAT: {
  1236. int err = parse_nat(a, info, log);
  1237. if (err)
  1238. return err;
  1239. break;
  1240. }
  1241. #endif
  1242. case OVS_CT_ATTR_EVENTMASK:
  1243. info->have_eventmask = true;
  1244. info->eventmask = nla_get_u32(a);
  1245. break;
  1246. default:
  1247. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  1248. type);
  1249. return -EINVAL;
  1250. }
  1251. }
  1252. #ifdef CONFIG_NF_CONNTRACK_MARK
  1253. if (!info->commit && info->mark.mask) {
  1254. OVS_NLERR(log,
  1255. "Setting conntrack mark requires 'commit' flag.");
  1256. return -EINVAL;
  1257. }
  1258. #endif
  1259. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1260. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1261. OVS_NLERR(log,
  1262. "Setting conntrack labels requires 'commit' flag.");
  1263. return -EINVAL;
  1264. }
  1265. #endif
  1266. if (rem > 0) {
  1267. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1268. return -EINVAL;
  1269. }
  1270. return 0;
  1271. }
  1272. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1273. {
  1274. if (attr == OVS_KEY_ATTR_CT_STATE)
  1275. return true;
  1276. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1277. attr == OVS_KEY_ATTR_CT_ZONE)
  1278. return true;
  1279. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1280. attr == OVS_KEY_ATTR_CT_MARK)
  1281. return true;
  1282. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1283. attr == OVS_KEY_ATTR_CT_LABELS) {
  1284. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1285. return ovs_net->xt_label;
  1286. }
  1287. return false;
  1288. }
  1289. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1290. const struct sw_flow_key *key,
  1291. struct sw_flow_actions **sfa, bool log)
  1292. {
  1293. struct ovs_conntrack_info ct_info;
  1294. const char *helper = NULL;
  1295. u16 family;
  1296. int err;
  1297. family = key_to_nfproto(key);
  1298. if (family == NFPROTO_UNSPEC) {
  1299. OVS_NLERR(log, "ct family unspecified");
  1300. return -EINVAL;
  1301. }
  1302. memset(&ct_info, 0, sizeof(ct_info));
  1303. ct_info.family = family;
  1304. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1305. NF_CT_DEFAULT_ZONE_DIR, 0);
  1306. err = parse_ct(attr, &ct_info, &helper, log);
  1307. if (err)
  1308. return err;
  1309. /* Set up template for tracking connections in specific zones. */
  1310. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1311. if (!ct_info.ct) {
  1312. OVS_NLERR(log, "Failed to allocate conntrack template");
  1313. return -ENOMEM;
  1314. }
  1315. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1316. nf_conntrack_get(&ct_info.ct->ct_general);
  1317. if (helper) {
  1318. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1319. if (err)
  1320. goto err_free_ct;
  1321. }
  1322. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1323. sizeof(ct_info), log);
  1324. if (err)
  1325. goto err_free_ct;
  1326. return 0;
  1327. err_free_ct:
  1328. __ovs_ct_free_action(&ct_info);
  1329. return err;
  1330. }
  1331. #ifdef CONFIG_NF_NAT_NEEDED
  1332. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1333. struct sk_buff *skb)
  1334. {
  1335. struct nlattr *start;
  1336. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1337. if (!start)
  1338. return false;
  1339. if (info->nat & OVS_CT_SRC_NAT) {
  1340. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1341. return false;
  1342. } else if (info->nat & OVS_CT_DST_NAT) {
  1343. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1344. return false;
  1345. } else {
  1346. goto out;
  1347. }
  1348. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1349. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1350. info->family == NFPROTO_IPV4) {
  1351. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1352. info->range.min_addr.ip) ||
  1353. (info->range.max_addr.ip
  1354. != info->range.min_addr.ip &&
  1355. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1356. info->range.max_addr.ip))))
  1357. return false;
  1358. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1359. info->family == NFPROTO_IPV6) {
  1360. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1361. &info->range.min_addr.in6) ||
  1362. (memcmp(&info->range.max_addr.in6,
  1363. &info->range.min_addr.in6,
  1364. sizeof(info->range.max_addr.in6)) &&
  1365. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1366. &info->range.max_addr.in6))))
  1367. return false;
  1368. } else {
  1369. return false;
  1370. }
  1371. }
  1372. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1373. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1374. ntohs(info->range.min_proto.all)) ||
  1375. (info->range.max_proto.all != info->range.min_proto.all &&
  1376. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1377. ntohs(info->range.max_proto.all)))))
  1378. return false;
  1379. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1380. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1381. return false;
  1382. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1383. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1384. return false;
  1385. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1386. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1387. return false;
  1388. out:
  1389. nla_nest_end(skb, start);
  1390. return true;
  1391. }
  1392. #endif
  1393. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1394. struct sk_buff *skb)
  1395. {
  1396. struct nlattr *start;
  1397. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1398. if (!start)
  1399. return -EMSGSIZE;
  1400. if (ct_info->commit && nla_put_flag(skb, ct_info->force
  1401. ? OVS_CT_ATTR_FORCE_COMMIT
  1402. : OVS_CT_ATTR_COMMIT))
  1403. return -EMSGSIZE;
  1404. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1405. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1406. return -EMSGSIZE;
  1407. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1408. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1409. &ct_info->mark))
  1410. return -EMSGSIZE;
  1411. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1412. labels_nonzero(&ct_info->labels.mask) &&
  1413. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1414. &ct_info->labels))
  1415. return -EMSGSIZE;
  1416. if (ct_info->helper) {
  1417. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1418. ct_info->helper->name))
  1419. return -EMSGSIZE;
  1420. }
  1421. if (ct_info->have_eventmask &&
  1422. nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
  1423. return -EMSGSIZE;
  1424. #ifdef CONFIG_NF_NAT_NEEDED
  1425. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1426. return -EMSGSIZE;
  1427. #endif
  1428. nla_nest_end(skb, start);
  1429. return 0;
  1430. }
  1431. void ovs_ct_free_action(const struct nlattr *a)
  1432. {
  1433. struct ovs_conntrack_info *ct_info = nla_data(a);
  1434. __ovs_ct_free_action(ct_info);
  1435. }
  1436. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1437. {
  1438. if (ct_info->helper)
  1439. nf_conntrack_helper_put(ct_info->helper);
  1440. if (ct_info->ct)
  1441. nf_ct_tmpl_free(ct_info->ct);
  1442. }
  1443. void ovs_ct_init(struct net *net)
  1444. {
  1445. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1446. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1447. if (nf_connlabels_get(net, n_bits - 1)) {
  1448. ovs_net->xt_label = false;
  1449. OVS_NLERR(true, "Failed to set connlabel length");
  1450. } else {
  1451. ovs_net->xt_label = true;
  1452. }
  1453. }
  1454. void ovs_ct_exit(struct net *net)
  1455. {
  1456. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1457. if (ovs_net->xt_label)
  1458. nf_connlabels_put(net);
  1459. }