Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 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 Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 14 | * Steve Whitehouse <steve@chygwyn.com> |
| 15 | * Updated for Thomas Graf's generic rules |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/netlink.h> |
| 21 | #include <linux/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 24 | #include <linux/list.h> |
| 25 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <net/neighbour.h> |
| 27 | #include <net/dst.h> |
| 28 | #include <net/flow.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 29 | #include <net/fib_rules.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <net/dn.h> |
| 31 | #include <net/dn_fib.h> |
| 32 | #include <net/dn_neigh.h> |
| 33 | #include <net/dn_dev.h> |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 34 | #include <net/dn_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 36 | static struct fib_rules_ops dn_fib_rules_ops; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct dn_fib_rule |
| 39 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 40 | struct fib_rule common; |
| 41 | unsigned char dst_len; |
| 42 | unsigned char src_len; |
| 43 | __le16 src; |
| 44 | __le16 srcmask; |
| 45 | __le16 dst; |
| 46 | __le16 dstmask; |
| 47 | __le16 srcmap; |
| 48 | u8 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static struct dn_fib_rule default_rule = { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 52 | .common = { |
| 53 | .refcnt = ATOMIC_INIT(2), |
| 54 | .pref = 0x7fff, |
| 55 | .table = RT_TABLE_MAIN, |
| 56 | .action = FR_ACT_TO_TBL, |
| 57 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 60 | |
| 61 | int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 63 | struct fib_lookup_arg arg = { |
| 64 | .result = res, |
| 65 | }; |
| 66 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 68 | err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg); |
| 69 | res->r = arg.rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | return err; |
| 72 | } |
| 73 | |
Adrian Bunk | 2aa7f36 | 2006-08-14 23:55:20 -0700 | [diff] [blame] | 74 | static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 75 | int flags, struct fib_lookup_arg *arg) |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 76 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 77 | int err = -EAGAIN; |
| 78 | struct dn_fib_table *tbl; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 79 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 80 | switch(rule->action) { |
| 81 | case FR_ACT_TO_TBL: |
| 82 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 84 | case FR_ACT_UNREACHABLE: |
| 85 | err = -ENETUNREACH; |
| 86 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 88 | case FR_ACT_PROHIBIT: |
| 89 | err = -EACCES; |
| 90 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 92 | case FR_ACT_BLACKHOLE: |
| 93 | default: |
| 94 | err = -EINVAL; |
| 95 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 98 | tbl = dn_fib_get_table(rule->table, 0); |
| 99 | if (tbl == NULL) |
| 100 | goto errout; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 101 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 102 | err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result); |
| 103 | if (err > 0) |
| 104 | err = -EAGAIN; |
| 105 | errout: |
| 106 | return err; |
| 107 | } |
| 108 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 109 | static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = { |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 110 | FRA_GENERIC_POLICY, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 114 | { |
| 115 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
Steven Whitehouse | 375d9d7 | 2006-11-07 15:09:17 -0800 | [diff] [blame] | 116 | __le16 daddr = fl->fld_dst; |
| 117 | __le16 saddr = fl->fld_src; |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 118 | |
| 119 | if (((saddr ^ r->src) & r->srcmask) || |
| 120 | ((daddr ^ r->dst) & r->dstmask)) |
| 121 | return 0; |
| 122 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 123 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 126 | static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 127 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 128 | struct nlattr **tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 130 | int err = -EINVAL; |
| 131 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 133 | if (frh->tos) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 134 | goto errout; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 135 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 136 | if (rule->table == RT_TABLE_UNSPEC) { |
| 137 | if (rule->action == FR_ACT_TO_TBL) { |
| 138 | struct dn_fib_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 140 | table = dn_fib_empty_table(); |
| 141 | if (table == NULL) { |
| 142 | err = -ENOBUFS; |
| 143 | goto errout; |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 146 | rule->table = table->n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 150 | if (frh->src_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 151 | r->src = nla_get_le16(tb[FRA_SRC]); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 152 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 153 | if (frh->dst_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 154 | r->dst = nla_get_le16(tb[FRA_DST]); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 155 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 156 | r->src_len = frh->src_len; |
| 157 | r->srcmask = dnet_make_mask(r->src_len); |
| 158 | r->dst_len = frh->dst_len; |
| 159 | r->dstmask = dnet_make_mask(r->dst_len); |
| 160 | err = 0; |
| 161 | errout: |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 166 | struct nlattr **tb) |
| 167 | { |
| 168 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 169 | |
| 170 | if (frh->src_len && (r->src_len != frh->src_len)) |
| 171 | return 0; |
| 172 | |
| 173 | if (frh->dst_len && (r->dst_len != frh->dst_len)) |
| 174 | return 0; |
| 175 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 176 | if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC]))) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 179 | if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST]))) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | |
| 182 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 185 | unsigned dnet_addr_type(__le16 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
| 187 | struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } }; |
| 188 | struct dn_fib_res res; |
| 189 | unsigned ret = RTN_UNICAST; |
Patrick McHardy | abcab26 | 2006-08-10 23:11:47 -0700 | [diff] [blame] | 190 | struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | res.r = NULL; |
| 193 | |
| 194 | if (tb) { |
| 195 | if (!tb->lookup(tb, &fl, &res)) { |
| 196 | ret = res.type; |
| 197 | dn_fib_res_put(&res); |
| 198 | } |
| 199 | } |
| 200 | return ret; |
| 201 | } |
| 202 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 203 | static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 204 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 206 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 208 | frh->family = AF_DECnet; |
| 209 | frh->dst_len = r->dst_len; |
| 210 | frh->src_len = r->src_len; |
| 211 | frh->tos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 213 | if (r->dst_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 214 | NLA_PUT_LE16(skb, FRA_DST, r->dst); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 215 | if (r->src_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 216 | NLA_PUT_LE16(skb, FRA_SRC, r->src); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | |
| 220 | nla_put_failure: |
| 221 | return -ENOBUFS; |
| 222 | } |
| 223 | |
| 224 | static u32 dn_fib_rule_default_pref(void) |
| 225 | { |
| 226 | struct list_head *pos; |
| 227 | struct fib_rule *rule; |
| 228 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 229 | if (!list_empty(&dn_fib_rules_ops.rules_list)) { |
| 230 | pos = dn_fib_rules_ops.rules_list.next; |
| 231 | if (pos->next != &dn_fib_rules_ops.rules_list) { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 232 | rule = list_entry(pos->next, struct fib_rule, list); |
| 233 | if (rule->pref) |
| 234 | return rule->pref - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 238 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 241 | static void dn_fib_rule_flush_cache(void) |
| 242 | { |
Thomas Graf | 4b19ca4 | 2007-03-28 14:18:52 -0700 | [diff] [blame] | 243 | dn_rt_cache_flush(-1); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 246 | static struct fib_rules_ops dn_fib_rules_ops = { |
| 247 | .family = AF_DECnet, |
| 248 | .rule_size = sizeof(struct dn_fib_rule), |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 249 | .addr_size = sizeof(u16), |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 250 | .action = dn_fib_rule_action, |
| 251 | .match = dn_fib_rule_match, |
| 252 | .configure = dn_fib_rule_configure, |
| 253 | .compare = dn_fib_rule_compare, |
| 254 | .fill = dn_fib_rule_fill, |
| 255 | .default_pref = dn_fib_rule_default_pref, |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 256 | .flush_cache = dn_fib_rule_flush_cache, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 257 | .nlgroup = RTNLGRP_DECnet_RULE, |
| 258 | .policy = dn_fib_rule_policy, |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 259 | .rules_list = LIST_HEAD_INIT(dn_fib_rules_ops.rules_list), |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 260 | .owner = THIS_MODULE, |
| 261 | }; |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | void __init dn_fib_rules_init(void) |
| 264 | { |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 265 | list_add_tail(&default_rule.common.list, |
| 266 | &dn_fib_rules_ops.rules_list); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 267 | fib_rules_register(&dn_fib_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void __exit dn_fib_rules_cleanup(void) |
| 271 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 272 | fib_rules_unregister(&dn_fib_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |