Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 3 | * operating system. DECnet is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * DECnet Routing Forwarding Information Base (Glue/Info List) |
| 7 | * |
| 8 | * Author: Steve Whitehouse <SteveW@ACM.org> |
| 9 | * |
| 10 | * |
| 11 | * Changes: |
| 12 | * Alexey Kuznetsov : SMP locking changes |
| 13 | * Steve Whitehouse : Rewrote it... Well to be more correct, I |
| 14 | * copied most of it from the ipv4 fib code. |
| 15 | * Steve Whitehouse : Updated it in style and fixed a few bugs |
| 16 | * which were fixed in the ipv4 code since |
| 17 | * this code was copied from it. |
| 18 | * |
| 19 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/net.h> |
| 22 | #include <linux/socket.h> |
| 23 | #include <linux/sockios.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/netlink.h> |
| 27 | #include <linux/rtnetlink.h> |
| 28 | #include <linux/proc_fs.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/timer.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <asm/atomic.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <net/neighbour.h> |
| 35 | #include <net/dst.h> |
| 36 | #include <net/flow.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 37 | #include <net/fib_rules.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/dn.h> |
| 39 | #include <net/dn_route.h> |
| 40 | #include <net/dn_fib.h> |
| 41 | #include <net/dn_neigh.h> |
| 42 | #include <net/dn_dev.h> |
| 43 | |
| 44 | #define RT_MIN_TABLE 1 |
| 45 | |
| 46 | #define for_fib_info() { struct dn_fib_info *fi;\ |
| 47 | for(fi = dn_fib_info_list; fi; fi = fi->fib_next) |
| 48 | #define endfor_fib_info() } |
| 49 | |
| 50 | #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\ |
| 51 | for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++) |
| 52 | |
| 53 | #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\ |
| 54 | for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++) |
| 55 | |
| 56 | #define endfor_nexthops(fi) } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static DEFINE_SPINLOCK(dn_fib_multipath_lock); |
| 59 | static struct dn_fib_info *dn_fib_info_list; |
Steven Whitehouse | a22ec36 | 2006-08-09 16:00:57 -0700 | [diff] [blame] | 60 | static DEFINE_SPINLOCK(dn_fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | static struct |
| 63 | { |
| 64 | int error; |
| 65 | u8 scope; |
Thomas Graf | a979101 | 2007-03-24 20:33:27 -0700 | [diff] [blame] | 66 | } dn_fib_props[RTN_MAX+1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | [RTN_UNSPEC] = { .error = 0, .scope = RT_SCOPE_NOWHERE }, |
| 68 | [RTN_UNICAST] = { .error = 0, .scope = RT_SCOPE_UNIVERSE }, |
| 69 | [RTN_LOCAL] = { .error = 0, .scope = RT_SCOPE_HOST }, |
| 70 | [RTN_BROADCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE }, |
| 71 | [RTN_ANYCAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE }, |
| 72 | [RTN_MULTICAST] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE }, |
| 73 | [RTN_BLACKHOLE] = { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE }, |
| 74 | [RTN_UNREACHABLE] = { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE }, |
| 75 | [RTN_PROHIBIT] = { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE }, |
| 76 | [RTN_THROW] = { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE }, |
| 77 | [RTN_NAT] = { .error = 0, .scope = RT_SCOPE_NOWHERE }, |
| 78 | [RTN_XRESOLVE] = { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE }, |
| 79 | }; |
| 80 | |
Adrian Bunk | 2aa7f36 | 2006-08-14 23:55:20 -0700 | [diff] [blame] | 81 | static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force); |
| 82 | static int dn_fib_sync_up(struct net_device *dev); |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | void dn_fib_free_info(struct dn_fib_info *fi) |
| 85 | { |
| 86 | if (fi->fib_dead == 0) { |
| 87 | printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | change_nexthops(fi) { |
| 92 | if (nh->nh_dev) |
| 93 | dev_put(nh->nh_dev); |
| 94 | nh->nh_dev = NULL; |
| 95 | } endfor_nexthops(fi); |
| 96 | kfree(fi); |
| 97 | } |
| 98 | |
| 99 | void dn_fib_release_info(struct dn_fib_info *fi) |
| 100 | { |
Steven Whitehouse | a22ec36 | 2006-08-09 16:00:57 -0700 | [diff] [blame] | 101 | spin_lock(&dn_fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (fi && --fi->fib_treeref == 0) { |
| 103 | if (fi->fib_next) |
| 104 | fi->fib_next->fib_prev = fi->fib_prev; |
| 105 | if (fi->fib_prev) |
| 106 | fi->fib_prev->fib_next = fi->fib_next; |
| 107 | if (fi == dn_fib_info_list) |
| 108 | dn_fib_info_list = fi->fib_next; |
| 109 | fi->fib_dead = 1; |
| 110 | dn_fib_info_put(fi); |
| 111 | } |
Steven Whitehouse | a22ec36 | 2006-08-09 16:00:57 -0700 | [diff] [blame] | 112 | spin_unlock(&dn_fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi) |
| 116 | { |
| 117 | const struct dn_fib_nh *onh = ofi->fib_nh; |
| 118 | |
| 119 | for_nexthops(fi) { |
| 120 | if (nh->nh_oif != onh->nh_oif || |
| 121 | nh->nh_gw != onh->nh_gw || |
| 122 | nh->nh_scope != onh->nh_scope || |
| 123 | nh->nh_weight != onh->nh_weight || |
| 124 | ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD)) |
| 125 | return -1; |
| 126 | onh++; |
| 127 | } endfor_nexthops(fi); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static inline struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi) |
| 132 | { |
| 133 | for_fib_info() { |
| 134 | if (fi->fib_nhs != nfi->fib_nhs) |
| 135 | continue; |
| 136 | if (nfi->fib_protocol == fi->fib_protocol && |
| 137 | nfi->fib_prefsrc == fi->fib_prefsrc && |
| 138 | nfi->fib_priority == fi->fib_priority && |
| 139 | memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 && |
| 140 | ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 && |
| 141 | (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0)) |
| 142 | return fi; |
| 143 | } endfor_fib_info(); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 147 | __le16 dn_fib_get_attr16(struct rtattr *attr, int attrlen, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | while(RTA_OK(attr,attrlen)) { |
| 150 | if (attr->rta_type == type) |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 151 | return *(__le16*)RTA_DATA(attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | attr = RTA_NEXT(attr, attrlen); |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int dn_fib_count_nhs(struct rtattr *rta) |
| 159 | { |
| 160 | int nhs = 0; |
| 161 | struct rtnexthop *nhp = RTA_DATA(rta); |
| 162 | int nhlen = RTA_PAYLOAD(rta); |
| 163 | |
| 164 | while(nhlen >= (int)sizeof(struct rtnexthop)) { |
| 165 | if ((nhlen -= nhp->rtnh_len) < 0) |
| 166 | return 0; |
| 167 | nhs++; |
| 168 | nhp = RTNH_NEXT(nhp); |
| 169 | } |
| 170 | |
| 171 | return nhs; |
| 172 | } |
| 173 | |
| 174 | static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct rtattr *rta, const struct rtmsg *r) |
| 175 | { |
| 176 | struct rtnexthop *nhp = RTA_DATA(rta); |
| 177 | int nhlen = RTA_PAYLOAD(rta); |
| 178 | |
| 179 | change_nexthops(fi) { |
| 180 | int attrlen = nhlen - sizeof(struct rtnexthop); |
| 181 | if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags; |
| 185 | nh->nh_oif = nhp->rtnh_ifindex; |
| 186 | nh->nh_weight = nhp->rtnh_hops + 1; |
| 187 | |
| 188 | if (attrlen) { |
| 189 | nh->nh_gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY); |
| 190 | } |
| 191 | nhp = RTNH_NEXT(nhp); |
| 192 | } endfor_nexthops(fi); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh) |
| 199 | { |
| 200 | int err; |
| 201 | |
| 202 | if (nh->nh_gw) { |
| 203 | struct flowi fl; |
| 204 | struct dn_fib_res res; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (nh->nh_flags&RTNH_F_ONLINK) { |
| 207 | struct net_device *dev; |
| 208 | |
| 209 | if (r->rtm_scope >= RT_SCOPE_LINK) |
| 210 | return -EINVAL; |
| 211 | if (dnet_addr_type(nh->nh_gw) != RTN_UNICAST) |
| 212 | return -EINVAL; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 213 | if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return -ENODEV; |
| 215 | if (!(dev->flags&IFF_UP)) |
| 216 | return -ENETDOWN; |
| 217 | nh->nh_dev = dev; |
| 218 | dev_hold(dev); |
| 219 | nh->nh_scope = RT_SCOPE_LINK; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | memset(&fl, 0, sizeof(fl)); |
| 224 | fl.fld_dst = nh->nh_gw; |
| 225 | fl.oif = nh->nh_oif; |
| 226 | fl.fld_scope = r->rtm_scope + 1; |
| 227 | |
| 228 | if (fl.fld_scope < RT_SCOPE_LINK) |
| 229 | fl.fld_scope = RT_SCOPE_LINK; |
| 230 | |
| 231 | if ((err = dn_fib_lookup(&fl, &res)) != 0) |
| 232 | return err; |
| 233 | |
| 234 | err = -EINVAL; |
| 235 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) |
| 236 | goto out; |
| 237 | nh->nh_scope = res.scope; |
| 238 | nh->nh_oif = DN_FIB_RES_OIF(res); |
| 239 | nh->nh_dev = DN_FIB_RES_DEV(res); |
| 240 | if (nh->nh_dev == NULL) |
| 241 | goto out; |
| 242 | dev_hold(nh->nh_dev); |
| 243 | err = -ENETDOWN; |
| 244 | if (!(nh->nh_dev->flags & IFF_UP)) |
| 245 | goto out; |
| 246 | err = 0; |
| 247 | out: |
| 248 | dn_fib_res_put(&res); |
| 249 | return err; |
| 250 | } else { |
| 251 | struct net_device *dev; |
| 252 | |
| 253 | if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK)) |
| 254 | return -EINVAL; |
| 255 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 256 | dev = __dev_get_by_index(&init_net, nh->nh_oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (dev == NULL || dev->dn_ptr == NULL) |
| 258 | return -ENODEV; |
| 259 | if (!(dev->flags&IFF_UP)) |
| 260 | return -ENETDOWN; |
| 261 | nh->nh_dev = dev; |
| 262 | dev_hold(nh->nh_dev); |
| 263 | nh->nh_scope = RT_SCOPE_HOST; |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct dn_kern_rta *rta, const struct nlmsghdr *nlh, int *errp) |
| 271 | { |
| 272 | int err; |
| 273 | struct dn_fib_info *fi = NULL; |
| 274 | struct dn_fib_info *ofi; |
| 275 | int nhs = 1; |
| 276 | |
Thomas Graf | a979101 | 2007-03-24 20:33:27 -0700 | [diff] [blame] | 277 | if (r->rtm_type > RTN_MAX) |
| 278 | goto err_inval; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (dn_fib_props[r->rtm_type].scope > r->rtm_scope) |
| 281 | goto err_inval; |
| 282 | |
| 283 | if (rta->rta_mp) { |
| 284 | nhs = dn_fib_count_nhs(rta->rta_mp); |
| 285 | if (nhs == 0) |
| 286 | goto err_inval; |
| 287 | } |
| 288 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 289 | fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | err = -ENOBUFS; |
| 291 | if (fi == NULL) |
| 292 | goto failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | fi->fib_protocol = r->rtm_protocol; |
| 295 | fi->fib_nhs = nhs; |
| 296 | fi->fib_flags = r->rtm_flags; |
| 297 | if (rta->rta_priority) |
| 298 | fi->fib_priority = *rta->rta_priority; |
| 299 | if (rta->rta_mx) { |
| 300 | int attrlen = RTA_PAYLOAD(rta->rta_mx); |
| 301 | struct rtattr *attr = RTA_DATA(rta->rta_mx); |
| 302 | |
| 303 | while(RTA_OK(attr, attrlen)) { |
| 304 | unsigned flavour = attr->rta_type; |
| 305 | if (flavour) { |
| 306 | if (flavour > RTAX_MAX) |
| 307 | goto err_inval; |
| 308 | fi->fib_metrics[flavour-1] = *(unsigned*)RTA_DATA(attr); |
| 309 | } |
| 310 | attr = RTA_NEXT(attr, attrlen); |
| 311 | } |
| 312 | } |
| 313 | if (rta->rta_prefsrc) |
| 314 | memcpy(&fi->fib_prefsrc, rta->rta_prefsrc, 2); |
| 315 | |
| 316 | if (rta->rta_mp) { |
| 317 | if ((err = dn_fib_get_nhs(fi, rta->rta_mp, r)) != 0) |
| 318 | goto failure; |
| 319 | if (rta->rta_oif && fi->fib_nh->nh_oif != *rta->rta_oif) |
| 320 | goto err_inval; |
| 321 | if (rta->rta_gw && memcmp(&fi->fib_nh->nh_gw, rta->rta_gw, 2)) |
| 322 | goto err_inval; |
| 323 | } else { |
| 324 | struct dn_fib_nh *nh = fi->fib_nh; |
| 325 | if (rta->rta_oif) |
| 326 | nh->nh_oif = *rta->rta_oif; |
| 327 | if (rta->rta_gw) |
| 328 | memcpy(&nh->nh_gw, rta->rta_gw, 2); |
| 329 | nh->nh_flags = r->rtm_flags; |
| 330 | nh->nh_weight = 1; |
| 331 | } |
| 332 | |
| 333 | if (r->rtm_type == RTN_NAT) { |
| 334 | if (rta->rta_gw == NULL || nhs != 1 || rta->rta_oif) |
| 335 | goto err_inval; |
| 336 | memcpy(&fi->fib_nh->nh_gw, rta->rta_gw, 2); |
| 337 | goto link_it; |
| 338 | } |
| 339 | |
| 340 | if (dn_fib_props[r->rtm_type].error) { |
| 341 | if (rta->rta_gw || rta->rta_oif || rta->rta_mp) |
| 342 | goto err_inval; |
| 343 | goto link_it; |
| 344 | } |
| 345 | |
| 346 | if (r->rtm_scope > RT_SCOPE_HOST) |
| 347 | goto err_inval; |
| 348 | |
| 349 | if (r->rtm_scope == RT_SCOPE_HOST) { |
| 350 | struct dn_fib_nh *nh = fi->fib_nh; |
| 351 | |
| 352 | /* Local address is added */ |
| 353 | if (nhs != 1 || nh->nh_gw) |
| 354 | goto err_inval; |
| 355 | nh->nh_scope = RT_SCOPE_NOWHERE; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 356 | nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | err = -ENODEV; |
| 358 | if (nh->nh_dev == NULL) |
| 359 | goto failure; |
| 360 | } else { |
| 361 | change_nexthops(fi) { |
| 362 | if ((err = dn_fib_check_nh(r, fi, nh)) != 0) |
| 363 | goto failure; |
| 364 | } endfor_nexthops(fi) |
| 365 | } |
| 366 | |
| 367 | if (fi->fib_prefsrc) { |
| 368 | if (r->rtm_type != RTN_LOCAL || rta->rta_dst == NULL || |
| 369 | memcmp(&fi->fib_prefsrc, rta->rta_dst, 2)) |
| 370 | if (dnet_addr_type(fi->fib_prefsrc) != RTN_LOCAL) |
| 371 | goto err_inval; |
| 372 | } |
| 373 | |
| 374 | link_it: |
| 375 | if ((ofi = dn_fib_find_info(fi)) != NULL) { |
| 376 | fi->fib_dead = 1; |
| 377 | dn_fib_free_info(fi); |
| 378 | ofi->fib_treeref++; |
| 379 | return ofi; |
| 380 | } |
| 381 | |
| 382 | fi->fib_treeref++; |
| 383 | atomic_inc(&fi->fib_clntref); |
Steven Whitehouse | a22ec36 | 2006-08-09 16:00:57 -0700 | [diff] [blame] | 384 | spin_lock(&dn_fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | fi->fib_next = dn_fib_info_list; |
| 386 | fi->fib_prev = NULL; |
| 387 | if (dn_fib_info_list) |
| 388 | dn_fib_info_list->fib_prev = fi; |
| 389 | dn_fib_info_list = fi; |
Steven Whitehouse | a22ec36 | 2006-08-09 16:00:57 -0700 | [diff] [blame] | 390 | spin_unlock(&dn_fib_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return fi; |
| 392 | |
| 393 | err_inval: |
| 394 | err = -EINVAL; |
| 395 | |
| 396 | failure: |
| 397 | *errp = err; |
| 398 | if (fi) { |
| 399 | fi->fib_dead = 1; |
| 400 | dn_fib_free_info(fi); |
| 401 | } |
| 402 | |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | int dn_fib_semantic_match(int type, struct dn_fib_info *fi, const struct flowi *fl, struct dn_fib_res *res) |
| 407 | { |
| 408 | int err = dn_fib_props[type].error; |
| 409 | |
| 410 | if (err == 0) { |
| 411 | if (fi->fib_flags & RTNH_F_DEAD) |
| 412 | return 1; |
| 413 | |
| 414 | res->fi = fi; |
| 415 | |
| 416 | switch(type) { |
| 417 | case RTN_NAT: |
| 418 | DN_FIB_RES_RESET(*res); |
| 419 | atomic_inc(&fi->fib_clntref); |
| 420 | return 0; |
| 421 | case RTN_UNICAST: |
| 422 | case RTN_LOCAL: |
| 423 | for_nexthops(fi) { |
| 424 | if (nh->nh_flags & RTNH_F_DEAD) |
| 425 | continue; |
| 426 | if (!fl->oif || fl->oif == nh->nh_oif) |
| 427 | break; |
| 428 | } |
| 429 | if (nhsel < fi->fib_nhs) { |
| 430 | res->nh_sel = nhsel; |
| 431 | atomic_inc(&fi->fib_clntref); |
| 432 | return 0; |
| 433 | } |
| 434 | endfor_nexthops(fi); |
| 435 | res->fi = NULL; |
| 436 | return 1; |
| 437 | default: |
| 438 | if (net_ratelimit()) |
| 439 | printk("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n", type); |
| 440 | res->fi = NULL; |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | } |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | void dn_fib_select_multipath(const struct flowi *fl, struct dn_fib_res *res) |
| 448 | { |
| 449 | struct dn_fib_info *fi = res->fi; |
| 450 | int w; |
| 451 | |
| 452 | spin_lock_bh(&dn_fib_multipath_lock); |
| 453 | if (fi->fib_power <= 0) { |
| 454 | int power = 0; |
| 455 | change_nexthops(fi) { |
| 456 | if (!(nh->nh_flags&RTNH_F_DEAD)) { |
| 457 | power += nh->nh_weight; |
| 458 | nh->nh_power = nh->nh_weight; |
| 459 | } |
| 460 | } endfor_nexthops(fi); |
| 461 | fi->fib_power = power; |
| 462 | if (power < 0) { |
| 463 | spin_unlock_bh(&dn_fib_multipath_lock); |
| 464 | res->nh_sel = 0; |
| 465 | return; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | w = jiffies % fi->fib_power; |
| 470 | |
| 471 | change_nexthops(fi) { |
| 472 | if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) { |
| 473 | if ((w -= nh->nh_power) <= 0) { |
| 474 | nh->nh_power--; |
| 475 | fi->fib_power--; |
| 476 | res->nh_sel = nhsel; |
| 477 | spin_unlock_bh(&dn_fib_multipath_lock); |
| 478 | return; |
| 479 | } |
| 480 | } |
| 481 | } endfor_nexthops(fi); |
| 482 | res->nh_sel = 0; |
| 483 | spin_unlock_bh(&dn_fib_multipath_lock); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | static int dn_fib_check_attr(struct rtmsg *r, struct rtattr **rta) |
| 488 | { |
| 489 | int i; |
| 490 | |
| 491 | for(i = 1; i <= RTA_MAX; i++) { |
| 492 | struct rtattr *attr = rta[i-1]; |
| 493 | if (attr) { |
| 494 | if (RTA_PAYLOAD(attr) < 4 && RTA_PAYLOAD(attr) != 2) |
| 495 | return -EINVAL; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 496 | if (i != RTA_MULTIPATH && i != RTA_METRICS && |
| 497 | i != RTA_TABLE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | rta[i-1] = (struct rtattr *)RTA_DATA(attr); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 505 | static int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 507 | struct net *net = skb->sk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | struct dn_fib_table *tb; |
| 509 | struct rtattr **rta = arg; |
| 510 | struct rtmsg *r = NLMSG_DATA(nlh); |
| 511 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 512 | if (net != &init_net) |
| 513 | return -EINVAL; |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (dn_fib_check_attr(r, rta)) |
| 516 | return -EINVAL; |
| 517 | |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 518 | tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (tb) |
| 520 | return tb->delete(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb)); |
| 521 | |
| 522 | return -ESRCH; |
| 523 | } |
| 524 | |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 525 | static int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 527 | struct net *net = skb->sk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct dn_fib_table *tb; |
| 529 | struct rtattr **rta = arg; |
| 530 | struct rtmsg *r = NLMSG_DATA(nlh); |
| 531 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 532 | if (net != &init_net) |
| 533 | return -EINVAL; |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (dn_fib_check_attr(r, rta)) |
| 536 | return -EINVAL; |
| 537 | |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 538 | tb = dn_fib_get_table(rtm_get_table(rta, r->rtm_table), 1); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 539 | if (tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return tb->insert(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb)); |
| 541 | |
| 542 | return -ENOBUFS; |
| 543 | } |
| 544 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 545 | static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | struct dn_fib_table *tb; |
| 548 | struct { |
| 549 | struct nlmsghdr nlh; |
| 550 | struct rtmsg rtm; |
| 551 | } req; |
| 552 | struct dn_kern_rta rta; |
| 553 | |
| 554 | memset(&req.rtm, 0, sizeof(req.rtm)); |
| 555 | memset(&rta, 0, sizeof(rta)); |
| 556 | |
| 557 | if (type == RTN_UNICAST) |
| 558 | tb = dn_fib_get_table(RT_MIN_TABLE, 1); |
| 559 | else |
| 560 | tb = dn_fib_get_table(RT_TABLE_LOCAL, 1); |
| 561 | |
| 562 | if (tb == NULL) |
| 563 | return; |
| 564 | |
| 565 | req.nlh.nlmsg_len = sizeof(req); |
| 566 | req.nlh.nlmsg_type = cmd; |
| 567 | req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND; |
| 568 | req.nlh.nlmsg_pid = 0; |
| 569 | req.nlh.nlmsg_seq = 0; |
| 570 | |
| 571 | req.rtm.rtm_dst_len = dst_len; |
| 572 | req.rtm.rtm_table = tb->n; |
| 573 | req.rtm.rtm_protocol = RTPROT_KERNEL; |
| 574 | req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST); |
| 575 | req.rtm.rtm_type = type; |
| 576 | |
| 577 | rta.rta_dst = &dst; |
| 578 | rta.rta_prefsrc = &ifa->ifa_local; |
| 579 | rta.rta_oif = &ifa->ifa_dev->dev->ifindex; |
| 580 | |
| 581 | if (cmd == RTM_NEWROUTE) |
| 582 | tb->insert(tb, &req.rtm, &rta, &req.nlh, NULL); |
| 583 | else |
| 584 | tb->delete(tb, &req.rtm, &rta, &req.nlh, NULL); |
| 585 | } |
| 586 | |
| 587 | static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa) |
| 588 | { |
| 589 | |
| 590 | fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa); |
| 591 | |
| 592 | #if 0 |
| 593 | if (!(dev->flags&IFF_UP)) |
| 594 | return; |
| 595 | /* In the future, we will want to add default routes here */ |
| 596 | |
| 597 | #endif |
| 598 | } |
| 599 | |
| 600 | static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa) |
| 601 | { |
| 602 | int found_it = 0; |
| 603 | struct net_device *dev; |
| 604 | struct dn_dev *dn_db; |
| 605 | struct dn_ifaddr *ifa2; |
| 606 | |
| 607 | ASSERT_RTNL(); |
| 608 | |
| 609 | /* Scan device list */ |
| 610 | read_lock(&dev_base_lock); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 611 | for_each_netdev(&init_net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | dn_db = dev->dn_ptr; |
| 613 | if (dn_db == NULL) |
| 614 | continue; |
| 615 | for(ifa2 = dn_db->ifa_list; ifa2; ifa2 = ifa2->ifa_next) { |
| 616 | if (ifa2->ifa_local == ifa->ifa_local) { |
| 617 | found_it = 1; |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | read_unlock(&dev_base_lock); |
| 623 | |
| 624 | if (found_it == 0) { |
| 625 | fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa); |
| 626 | |
| 627 | if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) { |
| 628 | if (dn_fib_sync_down(ifa->ifa_local, NULL, 0)) |
| 629 | dn_fib_flush(); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static void dn_fib_disable_addr(struct net_device *dev, int force) |
| 635 | { |
| 636 | if (dn_fib_sync_down(0, dev, force)) |
| 637 | dn_fib_flush(); |
| 638 | dn_rt_cache_flush(0); |
| 639 | neigh_ifdown(&dn_neigh_table, dev); |
| 640 | } |
| 641 | |
| 642 | static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 643 | { |
| 644 | struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr; |
| 645 | |
| 646 | switch(event) { |
| 647 | case NETDEV_UP: |
| 648 | dn_fib_add_ifaddr(ifa); |
| 649 | dn_fib_sync_up(ifa->ifa_dev->dev); |
| 650 | dn_rt_cache_flush(-1); |
| 651 | break; |
| 652 | case NETDEV_DOWN: |
| 653 | dn_fib_del_ifaddr(ifa); |
| 654 | if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) { |
| 655 | dn_fib_disable_addr(ifa->ifa_dev->dev, 1); |
| 656 | } else { |
| 657 | dn_rt_cache_flush(-1); |
| 658 | } |
| 659 | break; |
| 660 | } |
| 661 | return NOTIFY_DONE; |
| 662 | } |
| 663 | |
Adrian Bunk | 2aa7f36 | 2006-08-14 23:55:20 -0700 | [diff] [blame] | 664 | static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 666 | int ret = 0; |
| 667 | int scope = RT_SCOPE_NOWHERE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 669 | if (force) |
| 670 | scope = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 672 | for_fib_info() { |
| 673 | /* |
| 674 | * This makes no sense for DECnet.... we will almost |
| 675 | * certainly have more than one local address the same |
| 676 | * over all our interfaces. It needs thinking about |
| 677 | * some more. |
| 678 | */ |
| 679 | if (local && fi->fib_prefsrc == local) { |
| 680 | fi->fib_flags |= RTNH_F_DEAD; |
| 681 | ret++; |
| 682 | } else if (dev && fi->fib_nhs) { |
| 683 | int dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 685 | change_nexthops(fi) { |
| 686 | if (nh->nh_flags&RTNH_F_DEAD) |
| 687 | dead++; |
| 688 | else if (nh->nh_dev == dev && |
| 689 | nh->nh_scope != scope) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | spin_lock_bh(&dn_fib_multipath_lock); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 691 | nh->nh_flags |= RTNH_F_DEAD; |
| 692 | fi->fib_power -= nh->nh_power; |
| 693 | nh->nh_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | spin_unlock_bh(&dn_fib_multipath_lock); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 695 | dead++; |
| 696 | } |
| 697 | } endfor_nexthops(fi) |
| 698 | if (dead == fi->fib_nhs) { |
| 699 | fi->fib_flags |= RTNH_F_DEAD; |
| 700 | ret++; |
| 701 | } |
| 702 | } |
| 703 | } endfor_fib_info(); |
| 704 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | |
Adrian Bunk | 2aa7f36 | 2006-08-14 23:55:20 -0700 | [diff] [blame] | 708 | static int dn_fib_sync_up(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 710 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 712 | if (!(dev->flags&IFF_UP)) |
| 713 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 715 | for_fib_info() { |
| 716 | int alive = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 718 | change_nexthops(fi) { |
| 719 | if (!(nh->nh_flags&RTNH_F_DEAD)) { |
| 720 | alive++; |
| 721 | continue; |
| 722 | } |
| 723 | if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP)) |
| 724 | continue; |
| 725 | if (nh->nh_dev != dev || dev->dn_ptr == NULL) |
| 726 | continue; |
| 727 | alive++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | spin_lock_bh(&dn_fib_multipath_lock); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 729 | nh->nh_power = 0; |
| 730 | nh->nh_flags &= ~RTNH_F_DEAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | spin_unlock_bh(&dn_fib_multipath_lock); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 732 | } endfor_nexthops(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 734 | if (alive > 0) { |
| 735 | fi->fib_flags &= ~RTNH_F_DEAD; |
| 736 | ret++; |
| 737 | } |
| 738 | } endfor_fib_info(); |
| 739 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | static struct notifier_block dn_fib_dnaddr_notifier = { |
| 743 | .notifier_call = dn_fib_dnaddr_event, |
| 744 | }; |
| 745 | |
| 746 | void __exit dn_fib_cleanup(void) |
| 747 | { |
| 748 | dn_fib_table_cleanup(); |
| 749 | dn_fib_rules_cleanup(); |
| 750 | |
| 751 | unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | void __init dn_fib_init(void) |
| 756 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | dn_fib_table_init(); |
| 758 | dn_fib_rules_init(); |
| 759 | |
| 760 | register_dnaddr_notifier(&dn_fib_dnaddr_notifier); |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 761 | |
| 762 | rtnl_register(PF_DECnet, RTM_NEWROUTE, dn_fib_rtm_newroute, NULL); |
| 763 | rtnl_register(PF_DECnet, RTM_DELROUTE, dn_fib_rtm_delroute, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |