xref: /linux/include/net/fib_rules.h (revision 0947c9fe56d9cf7ad0bc3a03ccd30446cde698e4)
114c0b97dSThomas Graf #ifndef __NET_FIB_RULES_H
214c0b97dSThomas Graf #define __NET_FIB_RULES_H
314c0b97dSThomas Graf 
414c0b97dSThomas Graf #include <linux/types.h>
514c0b97dSThomas Graf #include <linux/netdevice.h>
614c0b97dSThomas Graf #include <linux/fib_rules.h>
714c0b97dSThomas Graf #include <net/flow.h>
89d9e6a58SThomas Graf #include <net/rtnetlink.h>
914c0b97dSThomas Graf 
1014c0b97dSThomas Graf struct fib_rule
1114c0b97dSThomas Graf {
1214c0b97dSThomas Graf 	struct list_head	list;
1314c0b97dSThomas Graf 	atomic_t		refcnt;
1414c0b97dSThomas Graf 	int			ifindex;
1514c0b97dSThomas Graf 	char			ifname[IFNAMSIZ];
16b8964ed9SThomas Graf 	u32			mark;
17b8964ed9SThomas Graf 	u32			mark_mask;
1814c0b97dSThomas Graf 	u32			pref;
1914c0b97dSThomas Graf 	u32			flags;
2014c0b97dSThomas Graf 	u32			table;
2114c0b97dSThomas Graf 	u8			action;
22*0947c9feSThomas Graf 	u32			target;
23*0947c9feSThomas Graf 	struct fib_rule *	ctarget;
2414c0b97dSThomas Graf 	struct rcu_head		rcu;
2514c0b97dSThomas Graf };
2614c0b97dSThomas Graf 
2714c0b97dSThomas Graf struct fib_lookup_arg
2814c0b97dSThomas Graf {
2914c0b97dSThomas Graf 	void			*lookup_ptr;
3014c0b97dSThomas Graf 	void			*result;
3114c0b97dSThomas Graf 	struct fib_rule		*rule;
3214c0b97dSThomas Graf };
3314c0b97dSThomas Graf 
3414c0b97dSThomas Graf struct fib_rules_ops
3514c0b97dSThomas Graf {
3614c0b97dSThomas Graf 	int			family;
3714c0b97dSThomas Graf 	struct list_head	list;
3814c0b97dSThomas Graf 	int			rule_size;
39e1701c68SThomas Graf 	int			addr_size;
40*0947c9feSThomas Graf 	int			unresolved_rules;
41*0947c9feSThomas Graf 	int			nr_goto_rules;
4214c0b97dSThomas Graf 
4314c0b97dSThomas Graf 	int			(*action)(struct fib_rule *,
4414c0b97dSThomas Graf 					  struct flowi *, int,
4514c0b97dSThomas Graf 					  struct fib_lookup_arg *);
4614c0b97dSThomas Graf 	int			(*match)(struct fib_rule *,
4714c0b97dSThomas Graf 					 struct flowi *, int);
4814c0b97dSThomas Graf 	int			(*configure)(struct fib_rule *,
4914c0b97dSThomas Graf 					     struct sk_buff *,
5014c0b97dSThomas Graf 					     struct nlmsghdr *,
5114c0b97dSThomas Graf 					     struct fib_rule_hdr *,
5214c0b97dSThomas Graf 					     struct nlattr **);
5314c0b97dSThomas Graf 	int			(*compare)(struct fib_rule *,
5414c0b97dSThomas Graf 					   struct fib_rule_hdr *,
5514c0b97dSThomas Graf 					   struct nlattr **);
5614c0b97dSThomas Graf 	int			(*fill)(struct fib_rule *, struct sk_buff *,
5714c0b97dSThomas Graf 					struct nlmsghdr *,
5814c0b97dSThomas Graf 					struct fib_rule_hdr *);
5914c0b97dSThomas Graf 	u32			(*default_pref)(void);
60339bf98fSThomas Graf 	size_t			(*nlmsg_payload)(struct fib_rule *);
6114c0b97dSThomas Graf 
6214c0b97dSThomas Graf 	int			nlgroup;
6314c0b97dSThomas Graf 	struct nla_policy	*policy;
6414c0b97dSThomas Graf 	struct list_head	*rules_list;
6514c0b97dSThomas Graf 	struct module		*owner;
6614c0b97dSThomas Graf };
6714c0b97dSThomas Graf 
681f6c9557SThomas Graf #define FRA_GENERIC_POLICY \
691f6c9557SThomas Graf 	[FRA_IFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, \
701f6c9557SThomas Graf 	[FRA_PRIORITY]	= { .type = NLA_U32 }, \
711f6c9557SThomas Graf 	[FRA_FWMARK]	= { .type = NLA_U32 }, \
721f6c9557SThomas Graf 	[FRA_FWMASK]	= { .type = NLA_U32 }, \
73*0947c9feSThomas Graf 	[FRA_TABLE]     = { .type = NLA_U32 }, \
74*0947c9feSThomas Graf 	[FRA_GOTO]	= { .type = NLA_U32 }
751f6c9557SThomas Graf 
7614c0b97dSThomas Graf static inline void fib_rule_get(struct fib_rule *rule)
7714c0b97dSThomas Graf {
7814c0b97dSThomas Graf 	atomic_inc(&rule->refcnt);
7914c0b97dSThomas Graf }
8014c0b97dSThomas Graf 
8114c0b97dSThomas Graf static inline void fib_rule_put_rcu(struct rcu_head *head)
8214c0b97dSThomas Graf {
8314c0b97dSThomas Graf 	struct fib_rule *rule = container_of(head, struct fib_rule, rcu);
8414c0b97dSThomas Graf 	kfree(rule);
8514c0b97dSThomas Graf }
8614c0b97dSThomas Graf 
8714c0b97dSThomas Graf static inline void fib_rule_put(struct fib_rule *rule)
8814c0b97dSThomas Graf {
8914c0b97dSThomas Graf 	if (atomic_dec_and_test(&rule->refcnt))
9014c0b97dSThomas Graf 		call_rcu(&rule->rcu, fib_rule_put_rcu);
9114c0b97dSThomas Graf }
9214c0b97dSThomas Graf 
939e762a4aSPatrick McHardy static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
949e762a4aSPatrick McHardy {
959e762a4aSPatrick McHardy 	if (nla[FRA_TABLE])
969e762a4aSPatrick McHardy 		return nla_get_u32(nla[FRA_TABLE]);
979e762a4aSPatrick McHardy 	return frh->table;
989e762a4aSPatrick McHardy }
999e762a4aSPatrick McHardy 
10014c0b97dSThomas Graf extern int			fib_rules_register(struct fib_rules_ops *);
10114c0b97dSThomas Graf extern int			fib_rules_unregister(struct fib_rules_ops *);
10214c0b97dSThomas Graf 
10314c0b97dSThomas Graf extern int			fib_rules_lookup(struct fib_rules_ops *,
10414c0b97dSThomas Graf 						 struct flowi *, int flags,
10514c0b97dSThomas Graf 						 struct fib_lookup_arg *);
10614c0b97dSThomas Graf #endif
107