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