netlink_compat.c 35 KB

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