• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <net/flow.h>
9 #include <net/rtnetlink.h>
10 
11 struct fib_kuid_range {
12 	kuid_t start;
13 	kuid_t end;
14 };
15 
16 struct fib_rule {
17 	struct list_head	list;
18 	int			iifindex;
19 	int			oifindex;
20 	u32			mark;
21 	u32			mark_mask;
22 	u32			flags;
23 	u32			table;
24 	u8			action;
25 	/* 3 bytes hole, try to use */
26 	u32			target;
27 	struct fib_rule __rcu	*ctarget;
28 	struct net		*fr_net;
29 
30 	atomic_t		refcnt;
31 	u32			pref;
32 	int			suppress_ifgroup;
33 	int			suppress_prefixlen;
34 	char			iifname[IFNAMSIZ];
35 	char			oifname[IFNAMSIZ];
36 	struct fib_kuid_range	uid_range;
37 	struct rcu_head		rcu;
38 };
39 
40 struct fib_lookup_arg {
41 	void			*lookup_ptr;
42 	void			*result;
43 	struct fib_rule		*rule;
44 	int			flags;
45 #define FIB_LOOKUP_NOREF	1
46 };
47 
48 struct fib_rules_ops {
49 	int			family;
50 	struct list_head	list;
51 	int			rule_size;
52 	int			addr_size;
53 	int			unresolved_rules;
54 	int			nr_goto_rules;
55 
56 	int			(*action)(struct fib_rule *,
57 					  struct flowi *, int,
58 					  struct fib_lookup_arg *);
59 	bool			(*suppress)(struct fib_rule *,
60 					    struct fib_lookup_arg *);
61 	int			(*match)(struct fib_rule *,
62 					 struct flowi *, int);
63 	int			(*configure)(struct fib_rule *,
64 					     struct sk_buff *,
65 					     struct fib_rule_hdr *,
66 					     struct nlattr **);
67 	void			(*delete)(struct fib_rule *);
68 	int			(*compare)(struct fib_rule *,
69 					   struct fib_rule_hdr *,
70 					   struct nlattr **);
71 	int			(*fill)(struct fib_rule *, struct sk_buff *,
72 					struct fib_rule_hdr *);
73 	u32			(*default_pref)(struct fib_rules_ops *ops);
74 	size_t			(*nlmsg_payload)(struct fib_rule *);
75 
76 	/* Called after modifications to the rules set, must flush
77 	 * the route cache if one exists. */
78 	void			(*flush_cache)(struct fib_rules_ops *ops);
79 
80 	int			nlgroup;
81 	const struct nla_policy	*policy;
82 	struct list_head	rules_list;
83 	struct module		*owner;
84 	struct net		*fro_net;
85 	struct rcu_head		rcu;
86 };
87 
88 #define FRA_GENERIC_POLICY \
89 	[FRA_IIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
90 	[FRA_OIFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
91 	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
92 	[FRA_FWMARK]	= { .type = NLA_U32 }, \
93 	[FRA_FWMASK]	= { .type = NLA_U32 }, \
94 	[FRA_TABLE]     = { .type = NLA_U32 }, \
95 	[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
96 	[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
97 	[FRA_GOTO]	= { .type = NLA_U32 }, \
98 	[FRA_UID_RANGE]	= { .len = sizeof(struct fib_rule_uid_range) }
99 
fib_rule_get(struct fib_rule * rule)100 static inline void fib_rule_get(struct fib_rule *rule)
101 {
102 	atomic_inc(&rule->refcnt);
103 }
104 
fib_rule_put_rcu(struct rcu_head * head)105 static inline void fib_rule_put_rcu(struct rcu_head *head)
106 {
107 	struct fib_rule *rule = container_of(head, struct fib_rule, rcu);
108 	release_net(rule->fr_net);
109 	kfree(rule);
110 }
111 
fib_rule_put(struct fib_rule * rule)112 static inline void fib_rule_put(struct fib_rule *rule)
113 {
114 	if (atomic_dec_and_test(&rule->refcnt))
115 		call_rcu(&rule->rcu, fib_rule_put_rcu);
116 }
117 
frh_get_table(struct fib_rule_hdr * frh,struct nlattr ** nla)118 static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
119 {
120 	if (nla[FRA_TABLE])
121 		return nla_get_u32(nla[FRA_TABLE]);
122 	return frh->table;
123 }
124 
125 struct fib_rules_ops *fib_rules_register(const struct fib_rules_ops *,
126 					 struct net *);
127 void fib_rules_unregister(struct fib_rules_ops *);
128 
129 int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
130 		     struct fib_lookup_arg *);
131 int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
132 			 u32 flags);
133 u32 fib_default_rule_pref(struct fib_rules_ops *ops);
134 #endif
135