em_meta.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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: DEV | | id: VALUE |
  31. * | data: | | data: 3 |
  32. * +-----------+ +-----------+
  33. * | |
  34. * ---> meta_ops[INT][DEV](...) |
  35. * | |
  36. * ----------- |
  37. * V V
  38. * +-----------+ +-----------+
  39. * | type: INT | | type: INT |
  40. * obj | id: DEV | | 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/module.h>
  61. #include <linux/types.h>
  62. #include <linux/kernel.h>
  63. #include <linux/sched.h>
  64. #include <linux/string.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/random.h>
  67. #include <linux/tc_ematch/tc_em_meta.h>
  68. #include <net/dst.h>
  69. #include <net/route.h>
  70. #include <net/pkt_cls.h>
  71. #include <net/sock.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. /**************************************************************************
  151. * skb attributes
  152. **************************************************************************/
  153. META_COLLECTOR(int_priority)
  154. {
  155. dst->value = skb->priority;
  156. }
  157. META_COLLECTOR(int_protocol)
  158. {
  159. /* Let userspace take care of the byte ordering */
  160. dst->value = skb->protocol;
  161. }
  162. META_COLLECTOR(int_pkttype)
  163. {
  164. dst->value = skb->pkt_type;
  165. }
  166. META_COLLECTOR(int_pktlen)
  167. {
  168. dst->value = skb->len;
  169. }
  170. META_COLLECTOR(int_datalen)
  171. {
  172. dst->value = skb->data_len;
  173. }
  174. META_COLLECTOR(int_maclen)
  175. {
  176. dst->value = skb->mac_len;
  177. }
  178. /**************************************************************************
  179. * Netfilter
  180. **************************************************************************/
  181. META_COLLECTOR(int_mark)
  182. {
  183. dst->value = skb->mark;
  184. }
  185. /**************************************************************************
  186. * Traffic Control
  187. **************************************************************************/
  188. META_COLLECTOR(int_tcindex)
  189. {
  190. dst->value = skb->tc_index;
  191. }
  192. /**************************************************************************
  193. * Routing
  194. **************************************************************************/
  195. META_COLLECTOR(int_rtclassid)
  196. {
  197. if (unlikely(skb->dst == NULL))
  198. *err = -1;
  199. else
  200. #ifdef CONFIG_NET_CLS_ROUTE
  201. dst->value = skb->dst->tclassid;
  202. #else
  203. dst->value = 0;
  204. #endif
  205. }
  206. META_COLLECTOR(int_rtiif)
  207. {
  208. if (unlikely(skb->dst == NULL))
  209. *err = -1;
  210. else
  211. dst->value = ((struct rtable*) skb->dst)->fl.iif;
  212. }
  213. /**************************************************************************
  214. * Socket Attributes
  215. **************************************************************************/
  216. #define SKIP_NONLOCAL(skb) \
  217. if (unlikely(skb->sk == NULL)) { \
  218. *err = -1; \
  219. return; \
  220. }
  221. META_COLLECTOR(int_sk_family)
  222. {
  223. SKIP_NONLOCAL(skb);
  224. dst->value = skb->sk->sk_family;
  225. }
  226. META_COLLECTOR(int_sk_state)
  227. {
  228. SKIP_NONLOCAL(skb);
  229. dst->value = skb->sk->sk_state;
  230. }
  231. META_COLLECTOR(int_sk_reuse)
  232. {
  233. SKIP_NONLOCAL(skb);
  234. dst->value = skb->sk->sk_reuse;
  235. }
  236. META_COLLECTOR(int_sk_bound_if)
  237. {
  238. SKIP_NONLOCAL(skb);
  239. /* No error if bound_dev_if is 0, legal userspace check */
  240. dst->value = skb->sk->sk_bound_dev_if;
  241. }
  242. META_COLLECTOR(var_sk_bound_if)
  243. {
  244. SKIP_NONLOCAL(skb);
  245. if (skb->sk->sk_bound_dev_if == 0) {
  246. dst->value = (unsigned long) "any";
  247. dst->len = 3;
  248. } else {
  249. struct net_device *dev;
  250. dev = dev_get_by_index(&init_net, skb->sk->sk_bound_dev_if);
  251. *err = var_dev(dev, dst);
  252. if (dev)
  253. dev_put(dev);
  254. }
  255. }
  256. META_COLLECTOR(int_sk_refcnt)
  257. {
  258. SKIP_NONLOCAL(skb);
  259. dst->value = atomic_read(&skb->sk->sk_refcnt);
  260. }
  261. META_COLLECTOR(int_sk_rcvbuf)
  262. {
  263. SKIP_NONLOCAL(skb);
  264. dst->value = skb->sk->sk_rcvbuf;
  265. }
  266. META_COLLECTOR(int_sk_shutdown)
  267. {
  268. SKIP_NONLOCAL(skb);
  269. dst->value = skb->sk->sk_shutdown;
  270. }
  271. META_COLLECTOR(int_sk_proto)
  272. {
  273. SKIP_NONLOCAL(skb);
  274. dst->value = skb->sk->sk_protocol;
  275. }
  276. META_COLLECTOR(int_sk_type)
  277. {
  278. SKIP_NONLOCAL(skb);
  279. dst->value = skb->sk->sk_type;
  280. }
  281. META_COLLECTOR(int_sk_rmem_alloc)
  282. {
  283. SKIP_NONLOCAL(skb);
  284. dst->value = atomic_read(&skb->sk->sk_rmem_alloc);
  285. }
  286. META_COLLECTOR(int_sk_wmem_alloc)
  287. {
  288. SKIP_NONLOCAL(skb);
  289. dst->value = atomic_read(&skb->sk->sk_wmem_alloc);
  290. }
  291. META_COLLECTOR(int_sk_omem_alloc)
  292. {
  293. SKIP_NONLOCAL(skb);
  294. dst->value = atomic_read(&skb->sk->sk_omem_alloc);
  295. }
  296. META_COLLECTOR(int_sk_rcv_qlen)
  297. {
  298. SKIP_NONLOCAL(skb);
  299. dst->value = skb->sk->sk_receive_queue.qlen;
  300. }
  301. META_COLLECTOR(int_sk_snd_qlen)
  302. {
  303. SKIP_NONLOCAL(skb);
  304. dst->value = skb->sk->sk_write_queue.qlen;
  305. }
  306. META_COLLECTOR(int_sk_wmem_queued)
  307. {
  308. SKIP_NONLOCAL(skb);
  309. dst->value = skb->sk->sk_wmem_queued;
  310. }
  311. META_COLLECTOR(int_sk_fwd_alloc)
  312. {
  313. SKIP_NONLOCAL(skb);
  314. dst->value = skb->sk->sk_forward_alloc;
  315. }
  316. META_COLLECTOR(int_sk_sndbuf)
  317. {
  318. SKIP_NONLOCAL(skb);
  319. dst->value = skb->sk->sk_sndbuf;
  320. }
  321. META_COLLECTOR(int_sk_alloc)
  322. {
  323. SKIP_NONLOCAL(skb);
  324. dst->value = skb->sk->sk_allocation;
  325. }
  326. META_COLLECTOR(int_sk_route_caps)
  327. {
  328. SKIP_NONLOCAL(skb);
  329. dst->value = skb->sk->sk_route_caps;
  330. }
  331. META_COLLECTOR(int_sk_hash)
  332. {
  333. SKIP_NONLOCAL(skb);
  334. dst->value = skb->sk->sk_hash;
  335. }
  336. META_COLLECTOR(int_sk_lingertime)
  337. {
  338. SKIP_NONLOCAL(skb);
  339. dst->value = skb->sk->sk_lingertime / HZ;
  340. }
  341. META_COLLECTOR(int_sk_err_qlen)
  342. {
  343. SKIP_NONLOCAL(skb);
  344. dst->value = skb->sk->sk_error_queue.qlen;
  345. }
  346. META_COLLECTOR(int_sk_ack_bl)
  347. {
  348. SKIP_NONLOCAL(skb);
  349. dst->value = skb->sk->sk_ack_backlog;
  350. }
  351. META_COLLECTOR(int_sk_max_ack_bl)
  352. {
  353. SKIP_NONLOCAL(skb);
  354. dst->value = skb->sk->sk_max_ack_backlog;
  355. }
  356. META_COLLECTOR(int_sk_prio)
  357. {
  358. SKIP_NONLOCAL(skb);
  359. dst->value = skb->sk->sk_priority;
  360. }
  361. META_COLLECTOR(int_sk_rcvlowat)
  362. {
  363. SKIP_NONLOCAL(skb);
  364. dst->value = skb->sk->sk_rcvlowat;
  365. }
  366. META_COLLECTOR(int_sk_rcvtimeo)
  367. {
  368. SKIP_NONLOCAL(skb);
  369. dst->value = skb->sk->sk_rcvtimeo / HZ;
  370. }
  371. META_COLLECTOR(int_sk_sndtimeo)
  372. {
  373. SKIP_NONLOCAL(skb);
  374. dst->value = skb->sk->sk_sndtimeo / HZ;
  375. }
  376. META_COLLECTOR(int_sk_sendmsg_off)
  377. {
  378. SKIP_NONLOCAL(skb);
  379. dst->value = skb->sk->sk_sndmsg_off;
  380. }
  381. META_COLLECTOR(int_sk_write_pend)
  382. {
  383. SKIP_NONLOCAL(skb);
  384. dst->value = skb->sk->sk_write_pending;
  385. }
  386. /**************************************************************************
  387. * Meta value collectors assignment table
  388. **************************************************************************/
  389. struct meta_ops
  390. {
  391. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  392. struct meta_value *, struct meta_obj *, int *);
  393. };
  394. #define META_ID(name) TCF_META_ID_##name
  395. #define META_FUNC(name) { .get = meta_##name }
  396. /* Meta value operations table listing all meta value collectors and
  397. * assigns them to a type and meta id. */
  398. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX+1][TCF_META_ID_MAX+1] = {
  399. [TCF_META_TYPE_VAR] = {
  400. [META_ID(DEV)] = META_FUNC(var_dev),
  401. [META_ID(SK_BOUND_IF)] = META_FUNC(var_sk_bound_if),
  402. },
  403. [TCF_META_TYPE_INT] = {
  404. [META_ID(RANDOM)] = META_FUNC(int_random),
  405. [META_ID(LOADAVG_0)] = META_FUNC(int_loadavg_0),
  406. [META_ID(LOADAVG_1)] = META_FUNC(int_loadavg_1),
  407. [META_ID(LOADAVG_2)] = META_FUNC(int_loadavg_2),
  408. [META_ID(DEV)] = META_FUNC(int_dev),
  409. [META_ID(PRIORITY)] = META_FUNC(int_priority),
  410. [META_ID(PROTOCOL)] = META_FUNC(int_protocol),
  411. [META_ID(PKTTYPE)] = META_FUNC(int_pkttype),
  412. [META_ID(PKTLEN)] = META_FUNC(int_pktlen),
  413. [META_ID(DATALEN)] = META_FUNC(int_datalen),
  414. [META_ID(MACLEN)] = META_FUNC(int_maclen),
  415. [META_ID(NFMARK)] = META_FUNC(int_mark),
  416. [META_ID(TCINDEX)] = META_FUNC(int_tcindex),
  417. [META_ID(RTCLASSID)] = META_FUNC(int_rtclassid),
  418. [META_ID(RTIIF)] = META_FUNC(int_rtiif),
  419. [META_ID(SK_FAMILY)] = META_FUNC(int_sk_family),
  420. [META_ID(SK_STATE)] = META_FUNC(int_sk_state),
  421. [META_ID(SK_REUSE)] = META_FUNC(int_sk_reuse),
  422. [META_ID(SK_BOUND_IF)] = META_FUNC(int_sk_bound_if),
  423. [META_ID(SK_REFCNT)] = META_FUNC(int_sk_refcnt),
  424. [META_ID(SK_RCVBUF)] = META_FUNC(int_sk_rcvbuf),
  425. [META_ID(SK_SNDBUF)] = META_FUNC(int_sk_sndbuf),
  426. [META_ID(SK_SHUTDOWN)] = META_FUNC(int_sk_shutdown),
  427. [META_ID(SK_PROTO)] = META_FUNC(int_sk_proto),
  428. [META_ID(SK_TYPE)] = META_FUNC(int_sk_type),
  429. [META_ID(SK_RMEM_ALLOC)] = META_FUNC(int_sk_rmem_alloc),
  430. [META_ID(SK_WMEM_ALLOC)] = META_FUNC(int_sk_wmem_alloc),
  431. [META_ID(SK_OMEM_ALLOC)] = META_FUNC(int_sk_omem_alloc),
  432. [META_ID(SK_WMEM_QUEUED)] = META_FUNC(int_sk_wmem_queued),
  433. [META_ID(SK_RCV_QLEN)] = META_FUNC(int_sk_rcv_qlen),
  434. [META_ID(SK_SND_QLEN)] = META_FUNC(int_sk_snd_qlen),
  435. [META_ID(SK_ERR_QLEN)] = META_FUNC(int_sk_err_qlen),
  436. [META_ID(SK_FORWARD_ALLOCS)] = META_FUNC(int_sk_fwd_alloc),
  437. [META_ID(SK_ALLOCS)] = META_FUNC(int_sk_alloc),
  438. [META_ID(SK_ROUTE_CAPS)] = META_FUNC(int_sk_route_caps),
  439. [META_ID(SK_HASH)] = META_FUNC(int_sk_hash),
  440. [META_ID(SK_LINGERTIME)] = META_FUNC(int_sk_lingertime),
  441. [META_ID(SK_ACK_BACKLOG)] = META_FUNC(int_sk_ack_bl),
  442. [META_ID(SK_MAX_ACK_BACKLOG)] = META_FUNC(int_sk_max_ack_bl),
  443. [META_ID(SK_PRIO)] = META_FUNC(int_sk_prio),
  444. [META_ID(SK_RCVLOWAT)] = META_FUNC(int_sk_rcvlowat),
  445. [META_ID(SK_RCVTIMEO)] = META_FUNC(int_sk_rcvtimeo),
  446. [META_ID(SK_SNDTIMEO)] = META_FUNC(int_sk_sndtimeo),
  447. [META_ID(SK_SENDMSG_OFF)] = META_FUNC(int_sk_sendmsg_off),
  448. [META_ID(SK_WRITE_PENDING)] = META_FUNC(int_sk_write_pend),
  449. }
  450. };
  451. static inline struct meta_ops * meta_ops(struct meta_value *val)
  452. {
  453. return &__meta_ops[meta_type(val)][meta_id(val)];
  454. }
  455. /**************************************************************************
  456. * Type specific operations for TCF_META_TYPE_VAR
  457. **************************************************************************/
  458. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  459. {
  460. int r = a->len - b->len;
  461. if (r == 0)
  462. r = memcmp((void *) a->value, (void *) b->value, a->len);
  463. return r;
  464. }
  465. static int meta_var_change(struct meta_value *dst, struct rtattr *rta)
  466. {
  467. int len = RTA_PAYLOAD(rta);
  468. dst->val = (unsigned long)kmemdup(RTA_DATA(rta), len, GFP_KERNEL);
  469. if (dst->val == 0UL)
  470. return -ENOMEM;
  471. dst->len = len;
  472. return 0;
  473. }
  474. static void meta_var_destroy(struct meta_value *v)
  475. {
  476. kfree((void *) v->val);
  477. }
  478. static void meta_var_apply_extras(struct meta_value *v,
  479. struct meta_obj *dst)
  480. {
  481. int shift = v->hdr.shift;
  482. if (shift && shift < dst->len)
  483. dst->len -= shift;
  484. }
  485. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  486. {
  487. if (v->val && v->len)
  488. RTA_PUT(skb, tlv, v->len, (void *) v->val);
  489. return 0;
  490. rtattr_failure:
  491. return -1;
  492. }
  493. /**************************************************************************
  494. * Type specific operations for TCF_META_TYPE_INT
  495. **************************************************************************/
  496. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  497. {
  498. /* Let gcc optimize it, the unlikely is not really based on
  499. * some numbers but jump free code for mismatches seems
  500. * more logical. */
  501. if (unlikely(a->value == b->value))
  502. return 0;
  503. else if (a->value < b->value)
  504. return -1;
  505. else
  506. return 1;
  507. }
  508. static int meta_int_change(struct meta_value *dst, struct rtattr *rta)
  509. {
  510. if (RTA_PAYLOAD(rta) >= sizeof(unsigned long)) {
  511. dst->val = *(unsigned long *) RTA_DATA(rta);
  512. dst->len = sizeof(unsigned long);
  513. } else if (RTA_PAYLOAD(rta) == sizeof(u32)) {
  514. dst->val = *(u32 *) RTA_DATA(rta);
  515. dst->len = sizeof(u32);
  516. } else
  517. return -EINVAL;
  518. return 0;
  519. }
  520. static void meta_int_apply_extras(struct meta_value *v,
  521. struct meta_obj *dst)
  522. {
  523. if (v->hdr.shift)
  524. dst->value >>= v->hdr.shift;
  525. if (v->val)
  526. dst->value &= v->val;
  527. }
  528. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  529. {
  530. if (v->len == sizeof(unsigned long))
  531. RTA_PUT(skb, tlv, sizeof(unsigned long), &v->val);
  532. else if (v->len == sizeof(u32)) {
  533. u32 d = v->val;
  534. RTA_PUT(skb, tlv, sizeof(d), &d);
  535. }
  536. return 0;
  537. rtattr_failure:
  538. return -1;
  539. }
  540. /**************************************************************************
  541. * Type specific operations table
  542. **************************************************************************/
  543. struct meta_type_ops
  544. {
  545. void (*destroy)(struct meta_value *);
  546. int (*compare)(struct meta_obj *, struct meta_obj *);
  547. int (*change)(struct meta_value *, struct rtattr *);
  548. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  549. int (*dump)(struct sk_buff *, struct meta_value *, int);
  550. };
  551. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX+1] = {
  552. [TCF_META_TYPE_VAR] = {
  553. .destroy = meta_var_destroy,
  554. .compare = meta_var_compare,
  555. .change = meta_var_change,
  556. .apply_extras = meta_var_apply_extras,
  557. .dump = meta_var_dump
  558. },
  559. [TCF_META_TYPE_INT] = {
  560. .compare = meta_int_compare,
  561. .change = meta_int_change,
  562. .apply_extras = meta_int_apply_extras,
  563. .dump = meta_int_dump
  564. }
  565. };
  566. static inline struct meta_type_ops * meta_type_ops(struct meta_value *v)
  567. {
  568. return &__meta_type_ops[meta_type(v)];
  569. }
  570. /**************************************************************************
  571. * Core
  572. **************************************************************************/
  573. static inline int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  574. struct meta_value *v, struct meta_obj *dst)
  575. {
  576. int err = 0;
  577. if (meta_id(v) == TCF_META_ID_VALUE) {
  578. dst->value = v->val;
  579. dst->len = v->len;
  580. return 0;
  581. }
  582. meta_ops(v)->get(skb, info, v, dst, &err);
  583. if (err < 0)
  584. return err;
  585. if (meta_type_ops(v)->apply_extras)
  586. meta_type_ops(v)->apply_extras(v, dst);
  587. return 0;
  588. }
  589. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  590. struct tcf_pkt_info *info)
  591. {
  592. int r;
  593. struct meta_match *meta = (struct meta_match *) m->data;
  594. struct meta_obj l_value, r_value;
  595. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  596. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  597. return 0;
  598. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  599. switch (meta->lvalue.hdr.op) {
  600. case TCF_EM_OPND_EQ:
  601. return !r;
  602. case TCF_EM_OPND_LT:
  603. return r < 0;
  604. case TCF_EM_OPND_GT:
  605. return r > 0;
  606. }
  607. return 0;
  608. }
  609. static inline void meta_delete(struct meta_match *meta)
  610. {
  611. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  612. if (ops && ops->destroy) {
  613. ops->destroy(&meta->lvalue);
  614. ops->destroy(&meta->rvalue);
  615. }
  616. kfree(meta);
  617. }
  618. static inline int meta_change_data(struct meta_value *dst, struct rtattr *rta)
  619. {
  620. if (rta) {
  621. if (RTA_PAYLOAD(rta) == 0)
  622. return -EINVAL;
  623. return meta_type_ops(dst)->change(dst, rta);
  624. }
  625. return 0;
  626. }
  627. static inline int meta_is_supported(struct meta_value *val)
  628. {
  629. return (!meta_id(val) || meta_ops(val)->get);
  630. }
  631. static int em_meta_change(struct tcf_proto *tp, void *data, int len,
  632. struct tcf_ematch *m)
  633. {
  634. int err = -EINVAL;
  635. struct rtattr *tb[TCA_EM_META_MAX];
  636. struct tcf_meta_hdr *hdr;
  637. struct meta_match *meta = NULL;
  638. if (rtattr_parse(tb, TCA_EM_META_MAX, data, len) < 0)
  639. goto errout;
  640. if (tb[TCA_EM_META_HDR-1] == NULL ||
  641. RTA_PAYLOAD(tb[TCA_EM_META_HDR-1]) < sizeof(*hdr))
  642. goto errout;
  643. hdr = RTA_DATA(tb[TCA_EM_META_HDR-1]);
  644. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  645. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  646. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  647. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  648. goto errout;
  649. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  650. if (meta == NULL)
  651. goto errout;
  652. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  653. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  654. if (!meta_is_supported(&meta->lvalue) ||
  655. !meta_is_supported(&meta->rvalue)) {
  656. err = -EOPNOTSUPP;
  657. goto errout;
  658. }
  659. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE-1]) < 0 ||
  660. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE-1]) < 0)
  661. goto errout;
  662. m->datalen = sizeof(*meta);
  663. m->data = (unsigned long) meta;
  664. err = 0;
  665. errout:
  666. if (err && meta)
  667. meta_delete(meta);
  668. return err;
  669. }
  670. static void em_meta_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  671. {
  672. if (m)
  673. meta_delete((struct meta_match *) m->data);
  674. }
  675. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  676. {
  677. struct meta_match *meta = (struct meta_match *) em->data;
  678. struct tcf_meta_hdr hdr;
  679. struct meta_type_ops *ops;
  680. memset(&hdr, 0, sizeof(hdr));
  681. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  682. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  683. RTA_PUT(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr);
  684. ops = meta_type_ops(&meta->lvalue);
  685. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  686. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  687. goto rtattr_failure;
  688. return 0;
  689. rtattr_failure:
  690. return -1;
  691. }
  692. static struct tcf_ematch_ops em_meta_ops = {
  693. .kind = TCF_EM_META,
  694. .change = em_meta_change,
  695. .match = em_meta_match,
  696. .destroy = em_meta_destroy,
  697. .dump = em_meta_dump,
  698. .owner = THIS_MODULE,
  699. .link = LIST_HEAD_INIT(em_meta_ops.link)
  700. };
  701. static int __init init_em_meta(void)
  702. {
  703. return tcf_em_register(&em_meta_ops);
  704. }
  705. static void __exit exit_em_meta(void)
  706. {
  707. tcf_em_unregister(&em_meta_ops);
  708. }
  709. MODULE_LICENSE("GPL");
  710. module_init(init_em_meta);
  711. module_exit(exit_em_meta);
  712. MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);