netlink_compat.c 35 KB

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