xref: /openbmc/linux/include/net/fib_rules.h (revision 711aab1d)
1 #ifndef __NET_FIB_RULES_H
2 #define __NET_FIB_RULES_H
3 
4 #include <linux/types.h>
5 #include <linux/slab.h>
6 #include <linux/netdevice.h>
7 #include <linux/fib_rules.h>
8 #include <linux/refcount.h>
9 #include <net/flow.h>
10 #include <net/rtnetlink.h>
11 #include <net/fib_notifier.h>
12 
13 struct fib_kuid_range {
14 	kuid_t start;
15 	kuid_t end;
16 };
17 
18 struct fib_rule {
19 	struct list_head	list;
20 	int			iifindex;
21 	int			oifindex;
22 	u32			mark;
23 	u32			mark_mask;
24 	u32			flags;
25 	u32			table;
26 	u8			action;
27 	u8			l3mdev;
28 	/* 2 bytes hole, try to use */
29 	u32			target;
30 	__be64			tun_id;
31 	struct fib_rule __rcu	*ctarget;
32 	struct net		*fr_net;
33 
34 	refcount_t		refcnt;
35 	u32			pref;
36 	int			suppress_ifgroup;
37 	int			suppress_prefixlen;
38 	char			iifname[IFNAMSIZ];
39 	char			oifname[IFNAMSIZ];
40 	struct fib_kuid_range	uid_range;
41 	struct rcu_head		rcu;
42 };
43 
44 struct fib_lookup_arg {
45 	void			*lookup_ptr;
46 	void			*result;
47 	struct fib_rule		*rule;
48 	u32			table;
49 	int			flags;
50 #define FIB_LOOKUP_NOREF		1
51 #define FIB_LOOKUP_IGNORE_LINKSTATE	2
52 };
53 
54 struct fib_rules_ops {
55 	int			family;
56 	struct list_head	list;
57 	int			rule_size;
58 	int			addr_size;
59 	int			unresolved_rules;
60 	int			nr_goto_rules;
61 	unsigned int		fib_rules_seq;
62 
63 	int			(*action)(struct fib_rule *,
64 					  struct flowi *, int,
65 					  struct fib_lookup_arg *);
66 	bool			(*suppress)(struct fib_rule *,
67 					    struct fib_lookup_arg *);
68 	int			(*match)(struct fib_rule *,
69 					 struct flowi *, int);
70 	int			(*configure)(struct fib_rule *,
71 					     struct sk_buff *,
72 					     struct fib_rule_hdr *,
73 					     struct nlattr **);
74 	int			(*delete)(struct fib_rule *);
75 	int			(*compare)(struct fib_rule *,
76 					   struct fib_rule_hdr *,
77 					   struct nlattr **);
78 	int			(*fill)(struct fib_rule *, struct sk_buff *,
79 					struct fib_rule_hdr *);
80 	size_t			(*nlmsg_payload)(struct fib_rule *);
81 
82 	/* Called after modifications to the rules set, must flush
83 	 * the route cache if one exists. */
84 	void			(*flush_cache)(struct fib_rules_ops *ops);
85 
86 	int			nlgroup;
87 	const struct nla_policy	*policy;
88 	struct list_head	rules_list;
89 	struct module		*owner;
90 	struct net		*fro_net;
91 	struct rcu_head		rcu;
92 };
93 
94 struct fib_rule_notifier_info {
95 	struct fib_notifier_info info; /* must be first */
96 	struct fib_rule *rule;
97 };
98 
99 #define FRA_GENERIC_POLICY \
100 	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
101 	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
102 	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
103 	[FRA_FWMARK]	= { .type = NLA_U32 }, \
104 	[FRA_FWMASK]	= { .type = NLA_U32 }, \
105 	[FRA_TABLE]     = { .type = NLA_U32 }, \
106 	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
107 	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
108 	[FRA_GOTO]	= { .type = NLA_U32 }, \
109 	[FRA_L3MDEV]	= { .type = NLA_U8 }, \
110 	[FRA_UID_RANGE]	= { .len = sizeof(struct fib_rule_uid_range) }
111 
112 static inline void fib_rule_get(struct fib_rule *rule)
113 {
114 	refcount_inc(&rule->refcnt);
115 }
116 
117 static inline void fib_rule_put(struct fib_rule *rule)
118 {
119 	if (refcount_dec_and_test(&rule->refcnt))
120 		kfree_rcu(rule, rcu);
121 }
122 
123 #ifdef CONFIG_NET_L3_MASTER_DEV
124 static inline u32 fib_rule_get_table(struct fib_rule *rule,
125 				     struct fib_lookup_arg *arg)
126 {
127 	return rule->l3mdev ? arg->table : rule->table;
128 }
129 #else
130 static inline u32 fib_rule_get_table(struct fib_rule *rule,
131 				     struct fib_lookup_arg *arg)
132 {
133 	return rule->table;
134 }
135 #endif
136 
137 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
138 {
139 	if (nla[FRA_TABLE])
140 		return nla_get_u32(nla[FRA_TABLE]);
141 	return frh->table;
142 }
143 
144 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
145 					 struct net *);
146 void fib_rules_unregister(struct fib_rules_ops *);
147 
148 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
149 		     struct fib_lookup_arg *);
150 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
151 			 u32 flags);
152 bool fib_rule_matchall(const struct fib_rule *rule);
153 int fib_rules_dump(struct net *net, struct notifier_block *nb, int family);
154 unsigned int fib_rules_seq_read(struct net *net, int family);
155 
156 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
157 		   struct netlink_ext_ack *extack);
158 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
159 		   struct netlink_ext_ack *extack);
160 #endif
161