cls_flow.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_flow.c Generic flow classifier
  4. *
  5. * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/list.h>
  10. #include <linux/jhash.h>
  11. #include <linux/random.h>
  12. #include <linux/pkt_cls.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/in.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <net/inet_sock.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/ip.h>
  23. #include <net/route.h>
  24. #include <net/flow_dissector.h>
  25. #include <net/tc_wrapper.h>
  26. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  27. #include <net/netfilter/nf_conntrack.h>
  28. #endif
  29. struct flow_head {
  30. struct list_head filters;
  31. struct rcu_head rcu;
  32. };
  33. struct flow_filter {
  34. struct list_head list;
  35. struct tcf_exts exts;
  36. struct tcf_ematch_tree ematches;
  37. struct tcf_proto *tp;
  38. struct timer_list perturb_timer;
  39. u32 perturb_period;
  40. u32 handle;
  41. u32 nkeys;
  42. u32 keymask;
  43. u32 mode;
  44. u32 mask;
  45. u32 xor;
  46. u32 rshift;
  47. u32 addend;
  48. u32 divisor;
  49. u32 baseclass;
  50. u32 hashrnd;
  51. struct rcu_work rwork;
  52. };
  53. static inline u32 addr_fold(void *addr)
  54. {
  55. unsigned long a = (unsigned long)addr;
  56. return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
  57. }
  58. static u32 flow_get_src(const struct sk_buff *skb, const struct flow_keys *flow)
  59. {
  60. __be32 src = flow_get_u32_src(flow);
  61. if (src)
  62. return ntohl(src);
  63. return addr_fold(skb->sk);
  64. }
  65. static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow)
  66. {
  67. __be32 dst = flow_get_u32_dst(flow);
  68. if (dst)
  69. return ntohl(dst);
  70. return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
  71. }
  72. static u32 flow_get_proto(const struct sk_buff *skb,
  73. const struct flow_keys *flow)
  74. {
  75. return flow->basic.ip_proto;
  76. }
  77. static u32 flow_get_proto_src(const struct sk_buff *skb,
  78. const struct flow_keys *flow)
  79. {
  80. if (flow->ports.ports)
  81. return ntohs(flow->ports.src);
  82. return addr_fold(skb->sk);
  83. }
  84. static u32 flow_get_proto_dst(const struct sk_buff *skb,
  85. const struct flow_keys *flow)
  86. {
  87. if (flow->ports.ports)
  88. return ntohs(flow->ports.dst);
  89. return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
  90. }
  91. static u32 flow_get_iif(const struct sk_buff *skb)
  92. {
  93. return skb->skb_iif;
  94. }
  95. static u32 flow_get_priority(const struct sk_buff *skb)
  96. {
  97. return skb->priority;
  98. }
  99. static u32 flow_get_mark(const struct sk_buff *skb)
  100. {
  101. return skb->mark;
  102. }
  103. static u32 flow_get_nfct(const struct sk_buff *skb)
  104. {
  105. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  106. return addr_fold(skb_nfct(skb));
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  112. #define CTTUPLE(skb, member) \
  113. ({ \
  114. enum ip_conntrack_info ctinfo; \
  115. const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
  116. if (ct == NULL) \
  117. goto fallback; \
  118. ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
  119. })
  120. #else
  121. #define CTTUPLE(skb, member) \
  122. ({ \
  123. goto fallback; \
  124. 0; \
  125. })
  126. #endif
  127. static u32 flow_get_nfct_src(const struct sk_buff *skb,
  128. const struct flow_keys *flow)
  129. {
  130. switch (skb_protocol(skb, true)) {
  131. case htons(ETH_P_IP):
  132. return ntohl(CTTUPLE(skb, src.u3.ip));
  133. case htons(ETH_P_IPV6):
  134. return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
  135. }
  136. fallback:
  137. return flow_get_src(skb, flow);
  138. }
  139. static u32 flow_get_nfct_dst(const struct sk_buff *skb,
  140. const struct flow_keys *flow)
  141. {
  142. switch (skb_protocol(skb, true)) {
  143. case htons(ETH_P_IP):
  144. return ntohl(CTTUPLE(skb, dst.u3.ip));
  145. case htons(ETH_P_IPV6):
  146. return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
  147. }
  148. fallback:
  149. return flow_get_dst(skb, flow);
  150. }
  151. static u32 flow_get_nfct_proto_src(const struct sk_buff *skb,
  152. const struct flow_keys *flow)
  153. {
  154. return ntohs(CTTUPLE(skb, src.u.all));
  155. fallback:
  156. return flow_get_proto_src(skb, flow);
  157. }
  158. static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb,
  159. const struct flow_keys *flow)
  160. {
  161. return ntohs(CTTUPLE(skb, dst.u.all));
  162. fallback:
  163. return flow_get_proto_dst(skb, flow);
  164. }
  165. static u32 flow_get_rtclassid(const struct sk_buff *skb)
  166. {
  167. #ifdef CONFIG_IP_ROUTE_CLASSID
  168. if (skb_dst(skb))
  169. return skb_dst(skb)->tclassid;
  170. #endif
  171. return 0;
  172. }
  173. static u32 flow_get_skuid(const struct sk_buff *skb)
  174. {
  175. struct sock *sk = skb_to_full_sk(skb);
  176. if (sk && sk->sk_socket && sk->sk_socket->file) {
  177. kuid_t skuid = sk->sk_socket->file->f_cred->fsuid;
  178. return from_kuid(&init_user_ns, skuid);
  179. }
  180. return 0;
  181. }
  182. static u32 flow_get_skgid(const struct sk_buff *skb)
  183. {
  184. struct sock *sk = skb_to_full_sk(skb);
  185. if (sk && sk->sk_socket && sk->sk_socket->file) {
  186. kgid_t skgid = sk->sk_socket->file->f_cred->fsgid;
  187. return from_kgid(&init_user_ns, skgid);
  188. }
  189. return 0;
  190. }
  191. static u32 flow_get_vlan_tag(const struct sk_buff *skb)
  192. {
  193. u16 tag;
  194. if (vlan_get_tag(skb, &tag) < 0)
  195. return 0;
  196. return tag & VLAN_VID_MASK;
  197. }
  198. static u32 flow_get_rxhash(struct sk_buff *skb)
  199. {
  200. return skb_get_hash(skb);
  201. }
  202. static u32 flow_key_get(struct sk_buff *skb, int key, struct flow_keys *flow)
  203. {
  204. switch (key) {
  205. case FLOW_KEY_SRC:
  206. return flow_get_src(skb, flow);
  207. case FLOW_KEY_DST:
  208. return flow_get_dst(skb, flow);
  209. case FLOW_KEY_PROTO:
  210. return flow_get_proto(skb, flow);
  211. case FLOW_KEY_PROTO_SRC:
  212. return flow_get_proto_src(skb, flow);
  213. case FLOW_KEY_PROTO_DST:
  214. return flow_get_proto_dst(skb, flow);
  215. case FLOW_KEY_IIF:
  216. return flow_get_iif(skb);
  217. case FLOW_KEY_PRIORITY:
  218. return flow_get_priority(skb);
  219. case FLOW_KEY_MARK:
  220. return flow_get_mark(skb);
  221. case FLOW_KEY_NFCT:
  222. return flow_get_nfct(skb);
  223. case FLOW_KEY_NFCT_SRC:
  224. return flow_get_nfct_src(skb, flow);
  225. case FLOW_KEY_NFCT_DST:
  226. return flow_get_nfct_dst(skb, flow);
  227. case FLOW_KEY_NFCT_PROTO_SRC:
  228. return flow_get_nfct_proto_src(skb, flow);
  229. case FLOW_KEY_NFCT_PROTO_DST:
  230. return flow_get_nfct_proto_dst(skb, flow);
  231. case FLOW_KEY_RTCLASSID:
  232. return flow_get_rtclassid(skb);
  233. case FLOW_KEY_SKUID:
  234. return flow_get_skuid(skb);
  235. case FLOW_KEY_SKGID:
  236. return flow_get_skgid(skb);
  237. case FLOW_KEY_VLAN_TAG:
  238. return flow_get_vlan_tag(skb);
  239. case FLOW_KEY_RXHASH:
  240. return flow_get_rxhash(skb);
  241. default:
  242. WARN_ON(1);
  243. return 0;
  244. }
  245. }
  246. #define FLOW_KEYS_NEEDED ((1 << FLOW_KEY_SRC) | \
  247. (1 << FLOW_KEY_DST) | \
  248. (1 << FLOW_KEY_PROTO) | \
  249. (1 << FLOW_KEY_PROTO_SRC) | \
  250. (1 << FLOW_KEY_PROTO_DST) | \
  251. (1 << FLOW_KEY_NFCT_SRC) | \
  252. (1 << FLOW_KEY_NFCT_DST) | \
  253. (1 << FLOW_KEY_NFCT_PROTO_SRC) | \
  254. (1 << FLOW_KEY_NFCT_PROTO_DST))
  255. TC_INDIRECT_SCOPE int flow_classify(struct sk_buff *skb,
  256. const struct tcf_proto *tp,
  257. struct tcf_result *res)
  258. {
  259. struct flow_head *head = rcu_dereference_bh(tp->root);
  260. struct flow_filter *f;
  261. u32 keymask;
  262. u32 classid;
  263. unsigned int n, key;
  264. int r;
  265. list_for_each_entry_rcu(f, &head->filters, list) {
  266. u32 keys[FLOW_KEY_MAX + 1];
  267. struct flow_keys flow_keys;
  268. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  269. continue;
  270. keymask = f->keymask;
  271. if (keymask & FLOW_KEYS_NEEDED)
  272. skb_flow_dissect_flow_keys(skb, &flow_keys, 0);
  273. for (n = 0; n < f->nkeys; n++) {
  274. key = ffs(keymask) - 1;
  275. keymask &= ~(1 << key);
  276. keys[n] = flow_key_get(skb, key, &flow_keys);
  277. }
  278. if (f->mode == FLOW_MODE_HASH)
  279. classid = jhash2(keys, f->nkeys, f->hashrnd);
  280. else {
  281. classid = keys[0];
  282. classid = (classid & f->mask) ^ f->xor;
  283. classid = (classid >> f->rshift) + f->addend;
  284. }
  285. if (f->divisor)
  286. classid %= f->divisor;
  287. res->class = 0;
  288. res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
  289. r = tcf_exts_exec(skb, &f->exts, res);
  290. if (r < 0)
  291. continue;
  292. return r;
  293. }
  294. return -1;
  295. }
  296. static void flow_perturbation(struct timer_list *t)
  297. {
  298. struct flow_filter *f = from_timer(f, t, perturb_timer);
  299. get_random_bytes(&f->hashrnd, 4);
  300. if (f->perturb_period)
  301. mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
  302. }
  303. static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
  304. [TCA_FLOW_KEYS] = { .type = NLA_U32 },
  305. [TCA_FLOW_MODE] = { .type = NLA_U32 },
  306. [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
  307. [TCA_FLOW_RSHIFT] = NLA_POLICY_MAX(NLA_U32,
  308. 31 /* BITS_PER_U32 - 1 */),
  309. [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
  310. [TCA_FLOW_MASK] = { .type = NLA_U32 },
  311. [TCA_FLOW_XOR] = { .type = NLA_U32 },
  312. [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
  313. [TCA_FLOW_ACT] = { .type = NLA_NESTED },
  314. [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
  315. [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
  316. [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
  317. };
  318. static void __flow_destroy_filter(struct flow_filter *f)
  319. {
  320. timer_shutdown_sync(&f->perturb_timer);
  321. tcf_exts_destroy(&f->exts);
  322. tcf_em_tree_destroy(&f->ematches);
  323. tcf_exts_put_net(&f->exts);
  324. kfree(f);
  325. }
  326. static void flow_destroy_filter_work(struct work_struct *work)
  327. {
  328. struct flow_filter *f = container_of(to_rcu_work(work),
  329. struct flow_filter,
  330. rwork);
  331. rtnl_lock();
  332. __flow_destroy_filter(f);
  333. rtnl_unlock();
  334. }
  335. static int flow_change(struct net *net, struct sk_buff *in_skb,
  336. struct tcf_proto *tp, unsigned long base,
  337. u32 handle, struct nlattr **tca,
  338. void **arg, u32 flags,
  339. struct netlink_ext_ack *extack)
  340. {
  341. struct flow_head *head = rtnl_dereference(tp->root);
  342. struct flow_filter *fold, *fnew;
  343. struct nlattr *opt = tca[TCA_OPTIONS];
  344. struct nlattr *tb[TCA_FLOW_MAX + 1];
  345. unsigned int nkeys = 0;
  346. unsigned int perturb_period = 0;
  347. u32 baseclass = 0;
  348. u32 keymask = 0;
  349. u32 mode;
  350. int err;
  351. if (opt == NULL)
  352. return -EINVAL;
  353. err = nla_parse_nested_deprecated(tb, TCA_FLOW_MAX, opt, flow_policy,
  354. NULL);
  355. if (err < 0)
  356. return err;
  357. if (tb[TCA_FLOW_BASECLASS]) {
  358. baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
  359. if (TC_H_MIN(baseclass) == 0)
  360. return -EINVAL;
  361. }
  362. if (tb[TCA_FLOW_KEYS]) {
  363. keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
  364. nkeys = hweight32(keymask);
  365. if (nkeys == 0)
  366. return -EINVAL;
  367. if (fls(keymask) - 1 > FLOW_KEY_MAX)
  368. return -EOPNOTSUPP;
  369. if ((keymask & (FLOW_KEY_SKUID|FLOW_KEY_SKGID)) &&
  370. sk_user_ns(NETLINK_CB(in_skb).sk) != &init_user_ns)
  371. return -EOPNOTSUPP;
  372. }
  373. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  374. if (!fnew)
  375. return -ENOBUFS;
  376. err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &fnew->ematches);
  377. if (err < 0)
  378. goto err1;
  379. err = tcf_exts_init(&fnew->exts, net, TCA_FLOW_ACT, TCA_FLOW_POLICE);
  380. if (err < 0)
  381. goto err2;
  382. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &fnew->exts, flags,
  383. extack);
  384. if (err < 0)
  385. goto err2;
  386. fold = *arg;
  387. if (fold) {
  388. err = -EINVAL;
  389. if (fold->handle != handle && handle)
  390. goto err2;
  391. /* Copy fold into fnew */
  392. fnew->tp = fold->tp;
  393. fnew->handle = fold->handle;
  394. fnew->nkeys = fold->nkeys;
  395. fnew->keymask = fold->keymask;
  396. fnew->mode = fold->mode;
  397. fnew->mask = fold->mask;
  398. fnew->xor = fold->xor;
  399. fnew->rshift = fold->rshift;
  400. fnew->addend = fold->addend;
  401. fnew->divisor = fold->divisor;
  402. fnew->baseclass = fold->baseclass;
  403. fnew->hashrnd = fold->hashrnd;
  404. mode = fold->mode;
  405. if (tb[TCA_FLOW_MODE])
  406. mode = nla_get_u32(tb[TCA_FLOW_MODE]);
  407. if (mode != FLOW_MODE_HASH && nkeys > 1)
  408. goto err2;
  409. if (mode == FLOW_MODE_HASH)
  410. perturb_period = fold->perturb_period;
  411. if (tb[TCA_FLOW_PERTURB]) {
  412. if (mode != FLOW_MODE_HASH)
  413. goto err2;
  414. perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
  415. }
  416. } else {
  417. err = -EINVAL;
  418. if (!handle)
  419. goto err2;
  420. if (!tb[TCA_FLOW_KEYS])
  421. goto err2;
  422. mode = FLOW_MODE_MAP;
  423. if (tb[TCA_FLOW_MODE])
  424. mode = nla_get_u32(tb[TCA_FLOW_MODE]);
  425. if (mode != FLOW_MODE_HASH && nkeys > 1)
  426. goto err2;
  427. if (tb[TCA_FLOW_PERTURB]) {
  428. if (mode != FLOW_MODE_HASH)
  429. goto err2;
  430. perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
  431. }
  432. if (TC_H_MAJ(baseclass) == 0) {
  433. struct Qdisc *q = tcf_block_q(tp->chain->block);
  434. baseclass = TC_H_MAKE(q->handle, baseclass);
  435. }
  436. if (TC_H_MIN(baseclass) == 0)
  437. baseclass = TC_H_MAKE(baseclass, 1);
  438. fnew->handle = handle;
  439. fnew->mask = ~0U;
  440. fnew->tp = tp;
  441. get_random_bytes(&fnew->hashrnd, 4);
  442. }
  443. timer_setup(&fnew->perturb_timer, flow_perturbation, TIMER_DEFERRABLE);
  444. tcf_block_netif_keep_dst(tp->chain->block);
  445. if (tb[TCA_FLOW_KEYS]) {
  446. fnew->keymask = keymask;
  447. fnew->nkeys = nkeys;
  448. }
  449. fnew->mode = mode;
  450. if (tb[TCA_FLOW_MASK])
  451. fnew->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
  452. if (tb[TCA_FLOW_XOR])
  453. fnew->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
  454. if (tb[TCA_FLOW_RSHIFT])
  455. fnew->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
  456. if (tb[TCA_FLOW_ADDEND])
  457. fnew->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
  458. if (tb[TCA_FLOW_DIVISOR])
  459. fnew->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
  460. if (baseclass)
  461. fnew->baseclass = baseclass;
  462. fnew->perturb_period = perturb_period;
  463. if (perturb_period)
  464. mod_timer(&fnew->perturb_timer, jiffies + perturb_period);
  465. if (!*arg)
  466. list_add_tail_rcu(&fnew->list, &head->filters);
  467. else
  468. list_replace_rcu(&fold->list, &fnew->list);
  469. *arg = fnew;
  470. if (fold) {
  471. tcf_exts_get_net(&fold->exts);
  472. tcf_queue_work(&fold->rwork, flow_destroy_filter_work);
  473. }
  474. return 0;
  475. err2:
  476. tcf_exts_destroy(&fnew->exts);
  477. tcf_em_tree_destroy(&fnew->ematches);
  478. err1:
  479. kfree(fnew);
  480. return err;
  481. }
  482. static int flow_delete(struct tcf_proto *tp, void *arg, bool *last,
  483. bool rtnl_held, struct netlink_ext_ack *extack)
  484. {
  485. struct flow_head *head = rtnl_dereference(tp->root);
  486. struct flow_filter *f = arg;
  487. list_del_rcu(&f->list);
  488. tcf_exts_get_net(&f->exts);
  489. tcf_queue_work(&f->rwork, flow_destroy_filter_work);
  490. *last = list_empty(&head->filters);
  491. return 0;
  492. }
  493. static int flow_init(struct tcf_proto *tp)
  494. {
  495. struct flow_head *head;
  496. head = kzalloc(sizeof(*head), GFP_KERNEL);
  497. if (head == NULL)
  498. return -ENOBUFS;
  499. INIT_LIST_HEAD(&head->filters);
  500. rcu_assign_pointer(tp->root, head);
  501. return 0;
  502. }
  503. static void flow_destroy(struct tcf_proto *tp, bool rtnl_held,
  504. struct netlink_ext_ack *extack)
  505. {
  506. struct flow_head *head = rtnl_dereference(tp->root);
  507. struct flow_filter *f, *next;
  508. list_for_each_entry_safe(f, next, &head->filters, list) {
  509. list_del_rcu(&f->list);
  510. if (tcf_exts_get_net(&f->exts))
  511. tcf_queue_work(&f->rwork, flow_destroy_filter_work);
  512. else
  513. __flow_destroy_filter(f);
  514. }
  515. kfree_rcu(head, rcu);
  516. }
  517. static void *flow_get(struct tcf_proto *tp, u32 handle)
  518. {
  519. struct flow_head *head = rtnl_dereference(tp->root);
  520. struct flow_filter *f;
  521. list_for_each_entry(f, &head->filters, list)
  522. if (f->handle == handle)
  523. return f;
  524. return NULL;
  525. }
  526. static int flow_dump(struct net *net, struct tcf_proto *tp, void *fh,
  527. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  528. {
  529. struct flow_filter *f = fh;
  530. struct nlattr *nest;
  531. if (f == NULL)
  532. return skb->len;
  533. t->tcm_handle = f->handle;
  534. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  535. if (nest == NULL)
  536. goto nla_put_failure;
  537. if (nla_put_u32(skb, TCA_FLOW_KEYS, f->keymask) ||
  538. nla_put_u32(skb, TCA_FLOW_MODE, f->mode))
  539. goto nla_put_failure;
  540. if (f->mask != ~0 || f->xor != 0) {
  541. if (nla_put_u32(skb, TCA_FLOW_MASK, f->mask) ||
  542. nla_put_u32(skb, TCA_FLOW_XOR, f->xor))
  543. goto nla_put_failure;
  544. }
  545. if (f->rshift &&
  546. nla_put_u32(skb, TCA_FLOW_RSHIFT, f->rshift))
  547. goto nla_put_failure;
  548. if (f->addend &&
  549. nla_put_u32(skb, TCA_FLOW_ADDEND, f->addend))
  550. goto nla_put_failure;
  551. if (f->divisor &&
  552. nla_put_u32(skb, TCA_FLOW_DIVISOR, f->divisor))
  553. goto nla_put_failure;
  554. if (f->baseclass &&
  555. nla_put_u32(skb, TCA_FLOW_BASECLASS, f->baseclass))
  556. goto nla_put_failure;
  557. if (f->perturb_period &&
  558. nla_put_u32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ))
  559. goto nla_put_failure;
  560. if (tcf_exts_dump(skb, &f->exts) < 0)
  561. goto nla_put_failure;
  562. #ifdef CONFIG_NET_EMATCH
  563. if (f->ematches.hdr.nmatches &&
  564. tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
  565. goto nla_put_failure;
  566. #endif
  567. nla_nest_end(skb, nest);
  568. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  569. goto nla_put_failure;
  570. return skb->len;
  571. nla_put_failure:
  572. nla_nest_cancel(skb, nest);
  573. return -1;
  574. }
  575. static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  576. bool rtnl_held)
  577. {
  578. struct flow_head *head = rtnl_dereference(tp->root);
  579. struct flow_filter *f;
  580. list_for_each_entry(f, &head->filters, list) {
  581. if (!tc_cls_stats_dump(tp, arg, f))
  582. break;
  583. }
  584. }
  585. static struct tcf_proto_ops cls_flow_ops __read_mostly = {
  586. .kind = "flow",
  587. .classify = flow_classify,
  588. .init = flow_init,
  589. .destroy = flow_destroy,
  590. .change = flow_change,
  591. .delete = flow_delete,
  592. .get = flow_get,
  593. .dump = flow_dump,
  594. .walk = flow_walk,
  595. .owner = THIS_MODULE,
  596. };
  597. MODULE_ALIAS_NET_CLS("flow");
  598. static int __init cls_flow_init(void)
  599. {
  600. return register_tcf_proto_ops(&cls_flow_ops);
  601. }
  602. static void __exit cls_flow_exit(void)
  603. {
  604. unregister_tcf_proto_ops(&cls_flow_ops);
  605. }
  606. module_init(cls_flow_init);
  607. module_exit(cls_flow_exit);
  608. MODULE_LICENSE("GPL");
  609. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  610. MODULE_DESCRIPTION("TC flow classifier");