blob: a735c08e9d90abe69c7ca41f9b6f4943725a8ac6 [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 *
Richard Alpe0655f6a2014-11-20 10:29:07 +01004 * Copyright (c) 1996-2006, 2013-2014, 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
37#include "core.h"
38#include "config.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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042
Patrick McHardya29a1942013-04-17 06:18:28 +000043#define MAX_ADDR_STR 60
Per Lidenb97bf3f2006-01-02 19:04:38 +010044
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080045static struct tipc_media * const media_info_array[] = {
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080046 &eth_media_info,
47#ifdef CONFIG_TIPC_MEDIA_IB
48 &ib_media_info,
49#endif
50 NULL
51};
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Richard Alpe0655f6a2014-11-20 10:29:07 +010053static const struct nla_policy
54tipc_nl_bearer_policy[TIPC_NLA_BEARER_MAX + 1] = {
55 [TIPC_NLA_BEARER_UNSPEC] = { .type = NLA_UNSPEC },
56 [TIPC_NLA_BEARER_NAME] = {
57 .type = NLA_STRING,
58 .len = TIPC_MAX_BEARER_NAME
59 },
60 [TIPC_NLA_BEARER_PROP] = { .type = NLA_NESTED },
61 [TIPC_NLA_BEARER_DOMAIN] = { .type = NLA_U32 }
62};
63
Richard Alpe46f15c62014-11-20 10:29:15 +010064static const struct nla_policy tipc_nl_media_policy[TIPC_NLA_MEDIA_MAX + 1] = {
65 [TIPC_NLA_MEDIA_UNSPEC] = { .type = NLA_UNSPEC },
66 [TIPC_NLA_MEDIA_NAME] = { .type = NLA_STRING },
67 [TIPC_NLA_MEDIA_PROP] = { .type = NLA_NESTED }
68};
69
Ying Xuef8322df2014-04-21 10:55:45 +080070struct tipc_bearer __rcu *bearer_list[MAX_BEARERS + 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
Ying Xuef2f98002015-01-09 15:27:05 +080072static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr,
73 bool shutting_down);
Allan Stephens3a777ff2011-04-21 13:58:26 -050074
Per Lidenb97bf3f2006-01-02 19:04:38 +010075/**
Allan Stephens5c216e12011-10-18 11:34:29 -040076 * tipc_media_find - locates specified media object by name
Per Lidenb97bf3f2006-01-02 19:04:38 +010077 */
Paul Gortmaker358a0d12011-12-29 20:19:42 -050078struct tipc_media *tipc_media_find(const char *name)
Per Lidenb97bf3f2006-01-02 19:04:38 +010079{
Per Lidenb97bf3f2006-01-02 19:04:38 +010080 u32 i;
81
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080082 for (i = 0; media_info_array[i] != NULL; i++) {
83 if (!strcmp(media_info_array[i]->name, name))
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080084 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 }
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080086 return media_info_array[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +010087}
88
89/**
Allan Stephensc79be452011-10-06 16:40:55 -040090 * media_find_id - locates specified media object by type identifier
91 */
Paul Gortmaker358a0d12011-12-29 20:19:42 -050092static struct tipc_media *media_find_id(u8 type)
Allan Stephensc79be452011-10-06 16:40:55 -040093{
94 u32 i;
95
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -080096 for (i = 0; media_info_array[i] != NULL; i++) {
97 if (media_info_array[i]->type_id == type)
Jon Paul Maloy5702dba2013-12-10 20:45:39 -080098 break;
Allan Stephensc79be452011-10-06 16:40:55 -040099 }
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800100 return media_info_array[i];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101}
102
103/**
Per Liden4323add2006-01-18 00:38:21 +0100104 * tipc_media_addr_printf - record media address in print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400106void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107{
Allan Stephensc61b6662011-10-07 11:31:49 -0400108 char addr_str[MAX_ADDR_STR];
Paul Gortmaker358a0d12011-12-29 20:19:42 -0500109 struct tipc_media *m_ptr;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400110 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111
Allan Stephens3d749a62011-10-07 15:19:11 -0400112 m_ptr = media_find_id(a->media_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113
Allan Stephensc61b6662011-10-07 11:31:49 -0400114 if (m_ptr && !m_ptr->addr2str(a, addr_str, sizeof(addr_str)))
Erik Hugnedc1aed32012-06-29 00:50:23 -0400115 ret = tipc_snprintf(buf, len, "%s(%s)", m_ptr->name, addr_str);
Allan Stephensc61b6662011-10-07 11:31:49 -0400116 else {
Allan Stephensc61b6662011-10-07 11:31:49 -0400117 u32 i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118
Erik Hugnedc1aed32012-06-29 00:50:23 -0400119 ret = tipc_snprintf(buf, len, "UNKNOWN(%u)", a->media_id);
Allan Stephens3d749a62011-10-07 15:19:11 -0400120 for (i = 0; i < sizeof(a->value); i++)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400121 ret += tipc_snprintf(buf - ret, len + ret,
122 "-%02x", a->value[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 }
124}
125
126/**
Per Liden4323add2006-01-18 00:38:21 +0100127 * tipc_media_get_names - record names of registered media in buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 */
Per Liden4323add2006-01-18 00:38:21 +0100129struct sk_buff *tipc_media_get_names(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130{
131 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 int i;
133
Per Liden4323add2006-01-18 00:38:21 +0100134 buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 if (!buf)
136 return NULL;
137
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800138 for (i = 0; media_info_array[i] != NULL; i++) {
Allan Stephensa31abe82011-10-07 09:25:12 -0400139 tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME,
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800140 media_info_array[i]->name,
141 strlen(media_info_array[i]->name) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 return buf;
144}
145
146/**
147 * bearer_name_validate - validate & (optionally) deconstruct bearer name
Ben Hutchings2c530402012-07-10 10:55:09 +0000148 * @name: ptr to bearer name string
149 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900150 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 * Returns 1 if bearer name is valid, otherwise 0.
152 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900153static int bearer_name_validate(const char *name,
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500154 struct tipc_bearer_names *name_parts)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
156 char name_copy[TIPC_MAX_BEARER_NAME];
157 char *media_name;
158 char *if_name;
159 u32 media_len;
160 u32 if_len;
161
162 /* copy bearer name & ensure length is OK */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
164 /* need above in case non-Posix strncpy() doesn't pad with nulls */
165 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
166 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
167 return 0;
168
169 /* ensure all component parts of bearer name are present */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 media_name = name_copy;
Allan Stephens2db99832010-12-31 18:59:33 +0000171 if_name = strchr(media_name, ':');
172 if (if_name == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 return 0;
174 *(if_name++) = 0;
175 media_len = if_name - media_name;
176 if_len = strlen(if_name) + 1;
177
178 /* validate component parts of bearer name */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900179 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
Ying Xuefc073932012-08-16 12:09:08 +0000180 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 return 0;
182
183 /* return bearer name components, if necessary */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 if (name_parts) {
185 strcpy(name_parts->media_name, media_name);
186 strcpy(name_parts->if_name, if_name);
187 }
188 return 1;
189}
190
191/**
Allan Stephens5c216e12011-10-18 11:34:29 -0400192 * tipc_bearer_find - locates bearer object with matching bearer name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 */
Allan Stephens5c216e12011-10-18 11:34:29 -0400194struct tipc_bearer *tipc_bearer_find(const char *name)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195{
Allan Stephens2d627b92011-01-07 13:00:11 -0500196 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 u32 i;
198
Ying Xue3874ccb2014-03-27 12:54:33 +0800199 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xuef8322df2014-04-21 10:55:45 +0800200 b_ptr = rtnl_dereference(bearer_list[i]);
Ying Xuef47de122014-03-27 12:54:34 +0800201 if (b_ptr && (!strcmp(b_ptr->name, name)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 return b_ptr;
203 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800204 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205}
206
207/**
Per Liden4323add2006-01-18 00:38:21 +0100208 * tipc_bearer_get_names - record names of bearers in buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 */
Per Liden4323add2006-01-18 00:38:21 +0100210struct sk_buff *tipc_bearer_get_names(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211{
212 struct sk_buff *buf;
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800213 struct tipc_bearer *b;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 int i, j;
215
Per Liden4323add2006-01-18 00:38:21 +0100216 buf = tipc_cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 if (!buf)
218 return NULL;
219
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800220 for (i = 0; media_info_array[i] != NULL; i++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 for (j = 0; j < MAX_BEARERS; j++) {
Ying Xuef8322df2014-04-21 10:55:45 +0800222 b = rtnl_dereference(bearer_list[j]);
Ying Xue3874ccb2014-03-27 12:54:33 +0800223 if (!b)
224 continue;
Ying Xuef47de122014-03-27 12:54:34 +0800225 if (b->media == media_info_array[i]) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900226 tipc_cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
Jon Paul Maloyef72a7e2013-12-10 20:45:40 -0800227 b->name,
228 strlen(b->name) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 }
230 }
231 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return buf;
233}
234
Ying Xue7a2f7d12014-04-21 10:55:46 +0800235void tipc_bearer_add_dest(u32 bearer_id, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800237 struct tipc_bearer *b_ptr;
238
239 rcu_read_lock();
240 b_ptr = rcu_dereference_rtnl(bearer_list[bearer_id]);
241 if (b_ptr) {
Ying Xue28dd9412014-04-21 10:55:51 +0800242 tipc_bcbearer_sort(&b_ptr->nodes, dest, true);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800243 tipc_disc_add_dest(b_ptr->link_req);
244 }
245 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246}
247
Ying Xue7a2f7d12014-04-21 10:55:46 +0800248void tipc_bearer_remove_dest(u32 bearer_id, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800250 struct tipc_bearer *b_ptr;
251
252 rcu_read_lock();
253 b_ptr = rcu_dereference_rtnl(bearer_list[bearer_id]);
254 if (b_ptr) {
Ying Xue28dd9412014-04-21 10:55:51 +0800255 tipc_bcbearer_sort(&b_ptr->nodes, dest, false);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800256 tipc_disc_remove_dest(b_ptr->link_req);
257 }
258 rcu_read_unlock();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259}
260
Allan Stephensb274f4a2010-05-11 14:30:16 +0000261/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 * tipc_enable_bearer - enable bearer with the given name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900263 */
Ying Xuec93d3ba2015-01-09 15:27:04 +0800264int tipc_enable_bearer(struct net *net, const char *name, u32 disc_domain,
265 u32 priority)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266{
Allan Stephens2d627b92011-01-07 13:00:11 -0500267 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -0500268 struct tipc_media *m_ptr;
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500269 struct tipc_bearer_names b_names;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 char addr_string[16];
271 u32 bearer_id;
272 u32 with_this_prio;
273 u32 i;
274 int res = -EINVAL;
275
Allan Stephensb58343f2011-11-08 13:48:28 -0500276 if (!tipc_own_addr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400277 pr_warn("Bearer <%s> rejected, not supported in standalone mode\n",
278 name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 return -ENOPROTOOPT;
Allan Stephensa10bd922006-06-25 23:52:17 -0700280 }
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500281 if (!bearer_name_validate(name, &b_names)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400282 pr_warn("Bearer <%s> rejected, illegal name\n", name);
Per Liden16cb4b32006-01-13 22:22:22 +0100283 return -EINVAL;
Allan Stephensa10bd922006-06-25 23:52:17 -0700284 }
Allan Stephens66e019a2011-04-20 16:24:07 -0500285 if (tipc_addr_domain_valid(disc_domain) &&
286 (disc_domain != tipc_own_addr)) {
287 if (tipc_in_scope(disc_domain, tipc_own_addr)) {
288 disc_domain = tipc_own_addr & TIPC_CLUSTER_MASK;
289 res = 0; /* accept any node in own cluster */
Allan Stephens336ebf52012-04-17 18:02:01 -0400290 } else if (in_own_cluster_exact(disc_domain))
Allan Stephens66e019a2011-04-20 16:24:07 -0500291 res = 0; /* accept specified node in own cluster */
292 }
293 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400294 pr_warn("Bearer <%s> rejected, illegal discovery domain\n",
295 name);
Allan Stephensa10bd922006-06-25 23:52:17 -0700296 return -EINVAL;
297 }
Allan Stephens9efde4a2011-11-03 13:15:10 -0400298 if ((priority > TIPC_MAX_LINK_PRI) &&
Allan Stephensa10bd922006-06-25 23:52:17 -0700299 (priority != TIPC_MEDIA_LINK_PRI)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400300 pr_warn("Bearer <%s> rejected, illegal priority\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 return -EINVAL;
Allan Stephensa10bd922006-06-25 23:52:17 -0700302 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303
Paul Gortmakerf19765f2011-12-29 21:39:49 -0500304 m_ptr = tipc_media_find(b_names.media_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 if (!m_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400306 pr_warn("Bearer <%s> rejected, media <%s> not registered\n",
307 name, b_names.media_name);
Ying Xue7216cd92014-04-21 10:55:48 +0800308 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 }
Per Liden16cb4b32006-01-13 22:22:22 +0100310
311 if (priority == TIPC_MEDIA_LINK_PRI)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 priority = m_ptr->priority;
313
314restart:
315 bearer_id = MAX_BEARERS;
316 with_this_prio = 1;
317 for (i = MAX_BEARERS; i-- != 0; ) {
Ying Xuef8322df2014-04-21 10:55:45 +0800318 b_ptr = rtnl_dereference(bearer_list[i]);
Ying Xuef47de122014-03-27 12:54:34 +0800319 if (!b_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 bearer_id = i;
321 continue;
322 }
Ying Xue3874ccb2014-03-27 12:54:33 +0800323 if (!strcmp(name, b_ptr->name)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400324 pr_warn("Bearer <%s> rejected, already enabled\n",
325 name);
Ying Xue7216cd92014-04-21 10:55:48 +0800326 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 }
Ying Xue3874ccb2014-03-27 12:54:33 +0800328 if ((b_ptr->priority == priority) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 (++with_this_prio > 2)) {
330 if (priority-- == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400331 pr_warn("Bearer <%s> rejected, duplicate priority\n",
332 name);
Ying Xue7216cd92014-04-21 10:55:48 +0800333 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 }
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400335 pr_warn("Bearer <%s> priority adjustment required %u->%u\n",
336 name, priority + 1, priority);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 goto restart;
338 }
339 }
340 if (bearer_id >= MAX_BEARERS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400341 pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
342 name, MAX_BEARERS);
Ying Xue7216cd92014-04-21 10:55:48 +0800343 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 }
345
Ying Xue3874ccb2014-03-27 12:54:33 +0800346 b_ptr = kzalloc(sizeof(*b_ptr), GFP_ATOMIC);
Ying Xue7216cd92014-04-21 10:55:48 +0800347 if (!b_ptr)
348 return -ENOMEM;
349
Allan Stephens2d627b92011-01-07 13:00:11 -0500350 strcpy(b_ptr->name, name);
Ying Xuee4d050c2013-12-10 20:45:43 -0800351 b_ptr->media = m_ptr;
Ying Xue4babbaa2013-10-18 07:23:17 +0200352 res = m_ptr->enable_media(b_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400354 pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
355 name, -res);
Ying Xue7216cd92014-04-21 10:55:48 +0800356 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 }
358
359 b_ptr->identity = bearer_id;
Allan Stephens5c216e12011-10-18 11:34:29 -0400360 b_ptr->tolerance = m_ptr->tolerance;
361 b_ptr->window = m_ptr->window;
Erik Hugnea21a5842014-03-28 10:32:08 +0100362 b_ptr->domain = disc_domain;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 b_ptr->net_plane = bearer_id + 'A';
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 b_ptr->priority = priority;
Allan Stephens3a777ff2011-04-21 13:58:26 -0500365
Ying Xuec93d3ba2015-01-09 15:27:04 +0800366 res = tipc_disc_create(net, b_ptr, &b_ptr->bcast_addr);
Allan Stephens3a777ff2011-04-21 13:58:26 -0500367 if (res) {
Ying Xuef2f98002015-01-09 15:27:05 +0800368 bearer_disable(net, b_ptr, false);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400369 pr_warn("Bearer <%s> rejected, discovery object creation failed\n",
370 name);
Ying Xue7216cd92014-04-21 10:55:48 +0800371 return -EINVAL;
Allan Stephens3a777ff2011-04-21 13:58:26 -0500372 }
Ying Xue3874ccb2014-03-27 12:54:33 +0800373
Ying Xuef8322df2014-04-21 10:55:45 +0800374 rcu_assign_pointer(bearer_list[bearer_id], b_ptr);
Ying Xue3874ccb2014-03-27 12:54:33 +0800375
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400376 pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
377 name,
378 tipc_addr_string_fill(addr_string, disc_domain), priority);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 return res;
380}
381
382/**
Erik Hugne512137e2013-12-06 10:08:00 -0500383 * tipc_reset_bearer - Reset all links established over this bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 */
Ying Xuec93d3ba2015-01-09 15:27:04 +0800385static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386{
Erik Hugne512137e2013-12-06 10:08:00 -0500387 pr_info("Resetting bearer <%s>\n", b_ptr->name);
Ying Xuef2f98002015-01-09 15:27:05 +0800388 tipc_link_reset_list(net, b_ptr->identity);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800389 tipc_disc_reset(net, b_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700390 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391}
392
393/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400394 * bearer_disable
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395 *
Ying Xue7216cd92014-04-21 10:55:48 +0800396 * Note: This routine assumes caller holds RTNL lock.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 */
Ying Xuef2f98002015-01-09 15:27:05 +0800398static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr,
399 bool shutting_down)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
Ying Xue3874ccb2014-03-27 12:54:33 +0800401 u32 i;
402
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400403 pr_info("Disabling bearer <%s>\n", b_ptr->name);
Ying Xue4babbaa2013-10-18 07:23:17 +0200404 b_ptr->media->disable_media(b_ptr);
Ying Xuea8304522014-02-13 17:29:17 -0500405
Ying Xuef2f98002015-01-09 15:27:05 +0800406 tipc_link_delete_list(net, b_ptr->identity, shutting_down);
Ying Xuea8304522014-02-13 17:29:17 -0500407 if (b_ptr->link_req)
408 tipc_disc_delete(b_ptr->link_req);
Ying Xue3874ccb2014-03-27 12:54:33 +0800409
410 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xuef8322df2014-04-21 10:55:45 +0800411 if (b_ptr == rtnl_dereference(bearer_list[i])) {
412 RCU_INIT_POINTER(bearer_list[i], NULL);
Ying Xue3874ccb2014-03-27 12:54:33 +0800413 break;
414 }
415 }
Ying Xuef8322df2014-04-21 10:55:45 +0800416 kfree_rcu(b_ptr, rcu);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417}
418
Ying Xuef2f98002015-01-09 15:27:05 +0800419int tipc_disable_bearer(struct net *net, const char *name)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420{
Allan Stephens2d627b92011-01-07 13:00:11 -0500421 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 int res;
423
Allan Stephens5c216e12011-10-18 11:34:29 -0400424 b_ptr = tipc_bearer_find(name);
Allan Stephensccc901e2010-10-14 13:58:26 +0000425 if (b_ptr == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400426 pr_warn("Attempt to disable unknown bearer <%s>\n", name);
Allan Stephensccc901e2010-10-14 13:58:26 +0000427 res = -EINVAL;
Allan Stephens28cc9372010-11-30 12:00:56 +0000428 } else {
Ying Xuef2f98002015-01-09 15:27:05 +0800429 bearer_disable(net, b_ptr, false);
Allan Stephens28cc9372010-11-30 12:00:56 +0000430 res = 0;
431 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 return res;
433}
434
Ying Xuee4d050c2013-12-10 20:45:43 -0800435int tipc_enable_l2_media(struct tipc_bearer *b)
436{
437 struct net_device *dev;
438 char *driver_name = strchr((const char *)b->name, ':') + 1;
439
440 /* Find device with specified name */
441 dev = dev_get_by_name(&init_net, driver_name);
442 if (!dev)
443 return -ENODEV;
444
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400445 /* Associate TIPC bearer with L2 bearer */
Ying Xue2231c5a2014-04-21 10:55:47 +0800446 rcu_assign_pointer(b->media_ptr, dev);
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400447 memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
Ying Xuee4d050c2013-12-10 20:45:43 -0800448 memcpy(b->bcast_addr.value, dev->broadcast, b->media->hwaddr_len);
449 b->bcast_addr.media_id = b->media->type_id;
450 b->bcast_addr.broadcast = 1;
451 b->mtu = dev->mtu;
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400452 b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
Ying Xuee4d050c2013-12-10 20:45:43 -0800453 rcu_assign_pointer(dev->tipc_ptr, b);
454 return 0;
455}
456
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400457/* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
Ying Xuee4d050c2013-12-10 20:45:43 -0800458 *
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400459 * Mark L2 bearer as inactive so that incoming buffers are thrown away,
Ying Xuee4d050c2013-12-10 20:45:43 -0800460 * then get worker thread to complete bearer cleanup. (Can't do cleanup
461 * here because cleanup code needs to sleep and caller holds spinlocks.)
462 */
463void tipc_disable_l2_media(struct tipc_bearer *b)
464{
Ying Xue2231c5a2014-04-21 10:55:47 +0800465 struct net_device *dev;
466
467 dev = (struct net_device *)rtnl_dereference(b->media_ptr);
468 RCU_INIT_POINTER(b->media_ptr, NULL);
Ying Xuee4d050c2013-12-10 20:45:43 -0800469 RCU_INIT_POINTER(dev->tipc_ptr, NULL);
Ying Xuef1c8d8c2014-04-21 10:55:49 +0800470 synchronize_net();
Ying Xuee4d050c2013-12-10 20:45:43 -0800471 dev_put(dev);
472}
473
474/**
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400475 * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
Ying Xuee4d050c2013-12-10 20:45:43 -0800476 * @buf: the packet to be sent
stephen hemminger963a18552014-01-12 12:48:00 -0800477 * @b_ptr: the bearer through which the packet is to be sent
Ying Xuee4d050c2013-12-10 20:45:43 -0800478 * @dest: peer destination address
479 */
480int tipc_l2_send_msg(struct sk_buff *buf, struct tipc_bearer *b,
481 struct tipc_media_addr *dest)
482{
483 struct sk_buff *clone;
Ying Xue2231c5a2014-04-21 10:55:47 +0800484 struct net_device *dev;
Ying Xuee4d050c2013-12-10 20:45:43 -0800485 int delta;
Ying Xue2231c5a2014-04-21 10:55:47 +0800486
487 dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr);
488 if (!dev)
489 return 0;
Ying Xuee4d050c2013-12-10 20:45:43 -0800490
491 clone = skb_clone(buf, GFP_ATOMIC);
492 if (!clone)
493 return 0;
494
495 delta = dev->hard_header_len - skb_headroom(buf);
496 if ((delta > 0) &&
497 pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
498 kfree_skb(clone);
499 return 0;
500 }
501
502 skb_reset_network_header(clone);
503 clone->dev = dev;
504 clone->protocol = htons(ETH_P_TIPC);
505 dev_hard_header(clone, dev, ETH_P_TIPC, dest->value,
506 dev->dev_addr, clone->len);
507 dev_queue_xmit(clone);
508 return 0;
509}
510
511/* tipc_bearer_send- sends buffer to destination over bearer
512 *
513 * IMPORTANT:
514 * The media send routine must not alter the buffer being passed in
515 * as it may be needed for later retransmission!
516 */
Ying Xue7a2f7d12014-04-21 10:55:46 +0800517void tipc_bearer_send(u32 bearer_id, struct sk_buff *buf,
Ying Xuee4d050c2013-12-10 20:45:43 -0800518 struct tipc_media_addr *dest)
519{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800520 struct tipc_bearer *b_ptr;
521
522 rcu_read_lock();
523 b_ptr = rcu_dereference_rtnl(bearer_list[bearer_id]);
524 if (likely(b_ptr))
525 b_ptr->media->send_msg(buf, b_ptr, dest);
526 rcu_read_unlock();
Ying Xuee4d050c2013-12-10 20:45:43 -0800527}
528
Ying Xue6e967ad2013-12-10 20:45:42 -0800529/**
530 * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
531 * @buf: the received packet
532 * @dev: the net device that the packet was received on
533 * @pt: the packet_type structure which was used to register this handler
534 * @orig_dev: the original receive net device in case the device is a bond
535 *
536 * Accept only packets explicitly sent to this node, or broadcast packets;
537 * ignores packets sent using interface multicast, and traffic sent to other
538 * nodes (which can happen if interface is running in promiscuous mode).
539 */
540static int tipc_l2_rcv_msg(struct sk_buff *buf, struct net_device *dev,
541 struct packet_type *pt, struct net_device *orig_dev)
542{
543 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544
Ying Xue6e967ad2013-12-10 20:45:42 -0800545 rcu_read_lock();
Ying Xueca07fb02014-04-21 10:55:43 +0800546 b_ptr = rcu_dereference_rtnl(dev->tipc_ptr);
Ying Xue6e967ad2013-12-10 20:45:42 -0800547 if (likely(b_ptr)) {
548 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
549 buf->next = NULL;
Ying Xuec93d3ba2015-01-09 15:27:04 +0800550 tipc_rcv(dev_net(dev), buf, b_ptr);
Ying Xue6e967ad2013-12-10 20:45:42 -0800551 rcu_read_unlock();
552 return NET_RX_SUCCESS;
553 }
554 }
555 rcu_read_unlock();
556
557 kfree_skb(buf);
558 return NET_RX_DROP;
559}
560
561/**
562 * tipc_l2_device_event - handle device events from network device
563 * @nb: the context of the notification
564 * @evt: the type of event
565 * @ptr: the net device that the event was on
566 *
567 * This function is called by the Ethernet driver in case of link
568 * change event.
569 */
570static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
571 void *ptr)
572{
Ying Xue6e967ad2013-12-10 20:45:42 -0800573 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800574 struct net *net = dev_net(dev);
575 struct tipc_bearer *b_ptr;
Ying Xue6e967ad2013-12-10 20:45:42 -0800576
Ying Xueca07fb02014-04-21 10:55:43 +0800577 b_ptr = rtnl_dereference(dev->tipc_ptr);
578 if (!b_ptr)
Ying Xue6e967ad2013-12-10 20:45:42 -0800579 return NOTIFY_DONE;
Ying Xue6e967ad2013-12-10 20:45:42 -0800580
581 b_ptr->mtu = dev->mtu;
582
583 switch (evt) {
584 case NETDEV_CHANGE:
585 if (netif_carrier_ok(dev))
586 break;
587 case NETDEV_DOWN:
588 case NETDEV_CHANGEMTU:
Ying Xuec93d3ba2015-01-09 15:27:04 +0800589 tipc_reset_bearer(net, b_ptr);
Erik Hugnea21a5842014-03-28 10:32:08 +0100590 break;
Ying Xue6e967ad2013-12-10 20:45:42 -0800591 case NETDEV_CHANGEADDR:
Jon Paul Maloy38504c22014-05-14 05:39:13 -0400592 b_ptr->media->raw2addr(b_ptr, &b_ptr->addr,
Erik Hugnea21a5842014-03-28 10:32:08 +0100593 (char *)dev->dev_addr);
Ying Xuec93d3ba2015-01-09 15:27:04 +0800594 tipc_reset_bearer(net, b_ptr);
Ying Xue6e967ad2013-12-10 20:45:42 -0800595 break;
596 case NETDEV_UNREGISTER:
597 case NETDEV_CHANGENAME:
Ying Xuef2f98002015-01-09 15:27:05 +0800598 bearer_disable(dev_net(dev), b_ptr, false);
Ying Xue6e967ad2013-12-10 20:45:42 -0800599 break;
600 }
Ying Xue6e967ad2013-12-10 20:45:42 -0800601 return NOTIFY_OK;
602}
603
604static struct packet_type tipc_packet_type __read_mostly = {
Joe Perches184593c2014-03-12 10:04:20 -0700605 .type = htons(ETH_P_TIPC),
Ying Xue6e967ad2013-12-10 20:45:42 -0800606 .func = tipc_l2_rcv_msg,
607};
608
609static struct notifier_block notifier = {
610 .notifier_call = tipc_l2_device_event,
611 .priority = 0,
612};
613
614int tipc_bearer_setup(void)
615{
Ying Xue970122f2014-02-20 11:32:50 +0800616 int err;
617
618 err = register_netdevice_notifier(&notifier);
619 if (err)
620 return err;
Ying Xue6e967ad2013-12-10 20:45:42 -0800621 dev_add_pack(&tipc_packet_type);
Ying Xue970122f2014-02-20 11:32:50 +0800622 return 0;
Ying Xue6e967ad2013-12-10 20:45:42 -0800623}
624
625void tipc_bearer_cleanup(void)
626{
627 unregister_netdevice_notifier(&notifier);
628 dev_remove_pack(&tipc_packet_type);
629}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630
Ying Xuef2f98002015-01-09 15:27:05 +0800631void tipc_bearer_stop(struct net *net)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632{
Ying Xue3874ccb2014-03-27 12:54:33 +0800633 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 u32 i;
635
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xuef8322df2014-04-21 10:55:45 +0800637 b_ptr = rtnl_dereference(bearer_list[i]);
Ying Xuef47de122014-03-27 12:54:34 +0800638 if (b_ptr) {
Ying Xuef2f98002015-01-09 15:27:05 +0800639 bearer_disable(net, b_ptr, true);
Ying Xue3874ccb2014-03-27 12:54:33 +0800640 bearer_list[i] = NULL;
641 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643}
Richard Alpe0655f6a2014-11-20 10:29:07 +0100644
Richard Alpe35b9dd72014-11-20 10:29:08 +0100645/* Caller should hold rtnl_lock to protect the bearer */
Richard Alped8182802014-11-24 11:10:29 +0100646static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
647 struct tipc_bearer *bearer)
Richard Alpe35b9dd72014-11-20 10:29:08 +0100648{
649 void *hdr;
650 struct nlattr *attrs;
651 struct nlattr *prop;
652
653 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
654 NLM_F_MULTI, TIPC_NL_BEARER_GET);
655 if (!hdr)
656 return -EMSGSIZE;
657
658 attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
659 if (!attrs)
660 goto msg_full;
661
662 if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
663 goto attr_msg_full;
664
665 prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
666 if (!prop)
667 goto prop_msg_full;
668 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
669 goto prop_msg_full;
670 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
671 goto prop_msg_full;
672 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
673 goto prop_msg_full;
674
675 nla_nest_end(msg->skb, prop);
676 nla_nest_end(msg->skb, attrs);
677 genlmsg_end(msg->skb, hdr);
678
679 return 0;
680
681prop_msg_full:
682 nla_nest_cancel(msg->skb, prop);
683attr_msg_full:
684 nla_nest_cancel(msg->skb, attrs);
685msg_full:
686 genlmsg_cancel(msg->skb, hdr);
687
688 return -EMSGSIZE;
689}
690
691int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
692{
693 int err;
694 int i = cb->args[0];
695 struct tipc_bearer *bearer;
696 struct tipc_nl_msg msg;
697
698 if (i == MAX_BEARERS)
699 return 0;
700
701 msg.skb = skb;
702 msg.portid = NETLINK_CB(cb->skb).portid;
703 msg.seq = cb->nlh->nlmsg_seq;
704
705 rtnl_lock();
706 for (i = 0; i < MAX_BEARERS; i++) {
707 bearer = rtnl_dereference(bearer_list[i]);
708 if (!bearer)
709 continue;
710
711 err = __tipc_nl_add_bearer(&msg, bearer);
712 if (err)
713 break;
714 }
715 rtnl_unlock();
716
717 cb->args[0] = i;
718 return skb->len;
719}
720
721int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
722{
723 int err;
724 char *name;
725 struct sk_buff *rep;
726 struct tipc_bearer *bearer;
727 struct tipc_nl_msg msg;
728 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
729
730 if (!info->attrs[TIPC_NLA_BEARER])
731 return -EINVAL;
732
733 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
734 info->attrs[TIPC_NLA_BEARER],
735 tipc_nl_bearer_policy);
736 if (err)
737 return err;
738
739 if (!attrs[TIPC_NLA_BEARER_NAME])
740 return -EINVAL;
741 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
742
743 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
744 if (!rep)
745 return -ENOMEM;
746
747 msg.skb = rep;
748 msg.portid = info->snd_portid;
749 msg.seq = info->snd_seq;
750
751 rtnl_lock();
752 bearer = tipc_bearer_find(name);
753 if (!bearer) {
754 err = -EINVAL;
755 goto err_out;
756 }
757
758 err = __tipc_nl_add_bearer(&msg, bearer);
759 if (err)
760 goto err_out;
761 rtnl_unlock();
762
763 return genlmsg_reply(rep, info);
764err_out:
765 rtnl_unlock();
766 nlmsg_free(rep);
767
768 return err;
769}
770
Richard Alpe0655f6a2014-11-20 10:29:07 +0100771int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
772{
773 int err;
774 char *name;
775 struct tipc_bearer *bearer;
776 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
Ying Xuef2f98002015-01-09 15:27:05 +0800777 struct net *net = genl_info_net(info);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100778
779 if (!info->attrs[TIPC_NLA_BEARER])
780 return -EINVAL;
781
782 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
783 info->attrs[TIPC_NLA_BEARER],
784 tipc_nl_bearer_policy);
785 if (err)
786 return err;
787
788 if (!attrs[TIPC_NLA_BEARER_NAME])
789 return -EINVAL;
790
791 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
792
793 rtnl_lock();
794 bearer = tipc_bearer_find(name);
795 if (!bearer) {
796 rtnl_unlock();
797 return -EINVAL;
798 }
799
Ying Xuef2f98002015-01-09 15:27:05 +0800800 bearer_disable(net, bearer, false);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100801 rtnl_unlock();
802
803 return 0;
804}
805
806int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
807{
Ying Xuec93d3ba2015-01-09 15:27:04 +0800808 struct net *net = genl_info_net(info);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100809 int err;
810 char *bearer;
811 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
812 u32 domain;
813 u32 prio;
814
815 prio = TIPC_MEDIA_LINK_PRI;
816 domain = tipc_own_addr & TIPC_CLUSTER_MASK;
817
818 if (!info->attrs[TIPC_NLA_BEARER])
819 return -EINVAL;
820
821 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
822 info->attrs[TIPC_NLA_BEARER],
823 tipc_nl_bearer_policy);
824 if (err)
825 return err;
826
827 if (!attrs[TIPC_NLA_BEARER_NAME])
828 return -EINVAL;
829
830 bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
831
832 if (attrs[TIPC_NLA_BEARER_DOMAIN])
833 domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
834
835 if (attrs[TIPC_NLA_BEARER_PROP]) {
836 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
837
838 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
839 props);
840 if (err)
841 return err;
842
843 if (props[TIPC_NLA_PROP_PRIO])
844 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
845 }
846
847 rtnl_lock();
Ying Xuec93d3ba2015-01-09 15:27:04 +0800848 err = tipc_enable_bearer(net, bearer, domain, prio);
Richard Alpe0655f6a2014-11-20 10:29:07 +0100849 if (err) {
850 rtnl_unlock();
851 return err;
852 }
853 rtnl_unlock();
854
855 return 0;
856}
Richard Alpe315c00b2014-11-20 10:29:09 +0100857
858int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
859{
860 int err;
861 char *name;
862 struct tipc_bearer *b;
863 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
864
865 if (!info->attrs[TIPC_NLA_BEARER])
866 return -EINVAL;
867
868 err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
869 info->attrs[TIPC_NLA_BEARER],
870 tipc_nl_bearer_policy);
871 if (err)
872 return err;
873
874 if (!attrs[TIPC_NLA_BEARER_NAME])
875 return -EINVAL;
876 name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
877
878 rtnl_lock();
879 b = tipc_bearer_find(name);
880 if (!b) {
881 rtnl_unlock();
882 return -EINVAL;
883 }
884
885 if (attrs[TIPC_NLA_BEARER_PROP]) {
886 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
887
888 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
889 props);
890 if (err) {
891 rtnl_unlock();
892 return err;
893 }
894
895 if (props[TIPC_NLA_PROP_TOL])
896 b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
897 if (props[TIPC_NLA_PROP_PRIO])
898 b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
899 if (props[TIPC_NLA_PROP_WIN])
900 b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
901 }
902 rtnl_unlock();
903
904 return 0;
905}
Richard Alpe46f15c62014-11-20 10:29:15 +0100906
Richard Alped8182802014-11-24 11:10:29 +0100907static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
908 struct tipc_media *media)
Richard Alpe46f15c62014-11-20 10:29:15 +0100909{
910 void *hdr;
911 struct nlattr *attrs;
912 struct nlattr *prop;
913
914 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
915 NLM_F_MULTI, TIPC_NL_MEDIA_GET);
916 if (!hdr)
917 return -EMSGSIZE;
918
919 attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
920 if (!attrs)
921 goto msg_full;
922
923 if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
924 goto attr_msg_full;
925
926 prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
927 if (!prop)
928 goto prop_msg_full;
929 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
930 goto prop_msg_full;
931 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
932 goto prop_msg_full;
933 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
934 goto prop_msg_full;
935
936 nla_nest_end(msg->skb, prop);
937 nla_nest_end(msg->skb, attrs);
938 genlmsg_end(msg->skb, hdr);
939
940 return 0;
941
942prop_msg_full:
943 nla_nest_cancel(msg->skb, prop);
944attr_msg_full:
945 nla_nest_cancel(msg->skb, attrs);
946msg_full:
947 genlmsg_cancel(msg->skb, hdr);
948
949 return -EMSGSIZE;
950}
951
952int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
953{
954 int err;
955 int i = cb->args[0];
956 struct tipc_nl_msg msg;
957
958 if (i == MAX_MEDIA)
959 return 0;
960
961 msg.skb = skb;
962 msg.portid = NETLINK_CB(cb->skb).portid;
963 msg.seq = cb->nlh->nlmsg_seq;
964
965 rtnl_lock();
966 for (; media_info_array[i] != NULL; i++) {
967 err = __tipc_nl_add_media(&msg, media_info_array[i]);
968 if (err)
969 break;
970 }
971 rtnl_unlock();
972
973 cb->args[0] = i;
974 return skb->len;
975}
976
977int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
978{
979 int err;
980 char *name;
981 struct tipc_nl_msg msg;
982 struct tipc_media *media;
983 struct sk_buff *rep;
984 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
985
986 if (!info->attrs[TIPC_NLA_MEDIA])
987 return -EINVAL;
988
989 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
990 info->attrs[TIPC_NLA_MEDIA],
991 tipc_nl_media_policy);
992 if (err)
993 return err;
994
995 if (!attrs[TIPC_NLA_MEDIA_NAME])
996 return -EINVAL;
997 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
998
999 rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1000 if (!rep)
1001 return -ENOMEM;
1002
1003 msg.skb = rep;
1004 msg.portid = info->snd_portid;
1005 msg.seq = info->snd_seq;
1006
1007 rtnl_lock();
1008 media = tipc_media_find(name);
1009 if (!media) {
1010 err = -EINVAL;
1011 goto err_out;
1012 }
1013
1014 err = __tipc_nl_add_media(&msg, media);
1015 if (err)
1016 goto err_out;
1017 rtnl_unlock();
1018
1019 return genlmsg_reply(rep, info);
1020err_out:
1021 rtnl_unlock();
1022 nlmsg_free(rep);
1023
1024 return err;
1025}
Richard Alpe1e554172014-11-20 10:29:16 +01001026
1027int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
1028{
1029 int err;
1030 char *name;
1031 struct tipc_media *m;
1032 struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
1033
1034 if (!info->attrs[TIPC_NLA_MEDIA])
1035 return -EINVAL;
1036
1037 err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
1038 info->attrs[TIPC_NLA_MEDIA],
1039 tipc_nl_media_policy);
1040
1041 if (!attrs[TIPC_NLA_MEDIA_NAME])
1042 return -EINVAL;
1043 name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
1044
1045 rtnl_lock();
1046 m = tipc_media_find(name);
1047 if (!m) {
1048 rtnl_unlock();
1049 return -EINVAL;
1050 }
1051
1052 if (attrs[TIPC_NLA_MEDIA_PROP]) {
1053 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1054
1055 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
1056 props);
1057 if (err) {
1058 rtnl_unlock();
1059 return err;
1060 }
1061
1062 if (props[TIPC_NLA_PROP_TOL])
1063 m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1064 if (props[TIPC_NLA_PROP_PRIO])
1065 m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1066 if (props[TIPC_NLA_PROP_WIN])
1067 m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1068 }
1069 rtnl_unlock();
1070
1071 return 0;
1072}