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