Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / gdm724x / netlink_k.c
CommitLineData
61e12104
WK
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
0ec473b5
JP
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
61e12104 16#include <linux/export.h>
bf5cad61 17#include <linux/mutex.h>
61e12104
WK
18#include <linux/etherdevice.h>
19#include <linux/netlink.h>
20#include <asm/byteorder.h>
21#include <net/sock.h>
22
23#include "netlink_k.h"
24
61e12104 25static DEFINE_MUTEX(netlink_mutex);
61e12104
WK
26
27#define ND_MAX_GROUP 30
28#define ND_IFINDEX_LEN sizeof(int)
29#define ND_NLMSG_SPACE(len) (NLMSG_SPACE(len) + ND_IFINDEX_LEN)
a6708372
DN
30#define ND_NLMSG_DATA(nlh) ((void *)((char *)NLMSG_DATA(nlh) + \
31 ND_IFINDEX_LEN))
ba7f55b7
IC
32#define ND_NLMSG_S_LEN(len) (len + ND_IFINDEX_LEN)
33#define ND_NLMSG_R_LEN(nlh) (nlh->nlmsg_len - ND_IFINDEX_LEN)
61e12104
WK
34#define ND_NLMSG_IFIDX(nlh) NLMSG_DATA(nlh)
35#define ND_MAX_MSG_LEN (1024 * 32)
36
37static void (*rcv_cb)(struct net_device *dev, u16 type, void *msg, int len);
38
39static void netlink_rcv_cb(struct sk_buff *skb)
40{
41 struct nlmsghdr *nlh;
42 struct net_device *dev;
43 u32 mlen;
44 void *msg;
45 int ifindex;
46
47 if (!rcv_cb) {
0ec473b5 48 pr_err("nl cb - unregistered\n");
61e12104
WK
49 return;
50 }
51
58419939 52 if (skb->len < NLMSG_HDRLEN) {
0ec473b5 53 pr_err("nl cb - invalid skb length\n");
61e12104
WK
54 return;
55 }
56
57 nlh = (struct nlmsghdr *)skb->data;
58
59 if (skb->len < nlh->nlmsg_len || nlh->nlmsg_len > ND_MAX_MSG_LEN) {
0ec473b5 60 pr_err("nl cb - invalid length (%d,%d)\n",
61e12104
WK
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 dev = dev_get_by_index(&init_net, ifindex);
70 if (dev) {
71 rcv_cb(dev, nlh->nlmsg_type, msg, mlen);
72 dev_put(dev);
73 } else {
0ec473b5 74 pr_err("nl cb - dev (%d) not found\n", ifindex);
61e12104
WK
75 }
76}
77
78static void netlink_rcv(struct sk_buff *skb)
79{
80 mutex_lock(&netlink_mutex);
81 netlink_rcv_cb(skb);
82 mutex_unlock(&netlink_mutex);
83}
84
85struct sock *netlink_init(int unit,
47678e37
BC
86 void (*cb)(struct net_device *dev, u16 type,
87 void *msg, int len))
61e12104
WK
88{
89 struct sock *sock;
61e12104
WK
90 struct netlink_kernel_cfg cfg = {
91 .input = netlink_rcv,
92 };
61e12104 93
61e12104 94 sock = netlink_kernel_create(&init_net, unit, &cfg);
61e12104
WK
95
96 if (sock)
97 rcv_cb = cb;
98
99 return sock;
100}
101
61e12104
WK
102int netlink_send(struct sock *sock, int group, u16 type, void *msg, int len)
103{
104 static u32 seq;
105 struct sk_buff *skb = NULL;
106 struct nlmsghdr *nlh;
107 int ret = 0;
108
109 if (group > ND_MAX_GROUP)
110 return -EINVAL;
111
b6f6fd8a 112 if (!netlink_has_listeners(sock, group + 1))
61e12104
WK
113 return -ESRCH;
114
115 skb = alloc_skb(NLMSG_SPACE(len), GFP_ATOMIC);
116 if (!skb)
117 return -ENOMEM;
118
119 seq++;
120
121 nlh = nlmsg_put(skb, 0, seq, type, len, 0);
122 memcpy(NLMSG_DATA(nlh), msg, len);
61e12104 123 NETLINK_CB(skb).portid = 0;
61e12104
WK
124 NETLINK_CB(skb).dst_group = 0;
125
ba7f55b7 126 ret = netlink_broadcast(sock, skb, 0, group + 1, GFP_ATOMIC);
61e12104
WK
127 if (!ret)
128 return len;
129
130 if (ret != -ESRCH)
0ec473b5 131 pr_err("nl broadcast g=%d, t=%d, l=%d, r=%d\n",
61e12104 132 group, type, len, ret);
ba7f55b7 133 else if (netlink_has_listeners(sock, group + 1))
61e12104
WK
134 return -EAGAIN;
135
136 return ret;
137}
This page took 0.386331 seconds and 5 git commands to generate.