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