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