flow.c 35 KB

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