fib_rules.h 3.8 KB

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