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