Skip to content

Commit 499834b

Browse files
leitaoNipaLocal
authored andcommitted
netpoll: Extract IPv6 address retrieval function
Extract the IPv6 address retrieval logic from netpoll_setup() into a dedicated helper function netpoll_take_ipv6() to improve code organization and readability. The function handles obtaining the local IPv6 address from the network device, including proper address type matching between local and remote addresses (link-local vs global), and includes appropriate error handling when IPv6 is not supported or no suitable address is available. Signed-off-by: Breno Leitao <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent e9c4d50 commit 499834b

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

net/core/netpoll.c

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,47 @@ static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev,
598598
}
599599
}
600600

601+
/*
602+
* Take the IPv6 from ndev and populate local_ip structure in netpoll
603+
*/
604+
static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
605+
{
606+
char buf[MAC_ADDR_STR_LEN + 1];
607+
int err = -EDESTADDRREQ;
608+
struct inet6_dev *idev;
609+
610+
if (!IS_ENABLED(CONFIG_IPV6)) {
611+
np_err(np, "IPv6 is not supported %s, aborting\n",
612+
egress_dev(np, buf));
613+
return -EINVAL;
614+
}
615+
616+
idev = __in6_dev_get(ndev);
617+
if (idev) {
618+
struct inet6_ifaddr *ifp;
619+
620+
read_lock_bh(&idev->lock);
621+
list_for_each_entry(ifp, &idev->addr_list, if_list) {
622+
if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
623+
!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
624+
continue;
625+
/* Got the IP, let's return */
626+
np->local_ip.in6 = ifp->addr;
627+
err = 0;
628+
break;
629+
}
630+
read_unlock_bh(&idev->lock);
631+
}
632+
if (err) {
633+
np_err(np, "no IPv6 address for %s, aborting\n",
634+
egress_dev(np, buf));
635+
return err;
636+
}
637+
638+
np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
639+
return 0;
640+
}
641+
601642
/*
602643
* Take the IPv4 from ndev and populate local_ip structure in netpoll
603644
*/
@@ -675,41 +716,12 @@ int netpoll_setup(struct netpoll *np)
675716
err = netpoll_take_ipv4(np, ndev);
676717
if (err)
677718
goto put;
678-
ip_overwritten = true;
679719
} else {
680-
#if IS_ENABLED(CONFIG_IPV6)
681-
struct inet6_dev *idev;
682-
683-
err = -EDESTADDRREQ;
684-
idev = __in6_dev_get(ndev);
685-
if (idev) {
686-
struct inet6_ifaddr *ifp;
687-
688-
read_lock_bh(&idev->lock);
689-
list_for_each_entry(ifp, &idev->addr_list, if_list) {
690-
if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
691-
!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
692-
continue;
693-
np->local_ip.in6 = ifp->addr;
694-
ip_overwritten = true;
695-
err = 0;
696-
break;
697-
}
698-
read_unlock_bh(&idev->lock);
699-
}
700-
if (err) {
701-
np_err(np, "no IPv6 address for %s, aborting\n",
702-
egress_dev(np, buf));
720+
err = netpoll_take_ipv6(np, ndev);
721+
if (err)
703722
goto put;
704-
} else
705-
np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
706-
#else
707-
np_err(np, "IPv6 is not supported %s, aborting\n",
708-
egress_dev(np, buf));
709-
err = -EINVAL;
710-
goto put;
711-
#endif
712723
}
724+
ip_overwritten = true;
713725
}
714726

715727
err = __netpoll_setup(np, ndev);

0 commit comments

Comments
 (0)