flow_netlink.c 55 KB

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