]> Pileus Git - ~andy/linux/blobdiff - net/ipv4/tcp_metrics.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / net / ipv4 / tcp_metrics.c
index 098b3a29f6f3a082af32af35d703bf884922ec77..fa950941de65c811897f24b6fe64f71be413940f 100644 (file)
@@ -22,7 +22,8 @@
 
 int sysctl_tcp_nometrics_save __read_mostly;
 
-static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
+static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
+                                                  const struct inetpeer_addr *daddr,
                                                   struct net *net, unsigned int hash);
 
 struct tcp_fastopen_metrics {
@@ -34,7 +35,8 @@ struct tcp_fastopen_metrics {
 
 struct tcp_metrics_block {
        struct tcp_metrics_block __rcu  *tcpm_next;
-       struct inetpeer_addr            tcpm_addr;
+       struct inetpeer_addr            tcpm_saddr;
+       struct inetpeer_addr            tcpm_daddr;
        unsigned long                   tcpm_stamp;
        u32                             tcpm_ts;
        u32                             tcpm_ts_stamp;
@@ -145,7 +147,8 @@ static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst
 #define TCP_METRICS_RECLAIM_PTR                (struct tcp_metrics_block *) 0x1UL
 
 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
-                                         struct inetpeer_addr *addr,
+                                         struct inetpeer_addr *saddr,
+                                         struct inetpeer_addr *daddr,
                                          unsigned int hash)
 {
        struct tcp_metrics_block *tm;
@@ -158,7 +161,7 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
        /* While waiting for the spin-lock the cache might have been populated
         * with this entry and so we have to check again.
         */
-       tm = __tcp_get_metrics(addr, net, hash);
+       tm = __tcp_get_metrics(saddr, daddr, net, hash);
        if (tm == TCP_METRICS_RECLAIM_PTR) {
                reclaim = true;
                tm = NULL;
@@ -183,7 +186,8 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
                if (!tm)
                        goto out_unlock;
        }
-       tm->tcpm_addr = *addr;
+       tm->tcpm_saddr = *saddr;
+       tm->tcpm_daddr = *daddr;
 
        tcpm_suck_dst(tm, dst, true);
 
@@ -206,7 +210,8 @@ static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, in
        return NULL;
 }
 
-static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
+static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
+                                                  const struct inetpeer_addr *daddr,
                                                   struct net *net, unsigned int hash)
 {
        struct tcp_metrics_block *tm;
@@ -214,7 +219,8 @@ static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *a
 
        for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
             tm = rcu_dereference(tm->tcpm_next)) {
-               if (addr_same(&tm->tcpm_addr, addr))
+               if (addr_same(&tm->tcpm_saddr, saddr) &&
+                   addr_same(&tm->tcpm_daddr, daddr))
                        break;
                depth++;
        }
@@ -225,19 +231,22 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
                                                       struct dst_entry *dst)
 {
        struct tcp_metrics_block *tm;
-       struct inetpeer_addr addr;
+       struct inetpeer_addr saddr, daddr;
        unsigned int hash;
        struct net *net;
 
-       addr.family = req->rsk_ops->family;
-       switch (addr.family) {
+       saddr.family = req->rsk_ops->family;
+       daddr.family = req->rsk_ops->family;
+       switch (daddr.family) {
        case AF_INET:
-               addr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
-               hash = (__force unsigned int) addr.addr.a4;
+               saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
+               daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
+               hash = (__force unsigned int) daddr.addr.a4;
                break;
 #if IS_ENABLED(CONFIG_IPV6)
        case AF_INET6:
-               *(struct in6_addr *)addr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
+               *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
+               *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
                hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
                break;
 #endif
@@ -250,7 +259,8 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
 
        for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
             tm = rcu_dereference(tm->tcpm_next)) {
-               if (addr_same(&tm->tcpm_addr, &addr))
+               if (addr_same(&tm->tcpm_saddr, &saddr) &&
+                   addr_same(&tm->tcpm_daddr, &daddr))
                        break;
        }
        tcpm_check_stamp(tm, dst);
@@ -260,19 +270,22 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
 {
        struct tcp_metrics_block *tm;
-       struct inetpeer_addr addr;
+       struct inetpeer_addr saddr, daddr;
        unsigned int hash;
        struct net *net;
 
-       addr.family = tw->tw_family;
-       switch (addr.family) {
+       saddr.family = tw->tw_family;
+       daddr.family = tw->tw_family;
+       switch (daddr.family) {
        case AF_INET:
-               addr.addr.a4 = tw->tw_daddr;
-               hash = (__force unsigned int) addr.addr.a4;
+               saddr.addr.a4 = tw->tw_rcv_saddr;
+               daddr.addr.a4 = tw->tw_daddr;
+               hash = (__force unsigned int) daddr.addr.a4;
                break;
 #if IS_ENABLED(CONFIG_IPV6)
        case AF_INET6:
-               *(struct in6_addr *)addr.addr.a6 = tw->tw_v6_daddr;
+               *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
+               *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
                hash = ipv6_addr_hash(&tw->tw_v6_daddr);
                break;
 #endif
@@ -285,7 +298,8 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
 
        for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
             tm = rcu_dereference(tm->tcpm_next)) {
-               if (addr_same(&tm->tcpm_addr, &addr))
+               if (addr_same(&tm->tcpm_saddr, &saddr) &&
+                   addr_same(&tm->tcpm_daddr, &daddr))
                        break;
        }
        return tm;
@@ -296,19 +310,22 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
                                                 bool create)
 {
        struct tcp_metrics_block *tm;
-       struct inetpeer_addr addr;
+       struct inetpeer_addr saddr, daddr;
        unsigned int hash;
        struct net *net;
 
-       addr.family = sk->sk_family;
-       switch (addr.family) {
+       saddr.family = sk->sk_family;
+       daddr.family = sk->sk_family;
+       switch (daddr.family) {
        case AF_INET:
-               addr.addr.a4 = inet_sk(sk)->inet_daddr;
-               hash = (__force unsigned int) addr.addr.a4;
+               saddr.addr.a4 = inet_sk(sk)->inet_saddr;
+               daddr.addr.a4 = inet_sk(sk)->inet_daddr;
+               hash = (__force unsigned int) daddr.addr.a4;
                break;
 #if IS_ENABLED(CONFIG_IPV6)
        case AF_INET6:
-               *(struct in6_addr *)addr.addr.a6 = sk->sk_v6_daddr;
+               *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
+               *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
                hash = ipv6_addr_hash(&sk->sk_v6_daddr);
                break;
 #endif
@@ -319,11 +336,11 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
        net = dev_net(dst->dev);
        hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
 
-       tm = __tcp_get_metrics(&addr, net, hash);
+       tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
        if (tm == TCP_METRICS_RECLAIM_PTR)
                tm = NULL;
        if (!tm && create)
-               tm = tcpm_new(dst, &addr, hash);
+               tm = tcpm_new(dst, &saddr, &daddr, hash);
        else
                tcpm_check_stamp(tm, dst);
 
@@ -737,15 +754,21 @@ static int tcp_metrics_fill_info(struct sk_buff *msg,
        struct nlattr *nest;
        int i;
 
-       switch (tm->tcpm_addr.family) {
+       switch (tm->tcpm_daddr.family) {
        case AF_INET:
                if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
-                               tm->tcpm_addr.addr.a4) < 0)
+                               tm->tcpm_daddr.addr.a4) < 0)
+                       goto nla_put_failure;
+               if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4,
+                               tm->tcpm_saddr.addr.a4) < 0)
                        goto nla_put_failure;
                break;
        case AF_INET6:
                if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
-                           tm->tcpm_addr.addr.a6) < 0)
+                           tm->tcpm_daddr.addr.a6) < 0)
+                       goto nla_put_failure;
+               if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16,
+                           tm->tcpm_saddr.addr.a6) < 0)
                        goto nla_put_failure;
                break;
        default:
@@ -868,44 +891,66 @@ done:
        return skb->len;
 }
 
-static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
-                        unsigned int *hash, int optional)
+static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
+                          unsigned int *hash, int optional, int v4, int v6)
 {
        struct nlattr *a;
 
-       a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
+       a = info->attrs[v4];
        if (a) {
                addr->family = AF_INET;
                addr->addr.a4 = nla_get_be32(a);
-               *hash = (__force unsigned int) addr->addr.a4;
+               if (hash)
+                       *hash = (__force unsigned int) addr->addr.a4;
                return 0;
        }
-       a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
+       a = info->attrs[v6];
        if (a) {
                if (nla_len(a) != sizeof(struct in6_addr))
                        return -EINVAL;
                addr->family = AF_INET6;
                memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
-               *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
+               if (hash)
+                       *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
                return 0;
        }
        return optional ? 1 : -EAFNOSUPPORT;
 }
 
+static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
+                        unsigned int *hash, int optional)
+{
+       return __parse_nl_addr(info, addr, hash, optional,
+                              TCP_METRICS_ATTR_ADDR_IPV4,
+                              TCP_METRICS_ATTR_ADDR_IPV6);
+}
+
+static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
+{
+       return __parse_nl_addr(info, addr, NULL, 0,
+                              TCP_METRICS_ATTR_SADDR_IPV4,
+                              TCP_METRICS_ATTR_SADDR_IPV6);
+}
+
 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
 {
        struct tcp_metrics_block *tm;
-       struct inetpeer_addr addr;
+       struct inetpeer_addr saddr, daddr;
        unsigned int hash;
        struct sk_buff *msg;
        struct net *net = genl_info_net(info);
        void *reply;
        int ret;
+       bool src = true;
 
-       ret = parse_nl_addr(info, &addr, &hash, 0);
+       ret = parse_nl_addr(info, &daddr, &hash, 0);
        if (ret < 0)
                return ret;
 
+       ret = parse_nl_saddr(info, &saddr);
+       if (ret < 0)
+               src = false;
+
        msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
        if (!msg)
                return -ENOMEM;
@@ -920,7 +965,8 @@ static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
        rcu_read_lock();
        for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
             tm = rcu_dereference(tm->tcpm_next)) {
-               if (addr_same(&tm->tcpm_addr, &addr)) {
+               if (addr_same(&tm->tcpm_daddr, &daddr) &&
+                   (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
                        ret = tcp_metrics_fill_info(msg, tm);
                        break;
                }
@@ -973,34 +1019,44 @@ static int tcp_metrics_flush_all(struct net *net)
 static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
 {
        struct tcpm_hash_bucket *hb;
-       struct tcp_metrics_block *tm;
+       struct tcp_metrics_block *tm, *tmlist = NULL;
        struct tcp_metrics_block __rcu **pp;
-       struct inetpeer_addr addr;
+       struct inetpeer_addr saddr, daddr;
        unsigned int hash;
        struct net *net = genl_info_net(info);
        int ret;
+       bool src = true;
 
-       ret = parse_nl_addr(info, &addr, &hash, 1);
+       ret = parse_nl_addr(info, &daddr, &hash, 1);
        if (ret < 0)
                return ret;
        if (ret > 0)
                return tcp_metrics_flush_all(net);
+       ret = parse_nl_saddr(info, &saddr);
+       if (ret < 0)
+               src = false;
 
        hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
        hb = net->ipv4.tcp_metrics_hash + hash;
        pp = &hb->chain;
        spin_lock_bh(&tcp_metrics_lock);
-       for (tm = deref_locked_genl(*pp); tm;
-            pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
-               if (addr_same(&tm->tcpm_addr, &addr)) {
+       for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) {
+               if (addr_same(&tm->tcpm_daddr, &daddr) &&
+                   (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
                        *pp = tm->tcpm_next;
-                       break;
+                       tm->tcpm_next = tmlist;
+                       tmlist = tm;
+               } else {
+                       pp = &tm->tcpm_next;
                }
        }
        spin_unlock_bh(&tcp_metrics_lock);
-       if (!tm)
+       if (!tmlist)
                return -ESRCH;
-       kfree_rcu(tm, rcu_head);
+       for (tm = tmlist; tm; tm = tmlist) {
+               tmlist = tm->tcpm_next;
+               kfree_rcu(tm, rcu_head);
+       }
        return 0;
 }