From: Alexander Bluhm Subject: check INP_IPV6 in callee To: tech@openbsd.org Date: Fri, 12 Jan 2024 00:22:51 +0100 Hi, A bunch of IPv4 funtions call their IPv6 counterpart if INP_IPV6 is set for the socket. I would like to use that consistently as the code in the caller gets simpler. I think the result is more readable and consistent. ok? bluhm Index: netinet/in_pcb.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/in_pcb.c,v diff -u -p -r1.284 in_pcb.c --- netinet/in_pcb.c 9 Jan 2024 19:57:00 -0000 1.284 +++ netinet/in_pcb.c 11 Jan 2024 12:43:58 -0000 @@ -517,7 +517,7 @@ in_pcbconnect(struct inpcb *inp, struct #ifdef INET6 if (ISSET(inp->inp_flags, INP_IPV6)) return (in6_pcbconnect(inp, nam)); -#endif /* INET6 */ +#endif if ((error = in_nam2sin(nam, &sin))) return (error); @@ -652,6 +652,13 @@ in_setsockaddr(struct inpcb *inp, struct { struct sockaddr_in *sin; +#ifdef INET6 + if (ISSET(inp->inp_flags, INP_IPV6)) { + in6_setsockaddr(inp, nam); + return; + } +#endif + nam->m_len = sizeof(*sin); sin = mtod(nam, struct sockaddr_in *); memset(sin, 0, sizeof(*sin)); @@ -671,7 +678,7 @@ in_setpeeraddr(struct inpcb *inp, struct in6_setpeeraddr(inp, nam); return; } -#endif /* INET6 */ +#endif nam->m_len = sizeof(*sin); sin = mtod(nam, struct sockaddr_in *); Index: netinet/tcp_usrreq.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/tcp_usrreq.c,v diff -u -p -r1.227 tcp_usrreq.c --- netinet/tcp_usrreq.c 3 Dec 2023 20:24:17 -0000 1.227 +++ netinet/tcp_usrreq.c 11 Jan 2024 12:36:37 -0000 @@ -502,7 +502,7 @@ tcp_detach(struct socket *so) { struct inpcb *inp; struct tcpcb *otp = NULL, *tp; - int error = 0; + int error; short ostate; soassertlocked(so); @@ -526,7 +526,7 @@ tcp_detach(struct socket *so) if (otp) tcp_trace(TA_USER, ostate, tp, otp, NULL, PRU_DETACH, 0); - return (error); + return (0); } /* @@ -685,26 +685,17 @@ tcp_accept(struct socket *so, struct mbu struct inpcb *inp; struct tcpcb *tp; int error; - short ostate; soassertlocked(so); if ((error = tcp_sogetpcb(so, &inp, &tp))) return (error); - if (so->so_options & SO_DEBUG) - ostate = tp->t_state; - -#ifdef INET6 - if (inp->inp_flags & INP_IPV6) - in6_setpeeraddr(inp, nam); - else -#endif - in_setpeeraddr(inp, nam); + in_setpeeraddr(inp, nam); if (so->so_options & SO_DEBUG) - tcp_trace(TA_USER, ostate, tp, tp, NULL, PRU_ACCEPT, 0); - return (error); + tcp_trace(TA_USER, tp->t_state, tp, tp, NULL, PRU_ACCEPT, 0); + return (0); } /* @@ -994,12 +985,7 @@ tcp_sockaddr(struct socket *so, struct m if ((error = tcp_sogetpcb(so, &inp, &tp))) return (error); -#ifdef INET6 - if (inp->inp_flags & INP_IPV6) - in6_setsockaddr(inp, nam); - else -#endif - in_setsockaddr(inp, nam); + in_setsockaddr(inp, nam); if (so->so_options & SO_DEBUG) tcp_trace(TA_USER, tp->t_state, tp, tp, NULL, @@ -1019,16 +1005,10 @@ tcp_peeraddr(struct socket *so, struct m if ((error = tcp_sogetpcb(so, &inp, &tp))) return (error); -#ifdef INET6 - if (inp->inp_flags & INP_IPV6) - in6_setpeeraddr(inp, nam); - else -#endif - in_setpeeraddr(inp, nam); + in_setpeeraddr(inp, nam); if (so->so_options & SO_DEBUG) - tcp_trace(TA_USER, tp->t_state, tp, tp, NULL, - PRU_PEERADDR, 0); + tcp_trace(TA_USER, tp->t_state, tp, tp, NULL, PRU_PEERADDR, 0); return (0); } Index: netinet/udp_usrreq.c =================================================================== RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/udp_usrreq.c,v diff -u -p -r1.313 udp_usrreq.c --- netinet/udp_usrreq.c 10 Jan 2024 16:44:30 -0000 1.313 +++ netinet/udp_usrreq.c 11 Jan 2024 12:23:30 -0000 @@ -936,9 +936,9 @@ udp_output(struct inpcb *inp, struct mbu struct in_addr laddr; int error = 0; -#ifdef DIAGNOSTIC - if ((inp->inp_flags & INP_IPV6) != 0) - panic("IPv6 inpcb to %s", __func__); +#ifdef INET6 + if (ISSET(inp->inp_flags, INP_IPV6)) + return (udp6_output(inp, m, addr, control)); #endif /* @@ -1230,7 +1230,6 @@ udp_send(struct socket *so, struct mbuf struct mbuf *control) { struct inpcb *inp = sotoinpcb(so); - int error; soassertlocked(so); @@ -1265,14 +1264,7 @@ udp_send(struct socket *so, struct mbuf } #endif -#ifdef INET6 - if (inp->inp_flags & INP_IPV6) - error = udp6_output(inp, m, addr, control); - else -#endif - error = udp_output(inp, m, addr, control); - - return (error); + return (udp_output(inp, m, addr, control)); } /*