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