linux/drivers/staging/gdm724x/netlink_k.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
   3 *
   4 * This software is licensed under the terms of the GNU General Public
   5 * License version 2, as published by the Free Software Foundation, and
   6 * may be copied, distributed, and modified under those terms.
   7 *
   8 * This program is distributed in the hope that it will be useful,
   9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11 * GNU General Public License for more details.
  12 */
  13
  14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15
  16#include <linux/export.h>
  17#include <linux/etherdevice.h>
  18#include <linux/netlink.h>
  19#include <asm/byteorder.h>
  20#include <net/sock.h>
  21
  22#include "netlink_k.h"
  23
  24#if defined(DEFINE_MUTEX)
  25static DEFINE_MUTEX(netlink_mutex);
  26#else
  27static struct semaphore netlink_mutex;
  28#define mutex_lock(x)           down(x)
  29#define mutex_unlock(x)         up(x)
  30#endif
  31
  32#define ND_MAX_GROUP            30
  33#define ND_IFINDEX_LEN          sizeof(int)
  34#define ND_NLMSG_SPACE(len)     (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
  35#define ND_NLMSG_DATA(nlh)      ((void *)((char *)NLMSG_DATA(nlh) + \
  36                                                  ND_IFINDEX_LEN))
  37#define ND_NLMSG_S_LEN(len)     (len + ND_IFINDEX_LEN)
  38#define ND_NLMSG_R_LEN(nlh)     (nlh->nlmsg_len - ND_IFINDEX_LEN)
  39#define ND_NLMSG_IFIDX(nlh)     NLMSG_DATA(nlh)
  40#define ND_MAX_MSG_LEN          (1024 * 32)
  41
  42static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
  43
  44static void netlink_rcv_cb(struct sk_buff *skb)
  45{
  46        struct nlmsghdr *nlh;
  47        struct net_device *dev;
  48        u32 mlen;
  49        void *msg;
  50        int ifindex;
  51
  52        if (!rcv_cb) {
  53                pr_err("nl cb - unregistered\n");
  54                return;
  55        }
  56
  57        if (skb->len < NLMSG_HDRLEN) {
  58                pr_err("nl cb - invalid skb length\n");
  59                return;
  60        }
  61
  62        nlh = (struct nlmsghdr *)skb->data;
  63
  64        if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) {
  65                pr_err("nl cb - invalid length (%d,%d)\n",
  66                       skb->len, nlh->nlmsg_len);
  67                return;
  68        }
  69
  70        memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
  71        msg = ND_NLMSG_DATA(nlh);
  72        mlen = ND_NLMSG_R_LEN(nlh);
  73
  74        dev = dev_get_by_index(&init_net, ifindex);
  75        if (dev) {
  76                rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
  77                dev_put(dev);
  78        } else {
  79                pr_err("nl cb - dev (%d) not found\n", ifindex);
  80        }
  81}
  82
  83static void netlink_rcv(struct sk_buff *skb)
  84{
  85        mutex_lock(&netlink_mutex);
  86        netlink_rcv_cb(skb);
  87        mutex_unlock(&netlink_mutex);
  88}
  89
  90struct sock *netlink_init(int unit,
  91                          void (*cb)(struct net_device *dev, u16 type,
  92                                     void *msg, int len))
  93{
  94        struct sock *sock;
  95        struct netlink_kernel_cfg cfg = {
  96                .input  = netlink_rcv,
  97        };
  98
  99#if !defined(DEFINE_MUTEX)
 100        init_MUTEX(&netlink_mutex);
 101#endif
 102
 103        sock = netlink_kernel_create(&init_net, unit, &cfg);
 104
 105        if (sock)
 106                rcv_cb = cb;
 107
 108        return sock;
 109}
 110
 111int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
 112{
 113        static u32 seq;
 114        struct sk_buff *skb = NULL;
 115        struct nlmsghdr *nlh;
 116        int ret = 0;
 117
 118        if (group > ND_MAX_GROUP)
 119                return -EINVAL;
 120
 121        if (!netlink_has_listeners(sock, group + 1))
 122                return -ESRCH;
 123
 124        skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
 125        if (!skb)
 126                return -ENOMEM;
 127
 128        seq++;
 129
 130        nlh = nlmsg_put(skb, 0, seq, type, len, 0);
 131        memcpy(NLMSG_DATA(nlh), msg, len);
 132        NETLINK_CB(skb).portid = 0;
 133        NETLINK_CB(skb).dst_group = 0;
 134
 135        ret = netlink_broadcast(sock, skb, 0, group + 1, GFP_ATOMIC);
 136        if (!ret)
 137                return len;
 138
 139        if (ret != -ESRCH)
 140                pr_err("nl broadcast g=%d, t=%d, l=%d, r=%d\n",
 141                       group, type, len, ret);
 142        else if (netlink_has_listeners(sock, group + 1))
 143                return -EAGAIN;
 144
 145        return ret;
 146}
 147