cls_flow.c 17 KB

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