]> Pileus Git - ~andy/linux/blob - net/ipv6/fib6_rules.c
fib_rules: add .suppress operation
[~andy/linux] / net / ipv6 / fib6_rules.c
1 /*
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
16 #include <linux/netdevice.h>
17 #include <linux/export.h>
18
19 #include <net/fib_rules.h>
20 #include <net/ipv6.h>
21 #include <net/addrconf.h>
22 #include <net/ip6_route.h>
23 #include <net/netlink.h>
24
25 struct fib6_rule {
26         struct fib_rule         common;
27         struct rt6key           src;
28         struct rt6key           dst;
29         u8                      tclass;
30 };
31
32 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
33                                    int flags, pol_lookup_t lookup)
34 {
35         struct fib_lookup_arg arg = {
36                 .lookup_ptr = lookup,
37                 .flags = FIB_LOOKUP_NOREF,
38         };
39
40         fib_rules_lookup(net->ipv6.fib6_rules_ops,
41                          flowi6_to_flowi(fl6), flags, &arg);
42
43         if (arg.result)
44                 return arg.result;
45
46         dst_hold(&net->ipv6.ip6_null_entry->dst);
47         return &net->ipv6.ip6_null_entry->dst;
48 }
49
50 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
51                             int flags, struct fib_lookup_arg *arg)
52 {
53         struct flowi6 *flp6 = &flp->u.ip6;
54         struct rt6_info *rt = NULL;
55         struct fib6_table *table;
56         struct net *net = rule->fr_net;
57         pol_lookup_t lookup = arg->lookup_ptr;
58
59         switch (rule->action) {
60         case FR_ACT_TO_TBL:
61                 break;
62         case FR_ACT_UNREACHABLE:
63                 rt = net->ipv6.ip6_null_entry;
64                 goto discard_pkt;
65         default:
66         case FR_ACT_BLACKHOLE:
67                 rt = net->ipv6.ip6_blk_hole_entry;
68                 goto discard_pkt;
69         case FR_ACT_PROHIBIT:
70                 rt = net->ipv6.ip6_prohibit_entry;
71                 goto discard_pkt;
72         }
73
74         table = fib6_get_table(net, rule->table);
75         if (table)
76                 rt = lookup(net, table, flp6, flags);
77
78         if (rt != net->ipv6.ip6_null_entry) {
79                 struct fib6_rule *r = (struct fib6_rule *)rule;
80
81                 /*
82                  * If we need to find a source address for this traffic,
83                  * we check the result if it meets requirement of the rule.
84                  */
85                 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
86                     r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
87                         struct in6_addr saddr;
88
89                         if (ipv6_dev_get_saddr(net,
90                                                ip6_dst_idev(&rt->dst)->dev,
91                                                &flp6->daddr,
92                                                rt6_flags2srcprefs(flags),
93                                                &saddr))
94                                 goto again;
95                         if (!ipv6_prefix_equal(&saddr, &r->src.addr,
96                                                r->src.plen))
97                                 goto again;
98                         flp6->saddr = saddr;
99                 }
100                 goto out;
101         }
102 again:
103         ip6_rt_put(rt);
104         rt = NULL;
105         goto out;
106
107 discard_pkt:
108         dst_hold(&rt->dst);
109 out:
110         arg->result = rt;
111         return rt == NULL ? -EAGAIN : 0;
112 }
113
114 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
115 {
116         struct rt6_info *rt = (struct rt6_info *) arg->result;
117         /* do not accept result if the route does
118          * not meet the required prefix length
119          */
120         if (rt->rt6i_dst.plen < rule->table_prefixlen_min) {
121                 ip6_rt_put(rt);
122                 return true;
123         }
124         return false;
125 }
126
127 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
128 {
129         struct fib6_rule *r = (struct fib6_rule *) rule;
130         struct flowi6 *fl6 = &fl->u.ip6;
131
132         if (r->dst.plen &&
133             !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
134                 return 0;
135
136         /*
137          * If FIB_RULE_FIND_SADDR is set and we do not have a
138          * source address for the traffic, we defer check for
139          * source address.
140          */
141         if (r->src.plen) {
142                 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
143                         if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
144                                                r->src.plen))
145                                 return 0;
146                 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
147                         return 0;
148         }
149
150         if (r->tclass && r->tclass != ((ntohl(fl6->flowlabel) >> 20) & 0xff))
151                 return 0;
152
153         return 1;
154 }
155
156 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
157         FRA_GENERIC_POLICY,
158 };
159
160 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
161                                struct fib_rule_hdr *frh,
162                                struct nlattr **tb)
163 {
164         int err = -EINVAL;
165         struct net *net = sock_net(skb->sk);
166         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
167
168         if (rule->action == FR_ACT_TO_TBL) {
169                 if (rule->table == RT6_TABLE_UNSPEC)
170                         goto errout;
171
172                 if (fib6_new_table(net, rule->table) == NULL) {
173                         err = -ENOBUFS;
174                         goto errout;
175                 }
176         }
177
178         if (frh->src_len)
179                 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
180                            sizeof(struct in6_addr));
181
182         if (frh->dst_len)
183                 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
184                            sizeof(struct in6_addr));
185
186         rule6->src.plen = frh->src_len;
187         rule6->dst.plen = frh->dst_len;
188         rule6->tclass = frh->tos;
189
190         err = 0;
191 errout:
192         return err;
193 }
194
195 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
196                              struct nlattr **tb)
197 {
198         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
199
200         if (frh->src_len && (rule6->src.plen != frh->src_len))
201                 return 0;
202
203         if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
204                 return 0;
205
206         if (frh->tos && (rule6->tclass != frh->tos))
207                 return 0;
208
209         if (frh->src_len &&
210             nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
211                 return 0;
212
213         if (frh->dst_len &&
214             nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
215                 return 0;
216
217         return 1;
218 }
219
220 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
221                           struct fib_rule_hdr *frh)
222 {
223         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
224
225         frh->dst_len = rule6->dst.plen;
226         frh->src_len = rule6->src.plen;
227         frh->tos = rule6->tclass;
228
229         if ((rule6->dst.plen &&
230              nla_put(skb, FRA_DST, sizeof(struct in6_addr),
231                      &rule6->dst.addr)) ||
232             (rule6->src.plen &&
233              nla_put(skb, FRA_SRC, sizeof(struct in6_addr),
234                      &rule6->src.addr)))
235                 goto nla_put_failure;
236         return 0;
237
238 nla_put_failure:
239         return -ENOBUFS;
240 }
241
242 static u32 fib6_rule_default_pref(struct fib_rules_ops *ops)
243 {
244         return 0x3FFF;
245 }
246
247 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
248 {
249         return nla_total_size(16) /* dst */
250                + nla_total_size(16); /* src */
251 }
252
253 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
254         .family                 = AF_INET6,
255         .rule_size              = sizeof(struct fib6_rule),
256         .addr_size              = sizeof(struct in6_addr),
257         .action                 = fib6_rule_action,
258         .match                  = fib6_rule_match,
259         .suppress               = fib6_rule_suppress,
260         .configure              = fib6_rule_configure,
261         .compare                = fib6_rule_compare,
262         .fill                   = fib6_rule_fill,
263         .default_pref           = fib6_rule_default_pref,
264         .nlmsg_payload          = fib6_rule_nlmsg_payload,
265         .nlgroup                = RTNLGRP_IPV6_RULE,
266         .policy                 = fib6_rule_policy,
267         .owner                  = THIS_MODULE,
268         .fro_net                = &init_net,
269 };
270
271 static int __net_init fib6_rules_net_init(struct net *net)
272 {
273         struct fib_rules_ops *ops;
274         int err = -ENOMEM;
275
276         ops = fib_rules_register(&fib6_rules_ops_template, net);
277         if (IS_ERR(ops))
278                 return PTR_ERR(ops);
279         net->ipv6.fib6_rules_ops = ops;
280
281
282         err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0,
283                                    RT6_TABLE_LOCAL, 0);
284         if (err)
285                 goto out_fib6_rules_ops;
286
287         err = fib_default_rule_add(net->ipv6.fib6_rules_ops,
288                                    0x7FFE, RT6_TABLE_MAIN, 0);
289         if (err)
290                 goto out_fib6_rules_ops;
291
292 out:
293         return err;
294
295 out_fib6_rules_ops:
296         fib_rules_unregister(ops);
297         goto out;
298 }
299
300 static void __net_exit fib6_rules_net_exit(struct net *net)
301 {
302         fib_rules_unregister(net->ipv6.fib6_rules_ops);
303 }
304
305 static struct pernet_operations fib6_rules_net_ops = {
306         .init = fib6_rules_net_init,
307         .exit = fib6_rules_net_exit,
308 };
309
310 int __init fib6_rules_init(void)
311 {
312         return register_pernet_subsys(&fib6_rules_net_ops);
313 }
314
315
316 void fib6_rules_cleanup(void)
317 {
318         unregister_pernet_subsys(&fib6_rules_net_ops);
319 }