flow.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Copyright (c) 2007-2011 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/tcp.h>
  37. #include <linux/udp.h>
  38. #include <linux/icmp.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/rculist.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/ndisc.h>
  44. static struct kmem_cache *flow_cache;
  45. static int check_header(struct sk_buff *skb, int len)
  46. {
  47. if (unlikely(skb->len < len))
  48. return -EINVAL;
  49. if (unlikely(!pskb_may_pull(skb, len)))
  50. return -ENOMEM;
  51. return 0;
  52. }
  53. static bool arphdr_ok(struct sk_buff *skb)
  54. {
  55. return pskb_may_pull(skb, skb_network_offset(skb) +
  56. sizeof(struct arp_eth_header));
  57. }
  58. static int check_iphdr(struct sk_buff *skb)
  59. {
  60. unsigned int nh_ofs = skb_network_offset(skb);
  61. unsigned int ip_len;
  62. int err;
  63. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  64. if (unlikely(err))
  65. return err;
  66. ip_len = ip_hdrlen(skb);
  67. if (unlikely(ip_len < sizeof(struct iphdr) ||
  68. skb->len < nh_ofs + ip_len))
  69. return -EINVAL;
  70. skb_set_transport_header(skb, nh_ofs + ip_len);
  71. return 0;
  72. }
  73. static bool tcphdr_ok(struct sk_buff *skb)
  74. {
  75. int th_ofs = skb_transport_offset(skb);
  76. int tcp_len;
  77. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  78. return false;
  79. tcp_len = tcp_hdrlen(skb);
  80. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  81. skb->len < th_ofs + tcp_len))
  82. return false;
  83. return true;
  84. }
  85. static bool udphdr_ok(struct sk_buff *skb)
  86. {
  87. return pskb_may_pull(skb, skb_transport_offset(skb) +
  88. sizeof(struct udphdr));
  89. }
  90. static bool icmphdr_ok(struct sk_buff *skb)
  91. {
  92. return pskb_may_pull(skb, skb_transport_offset(skb) +
  93. sizeof(struct icmphdr));
  94. }
  95. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  96. {
  97. struct timespec cur_ts;
  98. u64 cur_ms, idle_ms;
  99. ktime_get_ts(&cur_ts);
  100. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  101. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  102. cur_ts.tv_nsec / NSEC_PER_MSEC;
  103. return cur_ms - idle_ms;
  104. }
  105. #define SW_FLOW_KEY_OFFSET(field) \
  106. (offsetof(struct sw_flow_key, field) + \
  107. FIELD_SIZEOF(struct sw_flow_key, field))
  108. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
  109. int *key_lenp)
  110. {
  111. unsigned int nh_ofs = skb_network_offset(skb);
  112. unsigned int nh_len;
  113. int payload_ofs;
  114. struct ipv6hdr *nh;
  115. uint8_t nexthdr;
  116. __be16 frag_off;
  117. int err;
  118. *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
  119. err = check_header(skb, nh_ofs + sizeof(*nh));
  120. if (unlikely(err))
  121. return err;
  122. nh = ipv6_hdr(skb);
  123. nexthdr = nh->nexthdr;
  124. payload_ofs = (u8 *)(nh + 1) - skb->data;
  125. key->ip.proto = NEXTHDR_NONE;
  126. key->ip.tos = ipv6_get_dsfield(nh);
  127. key->ip.ttl = nh->hop_limit;
  128. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  129. key->ipv6.addr.src = nh->saddr;
  130. key->ipv6.addr.dst = nh->daddr;
  131. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  132. if (unlikely(payload_ofs < 0))
  133. return -EINVAL;
  134. if (frag_off) {
  135. if (frag_off & htons(~0x7))
  136. key->ip.frag = OVS_FRAG_TYPE_LATER;
  137. else
  138. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  139. }
  140. nh_len = payload_ofs - nh_ofs;
  141. skb_set_transport_header(skb, nh_ofs + nh_len);
  142. key->ip.proto = nexthdr;
  143. return nh_len;
  144. }
  145. static bool icmp6hdr_ok(struct sk_buff *skb)
  146. {
  147. return pskb_may_pull(skb, skb_transport_offset(skb) +
  148. sizeof(struct icmp6hdr));
  149. }
  150. #define TCP_FLAGS_OFFSET 13
  151. #define TCP_FLAG_MASK 0x3f
  152. void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
  153. {
  154. u8 tcp_flags = 0;
  155. if ((flow->key.eth.type == htons(ETH_P_IP) ||
  156. flow->key.eth.type == htons(ETH_P_IPV6)) &&
  157. flow->key.ip.proto == IPPROTO_TCP &&
  158. likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
  159. u8 *tcp = (u8 *)tcp_hdr(skb);
  160. tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
  161. }
  162. spin_lock(&flow->lock);
  163. flow->used = jiffies;
  164. flow->packet_count++;
  165. flow->byte_count += skb->len;
  166. flow->tcp_flags |= tcp_flags;
  167. spin_unlock(&flow->lock);
  168. }
  169. struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
  170. {
  171. int actions_len = nla_len(actions);
  172. struct sw_flow_actions *sfa;
  173. /* At least DP_MAX_PORTS actions are required to be able to flood a
  174. * packet to every port. Factor of 2 allows for setting VLAN tags,
  175. * etc. */
  176. if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
  177. return ERR_PTR(-EINVAL);
  178. sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
  179. if (!sfa)
  180. return ERR_PTR(-ENOMEM);
  181. sfa->actions_len = actions_len;
  182. memcpy(sfa->actions, nla_data(actions), actions_len);
  183. return sfa;
  184. }
  185. struct sw_flow *ovs_flow_alloc(void)
  186. {
  187. struct sw_flow *flow;
  188. flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
  189. if (!flow)
  190. return ERR_PTR(-ENOMEM);
  191. spin_lock_init(&flow->lock);
  192. flow->sf_acts = NULL;
  193. return flow;
  194. }
  195. static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
  196. {
  197. hash = jhash_1word(hash, table->hash_seed);
  198. return flex_array_get(table->buckets,
  199. (hash & (table->n_buckets - 1)));
  200. }
  201. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  202. {
  203. struct flex_array *buckets;
  204. int i, err;
  205. buckets = flex_array_alloc(sizeof(struct hlist_head *),
  206. n_buckets, GFP_KERNEL);
  207. if (!buckets)
  208. return NULL;
  209. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  210. if (err) {
  211. flex_array_free(buckets);
  212. return NULL;
  213. }
  214. for (i = 0; i < n_buckets; i++)
  215. INIT_HLIST_HEAD((struct hlist_head *)
  216. flex_array_get(buckets, i));
  217. return buckets;
  218. }
  219. static void free_buckets(struct flex_array *buckets)
  220. {
  221. flex_array_free(buckets);
  222. }
  223. struct flow_table *ovs_flow_tbl_alloc(int new_size)
  224. {
  225. struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
  226. if (!table)
  227. return NULL;
  228. table->buckets = alloc_buckets(new_size);
  229. if (!table->buckets) {
  230. kfree(table);
  231. return NULL;
  232. }
  233. table->n_buckets = new_size;
  234. table->count = 0;
  235. table->node_ver = 0;
  236. table->keep_flows = false;
  237. get_random_bytes(&table->hash_seed, sizeof(u32));
  238. return table;
  239. }
  240. void ovs_flow_tbl_destroy(struct flow_table *table)
  241. {
  242. int i;
  243. if (!table)
  244. return;
  245. if (table->keep_flows)
  246. goto skip_flows;
  247. for (i = 0; i < table->n_buckets; i++) {
  248. struct sw_flow *flow;
  249. struct hlist_head *head = flex_array_get(table->buckets, i);
  250. struct hlist_node *node, *n;
  251. int ver = table->node_ver;
  252. hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
  253. hlist_del_rcu(&flow->hash_node[ver]);
  254. ovs_flow_free(flow);
  255. }
  256. }
  257. skip_flows:
  258. free_buckets(table->buckets);
  259. kfree(table);
  260. }
  261. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  262. {
  263. struct flow_table *table = container_of(rcu, struct flow_table, rcu);
  264. ovs_flow_tbl_destroy(table);
  265. }
  266. void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
  267. {
  268. if (!table)
  269. return;
  270. call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
  271. }
  272. struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
  273. {
  274. struct sw_flow *flow;
  275. struct hlist_head *head;
  276. struct hlist_node *n;
  277. int ver;
  278. int i;
  279. ver = table->node_ver;
  280. while (*bucket < table->n_buckets) {
  281. i = 0;
  282. head = flex_array_get(table->buckets, *bucket);
  283. hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
  284. if (i < *last) {
  285. i++;
  286. continue;
  287. }
  288. *last = i + 1;
  289. return flow;
  290. }
  291. (*bucket)++;
  292. *last = 0;
  293. }
  294. return NULL;
  295. }
  296. static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
  297. {
  298. int old_ver;
  299. int i;
  300. old_ver = old->node_ver;
  301. new->node_ver = !old_ver;
  302. /* Insert in new table. */
  303. for (i = 0; i < old->n_buckets; i++) {
  304. struct sw_flow *flow;
  305. struct hlist_head *head;
  306. struct hlist_node *n;
  307. head = flex_array_get(old->buckets, i);
  308. hlist_for_each_entry(flow, n, head, hash_node[old_ver])
  309. ovs_flow_tbl_insert(new, flow);
  310. }
  311. old->keep_flows = true;
  312. }
  313. static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
  314. {
  315. struct flow_table *new_table;
  316. new_table = ovs_flow_tbl_alloc(n_buckets);
  317. if (!new_table)
  318. return ERR_PTR(-ENOMEM);
  319. flow_table_copy_flows(table, new_table);
  320. return new_table;
  321. }
  322. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
  323. {
  324. return __flow_tbl_rehash(table, table->n_buckets);
  325. }
  326. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
  327. {
  328. return __flow_tbl_rehash(table, table->n_buckets * 2);
  329. }
  330. void ovs_flow_free(struct sw_flow *flow)
  331. {
  332. if (unlikely(!flow))
  333. return;
  334. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  335. kmem_cache_free(flow_cache, flow);
  336. }
  337. /* RCU callback used by ovs_flow_deferred_free. */
  338. static void rcu_free_flow_callback(struct rcu_head *rcu)
  339. {
  340. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  341. ovs_flow_free(flow);
  342. }
  343. /* Schedules 'flow' to be freed after the next RCU grace period.
  344. * The caller must hold rcu_read_lock for this to be sensible. */
  345. void ovs_flow_deferred_free(struct sw_flow *flow)
  346. {
  347. call_rcu(&flow->rcu, rcu_free_flow_callback);
  348. }
  349. /* RCU callback used by ovs_flow_deferred_free_acts. */
  350. static void rcu_free_acts_callback(struct rcu_head *rcu)
  351. {
  352. struct sw_flow_actions *sf_acts = container_of(rcu,
  353. struct sw_flow_actions, rcu);
  354. kfree(sf_acts);
  355. }
  356. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  357. * The caller must hold rcu_read_lock for this to be sensible. */
  358. void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
  359. {
  360. call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
  361. }
  362. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  363. {
  364. struct qtag_prefix {
  365. __be16 eth_type; /* ETH_P_8021Q */
  366. __be16 tci;
  367. };
  368. struct qtag_prefix *qp;
  369. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  370. return 0;
  371. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  372. sizeof(__be16))))
  373. return -ENOMEM;
  374. qp = (struct qtag_prefix *) skb->data;
  375. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  376. __skb_pull(skb, sizeof(struct qtag_prefix));
  377. return 0;
  378. }
  379. static __be16 parse_ethertype(struct sk_buff *skb)
  380. {
  381. struct llc_snap_hdr {
  382. u8 dsap; /* Always 0xAA */
  383. u8 ssap; /* Always 0xAA */
  384. u8 ctrl;
  385. u8 oui[3];
  386. __be16 ethertype;
  387. };
  388. struct llc_snap_hdr *llc;
  389. __be16 proto;
  390. proto = *(__be16 *) skb->data;
  391. __skb_pull(skb, sizeof(__be16));
  392. if (ntohs(proto) >= 1536)
  393. return proto;
  394. if (skb->len < sizeof(struct llc_snap_hdr))
  395. return htons(ETH_P_802_2);
  396. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  397. return htons(0);
  398. llc = (struct llc_snap_hdr *) skb->data;
  399. if (llc->dsap != LLC_SAP_SNAP ||
  400. llc->ssap != LLC_SAP_SNAP ||
  401. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  402. return htons(ETH_P_802_2);
  403. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  404. return llc->ethertype;
  405. }
  406. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  407. int *key_lenp, int nh_len)
  408. {
  409. struct icmp6hdr *icmp = icmp6_hdr(skb);
  410. int error = 0;
  411. int key_len;
  412. /* The ICMPv6 type and code fields use the 16-bit transport port
  413. * fields, so we need to store them in 16-bit network byte order.
  414. */
  415. key->ipv6.tp.src = htons(icmp->icmp6_type);
  416. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  417. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  418. if (icmp->icmp6_code == 0 &&
  419. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  420. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  421. int icmp_len = skb->len - skb_transport_offset(skb);
  422. struct nd_msg *nd;
  423. int offset;
  424. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  425. /* In order to process neighbor discovery options, we need the
  426. * entire packet.
  427. */
  428. if (unlikely(icmp_len < sizeof(*nd)))
  429. goto out;
  430. if (unlikely(skb_linearize(skb))) {
  431. error = -ENOMEM;
  432. goto out;
  433. }
  434. nd = (struct nd_msg *)skb_transport_header(skb);
  435. key->ipv6.nd.target = nd->target;
  436. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  437. icmp_len -= sizeof(*nd);
  438. offset = 0;
  439. while (icmp_len >= 8) {
  440. struct nd_opt_hdr *nd_opt =
  441. (struct nd_opt_hdr *)(nd->opt + offset);
  442. int opt_len = nd_opt->nd_opt_len * 8;
  443. if (unlikely(!opt_len || opt_len > icmp_len))
  444. goto invalid;
  445. /* Store the link layer address if the appropriate
  446. * option is provided. It is considered an error if
  447. * the same link layer option is specified twice.
  448. */
  449. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  450. && opt_len == 8) {
  451. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  452. goto invalid;
  453. memcpy(key->ipv6.nd.sll,
  454. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  455. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  456. && opt_len == 8) {
  457. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  458. goto invalid;
  459. memcpy(key->ipv6.nd.tll,
  460. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  461. }
  462. icmp_len -= opt_len;
  463. offset += opt_len;
  464. }
  465. }
  466. goto out;
  467. invalid:
  468. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  469. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  470. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  471. out:
  472. *key_lenp = key_len;
  473. return error;
  474. }
  475. /**
  476. * ovs_flow_extract - extracts a flow key from an Ethernet frame.
  477. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  478. * Ethernet header
  479. * @in_port: port number on which @skb was received.
  480. * @key: output flow key
  481. * @key_lenp: length of output flow key
  482. *
  483. * The caller must ensure that skb->len >= ETH_HLEN.
  484. *
  485. * Returns 0 if successful, otherwise a negative errno value.
  486. *
  487. * Initializes @skb header pointers as follows:
  488. *
  489. * - skb->mac_header: the Ethernet header.
  490. *
  491. * - skb->network_header: just past the Ethernet header, or just past the
  492. * VLAN header, to the first byte of the Ethernet payload.
  493. *
  494. * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
  495. * on output, then just past the IP header, if one is present and
  496. * of a correct length, otherwise the same as skb->network_header.
  497. * For other key->dl_type values it is left untouched.
  498. */
  499. int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
  500. int *key_lenp)
  501. {
  502. int error = 0;
  503. int key_len = SW_FLOW_KEY_OFFSET(eth);
  504. struct ethhdr *eth;
  505. memset(key, 0, sizeof(*key));
  506. key->phy.priority = skb->priority;
  507. key->phy.in_port = in_port;
  508. skb_reset_mac_header(skb);
  509. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  510. * header in the linear data area.
  511. */
  512. eth = eth_hdr(skb);
  513. memcpy(key->eth.src, eth->h_source, ETH_ALEN);
  514. memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
  515. __skb_pull(skb, 2 * ETH_ALEN);
  516. if (vlan_tx_tag_present(skb))
  517. key->eth.tci = htons(skb->vlan_tci);
  518. else if (eth->h_proto == htons(ETH_P_8021Q))
  519. if (unlikely(parse_vlan(skb, key)))
  520. return -ENOMEM;
  521. key->eth.type = parse_ethertype(skb);
  522. if (unlikely(key->eth.type == htons(0)))
  523. return -ENOMEM;
  524. skb_reset_network_header(skb);
  525. __skb_push(skb, skb->data - skb_mac_header(skb));
  526. /* Network layer. */
  527. if (key->eth.type == htons(ETH_P_IP)) {
  528. struct iphdr *nh;
  529. __be16 offset;
  530. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  531. error = check_iphdr(skb);
  532. if (unlikely(error)) {
  533. if (error == -EINVAL) {
  534. skb->transport_header = skb->network_header;
  535. error = 0;
  536. }
  537. goto out;
  538. }
  539. nh = ip_hdr(skb);
  540. key->ipv4.addr.src = nh->saddr;
  541. key->ipv4.addr.dst = nh->daddr;
  542. key->ip.proto = nh->protocol;
  543. key->ip.tos = nh->tos;
  544. key->ip.ttl = nh->ttl;
  545. offset = nh->frag_off & htons(IP_OFFSET);
  546. if (offset) {
  547. key->ip.frag = OVS_FRAG_TYPE_LATER;
  548. goto out;
  549. }
  550. if (nh->frag_off & htons(IP_MF) ||
  551. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  552. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  553. /* Transport layer. */
  554. if (key->ip.proto == IPPROTO_TCP) {
  555. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  556. if (tcphdr_ok(skb)) {
  557. struct tcphdr *tcp = tcp_hdr(skb);
  558. key->ipv4.tp.src = tcp->source;
  559. key->ipv4.tp.dst = tcp->dest;
  560. }
  561. } else if (key->ip.proto == IPPROTO_UDP) {
  562. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  563. if (udphdr_ok(skb)) {
  564. struct udphdr *udp = udp_hdr(skb);
  565. key->ipv4.tp.src = udp->source;
  566. key->ipv4.tp.dst = udp->dest;
  567. }
  568. } else if (key->ip.proto == IPPROTO_ICMP) {
  569. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  570. if (icmphdr_ok(skb)) {
  571. struct icmphdr *icmp = icmp_hdr(skb);
  572. /* The ICMP type and code fields use the 16-bit
  573. * transport port fields, so we need to store
  574. * them in 16-bit network byte order. */
  575. key->ipv4.tp.src = htons(icmp->type);
  576. key->ipv4.tp.dst = htons(icmp->code);
  577. }
  578. }
  579. } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
  580. struct arp_eth_header *arp;
  581. arp = (struct arp_eth_header *)skb_network_header(skb);
  582. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  583. && arp->ar_pro == htons(ETH_P_IP)
  584. && arp->ar_hln == ETH_ALEN
  585. && arp->ar_pln == 4) {
  586. /* We only match on the lower 8 bits of the opcode. */
  587. if (ntohs(arp->ar_op) <= 0xff)
  588. key->ip.proto = ntohs(arp->ar_op);
  589. if (key->ip.proto == ARPOP_REQUEST
  590. || key->ip.proto == ARPOP_REPLY) {
  591. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  592. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  593. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  594. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  595. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  596. }
  597. }
  598. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  599. int nh_len; /* IPv6 Header + Extensions */
  600. nh_len = parse_ipv6hdr(skb, key, &key_len);
  601. if (unlikely(nh_len < 0)) {
  602. if (nh_len == -EINVAL)
  603. skb->transport_header = skb->network_header;
  604. else
  605. error = nh_len;
  606. goto out;
  607. }
  608. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  609. goto out;
  610. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  611. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  612. /* Transport layer. */
  613. if (key->ip.proto == NEXTHDR_TCP) {
  614. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  615. if (tcphdr_ok(skb)) {
  616. struct tcphdr *tcp = tcp_hdr(skb);
  617. key->ipv6.tp.src = tcp->source;
  618. key->ipv6.tp.dst = tcp->dest;
  619. }
  620. } else if (key->ip.proto == NEXTHDR_UDP) {
  621. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  622. if (udphdr_ok(skb)) {
  623. struct udphdr *udp = udp_hdr(skb);
  624. key->ipv6.tp.src = udp->source;
  625. key->ipv6.tp.dst = udp->dest;
  626. }
  627. } else if (key->ip.proto == NEXTHDR_ICMP) {
  628. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  629. if (icmp6hdr_ok(skb)) {
  630. error = parse_icmpv6(skb, key, &key_len, nh_len);
  631. if (error < 0)
  632. goto out;
  633. }
  634. }
  635. }
  636. out:
  637. *key_lenp = key_len;
  638. return error;
  639. }
  640. u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
  641. {
  642. return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
  643. }
  644. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
  645. struct sw_flow_key *key, int key_len)
  646. {
  647. struct sw_flow *flow;
  648. struct hlist_node *n;
  649. struct hlist_head *head;
  650. u32 hash;
  651. hash = ovs_flow_hash(key, key_len);
  652. head = find_bucket(table, hash);
  653. hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
  654. if (flow->hash == hash &&
  655. !memcmp(&flow->key, key, key_len)) {
  656. return flow;
  657. }
  658. }
  659. return NULL;
  660. }
  661. void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
  662. {
  663. struct hlist_head *head;
  664. head = find_bucket(table, flow->hash);
  665. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  666. table->count++;
  667. }
  668. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  669. {
  670. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  671. table->count--;
  672. BUG_ON(table->count < 0);
  673. }
  674. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  675. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  676. [OVS_KEY_ATTR_ENCAP] = -1,
  677. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  678. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  679. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  680. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  681. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  682. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  683. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  684. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  685. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  686. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  687. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  688. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  689. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  690. };
  691. static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  692. const struct nlattr *a[], u32 *attrs)
  693. {
  694. const struct ovs_key_icmp *icmp_key;
  695. const struct ovs_key_tcp *tcp_key;
  696. const struct ovs_key_udp *udp_key;
  697. switch (swkey->ip.proto) {
  698. case IPPROTO_TCP:
  699. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  700. return -EINVAL;
  701. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  702. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  703. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  704. swkey->ipv4.tp.src = tcp_key->tcp_src;
  705. swkey->ipv4.tp.dst = tcp_key->tcp_dst;
  706. break;
  707. case IPPROTO_UDP:
  708. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  709. return -EINVAL;
  710. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  711. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  712. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  713. swkey->ipv4.tp.src = udp_key->udp_src;
  714. swkey->ipv4.tp.dst = udp_key->udp_dst;
  715. break;
  716. case IPPROTO_ICMP:
  717. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
  718. return -EINVAL;
  719. *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  720. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  721. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  722. swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
  723. swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
  724. break;
  725. }
  726. return 0;
  727. }
  728. static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  729. const struct nlattr *a[], u32 *attrs)
  730. {
  731. const struct ovs_key_icmpv6 *icmpv6_key;
  732. const struct ovs_key_tcp *tcp_key;
  733. const struct ovs_key_udp *udp_key;
  734. switch (swkey->ip.proto) {
  735. case IPPROTO_TCP:
  736. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  737. return -EINVAL;
  738. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  739. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  740. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  741. swkey->ipv6.tp.src = tcp_key->tcp_src;
  742. swkey->ipv6.tp.dst = tcp_key->tcp_dst;
  743. break;
  744. case IPPROTO_UDP:
  745. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  746. return -EINVAL;
  747. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  748. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  749. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  750. swkey->ipv6.tp.src = udp_key->udp_src;
  751. swkey->ipv6.tp.dst = udp_key->udp_dst;
  752. break;
  753. case IPPROTO_ICMPV6:
  754. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
  755. return -EINVAL;
  756. *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  757. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  758. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  759. swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
  760. swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
  761. if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  762. swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  763. const struct ovs_key_nd *nd_key;
  764. if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
  765. return -EINVAL;
  766. *attrs &= ~(1 << OVS_KEY_ATTR_ND);
  767. *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  768. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  769. memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
  770. sizeof(swkey->ipv6.nd.target));
  771. memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
  772. memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
  773. }
  774. break;
  775. }
  776. return 0;
  777. }
  778. static int parse_flow_nlattrs(const struct nlattr *attr,
  779. const struct nlattr *a[], u32 *attrsp)
  780. {
  781. const struct nlattr *nla;
  782. u32 attrs;
  783. int rem;
  784. attrs = 0;
  785. nla_for_each_nested(nla, attr, rem) {
  786. u16 type = nla_type(nla);
  787. int expected_len;
  788. if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
  789. return -EINVAL;
  790. expected_len = ovs_key_lens[type];
  791. if (nla_len(nla) != expected_len && expected_len != -1)
  792. return -EINVAL;
  793. attrs |= 1 << type;
  794. a[type] = nla;
  795. }
  796. if (rem)
  797. return -EINVAL;
  798. *attrsp = attrs;
  799. return 0;
  800. }
  801. /**
  802. * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
  803. * @swkey: receives the extracted flow key.
  804. * @key_lenp: number of bytes used in @swkey.
  805. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  806. * sequence.
  807. */
  808. int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
  809. const struct nlattr *attr)
  810. {
  811. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  812. const struct ovs_key_ethernet *eth_key;
  813. int key_len;
  814. u32 attrs;
  815. int err;
  816. memset(swkey, 0, sizeof(struct sw_flow_key));
  817. key_len = SW_FLOW_KEY_OFFSET(eth);
  818. err = parse_flow_nlattrs(attr, a, &attrs);
  819. if (err)
  820. return err;
  821. /* Metadata attributes. */
  822. if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  823. swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
  824. attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  825. }
  826. if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  827. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  828. if (in_port >= DP_MAX_PORTS)
  829. return -EINVAL;
  830. swkey->phy.in_port = in_port;
  831. attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  832. } else {
  833. swkey->phy.in_port = USHRT_MAX;
  834. }
  835. /* Data attributes. */
  836. if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
  837. return -EINVAL;
  838. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  839. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  840. memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
  841. memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
  842. if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
  843. nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
  844. const struct nlattr *encap;
  845. __be16 tci;
  846. if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
  847. (1 << OVS_KEY_ATTR_ETHERTYPE) |
  848. (1 << OVS_KEY_ATTR_ENCAP)))
  849. return -EINVAL;
  850. encap = a[OVS_KEY_ATTR_ENCAP];
  851. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  852. if (tci & htons(VLAN_TAG_PRESENT)) {
  853. swkey->eth.tci = tci;
  854. err = parse_flow_nlattrs(encap, a, &attrs);
  855. if (err)
  856. return err;
  857. } else if (!tci) {
  858. /* Corner case for truncated 802.1Q header. */
  859. if (nla_len(encap))
  860. return -EINVAL;
  861. swkey->eth.type = htons(ETH_P_8021Q);
  862. *key_lenp = key_len;
  863. return 0;
  864. } else {
  865. return -EINVAL;
  866. }
  867. }
  868. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  869. swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  870. if (ntohs(swkey->eth.type) < 1536)
  871. return -EINVAL;
  872. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  873. } else {
  874. swkey->eth.type = htons(ETH_P_802_2);
  875. }
  876. if (swkey->eth.type == htons(ETH_P_IP)) {
  877. const struct ovs_key_ipv4 *ipv4_key;
  878. if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
  879. return -EINVAL;
  880. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  881. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  882. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  883. if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
  884. return -EINVAL;
  885. swkey->ip.proto = ipv4_key->ipv4_proto;
  886. swkey->ip.tos = ipv4_key->ipv4_tos;
  887. swkey->ip.ttl = ipv4_key->ipv4_ttl;
  888. swkey->ip.frag = ipv4_key->ipv4_frag;
  889. swkey->ipv4.addr.src = ipv4_key->ipv4_src;
  890. swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
  891. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  892. err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  893. if (err)
  894. return err;
  895. }
  896. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  897. const struct ovs_key_ipv6 *ipv6_key;
  898. if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
  899. return -EINVAL;
  900. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  901. key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
  902. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  903. if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
  904. return -EINVAL;
  905. swkey->ipv6.label = ipv6_key->ipv6_label;
  906. swkey->ip.proto = ipv6_key->ipv6_proto;
  907. swkey->ip.tos = ipv6_key->ipv6_tclass;
  908. swkey->ip.ttl = ipv6_key->ipv6_hlimit;
  909. swkey->ip.frag = ipv6_key->ipv6_frag;
  910. memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
  911. sizeof(swkey->ipv6.addr.src));
  912. memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
  913. sizeof(swkey->ipv6.addr.dst));
  914. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  915. err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  916. if (err)
  917. return err;
  918. }
  919. } else if (swkey->eth.type == htons(ETH_P_ARP)) {
  920. const struct ovs_key_arp *arp_key;
  921. if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
  922. return -EINVAL;
  923. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  924. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  925. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  926. swkey->ipv4.addr.src = arp_key->arp_sip;
  927. swkey->ipv4.addr.dst = arp_key->arp_tip;
  928. if (arp_key->arp_op & htons(0xff00))
  929. return -EINVAL;
  930. swkey->ip.proto = ntohs(arp_key->arp_op);
  931. memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
  932. memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
  933. }
  934. if (attrs)
  935. return -EINVAL;
  936. *key_lenp = key_len;
  937. return 0;
  938. }
  939. /**
  940. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  941. * @in_port: receives the extracted input port.
  942. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  943. * sequence.
  944. *
  945. * This parses a series of Netlink attributes that form a flow key, which must
  946. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  947. * get the metadata, that is, the parts of the flow key that cannot be
  948. * extracted from the packet itself.
  949. */
  950. int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port,
  951. const struct nlattr *attr)
  952. {
  953. const struct nlattr *nla;
  954. int rem;
  955. *in_port = USHRT_MAX;
  956. *priority = 0;
  957. nla_for_each_nested(nla, attr, rem) {
  958. int type = nla_type(nla);
  959. if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
  960. if (nla_len(nla) != ovs_key_lens[type])
  961. return -EINVAL;
  962. switch (type) {
  963. case OVS_KEY_ATTR_PRIORITY:
  964. *priority = nla_get_u32(nla);
  965. break;
  966. case OVS_KEY_ATTR_IN_PORT:
  967. if (nla_get_u32(nla) >= DP_MAX_PORTS)
  968. return -EINVAL;
  969. *in_port = nla_get_u32(nla);
  970. break;
  971. }
  972. }
  973. }
  974. if (rem)
  975. return -EINVAL;
  976. return 0;
  977. }
  978. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
  979. {
  980. struct ovs_key_ethernet *eth_key;
  981. struct nlattr *nla, *encap;
  982. if (swkey->phy.priority &&
  983. nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
  984. goto nla_put_failure;
  985. if (swkey->phy.in_port != USHRT_MAX &&
  986. nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
  987. goto nla_put_failure;
  988. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  989. if (!nla)
  990. goto nla_put_failure;
  991. eth_key = nla_data(nla);
  992. memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
  993. memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
  994. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  995. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
  996. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
  997. goto nla_put_failure;
  998. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  999. if (!swkey->eth.tci)
  1000. goto unencap;
  1001. } else {
  1002. encap = NULL;
  1003. }
  1004. if (swkey->eth.type == htons(ETH_P_802_2))
  1005. goto unencap;
  1006. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
  1007. goto nla_put_failure;
  1008. if (swkey->eth.type == htons(ETH_P_IP)) {
  1009. struct ovs_key_ipv4 *ipv4_key;
  1010. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1011. if (!nla)
  1012. goto nla_put_failure;
  1013. ipv4_key = nla_data(nla);
  1014. ipv4_key->ipv4_src = swkey->ipv4.addr.src;
  1015. ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
  1016. ipv4_key->ipv4_proto = swkey->ip.proto;
  1017. ipv4_key->ipv4_tos = swkey->ip.tos;
  1018. ipv4_key->ipv4_ttl = swkey->ip.ttl;
  1019. ipv4_key->ipv4_frag = swkey->ip.frag;
  1020. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1021. struct ovs_key_ipv6 *ipv6_key;
  1022. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1023. if (!nla)
  1024. goto nla_put_failure;
  1025. ipv6_key = nla_data(nla);
  1026. memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
  1027. sizeof(ipv6_key->ipv6_src));
  1028. memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
  1029. sizeof(ipv6_key->ipv6_dst));
  1030. ipv6_key->ipv6_label = swkey->ipv6.label;
  1031. ipv6_key->ipv6_proto = swkey->ip.proto;
  1032. ipv6_key->ipv6_tclass = swkey->ip.tos;
  1033. ipv6_key->ipv6_hlimit = swkey->ip.ttl;
  1034. ipv6_key->ipv6_frag = swkey->ip.frag;
  1035. } else if (swkey->eth.type == htons(ETH_P_ARP)) {
  1036. struct ovs_key_arp *arp_key;
  1037. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1038. if (!nla)
  1039. goto nla_put_failure;
  1040. arp_key = nla_data(nla);
  1041. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1042. arp_key->arp_sip = swkey->ipv4.addr.src;
  1043. arp_key->arp_tip = swkey->ipv4.addr.dst;
  1044. arp_key->arp_op = htons(swkey->ip.proto);
  1045. memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
  1046. memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
  1047. }
  1048. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1049. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1050. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1051. if (swkey->ip.proto == IPPROTO_TCP) {
  1052. struct ovs_key_tcp *tcp_key;
  1053. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1054. if (!nla)
  1055. goto nla_put_failure;
  1056. tcp_key = nla_data(nla);
  1057. if (swkey->eth.type == htons(ETH_P_IP)) {
  1058. tcp_key->tcp_src = swkey->ipv4.tp.src;
  1059. tcp_key->tcp_dst = swkey->ipv4.tp.dst;
  1060. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1061. tcp_key->tcp_src = swkey->ipv6.tp.src;
  1062. tcp_key->tcp_dst = swkey->ipv6.tp.dst;
  1063. }
  1064. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1065. struct ovs_key_udp *udp_key;
  1066. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1067. if (!nla)
  1068. goto nla_put_failure;
  1069. udp_key = nla_data(nla);
  1070. if (swkey->eth.type == htons(ETH_P_IP)) {
  1071. udp_key->udp_src = swkey->ipv4.tp.src;
  1072. udp_key->udp_dst = swkey->ipv4.tp.dst;
  1073. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1074. udp_key->udp_src = swkey->ipv6.tp.src;
  1075. udp_key->udp_dst = swkey->ipv6.tp.dst;
  1076. }
  1077. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1078. swkey->ip.proto == IPPROTO_ICMP) {
  1079. struct ovs_key_icmp *icmp_key;
  1080. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1081. if (!nla)
  1082. goto nla_put_failure;
  1083. icmp_key = nla_data(nla);
  1084. icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
  1085. icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
  1086. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1087. swkey->ip.proto == IPPROTO_ICMPV6) {
  1088. struct ovs_key_icmpv6 *icmpv6_key;
  1089. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1090. sizeof(*icmpv6_key));
  1091. if (!nla)
  1092. goto nla_put_failure;
  1093. icmpv6_key = nla_data(nla);
  1094. icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
  1095. icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
  1096. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1097. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1098. struct ovs_key_nd *nd_key;
  1099. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1100. if (!nla)
  1101. goto nla_put_failure;
  1102. nd_key = nla_data(nla);
  1103. memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
  1104. sizeof(nd_key->nd_target));
  1105. memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
  1106. memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
  1107. }
  1108. }
  1109. }
  1110. unencap:
  1111. if (encap)
  1112. nla_nest_end(skb, encap);
  1113. return 0;
  1114. nla_put_failure:
  1115. return -EMSGSIZE;
  1116. }
  1117. /* Initializes the flow module.
  1118. * Returns zero if successful or a negative error code. */
  1119. int ovs_flow_init(void)
  1120. {
  1121. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1122. 0, NULL);
  1123. if (flow_cache == NULL)
  1124. return -ENOMEM;
  1125. return 0;
  1126. }
  1127. /* Uninitializes the flow module. */
  1128. void ovs_flow_exit(void)
  1129. {
  1130. kmem_cache_destroy(flow_cache);
  1131. }