fib6_rules.c 7.6 KB
Newer Older
T
Thomas Graf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
 *
 * Copyright (C)2003-2006 Helsinki University of Technology
 * Copyright (C)2003-2006 USAGI/WIDE Project
 *
 *	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, version 2.
 *
 * Authors
 *	Thomas Graf		<tgraf@suug.ch>
 *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
 */

#include <linux/netdevice.h>
17
#include <linux/export.h>
T
Thomas Graf 已提交
18 19 20

#include <net/fib_rules.h>
#include <net/ipv6.h>
21
#include <net/addrconf.h>
T
Thomas Graf 已提交
22 23 24
#include <net/ip6_route.h>
#include <net/netlink.h>

25
struct fib6_rule {
T
Thomas Graf 已提交
26 27 28 29 30 31
	struct fib_rule		common;
	struct rt6key		src;
	struct rt6key		dst;
	u8			tclass;
};

32
struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
33
				   int flags, pol_lookup_t lookup)
T
Thomas Graf 已提交
34 35 36
{
	struct fib_lookup_arg arg = {
		.lookup_ptr = lookup,
37
		.flags = FIB_LOOKUP_NOREF,
T
Thomas Graf 已提交
38 39
	};

40 41
	fib_rules_lookup(net->ipv6.fib6_rules_ops,
			 flowi6_to_flowi(fl6), flags, &arg);
T
Thomas Graf 已提交
42

43
	if (arg.result)
44
		return arg.result;
45

46 47
	dst_hold(&net->ipv6.ip6_null_entry->dst);
	return &net->ipv6.ip6_null_entry->dst;
T
Thomas Graf 已提交
48 49
}

A
Adrian Bunk 已提交
50 51
static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
			    int flags, struct fib_lookup_arg *arg)
T
Thomas Graf 已提交
52
{
53
	struct flowi6 *flp6 = &flp->u.ip6;
T
Thomas Graf 已提交
54 55
	struct rt6_info *rt = NULL;
	struct fib6_table *table;
56
	struct net *net = rule->fr_net;
T
Thomas Graf 已提交
57
	pol_lookup_t lookup = arg->lookup_ptr;
58
	int err = 0;
T
Thomas Graf 已提交
59 60 61 62 63

	switch (rule->action) {
	case FR_ACT_TO_TBL:
		break;
	case FR_ACT_UNREACHABLE:
64
		err = -ENETUNREACH;
65
		rt = net->ipv6.ip6_null_entry;
T
Thomas Graf 已提交
66 67 68
		goto discard_pkt;
	default:
	case FR_ACT_BLACKHOLE:
69
		err = -EINVAL;
70
		rt = net->ipv6.ip6_blk_hole_entry;
T
Thomas Graf 已提交
71 72
		goto discard_pkt;
	case FR_ACT_PROHIBIT:
73
		err = -EACCES;
74
		rt = net->ipv6.ip6_prohibit_entry;
T
Thomas Graf 已提交
75 76 77
		goto discard_pkt;
	}

78
	table = fib6_get_table(net, rule->table);
79 80 81 82
	if (!table) {
		err = -EAGAIN;
		goto out;
	}
T
Thomas Graf 已提交
83

84
	rt = lookup(net, table, flp6, flags);
85
	if (rt != net->ipv6.ip6_null_entry) {
86 87 88 89 90 91 92 93 94
		struct fib6_rule *r = (struct fib6_rule *)rule;

		/*
		 * If we need to find a source address for this traffic,
		 * we check the result if it meets requirement of the rule.
		 */
		if ((rule->flags & FIB_RULE_FIND_SADDR) &&
		    r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
			struct in6_addr saddr;
95

96
			if (ipv6_dev_get_saddr(net,
97
					       ip6_dst_idev(&rt->dst)->dev,
98
					       &flp6->daddr,
99
					       rt6_flags2srcprefs(flags),
100
					       &saddr))
101 102 103 104
				goto again;
			if (!ipv6_prefix_equal(&saddr, &r->src.addr,
					       r->src.plen))
				goto again;
A
Alexey Dobriyan 已提交
105
			flp6->saddr = saddr;
106
		}
107
		err = rt->dst.error;
T
Thomas Graf 已提交
108
		goto out;
109 110
	}
again:
A
Amerigo Wang 已提交
111
	ip6_rt_put(rt);
112
	err = -EAGAIN;
113 114 115
	rt = NULL;
	goto out;

T
Thomas Graf 已提交
116
discard_pkt:
117
	dst_hold(&rt->dst);
T
Thomas Graf 已提交
118 119
out:
	arg->result = rt;
120
	return err;
T
Thomas Graf 已提交
121 122
}

123 124 125
static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
{
	struct rt6_info *rt = (struct rt6_info *) arg->result;
126 127 128 129 130
	struct net_device *dev = NULL;

	if (rt->rt6i_idev)
		dev = rt->rt6i_idev->dev;

131 132 133
	/* do not accept result if the route does
	 * not meet the required prefix length
	 */
134
	if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
135 136 137 138 139 140 141 142 143 144 145
		goto suppress_route;

	/* do not accept result if the route uses a device
	 * belonging to a forbidden interface group
	 */
	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
		goto suppress_route;

	return false;

suppress_route:
S
Stefan Tomanek 已提交
146 147
	ip6_rt_put(rt);
	return true;
148
}
T
Thomas Graf 已提交
149 150 151 152

static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
{
	struct fib6_rule *r = (struct fib6_rule *) rule;
153
	struct flowi6 *fl6 = &fl->u.ip6;
T
Thomas Graf 已提交
154

155
	if (r->dst.plen &&
156
	    !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
T
Thomas Graf 已提交
157 158
		return 0;

159 160 161 162 163
	/*
	 * If FIB_RULE_FIND_SADDR is set and we do not have a
	 * source address for the traffic, we defer check for
	 * source address.
	 */
164
	if (r->src.plen) {
165
		if (flags & RT6_LOOKUP_F_HAS_SADDR) {
166
			if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
167 168 169
					       r->src.plen))
				return 0;
		} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
170 171
			return 0;
	}
T
Thomas Graf 已提交
172

173
	if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
174 175
		return 0;

T
Thomas Graf 已提交
176 177 178
	return 1;
}

179
static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
180
	FRA_GENERIC_POLICY,
T
Thomas Graf 已提交
181 182 183
};

static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
184
			       struct fib_rule_hdr *frh,
T
Thomas Graf 已提交
185 186 187
			       struct nlattr **tb)
{
	int err = -EINVAL;
188
	struct net *net = sock_net(skb->sk);
T
Thomas Graf 已提交
189 190 191 192 193 194
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

	if (rule->action == FR_ACT_TO_TBL) {
		if (rule->table == RT6_TABLE_UNSPEC)
			goto errout;

195
		if (fib6_new_table(net, rule->table) == NULL) {
T
Thomas Graf 已提交
196 197 198 199 200
			err = -ENOBUFS;
			goto errout;
		}
	}

201
	if (frh->src_len)
202
		rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
T
Thomas Graf 已提交
203

204
	if (frh->dst_len)
205
		rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
T
Thomas Graf 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	rule6->src.plen = frh->src_len;
	rule6->dst.plen = frh->dst_len;
	rule6->tclass = frh->tos;

	err = 0;
errout:
	return err;
}

static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
			     struct nlattr **tb)
{
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

	if (frh->src_len && (rule6->src.plen != frh->src_len))
		return 0;

	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
		return 0;

	if (frh->tos && (rule6->tclass != frh->tos))
		return 0;

230
	if (frh->src_len &&
T
Thomas Graf 已提交
231 232 233
	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
		return 0;

234
	if (frh->dst_len &&
T
Thomas Graf 已提交
235 236 237 238 239 240 241
	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
		return 0;

	return 1;
}

static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
242
			  struct fib_rule_hdr *frh)
T
Thomas Graf 已提交
243 244 245 246 247 248 249
{
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

	frh->dst_len = rule6->dst.plen;
	frh->src_len = rule6->src.plen;
	frh->tos = rule6->tclass;

D
David S. Miller 已提交
250
	if ((rule6->dst.plen &&
251
	     nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
D
David S. Miller 已提交
252
	    (rule6->src.plen &&
253
	     nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
D
David S. Miller 已提交
254
		goto nla_put_failure;
T
Thomas Graf 已提交
255 256 257 258 259 260
	return 0;

nla_put_failure:
	return -ENOBUFS;
}

261
static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
T
Thomas Graf 已提交
262 263 264 265
{
	return 0x3FFF;
}

266 267 268 269 270 271
static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
{
	return nla_total_size(16) /* dst */
	       + nla_total_size(16); /* src */
}

272
static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
273
	.family			= AF_INET6,
T
Thomas Graf 已提交
274
	.rule_size		= sizeof(struct fib6_rule),
275
	.addr_size		= sizeof(struct in6_addr),
T
Thomas Graf 已提交
276 277
	.action			= fib6_rule_action,
	.match			= fib6_rule_match,
278
	.suppress		= fib6_rule_suppress,
T
Thomas Graf 已提交
279 280 281 282
	.configure		= fib6_rule_configure,
	.compare		= fib6_rule_compare,
	.fill			= fib6_rule_fill,
	.default_pref		= fib6_rule_default_pref,
283
	.nlmsg_payload		= fib6_rule_nlmsg_payload,
T
Thomas Graf 已提交
284 285 286
	.nlgroup		= RTNLGRP_IPV6_RULE,
	.policy			= fib6_rule_policy,
	.owner			= THIS_MODULE,
287
	.fro_net		= &init_net,
T
Thomas Graf 已提交
288 289
};

290
static int __net_init fib6_rules_net_init(struct net *net)
T
Thomas Graf 已提交
291
{
292
	struct fib_rules_ops *ops;
293
	int err = -ENOMEM;
294

295 296 297
	ops = fib_rules_register(&fib6_rules_ops_template, net);
	if (IS_ERR(ops))
		return PTR_ERR(ops);
298

299
	err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
300 301
	if (err)
		goto out_fib6_rules_ops;
302

303
	err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
304
	if (err)
305
		goto out_fib6_rules_ops;
306

307
	net->ipv6.fib6_rules_ops = ops;
308
out:
309
	return err;
310

311
out_fib6_rules_ops:
312
	fib_rules_unregister(ops);
313
	goto out;
T
Thomas Graf 已提交
314 315
}

316
static void __net_exit fib6_rules_net_exit(struct net *net)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
	fib_rules_unregister(net->ipv6.fib6_rules_ops);
}

static struct pernet_operations fib6_rules_net_ops = {
	.init = fib6_rules_net_init,
	.exit = fib6_rules_net_exit,
};

int __init fib6_rules_init(void)
{
	return register_pernet_subsys(&fib6_rules_net_ops);
}


T
Thomas Graf 已提交
332 333
void fib6_rules_cleanup(void)
{
334
	unregister_pernet_subsys(&fib6_rules_net_ops);
T
Thomas Graf 已提交
335
}