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[])
239 struct tipc_net *tn = tipc_net(net);
240 struct tipc_bearer_names b_names;
241 int with_this_prio = 1;
242 struct tipc_bearer *b;
243 struct tipc_media *m;
249 if (!bearer_name_validate(name, &b_names)) {
250 errstr = "illegal name";
254 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
255 errstr = "illegal priority";
259 m = tipc_media_find(b_names.media_name);
261 errstr = "media not registered";
265 if (prio == TIPC_MEDIA_LINK_PRI)
268 /* Check new bearer vs existing ones and find free bearer id if any */
269 while (bearer_id < MAX_BEARERS) {
270 b = rtnl_dereference(tn->bearer_list[bearer_id]);
273 if (!strcmp(name, b->name)) {
274 errstr = "already enabled";
278 if (b->priority != prio)
280 if (++with_this_prio <= 2)
282 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
284 if (prio == TIPC_MIN_LINK_PRI) {
285 errstr = "cannot adjust to lower";
288 pr_warn("Bearer <%s>: trying with adjusted priority\n", name);
294 if (bearer_id >= MAX_BEARERS) {
295 errstr = "max 3 bearers permitted";
299 b = kzalloc(sizeof(*b), GFP_ATOMIC);
303 strcpy(b->name, name);
305 res = m->enable_media(net, b, attr);
308 errstr = "failed to enable media";
312 b->identity = bearer_id;
313 b->tolerance = m->tolerance;
314 b->window = m->window;
315 b->domain = disc_domain;
316 b->net_plane = bearer_id + 'A';
318 test_and_set_bit_lock(0, &b->up);
319 refcount_set(&b->refcnt, 1);
321 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
323 bearer_disable(net, b);
324 errstr = "failed to create discoverer";
328 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
330 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
332 if (tipc_mon_create(net, bearer_id)) {
333 bearer_disable(net, b);
337 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
341 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
346 * tipc_reset_bearer - Reset all links established over this bearer
348 static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
350 pr_info("Resetting bearer <%s>\n", b->name);
351 tipc_node_delete_links(net, b->identity);
352 tipc_disc_reset(net, b);
356 bool tipc_bearer_hold(struct tipc_bearer *b)
358 return (b && refcount_inc_not_zero(&b->refcnt));
361 void tipc_bearer_put(struct tipc_bearer *b)
363 if (b && refcount_dec_and_test(&b->refcnt))
370 * Note: This routine assumes caller holds RTNL lock.
372 static void bearer_disable(struct net *net, struct tipc_bearer *b)
374 struct tipc_net *tn = tipc_net(net);
375 int bearer_id = b->identity;
377 pr_info("Disabling bearer <%s>\n", b->name);
378 clear_bit_unlock(0, &b->up);
379 tipc_node_delete_links(net, bearer_id);
380 b->media->disable_media(b);
381 RCU_INIT_POINTER(b->media_ptr, NULL);
383 tipc_disc_delete(b->disc);
384 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
386 tipc_mon_delete(net, bearer_id);
389 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
390 struct nlattr *attr[])
392 char *dev_name = strchr((const char *)b->name, ':') + 1;
393 int hwaddr_len = b->media->hwaddr_len;
394 u8 node_id[NODE_ID_LEN] = {0,};
395 struct net_device *dev;
397 /* Find device with specified name */
398 dev = dev_get_by_name(net, dev_name);
401 if (tipc_mtu_bad(dev, 0)) {
405 if (dev == net->loopback_dev) {
407 pr_info("Enabling <%s> not permitted\n", b->name);
411 /* Autoconfigure own node identity if needed */
412 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
413 memcpy(node_id, dev->dev_addr, hwaddr_len);
414 tipc_net_init(net, node_id, 0);
416 if (!tipc_own_id(net)) {
418 pr_warn("Failed to obtain node identity\n");
422 /* Associate TIPC bearer with L2 bearer */
423 rcu_assign_pointer(b->media_ptr, dev);
425 b->pt.type = htons(ETH_P_TIPC);
426 b->pt.func = tipc_l2_rcv_msg;
427 dev_add_pack(&b->pt);
428 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
429 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
430 b->bcast_addr.media_id = b->media->type_id;
431 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
433 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
434 rcu_assign_pointer(dev->tipc_ptr, b);
438 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
440 * Mark L2 bearer as inactive so that incoming buffers are thrown away
442 void tipc_disable_l2_media(struct tipc_bearer *b)
444 struct net_device *dev;
446 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
447 dev_remove_pack(&b->pt);
448 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
454 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
455 * @skb: the packet to be sent
456 * @b: the bearer through which the packet is to be sent
457 * @dest: peer destination address
459 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
460 struct tipc_bearer *b, struct tipc_media_addr *dest)
462 struct net_device *dev;
465 dev = (struct net_device *)rcu_dereference(b->media_ptr);
469 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
470 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
474 skb_reset_network_header(skb);
476 skb->protocol = htons(ETH_P_TIPC);
477 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
478 dev->dev_addr, skb->len);
483 bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
486 struct tipc_bearer *b;
489 b = bearer_get(net, bearer_id);
491 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
496 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
499 struct tipc_bearer *b;
502 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
509 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
511 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
513 struct tipc_media_addr *dest)
515 struct tipc_msg *hdr = buf_msg(skb);
516 struct tipc_bearer *b;
519 b = bearer_get(net, bearer_id);
520 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
521 #ifdef CONFIG_TIPC_CRYPTO
522 tipc_crypto_xmit(net, &skb, b, dest, NULL);
525 b->media->send_msg(net, skb, b, dest);
532 /* tipc_bearer_xmit() -send buffer to destination over bearer
534 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
535 struct sk_buff_head *xmitq,
536 struct tipc_media_addr *dst,
537 struct tipc_node *__dnode)
539 struct tipc_bearer *b;
540 struct sk_buff *skb, *tmp;
542 if (skb_queue_empty(xmitq))
546 b = bearer_get(net, bearer_id);
548 __skb_queue_purge(xmitq);
549 skb_queue_walk_safe(xmitq, skb, tmp) {
550 __skb_dequeue(xmitq);
551 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
552 #ifdef CONFIG_TIPC_CRYPTO
553 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
556 b->media->send_msg(net, skb, b, dst);
564 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
566 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
567 struct sk_buff_head *xmitq)
569 struct tipc_net *tn = tipc_net(net);
570 struct tipc_media_addr *dst;
571 int net_id = tn->net_id;
572 struct tipc_bearer *b;
573 struct sk_buff *skb, *tmp;
574 struct tipc_msg *hdr;
577 b = bearer_get(net, bearer_id);
578 if (unlikely(!b || !test_bit(0, &b->up)))
579 __skb_queue_purge(xmitq);
580 skb_queue_walk_safe(xmitq, skb, tmp) {
582 msg_set_non_seq(hdr, 1);
583 msg_set_mc_netid(hdr, net_id);
584 __skb_dequeue(xmitq);
585 dst = &b->bcast_addr;
586 #ifdef CONFIG_TIPC_CRYPTO
587 tipc_crypto_xmit(net, &skb, b, dst, NULL);
590 b->media->send_msg(net, skb, b, dst);
596 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
597 * @buf: the received packet
598 * @dev: the net device that the packet was received on
599 * @pt: the packet_type structure which was used to register this handler
600 * @orig_dev: the original receive net device in case the device is a bond
602 * Accept only packets explicitly sent to this node, or broadcast packets;
603 * ignores packets sent using interface multicast, and traffic sent to other
604 * nodes (which can happen if interface is running in promiscuous mode).
606 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
607 struct packet_type *pt, struct net_device *orig_dev)
609 struct tipc_bearer *b;
612 b = rcu_dereference(dev->tipc_ptr) ?:
613 rcu_dereference(orig_dev->tipc_ptr);
614 if (likely(b && test_bit(0, &b->up) &&
615 (skb->pkt_type <= PACKET_MULTICAST))) {
616 skb_mark_not_on_list(skb);
617 TIPC_SKB_CB(skb)->flags = 0;
618 tipc_rcv(dev_net(b->pt.dev), skb, b);
620 return NET_RX_SUCCESS;
628 * tipc_l2_device_event - handle device events from network device
629 * @nb: the context of the notification
630 * @evt: the type of event
631 * @ptr: the net device that the event was on
633 * This function is called by the Ethernet driver in case of link
636 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
639 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
640 struct net *net = dev_net(dev);
641 struct tipc_bearer *b;
643 b = rtnl_dereference(dev->tipc_ptr);
647 trace_tipc_l2_device_event(dev, b, evt);
650 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
651 test_and_set_bit_lock(0, &b->up);
655 case NETDEV_GOING_DOWN:
656 clear_bit_unlock(0, &b->up);
657 tipc_reset_bearer(net, b);
660 test_and_set_bit_lock(0, &b->up);
662 case NETDEV_CHANGEMTU:
663 if (tipc_mtu_bad(dev, 0)) {
664 bearer_disable(net, b);
668 tipc_reset_bearer(net, b);
670 case NETDEV_CHANGEADDR:
671 b->media->raw2addr(b, &b->addr,
672 (char *)dev->dev_addr);
673 tipc_reset_bearer(net, b);
675 case NETDEV_UNREGISTER:
676 case NETDEV_CHANGENAME:
677 bearer_disable(net, b);
683 static struct notifier_block notifier = {
684 .notifier_call = tipc_l2_device_event,
688 int tipc_bearer_setup(void)
690 return register_netdevice_notifier(¬ifier);
693 void tipc_bearer_cleanup(void)
695 unregister_netdevice_notifier(¬ifier);
698 void tipc_bearer_stop(struct net *net)
700 struct tipc_net *tn = net_generic(net, tipc_net_id);
701 struct tipc_bearer *b;
704 for (i = 0; i < MAX_BEARERS; i++) {
705 b = rtnl_dereference(tn->bearer_list[i]);
707 bearer_disable(net, b);
708 tn->bearer_list[i] = NULL;
713 void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
715 struct net_device *dev = net->loopback_dev;
716 struct sk_buff *skb, *_skb;
719 skb_queue_walk(pkts, _skb) {
720 skb = pskb_copy(_skb, GFP_ATOMIC);
724 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
725 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
730 skb_reset_network_header(skb);
731 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
732 dev->dev_addr, skb->len);
734 skb->pkt_type = PACKET_HOST;
735 skb->ip_summed = CHECKSUM_UNNECESSARY;
736 skb->protocol = eth_type_trans(skb, dev);
741 static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
742 struct packet_type *pt, struct net_device *od)
745 return NET_RX_SUCCESS;
748 int tipc_attach_loopback(struct net *net)
750 struct net_device *dev = net->loopback_dev;
751 struct tipc_net *tn = tipc_net(net);
757 tn->loopback_pt.dev = dev;
758 tn->loopback_pt.type = htons(ETH_P_TIPC);
759 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
760 dev_add_pack(&tn->loopback_pt);
764 void tipc_detach_loopback(struct net *net)
766 struct tipc_net *tn = tipc_net(net);
768 dev_remove_pack(&tn->loopback_pt);
769 dev_put(net->loopback_dev);
772 /* Caller should hold rtnl_lock to protect the bearer */
773 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
774 struct tipc_bearer *bearer, int nlflags)
777 struct nlattr *attrs;
780 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
781 nlflags, TIPC_NL_BEARER_GET);
785 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
789 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
792 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
795 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
797 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
799 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
801 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
802 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
805 nla_nest_end(msg->skb, prop);
807 #ifdef CONFIG_TIPC_MEDIA_UDP
808 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
809 if (tipc_udp_nl_add_bearer_data(msg, bearer))
814 nla_nest_end(msg->skb, attrs);
815 genlmsg_end(msg->skb, hdr);
820 nla_nest_cancel(msg->skb, prop);
822 nla_nest_cancel(msg->skb, attrs);
824 genlmsg_cancel(msg->skb, hdr);
829 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
833 struct tipc_bearer *bearer;
834 struct tipc_nl_msg msg;
835 struct net *net = sock_net(skb->sk);
836 struct tipc_net *tn = net_generic(net, tipc_net_id);
838 if (i == MAX_BEARERS)
842 msg.portid = NETLINK_CB(cb->skb).portid;
843 msg.seq = cb->nlh->nlmsg_seq;
846 for (i = 0; i < MAX_BEARERS; i++) {
847 bearer = rtnl_dereference(tn->bearer_list[i]);
851 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
861 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
866 struct tipc_bearer *bearer;
867 struct tipc_nl_msg msg;
868 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
869 struct net *net = genl_info_net(info);
871 if (!info->attrs[TIPC_NLA_BEARER])
874 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
875 info->attrs[TIPC_NLA_BEARER],
876 tipc_nl_bearer_policy, info->extack);
880 if (!attrs[TIPC_NLA_BEARER_NAME])
882 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
884 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
889 msg.portid = info->snd_portid;
890 msg.seq = info->snd_seq;
893 bearer = tipc_bearer_find(net, name);
899 err = __tipc_nl_add_bearer(&msg, bearer, 0);
904 return genlmsg_reply(rep, info);
912 int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
916 struct tipc_bearer *bearer;
917 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
918 struct net *net = sock_net(skb->sk);
920 if (!info->attrs[TIPC_NLA_BEARER])
923 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
924 info->attrs[TIPC_NLA_BEARER],
925 tipc_nl_bearer_policy, info->extack);
929 if (!attrs[TIPC_NLA_BEARER_NAME])
932 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
934 bearer = tipc_bearer_find(net, name);
938 bearer_disable(net, bearer);
943 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
948 err = __tipc_nl_bearer_disable(skb, info);
954 int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
958 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
959 struct net *net = sock_net(skb->sk);
963 prio = TIPC_MEDIA_LINK_PRI;
965 if (!info->attrs[TIPC_NLA_BEARER])
968 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
969 info->attrs[TIPC_NLA_BEARER],
970 tipc_nl_bearer_policy, info->extack);
974 if (!attrs[TIPC_NLA_BEARER_NAME])
977 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
979 if (attrs[TIPC_NLA_BEARER_DOMAIN])
980 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
982 if (attrs[TIPC_NLA_BEARER_PROP]) {
983 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
985 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
990 if (props[TIPC_NLA_PROP_PRIO])
991 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
994 return tipc_enable_bearer(net, bearer, domain, prio, attrs);
997 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1002 err = __tipc_nl_bearer_enable(skb, info);
1008 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1012 struct tipc_bearer *b;
1013 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1014 struct net *net = sock_net(skb->sk);
1016 if (!info->attrs[TIPC_NLA_BEARER])
1019 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1020 info->attrs[TIPC_NLA_BEARER],
1021 tipc_nl_bearer_policy, info->extack);
1025 if (!attrs[TIPC_NLA_BEARER_NAME])
1027 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1030 b = tipc_bearer_find(net, name);
1036 #ifdef CONFIG_TIPC_MEDIA_UDP
1037 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1038 err = tipc_udp_nl_bearer_add(b,
1039 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1051 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1053 struct tipc_bearer *b;
1054 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1055 struct net *net = sock_net(skb->sk);
1059 if (!info->attrs[TIPC_NLA_BEARER])
1062 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1063 info->attrs[TIPC_NLA_BEARER],
1064 tipc_nl_bearer_policy, info->extack);
1068 if (!attrs[TIPC_NLA_BEARER_NAME])
1070 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1072 b = tipc_bearer_find(net, name);
1076 if (attrs[TIPC_NLA_BEARER_PROP]) {
1077 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1079 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1084 if (props[TIPC_NLA_PROP_TOL]) {
1085 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1086 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1088 if (props[TIPC_NLA_PROP_PRIO])
1089 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1090 if (props[TIPC_NLA_PROP_WIN])
1091 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1092 if (props[TIPC_NLA_PROP_MTU]) {
1093 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP)
1095 #ifdef CONFIG_TIPC_MEDIA_UDP
1096 if (tipc_udp_mtu_bad(nla_get_u32
1097 (props[TIPC_NLA_PROP_MTU])))
1099 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1100 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1108 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1113 err = __tipc_nl_bearer_set(skb, info);
1119 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1120 struct tipc_media *media, int nlflags)
1123 struct nlattr *attrs;
1124 struct nlattr *prop;
1126 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1127 nlflags, TIPC_NL_MEDIA_GET);
1131 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1135 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1138 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1141 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1143 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1145 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
1147 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1148 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1151 nla_nest_end(msg->skb, prop);
1152 nla_nest_end(msg->skb, attrs);
1153 genlmsg_end(msg->skb, hdr);
1158 nla_nest_cancel(msg->skb, prop);
1160 nla_nest_cancel(msg->skb, attrs);
1162 genlmsg_cancel(msg->skb, hdr);
1167 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1170 int i = cb->args[0];
1171 struct tipc_nl_msg msg;
1177 msg.portid = NETLINK_CB(cb->skb).portid;
1178 msg.seq = cb->nlh->nlmsg_seq;
1181 for (; media_info_array[i] != NULL; i++) {
1182 err = __tipc_nl_add_media(&msg, media_info_array[i],
1193 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1197 struct tipc_nl_msg msg;
1198 struct tipc_media *media;
1199 struct sk_buff *rep;
1200 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1202 if (!info->attrs[TIPC_NLA_MEDIA])
1205 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1206 info->attrs[TIPC_NLA_MEDIA],
1207 tipc_nl_media_policy, info->extack);
1211 if (!attrs[TIPC_NLA_MEDIA_NAME])
1213 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1215 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1220 msg.portid = info->snd_portid;
1221 msg.seq = info->snd_seq;
1224 media = tipc_media_find(name);
1230 err = __tipc_nl_add_media(&msg, media, 0);
1235 return genlmsg_reply(rep, info);
1243 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1247 struct tipc_media *m;
1248 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1250 if (!info->attrs[TIPC_NLA_MEDIA])
1253 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1254 info->attrs[TIPC_NLA_MEDIA],
1255 tipc_nl_media_policy, info->extack);
1257 if (!attrs[TIPC_NLA_MEDIA_NAME])
1259 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1261 m = tipc_media_find(name);
1265 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1266 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1268 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1273 if (props[TIPC_NLA_PROP_TOL])
1274 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1275 if (props[TIPC_NLA_PROP_PRIO])
1276 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1277 if (props[TIPC_NLA_PROP_WIN])
1278 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1279 if (props[TIPC_NLA_PROP_MTU]) {
1280 if (m->type_id != TIPC_MEDIA_TYPE_UDP)
1282 #ifdef CONFIG_TIPC_MEDIA_UDP
1283 if (tipc_udp_mtu_bad(nla_get_u32
1284 (props[TIPC_NLA_PROP_MTU])))
1286 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1294 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1299 err = __tipc_nl_media_set(skb, info);