flow_netlink.c 71 KB

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