conntrack.c 36 KB

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