conntrack.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. /* Mark NAT done if successful and update the flow key. */
  779. if (err == NF_ACCEPT)
  780. ovs_nat_update_key(key, skb, maniptype);
  781. return err;
  782. }
  783. #else /* !CONFIG_NF_NAT_NEEDED */
  784. static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
  785. const struct ovs_conntrack_info *info,
  786. struct sk_buff *skb, struct nf_conn *ct,
  787. enum ip_conntrack_info ctinfo)
  788. {
  789. return NF_ACCEPT;
  790. }
  791. #endif
  792. /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
  793. * not done already. Update key with new CT state after passing the packet
  794. * through conntrack.
  795. * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
  796. * set to NULL and 0 will be returned.
  797. */
  798. static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  799. const struct ovs_conntrack_info *info,
  800. struct sk_buff *skb)
  801. {
  802. /* If we are recirculating packets to match on conntrack fields and
  803. * committing with a separate conntrack action, then we don't need to
  804. * actually run the packet through conntrack twice unless it's for a
  805. * different zone.
  806. */
  807. bool cached = skb_nfct_cached(net, key, info, skb);
  808. enum ip_conntrack_info ctinfo;
  809. struct nf_conn *ct;
  810. if (!cached) {
  811. struct nf_conn *tmpl = info->ct;
  812. int err;
  813. /* Associate skb with specified zone. */
  814. if (tmpl) {
  815. if (skb_nfct(skb))
  816. nf_conntrack_put(skb_nfct(skb));
  817. nf_conntrack_get(&tmpl->ct_general);
  818. nf_ct_set(skb, tmpl, IP_CT_NEW);
  819. }
  820. err = nf_conntrack_in(net, info->family,
  821. NF_INET_PRE_ROUTING, skb);
  822. if (err != NF_ACCEPT)
  823. return -ENOENT;
  824. /* Clear CT state NAT flags to mark that we have not yet done
  825. * NAT after the nf_conntrack_in() call. We can actually clear
  826. * the whole state, as it will be re-initialized below.
  827. */
  828. key->ct_state = 0;
  829. /* Update the key, but keep the NAT flags. */
  830. ovs_ct_update_key(skb, info, key, true, true);
  831. }
  832. ct = nf_ct_get(skb, &ctinfo);
  833. if (ct) {
  834. /* Packets starting a new connection must be NATted before the
  835. * helper, so that the helper knows about the NAT. We enforce
  836. * this by delaying both NAT and helper calls for unconfirmed
  837. * connections until the committing CT action. For later
  838. * packets NAT and Helper may be called in either order.
  839. *
  840. * NAT will be done only if the CT action has NAT, and only
  841. * once per packet (per zone), as guarded by the NAT bits in
  842. * the key->ct_state.
  843. */
  844. if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
  845. (nf_ct_is_confirmed(ct) || info->commit) &&
  846. ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
  847. return -EINVAL;
  848. }
  849. /* Userspace may decide to perform a ct lookup without a helper
  850. * specified followed by a (recirculate and) commit with one.
  851. * Therefore, for unconfirmed connections which we will commit,
  852. * we need to attach the helper here.
  853. */
  854. if (!nf_ct_is_confirmed(ct) && info->commit &&
  855. info->helper && !nfct_help(ct)) {
  856. int err = __nf_ct_try_assign_helper(ct, info->ct,
  857. GFP_ATOMIC);
  858. if (err)
  859. return err;
  860. }
  861. /* Call the helper only if:
  862. * - nf_conntrack_in() was executed above ("!cached") for a
  863. * confirmed connection, or
  864. * - When committing an unconfirmed connection.
  865. */
  866. if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
  867. ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
  868. return -EINVAL;
  869. }
  870. }
  871. return 0;
  872. }
  873. /* Lookup connection and read fields into key. */
  874. static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
  875. const struct ovs_conntrack_info *info,
  876. struct sk_buff *skb)
  877. {
  878. struct nf_conntrack_expect *exp;
  879. /* If we pass an expected packet through nf_conntrack_in() the
  880. * expectation is typically removed, but the packet could still be
  881. * lost in upcall processing. To prevent this from happening we
  882. * perform an explicit expectation lookup. Expected connections are
  883. * always new, and will be passed through conntrack only when they are
  884. * committed, as it is OK to remove the expectation at that time.
  885. */
  886. exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
  887. if (exp) {
  888. u8 state;
  889. /* NOTE: New connections are NATted and Helped only when
  890. * committed, so we are not calling into NAT here.
  891. */
  892. state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
  893. __ovs_ct_update_key(key, state, &info->zone, exp->master);
  894. } else {
  895. struct nf_conn *ct;
  896. int err;
  897. err = __ovs_ct_lookup(net, key, info, skb);
  898. if (err)
  899. return err;
  900. ct = (struct nf_conn *)skb_nfct(skb);
  901. if (ct)
  902. nf_ct_deliver_cached_events(ct);
  903. }
  904. return 0;
  905. }
  906. static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
  907. {
  908. size_t i;
  909. for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
  910. if (labels->ct_labels_32[i])
  911. return true;
  912. return false;
  913. }
  914. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  915. static struct hlist_head *ct_limit_hash_bucket(
  916. const struct ovs_ct_limit_info *info, u16 zone)
  917. {
  918. return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
  919. }
  920. /* Call with ovs_mutex */
  921. static void ct_limit_set(const struct ovs_ct_limit_info *info,
  922. struct ovs_ct_limit *new_ct_limit)
  923. {
  924. struct ovs_ct_limit *ct_limit;
  925. struct hlist_head *head;
  926. head = ct_limit_hash_bucket(info, new_ct_limit->zone);
  927. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  928. if (ct_limit->zone == new_ct_limit->zone) {
  929. hlist_replace_rcu(&ct_limit->hlist_node,
  930. &new_ct_limit->hlist_node);
  931. kfree_rcu(ct_limit, rcu);
  932. return;
  933. }
  934. }
  935. hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
  936. }
  937. /* Call with ovs_mutex */
  938. static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
  939. {
  940. struct ovs_ct_limit *ct_limit;
  941. struct hlist_head *head;
  942. struct hlist_node *n;
  943. head = ct_limit_hash_bucket(info, zone);
  944. hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
  945. if (ct_limit->zone == zone) {
  946. hlist_del_rcu(&ct_limit->hlist_node);
  947. kfree_rcu(ct_limit, rcu);
  948. return;
  949. }
  950. }
  951. }
  952. /* Call with RCU read lock */
  953. static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
  954. {
  955. struct ovs_ct_limit *ct_limit;
  956. struct hlist_head *head;
  957. head = ct_limit_hash_bucket(info, zone);
  958. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  959. if (ct_limit->zone == zone)
  960. return ct_limit->limit;
  961. }
  962. return info->default_limit;
  963. }
  964. static int ovs_ct_check_limit(struct net *net,
  965. const struct ovs_conntrack_info *info,
  966. const struct nf_conntrack_tuple *tuple)
  967. {
  968. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  969. const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  970. u32 per_zone_limit, connections;
  971. u32 conncount_key;
  972. conncount_key = info->zone.id;
  973. per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
  974. if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
  975. return 0;
  976. connections = nf_conncount_count(net, ct_limit_info->data,
  977. &conncount_key, tuple, &info->zone);
  978. if (connections > per_zone_limit)
  979. return -ENOMEM;
  980. return 0;
  981. }
  982. #endif
  983. /* Lookup connection and confirm if unconfirmed. */
  984. static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
  985. const struct ovs_conntrack_info *info,
  986. struct sk_buff *skb)
  987. {
  988. enum ip_conntrack_info ctinfo;
  989. struct nf_conn *ct;
  990. int err;
  991. err = __ovs_ct_lookup(net, key, info, skb);
  992. if (err)
  993. return err;
  994. /* The connection could be invalid, in which case this is a no-op.*/
  995. ct = nf_ct_get(skb, &ctinfo);
  996. if (!ct)
  997. return 0;
  998. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  999. if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
  1000. if (!nf_ct_is_confirmed(ct)) {
  1001. err = ovs_ct_check_limit(net, info,
  1002. &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  1003. if (err) {
  1004. net_warn_ratelimited("openvswitch: zone: %u "
  1005. "execeeds conntrack limit\n",
  1006. info->zone.id);
  1007. return err;
  1008. }
  1009. }
  1010. }
  1011. #endif
  1012. /* Set the conntrack event mask if given. NEW and DELETE events have
  1013. * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
  1014. * typically would receive many kinds of updates. Setting the event
  1015. * mask allows those events to be filtered. The set event mask will
  1016. * remain in effect for the lifetime of the connection unless changed
  1017. * by a further CT action with both the commit flag and the eventmask
  1018. * option. */
  1019. if (info->have_eventmask) {
  1020. struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
  1021. if (cache)
  1022. cache->ctmask = info->eventmask;
  1023. }
  1024. /* Apply changes before confirming the connection so that the initial
  1025. * conntrack NEW netlink event carries the values given in the CT
  1026. * action.
  1027. */
  1028. if (info->mark.mask) {
  1029. err = ovs_ct_set_mark(ct, key, info->mark.value,
  1030. info->mark.mask);
  1031. if (err)
  1032. return err;
  1033. }
  1034. if (!nf_ct_is_confirmed(ct)) {
  1035. err = ovs_ct_init_labels(ct, key, &info->labels.value,
  1036. &info->labels.mask);
  1037. if (err)
  1038. return err;
  1039. } else if (labels_nonzero(&info->labels.mask)) {
  1040. err = ovs_ct_set_labels(ct, key, &info->labels.value,
  1041. &info->labels.mask);
  1042. if (err)
  1043. return err;
  1044. }
  1045. /* This will take care of sending queued events even if the connection
  1046. * is already confirmed.
  1047. */
  1048. if (nf_conntrack_confirm(skb) != NF_ACCEPT)
  1049. return -EINVAL;
  1050. return 0;
  1051. }
  1052. /* Trim the skb to the length specified by the IP/IPv6 header,
  1053. * removing any trailing lower-layer padding. This prepares the skb
  1054. * for higher-layer processing that assumes skb->len excludes padding
  1055. * (such as nf_ip_checksum). The caller needs to pull the skb to the
  1056. * network header, and ensure ip_hdr/ipv6_hdr points to valid data.
  1057. */
  1058. static int ovs_skb_network_trim(struct sk_buff *skb)
  1059. {
  1060. unsigned int len;
  1061. int err;
  1062. switch (skb->protocol) {
  1063. case htons(ETH_P_IP):
  1064. len = ntohs(ip_hdr(skb)->tot_len);
  1065. break;
  1066. case htons(ETH_P_IPV6):
  1067. len = sizeof(struct ipv6hdr)
  1068. + ntohs(ipv6_hdr(skb)->payload_len);
  1069. break;
  1070. default:
  1071. len = skb->len;
  1072. }
  1073. err = pskb_trim_rcsum(skb, len);
  1074. if (err)
  1075. kfree_skb(skb);
  1076. return err;
  1077. }
  1078. /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
  1079. * value if 'skb' is freed.
  1080. */
  1081. int ovs_ct_execute(struct net *net, struct sk_buff *skb,
  1082. struct sw_flow_key *key,
  1083. const struct ovs_conntrack_info *info)
  1084. {
  1085. int nh_ofs;
  1086. int err;
  1087. /* The conntrack module expects to be working at L3. */
  1088. nh_ofs = skb_network_offset(skb);
  1089. skb_pull_rcsum(skb, nh_ofs);
  1090. err = ovs_skb_network_trim(skb);
  1091. if (err)
  1092. return err;
  1093. if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
  1094. err = handle_fragments(net, key, info->zone.id, skb);
  1095. if (err)
  1096. return err;
  1097. }
  1098. if (info->commit)
  1099. err = ovs_ct_commit(net, key, info, skb);
  1100. else
  1101. err = ovs_ct_lookup(net, key, info, skb);
  1102. skb_push(skb, nh_ofs);
  1103. skb_postpush_rcsum(skb, skb->data, nh_ofs);
  1104. if (err)
  1105. kfree_skb(skb);
  1106. return err;
  1107. }
  1108. int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
  1109. {
  1110. if (skb_nfct(skb)) {
  1111. nf_conntrack_put(skb_nfct(skb));
  1112. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  1113. ovs_ct_fill_key(skb, key);
  1114. }
  1115. return 0;
  1116. }
  1117. static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
  1118. const struct sw_flow_key *key, bool log)
  1119. {
  1120. struct nf_conntrack_helper *helper;
  1121. struct nf_conn_help *help;
  1122. helper = nf_conntrack_helper_try_module_get(name, info->family,
  1123. key->ip.proto);
  1124. if (!helper) {
  1125. OVS_NLERR(log, "Unknown helper \"%s\"", name);
  1126. return -EINVAL;
  1127. }
  1128. help = nf_ct_helper_ext_add(info->ct, GFP_KERNEL);
  1129. if (!help) {
  1130. nf_conntrack_helper_put(helper);
  1131. return -ENOMEM;
  1132. }
  1133. rcu_assign_pointer(help->helper, helper);
  1134. info->helper = helper;
  1135. if (info->nat)
  1136. request_module("ip_nat_%s", name);
  1137. return 0;
  1138. }
  1139. #ifdef CONFIG_NF_NAT_NEEDED
  1140. static int parse_nat(const struct nlattr *attr,
  1141. struct ovs_conntrack_info *info, bool log)
  1142. {
  1143. struct nlattr *a;
  1144. int rem;
  1145. bool have_ip_max = false;
  1146. bool have_proto_max = false;
  1147. bool ip_vers = (info->family == NFPROTO_IPV6);
  1148. nla_for_each_nested(a, attr, rem) {
  1149. static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
  1150. [OVS_NAT_ATTR_SRC] = {0, 0},
  1151. [OVS_NAT_ATTR_DST] = {0, 0},
  1152. [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
  1153. sizeof(struct in6_addr)},
  1154. [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
  1155. sizeof(struct in6_addr)},
  1156. [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
  1157. [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
  1158. [OVS_NAT_ATTR_PERSISTENT] = {0, 0},
  1159. [OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
  1160. [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
  1161. };
  1162. int type = nla_type(a);
  1163. if (type > OVS_NAT_ATTR_MAX) {
  1164. OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
  1165. type, OVS_NAT_ATTR_MAX);
  1166. return -EINVAL;
  1167. }
  1168. if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
  1169. OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
  1170. type, nla_len(a),
  1171. ovs_nat_attr_lens[type][ip_vers]);
  1172. return -EINVAL;
  1173. }
  1174. switch (type) {
  1175. case OVS_NAT_ATTR_SRC:
  1176. case OVS_NAT_ATTR_DST:
  1177. if (info->nat) {
  1178. OVS_NLERR(log, "Only one type of NAT may be specified");
  1179. return -ERANGE;
  1180. }
  1181. info->nat |= OVS_CT_NAT;
  1182. info->nat |= ((type == OVS_NAT_ATTR_SRC)
  1183. ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
  1184. break;
  1185. case OVS_NAT_ATTR_IP_MIN:
  1186. nla_memcpy(&info->range.min_addr, a,
  1187. sizeof(info->range.min_addr));
  1188. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1189. break;
  1190. case OVS_NAT_ATTR_IP_MAX:
  1191. have_ip_max = true;
  1192. nla_memcpy(&info->range.max_addr, a,
  1193. sizeof(info->range.max_addr));
  1194. info->range.flags |= NF_NAT_RANGE_MAP_IPS;
  1195. break;
  1196. case OVS_NAT_ATTR_PROTO_MIN:
  1197. info->range.min_proto.all = htons(nla_get_u16(a));
  1198. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1199. break;
  1200. case OVS_NAT_ATTR_PROTO_MAX:
  1201. have_proto_max = true;
  1202. info->range.max_proto.all = htons(nla_get_u16(a));
  1203. info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
  1204. break;
  1205. case OVS_NAT_ATTR_PERSISTENT:
  1206. info->range.flags |= NF_NAT_RANGE_PERSISTENT;
  1207. break;
  1208. case OVS_NAT_ATTR_PROTO_HASH:
  1209. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
  1210. break;
  1211. case OVS_NAT_ATTR_PROTO_RANDOM:
  1212. info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
  1213. break;
  1214. default:
  1215. OVS_NLERR(log, "Unknown nat attribute (%d)", type);
  1216. return -EINVAL;
  1217. }
  1218. }
  1219. if (rem > 0) {
  1220. OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
  1221. return -EINVAL;
  1222. }
  1223. if (!info->nat) {
  1224. /* Do not allow flags if no type is given. */
  1225. if (info->range.flags) {
  1226. OVS_NLERR(log,
  1227. "NAT flags may be given only when NAT range (SRC or DST) is also specified."
  1228. );
  1229. return -EINVAL;
  1230. }
  1231. info->nat = OVS_CT_NAT; /* NAT existing connections. */
  1232. } else if (!info->commit) {
  1233. OVS_NLERR(log,
  1234. "NAT attributes may be specified only when CT COMMIT flag is also specified."
  1235. );
  1236. return -EINVAL;
  1237. }
  1238. /* Allow missing IP_MAX. */
  1239. if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
  1240. memcpy(&info->range.max_addr, &info->range.min_addr,
  1241. sizeof(info->range.max_addr));
  1242. }
  1243. /* Allow missing PROTO_MAX. */
  1244. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1245. !have_proto_max) {
  1246. info->range.max_proto.all = info->range.min_proto.all;
  1247. }
  1248. return 0;
  1249. }
  1250. #endif
  1251. static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
  1252. [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1253. [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
  1254. [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
  1255. .maxlen = sizeof(u16) },
  1256. [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
  1257. .maxlen = sizeof(struct md_mark) },
  1258. [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
  1259. .maxlen = sizeof(struct md_labels) },
  1260. [OVS_CT_ATTR_HELPER] = { .minlen = 1,
  1261. .maxlen = NF_CT_HELPER_NAME_LEN },
  1262. #ifdef CONFIG_NF_NAT_NEEDED
  1263. /* NAT length is checked when parsing the nested attributes. */
  1264. [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
  1265. #endif
  1266. [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
  1267. .maxlen = sizeof(u32) },
  1268. };
  1269. static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
  1270. const char **helper, bool log)
  1271. {
  1272. struct nlattr *a;
  1273. int rem;
  1274. nla_for_each_nested(a, attr, rem) {
  1275. int type = nla_type(a);
  1276. int maxlen;
  1277. int minlen;
  1278. if (type > OVS_CT_ATTR_MAX) {
  1279. OVS_NLERR(log,
  1280. "Unknown conntrack attr (type=%d, max=%d)",
  1281. type, OVS_CT_ATTR_MAX);
  1282. return -EINVAL;
  1283. }
  1284. maxlen = ovs_ct_attr_lens[type].maxlen;
  1285. minlen = ovs_ct_attr_lens[type].minlen;
  1286. if (nla_len(a) < minlen || nla_len(a) > maxlen) {
  1287. OVS_NLERR(log,
  1288. "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
  1289. type, nla_len(a), maxlen);
  1290. return -EINVAL;
  1291. }
  1292. switch (type) {
  1293. case OVS_CT_ATTR_FORCE_COMMIT:
  1294. info->force = true;
  1295. /* fall through. */
  1296. case OVS_CT_ATTR_COMMIT:
  1297. info->commit = true;
  1298. break;
  1299. #ifdef CONFIG_NF_CONNTRACK_ZONES
  1300. case OVS_CT_ATTR_ZONE:
  1301. info->zone.id = nla_get_u16(a);
  1302. break;
  1303. #endif
  1304. #ifdef CONFIG_NF_CONNTRACK_MARK
  1305. case OVS_CT_ATTR_MARK: {
  1306. struct md_mark *mark = nla_data(a);
  1307. if (!mark->mask) {
  1308. OVS_NLERR(log, "ct_mark mask cannot be 0");
  1309. return -EINVAL;
  1310. }
  1311. info->mark = *mark;
  1312. break;
  1313. }
  1314. #endif
  1315. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1316. case OVS_CT_ATTR_LABELS: {
  1317. struct md_labels *labels = nla_data(a);
  1318. if (!labels_nonzero(&labels->mask)) {
  1319. OVS_NLERR(log, "ct_labels mask cannot be 0");
  1320. return -EINVAL;
  1321. }
  1322. info->labels = *labels;
  1323. break;
  1324. }
  1325. #endif
  1326. case OVS_CT_ATTR_HELPER:
  1327. *helper = nla_data(a);
  1328. if (!memchr(*helper, '\0', nla_len(a))) {
  1329. OVS_NLERR(log, "Invalid conntrack helper");
  1330. return -EINVAL;
  1331. }
  1332. break;
  1333. #ifdef CONFIG_NF_NAT_NEEDED
  1334. case OVS_CT_ATTR_NAT: {
  1335. int err = parse_nat(a, info, log);
  1336. if (err)
  1337. return err;
  1338. break;
  1339. }
  1340. #endif
  1341. case OVS_CT_ATTR_EVENTMASK:
  1342. info->have_eventmask = true;
  1343. info->eventmask = nla_get_u32(a);
  1344. break;
  1345. default:
  1346. OVS_NLERR(log, "Unknown conntrack attr (%d)",
  1347. type);
  1348. return -EINVAL;
  1349. }
  1350. }
  1351. #ifdef CONFIG_NF_CONNTRACK_MARK
  1352. if (!info->commit && info->mark.mask) {
  1353. OVS_NLERR(log,
  1354. "Setting conntrack mark requires 'commit' flag.");
  1355. return -EINVAL;
  1356. }
  1357. #endif
  1358. #ifdef CONFIG_NF_CONNTRACK_LABELS
  1359. if (!info->commit && labels_nonzero(&info->labels.mask)) {
  1360. OVS_NLERR(log,
  1361. "Setting conntrack labels requires 'commit' flag.");
  1362. return -EINVAL;
  1363. }
  1364. #endif
  1365. if (rem > 0) {
  1366. OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
  1367. return -EINVAL;
  1368. }
  1369. return 0;
  1370. }
  1371. bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
  1372. {
  1373. if (attr == OVS_KEY_ATTR_CT_STATE)
  1374. return true;
  1375. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1376. attr == OVS_KEY_ATTR_CT_ZONE)
  1377. return true;
  1378. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
  1379. attr == OVS_KEY_ATTR_CT_MARK)
  1380. return true;
  1381. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1382. attr == OVS_KEY_ATTR_CT_LABELS) {
  1383. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1384. return ovs_net->xt_label;
  1385. }
  1386. return false;
  1387. }
  1388. int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
  1389. const struct sw_flow_key *key,
  1390. struct sw_flow_actions **sfa, bool log)
  1391. {
  1392. struct ovs_conntrack_info ct_info;
  1393. const char *helper = NULL;
  1394. u16 family;
  1395. int err;
  1396. family = key_to_nfproto(key);
  1397. if (family == NFPROTO_UNSPEC) {
  1398. OVS_NLERR(log, "ct family unspecified");
  1399. return -EINVAL;
  1400. }
  1401. memset(&ct_info, 0, sizeof(ct_info));
  1402. ct_info.family = family;
  1403. nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
  1404. NF_CT_DEFAULT_ZONE_DIR, 0);
  1405. err = parse_ct(attr, &ct_info, &helper, log);
  1406. if (err)
  1407. return err;
  1408. /* Set up template for tracking connections in specific zones. */
  1409. ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
  1410. if (!ct_info.ct) {
  1411. OVS_NLERR(log, "Failed to allocate conntrack template");
  1412. return -ENOMEM;
  1413. }
  1414. if (helper) {
  1415. err = ovs_ct_add_helper(&ct_info, helper, key, log);
  1416. if (err)
  1417. goto err_free_ct;
  1418. }
  1419. err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
  1420. sizeof(ct_info), log);
  1421. if (err)
  1422. goto err_free_ct;
  1423. __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
  1424. nf_conntrack_get(&ct_info.ct->ct_general);
  1425. return 0;
  1426. err_free_ct:
  1427. __ovs_ct_free_action(&ct_info);
  1428. return err;
  1429. }
  1430. #ifdef CONFIG_NF_NAT_NEEDED
  1431. static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
  1432. struct sk_buff *skb)
  1433. {
  1434. struct nlattr *start;
  1435. start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
  1436. if (!start)
  1437. return false;
  1438. if (info->nat & OVS_CT_SRC_NAT) {
  1439. if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
  1440. return false;
  1441. } else if (info->nat & OVS_CT_DST_NAT) {
  1442. if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
  1443. return false;
  1444. } else {
  1445. goto out;
  1446. }
  1447. if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
  1448. if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
  1449. info->family == NFPROTO_IPV4) {
  1450. if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1451. info->range.min_addr.ip) ||
  1452. (info->range.max_addr.ip
  1453. != info->range.min_addr.ip &&
  1454. (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1455. info->range.max_addr.ip))))
  1456. return false;
  1457. } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
  1458. info->family == NFPROTO_IPV6) {
  1459. if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
  1460. &info->range.min_addr.in6) ||
  1461. (memcmp(&info->range.max_addr.in6,
  1462. &info->range.min_addr.in6,
  1463. sizeof(info->range.max_addr.in6)) &&
  1464. (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
  1465. &info->range.max_addr.in6))))
  1466. return false;
  1467. } else {
  1468. return false;
  1469. }
  1470. }
  1471. if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
  1472. (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
  1473. ntohs(info->range.min_proto.all)) ||
  1474. (info->range.max_proto.all != info->range.min_proto.all &&
  1475. nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
  1476. ntohs(info->range.max_proto.all)))))
  1477. return false;
  1478. if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
  1479. nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
  1480. return false;
  1481. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
  1482. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
  1483. return false;
  1484. if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
  1485. nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
  1486. return false;
  1487. out:
  1488. nla_nest_end(skb, start);
  1489. return true;
  1490. }
  1491. #endif
  1492. int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
  1493. struct sk_buff *skb)
  1494. {
  1495. struct nlattr *start;
  1496. start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
  1497. if (!start)
  1498. return -EMSGSIZE;
  1499. if (ct_info->commit && nla_put_flag(skb, ct_info->force
  1500. ? OVS_CT_ATTR_FORCE_COMMIT
  1501. : OVS_CT_ATTR_COMMIT))
  1502. return -EMSGSIZE;
  1503. if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
  1504. nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
  1505. return -EMSGSIZE;
  1506. if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
  1507. nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
  1508. &ct_info->mark))
  1509. return -EMSGSIZE;
  1510. if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
  1511. labels_nonzero(&ct_info->labels.mask) &&
  1512. nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
  1513. &ct_info->labels))
  1514. return -EMSGSIZE;
  1515. if (ct_info->helper) {
  1516. if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
  1517. ct_info->helper->name))
  1518. return -EMSGSIZE;
  1519. }
  1520. if (ct_info->have_eventmask &&
  1521. nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
  1522. return -EMSGSIZE;
  1523. #ifdef CONFIG_NF_NAT_NEEDED
  1524. if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
  1525. return -EMSGSIZE;
  1526. #endif
  1527. nla_nest_end(skb, start);
  1528. return 0;
  1529. }
  1530. void ovs_ct_free_action(const struct nlattr *a)
  1531. {
  1532. struct ovs_conntrack_info *ct_info = nla_data(a);
  1533. __ovs_ct_free_action(ct_info);
  1534. }
  1535. static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
  1536. {
  1537. if (ct_info->helper)
  1538. nf_conntrack_helper_put(ct_info->helper);
  1539. if (ct_info->ct)
  1540. nf_ct_tmpl_free(ct_info->ct);
  1541. }
  1542. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1543. static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
  1544. {
  1545. int i, err;
  1546. ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
  1547. GFP_KERNEL);
  1548. if (!ovs_net->ct_limit_info)
  1549. return -ENOMEM;
  1550. ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
  1551. ovs_net->ct_limit_info->limits =
  1552. kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
  1553. GFP_KERNEL);
  1554. if (!ovs_net->ct_limit_info->limits) {
  1555. kfree(ovs_net->ct_limit_info);
  1556. return -ENOMEM;
  1557. }
  1558. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
  1559. INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
  1560. ovs_net->ct_limit_info->data =
  1561. nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
  1562. if (IS_ERR(ovs_net->ct_limit_info->data)) {
  1563. err = PTR_ERR(ovs_net->ct_limit_info->data);
  1564. kfree(ovs_net->ct_limit_info->limits);
  1565. kfree(ovs_net->ct_limit_info);
  1566. pr_err("openvswitch: failed to init nf_conncount %d\n", err);
  1567. return err;
  1568. }
  1569. return 0;
  1570. }
  1571. static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
  1572. {
  1573. const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
  1574. int i;
  1575. nf_conncount_destroy(net, NFPROTO_INET, info->data);
  1576. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
  1577. struct hlist_head *head = &info->limits[i];
  1578. struct ovs_ct_limit *ct_limit;
  1579. hlist_for_each_entry_rcu(ct_limit, head, hlist_node)
  1580. kfree_rcu(ct_limit, rcu);
  1581. }
  1582. kfree(ovs_net->ct_limit_info->limits);
  1583. kfree(ovs_net->ct_limit_info);
  1584. }
  1585. static struct sk_buff *
  1586. ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
  1587. struct ovs_header **ovs_reply_header)
  1588. {
  1589. struct ovs_header *ovs_header = info->userhdr;
  1590. struct sk_buff *skb;
  1591. skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1592. if (!skb)
  1593. return ERR_PTR(-ENOMEM);
  1594. *ovs_reply_header = genlmsg_put(skb, info->snd_portid,
  1595. info->snd_seq,
  1596. &dp_ct_limit_genl_family, 0, cmd);
  1597. if (!*ovs_reply_header) {
  1598. nlmsg_free(skb);
  1599. return ERR_PTR(-EMSGSIZE);
  1600. }
  1601. (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
  1602. return skb;
  1603. }
  1604. static bool check_zone_id(int zone_id, u16 *pzone)
  1605. {
  1606. if (zone_id >= 0 && zone_id <= 65535) {
  1607. *pzone = (u16)zone_id;
  1608. return true;
  1609. }
  1610. return false;
  1611. }
  1612. static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
  1613. struct ovs_ct_limit_info *info)
  1614. {
  1615. struct ovs_zone_limit *zone_limit;
  1616. int rem;
  1617. u16 zone;
  1618. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1619. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1620. while (rem >= sizeof(*zone_limit)) {
  1621. if (unlikely(zone_limit->zone_id ==
  1622. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1623. ovs_lock();
  1624. info->default_limit = zone_limit->limit;
  1625. ovs_unlock();
  1626. } else if (unlikely(!check_zone_id(
  1627. zone_limit->zone_id, &zone))) {
  1628. OVS_NLERR(true, "zone id is out of range");
  1629. } else {
  1630. struct ovs_ct_limit *ct_limit;
  1631. ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
  1632. if (!ct_limit)
  1633. return -ENOMEM;
  1634. ct_limit->zone = zone;
  1635. ct_limit->limit = zone_limit->limit;
  1636. ovs_lock();
  1637. ct_limit_set(info, ct_limit);
  1638. ovs_unlock();
  1639. }
  1640. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1641. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1642. NLA_ALIGN(sizeof(*zone_limit)));
  1643. }
  1644. if (rem)
  1645. OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
  1646. return 0;
  1647. }
  1648. static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
  1649. struct ovs_ct_limit_info *info)
  1650. {
  1651. struct ovs_zone_limit *zone_limit;
  1652. int rem;
  1653. u16 zone;
  1654. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1655. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1656. while (rem >= sizeof(*zone_limit)) {
  1657. if (unlikely(zone_limit->zone_id ==
  1658. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1659. ovs_lock();
  1660. info->default_limit = OVS_CT_LIMIT_DEFAULT;
  1661. ovs_unlock();
  1662. } else if (unlikely(!check_zone_id(
  1663. zone_limit->zone_id, &zone))) {
  1664. OVS_NLERR(true, "zone id is out of range");
  1665. } else {
  1666. ovs_lock();
  1667. ct_limit_del(info, zone);
  1668. ovs_unlock();
  1669. }
  1670. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1671. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1672. NLA_ALIGN(sizeof(*zone_limit)));
  1673. }
  1674. if (rem)
  1675. OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
  1676. return 0;
  1677. }
  1678. static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
  1679. struct sk_buff *reply)
  1680. {
  1681. struct ovs_zone_limit zone_limit;
  1682. int err;
  1683. zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
  1684. zone_limit.limit = info->default_limit;
  1685. err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
  1686. if (err)
  1687. return err;
  1688. return 0;
  1689. }
  1690. static int __ovs_ct_limit_get_zone_limit(struct net *net,
  1691. struct nf_conncount_data *data,
  1692. u16 zone_id, u32 limit,
  1693. struct sk_buff *reply)
  1694. {
  1695. struct nf_conntrack_zone ct_zone;
  1696. struct ovs_zone_limit zone_limit;
  1697. u32 conncount_key = zone_id;
  1698. zone_limit.zone_id = zone_id;
  1699. zone_limit.limit = limit;
  1700. nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
  1701. zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL,
  1702. &ct_zone);
  1703. return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
  1704. }
  1705. static int ovs_ct_limit_get_zone_limit(struct net *net,
  1706. struct nlattr *nla_zone_limit,
  1707. struct ovs_ct_limit_info *info,
  1708. struct sk_buff *reply)
  1709. {
  1710. struct ovs_zone_limit *zone_limit;
  1711. int rem, err;
  1712. u32 limit;
  1713. u16 zone;
  1714. rem = NLA_ALIGN(nla_len(nla_zone_limit));
  1715. zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
  1716. while (rem >= sizeof(*zone_limit)) {
  1717. if (unlikely(zone_limit->zone_id ==
  1718. OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
  1719. err = ovs_ct_limit_get_default_limit(info, reply);
  1720. if (err)
  1721. return err;
  1722. } else if (unlikely(!check_zone_id(zone_limit->zone_id,
  1723. &zone))) {
  1724. OVS_NLERR(true, "zone id is out of range");
  1725. } else {
  1726. rcu_read_lock();
  1727. limit = ct_limit_get(info, zone);
  1728. rcu_read_unlock();
  1729. err = __ovs_ct_limit_get_zone_limit(
  1730. net, info->data, zone, limit, reply);
  1731. if (err)
  1732. return err;
  1733. }
  1734. rem -= NLA_ALIGN(sizeof(*zone_limit));
  1735. zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
  1736. NLA_ALIGN(sizeof(*zone_limit)));
  1737. }
  1738. if (rem)
  1739. OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
  1740. return 0;
  1741. }
  1742. static int ovs_ct_limit_get_all_zone_limit(struct net *net,
  1743. struct ovs_ct_limit_info *info,
  1744. struct sk_buff *reply)
  1745. {
  1746. struct ovs_ct_limit *ct_limit;
  1747. struct hlist_head *head;
  1748. int i, err = 0;
  1749. err = ovs_ct_limit_get_default_limit(info, reply);
  1750. if (err)
  1751. return err;
  1752. rcu_read_lock();
  1753. for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
  1754. head = &info->limits[i];
  1755. hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
  1756. err = __ovs_ct_limit_get_zone_limit(net, info->data,
  1757. ct_limit->zone, ct_limit->limit, reply);
  1758. if (err)
  1759. goto exit_err;
  1760. }
  1761. }
  1762. exit_err:
  1763. rcu_read_unlock();
  1764. return err;
  1765. }
  1766. static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1767. {
  1768. struct nlattr **a = info->attrs;
  1769. struct sk_buff *reply;
  1770. struct ovs_header *ovs_reply_header;
  1771. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1772. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1773. int err;
  1774. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
  1775. &ovs_reply_header);
  1776. if (IS_ERR(reply))
  1777. return PTR_ERR(reply);
  1778. if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1779. err = -EINVAL;
  1780. goto exit_err;
  1781. }
  1782. err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
  1783. ct_limit_info);
  1784. if (err)
  1785. goto exit_err;
  1786. static_branch_enable(&ovs_ct_limit_enabled);
  1787. genlmsg_end(reply, ovs_reply_header);
  1788. return genlmsg_reply(reply, info);
  1789. exit_err:
  1790. nlmsg_free(reply);
  1791. return err;
  1792. }
  1793. static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1794. {
  1795. struct nlattr **a = info->attrs;
  1796. struct sk_buff *reply;
  1797. struct ovs_header *ovs_reply_header;
  1798. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1799. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1800. int err;
  1801. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
  1802. &ovs_reply_header);
  1803. if (IS_ERR(reply))
  1804. return PTR_ERR(reply);
  1805. if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1806. err = -EINVAL;
  1807. goto exit_err;
  1808. }
  1809. err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
  1810. ct_limit_info);
  1811. if (err)
  1812. goto exit_err;
  1813. genlmsg_end(reply, ovs_reply_header);
  1814. return genlmsg_reply(reply, info);
  1815. exit_err:
  1816. nlmsg_free(reply);
  1817. return err;
  1818. }
  1819. static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1820. {
  1821. struct nlattr **a = info->attrs;
  1822. struct nlattr *nla_reply;
  1823. struct sk_buff *reply;
  1824. struct ovs_header *ovs_reply_header;
  1825. struct net *net = sock_net(skb->sk);
  1826. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1827. struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
  1828. int err;
  1829. reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
  1830. &ovs_reply_header);
  1831. if (IS_ERR(reply))
  1832. return PTR_ERR(reply);
  1833. nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
  1834. if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
  1835. err = ovs_ct_limit_get_zone_limit(
  1836. net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
  1837. reply);
  1838. if (err)
  1839. goto exit_err;
  1840. } else {
  1841. err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
  1842. reply);
  1843. if (err)
  1844. goto exit_err;
  1845. }
  1846. nla_nest_end(reply, nla_reply);
  1847. genlmsg_end(reply, ovs_reply_header);
  1848. return genlmsg_reply(reply, info);
  1849. exit_err:
  1850. nlmsg_free(reply);
  1851. return err;
  1852. }
  1853. static struct genl_ops ct_limit_genl_ops[] = {
  1854. { .cmd = OVS_CT_LIMIT_CMD_SET,
  1855. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
  1856. * privilege. */
  1857. .policy = ct_limit_policy,
  1858. .doit = ovs_ct_limit_cmd_set,
  1859. },
  1860. { .cmd = OVS_CT_LIMIT_CMD_DEL,
  1861. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
  1862. * privilege. */
  1863. .policy = ct_limit_policy,
  1864. .doit = ovs_ct_limit_cmd_del,
  1865. },
  1866. { .cmd = OVS_CT_LIMIT_CMD_GET,
  1867. .flags = 0, /* OK for unprivileged users. */
  1868. .policy = ct_limit_policy,
  1869. .doit = ovs_ct_limit_cmd_get,
  1870. },
  1871. };
  1872. static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
  1873. .name = OVS_CT_LIMIT_MCGROUP,
  1874. };
  1875. struct genl_family dp_ct_limit_genl_family __ro_after_init = {
  1876. .hdrsize = sizeof(struct ovs_header),
  1877. .name = OVS_CT_LIMIT_FAMILY,
  1878. .version = OVS_CT_LIMIT_VERSION,
  1879. .maxattr = OVS_CT_LIMIT_ATTR_MAX,
  1880. .netnsok = true,
  1881. .parallel_ops = true,
  1882. .ops = ct_limit_genl_ops,
  1883. .n_ops = ARRAY_SIZE(ct_limit_genl_ops),
  1884. .mcgrps = &ovs_ct_limit_multicast_group,
  1885. .n_mcgrps = 1,
  1886. .module = THIS_MODULE,
  1887. };
  1888. #endif
  1889. int ovs_ct_init(struct net *net)
  1890. {
  1891. unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
  1892. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1893. if (nf_connlabels_get(net, n_bits - 1)) {
  1894. ovs_net->xt_label = false;
  1895. OVS_NLERR(true, "Failed to set connlabel length");
  1896. } else {
  1897. ovs_net->xt_label = true;
  1898. }
  1899. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1900. return ovs_ct_limit_init(net, ovs_net);
  1901. #else
  1902. return 0;
  1903. #endif
  1904. }
  1905. void ovs_ct_exit(struct net *net)
  1906. {
  1907. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1908. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  1909. ovs_ct_limit_exit(net, ovs_net);
  1910. #endif
  1911. if (ovs_net->xt_label)
  1912. nf_connlabels_put(net);
  1913. }