netlink_compat.c 32 KB

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