cls_flower.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 <net/sch_generic.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/ip.h>
  22. #include <net/flow_dissector.h>
  23. #include <net/dst.h>
  24. #include <net/dst_metadata.h>
  25. struct fl_flow_key {
  26. int indev_ifindex;
  27. struct flow_dissector_key_control control;
  28. struct flow_dissector_key_control enc_control;
  29. struct flow_dissector_key_basic basic;
  30. struct flow_dissector_key_eth_addrs eth;
  31. struct flow_dissector_key_vlan vlan;
  32. union {
  33. struct flow_dissector_key_ipv4_addrs ipv4;
  34. struct flow_dissector_key_ipv6_addrs ipv6;
  35. };
  36. struct flow_dissector_key_ports tp;
  37. struct flow_dissector_key_keyid enc_key_id;
  38. union {
  39. struct flow_dissector_key_ipv4_addrs enc_ipv4;
  40. struct flow_dissector_key_ipv6_addrs enc_ipv6;
  41. };
  42. } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  43. struct fl_flow_mask_range {
  44. unsigned short int start;
  45. unsigned short int end;
  46. };
  47. struct fl_flow_mask {
  48. struct fl_flow_key key;
  49. struct fl_flow_mask_range range;
  50. struct rcu_head rcu;
  51. };
  52. struct cls_fl_head {
  53. struct rhashtable ht;
  54. struct fl_flow_mask mask;
  55. struct flow_dissector dissector;
  56. u32 hgen;
  57. bool mask_assigned;
  58. struct list_head filters;
  59. struct rhashtable_params ht_params;
  60. union {
  61. struct work_struct work;
  62. struct rcu_head rcu;
  63. };
  64. };
  65. struct cls_fl_filter {
  66. struct rhash_head ht_node;
  67. struct fl_flow_key mkey;
  68. struct tcf_exts exts;
  69. struct tcf_result res;
  70. struct fl_flow_key key;
  71. struct list_head list;
  72. u32 handle;
  73. u32 flags;
  74. struct rcu_head rcu;
  75. };
  76. static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
  77. {
  78. return mask->range.end - mask->range.start;
  79. }
  80. static void fl_mask_update_range(struct fl_flow_mask *mask)
  81. {
  82. const u8 *bytes = (const u8 *) &mask->key;
  83. size_t size = sizeof(mask->key);
  84. size_t i, first = 0, last = size - 1;
  85. for (i = 0; i < sizeof(mask->key); i++) {
  86. if (bytes[i]) {
  87. if (!first && i)
  88. first = i;
  89. last = i;
  90. }
  91. }
  92. mask->range.start = rounddown(first, sizeof(long));
  93. mask->range.end = roundup(last + 1, sizeof(long));
  94. }
  95. static void *fl_key_get_start(struct fl_flow_key *key,
  96. const struct fl_flow_mask *mask)
  97. {
  98. return (u8 *) key + mask->range.start;
  99. }
  100. static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
  101. struct fl_flow_mask *mask)
  102. {
  103. const long *lkey = fl_key_get_start(key, mask);
  104. const long *lmask = fl_key_get_start(&mask->key, mask);
  105. long *lmkey = fl_key_get_start(mkey, mask);
  106. int i;
  107. for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
  108. *lmkey++ = *lkey++ & *lmask++;
  109. }
  110. static void fl_clear_masked_range(struct fl_flow_key *key,
  111. struct fl_flow_mask *mask)
  112. {
  113. memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
  114. }
  115. static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  116. struct tcf_result *res)
  117. {
  118. struct cls_fl_head *head = rcu_dereference_bh(tp->root);
  119. struct cls_fl_filter *f;
  120. struct fl_flow_key skb_key;
  121. struct fl_flow_key skb_mkey;
  122. struct ip_tunnel_info *info;
  123. if (!atomic_read(&head->ht.nelems))
  124. return -1;
  125. fl_clear_masked_range(&skb_key, &head->mask);
  126. info = skb_tunnel_info(skb);
  127. if (info) {
  128. struct ip_tunnel_key *key = &info->key;
  129. switch (ip_tunnel_info_af(info)) {
  130. case AF_INET:
  131. skb_key.enc_control.addr_type =
  132. FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  133. skb_key.enc_ipv4.src = key->u.ipv4.src;
  134. skb_key.enc_ipv4.dst = key->u.ipv4.dst;
  135. break;
  136. case AF_INET6:
  137. skb_key.enc_control.addr_type =
  138. FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  139. skb_key.enc_ipv6.src = key->u.ipv6.src;
  140. skb_key.enc_ipv6.dst = key->u.ipv6.dst;
  141. break;
  142. }
  143. skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
  144. }
  145. skb_key.indev_ifindex = skb->skb_iif;
  146. /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
  147. * so do it rather here.
  148. */
  149. skb_key.basic.n_proto = skb->protocol;
  150. skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
  151. fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
  152. f = rhashtable_lookup_fast(&head->ht,
  153. fl_key_get_start(&skb_mkey, &head->mask),
  154. head->ht_params);
  155. if (f && !tc_skip_sw(f->flags)) {
  156. *res = f->res;
  157. return tcf_exts_exec(skb, &f->exts, res);
  158. }
  159. return -1;
  160. }
  161. static int fl_init(struct tcf_proto *tp)
  162. {
  163. struct cls_fl_head *head;
  164. head = kzalloc(sizeof(*head), GFP_KERNEL);
  165. if (!head)
  166. return -ENOBUFS;
  167. INIT_LIST_HEAD_RCU(&head->filters);
  168. rcu_assign_pointer(tp->root, head);
  169. return 0;
  170. }
  171. static void fl_destroy_filter(struct rcu_head *head)
  172. {
  173. struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
  174. tcf_exts_destroy(&f->exts);
  175. kfree(f);
  176. }
  177. static void fl_hw_destroy_filter(struct tcf_proto *tp, unsigned long cookie)
  178. {
  179. struct net_device *dev = tp->q->dev_queue->dev;
  180. struct tc_cls_flower_offload offload = {0};
  181. struct tc_to_netdev tc;
  182. if (!tc_should_offload(dev, tp, 0))
  183. return;
  184. offload.command = TC_CLSFLOWER_DESTROY;
  185. offload.cookie = cookie;
  186. tc.type = TC_SETUP_CLSFLOWER;
  187. tc.cls_flower = &offload;
  188. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc);
  189. }
  190. static int fl_hw_replace_filter(struct tcf_proto *tp,
  191. struct flow_dissector *dissector,
  192. struct fl_flow_key *mask,
  193. struct fl_flow_key *key,
  194. struct tcf_exts *actions,
  195. unsigned long cookie, u32 flags)
  196. {
  197. struct net_device *dev = tp->q->dev_queue->dev;
  198. struct tc_cls_flower_offload offload = {0};
  199. struct tc_to_netdev tc;
  200. int err;
  201. if (!tc_should_offload(dev, tp, flags))
  202. return tc_skip_sw(flags) ? -EINVAL : 0;
  203. offload.command = TC_CLSFLOWER_REPLACE;
  204. offload.cookie = cookie;
  205. offload.dissector = dissector;
  206. offload.mask = mask;
  207. offload.key = key;
  208. offload.exts = actions;
  209. tc.type = TC_SETUP_CLSFLOWER;
  210. tc.cls_flower = &offload;
  211. err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
  212. &tc);
  213. if (tc_skip_sw(flags))
  214. return err;
  215. return 0;
  216. }
  217. static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
  218. {
  219. struct net_device *dev = tp->q->dev_queue->dev;
  220. struct tc_cls_flower_offload offload = {0};
  221. struct tc_to_netdev tc;
  222. if (!tc_should_offload(dev, tp, 0))
  223. return;
  224. offload.command = TC_CLSFLOWER_STATS;
  225. offload.cookie = (unsigned long)f;
  226. offload.exts = &f->exts;
  227. tc.type = TC_SETUP_CLSFLOWER;
  228. tc.cls_flower = &offload;
  229. dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc);
  230. }
  231. static void fl_destroy_sleepable(struct work_struct *work)
  232. {
  233. struct cls_fl_head *head = container_of(work, struct cls_fl_head,
  234. work);
  235. if (head->mask_assigned)
  236. rhashtable_destroy(&head->ht);
  237. kfree(head);
  238. module_put(THIS_MODULE);
  239. }
  240. static void fl_destroy_rcu(struct rcu_head *rcu)
  241. {
  242. struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
  243. INIT_WORK(&head->work, fl_destroy_sleepable);
  244. schedule_work(&head->work);
  245. }
  246. static bool fl_destroy(struct tcf_proto *tp, bool force)
  247. {
  248. struct cls_fl_head *head = rtnl_dereference(tp->root);
  249. struct cls_fl_filter *f, *next;
  250. if (!force && !list_empty(&head->filters))
  251. return false;
  252. list_for_each_entry_safe(f, next, &head->filters, list) {
  253. fl_hw_destroy_filter(tp, (unsigned long)f);
  254. list_del_rcu(&f->list);
  255. call_rcu(&f->rcu, fl_destroy_filter);
  256. }
  257. __module_get(THIS_MODULE);
  258. call_rcu(&head->rcu, fl_destroy_rcu);
  259. return true;
  260. }
  261. static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
  262. {
  263. struct cls_fl_head *head = rtnl_dereference(tp->root);
  264. struct cls_fl_filter *f;
  265. list_for_each_entry(f, &head->filters, list)
  266. if (f->handle == handle)
  267. return (unsigned long) f;
  268. return 0;
  269. }
  270. static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
  271. [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
  272. [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
  273. [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
  274. .len = IFNAMSIZ },
  275. [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
  276. [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
  277. [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
  278. [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
  279. [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
  280. [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
  281. [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
  282. [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
  283. [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
  284. [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
  285. [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  286. [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  287. [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  288. [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  289. [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
  290. [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
  291. [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
  292. [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
  293. [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
  294. [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
  295. [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
  296. [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  297. [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  298. [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
  299. [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  300. [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
  301. [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  302. [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  303. [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  304. [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  305. [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
  306. [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
  307. [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
  308. [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
  309. };
  310. static void fl_set_key_val(struct nlattr **tb,
  311. void *val, int val_type,
  312. void *mask, int mask_type, int len)
  313. {
  314. if (!tb[val_type])
  315. return;
  316. memcpy(val, nla_data(tb[val_type]), len);
  317. if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
  318. memset(mask, 0xff, len);
  319. else
  320. memcpy(mask, nla_data(tb[mask_type]), len);
  321. }
  322. static void fl_set_key_vlan(struct nlattr **tb,
  323. struct flow_dissector_key_vlan *key_val,
  324. struct flow_dissector_key_vlan *key_mask)
  325. {
  326. #define VLAN_PRIORITY_MASK 0x7
  327. if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
  328. key_val->vlan_id =
  329. nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
  330. key_mask->vlan_id = VLAN_VID_MASK;
  331. }
  332. if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
  333. key_val->vlan_priority =
  334. nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
  335. VLAN_PRIORITY_MASK;
  336. key_mask->vlan_priority = VLAN_PRIORITY_MASK;
  337. }
  338. }
  339. static int fl_set_key(struct net *net, struct nlattr **tb,
  340. struct fl_flow_key *key, struct fl_flow_key *mask)
  341. {
  342. __be16 ethertype;
  343. #ifdef CONFIG_NET_CLS_IND
  344. if (tb[TCA_FLOWER_INDEV]) {
  345. int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
  346. if (err < 0)
  347. return err;
  348. key->indev_ifindex = err;
  349. mask->indev_ifindex = 0xffffffff;
  350. }
  351. #endif
  352. fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  353. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  354. sizeof(key->eth.dst));
  355. fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  356. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  357. sizeof(key->eth.src));
  358. if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
  359. ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
  360. if (ethertype == htons(ETH_P_8021Q)) {
  361. fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
  362. fl_set_key_val(tb, &key->basic.n_proto,
  363. TCA_FLOWER_KEY_VLAN_ETH_TYPE,
  364. &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
  365. sizeof(key->basic.n_proto));
  366. } else {
  367. key->basic.n_proto = ethertype;
  368. mask->basic.n_proto = cpu_to_be16(~0);
  369. }
  370. }
  371. if (key->basic.n_proto == htons(ETH_P_IP) ||
  372. key->basic.n_proto == htons(ETH_P_IPV6)) {
  373. fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  374. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  375. sizeof(key->basic.ip_proto));
  376. }
  377. if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
  378. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  379. fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  380. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  381. sizeof(key->ipv4.src));
  382. fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  383. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  384. sizeof(key->ipv4.dst));
  385. } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
  386. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  387. fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  388. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  389. sizeof(key->ipv6.src));
  390. fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  391. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  392. sizeof(key->ipv6.dst));
  393. }
  394. if (key->basic.ip_proto == IPPROTO_TCP) {
  395. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  396. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  397. sizeof(key->tp.src));
  398. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  399. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  400. sizeof(key->tp.dst));
  401. } else if (key->basic.ip_proto == IPPROTO_UDP) {
  402. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  403. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  404. sizeof(key->tp.src));
  405. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  406. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  407. sizeof(key->tp.dst));
  408. }
  409. if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
  410. tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
  411. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  412. fl_set_key_val(tb, &key->enc_ipv4.src,
  413. TCA_FLOWER_KEY_ENC_IPV4_SRC,
  414. &mask->enc_ipv4.src,
  415. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  416. sizeof(key->enc_ipv4.src));
  417. fl_set_key_val(tb, &key->enc_ipv4.dst,
  418. TCA_FLOWER_KEY_ENC_IPV4_DST,
  419. &mask->enc_ipv4.dst,
  420. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  421. sizeof(key->enc_ipv4.dst));
  422. }
  423. if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
  424. tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
  425. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  426. fl_set_key_val(tb, &key->enc_ipv6.src,
  427. TCA_FLOWER_KEY_ENC_IPV6_SRC,
  428. &mask->enc_ipv6.src,
  429. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  430. sizeof(key->enc_ipv6.src));
  431. fl_set_key_val(tb, &key->enc_ipv6.dst,
  432. TCA_FLOWER_KEY_ENC_IPV6_DST,
  433. &mask->enc_ipv6.dst,
  434. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  435. sizeof(key->enc_ipv6.dst));
  436. }
  437. fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
  438. &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
  439. sizeof(key->enc_key_id.keyid));
  440. return 0;
  441. }
  442. static bool fl_mask_eq(struct fl_flow_mask *mask1,
  443. struct fl_flow_mask *mask2)
  444. {
  445. const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
  446. const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
  447. return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
  448. !memcmp(lmask1, lmask2, fl_mask_range(mask1));
  449. }
  450. static const struct rhashtable_params fl_ht_params = {
  451. .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
  452. .head_offset = offsetof(struct cls_fl_filter, ht_node),
  453. .automatic_shrinking = true,
  454. };
  455. static int fl_init_hashtable(struct cls_fl_head *head,
  456. struct fl_flow_mask *mask)
  457. {
  458. head->ht_params = fl_ht_params;
  459. head->ht_params.key_len = fl_mask_range(mask);
  460. head->ht_params.key_offset += mask->range.start;
  461. return rhashtable_init(&head->ht, &head->ht_params);
  462. }
  463. #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
  464. #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
  465. #define FL_KEY_IS_MASKED(mask, member) \
  466. memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
  467. 0, FL_KEY_MEMBER_SIZE(member)) \
  468. #define FL_KEY_SET(keys, cnt, id, member) \
  469. do { \
  470. keys[cnt].key_id = id; \
  471. keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
  472. cnt++; \
  473. } while(0);
  474. #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
  475. do { \
  476. if (FL_KEY_IS_MASKED(mask, member)) \
  477. FL_KEY_SET(keys, cnt, id, member); \
  478. } while(0);
  479. static void fl_init_dissector(struct cls_fl_head *head,
  480. struct fl_flow_mask *mask)
  481. {
  482. struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
  483. size_t cnt = 0;
  484. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
  485. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
  486. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  487. FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
  488. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  489. FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
  490. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  491. FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
  492. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  493. FLOW_DISSECTOR_KEY_PORTS, tp);
  494. FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
  495. FLOW_DISSECTOR_KEY_VLAN, vlan);
  496. skb_flow_dissector_init(&head->dissector, keys, cnt);
  497. }
  498. static int fl_check_assign_mask(struct cls_fl_head *head,
  499. struct fl_flow_mask *mask)
  500. {
  501. int err;
  502. if (head->mask_assigned) {
  503. if (!fl_mask_eq(&head->mask, mask))
  504. return -EINVAL;
  505. else
  506. return 0;
  507. }
  508. /* Mask is not assigned yet. So assign it and init hashtable
  509. * according to that.
  510. */
  511. err = fl_init_hashtable(head, mask);
  512. if (err)
  513. return err;
  514. memcpy(&head->mask, mask, sizeof(head->mask));
  515. head->mask_assigned = true;
  516. fl_init_dissector(head, mask);
  517. return 0;
  518. }
  519. static int fl_set_parms(struct net *net, struct tcf_proto *tp,
  520. struct cls_fl_filter *f, struct fl_flow_mask *mask,
  521. unsigned long base, struct nlattr **tb,
  522. struct nlattr *est, bool ovr)
  523. {
  524. struct tcf_exts e;
  525. int err;
  526. err = tcf_exts_init(&e, TCA_FLOWER_ACT, 0);
  527. if (err < 0)
  528. return err;
  529. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  530. if (err < 0)
  531. goto errout;
  532. if (tb[TCA_FLOWER_CLASSID]) {
  533. f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
  534. tcf_bind_filter(tp, &f->res, base);
  535. }
  536. err = fl_set_key(net, tb, &f->key, &mask->key);
  537. if (err)
  538. goto errout;
  539. fl_mask_update_range(mask);
  540. fl_set_masked_key(&f->mkey, &f->key, mask);
  541. tcf_exts_change(tp, &f->exts, &e);
  542. return 0;
  543. errout:
  544. tcf_exts_destroy(&e);
  545. return err;
  546. }
  547. static u32 fl_grab_new_handle(struct tcf_proto *tp,
  548. struct cls_fl_head *head)
  549. {
  550. unsigned int i = 0x80000000;
  551. u32 handle;
  552. do {
  553. if (++head->hgen == 0x7FFFFFFF)
  554. head->hgen = 1;
  555. } while (--i > 0 && fl_get(tp, head->hgen));
  556. if (unlikely(i == 0)) {
  557. pr_err("Insufficient number of handles\n");
  558. handle = 0;
  559. } else {
  560. handle = head->hgen;
  561. }
  562. return handle;
  563. }
  564. static int fl_change(struct net *net, struct sk_buff *in_skb,
  565. struct tcf_proto *tp, unsigned long base,
  566. u32 handle, struct nlattr **tca,
  567. unsigned long *arg, bool ovr)
  568. {
  569. struct cls_fl_head *head = rtnl_dereference(tp->root);
  570. struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
  571. struct cls_fl_filter *fnew;
  572. struct nlattr *tb[TCA_FLOWER_MAX + 1];
  573. struct fl_flow_mask mask = {};
  574. int err;
  575. if (!tca[TCA_OPTIONS])
  576. return -EINVAL;
  577. err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], fl_policy);
  578. if (err < 0)
  579. return err;
  580. if (fold && handle && fold->handle != handle)
  581. return -EINVAL;
  582. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  583. if (!fnew)
  584. return -ENOBUFS;
  585. err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
  586. if (err < 0)
  587. goto errout;
  588. if (!handle) {
  589. handle = fl_grab_new_handle(tp, head);
  590. if (!handle) {
  591. err = -EINVAL;
  592. goto errout;
  593. }
  594. }
  595. fnew->handle = handle;
  596. if (tb[TCA_FLOWER_FLAGS]) {
  597. fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
  598. if (!tc_flags_valid(fnew->flags)) {
  599. err = -EINVAL;
  600. goto errout;
  601. }
  602. }
  603. err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
  604. if (err)
  605. goto errout;
  606. err = fl_check_assign_mask(head, &mask);
  607. if (err)
  608. goto errout;
  609. if (!tc_skip_sw(fnew->flags)) {
  610. err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
  611. head->ht_params);
  612. if (err)
  613. goto errout;
  614. }
  615. err = fl_hw_replace_filter(tp,
  616. &head->dissector,
  617. &mask.key,
  618. &fnew->key,
  619. &fnew->exts,
  620. (unsigned long)fnew,
  621. fnew->flags);
  622. if (err)
  623. goto errout;
  624. if (fold) {
  625. if (!tc_skip_sw(fold->flags))
  626. rhashtable_remove_fast(&head->ht, &fold->ht_node,
  627. head->ht_params);
  628. fl_hw_destroy_filter(tp, (unsigned long)fold);
  629. }
  630. *arg = (unsigned long) fnew;
  631. if (fold) {
  632. list_replace_rcu(&fold->list, &fnew->list);
  633. tcf_unbind_filter(tp, &fold->res);
  634. call_rcu(&fold->rcu, fl_destroy_filter);
  635. } else {
  636. list_add_tail_rcu(&fnew->list, &head->filters);
  637. }
  638. return 0;
  639. errout:
  640. tcf_exts_destroy(&fnew->exts);
  641. kfree(fnew);
  642. return err;
  643. }
  644. static int fl_delete(struct tcf_proto *tp, unsigned long arg)
  645. {
  646. struct cls_fl_head *head = rtnl_dereference(tp->root);
  647. struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
  648. if (!tc_skip_sw(f->flags))
  649. rhashtable_remove_fast(&head->ht, &f->ht_node,
  650. head->ht_params);
  651. list_del_rcu(&f->list);
  652. fl_hw_destroy_filter(tp, (unsigned long)f);
  653. tcf_unbind_filter(tp, &f->res);
  654. call_rcu(&f->rcu, fl_destroy_filter);
  655. return 0;
  656. }
  657. static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  658. {
  659. struct cls_fl_head *head = rtnl_dereference(tp->root);
  660. struct cls_fl_filter *f;
  661. list_for_each_entry_rcu(f, &head->filters, list) {
  662. if (arg->count < arg->skip)
  663. goto skip;
  664. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  665. arg->stop = 1;
  666. break;
  667. }
  668. skip:
  669. arg->count++;
  670. }
  671. }
  672. static int fl_dump_key_val(struct sk_buff *skb,
  673. void *val, int val_type,
  674. void *mask, int mask_type, int len)
  675. {
  676. int err;
  677. if (!memchr_inv(mask, 0, len))
  678. return 0;
  679. err = nla_put(skb, val_type, len, val);
  680. if (err)
  681. return err;
  682. if (mask_type != TCA_FLOWER_UNSPEC) {
  683. err = nla_put(skb, mask_type, len, mask);
  684. if (err)
  685. return err;
  686. }
  687. return 0;
  688. }
  689. static int fl_dump_key_vlan(struct sk_buff *skb,
  690. struct flow_dissector_key_vlan *vlan_key,
  691. struct flow_dissector_key_vlan *vlan_mask)
  692. {
  693. int err;
  694. if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
  695. return 0;
  696. if (vlan_mask->vlan_id) {
  697. err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
  698. vlan_key->vlan_id);
  699. if (err)
  700. return err;
  701. }
  702. if (vlan_mask->vlan_priority) {
  703. err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
  704. vlan_key->vlan_priority);
  705. if (err)
  706. return err;
  707. }
  708. return 0;
  709. }
  710. static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  711. struct sk_buff *skb, struct tcmsg *t)
  712. {
  713. struct cls_fl_head *head = rtnl_dereference(tp->root);
  714. struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
  715. struct nlattr *nest;
  716. struct fl_flow_key *key, *mask;
  717. if (!f)
  718. return skb->len;
  719. t->tcm_handle = f->handle;
  720. nest = nla_nest_start(skb, TCA_OPTIONS);
  721. if (!nest)
  722. goto nla_put_failure;
  723. if (f->res.classid &&
  724. nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
  725. goto nla_put_failure;
  726. key = &f->key;
  727. mask = &head->mask.key;
  728. if (mask->indev_ifindex) {
  729. struct net_device *dev;
  730. dev = __dev_get_by_index(net, key->indev_ifindex);
  731. if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
  732. goto nla_put_failure;
  733. }
  734. fl_hw_update_stats(tp, f);
  735. if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  736. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  737. sizeof(key->eth.dst)) ||
  738. fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  739. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  740. sizeof(key->eth.src)) ||
  741. fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
  742. &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
  743. sizeof(key->basic.n_proto)))
  744. goto nla_put_failure;
  745. if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
  746. goto nla_put_failure;
  747. if ((key->basic.n_proto == htons(ETH_P_IP) ||
  748. key->basic.n_proto == htons(ETH_P_IPV6)) &&
  749. fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  750. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  751. sizeof(key->basic.ip_proto)))
  752. goto nla_put_failure;
  753. if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  754. (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  755. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  756. sizeof(key->ipv4.src)) ||
  757. fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  758. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  759. sizeof(key->ipv4.dst))))
  760. goto nla_put_failure;
  761. else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  762. (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  763. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  764. sizeof(key->ipv6.src)) ||
  765. fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  766. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  767. sizeof(key->ipv6.dst))))
  768. goto nla_put_failure;
  769. if (key->basic.ip_proto == IPPROTO_TCP &&
  770. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  771. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  772. sizeof(key->tp.src)) ||
  773. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  774. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  775. sizeof(key->tp.dst))))
  776. goto nla_put_failure;
  777. else if (key->basic.ip_proto == IPPROTO_UDP &&
  778. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  779. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  780. sizeof(key->tp.src)) ||
  781. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  782. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  783. sizeof(key->tp.dst))))
  784. goto nla_put_failure;
  785. if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  786. (fl_dump_key_val(skb, &key->enc_ipv4.src,
  787. TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
  788. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  789. sizeof(key->enc_ipv4.src)) ||
  790. fl_dump_key_val(skb, &key->enc_ipv4.dst,
  791. TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
  792. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  793. sizeof(key->enc_ipv4.dst))))
  794. goto nla_put_failure;
  795. else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  796. (fl_dump_key_val(skb, &key->enc_ipv6.src,
  797. TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
  798. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  799. sizeof(key->enc_ipv6.src)) ||
  800. fl_dump_key_val(skb, &key->enc_ipv6.dst,
  801. TCA_FLOWER_KEY_ENC_IPV6_DST,
  802. &mask->enc_ipv6.dst,
  803. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  804. sizeof(key->enc_ipv6.dst))))
  805. goto nla_put_failure;
  806. if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
  807. &mask->enc_key_id, TCA_FLOWER_UNSPEC,
  808. sizeof(key->enc_key_id)))
  809. goto nla_put_failure;
  810. nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags);
  811. if (tcf_exts_dump(skb, &f->exts))
  812. goto nla_put_failure;
  813. nla_nest_end(skb, nest);
  814. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  815. goto nla_put_failure;
  816. return skb->len;
  817. nla_put_failure:
  818. nla_nest_cancel(skb, nest);
  819. return -1;
  820. }
  821. static struct tcf_proto_ops cls_fl_ops __read_mostly = {
  822. .kind = "flower",
  823. .classify = fl_classify,
  824. .init = fl_init,
  825. .destroy = fl_destroy,
  826. .get = fl_get,
  827. .change = fl_change,
  828. .delete = fl_delete,
  829. .walk = fl_walk,
  830. .dump = fl_dump,
  831. .owner = THIS_MODULE,
  832. };
  833. static int __init cls_fl_init(void)
  834. {
  835. return register_tcf_proto_ops(&cls_fl_ops);
  836. }
  837. static void __exit cls_fl_exit(void)
  838. {
  839. unregister_tcf_proto_ops(&cls_fl_ops);
  840. }
  841. module_init(cls_fl_init);
  842. module_exit(cls_fl_exit);
  843. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  844. MODULE_DESCRIPTION("Flower classifier");
  845. MODULE_LICENSE("GPL v2");