diff options
Diffstat (limited to 'net/decnet/dn_rules.c')
| -rw-r--r-- | net/decnet/dn_rules.c | 512 |
1 files changed, 201 insertions, 311 deletions
diff --git a/net/decnet/dn_rules.c b/net/decnet/dn_rules.c index 446faafe2065..3e0c882c90bf 100644 --- a/net/decnet/dn_rules.c +++ b/net/decnet/dn_rules.c | |||
| @@ -11,261 +11,213 @@ | |||
| 11 | * | 11 | * |
| 12 | * | 12 | * |
| 13 | * Changes: | 13 | * Changes: |
| 14 | * Steve Whitehouse <steve@chygwyn.com> | ||
| 15 | * Updated for Thomas Graf's generic rules | ||
| 14 | * | 16 | * |
| 15 | */ | 17 | */ |
| 16 | #include <linux/config.h> | ||
| 17 | #include <linux/string.h> | ||
| 18 | #include <linux/net.h> | 18 | #include <linux/net.h> |
| 19 | #include <linux/socket.h> | ||
| 20 | #include <linux/sockios.h> | ||
| 21 | #include <linux/init.h> | 19 | #include <linux/init.h> |
| 22 | #include <linux/skbuff.h> | ||
| 23 | #include <linux/netlink.h> | 20 | #include <linux/netlink.h> |
| 24 | #include <linux/rtnetlink.h> | 21 | #include <linux/rtnetlink.h> |
| 25 | #include <linux/proc_fs.h> | ||
| 26 | #include <linux/netdevice.h> | 22 | #include <linux/netdevice.h> |
| 27 | #include <linux/timer.h> | ||
| 28 | #include <linux/spinlock.h> | 23 | #include <linux/spinlock.h> |
| 29 | #include <linux/in_route.h> | ||
| 30 | #include <linux/list.h> | 24 | #include <linux/list.h> |
| 31 | #include <linux/rcupdate.h> | 25 | #include <linux/rcupdate.h> |
| 32 | #include <asm/atomic.h> | ||
| 33 | #include <asm/uaccess.h> | ||
| 34 | #include <net/neighbour.h> | 26 | #include <net/neighbour.h> |
| 35 | #include <net/dst.h> | 27 | #include <net/dst.h> |
| 36 | #include <net/flow.h> | 28 | #include <net/flow.h> |
| 29 | #include <net/fib_rules.h> | ||
| 37 | #include <net/dn.h> | 30 | #include <net/dn.h> |
| 38 | #include <net/dn_fib.h> | 31 | #include <net/dn_fib.h> |
| 39 | #include <net/dn_neigh.h> | 32 | #include <net/dn_neigh.h> |
| 40 | #include <net/dn_dev.h> | 33 | #include <net/dn_dev.h> |
| 41 | 34 | ||
| 35 | static struct fib_rules_ops dn_fib_rules_ops; | ||
| 36 | |||
| 42 | struct dn_fib_rule | 37 | struct dn_fib_rule |
| 43 | { | 38 | { |
| 44 | struct hlist_node r_hlist; | 39 | struct fib_rule common; |
| 45 | atomic_t r_clntref; | 40 | unsigned char dst_len; |
| 46 | u32 r_preference; | 41 | unsigned char src_len; |
| 47 | unsigned char r_table; | 42 | __le16 src; |
| 48 | unsigned char r_action; | 43 | __le16 srcmask; |
| 49 | unsigned char r_dst_len; | 44 | __le16 dst; |
| 50 | unsigned char r_src_len; | 45 | __le16 dstmask; |
| 51 | __le16 r_src; | 46 | __le16 srcmap; |
| 52 | __le16 r_srcmask; | 47 | u8 flags; |
| 53 | __le16 r_dst; | ||
| 54 | __le16 r_dstmask; | ||
| 55 | __le16 r_srcmap; | ||
| 56 | u8 r_flags; | ||
| 57 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | 48 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 58 | u32 r_fwmark; | 49 | u32 fwmark; |
| 50 | u32 fwmask; | ||
| 59 | #endif | 51 | #endif |
| 60 | int r_ifindex; | ||
| 61 | char r_ifname[IFNAMSIZ]; | ||
| 62 | int r_dead; | ||
| 63 | struct rcu_head rcu; | ||
| 64 | }; | 52 | }; |
| 65 | 53 | ||
| 66 | static struct dn_fib_rule default_rule = { | 54 | static struct dn_fib_rule default_rule = { |
| 67 | .r_clntref = ATOMIC_INIT(2), | 55 | .common = { |
| 68 | .r_preference = 0x7fff, | 56 | .refcnt = ATOMIC_INIT(2), |
| 69 | .r_table = RT_TABLE_MAIN, | 57 | .pref = 0x7fff, |
| 70 | .r_action = RTN_UNICAST | 58 | .table = RT_TABLE_MAIN, |
| 59 | .action = FR_ACT_TO_TBL, | ||
| 60 | }, | ||
| 71 | }; | 61 | }; |
| 72 | 62 | ||
| 73 | static struct hlist_head dn_fib_rules; | 63 | static LIST_HEAD(dn_fib_rules); |
| 64 | |||
| 74 | 65 | ||
| 75 | int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | 66 | int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res) |
| 76 | { | 67 | { |
| 77 | struct rtattr **rta = arg; | 68 | struct fib_lookup_arg arg = { |
| 78 | struct rtmsg *rtm = NLMSG_DATA(nlh); | 69 | .result = res, |
| 79 | struct dn_fib_rule *r; | 70 | }; |
| 80 | struct hlist_node *node; | 71 | int err; |
| 81 | int err = -ESRCH; | 72 | |
| 82 | 73 | err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg); | |
| 83 | hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) { | 74 | res->r = arg.rule; |
| 84 | if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) && | ||
| 85 | rtm->rtm_src_len == r->r_src_len && | ||
| 86 | rtm->rtm_dst_len == r->r_dst_len && | ||
| 87 | (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) && | ||
| 88 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | ||
| 89 | (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) && | ||
| 90 | #endif | ||
| 91 | (!rtm->rtm_type || rtm->rtm_type == r->r_action) && | ||
| 92 | (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) && | ||
| 93 | (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) && | ||
| 94 | (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) { | ||
| 95 | |||
| 96 | err = -EPERM; | ||
| 97 | if (r == &default_rule) | ||
| 98 | break; | ||
| 99 | |||
| 100 | hlist_del_rcu(&r->r_hlist); | ||
| 101 | r->r_dead = 1; | ||
| 102 | dn_fib_rule_put(r); | ||
| 103 | err = 0; | ||
| 104 | break; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | 75 | ||
| 108 | return err; | 76 | return err; |
| 109 | } | 77 | } |
| 110 | 78 | ||
| 111 | static inline void dn_fib_rule_put_rcu(struct rcu_head *head) | 79 | static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 80 | int flags, struct fib_lookup_arg *arg) | ||
| 112 | { | 81 | { |
| 113 | struct dn_fib_rule *r = container_of(head, struct dn_fib_rule, rcu); | 82 | int err = -EAGAIN; |
| 114 | kfree(r); | 83 | struct dn_fib_table *tbl; |
| 115 | } | ||
| 116 | 84 | ||
| 117 | void dn_fib_rule_put(struct dn_fib_rule *r) | 85 | switch(rule->action) { |
| 118 | { | 86 | case FR_ACT_TO_TBL: |
| 119 | if (atomic_dec_and_test(&r->r_clntref)) { | 87 | break; |
| 120 | if (r->r_dead) | 88 | |
| 121 | call_rcu(&r->rcu, dn_fib_rule_put_rcu); | 89 | case FR_ACT_UNREACHABLE: |
| 122 | else | 90 | err = -ENETUNREACH; |
| 123 | printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n"); | 91 | goto errout; |
| 92 | |||
| 93 | case FR_ACT_PROHIBIT: | ||
| 94 | err = -EACCES; | ||
| 95 | goto errout; | ||
| 96 | |||
| 97 | case FR_ACT_BLACKHOLE: | ||
| 98 | default: | ||
| 99 | err = -EINVAL; | ||
| 100 | goto errout; | ||
| 124 | } | 101 | } |
| 102 | |||
| 103 | tbl = dn_fib_get_table(rule->table, 0); | ||
| 104 | if (tbl == NULL) | ||
| 105 | goto errout; | ||
| 106 | |||
| 107 | err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result); | ||
| 108 | if (err > 0) | ||
| 109 | err = -EAGAIN; | ||
| 110 | errout: | ||
| 111 | return err; | ||
| 125 | } | 112 | } |
| 126 | 113 | ||
| 114 | static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = { | ||
| 115 | [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, | ||
| 116 | [FRA_PRIORITY] = { .type = NLA_U32 }, | ||
| 117 | [FRA_SRC] = { .type = NLA_U16 }, | ||
| 118 | [FRA_DST] = { .type = NLA_U16 }, | ||
| 119 | [FRA_FWMARK] = { .type = NLA_U32 }, | ||
| 120 | [FRA_FWMASK] = { .type = NLA_U32 }, | ||
| 121 | [FRA_TABLE] = { .type = NLA_U32 }, | ||
| 122 | }; | ||
| 127 | 123 | ||
| 128 | int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | 124 | static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 129 | { | 125 | { |
| 130 | struct rtattr **rta = arg; | 126 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 131 | struct rtmsg *rtm = NLMSG_DATA(nlh); | 127 | u16 daddr = fl->fld_dst; |
| 132 | struct dn_fib_rule *r, *new_r, *last = NULL; | 128 | u16 saddr = fl->fld_src; |
| 133 | struct hlist_node *node = NULL; | 129 | |
| 134 | unsigned char table_id; | 130 | if (((saddr ^ r->src) & r->srcmask) || |
| 135 | 131 | ((daddr ^ r->dst) & r->dstmask)) | |
| 136 | if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16) | 132 | return 0; |
| 137 | return -EINVAL; | ||
| 138 | |||
| 139 | if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ) | ||
| 140 | return -EINVAL; | ||
| 141 | |||
| 142 | if (rtm->rtm_type == RTN_NAT) | ||
| 143 | return -EINVAL; | ||
| 144 | |||
| 145 | table_id = rtm->rtm_table; | ||
| 146 | if (table_id == RT_TABLE_UNSPEC) { | ||
| 147 | struct dn_fib_table *tb; | ||
| 148 | if (rtm->rtm_type == RTN_UNICAST) { | ||
| 149 | if ((tb = dn_fib_empty_table()) == NULL) | ||
| 150 | return -ENOBUFS; | ||
| 151 | table_id = tb->n; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | 133 | ||
| 155 | new_r = kmalloc(sizeof(*new_r), GFP_KERNEL); | ||
| 156 | if (!new_r) | ||
| 157 | return -ENOMEM; | ||
| 158 | memset(new_r, 0, sizeof(*new_r)); | ||
| 159 | |||
| 160 | if (rta[RTA_SRC-1]) | ||
| 161 | memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2); | ||
| 162 | if (rta[RTA_DST-1]) | ||
| 163 | memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2); | ||
| 164 | if (rta[RTA_GATEWAY-1]) | ||
| 165 | memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2); | ||
| 166 | new_r->r_src_len = rtm->rtm_src_len; | ||
| 167 | new_r->r_dst_len = rtm->rtm_dst_len; | ||
| 168 | new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len); | ||
| 169 | new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len); | ||
| 170 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | 134 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 171 | if (rta[RTA_PROTOINFO-1]) | 135 | if ((r->fwmark ^ fl->fld_fwmark) & r->fwmask) |
| 172 | memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4); | 136 | return 0; |
| 173 | #endif | 137 | #endif |
| 174 | new_r->r_action = rtm->rtm_type; | ||
| 175 | new_r->r_flags = rtm->rtm_flags; | ||
| 176 | if (rta[RTA_PRIORITY-1]) | ||
| 177 | memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4); | ||
| 178 | new_r->r_table = table_id; | ||
| 179 | if (rta[RTA_IIF-1]) { | ||
| 180 | struct net_device *dev; | ||
| 181 | rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ); | ||
| 182 | new_r->r_ifindex = -1; | ||
| 183 | dev = dev_get_by_name(new_r->r_ifname); | ||
| 184 | if (dev) { | ||
| 185 | new_r->r_ifindex = dev->ifindex; | ||
| 186 | dev_put(dev); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | 138 | ||
| 190 | r = container_of(dn_fib_rules.first, struct dn_fib_rule, r_hlist); | 139 | return 1; |
| 191 | if (!new_r->r_preference) { | 140 | } |
| 192 | if (r && r->r_hlist.next != NULL) { | 141 | |
| 193 | r = container_of(r->r_hlist.next, struct dn_fib_rule, r_hlist); | 142 | static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 194 | if (r->r_preference) | 143 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 195 | new_r->r_preference = r->r_preference - 1; | 144 | struct nlattr **tb) |
| 145 | { | ||
| 146 | int err = -EINVAL; | ||
| 147 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; | ||
| 148 | |||
| 149 | if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos) | ||
| 150 | goto errout; | ||
| 151 | |||
| 152 | if (rule->table == RT_TABLE_UNSPEC) { | ||
| 153 | if (rule->action == FR_ACT_TO_TBL) { | ||
| 154 | struct dn_fib_table *table; | ||
| 155 | |||
| 156 | table = dn_fib_empty_table(); | ||
| 157 | if (table == NULL) { | ||
| 158 | err = -ENOBUFS; | ||
| 159 | goto errout; | ||
| 160 | } | ||
| 161 | |||
| 162 | rule->table = table->n; | ||
| 196 | } | 163 | } |
| 197 | } | 164 | } |
| 198 | 165 | ||
| 199 | hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) { | 166 | if (tb[FRA_SRC]) |
| 200 | if (r->r_preference > new_r->r_preference) | 167 | r->src = nla_get_u16(tb[FRA_SRC]); |
| 201 | break; | 168 | |
| 202 | last = r; | 169 | if (tb[FRA_DST]) |
| 170 | r->dst = nla_get_u16(tb[FRA_DST]); | ||
| 171 | |||
| 172 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | ||
| 173 | if (tb[FRA_FWMARK]) { | ||
| 174 | r->fwmark = nla_get_u32(tb[FRA_FWMARK]); | ||
| 175 | if (r->fwmark) | ||
| 176 | /* compatibility: if the mark value is non-zero all bits | ||
| 177 | * are compared unless a mask is explicitly specified. | ||
| 178 | */ | ||
| 179 | r->fwmask = 0xFFFFFFFF; | ||
| 203 | } | 180 | } |
| 204 | atomic_inc(&new_r->r_clntref); | ||
| 205 | 181 | ||
| 206 | if (last) | 182 | if (tb[FRA_FWMASK]) |
| 207 | hlist_add_after_rcu(&last->r_hlist, &new_r->r_hlist); | 183 | r->fwmask = nla_get_u32(tb[FRA_FWMASK]); |
| 208 | else | 184 | #endif |
| 209 | hlist_add_before_rcu(&new_r->r_hlist, &r->r_hlist); | ||
| 210 | return 0; | ||
| 211 | } | ||
| 212 | 185 | ||
| 186 | r->src_len = frh->src_len; | ||
| 187 | r->srcmask = dnet_make_mask(r->src_len); | ||
| 188 | r->dst_len = frh->dst_len; | ||
| 189 | r->dstmask = dnet_make_mask(r->dst_len); | ||
| 190 | err = 0; | ||
| 191 | errout: | ||
| 192 | return err; | ||
| 193 | } | ||
| 213 | 194 | ||
| 214 | int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res) | 195 | static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 196 | struct nlattr **tb) | ||
| 215 | { | 197 | { |
| 216 | struct dn_fib_rule *r, *policy; | 198 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 217 | struct dn_fib_table *tb; | 199 | |
| 218 | __le16 saddr = flp->fld_src; | 200 | if (frh->src_len && (r->src_len != frh->src_len)) |
| 219 | __le16 daddr = flp->fld_dst; | 201 | return 0; |
| 220 | struct hlist_node *node; | ||
| 221 | int err; | ||
| 222 | 202 | ||
| 223 | rcu_read_lock(); | 203 | if (frh->dst_len && (r->dst_len != frh->dst_len)) |
| 204 | return 0; | ||
| 224 | 205 | ||
| 225 | hlist_for_each_entry_rcu(r, node, &dn_fib_rules, r_hlist) { | ||
| 226 | if (((saddr^r->r_src) & r->r_srcmask) || | ||
| 227 | ((daddr^r->r_dst) & r->r_dstmask) || | ||
| 228 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | 206 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 229 | (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) || | 207 | if (tb[FRA_FWMARK] && (r->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 208 | return 0; | ||
| 209 | |||
| 210 | if (tb[FRA_FWMASK] && (r->fwmask != nla_get_u32(tb[FRA_FWMASK]))) | ||
| 211 | return 0; | ||
| 230 | #endif | 212 | #endif |
| 231 | (r->r_ifindex && r->r_ifindex != flp->iif)) | ||
| 232 | continue; | ||
| 233 | |||
| 234 | switch(r->r_action) { | ||
| 235 | case RTN_UNICAST: | ||
| 236 | case RTN_NAT: | ||
| 237 | policy = r; | ||
| 238 | break; | ||
| 239 | case RTN_UNREACHABLE: | ||
| 240 | rcu_read_unlock(); | ||
| 241 | return -ENETUNREACH; | ||
| 242 | default: | ||
| 243 | case RTN_BLACKHOLE: | ||
| 244 | rcu_read_unlock(); | ||
| 245 | return -EINVAL; | ||
| 246 | case RTN_PROHIBIT: | ||
| 247 | rcu_read_unlock(); | ||
| 248 | return -EACCES; | ||
| 249 | } | ||
| 250 | 213 | ||
| 251 | if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL) | 214 | if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC]))) |
| 252 | continue; | 215 | return 0; |
| 253 | err = tb->lookup(tb, flp, res); | 216 | |
| 254 | if (err == 0) { | 217 | if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST]))) |
| 255 | res->r = policy; | 218 | return 0; |
| 256 | if (policy) | ||
| 257 | atomic_inc(&policy->r_clntref); | ||
| 258 | rcu_read_unlock(); | ||
| 259 | return 0; | ||
| 260 | } | ||
| 261 | if (err < 0 && err != -EAGAIN) { | ||
| 262 | rcu_read_unlock(); | ||
| 263 | return err; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | 219 | ||
| 267 | rcu_read_unlock(); | 220 | return 1; |
| 268 | return -ESRCH; | ||
| 269 | } | 221 | } |
| 270 | 222 | ||
| 271 | unsigned dnet_addr_type(__le16 addr) | 223 | unsigned dnet_addr_type(__le16 addr) |
| @@ -273,7 +225,7 @@ unsigned dnet_addr_type(__le16 addr) | |||
| 273 | struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } }; | 225 | struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } }; |
| 274 | struct dn_fib_res res; | 226 | struct dn_fib_res res; |
| 275 | unsigned ret = RTN_UNICAST; | 227 | unsigned ret = RTN_UNICAST; |
| 276 | struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL]; | 228 | struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0); |
| 277 | 229 | ||
| 278 | res.r = NULL; | 230 | res.r = NULL; |
| 279 | 231 | ||
| @@ -286,141 +238,79 @@ unsigned dnet_addr_type(__le16 addr) | |||
| 286 | return ret; | 238 | return ret; |
| 287 | } | 239 | } |
| 288 | 240 | ||
| 289 | __le16 dn_fib_rules_policy(__le16 saddr, struct dn_fib_res *res, unsigned *flags) | 241 | static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 242 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) | ||
| 290 | { | 243 | { |
| 291 | struct dn_fib_rule *r = res->r; | 244 | struct dn_fib_rule *r = (struct dn_fib_rule *)rule; |
| 292 | 245 | ||
| 293 | if (r->r_action == RTN_NAT) { | 246 | frh->family = AF_DECnet; |
| 294 | int addrtype = dnet_addr_type(r->r_srcmap); | 247 | frh->dst_len = r->dst_len; |
| 248 | frh->src_len = r->src_len; | ||
| 249 | frh->tos = 0; | ||
| 295 | 250 | ||
| 296 | if (addrtype == RTN_NAT) { | 251 | #ifdef CONFIG_DECNET_ROUTE_FWMARK |
| 297 | saddr = (saddr&~r->r_srcmask)|r->r_srcmap; | 252 | if (r->fwmark) |
| 298 | *flags |= RTCF_SNAT; | 253 | NLA_PUT_U32(skb, FRA_FWMARK, r->fwmark); |
| 299 | } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) { | 254 | if (r->fwmask || r->fwmark) |
| 300 | saddr = r->r_srcmap; | 255 | NLA_PUT_U32(skb, FRA_FWMASK, r->fwmask); |
| 301 | *flags |= RTCF_MASQ; | 256 | #endif |
| 302 | } | 257 | if (r->dst_len) |
| 303 | } | 258 | NLA_PUT_U16(skb, FRA_DST, r->dst); |
| 304 | return saddr; | 259 | if (r->src_len) |
| 305 | } | 260 | NLA_PUT_U16(skb, FRA_SRC, r->src); |
| 306 | |||
| 307 | static void dn_fib_rules_detach(struct net_device *dev) | ||
| 308 | { | ||
| 309 | struct hlist_node *node; | ||
| 310 | struct dn_fib_rule *r; | ||
| 311 | |||
| 312 | hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) { | ||
| 313 | if (r->r_ifindex == dev->ifindex) | ||
| 314 | r->r_ifindex = -1; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | 261 | ||
| 318 | static void dn_fib_rules_attach(struct net_device *dev) | 262 | return 0; |
| 319 | { | ||
| 320 | struct hlist_node *node; | ||
| 321 | struct dn_fib_rule *r; | ||
| 322 | 263 | ||
| 323 | hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) { | 264 | nla_put_failure: |
| 324 | if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) | 265 | return -ENOBUFS; |
| 325 | r->r_ifindex = dev->ifindex; | ||
| 326 | } | ||
| 327 | } | 266 | } |
| 328 | 267 | ||
| 329 | static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr) | 268 | static u32 dn_fib_rule_default_pref(void) |
| 330 | { | 269 | { |
| 331 | struct net_device *dev = ptr; | 270 | struct list_head *pos; |
| 332 | 271 | struct fib_rule *rule; | |
| 333 | switch(event) { | 272 | |
| 334 | case NETDEV_UNREGISTER: | 273 | if (!list_empty(&dn_fib_rules)) { |
| 335 | dn_fib_rules_detach(dev); | 274 | pos = dn_fib_rules.next; |
| 336 | dn_fib_sync_down(0, dev, 1); | 275 | if (pos->next != &dn_fib_rules) { |
| 337 | case NETDEV_REGISTER: | 276 | rule = list_entry(pos->next, struct fib_rule, list); |
| 338 | dn_fib_rules_attach(dev); | 277 | if (rule->pref) |
| 339 | dn_fib_sync_up(dev); | 278 | return rule->pref - 1; |
| 279 | } | ||
| 340 | } | 280 | } |
| 341 | 281 | ||
| 342 | return NOTIFY_DONE; | 282 | return 0; |
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | static struct notifier_block dn_fib_rules_notifier = { | ||
| 347 | .notifier_call = dn_fib_rules_event, | ||
| 348 | }; | ||
| 349 | |||
| 350 | static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r, | ||
| 351 | struct netlink_callback *cb, unsigned int flags) | ||
| 352 | { | ||
| 353 | struct rtmsg *rtm; | ||
| 354 | struct nlmsghdr *nlh; | ||
| 355 | unsigned char *b = skb->tail; | ||
| 356 | |||
| 357 | |||
| 358 | nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags); | ||
| 359 | rtm = NLMSG_DATA(nlh); | ||
| 360 | rtm->rtm_family = AF_DECnet; | ||
| 361 | rtm->rtm_dst_len = r->r_dst_len; | ||
| 362 | rtm->rtm_src_len = r->r_src_len; | ||
| 363 | rtm->rtm_tos = 0; | ||
| 364 | #ifdef CONFIG_DECNET_ROUTE_FWMARK | ||
| 365 | if (r->r_fwmark) | ||
| 366 | RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark); | ||
| 367 | #endif | ||
| 368 | rtm->rtm_table = r->r_table; | ||
| 369 | rtm->rtm_protocol = 0; | ||
| 370 | rtm->rtm_scope = 0; | ||
| 371 | rtm->rtm_type = r->r_action; | ||
| 372 | rtm->rtm_flags = r->r_flags; | ||
| 373 | |||
| 374 | if (r->r_dst_len) | ||
| 375 | RTA_PUT(skb, RTA_DST, 2, &r->r_dst); | ||
| 376 | if (r->r_src_len) | ||
| 377 | RTA_PUT(skb, RTA_SRC, 2, &r->r_src); | ||
| 378 | if (r->r_ifname[0]) | ||
| 379 | RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname); | ||
| 380 | if (r->r_preference) | ||
| 381 | RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference); | ||
| 382 | if (r->r_srcmap) | ||
| 383 | RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap); | ||
| 384 | nlh->nlmsg_len = skb->tail - b; | ||
| 385 | return skb->len; | ||
| 386 | |||
| 387 | nlmsg_failure: | ||
| 388 | rtattr_failure: | ||
| 389 | skb_trim(skb, b - skb->data); | ||
| 390 | return -1; | ||
| 391 | } | 283 | } |
| 392 | 284 | ||
| 393 | int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) | 285 | int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) |
| 394 | { | 286 | { |
| 395 | int idx = 0; | 287 | return fib_rules_dump(skb, cb, AF_DECnet); |
| 396 | int s_idx = cb->args[0]; | ||
| 397 | struct dn_fib_rule *r; | ||
| 398 | struct hlist_node *node; | ||
| 399 | |||
| 400 | rcu_read_lock(); | ||
| 401 | hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) { | ||
| 402 | if (idx < s_idx) | ||
| 403 | continue; | ||
| 404 | if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0) | ||
| 405 | break; | ||
| 406 | idx++; | ||
| 407 | } | ||
| 408 | rcu_read_unlock(); | ||
| 409 | cb->args[0] = idx; | ||
| 410 | |||
| 411 | return skb->len; | ||
| 412 | } | 288 | } |
| 413 | 289 | ||
| 290 | static struct fib_rules_ops dn_fib_rules_ops = { | ||
| 291 | .family = AF_DECnet, | ||
| 292 | .rule_size = sizeof(struct dn_fib_rule), | ||
| 293 | .action = dn_fib_rule_action, | ||
| 294 | .match = dn_fib_rule_match, | ||
| 295 | .configure = dn_fib_rule_configure, | ||
| 296 | .compare = dn_fib_rule_compare, | ||
| 297 | .fill = dn_fib_rule_fill, | ||
| 298 | .default_pref = dn_fib_rule_default_pref, | ||
| 299 | .nlgroup = RTNLGRP_DECnet_RULE, | ||
| 300 | .policy = dn_fib_rule_policy, | ||
| 301 | .rules_list = &dn_fib_rules, | ||
| 302 | .owner = THIS_MODULE, | ||
| 303 | }; | ||
| 304 | |||
| 414 | void __init dn_fib_rules_init(void) | 305 | void __init dn_fib_rules_init(void) |
| 415 | { | 306 | { |
| 416 | INIT_HLIST_HEAD(&dn_fib_rules); | 307 | list_add_tail(&default_rule.common.list, &dn_fib_rules); |
| 417 | hlist_add_head(&default_rule.r_hlist, &dn_fib_rules); | 308 | fib_rules_register(&dn_fib_rules_ops); |
| 418 | register_netdevice_notifier(&dn_fib_rules_notifier); | ||
| 419 | } | 309 | } |
| 420 | 310 | ||
| 421 | void __exit dn_fib_rules_cleanup(void) | 311 | void __exit dn_fib_rules_cleanup(void) |
| 422 | { | 312 | { |
| 423 | unregister_netdevice_notifier(&dn_fib_rules_notifier); | 313 | fib_rules_unregister(&dn_fib_rules_ops); |
| 424 | } | 314 | } |
| 425 | 315 | ||
| 426 | 316 | ||
