flow_netlink.c 42 KB

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