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