cls_flow.c 16 KB

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