flow_netlink.c 101 KB

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