blob: fc0b787fb9286e9799e5c551bebc02500cb023ea [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * net/tipc/bearer.c: TIPC bearer code
3 *
4 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
5 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <net/sock.h>
38#include "core.h"
39#include "bearer.h"
40#include "link.h"
41#include "discover.h"
42#include "monitor.h"
43#include "bcast.h"
44#include "netlink.h"
45#include "udp_media.h"
46#include "trace.h"
47
48#define MAX_ADDR_STR 60
49
50static struct tipc_media * const media_info_array[] = {
51 &eth_media_info,
52#ifdef CONFIG_TIPC_MEDIA_IB
53 &ib_media_info,
54#endif
55#ifdef CONFIG_TIPC_MEDIA_UDP
56 &udp_media_info,
57#endif
58 NULL
59};
60
61static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
62{
63 struct tipc_net *tn = tipc_net(net);
64
65 return rcu_dereference(tn->bearer_list[bearer_id]);
66}
67
68static void bearer_disable(struct net *net, struct tipc_bearer *b);
69static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
70 struct packet_type *pt, struct net_device *orig_dev);
71
72/**
73 * tipc_media_find - locates specified media object by name
74 */
75struct tipc_media *tipc_media_find(const char *name)
76{
77 u32 i;
78
79 for (i = 0; media_info_array[i] != NULL; i++) {
80 if (!strcmp(media_info_array[i]->name, name))
81 break;
82 }
83 return media_info_array[i];
84}
85
86/**
87 * media_find_id - locates specified media object by type identifier
88 */
89static struct tipc_media *media_find_id(u8 type)
90{
91 u32 i;
92
93 for (i = 0; media_info_array[i] != NULL; i++) {
94 if (media_info_array[i]->type_id == type)
95 break;
96 }
97 return media_info_array[i];
98}
99
100/**
101 * tipc_media_addr_printf - record media address in print buffer
102 */
103int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
104{
105 char addr_str[MAX_ADDR_STR];
106 struct tipc_media *m;
107 int ret;
108
109 m = media_find_id(a->media_id);
110
111 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
112 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
113 else {
114 u32 i;
115
116 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
117 for (i = 0; i < sizeof(a->value); i++)
118 ret += scnprintf(buf + ret, len - ret,
119 "-%x", a->value[i]);
120 }
121 return ret;
122}
123
124/**
125 * bearer_name_validate - validate & (optionally) deconstruct bearer name
126 * @name: ptr to bearer name string
127 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
128 *
129 * Returns 1 if bearer name is valid, otherwise 0.
130 */
131static int bearer_name_validate(const char *name,
132 struct tipc_bearer_names *name_parts)
133{
134 char name_copy[TIPC_MAX_BEARER_NAME];
135 char *media_name;
136 char *if_name;
137 u32 media_len;
138 u32 if_len;
139
140 /* copy bearer name & ensure length is OK */
141 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
142 /* need above in case non-Posix strncpy() doesn't pad with nulls */
143 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
144 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
145 return 0;
146
147 /* ensure all component parts of bearer name are present */
148 media_name = name_copy;
149 if_name = strchr(media_name, ':');
150 if (if_name == NULL)
151 return 0;
152 *(if_name++) = 0;
153 media_len = if_name - media_name;
154 if_len = strlen(if_name) + 1;
155
156 /* validate component parts of bearer name */
157 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
158 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
159 return 0;
160
161 /* return bearer name components, if necessary */
162 if (name_parts) {
163 if (strscpy(name_parts->media_name, media_name,
164 TIPC_MAX_MEDIA_NAME) < 0)
165 return 0;
166 if (strscpy(name_parts->if_name, if_name,
167 TIPC_MAX_IF_NAME) < 0)
168 return 0;
169 }
170 return 1;
171}
172
173/**
174 * tipc_bearer_find - locates bearer object with matching bearer name
175 */
176struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
177{
178 struct tipc_net *tn = net_generic(net, tipc_net_id);
179 struct tipc_bearer *b;
180 u32 i;
181
182 for (i = 0; i < MAX_BEARERS; i++) {
183 b = rtnl_dereference(tn->bearer_list[i]);
184 if (b && (!strcmp(b->name, name)))
185 return b;
186 }
187 return NULL;
188}
189
190/* tipc_bearer_get_name - get the bearer name from its id.
191 * @net: network namespace
192 * @name: a pointer to the buffer where the name will be stored.
193 * @bearer_id: the id to get the name from.
194 */
195int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
196{
197 struct tipc_net *tn = tipc_net(net);
198 struct tipc_bearer *b;
199
200 if (bearer_id >= MAX_BEARERS)
201 return -EINVAL;
202
203 b = rtnl_dereference(tn->bearer_list[bearer_id]);
204 if (!b)
205 return -EINVAL;
206
207 strcpy(name, b->name);
208 return 0;
209}
210
211void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
212{
213 struct tipc_net *tn = net_generic(net, tipc_net_id);
214 struct tipc_bearer *b;
215
216 rcu_read_lock();
217 b = rcu_dereference(tn->bearer_list[bearer_id]);
218 if (b)
219 tipc_disc_add_dest(b->disc);
220 rcu_read_unlock();
221}
222
223void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
224{
225 struct tipc_net *tn = net_generic(net, tipc_net_id);
226 struct tipc_bearer *b;
227
228 rcu_read_lock();
229 b = rcu_dereference(tn->bearer_list[bearer_id]);
230 if (b)
231 tipc_disc_remove_dest(b->disc);
232 rcu_read_unlock();
233}
234
235/**
236 * tipc_enable_bearer - enable bearer with the given name
237 */
238static int tipc_enable_bearer(struct net *net, const char *name,
239 u32 disc_domain, u32 prio,
240 struct nlattr *attr[],
241 struct netlink_ext_ack *extack)
242{
243 struct tipc_net *tn = tipc_net(net);
244 struct tipc_bearer_names b_names;
245 int with_this_prio = 1;
246 struct tipc_bearer *b;
247 struct tipc_media *m;
248 struct sk_buff *skb;
249 int bearer_id = 0;
250 int res = -EINVAL;
251 char *errstr = "";
252 u32 i;
253
254 if (!bearer_name_validate(name, &b_names)) {
255 NL_SET_ERR_MSG(extack, "Illegal name");
256 return res;
257 }
258
259 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
260 errstr = "illegal priority";
261 NL_SET_ERR_MSG(extack, "Illegal priority");
262 goto rejected;
263 }
264
265 m = tipc_media_find(b_names.media_name);
266 if (!m) {
267 errstr = "media not registered";
268 NL_SET_ERR_MSG(extack, "Media not registered");
269 goto rejected;
270 }
271
272 if (prio == TIPC_MEDIA_LINK_PRI)
273 prio = m->priority;
274
275 /* Check new bearer vs existing ones and find free bearer id if any */
276 bearer_id = MAX_BEARERS;
277 i = MAX_BEARERS;
278 while (i-- != 0) {
279 b = rtnl_dereference(tn->bearer_list[i]);
280 if (!b) {
281 bearer_id = i;
282 continue;
283 }
284 if (!strcmp(name, b->name)) {
285 errstr = "already enabled";
286 NL_SET_ERR_MSG(extack, "Already enabled");
287 goto rejected;
288 }
289
290 if (b->priority == prio &&
291 (++with_this_prio > 2)) {
292 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
293 name, prio);
294
295 if (prio == TIPC_MIN_LINK_PRI) {
296 errstr = "cannot adjust to lower";
297 NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
298 goto rejected;
299 }
300
301 pr_warn("Bearer <%s>: trying with adjusted priority\n",
302 name);
303 prio--;
304 bearer_id = MAX_BEARERS;
305 i = MAX_BEARERS;
306 with_this_prio = 1;
307 }
308 }
309
310 if (bearer_id >= MAX_BEARERS) {
311 errstr = "max 3 bearers permitted";
312 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
313 goto rejected;
314 }
315
316 b = kzalloc(sizeof(*b), GFP_ATOMIC);
317 if (!b)
318 return -ENOMEM;
319
320 strcpy(b->name, name);
321 b->media = m;
322 res = m->enable_media(net, b, attr);
323 if (res) {
324 kfree(b);
325 errstr = "failed to enable media";
326 NL_SET_ERR_MSG(extack, "Failed to enable media");
327 goto rejected;
328 }
329
330 b->identity = bearer_id;
331 b->tolerance = m->tolerance;
332 b->window = m->window;
333 b->domain = disc_domain;
334 b->net_plane = bearer_id + 'A';
335 b->priority = prio;
336 test_and_set_bit_lock(0, &b->up);
337 refcount_set(&b->refcnt, 1);
338
339 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
340 if (res) {
341 bearer_disable(net, b);
342 errstr = "failed to create discoverer";
343 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
344 goto rejected;
345 }
346
347 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
348 if (skb)
349 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
350
351 if (tipc_mon_create(net, bearer_id)) {
352 bearer_disable(net, b);
353 return -ENOMEM;
354 }
355
356 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
357
358 return res;
359rejected:
360 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
361 return res;
362}
363
364/**
365 * tipc_reset_bearer - Reset all links established over this bearer
366 */
367static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
368{
369 pr_info("Resetting bearer <%s>\n", b->name);
370 tipc_node_delete_links(net, b->identity);
371 tipc_disc_reset(net, b);
372 return 0;
373}
374
375bool tipc_bearer_hold(struct tipc_bearer *b)
376{
377 return (b && refcount_inc_not_zero(&b->refcnt));
378}
379
380void tipc_bearer_put(struct tipc_bearer *b)
381{
382 if (b && refcount_dec_and_test(&b->refcnt))
383 kfree_rcu(b, rcu);
384}
385
386/**
387 * bearer_disable
388 *
389 * Note: This routine assumes caller holds RTNL lock.
390 */
391static void bearer_disable(struct net *net, struct tipc_bearer *b)
392{
393 struct tipc_net *tn = tipc_net(net);
394 int bearer_id = b->identity;
395
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);
401 if (b->disc)
402 tipc_disc_delete(b->disc);
403 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
404 tipc_bearer_put(b);
405 tipc_mon_delete(net, bearer_id);
406}
407
408int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
409 struct nlattr *attr[])
410{
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;
415
416 /* Find device with specified name */
417 dev = dev_get_by_name(net, dev_name);
418 if (!dev)
419 return -ENODEV;
420 if (tipc_mtu_bad(dev, 0)) {
421 dev_put(dev);
422 return -EINVAL;
423 }
424 if (dev == net->loopback_dev) {
425 dev_put(dev);
426 pr_info("Enabling <%s> not permitted\n", b->name);
427 return -EINVAL;
428 }
429
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);
434 }
435 if (!tipc_own_id(net)) {
436 dev_put(dev);
437 pr_warn("Failed to obtain node identity\n");
438 return -EINVAL;
439 }
440
441 /* Associate TIPC bearer with L2 bearer */
442 rcu_assign_pointer(b->media_ptr, dev);
443 b->pt.dev = 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;
451 b->mtu = dev->mtu;
452 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
453 rcu_assign_pointer(dev->tipc_ptr, b);
454 return 0;
455}
456
457/* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
458 *
459 * Mark L2 bearer as inactive so that incoming buffers are thrown away
460 */
461void tipc_disable_l2_media(struct tipc_bearer *b)
462{
463 struct net_device *dev;
464
465 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
466 dev_remove_pack(&b->pt);
467 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
468 synchronize_net();
469 dev_put(dev);
470}
471
472/**
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
477 */
478int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
479 struct tipc_bearer *b, struct tipc_media_addr *dest)
480{
481 struct net_device *dev;
482 int delta;
483
484 dev = (struct net_device *)rcu_dereference(b->media_ptr);
485 if (!dev)
486 return 0;
487
488 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
489 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
490 kfree_skb(skb);
491 return 0;
492 }
493 skb_reset_network_header(skb);
494 skb->dev = dev;
495 skb->protocol = htons(ETH_P_TIPC);
496 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
497 dev->dev_addr, skb->len);
498 dev_queue_xmit(skb);
499 return 0;
500}
501
502bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
503{
504 bool supp = false;
505 struct tipc_bearer *b;
506
507 rcu_read_lock();
508 b = bearer_get(net, bearer_id);
509 if (b)
510 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
511 rcu_read_unlock();
512 return supp;
513}
514
515int tipc_bearer_mtu(struct net *net, u32 bearer_id)
516{
517 int mtu = 0;
518 struct tipc_bearer *b;
519
520 rcu_read_lock();
521 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
522 if (b)
523 mtu = b->mtu;
524 rcu_read_unlock();
525 return mtu;
526}
527
528/* tipc_bearer_xmit_skb - sends buffer to destination over bearer
529 */
530void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
531 struct sk_buff *skb,
532 struct tipc_media_addr *dest)
533{
534 struct tipc_msg *hdr = buf_msg(skb);
535 struct tipc_bearer *b;
536
537 rcu_read_lock();
538 b = bearer_get(net, bearer_id);
539 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr))))
540 b->media->send_msg(net, skb, b, dest);
541 else
542 kfree_skb(skb);
543 rcu_read_unlock();
544}
545
546/* tipc_bearer_xmit() -send buffer to destination over bearer
547 */
548void tipc_bearer_xmit(struct net *net, u32 bearer_id,
549 struct sk_buff_head *xmitq,
550 struct tipc_media_addr *dst)
551{
552 struct tipc_bearer *b;
553 struct sk_buff *skb, *tmp;
554
555 if (skb_queue_empty(xmitq))
556 return;
557
558 rcu_read_lock();
559 b = bearer_get(net, bearer_id);
560 if (unlikely(!b))
561 __skb_queue_purge(xmitq);
562 skb_queue_walk_safe(xmitq, skb, tmp) {
563 __skb_dequeue(xmitq);
564 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb))))
565 b->media->send_msg(net, skb, b, dst);
566 else
567 kfree_skb(skb);
568 }
569 rcu_read_unlock();
570}
571
572/* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
573 */
574void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
575 struct sk_buff_head *xmitq)
576{
577 struct tipc_net *tn = tipc_net(net);
578 int net_id = tn->net_id;
579 struct tipc_bearer *b;
580 struct sk_buff *skb, *tmp;
581 struct tipc_msg *hdr;
582
583 rcu_read_lock();
584 b = bearer_get(net, bearer_id);
585 if (unlikely(!b || !test_bit(0, &b->up)))
586 __skb_queue_purge(xmitq);
587 skb_queue_walk_safe(xmitq, skb, tmp) {
588 hdr = buf_msg(skb);
589 msg_set_non_seq(hdr, 1);
590 msg_set_mc_netid(hdr, net_id);
591 __skb_dequeue(xmitq);
592 b->media->send_msg(net, skb, b, &b->bcast_addr);
593 }
594 rcu_read_unlock();
595}
596
597/**
598 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
599 * @buf: the received packet
600 * @dev: the net device that the packet was received on
601 * @pt: the packet_type structure which was used to register this handler
602 * @orig_dev: the original receive net device in case the device is a bond
603 *
604 * Accept only packets explicitly sent to this node, or broadcast packets;
605 * ignores packets sent using interface multicast, and traffic sent to other
606 * nodes (which can happen if interface is running in promiscuous mode).
607 */
608static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
609 struct packet_type *pt, struct net_device *orig_dev)
610{
611 struct tipc_bearer *b;
612
613 rcu_read_lock();
614 b = rcu_dereference(dev->tipc_ptr) ?:
615 rcu_dereference(orig_dev->tipc_ptr);
616 if (likely(b && test_bit(0, &b->up) &&
617 (skb->pkt_type <= PACKET_MULTICAST))) {
618 skb_mark_not_on_list(skb);
619 tipc_rcv(dev_net(b->pt.dev), skb, b);
620 rcu_read_unlock();
621 return NET_RX_SUCCESS;
622 }
623 rcu_read_unlock();
624 kfree_skb(skb);
625 return NET_RX_DROP;
626}
627
628/**
629 * tipc_l2_device_event - handle device events from network device
630 * @nb: the context of the notification
631 * @evt: the type of event
632 * @ptr: the net device that the event was on
633 *
634 * This function is called by the Ethernet driver in case of link
635 * change event.
636 */
637static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
638 void *ptr)
639{
640 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
641 struct net *net = dev_net(dev);
642 struct tipc_bearer *b;
643
644 b = rtnl_dereference(dev->tipc_ptr);
645 if (!b)
646 return NOTIFY_DONE;
647
648 trace_tipc_l2_device_event(dev, b, evt);
649 switch (evt) {
650 case NETDEV_CHANGE:
651 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
652 test_and_set_bit_lock(0, &b->up);
653 break;
654 }
655 /* fall through */
656 case NETDEV_GOING_DOWN:
657 clear_bit_unlock(0, &b->up);
658 tipc_reset_bearer(net, b);
659 break;
660 case NETDEV_UP:
661 test_and_set_bit_lock(0, &b->up);
662 break;
663 case NETDEV_CHANGEMTU:
664 if (tipc_mtu_bad(dev, 0)) {
665 bearer_disable(net, b);
666 break;
667 }
668 b->mtu = dev->mtu;
669 tipc_reset_bearer(net, b);
670 break;
671 case NETDEV_CHANGEADDR:
672 b->media->raw2addr(b, &b->addr,
673 (char *)dev->dev_addr);
674 tipc_reset_bearer(net, b);
675 break;
676 case NETDEV_UNREGISTER:
677 case NETDEV_CHANGENAME:
678 bearer_disable(net, b);
679 break;
680 }
681 return NOTIFY_OK;
682}
683
684static struct notifier_block notifier = {
685 .notifier_call = tipc_l2_device_event,
686 .priority = 0,
687};
688
689int tipc_bearer_setup(void)
690{
691 return register_netdevice_notifier(&notifier);
692}
693
694void tipc_bearer_cleanup(void)
695{
696 unregister_netdevice_notifier(&notifier);
697}
698
699void tipc_bearer_stop(struct net *net)
700{
701 struct tipc_net *tn = net_generic(net, tipc_net_id);
702 struct tipc_bearer *b;
703 u32 i;
704
705 for (i = 0; i < MAX_BEARERS; i++) {
706 b = rtnl_dereference(tn->bearer_list[i]);
707 if (b) {
708 bearer_disable(net, b);
709 tn->bearer_list[i] = NULL;
710 }
711 }
712}
713
714void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
715{
716 struct net_device *dev = net->loopback_dev;
717 struct sk_buff *skb, *_skb;
718 int exp;
719
720 skb_queue_walk(pkts, _skb) {
721 skb = pskb_copy(_skb, GFP_ATOMIC);
722 if (!skb)
723 continue;
724
725 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
726 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
727 kfree_skb(skb);
728 continue;
729 }
730
731 skb_reset_network_header(skb);
732 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
733 dev->dev_addr, skb->len);
734 skb->dev = dev;
735 skb->pkt_type = PACKET_HOST;
736 skb->ip_summed = CHECKSUM_UNNECESSARY;
737 skb->protocol = eth_type_trans(skb, dev);
738 netif_rx_ni(skb);
739 }
740}
741
742static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
743 struct packet_type *pt, struct net_device *od)
744{
745 consume_skb(skb);
746 return NET_RX_SUCCESS;
747}
748
749int tipc_attach_loopback(struct net *net)
750{
751 struct net_device *dev = net->loopback_dev;
752 struct tipc_net *tn = tipc_net(net);
753
754 if (!dev)
755 return -ENODEV;
756
757 dev_hold(dev);
758 tn->loopback_pt.dev = dev;
759 tn->loopback_pt.type = htons(ETH_P_TIPC);
760 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
761 dev_add_pack(&tn->loopback_pt);
762 return 0;
763}
764
765void tipc_detach_loopback(struct net *net)
766{
767 struct tipc_net *tn = tipc_net(net);
768
769 dev_remove_pack(&tn->loopback_pt);
770 dev_put(net->loopback_dev);
771}
772
773/* Caller should hold rtnl_lock to protect the bearer */
774static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
775 struct tipc_bearer *bearer, int nlflags)
776{
777 void *hdr;
778 struct nlattr *attrs;
779 struct nlattr *prop;
780
781 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
782 nlflags, TIPC_NL_BEARER_GET);
783 if (!hdr)
784 return -EMSGSIZE;
785
786 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
787 if (!attrs)
788 goto msg_full;
789
790 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
791 goto attr_msg_full;
792
793 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
794 if (!prop)
795 goto prop_msg_full;
796 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
797 goto prop_msg_full;
798 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
799 goto prop_msg_full;
800 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
801 goto prop_msg_full;
802 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
803 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
804 goto prop_msg_full;
805
806 nla_nest_end(msg->skb, prop);
807
808#ifdef CONFIG_TIPC_MEDIA_UDP
809 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
810 if (tipc_udp_nl_add_bearer_data(msg, bearer))
811 goto attr_msg_full;
812 }
813#endif
814
815 nla_nest_end(msg->skb, attrs);
816 genlmsg_end(msg->skb, hdr);
817
818 return 0;
819
820prop_msg_full:
821 nla_nest_cancel(msg->skb, prop);
822attr_msg_full:
823 nla_nest_cancel(msg->skb, attrs);
824msg_full:
825 genlmsg_cancel(msg->skb, hdr);
826
827 return -EMSGSIZE;
828}
829
830int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
831{
832 int err;
833 int i = cb->args[0];
834 struct tipc_bearer *bearer;
835 struct tipc_nl_msg msg;
836 struct net *net = sock_net(skb->sk);
837 struct tipc_net *tn = net_generic(net, tipc_net_id);
838
839 if (i == MAX_BEARERS)
840 return 0;
841
842 msg.skb = skb;
843 msg.portid = NETLINK_CB(cb->skb).portid;
844 msg.seq = cb->nlh->nlmsg_seq;
845
846 rtnl_lock();
847 for (i = 0; i < MAX_BEARERS; i++) {
848 bearer = rtnl_dereference(tn->bearer_list[i]);
849 if (!bearer)
850 continue;
851
852 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
853 if (err)
854 break;
855 }
856 rtnl_unlock();
857
858 cb->args[0] = i;
859 return skb->len;
860}
861
862int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
863{
864 int err;
865 char *name;
866 struct sk_buff *rep;
867 struct tipc_bearer *bearer;
868 struct tipc_nl_msg msg;
869 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
870 struct net *net = genl_info_net(info);
871
872 if (!info->attrs[TIPC_NLA_BEARER])
873 return -EINVAL;
874
875 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
876 info->attrs[TIPC_NLA_BEARER],
877 tipc_nl_bearer_policy, info->extack);
878 if (err)
879 return err;
880
881 if (!attrs[TIPC_NLA_BEARER_NAME])
882 return -EINVAL;
883 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
884
885 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
886 if (!rep)
887 return -ENOMEM;
888
889 msg.skb = rep;
890 msg.portid = info->snd_portid;
891 msg.seq = info->snd_seq;
892
893 rtnl_lock();
894 bearer = tipc_bearer_find(net, name);
895 if (!bearer) {
896 err = -EINVAL;
897 NL_SET_ERR_MSG(info->extack, "Bearer not found");
898 goto err_out;
899 }
900
901 err = __tipc_nl_add_bearer(&msg, bearer, 0);
902 if (err)
903 goto err_out;
904 rtnl_unlock();
905
906 return genlmsg_reply(rep, info);
907err_out:
908 rtnl_unlock();
909 nlmsg_free(rep);
910
911 return err;
912}
913
914int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
915{
916 int err;
917 char *name;
918 struct tipc_bearer *bearer;
919 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
920 struct net *net = sock_net(skb->sk);
921
922 if (!info->attrs[TIPC_NLA_BEARER])
923 return -EINVAL;
924
925 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
926 info->attrs[TIPC_NLA_BEARER],
927 tipc_nl_bearer_policy, info->extack);
928 if (err)
929 return err;
930
931 if (!attrs[TIPC_NLA_BEARER_NAME])
932 return -EINVAL;
933
934 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
935
936 bearer = tipc_bearer_find(net, name);
937 if (!bearer) {
938 NL_SET_ERR_MSG(info->extack, "Bearer not found");
939 return -EINVAL;
940 }
941
942 bearer_disable(net, bearer);
943
944 return 0;
945}
946
947int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
948{
949 int err;
950
951 rtnl_lock();
952 err = __tipc_nl_bearer_disable(skb, info);
953 rtnl_unlock();
954
955 return err;
956}
957
958int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
959{
960 int err;
961 char *bearer;
962 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
963 struct net *net = sock_net(skb->sk);
964 u32 domain = 0;
965 u32 prio;
966
967 prio = TIPC_MEDIA_LINK_PRI;
968
969 if (!info->attrs[TIPC_NLA_BEARER])
970 return -EINVAL;
971
972 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
973 info->attrs[TIPC_NLA_BEARER],
974 tipc_nl_bearer_policy, info->extack);
975 if (err)
976 return err;
977
978 if (!attrs[TIPC_NLA_BEARER_NAME])
979 return -EINVAL;
980
981 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
982
983 if (attrs[TIPC_NLA_BEARER_DOMAIN])
984 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
985
986 if (attrs[TIPC_NLA_BEARER_PROP]) {
987 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
988
989 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
990 props);
991 if (err)
992 return err;
993
994 if (props[TIPC_NLA_PROP_PRIO])
995 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
996 }
997
998 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
999 info->extack);
1000}
1001
1002int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1003{
1004 int err;
1005
1006 rtnl_lock();
1007 err = __tipc_nl_bearer_enable(skb, info);
1008 rtnl_unlock();
1009
1010 return err;
1011}
1012
1013int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1014{
1015 int err;
1016 char *name;
1017 struct tipc_bearer *b;
1018 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1019 struct net *net = sock_net(skb->sk);
1020
1021 if (!info->attrs[TIPC_NLA_BEARER])
1022 return -EINVAL;
1023
1024 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1025 info->attrs[TIPC_NLA_BEARER],
1026 tipc_nl_bearer_policy, info->extack);
1027 if (err)
1028 return err;
1029
1030 if (!attrs[TIPC_NLA_BEARER_NAME])
1031 return -EINVAL;
1032 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1033
1034 rtnl_lock();
1035 b = tipc_bearer_find(net, name);
1036 if (!b) {
1037 rtnl_unlock();
1038 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1039 return -EINVAL;
1040 }
1041
1042#ifdef CONFIG_TIPC_MEDIA_UDP
1043 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1044 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1045 rtnl_unlock();
1046 NL_SET_ERR_MSG(info->extack, "UDP option is unsupported");
1047 return -EINVAL;
1048 }
1049
1050 err = tipc_udp_nl_bearer_add(b,
1051 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1052 if (err) {
1053 rtnl_unlock();
1054 return err;
1055 }
1056 }
1057#endif
1058 rtnl_unlock();
1059
1060 return 0;
1061}
1062
1063int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1064{
1065 struct tipc_bearer *b;
1066 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1067 struct net *net = sock_net(skb->sk);
1068 char *name;
1069 int err;
1070
1071 if (!info->attrs[TIPC_NLA_BEARER])
1072 return -EINVAL;
1073
1074 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1075 info->attrs[TIPC_NLA_BEARER],
1076 tipc_nl_bearer_policy, info->extack);
1077 if (err)
1078 return err;
1079
1080 if (!attrs[TIPC_NLA_BEARER_NAME])
1081 return -EINVAL;
1082 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1083
1084 b = tipc_bearer_find(net, name);
1085 if (!b) {
1086 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1087 return -EINVAL;
1088 }
1089
1090 if (attrs[TIPC_NLA_BEARER_PROP]) {
1091 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1092
1093 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1094 props);
1095 if (err)
1096 return err;
1097
1098 if (props[TIPC_NLA_PROP_TOL]) {
1099 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1100 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1101 }
1102 if (props[TIPC_NLA_PROP_PRIO])
1103 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1104 if (props[TIPC_NLA_PROP_WIN])
1105 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1106 if (props[TIPC_NLA_PROP_MTU]) {
1107 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1108 NL_SET_ERR_MSG(info->extack,
1109 "MTU property is unsupported");
1110 return -EINVAL;
1111 }
1112#ifdef CONFIG_TIPC_MEDIA_UDP
1113 if (tipc_udp_mtu_bad(nla_get_u32
1114 (props[TIPC_NLA_PROP_MTU]))) {
1115 NL_SET_ERR_MSG(info->extack,
1116 "MTU value is out-of-range");
1117 return -EINVAL;
1118 }
1119 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1120 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1121#endif
1122 }
1123 }
1124
1125 return 0;
1126}
1127
1128int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1129{
1130 int err;
1131
1132 rtnl_lock();
1133 err = __tipc_nl_bearer_set(skb, info);
1134 rtnl_unlock();
1135
1136 return err;
1137}
1138
1139static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1140 struct tipc_media *media, int nlflags)
1141{
1142 void *hdr;
1143 struct nlattr *attrs;
1144 struct nlattr *prop;
1145
1146 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1147 nlflags, TIPC_NL_MEDIA_GET);
1148 if (!hdr)
1149 return -EMSGSIZE;
1150
1151 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1152 if (!attrs)
1153 goto msg_full;
1154
1155 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1156 goto attr_msg_full;
1157
1158 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1159 if (!prop)
1160 goto prop_msg_full;
1161 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1162 goto prop_msg_full;
1163 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1164 goto prop_msg_full;
1165 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
1166 goto prop_msg_full;
1167 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1168 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1169 goto prop_msg_full;
1170
1171 nla_nest_end(msg->skb, prop);
1172 nla_nest_end(msg->skb, attrs);
1173 genlmsg_end(msg->skb, hdr);
1174
1175 return 0;
1176
1177prop_msg_full:
1178 nla_nest_cancel(msg->skb, prop);
1179attr_msg_full:
1180 nla_nest_cancel(msg->skb, attrs);
1181msg_full:
1182 genlmsg_cancel(msg->skb, hdr);
1183
1184 return -EMSGSIZE;
1185}
1186
1187int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1188{
1189 int err;
1190 int i = cb->args[0];
1191 struct tipc_nl_msg msg;
1192
1193 if (i == MAX_MEDIA)
1194 return 0;
1195
1196 msg.skb = skb;
1197 msg.portid = NETLINK_CB(cb->skb).portid;
1198 msg.seq = cb->nlh->nlmsg_seq;
1199
1200 rtnl_lock();
1201 for (; media_info_array[i] != NULL; i++) {
1202 err = __tipc_nl_add_media(&msg, media_info_array[i],
1203 NLM_F_MULTI);
1204 if (err)
1205 break;
1206 }
1207 rtnl_unlock();
1208
1209 cb->args[0] = i;
1210 return skb->len;
1211}
1212
1213int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1214{
1215 int err;
1216 char *name;
1217 struct tipc_nl_msg msg;
1218 struct tipc_media *media;
1219 struct sk_buff *rep;
1220 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1221
1222 if (!info->attrs[TIPC_NLA_MEDIA])
1223 return -EINVAL;
1224
1225 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1226 info->attrs[TIPC_NLA_MEDIA],
1227 tipc_nl_media_policy, info->extack);
1228 if (err)
1229 return err;
1230
1231 if (!attrs[TIPC_NLA_MEDIA_NAME])
1232 return -EINVAL;
1233 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1234
1235 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1236 if (!rep)
1237 return -ENOMEM;
1238
1239 msg.skb = rep;
1240 msg.portid = info->snd_portid;
1241 msg.seq = info->snd_seq;
1242
1243 rtnl_lock();
1244 media = tipc_media_find(name);
1245 if (!media) {
1246 NL_SET_ERR_MSG(info->extack, "Media not found");
1247 err = -EINVAL;
1248 goto err_out;
1249 }
1250
1251 err = __tipc_nl_add_media(&msg, media, 0);
1252 if (err)
1253 goto err_out;
1254 rtnl_unlock();
1255
1256 return genlmsg_reply(rep, info);
1257err_out:
1258 rtnl_unlock();
1259 nlmsg_free(rep);
1260
1261 return err;
1262}
1263
1264int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1265{
1266 int err;
1267 char *name;
1268 struct tipc_media *m;
1269 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1270
1271 if (!info->attrs[TIPC_NLA_MEDIA])
1272 return -EINVAL;
1273
1274 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1275 info->attrs[TIPC_NLA_MEDIA],
1276 tipc_nl_media_policy, info->extack);
1277
1278 if (!attrs[TIPC_NLA_MEDIA_NAME])
1279 return -EINVAL;
1280 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1281
1282 m = tipc_media_find(name);
1283 if (!m) {
1284 NL_SET_ERR_MSG(info->extack, "Media not found");
1285 return -EINVAL;
1286 }
1287 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1288 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1289
1290 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1291 props);
1292 if (err)
1293 return err;
1294
1295 if (props[TIPC_NLA_PROP_TOL])
1296 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1297 if (props[TIPC_NLA_PROP_PRIO])
1298 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1299 if (props[TIPC_NLA_PROP_WIN])
1300 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1301 if (props[TIPC_NLA_PROP_MTU]) {
1302 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1303 NL_SET_ERR_MSG(info->extack,
1304 "MTU property is unsupported");
1305 return -EINVAL;
1306 }
1307#ifdef CONFIG_TIPC_MEDIA_UDP
1308 if (tipc_udp_mtu_bad(nla_get_u32
1309 (props[TIPC_NLA_PROP_MTU]))) {
1310 NL_SET_ERR_MSG(info->extack,
1311 "MTU value is out-of-range");
1312 return -EINVAL;
1313 }
1314 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1315#endif
1316 }
1317 }
1318
1319 return 0;
1320}
1321
1322int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1323{
1324 int err;
1325
1326 rtnl_lock();
1327 err = __tipc_nl_media_set(skb, info);
1328 rtnl_unlock();
1329
1330 return err;
1331}