flow_netlink.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  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. /**
  1018. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1019. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  1020. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1021. * sequence.
  1022. * @log: Boolean to allow kernel error logging. Normally true, but when
  1023. * probing for feature compatibility this should be passed in as false to
  1024. * suppress unnecessary error logging.
  1025. *
  1026. * This parses a series of Netlink attributes that form a flow key, which must
  1027. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1028. * get the metadata, that is, the parts of the flow key that cannot be
  1029. * extracted from the packet itself.
  1030. */
  1031. int ovs_nla_get_flow_metadata(const struct nlattr *attr,
  1032. struct sw_flow_key *key,
  1033. bool log)
  1034. {
  1035. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1036. struct sw_flow_match match;
  1037. u64 attrs = 0;
  1038. int err;
  1039. err = parse_flow_nlattrs(attr, a, &attrs, log);
  1040. if (err)
  1041. return -EINVAL;
  1042. memset(&match, 0, sizeof(match));
  1043. match.key = key;
  1044. key->phy.in_port = DP_MAX_PORTS;
  1045. return metadata_from_nlattrs(&match, &attrs, a, false, log);
  1046. }
  1047. int ovs_nla_put_flow(const struct sw_flow_key *swkey,
  1048. const struct sw_flow_key *output, struct sk_buff *skb)
  1049. {
  1050. struct ovs_key_ethernet *eth_key;
  1051. struct nlattr *nla, *encap;
  1052. bool is_mask = (swkey != output);
  1053. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1054. goto nla_put_failure;
  1055. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1056. goto nla_put_failure;
  1057. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1058. goto nla_put_failure;
  1059. if ((swkey->tun_key.ipv4_dst || is_mask)) {
  1060. const void *opts = NULL;
  1061. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1062. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1063. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  1064. swkey->tun_opts_len))
  1065. goto nla_put_failure;
  1066. }
  1067. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1068. if (is_mask && (output->phy.in_port == 0xffff))
  1069. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1070. goto nla_put_failure;
  1071. } else {
  1072. u16 upper_u16;
  1073. upper_u16 = !is_mask ? 0 : 0xffff;
  1074. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1075. (upper_u16 << 16) | output->phy.in_port))
  1076. goto nla_put_failure;
  1077. }
  1078. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1079. goto nla_put_failure;
  1080. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1081. if (!nla)
  1082. goto nla_put_failure;
  1083. eth_key = nla_data(nla);
  1084. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1085. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1086. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1087. __be16 eth_type;
  1088. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1089. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1090. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1091. goto nla_put_failure;
  1092. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1093. if (!swkey->eth.tci)
  1094. goto unencap;
  1095. } else
  1096. encap = NULL;
  1097. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1098. /*
  1099. * Ethertype 802.2 is represented in the netlink with omitted
  1100. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1101. * 0xffff in the mask attribute. Ethertype can also
  1102. * be wildcarded.
  1103. */
  1104. if (is_mask && output->eth.type)
  1105. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1106. output->eth.type))
  1107. goto nla_put_failure;
  1108. goto unencap;
  1109. }
  1110. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1111. goto nla_put_failure;
  1112. if (swkey->eth.type == htons(ETH_P_IP)) {
  1113. struct ovs_key_ipv4 *ipv4_key;
  1114. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1115. if (!nla)
  1116. goto nla_put_failure;
  1117. ipv4_key = nla_data(nla);
  1118. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1119. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1120. ipv4_key->ipv4_proto = output->ip.proto;
  1121. ipv4_key->ipv4_tos = output->ip.tos;
  1122. ipv4_key->ipv4_ttl = output->ip.ttl;
  1123. ipv4_key->ipv4_frag = output->ip.frag;
  1124. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1125. struct ovs_key_ipv6 *ipv6_key;
  1126. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1127. if (!nla)
  1128. goto nla_put_failure;
  1129. ipv6_key = nla_data(nla);
  1130. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1131. sizeof(ipv6_key->ipv6_src));
  1132. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1133. sizeof(ipv6_key->ipv6_dst));
  1134. ipv6_key->ipv6_label = output->ipv6.label;
  1135. ipv6_key->ipv6_proto = output->ip.proto;
  1136. ipv6_key->ipv6_tclass = output->ip.tos;
  1137. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1138. ipv6_key->ipv6_frag = output->ip.frag;
  1139. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1140. swkey->eth.type == htons(ETH_P_RARP)) {
  1141. struct ovs_key_arp *arp_key;
  1142. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1143. if (!nla)
  1144. goto nla_put_failure;
  1145. arp_key = nla_data(nla);
  1146. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1147. arp_key->arp_sip = output->ipv4.addr.src;
  1148. arp_key->arp_tip = output->ipv4.addr.dst;
  1149. arp_key->arp_op = htons(output->ip.proto);
  1150. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1151. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1152. } else if (eth_p_mpls(swkey->eth.type)) {
  1153. struct ovs_key_mpls *mpls_key;
  1154. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1155. if (!nla)
  1156. goto nla_put_failure;
  1157. mpls_key = nla_data(nla);
  1158. mpls_key->mpls_lse = output->mpls.top_lse;
  1159. }
  1160. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1161. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1162. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1163. if (swkey->ip.proto == IPPROTO_TCP) {
  1164. struct ovs_key_tcp *tcp_key;
  1165. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1166. if (!nla)
  1167. goto nla_put_failure;
  1168. tcp_key = nla_data(nla);
  1169. tcp_key->tcp_src = output->tp.src;
  1170. tcp_key->tcp_dst = output->tp.dst;
  1171. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1172. output->tp.flags))
  1173. goto nla_put_failure;
  1174. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1175. struct ovs_key_udp *udp_key;
  1176. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1177. if (!nla)
  1178. goto nla_put_failure;
  1179. udp_key = nla_data(nla);
  1180. udp_key->udp_src = output->tp.src;
  1181. udp_key->udp_dst = output->tp.dst;
  1182. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1183. struct ovs_key_sctp *sctp_key;
  1184. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1185. if (!nla)
  1186. goto nla_put_failure;
  1187. sctp_key = nla_data(nla);
  1188. sctp_key->sctp_src = output->tp.src;
  1189. sctp_key->sctp_dst = output->tp.dst;
  1190. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1191. swkey->ip.proto == IPPROTO_ICMP) {
  1192. struct ovs_key_icmp *icmp_key;
  1193. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1194. if (!nla)
  1195. goto nla_put_failure;
  1196. icmp_key = nla_data(nla);
  1197. icmp_key->icmp_type = ntohs(output->tp.src);
  1198. icmp_key->icmp_code = ntohs(output->tp.dst);
  1199. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1200. swkey->ip.proto == IPPROTO_ICMPV6) {
  1201. struct ovs_key_icmpv6 *icmpv6_key;
  1202. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1203. sizeof(*icmpv6_key));
  1204. if (!nla)
  1205. goto nla_put_failure;
  1206. icmpv6_key = nla_data(nla);
  1207. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1208. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1209. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1210. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1211. struct ovs_key_nd *nd_key;
  1212. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1213. if (!nla)
  1214. goto nla_put_failure;
  1215. nd_key = nla_data(nla);
  1216. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1217. sizeof(nd_key->nd_target));
  1218. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1219. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1220. }
  1221. }
  1222. }
  1223. unencap:
  1224. if (encap)
  1225. nla_nest_end(skb, encap);
  1226. return 0;
  1227. nla_put_failure:
  1228. return -EMSGSIZE;
  1229. }
  1230. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1231. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1232. {
  1233. struct sw_flow_actions *sfa;
  1234. if (size > MAX_ACTIONS_BUFSIZE) {
  1235. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1236. return ERR_PTR(-EINVAL);
  1237. }
  1238. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1239. if (!sfa)
  1240. return ERR_PTR(-ENOMEM);
  1241. sfa->actions_len = 0;
  1242. return sfa;
  1243. }
  1244. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1245. * The caller must hold rcu_read_lock for this to be sensible. */
  1246. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1247. {
  1248. kfree_rcu(sf_acts, rcu);
  1249. }
  1250. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1251. int attr_len, bool log)
  1252. {
  1253. struct sw_flow_actions *acts;
  1254. int new_acts_size;
  1255. int req_size = NLA_ALIGN(attr_len);
  1256. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1257. (*sfa)->actions_len;
  1258. if (req_size <= (ksize(*sfa) - next_offset))
  1259. goto out;
  1260. new_acts_size = ksize(*sfa) * 2;
  1261. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1262. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1263. return ERR_PTR(-EMSGSIZE);
  1264. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1265. }
  1266. acts = nla_alloc_flow_actions(new_acts_size, log);
  1267. if (IS_ERR(acts))
  1268. return (void *)acts;
  1269. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1270. acts->actions_len = (*sfa)->actions_len;
  1271. kfree(*sfa);
  1272. *sfa = acts;
  1273. out:
  1274. (*sfa)->actions_len += req_size;
  1275. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1276. }
  1277. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1278. int attrtype, void *data, int len, bool log)
  1279. {
  1280. struct nlattr *a;
  1281. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1282. if (IS_ERR(a))
  1283. return a;
  1284. a->nla_type = attrtype;
  1285. a->nla_len = nla_attr_size(len);
  1286. if (data)
  1287. memcpy(nla_data(a), data, len);
  1288. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1289. return a;
  1290. }
  1291. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1292. void *data, int len, bool log)
  1293. {
  1294. struct nlattr *a;
  1295. a = __add_action(sfa, attrtype, data, len, log);
  1296. return PTR_ERR_OR_ZERO(a);
  1297. }
  1298. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1299. int attrtype, bool log)
  1300. {
  1301. int used = (*sfa)->actions_len;
  1302. int err;
  1303. err = add_action(sfa, attrtype, NULL, 0, log);
  1304. if (err)
  1305. return err;
  1306. return used;
  1307. }
  1308. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1309. int st_offset)
  1310. {
  1311. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1312. st_offset);
  1313. a->nla_len = sfa->actions_len - st_offset;
  1314. }
  1315. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1316. const struct sw_flow_key *key,
  1317. int depth, struct sw_flow_actions **sfa,
  1318. __be16 eth_type, __be16 vlan_tci, bool log);
  1319. static int validate_and_copy_sample(const struct nlattr *attr,
  1320. const struct sw_flow_key *key, int depth,
  1321. struct sw_flow_actions **sfa,
  1322. __be16 eth_type, __be16 vlan_tci, bool log)
  1323. {
  1324. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1325. const struct nlattr *probability, *actions;
  1326. const struct nlattr *a;
  1327. int rem, start, err, st_acts;
  1328. memset(attrs, 0, sizeof(attrs));
  1329. nla_for_each_nested(a, attr, rem) {
  1330. int type = nla_type(a);
  1331. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1332. return -EINVAL;
  1333. attrs[type] = a;
  1334. }
  1335. if (rem)
  1336. return -EINVAL;
  1337. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1338. if (!probability || nla_len(probability) != sizeof(u32))
  1339. return -EINVAL;
  1340. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1341. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1342. return -EINVAL;
  1343. /* validation done, copy sample action. */
  1344. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1345. if (start < 0)
  1346. return start;
  1347. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1348. nla_data(probability), sizeof(u32), log);
  1349. if (err)
  1350. return err;
  1351. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1352. if (st_acts < 0)
  1353. return st_acts;
  1354. err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
  1355. eth_type, vlan_tci, log);
  1356. if (err)
  1357. return err;
  1358. add_nested_action_end(*sfa, st_acts);
  1359. add_nested_action_end(*sfa, start);
  1360. return 0;
  1361. }
  1362. static int validate_tp_port(const struct sw_flow_key *flow_key,
  1363. __be16 eth_type)
  1364. {
  1365. if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
  1366. (flow_key->tp.src || flow_key->tp.dst))
  1367. return 0;
  1368. return -EINVAL;
  1369. }
  1370. void ovs_match_init(struct sw_flow_match *match,
  1371. struct sw_flow_key *key,
  1372. struct sw_flow_mask *mask)
  1373. {
  1374. memset(match, 0, sizeof(*match));
  1375. match->key = key;
  1376. match->mask = mask;
  1377. memset(key, 0, sizeof(*key));
  1378. if (mask) {
  1379. memset(&mask->key, 0, sizeof(mask->key));
  1380. mask->range.start = mask->range.end = 0;
  1381. }
  1382. }
  1383. static int validate_geneve_opts(struct sw_flow_key *key)
  1384. {
  1385. struct geneve_opt *option;
  1386. int opts_len = key->tun_opts_len;
  1387. bool crit_opt = false;
  1388. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1389. while (opts_len > 0) {
  1390. int len;
  1391. if (opts_len < sizeof(*option))
  1392. return -EINVAL;
  1393. len = sizeof(*option) + option->length * 4;
  1394. if (len > opts_len)
  1395. return -EINVAL;
  1396. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1397. option = (struct geneve_opt *)((u8 *)option + len);
  1398. opts_len -= len;
  1399. };
  1400. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1401. return 0;
  1402. }
  1403. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1404. struct sw_flow_actions **sfa, bool log)
  1405. {
  1406. struct sw_flow_match match;
  1407. struct sw_flow_key key;
  1408. struct ovs_tunnel_info *tun_info;
  1409. struct nlattr *a;
  1410. int err, start, opts_type;
  1411. ovs_match_init(&match, &key, NULL);
  1412. opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1413. if (opts_type < 0)
  1414. return opts_type;
  1415. if (key.tun_opts_len) {
  1416. switch (opts_type) {
  1417. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1418. err = validate_geneve_opts(&key);
  1419. if (err < 0)
  1420. return err;
  1421. break;
  1422. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1423. break;
  1424. }
  1425. };
  1426. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1427. if (start < 0)
  1428. return start;
  1429. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1430. sizeof(*tun_info) + key.tun_opts_len, log);
  1431. if (IS_ERR(a))
  1432. return PTR_ERR(a);
  1433. tun_info = nla_data(a);
  1434. tun_info->tunnel = key.tun_key;
  1435. tun_info->options_len = key.tun_opts_len;
  1436. if (tun_info->options_len) {
  1437. /* We need to store the options in the action itself since
  1438. * everything else will go away after flow setup. We can append
  1439. * it to tun_info and then point there.
  1440. */
  1441. memcpy((tun_info + 1),
  1442. TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
  1443. tun_info->options = (tun_info + 1);
  1444. } else {
  1445. tun_info->options = NULL;
  1446. }
  1447. add_nested_action_end(*sfa, start);
  1448. return err;
  1449. }
  1450. static int validate_set(const struct nlattr *a,
  1451. const struct sw_flow_key *flow_key,
  1452. struct sw_flow_actions **sfa,
  1453. bool *set_tun, __be16 eth_type, bool log)
  1454. {
  1455. const struct nlattr *ovs_key = nla_data(a);
  1456. int key_type = nla_type(ovs_key);
  1457. /* There can be only one key in a action */
  1458. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1459. return -EINVAL;
  1460. if (key_type > OVS_KEY_ATTR_MAX ||
  1461. (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
  1462. ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
  1463. return -EINVAL;
  1464. switch (key_type) {
  1465. const struct ovs_key_ipv4 *ipv4_key;
  1466. const struct ovs_key_ipv6 *ipv6_key;
  1467. int err;
  1468. case OVS_KEY_ATTR_PRIORITY:
  1469. case OVS_KEY_ATTR_SKB_MARK:
  1470. case OVS_KEY_ATTR_ETHERNET:
  1471. break;
  1472. case OVS_KEY_ATTR_TUNNEL:
  1473. if (eth_p_mpls(eth_type))
  1474. return -EINVAL;
  1475. *set_tun = true;
  1476. err = validate_and_copy_set_tun(a, sfa, log);
  1477. if (err)
  1478. return err;
  1479. break;
  1480. case OVS_KEY_ATTR_IPV4:
  1481. if (eth_type != htons(ETH_P_IP))
  1482. return -EINVAL;
  1483. if (!flow_key->ip.proto)
  1484. return -EINVAL;
  1485. ipv4_key = nla_data(ovs_key);
  1486. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1487. return -EINVAL;
  1488. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1489. return -EINVAL;
  1490. break;
  1491. case OVS_KEY_ATTR_IPV6:
  1492. if (eth_type != htons(ETH_P_IPV6))
  1493. return -EINVAL;
  1494. if (!flow_key->ip.proto)
  1495. return -EINVAL;
  1496. ipv6_key = nla_data(ovs_key);
  1497. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1498. return -EINVAL;
  1499. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1500. return -EINVAL;
  1501. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1502. return -EINVAL;
  1503. break;
  1504. case OVS_KEY_ATTR_TCP:
  1505. if (flow_key->ip.proto != IPPROTO_TCP)
  1506. return -EINVAL;
  1507. return validate_tp_port(flow_key, eth_type);
  1508. case OVS_KEY_ATTR_UDP:
  1509. if (flow_key->ip.proto != IPPROTO_UDP)
  1510. return -EINVAL;
  1511. return validate_tp_port(flow_key, eth_type);
  1512. case OVS_KEY_ATTR_MPLS:
  1513. if (!eth_p_mpls(eth_type))
  1514. return -EINVAL;
  1515. break;
  1516. case OVS_KEY_ATTR_SCTP:
  1517. if (flow_key->ip.proto != IPPROTO_SCTP)
  1518. return -EINVAL;
  1519. return validate_tp_port(flow_key, eth_type);
  1520. default:
  1521. return -EINVAL;
  1522. }
  1523. return 0;
  1524. }
  1525. static int validate_userspace(const struct nlattr *attr)
  1526. {
  1527. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1528. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1529. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1530. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1531. };
  1532. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1533. int error;
  1534. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1535. attr, userspace_policy);
  1536. if (error)
  1537. return error;
  1538. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1539. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1540. return -EINVAL;
  1541. return 0;
  1542. }
  1543. static int copy_action(const struct nlattr *from,
  1544. struct sw_flow_actions **sfa, bool log)
  1545. {
  1546. int totlen = NLA_ALIGN(from->nla_len);
  1547. struct nlattr *to;
  1548. to = reserve_sfa_size(sfa, from->nla_len, log);
  1549. if (IS_ERR(to))
  1550. return PTR_ERR(to);
  1551. memcpy(to, from, totlen);
  1552. return 0;
  1553. }
  1554. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1555. const struct sw_flow_key *key,
  1556. int depth, struct sw_flow_actions **sfa,
  1557. __be16 eth_type, __be16 vlan_tci, bool log)
  1558. {
  1559. const struct nlattr *a;
  1560. int rem, err;
  1561. if (depth >= SAMPLE_ACTION_DEPTH)
  1562. return -EOVERFLOW;
  1563. nla_for_each_nested(a, attr, rem) {
  1564. /* Expected argument lengths, (u32)-1 for variable length. */
  1565. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1566. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1567. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1568. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1569. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1570. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1571. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1572. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1573. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1574. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1575. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1576. };
  1577. const struct ovs_action_push_vlan *vlan;
  1578. int type = nla_type(a);
  1579. bool skip_copy;
  1580. if (type > OVS_ACTION_ATTR_MAX ||
  1581. (action_lens[type] != nla_len(a) &&
  1582. action_lens[type] != (u32)-1))
  1583. return -EINVAL;
  1584. skip_copy = false;
  1585. switch (type) {
  1586. case OVS_ACTION_ATTR_UNSPEC:
  1587. return -EINVAL;
  1588. case OVS_ACTION_ATTR_USERSPACE:
  1589. err = validate_userspace(a);
  1590. if (err)
  1591. return err;
  1592. break;
  1593. case OVS_ACTION_ATTR_OUTPUT:
  1594. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1595. return -EINVAL;
  1596. break;
  1597. case OVS_ACTION_ATTR_HASH: {
  1598. const struct ovs_action_hash *act_hash = nla_data(a);
  1599. switch (act_hash->hash_alg) {
  1600. case OVS_HASH_ALG_L4:
  1601. break;
  1602. default:
  1603. return -EINVAL;
  1604. }
  1605. break;
  1606. }
  1607. case OVS_ACTION_ATTR_POP_VLAN:
  1608. vlan_tci = htons(0);
  1609. break;
  1610. case OVS_ACTION_ATTR_PUSH_VLAN:
  1611. vlan = nla_data(a);
  1612. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1613. return -EINVAL;
  1614. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1615. return -EINVAL;
  1616. vlan_tci = vlan->vlan_tci;
  1617. break;
  1618. case OVS_ACTION_ATTR_RECIRC:
  1619. break;
  1620. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1621. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1622. if (!eth_p_mpls(mpls->mpls_ethertype))
  1623. return -EINVAL;
  1624. /* Prohibit push MPLS other than to a white list
  1625. * for packets that have a known tag order.
  1626. */
  1627. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1628. (eth_type != htons(ETH_P_IP) &&
  1629. eth_type != htons(ETH_P_IPV6) &&
  1630. eth_type != htons(ETH_P_ARP) &&
  1631. eth_type != htons(ETH_P_RARP) &&
  1632. !eth_p_mpls(eth_type)))
  1633. return -EINVAL;
  1634. eth_type = mpls->mpls_ethertype;
  1635. break;
  1636. }
  1637. case OVS_ACTION_ATTR_POP_MPLS:
  1638. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1639. !eth_p_mpls(eth_type))
  1640. return -EINVAL;
  1641. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1642. * as there is no check here to ensure that the new
  1643. * eth_type is valid and thus set actions could
  1644. * write off the end of the packet or otherwise
  1645. * corrupt it.
  1646. *
  1647. * Support for these actions is planned using packet
  1648. * recirculation.
  1649. */
  1650. eth_type = htons(0);
  1651. break;
  1652. case OVS_ACTION_ATTR_SET:
  1653. err = validate_set(a, key, sfa,
  1654. &skip_copy, eth_type, log);
  1655. if (err)
  1656. return err;
  1657. break;
  1658. case OVS_ACTION_ATTR_SAMPLE:
  1659. err = validate_and_copy_sample(a, key, depth, sfa,
  1660. eth_type, vlan_tci, log);
  1661. if (err)
  1662. return err;
  1663. skip_copy = true;
  1664. break;
  1665. default:
  1666. OVS_NLERR(log, "Unknown Action type %d", type);
  1667. return -EINVAL;
  1668. }
  1669. if (!skip_copy) {
  1670. err = copy_action(a, sfa, log);
  1671. if (err)
  1672. return err;
  1673. }
  1674. }
  1675. if (rem > 0)
  1676. return -EINVAL;
  1677. return 0;
  1678. }
  1679. int ovs_nla_copy_actions(const struct nlattr *attr,
  1680. const struct sw_flow_key *key,
  1681. struct sw_flow_actions **sfa, bool log)
  1682. {
  1683. int err;
  1684. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1685. if (IS_ERR(*sfa))
  1686. return PTR_ERR(*sfa);
  1687. err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
  1688. key->eth.tci, log);
  1689. if (err)
  1690. kfree(*sfa);
  1691. return err;
  1692. }
  1693. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1694. {
  1695. const struct nlattr *a;
  1696. struct nlattr *start;
  1697. int err = 0, rem;
  1698. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1699. if (!start)
  1700. return -EMSGSIZE;
  1701. nla_for_each_nested(a, attr, rem) {
  1702. int type = nla_type(a);
  1703. struct nlattr *st_sample;
  1704. switch (type) {
  1705. case OVS_SAMPLE_ATTR_PROBABILITY:
  1706. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1707. sizeof(u32), nla_data(a)))
  1708. return -EMSGSIZE;
  1709. break;
  1710. case OVS_SAMPLE_ATTR_ACTIONS:
  1711. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1712. if (!st_sample)
  1713. return -EMSGSIZE;
  1714. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1715. if (err)
  1716. return err;
  1717. nla_nest_end(skb, st_sample);
  1718. break;
  1719. }
  1720. }
  1721. nla_nest_end(skb, start);
  1722. return err;
  1723. }
  1724. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1725. {
  1726. const struct nlattr *ovs_key = nla_data(a);
  1727. int key_type = nla_type(ovs_key);
  1728. struct nlattr *start;
  1729. int err;
  1730. switch (key_type) {
  1731. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1732. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1733. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1734. if (!start)
  1735. return -EMSGSIZE;
  1736. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1737. tun_info->options_len ?
  1738. tun_info->options : NULL,
  1739. tun_info->options_len);
  1740. if (err)
  1741. return err;
  1742. nla_nest_end(skb, start);
  1743. break;
  1744. }
  1745. default:
  1746. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1747. return -EMSGSIZE;
  1748. break;
  1749. }
  1750. return 0;
  1751. }
  1752. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1753. {
  1754. const struct nlattr *a;
  1755. int rem, err;
  1756. nla_for_each_attr(a, attr, len, rem) {
  1757. int type = nla_type(a);
  1758. switch (type) {
  1759. case OVS_ACTION_ATTR_SET:
  1760. err = set_action_to_attr(a, skb);
  1761. if (err)
  1762. return err;
  1763. break;
  1764. case OVS_ACTION_ATTR_SAMPLE:
  1765. err = sample_action_to_attr(a, skb);
  1766. if (err)
  1767. return err;
  1768. break;
  1769. default:
  1770. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1771. return -EMSGSIZE;
  1772. break;
  1773. }
  1774. }
  1775. return 0;
  1776. }