fib_rules.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef __NET_FIB_RULES_H
  2. #define __NET_FIB_RULES_H
  3. #include <linux/types.h>
  4. #include <linux/slab.h>
  5. #include <linux/netdevice.h>
  6. #include <linux/fib_rules.h>
  7. #include <net/flow.h>
  8. #include <net/rtnetlink.h>
  9. struct fib_rule {
  10. struct list_head list;
  11. int iifindex;
  12. int oifindex;
  13. u32 mark;
  14. u32 mark_mask;
  15. u32 flags;
  16. u32 table;
  17. u8 action;
  18. /* 3 bytes hole, try to use */
  19. u32 target;
  20. struct fib_rule __rcu *ctarget;
  21. struct net *fr_net;
  22. atomic_t refcnt;
  23. u32 pref;
  24. int suppress_ifgroup;
  25. int suppress_prefixlen;
  26. char iifname[IFNAMSIZ];
  27. char oifname[IFNAMSIZ];
  28. struct rcu_head rcu;
  29. };
  30. struct fib_lookup_arg {
  31. void *lookup_ptr;
  32. void *result;
  33. struct fib_rule *rule;
  34. int flags;
  35. #define FIB_LOOKUP_NOREF 1
  36. };
  37. struct fib_rules_ops {
  38. int family;
  39. struct list_head list;
  40. int rule_size;
  41. int addr_size;
  42. int unresolved_rules;
  43. int nr_goto_rules;
  44. int (*action)(struct fib_rule *,
  45. struct flowi *, int,
  46. struct fib_lookup_arg *);
  47. bool (*suppress)(struct fib_rule *,
  48. struct fib_lookup_arg *);
  49. int (*match)(struct fib_rule *,
  50. struct flowi *, int);
  51. int (*configure)(struct fib_rule *,
  52. struct sk_buff *,
  53. struct fib_rule_hdr *,
  54. struct nlattr **);
  55. void (*delete)(struct fib_rule *);
  56. int (*compare)(struct fib_rule *,
  57. struct fib_rule_hdr *,
  58. struct nlattr **);
  59. int (*fill)(struct fib_rule *, struct sk_buff *,
  60. struct fib_rule_hdr *);
  61. u32 (*default_pref)(struct fib_rules_ops *ops);
  62. size_t (*nlmsg_payload)(struct fib_rule *);
  63. /* Called after modifications to the rules set, must flush
  64. * the route cache if one exists. */
  65. void (*flush_cache)(struct fib_rules_ops *ops);
  66. int nlgroup;
  67. const struct nla_policy *policy;
  68. struct list_head rules_list;
  69. struct module *owner;
  70. struct net *fro_net;
  71. struct rcu_head rcu;
  72. };
  73. #define FRA_GENERIC_POLICY \
  74. [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  75. [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  76. [FRA_PRIORITY] = { .type = NLA_U32 }, \
  77. [FRA_FWMARK] = { .type = NLA_U32 }, \
  78. [FRA_FWMASK] = { .type = NLA_U32 }, \
  79. [FRA_TABLE] = { .type = NLA_U32 }, \
  80. [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
  81. [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
  82. [FRA_GOTO] = { .type = NLA_U32 }
  83. static inline void fib_rule_get(struct fib_rule *rule)
  84. {
  85. atomic_inc(&rule->refcnt);
  86. }
  87. static inline void fib_rule_put_rcu(struct rcu_head *head)
  88. {
  89. struct fib_rule *rule = container_of(head, struct fib_rule, rcu);
  90. release_net(rule->fr_net);
  91. kfree(rule);
  92. }
  93. static inline void fib_rule_put(struct fib_rule *rule)
  94. {
  95. if (atomic_dec_and_test(&rule->refcnt))
  96. call_rcu(&rule->rcu, fib_rule_put_rcu);
  97. }
  98. static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
  99. {
  100. if (nla[FRA_TABLE])
  101. return nla_get_u32(nla[FRA_TABLE]);
  102. return frh->table;
  103. }
  104. extern struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *, struct net *);
  105. extern void fib_rules_unregister(struct fib_rules_ops *);
  106. extern int fib_rules_lookup(struct fib_rules_ops *,
  107. struct flowi *, int flags,
  108. struct fib_lookup_arg *);
  109. extern int fib_default_rule_add(struct fib_rules_ops *,
  110. u32 pref, u32 table,
  111. u32 flags);
  112. extern u32 fib_default_rule_pref(struct fib_rules_ops *ops);
  113. #endif