cls_flow.c 16 KB

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