flow_netlink.c 66 KB

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