flow_netlink.c 76 KB

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