0001
0002 #ifndef __NET_FIB_RULES_H
0003 #define __NET_FIB_RULES_H
0004
0005 #include <linux/types.h>
0006 #include <linux/slab.h>
0007 #include <linux/netdevice.h>
0008 #include <linux/fib_rules.h>
0009 #include <linux/refcount.h>
0010 #include <net/flow.h>
0011 #include <net/rtnetlink.h>
0012 #include <net/fib_notifier.h>
0013 #include <linux/indirect_call_wrapper.h>
0014
0015 struct fib_kuid_range {
0016 kuid_t start;
0017 kuid_t end;
0018 };
0019
0020 struct fib_rule {
0021 struct list_head list;
0022 int iifindex;
0023 int oifindex;
0024 u32 mark;
0025 u32 mark_mask;
0026 u32 flags;
0027 u32 table;
0028 u8 action;
0029 u8 l3mdev;
0030 u8 proto;
0031 u8 ip_proto;
0032 u32 target;
0033 __be64 tun_id;
0034 struct fib_rule __rcu *ctarget;
0035 struct net *fr_net;
0036
0037 refcount_t refcnt;
0038 u32 pref;
0039 int suppress_ifgroup;
0040 int suppress_prefixlen;
0041 char iifname[IFNAMSIZ];
0042 char oifname[IFNAMSIZ];
0043 struct fib_kuid_range uid_range;
0044 struct fib_rule_port_range sport_range;
0045 struct fib_rule_port_range dport_range;
0046 struct rcu_head rcu;
0047 };
0048
0049 struct fib_lookup_arg {
0050 void *lookup_ptr;
0051 const void *lookup_data;
0052 void *result;
0053 struct fib_rule *rule;
0054 u32 table;
0055 int flags;
0056 #define FIB_LOOKUP_NOREF 1
0057 #define FIB_LOOKUP_IGNORE_LINKSTATE 2
0058 };
0059
0060 struct fib_rules_ops {
0061 int family;
0062 struct list_head list;
0063 int rule_size;
0064 int addr_size;
0065 int unresolved_rules;
0066 int nr_goto_rules;
0067 unsigned int fib_rules_seq;
0068
0069 int (*action)(struct fib_rule *,
0070 struct flowi *, int,
0071 struct fib_lookup_arg *);
0072 bool (*suppress)(struct fib_rule *, int,
0073 struct fib_lookup_arg *);
0074 int (*match)(struct fib_rule *,
0075 struct flowi *, int);
0076 int (*configure)(struct fib_rule *,
0077 struct sk_buff *,
0078 struct fib_rule_hdr *,
0079 struct nlattr **,
0080 struct netlink_ext_ack *);
0081 int (*delete)(struct fib_rule *);
0082 int (*compare)(struct fib_rule *,
0083 struct fib_rule_hdr *,
0084 struct nlattr **);
0085 int (*fill)(struct fib_rule *, struct sk_buff *,
0086 struct fib_rule_hdr *);
0087 size_t (*nlmsg_payload)(struct fib_rule *);
0088
0089
0090
0091 void (*flush_cache)(struct fib_rules_ops *ops);
0092
0093 int nlgroup;
0094 struct list_head rules_list;
0095 struct module *owner;
0096 struct net *fro_net;
0097 struct rcu_head rcu;
0098 };
0099
0100 struct fib_rule_notifier_info {
0101 struct fib_notifier_info info;
0102 struct fib_rule *rule;
0103 };
0104
0105 static inline void fib_rule_get(struct fib_rule *rule)
0106 {
0107 refcount_inc(&rule->refcnt);
0108 }
0109
0110 static inline void fib_rule_put(struct fib_rule *rule)
0111 {
0112 if (refcount_dec_and_test(&rule->refcnt))
0113 kfree_rcu(rule, rcu);
0114 }
0115
0116 #ifdef CONFIG_NET_L3_MASTER_DEV
0117 static inline u32 fib_rule_get_table(struct fib_rule *rule,
0118 struct fib_lookup_arg *arg)
0119 {
0120 return rule->l3mdev ? arg->table : rule->table;
0121 }
0122 #else
0123 static inline u32 fib_rule_get_table(struct fib_rule *rule,
0124 struct fib_lookup_arg *arg)
0125 {
0126 return rule->table;
0127 }
0128 #endif
0129
0130 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
0131 {
0132 if (nla[FRA_TABLE])
0133 return nla_get_u32(nla[FRA_TABLE]);
0134 return frh->table;
0135 }
0136
0137 static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
0138 {
0139 return range->start != 0 && range->end != 0;
0140 }
0141
0142 static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
0143 __be16 port)
0144 {
0145 return ntohs(port) >= a->start &&
0146 ntohs(port) <= a->end;
0147 }
0148
0149 static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
0150 {
0151 return a->start != 0 && a->end != 0 && a->end < 0xffff &&
0152 a->start <= a->end;
0153 }
0154
0155 static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
0156 struct fib_rule_port_range *b)
0157 {
0158 return a->start == b->start &&
0159 a->end == b->end;
0160 }
0161
0162 static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
0163 {
0164 return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
0165 fib_rule_port_range_set(&rule->sport_range) ||
0166 fib_rule_port_range_set(&rule->dport_range));
0167 }
0168
0169 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
0170 struct net *);
0171 void fib_rules_unregister(struct fib_rules_ops *);
0172
0173 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
0174 struct fib_lookup_arg *);
0175 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
0176 u32 flags);
0177 bool fib_rule_matchall(const struct fib_rule *rule);
0178 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
0179 struct netlink_ext_ack *extack);
0180 unsigned int fib_rules_seq_read(struct net *net, int family);
0181
0182 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
0183 struct netlink_ext_ack *extack);
0184 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
0185 struct netlink_ext_ack *extack);
0186
0187 INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
0188 struct flowi *fl, int flags));
0189 INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
0190 struct flowi *fl, int flags));
0191
0192 INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
0193 struct flowi *flp, int flags,
0194 struct fib_lookup_arg *arg));
0195 INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
0196 struct flowi *flp, int flags,
0197 struct fib_lookup_arg *arg));
0198
0199 INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
0200 int flags,
0201 struct fib_lookup_arg *arg));
0202 INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
0203 int flags,
0204 struct fib_lookup_arg *arg));
0205 #endif