cls_flow.c 16 KB

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