flow_netlink.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "flow.h"
  20. #include "datapath.h"
  21. #include <linux/uaccess.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <net/llc_pdu.h>
  27. #include <linux/kernel.h>
  28. #include <linux/jhash.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/llc.h>
  31. #include <linux/module.h>
  32. #include <linux/in.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/ip.h>
  36. #include <linux/ipv6.h>
  37. #include <linux/sctp.h>
  38. #include <linux/tcp.h>
  39. #include <linux/udp.h>
  40. #include <linux/icmp.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/rculist.h>
  43. #include <net/geneve.h>
  44. #include <net/ip.h>
  45. #include <net/ipv6.h>
  46. #include <net/ndisc.h>
  47. #include <net/mpls.h>
  48. #include "flow_netlink.h"
  49. #include "vport-vxlan.h"
  50. struct ovs_len_tbl {
  51. int len;
  52. const struct ovs_len_tbl *next;
  53. };
  54. #define OVS_ATTR_NESTED -1
  55. static void update_range(struct sw_flow_match *match,
  56. size_t offset, size_t size, bool is_mask)
  57. {
  58. struct sw_flow_key_range *range;
  59. size_t start = rounddown(offset, sizeof(long));
  60. size_t end = roundup(offset + size, sizeof(long));
  61. if (!is_mask)
  62. range = &match->range;
  63. else
  64. range = &match->mask->range;
  65. if (range->start == range->end) {
  66. range->start = start;
  67. range->end = end;
  68. return;
  69. }
  70. if (range->start > start)
  71. range->start = start;
  72. if (range->end < end)
  73. range->end = end;
  74. }
  75. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  76. do { \
  77. update_range(match, offsetof(struct sw_flow_key, field), \
  78. sizeof((match)->key->field), is_mask); \
  79. if (is_mask) \
  80. (match)->mask->key.field = value; \
  81. else \
  82. (match)->key->field = value; \
  83. } while (0)
  84. #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
  85. do { \
  86. update_range(match, offset, len, is_mask); \
  87. if (is_mask) \
  88. memcpy((u8 *)&(match)->mask->key + offset, value_p, \
  89. len); \
  90. else \
  91. memcpy((u8 *)(match)->key + offset, value_p, len); \
  92. } while (0)
  93. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  94. SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
  95. value_p, len, is_mask)
  96. #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
  97. do { \
  98. update_range(match, offsetof(struct sw_flow_key, field), \
  99. sizeof((match)->key->field), is_mask); \
  100. if (is_mask) \
  101. memset((u8 *)&(match)->mask->key.field, value, \
  102. sizeof((match)->mask->key.field)); \
  103. else \
  104. memset((u8 *)&(match)->key->field, value, \
  105. sizeof((match)->key->field)); \
  106. } while (0)
  107. static bool match_validate(const struct sw_flow_match *match,
  108. u64 key_attrs, u64 mask_attrs, bool log)
  109. {
  110. u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
  111. u64 mask_allowed = key_attrs; /* At most allow all key attributes */
  112. /* The following mask attributes allowed only if they
  113. * pass the validation tests. */
  114. mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
  115. | (1 << OVS_KEY_ATTR_IPV6)
  116. | (1 << OVS_KEY_ATTR_TCP)
  117. | (1 << OVS_KEY_ATTR_TCP_FLAGS)
  118. | (1 << OVS_KEY_ATTR_UDP)
  119. | (1 << OVS_KEY_ATTR_SCTP)
  120. | (1 << OVS_KEY_ATTR_ICMP)
  121. | (1 << OVS_KEY_ATTR_ICMPV6)
  122. | (1 << OVS_KEY_ATTR_ARP)
  123. | (1 << OVS_KEY_ATTR_ND)
  124. | (1 << OVS_KEY_ATTR_MPLS));
  125. /* Always allowed mask fields. */
  126. mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
  127. | (1 << OVS_KEY_ATTR_IN_PORT)
  128. | (1 << OVS_KEY_ATTR_ETHERTYPE));
  129. /* Check key attributes. */
  130. if (match->key->eth.type == htons(ETH_P_ARP)
  131. || match->key->eth.type == htons(ETH_P_RARP)) {
  132. key_expected |= 1 << OVS_KEY_ATTR_ARP;
  133. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  134. mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
  135. }
  136. if (eth_p_mpls(match->key->eth.type)) {
  137. key_expected |= 1 << OVS_KEY_ATTR_MPLS;
  138. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  139. mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
  140. }
  141. if (match->key->eth.type == htons(ETH_P_IP)) {
  142. key_expected |= 1 << OVS_KEY_ATTR_IPV4;
  143. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  144. mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
  145. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  146. if (match->key->ip.proto == IPPROTO_UDP) {
  147. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  148. if (match->mask && (match->mask->key.ip.proto == 0xff))
  149. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  150. }
  151. if (match->key->ip.proto == IPPROTO_SCTP) {
  152. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  153. if (match->mask && (match->mask->key.ip.proto == 0xff))
  154. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  155. }
  156. if (match->key->ip.proto == IPPROTO_TCP) {
  157. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  158. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  159. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  160. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  161. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  162. }
  163. }
  164. if (match->key->ip.proto == IPPROTO_ICMP) {
  165. key_expected |= 1 << OVS_KEY_ATTR_ICMP;
  166. if (match->mask && (match->mask->key.ip.proto == 0xff))
  167. mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
  168. }
  169. }
  170. }
  171. if (match->key->eth.type == htons(ETH_P_IPV6)) {
  172. key_expected |= 1 << OVS_KEY_ATTR_IPV6;
  173. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  174. mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
  175. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  176. if (match->key->ip.proto == IPPROTO_UDP) {
  177. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  178. if (match->mask && (match->mask->key.ip.proto == 0xff))
  179. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  180. }
  181. if (match->key->ip.proto == IPPROTO_SCTP) {
  182. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  183. if (match->mask && (match->mask->key.ip.proto == 0xff))
  184. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  185. }
  186. if (match->key->ip.proto == IPPROTO_TCP) {
  187. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  188. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  189. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  190. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  191. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  192. }
  193. }
  194. if (match->key->ip.proto == IPPROTO_ICMPV6) {
  195. key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
  196. if (match->mask && (match->mask->key.ip.proto == 0xff))
  197. mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
  198. if (match->key->tp.src ==
  199. htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  200. match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  201. key_expected |= 1 << OVS_KEY_ATTR_ND;
  202. if (match->mask && (match->mask->key.tp.src == htons(0xff)))
  203. mask_allowed |= 1 << OVS_KEY_ATTR_ND;
  204. }
  205. }
  206. }
  207. }
  208. if ((key_attrs & key_expected) != key_expected) {
  209. /* Key attributes check failed. */
  210. OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
  211. (unsigned long long)key_attrs,
  212. (unsigned long long)key_expected);
  213. return false;
  214. }
  215. if ((mask_attrs & mask_allowed) != mask_attrs) {
  216. /* Mask attributes check failed. */
  217. OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
  218. (unsigned long long)mask_attrs,
  219. (unsigned long long)mask_allowed);
  220. return false;
  221. }
  222. return true;
  223. }
  224. size_t ovs_tun_key_attr_size(void)
  225. {
  226. /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
  227. * updating this function.
  228. */
  229. return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  230. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  231. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  232. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  233. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  234. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  235. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  236. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  237. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  238. /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
  239. * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
  240. */
  241. + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
  242. + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
  243. }
  244. size_t ovs_key_attr_size(void)
  245. {
  246. /* Whenever adding new OVS_KEY_ FIELDS, we should consider
  247. * updating this function.
  248. */
  249. BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
  250. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  251. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  252. + ovs_tun_key_attr_size()
  253. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  254. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  255. + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
  256. + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
  257. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  258. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  259. + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
  260. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  261. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  262. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  263. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  264. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  265. }
  266. static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  267. [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
  268. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
  269. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
  270. [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
  271. [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
  272. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
  273. [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
  274. [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
  275. [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
  276. [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
  277. [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
  278. [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED },
  279. };
  280. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  281. static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  282. [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
  283. [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
  284. [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
  285. [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
  286. [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
  287. [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
  288. [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
  289. [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
  290. [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
  291. [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
  292. [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
  293. [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
  294. [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
  295. [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
  296. [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
  297. [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
  298. [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
  299. [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
  300. [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
  301. [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
  302. .next = ovs_tunnel_key_lens, },
  303. [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
  304. };
  305. static bool is_all_zero(const u8 *fp, size_t size)
  306. {
  307. int i;
  308. if (!fp)
  309. return false;
  310. for (i = 0; i < size; i++)
  311. if (fp[i])
  312. return false;
  313. return true;
  314. }
  315. static int __parse_flow_nlattrs(const struct nlattr *attr,
  316. const struct nlattr *a[],
  317. u64 *attrsp, bool log, bool nz)
  318. {
  319. const struct nlattr *nla;
  320. u64 attrs;
  321. int rem;
  322. attrs = *attrsp;
  323. nla_for_each_nested(nla, attr, rem) {
  324. u16 type = nla_type(nla);
  325. int expected_len;
  326. if (type > OVS_KEY_ATTR_MAX) {
  327. OVS_NLERR(log, "Key type %d is out of range max %d",
  328. type, OVS_KEY_ATTR_MAX);
  329. return -EINVAL;
  330. }
  331. if (attrs & (1 << type)) {
  332. OVS_NLERR(log, "Duplicate key (type %d).", type);
  333. return -EINVAL;
  334. }
  335. expected_len = ovs_key_lens[type].len;
  336. if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
  337. OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
  338. type, nla_len(nla), expected_len);
  339. return -EINVAL;
  340. }
  341. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  342. attrs |= 1 << type;
  343. a[type] = nla;
  344. }
  345. }
  346. if (rem) {
  347. OVS_NLERR(log, "Message has %d unknown bytes.", rem);
  348. return -EINVAL;
  349. }
  350. *attrsp = attrs;
  351. return 0;
  352. }
  353. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  354. const struct nlattr *a[], u64 *attrsp,
  355. bool log)
  356. {
  357. return __parse_flow_nlattrs(attr, a, attrsp, log, true);
  358. }
  359. static int parse_flow_nlattrs(const struct nlattr *attr,
  360. const struct nlattr *a[], u64 *attrsp,
  361. bool log)
  362. {
  363. return __parse_flow_nlattrs(attr, a, attrsp, log, false);
  364. }
  365. static int genev_tun_opt_from_nlattr(const struct nlattr *a,
  366. struct sw_flow_match *match, bool is_mask,
  367. bool log)
  368. {
  369. unsigned long opt_key_offset;
  370. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  371. OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
  372. nla_len(a), sizeof(match->key->tun_opts));
  373. return -EINVAL;
  374. }
  375. if (nla_len(a) % 4 != 0) {
  376. OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
  377. nla_len(a));
  378. return -EINVAL;
  379. }
  380. /* We need to record the length of the options passed
  381. * down, otherwise packets with the same format but
  382. * additional options will be silently matched.
  383. */
  384. if (!is_mask) {
  385. SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
  386. false);
  387. } else {
  388. /* This is somewhat unusual because it looks at
  389. * both the key and mask while parsing the
  390. * attributes (and by extension assumes the key
  391. * is parsed first). Normally, we would verify
  392. * that each is the correct length and that the
  393. * attributes line up in the validate function.
  394. * However, that is difficult because this is
  395. * variable length and we won't have the
  396. * information later.
  397. */
  398. if (match->key->tun_opts_len != nla_len(a)) {
  399. OVS_NLERR(log, "Geneve option len %d != mask len %d",
  400. match->key->tun_opts_len, nla_len(a));
  401. return -EINVAL;
  402. }
  403. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  404. }
  405. opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
  406. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
  407. nla_len(a), is_mask);
  408. return 0;
  409. }
  410. static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
  411. [OVS_VXLAN_EXT_GBP] = { .type = NLA_U32 },
  412. };
  413. static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
  414. struct sw_flow_match *match, bool is_mask,
  415. bool log)
  416. {
  417. struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
  418. unsigned long opt_key_offset;
  419. struct ovs_vxlan_opts opts;
  420. int err;
  421. BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
  422. err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
  423. if (err < 0)
  424. return err;
  425. memset(&opts, 0, sizeof(opts));
  426. if (tb[OVS_VXLAN_EXT_GBP])
  427. opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
  428. if (!is_mask)
  429. SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
  430. else
  431. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  432. opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
  433. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
  434. is_mask);
  435. return 0;
  436. }
  437. static int ipv4_tun_from_nlattr(const struct nlattr *attr,
  438. struct sw_flow_match *match, bool is_mask,
  439. bool log)
  440. {
  441. struct nlattr *a;
  442. int rem;
  443. bool ttl = false;
  444. __be16 tun_flags = 0;
  445. int opts_type = 0;
  446. nla_for_each_nested(a, attr, rem) {
  447. int type = nla_type(a);
  448. int err;
  449. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  450. OVS_NLERR(log, "Tunnel attr %d out of range max %d",
  451. type, OVS_TUNNEL_KEY_ATTR_MAX);
  452. return -EINVAL;
  453. }
  454. if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
  455. ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
  456. OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
  457. type, nla_len(a), ovs_tunnel_key_lens[type].len);
  458. return -EINVAL;
  459. }
  460. switch (type) {
  461. case OVS_TUNNEL_KEY_ATTR_ID:
  462. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  463. nla_get_be64(a), is_mask);
  464. tun_flags |= TUNNEL_KEY;
  465. break;
  466. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  467. SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
  468. nla_get_be32(a), is_mask);
  469. break;
  470. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  471. SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
  472. nla_get_be32(a), is_mask);
  473. break;
  474. case OVS_TUNNEL_KEY_ATTR_TOS:
  475. SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
  476. nla_get_u8(a), is_mask);
  477. break;
  478. case OVS_TUNNEL_KEY_ATTR_TTL:
  479. SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
  480. nla_get_u8(a), is_mask);
  481. ttl = true;
  482. break;
  483. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  484. tun_flags |= TUNNEL_DONT_FRAGMENT;
  485. break;
  486. case OVS_TUNNEL_KEY_ATTR_CSUM:
  487. tun_flags |= TUNNEL_CSUM;
  488. break;
  489. case OVS_TUNNEL_KEY_ATTR_TP_SRC:
  490. SW_FLOW_KEY_PUT(match, tun_key.tp_src,
  491. nla_get_be16(a), is_mask);
  492. break;
  493. case OVS_TUNNEL_KEY_ATTR_TP_DST:
  494. SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
  495. nla_get_be16(a), is_mask);
  496. break;
  497. case OVS_TUNNEL_KEY_ATTR_OAM:
  498. tun_flags |= TUNNEL_OAM;
  499. break;
  500. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  501. if (opts_type) {
  502. OVS_NLERR(log, "Multiple metadata blocks provided");
  503. return -EINVAL;
  504. }
  505. err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
  506. if (err)
  507. return err;
  508. tun_flags |= TUNNEL_GENEVE_OPT;
  509. opts_type = type;
  510. break;
  511. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  512. if (opts_type) {
  513. OVS_NLERR(log, "Multiple metadata blocks provided");
  514. return -EINVAL;
  515. }
  516. err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
  517. if (err)
  518. return err;
  519. tun_flags |= TUNNEL_VXLAN_OPT;
  520. opts_type = type;
  521. break;
  522. default:
  523. OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
  524. type);
  525. return -EINVAL;
  526. }
  527. }
  528. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  529. if (rem > 0) {
  530. OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
  531. rem);
  532. return -EINVAL;
  533. }
  534. if (!is_mask) {
  535. if (!match->key->tun_key.ipv4_dst) {
  536. OVS_NLERR(log, "IPv4 tunnel dst address is zero");
  537. return -EINVAL;
  538. }
  539. if (!ttl) {
  540. OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
  541. return -EINVAL;
  542. }
  543. }
  544. return opts_type;
  545. }
  546. static int vxlan_opt_to_nlattr(struct sk_buff *skb,
  547. const void *tun_opts, int swkey_tun_opts_len)
  548. {
  549. const struct ovs_vxlan_opts *opts = tun_opts;
  550. struct nlattr *nla;
  551. nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
  552. if (!nla)
  553. return -EMSGSIZE;
  554. if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
  555. return -EMSGSIZE;
  556. nla_nest_end(skb, nla);
  557. return 0;
  558. }
  559. static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
  560. const struct ovs_key_ipv4_tunnel *output,
  561. const void *tun_opts, int swkey_tun_opts_len)
  562. {
  563. if (output->tun_flags & TUNNEL_KEY &&
  564. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  565. return -EMSGSIZE;
  566. if (output->ipv4_src &&
  567. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
  568. return -EMSGSIZE;
  569. if (output->ipv4_dst &&
  570. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
  571. return -EMSGSIZE;
  572. if (output->ipv4_tos &&
  573. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  574. return -EMSGSIZE;
  575. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  576. return -EMSGSIZE;
  577. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  578. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  579. return -EMSGSIZE;
  580. if ((output->tun_flags & TUNNEL_CSUM) &&
  581. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  582. return -EMSGSIZE;
  583. if (output->tp_src &&
  584. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
  585. return -EMSGSIZE;
  586. if (output->tp_dst &&
  587. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
  588. return -EMSGSIZE;
  589. if ((output->tun_flags & TUNNEL_OAM) &&
  590. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  591. return -EMSGSIZE;
  592. if (tun_opts) {
  593. if (output->tun_flags & TUNNEL_GENEVE_OPT &&
  594. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  595. swkey_tun_opts_len, tun_opts))
  596. return -EMSGSIZE;
  597. else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
  598. vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
  599. return -EMSGSIZE;
  600. }
  601. return 0;
  602. }
  603. static int ipv4_tun_to_nlattr(struct sk_buff *skb,
  604. const struct ovs_key_ipv4_tunnel *output,
  605. const void *tun_opts, int swkey_tun_opts_len)
  606. {
  607. struct nlattr *nla;
  608. int err;
  609. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  610. if (!nla)
  611. return -EMSGSIZE;
  612. err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
  613. if (err)
  614. return err;
  615. nla_nest_end(skb, nla);
  616. return 0;
  617. }
  618. int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
  619. const struct ovs_tunnel_info *egress_tun_info)
  620. {
  621. return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
  622. egress_tun_info->options,
  623. egress_tun_info->options_len);
  624. }
  625. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  626. const struct nlattr **a, bool is_mask,
  627. bool log)
  628. {
  629. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  630. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  631. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  632. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  633. }
  634. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  635. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  636. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  637. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  638. }
  639. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  640. SW_FLOW_KEY_PUT(match, phy.priority,
  641. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  642. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  643. }
  644. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  645. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  646. if (is_mask) {
  647. in_port = 0xffffffff; /* Always exact match in_port. */
  648. } else if (in_port >= DP_MAX_PORTS) {
  649. OVS_NLERR(log, "Port %d exceeds max allowable %d",
  650. in_port, DP_MAX_PORTS);
  651. return -EINVAL;
  652. }
  653. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  654. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  655. } else if (!is_mask) {
  656. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  657. }
  658. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  659. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  660. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  661. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  662. }
  663. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  664. if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  665. is_mask, log) < 0)
  666. return -EINVAL;
  667. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  668. }
  669. return 0;
  670. }
  671. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  672. const struct nlattr **a, bool is_mask,
  673. bool log)
  674. {
  675. int err;
  676. err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
  677. if (err)
  678. return err;
  679. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  680. const struct ovs_key_ethernet *eth_key;
  681. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  682. SW_FLOW_KEY_MEMCPY(match, eth.src,
  683. eth_key->eth_src, ETH_ALEN, is_mask);
  684. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  685. eth_key->eth_dst, ETH_ALEN, is_mask);
  686. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  687. }
  688. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  689. __be16 tci;
  690. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  691. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  692. if (is_mask)
  693. OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
  694. else
  695. OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
  696. return -EINVAL;
  697. }
  698. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  699. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  700. }
  701. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  702. __be16 eth_type;
  703. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  704. if (is_mask) {
  705. /* Always exact match EtherType. */
  706. eth_type = htons(0xffff);
  707. } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
  708. OVS_NLERR(log, "EtherType %x is less than min %x",
  709. ntohs(eth_type), ETH_P_802_3_MIN);
  710. return -EINVAL;
  711. }
  712. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  713. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  714. } else if (!is_mask) {
  715. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  716. }
  717. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  718. const struct ovs_key_ipv4 *ipv4_key;
  719. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  720. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  721. OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
  722. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  723. return -EINVAL;
  724. }
  725. SW_FLOW_KEY_PUT(match, ip.proto,
  726. ipv4_key->ipv4_proto, is_mask);
  727. SW_FLOW_KEY_PUT(match, ip.tos,
  728. ipv4_key->ipv4_tos, is_mask);
  729. SW_FLOW_KEY_PUT(match, ip.ttl,
  730. ipv4_key->ipv4_ttl, is_mask);
  731. SW_FLOW_KEY_PUT(match, ip.frag,
  732. ipv4_key->ipv4_frag, is_mask);
  733. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  734. ipv4_key->ipv4_src, is_mask);
  735. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  736. ipv4_key->ipv4_dst, is_mask);
  737. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  738. }
  739. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  740. const struct ovs_key_ipv6 *ipv6_key;
  741. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  742. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  743. OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
  744. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  745. return -EINVAL;
  746. }
  747. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  748. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
  749. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  750. return -EINVAL;
  751. }
  752. SW_FLOW_KEY_PUT(match, ipv6.label,
  753. ipv6_key->ipv6_label, is_mask);
  754. SW_FLOW_KEY_PUT(match, ip.proto,
  755. ipv6_key->ipv6_proto, is_mask);
  756. SW_FLOW_KEY_PUT(match, ip.tos,
  757. ipv6_key->ipv6_tclass, is_mask);
  758. SW_FLOW_KEY_PUT(match, ip.ttl,
  759. ipv6_key->ipv6_hlimit, is_mask);
  760. SW_FLOW_KEY_PUT(match, ip.frag,
  761. ipv6_key->ipv6_frag, is_mask);
  762. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  763. ipv6_key->ipv6_src,
  764. sizeof(match->key->ipv6.addr.src),
  765. is_mask);
  766. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  767. ipv6_key->ipv6_dst,
  768. sizeof(match->key->ipv6.addr.dst),
  769. is_mask);
  770. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  771. }
  772. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  773. const struct ovs_key_arp *arp_key;
  774. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  775. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  776. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  777. arp_key->arp_op);
  778. return -EINVAL;
  779. }
  780. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  781. arp_key->arp_sip, is_mask);
  782. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  783. arp_key->arp_tip, is_mask);
  784. SW_FLOW_KEY_PUT(match, ip.proto,
  785. ntohs(arp_key->arp_op), is_mask);
  786. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  787. arp_key->arp_sha, ETH_ALEN, is_mask);
  788. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  789. arp_key->arp_tha, ETH_ALEN, is_mask);
  790. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  791. }
  792. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  793. const struct ovs_key_mpls *mpls_key;
  794. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  795. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  796. mpls_key->mpls_lse, is_mask);
  797. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  798. }
  799. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  800. const struct ovs_key_tcp *tcp_key;
  801. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  802. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  803. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  804. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  805. }
  806. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  807. SW_FLOW_KEY_PUT(match, tp.flags,
  808. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  809. is_mask);
  810. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  811. }
  812. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  813. const struct ovs_key_udp *udp_key;
  814. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  815. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  816. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  817. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  818. }
  819. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  820. const struct ovs_key_sctp *sctp_key;
  821. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  822. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  823. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  824. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  825. }
  826. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  827. const struct ovs_key_icmp *icmp_key;
  828. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  829. SW_FLOW_KEY_PUT(match, tp.src,
  830. htons(icmp_key->icmp_type), is_mask);
  831. SW_FLOW_KEY_PUT(match, tp.dst,
  832. htons(icmp_key->icmp_code), is_mask);
  833. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  834. }
  835. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  836. const struct ovs_key_icmpv6 *icmpv6_key;
  837. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  838. SW_FLOW_KEY_PUT(match, tp.src,
  839. htons(icmpv6_key->icmpv6_type), is_mask);
  840. SW_FLOW_KEY_PUT(match, tp.dst,
  841. htons(icmpv6_key->icmpv6_code), is_mask);
  842. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  843. }
  844. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  845. const struct ovs_key_nd *nd_key;
  846. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  847. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  848. nd_key->nd_target,
  849. sizeof(match->key->ipv6.nd.target),
  850. is_mask);
  851. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  852. nd_key->nd_sll, ETH_ALEN, is_mask);
  853. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  854. nd_key->nd_tll, ETH_ALEN, is_mask);
  855. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  856. }
  857. if (attrs != 0) {
  858. OVS_NLERR(log, "Unknown key attributes %llx",
  859. (unsigned long long)attrs);
  860. return -EINVAL;
  861. }
  862. return 0;
  863. }
  864. static void nlattr_set(struct nlattr *attr, u8 val,
  865. const struct ovs_len_tbl *tbl)
  866. {
  867. struct nlattr *nla;
  868. int rem;
  869. /* The nlattr stream should already have been validated */
  870. nla_for_each_nested(nla, attr, rem) {
  871. if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
  872. nlattr_set(nla, val, tbl[nla_type(nla)].next);
  873. else
  874. memset(nla_data(nla), val, nla_len(nla));
  875. }
  876. }
  877. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  878. {
  879. nlattr_set(attr, val, ovs_key_lens);
  880. }
  881. /**
  882. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  883. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  884. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  885. * does not include any don't care bit.
  886. * @match: receives the extracted flow match information.
  887. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  888. * sequence. The fields should of the packet that triggered the creation
  889. * of this flow.
  890. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  891. * attribute specifies the mask field of the wildcarded flow.
  892. * @log: Boolean to allow kernel error logging. Normally true, but when
  893. * probing for feature compatibility this should be passed in as false to
  894. * suppress unnecessary error logging.
  895. */
  896. int ovs_nla_get_match(struct sw_flow_match *match,
  897. const struct nlattr *nla_key,
  898. const struct nlattr *nla_mask,
  899. bool log)
  900. {
  901. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  902. const struct nlattr *encap;
  903. struct nlattr *newmask = NULL;
  904. u64 key_attrs = 0;
  905. u64 mask_attrs = 0;
  906. bool encap_valid = false;
  907. int err;
  908. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  909. if (err)
  910. return err;
  911. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  912. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  913. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  914. __be16 tci;
  915. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  916. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  917. OVS_NLERR(log, "Invalid Vlan frame.");
  918. return -EINVAL;
  919. }
  920. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  921. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  922. encap = a[OVS_KEY_ATTR_ENCAP];
  923. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  924. encap_valid = true;
  925. if (tci & htons(VLAN_TAG_PRESENT)) {
  926. err = parse_flow_nlattrs(encap, a, &key_attrs, log);
  927. if (err)
  928. return err;
  929. } else if (!tci) {
  930. /* Corner case for truncated 802.1Q header. */
  931. if (nla_len(encap)) {
  932. OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
  933. return -EINVAL;
  934. }
  935. } else {
  936. OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
  937. return -EINVAL;
  938. }
  939. }
  940. err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
  941. if (err)
  942. return err;
  943. if (match->mask) {
  944. if (!nla_mask) {
  945. /* Create an exact match mask. We need to set to 0xff
  946. * all the 'match->mask' fields that have been touched
  947. * in 'match->key'. We cannot simply memset
  948. * 'match->mask', because padding bytes and fields not
  949. * specified in 'match->key' should be left to 0.
  950. * Instead, we use a stream of netlink attributes,
  951. * copied from 'key' and set to 0xff.
  952. * ovs_key_from_nlattrs() will take care of filling
  953. * 'match->mask' appropriately.
  954. */
  955. newmask = kmemdup(nla_key,
  956. nla_total_size(nla_len(nla_key)),
  957. GFP_KERNEL);
  958. if (!newmask)
  959. return -ENOMEM;
  960. mask_set_nlattr(newmask, 0xff);
  961. /* The userspace does not send tunnel attributes that
  962. * are 0, but we should not wildcard them nonetheless.
  963. */
  964. if (match->key->tun_key.ipv4_dst)
  965. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  966. 0xff, true);
  967. nla_mask = newmask;
  968. }
  969. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  970. if (err)
  971. goto free_newmask;
  972. /* Always match on tci. */
  973. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  974. if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
  975. __be16 eth_type = 0;
  976. __be16 tci = 0;
  977. if (!encap_valid) {
  978. OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
  979. err = -EINVAL;
  980. goto free_newmask;
  981. }
  982. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  983. if (a[OVS_KEY_ATTR_ETHERTYPE])
  984. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  985. if (eth_type == htons(0xffff)) {
  986. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  987. encap = a[OVS_KEY_ATTR_ENCAP];
  988. err = parse_flow_mask_nlattrs(encap, a,
  989. &mask_attrs, log);
  990. if (err)
  991. goto free_newmask;
  992. } else {
  993. OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
  994. ntohs(eth_type));
  995. err = -EINVAL;
  996. goto free_newmask;
  997. }
  998. if (a[OVS_KEY_ATTR_VLAN])
  999. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1000. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1001. OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
  1002. ntohs(tci));
  1003. err = -EINVAL;
  1004. goto free_newmask;
  1005. }
  1006. }
  1007. err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
  1008. if (err)
  1009. goto free_newmask;
  1010. }
  1011. if (!match_validate(match, key_attrs, mask_attrs, log))
  1012. err = -EINVAL;
  1013. free_newmask:
  1014. kfree(newmask);
  1015. return err;
  1016. }
  1017. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1018. {
  1019. size_t len;
  1020. if (!attr)
  1021. return 0;
  1022. len = nla_len(attr);
  1023. if (len < 1 || len > MAX_UFID_LENGTH) {
  1024. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1025. nla_len(attr), MAX_UFID_LENGTH);
  1026. return 0;
  1027. }
  1028. return len;
  1029. }
  1030. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1031. * or false otherwise.
  1032. */
  1033. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1034. bool log)
  1035. {
  1036. sfid->ufid_len = get_ufid_len(attr, log);
  1037. if (sfid->ufid_len)
  1038. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1039. return sfid->ufid_len;
  1040. }
  1041. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1042. const struct sw_flow_key *key, bool log)
  1043. {
  1044. struct sw_flow_key *new_key;
  1045. if (ovs_nla_get_ufid(sfid, ufid, log))
  1046. return 0;
  1047. /* If UFID was not provided, use unmasked key. */
  1048. new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
  1049. if (!new_key)
  1050. return -ENOMEM;
  1051. memcpy(new_key, key, sizeof(*key));
  1052. sfid->unmasked_key = new_key;
  1053. return 0;
  1054. }
  1055. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1056. {
  1057. return attr ? nla_get_u32(attr) : 0;
  1058. }
  1059. /**
  1060. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1061. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  1062. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1063. * sequence.
  1064. * @log: Boolean to allow kernel error logging. Normally true, but when
  1065. * probing for feature compatibility this should be passed in as false to
  1066. * suppress unnecessary error logging.
  1067. *
  1068. * This parses a series of Netlink attributes that form a flow key, which must
  1069. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1070. * get the metadata, that is, the parts of the flow key that cannot be
  1071. * extracted from the packet itself.
  1072. */
  1073. int ovs_nla_get_flow_metadata(const struct nlattr *attr,
  1074. struct sw_flow_key *key,
  1075. bool log)
  1076. {
  1077. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1078. struct sw_flow_match match;
  1079. u64 attrs = 0;
  1080. int err;
  1081. err = parse_flow_nlattrs(attr, a, &attrs, log);
  1082. if (err)
  1083. return -EINVAL;
  1084. memset(&match, 0, sizeof(match));
  1085. match.key = key;
  1086. key->phy.in_port = DP_MAX_PORTS;
  1087. return metadata_from_nlattrs(&match, &attrs, a, false, log);
  1088. }
  1089. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1090. const struct sw_flow_key *output, bool is_mask,
  1091. struct sk_buff *skb)
  1092. {
  1093. struct ovs_key_ethernet *eth_key;
  1094. struct nlattr *nla, *encap;
  1095. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1096. goto nla_put_failure;
  1097. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1098. goto nla_put_failure;
  1099. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1100. goto nla_put_failure;
  1101. if ((swkey->tun_key.ipv4_dst || is_mask)) {
  1102. const void *opts = NULL;
  1103. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1104. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1105. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  1106. swkey->tun_opts_len))
  1107. goto nla_put_failure;
  1108. }
  1109. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1110. if (is_mask && (output->phy.in_port == 0xffff))
  1111. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1112. goto nla_put_failure;
  1113. } else {
  1114. u16 upper_u16;
  1115. upper_u16 = !is_mask ? 0 : 0xffff;
  1116. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1117. (upper_u16 << 16) | output->phy.in_port))
  1118. goto nla_put_failure;
  1119. }
  1120. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1121. goto nla_put_failure;
  1122. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1123. if (!nla)
  1124. goto nla_put_failure;
  1125. eth_key = nla_data(nla);
  1126. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1127. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1128. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1129. __be16 eth_type;
  1130. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1131. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1132. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1133. goto nla_put_failure;
  1134. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1135. if (!swkey->eth.tci)
  1136. goto unencap;
  1137. } else
  1138. encap = NULL;
  1139. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1140. /*
  1141. * Ethertype 802.2 is represented in the netlink with omitted
  1142. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1143. * 0xffff in the mask attribute. Ethertype can also
  1144. * be wildcarded.
  1145. */
  1146. if (is_mask && output->eth.type)
  1147. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1148. output->eth.type))
  1149. goto nla_put_failure;
  1150. goto unencap;
  1151. }
  1152. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1153. goto nla_put_failure;
  1154. if (swkey->eth.type == htons(ETH_P_IP)) {
  1155. struct ovs_key_ipv4 *ipv4_key;
  1156. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1157. if (!nla)
  1158. goto nla_put_failure;
  1159. ipv4_key = nla_data(nla);
  1160. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1161. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1162. ipv4_key->ipv4_proto = output->ip.proto;
  1163. ipv4_key->ipv4_tos = output->ip.tos;
  1164. ipv4_key->ipv4_ttl = output->ip.ttl;
  1165. ipv4_key->ipv4_frag = output->ip.frag;
  1166. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1167. struct ovs_key_ipv6 *ipv6_key;
  1168. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1169. if (!nla)
  1170. goto nla_put_failure;
  1171. ipv6_key = nla_data(nla);
  1172. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1173. sizeof(ipv6_key->ipv6_src));
  1174. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1175. sizeof(ipv6_key->ipv6_dst));
  1176. ipv6_key->ipv6_label = output->ipv6.label;
  1177. ipv6_key->ipv6_proto = output->ip.proto;
  1178. ipv6_key->ipv6_tclass = output->ip.tos;
  1179. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1180. ipv6_key->ipv6_frag = output->ip.frag;
  1181. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1182. swkey->eth.type == htons(ETH_P_RARP)) {
  1183. struct ovs_key_arp *arp_key;
  1184. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1185. if (!nla)
  1186. goto nla_put_failure;
  1187. arp_key = nla_data(nla);
  1188. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1189. arp_key->arp_sip = output->ipv4.addr.src;
  1190. arp_key->arp_tip = output->ipv4.addr.dst;
  1191. arp_key->arp_op = htons(output->ip.proto);
  1192. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1193. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1194. } else if (eth_p_mpls(swkey->eth.type)) {
  1195. struct ovs_key_mpls *mpls_key;
  1196. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1197. if (!nla)
  1198. goto nla_put_failure;
  1199. mpls_key = nla_data(nla);
  1200. mpls_key->mpls_lse = output->mpls.top_lse;
  1201. }
  1202. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1203. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1204. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1205. if (swkey->ip.proto == IPPROTO_TCP) {
  1206. struct ovs_key_tcp *tcp_key;
  1207. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1208. if (!nla)
  1209. goto nla_put_failure;
  1210. tcp_key = nla_data(nla);
  1211. tcp_key->tcp_src = output->tp.src;
  1212. tcp_key->tcp_dst = output->tp.dst;
  1213. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1214. output->tp.flags))
  1215. goto nla_put_failure;
  1216. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1217. struct ovs_key_udp *udp_key;
  1218. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1219. if (!nla)
  1220. goto nla_put_failure;
  1221. udp_key = nla_data(nla);
  1222. udp_key->udp_src = output->tp.src;
  1223. udp_key->udp_dst = output->tp.dst;
  1224. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1225. struct ovs_key_sctp *sctp_key;
  1226. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1227. if (!nla)
  1228. goto nla_put_failure;
  1229. sctp_key = nla_data(nla);
  1230. sctp_key->sctp_src = output->tp.src;
  1231. sctp_key->sctp_dst = output->tp.dst;
  1232. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1233. swkey->ip.proto == IPPROTO_ICMP) {
  1234. struct ovs_key_icmp *icmp_key;
  1235. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1236. if (!nla)
  1237. goto nla_put_failure;
  1238. icmp_key = nla_data(nla);
  1239. icmp_key->icmp_type = ntohs(output->tp.src);
  1240. icmp_key->icmp_code = ntohs(output->tp.dst);
  1241. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1242. swkey->ip.proto == IPPROTO_ICMPV6) {
  1243. struct ovs_key_icmpv6 *icmpv6_key;
  1244. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1245. sizeof(*icmpv6_key));
  1246. if (!nla)
  1247. goto nla_put_failure;
  1248. icmpv6_key = nla_data(nla);
  1249. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1250. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1251. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1252. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1253. struct ovs_key_nd *nd_key;
  1254. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1255. if (!nla)
  1256. goto nla_put_failure;
  1257. nd_key = nla_data(nla);
  1258. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1259. sizeof(nd_key->nd_target));
  1260. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1261. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1262. }
  1263. }
  1264. }
  1265. unencap:
  1266. if (encap)
  1267. nla_nest_end(skb, encap);
  1268. return 0;
  1269. nla_put_failure:
  1270. return -EMSGSIZE;
  1271. }
  1272. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1273. const struct sw_flow_key *output, int attr, bool is_mask,
  1274. struct sk_buff *skb)
  1275. {
  1276. int err;
  1277. struct nlattr *nla;
  1278. nla = nla_nest_start(skb, attr);
  1279. if (!nla)
  1280. return -EMSGSIZE;
  1281. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1282. if (err)
  1283. return err;
  1284. nla_nest_end(skb, nla);
  1285. return 0;
  1286. }
  1287. /* Called with ovs_mutex or RCU read lock. */
  1288. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1289. {
  1290. if (ovs_identifier_is_ufid(&flow->id))
  1291. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1292. flow->id.ufid);
  1293. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1294. OVS_FLOW_ATTR_KEY, false, skb);
  1295. }
  1296. /* Called with ovs_mutex or RCU read lock. */
  1297. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1298. {
  1299. return ovs_nla_put_key(&flow->key, &flow->key,
  1300. OVS_FLOW_ATTR_KEY, false, skb);
  1301. }
  1302. /* Called with ovs_mutex or RCU read lock. */
  1303. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1304. {
  1305. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1306. OVS_FLOW_ATTR_MASK, true, skb);
  1307. }
  1308. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1309. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1310. {
  1311. struct sw_flow_actions *sfa;
  1312. if (size > MAX_ACTIONS_BUFSIZE) {
  1313. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1314. return ERR_PTR(-EINVAL);
  1315. }
  1316. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1317. if (!sfa)
  1318. return ERR_PTR(-ENOMEM);
  1319. sfa->actions_len = 0;
  1320. return sfa;
  1321. }
  1322. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1323. * The caller must hold rcu_read_lock for this to be sensible. */
  1324. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1325. {
  1326. kfree_rcu(sf_acts, rcu);
  1327. }
  1328. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1329. int attr_len, bool log)
  1330. {
  1331. struct sw_flow_actions *acts;
  1332. int new_acts_size;
  1333. int req_size = NLA_ALIGN(attr_len);
  1334. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1335. (*sfa)->actions_len;
  1336. if (req_size <= (ksize(*sfa) - next_offset))
  1337. goto out;
  1338. new_acts_size = ksize(*sfa) * 2;
  1339. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1340. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1341. return ERR_PTR(-EMSGSIZE);
  1342. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1343. }
  1344. acts = nla_alloc_flow_actions(new_acts_size, log);
  1345. if (IS_ERR(acts))
  1346. return (void *)acts;
  1347. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1348. acts->actions_len = (*sfa)->actions_len;
  1349. kfree(*sfa);
  1350. *sfa = acts;
  1351. out:
  1352. (*sfa)->actions_len += req_size;
  1353. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1354. }
  1355. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1356. int attrtype, void *data, int len, bool log)
  1357. {
  1358. struct nlattr *a;
  1359. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1360. if (IS_ERR(a))
  1361. return a;
  1362. a->nla_type = attrtype;
  1363. a->nla_len = nla_attr_size(len);
  1364. if (data)
  1365. memcpy(nla_data(a), data, len);
  1366. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1367. return a;
  1368. }
  1369. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1370. void *data, int len, bool log)
  1371. {
  1372. struct nlattr *a;
  1373. a = __add_action(sfa, attrtype, data, len, log);
  1374. return PTR_ERR_OR_ZERO(a);
  1375. }
  1376. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1377. int attrtype, bool log)
  1378. {
  1379. int used = (*sfa)->actions_len;
  1380. int err;
  1381. err = add_action(sfa, attrtype, NULL, 0, log);
  1382. if (err)
  1383. return err;
  1384. return used;
  1385. }
  1386. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1387. int st_offset)
  1388. {
  1389. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1390. st_offset);
  1391. a->nla_len = sfa->actions_len - st_offset;
  1392. }
  1393. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1394. const struct sw_flow_key *key,
  1395. int depth, struct sw_flow_actions **sfa,
  1396. __be16 eth_type, __be16 vlan_tci, bool log);
  1397. static int validate_and_copy_sample(const struct nlattr *attr,
  1398. const struct sw_flow_key *key, int depth,
  1399. struct sw_flow_actions **sfa,
  1400. __be16 eth_type, __be16 vlan_tci, bool log)
  1401. {
  1402. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1403. const struct nlattr *probability, *actions;
  1404. const struct nlattr *a;
  1405. int rem, start, err, st_acts;
  1406. memset(attrs, 0, sizeof(attrs));
  1407. nla_for_each_nested(a, attr, rem) {
  1408. int type = nla_type(a);
  1409. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1410. return -EINVAL;
  1411. attrs[type] = a;
  1412. }
  1413. if (rem)
  1414. return -EINVAL;
  1415. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1416. if (!probability || nla_len(probability) != sizeof(u32))
  1417. return -EINVAL;
  1418. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1419. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1420. return -EINVAL;
  1421. /* validation done, copy sample action. */
  1422. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1423. if (start < 0)
  1424. return start;
  1425. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1426. nla_data(probability), sizeof(u32), log);
  1427. if (err)
  1428. return err;
  1429. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1430. if (st_acts < 0)
  1431. return st_acts;
  1432. err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
  1433. eth_type, vlan_tci, log);
  1434. if (err)
  1435. return err;
  1436. add_nested_action_end(*sfa, st_acts);
  1437. add_nested_action_end(*sfa, start);
  1438. return 0;
  1439. }
  1440. void ovs_match_init(struct sw_flow_match *match,
  1441. struct sw_flow_key *key,
  1442. struct sw_flow_mask *mask)
  1443. {
  1444. memset(match, 0, sizeof(*match));
  1445. match->key = key;
  1446. match->mask = mask;
  1447. memset(key, 0, sizeof(*key));
  1448. if (mask) {
  1449. memset(&mask->key, 0, sizeof(mask->key));
  1450. mask->range.start = mask->range.end = 0;
  1451. }
  1452. }
  1453. static int validate_geneve_opts(struct sw_flow_key *key)
  1454. {
  1455. struct geneve_opt *option;
  1456. int opts_len = key->tun_opts_len;
  1457. bool crit_opt = false;
  1458. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1459. while (opts_len > 0) {
  1460. int len;
  1461. if (opts_len < sizeof(*option))
  1462. return -EINVAL;
  1463. len = sizeof(*option) + option->length * 4;
  1464. if (len > opts_len)
  1465. return -EINVAL;
  1466. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1467. option = (struct geneve_opt *)((u8 *)option + len);
  1468. opts_len -= len;
  1469. };
  1470. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1471. return 0;
  1472. }
  1473. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1474. struct sw_flow_actions **sfa, bool log)
  1475. {
  1476. struct sw_flow_match match;
  1477. struct sw_flow_key key;
  1478. struct ovs_tunnel_info *tun_info;
  1479. struct nlattr *a;
  1480. int err = 0, start, opts_type;
  1481. ovs_match_init(&match, &key, NULL);
  1482. opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1483. if (opts_type < 0)
  1484. return opts_type;
  1485. if (key.tun_opts_len) {
  1486. switch (opts_type) {
  1487. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1488. err = validate_geneve_opts(&key);
  1489. if (err < 0)
  1490. return err;
  1491. break;
  1492. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1493. break;
  1494. }
  1495. };
  1496. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1497. if (start < 0)
  1498. return start;
  1499. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1500. sizeof(*tun_info) + key.tun_opts_len, log);
  1501. if (IS_ERR(a))
  1502. return PTR_ERR(a);
  1503. tun_info = nla_data(a);
  1504. tun_info->tunnel = key.tun_key;
  1505. tun_info->options_len = key.tun_opts_len;
  1506. if (tun_info->options_len) {
  1507. /* We need to store the options in the action itself since
  1508. * everything else will go away after flow setup. We can append
  1509. * it to tun_info and then point there.
  1510. */
  1511. memcpy((tun_info + 1),
  1512. TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
  1513. tun_info->options = (tun_info + 1);
  1514. } else {
  1515. tun_info->options = NULL;
  1516. }
  1517. add_nested_action_end(*sfa, start);
  1518. return err;
  1519. }
  1520. /* Return false if there are any non-masked bits set.
  1521. * Mask follows data immediately, before any netlink padding.
  1522. */
  1523. static bool validate_masked(u8 *data, int len)
  1524. {
  1525. u8 *mask = data + len;
  1526. while (len--)
  1527. if (*data++ & ~*mask++)
  1528. return false;
  1529. return true;
  1530. }
  1531. static int validate_set(const struct nlattr *a,
  1532. const struct sw_flow_key *flow_key,
  1533. struct sw_flow_actions **sfa,
  1534. bool *skip_copy, __be16 eth_type, bool masked, bool log)
  1535. {
  1536. const struct nlattr *ovs_key = nla_data(a);
  1537. int key_type = nla_type(ovs_key);
  1538. size_t key_len;
  1539. /* There can be only one key in a action */
  1540. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1541. return -EINVAL;
  1542. key_len = nla_len(ovs_key);
  1543. if (masked)
  1544. key_len /= 2;
  1545. if (key_type > OVS_KEY_ATTR_MAX ||
  1546. (ovs_key_lens[key_type].len != key_len &&
  1547. ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
  1548. return -EINVAL;
  1549. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  1550. return -EINVAL;
  1551. switch (key_type) {
  1552. const struct ovs_key_ipv4 *ipv4_key;
  1553. const struct ovs_key_ipv6 *ipv6_key;
  1554. int err;
  1555. case OVS_KEY_ATTR_PRIORITY:
  1556. case OVS_KEY_ATTR_SKB_MARK:
  1557. case OVS_KEY_ATTR_ETHERNET:
  1558. break;
  1559. case OVS_KEY_ATTR_TUNNEL:
  1560. if (eth_p_mpls(eth_type))
  1561. return -EINVAL;
  1562. if (masked)
  1563. return -EINVAL; /* Masked tunnel set not supported. */
  1564. *skip_copy = true;
  1565. err = validate_and_copy_set_tun(a, sfa, log);
  1566. if (err)
  1567. return err;
  1568. break;
  1569. case OVS_KEY_ATTR_IPV4:
  1570. if (eth_type != htons(ETH_P_IP))
  1571. return -EINVAL;
  1572. ipv4_key = nla_data(ovs_key);
  1573. if (masked) {
  1574. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  1575. /* Non-writeable fields. */
  1576. if (mask->ipv4_proto || mask->ipv4_frag)
  1577. return -EINVAL;
  1578. } else {
  1579. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1580. return -EINVAL;
  1581. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1582. return -EINVAL;
  1583. }
  1584. break;
  1585. case OVS_KEY_ATTR_IPV6:
  1586. if (eth_type != htons(ETH_P_IPV6))
  1587. return -EINVAL;
  1588. ipv6_key = nla_data(ovs_key);
  1589. if (masked) {
  1590. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  1591. /* Non-writeable fields. */
  1592. if (mask->ipv6_proto || mask->ipv6_frag)
  1593. return -EINVAL;
  1594. /* Invalid bits in the flow label mask? */
  1595. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  1596. return -EINVAL;
  1597. } else {
  1598. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1599. return -EINVAL;
  1600. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1601. return -EINVAL;
  1602. }
  1603. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1604. return -EINVAL;
  1605. break;
  1606. case OVS_KEY_ATTR_TCP:
  1607. if ((eth_type != htons(ETH_P_IP) &&
  1608. eth_type != htons(ETH_P_IPV6)) ||
  1609. flow_key->ip.proto != IPPROTO_TCP)
  1610. return -EINVAL;
  1611. break;
  1612. case OVS_KEY_ATTR_UDP:
  1613. if ((eth_type != htons(ETH_P_IP) &&
  1614. eth_type != htons(ETH_P_IPV6)) ||
  1615. flow_key->ip.proto != IPPROTO_UDP)
  1616. return -EINVAL;
  1617. break;
  1618. case OVS_KEY_ATTR_MPLS:
  1619. if (!eth_p_mpls(eth_type))
  1620. return -EINVAL;
  1621. break;
  1622. case OVS_KEY_ATTR_SCTP:
  1623. if ((eth_type != htons(ETH_P_IP) &&
  1624. eth_type != htons(ETH_P_IPV6)) ||
  1625. flow_key->ip.proto != IPPROTO_SCTP)
  1626. return -EINVAL;
  1627. break;
  1628. default:
  1629. return -EINVAL;
  1630. }
  1631. /* Convert non-masked non-tunnel set actions to masked set actions. */
  1632. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  1633. int start, len = key_len * 2;
  1634. struct nlattr *at;
  1635. *skip_copy = true;
  1636. start = add_nested_action_start(sfa,
  1637. OVS_ACTION_ATTR_SET_TO_MASKED,
  1638. log);
  1639. if (start < 0)
  1640. return start;
  1641. at = __add_action(sfa, key_type, NULL, len, log);
  1642. if (IS_ERR(at))
  1643. return PTR_ERR(at);
  1644. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  1645. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  1646. /* Clear non-writeable bits from otherwise writeable fields. */
  1647. if (key_type == OVS_KEY_ATTR_IPV6) {
  1648. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  1649. mask->ipv6_label &= htonl(0x000FFFFF);
  1650. }
  1651. add_nested_action_end(*sfa, start);
  1652. }
  1653. return 0;
  1654. }
  1655. static int validate_userspace(const struct nlattr *attr)
  1656. {
  1657. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1658. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1659. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1660. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1661. };
  1662. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1663. int error;
  1664. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1665. attr, userspace_policy);
  1666. if (error)
  1667. return error;
  1668. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1669. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1670. return -EINVAL;
  1671. return 0;
  1672. }
  1673. static int copy_action(const struct nlattr *from,
  1674. struct sw_flow_actions **sfa, bool log)
  1675. {
  1676. int totlen = NLA_ALIGN(from->nla_len);
  1677. struct nlattr *to;
  1678. to = reserve_sfa_size(sfa, from->nla_len, log);
  1679. if (IS_ERR(to))
  1680. return PTR_ERR(to);
  1681. memcpy(to, from, totlen);
  1682. return 0;
  1683. }
  1684. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1685. const struct sw_flow_key *key,
  1686. int depth, struct sw_flow_actions **sfa,
  1687. __be16 eth_type, __be16 vlan_tci, bool log)
  1688. {
  1689. const struct nlattr *a;
  1690. int rem, err;
  1691. if (depth >= SAMPLE_ACTION_DEPTH)
  1692. return -EOVERFLOW;
  1693. nla_for_each_nested(a, attr, rem) {
  1694. /* Expected argument lengths, (u32)-1 for variable length. */
  1695. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1696. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1697. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1698. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1699. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1700. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1701. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1702. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1703. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1704. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  1705. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1706. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1707. };
  1708. const struct ovs_action_push_vlan *vlan;
  1709. int type = nla_type(a);
  1710. bool skip_copy;
  1711. if (type > OVS_ACTION_ATTR_MAX ||
  1712. (action_lens[type] != nla_len(a) &&
  1713. action_lens[type] != (u32)-1))
  1714. return -EINVAL;
  1715. skip_copy = false;
  1716. switch (type) {
  1717. case OVS_ACTION_ATTR_UNSPEC:
  1718. return -EINVAL;
  1719. case OVS_ACTION_ATTR_USERSPACE:
  1720. err = validate_userspace(a);
  1721. if (err)
  1722. return err;
  1723. break;
  1724. case OVS_ACTION_ATTR_OUTPUT:
  1725. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1726. return -EINVAL;
  1727. break;
  1728. case OVS_ACTION_ATTR_HASH: {
  1729. const struct ovs_action_hash *act_hash = nla_data(a);
  1730. switch (act_hash->hash_alg) {
  1731. case OVS_HASH_ALG_L4:
  1732. break;
  1733. default:
  1734. return -EINVAL;
  1735. }
  1736. break;
  1737. }
  1738. case OVS_ACTION_ATTR_POP_VLAN:
  1739. vlan_tci = htons(0);
  1740. break;
  1741. case OVS_ACTION_ATTR_PUSH_VLAN:
  1742. vlan = nla_data(a);
  1743. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1744. return -EINVAL;
  1745. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1746. return -EINVAL;
  1747. vlan_tci = vlan->vlan_tci;
  1748. break;
  1749. case OVS_ACTION_ATTR_RECIRC:
  1750. break;
  1751. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1752. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1753. if (!eth_p_mpls(mpls->mpls_ethertype))
  1754. return -EINVAL;
  1755. /* Prohibit push MPLS other than to a white list
  1756. * for packets that have a known tag order.
  1757. */
  1758. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1759. (eth_type != htons(ETH_P_IP) &&
  1760. eth_type != htons(ETH_P_IPV6) &&
  1761. eth_type != htons(ETH_P_ARP) &&
  1762. eth_type != htons(ETH_P_RARP) &&
  1763. !eth_p_mpls(eth_type)))
  1764. return -EINVAL;
  1765. eth_type = mpls->mpls_ethertype;
  1766. break;
  1767. }
  1768. case OVS_ACTION_ATTR_POP_MPLS:
  1769. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1770. !eth_p_mpls(eth_type))
  1771. return -EINVAL;
  1772. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1773. * as there is no check here to ensure that the new
  1774. * eth_type is valid and thus set actions could
  1775. * write off the end of the packet or otherwise
  1776. * corrupt it.
  1777. *
  1778. * Support for these actions is planned using packet
  1779. * recirculation.
  1780. */
  1781. eth_type = htons(0);
  1782. break;
  1783. case OVS_ACTION_ATTR_SET:
  1784. err = validate_set(a, key, sfa,
  1785. &skip_copy, eth_type, false, log);
  1786. if (err)
  1787. return err;
  1788. break;
  1789. case OVS_ACTION_ATTR_SET_MASKED:
  1790. err = validate_set(a, key, sfa,
  1791. &skip_copy, eth_type, true, log);
  1792. if (err)
  1793. return err;
  1794. break;
  1795. case OVS_ACTION_ATTR_SAMPLE:
  1796. err = validate_and_copy_sample(a, key, depth, sfa,
  1797. eth_type, vlan_tci, log);
  1798. if (err)
  1799. return err;
  1800. skip_copy = true;
  1801. break;
  1802. default:
  1803. OVS_NLERR(log, "Unknown Action type %d", type);
  1804. return -EINVAL;
  1805. }
  1806. if (!skip_copy) {
  1807. err = copy_action(a, sfa, log);
  1808. if (err)
  1809. return err;
  1810. }
  1811. }
  1812. if (rem > 0)
  1813. return -EINVAL;
  1814. return 0;
  1815. }
  1816. /* 'key' must be the masked key. */
  1817. int ovs_nla_copy_actions(const struct nlattr *attr,
  1818. const struct sw_flow_key *key,
  1819. struct sw_flow_actions **sfa, bool log)
  1820. {
  1821. int err;
  1822. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1823. if (IS_ERR(*sfa))
  1824. return PTR_ERR(*sfa);
  1825. err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
  1826. key->eth.tci, log);
  1827. if (err)
  1828. kfree(*sfa);
  1829. return err;
  1830. }
  1831. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1832. {
  1833. const struct nlattr *a;
  1834. struct nlattr *start;
  1835. int err = 0, rem;
  1836. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1837. if (!start)
  1838. return -EMSGSIZE;
  1839. nla_for_each_nested(a, attr, rem) {
  1840. int type = nla_type(a);
  1841. struct nlattr *st_sample;
  1842. switch (type) {
  1843. case OVS_SAMPLE_ATTR_PROBABILITY:
  1844. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1845. sizeof(u32), nla_data(a)))
  1846. return -EMSGSIZE;
  1847. break;
  1848. case OVS_SAMPLE_ATTR_ACTIONS:
  1849. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1850. if (!st_sample)
  1851. return -EMSGSIZE;
  1852. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1853. if (err)
  1854. return err;
  1855. nla_nest_end(skb, st_sample);
  1856. break;
  1857. }
  1858. }
  1859. nla_nest_end(skb, start);
  1860. return err;
  1861. }
  1862. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1863. {
  1864. const struct nlattr *ovs_key = nla_data(a);
  1865. int key_type = nla_type(ovs_key);
  1866. struct nlattr *start;
  1867. int err;
  1868. switch (key_type) {
  1869. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1870. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1871. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1872. if (!start)
  1873. return -EMSGSIZE;
  1874. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1875. tun_info->options_len ?
  1876. tun_info->options : NULL,
  1877. tun_info->options_len);
  1878. if (err)
  1879. return err;
  1880. nla_nest_end(skb, start);
  1881. break;
  1882. }
  1883. default:
  1884. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1885. return -EMSGSIZE;
  1886. break;
  1887. }
  1888. return 0;
  1889. }
  1890. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  1891. struct sk_buff *skb)
  1892. {
  1893. const struct nlattr *ovs_key = nla_data(a);
  1894. struct nlattr *nla;
  1895. size_t key_len = nla_len(ovs_key) / 2;
  1896. /* Revert the conversion we did from a non-masked set action to
  1897. * masked set action.
  1898. */
  1899. nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1900. if (!nla)
  1901. return -EMSGSIZE;
  1902. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  1903. return -EMSGSIZE;
  1904. nla_nest_end(skb, nla);
  1905. return 0;
  1906. }
  1907. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1908. {
  1909. const struct nlattr *a;
  1910. int rem, err;
  1911. nla_for_each_attr(a, attr, len, rem) {
  1912. int type = nla_type(a);
  1913. switch (type) {
  1914. case OVS_ACTION_ATTR_SET:
  1915. err = set_action_to_attr(a, skb);
  1916. if (err)
  1917. return err;
  1918. break;
  1919. case OVS_ACTION_ATTR_SET_TO_MASKED:
  1920. err = masked_set_action_to_set_action_attr(a, skb);
  1921. if (err)
  1922. return err;
  1923. break;
  1924. case OVS_ACTION_ATTR_SAMPLE:
  1925. err = sample_action_to_attr(a, skb);
  1926. if (err)
  1927. return err;
  1928. break;
  1929. default:
  1930. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1931. return -EMSGSIZE;
  1932. break;
  1933. }
  1934. }
  1935. return 0;
  1936. }