conntrack.c 58 KB

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