cls_flow.c 17 KB

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