netlink_compat.c 32 KB

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