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
490490 return -1;
491491 }
492492
493+static 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+
493521 size_t fib_nlmsg_size(struct fib_info *fi)
494522 {
495523 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
@@ -507,32 +535,35 @@ size_t fib_nlmsg_size(struct fib_info *fi) · 11 unmodified lines
507535 payload += nla_total_size(4); /* RTA_NH_ID */
508536
509537 if (nhs) {
510− size_t nh_encapsize = 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));
538+ size_t mpsize = 0;
515539 unsigned int i;
516540
517− /* may contain flow and gateway attribute */
518− nhsize += 2 * nla_total_size(4);
519−
520− /* grab encap info */
521541 for (i = 0; i < fib_info_num_path(fi); i++) {
522542 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
543+ size_t nhsize;
544+
545+ nhsize = fib_nexthop_nlmsg_size(nhc, nhs != 1);
523546
524− if (nhc->nhc_lwtstate) {
525− /* RTA_ENCAP_TYPE */
526− nh_encapsize += lwtunnel_get_encap_size(
527− nhc->nhc_lwtstate);
528− /* RTA_ENCAP */
529− nh_encapsize += nla_total_size(2);
547+ if (nhs != 1)
548+ nhsize += NLA_ALIGN(sizeof(struct rtnexthop));
549+
550+#ifdef CONFIG_IP_ROUTE_CLASSID
551+ if (nhc->nhc_family == AF_INET) {
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);
530557 }
558+#endif
559+ if (nhs == 1)
560+ payload += nhsize;
561+ else
562+ mpsize += nhsize;
531563 }
532564
533− /* all nexthops are packed in a nested attribute */
534− payload += nla_total_size((nhs * nhsize) + nh_encapsize);
535−
565+ if (nhs != 1)
566+ payload += nla_total_size(mpsize);
536567 }
537568
538569 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