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