blob: 261202f73876a0c8820cd987a4bc1833066ae532 [file] [log] [blame]
Thomas Graf14c0b972006-08-04 03:38:38 -07001#ifndef __NET_FIB_RULES_H
2#define __NET_FIB_RULES_H
3
4#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Thomas Graf14c0b972006-08-04 03:38:38 -07006#include <linux/netdevice.h>
7#include <linux/fib_rules.h>
8#include <net/flow.h>
Thomas Graf9d9e6a52007-03-25 23:20:05 -07009#include <net/rtnetlink.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070010
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +090011struct fib_kuid_range {
12 kuid_t start;
13 kuid_t end;
14};
15
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000016struct fib_rule {
Thomas Graf14c0b972006-08-04 03:38:38 -070017 struct list_head list;
Patrick McHardy491deb24b2009-12-03 01:25:54 +000018 int iifindex;
Patrick McHardy1b038a52009-12-03 01:25:56 +000019 int oifindex;
Thomas Grafb8964ed2006-11-09 15:22:18 -080020 u32 mark;
21 u32 mark_mask;
Thomas Graf14c0b972006-08-04 03:38:38 -070022 u32 flags;
23 u32 table;
24 u8 action;
David Ahern96c63fa2016-06-08 10:55:39 -070025 u8 l3mdev;
26 /* 2 bytes hole, try to use */
Thomas Graf0947c9fe2007-03-26 17:14:15 -070027 u32 target;
Thomas Grafe7030872015-07-21 10:44:01 +020028 __be64 tun_id;
Eric Dumazet7a2b03c2010-10-26 09:24:55 +000029 struct fib_rule __rcu *ctarget;
Eric Dumazetfba36792013-08-03 11:50:35 -070030 struct net *fr_net;
31
32 atomic_t refcnt;
33 u32 pref;
34 int suppress_ifgroup;
35 int suppress_prefixlen;
Patrick McHardy491deb24b2009-12-03 01:25:54 +000036 char iifname[IFNAMSIZ];
Patrick McHardy1b038a52009-12-03 01:25:56 +000037 char oifname[IFNAMSIZ];
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +090038 struct fib_kuid_range uid_range;
Thomas Graf14c0b972006-08-04 03:38:38 -070039 struct rcu_head rcu;
40};
41
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000042struct fib_lookup_arg {
Thomas Graf14c0b972006-08-04 03:38:38 -070043 void *lookup_ptr;
44 void *result;
45 struct fib_rule *rule;
David Ahern96c63fa2016-06-08 10:55:39 -070046 u32 table;
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000047 int flags;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -040048#define FIB_LOOKUP_NOREF 1
49#define FIB_LOOKUP_IGNORE_LINKSTATE 2
Thomas Graf14c0b972006-08-04 03:38:38 -070050};
51
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000052struct fib_rules_ops {
Thomas Graf14c0b972006-08-04 03:38:38 -070053 int family;
54 struct list_head list;
55 int rule_size;
Thomas Grafe1701c62007-03-24 12:46:02 -070056 int addr_size;
Thomas Graf0947c9fe2007-03-26 17:14:15 -070057 int unresolved_rules;
58 int nr_goto_rules;
Thomas Graf14c0b972006-08-04 03:38:38 -070059
60 int (*action)(struct fib_rule *,
61 struct flowi *, int,
62 struct fib_lookup_arg *);
Stefan Tomanek7764a452013-08-01 02:17:15 +020063 bool (*suppress)(struct fib_rule *,
64 struct fib_lookup_arg *);
Thomas Graf14c0b972006-08-04 03:38:38 -070065 int (*match)(struct fib_rule *,
66 struct flowi *, int);
67 int (*configure)(struct fib_rule *,
68 struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070069 struct fib_rule_hdr *,
70 struct nlattr **);
Alexander Duyck0ddcf432015-03-06 13:47:00 -080071 int (*delete)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070072 int (*compare)(struct fib_rule *,
73 struct fib_rule_hdr *,
74 struct nlattr **);
75 int (*fill)(struct fib_rule *, struct sk_buff *,
Thomas Graf14c0b972006-08-04 03:38:38 -070076 struct fib_rule_hdr *);
Thomas Graf339bf982006-11-10 14:10:15 -080077 size_t (*nlmsg_payload)(struct fib_rule *);
Thomas Graf14c0b972006-08-04 03:38:38 -070078
Thomas Graf73417f62007-03-27 13:56:52 -070079 /* Called after modifications to the rules set, must flush
80 * the route cache if one exists. */
Denis V. Lunevae299fc2008-07-05 19:01:28 -070081 void (*flush_cache)(struct fib_rules_ops *ops);
Thomas Graf73417f62007-03-27 13:56:52 -070082
Thomas Graf14c0b972006-08-04 03:38:38 -070083 int nlgroup;
Patrick McHardyef7c79e2007-06-05 12:38:30 -070084 const struct nla_policy *policy;
Denis V. Lunev76c72d42007-09-16 15:44:27 -070085 struct list_head rules_list;
Thomas Graf14c0b972006-08-04 03:38:38 -070086 struct module *owner;
Denis V. Lunev035923832008-01-20 16:46:01 -080087 struct net *fro_net;
Eric W. Biedermane9c51582009-12-03 12:22:55 -080088 struct rcu_head rcu;
Thomas Graf14c0b972006-08-04 03:38:38 -070089};
90
Thomas Graf1f6c9552006-11-09 15:22:48 -080091#define FRA_GENERIC_POLICY \
Patrick McHardy491deb24b2009-12-03 01:25:54 +000092 [FRA_IIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Patrick McHardy1b038a52009-12-03 01:25:56 +000093 [FRA_OIFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
Thomas Graf1f6c9552006-11-09 15:22:48 -080094 [FRA_PRIORITY] = { .type = NLA_U32 }, \
95 [FRA_FWMARK] = { .type = NLA_U32 }, \
96 [FRA_FWMASK] = { .type = NLA_U32 }, \
Thomas Graf0947c9fe2007-03-26 17:14:15 -070097 [FRA_TABLE] = { .type = NLA_U32 }, \
Lorenzo Colitti3b824972014-03-31 16:23:51 +090098 [FRA_UID_START] = { .type = NLA_U32 }, \
99 [FRA_UID_END] = { .type = NLA_U32 }, \
Stefan Tomanek73f56982013-08-03 14:14:43 +0200100 [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
Stefan Tomanek6ef94cf2013-08-02 17:19:56 +0200101 [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
David Ahern96c63fa2016-06-08 10:55:39 -0700102 [FRA_GOTO] = { .type = NLA_U32 }, \
Lorenzo Colitti2ec93fe2016-11-04 02:23:42 +0900103 [FRA_L3MDEV] = { .type = NLA_U8 }, \
104 [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }
Thomas Graf1f6c9552006-11-09 15:22:48 -0800105
Thomas Graf14c0b972006-08-04 03:38:38 -0700106static inline void fib_rule_get(struct fib_rule *rule)
107{
108 atomic_inc(&rule->refcnt);
109}
110
Thomas Graf14c0b972006-08-04 03:38:38 -0700111static inline void fib_rule_put(struct fib_rule *rule)
112{
113 if (atomic_dec_and_test(&rule->refcnt))
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500114 kfree_rcu(rule, rcu);
Thomas Graf14c0b972006-08-04 03:38:38 -0700115}
116
David Ahern96c63fa2016-06-08 10:55:39 -0700117#ifdef CONFIG_NET_L3_MASTER_DEV
118static inline u32 fib_rule_get_table(struct fib_rule *rule,
119 struct fib_lookup_arg *arg)
120{
121 return rule->l3mdev ? arg->table : rule->table;
122}
123#else
124static inline u32 fib_rule_get_table(struct fib_rule *rule,
125 struct fib_lookup_arg *arg)
126{
127 return rule->table;
128}
129#endif
130
Patrick McHardy9e762a42006-08-10 23:09:48 -0700131static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
132{
133 if (nla[FRA_TABLE])
134 return nla_get_u32(nla[FRA_TABLE]);
135 return frh->table;
136}
137
Joe Perches8de68792013-09-20 11:23:23 -0700138struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
139 struct net *);
140void fib_rules_unregister(struct fib_rules_ops *);
Thomas Graf14c0b972006-08-04 03:38:38 -0700141
Joe Perches8de68792013-09-20 11:23:23 -0700142int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
143 struct fib_lookup_arg *);
144int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
145 u32 flags);
David Ahern96c63fa2016-06-08 10:55:39 -0700146
147int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh);
148int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh);
Thomas Graf14c0b972006-08-04 03:38:38 -0700149#endif