netlink_compat.c 33 KB

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