blob: 72c31ef985eb37e2125cfa80bda7b1b3389e8466 [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)) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200252 NL_SET_ERR_MSG(extack, "Illegal name");
Olivier Deprez92d4c212022-12-06 15:05:30 +0100253 return res;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 }
255
256 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
257 errstr = "illegal priority";
Olivier Deprez0e641232021-09-23 10:07:05 +0200258 NL_SET_ERR_MSG(extack, "Illegal priority");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 goto rejected;
260 }
261
262 m = tipc_media_find(b_names.media_name);
263 if (!m) {
264 errstr = "media not registered";
Olivier Deprez0e641232021-09-23 10:07:05 +0200265 NL_SET_ERR_MSG(extack, "Media not registered");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 goto rejected;
267 }
268
269 if (prio == TIPC_MEDIA_LINK_PRI)
270 prio = m->priority;
271
272 /* Check new bearer vs existing ones and find free bearer id if any */
Olivier Deprez0e641232021-09-23 10:07:05 +0200273 bearer_id = MAX_BEARERS;
274 i = MAX_BEARERS;
275 while (i-- != 0) {
276 b = rtnl_dereference(tn->bearer_list[i]);
277 if (!b) {
278 bearer_id = i;
279 continue;
280 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 if (!strcmp(name, b->name)) {
282 errstr = "already enabled";
Olivier Deprez0e641232021-09-23 10:07:05 +0200283 NL_SET_ERR_MSG(extack, "Already enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284 goto rejected;
285 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200286
287 if (b->priority == prio &&
288 (++with_this_prio > 2)) {
289 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
290 name, prio);
291
292 if (prio == TIPC_MIN_LINK_PRI) {
293 errstr = "cannot adjust to lower";
294 NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
295 goto rejected;
296 }
297
298 pr_warn("Bearer <%s>: trying with adjusted priority\n",
299 name);
300 prio--;
301 bearer_id = MAX_BEARERS;
302 i = MAX_BEARERS;
303 with_this_prio = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305 }
306
307 if (bearer_id >= MAX_BEARERS) {
308 errstr = "max 3 bearers permitted";
Olivier Deprez0e641232021-09-23 10:07:05 +0200309 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310 goto rejected;
311 }
312
313 b = kzalloc(sizeof(*b), GFP_ATOMIC);
314 if (!b)
315 return -ENOMEM;
316
317 strcpy(b->name, name);
318 b->media = m;
319 res = m->enable_media(net, b, attr);
320 if (res) {
321 kfree(b);
322 errstr = "failed to enable media";
Olivier Deprez0e641232021-09-23 10:07:05 +0200323 NL_SET_ERR_MSG(extack, "Failed to enable media");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324 goto rejected;
325 }
326
327 b->identity = bearer_id;
328 b->tolerance = m->tolerance;
Olivier Deprez157378f2022-04-04 15:47:50 +0200329 b->min_win = m->min_win;
330 b->max_win = m->max_win;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331 b->domain = disc_domain;
332 b->net_plane = bearer_id + 'A';
333 b->priority = prio;
Olivier Deprez157378f2022-04-04 15:47:50 +0200334 refcount_set(&b->refcnt, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335
336 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
337 if (res) {
338 bearer_disable(net, b);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 errstr = "failed to create discoverer";
Olivier Deprez0e641232021-09-23 10:07:05 +0200340 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000341 goto rejected;
342 }
343
Olivier Deprez157378f2022-04-04 15:47:50 +0200344 /* Create monitoring data before accepting activate messages */
345 if (tipc_mon_create(net, bearer_id)) {
346 bearer_disable(net, b);
347 kfree_skb(skb);
348 return -ENOMEM;
349 }
350
351 test_and_set_bit_lock(0, &b->up);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
353 if (skb)
354 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
355
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200375bool 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386/**
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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200404 tipc_bearer_put(b);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405 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 }
David Brazdil0f672f62019-12-10 10:32:29 +0000424 if (dev == net->loopback_dev) {
425 dev_put(dev);
426 pr_info("Enabling <%s> not permitted\n", b->name);
427 return -EINVAL;
428 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429
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
David Brazdil0f672f62019-12-10 10:32:29 +0000484 dev = (struct net_device *)rcu_dereference(b->media_ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485 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();
David Brazdil0f672f62019-12-10 10:32:29 +0000521 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522 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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200539 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
540#ifdef CONFIG_TIPC_CRYPTO
541 tipc_crypto_xmit(net, &skb, b, dest, NULL);
542 if (skb)
543#endif
544 b->media->send_msg(net, skb, b, dest);
545 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000546 kfree_skb(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200547 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548 rcu_read_unlock();
549}
550
551/* tipc_bearer_xmit() -send buffer to destination over bearer
552 */
553void tipc_bearer_xmit(struct net *net, u32 bearer_id,
554 struct sk_buff_head *xmitq,
Olivier Deprez157378f2022-04-04 15:47:50 +0200555 struct tipc_media_addr *dst,
556 struct tipc_node *__dnode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557{
558 struct tipc_bearer *b;
559 struct sk_buff *skb, *tmp;
560
561 if (skb_queue_empty(xmitq))
562 return;
563
564 rcu_read_lock();
565 b = bearer_get(net, bearer_id);
566 if (unlikely(!b))
567 __skb_queue_purge(xmitq);
568 skb_queue_walk_safe(xmitq, skb, tmp) {
569 __skb_dequeue(xmitq);
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
571#ifdef CONFIG_TIPC_CRYPTO
572 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
573 if (skb)
574#endif
575 b->media->send_msg(net, skb, b, dst);
576 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000577 kfree_skb(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200578 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579 }
580 rcu_read_unlock();
581}
582
583/* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
584 */
585void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
586 struct sk_buff_head *xmitq)
587{
588 struct tipc_net *tn = tipc_net(net);
Olivier Deprez157378f2022-04-04 15:47:50 +0200589 struct tipc_media_addr *dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590 int net_id = tn->net_id;
591 struct tipc_bearer *b;
592 struct sk_buff *skb, *tmp;
593 struct tipc_msg *hdr;
594
595 rcu_read_lock();
596 b = bearer_get(net, bearer_id);
597 if (unlikely(!b || !test_bit(0, &b->up)))
598 __skb_queue_purge(xmitq);
599 skb_queue_walk_safe(xmitq, skb, tmp) {
600 hdr = buf_msg(skb);
601 msg_set_non_seq(hdr, 1);
602 msg_set_mc_netid(hdr, net_id);
603 __skb_dequeue(xmitq);
Olivier Deprez157378f2022-04-04 15:47:50 +0200604 dst = &b->bcast_addr;
605#ifdef CONFIG_TIPC_CRYPTO
606 tipc_crypto_xmit(net, &skb, b, dst, NULL);
607 if (skb)
608#endif
609 b->media->send_msg(net, skb, b, dst);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000610 }
611 rcu_read_unlock();
612}
613
614/**
615 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
Olivier Deprez157378f2022-04-04 15:47:50 +0200616 * @skb: the received message
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617 * @dev: the net device that the packet was received on
618 * @pt: the packet_type structure which was used to register this handler
619 * @orig_dev: the original receive net device in case the device is a bond
620 *
621 * Accept only packets explicitly sent to this node, or broadcast packets;
622 * ignores packets sent using interface multicast, and traffic sent to other
623 * nodes (which can happen if interface is running in promiscuous mode).
624 */
625static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
626 struct packet_type *pt, struct net_device *orig_dev)
627{
628 struct tipc_bearer *b;
629
630 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +0000631 b = rcu_dereference(dev->tipc_ptr) ?:
632 rcu_dereference(orig_dev->tipc_ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 if (likely(b && test_bit(0, &b->up) &&
634 (skb->pkt_type <= PACKET_MULTICAST))) {
David Brazdil0f672f62019-12-10 10:32:29 +0000635 skb_mark_not_on_list(skb);
Olivier Deprez157378f2022-04-04 15:47:50 +0200636 TIPC_SKB_CB(skb)->flags = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000637 tipc_rcv(dev_net(b->pt.dev), skb, b);
638 rcu_read_unlock();
639 return NET_RX_SUCCESS;
640 }
641 rcu_read_unlock();
642 kfree_skb(skb);
643 return NET_RX_DROP;
644}
645
646/**
647 * tipc_l2_device_event - handle device events from network device
648 * @nb: the context of the notification
649 * @evt: the type of event
650 * @ptr: the net device that the event was on
651 *
652 * This function is called by the Ethernet driver in case of link
653 * change event.
654 */
655static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
656 void *ptr)
657{
658 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
659 struct net *net = dev_net(dev);
660 struct tipc_bearer *b;
661
662 b = rtnl_dereference(dev->tipc_ptr);
663 if (!b)
664 return NOTIFY_DONE;
665
David Brazdil0f672f62019-12-10 10:32:29 +0000666 trace_tipc_l2_device_event(dev, b, evt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000667 switch (evt) {
668 case NETDEV_CHANGE:
669 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
670 test_and_set_bit_lock(0, &b->up);
671 break;
672 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000674 case NETDEV_GOING_DOWN:
675 clear_bit_unlock(0, &b->up);
676 tipc_reset_bearer(net, b);
677 break;
678 case NETDEV_UP:
679 test_and_set_bit_lock(0, &b->up);
680 break;
681 case NETDEV_CHANGEMTU:
682 if (tipc_mtu_bad(dev, 0)) {
683 bearer_disable(net, b);
684 break;
685 }
686 b->mtu = dev->mtu;
687 tipc_reset_bearer(net, b);
688 break;
689 case NETDEV_CHANGEADDR:
690 b->media->raw2addr(b, &b->addr,
691 (char *)dev->dev_addr);
692 tipc_reset_bearer(net, b);
693 break;
694 case NETDEV_UNREGISTER:
695 case NETDEV_CHANGENAME:
696 bearer_disable(net, b);
697 break;
698 }
699 return NOTIFY_OK;
700}
701
702static struct notifier_block notifier = {
703 .notifier_call = tipc_l2_device_event,
704 .priority = 0,
705};
706
707int tipc_bearer_setup(void)
708{
709 return register_netdevice_notifier(&notifier);
710}
711
712void tipc_bearer_cleanup(void)
713{
714 unregister_netdevice_notifier(&notifier);
715}
716
717void tipc_bearer_stop(struct net *net)
718{
719 struct tipc_net *tn = net_generic(net, tipc_net_id);
720 struct tipc_bearer *b;
721 u32 i;
722
723 for (i = 0; i < MAX_BEARERS; i++) {
724 b = rtnl_dereference(tn->bearer_list[i]);
725 if (b) {
726 bearer_disable(net, b);
727 tn->bearer_list[i] = NULL;
728 }
729 }
730}
731
David Brazdil0f672f62019-12-10 10:32:29 +0000732void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
733{
734 struct net_device *dev = net->loopback_dev;
735 struct sk_buff *skb, *_skb;
736 int exp;
737
738 skb_queue_walk(pkts, _skb) {
739 skb = pskb_copy(_skb, GFP_ATOMIC);
740 if (!skb)
741 continue;
742
743 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
744 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
745 kfree_skb(skb);
746 continue;
747 }
748
749 skb_reset_network_header(skb);
750 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
751 dev->dev_addr, skb->len);
752 skb->dev = dev;
753 skb->pkt_type = PACKET_HOST;
754 skb->ip_summed = CHECKSUM_UNNECESSARY;
755 skb->protocol = eth_type_trans(skb, dev);
756 netif_rx_ni(skb);
757 }
758}
759
760static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
761 struct packet_type *pt, struct net_device *od)
762{
763 consume_skb(skb);
764 return NET_RX_SUCCESS;
765}
766
767int tipc_attach_loopback(struct net *net)
768{
769 struct net_device *dev = net->loopback_dev;
770 struct tipc_net *tn = tipc_net(net);
771
772 if (!dev)
773 return -ENODEV;
774
775 dev_hold(dev);
776 tn->loopback_pt.dev = dev;
777 tn->loopback_pt.type = htons(ETH_P_TIPC);
778 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
779 dev_add_pack(&tn->loopback_pt);
780 return 0;
781}
782
783void tipc_detach_loopback(struct net *net)
784{
785 struct tipc_net *tn = tipc_net(net);
786
787 dev_remove_pack(&tn->loopback_pt);
788 dev_put(net->loopback_dev);
789}
790
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791/* Caller should hold rtnl_lock to protect the bearer */
792static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
793 struct tipc_bearer *bearer, int nlflags)
794{
795 void *hdr;
796 struct nlattr *attrs;
797 struct nlattr *prop;
798
799 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
800 nlflags, TIPC_NL_BEARER_GET);
801 if (!hdr)
802 return -EMSGSIZE;
803
David Brazdil0f672f62019-12-10 10:32:29 +0000804 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000805 if (!attrs)
806 goto msg_full;
807
808 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
809 goto attr_msg_full;
810
David Brazdil0f672f62019-12-10 10:32:29 +0000811 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812 if (!prop)
813 goto prop_msg_full;
814 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
815 goto prop_msg_full;
816 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
817 goto prop_msg_full;
Olivier Deprez157378f2022-04-04 15:47:50 +0200818 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000819 goto prop_msg_full;
820 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
821 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
822 goto prop_msg_full;
823
824 nla_nest_end(msg->skb, prop);
825
826#ifdef CONFIG_TIPC_MEDIA_UDP
827 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
828 if (tipc_udp_nl_add_bearer_data(msg, bearer))
829 goto attr_msg_full;
830 }
831#endif
832
833 nla_nest_end(msg->skb, attrs);
834 genlmsg_end(msg->skb, hdr);
835
836 return 0;
837
838prop_msg_full:
839 nla_nest_cancel(msg->skb, prop);
840attr_msg_full:
841 nla_nest_cancel(msg->skb, attrs);
842msg_full:
843 genlmsg_cancel(msg->skb, hdr);
844
845 return -EMSGSIZE;
846}
847
848int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
849{
850 int err;
851 int i = cb->args[0];
852 struct tipc_bearer *bearer;
853 struct tipc_nl_msg msg;
854 struct net *net = sock_net(skb->sk);
855 struct tipc_net *tn = net_generic(net, tipc_net_id);
856
857 if (i == MAX_BEARERS)
858 return 0;
859
860 msg.skb = skb;
861 msg.portid = NETLINK_CB(cb->skb).portid;
862 msg.seq = cb->nlh->nlmsg_seq;
863
864 rtnl_lock();
865 for (i = 0; i < MAX_BEARERS; i++) {
866 bearer = rtnl_dereference(tn->bearer_list[i]);
867 if (!bearer)
868 continue;
869
870 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
871 if (err)
872 break;
873 }
874 rtnl_unlock();
875
876 cb->args[0] = i;
877 return skb->len;
878}
879
880int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
881{
882 int err;
883 char *name;
884 struct sk_buff *rep;
885 struct tipc_bearer *bearer;
886 struct tipc_nl_msg msg;
887 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
888 struct net *net = genl_info_net(info);
889
890 if (!info->attrs[TIPC_NLA_BEARER])
891 return -EINVAL;
892
David Brazdil0f672f62019-12-10 10:32:29 +0000893 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
894 info->attrs[TIPC_NLA_BEARER],
895 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000896 if (err)
897 return err;
898
899 if (!attrs[TIPC_NLA_BEARER_NAME])
900 return -EINVAL;
901 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
902
903 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
904 if (!rep)
905 return -ENOMEM;
906
907 msg.skb = rep;
908 msg.portid = info->snd_portid;
909 msg.seq = info->snd_seq;
910
911 rtnl_lock();
912 bearer = tipc_bearer_find(net, name);
913 if (!bearer) {
914 err = -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200915 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916 goto err_out;
917 }
918
919 err = __tipc_nl_add_bearer(&msg, bearer, 0);
920 if (err)
921 goto err_out;
922 rtnl_unlock();
923
924 return genlmsg_reply(rep, info);
925err_out:
926 rtnl_unlock();
927 nlmsg_free(rep);
928
929 return err;
930}
931
932int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
933{
934 int err;
935 char *name;
936 struct tipc_bearer *bearer;
937 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
938 struct net *net = sock_net(skb->sk);
939
940 if (!info->attrs[TIPC_NLA_BEARER])
941 return -EINVAL;
942
David Brazdil0f672f62019-12-10 10:32:29 +0000943 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
944 info->attrs[TIPC_NLA_BEARER],
945 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000946 if (err)
947 return err;
948
949 if (!attrs[TIPC_NLA_BEARER_NAME])
950 return -EINVAL;
951
952 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
953
954 bearer = tipc_bearer_find(net, name);
Olivier Deprez0e641232021-09-23 10:07:05 +0200955 if (!bearer) {
956 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200958 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000959
960 bearer_disable(net, bearer);
961
962 return 0;
963}
964
965int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
966{
967 int err;
968
969 rtnl_lock();
970 err = __tipc_nl_bearer_disable(skb, info);
971 rtnl_unlock();
972
973 return err;
974}
975
976int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
977{
978 int err;
979 char *bearer;
980 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
981 struct net *net = sock_net(skb->sk);
982 u32 domain = 0;
983 u32 prio;
984
985 prio = TIPC_MEDIA_LINK_PRI;
986
987 if (!info->attrs[TIPC_NLA_BEARER])
988 return -EINVAL;
989
David Brazdil0f672f62019-12-10 10:32:29 +0000990 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
991 info->attrs[TIPC_NLA_BEARER],
992 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000993 if (err)
994 return err;
995
996 if (!attrs[TIPC_NLA_BEARER_NAME])
997 return -EINVAL;
998
999 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1000
1001 if (attrs[TIPC_NLA_BEARER_DOMAIN])
1002 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
1003
1004 if (attrs[TIPC_NLA_BEARER_PROP]) {
1005 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1006
1007 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1008 props);
1009 if (err)
1010 return err;
1011
1012 if (props[TIPC_NLA_PROP_PRIO])
1013 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1014 }
1015
Olivier Deprez0e641232021-09-23 10:07:05 +02001016 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1017 info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001018}
1019
1020int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1021{
1022 int err;
1023
1024 rtnl_lock();
1025 err = __tipc_nl_bearer_enable(skb, info);
1026 rtnl_unlock();
1027
1028 return err;
1029}
1030
1031int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1032{
1033 int err;
1034 char *name;
1035 struct tipc_bearer *b;
1036 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1037 struct net *net = sock_net(skb->sk);
1038
1039 if (!info->attrs[TIPC_NLA_BEARER])
1040 return -EINVAL;
1041
David Brazdil0f672f62019-12-10 10:32:29 +00001042 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1043 info->attrs[TIPC_NLA_BEARER],
1044 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001045 if (err)
1046 return err;
1047
1048 if (!attrs[TIPC_NLA_BEARER_NAME])
1049 return -EINVAL;
1050 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1051
1052 rtnl_lock();
1053 b = tipc_bearer_find(net, name);
1054 if (!b) {
1055 rtnl_unlock();
Olivier Deprez0e641232021-09-23 10:07:05 +02001056 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 return -EINVAL;
1058 }
1059
1060#ifdef CONFIG_TIPC_MEDIA_UDP
1061 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1062 err = tipc_udp_nl_bearer_add(b,
1063 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1064 if (err) {
1065 rtnl_unlock();
1066 return err;
1067 }
1068 }
1069#endif
1070 rtnl_unlock();
1071
1072 return 0;
1073}
1074
1075int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1076{
1077 struct tipc_bearer *b;
1078 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1079 struct net *net = sock_net(skb->sk);
1080 char *name;
1081 int err;
1082
1083 if (!info->attrs[TIPC_NLA_BEARER])
1084 return -EINVAL;
1085
David Brazdil0f672f62019-12-10 10:32:29 +00001086 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1087 info->attrs[TIPC_NLA_BEARER],
1088 tipc_nl_bearer_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089 if (err)
1090 return err;
1091
1092 if (!attrs[TIPC_NLA_BEARER_NAME])
1093 return -EINVAL;
1094 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1095
1096 b = tipc_bearer_find(net, name);
Olivier Deprez0e641232021-09-23 10:07:05 +02001097 if (!b) {
1098 NL_SET_ERR_MSG(info->extack, "Bearer not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001100 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001101
1102 if (attrs[TIPC_NLA_BEARER_PROP]) {
1103 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1104
1105 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1106 props);
1107 if (err)
1108 return err;
1109
1110 if (props[TIPC_NLA_PROP_TOL]) {
1111 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1112 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1113 }
1114 if (props[TIPC_NLA_PROP_PRIO])
1115 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1116 if (props[TIPC_NLA_PROP_WIN])
Olivier Deprez157378f2022-04-04 15:47:50 +02001117 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118 if (props[TIPC_NLA_PROP_MTU]) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001119 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1120 NL_SET_ERR_MSG(info->extack,
1121 "MTU property is unsupported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001123 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001124#ifdef CONFIG_TIPC_MEDIA_UDP
1125 if (tipc_udp_mtu_bad(nla_get_u32
Olivier Deprez0e641232021-09-23 10:07:05 +02001126 (props[TIPC_NLA_PROP_MTU]))) {
1127 NL_SET_ERR_MSG(info->extack,
1128 "MTU value is out-of-range");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001130 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001131 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1132 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1133#endif
1134 }
1135 }
1136
1137 return 0;
1138}
1139
1140int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1141{
1142 int err;
1143
1144 rtnl_lock();
1145 err = __tipc_nl_bearer_set(skb, info);
1146 rtnl_unlock();
1147
1148 return err;
1149}
1150
1151static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1152 struct tipc_media *media, int nlflags)
1153{
1154 void *hdr;
1155 struct nlattr *attrs;
1156 struct nlattr *prop;
1157
1158 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1159 nlflags, TIPC_NL_MEDIA_GET);
1160 if (!hdr)
1161 return -EMSGSIZE;
1162
David Brazdil0f672f62019-12-10 10:32:29 +00001163 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001164 if (!attrs)
1165 goto msg_full;
1166
1167 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1168 goto attr_msg_full;
1169
David Brazdil0f672f62019-12-10 10:32:29 +00001170 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171 if (!prop)
1172 goto prop_msg_full;
1173 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1174 goto prop_msg_full;
1175 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1176 goto prop_msg_full;
Olivier Deprez157378f2022-04-04 15:47:50 +02001177 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001178 goto prop_msg_full;
1179 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1180 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1181 goto prop_msg_full;
1182
1183 nla_nest_end(msg->skb, prop);
1184 nla_nest_end(msg->skb, attrs);
1185 genlmsg_end(msg->skb, hdr);
1186
1187 return 0;
1188
1189prop_msg_full:
1190 nla_nest_cancel(msg->skb, prop);
1191attr_msg_full:
1192 nla_nest_cancel(msg->skb, attrs);
1193msg_full:
1194 genlmsg_cancel(msg->skb, hdr);
1195
1196 return -EMSGSIZE;
1197}
1198
1199int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1200{
1201 int err;
1202 int i = cb->args[0];
1203 struct tipc_nl_msg msg;
1204
1205 if (i == MAX_MEDIA)
1206 return 0;
1207
1208 msg.skb = skb;
1209 msg.portid = NETLINK_CB(cb->skb).portid;
1210 msg.seq = cb->nlh->nlmsg_seq;
1211
1212 rtnl_lock();
1213 for (; media_info_array[i] != NULL; i++) {
1214 err = __tipc_nl_add_media(&msg, media_info_array[i],
1215 NLM_F_MULTI);
1216 if (err)
1217 break;
1218 }
1219 rtnl_unlock();
1220
1221 cb->args[0] = i;
1222 return skb->len;
1223}
1224
1225int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1226{
1227 int err;
1228 char *name;
1229 struct tipc_nl_msg msg;
1230 struct tipc_media *media;
1231 struct sk_buff *rep;
1232 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1233
1234 if (!info->attrs[TIPC_NLA_MEDIA])
1235 return -EINVAL;
1236
David Brazdil0f672f62019-12-10 10:32:29 +00001237 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1238 info->attrs[TIPC_NLA_MEDIA],
1239 tipc_nl_media_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001240 if (err)
1241 return err;
1242
1243 if (!attrs[TIPC_NLA_MEDIA_NAME])
1244 return -EINVAL;
1245 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1246
1247 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1248 if (!rep)
1249 return -ENOMEM;
1250
1251 msg.skb = rep;
1252 msg.portid = info->snd_portid;
1253 msg.seq = info->snd_seq;
1254
1255 rtnl_lock();
1256 media = tipc_media_find(name);
1257 if (!media) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001258 NL_SET_ERR_MSG(info->extack, "Media not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001259 err = -EINVAL;
1260 goto err_out;
1261 }
1262
1263 err = __tipc_nl_add_media(&msg, media, 0);
1264 if (err)
1265 goto err_out;
1266 rtnl_unlock();
1267
1268 return genlmsg_reply(rep, info);
1269err_out:
1270 rtnl_unlock();
1271 nlmsg_free(rep);
1272
1273 return err;
1274}
1275
1276int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1277{
1278 int err;
1279 char *name;
1280 struct tipc_media *m;
1281 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1282
1283 if (!info->attrs[TIPC_NLA_MEDIA])
1284 return -EINVAL;
1285
David Brazdil0f672f62019-12-10 10:32:29 +00001286 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1287 info->attrs[TIPC_NLA_MEDIA],
1288 tipc_nl_media_policy, info->extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001289
1290 if (!attrs[TIPC_NLA_MEDIA_NAME])
1291 return -EINVAL;
1292 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1293
1294 m = tipc_media_find(name);
Olivier Deprez0e641232021-09-23 10:07:05 +02001295 if (!m) {
1296 NL_SET_ERR_MSG(info->extack, "Media not found");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001297 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001298 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001299 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1300 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1301
1302 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1303 props);
1304 if (err)
1305 return err;
1306
1307 if (props[TIPC_NLA_PROP_TOL])
1308 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1309 if (props[TIPC_NLA_PROP_PRIO])
1310 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1311 if (props[TIPC_NLA_PROP_WIN])
Olivier Deprez157378f2022-04-04 15:47:50 +02001312 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001313 if (props[TIPC_NLA_PROP_MTU]) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001314 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1315 NL_SET_ERR_MSG(info->extack,
1316 "MTU property is unsupported");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001317 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001318 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001319#ifdef CONFIG_TIPC_MEDIA_UDP
1320 if (tipc_udp_mtu_bad(nla_get_u32
Olivier Deprez0e641232021-09-23 10:07:05 +02001321 (props[TIPC_NLA_PROP_MTU]))) {
1322 NL_SET_ERR_MSG(info->extack,
1323 "MTU value is out-of-range");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001324 return -EINVAL;
Olivier Deprez0e641232021-09-23 10:07:05 +02001325 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1327#endif
1328 }
1329 }
1330
1331 return 0;
1332}
1333
1334int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1335{
1336 int err;
1337
1338 rtnl_lock();
1339 err = __tipc_nl_media_set(skb, info);
1340 rtnl_unlock();
1341
1342 return err;
1343}