blob: 6911f1cab2063fb5dd3842301c04b91d66c4ba1c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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"
David Brazdil0f672f62019-12-10 10:32:29 +000046#include "trace.h"
Olivier Deprez157378f2022-04-04 15:47:50 +020047#include "crypto.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
49#define MAX_ADDR_STR 60
50
51static struct tipc_media * const media_info_array[] = {
52 &eth_media_info,
53#ifdef CONFIG_TIPC_MEDIA_IB
54 &ib_media_info,
55#endif
56#ifdef CONFIG_TIPC_MEDIA_UDP
57 &udp_media_info,
58#endif
59 NULL
60};
61
62static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
63{
64 struct tipc_net *tn = tipc_net(net);
65
David Brazdil0f672f62019-12-10 10:32:29 +000066 return rcu_dereference(tn->bearer_list[bearer_id]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067}
68
69static void bearer_disable(struct net *net, struct tipc_bearer *b);
70static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
71 struct packet_type *pt, struct net_device *orig_dev);
72
73/**
74 * tipc_media_find - locates specified media object by name
75 */
76struct tipc_media *tipc_media_find(const char *name)
77{
78 u32 i;
79
80 for (i = 0; media_info_array[i] != NULL; i++) {
81 if (!strcmp(media_info_array[i]->name, name))
82 break;
83 }
84 return media_info_array[i];
85}
86
87/**
88 * media_find_id - locates specified media object by type identifier
89 */
90static struct tipc_media *media_find_id(u8 type)
91{
92 u32 i;
93
94 for (i = 0; media_info_array[i] != NULL; i++) {
95 if (media_info_array[i]->type_id == type)
96 break;
97 }
98 return media_info_array[i];
99}
100
101/**
102 * tipc_media_addr_printf - record media address in print buffer
103 */
David Brazdil0f672f62019-12-10 10:32:29 +0000104int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105{
106 char addr_str[MAX_ADDR_STR];
107 struct tipc_media *m;
108 int ret;
109
110 m = media_find_id(a->media_id);
111
112 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
113 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
114 else {
115 u32 i;
116
117 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
118 for (i = 0; i < sizeof(a->value); i++)
David Brazdil0f672f62019-12-10 10:32:29 +0000119 ret += scnprintf(buf + ret, len - ret,
120 "-%x", a->value[i]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 }
David Brazdil0f672f62019-12-10 10:32:29 +0000122 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123}
124
125/**
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)
129 *
130 * Returns 1 if bearer name is valid, otherwise 0.
131 */
132static int bearer_name_validate(const char *name,
133 struct tipc_bearer_names *name_parts)
134{
135 char name_copy[TIPC_MAX_BEARER_NAME];
136 char *media_name;
137 char *if_name;
138 u32 media_len;
139 u32 if_len;
140
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)
146 return 0;
147
148 /* ensure all component parts of bearer name are present */
149 media_name = name_copy;
150 if_name = strchr(media_name, ':');
151 if (if_name == NULL)
152 return 0;
153 *(if_name++) = 0;
154 media_len = if_name - media_name;
155 if_len = strlen(if_name) + 1;
156
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))
160 return 0;
161
162 /* return bearer name components, if necessary */
163 if (name_parts) {
164 strcpy(name_parts->media_name, media_name);
165 strcpy(name_parts->if_name, if_name);
166 }
167 return 1;
168}
169
170/**
171 * tipc_bearer_find - locates bearer object with matching bearer name
172 */
173struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
174{
175 struct tipc_net *tn = net_generic(net, tipc_net_id);
176 struct tipc_bearer *b;
177 u32 i;
178
179 for (i = 0; i < MAX_BEARERS; i++) {
180 b = rtnl_dereference(tn->bearer_list[i]);
181 if (b && (!strcmp(b->name, name)))
182 return b;
183 }
184 return NULL;
185}
186
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.
191 */
192int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
193{
194 struct tipc_net *tn = tipc_net(net);
195 struct tipc_bearer *b;
196
197 if (bearer_id >= MAX_BEARERS)
198 return -EINVAL;
199
200 b = rtnl_dereference(tn->bearer_list[bearer_id]);
201 if (!b)
202 return -EINVAL;
203
204 strcpy(name, b->name);
205 return 0;
206}
207
208void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
209{
210 struct tipc_net *tn = net_generic(net, tipc_net_id);
211 struct tipc_bearer *b;
212
213 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000214 b = rcu_dereference(tn->bearer_list[bearer_id]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215 if (b)
216 tipc_disc_add_dest(b->disc);
217 rcu_read_unlock();
218}
219
220void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
221{
222 struct tipc_net *tn = net_generic(net, tipc_net_id);
223 struct tipc_bearer *b;
224
225 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000226 b = rcu_dereference(tn->bearer_list[bearer_id]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000227 if (b)
228 tipc_disc_remove_dest(b->disc);
229 rcu_read_unlock();
230}
231
232/**
233 * tipc_enable_bearer - enable bearer with the given name
234 */
235static int tipc_enable_bearer(struct net *net, const char *name,
236 u32 disc_domain, u32 prio,
Olivier Deprez0e641232021-09-23 10:07:05 +0200237 struct nlattr *attr[],
238 struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239{
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;
245 struct sk_buff *skb;
246 int bearer_id = 0;
247 int res = -EINVAL;
248 char *errstr = "";
Olivier Deprez0e641232021-09-23 10:07:05 +0200249 u32 i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250
251 if (!bearer_name_validate(name, &b_names)) {
252 errstr = "illegal name";
Olivier Deprez0e641232021-09-23 10:07:05 +0200253 NL_SET_ERR_MSG(extack, "Illegal name");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 goto rejected;
255 }
256
257 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
258 errstr = "illegal priority";
Olivier Deprez0e641232021-09-23 10:07:05 +0200259 NL_SET_ERR_MSG(extack, "Illegal priority");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 goto rejected;
261 }
262
263 m = tipc_media_find(b_names.media_name);
264 if (!m) {
265 errstr = "media not registered";
Olivier Deprez0e641232021-09-23 10:07:05 +0200266 NL_SET_ERR_MSG(extack, "Media not registered");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 goto rejected;
268 }
269
270 if (prio == TIPC_MEDIA_LINK_PRI)
271 prio = m->priority;
272
273 /* Check new bearer vs existing ones and find free bearer id if any */
Olivier Deprez0e641232021-09-23 10:07:05 +0200274 bearer_id = MAX_BEARERS;
275 i = MAX_BEARERS;
276 while (i-- != 0) {
277 b = rtnl_dereference(tn->bearer_list[i]);
278 if (!b) {
279 bearer_id = i;
280 continue;
281 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000282 if (!strcmp(name, b->name)) {
283 errstr = "already enabled";
Olivier Deprez0e641232021-09-23 10:07:05 +0200284 NL_SET_ERR_MSG(extack, "Already enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000285 goto rejected;
286 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200287
288 if (b->priority == prio &&
289 (++with_this_prio > 2)) {
290 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
291 name, prio);
292
293 if (prio == TIPC_MIN_LINK_PRI) {
294 errstr = "cannot adjust to lower";
295 NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
296 goto rejected;
297 }
298
299 pr_warn("Bearer <%s>: trying with adjusted priority\n",
300 name);
301 prio--;
302 bearer_id = MAX_BEARERS;
303 i = MAX_BEARERS;
304 with_this_prio = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306 }
307
308 if (bearer_id >= MAX_BEARERS) {
309 errstr = "max 3 bearers permitted";
Olivier Deprez0e641232021-09-23 10:07:05 +0200310 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311 goto rejected;
312 }
313
314 b = kzalloc(sizeof(*b), GFP_ATOMIC);
315 if (!b)
316 return -ENOMEM;
317
318 strcpy(b->name, name);
319 b->media = m;
320 res = m->enable_media(net, b, attr);
321 if (res) {
322 kfree(b);
323 errstr = "failed to enable media";
Olivier Deprez0e641232021-09-23 10:07:05 +0200324 NL_SET_ERR_MSG(extack, "Failed to enable media");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325 goto rejected;
326 }
327
328 b->identity = bearer_id;
329 b->tolerance = m->tolerance;
Olivier Deprez157378f2022-04-04 15:47:50 +0200330 b->min_win = m->min_win;
331 b->max_win = m->max_win;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332 b->domain = disc_domain;
333 b->net_plane = bearer_id + 'A';
334 b->priority = prio;
Olivier Deprez157378f2022-04-04 15:47:50 +0200335 refcount_set(&b->refcnt, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336
337 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
338 if (res) {
339 bearer_disable(net, b);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 errstr = "failed to create discoverer";
Olivier Deprez0e641232021-09-23 10:07:05 +0200341 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342 goto rejected;
343 }
344
Olivier Deprez157378f2022-04-04 15:47:50 +0200345 /* Create monitoring data before accepting activate messages */
346 if (tipc_mon_create(net, bearer_id)) {
347 bearer_disable(net, b);
348 kfree_skb(skb);
349 return -ENOMEM;
350 }
351
352 test_and_set_bit_lock(0, &b->up);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
354 if (skb)
355 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
356
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000357 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
358
359 return res;
360rejected:
361 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
362 return res;
363}
364
365/**
366 * tipc_reset_bearer - Reset all links established over this bearer
367 */
368static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
369{
370 pr_info("Resetting bearer <%s>\n", b->name);
371 tipc_node_delete_links(net, b->identity);
372 tipc_disc_reset(net, b);
373 return 0;
374}
375
Olivier Deprez157378f2022-04-04 15:47:50 +0200376bool tipc_bearer_hold(struct tipc_bearer *b)
377{
378 return (b && refcount_inc_not_zero(&b->refcnt));
379}
380
381void tipc_bearer_put(struct tipc_bearer *b)
382{
383 if (b && refcount_dec_and_test(&b->refcnt))
384 kfree_rcu(b, rcu);
385}
386
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000387/**
388 * bearer_disable
389 *
390 * Note: This routine assumes caller holds RTNL lock.
391 */
392static void bearer_disable(struct net *net, struct tipc_bearer *b)
393{
394 struct tipc_net *tn = tipc_net(net);
395 int bearer_id = b->identity;
396
397 pr_info("Disabling bearer <%s>\n", b->name);
398 clear_bit_unlock(0, &b->up);
399 tipc_node_delete_links(net, bearer_id);
400 b->media->disable_media(b);
401 RCU_INIT_POINTER(b->media_ptr, NULL);
402 if (b->disc)
403 tipc_disc_delete(b->disc);
404 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200405 tipc_bearer_put(b);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406 tipc_mon_delete(net, bearer_id);
407}
408
409int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
410 struct nlattr *attr[])
411{
412 char *dev_name = strchr((const char *)b->name, ':') + 1;
413 int hwaddr_len = b->media->hwaddr_len;
414 u8 node_id[NODE_ID_LEN] = {0,};
415 struct net_device *dev;
416
417 /* Find device with specified name */
418 dev = dev_get_by_name(net, dev_name);
419 if (!dev)
420 return -ENODEV;
421 if (tipc_mtu_bad(dev, 0)) {
422 dev_put(dev);
423 return -EINVAL;
424 }
David Brazdil0f672f62019-12-10 10:32:29 +0000425 if (dev == net->loopback_dev) {
426 dev_put(dev);
427 pr_info("Enabling <%s> not permitted\n", b->name);
428 return -EINVAL;
429 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430
431 /* Autoconfigure own node identity if needed */
432 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
433 memcpy(node_id, dev->dev_addr, hwaddr_len);
434 tipc_net_init(net, node_id, 0);
435 }
436 if (!tipc_own_id(net)) {
437 dev_put(dev);
438 pr_warn("Failed to obtain node identity\n");
439 return -EINVAL;
440 }
441
442 /* Associate TIPC bearer with L2 bearer */
443 rcu_assign_pointer(b->media_ptr, dev);
444 b->pt.dev = dev;
445 b->pt.type = htons(ETH_P_TIPC);
446 b->pt.func = tipc_l2_rcv_msg;
447 dev_add_pack(&b->pt);
448 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
449 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
450 b->bcast_addr.media_id = b->media->type_id;
451 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
452 b->mtu = dev->mtu;
453 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
454 rcu_assign_pointer(dev->tipc_ptr, b);
455 return 0;
456}
457
458/* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
459 *
460 * Mark L2 bearer as inactive so that incoming buffers are thrown away
461 */
462void tipc_disable_l2_media(struct tipc_bearer *b)
463{
464 struct net_device *dev;
465
466 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
467 dev_remove_pack(&b->pt);
468 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
469 synchronize_net();
470 dev_put(dev);
471}
472
473/**
474 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
475 * @skb: the packet to be sent
476 * @b: the bearer through which the packet is to be sent
477 * @dest: peer destination address
478 */
479int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
480 struct tipc_bearer *b, struct tipc_media_addr *dest)
481{
482 struct net_device *dev;
483 int delta;
484
David Brazdil0f672f62019-12-10 10:32:29 +0000485 dev = (struct net_device *)rcu_dereference(b->media_ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 if (!dev)
487 return 0;
488
489 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
490 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
491 kfree_skb(skb);
492 return 0;
493 }
494 skb_reset_network_header(skb);
495 skb->dev = dev;
496 skb->protocol = htons(ETH_P_TIPC);
497 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
498 dev->dev_addr, skb->len);
499 dev_queue_xmit(skb);
500 return 0;
501}
502
503bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
504{
505 bool supp = false;
506 struct tipc_bearer *b;
507
508 rcu_read_lock();
509 b = bearer_get(net, bearer_id);
510 if (b)
511 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
512 rcu_read_unlock();
513 return supp;
514}
515
516int tipc_bearer_mtu(struct net *net, u32 bearer_id)
517{
518 int mtu = 0;
519 struct tipc_bearer *b;
520
521 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000522 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 if (b)
524 mtu = b->mtu;
525 rcu_read_unlock();
526 return mtu;
527}
528
529/* tipc_bearer_xmit_skb - sends buffer to destination over bearer
530 */
531void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
532 struct sk_buff *skb,
533 struct tipc_media_addr *dest)
534{
535 struct tipc_msg *hdr = buf_msg(skb);
536 struct tipc_bearer *b;
537
538 rcu_read_lock();
539 b = bearer_get(net, bearer_id);
Olivier Deprez157378f2022-04-04 15:47:50 +0200540 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
541#ifdef CONFIG_TIPC_CRYPTO
542 tipc_crypto_xmit(net, &skb, b, dest, NULL);
543 if (skb)
544#endif
545 b->media->send_msg(net, skb, b, dest);
546 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 kfree_skb(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200548 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549 rcu_read_unlock();
550}
551
552/* tipc_bearer_xmit() -send buffer to destination over bearer
553 */
554void tipc_bearer_xmit(struct net *net, u32 bearer_id,
555 struct sk_buff_head *xmitq,
Olivier Deprez157378f2022-04-04 15:47:50 +0200556 struct tipc_media_addr *dst,
557 struct tipc_node *__dnode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000558{
559 struct tipc_bearer *b;
560 struct sk_buff *skb, *tmp;
561
562 if (skb_queue_empty(xmitq))
563 return;
564
565 rcu_read_lock();
566 b = bearer_get(net, bearer_id);
567 if (unlikely(!b))
568 __skb_queue_purge(xmitq);
569 skb_queue_walk_safe(xmitq, skb, tmp) {
570 __skb_dequeue(xmitq);
Olivier Deprez157378f2022-04-04 15:47:50 +0200571 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
572#ifdef CONFIG_TIPC_CRYPTO
573 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
574 if (skb)
575#endif
576 b->media->send_msg(net, skb, b, dst);
577 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000578 kfree_skb(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200579 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580 }
581 rcu_read_unlock();
582}
583
584/* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
585 */
586void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
587 struct sk_buff_head *xmitq)
588{
589 struct tipc_net *tn = tipc_net(net);
Olivier Deprez157378f2022-04-04 15:47:50 +0200590 struct tipc_media_addr *dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000591 int net_id = tn->net_id;
592 struct tipc_bearer *b;
593 struct sk_buff *skb, *tmp;
594 struct tipc_msg *hdr;
595
596 rcu_read_lock();
597 b = bearer_get(net, bearer_id);
598 if (unlikely(!b || !test_bit(0, &b->up)))
599 __skb_queue_purge(xmitq);
600 skb_queue_walk_safe(xmitq, skb, tmp) {
601 hdr = buf_msg(skb);
602 msg_set_non_seq(hdr, 1);
603 msg_set_mc_netid(hdr, net_id);
604 __skb_dequeue(xmitq);
Olivier Deprez157378f2022-04-04 15:47:50 +0200605 dst = &b->bcast_addr;
606#ifdef CONFIG_TIPC_CRYPTO
607 tipc_crypto_xmit(net, &skb, b, dst, NULL);
608 if (skb)
609#endif
610 b->media->send_msg(net, skb, b, dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000611 }
612 rcu_read_unlock();
613}
614
615/**
616 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
Olivier Deprez157378f2022-04-04 15:47:50 +0200617 * @skb: the received message
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 * @dev: the net device that the packet was received on
619 * @pt: the packet_type structure which was used to register this handler
620 * @orig_dev: the original receive net device in case the device is a bond
621 *
622 * Accept only packets explicitly sent to this node, or broadcast packets;
623 * ignores packets sent using interface multicast, and traffic sent to other
624 * nodes (which can happen if interface is running in promiscuous mode).
625 */
626static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
627 struct packet_type *pt, struct net_device *orig_dev)
628{
629 struct tipc_bearer *b;
630
631 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000632 b = rcu_dereference(dev->tipc_ptr) ?:
633 rcu_dereference(orig_dev->tipc_ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000634 if (likely(b && test_bit(0, &b->up) &&
635 (skb->pkt_type <= PACKET_MULTICAST))) {
David Brazdil0f672f62019-12-10 10:32:29 +0000636 skb_mark_not_on_list(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200637 TIPC_SKB_CB(skb)->flags = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638 tipc_rcv(dev_net(b->pt.dev), skb, b);
639 rcu_read_unlock();
640 return NET_RX_SUCCESS;
641 }
642 rcu_read_unlock();
643 kfree_skb(skb);
644 return NET_RX_DROP;
645}
646
647/**
648 * tipc_l2_device_event - handle device events from network device
649 * @nb: the context of the notification
650 * @evt: the type of event
651 * @ptr: the net device that the event was on
652 *
653 * This function is called by the Ethernet driver in case of link
654 * change event.
655 */
656static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
657 void *ptr)
658{
659 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
660 struct net *net = dev_net(dev);
661 struct tipc_bearer *b;
662
663 b = rtnl_dereference(dev->tipc_ptr);
664 if (!b)
665 return NOTIFY_DONE;
666
David Brazdil0f672f62019-12-10 10:32:29 +0000667 trace_tipc_l2_device_event(dev, b, evt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668 switch (evt) {
669 case NETDEV_CHANGE:
670 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
671 test_and_set_bit_lock(0, &b->up);
672 break;
673 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200674 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675 case NETDEV_GOING_DOWN:
676 clear_bit_unlock(0, &b->up);
677 tipc_reset_bearer(net, b);
678 break;
679 case NETDEV_UP:
680 test_and_set_bit_lock(0, &b->up);
681 break;
682 case NETDEV_CHANGEMTU:
683 if (tipc_mtu_bad(dev, 0)) {
684 bearer_disable(net, b);
685 break;
686 }
687 b->mtu = dev->mtu;
688 tipc_reset_bearer(net, b);
689 break;
690 case NETDEV_CHANGEADDR:
691 b->media->raw2addr(b, &b->addr,
692 (char *)dev->dev_addr);
693 tipc_reset_bearer(net, b);
694 break;
695 case NETDEV_UNREGISTER:
696 case NETDEV_CHANGENAME:
697 bearer_disable(net, b);
698 break;
699 }
700 return NOTIFY_OK;
701}
702
703static struct notifier_block notifier = {
704 .notifier_call = tipc_l2_device_event,
705 .priority = 0,
706};
707
708int tipc_bearer_setup(void)
709{
710 return register_netdevice_notifier(&notifier);
711}
712
713void tipc_bearer_cleanup(void)
714{
715 unregister_netdevice_notifier(&notifier);
716}
717
718void tipc_bearer_stop(struct net *net)
719{
720 struct tipc_net *tn = net_generic(net, tipc_net_id);
721 struct tipc_bearer *b;
722 u32 i;
723
724 for (i = 0; i < MAX_BEARERS; i++) {
725 b = rtnl_dereference(tn->bearer_list[i]);
726 if (b) {
727 bearer_disable(net, b);
728 tn->bearer_list[i] = NULL;
729 }
730 }
731}
732
David Brazdil0f672f62019-12-10 10:32:29 +0000733void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
734{
735 struct net_device *dev = net->loopback_dev;
736 struct sk_buff *skb, *_skb;
737 int exp;
738
739 skb_queue_walk(pkts, _skb) {
740 skb = pskb_copy(_skb, GFP_ATOMIC);
741 if (!skb)
742 continue;
743
744 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
745 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
746 kfree_skb(skb);
747 continue;
748 }
749
750 skb_reset_network_header(skb);
751 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
752 dev->dev_addr, skb->len);
753 skb->dev = dev;
754 skb->pkt_type = PACKET_HOST;
755 skb->ip_summed = CHECKSUM_UNNECESSARY;
756 skb->protocol = eth_type_trans(skb, dev);
757 netif_rx_ni(skb);
758 }
759}
760
761static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
762 struct packet_type *pt, struct net_device *od)
763{
764 consume_skb(skb);
765 return NET_RX_SUCCESS;
766}
767
768int tipc_attach_loopback(struct net *net)
769{
770 struct net_device *dev = net->loopback_dev;
771 struct tipc_net *tn = tipc_net(net);
772
773 if (!dev)
774 return -ENODEV;
775
776 dev_hold(dev);
777 tn->loopback_pt.dev = dev;
778 tn->loopback_pt.type = htons(ETH_P_TIPC);
779 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
780 dev_add_pack(&tn->loopback_pt);
781 return 0;
782}
783
784void tipc_detach_loopback(struct net *net)
785{
786 struct tipc_net *tn = tipc_net(net);
787
788 dev_remove_pack(&tn->loopback_pt);
789 dev_put(net->loopback_dev);
790}
791
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792/* Caller should hold rtnl_lock to protect the bearer */
793static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
794 struct tipc_bearer *bearer, int nlflags)
795{
796 void *hdr;
797 struct nlattr *attrs;
798 struct nlattr *prop;
799
800 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
801 nlflags, TIPC_NL_BEARER_GET);
802 if (!hdr)
803 return -EMSGSIZE;
804
David Brazdil0f672f62019-12-10 10:32:29 +0000805 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000806 if (!attrs)
807 goto msg_full;
808
809 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
810 goto attr_msg_full;
811
David Brazdil0f672f62019-12-10 10:32:29 +0000812 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 if (!prop)
814 goto prop_msg_full;
815 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
816 goto prop_msg_full;
817 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
818 goto prop_msg_full;
Olivier Deprez157378f2022-04-04 15:47:50 +0200819 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 goto prop_msg_full;
821 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
822 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
823 goto prop_msg_full;
824
825 nla_nest_end(msg->skb, prop);
826
827#ifdef CONFIG_TIPC_MEDIA_UDP
828 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
829 if (tipc_udp_nl_add_bearer_data(msg, bearer))
830 goto attr_msg_full;
831 }
832#endif
833
834 nla_nest_end(msg->skb, attrs);
835 genlmsg_end(msg->skb, hdr);
836
837 return 0;
838
839prop_msg_full:
840 nla_nest_cancel(msg->skb, prop);
841attr_msg_full:
842 nla_nest_cancel(msg->skb, attrs);
843msg_full:
844 genlmsg_cancel(msg->skb, hdr);
845
846 return -EMSGSIZE;
847}
848
849int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
850{
851 int err;
852 int i = cb->args[0];
853 struct tipc_bearer *bearer;
854 struct tipc_nl_msg msg;
855 struct net *net = sock_net(skb->sk);
856 struct tipc_net *tn = net_generic(net, tipc_net_id);
857
858 if (i == MAX_BEARERS)
859 return 0;
860
861 msg.skb = skb;
862 msg.portid = NETLINK_CB(cb->skb).portid;
863 msg.seq = cb->nlh->nlmsg_seq;
864
865 rtnl_lock();
866 for (i = 0; i < MAX_BEARERS; i++) {
867 bearer = rtnl_dereference(tn->bearer_list[i]);
868 if (!bearer)
869 continue;
870
871 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
872 if (err)
873 break;
874 }
875 rtnl_unlock();
876
877 cb->args[0] = i;
878 return skb->len;
879}
880
881int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
882{
883 int err;
884 char *name;
885 struct sk_buff *rep;
886 struct tipc_bearer *bearer;
887 struct tipc_nl_msg msg;
888 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
889 struct net *net = genl_info_net(info);
890
891 if (!info->attrs[TIPC_NLA_BEARER])
892 return -EINVAL;
893
David Brazdil0f672f62019-12-10 10:32:29 +0000894 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
895 info->attrs[TIPC_NLA_BEARER],
896 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000897 if (err)
898 return err;
899
900 if (!attrs[TIPC_NLA_BEARER_NAME])
901 return -EINVAL;
902 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
903
904 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
905 if (!rep)
906 return -ENOMEM;
907
908 msg.skb = rep;
909 msg.portid = info->snd_portid;
910 msg.seq = info->snd_seq;
911
912 rtnl_lock();
913 bearer = tipc_bearer_find(net, name);
914 if (!bearer) {
915 err = -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200916 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000917 goto err_out;
918 }
919
920 err = __tipc_nl_add_bearer(&msg, bearer, 0);
921 if (err)
922 goto err_out;
923 rtnl_unlock();
924
925 return genlmsg_reply(rep, info);
926err_out:
927 rtnl_unlock();
928 nlmsg_free(rep);
929
930 return err;
931}
932
933int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
934{
935 int err;
936 char *name;
937 struct tipc_bearer *bearer;
938 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
939 struct net *net = sock_net(skb->sk);
940
941 if (!info->attrs[TIPC_NLA_BEARER])
942 return -EINVAL;
943
David Brazdil0f672f62019-12-10 10:32:29 +0000944 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
945 info->attrs[TIPC_NLA_BEARER],
946 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000947 if (err)
948 return err;
949
950 if (!attrs[TIPC_NLA_BEARER_NAME])
951 return -EINVAL;
952
953 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
954
955 bearer = tipc_bearer_find(net, name);
Olivier Deprez0e641232021-09-23 10:07:05 +0200956 if (!bearer) {
957 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200959 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960
961 bearer_disable(net, bearer);
962
963 return 0;
964}
965
966int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
967{
968 int err;
969
970 rtnl_lock();
971 err = __tipc_nl_bearer_disable(skb, info);
972 rtnl_unlock();
973
974 return err;
975}
976
977int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
978{
979 int err;
980 char *bearer;
981 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
982 struct net *net = sock_net(skb->sk);
983 u32 domain = 0;
984 u32 prio;
985
986 prio = TIPC_MEDIA_LINK_PRI;
987
988 if (!info->attrs[TIPC_NLA_BEARER])
989 return -EINVAL;
990
David Brazdil0f672f62019-12-10 10:32:29 +0000991 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
992 info->attrs[TIPC_NLA_BEARER],
993 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000994 if (err)
995 return err;
996
997 if (!attrs[TIPC_NLA_BEARER_NAME])
998 return -EINVAL;
999
1000 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1001
1002 if (attrs[TIPC_NLA_BEARER_DOMAIN])
1003 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
1004
1005 if (attrs[TIPC_NLA_BEARER_PROP]) {
1006 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1007
1008 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1009 props);
1010 if (err)
1011 return err;
1012
1013 if (props[TIPC_NLA_PROP_PRIO])
1014 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1015 }
1016
Olivier Deprez0e641232021-09-23 10:07:05 +02001017 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1018 info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019}
1020
1021int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1022{
1023 int err;
1024
1025 rtnl_lock();
1026 err = __tipc_nl_bearer_enable(skb, info);
1027 rtnl_unlock();
1028
1029 return err;
1030}
1031
1032int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1033{
1034 int err;
1035 char *name;
1036 struct tipc_bearer *b;
1037 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1038 struct net *net = sock_net(skb->sk);
1039
1040 if (!info->attrs[TIPC_NLA_BEARER])
1041 return -EINVAL;
1042
David Brazdil0f672f62019-12-10 10:32:29 +00001043 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1044 info->attrs[TIPC_NLA_BEARER],
1045 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001046 if (err)
1047 return err;
1048
1049 if (!attrs[TIPC_NLA_BEARER_NAME])
1050 return -EINVAL;
1051 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1052
1053 rtnl_lock();
1054 b = tipc_bearer_find(net, name);
1055 if (!b) {
1056 rtnl_unlock();
Olivier Deprez0e641232021-09-23 10:07:05 +02001057 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058 return -EINVAL;
1059 }
1060
1061#ifdef CONFIG_TIPC_MEDIA_UDP
1062 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1063 err = tipc_udp_nl_bearer_add(b,
1064 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1065 if (err) {
1066 rtnl_unlock();
1067 return err;
1068 }
1069 }
1070#endif
1071 rtnl_unlock();
1072
1073 return 0;
1074}
1075
1076int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1077{
1078 struct tipc_bearer *b;
1079 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1080 struct net *net = sock_net(skb->sk);
1081 char *name;
1082 int err;
1083
1084 if (!info->attrs[TIPC_NLA_BEARER])
1085 return -EINVAL;
1086
David Brazdil0f672f62019-12-10 10:32:29 +00001087 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1088 info->attrs[TIPC_NLA_BEARER],
1089 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001090 if (err)
1091 return err;
1092
1093 if (!attrs[TIPC_NLA_BEARER_NAME])
1094 return -EINVAL;
1095 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1096
1097 b = tipc_bearer_find(net, name);
Olivier Deprez0e641232021-09-23 10:07:05 +02001098 if (!b) {
1099 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001101 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102
1103 if (attrs[TIPC_NLA_BEARER_PROP]) {
1104 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1105
1106 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1107 props);
1108 if (err)
1109 return err;
1110
1111 if (props[TIPC_NLA_PROP_TOL]) {
1112 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1113 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1114 }
1115 if (props[TIPC_NLA_PROP_PRIO])
1116 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1117 if (props[TIPC_NLA_PROP_WIN])
Olivier Deprez157378f2022-04-04 15:47:50 +02001118 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001119 if (props[TIPC_NLA_PROP_MTU]) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001120 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1121 NL_SET_ERR_MSG(info->extack,
1122 "MTU property is unsupported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001123 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001124 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001125#ifdef CONFIG_TIPC_MEDIA_UDP
1126 if (tipc_udp_mtu_bad(nla_get_u32
Olivier Deprez0e641232021-09-23 10:07:05 +02001127 (props[TIPC_NLA_PROP_MTU]))) {
1128 NL_SET_ERR_MSG(info->extack,
1129 "MTU value is out-of-range");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001131 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001132 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1133 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1134#endif
1135 }
1136 }
1137
1138 return 0;
1139}
1140
1141int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1142{
1143 int err;
1144
1145 rtnl_lock();
1146 err = __tipc_nl_bearer_set(skb, info);
1147 rtnl_unlock();
1148
1149 return err;
1150}
1151
1152static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1153 struct tipc_media *media, int nlflags)
1154{
1155 void *hdr;
1156 struct nlattr *attrs;
1157 struct nlattr *prop;
1158
1159 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1160 nlflags, TIPC_NL_MEDIA_GET);
1161 if (!hdr)
1162 return -EMSGSIZE;
1163
David Brazdil0f672f62019-12-10 10:32:29 +00001164 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001165 if (!attrs)
1166 goto msg_full;
1167
1168 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1169 goto attr_msg_full;
1170
David Brazdil0f672f62019-12-10 10:32:29 +00001171 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001172 if (!prop)
1173 goto prop_msg_full;
1174 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1175 goto prop_msg_full;
1176 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1177 goto prop_msg_full;
Olivier Deprez157378f2022-04-04 15:47:50 +02001178 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 goto prop_msg_full;
1180 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1181 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1182 goto prop_msg_full;
1183
1184 nla_nest_end(msg->skb, prop);
1185 nla_nest_end(msg->skb, attrs);
1186 genlmsg_end(msg->skb, hdr);
1187
1188 return 0;
1189
1190prop_msg_full:
1191 nla_nest_cancel(msg->skb, prop);
1192attr_msg_full:
1193 nla_nest_cancel(msg->skb, attrs);
1194msg_full:
1195 genlmsg_cancel(msg->skb, hdr);
1196
1197 return -EMSGSIZE;
1198}
1199
1200int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1201{
1202 int err;
1203 int i = cb->args[0];
1204 struct tipc_nl_msg msg;
1205
1206 if (i == MAX_MEDIA)
1207 return 0;
1208
1209 msg.skb = skb;
1210 msg.portid = NETLINK_CB(cb->skb).portid;
1211 msg.seq = cb->nlh->nlmsg_seq;
1212
1213 rtnl_lock();
1214 for (; media_info_array[i] != NULL; i++) {
1215 err = __tipc_nl_add_media(&msg, media_info_array[i],
1216 NLM_F_MULTI);
1217 if (err)
1218 break;
1219 }
1220 rtnl_unlock();
1221
1222 cb->args[0] = i;
1223 return skb->len;
1224}
1225
1226int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1227{
1228 int err;
1229 char *name;
1230 struct tipc_nl_msg msg;
1231 struct tipc_media *media;
1232 struct sk_buff *rep;
1233 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1234
1235 if (!info->attrs[TIPC_NLA_MEDIA])
1236 return -EINVAL;
1237
David Brazdil0f672f62019-12-10 10:32:29 +00001238 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1239 info->attrs[TIPC_NLA_MEDIA],
1240 tipc_nl_media_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001241 if (err)
1242 return err;
1243
1244 if (!attrs[TIPC_NLA_MEDIA_NAME])
1245 return -EINVAL;
1246 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1247
1248 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1249 if (!rep)
1250 return -ENOMEM;
1251
1252 msg.skb = rep;
1253 msg.portid = info->snd_portid;
1254 msg.seq = info->snd_seq;
1255
1256 rtnl_lock();
1257 media = tipc_media_find(name);
1258 if (!media) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001259 NL_SET_ERR_MSG(info->extack, "Media not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001260 err = -EINVAL;
1261 goto err_out;
1262 }
1263
1264 err = __tipc_nl_add_media(&msg, media, 0);
1265 if (err)
1266 goto err_out;
1267 rtnl_unlock();
1268
1269 return genlmsg_reply(rep, info);
1270err_out:
1271 rtnl_unlock();
1272 nlmsg_free(rep);
1273
1274 return err;
1275}
1276
1277int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1278{
1279 int err;
1280 char *name;
1281 struct tipc_media *m;
1282 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1283
1284 if (!info->attrs[TIPC_NLA_MEDIA])
1285 return -EINVAL;
1286
David Brazdil0f672f62019-12-10 10:32:29 +00001287 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1288 info->attrs[TIPC_NLA_MEDIA],
1289 tipc_nl_media_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290
1291 if (!attrs[TIPC_NLA_MEDIA_NAME])
1292 return -EINVAL;
1293 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1294
1295 m = tipc_media_find(name);
Olivier Deprez0e641232021-09-23 10:07:05 +02001296 if (!m) {
1297 NL_SET_ERR_MSG(info->extack, "Media not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001298 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001299 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001300 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1301 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1302
1303 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1304 props);
1305 if (err)
1306 return err;
1307
1308 if (props[TIPC_NLA_PROP_TOL])
1309 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1310 if (props[TIPC_NLA_PROP_PRIO])
1311 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1312 if (props[TIPC_NLA_PROP_WIN])
Olivier Deprez157378f2022-04-04 15:47:50 +02001313 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314 if (props[TIPC_NLA_PROP_MTU]) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001315 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1316 NL_SET_ERR_MSG(info->extack,
1317 "MTU property is unsupported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001318 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001319 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001320#ifdef CONFIG_TIPC_MEDIA_UDP
1321 if (tipc_udp_mtu_bad(nla_get_u32
Olivier Deprez0e641232021-09-23 10:07:05 +02001322 (props[TIPC_NLA_PROP_MTU]))) {
1323 NL_SET_ERR_MSG(info->extack,
1324 "MTU value is out-of-range");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001325 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001326 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001327 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1328#endif
1329 }
1330 }
1331
1332 return 0;
1333}
1334
1335int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1336{
1337 int err;
1338
1339 rtnl_lock();
1340 err = __tipc_nl_media_set(skb, info);
1341 rtnl_unlock();
1342
1343 return err;
1344}