em_meta.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * net/sched/em_meta.c Metadata ematch
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. *
  11. * ==========================================================================
  12. *
  13. * The metadata ematch compares two meta objects where each object
  14. * represents either a meta value stored in the kernel or a static
  15. * value provided by userspace. The objects are not provided by
  16. * userspace itself but rather a definition providing the information
  17. * to build them. Every object is of a certain type which must be
  18. * equal to the object it is being compared to.
  19. *
  20. * The definition of a objects conists of the type (meta type), a
  21. * identifier (meta id) and additional type specific information.
  22. * The meta id is either TCF_META_TYPE_VALUE for values provided by
  23. * userspace or a index to the meta operations table consisting of
  24. * function pointers to type specific meta data collectors returning
  25. * the value of the requested meta value.
  26. *
  27. * lvalue rvalue
  28. * +-----------+ +-----------+
  29. * | type: INT | | type: INT |
  30. * def | id: INDEV | | id: VALUE |
  31. * | data: | | data: 3 |
  32. * +-----------+ +-----------+
  33. * | |
  34. * ---> meta_ops[INT][INDEV](...) |
  35. * | |
  36. * ----------- |
  37. * V V
  38. * +-----------+ +-----------+
  39. * | type: INT | | type: INT |
  40. * obj | id: INDEV | | id: VALUE |
  41. * | data: 2 |<--data got filled out | data: 3 |
  42. * +-----------+ +-----------+
  43. * | |
  44. * --------------> 2 equals 3 <--------------
  45. *
  46. * This is a simplified schema, the complexity varies depending
  47. * on the meta type. Obviously, the length of the data must also
  48. * be provided for non-numeric types.
  49. *
  50. * Additionaly, type dependant modifiers such as shift operators
  51. * or mask may be applied to extend the functionaliy. As of now,
  52. * the variable length type supports shifting the byte string to
  53. * the right, eating up any number of octets and thus supporting
  54. * wildcard interface name comparisons such as "ppp%" matching
  55. * ppp0..9.
  56. *
  57. * NOTE: Certain meta values depend on other subsystems and are
  58. * only available if that subsytem is enabled in the kernel.
  59. */
  60. #include <linux/config.h>
  61. #include <linux/module.h>
  62. #include <linux/types.h>
  63. #include <linux/kernel.h>
  64. #include <linux/sched.h>
  65. #include <linux/string.h>
  66. #include <linux/skbuff.h>
  67. #include <linux/random.h>
  68. #include <linux/tc_ematch/tc_em_meta.h>
  69. #include <net/dst.h>
  70. #include <net/route.h>
  71. #include <net/pkt_cls.h>
  72. struct meta_obj
  73. {
  74. unsigned long value;
  75. unsigned int len;
  76. };
  77. struct meta_value
  78. {
  79. struct tcf_meta_val hdr;
  80. unsigned long val;
  81. unsigned int len;
  82. };
  83. struct meta_match
  84. {
  85. struct meta_value lvalue;
  86. struct meta_value rvalue;
  87. };
  88. static inline int meta_id(struct meta_value *v)
  89. {
  90. return TCF_META_ID(v->hdr.kind);
  91. }
  92. static inline int meta_type(struct meta_value *v)
  93. {
  94. return TCF_META_TYPE(v->hdr.kind);
  95. }
  96. #define META_COLLECTOR(FUNC) static void meta_##FUNC(struct sk_buff *skb, \
  97. struct tcf_pkt_info *info, struct meta_value *v, \
  98. struct meta_obj *dst, int *err)
  99. /**************************************************************************
  100. * System status & misc
  101. **************************************************************************/
  102. META_COLLECTOR(int_random)
  103. {
  104. get_random_bytes(&dst->value, sizeof(dst->value));
  105. }
  106. static inline unsigned long fixed_loadavg(int load)
  107. {
  108. int rnd_load = load + (FIXED_1/200);
  109. int rnd_frac = ((rnd_load & (FIXED_1-1)) * 100) >> FSHIFT;
  110. return ((rnd_load >> FSHIFT) * 100) + rnd_frac;
  111. }
  112. META_COLLECTOR(int_loadavg_0)
  113. {
  114. dst->value = fixed_loadavg(avenrun[0]);
  115. }
  116. META_COLLECTOR(int_loadavg_1)
  117. {
  118. dst->value = fixed_loadavg(avenrun[1]);
  119. }
  120. META_COLLECTOR(int_loadavg_2)
  121. {
  122. dst->value = fixed_loadavg(avenrun[2]);
  123. }
  124. /**************************************************************************
  125. * Device names & indices
  126. **************************************************************************/
  127. static inline int int_dev(struct net_device *dev, struct meta_obj *dst)
  128. {
  129. if (unlikely(dev == NULL))
  130. return -1;
  131. dst->value = dev->ifindex;
  132. return 0;
  133. }
  134. static inline int var_dev(struct net_device *dev, struct meta_obj *dst)
  135. {
  136. if (unlikely(dev == NULL))
  137. return -1;
  138. dst->value = (unsigned long) dev->name;
  139. dst->len = strlen(dev->name);
  140. return 0;
  141. }
  142. META_COLLECTOR(int_dev)
  143. {
  144. *err = int_dev(skb->dev, dst);
  145. }
  146. META_COLLECTOR(var_dev)
  147. {
  148. *err = var_dev(skb->dev, dst);
  149. }
  150. META_COLLECTOR(int_indev)
  151. {
  152. *err = int_dev(skb->input_dev, dst);
  153. }
  154. META_COLLECTOR(var_indev)
  155. {
  156. *err = var_dev(skb->input_dev, dst);
  157. }
  158. META_COLLECTOR(int_realdev)
  159. {
  160. *err = int_dev(skb->real_dev, dst);
  161. }
  162. META_COLLECTOR(var_realdev)
  163. {
  164. *err = var_dev(skb->real_dev, dst);
  165. }
  166. /**************************************************************************
  167. * skb attributes
  168. **************************************************************************/
  169. META_COLLECTOR(int_priority)
  170. {
  171. dst->value = skb->priority;
  172. }
  173. META_COLLECTOR(int_protocol)
  174. {
  175. /* Let userspace take care of the byte ordering */
  176. dst->value = skb->protocol;
  177. }
  178. META_COLLECTOR(int_security)
  179. {
  180. dst->value = skb->security;
  181. }
  182. META_COLLECTOR(int_pkttype)
  183. {
  184. dst->value = skb->pkt_type;
  185. }
  186. META_COLLECTOR(int_pktlen)
  187. {
  188. dst->value = skb->len;
  189. }
  190. META_COLLECTOR(int_datalen)
  191. {
  192. dst->value = skb->data_len;
  193. }
  194. META_COLLECTOR(int_maclen)
  195. {
  196. dst->value = skb->mac_len;
  197. }
  198. /**************************************************************************
  199. * Netfilter
  200. **************************************************************************/
  201. #ifdef CONFIG_NETFILTER
  202. META_COLLECTOR(int_nfmark)
  203. {
  204. dst->value = skb->nfmark;
  205. }
  206. #endif
  207. /**************************************************************************
  208. * Traffic Control
  209. **************************************************************************/
  210. META_COLLECTOR(int_tcindex)
  211. {
  212. dst->value = skb->tc_index;
  213. }
  214. #ifdef CONFIG_NET_CLS_ACT
  215. META_COLLECTOR(int_tcverd)
  216. {
  217. dst->value = skb->tc_verd;
  218. }
  219. META_COLLECTOR(int_tcclassid)
  220. {
  221. dst->value = skb->tc_classid;
  222. }
  223. #endif
  224. /**************************************************************************
  225. * Routing
  226. **************************************************************************/
  227. #ifdef CONFIG_NET_CLS_ROUTE
  228. META_COLLECTOR(int_rtclassid)
  229. {
  230. if (unlikely(skb->dst == NULL))
  231. *err = -1;
  232. else
  233. dst->value = skb->dst->tclassid;
  234. }
  235. #endif
  236. META_COLLECTOR(int_rtiif)
  237. {
  238. if (unlikely(skb->dst == NULL))
  239. *err = -1;
  240. else
  241. dst->value = ((struct rtable*) skb->dst)->fl.iif;
  242. }
  243. /**************************************************************************
  244. * Meta value collectors assignment table
  245. **************************************************************************/
  246. struct meta_ops
  247. {
  248. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  249. struct meta_value *, struct meta_obj *, int *);
  250. };
  251. /* Meta value operations table listing all meta value collectors and
  252. * assigns them to a type and meta id. */
  253. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX+1][TCF_META_ID_MAX+1] = {
  254. [TCF_META_TYPE_VAR] = {
  255. [TCF_META_ID_DEV] = { .get = meta_var_dev },
  256. [TCF_META_ID_INDEV] = { .get = meta_var_indev },
  257. [TCF_META_ID_REALDEV] = { .get = meta_var_realdev }
  258. },
  259. [TCF_META_TYPE_INT] = {
  260. [TCF_META_ID_RANDOM] = { .get = meta_int_random },
  261. [TCF_META_ID_LOADAVG_0] = { .get = meta_int_loadavg_0 },
  262. [TCF_META_ID_LOADAVG_1] = { .get = meta_int_loadavg_1 },
  263. [TCF_META_ID_LOADAVG_2] = { .get = meta_int_loadavg_2 },
  264. [TCF_META_ID_DEV] = { .get = meta_int_dev },
  265. [TCF_META_ID_INDEV] = { .get = meta_int_indev },
  266. [TCF_META_ID_REALDEV] = { .get = meta_int_realdev },
  267. [TCF_META_ID_PRIORITY] = { .get = meta_int_priority },
  268. [TCF_META_ID_PROTOCOL] = { .get = meta_int_protocol },
  269. [TCF_META_ID_SECURITY] = { .get = meta_int_security },
  270. [TCF_META_ID_PKTTYPE] = { .get = meta_int_pkttype },
  271. [TCF_META_ID_PKTLEN] = { .get = meta_int_pktlen },
  272. [TCF_META_ID_DATALEN] = { .get = meta_int_datalen },
  273. [TCF_META_ID_MACLEN] = { .get = meta_int_maclen },
  274. #ifdef CONFIG_NETFILTER
  275. [TCF_META_ID_NFMARK] = { .get = meta_int_nfmark },
  276. #endif
  277. [TCF_META_ID_TCINDEX] = { .get = meta_int_tcindex },
  278. #ifdef CONFIG_NET_CLS_ACT
  279. [TCF_META_ID_TCVERDICT] = { .get = meta_int_tcverd },
  280. [TCF_META_ID_TCCLASSID] = { .get = meta_int_tcclassid },
  281. #endif
  282. #ifdef CONFIG_NET_CLS_ROUTE
  283. [TCF_META_ID_RTCLASSID] = { .get = meta_int_rtclassid },
  284. #endif
  285. [TCF_META_ID_RTIIF] = { .get = meta_int_rtiif }
  286. }
  287. };
  288. static inline struct meta_ops * meta_ops(struct meta_value *val)
  289. {
  290. return &__meta_ops[meta_type(val)][meta_id(val)];
  291. }
  292. /**************************************************************************
  293. * Type specific operations for TCF_META_TYPE_VAR
  294. **************************************************************************/
  295. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  296. {
  297. int r = a->len - b->len;
  298. if (r == 0)
  299. r = memcmp((void *) a->value, (void *) b->value, a->len);
  300. return r;
  301. }
  302. static int meta_var_change(struct meta_value *dst, struct rtattr *rta)
  303. {
  304. int len = RTA_PAYLOAD(rta);
  305. dst->val = (unsigned long) kmalloc(len, GFP_KERNEL);
  306. if (dst->val == 0UL)
  307. return -ENOMEM;
  308. memcpy((void *) dst->val, RTA_DATA(rta), len);
  309. dst->len = len;
  310. return 0;
  311. }
  312. static void meta_var_destroy(struct meta_value *v)
  313. {
  314. if (v->val)
  315. kfree((void *) v->val);
  316. }
  317. static void meta_var_apply_extras(struct meta_value *v,
  318. struct meta_obj *dst)
  319. {
  320. int shift = v->hdr.shift;
  321. if (shift && shift < dst->len)
  322. dst->len -= shift;
  323. }
  324. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  325. {
  326. if (v->val && v->len)
  327. RTA_PUT(skb, tlv, v->len, (void *) v->val);
  328. return 0;
  329. rtattr_failure:
  330. return -1;
  331. }
  332. /**************************************************************************
  333. * Type specific operations for TCF_META_TYPE_INT
  334. **************************************************************************/
  335. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  336. {
  337. /* Let gcc optimize it, the unlikely is not really based on
  338. * some numbers but jump free code for mismatches seems
  339. * more logical. */
  340. if (unlikely(a == b))
  341. return 0;
  342. else if (a < b)
  343. return -1;
  344. else
  345. return 1;
  346. }
  347. static int meta_int_change(struct meta_value *dst, struct rtattr *rta)
  348. {
  349. if (RTA_PAYLOAD(rta) >= sizeof(unsigned long)) {
  350. dst->val = *(unsigned long *) RTA_DATA(rta);
  351. dst->len = sizeof(unsigned long);
  352. } else if (RTA_PAYLOAD(rta) == sizeof(u32)) {
  353. dst->val = *(u32 *) RTA_DATA(rta);
  354. dst->len = sizeof(u32);
  355. } else
  356. return -EINVAL;
  357. return 0;
  358. }
  359. static void meta_int_apply_extras(struct meta_value *v,
  360. struct meta_obj *dst)
  361. {
  362. if (v->hdr.shift)
  363. dst->value >>= v->hdr.shift;
  364. if (v->val)
  365. dst->value &= v->val;
  366. }
  367. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  368. {
  369. if (v->len == sizeof(unsigned long))
  370. RTA_PUT(skb, tlv, sizeof(unsigned long), &v->val);
  371. else if (v->len == sizeof(u32)) {
  372. u32 d = v->val;
  373. RTA_PUT(skb, tlv, sizeof(d), &d);
  374. }
  375. return 0;
  376. rtattr_failure:
  377. return -1;
  378. }
  379. /**************************************************************************
  380. * Type specific operations table
  381. **************************************************************************/
  382. struct meta_type_ops
  383. {
  384. void (*destroy)(struct meta_value *);
  385. int (*compare)(struct meta_obj *, struct meta_obj *);
  386. int (*change)(struct meta_value *, struct rtattr *);
  387. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  388. int (*dump)(struct sk_buff *, struct meta_value *, int);
  389. };
  390. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX+1] = {
  391. [TCF_META_TYPE_VAR] = {
  392. .destroy = meta_var_destroy,
  393. .compare = meta_var_compare,
  394. .change = meta_var_change,
  395. .apply_extras = meta_var_apply_extras,
  396. .dump = meta_var_dump
  397. },
  398. [TCF_META_TYPE_INT] = {
  399. .compare = meta_int_compare,
  400. .change = meta_int_change,
  401. .apply_extras = meta_int_apply_extras,
  402. .dump = meta_int_dump
  403. }
  404. };
  405. static inline struct meta_type_ops * meta_type_ops(struct meta_value *v)
  406. {
  407. return &__meta_type_ops[meta_type(v)];
  408. }
  409. /**************************************************************************
  410. * Core
  411. **************************************************************************/
  412. static inline int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  413. struct meta_value *v, struct meta_obj *dst)
  414. {
  415. int err = 0;
  416. if (meta_id(v) == TCF_META_ID_VALUE) {
  417. dst->value = v->val;
  418. dst->len = v->len;
  419. return 0;
  420. }
  421. meta_ops(v)->get(skb, info, v, dst, &err);
  422. if (err < 0)
  423. return err;
  424. if (meta_type_ops(v)->apply_extras)
  425. meta_type_ops(v)->apply_extras(v, dst);
  426. return 0;
  427. }
  428. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  429. struct tcf_pkt_info *info)
  430. {
  431. int r;
  432. struct meta_match *meta = (struct meta_match *) m->data;
  433. struct meta_obj l_value, r_value;
  434. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  435. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  436. return 0;
  437. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  438. switch (meta->lvalue.hdr.op) {
  439. case TCF_EM_OPND_EQ:
  440. return !r;
  441. case TCF_EM_OPND_LT:
  442. return r < 0;
  443. case TCF_EM_OPND_GT:
  444. return r > 0;
  445. }
  446. return 0;
  447. }
  448. static inline void meta_delete(struct meta_match *meta)
  449. {
  450. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  451. if (ops && ops->destroy) {
  452. ops->destroy(&meta->lvalue);
  453. ops->destroy(&meta->rvalue);
  454. }
  455. kfree(meta);
  456. }
  457. static inline int meta_change_data(struct meta_value *dst, struct rtattr *rta)
  458. {
  459. if (rta) {
  460. if (RTA_PAYLOAD(rta) == 0)
  461. return -EINVAL;
  462. return meta_type_ops(dst)->change(dst, rta);
  463. }
  464. return 0;
  465. }
  466. static inline int meta_is_supported(struct meta_value *val)
  467. {
  468. return (!meta_id(val) || meta_ops(val)->get);
  469. }
  470. static int em_meta_change(struct tcf_proto *tp, void *data, int len,
  471. struct tcf_ematch *m)
  472. {
  473. int err = -EINVAL;
  474. struct rtattr *tb[TCA_EM_META_MAX];
  475. struct tcf_meta_hdr *hdr;
  476. struct meta_match *meta = NULL;
  477. if (rtattr_parse(tb, TCA_EM_META_MAX, data, len) < 0)
  478. goto errout;
  479. if (tb[TCA_EM_META_HDR-1] == NULL ||
  480. RTA_PAYLOAD(tb[TCA_EM_META_HDR-1]) < sizeof(*hdr))
  481. goto errout;
  482. hdr = RTA_DATA(tb[TCA_EM_META_HDR-1]);
  483. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  484. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  485. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  486. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  487. goto errout;
  488. meta = kmalloc(sizeof(*meta), GFP_KERNEL);
  489. if (meta == NULL)
  490. goto errout;
  491. memset(meta, 0, sizeof(*meta));
  492. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  493. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  494. if (!meta_is_supported(&meta->lvalue) ||
  495. !meta_is_supported(&meta->rvalue)) {
  496. err = -EOPNOTSUPP;
  497. goto errout;
  498. }
  499. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE-1]) < 0 ||
  500. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE-1]) < 0)
  501. goto errout;
  502. m->datalen = sizeof(*meta);
  503. m->data = (unsigned long) meta;
  504. err = 0;
  505. errout:
  506. if (err && meta)
  507. meta_delete(meta);
  508. return err;
  509. }
  510. static void em_meta_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  511. {
  512. if (m)
  513. meta_delete((struct meta_match *) m->data);
  514. }
  515. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  516. {
  517. struct meta_match *meta = (struct meta_match *) em->data;
  518. struct tcf_meta_hdr hdr;
  519. struct meta_type_ops *ops;
  520. memset(&hdr, 0, sizeof(hdr));
  521. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  522. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  523. RTA_PUT(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr);
  524. ops = meta_type_ops(&meta->lvalue);
  525. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  526. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  527. goto rtattr_failure;
  528. return 0;
  529. rtattr_failure:
  530. return -1;
  531. }
  532. static struct tcf_ematch_ops em_meta_ops = {
  533. .kind = TCF_EM_META,
  534. .change = em_meta_change,
  535. .match = em_meta_match,
  536. .destroy = em_meta_destroy,
  537. .dump = em_meta_dump,
  538. .owner = THIS_MODULE,
  539. .link = LIST_HEAD_INIT(em_meta_ops.link)
  540. };
  541. static int __init init_em_meta(void)
  542. {
  543. return tcf_em_register(&em_meta_ops);
  544. }
  545. static void __exit exit_em_meta(void)
  546. {
  547. tcf_em_unregister(&em_meta_ops);
  548. }
  549. MODULE_LICENSE("GPL");
  550. module_init(init_em_meta);
  551. module_exit(exit_em_meta);