git: 74ed2e8ab202 - main - raw ip: fix regression with multicast and RSVP

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 02 Sep 2022 19:17:21 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=74ed2e8ab20264595bfbf67c117462df68b856ce

commit 74ed2e8ab20264595bfbf67c117462df68b856ce
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-09-02 19:17:09 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-09-02 19:17:09 +0000

    raw ip: fix regression with multicast and RSVP
    
    With 61f7427f02a raw sockets protosw has wildcard pr_protocol.  Protocol
    of a specific pcb is stored in inp_ip_p.
    
    Reviewed by:            karels
    Reported by:            karels
    Differential revision:  https://reviews.freebsd.org/D36429
    Fixes:                  61f7427f02a307d28af674a12c45dd546e3898e4
---
 sys/netinet/ip_input.c    | 4 ----
 sys/netinet/ip_mroute.c   | 6 +-----
 sys/netinet/raw_ip.c      | 8 ++++++++
 sys/netinet6/ip6_mroute.c | 7 +------
 sys/netinet6/ip6_output.c | 5 ++---
 sys/netinet6/raw_ip6.c    | 7 +++++--
 6 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
index 059f26869e0e..41ba82ea81dd 100644
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -1313,10 +1313,6 @@ int
 ip_rsvp_init(struct socket *so)
 {
 
-	if (so->so_type != SOCK_RAW ||
-	    so->so_proto->pr_protocol != IPPROTO_RSVP)
-		return EOPNOTSUPP;
-
 	if (V_ip_rsvpd != NULL)
 		return EADDRINUSE;
 
diff --git a/sys/netinet/ip_mroute.c b/sys/netinet/ip_mroute.c
index f77898fb3aa4..383f2dfc076e 100644
--- a/sys/netinet/ip_mroute.c
+++ b/sys/netinet/ip_mroute.c
@@ -686,11 +686,7 @@ static int
 ip_mrouter_init(struct socket *so, int version)
 {
 
-    CTR3(KTR_IPMF, "%s: so_type %d, pr_protocol %d", __func__,
-        so->so_type, so->so_proto->pr_protocol);
-
-    if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP)
-	return EOPNOTSUPP;
+    CTR2(KTR_IPMF, "%s: so %p", __func__, so);
 
     if (version != 1)
 	return ENOPROTOOPT;
diff --git a/sys/netinet/raw_ip.c b/sys/netinet/raw_ip.c
index 3e7c81341c08..fb5628324020 100644
--- a/sys/netinet/raw_ip.c
+++ b/sys/netinet/raw_ip.c
@@ -693,6 +693,8 @@ rip_ctloutput(struct socket *so, struct sockopt *sopt)
 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
 			if (error != 0)
 				return (error);
+			if (inp->inp_ip_p != IPPROTO_IGMP)
+				return (EOPNOTSUPP);
 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
 				EOPNOTSUPP;
 			break;
@@ -747,6 +749,8 @@ rip_ctloutput(struct socket *so, struct sockopt *sopt)
 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
 			if (error != 0)
 				return (error);
+			if (inp->inp_ip_p != IPPROTO_RSVP)
+				return (EOPNOTSUPP);
 			error = ip_rsvp_init(so);
 			break;
 
@@ -762,6 +766,8 @@ rip_ctloutput(struct socket *so, struct sockopt *sopt)
 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
 			if (error != 0)
 				return (error);
+			if (inp->inp_ip_p != IPPROTO_RSVP)
+				return (EOPNOTSUPP);
 			error = ip_rsvp_vif ?
 				ip_rsvp_vif(so, sopt) : EINVAL;
 			break;
@@ -781,6 +787,8 @@ rip_ctloutput(struct socket *so, struct sockopt *sopt)
 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
 			if (error != 0)
 				return (error);
+			if (inp->inp_ip_p != IPPROTO_IGMP)
+				return (EOPNOTSUPP);
 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
 					EOPNOTSUPP;
 			break;
diff --git a/sys/netinet6/ip6_mroute.c b/sys/netinet6/ip6_mroute.c
index 9e864859c279..9465c6662018 100644
--- a/sys/netinet6/ip6_mroute.c
+++ b/sys/netinet6/ip6_mroute.c
@@ -555,12 +555,7 @@ static int
 ip6_mrouter_init(struct socket *so, int v, int cmd)
 {
 
-	MRT6_DLOG(DEBUG_ANY, "so_type = %d, pr_protocol = %d",
-	    so->so_type, so->so_proto->pr_protocol);
-
-	if (so->so_type != SOCK_RAW ||
-	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
-		return (EOPNOTSUPP);
+	MRT6_DLOG(DEBUG_ANY, "%s: socket %p", __func__, so);
 
 	if (v != 1)
 		return (ENOPROTOOPT);
diff --git a/sys/netinet6/ip6_output.c b/sys/netinet6/ip6_output.c
index fc330290d6a3..a1504bfb8da6 100644
--- a/sys/netinet6/ip6_output.c
+++ b/sys/netinet6/ip6_output.c
@@ -2451,8 +2451,7 @@ ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
 				 * values or -1 as a special value.
 				 */
 				error = EINVAL;
-			} else if (so->so_proto->pr_protocol ==
-			    IPPROTO_ICMPV6) {
+			} else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
 				if (optval != icmp6off)
 					error = EINVAL;
 			} else
@@ -2460,7 +2459,7 @@ ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
 			break;
 
 		case SOPT_GET:
-			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
+			if (inp->inp_ip_p == IPPROTO_ICMPV6)
 				optval = icmp6off;
 			else
 				optval = inp->in6p_cksum;
diff --git a/sys/netinet6/raw_ip6.c b/sys/netinet6/raw_ip6.c
index acc6787d59a7..0a5d343f3725 100644
--- a/sys/netinet6/raw_ip6.c
+++ b/sys/netinet6/raw_ip6.c
@@ -596,7 +596,7 @@ release:
 int
 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
 {
-	struct inpcb *inp;
+	struct inpcb *inp = sotoinpcb(so);
 	int error;
 
 	if (sopt->sopt_level == IPPROTO_ICMPV6)
@@ -608,7 +608,6 @@ rip6_ctloutput(struct socket *so, struct sockopt *sopt)
 	else if (sopt->sopt_level != IPPROTO_IPV6) {
 		if (sopt->sopt_level == SOL_SOCKET &&
 		    sopt->sopt_name == SO_SETFIB) {
-			inp = sotoinpcb(so);
 			INP_WLOCK(inp);
 			inp->inp_inc.inc_fibnum = so->so_fibnum;
 			INP_WUNLOCK(inp);
@@ -629,6 +628,8 @@ rip6_ctloutput(struct socket *so, struct sockopt *sopt)
 		case MRT6_ADD_MFC:
 		case MRT6_DEL_MFC:
 		case MRT6_PIM:
+			if (inp->inp_ip_p != IPPROTO_ICMPV6)
+				return (EOPNOTSUPP);
 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
 			    EOPNOTSUPP;
 			break;
@@ -650,6 +651,8 @@ rip6_ctloutput(struct socket *so, struct sockopt *sopt)
 		case MRT6_ADD_MFC:
 		case MRT6_DEL_MFC:
 		case MRT6_PIM:
+			if (inp->inp_ip_p != IPPROTO_ICMPV6)
+				return (EOPNOTSUPP);
 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
 			    EOPNOTSUPP;
 			break;