Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/bearer.c: TIPC bearer code |
| 3 | * |
| 4 | * Copyright (c) 1996-2006, 2013-2016, Ericsson AB |
| 5 | * Copyright (c) 2004-2006, 2010-2013, Wind River Systems |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include <net/sock.h> |
| 38 | #include "core.h" |
| 39 | #include "bearer.h" |
| 40 | #include "link.h" |
| 41 | #include "discover.h" |
| 42 | #include "monitor.h" |
| 43 | #include "bcast.h" |
| 44 | #include "netlink.h" |
| 45 | #include "udp_media.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 46 | #include "trace.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | |
| 48 | #define MAX_ADDR_STR 60 |
| 49 | |
| 50 | static struct tipc_media * const media_info_array[] = { |
| 51 | ð_media_info, |
| 52 | #ifdef CONFIG_TIPC_MEDIA_IB |
| 53 | &ib_media_info, |
| 54 | #endif |
| 55 | #ifdef CONFIG_TIPC_MEDIA_UDP |
| 56 | &udp_media_info, |
| 57 | #endif |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | static struct tipc_bearer *bearer_get(struct net *net, int bearer_id) |
| 62 | { |
| 63 | struct tipc_net *tn = tipc_net(net); |
| 64 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 65 | return rcu_dereference(tn->bearer_list[bearer_id]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void bearer_disable(struct net *net, struct tipc_bearer *b); |
| 69 | static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev, |
| 70 | struct packet_type *pt, struct net_device *orig_dev); |
| 71 | |
| 72 | /** |
| 73 | * tipc_media_find - locates specified media object by name |
| 74 | */ |
| 75 | struct tipc_media *tipc_media_find(const char *name) |
| 76 | { |
| 77 | u32 i; |
| 78 | |
| 79 | for (i = 0; media_info_array[i] != NULL; i++) { |
| 80 | if (!strcmp(media_info_array[i]->name, name)) |
| 81 | break; |
| 82 | } |
| 83 | return media_info_array[i]; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * media_find_id - locates specified media object by type identifier |
| 88 | */ |
| 89 | static struct tipc_media *media_find_id(u8 type) |
| 90 | { |
| 91 | u32 i; |
| 92 | |
| 93 | for (i = 0; media_info_array[i] != NULL; i++) { |
| 94 | if (media_info_array[i]->type_id == type) |
| 95 | break; |
| 96 | } |
| 97 | return media_info_array[i]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * tipc_media_addr_printf - record media address in print buffer |
| 102 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 103 | int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | { |
| 105 | char addr_str[MAX_ADDR_STR]; |
| 106 | struct tipc_media *m; |
| 107 | int ret; |
| 108 | |
| 109 | m = media_find_id(a->media_id); |
| 110 | |
| 111 | if (m && !m->addr2str(a, addr_str, sizeof(addr_str))) |
| 112 | ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str); |
| 113 | else { |
| 114 | u32 i; |
| 115 | |
| 116 | ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id); |
| 117 | for (i = 0; i < sizeof(a->value); i++) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 118 | ret += scnprintf(buf + ret, len - ret, |
| 119 | "-%x", a->value[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 121 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * bearer_name_validate - validate & (optionally) deconstruct bearer name |
| 126 | * @name: ptr to bearer name string |
| 127 | * @name_parts: ptr to area for bearer name components (or NULL if not needed) |
| 128 | * |
| 129 | * Returns 1 if bearer name is valid, otherwise 0. |
| 130 | */ |
| 131 | static int bearer_name_validate(const char *name, |
| 132 | struct tipc_bearer_names *name_parts) |
| 133 | { |
| 134 | char name_copy[TIPC_MAX_BEARER_NAME]; |
| 135 | char *media_name; |
| 136 | char *if_name; |
| 137 | u32 media_len; |
| 138 | u32 if_len; |
| 139 | |
| 140 | /* copy bearer name & ensure length is OK */ |
| 141 | name_copy[TIPC_MAX_BEARER_NAME - 1] = 0; |
| 142 | /* need above in case non-Posix strncpy() doesn't pad with nulls */ |
| 143 | strncpy(name_copy, name, TIPC_MAX_BEARER_NAME); |
| 144 | if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0) |
| 145 | return 0; |
| 146 | |
| 147 | /* ensure all component parts of bearer name are present */ |
| 148 | media_name = name_copy; |
| 149 | if_name = strchr(media_name, ':'); |
| 150 | if (if_name == NULL) |
| 151 | return 0; |
| 152 | *(if_name++) = 0; |
| 153 | media_len = if_name - media_name; |
| 154 | if_len = strlen(if_name) + 1; |
| 155 | |
| 156 | /* validate component parts of bearer name */ |
| 157 | if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) || |
| 158 | (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME)) |
| 159 | return 0; |
| 160 | |
| 161 | /* return bearer name components, if necessary */ |
| 162 | if (name_parts) { |
| 163 | strcpy(name_parts->media_name, media_name); |
| 164 | strcpy(name_parts->if_name, if_name); |
| 165 | } |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * tipc_bearer_find - locates bearer object with matching bearer name |
| 171 | */ |
| 172 | struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name) |
| 173 | { |
| 174 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 175 | struct tipc_bearer *b; |
| 176 | u32 i; |
| 177 | |
| 178 | for (i = 0; i < MAX_BEARERS; i++) { |
| 179 | b = rtnl_dereference(tn->bearer_list[i]); |
| 180 | if (b && (!strcmp(b->name, name))) |
| 181 | return b; |
| 182 | } |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | /* tipc_bearer_get_name - get the bearer name from its id. |
| 187 | * @net: network namespace |
| 188 | * @name: a pointer to the buffer where the name will be stored. |
| 189 | * @bearer_id: the id to get the name from. |
| 190 | */ |
| 191 | int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id) |
| 192 | { |
| 193 | struct tipc_net *tn = tipc_net(net); |
| 194 | struct tipc_bearer *b; |
| 195 | |
| 196 | if (bearer_id >= MAX_BEARERS) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | b = rtnl_dereference(tn->bearer_list[bearer_id]); |
| 200 | if (!b) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | strcpy(name, b->name); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest) |
| 208 | { |
| 209 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 210 | struct tipc_bearer *b; |
| 211 | |
| 212 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 213 | b = rcu_dereference(tn->bearer_list[bearer_id]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 214 | if (b) |
| 215 | tipc_disc_add_dest(b->disc); |
| 216 | rcu_read_unlock(); |
| 217 | } |
| 218 | |
| 219 | void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest) |
| 220 | { |
| 221 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 222 | struct tipc_bearer *b; |
| 223 | |
| 224 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | b = rcu_dereference(tn->bearer_list[bearer_id]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | if (b) |
| 227 | tipc_disc_remove_dest(b->disc); |
| 228 | rcu_read_unlock(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * tipc_enable_bearer - enable bearer with the given name |
| 233 | */ |
| 234 | static int tipc_enable_bearer(struct net *net, const char *name, |
| 235 | u32 disc_domain, u32 prio, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 236 | struct nlattr *attr[], |
| 237 | struct netlink_ext_ack *extack) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 238 | { |
| 239 | struct tipc_net *tn = tipc_net(net); |
| 240 | struct tipc_bearer_names b_names; |
| 241 | int with_this_prio = 1; |
| 242 | struct tipc_bearer *b; |
| 243 | struct tipc_media *m; |
| 244 | struct sk_buff *skb; |
| 245 | int bearer_id = 0; |
| 246 | int res = -EINVAL; |
| 247 | char *errstr = ""; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 248 | u32 i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 249 | |
| 250 | if (!bearer_name_validate(name, &b_names)) { |
| 251 | errstr = "illegal name"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 252 | NL_SET_ERR_MSG(extack, "Illegal name"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 253 | goto rejected; |
| 254 | } |
| 255 | |
| 256 | if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) { |
| 257 | errstr = "illegal priority"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 258 | NL_SET_ERR_MSG(extack, "Illegal priority"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 259 | goto rejected; |
| 260 | } |
| 261 | |
| 262 | m = tipc_media_find(b_names.media_name); |
| 263 | if (!m) { |
| 264 | errstr = "media not registered"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 265 | NL_SET_ERR_MSG(extack, "Media not registered"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 266 | goto rejected; |
| 267 | } |
| 268 | |
| 269 | if (prio == TIPC_MEDIA_LINK_PRI) |
| 270 | prio = m->priority; |
| 271 | |
| 272 | /* Check new bearer vs existing ones and find free bearer id if any */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 273 | bearer_id = MAX_BEARERS; |
| 274 | i = MAX_BEARERS; |
| 275 | while (i-- != 0) { |
| 276 | b = rtnl_dereference(tn->bearer_list[i]); |
| 277 | if (!b) { |
| 278 | bearer_id = i; |
| 279 | continue; |
| 280 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 281 | if (!strcmp(name, b->name)) { |
| 282 | errstr = "already enabled"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 283 | NL_SET_ERR_MSG(extack, "Already enabled"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 284 | goto rejected; |
| 285 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 286 | |
| 287 | if (b->priority == prio && |
| 288 | (++with_this_prio > 2)) { |
| 289 | pr_warn("Bearer <%s>: already 2 bearers with priority %u\n", |
| 290 | name, prio); |
| 291 | |
| 292 | if (prio == TIPC_MIN_LINK_PRI) { |
| 293 | errstr = "cannot adjust to lower"; |
| 294 | NL_SET_ERR_MSG(extack, "Cannot adjust to lower"); |
| 295 | goto rejected; |
| 296 | } |
| 297 | |
| 298 | pr_warn("Bearer <%s>: trying with adjusted priority\n", |
| 299 | name); |
| 300 | prio--; |
| 301 | bearer_id = MAX_BEARERS; |
| 302 | i = MAX_BEARERS; |
| 303 | with_this_prio = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 304 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (bearer_id >= MAX_BEARERS) { |
| 308 | errstr = "max 3 bearers permitted"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 309 | NL_SET_ERR_MSG(extack, "Max 3 bearers permitted"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 310 | goto rejected; |
| 311 | } |
| 312 | |
| 313 | b = kzalloc(sizeof(*b), GFP_ATOMIC); |
| 314 | if (!b) |
| 315 | return -ENOMEM; |
| 316 | |
| 317 | strcpy(b->name, name); |
| 318 | b->media = m; |
| 319 | res = m->enable_media(net, b, attr); |
| 320 | if (res) { |
| 321 | kfree(b); |
| 322 | errstr = "failed to enable media"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 323 | NL_SET_ERR_MSG(extack, "Failed to enable media"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 324 | goto rejected; |
| 325 | } |
| 326 | |
| 327 | b->identity = bearer_id; |
| 328 | b->tolerance = m->tolerance; |
| 329 | b->window = m->window; |
| 330 | b->domain = disc_domain; |
| 331 | b->net_plane = bearer_id + 'A'; |
| 332 | b->priority = prio; |
| 333 | test_and_set_bit_lock(0, &b->up); |
| 334 | |
| 335 | res = tipc_disc_create(net, b, &b->bcast_addr, &skb); |
| 336 | if (res) { |
| 337 | bearer_disable(net, b); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 338 | errstr = "failed to create discoverer"; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 339 | NL_SET_ERR_MSG(extack, "Failed to create discoverer"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 340 | goto rejected; |
| 341 | } |
| 342 | |
| 343 | rcu_assign_pointer(tn->bearer_list[bearer_id], b); |
| 344 | if (skb) |
| 345 | tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr); |
| 346 | |
| 347 | if (tipc_mon_create(net, bearer_id)) { |
| 348 | bearer_disable(net, b); |
| 349 | return -ENOMEM; |
| 350 | } |
| 351 | |
| 352 | pr_info("Enabled bearer <%s>, priority %u\n", name, prio); |
| 353 | |
| 354 | return res; |
| 355 | rejected: |
| 356 | pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr); |
| 357 | return res; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * tipc_reset_bearer - Reset all links established over this bearer |
| 362 | */ |
| 363 | static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b) |
| 364 | { |
| 365 | pr_info("Resetting bearer <%s>\n", b->name); |
| 366 | tipc_node_delete_links(net, b->identity); |
| 367 | tipc_disc_reset(net, b); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * bearer_disable |
| 373 | * |
| 374 | * Note: This routine assumes caller holds RTNL lock. |
| 375 | */ |
| 376 | static void bearer_disable(struct net *net, struct tipc_bearer *b) |
| 377 | { |
| 378 | struct tipc_net *tn = tipc_net(net); |
| 379 | int bearer_id = b->identity; |
| 380 | |
| 381 | pr_info("Disabling bearer <%s>\n", b->name); |
| 382 | clear_bit_unlock(0, &b->up); |
| 383 | tipc_node_delete_links(net, bearer_id); |
| 384 | b->media->disable_media(b); |
| 385 | RCU_INIT_POINTER(b->media_ptr, NULL); |
| 386 | if (b->disc) |
| 387 | tipc_disc_delete(b->disc); |
| 388 | RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL); |
| 389 | kfree_rcu(b, rcu); |
| 390 | tipc_mon_delete(net, bearer_id); |
| 391 | } |
| 392 | |
| 393 | int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b, |
| 394 | struct nlattr *attr[]) |
| 395 | { |
| 396 | char *dev_name = strchr((const char *)b->name, ':') + 1; |
| 397 | int hwaddr_len = b->media->hwaddr_len; |
| 398 | u8 node_id[NODE_ID_LEN] = {0,}; |
| 399 | struct net_device *dev; |
| 400 | |
| 401 | /* Find device with specified name */ |
| 402 | dev = dev_get_by_name(net, dev_name); |
| 403 | if (!dev) |
| 404 | return -ENODEV; |
| 405 | if (tipc_mtu_bad(dev, 0)) { |
| 406 | dev_put(dev); |
| 407 | return -EINVAL; |
| 408 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 409 | if (dev == net->loopback_dev) { |
| 410 | dev_put(dev); |
| 411 | pr_info("Enabling <%s> not permitted\n", b->name); |
| 412 | return -EINVAL; |
| 413 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 414 | |
| 415 | /* Autoconfigure own node identity if needed */ |
| 416 | if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) { |
| 417 | memcpy(node_id, dev->dev_addr, hwaddr_len); |
| 418 | tipc_net_init(net, node_id, 0); |
| 419 | } |
| 420 | if (!tipc_own_id(net)) { |
| 421 | dev_put(dev); |
| 422 | pr_warn("Failed to obtain node identity\n"); |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
| 426 | /* Associate TIPC bearer with L2 bearer */ |
| 427 | rcu_assign_pointer(b->media_ptr, dev); |
| 428 | b->pt.dev = dev; |
| 429 | b->pt.type = htons(ETH_P_TIPC); |
| 430 | b->pt.func = tipc_l2_rcv_msg; |
| 431 | dev_add_pack(&b->pt); |
| 432 | memset(&b->bcast_addr, 0, sizeof(b->bcast_addr)); |
| 433 | memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len); |
| 434 | b->bcast_addr.media_id = b->media->type_id; |
| 435 | b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT; |
| 436 | b->mtu = dev->mtu; |
| 437 | b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr); |
| 438 | rcu_assign_pointer(dev->tipc_ptr, b); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface |
| 443 | * |
| 444 | * Mark L2 bearer as inactive so that incoming buffers are thrown away |
| 445 | */ |
| 446 | void tipc_disable_l2_media(struct tipc_bearer *b) |
| 447 | { |
| 448 | struct net_device *dev; |
| 449 | |
| 450 | dev = (struct net_device *)rtnl_dereference(b->media_ptr); |
| 451 | dev_remove_pack(&b->pt); |
| 452 | RCU_INIT_POINTER(dev->tipc_ptr, NULL); |
| 453 | synchronize_net(); |
| 454 | dev_put(dev); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * tipc_l2_send_msg - send a TIPC packet out over an L2 interface |
| 459 | * @skb: the packet to be sent |
| 460 | * @b: the bearer through which the packet is to be sent |
| 461 | * @dest: peer destination address |
| 462 | */ |
| 463 | int tipc_l2_send_msg(struct net *net, struct sk_buff *skb, |
| 464 | struct tipc_bearer *b, struct tipc_media_addr *dest) |
| 465 | { |
| 466 | struct net_device *dev; |
| 467 | int delta; |
| 468 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 469 | dev = (struct net_device *)rcu_dereference(b->media_ptr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 470 | if (!dev) |
| 471 | return 0; |
| 472 | |
| 473 | delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb)); |
| 474 | if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) { |
| 475 | kfree_skb(skb); |
| 476 | return 0; |
| 477 | } |
| 478 | skb_reset_network_header(skb); |
| 479 | skb->dev = dev; |
| 480 | skb->protocol = htons(ETH_P_TIPC); |
| 481 | dev_hard_header(skb, dev, ETH_P_TIPC, dest->value, |
| 482 | dev->dev_addr, skb->len); |
| 483 | dev_queue_xmit(skb); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id) |
| 488 | { |
| 489 | bool supp = false; |
| 490 | struct tipc_bearer *b; |
| 491 | |
| 492 | rcu_read_lock(); |
| 493 | b = bearer_get(net, bearer_id); |
| 494 | if (b) |
| 495 | supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT); |
| 496 | rcu_read_unlock(); |
| 497 | return supp; |
| 498 | } |
| 499 | |
| 500 | int tipc_bearer_mtu(struct net *net, u32 bearer_id) |
| 501 | { |
| 502 | int mtu = 0; |
| 503 | struct tipc_bearer *b; |
| 504 | |
| 505 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 506 | b = rcu_dereference(tipc_net(net)->bearer_list[bearer_id]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 507 | if (b) |
| 508 | mtu = b->mtu; |
| 509 | rcu_read_unlock(); |
| 510 | return mtu; |
| 511 | } |
| 512 | |
| 513 | /* tipc_bearer_xmit_skb - sends buffer to destination over bearer |
| 514 | */ |
| 515 | void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id, |
| 516 | struct sk_buff *skb, |
| 517 | struct tipc_media_addr *dest) |
| 518 | { |
| 519 | struct tipc_msg *hdr = buf_msg(skb); |
| 520 | struct tipc_bearer *b; |
| 521 | |
| 522 | rcu_read_lock(); |
| 523 | b = bearer_get(net, bearer_id); |
| 524 | if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) |
| 525 | b->media->send_msg(net, skb, b, dest); |
| 526 | else |
| 527 | kfree_skb(skb); |
| 528 | rcu_read_unlock(); |
| 529 | } |
| 530 | |
| 531 | /* tipc_bearer_xmit() -send buffer to destination over bearer |
| 532 | */ |
| 533 | void tipc_bearer_xmit(struct net *net, u32 bearer_id, |
| 534 | struct sk_buff_head *xmitq, |
| 535 | struct tipc_media_addr *dst) |
| 536 | { |
| 537 | struct tipc_bearer *b; |
| 538 | struct sk_buff *skb, *tmp; |
| 539 | |
| 540 | if (skb_queue_empty(xmitq)) |
| 541 | return; |
| 542 | |
| 543 | rcu_read_lock(); |
| 544 | b = bearer_get(net, bearer_id); |
| 545 | if (unlikely(!b)) |
| 546 | __skb_queue_purge(xmitq); |
| 547 | skb_queue_walk_safe(xmitq, skb, tmp) { |
| 548 | __skb_dequeue(xmitq); |
| 549 | if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) |
| 550 | b->media->send_msg(net, skb, b, dst); |
| 551 | else |
| 552 | kfree_skb(skb); |
| 553 | } |
| 554 | rcu_read_unlock(); |
| 555 | } |
| 556 | |
| 557 | /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations |
| 558 | */ |
| 559 | void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id, |
| 560 | struct sk_buff_head *xmitq) |
| 561 | { |
| 562 | struct tipc_net *tn = tipc_net(net); |
| 563 | int net_id = tn->net_id; |
| 564 | struct tipc_bearer *b; |
| 565 | struct sk_buff *skb, *tmp; |
| 566 | struct tipc_msg *hdr; |
| 567 | |
| 568 | rcu_read_lock(); |
| 569 | b = bearer_get(net, bearer_id); |
| 570 | if (unlikely(!b || !test_bit(0, &b->up))) |
| 571 | __skb_queue_purge(xmitq); |
| 572 | skb_queue_walk_safe(xmitq, skb, tmp) { |
| 573 | hdr = buf_msg(skb); |
| 574 | msg_set_non_seq(hdr, 1); |
| 575 | msg_set_mc_netid(hdr, net_id); |
| 576 | __skb_dequeue(xmitq); |
| 577 | b->media->send_msg(net, skb, b, &b->bcast_addr); |
| 578 | } |
| 579 | rcu_read_unlock(); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * tipc_l2_rcv_msg - handle incoming TIPC message from an interface |
| 584 | * @buf: the received packet |
| 585 | * @dev: the net device that the packet was received on |
| 586 | * @pt: the packet_type structure which was used to register this handler |
| 587 | * @orig_dev: the original receive net device in case the device is a bond |
| 588 | * |
| 589 | * Accept only packets explicitly sent to this node, or broadcast packets; |
| 590 | * ignores packets sent using interface multicast, and traffic sent to other |
| 591 | * nodes (which can happen if interface is running in promiscuous mode). |
| 592 | */ |
| 593 | static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev, |
| 594 | struct packet_type *pt, struct net_device *orig_dev) |
| 595 | { |
| 596 | struct tipc_bearer *b; |
| 597 | |
| 598 | rcu_read_lock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 599 | b = rcu_dereference(dev->tipc_ptr) ?: |
| 600 | rcu_dereference(orig_dev->tipc_ptr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 601 | if (likely(b && test_bit(0, &b->up) && |
| 602 | (skb->pkt_type <= PACKET_MULTICAST))) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 603 | skb_mark_not_on_list(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 604 | tipc_rcv(dev_net(b->pt.dev), skb, b); |
| 605 | rcu_read_unlock(); |
| 606 | return NET_RX_SUCCESS; |
| 607 | } |
| 608 | rcu_read_unlock(); |
| 609 | kfree_skb(skb); |
| 610 | return NET_RX_DROP; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * tipc_l2_device_event - handle device events from network device |
| 615 | * @nb: the context of the notification |
| 616 | * @evt: the type of event |
| 617 | * @ptr: the net device that the event was on |
| 618 | * |
| 619 | * This function is called by the Ethernet driver in case of link |
| 620 | * change event. |
| 621 | */ |
| 622 | static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt, |
| 623 | void *ptr) |
| 624 | { |
| 625 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 626 | struct net *net = dev_net(dev); |
| 627 | struct tipc_bearer *b; |
| 628 | |
| 629 | b = rtnl_dereference(dev->tipc_ptr); |
| 630 | if (!b) |
| 631 | return NOTIFY_DONE; |
| 632 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 633 | trace_tipc_l2_device_event(dev, b, evt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 634 | switch (evt) { |
| 635 | case NETDEV_CHANGE: |
| 636 | if (netif_carrier_ok(dev) && netif_oper_up(dev)) { |
| 637 | test_and_set_bit_lock(0, &b->up); |
| 638 | break; |
| 639 | } |
| 640 | /* fall through */ |
| 641 | case NETDEV_GOING_DOWN: |
| 642 | clear_bit_unlock(0, &b->up); |
| 643 | tipc_reset_bearer(net, b); |
| 644 | break; |
| 645 | case NETDEV_UP: |
| 646 | test_and_set_bit_lock(0, &b->up); |
| 647 | break; |
| 648 | case NETDEV_CHANGEMTU: |
| 649 | if (tipc_mtu_bad(dev, 0)) { |
| 650 | bearer_disable(net, b); |
| 651 | break; |
| 652 | } |
| 653 | b->mtu = dev->mtu; |
| 654 | tipc_reset_bearer(net, b); |
| 655 | break; |
| 656 | case NETDEV_CHANGEADDR: |
| 657 | b->media->raw2addr(b, &b->addr, |
| 658 | (char *)dev->dev_addr); |
| 659 | tipc_reset_bearer(net, b); |
| 660 | break; |
| 661 | case NETDEV_UNREGISTER: |
| 662 | case NETDEV_CHANGENAME: |
| 663 | bearer_disable(net, b); |
| 664 | break; |
| 665 | } |
| 666 | return NOTIFY_OK; |
| 667 | } |
| 668 | |
| 669 | static struct notifier_block notifier = { |
| 670 | .notifier_call = tipc_l2_device_event, |
| 671 | .priority = 0, |
| 672 | }; |
| 673 | |
| 674 | int tipc_bearer_setup(void) |
| 675 | { |
| 676 | return register_netdevice_notifier(¬ifier); |
| 677 | } |
| 678 | |
| 679 | void tipc_bearer_cleanup(void) |
| 680 | { |
| 681 | unregister_netdevice_notifier(¬ifier); |
| 682 | } |
| 683 | |
| 684 | void tipc_bearer_stop(struct net *net) |
| 685 | { |
| 686 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 687 | struct tipc_bearer *b; |
| 688 | u32 i; |
| 689 | |
| 690 | for (i = 0; i < MAX_BEARERS; i++) { |
| 691 | b = rtnl_dereference(tn->bearer_list[i]); |
| 692 | if (b) { |
| 693 | bearer_disable(net, b); |
| 694 | tn->bearer_list[i] = NULL; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 699 | void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts) |
| 700 | { |
| 701 | struct net_device *dev = net->loopback_dev; |
| 702 | struct sk_buff *skb, *_skb; |
| 703 | int exp; |
| 704 | |
| 705 | skb_queue_walk(pkts, _skb) { |
| 706 | skb = pskb_copy(_skb, GFP_ATOMIC); |
| 707 | if (!skb) |
| 708 | continue; |
| 709 | |
| 710 | exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb)); |
| 711 | if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) { |
| 712 | kfree_skb(skb); |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | skb_reset_network_header(skb); |
| 717 | dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr, |
| 718 | dev->dev_addr, skb->len); |
| 719 | skb->dev = dev; |
| 720 | skb->pkt_type = PACKET_HOST; |
| 721 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 722 | skb->protocol = eth_type_trans(skb, dev); |
| 723 | netif_rx_ni(skb); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev, |
| 728 | struct packet_type *pt, struct net_device *od) |
| 729 | { |
| 730 | consume_skb(skb); |
| 731 | return NET_RX_SUCCESS; |
| 732 | } |
| 733 | |
| 734 | int tipc_attach_loopback(struct net *net) |
| 735 | { |
| 736 | struct net_device *dev = net->loopback_dev; |
| 737 | struct tipc_net *tn = tipc_net(net); |
| 738 | |
| 739 | if (!dev) |
| 740 | return -ENODEV; |
| 741 | |
| 742 | dev_hold(dev); |
| 743 | tn->loopback_pt.dev = dev; |
| 744 | tn->loopback_pt.type = htons(ETH_P_TIPC); |
| 745 | tn->loopback_pt.func = tipc_loopback_rcv_pkt; |
| 746 | dev_add_pack(&tn->loopback_pt); |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | void tipc_detach_loopback(struct net *net) |
| 751 | { |
| 752 | struct tipc_net *tn = tipc_net(net); |
| 753 | |
| 754 | dev_remove_pack(&tn->loopback_pt); |
| 755 | dev_put(net->loopback_dev); |
| 756 | } |
| 757 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 758 | /* Caller should hold rtnl_lock to protect the bearer */ |
| 759 | static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg, |
| 760 | struct tipc_bearer *bearer, int nlflags) |
| 761 | { |
| 762 | void *hdr; |
| 763 | struct nlattr *attrs; |
| 764 | struct nlattr *prop; |
| 765 | |
| 766 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
| 767 | nlflags, TIPC_NL_BEARER_GET); |
| 768 | if (!hdr) |
| 769 | return -EMSGSIZE; |
| 770 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 771 | attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 772 | if (!attrs) |
| 773 | goto msg_full; |
| 774 | |
| 775 | if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name)) |
| 776 | goto attr_msg_full; |
| 777 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 778 | prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 779 | if (!prop) |
| 780 | goto prop_msg_full; |
| 781 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority)) |
| 782 | goto prop_msg_full; |
| 783 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance)) |
| 784 | goto prop_msg_full; |
| 785 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window)) |
| 786 | goto prop_msg_full; |
| 787 | if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) |
| 788 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu)) |
| 789 | goto prop_msg_full; |
| 790 | |
| 791 | nla_nest_end(msg->skb, prop); |
| 792 | |
| 793 | #ifdef CONFIG_TIPC_MEDIA_UDP |
| 794 | if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) { |
| 795 | if (tipc_udp_nl_add_bearer_data(msg, bearer)) |
| 796 | goto attr_msg_full; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | nla_nest_end(msg->skb, attrs); |
| 801 | genlmsg_end(msg->skb, hdr); |
| 802 | |
| 803 | return 0; |
| 804 | |
| 805 | prop_msg_full: |
| 806 | nla_nest_cancel(msg->skb, prop); |
| 807 | attr_msg_full: |
| 808 | nla_nest_cancel(msg->skb, attrs); |
| 809 | msg_full: |
| 810 | genlmsg_cancel(msg->skb, hdr); |
| 811 | |
| 812 | return -EMSGSIZE; |
| 813 | } |
| 814 | |
| 815 | int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 816 | { |
| 817 | int err; |
| 818 | int i = cb->args[0]; |
| 819 | struct tipc_bearer *bearer; |
| 820 | struct tipc_nl_msg msg; |
| 821 | struct net *net = sock_net(skb->sk); |
| 822 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 823 | |
| 824 | if (i == MAX_BEARERS) |
| 825 | return 0; |
| 826 | |
| 827 | msg.skb = skb; |
| 828 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 829 | msg.seq = cb->nlh->nlmsg_seq; |
| 830 | |
| 831 | rtnl_lock(); |
| 832 | for (i = 0; i < MAX_BEARERS; i++) { |
| 833 | bearer = rtnl_dereference(tn->bearer_list[i]); |
| 834 | if (!bearer) |
| 835 | continue; |
| 836 | |
| 837 | err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI); |
| 838 | if (err) |
| 839 | break; |
| 840 | } |
| 841 | rtnl_unlock(); |
| 842 | |
| 843 | cb->args[0] = i; |
| 844 | return skb->len; |
| 845 | } |
| 846 | |
| 847 | int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info) |
| 848 | { |
| 849 | int err; |
| 850 | char *name; |
| 851 | struct sk_buff *rep; |
| 852 | struct tipc_bearer *bearer; |
| 853 | struct tipc_nl_msg msg; |
| 854 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 855 | struct net *net = genl_info_net(info); |
| 856 | |
| 857 | if (!info->attrs[TIPC_NLA_BEARER]) |
| 858 | return -EINVAL; |
| 859 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 860 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX, |
| 861 | info->attrs[TIPC_NLA_BEARER], |
| 862 | tipc_nl_bearer_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 863 | if (err) |
| 864 | return err; |
| 865 | |
| 866 | if (!attrs[TIPC_NLA_BEARER_NAME]) |
| 867 | return -EINVAL; |
| 868 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); |
| 869 | |
| 870 | rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 871 | if (!rep) |
| 872 | return -ENOMEM; |
| 873 | |
| 874 | msg.skb = rep; |
| 875 | msg.portid = info->snd_portid; |
| 876 | msg.seq = info->snd_seq; |
| 877 | |
| 878 | rtnl_lock(); |
| 879 | bearer = tipc_bearer_find(net, name); |
| 880 | if (!bearer) { |
| 881 | err = -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 882 | NL_SET_ERR_MSG(info->extack, "Bearer not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 883 | goto err_out; |
| 884 | } |
| 885 | |
| 886 | err = __tipc_nl_add_bearer(&msg, bearer, 0); |
| 887 | if (err) |
| 888 | goto err_out; |
| 889 | rtnl_unlock(); |
| 890 | |
| 891 | return genlmsg_reply(rep, info); |
| 892 | err_out: |
| 893 | rtnl_unlock(); |
| 894 | nlmsg_free(rep); |
| 895 | |
| 896 | return err; |
| 897 | } |
| 898 | |
| 899 | int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info) |
| 900 | { |
| 901 | int err; |
| 902 | char *name; |
| 903 | struct tipc_bearer *bearer; |
| 904 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 905 | struct net *net = sock_net(skb->sk); |
| 906 | |
| 907 | if (!info->attrs[TIPC_NLA_BEARER]) |
| 908 | return -EINVAL; |
| 909 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 910 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX, |
| 911 | info->attrs[TIPC_NLA_BEARER], |
| 912 | tipc_nl_bearer_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 913 | if (err) |
| 914 | return err; |
| 915 | |
| 916 | if (!attrs[TIPC_NLA_BEARER_NAME]) |
| 917 | return -EINVAL; |
| 918 | |
| 919 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); |
| 920 | |
| 921 | bearer = tipc_bearer_find(net, name); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 922 | if (!bearer) { |
| 923 | NL_SET_ERR_MSG(info->extack, "Bearer not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 924 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 925 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 926 | |
| 927 | bearer_disable(net, bearer); |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info) |
| 933 | { |
| 934 | int err; |
| 935 | |
| 936 | rtnl_lock(); |
| 937 | err = __tipc_nl_bearer_disable(skb, info); |
| 938 | rtnl_unlock(); |
| 939 | |
| 940 | return err; |
| 941 | } |
| 942 | |
| 943 | int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info) |
| 944 | { |
| 945 | int err; |
| 946 | char *bearer; |
| 947 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 948 | struct net *net = sock_net(skb->sk); |
| 949 | u32 domain = 0; |
| 950 | u32 prio; |
| 951 | |
| 952 | prio = TIPC_MEDIA_LINK_PRI; |
| 953 | |
| 954 | if (!info->attrs[TIPC_NLA_BEARER]) |
| 955 | return -EINVAL; |
| 956 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 957 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX, |
| 958 | info->attrs[TIPC_NLA_BEARER], |
| 959 | tipc_nl_bearer_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 960 | if (err) |
| 961 | return err; |
| 962 | |
| 963 | if (!attrs[TIPC_NLA_BEARER_NAME]) |
| 964 | return -EINVAL; |
| 965 | |
| 966 | bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]); |
| 967 | |
| 968 | if (attrs[TIPC_NLA_BEARER_DOMAIN]) |
| 969 | domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]); |
| 970 | |
| 971 | if (attrs[TIPC_NLA_BEARER_PROP]) { |
| 972 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; |
| 973 | |
| 974 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], |
| 975 | props); |
| 976 | if (err) |
| 977 | return err; |
| 978 | |
| 979 | if (props[TIPC_NLA_PROP_PRIO]) |
| 980 | prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); |
| 981 | } |
| 982 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 983 | return tipc_enable_bearer(net, bearer, domain, prio, attrs, |
| 984 | info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info) |
| 988 | { |
| 989 | int err; |
| 990 | |
| 991 | rtnl_lock(); |
| 992 | err = __tipc_nl_bearer_enable(skb, info); |
| 993 | rtnl_unlock(); |
| 994 | |
| 995 | return err; |
| 996 | } |
| 997 | |
| 998 | int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info) |
| 999 | { |
| 1000 | int err; |
| 1001 | char *name; |
| 1002 | struct tipc_bearer *b; |
| 1003 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 1004 | struct net *net = sock_net(skb->sk); |
| 1005 | |
| 1006 | if (!info->attrs[TIPC_NLA_BEARER]) |
| 1007 | return -EINVAL; |
| 1008 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1009 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX, |
| 1010 | info->attrs[TIPC_NLA_BEARER], |
| 1011 | tipc_nl_bearer_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1012 | if (err) |
| 1013 | return err; |
| 1014 | |
| 1015 | if (!attrs[TIPC_NLA_BEARER_NAME]) |
| 1016 | return -EINVAL; |
| 1017 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); |
| 1018 | |
| 1019 | rtnl_lock(); |
| 1020 | b = tipc_bearer_find(net, name); |
| 1021 | if (!b) { |
| 1022 | rtnl_unlock(); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1023 | NL_SET_ERR_MSG(info->extack, "Bearer not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | |
| 1027 | #ifdef CONFIG_TIPC_MEDIA_UDP |
| 1028 | if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) { |
| 1029 | err = tipc_udp_nl_bearer_add(b, |
| 1030 | attrs[TIPC_NLA_BEARER_UDP_OPTS]); |
| 1031 | if (err) { |
| 1032 | rtnl_unlock(); |
| 1033 | return err; |
| 1034 | } |
| 1035 | } |
| 1036 | #endif |
| 1037 | rtnl_unlock(); |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info) |
| 1043 | { |
| 1044 | struct tipc_bearer *b; |
| 1045 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 1046 | struct net *net = sock_net(skb->sk); |
| 1047 | char *name; |
| 1048 | int err; |
| 1049 | |
| 1050 | if (!info->attrs[TIPC_NLA_BEARER]) |
| 1051 | return -EINVAL; |
| 1052 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1053 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX, |
| 1054 | info->attrs[TIPC_NLA_BEARER], |
| 1055 | tipc_nl_bearer_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1056 | if (err) |
| 1057 | return err; |
| 1058 | |
| 1059 | if (!attrs[TIPC_NLA_BEARER_NAME]) |
| 1060 | return -EINVAL; |
| 1061 | name = nla_data(attrs[TIPC_NLA_BEARER_NAME]); |
| 1062 | |
| 1063 | b = tipc_bearer_find(net, name); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1064 | if (!b) { |
| 1065 | NL_SET_ERR_MSG(info->extack, "Bearer not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1066 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1067 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1068 | |
| 1069 | if (attrs[TIPC_NLA_BEARER_PROP]) { |
| 1070 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; |
| 1071 | |
| 1072 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP], |
| 1073 | props); |
| 1074 | if (err) |
| 1075 | return err; |
| 1076 | |
| 1077 | if (props[TIPC_NLA_PROP_TOL]) { |
| 1078 | b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); |
| 1079 | tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL); |
| 1080 | } |
| 1081 | if (props[TIPC_NLA_PROP_PRIO]) |
| 1082 | b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); |
| 1083 | if (props[TIPC_NLA_PROP_WIN]) |
| 1084 | b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); |
| 1085 | if (props[TIPC_NLA_PROP_MTU]) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1086 | if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) { |
| 1087 | NL_SET_ERR_MSG(info->extack, |
| 1088 | "MTU property is unsupported"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1089 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1090 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1091 | #ifdef CONFIG_TIPC_MEDIA_UDP |
| 1092 | if (tipc_udp_mtu_bad(nla_get_u32 |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1093 | (props[TIPC_NLA_PROP_MTU]))) { |
| 1094 | NL_SET_ERR_MSG(info->extack, |
| 1095 | "MTU value is out-of-range"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1096 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1097 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1098 | b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]); |
| 1099 | tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU); |
| 1100 | #endif |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info) |
| 1108 | { |
| 1109 | int err; |
| 1110 | |
| 1111 | rtnl_lock(); |
| 1112 | err = __tipc_nl_bearer_set(skb, info); |
| 1113 | rtnl_unlock(); |
| 1114 | |
| 1115 | return err; |
| 1116 | } |
| 1117 | |
| 1118 | static int __tipc_nl_add_media(struct tipc_nl_msg *msg, |
| 1119 | struct tipc_media *media, int nlflags) |
| 1120 | { |
| 1121 | void *hdr; |
| 1122 | struct nlattr *attrs; |
| 1123 | struct nlattr *prop; |
| 1124 | |
| 1125 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family, |
| 1126 | nlflags, TIPC_NL_MEDIA_GET); |
| 1127 | if (!hdr) |
| 1128 | return -EMSGSIZE; |
| 1129 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1130 | attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1131 | if (!attrs) |
| 1132 | goto msg_full; |
| 1133 | |
| 1134 | if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name)) |
| 1135 | goto attr_msg_full; |
| 1136 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1137 | prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1138 | if (!prop) |
| 1139 | goto prop_msg_full; |
| 1140 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority)) |
| 1141 | goto prop_msg_full; |
| 1142 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance)) |
| 1143 | goto prop_msg_full; |
| 1144 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window)) |
| 1145 | goto prop_msg_full; |
| 1146 | if (media->type_id == TIPC_MEDIA_TYPE_UDP) |
| 1147 | if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu)) |
| 1148 | goto prop_msg_full; |
| 1149 | |
| 1150 | nla_nest_end(msg->skb, prop); |
| 1151 | nla_nest_end(msg->skb, attrs); |
| 1152 | genlmsg_end(msg->skb, hdr); |
| 1153 | |
| 1154 | return 0; |
| 1155 | |
| 1156 | prop_msg_full: |
| 1157 | nla_nest_cancel(msg->skb, prop); |
| 1158 | attr_msg_full: |
| 1159 | nla_nest_cancel(msg->skb, attrs); |
| 1160 | msg_full: |
| 1161 | genlmsg_cancel(msg->skb, hdr); |
| 1162 | |
| 1163 | return -EMSGSIZE; |
| 1164 | } |
| 1165 | |
| 1166 | int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1167 | { |
| 1168 | int err; |
| 1169 | int i = cb->args[0]; |
| 1170 | struct tipc_nl_msg msg; |
| 1171 | |
| 1172 | if (i == MAX_MEDIA) |
| 1173 | return 0; |
| 1174 | |
| 1175 | msg.skb = skb; |
| 1176 | msg.portid = NETLINK_CB(cb->skb).portid; |
| 1177 | msg.seq = cb->nlh->nlmsg_seq; |
| 1178 | |
| 1179 | rtnl_lock(); |
| 1180 | for (; media_info_array[i] != NULL; i++) { |
| 1181 | err = __tipc_nl_add_media(&msg, media_info_array[i], |
| 1182 | NLM_F_MULTI); |
| 1183 | if (err) |
| 1184 | break; |
| 1185 | } |
| 1186 | rtnl_unlock(); |
| 1187 | |
| 1188 | cb->args[0] = i; |
| 1189 | return skb->len; |
| 1190 | } |
| 1191 | |
| 1192 | int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info) |
| 1193 | { |
| 1194 | int err; |
| 1195 | char *name; |
| 1196 | struct tipc_nl_msg msg; |
| 1197 | struct tipc_media *media; |
| 1198 | struct sk_buff *rep; |
| 1199 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 1200 | |
| 1201 | if (!info->attrs[TIPC_NLA_MEDIA]) |
| 1202 | return -EINVAL; |
| 1203 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1204 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX, |
| 1205 | info->attrs[TIPC_NLA_MEDIA], |
| 1206 | tipc_nl_media_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1207 | if (err) |
| 1208 | return err; |
| 1209 | |
| 1210 | if (!attrs[TIPC_NLA_MEDIA_NAME]) |
| 1211 | return -EINVAL; |
| 1212 | name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); |
| 1213 | |
| 1214 | rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1215 | if (!rep) |
| 1216 | return -ENOMEM; |
| 1217 | |
| 1218 | msg.skb = rep; |
| 1219 | msg.portid = info->snd_portid; |
| 1220 | msg.seq = info->snd_seq; |
| 1221 | |
| 1222 | rtnl_lock(); |
| 1223 | media = tipc_media_find(name); |
| 1224 | if (!media) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1225 | NL_SET_ERR_MSG(info->extack, "Media not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1226 | err = -EINVAL; |
| 1227 | goto err_out; |
| 1228 | } |
| 1229 | |
| 1230 | err = __tipc_nl_add_media(&msg, media, 0); |
| 1231 | if (err) |
| 1232 | goto err_out; |
| 1233 | rtnl_unlock(); |
| 1234 | |
| 1235 | return genlmsg_reply(rep, info); |
| 1236 | err_out: |
| 1237 | rtnl_unlock(); |
| 1238 | nlmsg_free(rep); |
| 1239 | |
| 1240 | return err; |
| 1241 | } |
| 1242 | |
| 1243 | int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info) |
| 1244 | { |
| 1245 | int err; |
| 1246 | char *name; |
| 1247 | struct tipc_media *m; |
| 1248 | struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1]; |
| 1249 | |
| 1250 | if (!info->attrs[TIPC_NLA_MEDIA]) |
| 1251 | return -EINVAL; |
| 1252 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1253 | err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX, |
| 1254 | info->attrs[TIPC_NLA_MEDIA], |
| 1255 | tipc_nl_media_policy, info->extack); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (!attrs[TIPC_NLA_MEDIA_NAME]) |
| 1258 | return -EINVAL; |
| 1259 | name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]); |
| 1260 | |
| 1261 | m = tipc_media_find(name); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1262 | if (!m) { |
| 1263 | NL_SET_ERR_MSG(info->extack, "Media not found"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1264 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1265 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1266 | if (attrs[TIPC_NLA_MEDIA_PROP]) { |
| 1267 | struct nlattr *props[TIPC_NLA_PROP_MAX + 1]; |
| 1268 | |
| 1269 | err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP], |
| 1270 | props); |
| 1271 | if (err) |
| 1272 | return err; |
| 1273 | |
| 1274 | if (props[TIPC_NLA_PROP_TOL]) |
| 1275 | m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]); |
| 1276 | if (props[TIPC_NLA_PROP_PRIO]) |
| 1277 | m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); |
| 1278 | if (props[TIPC_NLA_PROP_WIN]) |
| 1279 | m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]); |
| 1280 | if (props[TIPC_NLA_PROP_MTU]) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1281 | if (m->type_id != TIPC_MEDIA_TYPE_UDP) { |
| 1282 | NL_SET_ERR_MSG(info->extack, |
| 1283 | "MTU property is unsupported"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1284 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1285 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1286 | #ifdef CONFIG_TIPC_MEDIA_UDP |
| 1287 | if (tipc_udp_mtu_bad(nla_get_u32 |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1288 | (props[TIPC_NLA_PROP_MTU]))) { |
| 1289 | NL_SET_ERR_MSG(info->extack, |
| 1290 | "MTU value is out-of-range"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1291 | return -EINVAL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1292 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1293 | m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]); |
| 1294 | #endif |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info) |
| 1302 | { |
| 1303 | int err; |
| 1304 | |
| 1305 | rtnl_lock(); |
| 1306 | err = __tipc_nl_media_set(skb, info); |
| 1307 | rtnl_unlock(); |
| 1308 | |
| 1309 | return err; |
| 1310 | } |