em_meta.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. * Additionally, type dependent 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 subsystem is enabled in the kernel.
  59. */
  60. #include <linux/slab.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/if_vlan.h>
  69. #include <linux/tc_ematch/tc_em_meta.h>
  70. #include <net/dst.h>
  71. #include <net/route.h>
  72. #include <net/pkt_cls.h>
  73. #include <net/sock.h>
  74. struct meta_obj {
  75. unsigned long value;
  76. unsigned int len;
  77. };
  78. struct meta_value {
  79. struct tcf_meta_val hdr;
  80. unsigned long val;
  81. unsigned int len;
  82. };
  83. struct meta_match {
  84. struct meta_value lvalue;
  85. struct meta_value rvalue;
  86. };
  87. static inline int meta_id(struct meta_value *v)
  88. {
  89. return TCF_META_ID(v->hdr.kind);
  90. }
  91. static inline int meta_type(struct meta_value *v)
  92. {
  93. return TCF_META_TYPE(v->hdr.kind);
  94. }
  95. #define META_COLLECTOR(FUNC) static void meta_##FUNC(struct sk_buff *skb, \
  96. struct tcf_pkt_info *info, struct meta_value *v, \
  97. struct meta_obj *dst, int *err)
  98. /**************************************************************************
  99. * System status & misc
  100. **************************************************************************/
  101. META_COLLECTOR(int_random)
  102. {
  103. get_random_bytes(&dst->value, sizeof(dst->value));
  104. }
  105. static inline unsigned long fixed_loadavg(int load)
  106. {
  107. int rnd_load = load + (FIXED_1/200);
  108. int rnd_frac = ((rnd_load & (FIXED_1-1)) * 100) >> FSHIFT;
  109. return ((rnd_load >> FSHIFT) * 100) + rnd_frac;
  110. }
  111. META_COLLECTOR(int_loadavg_0)
  112. {
  113. dst->value = fixed_loadavg(avenrun[0]);
  114. }
  115. META_COLLECTOR(int_loadavg_1)
  116. {
  117. dst->value = fixed_loadavg(avenrun[1]);
  118. }
  119. META_COLLECTOR(int_loadavg_2)
  120. {
  121. dst->value = fixed_loadavg(avenrun[2]);
  122. }
  123. /**************************************************************************
  124. * Device names & indices
  125. **************************************************************************/
  126. static inline int int_dev(struct net_device *dev, struct meta_obj *dst)
  127. {
  128. if (unlikely(dev == NULL))
  129. return -1;
  130. dst->value = dev->ifindex;
  131. return 0;
  132. }
  133. static inline int var_dev(struct net_device *dev, struct meta_obj *dst)
  134. {
  135. if (unlikely(dev == NULL))
  136. return -1;
  137. dst->value = (unsigned long) dev->name;
  138. dst->len = strlen(dev->name);
  139. return 0;
  140. }
  141. META_COLLECTOR(int_dev)
  142. {
  143. *err = int_dev(skb->dev, dst);
  144. }
  145. META_COLLECTOR(var_dev)
  146. {
  147. *err = var_dev(skb->dev, dst);
  148. }
  149. /**************************************************************************
  150. * vlan tag
  151. **************************************************************************/
  152. META_COLLECTOR(int_vlan_tag)
  153. {
  154. unsigned short tag;
  155. tag = skb_vlan_tag_get(skb);
  156. if (!tag && __vlan_get_tag(skb, &tag))
  157. *err = -1;
  158. else
  159. dst->value = tag;
  160. }
  161. /**************************************************************************
  162. * skb attributes
  163. **************************************************************************/
  164. META_COLLECTOR(int_priority)
  165. {
  166. dst->value = skb->priority;
  167. }
  168. META_COLLECTOR(int_protocol)
  169. {
  170. /* Let userspace take care of the byte ordering */
  171. dst->value = tc_skb_protocol(skb);
  172. }
  173. META_COLLECTOR(int_pkttype)
  174. {
  175. dst->value = skb->pkt_type;
  176. }
  177. META_COLLECTOR(int_pktlen)
  178. {
  179. dst->value = skb->len;
  180. }
  181. META_COLLECTOR(int_datalen)
  182. {
  183. dst->value = skb->data_len;
  184. }
  185. META_COLLECTOR(int_maclen)
  186. {
  187. dst->value = skb->mac_len;
  188. }
  189. META_COLLECTOR(int_rxhash)
  190. {
  191. dst->value = skb_get_hash(skb);
  192. }
  193. /**************************************************************************
  194. * Netfilter
  195. **************************************************************************/
  196. META_COLLECTOR(int_mark)
  197. {
  198. dst->value = skb->mark;
  199. }
  200. /**************************************************************************
  201. * Traffic Control
  202. **************************************************************************/
  203. META_COLLECTOR(int_tcindex)
  204. {
  205. dst->value = skb->tc_index;
  206. }
  207. /**************************************************************************
  208. * Routing
  209. **************************************************************************/
  210. META_COLLECTOR(int_rtclassid)
  211. {
  212. if (unlikely(skb_dst(skb) == NULL))
  213. *err = -1;
  214. else
  215. #ifdef CONFIG_IP_ROUTE_CLASSID
  216. dst->value = skb_dst(skb)->tclassid;
  217. #else
  218. dst->value = 0;
  219. #endif
  220. }
  221. META_COLLECTOR(int_rtiif)
  222. {
  223. if (unlikely(skb_rtable(skb) == NULL))
  224. *err = -1;
  225. else
  226. dst->value = inet_iif(skb);
  227. }
  228. /**************************************************************************
  229. * Socket Attributes
  230. **************************************************************************/
  231. #define skip_nonlocal(skb) \
  232. (unlikely(skb->sk == NULL))
  233. META_COLLECTOR(int_sk_family)
  234. {
  235. if (skip_nonlocal(skb)) {
  236. *err = -1;
  237. return;
  238. }
  239. dst->value = skb->sk->sk_family;
  240. }
  241. META_COLLECTOR(int_sk_state)
  242. {
  243. if (skip_nonlocal(skb)) {
  244. *err = -1;
  245. return;
  246. }
  247. dst->value = skb->sk->sk_state;
  248. }
  249. META_COLLECTOR(int_sk_reuse)
  250. {
  251. if (skip_nonlocal(skb)) {
  252. *err = -1;
  253. return;
  254. }
  255. dst->value = skb->sk->sk_reuse;
  256. }
  257. META_COLLECTOR(int_sk_bound_if)
  258. {
  259. if (skip_nonlocal(skb)) {
  260. *err = -1;
  261. return;
  262. }
  263. /* No error if bound_dev_if is 0, legal userspace check */
  264. dst->value = skb->sk->sk_bound_dev_if;
  265. }
  266. META_COLLECTOR(var_sk_bound_if)
  267. {
  268. if (skip_nonlocal(skb)) {
  269. *err = -1;
  270. return;
  271. }
  272. if (skb->sk->sk_bound_dev_if == 0) {
  273. dst->value = (unsigned long) "any";
  274. dst->len = 3;
  275. } else {
  276. struct net_device *dev;
  277. rcu_read_lock();
  278. dev = dev_get_by_index_rcu(sock_net(skb->sk),
  279. skb->sk->sk_bound_dev_if);
  280. *err = var_dev(dev, dst);
  281. rcu_read_unlock();
  282. }
  283. }
  284. META_COLLECTOR(int_sk_refcnt)
  285. {
  286. if (skip_nonlocal(skb)) {
  287. *err = -1;
  288. return;
  289. }
  290. dst->value = atomic_read(&skb->sk->sk_refcnt);
  291. }
  292. META_COLLECTOR(int_sk_rcvbuf)
  293. {
  294. const struct sock *sk = skb_to_full_sk(skb);
  295. if (!sk) {
  296. *err = -1;
  297. return;
  298. }
  299. dst->value = sk->sk_rcvbuf;
  300. }
  301. META_COLLECTOR(int_sk_shutdown)
  302. {
  303. const struct sock *sk = skb_to_full_sk(skb);
  304. if (!sk) {
  305. *err = -1;
  306. return;
  307. }
  308. dst->value = sk->sk_shutdown;
  309. }
  310. META_COLLECTOR(int_sk_proto)
  311. {
  312. const struct sock *sk = skb_to_full_sk(skb);
  313. if (!sk) {
  314. *err = -1;
  315. return;
  316. }
  317. dst->value = sk->sk_protocol;
  318. }
  319. META_COLLECTOR(int_sk_type)
  320. {
  321. const struct sock *sk = skb_to_full_sk(skb);
  322. if (!sk) {
  323. *err = -1;
  324. return;
  325. }
  326. dst->value = sk->sk_type;
  327. }
  328. META_COLLECTOR(int_sk_rmem_alloc)
  329. {
  330. const struct sock *sk = skb_to_full_sk(skb);
  331. if (!sk) {
  332. *err = -1;
  333. return;
  334. }
  335. dst->value = sk_rmem_alloc_get(sk);
  336. }
  337. META_COLLECTOR(int_sk_wmem_alloc)
  338. {
  339. const struct sock *sk = skb_to_full_sk(skb);
  340. if (!sk) {
  341. *err = -1;
  342. return;
  343. }
  344. dst->value = sk_wmem_alloc_get(sk);
  345. }
  346. META_COLLECTOR(int_sk_omem_alloc)
  347. {
  348. const struct sock *sk = skb_to_full_sk(skb);
  349. if (!sk) {
  350. *err = -1;
  351. return;
  352. }
  353. dst->value = atomic_read(&sk->sk_omem_alloc);
  354. }
  355. META_COLLECTOR(int_sk_rcv_qlen)
  356. {
  357. const struct sock *sk = skb_to_full_sk(skb);
  358. if (!sk) {
  359. *err = -1;
  360. return;
  361. }
  362. dst->value = sk->sk_receive_queue.qlen;
  363. }
  364. META_COLLECTOR(int_sk_snd_qlen)
  365. {
  366. const struct sock *sk = skb_to_full_sk(skb);
  367. if (!sk) {
  368. *err = -1;
  369. return;
  370. }
  371. dst->value = sk->sk_write_queue.qlen;
  372. }
  373. META_COLLECTOR(int_sk_wmem_queued)
  374. {
  375. const struct sock *sk = skb_to_full_sk(skb);
  376. if (!sk) {
  377. *err = -1;
  378. return;
  379. }
  380. dst->value = sk->sk_wmem_queued;
  381. }
  382. META_COLLECTOR(int_sk_fwd_alloc)
  383. {
  384. const struct sock *sk = skb_to_full_sk(skb);
  385. if (!sk) {
  386. *err = -1;
  387. return;
  388. }
  389. dst->value = sk->sk_forward_alloc;
  390. }
  391. META_COLLECTOR(int_sk_sndbuf)
  392. {
  393. const struct sock *sk = skb_to_full_sk(skb);
  394. if (!sk) {
  395. *err = -1;
  396. return;
  397. }
  398. dst->value = sk->sk_sndbuf;
  399. }
  400. META_COLLECTOR(int_sk_alloc)
  401. {
  402. const struct sock *sk = skb_to_full_sk(skb);
  403. if (!sk) {
  404. *err = -1;
  405. return;
  406. }
  407. dst->value = (__force int) sk->sk_allocation;
  408. }
  409. META_COLLECTOR(int_sk_hash)
  410. {
  411. if (skip_nonlocal(skb)) {
  412. *err = -1;
  413. return;
  414. }
  415. dst->value = skb->sk->sk_hash;
  416. }
  417. META_COLLECTOR(int_sk_lingertime)
  418. {
  419. const struct sock *sk = skb_to_full_sk(skb);
  420. if (!sk) {
  421. *err = -1;
  422. return;
  423. }
  424. dst->value = sk->sk_lingertime / HZ;
  425. }
  426. META_COLLECTOR(int_sk_err_qlen)
  427. {
  428. const struct sock *sk = skb_to_full_sk(skb);
  429. if (!sk) {
  430. *err = -1;
  431. return;
  432. }
  433. dst->value = sk->sk_error_queue.qlen;
  434. }
  435. META_COLLECTOR(int_sk_ack_bl)
  436. {
  437. const struct sock *sk = skb_to_full_sk(skb);
  438. if (!sk) {
  439. *err = -1;
  440. return;
  441. }
  442. dst->value = sk->sk_ack_backlog;
  443. }
  444. META_COLLECTOR(int_sk_max_ack_bl)
  445. {
  446. const struct sock *sk = skb_to_full_sk(skb);
  447. if (!sk) {
  448. *err = -1;
  449. return;
  450. }
  451. dst->value = sk->sk_max_ack_backlog;
  452. }
  453. META_COLLECTOR(int_sk_prio)
  454. {
  455. const struct sock *sk = skb_to_full_sk(skb);
  456. if (!sk) {
  457. *err = -1;
  458. return;
  459. }
  460. dst->value = sk->sk_priority;
  461. }
  462. META_COLLECTOR(int_sk_rcvlowat)
  463. {
  464. const struct sock *sk = skb_to_full_sk(skb);
  465. if (!sk) {
  466. *err = -1;
  467. return;
  468. }
  469. dst->value = sk->sk_rcvlowat;
  470. }
  471. META_COLLECTOR(int_sk_rcvtimeo)
  472. {
  473. const struct sock *sk = skb_to_full_sk(skb);
  474. if (!sk) {
  475. *err = -1;
  476. return;
  477. }
  478. dst->value = sk->sk_rcvtimeo / HZ;
  479. }
  480. META_COLLECTOR(int_sk_sndtimeo)
  481. {
  482. const struct sock *sk = skb_to_full_sk(skb);
  483. if (!sk) {
  484. *err = -1;
  485. return;
  486. }
  487. dst->value = sk->sk_sndtimeo / HZ;
  488. }
  489. META_COLLECTOR(int_sk_sendmsg_off)
  490. {
  491. const struct sock *sk = skb_to_full_sk(skb);
  492. if (!sk) {
  493. *err = -1;
  494. return;
  495. }
  496. dst->value = sk->sk_frag.offset;
  497. }
  498. META_COLLECTOR(int_sk_write_pend)
  499. {
  500. const struct sock *sk = skb_to_full_sk(skb);
  501. if (!sk) {
  502. *err = -1;
  503. return;
  504. }
  505. dst->value = sk->sk_write_pending;
  506. }
  507. /**************************************************************************
  508. * Meta value collectors assignment table
  509. **************************************************************************/
  510. struct meta_ops {
  511. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  512. struct meta_value *, struct meta_obj *, int *);
  513. };
  514. #define META_ID(name) TCF_META_ID_##name
  515. #define META_FUNC(name) { .get = meta_##name }
  516. /* Meta value operations table listing all meta value collectors and
  517. * assigns them to a type and meta id. */
  518. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX + 1][TCF_META_ID_MAX + 1] = {
  519. [TCF_META_TYPE_VAR] = {
  520. [META_ID(DEV)] = META_FUNC(var_dev),
  521. [META_ID(SK_BOUND_IF)] = META_FUNC(var_sk_bound_if),
  522. },
  523. [TCF_META_TYPE_INT] = {
  524. [META_ID(RANDOM)] = META_FUNC(int_random),
  525. [META_ID(LOADAVG_0)] = META_FUNC(int_loadavg_0),
  526. [META_ID(LOADAVG_1)] = META_FUNC(int_loadavg_1),
  527. [META_ID(LOADAVG_2)] = META_FUNC(int_loadavg_2),
  528. [META_ID(DEV)] = META_FUNC(int_dev),
  529. [META_ID(PRIORITY)] = META_FUNC(int_priority),
  530. [META_ID(PROTOCOL)] = META_FUNC(int_protocol),
  531. [META_ID(PKTTYPE)] = META_FUNC(int_pkttype),
  532. [META_ID(PKTLEN)] = META_FUNC(int_pktlen),
  533. [META_ID(DATALEN)] = META_FUNC(int_datalen),
  534. [META_ID(MACLEN)] = META_FUNC(int_maclen),
  535. [META_ID(NFMARK)] = META_FUNC(int_mark),
  536. [META_ID(TCINDEX)] = META_FUNC(int_tcindex),
  537. [META_ID(RTCLASSID)] = META_FUNC(int_rtclassid),
  538. [META_ID(RTIIF)] = META_FUNC(int_rtiif),
  539. [META_ID(SK_FAMILY)] = META_FUNC(int_sk_family),
  540. [META_ID(SK_STATE)] = META_FUNC(int_sk_state),
  541. [META_ID(SK_REUSE)] = META_FUNC(int_sk_reuse),
  542. [META_ID(SK_BOUND_IF)] = META_FUNC(int_sk_bound_if),
  543. [META_ID(SK_REFCNT)] = META_FUNC(int_sk_refcnt),
  544. [META_ID(SK_RCVBUF)] = META_FUNC(int_sk_rcvbuf),
  545. [META_ID(SK_SNDBUF)] = META_FUNC(int_sk_sndbuf),
  546. [META_ID(SK_SHUTDOWN)] = META_FUNC(int_sk_shutdown),
  547. [META_ID(SK_PROTO)] = META_FUNC(int_sk_proto),
  548. [META_ID(SK_TYPE)] = META_FUNC(int_sk_type),
  549. [META_ID(SK_RMEM_ALLOC)] = META_FUNC(int_sk_rmem_alloc),
  550. [META_ID(SK_WMEM_ALLOC)] = META_FUNC(int_sk_wmem_alloc),
  551. [META_ID(SK_OMEM_ALLOC)] = META_FUNC(int_sk_omem_alloc),
  552. [META_ID(SK_WMEM_QUEUED)] = META_FUNC(int_sk_wmem_queued),
  553. [META_ID(SK_RCV_QLEN)] = META_FUNC(int_sk_rcv_qlen),
  554. [META_ID(SK_SND_QLEN)] = META_FUNC(int_sk_snd_qlen),
  555. [META_ID(SK_ERR_QLEN)] = META_FUNC(int_sk_err_qlen),
  556. [META_ID(SK_FORWARD_ALLOCS)] = META_FUNC(int_sk_fwd_alloc),
  557. [META_ID(SK_ALLOCS)] = META_FUNC(int_sk_alloc),
  558. [META_ID(SK_HASH)] = META_FUNC(int_sk_hash),
  559. [META_ID(SK_LINGERTIME)] = META_FUNC(int_sk_lingertime),
  560. [META_ID(SK_ACK_BACKLOG)] = META_FUNC(int_sk_ack_bl),
  561. [META_ID(SK_MAX_ACK_BACKLOG)] = META_FUNC(int_sk_max_ack_bl),
  562. [META_ID(SK_PRIO)] = META_FUNC(int_sk_prio),
  563. [META_ID(SK_RCVLOWAT)] = META_FUNC(int_sk_rcvlowat),
  564. [META_ID(SK_RCVTIMEO)] = META_FUNC(int_sk_rcvtimeo),
  565. [META_ID(SK_SNDTIMEO)] = META_FUNC(int_sk_sndtimeo),
  566. [META_ID(SK_SENDMSG_OFF)] = META_FUNC(int_sk_sendmsg_off),
  567. [META_ID(SK_WRITE_PENDING)] = META_FUNC(int_sk_write_pend),
  568. [META_ID(VLAN_TAG)] = META_FUNC(int_vlan_tag),
  569. [META_ID(RXHASH)] = META_FUNC(int_rxhash),
  570. }
  571. };
  572. static inline struct meta_ops *meta_ops(struct meta_value *val)
  573. {
  574. return &__meta_ops[meta_type(val)][meta_id(val)];
  575. }
  576. /**************************************************************************
  577. * Type specific operations for TCF_META_TYPE_VAR
  578. **************************************************************************/
  579. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  580. {
  581. int r = a->len - b->len;
  582. if (r == 0)
  583. r = memcmp((void *) a->value, (void *) b->value, a->len);
  584. return r;
  585. }
  586. static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
  587. {
  588. int len = nla_len(nla);
  589. dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
  590. if (dst->val == 0UL)
  591. return -ENOMEM;
  592. dst->len = len;
  593. return 0;
  594. }
  595. static void meta_var_destroy(struct meta_value *v)
  596. {
  597. kfree((void *) v->val);
  598. }
  599. static void meta_var_apply_extras(struct meta_value *v,
  600. struct meta_obj *dst)
  601. {
  602. int shift = v->hdr.shift;
  603. if (shift && shift < dst->len)
  604. dst->len -= shift;
  605. }
  606. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  607. {
  608. if (v->val && v->len &&
  609. nla_put(skb, tlv, v->len, (void *) v->val))
  610. goto nla_put_failure;
  611. return 0;
  612. nla_put_failure:
  613. return -1;
  614. }
  615. /**************************************************************************
  616. * Type specific operations for TCF_META_TYPE_INT
  617. **************************************************************************/
  618. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  619. {
  620. /* Let gcc optimize it, the unlikely is not really based on
  621. * some numbers but jump free code for mismatches seems
  622. * more logical. */
  623. if (unlikely(a->value == b->value))
  624. return 0;
  625. else if (a->value < b->value)
  626. return -1;
  627. else
  628. return 1;
  629. }
  630. static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
  631. {
  632. if (nla_len(nla) >= sizeof(unsigned long)) {
  633. dst->val = *(unsigned long *) nla_data(nla);
  634. dst->len = sizeof(unsigned long);
  635. } else if (nla_len(nla) == sizeof(u32)) {
  636. dst->val = nla_get_u32(nla);
  637. dst->len = sizeof(u32);
  638. } else
  639. return -EINVAL;
  640. return 0;
  641. }
  642. static void meta_int_apply_extras(struct meta_value *v,
  643. struct meta_obj *dst)
  644. {
  645. if (v->hdr.shift)
  646. dst->value >>= v->hdr.shift;
  647. if (v->val)
  648. dst->value &= v->val;
  649. }
  650. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  651. {
  652. if (v->len == sizeof(unsigned long)) {
  653. if (nla_put(skb, tlv, sizeof(unsigned long), &v->val))
  654. goto nla_put_failure;
  655. } else if (v->len == sizeof(u32)) {
  656. if (nla_put_u32(skb, tlv, v->val))
  657. goto nla_put_failure;
  658. }
  659. return 0;
  660. nla_put_failure:
  661. return -1;
  662. }
  663. /**************************************************************************
  664. * Type specific operations table
  665. **************************************************************************/
  666. struct meta_type_ops {
  667. void (*destroy)(struct meta_value *);
  668. int (*compare)(struct meta_obj *, struct meta_obj *);
  669. int (*change)(struct meta_value *, struct nlattr *);
  670. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  671. int (*dump)(struct sk_buff *, struct meta_value *, int);
  672. };
  673. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX + 1] = {
  674. [TCF_META_TYPE_VAR] = {
  675. .destroy = meta_var_destroy,
  676. .compare = meta_var_compare,
  677. .change = meta_var_change,
  678. .apply_extras = meta_var_apply_extras,
  679. .dump = meta_var_dump
  680. },
  681. [TCF_META_TYPE_INT] = {
  682. .compare = meta_int_compare,
  683. .change = meta_int_change,
  684. .apply_extras = meta_int_apply_extras,
  685. .dump = meta_int_dump
  686. }
  687. };
  688. static inline struct meta_type_ops *meta_type_ops(struct meta_value *v)
  689. {
  690. return &__meta_type_ops[meta_type(v)];
  691. }
  692. /**************************************************************************
  693. * Core
  694. **************************************************************************/
  695. static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  696. struct meta_value *v, struct meta_obj *dst)
  697. {
  698. int err = 0;
  699. if (meta_id(v) == TCF_META_ID_VALUE) {
  700. dst->value = v->val;
  701. dst->len = v->len;
  702. return 0;
  703. }
  704. meta_ops(v)->get(skb, info, v, dst, &err);
  705. if (err < 0)
  706. return err;
  707. if (meta_type_ops(v)->apply_extras)
  708. meta_type_ops(v)->apply_extras(v, dst);
  709. return 0;
  710. }
  711. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  712. struct tcf_pkt_info *info)
  713. {
  714. int r;
  715. struct meta_match *meta = (struct meta_match *) m->data;
  716. struct meta_obj l_value, r_value;
  717. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  718. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  719. return 0;
  720. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  721. switch (meta->lvalue.hdr.op) {
  722. case TCF_EM_OPND_EQ:
  723. return !r;
  724. case TCF_EM_OPND_LT:
  725. return r < 0;
  726. case TCF_EM_OPND_GT:
  727. return r > 0;
  728. }
  729. return 0;
  730. }
  731. static void meta_delete(struct meta_match *meta)
  732. {
  733. if (meta) {
  734. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  735. if (ops && ops->destroy) {
  736. ops->destroy(&meta->lvalue);
  737. ops->destroy(&meta->rvalue);
  738. }
  739. }
  740. kfree(meta);
  741. }
  742. static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
  743. {
  744. if (nla) {
  745. if (nla_len(nla) == 0)
  746. return -EINVAL;
  747. return meta_type_ops(dst)->change(dst, nla);
  748. }
  749. return 0;
  750. }
  751. static inline int meta_is_supported(struct meta_value *val)
  752. {
  753. return !meta_id(val) || meta_ops(val)->get;
  754. }
  755. static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
  756. [TCA_EM_META_HDR] = { .len = sizeof(struct tcf_meta_hdr) },
  757. };
  758. static int em_meta_change(struct net *net, void *data, int len,
  759. struct tcf_ematch *m)
  760. {
  761. int err;
  762. struct nlattr *tb[TCA_EM_META_MAX + 1];
  763. struct tcf_meta_hdr *hdr;
  764. struct meta_match *meta = NULL;
  765. err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy);
  766. if (err < 0)
  767. goto errout;
  768. err = -EINVAL;
  769. if (tb[TCA_EM_META_HDR] == NULL)
  770. goto errout;
  771. hdr = nla_data(tb[TCA_EM_META_HDR]);
  772. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  773. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  774. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  775. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  776. goto errout;
  777. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  778. if (meta == NULL) {
  779. err = -ENOMEM;
  780. goto errout;
  781. }
  782. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  783. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  784. if (!meta_is_supported(&meta->lvalue) ||
  785. !meta_is_supported(&meta->rvalue)) {
  786. err = -EOPNOTSUPP;
  787. goto errout;
  788. }
  789. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
  790. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
  791. goto errout;
  792. m->datalen = sizeof(*meta);
  793. m->data = (unsigned long) meta;
  794. err = 0;
  795. errout:
  796. if (err && meta)
  797. meta_delete(meta);
  798. return err;
  799. }
  800. static void em_meta_destroy(struct tcf_ematch *m)
  801. {
  802. if (m)
  803. meta_delete((struct meta_match *) m->data);
  804. }
  805. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  806. {
  807. struct meta_match *meta = (struct meta_match *) em->data;
  808. struct tcf_meta_hdr hdr;
  809. struct meta_type_ops *ops;
  810. memset(&hdr, 0, sizeof(hdr));
  811. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  812. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  813. if (nla_put(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr))
  814. goto nla_put_failure;
  815. ops = meta_type_ops(&meta->lvalue);
  816. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  817. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  818. goto nla_put_failure;
  819. return 0;
  820. nla_put_failure:
  821. return -1;
  822. }
  823. static struct tcf_ematch_ops em_meta_ops = {
  824. .kind = TCF_EM_META,
  825. .change = em_meta_change,
  826. .match = em_meta_match,
  827. .destroy = em_meta_destroy,
  828. .dump = em_meta_dump,
  829. .owner = THIS_MODULE,
  830. .link = LIST_HEAD_INIT(em_meta_ops.link)
  831. };
  832. static int __init init_em_meta(void)
  833. {
  834. return tcf_em_register(&em_meta_ops);
  835. }
  836. static void __exit exit_em_meta(void)
  837. {
  838. tcf_em_unregister(&em_meta_ops);
  839. }
  840. MODULE_LICENSE("GPL");
  841. module_init(init_em_meta);
  842. module_exit(exit_em_meta);
  843. MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);