From nobody Fri Jan 10 20:04:19 2025 X-Original-To: dev-commits-src-main@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 4YVCKc2ccfz5kSpR; Fri, 10 Jan 2025 20:04:20 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YVCKc0WY0z4V5g; Fri, 10 Jan 2025 20:04:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736539460; 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=qqjeg4MRqNuaA1p0fXnf+AHMXKT4Jj4hSTwnRAHSI6Q=; b=Jit1BNm4Uk4go8bBF7hqXc7mO3I4ajZNyq6rX0sHZWmanV/up5ENlVKPLNzckK0VPRflnn QlmO8vVvzR4J2AcghQamDRG25iN8xy12PGBJndue3Nzm00vNj6WnsFIwgB0j+vFDtqhnrJ 7yjAUfsap7pLnyxYjH2Vcy0WLWJvsKsX7eQh7HLcZt/Y/eUk1AQMatWnNUfR604hFoad8z Kqa3gPFgOejYcmv6ctAOnrgTBlZYyA5apbAETG0vx+sVN0izyx8zrrS/B1ojUWVK+uTMFm 2MvPOqcO/TKgqHjae6lWa+DfKolW6rzQKHVJUCPTAa7xfOzr8U9DzIAd9dqLiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736539460; 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=qqjeg4MRqNuaA1p0fXnf+AHMXKT4Jj4hSTwnRAHSI6Q=; b=W9rzHOG/tlgde8uFbVWogNiODk/vwujyQ9SV00meLzqZecRgFtWFbM/ABhX//mTMAlI6t2 WtI98XWJLG2lfGoVC+i7rEJXO6vcZhBfwKliH1d9FRZX8JgBqyBjBZ7yKBOZy8hHyBL2j0 /d2VXgH/80yGMO6o4Q9EwiRu75/ARx7lsOIhl9sMIMP7QippXtiL8XeO5pgSY9JD3ys5ea 1hm/Fdst44VY4NZEBZLb8G2dvzDvRGMwFVB3TDucGroSKNyPDAEsvruA65+cFeQ6kRTAhP NzJk5hendl2LOpXvwn3zbToEeM8WpfHOIVL+j79NlD5YSPhIC6qNWF++rtP1TQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1736539460; a=rsa-sha256; cv=none; b=bn54UnYRz7upHoa73g38SnbEcUxN4vwv1Gg1vmQ9KY2tcZPr2MgRrDFGyweYgEPCXfMgcf UxIWWWpuwL7f5zlRsWTkOt7A0KuPrjFkW5r3L6OEM9rXNJd3mXhA81JJcprJYWa+TCIwjM MgjEy+yOiw6LCkpwODxIwOdaofpZK0ZnRVjg5JM6uKKuYSekjS8i7JKWNCV73weyCB3jSi kDseYKlPCcfyFpdE14smqICDA/s2coL+M0Ne4iTZbm8klZGld2OG20cBm4nGNeYMDP/4dY qd5kq0ulSH1tUPl4MQWHhKo/FcPgEOvfeHz8wAEsoIBGUQCaC0Kt3gsnj6bwzw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4YVCKc01sLzBWC; Fri, 10 Jan 2025 20:04:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 50AK4Jvb018402; Fri, 10 Jan 2025 20:04:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50AK4Jo0018399; Fri, 10 Jan 2025 20:04:19 GMT (envelope-from git) Date: Fri, 10 Jan 2025 20:04:19 GMT Message-Id: <202501102004.50AK4Jo0018399@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 9234a50752cd - main - ng_ksocket: add IPv6 support for address parsing and unparsing List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9234a50752cd47887849d4665af0f9f4abdefb5d Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=9234a50752cd47887849d4665af0f9f4abdefb5d commit 9234a50752cd47887849d4665af0f9f4abdefb5d Author: Seyed Pouria Mousavizadeh Tehrani AuthorDate: 2025-01-10 09:16:18 +0000 Commit: Gleb Smirnoff CommitDate: 2025-01-10 20:00:58 +0000 ng_ksocket: add IPv6 support for address parsing and unparsing Differential Revision: https://reviews.freebsd.org/D48204 --- share/man/man4/ng_ksocket.4 | 13 +++++-- sys/netgraph/ng_ksocket.c | 94 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 9 deletions(-) diff --git a/share/man/man4/ng_ksocket.4 b/share/man/man4/ng_ksocket.4 index 1f32d39dc7d0..bb653c3688ad 100644 --- a/share/man/man4/ng_ksocket.4 +++ b/share/man/man4/ng_ksocket.4 @@ -32,7 +32,7 @@ .\" .\" Author: Archie Cobbs .\" -.Dd January 9, 2012 +.Dd January 9, 2025 .Dt NG_KSOCKET 4 .Os .Sh NAME @@ -183,7 +183,8 @@ in the argument field, the normal equivalent of the C structure is an acceptable form. For the -.Dv PF_INET +.Dv PF_INET , +.Dv PF_INET6 and .Dv PF_LOCAL address families, a more convenient form is also used, which is @@ -191,7 +192,11 @@ the protocol family name, followed by a slash, followed by the actual address. For .Dv PF_INET , -the address is an IP address followed by an optional colon and port number. +the address is an IPv4 address followed by an optional colon and port number. +For +.Dv PF_INET6 , +the address is an IPv6 address enclosed in square brackets followed +by an optional colon and port number. For .Dv PF_LOCAL , the address is the pathname as a doubly quoted string. @@ -202,6 +207,8 @@ Examples: local/"/tmp/foo.socket" .It Dv PF_INET inet/192.168.1.1:1234 +.It Dv PF_INET6 +inet6/[2001::1]:1234 .It Other .Dv "\&{ family=16 len=16 data=[0x70 0x00 0x01 0x23] \&}" .El diff --git a/sys/netgraph/ng_ksocket.c b/sys/netgraph/ng_ksocket.c index 3e4427f9e387..43a2747224f3 100644 --- a/sys/netgraph/ng_ksocket.c +++ b/sys/netgraph/ng_ksocket.c @@ -58,6 +58,9 @@ #include #include +#include +#include + #include #include #include @@ -66,6 +69,8 @@ #include #include +#include + #ifdef NG_SEPARATE_MALLOC static MALLOC_DEFINE(M_NETGRAPH_KSOCKET, "netgraph_ksock", "netgraph ksock node"); @@ -147,6 +152,19 @@ static const struct ng_ksocket_alias ng_ksocket_protos[] = { { "swipe", IPPROTO_SWIPE, PF_INET }, { "encap", IPPROTO_ENCAP, PF_INET }, { "pim", IPPROTO_PIM, PF_INET }, + { "ip6", IPPROTO_IPV6, PF_INET6 }, + { "raw6", IPPROTO_RAW, PF_INET6 }, + { "icmp6", IPPROTO_ICMPV6, PF_INET6 }, + { "igmp6", IPPROTO_IGMP, PF_INET6 }, + { "tcp6", IPPROTO_TCP, PF_INET6 }, + { "udp6", IPPROTO_UDP, PF_INET6 }, + { "gre6", IPPROTO_GRE, PF_INET6 }, + { "esp6", IPPROTO_ESP, PF_INET6 }, + { "ah6", IPPROTO_AH, PF_INET6 }, + { "swipe6", IPPROTO_SWIPE, PF_INET6 }, + { "encap6", IPPROTO_ENCAP, PF_INET6 }, + { "divert6", IPPROTO_DIVERT, PF_INET6 }, + { "pim6", IPPROTO_PIM, PF_INET6 }, { NULL, -1 }, }; @@ -296,9 +314,58 @@ ng_ksocket_sockaddr_parse(const struct ng_parse_type *type, break; } -#if 0 - case PF_INET6: /* XXX implement this someday */ -#endif + case PF_INET6: + { + struct sockaddr_in6 *const sin6 = (struct sockaddr_in6 *)sa; + char *eptr; + char addr[INET6_ADDRSTRLEN]; + char ifname[16]; + u_long port; + bool hasifname = true; + + /* RFC 3986 Section 3.2.2, Validate IP literal within square brackets. */ + if (s[*off] == '[' && (strstr(&s[*off], "]"))) + (*off)++; + else + return (EINVAL); + if ((eptr = strstr(&s[*off], "%")) == NULL) { + hasifname = false; + eptr = strstr(&s[*off], "]"); + } + snprintf(addr, eptr - (s + *off) + 1, "%s", &s[*off]); + *off += (eptr - (s + *off)); + if (!inet_pton(AF_INET6, addr, &sin6->sin6_addr)) + return (EINVAL); + + if (hasifname) { + uint16_t scope; + + eptr = strstr(&s[*off], "]"); + (*off)++; + snprintf(ifname, eptr - (s + *off) + 1, "%s", &s[*off]); + *off += (eptr - (s + *off)); + + if (sin6->sin6_addr.s6_addr16[0] != IPV6_ADDR_INT16_ULL) + return (EINVAL); + scope = in6_getscope(&sin6->sin6_addr); + sin6->sin6_scope_id = + in6_getscopezone(ifunit(ifname), scope); + } + + (*off)++; + if (s[*off] == ':') { + (*off)++; + port = strtoul(s + *off, &eptr, 10); + if (port > 0xffff || eptr == s + *off) + return (EINVAL); + *off += (eptr - (s + *off)); + sin6->sin6_port = htons(port); + } else + sin6->sin6_port = 0; + + sin6->sin6_len = sizeof(*sin6); + break; + } default: return (EINVAL); @@ -358,9 +425,24 @@ ng_ksocket_sockaddr_unparse(const struct ng_parse_type *type, return(0); } -#if 0 - case PF_INET6: /* XXX implement this someday */ -#endif + case PF_INET6: + { + const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa; + char addr[INET6_ADDRSTRLEN]; + + inet_ntop(AF_INET6, &sin6->sin6_addr, addr, INET6_ADDRSTRLEN); + slen += snprintf(cbuf, cbuflen, "inet6/[%s]", addr); + + if (sin6->sin6_port != 0) { + slen += snprintf(cbuf + strlen(cbuf), + cbuflen - strlen(cbuf), ":%d", + (u_int)ntohs(sin6->sin6_port)); + } + if (slen >= cbuflen) + return (ERANGE); + *off += sizeof(*sin6); + return(0); + } default: return (*ng_ksocket_generic_sockaddr_type.supertype->unparse)