From nobody Tue Jan 17 10:07:02 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Nx4Lb1fW6z2srY5; Tue, 17 Jan 2023 10:07:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Nx4Lb10dyz3mDw; Tue, 17 Jan 2023 10:07:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673950023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=R1tZwZ7n/7Lddf1K770W3Cu/f3NjvQ9j1WeOfz2Uaag=; b=M7nIed/YqFsCr9y0VubdlxWbKExSGjfPu4gGYJbL4IMjF4MZp0zwrVlHi0vb5tH7MplQDd yH5Vs7rQl2Zv0dyW+BsXEHsYIvp2KjPVjR6hqm+9cYNrujsQuGuvrt58zskJS5YE1hPG79 /owrZfHNqZ9f5OVz0uyaIQmM4/gn8mNl7z+eK8CfCnlF4XsM4xO3gjHdmUwhOinAgsurH3 X/9e4D0hT1XGTQDhbo3D5FjxH6r3e4WDS9sVN6kgn1Uwxfsjn0VeQTNj/qdPbP1vBNPSWv znqC//8HNHSp7jswfvGzb6L5OoHQScbWBX03FzqeomV/sj655BvstU6jWbYtAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673950023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=R1tZwZ7n/7Lddf1K770W3Cu/f3NjvQ9j1WeOfz2Uaag=; b=jvLr8Aq0420YzmnCBNoCHxeIjgauHhS11PIGERypwApXfvXE+VB6z6wxNQKaEuSYcgX8PF kEIr1ivWvuwFn4RNu7NZ3dRyMEFQ5IMwgpOzqNltj8Z70+QpqTA/kolyTs1GvJ6yPXAwiC 5gBRA/DaQst1Qhb8hdMPUWodvEapdQ5HHdM8XI/bOyCsHF/7DJg+KM4q0D3gkDNSKuVZ9Z BZIfauy5gpGVlvW24HSokByC8zFrwM/9L7zQr1Ck/2jUA2brMCMP9Kc0JIbHV8h5hjC755 piTGwZ/sBqlVrDCLTQpD6ey8Y1zmnMMA4JVy1ZCiJZLcsYzwExVPCXrlv3mm2g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673950023; a=rsa-sha256; cv=none; b=m4oEndyYFeAXbRk/MbwicjXt7lY/9jow6fRY9/c1sDfElkp5SYE2TI3zxef42fm2zmU4cK BjVKxj1kIBY0gzeAONs6OiglYgWUj8P1ZB+V4WNbJwdJrGCuYypqGtze7bVoojEG8eu7pQ bSfHKXeMJ6Suu7M7dXY7rOtVzabFCB9W72nZmHm0z3dOjLdgY04RMOwUzCuO7BsmtDNvuT qUJUybfdfGqnP6WKMzGMy5D7Ejh5eLIBoBZApfVI87Jp3CcgY+YyvSABYZNKgpiGp9gyYE ZZPDaG9PpIm1DTWdsL2zd9KRCzfOZHrjgerxf++7ObuHOsiSj8nye9o5ejB5+w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Nx4LZ74S0zRkW; Tue, 17 Jan 2023 10:07:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30HA72P7034936; Tue, 17 Jan 2023 10:07:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30HA726I034935; Tue, 17 Jan 2023 10:07:02 GMT (envelope-from git) Date: Tue, 17 Jan 2023 10:07:02 GMT Message-Id: <202301171007.30HA726I034935@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Tom Jones Subject: git: cef7ab70ff44 - main - netcat: Allow nc to be an if_tun tunnel broker List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: thj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cef7ab70ff44955b97e543fd03e664c89ce05bc3 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by thj: URL: https://cgit.FreeBSD.org/src/commit/?id=cef7ab70ff44955b97e543fd03e664c89ce05bc3 commit cef7ab70ff44955b97e543fd03e664c89ce05bc3 Author: Tom Jones AuthorDate: 2023-01-17 10:02:06 +0000 Commit: Tom Jones CommitDate: 2023-01-17 10:05:49 +0000 netcat: Allow nc to be an if_tun tunnel broker Reviewed by: kevans Relnotes: yes Sponsored by: Zenarmor Sponsored by: OPNsense Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D37435 --- contrib/netcat/nc.1 | 9 ++++++++- contrib/netcat/netcat.c | 27 +++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/contrib/netcat/nc.1 b/contrib/netcat/nc.1 index 2627920cca15..9f8696135f35 100644 --- a/contrib/netcat/nc.1 +++ b/contrib/netcat/nc.1 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 10, 2020 +.Dd January 17, 2023 .Dt NC 1 .Os .Sh NAME @@ -47,6 +47,7 @@ .Op Fl p Ar source_port .Op Fl s Ar source .Op Fl T Ar toskeyword +.Op Fl -tun Ar tundev .Op Fl V Ar rtable .Op Fl w Ar timeout .Op Fl X Ar proxy_protocol @@ -240,6 +241,12 @@ to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests. This makes it possible to use .Nm to script telnet sessions. +.It Fl -tun Ar tundev +Causes +.Nm +to use the provided +.Xr tun 4 +for input and output rather than the default of stdin and stdout. .It Fl U Specifies to use .Ux Ns -domain diff --git a/contrib/netcat/netcat.c b/contrib/netcat/netcat.c index eb3c7544be76..65266b99c28e 100644 --- a/contrib/netcat/netcat.c +++ b/contrib/netcat/netcat.c @@ -112,6 +112,7 @@ int rtableid = -1; int timeout = -1; int family = AF_UNSPEC; +int tun_fd = -1; char *portlist[PORT_MAX+1]; char *unix_dg_tmp_socket; @@ -144,6 +145,10 @@ void add_ipsec_policy(int, int, char *); char *ipsec_policy[2]; #endif +enum { + FREEBSD_TUN = CHAR_MAX, /* avoid collision with return values from getopt */ +}; + int main(int argc, char *argv[]) { @@ -156,12 +161,13 @@ main(int argc, char *argv[]) socklen_t len; struct sockaddr_storage cliaddr; char *proxy; - const char *errstr, *proxyhost = "", *proxyport = NULL; + const char *errstr, *proxyhost = "", *proxyport = NULL, *tundev = NULL; struct addrinfo proxyhints; char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE]; struct option longopts[] = { { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 }, { "sctp", no_argument, &FreeBSD_sctp, 1 }, + { "tun", required_argument, NULL, FREEBSD_TUN }, { NULL, 0, NULL, 0 } }; @@ -326,6 +332,9 @@ main(int argc, char *argv[]) if (Tflag < 0 || Tflag > 255 || errstr || errno) errx(1, "illegal tos value %s", optarg); break; + case FREEBSD_TUN: + tundev = optarg; + break; case 0: /* Long option. */ break; @@ -365,6 +374,13 @@ main(int argc, char *argv[]) if (family == AF_UNIX) errx(1, "cannot use -U and --sctp"); } + if (tundev != NULL) { + if (!uflag) + errx(1, "must use --tun with -u"); + tun_fd = open(tundev, O_RDWR); + if (tun_fd == -1) + errx(1, "unable to open tun device %s", tundev); + } /* Get name of temporary socket for unix datagram client */ if ((family == AF_UNIX) && uflag && !lflag) { @@ -564,6 +580,8 @@ main(int argc, char *argv[]) if (s) close(s); + if (tun_fd != -1) + close(tun_fd); exit(ret); } @@ -840,7 +858,7 @@ readwrite(int net_fd) stdin_fd = -1; /* stdin */ - pfd[POLL_STDIN].fd = stdin_fd; + pfd[POLL_STDIN].fd = (tun_fd != -1) ? tun_fd : stdin_fd; pfd[POLL_STDIN].events = POLLIN; /* network out */ @@ -852,7 +870,7 @@ readwrite(int net_fd) pfd[POLL_NETIN].events = POLLIN; /* stdout */ - pfd[POLL_STDOUT].fd = stdout_fd; + pfd[POLL_STDOUT].fd = (tun_fd != -1) ? tun_fd : stdout_fd; pfd[POLL_STDOUT].events = 0; while (1) { @@ -1440,6 +1458,7 @@ help(void) \t-n Suppress name/port resolutions\n\ \t--no-tcpopt Disable TCP options\n\ \t--sctp\t SCTP mode\n\ + \t--tun tundev Use tun device rather than stdio\n\ \t-O length TCP send buffer length\n\ \t-P proxyuser\tUsername for proxy authentication\n\ \t-p port\t Specify local port for remote connects\n\ @@ -1500,7 +1519,7 @@ usage(int ret) #endif "\t [--no-tcpopt] [--sctp]\n" "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n" - "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" + "\t [--tun tundev] [-V rtable] [-w timeout] [-X proxy_protocol]\n" "\t [-x proxy_address[:port]] [destination] [port]\n"); if (ret) exit(1);