conntrack.c 41 KB

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