netlink_compat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 "config.h"
  35. #include "bearer.h"
  36. #include "link.h"
  37. #include "name_table.h"
  38. #include <net/genetlink.h>
  39. #include <linux/tipc_config.h>
  40. /* The legacy API had an artificial message length limit called
  41. * ULTRA_STRING_MAX_LEN.
  42. */
  43. #define ULTRA_STRING_MAX_LEN 32768
  44. #define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
  45. #define REPLY_TRUNCATED "<truncated>\n"
  46. struct tipc_nl_compat_msg {
  47. u16 cmd;
  48. int rep_type;
  49. int rep_size;
  50. int req_type;
  51. struct sk_buff *rep;
  52. struct tlv_desc *req;
  53. struct sock *dst_sk;
  54. };
  55. struct tipc_nl_compat_cmd_dump {
  56. int (*header)(struct tipc_nl_compat_msg *);
  57. int (*dumpit)(struct sk_buff *, struct netlink_callback *);
  58. int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
  59. };
  60. struct tipc_nl_compat_cmd_doit {
  61. int (*doit)(struct sk_buff *skb, struct genl_info *info);
  62. int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
  63. };
  64. static int tipc_skb_tailroom(struct sk_buff *skb)
  65. {
  66. int tailroom;
  67. int limit;
  68. tailroom = skb_tailroom(skb);
  69. limit = TIPC_SKB_MAX - skb->len;
  70. if (tailroom < limit)
  71. return tailroom;
  72. return limit;
  73. }
  74. static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
  75. {
  76. struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
  77. if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
  78. return -EMSGSIZE;
  79. skb_put(skb, TLV_SPACE(len));
  80. tlv->tlv_type = htons(type);
  81. tlv->tlv_len = htons(TLV_LENGTH(len));
  82. if (len && data)
  83. memcpy(TLV_DATA(tlv), data, len);
  84. return 0;
  85. }
  86. static void tipc_tlv_init(struct sk_buff *skb, u16 type)
  87. {
  88. struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
  89. TLV_SET_LEN(tlv, 0);
  90. TLV_SET_TYPE(tlv, type);
  91. skb_put(skb, sizeof(struct tlv_desc));
  92. }
  93. static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
  94. {
  95. int n;
  96. u16 len;
  97. u32 rem;
  98. char *buf;
  99. struct tlv_desc *tlv;
  100. va_list args;
  101. rem = tipc_skb_tailroom(skb);
  102. tlv = (struct tlv_desc *)skb->data;
  103. len = TLV_GET_LEN(tlv);
  104. buf = TLV_DATA(tlv) + len;
  105. va_start(args, fmt);
  106. n = vscnprintf(buf, rem, fmt, args);
  107. va_end(args);
  108. TLV_SET_LEN(tlv, n + len);
  109. skb_put(skb, n);
  110. return n;
  111. }
  112. static struct sk_buff *tipc_tlv_alloc(int size)
  113. {
  114. int hdr_len;
  115. struct sk_buff *buf;
  116. size = TLV_SPACE(size);
  117. hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
  118. buf = alloc_skb(hdr_len + size, GFP_KERNEL);
  119. if (!buf)
  120. return NULL;
  121. skb_reserve(buf, hdr_len);
  122. return buf;
  123. }
  124. static struct sk_buff *tipc_get_err_tlv(char *str)
  125. {
  126. int str_len = strlen(str) + 1;
  127. struct sk_buff *buf;
  128. buf = tipc_tlv_alloc(TLV_SPACE(str_len));
  129. if (buf)
  130. tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
  131. return buf;
  132. }
  133. static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
  134. struct tipc_nl_compat_msg *msg,
  135. struct sk_buff *arg)
  136. {
  137. int len = 0;
  138. int err;
  139. struct sk_buff *buf;
  140. struct nlmsghdr *nlmsg;
  141. struct netlink_callback cb;
  142. memset(&cb, 0, sizeof(cb));
  143. cb.nlh = (struct nlmsghdr *)arg->data;
  144. cb.skb = arg;
  145. buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  146. if (!buf)
  147. return -ENOMEM;
  148. buf->sk = msg->dst_sk;
  149. do {
  150. int rem;
  151. len = (*cmd->dumpit)(buf, &cb);
  152. nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
  153. struct nlattr **attrs;
  154. err = tipc_nlmsg_parse(nlmsg, &attrs);
  155. if (err)
  156. goto err_out;
  157. err = (*cmd->format)(msg, attrs);
  158. if (err)
  159. goto err_out;
  160. if (tipc_skb_tailroom(msg->rep) <= 1) {
  161. err = -EMSGSIZE;
  162. goto err_out;
  163. }
  164. }
  165. skb_reset_tail_pointer(buf);
  166. buf->len = 0;
  167. } while (len);
  168. err = 0;
  169. err_out:
  170. kfree_skb(buf);
  171. if (err == -EMSGSIZE) {
  172. /* The legacy API only considered messages filling
  173. * "ULTRA_STRING_MAX_LEN" to be truncated.
  174. */
  175. if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
  176. char *tail = skb_tail_pointer(msg->rep);
  177. if (*tail != '\0')
  178. sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
  179. REPLY_TRUNCATED);
  180. }
  181. return 0;
  182. }
  183. return err;
  184. }
  185. static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
  186. struct tipc_nl_compat_msg *msg)
  187. {
  188. int err;
  189. struct sk_buff *arg;
  190. if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
  191. return -EINVAL;
  192. msg->rep = tipc_tlv_alloc(msg->rep_size);
  193. if (!msg->rep)
  194. return -ENOMEM;
  195. if (msg->rep_type)
  196. tipc_tlv_init(msg->rep, msg->rep_type);
  197. if (cmd->header)
  198. (*cmd->header)(msg);
  199. arg = nlmsg_new(0, GFP_KERNEL);
  200. if (!arg) {
  201. kfree_skb(msg->rep);
  202. return -ENOMEM;
  203. }
  204. err = __tipc_nl_compat_dumpit(cmd, msg, arg);
  205. if (err)
  206. kfree_skb(msg->rep);
  207. kfree_skb(arg);
  208. return err;
  209. }
  210. static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
  211. struct tipc_nl_compat_msg *msg)
  212. {
  213. int err;
  214. struct sk_buff *doit_buf;
  215. struct sk_buff *trans_buf;
  216. struct nlattr **attrbuf;
  217. struct genl_info info;
  218. trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  219. if (!trans_buf)
  220. return -ENOMEM;
  221. err = (*cmd->transcode)(trans_buf, msg);
  222. if (err)
  223. goto trans_out;
  224. attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
  225. sizeof(struct nlattr *), GFP_KERNEL);
  226. if (!attrbuf) {
  227. err = -ENOMEM;
  228. goto trans_out;
  229. }
  230. err = nla_parse(attrbuf, tipc_genl_family.maxattr,
  231. (const struct nlattr *)trans_buf->data,
  232. trans_buf->len, NULL);
  233. if (err)
  234. goto parse_out;
  235. doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  236. if (!doit_buf) {
  237. err = -ENOMEM;
  238. goto parse_out;
  239. }
  240. doit_buf->sk = msg->dst_sk;
  241. memset(&info, 0, sizeof(info));
  242. info.attrs = attrbuf;
  243. err = (*cmd->doit)(doit_buf, &info);
  244. kfree_skb(doit_buf);
  245. parse_out:
  246. kfree(attrbuf);
  247. trans_out:
  248. kfree_skb(trans_buf);
  249. return err;
  250. }
  251. static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
  252. struct tipc_nl_compat_msg *msg)
  253. {
  254. int err;
  255. if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
  256. return -EINVAL;
  257. err = __tipc_nl_compat_doit(cmd, msg);
  258. if (err)
  259. return err;
  260. /* The legacy API considered an empty message a success message */
  261. msg->rep = tipc_tlv_alloc(0);
  262. if (!msg->rep)
  263. return -ENOMEM;
  264. return 0;
  265. }
  266. static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
  267. struct nlattr **attrs)
  268. {
  269. struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
  270. nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
  271. NULL);
  272. return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
  273. nla_data(bearer[TIPC_NLA_BEARER_NAME]),
  274. nla_len(bearer[TIPC_NLA_BEARER_NAME]));
  275. }
  276. static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
  277. struct tipc_nl_compat_msg *msg)
  278. {
  279. struct nlattr *prop;
  280. struct nlattr *bearer;
  281. struct tipc_bearer_config *b;
  282. b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
  283. bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
  284. if (!bearer)
  285. return -EMSGSIZE;
  286. if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
  287. return -EMSGSIZE;
  288. if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
  289. return -EMSGSIZE;
  290. if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
  291. prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
  292. if (!prop)
  293. return -EMSGSIZE;
  294. if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
  295. return -EMSGSIZE;
  296. nla_nest_end(skb, prop);
  297. }
  298. nla_nest_end(skb, bearer);
  299. return 0;
  300. }
  301. static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
  302. struct tipc_nl_compat_msg *msg)
  303. {
  304. char *name;
  305. struct nlattr *bearer;
  306. name = (char *)TLV_DATA(msg->req);
  307. bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
  308. if (!bearer)
  309. return -EMSGSIZE;
  310. if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
  311. return -EMSGSIZE;
  312. nla_nest_end(skb, bearer);
  313. return 0;
  314. }
  315. static inline u32 perc(u32 count, u32 total)
  316. {
  317. return (count * 100 + (total / 2)) / total;
  318. }
  319. static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
  320. struct nlattr *prop[], struct nlattr *stats[])
  321. {
  322. tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
  323. nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
  324. tipc_tlv_sprintf(msg->rep,
  325. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  326. nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
  327. nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
  328. nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
  329. nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
  330. nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
  331. tipc_tlv_sprintf(msg->rep,
  332. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  333. nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
  334. nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
  335. nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
  336. nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
  337. nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
  338. tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
  339. nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
  340. nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
  341. nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
  342. tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
  343. nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
  344. nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
  345. nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
  346. tipc_tlv_sprintf(msg->rep,
  347. " Congestion link:%u Send queue max:%u avg:%u",
  348. nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
  349. nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
  350. nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
  351. }
  352. static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
  353. struct nlattr **attrs)
  354. {
  355. char *name;
  356. struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
  357. struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
  358. struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
  359. nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
  360. nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
  361. NULL);
  362. nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
  363. NULL);
  364. name = (char *)TLV_DATA(msg->req);
  365. if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
  366. return 0;
  367. tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
  368. nla_data(link[TIPC_NLA_LINK_NAME]));
  369. if (link[TIPC_NLA_LINK_BROADCAST]) {
  370. __fill_bc_link_stat(msg, prop, stats);
  371. return 0;
  372. }
  373. if (link[TIPC_NLA_LINK_ACTIVE])
  374. tipc_tlv_sprintf(msg->rep, " ACTIVE");
  375. else if (link[TIPC_NLA_LINK_UP])
  376. tipc_tlv_sprintf(msg->rep, " STANDBY");
  377. else
  378. tipc_tlv_sprintf(msg->rep, " DEFUNCT");
  379. tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
  380. nla_get_u32(link[TIPC_NLA_LINK_MTU]),
  381. nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
  382. tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
  383. nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
  384. nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
  385. tipc_tlv_sprintf(msg->rep,
  386. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  387. nla_get_u32(link[TIPC_NLA_LINK_RX]) -
  388. nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
  389. nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
  390. nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
  391. nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
  392. nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
  393. tipc_tlv_sprintf(msg->rep,
  394. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  395. nla_get_u32(link[TIPC_NLA_LINK_TX]) -
  396. nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
  397. nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
  398. nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
  399. nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
  400. nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
  401. tipc_tlv_sprintf(msg->rep,
  402. " TX profile sample:%u packets average:%u octets\n",
  403. nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
  404. nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
  405. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
  406. tipc_tlv_sprintf(msg->rep,
  407. " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
  408. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
  409. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
  410. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
  411. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
  412. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
  413. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
  414. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
  415. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
  416. tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
  417. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
  418. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
  419. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
  420. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
  421. perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
  422. nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
  423. tipc_tlv_sprintf(msg->rep,
  424. " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
  425. nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
  426. nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
  427. nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
  428. nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
  429. nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
  430. tipc_tlv_sprintf(msg->rep,
  431. " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
  432. nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
  433. nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
  434. nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
  435. nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
  436. nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
  437. tipc_tlv_sprintf(msg->rep,
  438. " Congestion link:%u Send queue max:%u avg:%u",
  439. nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
  440. nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
  441. nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
  442. return 0;
  443. }
  444. static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
  445. struct nlattr **attrs)
  446. {
  447. struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
  448. struct tipc_link_info link_info;
  449. nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
  450. link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
  451. link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
  452. strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
  453. return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
  454. &link_info, sizeof(link_info));
  455. }
  456. static int tipc_nl_compat_link_set(struct sk_buff *skb,
  457. struct tipc_nl_compat_msg *msg)
  458. {
  459. struct nlattr *link;
  460. struct nlattr *prop;
  461. struct tipc_link_config *lc;
  462. lc = (struct tipc_link_config *)TLV_DATA(msg->req);
  463. link = nla_nest_start(skb, TIPC_NLA_LINK);
  464. if (!link)
  465. return -EMSGSIZE;
  466. if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
  467. return -EMSGSIZE;
  468. prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
  469. if (!prop)
  470. return -EMSGSIZE;
  471. if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
  472. if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
  473. return -EMSGSIZE;
  474. } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
  475. if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
  476. return -EMSGSIZE;
  477. } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
  478. if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
  479. return -EMSGSIZE;
  480. }
  481. nla_nest_end(skb, prop);
  482. nla_nest_end(skb, link);
  483. return 0;
  484. }
  485. static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
  486. struct tipc_nl_compat_msg *msg)
  487. {
  488. char *name;
  489. struct nlattr *link;
  490. name = (char *)TLV_DATA(msg->req);
  491. link = nla_nest_start(skb, TIPC_NLA_LINK);
  492. if (!link)
  493. return -EMSGSIZE;
  494. if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
  495. return -EMSGSIZE;
  496. nla_nest_end(skb, link);
  497. return 0;
  498. }
  499. static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
  500. {
  501. int i;
  502. u32 depth;
  503. struct tipc_name_table_query *ntq;
  504. static const char * const header[] = {
  505. "Type ",
  506. "Lower Upper ",
  507. "Port Identity ",
  508. "Publication Scope"
  509. };
  510. ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
  511. depth = ntohl(ntq->depth);
  512. if (depth > 4)
  513. depth = 4;
  514. for (i = 0; i < depth; i++)
  515. tipc_tlv_sprintf(msg->rep, header[i]);
  516. tipc_tlv_sprintf(msg->rep, "\n");
  517. return 0;
  518. }
  519. static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
  520. struct nlattr **attrs)
  521. {
  522. char port_str[27];
  523. struct tipc_name_table_query *ntq;
  524. struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
  525. struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
  526. u32 node, depth, type, lowbound, upbound;
  527. static const char * const scope_str[] = {"", " zone", " cluster",
  528. " node"};
  529. nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
  530. attrs[TIPC_NLA_NAME_TABLE], NULL);
  531. nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
  532. NULL);
  533. ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
  534. depth = ntohl(ntq->depth);
  535. type = ntohl(ntq->type);
  536. lowbound = ntohl(ntq->lowbound);
  537. upbound = ntohl(ntq->upbound);
  538. if (!(depth & TIPC_NTQ_ALLTYPES) &&
  539. (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
  540. return 0;
  541. if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
  542. return 0;
  543. if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
  544. return 0;
  545. tipc_tlv_sprintf(msg->rep, "%-10u ",
  546. nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
  547. if (depth == 1)
  548. goto out;
  549. tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
  550. nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
  551. nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
  552. if (depth == 2)
  553. goto out;
  554. node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
  555. sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
  556. tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
  557. tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
  558. if (depth == 3)
  559. goto out;
  560. tipc_tlv_sprintf(msg->rep, "%-10u %s",
  561. nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
  562. scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
  563. out:
  564. tipc_tlv_sprintf(msg->rep, "\n");
  565. return 0;
  566. }
  567. static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
  568. {
  569. struct tipc_nl_compat_cmd_dump dump;
  570. struct tipc_nl_compat_cmd_doit doit;
  571. memset(&dump, 0, sizeof(dump));
  572. memset(&doit, 0, sizeof(doit));
  573. switch (msg->cmd) {
  574. case TIPC_CMD_GET_BEARER_NAMES:
  575. msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
  576. dump.dumpit = tipc_nl_bearer_dump;
  577. dump.format = tipc_nl_compat_bearer_dump;
  578. return tipc_nl_compat_dumpit(&dump, msg);
  579. case TIPC_CMD_ENABLE_BEARER:
  580. msg->req_type = TIPC_TLV_BEARER_CONFIG;
  581. doit.doit = tipc_nl_bearer_enable;
  582. doit.transcode = tipc_nl_compat_bearer_enable;
  583. return tipc_nl_compat_doit(&doit, msg);
  584. case TIPC_CMD_DISABLE_BEARER:
  585. msg->req_type = TIPC_TLV_BEARER_NAME;
  586. doit.doit = tipc_nl_bearer_disable;
  587. doit.transcode = tipc_nl_compat_bearer_disable;
  588. return tipc_nl_compat_doit(&doit, msg);
  589. case TIPC_CMD_SHOW_LINK_STATS:
  590. msg->req_type = TIPC_TLV_LINK_NAME;
  591. msg->rep_size = ULTRA_STRING_MAX_LEN;
  592. msg->rep_type = TIPC_TLV_ULTRA_STRING;
  593. dump.dumpit = tipc_nl_link_dump;
  594. dump.format = tipc_nl_compat_link_stat_dump;
  595. return tipc_nl_compat_dumpit(&dump, msg);
  596. case TIPC_CMD_GET_LINKS:
  597. msg->req_type = TIPC_TLV_NET_ADDR;
  598. msg->rep_size = ULTRA_STRING_MAX_LEN;
  599. dump.dumpit = tipc_nl_link_dump;
  600. dump.format = tipc_nl_compat_link_dump;
  601. return tipc_nl_compat_dumpit(&dump, msg);
  602. case TIPC_CMD_SET_LINK_TOL:
  603. case TIPC_CMD_SET_LINK_PRI:
  604. case TIPC_CMD_SET_LINK_WINDOW:
  605. msg->req_type = TIPC_TLV_LINK_CONFIG;
  606. doit.doit = tipc_nl_link_set;
  607. doit.transcode = tipc_nl_compat_link_set;
  608. return tipc_nl_compat_doit(&doit, msg);
  609. case TIPC_CMD_RESET_LINK_STATS:
  610. msg->req_type = TIPC_TLV_LINK_NAME;
  611. doit.doit = tipc_nl_link_reset_stats;
  612. doit.transcode = tipc_nl_compat_link_reset_stats;
  613. return tipc_nl_compat_doit(&doit, msg);
  614. case TIPC_CMD_SHOW_NAME_TABLE:
  615. msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
  616. msg->rep_size = ULTRA_STRING_MAX_LEN;
  617. msg->rep_type = TIPC_TLV_ULTRA_STRING;
  618. dump.header = tipc_nl_compat_name_table_dump_header;
  619. dump.dumpit = tipc_nl_name_table_dump;
  620. dump.format = tipc_nl_compat_name_table_dump;
  621. return tipc_nl_compat_dumpit(&dump, msg);
  622. }
  623. return -EOPNOTSUPP;
  624. }
  625. static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
  626. {
  627. int err;
  628. int len;
  629. struct tipc_nl_compat_msg msg;
  630. struct nlmsghdr *req_nlh;
  631. struct nlmsghdr *rep_nlh;
  632. struct tipc_genlmsghdr *req_userhdr = info->userhdr;
  633. struct net *net = genl_info_net(info);
  634. memset(&msg, 0, sizeof(msg));
  635. req_nlh = (struct nlmsghdr *)skb->data;
  636. msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
  637. msg.cmd = req_userhdr->cmd;
  638. msg.dst_sk = info->dst_sk;
  639. if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
  640. msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
  641. err = -EACCES;
  642. goto send;
  643. }
  644. len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
  645. if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
  646. msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
  647. err = -EOPNOTSUPP;
  648. goto send;
  649. }
  650. err = tipc_nl_compat_handle(&msg);
  651. if (err == -EOPNOTSUPP)
  652. msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
  653. else if (err == -EINVAL)
  654. msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
  655. send:
  656. if (!msg.rep)
  657. return err;
  658. len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
  659. skb_push(msg.rep, len);
  660. rep_nlh = nlmsg_hdr(msg.rep);
  661. memcpy(rep_nlh, info->nlhdr, len);
  662. rep_nlh->nlmsg_len = msg.rep->len;
  663. genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
  664. return err;
  665. }
  666. static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
  667. {
  668. struct net *net = genl_info_net(info);
  669. struct sk_buff *rep_buf;
  670. struct nlmsghdr *rep_nlh;
  671. struct nlmsghdr *req_nlh = info->nlhdr;
  672. struct tipc_genlmsghdr *req_userhdr = info->userhdr;
  673. int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
  674. u16 cmd;
  675. if ((req_userhdr->cmd & 0xC000) &&
  676. (!netlink_net_capable(skb, CAP_NET_ADMIN)))
  677. cmd = TIPC_CMD_NOT_NET_ADMIN;
  678. else
  679. cmd = req_userhdr->cmd;
  680. rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
  681. nlmsg_data(req_nlh) + GENL_HDRLEN +
  682. TIPC_GENL_HDRLEN,
  683. nlmsg_attrlen(req_nlh, GENL_HDRLEN +
  684. TIPC_GENL_HDRLEN), hdr_space);
  685. if (rep_buf) {
  686. skb_push(rep_buf, hdr_space);
  687. rep_nlh = nlmsg_hdr(rep_buf);
  688. memcpy(rep_nlh, req_nlh, hdr_space);
  689. rep_nlh->nlmsg_len = rep_buf->len;
  690. genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
  691. }
  692. return 0;
  693. }
  694. /* Temporary function to keep functionality throughout the patchset
  695. * without having to mess with the global variables and other trickery
  696. * of the old API.
  697. */
  698. static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
  699. {
  700. struct tipc_genlmsghdr *req = info->userhdr;
  701. switch (req->cmd) {
  702. case TIPC_CMD_GET_BEARER_NAMES:
  703. case TIPC_CMD_ENABLE_BEARER:
  704. case TIPC_CMD_DISABLE_BEARER:
  705. case TIPC_CMD_SHOW_LINK_STATS:
  706. case TIPC_CMD_GET_LINKS:
  707. case TIPC_CMD_SET_LINK_TOL:
  708. case TIPC_CMD_SET_LINK_PRI:
  709. case TIPC_CMD_SET_LINK_WINDOW:
  710. case TIPC_CMD_RESET_LINK_STATS:
  711. case TIPC_CMD_SHOW_NAME_TABLE:
  712. return tipc_nl_compat_recv(skb, info);
  713. }
  714. return handle_cmd(skb, info);
  715. }
  716. static struct genl_family tipc_genl_compat_family = {
  717. .id = GENL_ID_GENERATE,
  718. .name = TIPC_GENL_NAME,
  719. .version = TIPC_GENL_VERSION,
  720. .hdrsize = TIPC_GENL_HDRLEN,
  721. .maxattr = 0,
  722. .netnsok = true,
  723. };
  724. static struct genl_ops tipc_genl_compat_ops[] = {
  725. {
  726. .cmd = TIPC_GENL_CMD,
  727. .doit = tipc_nl_compat_tmp_wrap,
  728. },
  729. };
  730. int tipc_netlink_compat_start(void)
  731. {
  732. int res;
  733. res = genl_register_family_with_ops(&tipc_genl_compat_family,
  734. tipc_genl_compat_ops);
  735. if (res) {
  736. pr_err("Failed to register legacy compat interface\n");
  737. return res;
  738. }
  739. return 0;
  740. }
  741. void tipc_netlink_compat_stop(void)
  742. {
  743. genl_unregister_family(&tipc_genl_compat_family);
  744. }