cls_flow.c 16 KB

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