blob: 4b10676c69d1917e4c30e086bf8f00b1e0f37ed4 [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 *);
Stefan Tomanek7764a452013-08-01 02:17:15 +020072 bool (*suppress)(struct fib_rule *,
73 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;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070094 const struct nla_policy *policy;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070095 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070096 struct module *owner;
Denis V. Lunev03592382008-01-20 16:46:01 -080097 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080098 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070099};
100
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200101struct fib_rule_notifier_info {
102 struct fib_notifier_info info; /* must be first */
103 struct fib_rule *rule;
104};
105
Thomas Graf1f6c9552006-11-09 15:22:48 -0800106#define FRA_GENERIC_POLICY \
David Ahern75425652019-05-22 12:07:43 -0700107 [FRA_UNSPEC] = { .strict_start_type = FRA_DPORT_RANGE + 1 }, \
Patrick McHardy491deb22009-12-03 01:25:54 +0000108 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Patrick McHardy1b038a52009-12-03 01:25:56 +0000109 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -0800110 [FRA_PRIORITY] = { .type = NLA_U32 }, \
111 [FRA_FWMARK] = { .type = NLA_U32 }, \
Jakub Kicinski4c16d642020-03-02 21:05:13 -0800112 [FRA_TUN_ID] = { .type = NLA_U64 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -0800113 [FRA_FWMASK] = { .type = NLA_U32 }, \
Thomas Graf0947c9fe2007-03-26 17:14:15 -0700114 [FRA_TABLE] = { .type = NLA_U32 }, \
Stefan Tomanek73f56982013-08-03 14:14:43 +0200115 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200116 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
David Ahern96c63fa2016-06-08 10:55:39 -0700117 [FRA_GOTO] = { .type = NLA_U32 }, \
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900118 [FRA_L3MDEV] = { .type = NLA_U8 }, \
Donald Sharp1b71af62018-02-23 14:01:52 -0500119 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }, \
Roopa Prabhu5f6f8452018-03-01 17:55:37 -0800120 [FRA_PROTOCOL] = { .type = NLA_U8 }, \
121 [FRA_IP_PROTO] = { .type = NLA_U8 }, \
122 [FRA_SPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }, \
123 [FRA_DPORT_RANGE] = { .len = sizeof(struct fib_rule_port_range) }
124
Thomas Graf1f6c9552006-11-09 15:22:48 -0800125
Thomas Graf14c0b972006-08-04 03:38:38 -0700126static inline void fib_rule_get(struct fib_rule *rule)
127{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300128 refcount_inc(&rule->refcnt);
Thomas Graf14c0b972006-08-04 03:38:38 -0700129}
130
Thomas Graf14c0b972006-08-04 03:38:38 -0700131static inline void fib_rule_put(struct fib_rule *rule)
132{
Reshetova, Elena717d1e92017-06-30 13:08:06 +0300133 if (refcount_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500134 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700135}
136
David Ahern96c63fa2016-06-08 10:55:39 -0700137#ifdef CONFIG_NET_L3_MASTER_DEV
138static inline u32 fib_rule_get_table(struct fib_rule *rule,
139 struct fib_lookup_arg *arg)
140{
141 return rule->l3mdev ? arg->table : rule->table;
142}
143#else
144static inline u32 fib_rule_get_table(struct fib_rule *rule,
145 struct fib_lookup_arg *arg)
146{
147 return rule->table;
148}
149#endif
150
Patrick McHardy9e762a42006-08-10 23:09:48 -0700151static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
152{
153 if (nla[FRA_TABLE])
154 return nla_get_u32(nla[FRA_TABLE]);
155 return frh->table;
156}
157
Roopa Prabhubfff4862018-02-28 22:40:16 -0500158static inline bool fib_rule_port_range_set(const struct fib_rule_port_range *range)
159{
160 return range->start != 0 && range->end != 0;
161}
162
163static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
164 __be16 port)
165{
166 return ntohs(port) >= a->start &&
167 ntohs(port) <= a->end;
168}
169
170static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
171{
172 return a->start != 0 && a->end != 0 && a->end < 0xffff &&
173 a->start <= a->end;
174}
175
176static inline bool fib_rule_port_range_compare(struct fib_rule_port_range *a,
177 struct fib_rule_port_range *b)
178{
179 return a->start == b->start &&
180 a->end == b->end;
181}
182
183static inline bool fib_rule_requires_fldissect(struct fib_rule *rule)
184{
Petar Penkov63f9ba12019-07-05 11:46:43 -0700185 return rule->iifindex != LOOPBACK_IFINDEX && (rule->ip_proto ||
Roopa Prabhubfff4862018-02-28 22:40:16 -0500186 fib_rule_port_range_set(&rule->sport_range) ||
Petar Penkov63f9ba12019-07-05 11:46:43 -0700187 fib_rule_port_range_set(&rule->dport_range));
Roopa Prabhubfff4862018-02-28 22:40:16 -0500188}
189
Joe Perches8de68792013-09-20 11:23:23 -0700190struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
191 struct net *);
192void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700193
Joe Perches8de68792013-09-20 11:23:23 -0700194int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
195 struct fib_lookup_arg *);
196int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
197 u32 flags);
Ido Schimmel3c710062017-03-16 09:08:12 +0100198bool fib_rule_matchall(const struct fib_rule *rule);
Jiri Pirkob7a59552019-10-03 11:49:30 +0200199int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
200 struct netlink_ext_ack *extack);
Ido Schimmel1b2a4442017-08-03 13:28:14 +0200201unsigned int fib_rules_seq_read(struct net *net, int family);
David Ahern96c63fa2016-06-08 10:55:39 -0700202
David Ahernc21ef3e2017-04-16 09:48:24 -0700203int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
204 struct netlink_ext_ack *extack);
205int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
206 struct netlink_ext_ack *extack);
Brian Vazquezb9aaec82020-07-26 15:48:16 -0700207
208INDIRECT_CALLABLE_DECLARE(int fib6_rule_match(struct fib_rule *rule,
209 struct flowi *fl, int flags));
210INDIRECT_CALLABLE_DECLARE(int fib4_rule_match(struct fib_rule *rule,
211 struct flowi *fl, int flags));
212
213INDIRECT_CALLABLE_DECLARE(int fib6_rule_action(struct fib_rule *rule,
214 struct flowi *flp, int flags,
215 struct fib_lookup_arg *arg));
216INDIRECT_CALLABLE_DECLARE(int fib4_rule_action(struct fib_rule *rule,
217 struct flowi *flp, int flags,
218 struct fib_lookup_arg *arg));
219
220INDIRECT_CALLABLE_DECLARE(bool fib6_rule_suppress(struct fib_rule *rule,
221 struct fib_lookup_arg *arg));
222INDIRECT_CALLABLE_DECLARE(bool fib4_rule_suppress(struct fib_rule *rule,
223 struct fib_lookup_arg *arg));
Thomas Graf14c0b972006-08-04 03:38:38 -0700224#endif