fib6_rules.c 7.7 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
		}
T
Thomas Graf 已提交
107
		goto out;
108 109
	}
again:
A
Amerigo Wang 已提交
110
	ip6_rt_put(rt);
111
	err = -EAGAIN;
112 113 114
	rt = NULL;
	goto out;

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

122 123 124
static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
{
	struct rt6_info *rt = (struct rt6_info *) arg->result;
125
	struct net_device *dev = rt->rt6i_idev->dev;
126 127 128
	/* do not accept result if the route does
	 * not meet the required prefix length
	 */
129 130 131 132 133 134 135 136 137 138 139 140
	if (rt->rt6i_dst.plen < rule->table_prefixlen_min)
		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:
141 142 143
		ip6_rt_put(rt);
		return true;
}
T
Thomas Graf 已提交
144 145 146 147

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

150
	if (r->dst.plen &&
151
	    !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
T
Thomas Graf 已提交
152 153
		return 0;

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

168
	if (r->tclass && r->tclass != ((ntohl(fl6->flowlabel) >> 20) & 0xff))
169 170
		return 0;

T
Thomas Graf 已提交
171 172 173
	return 1;
}

174
static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
175
	FRA_GENERIC_POLICY,
T
Thomas Graf 已提交
176 177 178
};

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

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

190
		if (fib6_new_table(net, rule->table) == NULL) {
T
Thomas Graf 已提交
191 192 193 194 195
			err = -ENOBUFS;
			goto errout;
		}
	}

196
	if (frh->src_len)
T
Thomas Graf 已提交
197 198 199
		nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
			   sizeof(struct in6_addr));

200
	if (frh->dst_len)
T
Thomas Graf 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
			   sizeof(struct in6_addr));

	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;

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

231
	if (frh->dst_len &&
T
Thomas Graf 已提交
232 233 234 235 236 237 238
	    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,
239
			  struct fib_rule_hdr *frh)
T
Thomas Graf 已提交
240 241 242 243 244 245 246
{
	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 已提交
247 248 249 250 251 252 253
	if ((rule6->dst.plen &&
	     nla_put(skb, FRA_DST, sizeof(struct in6_addr),
		     &rule6->dst.addr)) ||
	    (rule6->src.plen &&
	     nla_put(skb, FRA_SRC, sizeof(struct in6_addr),
		     &rule6->src.addr)))
		goto nla_put_failure;
T
Thomas Graf 已提交
254 255 256 257 258 259
	return 0;

nla_put_failure:
	return -ENOBUFS;
}

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

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

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

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

294 295 296 297
	ops = fib_rules_register(&fib6_rules_ops_template, net);
	if (IS_ERR(ops))
		return PTR_ERR(ops);
	net->ipv6.fib6_rules_ops = ops;
298 299


300
	err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
301
				   RT6_TABLE_LOCAL, 0);
302 303
	if (err)
		goto out_fib6_rules_ops;
304

305 306 307
	err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
				   0x7FFE, RT6_TABLE_MAIN, 0);
	if (err)
308
		goto out_fib6_rules_ops;
309 310

out:
311
	return err;
312

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

318
static void __net_exit fib6_rules_net_exit(struct net *net)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	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 已提交
334 335
void fib6_rules_cleanup(void)
{
336
	unregister_pernet_subsys(&fib6_rules_net_ops);
T
Thomas Graf 已提交
337
}