cls_flow.c 16 KB

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