blob: ea3035b4e3e8d078e2b3c421aca48c2db0d51b7a [file] [log] [blame]
Thomas Graf101367c2006-08-04 03:39:02 -07001/*
2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
3 *
4 * Copyright (C)2003-2006 Helsinki University of Technology
5 * Copyright (C)2003-2006 USAGI/WIDE Project
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
10 *
11 * Authors
12 * Thomas Graf <tgraf@suug.ch>
13 * Ville Nuorvala <vnuorval@tcs.hut.fi>
14 */
15
Thomas Graf101367c2006-08-04 03:39:02 -070016#include <linux/netdevice.h>
17
18#include <net/fib_rules.h>
19#include <net/ipv6.h>
20#include <net/ip6_route.h>
21#include <net/netlink.h>
22
23struct fib6_rule
24{
25 struct fib_rule common;
26 struct rt6key src;
27 struct rt6key dst;
28 u8 tclass;
29};
30
31static struct fib_rules_ops fib6_rules_ops;
32
33static struct fib6_rule main_rule = {
34 .common = {
35 .refcnt = ATOMIC_INIT(2),
36 .pref = 0x7FFE,
37 .action = FR_ACT_TO_TBL,
38 .table = RT6_TABLE_MAIN,
39 },
40};
41
42static struct fib6_rule local_rule = {
43 .common = {
44 .refcnt = ATOMIC_INIT(2),
45 .pref = 0,
46 .action = FR_ACT_TO_TBL,
47 .table = RT6_TABLE_LOCAL,
48 .flags = FIB_RULE_PERMANENT,
49 },
50};
51
52static LIST_HEAD(fib6_rules);
53
54struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
55 pol_lookup_t lookup)
56{
57 struct fib_lookup_arg arg = {
58 .lookup_ptr = lookup,
59 };
60
61 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
62 if (arg.rule)
63 fib_rule_put(arg.rule);
64
Ville Nuorvalab1429552006-08-08 16:44:17 -070065 if (arg.result)
YOSHIFUJI Hideaki40aa7b92006-10-19 13:50:09 +090066 return arg.result;
Ville Nuorvalab1429552006-08-08 16:44:17 -070067
68 dst_hold(&ip6_null_entry.u.dst);
69 return &ip6_null_entry.u.dst;
Thomas Graf101367c2006-08-04 03:39:02 -070070}
71
Adrian Bunk8ce11e62006-08-07 21:50:48 -070072static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
73 int flags, struct fib_lookup_arg *arg)
Thomas Graf101367c2006-08-04 03:39:02 -070074{
75 struct rt6_info *rt = NULL;
76 struct fib6_table *table;
77 pol_lookup_t lookup = arg->lookup_ptr;
78
79 switch (rule->action) {
80 case FR_ACT_TO_TBL:
81 break;
82 case FR_ACT_UNREACHABLE:
83 rt = &ip6_null_entry;
84 goto discard_pkt;
85 default:
86 case FR_ACT_BLACKHOLE:
87 rt = &ip6_blk_hole_entry;
88 goto discard_pkt;
89 case FR_ACT_PROHIBIT:
90 rt = &ip6_prohibit_entry;
91 goto discard_pkt;
92 }
93
94 table = fib6_get_table(rule->table);
95 if (table)
96 rt = lookup(table, flp, flags);
97
98 if (rt != &ip6_null_entry)
99 goto out;
Thomas Graf101367c2006-08-04 03:39:02 -0700100 dst_release(&rt->u.dst);
Patrick McHardy3226f6882006-08-06 22:24:08 -0700101 rt = NULL;
102 goto out;
103
Thomas Graf101367c2006-08-04 03:39:02 -0700104discard_pkt:
105 dst_hold(&rt->u.dst);
106out:
107 arg->result = rt;
108 return rt == NULL ? -EAGAIN : 0;
109}
110
111
112static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
113{
114 struct fib6_rule *r = (struct fib6_rule *) rule;
115
Thomas Grafadaa70b2006-10-13 15:01:03 -0700116 if (r->dst.plen &&
117 !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
Thomas Graf101367c2006-08-04 03:39:02 -0700118 return 0;
119
Thomas Grafadaa70b2006-10-13 15:01:03 -0700120 if (r->src.plen) {
121 if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
122 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
123 return 0;
124 }
Thomas Graf101367c2006-08-04 03:39:02 -0700125
YOSHIFUJI Hideaki2cc67cc2006-08-21 19:18:57 +0900126 if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
127 return 0;
128
Thomas Graf101367c2006-08-04 03:39:02 -0700129 return 1;
130}
131
YOSHIFUJI Hideaki2613aad2006-08-25 16:05:00 -0700132static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800133 FRA_GENERIC_POLICY,
Thomas Graf101367c2006-08-04 03:39:02 -0700134};
135
136static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
137 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
138 struct nlattr **tb)
139{
140 int err = -EINVAL;
141 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
142
Thomas Graf101367c2006-08-04 03:39:02 -0700143 if (rule->action == FR_ACT_TO_TBL) {
144 if (rule->table == RT6_TABLE_UNSPEC)
145 goto errout;
146
147 if (fib6_new_table(rule->table) == NULL) {
148 err = -ENOBUFS;
149 goto errout;
150 }
151 }
152
Thomas Grafe1701c62007-03-24 12:46:02 -0700153 if (frh->src_len)
Thomas Graf101367c2006-08-04 03:39:02 -0700154 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
155 sizeof(struct in6_addr));
156
Thomas Grafe1701c62007-03-24 12:46:02 -0700157 if (frh->dst_len)
Thomas Graf101367c2006-08-04 03:39:02 -0700158 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
159 sizeof(struct in6_addr));
160
161 rule6->src.plen = frh->src_len;
162 rule6->dst.plen = frh->dst_len;
163 rule6->tclass = frh->tos;
164
165 err = 0;
166errout:
167 return err;
168}
169
170static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
171 struct nlattr **tb)
172{
173 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
174
175 if (frh->src_len && (rule6->src.plen != frh->src_len))
176 return 0;
177
178 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
179 return 0;
180
181 if (frh->tos && (rule6->tclass != frh->tos))
182 return 0;
183
Thomas Grafe1701c62007-03-24 12:46:02 -0700184 if (frh->src_len &&
Thomas Graf101367c2006-08-04 03:39:02 -0700185 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
186 return 0;
187
Thomas Grafe1701c62007-03-24 12:46:02 -0700188 if (frh->dst_len &&
Thomas Graf101367c2006-08-04 03:39:02 -0700189 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
190 return 0;
191
192 return 1;
193}
194
195static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
196 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
197{
198 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
199
200 frh->family = AF_INET6;
201 frh->dst_len = rule6->dst.plen;
202 frh->src_len = rule6->src.plen;
203 frh->tos = rule6->tclass;
204
205 if (rule6->dst.plen)
206 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
207 &rule6->dst.addr);
208
209 if (rule6->src.plen)
210 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
211 &rule6->src.addr);
212
Thomas Graf101367c2006-08-04 03:39:02 -0700213 return 0;
214
215nla_put_failure:
216 return -ENOBUFS;
217}
218
219int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
220{
221 return fib_rules_dump(skb, cb, AF_INET6);
222}
223
224static u32 fib6_rule_default_pref(void)
225{
226 return 0x3FFF;
227}
228
Thomas Graf339bf982006-11-10 14:10:15 -0800229static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
230{
231 return nla_total_size(16) /* dst */
232 + nla_total_size(16); /* src */
233}
234
Thomas Graf101367c2006-08-04 03:39:02 -0700235static struct fib_rules_ops fib6_rules_ops = {
236 .family = AF_INET6,
237 .rule_size = sizeof(struct fib6_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700238 .addr_size = sizeof(struct in6_addr),
Thomas Graf101367c2006-08-04 03:39:02 -0700239 .action = fib6_rule_action,
240 .match = fib6_rule_match,
241 .configure = fib6_rule_configure,
242 .compare = fib6_rule_compare,
243 .fill = fib6_rule_fill,
244 .default_pref = fib6_rule_default_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800245 .nlmsg_payload = fib6_rule_nlmsg_payload,
Thomas Graf101367c2006-08-04 03:39:02 -0700246 .nlgroup = RTNLGRP_IPV6_RULE,
247 .policy = fib6_rule_policy,
248 .rules_list = &fib6_rules,
249 .owner = THIS_MODULE,
250};
251
252void __init fib6_rules_init(void)
253{
254 list_add_tail(&local_rule.common.list, &fib6_rules);
255 list_add_tail(&main_rule.common.list, &fib6_rules);
256
257 fib_rules_register(&fib6_rules_ops);
258}
259
260void fib6_rules_cleanup(void)
261{
262 fib_rules_unregister(&fib6_rules_ops);
263}