flow_netlink.c 76 KB

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