conntrack.c 58 KB

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