Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/bearer.c: TIPC bearer code | |
c4307285 | 3 | * |
0655f6a8 | 4 | * Copyright (c) 1996-2006, 2013-2014, Ericsson AB |
e4d050cb | 5 | * Copyright (c) 2004-2006, 2010-2013, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the names of the copyright holders nor the names of its | |
17 | * contributors may be used to endorse or promote products derived from | |
18 | * this software without specific prior written permission. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * Alternatively, this software may be distributed under the terms of the |
21 | * GNU General Public License ("GPL") version 2 as published by the Free | |
22 | * Software Foundation. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
7f9f95d9 | 37 | #include <net/sock.h> |
b97bf3fd PL |
38 | #include "core.h" |
39 | #include "config.h" | |
b97bf3fd | 40 | #include "bearer.h" |
0655f6a8 | 41 | #include "link.h" |
b97bf3fd | 42 | #include "discover.h" |
1da46568 | 43 | #include "bcast.h" |
b97bf3fd | 44 | |
a29a194a | 45 | #define MAX_ADDR_STR 60 |
b97bf3fd | 46 | |
ef72a7e0 | 47 | static struct tipc_media * const media_info_array[] = { |
5702dbab JPM |
48 | ð_media_info, |
49 | #ifdef CONFIG_TIPC_MEDIA_IB | |
50 | &ib_media_info, | |
51 | #endif | |
52 | NULL | |
53 | }; | |
b97bf3fd | 54 | |
0655f6a8 RA |
55 | static const struct nla_policy |
56 | tipc_nl_bearer_policy[TIPC_NLA_BEARER_MAX + 1] = { | |
57 | [TIPC_NLA_BEARER_UNSPEC] = { .type = NLA_UNSPEC }, | |
58 | [TIPC_NLA_BEARER_NAME] = { | |
59 | .type = NLA_STRING, | |
60 | .len = TIPC_MAX_BEARER_NAME | |
61 | }, | |
62 | [TIPC_NLA_BEARER_PROP] = { .type = NLA_NESTED }, | |
63 | [TIPC_NLA_BEARER_DOMAIN] = { .type = NLA_U32 } | |
64 | }; | |
65 | ||
46f15c67 RA |
66 | static const struct nla_policy tipc_nl_media_policy[TIPC_NLA_MEDIA_MAX + 1] = { |
67 | [TIPC_NLA_MEDIA_UNSPEC] = { .type = NLA_UNSPEC }, | |
68 | [TIPC_NLA_MEDIA_NAME] = { .type = NLA_STRING }, | |
69 | [TIPC_NLA_MEDIA_PROP] = { .type = NLA_NESTED } | |
70 | }; | |
71 | ||
f2f9800d YX |
72 | static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr, |
73 | bool shutting_down); | |
3a777ff8 | 74 | |
b97bf3fd | 75 | /** |
5c216e1d | 76 | * tipc_media_find - locates specified media object by name |
b97bf3fd | 77 | */ |
358a0d1c | 78 | struct tipc_media *tipc_media_find(const char *name) |
b97bf3fd | 79 | { |
b97bf3fd PL |
80 | u32 i; |
81 | ||
ef72a7e0 JPM |
82 | for (i = 0; media_info_array[i] != NULL; i++) { |
83 | if (!strcmp(media_info_array[i]->name, name)) | |
5702dbab | 84 | break; |
b97bf3fd | 85 | } |
ef72a7e0 | 86 | return media_info_array[i]; |
b97bf3fd PL |
87 | } |
88 | ||
c79be454 AS |
89 | /** |
90 | * media_find_id - locates specified media object by type identifier | |
91 | */ | |
358a0d1c | 92 | static struct tipc_media *media_find_id(u8 type) |
c79be454 AS |
93 | { |
94 | u32 i; | |
95 | ||
ef72a7e0 JPM |
96 | for (i = 0; media_info_array[i] != NULL; i++) { |
97 | if (media_info_array[i]->type_id == type) | |
5702dbab | 98 | break; |
c79be454 | 99 | } |
ef72a7e0 | 100 | return media_info_array[i]; |
b97bf3fd PL |
101 | } |
102 | ||
103 | /** | |
4323add6 | 104 | * tipc_media_addr_printf - record media address in print buffer |
b97bf3fd | 105 | */ |
dc1aed37 | 106 | void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a) |
b97bf3fd | 107 | { |
c61b666e | 108 | char addr_str[MAX_ADDR_STR]; |
358a0d1c | 109 | struct tipc_media *m_ptr; |
dc1aed37 | 110 | int ret; |
b97bf3fd | 111 | |
3d749a6a | 112 | m_ptr = media_find_id(a->media_id); |
b97bf3fd | 113 | |
c61b666e | 114 | if (m_ptr && !m_ptr->addr2str(a, addr_str, sizeof(addr_str))) |
dc1aed37 | 115 | ret = tipc_snprintf(buf, len, "%s(%s)", m_ptr->name, addr_str); |
c61b666e | 116 | else { |
c61b666e | 117 | u32 i; |
b97bf3fd | 118 | |
dc1aed37 | 119 | ret = tipc_snprintf(buf, len, "UNKNOWN(%u)", a->media_id); |
3d749a6a | 120 | for (i = 0; i < sizeof(a->value); i++) |
dc1aed37 EH |
121 | ret += tipc_snprintf(buf - ret, len + ret, |
122 | "-%02x", a->value[i]); | |
b97bf3fd PL |
123 | } |
124 | } | |
125 | ||
126 | /** | |
4323add6 | 127 | * tipc_media_get_names - record names of registered media in buffer |
b97bf3fd | 128 | */ |
4323add6 | 129 | struct sk_buff *tipc_media_get_names(void) |
b97bf3fd PL |
130 | { |
131 | struct sk_buff *buf; | |
b97bf3fd PL |
132 | int i; |
133 | ||
4323add6 | 134 | buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME)); |
b97bf3fd PL |
135 | if (!buf) |
136 | return NULL; | |
137 | ||
ef72a7e0 | 138 | for (i = 0; media_info_array[i] != NULL; i++) { |
a31abe8d | 139 | tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, |
ef72a7e0 JPM |
140 | media_info_array[i]->name, |
141 | strlen(media_info_array[i]->name) + 1); | |
b97bf3fd | 142 | } |
b97bf3fd PL |
143 | return buf; |
144 | } | |
145 | ||
146 | /** | |
147 | * bearer_name_validate - validate & (optionally) deconstruct bearer name | |
2c53040f BH |
148 | * @name: ptr to bearer name string |
149 | * @name_parts: ptr to area for bearer name components (or NULL if not needed) | |
c4307285 | 150 | * |
b97bf3fd PL |
151 | * Returns 1 if bearer name is valid, otherwise 0. |
152 | */ | |
c4307285 | 153 | static int bearer_name_validate(const char *name, |
f19765f4 | 154 | struct tipc_bearer_names *name_parts) |
b97bf3fd PL |
155 | { |
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 */ | |
b97bf3fd PL |
163 | 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 */ | |
b97bf3fd | 170 | media_name = name_copy; |
2db9983a AS |
171 | if_name = strchr(media_name, ':'); |
172 | if (if_name == NULL) | |
b97bf3fd PL |
173 | 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 */ | |
c4307285 | 179 | if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) || |
fc073938 | 180 | (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME)) |
b97bf3fd PL |
181 | return 0; |
182 | ||
183 | /* return bearer name components, if necessary */ | |
b97bf3fd PL |
184 | 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 | /** | |
5c216e1d | 192 | * tipc_bearer_find - locates bearer object with matching bearer name |
b97bf3fd | 193 | */ |
7f9f95d9 | 194 | struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name) |
b97bf3fd | 195 | { |
7f9f95d9 | 196 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
2d627b92 | 197 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
198 | u32 i; |
199 | ||
3874ccbb | 200 | for (i = 0; i < MAX_BEARERS; i++) { |
7f9f95d9 | 201 | b_ptr = rtnl_dereference(tn->bearer_list[i]); |
f47de12b | 202 | if (b_ptr && (!strcmp(b_ptr->name, name))) |
b97bf3fd PL |
203 | return b_ptr; |
204 | } | |
1fc54d8f | 205 | return NULL; |
b97bf3fd PL |
206 | } |
207 | ||
7f9f95d9 | 208 | void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest) |
b97bf3fd | 209 | { |
7f9f95d9 | 210 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
7a2f7d18 YX |
211 | struct tipc_bearer *b_ptr; |
212 | ||
213 | rcu_read_lock(); | |
7f9f95d9 | 214 | b_ptr = rcu_dereference_rtnl(tn->bearer_list[bearer_id]); |
7a2f7d18 | 215 | if (b_ptr) { |
7f9f95d9 | 216 | tipc_bcbearer_sort(net, &b_ptr->nodes, dest, true); |
7a2f7d18 YX |
217 | tipc_disc_add_dest(b_ptr->link_req); |
218 | } | |
219 | rcu_read_unlock(); | |
b97bf3fd PL |
220 | } |
221 | ||
7f9f95d9 | 222 | void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest) |
b97bf3fd | 223 | { |
7f9f95d9 | 224 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
7a2f7d18 YX |
225 | struct tipc_bearer *b_ptr; |
226 | ||
227 | rcu_read_lock(); | |
7f9f95d9 | 228 | b_ptr = rcu_dereference_rtnl(tn->bearer_list[bearer_id]); |
7a2f7d18 | 229 | if (b_ptr) { |
7f9f95d9 | 230 | tipc_bcbearer_sort(net, &b_ptr->nodes, dest, false); |
7a2f7d18 YX |
231 | tipc_disc_remove_dest(b_ptr->link_req); |
232 | } | |
233 | rcu_read_unlock(); | |
b97bf3fd PL |
234 | } |
235 | ||
b97bf3fd PL |
236 | /** |
237 | * tipc_enable_bearer - enable bearer with the given name | |
c4307285 | 238 | */ |
c93d3baa YX |
239 | int tipc_enable_bearer(struct net *net, const char *name, u32 disc_domain, |
240 | u32 priority) | |
b97bf3fd | 241 | { |
7f9f95d9 | 242 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
2d627b92 | 243 | struct tipc_bearer *b_ptr; |
358a0d1c | 244 | struct tipc_media *m_ptr; |
f19765f4 | 245 | struct tipc_bearer_names b_names; |
b97bf3fd PL |
246 | char addr_string[16]; |
247 | u32 bearer_id; | |
248 | u32 with_this_prio; | |
249 | u32 i; | |
250 | int res = -EINVAL; | |
251 | ||
34747539 | 252 | if (!tn->own_addr) { |
2cf8aa19 EH |
253 | pr_warn("Bearer <%s> rejected, not supported in standalone mode\n", |
254 | name); | |
b97bf3fd | 255 | return -ENOPROTOOPT; |
a10bd924 | 256 | } |
f19765f4 | 257 | if (!bearer_name_validate(name, &b_names)) { |
2cf8aa19 | 258 | pr_warn("Bearer <%s> rejected, illegal name\n", name); |
16cb4b33 | 259 | return -EINVAL; |
a10bd924 | 260 | } |
66e019a6 | 261 | if (tipc_addr_domain_valid(disc_domain) && |
34747539 YX |
262 | (disc_domain != tn->own_addr)) { |
263 | if (tipc_in_scope(disc_domain, tn->own_addr)) { | |
264 | disc_domain = tn->own_addr & TIPC_CLUSTER_MASK; | |
66e019a6 | 265 | res = 0; /* accept any node in own cluster */ |
34747539 | 266 | } else if (in_own_cluster_exact(net, disc_domain)) |
66e019a6 AS |
267 | res = 0; /* accept specified node in own cluster */ |
268 | } | |
269 | if (res) { | |
2cf8aa19 EH |
270 | pr_warn("Bearer <%s> rejected, illegal discovery domain\n", |
271 | name); | |
a10bd924 AS |
272 | return -EINVAL; |
273 | } | |
9efde4a0 | 274 | if ((priority > TIPC_MAX_LINK_PRI) && |
a10bd924 | 275 | (priority != TIPC_MEDIA_LINK_PRI)) { |
2cf8aa19 | 276 | pr_warn("Bearer <%s> rejected, illegal priority\n", name); |
b97bf3fd | 277 | return -EINVAL; |
a10bd924 | 278 | } |
b97bf3fd | 279 | |
f19765f4 | 280 | m_ptr = tipc_media_find(b_names.media_name); |
b97bf3fd | 281 | if (!m_ptr) { |
2cf8aa19 EH |
282 | pr_warn("Bearer <%s> rejected, media <%s> not registered\n", |
283 | name, b_names.media_name); | |
7216cd94 | 284 | return -EINVAL; |
b97bf3fd | 285 | } |
16cb4b33 PL |
286 | |
287 | if (priority == TIPC_MEDIA_LINK_PRI) | |
b97bf3fd PL |
288 | priority = m_ptr->priority; |
289 | ||
290 | restart: | |
291 | bearer_id = MAX_BEARERS; | |
292 | with_this_prio = 1; | |
293 | for (i = MAX_BEARERS; i-- != 0; ) { | |
7f9f95d9 | 294 | b_ptr = rtnl_dereference(tn->bearer_list[i]); |
f47de12b | 295 | if (!b_ptr) { |
b97bf3fd PL |
296 | bearer_id = i; |
297 | continue; | |
298 | } | |
3874ccbb | 299 | if (!strcmp(name, b_ptr->name)) { |
2cf8aa19 EH |
300 | pr_warn("Bearer <%s> rejected, already enabled\n", |
301 | name); | |
7216cd94 | 302 | return -EINVAL; |
b97bf3fd | 303 | } |
3874ccbb | 304 | if ((b_ptr->priority == priority) && |
b97bf3fd PL |
305 | (++with_this_prio > 2)) { |
306 | if (priority-- == 0) { | |
2cf8aa19 EH |
307 | pr_warn("Bearer <%s> rejected, duplicate priority\n", |
308 | name); | |
7216cd94 | 309 | return -EINVAL; |
b97bf3fd | 310 | } |
2cf8aa19 EH |
311 | pr_warn("Bearer <%s> priority adjustment required %u->%u\n", |
312 | name, priority + 1, priority); | |
b97bf3fd PL |
313 | goto restart; |
314 | } | |
315 | } | |
316 | if (bearer_id >= MAX_BEARERS) { | |
2cf8aa19 EH |
317 | pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n", |
318 | name, MAX_BEARERS); | |
7216cd94 | 319 | return -EINVAL; |
b97bf3fd PL |
320 | } |
321 | ||
3874ccbb | 322 | b_ptr = kzalloc(sizeof(*b_ptr), GFP_ATOMIC); |
7216cd94 YX |
323 | if (!b_ptr) |
324 | return -ENOMEM; | |
325 | ||
2d627b92 | 326 | strcpy(b_ptr->name, name); |
e4d050cb | 327 | b_ptr->media = m_ptr; |
7f9f95d9 | 328 | res = m_ptr->enable_media(net, b_ptr); |
b97bf3fd | 329 | if (res) { |
2cf8aa19 EH |
330 | pr_warn("Bearer <%s> rejected, enable failure (%d)\n", |
331 | name, -res); | |
7216cd94 | 332 | return -EINVAL; |
b97bf3fd PL |
333 | } |
334 | ||
335 | b_ptr->identity = bearer_id; | |
5c216e1d AS |
336 | b_ptr->tolerance = m_ptr->tolerance; |
337 | b_ptr->window = m_ptr->window; | |
a21a584d | 338 | b_ptr->domain = disc_domain; |
b97bf3fd | 339 | b_ptr->net_plane = bearer_id + 'A'; |
b97bf3fd | 340 | b_ptr->priority = priority; |
3a777ff8 | 341 | |
c93d3baa | 342 | res = tipc_disc_create(net, b_ptr, &b_ptr->bcast_addr); |
3a777ff8 | 343 | if (res) { |
f2f9800d | 344 | bearer_disable(net, b_ptr, false); |
2cf8aa19 EH |
345 | pr_warn("Bearer <%s> rejected, discovery object creation failed\n", |
346 | name); | |
7216cd94 | 347 | return -EINVAL; |
3a777ff8 | 348 | } |
3874ccbb | 349 | |
7f9f95d9 | 350 | rcu_assign_pointer(tn->bearer_list[bearer_id], b_ptr); |
3874ccbb | 351 | |
2cf8aa19 EH |
352 | pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n", |
353 | name, | |
354 | tipc_addr_string_fill(addr_string, disc_domain), priority); | |
b97bf3fd PL |
355 | return res; |
356 | } | |
357 | ||
358 | /** | |
512137ee | 359 | * tipc_reset_bearer - Reset all links established over this bearer |
b97bf3fd | 360 | */ |
c93d3baa | 361 | static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b_ptr) |
b97bf3fd | 362 | { |
512137ee | 363 | pr_info("Resetting bearer <%s>\n", b_ptr->name); |
f2f9800d | 364 | tipc_link_reset_list(net, b_ptr->identity); |
c93d3baa | 365 | tipc_disc_reset(net, b_ptr); |
0e35fd5e | 366 | return 0; |
b97bf3fd PL |
367 | } |
368 | ||
369 | /** | |
617d3c7a | 370 | * bearer_disable |
c4307285 | 371 | * |
7216cd94 | 372 | * Note: This routine assumes caller holds RTNL lock. |
b97bf3fd | 373 | */ |
f2f9800d YX |
374 | static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr, |
375 | bool shutting_down) | |
b97bf3fd | 376 | { |
7f9f95d9 | 377 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
3874ccbb YX |
378 | u32 i; |
379 | ||
2cf8aa19 | 380 | pr_info("Disabling bearer <%s>\n", b_ptr->name); |
4babbaa8 | 381 | b_ptr->media->disable_media(b_ptr); |
d4cca39d | 382 | |
f2f9800d | 383 | tipc_link_delete_list(net, b_ptr->identity, shutting_down); |
a8304529 YX |
384 | if (b_ptr->link_req) |
385 | tipc_disc_delete(b_ptr->link_req); | |
3874ccbb YX |
386 | |
387 | for (i = 0; i < MAX_BEARERS; i++) { | |
7f9f95d9 YX |
388 | if (b_ptr == rtnl_dereference(tn->bearer_list[i])) { |
389 | RCU_INIT_POINTER(tn->bearer_list[i], NULL); | |
3874ccbb YX |
390 | break; |
391 | } | |
392 | } | |
f8322dfc | 393 | kfree_rcu(b_ptr, rcu); |
b97bf3fd PL |
394 | } |
395 | ||
f2f9800d | 396 | int tipc_disable_bearer(struct net *net, const char *name) |
b97bf3fd | 397 | { |
2d627b92 | 398 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
399 | int res; |
400 | ||
7f9f95d9 | 401 | b_ptr = tipc_bearer_find(net, name); |
ccc901ee | 402 | if (b_ptr == NULL) { |
2cf8aa19 | 403 | pr_warn("Attempt to disable unknown bearer <%s>\n", name); |
ccc901ee | 404 | res = -EINVAL; |
28cc937e | 405 | } else { |
f2f9800d | 406 | bearer_disable(net, b_ptr, false); |
28cc937e AS |
407 | res = 0; |
408 | } | |
b97bf3fd PL |
409 | return res; |
410 | } | |
411 | ||
7f9f95d9 | 412 | int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b) |
e4d050cb YX |
413 | { |
414 | struct net_device *dev; | |
415 | char *driver_name = strchr((const char *)b->name, ':') + 1; | |
416 | ||
417 | /* Find device with specified name */ | |
7f9f95d9 | 418 | dev = dev_get_by_name(net, driver_name); |
e4d050cb YX |
419 | if (!dev) |
420 | return -ENODEV; | |
421 | ||
38504c28 | 422 | /* Associate TIPC bearer with L2 bearer */ |
2231c5af | 423 | rcu_assign_pointer(b->media_ptr, dev); |
38504c28 | 424 | memset(&b->bcast_addr, 0, sizeof(b->bcast_addr)); |
e4d050cb YX |
425 | memcpy(b->bcast_addr.value, dev->broadcast, b->media->hwaddr_len); |
426 | b->bcast_addr.media_id = b->media->type_id; | |
427 | b->bcast_addr.broadcast = 1; | |
428 | b->mtu = dev->mtu; | |
38504c28 | 429 | b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr); |
e4d050cb YX |
430 | rcu_assign_pointer(dev->tipc_ptr, b); |
431 | return 0; | |
432 | } | |
433 | ||
38504c28 | 434 | /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface |
e4d050cb | 435 | * |
38504c28 | 436 | * Mark L2 bearer as inactive so that incoming buffers are thrown away, |
e4d050cb YX |
437 | * then get worker thread to complete bearer cleanup. (Can't do cleanup |
438 | * here because cleanup code needs to sleep and caller holds spinlocks.) | |
439 | */ | |
440 | void tipc_disable_l2_media(struct tipc_bearer *b) | |
441 | { | |
2231c5af YX |
442 | struct net_device *dev; |
443 | ||
444 | dev = (struct net_device *)rtnl_dereference(b->media_ptr); | |
445 | RCU_INIT_POINTER(b->media_ptr, NULL); | |
e4d050cb | 446 | RCU_INIT_POINTER(dev->tipc_ptr, NULL); |
f1c8d8cb | 447 | synchronize_net(); |
e4d050cb YX |
448 | dev_put(dev); |
449 | } | |
450 | ||
451 | /** | |
38504c28 | 452 | * tipc_l2_send_msg - send a TIPC packet out over an L2 interface |
e4d050cb | 453 | * @buf: the packet to be sent |
963a1855 | 454 | * @b_ptr: the bearer through which the packet is to be sent |
e4d050cb YX |
455 | * @dest: peer destination address |
456 | */ | |
1da46568 YX |
457 | int tipc_l2_send_msg(struct net *net, struct sk_buff *buf, |
458 | struct tipc_bearer *b, struct tipc_media_addr *dest) | |
e4d050cb YX |
459 | { |
460 | struct sk_buff *clone; | |
2231c5af | 461 | struct net_device *dev; |
e4d050cb | 462 | int delta; |
2231c5af YX |
463 | |
464 | dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr); | |
465 | if (!dev) | |
466 | return 0; | |
e4d050cb YX |
467 | |
468 | clone = skb_clone(buf, GFP_ATOMIC); | |
469 | if (!clone) | |
470 | return 0; | |
471 | ||
472 | delta = dev->hard_header_len - skb_headroom(buf); | |
473 | if ((delta > 0) && | |
474 | pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) { | |
475 | kfree_skb(clone); | |
476 | return 0; | |
477 | } | |
478 | ||
479 | skb_reset_network_header(clone); | |
480 | clone->dev = dev; | |
481 | clone->protocol = htons(ETH_P_TIPC); | |
482 | dev_hard_header(clone, dev, ETH_P_TIPC, dest->value, | |
483 | dev->dev_addr, clone->len); | |
484 | dev_queue_xmit(clone); | |
485 | return 0; | |
486 | } | |
487 | ||
488 | /* tipc_bearer_send- sends buffer to destination over bearer | |
489 | * | |
490 | * IMPORTANT: | |
491 | * The media send routine must not alter the buffer being passed in | |
492 | * as it may be needed for later retransmission! | |
493 | */ | |
7f9f95d9 | 494 | void tipc_bearer_send(struct net *net, u32 bearer_id, struct sk_buff *buf, |
e4d050cb YX |
495 | struct tipc_media_addr *dest) |
496 | { | |
7f9f95d9 | 497 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
7a2f7d18 YX |
498 | struct tipc_bearer *b_ptr; |
499 | ||
500 | rcu_read_lock(); | |
7f9f95d9 | 501 | b_ptr = rcu_dereference_rtnl(tn->bearer_list[bearer_id]); |
7a2f7d18 | 502 | if (likely(b_ptr)) |
1da46568 | 503 | b_ptr->media->send_msg(net, buf, b_ptr, dest); |
7a2f7d18 | 504 | rcu_read_unlock(); |
e4d050cb YX |
505 | } |
506 | ||
6e967adf YX |
507 | /** |
508 | * tipc_l2_rcv_msg - handle incoming TIPC message from an interface | |
509 | * @buf: the received packet | |
510 | * @dev: the net device that the packet was received on | |
511 | * @pt: the packet_type structure which was used to register this handler | |
512 | * @orig_dev: the original receive net device in case the device is a bond | |
513 | * | |
514 | * Accept only packets explicitly sent to this node, or broadcast packets; | |
515 | * ignores packets sent using interface multicast, and traffic sent to other | |
516 | * nodes (which can happen if interface is running in promiscuous mode). | |
517 | */ | |
518 | static int tipc_l2_rcv_msg(struct sk_buff *buf, struct net_device *dev, | |
519 | struct packet_type *pt, struct net_device *orig_dev) | |
520 | { | |
521 | struct tipc_bearer *b_ptr; | |
522 | ||
6e967adf | 523 | rcu_read_lock(); |
ca07fb07 | 524 | b_ptr = rcu_dereference_rtnl(dev->tipc_ptr); |
6e967adf YX |
525 | if (likely(b_ptr)) { |
526 | if (likely(buf->pkt_type <= PACKET_BROADCAST)) { | |
527 | buf->next = NULL; | |
c93d3baa | 528 | tipc_rcv(dev_net(dev), buf, b_ptr); |
6e967adf YX |
529 | rcu_read_unlock(); |
530 | return NET_RX_SUCCESS; | |
531 | } | |
532 | } | |
533 | rcu_read_unlock(); | |
534 | ||
535 | kfree_skb(buf); | |
536 | return NET_RX_DROP; | |
537 | } | |
538 | ||
539 | /** | |
540 | * tipc_l2_device_event - handle device events from network device | |
541 | * @nb: the context of the notification | |
542 | * @evt: the type of event | |
543 | * @ptr: the net device that the event was on | |
544 | * | |
545 | * This function is called by the Ethernet driver in case of link | |
546 | * change event. | |
547 | */ | |
548 | static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt, | |
549 | void *ptr) | |
550 | { | |
6e967adf | 551 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
c93d3baa YX |
552 | struct net *net = dev_net(dev); |
553 | struct tipc_bearer *b_ptr; | |
6e967adf | 554 | |
ca07fb07 YX |
555 | b_ptr = rtnl_dereference(dev->tipc_ptr); |
556 | if (!b_ptr) | |
6e967adf | 557 | return NOTIFY_DONE; |
6e967adf YX |
558 | |
559 | b_ptr->mtu = dev->mtu; | |
560 | ||
561 | switch (evt) { | |
562 | case NETDEV_CHANGE: | |
563 | if (netif_carrier_ok(dev)) | |
564 | break; | |
565 | case NETDEV_DOWN: | |
566 | case NETDEV_CHANGEMTU: | |
c93d3baa | 567 | tipc_reset_bearer(net, b_ptr); |
a21a584d | 568 | break; |
6e967adf | 569 | case NETDEV_CHANGEADDR: |
38504c28 | 570 | b_ptr->media->raw2addr(b_ptr, &b_ptr->addr, |
a21a584d | 571 | (char *)dev->dev_addr); |
c93d3baa | 572 | tipc_reset_bearer(net, b_ptr); |
6e967adf YX |
573 | break; |
574 | case NETDEV_UNREGISTER: | |
575 | case NETDEV_CHANGENAME: | |
f2f9800d | 576 | bearer_disable(dev_net(dev), b_ptr, false); |
6e967adf YX |
577 | break; |
578 | } | |
6e967adf YX |
579 | return NOTIFY_OK; |
580 | } | |
581 | ||
582 | static struct packet_type tipc_packet_type __read_mostly = { | |
184593c7 | 583 | .type = htons(ETH_P_TIPC), |
6e967adf YX |
584 | .func = tipc_l2_rcv_msg, |
585 | }; | |
586 | ||
587 | static struct notifier_block notifier = { | |
588 | .notifier_call = tipc_l2_device_event, | |
589 | .priority = 0, | |
590 | }; | |
591 | ||
592 | int tipc_bearer_setup(void) | |
593 | { | |
970122fd YX |
594 | int err; |
595 | ||
596 | err = register_netdevice_notifier(¬ifier); | |
597 | if (err) | |
598 | return err; | |
6e967adf | 599 | dev_add_pack(&tipc_packet_type); |
970122fd | 600 | return 0; |
6e967adf YX |
601 | } |
602 | ||
603 | void tipc_bearer_cleanup(void) | |
604 | { | |
605 | unregister_netdevice_notifier(¬ifier); | |
606 | dev_remove_pack(&tipc_packet_type); | |
607 | } | |
b97bf3fd | 608 | |
f2f9800d | 609 | void tipc_bearer_stop(struct net *net) |
b97bf3fd | 610 | { |
7f9f95d9 | 611 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
3874ccbb | 612 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
613 | u32 i; |
614 | ||
b97bf3fd | 615 | for (i = 0; i < MAX_BEARERS; i++) { |
7f9f95d9 | 616 | b_ptr = rtnl_dereference(tn->bearer_list[i]); |
f47de12b | 617 | if (b_ptr) { |
f2f9800d | 618 | bearer_disable(net, b_ptr, true); |
7f9f95d9 | 619 | tn->bearer_list[i] = NULL; |
3874ccbb | 620 | } |
b97bf3fd | 621 | } |
b97bf3fd | 622 | } |
0655f6a8 | 623 | |
35b9dd76 | 624 | /* Caller should hold rtnl_lock to protect the bearer */ |
d8182804 RA |
625 | static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg, |
626 | struct tipc_bearer *bearer) | |
35b9dd76 RA |
627 | { |
628 | void *hdr; | |
629 | struct nlattr *attrs; | |
630 | struct nlattr *prop; | |
631 | ||
bfb3e5dd | 632 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
35b9dd76 RA |
633 | NLM_F_MULTI, TIPC_NL_BEARER_GET); |
634 | if (!hdr) | |
635 | return -EMSGSIZE; | |
636 | ||
637 | attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER); | |
638 | if (!attrs) | |
639 | goto msg_full; | |
640 | ||
641 | if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name)) | |
642 | goto attr_msg_full; | |
643 | ||
644 | prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP); | |
645 | if (!prop) | |
646 | goto prop_msg_full; | |
647 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority)) | |
648 | goto prop_msg_full; | |
649 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance)) | |
650 | goto prop_msg_full; | |
651 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window)) | |
652 | goto prop_msg_full; | |
653 | ||
654 | nla_nest_end(msg->skb, prop); | |
655 | nla_nest_end(msg->skb, attrs); | |
656 | genlmsg_end(msg->skb, hdr); | |
657 | ||
658 | return 0; | |
659 | ||
660 | prop_msg_full: | |
661 | nla_nest_cancel(msg->skb, prop); | |
662 | attr_msg_full: | |
663 | nla_nest_cancel(msg->skb, attrs); | |
664 | msg_full: | |
665 | genlmsg_cancel(msg->skb, hdr); | |
666 | ||
667 | return -EMSGSIZE; | |
668 | } | |
669 | ||
670 | int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
671 | { | |
672 | int err; | |
673 | int i = cb->args[0]; | |
674 | struct tipc_bearer *bearer; | |
675 | struct tipc_nl_msg msg; | |
7f9f95d9 YX |
676 | struct net *net = sock_net(skb->sk); |
677 | struct tipc_net *tn = net_generic(net, tipc_net_id); | |
35b9dd76 RA |
678 | |
679 | if (i == MAX_BEARERS) | |
680 | return 0; | |
681 | ||
682 | msg.skb = skb; | |
683 | msg.portid = NETLINK_CB(cb->skb).portid; | |
684 | msg.seq = cb->nlh->nlmsg_seq; | |
685 | ||
686 | rtnl_lock(); | |
687 | for (i = 0; i < MAX_BEARERS; i++) { | |
7f9f95d9 | 688 | bearer = rtnl_dereference(tn->bearer_list[i]); |
35b9dd76 RA |
689 | if (!bearer) |
690 | continue; | |
691 | ||
692 | err = __tipc_nl_add_bearer(&msg, bearer); | |
693 | if (err) | |
694 | break; | |
695 | } | |
696 | rtnl_unlock(); | |
697 | ||
698 | cb->args[0] = i; | |
699 | return skb->len; | |
700 | } | |
701 | ||
702 | int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info) | |
703 | { | |
704 | int err; | |
705 | char *name; | |
706 | struct sk_buff *rep; | |
707 | struct tipc_bearer *bearer; | |
708 | struct tipc_nl_msg msg; | |
709 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
7f9f95d9 | 710 | struct net *net = genl_info_net(info); |
35b9dd76 RA |
711 | |
712 | if (!info->attrs[TIPC_NLA_BEARER]) | |
713 | return -EINVAL; | |
714 | ||
715 | err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, | |
716 | info->attrs[TIPC_NLA_BEARER], | |
717 | tipc_nl_bearer_policy); | |
718 | if (err) | |
719 | return err; | |
720 | ||
721 | if (!attrs[TIPC_NLA_BEARER_NAME]) | |
722 | return -EINVAL; | |
723 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); | |
724 | ||
725 | rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
726 | if (!rep) | |
727 | return -ENOMEM; | |
728 | ||
729 | msg.skb = rep; | |
730 | msg.portid = info->snd_portid; | |
731 | msg.seq = info->snd_seq; | |
732 | ||
733 | rtnl_lock(); | |
7f9f95d9 | 734 | bearer = tipc_bearer_find(net, name); |
35b9dd76 RA |
735 | if (!bearer) { |
736 | err = -EINVAL; | |
737 | goto err_out; | |
738 | } | |
739 | ||
740 | err = __tipc_nl_add_bearer(&msg, bearer); | |
741 | if (err) | |
742 | goto err_out; | |
743 | rtnl_unlock(); | |
744 | ||
745 | return genlmsg_reply(rep, info); | |
746 | err_out: | |
747 | rtnl_unlock(); | |
748 | nlmsg_free(rep); | |
749 | ||
750 | return err; | |
751 | } | |
752 | ||
0655f6a8 RA |
753 | int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info) |
754 | { | |
755 | int err; | |
756 | char *name; | |
757 | struct tipc_bearer *bearer; | |
758 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
f2f9800d | 759 | struct net *net = genl_info_net(info); |
0655f6a8 RA |
760 | |
761 | if (!info->attrs[TIPC_NLA_BEARER]) | |
762 | return -EINVAL; | |
763 | ||
764 | err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, | |
765 | info->attrs[TIPC_NLA_BEARER], | |
766 | tipc_nl_bearer_policy); | |
767 | if (err) | |
768 | return err; | |
769 | ||
770 | if (!attrs[TIPC_NLA_BEARER_NAME]) | |
771 | return -EINVAL; | |
772 | ||
773 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); | |
774 | ||
775 | rtnl_lock(); | |
7f9f95d9 | 776 | bearer = tipc_bearer_find(net, name); |
0655f6a8 RA |
777 | if (!bearer) { |
778 | rtnl_unlock(); | |
779 | return -EINVAL; | |
780 | } | |
781 | ||
f2f9800d | 782 | bearer_disable(net, bearer, false); |
0655f6a8 RA |
783 | rtnl_unlock(); |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info) | |
789 | { | |
c93d3baa | 790 | struct net *net = genl_info_net(info); |
34747539 | 791 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
0655f6a8 RA |
792 | int err; |
793 | char *bearer; | |
794 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
795 | u32 domain; | |
796 | u32 prio; | |
797 | ||
798 | prio = TIPC_MEDIA_LINK_PRI; | |
34747539 | 799 | domain = tn->own_addr & TIPC_CLUSTER_MASK; |
0655f6a8 RA |
800 | |
801 | if (!info->attrs[TIPC_NLA_BEARER]) | |
802 | return -EINVAL; | |
803 | ||
804 | err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, | |
805 | info->attrs[TIPC_NLA_BEARER], | |
806 | tipc_nl_bearer_policy); | |
807 | if (err) | |
808 | return err; | |
809 | ||
810 | if (!attrs[TIPC_NLA_BEARER_NAME]) | |
811 | return -EINVAL; | |
812 | ||
813 | bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]); | |
814 | ||
815 | if (attrs[TIPC_NLA_BEARER_DOMAIN]) | |
816 | domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]); | |
817 | ||
818 | if (attrs[TIPC_NLA_BEARER_PROP]) { | |
819 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; | |
820 | ||
821 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], | |
822 | props); | |
823 | if (err) | |
824 | return err; | |
825 | ||
826 | if (props[TIPC_NLA_PROP_PRIO]) | |
827 | prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); | |
828 | } | |
829 | ||
830 | rtnl_lock(); | |
c93d3baa | 831 | err = tipc_enable_bearer(net, bearer, domain, prio); |
0655f6a8 RA |
832 | if (err) { |
833 | rtnl_unlock(); | |
834 | return err; | |
835 | } | |
836 | rtnl_unlock(); | |
837 | ||
838 | return 0; | |
839 | } | |
315c00bc RA |
840 | |
841 | int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info) | |
842 | { | |
843 | int err; | |
844 | char *name; | |
845 | struct tipc_bearer *b; | |
846 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
7f9f95d9 | 847 | struct net *net = genl_info_net(info); |
315c00bc RA |
848 | |
849 | if (!info->attrs[TIPC_NLA_BEARER]) | |
850 | return -EINVAL; | |
851 | ||
852 | err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX, | |
853 | info->attrs[TIPC_NLA_BEARER], | |
854 | tipc_nl_bearer_policy); | |
855 | if (err) | |
856 | return err; | |
857 | ||
858 | if (!attrs[TIPC_NLA_BEARER_NAME]) | |
859 | return -EINVAL; | |
860 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); | |
861 | ||
862 | rtnl_lock(); | |
7f9f95d9 | 863 | b = tipc_bearer_find(net, name); |
315c00bc RA |
864 | if (!b) { |
865 | rtnl_unlock(); | |
866 | return -EINVAL; | |
867 | } | |
868 | ||
869 | if (attrs[TIPC_NLA_BEARER_PROP]) { | |
870 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; | |
871 | ||
872 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], | |
873 | props); | |
874 | if (err) { | |
875 | rtnl_unlock(); | |
876 | return err; | |
877 | } | |
878 | ||
879 | if (props[TIPC_NLA_PROP_TOL]) | |
880 | b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); | |
881 | if (props[TIPC_NLA_PROP_PRIO]) | |
882 | b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); | |
883 | if (props[TIPC_NLA_PROP_WIN]) | |
884 | b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); | |
885 | } | |
886 | rtnl_unlock(); | |
887 | ||
888 | return 0; | |
889 | } | |
46f15c67 | 890 | |
d8182804 RA |
891 | static int __tipc_nl_add_media(struct tipc_nl_msg *msg, |
892 | struct tipc_media *media) | |
46f15c67 RA |
893 | { |
894 | void *hdr; | |
895 | struct nlattr *attrs; | |
896 | struct nlattr *prop; | |
897 | ||
bfb3e5dd | 898 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
46f15c67 RA |
899 | NLM_F_MULTI, TIPC_NL_MEDIA_GET); |
900 | if (!hdr) | |
901 | return -EMSGSIZE; | |
902 | ||
903 | attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA); | |
904 | if (!attrs) | |
905 | goto msg_full; | |
906 | ||
907 | if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name)) | |
908 | goto attr_msg_full; | |
909 | ||
910 | prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP); | |
911 | if (!prop) | |
912 | goto prop_msg_full; | |
913 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority)) | |
914 | goto prop_msg_full; | |
915 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance)) | |
916 | goto prop_msg_full; | |
917 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window)) | |
918 | goto prop_msg_full; | |
919 | ||
920 | nla_nest_end(msg->skb, prop); | |
921 | nla_nest_end(msg->skb, attrs); | |
922 | genlmsg_end(msg->skb, hdr); | |
923 | ||
924 | return 0; | |
925 | ||
926 | prop_msg_full: | |
927 | nla_nest_cancel(msg->skb, prop); | |
928 | attr_msg_full: | |
929 | nla_nest_cancel(msg->skb, attrs); | |
930 | msg_full: | |
931 | genlmsg_cancel(msg->skb, hdr); | |
932 | ||
933 | return -EMSGSIZE; | |
934 | } | |
935 | ||
936 | int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
937 | { | |
938 | int err; | |
939 | int i = cb->args[0]; | |
940 | struct tipc_nl_msg msg; | |
941 | ||
942 | if (i == MAX_MEDIA) | |
943 | return 0; | |
944 | ||
945 | msg.skb = skb; | |
946 | msg.portid = NETLINK_CB(cb->skb).portid; | |
947 | msg.seq = cb->nlh->nlmsg_seq; | |
948 | ||
949 | rtnl_lock(); | |
950 | for (; media_info_array[i] != NULL; i++) { | |
951 | err = __tipc_nl_add_media(&msg, media_info_array[i]); | |
952 | if (err) | |
953 | break; | |
954 | } | |
955 | rtnl_unlock(); | |
956 | ||
957 | cb->args[0] = i; | |
958 | return skb->len; | |
959 | } | |
960 | ||
961 | int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info) | |
962 | { | |
963 | int err; | |
964 | char *name; | |
965 | struct tipc_nl_msg msg; | |
966 | struct tipc_media *media; | |
967 | struct sk_buff *rep; | |
968 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
969 | ||
970 | if (!info->attrs[TIPC_NLA_MEDIA]) | |
971 | return -EINVAL; | |
972 | ||
973 | err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX, | |
974 | info->attrs[TIPC_NLA_MEDIA], | |
975 | tipc_nl_media_policy); | |
976 | if (err) | |
977 | return err; | |
978 | ||
979 | if (!attrs[TIPC_NLA_MEDIA_NAME]) | |
980 | return -EINVAL; | |
981 | name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); | |
982 | ||
983 | rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
984 | if (!rep) | |
985 | return -ENOMEM; | |
986 | ||
987 | msg.skb = rep; | |
988 | msg.portid = info->snd_portid; | |
989 | msg.seq = info->snd_seq; | |
990 | ||
991 | rtnl_lock(); | |
992 | media = tipc_media_find(name); | |
993 | if (!media) { | |
994 | err = -EINVAL; | |
995 | goto err_out; | |
996 | } | |
997 | ||
998 | err = __tipc_nl_add_media(&msg, media); | |
999 | if (err) | |
1000 | goto err_out; | |
1001 | rtnl_unlock(); | |
1002 | ||
1003 | return genlmsg_reply(rep, info); | |
1004 | err_out: | |
1005 | rtnl_unlock(); | |
1006 | nlmsg_free(rep); | |
1007 | ||
1008 | return err; | |
1009 | } | |
1e55417d RA |
1010 | |
1011 | int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info) | |
1012 | { | |
1013 | int err; | |
1014 | char *name; | |
1015 | struct tipc_media *m; | |
1016 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; | |
1017 | ||
1018 | if (!info->attrs[TIPC_NLA_MEDIA]) | |
1019 | return -EINVAL; | |
1020 | ||
1021 | err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX, | |
1022 | info->attrs[TIPC_NLA_MEDIA], | |
1023 | tipc_nl_media_policy); | |
1024 | ||
1025 | if (!attrs[TIPC_NLA_MEDIA_NAME]) | |
1026 | return -EINVAL; | |
1027 | name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); | |
1028 | ||
1029 | rtnl_lock(); | |
1030 | m = tipc_media_find(name); | |
1031 | if (!m) { | |
1032 | rtnl_unlock(); | |
1033 | return -EINVAL; | |
1034 | } | |
1035 | ||
1036 | if (attrs[TIPC_NLA_MEDIA_PROP]) { | |
1037 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; | |
1038 | ||
1039 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP], | |
1040 | props); | |
1041 | if (err) { | |
1042 | rtnl_unlock(); | |
1043 | return err; | |
1044 | } | |
1045 | ||
1046 | if (props[TIPC_NLA_PROP_TOL]) | |
1047 | m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); | |
1048 | if (props[TIPC_NLA_PROP_PRIO]) | |
1049 | m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); | |
1050 | if (props[TIPC_NLA_PROP_WIN]) | |
1051 | m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); | |
1052 | } | |
1053 | rtnl_unlock(); | |
1054 | ||
1055 | return 0; | |
1056 | } |