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