flow_netlink.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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(0xff)))
  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. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  678. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
  679. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  680. return -EINVAL;
  681. }
  682. SW_FLOW_KEY_PUT(match, ipv6.label,
  683. ipv6_key->ipv6_label, is_mask);
  684. SW_FLOW_KEY_PUT(match, ip.proto,
  685. ipv6_key->ipv6_proto, is_mask);
  686. SW_FLOW_KEY_PUT(match, ip.tos,
  687. ipv6_key->ipv6_tclass, is_mask);
  688. SW_FLOW_KEY_PUT(match, ip.ttl,
  689. ipv6_key->ipv6_hlimit, is_mask);
  690. SW_FLOW_KEY_PUT(match, ip.frag,
  691. ipv6_key->ipv6_frag, is_mask);
  692. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  693. ipv6_key->ipv6_src,
  694. sizeof(match->key->ipv6.addr.src),
  695. is_mask);
  696. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  697. ipv6_key->ipv6_dst,
  698. sizeof(match->key->ipv6.addr.dst),
  699. is_mask);
  700. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  701. }
  702. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  703. const struct ovs_key_arp *arp_key;
  704. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  705. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  706. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  707. arp_key->arp_op);
  708. return -EINVAL;
  709. }
  710. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  711. arp_key->arp_sip, is_mask);
  712. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  713. arp_key->arp_tip, is_mask);
  714. SW_FLOW_KEY_PUT(match, ip.proto,
  715. ntohs(arp_key->arp_op), is_mask);
  716. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  717. arp_key->arp_sha, ETH_ALEN, is_mask);
  718. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  719. arp_key->arp_tha, ETH_ALEN, is_mask);
  720. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  721. }
  722. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  723. const struct ovs_key_mpls *mpls_key;
  724. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  725. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  726. mpls_key->mpls_lse, is_mask);
  727. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  728. }
  729. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  730. const struct ovs_key_tcp *tcp_key;
  731. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  732. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  733. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  734. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  735. }
  736. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  737. SW_FLOW_KEY_PUT(match, tp.flags,
  738. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  739. is_mask);
  740. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  741. }
  742. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  743. const struct ovs_key_udp *udp_key;
  744. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  745. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  746. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  747. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  748. }
  749. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  750. const struct ovs_key_sctp *sctp_key;
  751. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  752. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  753. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  754. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  755. }
  756. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  757. const struct ovs_key_icmp *icmp_key;
  758. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  759. SW_FLOW_KEY_PUT(match, tp.src,
  760. htons(icmp_key->icmp_type), is_mask);
  761. SW_FLOW_KEY_PUT(match, tp.dst,
  762. htons(icmp_key->icmp_code), is_mask);
  763. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  764. }
  765. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  766. const struct ovs_key_icmpv6 *icmpv6_key;
  767. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  768. SW_FLOW_KEY_PUT(match, tp.src,
  769. htons(icmpv6_key->icmpv6_type), is_mask);
  770. SW_FLOW_KEY_PUT(match, tp.dst,
  771. htons(icmpv6_key->icmpv6_code), is_mask);
  772. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  773. }
  774. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  775. const struct ovs_key_nd *nd_key;
  776. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  777. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  778. nd_key->nd_target,
  779. sizeof(match->key->ipv6.nd.target),
  780. is_mask);
  781. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  782. nd_key->nd_sll, ETH_ALEN, is_mask);
  783. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  784. nd_key->nd_tll, ETH_ALEN, is_mask);
  785. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  786. }
  787. if (attrs != 0) {
  788. OVS_NLERR(log, "Unknown key attributes %llx",
  789. (unsigned long long)attrs);
  790. return -EINVAL;
  791. }
  792. return 0;
  793. }
  794. static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
  795. {
  796. struct nlattr *nla;
  797. int rem;
  798. /* The nlattr stream should already have been validated */
  799. nla_for_each_nested(nla, attr, rem) {
  800. /* We assume that ovs_key_lens[type] == -1 means that type is a
  801. * nested attribute
  802. */
  803. if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
  804. nlattr_set(nla, val, false);
  805. else
  806. memset(nla_data(nla), val, nla_len(nla));
  807. }
  808. }
  809. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  810. {
  811. nlattr_set(attr, val, true);
  812. }
  813. /**
  814. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  815. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  816. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  817. * does not include any don't care bit.
  818. * @match: receives the extracted flow match information.
  819. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  820. * sequence. The fields should of the packet that triggered the creation
  821. * of this flow.
  822. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  823. * attribute specifies the mask field of the wildcarded flow.
  824. * @log: Boolean to allow kernel error logging. Normally true, but when
  825. * probing for feature compatibility this should be passed in as false to
  826. * suppress unnecessary error logging.
  827. */
  828. int ovs_nla_get_match(struct sw_flow_match *match,
  829. const struct nlattr *nla_key,
  830. const struct nlattr *nla_mask,
  831. bool log)
  832. {
  833. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  834. const struct nlattr *encap;
  835. struct nlattr *newmask = NULL;
  836. u64 key_attrs = 0;
  837. u64 mask_attrs = 0;
  838. bool encap_valid = false;
  839. int err;
  840. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  841. if (err)
  842. return err;
  843. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  844. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  845. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  846. __be16 tci;
  847. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  848. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  849. OVS_NLERR(log, "Invalid Vlan frame.");
  850. return -EINVAL;
  851. }
  852. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  853. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  854. encap = a[OVS_KEY_ATTR_ENCAP];
  855. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  856. encap_valid = true;
  857. if (tci & htons(VLAN_TAG_PRESENT)) {
  858. err = parse_flow_nlattrs(encap, a, &key_attrs, log);
  859. if (err)
  860. return err;
  861. } else if (!tci) {
  862. /* Corner case for truncated 802.1Q header. */
  863. if (nla_len(encap)) {
  864. OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
  865. return -EINVAL;
  866. }
  867. } else {
  868. OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
  869. return -EINVAL;
  870. }
  871. }
  872. err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
  873. if (err)
  874. return err;
  875. if (match->mask) {
  876. if (!nla_mask) {
  877. /* Create an exact match mask. We need to set to 0xff
  878. * all the 'match->mask' fields that have been touched
  879. * in 'match->key'. We cannot simply memset
  880. * 'match->mask', because padding bytes and fields not
  881. * specified in 'match->key' should be left to 0.
  882. * Instead, we use a stream of netlink attributes,
  883. * copied from 'key' and set to 0xff.
  884. * ovs_key_from_nlattrs() will take care of filling
  885. * 'match->mask' appropriately.
  886. */
  887. newmask = kmemdup(nla_key,
  888. nla_total_size(nla_len(nla_key)),
  889. GFP_KERNEL);
  890. if (!newmask)
  891. return -ENOMEM;
  892. mask_set_nlattr(newmask, 0xff);
  893. /* The userspace does not send tunnel attributes that
  894. * are 0, but we should not wildcard them nonetheless.
  895. */
  896. if (match->key->tun_key.ipv4_dst)
  897. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  898. 0xff, true);
  899. nla_mask = newmask;
  900. }
  901. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  902. if (err)
  903. goto free_newmask;
  904. /* Always match on tci. */
  905. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  906. if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
  907. __be16 eth_type = 0;
  908. __be16 tci = 0;
  909. if (!encap_valid) {
  910. OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
  911. err = -EINVAL;
  912. goto free_newmask;
  913. }
  914. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  915. if (a[OVS_KEY_ATTR_ETHERTYPE])
  916. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  917. if (eth_type == htons(0xffff)) {
  918. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  919. encap = a[OVS_KEY_ATTR_ENCAP];
  920. err = parse_flow_mask_nlattrs(encap, a,
  921. &mask_attrs, log);
  922. if (err)
  923. goto free_newmask;
  924. } else {
  925. OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
  926. ntohs(eth_type));
  927. err = -EINVAL;
  928. goto free_newmask;
  929. }
  930. if (a[OVS_KEY_ATTR_VLAN])
  931. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  932. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  933. OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
  934. ntohs(tci));
  935. err = -EINVAL;
  936. goto free_newmask;
  937. }
  938. }
  939. err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
  940. if (err)
  941. goto free_newmask;
  942. }
  943. if (!match_validate(match, key_attrs, mask_attrs, log))
  944. err = -EINVAL;
  945. free_newmask:
  946. kfree(newmask);
  947. return err;
  948. }
  949. /**
  950. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  951. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  952. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  953. * sequence.
  954. * @log: Boolean to allow kernel error logging. Normally true, but when
  955. * probing for feature compatibility this should be passed in as false to
  956. * suppress unnecessary error logging.
  957. *
  958. * This parses a series of Netlink attributes that form a flow key, which must
  959. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  960. * get the metadata, that is, the parts of the flow key that cannot be
  961. * extracted from the packet itself.
  962. */
  963. int ovs_nla_get_flow_metadata(const struct nlattr *attr,
  964. struct sw_flow_key *key,
  965. bool log)
  966. {
  967. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  968. struct sw_flow_match match;
  969. u64 attrs = 0;
  970. int err;
  971. err = parse_flow_nlattrs(attr, a, &attrs, log);
  972. if (err)
  973. return -EINVAL;
  974. memset(&match, 0, sizeof(match));
  975. match.key = key;
  976. key->phy.in_port = DP_MAX_PORTS;
  977. return metadata_from_nlattrs(&match, &attrs, a, false, log);
  978. }
  979. int ovs_nla_put_flow(const struct sw_flow_key *swkey,
  980. const struct sw_flow_key *output, struct sk_buff *skb)
  981. {
  982. struct ovs_key_ethernet *eth_key;
  983. struct nlattr *nla, *encap;
  984. bool is_mask = (swkey != output);
  985. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  986. goto nla_put_failure;
  987. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  988. goto nla_put_failure;
  989. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  990. goto nla_put_failure;
  991. if ((swkey->tun_key.ipv4_dst || is_mask)) {
  992. const struct geneve_opt *opts = NULL;
  993. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  994. opts = GENEVE_OPTS(output, swkey->tun_opts_len);
  995. if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
  996. swkey->tun_opts_len))
  997. goto nla_put_failure;
  998. }
  999. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1000. if (is_mask && (output->phy.in_port == 0xffff))
  1001. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1002. goto nla_put_failure;
  1003. } else {
  1004. u16 upper_u16;
  1005. upper_u16 = !is_mask ? 0 : 0xffff;
  1006. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1007. (upper_u16 << 16) | output->phy.in_port))
  1008. goto nla_put_failure;
  1009. }
  1010. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1011. goto nla_put_failure;
  1012. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1013. if (!nla)
  1014. goto nla_put_failure;
  1015. eth_key = nla_data(nla);
  1016. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1017. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1018. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1019. __be16 eth_type;
  1020. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1021. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1022. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1023. goto nla_put_failure;
  1024. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1025. if (!swkey->eth.tci)
  1026. goto unencap;
  1027. } else
  1028. encap = NULL;
  1029. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1030. /*
  1031. * Ethertype 802.2 is represented in the netlink with omitted
  1032. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1033. * 0xffff in the mask attribute. Ethertype can also
  1034. * be wildcarded.
  1035. */
  1036. if (is_mask && output->eth.type)
  1037. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1038. output->eth.type))
  1039. goto nla_put_failure;
  1040. goto unencap;
  1041. }
  1042. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1043. goto nla_put_failure;
  1044. if (swkey->eth.type == htons(ETH_P_IP)) {
  1045. struct ovs_key_ipv4 *ipv4_key;
  1046. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1047. if (!nla)
  1048. goto nla_put_failure;
  1049. ipv4_key = nla_data(nla);
  1050. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1051. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1052. ipv4_key->ipv4_proto = output->ip.proto;
  1053. ipv4_key->ipv4_tos = output->ip.tos;
  1054. ipv4_key->ipv4_ttl = output->ip.ttl;
  1055. ipv4_key->ipv4_frag = output->ip.frag;
  1056. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1057. struct ovs_key_ipv6 *ipv6_key;
  1058. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1059. if (!nla)
  1060. goto nla_put_failure;
  1061. ipv6_key = nla_data(nla);
  1062. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1063. sizeof(ipv6_key->ipv6_src));
  1064. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1065. sizeof(ipv6_key->ipv6_dst));
  1066. ipv6_key->ipv6_label = output->ipv6.label;
  1067. ipv6_key->ipv6_proto = output->ip.proto;
  1068. ipv6_key->ipv6_tclass = output->ip.tos;
  1069. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1070. ipv6_key->ipv6_frag = output->ip.frag;
  1071. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1072. swkey->eth.type == htons(ETH_P_RARP)) {
  1073. struct ovs_key_arp *arp_key;
  1074. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1075. if (!nla)
  1076. goto nla_put_failure;
  1077. arp_key = nla_data(nla);
  1078. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1079. arp_key->arp_sip = output->ipv4.addr.src;
  1080. arp_key->arp_tip = output->ipv4.addr.dst;
  1081. arp_key->arp_op = htons(output->ip.proto);
  1082. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1083. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1084. } else if (eth_p_mpls(swkey->eth.type)) {
  1085. struct ovs_key_mpls *mpls_key;
  1086. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1087. if (!nla)
  1088. goto nla_put_failure;
  1089. mpls_key = nla_data(nla);
  1090. mpls_key->mpls_lse = output->mpls.top_lse;
  1091. }
  1092. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1093. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1094. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1095. if (swkey->ip.proto == IPPROTO_TCP) {
  1096. struct ovs_key_tcp *tcp_key;
  1097. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1098. if (!nla)
  1099. goto nla_put_failure;
  1100. tcp_key = nla_data(nla);
  1101. tcp_key->tcp_src = output->tp.src;
  1102. tcp_key->tcp_dst = output->tp.dst;
  1103. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1104. output->tp.flags))
  1105. goto nla_put_failure;
  1106. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1107. struct ovs_key_udp *udp_key;
  1108. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1109. if (!nla)
  1110. goto nla_put_failure;
  1111. udp_key = nla_data(nla);
  1112. udp_key->udp_src = output->tp.src;
  1113. udp_key->udp_dst = output->tp.dst;
  1114. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1115. struct ovs_key_sctp *sctp_key;
  1116. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1117. if (!nla)
  1118. goto nla_put_failure;
  1119. sctp_key = nla_data(nla);
  1120. sctp_key->sctp_src = output->tp.src;
  1121. sctp_key->sctp_dst = output->tp.dst;
  1122. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1123. swkey->ip.proto == IPPROTO_ICMP) {
  1124. struct ovs_key_icmp *icmp_key;
  1125. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1126. if (!nla)
  1127. goto nla_put_failure;
  1128. icmp_key = nla_data(nla);
  1129. icmp_key->icmp_type = ntohs(output->tp.src);
  1130. icmp_key->icmp_code = ntohs(output->tp.dst);
  1131. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1132. swkey->ip.proto == IPPROTO_ICMPV6) {
  1133. struct ovs_key_icmpv6 *icmpv6_key;
  1134. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1135. sizeof(*icmpv6_key));
  1136. if (!nla)
  1137. goto nla_put_failure;
  1138. icmpv6_key = nla_data(nla);
  1139. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1140. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1141. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1142. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1143. struct ovs_key_nd *nd_key;
  1144. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1145. if (!nla)
  1146. goto nla_put_failure;
  1147. nd_key = nla_data(nla);
  1148. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1149. sizeof(nd_key->nd_target));
  1150. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1151. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1152. }
  1153. }
  1154. }
  1155. unencap:
  1156. if (encap)
  1157. nla_nest_end(skb, encap);
  1158. return 0;
  1159. nla_put_failure:
  1160. return -EMSGSIZE;
  1161. }
  1162. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1163. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1164. {
  1165. struct sw_flow_actions *sfa;
  1166. if (size > MAX_ACTIONS_BUFSIZE) {
  1167. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1168. return ERR_PTR(-EINVAL);
  1169. }
  1170. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1171. if (!sfa)
  1172. return ERR_PTR(-ENOMEM);
  1173. sfa->actions_len = 0;
  1174. return sfa;
  1175. }
  1176. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1177. * The caller must hold rcu_read_lock for this to be sensible. */
  1178. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1179. {
  1180. kfree_rcu(sf_acts, rcu);
  1181. }
  1182. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1183. int attr_len, bool log)
  1184. {
  1185. struct sw_flow_actions *acts;
  1186. int new_acts_size;
  1187. int req_size = NLA_ALIGN(attr_len);
  1188. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1189. (*sfa)->actions_len;
  1190. if (req_size <= (ksize(*sfa) - next_offset))
  1191. goto out;
  1192. new_acts_size = ksize(*sfa) * 2;
  1193. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1194. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1195. return ERR_PTR(-EMSGSIZE);
  1196. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1197. }
  1198. acts = nla_alloc_flow_actions(new_acts_size, log);
  1199. if (IS_ERR(acts))
  1200. return (void *)acts;
  1201. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1202. acts->actions_len = (*sfa)->actions_len;
  1203. kfree(*sfa);
  1204. *sfa = acts;
  1205. out:
  1206. (*sfa)->actions_len += req_size;
  1207. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1208. }
  1209. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1210. int attrtype, void *data, int len, bool log)
  1211. {
  1212. struct nlattr *a;
  1213. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1214. if (IS_ERR(a))
  1215. return a;
  1216. a->nla_type = attrtype;
  1217. a->nla_len = nla_attr_size(len);
  1218. if (data)
  1219. memcpy(nla_data(a), data, len);
  1220. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1221. return a;
  1222. }
  1223. static int add_action(struct sw_flow_actions **sfa, int attrtype,
  1224. void *data, int len, bool log)
  1225. {
  1226. struct nlattr *a;
  1227. a = __add_action(sfa, attrtype, data, len, log);
  1228. return PTR_ERR_OR_ZERO(a);
  1229. }
  1230. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1231. int attrtype, bool log)
  1232. {
  1233. int used = (*sfa)->actions_len;
  1234. int err;
  1235. err = add_action(sfa, attrtype, NULL, 0, log);
  1236. if (err)
  1237. return err;
  1238. return used;
  1239. }
  1240. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1241. int st_offset)
  1242. {
  1243. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1244. st_offset);
  1245. a->nla_len = sfa->actions_len - st_offset;
  1246. }
  1247. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1248. const struct sw_flow_key *key,
  1249. int depth, struct sw_flow_actions **sfa,
  1250. __be16 eth_type, __be16 vlan_tci, bool log);
  1251. static int validate_and_copy_sample(const struct nlattr *attr,
  1252. const struct sw_flow_key *key, int depth,
  1253. struct sw_flow_actions **sfa,
  1254. __be16 eth_type, __be16 vlan_tci, bool log)
  1255. {
  1256. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1257. const struct nlattr *probability, *actions;
  1258. const struct nlattr *a;
  1259. int rem, start, err, st_acts;
  1260. memset(attrs, 0, sizeof(attrs));
  1261. nla_for_each_nested(a, attr, rem) {
  1262. int type = nla_type(a);
  1263. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1264. return -EINVAL;
  1265. attrs[type] = a;
  1266. }
  1267. if (rem)
  1268. return -EINVAL;
  1269. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1270. if (!probability || nla_len(probability) != sizeof(u32))
  1271. return -EINVAL;
  1272. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1273. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1274. return -EINVAL;
  1275. /* validation done, copy sample action. */
  1276. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1277. if (start < 0)
  1278. return start;
  1279. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1280. nla_data(probability), sizeof(u32), log);
  1281. if (err)
  1282. return err;
  1283. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1284. if (st_acts < 0)
  1285. return st_acts;
  1286. err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
  1287. eth_type, vlan_tci, log);
  1288. if (err)
  1289. return err;
  1290. add_nested_action_end(*sfa, st_acts);
  1291. add_nested_action_end(*sfa, start);
  1292. return 0;
  1293. }
  1294. static int validate_tp_port(const struct sw_flow_key *flow_key,
  1295. __be16 eth_type)
  1296. {
  1297. if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
  1298. (flow_key->tp.src || flow_key->tp.dst))
  1299. return 0;
  1300. return -EINVAL;
  1301. }
  1302. void ovs_match_init(struct sw_flow_match *match,
  1303. struct sw_flow_key *key,
  1304. struct sw_flow_mask *mask)
  1305. {
  1306. memset(match, 0, sizeof(*match));
  1307. match->key = key;
  1308. match->mask = mask;
  1309. memset(key, 0, sizeof(*key));
  1310. if (mask) {
  1311. memset(&mask->key, 0, sizeof(mask->key));
  1312. mask->range.start = mask->range.end = 0;
  1313. }
  1314. }
  1315. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1316. struct sw_flow_actions **sfa, bool log)
  1317. {
  1318. struct sw_flow_match match;
  1319. struct sw_flow_key key;
  1320. struct ovs_tunnel_info *tun_info;
  1321. struct nlattr *a;
  1322. int err, start;
  1323. ovs_match_init(&match, &key, NULL);
  1324. err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
  1325. if (err)
  1326. return err;
  1327. if (key.tun_opts_len) {
  1328. struct geneve_opt *option = GENEVE_OPTS(&key,
  1329. key.tun_opts_len);
  1330. int opts_len = key.tun_opts_len;
  1331. bool crit_opt = false;
  1332. while (opts_len > 0) {
  1333. int len;
  1334. if (opts_len < sizeof(*option))
  1335. return -EINVAL;
  1336. len = sizeof(*option) + option->length * 4;
  1337. if (len > opts_len)
  1338. return -EINVAL;
  1339. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1340. option = (struct geneve_opt *)((u8 *)option + len);
  1341. opts_len -= len;
  1342. };
  1343. key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1344. };
  1345. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1346. if (start < 0)
  1347. return start;
  1348. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1349. sizeof(*tun_info) + key.tun_opts_len, log);
  1350. if (IS_ERR(a))
  1351. return PTR_ERR(a);
  1352. tun_info = nla_data(a);
  1353. tun_info->tunnel = key.tun_key;
  1354. tun_info->options_len = key.tun_opts_len;
  1355. if (tun_info->options_len) {
  1356. /* We need to store the options in the action itself since
  1357. * everything else will go away after flow setup. We can append
  1358. * it to tun_info and then point there.
  1359. */
  1360. memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
  1361. key.tun_opts_len);
  1362. tun_info->options = (struct geneve_opt *)(tun_info + 1);
  1363. } else {
  1364. tun_info->options = NULL;
  1365. }
  1366. add_nested_action_end(*sfa, start);
  1367. return err;
  1368. }
  1369. static int validate_set(const struct nlattr *a,
  1370. const struct sw_flow_key *flow_key,
  1371. struct sw_flow_actions **sfa,
  1372. bool *set_tun, __be16 eth_type, bool log)
  1373. {
  1374. const struct nlattr *ovs_key = nla_data(a);
  1375. int key_type = nla_type(ovs_key);
  1376. /* There can be only one key in a action */
  1377. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1378. return -EINVAL;
  1379. if (key_type > OVS_KEY_ATTR_MAX ||
  1380. (ovs_key_lens[key_type] != nla_len(ovs_key) &&
  1381. ovs_key_lens[key_type] != -1))
  1382. return -EINVAL;
  1383. switch (key_type) {
  1384. const struct ovs_key_ipv4 *ipv4_key;
  1385. const struct ovs_key_ipv6 *ipv6_key;
  1386. int err;
  1387. case OVS_KEY_ATTR_PRIORITY:
  1388. case OVS_KEY_ATTR_SKB_MARK:
  1389. case OVS_KEY_ATTR_ETHERNET:
  1390. break;
  1391. case OVS_KEY_ATTR_TUNNEL:
  1392. if (eth_p_mpls(eth_type))
  1393. return -EINVAL;
  1394. *set_tun = true;
  1395. err = validate_and_copy_set_tun(a, sfa, log);
  1396. if (err)
  1397. return err;
  1398. break;
  1399. case OVS_KEY_ATTR_IPV4:
  1400. if (eth_type != htons(ETH_P_IP))
  1401. return -EINVAL;
  1402. if (!flow_key->ip.proto)
  1403. return -EINVAL;
  1404. ipv4_key = nla_data(ovs_key);
  1405. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1406. return -EINVAL;
  1407. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1408. return -EINVAL;
  1409. break;
  1410. case OVS_KEY_ATTR_IPV6:
  1411. if (eth_type != htons(ETH_P_IPV6))
  1412. return -EINVAL;
  1413. if (!flow_key->ip.proto)
  1414. return -EINVAL;
  1415. ipv6_key = nla_data(ovs_key);
  1416. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1417. return -EINVAL;
  1418. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1419. return -EINVAL;
  1420. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1421. return -EINVAL;
  1422. break;
  1423. case OVS_KEY_ATTR_TCP:
  1424. if (flow_key->ip.proto != IPPROTO_TCP)
  1425. return -EINVAL;
  1426. return validate_tp_port(flow_key, eth_type);
  1427. case OVS_KEY_ATTR_UDP:
  1428. if (flow_key->ip.proto != IPPROTO_UDP)
  1429. return -EINVAL;
  1430. return validate_tp_port(flow_key, eth_type);
  1431. case OVS_KEY_ATTR_MPLS:
  1432. if (!eth_p_mpls(eth_type))
  1433. return -EINVAL;
  1434. break;
  1435. case OVS_KEY_ATTR_SCTP:
  1436. if (flow_key->ip.proto != IPPROTO_SCTP)
  1437. return -EINVAL;
  1438. return validate_tp_port(flow_key, eth_type);
  1439. default:
  1440. return -EINVAL;
  1441. }
  1442. return 0;
  1443. }
  1444. static int validate_userspace(const struct nlattr *attr)
  1445. {
  1446. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1447. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1448. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1449. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1450. };
  1451. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1452. int error;
  1453. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1454. attr, userspace_policy);
  1455. if (error)
  1456. return error;
  1457. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1458. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1459. return -EINVAL;
  1460. return 0;
  1461. }
  1462. static int copy_action(const struct nlattr *from,
  1463. struct sw_flow_actions **sfa, bool log)
  1464. {
  1465. int totlen = NLA_ALIGN(from->nla_len);
  1466. struct nlattr *to;
  1467. to = reserve_sfa_size(sfa, from->nla_len, log);
  1468. if (IS_ERR(to))
  1469. return PTR_ERR(to);
  1470. memcpy(to, from, totlen);
  1471. return 0;
  1472. }
  1473. static int __ovs_nla_copy_actions(const struct nlattr *attr,
  1474. const struct sw_flow_key *key,
  1475. int depth, struct sw_flow_actions **sfa,
  1476. __be16 eth_type, __be16 vlan_tci, bool log)
  1477. {
  1478. const struct nlattr *a;
  1479. int rem, err;
  1480. if (depth >= SAMPLE_ACTION_DEPTH)
  1481. return -EOVERFLOW;
  1482. nla_for_each_nested(a, attr, rem) {
  1483. /* Expected argument lengths, (u32)-1 for variable length. */
  1484. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1485. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1486. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1487. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1488. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1489. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1490. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1491. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1492. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1493. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1494. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
  1495. };
  1496. const struct ovs_action_push_vlan *vlan;
  1497. int type = nla_type(a);
  1498. bool skip_copy;
  1499. if (type > OVS_ACTION_ATTR_MAX ||
  1500. (action_lens[type] != nla_len(a) &&
  1501. action_lens[type] != (u32)-1))
  1502. return -EINVAL;
  1503. skip_copy = false;
  1504. switch (type) {
  1505. case OVS_ACTION_ATTR_UNSPEC:
  1506. return -EINVAL;
  1507. case OVS_ACTION_ATTR_USERSPACE:
  1508. err = validate_userspace(a);
  1509. if (err)
  1510. return err;
  1511. break;
  1512. case OVS_ACTION_ATTR_OUTPUT:
  1513. if (nla_get_u32(a) >= DP_MAX_PORTS)
  1514. return -EINVAL;
  1515. break;
  1516. case OVS_ACTION_ATTR_HASH: {
  1517. const struct ovs_action_hash *act_hash = nla_data(a);
  1518. switch (act_hash->hash_alg) {
  1519. case OVS_HASH_ALG_L4:
  1520. break;
  1521. default:
  1522. return -EINVAL;
  1523. }
  1524. break;
  1525. }
  1526. case OVS_ACTION_ATTR_POP_VLAN:
  1527. vlan_tci = htons(0);
  1528. break;
  1529. case OVS_ACTION_ATTR_PUSH_VLAN:
  1530. vlan = nla_data(a);
  1531. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  1532. return -EINVAL;
  1533. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  1534. return -EINVAL;
  1535. vlan_tci = vlan->vlan_tci;
  1536. break;
  1537. case OVS_ACTION_ATTR_RECIRC:
  1538. break;
  1539. case OVS_ACTION_ATTR_PUSH_MPLS: {
  1540. const struct ovs_action_push_mpls *mpls = nla_data(a);
  1541. if (!eth_p_mpls(mpls->mpls_ethertype))
  1542. return -EINVAL;
  1543. /* Prohibit push MPLS other than to a white list
  1544. * for packets that have a known tag order.
  1545. */
  1546. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1547. (eth_type != htons(ETH_P_IP) &&
  1548. eth_type != htons(ETH_P_IPV6) &&
  1549. eth_type != htons(ETH_P_ARP) &&
  1550. eth_type != htons(ETH_P_RARP) &&
  1551. !eth_p_mpls(eth_type)))
  1552. return -EINVAL;
  1553. eth_type = mpls->mpls_ethertype;
  1554. break;
  1555. }
  1556. case OVS_ACTION_ATTR_POP_MPLS:
  1557. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  1558. !eth_p_mpls(eth_type))
  1559. return -EINVAL;
  1560. /* Disallow subsequent L2.5+ set and mpls_pop actions
  1561. * as there is no check here to ensure that the new
  1562. * eth_type is valid and thus set actions could
  1563. * write off the end of the packet or otherwise
  1564. * corrupt it.
  1565. *
  1566. * Support for these actions is planned using packet
  1567. * recirculation.
  1568. */
  1569. eth_type = htons(0);
  1570. break;
  1571. case OVS_ACTION_ATTR_SET:
  1572. err = validate_set(a, key, sfa,
  1573. &skip_copy, eth_type, log);
  1574. if (err)
  1575. return err;
  1576. break;
  1577. case OVS_ACTION_ATTR_SAMPLE:
  1578. err = validate_and_copy_sample(a, key, depth, sfa,
  1579. eth_type, vlan_tci, log);
  1580. if (err)
  1581. return err;
  1582. skip_copy = true;
  1583. break;
  1584. default:
  1585. OVS_NLERR(log, "Unknown Action type %d", type);
  1586. return -EINVAL;
  1587. }
  1588. if (!skip_copy) {
  1589. err = copy_action(a, sfa, log);
  1590. if (err)
  1591. return err;
  1592. }
  1593. }
  1594. if (rem > 0)
  1595. return -EINVAL;
  1596. return 0;
  1597. }
  1598. int ovs_nla_copy_actions(const struct nlattr *attr,
  1599. const struct sw_flow_key *key,
  1600. struct sw_flow_actions **sfa, bool log)
  1601. {
  1602. int err;
  1603. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  1604. if (IS_ERR(*sfa))
  1605. return PTR_ERR(*sfa);
  1606. err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
  1607. key->eth.tci, log);
  1608. if (err)
  1609. kfree(*sfa);
  1610. return err;
  1611. }
  1612. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  1613. {
  1614. const struct nlattr *a;
  1615. struct nlattr *start;
  1616. int err = 0, rem;
  1617. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  1618. if (!start)
  1619. return -EMSGSIZE;
  1620. nla_for_each_nested(a, attr, rem) {
  1621. int type = nla_type(a);
  1622. struct nlattr *st_sample;
  1623. switch (type) {
  1624. case OVS_SAMPLE_ATTR_PROBABILITY:
  1625. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  1626. sizeof(u32), nla_data(a)))
  1627. return -EMSGSIZE;
  1628. break;
  1629. case OVS_SAMPLE_ATTR_ACTIONS:
  1630. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  1631. if (!st_sample)
  1632. return -EMSGSIZE;
  1633. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  1634. if (err)
  1635. return err;
  1636. nla_nest_end(skb, st_sample);
  1637. break;
  1638. }
  1639. }
  1640. nla_nest_end(skb, start);
  1641. return err;
  1642. }
  1643. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  1644. {
  1645. const struct nlattr *ovs_key = nla_data(a);
  1646. int key_type = nla_type(ovs_key);
  1647. struct nlattr *start;
  1648. int err;
  1649. switch (key_type) {
  1650. case OVS_KEY_ATTR_TUNNEL_INFO: {
  1651. struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
  1652. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  1653. if (!start)
  1654. return -EMSGSIZE;
  1655. err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
  1656. tun_info->options_len ?
  1657. tun_info->options : NULL,
  1658. tun_info->options_len);
  1659. if (err)
  1660. return err;
  1661. nla_nest_end(skb, start);
  1662. break;
  1663. }
  1664. default:
  1665. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  1666. return -EMSGSIZE;
  1667. break;
  1668. }
  1669. return 0;
  1670. }
  1671. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  1672. {
  1673. const struct nlattr *a;
  1674. int rem, err;
  1675. nla_for_each_attr(a, attr, len, rem) {
  1676. int type = nla_type(a);
  1677. switch (type) {
  1678. case OVS_ACTION_ATTR_SET:
  1679. err = set_action_to_attr(a, skb);
  1680. if (err)
  1681. return err;
  1682. break;
  1683. case OVS_ACTION_ATTR_SAMPLE:
  1684. err = sample_action_to_attr(a, skb);
  1685. if (err)
  1686. return err;
  1687. break;
  1688. default:
  1689. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  1690. return -EMSGSIZE;
  1691. break;
  1692. }
  1693. }
  1694. return 0;
  1695. }