pkt_cls.h 9.1 KB

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