Index | Thread | Search

From:
Vitaliy Makkoveev <otto@bsdbox.dev>
Subject:
Re: assert inp has correct IPv6 flag
To:
Alexander Bluhm <alexander.bluhm@gmx.net>
Cc:
tech@openbsd.org
Date:
Sat, 20 Jan 2024 09:23:40 +0300

Download raw body.

Thread
> On 19 Jan 2024, at 19:50, Alexander Bluhm <alexander.bluhm@gmx.net> wrote:
> 
> Hi,
> 
> Since inpcb tables for UDP and Raw IP have been split into IPv4 and
> IPv6, we can assert that INP_IPV6 flag is correct instead of checking
> it.
> 
> While there give the table variable a nicer name.
> 
> ok?
> 

ok mvs

> bluhm
> 
> Index: netinet/raw_ip.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/raw_ip.c,v
> diff -u -p -r1.153 raw_ip.c
> --- netinet/raw_ip.c	15 Dec 2023 00:24:56 -0000	1.153
> +++ netinet/raw_ip.c	19 Jan 2024 16:02:22 -0000
> @@ -171,12 +171,10 @@ rip_input(struct mbuf **mp, int *offp, i
> 	rw_enter_write(&rawcbtable.inpt_notify);
> 	mtx_enter(&rawcbtable.inpt_mtx);
> 	TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) {
> +		KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
> +
> 		if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE)
> 			continue;
> -#ifdef INET6
> -		if (inp->inp_flags & INP_IPV6)
> -			continue;
> -#endif
> 		if (rtable_l2(inp->inp_rtableid) !=
> 		    rtable_l2(m->m_pkthdr.ph_rtableid))
> 			continue;
> Index: netinet/udp_usrreq.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/udp_usrreq.c,v
> diff -u -p -r1.314 udp_usrreq.c
> --- netinet/udp_usrreq.c	19 Jan 2024 02:24:07 -0000	1.314
> +++ netinet/udp_usrreq.c	19 Jan 2024 16:00:13 -0000
> @@ -381,7 +381,7 @@ udp_input(struct mbuf **mp, int *offp, i
> 
> 	if (m->m_flags & (M_BCAST|M_MCAST)) {
> 		SIMPLEQ_HEAD(, inpcb) inpcblist;
> -		struct inpcbtable *tb;
> +		struct inpcbtable *table;
> 
> 		/*
> 		 * Deliver a multicast or broadcast datagram to *all* sockets
> @@ -406,23 +406,21 @@ udp_input(struct mbuf **mp, int *offp, i
> 		SIMPLEQ_INIT(&inpcblist);
> #ifdef INET6
> 		if (ip6)
> -			tb = &udb6table;
> +			table = &udb6table;
> 		else
> #endif
> -			tb = &udbtable;
> +			table = &udbtable;
> 
> -		rw_enter_write(&tb->inpt_notify);
> -		mtx_enter(&tb->inpt_mtx);
> -		TAILQ_FOREACH(inp, &tb->inpt_queue, inp_queue) {
> -			if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE)
> -				continue;
> -#ifdef INET6
> -			/* table is per AF, panic if it does not match */
> +		rw_enter_write(&table->inpt_notify);
> +		mtx_enter(&table->inpt_mtx);
> +		TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
> 			if (ip6)
> 				KASSERT(ISSET(inp->inp_flags, INP_IPV6));
> 			else
> 				KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
> -#endif
> +
> +			if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE)
> +				continue;
> 			if (rtable_l2(inp->inp_rtableid) !=
> 			    rtable_l2(m->m_pkthdr.ph_rtableid))
> 				continue;
> @@ -481,10 +479,10 @@ udp_input(struct mbuf **mp, int *offp, i
> 			    SO_REUSEADDR)) == 0)
> 				break;
> 		}
> -		mtx_leave(&tb->inpt_mtx);
> +		mtx_leave(&table->inpt_mtx);
> 
> 		if (SIMPLEQ_EMPTY(&inpcblist)) {
> -			rw_exit_write(&tb->inpt_notify);
> +			rw_exit_write(&table->inpt_notify);
> 
> 			/*
> 			 * No matching pcb found; discard datagram.
> @@ -509,7 +507,7 @@ udp_input(struct mbuf **mp, int *offp, i
> 			}
> 			in_pcbunref(inp);
> 		}
> -		rw_exit_write(&tb->inpt_notify);
> +		rw_exit_write(&table->inpt_notify);
> 
> 		return IPPROTO_DONE;
> 	}
> @@ -1098,7 +1096,7 @@ release:
> int
> udp_attach(struct socket *so, int proto, int wait)
> {
> -	struct inpcbtable *tb;
> +	struct inpcbtable *table;
> 	int error;
> 
> 	if (so->so_pcb != NULL)
> @@ -1110,11 +1108,11 @@ udp_attach(struct socket *so, int proto,
> 	NET_ASSERT_LOCKED();
> #ifdef INET6
> 	if (so->so_proto->pr_domain->dom_family == PF_INET6)
> -		tb = &udb6table;
> +		table = &udb6table;
> 	else
> #endif
> -		tb = &udbtable;
> -	if ((error = in_pcballoc(so, tb, wait)))
> +		table = &udbtable;
> +	if ((error = in_pcballoc(so, table, wait)))
> 		return error;
> #ifdef INET6
> 	if (sotoinpcb(so)->inp_flags & INP_IPV6)
> Index: netinet6/raw_ip6.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/raw_ip6.c,v
> diff -u -p -r1.178 raw_ip6.c
> --- netinet6/raw_ip6.c	15 Dec 2023 00:24:56 -0000	1.178
> +++ netinet6/raw_ip6.c	19 Jan 2024 16:00:05 -0000
> @@ -183,14 +183,14 @@ rip6_input(struct mbuf **mp, int *offp, 
> 	rw_enter_write(&rawin6pcbtable.inpt_notify);
> 	mtx_enter(&rawin6pcbtable.inpt_mtx);
> 	TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue, inp_queue) {
> +		KASSERT(ISSET(inp->inp_flags, INP_IPV6));
> +
> 		if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE)
> 			continue;
> 		if (rtable_l2(inp->inp_rtableid) !=
> 		    rtable_l2(m->m_pkthdr.ph_rtableid))
> 			continue;
> 
> -		if (!(inp->inp_flags & INP_IPV6))
> -			continue;
> 		if ((inp->inp_ipv6.ip6_nxt || proto == IPPROTO_ICMPV6) &&
> 		    inp->inp_ipv6.ip6_nxt != proto)
> 			continue;
>