pkt_cls.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #ifndef __NET_PKT_CLS_H
  2. #define __NET_PKT_CLS_H
  3. #include <linux/pkt_cls.h>
  4. #include <net/sch_generic.h>
  5. #include <net/act_api.h>
  6. /* Basic packet classifier frontend definitions. */
  7. struct tcf_walker
  8. {
  9. int stop;
  10. int skip;
  11. int count;
  12. int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
  13. };
  14. extern int register_tcf_proto_ops(struct tcf_proto_ops *ops);
  15. extern int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
  16. static inline unsigned long
  17. __cls_set_class(unsigned long *clp, unsigned long cl)
  18. {
  19. unsigned long old_cl;
  20. old_cl = *clp;
  21. *clp = cl;
  22. return old_cl;
  23. }
  24. static inline unsigned long
  25. cls_set_class(struct tcf_proto *tp, unsigned long *clp,
  26. unsigned long cl)
  27. {
  28. unsigned long old_cl;
  29. tcf_tree_lock(tp);
  30. old_cl = __cls_set_class(clp, cl);
  31. tcf_tree_unlock(tp);
  32. return old_cl;
  33. }
  34. static inline void
  35. tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
  36. {
  37. unsigned long cl;
  38. cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
  39. cl = cls_set_class(tp, &r->class, cl);
  40. if (cl)
  41. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  42. }
  43. static inline void
  44. tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
  45. {
  46. unsigned long cl;
  47. if ((cl = __cls_set_class(&r->class, 0)) != 0)
  48. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  49. }
  50. struct tcf_exts
  51. {
  52. #ifdef CONFIG_NET_CLS_ACT
  53. struct tc_action *action;
  54. #elif defined CONFIG_NET_CLS_POLICE
  55. struct tcf_police *police;
  56. #endif
  57. };
  58. /* Map to export classifier specific extension TLV types to the
  59. * generic extensions API. Unsupported extensions must be set to 0.
  60. */
  61. struct tcf_ext_map
  62. {
  63. int action;
  64. int police;
  65. };
  66. /**
  67. * tcf_exts_is_predicative - check if a predicative extension is present
  68. * @exts: tc filter extensions handle
  69. *
  70. * Returns 1 if a predicative extension is present, i.e. an extension which
  71. * might cause further actions and thus overrule the regular tcf_result.
  72. */
  73. static inline int
  74. tcf_exts_is_predicative(struct tcf_exts *exts)
  75. {
  76. #ifdef CONFIG_NET_CLS_ACT
  77. return !!exts->action;
  78. #elif defined CONFIG_NET_CLS_POLICE
  79. return !!exts->police;
  80. #else
  81. return 0;
  82. #endif
  83. }
  84. /**
  85. * tcf_exts_is_available - check if at least one extension is present
  86. * @exts: tc filter extensions handle
  87. *
  88. * Returns 1 if at least one extension is present.
  89. */
  90. static inline int
  91. tcf_exts_is_available(struct tcf_exts *exts)
  92. {
  93. /* All non-predicative extensions must be added here. */
  94. return tcf_exts_is_predicative(exts);
  95. }
  96. /**
  97. * tcf_exts_exec - execute tc filter extensions
  98. * @skb: socket buffer
  99. * @exts: tc filter extensions handle
  100. * @res: desired result
  101. *
  102. * Executes all configured extensions. Returns 0 on a normal execution,
  103. * a negative number if the filter must be considered unmatched or
  104. * a positive action code (TC_ACT_*) which must be returned to the
  105. * underlying layer.
  106. */
  107. static inline int
  108. tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
  109. struct tcf_result *res)
  110. {
  111. #ifdef CONFIG_NET_CLS_ACT
  112. if (exts->action)
  113. return tcf_action_exec(skb, exts->action, res);
  114. #elif defined CONFIG_NET_CLS_POLICE
  115. if (exts->police)
  116. return tcf_police(skb, exts->police);
  117. #endif
  118. return 0;
  119. }
  120. extern int tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
  121. struct rtattr *rate_tlv, struct tcf_exts *exts,
  122. struct tcf_ext_map *map);
  123. extern void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts);
  124. extern void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
  125. struct tcf_exts *src);
  126. extern int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
  127. struct tcf_ext_map *map);
  128. extern int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
  129. struct tcf_ext_map *map);
  130. /**
  131. * struct tcf_pkt_info - packet information
  132. */
  133. struct tcf_pkt_info
  134. {
  135. unsigned char * ptr;
  136. int nexthdr;
  137. };
  138. #ifdef CONFIG_NET_EMATCH
  139. struct tcf_ematch_ops;
  140. /**
  141. * struct tcf_ematch - extended match (ematch)
  142. *
  143. * @matchid: identifier to allow userspace to reidentify a match
  144. * @flags: flags specifying attributes and the relation to other matches
  145. * @ops: the operations lookup table of the corresponding ematch module
  146. * @datalen: length of the ematch specific configuration data
  147. * @data: ematch specific data
  148. */
  149. struct tcf_ematch
  150. {
  151. struct tcf_ematch_ops * ops;
  152. unsigned long data;
  153. unsigned int datalen;
  154. u16 matchid;
  155. u16 flags;
  156. };
  157. static inline int tcf_em_is_container(struct tcf_ematch *em)
  158. {
  159. return !em->ops;
  160. }
  161. static inline int tcf_em_is_simple(struct tcf_ematch *em)
  162. {
  163. return em->flags & TCF_EM_SIMPLE;
  164. }
  165. static inline int tcf_em_is_inverted(struct tcf_ematch *em)
  166. {
  167. return em->flags & TCF_EM_INVERT;
  168. }
  169. static inline int tcf_em_last_match(struct tcf_ematch *em)
  170. {
  171. return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
  172. }
  173. static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
  174. {
  175. if (tcf_em_last_match(em))
  176. return 1;
  177. if (result == 0 && em->flags & TCF_EM_REL_AND)
  178. return 1;
  179. if (result != 0 && em->flags & TCF_EM_REL_OR)
  180. return 1;
  181. return 0;
  182. }
  183. /**
  184. * struct tcf_ematch_tree - ematch tree handle
  185. *
  186. * @hdr: ematch tree header supplied by userspace
  187. * @matches: array of ematches
  188. */
  189. struct tcf_ematch_tree
  190. {
  191. struct tcf_ematch_tree_hdr hdr;
  192. struct tcf_ematch * matches;
  193. };
  194. /**
  195. * struct tcf_ematch_ops - ematch module operations
  196. *
  197. * @kind: identifier (kind) of this ematch module
  198. * @datalen: length of expected configuration data (optional)
  199. * @change: called during validation (optional)
  200. * @match: called during ematch tree evaluation, must return 1/0
  201. * @destroy: called during destroyage (optional)
  202. * @dump: called during dumping process (optional)
  203. * @owner: owner, must be set to THIS_MODULE
  204. * @link: link to previous/next ematch module (internal use)
  205. */
  206. struct tcf_ematch_ops
  207. {
  208. int kind;
  209. int datalen;
  210. int (*change)(struct tcf_proto *, void *,
  211. int, struct tcf_ematch *);
  212. int (*match)(struct sk_buff *, struct tcf_ematch *,
  213. struct tcf_pkt_info *);
  214. void (*destroy)(struct tcf_proto *,
  215. struct tcf_ematch *);
  216. int (*dump)(struct sk_buff *, struct tcf_ematch *);
  217. struct module *owner;
  218. struct list_head link;
  219. };
  220. extern int tcf_em_register(struct tcf_ematch_ops *);
  221. extern int tcf_em_unregister(struct tcf_ematch_ops *);
  222. extern int tcf_em_tree_validate(struct tcf_proto *, struct rtattr *,
  223. struct tcf_ematch_tree *);
  224. extern void tcf_em_tree_destroy(struct tcf_proto *, struct tcf_ematch_tree *);
  225. extern int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
  226. extern int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
  227. struct tcf_pkt_info *);
  228. /**
  229. * tcf_em_tree_change - replace ematch tree of a running classifier
  230. *
  231. * @tp: classifier kind handle
  232. * @dst: destination ematch tree variable
  233. * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
  234. *
  235. * This functions replaces the ematch tree in @dst with the ematch
  236. * tree in @src. The classifier in charge of the ematch tree may be
  237. * running.
  238. */
  239. static inline void tcf_em_tree_change(struct tcf_proto *tp,
  240. struct tcf_ematch_tree *dst,
  241. struct tcf_ematch_tree *src)
  242. {
  243. tcf_tree_lock(tp);
  244. memcpy(dst, src, sizeof(*dst));
  245. tcf_tree_unlock(tp);
  246. }
  247. /**
  248. * tcf_em_tree_match - evaulate an ematch tree
  249. *
  250. * @skb: socket buffer of the packet in question
  251. * @tree: ematch tree to be used for evaluation
  252. * @info: packet information examined by classifier
  253. *
  254. * This function matches @skb against the ematch tree in @tree by going
  255. * through all ematches respecting their logic relations returning
  256. * as soon as the result is obvious.
  257. *
  258. * Returns 1 if the ematch tree as-one matches, no ematches are configured
  259. * or ematch is not enabled in the kernel, otherwise 0 is returned.
  260. */
  261. static inline int tcf_em_tree_match(struct sk_buff *skb,
  262. struct tcf_ematch_tree *tree,
  263. struct tcf_pkt_info *info)
  264. {
  265. if (tree->hdr.nmatches)
  266. return __tcf_em_tree_match(skb, tree, info);
  267. else
  268. return 1;
  269. }
  270. #else /* CONFIG_NET_EMATCH */
  271. struct tcf_ematch_tree
  272. {
  273. };
  274. #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
  275. #define tcf_em_tree_destroy(tp, t) do { (void)(t); } while(0)
  276. #define tcf_em_tree_dump(skb, t, tlv) (0)
  277. #define tcf_em_tree_change(tp, dst, src) do { } while(0)
  278. #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
  279. #endif /* CONFIG_NET_EMATCH */
  280. static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
  281. {
  282. switch (layer) {
  283. case TCF_LAYER_LINK:
  284. return skb->data;
  285. case TCF_LAYER_NETWORK:
  286. return skb->nh.raw;
  287. case TCF_LAYER_TRANSPORT:
  288. return skb->h.raw;
  289. }
  290. return NULL;
  291. }
  292. static inline int tcf_valid_offset(struct sk_buff *skb, unsigned char *ptr,
  293. int len)
  294. {
  295. return unlikely((ptr + len) < skb->tail && ptr > skb->head);
  296. }
  297. #ifdef CONFIG_NET_CLS_IND
  298. static inline int
  299. tcf_change_indev(struct tcf_proto *tp, char *indev, struct rtattr *indev_tlv)
  300. {
  301. if (rtattr_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
  302. return -EINVAL;
  303. return 0;
  304. }
  305. static inline int
  306. tcf_match_indev(struct sk_buff *skb, char *indev)
  307. {
  308. if (indev[0]) {
  309. if (!skb->input_dev)
  310. return 0;
  311. if (strcmp(indev, skb->input_dev->name))
  312. return 0;
  313. }
  314. return 1;
  315. }
  316. #endif /* CONFIG_NET_CLS_IND */
  317. #endif