netlink_compat.c 33 KB

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