flow_netlink.c 86 KB

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