netlink_compat.c 35 KB

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