flow.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 *node, *n;
  248. int ver = table->node_ver;
  249. hlist_for_each_entry_safe(flow, node, 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. struct hlist_node *n;
  274. int ver;
  275. int i;
  276. ver = table->node_ver;
  277. while (*bucket < table->n_buckets) {
  278. i = 0;
  279. head = flex_array_get(table->buckets, *bucket);
  280. hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
  281. if (i < *last) {
  282. i++;
  283. continue;
  284. }
  285. *last = i + 1;
  286. return flow;
  287. }
  288. (*bucket)++;
  289. *last = 0;
  290. }
  291. return NULL;
  292. }
  293. static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
  294. {
  295. int old_ver;
  296. int i;
  297. old_ver = old->node_ver;
  298. new->node_ver = !old_ver;
  299. /* Insert in new table. */
  300. for (i = 0; i < old->n_buckets; i++) {
  301. struct sw_flow *flow;
  302. struct hlist_head *head;
  303. struct hlist_node *n;
  304. head = flex_array_get(old->buckets, i);
  305. hlist_for_each_entry(flow, n, head, hash_node[old_ver])
  306. ovs_flow_tbl_insert(new, flow);
  307. }
  308. old->keep_flows = true;
  309. }
  310. static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
  311. {
  312. struct flow_table *new_table;
  313. new_table = ovs_flow_tbl_alloc(n_buckets);
  314. if (!new_table)
  315. return ERR_PTR(-ENOMEM);
  316. flow_table_copy_flows(table, new_table);
  317. return new_table;
  318. }
  319. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
  320. {
  321. return __flow_tbl_rehash(table, table->n_buckets);
  322. }
  323. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
  324. {
  325. return __flow_tbl_rehash(table, table->n_buckets * 2);
  326. }
  327. void ovs_flow_free(struct sw_flow *flow)
  328. {
  329. if (unlikely(!flow))
  330. return;
  331. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  332. kmem_cache_free(flow_cache, flow);
  333. }
  334. /* RCU callback used by ovs_flow_deferred_free. */
  335. static void rcu_free_flow_callback(struct rcu_head *rcu)
  336. {
  337. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  338. ovs_flow_free(flow);
  339. }
  340. /* Schedules 'flow' to be freed after the next RCU grace period.
  341. * The caller must hold rcu_read_lock for this to be sensible. */
  342. void ovs_flow_deferred_free(struct sw_flow *flow)
  343. {
  344. call_rcu(&flow->rcu, rcu_free_flow_callback);
  345. }
  346. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  347. * The caller must hold rcu_read_lock for this to be sensible. */
  348. void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
  349. {
  350. kfree_rcu(sf_acts, rcu);
  351. }
  352. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  353. {
  354. struct qtag_prefix {
  355. __be16 eth_type; /* ETH_P_8021Q */
  356. __be16 tci;
  357. };
  358. struct qtag_prefix *qp;
  359. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  360. return 0;
  361. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  362. sizeof(__be16))))
  363. return -ENOMEM;
  364. qp = (struct qtag_prefix *) skb->data;
  365. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  366. __skb_pull(skb, sizeof(struct qtag_prefix));
  367. return 0;
  368. }
  369. static __be16 parse_ethertype(struct sk_buff *skb)
  370. {
  371. struct llc_snap_hdr {
  372. u8 dsap; /* Always 0xAA */
  373. u8 ssap; /* Always 0xAA */
  374. u8 ctrl;
  375. u8 oui[3];
  376. __be16 ethertype;
  377. };
  378. struct llc_snap_hdr *llc;
  379. __be16 proto;
  380. proto = *(__be16 *) skb->data;
  381. __skb_pull(skb, sizeof(__be16));
  382. if (ntohs(proto) >= 1536)
  383. return proto;
  384. if (skb->len < sizeof(struct llc_snap_hdr))
  385. return htons(ETH_P_802_2);
  386. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  387. return htons(0);
  388. llc = (struct llc_snap_hdr *) skb->data;
  389. if (llc->dsap != LLC_SAP_SNAP ||
  390. llc->ssap != LLC_SAP_SNAP ||
  391. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  392. return htons(ETH_P_802_2);
  393. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  394. return llc->ethertype;
  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_node *n;
  638. struct hlist_head *head;
  639. u32 hash;
  640. hash = ovs_flow_hash(key, key_len);
  641. head = find_bucket(table, hash);
  642. hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
  643. if (flow->hash == hash &&
  644. !memcmp(&flow->key, key, key_len)) {
  645. return flow;
  646. }
  647. }
  648. return NULL;
  649. }
  650. void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
  651. {
  652. struct hlist_head *head;
  653. head = find_bucket(table, flow->hash);
  654. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  655. table->count++;
  656. }
  657. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  658. {
  659. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  660. table->count--;
  661. BUG_ON(table->count < 0);
  662. }
  663. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  664. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  665. [OVS_KEY_ATTR_ENCAP] = -1,
  666. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  667. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  668. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  669. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  670. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  671. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  672. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  673. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  674. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  675. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  676. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  677. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  678. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  679. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  680. };
  681. static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  682. const struct nlattr *a[], u32 *attrs)
  683. {
  684. const struct ovs_key_icmp *icmp_key;
  685. const struct ovs_key_tcp *tcp_key;
  686. const struct ovs_key_udp *udp_key;
  687. switch (swkey->ip.proto) {
  688. case IPPROTO_TCP:
  689. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  690. return -EINVAL;
  691. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  692. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  693. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  694. swkey->ipv4.tp.src = tcp_key->tcp_src;
  695. swkey->ipv4.tp.dst = tcp_key->tcp_dst;
  696. break;
  697. case IPPROTO_UDP:
  698. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  699. return -EINVAL;
  700. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  701. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  702. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  703. swkey->ipv4.tp.src = udp_key->udp_src;
  704. swkey->ipv4.tp.dst = udp_key->udp_dst;
  705. break;
  706. case IPPROTO_ICMP:
  707. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
  708. return -EINVAL;
  709. *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  710. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  711. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  712. swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
  713. swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
  714. break;
  715. }
  716. return 0;
  717. }
  718. static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  719. const struct nlattr *a[], u32 *attrs)
  720. {
  721. const struct ovs_key_icmpv6 *icmpv6_key;
  722. const struct ovs_key_tcp *tcp_key;
  723. const struct ovs_key_udp *udp_key;
  724. switch (swkey->ip.proto) {
  725. case IPPROTO_TCP:
  726. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  727. return -EINVAL;
  728. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  729. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  730. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  731. swkey->ipv6.tp.src = tcp_key->tcp_src;
  732. swkey->ipv6.tp.dst = tcp_key->tcp_dst;
  733. break;
  734. case IPPROTO_UDP:
  735. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  736. return -EINVAL;
  737. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  738. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  739. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  740. swkey->ipv6.tp.src = udp_key->udp_src;
  741. swkey->ipv6.tp.dst = udp_key->udp_dst;
  742. break;
  743. case IPPROTO_ICMPV6:
  744. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
  745. return -EINVAL;
  746. *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  747. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  748. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  749. swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
  750. swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
  751. if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  752. swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  753. const struct ovs_key_nd *nd_key;
  754. if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
  755. return -EINVAL;
  756. *attrs &= ~(1 << OVS_KEY_ATTR_ND);
  757. *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  758. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  759. memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
  760. sizeof(swkey->ipv6.nd.target));
  761. memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
  762. memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
  763. }
  764. break;
  765. }
  766. return 0;
  767. }
  768. static int parse_flow_nlattrs(const struct nlattr *attr,
  769. const struct nlattr *a[], u32 *attrsp)
  770. {
  771. const struct nlattr *nla;
  772. u32 attrs;
  773. int rem;
  774. attrs = 0;
  775. nla_for_each_nested(nla, attr, rem) {
  776. u16 type = nla_type(nla);
  777. int expected_len;
  778. if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
  779. return -EINVAL;
  780. expected_len = ovs_key_lens[type];
  781. if (nla_len(nla) != expected_len && expected_len != -1)
  782. return -EINVAL;
  783. attrs |= 1 << type;
  784. a[type] = nla;
  785. }
  786. if (rem)
  787. return -EINVAL;
  788. *attrsp = attrs;
  789. return 0;
  790. }
  791. /**
  792. * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
  793. * @swkey: receives the extracted flow key.
  794. * @key_lenp: number of bytes used in @swkey.
  795. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  796. * sequence.
  797. */
  798. int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
  799. const struct nlattr *attr)
  800. {
  801. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  802. const struct ovs_key_ethernet *eth_key;
  803. int key_len;
  804. u32 attrs;
  805. int err;
  806. memset(swkey, 0, sizeof(struct sw_flow_key));
  807. key_len = SW_FLOW_KEY_OFFSET(eth);
  808. err = parse_flow_nlattrs(attr, a, &attrs);
  809. if (err)
  810. return err;
  811. /* Metadata attributes. */
  812. if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  813. swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
  814. attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  815. }
  816. if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  817. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  818. if (in_port >= DP_MAX_PORTS)
  819. return -EINVAL;
  820. swkey->phy.in_port = in_port;
  821. attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  822. } else {
  823. swkey->phy.in_port = DP_MAX_PORTS;
  824. }
  825. if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  826. swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  827. attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  828. }
  829. /* Data attributes. */
  830. if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
  831. return -EINVAL;
  832. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  833. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  834. memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
  835. memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
  836. if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
  837. nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
  838. const struct nlattr *encap;
  839. __be16 tci;
  840. if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
  841. (1 << OVS_KEY_ATTR_ETHERTYPE) |
  842. (1 << OVS_KEY_ATTR_ENCAP)))
  843. return -EINVAL;
  844. encap = a[OVS_KEY_ATTR_ENCAP];
  845. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  846. if (tci & htons(VLAN_TAG_PRESENT)) {
  847. swkey->eth.tci = tci;
  848. err = parse_flow_nlattrs(encap, a, &attrs);
  849. if (err)
  850. return err;
  851. } else if (!tci) {
  852. /* Corner case for truncated 802.1Q header. */
  853. if (nla_len(encap))
  854. return -EINVAL;
  855. swkey->eth.type = htons(ETH_P_8021Q);
  856. *key_lenp = key_len;
  857. return 0;
  858. } else {
  859. return -EINVAL;
  860. }
  861. }
  862. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  863. swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  864. if (ntohs(swkey->eth.type) < 1536)
  865. return -EINVAL;
  866. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  867. } else {
  868. swkey->eth.type = htons(ETH_P_802_2);
  869. }
  870. if (swkey->eth.type == htons(ETH_P_IP)) {
  871. const struct ovs_key_ipv4 *ipv4_key;
  872. if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
  873. return -EINVAL;
  874. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  875. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  876. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  877. if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
  878. return -EINVAL;
  879. swkey->ip.proto = ipv4_key->ipv4_proto;
  880. swkey->ip.tos = ipv4_key->ipv4_tos;
  881. swkey->ip.ttl = ipv4_key->ipv4_ttl;
  882. swkey->ip.frag = ipv4_key->ipv4_frag;
  883. swkey->ipv4.addr.src = ipv4_key->ipv4_src;
  884. swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
  885. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  886. err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  887. if (err)
  888. return err;
  889. }
  890. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  891. const struct ovs_key_ipv6 *ipv6_key;
  892. if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
  893. return -EINVAL;
  894. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  895. key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
  896. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  897. if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
  898. return -EINVAL;
  899. swkey->ipv6.label = ipv6_key->ipv6_label;
  900. swkey->ip.proto = ipv6_key->ipv6_proto;
  901. swkey->ip.tos = ipv6_key->ipv6_tclass;
  902. swkey->ip.ttl = ipv6_key->ipv6_hlimit;
  903. swkey->ip.frag = ipv6_key->ipv6_frag;
  904. memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
  905. sizeof(swkey->ipv6.addr.src));
  906. memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
  907. sizeof(swkey->ipv6.addr.dst));
  908. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  909. err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  910. if (err)
  911. return err;
  912. }
  913. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  914. swkey->eth.type == htons(ETH_P_RARP)) {
  915. const struct ovs_key_arp *arp_key;
  916. if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
  917. return -EINVAL;
  918. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  919. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  920. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  921. swkey->ipv4.addr.src = arp_key->arp_sip;
  922. swkey->ipv4.addr.dst = arp_key->arp_tip;
  923. if (arp_key->arp_op & htons(0xff00))
  924. return -EINVAL;
  925. swkey->ip.proto = ntohs(arp_key->arp_op);
  926. memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
  927. memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
  928. }
  929. if (attrs)
  930. return -EINVAL;
  931. *key_lenp = key_len;
  932. return 0;
  933. }
  934. /**
  935. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  936. * @priority: receives the skb priority
  937. * @mark: receives the skb mark
  938. * @in_port: receives the extracted input port.
  939. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  940. * sequence.
  941. *
  942. * This parses a series of Netlink attributes that form a flow key, which must
  943. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  944. * get the metadata, that is, the parts of the flow key that cannot be
  945. * extracted from the packet itself.
  946. */
  947. int ovs_flow_metadata_from_nlattrs(u32 *priority, u32 *mark, u16 *in_port,
  948. const struct nlattr *attr)
  949. {
  950. const struct nlattr *nla;
  951. int rem;
  952. *in_port = DP_MAX_PORTS;
  953. *priority = 0;
  954. *mark = 0;
  955. nla_for_each_nested(nla, attr, rem) {
  956. int type = nla_type(nla);
  957. if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
  958. if (nla_len(nla) != ovs_key_lens[type])
  959. return -EINVAL;
  960. switch (type) {
  961. case OVS_KEY_ATTR_PRIORITY:
  962. *priority = nla_get_u32(nla);
  963. break;
  964. case OVS_KEY_ATTR_IN_PORT:
  965. if (nla_get_u32(nla) >= DP_MAX_PORTS)
  966. return -EINVAL;
  967. *in_port = nla_get_u32(nla);
  968. break;
  969. case OVS_KEY_ATTR_SKB_MARK:
  970. *mark = nla_get_u32(nla);
  971. break;
  972. }
  973. }
  974. }
  975. if (rem)
  976. return -EINVAL;
  977. return 0;
  978. }
  979. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
  980. {
  981. struct ovs_key_ethernet *eth_key;
  982. struct nlattr *nla, *encap;
  983. if (swkey->phy.priority &&
  984. nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
  985. goto nla_put_failure;
  986. if (swkey->phy.in_port != DP_MAX_PORTS &&
  987. nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
  988. goto nla_put_failure;
  989. if (swkey->phy.skb_mark &&
  990. nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
  991. goto nla_put_failure;
  992. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  993. if (!nla)
  994. goto nla_put_failure;
  995. eth_key = nla_data(nla);
  996. memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
  997. memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
  998. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  999. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
  1000. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
  1001. goto nla_put_failure;
  1002. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1003. if (!swkey->eth.tci)
  1004. goto unencap;
  1005. } else {
  1006. encap = NULL;
  1007. }
  1008. if (swkey->eth.type == htons(ETH_P_802_2))
  1009. goto unencap;
  1010. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
  1011. goto nla_put_failure;
  1012. if (swkey->eth.type == htons(ETH_P_IP)) {
  1013. struct ovs_key_ipv4 *ipv4_key;
  1014. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1015. if (!nla)
  1016. goto nla_put_failure;
  1017. ipv4_key = nla_data(nla);
  1018. ipv4_key->ipv4_src = swkey->ipv4.addr.src;
  1019. ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
  1020. ipv4_key->ipv4_proto = swkey->ip.proto;
  1021. ipv4_key->ipv4_tos = swkey->ip.tos;
  1022. ipv4_key->ipv4_ttl = swkey->ip.ttl;
  1023. ipv4_key->ipv4_frag = swkey->ip.frag;
  1024. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1025. struct ovs_key_ipv6 *ipv6_key;
  1026. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1027. if (!nla)
  1028. goto nla_put_failure;
  1029. ipv6_key = nla_data(nla);
  1030. memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
  1031. sizeof(ipv6_key->ipv6_src));
  1032. memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
  1033. sizeof(ipv6_key->ipv6_dst));
  1034. ipv6_key->ipv6_label = swkey->ipv6.label;
  1035. ipv6_key->ipv6_proto = swkey->ip.proto;
  1036. ipv6_key->ipv6_tclass = swkey->ip.tos;
  1037. ipv6_key->ipv6_hlimit = swkey->ip.ttl;
  1038. ipv6_key->ipv6_frag = swkey->ip.frag;
  1039. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1040. swkey->eth.type == htons(ETH_P_RARP)) {
  1041. struct ovs_key_arp *arp_key;
  1042. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1043. if (!nla)
  1044. goto nla_put_failure;
  1045. arp_key = nla_data(nla);
  1046. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1047. arp_key->arp_sip = swkey->ipv4.addr.src;
  1048. arp_key->arp_tip = swkey->ipv4.addr.dst;
  1049. arp_key->arp_op = htons(swkey->ip.proto);
  1050. memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
  1051. memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
  1052. }
  1053. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1054. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1055. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1056. if (swkey->ip.proto == IPPROTO_TCP) {
  1057. struct ovs_key_tcp *tcp_key;
  1058. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1059. if (!nla)
  1060. goto nla_put_failure;
  1061. tcp_key = nla_data(nla);
  1062. if (swkey->eth.type == htons(ETH_P_IP)) {
  1063. tcp_key->tcp_src = swkey->ipv4.tp.src;
  1064. tcp_key->tcp_dst = swkey->ipv4.tp.dst;
  1065. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1066. tcp_key->tcp_src = swkey->ipv6.tp.src;
  1067. tcp_key->tcp_dst = swkey->ipv6.tp.dst;
  1068. }
  1069. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1070. struct ovs_key_udp *udp_key;
  1071. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1072. if (!nla)
  1073. goto nla_put_failure;
  1074. udp_key = nla_data(nla);
  1075. if (swkey->eth.type == htons(ETH_P_IP)) {
  1076. udp_key->udp_src = swkey->ipv4.tp.src;
  1077. udp_key->udp_dst = swkey->ipv4.tp.dst;
  1078. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1079. udp_key->udp_src = swkey->ipv6.tp.src;
  1080. udp_key->udp_dst = swkey->ipv6.tp.dst;
  1081. }
  1082. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1083. swkey->ip.proto == IPPROTO_ICMP) {
  1084. struct ovs_key_icmp *icmp_key;
  1085. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1086. if (!nla)
  1087. goto nla_put_failure;
  1088. icmp_key = nla_data(nla);
  1089. icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
  1090. icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
  1091. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1092. swkey->ip.proto == IPPROTO_ICMPV6) {
  1093. struct ovs_key_icmpv6 *icmpv6_key;
  1094. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1095. sizeof(*icmpv6_key));
  1096. if (!nla)
  1097. goto nla_put_failure;
  1098. icmpv6_key = nla_data(nla);
  1099. icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
  1100. icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
  1101. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1102. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1103. struct ovs_key_nd *nd_key;
  1104. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1105. if (!nla)
  1106. goto nla_put_failure;
  1107. nd_key = nla_data(nla);
  1108. memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
  1109. sizeof(nd_key->nd_target));
  1110. memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
  1111. memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
  1112. }
  1113. }
  1114. }
  1115. unencap:
  1116. if (encap)
  1117. nla_nest_end(skb, encap);
  1118. return 0;
  1119. nla_put_failure:
  1120. return -EMSGSIZE;
  1121. }
  1122. /* Initializes the flow module.
  1123. * Returns zero if successful or a negative error code. */
  1124. int ovs_flow_init(void)
  1125. {
  1126. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1127. 0, NULL);
  1128. if (flow_cache == NULL)
  1129. return -ENOMEM;
  1130. return 0;
  1131. }
  1132. /* Uninitializes the flow module. */
  1133. void ovs_flow_exit(void)
  1134. {
  1135. kmem_cache_destroy(flow_cache);
  1136. }