1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_FIB_RULES_H
3 #define __NET_FIB_RULES_H
5 #include <linux/types.h>
6 #include <linux/slab.h>
7 #include <linux/netdevice.h>
8 #include <linux/fib_rules.h>
9 #include <linux/refcount.h>
11 #include <net/rtnetlink.h>
12 #include <net/fib_notifier.h>
14 struct fib_kuid_range {
20 struct list_head list;
29 /* 2 bytes hole, try to use */
32 struct fib_rule __rcu *ctarget;
38 int suppress_prefixlen;
39 char iifname[IFNAMSIZ];
40 char oifname[IFNAMSIZ];
41 struct fib_kuid_range uid_range;
45 struct fib_lookup_arg {
48 struct fib_rule *rule;
51 #define FIB_LOOKUP_NOREF 1
52 #define FIB_LOOKUP_IGNORE_LINKSTATE 2
55 struct fib_rules_ops {
57 struct list_head list;
62 unsigned int fib_rules_seq;
64 int (*action)(struct fib_rule *,
66 struct fib_lookup_arg *);
67 bool (*suppress)(struct fib_rule *,
68 struct fib_lookup_arg *);
69 int (*match)(struct fib_rule *,
71 int (*configure)(struct fib_rule *,
73 struct fib_rule_hdr *,
75 int (*delete)(struct fib_rule *);
76 int (*compare)(struct fib_rule *,
77 struct fib_rule_hdr *,
79 int (*fill)(struct fib_rule *, struct sk_buff *,
80 struct fib_rule_hdr *);
81 size_t (*nlmsg_payload)(struct fib_rule *);
83 /* Called after modifications to the rules set, must flush
84 * the route cache if one exists. */
85 void (*flush_cache)(struct fib_rules_ops *ops);
88 const struct nla_policy *policy;
89 struct list_head rules_list;
95 struct fib_rule_notifier_info {
96 struct fib_notifier_info info; /* must be first */
97 struct fib_rule *rule;
100 #define FRA_GENERIC_POLICY \
101 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
102 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
103 [FRA_PRIORITY] = { .type = NLA_U32 }, \
104 [FRA_FWMARK] = { .type = NLA_U32 }, \
105 [FRA_FWMASK] = { .type = NLA_U32 }, \
106 [FRA_TABLE] = { .type = NLA_U32 }, \
107 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
108 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
109 [FRA_GOTO] = { .type = NLA_U32 }, \
110 [FRA_L3MDEV] = { .type = NLA_U8 }, \
111 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }
113 static inline void fib_rule_get(struct fib_rule *rule)
115 refcount_inc(&rule->refcnt);
118 static inline void fib_rule_put(struct fib_rule *rule)
120 if (refcount_dec_and_test(&rule->refcnt))
121 kfree_rcu(rule, rcu);
124 #ifdef CONFIG_NET_L3_MASTER_DEV
125 static inline u32 fib_rule_get_table(struct fib_rule *rule,
126 struct fib_lookup_arg *arg)
128 return rule->l3mdev ? arg->table : rule->table;
131 static inline u32 fib_rule_get_table(struct fib_rule *rule,
132 struct fib_lookup_arg *arg)
138 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
141 return nla_get_u32(nla[FRA_TABLE]);
145 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
147 void fib_rules_unregister(struct fib_rules_ops *);
149 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
150 struct fib_lookup_arg *);
151 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
153 bool fib_rule_matchall(const struct fib_rule *rule);
154 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
155 unsigned int fib_rules_seq_read(struct net *net, int family);
157 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
158 struct netlink_ext_ack *extack);
159 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
160 struct netlink_ext_ack *extack);