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