2 * net/tipc/bearer.c: TIPC bearer code
4 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
5 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
45 #include "udp_media.h"
49 #define MAX_ADDR_STR 60
51 static struct tipc_media * const media_info_array[] = {
53 #ifdef CONFIG_TIPC_MEDIA_IB
56 #ifdef CONFIG_TIPC_MEDIA_UDP
62 static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
64 struct tipc_net *tn = tipc_net(net);
66 return rcu_dereference(tn->bearer_list[bearer_id]);
69 static void bearer_disable(struct net *net, struct tipc_bearer *b);
70 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
71 struct packet_type *pt, struct net_device *orig_dev);
74 * tipc_media_find - locates specified media object by name
75 * @name: name to locate
77 struct tipc_media *tipc_media_find(const char *name)
81 for (i = 0; media_info_array[i] != NULL; i++) {
82 if (!strcmp(media_info_array[i]->name, name))
85 return media_info_array[i];
89 * media_find_id - locates specified media object by type identifier
90 * @type: type identifier to locate
92 static struct tipc_media *media_find_id(u8 type)
96 for (i = 0; media_info_array[i] != NULL; i++) {
97 if (media_info_array[i]->type_id == type)
100 return media_info_array[i];
104 * tipc_media_addr_printf - record media address in print buffer
105 * @buf: output buffer
106 * @len: output buffer size remaining
107 * @a: input media address
109 int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
111 char addr_str[MAX_ADDR_STR];
112 struct tipc_media *m;
115 m = media_find_id(a->media_id);
117 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
118 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
122 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
123 for (i = 0; i < sizeof(a->value); i++)
124 ret += scnprintf(buf + ret, len - ret,
131 * bearer_name_validate - validate & (optionally) deconstruct bearer name
132 * @name: ptr to bearer name string
133 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
135 * Return: 1 if bearer name is valid, otherwise 0.
137 static int bearer_name_validate(const char *name,
138 struct tipc_bearer_names *name_parts)
140 char name_copy[TIPC_MAX_BEARER_NAME];
146 /* copy bearer name & ensure length is OK */
147 if (strscpy(name_copy, name, TIPC_MAX_BEARER_NAME) < 0)
150 /* ensure all component parts of bearer name are present */
151 media_name = name_copy;
152 if_name = strchr(media_name, ':');
156 media_len = if_name - media_name;
157 if_len = strlen(if_name) + 1;
159 /* validate component parts of bearer name */
160 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
161 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
164 /* return bearer name components, if necessary */
166 strcpy(name_parts->media_name, media_name);
167 strcpy(name_parts->if_name, if_name);
173 * tipc_bearer_find - locates bearer object with matching bearer name
174 * @net: the applicable net namespace
175 * @name: bearer name to locate
177 struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
179 struct tipc_net *tn = net_generic(net, tipc_net_id);
180 struct tipc_bearer *b;
183 for (i = 0; i < MAX_BEARERS; i++) {
184 b = rtnl_dereference(tn->bearer_list[i]);
185 if (b && (!strcmp(b->name, name)))
191 /* tipc_bearer_get_name - get the bearer name from its id.
192 * @net: network namespace
193 * @name: a pointer to the buffer where the name will be stored.
194 * @bearer_id: the id to get the name from.
196 int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
198 struct tipc_net *tn = tipc_net(net);
199 struct tipc_bearer *b;
201 if (bearer_id >= MAX_BEARERS)
204 b = rtnl_dereference(tn->bearer_list[bearer_id]);
208 strcpy(name, b->name);
212 void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
214 struct tipc_net *tn = net_generic(net, tipc_net_id);
215 struct tipc_bearer *b;
218 b = rcu_dereference(tn->bearer_list[bearer_id]);
220 tipc_disc_add_dest(b->disc);
224 void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
226 struct tipc_net *tn = net_generic(net, tipc_net_id);
227 struct tipc_bearer *b;
230 b = rcu_dereference(tn->bearer_list[bearer_id]);
232 tipc_disc_remove_dest(b->disc);
237 * tipc_enable_bearer - enable bearer with the given name
238 * @net: the applicable net namespace
239 * @name: bearer name to enable
240 * @disc_domain: bearer domain
241 * @prio: bearer priority
242 * @attr: nlattr array
243 * @extack: netlink extended ack
245 static int tipc_enable_bearer(struct net *net, const char *name,
246 u32 disc_domain, u32 prio,
247 struct nlattr *attr[],
248 struct netlink_ext_ack *extack)
250 struct tipc_net *tn = tipc_net(net);
251 struct tipc_bearer_names b_names;
252 int with_this_prio = 1;
253 struct tipc_bearer *b;
254 struct tipc_media *m;
261 if (!bearer_name_validate(name, &b_names)) {
262 errstr = "illegal name";
263 NL_SET_ERR_MSG(extack, "Illegal name");
267 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
268 errstr = "illegal priority";
269 NL_SET_ERR_MSG(extack, "Illegal priority");
273 m = tipc_media_find(b_names.media_name);
275 errstr = "media not registered";
276 NL_SET_ERR_MSG(extack, "Media not registered");
280 if (prio == TIPC_MEDIA_LINK_PRI)
283 /* Check new bearer vs existing ones and find free bearer id if any */
284 bearer_id = MAX_BEARERS;
287 b = rtnl_dereference(tn->bearer_list[i]);
292 if (!strcmp(name, b->name)) {
293 errstr = "already enabled";
294 NL_SET_ERR_MSG(extack, "Already enabled");
298 if (b->priority == prio &&
299 (++with_this_prio > 2)) {
300 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
303 if (prio == TIPC_MIN_LINK_PRI) {
304 errstr = "cannot adjust to lower";
305 NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
309 pr_warn("Bearer <%s>: trying with adjusted priority\n",
312 bearer_id = MAX_BEARERS;
318 if (bearer_id >= MAX_BEARERS) {
319 errstr = "max 3 bearers permitted";
320 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
324 b = kzalloc(sizeof(*b), GFP_ATOMIC);
328 strcpy(b->name, name);
330 res = m->enable_media(net, b, attr);
333 errstr = "failed to enable media";
334 NL_SET_ERR_MSG(extack, "Failed to enable media");
338 b->identity = bearer_id;
339 b->tolerance = m->tolerance;
340 b->min_win = m->min_win;
341 b->max_win = m->max_win;
342 b->domain = disc_domain;
343 b->net_plane = bearer_id + 'A';
345 refcount_set(&b->refcnt, 1);
347 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
349 bearer_disable(net, b);
350 errstr = "failed to create discoverer";
351 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
355 /* Create monitoring data before accepting activate messages */
356 if (tipc_mon_create(net, bearer_id)) {
357 bearer_disable(net, b);
362 test_and_set_bit_lock(0, &b->up);
363 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
365 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
367 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
371 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
376 * tipc_reset_bearer - Reset all links established over this bearer
377 * @net: the applicable net namespace
378 * @b: the target bearer
380 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
382 pr_info("Resetting bearer <%s>\n", b->name);
383 tipc_node_delete_links(net, b->identity);
384 tipc_disc_reset(net, b);
388 bool tipc_bearer_hold(struct tipc_bearer *b)
390 return (b && refcount_inc_not_zero(&b->refcnt));
393 void tipc_bearer_put(struct tipc_bearer *b)
395 if (b && refcount_dec_and_test(&b->refcnt))
400 * bearer_disable - disable this bearer
401 * @net: the applicable net namespace
402 * @b: the bearer to disable
404 * Note: This routine assumes caller holds RTNL lock.
406 static void bearer_disable(struct net *net, struct tipc_bearer *b)
408 struct tipc_net *tn = tipc_net(net);
409 int bearer_id = b->identity;
411 pr_info("Disabling bearer <%s>\n", b->name);
412 clear_bit_unlock(0, &b->up);
413 tipc_node_delete_links(net, bearer_id);
414 b->media->disable_media(b);
415 RCU_INIT_POINTER(b->media_ptr, NULL);
417 tipc_disc_delete(b->disc);
418 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
420 tipc_mon_delete(net, bearer_id);
423 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
424 struct nlattr *attr[])
426 char *dev_name = strchr((const char *)b->name, ':') + 1;
427 int hwaddr_len = b->media->hwaddr_len;
428 u8 node_id[NODE_ID_LEN] = {0,};
429 struct net_device *dev;
431 /* Find device with specified name */
432 dev = dev_get_by_name(net, dev_name);
435 if (tipc_mtu_bad(dev, 0)) {
439 if (dev == net->loopback_dev) {
441 pr_info("Enabling <%s> not permitted\n", b->name);
445 /* Autoconfigure own node identity if needed */
446 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
447 memcpy(node_id, dev->dev_addr, hwaddr_len);
448 tipc_net_init(net, node_id, 0);
450 if (!tipc_own_id(net)) {
452 pr_warn("Failed to obtain node identity\n");
456 /* Associate TIPC bearer with L2 bearer */
457 rcu_assign_pointer(b->media_ptr, dev);
459 b->pt.type = htons(ETH_P_TIPC);
460 b->pt.func = tipc_l2_rcv_msg;
461 dev_add_pack(&b->pt);
462 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
463 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
464 b->bcast_addr.media_id = b->media->type_id;
465 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
467 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
468 rcu_assign_pointer(dev->tipc_ptr, b);
472 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
473 * @b: the target bearer
475 * Mark L2 bearer as inactive so that incoming buffers are thrown away
477 void tipc_disable_l2_media(struct tipc_bearer *b)
479 struct net_device *dev;
481 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
482 dev_remove_pack(&b->pt);
483 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
489 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
490 * @net: the associated network namespace
491 * @skb: the packet to be sent
492 * @b: the bearer through which the packet is to be sent
493 * @dest: peer destination address
495 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
496 struct tipc_bearer *b, struct tipc_media_addr *dest)
498 struct net_device *dev;
501 dev = (struct net_device *)rcu_dereference(b->media_ptr);
505 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
506 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
510 skb_reset_network_header(skb);
512 skb->protocol = htons(ETH_P_TIPC);
513 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
514 dev->dev_addr, skb->len);
519 bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
522 struct tipc_bearer *b;
525 b = bearer_get(net, bearer_id);
527 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
532 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
535 struct tipc_bearer *b;
538 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
545 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
547 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
549 struct tipc_media_addr *dest)
551 struct tipc_msg *hdr = buf_msg(skb);
552 struct tipc_bearer *b;
555 b = bearer_get(net, bearer_id);
556 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
557 #ifdef CONFIG_TIPC_CRYPTO
558 tipc_crypto_xmit(net, &skb, b, dest, NULL);
561 b->media->send_msg(net, skb, b, dest);
568 /* tipc_bearer_xmit() -send buffer to destination over bearer
570 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
571 struct sk_buff_head *xmitq,
572 struct tipc_media_addr *dst,
573 struct tipc_node *__dnode)
575 struct tipc_bearer *b;
576 struct sk_buff *skb, *tmp;
578 if (skb_queue_empty(xmitq))
582 b = bearer_get(net, bearer_id);
584 __skb_queue_purge(xmitq);
585 skb_queue_walk_safe(xmitq, skb, tmp) {
586 __skb_dequeue(xmitq);
587 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
588 #ifdef CONFIG_TIPC_CRYPTO
589 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
592 b->media->send_msg(net, skb, b, dst);
600 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
602 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
603 struct sk_buff_head *xmitq)
605 struct tipc_net *tn = tipc_net(net);
606 struct tipc_media_addr *dst;
607 int net_id = tn->net_id;
608 struct tipc_bearer *b;
609 struct sk_buff *skb, *tmp;
610 struct tipc_msg *hdr;
613 b = bearer_get(net, bearer_id);
614 if (unlikely(!b || !test_bit(0, &b->up)))
615 __skb_queue_purge(xmitq);
616 skb_queue_walk_safe(xmitq, skb, tmp) {
618 msg_set_non_seq(hdr, 1);
619 msg_set_mc_netid(hdr, net_id);
620 __skb_dequeue(xmitq);
621 dst = &b->bcast_addr;
622 #ifdef CONFIG_TIPC_CRYPTO
623 tipc_crypto_xmit(net, &skb, b, dst, NULL);
626 b->media->send_msg(net, skb, b, dst);
632 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
633 * @skb: the received message
634 * @dev: the net device that the packet was received on
635 * @pt: the packet_type structure which was used to register this handler
636 * @orig_dev: the original receive net device in case the device is a bond
638 * Accept only packets explicitly sent to this node, or broadcast packets;
639 * ignores packets sent using interface multicast, and traffic sent to other
640 * nodes (which can happen if interface is running in promiscuous mode).
642 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
643 struct packet_type *pt, struct net_device *orig_dev)
645 struct tipc_bearer *b;
648 b = rcu_dereference(dev->tipc_ptr) ?:
649 rcu_dereference(orig_dev->tipc_ptr);
650 if (likely(b && test_bit(0, &b->up) &&
651 (skb->pkt_type <= PACKET_MULTICAST))) {
652 skb_mark_not_on_list(skb);
653 TIPC_SKB_CB(skb)->flags = 0;
654 tipc_rcv(dev_net(b->pt.dev), skb, b);
656 return NET_RX_SUCCESS;
664 * tipc_l2_device_event - handle device events from network device
665 * @nb: the context of the notification
666 * @evt: the type of event
667 * @ptr: the net device that the event was on
669 * This function is called by the Ethernet driver in case of link
672 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
675 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
676 struct net *net = dev_net(dev);
677 struct tipc_bearer *b;
679 b = rtnl_dereference(dev->tipc_ptr);
683 trace_tipc_l2_device_event(dev, b, evt);
686 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
687 test_and_set_bit_lock(0, &b->up);
691 case NETDEV_GOING_DOWN:
692 clear_bit_unlock(0, &b->up);
693 tipc_reset_bearer(net, b);
696 test_and_set_bit_lock(0, &b->up);
698 case NETDEV_CHANGEMTU:
699 if (tipc_mtu_bad(dev, 0)) {
700 bearer_disable(net, b);
704 tipc_reset_bearer(net, b);
706 case NETDEV_CHANGEADDR:
707 b->media->raw2addr(b, &b->addr,
708 (char *)dev->dev_addr);
709 tipc_reset_bearer(net, b);
711 case NETDEV_UNREGISTER:
712 case NETDEV_CHANGENAME:
713 bearer_disable(net, b);
719 static struct notifier_block notifier = {
720 .notifier_call = tipc_l2_device_event,
724 int tipc_bearer_setup(void)
726 return register_netdevice_notifier(¬ifier);
729 void tipc_bearer_cleanup(void)
731 unregister_netdevice_notifier(¬ifier);
734 void tipc_bearer_stop(struct net *net)
736 struct tipc_net *tn = net_generic(net, tipc_net_id);
737 struct tipc_bearer *b;
740 for (i = 0; i < MAX_BEARERS; i++) {
741 b = rtnl_dereference(tn->bearer_list[i]);
743 bearer_disable(net, b);
744 tn->bearer_list[i] = NULL;
749 void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
751 struct net_device *dev = net->loopback_dev;
752 struct sk_buff *skb, *_skb;
755 skb_queue_walk(pkts, _skb) {
756 skb = pskb_copy(_skb, GFP_ATOMIC);
760 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
761 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
766 skb_reset_network_header(skb);
767 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
768 dev->dev_addr, skb->len);
770 skb->pkt_type = PACKET_HOST;
771 skb->ip_summed = CHECKSUM_UNNECESSARY;
772 skb->protocol = eth_type_trans(skb, dev);
777 static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
778 struct packet_type *pt, struct net_device *od)
781 return NET_RX_SUCCESS;
784 int tipc_attach_loopback(struct net *net)
786 struct net_device *dev = net->loopback_dev;
787 struct tipc_net *tn = tipc_net(net);
793 tn->loopback_pt.dev = dev;
794 tn->loopback_pt.type = htons(ETH_P_TIPC);
795 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
796 dev_add_pack(&tn->loopback_pt);
800 void tipc_detach_loopback(struct net *net)
802 struct tipc_net *tn = tipc_net(net);
804 dev_remove_pack(&tn->loopback_pt);
805 dev_put(net->loopback_dev);
808 /* Caller should hold rtnl_lock to protect the bearer */
809 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
810 struct tipc_bearer *bearer, int nlflags)
813 struct nlattr *attrs;
816 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
817 nlflags, TIPC_NL_BEARER_GET);
821 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
825 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
828 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
831 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
833 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
835 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
837 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
838 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
841 nla_nest_end(msg->skb, prop);
843 #ifdef CONFIG_TIPC_MEDIA_UDP
844 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
845 if (tipc_udp_nl_add_bearer_data(msg, bearer))
850 nla_nest_end(msg->skb, attrs);
851 genlmsg_end(msg->skb, hdr);
856 nla_nest_cancel(msg->skb, prop);
858 nla_nest_cancel(msg->skb, attrs);
860 genlmsg_cancel(msg->skb, hdr);
865 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
869 struct tipc_bearer *bearer;
870 struct tipc_nl_msg msg;
871 struct net *net = sock_net(skb->sk);
872 struct tipc_net *tn = net_generic(net, tipc_net_id);
874 if (i == MAX_BEARERS)
878 msg.portid = NETLINK_CB(cb->skb).portid;
879 msg.seq = cb->nlh->nlmsg_seq;
882 for (i = 0; i < MAX_BEARERS; i++) {
883 bearer = rtnl_dereference(tn->bearer_list[i]);
887 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
897 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
902 struct tipc_bearer *bearer;
903 struct tipc_nl_msg msg;
904 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
905 struct net *net = genl_info_net(info);
907 if (!info->attrs[TIPC_NLA_BEARER])
910 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
911 info->attrs[TIPC_NLA_BEARER],
912 tipc_nl_bearer_policy, info->extack);
916 if (!attrs[TIPC_NLA_BEARER_NAME])
918 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
920 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
925 msg.portid = info->snd_portid;
926 msg.seq = info->snd_seq;
929 bearer = tipc_bearer_find(net, name);
932 NL_SET_ERR_MSG(info->extack, "Bearer not found");
936 err = __tipc_nl_add_bearer(&msg, bearer, 0);
941 return genlmsg_reply(rep, info);
949 int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
953 struct tipc_bearer *bearer;
954 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
955 struct net *net = sock_net(skb->sk);
957 if (!info->attrs[TIPC_NLA_BEARER])
960 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
961 info->attrs[TIPC_NLA_BEARER],
962 tipc_nl_bearer_policy, info->extack);
966 if (!attrs[TIPC_NLA_BEARER_NAME])
969 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
971 bearer = tipc_bearer_find(net, name);
973 NL_SET_ERR_MSG(info->extack, "Bearer not found");
977 bearer_disable(net, bearer);
982 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
987 err = __tipc_nl_bearer_disable(skb, info);
993 int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
997 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
998 struct net *net = sock_net(skb->sk);
1002 prio = TIPC_MEDIA_LINK_PRI;
1004 if (!info->attrs[TIPC_NLA_BEARER])
1007 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1008 info->attrs[TIPC_NLA_BEARER],
1009 tipc_nl_bearer_policy, info->extack);
1013 if (!attrs[TIPC_NLA_BEARER_NAME])
1016 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1018 if (attrs[TIPC_NLA_BEARER_DOMAIN])
1019 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
1021 if (attrs[TIPC_NLA_BEARER_PROP]) {
1022 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1024 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1029 if (props[TIPC_NLA_PROP_PRIO])
1030 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1033 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1037 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1042 err = __tipc_nl_bearer_enable(skb, info);
1048 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1052 struct tipc_bearer *b;
1053 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1054 struct net *net = sock_net(skb->sk);
1056 if (!info->attrs[TIPC_NLA_BEARER])
1059 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1060 info->attrs[TIPC_NLA_BEARER],
1061 tipc_nl_bearer_policy, info->extack);
1065 if (!attrs[TIPC_NLA_BEARER_NAME])
1067 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1070 b = tipc_bearer_find(net, name);
1073 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1077 #ifdef CONFIG_TIPC_MEDIA_UDP
1078 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1079 err = tipc_udp_nl_bearer_add(b,
1080 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1092 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1094 struct tipc_bearer *b;
1095 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1096 struct net *net = sock_net(skb->sk);
1100 if (!info->attrs[TIPC_NLA_BEARER])
1103 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1104 info->attrs[TIPC_NLA_BEARER],
1105 tipc_nl_bearer_policy, info->extack);
1109 if (!attrs[TIPC_NLA_BEARER_NAME])
1111 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1113 b = tipc_bearer_find(net, name);
1115 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1119 if (attrs[TIPC_NLA_BEARER_PROP]) {
1120 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1122 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1127 if (props[TIPC_NLA_PROP_TOL]) {
1128 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1129 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1131 if (props[TIPC_NLA_PROP_PRIO])
1132 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1133 if (props[TIPC_NLA_PROP_WIN])
1134 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1135 if (props[TIPC_NLA_PROP_MTU]) {
1136 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1137 NL_SET_ERR_MSG(info->extack,
1138 "MTU property is unsupported");
1141 #ifdef CONFIG_TIPC_MEDIA_UDP
1142 if (tipc_udp_mtu_bad(nla_get_u32
1143 (props[TIPC_NLA_PROP_MTU]))) {
1144 NL_SET_ERR_MSG(info->extack,
1145 "MTU value is out-of-range");
1148 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1149 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1157 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1162 err = __tipc_nl_bearer_set(skb, info);
1168 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1169 struct tipc_media *media, int nlflags)
1172 struct nlattr *attrs;
1173 struct nlattr *prop;
1175 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1176 nlflags, TIPC_NL_MEDIA_GET);
1180 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1184 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1187 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1190 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1192 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1194 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
1196 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1197 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1200 nla_nest_end(msg->skb, prop);
1201 nla_nest_end(msg->skb, attrs);
1202 genlmsg_end(msg->skb, hdr);
1207 nla_nest_cancel(msg->skb, prop);
1209 nla_nest_cancel(msg->skb, attrs);
1211 genlmsg_cancel(msg->skb, hdr);
1216 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1219 int i = cb->args[0];
1220 struct tipc_nl_msg msg;
1226 msg.portid = NETLINK_CB(cb->skb).portid;
1227 msg.seq = cb->nlh->nlmsg_seq;
1230 for (; media_info_array[i] != NULL; i++) {
1231 err = __tipc_nl_add_media(&msg, media_info_array[i],
1242 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1246 struct tipc_nl_msg msg;
1247 struct tipc_media *media;
1248 struct sk_buff *rep;
1249 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1251 if (!info->attrs[TIPC_NLA_MEDIA])
1254 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1255 info->attrs[TIPC_NLA_MEDIA],
1256 tipc_nl_media_policy, info->extack);
1260 if (!attrs[TIPC_NLA_MEDIA_NAME])
1262 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1264 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1269 msg.portid = info->snd_portid;
1270 msg.seq = info->snd_seq;
1273 media = tipc_media_find(name);
1275 NL_SET_ERR_MSG(info->extack, "Media not found");
1280 err = __tipc_nl_add_media(&msg, media, 0);
1285 return genlmsg_reply(rep, info);
1293 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1297 struct tipc_media *m;
1298 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1300 if (!info->attrs[TIPC_NLA_MEDIA])
1303 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1304 info->attrs[TIPC_NLA_MEDIA],
1305 tipc_nl_media_policy, info->extack);
1307 if (!attrs[TIPC_NLA_MEDIA_NAME])
1309 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1311 m = tipc_media_find(name);
1313 NL_SET_ERR_MSG(info->extack, "Media not found");
1316 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1317 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1319 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1324 if (props[TIPC_NLA_PROP_TOL])
1325 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1326 if (props[TIPC_NLA_PROP_PRIO])
1327 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1328 if (props[TIPC_NLA_PROP_WIN])
1329 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1330 if (props[TIPC_NLA_PROP_MTU]) {
1331 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1332 NL_SET_ERR_MSG(info->extack,
1333 "MTU property is unsupported");
1336 #ifdef CONFIG_TIPC_MEDIA_UDP
1337 if (tipc_udp_mtu_bad(nla_get_u32
1338 (props[TIPC_NLA_PROP_MTU]))) {
1339 NL_SET_ERR_MSG(info->extack,
1340 "MTU value is out-of-range");
1343 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1351 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1356 err = __tipc_nl_media_set(skb, info);