igmp: fix the problem when mc leave group
Subsystems (3)
PATCH 1/1 igmp: fix the problem when mc leave group
The problem was triggered by these steps:
1) create socket, bind and then setsockopt for add mc group. mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("192.168.1.2"); setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
2) drop the mc group for this socket.
mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("0.0.0.0"); setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
3) and then drop the socket, I found the mc group was still used by the dev:
netstat -g
Interface RefCnt Group
--------------- ------ ---------------------
eth2 1 255.0.0.37
Normally even though the IP_DROP_MEMBERSHIP return error, the mc group still need to be released for the netdev when drop the socket, but this process was broken when route default is NULL, the reason is that:
The ip_mc_leave_group() will choose the in_dev by the imr_interface.s_addr, if input addr is NULL, the default route dev will be chosen, then the ifindex is got from the dev, then polling the inet->mc_list and return -ENODEV, but if the default route dev is NULL, the in_dev and ifIndex is both NULL, when polling the inet->mc_list, the mc group will be released from the mc_list, but the dev didn't dec the refcnt for this mc group, so when dropping the socket, the mc_list is NULL and the dev still keep this group.
Fix this by checking the ifindex when polling the mc_list in ip_mc_leave_group(), don't release the mc group from the inet->mc_list if the index is 0, leave this work to ip_mc_drop_socket().
--- net/ipv4/igmp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
net/ipv4/igmp.c +2 -3
@@ -1950,10 +1950,9 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) imlp = &iml->next_rcu) {if (iml->multi.imr_multiaddr.s_addr != group)continue ; -if (ifindex) { -if (iml->multi.imr_ifindex != ifindex) +if (ifindex || iml->multi.imr_ifindex != ifindex)continue ; - }else if (imr->imr_address.s_addr && imr->imr_address.s_addr != +else if (imr->imr_address.s_addr && imr->imr_address.s_addr != iml->multi.imr_address.s_addr)continue ;
Signature · 1 line
1.8.0
Hi,
Quoted 31 lines · Ding Tianhong
The problem was triggered by these steps:
1) create socket, bind and then setsockopt for add mc group.
mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("192.168.1.2"); setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));2) drop the mc group for this socket.
mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("0.0.0.0"); setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));3) and then drop the socket, I found the mc group was still used by the dev:
netstat -g Interface RefCnt Group --------------- ------ --------------------- eth2 1 255.0.0.37Normally even though the
IP_DROP_MEMBERSHIPreturn error, the mc group still need to be released for the netdev when drop the socket, but this process was broken when route default is NULL, the reason is that:The
ip_mc_leave_group()will choose thein_devby the imr_interface.s_addr, if input addr is NULL, the default route dev will be chosen, then the ifindex is got from the dev, then polling theinet->mc_listand return-ENODEV, but if the default route dev is NULL, thein_devand ifIndex is both NULL, when polling theinet->mc_list, the mc group will be released from themc_list, but the dev didn't dec the refcnt for this mc group, so when dropping the socket, themc_listis NULL and the dev still keep this group.
Quoted 4 lines
Fix this by checking the ifindex when polling the
mc_listinip_mc_leave_group(), don't release the mc group from theinet->mc_listif the index is 0, leave this work toip_mc_drop_socket().
No, we should make it aligned with IPv6 (RFC3493) and BSDs, so...
Quoted 10 lines
Signed-off-by Ding Tianhong <[email protected]>--- net/ipv4/igmp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/ipv4/igmp.c +0
-0
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1953
Quoted 6 lines
net/ipv4/igmp.c +1
-2@@ -1950,10 +1950,9 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) imlp = &iml->next_rcu) {if (iml->multi.imr_multiaddr.s_addr != group)continue ; -if (ifindex) { -if (iml->multi.imr_ifindex != ifindex) +if (ifindex || iml->multi.imr_ifindex != ifindex)
ifindex && iml->multi.imr_ifindex != ifindex
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1954
Quoted 1 line
net/ipv4/igmp.c +0
-0continue ;
Fix indentation.
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1958
Quoted 6 lines
net/ipv4/igmp.c +1
-1- }else if (imr->imr_address.s_addr && imr->imr_address.s_addr != +else if (imr->imr_address.s_addr && imr->imr_address.s_addr != iml->multi.imr_address.s_addr)continue ;
Regarads,
Signature · 2 lines
Hideaki Yoshifuji <[email protected]> Technical Division, MIRACLE LINUX CORPORATION
Quoted 34 lines · YOSHIFUJI Hideaki
Hi,
Quoted 31 lines · Ding Tianhong
The problem was triggered by these steps:
1) create socket, bind and then setsockopt for add mc group.
mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("192.168.1.2"); setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));2) drop the mc group for this socket.
mreq.imr_multiaddr.s_addr = inet_addr("255.0.0.37"); mreq.imr_interface.s_addr = inet_addr("0.0.0.0"); setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));3) and then drop the socket, I found the mc group was still used by the dev:
netstat -g Interface RefCnt Group --------------- ------ --------------------- eth2 1 255.0.0.37Normally even though the
IP_DROP_MEMBERSHIPreturn error, the mc group still need to be released for the netdev when drop the socket, but this process was broken when route default is NULL, the reason is that:The
ip_mc_leave_group()will choose thein_devby the imr_interface.s_addr, if input addr is NULL, the default route dev will be chosen, then the ifindex is got from the dev, then polling theinet->mc_listand return-ENODEV, but if the default route dev is NULL, thein_devand ifIndex is both NULL, when polling theinet->mc_list, the mc group will be released from themc_list, but the dev didn't dec the refcnt for this mc group, so when dropping the socket, themc_listis NULL and the dev still keep this group.
Quoted 8 lines
Quoted 4 lines
Fix this by checking the ifindex when polling the
mc_listinip_mc_leave_group(), don't release the mc group from theinet->mc_listif the index is 0, leave this work toip_mc_drop_socket().No, we should make it aligned with IPv6 (RFC3493) and BSDs, so...
Ok, I will check it and then think about this problem.
Quoted 5 lines
Quoted 5 lines
Signed-off-by Ding Tianhong <[email protected]>--- net/ipv4/igmp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Quoted 14 lines
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1953
Quoted 11 lines
net/ipv4/igmp.c +1
-2@@ -1950,10 +1950,9 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) imlp = &iml->next_rcu) {if (iml->multi.imr_multiaddr.s_addr != group)continue ; -if (ifindex) { -if (iml->multi.imr_ifindex != ifindex) +if (ifindex || iml->multi.imr_ifindex != ifindex)ifindex &&
iml->multi.imr_ifindex!= ifindex
yes, my solution looks get another problem, thanks for your advise, I will fix it.
Ding
Quoted 13 lines
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1954
Quoted 1 line
net/ipv4/igmp.c +0
-0continue ;Fix indentation.
net/ipv4/igmp.c int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) · line 1958
Quoted 6 lines
net/ipv4/igmp.c +1
-1- }else if (imr->imr_address.s_addr && imr->imr_address.s_addr != +else if (imr->imr_address.s_addr && imr->imr_address.s_addr != iml->multi.imr_address.s_addr)continue ;Regarads,