flow.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * Copyright (c) 2007-2013 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/sctp.h>
  37. #include <linux/tcp.h>
  38. #include <linux/udp.h>
  39. #include <linux/icmp.h>
  40. #include <linux/icmpv6.h>
  41. #include <linux/rculist.h>
  42. #include <net/ip.h>
  43. #include <net/ip_tunnels.h>
  44. #include <net/ipv6.h>
  45. #include <net/ndisc.h>
  46. static struct kmem_cache *flow_cache;
  47. static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
  48. struct sw_flow_key_range *range, u8 val);
  49. static void update_range__(struct sw_flow_match *match,
  50. size_t offset, size_t size, bool is_mask)
  51. {
  52. struct sw_flow_key_range *range = NULL;
  53. size_t start = offset;
  54. size_t end = offset + size;
  55. if (!is_mask)
  56. range = &match->range;
  57. else if (match->mask)
  58. range = &match->mask->range;
  59. if (!range)
  60. return;
  61. if (range->start == range->end) {
  62. range->start = start;
  63. range->end = end;
  64. return;
  65. }
  66. if (range->start > start)
  67. range->start = start;
  68. if (range->end < end)
  69. range->end = end;
  70. }
  71. #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
  72. do { \
  73. update_range__(match, offsetof(struct sw_flow_key, field), \
  74. sizeof((match)->key->field), is_mask); \
  75. if (is_mask) { \
  76. if ((match)->mask) \
  77. (match)->mask->key.field = value; \
  78. } else { \
  79. (match)->key->field = value; \
  80. } \
  81. } while (0)
  82. #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
  83. do { \
  84. update_range__(match, offsetof(struct sw_flow_key, field), \
  85. len, is_mask); \
  86. if (is_mask) { \
  87. if ((match)->mask) \
  88. memcpy(&(match)->mask->key.field, value_p, len);\
  89. } else { \
  90. memcpy(&(match)->key->field, value_p, len); \
  91. } \
  92. } while (0)
  93. void ovs_match_init(struct sw_flow_match *match,
  94. struct sw_flow_key *key,
  95. struct sw_flow_mask *mask)
  96. {
  97. memset(match, 0, sizeof(*match));
  98. match->key = key;
  99. match->mask = mask;
  100. memset(key, 0, sizeof(*key));
  101. if (mask) {
  102. memset(&mask->key, 0, sizeof(mask->key));
  103. mask->range.start = mask->range.end = 0;
  104. }
  105. }
  106. static bool ovs_match_validate(const struct sw_flow_match *match,
  107. u64 key_attrs, u64 mask_attrs)
  108. {
  109. u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
  110. u64 mask_allowed = key_attrs; /* At most allow all key attributes */
  111. /* The following mask attributes allowed only if they
  112. * pass the validation tests. */
  113. mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
  114. | (1 << OVS_KEY_ATTR_IPV6)
  115. | (1 << OVS_KEY_ATTR_TCP)
  116. | (1 << OVS_KEY_ATTR_UDP)
  117. | (1 << OVS_KEY_ATTR_SCTP)
  118. | (1 << OVS_KEY_ATTR_ICMP)
  119. | (1 << OVS_KEY_ATTR_ICMPV6)
  120. | (1 << OVS_KEY_ATTR_ARP)
  121. | (1 << OVS_KEY_ATTR_ND));
  122. /* Always allowed mask fields. */
  123. mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
  124. | (1 << OVS_KEY_ATTR_IN_PORT)
  125. | (1 << OVS_KEY_ATTR_ETHERTYPE));
  126. /* Check key attributes. */
  127. if (match->key->eth.type == htons(ETH_P_ARP)
  128. || match->key->eth.type == htons(ETH_P_RARP)) {
  129. key_expected |= 1 << OVS_KEY_ATTR_ARP;
  130. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  131. mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
  132. }
  133. if (match->key->eth.type == htons(ETH_P_IP)) {
  134. key_expected |= 1 << OVS_KEY_ATTR_IPV4;
  135. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  136. mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
  137. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  138. if (match->key->ip.proto == IPPROTO_UDP) {
  139. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  140. if (match->mask && (match->mask->key.ip.proto == 0xff))
  141. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  142. }
  143. if (match->key->ip.proto == IPPROTO_SCTP) {
  144. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  145. if (match->mask && (match->mask->key.ip.proto == 0xff))
  146. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  147. }
  148. if (match->key->ip.proto == IPPROTO_TCP) {
  149. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  150. if (match->mask && (match->mask->key.ip.proto == 0xff))
  151. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  152. }
  153. if (match->key->ip.proto == IPPROTO_ICMP) {
  154. key_expected |= 1 << OVS_KEY_ATTR_ICMP;
  155. if (match->mask && (match->mask->key.ip.proto == 0xff))
  156. mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
  157. }
  158. }
  159. }
  160. if (match->key->eth.type == htons(ETH_P_IPV6)) {
  161. key_expected |= 1 << OVS_KEY_ATTR_IPV6;
  162. if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
  163. mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
  164. if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
  165. if (match->key->ip.proto == IPPROTO_UDP) {
  166. key_expected |= 1 << OVS_KEY_ATTR_UDP;
  167. if (match->mask && (match->mask->key.ip.proto == 0xff))
  168. mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
  169. }
  170. if (match->key->ip.proto == IPPROTO_SCTP) {
  171. key_expected |= 1 << OVS_KEY_ATTR_SCTP;
  172. if (match->mask && (match->mask->key.ip.proto == 0xff))
  173. mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
  174. }
  175. if (match->key->ip.proto == IPPROTO_TCP) {
  176. key_expected |= 1 << OVS_KEY_ATTR_TCP;
  177. if (match->mask && (match->mask->key.ip.proto == 0xff))
  178. mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
  179. }
  180. if (match->key->ip.proto == IPPROTO_ICMPV6) {
  181. key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
  182. if (match->mask && (match->mask->key.ip.proto == 0xff))
  183. mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
  184. if (match->key->ipv6.tp.src ==
  185. htons(NDISC_NEIGHBOUR_SOLICITATION) ||
  186. match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  187. key_expected |= 1 << OVS_KEY_ATTR_ND;
  188. if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
  189. mask_allowed |= 1 << OVS_KEY_ATTR_ND;
  190. }
  191. }
  192. }
  193. }
  194. if ((key_attrs & key_expected) != key_expected) {
  195. /* Key attributes check failed. */
  196. OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
  197. key_attrs, key_expected);
  198. return false;
  199. }
  200. if ((mask_attrs & mask_allowed) != mask_attrs) {
  201. /* Mask attributes check failed. */
  202. OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
  203. mask_attrs, mask_allowed);
  204. return false;
  205. }
  206. return true;
  207. }
  208. static int check_header(struct sk_buff *skb, int len)
  209. {
  210. if (unlikely(skb->len < len))
  211. return -EINVAL;
  212. if (unlikely(!pskb_may_pull(skb, len)))
  213. return -ENOMEM;
  214. return 0;
  215. }
  216. static bool arphdr_ok(struct sk_buff *skb)
  217. {
  218. return pskb_may_pull(skb, skb_network_offset(skb) +
  219. sizeof(struct arp_eth_header));
  220. }
  221. static int check_iphdr(struct sk_buff *skb)
  222. {
  223. unsigned int nh_ofs = skb_network_offset(skb);
  224. unsigned int ip_len;
  225. int err;
  226. err = check_header(skb, nh_ofs + sizeof(struct iphdr));
  227. if (unlikely(err))
  228. return err;
  229. ip_len = ip_hdrlen(skb);
  230. if (unlikely(ip_len < sizeof(struct iphdr) ||
  231. skb->len < nh_ofs + ip_len))
  232. return -EINVAL;
  233. skb_set_transport_header(skb, nh_ofs + ip_len);
  234. return 0;
  235. }
  236. static bool tcphdr_ok(struct sk_buff *skb)
  237. {
  238. int th_ofs = skb_transport_offset(skb);
  239. int tcp_len;
  240. if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
  241. return false;
  242. tcp_len = tcp_hdrlen(skb);
  243. if (unlikely(tcp_len < sizeof(struct tcphdr) ||
  244. skb->len < th_ofs + tcp_len))
  245. return false;
  246. return true;
  247. }
  248. static bool udphdr_ok(struct sk_buff *skb)
  249. {
  250. return pskb_may_pull(skb, skb_transport_offset(skb) +
  251. sizeof(struct udphdr));
  252. }
  253. static bool sctphdr_ok(struct sk_buff *skb)
  254. {
  255. return pskb_may_pull(skb, skb_transport_offset(skb) +
  256. sizeof(struct sctphdr));
  257. }
  258. static bool icmphdr_ok(struct sk_buff *skb)
  259. {
  260. return pskb_may_pull(skb, skb_transport_offset(skb) +
  261. sizeof(struct icmphdr));
  262. }
  263. u64 ovs_flow_used_time(unsigned long flow_jiffies)
  264. {
  265. struct timespec cur_ts;
  266. u64 cur_ms, idle_ms;
  267. ktime_get_ts(&cur_ts);
  268. idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
  269. cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
  270. cur_ts.tv_nsec / NSEC_PER_MSEC;
  271. return cur_ms - idle_ms;
  272. }
  273. static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
  274. {
  275. unsigned int nh_ofs = skb_network_offset(skb);
  276. unsigned int nh_len;
  277. int payload_ofs;
  278. struct ipv6hdr *nh;
  279. uint8_t nexthdr;
  280. __be16 frag_off;
  281. int err;
  282. err = check_header(skb, nh_ofs + sizeof(*nh));
  283. if (unlikely(err))
  284. return err;
  285. nh = ipv6_hdr(skb);
  286. nexthdr = nh->nexthdr;
  287. payload_ofs = (u8 *)(nh + 1) - skb->data;
  288. key->ip.proto = NEXTHDR_NONE;
  289. key->ip.tos = ipv6_get_dsfield(nh);
  290. key->ip.ttl = nh->hop_limit;
  291. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  292. key->ipv6.addr.src = nh->saddr;
  293. key->ipv6.addr.dst = nh->daddr;
  294. payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
  295. if (unlikely(payload_ofs < 0))
  296. return -EINVAL;
  297. if (frag_off) {
  298. if (frag_off & htons(~0x7))
  299. key->ip.frag = OVS_FRAG_TYPE_LATER;
  300. else
  301. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  302. }
  303. nh_len = payload_ofs - nh_ofs;
  304. skb_set_transport_header(skb, nh_ofs + nh_len);
  305. key->ip.proto = nexthdr;
  306. return nh_len;
  307. }
  308. static bool icmp6hdr_ok(struct sk_buff *skb)
  309. {
  310. return pskb_may_pull(skb, skb_transport_offset(skb) +
  311. sizeof(struct icmp6hdr));
  312. }
  313. void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
  314. const struct sw_flow_mask *mask)
  315. {
  316. u8 *m = (u8 *)&mask->key + mask->range.start;
  317. u8 *s = (u8 *)src + mask->range.start;
  318. u8 *d = (u8 *)dst + mask->range.start;
  319. int i;
  320. memset(dst, 0, sizeof(*dst));
  321. for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
  322. *d = *s & *m;
  323. d++, s++, m++;
  324. }
  325. }
  326. #define TCP_FLAGS_OFFSET 13
  327. #define TCP_FLAG_MASK 0x3f
  328. void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
  329. {
  330. u8 tcp_flags = 0;
  331. if ((flow->key.eth.type == htons(ETH_P_IP) ||
  332. flow->key.eth.type == htons(ETH_P_IPV6)) &&
  333. flow->key.ip.proto == IPPROTO_TCP &&
  334. likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
  335. u8 *tcp = (u8 *)tcp_hdr(skb);
  336. tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
  337. }
  338. spin_lock(&flow->lock);
  339. flow->used = jiffies;
  340. flow->packet_count++;
  341. flow->byte_count += skb->len;
  342. flow->tcp_flags |= tcp_flags;
  343. spin_unlock(&flow->lock);
  344. }
  345. struct sw_flow_actions *ovs_flow_actions_alloc(int size)
  346. {
  347. struct sw_flow_actions *sfa;
  348. if (size > MAX_ACTIONS_BUFSIZE)
  349. return ERR_PTR(-EINVAL);
  350. sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
  351. if (!sfa)
  352. return ERR_PTR(-ENOMEM);
  353. sfa->actions_len = 0;
  354. return sfa;
  355. }
  356. struct sw_flow *ovs_flow_alloc(void)
  357. {
  358. struct sw_flow *flow;
  359. flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
  360. if (!flow)
  361. return ERR_PTR(-ENOMEM);
  362. spin_lock_init(&flow->lock);
  363. flow->sf_acts = NULL;
  364. flow->mask = NULL;
  365. return flow;
  366. }
  367. static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
  368. {
  369. hash = jhash_1word(hash, table->hash_seed);
  370. return flex_array_get(table->buckets,
  371. (hash & (table->n_buckets - 1)));
  372. }
  373. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  374. {
  375. struct flex_array *buckets;
  376. int i, err;
  377. buckets = flex_array_alloc(sizeof(struct hlist_head),
  378. n_buckets, GFP_KERNEL);
  379. if (!buckets)
  380. return NULL;
  381. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  382. if (err) {
  383. flex_array_free(buckets);
  384. return NULL;
  385. }
  386. for (i = 0; i < n_buckets; i++)
  387. INIT_HLIST_HEAD((struct hlist_head *)
  388. flex_array_get(buckets, i));
  389. return buckets;
  390. }
  391. static void free_buckets(struct flex_array *buckets)
  392. {
  393. flex_array_free(buckets);
  394. }
  395. static struct flow_table *__flow_tbl_alloc(int new_size)
  396. {
  397. struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
  398. if (!table)
  399. return NULL;
  400. table->buckets = alloc_buckets(new_size);
  401. if (!table->buckets) {
  402. kfree(table);
  403. return NULL;
  404. }
  405. table->n_buckets = new_size;
  406. table->count = 0;
  407. table->node_ver = 0;
  408. table->keep_flows = false;
  409. get_random_bytes(&table->hash_seed, sizeof(u32));
  410. table->mask_list = NULL;
  411. return table;
  412. }
  413. static void __flow_tbl_destroy(struct flow_table *table)
  414. {
  415. int i;
  416. if (table->keep_flows)
  417. goto skip_flows;
  418. for (i = 0; i < table->n_buckets; i++) {
  419. struct sw_flow *flow;
  420. struct hlist_head *head = flex_array_get(table->buckets, i);
  421. struct hlist_node *n;
  422. int ver = table->node_ver;
  423. hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
  424. hlist_del(&flow->hash_node[ver]);
  425. ovs_flow_free(flow, false);
  426. }
  427. }
  428. BUG_ON(!list_empty(table->mask_list));
  429. kfree(table->mask_list);
  430. skip_flows:
  431. free_buckets(table->buckets);
  432. kfree(table);
  433. }
  434. struct flow_table *ovs_flow_tbl_alloc(int new_size)
  435. {
  436. struct flow_table *table = __flow_tbl_alloc(new_size);
  437. if (!table)
  438. return NULL;
  439. table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
  440. if (!table->mask_list) {
  441. table->keep_flows = true;
  442. __flow_tbl_destroy(table);
  443. return NULL;
  444. }
  445. INIT_LIST_HEAD(table->mask_list);
  446. return table;
  447. }
  448. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  449. {
  450. struct flow_table *table = container_of(rcu, struct flow_table, rcu);
  451. __flow_tbl_destroy(table);
  452. }
  453. void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
  454. {
  455. if (!table)
  456. return;
  457. if (deferred)
  458. call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
  459. else
  460. __flow_tbl_destroy(table);
  461. }
  462. struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
  463. {
  464. struct sw_flow *flow;
  465. struct hlist_head *head;
  466. int ver;
  467. int i;
  468. ver = table->node_ver;
  469. while (*bucket < table->n_buckets) {
  470. i = 0;
  471. head = flex_array_get(table->buckets, *bucket);
  472. hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
  473. if (i < *last) {
  474. i++;
  475. continue;
  476. }
  477. *last = i + 1;
  478. return flow;
  479. }
  480. (*bucket)++;
  481. *last = 0;
  482. }
  483. return NULL;
  484. }
  485. static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
  486. {
  487. struct hlist_head *head;
  488. head = find_bucket(table, flow->hash);
  489. hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
  490. table->count++;
  491. }
  492. static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
  493. {
  494. int old_ver;
  495. int i;
  496. old_ver = old->node_ver;
  497. new->node_ver = !old_ver;
  498. /* Insert in new table. */
  499. for (i = 0; i < old->n_buckets; i++) {
  500. struct sw_flow *flow;
  501. struct hlist_head *head;
  502. head = flex_array_get(old->buckets, i);
  503. hlist_for_each_entry(flow, head, hash_node[old_ver])
  504. __tbl_insert(new, flow);
  505. }
  506. new->mask_list = old->mask_list;
  507. old->keep_flows = true;
  508. }
  509. static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
  510. {
  511. struct flow_table *new_table;
  512. new_table = __flow_tbl_alloc(n_buckets);
  513. if (!new_table)
  514. return ERR_PTR(-ENOMEM);
  515. flow_table_copy_flows(table, new_table);
  516. return new_table;
  517. }
  518. struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
  519. {
  520. return __flow_tbl_rehash(table, table->n_buckets);
  521. }
  522. struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
  523. {
  524. return __flow_tbl_rehash(table, table->n_buckets * 2);
  525. }
  526. static void __flow_free(struct sw_flow *flow)
  527. {
  528. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  529. kmem_cache_free(flow_cache, flow);
  530. }
  531. static void rcu_free_flow_callback(struct rcu_head *rcu)
  532. {
  533. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  534. __flow_free(flow);
  535. }
  536. void ovs_flow_free(struct sw_flow *flow, bool deferred)
  537. {
  538. if (!flow)
  539. return;
  540. ovs_sw_flow_mask_del_ref(flow->mask, deferred);
  541. if (deferred)
  542. call_rcu(&flow->rcu, rcu_free_flow_callback);
  543. else
  544. __flow_free(flow);
  545. }
  546. /* Schedules 'sf_acts' to be freed after the next RCU grace period.
  547. * The caller must hold rcu_read_lock for this to be sensible. */
  548. void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
  549. {
  550. kfree_rcu(sf_acts, rcu);
  551. }
  552. static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  553. {
  554. struct qtag_prefix {
  555. __be16 eth_type; /* ETH_P_8021Q */
  556. __be16 tci;
  557. };
  558. struct qtag_prefix *qp;
  559. if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
  560. return 0;
  561. if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
  562. sizeof(__be16))))
  563. return -ENOMEM;
  564. qp = (struct qtag_prefix *) skb->data;
  565. key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
  566. __skb_pull(skb, sizeof(struct qtag_prefix));
  567. return 0;
  568. }
  569. static __be16 parse_ethertype(struct sk_buff *skb)
  570. {
  571. struct llc_snap_hdr {
  572. u8 dsap; /* Always 0xAA */
  573. u8 ssap; /* Always 0xAA */
  574. u8 ctrl;
  575. u8 oui[3];
  576. __be16 ethertype;
  577. };
  578. struct llc_snap_hdr *llc;
  579. __be16 proto;
  580. proto = *(__be16 *) skb->data;
  581. __skb_pull(skb, sizeof(__be16));
  582. if (ntohs(proto) >= ETH_P_802_3_MIN)
  583. return proto;
  584. if (skb->len < sizeof(struct llc_snap_hdr))
  585. return htons(ETH_P_802_2);
  586. if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
  587. return htons(0);
  588. llc = (struct llc_snap_hdr *) skb->data;
  589. if (llc->dsap != LLC_SAP_SNAP ||
  590. llc->ssap != LLC_SAP_SNAP ||
  591. (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
  592. return htons(ETH_P_802_2);
  593. __skb_pull(skb, sizeof(struct llc_snap_hdr));
  594. if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
  595. return llc->ethertype;
  596. return htons(ETH_P_802_2);
  597. }
  598. static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
  599. int nh_len)
  600. {
  601. struct icmp6hdr *icmp = icmp6_hdr(skb);
  602. /* The ICMPv6 type and code fields use the 16-bit transport port
  603. * fields, so we need to store them in 16-bit network byte order.
  604. */
  605. key->ipv6.tp.src = htons(icmp->icmp6_type);
  606. key->ipv6.tp.dst = htons(icmp->icmp6_code);
  607. if (icmp->icmp6_code == 0 &&
  608. (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  609. icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
  610. int icmp_len = skb->len - skb_transport_offset(skb);
  611. struct nd_msg *nd;
  612. int offset;
  613. /* In order to process neighbor discovery options, we need the
  614. * entire packet.
  615. */
  616. if (unlikely(icmp_len < sizeof(*nd)))
  617. return 0;
  618. if (unlikely(skb_linearize(skb)))
  619. return -ENOMEM;
  620. nd = (struct nd_msg *)skb_transport_header(skb);
  621. key->ipv6.nd.target = nd->target;
  622. icmp_len -= sizeof(*nd);
  623. offset = 0;
  624. while (icmp_len >= 8) {
  625. struct nd_opt_hdr *nd_opt =
  626. (struct nd_opt_hdr *)(nd->opt + offset);
  627. int opt_len = nd_opt->nd_opt_len * 8;
  628. if (unlikely(!opt_len || opt_len > icmp_len))
  629. return 0;
  630. /* Store the link layer address if the appropriate
  631. * option is provided. It is considered an error if
  632. * the same link layer option is specified twice.
  633. */
  634. if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
  635. && opt_len == 8) {
  636. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
  637. goto invalid;
  638. memcpy(key->ipv6.nd.sll,
  639. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  640. } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
  641. && opt_len == 8) {
  642. if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
  643. goto invalid;
  644. memcpy(key->ipv6.nd.tll,
  645. &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
  646. }
  647. icmp_len -= opt_len;
  648. offset += opt_len;
  649. }
  650. }
  651. return 0;
  652. invalid:
  653. memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
  654. memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
  655. memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
  656. return 0;
  657. }
  658. /**
  659. * ovs_flow_extract - extracts a flow key from an Ethernet frame.
  660. * @skb: sk_buff that contains the frame, with skb->data pointing to the
  661. * Ethernet header
  662. * @in_port: port number on which @skb was received.
  663. * @key: output flow key
  664. *
  665. * The caller must ensure that skb->len >= ETH_HLEN.
  666. *
  667. * Returns 0 if successful, otherwise a negative errno value.
  668. *
  669. * Initializes @skb header pointers as follows:
  670. *
  671. * - skb->mac_header: the Ethernet header.
  672. *
  673. * - skb->network_header: just past the Ethernet header, or just past the
  674. * VLAN header, to the first byte of the Ethernet payload.
  675. *
  676. * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
  677. * on output, then just past the IP header, if one is present and
  678. * of a correct length, otherwise the same as skb->network_header.
  679. * For other key->eth.type values it is left untouched.
  680. */
  681. int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
  682. {
  683. int error;
  684. struct ethhdr *eth;
  685. memset(key, 0, sizeof(*key));
  686. key->phy.priority = skb->priority;
  687. if (OVS_CB(skb)->tun_key)
  688. memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
  689. key->phy.in_port = in_port;
  690. key->phy.skb_mark = skb->mark;
  691. skb_reset_mac_header(skb);
  692. /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
  693. * header in the linear data area.
  694. */
  695. eth = eth_hdr(skb);
  696. memcpy(key->eth.src, eth->h_source, ETH_ALEN);
  697. memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
  698. __skb_pull(skb, 2 * ETH_ALEN);
  699. /* We are going to push all headers that we pull, so no need to
  700. * update skb->csum here.
  701. */
  702. if (vlan_tx_tag_present(skb))
  703. key->eth.tci = htons(skb->vlan_tci);
  704. else if (eth->h_proto == htons(ETH_P_8021Q))
  705. if (unlikely(parse_vlan(skb, key)))
  706. return -ENOMEM;
  707. key->eth.type = parse_ethertype(skb);
  708. if (unlikely(key->eth.type == htons(0)))
  709. return -ENOMEM;
  710. skb_reset_network_header(skb);
  711. __skb_push(skb, skb->data - skb_mac_header(skb));
  712. /* Network layer. */
  713. if (key->eth.type == htons(ETH_P_IP)) {
  714. struct iphdr *nh;
  715. __be16 offset;
  716. error = check_iphdr(skb);
  717. if (unlikely(error)) {
  718. if (error == -EINVAL) {
  719. skb->transport_header = skb->network_header;
  720. error = 0;
  721. }
  722. return error;
  723. }
  724. nh = ip_hdr(skb);
  725. key->ipv4.addr.src = nh->saddr;
  726. key->ipv4.addr.dst = nh->daddr;
  727. key->ip.proto = nh->protocol;
  728. key->ip.tos = nh->tos;
  729. key->ip.ttl = nh->ttl;
  730. offset = nh->frag_off & htons(IP_OFFSET);
  731. if (offset) {
  732. key->ip.frag = OVS_FRAG_TYPE_LATER;
  733. return 0;
  734. }
  735. if (nh->frag_off & htons(IP_MF) ||
  736. skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  737. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  738. /* Transport layer. */
  739. if (key->ip.proto == IPPROTO_TCP) {
  740. if (tcphdr_ok(skb)) {
  741. struct tcphdr *tcp = tcp_hdr(skb);
  742. key->ipv4.tp.src = tcp->source;
  743. key->ipv4.tp.dst = tcp->dest;
  744. }
  745. } else if (key->ip.proto == IPPROTO_UDP) {
  746. if (udphdr_ok(skb)) {
  747. struct udphdr *udp = udp_hdr(skb);
  748. key->ipv4.tp.src = udp->source;
  749. key->ipv4.tp.dst = udp->dest;
  750. }
  751. } else if (key->ip.proto == IPPROTO_SCTP) {
  752. if (sctphdr_ok(skb)) {
  753. struct sctphdr *sctp = sctp_hdr(skb);
  754. key->ipv4.tp.src = sctp->source;
  755. key->ipv4.tp.dst = sctp->dest;
  756. }
  757. } else if (key->ip.proto == IPPROTO_ICMP) {
  758. if (icmphdr_ok(skb)) {
  759. struct icmphdr *icmp = icmp_hdr(skb);
  760. /* The ICMP type and code fields use the 16-bit
  761. * transport port fields, so we need to store
  762. * them in 16-bit network byte order. */
  763. key->ipv4.tp.src = htons(icmp->type);
  764. key->ipv4.tp.dst = htons(icmp->code);
  765. }
  766. }
  767. } else if ((key->eth.type == htons(ETH_P_ARP) ||
  768. key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
  769. struct arp_eth_header *arp;
  770. arp = (struct arp_eth_header *)skb_network_header(skb);
  771. if (arp->ar_hrd == htons(ARPHRD_ETHER)
  772. && arp->ar_pro == htons(ETH_P_IP)
  773. && arp->ar_hln == ETH_ALEN
  774. && arp->ar_pln == 4) {
  775. /* We only match on the lower 8 bits of the opcode. */
  776. if (ntohs(arp->ar_op) <= 0xff)
  777. key->ip.proto = ntohs(arp->ar_op);
  778. memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
  779. memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
  780. memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
  781. memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
  782. }
  783. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  784. int nh_len; /* IPv6 Header + Extensions */
  785. nh_len = parse_ipv6hdr(skb, key);
  786. if (unlikely(nh_len < 0)) {
  787. if (nh_len == -EINVAL) {
  788. skb->transport_header = skb->network_header;
  789. error = 0;
  790. } else {
  791. error = nh_len;
  792. }
  793. return error;
  794. }
  795. if (key->ip.frag == OVS_FRAG_TYPE_LATER)
  796. return 0;
  797. if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  798. key->ip.frag = OVS_FRAG_TYPE_FIRST;
  799. /* Transport layer. */
  800. if (key->ip.proto == NEXTHDR_TCP) {
  801. if (tcphdr_ok(skb)) {
  802. struct tcphdr *tcp = tcp_hdr(skb);
  803. key->ipv6.tp.src = tcp->source;
  804. key->ipv6.tp.dst = tcp->dest;
  805. }
  806. } else if (key->ip.proto == NEXTHDR_UDP) {
  807. if (udphdr_ok(skb)) {
  808. struct udphdr *udp = udp_hdr(skb);
  809. key->ipv6.tp.src = udp->source;
  810. key->ipv6.tp.dst = udp->dest;
  811. }
  812. } else if (key->ip.proto == NEXTHDR_SCTP) {
  813. if (sctphdr_ok(skb)) {
  814. struct sctphdr *sctp = sctp_hdr(skb);
  815. key->ipv6.tp.src = sctp->source;
  816. key->ipv6.tp.dst = sctp->dest;
  817. }
  818. } else if (key->ip.proto == NEXTHDR_ICMP) {
  819. if (icmp6hdr_ok(skb)) {
  820. error = parse_icmpv6(skb, key, nh_len);
  821. if (error)
  822. return error;
  823. }
  824. }
  825. }
  826. return 0;
  827. }
  828. static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
  829. {
  830. return jhash2((u32 *)((u8 *)key + key_start),
  831. DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
  832. }
  833. static int flow_key_start(const struct sw_flow_key *key)
  834. {
  835. if (key->tun_key.ipv4_dst)
  836. return 0;
  837. else
  838. return offsetof(struct sw_flow_key, phy);
  839. }
  840. static bool __cmp_key(const struct sw_flow_key *key1,
  841. const struct sw_flow_key *key2, int key_start, int key_len)
  842. {
  843. return !memcmp((u8 *)key1 + key_start,
  844. (u8 *)key2 + key_start, (key_len - key_start));
  845. }
  846. static bool __flow_cmp_key(const struct sw_flow *flow,
  847. const struct sw_flow_key *key, int key_start, int key_len)
  848. {
  849. return __cmp_key(&flow->key, key, key_start, key_len);
  850. }
  851. static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
  852. const struct sw_flow_key *key, int key_start, int key_len)
  853. {
  854. return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
  855. }
  856. bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  857. const struct sw_flow_key *key, int key_len)
  858. {
  859. int key_start;
  860. key_start = flow_key_start(key);
  861. return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
  862. }
  863. struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
  864. struct sw_flow_match *match)
  865. {
  866. struct sw_flow_key *unmasked = match->key;
  867. int key_len = match->range.end;
  868. struct sw_flow *flow;
  869. flow = ovs_flow_lookup(table, unmasked);
  870. if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
  871. flow = NULL;
  872. return flow;
  873. }
  874. static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
  875. const struct sw_flow_key *flow_key,
  876. struct sw_flow_mask *mask)
  877. {
  878. struct sw_flow *flow;
  879. struct hlist_head *head;
  880. int key_start = mask->range.start;
  881. int key_len = mask->range.end;
  882. u32 hash;
  883. struct sw_flow_key masked_key;
  884. ovs_flow_key_mask(&masked_key, flow_key, mask);
  885. hash = ovs_flow_hash(&masked_key, key_start, key_len);
  886. head = find_bucket(table, hash);
  887. hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
  888. if (flow->mask == mask &&
  889. __flow_cmp_key(flow, &masked_key, key_start, key_len))
  890. return flow;
  891. }
  892. return NULL;
  893. }
  894. struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
  895. const struct sw_flow_key *key)
  896. {
  897. struct sw_flow *flow = NULL;
  898. struct sw_flow_mask *mask;
  899. list_for_each_entry_rcu(mask, tbl->mask_list, list) {
  900. flow = ovs_masked_flow_lookup(tbl, key, mask);
  901. if (flow) /* Found */
  902. break;
  903. }
  904. return flow;
  905. }
  906. void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
  907. {
  908. flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
  909. flow->mask->range.end);
  910. __tbl_insert(table, flow);
  911. }
  912. void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
  913. {
  914. BUG_ON(table->count == 0);
  915. hlist_del_rcu(&flow->hash_node[table->node_ver]);
  916. table->count--;
  917. }
  918. /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
  919. const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
  920. [OVS_KEY_ATTR_ENCAP] = -1,
  921. [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
  922. [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
  923. [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
  924. [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
  925. [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
  926. [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
  927. [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
  928. [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
  929. [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
  930. [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
  931. [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
  932. [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
  933. [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
  934. [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
  935. [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
  936. [OVS_KEY_ATTR_TUNNEL] = -1,
  937. };
  938. static bool is_all_zero(const u8 *fp, size_t size)
  939. {
  940. int i;
  941. if (!fp)
  942. return false;
  943. for (i = 0; i < size; i++)
  944. if (fp[i])
  945. return false;
  946. return true;
  947. }
  948. static int __parse_flow_nlattrs(const struct nlattr *attr,
  949. const struct nlattr *a[],
  950. u64 *attrsp, bool nz)
  951. {
  952. const struct nlattr *nla;
  953. u32 attrs;
  954. int rem;
  955. attrs = *attrsp;
  956. nla_for_each_nested(nla, attr, rem) {
  957. u16 type = nla_type(nla);
  958. int expected_len;
  959. if (type > OVS_KEY_ATTR_MAX) {
  960. OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
  961. type, OVS_KEY_ATTR_MAX);
  962. }
  963. if (attrs & (1 << type)) {
  964. OVS_NLERR("Duplicate key attribute (type %d).\n", type);
  965. return -EINVAL;
  966. }
  967. expected_len = ovs_key_lens[type];
  968. if (nla_len(nla) != expected_len && expected_len != -1) {
  969. OVS_NLERR("Key attribute has unexpected length (type=%d"
  970. ", length=%d, expected=%d).\n", type,
  971. nla_len(nla), expected_len);
  972. return -EINVAL;
  973. }
  974. if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
  975. attrs |= 1 << type;
  976. a[type] = nla;
  977. }
  978. }
  979. if (rem) {
  980. OVS_NLERR("Message has %d unknown bytes.\n", rem);
  981. return -EINVAL;
  982. }
  983. *attrsp = attrs;
  984. return 0;
  985. }
  986. static int parse_flow_mask_nlattrs(const struct nlattr *attr,
  987. const struct nlattr *a[], u64 *attrsp)
  988. {
  989. return __parse_flow_nlattrs(attr, a, attrsp, true);
  990. }
  991. static int parse_flow_nlattrs(const struct nlattr *attr,
  992. const struct nlattr *a[], u64 *attrsp)
  993. {
  994. return __parse_flow_nlattrs(attr, a, attrsp, false);
  995. }
  996. int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
  997. struct sw_flow_match *match, bool is_mask)
  998. {
  999. struct nlattr *a;
  1000. int rem;
  1001. bool ttl = false;
  1002. __be16 tun_flags = 0;
  1003. nla_for_each_nested(a, attr, rem) {
  1004. int type = nla_type(a);
  1005. static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
  1006. [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
  1007. [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
  1008. [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
  1009. [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
  1010. [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
  1011. [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
  1012. [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
  1013. };
  1014. if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
  1015. OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
  1016. type, OVS_TUNNEL_KEY_ATTR_MAX);
  1017. return -EINVAL;
  1018. }
  1019. if (ovs_tunnel_key_lens[type] != nla_len(a)) {
  1020. OVS_NLERR("IPv4 tunnel attribute type has unexpected "
  1021. " length (type=%d, length=%d, expected=%d).\n",
  1022. type, nla_len(a), ovs_tunnel_key_lens[type]);
  1023. return -EINVAL;
  1024. }
  1025. switch (type) {
  1026. case OVS_TUNNEL_KEY_ATTR_ID:
  1027. SW_FLOW_KEY_PUT(match, tun_key.tun_id,
  1028. nla_get_be64(a), is_mask);
  1029. tun_flags |= TUNNEL_KEY;
  1030. break;
  1031. case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
  1032. SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
  1033. nla_get_be32(a), is_mask);
  1034. break;
  1035. case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
  1036. SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
  1037. nla_get_be32(a), is_mask);
  1038. break;
  1039. case OVS_TUNNEL_KEY_ATTR_TOS:
  1040. SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
  1041. nla_get_u8(a), is_mask);
  1042. break;
  1043. case OVS_TUNNEL_KEY_ATTR_TTL:
  1044. SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
  1045. nla_get_u8(a), is_mask);
  1046. ttl = true;
  1047. break;
  1048. case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
  1049. tun_flags |= TUNNEL_DONT_FRAGMENT;
  1050. break;
  1051. case OVS_TUNNEL_KEY_ATTR_CSUM:
  1052. tun_flags |= TUNNEL_CSUM;
  1053. break;
  1054. default:
  1055. return -EINVAL;
  1056. }
  1057. }
  1058. SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
  1059. if (rem > 0) {
  1060. OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
  1061. return -EINVAL;
  1062. }
  1063. if (!is_mask) {
  1064. if (!match->key->tun_key.ipv4_dst) {
  1065. OVS_NLERR("IPv4 tunnel destination address is zero.\n");
  1066. return -EINVAL;
  1067. }
  1068. if (!ttl) {
  1069. OVS_NLERR("IPv4 tunnel TTL not specified.\n");
  1070. return -EINVAL;
  1071. }
  1072. }
  1073. return 0;
  1074. }
  1075. int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
  1076. const struct ovs_key_ipv4_tunnel *tun_key,
  1077. const struct ovs_key_ipv4_tunnel *output)
  1078. {
  1079. struct nlattr *nla;
  1080. nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
  1081. if (!nla)
  1082. return -EMSGSIZE;
  1083. if (output->tun_flags & TUNNEL_KEY &&
  1084. nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
  1085. return -EMSGSIZE;
  1086. if (output->ipv4_src &&
  1087. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
  1088. return -EMSGSIZE;
  1089. if (output->ipv4_dst &&
  1090. nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
  1091. return -EMSGSIZE;
  1092. if (output->ipv4_tos &&
  1093. nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
  1094. return -EMSGSIZE;
  1095. if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
  1096. return -EMSGSIZE;
  1097. if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
  1098. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
  1099. return -EMSGSIZE;
  1100. if ((output->tun_flags & TUNNEL_CSUM) &&
  1101. nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
  1102. return -EMSGSIZE;
  1103. nla_nest_end(skb, nla);
  1104. return 0;
  1105. }
  1106. static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
  1107. const struct nlattr **a, bool is_mask)
  1108. {
  1109. if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
  1110. SW_FLOW_KEY_PUT(match, phy.priority,
  1111. nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
  1112. *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
  1113. }
  1114. if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
  1115. u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
  1116. if (is_mask)
  1117. in_port = 0xffffffff; /* Always exact match in_port. */
  1118. else if (in_port >= DP_MAX_PORTS)
  1119. return -EINVAL;
  1120. SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
  1121. *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
  1122. } else if (!is_mask) {
  1123. SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
  1124. }
  1125. if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
  1126. uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
  1127. SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
  1128. *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
  1129. }
  1130. if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
  1131. if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
  1132. is_mask))
  1133. return -EINVAL;
  1134. *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
  1135. }
  1136. return 0;
  1137. }
  1138. static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
  1139. const struct nlattr **a, bool is_mask)
  1140. {
  1141. int err;
  1142. u64 orig_attrs = attrs;
  1143. err = metadata_from_nlattrs(match, &attrs, a, is_mask);
  1144. if (err)
  1145. return err;
  1146. if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
  1147. const struct ovs_key_ethernet *eth_key;
  1148. eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
  1149. SW_FLOW_KEY_MEMCPY(match, eth.src,
  1150. eth_key->eth_src, ETH_ALEN, is_mask);
  1151. SW_FLOW_KEY_MEMCPY(match, eth.dst,
  1152. eth_key->eth_dst, ETH_ALEN, is_mask);
  1153. attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
  1154. }
  1155. if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
  1156. __be16 tci;
  1157. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1158. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1159. if (is_mask)
  1160. OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
  1161. else
  1162. OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
  1163. return -EINVAL;
  1164. }
  1165. SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
  1166. attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
  1167. } else if (!is_mask)
  1168. SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
  1169. if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
  1170. __be16 eth_type;
  1171. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1172. if (is_mask) {
  1173. /* Always exact match EtherType. */
  1174. eth_type = htons(0xffff);
  1175. } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
  1176. OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
  1177. ntohs(eth_type), ETH_P_802_3_MIN);
  1178. return -EINVAL;
  1179. }
  1180. SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
  1181. attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1182. } else if (!is_mask) {
  1183. SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
  1184. }
  1185. if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1186. const struct ovs_key_ipv4 *ipv4_key;
  1187. ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
  1188. if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
  1189. OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
  1190. ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
  1191. return -EINVAL;
  1192. }
  1193. SW_FLOW_KEY_PUT(match, ip.proto,
  1194. ipv4_key->ipv4_proto, is_mask);
  1195. SW_FLOW_KEY_PUT(match, ip.tos,
  1196. ipv4_key->ipv4_tos, is_mask);
  1197. SW_FLOW_KEY_PUT(match, ip.ttl,
  1198. ipv4_key->ipv4_ttl, is_mask);
  1199. SW_FLOW_KEY_PUT(match, ip.frag,
  1200. ipv4_key->ipv4_frag, is_mask);
  1201. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1202. ipv4_key->ipv4_src, is_mask);
  1203. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1204. ipv4_key->ipv4_dst, is_mask);
  1205. attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
  1206. }
  1207. if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
  1208. const struct ovs_key_ipv6 *ipv6_key;
  1209. ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
  1210. if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
  1211. OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
  1212. ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
  1213. return -EINVAL;
  1214. }
  1215. SW_FLOW_KEY_PUT(match, ipv6.label,
  1216. ipv6_key->ipv6_label, is_mask);
  1217. SW_FLOW_KEY_PUT(match, ip.proto,
  1218. ipv6_key->ipv6_proto, is_mask);
  1219. SW_FLOW_KEY_PUT(match, ip.tos,
  1220. ipv6_key->ipv6_tclass, is_mask);
  1221. SW_FLOW_KEY_PUT(match, ip.ttl,
  1222. ipv6_key->ipv6_hlimit, is_mask);
  1223. SW_FLOW_KEY_PUT(match, ip.frag,
  1224. ipv6_key->ipv6_frag, is_mask);
  1225. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
  1226. ipv6_key->ipv6_src,
  1227. sizeof(match->key->ipv6.addr.src),
  1228. is_mask);
  1229. SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
  1230. ipv6_key->ipv6_dst,
  1231. sizeof(match->key->ipv6.addr.dst),
  1232. is_mask);
  1233. attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
  1234. }
  1235. if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
  1236. const struct ovs_key_arp *arp_key;
  1237. arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
  1238. if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
  1239. OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
  1240. arp_key->arp_op);
  1241. return -EINVAL;
  1242. }
  1243. SW_FLOW_KEY_PUT(match, ipv4.addr.src,
  1244. arp_key->arp_sip, is_mask);
  1245. SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
  1246. arp_key->arp_tip, is_mask);
  1247. SW_FLOW_KEY_PUT(match, ip.proto,
  1248. ntohs(arp_key->arp_op), is_mask);
  1249. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
  1250. arp_key->arp_sha, ETH_ALEN, is_mask);
  1251. SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
  1252. arp_key->arp_tha, ETH_ALEN, is_mask);
  1253. attrs &= ~(1 << OVS_KEY_ATTR_ARP);
  1254. }
  1255. if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
  1256. const struct ovs_key_tcp *tcp_key;
  1257. tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
  1258. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1259. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1260. tcp_key->tcp_src, is_mask);
  1261. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1262. tcp_key->tcp_dst, is_mask);
  1263. } else {
  1264. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1265. tcp_key->tcp_src, is_mask);
  1266. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1267. tcp_key->tcp_dst, is_mask);
  1268. }
  1269. attrs &= ~(1 << OVS_KEY_ATTR_TCP);
  1270. }
  1271. if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
  1272. const struct ovs_key_udp *udp_key;
  1273. udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
  1274. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1275. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1276. udp_key->udp_src, is_mask);
  1277. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1278. udp_key->udp_dst, is_mask);
  1279. } else {
  1280. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1281. udp_key->udp_src, is_mask);
  1282. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1283. udp_key->udp_dst, is_mask);
  1284. }
  1285. attrs &= ~(1 << OVS_KEY_ATTR_UDP);
  1286. }
  1287. if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
  1288. const struct ovs_key_sctp *sctp_key;
  1289. sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
  1290. if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
  1291. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1292. sctp_key->sctp_src, is_mask);
  1293. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1294. sctp_key->sctp_dst, is_mask);
  1295. } else {
  1296. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1297. sctp_key->sctp_src, is_mask);
  1298. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1299. sctp_key->sctp_dst, is_mask);
  1300. }
  1301. attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
  1302. }
  1303. if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
  1304. const struct ovs_key_icmp *icmp_key;
  1305. icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
  1306. SW_FLOW_KEY_PUT(match, ipv4.tp.src,
  1307. htons(icmp_key->icmp_type), is_mask);
  1308. SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
  1309. htons(icmp_key->icmp_code), is_mask);
  1310. attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
  1311. }
  1312. if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
  1313. const struct ovs_key_icmpv6 *icmpv6_key;
  1314. icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
  1315. SW_FLOW_KEY_PUT(match, ipv6.tp.src,
  1316. htons(icmpv6_key->icmpv6_type), is_mask);
  1317. SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
  1318. htons(icmpv6_key->icmpv6_code), is_mask);
  1319. attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
  1320. }
  1321. if (attrs & (1 << OVS_KEY_ATTR_ND)) {
  1322. const struct ovs_key_nd *nd_key;
  1323. nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
  1324. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
  1325. nd_key->nd_target,
  1326. sizeof(match->key->ipv6.nd.target),
  1327. is_mask);
  1328. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
  1329. nd_key->nd_sll, ETH_ALEN, is_mask);
  1330. SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
  1331. nd_key->nd_tll, ETH_ALEN, is_mask);
  1332. attrs &= ~(1 << OVS_KEY_ATTR_ND);
  1333. }
  1334. if (attrs != 0)
  1335. return -EINVAL;
  1336. return 0;
  1337. }
  1338. /**
  1339. * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
  1340. * mask. In case the 'mask' is NULL, the flow is treated as exact match
  1341. * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  1342. * does not include any don't care bit.
  1343. * @match: receives the extracted flow match information.
  1344. * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1345. * sequence. The fields should of the packet that triggered the creation
  1346. * of this flow.
  1347. * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
  1348. * attribute specifies the mask field of the wildcarded flow.
  1349. */
  1350. int ovs_match_from_nlattrs(struct sw_flow_match *match,
  1351. const struct nlattr *key,
  1352. const struct nlattr *mask)
  1353. {
  1354. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1355. const struct nlattr *encap;
  1356. u64 key_attrs = 0;
  1357. u64 mask_attrs = 0;
  1358. bool encap_valid = false;
  1359. int err;
  1360. err = parse_flow_nlattrs(key, a, &key_attrs);
  1361. if (err)
  1362. return err;
  1363. if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
  1364. (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
  1365. (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
  1366. __be16 tci;
  1367. if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
  1368. (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
  1369. OVS_NLERR("Invalid Vlan frame.\n");
  1370. return -EINVAL;
  1371. }
  1372. key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1373. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1374. encap = a[OVS_KEY_ATTR_ENCAP];
  1375. key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1376. encap_valid = true;
  1377. if (tci & htons(VLAN_TAG_PRESENT)) {
  1378. err = parse_flow_nlattrs(encap, a, &key_attrs);
  1379. if (err)
  1380. return err;
  1381. } else if (!tci) {
  1382. /* Corner case for truncated 802.1Q header. */
  1383. if (nla_len(encap)) {
  1384. OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
  1385. return -EINVAL;
  1386. }
  1387. } else {
  1388. OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
  1389. return -EINVAL;
  1390. }
  1391. }
  1392. err = ovs_key_from_nlattrs(match, key_attrs, a, false);
  1393. if (err)
  1394. return err;
  1395. if (mask) {
  1396. err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
  1397. if (err)
  1398. return err;
  1399. if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
  1400. __be16 eth_type = 0;
  1401. __be16 tci = 0;
  1402. if (!encap_valid) {
  1403. OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
  1404. return -EINVAL;
  1405. }
  1406. mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
  1407. if (a[OVS_KEY_ATTR_ETHERTYPE])
  1408. eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
  1409. if (eth_type == htons(0xffff)) {
  1410. mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
  1411. encap = a[OVS_KEY_ATTR_ENCAP];
  1412. err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
  1413. } else {
  1414. OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
  1415. ntohs(eth_type));
  1416. return -EINVAL;
  1417. }
  1418. if (a[OVS_KEY_ATTR_VLAN])
  1419. tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
  1420. if (!(tci & htons(VLAN_TAG_PRESENT))) {
  1421. OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
  1422. return -EINVAL;
  1423. }
  1424. }
  1425. err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
  1426. if (err)
  1427. return err;
  1428. } else {
  1429. /* Populate exact match flow's key mask. */
  1430. if (match->mask)
  1431. ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
  1432. }
  1433. if (!ovs_match_validate(match, key_attrs, mask_attrs))
  1434. return -EINVAL;
  1435. return 0;
  1436. }
  1437. /**
  1438. * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
  1439. * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
  1440. * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  1441. * sequence.
  1442. *
  1443. * This parses a series of Netlink attributes that form a flow key, which must
  1444. * take the same form accepted by flow_from_nlattrs(), but only enough of it to
  1445. * get the metadata, that is, the parts of the flow key that cannot be
  1446. * extracted from the packet itself.
  1447. */
  1448. int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
  1449. const struct nlattr *attr)
  1450. {
  1451. struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
  1452. const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
  1453. u64 attrs = 0;
  1454. int err;
  1455. struct sw_flow_match match;
  1456. flow->key.phy.in_port = DP_MAX_PORTS;
  1457. flow->key.phy.priority = 0;
  1458. flow->key.phy.skb_mark = 0;
  1459. memset(tun_key, 0, sizeof(flow->key.tun_key));
  1460. err = parse_flow_nlattrs(attr, a, &attrs);
  1461. if (err)
  1462. return -EINVAL;
  1463. memset(&match, 0, sizeof(match));
  1464. match.key = &flow->key;
  1465. err = metadata_from_nlattrs(&match, &attrs, a, false);
  1466. if (err)
  1467. return err;
  1468. return 0;
  1469. }
  1470. int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
  1471. const struct sw_flow_key *output, struct sk_buff *skb)
  1472. {
  1473. struct ovs_key_ethernet *eth_key;
  1474. struct nlattr *nla, *encap;
  1475. bool is_mask = (swkey != output);
  1476. if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
  1477. goto nla_put_failure;
  1478. if ((swkey->tun_key.ipv4_dst || is_mask) &&
  1479. ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
  1480. goto nla_put_failure;
  1481. if (swkey->phy.in_port == DP_MAX_PORTS) {
  1482. if (is_mask && (output->phy.in_port == 0xffff))
  1483. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
  1484. goto nla_put_failure;
  1485. } else {
  1486. u16 upper_u16;
  1487. upper_u16 = !is_mask ? 0 : 0xffff;
  1488. if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
  1489. (upper_u16 << 16) | output->phy.in_port))
  1490. goto nla_put_failure;
  1491. }
  1492. if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
  1493. goto nla_put_failure;
  1494. nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
  1495. if (!nla)
  1496. goto nla_put_failure;
  1497. eth_key = nla_data(nla);
  1498. memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
  1499. memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
  1500. if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
  1501. __be16 eth_type;
  1502. eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
  1503. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
  1504. nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
  1505. goto nla_put_failure;
  1506. encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
  1507. if (!swkey->eth.tci)
  1508. goto unencap;
  1509. } else
  1510. encap = NULL;
  1511. if (swkey->eth.type == htons(ETH_P_802_2)) {
  1512. /*
  1513. * Ethertype 802.2 is represented in the netlink with omitted
  1514. * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
  1515. * 0xffff in the mask attribute. Ethertype can also
  1516. * be wildcarded.
  1517. */
  1518. if (is_mask && output->eth.type)
  1519. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
  1520. output->eth.type))
  1521. goto nla_put_failure;
  1522. goto unencap;
  1523. }
  1524. if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
  1525. goto nla_put_failure;
  1526. if (swkey->eth.type == htons(ETH_P_IP)) {
  1527. struct ovs_key_ipv4 *ipv4_key;
  1528. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
  1529. if (!nla)
  1530. goto nla_put_failure;
  1531. ipv4_key = nla_data(nla);
  1532. ipv4_key->ipv4_src = output->ipv4.addr.src;
  1533. ipv4_key->ipv4_dst = output->ipv4.addr.dst;
  1534. ipv4_key->ipv4_proto = output->ip.proto;
  1535. ipv4_key->ipv4_tos = output->ip.tos;
  1536. ipv4_key->ipv4_ttl = output->ip.ttl;
  1537. ipv4_key->ipv4_frag = output->ip.frag;
  1538. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1539. struct ovs_key_ipv6 *ipv6_key;
  1540. nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
  1541. if (!nla)
  1542. goto nla_put_failure;
  1543. ipv6_key = nla_data(nla);
  1544. memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
  1545. sizeof(ipv6_key->ipv6_src));
  1546. memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
  1547. sizeof(ipv6_key->ipv6_dst));
  1548. ipv6_key->ipv6_label = output->ipv6.label;
  1549. ipv6_key->ipv6_proto = output->ip.proto;
  1550. ipv6_key->ipv6_tclass = output->ip.tos;
  1551. ipv6_key->ipv6_hlimit = output->ip.ttl;
  1552. ipv6_key->ipv6_frag = output->ip.frag;
  1553. } else if (swkey->eth.type == htons(ETH_P_ARP) ||
  1554. swkey->eth.type == htons(ETH_P_RARP)) {
  1555. struct ovs_key_arp *arp_key;
  1556. nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
  1557. if (!nla)
  1558. goto nla_put_failure;
  1559. arp_key = nla_data(nla);
  1560. memset(arp_key, 0, sizeof(struct ovs_key_arp));
  1561. arp_key->arp_sip = output->ipv4.addr.src;
  1562. arp_key->arp_tip = output->ipv4.addr.dst;
  1563. arp_key->arp_op = htons(output->ip.proto);
  1564. memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
  1565. memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
  1566. }
  1567. if ((swkey->eth.type == htons(ETH_P_IP) ||
  1568. swkey->eth.type == htons(ETH_P_IPV6)) &&
  1569. swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
  1570. if (swkey->ip.proto == IPPROTO_TCP) {
  1571. struct ovs_key_tcp *tcp_key;
  1572. nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
  1573. if (!nla)
  1574. goto nla_put_failure;
  1575. tcp_key = nla_data(nla);
  1576. if (swkey->eth.type == htons(ETH_P_IP)) {
  1577. tcp_key->tcp_src = output->ipv4.tp.src;
  1578. tcp_key->tcp_dst = output->ipv4.tp.dst;
  1579. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1580. tcp_key->tcp_src = output->ipv6.tp.src;
  1581. tcp_key->tcp_dst = output->ipv6.tp.dst;
  1582. }
  1583. } else if (swkey->ip.proto == IPPROTO_UDP) {
  1584. struct ovs_key_udp *udp_key;
  1585. nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
  1586. if (!nla)
  1587. goto nla_put_failure;
  1588. udp_key = nla_data(nla);
  1589. if (swkey->eth.type == htons(ETH_P_IP)) {
  1590. udp_key->udp_src = output->ipv4.tp.src;
  1591. udp_key->udp_dst = output->ipv4.tp.dst;
  1592. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1593. udp_key->udp_src = output->ipv6.tp.src;
  1594. udp_key->udp_dst = output->ipv6.tp.dst;
  1595. }
  1596. } else if (swkey->ip.proto == IPPROTO_SCTP) {
  1597. struct ovs_key_sctp *sctp_key;
  1598. nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
  1599. if (!nla)
  1600. goto nla_put_failure;
  1601. sctp_key = nla_data(nla);
  1602. if (swkey->eth.type == htons(ETH_P_IP)) {
  1603. sctp_key->sctp_src = swkey->ipv4.tp.src;
  1604. sctp_key->sctp_dst = swkey->ipv4.tp.dst;
  1605. } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
  1606. sctp_key->sctp_src = swkey->ipv6.tp.src;
  1607. sctp_key->sctp_dst = swkey->ipv6.tp.dst;
  1608. }
  1609. } else if (swkey->eth.type == htons(ETH_P_IP) &&
  1610. swkey->ip.proto == IPPROTO_ICMP) {
  1611. struct ovs_key_icmp *icmp_key;
  1612. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
  1613. if (!nla)
  1614. goto nla_put_failure;
  1615. icmp_key = nla_data(nla);
  1616. icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
  1617. icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
  1618. } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
  1619. swkey->ip.proto == IPPROTO_ICMPV6) {
  1620. struct ovs_key_icmpv6 *icmpv6_key;
  1621. nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
  1622. sizeof(*icmpv6_key));
  1623. if (!nla)
  1624. goto nla_put_failure;
  1625. icmpv6_key = nla_data(nla);
  1626. icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
  1627. icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
  1628. if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
  1629. icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
  1630. struct ovs_key_nd *nd_key;
  1631. nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
  1632. if (!nla)
  1633. goto nla_put_failure;
  1634. nd_key = nla_data(nla);
  1635. memcpy(nd_key->nd_target, &output->ipv6.nd.target,
  1636. sizeof(nd_key->nd_target));
  1637. memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
  1638. memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
  1639. }
  1640. }
  1641. }
  1642. unencap:
  1643. if (encap)
  1644. nla_nest_end(skb, encap);
  1645. return 0;
  1646. nla_put_failure:
  1647. return -EMSGSIZE;
  1648. }
  1649. /* Initializes the flow module.
  1650. * Returns zero if successful or a negative error code. */
  1651. int ovs_flow_init(void)
  1652. {
  1653. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  1654. 0, NULL);
  1655. if (flow_cache == NULL)
  1656. return -ENOMEM;
  1657. return 0;
  1658. }
  1659. /* Uninitializes the flow module. */
  1660. void ovs_flow_exit(void)
  1661. {
  1662. kmem_cache_destroy(flow_cache);
  1663. }
  1664. struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
  1665. {
  1666. struct sw_flow_mask *mask;
  1667. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  1668. if (mask)
  1669. mask->ref_count = 0;
  1670. return mask;
  1671. }
  1672. void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
  1673. {
  1674. mask->ref_count++;
  1675. }
  1676. void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
  1677. {
  1678. if (!mask)
  1679. return;
  1680. BUG_ON(!mask->ref_count);
  1681. mask->ref_count--;
  1682. if (!mask->ref_count) {
  1683. list_del_rcu(&mask->list);
  1684. if (deferred)
  1685. kfree_rcu(mask, rcu);
  1686. else
  1687. kfree(mask);
  1688. }
  1689. }
  1690. static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
  1691. const struct sw_flow_mask *b)
  1692. {
  1693. u8 *a_ = (u8 *)&a->key + a->range.start;
  1694. u8 *b_ = (u8 *)&b->key + b->range.start;
  1695. return (a->range.end == b->range.end)
  1696. && (a->range.start == b->range.start)
  1697. && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
  1698. }
  1699. struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
  1700. const struct sw_flow_mask *mask)
  1701. {
  1702. struct list_head *ml;
  1703. list_for_each(ml, tbl->mask_list) {
  1704. struct sw_flow_mask *m;
  1705. m = container_of(ml, struct sw_flow_mask, list);
  1706. if (ovs_sw_flow_mask_equal(mask, m))
  1707. return m;
  1708. }
  1709. return NULL;
  1710. }
  1711. /**
  1712. * add a new mask into the mask list.
  1713. * The caller needs to make sure that 'mask' is not the same
  1714. * as any masks that are already on the list.
  1715. */
  1716. void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
  1717. {
  1718. list_add_rcu(&mask->list, tbl->mask_list);
  1719. }
  1720. /**
  1721. * Set 'range' fields in the mask to the value of 'val'.
  1722. */
  1723. static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
  1724. struct sw_flow_key_range *range, u8 val)
  1725. {
  1726. u8 *m = (u8 *)&mask->key + range->start;
  1727. mask->range = *range;
  1728. memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
  1729. }