1 /*
2 * net/tipc/bearer.c: TIPC bearer code
3 *
4 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
5 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <net/sock.h>
38 #include "core.h"
39 #include "bearer.h"
40 #include "link.h"
41 #include "discover.h"
42 #include "monitor.h"
43 #include "bcast.h"
44 #include "netlink.h"
45 #include "udp_media.h"
46
47 #define MAX_ADDR_STR 60
48
49 static struct tipc_media * const media_info_array[] = {
50 ð_media_info,
51 #ifdef CONFIG_TIPC_MEDIA_IB
52 &ib_media_info,
53 #endif
54 #ifdef CONFIG_TIPC_MEDIA_UDP
55 &udp_media_info,
56 #endif
57 NULL
58 };
59
bearer_get(struct net * net,int bearer_id)60 static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
61 {
62 struct tipc_net *tn = tipc_net(net);
63
64 return rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
65 }
66
67 static void bearer_disable(struct net *net, struct tipc_bearer *b);
68
69 /**
70 * tipc_media_find - locates specified media object by name
71 */
tipc_media_find(const char * name)72 struct tipc_media *tipc_media_find(const char *name)
73 {
74 u32 i;
75
76 for (i = 0; media_info_array[i] != NULL; i++) {
77 if (!strcmp(media_info_array[i]->name, name))
78 break;
79 }
80 return media_info_array[i];
81 }
82
83 /**
84 * media_find_id - locates specified media object by type identifier
85 */
media_find_id(u8 type)86 static struct tipc_media *media_find_id(u8 type)
87 {
88 u32 i;
89
90 for (i = 0; media_info_array[i] != NULL; i++) {
91 if (media_info_array[i]->type_id == type)
92 break;
93 }
94 return media_info_array[i];
95 }
96
97 /**
98 * tipc_media_addr_printf - record media address in print buffer
99 */
tipc_media_addr_printf(char * buf,int len,struct tipc_media_addr * a)100 void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
101 {
102 char addr_str[MAX_ADDR_STR];
103 struct tipc_media *m;
104 int ret;
105
106 m = media_find_id(a->media_id);
107
108 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
109 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
110 else {
111 u32 i;
112
113 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
114 for (i = 0; i < sizeof(a->value); i++)
115 ret += scnprintf(buf - ret, len + ret,
116 "-%02x", a->value[i]);
117 }
118 }
119
120 /**
121 * bearer_name_validate - validate & (optionally) deconstruct bearer name
122 * @name: ptr to bearer name string
123 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
124 *
125 * Returns 1 if bearer name is valid, otherwise 0.
126 */
bearer_name_validate(const char * name,struct tipc_bearer_names * name_parts)127 static int bearer_name_validate(const char *name,
128 struct tipc_bearer_names *name_parts)
129 {
130 char name_copy[TIPC_MAX_BEARER_NAME];
131 char *media_name;
132 char *if_name;
133 u32 media_len;
134 u32 if_len;
135
136 /* copy bearer name & ensure length is OK */
137 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
138 /* need above in case non-Posix strncpy() doesn't pad with nulls */
139 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
140 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
141 return 0;
142
143 /* ensure all component parts of bearer name are present */
144 media_name = name_copy;
145 if_name = strchr(media_name, ':');
146 if (if_name == NULL)
147 return 0;
148 *(if_name++) = 0;
149 media_len = if_name - media_name;
150 if_len = strlen(if_name) + 1;
151
152 /* validate component parts of bearer name */
153 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
154 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
155 return 0;
156
157 /* return bearer name components, if necessary */
158 if (name_parts) {
159 strcpy(name_parts->media_name, media_name);
160 strcpy(name_parts->if_name, if_name);
161 }
162 return 1;
163 }
164
165 /**
166 * tipc_bearer_find - locates bearer object with matching bearer name
167 */
tipc_bearer_find(struct net * net,const char * name)168 struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
169 {
170 struct tipc_net *tn = net_generic(net, tipc_net_id);
171 struct tipc_bearer *b;
172 u32 i;
173
174 for (i = 0; i < MAX_BEARERS; i++) {
175 b = rtnl_dereference(tn->bearer_list[i]);
176 if (b && (!strcmp(b->name, name)))
177 return b;
178 }
179 return NULL;
180 }
181
182 /* tipc_bearer_get_name - get the bearer name from its id.
183 * @net: network namespace
184 * @name: a pointer to the buffer where the name will be stored.
185 * @bearer_id: the id to get the name from.
186 */
tipc_bearer_get_name(struct net * net,char * name,u32 bearer_id)187 int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
188 {
189 struct tipc_net *tn = tipc_net(net);
190 struct tipc_bearer *b;
191
192 if (bearer_id >= MAX_BEARERS)
193 return -EINVAL;
194
195 b = rtnl_dereference(tn->bearer_list[bearer_id]);
196 if (!b)
197 return -EINVAL;
198
199 strcpy(name, b->name);
200 return 0;
201 }
202
tipc_bearer_add_dest(struct net * net,u32 bearer_id,u32 dest)203 void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
204 {
205 struct tipc_net *tn = net_generic(net, tipc_net_id);
206 struct tipc_bearer *b;
207
208 rcu_read_lock();
209 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
210 if (b)
211 tipc_disc_add_dest(b->link_req);
212 rcu_read_unlock();
213 }
214
tipc_bearer_remove_dest(struct net * net,u32 bearer_id,u32 dest)215 void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
216 {
217 struct tipc_net *tn = net_generic(net, tipc_net_id);
218 struct tipc_bearer *b;
219
220 rcu_read_lock();
221 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
222 if (b)
223 tipc_disc_remove_dest(b->link_req);
224 rcu_read_unlock();
225 }
226
227 /**
228 * tipc_enable_bearer - enable bearer with the given name
229 */
tipc_enable_bearer(struct net * net,const char * name,u32 disc_domain,u32 priority,struct nlattr * attr[])230 static int tipc_enable_bearer(struct net *net, const char *name,
231 u32 disc_domain, u32 priority,
232 struct nlattr *attr[])
233 {
234 struct tipc_net *tn = net_generic(net, tipc_net_id);
235 struct tipc_bearer *b;
236 struct tipc_media *m;
237 struct tipc_bearer_names b_names;
238 struct sk_buff *skb;
239 char addr_string[16];
240 u32 bearer_id;
241 u32 with_this_prio;
242 u32 i;
243 int res = -EINVAL;
244
245 if (!tn->own_addr) {
246 pr_warn("Bearer <%s> rejected, not supported in standalone mode\n",
247 name);
248 return -ENOPROTOOPT;
249 }
250 if (!bearer_name_validate(name, &b_names)) {
251 pr_warn("Bearer <%s> rejected, illegal name\n", name);
252 return -EINVAL;
253 }
254 if (tipc_addr_domain_valid(disc_domain) &&
255 (disc_domain != tn->own_addr)) {
256 if (tipc_in_scope(disc_domain, tn->own_addr)) {
257 disc_domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK;
258 res = 0; /* accept any node in own cluster */
259 } else if (in_own_cluster_exact(net, disc_domain))
260 res = 0; /* accept specified node in own cluster */
261 }
262 if (res) {
263 pr_warn("Bearer <%s> rejected, illegal discovery domain\n",
264 name);
265 return -EINVAL;
266 }
267 if ((priority > TIPC_MAX_LINK_PRI) &&
268 (priority != TIPC_MEDIA_LINK_PRI)) {
269 pr_warn("Bearer <%s> rejected, illegal priority\n", name);
270 return -EINVAL;
271 }
272
273 m = tipc_media_find(b_names.media_name);
274 if (!m) {
275 pr_warn("Bearer <%s> rejected, media <%s> not registered\n",
276 name, b_names.media_name);
277 return -EINVAL;
278 }
279
280 if (priority == TIPC_MEDIA_LINK_PRI)
281 priority = m->priority;
282
283 restart:
284 bearer_id = MAX_BEARERS;
285 with_this_prio = 1;
286 for (i = MAX_BEARERS; i-- != 0; ) {
287 b = rtnl_dereference(tn->bearer_list[i]);
288 if (!b) {
289 bearer_id = i;
290 continue;
291 }
292 if (!strcmp(name, b->name)) {
293 pr_warn("Bearer <%s> rejected, already enabled\n",
294 name);
295 return -EINVAL;
296 }
297 if ((b->priority == priority) &&
298 (++with_this_prio > 2)) {
299 if (priority-- == 0) {
300 pr_warn("Bearer <%s> rejected, duplicate priority\n",
301 name);
302 return -EINVAL;
303 }
304 pr_warn("Bearer <%s> priority adjustment required %u->%u\n",
305 name, priority + 1, priority);
306 goto restart;
307 }
308 }
309 if (bearer_id >= MAX_BEARERS) {
310 pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
311 name, MAX_BEARERS);
312 return -EINVAL;
313 }
314
315 b = kzalloc(sizeof(*b), GFP_ATOMIC);
316 if (!b)
317 return -ENOMEM;
318
319 strcpy(b->name, name);
320 b->media = m;
321 res = m->enable_media(net, b, attr);
322 if (res) {
323 pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
324 name, -res);
325 kfree(b);
326 return -EINVAL;
327 }
328
329 b->identity = bearer_id;
330 b->tolerance = m->tolerance;
331 b->window = m->window;
332 b->domain = disc_domain;
333 b->net_plane = bearer_id + 'A';
334 b->priority = priority;
335 test_and_set_bit_lock(0, &b->up);
336
337 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
338 if (res) {
339 bearer_disable(net, b);
340 pr_warn("Bearer <%s> rejected, discovery object creation failed\n",
341 name);
342 return -EINVAL;
343 }
344
345 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
346 if (skb)
347 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
348
349 if (tipc_mon_create(net, bearer_id)) {
350 bearer_disable(net, b);
351 return -ENOMEM;
352 }
353
354 pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
355 name,
356 tipc_addr_string_fill(addr_string, disc_domain), priority);
357 return res;
358 }
359
360 /**
361 * tipc_reset_bearer - Reset all links established over this bearer
362 */
tipc_reset_bearer(struct net * net,struct tipc_bearer * b)363 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
364 {
365 pr_info("Resetting bearer <%s>\n", b->name);
366 tipc_node_delete_links(net, b->identity);
367 tipc_disc_reset(net, b);
368 return 0;
369 }
370
371 /* tipc_bearer_reset_all - reset all links on all bearers
372 */
tipc_bearer_reset_all(struct net * net)373 void tipc_bearer_reset_all(struct net *net)
374 {
375 struct tipc_bearer *b;
376 int i;
377
378 for (i = 0; i < MAX_BEARERS; i++) {
379 b = bearer_get(net, i);
380 if (b)
381 clear_bit_unlock(0, &b->up);
382 }
383 for (i = 0; i < MAX_BEARERS; i++) {
384 b = bearer_get(net, i);
385 if (b)
386 tipc_reset_bearer(net, b);
387 }
388 for (i = 0; i < MAX_BEARERS; i++) {
389 b = bearer_get(net, i);
390 if (b)
391 test_and_set_bit_lock(0, &b->up);
392 }
393 }
394
395 /**
396 * bearer_disable
397 *
398 * Note: This routine assumes caller holds RTNL lock.
399 */
bearer_disable(struct net * net,struct tipc_bearer * b)400 static void bearer_disable(struct net *net, struct tipc_bearer *b)
401 {
402 struct tipc_net *tn = tipc_net(net);
403 int bearer_id = b->identity;
404
405 pr_info("Disabling bearer <%s>\n", b->name);
406 clear_bit_unlock(0, &b->up);
407 tipc_node_delete_links(net, bearer_id);
408 b->media->disable_media(b);
409 RCU_INIT_POINTER(b->media_ptr, NULL);
410 if (b->link_req)
411 tipc_disc_delete(b->link_req);
412 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
413 kfree_rcu(b, rcu);
414 tipc_mon_delete(net, bearer_id);
415 }
416
tipc_enable_l2_media(struct net * net,struct tipc_bearer * b,struct nlattr * attr[])417 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
418 struct nlattr *attr[])
419 {
420 struct net_device *dev;
421 char *driver_name = strchr((const char *)b->name, ':') + 1;
422
423 /* Find device with specified name */
424 dev = dev_get_by_name(net, driver_name);
425 if (!dev)
426 return -ENODEV;
427 if (tipc_mtu_bad(dev, 0)) {
428 dev_put(dev);
429 return -EINVAL;
430 }
431
432 /* Associate TIPC bearer with L2 bearer */
433 rcu_assign_pointer(b->media_ptr, dev);
434 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
435 memcpy(b->bcast_addr.value, dev->broadcast, b->media->hwaddr_len);
436 b->bcast_addr.media_id = b->media->type_id;
437 b->bcast_addr.broadcast = 1;
438 b->mtu = dev->mtu;
439 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
440 rcu_assign_pointer(dev->tipc_ptr, b);
441 return 0;
442 }
443
444 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
445 *
446 * Mark L2 bearer as inactive so that incoming buffers are thrown away
447 */
tipc_disable_l2_media(struct tipc_bearer * b)448 void tipc_disable_l2_media(struct tipc_bearer *b)
449 {
450 struct net_device *dev;
451
452 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
453 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
454 synchronize_net();
455 dev_put(dev);
456 }
457
458 /**
459 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
460 * @skb: the packet to be sent
461 * @b: the bearer through which the packet is to be sent
462 * @dest: peer destination address
463 */
tipc_l2_send_msg(struct net * net,struct sk_buff * skb,struct tipc_bearer * b,struct tipc_media_addr * dest)464 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
465 struct tipc_bearer *b, struct tipc_media_addr *dest)
466 {
467 struct net_device *dev;
468 int delta;
469
470 dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr);
471 if (!dev)
472 return 0;
473
474 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
475 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
476 kfree_skb(skb);
477 return 0;
478 }
479 skb_reset_network_header(skb);
480 skb->dev = dev;
481 skb->protocol = htons(ETH_P_TIPC);
482 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
483 dev->dev_addr, skb->len);
484 dev_queue_xmit(skb);
485 return 0;
486 }
487
tipc_bearer_mtu(struct net * net,u32 bearer_id)488 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
489 {
490 int mtu = 0;
491 struct tipc_bearer *b;
492
493 rcu_read_lock();
494 b = rcu_dereference_rtnl(tipc_net(net)->bearer_list[bearer_id]);
495 if (b)
496 mtu = b->mtu;
497 rcu_read_unlock();
498 return mtu;
499 }
500
501 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
502 */
tipc_bearer_xmit_skb(struct net * net,u32 bearer_id,struct sk_buff * skb,struct tipc_media_addr * dest)503 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
504 struct sk_buff *skb,
505 struct tipc_media_addr *dest)
506 {
507 struct tipc_msg *hdr = buf_msg(skb);
508 struct tipc_bearer *b;
509
510 rcu_read_lock();
511 b = bearer_get(net, bearer_id);
512 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr))))
513 b->media->send_msg(net, skb, b, dest);
514 else
515 kfree_skb(skb);
516 rcu_read_unlock();
517 }
518
519 /* tipc_bearer_xmit() -send buffer to destination over bearer
520 */
tipc_bearer_xmit(struct net * net,u32 bearer_id,struct sk_buff_head * xmitq,struct tipc_media_addr * dst)521 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
522 struct sk_buff_head *xmitq,
523 struct tipc_media_addr *dst)
524 {
525 struct tipc_bearer *b;
526 struct sk_buff *skb, *tmp;
527
528 if (skb_queue_empty(xmitq))
529 return;
530
531 rcu_read_lock();
532 b = bearer_get(net, bearer_id);
533 if (unlikely(!b))
534 __skb_queue_purge(xmitq);
535 skb_queue_walk_safe(xmitq, skb, tmp) {
536 __skb_dequeue(xmitq);
537 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb))))
538 b->media->send_msg(net, skb, b, dst);
539 else
540 kfree_skb(skb);
541 }
542 rcu_read_unlock();
543 }
544
545 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
546 */
tipc_bearer_bc_xmit(struct net * net,u32 bearer_id,struct sk_buff_head * xmitq)547 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
548 struct sk_buff_head *xmitq)
549 {
550 struct tipc_net *tn = tipc_net(net);
551 int net_id = tn->net_id;
552 struct tipc_bearer *b;
553 struct sk_buff *skb, *tmp;
554 struct tipc_msg *hdr;
555
556 rcu_read_lock();
557 b = bearer_get(net, bearer_id);
558 if (unlikely(!b || !test_bit(0, &b->up)))
559 __skb_queue_purge(xmitq);
560 skb_queue_walk_safe(xmitq, skb, tmp) {
561 hdr = buf_msg(skb);
562 msg_set_non_seq(hdr, 1);
563 msg_set_mc_netid(hdr, net_id);
564 __skb_dequeue(xmitq);
565 b->media->send_msg(net, skb, b, &b->bcast_addr);
566 }
567 rcu_read_unlock();
568 }
569
570 /**
571 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
572 * @buf: the received packet
573 * @dev: the net device that the packet was received on
574 * @pt: the packet_type structure which was used to register this handler
575 * @orig_dev: the original receive net device in case the device is a bond
576 *
577 * Accept only packets explicitly sent to this node, or broadcast packets;
578 * ignores packets sent using interface multicast, and traffic sent to other
579 * nodes (which can happen if interface is running in promiscuous mode).
580 */
tipc_l2_rcv_msg(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)581 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
582 struct packet_type *pt, struct net_device *orig_dev)
583 {
584 struct tipc_bearer *b;
585
586 rcu_read_lock();
587 b = rcu_dereference_rtnl(dev->tipc_ptr);
588 if (likely(b && test_bit(0, &b->up) &&
589 (skb->pkt_type <= PACKET_BROADCAST))) {
590 skb->next = NULL;
591 tipc_rcv(dev_net(dev), skb, b);
592 rcu_read_unlock();
593 return NET_RX_SUCCESS;
594 }
595 rcu_read_unlock();
596 kfree_skb(skb);
597 return NET_RX_DROP;
598 }
599
600 /**
601 * tipc_l2_device_event - handle device events from network device
602 * @nb: the context of the notification
603 * @evt: the type of event
604 * @ptr: the net device that the event was on
605 *
606 * This function is called by the Ethernet driver in case of link
607 * change event.
608 */
tipc_l2_device_event(struct notifier_block * nb,unsigned long evt,void * ptr)609 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
610 void *ptr)
611 {
612 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
613 struct net *net = dev_net(dev);
614 struct tipc_bearer *b;
615
616 b = rtnl_dereference(dev->tipc_ptr);
617 if (!b)
618 return NOTIFY_DONE;
619
620 switch (evt) {
621 case NETDEV_CHANGE:
622 if (netif_carrier_ok(dev))
623 break;
624 case NETDEV_UP:
625 test_and_set_bit_lock(0, &b->up);
626 break;
627 case NETDEV_GOING_DOWN:
628 clear_bit_unlock(0, &b->up);
629 tipc_reset_bearer(net, b);
630 break;
631 case NETDEV_CHANGEMTU:
632 if (tipc_mtu_bad(dev, 0)) {
633 bearer_disable(net, b);
634 break;
635 }
636 b->mtu = dev->mtu;
637 tipc_reset_bearer(net, b);
638 break;
639 case NETDEV_CHANGEADDR:
640 b->media->raw2addr(b, &b->addr,
641 (char *)dev->dev_addr);
642 tipc_reset_bearer(net, b);
643 break;
644 case NETDEV_UNREGISTER:
645 case NETDEV_CHANGENAME:
646 bearer_disable(dev_net(dev), b);
647 break;
648 }
649 return NOTIFY_OK;
650 }
651
652 static struct packet_type tipc_packet_type __read_mostly = {
653 .type = htons(ETH_P_TIPC),
654 .func = tipc_l2_rcv_msg,
655 };
656
657 static struct notifier_block notifier = {
658 .notifier_call = tipc_l2_device_event,
659 .priority = 0,
660 };
661
tipc_bearer_setup(void)662 int tipc_bearer_setup(void)
663 {
664 int err;
665
666 err = register_netdevice_notifier(¬ifier);
667 if (err)
668 return err;
669 dev_add_pack(&tipc_packet_type);
670 return 0;
671 }
672
tipc_bearer_cleanup(void)673 void tipc_bearer_cleanup(void)
674 {
675 unregister_netdevice_notifier(¬ifier);
676 dev_remove_pack(&tipc_packet_type);
677 }
678
tipc_bearer_stop(struct net * net)679 void tipc_bearer_stop(struct net *net)
680 {
681 struct tipc_net *tn = net_generic(net, tipc_net_id);
682 struct tipc_bearer *b;
683 u32 i;
684
685 for (i = 0; i < MAX_BEARERS; i++) {
686 b = rtnl_dereference(tn->bearer_list[i]);
687 if (b) {
688 bearer_disable(net, b);
689 tn->bearer_list[i] = NULL;
690 }
691 }
692 }
693
694 /* Caller should hold rtnl_lock to protect the bearer */
__tipc_nl_add_bearer(struct tipc_nl_msg * msg,struct tipc_bearer * bearer,int nlflags)695 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
696 struct tipc_bearer *bearer, int nlflags)
697 {
698 void *hdr;
699 struct nlattr *attrs;
700 struct nlattr *prop;
701
702 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
703 nlflags, TIPC_NL_BEARER_GET);
704 if (!hdr)
705 return -EMSGSIZE;
706
707 attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
708 if (!attrs)
709 goto msg_full;
710
711 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
712 goto attr_msg_full;
713
714 prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
715 if (!prop)
716 goto prop_msg_full;
717 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
718 goto prop_msg_full;
719 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
720 goto prop_msg_full;
721 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
722 goto prop_msg_full;
723
724 nla_nest_end(msg->skb, prop);
725
726 #ifdef CONFIG_TIPC_MEDIA_UDP
727 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
728 if (tipc_udp_nl_add_bearer_data(msg, bearer))
729 goto attr_msg_full;
730 }
731 #endif
732
733 nla_nest_end(msg->skb, attrs);
734 genlmsg_end(msg->skb, hdr);
735
736 return 0;
737
738 prop_msg_full:
739 nla_nest_cancel(msg->skb, prop);
740 attr_msg_full:
741 nla_nest_cancel(msg->skb, attrs);
742 msg_full:
743 genlmsg_cancel(msg->skb, hdr);
744
745 return -EMSGSIZE;
746 }
747
tipc_nl_bearer_dump(struct sk_buff * skb,struct netlink_callback * cb)748 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
749 {
750 int err;
751 int i = cb->args[0];
752 struct tipc_bearer *bearer;
753 struct tipc_nl_msg msg;
754 struct net *net = sock_net(skb->sk);
755 struct tipc_net *tn = net_generic(net, tipc_net_id);
756
757 if (i == MAX_BEARERS)
758 return 0;
759
760 msg.skb = skb;
761 msg.portid = NETLINK_CB(cb->skb).portid;
762 msg.seq = cb->nlh->nlmsg_seq;
763
764 rtnl_lock();
765 for (i = 0; i < MAX_BEARERS; i++) {
766 bearer = rtnl_dereference(tn->bearer_list[i]);
767 if (!bearer)
768 continue;
769
770 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
771 if (err)
772 break;
773 }
774 rtnl_unlock();
775
776 cb->args[0] = i;
777 return skb->len;
778 }
779
tipc_nl_bearer_get(struct sk_buff * skb,struct genl_info * info)780 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
781 {
782 int err;
783 char *name;
784 struct sk_buff *rep;
785 struct tipc_bearer *bearer;
786 struct tipc_nl_msg msg;
787 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
788 struct net *net = genl_info_net(info);
789
790 if (!info->attrs[TIPC_NLA_BEARER])
791 return -EINVAL;
792
793 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
794 info->attrs[TIPC_NLA_BEARER],
795 tipc_nl_bearer_policy);
796 if (err)
797 return err;
798
799 if (!attrs[TIPC_NLA_BEARER_NAME])
800 return -EINVAL;
801 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
802
803 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
804 if (!rep)
805 return -ENOMEM;
806
807 msg.skb = rep;
808 msg.portid = info->snd_portid;
809 msg.seq = info->snd_seq;
810
811 rtnl_lock();
812 bearer = tipc_bearer_find(net, name);
813 if (!bearer) {
814 err = -EINVAL;
815 goto err_out;
816 }
817
818 err = __tipc_nl_add_bearer(&msg, bearer, 0);
819 if (err)
820 goto err_out;
821 rtnl_unlock();
822
823 return genlmsg_reply(rep, info);
824 err_out:
825 rtnl_unlock();
826 nlmsg_free(rep);
827
828 return err;
829 }
830
tipc_nl_bearer_disable(struct sk_buff * skb,struct genl_info * info)831 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
832 {
833 int err;
834 char *name;
835 struct tipc_bearer *bearer;
836 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
837 struct net *net = sock_net(skb->sk);
838
839 if (!info->attrs[TIPC_NLA_BEARER])
840 return -EINVAL;
841
842 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
843 info->attrs[TIPC_NLA_BEARER],
844 tipc_nl_bearer_policy);
845 if (err)
846 return err;
847
848 if (!attrs[TIPC_NLA_BEARER_NAME])
849 return -EINVAL;
850
851 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
852
853 rtnl_lock();
854 bearer = tipc_bearer_find(net, name);
855 if (!bearer) {
856 rtnl_unlock();
857 return -EINVAL;
858 }
859
860 bearer_disable(net, bearer);
861 rtnl_unlock();
862
863 return 0;
864 }
865
tipc_nl_bearer_enable(struct sk_buff * skb,struct genl_info * info)866 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
867 {
868 int err;
869 char *bearer;
870 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
871 struct net *net = sock_net(skb->sk);
872 struct tipc_net *tn = net_generic(net, tipc_net_id);
873 u32 domain;
874 u32 prio;
875
876 prio = TIPC_MEDIA_LINK_PRI;
877 domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK;
878
879 if (!info->attrs[TIPC_NLA_BEARER])
880 return -EINVAL;
881
882 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
883 info->attrs[TIPC_NLA_BEARER],
884 tipc_nl_bearer_policy);
885 if (err)
886 return err;
887
888 if (!attrs[TIPC_NLA_BEARER_NAME])
889 return -EINVAL;
890
891 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
892
893 if (attrs[TIPC_NLA_BEARER_DOMAIN])
894 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
895
896 if (attrs[TIPC_NLA_BEARER_PROP]) {
897 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
898
899 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
900 props);
901 if (err)
902 return err;
903
904 if (props[TIPC_NLA_PROP_PRIO])
905 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
906 }
907
908 rtnl_lock();
909 err = tipc_enable_bearer(net, bearer, domain, prio, attrs);
910 if (err) {
911 rtnl_unlock();
912 return err;
913 }
914 rtnl_unlock();
915
916 return 0;
917 }
918
tipc_nl_bearer_add(struct sk_buff * skb,struct genl_info * info)919 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
920 {
921 int err;
922 char *name;
923 struct tipc_bearer *b;
924 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
925 struct net *net = sock_net(skb->sk);
926
927 if (!info->attrs[TIPC_NLA_BEARER])
928 return -EINVAL;
929
930 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
931 info->attrs[TIPC_NLA_BEARER],
932 tipc_nl_bearer_policy);
933 if (err)
934 return err;
935
936 if (!attrs[TIPC_NLA_BEARER_NAME])
937 return -EINVAL;
938 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
939
940 rtnl_lock();
941 b = tipc_bearer_find(net, name);
942 if (!b) {
943 rtnl_unlock();
944 return -EINVAL;
945 }
946
947 #ifdef CONFIG_TIPC_MEDIA_UDP
948 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
949 err = tipc_udp_nl_bearer_add(b,
950 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
951 if (err) {
952 rtnl_unlock();
953 return err;
954 }
955 }
956 #endif
957 rtnl_unlock();
958
959 return 0;
960 }
961
tipc_nl_bearer_set(struct sk_buff * skb,struct genl_info * info)962 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
963 {
964 int err;
965 char *name;
966 struct tipc_bearer *b;
967 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
968 struct net *net = sock_net(skb->sk);
969
970 if (!info->attrs[TIPC_NLA_BEARER])
971 return -EINVAL;
972
973 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
974 info->attrs[TIPC_NLA_BEARER],
975 tipc_nl_bearer_policy);
976 if (err)
977 return err;
978
979 if (!attrs[TIPC_NLA_BEARER_NAME])
980 return -EINVAL;
981 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
982
983 rtnl_lock();
984 b = tipc_bearer_find(net, name);
985 if (!b) {
986 rtnl_unlock();
987 return -EINVAL;
988 }
989
990 if (attrs[TIPC_NLA_BEARER_PROP]) {
991 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
992
993 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
994 props);
995 if (err) {
996 rtnl_unlock();
997 return err;
998 }
999
1000 if (props[TIPC_NLA_PROP_TOL])
1001 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1002 if (props[TIPC_NLA_PROP_PRIO])
1003 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1004 if (props[TIPC_NLA_PROP_WIN])
1005 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1006 }
1007 rtnl_unlock();
1008
1009 return 0;
1010 }
1011
__tipc_nl_add_media(struct tipc_nl_msg * msg,struct tipc_media * media,int nlflags)1012 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1013 struct tipc_media *media, int nlflags)
1014 {
1015 void *hdr;
1016 struct nlattr *attrs;
1017 struct nlattr *prop;
1018
1019 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1020 nlflags, TIPC_NL_MEDIA_GET);
1021 if (!hdr)
1022 return -EMSGSIZE;
1023
1024 attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
1025 if (!attrs)
1026 goto msg_full;
1027
1028 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1029 goto attr_msg_full;
1030
1031 prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
1032 if (!prop)
1033 goto prop_msg_full;
1034 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1035 goto prop_msg_full;
1036 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1037 goto prop_msg_full;
1038 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
1039 goto prop_msg_full;
1040
1041 nla_nest_end(msg->skb, prop);
1042 nla_nest_end(msg->skb, attrs);
1043 genlmsg_end(msg->skb, hdr);
1044
1045 return 0;
1046
1047 prop_msg_full:
1048 nla_nest_cancel(msg->skb, prop);
1049 attr_msg_full:
1050 nla_nest_cancel(msg->skb, attrs);
1051 msg_full:
1052 genlmsg_cancel(msg->skb, hdr);
1053
1054 return -EMSGSIZE;
1055 }
1056
tipc_nl_media_dump(struct sk_buff * skb,struct netlink_callback * cb)1057 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1058 {
1059 int err;
1060 int i = cb->args[0];
1061 struct tipc_nl_msg msg;
1062
1063 if (i == MAX_MEDIA)
1064 return 0;
1065
1066 msg.skb = skb;
1067 msg.portid = NETLINK_CB(cb->skb).portid;
1068 msg.seq = cb->nlh->nlmsg_seq;
1069
1070 rtnl_lock();
1071 for (; media_info_array[i] != NULL; i++) {
1072 err = __tipc_nl_add_media(&msg, media_info_array[i],
1073 NLM_F_MULTI);
1074 if (err)
1075 break;
1076 }
1077 rtnl_unlock();
1078
1079 cb->args[0] = i;
1080 return skb->len;
1081 }
1082
tipc_nl_media_get(struct sk_buff * skb,struct genl_info * info)1083 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1084 {
1085 int err;
1086 char *name;
1087 struct tipc_nl_msg msg;
1088 struct tipc_media *media;
1089 struct sk_buff *rep;
1090 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1091
1092 if (!info->attrs[TIPC_NLA_MEDIA])
1093 return -EINVAL;
1094
1095 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
1096 info->attrs[TIPC_NLA_MEDIA],
1097 tipc_nl_media_policy);
1098 if (err)
1099 return err;
1100
1101 if (!attrs[TIPC_NLA_MEDIA_NAME])
1102 return -EINVAL;
1103 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1104
1105 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1106 if (!rep)
1107 return -ENOMEM;
1108
1109 msg.skb = rep;
1110 msg.portid = info->snd_portid;
1111 msg.seq = info->snd_seq;
1112
1113 rtnl_lock();
1114 media = tipc_media_find(name);
1115 if (!media) {
1116 err = -EINVAL;
1117 goto err_out;
1118 }
1119
1120 err = __tipc_nl_add_media(&msg, media, 0);
1121 if (err)
1122 goto err_out;
1123 rtnl_unlock();
1124
1125 return genlmsg_reply(rep, info);
1126 err_out:
1127 rtnl_unlock();
1128 nlmsg_free(rep);
1129
1130 return err;
1131 }
1132
tipc_nl_media_set(struct sk_buff * skb,struct genl_info * info)1133 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1134 {
1135 int err;
1136 char *name;
1137 struct tipc_media *m;
1138 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1139
1140 if (!info->attrs[TIPC_NLA_MEDIA])
1141 return -EINVAL;
1142
1143 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
1144 info->attrs[TIPC_NLA_MEDIA],
1145 tipc_nl_media_policy);
1146
1147 if (!attrs[TIPC_NLA_MEDIA_NAME])
1148 return -EINVAL;
1149 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1150
1151 rtnl_lock();
1152 m = tipc_media_find(name);
1153 if (!m) {
1154 rtnl_unlock();
1155 return -EINVAL;
1156 }
1157
1158 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1159 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1160
1161 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1162 props);
1163 if (err) {
1164 rtnl_unlock();
1165 return err;
1166 }
1167
1168 if (props[TIPC_NLA_PROP_TOL])
1169 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1170 if (props[TIPC_NLA_PROP_PRIO])
1171 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1172 if (props[TIPC_NLA_PROP_WIN])
1173 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1174 }
1175 rtnl_unlock();
1176
1177 return 0;
1178 }
1179