Add inet6_{test|set|clear|assign}_bit() helpers.
Note that I am using bits from inet->inet_flags,
this might change in the future if we need more flags.
While solving data-races accessing np->mc_loop,
this patch also allows to implement lockless accesses
to np->mcast_hops in the following patch.
Also constify sk_mc_loop() argument.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
 #if defined(__BIG_ENDIAN_BITFIELD)
        /* Packed in 16bits. */
        __s16                   mcast_hops:9;
-       __u16                   __unused_2:6,
-                               mc_loop:1;
+       __u16                   __unused_2:7,
 #else
-       __u16                   mc_loop:1,
-                               __unused_2:6;
+       __u16                   __unused_2:7;
        __s16                   mcast_hops:9;
 #endif
        int                     ucast_oif;
        struct inet6_cork       cork;
 };
 
+/* We currently use available bits from inet_sk(sk)->inet_flags,
+ * this could change in the future.
+ */
+#define inet6_test_bit(nr, sk)                 \
+       test_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
+#define inet6_set_bit(nr, sk)                  \
+       set_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
+#define inet6_clear_bit(nr, sk)                        \
+       clear_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
+#define inet6_assign_bit(nr, sk, val)          \
+       assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val)
+
 /* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
 struct raw6_sock {
        /* inet_sock has to be the first member of raw6_sock */
 
        INET_FLAGS_NODEFRAG     = 17,
        INET_FLAGS_BIND_ADDRESS_NO_PORT = 18,
        INET_FLAGS_DEFER_CONNECT = 19,
+       INET_FLAGS_MC6_LOOP     = 20,
 };
 
 /* cmsg flags for inet */
 
        }
 }
 
-bool sk_mc_loop(struct sock *sk);
+bool sk_mc_loop(const struct sock *sk);
 
 static inline bool sk_can_gso(const struct sock *sk)
 {
 
        return ret;
 }
 
-bool sk_mc_loop(struct sock *sk)
+bool sk_mc_loop(const struct sock *sk)
 {
        if (dev_recursion_level())
                return false;
                return inet_test_bit(MC_LOOP, sk);
 #if IS_ENABLED(CONFIG_IPV6)
        case AF_INET6:
-               return inet6_sk(sk)->mc_loop;
+               return inet6_test_bit(MC6_LOOP, sk);
 #endif
        }
        WARN_ON_ONCE(1);
 
        inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
        np->hop_limit   = -1;
        np->mcast_hops  = IPV6_DEFAULT_MCASTHOPS;
-       np->mc_loop     = 1;
+       inet6_set_bit(MC6_LOOP, sk);
        np->mc_all      = 1;
        np->pmtudisc    = IPV6_PMTUDISC_WANT;
        np->repflow     = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
 
                        return -EINVAL;
                WRITE_ONCE(np->hop_limit, val);
                return 0;
+       case IPV6_MULTICAST_LOOP:
+               if (optlen < sizeof(int))
+                       return -EINVAL;
+               if (val != valbool)
+                       return -EINVAL;
+               inet6_assign_bit(MC6_LOOP, sk, valbool);
+               return 0;
        }
        if (needs_rtnl)
                rtnl_lock();
                retv = 0;
                break;
 
-       case IPV6_MULTICAST_LOOP:
-               if (optlen < sizeof(int))
-                       goto e_inval;
-               if (val != valbool)
-                       goto e_inval;
-               np->mc_loop = valbool;
-               retv = 0;
-               break;
-
        case IPV6_UNICAST_IF:
        {
                struct net_device *dev = NULL;
        }
 
        case IPV6_MULTICAST_LOOP:
-               val = np->mc_loop;
+               val = inet6_test_bit(MC6_LOOP, sk);
                break;
 
        case IPV6_MULTICAST_IF:
 
        np = inet6_sk(sk);
        np->hop_limit = 255;
        /* Do not loopback ndisc messages */
-       np->mc_loop = 0;
+       inet6_clear_bit(MC6_LOOP, sk);
 
        return 0;
 }
 
 static void set_mcast_loop(struct sock *sk, u_char loop)
 {
        /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
-       lock_sock(sk);
        inet_assign_bit(MC_LOOP, sk, loop);
 #ifdef CONFIG_IP_VS_IPV6
-       if (sk->sk_family == AF_INET6) {
-               struct ipv6_pinfo *np = inet6_sk(sk);
-
+       if (READ_ONCE(sk->sk_family) == AF_INET6) {
                /* IPV6_MULTICAST_LOOP */
-               np->mc_loop = loop ? 1 : 0;
+               inet6_assign_bit(MC6_LOOP, sk, loop);
        }
 #endif
-       release_sock(sk);
 }
 
 /*