em_meta.c 23 KB

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