xref: /linux/net/ipv4/fib_rules.c (revision 339bf98ffc6a8d8eb16fc532ac57ffbced2f8a68)
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>
291da177e4SLinus Torvalds #include <net/ip.h>
301da177e4SLinus Torvalds #include <net/route.h>
311da177e4SLinus Torvalds #include <net/tcp.h>
321da177e4SLinus Torvalds #include <net/ip_fib.h>
33e1ef4bf2SThomas Graf #include <net/fib_rules.h>
341da177e4SLinus Torvalds 
35e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops;
361da177e4SLinus Torvalds 
37e1ef4bf2SThomas Graf struct fib4_rule
381da177e4SLinus Torvalds {
39e1ef4bf2SThomas Graf 	struct fib_rule		common;
40e1ef4bf2SThomas Graf 	u8			dst_len;
41e1ef4bf2SThomas Graf 	u8			src_len;
42e1ef4bf2SThomas Graf 	u8			tos;
4381f7bf6cSAl Viro 	__be32			src;
4481f7bf6cSAl Viro 	__be32			srcmask;
4581f7bf6cSAl Viro 	__be32			dst;
4681f7bf6cSAl Viro 	__be32			dstmask;
471da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
48e1ef4bf2SThomas Graf 	u32			tclassid;
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds };
511da177e4SLinus Torvalds 
52e1ef4bf2SThomas Graf static struct fib4_rule default_rule = {
53e1ef4bf2SThomas Graf 	.common = {
54e1ef4bf2SThomas Graf 		.refcnt =	ATOMIC_INIT(2),
55e1ef4bf2SThomas Graf 		.pref =		0x7FFF,
56e1ef4bf2SThomas Graf 		.table =	RT_TABLE_DEFAULT,
57e1ef4bf2SThomas Graf 		.action =	FR_ACT_TO_TBL,
58e1ef4bf2SThomas Graf 	},
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
61e1ef4bf2SThomas Graf static struct fib4_rule main_rule = {
62e1ef4bf2SThomas Graf 	.common = {
63e1ef4bf2SThomas Graf 		.refcnt =	ATOMIC_INIT(2),
64e1ef4bf2SThomas Graf 		.pref =		0x7FFE,
65e1ef4bf2SThomas Graf 		.table =	RT_TABLE_MAIN,
66e1ef4bf2SThomas Graf 		.action =	FR_ACT_TO_TBL,
67e1ef4bf2SThomas Graf 	},
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
70e1ef4bf2SThomas Graf static struct fib4_rule local_rule = {
71e1ef4bf2SThomas Graf 	.common = {
72e1ef4bf2SThomas Graf 		.refcnt =	ATOMIC_INIT(2),
73e1ef4bf2SThomas Graf 		.table =	RT_TABLE_LOCAL,
74e1ef4bf2SThomas Graf 		.action =	FR_ACT_TO_TBL,
75e1ef4bf2SThomas Graf 		.flags =	FIB_RULE_PERMANENT,
76e1ef4bf2SThomas Graf 	},
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79e1ef4bf2SThomas Graf static LIST_HEAD(fib4_rules);
807b204afdSRobert Olsson 
81e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE
82e1ef4bf2SThomas Graf u32 fib_rules_tclass(struct fib_result *res)
831da177e4SLinus Torvalds {
84e1ef4bf2SThomas Graf 	return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
85e1ef4bf2SThomas Graf }
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 
88e1ef4bf2SThomas Graf int fib_lookup(struct flowi *flp, struct fib_result *res)
89e1ef4bf2SThomas Graf {
90e1ef4bf2SThomas Graf 	struct fib_lookup_arg arg = {
91e1ef4bf2SThomas Graf 		.result = res,
92e1ef4bf2SThomas Graf 	};
93e1ef4bf2SThomas Graf 	int err;
94e1ef4bf2SThomas Graf 
95e1ef4bf2SThomas Graf 	err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
96e1ef4bf2SThomas Graf 	res->r = arg.rule;
97e1ef4bf2SThomas Graf 
981da177e4SLinus Torvalds 	return err;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1018ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
1028ce11e6aSAdrian Bunk 			    int flags, struct fib_lookup_arg *arg)
103e1ef4bf2SThomas Graf {
104e1ef4bf2SThomas Graf 	int err = -EAGAIN;
105e1ef4bf2SThomas Graf 	struct fib_table *tbl;
106e1ef4bf2SThomas Graf 
107e1ef4bf2SThomas Graf 	switch (rule->action) {
108e1ef4bf2SThomas Graf 	case FR_ACT_TO_TBL:
109e1ef4bf2SThomas Graf 		break;
110e1ef4bf2SThomas Graf 
111e1ef4bf2SThomas Graf 	case FR_ACT_UNREACHABLE:
112e1ef4bf2SThomas Graf 		err = -ENETUNREACH;
113e1ef4bf2SThomas Graf 		goto errout;
114e1ef4bf2SThomas Graf 
115e1ef4bf2SThomas Graf 	case FR_ACT_PROHIBIT:
116e1ef4bf2SThomas Graf 		err = -EACCES;
117e1ef4bf2SThomas Graf 		goto errout;
118e1ef4bf2SThomas Graf 
119e1ef4bf2SThomas Graf 	case FR_ACT_BLACKHOLE:
120e1ef4bf2SThomas Graf 	default:
121e1ef4bf2SThomas Graf 		err = -EINVAL;
122e1ef4bf2SThomas Graf 		goto errout;
123e1ef4bf2SThomas Graf 	}
124e1ef4bf2SThomas Graf 
125e1ef4bf2SThomas Graf 	if ((tbl = fib_get_table(rule->table)) == NULL)
126e1ef4bf2SThomas Graf 		goto errout;
127e1ef4bf2SThomas Graf 
128e1ef4bf2SThomas Graf 	err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
129e1ef4bf2SThomas Graf 	if (err > 0)
130e1ef4bf2SThomas Graf 		err = -EAGAIN;
131e1ef4bf2SThomas Graf errout:
132e1ef4bf2SThomas Graf 	return err;
133e1ef4bf2SThomas Graf }
134e1ef4bf2SThomas Graf 
135e1ef4bf2SThomas Graf 
136e1ef4bf2SThomas Graf void fib_select_default(const struct flowi *flp, struct fib_result *res)
137e1ef4bf2SThomas Graf {
138e1ef4bf2SThomas Graf 	if (res->r && res->r->action == FR_ACT_TO_TBL &&
139e1ef4bf2SThomas Graf 	    FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
140e1ef4bf2SThomas Graf 		struct fib_table *tb;
141e1ef4bf2SThomas Graf 		if ((tb = fib_get_table(res->r->table)) != NULL)
142e1ef4bf2SThomas Graf 			tb->tb_select_default(tb, flp, res);
143e1ef4bf2SThomas Graf 	}
144e1ef4bf2SThomas Graf }
145e1ef4bf2SThomas Graf 
146e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
147e1ef4bf2SThomas Graf {
148e1ef4bf2SThomas Graf 	struct fib4_rule *r = (struct fib4_rule *) rule;
14981f7bf6cSAl Viro 	__be32 daddr = fl->fl4_dst;
15081f7bf6cSAl Viro 	__be32 saddr = fl->fl4_src;
151e1ef4bf2SThomas Graf 
152e1ef4bf2SThomas Graf 	if (((saddr ^ r->src) & r->srcmask) ||
153e1ef4bf2SThomas Graf 	    ((daddr ^ r->dst) & r->dstmask))
154e1ef4bf2SThomas Graf 		return 0;
155e1ef4bf2SThomas Graf 
156e1ef4bf2SThomas Graf 	if (r->tos && (r->tos != fl->fl4_tos))
157e1ef4bf2SThomas Graf 		return 0;
158e1ef4bf2SThomas Graf 
159e1ef4bf2SThomas Graf 	return 1;
160e1ef4bf2SThomas Graf }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static struct fib_table *fib_empty_table(void)
1631da177e4SLinus Torvalds {
1642dfe55b4SPatrick McHardy 	u32 id;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	for (id = 1; id <= RT_TABLE_MAX; id++)
1671af5a8c4SPatrick McHardy 		if (fib_get_table(id) == NULL)
1681af5a8c4SPatrick McHardy 			return fib_new_table(id);
1691da177e4SLinus Torvalds 	return NULL;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
172e1ef4bf2SThomas Graf static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
1731f6c9557SThomas Graf 	FRA_GENERIC_POLICY,
174e1ef4bf2SThomas Graf 	[FRA_SRC]	= { .type = NLA_U32 },
175e1ef4bf2SThomas Graf 	[FRA_DST]	= { .type = NLA_U32 },
176e1ef4bf2SThomas Graf 	[FRA_FLOW]	= { .type = NLA_U32 },
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds 
179e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
180e1ef4bf2SThomas Graf 			       struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
181e1ef4bf2SThomas Graf 			       struct nlattr **tb)
1821da177e4SLinus Torvalds {
183e1ef4bf2SThomas Graf 	int err = -EINVAL;
184e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1851da177e4SLinus Torvalds 
186e1ef4bf2SThomas Graf 	if (frh->src_len > 32 || frh->dst_len > 32 ||
187e1ef4bf2SThomas Graf 	    (frh->tos & ~IPTOS_TOS_MASK))
188e1ef4bf2SThomas Graf 		goto errout;
189e1ef4bf2SThomas Graf 
190e1ef4bf2SThomas Graf 	if (rule->table == RT_TABLE_UNSPEC) {
191e1ef4bf2SThomas Graf 		if (rule->action == FR_ACT_TO_TBL) {
192e1ef4bf2SThomas Graf 			struct fib_table *table;
193e1ef4bf2SThomas Graf 
194e1ef4bf2SThomas Graf 			table = fib_empty_table();
195e1ef4bf2SThomas Graf 			if (table == NULL) {
196e1ef4bf2SThomas Graf 				err = -ENOBUFS;
197e1ef4bf2SThomas Graf 				goto errout;
198e1ef4bf2SThomas Graf 			}
199e1ef4bf2SThomas Graf 
200e1ef4bf2SThomas Graf 			rule->table = table->tb_id;
201e1ef4bf2SThomas Graf 		}
202e1ef4bf2SThomas Graf 	}
203e1ef4bf2SThomas Graf 
204e1ef4bf2SThomas Graf 	if (tb[FRA_SRC])
20545d60b9eSAl Viro 		rule4->src = nla_get_be32(tb[FRA_SRC]);
206e1ef4bf2SThomas Graf 
207e1ef4bf2SThomas Graf 	if (tb[FRA_DST])
20845d60b9eSAl Viro 		rule4->dst = nla_get_be32(tb[FRA_DST]);
209e1ef4bf2SThomas Graf 
2101da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE
211e1ef4bf2SThomas Graf 	if (tb[FRA_FLOW])
212e1ef4bf2SThomas Graf 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
2131da177e4SLinus Torvalds #endif
2141da177e4SLinus Torvalds 
215e1ef4bf2SThomas Graf 	rule4->src_len = frh->src_len;
216e1ef4bf2SThomas Graf 	rule4->srcmask = inet_make_mask(rule4->src_len);
217e1ef4bf2SThomas Graf 	rule4->dst_len = frh->dst_len;
218e1ef4bf2SThomas Graf 	rule4->dstmask = inet_make_mask(rule4->dst_len);
219e1ef4bf2SThomas Graf 	rule4->tos = frh->tos;
220e1ef4bf2SThomas Graf 
221e1ef4bf2SThomas Graf 	err = 0;
222e1ef4bf2SThomas Graf errout:
223e1ef4bf2SThomas Graf 	return err;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
227e1ef4bf2SThomas Graf 			     struct nlattr **tb)
228a5cdc030SPatrick McHardy {
229e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
230a5cdc030SPatrick McHardy 
231e1ef4bf2SThomas Graf 	if (frh->src_len && (rule4->src_len != frh->src_len))
232e1ef4bf2SThomas Graf 		return 0;
233e1ef4bf2SThomas Graf 
234e1ef4bf2SThomas Graf 	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
235e1ef4bf2SThomas Graf 		return 0;
236e1ef4bf2SThomas Graf 
237e1ef4bf2SThomas Graf 	if (frh->tos && (rule4->tos != frh->tos))
238e1ef4bf2SThomas Graf 		return 0;
239e1ef4bf2SThomas Graf 
240e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE
241e1ef4bf2SThomas Graf 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
242e1ef4bf2SThomas Graf 		return 0;
243e1ef4bf2SThomas Graf #endif
244e1ef4bf2SThomas Graf 
24545d60b9eSAl Viro 	if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC])))
246e1ef4bf2SThomas Graf 		return 0;
247e1ef4bf2SThomas Graf 
24845d60b9eSAl Viro 	if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST])))
249e1ef4bf2SThomas Graf 		return 0;
250e1ef4bf2SThomas Graf 
251e1ef4bf2SThomas Graf 	return 1;
252a5cdc030SPatrick McHardy }
253a5cdc030SPatrick McHardy 
254e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
255e1ef4bf2SThomas Graf 			  struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
2561da177e4SLinus Torvalds {
257e1ef4bf2SThomas Graf 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
2581da177e4SLinus Torvalds 
259e1ef4bf2SThomas Graf 	frh->family = AF_INET;
260e1ef4bf2SThomas Graf 	frh->dst_len = rule4->dst_len;
261e1ef4bf2SThomas Graf 	frh->src_len = rule4->src_len;
262e1ef4bf2SThomas Graf 	frh->tos = rule4->tos;
2631da177e4SLinus Torvalds 
264e1ef4bf2SThomas Graf 	if (rule4->dst_len)
26545d60b9eSAl Viro 		NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
266e1ef4bf2SThomas Graf 
267e1ef4bf2SThomas Graf 	if (rule4->src_len)
26845d60b9eSAl Viro 		NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
269e1ef4bf2SThomas Graf 
270e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE
271e1ef4bf2SThomas Graf 	if (rule4->tclassid)
272e1ef4bf2SThomas Graf 		NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
273e1ef4bf2SThomas Graf #endif
274e1ef4bf2SThomas Graf 	return 0;
275e1ef4bf2SThomas Graf 
276e1ef4bf2SThomas Graf nla_put_failure:
277e1ef4bf2SThomas Graf 	return -ENOBUFS;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280e1ef4bf2SThomas Graf int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
2811da177e4SLinus Torvalds {
282e1ef4bf2SThomas Graf 	return fib_rules_dump(skb, cb, AF_INET);
283e1ef4bf2SThomas Graf }
284e1ef4bf2SThomas Graf 
285e1ef4bf2SThomas Graf static u32 fib4_rule_default_pref(void)
286e1ef4bf2SThomas Graf {
287e1ef4bf2SThomas Graf 	struct list_head *pos;
288e1ef4bf2SThomas Graf 	struct fib_rule *rule;
289e1ef4bf2SThomas Graf 
290e1ef4bf2SThomas Graf 	if (!list_empty(&fib4_rules)) {
291e1ef4bf2SThomas Graf 		pos = fib4_rules.next;
292e1ef4bf2SThomas Graf 		if (pos->next != &fib4_rules) {
293e1ef4bf2SThomas Graf 			rule = list_entry(pos->next, struct fib_rule, list);
294e1ef4bf2SThomas Graf 			if (rule->pref)
295e1ef4bf2SThomas Graf 				return rule->pref - 1;
296e1ef4bf2SThomas Graf 		}
297e1ef4bf2SThomas Graf 	}
298e1ef4bf2SThomas Graf 
299e1ef4bf2SThomas Graf 	return 0;
300e1ef4bf2SThomas Graf }
301e1ef4bf2SThomas Graf 
302*339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
303*339bf98fSThomas Graf {
304*339bf98fSThomas Graf 	return nla_total_size(4) /* dst */
305*339bf98fSThomas Graf 	       + nla_total_size(4) /* src */
306*339bf98fSThomas Graf 	       + nla_total_size(4); /* flow */
307*339bf98fSThomas Graf }
308*339bf98fSThomas Graf 
309e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops = {
310e1ef4bf2SThomas Graf 	.family		= AF_INET,
311e1ef4bf2SThomas Graf 	.rule_size	= sizeof(struct fib4_rule),
312e1ef4bf2SThomas Graf 	.action		= fib4_rule_action,
313e1ef4bf2SThomas Graf 	.match		= fib4_rule_match,
314e1ef4bf2SThomas Graf 	.configure	= fib4_rule_configure,
315e1ef4bf2SThomas Graf 	.compare	= fib4_rule_compare,
316e1ef4bf2SThomas Graf 	.fill		= fib4_rule_fill,
317e1ef4bf2SThomas Graf 	.default_pref	= fib4_rule_default_pref,
318*339bf98fSThomas Graf 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
319e1ef4bf2SThomas Graf 	.nlgroup	= RTNLGRP_IPV4_RULE,
320e1ef4bf2SThomas Graf 	.policy		= fib4_rule_policy,
321e1ef4bf2SThomas Graf 	.rules_list	= &fib4_rules,
322e1ef4bf2SThomas Graf 	.owner		= THIS_MODULE,
323e1ef4bf2SThomas Graf };
324e1ef4bf2SThomas Graf 
325e1ef4bf2SThomas Graf void __init fib4_rules_init(void)
326e1ef4bf2SThomas Graf {
327e1ef4bf2SThomas Graf 	list_add_tail(&local_rule.common.list, &fib4_rules);
328e1ef4bf2SThomas Graf 	list_add_tail(&main_rule.common.list, &fib4_rules);
329e1ef4bf2SThomas Graf 	list_add_tail(&default_rule.common.list, &fib4_rules);
330e1ef4bf2SThomas Graf 
331e1ef4bf2SThomas Graf 	fib_rules_register(&fib4_rules_ops);
332e1ef4bf2SThomas Graf }
333