netlink_compat.c 32 KB

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