netlink_compat.c 32 KB

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