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), expected_len)) {
  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).\n",
  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. if (tbl[nla_type(nla)].next)
  1219. tbl = tbl[nla_type(nla)].next;
  1220. nlattr_set(nla, val, tbl);
  1221. } else {
  1222. memset(nla_data(nla), val, nla_len(nla));
  1223. }
  1224. if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
  1225. *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
  1226. }
  1227. }
  1228. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  1229. {
  1230. nlattr_set(attr, val, ovs_key_lens);
  1231. }
  1232. /**
  1233. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  1234. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1235. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1236. * does not include any don't care bit.
  1237. * @net: Used to determine per-namespace field support.
  1238. * @match: receives the extracted flow match information.
  1239. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1240. * sequence. The fields should of the packet that triggered the creation
  1241. * of this flow.
  1242. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  1243. * attribute specifies the mask field of the wildcarded flow.
  1244. * @log: Boolean to allow kernel error logging. Normally true, but when
  1245. * probing for feature compatibility this should be passed in as false to
  1246. * suppress unnecessary error logging.
  1247. */
  1248. int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
  1249. const struct nlattr *nla_key,
  1250. const struct nlattr *nla_mask,
  1251. bool log)
  1252. {
  1253. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1254. struct nlattr *newmask = NULL;
  1255. u64 key_attrs = 0;
  1256. u64 mask_attrs = 0;
  1257. int err;
  1258. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  1259. if (err)
  1260. return err;
  1261. err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
  1262. if (err)
  1263. return err;
  1264. err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
  1265. if (err)
  1266. return err;
  1267. if (match->mask) {
  1268. if (!nla_mask) {
  1269. /* Create an exact match mask. We need to set to 0xff
  1270. * all the 'match->mask' fields that have been touched
  1271. * in 'match->key'. We cannot simply memset
  1272. * 'match->mask', because padding bytes and fields not
  1273. * specified in 'match->key' should be left to 0.
  1274. * Instead, we use a stream of netlink attributes,
  1275. * copied from 'key' and set to 0xff.
  1276. * ovs_key_from_nlattrs() will take care of filling
  1277. * 'match->mask' appropriately.
  1278. */
  1279. newmask = kmemdup(nla_key,
  1280. nla_total_size(nla_len(nla_key)),
  1281. GFP_KERNEL);
  1282. if (!newmask)
  1283. return -ENOMEM;
  1284. mask_set_nlattr(newmask, 0xff);
  1285. /* The userspace does not send tunnel attributes that
  1286. * are 0, but we should not wildcard them nonetheless.
  1287. */
  1288. if (match->key->tun_proto)
  1289. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  1290. 0xff, true);
  1291. nla_mask = newmask;
  1292. }
  1293. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  1294. if (err)
  1295. goto free_newmask;
  1296. /* Always match on tci. */
  1297. SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
  1298. SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
  1299. err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
  1300. if (err)
  1301. goto free_newmask;
  1302. err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
  1303. log);
  1304. if (err)
  1305. goto free_newmask;
  1306. }
  1307. if (!match_validate(match, key_attrs, mask_attrs, log))
  1308. err = -EINVAL;
  1309. free_newmask:
  1310. kfree(newmask);
  1311. return err;
  1312. }
  1313. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1314. {
  1315. size_t len;
  1316. if (!attr)
  1317. return 0;
  1318. len = nla_len(attr);
  1319. if (len < 1 || len > MAX_UFID_LENGTH) {
  1320. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1321. nla_len(attr), MAX_UFID_LENGTH);
  1322. return 0;
  1323. }
  1324. return len;
  1325. }
  1326. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1327. * or false otherwise.
  1328. */
  1329. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1330. bool log)
  1331. {
  1332. sfid->ufid_len = get_ufid_len(attr, log);
  1333. if (sfid->ufid_len)
  1334. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1335. return sfid->ufid_len;
  1336. }
  1337. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1338. const struct sw_flow_key *key, bool log)
  1339. {
  1340. struct sw_flow_key *new_key;
  1341. if (ovs_nla_get_ufid(sfid, ufid, log))
  1342. return 0;
  1343. /* If UFID was not provided, use unmasked key. */
  1344. new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
  1345. if (!new_key)
  1346. return -ENOMEM;
  1347. memcpy(new_key, key, sizeof(*key));
  1348. sfid->unmasked_key = new_key;
  1349. return 0;
  1350. }
  1351. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1352. {
  1353. return attr ? nla_get_u32(attr) : 0;
  1354. }
  1355. /**
  1356. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1357. * @net: Network namespace.
  1358. * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
  1359. * metadata.
  1360. * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
  1361. * attributes.
  1362. * @attrs: Bit mask for the netlink attributes included in @a.
  1363. * @log: Boolean to allow kernel error logging. Normally true, but when
  1364. * probing for feature compatibility this should be passed in as false to
  1365. * suppress unnecessary error logging.
  1366. *
  1367. * This parses a series of Netlink attributes that form a flow key, which must
  1368. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1369. * get the metadata, that is, the parts of the flow key that cannot be
  1370. * extracted from the packet itself.
  1371. *
  1372. * This must be called before the packet key fields are filled in 'key'.
  1373. */
  1374. int ovs_nla_get_flow_metadata(struct net *net,
  1375. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
  1376. u64 attrs, struct sw_flow_key *key, bool log)
  1377. {
  1378. struct sw_flow_match match;
  1379. memset(&match, 0, sizeof(match));
  1380. match.key = key;
  1381. key->ct_state = 0;
  1382. key->ct_zone = 0;
  1383. key->ct_orig_proto = 0;
  1384. memset(&key->ct, 0, sizeof(key->ct));
  1385. memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
  1386. memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
  1387. key->phy.in_port = DP_MAX_PORTS;
  1388. return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
  1389. }
  1390. static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
  1391. bool is_mask)
  1392. {
  1393. __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
  1394. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1395. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
  1396. return -EMSGSIZE;
  1397. return 0;
  1398. }
  1399. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1400. const struct sw_flow_key *output, bool is_mask,
  1401. struct sk_buff *skb)
  1402. {
  1403. struct ovs_key_ethernet *eth_key;
  1404. struct nlattr *nla;
  1405. struct nlattr *encap = NULL;
  1406. struct nlattr *in_encap = NULL;
  1407. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1408. goto nla_put_failure;
  1409. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1410. goto nla_put_failure;
  1411. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1412. goto nla_put_failure;
  1413. if ((swkey->tun_proto || is_mask)) {
  1414. const void *opts = NULL;
  1415. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1416. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1417. if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
  1418. swkey->tun_opts_len, swkey->tun_proto))
  1419. goto nla_put_failure;
  1420. }
  1421. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1422. if (is_mask && (output->phy.in_port == 0xffff))
  1423. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1424. goto nla_put_failure;
  1425. } else {
  1426. u16 upper_u16;
  1427. upper_u16 = !is_mask ? 0 : 0xffff;
  1428. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1429. (upper_u16 << 16) | output->phy.in_port))
  1430. goto nla_put_failure;
  1431. }
  1432. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1433. goto nla_put_failure;
  1434. if (ovs_ct_put_key(swkey, output, skb))
  1435. goto nla_put_failure;
  1436. if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
  1437. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1438. if (!nla)
  1439. goto nla_put_failure;
  1440. eth_key = nla_data(nla);
  1441. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1442. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1443. if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
  1444. if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
  1445. goto nla_put_failure;
  1446. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1447. if (!swkey->eth.vlan.tci)
  1448. goto unencap;
  1449. if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
  1450. if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
  1451. goto nla_put_failure;
  1452. in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1453. if (!swkey->eth.cvlan.tci)
  1454. goto unencap;
  1455. }
  1456. }
  1457. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1458. /*
  1459. * Ethertype 802.2 is represented in the netlink with omitted
  1460. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1461. * 0xffff in the mask attribute. Ethertype can also
  1462. * be wildcarded.
  1463. */
  1464. if (is_mask && output->eth.type)
  1465. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1466. output->eth.type))
  1467. goto nla_put_failure;
  1468. goto unencap;
  1469. }
  1470. }
  1471. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1472. goto nla_put_failure;
  1473. if (eth_type_vlan(swkey->eth.type)) {
  1474. /* There are 3 VLAN tags, we don't know anything about the rest
  1475. * of the packet, so truncate here.
  1476. */
  1477. WARN_ON_ONCE(!(encap && in_encap));
  1478. goto unencap;
  1479. }
  1480. if (swkey->eth.type == htons(ETH_P_IP)) {
  1481. struct ovs_key_ipv4 *ipv4_key;
  1482. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1483. if (!nla)
  1484. goto nla_put_failure;
  1485. ipv4_key = nla_data(nla);
  1486. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1487. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1488. ipv4_key->ipv4_proto = output->ip.proto;
  1489. ipv4_key->ipv4_tos = output->ip.tos;
  1490. ipv4_key->ipv4_ttl = output->ip.ttl;
  1491. ipv4_key->ipv4_frag = output->ip.frag;
  1492. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1493. struct ovs_key_ipv6 *ipv6_key;
  1494. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1495. if (!nla)
  1496. goto nla_put_failure;
  1497. ipv6_key = nla_data(nla);
  1498. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1499. sizeof(ipv6_key->ipv6_src));
  1500. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1501. sizeof(ipv6_key->ipv6_dst));
  1502. ipv6_key->ipv6_label = output->ipv6.label;
  1503. ipv6_key->ipv6_proto = output->ip.proto;
  1504. ipv6_key->ipv6_tclass = output->ip.tos;
  1505. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1506. ipv6_key->ipv6_frag = output->ip.frag;
  1507. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1508. swkey->eth.type == htons(ETH_P_RARP)) {
  1509. struct ovs_key_arp *arp_key;
  1510. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1511. if (!nla)
  1512. goto nla_put_failure;
  1513. arp_key = nla_data(nla);
  1514. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1515. arp_key->arp_sip = output->ipv4.addr.src;
  1516. arp_key->arp_tip = output->ipv4.addr.dst;
  1517. arp_key->arp_op = htons(output->ip.proto);
  1518. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1519. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1520. } else if (eth_p_mpls(swkey->eth.type)) {
  1521. struct ovs_key_mpls *mpls_key;
  1522. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1523. if (!nla)
  1524. goto nla_put_failure;
  1525. mpls_key = nla_data(nla);
  1526. mpls_key->mpls_lse = output->mpls.top_lse;
  1527. }
  1528. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1529. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1530. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1531. if (swkey->ip.proto == IPPROTO_TCP) {
  1532. struct ovs_key_tcp *tcp_key;
  1533. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1534. if (!nla)
  1535. goto nla_put_failure;
  1536. tcp_key = nla_data(nla);
  1537. tcp_key->tcp_src = output->tp.src;
  1538. tcp_key->tcp_dst = output->tp.dst;
  1539. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1540. output->tp.flags))
  1541. goto nla_put_failure;
  1542. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1543. struct ovs_key_udp *udp_key;
  1544. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1545. if (!nla)
  1546. goto nla_put_failure;
  1547. udp_key = nla_data(nla);
  1548. udp_key->udp_src = output->tp.src;
  1549. udp_key->udp_dst = output->tp.dst;
  1550. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1551. struct ovs_key_sctp *sctp_key;
  1552. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1553. if (!nla)
  1554. goto nla_put_failure;
  1555. sctp_key = nla_data(nla);
  1556. sctp_key->sctp_src = output->tp.src;
  1557. sctp_key->sctp_dst = output->tp.dst;
  1558. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1559. swkey->ip.proto == IPPROTO_ICMP) {
  1560. struct ovs_key_icmp *icmp_key;
  1561. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1562. if (!nla)
  1563. goto nla_put_failure;
  1564. icmp_key = nla_data(nla);
  1565. icmp_key->icmp_type = ntohs(output->tp.src);
  1566. icmp_key->icmp_code = ntohs(output->tp.dst);
  1567. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1568. swkey->ip.proto == IPPROTO_ICMPV6) {
  1569. struct ovs_key_icmpv6 *icmpv6_key;
  1570. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1571. sizeof(*icmpv6_key));
  1572. if (!nla)
  1573. goto nla_put_failure;
  1574. icmpv6_key = nla_data(nla);
  1575. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1576. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1577. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1578. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1579. struct ovs_key_nd *nd_key;
  1580. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1581. if (!nla)
  1582. goto nla_put_failure;
  1583. nd_key = nla_data(nla);
  1584. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1585. sizeof(nd_key->nd_target));
  1586. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1587. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1588. }
  1589. }
  1590. }
  1591. unencap:
  1592. if (in_encap)
  1593. nla_nest_end(skb, in_encap);
  1594. if (encap)
  1595. nla_nest_end(skb, encap);
  1596. return 0;
  1597. nla_put_failure:
  1598. return -EMSGSIZE;
  1599. }
  1600. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1601. const struct sw_flow_key *output, int attr, bool is_mask,
  1602. struct sk_buff *skb)
  1603. {
  1604. int err;
  1605. struct nlattr *nla;
  1606. nla = nla_nest_start(skb, attr);
  1607. if (!nla)
  1608. return -EMSGSIZE;
  1609. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1610. if (err)
  1611. return err;
  1612. nla_nest_end(skb, nla);
  1613. return 0;
  1614. }
  1615. /* Called with ovs_mutex or RCU read lock. */
  1616. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1617. {
  1618. if (ovs_identifier_is_ufid(&flow->id))
  1619. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1620. flow->id.ufid);
  1621. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1622. OVS_FLOW_ATTR_KEY, false, skb);
  1623. }
  1624. /* Called with ovs_mutex or RCU read lock. */
  1625. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1626. {
  1627. return ovs_nla_put_key(&flow->key, &flow->key,
  1628. OVS_FLOW_ATTR_KEY, false, skb);
  1629. }
  1630. /* Called with ovs_mutex or RCU read lock. */
  1631. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1632. {
  1633. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1634. OVS_FLOW_ATTR_MASK, true, skb);
  1635. }
  1636. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1637. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1638. {
  1639. struct sw_flow_actions *sfa;
  1640. if (size > MAX_ACTIONS_BUFSIZE) {
  1641. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1642. return ERR_PTR(-EINVAL);
  1643. }
  1644. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1645. if (!sfa)
  1646. return ERR_PTR(-ENOMEM);
  1647. sfa->actions_len = 0;
  1648. return sfa;
  1649. }
  1650. static void ovs_nla_free_set_action(const struct nlattr *a)
  1651. {
  1652. const struct nlattr *ovs_key = nla_data(a);
  1653. struct ovs_tunnel_info *ovs_tun;
  1654. switch (nla_type(ovs_key)) {
  1655. case OVS_KEY_ATTR_TUNNEL_INFO:
  1656. ovs_tun = nla_data(ovs_key);
  1657. dst_release((struct dst_entry *)ovs_tun->tun_dst);
  1658. break;
  1659. }
  1660. }
  1661. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1662. {
  1663. const struct nlattr *a;
  1664. int rem;
  1665. if (!sf_acts)
  1666. return;
  1667. nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
  1668. switch (nla_type(a)) {
  1669. case OVS_ACTION_ATTR_SET:
  1670. ovs_nla_free_set_action(a);
  1671. break;
  1672. case OVS_ACTION_ATTR_CT:
  1673. ovs_ct_free_action(a);
  1674. break;
  1675. }
  1676. }
  1677. kfree(sf_acts);
  1678. }
  1679. static void __ovs_nla_free_flow_actions(struct rcu_head *head)
  1680. {
  1681. ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
  1682. }
  1683. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1684. * The caller must hold rcu_read_lock for this to be sensible. */
  1685. void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
  1686. {
  1687. call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
  1688. }
  1689. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1690. int attr_len, bool log)
  1691. {
  1692. struct sw_flow_actions *acts;
  1693. int new_acts_size;
  1694. int req_size = NLA_ALIGN(attr_len);
  1695. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1696. (*sfa)->actions_len;
  1697. if (req_size <= (ksize(*sfa) - next_offset))
  1698. goto out;
  1699. new_acts_size = ksize(*sfa) * 2;
  1700. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1701. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1702. return ERR_PTR(-EMSGSIZE);
  1703. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1704. }
  1705. acts = nla_alloc_flow_actions(new_acts_size, log);
  1706. if (IS_ERR(acts))
  1707. return (void *)acts;
  1708. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1709. acts->actions_len = (*sfa)->actions_len;
  1710. acts->orig_len = (*sfa)->orig_len;
  1711. kfree(*sfa);
  1712. *sfa = acts;
  1713. out:
  1714. (*sfa)->actions_len += req_size;
  1715. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1716. }
  1717. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1718. int attrtype, void *data, int len, bool log)
  1719. {
  1720. struct nlattr *a;
  1721. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1722. if (IS_ERR(a))
  1723. return a;
  1724. a->nla_type = attrtype;
  1725. a->nla_len = nla_attr_size(len);
  1726. if (data)
  1727. memcpy(nla_data(a), data, len);
  1728. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1729. return a;
  1730. }
  1731. int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
  1732. int len, bool log)
  1733. {
  1734. struct nlattr *a;
  1735. a = __add_action(sfa, attrtype, data, len, log);
  1736. return PTR_ERR_OR_ZERO(a);
  1737. }
  1738. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1739. int attrtype, bool log)
  1740. {
  1741. int used = (*sfa)->actions_len;
  1742. int err;
  1743. err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
  1744. if (err)
  1745. return err;
  1746. return used;
  1747. }
  1748. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1749. int st_offset)
  1750. {
  1751. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1752. st_offset);
  1753. a->nla_len = sfa->actions_len - st_offset;
  1754. }
  1755. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1756. const struct sw_flow_key *key,
  1757. struct sw_flow_actions **sfa,
  1758. __be16 eth_type, __be16 vlan_tci, bool log);
  1759. static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
  1760. const struct sw_flow_key *key,
  1761. struct sw_flow_actions **sfa,
  1762. __be16 eth_type, __be16 vlan_tci,
  1763. bool log, bool last)
  1764. {
  1765. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1766. const struct nlattr *probability, *actions;
  1767. const struct nlattr *a;
  1768. int rem, start, err;
  1769. struct sample_arg arg;
  1770. memset(attrs, 0, sizeof(attrs));
  1771. nla_for_each_nested(a, attr, rem) {
  1772. int type = nla_type(a);
  1773. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1774. return -EINVAL;
  1775. attrs[type] = a;
  1776. }
  1777. if (rem)
  1778. return -EINVAL;
  1779. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1780. if (!probability || nla_len(probability) != sizeof(u32))
  1781. return -EINVAL;
  1782. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1783. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1784. return -EINVAL;
  1785. /* validation done, copy sample action. */
  1786. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1787. if (start < 0)
  1788. return start;
  1789. /* When both skb and flow may be changed, put the sample
  1790. * into a deferred fifo. On the other hand, if only skb
  1791. * may be modified, the actions can be executed in place.
  1792. *
  1793. * Do this analysis at the flow installation time.
  1794. * Set 'clone_action->exec' to true if the actions can be
  1795. * executed without being deferred.
  1796. *
  1797. * If the sample is the last action, it can always be excuted
  1798. * rather than deferred.
  1799. */
  1800. arg.exec = last || !actions_may_change_flow(actions);
  1801. arg.probability = nla_get_u32(probability);
  1802. err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
  1803. log);
  1804. if (err)
  1805. return err;
  1806. err = __ovs_nla_copy_actions(net, actions, key, sfa,
  1807. eth_type, vlan_tci, log);
  1808. if (err)
  1809. return err;
  1810. add_nested_action_end(*sfa, start);
  1811. return 0;
  1812. }
  1813. void ovs_match_init(struct sw_flow_match *match,
  1814. struct sw_flow_key *key,
  1815. bool reset_key,
  1816. struct sw_flow_mask *mask)
  1817. {
  1818. memset(match, 0, sizeof(*match));
  1819. match->key = key;
  1820. match->mask = mask;
  1821. if (reset_key)
  1822. memset(key, 0, sizeof(*key));
  1823. if (mask) {
  1824. memset(&mask->key, 0, sizeof(mask->key));
  1825. mask->range.start = mask->range.end = 0;
  1826. }
  1827. }
  1828. static int validate_geneve_opts(struct sw_flow_key *key)
  1829. {
  1830. struct geneve_opt *option;
  1831. int opts_len = key->tun_opts_len;
  1832. bool crit_opt = false;
  1833. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1834. while (opts_len > 0) {
  1835. int len;
  1836. if (opts_len < sizeof(*option))
  1837. return -EINVAL;
  1838. len = sizeof(*option) + option->length * 4;
  1839. if (len > opts_len)
  1840. return -EINVAL;
  1841. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1842. option = (struct geneve_opt *)((u8 *)option + len);
  1843. opts_len -= len;
  1844. };
  1845. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1846. return 0;
  1847. }
  1848. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1849. struct sw_flow_actions **sfa, bool log)
  1850. {
  1851. struct sw_flow_match match;
  1852. struct sw_flow_key key;
  1853. struct metadata_dst *tun_dst;
  1854. struct ip_tunnel_info *tun_info;
  1855. struct ovs_tunnel_info *ovs_tun;
  1856. struct nlattr *a;
  1857. int err = 0, start, opts_type;
  1858. ovs_match_init(&match, &key, true, NULL);
  1859. opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
  1860. if (opts_type < 0)
  1861. return opts_type;
  1862. if (key.tun_opts_len) {
  1863. switch (opts_type) {
  1864. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1865. err = validate_geneve_opts(&key);
  1866. if (err < 0)
  1867. return err;
  1868. break;
  1869. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1870. break;
  1871. }
  1872. };
  1873. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1874. if (start < 0)
  1875. return start;
  1876. tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
  1877. if (!tun_dst)
  1878. return -ENOMEM;
  1879. err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
  1880. if (err) {
  1881. dst_release((struct dst_entry *)tun_dst);
  1882. return err;
  1883. }
  1884. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1885. sizeof(*ovs_tun), log);
  1886. if (IS_ERR(a)) {
  1887. dst_release((struct dst_entry *)tun_dst);
  1888. return PTR_ERR(a);
  1889. }
  1890. ovs_tun = nla_data(a);
  1891. ovs_tun->tun_dst = tun_dst;
  1892. tun_info = &tun_dst->u.tun_info;
  1893. tun_info->mode = IP_TUNNEL_INFO_TX;
  1894. if (key.tun_proto == AF_INET6)
  1895. tun_info->mode |= IP_TUNNEL_INFO_IPV6;
  1896. tun_info->key = key.tun_key;
  1897. /* We need to store the options in the action itself since
  1898. * everything else will go away after flow setup. We can append
  1899. * it to tun_info and then point there.
  1900. */
  1901. ip_tunnel_info_opts_set(tun_info,
  1902. TUN_METADATA_OPTS(&key, key.tun_opts_len),
  1903. key.tun_opts_len);
  1904. add_nested_action_end(*sfa, start);
  1905. return err;
  1906. }
  1907. /* Return false if there are any non-masked bits set.
  1908. * Mask follows data immediately, before any netlink padding.
  1909. */
  1910. static bool validate_masked(u8 *data, int len)
  1911. {
  1912. u8 *mask = data + len;
  1913. while (len--)
  1914. if (*data++ & ~*mask++)
  1915. return false;
  1916. return true;
  1917. }
  1918. static int validate_set(const struct nlattr *a,
  1919. const struct sw_flow_key *flow_key,
  1920. struct sw_flow_actions **sfa, bool *skip_copy,
  1921. u8 mac_proto, __be16 eth_type, bool masked, bool log)
  1922. {
  1923. const struct nlattr *ovs_key = nla_data(a);
  1924. int key_type = nla_type(ovs_key);
  1925. size_t key_len;
  1926. /* There can be only one key in a action */
  1927. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1928. return -EINVAL;
  1929. key_len = nla_len(ovs_key);
  1930. if (masked)
  1931. key_len /= 2;
  1932. if (key_type > OVS_KEY_ATTR_MAX ||
  1933. !check_attr_len(key_len, ovs_key_lens[key_type].len))
  1934. return -EINVAL;
  1935. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  1936. return -EINVAL;
  1937. switch (key_type) {
  1938. const struct ovs_key_ipv4 *ipv4_key;
  1939. const struct ovs_key_ipv6 *ipv6_key;
  1940. int err;
  1941. case OVS_KEY_ATTR_PRIORITY:
  1942. case OVS_KEY_ATTR_SKB_MARK:
  1943. case OVS_KEY_ATTR_CT_MARK:
  1944. case OVS_KEY_ATTR_CT_LABELS:
  1945. break;
  1946. case OVS_KEY_ATTR_ETHERNET:
  1947. if (mac_proto != MAC_PROTO_ETHERNET)
  1948. return -EINVAL;
  1949. break;
  1950. case OVS_KEY_ATTR_TUNNEL:
  1951. if (masked)
  1952. return -EINVAL; /* Masked tunnel set not supported. */
  1953. *skip_copy = true;
  1954. err = validate_and_copy_set_tun(a, sfa, log);
  1955. if (err)
  1956. return err;
  1957. break;
  1958. case OVS_KEY_ATTR_IPV4:
  1959. if (eth_type != htons(ETH_P_IP))
  1960. return -EINVAL;
  1961. ipv4_key = nla_data(ovs_key);
  1962. if (masked) {
  1963. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  1964. /* Non-writeable fields. */
  1965. if (mask->ipv4_proto || mask->ipv4_frag)
  1966. return -EINVAL;
  1967. } else {
  1968. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1969. return -EINVAL;
  1970. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1971. return -EINVAL;
  1972. }
  1973. break;
  1974. case OVS_KEY_ATTR_IPV6:
  1975. if (eth_type != htons(ETH_P_IPV6))
  1976. return -EINVAL;
  1977. ipv6_key = nla_data(ovs_key);
  1978. if (masked) {
  1979. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  1980. /* Non-writeable fields. */
  1981. if (mask->ipv6_proto || mask->ipv6_frag)
  1982. return -EINVAL;
  1983. /* Invalid bits in the flow label mask? */
  1984. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  1985. return -EINVAL;
  1986. } else {
  1987. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1988. return -EINVAL;
  1989. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1990. return -EINVAL;
  1991. }
  1992. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1993. return -EINVAL;
  1994. break;
  1995. case OVS_KEY_ATTR_TCP:
  1996. if ((eth_type != htons(ETH_P_IP) &&
  1997. eth_type != htons(ETH_P_IPV6)) ||
  1998. flow_key->ip.proto != IPPROTO_TCP)
  1999. return -EINVAL;
  2000. break;
  2001. case OVS_KEY_ATTR_UDP:
  2002. if ((eth_type != htons(ETH_P_IP) &&
  2003. eth_type != htons(ETH_P_IPV6)) ||
  2004. flow_key->ip.proto != IPPROTO_UDP)
  2005. return -EINVAL;
  2006. break;
  2007. case OVS_KEY_ATTR_MPLS:
  2008. if (!eth_p_mpls(eth_type))
  2009. return -EINVAL;
  2010. break;
  2011. case OVS_KEY_ATTR_SCTP:
  2012. if ((eth_type != htons(ETH_P_IP) &&
  2013. eth_type != htons(ETH_P_IPV6)) ||
  2014. flow_key->ip.proto != IPPROTO_SCTP)
  2015. return -EINVAL;
  2016. break;
  2017. default:
  2018. return -EINVAL;
  2019. }
  2020. /* Convert non-masked non-tunnel set actions to masked set actions. */
  2021. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  2022. int start, len = key_len * 2;
  2023. struct nlattr *at;
  2024. *skip_copy = true;
  2025. start = add_nested_action_start(sfa,
  2026. OVS_ACTION_ATTR_SET_TO_MASKED,
  2027. log);
  2028. if (start < 0)
  2029. return start;
  2030. at = __add_action(sfa, key_type, NULL, len, log);
  2031. if (IS_ERR(at))
  2032. return PTR_ERR(at);
  2033. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  2034. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  2035. /* Clear non-writeable bits from otherwise writeable fields. */
  2036. if (key_type == OVS_KEY_ATTR_IPV6) {
  2037. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  2038. mask->ipv6_label &= htonl(0x000FFFFF);
  2039. }
  2040. add_nested_action_end(*sfa, start);
  2041. }
  2042. return 0;
  2043. }
  2044. static int validate_userspace(const struct nlattr *attr)
  2045. {
  2046. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  2047. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  2048. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  2049. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  2050. };
  2051. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  2052. int error;
  2053. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
  2054. userspace_policy, NULL);
  2055. if (error)
  2056. return error;
  2057. if (!a[OVS_USERSPACE_ATTR_PID] ||
  2058. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  2059. return -EINVAL;
  2060. return 0;
  2061. }
  2062. static int copy_action(const struct nlattr *from,
  2063. struct sw_flow_actions **sfa, bool log)
  2064. {
  2065. int totlen = NLA_ALIGN(from->nla_len);
  2066. struct nlattr *to;
  2067. to = reserve_sfa_size(sfa, from->nla_len, log);
  2068. if (IS_ERR(to))
  2069. return PTR_ERR(to);
  2070. memcpy(to, from, totlen);
  2071. return 0;
  2072. }
  2073. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2074. const struct sw_flow_key *key,
  2075. struct sw_flow_actions **sfa,
  2076. __be16 eth_type, __be16 vlan_tci, bool log)
  2077. {
  2078. u8 mac_proto = ovs_key_mac_proto(key);
  2079. const struct nlattr *a;
  2080. int rem, err;
  2081. nla_for_each_nested(a, attr, rem) {
  2082. /* Expected argument lengths, (u32)-1 for variable length. */
  2083. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  2084. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  2085. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  2086. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  2087. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  2088. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  2089. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  2090. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  2091. [OVS_ACTION_ATTR_SET] = (u32)-1,
  2092. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  2093. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  2094. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
  2095. [OVS_ACTION_ATTR_CT] = (u32)-1,
  2096. [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
  2097. [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
  2098. [OVS_ACTION_ATTR_POP_ETH] = 0,
  2099. };
  2100. const struct ovs_action_push_vlan *vlan;
  2101. int type = nla_type(a);
  2102. bool skip_copy;
  2103. if (type > OVS_ACTION_ATTR_MAX ||
  2104. (action_lens[type] != nla_len(a) &&
  2105. action_lens[type] != (u32)-1))
  2106. return -EINVAL;
  2107. skip_copy = false;
  2108. switch (type) {
  2109. case OVS_ACTION_ATTR_UNSPEC:
  2110. return -EINVAL;
  2111. case OVS_ACTION_ATTR_USERSPACE:
  2112. err = validate_userspace(a);
  2113. if (err)
  2114. return err;
  2115. break;
  2116. case OVS_ACTION_ATTR_OUTPUT:
  2117. if (nla_get_u32(a) >= DP_MAX_PORTS)
  2118. return -EINVAL;
  2119. break;
  2120. case OVS_ACTION_ATTR_TRUNC: {
  2121. const struct ovs_action_trunc *trunc = nla_data(a);
  2122. if (trunc->max_len < ETH_HLEN)
  2123. return -EINVAL;
  2124. break;
  2125. }
  2126. case OVS_ACTION_ATTR_HASH: {
  2127. const struct ovs_action_hash *act_hash = nla_data(a);
  2128. switch (act_hash->hash_alg) {
  2129. case OVS_HASH_ALG_L4:
  2130. break;
  2131. default:
  2132. return -EINVAL;
  2133. }
  2134. break;
  2135. }
  2136. case OVS_ACTION_ATTR_POP_VLAN:
  2137. if (mac_proto != MAC_PROTO_ETHERNET)
  2138. return -EINVAL;
  2139. vlan_tci = htons(0);
  2140. break;
  2141. case OVS_ACTION_ATTR_PUSH_VLAN:
  2142. if (mac_proto != MAC_PROTO_ETHERNET)
  2143. return -EINVAL;
  2144. vlan = nla_data(a);
  2145. if (!eth_type_vlan(vlan->vlan_tpid))
  2146. return -EINVAL;
  2147. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  2148. return -EINVAL;
  2149. vlan_tci = vlan->vlan_tci;
  2150. break;
  2151. case OVS_ACTION_ATTR_RECIRC:
  2152. break;
  2153. case OVS_ACTION_ATTR_PUSH_MPLS: {
  2154. const struct ovs_action_push_mpls *mpls = nla_data(a);
  2155. if (!eth_p_mpls(mpls->mpls_ethertype))
  2156. return -EINVAL;
  2157. /* Prohibit push MPLS other than to a white list
  2158. * for packets that have a known tag order.
  2159. */
  2160. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  2161. (eth_type != htons(ETH_P_IP) &&
  2162. eth_type != htons(ETH_P_IPV6) &&
  2163. eth_type != htons(ETH_P_ARP) &&
  2164. eth_type != htons(ETH_P_RARP) &&
  2165. !eth_p_mpls(eth_type)))
  2166. return -EINVAL;
  2167. eth_type = mpls->mpls_ethertype;
  2168. break;
  2169. }
  2170. case OVS_ACTION_ATTR_POP_MPLS:
  2171. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  2172. !eth_p_mpls(eth_type))
  2173. return -EINVAL;
  2174. /* Disallow subsequent L2.5+ set and mpls_pop actions
  2175. * as there is no check here to ensure that the new
  2176. * eth_type is valid and thus set actions could
  2177. * write off the end of the packet or otherwise
  2178. * corrupt it.
  2179. *
  2180. * Support for these actions is planned using packet
  2181. * recirculation.
  2182. */
  2183. eth_type = htons(0);
  2184. break;
  2185. case OVS_ACTION_ATTR_SET:
  2186. err = validate_set(a, key, sfa,
  2187. &skip_copy, mac_proto, eth_type,
  2188. false, log);
  2189. if (err)
  2190. return err;
  2191. break;
  2192. case OVS_ACTION_ATTR_SET_MASKED:
  2193. err = validate_set(a, key, sfa,
  2194. &skip_copy, mac_proto, eth_type,
  2195. true, log);
  2196. if (err)
  2197. return err;
  2198. break;
  2199. case OVS_ACTION_ATTR_SAMPLE: {
  2200. bool last = nla_is_last(a, rem);
  2201. err = validate_and_copy_sample(net, a, key, sfa,
  2202. eth_type, vlan_tci,
  2203. log, last);
  2204. if (err)
  2205. return err;
  2206. skip_copy = true;
  2207. break;
  2208. }
  2209. case OVS_ACTION_ATTR_CT:
  2210. err = ovs_ct_copy_action(net, a, key, sfa, log);
  2211. if (err)
  2212. return err;
  2213. skip_copy = true;
  2214. break;
  2215. case OVS_ACTION_ATTR_PUSH_ETH:
  2216. /* Disallow pushing an Ethernet header if one
  2217. * is already present */
  2218. if (mac_proto != MAC_PROTO_NONE)
  2219. return -EINVAL;
  2220. mac_proto = MAC_PROTO_NONE;
  2221. break;
  2222. case OVS_ACTION_ATTR_POP_ETH:
  2223. if (mac_proto != MAC_PROTO_ETHERNET)
  2224. return -EINVAL;
  2225. if (vlan_tci & htons(VLAN_TAG_PRESENT))
  2226. return -EINVAL;
  2227. mac_proto = MAC_PROTO_ETHERNET;
  2228. break;
  2229. default:
  2230. OVS_NLERR(log, "Unknown Action type %d", type);
  2231. return -EINVAL;
  2232. }
  2233. if (!skip_copy) {
  2234. err = copy_action(a, sfa, log);
  2235. if (err)
  2236. return err;
  2237. }
  2238. }
  2239. if (rem > 0)
  2240. return -EINVAL;
  2241. return 0;
  2242. }
  2243. /* 'key' must be the masked key. */
  2244. int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2245. const struct sw_flow_key *key,
  2246. struct sw_flow_actions **sfa, bool log)
  2247. {
  2248. int err;
  2249. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  2250. if (IS_ERR(*sfa))
  2251. return PTR_ERR(*sfa);
  2252. (*sfa)->orig_len = nla_len(attr);
  2253. err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
  2254. key->eth.vlan.tci, log);
  2255. if (err)
  2256. ovs_nla_free_flow_actions(*sfa);
  2257. return err;
  2258. }
  2259. static int sample_action_to_attr(const struct nlattr *attr,
  2260. struct sk_buff *skb)
  2261. {
  2262. struct nlattr *start, *ac_start = NULL, *sample_arg;
  2263. int err = 0, rem = nla_len(attr);
  2264. const struct sample_arg *arg;
  2265. struct nlattr *actions;
  2266. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  2267. if (!start)
  2268. return -EMSGSIZE;
  2269. sample_arg = nla_data(attr);
  2270. arg = nla_data(sample_arg);
  2271. actions = nla_next(sample_arg, &rem);
  2272. if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
  2273. err = -EMSGSIZE;
  2274. goto out;
  2275. }
  2276. ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  2277. if (!ac_start) {
  2278. err = -EMSGSIZE;
  2279. goto out;
  2280. }
  2281. err = ovs_nla_put_actions(actions, rem, skb);
  2282. out:
  2283. if (err) {
  2284. nla_nest_cancel(skb, ac_start);
  2285. nla_nest_cancel(skb, start);
  2286. } else {
  2287. nla_nest_end(skb, ac_start);
  2288. nla_nest_end(skb, start);
  2289. }
  2290. return err;
  2291. }
  2292. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  2293. {
  2294. const struct nlattr *ovs_key = nla_data(a);
  2295. int key_type = nla_type(ovs_key);
  2296. struct nlattr *start;
  2297. int err;
  2298. switch (key_type) {
  2299. case OVS_KEY_ATTR_TUNNEL_INFO: {
  2300. struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
  2301. struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
  2302. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2303. if (!start)
  2304. return -EMSGSIZE;
  2305. err = ip_tun_to_nlattr(skb, &tun_info->key,
  2306. ip_tunnel_info_opts(tun_info),
  2307. tun_info->options_len,
  2308. ip_tunnel_info_af(tun_info));
  2309. if (err)
  2310. return err;
  2311. nla_nest_end(skb, start);
  2312. break;
  2313. }
  2314. default:
  2315. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  2316. return -EMSGSIZE;
  2317. break;
  2318. }
  2319. return 0;
  2320. }
  2321. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  2322. struct sk_buff *skb)
  2323. {
  2324. const struct nlattr *ovs_key = nla_data(a);
  2325. struct nlattr *nla;
  2326. size_t key_len = nla_len(ovs_key) / 2;
  2327. /* Revert the conversion we did from a non-masked set action to
  2328. * masked set action.
  2329. */
  2330. nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2331. if (!nla)
  2332. return -EMSGSIZE;
  2333. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  2334. return -EMSGSIZE;
  2335. nla_nest_end(skb, nla);
  2336. return 0;
  2337. }
  2338. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  2339. {
  2340. const struct nlattr *a;
  2341. int rem, err;
  2342. nla_for_each_attr(a, attr, len, rem) {
  2343. int type = nla_type(a);
  2344. switch (type) {
  2345. case OVS_ACTION_ATTR_SET:
  2346. err = set_action_to_attr(a, skb);
  2347. if (err)
  2348. return err;
  2349. break;
  2350. case OVS_ACTION_ATTR_SET_TO_MASKED:
  2351. err = masked_set_action_to_set_action_attr(a, skb);
  2352. if (err)
  2353. return err;
  2354. break;
  2355. case OVS_ACTION_ATTR_SAMPLE:
  2356. err = sample_action_to_attr(a, skb);
  2357. if (err)
  2358. return err;
  2359. break;
  2360. case OVS_ACTION_ATTR_CT:
  2361. err = ovs_ct_action_to_attr(nla_data(a), skb);
  2362. if (err)
  2363. return err;
  2364. break;
  2365. default:
  2366. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  2367. return -EMSGSIZE;
  2368. break;
  2369. }
  2370. }
  2371. return 0;
  2372. }