flow.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. return llc->ethertype;
  393. }
  394. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  395. int *key_lenp, int nh_len)
  396. {
  397. struct icmp6hdr *icmp = icmp6_hdr(skb);
  398. int error = 0;
  399. int key_len;
  400. /* The ICMPv6 type and code fields use the 16-bit transport port
  401. * fields, so we need to store them in 16-bit network byte order.
  402. */
  403. key->ipv6.tp.src = htons(icmp->icmp6_type);
  404. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  405. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  406. if (icmp->icmp6_code == 0 &&
  407. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  408. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  409. int icmp_len = skb->len - skb_transport_offset(skb);
  410. struct nd_msg *nd;
  411. int offset;
  412. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  413. /* In order to process neighbor discovery options, we need the
  414. * entire packet.
  415. */
  416. if (unlikely(icmp_len < sizeof(*nd)))
  417. goto out;
  418. if (unlikely(skb_linearize(skb))) {
  419. error = -ENOMEM;
  420. goto out;
  421. }
  422. nd = (struct nd_msg *)skb_transport_header(skb);
  423. key->ipv6.nd.target = nd->target;
  424. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  425. icmp_len -= sizeof(*nd);
  426. offset = 0;
  427. while (icmp_len >= 8) {
  428. struct nd_opt_hdr *nd_opt =
  429. (struct nd_opt_hdr *)(nd->opt + offset);
  430. int opt_len = nd_opt->nd_opt_len * 8;
  431. if (unlikely(!opt_len || opt_len > icmp_len))
  432. goto invalid;
  433. /* Store the link layer address if the appropriate
  434. * option is provided. It is considered an error if
  435. * the same link layer option is specified twice.
  436. */
  437. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  438. && opt_len == 8) {
  439. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  440. goto invalid;
  441. memcpy(key->ipv6.nd.sll,
  442. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  443. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  444. && opt_len == 8) {
  445. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  446. goto invalid;
  447. memcpy(key->ipv6.nd.tll,
  448. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  449. }
  450. icmp_len -= opt_len;
  451. offset += opt_len;
  452. }
  453. }
  454. goto out;
  455. invalid:
  456. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  457. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  458. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  459. out:
  460. *key_lenp = key_len;
  461. return error;
  462. }
  463. /**
  464. * ovs_flow_extract - extracts a flow key from an Ethernet frame.
  465. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  466. * Ethernet header
  467. * @in_port: port number on which @skb was received.
  468. * @key: output flow key
  469. * @key_lenp: length of output flow key
  470. *
  471. * The caller must ensure that skb->len >= ETH_HLEN.
  472. *
  473. * Returns 0 if successful, otherwise a negative errno value.
  474. *
  475. * Initializes @skb header pointers as follows:
  476. *
  477. * - skb->mac_header: the Ethernet header.
  478. *
  479. * - skb->network_header: just past the Ethernet header, or just past the
  480. * VLAN header, to the first byte of the Ethernet payload.
  481. *
  482. * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
  483. * on output, then just past the IP header, if one is present and
  484. * of a correct length, otherwise the same as skb->network_header.
  485. * For other key->dl_type values it is left untouched.
  486. */
  487. int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
  488. int *key_lenp)
  489. {
  490. int error = 0;
  491. int key_len = SW_FLOW_KEY_OFFSET(eth);
  492. struct ethhdr *eth;
  493. memset(key, 0, sizeof(*key));
  494. key->phy.priority = skb->priority;
  495. key->phy.in_port = in_port;
  496. key->phy.skb_mark = skb->mark;
  497. skb_reset_mac_header(skb);
  498. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  499. * header in the linear data area.
  500. */
  501. eth = eth_hdr(skb);
  502. memcpy(key->eth.src, eth->h_source, ETH_ALEN);
  503. memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
  504. __skb_pull(skb, 2 * ETH_ALEN);
  505. if (vlan_tx_tag_present(skb))
  506. key->eth.tci = htons(skb->vlan_tci);
  507. else if (eth->h_proto == htons(ETH_P_8021Q))
  508. if (unlikely(parse_vlan(skb, key)))
  509. return -ENOMEM;
  510. key->eth.type = parse_ethertype(skb);
  511. if (unlikely(key->eth.type == htons(0)))
  512. return -ENOMEM;
  513. skb_reset_network_header(skb);
  514. __skb_push(skb, skb->data - skb_mac_header(skb));
  515. /* Network layer. */
  516. if (key->eth.type == htons(ETH_P_IP)) {
  517. struct iphdr *nh;
  518. __be16 offset;
  519. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  520. error = check_iphdr(skb);
  521. if (unlikely(error)) {
  522. if (error == -EINVAL) {
  523. skb->transport_header = skb->network_header;
  524. error = 0;
  525. }
  526. goto out;
  527. }
  528. nh = ip_hdr(skb);
  529. key->ipv4.addr.src = nh->saddr;
  530. key->ipv4.addr.dst = nh->daddr;
  531. key->ip.proto = nh->protocol;
  532. key->ip.tos = nh->tos;
  533. key->ip.ttl = nh->ttl;
  534. offset = nh->frag_off & htons(IP_OFFSET);
  535. if (offset) {
  536. key->ip.frag = OVS_FRAG_TYPE_LATER;
  537. goto out;
  538. }
  539. if (nh->frag_off & htons(IP_MF) ||
  540. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  541. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  542. /* Transport layer. */
  543. if (key->ip.proto == IPPROTO_TCP) {
  544. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  545. if (tcphdr_ok(skb)) {
  546. struct tcphdr *tcp = tcp_hdr(skb);
  547. key->ipv4.tp.src = tcp->source;
  548. key->ipv4.tp.dst = tcp->dest;
  549. }
  550. } else if (key->ip.proto == IPPROTO_UDP) {
  551. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  552. if (udphdr_ok(skb)) {
  553. struct udphdr *udp = udp_hdr(skb);
  554. key->ipv4.tp.src = udp->source;
  555. key->ipv4.tp.dst = udp->dest;
  556. }
  557. } else if (key->ip.proto == IPPROTO_ICMP) {
  558. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  559. if (icmphdr_ok(skb)) {
  560. struct icmphdr *icmp = icmp_hdr(skb);
  561. /* The ICMP type and code fields use the 16-bit
  562. * transport port fields, so we need to store
  563. * them in 16-bit network byte order. */
  564. key->ipv4.tp.src = htons(icmp->type);
  565. key->ipv4.tp.dst = htons(icmp->code);
  566. }
  567. }
  568. } else if ((key->eth.type == htons(ETH_P_ARP) ||
  569. key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
  570. struct arp_eth_header *arp;
  571. arp = (struct arp_eth_header *)skb_network_header(skb);
  572. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  573. && arp->ar_pro == htons(ETH_P_IP)
  574. && arp->ar_hln == ETH_ALEN
  575. && arp->ar_pln == 4) {
  576. /* We only match on the lower 8 bits of the opcode. */
  577. if (ntohs(arp->ar_op) <= 0xff)
  578. key->ip.proto = ntohs(arp->ar_op);
  579. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  580. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  581. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  582. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  583. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  584. }
  585. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  586. int nh_len; /* IPv6 Header + Extensions */
  587. nh_len = parse_ipv6hdr(skb, key, &key_len);
  588. if (unlikely(nh_len < 0)) {
  589. if (nh_len == -EINVAL)
  590. skb->transport_header = skb->network_header;
  591. else
  592. error = nh_len;
  593. goto out;
  594. }
  595. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  596. goto out;
  597. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  598. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  599. /* Transport layer. */
  600. if (key->ip.proto == NEXTHDR_TCP) {
  601. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  602. if (tcphdr_ok(skb)) {
  603. struct tcphdr *tcp = tcp_hdr(skb);
  604. key->ipv6.tp.src = tcp->source;
  605. key->ipv6.tp.dst = tcp->dest;
  606. }
  607. } else if (key->ip.proto == NEXTHDR_UDP) {
  608. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  609. if (udphdr_ok(skb)) {
  610. struct udphdr *udp = udp_hdr(skb);
  611. key->ipv6.tp.src = udp->source;
  612. key->ipv6.tp.dst = udp->dest;
  613. }
  614. } else if (key->ip.proto == NEXTHDR_ICMP) {
  615. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  616. if (icmp6hdr_ok(skb)) {
  617. error = parse_icmpv6(skb, key, &key_len, nh_len);
  618. if (error < 0)
  619. goto out;
  620. }
  621. }
  622. }
  623. out:
  624. *key_lenp = key_len;
  625. return error;
  626. }
  627. u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
  628. {
  629. return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
  630. }
  631. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
  632. struct sw_flow_key *key, int key_len)
  633. {
  634. struct sw_flow *flow;
  635. struct hlist_head *head;
  636. u32 hash;
  637. hash = ovs_flow_hash(key, key_len);
  638. head = find_bucket(table, hash);
  639. hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
  640. if (flow->hash == hash &&
  641. !memcmp(&flow->key, key, key_len)) {
  642. return flow;
  643. }
  644. }
  645. return NULL;
  646. }
  647. void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
  648. {
  649. struct hlist_head *head;
  650. head = find_bucket(table, flow->hash);
  651. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  652. table->count++;
  653. }
  654. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  655. {
  656. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  657. table->count--;
  658. BUG_ON(table->count < 0);
  659. }
  660. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  661. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  662. [OVS_KEY_ATTR_ENCAP] = -1,
  663. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  664. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  665. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  666. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  667. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  668. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  669. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  670. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  671. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  672. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  673. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  674. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  675. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  676. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  677. };
  678. static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  679. const struct nlattr *a[], u32 *attrs)
  680. {
  681. const struct ovs_key_icmp *icmp_key;
  682. const struct ovs_key_tcp *tcp_key;
  683. const struct ovs_key_udp *udp_key;
  684. switch (swkey->ip.proto) {
  685. case IPPROTO_TCP:
  686. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  687. return -EINVAL;
  688. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  689. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  690. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  691. swkey->ipv4.tp.src = tcp_key->tcp_src;
  692. swkey->ipv4.tp.dst = tcp_key->tcp_dst;
  693. break;
  694. case IPPROTO_UDP:
  695. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  696. return -EINVAL;
  697. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  698. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  699. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  700. swkey->ipv4.tp.src = udp_key->udp_src;
  701. swkey->ipv4.tp.dst = udp_key->udp_dst;
  702. break;
  703. case IPPROTO_ICMP:
  704. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
  705. return -EINVAL;
  706. *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  707. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  708. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  709. swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
  710. swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
  711. break;
  712. }
  713. return 0;
  714. }
  715. static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  716. const struct nlattr *a[], u32 *attrs)
  717. {
  718. const struct ovs_key_icmpv6 *icmpv6_key;
  719. const struct ovs_key_tcp *tcp_key;
  720. const struct ovs_key_udp *udp_key;
  721. switch (swkey->ip.proto) {
  722. case IPPROTO_TCP:
  723. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  724. return -EINVAL;
  725. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  726. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  727. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  728. swkey->ipv6.tp.src = tcp_key->tcp_src;
  729. swkey->ipv6.tp.dst = tcp_key->tcp_dst;
  730. break;
  731. case IPPROTO_UDP:
  732. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  733. return -EINVAL;
  734. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  735. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  736. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  737. swkey->ipv6.tp.src = udp_key->udp_src;
  738. swkey->ipv6.tp.dst = udp_key->udp_dst;
  739. break;
  740. case IPPROTO_ICMPV6:
  741. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
  742. return -EINVAL;
  743. *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  744. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  745. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  746. swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
  747. swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
  748. if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  749. swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  750. const struct ovs_key_nd *nd_key;
  751. if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
  752. return -EINVAL;
  753. *attrs &= ~(1 << OVS_KEY_ATTR_ND);
  754. *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  755. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  756. memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
  757. sizeof(swkey->ipv6.nd.target));
  758. memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
  759. memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
  760. }
  761. break;
  762. }
  763. return 0;
  764. }
  765. static int parse_flow_nlattrs(const struct nlattr *attr,
  766. const struct nlattr *a[], u32 *attrsp)
  767. {
  768. const struct nlattr *nla;
  769. u32 attrs;
  770. int rem;
  771. attrs = 0;
  772. nla_for_each_nested(nla, attr, rem) {
  773. u16 type = nla_type(nla);
  774. int expected_len;
  775. if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
  776. return -EINVAL;
  777. expected_len = ovs_key_lens[type];
  778. if (nla_len(nla) != expected_len && expected_len != -1)
  779. return -EINVAL;
  780. attrs |= 1 << type;
  781. a[type] = nla;
  782. }
  783. if (rem)
  784. return -EINVAL;
  785. *attrsp = attrs;
  786. return 0;
  787. }
  788. /**
  789. * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
  790. * @swkey: receives the extracted flow key.
  791. * @key_lenp: number of bytes used in @swkey.
  792. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  793. * sequence.
  794. */
  795. int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
  796. const struct nlattr *attr)
  797. {
  798. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  799. const struct ovs_key_ethernet *eth_key;
  800. int key_len;
  801. u32 attrs;
  802. int err;
  803. memset(swkey, 0, sizeof(struct sw_flow_key));
  804. key_len = SW_FLOW_KEY_OFFSET(eth);
  805. err = parse_flow_nlattrs(attr, a, &attrs);
  806. if (err)
  807. return err;
  808. /* Metadata attributes. */
  809. if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  810. swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
  811. attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  812. }
  813. if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  814. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  815. if (in_port >= DP_MAX_PORTS)
  816. return -EINVAL;
  817. swkey->phy.in_port = in_port;
  818. attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  819. } else {
  820. swkey->phy.in_port = DP_MAX_PORTS;
  821. }
  822. if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  823. swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  824. attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  825. }
  826. /* Data attributes. */
  827. if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
  828. return -EINVAL;
  829. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  830. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  831. memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
  832. memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
  833. if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
  834. nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
  835. const struct nlattr *encap;
  836. __be16 tci;
  837. if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
  838. (1 << OVS_KEY_ATTR_ETHERTYPE) |
  839. (1 << OVS_KEY_ATTR_ENCAP)))
  840. return -EINVAL;
  841. encap = a[OVS_KEY_ATTR_ENCAP];
  842. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  843. if (tci & htons(VLAN_TAG_PRESENT)) {
  844. swkey->eth.tci = tci;
  845. err = parse_flow_nlattrs(encap, a, &attrs);
  846. if (err)
  847. return err;
  848. } else if (!tci) {
  849. /* Corner case for truncated 802.1Q header. */
  850. if (nla_len(encap))
  851. return -EINVAL;
  852. swkey->eth.type = htons(ETH_P_8021Q);
  853. *key_lenp = key_len;
  854. return 0;
  855. } else {
  856. return -EINVAL;
  857. }
  858. }
  859. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  860. swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  861. if (ntohs(swkey->eth.type) < 1536)
  862. return -EINVAL;
  863. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  864. } else {
  865. swkey->eth.type = htons(ETH_P_802_2);
  866. }
  867. if (swkey->eth.type == htons(ETH_P_IP)) {
  868. const struct ovs_key_ipv4 *ipv4_key;
  869. if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
  870. return -EINVAL;
  871. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  872. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  873. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  874. if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
  875. return -EINVAL;
  876. swkey->ip.proto = ipv4_key->ipv4_proto;
  877. swkey->ip.tos = ipv4_key->ipv4_tos;
  878. swkey->ip.ttl = ipv4_key->ipv4_ttl;
  879. swkey->ip.frag = ipv4_key->ipv4_frag;
  880. swkey->ipv4.addr.src = ipv4_key->ipv4_src;
  881. swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
  882. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  883. err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  884. if (err)
  885. return err;
  886. }
  887. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  888. const struct ovs_key_ipv6 *ipv6_key;
  889. if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
  890. return -EINVAL;
  891. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  892. key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
  893. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  894. if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
  895. return -EINVAL;
  896. swkey->ipv6.label = ipv6_key->ipv6_label;
  897. swkey->ip.proto = ipv6_key->ipv6_proto;
  898. swkey->ip.tos = ipv6_key->ipv6_tclass;
  899. swkey->ip.ttl = ipv6_key->ipv6_hlimit;
  900. swkey->ip.frag = ipv6_key->ipv6_frag;
  901. memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
  902. sizeof(swkey->ipv6.addr.src));
  903. memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
  904. sizeof(swkey->ipv6.addr.dst));
  905. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  906. err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  907. if (err)
  908. return err;
  909. }
  910. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  911. swkey->eth.type == htons(ETH_P_RARP)) {
  912. const struct ovs_key_arp *arp_key;
  913. if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
  914. return -EINVAL;
  915. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  916. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  917. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  918. swkey->ipv4.addr.src = arp_key->arp_sip;
  919. swkey->ipv4.addr.dst = arp_key->arp_tip;
  920. if (arp_key->arp_op & htons(0xff00))
  921. return -EINVAL;
  922. swkey->ip.proto = ntohs(arp_key->arp_op);
  923. memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
  924. memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
  925. }
  926. if (attrs)
  927. return -EINVAL;
  928. *key_lenp = key_len;
  929. return 0;
  930. }
  931. /**
  932. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  933. * @priority: receives the skb priority
  934. * @mark: receives the skb mark
  935. * @in_port: receives the extracted input port.
  936. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  937. * sequence.
  938. *
  939. * This parses a series of Netlink attributes that form a flow key, which must
  940. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  941. * get the metadata, that is, the parts of the flow key that cannot be
  942. * extracted from the packet itself.
  943. */
  944. int ovs_flow_metadata_from_nlattrs(u32 *priority, u32 *mark, u16 *in_port,
  945. const struct nlattr *attr)
  946. {
  947. const struct nlattr *nla;
  948. int rem;
  949. *in_port = DP_MAX_PORTS;
  950. *priority = 0;
  951. *mark = 0;
  952. nla_for_each_nested(nla, attr, rem) {
  953. int type = nla_type(nla);
  954. if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
  955. if (nla_len(nla) != ovs_key_lens[type])
  956. return -EINVAL;
  957. switch (type) {
  958. case OVS_KEY_ATTR_PRIORITY:
  959. *priority = nla_get_u32(nla);
  960. break;
  961. case OVS_KEY_ATTR_IN_PORT:
  962. if (nla_get_u32(nla) >= DP_MAX_PORTS)
  963. return -EINVAL;
  964. *in_port = nla_get_u32(nla);
  965. break;
  966. case OVS_KEY_ATTR_SKB_MARK:
  967. *mark = 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. goto nla_put_failure;
  983. if (swkey->phy.in_port != DP_MAX_PORTS &&
  984. nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
  985. goto nla_put_failure;
  986. if (swkey->phy.skb_mark &&
  987. nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
  988. goto nla_put_failure;
  989. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  990. if (!nla)
  991. goto nla_put_failure;
  992. eth_key = nla_data(nla);
  993. memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
  994. memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
  995. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  996. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
  997. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
  998. goto nla_put_failure;
  999. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1000. if (!swkey->eth.tci)
  1001. goto unencap;
  1002. } else {
  1003. encap = NULL;
  1004. }
  1005. if (swkey->eth.type == htons(ETH_P_802_2))
  1006. goto unencap;
  1007. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
  1008. goto nla_put_failure;
  1009. if (swkey->eth.type == htons(ETH_P_IP)) {
  1010. struct ovs_key_ipv4 *ipv4_key;
  1011. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1012. if (!nla)
  1013. goto nla_put_failure;
  1014. ipv4_key = nla_data(nla);
  1015. ipv4_key->ipv4_src = swkey->ipv4.addr.src;
  1016. ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
  1017. ipv4_key->ipv4_proto = swkey->ip.proto;
  1018. ipv4_key->ipv4_tos = swkey->ip.tos;
  1019. ipv4_key->ipv4_ttl = swkey->ip.ttl;
  1020. ipv4_key->ipv4_frag = swkey->ip.frag;
  1021. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1022. struct ovs_key_ipv6 *ipv6_key;
  1023. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1024. if (!nla)
  1025. goto nla_put_failure;
  1026. ipv6_key = nla_data(nla);
  1027. memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
  1028. sizeof(ipv6_key->ipv6_src));
  1029. memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
  1030. sizeof(ipv6_key->ipv6_dst));
  1031. ipv6_key->ipv6_label = swkey->ipv6.label;
  1032. ipv6_key->ipv6_proto = swkey->ip.proto;
  1033. ipv6_key->ipv6_tclass = swkey->ip.tos;
  1034. ipv6_key->ipv6_hlimit = swkey->ip.ttl;
  1035. ipv6_key->ipv6_frag = swkey->ip.frag;
  1036. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1037. swkey->eth.type == htons(ETH_P_RARP)) {
  1038. struct ovs_key_arp *arp_key;
  1039. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1040. if (!nla)
  1041. goto nla_put_failure;
  1042. arp_key = nla_data(nla);
  1043. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1044. arp_key->arp_sip = swkey->ipv4.addr.src;
  1045. arp_key->arp_tip = swkey->ipv4.addr.dst;
  1046. arp_key->arp_op = htons(swkey->ip.proto);
  1047. memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
  1048. memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
  1049. }
  1050. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1051. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1052. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1053. if (swkey->ip.proto == IPPROTO_TCP) {
  1054. struct ovs_key_tcp *tcp_key;
  1055. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1056. if (!nla)
  1057. goto nla_put_failure;
  1058. tcp_key = nla_data(nla);
  1059. if (swkey->eth.type == htons(ETH_P_IP)) {
  1060. tcp_key->tcp_src = swkey->ipv4.tp.src;
  1061. tcp_key->tcp_dst = swkey->ipv4.tp.dst;
  1062. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1063. tcp_key->tcp_src = swkey->ipv6.tp.src;
  1064. tcp_key->tcp_dst = swkey->ipv6.tp.dst;
  1065. }
  1066. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1067. struct ovs_key_udp *udp_key;
  1068. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1069. if (!nla)
  1070. goto nla_put_failure;
  1071. udp_key = nla_data(nla);
  1072. if (swkey->eth.type == htons(ETH_P_IP)) {
  1073. udp_key->udp_src = swkey->ipv4.tp.src;
  1074. udp_key->udp_dst = swkey->ipv4.tp.dst;
  1075. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1076. udp_key->udp_src = swkey->ipv6.tp.src;
  1077. udp_key->udp_dst = swkey->ipv6.tp.dst;
  1078. }
  1079. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1080. swkey->ip.proto == IPPROTO_ICMP) {
  1081. struct ovs_key_icmp *icmp_key;
  1082. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1083. if (!nla)
  1084. goto nla_put_failure;
  1085. icmp_key = nla_data(nla);
  1086. icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
  1087. icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
  1088. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1089. swkey->ip.proto == IPPROTO_ICMPV6) {
  1090. struct ovs_key_icmpv6 *icmpv6_key;
  1091. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1092. sizeof(*icmpv6_key));
  1093. if (!nla)
  1094. goto nla_put_failure;
  1095. icmpv6_key = nla_data(nla);
  1096. icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
  1097. icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
  1098. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1099. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1100. struct ovs_key_nd *nd_key;
  1101. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1102. if (!nla)
  1103. goto nla_put_failure;
  1104. nd_key = nla_data(nla);
  1105. memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
  1106. sizeof(nd_key->nd_target));
  1107. memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
  1108. memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
  1109. }
  1110. }
  1111. }
  1112. unencap:
  1113. if (encap)
  1114. nla_nest_end(skb, encap);
  1115. return 0;
  1116. nla_put_failure:
  1117. return -EMSGSIZE;
  1118. }
  1119. /* Initializes the flow module.
  1120. * Returns zero if successful or a negative error code. */
  1121. int ovs_flow_init(void)
  1122. {
  1123. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1124. 0, NULL);
  1125. if (flow_cache == NULL)
  1126. return -ENOMEM;
  1127. return 0;
  1128. }
  1129. /* Uninitializes the flow module. */
  1130. void ovs_flow_exit(void)
  1131. {
  1132. kmem_cache_destroy(flow_cache);
  1133. }