From owner-dev-commits-src-main@freebsd.org Sat Jan 16 23:45:37 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id AC9574D00B1; Sat, 16 Jan 2021 23:45:37 +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 4DJF6T4TNLz3KRZ; Sat, 16 Jan 2021 23:45:37 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 8C80E179E3; Sat, 16 Jan 2021 23:45:37 +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 10GNjbr4018693; Sat, 16 Jan 2021 23:45:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10GNjbUb018692; Sat, 16 Jan 2021 23:45:37 GMT (envelope-from git) Date: Sat, 16 Jan 2021 23:45:37 GMT Message-Id: <202101162345.10GNjbUb018692@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: d28210b2c2aa - main - Remove remnants of classful behavior in route(8). MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d28210b2c2aaf3200907ed30d296b0d4856dd03c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jan 2021 23:45:37 -0000 The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=d28210b2c2aaf3200907ed30d296b0d4856dd03c commit d28210b2c2aaf3200907ed30d296b0d4856dd03c Author: Alexander V. Chernikov AuthorDate: 2021-01-16 23:06:32 +0000 Commit: Alexander V. Chernikov CommitDate: 2021-01-16 23:45:30 +0000 Remove remnants of classful behavior in route(8). Curently route(8) treats some addresses as network addresses: RTA_DST: inet 10.0.0.0; RTA_NETMASK: inet 255.0.0.0; RTA_IFP: link ; RTM_GET: Report Metrics: len 240, pid: 0, seq 1, errno 0, flags: locks: inits: sockaddrs: 10.0.0.0 255.0.0.0 link#0 Note added `RTA_NETMASK` in the request. Host address from the same network is ok: route -nv get 10.0.0.1 RTA_DST: inet 10.0.0.1 RTA_IFP: link RTM_GET: Report Metrics: len 224, pid: 0, seq 1, errno 0, flags: ... route to: 10.0.0.1 destination: 10.0.0.0 mask: 255.255.255.0 This change eliminates network auto-guessing part AND reading network from /etc/networks. Reviewed By: rgrimes Differential Revision: https://reviews.freebsd.org/D24401 --- sbin/route/route.c | 69 +++++++++++------------------------------------------- 1 file changed, 14 insertions(+), 55 deletions(-) diff --git a/sbin/route/route.c b/sbin/route/route.c index 9c9e4b304848..51a0c68746a6 100644 --- a/sbin/route/route.c +++ b/sbin/route/route.c @@ -120,8 +120,7 @@ static int flushroutes_fib(int); static int getaddr(int, char *, struct hostent **, int); static int keyword(const char *); #ifdef INET -static void inet_makenetandmask(u_long, struct sockaddr_in *, - struct sockaddr_in *, u_long); +static void inet_makemask(struct sockaddr_in *, u_long); #endif #ifdef INET6 static int inet6_makenetandmask(struct sockaddr_in6 *, const char *); @@ -1113,40 +1112,15 @@ newroute_fib(int fib, char *cmd, int flags) #ifdef INET static void -inet_makenetandmask(u_long net, struct sockaddr_in *sin, - struct sockaddr_in *sin_mask, u_long bits) +inet_makemask(struct sockaddr_in *sin_mask, u_long bits) { u_long mask = 0; rtm_addrs |= RTA_NETMASK; - /* - * MSB of net should be meaningful. 0/0 is exception. - */ - if (net > 0) - while ((net & 0xff000000) == 0) - net <<= 8; - - /* - * If no /xx was specified we must calculate the - * CIDR address. - */ - if ((bits == 0) && (net != 0)) { - u_long i, j; - - for(i = 0, j = 0xff; i < 4; i++) { - if (net & j) { - break; - } - j <<= 8; - } - /* i holds the first non zero bit */ - bits = 32 - (i*8); - } if (bits != 0) mask = 0xffffffff << (32 - bits); - sin->sin_addr.s_addr = htonl(net); sin_mask->sin_addr.s_addr = htonl(mask); sin_mask->sin_len = sizeof(struct sockaddr_in); sin_mask->sin_family = AF_INET; @@ -1186,8 +1160,6 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags) #if defined(INET) struct sockaddr_in *sin; struct hostent *hp; - struct netent *np; - u_long val; char *q; #elif defined(INET6) char *q; @@ -1314,34 +1286,21 @@ getaddr(int idx, char *str, struct hostent **hpp, int nrflags) q = strchr(str,'/'); if (q != NULL && idx == RTAX_DST) { + /* A.B.C.D/NUM */ *q = '\0'; - if ((val = inet_network(str)) != INADDR_NONE) { - inet_makenetandmask(val, sin, - (struct sockaddr_in *)&so[RTAX_NETMASK], - strtoul(q+1, 0, 0)); - return (0); - } - *q = '/'; - } - if ((idx != RTAX_DST || (nrflags & F_FORCENET) == 0) && - inet_aton(str, &sin->sin_addr)) { - val = sin->sin_addr.s_addr; - if (idx != RTAX_DST || nrflags & F_FORCEHOST || - inet_lnaof(sin->sin_addr) != INADDR_ANY) - return (1); - else { - val = ntohl(val); - goto netdone; - } - } - if (idx == RTAX_DST && (nrflags & F_FORCEHOST) == 0 && - ((val = inet_network(str)) != INADDR_NONE || - ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) { -netdone: - inet_makenetandmask(val, sin, - (struct sockaddr_in *)&so[RTAX_NETMASK], 0); + if (inet_aton(str, &sin->sin_addr) == 0) + errx(EX_NOHOST, "bad address: %s", str); + + int masklen = strtol(q + 1, NULL, 10); + if (masklen < 0 || masklen > 32) + errx(EX_NOHOST, "bad mask length: %s", q + 1); + + inet_makemask((struct sockaddr_in *)&so[RTAX_NETMASK],masklen); return (0); } + if (inet_aton(str, &sin->sin_addr) != 0) + return (1); + hp = gethostbyname(str); if (hp != NULL) { *hpp = hp;