netlink_compat.c 32 KB

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