PageRenderTime 30ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/net/sched/em_meta.c

https://github.com/mstsirkin/kvm
C | 872 lines | 621 code | 146 blank | 105 comment | 64 complexity | ff99480c299906a293c3f646d18dd1a9 MD5 | raw file
  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 = vlan_tx_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 = skb->protocol;
  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_rxhash(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 = skb_rtable(skb)->rt_iif;
  227. }
  228. /**************************************************************************
  229. * Socket Attributes
  230. **************************************************************************/
  231. #define SKIP_NONLOCAL(skb) \
  232. if (unlikely(skb->sk == NULL)) { \
  233. *err = -1; \
  234. return; \
  235. }
  236. META_COLLECTOR(int_sk_family)
  237. {
  238. SKIP_NONLOCAL(skb);
  239. dst->value = skb->sk->sk_family;
  240. }
  241. META_COLLECTOR(int_sk_state)
  242. {
  243. SKIP_NONLOCAL(skb);
  244. dst->value = skb->sk->sk_state;
  245. }
  246. META_COLLECTOR(int_sk_reuse)
  247. {
  248. SKIP_NONLOCAL(skb);
  249. dst->value = skb->sk->sk_reuse;
  250. }
  251. META_COLLECTOR(int_sk_bound_if)
  252. {
  253. SKIP_NONLOCAL(skb);
  254. /* No error if bound_dev_if is 0, legal userspace check */
  255. dst->value = skb->sk->sk_bound_dev_if;
  256. }
  257. META_COLLECTOR(var_sk_bound_if)
  258. {
  259. SKIP_NONLOCAL(skb);
  260. if (skb->sk->sk_bound_dev_if == 0) {
  261. dst->value = (unsigned long) "any";
  262. dst->len = 3;
  263. } else {
  264. struct net_device *dev;
  265. rcu_read_lock();
  266. dev = dev_get_by_index_rcu(sock_net(skb->sk),
  267. skb->sk->sk_bound_dev_if);
  268. *err = var_dev(dev, dst);
  269. rcu_read_unlock();
  270. }
  271. }
  272. META_COLLECTOR(int_sk_refcnt)
  273. {
  274. SKIP_NONLOCAL(skb);
  275. dst->value = atomic_read(&skb->sk->sk_refcnt);
  276. }
  277. META_COLLECTOR(int_sk_rcvbuf)
  278. {
  279. SKIP_NONLOCAL(skb);
  280. dst->value = skb->sk->sk_rcvbuf;
  281. }
  282. META_COLLECTOR(int_sk_shutdown)
  283. {
  284. SKIP_NONLOCAL(skb);
  285. dst->value = skb->sk->sk_shutdown;
  286. }
  287. META_COLLECTOR(int_sk_proto)
  288. {
  289. SKIP_NONLOCAL(skb);
  290. dst->value = skb->sk->sk_protocol;
  291. }
  292. META_COLLECTOR(int_sk_type)
  293. {
  294. SKIP_NONLOCAL(skb);
  295. dst->value = skb->sk->sk_type;
  296. }
  297. META_COLLECTOR(int_sk_rmem_alloc)
  298. {
  299. SKIP_NONLOCAL(skb);
  300. dst->value = sk_rmem_alloc_get(skb->sk);
  301. }
  302. META_COLLECTOR(int_sk_wmem_alloc)
  303. {
  304. SKIP_NONLOCAL(skb);
  305. dst->value = sk_wmem_alloc_get(skb->sk);
  306. }
  307. META_COLLECTOR(int_sk_omem_alloc)
  308. {
  309. SKIP_NONLOCAL(skb);
  310. dst->value = atomic_read(&skb->sk->sk_omem_alloc);
  311. }
  312. META_COLLECTOR(int_sk_rcv_qlen)
  313. {
  314. SKIP_NONLOCAL(skb);
  315. dst->value = skb->sk->sk_receive_queue.qlen;
  316. }
  317. META_COLLECTOR(int_sk_snd_qlen)
  318. {
  319. SKIP_NONLOCAL(skb);
  320. dst->value = skb->sk->sk_write_queue.qlen;
  321. }
  322. META_COLLECTOR(int_sk_wmem_queued)
  323. {
  324. SKIP_NONLOCAL(skb);
  325. dst->value = skb->sk->sk_wmem_queued;
  326. }
  327. META_COLLECTOR(int_sk_fwd_alloc)
  328. {
  329. SKIP_NONLOCAL(skb);
  330. dst->value = skb->sk->sk_forward_alloc;
  331. }
  332. META_COLLECTOR(int_sk_sndbuf)
  333. {
  334. SKIP_NONLOCAL(skb);
  335. dst->value = skb->sk->sk_sndbuf;
  336. }
  337. META_COLLECTOR(int_sk_alloc)
  338. {
  339. SKIP_NONLOCAL(skb);
  340. dst->value = (__force int) skb->sk->sk_allocation;
  341. }
  342. META_COLLECTOR(int_sk_hash)
  343. {
  344. SKIP_NONLOCAL(skb);
  345. dst->value = skb->sk->sk_hash;
  346. }
  347. META_COLLECTOR(int_sk_lingertime)
  348. {
  349. SKIP_NONLOCAL(skb);
  350. dst->value = skb->sk->sk_lingertime / HZ;
  351. }
  352. META_COLLECTOR(int_sk_err_qlen)
  353. {
  354. SKIP_NONLOCAL(skb);
  355. dst->value = skb->sk->sk_error_queue.qlen;
  356. }
  357. META_COLLECTOR(int_sk_ack_bl)
  358. {
  359. SKIP_NONLOCAL(skb);
  360. dst->value = skb->sk->sk_ack_backlog;
  361. }
  362. META_COLLECTOR(int_sk_max_ack_bl)
  363. {
  364. SKIP_NONLOCAL(skb);
  365. dst->value = skb->sk->sk_max_ack_backlog;
  366. }
  367. META_COLLECTOR(int_sk_prio)
  368. {
  369. SKIP_NONLOCAL(skb);
  370. dst->value = skb->sk->sk_priority;
  371. }
  372. META_COLLECTOR(int_sk_rcvlowat)
  373. {
  374. SKIP_NONLOCAL(skb);
  375. dst->value = skb->sk->sk_rcvlowat;
  376. }
  377. META_COLLECTOR(int_sk_rcvtimeo)
  378. {
  379. SKIP_NONLOCAL(skb);
  380. dst->value = skb->sk->sk_rcvtimeo / HZ;
  381. }
  382. META_COLLECTOR(int_sk_sndtimeo)
  383. {
  384. SKIP_NONLOCAL(skb);
  385. dst->value = skb->sk->sk_sndtimeo / HZ;
  386. }
  387. META_COLLECTOR(int_sk_sendmsg_off)
  388. {
  389. SKIP_NONLOCAL(skb);
  390. dst->value = skb->sk->sk_sndmsg_off;
  391. }
  392. META_COLLECTOR(int_sk_write_pend)
  393. {
  394. SKIP_NONLOCAL(skb);
  395. dst->value = skb->sk->sk_write_pending;
  396. }
  397. /**************************************************************************
  398. * Meta value collectors assignment table
  399. **************************************************************************/
  400. struct meta_ops {
  401. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  402. struct meta_value *, struct meta_obj *, int *);
  403. };
  404. #define META_ID(name) TCF_META_ID_##name
  405. #define META_FUNC(name) { .get = meta_##name }
  406. /* Meta value operations table listing all meta value collectors and
  407. * assigns them to a type and meta id. */
  408. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX + 1][TCF_META_ID_MAX + 1] = {
  409. [TCF_META_TYPE_VAR] = {
  410. [META_ID(DEV)] = META_FUNC(var_dev),
  411. [META_ID(SK_BOUND_IF)] = META_FUNC(var_sk_bound_if),
  412. },
  413. [TCF_META_TYPE_INT] = {
  414. [META_ID(RANDOM)] = META_FUNC(int_random),
  415. [META_ID(LOADAVG_0)] = META_FUNC(int_loadavg_0),
  416. [META_ID(LOADAVG_1)] = META_FUNC(int_loadavg_1),
  417. [META_ID(LOADAVG_2)] = META_FUNC(int_loadavg_2),
  418. [META_ID(DEV)] = META_FUNC(int_dev),
  419. [META_ID(PRIORITY)] = META_FUNC(int_priority),
  420. [META_ID(PROTOCOL)] = META_FUNC(int_protocol),
  421. [META_ID(PKTTYPE)] = META_FUNC(int_pkttype),
  422. [META_ID(PKTLEN)] = META_FUNC(int_pktlen),
  423. [META_ID(DATALEN)] = META_FUNC(int_datalen),
  424. [META_ID(MACLEN)] = META_FUNC(int_maclen),
  425. [META_ID(NFMARK)] = META_FUNC(int_mark),
  426. [META_ID(TCINDEX)] = META_FUNC(int_tcindex),
  427. [META_ID(RTCLASSID)] = META_FUNC(int_rtclassid),
  428. [META_ID(RTIIF)] = META_FUNC(int_rtiif),
  429. [META_ID(SK_FAMILY)] = META_FUNC(int_sk_family),
  430. [META_ID(SK_STATE)] = META_FUNC(int_sk_state),
  431. [META_ID(SK_REUSE)] = META_FUNC(int_sk_reuse),
  432. [META_ID(SK_BOUND_IF)] = META_FUNC(int_sk_bound_if),
  433. [META_ID(SK_REFCNT)] = META_FUNC(int_sk_refcnt),
  434. [META_ID(SK_RCVBUF)] = META_FUNC(int_sk_rcvbuf),
  435. [META_ID(SK_SNDBUF)] = META_FUNC(int_sk_sndbuf),
  436. [META_ID(SK_SHUTDOWN)] = META_FUNC(int_sk_shutdown),
  437. [META_ID(SK_PROTO)] = META_FUNC(int_sk_proto),
  438. [META_ID(SK_TYPE)] = META_FUNC(int_sk_type),
  439. [META_ID(SK_RMEM_ALLOC)] = META_FUNC(int_sk_rmem_alloc),
  440. [META_ID(SK_WMEM_ALLOC)] = META_FUNC(int_sk_wmem_alloc),
  441. [META_ID(SK_OMEM_ALLOC)] = META_FUNC(int_sk_omem_alloc),
  442. [META_ID(SK_WMEM_QUEUED)] = META_FUNC(int_sk_wmem_queued),
  443. [META_ID(SK_RCV_QLEN)] = META_FUNC(int_sk_rcv_qlen),
  444. [META_ID(SK_SND_QLEN)] = META_FUNC(int_sk_snd_qlen),
  445. [META_ID(SK_ERR_QLEN)] = META_FUNC(int_sk_err_qlen),
  446. [META_ID(SK_FORWARD_ALLOCS)] = META_FUNC(int_sk_fwd_alloc),
  447. [META_ID(SK_ALLOCS)] = META_FUNC(int_sk_alloc),
  448. [META_ID(SK_HASH)] = META_FUNC(int_sk_hash),
  449. [META_ID(SK_LINGERTIME)] = META_FUNC(int_sk_lingertime),
  450. [META_ID(SK_ACK_BACKLOG)] = META_FUNC(int_sk_ack_bl),
  451. [META_ID(SK_MAX_ACK_BACKLOG)] = META_FUNC(int_sk_max_ack_bl),
  452. [META_ID(SK_PRIO)] = META_FUNC(int_sk_prio),
  453. [META_ID(SK_RCVLOWAT)] = META_FUNC(int_sk_rcvlowat),
  454. [META_ID(SK_RCVTIMEO)] = META_FUNC(int_sk_rcvtimeo),
  455. [META_ID(SK_SNDTIMEO)] = META_FUNC(int_sk_sndtimeo),
  456. [META_ID(SK_SENDMSG_OFF)] = META_FUNC(int_sk_sendmsg_off),
  457. [META_ID(SK_WRITE_PENDING)] = META_FUNC(int_sk_write_pend),
  458. [META_ID(VLAN_TAG)] = META_FUNC(int_vlan_tag),
  459. [META_ID(RXHASH)] = META_FUNC(int_rxhash),
  460. }
  461. };
  462. static inline struct meta_ops *meta_ops(struct meta_value *val)
  463. {
  464. return &__meta_ops[meta_type(val)][meta_id(val)];
  465. }
  466. /**************************************************************************
  467. * Type specific operations for TCF_META_TYPE_VAR
  468. **************************************************************************/
  469. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  470. {
  471. int r = a->len - b->len;
  472. if (r == 0)
  473. r = memcmp((void *) a->value, (void *) b->value, a->len);
  474. return r;
  475. }
  476. static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
  477. {
  478. int len = nla_len(nla);
  479. dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
  480. if (dst->val == 0UL)
  481. return -ENOMEM;
  482. dst->len = len;
  483. return 0;
  484. }
  485. static void meta_var_destroy(struct meta_value *v)
  486. {
  487. kfree((void *) v->val);
  488. }
  489. static void meta_var_apply_extras(struct meta_value *v,
  490. struct meta_obj *dst)
  491. {
  492. int shift = v->hdr.shift;
  493. if (shift && shift < dst->len)
  494. dst->len -= shift;
  495. }
  496. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  497. {
  498. if (v->val && v->len)
  499. NLA_PUT(skb, tlv, v->len, (void *) v->val);
  500. return 0;
  501. nla_put_failure:
  502. return -1;
  503. }
  504. /**************************************************************************
  505. * Type specific operations for TCF_META_TYPE_INT
  506. **************************************************************************/
  507. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  508. {
  509. /* Let gcc optimize it, the unlikely is not really based on
  510. * some numbers but jump free code for mismatches seems
  511. * more logical. */
  512. if (unlikely(a->value == b->value))
  513. return 0;
  514. else if (a->value < b->value)
  515. return -1;
  516. else
  517. return 1;
  518. }
  519. static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
  520. {
  521. if (nla_len(nla) >= sizeof(unsigned long)) {
  522. dst->val = *(unsigned long *) nla_data(nla);
  523. dst->len = sizeof(unsigned long);
  524. } else if (nla_len(nla) == sizeof(u32)) {
  525. dst->val = nla_get_u32(nla);
  526. dst->len = sizeof(u32);
  527. } else
  528. return -EINVAL;
  529. return 0;
  530. }
  531. static void meta_int_apply_extras(struct meta_value *v,
  532. struct meta_obj *dst)
  533. {
  534. if (v->hdr.shift)
  535. dst->value >>= v->hdr.shift;
  536. if (v->val)
  537. dst->value &= v->val;
  538. }
  539. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  540. {
  541. if (v->len == sizeof(unsigned long))
  542. NLA_PUT(skb, tlv, sizeof(unsigned long), &v->val);
  543. else if (v->len == sizeof(u32))
  544. NLA_PUT_U32(skb, tlv, v->val);
  545. return 0;
  546. nla_put_failure:
  547. return -1;
  548. }
  549. /**************************************************************************
  550. * Type specific operations table
  551. **************************************************************************/
  552. struct meta_type_ops {
  553. void (*destroy)(struct meta_value *);
  554. int (*compare)(struct meta_obj *, struct meta_obj *);
  555. int (*change)(struct meta_value *, struct nlattr *);
  556. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  557. int (*dump)(struct sk_buff *, struct meta_value *, int);
  558. };
  559. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX + 1] = {
  560. [TCF_META_TYPE_VAR] = {
  561. .destroy = meta_var_destroy,
  562. .compare = meta_var_compare,
  563. .change = meta_var_change,
  564. .apply_extras = meta_var_apply_extras,
  565. .dump = meta_var_dump
  566. },
  567. [TCF_META_TYPE_INT] = {
  568. .compare = meta_int_compare,
  569. .change = meta_int_change,
  570. .apply_extras = meta_int_apply_extras,
  571. .dump = meta_int_dump
  572. }
  573. };
  574. static inline struct meta_type_ops *meta_type_ops(struct meta_value *v)
  575. {
  576. return &__meta_type_ops[meta_type(v)];
  577. }
  578. /**************************************************************************
  579. * Core
  580. **************************************************************************/
  581. static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  582. struct meta_value *v, struct meta_obj *dst)
  583. {
  584. int err = 0;
  585. if (meta_id(v) == TCF_META_ID_VALUE) {
  586. dst->value = v->val;
  587. dst->len = v->len;
  588. return 0;
  589. }
  590. meta_ops(v)->get(skb, info, v, dst, &err);
  591. if (err < 0)
  592. return err;
  593. if (meta_type_ops(v)->apply_extras)
  594. meta_type_ops(v)->apply_extras(v, dst);
  595. return 0;
  596. }
  597. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  598. struct tcf_pkt_info *info)
  599. {
  600. int r;
  601. struct meta_match *meta = (struct meta_match *) m->data;
  602. struct meta_obj l_value, r_value;
  603. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  604. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  605. return 0;
  606. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  607. switch (meta->lvalue.hdr.op) {
  608. case TCF_EM_OPND_EQ:
  609. return !r;
  610. case TCF_EM_OPND_LT:
  611. return r < 0;
  612. case TCF_EM_OPND_GT:
  613. return r > 0;
  614. }
  615. return 0;
  616. }
  617. static void meta_delete(struct meta_match *meta)
  618. {
  619. if (meta) {
  620. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  621. if (ops && ops->destroy) {
  622. ops->destroy(&meta->lvalue);
  623. ops->destroy(&meta->rvalue);
  624. }
  625. }
  626. kfree(meta);
  627. }
  628. static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
  629. {
  630. if (nla) {
  631. if (nla_len(nla) == 0)
  632. return -EINVAL;
  633. return meta_type_ops(dst)->change(dst, nla);
  634. }
  635. return 0;
  636. }
  637. static inline int meta_is_supported(struct meta_value *val)
  638. {
  639. return !meta_id(val) || meta_ops(val)->get;
  640. }
  641. static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
  642. [TCA_EM_META_HDR] = { .len = sizeof(struct tcf_meta_hdr) },
  643. };
  644. static int em_meta_change(struct tcf_proto *tp, void *data, int len,
  645. struct tcf_ematch *m)
  646. {
  647. int err;
  648. struct nlattr *tb[TCA_EM_META_MAX + 1];
  649. struct tcf_meta_hdr *hdr;
  650. struct meta_match *meta = NULL;
  651. err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy);
  652. if (err < 0)
  653. goto errout;
  654. err = -EINVAL;
  655. if (tb[TCA_EM_META_HDR] == NULL)
  656. goto errout;
  657. hdr = nla_data(tb[TCA_EM_META_HDR]);
  658. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  659. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  660. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  661. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  662. goto errout;
  663. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  664. if (meta == NULL)
  665. goto errout;
  666. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  667. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  668. if (!meta_is_supported(&meta->lvalue) ||
  669. !meta_is_supported(&meta->rvalue)) {
  670. err = -EOPNOTSUPP;
  671. goto errout;
  672. }
  673. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
  674. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
  675. goto errout;
  676. m->datalen = sizeof(*meta);
  677. m->data = (unsigned long) meta;
  678. err = 0;
  679. errout:
  680. if (err && meta)
  681. meta_delete(meta);
  682. return err;
  683. }
  684. static void em_meta_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  685. {
  686. if (m)
  687. meta_delete((struct meta_match *) m->data);
  688. }
  689. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  690. {
  691. struct meta_match *meta = (struct meta_match *) em->data;
  692. struct tcf_meta_hdr hdr;
  693. struct meta_type_ops *ops;
  694. memset(&hdr, 0, sizeof(hdr));
  695. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  696. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  697. NLA_PUT(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr);
  698. ops = meta_type_ops(&meta->lvalue);
  699. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  700. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  701. goto nla_put_failure;
  702. return 0;
  703. nla_put_failure:
  704. return -1;
  705. }
  706. static struct tcf_ematch_ops em_meta_ops = {
  707. .kind = TCF_EM_META,
  708. .change = em_meta_change,
  709. .match = em_meta_match,
  710. .destroy = em_meta_destroy,
  711. .dump = em_meta_dump,
  712. .owner = THIS_MODULE,
  713. .link = LIST_HEAD_INIT(em_meta_ops.link)
  714. };
  715. static int __init init_em_meta(void)
  716. {
  717. return tcf_em_register(&em_meta_ops);
  718. }
  719. static void __exit exit_em_meta(void)
  720. {
  721. tcf_em_unregister(&em_meta_ops);
  722. }
  723. MODULE_LICENSE("GPL");
  724. module_init(init_em_meta);
  725. module_exit(exit_em_meta);
  726. MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);