flow_netlink.c 49 KB

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