OSDN Git Service

net: add helpers checking if socket can be bound to nonlocal address
authorVincent Bernat <vincent@bernat.im>
Tue, 31 Jul 2018 19:18:11 +0000 (21:18 +0200)
committerDavid S. Miller <davem@davemloft.net>
Wed, 1 Aug 2018 16:50:04 +0000 (09:50 -0700)
The construction "net->ipv4.sysctl_ip_nonlocal_bind || inet->freebind
|| inet->transparent" is present three times and its IPv6 counterpart
is also present three times. We introduce two small helpers to
characterize these tests uniformly.

Signed-off-by: Vincent Bernat <vincent@bernat.im>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/inet_sock.h
include/net/ipv6.h
net/ipv4/af_inet.c
net/ipv4/ping.c
net/ipv6/af_inet6.c
net/ipv6/datagram.c

index 314be48..e03b933 100644 (file)
@@ -359,4 +359,12 @@ static inline bool inet_get_convert_csum(struct sock *sk)
        return !!inet_sk(sk)->convert_csum;
 }
 
+
+static inline bool inet_can_nonlocal_bind(struct net *net,
+                                         struct inet_sock *inet)
+{
+       return net->ipv4.sysctl_ip_nonlocal_bind ||
+               inet->freebind || inet->transparent;
+}
+
 #endif /* _INET_SOCK_H */
index a44509f..82deb68 100644 (file)
@@ -766,6 +766,13 @@ static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6,
        return hlimit;
 }
 
+static inline bool ipv6_can_nonlocal_bind(struct net *net,
+                                         struct inet_sock *inet)
+{
+       return net->ipv6.sysctl.ip_nonlocal_bind ||
+               inet->freebind || inet->transparent;
+}
+
 /* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store
  * Equivalent to :     flow->v6addrs.src = iph->saddr;
  *                     flow->v6addrs.dst = iph->daddr;
index f2a0a3b..ee707b9 100644 (file)
@@ -486,8 +486,7 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
         *  is temporarily down)
         */
        err = -EADDRNOTAVAIL;
-       if (!net->ipv4.sysctl_ip_nonlocal_bind &&
-           !(inet->freebind || inet->transparent) &&
+       if (!inet_can_nonlocal_bind(net, inet) &&
            addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
            chk_addr_ret != RTN_LOCAL &&
            chk_addr_ret != RTN_MULTICAST &&
index b54c964..8d7aaf1 100644 (file)
@@ -320,8 +320,7 @@ static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
                if (addr->sin_addr.s_addr == htonl(INADDR_ANY))
                        chk_addr_ret = RTN_LOCAL;
 
-               if ((net->ipv4.sysctl_ip_nonlocal_bind == 0 &&
-                   isk->freebind == 0 && isk->transparent == 0 &&
+               if ((!inet_can_nonlocal_bind(net, isk) &&
                     chk_addr_ret != RTN_LOCAL) ||
                    chk_addr_ret == RTN_MULTICAST ||
                    chk_addr_ret == RTN_BROADCAST)
@@ -361,8 +360,7 @@ static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
                                                    scoped);
                rcu_read_unlock();
 
-               if (!(net->ipv6.sysctl.ip_nonlocal_bind ||
-                     isk->freebind || isk->transparent || has_addr ||
+               if (!(ipv6_can_nonlocal_bind(net, isk) || has_addr ||
                      addr_type == IPV6_ADDR_ANY))
                        return -EADDRNOTAVAIL;
 
index c953535..020f6e1 100644 (file)
@@ -322,8 +322,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
                /* Reproduce AF_INET checks to make the bindings consistent */
                v4addr = addr->sin6_addr.s6_addr32[3];
                chk_addr_ret = inet_addr_type(net, v4addr);
-               if (!net->ipv4.sysctl_ip_nonlocal_bind &&
-                   !(inet->freebind || inet->transparent) &&
+               if (!inet_can_nonlocal_bind(net, inet) &&
                    v4addr != htonl(INADDR_ANY) &&
                    chk_addr_ret != RTN_LOCAL &&
                    chk_addr_ret != RTN_MULTICAST &&
@@ -362,8 +361,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
                         */
                        v4addr = LOOPBACK4_IPV6;
                        if (!(addr_type & IPV6_ADDR_MULTICAST)) {
-                               if (!net->ipv6.sysctl.ip_nonlocal_bind &&
-                                   !(inet->freebind || inet->transparent) &&
+                               if (!ipv6_can_nonlocal_bind(net, inet) &&
                                    !ipv6_chk_addr(net, &addr->sin6_addr,
                                                   dev, 0)) {
                                        err = -EADDRNOTAVAIL;
index f0264df..1ede7a1 100644 (file)
@@ -803,8 +803,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
 
                        if (addr_type != IPV6_ADDR_ANY) {
                                int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
-                               if (!(net->ipv6.sysctl.ip_nonlocal_bind ||
-                                     inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
+                               if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) &&
                                    !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr,
                                                             dev, !strict, 0,
                                                             IFA_F_TENTATIVE) &&