cls_flow.c 15 KB

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