cls_flow.c 15 KB

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