conntrack.c 61 KB

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