pkt_cls.h 9.1 KB

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