fib_rules.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. u8 l3mdev;
  19. /* 2 bytes hole, try to use */
  20. u32 target;
  21. __be64 tun_id;
  22. struct fib_rule __rcu *ctarget;
  23. struct net *fr_net;
  24. atomic_t refcnt;
  25. u32 pref;
  26. int suppress_ifgroup;
  27. int suppress_prefixlen;
  28. char iifname[IFNAMSIZ];
  29. char oifname[IFNAMSIZ];
  30. struct rcu_head rcu;
  31. };
  32. struct fib_lookup_arg {
  33. void *lookup_ptr;
  34. void *result;
  35. struct fib_rule *rule;
  36. u32 table;
  37. int flags;
  38. #define FIB_LOOKUP_NOREF 1
  39. #define FIB_LOOKUP_IGNORE_LINKSTATE 2
  40. };
  41. struct fib_rules_ops {
  42. int family;
  43. struct list_head list;
  44. int rule_size;
  45. int addr_size;
  46. int unresolved_rules;
  47. int nr_goto_rules;
  48. int (*action)(struct fib_rule *,
  49. struct flowi *, int,
  50. struct fib_lookup_arg *);
  51. bool (*suppress)(struct fib_rule *,
  52. struct fib_lookup_arg *);
  53. int (*match)(struct fib_rule *,
  54. struct flowi *, int);
  55. int (*configure)(struct fib_rule *,
  56. struct sk_buff *,
  57. struct fib_rule_hdr *,
  58. struct nlattr **);
  59. int (*delete)(struct fib_rule *);
  60. int (*compare)(struct fib_rule *,
  61. struct fib_rule_hdr *,
  62. struct nlattr **);
  63. int (*fill)(struct fib_rule *, struct sk_buff *,
  64. struct fib_rule_hdr *);
  65. size_t (*nlmsg_payload)(struct fib_rule *);
  66. /* Called after modifications to the rules set, must flush
  67. * the route cache if one exists. */
  68. void (*flush_cache)(struct fib_rules_ops *ops);
  69. int nlgroup;
  70. const struct nla_policy *policy;
  71. struct list_head rules_list;
  72. struct module *owner;
  73. struct net *fro_net;
  74. struct rcu_head rcu;
  75. };
  76. #define FRA_GENERIC_POLICY \
  77. [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  78. [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
  79. [FRA_PRIORITY] = { .type = NLA_U32 }, \
  80. [FRA_FWMARK] = { .type = NLA_U32 }, \
  81. [FRA_FWMASK] = { .type = NLA_U32 }, \
  82. [FRA_TABLE] = { .type = NLA_U32 }, \
  83. [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
  84. [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
  85. [FRA_GOTO] = { .type = NLA_U32 }, \
  86. [FRA_L3MDEV] = { .type = NLA_U8 }
  87. static inline void fib_rule_get(struct fib_rule *rule)
  88. {
  89. atomic_inc(&rule->refcnt);
  90. }
  91. static inline void fib_rule_put(struct fib_rule *rule)
  92. {
  93. if (atomic_dec_and_test(&rule->refcnt))
  94. kfree_rcu(rule, rcu);
  95. }
  96. #ifdef CONFIG_NET_L3_MASTER_DEV
  97. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  98. struct fib_lookup_arg *arg)
  99. {
  100. return rule->l3mdev ? arg->table : rule->table;
  101. }
  102. #else
  103. static inline u32 fib_rule_get_table(struct fib_rule *rule,
  104. struct fib_lookup_arg *arg)
  105. {
  106. return rule->table;
  107. }
  108. #endif
  109. static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
  110. {
  111. if (nla[FRA_TABLE])
  112. return nla_get_u32(nla[FRA_TABLE]);
  113. return frh->table;
  114. }
  115. struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
  116. struct net *);
  117. void fib_rules_unregister(struct fib_rules_ops *);
  118. int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
  119. struct fib_lookup_arg *);
  120. int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
  121. u32 flags);
  122. int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh);
  123. int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh);
  124. #endif