cls_flow.c 16 KB

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