1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include "core.h"
38#include "name_table.h"
39#include "subscr.h"
40
41
42
43
44
45
46
47
48struct tipc_subscriber {
49 struct kref kref;
50 int conid;
51 spinlock_t lock;
52 struct list_head subscrp_list;
53};
54
55static void tipc_subscrp_delete(struct tipc_subscription *sub);
56static void tipc_subscrb_put(struct tipc_subscriber *subscriber);
57
58
59
60
61
62
63
64
65static u32 htohl(u32 in, int swap)
66{
67 return swap ? swab32(in) : in;
68}
69
70static void tipc_subscrp_send_event(struct tipc_subscription *sub,
71 u32 found_lower, u32 found_upper,
72 u32 event, u32 port_ref, u32 node)
73{
74 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
75 struct tipc_subscriber *subscriber = sub->subscriber;
76 struct kvec msg_sect;
77
78 msg_sect.iov_base = (void *)&sub->evt;
79 msg_sect.iov_len = sizeof(struct tipc_event);
80 sub->evt.event = htohl(event, sub->swap);
81 sub->evt.found_lower = htohl(found_lower, sub->swap);
82 sub->evt.found_upper = htohl(found_upper, sub->swap);
83 sub->evt.port.ref = htohl(port_ref, sub->swap);
84 sub->evt.port.node = htohl(node, sub->swap);
85 tipc_conn_sendmsg(tn->topsrv, subscriber->conid, NULL,
86 msg_sect.iov_base, msg_sect.iov_len);
87}
88
89
90
91
92
93
94
95int tipc_subscrp_check_overlap(struct tipc_subscription *sub, u32 found_lower,
96 u32 found_upper)
97{
98 if (found_lower < sub->seq.lower)
99 found_lower = sub->seq.lower;
100 if (found_upper > sub->seq.upper)
101 found_upper = sub->seq.upper;
102 if (found_lower > found_upper)
103 return 0;
104 return 1;
105}
106
107void tipc_subscrp_report_overlap(struct tipc_subscription *sub, u32 found_lower,
108 u32 found_upper, u32 event, u32 port_ref,
109 u32 node, int must)
110{
111 if (!tipc_subscrp_check_overlap(sub, found_lower, found_upper))
112 return;
113 if (!must && !(sub->filter & TIPC_SUB_PORTS))
114 return;
115
116 tipc_subscrp_send_event(sub, found_lower, found_upper, event, port_ref,
117 node);
118}
119
120static void tipc_subscrp_timeout(unsigned long data)
121{
122 struct tipc_subscription *sub = (struct tipc_subscription *)data;
123 struct tipc_subscriber *subscriber = sub->subscriber;
124
125
126 tipc_subscrp_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
127 TIPC_SUBSCR_TIMEOUT, 0, 0);
128
129 spin_lock_bh(&subscriber->lock);
130 tipc_subscrp_delete(sub);
131 spin_unlock_bh(&subscriber->lock);
132
133 tipc_subscrb_put(subscriber);
134}
135
136static void tipc_subscrb_kref_release(struct kref *kref)
137{
138 struct tipc_subscriber *subcriber = container_of(kref,
139 struct tipc_subscriber, kref);
140
141 kfree(subcriber);
142}
143
144static void tipc_subscrb_put(struct tipc_subscriber *subscriber)
145{
146 kref_put(&subscriber->kref, tipc_subscrb_kref_release);
147}
148
149static void tipc_subscrb_get(struct tipc_subscriber *subscriber)
150{
151 kref_get(&subscriber->kref);
152}
153
154static struct tipc_subscriber *tipc_subscrb_create(int conid)
155{
156 struct tipc_subscriber *subscriber;
157
158 subscriber = kzalloc(sizeof(*subscriber), GFP_ATOMIC);
159 if (!subscriber) {
160 pr_warn("Subscriber rejected, no memory\n");
161 return NULL;
162 }
163 kref_init(&subscriber->kref);
164 INIT_LIST_HEAD(&subscriber->subscrp_list);
165 subscriber->conid = conid;
166 spin_lock_init(&subscriber->lock);
167
168 return subscriber;
169}
170
171static void tipc_subscrb_delete(struct tipc_subscriber *subscriber)
172{
173 struct tipc_subscription *sub, *temp;
174
175 spin_lock_bh(&subscriber->lock);
176
177 list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
178 subscrp_list) {
179 if (del_timer(&sub->timer)) {
180 tipc_subscrp_delete(sub);
181 tipc_subscrb_put(subscriber);
182 }
183 }
184 spin_unlock_bh(&subscriber->lock);
185
186 tipc_subscrb_put(subscriber);
187}
188
189static void tipc_subscrp_delete(struct tipc_subscription *sub)
190{
191 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
192
193 tipc_nametbl_unsubscribe(sub);
194 list_del(&sub->subscrp_list);
195 kfree(sub);
196 atomic_dec(&tn->subscription_count);
197}
198
199static void tipc_subscrp_cancel(struct tipc_subscr *s,
200 struct tipc_subscriber *subscriber)
201{
202 struct tipc_subscription *sub, *temp;
203
204 spin_lock_bh(&subscriber->lock);
205
206 list_for_each_entry_safe(sub, temp, &subscriber->subscrp_list,
207 subscrp_list) {
208 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
209 if (del_timer(&sub->timer)) {
210 tipc_subscrp_delete(sub);
211 tipc_subscrb_put(subscriber);
212 }
213 break;
214 }
215 }
216 spin_unlock_bh(&subscriber->lock);
217}
218
219static int tipc_subscrp_create(struct net *net, struct tipc_subscr *s,
220 struct tipc_subscriber *subscriber,
221 struct tipc_subscription **sub_p)
222{
223 struct tipc_net *tn = net_generic(net, tipc_net_id);
224 struct tipc_subscription *sub;
225 int swap;
226
227
228 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
229
230
231 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
232 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
233 tipc_subscrp_cancel(s, subscriber);
234 return 0;
235 }
236
237
238 if (atomic_read(&tn->subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
239 pr_warn("Subscription rejected, limit reached (%u)\n",
240 TIPC_MAX_SUBSCRIPTIONS);
241 return -EINVAL;
242 }
243
244
245 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
246 if (!sub) {
247 pr_warn("Subscription rejected, no memory\n");
248 return -ENOMEM;
249 }
250
251
252 sub->net = net;
253 sub->seq.type = htohl(s->seq.type, swap);
254 sub->seq.lower = htohl(s->seq.lower, swap);
255 sub->seq.upper = htohl(s->seq.upper, swap);
256 sub->timeout = msecs_to_jiffies(htohl(s->timeout, swap));
257 sub->filter = htohl(s->filter, swap);
258 if ((!(sub->filter & TIPC_SUB_PORTS) ==
259 !(sub->filter & TIPC_SUB_SERVICE)) ||
260 (sub->seq.lower > sub->seq.upper)) {
261 pr_warn("Subscription rejected, illegal request\n");
262 kfree(sub);
263 return -EINVAL;
264 }
265 spin_lock_bh(&subscriber->lock);
266 list_add(&sub->subscrp_list, &subscriber->subscrp_list);
267 spin_unlock_bh(&subscriber->lock);
268 sub->subscriber = subscriber;
269 sub->swap = swap;
270 memcpy(&sub->evt.s, s, sizeof(*s));
271 atomic_inc(&tn->subscription_count);
272 setup_timer(&sub->timer, tipc_subscrp_timeout, (unsigned long)sub);
273 if (sub->timeout != TIPC_WAIT_FOREVER)
274 sub->timeout += jiffies;
275 if (!mod_timer(&sub->timer, sub->timeout))
276 tipc_subscrb_get(subscriber);
277 *sub_p = sub;
278 return 0;
279}
280
281
282static void tipc_subscrb_shutdown_cb(int conid, void *usr_data)
283{
284 tipc_subscrb_delete((struct tipc_subscriber *)usr_data);
285}
286
287
288static void tipc_subscrb_rcv_cb(struct net *net, int conid,
289 struct sockaddr_tipc *addr, void *usr_data,
290 void *buf, size_t len)
291{
292 struct tipc_subscriber *subscriber = usr_data;
293 struct tipc_subscription *sub = NULL;
294 struct tipc_net *tn = net_generic(net, tipc_net_id);
295
296 tipc_subscrp_create(net, (struct tipc_subscr *)buf, subscriber, &sub);
297 if (sub)
298 tipc_nametbl_subscribe(sub);
299 else
300 tipc_conn_terminate(tn->topsrv, subscriber->conid);
301}
302
303
304static void *tipc_subscrb_connect_cb(int conid)
305{
306 return (void *)tipc_subscrb_create(conid);
307}
308
309int tipc_topsrv_start(struct net *net)
310{
311 struct tipc_net *tn = net_generic(net, tipc_net_id);
312 const char name[] = "topology_server";
313 struct tipc_server *topsrv;
314 struct sockaddr_tipc *saddr;
315
316 saddr = kzalloc(sizeof(*saddr), GFP_ATOMIC);
317 if (!saddr)
318 return -ENOMEM;
319 saddr->family = AF_TIPC;
320 saddr->addrtype = TIPC_ADDR_NAMESEQ;
321 saddr->addr.nameseq.type = TIPC_TOP_SRV;
322 saddr->addr.nameseq.lower = TIPC_TOP_SRV;
323 saddr->addr.nameseq.upper = TIPC_TOP_SRV;
324 saddr->scope = TIPC_NODE_SCOPE;
325
326 topsrv = kzalloc(sizeof(*topsrv), GFP_ATOMIC);
327 if (!topsrv) {
328 kfree(saddr);
329 return -ENOMEM;
330 }
331 topsrv->net = net;
332 topsrv->saddr = saddr;
333 topsrv->imp = TIPC_CRITICAL_IMPORTANCE;
334 topsrv->type = SOCK_SEQPACKET;
335 topsrv->max_rcvbuf_size = sizeof(struct tipc_subscr);
336 topsrv->tipc_conn_recvmsg = tipc_subscrb_rcv_cb;
337 topsrv->tipc_conn_new = tipc_subscrb_connect_cb;
338 topsrv->tipc_conn_shutdown = tipc_subscrb_shutdown_cb;
339
340 strncpy(topsrv->name, name, strlen(name) + 1);
341 tn->topsrv = topsrv;
342 atomic_set(&tn->subscription_count, 0);
343
344 return tipc_server_start(topsrv);
345}
346
347void tipc_topsrv_stop(struct net *net)
348{
349 struct tipc_net *tn = net_generic(net, tipc_net_id);
350 struct tipc_server *topsrv = tn->topsrv;
351
352 tipc_server_stop(topsrv);
353 kfree(topsrv->saddr);
354 kfree(topsrv);
355}
356