dn_fib.c 18.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * DECnet       An implementation of the DECnet protocol suite for the LINUX
 *              operating system.  DECnet is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              DECnet Routing Forwarding Information Base (Glue/Info List)
 *
 * Author:      Steve Whitehouse <SteveW@ACM.org>
 *
 *
 * Changes:
 *              Alexey Kuznetsov : SMP locking changes
 *              Steve Whitehouse : Rewrote it... Well to be more correct, I
 *                                 copied most of it from the ipv4 fib code.
 *              Steve Whitehouse : Updated it in style and fixed a few bugs
 *                                 which were fixed in the ipv4 code since
 *                                 this code was copied from it.
 *
 */
#include <linux/string.h>
#include <linux/net.h>
#include <linux/socket.h>
24
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33
#include <linux/sockios.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/proc_fs.h>
#include <linux/netdevice.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
34
#include <linux/atomic.h>
35
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38
#include <net/neighbour.h>
#include <net/dst.h>
#include <net/flow.h>
39
#include <net/fib_rules.h>
Linus Torvalds's avatar
Linus Torvalds committed
40 41 42 43 44
#include <net/dn.h>
#include <net/dn_route.h>
#include <net/dn_fib.h>
#include <net/dn_neigh.h>
#include <net/dn_dev.h>
45
#include <net/nexthop.h>
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#define RT_MIN_TABLE 1

#define for_fib_info() { struct dn_fib_info *fi;\
	for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
#define endfor_fib_info() }

#define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
	for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)

#define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
	for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)

#define endfor_nexthops(fi) }

static DEFINE_SPINLOCK(dn_fib_multipath_lock);
static struct dn_fib_info *dn_fib_info_list;
63
static DEFINE_SPINLOCK(dn_fib_info_lock);
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66 67 68

static struct
{
	int error;
	u8 scope;
69
} dn_fib_props[RTN_MAX+1] = {
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83
	[RTN_UNSPEC] =      { .error = 0,       .scope = RT_SCOPE_NOWHERE },
	[RTN_UNICAST] =     { .error = 0,       .scope = RT_SCOPE_UNIVERSE },
	[RTN_LOCAL] =       { .error = 0,       .scope = RT_SCOPE_HOST },
	[RTN_BROADCAST] =   { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
	[RTN_ANYCAST] =     { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
	[RTN_MULTICAST] =   { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
	[RTN_BLACKHOLE] =   { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE },
	[RTN_UNREACHABLE] = { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE },
	[RTN_PROHIBIT] =    { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE },
	[RTN_THROW] =       { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE },
	[RTN_NAT] =         { .error = 0,       .scope = RT_SCOPE_NOWHERE },
	[RTN_XRESOLVE] =    { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE },
};

Adrian Bunk's avatar
Adrian Bunk committed
84 85 86
static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force);
static int dn_fib_sync_up(struct net_device *dev);

Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
void dn_fib_free_info(struct dn_fib_info *fi)
{
	if (fi->fib_dead == 0) {
		printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
		return;
	}

	change_nexthops(fi) {
		if (nh->nh_dev)
			dev_put(nh->nh_dev);
		nh->nh_dev = NULL;
	} endfor_nexthops(fi);
	kfree(fi);
}

void dn_fib_release_info(struct dn_fib_info *fi)
{
104
	spin_lock(&dn_fib_info_lock);
Linus Torvalds's avatar
Linus Torvalds committed
105 106 107 108 109 110 111 112 113 114
	if (fi && --fi->fib_treeref == 0) {
		if (fi->fib_next)
			fi->fib_next->fib_prev = fi->fib_prev;
		if (fi->fib_prev)
			fi->fib_prev->fib_next = fi->fib_next;
		if (fi == dn_fib_info_list)
			dn_fib_info_list = fi->fib_next;
		fi->fib_dead = 1;
		dn_fib_info_put(fi);
	}
115
	spin_unlock(&dn_fib_info_lock);
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

static inline int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
{
	const struct dn_fib_nh *onh = ofi->fib_nh;

	for_nexthops(fi) {
		if (nh->nh_oif != onh->nh_oif ||
			nh->nh_gw != onh->nh_gw ||
			nh->nh_scope != onh->nh_scope ||
			nh->nh_weight != onh->nh_weight ||
			((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
				return -1;
		onh++;
	} endfor_nexthops(fi);
	return 0;
}

static inline struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
{
	for_fib_info() {
		if (fi->fib_nhs != nfi->fib_nhs)
			continue;
		if (nfi->fib_protocol == fi->fib_protocol &&
			nfi->fib_prefsrc == fi->fib_prefsrc &&
			nfi->fib_priority == fi->fib_priority &&
			memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 &&
			((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
			(nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
				return fi;
	} endfor_fib_info();
	return NULL;
}

150
static int dn_fib_count_nhs(const struct nlattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
151
{
152 153
	struct rtnexthop *nhp = nla_data(attr);
	int nhs = 0, nhlen = nla_len(attr);
Linus Torvalds's avatar
Linus Torvalds committed
154

155
	while (rtnh_ok(nhp, nhlen)) {
Linus Torvalds's avatar
Linus Torvalds committed
156
		nhs++;
157
		nhp = rtnh_next(nhp, &nhlen);
Linus Torvalds's avatar
Linus Torvalds committed
158 159
	}

160 161
	/* leftover implies invalid nexthop configuration, discard it */
	return nhlen > 0 ? 0 : nhs;
Linus Torvalds's avatar
Linus Torvalds committed
162 163
}

164 165
static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct nlattr *attr,
			  const struct rtmsg *r)
Linus Torvalds's avatar
Linus Torvalds committed
166
{
167 168
	struct rtnexthop *nhp = nla_data(attr);
	int nhlen = nla_len(attr);
Linus Torvalds's avatar
Linus Torvalds committed
169 170

	change_nexthops(fi) {
171 172 173
		int attrlen;

		if (!rtnh_ok(nhp, nhlen))
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177 178 179
			return -EINVAL;

		nh->nh_flags  = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
		nh->nh_oif    = nhp->rtnh_ifindex;
		nh->nh_weight = nhp->rtnh_hops + 1;

180 181
		attrlen = rtnh_attrlen(nhp);
		if (attrlen > 0) {
182 183 184 185
			struct nlattr *gw_attr;

			gw_attr = nla_find((struct nlattr *) (nhp + 1), attrlen, RTA_GATEWAY);
			nh->nh_gw = gw_attr ? nla_get_le16(gw_attr) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
186
		}
187 188

		nhp = rtnh_next(nhp, &nhlen);
Linus Torvalds's avatar
Linus Torvalds committed
189 190 191 192 193 194 195 196 197 198 199
	} endfor_nexthops(fi);

	return 0;
}


static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
{
	int err;

	if (nh->nh_gw) {
200
		struct flowidn fld;
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207 208 209
		struct dn_fib_res res;

		if (nh->nh_flags&RTNH_F_ONLINK) {
			struct net_device *dev;

			if (r->rtm_scope >= RT_SCOPE_LINK)
				return -EINVAL;
			if (dnet_addr_type(nh->nh_gw) != RTN_UNICAST)
				return -EINVAL;
210
			if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
211 212 213 214 215 216 217 218 219
				return -ENODEV;
			if (!(dev->flags&IFF_UP))
				return -ENETDOWN;
			nh->nh_dev = dev;
			dev_hold(dev);
			nh->nh_scope = RT_SCOPE_LINK;
			return 0;
		}

220 221 222 223
		memset(&fld, 0, sizeof(fld));
		fld.daddr = nh->nh_gw;
		fld.flowidn_oif = nh->nh_oif;
		fld.flowidn_scope = r->rtm_scope + 1;
Linus Torvalds's avatar
Linus Torvalds committed
224

225 226
		if (fld.flowidn_scope < RT_SCOPE_LINK)
			fld.flowidn_scope = RT_SCOPE_LINK;
Linus Torvalds's avatar
Linus Torvalds committed
227

228
		if ((err = dn_fib_lookup(&fld, &res)) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
			return err;

		err = -EINVAL;
		if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
			goto out;
		nh->nh_scope = res.scope;
		nh->nh_oif = DN_FIB_RES_OIF(res);
		nh->nh_dev = DN_FIB_RES_DEV(res);
		if (nh->nh_dev == NULL)
			goto out;
		dev_hold(nh->nh_dev);
		err = -ENETDOWN;
		if (!(nh->nh_dev->flags & IFF_UP))
			goto out;
		err = 0;
out:
		dn_fib_res_put(&res);
		return err;
	} else {
		struct net_device *dev;

		if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
			return -EINVAL;

253
		dev = __dev_get_by_index(&init_net, nh->nh_oif);
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256 257 258 259 260 261 262 263 264 265 266
		if (dev == NULL || dev->dn_ptr == NULL)
			return -ENODEV;
		if (!(dev->flags&IFF_UP))
			return -ENETDOWN;
		nh->nh_dev = dev;
		dev_hold(nh->nh_dev);
		nh->nh_scope = RT_SCOPE_HOST;
	}

	return 0;
}


267 268
struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct nlattr *attrs[],
				       const struct nlmsghdr *nlh, int *errp)
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273 274
{
	int err;
	struct dn_fib_info *fi = NULL;
	struct dn_fib_info *ofi;
	int nhs = 1;

275 276 277
	if (r->rtm_type > RTN_MAX)
		goto err_inval;

Linus Torvalds's avatar
Linus Torvalds committed
278 279 280
	if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
		goto err_inval;

281 282 283
	if (attrs[RTA_MULTIPATH] &&
	    (nhs = dn_fib_count_nhs(attrs[RTA_MULTIPATH])) == 0)
		goto err_inval;
Linus Torvalds's avatar
Linus Torvalds committed
284

285
	fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288 289 290 291 292 293
	err = -ENOBUFS;
	if (fi == NULL)
		goto failure;

	fi->fib_protocol = r->rtm_protocol;
	fi->fib_nhs = nhs;
	fi->fib_flags = r->rtm_flags;

294 295 296 297 298 299
	if (attrs[RTA_PRIORITY])
		fi->fib_priority = nla_get_u32(attrs[RTA_PRIORITY]);

	if (attrs[RTA_METRICS]) {
		struct nlattr *attr;
		int rem;
300

301 302 303 304
		nla_for_each_nested(attr, attrs[RTA_METRICS], rem) {
			int type = nla_type(attr);

			if (type) {
305 306
				if (type > RTAX_MAX || type == RTAX_CC_ALGO ||
				    nla_len(attr) < 4)
Linus Torvalds's avatar
Linus Torvalds committed
307
					goto err_inval;
308 309

				fi->fib_metrics[type-1] = nla_get_u32(attr);
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313
			}
		}
	}

314 315 316 317 318
	if (attrs[RTA_PREFSRC])
		fi->fib_prefsrc = nla_get_le16(attrs[RTA_PREFSRC]);

	if (attrs[RTA_MULTIPATH]) {
		if ((err = dn_fib_get_nhs(fi, attrs[RTA_MULTIPATH], r)) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
319
			goto failure;
320 321 322

		if (attrs[RTA_OIF] &&
		    fi->fib_nh->nh_oif != nla_get_u32(attrs[RTA_OIF]))
Linus Torvalds's avatar
Linus Torvalds committed
323
			goto err_inval;
324 325 326

		if (attrs[RTA_GATEWAY] &&
		    fi->fib_nh->nh_gw != nla_get_le16(attrs[RTA_GATEWAY]))
Linus Torvalds's avatar
Linus Torvalds committed
327 328 329
			goto err_inval;
	} else {
		struct dn_fib_nh *nh = fi->fib_nh;
330 331 332 333 334 335 336

		if (attrs[RTA_OIF])
			nh->nh_oif = nla_get_u32(attrs[RTA_OIF]);

		if (attrs[RTA_GATEWAY])
			nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);

Linus Torvalds's avatar
Linus Torvalds committed
337 338 339 340 341
		nh->nh_flags = r->rtm_flags;
		nh->nh_weight = 1;
	}

	if (r->rtm_type == RTN_NAT) {
342
		if (!attrs[RTA_GATEWAY] || nhs != 1 || attrs[RTA_OIF])
Linus Torvalds's avatar
Linus Torvalds committed
343
			goto err_inval;
344 345

		fi->fib_nh->nh_gw = nla_get_le16(attrs[RTA_GATEWAY]);
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348 349
		goto link_it;
	}

	if (dn_fib_props[r->rtm_type].error) {
350
		if (attrs[RTA_GATEWAY] || attrs[RTA_OIF] || attrs[RTA_MULTIPATH])
Linus Torvalds's avatar
Linus Torvalds committed
351
			goto err_inval;
352

Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356 357 358 359 360 361 362 363 364 365
		goto link_it;
	}

	if (r->rtm_scope > RT_SCOPE_HOST)
		goto err_inval;

	if (r->rtm_scope == RT_SCOPE_HOST) {
		struct dn_fib_nh *nh = fi->fib_nh;

		/* Local address is added */
		if (nhs != 1 || nh->nh_gw)
			goto err_inval;
		nh->nh_scope = RT_SCOPE_NOWHERE;
366
		nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif);
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373 374 375 376 377
		err = -ENODEV;
		if (nh->nh_dev == NULL)
			goto failure;
	} else {
		change_nexthops(fi) {
			if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
				goto failure;
		} endfor_nexthops(fi)
	}

	if (fi->fib_prefsrc) {
378 379
		if (r->rtm_type != RTN_LOCAL || !attrs[RTA_DST] ||
		    fi->fib_prefsrc != nla_get_le16(attrs[RTA_DST]))
Linus Torvalds's avatar
Linus Torvalds committed
380 381 382 383 384 385 386 387 388 389 390 391 392
			if (dnet_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
				goto err_inval;
	}

link_it:
	if ((ofi = dn_fib_find_info(fi)) != NULL) {
		fi->fib_dead = 1;
		dn_fib_free_info(fi);
		ofi->fib_treeref++;
		return ofi;
	}

	fi->fib_treeref++;
393
	refcount_set(&fi->fib_clntref, 1);
394
	spin_lock(&dn_fib_info_lock);
Linus Torvalds's avatar
Linus Torvalds committed
395 396 397 398 399
	fi->fib_next = dn_fib_info_list;
	fi->fib_prev = NULL;
	if (dn_fib_info_list)
		dn_fib_info_list->fib_prev = fi;
	dn_fib_info_list = fi;
400
	spin_unlock(&dn_fib_info_lock);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	return fi;

err_inval:
	err = -EINVAL;

failure:
	*errp = err;
	if (fi) {
		fi->fib_dead = 1;
		dn_fib_free_info(fi);
	}

	return NULL;
}

416
int dn_fib_semantic_match(int type, struct dn_fib_info *fi, const struct flowidn *fld, struct dn_fib_res *res)
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421 422 423 424 425
{
	int err = dn_fib_props[type].error;

	if (err == 0) {
		if (fi->fib_flags & RTNH_F_DEAD)
			return 1;

		res->fi = fi;

426 427 428
		switch (type) {
		case RTN_NAT:
			DN_FIB_RES_RESET(*res);
429
			refcount_inc(&fi->fib_clntref);
430 431 432 433 434 435 436 437 438 439 440 441
			return 0;
		case RTN_UNICAST:
		case RTN_LOCAL:
			for_nexthops(fi) {
				if (nh->nh_flags & RTNH_F_DEAD)
					continue;
				if (!fld->flowidn_oif ||
				    fld->flowidn_oif == nh->nh_oif)
					break;
			}
			if (nhsel < fi->fib_nhs) {
				res->nh_sel = nhsel;
442
				refcount_inc(&fi->fib_clntref);
Linus Torvalds's avatar
Linus Torvalds committed
443
				return 0;
444 445 446 447 448
			}
			endfor_nexthops(fi);
			res->fi = NULL;
			return 1;
		default:
449 450
			net_err_ratelimited("DECnet: impossible routing event : dn_fib_semantic_match type=%d\n",
					    type);
451 452
			res->fi = NULL;
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456 457
		}
	}
	return err;
}

458
void dn_fib_select_multipath(const struct flowidn *fld, struct dn_fib_res *res)
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
	struct dn_fib_info *fi = res->fi;
	int w;

	spin_lock_bh(&dn_fib_multipath_lock);
	if (fi->fib_power <= 0) {
		int power = 0;
		change_nexthops(fi) {
			if (!(nh->nh_flags&RTNH_F_DEAD)) {
				power += nh->nh_weight;
				nh->nh_power = nh->nh_weight;
			}
		} endfor_nexthops(fi);
		fi->fib_power = power;
		if (power < 0) {
			spin_unlock_bh(&dn_fib_multipath_lock);
			res->nh_sel = 0;
			return;
		}
	}

	w = jiffies % fi->fib_power;

	change_nexthops(fi) {
		if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
			if ((w -= nh->nh_power) <= 0) {
				nh->nh_power--;
				fi->fib_power--;
				res->nh_sel = nhsel;
				spin_unlock_bh(&dn_fib_multipath_lock);
				return;
			}
		}
	} endfor_nexthops(fi);
	res->nh_sel = 0;
	spin_unlock_bh(&dn_fib_multipath_lock);
}

497
static inline u32 rtm_get_table(struct nlattr *attrs[], u8 table)
498
{
499 500
	if (attrs[RTA_TABLE])
		table = nla_get_u32(attrs[RTA_TABLE]);
501 502 503 504

	return table;
}

505 506
static int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
507
{
508
	struct net *net = sock_net(skb->sk);
Linus Torvalds's avatar
Linus Torvalds committed
509
	struct dn_fib_table *tb;
510 511 512
	struct rtmsg *r = nlmsg_data(nlh);
	struct nlattr *attrs[RTA_MAX+1];
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
513

514
	if (!netlink_capable(skb, CAP_NET_ADMIN))
515 516
		return -EPERM;

517
	if (!net_eq(net, &init_net))
518 519
		return -EINVAL;

520
	err = nlmsg_parse(nlh, sizeof(*r), attrs, RTA_MAX, rtm_dn_policy,
521
			  extack);
522 523
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
524

525 526 527
	tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 0);
	if (!tb)
		return -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
528

529
	return tb->delete(tb, r, attrs, nlh, &NETLINK_CB(skb));
Linus Torvalds's avatar
Linus Torvalds committed
530 531
}

532 533
static int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
Linus Torvalds's avatar
Linus Torvalds committed
534
{
535
	struct net *net = sock_net(skb->sk);
Linus Torvalds's avatar
Linus Torvalds committed
536
	struct dn_fib_table *tb;
537 538 539
	struct rtmsg *r = nlmsg_data(nlh);
	struct nlattr *attrs[RTA_MAX+1];
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
540

541
	if (!netlink_capable(skb, CAP_NET_ADMIN))
542 543
		return -EPERM;

544
	if (!net_eq(net, &init_net))
545 546
		return -EINVAL;

547
	err = nlmsg_parse(nlh, sizeof(*r), attrs, RTA_MAX, rtm_dn_policy,
548
			  extack);
549 550
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
551

552 553 554
	tb = dn_fib_get_table(rtm_get_table(attrs, r->rtm_table), 1);
	if (!tb)
		return -ENOBUFS;
Linus Torvalds's avatar
Linus Torvalds committed
555

556
	return tb->insert(tb, r, attrs, nlh, &NETLINK_CB(skb));
Linus Torvalds's avatar
Linus Torvalds committed
557 558
}

559
static void fib_magic(int cmd, int type, __le16 dst, int dst_len, struct dn_ifaddr *ifa)
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565
{
	struct dn_fib_table *tb;
	struct {
		struct nlmsghdr nlh;
		struct rtmsg rtm;
	} req;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
	struct {
		struct nlattr hdr;
		__le16 dst;
	} dst_attr = {
		.dst = dst,
	};
	struct {
		struct nlattr hdr;
		__le16 prefsrc;
	} prefsrc_attr = {
		.prefsrc = ifa->ifa_local,
	};
	struct {
		struct nlattr hdr;
		u32 oif;
	} oif_attr = {
		.oif = ifa->ifa_dev->dev->ifindex,
	};
	struct nlattr *attrs[RTA_MAX+1] = {
		[RTA_DST] = (struct nlattr *) &dst_attr,
		[RTA_PREFSRC] = (struct nlattr * ) &prefsrc_attr,
		[RTA_OIF] = (struct nlattr *) &oif_attr,
	};
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	memset(&req.rtm, 0, sizeof(req.rtm));

	if (type == RTN_UNICAST)
		tb = dn_fib_get_table(RT_MIN_TABLE, 1);
	else
		tb = dn_fib_get_table(RT_TABLE_LOCAL, 1);

	if (tb == NULL)
		return;

	req.nlh.nlmsg_len = sizeof(req);
	req.nlh.nlmsg_type = cmd;
	req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
	req.nlh.nlmsg_pid = 0;
	req.nlh.nlmsg_seq = 0;

	req.rtm.rtm_dst_len = dst_len;
	req.rtm.rtm_table = tb->n;
	req.rtm.rtm_protocol = RTPROT_KERNEL;
	req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
	req.rtm.rtm_type = type;

	if (cmd == RTM_NEWROUTE)
613
		tb->insert(tb, &req.rtm, attrs, &req.nlh, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
614
	else
615
		tb->delete(tb, &req.rtm, attrs, &req.nlh, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
}

static void dn_fib_add_ifaddr(struct dn_ifaddr *ifa)
{

	fib_magic(RTM_NEWROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);

#if 0
	if (!(dev->flags&IFF_UP))
		return;
	/* In the future, we will want to add default routes here */

#endif
}

static void dn_fib_del_ifaddr(struct dn_ifaddr *ifa)
{
	int found_it = 0;
	struct net_device *dev;
	struct dn_dev *dn_db;
	struct dn_ifaddr *ifa2;

	ASSERT_RTNL();

	/* Scan device list */
641 642
	rcu_read_lock();
	for_each_netdev_rcu(&init_net, dev) {
643
		dn_db = rcu_dereference(dev->dn_ptr);
Linus Torvalds's avatar
Linus Torvalds committed
644 645
		if (dn_db == NULL)
			continue;
646 647 648
		for (ifa2 = rcu_dereference(dn_db->ifa_list);
		     ifa2 != NULL;
		     ifa2 = rcu_dereference(ifa2->ifa_next)) {
Linus Torvalds's avatar
Linus Torvalds committed
649 650 651 652 653 654
			if (ifa2->ifa_local == ifa->ifa_local) {
				found_it = 1;
				break;
			}
		}
	}
655
	rcu_read_unlock();
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

	if (found_it == 0) {
		fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 16, ifa);

		if (dnet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
			if (dn_fib_sync_down(ifa->ifa_local, NULL, 0))
				dn_fib_flush();
		}
	}
}

static void dn_fib_disable_addr(struct net_device *dev, int force)
{
	if (dn_fib_sync_down(0, dev, force))
		dn_fib_flush();
	dn_rt_cache_flush(0);
	neigh_ifdown(&dn_neigh_table, dev);
}

static int dn_fib_dnaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
{
	struct dn_ifaddr *ifa = (struct dn_ifaddr *)ptr;

679 680 681 682 683 684 685 686 687 688 689
	switch (event) {
	case NETDEV_UP:
		dn_fib_add_ifaddr(ifa);
		dn_fib_sync_up(ifa->ifa_dev->dev);
		dn_rt_cache_flush(-1);
		break;
	case NETDEV_DOWN:
		dn_fib_del_ifaddr(ifa);
		if (ifa->ifa_dev && ifa->ifa_dev->ifa_list == NULL) {
			dn_fib_disable_addr(ifa->ifa_dev->dev, 1);
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
690
			dn_rt_cache_flush(-1);
691 692
		}
		break;
Linus Torvalds's avatar
Linus Torvalds committed
693 694 695 696
	}
	return NOTIFY_DONE;
}

Adrian Bunk's avatar
Adrian Bunk committed
697
static int dn_fib_sync_down(__le16 local, struct net_device *dev, int force)
Linus Torvalds's avatar
Linus Torvalds committed
698
{
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	int ret = 0;
	int scope = RT_SCOPE_NOWHERE;

	if (force)
		scope = -1;

	for_fib_info() {
		/*
		 * This makes no sense for DECnet.... we will almost
		 * certainly have more than one local address the same
		 * over all our interfaces. It needs thinking about
		 * some more.
		 */
		if (local && fi->fib_prefsrc == local) {
			fi->fib_flags |= RTNH_F_DEAD;
			ret++;
		} else if (dev && fi->fib_nhs) {
			int dead = 0;

			change_nexthops(fi) {
				if (nh->nh_flags&RTNH_F_DEAD)
					dead++;
				else if (nh->nh_dev == dev &&
						nh->nh_scope != scope) {
Linus Torvalds's avatar
Linus Torvalds committed
723
					spin_lock_bh(&dn_fib_multipath_lock);
724 725 726
					nh->nh_flags |= RTNH_F_DEAD;
					fi->fib_power -= nh->nh_power;
					nh->nh_power = 0;
Linus Torvalds's avatar
Linus Torvalds committed
727
					spin_unlock_bh(&dn_fib_multipath_lock);
728 729 730 731 732 733 734 735 736 737
					dead++;
				}
			} endfor_nexthops(fi)
			if (dead == fi->fib_nhs) {
				fi->fib_flags |= RTNH_F_DEAD;
				ret++;
			}
		}
	} endfor_fib_info();
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740
}


Adrian Bunk's avatar
Adrian Bunk committed
741
static int dn_fib_sync_up(struct net_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
742
{
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	int ret = 0;

	if (!(dev->flags&IFF_UP))
		return 0;

	for_fib_info() {
		int alive = 0;

		change_nexthops(fi) {
			if (!(nh->nh_flags&RTNH_F_DEAD)) {
				alive++;
				continue;
			}
			if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
				continue;
			if (nh->nh_dev != dev || dev->dn_ptr == NULL)
				continue;
			alive++;
Linus Torvalds's avatar
Linus Torvalds committed
761
			spin_lock_bh(&dn_fib_multipath_lock);
762 763
			nh->nh_power = 0;
			nh->nh_flags &= ~RTNH_F_DEAD;
Linus Torvalds's avatar
Linus Torvalds committed
764
			spin_unlock_bh(&dn_fib_multipath_lock);
765 766 767 768 769 770 771 772
		} endfor_nexthops(fi);

		if (alive > 0) {
			fi->fib_flags &= ~RTNH_F_DEAD;
			ret++;
		}
	} endfor_fib_info();
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
}

static struct notifier_block dn_fib_dnaddr_notifier = {
	.notifier_call = dn_fib_dnaddr_event,
};

void __exit dn_fib_cleanup(void)
{
	dn_fib_table_cleanup();
	dn_fib_rules_cleanup();

	unregister_dnaddr_notifier(&dn_fib_dnaddr_notifier);
}


void __init dn_fib_init(void)
{
	dn_fib_table_init();
	dn_fib_rules_init();

	register_dnaddr_notifier(&dn_fib_dnaddr_notifier);
794

795 796 797 798
	rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_NEWROUTE,
			     dn_fib_rtm_newroute, NULL, 0);
	rtnl_register_module(THIS_MODULE, PF_DECnet, RTM_DELROUTE,
			     dn_fib_rtm_delroute, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
799
}