flow_netlink.c 48 KB

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