History

ipv4: Fix fib_nlmsg_size() for RTA_VIA nexthops

fib_nlmsg_size() still estimates nexthop space as if every gateway is encoded as an IPv4 RTA_GATEWAY attribute. IPv4 routes can also carry an IPv6 gateway, which fib_nexthop_info() dumps as RTA_VIA. As a result, route notifications can allocate an skb that is too small. fib_dump_info() then fails with -EMSGSIZE and rtmsg_fib() hits the WARN_ON() that marks such failures as a fib_nlmsg_size() bug. With panic_on_warn set, this becomes a kernel panic. Mirror the actual nexthop dump layout in fib_nlmsg_size(): account for IPv6 nexthop gateways dumped as RTA_VIA, for the no-header rtnexthop layout used inside RTA_MULTIPATH, and for RTA_FLOW only when it is actually present.

Zihan Xi committed 3 months ago

Commit 4ff95481 parent 5d9686a1 file · +49 −18Browse files

Unified · Split

net/ipv4/fib_semantics.c+49−18
@@ -490,6 +490,34 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev) · 489 unmodified lines
490 return -1;490 return -1;
491}491}
492 492
493static size_t fib_nexthop_nlmsg_size(const struct fib_nh_common *nhc,
494 bool skip_oif)
495{
496 size_t nhsize = 0;
497
498 switch (nhc->nhc_gw_family) {
499 case AF_INET:
500 nhsize += nla_total_size(4); /* RTA_GATEWAY */
501 break;
502 case AF_INET6:
503 nhsize += nla_total_size(sizeof(struct rtvia) +
504 sizeof(struct in6_addr));
505 break;
506 }
507
508 if (!skip_oif && nhc->nhc_dev)
509 nhsize += nla_total_size(4); /* RTA_OIF */
510
511 if (nhc->nhc_lwtstate) {
512 /* RTA_ENCAP */
513 nhsize += lwtunnel_get_encap_size(nhc->nhc_lwtstate);
514 /* RTA_ENCAP_TYPE */
515 nhsize += nla_total_size(2);
516 }
517
518 return nhsize;
519}
520
493size_t fib_nlmsg_size(struct fib_info *fi)521size_t fib_nlmsg_size(struct fib_info *fi)
494{522{
495 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))523 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
@@ -507,32 +535,35 @@ size_t fib_nlmsg_size(struct fib_info *fi) · 11 unmodified lines
507 payload += nla_total_size(4); /* RTA_NH_ID */535 payload += nla_total_size(4); /* RTA_NH_ID */
508 536
509 if (nhs) {537 if (nhs) {
510 size_t nh_encapsize = 0;538 size_t mpsize = 0;
511 /* Also handles the special case nhs == 1 */
512
513 /* each nexthop is packed in an attribute */
514 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
515 unsigned int i;539 unsigned int i;
516 540
517 /* may contain flow and gateway attribute */
518 nhsize += 2 * nla_total_size(4);
519
520 /* grab encap info */
521 for (i = 0; i < fib_info_num_path(fi); i++) {541 for (i = 0; i < fib_info_num_path(fi); i++) {
522 struct fib_nh_common *nhc = fib_info_nhc(fi, i);542 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
543 size_t nhsize;
544
545 nhsize = fib_nexthop_nlmsg_size(nhc, nhs != 1);
523 546
524 if (nhc->nhc_lwtstate) {547 if (nhs != 1)
525 /* RTA_ENCAP_TYPE */548 nhsize += NLA_ALIGN(sizeof(struct rtnexthop));
526 nh_encapsize += lwtunnel_get_encap_size(549
527 nhc->nhc_lwtstate);550#ifdef CONFIG_IP_ROUTE_CLASSID
528 /* RTA_ENCAP */551 if (nhc->nhc_family == AF_INET) {
529 nh_encapsize += nla_total_size(2);552 struct fib_nh *nh;
553
554 nh = container_of(nhc, struct fib_nh, nh_common);
555 if (nh->nh_tclassid)
556 nhsize += nla_total_size(4);
530 }557 }
558#endif
559 if (nhs == 1)
560 payload += nhsize;
561 else
562 mpsize += nhsize;
531 }563 }
532 564
533 /* all nexthops are packed in a nested attribute */565 if (nhs != 1)
534 payload += nla_total_size((nhs * nhsize) + nh_encapsize);566 payload += nla_total_size(mpsize);
535
536 }567 }
537 568
538 return payload;569 return payload;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t