[NET]: Make code static.
[linux-2.6/verdex.git] / net / ipv6 / fib6_rules.c
blobbf9bba83b852e06ea428e3729cf3516b1d36493a
1 /*
2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules
4 * Copyright (C)2003-2006 Helsinki University of Technology
5 * Copyright (C)2003-2006 USAGI/WIDE Project
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.
11 * Authors
12 * Thomas Graf <tgraf@suug.ch>
13 * Ville Nuorvala <vnuorval@tcs.hut.fi>
16 #include <linux/config.h>
17 #include <linux/netdevice.h>
19 #include <net/fib_rules.h>
20 #include <net/ipv6.h>
21 #include <net/ip6_route.h>
22 #include <net/netlink.h>
24 struct fib6_rule
26 struct fib_rule common;
27 struct rt6key src;
28 struct rt6key dst;
29 u8 tclass;
32 static struct fib_rules_ops fib6_rules_ops;
34 static struct fib6_rule main_rule = {
35 .common = {
36 .refcnt = ATOMIC_INIT(2),
37 .pref = 0x7FFE,
38 .action = FR_ACT_TO_TBL,
39 .table = RT6_TABLE_MAIN,
43 static struct fib6_rule local_rule = {
44 .common = {
45 .refcnt = ATOMIC_INIT(2),
46 .pref = 0,
47 .action = FR_ACT_TO_TBL,
48 .table = RT6_TABLE_LOCAL,
49 .flags = FIB_RULE_PERMANENT,
53 static LIST_HEAD(fib6_rules);
55 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
56 pol_lookup_t lookup)
58 struct fib_lookup_arg arg = {
59 .lookup_ptr = lookup,
62 fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
63 if (arg.rule)
64 fib_rule_put(arg.rule);
66 return (struct dst_entry *) arg.result;
69 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
70 int flags, struct fib_lookup_arg *arg)
72 struct rt6_info *rt = NULL;
73 struct fib6_table *table;
74 pol_lookup_t lookup = arg->lookup_ptr;
76 switch (rule->action) {
77 case FR_ACT_TO_TBL:
78 break;
79 case FR_ACT_UNREACHABLE:
80 rt = &ip6_null_entry;
81 goto discard_pkt;
82 default:
83 case FR_ACT_BLACKHOLE:
84 rt = &ip6_blk_hole_entry;
85 goto discard_pkt;
86 case FR_ACT_PROHIBIT:
87 rt = &ip6_prohibit_entry;
88 goto discard_pkt;
91 table = fib6_get_table(rule->table);
92 if (table)
93 rt = lookup(table, flp, flags);
95 if (rt != &ip6_null_entry)
96 goto out;
97 dst_release(&rt->u.dst);
98 rt = NULL;
99 goto out;
101 discard_pkt:
102 dst_hold(&rt->u.dst);
103 out:
104 arg->result = rt;
105 return rt == NULL ? -EAGAIN : 0;
109 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
111 struct fib6_rule *r = (struct fib6_rule *) rule;
113 if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
114 return 0;
116 if ((flags & RT6_F_HAS_SADDR) &&
117 !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
118 return 0;
120 return 1;
123 static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = {
124 [FRA_IFNAME] = { .type = NLA_STRING },
125 [FRA_PRIORITY] = { .type = NLA_U32 },
126 [FRA_SRC] = { .minlen = sizeof(struct in6_addr) },
127 [FRA_DST] = { .minlen = sizeof(struct in6_addr) },
130 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
131 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
132 struct nlattr **tb)
134 int err = -EINVAL;
135 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
137 if (frh->src_len > 128 || frh->dst_len > 128 ||
138 (frh->tos & ~IPV6_FLOWINFO_MASK))
139 goto errout;
141 if (rule->action == FR_ACT_TO_TBL) {
142 if (rule->table == RT6_TABLE_UNSPEC)
143 goto errout;
145 if (fib6_new_table(rule->table) == NULL) {
146 err = -ENOBUFS;
147 goto errout;
151 if (tb[FRA_SRC])
152 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
153 sizeof(struct in6_addr));
155 if (tb[FRA_DST])
156 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
157 sizeof(struct in6_addr));
159 rule6->src.plen = frh->src_len;
160 rule6->dst.plen = frh->dst_len;
161 rule6->tclass = frh->tos;
163 err = 0;
164 errout:
165 return err;
168 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
169 struct nlattr **tb)
171 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
173 if (frh->src_len && (rule6->src.plen != frh->src_len))
174 return 0;
176 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
177 return 0;
179 if (frh->tos && (rule6->tclass != frh->tos))
180 return 0;
182 if (tb[FRA_SRC] &&
183 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
184 return 0;
186 if (tb[FRA_DST] &&
187 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
188 return 0;
190 return 1;
193 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
194 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
196 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
198 frh->family = AF_INET6;
199 frh->dst_len = rule6->dst.plen;
200 frh->src_len = rule6->src.plen;
201 frh->tos = rule6->tclass;
203 if (rule6->dst.plen)
204 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
205 &rule6->dst.addr);
207 if (rule6->src.plen)
208 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
209 &rule6->src.addr);
211 return 0;
213 nla_put_failure:
214 return -ENOBUFS;
217 int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
219 return fib_rules_dump(skb, cb, AF_INET6);
222 static u32 fib6_rule_default_pref(void)
224 return 0x3FFF;
227 static struct fib_rules_ops fib6_rules_ops = {
228 .family = AF_INET6,
229 .rule_size = sizeof(struct fib6_rule),
230 .action = fib6_rule_action,
231 .match = fib6_rule_match,
232 .configure = fib6_rule_configure,
233 .compare = fib6_rule_compare,
234 .fill = fib6_rule_fill,
235 .default_pref = fib6_rule_default_pref,
236 .nlgroup = RTNLGRP_IPV6_RULE,
237 .policy = fib6_rule_policy,
238 .rules_list = &fib6_rules,
239 .owner = THIS_MODULE,
242 void __init fib6_rules_init(void)
244 list_add_tail(&local_rule.common.list, &fib6_rules);
245 list_add_tail(&main_rule.common.list, &fib6_rules);
247 fib_rules_register(&fib6_rules_ops);
250 void fib6_rules_cleanup(void)
252 fib_rules_unregister(&fib6_rules_ops);