fib_rules.c 11.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		IPv4 Forwarding Information Base: policy rules.
 *
 * Version:	$Id: fib_rules.c,v 1.17 2001/10/31 21:55:54 davem Exp $
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Fixes:
 * 		Rani Assaf	:	local_rule cannot be deleted
 *		Marc Boucher	:	routing by fwmark
 */

#include <asm/uaccess.h>
#include <asm/system.h>
#include <linux/bitops.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/errno.h>
#include <linux/in.h>
#include <linux/inet.h>
35
#include <linux/inetdevice.h>
L
Linus Torvalds 已提交
36 37 38 39 40 41
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/proc_fs.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/init.h>
42 43
#include <linux/list.h>
#include <linux/rcupdate.h>
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55

#include <net/ip.h>
#include <net/protocol.h>
#include <net/route.h>
#include <net/tcp.h>
#include <net/sock.h>
#include <net/ip_fib.h>

#define FRprintk(a...)

struct fib_rule
{
56
	struct hlist_node hlist;
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	atomic_t	r_clntref;
	u32		r_preference;
	unsigned char	r_table;
	unsigned char	r_action;
	unsigned char	r_dst_len;
	unsigned char	r_src_len;
	u32		r_src;
	u32		r_srcmask;
	u32		r_dst;
	u32		r_dstmask;
	u32		r_srcmap;
	u8		r_flags;
	u8		r_tos;
#ifdef CONFIG_IP_ROUTE_FWMARK
	u32		r_fwmark;
#endif
	int		r_ifindex;
#ifdef CONFIG_NET_CLS_ROUTE
	__u32		r_tclassid;
#endif
	char		r_ifname[IFNAMSIZ];
	int		r_dead;
79
	struct		rcu_head rcu;
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
};

static struct fib_rule default_rule = {
	.r_clntref =	ATOMIC_INIT(2),
	.r_preference =	0x7FFF,
	.r_table =	RT_TABLE_DEFAULT,
	.r_action =	RTN_UNICAST,
};

static struct fib_rule main_rule = {
	.r_clntref =	ATOMIC_INIT(2),
	.r_preference =	0x7FFE,
	.r_table =	RT_TABLE_MAIN,
	.r_action =	RTN_UNICAST,
};

static struct fib_rule local_rule = {
	.r_clntref =	ATOMIC_INIT(2),
	.r_table =	RT_TABLE_LOCAL,
	.r_action =	RTN_UNICAST,
};

102
static struct hlist_head fib_rules;
103 104

/* writer func called from netlink -- rtnl_sem hold*/
L
Linus Torvalds 已提交
105

106 107
static void rtmsg_rule(int, struct fib_rule *);

L
Linus Torvalds 已提交
108 109 110 111
int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
	struct rtattr **rta = arg;
	struct rtmsg *rtm = NLMSG_DATA(nlh);
112 113
	struct fib_rule *r;
	struct hlist_node *node;
L
Linus Torvalds 已提交
114 115
	int err = -ESRCH;

116
	hlist_for_each_entry(r, node, &fib_rules, hlist) {
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
		    rtm->rtm_src_len == r->r_src_len &&
		    rtm->rtm_dst_len == r->r_dst_len &&
		    (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
		    rtm->rtm_tos == r->r_tos &&
#ifdef CONFIG_IP_ROUTE_FWMARK
		    (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
#endif
		    (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
		    (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
		    (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
		    (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
			err = -EPERM;
			if (r == &local_rule)
				break;

133
			hlist_del_rcu(&r->hlist);
L
Linus Torvalds 已提交
134
			r->r_dead = 1;
135
			rtmsg_rule(RTM_DELRULE, r);
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
			fib_rule_put(r);
			err = 0;
			break;
		}
	}
	return err;
}

/* Allocate new unique table id */

static struct fib_table *fib_empty_table(void)
{
	int id;

	for (id = 1; id <= RT_TABLE_MAX; id++)
		if (fib_tables[id] == NULL)
			return __fib_new_table(id);
	return NULL;
}

156 157 158 159 160 161
static inline void fib_rule_put_rcu(struct rcu_head *head)
{
	struct fib_rule *r = container_of(head, struct fib_rule, rcu);
	kfree(r);
}

L
Linus Torvalds 已提交
162 163 164 165
void fib_rule_put(struct fib_rule *r)
{
	if (atomic_dec_and_test(&r->r_clntref)) {
		if (r->r_dead)
166
			call_rcu(&r->rcu, fib_rule_put_rcu);
L
Linus Torvalds 已提交
167 168 169 170 171
		else
			printk("Freeing alive rule %p\n", r);
	}
}

172 173
/* writer func called from netlink -- rtnl_sem hold*/

L
Linus Torvalds 已提交
174 175 176 177
int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
	struct rtattr **rta = arg;
	struct rtmsg *rtm = NLMSG_DATA(nlh);
178 179
	struct fib_rule *r, *new_r, *last = NULL;
	struct hlist_node *node = NULL;
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	unsigned char table_id;

	if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
	    (rtm->rtm_tos & ~IPTOS_TOS_MASK))
		return -EINVAL;

	if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
		return -EINVAL;

	table_id = rtm->rtm_table;
	if (table_id == RT_TABLE_UNSPEC) {
		struct fib_table *table;
		if (rtm->rtm_type == RTN_UNICAST) {
			if ((table = fib_empty_table()) == NULL)
				return -ENOBUFS;
			table_id = table->tb_id;
		}
	}

199
	new_r = kzalloc(sizeof(*new_r), GFP_KERNEL);
L
Linus Torvalds 已提交
200 201
	if (!new_r)
		return -ENOMEM;
202

L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	if (rta[RTA_SRC-1])
		memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
	if (rta[RTA_DST-1])
		memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
	if (rta[RTA_GATEWAY-1])
		memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
	new_r->r_src_len = rtm->rtm_src_len;
	new_r->r_dst_len = rtm->rtm_dst_len;
	new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
	new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
	new_r->r_tos = rtm->rtm_tos;
#ifdef CONFIG_IP_ROUTE_FWMARK
	if (rta[RTA_PROTOINFO-1])
		memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
#endif
	new_r->r_action = rtm->rtm_type;
	new_r->r_flags = rtm->rtm_flags;
	if (rta[RTA_PRIORITY-1])
		memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
	new_r->r_table = table_id;
	if (rta[RTA_IIF-1]) {
		struct net_device *dev;
		rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
		new_r->r_ifindex = -1;
		dev = __dev_get_by_name(new_r->r_ifname);
		if (dev)
			new_r->r_ifindex = dev->ifindex;
	}
#ifdef CONFIG_NET_CLS_ROUTE
	if (rta[RTA_FLOW-1])
		memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
#endif
235
	r = container_of(fib_rules.first, struct fib_rule, hlist);
L
Linus Torvalds 已提交
236 237

	if (!new_r->r_preference) {
238 239
		if (r && r->hlist.next != NULL) {
			r = container_of(r->hlist.next, struct fib_rule, hlist);
L
Linus Torvalds 已提交
240 241 242 243 244
			if (r->r_preference)
				new_r->r_preference = r->r_preference - 1;
		}
	}

245
	hlist_for_each_entry(r, node, &fib_rules, hlist) {
L
Linus Torvalds 已提交
246 247
		if (r->r_preference > new_r->r_preference)
			break;
248
		last = r;
L
Linus Torvalds 已提交
249 250
	}
	atomic_inc(&new_r->r_clntref);
251 252 253 254 255 256

	if (last)
		hlist_add_after_rcu(&last->hlist, &new_r->hlist);
	else
		hlist_add_before_rcu(&new_r->hlist, &r->hlist);

257
	rtmsg_rule(RTM_NEWRULE, new_r);
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269
	return 0;
}

#ifdef CONFIG_NET_CLS_ROUTE
u32 fib_rules_tclass(struct fib_result *res)
{
	if (res->r)
		return res->r->r_tclassid;
	return 0;
}
#endif

270
/* callers should hold rtnl semaphore */
L
Linus Torvalds 已提交
271 272 273

static void fib_rules_detach(struct net_device *dev)
{
274
	struct hlist_node *node;
L
Linus Torvalds 已提交
275 276
	struct fib_rule *r;

277 278
	hlist_for_each_entry(r, node, &fib_rules, hlist) {
		if (r->r_ifindex == dev->ifindex)
L
Linus Torvalds 已提交
279
			r->r_ifindex = -1;
280

L
Linus Torvalds 已提交
281 282 283
	}
}

284 285
/* callers should hold rtnl semaphore */

L
Linus Torvalds 已提交
286 287
static void fib_rules_attach(struct net_device *dev)
{
288
	struct hlist_node *node;
L
Linus Torvalds 已提交
289 290
	struct fib_rule *r;

291 292
	hlist_for_each_entry(r, node, &fib_rules, hlist) {
		if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300 301
			r->r_ifindex = dev->ifindex;
	}
}

int fib_lookup(const struct flowi *flp, struct fib_result *res)
{
	int err;
	struct fib_rule *r, *policy;
	struct fib_table *tb;
302
	struct hlist_node *node;
L
Linus Torvalds 已提交
303 304 305 306 307 308

	u32 daddr = flp->fl4_dst;
	u32 saddr = flp->fl4_src;

FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
	NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src));
309 310 311 312

	rcu_read_lock();

	hlist_for_each_entry_rcu(r, node, &fib_rules, hlist) {
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
		if (((saddr^r->r_src) & r->r_srcmask) ||
		    ((daddr^r->r_dst) & r->r_dstmask) ||
		    (r->r_tos && r->r_tos != flp->fl4_tos) ||
#ifdef CONFIG_IP_ROUTE_FWMARK
		    (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) ||
#endif
		    (r->r_ifindex && r->r_ifindex != flp->iif))
			continue;

FRprintk("tb %d r %d ", r->r_table, r->r_action);
		switch (r->r_action) {
		case RTN_UNICAST:
			policy = r;
			break;
		case RTN_UNREACHABLE:
328
			rcu_read_unlock();
L
Linus Torvalds 已提交
329 330 331
			return -ENETUNREACH;
		default:
		case RTN_BLACKHOLE:
332
			rcu_read_unlock();
L
Linus Torvalds 已提交
333 334
			return -EINVAL;
		case RTN_PROHIBIT:
335
			rcu_read_unlock();
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345
			return -EACCES;
		}

		if ((tb = fib_get_table(r->r_table)) == NULL)
			continue;
		err = tb->tb_lookup(tb, flp, res);
		if (err == 0) {
			res->r = policy;
			if (policy)
				atomic_inc(&policy->r_clntref);
346
			rcu_read_unlock();
L
Linus Torvalds 已提交
347 348 349
			return 0;
		}
		if (err < 0 && err != -EAGAIN) {
350
			rcu_read_unlock();
L
Linus Torvalds 已提交
351 352 353 354
			return err;
		}
	}
FRprintk("FAILURE\n");
355
	rcu_read_unlock();
L
Linus Torvalds 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	return -ENETUNREACH;
}

void fib_select_default(const struct flowi *flp, struct fib_result *res)
{
	if (res->r && res->r->r_action == RTN_UNICAST &&
	    FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
		struct fib_table *tb;
		if ((tb = fib_get_table(res->r->r_table)) != NULL)
			tb->tb_select_default(tb, flp, res);
	}
}

static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
{
	struct net_device *dev = ptr;

	if (event == NETDEV_UNREGISTER)
		fib_rules_detach(dev);
	else if (event == NETDEV_REGISTER)
		fib_rules_attach(dev);
	return NOTIFY_DONE;
}


static struct notifier_block fib_rules_notifier = {
	.notifier_call =fib_rules_event,
};

static __inline__ int inet_fill_rule(struct sk_buff *skb,
				     struct fib_rule *r,
387
				     u32 pid, u32 seq, int event,
388
				     unsigned int flags)
L
Linus Torvalds 已提交
389 390 391 392 393
{
	struct rtmsg *rtm;
	struct nlmsghdr  *nlh;
	unsigned char	 *b = skb->tail;

394
	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	rtm = NLMSG_DATA(nlh);
	rtm->rtm_family = AF_INET;
	rtm->rtm_dst_len = r->r_dst_len;
	rtm->rtm_src_len = r->r_src_len;
	rtm->rtm_tos = r->r_tos;
#ifdef CONFIG_IP_ROUTE_FWMARK
	if (r->r_fwmark)
		RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
#endif
	rtm->rtm_table = r->r_table;
	rtm->rtm_protocol = 0;
	rtm->rtm_scope = 0;
	rtm->rtm_type = r->r_action;
	rtm->rtm_flags = r->r_flags;

	if (r->r_dst_len)
		RTA_PUT(skb, RTA_DST, 4, &r->r_dst);
	if (r->r_src_len)
		RTA_PUT(skb, RTA_SRC, 4, &r->r_src);
	if (r->r_ifname[0])
		RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
	if (r->r_preference)
		RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
	if (r->r_srcmap)
		RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap);
#ifdef CONFIG_NET_CLS_ROUTE
	if (r->r_tclassid)
		RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid);
#endif
	nlh->nlmsg_len = skb->tail - b;
	return skb->len;

nlmsg_failure:
rtattr_failure:
	skb_trim(skb, b - skb->data);
	return -1;
}

433 434
/* callers should hold rtnl semaphore */

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static void rtmsg_rule(int event, struct fib_rule *r)
{
	int size = NLMSG_SPACE(sizeof(struct rtmsg) + 128);
	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);

	if (!skb)
		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, ENOBUFS);
	else if (inet_fill_rule(skb, r, 0, 0, event, 0) < 0) {
		kfree_skb(skb);
		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, EINVAL);
	} else {
		netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_RULE, GFP_KERNEL);
	}
}

L
Linus Torvalds 已提交
450 451
int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
{
452
	int idx = 0;
L
Linus Torvalds 已提交
453 454
	int s_idx = cb->args[0];
	struct fib_rule *r;
455 456 457 458
	struct hlist_node *node;

	rcu_read_lock();
	hlist_for_each_entry(r, node, &fib_rules, hlist) {
L
Linus Torvalds 已提交
459
		if (idx < s_idx)
460
			goto next;
461 462 463
		if (inet_fill_rule(skb, r, NETLINK_CB(cb->skb).pid,
				   cb->nlh->nlmsg_seq,
				   RTM_NEWRULE, NLM_F_MULTI) < 0)
L
Linus Torvalds 已提交
464
			break;
465
next:
466
		idx++;
L
Linus Torvalds 已提交
467
	}
468
	rcu_read_unlock();
L
Linus Torvalds 已提交
469 470 471 472 473 474 475
	cb->args[0] = idx;

	return skb->len;
}

void __init fib_rules_init(void)
{
476 477 478 479
	INIT_HLIST_HEAD(&fib_rules);
	hlist_add_head(&local_rule.hlist, &fib_rules);
	hlist_add_after(&local_rule.hlist, &main_rule.hlist);
	hlist_add_after(&main_rule.hlist, &default_rule.hlist);
L
Linus Torvalds 已提交
480 481
	register_netdevice_notifier(&fib_rules_notifier);
}