flow_netlink.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  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 <net/vxlan.h>
  49. #include "flow_netlink.h"
  50. struct ovs_len_tbl {
  51. int len;
  52. const struct ovs_len_tbl *next;
  53. };
  54. #define OVS_ATTR_NESTED -1
  55. #define OVS_ATTR_VARIABLE -2
  56. static void update_range(struct sw_flow_match *match,
  57. size_t offset, size_t size, bool is_mask)
  58. {
  59. struct sw_flow_key_range *range;
  60. size_t start = rounddown(offset, sizeof(long));
  61. size_t end = roundup(offset + size, sizeof(long));
  62. if (!is_mask)
  63. range = &match->range;
  64. else
  65. range = &match->mask->range;
  66. if (range->start == range->end) {
  67. range->start = start;
  68. range->end = end;
  69. return;
  70. }
  71. if (range->start > start)
  72. range->start = start;
  73. if (range->end < end)
  74. range->end = end;
  75. }
  76. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  77. do { \
  78. update_range(match, offsetof(struct sw_flow_key, field), \
  79. sizeof((match)->key->field), is_mask); \
  80. if (is_mask) \
  81. (match)->mask->key.field = value; \
  82. else \
  83. (match)->key->field = value; \
  84. } while (0)
  85. #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
  86. do { \
  87. update_range(match, offset, len, is_mask); \
  88. if (is_mask) \
  89. memcpy((u8 *)&(match)->mask->key + offset, value_p, \
  90. len); \
  91. else \
  92. memcpy((u8 *)(match)->key + offset, value_p, len); \
  93. } while (0)
  94. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  95. SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
  96. value_p, len, is_mask)
  97. #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
  98. do { \
  99. update_range(match, offsetof(struct sw_flow_key, field), \
  100. sizeof((match)->key->field), is_mask); \
  101. if (is_mask) \
  102. memset((u8 *)&(match)->mask->key.field, value, \
  103. sizeof((match)->mask->key.field)); \
  104. else \
  105. memset((u8 *)&(match)->key->field, value, \
  106. sizeof((match)->key->field)); \
  107. } while (0)
  108. static bool match_validate(const struct sw_flow_match *match,
  109. u64 key_attrs, u64 mask_attrs, bool log)
  110. {
  111. u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
  112. u64 mask_allowed = key_attrs; /* At most allow all key attributes */
  113. /* The following mask attributes allowed only if they
  114. * pass the validation tests. */
  115. mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
  116. | (1 << OVS_KEY_ATTR_IPV6)
  117. | (1 << OVS_KEY_ATTR_TCP)
  118. | (1 << OVS_KEY_ATTR_TCP_FLAGS)
  119. | (1 << OVS_KEY_ATTR_UDP)
  120. | (1 << OVS_KEY_ATTR_SCTP)
  121. | (1 << OVS_KEY_ATTR_ICMP)
  122. | (1 << OVS_KEY_ATTR_ICMPV6)
  123. | (1 << OVS_KEY_ATTR_ARP)
  124. | (1 << OVS_KEY_ATTR_ND)
  125. | (1 << OVS_KEY_ATTR_MPLS));
  126. /* Always allowed mask fields. */
  127. mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
  128. | (1 << OVS_KEY_ATTR_IN_PORT)
  129. | (1 << OVS_KEY_ATTR_ETHERTYPE));
  130. /* Check key attributes. */
  131. if (match->key->eth.type == htons(ETH_P_ARP)
  132. || match->key->eth.type == htons(ETH_P_RARP)) {
  133. key_expected |= 1 << OVS_KEY_ATTR_ARP;
  134. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  135. mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
  136. }
  137. if (eth_p_mpls(match->key->eth.type)) {
  138. key_expected |= 1 << OVS_KEY_ATTR_MPLS;
  139. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  140. mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
  141. }
  142. if (match->key->eth.type == htons(ETH_P_IP)) {
  143. key_expected |= 1 << OVS_KEY_ATTR_IPV4;
  144. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  145. mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
  146. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  147. if (match->key->ip.proto == IPPROTO_UDP) {
  148. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  149. if (match->mask && (match->mask->key.ip.proto == 0xff))
  150. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  151. }
  152. if (match->key->ip.proto == IPPROTO_SCTP) {
  153. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  154. if (match->mask && (match->mask->key.ip.proto == 0xff))
  155. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  156. }
  157. if (match->key->ip.proto == IPPROTO_TCP) {
  158. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  159. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  160. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  161. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  162. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  163. }
  164. }
  165. if (match->key->ip.proto == IPPROTO_ICMP) {
  166. key_expected |= 1 << OVS_KEY_ATTR_ICMP;
  167. if (match->mask && (match->mask->key.ip.proto == 0xff))
  168. mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
  169. }
  170. }
  171. }
  172. if (match->key->eth.type == htons(ETH_P_IPV6)) {
  173. key_expected |= 1 << OVS_KEY_ATTR_IPV6;
  174. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  175. mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
  176. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  177. if (match->key->ip.proto == IPPROTO_UDP) {
  178. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  179. if (match->mask && (match->mask->key.ip.proto == 0xff))
  180. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  181. }
  182. if (match->key->ip.proto == IPPROTO_SCTP) {
  183. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  184. if (match->mask && (match->mask->key.ip.proto == 0xff))
  185. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  186. }
  187. if (match->key->ip.proto == IPPROTO_TCP) {
  188. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  189. key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  190. if (match->mask && (match->mask->key.ip.proto == 0xff)) {
  191. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  192. mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
  193. }
  194. }
  195. if (match->key->ip.proto == IPPROTO_ICMPV6) {
  196. key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
  197. if (match->mask && (match->mask->key.ip.proto == 0xff))
  198. mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
  199. if (match->key->tp.src ==
  200. htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  201. match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  202. key_expected |= 1 << OVS_KEY_ATTR_ND;
  203. if (match->mask && (match->mask->key.tp.src == htons(0xff)))
  204. mask_allowed |= 1 << OVS_KEY_ATTR_ND;
  205. }
  206. }
  207. }
  208. }
  209. if ((key_attrs & key_expected) != key_expected) {
  210. /* Key attributes check failed. */
  211. OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
  212. (unsigned long long)key_attrs,
  213. (unsigned long long)key_expected);
  214. return false;
  215. }
  216. if ((mask_attrs & mask_allowed) != mask_attrs) {
  217. /* Mask attributes check failed. */
  218. OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
  219. (unsigned long long)mask_attrs,
  220. (unsigned long long)mask_allowed);
  221. return false;
  222. }
  223. return true;
  224. }
  225. size_t ovs_tun_key_attr_size(void)
  226. {
  227. /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
  228. * updating this function.
  229. */
  230. return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  231. + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
  232. + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
  233. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  234. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  235. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  236. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  237. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
  238. + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
  239. /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
  240. * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
  241. */
  242. + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
  243. + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
  244. }
  245. size_t ovs_key_attr_size(void)
  246. {
  247. /* Whenever adding new OVS_KEY_ FIELDS, we should consider
  248. * updating this function.
  249. */
  250. BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
  251. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  252. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  253. + ovs_tun_key_attr_size()
  254. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  255. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  256. + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
  257. + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
  258. + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
  259. + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
  260. + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
  261. + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
  262. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  263. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  264. + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
  265. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  266. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  267. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  268. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  269. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  270. }
  271. static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
  272. [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
  273. };
  274. static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  275. [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
  276. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
  277. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
  278. [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
  279. [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
  280. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
  281. [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
  282. [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
  283. [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
  284. [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
  285. [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
  286. [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
  287. .next = ovs_vxlan_ext_key_lens },
  288. [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  289. [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  290. };
  291. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  292. static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  293. [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
  294. [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
  295. [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
  296. [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
  297. [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
  298. [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
  299. [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
  300. [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
  301. [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
  302. [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
  303. [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
  304. [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
  305. [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
  306. [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
  307. [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
  308. [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
  309. [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
  310. [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
  311. [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
  312. [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
  313. .next = ovs_tunnel_key_lens, },
  314. [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
  315. [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
  316. [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
  317. [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
  318. [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
  319. };
  320. static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
  321. {
  322. return expected_len == attr_len ||
  323. expected_len == OVS_ATTR_NESTED ||
  324. expected_len == OVS_ATTR_VARIABLE;
  325. }
  326. static bool is_all_zero(const u8 *fp, size_t size)
  327. {
  328. int i;
  329. if (!fp)
  330. return false;
  331. for (i = 0; i < size; i++)
  332. if (fp[i])
  333. return false;
  334. return true;
  335. }
  336. static int __parse_flow_nlattrs(const struct nlattr *attr,
  337. const struct nlattr *a[],
  338. u64 *attrsp, bool log, bool nz)
  339. {
  340. const struct nlattr *nla;
  341. u64 attrs;
  342. int rem;
  343. attrs = *attrsp;
  344. nla_for_each_nested(nla, attr, rem) {
  345. u16 type = nla_type(nla);
  346. int expected_len;
  347. if (type > OVS_KEY_ATTR_MAX) {
  348. OVS_NLERR(log, "Key type %d is out of range max %d",
  349. type, OVS_KEY_ATTR_MAX);
  350. return -EINVAL;
  351. }
  352. if (attrs & (1 << type)) {
  353. OVS_NLERR(log, "Duplicate key (type %d).", type);
  354. return -EINVAL;
  355. }
  356. expected_len = ovs_key_lens[type].len;
  357. if (!check_attr_len(nla_len(nla), expected_len)) {
  358. OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
  359. type, nla_len(nla), expected_len);
  360. return -EINVAL;
  361. }
  362. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  363. attrs |= 1 << type;
  364. a[type] = nla;
  365. }
  366. }
  367. if (rem) {
  368. OVS_NLERR(log, "Message has %d unknown bytes.", rem);
  369. return -EINVAL;
  370. }
  371. *attrsp = attrs;
  372. return 0;
  373. }
  374. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  375. const struct nlattr *a[], u64 *attrsp,
  376. bool log)
  377. {
  378. return __parse_flow_nlattrs(attr, a, attrsp, log, true);
  379. }
  380. static int parse_flow_nlattrs(const struct nlattr *attr,
  381. const struct nlattr *a[], u64 *attrsp,
  382. bool log)
  383. {
  384. return __parse_flow_nlattrs(attr, a, attrsp, log, false);
  385. }
  386. static int genev_tun_opt_from_nlattr(const struct nlattr *a,
  387. struct sw_flow_match *match, bool is_mask,
  388. bool log)
  389. {
  390. unsigned long opt_key_offset;
  391. if (nla_len(a) > sizeof(match->key->tun_opts)) {
  392. OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
  393. nla_len(a), sizeof(match->key->tun_opts));
  394. return -EINVAL;
  395. }
  396. if (nla_len(a) % 4 != 0) {
  397. OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
  398. nla_len(a));
  399. return -EINVAL;
  400. }
  401. /* We need to record the length of the options passed
  402. * down, otherwise packets with the same format but
  403. * additional options will be silently matched.
  404. */
  405. if (!is_mask) {
  406. SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
  407. false);
  408. } else {
  409. /* This is somewhat unusual because it looks at
  410. * both the key and mask while parsing the
  411. * attributes (and by extension assumes the key
  412. * is parsed first). Normally, we would verify
  413. * that each is the correct length and that the
  414. * attributes line up in the validate function.
  415. * However, that is difficult because this is
  416. * variable length and we won't have the
  417. * information later.
  418. */
  419. if (match->key->tun_opts_len != nla_len(a)) {
  420. OVS_NLERR(log, "Geneve option len %d != mask len %d",
  421. match->key->tun_opts_len, nla_len(a));
  422. return -EINVAL;
  423. }
  424. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  425. }
  426. opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
  427. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
  428. nla_len(a), is_mask);
  429. return 0;
  430. }
  431. static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
  432. struct sw_flow_match *match, bool is_mask,
  433. bool log)
  434. {
  435. struct nlattr *a;
  436. int rem;
  437. unsigned long opt_key_offset;
  438. struct vxlan_metadata opts;
  439. BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
  440. memset(&opts, 0, sizeof(opts));
  441. nla_for_each_nested(a, attr, rem) {
  442. int type = nla_type(a);
  443. if (type > OVS_VXLAN_EXT_MAX) {
  444. OVS_NLERR(log, "VXLAN extension %d out of range max %d",
  445. type, OVS_VXLAN_EXT_MAX);
  446. return -EINVAL;
  447. }
  448. if (!check_attr_len(nla_len(a),
  449. ovs_vxlan_ext_key_lens[type].len)) {
  450. OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
  451. type, nla_len(a),
  452. ovs_vxlan_ext_key_lens[type].len);
  453. return -EINVAL;
  454. }
  455. switch (type) {
  456. case OVS_VXLAN_EXT_GBP:
  457. opts.gbp = nla_get_u32(a);
  458. break;
  459. default:
  460. OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
  461. type);
  462. return -EINVAL;
  463. }
  464. }
  465. if (rem) {
  466. OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
  467. rem);
  468. return -EINVAL;
  469. }
  470. if (!is_mask)
  471. SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
  472. else
  473. SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
  474. opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
  475. SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
  476. is_mask);
  477. return 0;
  478. }
  479. static int ip_tun_from_nlattr(const struct nlattr *attr,
  480. struct sw_flow_match *match, bool is_mask,
  481. bool log)
  482. {
  483. bool ttl = false, ipv4 = false, ipv6 = false;
  484. __be16 tun_flags = 0;
  485. int opts_type = 0;
  486. struct nlattr *a;
  487. int rem;
  488. nla_for_each_nested(a, attr, rem) {
  489. int type = nla_type(a);
  490. int err;
  491. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  492. OVS_NLERR(log, "Tunnel attr %d out of range max %d",
  493. type, OVS_TUNNEL_KEY_ATTR_MAX);
  494. return -EINVAL;
  495. }
  496. if (!check_attr_len(nla_len(a),
  497. ovs_tunnel_key_lens[type].len)) {
  498. OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
  499. type, nla_len(a), ovs_tunnel_key_lens[type].len);
  500. return -EINVAL;
  501. }
  502. switch (type) {
  503. case OVS_TUNNEL_KEY_ATTR_ID:
  504. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  505. nla_get_be64(a), is_mask);
  506. tun_flags |= TUNNEL_KEY;
  507. break;
  508. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  509. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
  510. nla_get_in_addr(a), is_mask);
  511. ipv4 = true;
  512. break;
  513. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  514. SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
  515. nla_get_in_addr(a), is_mask);
  516. ipv4 = true;
  517. break;
  518. case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
  519. SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
  520. nla_get_in6_addr(a), is_mask);
  521. ipv6 = true;
  522. break;
  523. case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
  524. SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
  525. nla_get_in6_addr(a), is_mask);
  526. ipv6 = true;
  527. break;
  528. case OVS_TUNNEL_KEY_ATTR_TOS:
  529. SW_FLOW_KEY_PUT(match, tun_key.tos,
  530. nla_get_u8(a), is_mask);
  531. break;
  532. case OVS_TUNNEL_KEY_ATTR_TTL:
  533. SW_FLOW_KEY_PUT(match, tun_key.ttl,
  534. nla_get_u8(a), is_mask);
  535. ttl = true;
  536. break;
  537. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  538. tun_flags |= TUNNEL_DONT_FRAGMENT;
  539. break;
  540. case OVS_TUNNEL_KEY_ATTR_CSUM:
  541. tun_flags |= TUNNEL_CSUM;
  542. break;
  543. case OVS_TUNNEL_KEY_ATTR_TP_SRC:
  544. SW_FLOW_KEY_PUT(match, tun_key.tp_src,
  545. nla_get_be16(a), is_mask);
  546. break;
  547. case OVS_TUNNEL_KEY_ATTR_TP_DST:
  548. SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
  549. nla_get_be16(a), is_mask);
  550. break;
  551. case OVS_TUNNEL_KEY_ATTR_OAM:
  552. tun_flags |= TUNNEL_OAM;
  553. break;
  554. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  555. if (opts_type) {
  556. OVS_NLERR(log, "Multiple metadata blocks provided");
  557. return -EINVAL;
  558. }
  559. err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
  560. if (err)
  561. return err;
  562. tun_flags |= TUNNEL_GENEVE_OPT;
  563. opts_type = type;
  564. break;
  565. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  566. if (opts_type) {
  567. OVS_NLERR(log, "Multiple metadata blocks provided");
  568. return -EINVAL;
  569. }
  570. err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
  571. if (err)
  572. return err;
  573. tun_flags |= TUNNEL_VXLAN_OPT;
  574. opts_type = type;
  575. break;
  576. case OVS_TUNNEL_KEY_ATTR_PAD:
  577. break;
  578. default:
  579. OVS_NLERR(log, "Unknown IP tunnel attribute %d",
  580. type);
  581. return -EINVAL;
  582. }
  583. }
  584. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  585. if (is_mask)
  586. SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
  587. else
  588. SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
  589. false);
  590. if (rem > 0) {
  591. OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
  592. rem);
  593. return -EINVAL;
  594. }
  595. if (ipv4 && ipv6) {
  596. OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
  597. return -EINVAL;
  598. }
  599. if (!is_mask) {
  600. if (!ipv4 && !ipv6) {
  601. OVS_NLERR(log, "IP tunnel dst address not specified");
  602. return -EINVAL;
  603. }
  604. if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
  605. OVS_NLERR(log, "IPv4 tunnel dst address is zero");
  606. return -EINVAL;
  607. }
  608. if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
  609. OVS_NLERR(log, "IPv6 tunnel dst address is zero");
  610. return -EINVAL;
  611. }
  612. if (!ttl) {
  613. OVS_NLERR(log, "IP tunnel TTL not specified.");
  614. return -EINVAL;
  615. }
  616. }
  617. return opts_type;
  618. }
  619. static int vxlan_opt_to_nlattr(struct sk_buff *skb,
  620. const void *tun_opts, int swkey_tun_opts_len)
  621. {
  622. const struct vxlan_metadata *opts = tun_opts;
  623. struct nlattr *nla;
  624. nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
  625. if (!nla)
  626. return -EMSGSIZE;
  627. if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
  628. return -EMSGSIZE;
  629. nla_nest_end(skb, nla);
  630. return 0;
  631. }
  632. static int __ip_tun_to_nlattr(struct sk_buff *skb,
  633. const struct ip_tunnel_key *output,
  634. const void *tun_opts, int swkey_tun_opts_len,
  635. unsigned short tun_proto)
  636. {
  637. if (output->tun_flags & TUNNEL_KEY &&
  638. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
  639. OVS_TUNNEL_KEY_ATTR_PAD))
  640. return -EMSGSIZE;
  641. switch (tun_proto) {
  642. case AF_INET:
  643. if (output->u.ipv4.src &&
  644. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
  645. output->u.ipv4.src))
  646. return -EMSGSIZE;
  647. if (output->u.ipv4.dst &&
  648. nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
  649. output->u.ipv4.dst))
  650. return -EMSGSIZE;
  651. break;
  652. case AF_INET6:
  653. if (!ipv6_addr_any(&output->u.ipv6.src) &&
  654. nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
  655. &output->u.ipv6.src))
  656. return -EMSGSIZE;
  657. if (!ipv6_addr_any(&output->u.ipv6.dst) &&
  658. nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
  659. &output->u.ipv6.dst))
  660. return -EMSGSIZE;
  661. break;
  662. }
  663. if (output->tos &&
  664. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
  665. return -EMSGSIZE;
  666. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
  667. return -EMSGSIZE;
  668. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  669. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  670. return -EMSGSIZE;
  671. if ((output->tun_flags & TUNNEL_CSUM) &&
  672. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  673. return -EMSGSIZE;
  674. if (output->tp_src &&
  675. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
  676. return -EMSGSIZE;
  677. if (output->tp_dst &&
  678. nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
  679. return -EMSGSIZE;
  680. if ((output->tun_flags & TUNNEL_OAM) &&
  681. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
  682. return -EMSGSIZE;
  683. if (swkey_tun_opts_len) {
  684. if (output->tun_flags & TUNNEL_GENEVE_OPT &&
  685. nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
  686. swkey_tun_opts_len, tun_opts))
  687. return -EMSGSIZE;
  688. else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
  689. vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
  690. return -EMSGSIZE;
  691. }
  692. return 0;
  693. }
  694. static int ip_tun_to_nlattr(struct sk_buff *skb,
  695. const struct ip_tunnel_key *output,
  696. const void *tun_opts, int swkey_tun_opts_len,
  697. unsigned short tun_proto)
  698. {
  699. struct nlattr *nla;
  700. int err;
  701. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  702. if (!nla)
  703. return -EMSGSIZE;
  704. err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
  705. tun_proto);
  706. if (err)
  707. return err;
  708. nla_nest_end(skb, nla);
  709. return 0;
  710. }
  711. int ovs_nla_put_tunnel_info(struct sk_buff *skb,
  712. struct ip_tunnel_info *tun_info)
  713. {
  714. return __ip_tun_to_nlattr(skb, &tun_info->key,
  715. ip_tunnel_info_opts(tun_info),
  716. tun_info->options_len,
  717. ip_tunnel_info_af(tun_info));
  718. }
  719. static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
  720. const struct nlattr *a[],
  721. bool is_mask, bool inner)
  722. {
  723. __be16 tci = 0;
  724. __be16 tpid = 0;
  725. if (a[OVS_KEY_ATTR_VLAN])
  726. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  727. if (a[OVS_KEY_ATTR_ETHERTYPE])
  728. tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  729. if (likely(!inner)) {
  730. SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
  731. SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
  732. } else {
  733. SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
  734. SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
  735. }
  736. return 0;
  737. }
  738. static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
  739. u64 key_attrs, bool inner,
  740. const struct nlattr **a, bool log)
  741. {
  742. __be16 tci = 0;
  743. if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  744. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  745. eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
  746. /* Not a VLAN. */
  747. return 0;
  748. }
  749. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  750. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  751. OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
  752. return -EINVAL;
  753. }
  754. if (a[OVS_KEY_ATTR_VLAN])
  755. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  756. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  757. if (tci) {
  758. OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
  759. (inner) ? "C-VLAN" : "VLAN");
  760. return -EINVAL;
  761. } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
  762. /* Corner case for truncated VLAN header. */
  763. OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
  764. (inner) ? "C-VLAN" : "VLAN");
  765. return -EINVAL;
  766. }
  767. }
  768. return 1;
  769. }
  770. static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
  771. u64 key_attrs, bool inner,
  772. const struct nlattr **a, bool log)
  773. {
  774. __be16 tci = 0;
  775. __be16 tpid = 0;
  776. bool encap_valid = !!(match->key->eth.vlan.tci &
  777. htons(VLAN_TAG_PRESENT));
  778. bool i_encap_valid = !!(match->key->eth.cvlan.tci &
  779. htons(VLAN_TAG_PRESENT));
  780. if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
  781. /* Not a VLAN. */
  782. return 0;
  783. }
  784. if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
  785. OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
  786. (inner) ? "C-VLAN" : "VLAN");
  787. return -EINVAL;
  788. }
  789. if (a[OVS_KEY_ATTR_VLAN])
  790. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  791. if (a[OVS_KEY_ATTR_ETHERTYPE])
  792. tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  793. if (tpid != htons(0xffff)) {
  794. OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
  795. (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
  796. return -EINVAL;
  797. }
  798. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  799. OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
  800. (inner) ? "C-VLAN" : "VLAN");
  801. return -EINVAL;
  802. }
  803. return 1;
  804. }
  805. static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
  806. u64 *key_attrs, bool inner,
  807. const struct nlattr **a, bool is_mask,
  808. bool log)
  809. {
  810. int err;
  811. const struct nlattr *encap;
  812. if (!is_mask)
  813. err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
  814. a, log);
  815. else
  816. err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
  817. a, log);
  818. if (err <= 0)
  819. return err;
  820. err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
  821. if (err)
  822. return err;
  823. *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  824. *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  825. *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  826. encap = a[OVS_KEY_ATTR_ENCAP];
  827. if (!is_mask)
  828. err = parse_flow_nlattrs(encap, a, key_attrs, log);
  829. else
  830. err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
  831. return err;
  832. }
  833. static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
  834. u64 *key_attrs, const struct nlattr **a,
  835. bool is_mask, bool log)
  836. {
  837. int err;
  838. bool encap_valid = false;
  839. err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
  840. is_mask, log);
  841. if (err)
  842. return err;
  843. encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
  844. if (encap_valid) {
  845. err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
  846. is_mask, log);
  847. if (err)
  848. return err;
  849. }
  850. return 0;
  851. }
  852. static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
  853. u64 *attrs, const struct nlattr **a,
  854. bool is_mask, bool log)
  855. {
  856. if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
  857. u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
  858. SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
  859. *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
  860. }
  861. if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
  862. u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
  863. SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
  864. *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
  865. }
  866. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  867. SW_FLOW_KEY_PUT(match, phy.priority,
  868. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  869. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  870. }
  871. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  872. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  873. if (is_mask) {
  874. in_port = 0xffffffff; /* Always exact match in_port. */
  875. } else if (in_port >= DP_MAX_PORTS) {
  876. OVS_NLERR(log, "Port %d exceeds max allowable %d",
  877. in_port, DP_MAX_PORTS);
  878. return -EINVAL;
  879. }
  880. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  881. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  882. } else if (!is_mask) {
  883. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  884. }
  885. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  886. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  887. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  888. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  889. }
  890. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  891. if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  892. is_mask, log) < 0)
  893. return -EINVAL;
  894. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  895. }
  896. if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
  897. ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
  898. u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
  899. if (ct_state & ~CT_SUPPORTED_MASK) {
  900. OVS_NLERR(log, "ct_state flags %08x unsupported",
  901. ct_state);
  902. return -EINVAL;
  903. }
  904. SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
  905. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
  906. }
  907. if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
  908. ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
  909. u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
  910. SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
  911. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
  912. }
  913. if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
  914. ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
  915. u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
  916. SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
  917. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
  918. }
  919. if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
  920. ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
  921. const struct ovs_key_ct_labels *cl;
  922. cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
  923. SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
  924. sizeof(*cl), is_mask);
  925. *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
  926. }
  927. return 0;
  928. }
  929. static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
  930. u64 attrs, const struct nlattr **a,
  931. bool is_mask, bool log)
  932. {
  933. int err;
  934. err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
  935. if (err)
  936. return err;
  937. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  938. const struct ovs_key_ethernet *eth_key;
  939. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  940. SW_FLOW_KEY_MEMCPY(match, eth.src,
  941. eth_key->eth_src, ETH_ALEN, is_mask);
  942. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  943. eth_key->eth_dst, ETH_ALEN, is_mask);
  944. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  945. }
  946. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  947. /* VLAN attribute is always parsed before getting here since it
  948. * may occur multiple times.
  949. */
  950. OVS_NLERR(log, "VLAN attribute unexpected.");
  951. return -EINVAL;
  952. }
  953. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  954. __be16 eth_type;
  955. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  956. if (is_mask) {
  957. /* Always exact match EtherType. */
  958. eth_type = htons(0xffff);
  959. } else if (!eth_proto_is_802_3(eth_type)) {
  960. OVS_NLERR(log, "EtherType %x is less than min %x",
  961. ntohs(eth_type), ETH_P_802_3_MIN);
  962. return -EINVAL;
  963. }
  964. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  965. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  966. } else if (!is_mask) {
  967. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  968. }
  969. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  970. const struct ovs_key_ipv4 *ipv4_key;
  971. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  972. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  973. OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
  974. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  975. return -EINVAL;
  976. }
  977. SW_FLOW_KEY_PUT(match, ip.proto,
  978. ipv4_key->ipv4_proto, is_mask);
  979. SW_FLOW_KEY_PUT(match, ip.tos,
  980. ipv4_key->ipv4_tos, is_mask);
  981. SW_FLOW_KEY_PUT(match, ip.ttl,
  982. ipv4_key->ipv4_ttl, is_mask);
  983. SW_FLOW_KEY_PUT(match, ip.frag,
  984. ipv4_key->ipv4_frag, is_mask);
  985. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  986. ipv4_key->ipv4_src, is_mask);
  987. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  988. ipv4_key->ipv4_dst, is_mask);
  989. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  990. }
  991. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  992. const struct ovs_key_ipv6 *ipv6_key;
  993. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  994. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  995. OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
  996. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  997. return -EINVAL;
  998. }
  999. if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
  1000. OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
  1001. ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
  1002. return -EINVAL;
  1003. }
  1004. SW_FLOW_KEY_PUT(match, ipv6.label,
  1005. ipv6_key->ipv6_label, is_mask);
  1006. SW_FLOW_KEY_PUT(match, ip.proto,
  1007. ipv6_key->ipv6_proto, is_mask);
  1008. SW_FLOW_KEY_PUT(match, ip.tos,
  1009. ipv6_key->ipv6_tclass, is_mask);
  1010. SW_FLOW_KEY_PUT(match, ip.ttl,
  1011. ipv6_key->ipv6_hlimit, is_mask);
  1012. SW_FLOW_KEY_PUT(match, ip.frag,
  1013. ipv6_key->ipv6_frag, is_mask);
  1014. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  1015. ipv6_key->ipv6_src,
  1016. sizeof(match->key->ipv6.addr.src),
  1017. is_mask);
  1018. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  1019. ipv6_key->ipv6_dst,
  1020. sizeof(match->key->ipv6.addr.dst),
  1021. is_mask);
  1022. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1023. }
  1024. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  1025. const struct ovs_key_arp *arp_key;
  1026. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1027. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  1028. OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
  1029. arp_key->arp_op);
  1030. return -EINVAL;
  1031. }
  1032. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1033. arp_key->arp_sip, is_mask);
  1034. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1035. arp_key->arp_tip, is_mask);
  1036. SW_FLOW_KEY_PUT(match, ip.proto,
  1037. ntohs(arp_key->arp_op), is_mask);
  1038. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  1039. arp_key->arp_sha, ETH_ALEN, is_mask);
  1040. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  1041. arp_key->arp_tha, ETH_ALEN, is_mask);
  1042. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1043. }
  1044. if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
  1045. const struct ovs_key_mpls *mpls_key;
  1046. mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
  1047. SW_FLOW_KEY_PUT(match, mpls.top_lse,
  1048. mpls_key->mpls_lse, is_mask);
  1049. attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
  1050. }
  1051. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  1052. const struct ovs_key_tcp *tcp_key;
  1053. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  1054. SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
  1055. SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
  1056. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  1057. }
  1058. if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
  1059. SW_FLOW_KEY_PUT(match, tp.flags,
  1060. nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
  1061. is_mask);
  1062. attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
  1063. }
  1064. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  1065. const struct ovs_key_udp *udp_key;
  1066. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  1067. SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
  1068. SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
  1069. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  1070. }
  1071. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  1072. const struct ovs_key_sctp *sctp_key;
  1073. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  1074. SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
  1075. SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
  1076. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  1077. }
  1078. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  1079. const struct ovs_key_icmp *icmp_key;
  1080. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  1081. SW_FLOW_KEY_PUT(match, tp.src,
  1082. htons(icmp_key->icmp_type), is_mask);
  1083. SW_FLOW_KEY_PUT(match, tp.dst,
  1084. htons(icmp_key->icmp_code), is_mask);
  1085. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  1086. }
  1087. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  1088. const struct ovs_key_icmpv6 *icmpv6_key;
  1089. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  1090. SW_FLOW_KEY_PUT(match, tp.src,
  1091. htons(icmpv6_key->icmpv6_type), is_mask);
  1092. SW_FLOW_KEY_PUT(match, tp.dst,
  1093. htons(icmpv6_key->icmpv6_code), is_mask);
  1094. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  1095. }
  1096. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  1097. const struct ovs_key_nd *nd_key;
  1098. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  1099. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  1100. nd_key->nd_target,
  1101. sizeof(match->key->ipv6.nd.target),
  1102. is_mask);
  1103. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  1104. nd_key->nd_sll, ETH_ALEN, is_mask);
  1105. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  1106. nd_key->nd_tll, ETH_ALEN, is_mask);
  1107. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  1108. }
  1109. if (attrs != 0) {
  1110. OVS_NLERR(log, "Unknown key attributes %llx",
  1111. (unsigned long long)attrs);
  1112. return -EINVAL;
  1113. }
  1114. return 0;
  1115. }
  1116. static void nlattr_set(struct nlattr *attr, u8 val,
  1117. const struct ovs_len_tbl *tbl)
  1118. {
  1119. struct nlattr *nla;
  1120. int rem;
  1121. /* The nlattr stream should already have been validated */
  1122. nla_for_each_nested(nla, attr, rem) {
  1123. if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
  1124. if (tbl[nla_type(nla)].next)
  1125. tbl = tbl[nla_type(nla)].next;
  1126. nlattr_set(nla, val, tbl);
  1127. } else {
  1128. memset(nla_data(nla), val, nla_len(nla));
  1129. }
  1130. if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
  1131. *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
  1132. }
  1133. }
  1134. static void mask_set_nlattr(struct nlattr *attr, u8 val)
  1135. {
  1136. nlattr_set(attr, val, ovs_key_lens);
  1137. }
  1138. /**
  1139. * ovs_nla_get_match - parses Netlink attributes into a flow key and
  1140. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1141. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1142. * does not include any don't care bit.
  1143. * @net: Used to determine per-namespace field support.
  1144. * @match: receives the extracted flow match information.
  1145. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1146. * sequence. The fields should of the packet that triggered the creation
  1147. * of this flow.
  1148. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  1149. * attribute specifies the mask field of the wildcarded flow.
  1150. * @log: Boolean to allow kernel error logging. Normally true, but when
  1151. * probing for feature compatibility this should be passed in as false to
  1152. * suppress unnecessary error logging.
  1153. */
  1154. int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
  1155. const struct nlattr *nla_key,
  1156. const struct nlattr *nla_mask,
  1157. bool log)
  1158. {
  1159. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1160. struct nlattr *newmask = NULL;
  1161. u64 key_attrs = 0;
  1162. u64 mask_attrs = 0;
  1163. int err;
  1164. err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
  1165. if (err)
  1166. return err;
  1167. err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
  1168. if (err)
  1169. return err;
  1170. err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
  1171. if (err)
  1172. return err;
  1173. if (match->mask) {
  1174. if (!nla_mask) {
  1175. /* Create an exact match mask. We need to set to 0xff
  1176. * all the 'match->mask' fields that have been touched
  1177. * in 'match->key'. We cannot simply memset
  1178. * 'match->mask', because padding bytes and fields not
  1179. * specified in 'match->key' should be left to 0.
  1180. * Instead, we use a stream of netlink attributes,
  1181. * copied from 'key' and set to 0xff.
  1182. * ovs_key_from_nlattrs() will take care of filling
  1183. * 'match->mask' appropriately.
  1184. */
  1185. newmask = kmemdup(nla_key,
  1186. nla_total_size(nla_len(nla_key)),
  1187. GFP_KERNEL);
  1188. if (!newmask)
  1189. return -ENOMEM;
  1190. mask_set_nlattr(newmask, 0xff);
  1191. /* The userspace does not send tunnel attributes that
  1192. * are 0, but we should not wildcard them nonetheless.
  1193. */
  1194. if (match->key->tun_proto)
  1195. SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
  1196. 0xff, true);
  1197. nla_mask = newmask;
  1198. }
  1199. err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
  1200. if (err)
  1201. goto free_newmask;
  1202. /* Always match on tci. */
  1203. SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
  1204. SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
  1205. err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
  1206. if (err)
  1207. goto free_newmask;
  1208. err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
  1209. log);
  1210. if (err)
  1211. goto free_newmask;
  1212. }
  1213. if (!match_validate(match, key_attrs, mask_attrs, log))
  1214. err = -EINVAL;
  1215. free_newmask:
  1216. kfree(newmask);
  1217. return err;
  1218. }
  1219. static size_t get_ufid_len(const struct nlattr *attr, bool log)
  1220. {
  1221. size_t len;
  1222. if (!attr)
  1223. return 0;
  1224. len = nla_len(attr);
  1225. if (len < 1 || len > MAX_UFID_LENGTH) {
  1226. OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
  1227. nla_len(attr), MAX_UFID_LENGTH);
  1228. return 0;
  1229. }
  1230. return len;
  1231. }
  1232. /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
  1233. * or false otherwise.
  1234. */
  1235. bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
  1236. bool log)
  1237. {
  1238. sfid->ufid_len = get_ufid_len(attr, log);
  1239. if (sfid->ufid_len)
  1240. memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
  1241. return sfid->ufid_len;
  1242. }
  1243. int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
  1244. const struct sw_flow_key *key, bool log)
  1245. {
  1246. struct sw_flow_key *new_key;
  1247. if (ovs_nla_get_ufid(sfid, ufid, log))
  1248. return 0;
  1249. /* If UFID was not provided, use unmasked key. */
  1250. new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
  1251. if (!new_key)
  1252. return -ENOMEM;
  1253. memcpy(new_key, key, sizeof(*key));
  1254. sfid->unmasked_key = new_key;
  1255. return 0;
  1256. }
  1257. u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  1258. {
  1259. return attr ? nla_get_u32(attr) : 0;
  1260. }
  1261. /**
  1262. * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
  1263. * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  1264. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1265. * sequence.
  1266. * @log: Boolean to allow kernel error logging. Normally true, but when
  1267. * probing for feature compatibility this should be passed in as false to
  1268. * suppress unnecessary error logging.
  1269. *
  1270. * This parses a series of Netlink attributes that form a flow key, which must
  1271. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1272. * get the metadata, that is, the parts of the flow key that cannot be
  1273. * extracted from the packet itself.
  1274. */
  1275. int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
  1276. struct sw_flow_key *key,
  1277. bool log)
  1278. {
  1279. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1280. struct sw_flow_match match;
  1281. u64 attrs = 0;
  1282. int err;
  1283. err = parse_flow_nlattrs(attr, a, &attrs, log);
  1284. if (err)
  1285. return -EINVAL;
  1286. memset(&match, 0, sizeof(match));
  1287. match.key = key;
  1288. memset(&key->ct, 0, sizeof(key->ct));
  1289. key->phy.in_port = DP_MAX_PORTS;
  1290. return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
  1291. }
  1292. static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
  1293. bool is_mask)
  1294. {
  1295. __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
  1296. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1297. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
  1298. return -EMSGSIZE;
  1299. return 0;
  1300. }
  1301. static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
  1302. const struct sw_flow_key *output, bool is_mask,
  1303. struct sk_buff *skb)
  1304. {
  1305. struct ovs_key_ethernet *eth_key;
  1306. struct nlattr *nla;
  1307. struct nlattr *encap = NULL;
  1308. struct nlattr *in_encap = NULL;
  1309. if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
  1310. goto nla_put_failure;
  1311. if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
  1312. goto nla_put_failure;
  1313. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1314. goto nla_put_failure;
  1315. if ((swkey->tun_proto || is_mask)) {
  1316. const void *opts = NULL;
  1317. if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
  1318. opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
  1319. if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
  1320. swkey->tun_opts_len, swkey->tun_proto))
  1321. goto nla_put_failure;
  1322. }
  1323. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1324. if (is_mask && (output->phy.in_port == 0xffff))
  1325. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1326. goto nla_put_failure;
  1327. } else {
  1328. u16 upper_u16;
  1329. upper_u16 = !is_mask ? 0 : 0xffff;
  1330. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1331. (upper_u16 << 16) | output->phy.in_port))
  1332. goto nla_put_failure;
  1333. }
  1334. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1335. goto nla_put_failure;
  1336. if (ovs_ct_put_key(output, skb))
  1337. goto nla_put_failure;
  1338. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1339. if (!nla)
  1340. goto nla_put_failure;
  1341. eth_key = nla_data(nla);
  1342. ether_addr_copy(eth_key->eth_src, output->eth.src);
  1343. ether_addr_copy(eth_key->eth_dst, output->eth.dst);
  1344. if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
  1345. if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
  1346. goto nla_put_failure;
  1347. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1348. if (!swkey->eth.vlan.tci)
  1349. goto unencap;
  1350. if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
  1351. if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
  1352. goto nla_put_failure;
  1353. in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1354. if (!swkey->eth.cvlan.tci)
  1355. goto unencap;
  1356. }
  1357. }
  1358. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1359. /*
  1360. * Ethertype 802.2 is represented in the netlink with omitted
  1361. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1362. * 0xffff in the mask attribute. Ethertype can also
  1363. * be wildcarded.
  1364. */
  1365. if (is_mask && output->eth.type)
  1366. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1367. output->eth.type))
  1368. goto nla_put_failure;
  1369. goto unencap;
  1370. }
  1371. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1372. goto nla_put_failure;
  1373. if (eth_type_vlan(swkey->eth.type)) {
  1374. /* There are 3 VLAN tags, we don't know anything about the rest
  1375. * of the packet, so truncate here.
  1376. */
  1377. WARN_ON_ONCE(!(encap && in_encap));
  1378. goto unencap;
  1379. }
  1380. if (swkey->eth.type == htons(ETH_P_IP)) {
  1381. struct ovs_key_ipv4 *ipv4_key;
  1382. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1383. if (!nla)
  1384. goto nla_put_failure;
  1385. ipv4_key = nla_data(nla);
  1386. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1387. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1388. ipv4_key->ipv4_proto = output->ip.proto;
  1389. ipv4_key->ipv4_tos = output->ip.tos;
  1390. ipv4_key->ipv4_ttl = output->ip.ttl;
  1391. ipv4_key->ipv4_frag = output->ip.frag;
  1392. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1393. struct ovs_key_ipv6 *ipv6_key;
  1394. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1395. if (!nla)
  1396. goto nla_put_failure;
  1397. ipv6_key = nla_data(nla);
  1398. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1399. sizeof(ipv6_key->ipv6_src));
  1400. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1401. sizeof(ipv6_key->ipv6_dst));
  1402. ipv6_key->ipv6_label = output->ipv6.label;
  1403. ipv6_key->ipv6_proto = output->ip.proto;
  1404. ipv6_key->ipv6_tclass = output->ip.tos;
  1405. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1406. ipv6_key->ipv6_frag = output->ip.frag;
  1407. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1408. swkey->eth.type == htons(ETH_P_RARP)) {
  1409. struct ovs_key_arp *arp_key;
  1410. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1411. if (!nla)
  1412. goto nla_put_failure;
  1413. arp_key = nla_data(nla);
  1414. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1415. arp_key->arp_sip = output->ipv4.addr.src;
  1416. arp_key->arp_tip = output->ipv4.addr.dst;
  1417. arp_key->arp_op = htons(output->ip.proto);
  1418. ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
  1419. ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
  1420. } else if (eth_p_mpls(swkey->eth.type)) {
  1421. struct ovs_key_mpls *mpls_key;
  1422. nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
  1423. if (!nla)
  1424. goto nla_put_failure;
  1425. mpls_key = nla_data(nla);
  1426. mpls_key->mpls_lse = output->mpls.top_lse;
  1427. }
  1428. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1429. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1430. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1431. if (swkey->ip.proto == IPPROTO_TCP) {
  1432. struct ovs_key_tcp *tcp_key;
  1433. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1434. if (!nla)
  1435. goto nla_put_failure;
  1436. tcp_key = nla_data(nla);
  1437. tcp_key->tcp_src = output->tp.src;
  1438. tcp_key->tcp_dst = output->tp.dst;
  1439. if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
  1440. output->tp.flags))
  1441. goto nla_put_failure;
  1442. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1443. struct ovs_key_udp *udp_key;
  1444. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1445. if (!nla)
  1446. goto nla_put_failure;
  1447. udp_key = nla_data(nla);
  1448. udp_key->udp_src = output->tp.src;
  1449. udp_key->udp_dst = output->tp.dst;
  1450. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1451. struct ovs_key_sctp *sctp_key;
  1452. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1453. if (!nla)
  1454. goto nla_put_failure;
  1455. sctp_key = nla_data(nla);
  1456. sctp_key->sctp_src = output->tp.src;
  1457. sctp_key->sctp_dst = output->tp.dst;
  1458. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1459. swkey->ip.proto == IPPROTO_ICMP) {
  1460. struct ovs_key_icmp *icmp_key;
  1461. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1462. if (!nla)
  1463. goto nla_put_failure;
  1464. icmp_key = nla_data(nla);
  1465. icmp_key->icmp_type = ntohs(output->tp.src);
  1466. icmp_key->icmp_code = ntohs(output->tp.dst);
  1467. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1468. swkey->ip.proto == IPPROTO_ICMPV6) {
  1469. struct ovs_key_icmpv6 *icmpv6_key;
  1470. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1471. sizeof(*icmpv6_key));
  1472. if (!nla)
  1473. goto nla_put_failure;
  1474. icmpv6_key = nla_data(nla);
  1475. icmpv6_key->icmpv6_type = ntohs(output->tp.src);
  1476. icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
  1477. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1478. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1479. struct ovs_key_nd *nd_key;
  1480. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1481. if (!nla)
  1482. goto nla_put_failure;
  1483. nd_key = nla_data(nla);
  1484. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1485. sizeof(nd_key->nd_target));
  1486. ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
  1487. ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
  1488. }
  1489. }
  1490. }
  1491. unencap:
  1492. if (in_encap)
  1493. nla_nest_end(skb, in_encap);
  1494. if (encap)
  1495. nla_nest_end(skb, encap);
  1496. return 0;
  1497. nla_put_failure:
  1498. return -EMSGSIZE;
  1499. }
  1500. int ovs_nla_put_key(const struct sw_flow_key *swkey,
  1501. const struct sw_flow_key *output, int attr, bool is_mask,
  1502. struct sk_buff *skb)
  1503. {
  1504. int err;
  1505. struct nlattr *nla;
  1506. nla = nla_nest_start(skb, attr);
  1507. if (!nla)
  1508. return -EMSGSIZE;
  1509. err = __ovs_nla_put_key(swkey, output, is_mask, skb);
  1510. if (err)
  1511. return err;
  1512. nla_nest_end(skb, nla);
  1513. return 0;
  1514. }
  1515. /* Called with ovs_mutex or RCU read lock. */
  1516. int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
  1517. {
  1518. if (ovs_identifier_is_ufid(&flow->id))
  1519. return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
  1520. flow->id.ufid);
  1521. return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
  1522. OVS_FLOW_ATTR_KEY, false, skb);
  1523. }
  1524. /* Called with ovs_mutex or RCU read lock. */
  1525. int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
  1526. {
  1527. return ovs_nla_put_key(&flow->key, &flow->key,
  1528. OVS_FLOW_ATTR_KEY, false, skb);
  1529. }
  1530. /* Called with ovs_mutex or RCU read lock. */
  1531. int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
  1532. {
  1533. return ovs_nla_put_key(&flow->key, &flow->mask->key,
  1534. OVS_FLOW_ATTR_MASK, true, skb);
  1535. }
  1536. #define MAX_ACTIONS_BUFSIZE (32 * 1024)
  1537. static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
  1538. {
  1539. struct sw_flow_actions *sfa;
  1540. if (size > MAX_ACTIONS_BUFSIZE) {
  1541. OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
  1542. return ERR_PTR(-EINVAL);
  1543. }
  1544. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  1545. if (!sfa)
  1546. return ERR_PTR(-ENOMEM);
  1547. sfa->actions_len = 0;
  1548. return sfa;
  1549. }
  1550. static void ovs_nla_free_set_action(const struct nlattr *a)
  1551. {
  1552. const struct nlattr *ovs_key = nla_data(a);
  1553. struct ovs_tunnel_info *ovs_tun;
  1554. switch (nla_type(ovs_key)) {
  1555. case OVS_KEY_ATTR_TUNNEL_INFO:
  1556. ovs_tun = nla_data(ovs_key);
  1557. dst_release((struct dst_entry *)ovs_tun->tun_dst);
  1558. break;
  1559. }
  1560. }
  1561. void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
  1562. {
  1563. const struct nlattr *a;
  1564. int rem;
  1565. if (!sf_acts)
  1566. return;
  1567. nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
  1568. switch (nla_type(a)) {
  1569. case OVS_ACTION_ATTR_SET:
  1570. ovs_nla_free_set_action(a);
  1571. break;
  1572. case OVS_ACTION_ATTR_CT:
  1573. ovs_ct_free_action(a);
  1574. break;
  1575. }
  1576. }
  1577. kfree(sf_acts);
  1578. }
  1579. static void __ovs_nla_free_flow_actions(struct rcu_head *head)
  1580. {
  1581. ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
  1582. }
  1583. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  1584. * The caller must hold rcu_read_lock for this to be sensible. */
  1585. void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
  1586. {
  1587. call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
  1588. }
  1589. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
  1590. int attr_len, bool log)
  1591. {
  1592. struct sw_flow_actions *acts;
  1593. int new_acts_size;
  1594. int req_size = NLA_ALIGN(attr_len);
  1595. int next_offset = offsetof(struct sw_flow_actions, actions) +
  1596. (*sfa)->actions_len;
  1597. if (req_size <= (ksize(*sfa) - next_offset))
  1598. goto out;
  1599. new_acts_size = ksize(*sfa) * 2;
  1600. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  1601. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  1602. return ERR_PTR(-EMSGSIZE);
  1603. new_acts_size = MAX_ACTIONS_BUFSIZE;
  1604. }
  1605. acts = nla_alloc_flow_actions(new_acts_size, log);
  1606. if (IS_ERR(acts))
  1607. return (void *)acts;
  1608. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  1609. acts->actions_len = (*sfa)->actions_len;
  1610. acts->orig_len = (*sfa)->orig_len;
  1611. kfree(*sfa);
  1612. *sfa = acts;
  1613. out:
  1614. (*sfa)->actions_len += req_size;
  1615. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  1616. }
  1617. static struct nlattr *__add_action(struct sw_flow_actions **sfa,
  1618. int attrtype, void *data, int len, bool log)
  1619. {
  1620. struct nlattr *a;
  1621. a = reserve_sfa_size(sfa, nla_attr_size(len), log);
  1622. if (IS_ERR(a))
  1623. return a;
  1624. a->nla_type = attrtype;
  1625. a->nla_len = nla_attr_size(len);
  1626. if (data)
  1627. memcpy(nla_data(a), data, len);
  1628. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  1629. return a;
  1630. }
  1631. int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
  1632. int len, bool log)
  1633. {
  1634. struct nlattr *a;
  1635. a = __add_action(sfa, attrtype, data, len, log);
  1636. return PTR_ERR_OR_ZERO(a);
  1637. }
  1638. static inline int add_nested_action_start(struct sw_flow_actions **sfa,
  1639. int attrtype, bool log)
  1640. {
  1641. int used = (*sfa)->actions_len;
  1642. int err;
  1643. err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
  1644. if (err)
  1645. return err;
  1646. return used;
  1647. }
  1648. static inline void add_nested_action_end(struct sw_flow_actions *sfa,
  1649. int st_offset)
  1650. {
  1651. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
  1652. st_offset);
  1653. a->nla_len = sfa->actions_len - st_offset;
  1654. }
  1655. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1656. const struct sw_flow_key *key,
  1657. int depth, struct sw_flow_actions **sfa,
  1658. __be16 eth_type, __be16 vlan_tci, bool log);
  1659. static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
  1660. const struct sw_flow_key *key, int depth,
  1661. struct sw_flow_actions **sfa,
  1662. __be16 eth_type, __be16 vlan_tci, bool log)
  1663. {
  1664. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  1665. const struct nlattr *probability, *actions;
  1666. const struct nlattr *a;
  1667. int rem, start, err, st_acts;
  1668. memset(attrs, 0, sizeof(attrs));
  1669. nla_for_each_nested(a, attr, rem) {
  1670. int type = nla_type(a);
  1671. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  1672. return -EINVAL;
  1673. attrs[type] = a;
  1674. }
  1675. if (rem)
  1676. return -EINVAL;
  1677. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  1678. if (!probability || nla_len(probability) != sizeof(u32))
  1679. return -EINVAL;
  1680. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  1681. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  1682. return -EINVAL;
  1683. /* validation done, copy sample action. */
  1684. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
  1685. if (start < 0)
  1686. return start;
  1687. err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
  1688. nla_data(probability), sizeof(u32), log);
  1689. if (err)
  1690. return err;
  1691. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
  1692. if (st_acts < 0)
  1693. return st_acts;
  1694. err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
  1695. eth_type, vlan_tci, log);
  1696. if (err)
  1697. return err;
  1698. add_nested_action_end(*sfa, st_acts);
  1699. add_nested_action_end(*sfa, start);
  1700. return 0;
  1701. }
  1702. void ovs_match_init(struct sw_flow_match *match,
  1703. struct sw_flow_key *key,
  1704. bool reset_key,
  1705. struct sw_flow_mask *mask)
  1706. {
  1707. memset(match, 0, sizeof(*match));
  1708. match->key = key;
  1709. match->mask = mask;
  1710. if (reset_key)
  1711. memset(key, 0, sizeof(*key));
  1712. if (mask) {
  1713. memset(&mask->key, 0, sizeof(mask->key));
  1714. mask->range.start = mask->range.end = 0;
  1715. }
  1716. }
  1717. static int validate_geneve_opts(struct sw_flow_key *key)
  1718. {
  1719. struct geneve_opt *option;
  1720. int opts_len = key->tun_opts_len;
  1721. bool crit_opt = false;
  1722. option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
  1723. while (opts_len > 0) {
  1724. int len;
  1725. if (opts_len < sizeof(*option))
  1726. return -EINVAL;
  1727. len = sizeof(*option) + option->length * 4;
  1728. if (len > opts_len)
  1729. return -EINVAL;
  1730. crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
  1731. option = (struct geneve_opt *)((u8 *)option + len);
  1732. opts_len -= len;
  1733. };
  1734. key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
  1735. return 0;
  1736. }
  1737. static int validate_and_copy_set_tun(const struct nlattr *attr,
  1738. struct sw_flow_actions **sfa, bool log)
  1739. {
  1740. struct sw_flow_match match;
  1741. struct sw_flow_key key;
  1742. struct metadata_dst *tun_dst;
  1743. struct ip_tunnel_info *tun_info;
  1744. struct ovs_tunnel_info *ovs_tun;
  1745. struct nlattr *a;
  1746. int err = 0, start, opts_type;
  1747. ovs_match_init(&match, &key, true, NULL);
  1748. opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
  1749. if (opts_type < 0)
  1750. return opts_type;
  1751. if (key.tun_opts_len) {
  1752. switch (opts_type) {
  1753. case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
  1754. err = validate_geneve_opts(&key);
  1755. if (err < 0)
  1756. return err;
  1757. break;
  1758. case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
  1759. break;
  1760. }
  1761. };
  1762. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
  1763. if (start < 0)
  1764. return start;
  1765. tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
  1766. if (!tun_dst)
  1767. return -ENOMEM;
  1768. err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
  1769. if (err) {
  1770. dst_release((struct dst_entry *)tun_dst);
  1771. return err;
  1772. }
  1773. a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
  1774. sizeof(*ovs_tun), log);
  1775. if (IS_ERR(a)) {
  1776. dst_release((struct dst_entry *)tun_dst);
  1777. return PTR_ERR(a);
  1778. }
  1779. ovs_tun = nla_data(a);
  1780. ovs_tun->tun_dst = tun_dst;
  1781. tun_info = &tun_dst->u.tun_info;
  1782. tun_info->mode = IP_TUNNEL_INFO_TX;
  1783. if (key.tun_proto == AF_INET6)
  1784. tun_info->mode |= IP_TUNNEL_INFO_IPV6;
  1785. tun_info->key = key.tun_key;
  1786. /* We need to store the options in the action itself since
  1787. * everything else will go away after flow setup. We can append
  1788. * it to tun_info and then point there.
  1789. */
  1790. ip_tunnel_info_opts_set(tun_info,
  1791. TUN_METADATA_OPTS(&key, key.tun_opts_len),
  1792. key.tun_opts_len);
  1793. add_nested_action_end(*sfa, start);
  1794. return err;
  1795. }
  1796. /* Return false if there are any non-masked bits set.
  1797. * Mask follows data immediately, before any netlink padding.
  1798. */
  1799. static bool validate_masked(u8 *data, int len)
  1800. {
  1801. u8 *mask = data + len;
  1802. while (len--)
  1803. if (*data++ & ~*mask++)
  1804. return false;
  1805. return true;
  1806. }
  1807. static int validate_set(const struct nlattr *a,
  1808. const struct sw_flow_key *flow_key,
  1809. struct sw_flow_actions **sfa,
  1810. bool *skip_copy, __be16 eth_type, bool masked, bool log)
  1811. {
  1812. const struct nlattr *ovs_key = nla_data(a);
  1813. int key_type = nla_type(ovs_key);
  1814. size_t key_len;
  1815. /* There can be only one key in a action */
  1816. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  1817. return -EINVAL;
  1818. key_len = nla_len(ovs_key);
  1819. if (masked)
  1820. key_len /= 2;
  1821. if (key_type > OVS_KEY_ATTR_MAX ||
  1822. !check_attr_len(key_len, ovs_key_lens[key_type].len))
  1823. return -EINVAL;
  1824. if (masked && !validate_masked(nla_data(ovs_key), key_len))
  1825. return -EINVAL;
  1826. switch (key_type) {
  1827. const struct ovs_key_ipv4 *ipv4_key;
  1828. const struct ovs_key_ipv6 *ipv6_key;
  1829. int err;
  1830. case OVS_KEY_ATTR_PRIORITY:
  1831. case OVS_KEY_ATTR_SKB_MARK:
  1832. case OVS_KEY_ATTR_CT_MARK:
  1833. case OVS_KEY_ATTR_CT_LABELS:
  1834. case OVS_KEY_ATTR_ETHERNET:
  1835. break;
  1836. case OVS_KEY_ATTR_TUNNEL:
  1837. if (masked)
  1838. return -EINVAL; /* Masked tunnel set not supported. */
  1839. *skip_copy = true;
  1840. err = validate_and_copy_set_tun(a, sfa, log);
  1841. if (err)
  1842. return err;
  1843. break;
  1844. case OVS_KEY_ATTR_IPV4:
  1845. if (eth_type != htons(ETH_P_IP))
  1846. return -EINVAL;
  1847. ipv4_key = nla_data(ovs_key);
  1848. if (masked) {
  1849. const struct ovs_key_ipv4 *mask = ipv4_key + 1;
  1850. /* Non-writeable fields. */
  1851. if (mask->ipv4_proto || mask->ipv4_frag)
  1852. return -EINVAL;
  1853. } else {
  1854. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  1855. return -EINVAL;
  1856. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  1857. return -EINVAL;
  1858. }
  1859. break;
  1860. case OVS_KEY_ATTR_IPV6:
  1861. if (eth_type != htons(ETH_P_IPV6))
  1862. return -EINVAL;
  1863. ipv6_key = nla_data(ovs_key);
  1864. if (masked) {
  1865. const struct ovs_key_ipv6 *mask = ipv6_key + 1;
  1866. /* Non-writeable fields. */
  1867. if (mask->ipv6_proto || mask->ipv6_frag)
  1868. return -EINVAL;
  1869. /* Invalid bits in the flow label mask? */
  1870. if (ntohl(mask->ipv6_label) & 0xFFF00000)
  1871. return -EINVAL;
  1872. } else {
  1873. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  1874. return -EINVAL;
  1875. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  1876. return -EINVAL;
  1877. }
  1878. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  1879. return -EINVAL;
  1880. break;
  1881. case OVS_KEY_ATTR_TCP:
  1882. if ((eth_type != htons(ETH_P_IP) &&
  1883. eth_type != htons(ETH_P_IPV6)) ||
  1884. flow_key->ip.proto != IPPROTO_TCP)
  1885. return -EINVAL;
  1886. break;
  1887. case OVS_KEY_ATTR_UDP:
  1888. if ((eth_type != htons(ETH_P_IP) &&
  1889. eth_type != htons(ETH_P_IPV6)) ||
  1890. flow_key->ip.proto != IPPROTO_UDP)
  1891. return -EINVAL;
  1892. break;
  1893. case OVS_KEY_ATTR_MPLS:
  1894. if (!eth_p_mpls(eth_type))
  1895. return -EINVAL;
  1896. break;
  1897. case OVS_KEY_ATTR_SCTP:
  1898. if ((eth_type != htons(ETH_P_IP) &&
  1899. eth_type != htons(ETH_P_IPV6)) ||
  1900. flow_key->ip.proto != IPPROTO_SCTP)
  1901. return -EINVAL;
  1902. break;
  1903. default:
  1904. return -EINVAL;
  1905. }
  1906. /* Convert non-masked non-tunnel set actions to masked set actions. */
  1907. if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
  1908. int start, len = key_len * 2;
  1909. struct nlattr *at;
  1910. *skip_copy = true;
  1911. start = add_nested_action_start(sfa,
  1912. OVS_ACTION_ATTR_SET_TO_MASKED,
  1913. log);
  1914. if (start < 0)
  1915. return start;
  1916. at = __add_action(sfa, key_type, NULL, len, log);
  1917. if (IS_ERR(at))
  1918. return PTR_ERR(at);
  1919. memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
  1920. memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
  1921. /* Clear non-writeable bits from otherwise writeable fields. */
  1922. if (key_type == OVS_KEY_ATTR_IPV6) {
  1923. struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
  1924. mask->ipv6_label &= htonl(0x000FFFFF);
  1925. }
  1926. add_nested_action_end(*sfa, start);
  1927. }
  1928. return 0;
  1929. }
  1930. static int validate_userspace(const struct nlattr *attr)
  1931. {
  1932. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  1933. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  1934. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  1935. [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
  1936. };
  1937. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  1938. int error;
  1939. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  1940. attr, userspace_policy);
  1941. if (error)
  1942. return error;
  1943. if (!a[OVS_USERSPACE_ATTR_PID] ||
  1944. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  1945. return -EINVAL;
  1946. return 0;
  1947. }
  1948. static int copy_action(const struct nlattr *from,
  1949. struct sw_flow_actions **sfa, bool log)
  1950. {
  1951. int totlen = NLA_ALIGN(from->nla_len);
  1952. struct nlattr *to;
  1953. to = reserve_sfa_size(sfa, from->nla_len, log);
  1954. if (IS_ERR(to))
  1955. return PTR_ERR(to);
  1956. memcpy(to, from, totlen);
  1957. return 0;
  1958. }
  1959. static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  1960. const struct sw_flow_key *key,
  1961. int depth, struct sw_flow_actions **sfa,
  1962. __be16 eth_type, __be16 vlan_tci, bool log)
  1963. {
  1964. const struct nlattr *a;
  1965. int rem, err;
  1966. if (depth >= SAMPLE_ACTION_DEPTH)
  1967. return -EOVERFLOW;
  1968. nla_for_each_nested(a, attr, rem) {
  1969. /* Expected argument lengths, (u32)-1 for variable length. */
  1970. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  1971. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  1972. [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
  1973. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  1974. [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
  1975. [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
  1976. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  1977. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  1978. [OVS_ACTION_ATTR_SET] = (u32)-1,
  1979. [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
  1980. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
  1981. [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
  1982. [OVS_ACTION_ATTR_CT] = (u32)-1,
  1983. [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
  1984. };
  1985. const struct ovs_action_push_vlan *vlan;
  1986. int type = nla_type(a);
  1987. bool skip_copy;
  1988. if (type > OVS_ACTION_ATTR_MAX ||
  1989. (action_lens[type] != nla_len(a) &&
  1990. action_lens[type] != (u32)-1))
  1991. return -EINVAL;
  1992. skip_copy = false;
  1993. switch (type) {
  1994. case OVS_ACTION_ATTR_UNSPEC:
  1995. return -EINVAL;
  1996. case OVS_ACTION_ATTR_USERSPACE:
  1997. err = validate_userspace(a);
  1998. if (err)
  1999. return err;
  2000. break;
  2001. case OVS_ACTION_ATTR_OUTPUT:
  2002. if (nla_get_u32(a) >= DP_MAX_PORTS)
  2003. return -EINVAL;
  2004. break;
  2005. case OVS_ACTION_ATTR_TRUNC: {
  2006. const struct ovs_action_trunc *trunc = nla_data(a);
  2007. if (trunc->max_len < ETH_HLEN)
  2008. return -EINVAL;
  2009. break;
  2010. }
  2011. case OVS_ACTION_ATTR_HASH: {
  2012. const struct ovs_action_hash *act_hash = nla_data(a);
  2013. switch (act_hash->hash_alg) {
  2014. case OVS_HASH_ALG_L4:
  2015. break;
  2016. default:
  2017. return -EINVAL;
  2018. }
  2019. break;
  2020. }
  2021. case OVS_ACTION_ATTR_POP_VLAN:
  2022. vlan_tci = htons(0);
  2023. break;
  2024. case OVS_ACTION_ATTR_PUSH_VLAN:
  2025. vlan = nla_data(a);
  2026. if (!eth_type_vlan(vlan->vlan_tpid))
  2027. return -EINVAL;
  2028. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  2029. return -EINVAL;
  2030. vlan_tci = vlan->vlan_tci;
  2031. break;
  2032. case OVS_ACTION_ATTR_RECIRC:
  2033. break;
  2034. case OVS_ACTION_ATTR_PUSH_MPLS: {
  2035. const struct ovs_action_push_mpls *mpls = nla_data(a);
  2036. if (!eth_p_mpls(mpls->mpls_ethertype))
  2037. return -EINVAL;
  2038. /* Prohibit push MPLS other than to a white list
  2039. * for packets that have a known tag order.
  2040. */
  2041. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  2042. (eth_type != htons(ETH_P_IP) &&
  2043. eth_type != htons(ETH_P_IPV6) &&
  2044. eth_type != htons(ETH_P_ARP) &&
  2045. eth_type != htons(ETH_P_RARP) &&
  2046. !eth_p_mpls(eth_type)))
  2047. return -EINVAL;
  2048. eth_type = mpls->mpls_ethertype;
  2049. break;
  2050. }
  2051. case OVS_ACTION_ATTR_POP_MPLS:
  2052. if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
  2053. !eth_p_mpls(eth_type))
  2054. return -EINVAL;
  2055. /* Disallow subsequent L2.5+ set and mpls_pop actions
  2056. * as there is no check here to ensure that the new
  2057. * eth_type is valid and thus set actions could
  2058. * write off the end of the packet or otherwise
  2059. * corrupt it.
  2060. *
  2061. * Support for these actions is planned using packet
  2062. * recirculation.
  2063. */
  2064. eth_type = htons(0);
  2065. break;
  2066. case OVS_ACTION_ATTR_SET:
  2067. err = validate_set(a, key, sfa,
  2068. &skip_copy, eth_type, false, log);
  2069. if (err)
  2070. return err;
  2071. break;
  2072. case OVS_ACTION_ATTR_SET_MASKED:
  2073. err = validate_set(a, key, sfa,
  2074. &skip_copy, eth_type, true, log);
  2075. if (err)
  2076. return err;
  2077. break;
  2078. case OVS_ACTION_ATTR_SAMPLE:
  2079. err = validate_and_copy_sample(net, a, key, depth, sfa,
  2080. eth_type, vlan_tci, log);
  2081. if (err)
  2082. return err;
  2083. skip_copy = true;
  2084. break;
  2085. case OVS_ACTION_ATTR_CT:
  2086. err = ovs_ct_copy_action(net, a, key, sfa, log);
  2087. if (err)
  2088. return err;
  2089. skip_copy = true;
  2090. break;
  2091. default:
  2092. OVS_NLERR(log, "Unknown Action type %d", type);
  2093. return -EINVAL;
  2094. }
  2095. if (!skip_copy) {
  2096. err = copy_action(a, sfa, log);
  2097. if (err)
  2098. return err;
  2099. }
  2100. }
  2101. if (rem > 0)
  2102. return -EINVAL;
  2103. return 0;
  2104. }
  2105. /* 'key' must be the masked key. */
  2106. int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
  2107. const struct sw_flow_key *key,
  2108. struct sw_flow_actions **sfa, bool log)
  2109. {
  2110. int err;
  2111. *sfa = nla_alloc_flow_actions(nla_len(attr), log);
  2112. if (IS_ERR(*sfa))
  2113. return PTR_ERR(*sfa);
  2114. (*sfa)->orig_len = nla_len(attr);
  2115. err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
  2116. key->eth.vlan.tci, log);
  2117. if (err)
  2118. ovs_nla_free_flow_actions(*sfa);
  2119. return err;
  2120. }
  2121. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  2122. {
  2123. const struct nlattr *a;
  2124. struct nlattr *start;
  2125. int err = 0, rem;
  2126. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  2127. if (!start)
  2128. return -EMSGSIZE;
  2129. nla_for_each_nested(a, attr, rem) {
  2130. int type = nla_type(a);
  2131. struct nlattr *st_sample;
  2132. switch (type) {
  2133. case OVS_SAMPLE_ATTR_PROBABILITY:
  2134. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
  2135. sizeof(u32), nla_data(a)))
  2136. return -EMSGSIZE;
  2137. break;
  2138. case OVS_SAMPLE_ATTR_ACTIONS:
  2139. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  2140. if (!st_sample)
  2141. return -EMSGSIZE;
  2142. err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
  2143. if (err)
  2144. return err;
  2145. nla_nest_end(skb, st_sample);
  2146. break;
  2147. }
  2148. }
  2149. nla_nest_end(skb, start);
  2150. return err;
  2151. }
  2152. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  2153. {
  2154. const struct nlattr *ovs_key = nla_data(a);
  2155. int key_type = nla_type(ovs_key);
  2156. struct nlattr *start;
  2157. int err;
  2158. switch (key_type) {
  2159. case OVS_KEY_ATTR_TUNNEL_INFO: {
  2160. struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
  2161. struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
  2162. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2163. if (!start)
  2164. return -EMSGSIZE;
  2165. err = ip_tun_to_nlattr(skb, &tun_info->key,
  2166. ip_tunnel_info_opts(tun_info),
  2167. tun_info->options_len,
  2168. ip_tunnel_info_af(tun_info));
  2169. if (err)
  2170. return err;
  2171. nla_nest_end(skb, start);
  2172. break;
  2173. }
  2174. default:
  2175. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  2176. return -EMSGSIZE;
  2177. break;
  2178. }
  2179. return 0;
  2180. }
  2181. static int masked_set_action_to_set_action_attr(const struct nlattr *a,
  2182. struct sk_buff *skb)
  2183. {
  2184. const struct nlattr *ovs_key = nla_data(a);
  2185. struct nlattr *nla;
  2186. size_t key_len = nla_len(ovs_key) / 2;
  2187. /* Revert the conversion we did from a non-masked set action to
  2188. * masked set action.
  2189. */
  2190. nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  2191. if (!nla)
  2192. return -EMSGSIZE;
  2193. if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
  2194. return -EMSGSIZE;
  2195. nla_nest_end(skb, nla);
  2196. return 0;
  2197. }
  2198. int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
  2199. {
  2200. const struct nlattr *a;
  2201. int rem, err;
  2202. nla_for_each_attr(a, attr, len, rem) {
  2203. int type = nla_type(a);
  2204. switch (type) {
  2205. case OVS_ACTION_ATTR_SET:
  2206. err = set_action_to_attr(a, skb);
  2207. if (err)
  2208. return err;
  2209. break;
  2210. case OVS_ACTION_ATTR_SET_TO_MASKED:
  2211. err = masked_set_action_to_set_action_attr(a, skb);
  2212. if (err)
  2213. return err;
  2214. break;
  2215. case OVS_ACTION_ATTR_SAMPLE:
  2216. err = sample_action_to_attr(a, skb);
  2217. if (err)
  2218. return err;
  2219. break;
  2220. case OVS_ACTION_ATTR_CT:
  2221. err = ovs_ct_action_to_attr(nla_data(a), skb);
  2222. if (err)
  2223. return err;
  2224. break;
  2225. default:
  2226. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  2227. return -EMSGSIZE;
  2228. break;
  2229. }
  2230. }
  2231. return 0;
  2232. }