flow_netlink.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. /*
  2. * Copyright (c) 2007-2014 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. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "flow.h"
  20. #include "datapath.h"
  21. #include <linux/uaccess.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <net/llc_pdu.h>
  27. #include <linux/kernel.h>
  28. #include <linux/jhash.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/llc.h>
  31. #include <linux/module.h>
  32. #include <linux/in.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/ip.h>
  36. #include <linux/ipv6.h>
  37. #include <linux/sctp.h>
  38. #include <linux/tcp.h>
  39. #include <linux/udp.h>
  40. #include <linux/icmp.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/rculist.h>
  43. #include <net/geneve.h>
  44. #include <net/ip.h>
  45. #include <net/ipv6.h>
  46. #include <net/ndisc.h>
  47. #include <net/mpls.h>
  48. #include <net/vxlan.h>
  49. #include "flow_netlink.h"
  50. struct ovs_len_tbl {
  51. int len;
  52. const struct ovs_len_tbl *next;
  53. };
  54. #define OVS_ATTR_NESTED -1
  55. #define OVS_ATTR_VARIABLE -2
  56. static void update_range(struct sw_flow_match *match,
  57. size_t offset, size_t size, bool is_mask)
  58. {
  59. struct sw_flow_key_range *range;
  60. size_t start = rounddown(offset, sizeof(long));
  61. size_t end = roundup(offset + size, sizeof(long));
  62. if (!is_mask)
  63. range = &match->range;
  64. else
  65. range = &match->mask->range;
  66. if (range->start == range->end) {
  67. range->start = start;
  68. range->end = end;
  69. return;
  70. }
  71. if (range->start > start)
  72. range->start = start;
  73. if (range->end < end)
  74. range->end = end;
  75. }
  76. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  77. do { \
  78. update_range(match, offsetof(struct sw_flow_key, field), \
  79. sizeof((match)->key->field), is_mask); \
  80. if (is_mask) \
  81. (match)->mask->key.field = value; \
  82. else \
  83. (match)->key->field = value; \
  84. } while (0)
  85. #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
  86. do { \
  87. update_range(match, offset, len, is_mask); \
  88. if (is_mask) \
  89. memcpy((u8 *)&(match)->mask->key + offset, value_p, \
  90. len); \
  91. else \
  92. memcpy((u8 *)(match)->key + offset, value_p, len); \
  93. } while (0)
  94. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  95. SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
  96. value_p, len, is_mask)
  97. #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
  98. do { \
  99. update_range(match, offsetof(struct sw_flow_key, field), \
  100. sizeof((match)->key->field), is_mask); \
  101. if (is_mask) \
  102. memset((u8 *)&(match)->mask->key.field, value, \
  103. sizeof((match)->mask->key.field)); \
  104. else \
  105. memset((u8 *)&(match)->key->field, value, \
  106. sizeof((match)->key->field)); \
  107. } while (0)
  108. static bool match_validate(const struct sw_flow_match *match,
  109. u64 key_attrs, u64 mask_attrs, bool log)
  110. {
  111. u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
  112. u64 mask_allowed = key_attrs; /* At most allow all key attributes */
  113. /* The following mask attributes allowed only if they
  114. * pass the validation tests. */
  115. mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
  116. | (1 << OVS_KEY_ATTR_IPV6)
  117. | (1 << OVS_KEY_ATTR_TCP)
  118. | (1 << OVS_KEY_ATTR_TCP_FLAGS)
  119. | (1 << OVS_KEY_ATTR_UDP)
  120. | (1 << OVS_KEY_ATTR_SCTP)
  121. | (1 << OVS_KEY_ATTR_ICMP)
  122. | (1 << OVS_KEY_ATTR_ICMPV6)
  123. | (1 << OVS_KEY_ATTR_ARP)
  124. | (1 << OVS_KEY_ATTR_ND)
  125. | (1 << OVS_KEY_ATTR_MPLS));
  126. /* Always allowed mask fields. */
  127. mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
  128. | (1 << OVS_KEY_ATTR_IN_PORT)
  129. | (1 << OVS_KEY_ATTR_ETHERTYPE));
  130. /* Check key attributes. */
  131. if (match->key->eth.type == htons(ETH_P_ARP)
  132. || match->key->eth.type == htons(ETH_P_RARP)) {
  133. key_expected |= 1 << OVS_KEY_ATTR_ARP;
  134. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  135. mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
  136. }
  137. if (eth_p_mpls(match->key->eth.type)) {
  138. key_expected |= 1 << OVS_KEY_ATTR_MPLS;
  139. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  140. mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
  141. }
  142. if (match->key->eth.type == htons(ETH_P_IP)) {
  143. key_expected |= 1 << OVS_KEY_ATTR_IPV4;
  144. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  145. mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
  146. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  147. if (match->key->ip.proto == IPPROTO_UDP) {
  148. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  149. if (match->mask && (match->mask->key.ip.proto == 0xff))
  150. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  151. }
  152. if (match->key->ip.proto == IPPROTO_SCTP) {
  153. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  154. if (match->mask && (match->mask->key.ip.proto == 0xff))
  155. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  156. }
  157. if (match->key->ip.proto == IPPROTO_TCP) {
  158. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  159. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  160. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  161. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  162. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  163. }
  164. }
  165. if (match->key->ip.proto == IPPROTO_ICMP) {
  166. key_expected |= 1 << OVS_KEY_ATTR_ICMP;
  167. if (match->mask && (match->mask->key.ip.proto == 0xff))
  168. mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
  169. }
  170. }
  171. }
  172. if (match->key->eth.type == htons(ETH_P_IPV6)) {
  173. key_expected |= 1 << OVS_KEY_ATTR_IPV6;
  174. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  175. mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
  176. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  177. if (match->key->ip.proto == IPPROTO_UDP) {
  178. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  179. if (match->mask && (match->mask->key.ip.proto == 0xff))
  180. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  181. }
  182. if (match->key->ip.proto == IPPROTO_SCTP) {
  183. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  184. if (match->mask && (match->mask->key.ip.proto == 0xff))
  185. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  186. }
  187. if (match->key->ip.proto == IPPROTO_TCP) {
  188. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  189. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  190. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  191. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  192. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  193. }
  194. }
  195. if (match->key->ip.proto == IPPROTO_ICMPV6) {
  196. key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
  197. if (match->mask && (match->mask->key.ip.proto == 0xff))
  198. mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
  199. if (match->key->tp.src ==
  200. htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  201. match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  202. key_expected |= 1 << OVS_KEY_ATTR_ND;
  203. if (match->mask && (match->mask->key.tp.src == htons(0xff)))
  204. mask_allowed |= 1 << OVS_KEY_ATTR_ND;
  205. }
  206. }
  207. }
  208. }
  209. if ((key_attrs & key_expected) != key_expected) {
  210. /* Key attributes check failed. */
  211. OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
  212. (unsigned long long)key_attrs,
  213. (unsigned long long)key_expected);
  214. return false;
  215. }
  216. if ((mask_attrs & mask_allowed) != mask_attrs) {
  217. /* Mask attributes check failed. */
  218. OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
  219. (unsigned long long)mask_attrs,
  220. (unsigned long long)mask_allowed);
  221. return false;
  222. }
  223. return true;
  224. }
  225. size_t ovs_tun_key_attr_size(void)
  226. {
  227. /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
  228. * updating this function.
  229. */
  230. return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  231. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  232. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  233. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  234. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  235. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  236. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  237. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  238. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  239. /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
  240. * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
  241. */
  242. + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
  243. + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
  244. }
  245. size_t ovs_key_attr_size(void)
  246. {
  247. /* Whenever adding new OVS_KEY_ FIELDS, we should consider
  248. * updating this function.
  249. */
  250. BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
  251. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  252. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  253. + ovs_tun_key_attr_size()
  254. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  255. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  256. + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
  257. + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
  258. + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
  259. + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
  260. + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
  261. + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
  262. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  263. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  264. + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
  265. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  266. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  267. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  268. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  269. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  270. }
  271. static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
  272. [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
  273. };
  274. static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  275. [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
  276. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
  277. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
  278. [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
  279. [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
  280. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
  281. [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
  282. [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
  283. [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
  284. [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
  285. [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
  286. [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
  287. .next = ovs_vxlan_ext_key_lens },
  288. };
  289. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  290. static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  291. [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
  292. [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
  293. [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
  294. [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
  295. [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
  296. [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
  297. [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
  298. [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
  299. [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
  300. [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
  301. [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
  302. [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
  303. [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
  304. [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
  305. [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
  306. [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
  307. [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
  308. [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
  309. [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
  310. [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
  311. .next = ovs_tunnel_key_lens, },
  312. [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
  313. [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
  314. [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
  315. [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
  316. [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
  317. };
  318. static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
  319. {
  320. return expected_len == attr_len ||
  321. expected_len == OVS_ATTR_NESTED ||
  322. expected_len == OVS_ATTR_VARIABLE;
  323. }
  324. static bool is_all_zero(const u8 *fp, size_t size)
  325. {
  326. int i;
  327. if (!fp)
  328. return false;
  329. for (i = 0; i < size; i++)
  330. if (fp[i])
  331. return false;
  332. return true;
  333. }
  334. static int __parse_flow_nlattrs(const struct nlattr *attr,
  335. const struct nlattr *a[],
  336. u64 *attrsp, bool log, bool nz)
  337. {
  338. const struct nlattr *nla;
  339. u64 attrs;
  340. int rem;
  341. attrs = *attrsp;
  342. nla_for_each_nested(nla, attr, rem) {
  343. u16 type = nla_type(nla);
  344. int expected_len;
  345. if (type > OVS_KEY_ATTR_MAX) {
  346. OVS_NLERR(log, "Key type %d is out of range max %d",
  347. type, OVS_KEY_ATTR_MAX);
  348. return -EINVAL;
  349. }
  350. if (attrs & (1 << type)) {
  351. OVS_NLERR(log, "Duplicate key (type %d).", type);
  352. return -EINVAL;
  353. }
  354. expected_len = ovs_key_lens[type].len;
  355. if (!check_attr_len(nla_len(nla), expected_len)) {
  356. OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
  357. type, nla_len(nla), expected_len);
  358. return -EINVAL;
  359. }
  360. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  361. attrs |= 1 << type;
  362. a[type] = nla;
  363. }
  364. }
  365. if (rem) {
  366. OVS_NLERR(log, "Message has %d unknown bytes.", rem);
  367. return -EINVAL;
  368. }
  369. *attrsp = attrs;
  370. return 0;
  371. }
  372. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  373. const struct nlattr *a[], u64 *attrsp,
  374. bool log)
  375. {
  376. return __parse_flow_nlattrs(attr, a, attrsp, log, true);
  377. }
  378. static int parse_flow_nlattrs(const struct nlattr *attr,
  379. const struct nlattr *a[], u64 *attrsp,
  380. bool log)
  381. {
  382. return __parse_flow_nlattrs(attr, a, attrsp, log, false);
  383. }
  384. static int genev_tun_opt_from_nlattr(const struct nlattr *a,
  385. struct sw_flow_match *match, bool is_mask,
  386. bool log)
  387. {
  388. unsigned long opt_key_offset;
  389. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  390. OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
  391. nla_len(a), sizeof(match->key->tun_opts));
  392. return -EINVAL;
  393. }
  394. if (nla_len(a) % 4 != 0) {
  395. OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
  396. nla_len(a));
  397. return -EINVAL;
  398. }
  399. /* We need to record the length of the options passed
  400. * down, otherwise packets with the same format but
  401. * additional options will be silently matched.
  402. */
  403. if (!is_mask) {
  404. SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
  405. false);
  406. } else {
  407. /* This is somewhat unusual because it looks at
  408. * both the key and mask while parsing the
  409. * attributes (and by extension assumes the key
  410. * is parsed first). Normally, we would verify
  411. * that each is the correct length and that the
  412. * attributes line up in the validate function.
  413. * However, that is difficult because this is
  414. * variable length and we won't have the
  415. * information later.
  416. */
  417. if (match->key->tun_opts_len != nla_len(a)) {
  418. OVS_NLERR(log, "Geneve option len %d != mask len %d",
  419. match->key->tun_opts_len, nla_len(a));
  420. return -EINVAL;
  421. }
  422. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  423. }
  424. opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
  425. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
  426. nla_len(a), is_mask);
  427. return 0;
  428. }
  429. static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
  430. struct sw_flow_match *match, bool is_mask,
  431. bool log)
  432. {
  433. struct nlattr *a;
  434. int rem;
  435. unsigned long opt_key_offset;
  436. struct vxlan_metadata opts;
  437. BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
  438. memset(&opts, 0, sizeof(opts));
  439. nla_for_each_nested(a, attr, rem) {
  440. int type = nla_type(a);
  441. if (type > OVS_VXLAN_EXT_MAX) {
  442. OVS_NLERR(log, "VXLAN extension %d out of range max %d",
  443. type, OVS_VXLAN_EXT_MAX);
  444. return -EINVAL;
  445. }
  446. if (!check_attr_len(nla_len(a),
  447. ovs_vxlan_ext_key_lens[type].len)) {
  448. OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
  449. type, nla_len(a),
  450. ovs_vxlan_ext_key_lens[type].len);
  451. return -EINVAL;
  452. }
  453. switch (type) {
  454. case OVS_VXLAN_EXT_GBP:
  455. opts.gbp = nla_get_u32(a);
  456. break;
  457. default:
  458. OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
  459. type);
  460. return -EINVAL;
  461. }
  462. }
  463. if (rem) {
  464. OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
  465. rem);
  466. return -EINVAL;
  467. }
  468. if (!is_mask)
  469. SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
  470. else
  471. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  472. opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
  473. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
  474. is_mask);
  475. return 0;
  476. }
  477. static int ipv4_tun_from_nlattr(const struct nlattr *attr,
  478. struct sw_flow_match *match, bool is_mask,
  479. bool log)
  480. {
  481. struct nlattr *a;
  482. int rem;
  483. bool ttl = false;
  484. __be16 tun_flags = 0;
  485. int opts_type = 0;
  486. nla_for_each_nested(a, attr, rem) {
  487. int type = nla_type(a);
  488. int err;
  489. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  490. OVS_NLERR(log, "Tunnel attr %d out of range max %d",
  491. type, OVS_TUNNEL_KEY_ATTR_MAX);
  492. return -EINVAL;
  493. }
  494. if (!check_attr_len(nla_len(a),
  495. ovs_tunnel_key_lens[type].len)) {
  496. OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
  497. type, nla_len(a), ovs_tunnel_key_lens[type].len);
  498. return -EINVAL;
  499. }
  500. switch (type) {
  501. case OVS_TUNNEL_KEY_ATTR_ID:
  502. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  503. nla_get_be64(a), is_mask);
  504. tun_flags |= TUNNEL_KEY;
  505. break;
  506. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  507. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
  508. nla_get_in_addr(a), is_mask);
  509. break;
  510. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  511. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
  512. nla_get_in_addr(a), is_mask);
  513. break;
  514. case OVS_TUNNEL_KEY_ATTR_TOS:
  515. SW_FLOW_KEY_PUT(match, tun_key.tos,
  516. nla_get_u8(a), is_mask);
  517. break;
  518. case OVS_TUNNEL_KEY_ATTR_TTL:
  519. SW_FLOW_KEY_PUT(match, tun_key.ttl,
  520. nla_get_u8(a), is_mask);
  521. ttl = true;
  522. break;
  523. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  524. tun_flags |= TUNNEL_DONT_FRAGMENT;
  525. break;
  526. case OVS_TUNNEL_KEY_ATTR_CSUM:
  527. tun_flags |= TUNNEL_CSUM;
  528. break;
  529. case OVS_TUNNEL_KEY_ATTR_TP_SRC:
  530. SW_FLOW_KEY_PUT(match, tun_key.tp_src,
  531. nla_get_be16(a), is_mask);
  532. break;
  533. case OVS_TUNNEL_KEY_ATTR_TP_DST:
  534. SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
  535. nla_get_be16(a), is_mask);
  536. break;
  537. case OVS_TUNNEL_KEY_ATTR_OAM:
  538. tun_flags |= TUNNEL_OAM;
  539. break;
  540. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  541. if (opts_type) {
  542. OVS_NLERR(log, "Multiple metadata blocks provided");
  543. return -EINVAL;
  544. }
  545. err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
  546. if (err)
  547. return err;
  548. tun_flags |= TUNNEL_GENEVE_OPT;
  549. opts_type = type;
  550. break;
  551. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  552. if (opts_type) {
  553. OVS_NLERR(log, "Multiple metadata blocks provided");
  554. return -EINVAL;
  555. }
  556. err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
  557. if (err)
  558. return err;
  559. tun_flags |= TUNNEL_VXLAN_OPT;
  560. opts_type = type;
  561. break;
  562. default:
  563. OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
  564. type);
  565. return -EINVAL;
  566. }
  567. }
  568. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  569. if (rem > 0) {
  570. OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
  571. rem);
  572. return -EINVAL;
  573. }
  574. if (!is_mask) {
  575. if (!match->key->tun_key.u.ipv4.dst) {
  576. OVS_NLERR(log, "IPv4 tunnel dst address is zero");
  577. return -EINVAL;
  578. }
  579. if (!ttl) {
  580. OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
  581. return -EINVAL;
  582. }
  583. }
  584. return opts_type;
  585. }
  586. static int vxlan_opt_to_nlattr(struct sk_buff *skb,
  587. const void *tun_opts, int swkey_tun_opts_len)
  588. {
  589. const struct vxlan_metadata *opts = tun_opts;
  590. struct nlattr *nla;
  591. nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
  592. if (!nla)
  593. return -EMSGSIZE;
  594. if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
  595. return -EMSGSIZE;
  596. nla_nest_end(skb, nla);
  597. return 0;
  598. }
  599. static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
  600. const struct ip_tunnel_key *output,
  601. const void *tun_opts, int swkey_tun_opts_len)
  602. {
  603. if (output->tun_flags & TUNNEL_KEY &&
  604. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  605. return -EMSGSIZE;
  606. if (output->u.ipv4.src &&
  607. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
  608. output->u.ipv4.src))
  609. return -EMSGSIZE;
  610. if (output->u.ipv4.dst &&
  611. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
  612. output->u.ipv4.dst))
  613. return -EMSGSIZE;
  614. if (output->tos &&
  615. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
  616. return -EMSGSIZE;
  617. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
  618. return -EMSGSIZE;
  619. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  620. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  621. return -EMSGSIZE;
  622. if ((output->tun_flags & TUNNEL_CSUM) &&
  623. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  624. return -EMSGSIZE;
  625. if (output->tp_src &&
  626. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
  627. return -EMSGSIZE;
  628. if (output->tp_dst &&
  629. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
  630. return -EMSGSIZE;
  631. if ((output->tun_flags & TUNNEL_OAM) &&
  632. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  633. return -EMSGSIZE;
  634. if (tun_opts) {
  635. if (output->tun_flags & TUNNEL_GENEVE_OPT &&
  636. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  637. swkey_tun_opts_len, tun_opts))
  638. return -EMSGSIZE;
  639. else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
  640. vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
  641. return -EMSGSIZE;
  642. }
  643. return 0;
  644. }
  645. static int ipv4_tun_to_nlattr(struct sk_buff *skb,
  646. const struct ip_tunnel_key *output,
  647. const void *tun_opts, int swkey_tun_opts_len)
  648. {
  649. struct nlattr *nla;
  650. int err;
  651. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  652. if (!nla)
  653. return -EMSGSIZE;
  654. err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
  655. if (err)
  656. return err;
  657. nla_nest_end(skb, nla);
  658. return 0;
  659. }
  660. int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
  661. const struct ip_tunnel_info *egress_tun_info,
  662. const void *egress_tun_opts)
  663. {
  664. return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
  665. egress_tun_opts,
  666. egress_tun_info->options_len);
  667. }
  668. static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
  669. u64 *attrs, const struct nlattr **a,
  670. bool is_mask, bool log)
  671. {
  672. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  673. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  674. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  675. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  676. }
  677. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  678. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  679. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  680. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  681. }
  682. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  683. SW_FLOW_KEY_PUT(match, phy.priority,
  684. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  685. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  686. }
  687. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  688. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  689. if (is_mask) {
  690. in_port = 0xffffffff; /* Always exact match in_port. */
  691. } else if (in_port >= DP_MAX_PORTS) {
  692. OVS_NLERR(log, "Port %d exceeds max allowable %d",
  693. in_port, DP_MAX_PORTS);
  694. return -EINVAL;
  695. }
  696. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  697. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  698. } else if (!is_mask) {
  699. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  700. }
  701. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  702. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  703. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  704. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  705. }
  706. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  707. if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  708. is_mask, log) < 0)
  709. return -EINVAL;
  710. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  711. }
  712. if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
  713. ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
  714. u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
  715. if (!is_mask && !ovs_ct_state_supported(ct_state)) {
  716. OVS_NLERR(log, "ct_state flags %08x unsupported",
  717. ct_state);
  718. return -EINVAL;
  719. }
  720. SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
  721. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
  722. }
  723. if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
  724. ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
  725. u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
  726. SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
  727. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
  728. }
  729. if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
  730. ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
  731. u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
  732. SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
  733. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
  734. }
  735. if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
  736. ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
  737. const struct ovs_key_ct_labels *cl;
  738. cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
  739. SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
  740. sizeof(*cl), is_mask);
  741. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
  742. }
  743. return 0;
  744. }
  745. static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
  746. u64 attrs, const struct nlattr **a,
  747. bool is_mask, bool log)
  748. {
  749. int err;
  750. err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
  751. if (err)
  752. return err;
  753. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  754. const struct ovs_key_ethernet *eth_key;
  755. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  756. SW_FLOW_KEY_MEMCPY(match, eth.src,
  757. eth_key->eth_src, ETH_ALEN, is_mask);
  758. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  759. eth_key->eth_dst, ETH_ALEN, is_mask);
  760. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  761. }
  762. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  763. __be16 tci;
  764. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  765. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  766. if (is_mask)
  767. OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
  768. else
  769. OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
  770. return -EINVAL;
  771. }
  772. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  773. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  774. }
  775. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  776. __be16 eth_type;
  777. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  778. if (is_mask) {
  779. /* Always exact match EtherType. */
  780. eth_type = htons(0xffff);
  781. } else if (!eth_proto_is_802_3(eth_type)) {
  782. OVS_NLERR(log, "EtherType %x is less than min %x",
  783. ntohs(eth_type), ETH_P_802_3_MIN);
  784. return -EINVAL;
  785. }
  786. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  787. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  788. } else if (!is_mask) {
  789. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  790. }
  791. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  792. const struct ovs_key_ipv4 *ipv4_key;
  793. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  794. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  795. OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
  796. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  797. return -EINVAL;
  798. }
  799. SW_FLOW_KEY_PUT(match, ip.proto,
  800. ipv4_key->ipv4_proto, is_mask);
  801. SW_FLOW_KEY_PUT(match, ip.tos,
  802. ipv4_key->ipv4_tos, is_mask);
  803. SW_FLOW_KEY_PUT(match, ip.ttl,
  804. ipv4_key->ipv4_ttl, is_mask);
  805. SW_FLOW_KEY_PUT(match, ip.frag,
  806. ipv4_key->ipv4_frag, is_mask);
  807. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  808. ipv4_key->ipv4_src, is_mask);
  809. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  810. ipv4_key->ipv4_dst, is_mask);
  811. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  812. }
  813. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  814. const struct ovs_key_ipv6 *ipv6_key;
  815. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  816. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  817. OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
  818. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  819. return -EINVAL;
  820. }
  821. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  822. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
  823. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  824. return -EINVAL;
  825. }
  826. SW_FLOW_KEY_PUT(match, ipv6.label,
  827. ipv6_key->ipv6_label, is_mask);
  828. SW_FLOW_KEY_PUT(match, ip.proto,
  829. ipv6_key->ipv6_proto, is_mask);
  830. SW_FLOW_KEY_PUT(match, ip.tos,
  831. ipv6_key->ipv6_tclass, is_mask);
  832. SW_FLOW_KEY_PUT(match, ip.ttl,
  833. ipv6_key->ipv6_hlimit, is_mask);
  834. SW_FLOW_KEY_PUT(match, ip.frag,
  835. ipv6_key->ipv6_frag, is_mask);
  836. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  837. ipv6_key->ipv6_src,
  838. sizeof(match->key->ipv6.addr.src),
  839. is_mask);
  840. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  841. ipv6_key->ipv6_dst,
  842. sizeof(match->key->ipv6.addr.dst),
  843. is_mask);
  844. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  845. }
  846. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  847. const struct ovs_key_arp *arp_key;
  848. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  849. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  850. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  851. arp_key->arp_op);
  852. return -EINVAL;
  853. }
  854. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  855. arp_key->arp_sip, is_mask);
  856. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  857. arp_key->arp_tip, is_mask);
  858. SW_FLOW_KEY_PUT(match, ip.proto,
  859. ntohs(arp_key->arp_op), is_mask);
  860. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  861. arp_key->arp_sha, ETH_ALEN, is_mask);
  862. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  863. arp_key->arp_tha, ETH_ALEN, is_mask);
  864. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  865. }
  866. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  867. const struct ovs_key_mpls *mpls_key;
  868. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  869. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  870. mpls_key->mpls_lse, is_mask);
  871. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  872. }
  873. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  874. const struct ovs_key_tcp *tcp_key;
  875. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  876. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  877. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  878. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  879. }
  880. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  881. SW_FLOW_KEY_PUT(match, tp.flags,
  882. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  883. is_mask);
  884. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  885. }
  886. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  887. const struct ovs_key_udp *udp_key;
  888. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  889. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  890. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  891. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  892. }
  893. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  894. const struct ovs_key_sctp *sctp_key;
  895. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  896. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  897. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  898. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  899. }
  900. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  901. const struct ovs_key_icmp *icmp_key;
  902. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  903. SW_FLOW_KEY_PUT(match, tp.src,
  904. htons(icmp_key->icmp_type), is_mask);
  905. SW_FLOW_KEY_PUT(match, tp.dst,
  906. htons(icmp_key->icmp_code), is_mask);
  907. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  908. }
  909. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  910. const struct ovs_key_icmpv6 *icmpv6_key;
  911. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  912. SW_FLOW_KEY_PUT(match, tp.src,
  913. htons(icmpv6_key->icmpv6_type), is_mask);
  914. SW_FLOW_KEY_PUT(match, tp.dst,
  915. htons(icmpv6_key->icmpv6_code), is_mask);
  916. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  917. }
  918. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  919. const struct ovs_key_nd *nd_key;
  920. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  921. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  922. nd_key->nd_target,
  923. sizeof(match->key->ipv6.nd.target),
  924. is_mask);
  925. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  926. nd_key->nd_sll, ETH_ALEN, is_mask);
  927. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  928. nd_key->nd_tll, ETH_ALEN, is_mask);
  929. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  930. }
  931. if (attrs != 0) {
  932. OVS_NLERR(log, "Unknown key attributes %llx",
  933. (unsigned long long)attrs);
  934. return -EINVAL;
  935. }
  936. return 0;
  937. }
  938. static void nlattr_set(struct nlattr *attr, u8 val,
  939. const struct ovs_len_tbl *tbl)
  940. {
  941. struct nlattr *nla;
  942. int rem;
  943. /* The nlattr stream should already have been validated */
  944. nla_for_each_nested(nla, attr, rem) {
  945. if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
  946. if (tbl[nla_type(nla)].next)
  947. tbl = tbl[nla_type(nla)].next;
  948. nlattr_set(nla, val, tbl);
  949. } else {
  950. memset(nla_data(nla), val, nla_len(nla));
  951. }
  952. }
  953. }
  954. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  955. {
  956. nlattr_set(attr, val, ovs_key_lens);
  957. }
  958. /**
  959. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  960. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  961. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  962. * does not include any don't care bit.
  963. * @net: Used to determine per-namespace field support.
  964. * @match: receives the extracted flow match information.
  965. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  966. * sequence. The fields should of the packet that triggered the creation
  967. * of this flow.
  968. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  969. * attribute specifies the mask field of the wildcarded flow.
  970. * @log: Boolean to allow kernel error logging. Normally true, but when
  971. * probing for feature compatibility this should be passed in as false to
  972. * suppress unnecessary error logging.
  973. */
  974. int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
  975. const struct nlattr *nla_key,
  976. const struct nlattr *nla_mask,
  977. bool log)
  978. {
  979. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  980. const struct nlattr *encap;
  981. struct nlattr *newmask = NULL;
  982. u64 key_attrs = 0;
  983. u64 mask_attrs = 0;
  984. bool encap_valid = false;
  985. int err;
  986. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  987. if (err)
  988. return err;
  989. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  990. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  991. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  992. __be16 tci;
  993. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  994. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  995. OVS_NLERR(log, "Invalid Vlan frame.");
  996. return -EINVAL;
  997. }
  998. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  999. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1000. encap = a[OVS_KEY_ATTR_ENCAP];
  1001. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1002. encap_valid = true;
  1003. if (tci & htons(VLAN_TAG_PRESENT)) {
  1004. err = parse_flow_nlattrs(encap, a, &key_attrs, log);
  1005. if (err)
  1006. return err;
  1007. } else if (!tci) {
  1008. /* Corner case for truncated 802.1Q header. */
  1009. if (nla_len(encap)) {
  1010. OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
  1011. return -EINVAL;
  1012. }
  1013. } else {
  1014. OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
  1015. return -EINVAL;
  1016. }
  1017. }
  1018. err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
  1019. if (err)
  1020. return err;
  1021. if (match->mask) {
  1022. if (!nla_mask) {
  1023. /* Create an exact match mask. We need to set to 0xff
  1024. * all the 'match->mask' fields that have been touched
  1025. * in 'match->key'. We cannot simply memset
  1026. * 'match->mask', because padding bytes and fields not
  1027. * specified in 'match->key' should be left to 0.
  1028. * Instead, we use a stream of netlink attributes,
  1029. * copied from 'key' and set to 0xff.
  1030. * ovs_key_from_nlattrs() will take care of filling
  1031. * 'match->mask' appropriately.
  1032. */
  1033. newmask = kmemdup(nla_key,
  1034. nla_total_size(nla_len(nla_key)),
  1035. GFP_KERNEL);
  1036. if (!newmask)
  1037. return -ENOMEM;
  1038. mask_set_nlattr(newmask, 0xff);
  1039. /* The userspace does not send tunnel attributes that
  1040. * are 0, but we should not wildcard them nonetheless.
  1041. */
  1042. if (match->key->tun_key.u.ipv4.dst)
  1043. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  1044. 0xff, true);
  1045. nla_mask = newmask;
  1046. }
  1047. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  1048. if (err)
  1049. goto free_newmask;
  1050. /* Always match on tci. */
  1051. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  1052. if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
  1053. __be16 eth_type = 0;
  1054. __be16 tci = 0;
  1055. if (!encap_valid) {
  1056. OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
  1057. err = -EINVAL;
  1058. goto free_newmask;
  1059. }
  1060. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1061. if (a[OVS_KEY_ATTR_ETHERTYPE])
  1062. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1063. if (eth_type == htons(0xffff)) {
  1064. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1065. encap = a[OVS_KEY_ATTR_ENCAP];
  1066. err = parse_flow_mask_nlattrs(encap, a,
  1067. &mask_attrs, log);
  1068. if (err)
  1069. goto free_newmask;
  1070. } else {
  1071. OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
  1072. ntohs(eth_type));
  1073. err = -EINVAL;
  1074. goto free_newmask;
  1075. }
  1076. if (a[OVS_KEY_ATTR_VLAN])
  1077. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1078. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1079. OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
  1080. ntohs(tci));
  1081. err = -EINVAL;
  1082. goto free_newmask;
  1083. }
  1084. }
  1085. err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
  1086. log);
  1087. if (err)
  1088. goto free_newmask;
  1089. }
  1090. if (!match_validate(match, key_attrs, mask_attrs, log))
  1091. err = -EINVAL;
  1092. free_newmask:
  1093. kfree(newmask);
  1094. return err;
  1095. }
  1096. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1097. {
  1098. size_t len;
  1099. if (!attr)
  1100. return 0;
  1101. len = nla_len(attr);
  1102. if (len < 1 || len > MAX_UFID_LENGTH) {
  1103. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1104. nla_len(attr), MAX_UFID_LENGTH);
  1105. return 0;
  1106. }
  1107. return len;
  1108. }
  1109. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1110. * or false otherwise.
  1111. */
  1112. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1113. bool log)
  1114. {
  1115. sfid->ufid_len = get_ufid_len(attr, log);
  1116. if (sfid->ufid_len)
  1117. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1118. return sfid->ufid_len;
  1119. }
  1120. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1121. const struct sw_flow_key *key, bool log)
  1122. {
  1123. struct sw_flow_key *new_key;
  1124. if (ovs_nla_get_ufid(sfid, ufid, log))
  1125. return 0;
  1126. /* If UFID was not provided, use unmasked key. */
  1127. new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
  1128. if (!new_key)
  1129. return -ENOMEM;
  1130. memcpy(new_key, key, sizeof(*key));
  1131. sfid->unmasked_key = new_key;
  1132. return 0;
  1133. }
  1134. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1135. {
  1136. return attr ? nla_get_u32(attr) : 0;
  1137. }
  1138. /**
  1139. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1140. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  1141. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1142. * sequence.
  1143. * @log: Boolean to allow kernel error logging. Normally true, but when
  1144. * probing for feature compatibility this should be passed in as false to
  1145. * suppress unnecessary error logging.
  1146. *
  1147. * This parses a series of Netlink attributes that form a flow key, which must
  1148. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1149. * get the metadata, that is, the parts of the flow key that cannot be
  1150. * extracted from the packet itself.
  1151. */
  1152. int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
  1153. struct sw_flow_key *key,
  1154. bool log)
  1155. {
  1156. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1157. struct sw_flow_match match;
  1158. u64 attrs = 0;
  1159. int err;
  1160. err = parse_flow_nlattrs(attr, a, &attrs, log);
  1161. if (err)
  1162. return -EINVAL;
  1163. memset(&match, 0, sizeof(match));
  1164. match.key = key;
  1165. memset(&key->ct, 0, sizeof(key->ct));
  1166. key->phy.in_port = DP_MAX_PORTS;
  1167. return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
  1168. }
  1169. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1170. const struct sw_flow_key *output, bool is_mask,
  1171. struct sk_buff *skb)
  1172. {
  1173. struct ovs_key_ethernet *eth_key;
  1174. struct nlattr *nla, *encap;
  1175. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1176. goto nla_put_failure;
  1177. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1178. goto nla_put_failure;
  1179. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1180. goto nla_put_failure;
  1181. if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
  1182. const void *opts = NULL;
  1183. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1184. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1185. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  1186. swkey->tun_opts_len))
  1187. goto nla_put_failure;
  1188. }
  1189. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1190. if (is_mask && (output->phy.in_port == 0xffff))
  1191. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1192. goto nla_put_failure;
  1193. } else {
  1194. u16 upper_u16;
  1195. upper_u16 = !is_mask ? 0 : 0xffff;
  1196. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1197. (upper_u16 << 16) | output->phy.in_port))
  1198. goto nla_put_failure;
  1199. }
  1200. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1201. goto nla_put_failure;
  1202. if (ovs_ct_put_key(output, skb))
  1203. goto nla_put_failure;
  1204. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1205. if (!nla)
  1206. goto nla_put_failure;
  1207. eth_key = nla_data(nla);
  1208. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1209. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1210. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1211. __be16 eth_type;
  1212. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1213. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1214. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1215. goto nla_put_failure;
  1216. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1217. if (!swkey->eth.tci)
  1218. goto unencap;
  1219. } else
  1220. encap = NULL;
  1221. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1222. /*
  1223. * Ethertype 802.2 is represented in the netlink with omitted
  1224. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1225. * 0xffff in the mask attribute. Ethertype can also
  1226. * be wildcarded.
  1227. */
  1228. if (is_mask && output->eth.type)
  1229. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1230. output->eth.type))
  1231. goto nla_put_failure;
  1232. goto unencap;
  1233. }
  1234. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1235. goto nla_put_failure;
  1236. if (swkey->eth.type == htons(ETH_P_IP)) {
  1237. struct ovs_key_ipv4 *ipv4_key;
  1238. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1239. if (!nla)
  1240. goto nla_put_failure;
  1241. ipv4_key = nla_data(nla);
  1242. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1243. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1244. ipv4_key->ipv4_proto = output->ip.proto;
  1245. ipv4_key->ipv4_tos = output->ip.tos;
  1246. ipv4_key->ipv4_ttl = output->ip.ttl;
  1247. ipv4_key->ipv4_frag = output->ip.frag;
  1248. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1249. struct ovs_key_ipv6 *ipv6_key;
  1250. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1251. if (!nla)
  1252. goto nla_put_failure;
  1253. ipv6_key = nla_data(nla);
  1254. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1255. sizeof(ipv6_key->ipv6_src));
  1256. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1257. sizeof(ipv6_key->ipv6_dst));
  1258. ipv6_key->ipv6_label = output->ipv6.label;
  1259. ipv6_key->ipv6_proto = output->ip.proto;
  1260. ipv6_key->ipv6_tclass = output->ip.tos;
  1261. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1262. ipv6_key->ipv6_frag = output->ip.frag;
  1263. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1264. swkey->eth.type == htons(ETH_P_RARP)) {
  1265. struct ovs_key_arp *arp_key;
  1266. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1267. if (!nla)
  1268. goto nla_put_failure;
  1269. arp_key = nla_data(nla);
  1270. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1271. arp_key->arp_sip = output->ipv4.addr.src;
  1272. arp_key->arp_tip = output->ipv4.addr.dst;
  1273. arp_key->arp_op = htons(output->ip.proto);
  1274. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1275. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1276. } else if (eth_p_mpls(swkey->eth.type)) {
  1277. struct ovs_key_mpls *mpls_key;
  1278. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1279. if (!nla)
  1280. goto nla_put_failure;
  1281. mpls_key = nla_data(nla);
  1282. mpls_key->mpls_lse = output->mpls.top_lse;
  1283. }
  1284. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1285. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1286. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1287. if (swkey->ip.proto == IPPROTO_TCP) {
  1288. struct ovs_key_tcp *tcp_key;
  1289. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1290. if (!nla)
  1291. goto nla_put_failure;
  1292. tcp_key = nla_data(nla);
  1293. tcp_key->tcp_src = output->tp.src;
  1294. tcp_key->tcp_dst = output->tp.dst;
  1295. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1296. output->tp.flags))
  1297. goto nla_put_failure;
  1298. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1299. struct ovs_key_udp *udp_key;
  1300. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1301. if (!nla)
  1302. goto nla_put_failure;
  1303. udp_key = nla_data(nla);
  1304. udp_key->udp_src = output->tp.src;
  1305. udp_key->udp_dst = output->tp.dst;
  1306. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1307. struct ovs_key_sctp *sctp_key;
  1308. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1309. if (!nla)
  1310. goto nla_put_failure;
  1311. sctp_key = nla_data(nla);
  1312. sctp_key->sctp_src = output->tp.src;
  1313. sctp_key->sctp_dst = output->tp.dst;
  1314. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1315. swkey->ip.proto == IPPROTO_ICMP) {
  1316. struct ovs_key_icmp *icmp_key;
  1317. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1318. if (!nla)
  1319. goto nla_put_failure;
  1320. icmp_key = nla_data(nla);
  1321. icmp_key->icmp_type = ntohs(output->tp.src);
  1322. icmp_key->icmp_code = ntohs(output->tp.dst);
  1323. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1324. swkey->ip.proto == IPPROTO_ICMPV6) {
  1325. struct ovs_key_icmpv6 *icmpv6_key;
  1326. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1327. sizeof(*icmpv6_key));
  1328. if (!nla)
  1329. goto nla_put_failure;
  1330. icmpv6_key = nla_data(nla);
  1331. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1332. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1333. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1334. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1335. struct ovs_key_nd *nd_key;
  1336. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1337. if (!nla)
  1338. goto nla_put_failure;
  1339. nd_key = nla_data(nla);
  1340. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1341. sizeof(nd_key->nd_target));
  1342. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1343. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1344. }
  1345. }
  1346. }
  1347. unencap:
  1348. if (encap)
  1349. nla_nest_end(skb, encap);
  1350. return 0;
  1351. nla_put_failure:
  1352. return -EMSGSIZE;
  1353. }
  1354. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1355. const struct sw_flow_key *output, int attr, bool is_mask,
  1356. struct sk_buff *skb)
  1357. {
  1358. int err;
  1359. struct nlattr *nla;
  1360. nla = nla_nest_start(skb, attr);
  1361. if (!nla)
  1362. return -EMSGSIZE;
  1363. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1364. if (err)
  1365. return err;
  1366. nla_nest_end(skb, nla);
  1367. return 0;
  1368. }
  1369. /* Called with ovs_mutex or RCU read lock. */
  1370. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1371. {
  1372. if (ovs_identifier_is_ufid(&flow->id))
  1373. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1374. flow->id.ufid);
  1375. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1376. OVS_FLOW_ATTR_KEY, false, skb);
  1377. }
  1378. /* Called with ovs_mutex or RCU read lock. */
  1379. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1380. {
  1381. return ovs_nla_put_key(&flow->key, &flow->key,
  1382. OVS_FLOW_ATTR_KEY, false, skb);
  1383. }
  1384. /* Called with ovs_mutex or RCU read lock. */
  1385. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1386. {
  1387. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1388. OVS_FLOW_ATTR_MASK, true, skb);
  1389. }
  1390. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1391. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1392. {
  1393. struct sw_flow_actions *sfa;
  1394. if (size > MAX_ACTIONS_BUFSIZE) {
  1395. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1396. return ERR_PTR(-EINVAL);
  1397. }
  1398. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1399. if (!sfa)
  1400. return ERR_PTR(-ENOMEM);
  1401. sfa->actions_len = 0;
  1402. return sfa;
  1403. }
  1404. static void ovs_nla_free_set_action(const struct nlattr *a)
  1405. {
  1406. const struct nlattr *ovs_key = nla_data(a);
  1407. struct ovs_tunnel_info *ovs_tun;
  1408. switch (nla_type(ovs_key)) {
  1409. case OVS_KEY_ATTR_TUNNEL_INFO:
  1410. ovs_tun = nla_data(ovs_key);
  1411. dst_release((struct dst_entry *)ovs_tun->tun_dst);
  1412. break;
  1413. }
  1414. }
  1415. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1416. {
  1417. const struct nlattr *a;
  1418. int rem;
  1419. if (!sf_acts)
  1420. return;
  1421. nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
  1422. switch (nla_type(a)) {
  1423. case OVS_ACTION_ATTR_SET:
  1424. ovs_nla_free_set_action(a);
  1425. break;
  1426. case OVS_ACTION_ATTR_CT:
  1427. ovs_ct_free_action(a);
  1428. break;
  1429. }
  1430. }
  1431. kfree(sf_acts);
  1432. }
  1433. static void __ovs_nla_free_flow_actions(struct rcu_head *head)
  1434. {
  1435. ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
  1436. }
  1437. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1438. * The caller must hold rcu_read_lock for this to be sensible. */
  1439. void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
  1440. {
  1441. call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
  1442. }
  1443. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1444. int attr_len, bool log)
  1445. {
  1446. struct sw_flow_actions *acts;
  1447. int new_acts_size;
  1448. int req_size = NLA_ALIGN(attr_len);
  1449. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1450. (*sfa)->actions_len;
  1451. if (req_size <= (ksize(*sfa) - next_offset))
  1452. goto out;
  1453. new_acts_size = ksize(*sfa) * 2;
  1454. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1455. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1456. return ERR_PTR(-EMSGSIZE);
  1457. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1458. }
  1459. acts = nla_alloc_flow_actions(new_acts_size, log);
  1460. if (IS_ERR(acts))
  1461. return (void *)acts;
  1462. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1463. acts->actions_len = (*sfa)->actions_len;
  1464. acts->orig_len = (*sfa)->orig_len;
  1465. kfree(*sfa);
  1466. *sfa = acts;
  1467. out:
  1468. (*sfa)->actions_len += req_size;
  1469. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1470. }
  1471. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1472. int attrtype, void *data, int len, bool log)
  1473. {
  1474. struct nlattr *a;
  1475. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1476. if (IS_ERR(a))
  1477. return a;
  1478. a->nla_type = attrtype;
  1479. a->nla_len = nla_attr_size(len);
  1480. if (data)
  1481. memcpy(nla_data(a), data, len);
  1482. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1483. return a;
  1484. }
  1485. int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
  1486. int len, bool log)
  1487. {
  1488. struct nlattr *a;
  1489. a = __add_action(sfa, attrtype, data, len, log);
  1490. return PTR_ERR_OR_ZERO(a);
  1491. }
  1492. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1493. int attrtype, bool log)
  1494. {
  1495. int used = (*sfa)->actions_len;
  1496. int err;
  1497. err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
  1498. if (err)
  1499. return err;
  1500. return used;
  1501. }
  1502. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1503. int st_offset)
  1504. {
  1505. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1506. st_offset);
  1507. a->nla_len = sfa->actions_len - st_offset;
  1508. }
  1509. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1510. const struct sw_flow_key *key,
  1511. int depth, struct sw_flow_actions **sfa,
  1512. __be16 eth_type, __be16 vlan_tci, bool log);
  1513. static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
  1514. const struct sw_flow_key *key, int depth,
  1515. struct sw_flow_actions **sfa,
  1516. __be16 eth_type, __be16 vlan_tci, bool log)
  1517. {
  1518. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1519. const struct nlattr *probability, *actions;
  1520. const struct nlattr *a;
  1521. int rem, start, err, st_acts;
  1522. memset(attrs, 0, sizeof(attrs));
  1523. nla_for_each_nested(a, attr, rem) {
  1524. int type = nla_type(a);
  1525. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1526. return -EINVAL;
  1527. attrs[type] = a;
  1528. }
  1529. if (rem)
  1530. return -EINVAL;
  1531. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1532. if (!probability || nla_len(probability) != sizeof(u32))
  1533. return -EINVAL;
  1534. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1535. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1536. return -EINVAL;
  1537. /* validation done, copy sample action. */
  1538. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1539. if (start < 0)
  1540. return start;
  1541. err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1542. nla_data(probability), sizeof(u32), log);
  1543. if (err)
  1544. return err;
  1545. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1546. if (st_acts < 0)
  1547. return st_acts;
  1548. err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
  1549. eth_type, vlan_tci, log);
  1550. if (err)
  1551. return err;
  1552. add_nested_action_end(*sfa, st_acts);
  1553. add_nested_action_end(*sfa, start);
  1554. return 0;
  1555. }
  1556. void ovs_match_init(struct sw_flow_match *match,
  1557. struct sw_flow_key *key,
  1558. struct sw_flow_mask *mask)
  1559. {
  1560. memset(match, 0, sizeof(*match));
  1561. match->key = key;
  1562. match->mask = mask;
  1563. memset(key, 0, sizeof(*key));
  1564. if (mask) {
  1565. memset(&mask->key, 0, sizeof(mask->key));
  1566. mask->range.start = mask->range.end = 0;
  1567. }
  1568. }
  1569. static int validate_geneve_opts(struct sw_flow_key *key)
  1570. {
  1571. struct geneve_opt *option;
  1572. int opts_len = key->tun_opts_len;
  1573. bool crit_opt = false;
  1574. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1575. while (opts_len > 0) {
  1576. int len;
  1577. if (opts_len < sizeof(*option))
  1578. return -EINVAL;
  1579. len = sizeof(*option) + option->length * 4;
  1580. if (len > opts_len)
  1581. return -EINVAL;
  1582. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1583. option = (struct geneve_opt *)((u8 *)option + len);
  1584. opts_len -= len;
  1585. };
  1586. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1587. return 0;
  1588. }
  1589. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1590. struct sw_flow_actions **sfa, bool log)
  1591. {
  1592. struct sw_flow_match match;
  1593. struct sw_flow_key key;
  1594. struct metadata_dst *tun_dst;
  1595. struct ip_tunnel_info *tun_info;
  1596. struct ovs_tunnel_info *ovs_tun;
  1597. struct nlattr *a;
  1598. int err = 0, start, opts_type;
  1599. ovs_match_init(&match, &key, NULL);
  1600. opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1601. if (opts_type < 0)
  1602. return opts_type;
  1603. if (key.tun_opts_len) {
  1604. switch (opts_type) {
  1605. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1606. err = validate_geneve_opts(&key);
  1607. if (err < 0)
  1608. return err;
  1609. break;
  1610. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1611. break;
  1612. }
  1613. };
  1614. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1615. if (start < 0)
  1616. return start;
  1617. tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
  1618. if (!tun_dst)
  1619. return -ENOMEM;
  1620. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1621. sizeof(*ovs_tun), log);
  1622. if (IS_ERR(a)) {
  1623. dst_release((struct dst_entry *)tun_dst);
  1624. return PTR_ERR(a);
  1625. }
  1626. ovs_tun = nla_data(a);
  1627. ovs_tun->tun_dst = tun_dst;
  1628. tun_info = &tun_dst->u.tun_info;
  1629. tun_info->mode = IP_TUNNEL_INFO_TX;
  1630. tun_info->key = key.tun_key;
  1631. /* We need to store the options in the action itself since
  1632. * everything else will go away after flow setup. We can append
  1633. * it to tun_info and then point there.
  1634. */
  1635. ip_tunnel_info_opts_set(tun_info,
  1636. TUN_METADATA_OPTS(&key, key.tun_opts_len),
  1637. key.tun_opts_len);
  1638. add_nested_action_end(*sfa, start);
  1639. return err;
  1640. }
  1641. /* Return false if there are any non-masked bits set.
  1642. * Mask follows data immediately, before any netlink padding.
  1643. */
  1644. static bool validate_masked(u8 *data, int len)
  1645. {
  1646. u8 *mask = data + len;
  1647. while (len--)
  1648. if (*data++ & ~*mask++)
  1649. return false;
  1650. return true;
  1651. }
  1652. static int validate_set(const struct nlattr *a,
  1653. const struct sw_flow_key *flow_key,
  1654. struct sw_flow_actions **sfa,
  1655. bool *skip_copy, __be16 eth_type, bool masked, bool log)
  1656. {
  1657. const struct nlattr *ovs_key = nla_data(a);
  1658. int key_type = nla_type(ovs_key);
  1659. size_t key_len;
  1660. /* There can be only one key in a action */
  1661. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1662. return -EINVAL;
  1663. key_len = nla_len(ovs_key);
  1664. if (masked)
  1665. key_len /= 2;
  1666. if (key_type > OVS_KEY_ATTR_MAX ||
  1667. !check_attr_len(key_len, ovs_key_lens[key_type].len))
  1668. return -EINVAL;
  1669. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  1670. return -EINVAL;
  1671. switch (key_type) {
  1672. const struct ovs_key_ipv4 *ipv4_key;
  1673. const struct ovs_key_ipv6 *ipv6_key;
  1674. int err;
  1675. case OVS_KEY_ATTR_PRIORITY:
  1676. case OVS_KEY_ATTR_SKB_MARK:
  1677. case OVS_KEY_ATTR_CT_MARK:
  1678. case OVS_KEY_ATTR_CT_LABELS:
  1679. case OVS_KEY_ATTR_ETHERNET:
  1680. break;
  1681. case OVS_KEY_ATTR_TUNNEL:
  1682. if (eth_p_mpls(eth_type))
  1683. return -EINVAL;
  1684. if (masked)
  1685. return -EINVAL; /* Masked tunnel set not supported. */
  1686. *skip_copy = true;
  1687. err = validate_and_copy_set_tun(a, sfa, log);
  1688. if (err)
  1689. return err;
  1690. break;
  1691. case OVS_KEY_ATTR_IPV4:
  1692. if (eth_type != htons(ETH_P_IP))
  1693. return -EINVAL;
  1694. ipv4_key = nla_data(ovs_key);
  1695. if (masked) {
  1696. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  1697. /* Non-writeable fields. */
  1698. if (mask->ipv4_proto || mask->ipv4_frag)
  1699. return -EINVAL;
  1700. } else {
  1701. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1702. return -EINVAL;
  1703. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1704. return -EINVAL;
  1705. }
  1706. break;
  1707. case OVS_KEY_ATTR_IPV6:
  1708. if (eth_type != htons(ETH_P_IPV6))
  1709. return -EINVAL;
  1710. ipv6_key = nla_data(ovs_key);
  1711. if (masked) {
  1712. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  1713. /* Non-writeable fields. */
  1714. if (mask->ipv6_proto || mask->ipv6_frag)
  1715. return -EINVAL;
  1716. /* Invalid bits in the flow label mask? */
  1717. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  1718. return -EINVAL;
  1719. } else {
  1720. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1721. return -EINVAL;
  1722. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1723. return -EINVAL;
  1724. }
  1725. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1726. return -EINVAL;
  1727. break;
  1728. case OVS_KEY_ATTR_TCP:
  1729. if ((eth_type != htons(ETH_P_IP) &&
  1730. eth_type != htons(ETH_P_IPV6)) ||
  1731. flow_key->ip.proto != IPPROTO_TCP)
  1732. return -EINVAL;
  1733. break;
  1734. case OVS_KEY_ATTR_UDP:
  1735. if ((eth_type != htons(ETH_P_IP) &&
  1736. eth_type != htons(ETH_P_IPV6)) ||
  1737. flow_key->ip.proto != IPPROTO_UDP)
  1738. return -EINVAL;
  1739. break;
  1740. case OVS_KEY_ATTR_MPLS:
  1741. if (!eth_p_mpls(eth_type))
  1742. return -EINVAL;
  1743. break;
  1744. case OVS_KEY_ATTR_SCTP:
  1745. if ((eth_type != htons(ETH_P_IP) &&
  1746. eth_type != htons(ETH_P_IPV6)) ||
  1747. flow_key->ip.proto != IPPROTO_SCTP)
  1748. return -EINVAL;
  1749. break;
  1750. default:
  1751. return -EINVAL;
  1752. }
  1753. /* Convert non-masked non-tunnel set actions to masked set actions. */
  1754. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  1755. int start, len = key_len * 2;
  1756. struct nlattr *at;
  1757. *skip_copy = true;
  1758. start = add_nested_action_start(sfa,
  1759. OVS_ACTION_ATTR_SET_TO_MASKED,
  1760. log);
  1761. if (start < 0)
  1762. return start;
  1763. at = __add_action(sfa, key_type, NULL, len, log);
  1764. if (IS_ERR(at))
  1765. return PTR_ERR(at);
  1766. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  1767. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  1768. /* Clear non-writeable bits from otherwise writeable fields. */
  1769. if (key_type == OVS_KEY_ATTR_IPV6) {
  1770. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  1771. mask->ipv6_label &= htonl(0x000FFFFF);
  1772. }
  1773. add_nested_action_end(*sfa, start);
  1774. }
  1775. return 0;
  1776. }
  1777. static int validate_userspace(const struct nlattr *attr)
  1778. {
  1779. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1780. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1781. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1782. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1783. };
  1784. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1785. int error;
  1786. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1787. attr, userspace_policy);
  1788. if (error)
  1789. return error;
  1790. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1791. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1792. return -EINVAL;
  1793. return 0;
  1794. }
  1795. static int copy_action(const struct nlattr *from,
  1796. struct sw_flow_actions **sfa, bool log)
  1797. {
  1798. int totlen = NLA_ALIGN(from->nla_len);
  1799. struct nlattr *to;
  1800. to = reserve_sfa_size(sfa, from->nla_len, log);
  1801. if (IS_ERR(to))
  1802. return PTR_ERR(to);
  1803. memcpy(to, from, totlen);
  1804. return 0;
  1805. }
  1806. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1807. const struct sw_flow_key *key,
  1808. int depth, struct sw_flow_actions **sfa,
  1809. __be16 eth_type, __be16 vlan_tci, bool log)
  1810. {
  1811. const struct nlattr *a;
  1812. int rem, err;
  1813. if (depth >= SAMPLE_ACTION_DEPTH)
  1814. return -EOVERFLOW;
  1815. nla_for_each_nested(a, attr, rem) {
  1816. /* Expected argument lengths, (u32)-1 for variable length. */
  1817. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1818. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1819. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1820. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1821. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1822. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1823. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1824. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1825. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1826. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  1827. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1828. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
  1829. [OVS_ACTION_ATTR_CT] = (u32)-1,
  1830. };
  1831. const struct ovs_action_push_vlan *vlan;
  1832. int type = nla_type(a);
  1833. bool skip_copy;
  1834. if (type > OVS_ACTION_ATTR_MAX ||
  1835. (action_lens[type] != nla_len(a) &&
  1836. action_lens[type] != (u32)-1))
  1837. return -EINVAL;
  1838. skip_copy = false;
  1839. switch (type) {
  1840. case OVS_ACTION_ATTR_UNSPEC:
  1841. return -EINVAL;
  1842. case OVS_ACTION_ATTR_USERSPACE:
  1843. err = validate_userspace(a);
  1844. if (err)
  1845. return err;
  1846. break;
  1847. case OVS_ACTION_ATTR_OUTPUT:
  1848. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1849. return -EINVAL;
  1850. break;
  1851. case OVS_ACTION_ATTR_HASH: {
  1852. const struct ovs_action_hash *act_hash = nla_data(a);
  1853. switch (act_hash->hash_alg) {
  1854. case OVS_HASH_ALG_L4:
  1855. break;
  1856. default:
  1857. return -EINVAL;
  1858. }
  1859. break;
  1860. }
  1861. case OVS_ACTION_ATTR_POP_VLAN:
  1862. vlan_tci = htons(0);
  1863. break;
  1864. case OVS_ACTION_ATTR_PUSH_VLAN:
  1865. vlan = nla_data(a);
  1866. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1867. return -EINVAL;
  1868. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1869. return -EINVAL;
  1870. vlan_tci = vlan->vlan_tci;
  1871. break;
  1872. case OVS_ACTION_ATTR_RECIRC:
  1873. break;
  1874. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1875. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1876. if (!eth_p_mpls(mpls->mpls_ethertype))
  1877. return -EINVAL;
  1878. /* Prohibit push MPLS other than to a white list
  1879. * for packets that have a known tag order.
  1880. */
  1881. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1882. (eth_type != htons(ETH_P_IP) &&
  1883. eth_type != htons(ETH_P_IPV6) &&
  1884. eth_type != htons(ETH_P_ARP) &&
  1885. eth_type != htons(ETH_P_RARP) &&
  1886. !eth_p_mpls(eth_type)))
  1887. return -EINVAL;
  1888. eth_type = mpls->mpls_ethertype;
  1889. break;
  1890. }
  1891. case OVS_ACTION_ATTR_POP_MPLS:
  1892. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1893. !eth_p_mpls(eth_type))
  1894. return -EINVAL;
  1895. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1896. * as there is no check here to ensure that the new
  1897. * eth_type is valid and thus set actions could
  1898. * write off the end of the packet or otherwise
  1899. * corrupt it.
  1900. *
  1901. * Support for these actions is planned using packet
  1902. * recirculation.
  1903. */
  1904. eth_type = htons(0);
  1905. break;
  1906. case OVS_ACTION_ATTR_SET:
  1907. err = validate_set(a, key, sfa,
  1908. &skip_copy, eth_type, false, log);
  1909. if (err)
  1910. return err;
  1911. break;
  1912. case OVS_ACTION_ATTR_SET_MASKED:
  1913. err = validate_set(a, key, sfa,
  1914. &skip_copy, eth_type, true, log);
  1915. if (err)
  1916. return err;
  1917. break;
  1918. case OVS_ACTION_ATTR_SAMPLE:
  1919. err = validate_and_copy_sample(net, a, key, depth, sfa,
  1920. eth_type, vlan_tci, log);
  1921. if (err)
  1922. return err;
  1923. skip_copy = true;
  1924. break;
  1925. case OVS_ACTION_ATTR_CT:
  1926. err = ovs_ct_copy_action(net, a, key, sfa, log);
  1927. if (err)
  1928. return err;
  1929. skip_copy = true;
  1930. break;
  1931. default:
  1932. OVS_NLERR(log, "Unknown Action type %d", type);
  1933. return -EINVAL;
  1934. }
  1935. if (!skip_copy) {
  1936. err = copy_action(a, sfa, log);
  1937. if (err)
  1938. return err;
  1939. }
  1940. }
  1941. if (rem > 0)
  1942. return -EINVAL;
  1943. return 0;
  1944. }
  1945. /* 'key' must be the masked key. */
  1946. int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1947. const struct sw_flow_key *key,
  1948. struct sw_flow_actions **sfa, bool log)
  1949. {
  1950. int err;
  1951. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1952. if (IS_ERR(*sfa))
  1953. return PTR_ERR(*sfa);
  1954. (*sfa)->orig_len = nla_len(attr);
  1955. err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
  1956. key->eth.tci, log);
  1957. if (err)
  1958. ovs_nla_free_flow_actions(*sfa);
  1959. return err;
  1960. }
  1961. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1962. {
  1963. const struct nlattr *a;
  1964. struct nlattr *start;
  1965. int err = 0, rem;
  1966. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1967. if (!start)
  1968. return -EMSGSIZE;
  1969. nla_for_each_nested(a, attr, rem) {
  1970. int type = nla_type(a);
  1971. struct nlattr *st_sample;
  1972. switch (type) {
  1973. case OVS_SAMPLE_ATTR_PROBABILITY:
  1974. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1975. sizeof(u32), nla_data(a)))
  1976. return -EMSGSIZE;
  1977. break;
  1978. case OVS_SAMPLE_ATTR_ACTIONS:
  1979. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1980. if (!st_sample)
  1981. return -EMSGSIZE;
  1982. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1983. if (err)
  1984. return err;
  1985. nla_nest_end(skb, st_sample);
  1986. break;
  1987. }
  1988. }
  1989. nla_nest_end(skb, start);
  1990. return err;
  1991. }
  1992. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1993. {
  1994. const struct nlattr *ovs_key = nla_data(a);
  1995. int key_type = nla_type(ovs_key);
  1996. struct nlattr *start;
  1997. int err;
  1998. switch (key_type) {
  1999. case OVS_KEY_ATTR_TUNNEL_INFO: {
  2000. struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
  2001. struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
  2002. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2003. if (!start)
  2004. return -EMSGSIZE;
  2005. err = ipv4_tun_to_nlattr(skb, &tun_info->key,
  2006. tun_info->options_len ?
  2007. ip_tunnel_info_opts(tun_info) : NULL,
  2008. tun_info->options_len);
  2009. if (err)
  2010. return err;
  2011. nla_nest_end(skb, start);
  2012. break;
  2013. }
  2014. default:
  2015. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  2016. return -EMSGSIZE;
  2017. break;
  2018. }
  2019. return 0;
  2020. }
  2021. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  2022. struct sk_buff *skb)
  2023. {
  2024. const struct nlattr *ovs_key = nla_data(a);
  2025. struct nlattr *nla;
  2026. size_t key_len = nla_len(ovs_key) / 2;
  2027. /* Revert the conversion we did from a non-masked set action to
  2028. * masked set action.
  2029. */
  2030. nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2031. if (!nla)
  2032. return -EMSGSIZE;
  2033. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  2034. return -EMSGSIZE;
  2035. nla_nest_end(skb, nla);
  2036. return 0;
  2037. }
  2038. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  2039. {
  2040. const struct nlattr *a;
  2041. int rem, err;
  2042. nla_for_each_attr(a, attr, len, rem) {
  2043. int type = nla_type(a);
  2044. switch (type) {
  2045. case OVS_ACTION_ATTR_SET:
  2046. err = set_action_to_attr(a, skb);
  2047. if (err)
  2048. return err;
  2049. break;
  2050. case OVS_ACTION_ATTR_SET_TO_MASKED:
  2051. err = masked_set_action_to_set_action_attr(a, skb);
  2052. if (err)
  2053. return err;
  2054. break;
  2055. case OVS_ACTION_ATTR_SAMPLE:
  2056. err = sample_action_to_attr(a, skb);
  2057. if (err)
  2058. return err;
  2059. break;
  2060. case OVS_ACTION_ATTR_CT:
  2061. err = ovs_ct_action_to_attr(nla_data(a), skb);
  2062. if (err)
  2063. return err;
  2064. break;
  2065. default:
  2066. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  2067. return -EMSGSIZE;
  2068. break;
  2069. }
  2070. }
  2071. return 0;
  2072. }