flow_netlink.c 63 KB

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