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