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