blob: a09c09f5572d66f8fe29724233460d557069ca60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * DECnet Routing Forwarding Information Base (Rules)
8 *
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
11 *
12 *
13 * Changes:
Steven Whitehousea8731cb2006-08-09 15:56:46 -070014 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/netlink.h>
21#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/spinlock.h>
Steven Whitehouseecba3202006-03-20 22:43:28 -080024#include <linux/list.h>
25#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/neighbour.h>
27#include <net/dst.h>
28#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070029#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/dn.h>
31#include <net/dn_fib.h>
32#include <net/dn_neigh.h>
33#include <net/dn_dev.h>
34
Steven Whitehousea8731cb2006-08-09 15:56:46 -070035static struct fib_rules_ops dn_fib_rules_ops;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct dn_fib_rule
38{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070039 struct fib_rule common;
40 unsigned char dst_len;
41 unsigned char src_len;
42 __le16 src;
43 __le16 srcmask;
44 __le16 dst;
45 __le16 dstmask;
46 __le16 srcmap;
47 u8 flags;
Steven Whitehousea8731cb2006-08-09 15:56:46 -070048 u32 fwmark;
Patrick McHardy88e91f22006-08-25 16:11:08 -070049 u32 fwmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52static struct dn_fib_rule default_rule = {
Steven Whitehousea8731cb2006-08-09 15:56:46 -070053 .common = {
54 .refcnt = ATOMIC_INIT(2),
55 .pref = 0x7fff,
56 .table = RT_TABLE_MAIN,
57 .action = FR_ACT_TO_TBL,
58 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Steven Whitehousea8731cb2006-08-09 15:56:46 -070061static LIST_HEAD(dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Steven Whitehousea8731cb2006-08-09 15:56:46 -070063
64int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070066 struct fib_lookup_arg arg = {
67 .result = res,
68 };
69 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Steven Whitehousea8731cb2006-08-09 15:56:46 -070071 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
72 res->r = arg.rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return err;
75}
76
Adrian Bunk2aa7f362006-08-14 23:55:20 -070077static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
78 int flags, struct fib_lookup_arg *arg)
Steven Whitehouseecba3202006-03-20 22:43:28 -080079{
Steven Whitehousea8731cb2006-08-09 15:56:46 -070080 int err = -EAGAIN;
81 struct dn_fib_table *tbl;
Steven Whitehouseecba3202006-03-20 22:43:28 -080082
Steven Whitehousea8731cb2006-08-09 15:56:46 -070083 switch(rule->action) {
84 case FR_ACT_TO_TBL:
85 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Steven Whitehousea8731cb2006-08-09 15:56:46 -070087 case FR_ACT_UNREACHABLE:
88 err = -ENETUNREACH;
89 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Steven Whitehousea8731cb2006-08-09 15:56:46 -070091 case FR_ACT_PROHIBIT:
92 err = -EACCES;
93 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Steven Whitehousea8731cb2006-08-09 15:56:46 -070095 case FR_ACT_BLACKHOLE:
96 default:
97 err = -EINVAL;
98 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700101 tbl = dn_fib_get_table(rule->table, 0);
102 if (tbl == NULL)
103 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800104
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700105 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
106 if (err > 0)
107 err = -EAGAIN;
108errout:
109 return err;
110}
111
112static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf5176f912006-08-26 20:13:18 -0700113 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700114 [FRA_PRIORITY] = { .type = NLA_U32 },
115 [FRA_SRC] = { .type = NLA_U16 },
116 [FRA_DST] = { .type = NLA_U16 },
117 [FRA_FWMARK] = { .type = NLA_U32 },
Patrick McHardy88e91f22006-08-25 16:11:08 -0700118 [FRA_FWMASK] = { .type = NLA_U32 },
Steven Whitehoused8803092006-08-11 16:43:41 -0700119 [FRA_TABLE] = { .type = NLA_U32 },
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700120};
121
122static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
123{
124 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Steven Whitehouse375d9d72006-11-07 15:09:17 -0800125 __le16 daddr = fl->fld_dst;
126 __le16 saddr = fl->fld_src;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700127
128 if (((saddr ^ r->src) & r->srcmask) ||
129 ((daddr ^ r->dst) & r->dstmask))
130 return 0;
131
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800132 if ((r->fwmark ^ fl->mark) & r->fwmask)
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700135 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700138static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
139 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
140 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700142 int err = -EINVAL;
143 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700145 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
146 goto errout;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800147
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700148 if (rule->table == RT_TABLE_UNSPEC) {
149 if (rule->action == FR_ACT_TO_TBL) {
150 struct dn_fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700152 table = dn_fib_empty_table();
153 if (table == NULL) {
154 err = -ENOBUFS;
155 goto errout;
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700158 rule->table = table->n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 }
161
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700162 if (tb[FRA_SRC])
163 r->src = nla_get_u16(tb[FRA_SRC]);
164
165 if (tb[FRA_DST])
166 r->dst = nla_get_u16(tb[FRA_DST]);
167
Patrick McHardy88e91f22006-08-25 16:11:08 -0700168 if (tb[FRA_FWMARK]) {
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700169 r->fwmark = nla_get_u32(tb[FRA_FWMARK]);
Patrick McHardy88e91f22006-08-25 16:11:08 -0700170 if (r->fwmark)
171 /* compatibility: if the mark value is non-zero all bits
172 * are compared unless a mask is explicitly specified.
173 */
174 r->fwmask = 0xFFFFFFFF;
175 }
176
177 if (tb[FRA_FWMASK])
178 r->fwmask = nla_get_u32(tb[FRA_FWMASK]);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700179
180 r->src_len = frh->src_len;
181 r->srcmask = dnet_make_mask(r->src_len);
182 r->dst_len = frh->dst_len;
183 r->dstmask = dnet_make_mask(r->dst_len);
184 err = 0;
185errout:
186 return err;
187}
188
189static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
190 struct nlattr **tb)
191{
192 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
193
194 if (frh->src_len && (r->src_len != frh->src_len))
195 return 0;
196
197 if (frh->dst_len && (r->dst_len != frh->dst_len))
198 return 0;
199
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700200 if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK])))
201 return 0;
Patrick McHardy88e91f22006-08-25 16:11:08 -0700202
203 if (tb[FRA_FWMASK] && (r->fwmask != nla_get_u32(tb[FRA_FWMASK])))
204 return 0;
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700205
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700206 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700207 return 0;
208
Steven Whitehoused1aa62f2006-08-11 16:44:18 -0700209 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700210 return 0;
211
212 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800215unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
218 struct dn_fib_res res;
219 unsigned ret = RTN_UNICAST;
Patrick McHardyabcab262006-08-10 23:11:47 -0700220 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 res.r = NULL;
223
224 if (tb) {
225 if (!tb->lookup(tb, &fl, &res)) {
226 ret = res.type;
227 dn_fib_res_put(&res);
228 }
229 }
230 return ret;
231}
232
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700233static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
234 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700236 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700238 frh->family = AF_DECnet;
239 frh->dst_len = r->dst_len;
240 frh->src_len = r->src_len;
241 frh->tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700243 if (r->fwmark)
244 NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark);
Patrick McHardy88e91f22006-08-25 16:11:08 -0700245 if (r->fwmask || r->fwmark)
246 NLA_PUT_U32(skb, FRA_FWMASK, r->fwmask);
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700247 if (r->dst_len)
248 NLA_PUT_U16(skb, FRA_DST, r->dst);
249 if (r->src_len)
250 NLA_PUT_U16(skb, FRA_SRC, r->src);
251
252 return 0;
253
254nla_put_failure:
255 return -ENOBUFS;
256}
257
258static u32 dn_fib_rule_default_pref(void)
259{
260 struct list_head *pos;
261 struct fib_rule *rule;
262
263 if (!list_empty(&dn_fib_rules)) {
264 pos = dn_fib_rules.next;
265 if (pos->next != &dn_fib_rules) {
266 rule = list_entry(pos->next, struct fib_rule, list);
267 if (rule->pref)
268 return rule->pref - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
276{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700277 return fib_rules_dump(skb, cb, AF_DECnet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700280static struct fib_rules_ops dn_fib_rules_ops = {
281 .family = AF_DECnet,
282 .rule_size = sizeof(struct dn_fib_rule),
283 .action = dn_fib_rule_action,
284 .match = dn_fib_rule_match,
285 .configure = dn_fib_rule_configure,
286 .compare = dn_fib_rule_compare,
287 .fill = dn_fib_rule_fill,
288 .default_pref = dn_fib_rule_default_pref,
289 .nlgroup = RTNLGRP_DECnet_RULE,
290 .policy = dn_fib_rule_policy,
291 .rules_list = &dn_fib_rules,
292 .owner = THIS_MODULE,
293};
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295void __init dn_fib_rules_init(void)
296{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700297 list_add_tail(&default_rule.common.list, &dn_fib_rules);
298 fib_rules_register(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301void __exit dn_fib_rules_cleanup(void)
302{
Steven Whitehousea8731cb2006-08-09 15:56:46 -0700303 fib_rules_unregister(&dn_fib_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306