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