flow_netlink.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007-2017 Nicira, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include "flow.h"
  7. #include "datapath.h"
  8. #include <linux/uaccess.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/if_ether.h>
  12. #include <linux/if_vlan.h>
  13. #include <net/llc_pdu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/jhash.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/llc.h>
  18. #include <linux/module.h>
  19. #include <linux/in.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/ip.h>
  23. #include <linux/ipv6.h>
  24. #include <linux/sctp.h>
  25. #include <linux/tcp.h>
  26. #include <linux/udp.h>
  27. #include <linux/icmp.h>
  28. #include <linux/icmpv6.h>
  29. #include <linux/rculist.h>
  30. #include <net/geneve.h>
  31. #include <net/ip.h>
  32. #include <net/ipv6.h>
  33. #include <net/ndisc.h>
  34. #include <net/mpls.h>
  35. #include <net/vxlan.h>
  36. #include <net/tun_proto.h>
  37. #include <net/erspan.h>
  38. #include "drop.h"
  39. #include "flow_netlink.h"
  40. struct ovs_len_tbl {
  41. int len;
  42. const struct ovs_len_tbl *next;
  43. };
  44. #define OVS_ATTR_NESTED -1
  45. #define OVS_ATTR_VARIABLE -2
  46. #define OVS_COPY_ACTIONS_MAX_DEPTH 16
  47. static bool actions_may_change_flow(const struct nlattr *actions)
  48. {
  49. struct nlattr *nla;
  50. int rem;
  51. nla_for_each_nested(nla, actions, rem) {
  52. u16 action = nla_type(nla);
  53. switch (action) {
  54. case OVS_ACTION_ATTR_OUTPUT:
  55. case OVS_ACTION_ATTR_RECIRC:
  56. case OVS_ACTION_ATTR_TRUNC:
  57. case OVS_ACTION_ATTR_USERSPACE:
  58. case OVS_ACTION_ATTR_DROP:
  59. case OVS_ACTION_ATTR_PSAMPLE:
  60. break;
  61. case OVS_ACTION_ATTR_CT:
  62. case OVS_ACTION_ATTR_CT_CLEAR:
  63. case OVS_ACTION_ATTR_HASH:
  64. case OVS_ACTION_ATTR_POP_ETH:
  65. case OVS_ACTION_ATTR_POP_MPLS:
  66. case OVS_ACTION_ATTR_POP_NSH:
  67. case OVS_ACTION_ATTR_POP_VLAN:
  68. case OVS_ACTION_ATTR_PUSH_ETH:
  69. case OVS_ACTION_ATTR_PUSH_MPLS:
  70. case OVS_ACTION_ATTR_PUSH_NSH:
  71. case OVS_ACTION_ATTR_PUSH_VLAN:
  72. case OVS_ACTION_ATTR_SAMPLE:
  73. case OVS_ACTION_ATTR_SET:
  74. case OVS_ACTION_ATTR_SET_MASKED:
  75. case OVS_ACTION_ATTR_METER:
  76. case OVS_ACTION_ATTR_CHECK_PKT_LEN:
  77. case OVS_ACTION_ATTR_ADD_MPLS:
  78. case OVS_ACTION_ATTR_DEC_TTL:
  79. default:
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. static void update_range(struct sw_flow_match *match,
  86. size_t offset, size_t size, bool is_mask)
  87. {
  88. struct sw_flow_key_range *range;
  89. size_t start = rounddown(offset, sizeof(long));
  90. size_t end = roundup(offset + size, sizeof(long));
  91. if (!is_mask)
  92. range = &match->range;
  93. else
  94. range = &match->mask->range;
  95. if (range->start == range->end) {
  96. range->start = start;
  97. range->end = end;
  98. return;
  99. }
  100. if (range->start > start)
  101. range->start = start;
  102. if (range->end < end)
  103. range->end = end;
  104. }
  105. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  106. do { \
  107. update_range(match, offsetof(struct sw_flow_key, field), \
  108. sizeof((match)->key->field), is_mask); \
  109. if (is_mask) \
  110. (match)->mask->key.field = value; \
  111. else \
  112. (match)->key->field = value; \
  113. } while (0)
  114. #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
  115. do { \
  116. update_range(match, offset, len, is_mask); \
  117. if (is_mask) \
  118. memcpy((u8 *)&(match)->mask->key + offset, value_p, \
  119. len); \
  120. else \
  121. memcpy((u8 *)(match)->key + offset, value_p, len); \
  122. } while (0)
  123. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  124. SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
  125. value_p, len, is_mask)
  126. #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
  127. do { \
  128. update_range(match, offsetof(struct sw_flow_key, field), \
  129. sizeof((match)->key->field), is_mask); \
  130. if (is_mask) \
  131. memset((u8 *)&(match)->mask->key.field, value, \
  132. sizeof((match)->mask->key.field)); \
  133. else \
  134. memset((u8 *)&(match)->key->field, value, \
  135. sizeof((match)->key->field)); \
  136. } while (0)
  137. #define SW_FLOW_KEY_BITMAP_COPY(match, field, value_p, nbits, is_mask) ({ \
  138. update_range(match, offsetof(struct sw_flow_key, field), \
  139. bitmap_size(nbits), is_mask); \
  140. bitmap_copy(is_mask ? (match)->mask->key.field : (match)->key->field, \
  141. value_p, nbits); \
  142. })
  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_MAX != 32);
  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. + nla_total_size(2); /* OVS_KEY_ATTR_IPV6_EXTHDRS */
  342. }
  343. static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
  344. [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
  345. };
  346. static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  347. [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
  348. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
  349. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
  350. [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
  351. [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
  352. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
  353. [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
  354. [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
  355. [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
  356. [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
  357. [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
  358. [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
  359. .next = ovs_vxlan_ext_key_lens },
  360. [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  361. [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  362. [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_VARIABLE },
  363. [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE] = { .len = 0 },
  364. };
  365. static const struct ovs_len_tbl
  366. ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
  367. [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
  368. [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
  369. [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
  370. };
  371. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  372. static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  373. [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
  374. [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
  375. [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
  376. [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
  377. [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
  378. [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
  379. [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
  380. [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
  381. [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
  382. [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
  383. [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
  384. [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
  385. [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
  386. [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
  387. [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
  388. [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
  389. [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
  390. [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
  391. [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
  392. [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
  393. .next = ovs_tunnel_key_lens, },
  394. [OVS_KEY_ATTR_MPLS] = { .len = OVS_ATTR_VARIABLE },
  395. [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
  396. [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
  397. [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
  398. [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
  399. [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
  400. .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
  401. [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
  402. .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
  403. [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
  404. .next = ovs_nsh_key_attr_lens, },
  405. [OVS_KEY_ATTR_IPV6_EXTHDRS] = {
  406. .len = sizeof(struct ovs_key_ipv6_exthdrs) },
  407. };
  408. static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
  409. {
  410. return expected_len == attr_len ||
  411. expected_len == OVS_ATTR_NESTED ||
  412. expected_len == OVS_ATTR_VARIABLE;
  413. }
  414. static bool is_all_zero(const u8 *fp, size_t size)
  415. {
  416. int i;
  417. if (!fp)
  418. return false;
  419. for (i = 0; i < size; i++)
  420. if (fp[i])
  421. return false;
  422. return true;
  423. }
  424. static int __parse_flow_nlattrs(const struct nlattr *attr,
  425. const struct nlattr *a[],
  426. u64 *attrsp, bool log, bool nz)
  427. {
  428. const struct nlattr *nla;
  429. u64 attrs;
  430. int rem;
  431. attrs = *attrsp;
  432. nla_for_each_nested(nla, attr, rem) {
  433. u16 type = nla_type(nla);
  434. int expected_len;
  435. if (type > OVS_KEY_ATTR_MAX) {
  436. OVS_NLERR(log, "Key type %d is out of range max %d",
  437. type, OVS_KEY_ATTR_MAX);
  438. return -EINVAL;
  439. }
  440. if (type == OVS_KEY_ATTR_PACKET_TYPE ||
  441. type == OVS_KEY_ATTR_ND_EXTENSIONS ||
  442. type == OVS_KEY_ATTR_TUNNEL_INFO) {
  443. OVS_NLERR(log, "Key type %d is not supported", type);
  444. return -EINVAL;
  445. }
  446. if (attrs & (1ULL << type)) {
  447. OVS_NLERR(log, "Duplicate key (type %d).", type);
  448. return -EINVAL;
  449. }
  450. expected_len = ovs_key_lens[type].len;
  451. if (!check_attr_len(nla_len(nla), expected_len)) {
  452. OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
  453. type, nla_len(nla), expected_len);
  454. return -EINVAL;
  455. }
  456. if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
  457. attrs |= 1ULL << type;
  458. a[type] = nla;
  459. }
  460. }
  461. if (rem) {
  462. OVS_NLERR(log, "Message has %d unknown bytes.", rem);
  463. return -EINVAL;
  464. }
  465. *attrsp = attrs;
  466. return 0;
  467. }
  468. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  469. const struct nlattr *a[], u64 *attrsp,
  470. bool log)
  471. {
  472. return __parse_flow_nlattrs(attr, a, attrsp, log, true);
  473. }
  474. int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
  475. u64 *attrsp, bool log)
  476. {
  477. return __parse_flow_nlattrs(attr, a, attrsp, log, false);
  478. }
  479. static int genev_tun_opt_from_nlattr(const struct nlattr *a,
  480. struct sw_flow_match *match, bool is_mask,
  481. bool log)
  482. {
  483. unsigned long opt_key_offset;
  484. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  485. OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
  486. nla_len(a), sizeof(match->key->tun_opts));
  487. return -EINVAL;
  488. }
  489. if (nla_len(a) % 4 != 0) {
  490. OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
  491. nla_len(a));
  492. return -EINVAL;
  493. }
  494. /* We need to record the length of the options passed
  495. * down, otherwise packets with the same format but
  496. * additional options will be silently matched.
  497. */
  498. if (!is_mask) {
  499. SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
  500. false);
  501. } else {
  502. /* This is somewhat unusual because it looks at
  503. * both the key and mask while parsing the
  504. * attributes (and by extension assumes the key
  505. * is parsed first). Normally, we would verify
  506. * that each is the correct length and that the
  507. * attributes line up in the validate function.
  508. * However, that is difficult because this is
  509. * variable length and we won't have the
  510. * information later.
  511. */
  512. if (match->key->tun_opts_len != nla_len(a)) {
  513. OVS_NLERR(log, "Geneve option len %d != mask len %d",
  514. match->key->tun_opts_len, nla_len(a));
  515. return -EINVAL;
  516. }
  517. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  518. }
  519. opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
  520. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
  521. nla_len(a), is_mask);
  522. return 0;
  523. }
  524. static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
  525. struct sw_flow_match *match, bool is_mask,
  526. bool log)
  527. {
  528. struct nlattr *a;
  529. int rem;
  530. unsigned long opt_key_offset;
  531. struct vxlan_metadata opts;
  532. BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
  533. memset(&opts, 0, sizeof(opts));
  534. nla_for_each_nested(a, attr, rem) {
  535. int type = nla_type(a);
  536. if (type > OVS_VXLAN_EXT_MAX) {
  537. OVS_NLERR(log, "VXLAN extension %d out of range max %d",
  538. type, OVS_VXLAN_EXT_MAX);
  539. return -EINVAL;
  540. }
  541. if (!check_attr_len(nla_len(a),
  542. ovs_vxlan_ext_key_lens[type].len)) {
  543. OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
  544. type, nla_len(a),
  545. ovs_vxlan_ext_key_lens[type].len);
  546. return -EINVAL;
  547. }
  548. switch (type) {
  549. case OVS_VXLAN_EXT_GBP:
  550. opts.gbp = nla_get_u32(a);
  551. break;
  552. default:
  553. OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
  554. type);
  555. return -EINVAL;
  556. }
  557. }
  558. if (rem) {
  559. OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
  560. rem);
  561. return -EINVAL;
  562. }
  563. if (!is_mask)
  564. SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
  565. else
  566. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  567. opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
  568. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
  569. is_mask);
  570. return 0;
  571. }
  572. static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
  573. struct sw_flow_match *match, bool is_mask,
  574. bool log)
  575. {
  576. unsigned long opt_key_offset;
  577. BUILD_BUG_ON(sizeof(struct erspan_metadata) >
  578. sizeof(match->key->tun_opts));
  579. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  580. OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
  581. nla_len(a), sizeof(match->key->tun_opts));
  582. return -EINVAL;
  583. }
  584. if (!is_mask)
  585. SW_FLOW_KEY_PUT(match, tun_opts_len,
  586. sizeof(struct erspan_metadata), false);
  587. else
  588. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  589. opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
  590. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
  591. nla_len(a), is_mask);
  592. return 0;
  593. }
  594. static int ip_tun_from_nlattr(const struct nlattr *attr,
  595. struct sw_flow_match *match, bool is_mask,
  596. bool log)
  597. {
  598. bool ttl = false, ipv4 = false, ipv6 = false;
  599. IP_TUNNEL_DECLARE_FLAGS(tun_flags) = { };
  600. bool info_bridge_mode = false;
  601. int opts_type = 0;
  602. struct nlattr *a;
  603. int rem;
  604. nla_for_each_nested(a, attr, rem) {
  605. int type = nla_type(a);
  606. int err;
  607. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  608. OVS_NLERR(log, "Tunnel attr %d out of range max %d",
  609. type, OVS_TUNNEL_KEY_ATTR_MAX);
  610. return -EINVAL;
  611. }
  612. if (!check_attr_len(nla_len(a),
  613. ovs_tunnel_key_lens[type].len)) {
  614. OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
  615. type, nla_len(a), ovs_tunnel_key_lens[type].len);
  616. return -EINVAL;
  617. }
  618. switch (type) {
  619. case OVS_TUNNEL_KEY_ATTR_ID:
  620. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  621. nla_get_be64(a), is_mask);
  622. __set_bit(IP_TUNNEL_KEY_BIT, tun_flags);
  623. break;
  624. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  625. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
  626. nla_get_in_addr(a), is_mask);
  627. ipv4 = true;
  628. break;
  629. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  630. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
  631. nla_get_in_addr(a), is_mask);
  632. ipv4 = true;
  633. break;
  634. case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
  635. SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
  636. nla_get_in6_addr(a), is_mask);
  637. ipv6 = true;
  638. break;
  639. case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
  640. SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
  641. nla_get_in6_addr(a), is_mask);
  642. ipv6 = true;
  643. break;
  644. case OVS_TUNNEL_KEY_ATTR_TOS:
  645. SW_FLOW_KEY_PUT(match, tun_key.tos,
  646. nla_get_u8(a), is_mask);
  647. break;
  648. case OVS_TUNNEL_KEY_ATTR_TTL:
  649. SW_FLOW_KEY_PUT(match, tun_key.ttl,
  650. nla_get_u8(a), is_mask);
  651. ttl = true;
  652. break;
  653. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  654. __set_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, tun_flags);
  655. break;
  656. case OVS_TUNNEL_KEY_ATTR_CSUM:
  657. __set_bit(IP_TUNNEL_CSUM_BIT, tun_flags);
  658. break;
  659. case OVS_TUNNEL_KEY_ATTR_TP_SRC:
  660. SW_FLOW_KEY_PUT(match, tun_key.tp_src,
  661. nla_get_be16(a), is_mask);
  662. break;
  663. case OVS_TUNNEL_KEY_ATTR_TP_DST:
  664. SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
  665. nla_get_be16(a), is_mask);
  666. break;
  667. case OVS_TUNNEL_KEY_ATTR_OAM:
  668. __set_bit(IP_TUNNEL_OAM_BIT, tun_flags);
  669. break;
  670. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  671. if (opts_type) {
  672. OVS_NLERR(log, "Multiple metadata blocks provided");
  673. return -EINVAL;
  674. }
  675. err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
  676. if (err)
  677. return err;
  678. __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, tun_flags);
  679. opts_type = type;
  680. break;
  681. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  682. if (opts_type) {
  683. OVS_NLERR(log, "Multiple metadata blocks provided");
  684. return -EINVAL;
  685. }
  686. err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
  687. if (err)
  688. return err;
  689. __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, tun_flags);
  690. opts_type = type;
  691. break;
  692. case OVS_TUNNEL_KEY_ATTR_PAD:
  693. break;
  694. case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
  695. if (opts_type) {
  696. OVS_NLERR(log, "Multiple metadata blocks provided");
  697. return -EINVAL;
  698. }
  699. err = erspan_tun_opt_from_nlattr(a, match, is_mask,
  700. log);
  701. if (err)
  702. return err;
  703. __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, tun_flags);
  704. opts_type = type;
  705. break;
  706. case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE:
  707. info_bridge_mode = true;
  708. ipv4 = true;
  709. break;
  710. default:
  711. OVS_NLERR(log, "Unknown IP tunnel attribute %d",
  712. type);
  713. return -EINVAL;
  714. }
  715. }
  716. SW_FLOW_KEY_BITMAP_COPY(match, tun_key.tun_flags, tun_flags,
  717. __IP_TUNNEL_FLAG_NUM, is_mask);
  718. if (is_mask)
  719. SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
  720. else
  721. SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
  722. false);
  723. if (rem > 0) {
  724. OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
  725. rem);
  726. return -EINVAL;
  727. }
  728. if (ipv4 && ipv6) {
  729. OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
  730. return -EINVAL;
  731. }
  732. if (!is_mask) {
  733. if (!ipv4 && !ipv6) {
  734. OVS_NLERR(log, "IP tunnel dst address not specified");
  735. return -EINVAL;
  736. }
  737. if (ipv4) {
  738. if (info_bridge_mode) {
  739. __clear_bit(IP_TUNNEL_KEY_BIT, tun_flags);
  740. if (match->key->tun_key.u.ipv4.src ||
  741. match->key->tun_key.u.ipv4.dst ||
  742. match->key->tun_key.tp_src ||
  743. match->key->tun_key.tp_dst ||
  744. match->key->tun_key.ttl ||
  745. match->key->tun_key.tos ||
  746. !ip_tunnel_flags_empty(tun_flags)) {
  747. OVS_NLERR(log, "IPv4 tun info is not correct");
  748. return -EINVAL;
  749. }
  750. } else if (!match->key->tun_key.u.ipv4.dst) {
  751. OVS_NLERR(log, "IPv4 tunnel dst address is zero");
  752. return -EINVAL;
  753. }
  754. }
  755. if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
  756. OVS_NLERR(log, "IPv6 tunnel dst address is zero");
  757. return -EINVAL;
  758. }
  759. if (!ttl && !info_bridge_mode) {
  760. OVS_NLERR(log, "IP tunnel TTL not specified.");
  761. return -EINVAL;
  762. }
  763. }
  764. return opts_type;
  765. }
  766. static int vxlan_opt_to_nlattr(struct sk_buff *skb,
  767. const void *tun_opts, int swkey_tun_opts_len)
  768. {
  769. const struct vxlan_metadata *opts = tun_opts;
  770. struct nlattr *nla;
  771. nla = nla_nest_start_noflag(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
  772. if (!nla)
  773. return -EMSGSIZE;
  774. if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
  775. return -EMSGSIZE;
  776. nla_nest_end(skb, nla);
  777. return 0;
  778. }
  779. static int __ip_tun_to_nlattr(struct sk_buff *skb,
  780. const struct ip_tunnel_key *output,
  781. const void *tun_opts, int swkey_tun_opts_len,
  782. unsigned short tun_proto, u8 mode)
  783. {
  784. if (test_bit(IP_TUNNEL_KEY_BIT, output->tun_flags) &&
  785. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
  786. OVS_TUNNEL_KEY_ATTR_PAD))
  787. return -EMSGSIZE;
  788. if (mode & IP_TUNNEL_INFO_BRIDGE)
  789. return nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE)
  790. ? -EMSGSIZE : 0;
  791. switch (tun_proto) {
  792. case AF_INET:
  793. if (output->u.ipv4.src &&
  794. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
  795. output->u.ipv4.src))
  796. return -EMSGSIZE;
  797. if (output->u.ipv4.dst &&
  798. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
  799. output->u.ipv4.dst))
  800. return -EMSGSIZE;
  801. break;
  802. case AF_INET6:
  803. if (!ipv6_addr_any(&output->u.ipv6.src) &&
  804. nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
  805. &output->u.ipv6.src))
  806. return -EMSGSIZE;
  807. if (!ipv6_addr_any(&output->u.ipv6.dst) &&
  808. nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
  809. &output->u.ipv6.dst))
  810. return -EMSGSIZE;
  811. break;
  812. }
  813. if (output->tos &&
  814. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
  815. return -EMSGSIZE;
  816. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
  817. return -EMSGSIZE;
  818. if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, output->tun_flags) &&
  819. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  820. return -EMSGSIZE;
  821. if (test_bit(IP_TUNNEL_CSUM_BIT, output->tun_flags) &&
  822. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  823. return -EMSGSIZE;
  824. if (output->tp_src &&
  825. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
  826. return -EMSGSIZE;
  827. if (output->tp_dst &&
  828. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
  829. return -EMSGSIZE;
  830. if (test_bit(IP_TUNNEL_OAM_BIT, output->tun_flags) &&
  831. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  832. return -EMSGSIZE;
  833. if (swkey_tun_opts_len) {
  834. if (test_bit(IP_TUNNEL_GENEVE_OPT_BIT, output->tun_flags) &&
  835. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  836. swkey_tun_opts_len, tun_opts))
  837. return -EMSGSIZE;
  838. else if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT,
  839. output->tun_flags) &&
  840. vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
  841. return -EMSGSIZE;
  842. else if (test_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
  843. output->tun_flags) &&
  844. nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
  845. swkey_tun_opts_len, tun_opts))
  846. return -EMSGSIZE;
  847. }
  848. return 0;
  849. }
  850. static int ip_tun_to_nlattr(struct sk_buff *skb,
  851. const struct ip_tunnel_key *output,
  852. const void *tun_opts, int swkey_tun_opts_len,
  853. unsigned short tun_proto, u8 mode)
  854. {
  855. struct nlattr *nla;
  856. int err;
  857. nla = nla_nest_start_noflag(skb, OVS_KEY_ATTR_TUNNEL);
  858. if (!nla)
  859. return -EMSGSIZE;
  860. err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
  861. tun_proto, mode);
  862. if (err)
  863. return err;
  864. nla_nest_end(skb, nla);
  865. return 0;
  866. }
  867. int ovs_nla_put_tunnel_info(struct sk_buff *skb,
  868. struct ip_tunnel_info *tun_info)
  869. {
  870. return __ip_tun_to_nlattr(skb, &tun_info->key,
  871. ip_tunnel_info_opts(tun_info),
  872. tun_info->options_len,
  873. ip_tunnel_info_af(tun_info), tun_info->mode);
  874. }
  875. static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
  876. const struct nlattr *a[],
  877. bool is_mask, bool inner)
  878. {
  879. __be16 tci = 0;
  880. __be16 tpid = 0;
  881. if (a[OVS_KEY_ATTR_VLAN])
  882. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  883. if (a[OVS_KEY_ATTR_ETHERTYPE])
  884. tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  885. if (likely(!inner)) {
  886. SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
  887. SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
  888. } else {
  889. SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
  890. SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
  891. }
  892. return 0;
  893. }
  894. static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
  895. u64 key_attrs, bool inner,
  896. const struct nlattr **a, bool log)
  897. {
  898. __be16 tci = 0;
  899. if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  900. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  901. eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
  902. /* Not a VLAN. */
  903. return 0;
  904. }
  905. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  906. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  907. OVS_NLERR(log, "Invalid %s frame", (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 (!(tci & htons(VLAN_CFI_MASK))) {
  913. if (tci) {
  914. OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
  915. (inner) ? "C-VLAN" : "VLAN");
  916. return -EINVAL;
  917. } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
  918. /* Corner case for truncated VLAN header. */
  919. OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
  920. (inner) ? "C-VLAN" : "VLAN");
  921. return -EINVAL;
  922. }
  923. }
  924. return 1;
  925. }
  926. static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
  927. u64 key_attrs, bool inner,
  928. const struct nlattr **a, bool log)
  929. {
  930. __be16 tci = 0;
  931. __be16 tpid = 0;
  932. bool encap_valid = !!(match->key->eth.vlan.tci &
  933. htons(VLAN_CFI_MASK));
  934. bool i_encap_valid = !!(match->key->eth.cvlan.tci &
  935. htons(VLAN_CFI_MASK));
  936. if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
  937. /* Not a VLAN. */
  938. return 0;
  939. }
  940. if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
  941. OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
  942. (inner) ? "C-VLAN" : "VLAN");
  943. return -EINVAL;
  944. }
  945. if (a[OVS_KEY_ATTR_VLAN])
  946. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  947. if (a[OVS_KEY_ATTR_ETHERTYPE])
  948. tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  949. if (tpid != htons(0xffff)) {
  950. OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
  951. (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
  952. return -EINVAL;
  953. }
  954. if (!(tci & htons(VLAN_CFI_MASK))) {
  955. OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
  956. (inner) ? "C-VLAN" : "VLAN");
  957. return -EINVAL;
  958. }
  959. return 1;
  960. }
  961. static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
  962. u64 *key_attrs, bool inner,
  963. const struct nlattr **a, bool is_mask,
  964. bool log)
  965. {
  966. int err;
  967. const struct nlattr *encap;
  968. if (!is_mask)
  969. err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
  970. a, log);
  971. else
  972. err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
  973. a, log);
  974. if (err <= 0)
  975. return err;
  976. err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
  977. if (err)
  978. return err;
  979. *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  980. *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  981. *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  982. encap = a[OVS_KEY_ATTR_ENCAP];
  983. if (!is_mask)
  984. err = parse_flow_nlattrs(encap, a, key_attrs, log);
  985. else
  986. err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
  987. return err;
  988. }
  989. static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
  990. u64 *key_attrs, const struct nlattr **a,
  991. bool is_mask, bool log)
  992. {
  993. int err;
  994. bool encap_valid = false;
  995. err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
  996. is_mask, log);
  997. if (err)
  998. return err;
  999. encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
  1000. if (encap_valid) {
  1001. err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
  1002. is_mask, log);
  1003. if (err)
  1004. return err;
  1005. }
  1006. return 0;
  1007. }
  1008. static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
  1009. u64 *attrs, const struct nlattr **a,
  1010. bool is_mask, bool log)
  1011. {
  1012. __be16 eth_type;
  1013. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1014. if (is_mask) {
  1015. /* Always exact match EtherType. */
  1016. eth_type = htons(0xffff);
  1017. } else if (!eth_proto_is_802_3(eth_type)) {
  1018. OVS_NLERR(log, "EtherType %x is less than min %x",
  1019. ntohs(eth_type), ETH_P_802_3_MIN);
  1020. return -EINVAL;
  1021. }
  1022. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  1023. *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1024. return 0;
  1025. }
  1026. static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
  1027. u64 *attrs, const struct nlattr **a,
  1028. bool is_mask, bool log)
  1029. {
  1030. u8 mac_proto = MAC_PROTO_ETHERNET;
  1031. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  1032. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  1033. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  1034. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  1035. }
  1036. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  1037. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  1038. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  1039. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  1040. }
  1041. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  1042. SW_FLOW_KEY_PUT(match, phy.priority,
  1043. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  1044. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  1045. }
  1046. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  1047. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  1048. if (is_mask) {
  1049. in_port = 0xffffffff; /* Always exact match in_port. */
  1050. } else if (in_port >= DP_MAX_PORTS) {
  1051. OVS_NLERR(log, "Port %d exceeds max allowable %d",
  1052. in_port, DP_MAX_PORTS);
  1053. return -EINVAL;
  1054. }
  1055. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  1056. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  1057. } else if (!is_mask) {
  1058. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  1059. }
  1060. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  1061. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  1062. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  1063. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  1064. }
  1065. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  1066. if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  1067. is_mask, log) < 0)
  1068. return -EINVAL;
  1069. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  1070. }
  1071. if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
  1072. ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
  1073. u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
  1074. if (ct_state & ~CT_SUPPORTED_MASK) {
  1075. OVS_NLERR(log, "ct_state flags %08x unsupported",
  1076. ct_state);
  1077. return -EINVAL;
  1078. }
  1079. SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
  1080. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
  1081. }
  1082. if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
  1083. ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
  1084. u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
  1085. SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
  1086. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
  1087. }
  1088. if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
  1089. ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
  1090. u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
  1091. SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
  1092. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
  1093. }
  1094. if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
  1095. ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
  1096. const struct ovs_key_ct_labels *cl;
  1097. cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
  1098. SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
  1099. sizeof(*cl), is_mask);
  1100. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
  1101. }
  1102. if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
  1103. const struct ovs_key_ct_tuple_ipv4 *ct;
  1104. ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
  1105. SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
  1106. SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
  1107. SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
  1108. SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
  1109. SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
  1110. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
  1111. }
  1112. if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
  1113. const struct ovs_key_ct_tuple_ipv6 *ct;
  1114. ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
  1115. SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
  1116. sizeof(match->key->ipv6.ct_orig.src),
  1117. is_mask);
  1118. SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
  1119. sizeof(match->key->ipv6.ct_orig.dst),
  1120. is_mask);
  1121. SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
  1122. SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
  1123. SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
  1124. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
  1125. }
  1126. /* For layer 3 packets the Ethernet type is provided
  1127. * and treated as metadata but no MAC addresses are provided.
  1128. */
  1129. if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
  1130. (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
  1131. mac_proto = MAC_PROTO_NONE;
  1132. /* Always exact match mac_proto */
  1133. SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
  1134. if (mac_proto == MAC_PROTO_NONE)
  1135. return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
  1136. log);
  1137. return 0;
  1138. }
  1139. /*
  1140. * Constructs NSH header 'nh' from attributes of OVS_ACTION_ATTR_PUSH_NSH,
  1141. * where 'nh' points to a memory block of 'size' bytes. It's assumed that
  1142. * attributes were previously validated with validate_push_nsh().
  1143. */
  1144. int nsh_hdr_from_nlattr(const struct nlattr *attr,
  1145. struct nshhdr *nh, size_t size)
  1146. {
  1147. struct nlattr *a;
  1148. int rem;
  1149. u8 flags = 0;
  1150. u8 ttl = 0;
  1151. int mdlen = 0;
  1152. if (size < NSH_BASE_HDR_LEN)
  1153. return -ENOBUFS;
  1154. nla_for_each_nested(a, attr, rem) {
  1155. int type = nla_type(a);
  1156. switch (type) {
  1157. case OVS_NSH_KEY_ATTR_BASE: {
  1158. const struct ovs_nsh_key_base *base = nla_data(a);
  1159. flags = base->flags;
  1160. ttl = base->ttl;
  1161. nh->np = base->np;
  1162. nh->mdtype = base->mdtype;
  1163. nh->path_hdr = base->path_hdr;
  1164. break;
  1165. }
  1166. case OVS_NSH_KEY_ATTR_MD1:
  1167. mdlen = nla_len(a);
  1168. if (mdlen > size - NSH_BASE_HDR_LEN)
  1169. return -ENOBUFS;
  1170. memcpy(&nh->md1, nla_data(a), mdlen);
  1171. break;
  1172. case OVS_NSH_KEY_ATTR_MD2:
  1173. mdlen = nla_len(a);
  1174. if (mdlen > size - NSH_BASE_HDR_LEN)
  1175. return -ENOBUFS;
  1176. memcpy(&nh->md2, nla_data(a), mdlen);
  1177. break;
  1178. default:
  1179. return -EINVAL;
  1180. }
  1181. }
  1182. /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
  1183. nh->ver_flags_ttl_len = 0;
  1184. nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
  1185. return 0;
  1186. }
  1187. static int nsh_key_put_from_nlattr(const struct nlattr *attr,
  1188. struct sw_flow_match *match, bool is_mask,
  1189. bool is_push_nsh, bool log)
  1190. {
  1191. struct nlattr *a;
  1192. int rem;
  1193. bool has_base = false;
  1194. bool has_md1 = false;
  1195. bool has_md2 = false;
  1196. u8 mdtype = 0;
  1197. int mdlen = 0;
  1198. if (WARN_ON(is_push_nsh && is_mask))
  1199. return -EINVAL;
  1200. nla_for_each_nested(a, attr, rem) {
  1201. int type = nla_type(a);
  1202. int i;
  1203. if (type > OVS_NSH_KEY_ATTR_MAX) {
  1204. OVS_NLERR(log, "nsh attr %d is out of range max %d",
  1205. type, OVS_NSH_KEY_ATTR_MAX);
  1206. return -EINVAL;
  1207. }
  1208. if (!check_attr_len(nla_len(a),
  1209. ovs_nsh_key_attr_lens[type].len)) {
  1210. OVS_NLERR(
  1211. log,
  1212. "nsh attr %d has unexpected len %d expected %d",
  1213. type,
  1214. nla_len(a),
  1215. ovs_nsh_key_attr_lens[type].len
  1216. );
  1217. return -EINVAL;
  1218. }
  1219. switch (type) {
  1220. case OVS_NSH_KEY_ATTR_BASE: {
  1221. const struct ovs_nsh_key_base *base = nla_data(a);
  1222. has_base = true;
  1223. mdtype = base->mdtype;
  1224. SW_FLOW_KEY_PUT(match, nsh.base.flags,
  1225. base->flags, is_mask);
  1226. SW_FLOW_KEY_PUT(match, nsh.base.ttl,
  1227. base->ttl, is_mask);
  1228. SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
  1229. base->mdtype, is_mask);
  1230. SW_FLOW_KEY_PUT(match, nsh.base.np,
  1231. base->np, is_mask);
  1232. SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
  1233. base->path_hdr, is_mask);
  1234. break;
  1235. }
  1236. case OVS_NSH_KEY_ATTR_MD1: {
  1237. const struct ovs_nsh_key_md1 *md1 = nla_data(a);
  1238. has_md1 = true;
  1239. for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
  1240. SW_FLOW_KEY_PUT(match, nsh.context[i],
  1241. md1->context[i], is_mask);
  1242. break;
  1243. }
  1244. case OVS_NSH_KEY_ATTR_MD2:
  1245. if (!is_push_nsh) /* Not supported MD type 2 yet */
  1246. return -ENOTSUPP;
  1247. has_md2 = true;
  1248. mdlen = nla_len(a);
  1249. if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
  1250. OVS_NLERR(
  1251. log,
  1252. "Invalid MD length %d for MD type %d",
  1253. mdlen,
  1254. mdtype
  1255. );
  1256. return -EINVAL;
  1257. }
  1258. break;
  1259. default:
  1260. OVS_NLERR(log, "Unknown nsh attribute %d",
  1261. type);
  1262. return -EINVAL;
  1263. }
  1264. }
  1265. if (rem > 0) {
  1266. OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
  1267. return -EINVAL;
  1268. }
  1269. if (has_md1 && has_md2) {
  1270. OVS_NLERR(
  1271. 1,
  1272. "invalid nsh attribute: md1 and md2 are exclusive."
  1273. );
  1274. return -EINVAL;
  1275. }
  1276. if (!is_mask) {
  1277. if ((has_md1 && mdtype != NSH_M_TYPE1) ||
  1278. (has_md2 && mdtype != NSH_M_TYPE2)) {
  1279. OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
  1280. mdtype);
  1281. return -EINVAL;
  1282. }
  1283. if (is_push_nsh &&
  1284. (!has_base || (!has_md1 && !has_md2))) {
  1285. OVS_NLERR(
  1286. 1,
  1287. "push_nsh: missing base or metadata attributes"
  1288. );
  1289. return -EINVAL;
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
  1295. u64 attrs, const struct nlattr **a,
  1296. bool is_mask, bool log)
  1297. {
  1298. int err;
  1299. err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
  1300. if (err)
  1301. return err;
  1302. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  1303. const struct ovs_key_ethernet *eth_key;
  1304. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  1305. SW_FLOW_KEY_MEMCPY(match, eth.src,
  1306. eth_key->eth_src, ETH_ALEN, is_mask);
  1307. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  1308. eth_key->eth_dst, ETH_ALEN, is_mask);
  1309. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  1310. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  1311. /* VLAN attribute is always parsed before getting here since it
  1312. * may occur multiple times.
  1313. */
  1314. OVS_NLERR(log, "VLAN attribute unexpected.");
  1315. return -EINVAL;
  1316. }
  1317. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  1318. err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
  1319. log);
  1320. if (err)
  1321. return err;
  1322. } else if (!is_mask) {
  1323. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  1324. }
  1325. } else if (!match->key->eth.type) {
  1326. OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
  1327. return -EINVAL;
  1328. }
  1329. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1330. const struct ovs_key_ipv4 *ipv4_key;
  1331. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  1332. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  1333. OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
  1334. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  1335. return -EINVAL;
  1336. }
  1337. SW_FLOW_KEY_PUT(match, ip.proto,
  1338. ipv4_key->ipv4_proto, is_mask);
  1339. SW_FLOW_KEY_PUT(match, ip.tos,
  1340. ipv4_key->ipv4_tos, is_mask);
  1341. SW_FLOW_KEY_PUT(match, ip.ttl,
  1342. ipv4_key->ipv4_ttl, is_mask);
  1343. SW_FLOW_KEY_PUT(match, ip.frag,
  1344. ipv4_key->ipv4_frag, is_mask);
  1345. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1346. ipv4_key->ipv4_src, is_mask);
  1347. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1348. ipv4_key->ipv4_dst, is_mask);
  1349. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  1350. }
  1351. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  1352. const struct ovs_key_ipv6 *ipv6_key;
  1353. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  1354. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  1355. OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
  1356. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  1357. return -EINVAL;
  1358. }
  1359. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  1360. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
  1361. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  1362. return -EINVAL;
  1363. }
  1364. SW_FLOW_KEY_PUT(match, ipv6.label,
  1365. ipv6_key->ipv6_label, is_mask);
  1366. SW_FLOW_KEY_PUT(match, ip.proto,
  1367. ipv6_key->ipv6_proto, is_mask);
  1368. SW_FLOW_KEY_PUT(match, ip.tos,
  1369. ipv6_key->ipv6_tclass, is_mask);
  1370. SW_FLOW_KEY_PUT(match, ip.ttl,
  1371. ipv6_key->ipv6_hlimit, is_mask);
  1372. SW_FLOW_KEY_PUT(match, ip.frag,
  1373. ipv6_key->ipv6_frag, is_mask);
  1374. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  1375. ipv6_key->ipv6_src,
  1376. sizeof(match->key->ipv6.addr.src),
  1377. is_mask);
  1378. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  1379. ipv6_key->ipv6_dst,
  1380. sizeof(match->key->ipv6.addr.dst),
  1381. is_mask);
  1382. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1383. }
  1384. if (attrs & (1ULL << OVS_KEY_ATTR_IPV6_EXTHDRS)) {
  1385. const struct ovs_key_ipv6_exthdrs *ipv6_exthdrs_key;
  1386. ipv6_exthdrs_key = nla_data(a[OVS_KEY_ATTR_IPV6_EXTHDRS]);
  1387. SW_FLOW_KEY_PUT(match, ipv6.exthdrs,
  1388. ipv6_exthdrs_key->hdrs, is_mask);
  1389. attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6_EXTHDRS);
  1390. }
  1391. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  1392. const struct ovs_key_arp *arp_key;
  1393. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1394. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  1395. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  1396. arp_key->arp_op);
  1397. return -EINVAL;
  1398. }
  1399. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1400. arp_key->arp_sip, is_mask);
  1401. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1402. arp_key->arp_tip, is_mask);
  1403. SW_FLOW_KEY_PUT(match, ip.proto,
  1404. ntohs(arp_key->arp_op), is_mask);
  1405. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  1406. arp_key->arp_sha, ETH_ALEN, is_mask);
  1407. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  1408. arp_key->arp_tha, ETH_ALEN, is_mask);
  1409. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1410. }
  1411. if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
  1412. if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
  1413. is_mask, false, log) < 0)
  1414. return -EINVAL;
  1415. attrs &= ~(1 << OVS_KEY_ATTR_NSH);
  1416. }
  1417. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  1418. const struct ovs_key_mpls *mpls_key;
  1419. u32 hdr_len;
  1420. u32 label_count, label_count_mask, i;
  1421. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  1422. hdr_len = nla_len(a[OVS_KEY_ATTR_MPLS]);
  1423. label_count = hdr_len / sizeof(struct ovs_key_mpls);
  1424. if (label_count == 0 || label_count > MPLS_LABEL_DEPTH ||
  1425. hdr_len % sizeof(struct ovs_key_mpls))
  1426. return -EINVAL;
  1427. label_count_mask = GENMASK(label_count - 1, 0);
  1428. for (i = 0 ; i < label_count; i++)
  1429. SW_FLOW_KEY_PUT(match, mpls.lse[i],
  1430. mpls_key[i].mpls_lse, is_mask);
  1431. SW_FLOW_KEY_PUT(match, mpls.num_labels_mask,
  1432. label_count_mask, is_mask);
  1433. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  1434. }
  1435. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  1436. const struct ovs_key_tcp *tcp_key;
  1437. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  1438. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  1439. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  1440. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  1441. }
  1442. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  1443. SW_FLOW_KEY_PUT(match, tp.flags,
  1444. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  1445. is_mask);
  1446. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  1447. }
  1448. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  1449. const struct ovs_key_udp *udp_key;
  1450. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  1451. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  1452. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  1453. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  1454. }
  1455. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  1456. const struct ovs_key_sctp *sctp_key;
  1457. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  1458. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  1459. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  1460. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  1461. }
  1462. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  1463. const struct ovs_key_icmp *icmp_key;
  1464. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  1465. SW_FLOW_KEY_PUT(match, tp.src,
  1466. htons(icmp_key->icmp_type), is_mask);
  1467. SW_FLOW_KEY_PUT(match, tp.dst,
  1468. htons(icmp_key->icmp_code), is_mask);
  1469. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  1470. }
  1471. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  1472. const struct ovs_key_icmpv6 *icmpv6_key;
  1473. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  1474. SW_FLOW_KEY_PUT(match, tp.src,
  1475. htons(icmpv6_key->icmpv6_type), is_mask);
  1476. SW_FLOW_KEY_PUT(match, tp.dst,
  1477. htons(icmpv6_key->icmpv6_code), is_mask);
  1478. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  1479. }
  1480. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  1481. const struct ovs_key_nd *nd_key;
  1482. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  1483. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  1484. nd_key->nd_target,
  1485. sizeof(match->key->ipv6.nd.target),
  1486. is_mask);
  1487. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  1488. nd_key->nd_sll, ETH_ALEN, is_mask);
  1489. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  1490. nd_key->nd_tll, ETH_ALEN, is_mask);
  1491. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  1492. }
  1493. if (attrs != 0) {
  1494. OVS_NLERR(log, "Unknown key attributes %llx",
  1495. (unsigned long long)attrs);
  1496. return -EINVAL;
  1497. }
  1498. return 0;
  1499. }
  1500. static void nlattr_set(struct nlattr *attr, u8 val,
  1501. const struct ovs_len_tbl *tbl)
  1502. {
  1503. struct nlattr *nla;
  1504. int rem;
  1505. /* The nlattr stream should already have been validated */
  1506. nla_for_each_nested(nla, attr, rem) {
  1507. if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
  1508. nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
  1509. else
  1510. memset(nla_data(nla), val, nla_len(nla));
  1511. if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
  1512. *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
  1513. }
  1514. }
  1515. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  1516. {
  1517. nlattr_set(attr, val, ovs_key_lens);
  1518. }
  1519. /**
  1520. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  1521. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1522. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1523. * does not include any don't care bit.
  1524. * @net: Used to determine per-namespace field support.
  1525. * @match: receives the extracted flow match information.
  1526. * @nla_key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1527. * sequence. The fields should of the packet that triggered the creation
  1528. * of this flow.
  1529. * @nla_mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_*
  1530. * Netlink attribute specifies the mask field of the wildcarded flow.
  1531. * @log: Boolean to allow kernel error logging. Normally true, but when
  1532. * probing for feature compatibility this should be passed in as false to
  1533. * suppress unnecessary error logging.
  1534. */
  1535. int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
  1536. const struct nlattr *nla_key,
  1537. const struct nlattr *nla_mask,
  1538. bool log)
  1539. {
  1540. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1541. struct nlattr *newmask = NULL;
  1542. u64 key_attrs = 0;
  1543. u64 mask_attrs = 0;
  1544. int err;
  1545. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  1546. if (err)
  1547. return err;
  1548. err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
  1549. if (err)
  1550. return err;
  1551. err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
  1552. if (err)
  1553. return err;
  1554. if (match->mask) {
  1555. if (!nla_mask) {
  1556. /* Create an exact match mask. We need to set to 0xff
  1557. * all the 'match->mask' fields that have been touched
  1558. * in 'match->key'. We cannot simply memset
  1559. * 'match->mask', because padding bytes and fields not
  1560. * specified in 'match->key' should be left to 0.
  1561. * Instead, we use a stream of netlink attributes,
  1562. * copied from 'key' and set to 0xff.
  1563. * ovs_key_from_nlattrs() will take care of filling
  1564. * 'match->mask' appropriately.
  1565. */
  1566. newmask = kmemdup(nla_key,
  1567. nla_total_size(nla_len(nla_key)),
  1568. GFP_KERNEL);
  1569. if (!newmask)
  1570. return -ENOMEM;
  1571. mask_set_nlattr(newmask, 0xff);
  1572. /* The userspace does not send tunnel attributes that
  1573. * are 0, but we should not wildcard them nonetheless.
  1574. */
  1575. if (match->key->tun_proto)
  1576. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  1577. 0xff, true);
  1578. nla_mask = newmask;
  1579. }
  1580. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  1581. if (err)
  1582. goto free_newmask;
  1583. /* Always match on tci. */
  1584. SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
  1585. SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
  1586. err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
  1587. if (err)
  1588. goto free_newmask;
  1589. err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
  1590. log);
  1591. if (err)
  1592. goto free_newmask;
  1593. }
  1594. if (!match_validate(match, key_attrs, mask_attrs, log))
  1595. err = -EINVAL;
  1596. free_newmask:
  1597. kfree(newmask);
  1598. return err;
  1599. }
  1600. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1601. {
  1602. size_t len;
  1603. if (!attr)
  1604. return 0;
  1605. len = nla_len(attr);
  1606. if (len < 1 || len > MAX_UFID_LENGTH) {
  1607. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1608. nla_len(attr), MAX_UFID_LENGTH);
  1609. return 0;
  1610. }
  1611. return len;
  1612. }
  1613. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1614. * or false otherwise.
  1615. */
  1616. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1617. bool log)
  1618. {
  1619. sfid->ufid_len = get_ufid_len(attr, log);
  1620. if (sfid->ufid_len)
  1621. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1622. return sfid->ufid_len;
  1623. }
  1624. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1625. const struct sw_flow_key *key, bool log)
  1626. {
  1627. struct sw_flow_key *new_key;
  1628. if (ovs_nla_get_ufid(sfid, ufid, log))
  1629. return 0;
  1630. /* If UFID was not provided, use unmasked key. */
  1631. new_key = kmalloc_obj(*new_key);
  1632. if (!new_key)
  1633. return -ENOMEM;
  1634. memcpy(new_key, key, sizeof(*key));
  1635. sfid->unmasked_key = new_key;
  1636. return 0;
  1637. }
  1638. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1639. {
  1640. return nla_get_u32_default(attr, 0);
  1641. }
  1642. /**
  1643. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1644. * @net: Network namespace.
  1645. * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
  1646. * metadata.
  1647. * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
  1648. * attributes.
  1649. * @attrs: Bit mask for the netlink attributes included in @a.
  1650. * @log: Boolean to allow kernel error logging. Normally true, but when
  1651. * probing for feature compatibility this should be passed in as false to
  1652. * suppress unnecessary error logging.
  1653. *
  1654. * This parses a series of Netlink attributes that form a flow key, which must
  1655. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1656. * get the metadata, that is, the parts of the flow key that cannot be
  1657. * extracted from the packet itself.
  1658. *
  1659. * This must be called before the packet key fields are filled in 'key'.
  1660. */
  1661. int ovs_nla_get_flow_metadata(struct net *net,
  1662. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
  1663. u64 attrs, struct sw_flow_key *key, bool log)
  1664. {
  1665. struct sw_flow_match match;
  1666. memset(&match, 0, sizeof(match));
  1667. match.key = key;
  1668. key->ct_state = 0;
  1669. key->ct_zone = 0;
  1670. key->ct_orig_proto = 0;
  1671. memset(&key->ct, 0, sizeof(key->ct));
  1672. memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
  1673. memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
  1674. key->phy.in_port = DP_MAX_PORTS;
  1675. return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
  1676. }
  1677. static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
  1678. bool is_mask)
  1679. {
  1680. __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
  1681. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1682. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
  1683. return -EMSGSIZE;
  1684. return 0;
  1685. }
  1686. static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
  1687. struct sk_buff *skb)
  1688. {
  1689. struct nlattr *start;
  1690. start = nla_nest_start_noflag(skb, OVS_KEY_ATTR_NSH);
  1691. if (!start)
  1692. return -EMSGSIZE;
  1693. if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
  1694. goto nla_put_failure;
  1695. if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
  1696. if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
  1697. sizeof(nsh->context), nsh->context))
  1698. goto nla_put_failure;
  1699. }
  1700. /* Don't support MD type 2 yet */
  1701. nla_nest_end(skb, start);
  1702. return 0;
  1703. nla_put_failure:
  1704. return -EMSGSIZE;
  1705. }
  1706. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1707. const struct sw_flow_key *output, bool is_mask,
  1708. struct sk_buff *skb)
  1709. {
  1710. struct ovs_key_ethernet *eth_key;
  1711. struct nlattr *nla;
  1712. struct nlattr *encap = NULL;
  1713. struct nlattr *in_encap = NULL;
  1714. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1715. goto nla_put_failure;
  1716. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1717. goto nla_put_failure;
  1718. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1719. goto nla_put_failure;
  1720. if ((swkey->tun_proto || is_mask)) {
  1721. const void *opts = NULL;
  1722. if (ip_tunnel_is_options_present(output->tun_key.tun_flags))
  1723. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1724. if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
  1725. swkey->tun_opts_len, swkey->tun_proto, 0))
  1726. goto nla_put_failure;
  1727. }
  1728. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1729. if (is_mask && (output->phy.in_port == 0xffff))
  1730. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1731. goto nla_put_failure;
  1732. } else {
  1733. u16 upper_u16;
  1734. upper_u16 = !is_mask ? 0 : 0xffff;
  1735. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1736. (upper_u16 << 16) | output->phy.in_port))
  1737. goto nla_put_failure;
  1738. }
  1739. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1740. goto nla_put_failure;
  1741. if (ovs_ct_put_key(swkey, output, skb))
  1742. goto nla_put_failure;
  1743. if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
  1744. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1745. if (!nla)
  1746. goto nla_put_failure;
  1747. eth_key = nla_data(nla);
  1748. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1749. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1750. if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
  1751. if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
  1752. goto nla_put_failure;
  1753. encap = nla_nest_start_noflag(skb, OVS_KEY_ATTR_ENCAP);
  1754. if (!swkey->eth.vlan.tci)
  1755. goto unencap;
  1756. if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
  1757. if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
  1758. goto nla_put_failure;
  1759. in_encap = nla_nest_start_noflag(skb,
  1760. OVS_KEY_ATTR_ENCAP);
  1761. if (!swkey->eth.cvlan.tci)
  1762. goto unencap;
  1763. }
  1764. }
  1765. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1766. /*
  1767. * Ethertype 802.2 is represented in the netlink with omitted
  1768. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1769. * 0xffff in the mask attribute. Ethertype can also
  1770. * be wildcarded.
  1771. */
  1772. if (is_mask && output->eth.type)
  1773. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1774. output->eth.type))
  1775. goto nla_put_failure;
  1776. goto unencap;
  1777. }
  1778. }
  1779. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1780. goto nla_put_failure;
  1781. if (eth_type_vlan(swkey->eth.type)) {
  1782. /* There are 3 VLAN tags, we don't know anything about the rest
  1783. * of the packet, so truncate here.
  1784. */
  1785. WARN_ON_ONCE(!(encap && in_encap));
  1786. goto unencap;
  1787. }
  1788. if (swkey->eth.type == htons(ETH_P_IP)) {
  1789. struct ovs_key_ipv4 *ipv4_key;
  1790. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1791. if (!nla)
  1792. goto nla_put_failure;
  1793. ipv4_key = nla_data(nla);
  1794. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1795. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1796. ipv4_key->ipv4_proto = output->ip.proto;
  1797. ipv4_key->ipv4_tos = output->ip.tos;
  1798. ipv4_key->ipv4_ttl = output->ip.ttl;
  1799. ipv4_key->ipv4_frag = output->ip.frag;
  1800. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1801. struct ovs_key_ipv6 *ipv6_key;
  1802. struct ovs_key_ipv6_exthdrs *ipv6_exthdrs_key;
  1803. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1804. if (!nla)
  1805. goto nla_put_failure;
  1806. ipv6_key = nla_data(nla);
  1807. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1808. sizeof(ipv6_key->ipv6_src));
  1809. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1810. sizeof(ipv6_key->ipv6_dst));
  1811. ipv6_key->ipv6_label = output->ipv6.label;
  1812. ipv6_key->ipv6_proto = output->ip.proto;
  1813. ipv6_key->ipv6_tclass = output->ip.tos;
  1814. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1815. ipv6_key->ipv6_frag = output->ip.frag;
  1816. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6_EXTHDRS,
  1817. sizeof(*ipv6_exthdrs_key));
  1818. if (!nla)
  1819. goto nla_put_failure;
  1820. ipv6_exthdrs_key = nla_data(nla);
  1821. ipv6_exthdrs_key->hdrs = output->ipv6.exthdrs;
  1822. } else if (swkey->eth.type == htons(ETH_P_NSH)) {
  1823. if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
  1824. goto nla_put_failure;
  1825. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1826. swkey->eth.type == htons(ETH_P_RARP)) {
  1827. struct ovs_key_arp *arp_key;
  1828. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1829. if (!nla)
  1830. goto nla_put_failure;
  1831. arp_key = nla_data(nla);
  1832. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1833. arp_key->arp_sip = output->ipv4.addr.src;
  1834. arp_key->arp_tip = output->ipv4.addr.dst;
  1835. arp_key->arp_op = htons(output->ip.proto);
  1836. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1837. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1838. } else if (eth_p_mpls(swkey->eth.type)) {
  1839. u8 i, num_labels;
  1840. struct ovs_key_mpls *mpls_key;
  1841. num_labels = hweight_long(output->mpls.num_labels_mask);
  1842. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS,
  1843. num_labels * sizeof(*mpls_key));
  1844. if (!nla)
  1845. goto nla_put_failure;
  1846. mpls_key = nla_data(nla);
  1847. for (i = 0; i < num_labels; i++)
  1848. mpls_key[i].mpls_lse = output->mpls.lse[i];
  1849. }
  1850. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1851. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1852. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1853. if (swkey->ip.proto == IPPROTO_TCP) {
  1854. struct ovs_key_tcp *tcp_key;
  1855. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1856. if (!nla)
  1857. goto nla_put_failure;
  1858. tcp_key = nla_data(nla);
  1859. tcp_key->tcp_src = output->tp.src;
  1860. tcp_key->tcp_dst = output->tp.dst;
  1861. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1862. output->tp.flags))
  1863. goto nla_put_failure;
  1864. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1865. struct ovs_key_udp *udp_key;
  1866. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1867. if (!nla)
  1868. goto nla_put_failure;
  1869. udp_key = nla_data(nla);
  1870. udp_key->udp_src = output->tp.src;
  1871. udp_key->udp_dst = output->tp.dst;
  1872. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1873. struct ovs_key_sctp *sctp_key;
  1874. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1875. if (!nla)
  1876. goto nla_put_failure;
  1877. sctp_key = nla_data(nla);
  1878. sctp_key->sctp_src = output->tp.src;
  1879. sctp_key->sctp_dst = output->tp.dst;
  1880. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1881. swkey->ip.proto == IPPROTO_ICMP) {
  1882. struct ovs_key_icmp *icmp_key;
  1883. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1884. if (!nla)
  1885. goto nla_put_failure;
  1886. icmp_key = nla_data(nla);
  1887. icmp_key->icmp_type = ntohs(output->tp.src);
  1888. icmp_key->icmp_code = ntohs(output->tp.dst);
  1889. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1890. swkey->ip.proto == IPPROTO_ICMPV6) {
  1891. struct ovs_key_icmpv6 *icmpv6_key;
  1892. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1893. sizeof(*icmpv6_key));
  1894. if (!nla)
  1895. goto nla_put_failure;
  1896. icmpv6_key = nla_data(nla);
  1897. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1898. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1899. if (swkey->tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  1900. swkey->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  1901. struct ovs_key_nd *nd_key;
  1902. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1903. if (!nla)
  1904. goto nla_put_failure;
  1905. nd_key = nla_data(nla);
  1906. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1907. sizeof(nd_key->nd_target));
  1908. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1909. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1910. }
  1911. }
  1912. }
  1913. unencap:
  1914. if (in_encap)
  1915. nla_nest_end(skb, in_encap);
  1916. if (encap)
  1917. nla_nest_end(skb, encap);
  1918. return 0;
  1919. nla_put_failure:
  1920. return -EMSGSIZE;
  1921. }
  1922. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1923. const struct sw_flow_key *output, int attr, bool is_mask,
  1924. struct sk_buff *skb)
  1925. {
  1926. int err;
  1927. struct nlattr *nla;
  1928. nla = nla_nest_start_noflag(skb, attr);
  1929. if (!nla)
  1930. return -EMSGSIZE;
  1931. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1932. if (err)
  1933. return err;
  1934. nla_nest_end(skb, nla);
  1935. return 0;
  1936. }
  1937. /* Called with ovs_mutex or RCU read lock. */
  1938. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1939. {
  1940. if (ovs_identifier_is_ufid(&flow->id))
  1941. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1942. flow->id.ufid);
  1943. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1944. OVS_FLOW_ATTR_KEY, false, skb);
  1945. }
  1946. /* Called with ovs_mutex or RCU read lock. */
  1947. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1948. {
  1949. return ovs_nla_put_key(&flow->key, &flow->key,
  1950. OVS_FLOW_ATTR_KEY, false, skb);
  1951. }
  1952. /* Called with ovs_mutex or RCU read lock. */
  1953. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1954. {
  1955. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1956. OVS_FLOW_ATTR_MASK, true, skb);
  1957. }
  1958. static struct sw_flow_actions *nla_alloc_flow_actions(int size)
  1959. {
  1960. struct sw_flow_actions *sfa;
  1961. sfa = kmalloc(kmalloc_size_roundup(sizeof(*sfa) + size), GFP_KERNEL);
  1962. if (!sfa)
  1963. return ERR_PTR(-ENOMEM);
  1964. sfa->actions_len = 0;
  1965. return sfa;
  1966. }
  1967. static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len);
  1968. static void ovs_nla_free_check_pkt_len_action(const struct nlattr *action)
  1969. {
  1970. const struct nlattr *a;
  1971. int rem;
  1972. nla_for_each_nested(a, action, rem) {
  1973. switch (nla_type(a)) {
  1974. case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL:
  1975. case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER:
  1976. ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
  1977. break;
  1978. }
  1979. }
  1980. }
  1981. static void ovs_nla_free_clone_action(const struct nlattr *action)
  1982. {
  1983. const struct nlattr *a = nla_data(action);
  1984. int rem = nla_len(action);
  1985. switch (nla_type(a)) {
  1986. case OVS_CLONE_ATTR_EXEC:
  1987. /* The real list of actions follows this attribute. */
  1988. a = nla_next(a, &rem);
  1989. ovs_nla_free_nested_actions(a, rem);
  1990. break;
  1991. }
  1992. }
  1993. static void ovs_nla_free_dec_ttl_action(const struct nlattr *action)
  1994. {
  1995. const struct nlattr *a = nla_data(action);
  1996. switch (nla_type(a)) {
  1997. case OVS_DEC_TTL_ATTR_ACTION:
  1998. ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
  1999. break;
  2000. }
  2001. }
  2002. static void ovs_nla_free_sample_action(const struct nlattr *action)
  2003. {
  2004. const struct nlattr *a = nla_data(action);
  2005. int rem = nla_len(action);
  2006. switch (nla_type(a)) {
  2007. case OVS_SAMPLE_ATTR_ARG:
  2008. /* The real list of actions follows this attribute. */
  2009. a = nla_next(a, &rem);
  2010. ovs_nla_free_nested_actions(a, rem);
  2011. break;
  2012. }
  2013. }
  2014. static void ovs_nla_free_set_action(const struct nlattr *a)
  2015. {
  2016. const struct nlattr *ovs_key = nla_data(a);
  2017. struct ovs_tunnel_info *ovs_tun;
  2018. switch (nla_type(ovs_key)) {
  2019. case OVS_KEY_ATTR_TUNNEL_INFO:
  2020. ovs_tun = nla_data(ovs_key);
  2021. dst_release((struct dst_entry *)ovs_tun->tun_dst);
  2022. break;
  2023. }
  2024. }
  2025. static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len)
  2026. {
  2027. const struct nlattr *a;
  2028. int rem;
  2029. /* Whenever new actions are added, the need to update this
  2030. * function should be considered.
  2031. */
  2032. BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 25);
  2033. if (!actions)
  2034. return;
  2035. nla_for_each_attr(a, actions, len, rem) {
  2036. switch (nla_type(a)) {
  2037. case OVS_ACTION_ATTR_CHECK_PKT_LEN:
  2038. ovs_nla_free_check_pkt_len_action(a);
  2039. break;
  2040. case OVS_ACTION_ATTR_CLONE:
  2041. ovs_nla_free_clone_action(a);
  2042. break;
  2043. case OVS_ACTION_ATTR_CT:
  2044. ovs_ct_free_action(a);
  2045. break;
  2046. case OVS_ACTION_ATTR_DEC_TTL:
  2047. ovs_nla_free_dec_ttl_action(a);
  2048. break;
  2049. case OVS_ACTION_ATTR_SAMPLE:
  2050. ovs_nla_free_sample_action(a);
  2051. break;
  2052. case OVS_ACTION_ATTR_SET:
  2053. ovs_nla_free_set_action(a);
  2054. break;
  2055. }
  2056. }
  2057. }
  2058. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  2059. {
  2060. if (!sf_acts)
  2061. return;
  2062. ovs_nla_free_nested_actions(sf_acts->actions, sf_acts->actions_len);
  2063. kfree(sf_acts);
  2064. }
  2065. static void __ovs_nla_free_flow_actions(struct rcu_head *head)
  2066. {
  2067. ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
  2068. }
  2069. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  2070. * The caller must hold rcu_read_lock for this to be sensible. */
  2071. void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
  2072. {
  2073. call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
  2074. }
  2075. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  2076. int attr_len, bool log)
  2077. {
  2078. struct sw_flow_actions *acts;
  2079. int new_acts_size;
  2080. size_t req_size = NLA_ALIGN(attr_len);
  2081. int next_offset = offsetof(struct sw_flow_actions, actions) +
  2082. (*sfa)->actions_len;
  2083. if (req_size <= (ksize(*sfa) - next_offset))
  2084. goto out;
  2085. new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
  2086. acts = nla_alloc_flow_actions(new_acts_size);
  2087. if (IS_ERR(acts))
  2088. return ERR_CAST(acts);
  2089. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  2090. acts->actions_len = (*sfa)->actions_len;
  2091. acts->orig_len = (*sfa)->orig_len;
  2092. kfree(*sfa);
  2093. *sfa = acts;
  2094. out:
  2095. (*sfa)->actions_len += req_size;
  2096. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  2097. }
  2098. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  2099. int attrtype, void *data, int len, bool log)
  2100. {
  2101. struct nlattr *a;
  2102. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  2103. if (IS_ERR(a))
  2104. return a;
  2105. a->nla_type = attrtype;
  2106. a->nla_len = nla_attr_size(len);
  2107. if (data)
  2108. memcpy(nla_data(a), data, len);
  2109. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  2110. return a;
  2111. }
  2112. int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
  2113. int len, bool log)
  2114. {
  2115. struct nlattr *a;
  2116. a = __add_action(sfa, attrtype, data, len, log);
  2117. return PTR_ERR_OR_ZERO(a);
  2118. }
  2119. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  2120. int attrtype, bool log)
  2121. {
  2122. int used = (*sfa)->actions_len;
  2123. int err;
  2124. err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
  2125. if (err)
  2126. return err;
  2127. return used;
  2128. }
  2129. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  2130. int st_offset)
  2131. {
  2132. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  2133. st_offset);
  2134. a->nla_len = sfa->actions_len - st_offset;
  2135. }
  2136. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2137. const struct sw_flow_key *key,
  2138. struct sw_flow_actions **sfa,
  2139. __be16 eth_type, __be16 vlan_tci,
  2140. u32 mpls_label_count, bool log,
  2141. u32 depth);
  2142. static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
  2143. const struct sw_flow_key *key,
  2144. struct sw_flow_actions **sfa,
  2145. __be16 eth_type, __be16 vlan_tci,
  2146. u32 mpls_label_count, bool log, bool last,
  2147. u32 depth)
  2148. {
  2149. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  2150. const struct nlattr *probability, *actions;
  2151. const struct nlattr *a;
  2152. int rem, start, err;
  2153. struct sample_arg arg;
  2154. memset(attrs, 0, sizeof(attrs));
  2155. nla_for_each_nested(a, attr, rem) {
  2156. int type = nla_type(a);
  2157. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  2158. return -EINVAL;
  2159. attrs[type] = a;
  2160. }
  2161. if (rem)
  2162. return -EINVAL;
  2163. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  2164. if (!probability || nla_len(probability) != sizeof(u32))
  2165. return -EINVAL;
  2166. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  2167. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  2168. return -EINVAL;
  2169. /* validation done, copy sample action. */
  2170. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  2171. if (start < 0)
  2172. return start;
  2173. /* When both skb and flow may be changed, put the sample
  2174. * into a deferred fifo. On the other hand, if only skb
  2175. * may be modified, the actions can be executed in place.
  2176. *
  2177. * Do this analysis at the flow installation time.
  2178. * Set 'clone_action->exec' to true if the actions can be
  2179. * executed without being deferred.
  2180. *
  2181. * If the sample is the last action, it can always be excuted
  2182. * rather than deferred.
  2183. */
  2184. arg.exec = last || !actions_may_change_flow(actions);
  2185. arg.probability = nla_get_u32(probability);
  2186. err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
  2187. log);
  2188. if (err)
  2189. return err;
  2190. err = __ovs_nla_copy_actions(net, actions, key, sfa,
  2191. eth_type, vlan_tci, mpls_label_count, log,
  2192. depth + 1);
  2193. if (err)
  2194. return err;
  2195. add_nested_action_end(*sfa, start);
  2196. return 0;
  2197. }
  2198. static int validate_and_copy_dec_ttl(struct net *net,
  2199. const struct nlattr *attr,
  2200. const struct sw_flow_key *key,
  2201. struct sw_flow_actions **sfa,
  2202. __be16 eth_type, __be16 vlan_tci,
  2203. u32 mpls_label_count, bool log,
  2204. u32 depth)
  2205. {
  2206. const struct nlattr *attrs[OVS_DEC_TTL_ATTR_MAX + 1];
  2207. int start, action_start, err, rem;
  2208. const struct nlattr *a, *actions;
  2209. memset(attrs, 0, sizeof(attrs));
  2210. nla_for_each_nested(a, attr, rem) {
  2211. int type = nla_type(a);
  2212. /* Ignore unknown attributes to be future proof. */
  2213. if (type > OVS_DEC_TTL_ATTR_MAX)
  2214. continue;
  2215. if (!type || attrs[type]) {
  2216. OVS_NLERR(log, "Duplicate or invalid key (type %d).",
  2217. type);
  2218. return -EINVAL;
  2219. }
  2220. attrs[type] = a;
  2221. }
  2222. if (rem) {
  2223. OVS_NLERR(log, "Message has %d unknown bytes.", rem);
  2224. return -EINVAL;
  2225. }
  2226. actions = attrs[OVS_DEC_TTL_ATTR_ACTION];
  2227. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) {
  2228. OVS_NLERR(log, "Missing valid actions attribute.");
  2229. return -EINVAL;
  2230. }
  2231. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_DEC_TTL, log);
  2232. if (start < 0)
  2233. return start;
  2234. action_start = add_nested_action_start(sfa, OVS_DEC_TTL_ATTR_ACTION, log);
  2235. if (action_start < 0)
  2236. return action_start;
  2237. err = __ovs_nla_copy_actions(net, actions, key, sfa, eth_type,
  2238. vlan_tci, mpls_label_count, log,
  2239. depth + 1);
  2240. if (err)
  2241. return err;
  2242. add_nested_action_end(*sfa, action_start);
  2243. add_nested_action_end(*sfa, start);
  2244. return 0;
  2245. }
  2246. static int validate_and_copy_clone(struct net *net,
  2247. const struct nlattr *attr,
  2248. const struct sw_flow_key *key,
  2249. struct sw_flow_actions **sfa,
  2250. __be16 eth_type, __be16 vlan_tci,
  2251. u32 mpls_label_count, bool log, bool last,
  2252. u32 depth)
  2253. {
  2254. int start, err;
  2255. u32 exec;
  2256. if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
  2257. return -EINVAL;
  2258. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
  2259. if (start < 0)
  2260. return start;
  2261. exec = last || !actions_may_change_flow(attr);
  2262. err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
  2263. sizeof(exec), log);
  2264. if (err)
  2265. return err;
  2266. err = __ovs_nla_copy_actions(net, attr, key, sfa,
  2267. eth_type, vlan_tci, mpls_label_count, log,
  2268. depth + 1);
  2269. if (err)
  2270. return err;
  2271. add_nested_action_end(*sfa, start);
  2272. return 0;
  2273. }
  2274. void ovs_match_init(struct sw_flow_match *match,
  2275. struct sw_flow_key *key,
  2276. bool reset_key,
  2277. struct sw_flow_mask *mask)
  2278. {
  2279. memset(match, 0, sizeof(*match));
  2280. match->key = key;
  2281. match->mask = mask;
  2282. if (reset_key)
  2283. memset(key, 0, sizeof(*key));
  2284. if (mask) {
  2285. memset(&mask->key, 0, sizeof(mask->key));
  2286. mask->range.start = mask->range.end = 0;
  2287. }
  2288. }
  2289. static int validate_geneve_opts(struct sw_flow_key *key)
  2290. {
  2291. struct geneve_opt *option;
  2292. int opts_len = key->tun_opts_len;
  2293. bool crit_opt = false;
  2294. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  2295. while (opts_len > 0) {
  2296. int len;
  2297. if (opts_len < sizeof(*option))
  2298. return -EINVAL;
  2299. len = sizeof(*option) + option->length * 4;
  2300. if (len > opts_len)
  2301. return -EINVAL;
  2302. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  2303. option = (struct geneve_opt *)((u8 *)option + len);
  2304. opts_len -= len;
  2305. }
  2306. if (crit_opt)
  2307. __set_bit(IP_TUNNEL_CRIT_OPT_BIT, key->tun_key.tun_flags);
  2308. return 0;
  2309. }
  2310. static int validate_and_copy_set_tun(const struct nlattr *attr,
  2311. struct sw_flow_actions **sfa, bool log)
  2312. {
  2313. IP_TUNNEL_DECLARE_FLAGS(dst_opt_type) = { };
  2314. struct sw_flow_match match;
  2315. struct sw_flow_key key;
  2316. struct metadata_dst *tun_dst;
  2317. struct ip_tunnel_info *tun_info;
  2318. struct ovs_tunnel_info *ovs_tun;
  2319. struct nlattr *a;
  2320. int err = 0, start, opts_type;
  2321. ovs_match_init(&match, &key, true, NULL);
  2322. opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
  2323. if (opts_type < 0)
  2324. return opts_type;
  2325. if (key.tun_opts_len) {
  2326. switch (opts_type) {
  2327. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  2328. err = validate_geneve_opts(&key);
  2329. if (err < 0)
  2330. return err;
  2331. __set_bit(IP_TUNNEL_GENEVE_OPT_BIT, dst_opt_type);
  2332. break;
  2333. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  2334. __set_bit(IP_TUNNEL_VXLAN_OPT_BIT, dst_opt_type);
  2335. break;
  2336. case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
  2337. __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT, dst_opt_type);
  2338. break;
  2339. }
  2340. }
  2341. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  2342. if (start < 0)
  2343. return start;
  2344. tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
  2345. GFP_KERNEL);
  2346. if (!tun_dst)
  2347. return -ENOMEM;
  2348. err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
  2349. if (err) {
  2350. dst_release((struct dst_entry *)tun_dst);
  2351. return err;
  2352. }
  2353. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  2354. sizeof(*ovs_tun), log);
  2355. if (IS_ERR(a)) {
  2356. dst_release((struct dst_entry *)tun_dst);
  2357. return PTR_ERR(a);
  2358. }
  2359. ovs_tun = nla_data(a);
  2360. ovs_tun->tun_dst = tun_dst;
  2361. tun_info = &tun_dst->u.tun_info;
  2362. tun_info->mode = IP_TUNNEL_INFO_TX;
  2363. if (key.tun_proto == AF_INET6)
  2364. tun_info->mode |= IP_TUNNEL_INFO_IPV6;
  2365. else if (key.tun_proto == AF_INET && key.tun_key.u.ipv4.dst == 0)
  2366. tun_info->mode |= IP_TUNNEL_INFO_BRIDGE;
  2367. tun_info->key = key.tun_key;
  2368. /* We need to store the options in the action itself since
  2369. * everything else will go away after flow setup. We can append
  2370. * it to tun_info and then point there.
  2371. */
  2372. ip_tunnel_info_opts_set(tun_info,
  2373. TUN_METADATA_OPTS(&key, key.tun_opts_len),
  2374. key.tun_opts_len, dst_opt_type);
  2375. add_nested_action_end(*sfa, start);
  2376. return err;
  2377. }
  2378. static bool validate_push_nsh(const struct nlattr *a, bool log)
  2379. {
  2380. struct nlattr *nsh_key = nla_data(a);
  2381. struct sw_flow_match match;
  2382. struct sw_flow_key key;
  2383. /* There must be one and only one NSH header. */
  2384. if (!nla_ok(nsh_key, nla_len(a)) ||
  2385. nla_total_size(nla_len(nsh_key)) != nla_len(a) ||
  2386. nla_type(nsh_key) != OVS_KEY_ATTR_NSH)
  2387. return false;
  2388. ovs_match_init(&match, &key, true, NULL);
  2389. return !nsh_key_put_from_nlattr(nsh_key, &match, false, true, log);
  2390. }
  2391. /* Return false if there are any non-masked bits set.
  2392. * Mask follows data immediately, before any netlink padding.
  2393. */
  2394. static bool validate_masked(u8 *data, int len)
  2395. {
  2396. u8 *mask = data + len;
  2397. while (len--)
  2398. if (*data++ & ~*mask++)
  2399. return false;
  2400. return true;
  2401. }
  2402. static int validate_set(const struct nlattr *a,
  2403. const struct sw_flow_key *flow_key,
  2404. struct sw_flow_actions **sfa, bool *skip_copy,
  2405. u8 mac_proto, __be16 eth_type, bool masked, bool log)
  2406. {
  2407. const struct nlattr *ovs_key = nla_data(a);
  2408. int key_type = nla_type(ovs_key);
  2409. size_t key_len;
  2410. /* There can be only one key in a action */
  2411. if (!nla_ok(ovs_key, nla_len(a)) ||
  2412. nla_total_size(nla_len(ovs_key)) != nla_len(a))
  2413. return -EINVAL;
  2414. key_len = nla_len(ovs_key);
  2415. if (masked)
  2416. key_len /= 2;
  2417. if (key_type > OVS_KEY_ATTR_MAX ||
  2418. !check_attr_len(key_len, ovs_key_lens[key_type].len))
  2419. return -EINVAL;
  2420. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  2421. return -EINVAL;
  2422. switch (key_type) {
  2423. case OVS_KEY_ATTR_PRIORITY:
  2424. case OVS_KEY_ATTR_SKB_MARK:
  2425. case OVS_KEY_ATTR_CT_MARK:
  2426. case OVS_KEY_ATTR_CT_LABELS:
  2427. break;
  2428. case OVS_KEY_ATTR_ETHERNET:
  2429. if (mac_proto != MAC_PROTO_ETHERNET)
  2430. return -EINVAL;
  2431. break;
  2432. case OVS_KEY_ATTR_TUNNEL: {
  2433. int err;
  2434. if (masked)
  2435. return -EINVAL; /* Masked tunnel set not supported. */
  2436. *skip_copy = true;
  2437. err = validate_and_copy_set_tun(a, sfa, log);
  2438. if (err)
  2439. return err;
  2440. break;
  2441. }
  2442. case OVS_KEY_ATTR_IPV4: {
  2443. const struct ovs_key_ipv4 *ipv4_key;
  2444. if (eth_type != htons(ETH_P_IP))
  2445. return -EINVAL;
  2446. ipv4_key = nla_data(ovs_key);
  2447. if (masked) {
  2448. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  2449. /* Non-writeable fields. */
  2450. if (mask->ipv4_proto || mask->ipv4_frag)
  2451. return -EINVAL;
  2452. } else {
  2453. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  2454. return -EINVAL;
  2455. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  2456. return -EINVAL;
  2457. }
  2458. break;
  2459. }
  2460. case OVS_KEY_ATTR_IPV6: {
  2461. const struct ovs_key_ipv6 *ipv6_key;
  2462. if (eth_type != htons(ETH_P_IPV6))
  2463. return -EINVAL;
  2464. ipv6_key = nla_data(ovs_key);
  2465. if (masked) {
  2466. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  2467. /* Non-writeable fields. */
  2468. if (mask->ipv6_proto || mask->ipv6_frag)
  2469. return -EINVAL;
  2470. /* Invalid bits in the flow label mask? */
  2471. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  2472. return -EINVAL;
  2473. } else {
  2474. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  2475. return -EINVAL;
  2476. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  2477. return -EINVAL;
  2478. }
  2479. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  2480. return -EINVAL;
  2481. break;
  2482. }
  2483. case OVS_KEY_ATTR_TCP:
  2484. if ((eth_type != htons(ETH_P_IP) &&
  2485. eth_type != htons(ETH_P_IPV6)) ||
  2486. flow_key->ip.proto != IPPROTO_TCP)
  2487. return -EINVAL;
  2488. break;
  2489. case OVS_KEY_ATTR_UDP:
  2490. if ((eth_type != htons(ETH_P_IP) &&
  2491. eth_type != htons(ETH_P_IPV6)) ||
  2492. flow_key->ip.proto != IPPROTO_UDP)
  2493. return -EINVAL;
  2494. break;
  2495. case OVS_KEY_ATTR_MPLS:
  2496. if (!eth_p_mpls(eth_type))
  2497. return -EINVAL;
  2498. if (key_len != sizeof(struct ovs_key_mpls))
  2499. return -EINVAL;
  2500. break;
  2501. case OVS_KEY_ATTR_SCTP:
  2502. if ((eth_type != htons(ETH_P_IP) &&
  2503. eth_type != htons(ETH_P_IPV6)) ||
  2504. flow_key->ip.proto != IPPROTO_SCTP)
  2505. return -EINVAL;
  2506. break;
  2507. default:
  2508. return -EINVAL;
  2509. }
  2510. /* Convert non-masked non-tunnel set actions to masked set actions. */
  2511. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  2512. int start, len = key_len * 2;
  2513. struct nlattr *at;
  2514. *skip_copy = true;
  2515. start = add_nested_action_start(sfa,
  2516. OVS_ACTION_ATTR_SET_TO_MASKED,
  2517. log);
  2518. if (start < 0)
  2519. return start;
  2520. at = __add_action(sfa, key_type, NULL, len, log);
  2521. if (IS_ERR(at))
  2522. return PTR_ERR(at);
  2523. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  2524. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  2525. /* Clear non-writeable bits from otherwise writeable fields. */
  2526. if (key_type == OVS_KEY_ATTR_IPV6) {
  2527. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  2528. mask->ipv6_label &= htonl(0x000FFFFF);
  2529. }
  2530. add_nested_action_end(*sfa, start);
  2531. }
  2532. return 0;
  2533. }
  2534. static int validate_userspace(const struct nlattr *attr)
  2535. {
  2536. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  2537. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  2538. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  2539. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  2540. };
  2541. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  2542. int error;
  2543. error = nla_parse_deprecated_strict(a, OVS_USERSPACE_ATTR_MAX,
  2544. nla_data(attr), nla_len(attr),
  2545. userspace_policy, NULL);
  2546. if (error)
  2547. return error;
  2548. if (!a[OVS_USERSPACE_ATTR_PID] ||
  2549. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  2550. return -EINVAL;
  2551. return 0;
  2552. }
  2553. static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
  2554. [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
  2555. [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
  2556. [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
  2557. };
  2558. static int validate_and_copy_check_pkt_len(struct net *net,
  2559. const struct nlattr *attr,
  2560. const struct sw_flow_key *key,
  2561. struct sw_flow_actions **sfa,
  2562. __be16 eth_type, __be16 vlan_tci,
  2563. u32 mpls_label_count,
  2564. bool log, bool last, u32 depth)
  2565. {
  2566. const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
  2567. struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
  2568. struct check_pkt_len_arg arg;
  2569. int nested_acts_start;
  2570. int start, err;
  2571. err = nla_parse_deprecated_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX,
  2572. nla_data(attr), nla_len(attr),
  2573. cpl_policy, NULL);
  2574. if (err)
  2575. return err;
  2576. if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
  2577. !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
  2578. return -EINVAL;
  2579. acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
  2580. acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
  2581. /* Both the nested action should be present. */
  2582. if (!acts_if_greater || !acts_if_lesser_eq)
  2583. return -EINVAL;
  2584. /* validation done, copy the nested actions. */
  2585. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
  2586. log);
  2587. if (start < 0)
  2588. return start;
  2589. arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
  2590. arg.exec_for_lesser_equal =
  2591. last || !actions_may_change_flow(acts_if_lesser_eq);
  2592. arg.exec_for_greater =
  2593. last || !actions_may_change_flow(acts_if_greater);
  2594. err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
  2595. sizeof(arg), log);
  2596. if (err)
  2597. return err;
  2598. nested_acts_start = add_nested_action_start(sfa,
  2599. OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
  2600. if (nested_acts_start < 0)
  2601. return nested_acts_start;
  2602. err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
  2603. eth_type, vlan_tci, mpls_label_count, log,
  2604. depth + 1);
  2605. if (err)
  2606. return err;
  2607. add_nested_action_end(*sfa, nested_acts_start);
  2608. nested_acts_start = add_nested_action_start(sfa,
  2609. OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
  2610. if (nested_acts_start < 0)
  2611. return nested_acts_start;
  2612. err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
  2613. eth_type, vlan_tci, mpls_label_count, log,
  2614. depth + 1);
  2615. if (err)
  2616. return err;
  2617. add_nested_action_end(*sfa, nested_acts_start);
  2618. add_nested_action_end(*sfa, start);
  2619. return 0;
  2620. }
  2621. static int validate_psample(const struct nlattr *attr)
  2622. {
  2623. static const struct nla_policy policy[OVS_PSAMPLE_ATTR_MAX + 1] = {
  2624. [OVS_PSAMPLE_ATTR_GROUP] = { .type = NLA_U32 },
  2625. [OVS_PSAMPLE_ATTR_COOKIE] = {
  2626. .type = NLA_BINARY,
  2627. .len = OVS_PSAMPLE_COOKIE_MAX_SIZE,
  2628. },
  2629. };
  2630. struct nlattr *a[OVS_PSAMPLE_ATTR_MAX + 1];
  2631. int err;
  2632. if (!IS_ENABLED(CONFIG_PSAMPLE))
  2633. return -EOPNOTSUPP;
  2634. err = nla_parse_nested(a, OVS_PSAMPLE_ATTR_MAX, attr, policy, NULL);
  2635. if (err)
  2636. return err;
  2637. return a[OVS_PSAMPLE_ATTR_GROUP] ? 0 : -EINVAL;
  2638. }
  2639. static int copy_action(const struct nlattr *from,
  2640. struct sw_flow_actions **sfa, bool log)
  2641. {
  2642. int totlen = NLA_ALIGN(from->nla_len);
  2643. struct nlattr *to;
  2644. to = reserve_sfa_size(sfa, from->nla_len, log);
  2645. if (IS_ERR(to))
  2646. return PTR_ERR(to);
  2647. memcpy(to, from, totlen);
  2648. return 0;
  2649. }
  2650. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2651. const struct sw_flow_key *key,
  2652. struct sw_flow_actions **sfa,
  2653. __be16 eth_type, __be16 vlan_tci,
  2654. u32 mpls_label_count, bool log,
  2655. u32 depth)
  2656. {
  2657. u8 mac_proto = ovs_key_mac_proto(key);
  2658. const struct nlattr *a;
  2659. int rem, err;
  2660. if (depth > OVS_COPY_ACTIONS_MAX_DEPTH)
  2661. return -EOVERFLOW;
  2662. nla_for_each_nested(a, attr, rem) {
  2663. /* Expected argument lengths, (u32)-1 for variable length. */
  2664. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  2665. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  2666. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  2667. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  2668. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  2669. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  2670. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  2671. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  2672. [OVS_ACTION_ATTR_SET] = (u32)-1,
  2673. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  2674. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  2675. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
  2676. [OVS_ACTION_ATTR_CT] = (u32)-1,
  2677. [OVS_ACTION_ATTR_CT_CLEAR] = 0,
  2678. [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
  2679. [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
  2680. [OVS_ACTION_ATTR_POP_ETH] = 0,
  2681. [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
  2682. [OVS_ACTION_ATTR_POP_NSH] = 0,
  2683. [OVS_ACTION_ATTR_METER] = sizeof(u32),
  2684. [OVS_ACTION_ATTR_CLONE] = (u32)-1,
  2685. [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
  2686. [OVS_ACTION_ATTR_ADD_MPLS] = sizeof(struct ovs_action_add_mpls),
  2687. [OVS_ACTION_ATTR_DEC_TTL] = (u32)-1,
  2688. [OVS_ACTION_ATTR_DROP] = sizeof(u32),
  2689. [OVS_ACTION_ATTR_PSAMPLE] = (u32)-1,
  2690. };
  2691. const struct ovs_action_push_vlan *vlan;
  2692. int type = nla_type(a);
  2693. bool skip_copy;
  2694. if (type > OVS_ACTION_ATTR_MAX ||
  2695. (action_lens[type] != nla_len(a) &&
  2696. action_lens[type] != (u32)-1))
  2697. return -EINVAL;
  2698. skip_copy = false;
  2699. switch (type) {
  2700. case OVS_ACTION_ATTR_UNSPEC:
  2701. return -EINVAL;
  2702. case OVS_ACTION_ATTR_USERSPACE:
  2703. err = validate_userspace(a);
  2704. if (err)
  2705. return err;
  2706. break;
  2707. case OVS_ACTION_ATTR_OUTPUT:
  2708. if (nla_get_u32(a) >= DP_MAX_PORTS)
  2709. return -EINVAL;
  2710. break;
  2711. case OVS_ACTION_ATTR_TRUNC: {
  2712. const struct ovs_action_trunc *trunc = nla_data(a);
  2713. if (trunc->max_len < ETH_HLEN)
  2714. return -EINVAL;
  2715. break;
  2716. }
  2717. case OVS_ACTION_ATTR_HASH: {
  2718. const struct ovs_action_hash *act_hash = nla_data(a);
  2719. switch (act_hash->hash_alg) {
  2720. case OVS_HASH_ALG_L4:
  2721. fallthrough;
  2722. case OVS_HASH_ALG_SYM_L4:
  2723. break;
  2724. default:
  2725. return -EINVAL;
  2726. }
  2727. break;
  2728. }
  2729. case OVS_ACTION_ATTR_POP_VLAN:
  2730. if (mac_proto != MAC_PROTO_ETHERNET)
  2731. return -EINVAL;
  2732. vlan_tci = htons(0);
  2733. break;
  2734. case OVS_ACTION_ATTR_PUSH_VLAN:
  2735. if (mac_proto != MAC_PROTO_ETHERNET)
  2736. return -EINVAL;
  2737. vlan = nla_data(a);
  2738. if (!eth_type_vlan(vlan->vlan_tpid))
  2739. return -EINVAL;
  2740. if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
  2741. return -EINVAL;
  2742. vlan_tci = vlan->vlan_tci;
  2743. break;
  2744. case OVS_ACTION_ATTR_RECIRC:
  2745. break;
  2746. case OVS_ACTION_ATTR_ADD_MPLS: {
  2747. const struct ovs_action_add_mpls *mpls = nla_data(a);
  2748. if (!eth_p_mpls(mpls->mpls_ethertype))
  2749. return -EINVAL;
  2750. if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK) {
  2751. if (vlan_tci & htons(VLAN_CFI_MASK) ||
  2752. (eth_type != htons(ETH_P_IP) &&
  2753. eth_type != htons(ETH_P_IPV6) &&
  2754. eth_type != htons(ETH_P_ARP) &&
  2755. eth_type != htons(ETH_P_RARP) &&
  2756. !eth_p_mpls(eth_type)))
  2757. return -EINVAL;
  2758. mpls_label_count++;
  2759. } else {
  2760. if (mac_proto == MAC_PROTO_ETHERNET) {
  2761. mpls_label_count = 1;
  2762. mac_proto = MAC_PROTO_NONE;
  2763. } else {
  2764. mpls_label_count++;
  2765. }
  2766. }
  2767. eth_type = mpls->mpls_ethertype;
  2768. break;
  2769. }
  2770. case OVS_ACTION_ATTR_PUSH_MPLS: {
  2771. const struct ovs_action_push_mpls *mpls = nla_data(a);
  2772. if (!eth_p_mpls(mpls->mpls_ethertype))
  2773. return -EINVAL;
  2774. /* Prohibit push MPLS other than to a white list
  2775. * for packets that have a known tag order.
  2776. */
  2777. if (vlan_tci & htons(VLAN_CFI_MASK) ||
  2778. (eth_type != htons(ETH_P_IP) &&
  2779. eth_type != htons(ETH_P_IPV6) &&
  2780. eth_type != htons(ETH_P_ARP) &&
  2781. eth_type != htons(ETH_P_RARP) &&
  2782. !eth_p_mpls(eth_type)))
  2783. return -EINVAL;
  2784. eth_type = mpls->mpls_ethertype;
  2785. mpls_label_count++;
  2786. break;
  2787. }
  2788. case OVS_ACTION_ATTR_POP_MPLS: {
  2789. __be16 proto;
  2790. if (vlan_tci & htons(VLAN_CFI_MASK) ||
  2791. !eth_p_mpls(eth_type))
  2792. return -EINVAL;
  2793. /* Disallow subsequent L2.5+ set actions and mpls_pop
  2794. * actions once the last MPLS label in the packet is
  2795. * popped as there is no check here to ensure that
  2796. * the new eth type is valid and thus set actions could
  2797. * write off the end of the packet or otherwise corrupt
  2798. * it.
  2799. *
  2800. * Support for these actions is planned using packet
  2801. * recirculation.
  2802. */
  2803. proto = nla_get_be16(a);
  2804. if (proto == htons(ETH_P_TEB) &&
  2805. mac_proto != MAC_PROTO_NONE)
  2806. return -EINVAL;
  2807. mpls_label_count--;
  2808. if (!eth_p_mpls(proto) || !mpls_label_count)
  2809. eth_type = htons(0);
  2810. else
  2811. eth_type = proto;
  2812. break;
  2813. }
  2814. case OVS_ACTION_ATTR_SET:
  2815. err = validate_set(a, key, sfa,
  2816. &skip_copy, mac_proto, eth_type,
  2817. false, log);
  2818. if (err)
  2819. return err;
  2820. break;
  2821. case OVS_ACTION_ATTR_SET_MASKED:
  2822. err = validate_set(a, key, sfa,
  2823. &skip_copy, mac_proto, eth_type,
  2824. true, log);
  2825. if (err)
  2826. return err;
  2827. break;
  2828. case OVS_ACTION_ATTR_SAMPLE: {
  2829. bool last = nla_is_last(a, rem);
  2830. err = validate_and_copy_sample(net, a, key, sfa,
  2831. eth_type, vlan_tci,
  2832. mpls_label_count,
  2833. log, last, depth);
  2834. if (err)
  2835. return err;
  2836. skip_copy = true;
  2837. break;
  2838. }
  2839. case OVS_ACTION_ATTR_CT:
  2840. err = ovs_ct_copy_action(net, a, key, sfa, log);
  2841. if (err)
  2842. return err;
  2843. skip_copy = true;
  2844. break;
  2845. case OVS_ACTION_ATTR_CT_CLEAR:
  2846. break;
  2847. case OVS_ACTION_ATTR_PUSH_ETH:
  2848. /* Disallow pushing an Ethernet header if one
  2849. * is already present */
  2850. if (mac_proto != MAC_PROTO_NONE)
  2851. return -EINVAL;
  2852. mac_proto = MAC_PROTO_ETHERNET;
  2853. break;
  2854. case OVS_ACTION_ATTR_POP_ETH:
  2855. if (mac_proto != MAC_PROTO_ETHERNET)
  2856. return -EINVAL;
  2857. if (vlan_tci & htons(VLAN_CFI_MASK))
  2858. return -EINVAL;
  2859. mac_proto = MAC_PROTO_NONE;
  2860. break;
  2861. case OVS_ACTION_ATTR_PUSH_NSH:
  2862. if (mac_proto != MAC_PROTO_ETHERNET) {
  2863. u8 next_proto;
  2864. next_proto = tun_p_from_eth_p(eth_type);
  2865. if (!next_proto)
  2866. return -EINVAL;
  2867. }
  2868. mac_proto = MAC_PROTO_NONE;
  2869. if (!validate_push_nsh(a, log))
  2870. return -EINVAL;
  2871. break;
  2872. case OVS_ACTION_ATTR_POP_NSH: {
  2873. __be16 inner_proto;
  2874. if (eth_type != htons(ETH_P_NSH))
  2875. return -EINVAL;
  2876. inner_proto = tun_p_to_eth_p(key->nsh.base.np);
  2877. if (!inner_proto)
  2878. return -EINVAL;
  2879. if (key->nsh.base.np == TUN_P_ETHERNET)
  2880. mac_proto = MAC_PROTO_ETHERNET;
  2881. else
  2882. mac_proto = MAC_PROTO_NONE;
  2883. break;
  2884. }
  2885. case OVS_ACTION_ATTR_METER:
  2886. /* Non-existent meters are simply ignored. */
  2887. break;
  2888. case OVS_ACTION_ATTR_CLONE: {
  2889. bool last = nla_is_last(a, rem);
  2890. err = validate_and_copy_clone(net, a, key, sfa,
  2891. eth_type, vlan_tci,
  2892. mpls_label_count,
  2893. log, last, depth);
  2894. if (err)
  2895. return err;
  2896. skip_copy = true;
  2897. break;
  2898. }
  2899. case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
  2900. bool last = nla_is_last(a, rem);
  2901. err = validate_and_copy_check_pkt_len(net, a, key, sfa,
  2902. eth_type,
  2903. vlan_tci,
  2904. mpls_label_count,
  2905. log, last,
  2906. depth);
  2907. if (err)
  2908. return err;
  2909. skip_copy = true;
  2910. break;
  2911. }
  2912. case OVS_ACTION_ATTR_DEC_TTL:
  2913. err = validate_and_copy_dec_ttl(net, a, key, sfa,
  2914. eth_type, vlan_tci,
  2915. mpls_label_count, log,
  2916. depth);
  2917. if (err)
  2918. return err;
  2919. skip_copy = true;
  2920. break;
  2921. case OVS_ACTION_ATTR_DROP:
  2922. if (!nla_is_last(a, rem))
  2923. return -EINVAL;
  2924. break;
  2925. case OVS_ACTION_ATTR_PSAMPLE:
  2926. err = validate_psample(a);
  2927. if (err)
  2928. return err;
  2929. break;
  2930. default:
  2931. OVS_NLERR(log, "Unknown Action type %d", type);
  2932. return -EINVAL;
  2933. }
  2934. if (!skip_copy) {
  2935. err = copy_action(a, sfa, log);
  2936. if (err)
  2937. return err;
  2938. }
  2939. }
  2940. if (rem > 0)
  2941. return -EINVAL;
  2942. return 0;
  2943. }
  2944. /* 'key' must be the masked key. */
  2945. int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2946. const struct sw_flow_key *key,
  2947. struct sw_flow_actions **sfa, bool log)
  2948. {
  2949. int err;
  2950. u32 mpls_label_count = 0;
  2951. *sfa = nla_alloc_flow_actions(nla_len(attr));
  2952. if (IS_ERR(*sfa))
  2953. return PTR_ERR(*sfa);
  2954. if (eth_p_mpls(key->eth.type))
  2955. mpls_label_count = hweight_long(key->mpls.num_labels_mask);
  2956. (*sfa)->orig_len = nla_len(attr);
  2957. err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
  2958. key->eth.vlan.tci, mpls_label_count, log,
  2959. 0);
  2960. if (err)
  2961. ovs_nla_free_flow_actions(*sfa);
  2962. return err;
  2963. }
  2964. static int sample_action_to_attr(const struct nlattr *attr,
  2965. struct sk_buff *skb)
  2966. {
  2967. struct nlattr *start, *ac_start = NULL, *sample_arg;
  2968. int err = 0, rem = nla_len(attr);
  2969. const struct sample_arg *arg;
  2970. struct nlattr *actions;
  2971. start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SAMPLE);
  2972. if (!start)
  2973. return -EMSGSIZE;
  2974. sample_arg = nla_data(attr);
  2975. arg = nla_data(sample_arg);
  2976. actions = nla_next(sample_arg, &rem);
  2977. if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
  2978. err = -EMSGSIZE;
  2979. goto out;
  2980. }
  2981. ac_start = nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS);
  2982. if (!ac_start) {
  2983. err = -EMSGSIZE;
  2984. goto out;
  2985. }
  2986. err = ovs_nla_put_actions(actions, rem, skb);
  2987. out:
  2988. if (err) {
  2989. nla_nest_cancel(skb, ac_start);
  2990. nla_nest_cancel(skb, start);
  2991. } else {
  2992. nla_nest_end(skb, ac_start);
  2993. nla_nest_end(skb, start);
  2994. }
  2995. return err;
  2996. }
  2997. static int clone_action_to_attr(const struct nlattr *attr,
  2998. struct sk_buff *skb)
  2999. {
  3000. struct nlattr *start;
  3001. int err = 0, rem = nla_len(attr);
  3002. start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CLONE);
  3003. if (!start)
  3004. return -EMSGSIZE;
  3005. /* Skipping the OVS_CLONE_ATTR_EXEC that is always the first attribute. */
  3006. attr = nla_next(nla_data(attr), &rem);
  3007. err = ovs_nla_put_actions(attr, rem, skb);
  3008. if (err)
  3009. nla_nest_cancel(skb, start);
  3010. else
  3011. nla_nest_end(skb, start);
  3012. return err;
  3013. }
  3014. static int check_pkt_len_action_to_attr(const struct nlattr *attr,
  3015. struct sk_buff *skb)
  3016. {
  3017. struct nlattr *start, *ac_start = NULL;
  3018. const struct check_pkt_len_arg *arg;
  3019. const struct nlattr *a, *cpl_arg;
  3020. int err = 0, rem = nla_len(attr);
  3021. start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
  3022. if (!start)
  3023. return -EMSGSIZE;
  3024. /* The first nested attribute in 'attr' is always
  3025. * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
  3026. */
  3027. cpl_arg = nla_data(attr);
  3028. arg = nla_data(cpl_arg);
  3029. if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
  3030. err = -EMSGSIZE;
  3031. goto out;
  3032. }
  3033. /* Second nested attribute in 'attr' is always
  3034. * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
  3035. */
  3036. a = nla_next(cpl_arg, &rem);
  3037. ac_start = nla_nest_start_noflag(skb,
  3038. OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
  3039. if (!ac_start) {
  3040. err = -EMSGSIZE;
  3041. goto out;
  3042. }
  3043. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  3044. if (err) {
  3045. nla_nest_cancel(skb, ac_start);
  3046. goto out;
  3047. } else {
  3048. nla_nest_end(skb, ac_start);
  3049. }
  3050. /* Third nested attribute in 'attr' is always
  3051. * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
  3052. */
  3053. a = nla_next(a, &rem);
  3054. ac_start = nla_nest_start_noflag(skb,
  3055. OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
  3056. if (!ac_start) {
  3057. err = -EMSGSIZE;
  3058. goto out;
  3059. }
  3060. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  3061. if (err) {
  3062. nla_nest_cancel(skb, ac_start);
  3063. goto out;
  3064. } else {
  3065. nla_nest_end(skb, ac_start);
  3066. }
  3067. nla_nest_end(skb, start);
  3068. return 0;
  3069. out:
  3070. nla_nest_cancel(skb, start);
  3071. return err;
  3072. }
  3073. static int dec_ttl_action_to_attr(const struct nlattr *attr,
  3074. struct sk_buff *skb)
  3075. {
  3076. struct nlattr *start, *action_start;
  3077. const struct nlattr *a;
  3078. int err = 0, rem;
  3079. start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_DEC_TTL);
  3080. if (!start)
  3081. return -EMSGSIZE;
  3082. nla_for_each_attr(a, nla_data(attr), nla_len(attr), rem) {
  3083. switch (nla_type(a)) {
  3084. case OVS_DEC_TTL_ATTR_ACTION:
  3085. action_start = nla_nest_start_noflag(skb, OVS_DEC_TTL_ATTR_ACTION);
  3086. if (!action_start) {
  3087. err = -EMSGSIZE;
  3088. goto out;
  3089. }
  3090. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  3091. if (err)
  3092. goto out;
  3093. nla_nest_end(skb, action_start);
  3094. break;
  3095. default:
  3096. /* Ignore all other option to be future compatible */
  3097. break;
  3098. }
  3099. }
  3100. nla_nest_end(skb, start);
  3101. return 0;
  3102. out:
  3103. nla_nest_cancel(skb, start);
  3104. return err;
  3105. }
  3106. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  3107. {
  3108. const struct nlattr *ovs_key = nla_data(a);
  3109. int key_type = nla_type(ovs_key);
  3110. struct nlattr *start;
  3111. int err;
  3112. switch (key_type) {
  3113. case OVS_KEY_ATTR_TUNNEL_INFO: {
  3114. struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
  3115. struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
  3116. start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
  3117. if (!start)
  3118. return -EMSGSIZE;
  3119. err = ip_tun_to_nlattr(skb, &tun_info->key,
  3120. ip_tunnel_info_opts(tun_info),
  3121. tun_info->options_len,
  3122. ip_tunnel_info_af(tun_info), tun_info->mode);
  3123. if (err)
  3124. return err;
  3125. nla_nest_end(skb, start);
  3126. break;
  3127. }
  3128. default:
  3129. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  3130. return -EMSGSIZE;
  3131. break;
  3132. }
  3133. return 0;
  3134. }
  3135. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  3136. struct sk_buff *skb)
  3137. {
  3138. const struct nlattr *ovs_key = nla_data(a);
  3139. struct nlattr *nla;
  3140. size_t key_len = nla_len(ovs_key) / 2;
  3141. /* Revert the conversion we did from a non-masked set action to
  3142. * masked set action.
  3143. */
  3144. nla = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
  3145. if (!nla)
  3146. return -EMSGSIZE;
  3147. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  3148. return -EMSGSIZE;
  3149. nla_nest_end(skb, nla);
  3150. return 0;
  3151. }
  3152. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  3153. {
  3154. const struct nlattr *a;
  3155. int rem, err;
  3156. nla_for_each_attr(a, attr, len, rem) {
  3157. int type = nla_type(a);
  3158. switch (type) {
  3159. case OVS_ACTION_ATTR_SET:
  3160. err = set_action_to_attr(a, skb);
  3161. if (err)
  3162. return err;
  3163. break;
  3164. case OVS_ACTION_ATTR_SET_TO_MASKED:
  3165. err = masked_set_action_to_set_action_attr(a, skb);
  3166. if (err)
  3167. return err;
  3168. break;
  3169. case OVS_ACTION_ATTR_SAMPLE:
  3170. err = sample_action_to_attr(a, skb);
  3171. if (err)
  3172. return err;
  3173. break;
  3174. case OVS_ACTION_ATTR_CT:
  3175. err = ovs_ct_action_to_attr(nla_data(a), skb);
  3176. if (err)
  3177. return err;
  3178. break;
  3179. case OVS_ACTION_ATTR_CLONE:
  3180. err = clone_action_to_attr(a, skb);
  3181. if (err)
  3182. return err;
  3183. break;
  3184. case OVS_ACTION_ATTR_CHECK_PKT_LEN:
  3185. err = check_pkt_len_action_to_attr(a, skb);
  3186. if (err)
  3187. return err;
  3188. break;
  3189. case OVS_ACTION_ATTR_DEC_TTL:
  3190. err = dec_ttl_action_to_attr(a, skb);
  3191. if (err)
  3192. return err;
  3193. break;
  3194. default:
  3195. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  3196. return -EMSGSIZE;
  3197. break;
  3198. }
  3199. }
  3200. return 0;
  3201. }