flow_netlink.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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. static void update_range__(struct sw_flow_match *match,
  50. size_t offset, size_t size, bool is_mask)
  51. {
  52. struct sw_flow_key_range *range = NULL;
  53. size_t start = rounddown(offset, sizeof(long));
  54. size_t end = roundup(offset + size, sizeof(long));
  55. if (!is_mask)
  56. range = &match->range;
  57. else if (match->mask)
  58. range = &match->mask->range;
  59. if (!range)
  60. return;
  61. if (range->start == range->end) {
  62. range->start = start;
  63. range->end = end;
  64. return;
  65. }
  66. if (range->start > start)
  67. range->start = start;
  68. if (range->end < end)
  69. range->end = end;
  70. }
  71. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  72. do { \
  73. update_range__(match, offsetof(struct sw_flow_key, field), \
  74. sizeof((match)->key->field), is_mask); \
  75. if (is_mask) { \
  76. if ((match)->mask) \
  77. (match)->mask->key.field = value; \
  78. } else { \
  79. (match)->key->field = value; \
  80. } \
  81. } while (0)
  82. #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
  83. do { \
  84. update_range__(match, offset, len, is_mask); \
  85. if (is_mask) \
  86. memcpy((u8 *)&(match)->mask->key + offset, value_p, \
  87. len); \
  88. else \
  89. memcpy((u8 *)(match)->key + offset, value_p, len); \
  90. } while (0)
  91. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  92. SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
  93. value_p, len, is_mask)
  94. #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
  95. do { \
  96. update_range__(match, offsetof(struct sw_flow_key, field), \
  97. sizeof((match)->key->field), is_mask); \
  98. if (is_mask) { \
  99. if ((match)->mask) \
  100. memset((u8 *)&(match)->mask->key.field, value,\
  101. sizeof((match)->mask->key.field)); \
  102. } else { \
  103. memset((u8 *)&(match)->key->field, value, \
  104. sizeof((match)->key->field)); \
  105. } \
  106. } while (0)
  107. static bool match_validate(const struct sw_flow_match *match,
  108. u64 key_attrs, u64 mask_attrs)
  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(0xffff)))
  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("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
  211. (unsigned long long)key_attrs, (unsigned long long)key_expected);
  212. return false;
  213. }
  214. if ((mask_attrs & mask_allowed) != mask_attrs) {
  215. /* Mask attributes check failed. */
  216. OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
  217. (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
  218. return false;
  219. }
  220. return true;
  221. }
  222. size_t ovs_key_attr_size(void)
  223. {
  224. /* Whenever adding new OVS_KEY_ FIELDS, we should consider
  225. * updating this function.
  226. */
  227. BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
  228. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  229. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  230. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  231. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  232. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  233. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  234. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  235. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  236. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  237. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  238. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  239. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  240. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  241. + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
  242. + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
  243. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  244. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  245. + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
  246. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  247. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  248. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  249. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  250. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  251. }
  252. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  253. static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  254. [OVS_KEY_ATTR_ENCAP] = -1,
  255. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  256. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  257. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  258. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  259. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  260. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  261. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  262. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  263. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  264. [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
  265. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  266. [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
  267. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  268. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  269. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  270. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  271. [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
  272. [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
  273. [OVS_KEY_ATTR_TUNNEL] = -1,
  274. [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
  275. };
  276. static bool is_all_zero(const u8 *fp, size_t size)
  277. {
  278. int i;
  279. if (!fp)
  280. return false;
  281. for (i = 0; i < size; i++)
  282. if (fp[i])
  283. return false;
  284. return true;
  285. }
  286. static int __parse_flow_nlattrs(const struct nlattr *attr,
  287. const struct nlattr *a[],
  288. u64 *attrsp, bool nz)
  289. {
  290. const struct nlattr *nla;
  291. u64 attrs;
  292. int rem;
  293. attrs = *attrsp;
  294. nla_for_each_nested(nla, attr, rem) {
  295. u16 type = nla_type(nla);
  296. int expected_len;
  297. if (type > OVS_KEY_ATTR_MAX) {
  298. OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
  299. type, OVS_KEY_ATTR_MAX);
  300. return -EINVAL;
  301. }
  302. if (attrs & (1 << type)) {
  303. OVS_NLERR("Duplicate key attribute (type %d).\n", type);
  304. return -EINVAL;
  305. }
  306. expected_len = ovs_key_lens[type];
  307. if (nla_len(nla) != expected_len && expected_len != -1) {
  308. OVS_NLERR("Key attribute has unexpected length (type=%d"
  309. ", length=%d, expected=%d).\n", type,
  310. nla_len(nla), expected_len);
  311. return -EINVAL;
  312. }
  313. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  314. attrs |= 1 << type;
  315. a[type] = nla;
  316. }
  317. }
  318. if (rem) {
  319. OVS_NLERR("Message has %d unknown bytes.\n", rem);
  320. return -EINVAL;
  321. }
  322. *attrsp = attrs;
  323. return 0;
  324. }
  325. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  326. const struct nlattr *a[], u64 *attrsp)
  327. {
  328. return __parse_flow_nlattrs(attr, a, attrsp, true);
  329. }
  330. static int parse_flow_nlattrs(const struct nlattr *attr,
  331. const struct nlattr *a[], u64 *attrsp)
  332. {
  333. return __parse_flow_nlattrs(attr, a, attrsp, false);
  334. }
  335. static int ipv4_tun_from_nlattr(const struct nlattr *attr,
  336. struct sw_flow_match *match, bool is_mask)
  337. {
  338. struct nlattr *a;
  339. int rem;
  340. bool ttl = false;
  341. __be16 tun_flags = 0;
  342. unsigned long opt_key_offset;
  343. nla_for_each_nested(a, attr, rem) {
  344. int type = nla_type(a);
  345. static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  346. [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
  347. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
  348. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
  349. [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
  350. [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
  351. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
  352. [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
  353. [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
  354. [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
  355. };
  356. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  357. OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
  358. type, OVS_TUNNEL_KEY_ATTR_MAX);
  359. return -EINVAL;
  360. }
  361. if (ovs_tunnel_key_lens[type] != nla_len(a) &&
  362. ovs_tunnel_key_lens[type] != -1) {
  363. OVS_NLERR("IPv4 tunnel attribute type has unexpected "
  364. " length (type=%d, length=%d, expected=%d).\n",
  365. type, nla_len(a), ovs_tunnel_key_lens[type]);
  366. return -EINVAL;
  367. }
  368. switch (type) {
  369. case OVS_TUNNEL_KEY_ATTR_ID:
  370. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  371. nla_get_be64(a), is_mask);
  372. tun_flags |= TUNNEL_KEY;
  373. break;
  374. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  375. SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
  376. nla_get_be32(a), is_mask);
  377. break;
  378. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  379. SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
  380. nla_get_be32(a), is_mask);
  381. break;
  382. case OVS_TUNNEL_KEY_ATTR_TOS:
  383. SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
  384. nla_get_u8(a), is_mask);
  385. break;
  386. case OVS_TUNNEL_KEY_ATTR_TTL:
  387. SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
  388. nla_get_u8(a), is_mask);
  389. ttl = true;
  390. break;
  391. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  392. tun_flags |= TUNNEL_DONT_FRAGMENT;
  393. break;
  394. case OVS_TUNNEL_KEY_ATTR_CSUM:
  395. tun_flags |= TUNNEL_CSUM;
  396. break;
  397. case OVS_TUNNEL_KEY_ATTR_OAM:
  398. tun_flags |= TUNNEL_OAM;
  399. break;
  400. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  401. tun_flags |= TUNNEL_OPTIONS_PRESENT;
  402. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  403. OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
  404. nla_len(a),
  405. sizeof(match->key->tun_opts));
  406. return -EINVAL;
  407. }
  408. if (nla_len(a) % 4 != 0) {
  409. OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
  410. nla_len(a));
  411. return -EINVAL;
  412. }
  413. /* We need to record the length of the options passed
  414. * down, otherwise packets with the same format but
  415. * additional options will be silently matched.
  416. */
  417. if (!is_mask) {
  418. SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
  419. false);
  420. } else {
  421. /* This is somewhat unusual because it looks at
  422. * both the key and mask while parsing the
  423. * attributes (and by extension assumes the key
  424. * is parsed first). Normally, we would verify
  425. * that each is the correct length and that the
  426. * attributes line up in the validate function.
  427. * However, that is difficult because this is
  428. * variable length and we won't have the
  429. * information later.
  430. */
  431. if (match->key->tun_opts_len != nla_len(a)) {
  432. OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
  433. match->key->tun_opts_len,
  434. nla_len(a));
  435. return -EINVAL;
  436. }
  437. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
  438. true);
  439. }
  440. opt_key_offset = (unsigned long)GENEVE_OPTS(
  441. (struct sw_flow_key *)0,
  442. nla_len(a));
  443. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset,
  444. nla_data(a), nla_len(a),
  445. is_mask);
  446. break;
  447. default:
  448. OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
  449. type);
  450. return -EINVAL;
  451. }
  452. }
  453. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  454. if (rem > 0) {
  455. OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
  456. return -EINVAL;
  457. }
  458. if (!is_mask) {
  459. if (!match->key->tun_key.ipv4_dst) {
  460. OVS_NLERR("IPv4 tunnel destination address is zero.\n");
  461. return -EINVAL;
  462. }
  463. if (!ttl) {
  464. OVS_NLERR("IPv4 tunnel TTL not specified.\n");
  465. return -EINVAL;
  466. }
  467. }
  468. return 0;
  469. }
  470. static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
  471. const struct ovs_key_ipv4_tunnel *output,
  472. const struct geneve_opt *tun_opts,
  473. int swkey_tun_opts_len)
  474. {
  475. if (output->tun_flags & TUNNEL_KEY &&
  476. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  477. return -EMSGSIZE;
  478. if (output->ipv4_src &&
  479. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
  480. return -EMSGSIZE;
  481. if (output->ipv4_dst &&
  482. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
  483. return -EMSGSIZE;
  484. if (output->ipv4_tos &&
  485. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  486. return -EMSGSIZE;
  487. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  488. return -EMSGSIZE;
  489. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  490. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  491. return -EMSGSIZE;
  492. if ((output->tun_flags & TUNNEL_CSUM) &&
  493. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  494. return -EMSGSIZE;
  495. if ((output->tun_flags & TUNNEL_OAM) &&
  496. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  497. return -EMSGSIZE;
  498. if (tun_opts &&
  499. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  500. swkey_tun_opts_len, tun_opts))
  501. return -EMSGSIZE;
  502. return 0;
  503. }
  504. static int ipv4_tun_to_nlattr(struct sk_buff *skb,
  505. const struct ovs_key_ipv4_tunnel *output,
  506. const struct geneve_opt *tun_opts,
  507. int swkey_tun_opts_len)
  508. {
  509. struct nlattr *nla;
  510. int err;
  511. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  512. if (!nla)
  513. return -EMSGSIZE;
  514. err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
  515. if (err)
  516. return err;
  517. nla_nest_end(skb, nla);
  518. return 0;
  519. }
  520. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  521. const struct nlattr **a, bool is_mask)
  522. {
  523. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  524. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  525. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  526. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  527. }
  528. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  529. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  530. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  531. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  532. }
  533. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  534. SW_FLOW_KEY_PUT(match, phy.priority,
  535. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  536. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  537. }
  538. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  539. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  540. if (is_mask) {
  541. in_port = 0xffffffff; /* Always exact match in_port. */
  542. } else if (in_port >= DP_MAX_PORTS) {
  543. OVS_NLERR("Port (%d) exceeds maximum allowable (%d).\n",
  544. in_port, DP_MAX_PORTS);
  545. return -EINVAL;
  546. }
  547. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  548. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  549. } else if (!is_mask) {
  550. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  551. }
  552. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  553. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  554. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  555. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  556. }
  557. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  558. if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  559. is_mask))
  560. return -EINVAL;
  561. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  562. }
  563. return 0;
  564. }
  565. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  566. const struct nlattr **a, bool is_mask)
  567. {
  568. int err;
  569. err = metadata_from_nlattrs(match, &attrs, a, is_mask);
  570. if (err)
  571. return err;
  572. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  573. const struct ovs_key_ethernet *eth_key;
  574. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  575. SW_FLOW_KEY_MEMCPY(match, eth.src,
  576. eth_key->eth_src, ETH_ALEN, is_mask);
  577. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  578. eth_key->eth_dst, ETH_ALEN, is_mask);
  579. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  580. }
  581. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  582. __be16 tci;
  583. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  584. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  585. if (is_mask)
  586. OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
  587. else
  588. OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
  589. return -EINVAL;
  590. }
  591. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  592. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  593. } else if (!is_mask)
  594. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  595. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  596. __be16 eth_type;
  597. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  598. if (is_mask) {
  599. /* Always exact match EtherType. */
  600. eth_type = htons(0xffff);
  601. } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
  602. OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
  603. ntohs(eth_type), ETH_P_802_3_MIN);
  604. return -EINVAL;
  605. }
  606. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  607. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  608. } else if (!is_mask) {
  609. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  610. }
  611. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  612. const struct ovs_key_ipv4 *ipv4_key;
  613. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  614. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  615. OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
  616. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  617. return -EINVAL;
  618. }
  619. SW_FLOW_KEY_PUT(match, ip.proto,
  620. ipv4_key->ipv4_proto, is_mask);
  621. SW_FLOW_KEY_PUT(match, ip.tos,
  622. ipv4_key->ipv4_tos, is_mask);
  623. SW_FLOW_KEY_PUT(match, ip.ttl,
  624. ipv4_key->ipv4_ttl, is_mask);
  625. SW_FLOW_KEY_PUT(match, ip.frag,
  626. ipv4_key->ipv4_frag, is_mask);
  627. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  628. ipv4_key->ipv4_src, is_mask);
  629. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  630. ipv4_key->ipv4_dst, is_mask);
  631. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  632. }
  633. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  634. const struct ovs_key_ipv6 *ipv6_key;
  635. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  636. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  637. OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
  638. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  639. return -EINVAL;
  640. }
  641. SW_FLOW_KEY_PUT(match, ipv6.label,
  642. ipv6_key->ipv6_label, is_mask);
  643. SW_FLOW_KEY_PUT(match, ip.proto,
  644. ipv6_key->ipv6_proto, is_mask);
  645. SW_FLOW_KEY_PUT(match, ip.tos,
  646. ipv6_key->ipv6_tclass, is_mask);
  647. SW_FLOW_KEY_PUT(match, ip.ttl,
  648. ipv6_key->ipv6_hlimit, is_mask);
  649. SW_FLOW_KEY_PUT(match, ip.frag,
  650. ipv6_key->ipv6_frag, is_mask);
  651. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  652. ipv6_key->ipv6_src,
  653. sizeof(match->key->ipv6.addr.src),
  654. is_mask);
  655. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  656. ipv6_key->ipv6_dst,
  657. sizeof(match->key->ipv6.addr.dst),
  658. is_mask);
  659. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  660. }
  661. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  662. const struct ovs_key_arp *arp_key;
  663. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  664. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  665. OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
  666. arp_key->arp_op);
  667. return -EINVAL;
  668. }
  669. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  670. arp_key->arp_sip, is_mask);
  671. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  672. arp_key->arp_tip, is_mask);
  673. SW_FLOW_KEY_PUT(match, ip.proto,
  674. ntohs(arp_key->arp_op), is_mask);
  675. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  676. arp_key->arp_sha, ETH_ALEN, is_mask);
  677. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  678. arp_key->arp_tha, ETH_ALEN, is_mask);
  679. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  680. }
  681. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  682. const struct ovs_key_mpls *mpls_key;
  683. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  684. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  685. mpls_key->mpls_lse, is_mask);
  686. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  687. }
  688. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  689. const struct ovs_key_tcp *tcp_key;
  690. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  691. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  692. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  693. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  694. }
  695. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  696. SW_FLOW_KEY_PUT(match, tp.flags,
  697. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  698. is_mask);
  699. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  700. }
  701. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  702. const struct ovs_key_udp *udp_key;
  703. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  704. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  705. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  706. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  707. }
  708. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  709. const struct ovs_key_sctp *sctp_key;
  710. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  711. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  712. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  713. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  714. }
  715. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  716. const struct ovs_key_icmp *icmp_key;
  717. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  718. SW_FLOW_KEY_PUT(match, tp.src,
  719. htons(icmp_key->icmp_type), is_mask);
  720. SW_FLOW_KEY_PUT(match, tp.dst,
  721. htons(icmp_key->icmp_code), is_mask);
  722. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  723. }
  724. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  725. const struct ovs_key_icmpv6 *icmpv6_key;
  726. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  727. SW_FLOW_KEY_PUT(match, tp.src,
  728. htons(icmpv6_key->icmpv6_type), is_mask);
  729. SW_FLOW_KEY_PUT(match, tp.dst,
  730. htons(icmpv6_key->icmpv6_code), is_mask);
  731. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  732. }
  733. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  734. const struct ovs_key_nd *nd_key;
  735. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  736. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  737. nd_key->nd_target,
  738. sizeof(match->key->ipv6.nd.target),
  739. is_mask);
  740. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  741. nd_key->nd_sll, ETH_ALEN, is_mask);
  742. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  743. nd_key->nd_tll, ETH_ALEN, is_mask);
  744. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  745. }
  746. if (attrs != 0) {
  747. OVS_NLERR("Unknown key attributes (%llx).\n",
  748. (unsigned long long)attrs);
  749. return -EINVAL;
  750. }
  751. return 0;
  752. }
  753. static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
  754. {
  755. struct nlattr *nla;
  756. int rem;
  757. /* The nlattr stream should already have been validated */
  758. nla_for_each_nested(nla, attr, rem) {
  759. /* We assume that ovs_key_lens[type] == -1 means that type is a
  760. * nested attribute
  761. */
  762. if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
  763. nlattr_set(nla, val, false);
  764. else
  765. memset(nla_data(nla), val, nla_len(nla));
  766. }
  767. }
  768. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  769. {
  770. nlattr_set(attr, val, true);
  771. }
  772. /**
  773. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  774. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  775. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  776. * does not include any don't care bit.
  777. * @match: receives the extracted flow match information.
  778. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  779. * sequence. The fields should of the packet that triggered the creation
  780. * of this flow.
  781. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  782. * attribute specifies the mask field of the wildcarded flow.
  783. */
  784. int ovs_nla_get_match(struct sw_flow_match *match,
  785. const struct nlattr *key,
  786. const struct nlattr *mask)
  787. {
  788. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  789. const struct nlattr *encap;
  790. struct nlattr *newmask = NULL;
  791. u64 key_attrs = 0;
  792. u64 mask_attrs = 0;
  793. bool encap_valid = false;
  794. int err;
  795. err = parse_flow_nlattrs(key, a, &key_attrs);
  796. if (err)
  797. return err;
  798. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  799. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  800. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  801. __be16 tci;
  802. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  803. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  804. OVS_NLERR("Invalid Vlan frame.\n");
  805. return -EINVAL;
  806. }
  807. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  808. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  809. encap = a[OVS_KEY_ATTR_ENCAP];
  810. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  811. encap_valid = true;
  812. if (tci & htons(VLAN_TAG_PRESENT)) {
  813. err = parse_flow_nlattrs(encap, a, &key_attrs);
  814. if (err)
  815. return err;
  816. } else if (!tci) {
  817. /* Corner case for truncated 802.1Q header. */
  818. if (nla_len(encap)) {
  819. OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
  820. return -EINVAL;
  821. }
  822. } else {
  823. OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
  824. return -EINVAL;
  825. }
  826. }
  827. err = ovs_key_from_nlattrs(match, key_attrs, a, false);
  828. if (err)
  829. return err;
  830. if (match->mask && !mask) {
  831. /* Create an exact match mask. We need to set to 0xff all the
  832. * 'match->mask' fields that have been touched in 'match->key'.
  833. * We cannot simply memset 'match->mask', because padding bytes
  834. * and fields not specified in 'match->key' should be left to 0.
  835. * Instead, we use a stream of netlink attributes, copied from
  836. * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
  837. * of filling 'match->mask' appropriately.
  838. */
  839. newmask = kmemdup(key, nla_total_size(nla_len(key)),
  840. GFP_KERNEL);
  841. if (!newmask)
  842. return -ENOMEM;
  843. mask_set_nlattr(newmask, 0xff);
  844. /* The userspace does not send tunnel attributes that are 0,
  845. * but we should not wildcard them nonetheless.
  846. */
  847. if (match->key->tun_key.ipv4_dst)
  848. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
  849. mask = newmask;
  850. }
  851. if (mask) {
  852. err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
  853. if (err)
  854. goto free_newmask;
  855. if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
  856. __be16 eth_type = 0;
  857. __be16 tci = 0;
  858. if (!encap_valid) {
  859. OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
  860. err = -EINVAL;
  861. goto free_newmask;
  862. }
  863. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  864. if (a[OVS_KEY_ATTR_ETHERTYPE])
  865. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  866. if (eth_type == htons(0xffff)) {
  867. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  868. encap = a[OVS_KEY_ATTR_ENCAP];
  869. err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
  870. if (err)
  871. goto free_newmask;
  872. } else {
  873. OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
  874. ntohs(eth_type));
  875. err = -EINVAL;
  876. goto free_newmask;
  877. }
  878. if (a[OVS_KEY_ATTR_VLAN])
  879. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  880. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  881. OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
  882. err = -EINVAL;
  883. goto free_newmask;
  884. }
  885. }
  886. err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
  887. if (err)
  888. goto free_newmask;
  889. }
  890. if (!match_validate(match, key_attrs, mask_attrs))
  891. err = -EINVAL;
  892. free_newmask:
  893. kfree(newmask);
  894. return err;
  895. }
  896. /**
  897. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  898. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  899. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  900. * sequence.
  901. *
  902. * This parses a series of Netlink attributes that form a flow key, which must
  903. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  904. * get the metadata, that is, the parts of the flow key that cannot be
  905. * extracted from the packet itself.
  906. */
  907. int ovs_nla_get_flow_metadata(const struct nlattr *attr,
  908. struct sw_flow_key *key)
  909. {
  910. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  911. struct sw_flow_match match;
  912. u64 attrs = 0;
  913. int err;
  914. err = parse_flow_nlattrs(attr, a, &attrs);
  915. if (err)
  916. return -EINVAL;
  917. memset(&match, 0, sizeof(match));
  918. match.key = key;
  919. key->phy.in_port = DP_MAX_PORTS;
  920. return metadata_from_nlattrs(&match, &attrs, a, false);
  921. }
  922. int ovs_nla_put_flow(const struct sw_flow_key *swkey,
  923. const struct sw_flow_key *output, struct sk_buff *skb)
  924. {
  925. struct ovs_key_ethernet *eth_key;
  926. struct nlattr *nla, *encap;
  927. bool is_mask = (swkey != output);
  928. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  929. goto nla_put_failure;
  930. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  931. goto nla_put_failure;
  932. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  933. goto nla_put_failure;
  934. if ((swkey->tun_key.ipv4_dst || is_mask)) {
  935. const struct geneve_opt *opts = NULL;
  936. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  937. opts = GENEVE_OPTS(output, swkey->tun_opts_len);
  938. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  939. swkey->tun_opts_len))
  940. goto nla_put_failure;
  941. }
  942. if (swkey->phy.in_port == DP_MAX_PORTS) {
  943. if (is_mask && (output->phy.in_port == 0xffff))
  944. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  945. goto nla_put_failure;
  946. } else {
  947. u16 upper_u16;
  948. upper_u16 = !is_mask ? 0 : 0xffff;
  949. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  950. (upper_u16 << 16) | output->phy.in_port))
  951. goto nla_put_failure;
  952. }
  953. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  954. goto nla_put_failure;
  955. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  956. if (!nla)
  957. goto nla_put_failure;
  958. eth_key = nla_data(nla);
  959. ether_addr_copy(eth_key->eth_src, output->eth.src);
  960. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  961. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  962. __be16 eth_type;
  963. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  964. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  965. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  966. goto nla_put_failure;
  967. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  968. if (!swkey->eth.tci)
  969. goto unencap;
  970. } else
  971. encap = NULL;
  972. if (swkey->eth.type == htons(ETH_P_802_2)) {
  973. /*
  974. * Ethertype 802.2 is represented in the netlink with omitted
  975. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  976. * 0xffff in the mask attribute. Ethertype can also
  977. * be wildcarded.
  978. */
  979. if (is_mask && output->eth.type)
  980. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  981. output->eth.type))
  982. goto nla_put_failure;
  983. goto unencap;
  984. }
  985. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  986. goto nla_put_failure;
  987. if (swkey->eth.type == htons(ETH_P_IP)) {
  988. struct ovs_key_ipv4 *ipv4_key;
  989. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  990. if (!nla)
  991. goto nla_put_failure;
  992. ipv4_key = nla_data(nla);
  993. ipv4_key->ipv4_src = output->ipv4.addr.src;
  994. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  995. ipv4_key->ipv4_proto = output->ip.proto;
  996. ipv4_key->ipv4_tos = output->ip.tos;
  997. ipv4_key->ipv4_ttl = output->ip.ttl;
  998. ipv4_key->ipv4_frag = output->ip.frag;
  999. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1000. struct ovs_key_ipv6 *ipv6_key;
  1001. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1002. if (!nla)
  1003. goto nla_put_failure;
  1004. ipv6_key = nla_data(nla);
  1005. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1006. sizeof(ipv6_key->ipv6_src));
  1007. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1008. sizeof(ipv6_key->ipv6_dst));
  1009. ipv6_key->ipv6_label = output->ipv6.label;
  1010. ipv6_key->ipv6_proto = output->ip.proto;
  1011. ipv6_key->ipv6_tclass = output->ip.tos;
  1012. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1013. ipv6_key->ipv6_frag = output->ip.frag;
  1014. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1015. swkey->eth.type == htons(ETH_P_RARP)) {
  1016. struct ovs_key_arp *arp_key;
  1017. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1018. if (!nla)
  1019. goto nla_put_failure;
  1020. arp_key = nla_data(nla);
  1021. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1022. arp_key->arp_sip = output->ipv4.addr.src;
  1023. arp_key->arp_tip = output->ipv4.addr.dst;
  1024. arp_key->arp_op = htons(output->ip.proto);
  1025. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1026. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1027. } else if (eth_p_mpls(swkey->eth.type)) {
  1028. struct ovs_key_mpls *mpls_key;
  1029. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1030. if (!nla)
  1031. goto nla_put_failure;
  1032. mpls_key = nla_data(nla);
  1033. mpls_key->mpls_lse = output->mpls.top_lse;
  1034. }
  1035. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1036. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1037. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1038. if (swkey->ip.proto == IPPROTO_TCP) {
  1039. struct ovs_key_tcp *tcp_key;
  1040. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1041. if (!nla)
  1042. goto nla_put_failure;
  1043. tcp_key = nla_data(nla);
  1044. tcp_key->tcp_src = output->tp.src;
  1045. tcp_key->tcp_dst = output->tp.dst;
  1046. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1047. output->tp.flags))
  1048. goto nla_put_failure;
  1049. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1050. struct ovs_key_udp *udp_key;
  1051. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1052. if (!nla)
  1053. goto nla_put_failure;
  1054. udp_key = nla_data(nla);
  1055. udp_key->udp_src = output->tp.src;
  1056. udp_key->udp_dst = output->tp.dst;
  1057. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1058. struct ovs_key_sctp *sctp_key;
  1059. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1060. if (!nla)
  1061. goto nla_put_failure;
  1062. sctp_key = nla_data(nla);
  1063. sctp_key->sctp_src = output->tp.src;
  1064. sctp_key->sctp_dst = output->tp.dst;
  1065. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1066. swkey->ip.proto == IPPROTO_ICMP) {
  1067. struct ovs_key_icmp *icmp_key;
  1068. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1069. if (!nla)
  1070. goto nla_put_failure;
  1071. icmp_key = nla_data(nla);
  1072. icmp_key->icmp_type = ntohs(output->tp.src);
  1073. icmp_key->icmp_code = ntohs(output->tp.dst);
  1074. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1075. swkey->ip.proto == IPPROTO_ICMPV6) {
  1076. struct ovs_key_icmpv6 *icmpv6_key;
  1077. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1078. sizeof(*icmpv6_key));
  1079. if (!nla)
  1080. goto nla_put_failure;
  1081. icmpv6_key = nla_data(nla);
  1082. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1083. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1084. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1085. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1086. struct ovs_key_nd *nd_key;
  1087. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1088. if (!nla)
  1089. goto nla_put_failure;
  1090. nd_key = nla_data(nla);
  1091. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1092. sizeof(nd_key->nd_target));
  1093. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1094. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1095. }
  1096. }
  1097. }
  1098. unencap:
  1099. if (encap)
  1100. nla_nest_end(skb, encap);
  1101. return 0;
  1102. nla_put_failure:
  1103. return -EMSGSIZE;
  1104. }
  1105. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1106. struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
  1107. {
  1108. struct sw_flow_actions *sfa;
  1109. if (size > MAX_ACTIONS_BUFSIZE) {
  1110. OVS_NLERR("Flow action size (%u bytes) exceeds maximum", size);
  1111. return ERR_PTR(-EINVAL);
  1112. }
  1113. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1114. if (!sfa)
  1115. return ERR_PTR(-ENOMEM);
  1116. sfa->actions_len = 0;
  1117. return sfa;
  1118. }
  1119. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1120. * The caller must hold rcu_read_lock for this to be sensible. */
  1121. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1122. {
  1123. kfree_rcu(sf_acts, rcu);
  1124. }
  1125. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1126. int attr_len)
  1127. {
  1128. struct sw_flow_actions *acts;
  1129. int new_acts_size;
  1130. int req_size = NLA_ALIGN(attr_len);
  1131. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1132. (*sfa)->actions_len;
  1133. if (req_size <= (ksize(*sfa) - next_offset))
  1134. goto out;
  1135. new_acts_size = ksize(*sfa) * 2;
  1136. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1137. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1138. return ERR_PTR(-EMSGSIZE);
  1139. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1140. }
  1141. acts = ovs_nla_alloc_flow_actions(new_acts_size);
  1142. if (IS_ERR(acts))
  1143. return (void *)acts;
  1144. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1145. acts->actions_len = (*sfa)->actions_len;
  1146. kfree(*sfa);
  1147. *sfa = acts;
  1148. out:
  1149. (*sfa)->actions_len += req_size;
  1150. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1151. }
  1152. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1153. int attrtype, void *data, int len)
  1154. {
  1155. struct nlattr *a;
  1156. a = reserve_sfa_size(sfa, nla_attr_size(len));
  1157. if (IS_ERR(a))
  1158. return a;
  1159. a->nla_type = attrtype;
  1160. a->nla_len = nla_attr_size(len);
  1161. if (data)
  1162. memcpy(nla_data(a), data, len);
  1163. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1164. return a;
  1165. }
  1166. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1167. void *data, int len)
  1168. {
  1169. struct nlattr *a;
  1170. a = __add_action(sfa, attrtype, data, len);
  1171. if (IS_ERR(a))
  1172. return PTR_ERR(a);
  1173. return 0;
  1174. }
  1175. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1176. int attrtype)
  1177. {
  1178. int used = (*sfa)->actions_len;
  1179. int err;
  1180. err = add_action(sfa, attrtype, NULL, 0);
  1181. if (err)
  1182. return err;
  1183. return used;
  1184. }
  1185. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1186. int st_offset)
  1187. {
  1188. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1189. st_offset);
  1190. a->nla_len = sfa->actions_len - st_offset;
  1191. }
  1192. static int ovs_nla_copy_actions__(const struct nlattr *attr,
  1193. const struct sw_flow_key *key,
  1194. int depth, struct sw_flow_actions **sfa,
  1195. __be16 eth_type, __be16 vlan_tci);
  1196. static int validate_and_copy_sample(const struct nlattr *attr,
  1197. const struct sw_flow_key *key, int depth,
  1198. struct sw_flow_actions **sfa,
  1199. __be16 eth_type, __be16 vlan_tci)
  1200. {
  1201. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1202. const struct nlattr *probability, *actions;
  1203. const struct nlattr *a;
  1204. int rem, start, err, st_acts;
  1205. memset(attrs, 0, sizeof(attrs));
  1206. nla_for_each_nested(a, attr, rem) {
  1207. int type = nla_type(a);
  1208. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1209. return -EINVAL;
  1210. attrs[type] = a;
  1211. }
  1212. if (rem)
  1213. return -EINVAL;
  1214. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1215. if (!probability || nla_len(probability) != sizeof(u32))
  1216. return -EINVAL;
  1217. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1218. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1219. return -EINVAL;
  1220. /* validation done, copy sample action. */
  1221. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
  1222. if (start < 0)
  1223. return start;
  1224. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1225. nla_data(probability), sizeof(u32));
  1226. if (err)
  1227. return err;
  1228. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
  1229. if (st_acts < 0)
  1230. return st_acts;
  1231. err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
  1232. eth_type, vlan_tci);
  1233. if (err)
  1234. return err;
  1235. add_nested_action_end(*sfa, st_acts);
  1236. add_nested_action_end(*sfa, start);
  1237. return 0;
  1238. }
  1239. static int validate_tp_port(const struct sw_flow_key *flow_key,
  1240. __be16 eth_type)
  1241. {
  1242. if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
  1243. (flow_key->tp.src || flow_key->tp.dst))
  1244. return 0;
  1245. return -EINVAL;
  1246. }
  1247. void ovs_match_init(struct sw_flow_match *match,
  1248. struct sw_flow_key *key,
  1249. struct sw_flow_mask *mask)
  1250. {
  1251. memset(match, 0, sizeof(*match));
  1252. match->key = key;
  1253. match->mask = mask;
  1254. memset(key, 0, sizeof(*key));
  1255. if (mask) {
  1256. memset(&mask->key, 0, sizeof(mask->key));
  1257. mask->range.start = mask->range.end = 0;
  1258. }
  1259. }
  1260. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1261. struct sw_flow_actions **sfa)
  1262. {
  1263. struct sw_flow_match match;
  1264. struct sw_flow_key key;
  1265. struct ovs_tunnel_info *tun_info;
  1266. struct nlattr *a;
  1267. int err, start;
  1268. ovs_match_init(&match, &key, NULL);
  1269. err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
  1270. if (err)
  1271. return err;
  1272. if (key.tun_opts_len) {
  1273. struct geneve_opt *option = GENEVE_OPTS(&key,
  1274. key.tun_opts_len);
  1275. int opts_len = key.tun_opts_len;
  1276. bool crit_opt = false;
  1277. while (opts_len > 0) {
  1278. int len;
  1279. if (opts_len < sizeof(*option))
  1280. return -EINVAL;
  1281. len = sizeof(*option) + option->length * 4;
  1282. if (len > opts_len)
  1283. return -EINVAL;
  1284. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1285. option = (struct geneve_opt *)((u8 *)option + len);
  1286. opts_len -= len;
  1287. };
  1288. key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1289. };
  1290. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
  1291. if (start < 0)
  1292. return start;
  1293. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1294. sizeof(*tun_info) + key.tun_opts_len);
  1295. if (IS_ERR(a))
  1296. return PTR_ERR(a);
  1297. tun_info = nla_data(a);
  1298. tun_info->tunnel = key.tun_key;
  1299. tun_info->options_len = key.tun_opts_len;
  1300. if (tun_info->options_len) {
  1301. /* We need to store the options in the action itself since
  1302. * everything else will go away after flow setup. We can append
  1303. * it to tun_info and then point there.
  1304. */
  1305. memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
  1306. key.tun_opts_len);
  1307. tun_info->options = (struct geneve_opt *)(tun_info + 1);
  1308. } else {
  1309. tun_info->options = NULL;
  1310. }
  1311. add_nested_action_end(*sfa, start);
  1312. return err;
  1313. }
  1314. static int validate_set(const struct nlattr *a,
  1315. const struct sw_flow_key *flow_key,
  1316. struct sw_flow_actions **sfa,
  1317. bool *set_tun, __be16 eth_type)
  1318. {
  1319. const struct nlattr *ovs_key = nla_data(a);
  1320. int key_type = nla_type(ovs_key);
  1321. /* There can be only one key in a action */
  1322. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1323. return -EINVAL;
  1324. if (key_type > OVS_KEY_ATTR_MAX ||
  1325. (ovs_key_lens[key_type] != nla_len(ovs_key) &&
  1326. ovs_key_lens[key_type] != -1))
  1327. return -EINVAL;
  1328. switch (key_type) {
  1329. const struct ovs_key_ipv4 *ipv4_key;
  1330. const struct ovs_key_ipv6 *ipv6_key;
  1331. int err;
  1332. case OVS_KEY_ATTR_PRIORITY:
  1333. case OVS_KEY_ATTR_SKB_MARK:
  1334. case OVS_KEY_ATTR_ETHERNET:
  1335. break;
  1336. case OVS_KEY_ATTR_TUNNEL:
  1337. if (eth_p_mpls(eth_type))
  1338. return -EINVAL;
  1339. *set_tun = true;
  1340. err = validate_and_copy_set_tun(a, sfa);
  1341. if (err)
  1342. return err;
  1343. break;
  1344. case OVS_KEY_ATTR_IPV4:
  1345. if (eth_type != htons(ETH_P_IP))
  1346. return -EINVAL;
  1347. if (!flow_key->ip.proto)
  1348. return -EINVAL;
  1349. ipv4_key = nla_data(ovs_key);
  1350. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1351. return -EINVAL;
  1352. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1353. return -EINVAL;
  1354. break;
  1355. case OVS_KEY_ATTR_IPV6:
  1356. if (eth_type != htons(ETH_P_IPV6))
  1357. return -EINVAL;
  1358. if (!flow_key->ip.proto)
  1359. return -EINVAL;
  1360. ipv6_key = nla_data(ovs_key);
  1361. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1362. return -EINVAL;
  1363. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1364. return -EINVAL;
  1365. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1366. return -EINVAL;
  1367. break;
  1368. case OVS_KEY_ATTR_TCP:
  1369. if (flow_key->ip.proto != IPPROTO_TCP)
  1370. return -EINVAL;
  1371. return validate_tp_port(flow_key, eth_type);
  1372. case OVS_KEY_ATTR_UDP:
  1373. if (flow_key->ip.proto != IPPROTO_UDP)
  1374. return -EINVAL;
  1375. return validate_tp_port(flow_key, eth_type);
  1376. case OVS_KEY_ATTR_MPLS:
  1377. if (!eth_p_mpls(eth_type))
  1378. return -EINVAL;
  1379. break;
  1380. case OVS_KEY_ATTR_SCTP:
  1381. if (flow_key->ip.proto != IPPROTO_SCTP)
  1382. return -EINVAL;
  1383. return validate_tp_port(flow_key, eth_type);
  1384. default:
  1385. return -EINVAL;
  1386. }
  1387. return 0;
  1388. }
  1389. static int validate_userspace(const struct nlattr *attr)
  1390. {
  1391. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1392. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1393. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1394. };
  1395. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1396. int error;
  1397. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1398. attr, userspace_policy);
  1399. if (error)
  1400. return error;
  1401. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1402. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1403. return -EINVAL;
  1404. return 0;
  1405. }
  1406. static int copy_action(const struct nlattr *from,
  1407. struct sw_flow_actions **sfa)
  1408. {
  1409. int totlen = NLA_ALIGN(from->nla_len);
  1410. struct nlattr *to;
  1411. to = reserve_sfa_size(sfa, from->nla_len);
  1412. if (IS_ERR(to))
  1413. return PTR_ERR(to);
  1414. memcpy(to, from, totlen);
  1415. return 0;
  1416. }
  1417. static int ovs_nla_copy_actions__(const struct nlattr *attr,
  1418. const struct sw_flow_key *key,
  1419. int depth, struct sw_flow_actions **sfa,
  1420. __be16 eth_type, __be16 vlan_tci)
  1421. {
  1422. const struct nlattr *a;
  1423. bool out_tnl_port = false;
  1424. int rem, err;
  1425. if (depth >= SAMPLE_ACTION_DEPTH)
  1426. return -EOVERFLOW;
  1427. nla_for_each_nested(a, attr, rem) {
  1428. /* Expected argument lengths, (u32)-1 for variable length. */
  1429. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1430. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1431. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1432. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1433. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1434. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1435. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1436. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1437. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1438. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1439. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1440. };
  1441. const struct ovs_action_push_vlan *vlan;
  1442. int type = nla_type(a);
  1443. bool skip_copy;
  1444. if (type > OVS_ACTION_ATTR_MAX ||
  1445. (action_lens[type] != nla_len(a) &&
  1446. action_lens[type] != (u32)-1))
  1447. return -EINVAL;
  1448. skip_copy = false;
  1449. switch (type) {
  1450. case OVS_ACTION_ATTR_UNSPEC:
  1451. return -EINVAL;
  1452. case OVS_ACTION_ATTR_USERSPACE:
  1453. err = validate_userspace(a);
  1454. if (err)
  1455. return err;
  1456. break;
  1457. case OVS_ACTION_ATTR_OUTPUT:
  1458. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1459. return -EINVAL;
  1460. out_tnl_port = false;
  1461. break;
  1462. case OVS_ACTION_ATTR_HASH: {
  1463. const struct ovs_action_hash *act_hash = nla_data(a);
  1464. switch (act_hash->hash_alg) {
  1465. case OVS_HASH_ALG_L4:
  1466. break;
  1467. default:
  1468. return -EINVAL;
  1469. }
  1470. break;
  1471. }
  1472. case OVS_ACTION_ATTR_POP_VLAN:
  1473. vlan_tci = htons(0);
  1474. break;
  1475. case OVS_ACTION_ATTR_PUSH_VLAN:
  1476. vlan = nla_data(a);
  1477. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1478. return -EINVAL;
  1479. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1480. return -EINVAL;
  1481. vlan_tci = vlan->vlan_tci;
  1482. break;
  1483. case OVS_ACTION_ATTR_RECIRC:
  1484. break;
  1485. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1486. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1487. /* Networking stack do not allow simultaneous Tunnel
  1488. * and MPLS GSO.
  1489. */
  1490. if (out_tnl_port)
  1491. return -EINVAL;
  1492. if (!eth_p_mpls(mpls->mpls_ethertype))
  1493. return -EINVAL;
  1494. /* Prohibit push MPLS other than to a white list
  1495. * for packets that have a known tag order.
  1496. */
  1497. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1498. (eth_type != htons(ETH_P_IP) &&
  1499. eth_type != htons(ETH_P_IPV6) &&
  1500. eth_type != htons(ETH_P_ARP) &&
  1501. eth_type != htons(ETH_P_RARP) &&
  1502. !eth_p_mpls(eth_type)))
  1503. return -EINVAL;
  1504. eth_type = mpls->mpls_ethertype;
  1505. break;
  1506. }
  1507. case OVS_ACTION_ATTR_POP_MPLS:
  1508. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1509. !eth_p_mpls(eth_type))
  1510. return -EINVAL;
  1511. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1512. * as there is no check here to ensure that the new
  1513. * eth_type is valid and thus set actions could
  1514. * write off the end of the packet or otherwise
  1515. * corrupt it.
  1516. *
  1517. * Support for these actions is planned using packet
  1518. * recirculation.
  1519. */
  1520. eth_type = htons(0);
  1521. break;
  1522. case OVS_ACTION_ATTR_SET:
  1523. err = validate_set(a, key, sfa,
  1524. &out_tnl_port, eth_type);
  1525. if (err)
  1526. return err;
  1527. skip_copy = out_tnl_port;
  1528. break;
  1529. case OVS_ACTION_ATTR_SAMPLE:
  1530. err = validate_and_copy_sample(a, key, depth, sfa,
  1531. eth_type, vlan_tci);
  1532. if (err)
  1533. return err;
  1534. skip_copy = true;
  1535. break;
  1536. default:
  1537. OVS_NLERR("Unknown tunnel attribute (%d).\n", type);
  1538. return -EINVAL;
  1539. }
  1540. if (!skip_copy) {
  1541. err = copy_action(a, sfa);
  1542. if (err)
  1543. return err;
  1544. }
  1545. }
  1546. if (rem > 0)
  1547. return -EINVAL;
  1548. return 0;
  1549. }
  1550. int ovs_nla_copy_actions(const struct nlattr *attr,
  1551. const struct sw_flow_key *key,
  1552. struct sw_flow_actions **sfa)
  1553. {
  1554. return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
  1555. key->eth.tci);
  1556. }
  1557. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1558. {
  1559. const struct nlattr *a;
  1560. struct nlattr *start;
  1561. int err = 0, rem;
  1562. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1563. if (!start)
  1564. return -EMSGSIZE;
  1565. nla_for_each_nested(a, attr, rem) {
  1566. int type = nla_type(a);
  1567. struct nlattr *st_sample;
  1568. switch (type) {
  1569. case OVS_SAMPLE_ATTR_PROBABILITY:
  1570. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1571. sizeof(u32), nla_data(a)))
  1572. return -EMSGSIZE;
  1573. break;
  1574. case OVS_SAMPLE_ATTR_ACTIONS:
  1575. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1576. if (!st_sample)
  1577. return -EMSGSIZE;
  1578. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1579. if (err)
  1580. return err;
  1581. nla_nest_end(skb, st_sample);
  1582. break;
  1583. }
  1584. }
  1585. nla_nest_end(skb, start);
  1586. return err;
  1587. }
  1588. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1589. {
  1590. const struct nlattr *ovs_key = nla_data(a);
  1591. int key_type = nla_type(ovs_key);
  1592. struct nlattr *start;
  1593. int err;
  1594. switch (key_type) {
  1595. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1596. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1597. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1598. if (!start)
  1599. return -EMSGSIZE;
  1600. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1601. tun_info->options_len ?
  1602. tun_info->options : NULL,
  1603. tun_info->options_len);
  1604. if (err)
  1605. return err;
  1606. nla_nest_end(skb, start);
  1607. break;
  1608. }
  1609. default:
  1610. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1611. return -EMSGSIZE;
  1612. break;
  1613. }
  1614. return 0;
  1615. }
  1616. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1617. {
  1618. const struct nlattr *a;
  1619. int rem, err;
  1620. nla_for_each_attr(a, attr, len, rem) {
  1621. int type = nla_type(a);
  1622. switch (type) {
  1623. case OVS_ACTION_ATTR_SET:
  1624. err = set_action_to_attr(a, skb);
  1625. if (err)
  1626. return err;
  1627. break;
  1628. case OVS_ACTION_ATTR_SAMPLE:
  1629. err = sample_action_to_attr(a, skb);
  1630. if (err)
  1631. return err;
  1632. break;
  1633. default:
  1634. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1635. return -EMSGSIZE;
  1636. break;
  1637. }
  1638. }
  1639. return 0;
  1640. }