flow.c 35 KB

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