1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/skbuff.h>
26#include <linux/timer.h>
27
28#include <linux/connector.h>
29
30static struct cb_id cn_test_id = { 0x123, 0x456 };
31static char cn_test_name[] = "cn_test";
32static struct sock *nls;
33static struct timer_list cn_test_timer;
34
35void cn_test_callback(void *data)
36{
37 struct cn_msg *msg = (struct cn_msg *)data;
38
39 printk("%s: %lu: idx=%x, val=%x, seq=%u, ack=%u, len=%d: %s.\n",
40 __func__, jiffies, msg->id.idx, msg->id.val,
41 msg->seq, msg->ack, msg->len, (char *)msg->data);
42}
43
44static int cn_test_want_notify(void)
45{
46 struct cn_ctl_msg *ctl;
47 struct cn_notify_req *req;
48 struct cn_msg *msg = NULL;
49 int size, size0;
50 struct sk_buff *skb;
51 struct nlmsghdr *nlh;
52 u32 group = 1;
53
54 size0 = sizeof(*msg) + sizeof(*ctl) + 3 * sizeof(*req);
55
56 size = NLMSG_SPACE(size0);
57
58 skb = alloc_skb(size, GFP_ATOMIC);
59 if (!skb) {
60 printk(KERN_ERR "Failed to allocate new skb with size=%u.\n",
61 size);
62
63 return -ENOMEM;
64 }
65
66 nlh = NLMSG_PUT(skb, 0, 0x123, NLMSG_DONE, size - sizeof(*nlh));
67
68 msg = (struct cn_msg *)NLMSG_DATA(nlh);
69
70 memset(msg, 0, size0);
71
72 msg->id.idx = -1;
73 msg->id.val = -1;
74 msg->seq = 0x123;
75 msg->ack = 0x345;
76 msg->len = size0 - sizeof(*msg);
77
78 ctl = (struct cn_ctl_msg *)(msg + 1);
79
80 ctl->idx_notify_num = 1;
81 ctl->val_notify_num = 2;
82 ctl->group = group;
83 ctl->len = msg->len - sizeof(*ctl);
84
85 req = (struct cn_notify_req *)(ctl + 1);
86
87
88
89
90 req->first = cn_test_id.idx;
91 req->range = 10;
92
93
94
95
96 req++;
97 req->first = cn_test_id.val;
98 req->range = 10;
99
100
101
102
103 req++;
104 req->first = cn_test_id.val + 20;
105 req->range = 10;
106
107 NETLINK_CB(skb).dst_group = ctl->group;
108
109 netlink_unicast(nls, skb, 0, 0);
110
111 printk(KERN_INFO "Request was sent. Group=0x%x.\n", ctl->group);
112
113 return 0;
114
115nlmsg_failure:
116 printk(KERN_ERR "Failed to send %u.%u\n", msg->seq, msg->ack);
117 kfree_skb(skb);
118 return -EINVAL;
119}
120
121static u32 cn_test_timer_counter;
122static void cn_test_timer_func(unsigned long __data)
123{
124 struct cn_msg *m;
125 char data[32];
126
127 m = kzalloc(sizeof(*m) + sizeof(data), GFP_ATOMIC);
128 if (m) {
129
130 memcpy(&m->id, &cn_test_id, sizeof(m->id));
131 m->seq = cn_test_timer_counter;
132 m->len = sizeof(data);
133
134 m->len =
135 scnprintf(data, sizeof(data), "counter = %u",
136 cn_test_timer_counter) + 1;
137
138 memcpy(m + 1, data, m->len);
139
140 cn_netlink_send(m, 0, GFP_ATOMIC);
141 kfree(m);
142 }
143
144 cn_test_timer_counter++;
145
146 mod_timer(&cn_test_timer, jiffies + HZ);
147}
148
149static int cn_test_init(void)
150{
151 int err;
152
153 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
154 if (err)
155 goto err_out;
156 cn_test_id.val++;
157 err = cn_add_callback(&cn_test_id, cn_test_name, cn_test_callback);
158 if (err) {
159 cn_del_callback(&cn_test_id);
160 goto err_out;
161 }
162
163 setup_timer(&cn_test_timer, cn_test_timer_func, 0);
164 cn_test_timer.expires = jiffies + HZ;
165 add_timer(&cn_test_timer);
166
167 return 0;
168
169 err_out:
170 if (nls && nls->sk_socket)
171 sock_release(nls->sk_socket);
172
173 return err;
174}
175
176static void cn_test_fini(void)
177{
178 del_timer_sync(&cn_test_timer);
179 cn_del_callback(&cn_test_id);
180 cn_test_id.val--;
181 cn_del_callback(&cn_test_id);
182 if (nls && nls->sk_socket)
183 sock_release(nls->sk_socket);
184}
185
186module_init(cn_test_init);
187module_exit(cn_test_fini);
188
189MODULE_LICENSE("GPL");
190MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
191MODULE_DESCRIPTION("Connector's test module");
192