flow.c 35 KB

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