netlink_compat.c 33 KB

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