cls_flow.c 16 KB

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