conntrack.c 52 KB

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