em_meta.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * net/sched/em_meta.c Metadata ematch
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. *
  11. * ==========================================================================
  12. *
  13. * The metadata ematch compares two meta objects where each object
  14. * represents either a meta value stored in the kernel or a static
  15. * value provided by userspace. The objects are not provided by
  16. * userspace itself but rather a definition providing the information
  17. * to build them. Every object is of a certain type which must be
  18. * equal to the object it is being compared to.
  19. *
  20. * The definition of a objects conists of the type (meta type), a
  21. * identifier (meta id) and additional type specific information.
  22. * The meta id is either TCF_META_TYPE_VALUE for values provided by
  23. * userspace or a index to the meta operations table consisting of
  24. * function pointers to type specific meta data collectors returning
  25. * the value of the requested meta value.
  26. *
  27. * lvalue rvalue
  28. * +-----------+ +-----------+
  29. * | type: INT | | type: INT |
  30. * def | id: DEV | | id: VALUE |
  31. * | data: | | data: 3 |
  32. * +-----------+ +-----------+
  33. * | |
  34. * ---> meta_ops[INT][DEV](...) |
  35. * | |
  36. * ----------- |
  37. * V V
  38. * +-----------+ +-----------+
  39. * | type: INT | | type: INT |
  40. * obj | id: DEV | | id: VALUE |
  41. * | data: 2 |<--data got filled out | data: 3 |
  42. * +-----------+ +-----------+
  43. * | |
  44. * --------------> 2 equals 3 <--------------
  45. *
  46. * This is a simplified schema, the complexity varies depending
  47. * on the meta type. Obviously, the length of the data must also
  48. * be provided for non-numeric types.
  49. *
  50. * Additionaly, type dependant modifiers such as shift operators
  51. * or mask may be applied to extend the functionaliy. As of now,
  52. * the variable length type supports shifting the byte string to
  53. * the right, eating up any number of octets and thus supporting
  54. * wildcard interface name comparisons such as "ppp%" matching
  55. * ppp0..9.
  56. *
  57. * NOTE: Certain meta values depend on other subsystems and are
  58. * only available if that 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. {
  76. unsigned long value;
  77. unsigned int len;
  78. };
  79. struct meta_value
  80. {
  81. struct tcf_meta_val hdr;
  82. unsigned long val;
  83. unsigned int len;
  84. };
  85. struct meta_match
  86. {
  87. struct meta_value lvalue;
  88. struct meta_value rvalue;
  89. };
  90. static inline int meta_id(struct meta_value *v)
  91. {
  92. return TCF_META_ID(v->hdr.kind);
  93. }
  94. static inline int meta_type(struct meta_value *v)
  95. {
  96. return TCF_META_TYPE(v->hdr.kind);
  97. }
  98. #define META_COLLECTOR(FUNC) static void meta_##FUNC(struct sk_buff *skb, \
  99. struct tcf_pkt_info *info, struct meta_value *v, \
  100. struct meta_obj *dst, int *err)
  101. /**************************************************************************
  102. * System status & misc
  103. **************************************************************************/
  104. META_COLLECTOR(int_random)
  105. {
  106. get_random_bytes(&dst->value, sizeof(dst->value));
  107. }
  108. static inline unsigned long fixed_loadavg(int load)
  109. {
  110. int rnd_load = load + (FIXED_1/200);
  111. int rnd_frac = ((rnd_load & (FIXED_1-1)) * 100) >> FSHIFT;
  112. return ((rnd_load >> FSHIFT) * 100) + rnd_frac;
  113. }
  114. META_COLLECTOR(int_loadavg_0)
  115. {
  116. dst->value = fixed_loadavg(avenrun[0]);
  117. }
  118. META_COLLECTOR(int_loadavg_1)
  119. {
  120. dst->value = fixed_loadavg(avenrun[1]);
  121. }
  122. META_COLLECTOR(int_loadavg_2)
  123. {
  124. dst->value = fixed_loadavg(avenrun[2]);
  125. }
  126. /**************************************************************************
  127. * Device names & indices
  128. **************************************************************************/
  129. static inline int int_dev(struct net_device *dev, struct meta_obj *dst)
  130. {
  131. if (unlikely(dev == NULL))
  132. return -1;
  133. dst->value = dev->ifindex;
  134. return 0;
  135. }
  136. static inline int var_dev(struct net_device *dev, struct meta_obj *dst)
  137. {
  138. if (unlikely(dev == NULL))
  139. return -1;
  140. dst->value = (unsigned long) dev->name;
  141. dst->len = strlen(dev->name);
  142. return 0;
  143. }
  144. META_COLLECTOR(int_dev)
  145. {
  146. *err = int_dev(skb->dev, dst);
  147. }
  148. META_COLLECTOR(var_dev)
  149. {
  150. *err = var_dev(skb->dev, dst);
  151. }
  152. /**************************************************************************
  153. * vlan tag
  154. **************************************************************************/
  155. META_COLLECTOR(int_vlan_tag)
  156. {
  157. unsigned short tag;
  158. tag = vlan_tx_tag_get(skb);
  159. if (!tag && __vlan_get_tag(skb, &tag))
  160. *err = -1;
  161. else
  162. dst->value = tag;
  163. }
  164. /**************************************************************************
  165. * skb attributes
  166. **************************************************************************/
  167. META_COLLECTOR(int_priority)
  168. {
  169. dst->value = skb->priority;
  170. }
  171. META_COLLECTOR(int_protocol)
  172. {
  173. /* Let userspace take care of the byte ordering */
  174. dst->value = skb->protocol;
  175. }
  176. META_COLLECTOR(int_pkttype)
  177. {
  178. dst->value = skb->pkt_type;
  179. }
  180. META_COLLECTOR(int_pktlen)
  181. {
  182. dst->value = skb->len;
  183. }
  184. META_COLLECTOR(int_datalen)
  185. {
  186. dst->value = skb->data_len;
  187. }
  188. META_COLLECTOR(int_maclen)
  189. {
  190. dst->value = skb->mac_len;
  191. }
  192. /**************************************************************************
  193. * Netfilter
  194. **************************************************************************/
  195. META_COLLECTOR(int_mark)
  196. {
  197. dst->value = skb->mark;
  198. }
  199. /**************************************************************************
  200. * Traffic Control
  201. **************************************************************************/
  202. META_COLLECTOR(int_tcindex)
  203. {
  204. dst->value = skb->tc_index;
  205. }
  206. /**************************************************************************
  207. * Routing
  208. **************************************************************************/
  209. META_COLLECTOR(int_rtclassid)
  210. {
  211. if (unlikely(skb_dst(skb) == NULL))
  212. *err = -1;
  213. else
  214. #ifdef CONFIG_NET_CLS_ROUTE
  215. dst->value = skb_dst(skb)->tclassid;
  216. #else
  217. dst->value = 0;
  218. #endif
  219. }
  220. META_COLLECTOR(int_rtiif)
  221. {
  222. if (unlikely(skb_rtable(skb) == NULL))
  223. *err = -1;
  224. else
  225. dst->value = skb_rtable(skb)->fl.iif;
  226. }
  227. /**************************************************************************
  228. * Socket Attributes
  229. **************************************************************************/
  230. #define SKIP_NONLOCAL(skb) \
  231. if (unlikely(skb->sk == NULL)) { \
  232. *err = -1; \
  233. return; \
  234. }
  235. META_COLLECTOR(int_sk_family)
  236. {
  237. SKIP_NONLOCAL(skb);
  238. dst->value = skb->sk->sk_family;
  239. }
  240. META_COLLECTOR(int_sk_state)
  241. {
  242. SKIP_NONLOCAL(skb);
  243. dst->value = skb->sk->sk_state;
  244. }
  245. META_COLLECTOR(int_sk_reuse)
  246. {
  247. SKIP_NONLOCAL(skb);
  248. dst->value = skb->sk->sk_reuse;
  249. }
  250. META_COLLECTOR(int_sk_bound_if)
  251. {
  252. SKIP_NONLOCAL(skb);
  253. /* No error if bound_dev_if is 0, legal userspace check */
  254. dst->value = skb->sk->sk_bound_dev_if;
  255. }
  256. META_COLLECTOR(var_sk_bound_if)
  257. {
  258. SKIP_NONLOCAL(skb);
  259. if (skb->sk->sk_bound_dev_if == 0) {
  260. dst->value = (unsigned long) "any";
  261. dst->len = 3;
  262. } else {
  263. struct net_device *dev;
  264. rcu_read_lock();
  265. dev = dev_get_by_index_rcu(sock_net(skb->sk),
  266. skb->sk->sk_bound_dev_if);
  267. *err = var_dev(dev, dst);
  268. rcu_read_unlock();
  269. }
  270. }
  271. META_COLLECTOR(int_sk_refcnt)
  272. {
  273. SKIP_NONLOCAL(skb);
  274. dst->value = atomic_read(&skb->sk->sk_refcnt);
  275. }
  276. META_COLLECTOR(int_sk_rcvbuf)
  277. {
  278. SKIP_NONLOCAL(skb);
  279. dst->value = skb->sk->sk_rcvbuf;
  280. }
  281. META_COLLECTOR(int_sk_shutdown)
  282. {
  283. SKIP_NONLOCAL(skb);
  284. dst->value = skb->sk->sk_shutdown;
  285. }
  286. META_COLLECTOR(int_sk_proto)
  287. {
  288. SKIP_NONLOCAL(skb);
  289. dst->value = skb->sk->sk_protocol;
  290. }
  291. META_COLLECTOR(int_sk_type)
  292. {
  293. SKIP_NONLOCAL(skb);
  294. dst->value = skb->sk->sk_type;
  295. }
  296. META_COLLECTOR(int_sk_rmem_alloc)
  297. {
  298. SKIP_NONLOCAL(skb);
  299. dst->value = sk_rmem_alloc_get(skb->sk);
  300. }
  301. META_COLLECTOR(int_sk_wmem_alloc)
  302. {
  303. SKIP_NONLOCAL(skb);
  304. dst->value = sk_wmem_alloc_get(skb->sk);
  305. }
  306. META_COLLECTOR(int_sk_omem_alloc)
  307. {
  308. SKIP_NONLOCAL(skb);
  309. dst->value = atomic_read(&skb->sk->sk_omem_alloc);
  310. }
  311. META_COLLECTOR(int_sk_rcv_qlen)
  312. {
  313. SKIP_NONLOCAL(skb);
  314. dst->value = skb->sk->sk_receive_queue.qlen;
  315. }
  316. META_COLLECTOR(int_sk_snd_qlen)
  317. {
  318. SKIP_NONLOCAL(skb);
  319. dst->value = skb->sk->sk_write_queue.qlen;
  320. }
  321. META_COLLECTOR(int_sk_wmem_queued)
  322. {
  323. SKIP_NONLOCAL(skb);
  324. dst->value = skb->sk->sk_wmem_queued;
  325. }
  326. META_COLLECTOR(int_sk_fwd_alloc)
  327. {
  328. SKIP_NONLOCAL(skb);
  329. dst->value = skb->sk->sk_forward_alloc;
  330. }
  331. META_COLLECTOR(int_sk_sndbuf)
  332. {
  333. SKIP_NONLOCAL(skb);
  334. dst->value = skb->sk->sk_sndbuf;
  335. }
  336. META_COLLECTOR(int_sk_alloc)
  337. {
  338. SKIP_NONLOCAL(skb);
  339. dst->value = skb->sk->sk_allocation;
  340. }
  341. META_COLLECTOR(int_sk_route_caps)
  342. {
  343. SKIP_NONLOCAL(skb);
  344. dst->value = skb->sk->sk_route_caps;
  345. }
  346. META_COLLECTOR(int_sk_hash)
  347. {
  348. SKIP_NONLOCAL(skb);
  349. dst->value = skb->sk->sk_hash;
  350. }
  351. META_COLLECTOR(int_sk_lingertime)
  352. {
  353. SKIP_NONLOCAL(skb);
  354. dst->value = skb->sk->sk_lingertime / HZ;
  355. }
  356. META_COLLECTOR(int_sk_err_qlen)
  357. {
  358. SKIP_NONLOCAL(skb);
  359. dst->value = skb->sk->sk_error_queue.qlen;
  360. }
  361. META_COLLECTOR(int_sk_ack_bl)
  362. {
  363. SKIP_NONLOCAL(skb);
  364. dst->value = skb->sk->sk_ack_backlog;
  365. }
  366. META_COLLECTOR(int_sk_max_ack_bl)
  367. {
  368. SKIP_NONLOCAL(skb);
  369. dst->value = skb->sk->sk_max_ack_backlog;
  370. }
  371. META_COLLECTOR(int_sk_prio)
  372. {
  373. SKIP_NONLOCAL(skb);
  374. dst->value = skb->sk->sk_priority;
  375. }
  376. META_COLLECTOR(int_sk_rcvlowat)
  377. {
  378. SKIP_NONLOCAL(skb);
  379. dst->value = skb->sk->sk_rcvlowat;
  380. }
  381. META_COLLECTOR(int_sk_rcvtimeo)
  382. {
  383. SKIP_NONLOCAL(skb);
  384. dst->value = skb->sk->sk_rcvtimeo / HZ;
  385. }
  386. META_COLLECTOR(int_sk_sndtimeo)
  387. {
  388. SKIP_NONLOCAL(skb);
  389. dst->value = skb->sk->sk_sndtimeo / HZ;
  390. }
  391. META_COLLECTOR(int_sk_sendmsg_off)
  392. {
  393. SKIP_NONLOCAL(skb);
  394. dst->value = skb->sk->sk_sndmsg_off;
  395. }
  396. META_COLLECTOR(int_sk_write_pend)
  397. {
  398. SKIP_NONLOCAL(skb);
  399. dst->value = skb->sk->sk_write_pending;
  400. }
  401. /**************************************************************************
  402. * Meta value collectors assignment table
  403. **************************************************************************/
  404. struct meta_ops
  405. {
  406. void (*get)(struct sk_buff *, struct tcf_pkt_info *,
  407. struct meta_value *, struct meta_obj *, int *);
  408. };
  409. #define META_ID(name) TCF_META_ID_##name
  410. #define META_FUNC(name) { .get = meta_##name }
  411. /* Meta value operations table listing all meta value collectors and
  412. * assigns them to a type and meta id. */
  413. static struct meta_ops __meta_ops[TCF_META_TYPE_MAX+1][TCF_META_ID_MAX+1] = {
  414. [TCF_META_TYPE_VAR] = {
  415. [META_ID(DEV)] = META_FUNC(var_dev),
  416. [META_ID(SK_BOUND_IF)] = META_FUNC(var_sk_bound_if),
  417. },
  418. [TCF_META_TYPE_INT] = {
  419. [META_ID(RANDOM)] = META_FUNC(int_random),
  420. [META_ID(LOADAVG_0)] = META_FUNC(int_loadavg_0),
  421. [META_ID(LOADAVG_1)] = META_FUNC(int_loadavg_1),
  422. [META_ID(LOADAVG_2)] = META_FUNC(int_loadavg_2),
  423. [META_ID(DEV)] = META_FUNC(int_dev),
  424. [META_ID(PRIORITY)] = META_FUNC(int_priority),
  425. [META_ID(PROTOCOL)] = META_FUNC(int_protocol),
  426. [META_ID(PKTTYPE)] = META_FUNC(int_pkttype),
  427. [META_ID(PKTLEN)] = META_FUNC(int_pktlen),
  428. [META_ID(DATALEN)] = META_FUNC(int_datalen),
  429. [META_ID(MACLEN)] = META_FUNC(int_maclen),
  430. [META_ID(NFMARK)] = META_FUNC(int_mark),
  431. [META_ID(TCINDEX)] = META_FUNC(int_tcindex),
  432. [META_ID(RTCLASSID)] = META_FUNC(int_rtclassid),
  433. [META_ID(RTIIF)] = META_FUNC(int_rtiif),
  434. [META_ID(SK_FAMILY)] = META_FUNC(int_sk_family),
  435. [META_ID(SK_STATE)] = META_FUNC(int_sk_state),
  436. [META_ID(SK_REUSE)] = META_FUNC(int_sk_reuse),
  437. [META_ID(SK_BOUND_IF)] = META_FUNC(int_sk_bound_if),
  438. [META_ID(SK_REFCNT)] = META_FUNC(int_sk_refcnt),
  439. [META_ID(SK_RCVBUF)] = META_FUNC(int_sk_rcvbuf),
  440. [META_ID(SK_SNDBUF)] = META_FUNC(int_sk_sndbuf),
  441. [META_ID(SK_SHUTDOWN)] = META_FUNC(int_sk_shutdown),
  442. [META_ID(SK_PROTO)] = META_FUNC(int_sk_proto),
  443. [META_ID(SK_TYPE)] = META_FUNC(int_sk_type),
  444. [META_ID(SK_RMEM_ALLOC)] = META_FUNC(int_sk_rmem_alloc),
  445. [META_ID(SK_WMEM_ALLOC)] = META_FUNC(int_sk_wmem_alloc),
  446. [META_ID(SK_OMEM_ALLOC)] = META_FUNC(int_sk_omem_alloc),
  447. [META_ID(SK_WMEM_QUEUED)] = META_FUNC(int_sk_wmem_queued),
  448. [META_ID(SK_RCV_QLEN)] = META_FUNC(int_sk_rcv_qlen),
  449. [META_ID(SK_SND_QLEN)] = META_FUNC(int_sk_snd_qlen),
  450. [META_ID(SK_ERR_QLEN)] = META_FUNC(int_sk_err_qlen),
  451. [META_ID(SK_FORWARD_ALLOCS)] = META_FUNC(int_sk_fwd_alloc),
  452. [META_ID(SK_ALLOCS)] = META_FUNC(int_sk_alloc),
  453. [META_ID(SK_ROUTE_CAPS)] = META_FUNC(int_sk_route_caps),
  454. [META_ID(SK_HASH)] = META_FUNC(int_sk_hash),
  455. [META_ID(SK_LINGERTIME)] = META_FUNC(int_sk_lingertime),
  456. [META_ID(SK_ACK_BACKLOG)] = META_FUNC(int_sk_ack_bl),
  457. [META_ID(SK_MAX_ACK_BACKLOG)] = META_FUNC(int_sk_max_ack_bl),
  458. [META_ID(SK_PRIO)] = META_FUNC(int_sk_prio),
  459. [META_ID(SK_RCVLOWAT)] = META_FUNC(int_sk_rcvlowat),
  460. [META_ID(SK_RCVTIMEO)] = META_FUNC(int_sk_rcvtimeo),
  461. [META_ID(SK_SNDTIMEO)] = META_FUNC(int_sk_sndtimeo),
  462. [META_ID(SK_SENDMSG_OFF)] = META_FUNC(int_sk_sendmsg_off),
  463. [META_ID(SK_WRITE_PENDING)] = META_FUNC(int_sk_write_pend),
  464. [META_ID(VLAN_TAG)] = META_FUNC(int_vlan_tag),
  465. }
  466. };
  467. static inline struct meta_ops * meta_ops(struct meta_value *val)
  468. {
  469. return &__meta_ops[meta_type(val)][meta_id(val)];
  470. }
  471. /**************************************************************************
  472. * Type specific operations for TCF_META_TYPE_VAR
  473. **************************************************************************/
  474. static int meta_var_compare(struct meta_obj *a, struct meta_obj *b)
  475. {
  476. int r = a->len - b->len;
  477. if (r == 0)
  478. r = memcmp((void *) a->value, (void *) b->value, a->len);
  479. return r;
  480. }
  481. static int meta_var_change(struct meta_value *dst, struct nlattr *nla)
  482. {
  483. int len = nla_len(nla);
  484. dst->val = (unsigned long)kmemdup(nla_data(nla), len, GFP_KERNEL);
  485. if (dst->val == 0UL)
  486. return -ENOMEM;
  487. dst->len = len;
  488. return 0;
  489. }
  490. static void meta_var_destroy(struct meta_value *v)
  491. {
  492. kfree((void *) v->val);
  493. }
  494. static void meta_var_apply_extras(struct meta_value *v,
  495. struct meta_obj *dst)
  496. {
  497. int shift = v->hdr.shift;
  498. if (shift && shift < dst->len)
  499. dst->len -= shift;
  500. }
  501. static int meta_var_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  502. {
  503. if (v->val && v->len)
  504. NLA_PUT(skb, tlv, v->len, (void *) v->val);
  505. return 0;
  506. nla_put_failure:
  507. return -1;
  508. }
  509. /**************************************************************************
  510. * Type specific operations for TCF_META_TYPE_INT
  511. **************************************************************************/
  512. static int meta_int_compare(struct meta_obj *a, struct meta_obj *b)
  513. {
  514. /* Let gcc optimize it, the unlikely is not really based on
  515. * some numbers but jump free code for mismatches seems
  516. * more logical. */
  517. if (unlikely(a->value == b->value))
  518. return 0;
  519. else if (a->value < b->value)
  520. return -1;
  521. else
  522. return 1;
  523. }
  524. static int meta_int_change(struct meta_value *dst, struct nlattr *nla)
  525. {
  526. if (nla_len(nla) >= sizeof(unsigned long)) {
  527. dst->val = *(unsigned long *) nla_data(nla);
  528. dst->len = sizeof(unsigned long);
  529. } else if (nla_len(nla) == sizeof(u32)) {
  530. dst->val = nla_get_u32(nla);
  531. dst->len = sizeof(u32);
  532. } else
  533. return -EINVAL;
  534. return 0;
  535. }
  536. static void meta_int_apply_extras(struct meta_value *v,
  537. struct meta_obj *dst)
  538. {
  539. if (v->hdr.shift)
  540. dst->value >>= v->hdr.shift;
  541. if (v->val)
  542. dst->value &= v->val;
  543. }
  544. static int meta_int_dump(struct sk_buff *skb, struct meta_value *v, int tlv)
  545. {
  546. if (v->len == sizeof(unsigned long))
  547. NLA_PUT(skb, tlv, sizeof(unsigned long), &v->val);
  548. else if (v->len == sizeof(u32)) {
  549. NLA_PUT_U32(skb, tlv, v->val);
  550. }
  551. return 0;
  552. nla_put_failure:
  553. return -1;
  554. }
  555. /**************************************************************************
  556. * Type specific operations table
  557. **************************************************************************/
  558. struct meta_type_ops
  559. {
  560. void (*destroy)(struct meta_value *);
  561. int (*compare)(struct meta_obj *, struct meta_obj *);
  562. int (*change)(struct meta_value *, struct nlattr *);
  563. void (*apply_extras)(struct meta_value *, struct meta_obj *);
  564. int (*dump)(struct sk_buff *, struct meta_value *, int);
  565. };
  566. static struct meta_type_ops __meta_type_ops[TCF_META_TYPE_MAX+1] = {
  567. [TCF_META_TYPE_VAR] = {
  568. .destroy = meta_var_destroy,
  569. .compare = meta_var_compare,
  570. .change = meta_var_change,
  571. .apply_extras = meta_var_apply_extras,
  572. .dump = meta_var_dump
  573. },
  574. [TCF_META_TYPE_INT] = {
  575. .compare = meta_int_compare,
  576. .change = meta_int_change,
  577. .apply_extras = meta_int_apply_extras,
  578. .dump = meta_int_dump
  579. }
  580. };
  581. static inline struct meta_type_ops * meta_type_ops(struct meta_value *v)
  582. {
  583. return &__meta_type_ops[meta_type(v)];
  584. }
  585. /**************************************************************************
  586. * Core
  587. **************************************************************************/
  588. static int meta_get(struct sk_buff *skb, struct tcf_pkt_info *info,
  589. struct meta_value *v, struct meta_obj *dst)
  590. {
  591. int err = 0;
  592. if (meta_id(v) == TCF_META_ID_VALUE) {
  593. dst->value = v->val;
  594. dst->len = v->len;
  595. return 0;
  596. }
  597. meta_ops(v)->get(skb, info, v, dst, &err);
  598. if (err < 0)
  599. return err;
  600. if (meta_type_ops(v)->apply_extras)
  601. meta_type_ops(v)->apply_extras(v, dst);
  602. return 0;
  603. }
  604. static int em_meta_match(struct sk_buff *skb, struct tcf_ematch *m,
  605. struct tcf_pkt_info *info)
  606. {
  607. int r;
  608. struct meta_match *meta = (struct meta_match *) m->data;
  609. struct meta_obj l_value, r_value;
  610. if (meta_get(skb, info, &meta->lvalue, &l_value) < 0 ||
  611. meta_get(skb, info, &meta->rvalue, &r_value) < 0)
  612. return 0;
  613. r = meta_type_ops(&meta->lvalue)->compare(&l_value, &r_value);
  614. switch (meta->lvalue.hdr.op) {
  615. case TCF_EM_OPND_EQ:
  616. return !r;
  617. case TCF_EM_OPND_LT:
  618. return r < 0;
  619. case TCF_EM_OPND_GT:
  620. return r > 0;
  621. }
  622. return 0;
  623. }
  624. static void meta_delete(struct meta_match *meta)
  625. {
  626. if (meta) {
  627. struct meta_type_ops *ops = meta_type_ops(&meta->lvalue);
  628. if (ops && ops->destroy) {
  629. ops->destroy(&meta->lvalue);
  630. ops->destroy(&meta->rvalue);
  631. }
  632. }
  633. kfree(meta);
  634. }
  635. static inline int meta_change_data(struct meta_value *dst, struct nlattr *nla)
  636. {
  637. if (nla) {
  638. if (nla_len(nla) == 0)
  639. return -EINVAL;
  640. return meta_type_ops(dst)->change(dst, nla);
  641. }
  642. return 0;
  643. }
  644. static inline int meta_is_supported(struct meta_value *val)
  645. {
  646. return (!meta_id(val) || meta_ops(val)->get);
  647. }
  648. static const struct nla_policy meta_policy[TCA_EM_META_MAX + 1] = {
  649. [TCA_EM_META_HDR] = { .len = sizeof(struct tcf_meta_hdr) },
  650. };
  651. static int em_meta_change(struct tcf_proto *tp, void *data, int len,
  652. struct tcf_ematch *m)
  653. {
  654. int err;
  655. struct nlattr *tb[TCA_EM_META_MAX + 1];
  656. struct tcf_meta_hdr *hdr;
  657. struct meta_match *meta = NULL;
  658. err = nla_parse(tb, TCA_EM_META_MAX, data, len, meta_policy);
  659. if (err < 0)
  660. goto errout;
  661. err = -EINVAL;
  662. if (tb[TCA_EM_META_HDR] == NULL)
  663. goto errout;
  664. hdr = nla_data(tb[TCA_EM_META_HDR]);
  665. if (TCF_META_TYPE(hdr->left.kind) != TCF_META_TYPE(hdr->right.kind) ||
  666. TCF_META_TYPE(hdr->left.kind) > TCF_META_TYPE_MAX ||
  667. TCF_META_ID(hdr->left.kind) > TCF_META_ID_MAX ||
  668. TCF_META_ID(hdr->right.kind) > TCF_META_ID_MAX)
  669. goto errout;
  670. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  671. if (meta == NULL)
  672. goto errout;
  673. memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
  674. memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
  675. if (!meta_is_supported(&meta->lvalue) ||
  676. !meta_is_supported(&meta->rvalue)) {
  677. err = -EOPNOTSUPP;
  678. goto errout;
  679. }
  680. if (meta_change_data(&meta->lvalue, tb[TCA_EM_META_LVALUE]) < 0 ||
  681. meta_change_data(&meta->rvalue, tb[TCA_EM_META_RVALUE]) < 0)
  682. goto errout;
  683. m->datalen = sizeof(*meta);
  684. m->data = (unsigned long) meta;
  685. err = 0;
  686. errout:
  687. if (err && meta)
  688. meta_delete(meta);
  689. return err;
  690. }
  691. static void em_meta_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
  692. {
  693. if (m)
  694. meta_delete((struct meta_match *) m->data);
  695. }
  696. static int em_meta_dump(struct sk_buff *skb, struct tcf_ematch *em)
  697. {
  698. struct meta_match *meta = (struct meta_match *) em->data;
  699. struct tcf_meta_hdr hdr;
  700. struct meta_type_ops *ops;
  701. memset(&hdr, 0, sizeof(hdr));
  702. memcpy(&hdr.left, &meta->lvalue.hdr, sizeof(hdr.left));
  703. memcpy(&hdr.right, &meta->rvalue.hdr, sizeof(hdr.right));
  704. NLA_PUT(skb, TCA_EM_META_HDR, sizeof(hdr), &hdr);
  705. ops = meta_type_ops(&meta->lvalue);
  706. if (ops->dump(skb, &meta->lvalue, TCA_EM_META_LVALUE) < 0 ||
  707. ops->dump(skb, &meta->rvalue, TCA_EM_META_RVALUE) < 0)
  708. goto nla_put_failure;
  709. return 0;
  710. nla_put_failure:
  711. return -1;
  712. }
  713. static struct tcf_ematch_ops em_meta_ops = {
  714. .kind = TCF_EM_META,
  715. .change = em_meta_change,
  716. .match = em_meta_match,
  717. .destroy = em_meta_destroy,
  718. .dump = em_meta_dump,
  719. .owner = THIS_MODULE,
  720. .link = LIST_HEAD_INIT(em_meta_ops.link)
  721. };
  722. static int __init init_em_meta(void)
  723. {
  724. return tcf_em_register(&em_meta_ops);
  725. }
  726. static void __exit exit_em_meta(void)
  727. {
  728. tcf_em_unregister(&em_meta_ops);
  729. }
  730. MODULE_LICENSE("GPL");
  731. module_init(init_em_meta);
  732. module_exit(exit_em_meta);
  733. MODULE_ALIAS_TCF_EMATCH(TCF_EM_META);