Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 5 | * operating system. DECnet is implemented using the BSD Socket |
| 6 | * interface as the means of communication with the user level. |
| 7 | * |
| 8 | * DECnet Routing Forwarding Information Base (Rules) |
| 9 | * |
| 10 | * Author: Steve Whitehouse <SteveW@ACM.org> |
| 11 | * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c |
| 12 | * |
| 13 | * |
| 14 | * Changes: |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 15 | * Steve Whitehouse <steve@chygwyn.com> |
| 16 | * Updated for Thomas Graf's generic rules |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * |
| 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/netlink.h> |
| 22 | #include <linux/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 25 | #include <linux/list.h> |
| 26 | #include <linux/rcupdate.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/neighbour.h> |
| 29 | #include <net/dst.h> |
| 30 | #include <net/flow.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 31 | #include <net/fib_rules.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/dn.h> |
| 33 | #include <net/dn_fib.h> |
| 34 | #include <net/dn_neigh.h> |
| 35 | #include <net/dn_dev.h> |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 36 | #include <net/dn_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 38 | static struct fib_rules_ops *dn_fib_rules_ops; |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct dn_fib_rule |
| 41 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 42 | struct fib_rule common; |
| 43 | unsigned char dst_len; |
| 44 | unsigned char src_len; |
| 45 | __le16 src; |
| 46 | __le16 srcmask; |
| 47 | __le16 dst; |
| 48 | __le16 dstmask; |
| 49 | __le16 srcmap; |
| 50 | u8 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 53 | |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 54 | int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 56 | struct fib_lookup_arg arg = { |
| 57 | .result = res, |
| 58 | }; |
| 59 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 61 | err = fib_rules_lookup(dn_fib_rules_ops, |
| 62 | flowidn_to_flowi(flp), 0, &arg); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 63 | res->r = arg.rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | return err; |
| 66 | } |
| 67 | |
Adrian Bunk | 2aa7f36 | 2006-08-14 23:55:20 -0700 | [diff] [blame] | 68 | static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 69 | int flags, struct fib_lookup_arg *arg) |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 70 | { |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 71 | struct flowidn *fld = &flp->u.dn; |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 72 | int err = -EAGAIN; |
| 73 | struct dn_fib_table *tbl; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 74 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 75 | switch(rule->action) { |
| 76 | case FR_ACT_TO_TBL: |
| 77 | break; |
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_UNREACHABLE: |
| 80 | err = -ENETUNREACH; |
| 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_PROHIBIT: |
| 84 | err = -EACCES; |
| 85 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 87 | case FR_ACT_BLACKHOLE: |
| 88 | default: |
| 89 | err = -EINVAL; |
| 90 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 93 | tbl = dn_fib_get_table(rule->table, 0); |
| 94 | if (tbl == NULL) |
| 95 | goto errout; |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 96 | |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 97 | err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 98 | if (err > 0) |
| 99 | err = -EAGAIN; |
| 100 | errout: |
| 101 | return err; |
| 102 | } |
| 103 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 104 | static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = { |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 105 | FRA_GENERIC_POLICY, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 109 | { |
| 110 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 111 | struct flowidn *fld = &fl->u.dn; |
| 112 | __le16 daddr = fld->daddr; |
| 113 | __le16 saddr = fld->saddr; |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 114 | |
| 115 | if (((saddr ^ r->src) & r->srcmask) || |
| 116 | ((daddr ^ r->dst) & r->dstmask)) |
| 117 | return 0; |
| 118 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 119 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 122 | 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] | 123 | struct fib_rule_hdr *frh, |
Roopa Prabhu | b16fb41 | 2018-04-21 09:41:31 -0700 | [diff] [blame] | 124 | struct nlattr **tb, |
| 125 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 127 | int err = -EINVAL; |
| 128 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Roopa Prabhu | b16fb41 | 2018-04-21 09:41:31 -0700 | [diff] [blame] | 130 | if (frh->tos) { |
| 131 | NL_SET_ERR_MSG(extack, "Invalid tos value"); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 132 | goto errout; |
Roopa Prabhu | b16fb41 | 2018-04-21 09:41:31 -0700 | [diff] [blame] | 133 | } |
Steven Whitehouse | ecba320 | 2006-03-20 22:43:28 -0800 | [diff] [blame] | 134 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 135 | if (rule->table == RT_TABLE_UNSPEC) { |
| 136 | if (rule->action == FR_ACT_TO_TBL) { |
| 137 | struct dn_fib_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 139 | table = dn_fib_empty_table(); |
| 140 | if (table == NULL) { |
| 141 | err = -ENOBUFS; |
| 142 | goto errout; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 145 | rule->table = table->n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 149 | if (frh->src_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 150 | r->src = nla_get_le16(tb[FRA_SRC]); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 151 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 152 | if (frh->dst_len) |
Al Viro | 2835fdf | 2007-02-09 18:13:37 +0000 | [diff] [blame] | 153 | r->dst = nla_get_le16(tb[FRA_DST]); |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 154 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 155 | r->src_len = frh->src_len; |
| 156 | r->srcmask = dnet_make_mask(r->src_len); |
| 157 | r->dst_len = frh->dst_len; |
| 158 | r->dstmask = dnet_make_mask(r->dst_len); |
| 159 | err = 0; |
| 160 | errout: |
| 161 | return err; |
| 162 | } |
| 163 | |
| 164 | static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 165 | struct nlattr **tb) |
| 166 | { |
| 167 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 168 | |
| 169 | if (frh->src_len && (r->src_len != frh->src_len)) |
| 170 | return 0; |
| 171 | |
| 172 | if (frh->dst_len && (r->dst_len != frh->dst_len)) |
| 173 | return 0; |
| 174 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 175 | if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC]))) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 176 | return 0; |
| 177 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 178 | if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST]))) |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | |
| 181 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 184 | unsigned int dnet_addr_type(__le16 addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 186 | struct flowidn fld = { .daddr = addr }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | struct dn_fib_res res; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 188 | unsigned int ret = RTN_UNICAST; |
Patrick McHardy | abcab26 | 2006-08-10 23:11:47 -0700 | [diff] [blame] | 189 | 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] | 190 | |
| 191 | res.r = NULL; |
| 192 | |
| 193 | if (tb) { |
David S. Miller | bef55ae | 2011-03-12 17:17:10 -0500 | [diff] [blame] | 194 | if (!tb->lookup(tb, &fld, &res)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | ret = res.type; |
| 196 | dn_fib_res_put(&res); |
| 197 | } |
| 198 | } |
| 199 | return ret; |
| 200 | } |
| 201 | |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 202 | 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] | 203 | struct fib_rule_hdr *frh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 205 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
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 | frh->dst_len = r->dst_len; |
| 208 | frh->src_len = r->src_len; |
| 209 | frh->tos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
David S. Miller | b21dddb | 2012-04-01 20:15:14 -0400 | [diff] [blame] | 211 | if ((r->dst_len && |
| 212 | nla_put_le16(skb, FRA_DST, r->dst)) || |
| 213 | (r->src_len && |
| 214 | nla_put_le16(skb, FRA_SRC, r->src))) |
| 215 | goto nla_put_failure; |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 216 | return 0; |
| 217 | |
| 218 | nla_put_failure: |
| 219 | return -ENOBUFS; |
| 220 | } |
| 221 | |
Denis V. Lunev | ae299fc | 2008-07-05 19:01:28 -0700 | [diff] [blame] | 222 | static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops) |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 223 | { |
Thomas Graf | 4b19ca4 | 2007-03-28 14:18:52 -0700 | [diff] [blame] | 224 | dn_rt_cache_flush(-1); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Andi Kleen | 04a6f82 | 2012-10-04 17:12:11 -0700 | [diff] [blame] | 227 | static const struct fib_rules_ops __net_initconst dn_fib_rules_ops_template = { |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 228 | .family = AF_DECnet, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 229 | .rule_size = sizeof(struct dn_fib_rule), |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 230 | .addr_size = sizeof(u16), |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 231 | .action = dn_fib_rule_action, |
| 232 | .match = dn_fib_rule_match, |
| 233 | .configure = dn_fib_rule_configure, |
| 234 | .compare = dn_fib_rule_compare, |
| 235 | .fill = dn_fib_rule_fill, |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 236 | .flush_cache = dn_fib_rule_flush_cache, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 237 | .nlgroup = RTNLGRP_DECnet_RULE, |
| 238 | .policy = dn_fib_rule_policy, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 239 | .owner = THIS_MODULE, |
Denis V. Lunev | 0359238 | 2008-01-20 16:46:01 -0800 | [diff] [blame] | 240 | .fro_net = &init_net, |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | void __init dn_fib_rules_init(void) |
| 244 | { |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 245 | dn_fib_rules_ops = |
| 246 | fib_rules_register(&dn_fib_rules_ops_template, &init_net); |
| 247 | BUG_ON(IS_ERR(dn_fib_rules_ops)); |
| 248 | BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff, |
Denis V. Lunev | 2994c63 | 2007-11-10 22:12:03 -0800 | [diff] [blame] | 249 | RT_TABLE_MAIN, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void __exit dn_fib_rules_cleanup(void) |
| 253 | { |
WANG Cong | 419df12 | 2015-03-31 11:01:46 -0700 | [diff] [blame] | 254 | rtnl_lock(); |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 255 | fib_rules_unregister(dn_fib_rules_ops); |
WANG Cong | 419df12 | 2015-03-31 11:01:46 -0700 | [diff] [blame] | 256 | rtnl_unlock(); |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 257 | rcu_barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |