flow_netlink.c 75 KB

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