flow_netlink.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  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_in_addr(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_in_addr(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_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
  568. output->ipv4_src))
  569. return -EMSGSIZE;
  570. if (output->ipv4_dst &&
  571. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
  572. output->ipv4_dst))
  573. return -EMSGSIZE;
  574. if (output->ipv4_tos &&
  575. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  576. return -EMSGSIZE;
  577. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  578. return -EMSGSIZE;
  579. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  580. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  581. return -EMSGSIZE;
  582. if ((output->tun_flags & TUNNEL_CSUM) &&
  583. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  584. return -EMSGSIZE;
  585. if (output->tp_src &&
  586. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
  587. return -EMSGSIZE;
  588. if (output->tp_dst &&
  589. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
  590. return -EMSGSIZE;
  591. if ((output->tun_flags & TUNNEL_OAM) &&
  592. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  593. return -EMSGSIZE;
  594. if (tun_opts) {
  595. if (output->tun_flags & TUNNEL_GENEVE_OPT &&
  596. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  597. swkey_tun_opts_len, tun_opts))
  598. return -EMSGSIZE;
  599. else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
  600. vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
  601. return -EMSGSIZE;
  602. }
  603. return 0;
  604. }
  605. static int ipv4_tun_to_nlattr(struct sk_buff *skb,
  606. const struct ovs_key_ipv4_tunnel *output,
  607. const void *tun_opts, int swkey_tun_opts_len)
  608. {
  609. struct nlattr *nla;
  610. int err;
  611. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  612. if (!nla)
  613. return -EMSGSIZE;
  614. err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
  615. if (err)
  616. return err;
  617. nla_nest_end(skb, nla);
  618. return 0;
  619. }
  620. int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
  621. const struct ovs_tunnel_info *egress_tun_info)
  622. {
  623. return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
  624. egress_tun_info->options,
  625. egress_tun_info->options_len);
  626. }
  627. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  628. const struct nlattr **a, bool is_mask,
  629. bool log)
  630. {
  631. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  632. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  633. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  634. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  635. }
  636. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  637. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  638. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  639. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  640. }
  641. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  642. SW_FLOW_KEY_PUT(match, phy.priority,
  643. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  644. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  645. }
  646. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  647. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  648. if (is_mask) {
  649. in_port = 0xffffffff; /* Always exact match in_port. */
  650. } else if (in_port >= DP_MAX_PORTS) {
  651. OVS_NLERR(log, "Port %d exceeds max allowable %d",
  652. in_port, DP_MAX_PORTS);
  653. return -EINVAL;
  654. }
  655. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  656. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  657. } else if (!is_mask) {
  658. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  659. }
  660. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  661. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  662. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  663. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  664. }
  665. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  666. if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  667. is_mask, log) < 0)
  668. return -EINVAL;
  669. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  670. }
  671. return 0;
  672. }
  673. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  674. const struct nlattr **a, bool is_mask,
  675. bool log)
  676. {
  677. int err;
  678. err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
  679. if (err)
  680. return err;
  681. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  682. const struct ovs_key_ethernet *eth_key;
  683. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  684. SW_FLOW_KEY_MEMCPY(match, eth.src,
  685. eth_key->eth_src, ETH_ALEN, is_mask);
  686. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  687. eth_key->eth_dst, ETH_ALEN, is_mask);
  688. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  689. }
  690. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  691. __be16 tci;
  692. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  693. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  694. if (is_mask)
  695. OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
  696. else
  697. OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
  698. return -EINVAL;
  699. }
  700. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  701. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  702. }
  703. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  704. __be16 eth_type;
  705. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  706. if (is_mask) {
  707. /* Always exact match EtherType. */
  708. eth_type = htons(0xffff);
  709. } else if (!eth_proto_is_802_3(eth_type)) {
  710. OVS_NLERR(log, "EtherType %x is less than min %x",
  711. ntohs(eth_type), ETH_P_802_3_MIN);
  712. return -EINVAL;
  713. }
  714. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  715. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  716. } else if (!is_mask) {
  717. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  718. }
  719. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  720. const struct ovs_key_ipv4 *ipv4_key;
  721. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  722. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  723. OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
  724. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  725. return -EINVAL;
  726. }
  727. SW_FLOW_KEY_PUT(match, ip.proto,
  728. ipv4_key->ipv4_proto, is_mask);
  729. SW_FLOW_KEY_PUT(match, ip.tos,
  730. ipv4_key->ipv4_tos, is_mask);
  731. SW_FLOW_KEY_PUT(match, ip.ttl,
  732. ipv4_key->ipv4_ttl, is_mask);
  733. SW_FLOW_KEY_PUT(match, ip.frag,
  734. ipv4_key->ipv4_frag, is_mask);
  735. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  736. ipv4_key->ipv4_src, is_mask);
  737. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  738. ipv4_key->ipv4_dst, is_mask);
  739. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  740. }
  741. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  742. const struct ovs_key_ipv6 *ipv6_key;
  743. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  744. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  745. OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
  746. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  747. return -EINVAL;
  748. }
  749. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  750. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
  751. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  752. return -EINVAL;
  753. }
  754. SW_FLOW_KEY_PUT(match, ipv6.label,
  755. ipv6_key->ipv6_label, is_mask);
  756. SW_FLOW_KEY_PUT(match, ip.proto,
  757. ipv6_key->ipv6_proto, is_mask);
  758. SW_FLOW_KEY_PUT(match, ip.tos,
  759. ipv6_key->ipv6_tclass, is_mask);
  760. SW_FLOW_KEY_PUT(match, ip.ttl,
  761. ipv6_key->ipv6_hlimit, is_mask);
  762. SW_FLOW_KEY_PUT(match, ip.frag,
  763. ipv6_key->ipv6_frag, is_mask);
  764. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  765. ipv6_key->ipv6_src,
  766. sizeof(match->key->ipv6.addr.src),
  767. is_mask);
  768. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  769. ipv6_key->ipv6_dst,
  770. sizeof(match->key->ipv6.addr.dst),
  771. is_mask);
  772. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  773. }
  774. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  775. const struct ovs_key_arp *arp_key;
  776. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  777. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  778. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  779. arp_key->arp_op);
  780. return -EINVAL;
  781. }
  782. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  783. arp_key->arp_sip, is_mask);
  784. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  785. arp_key->arp_tip, is_mask);
  786. SW_FLOW_KEY_PUT(match, ip.proto,
  787. ntohs(arp_key->arp_op), is_mask);
  788. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  789. arp_key->arp_sha, ETH_ALEN, is_mask);
  790. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  791. arp_key->arp_tha, ETH_ALEN, is_mask);
  792. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  793. }
  794. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  795. const struct ovs_key_mpls *mpls_key;
  796. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  797. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  798. mpls_key->mpls_lse, is_mask);
  799. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  800. }
  801. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  802. const struct ovs_key_tcp *tcp_key;
  803. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  804. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  805. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  806. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  807. }
  808. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  809. SW_FLOW_KEY_PUT(match, tp.flags,
  810. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  811. is_mask);
  812. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  813. }
  814. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  815. const struct ovs_key_udp *udp_key;
  816. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  817. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  818. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  819. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  820. }
  821. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  822. const struct ovs_key_sctp *sctp_key;
  823. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  824. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  825. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  826. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  827. }
  828. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  829. const struct ovs_key_icmp *icmp_key;
  830. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  831. SW_FLOW_KEY_PUT(match, tp.src,
  832. htons(icmp_key->icmp_type), is_mask);
  833. SW_FLOW_KEY_PUT(match, tp.dst,
  834. htons(icmp_key->icmp_code), is_mask);
  835. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  836. }
  837. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  838. const struct ovs_key_icmpv6 *icmpv6_key;
  839. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  840. SW_FLOW_KEY_PUT(match, tp.src,
  841. htons(icmpv6_key->icmpv6_type), is_mask);
  842. SW_FLOW_KEY_PUT(match, tp.dst,
  843. htons(icmpv6_key->icmpv6_code), is_mask);
  844. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  845. }
  846. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  847. const struct ovs_key_nd *nd_key;
  848. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  849. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  850. nd_key->nd_target,
  851. sizeof(match->key->ipv6.nd.target),
  852. is_mask);
  853. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  854. nd_key->nd_sll, ETH_ALEN, is_mask);
  855. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  856. nd_key->nd_tll, ETH_ALEN, is_mask);
  857. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  858. }
  859. if (attrs != 0) {
  860. OVS_NLERR(log, "Unknown key attributes %llx",
  861. (unsigned long long)attrs);
  862. return -EINVAL;
  863. }
  864. return 0;
  865. }
  866. static void nlattr_set(struct nlattr *attr, u8 val,
  867. const struct ovs_len_tbl *tbl)
  868. {
  869. struct nlattr *nla;
  870. int rem;
  871. /* The nlattr stream should already have been validated */
  872. nla_for_each_nested(nla, attr, rem) {
  873. if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
  874. nlattr_set(nla, val, tbl[nla_type(nla)].next);
  875. else
  876. memset(nla_data(nla), val, nla_len(nla));
  877. }
  878. }
  879. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  880. {
  881. nlattr_set(attr, val, ovs_key_lens);
  882. }
  883. /**
  884. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  885. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  886. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  887. * does not include any don't care bit.
  888. * @match: receives the extracted flow match information.
  889. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  890. * sequence. The fields should of the packet that triggered the creation
  891. * of this flow.
  892. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  893. * attribute specifies the mask field of the wildcarded flow.
  894. * @log: Boolean to allow kernel error logging. Normally true, but when
  895. * probing for feature compatibility this should be passed in as false to
  896. * suppress unnecessary error logging.
  897. */
  898. int ovs_nla_get_match(struct sw_flow_match *match,
  899. const struct nlattr *nla_key,
  900. const struct nlattr *nla_mask,
  901. bool log)
  902. {
  903. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  904. const struct nlattr *encap;
  905. struct nlattr *newmask = NULL;
  906. u64 key_attrs = 0;
  907. u64 mask_attrs = 0;
  908. bool encap_valid = false;
  909. int err;
  910. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  911. if (err)
  912. return err;
  913. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  914. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  915. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  916. __be16 tci;
  917. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  918. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  919. OVS_NLERR(log, "Invalid Vlan frame.");
  920. return -EINVAL;
  921. }
  922. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  923. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  924. encap = a[OVS_KEY_ATTR_ENCAP];
  925. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  926. encap_valid = true;
  927. if (tci & htons(VLAN_TAG_PRESENT)) {
  928. err = parse_flow_nlattrs(encap, a, &key_attrs, log);
  929. if (err)
  930. return err;
  931. } else if (!tci) {
  932. /* Corner case for truncated 802.1Q header. */
  933. if (nla_len(encap)) {
  934. OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
  935. return -EINVAL;
  936. }
  937. } else {
  938. OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
  939. return -EINVAL;
  940. }
  941. }
  942. err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
  943. if (err)
  944. return err;
  945. if (match->mask) {
  946. if (!nla_mask) {
  947. /* Create an exact match mask. We need to set to 0xff
  948. * all the 'match->mask' fields that have been touched
  949. * in 'match->key'. We cannot simply memset
  950. * 'match->mask', because padding bytes and fields not
  951. * specified in 'match->key' should be left to 0.
  952. * Instead, we use a stream of netlink attributes,
  953. * copied from 'key' and set to 0xff.
  954. * ovs_key_from_nlattrs() will take care of filling
  955. * 'match->mask' appropriately.
  956. */
  957. newmask = kmemdup(nla_key,
  958. nla_total_size(nla_len(nla_key)),
  959. GFP_KERNEL);
  960. if (!newmask)
  961. return -ENOMEM;
  962. mask_set_nlattr(newmask, 0xff);
  963. /* The userspace does not send tunnel attributes that
  964. * are 0, but we should not wildcard them nonetheless.
  965. */
  966. if (match->key->tun_key.ipv4_dst)
  967. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  968. 0xff, true);
  969. nla_mask = newmask;
  970. }
  971. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  972. if (err)
  973. goto free_newmask;
  974. /* Always match on tci. */
  975. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  976. if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
  977. __be16 eth_type = 0;
  978. __be16 tci = 0;
  979. if (!encap_valid) {
  980. OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
  981. err = -EINVAL;
  982. goto free_newmask;
  983. }
  984. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  985. if (a[OVS_KEY_ATTR_ETHERTYPE])
  986. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  987. if (eth_type == htons(0xffff)) {
  988. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  989. encap = a[OVS_KEY_ATTR_ENCAP];
  990. err = parse_flow_mask_nlattrs(encap, a,
  991. &mask_attrs, log);
  992. if (err)
  993. goto free_newmask;
  994. } else {
  995. OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
  996. ntohs(eth_type));
  997. err = -EINVAL;
  998. goto free_newmask;
  999. }
  1000. if (a[OVS_KEY_ATTR_VLAN])
  1001. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1002. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1003. OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
  1004. ntohs(tci));
  1005. err = -EINVAL;
  1006. goto free_newmask;
  1007. }
  1008. }
  1009. err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
  1010. if (err)
  1011. goto free_newmask;
  1012. }
  1013. if (!match_validate(match, key_attrs, mask_attrs, log))
  1014. err = -EINVAL;
  1015. free_newmask:
  1016. kfree(newmask);
  1017. return err;
  1018. }
  1019. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1020. {
  1021. size_t len;
  1022. if (!attr)
  1023. return 0;
  1024. len = nla_len(attr);
  1025. if (len < 1 || len > MAX_UFID_LENGTH) {
  1026. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1027. nla_len(attr), MAX_UFID_LENGTH);
  1028. return 0;
  1029. }
  1030. return len;
  1031. }
  1032. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1033. * or false otherwise.
  1034. */
  1035. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1036. bool log)
  1037. {
  1038. sfid->ufid_len = get_ufid_len(attr, log);
  1039. if (sfid->ufid_len)
  1040. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1041. return sfid->ufid_len;
  1042. }
  1043. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1044. const struct sw_flow_key *key, bool log)
  1045. {
  1046. struct sw_flow_key *new_key;
  1047. if (ovs_nla_get_ufid(sfid, ufid, log))
  1048. return 0;
  1049. /* If UFID was not provided, use unmasked key. */
  1050. new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
  1051. if (!new_key)
  1052. return -ENOMEM;
  1053. memcpy(new_key, key, sizeof(*key));
  1054. sfid->unmasked_key = new_key;
  1055. return 0;
  1056. }
  1057. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1058. {
  1059. return attr ? nla_get_u32(attr) : 0;
  1060. }
  1061. /**
  1062. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1063. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  1064. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1065. * sequence.
  1066. * @log: Boolean to allow kernel error logging. Normally true, but when
  1067. * probing for feature compatibility this should be passed in as false to
  1068. * suppress unnecessary error logging.
  1069. *
  1070. * This parses a series of Netlink attributes that form a flow key, which must
  1071. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1072. * get the metadata, that is, the parts of the flow key that cannot be
  1073. * extracted from the packet itself.
  1074. */
  1075. int ovs_nla_get_flow_metadata(const struct nlattr *attr,
  1076. struct sw_flow_key *key,
  1077. bool log)
  1078. {
  1079. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1080. struct sw_flow_match match;
  1081. u64 attrs = 0;
  1082. int err;
  1083. err = parse_flow_nlattrs(attr, a, &attrs, log);
  1084. if (err)
  1085. return -EINVAL;
  1086. memset(&match, 0, sizeof(match));
  1087. match.key = key;
  1088. key->phy.in_port = DP_MAX_PORTS;
  1089. return metadata_from_nlattrs(&match, &attrs, a, false, log);
  1090. }
  1091. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1092. const struct sw_flow_key *output, bool is_mask,
  1093. struct sk_buff *skb)
  1094. {
  1095. struct ovs_key_ethernet *eth_key;
  1096. struct nlattr *nla, *encap;
  1097. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1098. goto nla_put_failure;
  1099. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1100. goto nla_put_failure;
  1101. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1102. goto nla_put_failure;
  1103. if ((swkey->tun_key.ipv4_dst || is_mask)) {
  1104. const void *opts = NULL;
  1105. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1106. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1107. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  1108. swkey->tun_opts_len))
  1109. goto nla_put_failure;
  1110. }
  1111. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1112. if (is_mask && (output->phy.in_port == 0xffff))
  1113. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1114. goto nla_put_failure;
  1115. } else {
  1116. u16 upper_u16;
  1117. upper_u16 = !is_mask ? 0 : 0xffff;
  1118. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1119. (upper_u16 << 16) | output->phy.in_port))
  1120. goto nla_put_failure;
  1121. }
  1122. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1123. goto nla_put_failure;
  1124. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1125. if (!nla)
  1126. goto nla_put_failure;
  1127. eth_key = nla_data(nla);
  1128. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1129. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1130. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1131. __be16 eth_type;
  1132. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1133. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1134. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1135. goto nla_put_failure;
  1136. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1137. if (!swkey->eth.tci)
  1138. goto unencap;
  1139. } else
  1140. encap = NULL;
  1141. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1142. /*
  1143. * Ethertype 802.2 is represented in the netlink with omitted
  1144. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1145. * 0xffff in the mask attribute. Ethertype can also
  1146. * be wildcarded.
  1147. */
  1148. if (is_mask && output->eth.type)
  1149. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1150. output->eth.type))
  1151. goto nla_put_failure;
  1152. goto unencap;
  1153. }
  1154. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1155. goto nla_put_failure;
  1156. if (swkey->eth.type == htons(ETH_P_IP)) {
  1157. struct ovs_key_ipv4 *ipv4_key;
  1158. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1159. if (!nla)
  1160. goto nla_put_failure;
  1161. ipv4_key = nla_data(nla);
  1162. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1163. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1164. ipv4_key->ipv4_proto = output->ip.proto;
  1165. ipv4_key->ipv4_tos = output->ip.tos;
  1166. ipv4_key->ipv4_ttl = output->ip.ttl;
  1167. ipv4_key->ipv4_frag = output->ip.frag;
  1168. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1169. struct ovs_key_ipv6 *ipv6_key;
  1170. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1171. if (!nla)
  1172. goto nla_put_failure;
  1173. ipv6_key = nla_data(nla);
  1174. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1175. sizeof(ipv6_key->ipv6_src));
  1176. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1177. sizeof(ipv6_key->ipv6_dst));
  1178. ipv6_key->ipv6_label = output->ipv6.label;
  1179. ipv6_key->ipv6_proto = output->ip.proto;
  1180. ipv6_key->ipv6_tclass = output->ip.tos;
  1181. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1182. ipv6_key->ipv6_frag = output->ip.frag;
  1183. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1184. swkey->eth.type == htons(ETH_P_RARP)) {
  1185. struct ovs_key_arp *arp_key;
  1186. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1187. if (!nla)
  1188. goto nla_put_failure;
  1189. arp_key = nla_data(nla);
  1190. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1191. arp_key->arp_sip = output->ipv4.addr.src;
  1192. arp_key->arp_tip = output->ipv4.addr.dst;
  1193. arp_key->arp_op = htons(output->ip.proto);
  1194. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1195. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1196. } else if (eth_p_mpls(swkey->eth.type)) {
  1197. struct ovs_key_mpls *mpls_key;
  1198. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1199. if (!nla)
  1200. goto nla_put_failure;
  1201. mpls_key = nla_data(nla);
  1202. mpls_key->mpls_lse = output->mpls.top_lse;
  1203. }
  1204. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1205. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1206. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1207. if (swkey->ip.proto == IPPROTO_TCP) {
  1208. struct ovs_key_tcp *tcp_key;
  1209. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1210. if (!nla)
  1211. goto nla_put_failure;
  1212. tcp_key = nla_data(nla);
  1213. tcp_key->tcp_src = output->tp.src;
  1214. tcp_key->tcp_dst = output->tp.dst;
  1215. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1216. output->tp.flags))
  1217. goto nla_put_failure;
  1218. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1219. struct ovs_key_udp *udp_key;
  1220. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1221. if (!nla)
  1222. goto nla_put_failure;
  1223. udp_key = nla_data(nla);
  1224. udp_key->udp_src = output->tp.src;
  1225. udp_key->udp_dst = output->tp.dst;
  1226. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1227. struct ovs_key_sctp *sctp_key;
  1228. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1229. if (!nla)
  1230. goto nla_put_failure;
  1231. sctp_key = nla_data(nla);
  1232. sctp_key->sctp_src = output->tp.src;
  1233. sctp_key->sctp_dst = output->tp.dst;
  1234. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1235. swkey->ip.proto == IPPROTO_ICMP) {
  1236. struct ovs_key_icmp *icmp_key;
  1237. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1238. if (!nla)
  1239. goto nla_put_failure;
  1240. icmp_key = nla_data(nla);
  1241. icmp_key->icmp_type = ntohs(output->tp.src);
  1242. icmp_key->icmp_code = ntohs(output->tp.dst);
  1243. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1244. swkey->ip.proto == IPPROTO_ICMPV6) {
  1245. struct ovs_key_icmpv6 *icmpv6_key;
  1246. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1247. sizeof(*icmpv6_key));
  1248. if (!nla)
  1249. goto nla_put_failure;
  1250. icmpv6_key = nla_data(nla);
  1251. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1252. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1253. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1254. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1255. struct ovs_key_nd *nd_key;
  1256. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1257. if (!nla)
  1258. goto nla_put_failure;
  1259. nd_key = nla_data(nla);
  1260. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1261. sizeof(nd_key->nd_target));
  1262. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1263. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1264. }
  1265. }
  1266. }
  1267. unencap:
  1268. if (encap)
  1269. nla_nest_end(skb, encap);
  1270. return 0;
  1271. nla_put_failure:
  1272. return -EMSGSIZE;
  1273. }
  1274. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1275. const struct sw_flow_key *output, int attr, bool is_mask,
  1276. struct sk_buff *skb)
  1277. {
  1278. int err;
  1279. struct nlattr *nla;
  1280. nla = nla_nest_start(skb, attr);
  1281. if (!nla)
  1282. return -EMSGSIZE;
  1283. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1284. if (err)
  1285. return err;
  1286. nla_nest_end(skb, nla);
  1287. return 0;
  1288. }
  1289. /* Called with ovs_mutex or RCU read lock. */
  1290. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1291. {
  1292. if (ovs_identifier_is_ufid(&flow->id))
  1293. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1294. flow->id.ufid);
  1295. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1296. OVS_FLOW_ATTR_KEY, false, skb);
  1297. }
  1298. /* Called with ovs_mutex or RCU read lock. */
  1299. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1300. {
  1301. return ovs_nla_put_key(&flow->key, &flow->key,
  1302. OVS_FLOW_ATTR_KEY, false, skb);
  1303. }
  1304. /* Called with ovs_mutex or RCU read lock. */
  1305. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1306. {
  1307. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1308. OVS_FLOW_ATTR_MASK, true, skb);
  1309. }
  1310. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1311. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1312. {
  1313. struct sw_flow_actions *sfa;
  1314. if (size > MAX_ACTIONS_BUFSIZE) {
  1315. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1316. return ERR_PTR(-EINVAL);
  1317. }
  1318. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1319. if (!sfa)
  1320. return ERR_PTR(-ENOMEM);
  1321. sfa->actions_len = 0;
  1322. return sfa;
  1323. }
  1324. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1325. * The caller must hold rcu_read_lock for this to be sensible. */
  1326. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1327. {
  1328. kfree_rcu(sf_acts, rcu);
  1329. }
  1330. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1331. int attr_len, bool log)
  1332. {
  1333. struct sw_flow_actions *acts;
  1334. int new_acts_size;
  1335. int req_size = NLA_ALIGN(attr_len);
  1336. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1337. (*sfa)->actions_len;
  1338. if (req_size <= (ksize(*sfa) - next_offset))
  1339. goto out;
  1340. new_acts_size = ksize(*sfa) * 2;
  1341. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1342. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1343. return ERR_PTR(-EMSGSIZE);
  1344. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1345. }
  1346. acts = nla_alloc_flow_actions(new_acts_size, log);
  1347. if (IS_ERR(acts))
  1348. return (void *)acts;
  1349. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1350. acts->actions_len = (*sfa)->actions_len;
  1351. kfree(*sfa);
  1352. *sfa = acts;
  1353. out:
  1354. (*sfa)->actions_len += req_size;
  1355. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1356. }
  1357. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1358. int attrtype, void *data, int len, bool log)
  1359. {
  1360. struct nlattr *a;
  1361. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1362. if (IS_ERR(a))
  1363. return a;
  1364. a->nla_type = attrtype;
  1365. a->nla_len = nla_attr_size(len);
  1366. if (data)
  1367. memcpy(nla_data(a), data, len);
  1368. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1369. return a;
  1370. }
  1371. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1372. void *data, int len, bool log)
  1373. {
  1374. struct nlattr *a;
  1375. a = __add_action(sfa, attrtype, data, len, log);
  1376. return PTR_ERR_OR_ZERO(a);
  1377. }
  1378. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1379. int attrtype, bool log)
  1380. {
  1381. int used = (*sfa)->actions_len;
  1382. int err;
  1383. err = add_action(sfa, attrtype, NULL, 0, log);
  1384. if (err)
  1385. return err;
  1386. return used;
  1387. }
  1388. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1389. int st_offset)
  1390. {
  1391. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1392. st_offset);
  1393. a->nla_len = sfa->actions_len - st_offset;
  1394. }
  1395. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1396. const struct sw_flow_key *key,
  1397. int depth, struct sw_flow_actions **sfa,
  1398. __be16 eth_type, __be16 vlan_tci, bool log);
  1399. static int validate_and_copy_sample(const struct nlattr *attr,
  1400. const struct sw_flow_key *key, int depth,
  1401. struct sw_flow_actions **sfa,
  1402. __be16 eth_type, __be16 vlan_tci, bool log)
  1403. {
  1404. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1405. const struct nlattr *probability, *actions;
  1406. const struct nlattr *a;
  1407. int rem, start, err, st_acts;
  1408. memset(attrs, 0, sizeof(attrs));
  1409. nla_for_each_nested(a, attr, rem) {
  1410. int type = nla_type(a);
  1411. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1412. return -EINVAL;
  1413. attrs[type] = a;
  1414. }
  1415. if (rem)
  1416. return -EINVAL;
  1417. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1418. if (!probability || nla_len(probability) != sizeof(u32))
  1419. return -EINVAL;
  1420. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1421. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1422. return -EINVAL;
  1423. /* validation done, copy sample action. */
  1424. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1425. if (start < 0)
  1426. return start;
  1427. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1428. nla_data(probability), sizeof(u32), log);
  1429. if (err)
  1430. return err;
  1431. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1432. if (st_acts < 0)
  1433. return st_acts;
  1434. err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
  1435. eth_type, vlan_tci, log);
  1436. if (err)
  1437. return err;
  1438. add_nested_action_end(*sfa, st_acts);
  1439. add_nested_action_end(*sfa, start);
  1440. return 0;
  1441. }
  1442. void ovs_match_init(struct sw_flow_match *match,
  1443. struct sw_flow_key *key,
  1444. struct sw_flow_mask *mask)
  1445. {
  1446. memset(match, 0, sizeof(*match));
  1447. match->key = key;
  1448. match->mask = mask;
  1449. memset(key, 0, sizeof(*key));
  1450. if (mask) {
  1451. memset(&mask->key, 0, sizeof(mask->key));
  1452. mask->range.start = mask->range.end = 0;
  1453. }
  1454. }
  1455. static int validate_geneve_opts(struct sw_flow_key *key)
  1456. {
  1457. struct geneve_opt *option;
  1458. int opts_len = key->tun_opts_len;
  1459. bool crit_opt = false;
  1460. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1461. while (opts_len > 0) {
  1462. int len;
  1463. if (opts_len < sizeof(*option))
  1464. return -EINVAL;
  1465. len = sizeof(*option) + option->length * 4;
  1466. if (len > opts_len)
  1467. return -EINVAL;
  1468. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1469. option = (struct geneve_opt *)((u8 *)option + len);
  1470. opts_len -= len;
  1471. };
  1472. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1473. return 0;
  1474. }
  1475. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1476. struct sw_flow_actions **sfa, bool log)
  1477. {
  1478. struct sw_flow_match match;
  1479. struct sw_flow_key key;
  1480. struct ovs_tunnel_info *tun_info;
  1481. struct nlattr *a;
  1482. int err = 0, start, opts_type;
  1483. ovs_match_init(&match, &key, NULL);
  1484. opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1485. if (opts_type < 0)
  1486. return opts_type;
  1487. if (key.tun_opts_len) {
  1488. switch (opts_type) {
  1489. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1490. err = validate_geneve_opts(&key);
  1491. if (err < 0)
  1492. return err;
  1493. break;
  1494. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1495. break;
  1496. }
  1497. };
  1498. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1499. if (start < 0)
  1500. return start;
  1501. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1502. sizeof(*tun_info) + key.tun_opts_len, log);
  1503. if (IS_ERR(a))
  1504. return PTR_ERR(a);
  1505. tun_info = nla_data(a);
  1506. tun_info->tunnel = key.tun_key;
  1507. tun_info->options_len = key.tun_opts_len;
  1508. if (tun_info->options_len) {
  1509. /* We need to store the options in the action itself since
  1510. * everything else will go away after flow setup. We can append
  1511. * it to tun_info and then point there.
  1512. */
  1513. memcpy((tun_info + 1),
  1514. TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
  1515. tun_info->options = (tun_info + 1);
  1516. } else {
  1517. tun_info->options = NULL;
  1518. }
  1519. add_nested_action_end(*sfa, start);
  1520. return err;
  1521. }
  1522. /* Return false if there are any non-masked bits set.
  1523. * Mask follows data immediately, before any netlink padding.
  1524. */
  1525. static bool validate_masked(u8 *data, int len)
  1526. {
  1527. u8 *mask = data + len;
  1528. while (len--)
  1529. if (*data++ & ~*mask++)
  1530. return false;
  1531. return true;
  1532. }
  1533. static int validate_set(const struct nlattr *a,
  1534. const struct sw_flow_key *flow_key,
  1535. struct sw_flow_actions **sfa,
  1536. bool *skip_copy, __be16 eth_type, bool masked, bool log)
  1537. {
  1538. const struct nlattr *ovs_key = nla_data(a);
  1539. int key_type = nla_type(ovs_key);
  1540. size_t key_len;
  1541. /* There can be only one key in a action */
  1542. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1543. return -EINVAL;
  1544. key_len = nla_len(ovs_key);
  1545. if (masked)
  1546. key_len /= 2;
  1547. if (key_type > OVS_KEY_ATTR_MAX ||
  1548. (ovs_key_lens[key_type].len != key_len &&
  1549. ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
  1550. return -EINVAL;
  1551. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  1552. return -EINVAL;
  1553. switch (key_type) {
  1554. const struct ovs_key_ipv4 *ipv4_key;
  1555. const struct ovs_key_ipv6 *ipv6_key;
  1556. int err;
  1557. case OVS_KEY_ATTR_PRIORITY:
  1558. case OVS_KEY_ATTR_SKB_MARK:
  1559. case OVS_KEY_ATTR_ETHERNET:
  1560. break;
  1561. case OVS_KEY_ATTR_TUNNEL:
  1562. if (eth_p_mpls(eth_type))
  1563. return -EINVAL;
  1564. if (masked)
  1565. return -EINVAL; /* Masked tunnel set not supported. */
  1566. *skip_copy = true;
  1567. err = validate_and_copy_set_tun(a, sfa, log);
  1568. if (err)
  1569. return err;
  1570. break;
  1571. case OVS_KEY_ATTR_IPV4:
  1572. if (eth_type != htons(ETH_P_IP))
  1573. return -EINVAL;
  1574. ipv4_key = nla_data(ovs_key);
  1575. if (masked) {
  1576. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  1577. /* Non-writeable fields. */
  1578. if (mask->ipv4_proto || mask->ipv4_frag)
  1579. return -EINVAL;
  1580. } else {
  1581. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1582. return -EINVAL;
  1583. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1584. return -EINVAL;
  1585. }
  1586. break;
  1587. case OVS_KEY_ATTR_IPV6:
  1588. if (eth_type != htons(ETH_P_IPV6))
  1589. return -EINVAL;
  1590. ipv6_key = nla_data(ovs_key);
  1591. if (masked) {
  1592. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  1593. /* Non-writeable fields. */
  1594. if (mask->ipv6_proto || mask->ipv6_frag)
  1595. return -EINVAL;
  1596. /* Invalid bits in the flow label mask? */
  1597. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  1598. return -EINVAL;
  1599. } else {
  1600. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1601. return -EINVAL;
  1602. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1603. return -EINVAL;
  1604. }
  1605. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1606. return -EINVAL;
  1607. break;
  1608. case OVS_KEY_ATTR_TCP:
  1609. if ((eth_type != htons(ETH_P_IP) &&
  1610. eth_type != htons(ETH_P_IPV6)) ||
  1611. flow_key->ip.proto != IPPROTO_TCP)
  1612. return -EINVAL;
  1613. break;
  1614. case OVS_KEY_ATTR_UDP:
  1615. if ((eth_type != htons(ETH_P_IP) &&
  1616. eth_type != htons(ETH_P_IPV6)) ||
  1617. flow_key->ip.proto != IPPROTO_UDP)
  1618. return -EINVAL;
  1619. break;
  1620. case OVS_KEY_ATTR_MPLS:
  1621. if (!eth_p_mpls(eth_type))
  1622. return -EINVAL;
  1623. break;
  1624. case OVS_KEY_ATTR_SCTP:
  1625. if ((eth_type != htons(ETH_P_IP) &&
  1626. eth_type != htons(ETH_P_IPV6)) ||
  1627. flow_key->ip.proto != IPPROTO_SCTP)
  1628. return -EINVAL;
  1629. break;
  1630. default:
  1631. return -EINVAL;
  1632. }
  1633. /* Convert non-masked non-tunnel set actions to masked set actions. */
  1634. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  1635. int start, len = key_len * 2;
  1636. struct nlattr *at;
  1637. *skip_copy = true;
  1638. start = add_nested_action_start(sfa,
  1639. OVS_ACTION_ATTR_SET_TO_MASKED,
  1640. log);
  1641. if (start < 0)
  1642. return start;
  1643. at = __add_action(sfa, key_type, NULL, len, log);
  1644. if (IS_ERR(at))
  1645. return PTR_ERR(at);
  1646. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  1647. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  1648. /* Clear non-writeable bits from otherwise writeable fields. */
  1649. if (key_type == OVS_KEY_ATTR_IPV6) {
  1650. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  1651. mask->ipv6_label &= htonl(0x000FFFFF);
  1652. }
  1653. add_nested_action_end(*sfa, start);
  1654. }
  1655. return 0;
  1656. }
  1657. static int validate_userspace(const struct nlattr *attr)
  1658. {
  1659. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1660. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1661. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1662. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1663. };
  1664. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1665. int error;
  1666. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1667. attr, userspace_policy);
  1668. if (error)
  1669. return error;
  1670. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1671. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1672. return -EINVAL;
  1673. return 0;
  1674. }
  1675. static int copy_action(const struct nlattr *from,
  1676. struct sw_flow_actions **sfa, bool log)
  1677. {
  1678. int totlen = NLA_ALIGN(from->nla_len);
  1679. struct nlattr *to;
  1680. to = reserve_sfa_size(sfa, from->nla_len, log);
  1681. if (IS_ERR(to))
  1682. return PTR_ERR(to);
  1683. memcpy(to, from, totlen);
  1684. return 0;
  1685. }
  1686. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1687. const struct sw_flow_key *key,
  1688. int depth, struct sw_flow_actions **sfa,
  1689. __be16 eth_type, __be16 vlan_tci, bool log)
  1690. {
  1691. const struct nlattr *a;
  1692. int rem, err;
  1693. if (depth >= SAMPLE_ACTION_DEPTH)
  1694. return -EOVERFLOW;
  1695. nla_for_each_nested(a, attr, rem) {
  1696. /* Expected argument lengths, (u32)-1 for variable length. */
  1697. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1698. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1699. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1700. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1701. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1702. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1703. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1704. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1705. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1706. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  1707. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1708. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1709. };
  1710. const struct ovs_action_push_vlan *vlan;
  1711. int type = nla_type(a);
  1712. bool skip_copy;
  1713. if (type > OVS_ACTION_ATTR_MAX ||
  1714. (action_lens[type] != nla_len(a) &&
  1715. action_lens[type] != (u32)-1))
  1716. return -EINVAL;
  1717. skip_copy = false;
  1718. switch (type) {
  1719. case OVS_ACTION_ATTR_UNSPEC:
  1720. return -EINVAL;
  1721. case OVS_ACTION_ATTR_USERSPACE:
  1722. err = validate_userspace(a);
  1723. if (err)
  1724. return err;
  1725. break;
  1726. case OVS_ACTION_ATTR_OUTPUT:
  1727. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1728. return -EINVAL;
  1729. break;
  1730. case OVS_ACTION_ATTR_HASH: {
  1731. const struct ovs_action_hash *act_hash = nla_data(a);
  1732. switch (act_hash->hash_alg) {
  1733. case OVS_HASH_ALG_L4:
  1734. break;
  1735. default:
  1736. return -EINVAL;
  1737. }
  1738. break;
  1739. }
  1740. case OVS_ACTION_ATTR_POP_VLAN:
  1741. vlan_tci = htons(0);
  1742. break;
  1743. case OVS_ACTION_ATTR_PUSH_VLAN:
  1744. vlan = nla_data(a);
  1745. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1746. return -EINVAL;
  1747. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1748. return -EINVAL;
  1749. vlan_tci = vlan->vlan_tci;
  1750. break;
  1751. case OVS_ACTION_ATTR_RECIRC:
  1752. break;
  1753. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1754. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1755. if (!eth_p_mpls(mpls->mpls_ethertype))
  1756. return -EINVAL;
  1757. /* Prohibit push MPLS other than to a white list
  1758. * for packets that have a known tag order.
  1759. */
  1760. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1761. (eth_type != htons(ETH_P_IP) &&
  1762. eth_type != htons(ETH_P_IPV6) &&
  1763. eth_type != htons(ETH_P_ARP) &&
  1764. eth_type != htons(ETH_P_RARP) &&
  1765. !eth_p_mpls(eth_type)))
  1766. return -EINVAL;
  1767. eth_type = mpls->mpls_ethertype;
  1768. break;
  1769. }
  1770. case OVS_ACTION_ATTR_POP_MPLS:
  1771. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1772. !eth_p_mpls(eth_type))
  1773. return -EINVAL;
  1774. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1775. * as there is no check here to ensure that the new
  1776. * eth_type is valid and thus set actions could
  1777. * write off the end of the packet or otherwise
  1778. * corrupt it.
  1779. *
  1780. * Support for these actions is planned using packet
  1781. * recirculation.
  1782. */
  1783. eth_type = htons(0);
  1784. break;
  1785. case OVS_ACTION_ATTR_SET:
  1786. err = validate_set(a, key, sfa,
  1787. &skip_copy, eth_type, false, log);
  1788. if (err)
  1789. return err;
  1790. break;
  1791. case OVS_ACTION_ATTR_SET_MASKED:
  1792. err = validate_set(a, key, sfa,
  1793. &skip_copy, eth_type, true, log);
  1794. if (err)
  1795. return err;
  1796. break;
  1797. case OVS_ACTION_ATTR_SAMPLE:
  1798. err = validate_and_copy_sample(a, key, depth, sfa,
  1799. eth_type, vlan_tci, log);
  1800. if (err)
  1801. return err;
  1802. skip_copy = true;
  1803. break;
  1804. default:
  1805. OVS_NLERR(log, "Unknown Action type %d", type);
  1806. return -EINVAL;
  1807. }
  1808. if (!skip_copy) {
  1809. err = copy_action(a, sfa, log);
  1810. if (err)
  1811. return err;
  1812. }
  1813. }
  1814. if (rem > 0)
  1815. return -EINVAL;
  1816. return 0;
  1817. }
  1818. /* 'key' must be the masked key. */
  1819. int ovs_nla_copy_actions(const struct nlattr *attr,
  1820. const struct sw_flow_key *key,
  1821. struct sw_flow_actions **sfa, bool log)
  1822. {
  1823. int err;
  1824. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1825. if (IS_ERR(*sfa))
  1826. return PTR_ERR(*sfa);
  1827. err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
  1828. key->eth.tci, log);
  1829. if (err)
  1830. kfree(*sfa);
  1831. return err;
  1832. }
  1833. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1834. {
  1835. const struct nlattr *a;
  1836. struct nlattr *start;
  1837. int err = 0, rem;
  1838. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1839. if (!start)
  1840. return -EMSGSIZE;
  1841. nla_for_each_nested(a, attr, rem) {
  1842. int type = nla_type(a);
  1843. struct nlattr *st_sample;
  1844. switch (type) {
  1845. case OVS_SAMPLE_ATTR_PROBABILITY:
  1846. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1847. sizeof(u32), nla_data(a)))
  1848. return -EMSGSIZE;
  1849. break;
  1850. case OVS_SAMPLE_ATTR_ACTIONS:
  1851. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1852. if (!st_sample)
  1853. return -EMSGSIZE;
  1854. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1855. if (err)
  1856. return err;
  1857. nla_nest_end(skb, st_sample);
  1858. break;
  1859. }
  1860. }
  1861. nla_nest_end(skb, start);
  1862. return err;
  1863. }
  1864. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1865. {
  1866. const struct nlattr *ovs_key = nla_data(a);
  1867. int key_type = nla_type(ovs_key);
  1868. struct nlattr *start;
  1869. int err;
  1870. switch (key_type) {
  1871. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1872. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1873. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1874. if (!start)
  1875. return -EMSGSIZE;
  1876. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1877. tun_info->options_len ?
  1878. tun_info->options : NULL,
  1879. tun_info->options_len);
  1880. if (err)
  1881. return err;
  1882. nla_nest_end(skb, start);
  1883. break;
  1884. }
  1885. default:
  1886. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1887. return -EMSGSIZE;
  1888. break;
  1889. }
  1890. return 0;
  1891. }
  1892. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  1893. struct sk_buff *skb)
  1894. {
  1895. const struct nlattr *ovs_key = nla_data(a);
  1896. struct nlattr *nla;
  1897. size_t key_len = nla_len(ovs_key) / 2;
  1898. /* Revert the conversion we did from a non-masked set action to
  1899. * masked set action.
  1900. */
  1901. nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1902. if (!nla)
  1903. return -EMSGSIZE;
  1904. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  1905. return -EMSGSIZE;
  1906. nla_nest_end(skb, nla);
  1907. return 0;
  1908. }
  1909. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1910. {
  1911. const struct nlattr *a;
  1912. int rem, err;
  1913. nla_for_each_attr(a, attr, len, rem) {
  1914. int type = nla_type(a);
  1915. switch (type) {
  1916. case OVS_ACTION_ATTR_SET:
  1917. err = set_action_to_attr(a, skb);
  1918. if (err)
  1919. return err;
  1920. break;
  1921. case OVS_ACTION_ATTR_SET_TO_MASKED:
  1922. err = masked_set_action_to_set_action_attr(a, skb);
  1923. if (err)
  1924. return err;
  1925. break;
  1926. case OVS_ACTION_ATTR_SAMPLE:
  1927. err = sample_action_to_attr(a, skb);
  1928. if (err)
  1929. return err;
  1930. break;
  1931. default:
  1932. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1933. return -EMSGSIZE;
  1934. break;
  1935. }
  1936. }
  1937. return 0;
  1938. }