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