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