flow_netlink.c 43 KB

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