flow.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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. }
  979. if (attrs & (1 << type)) {
  980. OVS_NLERR("Duplicate key attribute (type %d).\n", type);
  981. return -EINVAL;
  982. }
  983. expected_len = ovs_key_lens[type];
  984. if (nla_len(nla) != expected_len && expected_len != -1) {
  985. OVS_NLERR("Key attribute has unexpected length (type=%d"
  986. ", length=%d, expected=%d).\n", type,
  987. nla_len(nla), expected_len);
  988. return -EINVAL;
  989. }
  990. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  991. attrs |= 1 << type;
  992. a[type] = nla;
  993. }
  994. }
  995. if (rem) {
  996. OVS_NLERR("Message has %d unknown bytes.\n", rem);
  997. return -EINVAL;
  998. }
  999. *attrsp = attrs;
  1000. return 0;
  1001. }
  1002. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  1003. const struct nlattr *a[], u64 *attrsp)
  1004. {
  1005. return __parse_flow_nlattrs(attr, a, attrsp, true);
  1006. }
  1007. static int parse_flow_nlattrs(const struct nlattr *attr,
  1008. const struct nlattr *a[], u64 *attrsp)
  1009. {
  1010. return __parse_flow_nlattrs(attr, a, attrsp, false);
  1011. }
  1012. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  1013. struct sw_flow_match *match, bool is_mask)
  1014. {
  1015. struct nlattr *a;
  1016. int rem;
  1017. bool ttl = false;
  1018. __be16 tun_flags = 0;
  1019. nla_for_each_nested(a, attr, rem) {
  1020. int type = nla_type(a);
  1021. static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  1022. [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
  1023. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
  1024. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
  1025. [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
  1026. [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
  1027. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
  1028. [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
  1029. };
  1030. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  1031. OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
  1032. type, OVS_TUNNEL_KEY_ATTR_MAX);
  1033. return -EINVAL;
  1034. }
  1035. if (ovs_tunnel_key_lens[type] != nla_len(a)) {
  1036. OVS_NLERR("IPv4 tunnel attribute type has unexpected "
  1037. " length (type=%d, length=%d, expected=%d).\n",
  1038. type, nla_len(a), ovs_tunnel_key_lens[type]);
  1039. return -EINVAL;
  1040. }
  1041. switch (type) {
  1042. case OVS_TUNNEL_KEY_ATTR_ID:
  1043. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  1044. nla_get_be64(a), is_mask);
  1045. tun_flags |= TUNNEL_KEY;
  1046. break;
  1047. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  1048. SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
  1049. nla_get_be32(a), is_mask);
  1050. break;
  1051. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  1052. SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
  1053. nla_get_be32(a), is_mask);
  1054. break;
  1055. case OVS_TUNNEL_KEY_ATTR_TOS:
  1056. SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
  1057. nla_get_u8(a), is_mask);
  1058. break;
  1059. case OVS_TUNNEL_KEY_ATTR_TTL:
  1060. SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
  1061. nla_get_u8(a), is_mask);
  1062. ttl = true;
  1063. break;
  1064. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  1065. tun_flags |= TUNNEL_DONT_FRAGMENT;
  1066. break;
  1067. case OVS_TUNNEL_KEY_ATTR_CSUM:
  1068. tun_flags |= TUNNEL_CSUM;
  1069. break;
  1070. default:
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  1075. if (rem > 0) {
  1076. OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
  1077. return -EINVAL;
  1078. }
  1079. if (!is_mask) {
  1080. if (!match->key->tun_key.ipv4_dst) {
  1081. OVS_NLERR("IPv4 tunnel destination address is zero.\n");
  1082. return -EINVAL;
  1083. }
  1084. if (!ttl) {
  1085. OVS_NLERR("IPv4 tunnel TTL not specified.\n");
  1086. return -EINVAL;
  1087. }
  1088. }
  1089. return 0;
  1090. }
  1091. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  1092. const struct ovs_key_ipv4_tunnel *tun_key,
  1093. const struct ovs_key_ipv4_tunnel *output)
  1094. {
  1095. struct nlattr *nla;
  1096. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  1097. if (!nla)
  1098. return -EMSGSIZE;
  1099. if (output->tun_flags & TUNNEL_KEY &&
  1100. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  1101. return -EMSGSIZE;
  1102. if (output->ipv4_src &&
  1103. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
  1104. return -EMSGSIZE;
  1105. if (output->ipv4_dst &&
  1106. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
  1107. return -EMSGSIZE;
  1108. if (output->ipv4_tos &&
  1109. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  1110. return -EMSGSIZE;
  1111. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  1112. return -EMSGSIZE;
  1113. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  1114. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  1115. return -EMSGSIZE;
  1116. if ((output->tun_flags & TUNNEL_CSUM) &&
  1117. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  1118. return -EMSGSIZE;
  1119. nla_nest_end(skb, nla);
  1120. return 0;
  1121. }
  1122. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  1123. const struct nlattr **a, bool is_mask)
  1124. {
  1125. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  1126. SW_FLOW_KEY_PUT(match, phy.priority,
  1127. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  1128. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  1129. }
  1130. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  1131. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  1132. if (is_mask)
  1133. in_port = 0xffffffff; /* Always exact match in_port. */
  1134. else if (in_port >= DP_MAX_PORTS)
  1135. return -EINVAL;
  1136. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  1137. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  1138. } else if (!is_mask) {
  1139. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  1140. }
  1141. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  1142. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  1143. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  1144. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  1145. }
  1146. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  1147. if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  1148. is_mask))
  1149. return -EINVAL;
  1150. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  1151. }
  1152. return 0;
  1153. }
  1154. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  1155. const struct nlattr **a, bool is_mask)
  1156. {
  1157. int err;
  1158. u64 orig_attrs = attrs;
  1159. err = metadata_from_nlattrs(match, &attrs, a, is_mask);
  1160. if (err)
  1161. return err;
  1162. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  1163. const struct ovs_key_ethernet *eth_key;
  1164. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  1165. SW_FLOW_KEY_MEMCPY(match, eth.src,
  1166. eth_key->eth_src, ETH_ALEN, is_mask);
  1167. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  1168. eth_key->eth_dst, ETH_ALEN, is_mask);
  1169. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  1170. }
  1171. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  1172. __be16 tci;
  1173. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1174. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1175. if (is_mask)
  1176. OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
  1177. else
  1178. OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
  1179. return -EINVAL;
  1180. }
  1181. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  1182. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  1183. } else if (!is_mask)
  1184. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  1185. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  1186. __be16 eth_type;
  1187. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1188. if (is_mask) {
  1189. /* Always exact match EtherType. */
  1190. eth_type = htons(0xffff);
  1191. } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
  1192. OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
  1193. ntohs(eth_type), ETH_P_802_3_MIN);
  1194. return -EINVAL;
  1195. }
  1196. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  1197. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1198. } else if (!is_mask) {
  1199. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  1200. }
  1201. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1202. const struct ovs_key_ipv4 *ipv4_key;
  1203. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  1204. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  1205. OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
  1206. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  1207. return -EINVAL;
  1208. }
  1209. SW_FLOW_KEY_PUT(match, ip.proto,
  1210. ipv4_key->ipv4_proto, is_mask);
  1211. SW_FLOW_KEY_PUT(match, ip.tos,
  1212. ipv4_key->ipv4_tos, is_mask);
  1213. SW_FLOW_KEY_PUT(match, ip.ttl,
  1214. ipv4_key->ipv4_ttl, is_mask);
  1215. SW_FLOW_KEY_PUT(match, ip.frag,
  1216. ipv4_key->ipv4_frag, is_mask);
  1217. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1218. ipv4_key->ipv4_src, is_mask);
  1219. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1220. ipv4_key->ipv4_dst, is_mask);
  1221. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  1222. }
  1223. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  1224. const struct ovs_key_ipv6 *ipv6_key;
  1225. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  1226. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  1227. OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
  1228. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  1229. return -EINVAL;
  1230. }
  1231. SW_FLOW_KEY_PUT(match, ipv6.label,
  1232. ipv6_key->ipv6_label, is_mask);
  1233. SW_FLOW_KEY_PUT(match, ip.proto,
  1234. ipv6_key->ipv6_proto, is_mask);
  1235. SW_FLOW_KEY_PUT(match, ip.tos,
  1236. ipv6_key->ipv6_tclass, is_mask);
  1237. SW_FLOW_KEY_PUT(match, ip.ttl,
  1238. ipv6_key->ipv6_hlimit, is_mask);
  1239. SW_FLOW_KEY_PUT(match, ip.frag,
  1240. ipv6_key->ipv6_frag, is_mask);
  1241. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  1242. ipv6_key->ipv6_src,
  1243. sizeof(match->key->ipv6.addr.src),
  1244. is_mask);
  1245. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  1246. ipv6_key->ipv6_dst,
  1247. sizeof(match->key->ipv6.addr.dst),
  1248. is_mask);
  1249. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1250. }
  1251. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  1252. const struct ovs_key_arp *arp_key;
  1253. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1254. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  1255. OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
  1256. arp_key->arp_op);
  1257. return -EINVAL;
  1258. }
  1259. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1260. arp_key->arp_sip, is_mask);
  1261. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1262. arp_key->arp_tip, is_mask);
  1263. SW_FLOW_KEY_PUT(match, ip.proto,
  1264. ntohs(arp_key->arp_op), is_mask);
  1265. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  1266. arp_key->arp_sha, ETH_ALEN, is_mask);
  1267. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  1268. arp_key->arp_tha, ETH_ALEN, is_mask);
  1269. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1270. }
  1271. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  1272. const struct ovs_key_tcp *tcp_key;
  1273. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  1274. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1275. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1276. tcp_key->tcp_src, is_mask);
  1277. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1278. tcp_key->tcp_dst, is_mask);
  1279. } else {
  1280. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1281. tcp_key->tcp_src, is_mask);
  1282. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1283. tcp_key->tcp_dst, is_mask);
  1284. }
  1285. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  1286. }
  1287. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  1288. const struct ovs_key_udp *udp_key;
  1289. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  1290. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1291. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1292. udp_key->udp_src, is_mask);
  1293. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1294. udp_key->udp_dst, is_mask);
  1295. } else {
  1296. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1297. udp_key->udp_src, is_mask);
  1298. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1299. udp_key->udp_dst, is_mask);
  1300. }
  1301. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  1302. }
  1303. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  1304. const struct ovs_key_sctp *sctp_key;
  1305. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  1306. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1307. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1308. sctp_key->sctp_src, is_mask);
  1309. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1310. sctp_key->sctp_dst, is_mask);
  1311. } else {
  1312. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1313. sctp_key->sctp_src, is_mask);
  1314. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1315. sctp_key->sctp_dst, is_mask);
  1316. }
  1317. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  1318. }
  1319. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  1320. const struct ovs_key_icmp *icmp_key;
  1321. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  1322. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1323. htons(icmp_key->icmp_type), is_mask);
  1324. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1325. htons(icmp_key->icmp_code), is_mask);
  1326. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  1327. }
  1328. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  1329. const struct ovs_key_icmpv6 *icmpv6_key;
  1330. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  1331. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1332. htons(icmpv6_key->icmpv6_type), is_mask);
  1333. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1334. htons(icmpv6_key->icmpv6_code), is_mask);
  1335. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  1336. }
  1337. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  1338. const struct ovs_key_nd *nd_key;
  1339. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  1340. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  1341. nd_key->nd_target,
  1342. sizeof(match->key->ipv6.nd.target),
  1343. is_mask);
  1344. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  1345. nd_key->nd_sll, ETH_ALEN, is_mask);
  1346. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  1347. nd_key->nd_tll, ETH_ALEN, is_mask);
  1348. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  1349. }
  1350. if (attrs != 0)
  1351. return -EINVAL;
  1352. return 0;
  1353. }
  1354. /**
  1355. * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
  1356. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1357. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1358. * does not include any don't care bit.
  1359. * @match: receives the extracted flow match information.
  1360. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1361. * sequence. The fields should of the packet that triggered the creation
  1362. * of this flow.
  1363. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  1364. * attribute specifies the mask field of the wildcarded flow.
  1365. */
  1366. int ovs_match_from_nlattrs(struct sw_flow_match *match,
  1367. const struct nlattr *key,
  1368. const struct nlattr *mask)
  1369. {
  1370. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1371. const struct nlattr *encap;
  1372. u64 key_attrs = 0;
  1373. u64 mask_attrs = 0;
  1374. bool encap_valid = false;
  1375. int err;
  1376. err = parse_flow_nlattrs(key, a, &key_attrs);
  1377. if (err)
  1378. return err;
  1379. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  1380. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  1381. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  1382. __be16 tci;
  1383. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  1384. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  1385. OVS_NLERR("Invalid Vlan frame.\n");
  1386. return -EINVAL;
  1387. }
  1388. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1389. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1390. encap = a[OVS_KEY_ATTR_ENCAP];
  1391. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1392. encap_valid = true;
  1393. if (tci & htons(VLAN_TAG_PRESENT)) {
  1394. err = parse_flow_nlattrs(encap, a, &key_attrs);
  1395. if (err)
  1396. return err;
  1397. } else if (!tci) {
  1398. /* Corner case for truncated 802.1Q header. */
  1399. if (nla_len(encap)) {
  1400. OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
  1401. return -EINVAL;
  1402. }
  1403. } else {
  1404. OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
  1405. return -EINVAL;
  1406. }
  1407. }
  1408. err = ovs_key_from_nlattrs(match, key_attrs, a, false);
  1409. if (err)
  1410. return err;
  1411. if (mask) {
  1412. err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
  1413. if (err)
  1414. return err;
  1415. if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
  1416. __be16 eth_type = 0;
  1417. __be16 tci = 0;
  1418. if (!encap_valid) {
  1419. OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
  1420. return -EINVAL;
  1421. }
  1422. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1423. if (a[OVS_KEY_ATTR_ETHERTYPE])
  1424. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1425. if (eth_type == htons(0xffff)) {
  1426. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1427. encap = a[OVS_KEY_ATTR_ENCAP];
  1428. err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
  1429. } else {
  1430. OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
  1431. ntohs(eth_type));
  1432. return -EINVAL;
  1433. }
  1434. if (a[OVS_KEY_ATTR_VLAN])
  1435. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1436. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1437. OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
  1438. return -EINVAL;
  1439. }
  1440. }
  1441. err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
  1442. if (err)
  1443. return err;
  1444. } else {
  1445. /* Populate exact match flow's key mask. */
  1446. if (match->mask)
  1447. ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
  1448. }
  1449. if (!ovs_match_validate(match, key_attrs, mask_attrs))
  1450. return -EINVAL;
  1451. return 0;
  1452. }
  1453. /**
  1454. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  1455. * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
  1456. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1457. * sequence.
  1458. *
  1459. * This parses a series of Netlink attributes that form a flow key, which must
  1460. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1461. * get the metadata, that is, the parts of the flow key that cannot be
  1462. * extracted from the packet itself.
  1463. */
  1464. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
  1465. const struct nlattr *attr)
  1466. {
  1467. struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
  1468. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1469. u64 attrs = 0;
  1470. int err;
  1471. struct sw_flow_match match;
  1472. flow->key.phy.in_port = DP_MAX_PORTS;
  1473. flow->key.phy.priority = 0;
  1474. flow->key.phy.skb_mark = 0;
  1475. memset(tun_key, 0, sizeof(flow->key.tun_key));
  1476. err = parse_flow_nlattrs(attr, a, &attrs);
  1477. if (err)
  1478. return -EINVAL;
  1479. memset(&match, 0, sizeof(match));
  1480. match.key = &flow->key;
  1481. err = metadata_from_nlattrs(&match, &attrs, a, false);
  1482. if (err)
  1483. return err;
  1484. return 0;
  1485. }
  1486. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
  1487. const struct sw_flow_key *output, struct sk_buff *skb)
  1488. {
  1489. struct ovs_key_ethernet *eth_key;
  1490. struct nlattr *nla, *encap;
  1491. bool is_mask = (swkey != output);
  1492. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1493. goto nla_put_failure;
  1494. if ((swkey->tun_key.ipv4_dst || is_mask) &&
  1495. ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
  1496. goto nla_put_failure;
  1497. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1498. if (is_mask && (output->phy.in_port == 0xffff))
  1499. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1500. goto nla_put_failure;
  1501. } else {
  1502. u16 upper_u16;
  1503. upper_u16 = !is_mask ? 0 : 0xffff;
  1504. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1505. (upper_u16 << 16) | output->phy.in_port))
  1506. goto nla_put_failure;
  1507. }
  1508. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1509. goto nla_put_failure;
  1510. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1511. if (!nla)
  1512. goto nla_put_failure;
  1513. eth_key = nla_data(nla);
  1514. memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
  1515. memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
  1516. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1517. __be16 eth_type;
  1518. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1519. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1520. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1521. goto nla_put_failure;
  1522. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1523. if (!swkey->eth.tci)
  1524. goto unencap;
  1525. } else
  1526. encap = NULL;
  1527. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1528. /*
  1529. * Ethertype 802.2 is represented in the netlink with omitted
  1530. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1531. * 0xffff in the mask attribute. Ethertype can also
  1532. * be wildcarded.
  1533. */
  1534. if (is_mask && output->eth.type)
  1535. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1536. output->eth.type))
  1537. goto nla_put_failure;
  1538. goto unencap;
  1539. }
  1540. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1541. goto nla_put_failure;
  1542. if (swkey->eth.type == htons(ETH_P_IP)) {
  1543. struct ovs_key_ipv4 *ipv4_key;
  1544. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1545. if (!nla)
  1546. goto nla_put_failure;
  1547. ipv4_key = nla_data(nla);
  1548. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1549. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1550. ipv4_key->ipv4_proto = output->ip.proto;
  1551. ipv4_key->ipv4_tos = output->ip.tos;
  1552. ipv4_key->ipv4_ttl = output->ip.ttl;
  1553. ipv4_key->ipv4_frag = output->ip.frag;
  1554. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1555. struct ovs_key_ipv6 *ipv6_key;
  1556. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1557. if (!nla)
  1558. goto nla_put_failure;
  1559. ipv6_key = nla_data(nla);
  1560. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1561. sizeof(ipv6_key->ipv6_src));
  1562. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1563. sizeof(ipv6_key->ipv6_dst));
  1564. ipv6_key->ipv6_label = output->ipv6.label;
  1565. ipv6_key->ipv6_proto = output->ip.proto;
  1566. ipv6_key->ipv6_tclass = output->ip.tos;
  1567. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1568. ipv6_key->ipv6_frag = output->ip.frag;
  1569. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1570. swkey->eth.type == htons(ETH_P_RARP)) {
  1571. struct ovs_key_arp *arp_key;
  1572. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1573. if (!nla)
  1574. goto nla_put_failure;
  1575. arp_key = nla_data(nla);
  1576. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1577. arp_key->arp_sip = output->ipv4.addr.src;
  1578. arp_key->arp_tip = output->ipv4.addr.dst;
  1579. arp_key->arp_op = htons(output->ip.proto);
  1580. memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
  1581. memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
  1582. }
  1583. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1584. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1585. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1586. if (swkey->ip.proto == IPPROTO_TCP) {
  1587. struct ovs_key_tcp *tcp_key;
  1588. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1589. if (!nla)
  1590. goto nla_put_failure;
  1591. tcp_key = nla_data(nla);
  1592. if (swkey->eth.type == htons(ETH_P_IP)) {
  1593. tcp_key->tcp_src = output->ipv4.tp.src;
  1594. tcp_key->tcp_dst = output->ipv4.tp.dst;
  1595. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1596. tcp_key->tcp_src = output->ipv6.tp.src;
  1597. tcp_key->tcp_dst = output->ipv6.tp.dst;
  1598. }
  1599. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1600. struct ovs_key_udp *udp_key;
  1601. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1602. if (!nla)
  1603. goto nla_put_failure;
  1604. udp_key = nla_data(nla);
  1605. if (swkey->eth.type == htons(ETH_P_IP)) {
  1606. udp_key->udp_src = output->ipv4.tp.src;
  1607. udp_key->udp_dst = output->ipv4.tp.dst;
  1608. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1609. udp_key->udp_src = output->ipv6.tp.src;
  1610. udp_key->udp_dst = output->ipv6.tp.dst;
  1611. }
  1612. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1613. struct ovs_key_sctp *sctp_key;
  1614. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1615. if (!nla)
  1616. goto nla_put_failure;
  1617. sctp_key = nla_data(nla);
  1618. if (swkey->eth.type == htons(ETH_P_IP)) {
  1619. sctp_key->sctp_src = swkey->ipv4.tp.src;
  1620. sctp_key->sctp_dst = swkey->ipv4.tp.dst;
  1621. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1622. sctp_key->sctp_src = swkey->ipv6.tp.src;
  1623. sctp_key->sctp_dst = swkey->ipv6.tp.dst;
  1624. }
  1625. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1626. swkey->ip.proto == IPPROTO_ICMP) {
  1627. struct ovs_key_icmp *icmp_key;
  1628. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1629. if (!nla)
  1630. goto nla_put_failure;
  1631. icmp_key = nla_data(nla);
  1632. icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
  1633. icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
  1634. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1635. swkey->ip.proto == IPPROTO_ICMPV6) {
  1636. struct ovs_key_icmpv6 *icmpv6_key;
  1637. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1638. sizeof(*icmpv6_key));
  1639. if (!nla)
  1640. goto nla_put_failure;
  1641. icmpv6_key = nla_data(nla);
  1642. icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
  1643. icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
  1644. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1645. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1646. struct ovs_key_nd *nd_key;
  1647. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1648. if (!nla)
  1649. goto nla_put_failure;
  1650. nd_key = nla_data(nla);
  1651. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1652. sizeof(nd_key->nd_target));
  1653. memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
  1654. memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
  1655. }
  1656. }
  1657. }
  1658. unencap:
  1659. if (encap)
  1660. nla_nest_end(skb, encap);
  1661. return 0;
  1662. nla_put_failure:
  1663. return -EMSGSIZE;
  1664. }
  1665. /* Initializes the flow module.
  1666. * Returns zero if successful or a negative error code. */
  1667. int ovs_flow_init(void)
  1668. {
  1669. BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
  1670. BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
  1671. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1672. 0, NULL);
  1673. if (flow_cache == NULL)
  1674. return -ENOMEM;
  1675. return 0;
  1676. }
  1677. /* Uninitializes the flow module. */
  1678. void ovs_flow_exit(void)
  1679. {
  1680. kmem_cache_destroy(flow_cache);
  1681. }
  1682. struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
  1683. {
  1684. struct sw_flow_mask *mask;
  1685. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  1686. if (mask)
  1687. mask->ref_count = 0;
  1688. return mask;
  1689. }
  1690. void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
  1691. {
  1692. mask->ref_count++;
  1693. }
  1694. void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
  1695. {
  1696. if (!mask)
  1697. return;
  1698. BUG_ON(!mask->ref_count);
  1699. mask->ref_count--;
  1700. if (!mask->ref_count) {
  1701. list_del_rcu(&mask->list);
  1702. if (deferred)
  1703. kfree_rcu(mask, rcu);
  1704. else
  1705. kfree(mask);
  1706. }
  1707. }
  1708. static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
  1709. const struct sw_flow_mask *b)
  1710. {
  1711. u8 *a_ = (u8 *)&a->key + a->range.start;
  1712. u8 *b_ = (u8 *)&b->key + b->range.start;
  1713. return (a->range.end == b->range.end)
  1714. && (a->range.start == b->range.start)
  1715. && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
  1716. }
  1717. struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
  1718. const struct sw_flow_mask *mask)
  1719. {
  1720. struct list_head *ml;
  1721. list_for_each(ml, tbl->mask_list) {
  1722. struct sw_flow_mask *m;
  1723. m = container_of(ml, struct sw_flow_mask, list);
  1724. if (ovs_sw_flow_mask_equal(mask, m))
  1725. return m;
  1726. }
  1727. return NULL;
  1728. }
  1729. /**
  1730. * add a new mask into the mask list.
  1731. * The caller needs to make sure that 'mask' is not the same
  1732. * as any masks that are already on the list.
  1733. */
  1734. void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
  1735. {
  1736. list_add_rcu(&mask->list, tbl->mask_list);
  1737. }
  1738. /**
  1739. * Set 'range' fields in the mask to the value of 'val'.
  1740. */
  1741. static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
  1742. struct sw_flow_key_range *range, u8 val)
  1743. {
  1744. u8 *m = (u8 *)&mask->key + range->start;
  1745. mask->range = *range;
  1746. memset(m, val, range_n_bytes(range));
  1747. }