flow.c 35 KB

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