netlink_compat.c 35 KB

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