em_meta.c 21 KB

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