flow_netlink.c 77 KB

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