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