netlink_compat.c 34 KB

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