2012-05-15 06:20:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
|
|
* may be copied, distributed, and modified under those terms.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2012-10-29 12:05:02 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2012-05-15 06:20:36 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/etherdevice.h>
|
2013-03-27 07:52:17 +01:00
|
|
|
#include <net/netlink.h>
|
2012-05-15 06:20:36 +02:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <net/sock.h>
|
2013-02-19 18:50:18 +01:00
|
|
|
#include "netlink_k.h"
|
2012-05-15 06:20:36 +02:00
|
|
|
|
|
|
|
#if !defined(NLMSG_HDRLEN)
|
|
|
|
#define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ND_MAX_GROUP 30
|
|
|
|
#define ND_IFINDEX_LEN sizeof(int)
|
2013-03-27 07:52:17 +01:00
|
|
|
#define ND_NLMSG_SPACE(len) (nlmsg_total_size(len) + ND_IFINDEX_LEN)
|
2012-05-15 06:20:36 +02:00
|
|
|
#define ND_NLMSG_DATA(nlh) \
|
2013-03-27 07:52:17 +01:00
|
|
|
((void *)((char *)nlmsg_data(nlh) + ND_IFINDEX_LEN))
|
2012-05-15 06:20:36 +02:00
|
|
|
#define ND_NLMSG_S_LEN(len) (len+ND_IFINDEX_LEN)
|
|
|
|
#define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len-ND_IFINDEX_LEN)
|
2013-03-27 07:52:17 +01:00
|
|
|
#define ND_NLMSG_IFIDX(nlh) nlmsg_data(nlh)
|
2012-05-15 06:20:36 +02:00
|
|
|
#define ND_MAX_MSG_LEN 8096
|
|
|
|
|
|
|
|
#if defined(DEFINE_MUTEX)
|
|
|
|
static DEFINE_MUTEX(netlink_mutex);
|
|
|
|
#else
|
|
|
|
static struct semaphore netlink_mutex;
|
|
|
|
#define mutex_lock(x) down(x)
|
|
|
|
#define mutex_unlock(x) up(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
|
|
|
|
|
|
|
|
static void netlink_rcv_cb(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
struct net_device *dev;
|
|
|
|
u32 mlen;
|
|
|
|
void *msg;
|
|
|
|
int ifindex;
|
|
|
|
|
2013-03-27 07:52:17 +01:00
|
|
|
if (skb->len >= NLMSG_HDRLEN) {
|
2012-05-15 06:20:36 +02:00
|
|
|
nlh = (struct nlmsghdr *)skb->data;
|
|
|
|
|
|
|
|
if (skb->len < nlh->nlmsg_len ||
|
|
|
|
nlh->nlmsg_len > ND_MAX_MSG_LEN) {
|
2012-10-29 12:05:02 +01:00
|
|
|
netdev_err(skb->dev, "Invalid length (%d,%d)\n",
|
|
|
|
skb->len, nlh->nlmsg_len);
|
2012-05-15 06:20:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&ifindex, ND_NLMSG_IFIDX(nlh), ND_IFINDEX_LEN);
|
|
|
|
msg = ND_NLMSG_DATA(nlh);
|
|
|
|
mlen = ND_NLMSG_R_LEN(nlh);
|
|
|
|
|
|
|
|
if (rcv_cb) {
|
|
|
|
dev = dev_get_by_index(&init_net, ifindex);
|
|
|
|
if (dev) {
|
|
|
|
rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
|
|
|
|
dev_put(dev);
|
|
|
|
} else
|
2012-10-29 12:05:02 +01:00
|
|
|
netdev_err(skb->dev,
|
|
|
|
"dev_get_by_index(%d) is not found.\n",
|
|
|
|
ifindex);
|
2012-05-15 06:20:36 +02:00
|
|
|
} else
|
2012-10-29 12:05:02 +01:00
|
|
|
netdev_err(skb->dev, "Unregistered Callback\n");
|
2012-05-15 06:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netlink_rcv(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
mutex_lock(&netlink_mutex);
|
|
|
|
netlink_rcv_cb(skb);
|
|
|
|
mutex_unlock(&netlink_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sock *netlink_init(int unit, void (*cb)(struct net_device *dev, u16 type,
|
|
|
|
void *msg, int len))
|
|
|
|
{
|
|
|
|
struct sock *sock;
|
2012-06-29 08:15:21 +02:00
|
|
|
struct netlink_kernel_cfg cfg = {
|
|
|
|
.input = netlink_rcv,
|
|
|
|
};
|
2012-05-15 06:20:36 +02:00
|
|
|
|
|
|
|
#if !defined(DEFINE_MUTEX)
|
|
|
|
init_MUTEX(&netlink_mutex);
|
|
|
|
#endif
|
|
|
|
|
2012-09-08 04:53:54 +02:00
|
|
|
sock = netlink_kernel_create(&init_net, unit, &cfg);
|
2012-05-15 06:20:36 +02:00
|
|
|
|
|
|
|
if (sock)
|
|
|
|
rcv_cb = cb;
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void netlink_exit(struct sock *sock)
|
|
|
|
{
|
2012-06-14 22:18:56 +02:00
|
|
|
netlink_kernel_release(sock);
|
2012-05-15 06:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
|
|
|
|
{
|
|
|
|
static u32 seq;
|
|
|
|
struct sk_buff *skb = NULL;
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (group > ND_MAX_GROUP) {
|
2012-10-29 12:05:02 +01:00
|
|
|
pr_err("Group %d is invalied.\n", group);
|
|
|
|
pr_err("Valid group is 0 ~ %d.\n", ND_MAX_GROUP);
|
2012-05-15 06:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-03-27 07:52:17 +01:00
|
|
|
skb = nlmsg_new(len, GFP_ATOMIC);
|
2012-05-15 06:20:36 +02:00
|
|
|
if (!skb) {
|
2012-10-29 12:05:02 +01:00
|
|
|
pr_err("netlink_broadcast ret=%d\n", ret);
|
2012-05-15 06:20:36 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
seq++;
|
2012-06-27 06:20:52 +02:00
|
|
|
nlh = nlmsg_put(skb, 0, seq, type, len, 0);
|
|
|
|
if (!nlh) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
memcpy(nlmsg_data(nlh), msg, len);
|
2012-05-15 06:20:36 +02:00
|
|
|
|
2012-09-07 22:12:54 +02:00
|
|
|
NETLINK_CB(skb).portid = 0;
|
2012-05-15 06:20:36 +02:00
|
|
|
NETLINK_CB(skb).dst_group = 0;
|
|
|
|
|
|
|
|
ret = netlink_broadcast(sock, skb, 0, group+1, GFP_ATOMIC);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return len;
|
|
|
|
else {
|
|
|
|
if (ret != -ESRCH) {
|
2012-10-29 12:05:02 +01:00
|
|
|
pr_err("netlink_broadcast g=%d, t=%d, l=%d, r=%d\n",
|
|
|
|
group, type, len, ret);
|
2012-05-15 06:20:36 +02:00
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|