linux/drivers/staging/gdm72xx/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/module.h>
  17#include <linux/etherdevice.h>
  18#include <net/netlink.h>
  19#include <asm/byteorder.h>
  20#include <net/sock.h>
  21#include "netlink_k.h"
  22
  23#if !defined(NLMSG_HDRLEN)
  24#define NLMSG_HDRLEN     ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
  25#endif
  26
  27#define ND_MAX_GROUP                    30
  28#define ND_IFINDEX_LEN                  sizeof(int)
  29#define ND_NLMSG_SPACE(len)             (nlmsg_total_size(len) + ND_IFINDEX_LEN)
  30#define ND_NLMSG_DATA(nlh) \
  31        ((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN))
  32#define ND_NLMSG_S_LEN(len)             (len+ND_IFINDEX_LEN)
  33#define ND_NLMSG_R_LEN(nlh)             (nlh->nlmsg_len-ND_IFINDEX_LEN)
  34#define ND_NLMSG_IFIDX(nlh)             nlmsg_data(nlh)
  35#define ND_MAX_MSG_LEN                  8096
  36
  37#if defined(DEFINE_MUTEX)
  38static DEFINE_MUTEX(netlink_mutex);
  39#else
  40static struct semaphore netlink_mutex;
  41#define mutex_lock(x)           down(x)
  42#define mutex_unlock(x)         up(x)
  43#endif
  44
  45static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
  46
  47static void netlink_rcv_cb(struct sk_buff *skb)
  48{
  49        struct nlmsghdr *nlh;
  50        struct net_device *dev;
  51        u32 mlen;
  52        void *msg;
  53        int ifindex;
  54
  55        if (skb->len >= NLMSG_HDRLEN) {
  56                nlh = (struct nlmsghdr *)skb->data;
  57
  58                if (skb->len < nlh->nlmsg_len ||
  59                nlh->nlmsg_len > ND_MAX_MSG_LEN) {
  60                        netdev_err(skb->dev, "Invalid length (%d,%d)\n",
  61                                   skb->len, nlh->nlmsg_len);
  62                        return;
  63                }
  64
  65                memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
  66                msg = ND_NLMSG_DATA(nlh);
  67                mlen = ND_NLMSG_R_LEN(nlh);
  68
  69                if (rcv_cb) {
  70                        dev = dev_get_by_index(&init_net, ifindex);
  71                        if (dev) {
  72                                rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
  73                                dev_put(dev);
  74                        } else
  75                                netdev_err(skb->dev,
  76                                           "dev_get_by_index(%d) is not found.\n",
  77                                           ifindex);
  78                } else
  79                        netdev_err(skb->dev, "Unregistered Callback\n");
  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, void (*cb)(struct net_device *dev, u16 type,
  91                                                void *msg, int len))
  92{
  93        struct sock *sock;
  94        struct netlink_kernel_cfg cfg = {
  95                .input  = netlink_rcv,
  96        };
  97
  98#if !defined(DEFINE_MUTEX)
  99        init_MUTEX(&netlink_mutex);
 100#endif
 101
 102        sock = netlink_kernel_create(&init_net, unit, &cfg);
 103
 104        if (sock)
 105                rcv_cb = cb;
 106
 107        return sock;
 108}
 109
 110void netlink_exit(struct sock *sock)
 111{
 112        netlink_kernel_release(sock);
 113}
 114
 115int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
 116{
 117        static u32 seq;
 118        struct sk_buff *skb = NULL;
 119        struct nlmsghdr *nlh;
 120        int ret = 0;
 121
 122        if (group > ND_MAX_GROUP) {
 123                pr_err("Group %d is invalied.\n", group);
 124                pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
 125                return -EINVAL;
 126        }
 127
 128        skb = nlmsg_new(len, GFP_ATOMIC);
 129        if (!skb) {
 130                pr_err("netlink_broadcast ret=%d\n", ret);
 131                return -ENOMEM;
 132        }
 133
 134        seq++;
 135        nlh = nlmsg_put(skb, 0, seq, type, len, 0);
 136        if (!nlh) {
 137                kfree_skb(skb);
 138                return -EMSGSIZE;
 139        }
 140        memcpy(nlmsg_data(nlh), msg, len);
 141
 142        NETLINK_CB(skb).portid = 0;
 143        NETLINK_CB(skb).dst_group = 0;
 144
 145        ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
 146
 147        if (!ret)
 148                return len;
 149        else {
 150                if (ret != -ESRCH) {
 151                        pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
 152                               group, type, len, ret);
 153                }
 154                ret = 0;
 155        }
 156        return ret;
 157}
 158