flow_netlink.c 66 KB

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