flow_netlink.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1048. const struct sw_flow_key *output, bool is_mask,
  1049. struct sk_buff *skb)
  1050. {
  1051. struct ovs_key_ethernet *eth_key;
  1052. struct nlattr *nla, *encap;
  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. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1231. const struct sw_flow_key *output, int attr, bool is_mask,
  1232. struct sk_buff *skb)
  1233. {
  1234. int err;
  1235. struct nlattr *nla;
  1236. nla = nla_nest_start(skb, attr);
  1237. if (!nla)
  1238. return -EMSGSIZE;
  1239. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1240. if (err)
  1241. return err;
  1242. nla_nest_end(skb, nla);
  1243. return 0;
  1244. }
  1245. /* Called with ovs_mutex or RCU read lock. */
  1246. int ovs_nla_put_unmasked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1247. {
  1248. return ovs_nla_put_key(&flow->unmasked_key, &flow->unmasked_key,
  1249. OVS_FLOW_ATTR_KEY, false, skb);
  1250. }
  1251. /* Called with ovs_mutex or RCU read lock. */
  1252. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1253. {
  1254. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1255. OVS_FLOW_ATTR_MASK, true, skb);
  1256. }
  1257. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1258. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1259. {
  1260. struct sw_flow_actions *sfa;
  1261. if (size > MAX_ACTIONS_BUFSIZE) {
  1262. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1263. return ERR_PTR(-EINVAL);
  1264. }
  1265. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1266. if (!sfa)
  1267. return ERR_PTR(-ENOMEM);
  1268. sfa->actions_len = 0;
  1269. return sfa;
  1270. }
  1271. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1272. * The caller must hold rcu_read_lock for this to be sensible. */
  1273. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1274. {
  1275. kfree_rcu(sf_acts, rcu);
  1276. }
  1277. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1278. int attr_len, bool log)
  1279. {
  1280. struct sw_flow_actions *acts;
  1281. int new_acts_size;
  1282. int req_size = NLA_ALIGN(attr_len);
  1283. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1284. (*sfa)->actions_len;
  1285. if (req_size <= (ksize(*sfa) - next_offset))
  1286. goto out;
  1287. new_acts_size = ksize(*sfa) * 2;
  1288. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1289. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1290. return ERR_PTR(-EMSGSIZE);
  1291. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1292. }
  1293. acts = nla_alloc_flow_actions(new_acts_size, log);
  1294. if (IS_ERR(acts))
  1295. return (void *)acts;
  1296. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1297. acts->actions_len = (*sfa)->actions_len;
  1298. kfree(*sfa);
  1299. *sfa = acts;
  1300. out:
  1301. (*sfa)->actions_len += req_size;
  1302. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1303. }
  1304. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1305. int attrtype, void *data, int len, bool log)
  1306. {
  1307. struct nlattr *a;
  1308. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1309. if (IS_ERR(a))
  1310. return a;
  1311. a->nla_type = attrtype;
  1312. a->nla_len = nla_attr_size(len);
  1313. if (data)
  1314. memcpy(nla_data(a), data, len);
  1315. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1316. return a;
  1317. }
  1318. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1319. void *data, int len, bool log)
  1320. {
  1321. struct nlattr *a;
  1322. a = __add_action(sfa, attrtype, data, len, log);
  1323. return PTR_ERR_OR_ZERO(a);
  1324. }
  1325. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1326. int attrtype, bool log)
  1327. {
  1328. int used = (*sfa)->actions_len;
  1329. int err;
  1330. err = add_action(sfa, attrtype, NULL, 0, log);
  1331. if (err)
  1332. return err;
  1333. return used;
  1334. }
  1335. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1336. int st_offset)
  1337. {
  1338. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1339. st_offset);
  1340. a->nla_len = sfa->actions_len - st_offset;
  1341. }
  1342. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1343. const struct sw_flow_key *key,
  1344. int depth, struct sw_flow_actions **sfa,
  1345. __be16 eth_type, __be16 vlan_tci, bool log);
  1346. static int validate_and_copy_sample(const struct nlattr *attr,
  1347. const struct sw_flow_key *key, int depth,
  1348. struct sw_flow_actions **sfa,
  1349. __be16 eth_type, __be16 vlan_tci, bool log)
  1350. {
  1351. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1352. const struct nlattr *probability, *actions;
  1353. const struct nlattr *a;
  1354. int rem, start, err, st_acts;
  1355. memset(attrs, 0, sizeof(attrs));
  1356. nla_for_each_nested(a, attr, rem) {
  1357. int type = nla_type(a);
  1358. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1359. return -EINVAL;
  1360. attrs[type] = a;
  1361. }
  1362. if (rem)
  1363. return -EINVAL;
  1364. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1365. if (!probability || nla_len(probability) != sizeof(u32))
  1366. return -EINVAL;
  1367. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1368. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1369. return -EINVAL;
  1370. /* validation done, copy sample action. */
  1371. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1372. if (start < 0)
  1373. return start;
  1374. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1375. nla_data(probability), sizeof(u32), log);
  1376. if (err)
  1377. return err;
  1378. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1379. if (st_acts < 0)
  1380. return st_acts;
  1381. err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
  1382. eth_type, vlan_tci, log);
  1383. if (err)
  1384. return err;
  1385. add_nested_action_end(*sfa, st_acts);
  1386. add_nested_action_end(*sfa, start);
  1387. return 0;
  1388. }
  1389. static int validate_tp_port(const struct sw_flow_key *flow_key,
  1390. __be16 eth_type)
  1391. {
  1392. if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
  1393. (flow_key->tp.src || flow_key->tp.dst))
  1394. return 0;
  1395. return -EINVAL;
  1396. }
  1397. void ovs_match_init(struct sw_flow_match *match,
  1398. struct sw_flow_key *key,
  1399. struct sw_flow_mask *mask)
  1400. {
  1401. memset(match, 0, sizeof(*match));
  1402. match->key = key;
  1403. match->mask = mask;
  1404. memset(key, 0, sizeof(*key));
  1405. if (mask) {
  1406. memset(&mask->key, 0, sizeof(mask->key));
  1407. mask->range.start = mask->range.end = 0;
  1408. }
  1409. }
  1410. static int validate_geneve_opts(struct sw_flow_key *key)
  1411. {
  1412. struct geneve_opt *option;
  1413. int opts_len = key->tun_opts_len;
  1414. bool crit_opt = false;
  1415. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1416. while (opts_len > 0) {
  1417. int len;
  1418. if (opts_len < sizeof(*option))
  1419. return -EINVAL;
  1420. len = sizeof(*option) + option->length * 4;
  1421. if (len > opts_len)
  1422. return -EINVAL;
  1423. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1424. option = (struct geneve_opt *)((u8 *)option + len);
  1425. opts_len -= len;
  1426. };
  1427. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1428. return 0;
  1429. }
  1430. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1431. struct sw_flow_actions **sfa, bool log)
  1432. {
  1433. struct sw_flow_match match;
  1434. struct sw_flow_key key;
  1435. struct ovs_tunnel_info *tun_info;
  1436. struct nlattr *a;
  1437. int err, start, opts_type;
  1438. ovs_match_init(&match, &key, NULL);
  1439. opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1440. if (opts_type < 0)
  1441. return opts_type;
  1442. if (key.tun_opts_len) {
  1443. switch (opts_type) {
  1444. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1445. err = validate_geneve_opts(&key);
  1446. if (err < 0)
  1447. return err;
  1448. break;
  1449. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1450. break;
  1451. }
  1452. };
  1453. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1454. if (start < 0)
  1455. return start;
  1456. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1457. sizeof(*tun_info) + key.tun_opts_len, log);
  1458. if (IS_ERR(a))
  1459. return PTR_ERR(a);
  1460. tun_info = nla_data(a);
  1461. tun_info->tunnel = key.tun_key;
  1462. tun_info->options_len = key.tun_opts_len;
  1463. if (tun_info->options_len) {
  1464. /* We need to store the options in the action itself since
  1465. * everything else will go away after flow setup. We can append
  1466. * it to tun_info and then point there.
  1467. */
  1468. memcpy((tun_info + 1),
  1469. TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
  1470. tun_info->options = (tun_info + 1);
  1471. } else {
  1472. tun_info->options = NULL;
  1473. }
  1474. add_nested_action_end(*sfa, start);
  1475. return err;
  1476. }
  1477. static int validate_set(const struct nlattr *a,
  1478. const struct sw_flow_key *flow_key,
  1479. struct sw_flow_actions **sfa,
  1480. bool *set_tun, __be16 eth_type, bool log)
  1481. {
  1482. const struct nlattr *ovs_key = nla_data(a);
  1483. int key_type = nla_type(ovs_key);
  1484. /* There can be only one key in a action */
  1485. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1486. return -EINVAL;
  1487. if (key_type > OVS_KEY_ATTR_MAX ||
  1488. (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
  1489. ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
  1490. return -EINVAL;
  1491. switch (key_type) {
  1492. const struct ovs_key_ipv4 *ipv4_key;
  1493. const struct ovs_key_ipv6 *ipv6_key;
  1494. int err;
  1495. case OVS_KEY_ATTR_PRIORITY:
  1496. case OVS_KEY_ATTR_SKB_MARK:
  1497. case OVS_KEY_ATTR_ETHERNET:
  1498. break;
  1499. case OVS_KEY_ATTR_TUNNEL:
  1500. if (eth_p_mpls(eth_type))
  1501. return -EINVAL;
  1502. *set_tun = true;
  1503. err = validate_and_copy_set_tun(a, sfa, log);
  1504. if (err)
  1505. return err;
  1506. break;
  1507. case OVS_KEY_ATTR_IPV4:
  1508. if (eth_type != htons(ETH_P_IP))
  1509. return -EINVAL;
  1510. if (!flow_key->ip.proto)
  1511. return -EINVAL;
  1512. ipv4_key = nla_data(ovs_key);
  1513. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1514. return -EINVAL;
  1515. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1516. return -EINVAL;
  1517. break;
  1518. case OVS_KEY_ATTR_IPV6:
  1519. if (eth_type != htons(ETH_P_IPV6))
  1520. return -EINVAL;
  1521. if (!flow_key->ip.proto)
  1522. return -EINVAL;
  1523. ipv6_key = nla_data(ovs_key);
  1524. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1525. return -EINVAL;
  1526. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1527. return -EINVAL;
  1528. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1529. return -EINVAL;
  1530. break;
  1531. case OVS_KEY_ATTR_TCP:
  1532. if (flow_key->ip.proto != IPPROTO_TCP)
  1533. return -EINVAL;
  1534. return validate_tp_port(flow_key, eth_type);
  1535. case OVS_KEY_ATTR_UDP:
  1536. if (flow_key->ip.proto != IPPROTO_UDP)
  1537. return -EINVAL;
  1538. return validate_tp_port(flow_key, eth_type);
  1539. case OVS_KEY_ATTR_MPLS:
  1540. if (!eth_p_mpls(eth_type))
  1541. return -EINVAL;
  1542. break;
  1543. case OVS_KEY_ATTR_SCTP:
  1544. if (flow_key->ip.proto != IPPROTO_SCTP)
  1545. return -EINVAL;
  1546. return validate_tp_port(flow_key, eth_type);
  1547. default:
  1548. return -EINVAL;
  1549. }
  1550. return 0;
  1551. }
  1552. static int validate_userspace(const struct nlattr *attr)
  1553. {
  1554. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1555. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1556. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1557. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1558. };
  1559. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1560. int error;
  1561. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1562. attr, userspace_policy);
  1563. if (error)
  1564. return error;
  1565. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1566. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1567. return -EINVAL;
  1568. return 0;
  1569. }
  1570. static int copy_action(const struct nlattr *from,
  1571. struct sw_flow_actions **sfa, bool log)
  1572. {
  1573. int totlen = NLA_ALIGN(from->nla_len);
  1574. struct nlattr *to;
  1575. to = reserve_sfa_size(sfa, from->nla_len, log);
  1576. if (IS_ERR(to))
  1577. return PTR_ERR(to);
  1578. memcpy(to, from, totlen);
  1579. return 0;
  1580. }
  1581. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1582. const struct sw_flow_key *key,
  1583. int depth, struct sw_flow_actions **sfa,
  1584. __be16 eth_type, __be16 vlan_tci, bool log)
  1585. {
  1586. const struct nlattr *a;
  1587. int rem, err;
  1588. if (depth >= SAMPLE_ACTION_DEPTH)
  1589. return -EOVERFLOW;
  1590. nla_for_each_nested(a, attr, rem) {
  1591. /* Expected argument lengths, (u32)-1 for variable length. */
  1592. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1593. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1594. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1595. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1596. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1597. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1598. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1599. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1600. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1601. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1602. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1603. };
  1604. const struct ovs_action_push_vlan *vlan;
  1605. int type = nla_type(a);
  1606. bool skip_copy;
  1607. if (type > OVS_ACTION_ATTR_MAX ||
  1608. (action_lens[type] != nla_len(a) &&
  1609. action_lens[type] != (u32)-1))
  1610. return -EINVAL;
  1611. skip_copy = false;
  1612. switch (type) {
  1613. case OVS_ACTION_ATTR_UNSPEC:
  1614. return -EINVAL;
  1615. case OVS_ACTION_ATTR_USERSPACE:
  1616. err = validate_userspace(a);
  1617. if (err)
  1618. return err;
  1619. break;
  1620. case OVS_ACTION_ATTR_OUTPUT:
  1621. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1622. return -EINVAL;
  1623. break;
  1624. case OVS_ACTION_ATTR_HASH: {
  1625. const struct ovs_action_hash *act_hash = nla_data(a);
  1626. switch (act_hash->hash_alg) {
  1627. case OVS_HASH_ALG_L4:
  1628. break;
  1629. default:
  1630. return -EINVAL;
  1631. }
  1632. break;
  1633. }
  1634. case OVS_ACTION_ATTR_POP_VLAN:
  1635. vlan_tci = htons(0);
  1636. break;
  1637. case OVS_ACTION_ATTR_PUSH_VLAN:
  1638. vlan = nla_data(a);
  1639. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1640. return -EINVAL;
  1641. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1642. return -EINVAL;
  1643. vlan_tci = vlan->vlan_tci;
  1644. break;
  1645. case OVS_ACTION_ATTR_RECIRC:
  1646. break;
  1647. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1648. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1649. if (!eth_p_mpls(mpls->mpls_ethertype))
  1650. return -EINVAL;
  1651. /* Prohibit push MPLS other than to a white list
  1652. * for packets that have a known tag order.
  1653. */
  1654. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1655. (eth_type != htons(ETH_P_IP) &&
  1656. eth_type != htons(ETH_P_IPV6) &&
  1657. eth_type != htons(ETH_P_ARP) &&
  1658. eth_type != htons(ETH_P_RARP) &&
  1659. !eth_p_mpls(eth_type)))
  1660. return -EINVAL;
  1661. eth_type = mpls->mpls_ethertype;
  1662. break;
  1663. }
  1664. case OVS_ACTION_ATTR_POP_MPLS:
  1665. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1666. !eth_p_mpls(eth_type))
  1667. return -EINVAL;
  1668. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1669. * as there is no check here to ensure that the new
  1670. * eth_type is valid and thus set actions could
  1671. * write off the end of the packet or otherwise
  1672. * corrupt it.
  1673. *
  1674. * Support for these actions is planned using packet
  1675. * recirculation.
  1676. */
  1677. eth_type = htons(0);
  1678. break;
  1679. case OVS_ACTION_ATTR_SET:
  1680. err = validate_set(a, key, sfa,
  1681. &skip_copy, eth_type, log);
  1682. if (err)
  1683. return err;
  1684. break;
  1685. case OVS_ACTION_ATTR_SAMPLE:
  1686. err = validate_and_copy_sample(a, key, depth, sfa,
  1687. eth_type, vlan_tci, log);
  1688. if (err)
  1689. return err;
  1690. skip_copy = true;
  1691. break;
  1692. default:
  1693. OVS_NLERR(log, "Unknown Action type %d", type);
  1694. return -EINVAL;
  1695. }
  1696. if (!skip_copy) {
  1697. err = copy_action(a, sfa, log);
  1698. if (err)
  1699. return err;
  1700. }
  1701. }
  1702. if (rem > 0)
  1703. return -EINVAL;
  1704. return 0;
  1705. }
  1706. int ovs_nla_copy_actions(const struct nlattr *attr,
  1707. const struct sw_flow_key *key,
  1708. struct sw_flow_actions **sfa, bool log)
  1709. {
  1710. int err;
  1711. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1712. if (IS_ERR(*sfa))
  1713. return PTR_ERR(*sfa);
  1714. err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
  1715. key->eth.tci, log);
  1716. if (err)
  1717. kfree(*sfa);
  1718. return err;
  1719. }
  1720. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1721. {
  1722. const struct nlattr *a;
  1723. struct nlattr *start;
  1724. int err = 0, rem;
  1725. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1726. if (!start)
  1727. return -EMSGSIZE;
  1728. nla_for_each_nested(a, attr, rem) {
  1729. int type = nla_type(a);
  1730. struct nlattr *st_sample;
  1731. switch (type) {
  1732. case OVS_SAMPLE_ATTR_PROBABILITY:
  1733. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1734. sizeof(u32), nla_data(a)))
  1735. return -EMSGSIZE;
  1736. break;
  1737. case OVS_SAMPLE_ATTR_ACTIONS:
  1738. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1739. if (!st_sample)
  1740. return -EMSGSIZE;
  1741. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1742. if (err)
  1743. return err;
  1744. nla_nest_end(skb, st_sample);
  1745. break;
  1746. }
  1747. }
  1748. nla_nest_end(skb, start);
  1749. return err;
  1750. }
  1751. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1752. {
  1753. const struct nlattr *ovs_key = nla_data(a);
  1754. int key_type = nla_type(ovs_key);
  1755. struct nlattr *start;
  1756. int err;
  1757. switch (key_type) {
  1758. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1759. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1760. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1761. if (!start)
  1762. return -EMSGSIZE;
  1763. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1764. tun_info->options_len ?
  1765. tun_info->options : NULL,
  1766. tun_info->options_len);
  1767. if (err)
  1768. return err;
  1769. nla_nest_end(skb, start);
  1770. break;
  1771. }
  1772. default:
  1773. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1774. return -EMSGSIZE;
  1775. break;
  1776. }
  1777. return 0;
  1778. }
  1779. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1780. {
  1781. const struct nlattr *a;
  1782. int rem, err;
  1783. nla_for_each_attr(a, attr, len, rem) {
  1784. int type = nla_type(a);
  1785. switch (type) {
  1786. case OVS_ACTION_ATTR_SET:
  1787. err = set_action_to_attr(a, skb);
  1788. if (err)
  1789. return err;
  1790. break;
  1791. case OVS_ACTION_ATTR_SAMPLE:
  1792. err = sample_action_to_attr(a, skb);
  1793. if (err)
  1794. return err;
  1795. break;
  1796. default:
  1797. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1798. return -EMSGSIZE;
  1799. break;
  1800. }
  1801. }
  1802. return 0;
  1803. }