flow_netlink.c 55 KB

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