flow.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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/ip_tunnels.h>
  43. #include <net/ipv6.h>
  44. #include <net/ndisc.h>
  45. static struct kmem_cache *flow_cache;
  46. static int check_header(struct sk_buff *skb, int len)
  47. {
  48. if (unlikely(skb->len < len))
  49. return -EINVAL;
  50. if (unlikely(!pskb_may_pull(skb, len)))
  51. return -ENOMEM;
  52. return 0;
  53. }
  54. static bool arphdr_ok(struct sk_buff *skb)
  55. {
  56. return pskb_may_pull(skb, skb_network_offset(skb) +
  57. sizeof(struct arp_eth_header));
  58. }
  59. static int check_iphdr(struct sk_buff *skb)
  60. {
  61. unsigned int nh_ofs = skb_network_offset(skb);
  62. unsigned int ip_len;
  63. int err;
  64. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  65. if (unlikely(err))
  66. return err;
  67. ip_len = ip_hdrlen(skb);
  68. if (unlikely(ip_len < sizeof(struct iphdr) ||
  69. skb->len < nh_ofs + ip_len))
  70. return -EINVAL;
  71. skb_set_transport_header(skb, nh_ofs + ip_len);
  72. return 0;
  73. }
  74. static bool tcphdr_ok(struct sk_buff *skb)
  75. {
  76. int th_ofs = skb_transport_offset(skb);
  77. int tcp_len;
  78. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  79. return false;
  80. tcp_len = tcp_hdrlen(skb);
  81. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  82. skb->len < th_ofs + tcp_len))
  83. return false;
  84. return true;
  85. }
  86. static bool udphdr_ok(struct sk_buff *skb)
  87. {
  88. return pskb_may_pull(skb, skb_transport_offset(skb) +
  89. sizeof(struct udphdr));
  90. }
  91. static bool icmphdr_ok(struct sk_buff *skb)
  92. {
  93. return pskb_may_pull(skb, skb_transport_offset(skb) +
  94. sizeof(struct icmphdr));
  95. }
  96. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  97. {
  98. struct timespec cur_ts;
  99. u64 cur_ms, idle_ms;
  100. ktime_get_ts(&cur_ts);
  101. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  102. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  103. cur_ts.tv_nsec / NSEC_PER_MSEC;
  104. return cur_ms - idle_ms;
  105. }
  106. #define SW_FLOW_KEY_OFFSET(field) \
  107. (offsetof(struct sw_flow_key, field) + \
  108. FIELD_SIZEOF(struct sw_flow_key, field))
  109. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
  110. int *key_lenp)
  111. {
  112. unsigned int nh_ofs = skb_network_offset(skb);
  113. unsigned int nh_len;
  114. int payload_ofs;
  115. struct ipv6hdr *nh;
  116. uint8_t nexthdr;
  117. __be16 frag_off;
  118. int err;
  119. *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
  120. err = check_header(skb, nh_ofs + sizeof(*nh));
  121. if (unlikely(err))
  122. return err;
  123. nh = ipv6_hdr(skb);
  124. nexthdr = nh->nexthdr;
  125. payload_ofs = (u8 *)(nh + 1) - skb->data;
  126. key->ip.proto = NEXTHDR_NONE;
  127. key->ip.tos = ipv6_get_dsfield(nh);
  128. key->ip.ttl = nh->hop_limit;
  129. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  130. key->ipv6.addr.src = nh->saddr;
  131. key->ipv6.addr.dst = nh->daddr;
  132. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  133. if (unlikely(payload_ofs < 0))
  134. return -EINVAL;
  135. if (frag_off) {
  136. if (frag_off & htons(~0x7))
  137. key->ip.frag = OVS_FRAG_TYPE_LATER;
  138. else
  139. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  140. }
  141. nh_len = payload_ofs - nh_ofs;
  142. skb_set_transport_header(skb, nh_ofs + nh_len);
  143. key->ip.proto = nexthdr;
  144. return nh_len;
  145. }
  146. static bool icmp6hdr_ok(struct sk_buff *skb)
  147. {
  148. return pskb_may_pull(skb, skb_transport_offset(skb) +
  149. sizeof(struct icmp6hdr));
  150. }
  151. #define TCP_FLAGS_OFFSET 13
  152. #define TCP_FLAG_MASK 0x3f
  153. void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
  154. {
  155. u8 tcp_flags = 0;
  156. if ((flow->key.eth.type == htons(ETH_P_IP) ||
  157. flow->key.eth.type == htons(ETH_P_IPV6)) &&
  158. flow->key.ip.proto == IPPROTO_TCP &&
  159. likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
  160. u8 *tcp = (u8 *)tcp_hdr(skb);
  161. tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
  162. }
  163. spin_lock(&flow->lock);
  164. flow->used = jiffies;
  165. flow->packet_count++;
  166. flow->byte_count += skb->len;
  167. flow->tcp_flags |= tcp_flags;
  168. spin_unlock(&flow->lock);
  169. }
  170. struct sw_flow_actions *ovs_flow_actions_alloc(int size)
  171. {
  172. struct sw_flow_actions *sfa;
  173. if (size > MAX_ACTIONS_BUFSIZE)
  174. return ERR_PTR(-EINVAL);
  175. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  176. if (!sfa)
  177. return ERR_PTR(-ENOMEM);
  178. sfa->actions_len = 0;
  179. return sfa;
  180. }
  181. struct sw_flow *ovs_flow_alloc(void)
  182. {
  183. struct sw_flow *flow;
  184. flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
  185. if (!flow)
  186. return ERR_PTR(-ENOMEM);
  187. spin_lock_init(&flow->lock);
  188. flow->sf_acts = NULL;
  189. return flow;
  190. }
  191. static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
  192. {
  193. hash = jhash_1word(hash, table->hash_seed);
  194. return flex_array_get(table->buckets,
  195. (hash & (table->n_buckets - 1)));
  196. }
  197. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  198. {
  199. struct flex_array *buckets;
  200. int i, err;
  201. buckets = flex_array_alloc(sizeof(struct hlist_head),
  202. n_buckets, GFP_KERNEL);
  203. if (!buckets)
  204. return NULL;
  205. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  206. if (err) {
  207. flex_array_free(buckets);
  208. return NULL;
  209. }
  210. for (i = 0; i < n_buckets; i++)
  211. INIT_HLIST_HEAD((struct hlist_head *)
  212. flex_array_get(buckets, i));
  213. return buckets;
  214. }
  215. static void free_buckets(struct flex_array *buckets)
  216. {
  217. flex_array_free(buckets);
  218. }
  219. struct flow_table *ovs_flow_tbl_alloc(int new_size)
  220. {
  221. struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
  222. if (!table)
  223. return NULL;
  224. table->buckets = alloc_buckets(new_size);
  225. if (!table->buckets) {
  226. kfree(table);
  227. return NULL;
  228. }
  229. table->n_buckets = new_size;
  230. table->count = 0;
  231. table->node_ver = 0;
  232. table->keep_flows = false;
  233. get_random_bytes(&table->hash_seed, sizeof(u32));
  234. return table;
  235. }
  236. void ovs_flow_tbl_destroy(struct flow_table *table)
  237. {
  238. int i;
  239. if (!table)
  240. return;
  241. if (table->keep_flows)
  242. goto skip_flows;
  243. for (i = 0; i < table->n_buckets; i++) {
  244. struct sw_flow *flow;
  245. struct hlist_head *head = flex_array_get(table->buckets, i);
  246. struct hlist_node *n;
  247. int ver = table->node_ver;
  248. hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
  249. hlist_del_rcu(&flow->hash_node[ver]);
  250. ovs_flow_free(flow);
  251. }
  252. }
  253. skip_flows:
  254. free_buckets(table->buckets);
  255. kfree(table);
  256. }
  257. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  258. {
  259. struct flow_table *table = container_of(rcu, struct flow_table, rcu);
  260. ovs_flow_tbl_destroy(table);
  261. }
  262. void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
  263. {
  264. if (!table)
  265. return;
  266. call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
  267. }
  268. struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
  269. {
  270. struct sw_flow *flow;
  271. struct hlist_head *head;
  272. int ver;
  273. int i;
  274. ver = table->node_ver;
  275. while (*bucket < table->n_buckets) {
  276. i = 0;
  277. head = flex_array_get(table->buckets, *bucket);
  278. hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
  279. if (i < *last) {
  280. i++;
  281. continue;
  282. }
  283. *last = i + 1;
  284. return flow;
  285. }
  286. (*bucket)++;
  287. *last = 0;
  288. }
  289. return NULL;
  290. }
  291. static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
  292. {
  293. struct hlist_head *head;
  294. head = find_bucket(table, flow->hash);
  295. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  296. table->count++;
  297. }
  298. static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
  299. {
  300. int old_ver;
  301. int i;
  302. old_ver = old->node_ver;
  303. new->node_ver = !old_ver;
  304. /* Insert in new table. */
  305. for (i = 0; i < old->n_buckets; i++) {
  306. struct sw_flow *flow;
  307. struct hlist_head *head;
  308. head = flex_array_get(old->buckets, i);
  309. hlist_for_each_entry(flow, head, hash_node[old_ver])
  310. __flow_tbl_insert(new, flow);
  311. }
  312. old->keep_flows = true;
  313. }
  314. static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
  315. {
  316. struct flow_table *new_table;
  317. new_table = ovs_flow_tbl_alloc(n_buckets);
  318. if (!new_table)
  319. return ERR_PTR(-ENOMEM);
  320. flow_table_copy_flows(table, new_table);
  321. return new_table;
  322. }
  323. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
  324. {
  325. return __flow_tbl_rehash(table, table->n_buckets);
  326. }
  327. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
  328. {
  329. return __flow_tbl_rehash(table, table->n_buckets * 2);
  330. }
  331. void ovs_flow_free(struct sw_flow *flow)
  332. {
  333. if (unlikely(!flow))
  334. return;
  335. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  336. kmem_cache_free(flow_cache, flow);
  337. }
  338. /* RCU callback used by ovs_flow_deferred_free. */
  339. static void rcu_free_flow_callback(struct rcu_head *rcu)
  340. {
  341. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  342. ovs_flow_free(flow);
  343. }
  344. /* Schedules 'flow' 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(struct sw_flow *flow)
  347. {
  348. call_rcu(&flow->rcu, rcu_free_flow_callback);
  349. }
  350. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  351. * The caller must hold rcu_read_lock for this to be sensible. */
  352. void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
  353. {
  354. kfree_rcu(sf_acts, rcu);
  355. }
  356. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  357. {
  358. struct qtag_prefix {
  359. __be16 eth_type; /* ETH_P_8021Q */
  360. __be16 tci;
  361. };
  362. struct qtag_prefix *qp;
  363. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  364. return 0;
  365. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  366. sizeof(__be16))))
  367. return -ENOMEM;
  368. qp = (struct qtag_prefix *) skb->data;
  369. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  370. __skb_pull(skb, sizeof(struct qtag_prefix));
  371. return 0;
  372. }
  373. static __be16 parse_ethertype(struct sk_buff *skb)
  374. {
  375. struct llc_snap_hdr {
  376. u8 dsap; /* Always 0xAA */
  377. u8 ssap; /* Always 0xAA */
  378. u8 ctrl;
  379. u8 oui[3];
  380. __be16 ethertype;
  381. };
  382. struct llc_snap_hdr *llc;
  383. __be16 proto;
  384. proto = *(__be16 *) skb->data;
  385. __skb_pull(skb, sizeof(__be16));
  386. if (ntohs(proto) >= ETH_P_802_3_MIN)
  387. return proto;
  388. if (skb->len < sizeof(struct llc_snap_hdr))
  389. return htons(ETH_P_802_2);
  390. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  391. return htons(0);
  392. llc = (struct llc_snap_hdr *) skb->data;
  393. if (llc->dsap != LLC_SAP_SNAP ||
  394. llc->ssap != LLC_SAP_SNAP ||
  395. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  396. return htons(ETH_P_802_2);
  397. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  398. if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
  399. return llc->ethertype;
  400. return htons(ETH_P_802_2);
  401. }
  402. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  403. int *key_lenp, int nh_len)
  404. {
  405. struct icmp6hdr *icmp = icmp6_hdr(skb);
  406. int error = 0;
  407. int key_len;
  408. /* The ICMPv6 type and code fields use the 16-bit transport port
  409. * fields, so we need to store them in 16-bit network byte order.
  410. */
  411. key->ipv6.tp.src = htons(icmp->icmp6_type);
  412. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  413. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  414. if (icmp->icmp6_code == 0 &&
  415. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  416. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  417. int icmp_len = skb->len - skb_transport_offset(skb);
  418. struct nd_msg *nd;
  419. int offset;
  420. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  421. /* In order to process neighbor discovery options, we need the
  422. * entire packet.
  423. */
  424. if (unlikely(icmp_len < sizeof(*nd)))
  425. goto out;
  426. if (unlikely(skb_linearize(skb))) {
  427. error = -ENOMEM;
  428. goto out;
  429. }
  430. nd = (struct nd_msg *)skb_transport_header(skb);
  431. key->ipv6.nd.target = nd->target;
  432. key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  433. icmp_len -= sizeof(*nd);
  434. offset = 0;
  435. while (icmp_len >= 8) {
  436. struct nd_opt_hdr *nd_opt =
  437. (struct nd_opt_hdr *)(nd->opt + offset);
  438. int opt_len = nd_opt->nd_opt_len * 8;
  439. if (unlikely(!opt_len || opt_len > icmp_len))
  440. goto invalid;
  441. /* Store the link layer address if the appropriate
  442. * option is provided. It is considered an error if
  443. * the same link layer option is specified twice.
  444. */
  445. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  446. && opt_len == 8) {
  447. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  448. goto invalid;
  449. memcpy(key->ipv6.nd.sll,
  450. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  451. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  452. && opt_len == 8) {
  453. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  454. goto invalid;
  455. memcpy(key->ipv6.nd.tll,
  456. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  457. }
  458. icmp_len -= opt_len;
  459. offset += opt_len;
  460. }
  461. }
  462. goto out;
  463. invalid:
  464. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  465. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  466. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  467. out:
  468. *key_lenp = key_len;
  469. return error;
  470. }
  471. /**
  472. * ovs_flow_extract - extracts a flow key from an Ethernet frame.
  473. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  474. * Ethernet header
  475. * @in_port: port number on which @skb was received.
  476. * @key: output flow key
  477. * @key_lenp: length of output flow key
  478. *
  479. * The caller must ensure that skb->len >= ETH_HLEN.
  480. *
  481. * Returns 0 if successful, otherwise a negative errno value.
  482. *
  483. * Initializes @skb header pointers as follows:
  484. *
  485. * - skb->mac_header: the Ethernet header.
  486. *
  487. * - skb->network_header: just past the Ethernet header, or just past the
  488. * VLAN header, to the first byte of the Ethernet payload.
  489. *
  490. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  491. * on output, then just past the IP header, if one is present and
  492. * of a correct length, otherwise the same as skb->network_header.
  493. * For other key->eth.type values it is left untouched.
  494. */
  495. int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
  496. int *key_lenp)
  497. {
  498. int error = 0;
  499. int key_len = SW_FLOW_KEY_OFFSET(eth);
  500. struct ethhdr *eth;
  501. memset(key, 0, sizeof(*key));
  502. key->phy.priority = skb->priority;
  503. if (OVS_CB(skb)->tun_key)
  504. memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
  505. key->phy.in_port = in_port;
  506. key->phy.skb_mark = skb->mark;
  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. /* We are going to push all headers that we pull, so no need to
  516. * update skb->csum here.
  517. */
  518. if (vlan_tx_tag_present(skb))
  519. key->eth.tci = htons(skb->vlan_tci);
  520. else if (eth->h_proto == htons(ETH_P_8021Q))
  521. if (unlikely(parse_vlan(skb, key)))
  522. return -ENOMEM;
  523. key->eth.type = parse_ethertype(skb);
  524. if (unlikely(key->eth.type == htons(0)))
  525. return -ENOMEM;
  526. skb_reset_network_header(skb);
  527. __skb_push(skb, skb->data - skb_mac_header(skb));
  528. /* Network layer. */
  529. if (key->eth.type == htons(ETH_P_IP)) {
  530. struct iphdr *nh;
  531. __be16 offset;
  532. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  533. error = check_iphdr(skb);
  534. if (unlikely(error)) {
  535. if (error == -EINVAL) {
  536. skb->transport_header = skb->network_header;
  537. error = 0;
  538. }
  539. goto out;
  540. }
  541. nh = ip_hdr(skb);
  542. key->ipv4.addr.src = nh->saddr;
  543. key->ipv4.addr.dst = nh->daddr;
  544. key->ip.proto = nh->protocol;
  545. key->ip.tos = nh->tos;
  546. key->ip.ttl = nh->ttl;
  547. offset = nh->frag_off & htons(IP_OFFSET);
  548. if (offset) {
  549. key->ip.frag = OVS_FRAG_TYPE_LATER;
  550. goto out;
  551. }
  552. if (nh->frag_off & htons(IP_MF) ||
  553. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  554. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  555. /* Transport layer. */
  556. if (key->ip.proto == IPPROTO_TCP) {
  557. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  558. if (tcphdr_ok(skb)) {
  559. struct tcphdr *tcp = tcp_hdr(skb);
  560. key->ipv4.tp.src = tcp->source;
  561. key->ipv4.tp.dst = tcp->dest;
  562. }
  563. } else if (key->ip.proto == IPPROTO_UDP) {
  564. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  565. if (udphdr_ok(skb)) {
  566. struct udphdr *udp = udp_hdr(skb);
  567. key->ipv4.tp.src = udp->source;
  568. key->ipv4.tp.dst = udp->dest;
  569. }
  570. } else if (key->ip.proto == IPPROTO_ICMP) {
  571. key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  572. if (icmphdr_ok(skb)) {
  573. struct icmphdr *icmp = icmp_hdr(skb);
  574. /* The ICMP type and code fields use the 16-bit
  575. * transport port fields, so we need to store
  576. * them in 16-bit network byte order. */
  577. key->ipv4.tp.src = htons(icmp->type);
  578. key->ipv4.tp.dst = htons(icmp->code);
  579. }
  580. }
  581. } else if ((key->eth.type == htons(ETH_P_ARP) ||
  582. key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
  583. struct arp_eth_header *arp;
  584. arp = (struct arp_eth_header *)skb_network_header(skb);
  585. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  586. && arp->ar_pro == htons(ETH_P_IP)
  587. && arp->ar_hln == ETH_ALEN
  588. && arp->ar_pln == 4) {
  589. /* We only match on the lower 8 bits of the opcode. */
  590. if (ntohs(arp->ar_op) <= 0xff)
  591. key->ip.proto = ntohs(arp->ar_op);
  592. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  593. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  594. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  595. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  596. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  597. }
  598. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  599. int nh_len; /* IPv6 Header + Extensions */
  600. nh_len = parse_ipv6hdr(skb, key, &key_len);
  601. if (unlikely(nh_len < 0)) {
  602. if (nh_len == -EINVAL)
  603. skb->transport_header = skb->network_header;
  604. else
  605. error = nh_len;
  606. goto out;
  607. }
  608. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  609. goto out;
  610. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  611. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  612. /* Transport layer. */
  613. if (key->ip.proto == NEXTHDR_TCP) {
  614. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  615. if (tcphdr_ok(skb)) {
  616. struct tcphdr *tcp = tcp_hdr(skb);
  617. key->ipv6.tp.src = tcp->source;
  618. key->ipv6.tp.dst = tcp->dest;
  619. }
  620. } else if (key->ip.proto == NEXTHDR_UDP) {
  621. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  622. if (udphdr_ok(skb)) {
  623. struct udphdr *udp = udp_hdr(skb);
  624. key->ipv6.tp.src = udp->source;
  625. key->ipv6.tp.dst = udp->dest;
  626. }
  627. } else if (key->ip.proto == NEXTHDR_ICMP) {
  628. key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  629. if (icmp6hdr_ok(skb)) {
  630. error = parse_icmpv6(skb, key, &key_len, nh_len);
  631. if (error < 0)
  632. goto out;
  633. }
  634. }
  635. }
  636. out:
  637. *key_lenp = key_len;
  638. return error;
  639. }
  640. static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
  641. {
  642. return jhash2((u32 *)((u8 *)key + key_start),
  643. DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
  644. }
  645. static int flow_key_start(struct sw_flow_key *key)
  646. {
  647. if (key->tun_key.ipv4_dst)
  648. return 0;
  649. else
  650. return offsetof(struct sw_flow_key, phy);
  651. }
  652. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
  653. struct sw_flow_key *key, int key_len)
  654. {
  655. struct sw_flow *flow;
  656. struct hlist_head *head;
  657. u8 *_key;
  658. int key_start;
  659. u32 hash;
  660. key_start = flow_key_start(key);
  661. hash = ovs_flow_hash(key, key_start, key_len);
  662. _key = (u8 *) key + key_start;
  663. head = find_bucket(table, hash);
  664. hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
  665. if (flow->hash == hash &&
  666. !memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) {
  667. return flow;
  668. }
  669. }
  670. return NULL;
  671. }
  672. void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
  673. struct sw_flow_key *key, int key_len)
  674. {
  675. flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len);
  676. memcpy(&flow->key, key, sizeof(flow->key));
  677. __flow_tbl_insert(table, flow);
  678. }
  679. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  680. {
  681. BUG_ON(table->count == 0);
  682. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  683. table->count--;
  684. }
  685. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  686. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  687. [OVS_KEY_ATTR_ENCAP] = -1,
  688. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  689. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  690. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  691. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  692. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  693. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  694. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  695. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  696. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  697. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  698. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  699. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  700. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  701. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  702. [OVS_KEY_ATTR_TUNNEL] = -1,
  703. };
  704. static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  705. const struct nlattr *a[], u32 *attrs)
  706. {
  707. const struct ovs_key_icmp *icmp_key;
  708. const struct ovs_key_tcp *tcp_key;
  709. const struct ovs_key_udp *udp_key;
  710. switch (swkey->ip.proto) {
  711. case IPPROTO_TCP:
  712. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  713. return -EINVAL;
  714. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  715. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  716. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  717. swkey->ipv4.tp.src = tcp_key->tcp_src;
  718. swkey->ipv4.tp.dst = tcp_key->tcp_dst;
  719. break;
  720. case IPPROTO_UDP:
  721. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  722. return -EINVAL;
  723. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  724. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  725. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  726. swkey->ipv4.tp.src = udp_key->udp_src;
  727. swkey->ipv4.tp.dst = udp_key->udp_dst;
  728. break;
  729. case IPPROTO_ICMP:
  730. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
  731. return -EINVAL;
  732. *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  733. *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
  734. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  735. swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
  736. swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
  737. break;
  738. }
  739. return 0;
  740. }
  741. static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
  742. const struct nlattr *a[], u32 *attrs)
  743. {
  744. const struct ovs_key_icmpv6 *icmpv6_key;
  745. const struct ovs_key_tcp *tcp_key;
  746. const struct ovs_key_udp *udp_key;
  747. switch (swkey->ip.proto) {
  748. case IPPROTO_TCP:
  749. if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
  750. return -EINVAL;
  751. *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  752. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  753. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  754. swkey->ipv6.tp.src = tcp_key->tcp_src;
  755. swkey->ipv6.tp.dst = tcp_key->tcp_dst;
  756. break;
  757. case IPPROTO_UDP:
  758. if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
  759. return -EINVAL;
  760. *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  761. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  762. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  763. swkey->ipv6.tp.src = udp_key->udp_src;
  764. swkey->ipv6.tp.dst = udp_key->udp_dst;
  765. break;
  766. case IPPROTO_ICMPV6:
  767. if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
  768. return -EINVAL;
  769. *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  770. *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
  771. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  772. swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
  773. swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
  774. if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  775. swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  776. const struct ovs_key_nd *nd_key;
  777. if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
  778. return -EINVAL;
  779. *attrs &= ~(1 << OVS_KEY_ATTR_ND);
  780. *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
  781. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  782. memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
  783. sizeof(swkey->ipv6.nd.target));
  784. memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
  785. memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
  786. }
  787. break;
  788. }
  789. return 0;
  790. }
  791. static int parse_flow_nlattrs(const struct nlattr *attr,
  792. const struct nlattr *a[], u32 *attrsp)
  793. {
  794. const struct nlattr *nla;
  795. u32 attrs;
  796. int rem;
  797. attrs = 0;
  798. nla_for_each_nested(nla, attr, rem) {
  799. u16 type = nla_type(nla);
  800. int expected_len;
  801. if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
  802. return -EINVAL;
  803. expected_len = ovs_key_lens[type];
  804. if (nla_len(nla) != expected_len && expected_len != -1)
  805. return -EINVAL;
  806. attrs |= 1 << type;
  807. a[type] = nla;
  808. }
  809. if (rem)
  810. return -EINVAL;
  811. *attrsp = attrs;
  812. return 0;
  813. }
  814. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  815. struct ovs_key_ipv4_tunnel *tun_key)
  816. {
  817. struct nlattr *a;
  818. int rem;
  819. bool ttl = false;
  820. memset(tun_key, 0, sizeof(*tun_key));
  821. nla_for_each_nested(a, attr, rem) {
  822. int type = nla_type(a);
  823. static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  824. [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
  825. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
  826. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
  827. [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
  828. [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
  829. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
  830. [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
  831. };
  832. if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
  833. ovs_tunnel_key_lens[type] != nla_len(a))
  834. return -EINVAL;
  835. switch (type) {
  836. case OVS_TUNNEL_KEY_ATTR_ID:
  837. tun_key->tun_id = nla_get_be64(a);
  838. tun_key->tun_flags |= TUNNEL_KEY;
  839. break;
  840. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  841. tun_key->ipv4_src = nla_get_be32(a);
  842. break;
  843. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  844. tun_key->ipv4_dst = nla_get_be32(a);
  845. break;
  846. case OVS_TUNNEL_KEY_ATTR_TOS:
  847. tun_key->ipv4_tos = nla_get_u8(a);
  848. break;
  849. case OVS_TUNNEL_KEY_ATTR_TTL:
  850. tun_key->ipv4_ttl = nla_get_u8(a);
  851. ttl = true;
  852. break;
  853. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  854. tun_key->tun_flags |= TUNNEL_DONT_FRAGMENT;
  855. break;
  856. case OVS_TUNNEL_KEY_ATTR_CSUM:
  857. tun_key->tun_flags |= TUNNEL_CSUM;
  858. break;
  859. default:
  860. return -EINVAL;
  861. }
  862. }
  863. if (rem > 0)
  864. return -EINVAL;
  865. if (!tun_key->ipv4_dst)
  866. return -EINVAL;
  867. if (!ttl)
  868. return -EINVAL;
  869. return 0;
  870. }
  871. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  872. const struct ovs_key_ipv4_tunnel *tun_key)
  873. {
  874. struct nlattr *nla;
  875. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  876. if (!nla)
  877. return -EMSGSIZE;
  878. if (tun_key->tun_flags & TUNNEL_KEY &&
  879. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id))
  880. return -EMSGSIZE;
  881. if (tun_key->ipv4_src &&
  882. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src))
  883. return -EMSGSIZE;
  884. if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst))
  885. return -EMSGSIZE;
  886. if (tun_key->ipv4_tos &&
  887. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos))
  888. return -EMSGSIZE;
  889. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl))
  890. return -EMSGSIZE;
  891. if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  892. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  893. return -EMSGSIZE;
  894. if ((tun_key->tun_flags & TUNNEL_CSUM) &&
  895. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  896. return -EMSGSIZE;
  897. nla_nest_end(skb, nla);
  898. return 0;
  899. }
  900. /**
  901. * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
  902. * @swkey: receives the extracted flow key.
  903. * @key_lenp: number of bytes used in @swkey.
  904. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  905. * sequence.
  906. */
  907. int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
  908. const struct nlattr *attr)
  909. {
  910. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  911. const struct ovs_key_ethernet *eth_key;
  912. int key_len;
  913. u32 attrs;
  914. int err;
  915. memset(swkey, 0, sizeof(struct sw_flow_key));
  916. key_len = SW_FLOW_KEY_OFFSET(eth);
  917. err = parse_flow_nlattrs(attr, a, &attrs);
  918. if (err)
  919. return err;
  920. /* Metadata attributes. */
  921. if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  922. swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
  923. attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  924. }
  925. if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  926. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  927. if (in_port >= DP_MAX_PORTS)
  928. return -EINVAL;
  929. swkey->phy.in_port = in_port;
  930. attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  931. } else {
  932. swkey->phy.in_port = DP_MAX_PORTS;
  933. }
  934. if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  935. swkey->phy.skb_mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  936. attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  937. }
  938. if (attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  939. err = ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
  940. if (err)
  941. return err;
  942. attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  943. }
  944. /* Data attributes. */
  945. if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
  946. return -EINVAL;
  947. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  948. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  949. memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
  950. memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
  951. if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
  952. nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
  953. const struct nlattr *encap;
  954. __be16 tci;
  955. if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
  956. (1 << OVS_KEY_ATTR_ETHERTYPE) |
  957. (1 << OVS_KEY_ATTR_ENCAP)))
  958. return -EINVAL;
  959. encap = a[OVS_KEY_ATTR_ENCAP];
  960. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  961. if (tci & htons(VLAN_TAG_PRESENT)) {
  962. swkey->eth.tci = tci;
  963. err = parse_flow_nlattrs(encap, a, &attrs);
  964. if (err)
  965. return err;
  966. } else if (!tci) {
  967. /* Corner case for truncated 802.1Q header. */
  968. if (nla_len(encap))
  969. return -EINVAL;
  970. swkey->eth.type = htons(ETH_P_8021Q);
  971. *key_lenp = key_len;
  972. return 0;
  973. } else {
  974. return -EINVAL;
  975. }
  976. }
  977. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  978. swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  979. if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
  980. return -EINVAL;
  981. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  982. } else {
  983. swkey->eth.type = htons(ETH_P_802_2);
  984. }
  985. if (swkey->eth.type == htons(ETH_P_IP)) {
  986. const struct ovs_key_ipv4 *ipv4_key;
  987. if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
  988. return -EINVAL;
  989. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  990. key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
  991. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  992. if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
  993. return -EINVAL;
  994. swkey->ip.proto = ipv4_key->ipv4_proto;
  995. swkey->ip.tos = ipv4_key->ipv4_tos;
  996. swkey->ip.ttl = ipv4_key->ipv4_ttl;
  997. swkey->ip.frag = ipv4_key->ipv4_frag;
  998. swkey->ipv4.addr.src = ipv4_key->ipv4_src;
  999. swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
  1000. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1001. err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  1002. if (err)
  1003. return err;
  1004. }
  1005. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1006. const struct ovs_key_ipv6 *ipv6_key;
  1007. if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
  1008. return -EINVAL;
  1009. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1010. key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
  1011. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  1012. if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
  1013. return -EINVAL;
  1014. swkey->ipv6.label = ipv6_key->ipv6_label;
  1015. swkey->ip.proto = ipv6_key->ipv6_proto;
  1016. swkey->ip.tos = ipv6_key->ipv6_tclass;
  1017. swkey->ip.ttl = ipv6_key->ipv6_hlimit;
  1018. swkey->ip.frag = ipv6_key->ipv6_frag;
  1019. memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
  1020. sizeof(swkey->ipv6.addr.src));
  1021. memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
  1022. sizeof(swkey->ipv6.addr.dst));
  1023. if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1024. err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
  1025. if (err)
  1026. return err;
  1027. }
  1028. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1029. swkey->eth.type == htons(ETH_P_RARP)) {
  1030. const struct ovs_key_arp *arp_key;
  1031. if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
  1032. return -EINVAL;
  1033. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1034. key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
  1035. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1036. swkey->ipv4.addr.src = arp_key->arp_sip;
  1037. swkey->ipv4.addr.dst = arp_key->arp_tip;
  1038. if (arp_key->arp_op & htons(0xff00))
  1039. return -EINVAL;
  1040. swkey->ip.proto = ntohs(arp_key->arp_op);
  1041. memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
  1042. memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
  1043. }
  1044. if (attrs)
  1045. return -EINVAL;
  1046. *key_lenp = key_len;
  1047. return 0;
  1048. }
  1049. /**
  1050. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  1051. * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
  1052. * @key_len: Length of key in @flow. Used for calculating flow hash.
  1053. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1054. * sequence.
  1055. *
  1056. * This parses a series of Netlink attributes that form a flow key, which must
  1057. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1058. * get the metadata, that is, the parts of the flow key that cannot be
  1059. * extracted from the packet itself.
  1060. */
  1061. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len,
  1062. const struct nlattr *attr)
  1063. {
  1064. struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
  1065. const struct nlattr *nla;
  1066. int rem;
  1067. flow->key.phy.in_port = DP_MAX_PORTS;
  1068. flow->key.phy.priority = 0;
  1069. flow->key.phy.skb_mark = 0;
  1070. memset(tun_key, 0, sizeof(flow->key.tun_key));
  1071. nla_for_each_nested(nla, attr, rem) {
  1072. int type = nla_type(nla);
  1073. if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
  1074. int err;
  1075. if (nla_len(nla) != ovs_key_lens[type])
  1076. return -EINVAL;
  1077. switch (type) {
  1078. case OVS_KEY_ATTR_PRIORITY:
  1079. flow->key.phy.priority = nla_get_u32(nla);
  1080. break;
  1081. case OVS_KEY_ATTR_TUNNEL:
  1082. err = ovs_ipv4_tun_from_nlattr(nla, tun_key);
  1083. if (err)
  1084. return err;
  1085. break;
  1086. case OVS_KEY_ATTR_IN_PORT:
  1087. if (nla_get_u32(nla) >= DP_MAX_PORTS)
  1088. return -EINVAL;
  1089. flow->key.phy.in_port = nla_get_u32(nla);
  1090. break;
  1091. case OVS_KEY_ATTR_SKB_MARK:
  1092. flow->key.phy.skb_mark = nla_get_u32(nla);
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. if (rem)
  1098. return -EINVAL;
  1099. flow->hash = ovs_flow_hash(&flow->key,
  1100. flow_key_start(&flow->key), key_len);
  1101. return 0;
  1102. }
  1103. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
  1104. {
  1105. struct ovs_key_ethernet *eth_key;
  1106. struct nlattr *nla, *encap;
  1107. if (swkey->phy.priority &&
  1108. nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
  1109. goto nla_put_failure;
  1110. if (swkey->tun_key.ipv4_dst &&
  1111. ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key))
  1112. goto nla_put_failure;
  1113. if (swkey->phy.in_port != DP_MAX_PORTS &&
  1114. nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
  1115. goto nla_put_failure;
  1116. if (swkey->phy.skb_mark &&
  1117. nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
  1118. goto nla_put_failure;
  1119. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1120. if (!nla)
  1121. goto nla_put_failure;
  1122. eth_key = nla_data(nla);
  1123. memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
  1124. memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
  1125. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1126. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
  1127. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
  1128. goto nla_put_failure;
  1129. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1130. if (!swkey->eth.tci)
  1131. goto unencap;
  1132. } else {
  1133. encap = NULL;
  1134. }
  1135. if (swkey->eth.type == htons(ETH_P_802_2))
  1136. goto unencap;
  1137. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
  1138. goto nla_put_failure;
  1139. if (swkey->eth.type == htons(ETH_P_IP)) {
  1140. struct ovs_key_ipv4 *ipv4_key;
  1141. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1142. if (!nla)
  1143. goto nla_put_failure;
  1144. ipv4_key = nla_data(nla);
  1145. ipv4_key->ipv4_src = swkey->ipv4.addr.src;
  1146. ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
  1147. ipv4_key->ipv4_proto = swkey->ip.proto;
  1148. ipv4_key->ipv4_tos = swkey->ip.tos;
  1149. ipv4_key->ipv4_ttl = swkey->ip.ttl;
  1150. ipv4_key->ipv4_frag = swkey->ip.frag;
  1151. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1152. struct ovs_key_ipv6 *ipv6_key;
  1153. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1154. if (!nla)
  1155. goto nla_put_failure;
  1156. ipv6_key = nla_data(nla);
  1157. memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
  1158. sizeof(ipv6_key->ipv6_src));
  1159. memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
  1160. sizeof(ipv6_key->ipv6_dst));
  1161. ipv6_key->ipv6_label = swkey->ipv6.label;
  1162. ipv6_key->ipv6_proto = swkey->ip.proto;
  1163. ipv6_key->ipv6_tclass = swkey->ip.tos;
  1164. ipv6_key->ipv6_hlimit = swkey->ip.ttl;
  1165. ipv6_key->ipv6_frag = swkey->ip.frag;
  1166. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1167. swkey->eth.type == htons(ETH_P_RARP)) {
  1168. struct ovs_key_arp *arp_key;
  1169. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1170. if (!nla)
  1171. goto nla_put_failure;
  1172. arp_key = nla_data(nla);
  1173. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1174. arp_key->arp_sip = swkey->ipv4.addr.src;
  1175. arp_key->arp_tip = swkey->ipv4.addr.dst;
  1176. arp_key->arp_op = htons(swkey->ip.proto);
  1177. memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
  1178. memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
  1179. }
  1180. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1181. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1182. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1183. if (swkey->ip.proto == IPPROTO_TCP) {
  1184. struct ovs_key_tcp *tcp_key;
  1185. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1186. if (!nla)
  1187. goto nla_put_failure;
  1188. tcp_key = nla_data(nla);
  1189. if (swkey->eth.type == htons(ETH_P_IP)) {
  1190. tcp_key->tcp_src = swkey->ipv4.tp.src;
  1191. tcp_key->tcp_dst = swkey->ipv4.tp.dst;
  1192. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1193. tcp_key->tcp_src = swkey->ipv6.tp.src;
  1194. tcp_key->tcp_dst = swkey->ipv6.tp.dst;
  1195. }
  1196. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1197. struct ovs_key_udp *udp_key;
  1198. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1199. if (!nla)
  1200. goto nla_put_failure;
  1201. udp_key = nla_data(nla);
  1202. if (swkey->eth.type == htons(ETH_P_IP)) {
  1203. udp_key->udp_src = swkey->ipv4.tp.src;
  1204. udp_key->udp_dst = swkey->ipv4.tp.dst;
  1205. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1206. udp_key->udp_src = swkey->ipv6.tp.src;
  1207. udp_key->udp_dst = swkey->ipv6.tp.dst;
  1208. }
  1209. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1210. swkey->ip.proto == IPPROTO_ICMP) {
  1211. struct ovs_key_icmp *icmp_key;
  1212. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1213. if (!nla)
  1214. goto nla_put_failure;
  1215. icmp_key = nla_data(nla);
  1216. icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
  1217. icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
  1218. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1219. swkey->ip.proto == IPPROTO_ICMPV6) {
  1220. struct ovs_key_icmpv6 *icmpv6_key;
  1221. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1222. sizeof(*icmpv6_key));
  1223. if (!nla)
  1224. goto nla_put_failure;
  1225. icmpv6_key = nla_data(nla);
  1226. icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
  1227. icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
  1228. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1229. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1230. struct ovs_key_nd *nd_key;
  1231. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1232. if (!nla)
  1233. goto nla_put_failure;
  1234. nd_key = nla_data(nla);
  1235. memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
  1236. sizeof(nd_key->nd_target));
  1237. memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
  1238. memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
  1239. }
  1240. }
  1241. }
  1242. unencap:
  1243. if (encap)
  1244. nla_nest_end(skb, encap);
  1245. return 0;
  1246. nla_put_failure:
  1247. return -EMSGSIZE;
  1248. }
  1249. /* Initializes the flow module.
  1250. * Returns zero if successful or a negative error code. */
  1251. int ovs_flow_init(void)
  1252. {
  1253. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1254. 0, NULL);
  1255. if (flow_cache == NULL)
  1256. return -ENOMEM;
  1257. return 0;
  1258. }
  1259. /* Uninitializes the flow module. */
  1260. void ovs_flow_exit(void)
  1261. {
  1262. kmem_cache_destroy(flow_cache);
  1263. }