conntrack.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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 = sizeof(cl->bits);
  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 || sizeof(cl->bits) < 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. skb_orphan(skb);
  322. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  323. err = nf_ct_frag6_gather(net, skb, user);
  324. if (err) {
  325. if (err != -EINPROGRESS)
  326. kfree_skb(skb);
  327. return err;
  328. }
  329. key->ip.proto = ipv6_hdr(skb)->nexthdr;
  330. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  331. #endif
  332. } else {
  333. kfree_skb(skb);
  334. return -EPFNOSUPPORT;
  335. }
  336. key->ip.frag = OVS_FRAG_TYPE_NONE;
  337. skb_clear_hash(skb);
  338. skb->ignore_df = 1;
  339. *OVS_CB(skb) = ovs_cb;
  340. return 0;
  341. }
  342. static struct nf_conntrack_expect *
  343. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  344. u16 proto, const struct sk_buff *skb)
  345. {
  346. struct nf_conntrack_tuple tuple;
  347. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
  348. return NULL;
  349. return __nf_ct_expect_find(net, zone, &tuple);
  350. }
  351. /* This replicates logic from nf_conntrack_core.c that is not exported. */
  352. static enum ip_conntrack_info
  353. ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
  354. {
  355. const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
  356. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  357. return IP_CT_ESTABLISHED_REPLY;
  358. /* Once we've had two way comms, always ESTABLISHED. */
  359. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
  360. return IP_CT_ESTABLISHED;
  361. if (test_bit(IPS_EXPECTED_BIT, &ct->status))
  362. return IP_CT_RELATED;
  363. return IP_CT_NEW;
  364. }
  365. /* Find an existing connection which this packet belongs to without
  366. * re-attributing statistics or modifying the connection state. This allows an
  367. * skb->nfct lost due to an upcall to be recovered during actions execution.
  368. *
  369. * Must be called with rcu_read_lock.
  370. *
  371. * On success, populates skb->nfct and skb->nfctinfo, and returns the
  372. * connection. Returns NULL if there is no existing entry.
  373. */
  374. static struct nf_conn *
  375. ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
  376. u8 l3num, struct sk_buff *skb)
  377. {
  378. struct nf_conntrack_l3proto *l3proto;
  379. struct nf_conntrack_l4proto *l4proto;
  380. struct nf_conntrack_tuple tuple;
  381. struct nf_conntrack_tuple_hash *h;
  382. struct nf_conn *ct;
  383. unsigned int dataoff;
  384. u8 protonum;
  385. l3proto = __nf_ct_l3proto_find(l3num);
  386. if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff,
  387. &protonum) <= 0) {
  388. pr_debug("ovs_ct_find_existing: Can't get protonum\n");
  389. return NULL;
  390. }
  391. l4proto = __nf_ct_l4proto_find(l3num, protonum);
  392. if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num,
  393. protonum, net, &tuple, l3proto, l4proto)) {
  394. pr_debug("ovs_ct_find_existing: Can't get tuple\n");
  395. return NULL;
  396. }
  397. /* look for tuple match */
  398. h = nf_conntrack_find_get(net, zone, &tuple);
  399. if (!h)
  400. return NULL; /* Not found. */
  401. ct = nf_ct_tuplehash_to_ctrack(h);
  402. skb->nfct = &ct->ct_general;
  403. skb->nfctinfo = ovs_ct_get_info(h);
  404. return ct;
  405. }
  406. /* Determine whether skb->nfct is equal to the result of conntrack lookup. */
  407. static bool skb_nfct_cached(struct net *net,
  408. const struct sw_flow_key *key,
  409. const struct ovs_conntrack_info *info,
  410. struct sk_buff *skb)
  411. {
  412. enum ip_conntrack_info ctinfo;
  413. struct nf_conn *ct;
  414. ct = nf_ct_get(skb, &ctinfo);
  415. /* If no ct, check if we have evidence that an existing conntrack entry
  416. * might be found for this skb. This happens when we lose a skb->nfct
  417. * due to an upcall. If the connection was not confirmed, it is not
  418. * cached and needs to be run through conntrack again.
  419. */
  420. if (!ct && key->ct.state & OVS_CS_F_TRACKED &&
  421. !(key->ct.state & OVS_CS_F_INVALID) &&
  422. key->ct.zone == info->zone.id)
  423. ct = ovs_ct_find_existing(net, &info->zone, info->family, skb);
  424. if (!ct)
  425. return false;
  426. if (!net_eq(net, read_pnet(&ct->ct_net)))
  427. return false;
  428. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  429. return false;
  430. if (info->helper) {
  431. struct nf_conn_help *help;
  432. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  433. if (help && rcu_access_pointer(help->helper) != info->helper)
  434. return false;
  435. }
  436. return true;
  437. }
  438. #ifdef CONFIG_NF_NAT_NEEDED
  439. /* Modelled after nf_nat_ipv[46]_fn().
  440. * range is only used for new, uninitialized NAT state.
  441. * Returns either NF_ACCEPT or NF_DROP.
  442. */
  443. static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
  444. enum ip_conntrack_info ctinfo,
  445. const struct nf_nat_range *range,
  446. enum nf_nat_manip_type maniptype)
  447. {
  448. int hooknum, nh_off, err = NF_ACCEPT;
  449. nh_off = skb_network_offset(skb);
  450. skb_pull(skb, nh_off);
  451. /* See HOOK2MANIP(). */
  452. if (maniptype == NF_NAT_MANIP_SRC)
  453. hooknum = NF_INET_LOCAL_IN; /* Source NAT */
  454. else
  455. hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
  456. switch (ctinfo) {
  457. case IP_CT_RELATED:
  458. case IP_CT_RELATED_REPLY:
  459. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  460. skb->protocol == htons(ETH_P_IP) &&
  461. ip_hdr(skb)->protocol == IPPROTO_ICMP) {
  462. if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
  463. hooknum))
  464. err = NF_DROP;
  465. goto push;
  466. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  467. skb->protocol == htons(ETH_P_IPV6)) {
  468. __be16 frag_off;
  469. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  470. int hdrlen = ipv6_skip_exthdr(skb,
  471. sizeof(struct ipv6hdr),
  472. &nexthdr, &frag_off);
  473. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  474. if (!nf_nat_icmpv6_reply_translation(skb, ct,
  475. ctinfo,
  476. hooknum,
  477. hdrlen))
  478. err = NF_DROP;
  479. goto push;
  480. }
  481. }
  482. /* Non-ICMP, fall thru to initialize if needed. */
  483. case IP_CT_NEW:
  484. /* Seen it before? This can happen for loopback, retrans,
  485. * or local packets.
  486. */
  487. if (!nf_nat_initialized(ct, maniptype)) {
  488. /* Initialize according to the NAT action. */
  489. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  490. /* Action is set up to establish a new
  491. * mapping.
  492. */
  493. ? nf_nat_setup_info(ct, range, maniptype)
  494. : nf_nat_alloc_null_binding(ct, hooknum);
  495. if (err != NF_ACCEPT)
  496. goto push;
  497. }
  498. break;
  499. case IP_CT_ESTABLISHED:
  500. case IP_CT_ESTABLISHED_REPLY:
  501. break;
  502. default:
  503. err = NF_DROP;
  504. goto push;
  505. }
  506. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  507. push:
  508. skb_push(skb, nh_off);
  509. return err;
  510. }
  511. static void ovs_nat_update_key(struct sw_flow_key *key,
  512. const struct sk_buff *skb,
  513. enum nf_nat_manip_type maniptype)
  514. {
  515. if (maniptype == NF_NAT_MANIP_SRC) {
  516. __be16 src;
  517. key->ct.state |= OVS_CS_F_SRC_NAT;
  518. if (key->eth.type == htons(ETH_P_IP))
  519. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  520. else if (key->eth.type == htons(ETH_P_IPV6))
  521. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  522. sizeof(key->ipv6.addr.src));
  523. else
  524. return;
  525. if (key->ip.proto == IPPROTO_UDP)
  526. src = udp_hdr(skb)->source;
  527. else if (key->ip.proto == IPPROTO_TCP)
  528. src = tcp_hdr(skb)->source;
  529. else if (key->ip.proto == IPPROTO_SCTP)
  530. src = sctp_hdr(skb)->source;
  531. else
  532. return;
  533. key->tp.src = src;
  534. } else {
  535. __be16 dst;
  536. key->ct.state |= OVS_CS_F_DST_NAT;
  537. if (key->eth.type == htons(ETH_P_IP))
  538. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  539. else if (key->eth.type == htons(ETH_P_IPV6))
  540. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  541. sizeof(key->ipv6.addr.dst));
  542. else
  543. return;
  544. if (key->ip.proto == IPPROTO_UDP)
  545. dst = udp_hdr(skb)->dest;
  546. else if (key->ip.proto == IPPROTO_TCP)
  547. dst = tcp_hdr(skb)->dest;
  548. else if (key->ip.proto == IPPROTO_SCTP)
  549. dst = sctp_hdr(skb)->dest;
  550. else
  551. return;
  552. key->tp.dst = dst;
  553. }
  554. }
  555. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  556. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  557. const struct ovs_conntrack_info *info,
  558. struct sk_buff *skb, struct nf_conn *ct,
  559. enum ip_conntrack_info ctinfo)
  560. {
  561. enum nf_nat_manip_type maniptype;
  562. int err;
  563. if (nf_ct_is_untracked(ct)) {
  564. /* A NAT action may only be performed on tracked packets. */
  565. return NF_ACCEPT;
  566. }
  567. /* Add NAT extension if not confirmed yet. */
  568. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  569. return NF_ACCEPT; /* Can't NAT. */
  570. /* Determine NAT type.
  571. * Check if the NAT type can be deduced from the tracked connection.
  572. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  573. * when committing.
  574. */
  575. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  576. ct->status & IPS_NAT_MASK &&
  577. (ctinfo != IP_CT_RELATED || info->commit)) {
  578. /* NAT an established or related connection like before. */
  579. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  580. /* This is the REPLY direction for a connection
  581. * for which NAT was applied in the forward
  582. * direction. Do the reverse NAT.
  583. */
  584. maniptype = ct->status & IPS_SRC_NAT
  585. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  586. else
  587. maniptype = ct->status & IPS_SRC_NAT
  588. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  589. } else if (info->nat & OVS_CT_SRC_NAT) {
  590. maniptype = NF_NAT_MANIP_SRC;
  591. } else if (info->nat & OVS_CT_DST_NAT) {
  592. maniptype = NF_NAT_MANIP_DST;
  593. } else {
  594. return NF_ACCEPT; /* Connection is not NATed. */
  595. }
  596. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  597. /* Mark NAT done if successful and update the flow key. */
  598. if (err == NF_ACCEPT)
  599. ovs_nat_update_key(key, skb, maniptype);
  600. return err;
  601. }
  602. #else /* !CONFIG_NF_NAT_NEEDED */
  603. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  604. const struct ovs_conntrack_info *info,
  605. struct sk_buff *skb, struct nf_conn *ct,
  606. enum ip_conntrack_info ctinfo)
  607. {
  608. return NF_ACCEPT;
  609. }
  610. #endif
  611. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  612. * not done already. Update key with new CT state after passing the packet
  613. * through conntrack.
  614. * Note that if the packet is deemed invalid by conntrack, skb->nfct will be
  615. * set to NULL and 0 will be returned.
  616. */
  617. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  618. const struct ovs_conntrack_info *info,
  619. struct sk_buff *skb)
  620. {
  621. /* If we are recirculating packets to match on conntrack fields and
  622. * committing with a separate conntrack action, then we don't need to
  623. * actually run the packet through conntrack twice unless it's for a
  624. * different zone.
  625. */
  626. bool cached = skb_nfct_cached(net, key, info, skb);
  627. enum ip_conntrack_info ctinfo;
  628. struct nf_conn *ct;
  629. if (!cached) {
  630. struct nf_conn *tmpl = info->ct;
  631. int err;
  632. /* Associate skb with specified zone. */
  633. if (tmpl) {
  634. if (skb->nfct)
  635. nf_conntrack_put(skb->nfct);
  636. nf_conntrack_get(&tmpl->ct_general);
  637. skb->nfct = &tmpl->ct_general;
  638. skb->nfctinfo = IP_CT_NEW;
  639. }
  640. err = nf_conntrack_in(net, info->family,
  641. NF_INET_PRE_ROUTING, skb);
  642. if (err != NF_ACCEPT)
  643. return -ENOENT;
  644. /* Clear CT state NAT flags to mark that we have not yet done
  645. * NAT after the nf_conntrack_in() call. We can actually clear
  646. * the whole state, as it will be re-initialized below.
  647. */
  648. key->ct.state = 0;
  649. /* Update the key, but keep the NAT flags. */
  650. ovs_ct_update_key(skb, info, key, true, true);
  651. }
  652. ct = nf_ct_get(skb, &ctinfo);
  653. if (ct) {
  654. /* Packets starting a new connection must be NATted before the
  655. * helper, so that the helper knows about the NAT. We enforce
  656. * this by delaying both NAT and helper calls for unconfirmed
  657. * connections until the committing CT action. For later
  658. * packets NAT and Helper may be called in either order.
  659. *
  660. * NAT will be done only if the CT action has NAT, and only
  661. * once per packet (per zone), as guarded by the NAT bits in
  662. * the key->ct.state.
  663. */
  664. if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) &&
  665. (nf_ct_is_confirmed(ct) || info->commit) &&
  666. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  667. return -EINVAL;
  668. }
  669. /* Userspace may decide to perform a ct lookup without a helper
  670. * specified followed by a (recirculate and) commit with one.
  671. * Therefore, for unconfirmed connections which we will commit,
  672. * we need to attach the helper here.
  673. */
  674. if (!nf_ct_is_confirmed(ct) && info->commit &&
  675. info->helper && !nfct_help(ct)) {
  676. int err = __nf_ct_try_assign_helper(ct, info->ct,
  677. GFP_ATOMIC);
  678. if (err)
  679. return err;
  680. }
  681. /* Call the helper only if:
  682. * - nf_conntrack_in() was executed above ("!cached") for a
  683. * confirmed connection, or
  684. * - When committing an unconfirmed connection.
  685. */
  686. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  687. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  688. return -EINVAL;
  689. }
  690. }
  691. return 0;
  692. }
  693. /* Lookup connection and read fields into key. */
  694. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  695. const struct ovs_conntrack_info *info,
  696. struct sk_buff *skb)
  697. {
  698. struct nf_conntrack_expect *exp;
  699. /* If we pass an expected packet through nf_conntrack_in() the
  700. * expectation is typically removed, but the packet could still be
  701. * lost in upcall processing. To prevent this from happening we
  702. * perform an explicit expectation lookup. Expected connections are
  703. * always new, and will be passed through conntrack only when they are
  704. * committed, as it is OK to remove the expectation at that time.
  705. */
  706. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  707. if (exp) {
  708. u8 state;
  709. /* NOTE: New connections are NATted and Helped only when
  710. * committed, so we are not calling into NAT here.
  711. */
  712. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  713. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  714. } else {
  715. struct nf_conn *ct;
  716. int err;
  717. err = __ovs_ct_lookup(net, key, info, skb);
  718. if (err)
  719. return err;
  720. ct = (struct nf_conn *)skb->nfct;
  721. if (ct)
  722. nf_ct_deliver_cached_events(ct);
  723. }
  724. return 0;
  725. }
  726. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  727. {
  728. size_t i;
  729. for (i = 0; i < sizeof(*labels); i++)
  730. if (labels->ct_labels[i])
  731. return true;
  732. return false;
  733. }
  734. /* Lookup connection and confirm if unconfirmed. */
  735. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  736. const struct ovs_conntrack_info *info,
  737. struct sk_buff *skb)
  738. {
  739. int err;
  740. err = __ovs_ct_lookup(net, key, info, skb);
  741. if (err)
  742. return err;
  743. /* Apply changes before confirming the connection so that the initial
  744. * conntrack NEW netlink event carries the values given in the CT
  745. * action.
  746. */
  747. if (info->mark.mask) {
  748. err = ovs_ct_set_mark(skb, key, info->mark.value,
  749. info->mark.mask);
  750. if (err)
  751. return err;
  752. }
  753. if (labels_nonzero(&info->labels.mask)) {
  754. err = ovs_ct_set_labels(skb, key, &info->labels.value,
  755. &info->labels.mask);
  756. if (err)
  757. return err;
  758. }
  759. /* This will take care of sending queued events even if the connection
  760. * is already confirmed.
  761. */
  762. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  763. return -EINVAL;
  764. return 0;
  765. }
  766. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  767. * value if 'skb' is freed.
  768. */
  769. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  770. struct sw_flow_key *key,
  771. const struct ovs_conntrack_info *info)
  772. {
  773. int nh_ofs;
  774. int err;
  775. /* The conntrack module expects to be working at L3. */
  776. nh_ofs = skb_network_offset(skb);
  777. skb_pull(skb, nh_ofs);
  778. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  779. err = handle_fragments(net, key, info->zone.id, skb);
  780. if (err)
  781. return err;
  782. }
  783. if (info->commit)
  784. err = ovs_ct_commit(net, key, info, skb);
  785. else
  786. err = ovs_ct_lookup(net, key, info, skb);
  787. skb_push(skb, nh_ofs);
  788. if (err)
  789. kfree_skb(skb);
  790. return err;
  791. }
  792. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  793. const struct sw_flow_key *key, bool log)
  794. {
  795. struct nf_conntrack_helper *helper;
  796. struct nf_conn_help *help;
  797. helper = nf_conntrack_helper_try_module_get(name, info->family,
  798. key->ip.proto);
  799. if (!helper) {
  800. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  801. return -EINVAL;
  802. }
  803. help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL);
  804. if (!help) {
  805. module_put(helper->me);
  806. return -ENOMEM;
  807. }
  808. rcu_assign_pointer(help->helper, helper);
  809. info->helper = helper;
  810. return 0;
  811. }
  812. #ifdef CONFIG_NF_NAT_NEEDED
  813. static int parse_nat(const struct nlattr *attr,
  814. struct ovs_conntrack_info *info, bool log)
  815. {
  816. struct nlattr *a;
  817. int rem;
  818. bool have_ip_max = false;
  819. bool have_proto_max = false;
  820. bool ip_vers = (info->family == NFPROTO_IPV6);
  821. nla_for_each_nested(a, attr, rem) {
  822. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  823. [OVS_NAT_ATTR_SRC] = {0, 0},
  824. [OVS_NAT_ATTR_DST] = {0, 0},
  825. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  826. sizeof(struct in6_addr)},
  827. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  828. sizeof(struct in6_addr)},
  829. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  830. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  831. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  832. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  833. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  834. };
  835. int type = nla_type(a);
  836. if (type > OVS_NAT_ATTR_MAX) {
  837. OVS_NLERR(log,
  838. "Unknown NAT attribute (type=%d, max=%d).\n",
  839. type, OVS_NAT_ATTR_MAX);
  840. return -EINVAL;
  841. }
  842. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  843. OVS_NLERR(log,
  844. "NAT attribute type %d has unexpected length (%d != %d).\n",
  845. type, nla_len(a),
  846. ovs_nat_attr_lens[type][ip_vers]);
  847. return -EINVAL;
  848. }
  849. switch (type) {
  850. case OVS_NAT_ATTR_SRC:
  851. case OVS_NAT_ATTR_DST:
  852. if (info->nat) {
  853. OVS_NLERR(log,
  854. "Only one type of NAT may be specified.\n"
  855. );
  856. return -ERANGE;
  857. }
  858. info->nat |= OVS_CT_NAT;
  859. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  860. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  861. break;
  862. case OVS_NAT_ATTR_IP_MIN:
  863. nla_memcpy(&info->range.min_addr, a,
  864. sizeof(info->range.min_addr));
  865. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  866. break;
  867. case OVS_NAT_ATTR_IP_MAX:
  868. have_ip_max = true;
  869. nla_memcpy(&info->range.max_addr, a,
  870. sizeof(info->range.max_addr));
  871. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  872. break;
  873. case OVS_NAT_ATTR_PROTO_MIN:
  874. info->range.min_proto.all = htons(nla_get_u16(a));
  875. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  876. break;
  877. case OVS_NAT_ATTR_PROTO_MAX:
  878. have_proto_max = true;
  879. info->range.max_proto.all = htons(nla_get_u16(a));
  880. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  881. break;
  882. case OVS_NAT_ATTR_PERSISTENT:
  883. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  884. break;
  885. case OVS_NAT_ATTR_PROTO_HASH:
  886. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  887. break;
  888. case OVS_NAT_ATTR_PROTO_RANDOM:
  889. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  890. break;
  891. default:
  892. OVS_NLERR(log, "Unknown nat attribute (%d).\n", type);
  893. return -EINVAL;
  894. }
  895. }
  896. if (rem > 0) {
  897. OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem);
  898. return -EINVAL;
  899. }
  900. if (!info->nat) {
  901. /* Do not allow flags if no type is given. */
  902. if (info->range.flags) {
  903. OVS_NLERR(log,
  904. "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n"
  905. );
  906. return -EINVAL;
  907. }
  908. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  909. } else if (!info->commit) {
  910. OVS_NLERR(log,
  911. "NAT attributes may be specified only when CT COMMIT flag is also specified.\n"
  912. );
  913. return -EINVAL;
  914. }
  915. /* Allow missing IP_MAX. */
  916. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  917. memcpy(&info->range.max_addr, &info->range.min_addr,
  918. sizeof(info->range.max_addr));
  919. }
  920. /* Allow missing PROTO_MAX. */
  921. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  922. !have_proto_max) {
  923. info->range.max_proto.all = info->range.min_proto.all;
  924. }
  925. return 0;
  926. }
  927. #endif
  928. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  929. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  930. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  931. .maxlen = sizeof(u16) },
  932. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  933. .maxlen = sizeof(struct md_mark) },
  934. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  935. .maxlen = sizeof(struct md_labels) },
  936. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  937. .maxlen = NF_CT_HELPER_NAME_LEN },
  938. #ifdef CONFIG_NF_NAT_NEEDED
  939. /* NAT length is checked when parsing the nested attributes. */
  940. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  941. #endif
  942. };
  943. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  944. const char **helper, bool log)
  945. {
  946. struct nlattr *a;
  947. int rem;
  948. nla_for_each_nested(a, attr, rem) {
  949. int type = nla_type(a);
  950. int maxlen = ovs_ct_attr_lens[type].maxlen;
  951. int minlen = ovs_ct_attr_lens[type].minlen;
  952. if (type > OVS_CT_ATTR_MAX) {
  953. OVS_NLERR(log,
  954. "Unknown conntrack attr (type=%d, max=%d)",
  955. type, OVS_CT_ATTR_MAX);
  956. return -EINVAL;
  957. }
  958. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  959. OVS_NLERR(log,
  960. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  961. type, nla_len(a), maxlen);
  962. return -EINVAL;
  963. }
  964. switch (type) {
  965. case OVS_CT_ATTR_COMMIT:
  966. info->commit = true;
  967. break;
  968. #ifdef CONFIG_NF_CONNTRACK_ZONES
  969. case OVS_CT_ATTR_ZONE:
  970. info->zone.id = nla_get_u16(a);
  971. break;
  972. #endif
  973. #ifdef CONFIG_NF_CONNTRACK_MARK
  974. case OVS_CT_ATTR_MARK: {
  975. struct md_mark *mark = nla_data(a);
  976. if (!mark->mask) {
  977. OVS_NLERR(log, "ct_mark mask cannot be 0");
  978. return -EINVAL;
  979. }
  980. info->mark = *mark;
  981. break;
  982. }
  983. #endif
  984. #ifdef CONFIG_NF_CONNTRACK_LABELS
  985. case OVS_CT_ATTR_LABELS: {
  986. struct md_labels *labels = nla_data(a);
  987. if (!labels_nonzero(&labels->mask)) {
  988. OVS_NLERR(log, "ct_labels mask cannot be 0");
  989. return -EINVAL;
  990. }
  991. info->labels = *labels;
  992. break;
  993. }
  994. #endif
  995. case OVS_CT_ATTR_HELPER:
  996. *helper = nla_data(a);
  997. if (!memchr(*helper, '\0', nla_len(a))) {
  998. OVS_NLERR(log, "Invalid conntrack helper");
  999. return -EINVAL;
  1000. }
  1001. break;
  1002. #ifdef CONFIG_NF_NAT_NEEDED
  1003. case OVS_CT_ATTR_NAT: {
  1004. int err = parse_nat(a, info, log);
  1005. if (err)
  1006. return err;
  1007. break;
  1008. }
  1009. #endif
  1010. default:
  1011. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  1012. type);
  1013. return -EINVAL;
  1014. }
  1015. }
  1016. #ifdef CONFIG_NF_CONNTRACK_MARK
  1017. if (!info->commit && info->mark.mask) {
  1018. OVS_NLERR(log,
  1019. "Setting conntrack mark requires 'commit' flag.");
  1020. return -EINVAL;
  1021. }
  1022. #endif
  1023. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1024. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1025. OVS_NLERR(log,
  1026. "Setting conntrack labels requires 'commit' flag.");
  1027. return -EINVAL;
  1028. }
  1029. #endif
  1030. if (rem > 0) {
  1031. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1032. return -EINVAL;
  1033. }
  1034. return 0;
  1035. }
  1036. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1037. {
  1038. if (attr == OVS_KEY_ATTR_CT_STATE)
  1039. return true;
  1040. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1041. attr == OVS_KEY_ATTR_CT_ZONE)
  1042. return true;
  1043. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1044. attr == OVS_KEY_ATTR_CT_MARK)
  1045. return true;
  1046. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1047. attr == OVS_KEY_ATTR_CT_LABELS) {
  1048. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1049. return ovs_net->xt_label;
  1050. }
  1051. return false;
  1052. }
  1053. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1054. const struct sw_flow_key *key,
  1055. struct sw_flow_actions **sfa, bool log)
  1056. {
  1057. struct ovs_conntrack_info ct_info;
  1058. const char *helper = NULL;
  1059. u16 family;
  1060. int err;
  1061. family = key_to_nfproto(key);
  1062. if (family == NFPROTO_UNSPEC) {
  1063. OVS_NLERR(log, "ct family unspecified");
  1064. return -EINVAL;
  1065. }
  1066. memset(&ct_info, 0, sizeof(ct_info));
  1067. ct_info.family = family;
  1068. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1069. NF_CT_DEFAULT_ZONE_DIR, 0);
  1070. err = parse_ct(attr, &ct_info, &helper, log);
  1071. if (err)
  1072. return err;
  1073. /* Set up template for tracking connections in specific zones. */
  1074. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1075. if (!ct_info.ct) {
  1076. OVS_NLERR(log, "Failed to allocate conntrack template");
  1077. return -ENOMEM;
  1078. }
  1079. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1080. nf_conntrack_get(&ct_info.ct->ct_general);
  1081. if (helper) {
  1082. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1083. if (err)
  1084. goto err_free_ct;
  1085. }
  1086. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1087. sizeof(ct_info), log);
  1088. if (err)
  1089. goto err_free_ct;
  1090. return 0;
  1091. err_free_ct:
  1092. __ovs_ct_free_action(&ct_info);
  1093. return err;
  1094. }
  1095. #ifdef CONFIG_NF_NAT_NEEDED
  1096. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1097. struct sk_buff *skb)
  1098. {
  1099. struct nlattr *start;
  1100. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1101. if (!start)
  1102. return false;
  1103. if (info->nat & OVS_CT_SRC_NAT) {
  1104. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1105. return false;
  1106. } else if (info->nat & OVS_CT_DST_NAT) {
  1107. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1108. return false;
  1109. } else {
  1110. goto out;
  1111. }
  1112. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1113. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1114. info->family == NFPROTO_IPV4) {
  1115. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1116. info->range.min_addr.ip) ||
  1117. (info->range.max_addr.ip
  1118. != info->range.min_addr.ip &&
  1119. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1120. info->range.max_addr.ip))))
  1121. return false;
  1122. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1123. info->family == NFPROTO_IPV6) {
  1124. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1125. &info->range.min_addr.in6) ||
  1126. (memcmp(&info->range.max_addr.in6,
  1127. &info->range.min_addr.in6,
  1128. sizeof(info->range.max_addr.in6)) &&
  1129. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1130. &info->range.max_addr.in6))))
  1131. return false;
  1132. } else {
  1133. return false;
  1134. }
  1135. }
  1136. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1137. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1138. ntohs(info->range.min_proto.all)) ||
  1139. (info->range.max_proto.all != info->range.min_proto.all &&
  1140. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1141. ntohs(info->range.max_proto.all)))))
  1142. return false;
  1143. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1144. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1145. return false;
  1146. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1147. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1148. return false;
  1149. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1150. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1151. return false;
  1152. out:
  1153. nla_nest_end(skb, start);
  1154. return true;
  1155. }
  1156. #endif
  1157. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1158. struct sk_buff *skb)
  1159. {
  1160. struct nlattr *start;
  1161. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1162. if (!start)
  1163. return -EMSGSIZE;
  1164. if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT))
  1165. return -EMSGSIZE;
  1166. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1167. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1168. return -EMSGSIZE;
  1169. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1170. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1171. &ct_info->mark))
  1172. return -EMSGSIZE;
  1173. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1174. labels_nonzero(&ct_info->labels.mask) &&
  1175. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1176. &ct_info->labels))
  1177. return -EMSGSIZE;
  1178. if (ct_info->helper) {
  1179. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1180. ct_info->helper->name))
  1181. return -EMSGSIZE;
  1182. }
  1183. #ifdef CONFIG_NF_NAT_NEEDED
  1184. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1185. return -EMSGSIZE;
  1186. #endif
  1187. nla_nest_end(skb, start);
  1188. return 0;
  1189. }
  1190. void ovs_ct_free_action(const struct nlattr *a)
  1191. {
  1192. struct ovs_conntrack_info *ct_info = nla_data(a);
  1193. __ovs_ct_free_action(ct_info);
  1194. }
  1195. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1196. {
  1197. if (ct_info->helper)
  1198. module_put(ct_info->helper->me);
  1199. if (ct_info->ct)
  1200. nf_ct_tmpl_free(ct_info->ct);
  1201. }
  1202. void ovs_ct_init(struct net *net)
  1203. {
  1204. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1205. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1206. if (nf_connlabels_get(net, n_bits - 1)) {
  1207. ovs_net->xt_label = false;
  1208. OVS_NLERR(true, "Failed to set connlabel length");
  1209. } else {
  1210. ovs_net->xt_label = true;
  1211. }
  1212. }
  1213. void ovs_ct_exit(struct net *net)
  1214. {
  1215. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1216. if (ovs_net->xt_label)
  1217. nf_connlabels_put(net);
  1218. }