flow_netlink.c 73 KB

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