xref: /openbmc/linux/net/ipv4/fib_rules.c (revision 3c71006d15fd3a99071a2b20d01de3edabc85767)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		IPv4 Forwarding Information Base: policy rules.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9e1ef4bf2SThomas Graf  *		Thomas Graf <tgraf@suug.ch>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * Fixes:
171da177e4SLinus Torvalds  *		Rani Assaf	:	local_rule cannot be deleted
181da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/types.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/netdevice.h>
241da177e4SLinus Torvalds #include <linux/netlink.h>
25e1ef4bf2SThomas Graf #include <linux/inetdevice.h>
261da177e4SLinus Torvalds #include <linux/init.h>
277b204afdSRobert Olsson #include <linux/list.h>
287b204afdSRobert Olsson #include <linux/rcupdate.h>
29bc3b2d7fSPaul Gortmaker #include <linux/export.h>
301da177e4SLinus Torvalds #include <net/ip.h>
311da177e4SLinus Torvalds #include <net/route.h>
321da177e4SLinus Torvalds #include <net/tcp.h>
331da177e4SLinus Torvalds #include <net/ip_fib.h>
34e1ef4bf2SThomas Graf #include <net/fib_rules.h>
351da177e4SLinus Torvalds 
366a31d2a9SEric Dumazet struct fib4_rule {
37e1ef4bf2SThomas Graf 	struct fib_rule		common;
38e1ef4bf2SThomas Graf 	u8			dst_len;
39e1ef4bf2SThomas Graf 	u8			src_len;
40e1ef4bf2SThomas Graf 	u8			tos;
4181f7bf6cSAl Viro 	__be32			src;
4281f7bf6cSAl Viro 	__be32			srcmask;
4381f7bf6cSAl Viro 	__be32			dst;
4481f7bf6cSAl Viro 	__be32			dstmask;
45c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
46e1ef4bf2SThomas Graf 	u32			tclassid;
471da177e4SLinus Torvalds #endif
481da177e4SLinus Torvalds };
491da177e4SLinus Torvalds 
50*3c71006dSIdo Schimmel static bool fib4_rule_matchall(const struct fib_rule *rule)
51*3c71006dSIdo Schimmel {
52*3c71006dSIdo Schimmel 	struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
53*3c71006dSIdo Schimmel 
54*3c71006dSIdo Schimmel 	if (r->dst_len || r->src_len || r->tos)
55*3c71006dSIdo Schimmel 		return false;
56*3c71006dSIdo Schimmel 	return fib_rule_matchall(rule);
57*3c71006dSIdo Schimmel }
58*3c71006dSIdo Schimmel 
59*3c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule)
60*3c71006dSIdo Schimmel {
61*3c71006dSIdo Schimmel 	if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
62*3c71006dSIdo Schimmel 	    rule->l3mdev)
63*3c71006dSIdo Schimmel 		return false;
64*3c71006dSIdo Schimmel 	if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
65*3c71006dSIdo Schimmel 	    rule->table != RT_TABLE_DEFAULT)
66*3c71006dSIdo Schimmel 		return false;
67*3c71006dSIdo Schimmel 	return true;
68*3c71006dSIdo Schimmel }
69*3c71006dSIdo Schimmel EXPORT_SYMBOL_GPL(fib4_rule_default);
70*3c71006dSIdo Schimmel 
710eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
720eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags)
73e1ef4bf2SThomas Graf {
74e1ef4bf2SThomas Graf 	struct fib_lookup_arg arg = {
75e1ef4bf2SThomas Graf 		.result = res,
760eeb075fSAndy Gospodarek 		.flags = flags,
77e1ef4bf2SThomas Graf 	};
78e1ef4bf2SThomas Graf 	int err;
79e1ef4bf2SThomas Graf 
809ee0034bSDavid Ahern 	/* update flow if oif or iif point to device enslaved to l3mdev */
819ee0034bSDavid Ahern 	l3mdev_update_flow(net, flowi4_to_flowi(flp));
829ee0034bSDavid Ahern 
8322bd5b9bSDavid S. Miller 	err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
8485b91b03SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
8585b91b03SDavid S. Miller 	if (arg.rule)
8685b91b03SDavid S. Miller 		res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
8785b91b03SDavid S. Miller 	else
8885b91b03SDavid S. Miller 		res->tclassid = 0;
8985b91b03SDavid S. Miller #endif
9049dd18baSPanu Matilainen 
9149dd18baSPanu Matilainen 	if (err == -ESRCH)
9249dd18baSPanu Matilainen 		err = -ENETUNREACH;
9349dd18baSPanu Matilainen 
941da177e4SLinus Torvalds 	return err;
951da177e4SLinus Torvalds }
96f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup);
971da177e4SLinus Torvalds 
988ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
998ce11e6aSAdrian Bunk 			    int flags, struct fib_lookup_arg *arg)
100e1ef4bf2SThomas Graf {
101e1ef4bf2SThomas Graf 	int err = -EAGAIN;
102e1ef4bf2SThomas Graf 	struct fib_table *tbl;
10396c63fa7SDavid Ahern 	u32 tb_id;
104e1ef4bf2SThomas Graf 
105e1ef4bf2SThomas Graf 	switch (rule->action) {
106e1ef4bf2SThomas Graf 	case FR_ACT_TO_TBL:
107e1ef4bf2SThomas Graf 		break;
108e1ef4bf2SThomas Graf 
109e1ef4bf2SThomas Graf 	case FR_ACT_UNREACHABLE:
110345e9b54SAlexander Duyck 		return -ENETUNREACH;
111e1ef4bf2SThomas Graf 
112e1ef4bf2SThomas Graf 	case FR_ACT_PROHIBIT:
113345e9b54SAlexander Duyck 		return -EACCES;
114e1ef4bf2SThomas Graf 
115e1ef4bf2SThomas Graf 	case FR_ACT_BLACKHOLE:
116e1ef4bf2SThomas Graf 	default:
117345e9b54SAlexander Duyck 		return -EINVAL;
118e1ef4bf2SThomas Graf 	}
119e1ef4bf2SThomas Graf 
120345e9b54SAlexander Duyck 	rcu_read_lock();
121e1ef4bf2SThomas Graf 
12296c63fa7SDavid Ahern 	tb_id = fib_rule_get_table(rule, arg);
12396c63fa7SDavid Ahern 	tbl = fib_get_table(rule->fr_net, tb_id);
124345e9b54SAlexander Duyck 	if (tbl)
125345e9b54SAlexander Duyck 		err = fib_table_lookup(tbl, &flp->u.ip4,
126345e9b54SAlexander Duyck 				       (struct fib_result *)arg->result,
127345e9b54SAlexander Duyck 				       arg->flags);
128345e9b54SAlexander Duyck 
129345e9b54SAlexander Duyck 	rcu_read_unlock();
130e1ef4bf2SThomas Graf 	return err;
131e1ef4bf2SThomas Graf }
132e1ef4bf2SThomas Graf 
1337764a45aSStefan Tomanek static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
1347764a45aSStefan Tomanek {
1356ef94cfaSStefan Tomanek 	struct fib_result *result = (struct fib_result *) arg->result;
136673498b8SStefan Tomanek 	struct net_device *dev = NULL;
137673498b8SStefan Tomanek 
138673498b8SStefan Tomanek 	if (result->fi)
139673498b8SStefan Tomanek 		dev = result->fi->fib_dev;
1406ef94cfaSStefan Tomanek 
1417764a45aSStefan Tomanek 	/* do not accept result if the route does
1427764a45aSStefan Tomanek 	 * not meet the required prefix length
1437764a45aSStefan Tomanek 	 */
14473f5698eSStefan Tomanek 	if (result->prefixlen <= rule->suppress_prefixlen)
1456ef94cfaSStefan Tomanek 		goto suppress_route;
1466ef94cfaSStefan Tomanek 
1476ef94cfaSStefan Tomanek 	/* do not accept result if the route uses a device
1486ef94cfaSStefan Tomanek 	 * belonging to a forbidden interface group
1496ef94cfaSStefan Tomanek 	 */
1506ef94cfaSStefan Tomanek 	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
1516ef94cfaSStefan Tomanek 		goto suppress_route;
1526ef94cfaSStefan Tomanek 
1536ef94cfaSStefan Tomanek 	return false;
1546ef94cfaSStefan Tomanek 
1556ef94cfaSStefan Tomanek suppress_route:
1567764a45aSStefan Tomanek 	if (!(arg->flags & FIB_LOOKUP_NOREF))
1577764a45aSStefan Tomanek 		fib_info_put(result->fi);
1587764a45aSStefan Tomanek 	return true;
1597764a45aSStefan Tomanek }
160e1ef4bf2SThomas Graf 
161e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
162e1ef4bf2SThomas Graf {
163e1ef4bf2SThomas Graf 	struct fib4_rule *r = (struct fib4_rule *) rule;
1649ade2286SDavid S. Miller 	struct flowi4 *fl4 = &fl->u.ip4;
1659ade2286SDavid S. Miller 	__be32 daddr = fl4->daddr;
1669ade2286SDavid S. Miller 	__be32 saddr = fl4->saddr;
167e1ef4bf2SThomas Graf 
168e1ef4bf2SThomas Graf 	if (((saddr ^ r->src) & r->srcmask) ||
169e1ef4bf2SThomas Graf 	    ((daddr ^ r->dst) & r->dstmask))
170e1ef4bf2SThomas Graf 		return 0;
171e1ef4bf2SThomas Graf 
1729ade2286SDavid S. Miller 	if (r->tos && (r->tos != fl4->flowi4_tos))
173e1ef4bf2SThomas Graf 		return 0;
174e1ef4bf2SThomas Graf 
175e1ef4bf2SThomas Graf 	return 1;
176e1ef4bf2SThomas Graf }
1771da177e4SLinus Torvalds 
1788ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net)
1791da177e4SLinus Torvalds {
1802dfe55b4SPatrick McHardy 	u32 id;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	for (id = 1; id <= RT_TABLE_MAX; id++)
18351456b29SIan Morris 		if (!fib_get_table(net, id))
1848ad4942cSDenis V. Lunev 			return fib_new_table(net, id);
1851da177e4SLinus Torvalds 	return NULL;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188b90eb754SJiri Pirko static int call_fib_rule_notifiers(struct net *net,
189b90eb754SJiri Pirko 				   enum fib_event_type event_type)
190b90eb754SJiri Pirko {
191b90eb754SJiri Pirko 	struct fib_notifier_info info;
192b90eb754SJiri Pirko 
193b90eb754SJiri Pirko 	return call_fib_notifiers(net, event_type, &info);
194b90eb754SJiri Pirko }
195b90eb754SJiri Pirko 
196d05f7a7dSIdo Schimmel void fib_rules_notify(struct net *net, struct notifier_block *nb)
197c0243892SIdo Schimmel {
198c0243892SIdo Schimmel 	struct fib_notifier_info info;
199c0243892SIdo Schimmel 
200c0243892SIdo Schimmel 	if (net->ipv4.fib_has_custom_rules)
201d05f7a7dSIdo Schimmel 		call_fib_notifier(nb, net, FIB_EVENT_RULE_ADD, &info);
202c0243892SIdo Schimmel }
203c0243892SIdo Schimmel 
204ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
2051f6c9557SThomas Graf 	FRA_GENERIC_POLICY,
206e1ef4bf2SThomas Graf 	[FRA_FLOW]	= { .type = NLA_U32 },
2071da177e4SLinus Torvalds };
2081da177e4SLinus Torvalds 
209e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
2108b3521eeSRami Rosen 			       struct fib_rule_hdr *frh,
211e1ef4bf2SThomas Graf 			       struct nlattr **tb)
2121da177e4SLinus Torvalds {
2133b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
214e1ef4bf2SThomas Graf 	int err = -EINVAL;
215e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
2161da177e4SLinus Torvalds 
217e1701c68SThomas Graf 	if (frh->tos & ~IPTOS_TOS_MASK)
218e1ef4bf2SThomas Graf 		goto errout;
219e1ef4bf2SThomas Graf 
2200ddcf43dSAlexander Duyck 	/* split local/main if they are not already split */
2210ddcf43dSAlexander Duyck 	err = fib_unmerge(net);
2220ddcf43dSAlexander Duyck 	if (err)
2230ddcf43dSAlexander Duyck 		goto errout;
2240ddcf43dSAlexander Duyck 
22596c63fa7SDavid Ahern 	if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
226e1ef4bf2SThomas Graf 		if (rule->action == FR_ACT_TO_TBL) {
227e1ef4bf2SThomas Graf 			struct fib_table *table;
228e1ef4bf2SThomas Graf 
229e4e4971cSDenis V. Lunev 			table = fib_empty_table(net);
23051456b29SIan Morris 			if (!table) {
231e1ef4bf2SThomas Graf 				err = -ENOBUFS;
232e1ef4bf2SThomas Graf 				goto errout;
233e1ef4bf2SThomas Graf 			}
234e1ef4bf2SThomas Graf 
235e1ef4bf2SThomas Graf 			rule->table = table->tb_id;
236e1ef4bf2SThomas Graf 		}
237e1ef4bf2SThomas Graf 	}
238e1ef4bf2SThomas Graf 
239e1701c68SThomas Graf 	if (frh->src_len)
24067b61f6cSJiri Benc 		rule4->src = nla_get_in_addr(tb[FRA_SRC]);
241e1ef4bf2SThomas Graf 
242e1701c68SThomas Graf 	if (frh->dst_len)
24367b61f6cSJiri Benc 		rule4->dst = nla_get_in_addr(tb[FRA_DST]);
244e1ef4bf2SThomas Graf 
245c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2467a9bc9b8SDavid S. Miller 	if (tb[FRA_FLOW]) {
247e1ef4bf2SThomas Graf 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
2487a9bc9b8SDavid S. Miller 		if (rule4->tclassid)
249f4530fa5SDavid S. Miller 			net->ipv4.fib_num_tclassid_users++;
2507a9bc9b8SDavid S. Miller 	}
2511da177e4SLinus Torvalds #endif
2521da177e4SLinus Torvalds 
253e1ef4bf2SThomas Graf 	rule4->src_len = frh->src_len;
254e1ef4bf2SThomas Graf 	rule4->srcmask = inet_make_mask(rule4->src_len);
255e1ef4bf2SThomas Graf 	rule4->dst_len = frh->dst_len;
256e1ef4bf2SThomas Graf 	rule4->dstmask = inet_make_mask(rule4->dst_len);
257e1ef4bf2SThomas Graf 	rule4->tos = frh->tos;
258e1ef4bf2SThomas Graf 
259f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = true;
260b90eb754SJiri Pirko 	call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD);
261104616e7SScott Feldman 
262e1ef4bf2SThomas Graf 	err = 0;
263e1ef4bf2SThomas Graf errout:
264e1ef4bf2SThomas Graf 	return err;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2670ddcf43dSAlexander Duyck static int fib4_rule_delete(struct fib_rule *rule)
2687a9bc9b8SDavid S. Miller {
269f4530fa5SDavid S. Miller 	struct net *net = rule->fr_net;
2700ddcf43dSAlexander Duyck 	int err;
2717a9bc9b8SDavid S. Miller 
2720ddcf43dSAlexander Duyck 	/* split local/main if they are not already split */
2730ddcf43dSAlexander Duyck 	err = fib_unmerge(net);
2740ddcf43dSAlexander Duyck 	if (err)
2750ddcf43dSAlexander Duyck 		goto errout;
2760ddcf43dSAlexander Duyck 
2770ddcf43dSAlexander Duyck #ifdef CONFIG_IP_ROUTE_CLASSID
2780ddcf43dSAlexander Duyck 	if (((struct fib4_rule *)rule)->tclassid)
279f4530fa5SDavid S. Miller 		net->ipv4.fib_num_tclassid_users--;
2807a9bc9b8SDavid S. Miller #endif
281f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = true;
282b90eb754SJiri Pirko 	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL);
2830ddcf43dSAlexander Duyck errout:
2840ddcf43dSAlexander Duyck 	return err;
2857a9bc9b8SDavid S. Miller }
2867a9bc9b8SDavid S. Miller 
287e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
288e1ef4bf2SThomas Graf 			     struct nlattr **tb)
289a5cdc030SPatrick McHardy {
290e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
291a5cdc030SPatrick McHardy 
292e1ef4bf2SThomas Graf 	if (frh->src_len && (rule4->src_len != frh->src_len))
293e1ef4bf2SThomas Graf 		return 0;
294e1ef4bf2SThomas Graf 
295e1ef4bf2SThomas Graf 	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
296e1ef4bf2SThomas Graf 		return 0;
297e1ef4bf2SThomas Graf 
298e1ef4bf2SThomas Graf 	if (frh->tos && (rule4->tos != frh->tos))
299e1ef4bf2SThomas Graf 		return 0;
300e1ef4bf2SThomas Graf 
301c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
302e1ef4bf2SThomas Graf 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
303e1ef4bf2SThomas Graf 		return 0;
304e1ef4bf2SThomas Graf #endif
305e1ef4bf2SThomas Graf 
30667b61f6cSJiri Benc 	if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
307e1ef4bf2SThomas Graf 		return 0;
308e1ef4bf2SThomas Graf 
30967b61f6cSJiri Benc 	if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
310e1ef4bf2SThomas Graf 		return 0;
311e1ef4bf2SThomas Graf 
312e1ef4bf2SThomas Graf 	return 1;
313a5cdc030SPatrick McHardy }
314a5cdc030SPatrick McHardy 
315e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
31604af8cf6SRami Rosen 			  struct fib_rule_hdr *frh)
3171da177e4SLinus Torvalds {
318e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
3191da177e4SLinus Torvalds 
320e1ef4bf2SThomas Graf 	frh->dst_len = rule4->dst_len;
321e1ef4bf2SThomas Graf 	frh->src_len = rule4->src_len;
322e1ef4bf2SThomas Graf 	frh->tos = rule4->tos;
3231da177e4SLinus Torvalds 
324f3756b79SDavid S. Miller 	if ((rule4->dst_len &&
325930345eaSJiri Benc 	     nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
326f3756b79SDavid S. Miller 	    (rule4->src_len &&
327930345eaSJiri Benc 	     nla_put_in_addr(skb, FRA_SRC, rule4->src)))
328f3756b79SDavid S. Miller 		goto nla_put_failure;
329c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
330f3756b79SDavid S. Miller 	if (rule4->tclassid &&
331f3756b79SDavid S. Miller 	    nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
332f3756b79SDavid S. Miller 		goto nla_put_failure;
333e1ef4bf2SThomas Graf #endif
334e1ef4bf2SThomas Graf 	return 0;
335e1ef4bf2SThomas Graf 
336e1ef4bf2SThomas Graf nla_put_failure:
337e1ef4bf2SThomas Graf 	return -ENOBUFS;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
340339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
341339bf98fSThomas Graf {
342339bf98fSThomas Graf 	return nla_total_size(4) /* dst */
343339bf98fSThomas Graf 	       + nla_total_size(4) /* src */
344339bf98fSThomas Graf 	       + nla_total_size(4); /* flow */
345339bf98fSThomas Graf }
346339bf98fSThomas Graf 
347ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
34873417f61SThomas Graf {
349bafa6d9dSNicolas Dichtel 	rt_cache_flush(ops->fro_net);
35073417f61SThomas Graf }
35173417f61SThomas Graf 
35204a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
35325239ceeSPatrick McHardy 	.family		= AF_INET,
354e1ef4bf2SThomas Graf 	.rule_size	= sizeof(struct fib4_rule),
355e1701c68SThomas Graf 	.addr_size	= sizeof(u32),
356e1ef4bf2SThomas Graf 	.action		= fib4_rule_action,
3577764a45aSStefan Tomanek 	.suppress	= fib4_rule_suppress,
358e1ef4bf2SThomas Graf 	.match		= fib4_rule_match,
359e1ef4bf2SThomas Graf 	.configure	= fib4_rule_configure,
3607a9bc9b8SDavid S. Miller 	.delete		= fib4_rule_delete,
361e1ef4bf2SThomas Graf 	.compare	= fib4_rule_compare,
362e1ef4bf2SThomas Graf 	.fill		= fib4_rule_fill,
363339bf98fSThomas Graf 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
36473417f61SThomas Graf 	.flush_cache	= fib4_rule_flush_cache,
365e1ef4bf2SThomas Graf 	.nlgroup	= RTNLGRP_IPV4_RULE,
366e1ef4bf2SThomas Graf 	.policy		= fib4_rule_policy,
367e1ef4bf2SThomas Graf 	.owner		= THIS_MODULE,
368e1ef4bf2SThomas Graf };
369e1ef4bf2SThomas Graf 
370e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops)
3712994c638SDenis V. Lunev {
3722994c638SDenis V. Lunev 	int err;
3732994c638SDenis V. Lunev 
3745adef180SPatrick McHardy 	err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
3752994c638SDenis V. Lunev 	if (err < 0)
3762994c638SDenis V. Lunev 		return err;
377e4e4971cSDenis V. Lunev 	err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
3782994c638SDenis V. Lunev 	if (err < 0)
3792994c638SDenis V. Lunev 		return err;
380e4e4971cSDenis V. Lunev 	err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
3812994c638SDenis V. Lunev 	if (err < 0)
3822994c638SDenis V. Lunev 		return err;
3832994c638SDenis V. Lunev 	return 0;
3842994c638SDenis V. Lunev }
3852994c638SDenis V. Lunev 
3867b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net)
387e1ef4bf2SThomas Graf {
388dbb50165SDenis V. Lunev 	int err;
389e4e4971cSDenis V. Lunev 	struct fib_rules_ops *ops;
390dbb50165SDenis V. Lunev 
391e9c5158aSEric W. Biederman 	ops = fib_rules_register(&fib4_rules_ops_template, net);
392e9c5158aSEric W. Biederman 	if (IS_ERR(ops))
393e9c5158aSEric W. Biederman 		return PTR_ERR(ops);
394e4e4971cSDenis V. Lunev 
395e4e4971cSDenis V. Lunev 	err = fib_default_rules_init(ops);
396dbb50165SDenis V. Lunev 	if (err < 0)
397dbb50165SDenis V. Lunev 		goto fail;
398e4e4971cSDenis V. Lunev 	net->ipv4.rules_ops = ops;
399f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = false;
400dbb50165SDenis V. Lunev 	return 0;
401dbb50165SDenis V. Lunev 
402dbb50165SDenis V. Lunev fail:
403dbb50165SDenis V. Lunev 	/* also cleans all rules already added */
4049e3a5487SDenis V. Lunev 	fib_rules_unregister(ops);
405dbb50165SDenis V. Lunev 	return err;
406e1ef4bf2SThomas Graf }
4077b1a74fdSDenis V. Lunev 
4087b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net)
4097b1a74fdSDenis V. Lunev {
4109e3a5487SDenis V. Lunev 	fib_rules_unregister(net->ipv4.rules_ops);
4117b1a74fdSDenis V. Lunev }
412