cls_flower.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * net/sched/cls_flower.c Flower classifier
  3. *
  4. * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/rhashtable.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/in6.h>
  18. #include <linux/ip.h>
  19. #include <linux/mpls.h>
  20. #include <net/sch_generic.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/ip.h>
  23. #include <net/flow_dissector.h>
  24. #include <net/dst.h>
  25. #include <net/dst_metadata.h>
  26. struct fl_flow_key {
  27. int indev_ifindex;
  28. struct flow_dissector_key_control control;
  29. struct flow_dissector_key_control enc_control;
  30. struct flow_dissector_key_basic basic;
  31. struct flow_dissector_key_eth_addrs eth;
  32. struct flow_dissector_key_vlan vlan;
  33. union {
  34. struct flow_dissector_key_ipv4_addrs ipv4;
  35. struct flow_dissector_key_ipv6_addrs ipv6;
  36. };
  37. struct flow_dissector_key_ports tp;
  38. struct flow_dissector_key_icmp icmp;
  39. struct flow_dissector_key_arp arp;
  40. struct flow_dissector_key_keyid enc_key_id;
  41. union {
  42. struct flow_dissector_key_ipv4_addrs enc_ipv4;
  43. struct flow_dissector_key_ipv6_addrs enc_ipv6;
  44. };
  45. struct flow_dissector_key_ports enc_tp;
  46. struct flow_dissector_key_mpls mpls;
  47. struct flow_dissector_key_tcp tcp;
  48. struct flow_dissector_key_ip ip;
  49. } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  50. struct fl_flow_mask_range {
  51. unsigned short int start;
  52. unsigned short int end;
  53. };
  54. struct fl_flow_mask {
  55. struct fl_flow_key key;
  56. struct fl_flow_mask_range range;
  57. struct rcu_head rcu;
  58. };
  59. struct cls_fl_head {
  60. struct rhashtable ht;
  61. struct fl_flow_mask mask;
  62. struct flow_dissector dissector;
  63. bool mask_assigned;
  64. struct list_head filters;
  65. struct rhashtable_params ht_params;
  66. union {
  67. struct work_struct work;
  68. struct rcu_head rcu;
  69. };
  70. struct idr handle_idr;
  71. };
  72. struct cls_fl_filter {
  73. struct rhash_head ht_node;
  74. struct fl_flow_key mkey;
  75. struct tcf_exts exts;
  76. struct tcf_result res;
  77. struct fl_flow_key key;
  78. struct list_head list;
  79. u32 handle;
  80. u32 flags;
  81. union {
  82. struct work_struct work;
  83. struct rcu_head rcu;
  84. };
  85. struct net_device *hw_dev;
  86. };
  87. static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
  88. {
  89. return mask->range.end - mask->range.start;
  90. }
  91. static void fl_mask_update_range(struct fl_flow_mask *mask)
  92. {
  93. const u8 *bytes = (const u8 *) &mask->key;
  94. size_t size = sizeof(mask->key);
  95. size_t i, first = 0, last = size - 1;
  96. for (i = 0; i < sizeof(mask->key); i++) {
  97. if (bytes[i]) {
  98. if (!first && i)
  99. first = i;
  100. last = i;
  101. }
  102. }
  103. mask->range.start = rounddown(first, sizeof(long));
  104. mask->range.end = roundup(last + 1, sizeof(long));
  105. }
  106. static void *fl_key_get_start(struct fl_flow_key *key,
  107. const struct fl_flow_mask *mask)
  108. {
  109. return (u8 *) key + mask->range.start;
  110. }
  111. static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
  112. struct fl_flow_mask *mask)
  113. {
  114. const long *lkey = fl_key_get_start(key, mask);
  115. const long *lmask = fl_key_get_start(&mask->key, mask);
  116. long *lmkey = fl_key_get_start(mkey, mask);
  117. int i;
  118. for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
  119. *lmkey++ = *lkey++ & *lmask++;
  120. }
  121. static void fl_clear_masked_range(struct fl_flow_key *key,
  122. struct fl_flow_mask *mask)
  123. {
  124. memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
  125. }
  126. static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
  127. struct fl_flow_key *mkey)
  128. {
  129. return rhashtable_lookup_fast(&head->ht,
  130. fl_key_get_start(mkey, &head->mask),
  131. head->ht_params);
  132. }
  133. static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  134. struct tcf_result *res)
  135. {
  136. struct cls_fl_head *head = rcu_dereference_bh(tp->root);
  137. struct cls_fl_filter *f;
  138. struct fl_flow_key skb_key;
  139. struct fl_flow_key skb_mkey;
  140. struct ip_tunnel_info *info;
  141. if (!atomic_read(&head->ht.nelems))
  142. return -1;
  143. flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
  144. fl_clear_masked_range(&skb_key, &head->mask);
  145. info = skb_tunnel_info(skb);
  146. if (info) {
  147. struct ip_tunnel_key *key = &info->key;
  148. switch (ip_tunnel_info_af(info)) {
  149. case AF_INET:
  150. skb_key.enc_control.addr_type =
  151. FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  152. skb_key.enc_ipv4.src = key->u.ipv4.src;
  153. skb_key.enc_ipv4.dst = key->u.ipv4.dst;
  154. break;
  155. case AF_INET6:
  156. skb_key.enc_control.addr_type =
  157. FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  158. skb_key.enc_ipv6.src = key->u.ipv6.src;
  159. skb_key.enc_ipv6.dst = key->u.ipv6.dst;
  160. break;
  161. }
  162. skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
  163. skb_key.enc_tp.src = key->tp_src;
  164. skb_key.enc_tp.dst = key->tp_dst;
  165. }
  166. skb_key.indev_ifindex = skb->skb_iif;
  167. /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
  168. * so do it rather here.
  169. */
  170. skb_key.basic.n_proto = skb->protocol;
  171. skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
  172. fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
  173. f = fl_lookup(head, &skb_mkey);
  174. if (f && !tc_skip_sw(f->flags)) {
  175. *res = f->res;
  176. return tcf_exts_exec(skb, &f->exts, res);
  177. }
  178. return -1;
  179. }
  180. static int fl_init(struct tcf_proto *tp)
  181. {
  182. struct cls_fl_head *head;
  183. head = kzalloc(sizeof(*head), GFP_KERNEL);
  184. if (!head)
  185. return -ENOBUFS;
  186. INIT_LIST_HEAD_RCU(&head->filters);
  187. rcu_assign_pointer(tp->root, head);
  188. idr_init(&head->handle_idr);
  189. return 0;
  190. }
  191. static void __fl_destroy_filter(struct cls_fl_filter *f)
  192. {
  193. tcf_exts_destroy(&f->exts);
  194. tcf_exts_put_net(&f->exts);
  195. kfree(f);
  196. }
  197. static void fl_destroy_filter_work(struct work_struct *work)
  198. {
  199. struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);
  200. rtnl_lock();
  201. __fl_destroy_filter(f);
  202. rtnl_unlock();
  203. }
  204. static void fl_destroy_filter(struct rcu_head *head)
  205. {
  206. struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
  207. INIT_WORK(&f->work, fl_destroy_filter_work);
  208. tcf_queue_work(&f->work);
  209. }
  210. static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
  211. {
  212. struct tc_cls_flower_offload cls_flower = {};
  213. struct net_device *dev = f->hw_dev;
  214. if (!tc_can_offload(dev))
  215. return;
  216. tc_cls_common_offload_init(&cls_flower.common, tp);
  217. cls_flower.command = TC_CLSFLOWER_DESTROY;
  218. cls_flower.cookie = (unsigned long) f;
  219. cls_flower.egress_dev = f->hw_dev != tp->q->dev_queue->dev;
  220. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER, &cls_flower);
  221. }
  222. static int fl_hw_replace_filter(struct tcf_proto *tp,
  223. struct flow_dissector *dissector,
  224. struct fl_flow_key *mask,
  225. struct cls_fl_filter *f)
  226. {
  227. struct net_device *dev = tp->q->dev_queue->dev;
  228. struct tc_cls_flower_offload cls_flower = {};
  229. int err;
  230. if (!tc_can_offload(dev)) {
  231. if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
  232. (f->hw_dev && !tc_can_offload(f->hw_dev))) {
  233. f->hw_dev = dev;
  234. return tc_skip_sw(f->flags) ? -EINVAL : 0;
  235. }
  236. dev = f->hw_dev;
  237. cls_flower.egress_dev = true;
  238. } else {
  239. f->hw_dev = dev;
  240. }
  241. tc_cls_common_offload_init(&cls_flower.common, tp);
  242. cls_flower.command = TC_CLSFLOWER_REPLACE;
  243. cls_flower.cookie = (unsigned long) f;
  244. cls_flower.dissector = dissector;
  245. cls_flower.mask = mask;
  246. cls_flower.key = &f->mkey;
  247. cls_flower.exts = &f->exts;
  248. err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
  249. &cls_flower);
  250. if (!err)
  251. f->flags |= TCA_CLS_FLAGS_IN_HW;
  252. if (tc_skip_sw(f->flags))
  253. return err;
  254. return 0;
  255. }
  256. static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
  257. {
  258. struct tc_cls_flower_offload cls_flower = {};
  259. struct net_device *dev = f->hw_dev;
  260. if (!tc_can_offload(dev))
  261. return;
  262. tc_cls_common_offload_init(&cls_flower.common, tp);
  263. cls_flower.command = TC_CLSFLOWER_STATS;
  264. cls_flower.cookie = (unsigned long) f;
  265. cls_flower.exts = &f->exts;
  266. cls_flower.egress_dev = f->hw_dev != tp->q->dev_queue->dev;
  267. dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
  268. &cls_flower);
  269. }
  270. static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
  271. {
  272. struct cls_fl_head *head = rtnl_dereference(tp->root);
  273. idr_remove_ext(&head->handle_idr, f->handle);
  274. list_del_rcu(&f->list);
  275. if (!tc_skip_hw(f->flags))
  276. fl_hw_destroy_filter(tp, f);
  277. tcf_unbind_filter(tp, &f->res);
  278. if (tcf_exts_get_net(&f->exts))
  279. call_rcu(&f->rcu, fl_destroy_filter);
  280. else
  281. __fl_destroy_filter(f);
  282. }
  283. static void fl_destroy_sleepable(struct work_struct *work)
  284. {
  285. struct cls_fl_head *head = container_of(work, struct cls_fl_head,
  286. work);
  287. if (head->mask_assigned)
  288. rhashtable_destroy(&head->ht);
  289. kfree(head);
  290. module_put(THIS_MODULE);
  291. }
  292. static void fl_destroy_rcu(struct rcu_head *rcu)
  293. {
  294. struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
  295. INIT_WORK(&head->work, fl_destroy_sleepable);
  296. schedule_work(&head->work);
  297. }
  298. static void fl_destroy(struct tcf_proto *tp)
  299. {
  300. struct cls_fl_head *head = rtnl_dereference(tp->root);
  301. struct cls_fl_filter *f, *next;
  302. list_for_each_entry_safe(f, next, &head->filters, list)
  303. __fl_delete(tp, f);
  304. idr_destroy(&head->handle_idr);
  305. __module_get(THIS_MODULE);
  306. call_rcu(&head->rcu, fl_destroy_rcu);
  307. }
  308. static void *fl_get(struct tcf_proto *tp, u32 handle)
  309. {
  310. struct cls_fl_head *head = rtnl_dereference(tp->root);
  311. return idr_find_ext(&head->handle_idr, handle);
  312. }
  313. static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
  314. [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
  315. [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
  316. [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
  317. .len = IFNAMSIZ },
  318. [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
  319. [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
  320. [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
  321. [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
  322. [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
  323. [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
  324. [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
  325. [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
  326. [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
  327. [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
  328. [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  329. [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  330. [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  331. [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  332. [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
  333. [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
  334. [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
  335. [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
  336. [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
  337. [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
  338. [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
  339. [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  340. [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  341. [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
  342. [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  343. [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
  344. [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  345. [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  346. [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  347. [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  348. [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
  349. [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
  350. [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
  351. [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
  352. [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
  353. [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
  354. [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
  355. [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
  356. [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
  357. [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
  358. [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
  359. [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
  360. [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
  361. [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
  362. [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
  363. [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
  364. [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
  365. [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
  366. [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
  367. [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
  368. [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
  369. [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
  370. [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
  371. [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
  372. [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
  373. [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
  374. [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
  375. [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
  376. [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
  377. [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
  378. [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
  379. [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
  380. [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
  381. [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
  382. [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
  383. [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
  384. [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
  385. [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
  386. [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
  387. [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
  388. [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
  389. [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
  390. [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
  391. };
  392. static void fl_set_key_val(struct nlattr **tb,
  393. void *val, int val_type,
  394. void *mask, int mask_type, int len)
  395. {
  396. if (!tb[val_type])
  397. return;
  398. memcpy(val, nla_data(tb[val_type]), len);
  399. if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
  400. memset(mask, 0xff, len);
  401. else
  402. memcpy(mask, nla_data(tb[mask_type]), len);
  403. }
  404. static int fl_set_key_mpls(struct nlattr **tb,
  405. struct flow_dissector_key_mpls *key_val,
  406. struct flow_dissector_key_mpls *key_mask)
  407. {
  408. if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
  409. key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
  410. key_mask->mpls_ttl = MPLS_TTL_MASK;
  411. }
  412. if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
  413. u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
  414. if (bos & ~MPLS_BOS_MASK)
  415. return -EINVAL;
  416. key_val->mpls_bos = bos;
  417. key_mask->mpls_bos = MPLS_BOS_MASK;
  418. }
  419. if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
  420. u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
  421. if (tc & ~MPLS_TC_MASK)
  422. return -EINVAL;
  423. key_val->mpls_tc = tc;
  424. key_mask->mpls_tc = MPLS_TC_MASK;
  425. }
  426. if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
  427. u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
  428. if (label & ~MPLS_LABEL_MASK)
  429. return -EINVAL;
  430. key_val->mpls_label = label;
  431. key_mask->mpls_label = MPLS_LABEL_MASK;
  432. }
  433. return 0;
  434. }
  435. static void fl_set_key_vlan(struct nlattr **tb,
  436. struct flow_dissector_key_vlan *key_val,
  437. struct flow_dissector_key_vlan *key_mask)
  438. {
  439. #define VLAN_PRIORITY_MASK 0x7
  440. if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
  441. key_val->vlan_id =
  442. nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
  443. key_mask->vlan_id = VLAN_VID_MASK;
  444. }
  445. if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
  446. key_val->vlan_priority =
  447. nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
  448. VLAN_PRIORITY_MASK;
  449. key_mask->vlan_priority = VLAN_PRIORITY_MASK;
  450. }
  451. }
  452. static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
  453. u32 *dissector_key, u32 *dissector_mask,
  454. u32 flower_flag_bit, u32 dissector_flag_bit)
  455. {
  456. if (flower_mask & flower_flag_bit) {
  457. *dissector_mask |= dissector_flag_bit;
  458. if (flower_key & flower_flag_bit)
  459. *dissector_key |= dissector_flag_bit;
  460. }
  461. }
  462. static int fl_set_key_flags(struct nlattr **tb,
  463. u32 *flags_key, u32 *flags_mask)
  464. {
  465. u32 key, mask;
  466. /* mask is mandatory for flags */
  467. if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
  468. return -EINVAL;
  469. key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
  470. mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
  471. *flags_key = 0;
  472. *flags_mask = 0;
  473. fl_set_key_flag(key, mask, flags_key, flags_mask,
  474. TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
  475. return 0;
  476. }
  477. static void fl_set_key_ip(struct nlattr **tb,
  478. struct flow_dissector_key_ip *key,
  479. struct flow_dissector_key_ip *mask)
  480. {
  481. fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
  482. &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
  483. sizeof(key->tos));
  484. fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
  485. &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
  486. sizeof(key->ttl));
  487. }
  488. static int fl_set_key(struct net *net, struct nlattr **tb,
  489. struct fl_flow_key *key, struct fl_flow_key *mask)
  490. {
  491. __be16 ethertype;
  492. int ret = 0;
  493. #ifdef CONFIG_NET_CLS_IND
  494. if (tb[TCA_FLOWER_INDEV]) {
  495. int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
  496. if (err < 0)
  497. return err;
  498. key->indev_ifindex = err;
  499. mask->indev_ifindex = 0xffffffff;
  500. }
  501. #endif
  502. fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  503. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  504. sizeof(key->eth.dst));
  505. fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  506. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  507. sizeof(key->eth.src));
  508. if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
  509. ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
  510. if (ethertype == htons(ETH_P_8021Q)) {
  511. fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
  512. fl_set_key_val(tb, &key->basic.n_proto,
  513. TCA_FLOWER_KEY_VLAN_ETH_TYPE,
  514. &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
  515. sizeof(key->basic.n_proto));
  516. } else {
  517. key->basic.n_proto = ethertype;
  518. mask->basic.n_proto = cpu_to_be16(~0);
  519. }
  520. }
  521. if (key->basic.n_proto == htons(ETH_P_IP) ||
  522. key->basic.n_proto == htons(ETH_P_IPV6)) {
  523. fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  524. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  525. sizeof(key->basic.ip_proto));
  526. fl_set_key_ip(tb, &key->ip, &mask->ip);
  527. }
  528. if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
  529. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  530. mask->control.addr_type = ~0;
  531. fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  532. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  533. sizeof(key->ipv4.src));
  534. fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  535. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  536. sizeof(key->ipv4.dst));
  537. } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
  538. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  539. mask->control.addr_type = ~0;
  540. fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  541. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  542. sizeof(key->ipv6.src));
  543. fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  544. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  545. sizeof(key->ipv6.dst));
  546. }
  547. if (key->basic.ip_proto == IPPROTO_TCP) {
  548. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  549. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  550. sizeof(key->tp.src));
  551. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  552. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  553. sizeof(key->tp.dst));
  554. fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
  555. &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
  556. sizeof(key->tcp.flags));
  557. } else if (key->basic.ip_proto == IPPROTO_UDP) {
  558. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  559. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  560. sizeof(key->tp.src));
  561. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  562. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  563. sizeof(key->tp.dst));
  564. } else if (key->basic.ip_proto == IPPROTO_SCTP) {
  565. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
  566. &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
  567. sizeof(key->tp.src));
  568. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
  569. &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
  570. sizeof(key->tp.dst));
  571. } else if (key->basic.n_proto == htons(ETH_P_IP) &&
  572. key->basic.ip_proto == IPPROTO_ICMP) {
  573. fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
  574. &mask->icmp.type,
  575. TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
  576. sizeof(key->icmp.type));
  577. fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
  578. &mask->icmp.code,
  579. TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
  580. sizeof(key->icmp.code));
  581. } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
  582. key->basic.ip_proto == IPPROTO_ICMPV6) {
  583. fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
  584. &mask->icmp.type,
  585. TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
  586. sizeof(key->icmp.type));
  587. fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
  588. &mask->icmp.code,
  589. TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
  590. sizeof(key->icmp.code));
  591. } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
  592. key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
  593. ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
  594. if (ret)
  595. return ret;
  596. } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
  597. key->basic.n_proto == htons(ETH_P_RARP)) {
  598. fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
  599. &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
  600. sizeof(key->arp.sip));
  601. fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
  602. &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
  603. sizeof(key->arp.tip));
  604. fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
  605. &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
  606. sizeof(key->arp.op));
  607. fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
  608. mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
  609. sizeof(key->arp.sha));
  610. fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
  611. mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
  612. sizeof(key->arp.tha));
  613. }
  614. if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
  615. tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
  616. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  617. mask->enc_control.addr_type = ~0;
  618. fl_set_key_val(tb, &key->enc_ipv4.src,
  619. TCA_FLOWER_KEY_ENC_IPV4_SRC,
  620. &mask->enc_ipv4.src,
  621. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  622. sizeof(key->enc_ipv4.src));
  623. fl_set_key_val(tb, &key->enc_ipv4.dst,
  624. TCA_FLOWER_KEY_ENC_IPV4_DST,
  625. &mask->enc_ipv4.dst,
  626. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  627. sizeof(key->enc_ipv4.dst));
  628. }
  629. if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
  630. tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
  631. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  632. mask->enc_control.addr_type = ~0;
  633. fl_set_key_val(tb, &key->enc_ipv6.src,
  634. TCA_FLOWER_KEY_ENC_IPV6_SRC,
  635. &mask->enc_ipv6.src,
  636. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  637. sizeof(key->enc_ipv6.src));
  638. fl_set_key_val(tb, &key->enc_ipv6.dst,
  639. TCA_FLOWER_KEY_ENC_IPV6_DST,
  640. &mask->enc_ipv6.dst,
  641. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  642. sizeof(key->enc_ipv6.dst));
  643. }
  644. fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
  645. &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
  646. sizeof(key->enc_key_id.keyid));
  647. fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
  648. &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
  649. sizeof(key->enc_tp.src));
  650. fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
  651. &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
  652. sizeof(key->enc_tp.dst));
  653. if (tb[TCA_FLOWER_KEY_FLAGS])
  654. ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
  655. return ret;
  656. }
  657. static bool fl_mask_eq(struct fl_flow_mask *mask1,
  658. struct fl_flow_mask *mask2)
  659. {
  660. const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
  661. const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
  662. return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
  663. !memcmp(lmask1, lmask2, fl_mask_range(mask1));
  664. }
  665. static const struct rhashtable_params fl_ht_params = {
  666. .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
  667. .head_offset = offsetof(struct cls_fl_filter, ht_node),
  668. .automatic_shrinking = true,
  669. };
  670. static int fl_init_hashtable(struct cls_fl_head *head,
  671. struct fl_flow_mask *mask)
  672. {
  673. head->ht_params = fl_ht_params;
  674. head->ht_params.key_len = fl_mask_range(mask);
  675. head->ht_params.key_offset += mask->range.start;
  676. return rhashtable_init(&head->ht, &head->ht_params);
  677. }
  678. #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
  679. #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
  680. #define FL_KEY_IS_MASKED(mask, member) \
  681. memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
  682. 0, FL_KEY_MEMBER_SIZE(member)) \
  683. #define FL_KEY_SET(keys, cnt, id, member) \
  684. do { \
  685. keys[cnt].key_id = id; \
  686. keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
  687. cnt++; \
  688. } while(0);
  689. #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
  690. do { \
  691. if (FL_KEY_IS_MASKED(mask, member)) \
  692. FL_KEY_SET(keys, cnt, id, member); \
  693. } while(0);
  694. static void fl_init_dissector(struct cls_fl_head *head,
  695. struct fl_flow_mask *mask)
  696. {
  697. struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
  698. size_t cnt = 0;
  699. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
  700. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
  701. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  702. FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
  703. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  704. FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
  705. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  706. FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
  707. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  708. FLOW_DISSECTOR_KEY_PORTS, tp);
  709. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  710. FLOW_DISSECTOR_KEY_IP, ip);
  711. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  712. FLOW_DISSECTOR_KEY_TCP, tcp);
  713. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  714. FLOW_DISSECTOR_KEY_ICMP, icmp);
  715. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  716. FLOW_DISSECTOR_KEY_ARP, arp);
  717. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  718. FLOW_DISSECTOR_KEY_MPLS, mpls);
  719. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  720. FLOW_DISSECTOR_KEY_VLAN, vlan);
  721. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  722. FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
  723. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  724. FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
  725. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  726. FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
  727. if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
  728. FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
  729. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
  730. enc_control);
  731. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  732. FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
  733. skb_flow_dissector_init(&head->dissector, keys, cnt);
  734. }
  735. static int fl_check_assign_mask(struct cls_fl_head *head,
  736. struct fl_flow_mask *mask)
  737. {
  738. int err;
  739. if (head->mask_assigned) {
  740. if (!fl_mask_eq(&head->mask, mask))
  741. return -EINVAL;
  742. else
  743. return 0;
  744. }
  745. /* Mask is not assigned yet. So assign it and init hashtable
  746. * according to that.
  747. */
  748. err = fl_init_hashtable(head, mask);
  749. if (err)
  750. return err;
  751. memcpy(&head->mask, mask, sizeof(head->mask));
  752. head->mask_assigned = true;
  753. fl_init_dissector(head, mask);
  754. return 0;
  755. }
  756. static int fl_set_parms(struct net *net, struct tcf_proto *tp,
  757. struct cls_fl_filter *f, struct fl_flow_mask *mask,
  758. unsigned long base, struct nlattr **tb,
  759. struct nlattr *est, bool ovr)
  760. {
  761. int err;
  762. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
  763. if (err < 0)
  764. return err;
  765. if (tb[TCA_FLOWER_CLASSID]) {
  766. f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
  767. tcf_bind_filter(tp, &f->res, base);
  768. }
  769. err = fl_set_key(net, tb, &f->key, &mask->key);
  770. if (err)
  771. return err;
  772. fl_mask_update_range(mask);
  773. fl_set_masked_key(&f->mkey, &f->key, mask);
  774. return 0;
  775. }
  776. static int fl_change(struct net *net, struct sk_buff *in_skb,
  777. struct tcf_proto *tp, unsigned long base,
  778. u32 handle, struct nlattr **tca,
  779. void **arg, bool ovr)
  780. {
  781. struct cls_fl_head *head = rtnl_dereference(tp->root);
  782. struct cls_fl_filter *fold = *arg;
  783. struct cls_fl_filter *fnew;
  784. struct nlattr **tb;
  785. struct fl_flow_mask mask = {};
  786. unsigned long idr_index;
  787. int err;
  788. if (!tca[TCA_OPTIONS])
  789. return -EINVAL;
  790. tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
  791. if (!tb)
  792. return -ENOBUFS;
  793. err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
  794. fl_policy, NULL);
  795. if (err < 0)
  796. goto errout_tb;
  797. if (fold && handle && fold->handle != handle) {
  798. err = -EINVAL;
  799. goto errout_tb;
  800. }
  801. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  802. if (!fnew) {
  803. err = -ENOBUFS;
  804. goto errout_tb;
  805. }
  806. err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
  807. if (err < 0)
  808. goto errout;
  809. if (!handle) {
  810. err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
  811. 1, 0x80000000, GFP_KERNEL);
  812. if (err)
  813. goto errout;
  814. fnew->handle = idr_index;
  815. }
  816. /* user specifies a handle and it doesn't exist */
  817. if (handle && !fold) {
  818. err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
  819. handle, handle + 1, GFP_KERNEL);
  820. if (err)
  821. goto errout;
  822. fnew->handle = idr_index;
  823. }
  824. if (tb[TCA_FLOWER_FLAGS]) {
  825. fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
  826. if (!tc_flags_valid(fnew->flags)) {
  827. err = -EINVAL;
  828. goto errout_idr;
  829. }
  830. }
  831. err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
  832. if (err)
  833. goto errout_idr;
  834. err = fl_check_assign_mask(head, &mask);
  835. if (err)
  836. goto errout_idr;
  837. if (!tc_skip_sw(fnew->flags)) {
  838. if (!fold && fl_lookup(head, &fnew->mkey)) {
  839. err = -EEXIST;
  840. goto errout_idr;
  841. }
  842. err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
  843. head->ht_params);
  844. if (err)
  845. goto errout_idr;
  846. }
  847. if (!tc_skip_hw(fnew->flags)) {
  848. err = fl_hw_replace_filter(tp,
  849. &head->dissector,
  850. &mask.key,
  851. fnew);
  852. if (err)
  853. goto errout_idr;
  854. }
  855. if (!tc_in_hw(fnew->flags))
  856. fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  857. if (fold) {
  858. if (!tc_skip_sw(fold->flags))
  859. rhashtable_remove_fast(&head->ht, &fold->ht_node,
  860. head->ht_params);
  861. if (!tc_skip_hw(fold->flags))
  862. fl_hw_destroy_filter(tp, fold);
  863. }
  864. *arg = fnew;
  865. if (fold) {
  866. fnew->handle = handle;
  867. idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
  868. list_replace_rcu(&fold->list, &fnew->list);
  869. tcf_unbind_filter(tp, &fold->res);
  870. tcf_exts_get_net(&fold->exts);
  871. call_rcu(&fold->rcu, fl_destroy_filter);
  872. } else {
  873. list_add_tail_rcu(&fnew->list, &head->filters);
  874. }
  875. kfree(tb);
  876. return 0;
  877. errout_idr:
  878. if (!fold)
  879. idr_remove_ext(&head->handle_idr, fnew->handle);
  880. errout:
  881. tcf_exts_destroy(&fnew->exts);
  882. kfree(fnew);
  883. errout_tb:
  884. kfree(tb);
  885. return err;
  886. }
  887. static int fl_delete(struct tcf_proto *tp, void *arg, bool *last)
  888. {
  889. struct cls_fl_head *head = rtnl_dereference(tp->root);
  890. struct cls_fl_filter *f = arg;
  891. if (!tc_skip_sw(f->flags))
  892. rhashtable_remove_fast(&head->ht, &f->ht_node,
  893. head->ht_params);
  894. __fl_delete(tp, f);
  895. *last = list_empty(&head->filters);
  896. return 0;
  897. }
  898. static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  899. {
  900. struct cls_fl_head *head = rtnl_dereference(tp->root);
  901. struct cls_fl_filter *f;
  902. list_for_each_entry_rcu(f, &head->filters, list) {
  903. if (arg->count < arg->skip)
  904. goto skip;
  905. if (arg->fn(tp, f, arg) < 0) {
  906. arg->stop = 1;
  907. break;
  908. }
  909. skip:
  910. arg->count++;
  911. }
  912. }
  913. static int fl_dump_key_val(struct sk_buff *skb,
  914. void *val, int val_type,
  915. void *mask, int mask_type, int len)
  916. {
  917. int err;
  918. if (!memchr_inv(mask, 0, len))
  919. return 0;
  920. err = nla_put(skb, val_type, len, val);
  921. if (err)
  922. return err;
  923. if (mask_type != TCA_FLOWER_UNSPEC) {
  924. err = nla_put(skb, mask_type, len, mask);
  925. if (err)
  926. return err;
  927. }
  928. return 0;
  929. }
  930. static int fl_dump_key_mpls(struct sk_buff *skb,
  931. struct flow_dissector_key_mpls *mpls_key,
  932. struct flow_dissector_key_mpls *mpls_mask)
  933. {
  934. int err;
  935. if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
  936. return 0;
  937. if (mpls_mask->mpls_ttl) {
  938. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
  939. mpls_key->mpls_ttl);
  940. if (err)
  941. return err;
  942. }
  943. if (mpls_mask->mpls_tc) {
  944. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
  945. mpls_key->mpls_tc);
  946. if (err)
  947. return err;
  948. }
  949. if (mpls_mask->mpls_label) {
  950. err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
  951. mpls_key->mpls_label);
  952. if (err)
  953. return err;
  954. }
  955. if (mpls_mask->mpls_bos) {
  956. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
  957. mpls_key->mpls_bos);
  958. if (err)
  959. return err;
  960. }
  961. return 0;
  962. }
  963. static int fl_dump_key_ip(struct sk_buff *skb,
  964. struct flow_dissector_key_ip *key,
  965. struct flow_dissector_key_ip *mask)
  966. {
  967. if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
  968. TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
  969. fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
  970. TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
  971. return -1;
  972. return 0;
  973. }
  974. static int fl_dump_key_vlan(struct sk_buff *skb,
  975. struct flow_dissector_key_vlan *vlan_key,
  976. struct flow_dissector_key_vlan *vlan_mask)
  977. {
  978. int err;
  979. if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
  980. return 0;
  981. if (vlan_mask->vlan_id) {
  982. err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
  983. vlan_key->vlan_id);
  984. if (err)
  985. return err;
  986. }
  987. if (vlan_mask->vlan_priority) {
  988. err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
  989. vlan_key->vlan_priority);
  990. if (err)
  991. return err;
  992. }
  993. return 0;
  994. }
  995. static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
  996. u32 *flower_key, u32 *flower_mask,
  997. u32 flower_flag_bit, u32 dissector_flag_bit)
  998. {
  999. if (dissector_mask & dissector_flag_bit) {
  1000. *flower_mask |= flower_flag_bit;
  1001. if (dissector_key & dissector_flag_bit)
  1002. *flower_key |= flower_flag_bit;
  1003. }
  1004. }
  1005. static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
  1006. {
  1007. u32 key, mask;
  1008. __be32 _key, _mask;
  1009. int err;
  1010. if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
  1011. return 0;
  1012. key = 0;
  1013. mask = 0;
  1014. fl_get_key_flag(flags_key, flags_mask, &key, &mask,
  1015. TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
  1016. _key = cpu_to_be32(key);
  1017. _mask = cpu_to_be32(mask);
  1018. err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
  1019. if (err)
  1020. return err;
  1021. return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
  1022. }
  1023. static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
  1024. struct sk_buff *skb, struct tcmsg *t)
  1025. {
  1026. struct cls_fl_head *head = rtnl_dereference(tp->root);
  1027. struct cls_fl_filter *f = fh;
  1028. struct nlattr *nest;
  1029. struct fl_flow_key *key, *mask;
  1030. if (!f)
  1031. return skb->len;
  1032. t->tcm_handle = f->handle;
  1033. nest = nla_nest_start(skb, TCA_OPTIONS);
  1034. if (!nest)
  1035. goto nla_put_failure;
  1036. if (f->res.classid &&
  1037. nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
  1038. goto nla_put_failure;
  1039. key = &f->key;
  1040. mask = &head->mask.key;
  1041. if (mask->indev_ifindex) {
  1042. struct net_device *dev;
  1043. dev = __dev_get_by_index(net, key->indev_ifindex);
  1044. if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
  1045. goto nla_put_failure;
  1046. }
  1047. if (!tc_skip_hw(f->flags))
  1048. fl_hw_update_stats(tp, f);
  1049. if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  1050. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  1051. sizeof(key->eth.dst)) ||
  1052. fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  1053. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  1054. sizeof(key->eth.src)) ||
  1055. fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
  1056. &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
  1057. sizeof(key->basic.n_proto)))
  1058. goto nla_put_failure;
  1059. if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
  1060. goto nla_put_failure;
  1061. if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
  1062. goto nla_put_failure;
  1063. if ((key->basic.n_proto == htons(ETH_P_IP) ||
  1064. key->basic.n_proto == htons(ETH_P_IPV6)) &&
  1065. (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  1066. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  1067. sizeof(key->basic.ip_proto)) ||
  1068. fl_dump_key_ip(skb, &key->ip, &mask->ip)))
  1069. goto nla_put_failure;
  1070. if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  1071. (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  1072. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  1073. sizeof(key->ipv4.src)) ||
  1074. fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  1075. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  1076. sizeof(key->ipv4.dst))))
  1077. goto nla_put_failure;
  1078. else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  1079. (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  1080. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  1081. sizeof(key->ipv6.src)) ||
  1082. fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  1083. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  1084. sizeof(key->ipv6.dst))))
  1085. goto nla_put_failure;
  1086. if (key->basic.ip_proto == IPPROTO_TCP &&
  1087. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  1088. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  1089. sizeof(key->tp.src)) ||
  1090. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  1091. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  1092. sizeof(key->tp.dst)) ||
  1093. fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
  1094. &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
  1095. sizeof(key->tcp.flags))))
  1096. goto nla_put_failure;
  1097. else if (key->basic.ip_proto == IPPROTO_UDP &&
  1098. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  1099. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  1100. sizeof(key->tp.src)) ||
  1101. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  1102. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  1103. sizeof(key->tp.dst))))
  1104. goto nla_put_failure;
  1105. else if (key->basic.ip_proto == IPPROTO_SCTP &&
  1106. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
  1107. &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
  1108. sizeof(key->tp.src)) ||
  1109. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
  1110. &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
  1111. sizeof(key->tp.dst))))
  1112. goto nla_put_failure;
  1113. else if (key->basic.n_proto == htons(ETH_P_IP) &&
  1114. key->basic.ip_proto == IPPROTO_ICMP &&
  1115. (fl_dump_key_val(skb, &key->icmp.type,
  1116. TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
  1117. TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
  1118. sizeof(key->icmp.type)) ||
  1119. fl_dump_key_val(skb, &key->icmp.code,
  1120. TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
  1121. TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
  1122. sizeof(key->icmp.code))))
  1123. goto nla_put_failure;
  1124. else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
  1125. key->basic.ip_proto == IPPROTO_ICMPV6 &&
  1126. (fl_dump_key_val(skb, &key->icmp.type,
  1127. TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
  1128. TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
  1129. sizeof(key->icmp.type)) ||
  1130. fl_dump_key_val(skb, &key->icmp.code,
  1131. TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
  1132. TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
  1133. sizeof(key->icmp.code))))
  1134. goto nla_put_failure;
  1135. else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
  1136. key->basic.n_proto == htons(ETH_P_RARP)) &&
  1137. (fl_dump_key_val(skb, &key->arp.sip,
  1138. TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
  1139. TCA_FLOWER_KEY_ARP_SIP_MASK,
  1140. sizeof(key->arp.sip)) ||
  1141. fl_dump_key_val(skb, &key->arp.tip,
  1142. TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
  1143. TCA_FLOWER_KEY_ARP_TIP_MASK,
  1144. sizeof(key->arp.tip)) ||
  1145. fl_dump_key_val(skb, &key->arp.op,
  1146. TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
  1147. TCA_FLOWER_KEY_ARP_OP_MASK,
  1148. sizeof(key->arp.op)) ||
  1149. fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
  1150. mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
  1151. sizeof(key->arp.sha)) ||
  1152. fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
  1153. mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
  1154. sizeof(key->arp.tha))))
  1155. goto nla_put_failure;
  1156. if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  1157. (fl_dump_key_val(skb, &key->enc_ipv4.src,
  1158. TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
  1159. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  1160. sizeof(key->enc_ipv4.src)) ||
  1161. fl_dump_key_val(skb, &key->enc_ipv4.dst,
  1162. TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
  1163. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  1164. sizeof(key->enc_ipv4.dst))))
  1165. goto nla_put_failure;
  1166. else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  1167. (fl_dump_key_val(skb, &key->enc_ipv6.src,
  1168. TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
  1169. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  1170. sizeof(key->enc_ipv6.src)) ||
  1171. fl_dump_key_val(skb, &key->enc_ipv6.dst,
  1172. TCA_FLOWER_KEY_ENC_IPV6_DST,
  1173. &mask->enc_ipv6.dst,
  1174. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  1175. sizeof(key->enc_ipv6.dst))))
  1176. goto nla_put_failure;
  1177. if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
  1178. &mask->enc_key_id, TCA_FLOWER_UNSPEC,
  1179. sizeof(key->enc_key_id)) ||
  1180. fl_dump_key_val(skb, &key->enc_tp.src,
  1181. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
  1182. &mask->enc_tp.src,
  1183. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
  1184. sizeof(key->enc_tp.src)) ||
  1185. fl_dump_key_val(skb, &key->enc_tp.dst,
  1186. TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
  1187. &mask->enc_tp.dst,
  1188. TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
  1189. sizeof(key->enc_tp.dst)))
  1190. goto nla_put_failure;
  1191. if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
  1192. goto nla_put_failure;
  1193. if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
  1194. goto nla_put_failure;
  1195. if (tcf_exts_dump(skb, &f->exts))
  1196. goto nla_put_failure;
  1197. nla_nest_end(skb, nest);
  1198. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  1199. goto nla_put_failure;
  1200. return skb->len;
  1201. nla_put_failure:
  1202. nla_nest_cancel(skb, nest);
  1203. return -1;
  1204. }
  1205. static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
  1206. {
  1207. struct cls_fl_filter *f = fh;
  1208. if (f && f->res.classid == classid)
  1209. f->res.class = cl;
  1210. }
  1211. static struct tcf_proto_ops cls_fl_ops __read_mostly = {
  1212. .kind = "flower",
  1213. .classify = fl_classify,
  1214. .init = fl_init,
  1215. .destroy = fl_destroy,
  1216. .get = fl_get,
  1217. .change = fl_change,
  1218. .delete = fl_delete,
  1219. .walk = fl_walk,
  1220. .dump = fl_dump,
  1221. .bind_class = fl_bind_class,
  1222. .owner = THIS_MODULE,
  1223. };
  1224. static int __init cls_fl_init(void)
  1225. {
  1226. return register_tcf_proto_ops(&cls_fl_ops);
  1227. }
  1228. static void __exit cls_fl_exit(void)
  1229. {
  1230. unregister_tcf_proto_ops(&cls_fl_ops);
  1231. }
  1232. module_init(cls_fl_init);
  1233. module_exit(cls_fl_exit);
  1234. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  1235. MODULE_DESCRIPTION("Flower classifier");
  1236. MODULE_LICENSE("GPL v2");