flow.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. /*
  2. * Copyright (c) 2007-2013 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. #include "flow.h"
  19. #include "datapath.h"
  20. #include <linux/uaccess.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if_vlan.h>
  25. #include <net/llc_pdu.h>
  26. #include <linux/kernel.h>
  27. #include <linux/jhash.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/llc.h>
  30. #include <linux/module.h>
  31. #include <linux/in.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/ip.h>
  35. #include <linux/ipv6.h>
  36. #include <linux/sctp.h>
  37. #include <linux/tcp.h>
  38. #include <linux/udp.h>
  39. #include <linux/icmp.h>
  40. #include <linux/icmpv6.h>
  41. #include <linux/rculist.h>
  42. #include <net/ip.h>
  43. #include <net/ip_tunnels.h>
  44. #include <net/ipv6.h>
  45. #include <net/ndisc.h>
  46. static struct kmem_cache *flow_cache;
  47. static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
  48. struct sw_flow_key_range *range, u8 val);
  49. static void update_range__(struct sw_flow_match *match,
  50. size_t offset, size_t size, bool is_mask)
  51. {
  52. struct sw_flow_key_range *range = NULL;
  53. size_t start = rounddown(offset, sizeof(long));
  54. size_t end = roundup(offset + size, sizeof(long));
  55. if (!is_mask)
  56. range = &match->range;
  57. else if (match->mask)
  58. range = &match->mask->range;
  59. if (!range)
  60. return;
  61. if (range->start == range->end) {
  62. range->start = start;
  63. range->end = end;
  64. return;
  65. }
  66. if (range->start > start)
  67. range->start = start;
  68. if (range->end < end)
  69. range->end = end;
  70. }
  71. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  72. do { \
  73. update_range__(match, offsetof(struct sw_flow_key, field), \
  74. sizeof((match)->key->field), is_mask); \
  75. if (is_mask) { \
  76. if ((match)->mask) \
  77. (match)->mask->key.field = value; \
  78. } else { \
  79. (match)->key->field = value; \
  80. } \
  81. } while (0)
  82. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  83. do { \
  84. update_range__(match, offsetof(struct sw_flow_key, field), \
  85. len, is_mask); \
  86. if (is_mask) { \
  87. if ((match)->mask) \
  88. memcpy(&(match)->mask->key.field, value_p, len);\
  89. } else { \
  90. memcpy(&(match)->key->field, value_p, len); \
  91. } \
  92. } while (0)
  93. static u16 range_n_bytes(const struct sw_flow_key_range *range)
  94. {
  95. return range->end - range->start;
  96. }
  97. void ovs_match_init(struct sw_flow_match *match,
  98. struct sw_flow_key *key,
  99. struct sw_flow_mask *mask)
  100. {
  101. memset(match, 0, sizeof(*match));
  102. match->key = key;
  103. match->mask = mask;
  104. memset(key, 0, sizeof(*key));
  105. if (mask) {
  106. memset(&mask->key, 0, sizeof(mask->key));
  107. mask->range.start = mask->range.end = 0;
  108. }
  109. }
  110. static bool ovs_match_validate(const struct sw_flow_match *match,
  111. u64 key_attrs, u64 mask_attrs)
  112. {
  113. u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
  114. u64 mask_allowed = key_attrs; /* At most allow all key attributes */
  115. /* The following mask attributes allowed only if they
  116. * pass the validation tests. */
  117. mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
  118. | (1 << OVS_KEY_ATTR_IPV6)
  119. | (1 << OVS_KEY_ATTR_TCP)
  120. | (1 << OVS_KEY_ATTR_UDP)
  121. | (1 << OVS_KEY_ATTR_SCTP)
  122. | (1 << OVS_KEY_ATTR_ICMP)
  123. | (1 << OVS_KEY_ATTR_ICMPV6)
  124. | (1 << OVS_KEY_ATTR_ARP)
  125. | (1 << OVS_KEY_ATTR_ND));
  126. /* Always allowed mask fields. */
  127. mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
  128. | (1 << OVS_KEY_ATTR_IN_PORT)
  129. | (1 << OVS_KEY_ATTR_ETHERTYPE));
  130. /* Check key attributes. */
  131. if (match->key->eth.type == htons(ETH_P_ARP)
  132. || match->key->eth.type == htons(ETH_P_RARP)) {
  133. key_expected |= 1 << OVS_KEY_ATTR_ARP;
  134. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  135. mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
  136. }
  137. if (match->key->eth.type == htons(ETH_P_IP)) {
  138. key_expected |= 1 << OVS_KEY_ATTR_IPV4;
  139. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  140. mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
  141. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  142. if (match->key->ip.proto == IPPROTO_UDP) {
  143. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  144. if (match->mask && (match->mask->key.ip.proto == 0xff))
  145. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  146. }
  147. if (match->key->ip.proto == IPPROTO_SCTP) {
  148. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  149. if (match->mask && (match->mask->key.ip.proto == 0xff))
  150. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  151. }
  152. if (match->key->ip.proto == IPPROTO_TCP) {
  153. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  154. if (match->mask && (match->mask->key.ip.proto == 0xff))
  155. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  156. }
  157. if (match->key->ip.proto == IPPROTO_ICMP) {
  158. key_expected |= 1 << OVS_KEY_ATTR_ICMP;
  159. if (match->mask && (match->mask->key.ip.proto == 0xff))
  160. mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
  161. }
  162. }
  163. }
  164. if (match->key->eth.type == htons(ETH_P_IPV6)) {
  165. key_expected |= 1 << OVS_KEY_ATTR_IPV6;
  166. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  167. mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
  168. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  169. if (match->key->ip.proto == IPPROTO_UDP) {
  170. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  171. if (match->mask && (match->mask->key.ip.proto == 0xff))
  172. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  173. }
  174. if (match->key->ip.proto == IPPROTO_SCTP) {
  175. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  176. if (match->mask && (match->mask->key.ip.proto == 0xff))
  177. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  178. }
  179. if (match->key->ip.proto == IPPROTO_TCP) {
  180. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  181. if (match->mask && (match->mask->key.ip.proto == 0xff))
  182. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  183. }
  184. if (match->key->ip.proto == IPPROTO_ICMPV6) {
  185. key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
  186. if (match->mask && (match->mask->key.ip.proto == 0xff))
  187. mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
  188. if (match->key->ipv6.tp.src ==
  189. htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  190. match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  191. key_expected |= 1 << OVS_KEY_ATTR_ND;
  192. if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
  193. mask_allowed |= 1 << OVS_KEY_ATTR_ND;
  194. }
  195. }
  196. }
  197. }
  198. if ((key_attrs & key_expected) != key_expected) {
  199. /* Key attributes check failed. */
  200. OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
  201. key_attrs, key_expected);
  202. return false;
  203. }
  204. if ((mask_attrs & mask_allowed) != mask_attrs) {
  205. /* Mask attributes check failed. */
  206. OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
  207. mask_attrs, mask_allowed);
  208. return false;
  209. }
  210. return true;
  211. }
  212. static int check_header(struct sk_buff *skb, int len)
  213. {
  214. if (unlikely(skb->len < len))
  215. return -EINVAL;
  216. if (unlikely(!pskb_may_pull(skb, len)))
  217. return -ENOMEM;
  218. return 0;
  219. }
  220. static bool arphdr_ok(struct sk_buff *skb)
  221. {
  222. return pskb_may_pull(skb, skb_network_offset(skb) +
  223. sizeof(struct arp_eth_header));
  224. }
  225. static int check_iphdr(struct sk_buff *skb)
  226. {
  227. unsigned int nh_ofs = skb_network_offset(skb);
  228. unsigned int ip_len;
  229. int err;
  230. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  231. if (unlikely(err))
  232. return err;
  233. ip_len = ip_hdrlen(skb);
  234. if (unlikely(ip_len < sizeof(struct iphdr) ||
  235. skb->len < nh_ofs + ip_len))
  236. return -EINVAL;
  237. skb_set_transport_header(skb, nh_ofs + ip_len);
  238. return 0;
  239. }
  240. static bool tcphdr_ok(struct sk_buff *skb)
  241. {
  242. int th_ofs = skb_transport_offset(skb);
  243. int tcp_len;
  244. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  245. return false;
  246. tcp_len = tcp_hdrlen(skb);
  247. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  248. skb->len < th_ofs + tcp_len))
  249. return false;
  250. return true;
  251. }
  252. static bool udphdr_ok(struct sk_buff *skb)
  253. {
  254. return pskb_may_pull(skb, skb_transport_offset(skb) +
  255. sizeof(struct udphdr));
  256. }
  257. static bool sctphdr_ok(struct sk_buff *skb)
  258. {
  259. return pskb_may_pull(skb, skb_transport_offset(skb) +
  260. sizeof(struct sctphdr));
  261. }
  262. static bool icmphdr_ok(struct sk_buff *skb)
  263. {
  264. return pskb_may_pull(skb, skb_transport_offset(skb) +
  265. sizeof(struct icmphdr));
  266. }
  267. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  268. {
  269. struct timespec cur_ts;
  270. u64 cur_ms, idle_ms;
  271. ktime_get_ts(&cur_ts);
  272. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  273. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  274. cur_ts.tv_nsec / NSEC_PER_MSEC;
  275. return cur_ms - idle_ms;
  276. }
  277. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
  278. {
  279. unsigned int nh_ofs = skb_network_offset(skb);
  280. unsigned int nh_len;
  281. int payload_ofs;
  282. struct ipv6hdr *nh;
  283. uint8_t nexthdr;
  284. __be16 frag_off;
  285. int err;
  286. err = check_header(skb, nh_ofs + sizeof(*nh));
  287. if (unlikely(err))
  288. return err;
  289. nh = ipv6_hdr(skb);
  290. nexthdr = nh->nexthdr;
  291. payload_ofs = (u8 *)(nh + 1) - skb->data;
  292. key->ip.proto = NEXTHDR_NONE;
  293. key->ip.tos = ipv6_get_dsfield(nh);
  294. key->ip.ttl = nh->hop_limit;
  295. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  296. key->ipv6.addr.src = nh->saddr;
  297. key->ipv6.addr.dst = nh->daddr;
  298. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  299. if (unlikely(payload_ofs < 0))
  300. return -EINVAL;
  301. if (frag_off) {
  302. if (frag_off & htons(~0x7))
  303. key->ip.frag = OVS_FRAG_TYPE_LATER;
  304. else
  305. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  306. }
  307. nh_len = payload_ofs - nh_ofs;
  308. skb_set_transport_header(skb, nh_ofs + nh_len);
  309. key->ip.proto = nexthdr;
  310. return nh_len;
  311. }
  312. static bool icmp6hdr_ok(struct sk_buff *skb)
  313. {
  314. return pskb_may_pull(skb, skb_transport_offset(skb) +
  315. sizeof(struct icmp6hdr));
  316. }
  317. void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
  318. const struct sw_flow_mask *mask)
  319. {
  320. const long *m = (long *)((u8 *)&mask->key + mask->range.start);
  321. const long *s = (long *)((u8 *)src + mask->range.start);
  322. long *d = (long *)((u8 *)dst + mask->range.start);
  323. int i;
  324. /* The memory outside of the 'mask->range' are not set since
  325. * further operations on 'dst' only uses contents within
  326. * 'mask->range'.
  327. */
  328. for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
  329. *d++ = *s++ & *m++;
  330. }
  331. #define TCP_FLAGS_OFFSET 13
  332. #define TCP_FLAG_MASK 0x3f
  333. void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
  334. {
  335. u8 tcp_flags = 0;
  336. if ((flow->key.eth.type == htons(ETH_P_IP) ||
  337. flow->key.eth.type == htons(ETH_P_IPV6)) &&
  338. flow->key.ip.proto == IPPROTO_TCP &&
  339. likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
  340. u8 *tcp = (u8 *)tcp_hdr(skb);
  341. tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
  342. }
  343. spin_lock(&flow->lock);
  344. flow->used = jiffies;
  345. flow->packet_count++;
  346. flow->byte_count += skb->len;
  347. flow->tcp_flags |= tcp_flags;
  348. spin_unlock(&flow->lock);
  349. }
  350. struct sw_flow_actions *ovs_flow_actions_alloc(int size)
  351. {
  352. struct sw_flow_actions *sfa;
  353. if (size > MAX_ACTIONS_BUFSIZE)
  354. return ERR_PTR(-EINVAL);
  355. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  356. if (!sfa)
  357. return ERR_PTR(-ENOMEM);
  358. sfa->actions_len = 0;
  359. return sfa;
  360. }
  361. struct sw_flow *ovs_flow_alloc(void)
  362. {
  363. struct sw_flow *flow;
  364. flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
  365. if (!flow)
  366. return ERR_PTR(-ENOMEM);
  367. spin_lock_init(&flow->lock);
  368. flow->sf_acts = NULL;
  369. flow->mask = NULL;
  370. return flow;
  371. }
  372. static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
  373. {
  374. hash = jhash_1word(hash, table->hash_seed);
  375. return flex_array_get(table->buckets,
  376. (hash & (table->n_buckets - 1)));
  377. }
  378. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  379. {
  380. struct flex_array *buckets;
  381. int i, err;
  382. buckets = flex_array_alloc(sizeof(struct hlist_head),
  383. n_buckets, GFP_KERNEL);
  384. if (!buckets)
  385. return NULL;
  386. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  387. if (err) {
  388. flex_array_free(buckets);
  389. return NULL;
  390. }
  391. for (i = 0; i < n_buckets; i++)
  392. INIT_HLIST_HEAD((struct hlist_head *)
  393. flex_array_get(buckets, i));
  394. return buckets;
  395. }
  396. static void free_buckets(struct flex_array *buckets)
  397. {
  398. flex_array_free(buckets);
  399. }
  400. static struct flow_table *__flow_tbl_alloc(int new_size)
  401. {
  402. struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
  403. if (!table)
  404. return NULL;
  405. table->buckets = alloc_buckets(new_size);
  406. if (!table->buckets) {
  407. kfree(table);
  408. return NULL;
  409. }
  410. table->n_buckets = new_size;
  411. table->count = 0;
  412. table->node_ver = 0;
  413. table->keep_flows = false;
  414. get_random_bytes(&table->hash_seed, sizeof(u32));
  415. table->mask_list = NULL;
  416. return table;
  417. }
  418. static void __flow_tbl_destroy(struct flow_table *table)
  419. {
  420. int i;
  421. if (table->keep_flows)
  422. goto skip_flows;
  423. for (i = 0; i < table->n_buckets; i++) {
  424. struct sw_flow *flow;
  425. struct hlist_head *head = flex_array_get(table->buckets, i);
  426. struct hlist_node *n;
  427. int ver = table->node_ver;
  428. hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
  429. hlist_del(&flow->hash_node[ver]);
  430. ovs_flow_free(flow, false);
  431. }
  432. }
  433. BUG_ON(!list_empty(table->mask_list));
  434. kfree(table->mask_list);
  435. skip_flows:
  436. free_buckets(table->buckets);
  437. kfree(table);
  438. }
  439. struct flow_table *ovs_flow_tbl_alloc(int new_size)
  440. {
  441. struct flow_table *table = __flow_tbl_alloc(new_size);
  442. if (!table)
  443. return NULL;
  444. table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
  445. if (!table->mask_list) {
  446. table->keep_flows = true;
  447. __flow_tbl_destroy(table);
  448. return NULL;
  449. }
  450. INIT_LIST_HEAD(table->mask_list);
  451. return table;
  452. }
  453. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  454. {
  455. struct flow_table *table = container_of(rcu, struct flow_table, rcu);
  456. __flow_tbl_destroy(table);
  457. }
  458. void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
  459. {
  460. if (!table)
  461. return;
  462. if (deferred)
  463. call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
  464. else
  465. __flow_tbl_destroy(table);
  466. }
  467. struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
  468. {
  469. struct sw_flow *flow;
  470. struct hlist_head *head;
  471. int ver;
  472. int i;
  473. ver = table->node_ver;
  474. while (*bucket < table->n_buckets) {
  475. i = 0;
  476. head = flex_array_get(table->buckets, *bucket);
  477. hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
  478. if (i < *last) {
  479. i++;
  480. continue;
  481. }
  482. *last = i + 1;
  483. return flow;
  484. }
  485. (*bucket)++;
  486. *last = 0;
  487. }
  488. return NULL;
  489. }
  490. static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
  491. {
  492. struct hlist_head *head;
  493. head = find_bucket(table, flow->hash);
  494. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  495. table->count++;
  496. }
  497. static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
  498. {
  499. int old_ver;
  500. int i;
  501. old_ver = old->node_ver;
  502. new->node_ver = !old_ver;
  503. /* Insert in new table. */
  504. for (i = 0; i < old->n_buckets; i++) {
  505. struct sw_flow *flow;
  506. struct hlist_head *head;
  507. head = flex_array_get(old->buckets, i);
  508. hlist_for_each_entry(flow, head, hash_node[old_ver])
  509. __tbl_insert(new, flow);
  510. }
  511. new->mask_list = old->mask_list;
  512. old->keep_flows = true;
  513. }
  514. static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
  515. {
  516. struct flow_table *new_table;
  517. new_table = __flow_tbl_alloc(n_buckets);
  518. if (!new_table)
  519. return ERR_PTR(-ENOMEM);
  520. flow_table_copy_flows(table, new_table);
  521. return new_table;
  522. }
  523. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
  524. {
  525. return __flow_tbl_rehash(table, table->n_buckets);
  526. }
  527. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
  528. {
  529. return __flow_tbl_rehash(table, table->n_buckets * 2);
  530. }
  531. static void __flow_free(struct sw_flow *flow)
  532. {
  533. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  534. kmem_cache_free(flow_cache, flow);
  535. }
  536. static void rcu_free_flow_callback(struct rcu_head *rcu)
  537. {
  538. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  539. __flow_free(flow);
  540. }
  541. void ovs_flow_free(struct sw_flow *flow, bool deferred)
  542. {
  543. if (!flow)
  544. return;
  545. ovs_sw_flow_mask_del_ref(flow->mask, deferred);
  546. if (deferred)
  547. call_rcu(&flow->rcu, rcu_free_flow_callback);
  548. else
  549. __flow_free(flow);
  550. }
  551. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  552. * The caller must hold rcu_read_lock for this to be sensible. */
  553. void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
  554. {
  555. kfree_rcu(sf_acts, rcu);
  556. }
  557. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  558. {
  559. struct qtag_prefix {
  560. __be16 eth_type; /* ETH_P_8021Q */
  561. __be16 tci;
  562. };
  563. struct qtag_prefix *qp;
  564. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  565. return 0;
  566. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  567. sizeof(__be16))))
  568. return -ENOMEM;
  569. qp = (struct qtag_prefix *) skb->data;
  570. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  571. __skb_pull(skb, sizeof(struct qtag_prefix));
  572. return 0;
  573. }
  574. static __be16 parse_ethertype(struct sk_buff *skb)
  575. {
  576. struct llc_snap_hdr {
  577. u8 dsap; /* Always 0xAA */
  578. u8 ssap; /* Always 0xAA */
  579. u8 ctrl;
  580. u8 oui[3];
  581. __be16 ethertype;
  582. };
  583. struct llc_snap_hdr *llc;
  584. __be16 proto;
  585. proto = *(__be16 *) skb->data;
  586. __skb_pull(skb, sizeof(__be16));
  587. if (ntohs(proto) >= ETH_P_802_3_MIN)
  588. return proto;
  589. if (skb->len < sizeof(struct llc_snap_hdr))
  590. return htons(ETH_P_802_2);
  591. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  592. return htons(0);
  593. llc = (struct llc_snap_hdr *) skb->data;
  594. if (llc->dsap != LLC_SAP_SNAP ||
  595. llc->ssap != LLC_SAP_SNAP ||
  596. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  597. return htons(ETH_P_802_2);
  598. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  599. if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
  600. return llc->ethertype;
  601. return htons(ETH_P_802_2);
  602. }
  603. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  604. int nh_len)
  605. {
  606. struct icmp6hdr *icmp = icmp6_hdr(skb);
  607. /* The ICMPv6 type and code fields use the 16-bit transport port
  608. * fields, so we need to store them in 16-bit network byte order.
  609. */
  610. key->ipv6.tp.src = htons(icmp->icmp6_type);
  611. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  612. if (icmp->icmp6_code == 0 &&
  613. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  614. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  615. int icmp_len = skb->len - skb_transport_offset(skb);
  616. struct nd_msg *nd;
  617. int offset;
  618. /* In order to process neighbor discovery options, we need the
  619. * entire packet.
  620. */
  621. if (unlikely(icmp_len < sizeof(*nd)))
  622. return 0;
  623. if (unlikely(skb_linearize(skb)))
  624. return -ENOMEM;
  625. nd = (struct nd_msg *)skb_transport_header(skb);
  626. key->ipv6.nd.target = nd->target;
  627. icmp_len -= sizeof(*nd);
  628. offset = 0;
  629. while (icmp_len >= 8) {
  630. struct nd_opt_hdr *nd_opt =
  631. (struct nd_opt_hdr *)(nd->opt + offset);
  632. int opt_len = nd_opt->nd_opt_len * 8;
  633. if (unlikely(!opt_len || opt_len > icmp_len))
  634. return 0;
  635. /* Store the link layer address if the appropriate
  636. * option is provided. It is considered an error if
  637. * the same link layer option is specified twice.
  638. */
  639. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  640. && opt_len == 8) {
  641. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  642. goto invalid;
  643. memcpy(key->ipv6.nd.sll,
  644. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  645. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  646. && opt_len == 8) {
  647. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  648. goto invalid;
  649. memcpy(key->ipv6.nd.tll,
  650. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  651. }
  652. icmp_len -= opt_len;
  653. offset += opt_len;
  654. }
  655. }
  656. return 0;
  657. invalid:
  658. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  659. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  660. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  661. return 0;
  662. }
  663. /**
  664. * ovs_flow_extract - extracts a flow key from an Ethernet frame.
  665. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  666. * Ethernet header
  667. * @in_port: port number on which @skb was received.
  668. * @key: output flow key
  669. *
  670. * The caller must ensure that skb->len >= ETH_HLEN.
  671. *
  672. * Returns 0 if successful, otherwise a negative errno value.
  673. *
  674. * Initializes @skb header pointers as follows:
  675. *
  676. * - skb->mac_header: the Ethernet header.
  677. *
  678. * - skb->network_header: just past the Ethernet header, or just past the
  679. * VLAN header, to the first byte of the Ethernet payload.
  680. *
  681. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  682. * on output, then just past the IP header, if one is present and
  683. * of a correct length, otherwise the same as skb->network_header.
  684. * For other key->eth.type values it is left untouched.
  685. */
  686. int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
  687. {
  688. int error;
  689. struct ethhdr *eth;
  690. memset(key, 0, sizeof(*key));
  691. key->phy.priority = skb->priority;
  692. if (OVS_CB(skb)->tun_key)
  693. memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
  694. key->phy.in_port = in_port;
  695. key->phy.skb_mark = skb->mark;
  696. skb_reset_mac_header(skb);
  697. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  698. * header in the linear data area.
  699. */
  700. eth = eth_hdr(skb);
  701. memcpy(key->eth.src, eth->h_source, ETH_ALEN);
  702. memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
  703. __skb_pull(skb, 2 * ETH_ALEN);
  704. /* We are going to push all headers that we pull, so no need to
  705. * update skb->csum here.
  706. */
  707. if (vlan_tx_tag_present(skb))
  708. key->eth.tci = htons(skb->vlan_tci);
  709. else if (eth->h_proto == htons(ETH_P_8021Q))
  710. if (unlikely(parse_vlan(skb, key)))
  711. return -ENOMEM;
  712. key->eth.type = parse_ethertype(skb);
  713. if (unlikely(key->eth.type == htons(0)))
  714. return -ENOMEM;
  715. skb_reset_network_header(skb);
  716. __skb_push(skb, skb->data - skb_mac_header(skb));
  717. /* Network layer. */
  718. if (key->eth.type == htons(ETH_P_IP)) {
  719. struct iphdr *nh;
  720. __be16 offset;
  721. error = check_iphdr(skb);
  722. if (unlikely(error)) {
  723. if (error == -EINVAL) {
  724. skb->transport_header = skb->network_header;
  725. error = 0;
  726. }
  727. return error;
  728. }
  729. nh = ip_hdr(skb);
  730. key->ipv4.addr.src = nh->saddr;
  731. key->ipv4.addr.dst = nh->daddr;
  732. key->ip.proto = nh->protocol;
  733. key->ip.tos = nh->tos;
  734. key->ip.ttl = nh->ttl;
  735. offset = nh->frag_off & htons(IP_OFFSET);
  736. if (offset) {
  737. key->ip.frag = OVS_FRAG_TYPE_LATER;
  738. return 0;
  739. }
  740. if (nh->frag_off & htons(IP_MF) ||
  741. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  742. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  743. /* Transport layer. */
  744. if (key->ip.proto == IPPROTO_TCP) {
  745. if (tcphdr_ok(skb)) {
  746. struct tcphdr *tcp = tcp_hdr(skb);
  747. key->ipv4.tp.src = tcp->source;
  748. key->ipv4.tp.dst = tcp->dest;
  749. }
  750. } else if (key->ip.proto == IPPROTO_UDP) {
  751. if (udphdr_ok(skb)) {
  752. struct udphdr *udp = udp_hdr(skb);
  753. key->ipv4.tp.src = udp->source;
  754. key->ipv4.tp.dst = udp->dest;
  755. }
  756. } else if (key->ip.proto == IPPROTO_SCTP) {
  757. if (sctphdr_ok(skb)) {
  758. struct sctphdr *sctp = sctp_hdr(skb);
  759. key->ipv4.tp.src = sctp->source;
  760. key->ipv4.tp.dst = sctp->dest;
  761. }
  762. } else if (key->ip.proto == IPPROTO_ICMP) {
  763. if (icmphdr_ok(skb)) {
  764. struct icmphdr *icmp = icmp_hdr(skb);
  765. /* The ICMP type and code fields use the 16-bit
  766. * transport port fields, so we need to store
  767. * them in 16-bit network byte order. */
  768. key->ipv4.tp.src = htons(icmp->type);
  769. key->ipv4.tp.dst = htons(icmp->code);
  770. }
  771. }
  772. } else if ((key->eth.type == htons(ETH_P_ARP) ||
  773. key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
  774. struct arp_eth_header *arp;
  775. arp = (struct arp_eth_header *)skb_network_header(skb);
  776. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  777. && arp->ar_pro == htons(ETH_P_IP)
  778. && arp->ar_hln == ETH_ALEN
  779. && arp->ar_pln == 4) {
  780. /* We only match on the lower 8 bits of the opcode. */
  781. if (ntohs(arp->ar_op) <= 0xff)
  782. key->ip.proto = ntohs(arp->ar_op);
  783. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  784. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  785. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  786. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  787. }
  788. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  789. int nh_len; /* IPv6 Header + Extensions */
  790. nh_len = parse_ipv6hdr(skb, key);
  791. if (unlikely(nh_len < 0)) {
  792. if (nh_len == -EINVAL) {
  793. skb->transport_header = skb->network_header;
  794. error = 0;
  795. } else {
  796. error = nh_len;
  797. }
  798. return error;
  799. }
  800. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  801. return 0;
  802. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  803. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  804. /* Transport layer. */
  805. if (key->ip.proto == NEXTHDR_TCP) {
  806. if (tcphdr_ok(skb)) {
  807. struct tcphdr *tcp = tcp_hdr(skb);
  808. key->ipv6.tp.src = tcp->source;
  809. key->ipv6.tp.dst = tcp->dest;
  810. }
  811. } else if (key->ip.proto == NEXTHDR_UDP) {
  812. if (udphdr_ok(skb)) {
  813. struct udphdr *udp = udp_hdr(skb);
  814. key->ipv6.tp.src = udp->source;
  815. key->ipv6.tp.dst = udp->dest;
  816. }
  817. } else if (key->ip.proto == NEXTHDR_SCTP) {
  818. if (sctphdr_ok(skb)) {
  819. struct sctphdr *sctp = sctp_hdr(skb);
  820. key->ipv6.tp.src = sctp->source;
  821. key->ipv6.tp.dst = sctp->dest;
  822. }
  823. } else if (key->ip.proto == NEXTHDR_ICMP) {
  824. if (icmp6hdr_ok(skb)) {
  825. error = parse_icmpv6(skb, key, nh_len);
  826. if (error)
  827. return error;
  828. }
  829. }
  830. }
  831. return 0;
  832. }
  833. static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
  834. int key_end)
  835. {
  836. u32 *hash_key = (u32 *)((u8 *)key + key_start);
  837. int hash_u32s = (key_end - key_start) >> 2;
  838. /* Make sure number of hash bytes are multiple of u32. */
  839. BUILD_BUG_ON(sizeof(long) % sizeof(u32));
  840. return jhash2(hash_key, hash_u32s, 0);
  841. }
  842. static int flow_key_start(const struct sw_flow_key *key)
  843. {
  844. if (key->tun_key.ipv4_dst)
  845. return 0;
  846. else
  847. return rounddown(offsetof(struct sw_flow_key, phy),
  848. sizeof(long));
  849. }
  850. static bool __cmp_key(const struct sw_flow_key *key1,
  851. const struct sw_flow_key *key2, int key_start, int key_end)
  852. {
  853. const long *cp1 = (long *)((u8 *)key1 + key_start);
  854. const long *cp2 = (long *)((u8 *)key2 + key_start);
  855. long diffs = 0;
  856. int i;
  857. for (i = key_start; i < key_end; i += sizeof(long))
  858. diffs |= *cp1++ ^ *cp2++;
  859. return diffs == 0;
  860. }
  861. static bool __flow_cmp_masked_key(const struct sw_flow *flow,
  862. const struct sw_flow_key *key, int key_start, int key_end)
  863. {
  864. return __cmp_key(&flow->key, key, key_start, key_end);
  865. }
  866. static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
  867. const struct sw_flow_key *key, int key_start, int key_end)
  868. {
  869. return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
  870. }
  871. bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  872. const struct sw_flow_key *key, int key_end)
  873. {
  874. int key_start;
  875. key_start = flow_key_start(key);
  876. return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
  877. }
  878. struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
  879. struct sw_flow_match *match)
  880. {
  881. struct sw_flow_key *unmasked = match->key;
  882. int key_end = match->range.end;
  883. struct sw_flow *flow;
  884. flow = ovs_flow_lookup(table, unmasked);
  885. if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
  886. flow = NULL;
  887. return flow;
  888. }
  889. static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
  890. const struct sw_flow_key *unmasked,
  891. struct sw_flow_mask *mask)
  892. {
  893. struct sw_flow *flow;
  894. struct hlist_head *head;
  895. int key_start = mask->range.start;
  896. int key_end = mask->range.end;
  897. u32 hash;
  898. struct sw_flow_key masked_key;
  899. ovs_flow_key_mask(&masked_key, unmasked, mask);
  900. hash = ovs_flow_hash(&masked_key, key_start, key_end);
  901. head = find_bucket(table, hash);
  902. hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
  903. if (flow->mask == mask &&
  904. __flow_cmp_masked_key(flow, &masked_key,
  905. key_start, key_end))
  906. return flow;
  907. }
  908. return NULL;
  909. }
  910. struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
  911. const struct sw_flow_key *key)
  912. {
  913. struct sw_flow *flow = NULL;
  914. struct sw_flow_mask *mask;
  915. list_for_each_entry_rcu(mask, tbl->mask_list, list) {
  916. flow = ovs_masked_flow_lookup(tbl, key, mask);
  917. if (flow) /* Found */
  918. break;
  919. }
  920. return flow;
  921. }
  922. void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
  923. {
  924. flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
  925. flow->mask->range.end);
  926. __tbl_insert(table, flow);
  927. }
  928. void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
  929. {
  930. BUG_ON(table->count == 0);
  931. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  932. table->count--;
  933. }
  934. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  935. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  936. [OVS_KEY_ATTR_ENCAP] = -1,
  937. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  938. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  939. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  940. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  941. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  942. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  943. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  944. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  945. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  946. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  947. [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
  948. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  949. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  950. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  951. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  952. [OVS_KEY_ATTR_TUNNEL] = -1,
  953. };
  954. static bool is_all_zero(const u8 *fp, size_t size)
  955. {
  956. int i;
  957. if (!fp)
  958. return false;
  959. for (i = 0; i < size; i++)
  960. if (fp[i])
  961. return false;
  962. return true;
  963. }
  964. static int __parse_flow_nlattrs(const struct nlattr *attr,
  965. const struct nlattr *a[],
  966. u64 *attrsp, bool nz)
  967. {
  968. const struct nlattr *nla;
  969. u32 attrs;
  970. int rem;
  971. attrs = *attrsp;
  972. nla_for_each_nested(nla, attr, rem) {
  973. u16 type = nla_type(nla);
  974. int expected_len;
  975. if (type > OVS_KEY_ATTR_MAX) {
  976. OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
  977. type, OVS_KEY_ATTR_MAX);
  978. return -EINVAL;
  979. }
  980. if (attrs & (1 << type)) {
  981. OVS_NLERR("Duplicate key attribute (type %d).\n", type);
  982. return -EINVAL;
  983. }
  984. expected_len = ovs_key_lens[type];
  985. if (nla_len(nla) != expected_len && expected_len != -1) {
  986. OVS_NLERR("Key attribute has unexpected length (type=%d"
  987. ", length=%d, expected=%d).\n", type,
  988. nla_len(nla), expected_len);
  989. return -EINVAL;
  990. }
  991. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  992. attrs |= 1 << type;
  993. a[type] = nla;
  994. }
  995. }
  996. if (rem) {
  997. OVS_NLERR("Message has %d unknown bytes.\n", rem);
  998. return -EINVAL;
  999. }
  1000. *attrsp = attrs;
  1001. return 0;
  1002. }
  1003. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  1004. const struct nlattr *a[], u64 *attrsp)
  1005. {
  1006. return __parse_flow_nlattrs(attr, a, attrsp, true);
  1007. }
  1008. static int parse_flow_nlattrs(const struct nlattr *attr,
  1009. const struct nlattr *a[], u64 *attrsp)
  1010. {
  1011. return __parse_flow_nlattrs(attr, a, attrsp, false);
  1012. }
  1013. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  1014. struct sw_flow_match *match, bool is_mask)
  1015. {
  1016. struct nlattr *a;
  1017. int rem;
  1018. bool ttl = false;
  1019. __be16 tun_flags = 0;
  1020. nla_for_each_nested(a, attr, rem) {
  1021. int type = nla_type(a);
  1022. static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  1023. [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
  1024. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
  1025. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
  1026. [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
  1027. [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
  1028. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
  1029. [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
  1030. };
  1031. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  1032. OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
  1033. type, OVS_TUNNEL_KEY_ATTR_MAX);
  1034. return -EINVAL;
  1035. }
  1036. if (ovs_tunnel_key_lens[type] != nla_len(a)) {
  1037. OVS_NLERR("IPv4 tunnel attribute type has unexpected "
  1038. " length (type=%d, length=%d, expected=%d).\n",
  1039. type, nla_len(a), ovs_tunnel_key_lens[type]);
  1040. return -EINVAL;
  1041. }
  1042. switch (type) {
  1043. case OVS_TUNNEL_KEY_ATTR_ID:
  1044. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  1045. nla_get_be64(a), is_mask);
  1046. tun_flags |= TUNNEL_KEY;
  1047. break;
  1048. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  1049. SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
  1050. nla_get_be32(a), is_mask);
  1051. break;
  1052. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  1053. SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
  1054. nla_get_be32(a), is_mask);
  1055. break;
  1056. case OVS_TUNNEL_KEY_ATTR_TOS:
  1057. SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
  1058. nla_get_u8(a), is_mask);
  1059. break;
  1060. case OVS_TUNNEL_KEY_ATTR_TTL:
  1061. SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
  1062. nla_get_u8(a), is_mask);
  1063. ttl = true;
  1064. break;
  1065. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  1066. tun_flags |= TUNNEL_DONT_FRAGMENT;
  1067. break;
  1068. case OVS_TUNNEL_KEY_ATTR_CSUM:
  1069. tun_flags |= TUNNEL_CSUM;
  1070. break;
  1071. default:
  1072. return -EINVAL;
  1073. }
  1074. }
  1075. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  1076. if (rem > 0) {
  1077. OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
  1078. return -EINVAL;
  1079. }
  1080. if (!is_mask) {
  1081. if (!match->key->tun_key.ipv4_dst) {
  1082. OVS_NLERR("IPv4 tunnel destination address is zero.\n");
  1083. return -EINVAL;
  1084. }
  1085. if (!ttl) {
  1086. OVS_NLERR("IPv4 tunnel TTL not specified.\n");
  1087. return -EINVAL;
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  1093. const struct ovs_key_ipv4_tunnel *tun_key,
  1094. const struct ovs_key_ipv4_tunnel *output)
  1095. {
  1096. struct nlattr *nla;
  1097. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  1098. if (!nla)
  1099. return -EMSGSIZE;
  1100. if (output->tun_flags & TUNNEL_KEY &&
  1101. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  1102. return -EMSGSIZE;
  1103. if (output->ipv4_src &&
  1104. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
  1105. return -EMSGSIZE;
  1106. if (output->ipv4_dst &&
  1107. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
  1108. return -EMSGSIZE;
  1109. if (output->ipv4_tos &&
  1110. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  1111. return -EMSGSIZE;
  1112. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  1113. return -EMSGSIZE;
  1114. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  1115. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  1116. return -EMSGSIZE;
  1117. if ((output->tun_flags & TUNNEL_CSUM) &&
  1118. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  1119. return -EMSGSIZE;
  1120. nla_nest_end(skb, nla);
  1121. return 0;
  1122. }
  1123. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  1124. const struct nlattr **a, bool is_mask)
  1125. {
  1126. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  1127. SW_FLOW_KEY_PUT(match, phy.priority,
  1128. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  1129. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  1130. }
  1131. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  1132. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  1133. if (is_mask)
  1134. in_port = 0xffffffff; /* Always exact match in_port. */
  1135. else if (in_port >= DP_MAX_PORTS)
  1136. return -EINVAL;
  1137. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  1138. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  1139. } else if (!is_mask) {
  1140. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  1141. }
  1142. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  1143. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  1144. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  1145. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  1146. }
  1147. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  1148. if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  1149. is_mask))
  1150. return -EINVAL;
  1151. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  1152. }
  1153. return 0;
  1154. }
  1155. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  1156. const struct nlattr **a, bool is_mask)
  1157. {
  1158. int err;
  1159. u64 orig_attrs = attrs;
  1160. err = metadata_from_nlattrs(match, &attrs, a, is_mask);
  1161. if (err)
  1162. return err;
  1163. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  1164. const struct ovs_key_ethernet *eth_key;
  1165. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  1166. SW_FLOW_KEY_MEMCPY(match, eth.src,
  1167. eth_key->eth_src, ETH_ALEN, is_mask);
  1168. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  1169. eth_key->eth_dst, ETH_ALEN, is_mask);
  1170. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  1171. }
  1172. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  1173. __be16 tci;
  1174. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1175. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1176. if (is_mask)
  1177. OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
  1178. else
  1179. OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
  1180. return -EINVAL;
  1181. }
  1182. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  1183. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  1184. } else if (!is_mask)
  1185. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  1186. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  1187. __be16 eth_type;
  1188. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1189. if (is_mask) {
  1190. /* Always exact match EtherType. */
  1191. eth_type = htons(0xffff);
  1192. } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
  1193. OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
  1194. ntohs(eth_type), ETH_P_802_3_MIN);
  1195. return -EINVAL;
  1196. }
  1197. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  1198. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1199. } else if (!is_mask) {
  1200. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  1201. }
  1202. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1203. const struct ovs_key_ipv4 *ipv4_key;
  1204. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  1205. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  1206. OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
  1207. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  1208. return -EINVAL;
  1209. }
  1210. SW_FLOW_KEY_PUT(match, ip.proto,
  1211. ipv4_key->ipv4_proto, is_mask);
  1212. SW_FLOW_KEY_PUT(match, ip.tos,
  1213. ipv4_key->ipv4_tos, is_mask);
  1214. SW_FLOW_KEY_PUT(match, ip.ttl,
  1215. ipv4_key->ipv4_ttl, is_mask);
  1216. SW_FLOW_KEY_PUT(match, ip.frag,
  1217. ipv4_key->ipv4_frag, is_mask);
  1218. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1219. ipv4_key->ipv4_src, is_mask);
  1220. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1221. ipv4_key->ipv4_dst, is_mask);
  1222. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  1223. }
  1224. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  1225. const struct ovs_key_ipv6 *ipv6_key;
  1226. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  1227. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  1228. OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
  1229. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  1230. return -EINVAL;
  1231. }
  1232. SW_FLOW_KEY_PUT(match, ipv6.label,
  1233. ipv6_key->ipv6_label, is_mask);
  1234. SW_FLOW_KEY_PUT(match, ip.proto,
  1235. ipv6_key->ipv6_proto, is_mask);
  1236. SW_FLOW_KEY_PUT(match, ip.tos,
  1237. ipv6_key->ipv6_tclass, is_mask);
  1238. SW_FLOW_KEY_PUT(match, ip.ttl,
  1239. ipv6_key->ipv6_hlimit, is_mask);
  1240. SW_FLOW_KEY_PUT(match, ip.frag,
  1241. ipv6_key->ipv6_frag, is_mask);
  1242. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  1243. ipv6_key->ipv6_src,
  1244. sizeof(match->key->ipv6.addr.src),
  1245. is_mask);
  1246. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  1247. ipv6_key->ipv6_dst,
  1248. sizeof(match->key->ipv6.addr.dst),
  1249. is_mask);
  1250. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1251. }
  1252. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  1253. const struct ovs_key_arp *arp_key;
  1254. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1255. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  1256. OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
  1257. arp_key->arp_op);
  1258. return -EINVAL;
  1259. }
  1260. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1261. arp_key->arp_sip, is_mask);
  1262. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1263. arp_key->arp_tip, is_mask);
  1264. SW_FLOW_KEY_PUT(match, ip.proto,
  1265. ntohs(arp_key->arp_op), is_mask);
  1266. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  1267. arp_key->arp_sha, ETH_ALEN, is_mask);
  1268. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  1269. arp_key->arp_tha, ETH_ALEN, is_mask);
  1270. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1271. }
  1272. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  1273. const struct ovs_key_tcp *tcp_key;
  1274. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  1275. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1276. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1277. tcp_key->tcp_src, is_mask);
  1278. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1279. tcp_key->tcp_dst, is_mask);
  1280. } else {
  1281. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1282. tcp_key->tcp_src, is_mask);
  1283. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1284. tcp_key->tcp_dst, is_mask);
  1285. }
  1286. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  1287. }
  1288. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  1289. const struct ovs_key_udp *udp_key;
  1290. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  1291. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1292. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1293. udp_key->udp_src, is_mask);
  1294. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1295. udp_key->udp_dst, is_mask);
  1296. } else {
  1297. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1298. udp_key->udp_src, is_mask);
  1299. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1300. udp_key->udp_dst, is_mask);
  1301. }
  1302. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  1303. }
  1304. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  1305. const struct ovs_key_sctp *sctp_key;
  1306. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  1307. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1308. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1309. sctp_key->sctp_src, is_mask);
  1310. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1311. sctp_key->sctp_dst, is_mask);
  1312. } else {
  1313. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1314. sctp_key->sctp_src, is_mask);
  1315. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1316. sctp_key->sctp_dst, is_mask);
  1317. }
  1318. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  1319. }
  1320. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  1321. const struct ovs_key_icmp *icmp_key;
  1322. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  1323. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1324. htons(icmp_key->icmp_type), is_mask);
  1325. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1326. htons(icmp_key->icmp_code), is_mask);
  1327. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  1328. }
  1329. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  1330. const struct ovs_key_icmpv6 *icmpv6_key;
  1331. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  1332. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1333. htons(icmpv6_key->icmpv6_type), is_mask);
  1334. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1335. htons(icmpv6_key->icmpv6_code), is_mask);
  1336. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  1337. }
  1338. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  1339. const struct ovs_key_nd *nd_key;
  1340. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  1341. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  1342. nd_key->nd_target,
  1343. sizeof(match->key->ipv6.nd.target),
  1344. is_mask);
  1345. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  1346. nd_key->nd_sll, ETH_ALEN, is_mask);
  1347. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  1348. nd_key->nd_tll, ETH_ALEN, is_mask);
  1349. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  1350. }
  1351. if (attrs != 0)
  1352. return -EINVAL;
  1353. return 0;
  1354. }
  1355. /**
  1356. * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
  1357. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1358. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1359. * does not include any don't care bit.
  1360. * @match: receives the extracted flow match information.
  1361. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1362. * sequence. The fields should of the packet that triggered the creation
  1363. * of this flow.
  1364. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  1365. * attribute specifies the mask field of the wildcarded flow.
  1366. */
  1367. int ovs_match_from_nlattrs(struct sw_flow_match *match,
  1368. const struct nlattr *key,
  1369. const struct nlattr *mask)
  1370. {
  1371. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1372. const struct nlattr *encap;
  1373. u64 key_attrs = 0;
  1374. u64 mask_attrs = 0;
  1375. bool encap_valid = false;
  1376. int err;
  1377. err = parse_flow_nlattrs(key, a, &key_attrs);
  1378. if (err)
  1379. return err;
  1380. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  1381. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  1382. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  1383. __be16 tci;
  1384. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  1385. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  1386. OVS_NLERR("Invalid Vlan frame.\n");
  1387. return -EINVAL;
  1388. }
  1389. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1390. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1391. encap = a[OVS_KEY_ATTR_ENCAP];
  1392. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1393. encap_valid = true;
  1394. if (tci & htons(VLAN_TAG_PRESENT)) {
  1395. err = parse_flow_nlattrs(encap, a, &key_attrs);
  1396. if (err)
  1397. return err;
  1398. } else if (!tci) {
  1399. /* Corner case for truncated 802.1Q header. */
  1400. if (nla_len(encap)) {
  1401. OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
  1402. return -EINVAL;
  1403. }
  1404. } else {
  1405. OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
  1406. return -EINVAL;
  1407. }
  1408. }
  1409. err = ovs_key_from_nlattrs(match, key_attrs, a, false);
  1410. if (err)
  1411. return err;
  1412. if (mask) {
  1413. err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
  1414. if (err)
  1415. return err;
  1416. if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
  1417. __be16 eth_type = 0;
  1418. __be16 tci = 0;
  1419. if (!encap_valid) {
  1420. OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
  1421. return -EINVAL;
  1422. }
  1423. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1424. if (a[OVS_KEY_ATTR_ETHERTYPE])
  1425. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1426. if (eth_type == htons(0xffff)) {
  1427. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1428. encap = a[OVS_KEY_ATTR_ENCAP];
  1429. err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
  1430. } else {
  1431. OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
  1432. ntohs(eth_type));
  1433. return -EINVAL;
  1434. }
  1435. if (a[OVS_KEY_ATTR_VLAN])
  1436. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1437. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1438. OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
  1439. return -EINVAL;
  1440. }
  1441. }
  1442. err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
  1443. if (err)
  1444. return err;
  1445. } else {
  1446. /* Populate exact match flow's key mask. */
  1447. if (match->mask)
  1448. ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
  1449. }
  1450. if (!ovs_match_validate(match, key_attrs, mask_attrs))
  1451. return -EINVAL;
  1452. return 0;
  1453. }
  1454. /**
  1455. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  1456. * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
  1457. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1458. * sequence.
  1459. *
  1460. * This parses a series of Netlink attributes that form a flow key, which must
  1461. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1462. * get the metadata, that is, the parts of the flow key that cannot be
  1463. * extracted from the packet itself.
  1464. */
  1465. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
  1466. const struct nlattr *attr)
  1467. {
  1468. struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
  1469. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1470. u64 attrs = 0;
  1471. int err;
  1472. struct sw_flow_match match;
  1473. flow->key.phy.in_port = DP_MAX_PORTS;
  1474. flow->key.phy.priority = 0;
  1475. flow->key.phy.skb_mark = 0;
  1476. memset(tun_key, 0, sizeof(flow->key.tun_key));
  1477. err = parse_flow_nlattrs(attr, a, &attrs);
  1478. if (err)
  1479. return -EINVAL;
  1480. memset(&match, 0, sizeof(match));
  1481. match.key = &flow->key;
  1482. err = metadata_from_nlattrs(&match, &attrs, a, false);
  1483. if (err)
  1484. return err;
  1485. return 0;
  1486. }
  1487. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
  1488. const struct sw_flow_key *output, struct sk_buff *skb)
  1489. {
  1490. struct ovs_key_ethernet *eth_key;
  1491. struct nlattr *nla, *encap;
  1492. bool is_mask = (swkey != output);
  1493. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1494. goto nla_put_failure;
  1495. if ((swkey->tun_key.ipv4_dst || is_mask) &&
  1496. ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
  1497. goto nla_put_failure;
  1498. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1499. if (is_mask && (output->phy.in_port == 0xffff))
  1500. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1501. goto nla_put_failure;
  1502. } else {
  1503. u16 upper_u16;
  1504. upper_u16 = !is_mask ? 0 : 0xffff;
  1505. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1506. (upper_u16 << 16) | output->phy.in_port))
  1507. goto nla_put_failure;
  1508. }
  1509. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1510. goto nla_put_failure;
  1511. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1512. if (!nla)
  1513. goto nla_put_failure;
  1514. eth_key = nla_data(nla);
  1515. memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
  1516. memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
  1517. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1518. __be16 eth_type;
  1519. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1520. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1521. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1522. goto nla_put_failure;
  1523. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1524. if (!swkey->eth.tci)
  1525. goto unencap;
  1526. } else
  1527. encap = NULL;
  1528. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1529. /*
  1530. * Ethertype 802.2 is represented in the netlink with omitted
  1531. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1532. * 0xffff in the mask attribute. Ethertype can also
  1533. * be wildcarded.
  1534. */
  1535. if (is_mask && output->eth.type)
  1536. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1537. output->eth.type))
  1538. goto nla_put_failure;
  1539. goto unencap;
  1540. }
  1541. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1542. goto nla_put_failure;
  1543. if (swkey->eth.type == htons(ETH_P_IP)) {
  1544. struct ovs_key_ipv4 *ipv4_key;
  1545. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1546. if (!nla)
  1547. goto nla_put_failure;
  1548. ipv4_key = nla_data(nla);
  1549. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1550. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1551. ipv4_key->ipv4_proto = output->ip.proto;
  1552. ipv4_key->ipv4_tos = output->ip.tos;
  1553. ipv4_key->ipv4_ttl = output->ip.ttl;
  1554. ipv4_key->ipv4_frag = output->ip.frag;
  1555. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1556. struct ovs_key_ipv6 *ipv6_key;
  1557. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1558. if (!nla)
  1559. goto nla_put_failure;
  1560. ipv6_key = nla_data(nla);
  1561. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1562. sizeof(ipv6_key->ipv6_src));
  1563. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1564. sizeof(ipv6_key->ipv6_dst));
  1565. ipv6_key->ipv6_label = output->ipv6.label;
  1566. ipv6_key->ipv6_proto = output->ip.proto;
  1567. ipv6_key->ipv6_tclass = output->ip.tos;
  1568. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1569. ipv6_key->ipv6_frag = output->ip.frag;
  1570. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1571. swkey->eth.type == htons(ETH_P_RARP)) {
  1572. struct ovs_key_arp *arp_key;
  1573. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1574. if (!nla)
  1575. goto nla_put_failure;
  1576. arp_key = nla_data(nla);
  1577. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1578. arp_key->arp_sip = output->ipv4.addr.src;
  1579. arp_key->arp_tip = output->ipv4.addr.dst;
  1580. arp_key->arp_op = htons(output->ip.proto);
  1581. memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
  1582. memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
  1583. }
  1584. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1585. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1586. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1587. if (swkey->ip.proto == IPPROTO_TCP) {
  1588. struct ovs_key_tcp *tcp_key;
  1589. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1590. if (!nla)
  1591. goto nla_put_failure;
  1592. tcp_key = nla_data(nla);
  1593. if (swkey->eth.type == htons(ETH_P_IP)) {
  1594. tcp_key->tcp_src = output->ipv4.tp.src;
  1595. tcp_key->tcp_dst = output->ipv4.tp.dst;
  1596. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1597. tcp_key->tcp_src = output->ipv6.tp.src;
  1598. tcp_key->tcp_dst = output->ipv6.tp.dst;
  1599. }
  1600. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1601. struct ovs_key_udp *udp_key;
  1602. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1603. if (!nla)
  1604. goto nla_put_failure;
  1605. udp_key = nla_data(nla);
  1606. if (swkey->eth.type == htons(ETH_P_IP)) {
  1607. udp_key->udp_src = output->ipv4.tp.src;
  1608. udp_key->udp_dst = output->ipv4.tp.dst;
  1609. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1610. udp_key->udp_src = output->ipv6.tp.src;
  1611. udp_key->udp_dst = output->ipv6.tp.dst;
  1612. }
  1613. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1614. struct ovs_key_sctp *sctp_key;
  1615. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1616. if (!nla)
  1617. goto nla_put_failure;
  1618. sctp_key = nla_data(nla);
  1619. if (swkey->eth.type == htons(ETH_P_IP)) {
  1620. sctp_key->sctp_src = swkey->ipv4.tp.src;
  1621. sctp_key->sctp_dst = swkey->ipv4.tp.dst;
  1622. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1623. sctp_key->sctp_src = swkey->ipv6.tp.src;
  1624. sctp_key->sctp_dst = swkey->ipv6.tp.dst;
  1625. }
  1626. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1627. swkey->ip.proto == IPPROTO_ICMP) {
  1628. struct ovs_key_icmp *icmp_key;
  1629. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1630. if (!nla)
  1631. goto nla_put_failure;
  1632. icmp_key = nla_data(nla);
  1633. icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
  1634. icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
  1635. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1636. swkey->ip.proto == IPPROTO_ICMPV6) {
  1637. struct ovs_key_icmpv6 *icmpv6_key;
  1638. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1639. sizeof(*icmpv6_key));
  1640. if (!nla)
  1641. goto nla_put_failure;
  1642. icmpv6_key = nla_data(nla);
  1643. icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
  1644. icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
  1645. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1646. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1647. struct ovs_key_nd *nd_key;
  1648. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1649. if (!nla)
  1650. goto nla_put_failure;
  1651. nd_key = nla_data(nla);
  1652. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1653. sizeof(nd_key->nd_target));
  1654. memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
  1655. memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
  1656. }
  1657. }
  1658. }
  1659. unencap:
  1660. if (encap)
  1661. nla_nest_end(skb, encap);
  1662. return 0;
  1663. nla_put_failure:
  1664. return -EMSGSIZE;
  1665. }
  1666. /* Initializes the flow module.
  1667. * Returns zero if successful or a negative error code. */
  1668. int ovs_flow_init(void)
  1669. {
  1670. BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
  1671. BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
  1672. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1673. 0, NULL);
  1674. if (flow_cache == NULL)
  1675. return -ENOMEM;
  1676. return 0;
  1677. }
  1678. /* Uninitializes the flow module. */
  1679. void ovs_flow_exit(void)
  1680. {
  1681. kmem_cache_destroy(flow_cache);
  1682. }
  1683. struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
  1684. {
  1685. struct sw_flow_mask *mask;
  1686. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  1687. if (mask)
  1688. mask->ref_count = 0;
  1689. return mask;
  1690. }
  1691. void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
  1692. {
  1693. mask->ref_count++;
  1694. }
  1695. void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
  1696. {
  1697. if (!mask)
  1698. return;
  1699. BUG_ON(!mask->ref_count);
  1700. mask->ref_count--;
  1701. if (!mask->ref_count) {
  1702. list_del_rcu(&mask->list);
  1703. if (deferred)
  1704. kfree_rcu(mask, rcu);
  1705. else
  1706. kfree(mask);
  1707. }
  1708. }
  1709. static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
  1710. const struct sw_flow_mask *b)
  1711. {
  1712. u8 *a_ = (u8 *)&a->key + a->range.start;
  1713. u8 *b_ = (u8 *)&b->key + b->range.start;
  1714. return (a->range.end == b->range.end)
  1715. && (a->range.start == b->range.start)
  1716. && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
  1717. }
  1718. struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
  1719. const struct sw_flow_mask *mask)
  1720. {
  1721. struct list_head *ml;
  1722. list_for_each(ml, tbl->mask_list) {
  1723. struct sw_flow_mask *m;
  1724. m = container_of(ml, struct sw_flow_mask, list);
  1725. if (ovs_sw_flow_mask_equal(mask, m))
  1726. return m;
  1727. }
  1728. return NULL;
  1729. }
  1730. /**
  1731. * add a new mask into the mask list.
  1732. * The caller needs to make sure that 'mask' is not the same
  1733. * as any masks that are already on the list.
  1734. */
  1735. void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
  1736. {
  1737. list_add_rcu(&mask->list, tbl->mask_list);
  1738. }
  1739. /**
  1740. * Set 'range' fields in the mask to the value of 'val'.
  1741. */
  1742. static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
  1743. struct sw_flow_key_range *range, u8 val)
  1744. {
  1745. u8 *m = (u8 *)&mask->key + range->start;
  1746. mask->range = *range;
  1747. memset(m, val, range_n_bytes(range));
  1748. }