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