flow_netlink.c 68 KB

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