Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, Ericsson AB |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the names of the copyright holders nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * Alternatively, this software may be distributed under the terms of the |
| 18 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 19 | * Software Foundation. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include "core.h" |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 35 | #include "bearer.h" |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 36 | #include "link.h" |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 37 | #include "name_table.h" |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 38 | #include "socket.h" |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 39 | #include "node.h" |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 40 | #include "net.h" |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 41 | #include <net/genetlink.h> |
| 42 | #include <linux/tipc_config.h> |
| 43 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 44 | /* The legacy API had an artificial message length limit called |
| 45 | * ULTRA_STRING_MAX_LEN. |
| 46 | */ |
| 47 | #define ULTRA_STRING_MAX_LEN 32768 |
| 48 | |
| 49 | #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN) |
| 50 | |
| 51 | #define REPLY_TRUNCATED "<truncated>\n" |
| 52 | |
| 53 | struct tipc_nl_compat_msg { |
| 54 | u16 cmd; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 55 | int rep_type; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 56 | int rep_size; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 57 | int req_type; |
Taras Kondratiuk | 4da5f00 | 2019-07-29 22:15:07 +0000 | [diff] [blame] | 58 | int req_size; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 59 | struct net *net; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 60 | struct sk_buff *rep; |
| 61 | struct tlv_desc *req; |
| 62 | struct sock *dst_sk; |
| 63 | }; |
| 64 | |
| 65 | struct tipc_nl_compat_cmd_dump { |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 66 | int (*header)(struct tipc_nl_compat_msg *); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 67 | int (*dumpit)(struct sk_buff *, struct netlink_callback *); |
| 68 | int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs); |
| 69 | }; |
| 70 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 71 | struct tipc_nl_compat_cmd_doit { |
| 72 | int (*doit)(struct sk_buff *skb, struct genl_info *info); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 73 | int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd, |
| 74 | struct sk_buff *skb, struct tipc_nl_compat_msg *msg); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 77 | static int tipc_skb_tailroom(struct sk_buff *skb) |
| 78 | { |
| 79 | int tailroom; |
| 80 | int limit; |
| 81 | |
| 82 | tailroom = skb_tailroom(skb); |
| 83 | limit = TIPC_SKB_MAX - skb->len; |
| 84 | |
| 85 | if (tailroom < limit) |
| 86 | return tailroom; |
| 87 | |
| 88 | return limit; |
| 89 | } |
| 90 | |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 91 | static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv) |
| 92 | { |
| 93 | return TLV_GET_LEN(tlv) - TLV_SPACE(0); |
| 94 | } |
| 95 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 96 | static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len) |
| 97 | { |
| 98 | struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb); |
| 99 | |
| 100 | if (tipc_skb_tailroom(skb) < TLV_SPACE(len)) |
| 101 | return -EMSGSIZE; |
| 102 | |
| 103 | skb_put(skb, TLV_SPACE(len)); |
| 104 | tlv->tlv_type = htons(type); |
| 105 | tlv->tlv_len = htons(TLV_LENGTH(len)); |
| 106 | if (len && data) |
| 107 | memcpy(TLV_DATA(tlv), data, len); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 112 | static void tipc_tlv_init(struct sk_buff *skb, u16 type) |
| 113 | { |
| 114 | struct tlv_desc *tlv = (struct tlv_desc *)skb->data; |
| 115 | |
| 116 | TLV_SET_LEN(tlv, 0); |
| 117 | TLV_SET_TYPE(tlv, type); |
| 118 | skb_put(skb, sizeof(struct tlv_desc)); |
| 119 | } |
| 120 | |
Andrew Lunn | 79b1119 | 2020-10-28 01:38:49 +0100 | [diff] [blame^] | 121 | static __printf(2, 3) int tipc_tlv_sprintf(struct sk_buff *skb, |
| 122 | const char *fmt, ...) |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 123 | { |
| 124 | int n; |
| 125 | u16 len; |
| 126 | u32 rem; |
| 127 | char *buf; |
| 128 | struct tlv_desc *tlv; |
| 129 | va_list args; |
| 130 | |
| 131 | rem = tipc_skb_tailroom(skb); |
| 132 | |
| 133 | tlv = (struct tlv_desc *)skb->data; |
| 134 | len = TLV_GET_LEN(tlv); |
| 135 | buf = TLV_DATA(tlv) + len; |
| 136 | |
| 137 | va_start(args, fmt); |
| 138 | n = vscnprintf(buf, rem, fmt, args); |
| 139 | va_end(args); |
| 140 | |
| 141 | TLV_SET_LEN(tlv, n + len); |
| 142 | skb_put(skb, n); |
| 143 | |
| 144 | return n; |
| 145 | } |
| 146 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 147 | static struct sk_buff *tipc_tlv_alloc(int size) |
| 148 | { |
| 149 | int hdr_len; |
| 150 | struct sk_buff *buf; |
| 151 | |
| 152 | size = TLV_SPACE(size); |
| 153 | hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); |
| 154 | |
| 155 | buf = alloc_skb(hdr_len + size, GFP_KERNEL); |
| 156 | if (!buf) |
| 157 | return NULL; |
| 158 | |
| 159 | skb_reserve(buf, hdr_len); |
| 160 | |
| 161 | return buf; |
| 162 | } |
| 163 | |
| 164 | static struct sk_buff *tipc_get_err_tlv(char *str) |
| 165 | { |
| 166 | int str_len = strlen(str) + 1; |
| 167 | struct sk_buff *buf; |
| 168 | |
| 169 | buf = tipc_tlv_alloc(TLV_SPACE(str_len)); |
| 170 | if (buf) |
| 171 | tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len); |
| 172 | |
| 173 | return buf; |
| 174 | } |
| 175 | |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 176 | static inline bool string_is_valid(char *s, int len) |
| 177 | { |
| 178 | return memchr(s, '\0', len) ? true : false; |
| 179 | } |
| 180 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 181 | static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, |
| 182 | struct tipc_nl_compat_msg *msg, |
| 183 | struct sk_buff *arg) |
| 184 | { |
Jiri Pirko | 6ea6776 | 2019-10-08 13:01:51 +0200 | [diff] [blame] | 185 | struct genl_dumpit_info info; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 186 | int len = 0; |
| 187 | int err; |
| 188 | struct sk_buff *buf; |
| 189 | struct nlmsghdr *nlmsg; |
| 190 | struct netlink_callback cb; |
Jiri Pirko | c6c0861 | 2019-10-05 20:04:40 +0200 | [diff] [blame] | 191 | struct nlattr **attrbuf; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 192 | |
| 193 | memset(&cb, 0, sizeof(cb)); |
| 194 | cb.nlh = (struct nlmsghdr *)arg->data; |
| 195 | cb.skb = arg; |
Jiri Pirko | 6ea6776 | 2019-10-08 13:01:51 +0200 | [diff] [blame] | 196 | cb.data = &info; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 197 | |
| 198 | buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 199 | if (!buf) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | buf->sk = msg->dst_sk; |
Cong Wang | 12a78b0 | 2018-09-11 15:12:17 -0700 | [diff] [blame] | 203 | if (__tipc_dump_start(&cb, msg->net)) { |
| 204 | kfree_skb(buf); |
| 205 | return -ENOMEM; |
| 206 | } |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 207 | |
Ying Xue | a7869e5 | 2020-01-04 10:48:36 +0800 | [diff] [blame] | 208 | attrbuf = kcalloc(tipc_genl_family.maxattr + 1, |
| 209 | sizeof(struct nlattr *), GFP_KERNEL); |
Jiri Pirko | c6c0861 | 2019-10-05 20:04:40 +0200 | [diff] [blame] | 210 | if (!attrbuf) { |
| 211 | err = -ENOMEM; |
| 212 | goto err_out; |
| 213 | } |
| 214 | |
Jiri Pirko | 6ea6776 | 2019-10-08 13:01:51 +0200 | [diff] [blame] | 215 | info.attrs = attrbuf; |
| 216 | err = nlmsg_parse_deprecated(cb.nlh, GENL_HDRLEN, attrbuf, |
| 217 | tipc_genl_family.maxattr, |
| 218 | tipc_genl_family.policy, NULL); |
| 219 | if (err) |
| 220 | goto err_out; |
| 221 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 222 | do { |
| 223 | int rem; |
| 224 | |
| 225 | len = (*cmd->dumpit)(buf, &cb); |
| 226 | |
| 227 | nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) { |
Jiri Pirko | c6c0861 | 2019-10-05 20:04:40 +0200 | [diff] [blame] | 228 | err = nlmsg_parse_deprecated(nlmsg, GENL_HDRLEN, |
| 229 | attrbuf, |
| 230 | tipc_genl_family.maxattr, |
| 231 | tipc_genl_family.policy, |
| 232 | NULL); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 233 | if (err) |
| 234 | goto err_out; |
| 235 | |
Jiri Pirko | c6c0861 | 2019-10-05 20:04:40 +0200 | [diff] [blame] | 236 | err = (*cmd->format)(msg, attrbuf); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 237 | if (err) |
| 238 | goto err_out; |
| 239 | |
| 240 | if (tipc_skb_tailroom(msg->rep) <= 1) { |
| 241 | err = -EMSGSIZE; |
| 242 | goto err_out; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | skb_reset_tail_pointer(buf); |
| 247 | buf->len = 0; |
| 248 | |
| 249 | } while (len); |
| 250 | |
| 251 | err = 0; |
| 252 | |
| 253 | err_out: |
Jiri Pirko | c6c0861 | 2019-10-05 20:04:40 +0200 | [diff] [blame] | 254 | kfree(attrbuf); |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 255 | tipc_dump_done(&cb); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 256 | kfree_skb(buf); |
| 257 | |
| 258 | if (err == -EMSGSIZE) { |
| 259 | /* The legacy API only considered messages filling |
| 260 | * "ULTRA_STRING_MAX_LEN" to be truncated. |
| 261 | */ |
| 262 | if ((TIPC_SKB_MAX - msg->rep->len) <= 1) { |
| 263 | char *tail = skb_tail_pointer(msg->rep); |
| 264 | |
| 265 | if (*tail != '\0') |
| 266 | sprintf(tail - sizeof(REPLY_TRUNCATED) - 1, |
| 267 | REPLY_TRUNCATED); |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, |
| 277 | struct tipc_nl_compat_msg *msg) |
| 278 | { |
Cong Wang | 47733f9 | 2020-08-15 16:29:15 -0700 | [diff] [blame] | 279 | struct nlmsghdr *nlh; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 280 | struct sk_buff *arg; |
Cong Wang | 47733f9 | 2020-08-15 16:29:15 -0700 | [diff] [blame] | 281 | int err; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 282 | |
Taras Kondratiuk | 4da5f00 | 2019-07-29 22:15:07 +0000 | [diff] [blame] | 283 | if (msg->req_type && (!msg->req_size || |
| 284 | !TLV_CHECK_TYPE(msg->req, msg->req_type))) |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 287 | msg->rep = tipc_tlv_alloc(msg->rep_size); |
| 288 | if (!msg->rep) |
| 289 | return -ENOMEM; |
| 290 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 291 | if (msg->rep_type) |
| 292 | tipc_tlv_init(msg->rep, msg->rep_type); |
| 293 | |
Xin Long | 2ac695d1 | 2019-03-31 22:50:10 +0800 | [diff] [blame] | 294 | if (cmd->header) { |
| 295 | err = (*cmd->header)(msg); |
| 296 | if (err) { |
| 297 | kfree_skb(msg->rep); |
| 298 | msg->rep = NULL; |
| 299 | return err; |
| 300 | } |
| 301 | } |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 302 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 303 | arg = nlmsg_new(0, GFP_KERNEL); |
| 304 | if (!arg) { |
| 305 | kfree_skb(msg->rep); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 306 | msg->rep = NULL; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 307 | return -ENOMEM; |
| 308 | } |
| 309 | |
Cong Wang | 47733f9 | 2020-08-15 16:29:15 -0700 | [diff] [blame] | 310 | nlh = nlmsg_put(arg, 0, 0, tipc_genl_family.id, 0, NLM_F_MULTI); |
| 311 | if (!nlh) { |
| 312 | kfree_skb(arg); |
| 313 | kfree_skb(msg->rep); |
| 314 | msg->rep = NULL; |
| 315 | return -EMSGSIZE; |
| 316 | } |
| 317 | nlmsg_end(arg, nlh); |
| 318 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 319 | err = __tipc_nl_compat_dumpit(cmd, msg, arg); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 320 | if (err) { |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 321 | kfree_skb(msg->rep); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 322 | msg->rep = NULL; |
| 323 | } |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 324 | kfree_skb(arg); |
| 325 | |
| 326 | return err; |
| 327 | } |
| 328 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 329 | static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd, |
| 330 | struct tipc_nl_compat_msg *msg) |
| 331 | { |
| 332 | int err; |
| 333 | struct sk_buff *doit_buf; |
| 334 | struct sk_buff *trans_buf; |
| 335 | struct nlattr **attrbuf; |
| 336 | struct genl_info info; |
| 337 | |
| 338 | trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 339 | if (!trans_buf) |
| 340 | return -ENOMEM; |
| 341 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 342 | attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1, |
| 343 | sizeof(struct nlattr *), |
| 344 | GFP_KERNEL); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 345 | if (!attrbuf) { |
| 346 | err = -ENOMEM; |
| 347 | goto trans_out; |
| 348 | } |
| 349 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 350 | doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 351 | if (!doit_buf) { |
| 352 | err = -ENOMEM; |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 353 | goto attrbuf_out; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 356 | memset(&info, 0, sizeof(info)); |
| 357 | info.attrs = attrbuf; |
| 358 | |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 359 | rtnl_lock(); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 360 | err = (*cmd->transcode)(cmd, trans_buf, msg); |
| 361 | if (err) |
| 362 | goto doit_out; |
| 363 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 364 | err = nla_parse_deprecated(attrbuf, tipc_genl_family.maxattr, |
| 365 | (const struct nlattr *)trans_buf->data, |
| 366 | trans_buf->len, NULL, NULL); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 367 | if (err) |
| 368 | goto doit_out; |
| 369 | |
| 370 | doit_buf->sk = msg->dst_sk; |
| 371 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 372 | err = (*cmd->doit)(doit_buf, &info); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 373 | doit_out: |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 374 | rtnl_unlock(); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 375 | |
| 376 | kfree_skb(doit_buf); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 377 | attrbuf_out: |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 378 | kfree(attrbuf); |
| 379 | trans_out: |
| 380 | kfree_skb(trans_buf); |
| 381 | |
| 382 | return err; |
| 383 | } |
| 384 | |
| 385 | static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd, |
| 386 | struct tipc_nl_compat_msg *msg) |
| 387 | { |
| 388 | int err; |
| 389 | |
Taras Kondratiuk | 4da5f00 | 2019-07-29 22:15:07 +0000 | [diff] [blame] | 390 | if (msg->req_type && (!msg->req_size || |
| 391 | !TLV_CHECK_TYPE(msg->req, msg->req_type))) |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 392 | return -EINVAL; |
| 393 | |
| 394 | err = __tipc_nl_compat_doit(cmd, msg); |
| 395 | if (err) |
| 396 | return err; |
| 397 | |
| 398 | /* The legacy API considered an empty message a success message */ |
| 399 | msg->rep = tipc_tlv_alloc(0); |
| 400 | if (!msg->rep) |
| 401 | return -ENOMEM; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 406 | static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg, |
| 407 | struct nlattr **attrs) |
| 408 | { |
| 409 | struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 410 | int err; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 411 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 412 | if (!attrs[TIPC_NLA_BEARER]) |
| 413 | return -EINVAL; |
| 414 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 415 | err = nla_parse_nested_deprecated(bearer, TIPC_NLA_BEARER_MAX, |
| 416 | attrs[TIPC_NLA_BEARER], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 417 | if (err) |
| 418 | return err; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 419 | |
| 420 | return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME, |
| 421 | nla_data(bearer[TIPC_NLA_BEARER_NAME]), |
| 422 | nla_len(bearer[TIPC_NLA_BEARER_NAME])); |
| 423 | } |
| 424 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 425 | static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd, |
| 426 | struct sk_buff *skb, |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 427 | struct tipc_nl_compat_msg *msg) |
| 428 | { |
| 429 | struct nlattr *prop; |
| 430 | struct nlattr *bearer; |
| 431 | struct tipc_bearer_config *b; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 432 | int len; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 433 | |
| 434 | b = (struct tipc_bearer_config *)TLV_DATA(msg->req); |
| 435 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 436 | bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 437 | if (!bearer) |
| 438 | return -EMSGSIZE; |
| 439 | |
Xin Long | 6f07e5f | 2019-03-31 22:50:08 +0800 | [diff] [blame] | 440 | len = TLV_GET_DATA_LEN(msg->req); |
| 441 | len -= offsetof(struct tipc_bearer_config, name); |
| 442 | if (len <= 0) |
| 443 | return -EINVAL; |
| 444 | |
| 445 | len = min_t(int, len, TIPC_MAX_BEARER_NAME); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 446 | if (!string_is_valid(b->name, len)) |
| 447 | return -EINVAL; |
| 448 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 449 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name)) |
| 450 | return -EMSGSIZE; |
| 451 | |
| 452 | if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain))) |
| 453 | return -EMSGSIZE; |
| 454 | |
| 455 | if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 456 | prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 457 | if (!prop) |
| 458 | return -EMSGSIZE; |
| 459 | if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority))) |
| 460 | return -EMSGSIZE; |
| 461 | nla_nest_end(skb, prop); |
| 462 | } |
| 463 | nla_nest_end(skb, bearer); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 468 | static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd, |
| 469 | struct sk_buff *skb, |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 470 | struct tipc_nl_compat_msg *msg) |
| 471 | { |
| 472 | char *name; |
| 473 | struct nlattr *bearer; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 474 | int len; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 475 | |
| 476 | name = (char *)TLV_DATA(msg->req); |
| 477 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 478 | bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 479 | if (!bearer) |
| 480 | return -EMSGSIZE; |
| 481 | |
Xin Long | 4f07b80 | 2019-06-25 00:28:19 +0800 | [diff] [blame] | 482 | len = TLV_GET_DATA_LEN(msg->req); |
| 483 | if (len <= 0) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | len = min_t(int, len, TIPC_MAX_BEARER_NAME); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 487 | if (!string_is_valid(name, len)) |
| 488 | return -EINVAL; |
| 489 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 490 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name)) |
| 491 | return -EMSGSIZE; |
| 492 | |
| 493 | nla_nest_end(skb, bearer); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 498 | static inline u32 perc(u32 count, u32 total) |
| 499 | { |
| 500 | return (count * 100 + (total / 2)) / total; |
| 501 | } |
| 502 | |
| 503 | static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg, |
| 504 | struct nlattr *prop[], struct nlattr *stats[]) |
| 505 | { |
| 506 | tipc_tlv_sprintf(msg->rep, " Window:%u packets\n", |
| 507 | nla_get_u32(prop[TIPC_NLA_PROP_WIN])); |
| 508 | |
| 509 | tipc_tlv_sprintf(msg->rep, |
| 510 | " RX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 511 | nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]), |
| 512 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]), |
| 513 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]), |
| 514 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]), |
| 515 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED])); |
| 516 | |
| 517 | tipc_tlv_sprintf(msg->rep, |
| 518 | " TX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 519 | nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]), |
| 520 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]), |
| 521 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]), |
| 522 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]), |
| 523 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED])); |
| 524 | |
| 525 | tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n", |
| 526 | nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]), |
| 527 | nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]), |
| 528 | nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES])); |
| 529 | |
| 530 | tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n", |
| 531 | nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]), |
| 532 | nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]), |
| 533 | nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED])); |
| 534 | |
| 535 | tipc_tlv_sprintf(msg->rep, |
| 536 | " Congestion link:%u Send queue max:%u avg:%u", |
| 537 | nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]), |
| 538 | nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]), |
| 539 | nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE])); |
| 540 | } |
| 541 | |
| 542 | static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg, |
| 543 | struct nlattr **attrs) |
| 544 | { |
| 545 | char *name; |
| 546 | struct nlattr *link[TIPC_NLA_LINK_MAX + 1]; |
| 547 | struct nlattr *prop[TIPC_NLA_PROP_MAX + 1]; |
| 548 | struct nlattr *stats[TIPC_NLA_STATS_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 549 | int err; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 550 | int len; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 551 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 552 | if (!attrs[TIPC_NLA_LINK]) |
| 553 | return -EINVAL; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 554 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 555 | err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX, |
| 556 | attrs[TIPC_NLA_LINK], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 557 | if (err) |
| 558 | return err; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 559 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 560 | if (!link[TIPC_NLA_LINK_PROP]) |
| 561 | return -EINVAL; |
| 562 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 563 | err = nla_parse_nested_deprecated(prop, TIPC_NLA_PROP_MAX, |
| 564 | link[TIPC_NLA_LINK_PROP], NULL, |
| 565 | NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 566 | if (err) |
| 567 | return err; |
| 568 | |
| 569 | if (!link[TIPC_NLA_LINK_STATS]) |
| 570 | return -EINVAL; |
| 571 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 572 | err = nla_parse_nested_deprecated(stats, TIPC_NLA_STATS_MAX, |
| 573 | link[TIPC_NLA_LINK_STATS], NULL, |
| 574 | NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 575 | if (err) |
| 576 | return err; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 577 | |
| 578 | name = (char *)TLV_DATA(msg->req); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 579 | |
Xin Long | 4f07b80 | 2019-06-25 00:28:19 +0800 | [diff] [blame] | 580 | len = TLV_GET_DATA_LEN(msg->req); |
| 581 | if (len <= 0) |
| 582 | return -EINVAL; |
| 583 | |
John Rutherford | fd567ac | 2019-11-26 13:52:55 +1100 | [diff] [blame] | 584 | len = min_t(int, len, TIPC_MAX_LINK_NAME); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 585 | if (!string_is_valid(name, len)) |
| 586 | return -EINVAL; |
| 587 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 588 | if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0) |
| 589 | return 0; |
| 590 | |
| 591 | tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n", |
Andrew Lunn | b3b7e64 | 2020-10-28 01:43:33 +0100 | [diff] [blame] | 592 | (char *)nla_data(link[TIPC_NLA_LINK_NAME])); |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 593 | |
| 594 | if (link[TIPC_NLA_LINK_BROADCAST]) { |
| 595 | __fill_bc_link_stat(msg, prop, stats); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | if (link[TIPC_NLA_LINK_ACTIVE]) |
| 600 | tipc_tlv_sprintf(msg->rep, " ACTIVE"); |
| 601 | else if (link[TIPC_NLA_LINK_UP]) |
| 602 | tipc_tlv_sprintf(msg->rep, " STANDBY"); |
| 603 | else |
| 604 | tipc_tlv_sprintf(msg->rep, " DEFUNCT"); |
| 605 | |
| 606 | tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u", |
| 607 | nla_get_u32(link[TIPC_NLA_LINK_MTU]), |
| 608 | nla_get_u32(prop[TIPC_NLA_PROP_PRIO])); |
| 609 | |
| 610 | tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n", |
| 611 | nla_get_u32(prop[TIPC_NLA_PROP_TOL]), |
| 612 | nla_get_u32(prop[TIPC_NLA_PROP_WIN])); |
| 613 | |
| 614 | tipc_tlv_sprintf(msg->rep, |
| 615 | " RX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 616 | nla_get_u32(link[TIPC_NLA_LINK_RX]) - |
| 617 | nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]), |
| 618 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]), |
| 619 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]), |
| 620 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]), |
| 621 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED])); |
| 622 | |
| 623 | tipc_tlv_sprintf(msg->rep, |
| 624 | " TX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 625 | nla_get_u32(link[TIPC_NLA_LINK_TX]) - |
| 626 | nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]), |
| 627 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]), |
| 628 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]), |
| 629 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]), |
| 630 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED])); |
| 631 | |
| 632 | tipc_tlv_sprintf(msg->rep, |
| 633 | " TX profile sample:%u packets average:%u octets\n", |
| 634 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]), |
| 635 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / |
| 636 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])); |
| 637 | |
| 638 | tipc_tlv_sprintf(msg->rep, |
| 639 | " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ", |
| 640 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), |
| 641 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 642 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), |
| 643 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 644 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), |
| 645 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 646 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), |
| 647 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]))); |
| 648 | |
| 649 | tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n", |
| 650 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), |
| 651 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 652 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), |
| 653 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 654 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), |
| 655 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]))); |
| 656 | |
| 657 | tipc_tlv_sprintf(msg->rep, |
| 658 | " RX states:%u probes:%u naks:%u defs:%u dups:%u\n", |
| 659 | nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]), |
| 660 | nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]), |
| 661 | nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]), |
| 662 | nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]), |
| 663 | nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES])); |
| 664 | |
| 665 | tipc_tlv_sprintf(msg->rep, |
| 666 | " TX states:%u probes:%u naks:%u acks:%u dups:%u\n", |
| 667 | nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]), |
| 668 | nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]), |
| 669 | nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]), |
| 670 | nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]), |
| 671 | nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED])); |
| 672 | |
| 673 | tipc_tlv_sprintf(msg->rep, |
| 674 | " Congestion link:%u Send queue max:%u avg:%u", |
| 675 | nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]), |
| 676 | nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]), |
| 677 | nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE])); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 682 | static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg, |
| 683 | struct nlattr **attrs) |
| 684 | { |
| 685 | struct nlattr *link[TIPC_NLA_LINK_MAX + 1]; |
| 686 | struct tipc_link_info link_info; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 687 | int err; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 688 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 689 | if (!attrs[TIPC_NLA_LINK]) |
| 690 | return -EINVAL; |
| 691 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 692 | err = nla_parse_nested_deprecated(link, TIPC_NLA_LINK_MAX, |
| 693 | attrs[TIPC_NLA_LINK], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 694 | if (err) |
| 695 | return err; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 696 | |
| 697 | link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]); |
| 698 | link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP])); |
Richard Alpe | 55e77a3 | 2016-07-01 11:11:21 +0200 | [diff] [blame] | 699 | nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME], |
Kangjie Lu | 5d2be14 | 2016-06-02 04:04:56 -0400 | [diff] [blame] | 700 | TIPC_MAX_LINK_NAME); |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 701 | |
| 702 | return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO, |
| 703 | &link_info, sizeof(link_info)); |
| 704 | } |
| 705 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 706 | static int __tipc_add_link_prop(struct sk_buff *skb, |
| 707 | struct tipc_nl_compat_msg *msg, |
| 708 | struct tipc_link_config *lc) |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 709 | { |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 710 | switch (msg->cmd) { |
| 711 | case TIPC_CMD_SET_LINK_PRI: |
| 712 | return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)); |
| 713 | case TIPC_CMD_SET_LINK_TOL: |
| 714 | return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)); |
| 715 | case TIPC_CMD_SET_LINK_WINDOW: |
| 716 | return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)); |
| 717 | } |
| 718 | |
| 719 | return -EINVAL; |
| 720 | } |
| 721 | |
| 722 | static int tipc_nl_compat_media_set(struct sk_buff *skb, |
| 723 | struct tipc_nl_compat_msg *msg) |
| 724 | { |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 725 | struct nlattr *prop; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 726 | struct nlattr *media; |
| 727 | struct tipc_link_config *lc; |
| 728 | |
| 729 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 730 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 731 | media = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 732 | if (!media) |
| 733 | return -EMSGSIZE; |
| 734 | |
| 735 | if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name)) |
| 736 | return -EMSGSIZE; |
| 737 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 738 | prop = nla_nest_start_noflag(skb, TIPC_NLA_MEDIA_PROP); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 739 | if (!prop) |
| 740 | return -EMSGSIZE; |
| 741 | |
| 742 | __tipc_add_link_prop(skb, msg, lc); |
| 743 | nla_nest_end(skb, prop); |
| 744 | nla_nest_end(skb, media); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int tipc_nl_compat_bearer_set(struct sk_buff *skb, |
| 750 | struct tipc_nl_compat_msg *msg) |
| 751 | { |
| 752 | struct nlattr *prop; |
| 753 | struct nlattr *bearer; |
| 754 | struct tipc_link_config *lc; |
| 755 | |
| 756 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 757 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 758 | bearer = nla_nest_start_noflag(skb, TIPC_NLA_BEARER); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 759 | if (!bearer) |
| 760 | return -EMSGSIZE; |
| 761 | |
| 762 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name)) |
| 763 | return -EMSGSIZE; |
| 764 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 765 | prop = nla_nest_start_noflag(skb, TIPC_NLA_BEARER_PROP); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 766 | if (!prop) |
| 767 | return -EMSGSIZE; |
| 768 | |
| 769 | __tipc_add_link_prop(skb, msg, lc); |
| 770 | nla_nest_end(skb, prop); |
| 771 | nla_nest_end(skb, bearer); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static int __tipc_nl_compat_link_set(struct sk_buff *skb, |
| 777 | struct tipc_nl_compat_msg *msg) |
| 778 | { |
| 779 | struct nlattr *prop; |
| 780 | struct nlattr *link; |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 781 | struct tipc_link_config *lc; |
| 782 | |
| 783 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 784 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 785 | link = nla_nest_start_noflag(skb, TIPC_NLA_LINK); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 786 | if (!link) |
| 787 | return -EMSGSIZE; |
| 788 | |
| 789 | if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name)) |
| 790 | return -EMSGSIZE; |
| 791 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 792 | prop = nla_nest_start_noflag(skb, TIPC_NLA_LINK_PROP); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 793 | if (!prop) |
| 794 | return -EMSGSIZE; |
| 795 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 796 | __tipc_add_link_prop(skb, msg, lc); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 797 | nla_nest_end(skb, prop); |
| 798 | nla_nest_end(skb, link); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 803 | static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd, |
| 804 | struct sk_buff *skb, |
| 805 | struct tipc_nl_compat_msg *msg) |
| 806 | { |
| 807 | struct tipc_link_config *lc; |
| 808 | struct tipc_bearer *bearer; |
| 809 | struct tipc_media *media; |
Ying Xue | edf5ff0 | 2019-01-14 17:22:27 +0800 | [diff] [blame] | 810 | int len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 811 | |
| 812 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 813 | |
Xin Long | 8c63bf9 | 2019-03-31 22:50:09 +0800 | [diff] [blame] | 814 | len = TLV_GET_DATA_LEN(msg->req); |
| 815 | len -= offsetof(struct tipc_link_config, name); |
| 816 | if (len <= 0) |
| 817 | return -EINVAL; |
| 818 | |
| 819 | len = min_t(int, len, TIPC_MAX_LINK_NAME); |
Ying Xue | edf5ff0 | 2019-01-14 17:22:27 +0800 | [diff] [blame] | 820 | if (!string_is_valid(lc->name, len)) |
| 821 | return -EINVAL; |
| 822 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 823 | media = tipc_media_find(lc->name); |
| 824 | if (media) { |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 825 | cmd->doit = &__tipc_nl_media_set; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 826 | return tipc_nl_compat_media_set(skb, msg); |
| 827 | } |
| 828 | |
| 829 | bearer = tipc_bearer_find(msg->net, lc->name); |
| 830 | if (bearer) { |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 831 | cmd->doit = &__tipc_nl_bearer_set; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 832 | return tipc_nl_compat_bearer_set(skb, msg); |
| 833 | } |
| 834 | |
| 835 | return __tipc_nl_compat_link_set(skb, msg); |
| 836 | } |
| 837 | |
| 838 | static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd, |
| 839 | struct sk_buff *skb, |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 840 | struct tipc_nl_compat_msg *msg) |
| 841 | { |
| 842 | char *name; |
| 843 | struct nlattr *link; |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 844 | int len; |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 845 | |
| 846 | name = (char *)TLV_DATA(msg->req); |
| 847 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 848 | link = nla_nest_start_noflag(skb, TIPC_NLA_LINK); |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 849 | if (!link) |
| 850 | return -EMSGSIZE; |
| 851 | |
Xin Long | 4f07b80 | 2019-06-25 00:28:19 +0800 | [diff] [blame] | 852 | len = TLV_GET_DATA_LEN(msg->req); |
| 853 | if (len <= 0) |
| 854 | return -EINVAL; |
| 855 | |
John Rutherford | fd567ac | 2019-11-26 13:52:55 +1100 | [diff] [blame] | 856 | len = min_t(int, len, TIPC_MAX_LINK_NAME); |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 857 | if (!string_is_valid(name, len)) |
| 858 | return -EINVAL; |
| 859 | |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 860 | if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name)) |
| 861 | return -EMSGSIZE; |
| 862 | |
| 863 | nla_nest_end(skb, link); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 868 | static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg) |
| 869 | { |
| 870 | int i; |
| 871 | u32 depth; |
| 872 | struct tipc_name_table_query *ntq; |
| 873 | static const char * const header[] = { |
| 874 | "Type ", |
| 875 | "Lower Upper ", |
| 876 | "Port Identity ", |
| 877 | "Publication Scope" |
| 878 | }; |
| 879 | |
| 880 | ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req); |
Ying Xue | 974cb0e | 2019-01-14 17:22:28 +0800 | [diff] [blame] | 881 | if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query)) |
| 882 | return -EINVAL; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 883 | |
| 884 | depth = ntohl(ntq->depth); |
| 885 | |
| 886 | if (depth > 4) |
| 887 | depth = 4; |
| 888 | for (i = 0; i < depth; i++) |
| 889 | tipc_tlv_sprintf(msg->rep, header[i]); |
| 890 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg, |
| 896 | struct nlattr **attrs) |
| 897 | { |
| 898 | char port_str[27]; |
| 899 | struct tipc_name_table_query *ntq; |
| 900 | struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1]; |
| 901 | struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1]; |
| 902 | u32 node, depth, type, lowbound, upbound; |
| 903 | static const char * const scope_str[] = {"", " zone", " cluster", |
| 904 | " node"}; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 905 | int err; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 906 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 907 | if (!attrs[TIPC_NLA_NAME_TABLE]) |
| 908 | return -EINVAL; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 909 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 910 | err = nla_parse_nested_deprecated(nt, TIPC_NLA_NAME_TABLE_MAX, |
| 911 | attrs[TIPC_NLA_NAME_TABLE], NULL, |
| 912 | NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 913 | if (err) |
| 914 | return err; |
| 915 | |
| 916 | if (!nt[TIPC_NLA_NAME_TABLE_PUBL]) |
| 917 | return -EINVAL; |
| 918 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 919 | err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX, |
| 920 | nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, |
| 921 | NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 922 | if (err) |
| 923 | return err; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 924 | |
| 925 | ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req); |
| 926 | |
| 927 | depth = ntohl(ntq->depth); |
| 928 | type = ntohl(ntq->type); |
| 929 | lowbound = ntohl(ntq->lowbound); |
| 930 | upbound = ntohl(ntq->upbound); |
| 931 | |
| 932 | if (!(depth & TIPC_NTQ_ALLTYPES) && |
| 933 | (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]))) |
| 934 | return 0; |
| 935 | if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]))) |
| 936 | return 0; |
| 937 | if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]))) |
| 938 | return 0; |
| 939 | |
| 940 | tipc_tlv_sprintf(msg->rep, "%-10u ", |
| 941 | nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])); |
| 942 | |
| 943 | if (depth == 1) |
| 944 | goto out; |
| 945 | |
| 946 | tipc_tlv_sprintf(msg->rep, "%-10u %-10u ", |
| 947 | nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]), |
| 948 | nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])); |
| 949 | |
| 950 | if (depth == 2) |
| 951 | goto out; |
| 952 | |
| 953 | node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]); |
| 954 | sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node), |
| 955 | tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF])); |
| 956 | tipc_tlv_sprintf(msg->rep, "%-26s ", port_str); |
| 957 | |
| 958 | if (depth == 3) |
| 959 | goto out; |
| 960 | |
| 961 | tipc_tlv_sprintf(msg->rep, "%-10u %s", |
Richard Alpe | 03aaaa9 | 2016-05-17 16:57:37 +0200 | [diff] [blame] | 962 | nla_get_u32(publ[TIPC_NLA_PUBL_KEY]), |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 963 | scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); |
| 964 | out: |
| 965 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 970 | static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, |
| 971 | struct nlattr **attrs) |
| 972 | { |
| 973 | u32 type, lower, upper; |
| 974 | struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 975 | int err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 976 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 977 | if (!attrs[TIPC_NLA_PUBL]) |
| 978 | return -EINVAL; |
| 979 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 980 | err = nla_parse_nested_deprecated(publ, TIPC_NLA_PUBL_MAX, |
| 981 | attrs[TIPC_NLA_PUBL], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 982 | if (err) |
| 983 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 984 | |
| 985 | type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]); |
| 986 | lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]); |
| 987 | upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]); |
| 988 | |
| 989 | if (lower == upper) |
| 990 | tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower); |
| 991 | else |
| 992 | tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper); |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock) |
| 998 | { |
| 999 | int err; |
| 1000 | void *hdr; |
| 1001 | struct nlattr *nest; |
| 1002 | struct sk_buff *args; |
| 1003 | struct tipc_nl_compat_cmd_dump dump; |
| 1004 | |
| 1005 | args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1006 | if (!args) |
| 1007 | return -ENOMEM; |
| 1008 | |
| 1009 | hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI, |
| 1010 | TIPC_NL_PUBL_GET); |
Gustavo A. R. Silva | f87d8ad | 2019-01-05 10:52:23 -0600 | [diff] [blame] | 1011 | if (!hdr) { |
| 1012 | kfree_skb(args); |
Kangjie Lu | 46273cf | 2018-12-26 00:09:04 -0600 | [diff] [blame] | 1013 | return -EMSGSIZE; |
Gustavo A. R. Silva | f87d8ad | 2019-01-05 10:52:23 -0600 | [diff] [blame] | 1014 | } |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1015 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1016 | nest = nla_nest_start_noflag(args, TIPC_NLA_SOCK); |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1017 | if (!nest) { |
| 1018 | kfree_skb(args); |
| 1019 | return -EMSGSIZE; |
| 1020 | } |
| 1021 | |
| 1022 | if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) { |
| 1023 | kfree_skb(args); |
| 1024 | return -EMSGSIZE; |
| 1025 | } |
| 1026 | |
| 1027 | nla_nest_end(args, nest); |
| 1028 | genlmsg_end(args, hdr); |
| 1029 | |
| 1030 | dump.dumpit = tipc_nl_publ_dump; |
| 1031 | dump.format = __tipc_nl_compat_publ_dump; |
| 1032 | |
| 1033 | err = __tipc_nl_compat_dumpit(&dump, msg, args); |
| 1034 | |
| 1035 | kfree_skb(args); |
| 1036 | |
| 1037 | return err; |
| 1038 | } |
| 1039 | |
| 1040 | static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg, |
| 1041 | struct nlattr **attrs) |
| 1042 | { |
| 1043 | int err; |
| 1044 | u32 sock_ref; |
| 1045 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; |
| 1046 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1047 | if (!attrs[TIPC_NLA_SOCK]) |
| 1048 | return -EINVAL; |
| 1049 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1050 | err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX, |
| 1051 | attrs[TIPC_NLA_SOCK], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1052 | if (err) |
| 1053 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1054 | |
| 1055 | sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); |
| 1056 | tipc_tlv_sprintf(msg->rep, "%u:", sock_ref); |
| 1057 | |
| 1058 | if (sock[TIPC_NLA_SOCK_CON]) { |
| 1059 | u32 node; |
| 1060 | struct nlattr *con[TIPC_NLA_CON_MAX + 1]; |
| 1061 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1062 | err = nla_parse_nested_deprecated(con, TIPC_NLA_CON_MAX, |
| 1063 | sock[TIPC_NLA_SOCK_CON], |
| 1064 | NULL, NULL); |
Aditya Pakki | 89dfd00 | 2018-12-23 18:54:53 -0600 | [diff] [blame] | 1065 | |
| 1066 | if (err) |
| 1067 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1068 | |
| 1069 | node = nla_get_u32(con[TIPC_NLA_CON_NODE]); |
| 1070 | tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>", |
| 1071 | tipc_zone(node), |
| 1072 | tipc_cluster(node), |
| 1073 | tipc_node(node), |
| 1074 | nla_get_u32(con[TIPC_NLA_CON_SOCK])); |
| 1075 | |
| 1076 | if (con[TIPC_NLA_CON_FLAG]) |
| 1077 | tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n", |
| 1078 | nla_get_u32(con[TIPC_NLA_CON_TYPE]), |
| 1079 | nla_get_u32(con[TIPC_NLA_CON_INST])); |
| 1080 | else |
| 1081 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 1082 | } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) { |
| 1083 | tipc_tlv_sprintf(msg->rep, " bound to"); |
| 1084 | |
| 1085 | err = tipc_nl_compat_publ_dump(msg, sock_ref); |
| 1086 | if (err) |
| 1087 | return err; |
| 1088 | } |
| 1089 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1094 | static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg, |
| 1095 | struct nlattr **attrs) |
| 1096 | { |
| 1097 | struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1098 | int err; |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1099 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1100 | if (!attrs[TIPC_NLA_MEDIA]) |
| 1101 | return -EINVAL; |
| 1102 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1103 | err = nla_parse_nested_deprecated(media, TIPC_NLA_MEDIA_MAX, |
| 1104 | attrs[TIPC_NLA_MEDIA], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1105 | if (err) |
| 1106 | return err; |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1107 | |
| 1108 | return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME, |
| 1109 | nla_data(media[TIPC_NLA_MEDIA_NAME]), |
| 1110 | nla_len(media[TIPC_NLA_MEDIA_NAME])); |
| 1111 | } |
| 1112 | |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1113 | static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg, |
| 1114 | struct nlattr **attrs) |
| 1115 | { |
| 1116 | struct tipc_node_info node_info; |
| 1117 | struct nlattr *node[TIPC_NLA_NODE_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1118 | int err; |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1119 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1120 | if (!attrs[TIPC_NLA_NODE]) |
| 1121 | return -EINVAL; |
| 1122 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1123 | err = nla_parse_nested_deprecated(node, TIPC_NLA_NODE_MAX, |
| 1124 | attrs[TIPC_NLA_NODE], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1125 | if (err) |
| 1126 | return err; |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1127 | |
| 1128 | node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR])); |
| 1129 | node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP])); |
| 1130 | |
| 1131 | return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info, |
| 1132 | sizeof(node_info)); |
| 1133 | } |
| 1134 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1135 | static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd, |
| 1136 | struct sk_buff *skb, |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1137 | struct tipc_nl_compat_msg *msg) |
| 1138 | { |
| 1139 | u32 val; |
| 1140 | struct nlattr *net; |
| 1141 | |
| 1142 | val = ntohl(*(__be32 *)TLV_DATA(msg->req)); |
| 1143 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 1144 | net = nla_nest_start_noflag(skb, TIPC_NLA_NET); |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1145 | if (!net) |
| 1146 | return -EMSGSIZE; |
| 1147 | |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1148 | if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) { |
| 1149 | if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val)) |
| 1150 | return -EMSGSIZE; |
| 1151 | } else if (msg->cmd == TIPC_CMD_SET_NETID) { |
| 1152 | if (nla_put_u32(skb, TIPC_NLA_NET_ID, val)) |
| 1153 | return -EMSGSIZE; |
| 1154 | } |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1155 | nla_nest_end(skb, net); |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1160 | static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg, |
| 1161 | struct nlattr **attrs) |
| 1162 | { |
| 1163 | __be32 id; |
| 1164 | struct nlattr *net[TIPC_NLA_NET_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1165 | int err; |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1166 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1167 | if (!attrs[TIPC_NLA_NET]) |
| 1168 | return -EINVAL; |
| 1169 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1170 | err = nla_parse_nested_deprecated(net, TIPC_NLA_NET_MAX, |
| 1171 | attrs[TIPC_NLA_NET], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1172 | if (err) |
| 1173 | return err; |
| 1174 | |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1175 | id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID])); |
| 1176 | |
| 1177 | return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id)); |
| 1178 | } |
| 1179 | |
Richard Alpe | 5a81a63 | 2015-02-09 09:50:17 +0100 | [diff] [blame] | 1180 | static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg) |
| 1181 | { |
| 1182 | msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN); |
| 1183 | if (!msg->rep) |
| 1184 | return -ENOMEM; |
| 1185 | |
| 1186 | tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING); |
| 1187 | tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n"); |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1192 | static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg) |
| 1193 | { |
| 1194 | struct tipc_nl_compat_cmd_dump dump; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1195 | struct tipc_nl_compat_cmd_doit doit; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1196 | |
| 1197 | memset(&dump, 0, sizeof(dump)); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1198 | memset(&doit, 0, sizeof(doit)); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1199 | |
| 1200 | switch (msg->cmd) { |
Richard Alpe | 22ae7cf | 2015-02-09 09:50:18 +0100 | [diff] [blame] | 1201 | case TIPC_CMD_NOOP: |
| 1202 | msg->rep = tipc_tlv_alloc(0); |
| 1203 | if (!msg->rep) |
| 1204 | return -ENOMEM; |
| 1205 | return 0; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1206 | case TIPC_CMD_GET_BEARER_NAMES: |
| 1207 | msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME); |
| 1208 | dump.dumpit = tipc_nl_bearer_dump; |
| 1209 | dump.format = tipc_nl_compat_bearer_dump; |
| 1210 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1211 | case TIPC_CMD_ENABLE_BEARER: |
| 1212 | msg->req_type = TIPC_TLV_BEARER_CONFIG; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1213 | doit.doit = __tipc_nl_bearer_enable; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1214 | doit.transcode = tipc_nl_compat_bearer_enable; |
| 1215 | return tipc_nl_compat_doit(&doit, msg); |
| 1216 | case TIPC_CMD_DISABLE_BEARER: |
| 1217 | msg->req_type = TIPC_TLV_BEARER_NAME; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1218 | doit.doit = __tipc_nl_bearer_disable; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1219 | doit.transcode = tipc_nl_compat_bearer_disable; |
| 1220 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 1221 | case TIPC_CMD_SHOW_LINK_STATS: |
| 1222 | msg->req_type = TIPC_TLV_LINK_NAME; |
| 1223 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1224 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1225 | dump.dumpit = tipc_nl_node_dump_link; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 1226 | dump.format = tipc_nl_compat_link_stat_dump; |
| 1227 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 1228 | case TIPC_CMD_GET_LINKS: |
| 1229 | msg->req_type = TIPC_TLV_NET_ADDR; |
| 1230 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1231 | dump.dumpit = tipc_nl_node_dump_link; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 1232 | dump.format = tipc_nl_compat_link_dump; |
| 1233 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 1234 | case TIPC_CMD_SET_LINK_TOL: |
| 1235 | case TIPC_CMD_SET_LINK_PRI: |
| 1236 | case TIPC_CMD_SET_LINK_WINDOW: |
| 1237 | msg->req_type = TIPC_TLV_LINK_CONFIG; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1238 | doit.doit = tipc_nl_node_set_link; |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 1239 | doit.transcode = tipc_nl_compat_link_set; |
| 1240 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 1241 | case TIPC_CMD_RESET_LINK_STATS: |
| 1242 | msg->req_type = TIPC_TLV_LINK_NAME; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1243 | doit.doit = tipc_nl_node_reset_link_stats; |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 1244 | doit.transcode = tipc_nl_compat_link_reset_stats; |
| 1245 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 1246 | case TIPC_CMD_SHOW_NAME_TABLE: |
| 1247 | msg->req_type = TIPC_TLV_NAME_TBL_QUERY; |
| 1248 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1249 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
| 1250 | dump.header = tipc_nl_compat_name_table_dump_header; |
| 1251 | dump.dumpit = tipc_nl_name_table_dump; |
| 1252 | dump.format = tipc_nl_compat_name_table_dump; |
| 1253 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1254 | case TIPC_CMD_SHOW_PORTS: |
| 1255 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1256 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
| 1257 | dump.dumpit = tipc_nl_sk_dump; |
| 1258 | dump.format = tipc_nl_compat_sk_dump; |
| 1259 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1260 | case TIPC_CMD_GET_MEDIA_NAMES: |
| 1261 | msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME); |
| 1262 | dump.dumpit = tipc_nl_media_dump; |
| 1263 | dump.format = tipc_nl_compat_media_dump; |
| 1264 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1265 | case TIPC_CMD_GET_NODES: |
| 1266 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1267 | dump.dumpit = tipc_nl_node_dump; |
| 1268 | dump.format = tipc_nl_compat_node_dump; |
| 1269 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1270 | case TIPC_CMD_SET_NODE_ADDR: |
| 1271 | msg->req_type = TIPC_TLV_NET_ADDR; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1272 | doit.doit = __tipc_nl_net_set; |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1273 | doit.transcode = tipc_nl_compat_net_set; |
| 1274 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1275 | case TIPC_CMD_SET_NETID: |
| 1276 | msg->req_type = TIPC_TLV_UNSIGNED; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1277 | doit.doit = __tipc_nl_net_set; |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1278 | doit.transcode = tipc_nl_compat_net_set; |
| 1279 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1280 | case TIPC_CMD_GET_NETID: |
| 1281 | msg->rep_size = sizeof(u32); |
| 1282 | dump.dumpit = tipc_nl_net_dump; |
| 1283 | dump.format = tipc_nl_compat_net_dump; |
| 1284 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 5a81a63 | 2015-02-09 09:50:17 +0100 | [diff] [blame] | 1285 | case TIPC_CMD_SHOW_STATS: |
| 1286 | return tipc_cmd_show_stats_compat(msg); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | return -EOPNOTSUPP; |
| 1290 | } |
| 1291 | |
| 1292 | static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info) |
| 1293 | { |
| 1294 | int err; |
| 1295 | int len; |
| 1296 | struct tipc_nl_compat_msg msg; |
| 1297 | struct nlmsghdr *req_nlh; |
| 1298 | struct nlmsghdr *rep_nlh; |
| 1299 | struct tipc_genlmsghdr *req_userhdr = info->userhdr; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1300 | |
| 1301 | memset(&msg, 0, sizeof(msg)); |
| 1302 | |
| 1303 | req_nlh = (struct nlmsghdr *)skb->data; |
| 1304 | msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN; |
| 1305 | msg.cmd = req_userhdr->cmd; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1306 | msg.net = genl_info_net(info); |
Florian Westphal | 619b174 | 2016-02-24 17:20:17 +0100 | [diff] [blame] | 1307 | msg.dst_sk = skb->sk; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1308 | |
| 1309 | if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) { |
| 1310 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN); |
| 1311 | err = -EACCES; |
| 1312 | goto send; |
| 1313 | } |
| 1314 | |
Taras Kondratiuk | 4da5f00 | 2019-07-29 22:15:07 +0000 | [diff] [blame] | 1315 | msg.req_size = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN); |
| 1316 | if (msg.req_size && !TLV_OK(msg.req, msg.req_size)) { |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1317 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); |
| 1318 | err = -EOPNOTSUPP; |
| 1319 | goto send; |
| 1320 | } |
| 1321 | |
| 1322 | err = tipc_nl_compat_handle(&msg); |
Richard Alpe | b063bc5 | 2015-05-06 13:58:56 +0200 | [diff] [blame] | 1323 | if ((err == -EOPNOTSUPP) || (err == -EPERM)) |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1324 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); |
| 1325 | else if (err == -EINVAL) |
| 1326 | msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR); |
| 1327 | send: |
| 1328 | if (!msg.rep) |
| 1329 | return err; |
| 1330 | |
| 1331 | len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); |
| 1332 | skb_push(msg.rep, len); |
| 1333 | rep_nlh = nlmsg_hdr(msg.rep); |
| 1334 | memcpy(rep_nlh, info->nlhdr, len); |
| 1335 | rep_nlh->nlmsg_len = msg.rep->len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1336 | genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1337 | |
| 1338 | return err; |
| 1339 | } |
| 1340 | |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 1341 | static const struct genl_small_ops tipc_genl_compat_ops[] = { |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1342 | { |
| 1343 | .cmd = TIPC_GENL_CMD, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1344 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Richard Alpe | 22ae7cf | 2015-02-09 09:50:18 +0100 | [diff] [blame] | 1345 | .doit = tipc_nl_compat_recv, |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1346 | }, |
| 1347 | }; |
| 1348 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1349 | static struct genl_family tipc_genl_compat_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1350 | .name = TIPC_GENL_NAME, |
| 1351 | .version = TIPC_GENL_VERSION, |
| 1352 | .hdrsize = TIPC_GENL_HDRLEN, |
| 1353 | .maxattr = 0, |
| 1354 | .netnsok = true, |
| 1355 | .module = THIS_MODULE, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 1356 | .small_ops = tipc_genl_compat_ops, |
| 1357 | .n_small_ops = ARRAY_SIZE(tipc_genl_compat_ops), |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1358 | }; |
| 1359 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1360 | int __init tipc_netlink_compat_start(void) |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1361 | { |
| 1362 | int res; |
| 1363 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1364 | res = genl_register_family(&tipc_genl_compat_family); |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1365 | if (res) { |
| 1366 | pr_err("Failed to register legacy compat interface\n"); |
| 1367 | return res; |
| 1368 | } |
| 1369 | |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | void tipc_netlink_compat_stop(void) |
| 1374 | { |
| 1375 | genl_unregister_family(&tipc_genl_compat_family); |
| 1376 | } |