blob: e32294f37c29a418987e6f1f13719644fdb2017d [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/bearer.c: TIPC bearer code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04004 * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
Ying Xuee4d050c2013-12-10 20:45:43 -08005 * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue7f9f95d2015-01-09 15:27:06 +080037#include <net/sock.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "bearer.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010040#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
Jon Paul Maloy35c55c92016-06-13 20:46:22 -040042#include "monitor.h"
Ying Xue1da46562015-01-09 15:27:07 +080043#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010044#include "netlink.h"
Richard Alpeef20cd42016-08-26 10:52:53 +020045#include "udp_media.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010046
Patrick McHardya29a1942013-04-17 06:18:28 +000047#define MAX_ADDR_STR 60
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080049static struct tipc_media * const media_info_array[] = {
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080050 &eth_media_info,
51#ifdef CONFIG_TIPC_MEDIA_IB
52 &ib_media_info,
53#endif
Erik Hugned0f91932015-03-05 10:23:49 +010054#ifdef CONFIG_TIPC_MEDIA_UDP
55 &udp_media_info,
56#endif
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080057 NULL
58};
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -040060static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
61{
62 struct tipc_net *tn = tipc_net(net);
63
64 return rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
65}
66
Jon Paul Maloy1a906322015-11-19 14:30:47 -050067static void bearer_disable(struct net *net, struct tipc_bearer *b);
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +020068static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
69 struct packet_type *pt, struct net_device *orig_dev);
Allan Stephens3a777ff2011-04-21 13:58:26 -050070
Per Lidenb97bf3f2006-01-02 19:04:38 +010071/**
Allan Stephens5c216e12011-10-18 11:34:29 -040072 * tipc_media_find - locates specified media object by name
Per Lidenb97bf3f2006-01-02 19:04:38 +010073 */
Paul Gortmaker358a0d12011-12-29 20:19:42 -050074struct tipc_media *tipc_media_find(const char *name)
Per Lidenb97bf3f2006-01-02 19:04:38 +010075{
Per Lidenb97bf3f2006-01-02 19:04:38 +010076 u32 i;
77
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080078 for (i = 0; media_info_array[i] != NULL; i++) {
79 if (!strcmp(media_info_array[i]->name, name))
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080080 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +010081 }
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080082 return media_info_array[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +010083}
84
85/**
Allan Stephensc79be452011-10-06 16:40:55 -040086 * media_find_id - locates specified media object by type identifier
87 */
Paul Gortmaker358a0d12011-12-29 20:19:42 -050088static struct tipc_media *media_find_id(u8 type)
Allan Stephensc79be452011-10-06 16:40:55 -040089{
90 u32 i;
91
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080092 for (i = 0; media_info_array[i] != NULL; i++) {
93 if (media_info_array[i]->type_id == type)
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080094 break;
Allan Stephensc79be452011-10-06 16:40:55 -040095 }
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080096 return media_info_array[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +010097}
98
99/**
Per Liden4323add2006-01-18 00:38:21 +0100100 * tipc_media_addr_printf - record media address in print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 */
Tuong Lienb4b97712018-12-19 09:17:56 +0700102int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103{
Allan Stephensc61b6662011-10-07 11:31:49 -0400104 char addr_str[MAX_ADDR_STR];
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500105 struct tipc_media *m;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400106 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500108 m = media_find_id(a->media_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500110 if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
111 ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
Allan Stephensc61b6662011-10-07 11:31:49 -0400112 else {
Allan Stephensc61b6662011-10-07 11:31:49 -0400113 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114
Richard Alpe941787b2015-02-09 09:50:19 +0100115 ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
Allan Stephens3d749a62011-10-07 15:19:11 -0400116 for (i = 0; i < sizeof(a->value); i++)
Tuong Lienb4b97712018-12-19 09:17:56 +0700117 ret += scnprintf(buf + ret, len - ret,
118 "-%x", a->value[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 }
Tuong Lienb4b97712018-12-19 09:17:56 +0700120 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121}
122
123/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 * bearer_name_validate - validate & (optionally) deconstruct bearer name
Ben Hutchings2c530402012-07-10 10:55:09 +0000125 * @name: ptr to bearer name string
126 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900127 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 * Returns 1 if bearer name is valid, otherwise 0.
129 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900130static int bearer_name_validate(const char *name,
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500131 struct tipc_bearer_names *name_parts)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132{
133 char name_copy[TIPC_MAX_BEARER_NAME];
134 char *media_name;
135 char *if_name;
136 u32 media_len;
137 u32 if_len;
138
139 /* copy bearer name & ensure length is OK */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
141 /* need above in case non-Posix strncpy() doesn't pad with nulls */
142 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
143 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
144 return 0;
145
146 /* ensure all component parts of bearer name are present */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 media_name = name_copy;
Allan Stephens2db99832010-12-31 18:59:33 +0000148 if_name = strchr(media_name, ':');
149 if (if_name == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 return 0;
151 *(if_name++) = 0;
152 media_len = if_name - media_name;
153 if_len = strlen(if_name) + 1;
154
155 /* validate component parts of bearer name */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900156 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
Ying Xuefc073932012-08-16 12:09:08 +0000157 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 return 0;
159
160 /* return bearer name components, if necessary */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 if (name_parts) {
162 strcpy(name_parts->media_name, media_name);
163 strcpy(name_parts->if_name, if_name);
164 }
165 return 1;
166}
167
168/**
Allan Stephens5c216e12011-10-18 11:34:29 -0400169 * tipc_bearer_find - locates bearer object with matching bearer name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 */
Ying Xue7f9f95d2015-01-09 15:27:06 +0800171struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800173 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500174 struct tipc_bearer *b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 u32 i;
176
Ying Xue3874ccb2014-03-27 12:54:33 +0800177 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500178 b = rtnl_dereference(tn->bearer_list[i]);
179 if (b && (!strcmp(b->name, name)))
180 return b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800182 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183}
184
Parthasarathy Bhuvaraganff0d3e72016-07-26 08:47:21 +0200185/* tipc_bearer_get_name - get the bearer name from its id.
186 * @net: network namespace
187 * @name: a pointer to the buffer where the name will be stored.
188 * @bearer_id: the id to get the name from.
189 */
190int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
191{
192 struct tipc_net *tn = tipc_net(net);
193 struct tipc_bearer *b;
194
195 if (bearer_id >= MAX_BEARERS)
196 return -EINVAL;
197
198 b = rtnl_dereference(tn->bearer_list[bearer_id]);
199 if (!b)
200 return -EINVAL;
201
202 strcpy(name, b->name);
203 return 0;
204}
205
Ying Xue7f9f95d2015-01-09 15:27:06 +0800206void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800208 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500209 struct tipc_bearer *b;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800210
211 rcu_read_lock();
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500212 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
213 if (b)
Jon Maloyb39e4652018-03-22 20:42:46 +0100214 tipc_disc_add_dest(b->disc);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800215 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216}
217
Ying Xue7f9f95d2015-01-09 15:27:06 +0800218void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800220 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500221 struct tipc_bearer *b;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800222
223 rcu_read_lock();
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500224 b = rcu_dereference_rtnl(tn->bearer_list[bearer_id]);
225 if (b)
Jon Maloyb39e4652018-03-22 20:42:46 +0100226 tipc_disc_remove_dest(b->disc);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800227 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228}
229
Allan Stephensb274f4a2010-05-11 14:30:16 +0000230/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 * tipc_enable_bearer - enable bearer with the given name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900232 */
Richard Alpe9ab15462015-02-09 09:50:05 +0100233static int tipc_enable_bearer(struct net *net, const char *name,
Jon Maloycb30a6332018-03-22 20:42:45 +0100234 u32 disc_domain, u32 prio,
Erik Hugned0f91932015-03-05 10:23:49 +0100235 struct nlattr *attr[])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Jon Maloycb30a6332018-03-22 20:42:45 +0100237 struct tipc_net *tn = tipc_net(net);
238 struct tipc_bearer_names b_names;
Jon Maloycb30a6332018-03-22 20:42:45 +0100239 int with_this_prio = 1;
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500240 struct tipc_bearer *b;
241 struct tipc_media *m;
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400242 struct sk_buff *skb;
Jon Maloycb30a6332018-03-22 20:42:45 +0100243 int bearer_id = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 int res = -EINVAL;
Jon Maloycb30a6332018-03-22 20:42:45 +0100245 char *errstr = "";
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500247 if (!bearer_name_validate(name, &b_names)) {
Jon Maloycb30a6332018-03-22 20:42:45 +0100248 errstr = "illegal name";
249 goto rejected;
Allan Stephensa10bd922006-06-25 23:52:17 -0700250 }
Jon Maloycb30a6332018-03-22 20:42:45 +0100251
Jon Maloycb30a6332018-03-22 20:42:45 +0100252 if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
253 errstr = "illegal priority";
254 goto rejected;
Allan Stephensa10bd922006-06-25 23:52:17 -0700255 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500257 m = tipc_media_find(b_names.media_name);
258 if (!m) {
Jon Maloycb30a6332018-03-22 20:42:45 +0100259 errstr = "media not registered";
260 goto rejected;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 }
Per Liden16cb4b32006-01-13 22:22:22 +0100262
Jon Maloycb30a6332018-03-22 20:42:45 +0100263 if (prio == TIPC_MEDIA_LINK_PRI)
264 prio = m->priority;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265
Jon Maloycb30a6332018-03-22 20:42:45 +0100266 /* Check new bearer vs existing ones and find free bearer id if any */
267 while (bearer_id < MAX_BEARERS) {
268 b = rtnl_dereference(tn->bearer_list[bearer_id]);
269 if (!b)
270 break;
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500271 if (!strcmp(name, b->name)) {
Jon Maloycb30a6332018-03-22 20:42:45 +0100272 errstr = "already enabled";
273 goto rejected;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 }
Jon Maloycb30a6332018-03-22 20:42:45 +0100275 bearer_id++;
276 if (b->priority != prio)
277 continue;
278 if (++with_this_prio <= 2)
279 continue;
280 pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
281 name, prio);
282 if (prio == TIPC_MIN_LINK_PRI) {
283 errstr = "cannot adjust to lower";
284 goto rejected;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 }
Jon Maloycb30a6332018-03-22 20:42:45 +0100286 pr_warn("Bearer <%s>: trying with adjusted priority\n", name);
287 prio--;
288 bearer_id = 0;
289 with_this_prio = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 }
Jon Maloycb30a6332018-03-22 20:42:45 +0100291
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 if (bearer_id >= MAX_BEARERS) {
Jon Maloycb30a6332018-03-22 20:42:45 +0100293 errstr = "max 3 bearers permitted";
294 goto rejected;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 }
296
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500297 b = kzalloc(sizeof(*b), GFP_ATOMIC);
298 if (!b)
Ying Xue7216cd92014-04-21 10:55:48 +0800299 return -ENOMEM;
300
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500301 strcpy(b->name, name);
302 b->media = m;
303 res = m->enable_media(net, b, attr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 if (res) {
Tommi Rantala19142552017-12-22 09:35:16 +0200305 kfree(b);
Jon Maloycb30a6332018-03-22 20:42:45 +0100306 errstr = "failed to enable media";
307 goto rejected;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 }
309
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500310 b->identity = bearer_id;
311 b->tolerance = m->tolerance;
312 b->window = m->window;
313 b->domain = disc_domain;
314 b->net_plane = bearer_id + 'A';
Jon Maloycb30a6332018-03-22 20:42:45 +0100315 b->priority = prio;
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400316 test_and_set_bit_lock(0, &b->up);
Allan Stephens3a777ff2011-04-21 13:58:26 -0500317
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400318 res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
Allan Stephens3a777ff2011-04-21 13:58:26 -0500319 if (res) {
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500320 bearer_disable(net, b);
Jon Maloycb30a6332018-03-22 20:42:45 +0100321 kfree(b);
322 errstr = "failed to create discoverer";
323 goto rejected;
Allan Stephens3a777ff2011-04-21 13:58:26 -0500324 }
Ying Xue3874ccb2014-03-27 12:54:33 +0800325
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500326 rcu_assign_pointer(tn->bearer_list[bearer_id], b);
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400327 if (skb)
328 tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400329
Tommi Rantala19142552017-12-22 09:35:16 +0200330 if (tipc_mon_create(net, bearer_id)) {
331 bearer_disable(net, b);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400332 return -ENOMEM;
Tommi Rantala19142552017-12-22 09:35:16 +0200333 }
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400334
Jon Maloy20263642018-03-22 20:42:47 +0100335 pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
336
Jon Maloycb30a6332018-03-22 20:42:45 +0100337 return res;
338rejected:
Jon Maloy20263642018-03-22 20:42:47 +0100339 pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 return res;
341}
342
343/**
Erik Hugne512137e2013-12-06 10:08:00 -0500344 * tipc_reset_bearer - Reset all links established over this bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 */
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500346static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347{
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500348 pr_info("Resetting bearer <%s>\n", b->name);
349 tipc_node_delete_links(net, b->identity);
350 tipc_disc_reset(net, b);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700351 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352}
353
354/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400355 * bearer_disable
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900356 *
Ying Xue7216cd92014-04-21 10:55:48 +0800357 * Note: This routine assumes caller holds RTNL lock.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 */
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500359static void bearer_disable(struct net *net, struct tipc_bearer *b)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360{
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400361 struct tipc_net *tn = tipc_net(net);
362 int bearer_id = b->identity;
Ying Xue3874ccb2014-03-27 12:54:33 +0800363
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500364 pr_info("Disabling bearer <%s>\n", b->name);
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400365 clear_bit_unlock(0, &b->up);
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400366 tipc_node_delete_links(net, bearer_id);
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400367 b->media->disable_media(b);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500368 RCU_INIT_POINTER(b->media_ptr, NULL);
Jon Maloyb39e4652018-03-22 20:42:46 +0100369 if (b->disc)
370 tipc_disc_delete(b->disc);
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400371 RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500372 kfree_rcu(b, rcu);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400373 tipc_mon_delete(net, bearer_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374}
375
Erik Hugned0f91932015-03-05 10:23:49 +0100376int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
377 struct nlattr *attr[])
Ying Xuee4d050c2013-12-10 20:45:43 -0800378{
Jon Maloy52dfae52018-03-22 20:42:52 +0100379 char *dev_name = strchr((const char *)b->name, ':') + 1;
380 int hwaddr_len = b->media->hwaddr_len;
381 u8 node_id[NODE_ID_LEN] = {0,};
Ying Xuee4d050c2013-12-10 20:45:43 -0800382 struct net_device *dev;
Ying Xuee4d050c2013-12-10 20:45:43 -0800383
384 /* Find device with specified name */
Jon Maloy52dfae52018-03-22 20:42:52 +0100385 dev = dev_get_by_name(net, dev_name);
Ying Xuee4d050c2013-12-10 20:45:43 -0800386 if (!dev)
387 return -ENODEV;
Michal Kubeček3de81b72016-12-02 09:33:41 +0100388 if (tipc_mtu_bad(dev, 0)) {
389 dev_put(dev);
390 return -EINVAL;
391 }
Ying Xuee4d050c2013-12-10 20:45:43 -0800392
Jon Maloy52dfae52018-03-22 20:42:52 +0100393 /* Autoconfigure own node identity if needed */
394 if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
395 memcpy(node_id, dev->dev_addr, hwaddr_len);
396 tipc_net_init(net, node_id, 0);
397 }
398 if (!tipc_own_id(net)) {
YueHaibing63135ee2018-07-25 18:00:49 +0800399 dev_put(dev);
Jon Maloy52dfae52018-03-22 20:42:52 +0100400 pr_warn("Failed to obtain node identity\n");
401 return -EINVAL;
402 }
403
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400404 /* Associate TIPC bearer with L2 bearer */
Ying Xue2231c5a2014-04-21 10:55:47 +0800405 rcu_assign_pointer(b->media_ptr, dev);
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +0200406 b->pt.dev = dev;
407 b->pt.type = htons(ETH_P_TIPC);
408 b->pt.func = tipc_l2_rcv_msg;
409 dev_add_pack(&b->pt);
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400410 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
Jon Maloy52dfae52018-03-22 20:42:52 +0100411 memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
Ying Xuee4d050c2013-12-10 20:45:43 -0800412 b->bcast_addr.media_id = b->media->type_id;
Jon Paul Maloy99999742017-01-18 13:50:50 -0500413 b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
Ying Xuee4d050c2013-12-10 20:45:43 -0800414 b->mtu = dev->mtu;
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400415 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
Ying Xuee4d050c2013-12-10 20:45:43 -0800416 rcu_assign_pointer(dev->tipc_ptr, b);
417 return 0;
418}
419
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400420/* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
Ying Xuee4d050c2013-12-10 20:45:43 -0800421 *
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400422 * Mark L2 bearer as inactive so that incoming buffers are thrown away
Ying Xuee4d050c2013-12-10 20:45:43 -0800423 */
424void tipc_disable_l2_media(struct tipc_bearer *b)
425{
Ying Xue2231c5a2014-04-21 10:55:47 +0800426 struct net_device *dev;
427
428 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +0200429 dev_remove_pack(&b->pt);
Ying Xuee4d050c2013-12-10 20:45:43 -0800430 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
Ying Xuef1c8d8c2014-04-21 10:55:49 +0800431 synchronize_net();
Ying Xuee4d050c2013-12-10 20:45:43 -0800432 dev_put(dev);
433}
434
435/**
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400436 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400437 * @skb: the packet to be sent
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500438 * @b: the bearer through which the packet is to be sent
Ying Xuee4d050c2013-12-10 20:45:43 -0800439 * @dest: peer destination address
440 */
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400441int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
Ying Xue1da46562015-01-09 15:27:07 +0800442 struct tipc_bearer *b, struct tipc_media_addr *dest)
Ying Xuee4d050c2013-12-10 20:45:43 -0800443{
Ying Xue2231c5a2014-04-21 10:55:47 +0800444 struct net_device *dev;
Ying Xuee4d050c2013-12-10 20:45:43 -0800445 int delta;
Ying Xue2231c5a2014-04-21 10:55:47 +0800446
447 dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr);
448 if (!dev)
449 return 0;
Ying Xuee4d050c2013-12-10 20:45:43 -0800450
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400451 delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
452 if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
453 kfree_skb(skb);
454 return 0;
455 }
Jon Paul Maloy7214bcf2015-10-22 08:51:45 -0400456 skb_reset_network_header(skb);
457 skb->dev = dev;
458 skb->protocol = htons(ETH_P_TIPC);
459 dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
460 dev->dev_addr, skb->len);
461 dev_queue_xmit(skb);
Ying Xuee4d050c2013-12-10 20:45:43 -0800462 return 0;
463}
464
Jon Paul Maloy99999742017-01-18 13:50:50 -0500465bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
466{
467 bool supp = false;
468 struct tipc_bearer *b;
469
470 rcu_read_lock();
471 b = bearer_get(net, bearer_id);
472 if (b)
473 supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
474 rcu_read_unlock();
475 return supp;
476}
477
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400478int tipc_bearer_mtu(struct net *net, u32 bearer_id)
479{
480 int mtu = 0;
481 struct tipc_bearer *b;
482
483 rcu_read_lock();
484 b = rcu_dereference_rtnl(tipc_net(net)->bearer_list[bearer_id]);
485 if (b)
486 mtu = b->mtu;
487 rcu_read_unlock();
488 return mtu;
489}
490
Jon Paul Maloy60852d62015-10-22 08:51:44 -0400491/* tipc_bearer_xmit_skb - sends buffer to destination over bearer
492 */
493void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
494 struct sk_buff *skb,
495 struct tipc_media_addr *dest)
496{
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400497 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy60852d62015-10-22 08:51:44 -0400498 struct tipc_bearer *b;
499
500 rcu_read_lock();
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400501 b = bearer_get(net, bearer_id);
502 if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr))))
Jon Paul Maloy60852d62015-10-22 08:51:44 -0400503 b->media->send_msg(net, skb, b, dest);
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400504 else
505 kfree_skb(skb);
Jon Paul Maloy60852d62015-10-22 08:51:44 -0400506 rcu_read_unlock();
Jon Paul Maloy60852d62015-10-22 08:51:44 -0400507}
508
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400509/* tipc_bearer_xmit() -send buffer to destination over bearer
510 */
511void tipc_bearer_xmit(struct net *net, u32 bearer_id,
512 struct sk_buff_head *xmitq,
513 struct tipc_media_addr *dst)
514{
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400515 struct tipc_bearer *b;
516 struct sk_buff *skb, *tmp;
517
518 if (skb_queue_empty(xmitq))
519 return;
520
521 rcu_read_lock();
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400522 b = bearer_get(net, bearer_id);
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400523 if (unlikely(!b))
524 __skb_queue_purge(xmitq);
525 skb_queue_walk_safe(xmitq, skb, tmp) {
526 __skb_dequeue(xmitq);
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400527 if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb))))
528 b->media->send_msg(net, skb, b, dst);
529 else
Wei Yongjun5128b182016-08-23 23:01:02 +0000530 kfree_skb(skb);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400531 }
532 rcu_read_unlock();
533}
534
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400535/* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
536 */
537void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
538 struct sk_buff_head *xmitq)
539{
540 struct tipc_net *tn = tipc_net(net);
541 int net_id = tn->net_id;
542 struct tipc_bearer *b;
543 struct sk_buff *skb, *tmp;
544 struct tipc_msg *hdr;
545
546 rcu_read_lock();
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400547 b = bearer_get(net, bearer_id);
548 if (unlikely(!b || !test_bit(0, &b->up)))
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400549 __skb_queue_purge(xmitq);
550 skb_queue_walk_safe(xmitq, skb, tmp) {
551 hdr = buf_msg(skb);
552 msg_set_non_seq(hdr, 1);
553 msg_set_mc_netid(hdr, net_id);
554 __skb_dequeue(xmitq);
555 b->media->send_msg(net, skb, b, &b->bcast_addr);
Jon Paul Maloyb06b2812015-10-22 08:51:42 -0400556 }
557 rcu_read_unlock();
558}
559
Ying Xue6e967ad2013-12-10 20:45:42 -0800560/**
561 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
562 * @buf: the received packet
563 * @dev: the net device that the packet was received on
564 * @pt: the packet_type structure which was used to register this handler
565 * @orig_dev: the original receive net device in case the device is a bond
566 *
567 * Accept only packets explicitly sent to this node, or broadcast packets;
568 * ignores packets sent using interface multicast, and traffic sent to other
569 * nodes (which can happen if interface is running in promiscuous mode).
570 */
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400571static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
Ying Xue6e967ad2013-12-10 20:45:42 -0800572 struct packet_type *pt, struct net_device *orig_dev)
573{
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500574 struct tipc_bearer *b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575
Ying Xue6e967ad2013-12-10 20:45:42 -0800576 rcu_read_lock();
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +0200577 b = rcu_dereference_rtnl(dev->tipc_ptr) ?:
578 rcu_dereference_rtnl(orig_dev->tipc_ptr);
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400579 if (likely(b && test_bit(0, &b->up) &&
Jon Paul Maloyfed5f572017-08-14 17:55:56 +0200580 (skb->pkt_type <= PACKET_MULTICAST))) {
David S. Millera8305bf2018-07-29 20:42:53 -0700581 skb_mark_not_on_list(skb);
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +0200582 tipc_rcv(dev_net(b->pt.dev), skb, b);
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400583 rcu_read_unlock();
584 return NET_RX_SUCCESS;
Ying Xue6e967ad2013-12-10 20:45:42 -0800585 }
586 rcu_read_unlock();
Jon Paul Maloy4e801fa2016-04-07 10:09:13 -0400587 kfree_skb(skb);
Ying Xue6e967ad2013-12-10 20:45:42 -0800588 return NET_RX_DROP;
589}
590
591/**
592 * tipc_l2_device_event - handle device events from network device
593 * @nb: the context of the notification
594 * @evt: the type of event
595 * @ptr: the net device that the event was on
596 *
597 * This function is called by the Ethernet driver in case of link
598 * change event.
599 */
600static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
601 void *ptr)
602{
Ying Xue6e967ad2013-12-10 20:45:42 -0800603 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800604 struct net *net = dev_net(dev);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500605 struct tipc_bearer *b;
Ying Xue6e967ad2013-12-10 20:45:42 -0800606
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500607 b = rtnl_dereference(dev->tipc_ptr);
608 if (!b)
Ying Xue6e967ad2013-12-10 20:45:42 -0800609 return NOTIFY_DONE;
Ying Xue6e967ad2013-12-10 20:45:42 -0800610
Ying Xue6e967ad2013-12-10 20:45:42 -0800611 switch (evt) {
612 case NETDEV_CHANGE:
Parthasarathy Bhuvaragan94b6ddc2018-09-25 21:56:57 +0200613 if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
614 test_and_set_bit_lock(0, &b->up);
Ying Xue6e967ad2013-12-10 20:45:42 -0800615 break;
Parthasarathy Bhuvaragan94b6ddc2018-09-25 21:56:57 +0200616 }
617 /* fall through */
Jon Paul Maloy282b3a02015-10-15 14:52:45 -0400618 case NETDEV_GOING_DOWN:
Jon Paul Maloy0d051bf2016-08-16 11:53:50 -0400619 clear_bit_unlock(0, &b->up);
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400620 tipc_reset_bearer(net, b);
621 break;
Parthasarathy Bhuvaragan94b6ddc2018-09-25 21:56:57 +0200622 case NETDEV_UP:
623 test_and_set_bit_lock(0, &b->up);
624 break;
Ying Xue6e967ad2013-12-10 20:45:42 -0800625 case NETDEV_CHANGEMTU:
Michal Kubeček3de81b72016-12-02 09:33:41 +0100626 if (tipc_mtu_bad(dev, 0)) {
627 bearer_disable(net, b);
628 break;
629 }
630 b->mtu = dev->mtu;
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500631 tipc_reset_bearer(net, b);
Erik Hugnea21a5842014-03-28 10:32:08 +0100632 break;
Ying Xue6e967ad2013-12-10 20:45:42 -0800633 case NETDEV_CHANGEADDR:
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500634 b->media->raw2addr(b, &b->addr,
Jon Paul Maloy5b7066c2016-04-07 10:09:14 -0400635 (char *)dev->dev_addr);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500636 tipc_reset_bearer(net, b);
Ying Xue6e967ad2013-12-10 20:45:42 -0800637 break;
638 case NETDEV_UNREGISTER:
639 case NETDEV_CHANGENAME:
Kleber Sacilotto de Souza8e0deed2017-09-06 11:08:06 +0200640 bearer_disable(net, b);
Ying Xue6e967ad2013-12-10 20:45:42 -0800641 break;
642 }
Ying Xue6e967ad2013-12-10 20:45:42 -0800643 return NOTIFY_OK;
644}
645
Ying Xue6e967ad2013-12-10 20:45:42 -0800646static struct notifier_block notifier = {
647 .notifier_call = tipc_l2_device_event,
648 .priority = 0,
649};
650
651int tipc_bearer_setup(void)
652{
Parthasarathy Bhuvaragand55c60e2017-08-28 17:57:02 +0200653 return register_netdevice_notifier(&notifier);
Ying Xue6e967ad2013-12-10 20:45:42 -0800654}
655
656void tipc_bearer_cleanup(void)
657{
658 unregister_netdevice_notifier(&notifier);
Ying Xue6e967ad2013-12-10 20:45:42 -0800659}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660
Ying Xuef2f98002015-01-09 15:27:05 +0800661void tipc_bearer_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800663 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500664 struct tipc_bearer *b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 u32 i;
666
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 for (i = 0; i < MAX_BEARERS; i++) {
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500668 b = rtnl_dereference(tn->bearer_list[i]);
669 if (b) {
670 bearer_disable(net, b);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800671 tn->bearer_list[i] = NULL;
Ying Xue3874ccb2014-03-27 12:54:33 +0800672 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674}
Richard Alpe0655f6a2014-11-20 10:29:07 +0100675
Richard Alpe35b9dd72014-11-20 10:29:08 +0100676/* Caller should hold rtnl_lock to protect the bearer */
Richard Alped8182802014-11-24 11:10:29 +0100677static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +0200678 struct tipc_bearer *bearer, int nlflags)
Richard Alpe35b9dd72014-11-20 10:29:08 +0100679{
680 void *hdr;
681 struct nlattr *attrs;
682 struct nlattr *prop;
683
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100684 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +0200685 nlflags, TIPC_NL_BEARER_GET);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100686 if (!hdr)
687 return -EMSGSIZE;
688
689 attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
690 if (!attrs)
691 goto msg_full;
692
693 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
694 goto attr_msg_full;
695
696 prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
697 if (!prop)
698 goto prop_msg_full;
699 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
700 goto prop_msg_full;
701 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
702 goto prop_msg_full;
703 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
704 goto prop_msg_full;
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +0200705 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
706 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
707 goto prop_msg_full;
Richard Alpe35b9dd72014-11-20 10:29:08 +0100708
709 nla_nest_end(msg->skb, prop);
Richard Alpefdb3acc2016-08-26 10:52:55 +0200710
711#ifdef CONFIG_TIPC_MEDIA_UDP
712 if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
713 if (tipc_udp_nl_add_bearer_data(msg, bearer))
714 goto attr_msg_full;
715 }
716#endif
717
Richard Alpe35b9dd72014-11-20 10:29:08 +0100718 nla_nest_end(msg->skb, attrs);
719 genlmsg_end(msg->skb, hdr);
720
721 return 0;
722
723prop_msg_full:
724 nla_nest_cancel(msg->skb, prop);
725attr_msg_full:
726 nla_nest_cancel(msg->skb, attrs);
727msg_full:
728 genlmsg_cancel(msg->skb, hdr);
729
730 return -EMSGSIZE;
731}
732
733int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
734{
735 int err;
736 int i = cb->args[0];
737 struct tipc_bearer *bearer;
738 struct tipc_nl_msg msg;
Ying Xue7f9f95d2015-01-09 15:27:06 +0800739 struct net *net = sock_net(skb->sk);
740 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100741
742 if (i == MAX_BEARERS)
743 return 0;
744
745 msg.skb = skb;
746 msg.portid = NETLINK_CB(cb->skb).portid;
747 msg.seq = cb->nlh->nlmsg_seq;
748
749 rtnl_lock();
750 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue7f9f95d2015-01-09 15:27:06 +0800751 bearer = rtnl_dereference(tn->bearer_list[i]);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100752 if (!bearer)
753 continue;
754
Nicolas Dichtelf2f67392015-04-28 18:33:50 +0200755 err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100756 if (err)
757 break;
758 }
759 rtnl_unlock();
760
761 cb->args[0] = i;
762 return skb->len;
763}
764
765int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
766{
767 int err;
768 char *name;
769 struct sk_buff *rep;
770 struct tipc_bearer *bearer;
771 struct tipc_nl_msg msg;
772 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
Ying Xue7f9f95d2015-01-09 15:27:06 +0800773 struct net *net = genl_info_net(info);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100774
775 if (!info->attrs[TIPC_NLA_BEARER])
776 return -EINVAL;
777
778 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
779 info->attrs[TIPC_NLA_BEARER],
Johannes Bergfe521452017-04-12 14:34:08 +0200780 tipc_nl_bearer_policy, info->extack);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100781 if (err)
782 return err;
783
784 if (!attrs[TIPC_NLA_BEARER_NAME])
785 return -EINVAL;
786 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
787
788 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
789 if (!rep)
790 return -ENOMEM;
791
792 msg.skb = rep;
793 msg.portid = info->snd_portid;
794 msg.seq = info->snd_seq;
795
796 rtnl_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +0800797 bearer = tipc_bearer_find(net, name);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100798 if (!bearer) {
799 err = -EINVAL;
800 goto err_out;
801 }
802
Nicolas Dichtelf2f67392015-04-28 18:33:50 +0200803 err = __tipc_nl_add_bearer(&msg, bearer, 0);
Richard Alpe35b9dd72014-11-20 10:29:08 +0100804 if (err)
805 goto err_out;
806 rtnl_unlock();
807
808 return genlmsg_reply(rep, info);
809err_out:
810 rtnl_unlock();
811 nlmsg_free(rep);
812
813 return err;
814}
815
Ying Xued59d8b72018-02-14 13:37:59 +0800816int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
Richard Alpe0655f6a2014-11-20 10:29:07 +0100817{
818 int err;
819 char *name;
820 struct tipc_bearer *bearer;
821 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
Richard Alpe9ab15462015-02-09 09:50:05 +0100822 struct net *net = sock_net(skb->sk);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100823
824 if (!info->attrs[TIPC_NLA_BEARER])
825 return -EINVAL;
826
827 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
828 info->attrs[TIPC_NLA_BEARER],
Johannes Bergfe521452017-04-12 14:34:08 +0200829 tipc_nl_bearer_policy, info->extack);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100830 if (err)
831 return err;
832
833 if (!attrs[TIPC_NLA_BEARER_NAME])
834 return -EINVAL;
835
836 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
837
Ying Xue7f9f95d2015-01-09 15:27:06 +0800838 bearer = tipc_bearer_find(net, name);
Ying Xued59d8b72018-02-14 13:37:59 +0800839 if (!bearer)
Richard Alpe0655f6a2014-11-20 10:29:07 +0100840 return -EINVAL;
Richard Alpe0655f6a2014-11-20 10:29:07 +0100841
Jon Paul Maloyb1c29f62015-05-14 10:46:11 -0400842 bearer_disable(net, bearer);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100843
844 return 0;
845}
846
Ying Xued59d8b72018-02-14 13:37:59 +0800847int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
848{
849 int err;
850
851 rtnl_lock();
852 err = __tipc_nl_bearer_disable(skb, info);
853 rtnl_unlock();
854
855 return err;
856}
857
Ying Xue45cf7ed2018-02-14 13:38:00 +0800858int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
Richard Alpe0655f6a2014-11-20 10:29:07 +0100859{
860 int err;
861 char *bearer;
862 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
Richard Alpe9ab15462015-02-09 09:50:05 +0100863 struct net *net = sock_net(skb->sk);
Jon Maloy20263642018-03-22 20:42:47 +0100864 u32 domain = 0;
Richard Alpe0655f6a2014-11-20 10:29:07 +0100865 u32 prio;
866
867 prio = TIPC_MEDIA_LINK_PRI;
Richard Alpe0655f6a2014-11-20 10:29:07 +0100868
869 if (!info->attrs[TIPC_NLA_BEARER])
870 return -EINVAL;
871
872 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
873 info->attrs[TIPC_NLA_BEARER],
Johannes Bergfe521452017-04-12 14:34:08 +0200874 tipc_nl_bearer_policy, info->extack);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100875 if (err)
876 return err;
877
878 if (!attrs[TIPC_NLA_BEARER_NAME])
879 return -EINVAL;
880
881 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
882
883 if (attrs[TIPC_NLA_BEARER_DOMAIN])
884 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
885
886 if (attrs[TIPC_NLA_BEARER_PROP]) {
887 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
888
889 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
890 props);
891 if (err)
892 return err;
893
894 if (props[TIPC_NLA_PROP_PRIO])
895 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
896 }
897
Ying Xue45cf7ed2018-02-14 13:38:00 +0800898 return tipc_enable_bearer(net, bearer, domain, prio, attrs);
899}
900
901int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
902{
903 int err;
904
Richard Alpe0655f6a2014-11-20 10:29:07 +0100905 rtnl_lock();
Ying Xue45cf7ed2018-02-14 13:38:00 +0800906 err = __tipc_nl_bearer_enable(skb, info);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100907 rtnl_unlock();
908
Ying Xue45cf7ed2018-02-14 13:38:00 +0800909 return err;
Richard Alpe0655f6a2014-11-20 10:29:07 +0100910}
Richard Alpe315c00b2014-11-20 10:29:09 +0100911
Richard Alpeef20cd42016-08-26 10:52:53 +0200912int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
913{
914 int err;
915 char *name;
916 struct tipc_bearer *b;
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(attrs, TIPC_NLA_BEARER_MAX,
924 info->attrs[TIPC_NLA_BEARER],
Johannes Bergfe521452017-04-12 14:34:08 +0200925 tipc_nl_bearer_policy, info->extack);
Richard Alpeef20cd42016-08-26 10:52:53 +0200926 if (err)
927 return err;
928
929 if (!attrs[TIPC_NLA_BEARER_NAME])
930 return -EINVAL;
931 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
932
933 rtnl_lock();
934 b = tipc_bearer_find(net, name);
935 if (!b) {
936 rtnl_unlock();
937 return -EINVAL;
938 }
939
940#ifdef CONFIG_TIPC_MEDIA_UDP
941 if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
942 err = tipc_udp_nl_bearer_add(b,
943 attrs[TIPC_NLA_BEARER_UDP_OPTS]);
944 if (err) {
945 rtnl_unlock();
946 return err;
947 }
948 }
949#endif
950 rtnl_unlock();
951
952 return 0;
953}
954
Ying Xue93532bb2018-02-14 13:38:01 +0800955int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
Richard Alpe315c00b2014-11-20 10:29:09 +0100956{
Richard Alpe315c00b2014-11-20 10:29:09 +0100957 struct tipc_bearer *b;
958 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
Richard Alpec3d6fb82015-05-06 13:58:54 +0200959 struct net *net = sock_net(skb->sk);
Jon Maloy37c64cf2018-02-14 13:34:39 +0100960 char *name;
961 int err;
Richard Alpe315c00b2014-11-20 10:29:09 +0100962
963 if (!info->attrs[TIPC_NLA_BEARER])
964 return -EINVAL;
965
966 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
967 info->attrs[TIPC_NLA_BEARER],
Johannes Bergfe521452017-04-12 14:34:08 +0200968 tipc_nl_bearer_policy, info->extack);
Richard Alpe315c00b2014-11-20 10:29:09 +0100969 if (err)
970 return err;
971
972 if (!attrs[TIPC_NLA_BEARER_NAME])
973 return -EINVAL;
974 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
975
Ying Xue7f9f95d2015-01-09 15:27:06 +0800976 b = tipc_bearer_find(net, name);
Ying Xue93532bb2018-02-14 13:38:01 +0800977 if (!b)
Richard Alpe315c00b2014-11-20 10:29:09 +0100978 return -EINVAL;
Richard Alpe315c00b2014-11-20 10:29:09 +0100979
980 if (attrs[TIPC_NLA_BEARER_PROP]) {
981 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
982
983 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
984 props);
Ying Xue93532bb2018-02-14 13:38:01 +0800985 if (err)
Richard Alpe315c00b2014-11-20 10:29:09 +0100986 return err;
Richard Alpe315c00b2014-11-20 10:29:09 +0100987
Jon Maloy37c64cf2018-02-14 13:34:39 +0100988 if (props[TIPC_NLA_PROP_TOL]) {
Richard Alpe315c00b2014-11-20 10:29:09 +0100989 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +0200990 tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
Jon Maloy37c64cf2018-02-14 13:34:39 +0100991 }
Richard Alpe315c00b2014-11-20 10:29:09 +0100992 if (props[TIPC_NLA_PROP_PRIO])
993 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
994 if (props[TIPC_NLA_PROP_WIN])
995 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
GhantaKrishnamurthy MohanKrishna682cd3c2018-04-19 11:06:20 +0200996 if (props[TIPC_NLA_PROP_MTU]) {
997 if (b->media->type_id != TIPC_MEDIA_TYPE_UDP)
998 return -EINVAL;
999#ifdef CONFIG_TIPC_MEDIA_UDP
1000 if (tipc_udp_mtu_bad(nla_get_u32
1001 (props[TIPC_NLA_PROP_MTU])))
1002 return -EINVAL;
1003 b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1004 tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
1005#endif
1006 }
Richard Alpe315c00b2014-11-20 10:29:09 +01001007 }
Richard Alpe315c00b2014-11-20 10:29:09 +01001008
1009 return 0;
1010}
Richard Alpe46f15c62014-11-20 10:29:15 +01001011
Ying Xue93532bb2018-02-14 13:38:01 +08001012int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
1013{
1014 int err;
1015
1016 rtnl_lock();
1017 err = __tipc_nl_bearer_set(skb, info);
1018 rtnl_unlock();
1019
1020 return err;
1021}
1022
Richard Alped8182802014-11-24 11:10:29 +01001023static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001024 struct tipc_media *media, int nlflags)
Richard Alpe46f15c62014-11-20 10:29:15 +01001025{
1026 void *hdr;
1027 struct nlattr *attrs;
1028 struct nlattr *prop;
1029
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001030 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001031 nlflags, TIPC_NL_MEDIA_GET);
Richard Alpe46f15c62014-11-20 10:29:15 +01001032 if (!hdr)
1033 return -EMSGSIZE;
1034
1035 attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
1036 if (!attrs)
1037 goto msg_full;
1038
1039 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
1040 goto attr_msg_full;
1041
1042 prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
1043 if (!prop)
1044 goto prop_msg_full;
1045 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
1046 goto prop_msg_full;
1047 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
1048 goto prop_msg_full;
1049 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
1050 goto prop_msg_full;
GhantaKrishnamurthy MohanKrishna901271e02018-04-19 11:06:19 +02001051 if (media->type_id == TIPC_MEDIA_TYPE_UDP)
1052 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
1053 goto prop_msg_full;
Richard Alpe46f15c62014-11-20 10:29:15 +01001054
1055 nla_nest_end(msg->skb, prop);
1056 nla_nest_end(msg->skb, attrs);
1057 genlmsg_end(msg->skb, hdr);
1058
1059 return 0;
1060
1061prop_msg_full:
1062 nla_nest_cancel(msg->skb, prop);
1063attr_msg_full:
1064 nla_nest_cancel(msg->skb, attrs);
1065msg_full:
1066 genlmsg_cancel(msg->skb, hdr);
1067
1068 return -EMSGSIZE;
1069}
1070
1071int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
1072{
1073 int err;
1074 int i = cb->args[0];
1075 struct tipc_nl_msg msg;
1076
1077 if (i == MAX_MEDIA)
1078 return 0;
1079
1080 msg.skb = skb;
1081 msg.portid = NETLINK_CB(cb->skb).portid;
1082 msg.seq = cb->nlh->nlmsg_seq;
1083
1084 rtnl_lock();
1085 for (; media_info_array[i] != NULL; i++) {
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001086 err = __tipc_nl_add_media(&msg, media_info_array[i],
1087 NLM_F_MULTI);
Richard Alpe46f15c62014-11-20 10:29:15 +01001088 if (err)
1089 break;
1090 }
1091 rtnl_unlock();
1092
1093 cb->args[0] = i;
1094 return skb->len;
1095}
1096
1097int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
1098{
1099 int err;
1100 char *name;
1101 struct tipc_nl_msg msg;
1102 struct tipc_media *media;
1103 struct sk_buff *rep;
1104 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1105
1106 if (!info->attrs[TIPC_NLA_MEDIA])
1107 return -EINVAL;
1108
1109 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
1110 info->attrs[TIPC_NLA_MEDIA],
Johannes Bergfe521452017-04-12 14:34:08 +02001111 tipc_nl_media_policy, info->extack);
Richard Alpe46f15c62014-11-20 10:29:15 +01001112 if (err)
1113 return err;
1114
1115 if (!attrs[TIPC_NLA_MEDIA_NAME])
1116 return -EINVAL;
1117 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1118
1119 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1120 if (!rep)
1121 return -ENOMEM;
1122
1123 msg.skb = rep;
1124 msg.portid = info->snd_portid;
1125 msg.seq = info->snd_seq;
1126
1127 rtnl_lock();
1128 media = tipc_media_find(name);
1129 if (!media) {
1130 err = -EINVAL;
1131 goto err_out;
1132 }
1133
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02001134 err = __tipc_nl_add_media(&msg, media, 0);
Richard Alpe46f15c62014-11-20 10:29:15 +01001135 if (err)
1136 goto err_out;
1137 rtnl_unlock();
1138
1139 return genlmsg_reply(rep, info);
1140err_out:
1141 rtnl_unlock();
1142 nlmsg_free(rep);
1143
1144 return err;
1145}
Richard Alpe1e554172014-11-20 10:29:16 +01001146
Ying Xue07ffb222018-02-14 13:38:02 +08001147int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
Richard Alpe1e554172014-11-20 10:29:16 +01001148{
1149 int err;
1150 char *name;
1151 struct tipc_media *m;
1152 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1153
1154 if (!info->attrs[TIPC_NLA_MEDIA])
1155 return -EINVAL;
1156
1157 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
1158 info->attrs[TIPC_NLA_MEDIA],
Johannes Bergfe521452017-04-12 14:34:08 +02001159 tipc_nl_media_policy, info->extack);
Richard Alpe1e554172014-11-20 10:29:16 +01001160
1161 if (!attrs[TIPC_NLA_MEDIA_NAME])
1162 return -EINVAL;
1163 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1164
Richard Alpe1e554172014-11-20 10:29:16 +01001165 m = tipc_media_find(name);
Ying Xue07ffb222018-02-14 13:38:02 +08001166 if (!m)
Richard Alpe1e554172014-11-20 10:29:16 +01001167 return -EINVAL;
Richard Alpe1e554172014-11-20 10:29:16 +01001168
1169 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1170 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1171
1172 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1173 props);
Ying Xue07ffb222018-02-14 13:38:02 +08001174 if (err)
Richard Alpe1e554172014-11-20 10:29:16 +01001175 return err;
Richard Alpe1e554172014-11-20 10:29:16 +01001176
1177 if (props[TIPC_NLA_PROP_TOL])
1178 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1179 if (props[TIPC_NLA_PROP_PRIO])
1180 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1181 if (props[TIPC_NLA_PROP_WIN])
1182 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
GhantaKrishnamurthy MohanKrishna901271e02018-04-19 11:06:19 +02001183 if (props[TIPC_NLA_PROP_MTU]) {
1184 if (m->type_id != TIPC_MEDIA_TYPE_UDP)
1185 return -EINVAL;
1186#ifdef CONFIG_TIPC_MEDIA_UDP
1187 if (tipc_udp_mtu_bad(nla_get_u32
1188 (props[TIPC_NLA_PROP_MTU])))
1189 return -EINVAL;
1190 m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
1191#endif
1192 }
Richard Alpe1e554172014-11-20 10:29:16 +01001193 }
Richard Alpe1e554172014-11-20 10:29:16 +01001194
1195 return 0;
1196}
Ying Xue07ffb222018-02-14 13:38:02 +08001197
1198int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1199{
1200 int err;
1201
1202 rtnl_lock();
1203 err = __tipc_nl_media_set(skb, info);
1204 rtnl_unlock();
1205
1206 return err;
1207}