LCOV - code coverage report
Current view: top level - net/core - sock_diag.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 97 0.0 %
Date: 2014-04-07 Functions: 0 15 0.0 %
Branches: 0 43 0.0 %

           Branch data     Line data    Source code
       1                 :            : #include <linux/mutex.h>
       2                 :            : #include <linux/socket.h>
       3                 :            : #include <linux/skbuff.h>
       4                 :            : #include <net/netlink.h>
       5                 :            : #include <net/net_namespace.h>
       6                 :            : #include <linux/module.h>
       7                 :            : #include <net/sock.h>
       8                 :            : 
       9                 :            : #include <linux/inet_diag.h>
      10                 :            : #include <linux/sock_diag.h>
      11                 :            : 
      12                 :            : static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
      13                 :            : static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
      14                 :            : static DEFINE_MUTEX(sock_diag_table_mutex);
      15                 :            : 
      16                 :          0 : int sock_diag_check_cookie(void *sk, __u32 *cookie)
      17                 :            : {
      18 [ #  # ][ #  # ]:          0 :         if ((cookie[0] != INET_DIAG_NOCOOKIE ||
      19         [ #  # ]:          0 :              cookie[1] != INET_DIAG_NOCOOKIE) &&
      20         [ #  # ]:          0 :             ((u32)(unsigned long)sk != cookie[0] ||
      21                 :          0 :              (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
      22                 :            :                 return -ESTALE;
      23                 :            :         else
      24                 :          0 :                 return 0;
      25                 :            : }
      26                 :            : EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
      27                 :            : 
      28                 :          0 : void sock_diag_save_cookie(void *sk, __u32 *cookie)
      29                 :            : {
      30                 :          0 :         cookie[0] = (u32)(unsigned long)sk;
      31                 :          0 :         cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
      32                 :          0 : }
      33                 :            : EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
      34                 :            : 
      35                 :          0 : int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
      36                 :            : {
      37                 :            :         u32 mem[SK_MEMINFO_VARS];
      38                 :            : 
      39                 :          0 :         mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
      40                 :          0 :         mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
      41                 :          0 :         mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
      42                 :          0 :         mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
      43                 :          0 :         mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
      44                 :          0 :         mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
      45                 :          0 :         mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
      46                 :          0 :         mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
      47                 :            : 
      48                 :          0 :         return nla_put(skb, attrtype, sizeof(mem), &mem);
      49                 :            : }
      50                 :            : EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
      51                 :            : 
      52                 :          0 : int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
      53                 :            :                              struct sk_buff *skb, int attrtype)
      54                 :            : {
      55                 :            :         struct nlattr *attr;
      56                 :            :         struct sk_filter *filter;
      57                 :            :         unsigned int len;
      58                 :            :         int err = 0;
      59                 :            : 
      60         [ #  # ]:          0 :         if (!ns_capable(user_ns, CAP_NET_ADMIN)) {
      61                 :          0 :                 nla_reserve(skb, attrtype, 0);
      62                 :          0 :                 return 0;
      63                 :            :         }
      64                 :            : 
      65                 :            :         rcu_read_lock();
      66                 :            : 
      67                 :          0 :         filter = rcu_dereference(sk->sk_filter);
      68         [ #  # ]:          0 :         len = filter ? filter->len * sizeof(struct sock_filter) : 0;
      69                 :            : 
      70                 :          0 :         attr = nla_reserve(skb, attrtype, len);
      71         [ #  # ]:          0 :         if (attr == NULL) {
      72                 :            :                 err = -EMSGSIZE;
      73                 :            :                 goto out;
      74                 :            :         }
      75                 :            : 
      76         [ #  # ]:          0 :         if (filter) {
      77                 :          0 :                 struct sock_filter *fb = (struct sock_filter *)nla_data(attr);
      78                 :            :                 int i;
      79                 :            : 
      80         [ #  # ]:          0 :                 for (i = 0; i < filter->len; i++, fb++)
      81                 :          0 :                         sk_decode_filter(&filter->insns[i], fb);
      82                 :            :         }
      83                 :            : 
      84                 :            : out:
      85                 :            :         rcu_read_unlock();
      86                 :          0 :         return err;
      87                 :            : }
      88                 :            : EXPORT_SYMBOL(sock_diag_put_filterinfo);
      89                 :            : 
      90                 :          0 : void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
      91                 :            : {
      92                 :          0 :         mutex_lock(&sock_diag_table_mutex);
      93                 :          0 :         inet_rcv_compat = fn;
      94                 :          0 :         mutex_unlock(&sock_diag_table_mutex);
      95                 :          0 : }
      96                 :            : EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
      97                 :            : 
      98                 :          0 : void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
      99                 :            : {
     100                 :          0 :         mutex_lock(&sock_diag_table_mutex);
     101                 :          0 :         inet_rcv_compat = NULL;
     102                 :          0 :         mutex_unlock(&sock_diag_table_mutex);
     103                 :          0 : }
     104                 :            : EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
     105                 :            : 
     106                 :          0 : int sock_diag_register(const struct sock_diag_handler *hndl)
     107                 :            : {
     108                 :            :         int err = 0;
     109                 :            : 
     110         [ #  # ]:          0 :         if (hndl->family >= AF_MAX)
     111                 :            :                 return -EINVAL;
     112                 :            : 
     113                 :          0 :         mutex_lock(&sock_diag_table_mutex);
     114         [ #  # ]:          0 :         if (sock_diag_handlers[hndl->family])
     115                 :            :                 err = -EBUSY;
     116                 :            :         else
     117                 :          0 :                 sock_diag_handlers[hndl->family] = hndl;
     118                 :          0 :         mutex_unlock(&sock_diag_table_mutex);
     119                 :            : 
     120                 :          0 :         return err;
     121                 :            : }
     122                 :            : EXPORT_SYMBOL_GPL(sock_diag_register);
     123                 :            : 
     124                 :          0 : void sock_diag_unregister(const struct sock_diag_handler *hnld)
     125                 :            : {
     126                 :          0 :         int family = hnld->family;
     127                 :            : 
     128         [ #  # ]:          0 :         if (family >= AF_MAX)
     129                 :          0 :                 return;
     130                 :            : 
     131                 :          0 :         mutex_lock(&sock_diag_table_mutex);
     132         [ #  # ]:          0 :         BUG_ON(sock_diag_handlers[family] != hnld);
     133                 :          0 :         sock_diag_handlers[family] = NULL;
     134                 :          0 :         mutex_unlock(&sock_diag_table_mutex);
     135                 :            : }
     136                 :            : EXPORT_SYMBOL_GPL(sock_diag_unregister);
     137                 :            : 
     138                 :          0 : static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
     139                 :            : {
     140                 :            :         int err;
     141                 :            :         struct sock_diag_req *req = nlmsg_data(nlh);
     142                 :            :         const struct sock_diag_handler *hndl;
     143                 :            : 
     144         [ #  # ]:          0 :         if (nlmsg_len(nlh) < sizeof(*req))
     145                 :            :                 return -EINVAL;
     146                 :            : 
     147         [ #  # ]:          0 :         if (req->sdiag_family >= AF_MAX)
     148                 :            :                 return -EINVAL;
     149                 :            : 
     150         [ #  # ]:          0 :         if (sock_diag_handlers[req->sdiag_family] == NULL)
     151                 :          0 :                 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
     152                 :            :                                 NETLINK_SOCK_DIAG, req->sdiag_family);
     153                 :            : 
     154                 :          0 :         mutex_lock(&sock_diag_table_mutex);
     155                 :          0 :         hndl = sock_diag_handlers[req->sdiag_family];
     156         [ #  # ]:          0 :         if (hndl == NULL)
     157                 :            :                 err = -ENOENT;
     158                 :            :         else
     159                 :          0 :                 err = hndl->dump(skb, nlh);
     160                 :          0 :         mutex_unlock(&sock_diag_table_mutex);
     161                 :            : 
     162                 :          0 :         return err;
     163                 :            : }
     164                 :            : 
     165                 :          0 : static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
     166                 :            : {
     167                 :            :         int ret;
     168                 :            : 
     169      [ #  #  # ]:          0 :         switch (nlh->nlmsg_type) {
     170                 :            :         case TCPDIAG_GETSOCK:
     171                 :            :         case DCCPDIAG_GETSOCK:
     172         [ #  # ]:          0 :                 if (inet_rcv_compat == NULL)
     173                 :          0 :                         request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
     174                 :            :                                         NETLINK_SOCK_DIAG, AF_INET);
     175                 :            : 
     176                 :          0 :                 mutex_lock(&sock_diag_table_mutex);
     177         [ #  # ]:          0 :                 if (inet_rcv_compat != NULL)
     178                 :          0 :                         ret = inet_rcv_compat(skb, nlh);
     179                 :            :                 else
     180                 :            :                         ret = -EOPNOTSUPP;
     181                 :          0 :                 mutex_unlock(&sock_diag_table_mutex);
     182                 :            : 
     183                 :          0 :                 return ret;
     184                 :            :         case SOCK_DIAG_BY_FAMILY:
     185                 :          0 :                 return __sock_diag_rcv_msg(skb, nlh);
     186                 :            :         default:
     187                 :            :                 return -EINVAL;
     188                 :            :         }
     189                 :            : }
     190                 :            : 
     191                 :            : static DEFINE_MUTEX(sock_diag_mutex);
     192                 :            : 
     193                 :          0 : static void sock_diag_rcv(struct sk_buff *skb)
     194                 :            : {
     195                 :          0 :         mutex_lock(&sock_diag_mutex);
     196                 :          0 :         netlink_rcv_skb(skb, &sock_diag_rcv_msg);
     197                 :          0 :         mutex_unlock(&sock_diag_mutex);
     198                 :          0 : }
     199                 :            : 
     200                 :          0 : static int __net_init diag_net_init(struct net *net)
     201                 :            : {
     202                 :          0 :         struct netlink_kernel_cfg cfg = {
     203                 :            :                 .input  = sock_diag_rcv,
     204                 :            :         };
     205                 :            : 
     206                 :          0 :         net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
     207         [ #  # ]:          0 :         return net->diag_nlsk == NULL ? -ENOMEM : 0;
     208                 :            : }
     209                 :            : 
     210                 :          0 : static void __net_exit diag_net_exit(struct net *net)
     211                 :            : {
     212                 :          0 :         netlink_kernel_release(net->diag_nlsk);
     213                 :          0 :         net->diag_nlsk = NULL;
     214                 :          0 : }
     215                 :            : 
     216                 :            : static struct pernet_operations diag_net_ops = {
     217                 :            :         .init = diag_net_init,
     218                 :            :         .exit = diag_net_exit,
     219                 :            : };
     220                 :            : 
     221                 :          0 : static int __init sock_diag_init(void)
     222                 :            : {
     223                 :          0 :         return register_pernet_subsys(&diag_net_ops);
     224                 :            : }
     225                 :            : 
     226                 :          0 : static void __exit sock_diag_exit(void)
     227                 :            : {
     228                 :          0 :         unregister_pernet_subsys(&diag_net_ops);
     229                 :          0 : }
     230                 :            : 
     231                 :            : module_init(sock_diag_init);
     232                 :            : module_exit(sock_diag_exit);
     233                 :            : MODULE_LICENSE("GPL");
     234                 :            : MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);

Generated by: LCOV version 1.9