fib6_rules.c 6.6 KB
Newer Older
T
Thomas Graf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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>

#include <net/fib_rules.h>
#include <net/ipv6.h>
20
#include <net/addrconf.h>
T
Thomas Graf 已提交
21 22 23 24 25 26 27 28 29 30 31
#include <net/ip6_route.h>
#include <net/netlink.h>

struct fib6_rule
{
	struct fib_rule		common;
	struct rt6key		src;
	struct rt6key		dst;
	u8			tclass;
};

32
static struct fib_rules_ops *fib6_rules_ops;
T
Thomas Graf 已提交
33

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

41
	fib_rules_lookup(fib6_rules_ops, fl, flags, &arg);
T
Thomas Graf 已提交
42 43 44
	if (arg.rule)
		fib_rule_put(arg.rule);

45
	if (arg.result)
46
		return arg.result;
47 48 49

	dst_hold(&ip6_null_entry.u.dst);
	return &ip6_null_entry.u.dst;
T
Thomas Graf 已提交
50 51
}

A
Adrian Bunk 已提交
52 53
static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
			    int flags, struct fib_lookup_arg *arg)
T
Thomas Graf 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
	struct rt6_info *rt = NULL;
	struct fib6_table *table;
	pol_lookup_t lookup = arg->lookup_ptr;

	switch (rule->action) {
	case FR_ACT_TO_TBL:
		break;
	case FR_ACT_UNREACHABLE:
		rt = &ip6_null_entry;
		goto discard_pkt;
	default:
	case FR_ACT_BLACKHOLE:
		rt = &ip6_blk_hole_entry;
		goto discard_pkt;
	case FR_ACT_PROHIBIT:
		rt = &ip6_prohibit_entry;
		goto discard_pkt;
	}

74
	table = fib6_get_table(&init_net, rule->table);
T
Thomas Graf 已提交
75 76 77
	if (table)
		rt = lookup(table, flp, flags);

78 79 80 81 82 83 84 85 86 87
	if (rt != &ip6_null_entry) {
		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;
88 89
			if (ipv6_dev_get_saddr(ip6_dst_idev(&rt->u.dst)->dev,
					       &flp->fl6_dst, &saddr))
90 91 92 93 94 95
				goto again;
			if (!ipv6_prefix_equal(&saddr, &r->src.addr,
					       r->src.plen))
				goto again;
			ipv6_addr_copy(&flp->fl6_src, &saddr);
		}
T
Thomas Graf 已提交
96
		goto out;
97 98
	}
again:
T
Thomas Graf 已提交
99
	dst_release(&rt->u.dst);
100 101 102
	rt = NULL;
	goto out;

T
Thomas Graf 已提交
103 104 105 106 107 108 109 110 111 112 113 114
discard_pkt:
	dst_hold(&rt->u.dst);
out:
	arg->result = rt;
	return rt == NULL ? -EAGAIN : 0;
}


static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
{
	struct fib6_rule *r = (struct fib6_rule *) rule;

115 116
	if (r->dst.plen &&
	    !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
T
Thomas Graf 已提交
117 118
		return 0;

119 120 121 122 123
	/*
	 * If FIB_RULE_FIND_SADDR is set and we do not have a
	 * source address for the traffic, we defer check for
	 * source address.
	 */
124
	if (r->src.plen) {
125 126 127 128 129
		if (flags & RT6_LOOKUP_F_HAS_SADDR) {
			if (!ipv6_prefix_equal(&fl->fl6_src, &r->src.addr,
					       r->src.plen))
				return 0;
		} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
130 131
			return 0;
	}
T
Thomas Graf 已提交
132

133 134 135
	if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
		return 0;

T
Thomas Graf 已提交
136 137 138
	return 1;
}

139
static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
140
	FRA_GENERIC_POLICY,
T
Thomas Graf 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
};

static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
			       struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
			       struct nlattr **tb)
{
	int err = -EINVAL;
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

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

154
		if (fib6_new_table(&init_net, rule->table) == NULL) {
T
Thomas Graf 已提交
155 156 157 158 159
			err = -ENOBUFS;
			goto errout;
		}
	}

160
	if (frh->src_len)
T
Thomas Graf 已提交
161 162 163
		nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
			   sizeof(struct in6_addr));

164
	if (frh->dst_len)
T
Thomas Graf 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		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;

191
	if (frh->src_len &&
T
Thomas Graf 已提交
192 193 194
	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
		return 0;

195
	if (frh->dst_len &&
T
Thomas Graf 已提交
196 197 198 199 200 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
	    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,
			  struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
{
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

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

	if (rule6->dst.plen)
		NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
			&rule6->dst.addr);

	if (rule6->src.plen)
		NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
			&rule6->src.addr);

	return 0;

nla_put_failure:
	return -ENOBUFS;
}

226
static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
T
Thomas Graf 已提交
227 228 229 230
{
	return 0x3FFF;
}

231 232 233 234 235 236
static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
{
	return nla_total_size(16) /* dst */
	       + nla_total_size(16); /* src */
}

237
static struct fib_rules_ops fib6_rules_ops_template = {
T
Thomas Graf 已提交
238 239
	.family			= AF_INET6,
	.rule_size		= sizeof(struct fib6_rule),
240
	.addr_size		= sizeof(struct in6_addr),
T
Thomas Graf 已提交
241 242 243 244 245 246
	.action			= fib6_rule_action,
	.match			= fib6_rule_match,
	.configure		= fib6_rule_configure,
	.compare		= fib6_rule_compare,
	.fill			= fib6_rule_fill,
	.default_pref		= fib6_rule_default_pref,
247
	.nlmsg_payload		= fib6_rule_nlmsg_payload,
T
Thomas Graf 已提交
248 249 250
	.nlgroup		= RTNLGRP_IPV6_RULE,
	.policy			= fib6_rule_policy,
	.owner			= THIS_MODULE,
251
	.fro_net		= &init_net,
T
Thomas Graf 已提交
252 253
};

254
static int __init fib6_default_rules_init(void)
T
Thomas Graf 已提交
255
{
256 257
	int err;

258 259 260 261 262 263 264 265
	fib6_rules_ops = kmemdup(&fib6_rules_ops_template,
				 sizeof(*fib6_rules_ops), GFP_KERNEL);
	if (!fib6_rules_ops)
		return -ENOMEM;

	INIT_LIST_HEAD(&fib6_rules_ops->rules_list);

	err = fib_default_rule_add(fib6_rules_ops, 0,
266 267 268
				   RT6_TABLE_LOCAL, FIB_RULE_PERMANENT);
	if (err < 0)
		return err;
269
	err = fib_default_rule_add(fib6_rules_ops, 0x7FFE, RT6_TABLE_MAIN, 0);
270 271 272 273
	if (err < 0)
		return err;
	return 0;
}
T
Thomas Graf 已提交
274

275
int __init fib6_rules_init(void)
276
{
277 278 279 280 281 282
	int ret;

	ret = fib6_default_rules_init();
	if (ret)
		goto out;

283
	ret = fib_rules_register(fib6_rules_ops);
284 285 286 287 288 289
	if (ret)
		goto out_default_rules_init;
out:
	return ret;

out_default_rules_init:
290 291
	fib_rules_cleanup_ops(fib6_rules_ops);
	kfree(fib6_rules_ops);
292
	goto out;
T
Thomas Graf 已提交
293 294 295 296
}

void fib6_rules_cleanup(void)
{
297 298
	fib_rules_unregister(fib6_rules_ops);
	kfree(fib6_rules_ops);
T
Thomas Graf 已提交
299
}