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