blob: 82da359bca035dc0ebfc019cc6d07acb5be77082 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Graf14c0b972006-08-04 03:38:38 -07002#ifndef __NET_FIB_RULES_H
3#define __NET_FIB_RULES_H
4
5#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Thomas Graf14c0b972006-08-04 03:38:38 -07007#include <linux/netdevice.h>
8#include <linux/fib_rules.h>
Reshetova, Elena717d1e92017-06-30 13:08:06 +03009#include <linux/refcount.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070010#include <net/flow.h>
Thomas Graf9d9e6a52007-03-25 23:20:05 -070011#include <net/rtnetlink.h>
Ido Schimmel1b2a4442017-08-03 13:28:14 +020012#include <net/fib_notifier.h>
Brian Vazquezb9aaec82020-07-26 15:48:16 -070013#include <linux/indirect_call_wrapper.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070014
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090015struct fib_kuid_range {
16 kuid_t start;
17 kuid_t end;
18};
19
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000020struct fib_rule {
Thomas Graf14c0b972006-08-04 03:38:38 -070021 struct list_head list;
Patrick McHardy491deb22009-12-03 01:25:54 +000022 int iifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +000023 int oifindex;
Thomas Grafb8964ed2006-11-09 15:22:18 -080024 u32 mark;
25 u32 mark_mask;
Thomas Graf14c0b972006-08-04 03:38:38 -070026 u32 flags;
27 u32 table;
28 u8 action;
David Ahern96c63fa2016-06-08 10:55:39 -070029 u8 l3mdev;
Donald Sharpcac56202018-02-20 08:55:58 -050030 u8 proto;
Roopa Prabhubfff4862018-02-28 22:40:16 -050031 u8 ip_proto;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070032 u32 target;
Thomas Grafe7030872015-07-21 10:44:01 +020033 __be64 tun_id;
Eric Dumazet7a2b03c2010-10-26 09:24:55 +000034 struct fib_rule __rcu *ctarget;
Eric Dumazetfba36792013-08-03 11:50:35 -070035 struct net *fr_net;
36
Reshetova, Elena717d1e92017-06-30 13:08:06 +030037 refcount_t refcnt;
Eric Dumazetfba36792013-08-03 11:50:35 -070038 u32 pref;
39 int suppress_ifgroup;
40 int suppress_prefixlen;
Patrick McHardy491deb22009-12-03 01:25:54 +000041 char iifname[IFNAMSIZ];
Patrick McHardy1b038a52009-12-03 01:25:56 +000042 char oifname[IFNAMSIZ];
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +090043 struct fib_kuid_range uid_range;
Roopa Prabhubfff4862018-02-28 22:40:16 -050044 struct fib_rule_port_range sport_range;
45 struct fib_rule_port_range dport_range;
Thomas Graf14c0b972006-08-04 03:38:38 -070046 struct rcu_head rcu;
47};
48
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000049struct fib_lookup_arg {
Thomas Graf14c0b972006-08-04 03:38:38 -070050 void *lookup_ptr;
David Ahernb75cc8f2018-03-02 08:32:17 -080051 const void *lookup_data;
Thomas Graf14c0b972006-08-04 03:38:38 -070052 void *result;
53 struct fib_rule *rule;
David Ahern96c63fa2016-06-08 10:55:39 -070054 u32 table;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000055 int flags;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -040056#define FIB_LOOKUP_NOREF 1
57#define FIB_LOOKUP_IGNORE_LINKSTATE 2
Thomas Graf14c0b972006-08-04 03:38:38 -070058};
59
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000060struct fib_rules_ops {
Thomas Graf14c0b972006-08-04 03:38:38 -070061 int family;
62 struct list_head list;
63 int rule_size;
Thomas Grafe1701c62007-03-24 12:46:02 -070064 int addr_size;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070065 int unresolved_rules;
66 int nr_goto_rules;
Ido Schimmel1b2a4442017-08-03 13:28:14 +020067 unsigned int fib_rules_seq;
Thomas Graf14c0b972006-08-04 03:38:38 -070068
69 int (*action)(struct fib_rule *,
70 struct flowi *, int,
71 struct fib_lookup_arg *);
msizanoen1cdef4852021-11-23 13:48:32 +010072 bool (*suppress)(struct fib_rule *, int,
Stefan Tomanek7764a452013-08-01 02:17:15 +020073 struct fib_lookup_arg *);
Thomas Graf14c0b972006-08-04 03:38:38 -070074 int (*match)(struct fib_rule *,
75 struct flowi *, int);
76 int (*configure)(struct fib_rule *,
77 struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070078 struct fib_rule_hdr *,
Roopa Prabhub16fb412018-04-21 09:41:31 -070079 struct nlattr **,
80 struct netlink_ext_ack *);
Alexander Duyck0ddcf432015-03-06 13:47:00 -080081 int (*delete)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070082 int (*compare)(struct fib_rule *,
83 struct fib_rule_hdr *,
84 struct nlattr **);
85 int (*fill)(struct fib_rule *, struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070086 struct fib_rule_hdr *);
Thomas Graf339bf982006-11-10 14:10:15 -080087 size_t (*nlmsg_payload)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070088
Thomas Graf73417f62007-03-27 13:56:52 -070089 /* Called after modifications to the rules set, must flush
90 * the route cache if one exists. */
Denis V. Lunevae299fc2008-07-05 19:01:28 -070091 void (*flush_cache)(struct fib_rules_ops *ops);
Thomas Graf73417f62007-03-27 13:56:52 -070092
Thomas Graf14c0b972006-08-04 03:38:38 -070093 int nlgroup;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070094 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070095 struct module *owner;
Denis V. Lunev03592382008-01-20 16:46:01 -080096 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080097 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070098};
99
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200100struct fib_rule_notifier_info {
101 struct fib_notifier_info info; /* must be first */
102 struct fib_rule *rule;
103};
104
Thomas Graf14c0b972006-08-04 03:38:38 -0700105static inline void fib_rule_get(struct fib_rule *rule)
106{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300107 refcount_inc(&rule->refcnt);
Thomas Graf14c0b972006-08-04 03:38:38 -0700108}
109
Thomas Graf14c0b972006-08-04 03:38:38 -0700110static inline void fib_rule_put(struct fib_rule *rule)
111{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300112 if (refcount_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500113 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700114}
115
David Ahern96c63fa2016-06-08 10:55:39 -0700116#ifdef CONFIG_NET_L3_MASTER_DEV
117static inline u32 fib_rule_get_table(struct fib_rule *rule,
118 struct fib_lookup_arg *arg)
119{
120 return rule->l3mdev ? arg->table : rule->table;
121}
122#else
123static inline u32 fib_rule_get_table(struct fib_rule *rule,
124 struct fib_lookup_arg *arg)
125{
126 return rule->table;
127}
128#endif
129
Patrick McHardy9e762a42006-08-10 23:09:48 -0700130static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
131{
132 if (nla[FRA_TABLE])
133 return nla_get_u32(nla[FRA_TABLE]);
134 return frh->table;
135}
136
Roopa Prabhubfff4862018-02-28 22:40:16 -0500137static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
138{
139 return range->start != 0 && range->end != 0;
140}
141
142static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
143 __be16 port)
144{
145 return ntohs(port) >= a->start &&
146 ntohs(port) <= a->end;
147}
148
149static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
150{
151 return a->start != 0 && a->end != 0 && a->end < 0xffff &&
152 a->start <= a->end;
153}
154
155static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
156 struct fib_rule_port_range *b)
157{
158 return a->start == b->start &&
159 a->end == b->end;
160}
161
162static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
163{
Petar Penkov63f9ba12019-07-05 11:46:43 -0700164 return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
Roopa Prabhubfff4862018-02-28 22:40:16 -0500165 fib_rule_port_range_set(&rule->sport_range) ||
Petar Penkov63f9ba12019-07-05 11:46:43 -0700166 fib_rule_port_range_set(&rule->dport_range));
Roopa Prabhubfff4862018-02-28 22:40:16 -0500167}
168
Joe Perches8de68792013-09-20 11:23:23 -0700169struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
170 struct net *);
171void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700172
Joe Perches8de68792013-09-20 11:23:23 -0700173int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
174 struct fib_lookup_arg *);
175int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
176 u32 flags);
Ido Schimmel3c710062017-03-16 09:08:12 +0100177bool fib_rule_matchall(const struct fib_rule *rule);
Jiri Pirkob7a59552019-10-03 11:49:30 +0200178int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
179 struct netlink_ext_ack *extack);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200180unsigned int fib_rules_seq_read(struct net *net, int family);
David Ahern96c63fa2016-06-08 10:55:39 -0700181
David Ahernc21ef3e2017-04-16 09:48:24 -0700182int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
183 struct netlink_ext_ack *extack);
184int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
185 struct netlink_ext_ack *extack);
Brian Vazquezb9aaec82020-07-26 15:48:16 -0700186
187INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
188 struct flowi *fl, int flags));
189INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
190 struct flowi *fl, int flags));
191
192INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
193 struct flowi *flp, int flags,
194 struct fib_lookup_arg *arg));
195INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
196 struct flowi *flp, int flags,
197 struct fib_lookup_arg *arg));
198
199INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
msizanoen1cdef4852021-11-23 13:48:32 +0100200 int flags,
Brian Vazquezb9aaec82020-07-26 15:48:16 -0700201 struct fib_lookup_arg *arg));
202INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
msizanoen1cdef4852021-11-23 13:48:32 +0100203 int flags,
Brian Vazquezb9aaec82020-07-26 15:48:16 -0700204 struct fib_lookup_arg *arg));
Thomas Graf14c0b972006-08-04 03:38:38 -0700205#endif