flow_netlink.c 75 KB

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