conntrack.c 44 KB

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