cls_tcindex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
  3. *
  4. * Written 1998,1999 by Werner Almesberger, EPFL ICA
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <net/act_api.h>
  13. #include <net/netlink.h>
  14. #include <net/pkt_cls.h>
  15. #include <net/sch_generic.h>
  16. /*
  17. * Passing parameters to the root seems to be done more awkwardly than really
  18. * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
  19. * verified. FIXME.
  20. */
  21. #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
  22. #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
  23. struct tcindex_filter_result {
  24. struct tcf_exts exts;
  25. struct tcf_result res;
  26. struct rcu_work rwork;
  27. };
  28. struct tcindex_filter {
  29. u16 key;
  30. struct tcindex_filter_result result;
  31. struct tcindex_filter __rcu *next;
  32. struct rcu_work rwork;
  33. };
  34. struct tcindex_data {
  35. struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
  36. struct tcindex_filter __rcu **h; /* imperfect hash; */
  37. struct tcf_proto *tp;
  38. u16 mask; /* AND key with mask */
  39. u32 shift; /* shift ANDed key to the right */
  40. u32 hash; /* hash table size; 0 if undefined */
  41. u32 alloc_hash; /* allocated size */
  42. u32 fall_through; /* 0: only classify if explicit match */
  43. struct rcu_work rwork;
  44. };
  45. static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
  46. {
  47. return tcf_exts_has_actions(&r->exts) || r->res.classid;
  48. }
  49. static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
  50. u16 key)
  51. {
  52. if (p->perfect) {
  53. struct tcindex_filter_result *f = p->perfect + key;
  54. return tcindex_filter_is_set(f) ? f : NULL;
  55. } else if (p->h) {
  56. struct tcindex_filter __rcu **fp;
  57. struct tcindex_filter *f;
  58. fp = &p->h[key % p->hash];
  59. for (f = rcu_dereference_bh_rtnl(*fp);
  60. f;
  61. fp = &f->next, f = rcu_dereference_bh_rtnl(*fp))
  62. if (f->key == key)
  63. return &f->result;
  64. }
  65. return NULL;
  66. }
  67. static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  68. struct tcf_result *res)
  69. {
  70. struct tcindex_data *p = rcu_dereference_bh(tp->root);
  71. struct tcindex_filter_result *f;
  72. int key = (skb->tc_index & p->mask) >> p->shift;
  73. pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
  74. skb, tp, res, p);
  75. f = tcindex_lookup(p, key);
  76. if (!f) {
  77. struct Qdisc *q = tcf_block_q(tp->chain->block);
  78. if (!p->fall_through)
  79. return -1;
  80. res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
  81. res->class = 0;
  82. pr_debug("alg 0x%x\n", res->classid);
  83. return 0;
  84. }
  85. *res = f->res;
  86. pr_debug("map 0x%x\n", res->classid);
  87. return tcf_exts_exec(skb, &f->exts, res);
  88. }
  89. static void *tcindex_get(struct tcf_proto *tp, u32 handle)
  90. {
  91. struct tcindex_data *p = rtnl_dereference(tp->root);
  92. struct tcindex_filter_result *r;
  93. pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
  94. if (p->perfect && handle >= p->alloc_hash)
  95. return NULL;
  96. r = tcindex_lookup(p, handle);
  97. return r && tcindex_filter_is_set(r) ? r : NULL;
  98. }
  99. static int tcindex_init(struct tcf_proto *tp)
  100. {
  101. struct tcindex_data *p;
  102. pr_debug("tcindex_init(tp %p)\n", tp);
  103. p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
  104. if (!p)
  105. return -ENOMEM;
  106. p->mask = 0xffff;
  107. p->hash = DEFAULT_HASH_SIZE;
  108. p->fall_through = 1;
  109. rcu_assign_pointer(tp->root, p);
  110. return 0;
  111. }
  112. static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
  113. {
  114. tcf_exts_destroy(&r->exts);
  115. tcf_exts_put_net(&r->exts);
  116. }
  117. static void tcindex_destroy_rexts_work(struct work_struct *work)
  118. {
  119. struct tcindex_filter_result *r;
  120. r = container_of(to_rcu_work(work),
  121. struct tcindex_filter_result,
  122. rwork);
  123. rtnl_lock();
  124. __tcindex_destroy_rexts(r);
  125. rtnl_unlock();
  126. }
  127. static void __tcindex_destroy_fexts(struct tcindex_filter *f)
  128. {
  129. tcf_exts_destroy(&f->result.exts);
  130. tcf_exts_put_net(&f->result.exts);
  131. kfree(f);
  132. }
  133. static void tcindex_destroy_fexts_work(struct work_struct *work)
  134. {
  135. struct tcindex_filter *f = container_of(to_rcu_work(work),
  136. struct tcindex_filter,
  137. rwork);
  138. rtnl_lock();
  139. __tcindex_destroy_fexts(f);
  140. rtnl_unlock();
  141. }
  142. static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
  143. struct netlink_ext_ack *extack)
  144. {
  145. struct tcindex_data *p = rtnl_dereference(tp->root);
  146. struct tcindex_filter_result *r = arg;
  147. struct tcindex_filter __rcu **walk;
  148. struct tcindex_filter *f = NULL;
  149. pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
  150. if (p->perfect) {
  151. if (!r->res.class)
  152. return -ENOENT;
  153. } else {
  154. int i;
  155. for (i = 0; i < p->hash; i++) {
  156. walk = p->h + i;
  157. for (f = rtnl_dereference(*walk); f;
  158. walk = &f->next, f = rtnl_dereference(*walk)) {
  159. if (&f->result == r)
  160. goto found;
  161. }
  162. }
  163. return -ENOENT;
  164. found:
  165. rcu_assign_pointer(*walk, rtnl_dereference(f->next));
  166. }
  167. tcf_unbind_filter(tp, &r->res);
  168. /* all classifiers are required to call tcf_exts_destroy() after rcu
  169. * grace period, since converted-to-rcu actions are relying on that
  170. * in cleanup() callback
  171. */
  172. if (f) {
  173. if (tcf_exts_get_net(&f->result.exts))
  174. tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
  175. else
  176. __tcindex_destroy_fexts(f);
  177. } else {
  178. if (tcf_exts_get_net(&r->exts))
  179. tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
  180. else
  181. __tcindex_destroy_rexts(r);
  182. }
  183. *last = false;
  184. return 0;
  185. }
  186. static void tcindex_destroy_work(struct work_struct *work)
  187. {
  188. struct tcindex_data *p = container_of(to_rcu_work(work),
  189. struct tcindex_data,
  190. rwork);
  191. kfree(p->perfect);
  192. kfree(p->h);
  193. kfree(p);
  194. }
  195. static inline int
  196. valid_perfect_hash(struct tcindex_data *p)
  197. {
  198. return p->hash > (p->mask >> p->shift);
  199. }
  200. static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = {
  201. [TCA_TCINDEX_HASH] = { .type = NLA_U32 },
  202. [TCA_TCINDEX_MASK] = { .type = NLA_U16 },
  203. [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 },
  204. [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 },
  205. [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 },
  206. };
  207. static int tcindex_filter_result_init(struct tcindex_filter_result *r)
  208. {
  209. memset(r, 0, sizeof(*r));
  210. return tcf_exts_init(&r->exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
  211. }
  212. static void tcindex_partial_destroy_work(struct work_struct *work)
  213. {
  214. struct tcindex_data *p = container_of(to_rcu_work(work),
  215. struct tcindex_data,
  216. rwork);
  217. kfree(p->perfect);
  218. kfree(p);
  219. }
  220. static void tcindex_free_perfect_hash(struct tcindex_data *cp)
  221. {
  222. int i;
  223. for (i = 0; i < cp->hash; i++)
  224. tcf_exts_destroy(&cp->perfect[i].exts);
  225. kfree(cp->perfect);
  226. }
  227. static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
  228. {
  229. int i, err = 0;
  230. cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
  231. GFP_KERNEL);
  232. if (!cp->perfect)
  233. return -ENOMEM;
  234. for (i = 0; i < cp->hash; i++) {
  235. err = tcf_exts_init(&cp->perfect[i].exts,
  236. TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
  237. if (err < 0)
  238. goto errout;
  239. #ifdef CONFIG_NET_CLS_ACT
  240. cp->perfect[i].exts.net = net;
  241. #endif
  242. }
  243. return 0;
  244. errout:
  245. tcindex_free_perfect_hash(cp);
  246. return err;
  247. }
  248. static int
  249. tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
  250. u32 handle, struct tcindex_data *p,
  251. struct tcindex_filter_result *r, struct nlattr **tb,
  252. struct nlattr *est, bool ovr, struct netlink_ext_ack *extack)
  253. {
  254. struct tcindex_filter_result new_filter_result, *old_r = r;
  255. struct tcindex_data *cp = NULL, *oldp;
  256. struct tcindex_filter *f = NULL; /* make gcc behave */
  257. struct tcf_result cr = {};
  258. int err, balloc = 0;
  259. struct tcf_exts e;
  260. err = tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
  261. if (err < 0)
  262. return err;
  263. err = tcf_exts_validate(net, tp, tb, est, &e, ovr, extack);
  264. if (err < 0)
  265. goto errout;
  266. err = -ENOMEM;
  267. /* tcindex_data attributes must look atomic to classifier/lookup so
  268. * allocate new tcindex data and RCU assign it onto root. Keeping
  269. * perfect hash and hash pointers from old data.
  270. */
  271. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  272. if (!cp)
  273. goto errout;
  274. cp->mask = p->mask;
  275. cp->shift = p->shift;
  276. cp->hash = p->hash;
  277. cp->alloc_hash = p->alloc_hash;
  278. cp->fall_through = p->fall_through;
  279. cp->tp = tp;
  280. if (tb[TCA_TCINDEX_HASH])
  281. cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
  282. if (tb[TCA_TCINDEX_MASK])
  283. cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
  284. if (tb[TCA_TCINDEX_SHIFT])
  285. cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
  286. if (!cp->hash) {
  287. /* Hash not specified, use perfect hash if the upper limit
  288. * of the hashing index is below the threshold.
  289. */
  290. if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
  291. cp->hash = (cp->mask >> cp->shift) + 1;
  292. else
  293. cp->hash = DEFAULT_HASH_SIZE;
  294. }
  295. if (p->perfect) {
  296. int i;
  297. if (tcindex_alloc_perfect_hash(net, cp) < 0)
  298. goto errout;
  299. cp->alloc_hash = cp->hash;
  300. for (i = 0; i < min(cp->hash, p->hash); i++)
  301. cp->perfect[i].res = p->perfect[i].res;
  302. balloc = 1;
  303. }
  304. cp->h = p->h;
  305. err = tcindex_filter_result_init(&new_filter_result);
  306. if (err < 0)
  307. goto errout_alloc;
  308. if (old_r)
  309. cr = r->res;
  310. err = -EBUSY;
  311. /* Hash already allocated, make sure that we still meet the
  312. * requirements for the allocated hash.
  313. */
  314. if (cp->perfect) {
  315. if (!valid_perfect_hash(cp) ||
  316. cp->hash > cp->alloc_hash)
  317. goto errout_alloc;
  318. } else if (cp->h && cp->hash != cp->alloc_hash) {
  319. goto errout_alloc;
  320. }
  321. err = -EINVAL;
  322. if (tb[TCA_TCINDEX_FALL_THROUGH])
  323. cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
  324. if (!cp->perfect && !cp->h)
  325. cp->alloc_hash = cp->hash;
  326. /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
  327. * but then, we'd fail handles that may become valid after some future
  328. * mask change. While this is extremely unlikely to ever matter,
  329. * the check below is safer (and also more backwards-compatible).
  330. */
  331. if (cp->perfect || valid_perfect_hash(cp))
  332. if (handle >= cp->alloc_hash)
  333. goto errout_alloc;
  334. err = -ENOMEM;
  335. if (!cp->perfect && !cp->h) {
  336. if (valid_perfect_hash(cp)) {
  337. if (tcindex_alloc_perfect_hash(net, cp) < 0)
  338. goto errout_alloc;
  339. balloc = 1;
  340. } else {
  341. struct tcindex_filter __rcu **hash;
  342. hash = kcalloc(cp->hash,
  343. sizeof(struct tcindex_filter *),
  344. GFP_KERNEL);
  345. if (!hash)
  346. goto errout_alloc;
  347. cp->h = hash;
  348. balloc = 2;
  349. }
  350. }
  351. if (cp->perfect)
  352. r = cp->perfect + handle;
  353. else
  354. r = tcindex_lookup(cp, handle) ? : &new_filter_result;
  355. if (r == &new_filter_result) {
  356. f = kzalloc(sizeof(*f), GFP_KERNEL);
  357. if (!f)
  358. goto errout_alloc;
  359. f->key = handle;
  360. f->next = NULL;
  361. err = tcindex_filter_result_init(&f->result);
  362. if (err < 0) {
  363. kfree(f);
  364. goto errout_alloc;
  365. }
  366. }
  367. if (tb[TCA_TCINDEX_CLASSID]) {
  368. cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
  369. tcf_bind_filter(tp, &cr, base);
  370. }
  371. if (old_r && old_r != r) {
  372. err = tcindex_filter_result_init(old_r);
  373. if (err < 0) {
  374. kfree(f);
  375. goto errout_alloc;
  376. }
  377. }
  378. oldp = p;
  379. r->res = cr;
  380. tcf_exts_change(&r->exts, &e);
  381. rcu_assign_pointer(tp->root, cp);
  382. if (r == &new_filter_result) {
  383. struct tcindex_filter *nfp;
  384. struct tcindex_filter __rcu **fp;
  385. f->result.res = r->res;
  386. tcf_exts_change(&f->result.exts, &r->exts);
  387. fp = cp->h + (handle % cp->hash);
  388. for (nfp = rtnl_dereference(*fp);
  389. nfp;
  390. fp = &nfp->next, nfp = rtnl_dereference(*fp))
  391. ; /* nothing */
  392. rcu_assign_pointer(*fp, f);
  393. } else {
  394. tcf_exts_destroy(&new_filter_result.exts);
  395. }
  396. if (oldp)
  397. tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
  398. return 0;
  399. errout_alloc:
  400. if (balloc == 1)
  401. tcindex_free_perfect_hash(cp);
  402. else if (balloc == 2)
  403. kfree(cp->h);
  404. tcf_exts_destroy(&new_filter_result.exts);
  405. errout:
  406. kfree(cp);
  407. tcf_exts_destroy(&e);
  408. return err;
  409. }
  410. static int
  411. tcindex_change(struct net *net, struct sk_buff *in_skb,
  412. struct tcf_proto *tp, unsigned long base, u32 handle,
  413. struct nlattr **tca, void **arg, bool ovr,
  414. struct netlink_ext_ack *extack)
  415. {
  416. struct nlattr *opt = tca[TCA_OPTIONS];
  417. struct nlattr *tb[TCA_TCINDEX_MAX + 1];
  418. struct tcindex_data *p = rtnl_dereference(tp->root);
  419. struct tcindex_filter_result *r = *arg;
  420. int err;
  421. pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
  422. "p %p,r %p,*arg %p\n",
  423. tp, handle, tca, arg, opt, p, r, arg ? *arg : NULL);
  424. if (!opt)
  425. return 0;
  426. err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy, NULL);
  427. if (err < 0)
  428. return err;
  429. return tcindex_set_parms(net, tp, base, handle, p, r, tb,
  430. tca[TCA_RATE], ovr, extack);
  431. }
  432. static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
  433. {
  434. struct tcindex_data *p = rtnl_dereference(tp->root);
  435. struct tcindex_filter *f, *next;
  436. int i;
  437. pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
  438. if (p->perfect) {
  439. for (i = 0; i < p->hash; i++) {
  440. if (!p->perfect[i].res.class)
  441. continue;
  442. if (walker->count >= walker->skip) {
  443. if (walker->fn(tp, p->perfect + i, walker) < 0) {
  444. walker->stop = 1;
  445. return;
  446. }
  447. }
  448. walker->count++;
  449. }
  450. }
  451. if (!p->h)
  452. return;
  453. for (i = 0; i < p->hash; i++) {
  454. for (f = rtnl_dereference(p->h[i]); f; f = next) {
  455. next = rtnl_dereference(f->next);
  456. if (walker->count >= walker->skip) {
  457. if (walker->fn(tp, &f->result, walker) < 0) {
  458. walker->stop = 1;
  459. return;
  460. }
  461. }
  462. walker->count++;
  463. }
  464. }
  465. }
  466. static void tcindex_destroy(struct tcf_proto *tp,
  467. struct netlink_ext_ack *extack)
  468. {
  469. struct tcindex_data *p = rtnl_dereference(tp->root);
  470. int i;
  471. pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
  472. if (p->perfect) {
  473. for (i = 0; i < p->hash; i++) {
  474. struct tcindex_filter_result *r = p->perfect + i;
  475. tcf_unbind_filter(tp, &r->res);
  476. if (tcf_exts_get_net(&r->exts))
  477. tcf_queue_work(&r->rwork,
  478. tcindex_destroy_rexts_work);
  479. else
  480. __tcindex_destroy_rexts(r);
  481. }
  482. }
  483. for (i = 0; p->h && i < p->hash; i++) {
  484. struct tcindex_filter *f, *next;
  485. bool last;
  486. for (f = rtnl_dereference(p->h[i]); f; f = next) {
  487. next = rtnl_dereference(f->next);
  488. tcindex_delete(tp, &f->result, &last, NULL);
  489. }
  490. }
  491. tcf_queue_work(&p->rwork, tcindex_destroy_work);
  492. }
  493. static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
  494. struct sk_buff *skb, struct tcmsg *t)
  495. {
  496. struct tcindex_data *p = rtnl_dereference(tp->root);
  497. struct tcindex_filter_result *r = fh;
  498. struct nlattr *nest;
  499. pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
  500. tp, fh, skb, t, p, r);
  501. pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
  502. nest = nla_nest_start(skb, TCA_OPTIONS);
  503. if (nest == NULL)
  504. goto nla_put_failure;
  505. if (!fh) {
  506. t->tcm_handle = ~0; /* whatever ... */
  507. if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) ||
  508. nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) ||
  509. nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) ||
  510. nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through))
  511. goto nla_put_failure;
  512. nla_nest_end(skb, nest);
  513. } else {
  514. if (p->perfect) {
  515. t->tcm_handle = r - p->perfect;
  516. } else {
  517. struct tcindex_filter *f;
  518. struct tcindex_filter __rcu **fp;
  519. int i;
  520. t->tcm_handle = 0;
  521. for (i = 0; !t->tcm_handle && i < p->hash; i++) {
  522. fp = &p->h[i];
  523. for (f = rtnl_dereference(*fp);
  524. !t->tcm_handle && f;
  525. fp = &f->next, f = rtnl_dereference(*fp)) {
  526. if (&f->result == r)
  527. t->tcm_handle = f->key;
  528. }
  529. }
  530. }
  531. pr_debug("handle = %d\n", t->tcm_handle);
  532. if (r->res.class &&
  533. nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
  534. goto nla_put_failure;
  535. if (tcf_exts_dump(skb, &r->exts) < 0)
  536. goto nla_put_failure;
  537. nla_nest_end(skb, nest);
  538. if (tcf_exts_dump_stats(skb, &r->exts) < 0)
  539. goto nla_put_failure;
  540. }
  541. return skb->len;
  542. nla_put_failure:
  543. nla_nest_cancel(skb, nest);
  544. return -1;
  545. }
  546. static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
  547. void *q, unsigned long base)
  548. {
  549. struct tcindex_filter_result *r = fh;
  550. if (r && r->res.classid == classid) {
  551. if (cl)
  552. __tcf_bind_filter(q, &r->res, base);
  553. else
  554. __tcf_unbind_filter(q, &r->res);
  555. }
  556. }
  557. static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
  558. .kind = "tcindex",
  559. .classify = tcindex_classify,
  560. .init = tcindex_init,
  561. .destroy = tcindex_destroy,
  562. .get = tcindex_get,
  563. .change = tcindex_change,
  564. .delete = tcindex_delete,
  565. .walk = tcindex_walk,
  566. .dump = tcindex_dump,
  567. .bind_class = tcindex_bind_class,
  568. .owner = THIS_MODULE,
  569. };
  570. static int __init init_tcindex(void)
  571. {
  572. return register_tcf_proto_ops(&cls_tcindex_ops);
  573. }
  574. static void __exit exit_tcindex(void)
  575. {
  576. unregister_tcf_proto_ops(&cls_tcindex_ops);
  577. }
  578. module_init(init_tcindex)
  579. module_exit(exit_tcindex)
  580. MODULE_LICENSE("GPL");