conntrack.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  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 <linux/static_key.h>
  19. #include <net/ip.h>
  20. #include <net/genetlink.h>
  21. #include <net/netfilter/nf_conntrack_core.h>
  22. #include <net/netfilter/nf_conntrack_count.h>
  23. #include <net/netfilter/nf_conntrack_helper.h>
  24. #include <net/netfilter/nf_conntrack_labels.h>
  25. #include <net/netfilter/nf_conntrack_seqadj.h>
  26. #include <net/netfilter/nf_conntrack_zones.h>
  27. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  28. #include <net/ipv6_frag.h>
  29. #ifdef CONFIG_NF_NAT_NEEDED
  30. #include <linux/netfilter/nf_nat.h>
  31. #include <net/netfilter/nf_nat_core.h>
  32. #include <net/netfilter/nf_nat_l3proto.h>
  33. #endif
  34. #include "datapath.h"
  35. #include "conntrack.h"
  36. #include "flow.h"
  37. #include "flow_netlink.h"
  38. struct ovs_ct_len_tbl {
  39. int maxlen;
  40. int minlen;
  41. };
  42. /* Metadata mark for masked write to conntrack mark */
  43. struct md_mark {
  44. u32 value;
  45. u32 mask;
  46. };
  47. /* Metadata label for masked write to conntrack label. */
  48. struct md_labels {
  49. struct ovs_key_ct_labels value;
  50. struct ovs_key_ct_labels mask;
  51. };
  52. enum ovs_ct_nat {
  53. OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
  54. OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
  55. OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
  56. };
  57. /* Conntrack action context for execution. */
  58. struct ovs_conntrack_info {
  59. struct nf_conntrack_helper *helper;
  60. struct nf_conntrack_zone zone;
  61. struct nf_conn *ct;
  62. u8 commit : 1;
  63. u8 nat : 3; /* enum ovs_ct_nat */
  64. u8 force : 1;
  65. u8 have_eventmask : 1;
  66. u16 family;
  67. u32 eventmask; /* Mask of 1 << IPCT_*. */
  68. struct md_mark mark;
  69. struct md_labels labels;
  70. #ifdef CONFIG_NF_NAT_NEEDED
  71. struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
  72. #endif
  73. };
  74. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  75. #define OVS_CT_LIMIT_UNLIMITED 0
  76. #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
  77. #define CT_LIMIT_HASH_BUCKETS 512
  78. static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
  79. struct ovs_ct_limit {
  80. /* Elements in ovs_ct_limit_info->limits hash table */
  81. struct hlist_node hlist_node;
  82. struct rcu_head rcu;
  83. u16 zone;
  84. u32 limit;
  85. };
  86. struct ovs_ct_limit_info {
  87. u32 default_limit;
  88. struct hlist_head *limits;
  89. struct nf_conncount_data *data;
  90. };
  91. static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
  92. [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
  93. };
  94. #endif
  95. static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
  96. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
  97. static u16 key_to_nfproto(const struct sw_flow_key *key)
  98. {
  99. switch (ntohs(key->eth.type)) {
  100. case ETH_P_IP:
  101. return NFPROTO_IPV4;
  102. case ETH_P_IPV6:
  103. return NFPROTO_IPV6;
  104. default:
  105. return NFPROTO_UNSPEC;
  106. }
  107. }
  108. /* Map SKB connection state into the values used by flow definition. */
  109. static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
  110. {
  111. u8 ct_state = OVS_CS_F_TRACKED;
  112. switch (ctinfo) {
  113. case IP_CT_ESTABLISHED_REPLY:
  114. case IP_CT_RELATED_REPLY:
  115. ct_state |= OVS_CS_F_REPLY_DIR;
  116. break;
  117. default:
  118. break;
  119. }
  120. switch (ctinfo) {
  121. case IP_CT_ESTABLISHED:
  122. case IP_CT_ESTABLISHED_REPLY:
  123. ct_state |= OVS_CS_F_ESTABLISHED;
  124. break;
  125. case IP_CT_RELATED:
  126. case IP_CT_RELATED_REPLY:
  127. ct_state |= OVS_CS_F_RELATED;
  128. break;
  129. case IP_CT_NEW:
  130. ct_state |= OVS_CS_F_NEW;
  131. break;
  132. default:
  133. break;
  134. }
  135. return ct_state;
  136. }
  137. static u32 ovs_ct_get_mark(const struct nf_conn *ct)
  138. {
  139. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  140. return ct ? ct->mark : 0;
  141. #else
  142. return 0;
  143. #endif
  144. }
  145. /* Guard against conntrack labels max size shrinking below 128 bits. */
  146. #if NF_CT_LABELS_MAX_SIZE < 16
  147. #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
  148. #endif
  149. static void ovs_ct_get_labels(const struct nf_conn *ct,
  150. struct ovs_key_ct_labels *labels)
  151. {
  152. struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
  153. if (cl)
  154. memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
  155. else
  156. memset(labels, 0, OVS_CT_LABELS_LEN);
  157. }
  158. static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
  159. const struct nf_conntrack_tuple *orig,
  160. u8 icmp_proto)
  161. {
  162. key->ct_orig_proto = orig->dst.protonum;
  163. if (orig->dst.protonum == icmp_proto) {
  164. key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
  165. key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
  166. } else {
  167. key->ct.orig_tp.src = orig->src.u.all;
  168. key->ct.orig_tp.dst = orig->dst.u.all;
  169. }
  170. }
  171. static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
  172. const struct nf_conntrack_zone *zone,
  173. const struct nf_conn *ct)
  174. {
  175. key->ct_state = state;
  176. key->ct_zone = zone->id;
  177. key->ct.mark = ovs_ct_get_mark(ct);
  178. ovs_ct_get_labels(ct, &key->ct.labels);
  179. if (ct) {
  180. const struct nf_conntrack_tuple *orig;
  181. /* Use the master if we have one. */
  182. if (ct->master)
  183. ct = ct->master;
  184. orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
  185. /* IP version must match with the master connection. */
  186. if (key->eth.type == htons(ETH_P_IP) &&
  187. nf_ct_l3num(ct) == NFPROTO_IPV4) {
  188. key->ipv4.ct_orig.src = orig->src.u3.ip;
  189. key->ipv4.ct_orig.dst = orig->dst.u3.ip;
  190. __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
  191. return;
  192. } else if (key->eth.type == htons(ETH_P_IPV6) &&
  193. !sw_flow_key_is_nd(key) &&
  194. nf_ct_l3num(ct) == NFPROTO_IPV6) {
  195. key->ipv6.ct_orig.src = orig->src.u3.in6;
  196. key->ipv6.ct_orig.dst = orig->dst.u3.in6;
  197. __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
  198. return;
  199. }
  200. }
  201. /* Clear 'ct_orig_proto' to mark the non-existence of conntrack
  202. * original direction key fields.
  203. */
  204. key->ct_orig_proto = 0;
  205. }
  206. /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
  207. * previously sent the packet to conntrack via the ct action. If
  208. * 'keep_nat_flags' is true, the existing NAT flags retained, else they are
  209. * initialized from the connection status.
  210. */
  211. static void ovs_ct_update_key(const struct sk_buff *skb,
  212. const struct ovs_conntrack_info *info,
  213. struct sw_flow_key *key, bool post_ct,
  214. bool keep_nat_flags)
  215. {
  216. const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
  217. enum ip_conntrack_info ctinfo;
  218. struct nf_conn *ct;
  219. u8 state = 0;
  220. ct = nf_ct_get(skb, &ctinfo);
  221. if (ct) {
  222. state = ovs_ct_get_state(ctinfo);
  223. /* All unconfirmed entries are NEW connections. */
  224. if (!nf_ct_is_confirmed(ct))
  225. state |= OVS_CS_F_NEW;
  226. /* OVS persists the related flag for the duration of the
  227. * connection.
  228. */
  229. if (ct->master)
  230. state |= OVS_CS_F_RELATED;
  231. if (keep_nat_flags) {
  232. state |= key->ct_state & OVS_CS_F_NAT_MASK;
  233. } else {
  234. if (ct->status & IPS_SRC_NAT)
  235. state |= OVS_CS_F_SRC_NAT;
  236. if (ct->status & IPS_DST_NAT)
  237. state |= OVS_CS_F_DST_NAT;
  238. }
  239. zone = nf_ct_zone(ct);
  240. } else if (post_ct) {
  241. state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
  242. if (info)
  243. zone = &info->zone;
  244. }
  245. __ovs_ct_update_key(key, state, zone, ct);
  246. }
  247. /* This is called to initialize CT key fields possibly coming in from the local
  248. * stack.
  249. */
  250. void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
  251. {
  252. ovs_ct_update_key(skb, NULL, key, false, false);
  253. }
  254. #define IN6_ADDR_INITIALIZER(ADDR) \
  255. { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \
  256. (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] }
  257. int ovs_ct_put_key(const struct sw_flow_key *swkey,
  258. const struct sw_flow_key *output, struct sk_buff *skb)
  259. {
  260. if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
  261. return -EMSGSIZE;
  262. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  263. nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
  264. return -EMSGSIZE;
  265. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  266. nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
  267. return -EMSGSIZE;
  268. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  269. nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
  270. &output->ct.labels))
  271. return -EMSGSIZE;
  272. if (swkey->ct_orig_proto) {
  273. if (swkey->eth.type == htons(ETH_P_IP)) {
  274. struct ovs_key_ct_tuple_ipv4 orig = {
  275. output->ipv4.ct_orig.src,
  276. output->ipv4.ct_orig.dst,
  277. output->ct.orig_tp.src,
  278. output->ct.orig_tp.dst,
  279. output->ct_orig_proto,
  280. };
  281. if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
  282. sizeof(orig), &orig))
  283. return -EMSGSIZE;
  284. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  285. struct ovs_key_ct_tuple_ipv6 orig = {
  286. IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.src),
  287. IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.dst),
  288. output->ct.orig_tp.src,
  289. output->ct.orig_tp.dst,
  290. output->ct_orig_proto,
  291. };
  292. if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
  293. sizeof(orig), &orig))
  294. return -EMSGSIZE;
  295. }
  296. }
  297. return 0;
  298. }
  299. static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
  300. u32 ct_mark, u32 mask)
  301. {
  302. #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
  303. u32 new_mark;
  304. new_mark = ct_mark | (ct->mark & ~(mask));
  305. if (ct->mark != new_mark) {
  306. ct->mark = new_mark;
  307. if (nf_ct_is_confirmed(ct))
  308. nf_conntrack_event_cache(IPCT_MARK, ct);
  309. key->ct.mark = new_mark;
  310. }
  311. return 0;
  312. #else
  313. return -ENOTSUPP;
  314. #endif
  315. }
  316. static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
  317. {
  318. struct nf_conn_labels *cl;
  319. cl = nf_ct_labels_find(ct);
  320. if (!cl) {
  321. nf_ct_labels_ext_add(ct);
  322. cl = nf_ct_labels_find(ct);
  323. }
  324. return cl;
  325. }
  326. /* Initialize labels for a new, yet to be committed conntrack entry. Note that
  327. * since the new connection is not yet confirmed, and thus no-one else has
  328. * access to it's labels, we simply write them over.
  329. */
  330. static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
  331. const struct ovs_key_ct_labels *labels,
  332. const struct ovs_key_ct_labels *mask)
  333. {
  334. struct nf_conn_labels *cl, *master_cl;
  335. bool have_mask = labels_nonzero(mask);
  336. /* Inherit master's labels to the related connection? */
  337. master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
  338. if (!master_cl && !have_mask)
  339. return 0; /* Nothing to do. */
  340. cl = ovs_ct_get_conn_labels(ct);
  341. if (!cl)
  342. return -ENOSPC;
  343. /* Inherit the master's labels, if any. */
  344. if (master_cl)
  345. *cl = *master_cl;
  346. if (have_mask) {
  347. u32 *dst = (u32 *)cl->bits;
  348. int i;
  349. for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
  350. dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
  351. (labels->ct_labels_32[i]
  352. & mask->ct_labels_32[i]);
  353. }
  354. /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
  355. * IPCT_LABEL bit is set in the event cache.
  356. */
  357. nf_conntrack_event_cache(IPCT_LABEL, ct);
  358. memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
  359. return 0;
  360. }
  361. static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
  362. const struct ovs_key_ct_labels *labels,
  363. const struct ovs_key_ct_labels *mask)
  364. {
  365. struct nf_conn_labels *cl;
  366. int err;
  367. cl = ovs_ct_get_conn_labels(ct);
  368. if (!cl)
  369. return -ENOSPC;
  370. err = nf_connlabels_replace(ct, labels->ct_labels_32,
  371. mask->ct_labels_32,
  372. OVS_CT_LABELS_LEN_32);
  373. if (err)
  374. return err;
  375. memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
  376. return 0;
  377. }
  378. /* 'skb' should already be pulled to nh_ofs. */
  379. static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
  380. {
  381. const struct nf_conntrack_helper *helper;
  382. const struct nf_conn_help *help;
  383. enum ip_conntrack_info ctinfo;
  384. unsigned int protoff;
  385. struct nf_conn *ct;
  386. int err;
  387. ct = nf_ct_get(skb, &ctinfo);
  388. if (!ct || ctinfo == IP_CT_RELATED_REPLY)
  389. return NF_ACCEPT;
  390. help = nfct_help(ct);
  391. if (!help)
  392. return NF_ACCEPT;
  393. helper = rcu_dereference(help->helper);
  394. if (!helper)
  395. return NF_ACCEPT;
  396. switch (proto) {
  397. case NFPROTO_IPV4:
  398. protoff = ip_hdrlen(skb);
  399. break;
  400. case NFPROTO_IPV6: {
  401. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  402. __be16 frag_off;
  403. int ofs;
  404. ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
  405. &frag_off);
  406. if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
  407. pr_debug("proto header not found\n");
  408. return NF_ACCEPT;
  409. }
  410. protoff = ofs;
  411. break;
  412. }
  413. default:
  414. WARN_ONCE(1, "helper invoked on non-IP family!");
  415. return NF_DROP;
  416. }
  417. err = helper->help(skb, protoff, ct, ctinfo);
  418. if (err != NF_ACCEPT)
  419. return err;
  420. /* Adjust seqs after helper. This is needed due to some helpers (e.g.,
  421. * FTP with NAT) adusting the TCP payload size when mangling IP
  422. * addresses and/or port numbers in the text-based control connection.
  423. */
  424. if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
  425. !nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
  426. return NF_DROP;
  427. return NF_ACCEPT;
  428. }
  429. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  430. * value if 'skb' is freed.
  431. */
  432. static int handle_fragments(struct net *net, struct sw_flow_key *key,
  433. u16 zone, struct sk_buff *skb)
  434. {
  435. struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
  436. int err;
  437. if (key->eth.type == htons(ETH_P_IP)) {
  438. enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
  439. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  440. err = ip_defrag(net, skb, user);
  441. if (err)
  442. return err;
  443. ovs_cb.mru = IPCB(skb)->frag_max_size;
  444. #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
  445. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  446. enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
  447. memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
  448. err = nf_ct_frag6_gather(net, skb, user);
  449. if (err) {
  450. if (err != -EINPROGRESS)
  451. kfree_skb(skb);
  452. return err;
  453. }
  454. key->ip.proto = ipv6_hdr(skb)->nexthdr;
  455. ovs_cb.mru = IP6CB(skb)->frag_max_size;
  456. #endif
  457. } else {
  458. kfree_skb(skb);
  459. return -EPFNOSUPPORT;
  460. }
  461. key->ip.frag = OVS_FRAG_TYPE_NONE;
  462. skb_clear_hash(skb);
  463. skb->ignore_df = 1;
  464. *OVS_CB(skb) = ovs_cb;
  465. return 0;
  466. }
  467. static struct nf_conntrack_expect *
  468. ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
  469. u16 proto, const struct sk_buff *skb)
  470. {
  471. struct nf_conntrack_tuple tuple;
  472. struct nf_conntrack_expect *exp;
  473. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
  474. return NULL;
  475. exp = __nf_ct_expect_find(net, zone, &tuple);
  476. if (exp) {
  477. struct nf_conntrack_tuple_hash *h;
  478. /* Delete existing conntrack entry, if it clashes with the
  479. * expectation. This can happen since conntrack ALGs do not
  480. * check for clashes between (new) expectations and existing
  481. * conntrack entries. nf_conntrack_in() will check the
  482. * expectations only if a conntrack entry can not be found,
  483. * which can lead to OVS finding the expectation (here) in the
  484. * init direction, but which will not be removed by the
  485. * nf_conntrack_in() call, if a matching conntrack entry is
  486. * found instead. In this case all init direction packets
  487. * would be reported as new related packets, while reply
  488. * direction packets would be reported as un-related
  489. * established packets.
  490. */
  491. h = nf_conntrack_find_get(net, zone, &tuple);
  492. if (h) {
  493. struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
  494. nf_ct_delete(ct, 0, 0);
  495. nf_conntrack_put(&ct->ct_general);
  496. }
  497. }
  498. return exp;
  499. }
  500. /* This replicates logic from nf_conntrack_core.c that is not exported. */
  501. static enum ip_conntrack_info
  502. ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
  503. {
  504. const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
  505. if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
  506. return IP_CT_ESTABLISHED_REPLY;
  507. /* Once we've had two way comms, always ESTABLISHED. */
  508. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
  509. return IP_CT_ESTABLISHED;
  510. if (test_bit(IPS_EXPECTED_BIT, &ct->status))
  511. return IP_CT_RELATED;
  512. return IP_CT_NEW;
  513. }
  514. /* Find an existing connection which this packet belongs to without
  515. * re-attributing statistics or modifying the connection state. This allows an
  516. * skb->_nfct lost due to an upcall to be recovered during actions execution.
  517. *
  518. * Must be called with rcu_read_lock.
  519. *
  520. * On success, populates skb->_nfct and returns the connection. Returns NULL
  521. * if there is no existing entry.
  522. */
  523. static struct nf_conn *
  524. ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
  525. u8 l3num, struct sk_buff *skb, bool natted)
  526. {
  527. struct nf_conntrack_tuple tuple;
  528. struct nf_conntrack_tuple_hash *h;
  529. struct nf_conn *ct;
  530. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
  531. net, &tuple)) {
  532. pr_debug("ovs_ct_find_existing: Can't get tuple\n");
  533. return NULL;
  534. }
  535. /* Must invert the tuple if skb has been transformed by NAT. */
  536. if (natted) {
  537. struct nf_conntrack_tuple inverse;
  538. if (!nf_ct_invert_tuplepr(&inverse, &tuple)) {
  539. pr_debug("ovs_ct_find_existing: Inversion failed!\n");
  540. return NULL;
  541. }
  542. tuple = inverse;
  543. }
  544. /* look for tuple match */
  545. h = nf_conntrack_find_get(net, zone, &tuple);
  546. if (!h)
  547. return NULL; /* Not found. */
  548. ct = nf_ct_tuplehash_to_ctrack(h);
  549. /* Inverted packet tuple matches the reverse direction conntrack tuple,
  550. * select the other tuplehash to get the right 'ctinfo' bits for this
  551. * packet.
  552. */
  553. if (natted)
  554. h = &ct->tuplehash[!h->tuple.dst.dir];
  555. nf_ct_set(skb, ct, ovs_ct_get_info(h));
  556. return ct;
  557. }
  558. static
  559. struct nf_conn *ovs_ct_executed(struct net *net,
  560. const struct sw_flow_key *key,
  561. const struct ovs_conntrack_info *info,
  562. struct sk_buff *skb,
  563. bool *ct_executed)
  564. {
  565. struct nf_conn *ct = NULL;
  566. /* If no ct, check if we have evidence that an existing conntrack entry
  567. * might be found for this skb. This happens when we lose a skb->_nfct
  568. * due to an upcall, or if the direction is being forced. If the
  569. * connection was not confirmed, it is not cached and needs to be run
  570. * through conntrack again.
  571. */
  572. *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
  573. !(key->ct_state & OVS_CS_F_INVALID) &&
  574. (key->ct_zone == info->zone.id);
  575. if (*ct_executed || (!key->ct_state && info->force)) {
  576. ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
  577. !!(key->ct_state &
  578. OVS_CS_F_NAT_MASK));
  579. }
  580. return ct;
  581. }
  582. /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
  583. static bool skb_nfct_cached(struct net *net,
  584. const struct sw_flow_key *key,
  585. const struct ovs_conntrack_info *info,
  586. struct sk_buff *skb)
  587. {
  588. enum ip_conntrack_info ctinfo;
  589. struct nf_conn *ct;
  590. bool ct_executed = true;
  591. ct = nf_ct_get(skb, &ctinfo);
  592. if (!ct)
  593. ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
  594. if (ct)
  595. nf_ct_get(skb, &ctinfo);
  596. else
  597. return false;
  598. if (!net_eq(net, read_pnet(&ct->ct_net)))
  599. return false;
  600. if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
  601. return false;
  602. if (info->helper) {
  603. struct nf_conn_help *help;
  604. help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
  605. if (help && rcu_access_pointer(help->helper) != info->helper)
  606. return false;
  607. }
  608. /* Force conntrack entry direction to the current packet? */
  609. if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
  610. /* Delete the conntrack entry if confirmed, else just release
  611. * the reference.
  612. */
  613. if (nf_ct_is_confirmed(ct))
  614. nf_ct_delete(ct, 0, 0);
  615. nf_conntrack_put(&ct->ct_general);
  616. nf_ct_set(skb, NULL, 0);
  617. return false;
  618. }
  619. return ct_executed;
  620. }
  621. #ifdef CONFIG_NF_NAT_NEEDED
  622. /* Modelled after nf_nat_ipv[46]_fn().
  623. * range is only used for new, uninitialized NAT state.
  624. * Returns either NF_ACCEPT or NF_DROP.
  625. */
  626. static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
  627. enum ip_conntrack_info ctinfo,
  628. const struct nf_nat_range2 *range,
  629. enum nf_nat_manip_type maniptype)
  630. {
  631. int hooknum, nh_off, err = NF_ACCEPT;
  632. nh_off = skb_network_offset(skb);
  633. skb_pull_rcsum(skb, nh_off);
  634. /* See HOOK2MANIP(). */
  635. if (maniptype == NF_NAT_MANIP_SRC)
  636. hooknum = NF_INET_LOCAL_IN; /* Source NAT */
  637. else
  638. hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
  639. switch (ctinfo) {
  640. case IP_CT_RELATED:
  641. case IP_CT_RELATED_REPLY:
  642. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  643. skb->protocol == htons(ETH_P_IP) &&
  644. ip_hdr(skb)->protocol == IPPROTO_ICMP) {
  645. if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
  646. hooknum))
  647. err = NF_DROP;
  648. goto push;
  649. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  650. skb->protocol == htons(ETH_P_IPV6)) {
  651. __be16 frag_off;
  652. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  653. int hdrlen = ipv6_skip_exthdr(skb,
  654. sizeof(struct ipv6hdr),
  655. &nexthdr, &frag_off);
  656. if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
  657. if (!nf_nat_icmpv6_reply_translation(skb, ct,
  658. ctinfo,
  659. hooknum,
  660. hdrlen))
  661. err = NF_DROP;
  662. goto push;
  663. }
  664. }
  665. /* Non-ICMP, fall thru to initialize if needed. */
  666. /* fall through */
  667. case IP_CT_NEW:
  668. /* Seen it before? This can happen for loopback, retrans,
  669. * or local packets.
  670. */
  671. if (!nf_nat_initialized(ct, maniptype)) {
  672. /* Initialize according to the NAT action. */
  673. err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
  674. /* Action is set up to establish a new
  675. * mapping.
  676. */
  677. ? nf_nat_setup_info(ct, range, maniptype)
  678. : nf_nat_alloc_null_binding(ct, hooknum);
  679. if (err != NF_ACCEPT)
  680. goto push;
  681. }
  682. break;
  683. case IP_CT_ESTABLISHED:
  684. case IP_CT_ESTABLISHED_REPLY:
  685. break;
  686. default:
  687. err = NF_DROP;
  688. goto push;
  689. }
  690. err = nf_nat_packet(ct, ctinfo, hooknum, skb);
  691. push:
  692. skb_push(skb, nh_off);
  693. skb_postpush_rcsum(skb, skb->data, nh_off);
  694. return err;
  695. }
  696. static void ovs_nat_update_key(struct sw_flow_key *key,
  697. const struct sk_buff *skb,
  698. enum nf_nat_manip_type maniptype)
  699. {
  700. if (maniptype == NF_NAT_MANIP_SRC) {
  701. __be16 src;
  702. key->ct_state |= OVS_CS_F_SRC_NAT;
  703. if (key->eth.type == htons(ETH_P_IP))
  704. key->ipv4.addr.src = ip_hdr(skb)->saddr;
  705. else if (key->eth.type == htons(ETH_P_IPV6))
  706. memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
  707. sizeof(key->ipv6.addr.src));
  708. else
  709. return;
  710. if (key->ip.proto == IPPROTO_UDP)
  711. src = udp_hdr(skb)->source;
  712. else if (key->ip.proto == IPPROTO_TCP)
  713. src = tcp_hdr(skb)->source;
  714. else if (key->ip.proto == IPPROTO_SCTP)
  715. src = sctp_hdr(skb)->source;
  716. else
  717. return;
  718. key->tp.src = src;
  719. } else {
  720. __be16 dst;
  721. key->ct_state |= OVS_CS_F_DST_NAT;
  722. if (key->eth.type == htons(ETH_P_IP))
  723. key->ipv4.addr.dst = ip_hdr(skb)->daddr;
  724. else if (key->eth.type == htons(ETH_P_IPV6))
  725. memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
  726. sizeof(key->ipv6.addr.dst));
  727. else
  728. return;
  729. if (key->ip.proto == IPPROTO_UDP)
  730. dst = udp_hdr(skb)->dest;
  731. else if (key->ip.proto == IPPROTO_TCP)
  732. dst = tcp_hdr(skb)->dest;
  733. else if (key->ip.proto == IPPROTO_SCTP)
  734. dst = sctp_hdr(skb)->dest;
  735. else
  736. return;
  737. key->tp.dst = dst;
  738. }
  739. }
  740. /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
  741. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  742. const struct ovs_conntrack_info *info,
  743. struct sk_buff *skb, struct nf_conn *ct,
  744. enum ip_conntrack_info ctinfo)
  745. {
  746. enum nf_nat_manip_type maniptype;
  747. int err;
  748. /* Add NAT extension if not confirmed yet. */
  749. if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
  750. return NF_ACCEPT; /* Can't NAT. */
  751. /* Determine NAT type.
  752. * Check if the NAT type can be deduced from the tracked connection.
  753. * Make sure new expected connections (IP_CT_RELATED) are NATted only
  754. * when committing.
  755. */
  756. if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
  757. ct->status & IPS_NAT_MASK &&
  758. (ctinfo != IP_CT_RELATED || info->commit)) {
  759. /* NAT an established or related connection like before. */
  760. if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
  761. /* This is the REPLY direction for a connection
  762. * for which NAT was applied in the forward
  763. * direction. Do the reverse NAT.
  764. */
  765. maniptype = ct->status & IPS_SRC_NAT
  766. ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
  767. else
  768. maniptype = ct->status & IPS_SRC_NAT
  769. ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
  770. } else if (info->nat & OVS_CT_SRC_NAT) {
  771. maniptype = NF_NAT_MANIP_SRC;
  772. } else if (info->nat & OVS_CT_DST_NAT) {
  773. maniptype = NF_NAT_MANIP_DST;
  774. } else {
  775. return NF_ACCEPT; /* Connection is not NATed. */
  776. }
  777. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
  778. if (err == NF_ACCEPT &&
  779. ct->status & IPS_SRC_NAT && ct->status & IPS_DST_NAT) {
  780. if (maniptype == NF_NAT_MANIP_SRC)
  781. maniptype = NF_NAT_MANIP_DST;
  782. else
  783. maniptype = NF_NAT_MANIP_SRC;
  784. err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
  785. maniptype);
  786. }
  787. /* Mark NAT done if successful and update the flow key. */
  788. if (err == NF_ACCEPT)
  789. ovs_nat_update_key(key, skb, maniptype);
  790. return err;
  791. }
  792. #else /* !CONFIG_NF_NAT_NEEDED */
  793. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  794. const struct ovs_conntrack_info *info,
  795. struct sk_buff *skb, struct nf_conn *ct,
  796. enum ip_conntrack_info ctinfo)
  797. {
  798. return NF_ACCEPT;
  799. }
  800. #endif
  801. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  802. * not done already. Update key with new CT state after passing the packet
  803. * through conntrack.
  804. * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
  805. * set to NULL and 0 will be returned.
  806. */
  807. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  808. const struct ovs_conntrack_info *info,
  809. struct sk_buff *skb)
  810. {
  811. /* If we are recirculating packets to match on conntrack fields and
  812. * committing with a separate conntrack action, then we don't need to
  813. * actually run the packet through conntrack twice unless it's for a
  814. * different zone.
  815. */
  816. bool cached = skb_nfct_cached(net, key, info, skb);
  817. enum ip_conntrack_info ctinfo;
  818. struct nf_conn *ct;
  819. if (!cached) {
  820. struct nf_conn *tmpl = info->ct;
  821. int err;
  822. /* Associate skb with specified zone. */
  823. if (tmpl) {
  824. if (skb_nfct(skb))
  825. nf_conntrack_put(skb_nfct(skb));
  826. nf_conntrack_get(&tmpl->ct_general);
  827. nf_ct_set(skb, tmpl, IP_CT_NEW);
  828. }
  829. err = nf_conntrack_in(net, info->family,
  830. NF_INET_PRE_ROUTING, skb);
  831. if (err != NF_ACCEPT)
  832. return -ENOENT;
  833. /* Clear CT state NAT flags to mark that we have not yet done
  834. * NAT after the nf_conntrack_in() call. We can actually clear
  835. * the whole state, as it will be re-initialized below.
  836. */
  837. key->ct_state = 0;
  838. /* Update the key, but keep the NAT flags. */
  839. ovs_ct_update_key(skb, info, key, true, true);
  840. }
  841. ct = nf_ct_get(skb, &ctinfo);
  842. if (ct) {
  843. /* Packets starting a new connection must be NATted before the
  844. * helper, so that the helper knows about the NAT. We enforce
  845. * this by delaying both NAT and helper calls for unconfirmed
  846. * connections until the committing CT action. For later
  847. * packets NAT and Helper may be called in either order.
  848. *
  849. * NAT will be done only if the CT action has NAT, and only
  850. * once per packet (per zone), as guarded by the NAT bits in
  851. * the key->ct_state.
  852. */
  853. if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
  854. (nf_ct_is_confirmed(ct) || info->commit) &&
  855. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  856. return -EINVAL;
  857. }
  858. /* Userspace may decide to perform a ct lookup without a helper
  859. * specified followed by a (recirculate and) commit with one.
  860. * Therefore, for unconfirmed connections which we will commit,
  861. * we need to attach the helper here.
  862. */
  863. if (!nf_ct_is_confirmed(ct) && info->commit &&
  864. info->helper && !nfct_help(ct)) {
  865. int err = __nf_ct_try_assign_helper(ct, info->ct,
  866. GFP_ATOMIC);
  867. if (err)
  868. return err;
  869. }
  870. /* Call the helper only if:
  871. * - nf_conntrack_in() was executed above ("!cached") for a
  872. * confirmed connection, or
  873. * - When committing an unconfirmed connection.
  874. */
  875. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  876. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  877. return -EINVAL;
  878. }
  879. }
  880. return 0;
  881. }
  882. /* Lookup connection and read fields into key. */
  883. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  884. const struct ovs_conntrack_info *info,
  885. struct sk_buff *skb)
  886. {
  887. struct nf_conntrack_expect *exp;
  888. /* If we pass an expected packet through nf_conntrack_in() the
  889. * expectation is typically removed, but the packet could still be
  890. * lost in upcall processing. To prevent this from happening we
  891. * perform an explicit expectation lookup. Expected connections are
  892. * always new, and will be passed through conntrack only when they are
  893. * committed, as it is OK to remove the expectation at that time.
  894. */
  895. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  896. if (exp) {
  897. u8 state;
  898. /* NOTE: New connections are NATted and Helped only when
  899. * committed, so we are not calling into NAT here.
  900. */
  901. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  902. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  903. } else {
  904. struct nf_conn *ct;
  905. int err;
  906. err = __ovs_ct_lookup(net, key, info, skb);
  907. if (err)
  908. return err;
  909. ct = (struct nf_conn *)skb_nfct(skb);
  910. if (ct)
  911. nf_ct_deliver_cached_events(ct);
  912. }
  913. return 0;
  914. }
  915. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  916. {
  917. size_t i;
  918. for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
  919. if (labels->ct_labels_32[i])
  920. return true;
  921. return false;
  922. }
  923. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  924. static struct hlist_head *ct_limit_hash_bucket(
  925. const struct ovs_ct_limit_info *info, u16 zone)
  926. {
  927. return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
  928. }
  929. /* Call with ovs_mutex */
  930. static void ct_limit_set(const struct ovs_ct_limit_info *info,
  931. struct ovs_ct_limit *new_ct_limit)
  932. {
  933. struct ovs_ct_limit *ct_limit;
  934. struct hlist_head *head;
  935. head = ct_limit_hash_bucket(info, new_ct_limit->zone);
  936. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  937. if (ct_limit->zone == new_ct_limit->zone) {
  938. hlist_replace_rcu(&ct_limit->hlist_node,
  939. &new_ct_limit->hlist_node);
  940. kfree_rcu(ct_limit, rcu);
  941. return;
  942. }
  943. }
  944. hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
  945. }
  946. /* Call with ovs_mutex */
  947. static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
  948. {
  949. struct ovs_ct_limit *ct_limit;
  950. struct hlist_head *head;
  951. struct hlist_node *n;
  952. head = ct_limit_hash_bucket(info, zone);
  953. hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
  954. if (ct_limit->zone == zone) {
  955. hlist_del_rcu(&ct_limit->hlist_node);
  956. kfree_rcu(ct_limit, rcu);
  957. return;
  958. }
  959. }
  960. }
  961. /* Call with RCU read lock */
  962. static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
  963. {
  964. struct ovs_ct_limit *ct_limit;
  965. struct hlist_head *head;
  966. head = ct_limit_hash_bucket(info, zone);
  967. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  968. if (ct_limit->zone == zone)
  969. return ct_limit->limit;
  970. }
  971. return info->default_limit;
  972. }
  973. static int ovs_ct_check_limit(struct net *net,
  974. const struct ovs_conntrack_info *info,
  975. const struct nf_conntrack_tuple *tuple)
  976. {
  977. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  978. const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  979. u32 per_zone_limit, connections;
  980. u32 conncount_key;
  981. conncount_key = info->zone.id;
  982. per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
  983. if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
  984. return 0;
  985. connections = nf_conncount_count(net, ct_limit_info->data,
  986. &conncount_key, tuple, &info->zone);
  987. if (connections > per_zone_limit)
  988. return -ENOMEM;
  989. return 0;
  990. }
  991. #endif
  992. /* Lookup connection and confirm if unconfirmed. */
  993. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  994. const struct ovs_conntrack_info *info,
  995. struct sk_buff *skb)
  996. {
  997. enum ip_conntrack_info ctinfo;
  998. struct nf_conn *ct;
  999. int err;
  1000. err = __ovs_ct_lookup(net, key, info, skb);
  1001. if (err)
  1002. return err;
  1003. /* The connection could be invalid, in which case this is a no-op.*/
  1004. ct = nf_ct_get(skb, &ctinfo);
  1005. if (!ct)
  1006. return 0;
  1007. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1008. if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
  1009. if (!nf_ct_is_confirmed(ct)) {
  1010. err = ovs_ct_check_limit(net, info,
  1011. &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  1012. if (err) {
  1013. net_warn_ratelimited("openvswitch: zone: %u "
  1014. "execeeds conntrack limit\n",
  1015. info->zone.id);
  1016. return err;
  1017. }
  1018. }
  1019. }
  1020. #endif
  1021. /* Set the conntrack event mask if given. NEW and DELETE events have
  1022. * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
  1023. * typically would receive many kinds of updates. Setting the event
  1024. * mask allows those events to be filtered. The set event mask will
  1025. * remain in effect for the lifetime of the connection unless changed
  1026. * by a further CT action with both the commit flag and the eventmask
  1027. * option. */
  1028. if (info->have_eventmask) {
  1029. struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
  1030. if (cache)
  1031. cache->ctmask = info->eventmask;
  1032. }
  1033. /* Apply changes before confirming the connection so that the initial
  1034. * conntrack NEW netlink event carries the values given in the CT
  1035. * action.
  1036. */
  1037. if (info->mark.mask) {
  1038. err = ovs_ct_set_mark(ct, key, info->mark.value,
  1039. info->mark.mask);
  1040. if (err)
  1041. return err;
  1042. }
  1043. if (!nf_ct_is_confirmed(ct)) {
  1044. err = ovs_ct_init_labels(ct, key, &info->labels.value,
  1045. &info->labels.mask);
  1046. if (err)
  1047. return err;
  1048. } else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1049. labels_nonzero(&info->labels.mask)) {
  1050. err = ovs_ct_set_labels(ct, key, &info->labels.value,
  1051. &info->labels.mask);
  1052. if (err)
  1053. return err;
  1054. }
  1055. /* This will take care of sending queued events even if the connection
  1056. * is already confirmed.
  1057. */
  1058. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  1059. return -EINVAL;
  1060. return 0;
  1061. }
  1062. /* Trim the skb to the length specified by the IP/IPv6 header,
  1063. * removing any trailing lower-layer padding. This prepares the skb
  1064. * for higher-layer processing that assumes skb->len excludes padding
  1065. * (such as nf_ip_checksum). The caller needs to pull the skb to the
  1066. * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
  1067. */
  1068. static int ovs_skb_network_trim(struct sk_buff *skb)
  1069. {
  1070. unsigned int len;
  1071. int err;
  1072. switch (skb->protocol) {
  1073. case htons(ETH_P_IP):
  1074. len = ntohs(ip_hdr(skb)->tot_len);
  1075. break;
  1076. case htons(ETH_P_IPV6):
  1077. len = sizeof(struct ipv6hdr)
  1078. + ntohs(ipv6_hdr(skb)->payload_len);
  1079. break;
  1080. default:
  1081. len = skb->len;
  1082. }
  1083. err = pskb_trim_rcsum(skb, len);
  1084. if (err)
  1085. kfree_skb(skb);
  1086. return err;
  1087. }
  1088. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  1089. * value if 'skb' is freed.
  1090. */
  1091. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  1092. struct sw_flow_key *key,
  1093. const struct ovs_conntrack_info *info)
  1094. {
  1095. int nh_ofs;
  1096. int err;
  1097. /* The conntrack module expects to be working at L3. */
  1098. nh_ofs = skb_network_offset(skb);
  1099. skb_pull_rcsum(skb, nh_ofs);
  1100. err = ovs_skb_network_trim(skb);
  1101. if (err)
  1102. return err;
  1103. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  1104. err = handle_fragments(net, key, info->zone.id, skb);
  1105. if (err)
  1106. return err;
  1107. }
  1108. if (info->commit)
  1109. err = ovs_ct_commit(net, key, info, skb);
  1110. else
  1111. err = ovs_ct_lookup(net, key, info, skb);
  1112. skb_push(skb, nh_ofs);
  1113. skb_postpush_rcsum(skb, skb->data, nh_ofs);
  1114. if (err)
  1115. kfree_skb(skb);
  1116. return err;
  1117. }
  1118. int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
  1119. {
  1120. if (skb_nfct(skb)) {
  1121. nf_conntrack_put(skb_nfct(skb));
  1122. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  1123. ovs_ct_fill_key(skb, key);
  1124. }
  1125. return 0;
  1126. }
  1127. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  1128. const struct sw_flow_key *key, bool log)
  1129. {
  1130. struct nf_conntrack_helper *helper;
  1131. struct nf_conn_help *help;
  1132. helper = nf_conntrack_helper_try_module_get(name, info->family,
  1133. key->ip.proto);
  1134. if (!helper) {
  1135. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  1136. return -EINVAL;
  1137. }
  1138. help = nf_ct_helper_ext_add(info->ct, GFP_KERNEL);
  1139. if (!help) {
  1140. nf_conntrack_helper_put(helper);
  1141. return -ENOMEM;
  1142. }
  1143. rcu_assign_pointer(help->helper, helper);
  1144. info->helper = helper;
  1145. if (info->nat)
  1146. request_module("ip_nat_%s", name);
  1147. return 0;
  1148. }
  1149. #ifdef CONFIG_NF_NAT_NEEDED
  1150. static int parse_nat(const struct nlattr *attr,
  1151. struct ovs_conntrack_info *info, bool log)
  1152. {
  1153. struct nlattr *a;
  1154. int rem;
  1155. bool have_ip_max = false;
  1156. bool have_proto_max = false;
  1157. bool ip_vers = (info->family == NFPROTO_IPV6);
  1158. nla_for_each_nested(a, attr, rem) {
  1159. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  1160. [OVS_NAT_ATTR_SRC] = {0, 0},
  1161. [OVS_NAT_ATTR_DST] = {0, 0},
  1162. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  1163. sizeof(struct in6_addr)},
  1164. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  1165. sizeof(struct in6_addr)},
  1166. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  1167. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  1168. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  1169. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  1170. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  1171. };
  1172. int type = nla_type(a);
  1173. if (type > OVS_NAT_ATTR_MAX) {
  1174. OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
  1175. type, OVS_NAT_ATTR_MAX);
  1176. return -EINVAL;
  1177. }
  1178. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  1179. OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
  1180. type, nla_len(a),
  1181. ovs_nat_attr_lens[type][ip_vers]);
  1182. return -EINVAL;
  1183. }
  1184. switch (type) {
  1185. case OVS_NAT_ATTR_SRC:
  1186. case OVS_NAT_ATTR_DST:
  1187. if (info->nat) {
  1188. OVS_NLERR(log, "Only one type of NAT may be specified");
  1189. return -ERANGE;
  1190. }
  1191. info->nat |= OVS_CT_NAT;
  1192. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  1193. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  1194. break;
  1195. case OVS_NAT_ATTR_IP_MIN:
  1196. nla_memcpy(&info->range.min_addr, a,
  1197. sizeof(info->range.min_addr));
  1198. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1199. break;
  1200. case OVS_NAT_ATTR_IP_MAX:
  1201. have_ip_max = true;
  1202. nla_memcpy(&info->range.max_addr, a,
  1203. sizeof(info->range.max_addr));
  1204. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1205. break;
  1206. case OVS_NAT_ATTR_PROTO_MIN:
  1207. info->range.min_proto.all = htons(nla_get_u16(a));
  1208. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1209. break;
  1210. case OVS_NAT_ATTR_PROTO_MAX:
  1211. have_proto_max = true;
  1212. info->range.max_proto.all = htons(nla_get_u16(a));
  1213. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1214. break;
  1215. case OVS_NAT_ATTR_PERSISTENT:
  1216. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  1217. break;
  1218. case OVS_NAT_ATTR_PROTO_HASH:
  1219. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  1220. break;
  1221. case OVS_NAT_ATTR_PROTO_RANDOM:
  1222. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  1223. break;
  1224. default:
  1225. OVS_NLERR(log, "Unknown nat attribute (%d)", type);
  1226. return -EINVAL;
  1227. }
  1228. }
  1229. if (rem > 0) {
  1230. OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
  1231. return -EINVAL;
  1232. }
  1233. if (!info->nat) {
  1234. /* Do not allow flags if no type is given. */
  1235. if (info->range.flags) {
  1236. OVS_NLERR(log,
  1237. "NAT flags may be given only when NAT range (SRC or DST) is also specified."
  1238. );
  1239. return -EINVAL;
  1240. }
  1241. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  1242. } else if (!info->commit) {
  1243. OVS_NLERR(log,
  1244. "NAT attributes may be specified only when CT COMMIT flag is also specified."
  1245. );
  1246. return -EINVAL;
  1247. }
  1248. /* Allow missing IP_MAX. */
  1249. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  1250. memcpy(&info->range.max_addr, &info->range.min_addr,
  1251. sizeof(info->range.max_addr));
  1252. }
  1253. /* Allow missing PROTO_MAX. */
  1254. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1255. !have_proto_max) {
  1256. info->range.max_proto.all = info->range.min_proto.all;
  1257. }
  1258. return 0;
  1259. }
  1260. #endif
  1261. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  1262. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1263. [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1264. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  1265. .maxlen = sizeof(u16) },
  1266. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  1267. .maxlen = sizeof(struct md_mark) },
  1268. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  1269. .maxlen = sizeof(struct md_labels) },
  1270. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  1271. .maxlen = NF_CT_HELPER_NAME_LEN },
  1272. #ifdef CONFIG_NF_NAT_NEEDED
  1273. /* NAT length is checked when parsing the nested attributes. */
  1274. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  1275. #endif
  1276. [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
  1277. .maxlen = sizeof(u32) },
  1278. };
  1279. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  1280. const char **helper, bool log)
  1281. {
  1282. struct nlattr *a;
  1283. int rem;
  1284. nla_for_each_nested(a, attr, rem) {
  1285. int type = nla_type(a);
  1286. int maxlen;
  1287. int minlen;
  1288. if (type > OVS_CT_ATTR_MAX) {
  1289. OVS_NLERR(log,
  1290. "Unknown conntrack attr (type=%d, max=%d)",
  1291. type, OVS_CT_ATTR_MAX);
  1292. return -EINVAL;
  1293. }
  1294. maxlen = ovs_ct_attr_lens[type].maxlen;
  1295. minlen = ovs_ct_attr_lens[type].minlen;
  1296. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  1297. OVS_NLERR(log,
  1298. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  1299. type, nla_len(a), maxlen);
  1300. return -EINVAL;
  1301. }
  1302. switch (type) {
  1303. case OVS_CT_ATTR_FORCE_COMMIT:
  1304. info->force = true;
  1305. /* fall through. */
  1306. case OVS_CT_ATTR_COMMIT:
  1307. info->commit = true;
  1308. break;
  1309. #ifdef CONFIG_NF_CONNTRACK_ZONES
  1310. case OVS_CT_ATTR_ZONE:
  1311. info->zone.id = nla_get_u16(a);
  1312. break;
  1313. #endif
  1314. #ifdef CONFIG_NF_CONNTRACK_MARK
  1315. case OVS_CT_ATTR_MARK: {
  1316. struct md_mark *mark = nla_data(a);
  1317. if (!mark->mask) {
  1318. OVS_NLERR(log, "ct_mark mask cannot be 0");
  1319. return -EINVAL;
  1320. }
  1321. info->mark = *mark;
  1322. break;
  1323. }
  1324. #endif
  1325. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1326. case OVS_CT_ATTR_LABELS: {
  1327. struct md_labels *labels = nla_data(a);
  1328. if (!labels_nonzero(&labels->mask)) {
  1329. OVS_NLERR(log, "ct_labels mask cannot be 0");
  1330. return -EINVAL;
  1331. }
  1332. info->labels = *labels;
  1333. break;
  1334. }
  1335. #endif
  1336. case OVS_CT_ATTR_HELPER:
  1337. *helper = nla_data(a);
  1338. if (!memchr(*helper, '\0', nla_len(a))) {
  1339. OVS_NLERR(log, "Invalid conntrack helper");
  1340. return -EINVAL;
  1341. }
  1342. break;
  1343. #ifdef CONFIG_NF_NAT_NEEDED
  1344. case OVS_CT_ATTR_NAT: {
  1345. int err = parse_nat(a, info, log);
  1346. if (err)
  1347. return err;
  1348. break;
  1349. }
  1350. #endif
  1351. case OVS_CT_ATTR_EVENTMASK:
  1352. info->have_eventmask = true;
  1353. info->eventmask = nla_get_u32(a);
  1354. break;
  1355. default:
  1356. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  1357. type);
  1358. return -EINVAL;
  1359. }
  1360. }
  1361. #ifdef CONFIG_NF_CONNTRACK_MARK
  1362. if (!info->commit && info->mark.mask) {
  1363. OVS_NLERR(log,
  1364. "Setting conntrack mark requires 'commit' flag.");
  1365. return -EINVAL;
  1366. }
  1367. #endif
  1368. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1369. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1370. OVS_NLERR(log,
  1371. "Setting conntrack labels requires 'commit' flag.");
  1372. return -EINVAL;
  1373. }
  1374. #endif
  1375. if (rem > 0) {
  1376. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1377. return -EINVAL;
  1378. }
  1379. return 0;
  1380. }
  1381. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1382. {
  1383. if (attr == OVS_KEY_ATTR_CT_STATE)
  1384. return true;
  1385. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1386. attr == OVS_KEY_ATTR_CT_ZONE)
  1387. return true;
  1388. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1389. attr == OVS_KEY_ATTR_CT_MARK)
  1390. return true;
  1391. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1392. attr == OVS_KEY_ATTR_CT_LABELS) {
  1393. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1394. return ovs_net->xt_label;
  1395. }
  1396. return false;
  1397. }
  1398. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1399. const struct sw_flow_key *key,
  1400. struct sw_flow_actions **sfa, bool log)
  1401. {
  1402. struct ovs_conntrack_info ct_info;
  1403. const char *helper = NULL;
  1404. u16 family;
  1405. int err;
  1406. family = key_to_nfproto(key);
  1407. if (family == NFPROTO_UNSPEC) {
  1408. OVS_NLERR(log, "ct family unspecified");
  1409. return -EINVAL;
  1410. }
  1411. memset(&ct_info, 0, sizeof(ct_info));
  1412. ct_info.family = family;
  1413. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1414. NF_CT_DEFAULT_ZONE_DIR, 0);
  1415. err = parse_ct(attr, &ct_info, &helper, log);
  1416. if (err)
  1417. return err;
  1418. /* Set up template for tracking connections in specific zones. */
  1419. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1420. if (!ct_info.ct) {
  1421. OVS_NLERR(log, "Failed to allocate conntrack template");
  1422. return -ENOMEM;
  1423. }
  1424. if (helper) {
  1425. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1426. if (err)
  1427. goto err_free_ct;
  1428. }
  1429. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1430. sizeof(ct_info), log);
  1431. if (err)
  1432. goto err_free_ct;
  1433. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1434. nf_conntrack_get(&ct_info.ct->ct_general);
  1435. return 0;
  1436. err_free_ct:
  1437. __ovs_ct_free_action(&ct_info);
  1438. return err;
  1439. }
  1440. #ifdef CONFIG_NF_NAT_NEEDED
  1441. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1442. struct sk_buff *skb)
  1443. {
  1444. struct nlattr *start;
  1445. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1446. if (!start)
  1447. return false;
  1448. if (info->nat & OVS_CT_SRC_NAT) {
  1449. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1450. return false;
  1451. } else if (info->nat & OVS_CT_DST_NAT) {
  1452. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1453. return false;
  1454. } else {
  1455. goto out;
  1456. }
  1457. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1458. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1459. info->family == NFPROTO_IPV4) {
  1460. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1461. info->range.min_addr.ip) ||
  1462. (info->range.max_addr.ip
  1463. != info->range.min_addr.ip &&
  1464. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1465. info->range.max_addr.ip))))
  1466. return false;
  1467. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1468. info->family == NFPROTO_IPV6) {
  1469. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1470. &info->range.min_addr.in6) ||
  1471. (memcmp(&info->range.max_addr.in6,
  1472. &info->range.min_addr.in6,
  1473. sizeof(info->range.max_addr.in6)) &&
  1474. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1475. &info->range.max_addr.in6))))
  1476. return false;
  1477. } else {
  1478. return false;
  1479. }
  1480. }
  1481. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1482. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1483. ntohs(info->range.min_proto.all)) ||
  1484. (info->range.max_proto.all != info->range.min_proto.all &&
  1485. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1486. ntohs(info->range.max_proto.all)))))
  1487. return false;
  1488. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1489. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1490. return false;
  1491. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1492. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1493. return false;
  1494. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1495. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1496. return false;
  1497. out:
  1498. nla_nest_end(skb, start);
  1499. return true;
  1500. }
  1501. #endif
  1502. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1503. struct sk_buff *skb)
  1504. {
  1505. struct nlattr *start;
  1506. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1507. if (!start)
  1508. return -EMSGSIZE;
  1509. if (ct_info->commit && nla_put_flag(skb, ct_info->force
  1510. ? OVS_CT_ATTR_FORCE_COMMIT
  1511. : OVS_CT_ATTR_COMMIT))
  1512. return -EMSGSIZE;
  1513. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1514. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1515. return -EMSGSIZE;
  1516. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1517. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1518. &ct_info->mark))
  1519. return -EMSGSIZE;
  1520. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1521. labels_nonzero(&ct_info->labels.mask) &&
  1522. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1523. &ct_info->labels))
  1524. return -EMSGSIZE;
  1525. if (ct_info->helper) {
  1526. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1527. ct_info->helper->name))
  1528. return -EMSGSIZE;
  1529. }
  1530. if (ct_info->have_eventmask &&
  1531. nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
  1532. return -EMSGSIZE;
  1533. #ifdef CONFIG_NF_NAT_NEEDED
  1534. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1535. return -EMSGSIZE;
  1536. #endif
  1537. nla_nest_end(skb, start);
  1538. return 0;
  1539. }
  1540. void ovs_ct_free_action(const struct nlattr *a)
  1541. {
  1542. struct ovs_conntrack_info *ct_info = nla_data(a);
  1543. __ovs_ct_free_action(ct_info);
  1544. }
  1545. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1546. {
  1547. if (ct_info->helper)
  1548. nf_conntrack_helper_put(ct_info->helper);
  1549. if (ct_info->ct)
  1550. nf_ct_tmpl_free(ct_info->ct);
  1551. }
  1552. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1553. static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
  1554. {
  1555. int i, err;
  1556. ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
  1557. GFP_KERNEL);
  1558. if (!ovs_net->ct_limit_info)
  1559. return -ENOMEM;
  1560. ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
  1561. ovs_net->ct_limit_info->limits =
  1562. kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
  1563. GFP_KERNEL);
  1564. if (!ovs_net->ct_limit_info->limits) {
  1565. kfree(ovs_net->ct_limit_info);
  1566. return -ENOMEM;
  1567. }
  1568. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
  1569. INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
  1570. ovs_net->ct_limit_info->data =
  1571. nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
  1572. if (IS_ERR(ovs_net->ct_limit_info->data)) {
  1573. err = PTR_ERR(ovs_net->ct_limit_info->data);
  1574. kfree(ovs_net->ct_limit_info->limits);
  1575. kfree(ovs_net->ct_limit_info);
  1576. pr_err("openvswitch: failed to init nf_conncount %d\n", err);
  1577. return err;
  1578. }
  1579. return 0;
  1580. }
  1581. static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
  1582. {
  1583. const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
  1584. int i;
  1585. nf_conncount_destroy(net, NFPROTO_INET, info->data);
  1586. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
  1587. struct hlist_head *head = &info->limits[i];
  1588. struct ovs_ct_limit *ct_limit;
  1589. hlist_for_each_entry_rcu(ct_limit, head, hlist_node)
  1590. kfree_rcu(ct_limit, rcu);
  1591. }
  1592. kfree(ovs_net->ct_limit_info->limits);
  1593. kfree(ovs_net->ct_limit_info);
  1594. }
  1595. static struct sk_buff *
  1596. ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
  1597. struct ovs_header **ovs_reply_header)
  1598. {
  1599. struct ovs_header *ovs_header = info->userhdr;
  1600. struct sk_buff *skb;
  1601. skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1602. if (!skb)
  1603. return ERR_PTR(-ENOMEM);
  1604. *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
  1605. info->snd_seq,
  1606. &dp_ct_limit_genl_family, 0, cmd);
  1607. if (!*ovs_reply_header) {
  1608. nlmsg_free(skb);
  1609. return ERR_PTR(-EMSGSIZE);
  1610. }
  1611. (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
  1612. return skb;
  1613. }
  1614. static bool check_zone_id(int zone_id, u16 *pzone)
  1615. {
  1616. if (zone_id >= 0 && zone_id <= 65535) {
  1617. *pzone = (u16)zone_id;
  1618. return true;
  1619. }
  1620. return false;
  1621. }
  1622. static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
  1623. struct ovs_ct_limit_info *info)
  1624. {
  1625. struct ovs_zone_limit *zone_limit;
  1626. int rem;
  1627. u16 zone;
  1628. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1629. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1630. while (rem >= sizeof(*zone_limit)) {
  1631. if (unlikely(zone_limit->zone_id ==
  1632. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1633. ovs_lock();
  1634. info->default_limit = zone_limit->limit;
  1635. ovs_unlock();
  1636. } else if (unlikely(!check_zone_id(
  1637. zone_limit->zone_id, &zone))) {
  1638. OVS_NLERR(true, "zone id is out of range");
  1639. } else {
  1640. struct ovs_ct_limit *ct_limit;
  1641. ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
  1642. if (!ct_limit)
  1643. return -ENOMEM;
  1644. ct_limit->zone = zone;
  1645. ct_limit->limit = zone_limit->limit;
  1646. ovs_lock();
  1647. ct_limit_set(info, ct_limit);
  1648. ovs_unlock();
  1649. }
  1650. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1651. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1652. NLA_ALIGN(sizeof(*zone_limit)));
  1653. }
  1654. if (rem)
  1655. OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
  1656. return 0;
  1657. }
  1658. static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
  1659. struct ovs_ct_limit_info *info)
  1660. {
  1661. struct ovs_zone_limit *zone_limit;
  1662. int rem;
  1663. u16 zone;
  1664. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1665. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1666. while (rem >= sizeof(*zone_limit)) {
  1667. if (unlikely(zone_limit->zone_id ==
  1668. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1669. ovs_lock();
  1670. info->default_limit = OVS_CT_LIMIT_DEFAULT;
  1671. ovs_unlock();
  1672. } else if (unlikely(!check_zone_id(
  1673. zone_limit->zone_id, &zone))) {
  1674. OVS_NLERR(true, "zone id is out of range");
  1675. } else {
  1676. ovs_lock();
  1677. ct_limit_del(info, zone);
  1678. ovs_unlock();
  1679. }
  1680. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1681. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1682. NLA_ALIGN(sizeof(*zone_limit)));
  1683. }
  1684. if (rem)
  1685. OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
  1686. return 0;
  1687. }
  1688. static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
  1689. struct sk_buff *reply)
  1690. {
  1691. struct ovs_zone_limit zone_limit;
  1692. int err;
  1693. zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
  1694. zone_limit.limit = info->default_limit;
  1695. err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
  1696. if (err)
  1697. return err;
  1698. return 0;
  1699. }
  1700. static int __ovs_ct_limit_get_zone_limit(struct net *net,
  1701. struct nf_conncount_data *data,
  1702. u16 zone_id, u32 limit,
  1703. struct sk_buff *reply)
  1704. {
  1705. struct nf_conntrack_zone ct_zone;
  1706. struct ovs_zone_limit zone_limit;
  1707. u32 conncount_key = zone_id;
  1708. zone_limit.zone_id = zone_id;
  1709. zone_limit.limit = limit;
  1710. nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
  1711. zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
  1712. &ct_zone);
  1713. return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
  1714. }
  1715. static int ovs_ct_limit_get_zone_limit(struct net *net,
  1716. struct nlattr *nla_zone_limit,
  1717. struct ovs_ct_limit_info *info,
  1718. struct sk_buff *reply)
  1719. {
  1720. struct ovs_zone_limit *zone_limit;
  1721. int rem, err;
  1722. u32 limit;
  1723. u16 zone;
  1724. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1725. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1726. while (rem >= sizeof(*zone_limit)) {
  1727. if (unlikely(zone_limit->zone_id ==
  1728. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1729. err = ovs_ct_limit_get_default_limit(info, reply);
  1730. if (err)
  1731. return err;
  1732. } else if (unlikely(!check_zone_id(zone_limit->zone_id,
  1733. &zone))) {
  1734. OVS_NLERR(true, "zone id is out of range");
  1735. } else {
  1736. rcu_read_lock();
  1737. limit = ct_limit_get(info, zone);
  1738. rcu_read_unlock();
  1739. err = __ovs_ct_limit_get_zone_limit(
  1740. net, info->data, zone, limit, reply);
  1741. if (err)
  1742. return err;
  1743. }
  1744. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1745. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1746. NLA_ALIGN(sizeof(*zone_limit)));
  1747. }
  1748. if (rem)
  1749. OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
  1750. return 0;
  1751. }
  1752. static int ovs_ct_limit_get_all_zone_limit(struct net *net,
  1753. struct ovs_ct_limit_info *info,
  1754. struct sk_buff *reply)
  1755. {
  1756. struct ovs_ct_limit *ct_limit;
  1757. struct hlist_head *head;
  1758. int i, err = 0;
  1759. err = ovs_ct_limit_get_default_limit(info, reply);
  1760. if (err)
  1761. return err;
  1762. rcu_read_lock();
  1763. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
  1764. head = &info->limits[i];
  1765. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  1766. err = __ovs_ct_limit_get_zone_limit(net, info->data,
  1767. ct_limit->zone, ct_limit->limit, reply);
  1768. if (err)
  1769. goto exit_err;
  1770. }
  1771. }
  1772. exit_err:
  1773. rcu_read_unlock();
  1774. return err;
  1775. }
  1776. static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1777. {
  1778. struct nlattr **a = info->attrs;
  1779. struct sk_buff *reply;
  1780. struct ovs_header *ovs_reply_header;
  1781. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1782. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1783. int err;
  1784. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
  1785. &ovs_reply_header);
  1786. if (IS_ERR(reply))
  1787. return PTR_ERR(reply);
  1788. if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1789. err = -EINVAL;
  1790. goto exit_err;
  1791. }
  1792. err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
  1793. ct_limit_info);
  1794. if (err)
  1795. goto exit_err;
  1796. static_branch_enable(&ovs_ct_limit_enabled);
  1797. genlmsg_end(reply, ovs_reply_header);
  1798. return genlmsg_reply(reply, info);
  1799. exit_err:
  1800. nlmsg_free(reply);
  1801. return err;
  1802. }
  1803. static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1804. {
  1805. struct nlattr **a = info->attrs;
  1806. struct sk_buff *reply;
  1807. struct ovs_header *ovs_reply_header;
  1808. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1809. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1810. int err;
  1811. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
  1812. &ovs_reply_header);
  1813. if (IS_ERR(reply))
  1814. return PTR_ERR(reply);
  1815. if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1816. err = -EINVAL;
  1817. goto exit_err;
  1818. }
  1819. err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
  1820. ct_limit_info);
  1821. if (err)
  1822. goto exit_err;
  1823. genlmsg_end(reply, ovs_reply_header);
  1824. return genlmsg_reply(reply, info);
  1825. exit_err:
  1826. nlmsg_free(reply);
  1827. return err;
  1828. }
  1829. static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1830. {
  1831. struct nlattr **a = info->attrs;
  1832. struct nlattr *nla_reply;
  1833. struct sk_buff *reply;
  1834. struct ovs_header *ovs_reply_header;
  1835. struct net *net = sock_net(skb->sk);
  1836. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1837. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1838. int err;
  1839. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
  1840. &ovs_reply_header);
  1841. if (IS_ERR(reply))
  1842. return PTR_ERR(reply);
  1843. nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
  1844. if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1845. err = ovs_ct_limit_get_zone_limit(
  1846. net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
  1847. reply);
  1848. if (err)
  1849. goto exit_err;
  1850. } else {
  1851. err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
  1852. reply);
  1853. if (err)
  1854. goto exit_err;
  1855. }
  1856. nla_nest_end(reply, nla_reply);
  1857. genlmsg_end(reply, ovs_reply_header);
  1858. return genlmsg_reply(reply, info);
  1859. exit_err:
  1860. nlmsg_free(reply);
  1861. return err;
  1862. }
  1863. static struct genl_ops ct_limit_genl_ops[] = {
  1864. { .cmd = OVS_CT_LIMIT_CMD_SET,
  1865. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
  1866. * privilege. */
  1867. .policy = ct_limit_policy,
  1868. .doit = ovs_ct_limit_cmd_set,
  1869. },
  1870. { .cmd = OVS_CT_LIMIT_CMD_DEL,
  1871. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
  1872. * privilege. */
  1873. .policy = ct_limit_policy,
  1874. .doit = ovs_ct_limit_cmd_del,
  1875. },
  1876. { .cmd = OVS_CT_LIMIT_CMD_GET,
  1877. .flags = 0, /* OK for unprivileged users. */
  1878. .policy = ct_limit_policy,
  1879. .doit = ovs_ct_limit_cmd_get,
  1880. },
  1881. };
  1882. static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
  1883. .name = OVS_CT_LIMIT_MCGROUP,
  1884. };
  1885. struct genl_family dp_ct_limit_genl_family __ro_after_init = {
  1886. .hdrsize = sizeof(struct ovs_header),
  1887. .name = OVS_CT_LIMIT_FAMILY,
  1888. .version = OVS_CT_LIMIT_VERSION,
  1889. .maxattr = OVS_CT_LIMIT_ATTR_MAX,
  1890. .netnsok = true,
  1891. .parallel_ops = true,
  1892. .ops = ct_limit_genl_ops,
  1893. .n_ops = ARRAY_SIZE(ct_limit_genl_ops),
  1894. .mcgrps = &ovs_ct_limit_multicast_group,
  1895. .n_mcgrps = 1,
  1896. .module = THIS_MODULE,
  1897. };
  1898. #endif
  1899. int ovs_ct_init(struct net *net)
  1900. {
  1901. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1902. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1903. if (nf_connlabels_get(net, n_bits - 1)) {
  1904. ovs_net->xt_label = false;
  1905. OVS_NLERR(true, "Failed to set connlabel length");
  1906. } else {
  1907. ovs_net->xt_label = true;
  1908. }
  1909. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1910. return ovs_ct_limit_init(net, ovs_net);
  1911. #else
  1912. return 0;
  1913. #endif
  1914. }
  1915. void ovs_ct_exit(struct net *net)
  1916. {
  1917. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1918. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1919. ovs_ct_limit_exit(net, ovs_net);
  1920. #endif
  1921. if (ovs_net->xt_label)
  1922. nf_connlabels_put(net);
  1923. }