Opened 9 days ago

Closed 9 days ago

#2413 closed defect (duplicate)

func ngx_parse_inet6_url() can not set ipv6 connection local address

Reported by: Shuokp31@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.22.x
Keywords: ipv6, addr Cc:
uname -a: Linux iBMC 5.10.0 #1 SMP PREEMPT(OFF) Thu Oct 27 14:14:59 CST 2022 armv7l GNU/Linux
nginx -V: nginx version: nginx/1.22.0
built by gcc 7.3.0 (GCC)
built with OpenSSL 1.1.1n 15 Mar 2022
TLS SNI support enabled

configure arguments: --with-cc=aarch64-target-linux-gnu-gcc --with-cpp=aarch64-target-linux-gnu-g++ --prefix="/opt/pme/web" --with-http_auth_request_module --with-http_ssl_module --with-ipv6 --without-http_gzip_module --without-http_upstream_zone_module --without-select_module --without-http_gzip_module --without-http_split_clients_module --without-http_fastcgi_module --without-http_uwsgi_module --without-http_scgi_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-stream_split_clients_module --add-module=../../nginx-upload-module --with-ld-opt="-Wl,-z,relro,-z,now -lcap" --with-cc-opt="-Wl,-z,relro,-z,now -fstack-protector-strong -DNGX_SYS_NERR=132 -DNGX_HAVE_SYSVSHM=1 -DKVM_USER_UPLOAD -DBMC_ENABLED -DBMC_ONLY=1"

Description

We found a problem that nginx cant set ipv6 connection's local address, pls consider the following modify in src/core/ngx_inet.c:

ngx_parse_inet6_url(ngx_pool_t *pool, ngx_url_t *u)
{
……

    if (len == 0) {
        u->err = "no host";
        return NGX_ERROR;
    }
 
    u->host.len = len + 2;
    u->host.data = host - 1;
 
+    ifname = ngx_strlchr(host, p, '%');
+    if (ifname != NULL) {
+        len = ifname - host;
+        ngx_memcpy((void *)name, (void *)(ifname + 1), p - ifname - 1);
+    }
 
    if (ngx_inet6_addr(host, len, sin6->sin6_addr.s6_addr) != NGX_OK) {
        u->err = "invalid IPv6 address";
        return NGX_ERROR;
    }
 
+    if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && ifname) {
+        index = if_nametoindex(name);
+        if (index) {
+            sin6->sin6_scope_id = index;
+        }
+    }
 
    if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
        u->wildcard = 1;
    }
 
    u->family = AF_INET6;
 
    return ngx_inet_add_addr(pool, u, &u->sockaddr.sockaddr, u->socklen, 1);
……
}

Any discussion and suggestions are welcome.

Change History (1)

comment:1 by Maxim Dounin, 9 days ago

Resolution: duplicate
Status: newclosed

Duplicate of #623.

Note: See TracTickets for help on using tickets.