em_meta.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. if (skip_nonlocal(skb)) {
  295. *err = -1;
  296. return;
  297. }
  298. dst->value = skb->sk->sk_rcvbuf;
  299. }
  300. META_COLLECTOR(int_sk_shutdown)
  301. {
  302. if (skip_nonlocal(skb)) {
  303. *err = -1;
  304. return;
  305. }
  306. dst->value = skb->sk->sk_shutdown;
  307. }
  308. META_COLLECTOR(int_sk_proto)
  309. {
  310. if (skip_nonlocal(skb)) {
  311. *err = -1;
  312. return;
  313. }
  314. dst->value = skb->sk->sk_protocol;
  315. }
  316. META_COLLECTOR(int_sk_type)
  317. {
  318. if (skip_nonlocal(skb)) {
  319. *err = -1;
  320. return;
  321. }
  322. dst->value = skb->sk->sk_type;
  323. }
  324. META_COLLECTOR(int_sk_rmem_alloc)
  325. {
  326. if (skip_nonlocal(skb)) {
  327. *err = -1;
  328. return;
  329. }
  330. dst->value = sk_rmem_alloc_get(skb->sk);
  331. }
  332. META_COLLECTOR(int_sk_wmem_alloc)
  333. {
  334. if (skip_nonlocal(skb)) {
  335. *err = -1;
  336. return;
  337. }
  338. dst->value = sk_wmem_alloc_get(skb->sk);
  339. }
  340. META_COLLECTOR(int_sk_omem_alloc)
  341. {
  342. if (skip_nonlocal(skb)) {
  343. *err = -1;
  344. return;
  345. }
  346. dst->value = atomic_read(&skb->sk->sk_omem_alloc);
  347. }
  348. META_COLLECTOR(int_sk_rcv_qlen)
  349. {
  350. if (skip_nonlocal(skb)) {
  351. *err = -1;
  352. return;
  353. }
  354. dst->value = skb->sk->sk_receive_queue.qlen;
  355. }
  356. META_COLLECTOR(int_sk_snd_qlen)
  357. {
  358. if (skip_nonlocal(skb)) {
  359. *err = -1;
  360. return;
  361. }
  362. dst->value = skb->sk->sk_write_queue.qlen;
  363. }
  364. META_COLLECTOR(int_sk_wmem_queued)
  365. {
  366. if (skip_nonlocal(skb)) {
  367. *err = -1;
  368. return;
  369. }
  370. dst->value = skb->sk->sk_wmem_queued;
  371. }
  372. META_COLLECTOR(int_sk_fwd_alloc)
  373. {
  374. if (skip_nonlocal(skb)) {
  375. *err = -1;
  376. return;
  377. }
  378. dst->value = skb->sk->sk_forward_alloc;
  379. }
  380. META_COLLECTOR(int_sk_sndbuf)
  381. {
  382. if (skip_nonlocal(skb)) {
  383. *err = -1;
  384. return;
  385. }
  386. dst->value = skb->sk->sk_sndbuf;
  387. }
  388. META_COLLECTOR(int_sk_alloc)
  389. {
  390. if (skip_nonlocal(skb)) {
  391. *err = -1;
  392. return;
  393. }
  394. dst->value = (__force int) skb->sk->sk_allocation;
  395. }
  396. META_COLLECTOR(int_sk_hash)
  397. {
  398. if (skip_nonlocal(skb)) {
  399. *err = -1;
  400. return;
  401. }
  402. dst->value = skb->sk->sk_hash;
  403. }
  404. META_COLLECTOR(int_sk_lingertime)
  405. {
  406. if (skip_nonlocal(skb)) {
  407. *err = -1;
  408. return;
  409. }
  410. dst->value = skb->sk->sk_lingertime / HZ;
  411. }
  412. META_COLLECTOR(int_sk_err_qlen)
  413. {
  414. if (skip_nonlocal(skb)) {
  415. *err = -1;
  416. return;
  417. }
  418. dst->value = skb->sk->sk_error_queue.qlen;
  419. }
  420. META_COLLECTOR(int_sk_ack_bl)
  421. {
  422. if (skip_nonlocal(skb)) {
  423. *err = -1;
  424. return;
  425. }
  426. dst->value = skb->sk->sk_ack_backlog;
  427. }
  428. META_COLLECTOR(int_sk_max_ack_bl)
  429. {
  430. if (skip_nonlocal(skb)) {
  431. *err = -1;
  432. return;
  433. }
  434. dst->value = skb->sk->sk_max_ack_backlog;
  435. }
  436. META_COLLECTOR(int_sk_prio)
  437. {
  438. if (skip_nonlocal(skb)) {
  439. *err = -1;
  440. return;
  441. }
  442. dst->value = skb->sk->sk_priority;
  443. }
  444. META_COLLECTOR(int_sk_rcvlowat)
  445. {
  446. if (skip_nonlocal(skb)) {
  447. *err = -1;
  448. return;
  449. }
  450. dst->value = skb->sk->sk_rcvlowat;
  451. }
  452. META_COLLECTOR(int_sk_rcvtimeo)
  453. {
  454. if (skip_nonlocal(skb)) {
  455. *err = -1;
  456. return;
  457. }
  458. dst->value = skb->sk->sk_rcvtimeo / HZ;
  459. }
  460. META_COLLECTOR(int_sk_sndtimeo)
  461. {
  462. if (skip_nonlocal(skb)) {
  463. *err = -1;
  464. return;
  465. }
  466. dst->value = skb->sk->sk_sndtimeo / HZ;
  467. }
  468. META_COLLECTOR(int_sk_sendmsg_off)
  469. {
  470. if (skip_nonlocal(skb)) {
  471. *err = -1;
  472. return;
  473. }
  474. dst->value = skb->sk->sk_frag.offset;
  475. }
  476. META_COLLECTOR(int_sk_write_pend)
  477. {
  478. if (skip_nonlocal(skb)) {
  479. *err = -1;
  480. return;
  481. }
  482. dst->value = skb->sk->sk_write_pending;
  483. }
  484. /**************************************************************************
  485. * Meta value collectors assignment table
  486. **************************************************************************/
  487. struct meta_ops {
  488. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  489. struct meta_value *, struct meta_obj *, int *);
  490. };
  491. #define META_ID(name) TCF_META_ID_##name
  492. #define META_FUNC(name) { .get = meta_##name }
  493. /* Meta value operations table listing all meta value collectors and
  494. * assigns them to a type and meta id. */
  495. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX + 1][TCF_META_ID_MAX + 1] = {
  496. [TCF_META_TYPE_VAR] = {
  497. [META_ID(DEV)] = META_FUNC(var_dev),
  498. [META_ID(SK_BOUND_IF)] = META_FUNC(var_sk_bound_if),
  499. },
  500. [TCF_META_TYPE_INT] = {
  501. [META_ID(RANDOM)] = META_FUNC(int_random),
  502. [META_ID(LOADAVG_0)] = META_FUNC(int_loadavg_0),
  503. [META_ID(LOADAVG_1)] = META_FUNC(int_loadavg_1),
  504. [META_ID(LOADAVG_2)] = META_FUNC(int_loadavg_2),
  505. [META_ID(DEV)] = META_FUNC(int_dev),
  506. [META_ID(PRIORITY)] = META_FUNC(int_priority),
  507. [META_ID(PROTOCOL)] = META_FUNC(int_protocol),
  508. [META_ID(PKTTYPE)] = META_FUNC(int_pkttype),
  509. [META_ID(PKTLEN)] = META_FUNC(int_pktlen),
  510. [META_ID(DATALEN)] = META_FUNC(int_datalen),
  511. [META_ID(MACLEN)] = META_FUNC(int_maclen),
  512. [META_ID(NFMARK)] = META_FUNC(int_mark),
  513. [META_ID(TCINDEX)] = META_FUNC(int_tcindex),
  514. [META_ID(RTCLASSID)] = META_FUNC(int_rtclassid),
  515. [META_ID(RTIIF)] = META_FUNC(int_rtiif),
  516. [META_ID(SK_FAMILY)] = META_FUNC(int_sk_family),
  517. [META_ID(SK_STATE)] = META_FUNC(int_sk_state),
  518. [META_ID(SK_REUSE)] = META_FUNC(int_sk_reuse),
  519. [META_ID(SK_BOUND_IF)] = META_FUNC(int_sk_bound_if),
  520. [META_ID(SK_REFCNT)] = META_FUNC(int_sk_refcnt),
  521. [META_ID(SK_RCVBUF)] = META_FUNC(int_sk_rcvbuf),
  522. [META_ID(SK_SNDBUF)] = META_FUNC(int_sk_sndbuf),
  523. [META_ID(SK_SHUTDOWN)] = META_FUNC(int_sk_shutdown),
  524. [META_ID(SK_PROTO)] = META_FUNC(int_sk_proto),
  525. [META_ID(SK_TYPE)] = META_FUNC(int_sk_type),
  526. [META_ID(SK_RMEM_ALLOC)] = META_FUNC(int_sk_rmem_alloc),
  527. [META_ID(SK_WMEM_ALLOC)] = META_FUNC(int_sk_wmem_alloc),
  528. [META_ID(SK_OMEM_ALLOC)] = META_FUNC(int_sk_omem_alloc),
  529. [META_ID(SK_WMEM_QUEUED)] = META_FUNC(int_sk_wmem_queued),
  530. [META_ID(SK_RCV_QLEN)] = META_FUNC(int_sk_rcv_qlen),
  531. [META_ID(SK_SND_QLEN)] = META_FUNC(int_sk_snd_qlen),
  532. [META_ID(SK_ERR_QLEN)] = META_FUNC(int_sk_err_qlen),
  533. [META_ID(SK_FORWARD_ALLOCS)] = META_FUNC(int_sk_fwd_alloc),
  534. [META_ID(SK_ALLOCS)] = META_FUNC(int_sk_alloc),
  535. [META_ID(SK_HASH)] = META_FUNC(int_sk_hash),
  536. [META_ID(SK_LINGERTIME)] = META_FUNC(int_sk_lingertime),
  537. [META_ID(SK_ACK_BACKLOG)] = META_FUNC(int_sk_ack_bl),
  538. [META_ID(SK_MAX_ACK_BACKLOG)] = META_FUNC(int_sk_max_ack_bl),
  539. [META_ID(SK_PRIO)] = META_FUNC(int_sk_prio),
  540. [META_ID(SK_RCVLOWAT)] = META_FUNC(int_sk_rcvlowat),
  541. [META_ID(SK_RCVTIMEO)] = META_FUNC(int_sk_rcvtimeo),
  542. [META_ID(SK_SNDTIMEO)] = META_FUNC(int_sk_sndtimeo),
  543. [META_ID(SK_SENDMSG_OFF)] = META_FUNC(int_sk_sendmsg_off),
  544. [META_ID(SK_WRITE_PENDING)] = META_FUNC(int_sk_write_pend),
  545. [META_ID(VLAN_TAG)] = META_FUNC(int_vlan_tag),
  546. [META_ID(RXHASH)] = META_FUNC(int_rxhash),
  547. }
  548. };
  549. static inline struct meta_ops *meta_ops(struct meta_value *val)
  550. {
  551. return &__meta_ops[meta_type(val)][meta_id(val)];
  552. }
  553. /**************************************************************************
  554. * Type specific operations for TCF_META_TYPE_VAR
  555. **************************************************************************/
  556. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  557. {
  558. int r = a->len - b->len;
  559. if (r == 0)
  560. r = memcmp((void *) a->value, (void *) b->value, a->len);
  561. return r;
  562. }
  563. static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
  564. {
  565. int len = nla_len(nla);
  566. dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
  567. if (dst->val == 0UL)
  568. return -ENOMEM;
  569. dst->len = len;
  570. return 0;
  571. }
  572. static void meta_var_destroy(struct meta_value *v)
  573. {
  574. kfree((void *) v->val);
  575. }
  576. static void meta_var_apply_extras(struct meta_value *v,
  577. struct meta_obj *dst)
  578. {
  579. int shift = v->hdr.shift;
  580. if (shift && shift < dst->len)
  581. dst->len -= shift;
  582. }
  583. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  584. {
  585. if (v->val && v->len &&
  586. nla_put(skb, tlv, v->len, (void *) v->val))
  587. goto nla_put_failure;
  588. return 0;
  589. nla_put_failure:
  590. return -1;
  591. }
  592. /**************************************************************************
  593. * Type specific operations for TCF_META_TYPE_INT
  594. **************************************************************************/
  595. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  596. {
  597. /* Let gcc optimize it, the unlikely is not really based on
  598. * some numbers but jump free code for mismatches seems
  599. * more logical. */
  600. if (unlikely(a->value == b->value))
  601. return 0;
  602. else if (a->value < b->value)
  603. return -1;
  604. else
  605. return 1;
  606. }
  607. static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
  608. {
  609. if (nla_len(nla) >= sizeof(unsigned long)) {
  610. dst->val = *(unsigned long *) nla_data(nla);
  611. dst->len = sizeof(unsigned long);
  612. } else if (nla_len(nla) == sizeof(u32)) {
  613. dst->val = nla_get_u32(nla);
  614. dst->len = sizeof(u32);
  615. } else
  616. return -EINVAL;
  617. return 0;
  618. }
  619. static void meta_int_apply_extras(struct meta_value *v,
  620. struct meta_obj *dst)
  621. {
  622. if (v->hdr.shift)
  623. dst->value >>= v->hdr.shift;
  624. if (v->val)
  625. dst->value &= v->val;
  626. }
  627. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  628. {
  629. if (v->len == sizeof(unsigned long)) {
  630. if (nla_put(skb, tlv, sizeof(unsigned long), &v->val))
  631. goto nla_put_failure;
  632. } else if (v->len == sizeof(u32)) {
  633. if (nla_put_u32(skb, tlv, v->val))
  634. goto nla_put_failure;
  635. }
  636. return 0;
  637. nla_put_failure:
  638. return -1;
  639. }
  640. /**************************************************************************
  641. * Type specific operations table
  642. **************************************************************************/
  643. struct meta_type_ops {
  644. void (*destroy)(struct meta_value *);
  645. int (*compare)(struct meta_obj *, struct meta_obj *);
  646. int (*change)(struct meta_value *, struct nlattr *);
  647. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  648. int (*dump)(struct sk_buff *, struct meta_value *, int);
  649. };
  650. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX + 1] = {
  651. [TCF_META_TYPE_VAR] = {
  652. .destroy = meta_var_destroy,
  653. .compare = meta_var_compare,
  654. .change = meta_var_change,
  655. .apply_extras = meta_var_apply_extras,
  656. .dump = meta_var_dump
  657. },
  658. [TCF_META_TYPE_INT] = {
  659. .compare = meta_int_compare,
  660. .change = meta_int_change,
  661. .apply_extras = meta_int_apply_extras,
  662. .dump = meta_int_dump
  663. }
  664. };
  665. static inline struct meta_type_ops *meta_type_ops(struct meta_value *v)
  666. {
  667. return &__meta_type_ops[meta_type(v)];
  668. }
  669. /**************************************************************************
  670. * Core
  671. **************************************************************************/
  672. static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  673. struct meta_value *v, struct meta_obj *dst)
  674. {
  675. int err = 0;
  676. if (meta_id(v) == TCF_META_ID_VALUE) {
  677. dst->value = v->val;
  678. dst->len = v->len;
  679. return 0;
  680. }
  681. meta_ops(v)->get(skb, info, v, dst, &err);
  682. if (err < 0)
  683. return err;
  684. if (meta_type_ops(v)->apply_extras)
  685. meta_type_ops(v)->apply_extras(v, dst);
  686. return 0;
  687. }
  688. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  689. struct tcf_pkt_info *info)
  690. {
  691. int r;
  692. struct meta_match *meta = (struct meta_match *) m->data;
  693. struct meta_obj l_value, r_value;
  694. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  695. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  696. return 0;
  697. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  698. switch (meta->lvalue.hdr.op) {
  699. case TCF_EM_OPND_EQ:
  700. return !r;
  701. case TCF_EM_OPND_LT:
  702. return r < 0;
  703. case TCF_EM_OPND_GT:
  704. return r > 0;
  705. }
  706. return 0;
  707. }
  708. static void meta_delete(struct meta_match *meta)
  709. {
  710. if (meta) {
  711. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  712. if (ops && ops->destroy) {
  713. ops->destroy(&meta->lvalue);
  714. ops->destroy(&meta->rvalue);
  715. }
  716. }
  717. kfree(meta);
  718. }
  719. static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
  720. {
  721. if (nla) {
  722. if (nla_len(nla) == 0)
  723. return -EINVAL;
  724. return meta_type_ops(dst)->change(dst, nla);
  725. }
  726. return 0;
  727. }
  728. static inline int meta_is_supported(struct meta_value *val)
  729. {
  730. return !meta_id(val) || meta_ops(val)->get;
  731. }
  732. static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
  733. [TCA_EM_META_HDR] = { .len = sizeof(struct tcf_meta_hdr) },
  734. };
  735. static int em_meta_change(struct net *net, void *data, int len,
  736. struct tcf_ematch *m)
  737. {
  738. int err;
  739. struct nlattr *tb[TCA_EM_META_MAX + 1];
  740. struct tcf_meta_hdr *hdr;
  741. struct meta_match *meta = NULL;
  742. err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy);
  743. if (err < 0)
  744. goto errout;
  745. err = -EINVAL;
  746. if (tb[TCA_EM_META_HDR] == NULL)
  747. goto errout;
  748. hdr = nla_data(tb[TCA_EM_META_HDR]);
  749. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  750. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  751. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  752. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  753. goto errout;
  754. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  755. if (meta == NULL) {
  756. err = -ENOMEM;
  757. goto errout;
  758. }
  759. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  760. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  761. if (!meta_is_supported(&meta->lvalue) ||
  762. !meta_is_supported(&meta->rvalue)) {
  763. err = -EOPNOTSUPP;
  764. goto errout;
  765. }
  766. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
  767. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
  768. goto errout;
  769. m->datalen = sizeof(*meta);
  770. m->data = (unsigned long) meta;
  771. err = 0;
  772. errout:
  773. if (err && meta)
  774. meta_delete(meta);
  775. return err;
  776. }
  777. static void em_meta_destroy(struct tcf_ematch *m)
  778. {
  779. if (m)
  780. meta_delete((struct meta_match *) m->data);
  781. }
  782. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  783. {
  784. struct meta_match *meta = (struct meta_match *) em->data;
  785. struct tcf_meta_hdr hdr;
  786. struct meta_type_ops *ops;
  787. memset(&hdr, 0, sizeof(hdr));
  788. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  789. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  790. if (nla_put(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr))
  791. goto nla_put_failure;
  792. ops = meta_type_ops(&meta->lvalue);
  793. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  794. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  795. goto nla_put_failure;
  796. return 0;
  797. nla_put_failure:
  798. return -1;
  799. }
  800. static struct tcf_ematch_ops em_meta_ops = {
  801. .kind = TCF_EM_META,
  802. .change = em_meta_change,
  803. .match = em_meta_match,
  804. .destroy = em_meta_destroy,
  805. .dump = em_meta_dump,
  806. .owner = THIS_MODULE,
  807. .link = LIST_HEAD_INIT(em_meta_ops.link)
  808. };
  809. static int __init init_em_meta(void)
  810. {
  811. return tcf_em_register(&em_meta_ops);
  812. }
  813. static void __exit exit_em_meta(void)
  814. {
  815. tcf_em_unregister(&em_meta_ops);
  816. }
  817. MODULE_LICENSE("GPL");
  818. module_init(init_em_meta);
  819. module_exit(exit_em_meta);
  820. MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);