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