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 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 49 | u32 fwmark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static struct dn_fib_rule default_rule = { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 54 | .common = { |
| 55 | .refcnt = ATOMIC_INIT(2), |
| 56 | .pref = 0x7fff, |
| 57 | .table = RT_TABLE_MAIN, |
| 58 | .action = FR_ACT_TO_TBL, |
| 59 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 62 | static LIST_HEAD(dn_fib_rules); |
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 | |
| 65 | int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 67 | struct fib_lookup_arg arg = { |
| 68 | .result = res, |
| 69 | }; |
| 70 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 72 | err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg); |
| 73 | res->r = arg.rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | return err; |
| 76 | } |
| 77 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 78 | int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, int flags, |
| 79 | struct fib_lookup_arg *arg) |
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 | int err = -EAGAIN; |
| 82 | struct dn_fib_table *tbl; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 83 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 84 | switch(rule->action) { |
| 85 | case FR_ACT_TO_TBL: |
| 86 | break; |
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_UNREACHABLE: |
| 89 | err = -ENETUNREACH; |
| 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_PROHIBIT: |
| 93 | err = -EACCES; |
| 94 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 96 | case FR_ACT_BLACKHOLE: |
| 97 | default: |
| 98 | err = -EINVAL; |
| 99 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 102 | tbl = dn_fib_get_table(rule->table, 0); |
| 103 | if (tbl == NULL) |
| 104 | goto errout; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 105 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 106 | err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result); |
| 107 | if (err > 0) |
| 108 | err = -EAGAIN; |
| 109 | errout: |
| 110 | return err; |
| 111 | } |
| 112 | |
| 113 | static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = { |
| 114 | [FRA_IFNAME] = { .type = NLA_STRING }, |
| 115 | [FRA_PRIORITY] = { .type = NLA_U32 }, |
| 116 | [FRA_SRC] = { .type = NLA_U16 }, |
| 117 | [FRA_DST] = { .type = NLA_U16 }, |
| 118 | [FRA_FWMARK] = { .type = NLA_U32 }, |
| 119 | }; |
| 120 | |
| 121 | static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 122 | { |
| 123 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 124 | u16 daddr = fl->fld_dst; |
| 125 | u16 saddr = fl->fld_src; |
| 126 | |
| 127 | if (((saddr ^ r->src) & r->srcmask) || |
| 128 | ((daddr ^ r->dst) & r->dstmask)) |
| 129 | return 0; |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 132 | if (r->fwmark && (r->fwmark != fl->fld_fwmark)) |
| 133 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 136 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 139 | static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 140 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 141 | struct nlattr **tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 143 | int err = -EINVAL; |
| 144 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
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 | if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos) |
| 147 | goto errout; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 148 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 149 | if (rule->table == RT_TABLE_UNSPEC) { |
| 150 | if (rule->action == FR_ACT_TO_TBL) { |
| 151 | struct dn_fib_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 153 | table = dn_fib_empty_table(); |
| 154 | if (table == NULL) { |
| 155 | err = -ENOBUFS; |
| 156 | goto errout; |
| 157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 159 | rule->table = table->n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 163 | if (tb[FRA_SRC]) |
| 164 | r->src = nla_get_u16(tb[FRA_SRC]); |
| 165 | |
| 166 | if (tb[FRA_DST]) |
| 167 | r->dst = nla_get_u16(tb[FRA_DST]); |
| 168 | |
| 169 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 170 | if (tb[FRA_FWMARK]) |
| 171 | r->fwmark = nla_get_u32(tb[FRA_FWMARK]); |
| 172 | #endif |
| 173 | |
| 174 | r->src_len = frh->src_len; |
| 175 | r->srcmask = dnet_make_mask(r->src_len); |
| 176 | r->dst_len = frh->dst_len; |
| 177 | r->dstmask = dnet_make_mask(r->dst_len); |
| 178 | err = 0; |
| 179 | errout: |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 184 | struct nlattr **tb) |
| 185 | { |
| 186 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 187 | |
| 188 | if (frh->src_len && (r->src_len != frh->src_len)) |
| 189 | return 0; |
| 190 | |
| 191 | if (frh->dst_len && (r->dst_len != frh->dst_len)) |
| 192 | return 0; |
| 193 | |
| 194 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 195 | if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 196 | return 0; |
| 197 | #endif |
| 198 | |
| 199 | if (tb[FRA_SRC] && (r->src != nla_get_u32(tb[FRA_SRC]))) |
| 200 | return 0; |
| 201 | |
| 202 | if (tb[FRA_DST] && (r->dst != nla_get_u32(tb[FRA_DST]))) |
| 203 | return 0; |
| 204 | |
| 205 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 208 | unsigned dnet_addr_type(__le16 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } }; |
| 211 | struct dn_fib_res res; |
| 212 | unsigned ret = RTN_UNICAST; |
| 213 | struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL]; |
| 214 | |
| 215 | res.r = NULL; |
| 216 | |
| 217 | if (tb) { |
| 218 | if (!tb->lookup(tb, &fl, &res)) { |
| 219 | ret = res.type; |
| 220 | dn_fib_res_put(&res); |
| 221 | } |
| 222 | } |
| 223 | return ret; |
| 224 | } |
| 225 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 226 | static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 227 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
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 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 231 | frh->family = AF_DECnet; |
| 232 | frh->dst_len = r->dst_len; |
| 233 | frh->src_len = r->src_len; |
| 234 | frh->tos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 236 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 237 | if (r->fwmark) |
| 238 | NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark); |
| 239 | #endif |
| 240 | if (r->dst_len) |
| 241 | NLA_PUT_U16(skb, FRA_DST, r->dst); |
| 242 | if (r->src_len) |
| 243 | NLA_PUT_U16(skb, FRA_SRC, r->src); |
| 244 | |
| 245 | return 0; |
| 246 | |
| 247 | nla_put_failure: |
| 248 | return -ENOBUFS; |
| 249 | } |
| 250 | |
| 251 | static u32 dn_fib_rule_default_pref(void) |
| 252 | { |
| 253 | struct list_head *pos; |
| 254 | struct fib_rule *rule; |
| 255 | |
| 256 | if (!list_empty(&dn_fib_rules)) { |
| 257 | pos = dn_fib_rules.next; |
| 258 | if (pos->next != &dn_fib_rules) { |
| 259 | rule = list_entry(pos->next, struct fib_rule, list); |
| 260 | if (rule->pref) |
| 261 | return rule->pref - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 265 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) |
| 269 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 270 | return fib_rules_dump(skb, cb, AF_DECnet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 273 | static struct fib_rules_ops dn_fib_rules_ops = { |
| 274 | .family = AF_DECnet, |
| 275 | .rule_size = sizeof(struct dn_fib_rule), |
| 276 | .action = dn_fib_rule_action, |
| 277 | .match = dn_fib_rule_match, |
| 278 | .configure = dn_fib_rule_configure, |
| 279 | .compare = dn_fib_rule_compare, |
| 280 | .fill = dn_fib_rule_fill, |
| 281 | .default_pref = dn_fib_rule_default_pref, |
| 282 | .nlgroup = RTNLGRP_DECnet_RULE, |
| 283 | .policy = dn_fib_rule_policy, |
| 284 | .rules_list = &dn_fib_rules, |
| 285 | .owner = THIS_MODULE, |
| 286 | }; |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | void __init dn_fib_rules_init(void) |
| 289 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 290 | list_add_tail(&default_rule.common.list, &dn_fib_rules); |
| 291 | fib_rules_register(&dn_fib_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void __exit dn_fib_rules_cleanup(void) |
| 295 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame^] | 296 | fib_rules_unregister(&dn_fib_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |