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
244 static int tipc_enable_bearer(struct net *net, const char *name,
245 u32 disc_domain, u32 prio,
246 struct nlattr *attr[])
248 struct tipc_net *tn = tipc_net(net);
249 struct tipc_bearer_names b_names;
250 int with_this_prio = 1;
251 struct tipc_bearer *b;
252 struct tipc_media *m;
258 if (!bearer_name_validate(name, &b_names)) {
259 errstr = "illegal name";
263 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
264 errstr = "illegal priority";
268 m = tipc_media_find(b_names.media_name);
270 errstr = "media not registered";
274 if (prio == TIPC_MEDIA_LINK_PRI)
277 /* Check new bearer vs existing ones and find free bearer id if any */
278 while (bearer_id < MAX_BEARERS) {
279 b = rtnl_dereference(tn->bearer_list[bearer_id]);
282 if (!strcmp(name, b->name)) {
283 errstr = "already enabled";
287 if (b->priority != prio)
289 if (++with_this_prio <= 2)
291 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
293 if (prio == TIPC_MIN_LINK_PRI) {
294 errstr = "cannot adjust to lower";
297 pr_warn("Bearer <%s>: trying with adjusted priority\n", name);
303 if (bearer_id >= MAX_BEARERS) {
304 errstr = "max 3 bearers permitted";
308 b = kzalloc(sizeof(*b), GFP_ATOMIC);
312 strcpy(b->name, name);
314 res = m->enable_media(net, b, attr);
317 errstr = "failed to enable media";
321 b->identity = bearer_id;
322 b->tolerance = m->tolerance;
323 b->min_win = m->min_win;
324 b->max_win = m->max_win;
325 b->domain = disc_domain;
326 b->net_plane = bearer_id + 'A';
328 refcount_set(&b->refcnt, 1);
330 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
332 bearer_disable(net, b);
333 errstr = "failed to create discoverer";
337 test_and_set_bit_lock(0, &b->up);
338 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
340 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
342 if (tipc_mon_create(net, bearer_id)) {
343 bearer_disable(net, b);
347 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
351 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
356 * tipc_reset_bearer - Reset all links established over this bearer
357 * @net: the applicable net namespace
358 * @b: the target bearer
360 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
362 pr_info("Resetting bearer <%s>\n", b->name);
363 tipc_node_delete_links(net, b->identity);
364 tipc_disc_reset(net, b);
368 bool tipc_bearer_hold(struct tipc_bearer *b)
370 return (b && refcount_inc_not_zero(&b->refcnt));
373 void tipc_bearer_put(struct tipc_bearer *b)
375 if (b && refcount_dec_and_test(&b->refcnt))
380 * bearer_disable - disable this bearer
381 * @net: the applicable net namespace
382 * @b: the bearer to disable
384 * Note: This routine assumes caller holds RTNL lock.
386 static void bearer_disable(struct net *net, struct tipc_bearer *b)
388 struct tipc_net *tn = tipc_net(net);
389 int bearer_id = b->identity;
391 pr_info("Disabling bearer <%s>\n", b->name);
392 clear_bit_unlock(0, &b->up);
393 tipc_node_delete_links(net, bearer_id);
394 b->media->disable_media(b);
395 RCU_INIT_POINTER(b->media_ptr, NULL);
397 tipc_disc_delete(b->disc);
398 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
400 tipc_mon_delete(net, bearer_id);
403 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
404 struct nlattr *attr[])
406 char *dev_name = strchr((const char *)b->name, ':') + 1;
407 int hwaddr_len = b->media->hwaddr_len;
408 u8 node_id[NODE_ID_LEN] = {0,};
409 struct net_device *dev;
411 /* Find device with specified name */
412 dev = dev_get_by_name(net, dev_name);
415 if (tipc_mtu_bad(dev, 0)) {
419 if (dev == net->loopback_dev) {
421 pr_info("Enabling <%s> not permitted\n", b->name);
425 /* Autoconfigure own node identity if needed */
426 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
427 memcpy(node_id, dev->dev_addr, hwaddr_len);
428 tipc_net_init(net, node_id, 0);
430 if (!tipc_own_id(net)) {
432 pr_warn("Failed to obtain node identity\n");
436 /* Associate TIPC bearer with L2 bearer */
437 rcu_assign_pointer(b->media_ptr, dev);
439 b->pt.type = htons(ETH_P_TIPC);
440 b->pt.func = tipc_l2_rcv_msg;
441 dev_add_pack(&b->pt);
442 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
443 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
444 b->bcast_addr.media_id = b->media->type_id;
445 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
447 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
448 rcu_assign_pointer(dev->tipc_ptr, b);
452 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
453 * @b: the target bearer
455 * Mark L2 bearer as inactive so that incoming buffers are thrown away
457 void tipc_disable_l2_media(struct tipc_bearer *b)
459 struct net_device *dev;
461 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
462 dev_remove_pack(&b->pt);
463 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
469 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
470 * @net: the associated network namespace
471 * @skb: the packet to be sent
472 * @b: the bearer through which the packet is to be sent
473 * @dest: peer destination address
475 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
476 struct tipc_bearer *b, struct tipc_media_addr *dest)
478 struct net_device *dev;
481 dev = (struct net_device *)rcu_dereference(b->media_ptr);
485 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
486 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
490 skb_reset_network_header(skb);
492 skb->protocol = htons(ETH_P_TIPC);
493 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
494 dev->dev_addr, skb->len);
499 bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
502 struct tipc_bearer *b;
505 b = bearer_get(net, bearer_id);
507 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
512 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
515 struct tipc_bearer *b;
518 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
525 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
527 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
529 struct tipc_media_addr *dest)
531 struct tipc_msg *hdr = buf_msg(skb);
532 struct tipc_bearer *b;
535 b = bearer_get(net, bearer_id);
536 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
537 #ifdef CONFIG_TIPC_CRYPTO
538 tipc_crypto_xmit(net, &skb, b, dest, NULL);
541 b->media->send_msg(net, skb, b, dest);
548 /* tipc_bearer_xmit() -send buffer to destination over bearer
550 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
551 struct sk_buff_head *xmitq,
552 struct tipc_media_addr *dst,
553 struct tipc_node *__dnode)
555 struct tipc_bearer *b;
556 struct sk_buff *skb, *tmp;
558 if (skb_queue_empty(xmitq))
562 b = bearer_get(net, bearer_id);
564 __skb_queue_purge(xmitq);
565 skb_queue_walk_safe(xmitq, skb, tmp) {
566 __skb_dequeue(xmitq);
567 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
568 #ifdef CONFIG_TIPC_CRYPTO
569 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
572 b->media->send_msg(net, skb, b, dst);
580 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
582 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
583 struct sk_buff_head *xmitq)
585 struct tipc_net *tn = tipc_net(net);
586 struct tipc_media_addr *dst;
587 int net_id = tn->net_id;
588 struct tipc_bearer *b;
589 struct sk_buff *skb, *tmp;
590 struct tipc_msg *hdr;
593 b = bearer_get(net, bearer_id);
594 if (unlikely(!b || !test_bit(0, &b->up)))
595 __skb_queue_purge(xmitq);
596 skb_queue_walk_safe(xmitq, skb, tmp) {
598 msg_set_non_seq(hdr, 1);
599 msg_set_mc_netid(hdr, net_id);
600 __skb_dequeue(xmitq);
601 dst = &b->bcast_addr;
602 #ifdef CONFIG_TIPC_CRYPTO
603 tipc_crypto_xmit(net, &skb, b, dst, NULL);
606 b->media->send_msg(net, skb, b, dst);
612 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
613 * @skb: the received message
614 * @dev: the net device that the packet was received on
615 * @pt: the packet_type structure which was used to register this handler
616 * @orig_dev: the original receive net device in case the device is a bond
618 * Accept only packets explicitly sent to this node, or broadcast packets;
619 * ignores packets sent using interface multicast, and traffic sent to other
620 * nodes (which can happen if interface is running in promiscuous mode).
622 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
623 struct packet_type *pt, struct net_device *orig_dev)
625 struct tipc_bearer *b;
628 b = rcu_dereference(dev->tipc_ptr) ?:
629 rcu_dereference(orig_dev->tipc_ptr);
630 if (likely(b && test_bit(0, &b->up) &&
631 (skb->pkt_type <= PACKET_MULTICAST))) {
632 skb_mark_not_on_list(skb);
633 TIPC_SKB_CB(skb)->flags = 0;
634 tipc_rcv(dev_net(b->pt.dev), skb, b);
636 return NET_RX_SUCCESS;
644 * tipc_l2_device_event - handle device events from network device
645 * @nb: the context of the notification
646 * @evt: the type of event
647 * @ptr: the net device that the event was on
649 * This function is called by the Ethernet driver in case of link
652 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
655 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
656 struct net *net = dev_net(dev);
657 struct tipc_bearer *b;
659 b = rtnl_dereference(dev->tipc_ptr);
663 trace_tipc_l2_device_event(dev, b, evt);
666 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
667 test_and_set_bit_lock(0, &b->up);
671 case NETDEV_GOING_DOWN:
672 clear_bit_unlock(0, &b->up);
673 tipc_reset_bearer(net, b);
676 test_and_set_bit_lock(0, &b->up);
678 case NETDEV_CHANGEMTU:
679 if (tipc_mtu_bad(dev, 0)) {
680 bearer_disable(net, b);
684 tipc_reset_bearer(net, b);
686 case NETDEV_CHANGEADDR:
687 b->media->raw2addr(b, &b->addr,
688 (char *)dev->dev_addr);
689 tipc_reset_bearer(net, b);
691 case NETDEV_UNREGISTER:
692 case NETDEV_CHANGENAME:
693 bearer_disable(net, b);
699 static struct notifier_block notifier = {
700 .notifier_call = tipc_l2_device_event,
704 int tipc_bearer_setup(void)
706 return register_netdevice_notifier(¬ifier);
709 void tipc_bearer_cleanup(void)
711 unregister_netdevice_notifier(¬ifier);
714 void tipc_bearer_stop(struct net *net)
716 struct tipc_net *tn = net_generic(net, tipc_net_id);
717 struct tipc_bearer *b;
720 for (i = 0; i < MAX_BEARERS; i++) {
721 b = rtnl_dereference(tn->bearer_list[i]);
723 bearer_disable(net, b);
724 tn->bearer_list[i] = NULL;
729 void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
731 struct net_device *dev = net->loopback_dev;
732 struct sk_buff *skb, *_skb;
735 skb_queue_walk(pkts, _skb) {
736 skb = pskb_copy(_skb, GFP_ATOMIC);
740 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
741 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
746 skb_reset_network_header(skb);
747 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
748 dev->dev_addr, skb->len);
750 skb->pkt_type = PACKET_HOST;
751 skb->ip_summed = CHECKSUM_UNNECESSARY;
752 skb->protocol = eth_type_trans(skb, dev);
757 static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
758 struct packet_type *pt, struct net_device *od)
761 return NET_RX_SUCCESS;
764 int tipc_attach_loopback(struct net *net)
766 struct net_device *dev = net->loopback_dev;
767 struct tipc_net *tn = tipc_net(net);
773 tn->loopback_pt.dev = dev;
774 tn->loopback_pt.type = htons(ETH_P_TIPC);
775 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
776 dev_add_pack(&tn->loopback_pt);
780 void tipc_detach_loopback(struct net *net)
782 struct tipc_net *tn = tipc_net(net);
784 dev_remove_pack(&tn->loopback_pt);
785 dev_put(net->loopback_dev);
788 /* Caller should hold rtnl_lock to protect the bearer */
789 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
790 struct tipc_bearer *bearer, int nlflags)
793 struct nlattr *attrs;
796 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
797 nlflags, TIPC_NL_BEARER_GET);
801 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
805 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
808 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
811 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
813 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
815 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
817 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
818 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
821 nla_nest_end(msg->skb, prop);
823 #ifdef CONFIG_TIPC_MEDIA_UDP
824 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
825 if (tipc_udp_nl_add_bearer_data(msg, bearer))
830 nla_nest_end(msg->skb, attrs);
831 genlmsg_end(msg->skb, hdr);
836 nla_nest_cancel(msg->skb, prop);
838 nla_nest_cancel(msg->skb, attrs);
840 genlmsg_cancel(msg->skb, hdr);
845 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
849 struct tipc_bearer *bearer;
850 struct tipc_nl_msg msg;
851 struct net *net = sock_net(skb->sk);
852 struct tipc_net *tn = net_generic(net, tipc_net_id);
854 if (i == MAX_BEARERS)
858 msg.portid = NETLINK_CB(cb->skb).portid;
859 msg.seq = cb->nlh->nlmsg_seq;
862 for (i = 0; i < MAX_BEARERS; i++) {
863 bearer = rtnl_dereference(tn->bearer_list[i]);
867 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
877 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
882 struct tipc_bearer *bearer;
883 struct tipc_nl_msg msg;
884 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
885 struct net *net = genl_info_net(info);
887 if (!info->attrs[TIPC_NLA_BEARER])
890 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
891 info->attrs[TIPC_NLA_BEARER],
892 tipc_nl_bearer_policy, info->extack);
896 if (!attrs[TIPC_NLA_BEARER_NAME])
898 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
900 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
905 msg.portid = info->snd_portid;
906 msg.seq = info->snd_seq;
909 bearer = tipc_bearer_find(net, name);
915 err = __tipc_nl_add_bearer(&msg, bearer, 0);
920 return genlmsg_reply(rep, info);
928 int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
932 struct tipc_bearer *bearer;
933 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
934 struct net *net = sock_net(skb->sk);
936 if (!info->attrs[TIPC_NLA_BEARER])
939 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
940 info->attrs[TIPC_NLA_BEARER],
941 tipc_nl_bearer_policy, info->extack);
945 if (!attrs[TIPC_NLA_BEARER_NAME])
948 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
950 bearer = tipc_bearer_find(net, name);
954 bearer_disable(net, bearer);
959 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
964 err = __tipc_nl_bearer_disable(skb, info);
970 int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
974 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
975 struct net *net = sock_net(skb->sk);
979 prio = TIPC_MEDIA_LINK_PRI;
981 if (!info->attrs[TIPC_NLA_BEARER])
984 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
985 info->attrs[TIPC_NLA_BEARER],
986 tipc_nl_bearer_policy, info->extack);
990 if (!attrs[TIPC_NLA_BEARER_NAME])
993 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
995 if (attrs[TIPC_NLA_BEARER_DOMAIN])
996 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
998 if (attrs[TIPC_NLA_BEARER_PROP]) {
999 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1001 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1006 if (props[TIPC_NLA_PROP_PRIO])
1007 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1010 return tipc_enable_bearer(net, bearer, domain, prio, attrs);
1013 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1018 err = __tipc_nl_bearer_enable(skb, info);
1024 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1028 struct tipc_bearer *b;
1029 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1030 struct net *net = sock_net(skb->sk);
1032 if (!info->attrs[TIPC_NLA_BEARER])
1035 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1036 info->attrs[TIPC_NLA_BEARER],
1037 tipc_nl_bearer_policy, info->extack);
1041 if (!attrs[TIPC_NLA_BEARER_NAME])
1043 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1046 b = tipc_bearer_find(net, name);
1052 #ifdef CONFIG_TIPC_MEDIA_UDP
1053 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1054 err = tipc_udp_nl_bearer_add(b,
1055 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1067 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1069 struct tipc_bearer *b;
1070 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1071 struct net *net = sock_net(skb->sk);
1075 if (!info->attrs[TIPC_NLA_BEARER])
1078 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1079 info->attrs[TIPC_NLA_BEARER],
1080 tipc_nl_bearer_policy, info->extack);
1084 if (!attrs[TIPC_NLA_BEARER_NAME])
1086 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1088 b = tipc_bearer_find(net, name);
1092 if (attrs[TIPC_NLA_BEARER_PROP]) {
1093 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1095 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1100 if (props[TIPC_NLA_PROP_TOL]) {
1101 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1102 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1104 if (props[TIPC_NLA_PROP_PRIO])
1105 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1106 if (props[TIPC_NLA_PROP_WIN])
1107 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1108 if (props[TIPC_NLA_PROP_MTU]) {
1109 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP)
1111 #ifdef CONFIG_TIPC_MEDIA_UDP
1112 if (tipc_udp_mtu_bad(nla_get_u32
1113 (props[TIPC_NLA_PROP_MTU])))
1115 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1116 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1124 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1129 err = __tipc_nl_bearer_set(skb, info);
1135 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1136 struct tipc_media *media, int nlflags)
1139 struct nlattr *attrs;
1140 struct nlattr *prop;
1142 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1143 nlflags, TIPC_NL_MEDIA_GET);
1147 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1151 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1154 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1157 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1159 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1161 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
1163 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1164 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1167 nla_nest_end(msg->skb, prop);
1168 nla_nest_end(msg->skb, attrs);
1169 genlmsg_end(msg->skb, hdr);
1174 nla_nest_cancel(msg->skb, prop);
1176 nla_nest_cancel(msg->skb, attrs);
1178 genlmsg_cancel(msg->skb, hdr);
1183 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1186 int i = cb->args[0];
1187 struct tipc_nl_msg msg;
1193 msg.portid = NETLINK_CB(cb->skb).portid;
1194 msg.seq = cb->nlh->nlmsg_seq;
1197 for (; media_info_array[i] != NULL; i++) {
1198 err = __tipc_nl_add_media(&msg, media_info_array[i],
1209 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1213 struct tipc_nl_msg msg;
1214 struct tipc_media *media;
1215 struct sk_buff *rep;
1216 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1218 if (!info->attrs[TIPC_NLA_MEDIA])
1221 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1222 info->attrs[TIPC_NLA_MEDIA],
1223 tipc_nl_media_policy, info->extack);
1227 if (!attrs[TIPC_NLA_MEDIA_NAME])
1229 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1231 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1236 msg.portid = info->snd_portid;
1237 msg.seq = info->snd_seq;
1240 media = tipc_media_find(name);
1246 err = __tipc_nl_add_media(&msg, media, 0);
1251 return genlmsg_reply(rep, info);
1259 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1263 struct tipc_media *m;
1264 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1266 if (!info->attrs[TIPC_NLA_MEDIA])
1269 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1270 info->attrs[TIPC_NLA_MEDIA],
1271 tipc_nl_media_policy, info->extack);
1273 if (!attrs[TIPC_NLA_MEDIA_NAME])
1275 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1277 m = tipc_media_find(name);
1281 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1282 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1284 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1289 if (props[TIPC_NLA_PROP_TOL])
1290 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1291 if (props[TIPC_NLA_PROP_PRIO])
1292 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1293 if (props[TIPC_NLA_PROP_WIN])
1294 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1295 if (props[TIPC_NLA_PROP_MTU]) {
1296 if (m->type_id != TIPC_MEDIA_TYPE_UDP)
1298 #ifdef CONFIG_TIPC_MEDIA_UDP
1299 if (tipc_udp_mtu_bad(nla_get_u32
1300 (props[TIPC_NLA_PROP_MTU])))
1302 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1310 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1315 err = __tipc_nl_media_set(skb, info);