aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4
diff options
context:
space:
mode:
authorThomas Graf <tgraf@suug.ch>2006-08-17 21:14:52 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2006-09-22 17:55:04 -0400
commit4e902c57417c4c285b98ba2722468d1c3ed83d1b (patch)
tree83251829a4bc9628a3543e5f70e7b11090fe22d9 /net/ipv4
parentab32ea5d8a760e7dd4339634e95d7be24ee5b842 (diff)
[IPv4]: FIB configuration using struct fib_config
Introduces struct fib_config replacing the ugly struct kern_rta prone to ordering issues. Avoids creating faked netlink messages for auto generated routes or requests via ioctl. A new interface net/nexthop.h is added to help navigate through nexthop configuration arrays. A new struct nl_info will be used to carry the necessary netlink information to be used for notifications later on. Signed-off-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/ipv4')
-rw-r--r--net/ipv4/fib_frontend.c364
-rw-r--r--net/ipv4/fib_hash.c94
-rw-r--r--net/ipv4/fib_lookup.h11
-rw-r--r--net/ipv4/fib_semantics.c385
-rw-r--r--net/ipv4/fib_trie.c76
5 files changed, 489 insertions, 441 deletions
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index ad4c14f968a1..acc18bdf2dee 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -253,42 +253,190 @@ e_inval:
253 253
254#ifndef CONFIG_IP_NOSIOCRT 254#ifndef CONFIG_IP_NOSIOCRT
255 255
256static inline u32 sk_extract_addr(struct sockaddr *addr)
257{
258 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
259}
260
261static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
262{
263 struct nlattr *nla;
264
265 nla = (struct nlattr *) ((char *) mx + len);
266 nla->nla_type = type;
267 nla->nla_len = nla_attr_size(4);
268 *(u32 *) nla_data(nla) = value;
269
270 return len + nla_total_size(4);
271}
272
273static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
274 struct fib_config *cfg)
275{
276 u32 addr;
277 int plen;
278
279 memset(cfg, 0, sizeof(*cfg));
280
281 if (rt->rt_dst.sa_family != AF_INET)
282 return -EAFNOSUPPORT;
283
284 /*
285 * Check mask for validity:
286 * a) it must be contiguous.
287 * b) destination must have all host bits clear.
288 * c) if application forgot to set correct family (AF_INET),
289 * reject request unless it is absolutely clear i.e.
290 * both family and mask are zero.
291 */
292 plen = 32;
293 addr = sk_extract_addr(&rt->rt_dst);
294 if (!(rt->rt_flags & RTF_HOST)) {
295 u32 mask = sk_extract_addr(&rt->rt_genmask);
296
297 if (rt->rt_genmask.sa_family != AF_INET) {
298 if (mask || rt->rt_genmask.sa_family)
299 return -EAFNOSUPPORT;
300 }
301
302 if (bad_mask(mask, addr))
303 return -EINVAL;
304
305 plen = inet_mask_len(mask);
306 }
307
308 cfg->fc_dst_len = plen;
309 cfg->fc_dst = addr;
310
311 if (cmd != SIOCDELRT) {
312 cfg->fc_nlflags = NLM_F_CREATE;
313 cfg->fc_protocol = RTPROT_BOOT;
314 }
315
316 if (rt->rt_metric)
317 cfg->fc_priority = rt->rt_metric - 1;
318
319 if (rt->rt_flags & RTF_REJECT) {
320 cfg->fc_scope = RT_SCOPE_HOST;
321 cfg->fc_type = RTN_UNREACHABLE;
322 return 0;
323 }
324
325 cfg->fc_scope = RT_SCOPE_NOWHERE;
326 cfg->fc_type = RTN_UNICAST;
327
328 if (rt->rt_dev) {
329 char *colon;
330 struct net_device *dev;
331 char devname[IFNAMSIZ];
332
333 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
334 return -EFAULT;
335
336 devname[IFNAMSIZ-1] = 0;
337 colon = strchr(devname, ':');
338 if (colon)
339 *colon = 0;
340 dev = __dev_get_by_name(devname);
341 if (!dev)
342 return -ENODEV;
343 cfg->fc_oif = dev->ifindex;
344 if (colon) {
345 struct in_ifaddr *ifa;
346 struct in_device *in_dev = __in_dev_get_rtnl(dev);
347 if (!in_dev)
348 return -ENODEV;
349 *colon = ':';
350 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
351 if (strcmp(ifa->ifa_label, devname) == 0)
352 break;
353 if (ifa == NULL)
354 return -ENODEV;
355 cfg->fc_prefsrc = ifa->ifa_local;
356 }
357 }
358
359 addr = sk_extract_addr(&rt->rt_gateway);
360 if (rt->rt_gateway.sa_family == AF_INET && addr) {
361 cfg->fc_gw = addr;
362 if (rt->rt_flags & RTF_GATEWAY &&
363 inet_addr_type(addr) == RTN_UNICAST)
364 cfg->fc_scope = RT_SCOPE_UNIVERSE;
365 }
366
367 if (cmd == SIOCDELRT)
368 return 0;
369
370 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
371 return -EINVAL;
372
373 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
374 cfg->fc_scope = RT_SCOPE_LINK;
375
376 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
377 struct nlattr *mx;
378 int len = 0;
379
380 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
381 if (mx == NULL)
382 return -ENOMEM;
383
384 if (rt->rt_flags & RTF_MTU)
385 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
386
387 if (rt->rt_flags & RTF_WINDOW)
388 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
389
390 if (rt->rt_flags & RTF_IRTT)
391 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
392
393 cfg->fc_mx = mx;
394 cfg->fc_mx_len = len;
395 }
396
397 return 0;
398}
399
256/* 400/*
257 * Handle IP routing ioctl calls. These are used to manipulate the routing tables 401 * Handle IP routing ioctl calls. These are used to manipulate the routing tables
258 */ 402 */
259 403
260int ip_rt_ioctl(unsigned int cmd, void __user *arg) 404int ip_rt_ioctl(unsigned int cmd, void __user *arg)
261{ 405{
406 struct fib_config cfg;
407 struct rtentry rt;
262 int err; 408 int err;
263 struct kern_rta rta;
264 struct rtentry r;
265 struct {
266 struct nlmsghdr nlh;
267 struct rtmsg rtm;
268 } req;
269 409
270 switch (cmd) { 410 switch (cmd) {
271 case SIOCADDRT: /* Add a route */ 411 case SIOCADDRT: /* Add a route */
272 case SIOCDELRT: /* Delete a route */ 412 case SIOCDELRT: /* Delete a route */
273 if (!capable(CAP_NET_ADMIN)) 413 if (!capable(CAP_NET_ADMIN))
274 return -EPERM; 414 return -EPERM;
275 if (copy_from_user(&r, arg, sizeof(struct rtentry))) 415
416 if (copy_from_user(&rt, arg, sizeof(rt)))
276 return -EFAULT; 417 return -EFAULT;
418
277 rtnl_lock(); 419 rtnl_lock();
278 err = fib_convert_rtentry(cmd, &req.nlh, &req.rtm, &rta, &r); 420 err = rtentry_to_fib_config(cmd, &rt, &cfg);
279 if (err == 0) { 421 if (err == 0) {
422 struct fib_table *tb;
423
280 if (cmd == SIOCDELRT) { 424 if (cmd == SIOCDELRT) {
281 struct fib_table *tb = fib_get_table(req.rtm.rtm_table); 425 tb = fib_get_table(cfg.fc_table);
282 err = -ESRCH;
283 if (tb) 426 if (tb)
284 err = tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL); 427 err = tb->tb_delete(tb, &cfg);
428 else
429 err = -ESRCH;
285 } else { 430 } else {
286 struct fib_table *tb = fib_new_table(req.rtm.rtm_table); 431 tb = fib_new_table(cfg.fc_table);
287 err = -ENOBUFS;
288 if (tb) 432 if (tb)
289 err = tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL); 433 err = tb->tb_insert(tb, &cfg);
434 else
435 err = -ENOBUFS;
290 } 436 }
291 kfree(rta.rta_mx); 437
438 /* allocated by rtentry_to_fib_config() */
439 kfree(cfg.fc_mx);
292 } 440 }
293 rtnl_unlock(); 441 rtnl_unlock();
294 return err; 442 return err;
@@ -305,51 +453,134 @@ int ip_rt_ioctl(unsigned int cmd, void *arg)
305 453
306#endif 454#endif
307 455
308static int inet_check_attr(struct rtmsg *r, struct rtattr **rta) 456static struct nla_policy rtm_ipv4_policy[RTA_MAX+1] __read_mostly = {
457 [RTA_DST] = { .type = NLA_U32 },
458 [RTA_SRC] = { .type = NLA_U32 },
459 [RTA_IIF] = { .type = NLA_U32 },
460 [RTA_OIF] = { .type = NLA_U32 },
461 [RTA_GATEWAY] = { .type = NLA_U32 },
462 [RTA_PRIORITY] = { .type = NLA_U32 },
463 [RTA_PREFSRC] = { .type = NLA_U32 },
464 [RTA_METRICS] = { .type = NLA_NESTED },
465 [RTA_MULTIPATH] = { .minlen = sizeof(struct rtnexthop) },
466 [RTA_PROTOINFO] = { .type = NLA_U32 },
467 [RTA_FLOW] = { .type = NLA_U32 },
468 [RTA_MP_ALGO] = { .type = NLA_U32 },
469};
470
471static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
472 struct fib_config *cfg)
309{ 473{
310 int i; 474 struct nlattr *attr;
311 475 int err, remaining;
312 for (i=1; i<=RTA_MAX; i++, rta++) { 476 struct rtmsg *rtm;
313 struct rtattr *attr = *rta; 477
314 if (attr) { 478 err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
315 if (RTA_PAYLOAD(attr) < 4) 479 if (err < 0)
316 return -EINVAL; 480 goto errout;
317 if (i != RTA_MULTIPATH && i != RTA_METRICS && 481
318 i != RTA_TABLE) 482 memset(cfg, 0, sizeof(*cfg));
319 *rta = (struct rtattr*)RTA_DATA(attr); 483
484 rtm = nlmsg_data(nlh);
485 cfg->fc_family = rtm->rtm_family;
486 cfg->fc_dst_len = rtm->rtm_dst_len;
487 cfg->fc_src_len = rtm->rtm_src_len;
488 cfg->fc_tos = rtm->rtm_tos;
489 cfg->fc_table = rtm->rtm_table;
490 cfg->fc_protocol = rtm->rtm_protocol;
491 cfg->fc_scope = rtm->rtm_scope;
492 cfg->fc_type = rtm->rtm_type;
493 cfg->fc_flags = rtm->rtm_flags;
494 cfg->fc_nlflags = nlh->nlmsg_flags;
495
496 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
497 cfg->fc_nlinfo.nlh = nlh;
498
499 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
500 switch (attr->nla_type) {
501 case RTA_DST:
502 cfg->fc_dst = nla_get_u32(attr);
503 break;
504 case RTA_SRC:
505 cfg->fc_src = nla_get_u32(attr);
506 break;
507 case RTA_OIF:
508 cfg->fc_oif = nla_get_u32(attr);
509 break;
510 case RTA_GATEWAY:
511 cfg->fc_gw = nla_get_u32(attr);
512 break;
513 case RTA_PRIORITY:
514 cfg->fc_priority = nla_get_u32(attr);
515 break;
516 case RTA_PREFSRC:
517 cfg->fc_prefsrc = nla_get_u32(attr);
518 break;
519 case RTA_METRICS:
520 cfg->fc_mx = nla_data(attr);
521 cfg->fc_mx_len = nla_len(attr);
522 break;
523 case RTA_MULTIPATH:
524 cfg->fc_mp = nla_data(attr);
525 cfg->fc_mp_len = nla_len(attr);
526 break;
527 case RTA_FLOW:
528 cfg->fc_flow = nla_get_u32(attr);
529 break;
530 case RTA_MP_ALGO:
531 cfg->fc_mp_alg = nla_get_u32(attr);
532 break;
533 case RTA_TABLE:
534 cfg->fc_table = nla_get_u32(attr);
535 break;
320 } 536 }
321 } 537 }
538
322 return 0; 539 return 0;
540errout:
541 return err;
323} 542}
324 543
325int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 544int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
326{ 545{
327 struct fib_table * tb; 546 struct fib_config cfg;
328 struct rtattr **rta = arg; 547 struct fib_table *tb;
329 struct rtmsg *r = NLMSG_DATA(nlh); 548 int err;
330 549
331 if (inet_check_attr(r, rta)) 550 err = rtm_to_fib_config(skb, nlh, &cfg);
332 return -EINVAL; 551 if (err < 0)
552 goto errout;
333 553
334 tb = fib_get_table(rtm_get_table(rta, r->rtm_table)); 554 tb = fib_get_table(cfg.fc_table);
335 if (tb) 555 if (tb == NULL) {
336 return tb->tb_delete(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb)); 556 err = -ESRCH;
337 return -ESRCH; 557 goto errout;
558 }
559
560 err = tb->tb_delete(tb, &cfg);
561errout:
562 return err;
338} 563}
339 564
340int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 565int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
341{ 566{
342 struct fib_table * tb; 567 struct fib_config cfg;
343 struct rtattr **rta = arg; 568 struct fib_table *tb;
344 struct rtmsg *r = NLMSG_DATA(nlh); 569 int err;
345 570
346 if (inet_check_attr(r, rta)) 571 err = rtm_to_fib_config(skb, nlh, &cfg);
347 return -EINVAL; 572 if (err < 0)
573 goto errout;
348 574
349 tb = fib_new_table(rtm_get_table(rta, r->rtm_table)); 575 tb = fib_new_table(cfg.fc_table);
350 if (tb) 576 if (tb == NULL) {
351 return tb->tb_insert(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb)); 577 err = -ENOBUFS;
352 return -ENOBUFS; 578 goto errout;
579 }
580
581 err = tb->tb_insert(tb, &cfg);
582errout:
583 return err;
353} 584}
354 585
355int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) 586int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
@@ -396,17 +627,19 @@ out:
396 only when netlink is already locked. 627 only when netlink is already locked.
397 */ 628 */
398 629
399static void fib_magic(int cmd, int type, u32 dst, int dst_len, struct in_ifaddr *ifa) 630static void fib_magic(int cmd, int type, u32 dst, int dst_len,
631 struct in_ifaddr *ifa)
400{ 632{
401 struct fib_table * tb; 633 struct fib_table *tb;
402 struct { 634 struct fib_config cfg = {
403 struct nlmsghdr nlh; 635 .fc_protocol = RTPROT_KERNEL,
404 struct rtmsg rtm; 636 .fc_type = type,
405 } req; 637 .fc_dst = dst,
406 struct kern_rta rta; 638 .fc_dst_len = dst_len,
407 639 .fc_prefsrc = ifa->ifa_local,
408 memset(&req.rtm, 0, sizeof(req.rtm)); 640 .fc_oif = ifa->ifa_dev->dev->ifindex,
409 memset(&rta, 0, sizeof(rta)); 641 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
642 };
410 643
411 if (type == RTN_UNICAST) 644 if (type == RTN_UNICAST)
412 tb = fib_new_table(RT_TABLE_MAIN); 645 tb = fib_new_table(RT_TABLE_MAIN);
@@ -416,26 +649,17 @@ static void fib_magic(int cmd, int type, u32 dst, int dst_len, struct in_ifaddr
416 if (tb == NULL) 649 if (tb == NULL)
417 return; 650 return;
418 651
419 req.nlh.nlmsg_len = sizeof(req); 652 cfg.fc_table = tb->tb_id;
420 req.nlh.nlmsg_type = cmd;
421 req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
422 req.nlh.nlmsg_pid = 0;
423 req.nlh.nlmsg_seq = 0;
424 653
425 req.rtm.rtm_dst_len = dst_len; 654 if (type != RTN_LOCAL)
426 req.rtm.rtm_table = tb->tb_id; 655 cfg.fc_scope = RT_SCOPE_LINK;
427 req.rtm.rtm_protocol = RTPROT_KERNEL; 656 else
428 req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST); 657 cfg.fc_scope = RT_SCOPE_HOST;
429 req.rtm.rtm_type = type;
430
431 rta.rta_dst = &dst;
432 rta.rta_prefsrc = &ifa->ifa_local;
433 rta.rta_oif = &ifa->ifa_dev->dev->ifindex;
434 658
435 if (cmd == RTM_NEWROUTE) 659 if (cmd == RTM_NEWROUTE)
436 tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL); 660 tb->tb_insert(tb, &cfg);
437 else 661 else
438 tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL); 662 tb->tb_delete(tb, &cfg);
439} 663}
440 664
441void fib_add_ifaddr(struct in_ifaddr *ifa) 665void fib_add_ifaddr(struct in_ifaddr *ifa)
diff --git a/net/ipv4/fib_hash.c b/net/ipv4/fib_hash.c
index b5bee1a71e5c..357557549ce5 100644
--- a/net/ipv4/fib_hash.c
+++ b/net/ipv4/fib_hash.c
@@ -379,42 +379,39 @@ static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
379 return NULL; 379 return NULL;
380} 380}
381 381
382static int 382static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
383fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
384 struct nlmsghdr *n, struct netlink_skb_parms *req)
385{ 383{
386 struct fn_hash *table = (struct fn_hash *) tb->tb_data; 384 struct fn_hash *table = (struct fn_hash *) tb->tb_data;
387 struct fib_node *new_f, *f; 385 struct fib_node *new_f, *f;
388 struct fib_alias *fa, *new_fa; 386 struct fib_alias *fa, *new_fa;
389 struct fn_zone *fz; 387 struct fn_zone *fz;
390 struct fib_info *fi; 388 struct fib_info *fi;
391 int z = r->rtm_dst_len; 389 u8 tos = cfg->fc_tos;
392 int type = r->rtm_type;
393 u8 tos = r->rtm_tos;
394 u32 key; 390 u32 key;
395 int err; 391 int err;
396 392
397 if (z > 32) 393 if (cfg->fc_dst_len > 32)
398 return -EINVAL; 394 return -EINVAL;
399 fz = table->fn_zones[z]; 395
400 if (!fz && !(fz = fn_new_zone(table, z))) 396 fz = table->fn_zones[cfg->fc_dst_len];
397 if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
401 return -ENOBUFS; 398 return -ENOBUFS;
402 399
403 key = 0; 400 key = 0;
404 if (rta->rta_dst) { 401 if (cfg->fc_dst) {
405 u32 dst; 402 if (cfg->fc_dst & ~FZ_MASK(fz))
406 memcpy(&dst, rta->rta_dst, 4);
407 if (dst & ~FZ_MASK(fz))
408 return -EINVAL; 403 return -EINVAL;
409 key = fz_key(dst, fz); 404 key = fz_key(cfg->fc_dst, fz);
410 } 405 }
411 406
412 if ((fi = fib_create_info(r, rta, n, &err)) == NULL) 407 fi = fib_create_info(cfg);
413 return err; 408 if (IS_ERR(fi))
409 return PTR_ERR(fi);
414 410
415 if (fz->fz_nent > (fz->fz_divisor<<1) && 411 if (fz->fz_nent > (fz->fz_divisor<<1) &&
416 fz->fz_divisor < FZ_MAX_DIVISOR && 412 fz->fz_divisor < FZ_MAX_DIVISOR &&
417 (z==32 || (1<<z) > fz->fz_divisor)) 413 (cfg->fc_dst_len == 32 ||
414 (1 << cfg->fc_dst_len) > fz->fz_divisor))
418 fn_rehash_zone(fz); 415 fn_rehash_zone(fz);
419 416
420 f = fib_find_node(fz, key); 417 f = fib_find_node(fz, key);
@@ -440,18 +437,18 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
440 struct fib_alias *fa_orig; 437 struct fib_alias *fa_orig;
441 438
442 err = -EEXIST; 439 err = -EEXIST;
443 if (n->nlmsg_flags & NLM_F_EXCL) 440 if (cfg->fc_nlflags & NLM_F_EXCL)
444 goto out; 441 goto out;
445 442
446 if (n->nlmsg_flags & NLM_F_REPLACE) { 443 if (cfg->fc_nlflags & NLM_F_REPLACE) {
447 struct fib_info *fi_drop; 444 struct fib_info *fi_drop;
448 u8 state; 445 u8 state;
449 446
450 write_lock_bh(&fib_hash_lock); 447 write_lock_bh(&fib_hash_lock);
451 fi_drop = fa->fa_info; 448 fi_drop = fa->fa_info;
452 fa->fa_info = fi; 449 fa->fa_info = fi;
453 fa->fa_type = type; 450 fa->fa_type = cfg->fc_type;
454 fa->fa_scope = r->rtm_scope; 451 fa->fa_scope = cfg->fc_scope;
455 state = fa->fa_state; 452 state = fa->fa_state;
456 fa->fa_state &= ~FA_S_ACCESSED; 453 fa->fa_state &= ~FA_S_ACCESSED;
457 fib_hash_genid++; 454 fib_hash_genid++;
@@ -474,17 +471,17 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
474 break; 471 break;
475 if (fa->fa_info->fib_priority != fi->fib_priority) 472 if (fa->fa_info->fib_priority != fi->fib_priority)
476 break; 473 break;
477 if (fa->fa_type == type && 474 if (fa->fa_type == cfg->fc_type &&
478 fa->fa_scope == r->rtm_scope && 475 fa->fa_scope == cfg->fc_scope &&
479 fa->fa_info == fi) 476 fa->fa_info == fi)
480 goto out; 477 goto out;
481 } 478 }
482 if (!(n->nlmsg_flags & NLM_F_APPEND)) 479 if (!(cfg->fc_nlflags & NLM_F_APPEND))
483 fa = fa_orig; 480 fa = fa_orig;
484 } 481 }
485 482
486 err = -ENOENT; 483 err = -ENOENT;
487 if (!(n->nlmsg_flags&NLM_F_CREATE)) 484 if (!(cfg->fc_nlflags & NLM_F_CREATE))
488 goto out; 485 goto out;
489 486
490 err = -ENOBUFS; 487 err = -ENOBUFS;
@@ -506,8 +503,8 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
506 503
507 new_fa->fa_info = fi; 504 new_fa->fa_info = fi;
508 new_fa->fa_tos = tos; 505 new_fa->fa_tos = tos;
509 new_fa->fa_type = type; 506 new_fa->fa_type = cfg->fc_type;
510 new_fa->fa_scope = r->rtm_scope; 507 new_fa->fa_scope = cfg->fc_scope;
511 new_fa->fa_state = 0; 508 new_fa->fa_state = 0;
512 509
513 /* 510 /*
@@ -526,7 +523,8 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
526 fz->fz_nent++; 523 fz->fz_nent++;
527 rt_cache_flush(-1); 524 rt_cache_flush(-1);
528 525
529 rtmsg_fib(RTM_NEWROUTE, key, new_fa, z, tb->tb_id, n, req); 526 rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
527 &cfg->fc_nlinfo);
530 return 0; 528 return 0;
531 529
532out_free_new_fa: 530out_free_new_fa:
@@ -537,30 +535,25 @@ out:
537} 535}
538 536
539 537
540static int 538static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
541fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
542 struct nlmsghdr *n, struct netlink_skb_parms *req)
543{ 539{
544 struct fn_hash *table = (struct fn_hash*)tb->tb_data; 540 struct fn_hash *table = (struct fn_hash*)tb->tb_data;
545 struct fib_node *f; 541 struct fib_node *f;
546 struct fib_alias *fa, *fa_to_delete; 542 struct fib_alias *fa, *fa_to_delete;
547 int z = r->rtm_dst_len;
548 struct fn_zone *fz; 543 struct fn_zone *fz;
549 u32 key; 544 u32 key;
550 u8 tos = r->rtm_tos;
551 545
552 if (z > 32) 546 if (cfg->fc_dst_len > 32)
553 return -EINVAL; 547 return -EINVAL;
554 if ((fz = table->fn_zones[z]) == NULL) 548
549 if ((fz = table->fn_zones[cfg->fc_dst_len]) == NULL)
555 return -ESRCH; 550 return -ESRCH;
556 551
557 key = 0; 552 key = 0;
558 if (rta->rta_dst) { 553 if (cfg->fc_dst) {
559 u32 dst; 554 if (cfg->fc_dst & ~FZ_MASK(fz))
560 memcpy(&dst, rta->rta_dst, 4);
561 if (dst & ~FZ_MASK(fz))
562 return -EINVAL; 555 return -EINVAL;
563 key = fz_key(dst, fz); 556 key = fz_key(cfg->fc_dst, fz);
564 } 557 }
565 558
566 f = fib_find_node(fz, key); 559 f = fib_find_node(fz, key);
@@ -568,7 +561,7 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
568 if (!f) 561 if (!f)
569 fa = NULL; 562 fa = NULL;
570 else 563 else
571 fa = fib_find_alias(&f->fn_alias, tos, 0); 564 fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
572 if (!fa) 565 if (!fa)
573 return -ESRCH; 566 return -ESRCH;
574 567
@@ -577,16 +570,16 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
577 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) { 570 list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
578 struct fib_info *fi = fa->fa_info; 571 struct fib_info *fi = fa->fa_info;
579 572
580 if (fa->fa_tos != tos) 573 if (fa->fa_tos != cfg->fc_tos)
581 break; 574 break;
582 575
583 if ((!r->rtm_type || 576 if ((!cfg->fc_type ||
584 fa->fa_type == r->rtm_type) && 577 fa->fa_type == cfg->fc_type) &&
585 (r->rtm_scope == RT_SCOPE_NOWHERE || 578 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
586 fa->fa_scope == r->rtm_scope) && 579 fa->fa_scope == cfg->fc_scope) &&
587 (!r->rtm_protocol || 580 (!cfg->fc_protocol ||
588 fi->fib_protocol == r->rtm_protocol) && 581 fi->fib_protocol == cfg->fc_protocol) &&
589 fib_nh_match(r, n, rta, fi) == 0) { 582 fib_nh_match(cfg, fi) == 0) {
590 fa_to_delete = fa; 583 fa_to_delete = fa;
591 break; 584 break;
592 } 585 }
@@ -596,7 +589,8 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
596 int kill_fn; 589 int kill_fn;
597 590
598 fa = fa_to_delete; 591 fa = fa_to_delete;
599 rtmsg_fib(RTM_DELROUTE, key, fa, z, tb->tb_id, n, req); 592 rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
593 tb->tb_id, &cfg->fc_nlinfo);
600 594
601 kill_fn = 0; 595 kill_fn = 0;
602 write_lock_bh(&fib_hash_lock); 596 write_lock_bh(&fib_hash_lock);
diff --git a/net/ipv4/fib_lookup.h b/net/ipv4/fib_lookup.h
index ddd52496b451..d6d1a89e4003 100644
--- a/net/ipv4/fib_lookup.h
+++ b/net/ipv4/fib_lookup.h
@@ -23,19 +23,14 @@ extern int fib_semantic_match(struct list_head *head,
23 struct fib_result *res, __u32 zone, __u32 mask, 23 struct fib_result *res, __u32 zone, __u32 mask,
24 int prefixlen); 24 int prefixlen);
25extern void fib_release_info(struct fib_info *); 25extern void fib_release_info(struct fib_info *);
26extern struct fib_info *fib_create_info(const struct rtmsg *r, 26extern struct fib_info *fib_create_info(struct fib_config *cfg);
27 struct kern_rta *rta, 27extern int fib_nh_match(struct fib_config *cfg, struct fib_info *fi);
28 const struct nlmsghdr *,
29 int *err);
30extern int fib_nh_match(struct rtmsg *r, struct nlmsghdr *,
31 struct kern_rta *rta, struct fib_info *fi);
32extern int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, 28extern int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
33 u32 tb_id, u8 type, u8 scope, void *dst, 29 u32 tb_id, u8 type, u8 scope, void *dst,
34 int dst_len, u8 tos, struct fib_info *fi, 30 int dst_len, u8 tos, struct fib_info *fi,
35 unsigned int); 31 unsigned int);
36extern void rtmsg_fib(int event, u32 key, struct fib_alias *fa, 32extern void rtmsg_fib(int event, u32 key, struct fib_alias *fa,
37 int z, u32 tb_id, 33 int dst_len, u32 tb_id, struct nl_info *info);
38 struct nlmsghdr *n, struct netlink_skb_parms *req);
39extern struct fib_alias *fib_find_alias(struct list_head *fah, 34extern struct fib_alias *fib_find_alias(struct list_head *fah,
40 u8 tos, u32 prio); 35 u8 tos, u32 prio);
41extern int fib_detect_death(struct fib_info *fi, int order, 36extern int fib_detect_death(struct fib_info *fi, int order,
diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index 5dfdad5cbcd4..340f9db389e5 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -44,6 +44,7 @@
44#include <net/ip_fib.h> 44#include <net/ip_fib.h>
45#include <net/ip_mp_alg.h> 45#include <net/ip_mp_alg.h>
46#include <net/netlink.h> 46#include <net/netlink.h>
47#include <net/nexthop.h>
47 48
48#include "fib_lookup.h" 49#include "fib_lookup.h"
49 50
@@ -273,27 +274,27 @@ int ip_fib_check_default(u32 gw, struct net_device *dev)
273} 274}
274 275
275void rtmsg_fib(int event, u32 key, struct fib_alias *fa, 276void rtmsg_fib(int event, u32 key, struct fib_alias *fa,
276 int z, u32 tb_id, 277 int dst_len, u32 tb_id, struct nl_info *info)
277 struct nlmsghdr *n, struct netlink_skb_parms *req)
278{ 278{
279 struct sk_buff *skb; 279 struct sk_buff *skb;
280 u32 pid = req ? req->pid : n->nlmsg_pid;
281 int payload = sizeof(struct rtmsg) + 256; 280 int payload = sizeof(struct rtmsg) + 256;
281 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
282 int err = -ENOBUFS; 282 int err = -ENOBUFS;
283 283
284 skb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL); 284 skb = nlmsg_new(nlmsg_total_size(payload), GFP_KERNEL);
285 if (skb == NULL) 285 if (skb == NULL)
286 goto errout; 286 goto errout;
287 287
288 err = fib_dump_info(skb, pid, n->nlmsg_seq, event, tb_id, 288 err = fib_dump_info(skb, info->pid, seq, event, tb_id,
289 fa->fa_type, fa->fa_scope, &key, z, fa->fa_tos, 289 fa->fa_type, fa->fa_scope, &key, dst_len,
290 fa->fa_info, 0); 290 fa->fa_tos, fa->fa_info, 0);
291 if (err < 0) { 291 if (err < 0) {
292 kfree_skb(skb); 292 kfree_skb(skb);
293 goto errout; 293 goto errout;
294 } 294 }
295 295
296 err = rtnl_notify(skb, pid, RTNLGRP_IPV4_ROUTE, n, GFP_KERNEL); 296 err = rtnl_notify(skb, info->pid, RTNLGRP_IPV4_ROUTE,
297 info->nlh, GFP_KERNEL);
297errout: 298errout:
298 if (err < 0) 299 if (err < 0)
299 rtnl_set_sk_err(RTNLGRP_IPV4_ROUTE, err); 300 rtnl_set_sk_err(RTNLGRP_IPV4_ROUTE, err);
@@ -342,102 +343,100 @@ int fib_detect_death(struct fib_info *fi, int order,
342 343
343#ifdef CONFIG_IP_ROUTE_MULTIPATH 344#ifdef CONFIG_IP_ROUTE_MULTIPATH
344 345
345static u32 fib_get_attr32(struct rtattr *attr, int attrlen, int type) 346static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
346{
347 while (RTA_OK(attr,attrlen)) {
348 if (attr->rta_type == type)
349 return *(u32*)RTA_DATA(attr);
350 attr = RTA_NEXT(attr, attrlen);
351 }
352 return 0;
353}
354
355static int
356fib_count_nexthops(struct rtattr *rta)
357{ 347{
358 int nhs = 0; 348 int nhs = 0;
359 struct rtnexthop *nhp = RTA_DATA(rta);
360 int nhlen = RTA_PAYLOAD(rta);
361 349
362 while (nhlen >= (int)sizeof(struct rtnexthop)) { 350 while (rtnh_ok(rtnh, remaining)) {
363 if ((nhlen -= nhp->rtnh_len) < 0)
364 return 0;
365 nhs++; 351 nhs++;
366 nhp = RTNH_NEXT(nhp); 352 rtnh = rtnh_next(rtnh, &remaining);
367 }; 353 }
368 return nhs; 354
355 /* leftover implies invalid nexthop configuration, discard it */
356 return remaining > 0 ? 0 : nhs;
369} 357}
370 358
371static int 359static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
372fib_get_nhs(struct fib_info *fi, const struct rtattr *rta, const struct rtmsg *r) 360 int remaining, struct fib_config *cfg)
373{ 361{
374 struct rtnexthop *nhp = RTA_DATA(rta);
375 int nhlen = RTA_PAYLOAD(rta);
376
377 change_nexthops(fi) { 362 change_nexthops(fi) {
378 int attrlen = nhlen - sizeof(struct rtnexthop); 363 int attrlen;
379 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0) 364
365 if (!rtnh_ok(rtnh, remaining))
380 return -EINVAL; 366 return -EINVAL;
381 nh->nh_flags = (r->rtm_flags&~0xFF) | nhp->rtnh_flags; 367
382 nh->nh_oif = nhp->rtnh_ifindex; 368 nh->nh_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
383 nh->nh_weight = nhp->rtnh_hops + 1; 369 nh->nh_oif = rtnh->rtnh_ifindex;
384 if (attrlen) { 370 nh->nh_weight = rtnh->rtnh_hops + 1;
385 nh->nh_gw = fib_get_attr32(RTNH_DATA(nhp), attrlen, RTA_GATEWAY); 371
372 attrlen = rtnh_attrlen(rtnh);
373 if (attrlen > 0) {
374 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
375
376 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
377 nh->nh_gw = nla ? nla_get_u32(nla) : 0;
386#ifdef CONFIG_NET_CLS_ROUTE 378#ifdef CONFIG_NET_CLS_ROUTE
387 nh->nh_tclassid = fib_get_attr32(RTNH_DATA(nhp), attrlen, RTA_FLOW); 379 nla = nla_find(attrs, attrlen, RTA_FLOW);
380 nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
388#endif 381#endif
389 } 382 }
390 nhp = RTNH_NEXT(nhp); 383
384 rtnh = rtnh_next(rtnh, &remaining);
391 } endfor_nexthops(fi); 385 } endfor_nexthops(fi);
386
392 return 0; 387 return 0;
393} 388}
394 389
395#endif 390#endif
396 391
397int fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct kern_rta *rta, 392int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
398 struct fib_info *fi)
399{ 393{
400#ifdef CONFIG_IP_ROUTE_MULTIPATH 394#ifdef CONFIG_IP_ROUTE_MULTIPATH
401 struct rtnexthop *nhp; 395 struct rtnexthop *rtnh;
402 int nhlen; 396 int remaining;
403#endif 397#endif
404 398
405 if (rta->rta_priority && 399 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
406 *rta->rta_priority != fi->fib_priority)
407 return 1; 400 return 1;
408 401
409 if (rta->rta_oif || rta->rta_gw) { 402 if (cfg->fc_oif || cfg->fc_gw) {
410 if ((!rta->rta_oif || *rta->rta_oif == fi->fib_nh->nh_oif) && 403 if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
411 (!rta->rta_gw || memcmp(rta->rta_gw, &fi->fib_nh->nh_gw, 4) == 0)) 404 (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
412 return 0; 405 return 0;
413 return 1; 406 return 1;
414 } 407 }
415 408
416#ifdef CONFIG_IP_ROUTE_MULTIPATH 409#ifdef CONFIG_IP_ROUTE_MULTIPATH
417 if (rta->rta_mp == NULL) 410 if (cfg->fc_mp == NULL)
418 return 0; 411 return 0;
419 nhp = RTA_DATA(rta->rta_mp); 412
420 nhlen = RTA_PAYLOAD(rta->rta_mp); 413 rtnh = cfg->fc_mp;
414 remaining = cfg->fc_mp_len;
421 415
422 for_nexthops(fi) { 416 for_nexthops(fi) {
423 int attrlen = nhlen - sizeof(struct rtnexthop); 417 int attrlen;
424 u32 gw;
425 418
426 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0) 419 if (!rtnh_ok(rtnh, remaining))
427 return -EINVAL; 420 return -EINVAL;
428 if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif) 421
422 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
429 return 1; 423 return 1;
430 if (attrlen) { 424
431 gw = fib_get_attr32(RTNH_DATA(nhp), attrlen, RTA_GATEWAY); 425 attrlen = rtnh_attrlen(rtnh);
432 if (gw && gw != nh->nh_gw) 426 if (attrlen < 0) {
427 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
428
429 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
430 if (nla && nla_get_u32(nla) != nh->nh_gw)
433 return 1; 431 return 1;
434#ifdef CONFIG_NET_CLS_ROUTE 432#ifdef CONFIG_NET_CLS_ROUTE
435 gw = fib_get_attr32(RTNH_DATA(nhp), attrlen, RTA_FLOW); 433 nla = nla_find(attrs, attrlen, RTA_FLOW);
436 if (gw && gw != nh->nh_tclassid) 434 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
437 return 1; 435 return 1;
438#endif 436#endif
439 } 437 }
440 nhp = RTNH_NEXT(nhp); 438
439 rtnh = rtnh_next(rtnh, &remaining);
441 } endfor_nexthops(fi); 440 } endfor_nexthops(fi);
442#endif 441#endif
443 return 0; 442 return 0;
@@ -488,7 +487,8 @@ int fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct kern_rta *rta,
488 |-> {local prefix} (terminal node) 487 |-> {local prefix} (terminal node)
489 */ 488 */
490 489
491static int fib_check_nh(const struct rtmsg *r, struct fib_info *fi, struct fib_nh *nh) 490static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
491 struct fib_nh *nh)
492{ 492{
493 int err; 493 int err;
494 494
@@ -502,7 +502,7 @@ static int fib_check_nh(const struct rtmsg *r, struct fib_info *fi, struct fib_n
502 if (nh->nh_flags&RTNH_F_ONLINK) { 502 if (nh->nh_flags&RTNH_F_ONLINK) {
503 struct net_device *dev; 503 struct net_device *dev;
504 504
505 if (r->rtm_scope >= RT_SCOPE_LINK) 505 if (cfg->fc_scope >= RT_SCOPE_LINK)
506 return -EINVAL; 506 return -EINVAL;
507 if (inet_addr_type(nh->nh_gw) != RTN_UNICAST) 507 if (inet_addr_type(nh->nh_gw) != RTN_UNICAST)
508 return -EINVAL; 508 return -EINVAL;
@@ -516,10 +516,15 @@ static int fib_check_nh(const struct rtmsg *r, struct fib_info *fi, struct fib_n
516 return 0; 516 return 0;
517 } 517 }
518 { 518 {
519 struct flowi fl = { .nl_u = { .ip4_u = 519 struct flowi fl = {
520 { .daddr = nh->nh_gw, 520 .nl_u = {
521 .scope = r->rtm_scope + 1 } }, 521 .ip4_u = {
522 .oif = nh->nh_oif }; 522 .daddr = nh->nh_gw,
523 .scope = cfg->fc_scope + 1,
524 },
525 },
526 .oif = nh->nh_oif,
527 };
523 528
524 /* It is not necessary, but requires a bit of thinking */ 529 /* It is not necessary, but requires a bit of thinking */
525 if (fl.fl4_scope < RT_SCOPE_LINK) 530 if (fl.fl4_scope < RT_SCOPE_LINK)
@@ -646,39 +651,28 @@ static void fib_hash_move(struct hlist_head *new_info_hash,
646 fib_hash_free(old_laddrhash, bytes); 651 fib_hash_free(old_laddrhash, bytes);
647} 652}
648 653
649struct fib_info * 654struct fib_info *fib_create_info(struct fib_config *cfg)
650fib_create_info(const struct rtmsg *r, struct kern_rta *rta,
651 const struct nlmsghdr *nlh, int *errp)
652{ 655{
653 int err; 656 int err;
654 struct fib_info *fi = NULL; 657 struct fib_info *fi = NULL;
655 struct fib_info *ofi; 658 struct fib_info *ofi;
656#ifdef CONFIG_IP_ROUTE_MULTIPATH
657 int nhs = 1; 659 int nhs = 1;
658#else
659 const int nhs = 1;
660#endif
661#ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
662 u32 mp_alg = IP_MP_ALG_NONE;
663#endif
664 660
665 /* Fast check to catch the most weird cases */ 661 /* Fast check to catch the most weird cases */
666 if (fib_props[r->rtm_type].scope > r->rtm_scope) 662 if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
667 goto err_inval; 663 goto err_inval;
668 664
669#ifdef CONFIG_IP_ROUTE_MULTIPATH 665#ifdef CONFIG_IP_ROUTE_MULTIPATH
670 if (rta->rta_mp) { 666 if (cfg->fc_mp) {
671 nhs = fib_count_nexthops(rta->rta_mp); 667 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
672 if (nhs == 0) 668 if (nhs == 0)
673 goto err_inval; 669 goto err_inval;
674 } 670 }
675#endif 671#endif
676#ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED 672#ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
677 if (rta->rta_mp_alg) { 673 if (cfg->fc_mp_alg) {
678 mp_alg = *rta->rta_mp_alg; 674 if (cfg->fc_mp_alg < IP_MP_ALG_NONE ||
679 675 cfg->fc_mp_alg > IP_MP_ALG_MAX)
680 if (mp_alg < IP_MP_ALG_NONE ||
681 mp_alg > IP_MP_ALG_MAX)
682 goto err_inval; 676 goto err_inval;
683 } 677 }
684#endif 678#endif
@@ -714,43 +708,42 @@ fib_create_info(const struct rtmsg *r, struct kern_rta *rta,
714 goto failure; 708 goto failure;
715 fib_info_cnt++; 709 fib_info_cnt++;
716 710
717 fi->fib_protocol = r->rtm_protocol; 711 fi->fib_protocol = cfg->fc_protocol;
712 fi->fib_flags = cfg->fc_flags;
713 fi->fib_priority = cfg->fc_priority;
714 fi->fib_prefsrc = cfg->fc_prefsrc;
718 715
719 fi->fib_nhs = nhs; 716 fi->fib_nhs = nhs;
720 change_nexthops(fi) { 717 change_nexthops(fi) {
721 nh->nh_parent = fi; 718 nh->nh_parent = fi;
722 } endfor_nexthops(fi) 719 } endfor_nexthops(fi)
723 720
724 fi->fib_flags = r->rtm_flags; 721 if (cfg->fc_mx) {
725 if (rta->rta_priority) 722 struct nlattr *nla;
726 fi->fib_priority = *rta->rta_priority; 723 int remaining;
727 if (rta->rta_mx) { 724
728 int attrlen = RTA_PAYLOAD(rta->rta_mx); 725 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
729 struct rtattr *attr = RTA_DATA(rta->rta_mx); 726 int type = nla->nla_type;
730 727
731 while (RTA_OK(attr, attrlen)) { 728 if (type) {
732 unsigned flavor = attr->rta_type; 729 if (type > RTAX_MAX)
733 if (flavor) {
734 if (flavor > RTAX_MAX)
735 goto err_inval; 730 goto err_inval;
736 fi->fib_metrics[flavor-1] = *(unsigned*)RTA_DATA(attr); 731 fi->fib_metrics[type - 1] = nla_get_u32(nla);
737 } 732 }
738 attr = RTA_NEXT(attr, attrlen);
739 } 733 }
740 } 734 }
741 if (rta->rta_prefsrc)
742 memcpy(&fi->fib_prefsrc, rta->rta_prefsrc, 4);
743 735
744 if (rta->rta_mp) { 736 if (cfg->fc_mp) {
745#ifdef CONFIG_IP_ROUTE_MULTIPATH 737#ifdef CONFIG_IP_ROUTE_MULTIPATH
746 if ((err = fib_get_nhs(fi, rta->rta_mp, r)) != 0) 738 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
739 if (err != 0)
747 goto failure; 740 goto failure;
748 if (rta->rta_oif && fi->fib_nh->nh_oif != *rta->rta_oif) 741 if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
749 goto err_inval; 742 goto err_inval;
750 if (rta->rta_gw && memcmp(&fi->fib_nh->nh_gw, rta->rta_gw, 4)) 743 if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
751 goto err_inval; 744 goto err_inval;
752#ifdef CONFIG_NET_CLS_ROUTE 745#ifdef CONFIG_NET_CLS_ROUTE
753 if (rta->rta_flow && memcmp(&fi->fib_nh->nh_tclassid, rta->rta_flow, 4)) 746 if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
754 goto err_inval; 747 goto err_inval;
755#endif 748#endif
756#else 749#else
@@ -758,34 +751,32 @@ fib_create_info(const struct rtmsg *r, struct kern_rta *rta,
758#endif 751#endif
759 } else { 752 } else {
760 struct fib_nh *nh = fi->fib_nh; 753 struct fib_nh *nh = fi->fib_nh;
761 if (rta->rta_oif) 754
762 nh->nh_oif = *rta->rta_oif; 755 nh->nh_oif = cfg->fc_oif;
763 if (rta->rta_gw) 756 nh->nh_gw = cfg->fc_gw;
764 memcpy(&nh->nh_gw, rta->rta_gw, 4); 757 nh->nh_flags = cfg->fc_flags;
765#ifdef CONFIG_NET_CLS_ROUTE 758#ifdef CONFIG_NET_CLS_ROUTE
766 if (rta->rta_flow) 759 nh->nh_tclassid = cfg->fc_flow;
767 memcpy(&nh->nh_tclassid, rta->rta_flow, 4);
768#endif 760#endif
769 nh->nh_flags = r->rtm_flags;
770#ifdef CONFIG_IP_ROUTE_MULTIPATH 761#ifdef CONFIG_IP_ROUTE_MULTIPATH
771 nh->nh_weight = 1; 762 nh->nh_weight = 1;
772#endif 763#endif
773 } 764 }
774 765
775#ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED 766#ifdef CONFIG_IP_ROUTE_MULTIPATH_CACHED
776 fi->fib_mp_alg = mp_alg; 767 fi->fib_mp_alg = cfg->fc_mp_alg;
777#endif 768#endif
778 769
779 if (fib_props[r->rtm_type].error) { 770 if (fib_props[cfg->fc_type].error) {
780 if (rta->rta_gw || rta->rta_oif || rta->rta_mp) 771 if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
781 goto err_inval; 772 goto err_inval;
782 goto link_it; 773 goto link_it;
783 } 774 }
784 775
785 if (r->rtm_scope > RT_SCOPE_HOST) 776 if (cfg->fc_scope > RT_SCOPE_HOST)
786 goto err_inval; 777 goto err_inval;
787 778
788 if (r->rtm_scope == RT_SCOPE_HOST) { 779 if (cfg->fc_scope == RT_SCOPE_HOST) {
789 struct fib_nh *nh = fi->fib_nh; 780 struct fib_nh *nh = fi->fib_nh;
790 781
791 /* Local address is added. */ 782 /* Local address is added. */
@@ -798,14 +789,14 @@ fib_create_info(const struct rtmsg *r, struct kern_rta *rta,
798 goto failure; 789 goto failure;
799 } else { 790 } else {
800 change_nexthops(fi) { 791 change_nexthops(fi) {
801 if ((err = fib_check_nh(r, fi, nh)) != 0) 792 if ((err = fib_check_nh(cfg, fi, nh)) != 0)
802 goto failure; 793 goto failure;
803 } endfor_nexthops(fi) 794 } endfor_nexthops(fi)
804 } 795 }
805 796
806 if (fi->fib_prefsrc) { 797 if (fi->fib_prefsrc) {
807 if (r->rtm_type != RTN_LOCAL || rta->rta_dst == NULL || 798 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
808 memcmp(&fi->fib_prefsrc, rta->rta_dst, 4)) 799 fi->fib_prefsrc != cfg->fc_dst)
809 if (inet_addr_type(fi->fib_prefsrc) != RTN_LOCAL) 800 if (inet_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
810 goto err_inval; 801 goto err_inval;
811 } 802 }
@@ -846,12 +837,12 @@ err_inval:
846 err = -EINVAL; 837 err = -EINVAL;
847 838
848failure: 839failure:
849 *errp = err;
850 if (fi) { 840 if (fi) {
851 fi->fib_dead = 1; 841 fi->fib_dead = 1;
852 free_fib_info(fi); 842 free_fib_info(fi);
853 } 843 }
854 return NULL; 844
845 return ERR_PTR(err);
855} 846}
856 847
857/* Note! fib_semantic_match intentionally uses RCU list functions. */ 848/* Note! fib_semantic_match intentionally uses RCU list functions. */
@@ -1012,150 +1003,6 @@ rtattr_failure:
1012 return -1; 1003 return -1;
1013} 1004}
1014 1005
1015#ifndef CONFIG_IP_NOSIOCRT
1016
1017int
1018fib_convert_rtentry(int cmd, struct nlmsghdr *nl, struct rtmsg *rtm,
1019 struct kern_rta *rta, struct rtentry *r)
1020{
1021 int plen;
1022 u32 *ptr;
1023
1024 memset(rtm, 0, sizeof(*rtm));
1025 memset(rta, 0, sizeof(*rta));
1026
1027 if (r->rt_dst.sa_family != AF_INET)
1028 return -EAFNOSUPPORT;
1029
1030 /* Check mask for validity:
1031 a) it must be contiguous.
1032 b) destination must have all host bits clear.
1033 c) if application forgot to set correct family (AF_INET),
1034 reject request unless it is absolutely clear i.e.
1035 both family and mask are zero.
1036 */
1037 plen = 32;
1038 ptr = &((struct sockaddr_in*)&r->rt_dst)->sin_addr.s_addr;
1039 if (!(r->rt_flags&RTF_HOST)) {
1040 u32 mask = ((struct sockaddr_in*)&r->rt_genmask)->sin_addr.s_addr;
1041 if (r->rt_genmask.sa_family != AF_INET) {
1042 if (mask || r->rt_genmask.sa_family)
1043 return -EAFNOSUPPORT;
1044 }
1045 if (bad_mask(mask, *ptr))
1046 return -EINVAL;
1047 plen = inet_mask_len(mask);
1048 }
1049
1050 nl->nlmsg_flags = NLM_F_REQUEST;
1051 nl->nlmsg_pid = 0;
1052 nl->nlmsg_seq = 0;
1053 nl->nlmsg_len = NLMSG_LENGTH(sizeof(*rtm));
1054 if (cmd == SIOCDELRT) {
1055 nl->nlmsg_type = RTM_DELROUTE;
1056 nl->nlmsg_flags = 0;
1057 } else {
1058 nl->nlmsg_type = RTM_NEWROUTE;
1059 nl->nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE;
1060 rtm->rtm_protocol = RTPROT_BOOT;
1061 }
1062
1063 rtm->rtm_dst_len = plen;
1064 rta->rta_dst = ptr;
1065
1066 if (r->rt_metric) {
1067 *(u32*)&r->rt_pad3 = r->rt_metric - 1;
1068 rta->rta_priority = (u32*)&r->rt_pad3;
1069 }
1070 if (r->rt_flags&RTF_REJECT) {
1071 rtm->rtm_scope = RT_SCOPE_HOST;
1072 rtm->rtm_type = RTN_UNREACHABLE;
1073 return 0;
1074 }
1075 rtm->rtm_scope = RT_SCOPE_NOWHERE;
1076 rtm->rtm_type = RTN_UNICAST;
1077
1078 if (r->rt_dev) {
1079 char *colon;
1080 struct net_device *dev;
1081 char devname[IFNAMSIZ];
1082
1083 if (copy_from_user(devname, r->rt_dev, IFNAMSIZ-1))
1084 return -EFAULT;
1085 devname[IFNAMSIZ-1] = 0;
1086 colon = strchr(devname, ':');
1087 if (colon)
1088 *colon = 0;
1089 dev = __dev_get_by_name(devname);
1090 if (!dev)
1091 return -ENODEV;
1092 rta->rta_oif = &dev->ifindex;
1093 if (colon) {
1094 struct in_ifaddr *ifa;
1095 struct in_device *in_dev = __in_dev_get_rtnl(dev);
1096 if (!in_dev)
1097 return -ENODEV;
1098 *colon = ':';
1099 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
1100 if (strcmp(ifa->ifa_label, devname) == 0)
1101 break;
1102 if (ifa == NULL)
1103 return -ENODEV;
1104 rta->rta_prefsrc = &ifa->ifa_local;
1105 }
1106 }
1107
1108 ptr = &((struct sockaddr_in*)&r->rt_gateway)->sin_addr.s_addr;
1109 if (r->rt_gateway.sa_family == AF_INET && *ptr) {
1110 rta->rta_gw = ptr;
1111 if (r->rt_flags&RTF_GATEWAY && inet_addr_type(*ptr) == RTN_UNICAST)
1112 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
1113 }
1114
1115 if (cmd == SIOCDELRT)
1116 return 0;
1117
1118 if (r->rt_flags&RTF_GATEWAY && rta->rta_gw == NULL)
1119 return -EINVAL;
1120
1121 if (rtm->rtm_scope == RT_SCOPE_NOWHERE)
1122 rtm->rtm_scope = RT_SCOPE_LINK;
1123
1124 if (r->rt_flags&(RTF_MTU|RTF_WINDOW|RTF_IRTT)) {
1125 struct rtattr *rec;
1126 struct rtattr *mx = kmalloc(RTA_LENGTH(3*RTA_LENGTH(4)), GFP_KERNEL);
1127 if (mx == NULL)
1128 return -ENOMEM;
1129 rta->rta_mx = mx;
1130 mx->rta_type = RTA_METRICS;
1131 mx->rta_len = RTA_LENGTH(0);
1132 if (r->rt_flags&RTF_MTU) {
1133 rec = (void*)((char*)mx + RTA_ALIGN(mx->rta_len));
1134 rec->rta_type = RTAX_ADVMSS;
1135 rec->rta_len = RTA_LENGTH(4);
1136 mx->rta_len += RTA_LENGTH(4);
1137 *(u32*)RTA_DATA(rec) = r->rt_mtu - 40;
1138 }
1139 if (r->rt_flags&RTF_WINDOW) {
1140 rec = (void*)((char*)mx + RTA_ALIGN(mx->rta_len));
1141 rec->rta_type = RTAX_WINDOW;
1142 rec->rta_len = RTA_LENGTH(4);
1143 mx->rta_len += RTA_LENGTH(4);
1144 *(u32*)RTA_DATA(rec) = r->rt_window;
1145 }
1146 if (r->rt_flags&RTF_IRTT) {
1147 rec = (void*)((char*)mx + RTA_ALIGN(mx->rta_len));
1148 rec->rta_type = RTAX_RTT;
1149 rec->rta_len = RTA_LENGTH(4);
1150 mx->rta_len += RTA_LENGTH(4);
1151 *(u32*)RTA_DATA(rec) = r->rt_irtt<<3;
1152 }
1153 }
1154 return 0;
1155}
1156
1157#endif
1158
1159/* 1006/*
1160 Update FIB if: 1007 Update FIB if:
1161 - local address disappeared -> we must delete all the entries 1008 - local address disappeared -> we must delete all the entries
diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 2a580eb2579b..41bef0a88ab6 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -1124,17 +1124,14 @@ err:
1124 return fa_head; 1124 return fa_head;
1125} 1125}
1126 1126
1127static int 1127static int fn_trie_insert(struct fib_table *tb, struct fib_config *cfg)
1128fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1129 struct nlmsghdr *nlhdr, struct netlink_skb_parms *req)
1130{ 1128{
1131 struct trie *t = (struct trie *) tb->tb_data; 1129 struct trie *t = (struct trie *) tb->tb_data;
1132 struct fib_alias *fa, *new_fa; 1130 struct fib_alias *fa, *new_fa;
1133 struct list_head *fa_head = NULL; 1131 struct list_head *fa_head = NULL;
1134 struct fib_info *fi; 1132 struct fib_info *fi;
1135 int plen = r->rtm_dst_len; 1133 int plen = cfg->fc_dst_len;
1136 int type = r->rtm_type; 1134 u8 tos = cfg->fc_tos;
1137 u8 tos = r->rtm_tos;
1138 u32 key, mask; 1135 u32 key, mask;
1139 int err; 1136 int err;
1140 struct leaf *l; 1137 struct leaf *l;
@@ -1142,11 +1139,7 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1142 if (plen > 32) 1139 if (plen > 32)
1143 return -EINVAL; 1140 return -EINVAL;
1144 1141
1145 key = 0; 1142 key = ntohl(cfg->fc_dst);
1146 if (rta->rta_dst)
1147 memcpy(&key, rta->rta_dst, 4);
1148
1149 key = ntohl(key);
1150 1143
1151 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen); 1144 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
1152 1145
@@ -1157,10 +1150,11 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1157 1150
1158 key = key & mask; 1151 key = key & mask;
1159 1152
1160 fi = fib_create_info(r, rta, nlhdr, &err); 1153 fi = fib_create_info(cfg);
1161 1154 if (IS_ERR(fi)) {
1162 if (!fi) 1155 err = PTR_ERR(fi);
1163 goto err; 1156 goto err;
1157 }
1164 1158
1165 l = fib_find_node(t, key); 1159 l = fib_find_node(t, key);
1166 fa = NULL; 1160 fa = NULL;
@@ -1185,10 +1179,10 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1185 struct fib_alias *fa_orig; 1179 struct fib_alias *fa_orig;
1186 1180
1187 err = -EEXIST; 1181 err = -EEXIST;
1188 if (nlhdr->nlmsg_flags & NLM_F_EXCL) 1182 if (cfg->fc_nlflags & NLM_F_EXCL)
1189 goto out; 1183 goto out;
1190 1184
1191 if (nlhdr->nlmsg_flags & NLM_F_REPLACE) { 1185 if (cfg->fc_nlflags & NLM_F_REPLACE) {
1192 struct fib_info *fi_drop; 1186 struct fib_info *fi_drop;
1193 u8 state; 1187 u8 state;
1194 1188
@@ -1200,8 +1194,8 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1200 fi_drop = fa->fa_info; 1194 fi_drop = fa->fa_info;
1201 new_fa->fa_tos = fa->fa_tos; 1195 new_fa->fa_tos = fa->fa_tos;
1202 new_fa->fa_info = fi; 1196 new_fa->fa_info = fi;
1203 new_fa->fa_type = type; 1197 new_fa->fa_type = cfg->fc_type;
1204 new_fa->fa_scope = r->rtm_scope; 1198 new_fa->fa_scope = cfg->fc_scope;
1205 state = fa->fa_state; 1199 state = fa->fa_state;
1206 new_fa->fa_state &= ~FA_S_ACCESSED; 1200 new_fa->fa_state &= ~FA_S_ACCESSED;
1207 1201
@@ -1224,17 +1218,17 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1224 break; 1218 break;
1225 if (fa->fa_info->fib_priority != fi->fib_priority) 1219 if (fa->fa_info->fib_priority != fi->fib_priority)
1226 break; 1220 break;
1227 if (fa->fa_type == type && 1221 if (fa->fa_type == cfg->fc_type &&
1228 fa->fa_scope == r->rtm_scope && 1222 fa->fa_scope == cfg->fc_scope &&
1229 fa->fa_info == fi) { 1223 fa->fa_info == fi) {
1230 goto out; 1224 goto out;
1231 } 1225 }
1232 } 1226 }
1233 if (!(nlhdr->nlmsg_flags & NLM_F_APPEND)) 1227 if (!(cfg->fc_nlflags & NLM_F_APPEND))
1234 fa = fa_orig; 1228 fa = fa_orig;
1235 } 1229 }
1236 err = -ENOENT; 1230 err = -ENOENT;
1237 if (!(nlhdr->nlmsg_flags & NLM_F_CREATE)) 1231 if (!(cfg->fc_nlflags & NLM_F_CREATE))
1238 goto out; 1232 goto out;
1239 1233
1240 err = -ENOBUFS; 1234 err = -ENOBUFS;
@@ -1244,8 +1238,8 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1244 1238
1245 new_fa->fa_info = fi; 1239 new_fa->fa_info = fi;
1246 new_fa->fa_tos = tos; 1240 new_fa->fa_tos = tos;
1247 new_fa->fa_type = type; 1241 new_fa->fa_type = cfg->fc_type;
1248 new_fa->fa_scope = r->rtm_scope; 1242 new_fa->fa_scope = cfg->fc_scope;
1249 new_fa->fa_state = 0; 1243 new_fa->fa_state = 0;
1250 /* 1244 /*
1251 * Insert new entry to the list. 1245 * Insert new entry to the list.
@@ -1262,7 +1256,8 @@ fn_trie_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1262 (fa ? &fa->fa_list : fa_head)); 1256 (fa ? &fa->fa_list : fa_head));
1263 1257
1264 rt_cache_flush(-1); 1258 rt_cache_flush(-1);
1265 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id, nlhdr, req); 1259 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id,
1260 &cfg->fc_nlinfo);
1266succeeded: 1261succeeded:
1267 return 0; 1262 return 0;
1268 1263
@@ -1548,28 +1543,21 @@ static int trie_leaf_remove(struct trie *t, t_key key)
1548 return 1; 1543 return 1;
1549} 1544}
1550 1545
1551static int 1546static int fn_trie_delete(struct fib_table *tb, struct fib_config *cfg)
1552fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1553 struct nlmsghdr *nlhdr, struct netlink_skb_parms *req)
1554{ 1547{
1555 struct trie *t = (struct trie *) tb->tb_data; 1548 struct trie *t = (struct trie *) tb->tb_data;
1556 u32 key, mask; 1549 u32 key, mask;
1557 int plen = r->rtm_dst_len; 1550 int plen = cfg->fc_dst_len;
1558 u8 tos = r->rtm_tos; 1551 u8 tos = cfg->fc_tos;
1559 struct fib_alias *fa, *fa_to_delete; 1552 struct fib_alias *fa, *fa_to_delete;
1560 struct list_head *fa_head; 1553 struct list_head *fa_head;
1561 struct leaf *l; 1554 struct leaf *l;
1562 struct leaf_info *li; 1555 struct leaf_info *li;
1563 1556
1564
1565 if (plen > 32) 1557 if (plen > 32)
1566 return -EINVAL; 1558 return -EINVAL;
1567 1559
1568 key = 0; 1560 key = ntohl(cfg->fc_dst);
1569 if (rta->rta_dst)
1570 memcpy(&key, rta->rta_dst, 4);
1571
1572 key = ntohl(key);
1573 mask = ntohl(inet_make_mask(plen)); 1561 mask = ntohl(inet_make_mask(plen));
1574 1562
1575 if (key & ~mask) 1563 if (key & ~mask)
@@ -1598,13 +1586,12 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1598 if (fa->fa_tos != tos) 1586 if (fa->fa_tos != tos)
1599 break; 1587 break;
1600 1588
1601 if ((!r->rtm_type || 1589 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
1602 fa->fa_type == r->rtm_type) && 1590 (cfg->fc_scope == RT_SCOPE_NOWHERE ||
1603 (r->rtm_scope == RT_SCOPE_NOWHERE || 1591 fa->fa_scope == cfg->fc_scope) &&
1604 fa->fa_scope == r->rtm_scope) && 1592 (!cfg->fc_protocol ||
1605 (!r->rtm_protocol || 1593 fi->fib_protocol == cfg->fc_protocol) &&
1606 fi->fib_protocol == r->rtm_protocol) && 1594 fib_nh_match(cfg, fi) == 0) {
1607 fib_nh_match(r, nlhdr, rta, fi) == 0) {
1608 fa_to_delete = fa; 1595 fa_to_delete = fa;
1609 break; 1596 break;
1610 } 1597 }
@@ -1614,7 +1601,8 @@ fn_trie_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
1614 return -ESRCH; 1601 return -ESRCH;
1615 1602
1616 fa = fa_to_delete; 1603 fa = fa_to_delete;
1617 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, nlhdr, req); 1604 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id,
1605 &cfg->fc_nlinfo);
1618 1606
1619 l = fib_find_node(t, key); 1607 l = fib_find_node(t, key);
1620 li = find_leaf_info(l, plen); 1608 li = find_leaf_info(l, plen);