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
76 struct tipc_media *tipc_media_find(const char *name)
80 for (i = 0; media_info_array[i] != NULL; i++) {
81 if (!strcmp(media_info_array[i]->name, name))
84 return media_info_array[i];
88 * media_find_id - locates specified media object by type identifier
90 static struct tipc_media *media_find_id(u8 type)
94 for (i = 0; media_info_array[i] != NULL; i++) {
95 if (media_info_array[i]->type_id == type)
98 return media_info_array[i];
102 * tipc_media_addr_printf - record media address in print buffer
104 int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
106 char addr_str[MAX_ADDR_STR];
107 struct tipc_media *m;
110 m = media_find_id(a->media_id);
112 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
113 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
117 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
118 for (i = 0; i < sizeof(a->value); i++)
119 ret += scnprintf(buf + ret, len - ret,
126 * bearer_name_validate - validate & (optionally) deconstruct bearer name
127 * @name: ptr to bearer name string
128 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
130 * Returns 1 if bearer name is valid, otherwise 0.
132 static int bearer_name_validate(const char *name,
133 struct tipc_bearer_names *name_parts)
135 char name_copy[TIPC_MAX_BEARER_NAME];
141 /* copy bearer name & ensure length is OK */
142 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
143 /* need above in case non-Posix strncpy() doesn't pad with nulls */
144 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
145 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
148 /* ensure all component parts of bearer name are present */
149 media_name = name_copy;
150 if_name = strchr(media_name, ':');
154 media_len = if_name - media_name;
155 if_len = strlen(if_name) + 1;
157 /* validate component parts of bearer name */
158 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
159 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
162 /* return bearer name components, if necessary */
164 strcpy(name_parts->media_name, media_name);
165 strcpy(name_parts->if_name, if_name);
171 * tipc_bearer_find - locates bearer object with matching bearer name
173 struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
175 struct tipc_net *tn = net_generic(net, tipc_net_id);
176 struct tipc_bearer *b;
179 for (i = 0; i < MAX_BEARERS; i++) {
180 b = rtnl_dereference(tn->bearer_list[i]);
181 if (b && (!strcmp(b->name, name)))
187 /* tipc_bearer_get_name - get the bearer name from its id.
188 * @net: network namespace
189 * @name: a pointer to the buffer where the name will be stored.
190 * @bearer_id: the id to get the name from.
192 int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
194 struct tipc_net *tn = tipc_net(net);
195 struct tipc_bearer *b;
197 if (bearer_id >= MAX_BEARERS)
200 b = rtnl_dereference(tn->bearer_list[bearer_id]);
204 strcpy(name, b->name);
208 void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
210 struct tipc_net *tn = net_generic(net, tipc_net_id);
211 struct tipc_bearer *b;
214 b = rcu_dereference(tn->bearer_list[bearer_id]);
216 tipc_disc_add_dest(b->disc);
220 void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
222 struct tipc_net *tn = net_generic(net, tipc_net_id);
223 struct tipc_bearer *b;
226 b = rcu_dereference(tn->bearer_list[bearer_id]);
228 tipc_disc_remove_dest(b->disc);
233 * tipc_enable_bearer - enable bearer with the given name
235 static int tipc_enable_bearer(struct net *net, const char *name,
236 u32 disc_domain, u32 prio,
237 struct nlattr *attr[],
238 struct netlink_ext_ack *extack)
240 struct tipc_net *tn = tipc_net(net);
241 struct tipc_bearer_names b_names;
242 int with_this_prio = 1;
243 struct tipc_bearer *b;
244 struct tipc_media *m;
251 if (!bearer_name_validate(name, &b_names)) {
252 NL_SET_ERR_MSG(extack, "Illegal name");
256 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
257 errstr = "illegal priority";
258 NL_SET_ERR_MSG(extack, "Illegal priority");
262 m = tipc_media_find(b_names.media_name);
264 errstr = "media not registered";
265 NL_SET_ERR_MSG(extack, "Media not registered");
269 if (prio == TIPC_MEDIA_LINK_PRI)
272 /* Check new bearer vs existing ones and find free bearer id if any */
273 bearer_id = MAX_BEARERS;
276 b = rtnl_dereference(tn->bearer_list[i]);
281 if (!strcmp(name, b->name)) {
282 errstr = "already enabled";
283 NL_SET_ERR_MSG(extack, "Already enabled");
287 if (b->priority == prio &&
288 (++with_this_prio > 2)) {
289 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
292 if (prio == TIPC_MIN_LINK_PRI) {
293 errstr = "cannot adjust to lower";
294 NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
298 pr_warn("Bearer <%s>: trying with adjusted priority\n",
301 bearer_id = MAX_BEARERS;
307 if (bearer_id >= MAX_BEARERS) {
308 errstr = "max 3 bearers permitted";
309 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
313 b = kzalloc(sizeof(*b), GFP_ATOMIC);
317 strcpy(b->name, name);
319 res = m->enable_media(net, b, attr);
322 errstr = "failed to enable media";
323 NL_SET_ERR_MSG(extack, "Failed to enable media");
327 b->identity = bearer_id;
328 b->tolerance = m->tolerance;
329 b->min_win = m->min_win;
330 b->max_win = m->max_win;
331 b->domain = disc_domain;
332 b->net_plane = bearer_id + 'A';
334 refcount_set(&b->refcnt, 1);
336 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
338 bearer_disable(net, b);
339 errstr = "failed to create discoverer";
340 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
344 /* Create monitoring data before accepting activate messages */
345 if (tipc_mon_create(net, bearer_id)) {
346 bearer_disable(net, b);
351 test_and_set_bit_lock(0, &b->up);
352 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
354 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
356 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
360 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
365 * tipc_reset_bearer - Reset all links established over this bearer
367 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
369 pr_info("Resetting bearer <%s>\n", b->name);
370 tipc_node_delete_links(net, b->identity);
371 tipc_disc_reset(net, b);
375 bool tipc_bearer_hold(struct tipc_bearer *b)
377 return (b && refcount_inc_not_zero(&b->refcnt));
380 void tipc_bearer_put(struct tipc_bearer *b)
382 if (b && refcount_dec_and_test(&b->refcnt))
389 * Note: This routine assumes caller holds RTNL lock.
391 static void bearer_disable(struct net *net, struct tipc_bearer *b)
393 struct tipc_net *tn = tipc_net(net);
394 int bearer_id = b->identity;
396 pr_info("Disabling bearer <%s>\n", b->name);
397 clear_bit_unlock(0, &b->up);
398 tipc_node_delete_links(net, bearer_id);
399 b->media->disable_media(b);
400 RCU_INIT_POINTER(b->media_ptr, NULL);
402 tipc_disc_delete(b->disc);
403 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
405 tipc_mon_delete(net, bearer_id);
408 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
409 struct nlattr *attr[])
411 char *dev_name = strchr((const char *)b->name, ':') + 1;
412 int hwaddr_len = b->media->hwaddr_len;
413 u8 node_id[NODE_ID_LEN] = {0,};
414 struct net_device *dev;
416 /* Find device with specified name */
417 dev = dev_get_by_name(net, dev_name);
420 if (tipc_mtu_bad(dev, 0)) {
424 if (dev == net->loopback_dev) {
426 pr_info("Enabling <%s> not permitted\n", b->name);
430 /* Autoconfigure own node identity if needed */
431 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
432 memcpy(node_id, dev->dev_addr, hwaddr_len);
433 tipc_net_init(net, node_id, 0);
435 if (!tipc_own_id(net)) {
437 pr_warn("Failed to obtain node identity\n");
441 /* Associate TIPC bearer with L2 bearer */
442 rcu_assign_pointer(b->media_ptr, dev);
444 b->pt.type = htons(ETH_P_TIPC);
445 b->pt.func = tipc_l2_rcv_msg;
446 dev_add_pack(&b->pt);
447 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
448 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
449 b->bcast_addr.media_id = b->media->type_id;
450 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
452 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
453 rcu_assign_pointer(dev->tipc_ptr, b);
457 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
459 * Mark L2 bearer as inactive so that incoming buffers are thrown away
461 void tipc_disable_l2_media(struct tipc_bearer *b)
463 struct net_device *dev;
465 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
466 dev_remove_pack(&b->pt);
467 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
473 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
474 * @skb: the packet to be sent
475 * @b: the bearer through which the packet is to be sent
476 * @dest: peer destination address
478 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
479 struct tipc_bearer *b, struct tipc_media_addr *dest)
481 struct net_device *dev;
484 dev = (struct net_device *)rcu_dereference(b->media_ptr);
488 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
489 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
493 skb_reset_network_header(skb);
495 skb->protocol = htons(ETH_P_TIPC);
496 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
497 dev->dev_addr, skb->len);
502 bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
505 struct tipc_bearer *b;
508 b = bearer_get(net, bearer_id);
510 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
515 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
518 struct tipc_bearer *b;
521 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
528 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
530 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
532 struct tipc_media_addr *dest)
534 struct tipc_msg *hdr = buf_msg(skb);
535 struct tipc_bearer *b;
538 b = bearer_get(net, bearer_id);
539 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
540 #ifdef CONFIG_TIPC_CRYPTO
541 tipc_crypto_xmit(net, &skb, b, dest, NULL);
544 b->media->send_msg(net, skb, b, dest);
551 /* tipc_bearer_xmit() -send buffer to destination over bearer
553 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
554 struct sk_buff_head *xmitq,
555 struct tipc_media_addr *dst,
556 struct tipc_node *__dnode)
558 struct tipc_bearer *b;
559 struct sk_buff *skb, *tmp;
561 if (skb_queue_empty(xmitq))
565 b = bearer_get(net, bearer_id);
567 __skb_queue_purge(xmitq);
568 skb_queue_walk_safe(xmitq, skb, tmp) {
569 __skb_dequeue(xmitq);
570 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
571 #ifdef CONFIG_TIPC_CRYPTO
572 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
575 b->media->send_msg(net, skb, b, dst);
583 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
585 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
586 struct sk_buff_head *xmitq)
588 struct tipc_net *tn = tipc_net(net);
589 struct tipc_media_addr *dst;
590 int net_id = tn->net_id;
591 struct tipc_bearer *b;
592 struct sk_buff *skb, *tmp;
593 struct tipc_msg *hdr;
596 b = bearer_get(net, bearer_id);
597 if (unlikely(!b || !test_bit(0, &b->up)))
598 __skb_queue_purge(xmitq);
599 skb_queue_walk_safe(xmitq, skb, tmp) {
601 msg_set_non_seq(hdr, 1);
602 msg_set_mc_netid(hdr, net_id);
603 __skb_dequeue(xmitq);
604 dst = &b->bcast_addr;
605 #ifdef CONFIG_TIPC_CRYPTO
606 tipc_crypto_xmit(net, &skb, b, dst, NULL);
609 b->media->send_msg(net, skb, b, dst);
615 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
616 * @skb: the received message
617 * @dev: the net device that the packet was received on
618 * @pt: the packet_type structure which was used to register this handler
619 * @orig_dev: the original receive net device in case the device is a bond
621 * Accept only packets explicitly sent to this node, or broadcast packets;
622 * ignores packets sent using interface multicast, and traffic sent to other
623 * nodes (which can happen if interface is running in promiscuous mode).
625 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
626 struct packet_type *pt, struct net_device *orig_dev)
628 struct tipc_bearer *b;
631 b = rcu_dereference(dev->tipc_ptr) ?:
632 rcu_dereference(orig_dev->tipc_ptr);
633 if (likely(b && test_bit(0, &b->up) &&
634 (skb->pkt_type <= PACKET_MULTICAST))) {
635 skb_mark_not_on_list(skb);
636 TIPC_SKB_CB(skb)->flags = 0;
637 tipc_rcv(dev_net(b->pt.dev), skb, b);
639 return NET_RX_SUCCESS;
647 * tipc_l2_device_event - handle device events from network device
648 * @nb: the context of the notification
649 * @evt: the type of event
650 * @ptr: the net device that the event was on
652 * This function is called by the Ethernet driver in case of link
655 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
658 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
659 struct net *net = dev_net(dev);
660 struct tipc_bearer *b;
662 b = rtnl_dereference(dev->tipc_ptr);
666 trace_tipc_l2_device_event(dev, b, evt);
669 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
670 test_and_set_bit_lock(0, &b->up);
674 case NETDEV_GOING_DOWN:
675 clear_bit_unlock(0, &b->up);
676 tipc_reset_bearer(net, b);
679 test_and_set_bit_lock(0, &b->up);
681 case NETDEV_CHANGEMTU:
682 if (tipc_mtu_bad(dev, 0)) {
683 bearer_disable(net, b);
687 tipc_reset_bearer(net, b);
689 case NETDEV_CHANGEADDR:
690 b->media->raw2addr(b, &b->addr,
691 (char *)dev->dev_addr);
692 tipc_reset_bearer(net, b);
694 case NETDEV_UNREGISTER:
695 case NETDEV_CHANGENAME:
696 bearer_disable(net, b);
702 static struct notifier_block notifier = {
703 .notifier_call = tipc_l2_device_event,
707 int tipc_bearer_setup(void)
709 return register_netdevice_notifier(¬ifier);
712 void tipc_bearer_cleanup(void)
714 unregister_netdevice_notifier(¬ifier);
717 void tipc_bearer_stop(struct net *net)
719 struct tipc_net *tn = net_generic(net, tipc_net_id);
720 struct tipc_bearer *b;
723 for (i = 0; i < MAX_BEARERS; i++) {
724 b = rtnl_dereference(tn->bearer_list[i]);
726 bearer_disable(net, b);
727 tn->bearer_list[i] = NULL;
732 void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
734 struct net_device *dev = net->loopback_dev;
735 struct sk_buff *skb, *_skb;
738 skb_queue_walk(pkts, _skb) {
739 skb = pskb_copy(_skb, GFP_ATOMIC);
743 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
744 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
749 skb_reset_network_header(skb);
750 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
751 dev->dev_addr, skb->len);
753 skb->pkt_type = PACKET_HOST;
754 skb->ip_summed = CHECKSUM_UNNECESSARY;
755 skb->protocol = eth_type_trans(skb, dev);
760 static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
761 struct packet_type *pt, struct net_device *od)
764 return NET_RX_SUCCESS;
767 int tipc_attach_loopback(struct net *net)
769 struct net_device *dev = net->loopback_dev;
770 struct tipc_net *tn = tipc_net(net);
776 tn->loopback_pt.dev = dev;
777 tn->loopback_pt.type = htons(ETH_P_TIPC);
778 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
779 dev_add_pack(&tn->loopback_pt);
783 void tipc_detach_loopback(struct net *net)
785 struct tipc_net *tn = tipc_net(net);
787 dev_remove_pack(&tn->loopback_pt);
788 dev_put(net->loopback_dev);
791 /* Caller should hold rtnl_lock to protect the bearer */
792 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
793 struct tipc_bearer *bearer, int nlflags)
796 struct nlattr *attrs;
799 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
800 nlflags, TIPC_NL_BEARER_GET);
804 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
808 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
811 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
814 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
816 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
818 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
820 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
821 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
824 nla_nest_end(msg->skb, prop);
826 #ifdef CONFIG_TIPC_MEDIA_UDP
827 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
828 if (tipc_udp_nl_add_bearer_data(msg, bearer))
833 nla_nest_end(msg->skb, attrs);
834 genlmsg_end(msg->skb, hdr);
839 nla_nest_cancel(msg->skb, prop);
841 nla_nest_cancel(msg->skb, attrs);
843 genlmsg_cancel(msg->skb, hdr);
848 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
852 struct tipc_bearer *bearer;
853 struct tipc_nl_msg msg;
854 struct net *net = sock_net(skb->sk);
855 struct tipc_net *tn = net_generic(net, tipc_net_id);
857 if (i == MAX_BEARERS)
861 msg.portid = NETLINK_CB(cb->skb).portid;
862 msg.seq = cb->nlh->nlmsg_seq;
865 for (i = 0; i < MAX_BEARERS; i++) {
866 bearer = rtnl_dereference(tn->bearer_list[i]);
870 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
880 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
885 struct tipc_bearer *bearer;
886 struct tipc_nl_msg msg;
887 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
888 struct net *net = genl_info_net(info);
890 if (!info->attrs[TIPC_NLA_BEARER])
893 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
894 info->attrs[TIPC_NLA_BEARER],
895 tipc_nl_bearer_policy, info->extack);
899 if (!attrs[TIPC_NLA_BEARER_NAME])
901 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
903 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
908 msg.portid = info->snd_portid;
909 msg.seq = info->snd_seq;
912 bearer = tipc_bearer_find(net, name);
915 NL_SET_ERR_MSG(info->extack, "Bearer not found");
919 err = __tipc_nl_add_bearer(&msg, bearer, 0);
924 return genlmsg_reply(rep, info);
932 int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
936 struct tipc_bearer *bearer;
937 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
938 struct net *net = sock_net(skb->sk);
940 if (!info->attrs[TIPC_NLA_BEARER])
943 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
944 info->attrs[TIPC_NLA_BEARER],
945 tipc_nl_bearer_policy, info->extack);
949 if (!attrs[TIPC_NLA_BEARER_NAME])
952 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
954 bearer = tipc_bearer_find(net, name);
956 NL_SET_ERR_MSG(info->extack, "Bearer not found");
960 bearer_disable(net, bearer);
965 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
970 err = __tipc_nl_bearer_disable(skb, info);
976 int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
980 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
981 struct net *net = sock_net(skb->sk);
985 prio = TIPC_MEDIA_LINK_PRI;
987 if (!info->attrs[TIPC_NLA_BEARER])
990 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
991 info->attrs[TIPC_NLA_BEARER],
992 tipc_nl_bearer_policy, info->extack);
996 if (!attrs[TIPC_NLA_BEARER_NAME])
999 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1001 if (attrs[TIPC_NLA_BEARER_DOMAIN])
1002 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
1004 if (attrs[TIPC_NLA_BEARER_PROP]) {
1005 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1007 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1012 if (props[TIPC_NLA_PROP_PRIO])
1013 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1016 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1020 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1025 err = __tipc_nl_bearer_enable(skb, info);
1031 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1035 struct tipc_bearer *b;
1036 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1037 struct net *net = sock_net(skb->sk);
1039 if (!info->attrs[TIPC_NLA_BEARER])
1042 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1043 info->attrs[TIPC_NLA_BEARER],
1044 tipc_nl_bearer_policy, info->extack);
1048 if (!attrs[TIPC_NLA_BEARER_NAME])
1050 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1053 b = tipc_bearer_find(net, name);
1056 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1060 #ifdef CONFIG_TIPC_MEDIA_UDP
1061 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1062 err = tipc_udp_nl_bearer_add(b,
1063 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1075 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1077 struct tipc_bearer *b;
1078 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1079 struct net *net = sock_net(skb->sk);
1083 if (!info->attrs[TIPC_NLA_BEARER])
1086 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1087 info->attrs[TIPC_NLA_BEARER],
1088 tipc_nl_bearer_policy, info->extack);
1092 if (!attrs[TIPC_NLA_BEARER_NAME])
1094 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1096 b = tipc_bearer_find(net, name);
1098 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1102 if (attrs[TIPC_NLA_BEARER_PROP]) {
1103 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1105 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1110 if (props[TIPC_NLA_PROP_TOL]) {
1111 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1112 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1114 if (props[TIPC_NLA_PROP_PRIO])
1115 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1116 if (props[TIPC_NLA_PROP_WIN])
1117 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1118 if (props[TIPC_NLA_PROP_MTU]) {
1119 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1120 NL_SET_ERR_MSG(info->extack,
1121 "MTU property is unsupported");
1124 #ifdef CONFIG_TIPC_MEDIA_UDP
1125 if (tipc_udp_mtu_bad(nla_get_u32
1126 (props[TIPC_NLA_PROP_MTU]))) {
1127 NL_SET_ERR_MSG(info->extack,
1128 "MTU value is out-of-range");
1131 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1132 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1140 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1145 err = __tipc_nl_bearer_set(skb, info);
1151 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1152 struct tipc_media *media, int nlflags)
1155 struct nlattr *attrs;
1156 struct nlattr *prop;
1158 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1159 nlflags, TIPC_NL_MEDIA_GET);
1163 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1167 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1170 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1173 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1175 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1177 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
1179 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1180 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1183 nla_nest_end(msg->skb, prop);
1184 nla_nest_end(msg->skb, attrs);
1185 genlmsg_end(msg->skb, hdr);
1190 nla_nest_cancel(msg->skb, prop);
1192 nla_nest_cancel(msg->skb, attrs);
1194 genlmsg_cancel(msg->skb, hdr);
1199 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1202 int i = cb->args[0];
1203 struct tipc_nl_msg msg;
1209 msg.portid = NETLINK_CB(cb->skb).portid;
1210 msg.seq = cb->nlh->nlmsg_seq;
1213 for (; media_info_array[i] != NULL; i++) {
1214 err = __tipc_nl_add_media(&msg, media_info_array[i],
1225 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1229 struct tipc_nl_msg msg;
1230 struct tipc_media *media;
1231 struct sk_buff *rep;
1232 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1234 if (!info->attrs[TIPC_NLA_MEDIA])
1237 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1238 info->attrs[TIPC_NLA_MEDIA],
1239 tipc_nl_media_policy, info->extack);
1243 if (!attrs[TIPC_NLA_MEDIA_NAME])
1245 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1247 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1252 msg.portid = info->snd_portid;
1253 msg.seq = info->snd_seq;
1256 media = tipc_media_find(name);
1258 NL_SET_ERR_MSG(info->extack, "Media not found");
1263 err = __tipc_nl_add_media(&msg, media, 0);
1268 return genlmsg_reply(rep, info);
1276 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1280 struct tipc_media *m;
1281 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1283 if (!info->attrs[TIPC_NLA_MEDIA])
1286 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1287 info->attrs[TIPC_NLA_MEDIA],
1288 tipc_nl_media_policy, info->extack);
1290 if (!attrs[TIPC_NLA_MEDIA_NAME])
1292 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1294 m = tipc_media_find(name);
1296 NL_SET_ERR_MSG(info->extack, "Media not found");
1299 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1300 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1302 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1307 if (props[TIPC_NLA_PROP_TOL])
1308 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1309 if (props[TIPC_NLA_PROP_PRIO])
1310 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1311 if (props[TIPC_NLA_PROP_WIN])
1312 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1313 if (props[TIPC_NLA_PROP_MTU]) {
1314 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1315 NL_SET_ERR_MSG(info->extack,
1316 "MTU property is unsupported");
1319 #ifdef CONFIG_TIPC_MEDIA_UDP
1320 if (tipc_udp_mtu_bad(nla_get_u32
1321 (props[TIPC_NLA_PROP_MTU]))) {
1322 NL_SET_ERR_MSG(info->extack,
1323 "MTU value is out-of-range");
1326 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1334 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1339 err = __tipc_nl_media_set(skb, info);