xref: /openbmc/linux/net/ipv4/fib_rules.c (revision 213f5f8f31f10aa1e83187ae20fb7fa4e626b724)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		IPv4 Forwarding Information Base: policy rules.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10e1ef4bf2SThomas Graf  *		Thomas Graf <tgraf@suug.ch>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Fixes:
131da177e4SLinus Torvalds  *		Rani Assaf	:	local_rule cannot be deleted
141da177e4SLinus Torvalds  *		Marc Boucher	:	routing by fwmark
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/types.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/netdevice.h>
201da177e4SLinus Torvalds #include <linux/netlink.h>
21e1ef4bf2SThomas Graf #include <linux/inetdevice.h>
221da177e4SLinus Torvalds #include <linux/init.h>
237b204afdSRobert Olsson #include <linux/list.h>
247b204afdSRobert Olsson #include <linux/rcupdate.h>
25bc3b2d7fSPaul Gortmaker #include <linux/export.h>
261da177e4SLinus Torvalds #include <net/ip.h>
271da177e4SLinus Torvalds #include <net/route.h>
281da177e4SLinus Torvalds #include <net/tcp.h>
291da177e4SLinus Torvalds #include <net/ip_fib.h>
305481d73fSDavid Ahern #include <net/nexthop.h>
31e1ef4bf2SThomas Graf #include <net/fib_rules.h>
32b9aaec8fSBrian Vazquez #include <linux/indirect_call_wrapper.h>
331da177e4SLinus Torvalds 
346a31d2a9SEric Dumazet struct fib4_rule {
35e1ef4bf2SThomas Graf 	struct fib_rule		common;
36e1ef4bf2SThomas Graf 	u8			dst_len;
37e1ef4bf2SThomas Graf 	u8			src_len;
38e1ef4bf2SThomas Graf 	u8			tos;
3981f7bf6cSAl Viro 	__be32			src;
4081f7bf6cSAl Viro 	__be32			srcmask;
4181f7bf6cSAl Viro 	__be32			dst;
4281f7bf6cSAl Viro 	__be32			dstmask;
43c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
44e1ef4bf2SThomas Graf 	u32			tclassid;
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
483c71006dSIdo Schimmel static bool fib4_rule_matchall(const struct fib_rule *rule)
493c71006dSIdo Schimmel {
503c71006dSIdo Schimmel 	struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
513c71006dSIdo Schimmel 
523c71006dSIdo Schimmel 	if (r->dst_len || r->src_len || r->tos)
533c71006dSIdo Schimmel 		return false;
543c71006dSIdo Schimmel 	return fib_rule_matchall(rule);
553c71006dSIdo Schimmel }
563c71006dSIdo Schimmel 
573c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule)
583c71006dSIdo Schimmel {
593c71006dSIdo Schimmel 	if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
603c71006dSIdo Schimmel 	    rule->l3mdev)
613c71006dSIdo Schimmel 		return false;
623c71006dSIdo Schimmel 	if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
633c71006dSIdo Schimmel 	    rule->table != RT_TABLE_DEFAULT)
643c71006dSIdo Schimmel 		return false;
653c71006dSIdo Schimmel 	return true;
663c71006dSIdo Schimmel }
673c71006dSIdo Schimmel EXPORT_SYMBOL_GPL(fib4_rule_default);
683c71006dSIdo Schimmel 
69b7a59557SJiri Pirko int fib4_rules_dump(struct net *net, struct notifier_block *nb,
70b7a59557SJiri Pirko 		    struct netlink_ext_ack *extack)
711b2a4440SIdo Schimmel {
72b7a59557SJiri Pirko 	return fib_rules_dump(net, nb, AF_INET, extack);
731b2a4440SIdo Schimmel }
741b2a4440SIdo Schimmel 
751b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net)
761b2a4440SIdo Schimmel {
771b2a4440SIdo Schimmel 	return fib_rules_seq_read(net, AF_INET);
781b2a4440SIdo Schimmel }
791b2a4440SIdo Schimmel 
800eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp,
810eeb075fSAndy Gospodarek 		 struct fib_result *res, unsigned int flags)
82e1ef4bf2SThomas Graf {
83e1ef4bf2SThomas Graf 	struct fib_lookup_arg arg = {
84e1ef4bf2SThomas Graf 		.result = res,
850eeb075fSAndy Gospodarek 		.flags = flags,
86e1ef4bf2SThomas Graf 	};
87e1ef4bf2SThomas Graf 	int err;
88e1ef4bf2SThomas Graf 
899ee0034bSDavid Ahern 	/* update flow if oif or iif point to device enslaved to l3mdev */
909ee0034bSDavid Ahern 	l3mdev_update_flow(net, flowi4_to_flowi(flp));
919ee0034bSDavid Ahern 
9222bd5b9bSDavid S. Miller 	err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
9385b91b03SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
9485b91b03SDavid S. Miller 	if (arg.rule)
9585b91b03SDavid S. Miller 		res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
9685b91b03SDavid S. Miller 	else
9785b91b03SDavid S. Miller 		res->tclassid = 0;
9885b91b03SDavid S. Miller #endif
9949dd18baSPanu Matilainen 
10049dd18baSPanu Matilainen 	if (err == -ESRCH)
10149dd18baSPanu Matilainen 		err = -ENETUNREACH;
10249dd18baSPanu Matilainen 
1031da177e4SLinus Torvalds 	return err;
1041da177e4SLinus Torvalds }
105f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup);
1061da177e4SLinus Torvalds 
107b9aaec8fSBrian Vazquez INDIRECT_CALLABLE_SCOPE int fib4_rule_action(struct fib_rule *rule,
108b9aaec8fSBrian Vazquez 					     struct flowi *flp, int flags,
109b9aaec8fSBrian Vazquez 					     struct fib_lookup_arg *arg)
110e1ef4bf2SThomas Graf {
111e1ef4bf2SThomas Graf 	int err = -EAGAIN;
112e1ef4bf2SThomas Graf 	struct fib_table *tbl;
11396c63fa7SDavid Ahern 	u32 tb_id;
114e1ef4bf2SThomas Graf 
115e1ef4bf2SThomas Graf 	switch (rule->action) {
116e1ef4bf2SThomas Graf 	case FR_ACT_TO_TBL:
117e1ef4bf2SThomas Graf 		break;
118e1ef4bf2SThomas Graf 
119e1ef4bf2SThomas Graf 	case FR_ACT_UNREACHABLE:
120345e9b54SAlexander Duyck 		return -ENETUNREACH;
121e1ef4bf2SThomas Graf 
122e1ef4bf2SThomas Graf 	case FR_ACT_PROHIBIT:
123345e9b54SAlexander Duyck 		return -EACCES;
124e1ef4bf2SThomas Graf 
125e1ef4bf2SThomas Graf 	case FR_ACT_BLACKHOLE:
126e1ef4bf2SThomas Graf 	default:
127345e9b54SAlexander Duyck 		return -EINVAL;
128e1ef4bf2SThomas Graf 	}
129e1ef4bf2SThomas Graf 
130345e9b54SAlexander Duyck 	rcu_read_lock();
131e1ef4bf2SThomas Graf 
13296c63fa7SDavid Ahern 	tb_id = fib_rule_get_table(rule, arg);
13396c63fa7SDavid Ahern 	tbl = fib_get_table(rule->fr_net, tb_id);
134345e9b54SAlexander Duyck 	if (tbl)
135345e9b54SAlexander Duyck 		err = fib_table_lookup(tbl, &flp->u.ip4,
136345e9b54SAlexander Duyck 				       (struct fib_result *)arg->result,
137345e9b54SAlexander Duyck 				       arg->flags);
138345e9b54SAlexander Duyck 
139345e9b54SAlexander Duyck 	rcu_read_unlock();
140e1ef4bf2SThomas Graf 	return err;
141e1ef4bf2SThomas Graf }
142e1ef4bf2SThomas Graf 
143b9aaec8fSBrian Vazquez INDIRECT_CALLABLE_SCOPE bool fib4_rule_suppress(struct fib_rule *rule,
144cdef4852Smsizanoen1 						int flags,
145b9aaec8fSBrian Vazquez 						struct fib_lookup_arg *arg)
1467764a45aSStefan Tomanek {
1476ef94cfaSStefan Tomanek 	struct fib_result *result = (struct fib_result *) arg->result;
148673498b8SStefan Tomanek 	struct net_device *dev = NULL;
149673498b8SStefan Tomanek 
1505481d73fSDavid Ahern 	if (result->fi) {
151dcb1ecb5SDavid Ahern 		struct fib_nh_common *nhc = fib_info_nhc(result->fi, 0);
1525481d73fSDavid Ahern 
153dcb1ecb5SDavid Ahern 		dev = nhc->nhc_dev;
1545481d73fSDavid Ahern 	}
1556ef94cfaSStefan Tomanek 
1567764a45aSStefan Tomanek 	/* do not accept result if the route does
1577764a45aSStefan Tomanek 	 * not meet the required prefix length
1587764a45aSStefan Tomanek 	 */
15973f5698eSStefan Tomanek 	if (result->prefixlen <= rule->suppress_prefixlen)
1606ef94cfaSStefan Tomanek 		goto suppress_route;
1616ef94cfaSStefan Tomanek 
1626ef94cfaSStefan Tomanek 	/* do not accept result if the route uses a device
1636ef94cfaSStefan Tomanek 	 * belonging to a forbidden interface group
1646ef94cfaSStefan Tomanek 	 */
1656ef94cfaSStefan Tomanek 	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
1666ef94cfaSStefan Tomanek 		goto suppress_route;
1676ef94cfaSStefan Tomanek 
1686ef94cfaSStefan Tomanek 	return false;
1696ef94cfaSStefan Tomanek 
1706ef94cfaSStefan Tomanek suppress_route:
1717764a45aSStefan Tomanek 	if (!(arg->flags & FIB_LOOKUP_NOREF))
1727764a45aSStefan Tomanek 		fib_info_put(result->fi);
1737764a45aSStefan Tomanek 	return true;
1747764a45aSStefan Tomanek }
175e1ef4bf2SThomas Graf 
176b9aaec8fSBrian Vazquez INDIRECT_CALLABLE_SCOPE int fib4_rule_match(struct fib_rule *rule,
177b9aaec8fSBrian Vazquez 					    struct flowi *fl, int flags)
178e1ef4bf2SThomas Graf {
179e1ef4bf2SThomas Graf 	struct fib4_rule *r = (struct fib4_rule *) rule;
1809ade2286SDavid S. Miller 	struct flowi4 *fl4 = &fl->u.ip4;
1819ade2286SDavid S. Miller 	__be32 daddr = fl4->daddr;
1829ade2286SDavid S. Miller 	__be32 saddr = fl4->saddr;
183e1ef4bf2SThomas Graf 
184e1ef4bf2SThomas Graf 	if (((saddr ^ r->src) & r->srcmask) ||
185e1ef4bf2SThomas Graf 	    ((daddr ^ r->dst) & r->dstmask))
186e1ef4bf2SThomas Graf 		return 0;
187e1ef4bf2SThomas Graf 
1889ade2286SDavid S. Miller 	if (r->tos && (r->tos != fl4->flowi4_tos))
189e1ef4bf2SThomas Graf 		return 0;
190e1ef4bf2SThomas Graf 
1914a2d73a4SRoopa Prabhu 	if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
1924a2d73a4SRoopa Prabhu 		return 0;
1934a2d73a4SRoopa Prabhu 
1944a2d73a4SRoopa Prabhu 	if (fib_rule_port_range_set(&rule->sport_range) &&
1954a2d73a4SRoopa Prabhu 	    !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
1964a2d73a4SRoopa Prabhu 		return 0;
1974a2d73a4SRoopa Prabhu 
1984a2d73a4SRoopa Prabhu 	if (fib_rule_port_range_set(&rule->dport_range) &&
1994a2d73a4SRoopa Prabhu 	    !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
2004a2d73a4SRoopa Prabhu 		return 0;
2014a2d73a4SRoopa Prabhu 
202e1ef4bf2SThomas Graf 	return 1;
203e1ef4bf2SThomas Graf }
2041da177e4SLinus Torvalds 
2058ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net)
2061da177e4SLinus Torvalds {
20758075ff5SYueHaibing 	u32 id = 1;
2081da177e4SLinus Torvalds 
20958075ff5SYueHaibing 	while (1) {
21051456b29SIan Morris 		if (!fib_get_table(net, id))
2118ad4942cSDenis V. Lunev 			return fib_new_table(net, id);
21258075ff5SYueHaibing 
21358075ff5SYueHaibing 		if (id++ == RT_TABLE_MAX)
21458075ff5SYueHaibing 			break;
21558075ff5SYueHaibing 	}
2161da177e4SLinus Torvalds 	return NULL;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
219ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
2201f6c9557SThomas Graf 	FRA_GENERIC_POLICY,
221e1ef4bf2SThomas Graf 	[FRA_FLOW]	= { .type = NLA_U32 },
2221da177e4SLinus Torvalds };
2231da177e4SLinus Torvalds 
224e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
2258b3521eeSRami Rosen 			       struct fib_rule_hdr *frh,
226b16fb418SRoopa Prabhu 			       struct nlattr **tb,
227b16fb418SRoopa Prabhu 			       struct netlink_ext_ack *extack)
2281da177e4SLinus Torvalds {
2293b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
230e1ef4bf2SThomas Graf 	int err = -EINVAL;
231e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
2321da177e4SLinus Torvalds 
233b16fb418SRoopa Prabhu 	if (frh->tos & ~IPTOS_TOS_MASK) {
234b16fb418SRoopa Prabhu 		NL_SET_ERR_MSG(extack, "Invalid tos");
235e1ef4bf2SThomas Graf 		goto errout;
236b16fb418SRoopa Prabhu 	}
237e1ef4bf2SThomas Graf 
2380ddcf43dSAlexander Duyck 	/* split local/main if they are not already split */
2390ddcf43dSAlexander Duyck 	err = fib_unmerge(net);
2400ddcf43dSAlexander Duyck 	if (err)
2410ddcf43dSAlexander Duyck 		goto errout;
2420ddcf43dSAlexander Duyck 
24396c63fa7SDavid Ahern 	if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
244e1ef4bf2SThomas Graf 		if (rule->action == FR_ACT_TO_TBL) {
245e1ef4bf2SThomas Graf 			struct fib_table *table;
246e1ef4bf2SThomas Graf 
247e4e4971cSDenis V. Lunev 			table = fib_empty_table(net);
24851456b29SIan Morris 			if (!table) {
249e1ef4bf2SThomas Graf 				err = -ENOBUFS;
250e1ef4bf2SThomas Graf 				goto errout;
251e1ef4bf2SThomas Graf 			}
252e1ef4bf2SThomas Graf 
253e1ef4bf2SThomas Graf 			rule->table = table->tb_id;
254e1ef4bf2SThomas Graf 		}
255e1ef4bf2SThomas Graf 	}
256e1ef4bf2SThomas Graf 
257e1701c68SThomas Graf 	if (frh->src_len)
25867b61f6cSJiri Benc 		rule4->src = nla_get_in_addr(tb[FRA_SRC]);
259e1ef4bf2SThomas Graf 
260e1701c68SThomas Graf 	if (frh->dst_len)
26167b61f6cSJiri Benc 		rule4->dst = nla_get_in_addr(tb[FRA_DST]);
262e1ef4bf2SThomas Graf 
263c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
2647a9bc9b8SDavid S. Miller 	if (tb[FRA_FLOW]) {
265e1ef4bf2SThomas Graf 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
2667a9bc9b8SDavid S. Miller 		if (rule4->tclassid)
267*213f5f8fSEric Dumazet 			atomic_inc(&net->ipv4.fib_num_tclassid_users);
2687a9bc9b8SDavid S. Miller 	}
2691da177e4SLinus Torvalds #endif
2701da177e4SLinus Torvalds 
271e37b1e97SRoopa Prabhu 	if (fib_rule_requires_fldissect(rule))
272e37b1e97SRoopa Prabhu 		net->ipv4.fib_rules_require_fldissect++;
273e37b1e97SRoopa Prabhu 
274e1ef4bf2SThomas Graf 	rule4->src_len = frh->src_len;
275e1ef4bf2SThomas Graf 	rule4->srcmask = inet_make_mask(rule4->src_len);
276e1ef4bf2SThomas Graf 	rule4->dst_len = frh->dst_len;
277e1ef4bf2SThomas Graf 	rule4->dstmask = inet_make_mask(rule4->dst_len);
278e1ef4bf2SThomas Graf 	rule4->tos = frh->tos;
279e1ef4bf2SThomas Graf 
280f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = true;
281104616e7SScott Feldman 
282e1ef4bf2SThomas Graf 	err = 0;
283e1ef4bf2SThomas Graf errout:
284e1ef4bf2SThomas Graf 	return err;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2870ddcf43dSAlexander Duyck static int fib4_rule_delete(struct fib_rule *rule)
2887a9bc9b8SDavid S. Miller {
289f4530fa5SDavid S. Miller 	struct net *net = rule->fr_net;
2900ddcf43dSAlexander Duyck 	int err;
2917a9bc9b8SDavid S. Miller 
2920ddcf43dSAlexander Duyck 	/* split local/main if they are not already split */
2930ddcf43dSAlexander Duyck 	err = fib_unmerge(net);
2940ddcf43dSAlexander Duyck 	if (err)
2950ddcf43dSAlexander Duyck 		goto errout;
2960ddcf43dSAlexander Duyck 
2970ddcf43dSAlexander Duyck #ifdef CONFIG_IP_ROUTE_CLASSID
2980ddcf43dSAlexander Duyck 	if (((struct fib4_rule *)rule)->tclassid)
299*213f5f8fSEric Dumazet 		atomic_dec(&net->ipv4.fib_num_tclassid_users);
3007a9bc9b8SDavid S. Miller #endif
301f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = true;
302e37b1e97SRoopa Prabhu 
303e37b1e97SRoopa Prabhu 	if (net->ipv4.fib_rules_require_fldissect &&
304e37b1e97SRoopa Prabhu 	    fib_rule_requires_fldissect(rule))
305e37b1e97SRoopa Prabhu 		net->ipv4.fib_rules_require_fldissect--;
3060ddcf43dSAlexander Duyck errout:
3070ddcf43dSAlexander Duyck 	return err;
3087a9bc9b8SDavid S. Miller }
3097a9bc9b8SDavid S. Miller 
310e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
311e1ef4bf2SThomas Graf 			     struct nlattr **tb)
312a5cdc030SPatrick McHardy {
313e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
314a5cdc030SPatrick McHardy 
315e1ef4bf2SThomas Graf 	if (frh->src_len && (rule4->src_len != frh->src_len))
316e1ef4bf2SThomas Graf 		return 0;
317e1ef4bf2SThomas Graf 
318e1ef4bf2SThomas Graf 	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
319e1ef4bf2SThomas Graf 		return 0;
320e1ef4bf2SThomas Graf 
321e1ef4bf2SThomas Graf 	if (frh->tos && (rule4->tos != frh->tos))
322e1ef4bf2SThomas Graf 		return 0;
323e1ef4bf2SThomas Graf 
324c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
325e1ef4bf2SThomas Graf 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
326e1ef4bf2SThomas Graf 		return 0;
327e1ef4bf2SThomas Graf #endif
328e1ef4bf2SThomas Graf 
32967b61f6cSJiri Benc 	if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
330e1ef4bf2SThomas Graf 		return 0;
331e1ef4bf2SThomas Graf 
33267b61f6cSJiri Benc 	if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
333e1ef4bf2SThomas Graf 		return 0;
334e1ef4bf2SThomas Graf 
335e1ef4bf2SThomas Graf 	return 1;
336a5cdc030SPatrick McHardy }
337a5cdc030SPatrick McHardy 
338e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
33904af8cf6SRami Rosen 			  struct fib_rule_hdr *frh)
3401da177e4SLinus Torvalds {
341e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
3421da177e4SLinus Torvalds 
343e1ef4bf2SThomas Graf 	frh->dst_len = rule4->dst_len;
344e1ef4bf2SThomas Graf 	frh->src_len = rule4->src_len;
345e1ef4bf2SThomas Graf 	frh->tos = rule4->tos;
3461da177e4SLinus Torvalds 
347f3756b79SDavid S. Miller 	if ((rule4->dst_len &&
348930345eaSJiri Benc 	     nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
349f3756b79SDavid S. Miller 	    (rule4->src_len &&
350930345eaSJiri Benc 	     nla_put_in_addr(skb, FRA_SRC, rule4->src)))
351f3756b79SDavid S. Miller 		goto nla_put_failure;
352c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID
353f3756b79SDavid S. Miller 	if (rule4->tclassid &&
354f3756b79SDavid S. Miller 	    nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
355f3756b79SDavid S. Miller 		goto nla_put_failure;
356e1ef4bf2SThomas Graf #endif
357e1ef4bf2SThomas Graf 	return 0;
358e1ef4bf2SThomas Graf 
359e1ef4bf2SThomas Graf nla_put_failure:
360e1ef4bf2SThomas Graf 	return -ENOBUFS;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
364339bf98fSThomas Graf {
365339bf98fSThomas Graf 	return nla_total_size(4) /* dst */
366339bf98fSThomas Graf 	       + nla_total_size(4) /* src */
367339bf98fSThomas Graf 	       + nla_total_size(4); /* flow */
368339bf98fSThomas Graf }
369339bf98fSThomas Graf 
370ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
37173417f61SThomas Graf {
372bafa6d9dSNicolas Dichtel 	rt_cache_flush(ops->fro_net);
37373417f61SThomas Graf }
37473417f61SThomas Graf 
37504a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
37625239ceeSPatrick McHardy 	.family		= AF_INET,
377e1ef4bf2SThomas Graf 	.rule_size	= sizeof(struct fib4_rule),
378e1701c68SThomas Graf 	.addr_size	= sizeof(u32),
379e1ef4bf2SThomas Graf 	.action		= fib4_rule_action,
3807764a45aSStefan Tomanek 	.suppress	= fib4_rule_suppress,
381e1ef4bf2SThomas Graf 	.match		= fib4_rule_match,
382e1ef4bf2SThomas Graf 	.configure	= fib4_rule_configure,
3837a9bc9b8SDavid S. Miller 	.delete		= fib4_rule_delete,
384e1ef4bf2SThomas Graf 	.compare	= fib4_rule_compare,
385e1ef4bf2SThomas Graf 	.fill		= fib4_rule_fill,
386339bf98fSThomas Graf 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
38773417f61SThomas Graf 	.flush_cache	= fib4_rule_flush_cache,
388e1ef4bf2SThomas Graf 	.nlgroup	= RTNLGRP_IPV4_RULE,
389e1ef4bf2SThomas Graf 	.policy		= fib4_rule_policy,
390e1ef4bf2SThomas Graf 	.owner		= THIS_MODULE,
391e1ef4bf2SThomas Graf };
392e1ef4bf2SThomas Graf 
393e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops)
3942994c638SDenis V. Lunev {
3952994c638SDenis V. Lunev 	int err;
3962994c638SDenis V. Lunev 
3975adef180SPatrick McHardy 	err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
3982994c638SDenis V. Lunev 	if (err < 0)
3992994c638SDenis V. Lunev 		return err;
400e4e4971cSDenis V. Lunev 	err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
4012994c638SDenis V. Lunev 	if (err < 0)
4022994c638SDenis V. Lunev 		return err;
403e4e4971cSDenis V. Lunev 	err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
4042994c638SDenis V. Lunev 	if (err < 0)
4052994c638SDenis V. Lunev 		return err;
4062994c638SDenis V. Lunev 	return 0;
4072994c638SDenis V. Lunev }
4082994c638SDenis V. Lunev 
4097b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net)
410e1ef4bf2SThomas Graf {
411dbb50165SDenis V. Lunev 	int err;
412e4e4971cSDenis V. Lunev 	struct fib_rules_ops *ops;
413dbb50165SDenis V. Lunev 
414e9c5158aSEric W. Biederman 	ops = fib_rules_register(&fib4_rules_ops_template, net);
415e9c5158aSEric W. Biederman 	if (IS_ERR(ops))
416e9c5158aSEric W. Biederman 		return PTR_ERR(ops);
417e4e4971cSDenis V. Lunev 
418e4e4971cSDenis V. Lunev 	err = fib_default_rules_init(ops);
419dbb50165SDenis V. Lunev 	if (err < 0)
420dbb50165SDenis V. Lunev 		goto fail;
421e4e4971cSDenis V. Lunev 	net->ipv4.rules_ops = ops;
422f4530fa5SDavid S. Miller 	net->ipv4.fib_has_custom_rules = false;
423e37b1e97SRoopa Prabhu 	net->ipv4.fib_rules_require_fldissect = 0;
424dbb50165SDenis V. Lunev 	return 0;
425dbb50165SDenis V. Lunev 
426dbb50165SDenis V. Lunev fail:
427dbb50165SDenis V. Lunev 	/* also cleans all rules already added */
4289e3a5487SDenis V. Lunev 	fib_rules_unregister(ops);
429dbb50165SDenis V. Lunev 	return err;
430e1ef4bf2SThomas Graf }
4317b1a74fdSDenis V. Lunev 
4327b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net)
4337b1a74fdSDenis V. Lunev {
4349e3a5487SDenis V. Lunev 	fib_rules_unregister(net->ipv4.rules_ops);
4357b1a74fdSDenis V. Lunev }
436