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