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
tipc_bearer_min_mtu(struct net *net, u32 bearer_id)528 int tipc_bearer_min_mtu(struct net *net, u32 bearer_id)
529 {
530 int mtu = TIPC_MIN_BEARER_MTU;
531 struct tipc_bearer *b;
532
533 rcu_read_lock();
534 b = bearer_get(net, bearer_id);
535 if (b)
536 mtu += b->encap_hlen;
537 rcu_read_unlock();
538 return mtu;
539 }
540
541 /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
542 */
tipc_bearer_xmit_skb(struct net *net, u32 bearer_id, struct sk_buff *skb, struct tipc_media_addr *dest)543 void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
544 struct sk_buff *skb,
545 struct tipc_media_addr *dest)
546 {
547 struct tipc_msg *hdr = buf_msg(skb);
548 struct tipc_bearer *b;
549
550 rcu_read_lock();
551 b = bearer_get(net, bearer_id);
552 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
553 #ifdef CONFIG_TIPC_CRYPTO
554 tipc_crypto_xmit(net, &skb, b, dest, NULL);
555 if (skb)
556 #endif
557 b->media->send_msg(net, skb, b, dest);
558 } else {
559 kfree_skb(skb);
560 }
561 rcu_read_unlock();
562 }
563
564 /* tipc_bearer_xmit() -send buffer to destination over bearer
565 */
tipc_bearer_xmit(struct net *net, u32 bearer_id, struct sk_buff_head *xmitq, struct tipc_media_addr *dst, struct tipc_node *__dnode)566 void tipc_bearer_xmit(struct net *net, u32 bearer_id,
567 struct sk_buff_head *xmitq,
568 struct tipc_media_addr *dst,
569 struct tipc_node *__dnode)
570 {
571 struct tipc_bearer *b;
572 struct sk_buff *skb, *tmp;
573
574 if (skb_queue_empty(xmitq))
575 return;
576
577 rcu_read_lock();
578 b = bearer_get(net, bearer_id);
579 if (unlikely(!b))
580 __skb_queue_purge(xmitq);
581 skb_queue_walk_safe(xmitq, skb, tmp) {
582 __skb_dequeue(xmitq);
583 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
584 #ifdef CONFIG_TIPC_CRYPTO
585 tipc_crypto_xmit(net, &skb, b, dst, __dnode);
586 if (skb)
587 #endif
588 b->media->send_msg(net, skb, b, dst);
589 } else {
590 kfree_skb(skb);
591 }
592 }
593 rcu_read_unlock();
594 }
595
596 /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
597 */
tipc_bearer_bc_xmit(struct net *net, u32 bearer_id, struct sk_buff_head *xmitq)598 void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
599 struct sk_buff_head *xmitq)
600 {
601 struct tipc_net *tn = tipc_net(net);
602 struct tipc_media_addr *dst;
603 int net_id = tn->net_id;
604 struct tipc_bearer *b;
605 struct sk_buff *skb, *tmp;
606 struct tipc_msg *hdr;
607
608 rcu_read_lock();
609 b = bearer_get(net, bearer_id);
610 if (unlikely(!b || !test_bit(0, &b->up)))
611 __skb_queue_purge(xmitq);
612 skb_queue_walk_safe(xmitq, skb, tmp) {
613 hdr = buf_msg(skb);
614 msg_set_non_seq(hdr, 1);
615 msg_set_mc_netid(hdr, net_id);
616 __skb_dequeue(xmitq);
617 dst = &b->bcast_addr;
618 #ifdef CONFIG_TIPC_CRYPTO
619 tipc_crypto_xmit(net, &skb, b, dst, NULL);
620 if (skb)
621 #endif
622 b->media->send_msg(net, skb, b, dst);
623 }
624 rcu_read_unlock();
625 }
626
627 /**
628 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
629 * @skb: the received message
630 * @dev: the net device that the packet was received on
631 * @pt: the packet_type structure which was used to register this handler
632 * @orig_dev: the original receive net device in case the device is a bond
633 *
634 * Accept only packets explicitly sent to this node, or broadcast packets;
635 * ignores packets sent using interface multicast, and traffic sent to other
636 * nodes (which can happen if interface is running in promiscuous mode).
637 */
tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)638 static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
639 struct packet_type *pt, struct net_device *orig_dev)
640 {
641 struct tipc_bearer *b;
642
643 rcu_read_lock();
644 b = rcu_dereference(dev->tipc_ptr) ?:
645 rcu_dereference(orig_dev->tipc_ptr);
646 if (likely(b && test_bit(0, &b->up) &&
647 (skb->pkt_type <= PACKET_MULTICAST))) {
648 skb_mark_not_on_list(skb);
649 TIPC_SKB_CB(skb)->flags = 0;
650 tipc_rcv(dev_net(b->pt.dev), skb, b);
651 rcu_read_unlock();
652 return NET_RX_SUCCESS;
653 }
654 rcu_read_unlock();
655 kfree_skb(skb);
656 return NET_RX_DROP;
657 }
658
659 /**
660 * tipc_l2_device_event - handle device events from network device
661 * @nb: the context of the notification
662 * @evt: the type of event
663 * @ptr: the net device that the event was on
664 *
665 * This function is called by the Ethernet driver in case of link
666 * change event.
667 */
tipc_l2_device_event(struct notifier_block *nb, unsigned long evt, void *ptr)668 static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
669 void *ptr)
670 {
671 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
672 struct net *net = dev_net(dev);
673 struct tipc_bearer *b;
674
675 b = rtnl_dereference(dev->tipc_ptr);
676 if (!b)
677 return NOTIFY_DONE;
678
679 trace_tipc_l2_device_event(dev, b, evt);
680 switch (evt) {
681 case NETDEV_CHANGE:
682 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
683 test_and_set_bit_lock(0, &b->up);
684 break;
685 }
686 fallthrough;
687 case NETDEV_GOING_DOWN:
688 clear_bit_unlock(0, &b->up);
689 tipc_reset_bearer(net, b);
690 break;
691 case NETDEV_UP:
692 test_and_set_bit_lock(0, &b->up);
693 break;
694 case NETDEV_CHANGEMTU:
695 if (tipc_mtu_bad(dev, 0)) {
696 bearer_disable(net, b);
697 break;
698 }
699 b->mtu = dev->mtu;
700 tipc_reset_bearer(net, b);
701 break;
702 case NETDEV_CHANGEADDR:
703 b->media->raw2addr(b, &b->addr,
704 (char *)dev->dev_addr);
705 tipc_reset_bearer(net, b);
706 break;
707 case NETDEV_UNREGISTER:
708 case NETDEV_CHANGENAME:
709 bearer_disable(net, b);
710 break;
711 }
712 return NOTIFY_OK;
713 }
714
715 static struct notifier_block notifier = {
716 .notifier_call = tipc_l2_device_event,
717 .priority = 0,
718 };
719
tipc_bearer_setup(void)720 int tipc_bearer_setup(void)
721 {
722 return register_netdevice_notifier(¬ifier);
723 }
724
tipc_bearer_cleanup(void)725 void tipc_bearer_cleanup(void)
726 {
727 unregister_netdevice_notifier(¬ifier);
728 }
729
tipc_bearer_stop(struct net *net)730 void tipc_bearer_stop(struct net *net)
731 {
732 struct tipc_net *tn = net_generic(net, tipc_net_id);
733 struct tipc_bearer *b;
734 u32 i;
735
736 for (i = 0; i < MAX_BEARERS; i++) {
737 b = rtnl_dereference(tn->bearer_list[i]);
738 if (b) {
739 bearer_disable(net, b);
740 tn->bearer_list[i] = NULL;
741 }
742 }
743 }
744
tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)745 void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
746 {
747 struct net_device *dev = net->loopback_dev;
748 struct sk_buff *skb, *_skb;
749 int exp;
750
751 skb_queue_walk(pkts, _skb) {
752 skb = pskb_copy(_skb, GFP_ATOMIC);
753 if (!skb)
754 continue;
755
756 exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
757 if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
758 kfree_skb(skb);
759 continue;
760 }
761
762 skb_reset_network_header(skb);
763 dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
764 dev->dev_addr, skb->len);
765 skb->dev = dev;
766 skb->pkt_type = PACKET_HOST;
767 skb->ip_summed = CHECKSUM_UNNECESSARY;
768 skb->protocol = eth_type_trans(skb, dev);
769 netif_rx_ni(skb);
770 }
771 }
772
tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *od)773 static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
774 struct packet_type *pt, struct net_device *od)
775 {
776 consume_skb(skb);
777 return NET_RX_SUCCESS;
778 }
779
tipc_attach_loopback(struct net *net)780 int tipc_attach_loopback(struct net *net)
781 {
782 struct net_device *dev = net->loopback_dev;
783 struct tipc_net *tn = tipc_net(net);
784
785 if (!dev)
786 return -ENODEV;
787
788 dev_hold(dev);
789 tn->loopback_pt.dev = dev;
790 tn->loopback_pt.type = htons(ETH_P_TIPC);
791 tn->loopback_pt.func = tipc_loopback_rcv_pkt;
792 dev_add_pack(&tn->loopback_pt);
793 return 0;
794 }
795
tipc_detach_loopback(struct net *net)796 void tipc_detach_loopback(struct net *net)
797 {
798 struct tipc_net *tn = tipc_net(net);
799
800 dev_remove_pack(&tn->loopback_pt);
801 dev_put(net->loopback_dev);
802 }
803
804 /* Caller should hold rtnl_lock to protect the bearer */
__tipc_nl_add_bearer(struct tipc_nl_msg *msg, struct tipc_bearer *bearer, int nlflags)805 static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
806 struct tipc_bearer *bearer, int nlflags)
807 {
808 void *hdr;
809 struct nlattr *attrs;
810 struct nlattr *prop;
811
812 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
813 nlflags, TIPC_NL_BEARER_GET);
814 if (!hdr)
815 return -EMSGSIZE;
816
817 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
818 if (!attrs)
819 goto msg_full;
820
821 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
822 goto attr_msg_full;
823
824 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
825 if (!prop)
826 goto prop_msg_full;
827 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
828 goto prop_msg_full;
829 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
830 goto prop_msg_full;
831 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
832 goto prop_msg_full;
833 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
834 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
835 goto prop_msg_full;
836
837 nla_nest_end(msg->skb, prop);
838
839 #ifdef CONFIG_TIPC_MEDIA_UDP
840 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
841 if (tipc_udp_nl_add_bearer_data(msg, bearer))
842 goto attr_msg_full;
843 }
844 #endif
845
846 nla_nest_end(msg->skb, attrs);
847 genlmsg_end(msg->skb, hdr);
848
849 return 0;
850
851 prop_msg_full:
852 nla_nest_cancel(msg->skb, prop);
853 attr_msg_full:
854 nla_nest_cancel(msg->skb, attrs);
855 msg_full:
856 genlmsg_cancel(msg->skb, hdr);
857
858 return -EMSGSIZE;
859 }
860
tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)861 int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
862 {
863 int err;
864 int i = cb->args[0];
865 struct tipc_bearer *bearer;
866 struct tipc_nl_msg msg;
867 struct net *net = sock_net(skb->sk);
868 struct tipc_net *tn = net_generic(net, tipc_net_id);
869
870 if (i == MAX_BEARERS)
871 return 0;
872
873 msg.skb = skb;
874 msg.portid = NETLINK_CB(cb->skb).portid;
875 msg.seq = cb->nlh->nlmsg_seq;
876
877 rtnl_lock();
878 for (i = 0; i < MAX_BEARERS; i++) {
879 bearer = rtnl_dereference(tn->bearer_list[i]);
880 if (!bearer)
881 continue;
882
883 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
884 if (err)
885 break;
886 }
887 rtnl_unlock();
888
889 cb->args[0] = i;
890 return skb->len;
891 }
892
tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)893 int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
894 {
895 int err;
896 char *name;
897 struct sk_buff *rep;
898 struct tipc_bearer *bearer;
899 struct tipc_nl_msg msg;
900 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
901 struct net *net = genl_info_net(info);
902
903 if (!info->attrs[TIPC_NLA_BEARER])
904 return -EINVAL;
905
906 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
907 info->attrs[TIPC_NLA_BEARER],
908 tipc_nl_bearer_policy, info->extack);
909 if (err)
910 return err;
911
912 if (!attrs[TIPC_NLA_BEARER_NAME])
913 return -EINVAL;
914 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
915
916 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
917 if (!rep)
918 return -ENOMEM;
919
920 msg.skb = rep;
921 msg.portid = info->snd_portid;
922 msg.seq = info->snd_seq;
923
924 rtnl_lock();
925 bearer = tipc_bearer_find(net, name);
926 if (!bearer) {
927 err = -EINVAL;
928 NL_SET_ERR_MSG(info->extack, "Bearer not found");
929 goto err_out;
930 }
931
932 err = __tipc_nl_add_bearer(&msg, bearer, 0);
933 if (err)
934 goto err_out;
935 rtnl_unlock();
936
937 return genlmsg_reply(rep, info);
938 err_out:
939 rtnl_unlock();
940 nlmsg_free(rep);
941
942 return err;
943 }
944
__tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)945 int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
946 {
947 int err;
948 char *name;
949 struct tipc_bearer *bearer;
950 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
951 struct net *net = sock_net(skb->sk);
952
953 if (!info->attrs[TIPC_NLA_BEARER])
954 return -EINVAL;
955
956 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
957 info->attrs[TIPC_NLA_BEARER],
958 tipc_nl_bearer_policy, info->extack);
959 if (err)
960 return err;
961
962 if (!attrs[TIPC_NLA_BEARER_NAME])
963 return -EINVAL;
964
965 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
966
967 bearer = tipc_bearer_find(net, name);
968 if (!bearer) {
969 NL_SET_ERR_MSG(info->extack, "Bearer not found");
970 return -EINVAL;
971 }
972
973 bearer_disable(net, bearer);
974
975 return 0;
976 }
977
tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)978 int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
979 {
980 int err;
981
982 rtnl_lock();
983 err = __tipc_nl_bearer_disable(skb, info);
984 rtnl_unlock();
985
986 return err;
987 }
988
__tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)989 int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
990 {
991 int err;
992 char *bearer;
993 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
994 struct net *net = sock_net(skb->sk);
995 u32 domain = 0;
996 u32 prio;
997
998 prio = TIPC_MEDIA_LINK_PRI;
999
1000 if (!info->attrs[TIPC_NLA_BEARER])
1001 return -EINVAL;
1002
1003 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1004 info->attrs[TIPC_NLA_BEARER],
1005 tipc_nl_bearer_policy, info->extack);
1006 if (err)
1007 return err;
1008
1009 if (!attrs[TIPC_NLA_BEARER_NAME])
1010 return -EINVAL;
1011
1012 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1013
1014 if (attrs[TIPC_NLA_BEARER_DOMAIN])
1015 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
1016
1017 if (attrs[TIPC_NLA_BEARER_PROP]) {
1018 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1019
1020 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1021 props);
1022 if (err)
1023 return err;
1024
1025 if (props[TIPC_NLA_PROP_PRIO])
1026 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1027 }
1028
1029 return tipc_enable_bearer(net, bearer, domain, prio, attrs,
1030 info->extack);
1031 }
1032
tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)1033 int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
1034 {
1035 int err;
1036
1037 rtnl_lock();
1038 err = __tipc_nl_bearer_enable(skb, info);
1039 rtnl_unlock();
1040
1041 return err;
1042 }
1043
tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)1044 int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
1045 {
1046 int err;
1047 char *name;
1048 struct tipc_bearer *b;
1049 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1050 struct net *net = sock_net(skb->sk);
1051
1052 if (!info->attrs[TIPC_NLA_BEARER])
1053 return -EINVAL;
1054
1055 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1056 info->attrs[TIPC_NLA_BEARER],
1057 tipc_nl_bearer_policy, info->extack);
1058 if (err)
1059 return err;
1060
1061 if (!attrs[TIPC_NLA_BEARER_NAME])
1062 return -EINVAL;
1063 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1064
1065 rtnl_lock();
1066 b = tipc_bearer_find(net, name);
1067 if (!b) {
1068 rtnl_unlock();
1069 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1070 return -EINVAL;
1071 }
1072
1073 #ifdef CONFIG_TIPC_MEDIA_UDP
1074 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
1075 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1076 rtnl_unlock();
1077 NL_SET_ERR_MSG(info->extack, "UDP option is unsupported");
1078 return -EINVAL;
1079 }
1080
1081 err = tipc_udp_nl_bearer_add(b,
1082 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
1083 if (err) {
1084 rtnl_unlock();
1085 return err;
1086 }
1087 }
1088 #endif
1089 rtnl_unlock();
1090
1091 return 0;
1092 }
1093
__tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)1094 int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1095 {
1096 struct tipc_bearer *b;
1097 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1098 struct net *net = sock_net(skb->sk);
1099 char *name;
1100 int err;
1101
1102 if (!info->attrs[TIPC_NLA_BEARER])
1103 return -EINVAL;
1104
1105 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
1106 info->attrs[TIPC_NLA_BEARER],
1107 tipc_nl_bearer_policy, info->extack);
1108 if (err)
1109 return err;
1110
1111 if (!attrs[TIPC_NLA_BEARER_NAME])
1112 return -EINVAL;
1113 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
1114
1115 b = tipc_bearer_find(net, name);
1116 if (!b) {
1117 NL_SET_ERR_MSG(info->extack, "Bearer not found");
1118 return -EINVAL;
1119 }
1120
1121 if (attrs[TIPC_NLA_BEARER_PROP]) {
1122 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1123
1124 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
1125 props);
1126 if (err)
1127 return err;
1128
1129 if (props[TIPC_NLA_PROP_TOL]) {
1130 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1131 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
1132 }
1133 if (props[TIPC_NLA_PROP_PRIO])
1134 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1135 if (props[TIPC_NLA_PROP_WIN])
1136 b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1137 if (props[TIPC_NLA_PROP_MTU]) {
1138 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
1139 NL_SET_ERR_MSG(info->extack,
1140 "MTU property is unsupported");
1141 return -EINVAL;
1142 }
1143 #ifdef CONFIG_TIPC_MEDIA_UDP
1144 if (nla_get_u32(props[TIPC_NLA_PROP_MTU]) <
1145 b->encap_hlen + TIPC_MIN_BEARER_MTU) {
1146 NL_SET_ERR_MSG(info->extack,
1147 "MTU value is out-of-range");
1148 return -EINVAL;
1149 }
1150 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1151 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1152 #endif
1153 }
1154 }
1155
1156 return 0;
1157 }
1158
tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)1159 int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1160 {
1161 int err;
1162
1163 rtnl_lock();
1164 err = __tipc_nl_bearer_set(skb, info);
1165 rtnl_unlock();
1166
1167 return err;
1168 }
1169
__tipc_nl_add_media(struct tipc_nl_msg *msg, struct tipc_media *media, int nlflags)1170 static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
1171 struct tipc_media *media, int nlflags)
1172 {
1173 void *hdr;
1174 struct nlattr *attrs;
1175 struct nlattr *prop;
1176
1177 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
1178 nlflags, TIPC_NL_MEDIA_GET);
1179 if (!hdr)
1180 return -EMSGSIZE;
1181
1182 attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
1183 if (!attrs)
1184 goto msg_full;
1185
1186 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1187 goto attr_msg_full;
1188
1189 prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
1190 if (!prop)
1191 goto prop_msg_full;
1192 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1193 goto prop_msg_full;
1194 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1195 goto prop_msg_full;
1196 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
1197 goto prop_msg_full;
1198 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1199 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1200 goto prop_msg_full;
1201
1202 nla_nest_end(msg->skb, prop);
1203 nla_nest_end(msg->skb, attrs);
1204 genlmsg_end(msg->skb, hdr);
1205
1206 return 0;
1207
1208 prop_msg_full:
1209 nla_nest_cancel(msg->skb, prop);
1210 attr_msg_full:
1211 nla_nest_cancel(msg->skb, attrs);
1212 msg_full:
1213 genlmsg_cancel(msg->skb, hdr);
1214
1215 return -EMSGSIZE;
1216 }
1217
tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)1218 int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1219 {
1220 int err;
1221 int i = cb->args[0];
1222 struct tipc_nl_msg msg;
1223
1224 if (i == MAX_MEDIA)
1225 return 0;
1226
1227 msg.skb = skb;
1228 msg.portid = NETLINK_CB(cb->skb).portid;
1229 msg.seq = cb->nlh->nlmsg_seq;
1230
1231 rtnl_lock();
1232 for (; media_info_array[i] != NULL; i++) {
1233 err = __tipc_nl_add_media(&msg, media_info_array[i],
1234 NLM_F_MULTI);
1235 if (err)
1236 break;
1237 }
1238 rtnl_unlock();
1239
1240 cb->args[0] = i;
1241 return skb->len;
1242 }
1243
tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)1244 int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1245 {
1246 int err;
1247 char *name;
1248 struct tipc_nl_msg msg;
1249 struct tipc_media *media;
1250 struct sk_buff *rep;
1251 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1252
1253 if (!info->attrs[TIPC_NLA_MEDIA])
1254 return -EINVAL;
1255
1256 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1257 info->attrs[TIPC_NLA_MEDIA],
1258 tipc_nl_media_policy, info->extack);
1259 if (err)
1260 return err;
1261
1262 if (!attrs[TIPC_NLA_MEDIA_NAME])
1263 return -EINVAL;
1264 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1265
1266 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1267 if (!rep)
1268 return -ENOMEM;
1269
1270 msg.skb = rep;
1271 msg.portid = info->snd_portid;
1272 msg.seq = info->snd_seq;
1273
1274 rtnl_lock();
1275 media = tipc_media_find(name);
1276 if (!media) {
1277 NL_SET_ERR_MSG(info->extack, "Media not found");
1278 err = -EINVAL;
1279 goto err_out;
1280 }
1281
1282 err = __tipc_nl_add_media(&msg, media, 0);
1283 if (err)
1284 goto err_out;
1285 rtnl_unlock();
1286
1287 return genlmsg_reply(rep, info);
1288 err_out:
1289 rtnl_unlock();
1290 nlmsg_free(rep);
1291
1292 return err;
1293 }
1294
__tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)1295 int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1296 {
1297 int err;
1298 char *name;
1299 struct tipc_media *m;
1300 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
1301
1302 if (!info->attrs[TIPC_NLA_MEDIA])
1303 return -EINVAL;
1304
1305 err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
1306 info->attrs[TIPC_NLA_MEDIA],
1307 tipc_nl_media_policy, info->extack);
1308
1309 if (!attrs[TIPC_NLA_MEDIA_NAME])
1310 return -EINVAL;
1311 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1312
1313 m = tipc_media_find(name);
1314 if (!m) {
1315 NL_SET_ERR_MSG(info->extack, "Media not found");
1316 return -EINVAL;
1317 }
1318 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1319 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1320
1321 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1322 props);
1323 if (err)
1324 return err;
1325
1326 if (props[TIPC_NLA_PROP_TOL])
1327 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1328 if (props[TIPC_NLA_PROP_PRIO])
1329 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1330 if (props[TIPC_NLA_PROP_WIN])
1331 m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1332 if (props[TIPC_NLA_PROP_MTU]) {
1333 if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
1334 NL_SET_ERR_MSG(info->extack,
1335 "MTU property is unsupported");
1336 return -EINVAL;
1337 }
1338 #ifdef CONFIG_TIPC_MEDIA_UDP
1339 if (tipc_udp_mtu_bad(nla_get_u32
1340 (props[TIPC_NLA_PROP_MTU]))) {
1341 NL_SET_ERR_MSG(info->extack,
1342 "MTU value is out-of-range");
1343 return -EINVAL;
1344 }
1345 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1346 #endif
1347 }
1348 }
1349
1350 return 0;
1351 }
1352
tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)1353 int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1354 {
1355 int err;
1356
1357 rtnl_lock();
1358 err = __tipc_nl_media_set(skb, info);
1359 rtnl_unlock();
1360
1361 return err;
1362 }
1363