flow_netlink.c 100 KB

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