1 /*
2 * net/tipc/bearer.c: TIPC bearer code
3 *
4 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
5 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <net/sock.h>
38 #include "core.h"
39 #include "bearer.h"
40 #include "link.h"
41 #include "discover.h"
42 #include "monitor.h"
43 #include "bcast.h"
44 #include "netlink.h"
45 #include "udp_media.h"
46 #include "trace.h"
47 #include "crypto.h"
48
49 #define MAX_ADDR_STR 60
50
51 static struct tipc_media * const media_info_array[] = {
52 ð_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
bearer_get(struct net * net,int bearer_id)62 static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
63 {
64 struct tipc_net *tn = tipc_net(net);
65
66 return rcu_dereference(tn->bearer_list[bearer_id]);
67 }
68
69 static void bearer_disable(struct net *net, struct tipc_bearer *b);
70 static 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 */
tipc_media_find(const char * name)76 struct 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 */
media_find_id(u8 type)90 static 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 */
tipc_media_addr_printf(char * buf,int len,struct tipc_media_addr * a)104 int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
105 {
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++)
119 ret += scnprintf(buf + ret, len - ret,
120 "-%x", a->value[i]);
121 }
122 return ret;
123 }
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 */
bearer_name_validate(const char * name,struct tipc_bearer_names * name_parts)132 static 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 */
tipc_bearer_find(struct net * net,const char * name)173 struct 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 */
tipc_bearer_get_name(struct net * net,char * name,u32 bearer_id)192 int 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
tipc_bearer_add_dest(struct net * net,u32 bearer_id,u32 dest)208 void 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();
214 b = rcu_dereference(tn->bearer_list[bearer_id]);
215 if (b)
216 tipc_disc_add_dest(b->disc);
217 rcu_read_unlock();
218 }
219
tipc_bearer_remove_dest(struct net * net,u32 bearer_id,u32 dest)220 void 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();
226 b = rcu_dereference(tn->bearer_list[bearer_id]);
227 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 */
tipc_enable_bearer(struct net * net,const char * name,u32 disc_domain,u32 prio,struct nlattr * attr[],struct netlink_ext_ack * extack)235 static int tipc_enable_bearer(struct net *net, const char *name,
236 u32 disc_domain, u32 prio,
237 struct nlattr *attr[],
238 struct netlink_ext_ack *extack)
239 {
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 = "";
249 u32 i;
250
251 if (!bearer_name_validate(name, &b_names)) {
252 NL_SET_ERR_MSG(extack, "Illegal name");
253 return res;
254 }
255
256 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
257 errstr = "illegal priority";
258 NL_SET_ERR_MSG(extack, "Illegal priority");
259 goto rejected;
260 }
261
262 m = tipc_media_find(b_names.media_name);
263 if (!m) {
264 errstr = "media not registered";
265 NL_SET_ERR_MSG(extack, "Media not registered");
266 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 */
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 }
281 if (!strcmp(name, b->name)) {
282 errstr = "already enabled";
283 NL_SET_ERR_MSG(extack, "Already enabled");
284 goto rejected;
285 }
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;
304 }
305 }
306
307 if (bearer_id >= MAX_BEARERS) {
308 errstr = "max 3 bearers permitted";
309 NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
310 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";
323 NL_SET_ERR_MSG(extack, "Failed to enable media");
324 goto rejected;
325 }
326
327 b->identity = bearer_id;
328 b->tolerance = m->tolerance;
329 b->min_win = m->min_win;
330 b->max_win = m->max_win;
331 b->domain = disc_domain;
332 b->net_plane = bearer_id + 'A';
333 b->priority = prio;
334 refcount_set(&b->refcnt, 1);
335
336 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
337 if (res) {
338 bearer_disable(net, b);
339 errstr = "failed to create discoverer";
340 NL_SET_ERR_MSG(extack, "Failed to create discoverer");
341 goto rejected;
342 }
343
344 /* 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);
352 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
356 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
357
358 return res;
359 rejected:
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 */
tipc_reset_bearer(struct net * net,struct tipc_bearer * b)367 static 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
tipc_bearer_hold(struct tipc_bearer * b)375 bool tipc_bearer_hold(struct tipc_bearer *b)
376 {
377 return (b && refcount_inc_not_zero(&b->refcnt));
378 }
379
tipc_bearer_put(struct tipc_bearer * b)380 void tipc_bearer_put(struct tipc_bearer *b)
381 {
382 if (b && refcount_dec_and_test(&b->refcnt))
383 kfree_rcu(b, rcu);
384 }
385
386 /**
387 * bearer_disable
388 *
389 * Note: This routine assumes caller holds RTNL lock.
390 */
bearer_disable(struct net * net,struct tipc_bearer * b)391 static void bearer_disable(struct net *net, struct tipc_bearer *b)
392 {
393 struct tipc_net *tn = tipc_net(net);
394 int bearer_id = b->identity;
395
396 pr_info("Disabling bearer <%s>\n", b->name);
397 clear_bit_unlock(0, &b->up);
398 tipc_node_delete_links(net, bearer_id);
399 b->media->disable_media(b);
400 RCU_INIT_POINTER(b->media_ptr, NULL);
401 if (b->disc)
402 tipc_disc_delete(b->disc);
403 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
404 tipc_bearer_put(b);
405 tipc_mon_delete(net, bearer_id);
406 }
407
tipc_enable_l2_media(struct net * net,struct tipc_bearer * b,struct nlattr * attr[])408 int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
409 struct nlattr *attr[])
410 {
411 char *dev_name = strchr((const char *)b->name, ':') + 1;
412 int hwaddr_len = b->media->hwaddr_len;
413 u8 node_id[NODE_ID_LEN] = {0,};
414 struct net_device *dev;
415
416 /* Find device with specified name */
417 dev = dev_get_by_name(net, dev_name);
418 if (!dev)
419 return -ENODEV;
420 if (tipc_mtu_bad(dev, 0)) {
421 dev_put(dev);
422 return -EINVAL;
423 }
424 if (dev == net->loopback_dev) {
425 dev_put(dev);
426 pr_info("Enabling <%s> not permitted\n", b->name);
427 return -EINVAL;
428 }
429
430 /* Autoconfigure own node identity if needed */
431 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
432 memcpy(node_id, dev->dev_addr, hwaddr_len);
433 tipc_net_init(net, node_id, 0);
434 }
435 if (!tipc_own_id(net)) {
436 dev_put(dev);
437 pr_warn("Failed to obtain node identity\n");
438 return -EINVAL;
439 }
440
441 /* Associate TIPC bearer with L2 bearer */
442 rcu_assign_pointer(b->media_ptr, dev);
443 b->pt.dev = dev;
444 b->pt.type = htons(ETH_P_TIPC);
445 b->pt.func = tipc_l2_rcv_msg;
446 dev_add_pack(&b->pt);
447 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
448 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
449 b->bcast_addr.media_id = b->media->type_id;
450 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
451 b->mtu = dev->mtu;
452 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
453 rcu_assign_pointer(dev->tipc_ptr, b);
454 return 0;
455 }
456
457 /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
458 *
459 * Mark L2 bearer as inactive so that incoming buffers are thrown away
460 */
tipc_disable_l2_media(struct tipc_bearer * b)461 void 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 */
tipc_l2_send_msg(struct net * net,struct sk_buff * skb,struct tipc_bearer * b,struct tipc_media_addr * dest)478 int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
479 struct tipc_bearer *b, struct tipc_media_addr *dest)
480 {
481 struct net_device *dev;
482 int delta;
483
484 dev = (struct net_device *)rcu_dereference(b->media_ptr);
485 if (!dev)
486 return 0;
487
488 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
489 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
490 kfree_skb(skb);
491 return 0;
492 }
493 skb_reset_network_header(skb);
494 skb->dev = dev;
495 skb->protocol = htons(ETH_P_TIPC);
496 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
497 dev->dev_addr, skb->len);
498 dev_queue_xmit(skb);
499 return 0;
500 }
501
tipc_bearer_bcast_support(struct net * net,u32 bearer_id)502 bool 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
tipc_bearer_mtu(struct net * net,u32 bearer_id)515 int tipc_bearer_mtu(struct net *net, u32 bearer_id)
516 {
517 int mtu = 0;
518 struct tipc_bearer *b;
519
520 rcu_read_lock();
521 b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]);
522 if (b)
523 mtu = b->mtu;
524 rcu_read_unlock();
525 return mtu;
526 }
527
528 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
529 */
tipc_bearer_xmit_skb(struct net * net,u32 bearer_id,struct sk_buff * skb,struct tipc_media_addr * dest)530 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
531 struct sk_buff *skb,
532 struct tipc_media_addr *dest)
533 {
534 struct tipc_msg *hdr = buf_msg(skb);
535 struct tipc_bearer *b;
536
537 rcu_read_lock();
538 b = bearer_get(net, bearer_id);
539 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
540 #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 {
546 kfree_skb(skb);
547 }
548 rcu_read_unlock();
549 }
550
551 /* tipc_bearer_xmit() -send buffer to destination over bearer
552 */
tipc_bearer_xmit(struct net * net,u32 bearer_id,struct sk_buff_head * xmitq,struct tipc_media_addr * dst,struct tipc_node * __dnode)553 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
554 struct sk_buff_head *xmitq,
555 struct tipc_media_addr *dst,
556 struct tipc_node *__dnode)
557 {
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);
570 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 {
577 kfree_skb(skb);
578 }
579 }
580 rcu_read_unlock();
581 }
582
583 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
584 */
tipc_bearer_bc_xmit(struct net * net,u32 bearer_id,struct sk_buff_head * xmitq)585 void 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);
589 struct tipc_media_addr *dst;
590 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);
604 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);
610 }
611 rcu_read_unlock();
612 }
613
614 /**
615 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
616 * @skb: the received message
617 * @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 */
tipc_l2_rcv_msg(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)625 static 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();
631 b = rcu_dereference(dev->tipc_ptr) ?:
632 rcu_dereference(orig_dev->tipc_ptr);
633 if (likely(b && test_bit(0, &b->up) &&
634 (skb->pkt_type <= PACKET_MULTICAST))) {
635 skb_mark_not_on_list(skb);
636 TIPC_SKB_CB(skb)->flags = 0;
637 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 */
tipc_l2_device_event(struct notifier_block * nb,unsigned long evt,void * ptr)655 static 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
666 trace_tipc_l2_device_event(dev, b, evt);
667 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 }
673 fallthrough;
674 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
702 static struct notifier_block notifier = {
703 .notifier_call = tipc_l2_device_event,
704 .priority = 0,
705 };
706
tipc_bearer_setup(void)707 int tipc_bearer_setup(void)
708 {
709 return register_netdevice_notifier(¬ifier);
710 }
711
tipc_bearer_cleanup(void)712 void tipc_bearer_cleanup(void)
713 {
714 unregister_netdevice_notifier(¬ifier);
715 }
716
tipc_bearer_stop(struct net * net)717 void 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
tipc_clone_to_loopback(struct net * net,struct sk_buff_head * pkts)732 void 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
tipc_loopback_rcv_pkt(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * od)760 static 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
tipc_attach_loopback(struct net * net)767 int 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
tipc_detach_loopback(struct net * net)783 void 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
791 /* Caller should hold rtnl_lock to protect the bearer */
__tipc_nl_add_bearer(struct tipc_nl_msg * msg,struct tipc_bearer * bearer,int nlflags)792 static 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
804 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
805 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
811 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
812 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;
818 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
819 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
838 prop_msg_full:
839 nla_nest_cancel(msg->skb, prop);
840 attr_msg_full:
841 nla_nest_cancel(msg->skb, attrs);
842 msg_full:
843 genlmsg_cancel(msg->skb, hdr);
844
845 return -EMSGSIZE;
846 }
847
tipc_nl_bearer_dump(struct sk_buff * skb,struct netlink_callback * cb)848 int 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
tipc_nl_bearer_get(struct sk_buff * skb,struct genl_info * info)880 int 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
893 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
894 info->attrs[TIPC_NLA_BEARER],
895 tipc_nl_bearer_policy, info->extack);
896 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;
915 NL_SET_ERR_MSG(info->extack, "Bearer not found");
916 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);
925 err_out:
926 rtnl_unlock();
927 nlmsg_free(rep);
928
929 return err;
930 }
931
__tipc_nl_bearer_disable(struct sk_buff * skb,struct genl_info * info)932 int __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
943 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
944 info->attrs[TIPC_NLA_BEARER],
945 tipc_nl_bearer_policy, info->extack);
946 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);
955 if (!bearer) {
956 NL_SET_ERR_MSG(info->extack, "Bearer not found");
957 return -EINVAL;
958 }
959
960 bearer_disable(net, bearer);
961
962 return 0;
963 }
964
tipc_nl_bearer_disable(struct sk_buff * skb,struct genl_info * info)965 int 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
__tipc_nl_bearer_enable(struct sk_buff * skb,struct genl_info * info)976 int __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
990 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
991 info->attrs[TIPC_NLA_BEARER],
992 tipc_nl_bearer_policy, info->extack);
993 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
1016 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1017 info->extack);
1018 }
1019
tipc_nl_bearer_enable(struct sk_buff * skb,struct genl_info * info)1020 int 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
tipc_nl_bearer_add(struct sk_buff * skb,struct genl_info * info)1031 int 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
1042 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1043 info->attrs[TIPC_NLA_BEARER],
1044 tipc_nl_bearer_policy, info->extack);
1045 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();
1056 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1057 return -EINVAL;
1058 }
1059
1060 #ifdef CONFIG_TIPC_MEDIA_UDP
1061 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1062 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1063 rtnl_unlock();
1064 NL_SET_ERR_MSG(info->extack, "UDP option is unsupported");
1065 return -EINVAL;
1066 }
1067
1068 err = tipc_udp_nl_bearer_add(b,
1069 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1070 if (err) {
1071 rtnl_unlock();
1072 return err;
1073 }
1074 }
1075 #endif
1076 rtnl_unlock();
1077
1078 return 0;
1079 }
1080
__tipc_nl_bearer_set(struct sk_buff * skb,struct genl_info * info)1081 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1082 {
1083 struct tipc_bearer *b;
1084 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1085 struct net *net = sock_net(skb->sk);
1086 char *name;
1087 int err;
1088
1089 if (!info->attrs[TIPC_NLA_BEARER])
1090 return -EINVAL;
1091
1092 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1093 info->attrs[TIPC_NLA_BEARER],
1094 tipc_nl_bearer_policy, info->extack);
1095 if (err)
1096 return err;
1097
1098 if (!attrs[TIPC_NLA_BEARER_NAME])
1099 return -EINVAL;
1100 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1101
1102 b = tipc_bearer_find(net, name);
1103 if (!b) {
1104 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1105 return -EINVAL;
1106 }
1107
1108 if (attrs[TIPC_NLA_BEARER_PROP]) {
1109 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1110
1111 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1112 props);
1113 if (err)
1114 return err;
1115
1116 if (props[TIPC_NLA_PROP_TOL]) {
1117 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1118 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1119 }
1120 if (props[TIPC_NLA_PROP_PRIO])
1121 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1122 if (props[TIPC_NLA_PROP_WIN])
1123 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1124 if (props[TIPC_NLA_PROP_MTU]) {
1125 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1126 NL_SET_ERR_MSG(info->extack,
1127 "MTU property is unsupported");
1128 return -EINVAL;
1129 }
1130 #ifdef CONFIG_TIPC_MEDIA_UDP
1131 if (tipc_udp_mtu_bad(nla_get_u32
1132 (props[TIPC_NLA_PROP_MTU]))) {
1133 NL_SET_ERR_MSG(info->extack,
1134 "MTU value is out-of-range");
1135 return -EINVAL;
1136 }
1137 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1138 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1139 #endif
1140 }
1141 }
1142
1143 return 0;
1144 }
1145
tipc_nl_bearer_set(struct sk_buff * skb,struct genl_info * info)1146 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1147 {
1148 int err;
1149
1150 rtnl_lock();
1151 err = __tipc_nl_bearer_set(skb, info);
1152 rtnl_unlock();
1153
1154 return err;
1155 }
1156
__tipc_nl_add_media(struct tipc_nl_msg * msg,struct tipc_media * media,int nlflags)1157 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1158 struct tipc_media *media, int nlflags)
1159 {
1160 void *hdr;
1161 struct nlattr *attrs;
1162 struct nlattr *prop;
1163
1164 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1165 nlflags, TIPC_NL_MEDIA_GET);
1166 if (!hdr)
1167 return -EMSGSIZE;
1168
1169 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1170 if (!attrs)
1171 goto msg_full;
1172
1173 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1174 goto attr_msg_full;
1175
1176 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1177 if (!prop)
1178 goto prop_msg_full;
1179 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1180 goto prop_msg_full;
1181 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1182 goto prop_msg_full;
1183 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
1184 goto prop_msg_full;
1185 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1186 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1187 goto prop_msg_full;
1188
1189 nla_nest_end(msg->skb, prop);
1190 nla_nest_end(msg->skb, attrs);
1191 genlmsg_end(msg->skb, hdr);
1192
1193 return 0;
1194
1195 prop_msg_full:
1196 nla_nest_cancel(msg->skb, prop);
1197 attr_msg_full:
1198 nla_nest_cancel(msg->skb, attrs);
1199 msg_full:
1200 genlmsg_cancel(msg->skb, hdr);
1201
1202 return -EMSGSIZE;
1203 }
1204
tipc_nl_media_dump(struct sk_buff * skb,struct netlink_callback * cb)1205 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1206 {
1207 int err;
1208 int i = cb->args[0];
1209 struct tipc_nl_msg msg;
1210
1211 if (i == MAX_MEDIA)
1212 return 0;
1213
1214 msg.skb = skb;
1215 msg.portid = NETLINK_CB(cb->skb).portid;
1216 msg.seq = cb->nlh->nlmsg_seq;
1217
1218 rtnl_lock();
1219 for (; media_info_array[i] != NULL; i++) {
1220 err = __tipc_nl_add_media(&msg, media_info_array[i],
1221 NLM_F_MULTI);
1222 if (err)
1223 break;
1224 }
1225 rtnl_unlock();
1226
1227 cb->args[0] = i;
1228 return skb->len;
1229 }
1230
tipc_nl_media_get(struct sk_buff * skb,struct genl_info * info)1231 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1232 {
1233 int err;
1234 char *name;
1235 struct tipc_nl_msg msg;
1236 struct tipc_media *media;
1237 struct sk_buff *rep;
1238 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1239
1240 if (!info->attrs[TIPC_NLA_MEDIA])
1241 return -EINVAL;
1242
1243 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1244 info->attrs[TIPC_NLA_MEDIA],
1245 tipc_nl_media_policy, info->extack);
1246 if (err)
1247 return err;
1248
1249 if (!attrs[TIPC_NLA_MEDIA_NAME])
1250 return -EINVAL;
1251 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1252
1253 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1254 if (!rep)
1255 return -ENOMEM;
1256
1257 msg.skb = rep;
1258 msg.portid = info->snd_portid;
1259 msg.seq = info->snd_seq;
1260
1261 rtnl_lock();
1262 media = tipc_media_find(name);
1263 if (!media) {
1264 NL_SET_ERR_MSG(info->extack, "Media not found");
1265 err = -EINVAL;
1266 goto err_out;
1267 }
1268
1269 err = __tipc_nl_add_media(&msg, media, 0);
1270 if (err)
1271 goto err_out;
1272 rtnl_unlock();
1273
1274 return genlmsg_reply(rep, info);
1275 err_out:
1276 rtnl_unlock();
1277 nlmsg_free(rep);
1278
1279 return err;
1280 }
1281
__tipc_nl_media_set(struct sk_buff * skb,struct genl_info * info)1282 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1283 {
1284 int err;
1285 char *name;
1286 struct tipc_media *m;
1287 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1288
1289 if (!info->attrs[TIPC_NLA_MEDIA])
1290 return -EINVAL;
1291
1292 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1293 info->attrs[TIPC_NLA_MEDIA],
1294 tipc_nl_media_policy, info->extack);
1295
1296 if (!attrs[TIPC_NLA_MEDIA_NAME])
1297 return -EINVAL;
1298 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1299
1300 m = tipc_media_find(name);
1301 if (!m) {
1302 NL_SET_ERR_MSG(info->extack, "Media not found");
1303 return -EINVAL;
1304 }
1305 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1306 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1307
1308 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1309 props);
1310 if (err)
1311 return err;
1312
1313 if (props[TIPC_NLA_PROP_TOL])
1314 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1315 if (props[TIPC_NLA_PROP_PRIO])
1316 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1317 if (props[TIPC_NLA_PROP_WIN])
1318 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1319 if (props[TIPC_NLA_PROP_MTU]) {
1320 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1321 NL_SET_ERR_MSG(info->extack,
1322 "MTU property is unsupported");
1323 return -EINVAL;
1324 }
1325 #ifdef CONFIG_TIPC_MEDIA_UDP
1326 if (tipc_udp_mtu_bad(nla_get_u32
1327 (props[TIPC_NLA_PROP_MTU]))) {
1328 NL_SET_ERR_MSG(info->extack,
1329 "MTU value is out-of-range");
1330 return -EINVAL;
1331 }
1332 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1333 #endif
1334 }
1335 }
1336
1337 return 0;
1338 }
1339
tipc_nl_media_set(struct sk_buff * skb,struct genl_info * info)1340 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1341 {
1342 int err;
1343
1344 rtnl_lock();
1345 err = __tipc_nl_media_set(skb, info);
1346 rtnl_unlock();
1347
1348 return err;
1349 }
1350