cls_flow.c 15 KB

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