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