cls_flow.c 16 KB

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