bearer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. /*
  2. * net/tipc/bearer.c: TIPC bearer code
  3. *
  4. * Copyright (c) 1996-2006, 2013-2016, Ericsson AB
  5. * Copyright (c) 2004-2006, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <net/sock.h>
  37. #include "core.h"
  38. #include "bearer.h"
  39. #include "link.h"
  40. #include "discover.h"
  41. #include "monitor.h"
  42. #include "bcast.h"
  43. #include "netlink.h"
  44. #include "udp_media.h"
  45. #include "trace.h"
  46. #include "crypto.h"
  47. #define MAX_ADDR_STR 60
  48. static struct tipc_media * const media_info_array[] = {
  49. &eth_media_info,
  50. #ifdef CONFIG_TIPC_MEDIA_IB
  51. &ib_media_info,
  52. #endif
  53. #ifdef CONFIG_TIPC_MEDIA_UDP
  54. &udp_media_info,
  55. #endif
  56. NULL
  57. };
  58. static struct tipc_bearer *bearer_get(struct net *net, int bearer_id)
  59. {
  60. struct tipc_net *tn = tipc_net(net);
  61. return rcu_dereference(tn->bearer_list[bearer_id]);
  62. }
  63. static void bearer_disable(struct net *net, struct tipc_bearer *b);
  64. static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
  65. struct packet_type *pt, struct net_device *orig_dev);
  66. /**
  67. * tipc_media_find - locates specified media object by name
  68. * @name: name to locate
  69. */
  70. struct tipc_media *tipc_media_find(const char *name)
  71. {
  72. u32 i;
  73. for (i = 0; media_info_array[i] != NULL; i++) {
  74. if (!strcmp(media_info_array[i]->name, name))
  75. break;
  76. }
  77. return media_info_array[i];
  78. }
  79. /**
  80. * media_find_id - locates specified media object by type identifier
  81. * @type: type identifier to locate
  82. */
  83. static struct tipc_media *media_find_id(u8 type)
  84. {
  85. u32 i;
  86. for (i = 0; media_info_array[i] != NULL; i++) {
  87. if (media_info_array[i]->type_id == type)
  88. break;
  89. }
  90. return media_info_array[i];
  91. }
  92. /**
  93. * tipc_media_addr_printf - record media address in print buffer
  94. * @buf: output buffer
  95. * @len: output buffer size remaining
  96. * @a: input media address
  97. */
  98. int tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
  99. {
  100. char addr_str[MAX_ADDR_STR];
  101. struct tipc_media *m;
  102. int ret;
  103. m = media_find_id(a->media_id);
  104. if (m && !m->addr2str(a, addr_str, sizeof(addr_str)))
  105. ret = scnprintf(buf, len, "%s(%s)", m->name, addr_str);
  106. else {
  107. u32 i;
  108. ret = scnprintf(buf, len, "UNKNOWN(%u)", a->media_id);
  109. for (i = 0; i < sizeof(a->value); i++)
  110. ret += scnprintf(buf + ret, len - ret,
  111. "-%x", a->value[i]);
  112. }
  113. return ret;
  114. }
  115. /**
  116. * bearer_name_validate - validate & (optionally) deconstruct bearer name
  117. * @name: ptr to bearer name string
  118. * @name_parts: ptr to area for bearer name components (or NULL if not needed)
  119. *
  120. * Return: 1 if bearer name is valid, otherwise 0.
  121. */
  122. static int bearer_name_validate(const char *name,
  123. struct tipc_bearer_names *name_parts)
  124. {
  125. char name_copy[TIPC_MAX_BEARER_NAME];
  126. char *media_name;
  127. char *if_name;
  128. u32 media_len;
  129. u32 if_len;
  130. /* copy bearer name & ensure length is OK */
  131. if (strscpy(name_copy, name, TIPC_MAX_BEARER_NAME) < 0)
  132. return 0;
  133. /* ensure all component parts of bearer name are present */
  134. media_name = name_copy;
  135. if_name = strchr(media_name, ':');
  136. if (if_name == NULL)
  137. return 0;
  138. *(if_name++) = 0;
  139. media_len = if_name - media_name;
  140. if_len = strlen(if_name) + 1;
  141. /* validate component parts of bearer name */
  142. if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
  143. (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
  144. return 0;
  145. /* return bearer name components, if necessary */
  146. if (name_parts) {
  147. if (strscpy(name_parts->media_name, media_name,
  148. TIPC_MAX_MEDIA_NAME) < 0)
  149. return 0;
  150. if (strscpy(name_parts->if_name, if_name,
  151. TIPC_MAX_IF_NAME) < 0)
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. /**
  157. * tipc_bearer_find - locates bearer object with matching bearer name
  158. * @net: the applicable net namespace
  159. * @name: bearer name to locate
  160. */
  161. struct tipc_bearer *tipc_bearer_find(struct net *net, const char *name)
  162. {
  163. struct tipc_net *tn = tipc_net(net);
  164. struct tipc_bearer *b;
  165. u32 i;
  166. for (i = 0; i < MAX_BEARERS; i++) {
  167. b = rtnl_dereference(tn->bearer_list[i]);
  168. if (b && (!strcmp(b->name, name)))
  169. return b;
  170. }
  171. return NULL;
  172. }
  173. /* tipc_bearer_get_name - get the bearer name from its id.
  174. * @net: network namespace
  175. * @name: a pointer to the buffer where the name will be stored.
  176. * @bearer_id: the id to get the name from.
  177. */
  178. int tipc_bearer_get_name(struct net *net, char *name, u32 bearer_id)
  179. {
  180. struct tipc_net *tn = tipc_net(net);
  181. struct tipc_bearer *b;
  182. if (bearer_id >= MAX_BEARERS)
  183. return -EINVAL;
  184. b = rtnl_dereference(tn->bearer_list[bearer_id]);
  185. if (!b)
  186. return -EINVAL;
  187. strcpy(name, b->name);
  188. return 0;
  189. }
  190. void tipc_bearer_add_dest(struct net *net, u32 bearer_id, u32 dest)
  191. {
  192. struct tipc_bearer *b;
  193. rcu_read_lock();
  194. b = bearer_get(net, bearer_id);
  195. if (b)
  196. tipc_disc_add_dest(b->disc);
  197. rcu_read_unlock();
  198. }
  199. void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
  200. {
  201. struct tipc_bearer *b;
  202. rcu_read_lock();
  203. b = bearer_get(net, bearer_id);
  204. if (b)
  205. tipc_disc_remove_dest(b->disc);
  206. rcu_read_unlock();
  207. }
  208. /**
  209. * tipc_enable_bearer - enable bearer with the given name
  210. * @net: the applicable net namespace
  211. * @name: bearer name to enable
  212. * @disc_domain: bearer domain
  213. * @prio: bearer priority
  214. * @attr: nlattr array
  215. * @extack: netlink extended ack
  216. */
  217. static int tipc_enable_bearer(struct net *net, const char *name,
  218. u32 disc_domain, u32 prio,
  219. struct nlattr *attr[],
  220. struct netlink_ext_ack *extack)
  221. {
  222. struct tipc_net *tn = tipc_net(net);
  223. struct tipc_bearer_names b_names;
  224. int with_this_prio = 1;
  225. struct tipc_bearer *b;
  226. struct tipc_media *m;
  227. struct sk_buff *skb;
  228. int bearer_id = 0;
  229. int res = -EINVAL;
  230. char *errstr = "";
  231. u32 i;
  232. if (!bearer_name_validate(name, &b_names)) {
  233. NL_SET_ERR_MSG(extack, "Illegal name");
  234. return res;
  235. }
  236. if (prio > TIPC_MAX_LINK_PRI && prio != TIPC_MEDIA_LINK_PRI) {
  237. errstr = "illegal priority";
  238. NL_SET_ERR_MSG(extack, "Illegal priority");
  239. goto rejected;
  240. }
  241. m = tipc_media_find(b_names.media_name);
  242. if (!m) {
  243. errstr = "media not registered";
  244. NL_SET_ERR_MSG(extack, "Media not registered");
  245. goto rejected;
  246. }
  247. if (prio == TIPC_MEDIA_LINK_PRI)
  248. prio = m->priority;
  249. /* Check new bearer vs existing ones and find free bearer id if any */
  250. bearer_id = MAX_BEARERS;
  251. i = MAX_BEARERS;
  252. while (i-- != 0) {
  253. b = rtnl_dereference(tn->bearer_list[i]);
  254. if (!b) {
  255. bearer_id = i;
  256. continue;
  257. }
  258. if (!strcmp(name, b->name)) {
  259. errstr = "already enabled";
  260. NL_SET_ERR_MSG(extack, "Already enabled");
  261. goto rejected;
  262. }
  263. if (b->priority == prio &&
  264. (++with_this_prio > 2)) {
  265. pr_warn("Bearer <%s>: already 2 bearers with priority %u\n",
  266. name, prio);
  267. if (prio == TIPC_MIN_LINK_PRI) {
  268. errstr = "cannot adjust to lower";
  269. NL_SET_ERR_MSG(extack, "Cannot adjust to lower");
  270. goto rejected;
  271. }
  272. pr_warn("Bearer <%s>: trying with adjusted priority\n",
  273. name);
  274. prio--;
  275. bearer_id = MAX_BEARERS;
  276. i = MAX_BEARERS;
  277. with_this_prio = 1;
  278. }
  279. }
  280. if (bearer_id >= MAX_BEARERS) {
  281. errstr = "max 3 bearers permitted";
  282. NL_SET_ERR_MSG(extack, "Max 3 bearers permitted");
  283. goto rejected;
  284. }
  285. b = kzalloc(sizeof(*b), GFP_ATOMIC);
  286. if (!b)
  287. return -ENOMEM;
  288. strscpy(b->name, name);
  289. b->media = m;
  290. res = m->enable_media(net, b, attr);
  291. if (res) {
  292. kfree(b);
  293. errstr = "failed to enable media";
  294. NL_SET_ERR_MSG(extack, "Failed to enable media");
  295. goto rejected;
  296. }
  297. b->identity = bearer_id;
  298. b->tolerance = m->tolerance;
  299. b->min_win = m->min_win;
  300. b->max_win = m->max_win;
  301. b->domain = disc_domain;
  302. b->net_plane = bearer_id + 'A';
  303. b->priority = prio;
  304. refcount_set(&b->refcnt, 1);
  305. res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
  306. if (res) {
  307. bearer_disable(net, b);
  308. errstr = "failed to create discoverer";
  309. NL_SET_ERR_MSG(extack, "Failed to create discoverer");
  310. goto rejected;
  311. }
  312. /* Create monitoring data before accepting activate messages */
  313. if (tipc_mon_create(net, bearer_id)) {
  314. bearer_disable(net, b);
  315. kfree_skb(skb);
  316. return -ENOMEM;
  317. }
  318. test_and_set_bit_lock(0, &b->up);
  319. rcu_assign_pointer(tn->bearer_list[bearer_id], b);
  320. if (skb)
  321. tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
  322. pr_info("Enabled bearer <%s>, priority %u\n", name, prio);
  323. return res;
  324. rejected:
  325. pr_warn("Enabling of bearer <%s> rejected, %s\n", name, errstr);
  326. return res;
  327. }
  328. /**
  329. * tipc_reset_bearer - Reset all links established over this bearer
  330. * @net: the applicable net namespace
  331. * @b: the target bearer
  332. */
  333. static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
  334. {
  335. pr_info("Resetting bearer <%s>\n", b->name);
  336. tipc_node_delete_links(net, b->identity);
  337. tipc_disc_reset(net, b);
  338. return 0;
  339. }
  340. bool tipc_bearer_hold(struct tipc_bearer *b)
  341. {
  342. return (b && refcount_inc_not_zero(&b->refcnt));
  343. }
  344. void tipc_bearer_put(struct tipc_bearer *b)
  345. {
  346. if (b && refcount_dec_and_test(&b->refcnt))
  347. kfree_rcu(b, rcu);
  348. }
  349. /**
  350. * bearer_disable - disable this bearer
  351. * @net: the applicable net namespace
  352. * @b: the bearer to disable
  353. *
  354. * Note: This routine assumes caller holds RTNL lock.
  355. */
  356. static void bearer_disable(struct net *net, struct tipc_bearer *b)
  357. {
  358. struct tipc_net *tn = tipc_net(net);
  359. int bearer_id = b->identity;
  360. pr_info("Disabling bearer <%s>\n", b->name);
  361. clear_bit_unlock(0, &b->up);
  362. tipc_node_delete_links(net, bearer_id);
  363. b->media->disable_media(b);
  364. RCU_INIT_POINTER(b->media_ptr, NULL);
  365. if (b->disc)
  366. tipc_disc_delete(b->disc);
  367. RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
  368. tipc_bearer_put(b);
  369. tipc_mon_delete(net, bearer_id);
  370. }
  371. int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
  372. struct nlattr *attr[])
  373. {
  374. char *dev_name = strchr((const char *)b->name, ':') + 1;
  375. int hwaddr_len = b->media->hwaddr_len;
  376. u8 node_id[NODE_ID_LEN] = {0,};
  377. struct net_device *dev;
  378. /* Find device with specified name */
  379. dev = dev_get_by_name(net, dev_name);
  380. if (!dev)
  381. return -ENODEV;
  382. if (tipc_mtu_bad(dev)) {
  383. dev_put(dev);
  384. return -EINVAL;
  385. }
  386. if (dev == net->loopback_dev) {
  387. dev_put(dev);
  388. pr_info("Enabling <%s> not permitted\n", b->name);
  389. return -EINVAL;
  390. }
  391. /* Autoconfigure own node identity if needed */
  392. if (!tipc_own_id(net) && hwaddr_len <= NODE_ID_LEN) {
  393. memcpy(node_id, dev->dev_addr, hwaddr_len);
  394. tipc_net_init(net, node_id, 0);
  395. }
  396. if (!tipc_own_id(net)) {
  397. dev_put(dev);
  398. pr_warn("Failed to obtain node identity\n");
  399. return -EINVAL;
  400. }
  401. /* Associate TIPC bearer with L2 bearer */
  402. rcu_assign_pointer(b->media_ptr, dev);
  403. b->pt.dev = dev;
  404. b->pt.type = htons(ETH_P_TIPC);
  405. b->pt.func = tipc_l2_rcv_msg;
  406. dev_add_pack(&b->pt);
  407. memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
  408. memcpy(b->bcast_addr.value, dev->broadcast, hwaddr_len);
  409. b->bcast_addr.media_id = b->media->type_id;
  410. b->bcast_addr.broadcast = TIPC_BROADCAST_SUPPORT;
  411. b->mtu = dev->mtu;
  412. b->media->raw2addr(b, &b->addr, (const char *)dev->dev_addr);
  413. rcu_assign_pointer(dev->tipc_ptr, b);
  414. return 0;
  415. }
  416. /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
  417. * @b: the target bearer
  418. *
  419. * Mark L2 bearer as inactive so that incoming buffers are thrown away
  420. */
  421. void tipc_disable_l2_media(struct tipc_bearer *b)
  422. {
  423. struct net_device *dev;
  424. dev = (struct net_device *)rtnl_dereference(b->media_ptr);
  425. dev_remove_pack(&b->pt);
  426. RCU_INIT_POINTER(dev->tipc_ptr, NULL);
  427. synchronize_net();
  428. dev_put(dev);
  429. }
  430. /**
  431. * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
  432. * @net: the associated network namespace
  433. * @skb: the packet to be sent
  434. * @b: the bearer through which the packet is to be sent
  435. * @dest: peer destination address
  436. */
  437. int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
  438. struct tipc_bearer *b, struct tipc_media_addr *dest)
  439. {
  440. struct net_device *dev;
  441. int delta;
  442. dev = (struct net_device *)rcu_dereference(b->media_ptr);
  443. if (!dev)
  444. return 0;
  445. delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
  446. if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
  447. kfree_skb(skb);
  448. return 0;
  449. }
  450. skb_reset_network_header(skb);
  451. skb->dev = dev;
  452. skb->protocol = htons(ETH_P_TIPC);
  453. dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
  454. dev->dev_addr, skb->len);
  455. dev_queue_xmit(skb);
  456. return 0;
  457. }
  458. bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
  459. {
  460. bool supp = false;
  461. struct tipc_bearer *b;
  462. rcu_read_lock();
  463. b = bearer_get(net, bearer_id);
  464. if (b)
  465. supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
  466. rcu_read_unlock();
  467. return supp;
  468. }
  469. int tipc_bearer_mtu(struct net *net, u32 bearer_id)
  470. {
  471. int mtu = 0;
  472. struct tipc_bearer *b;
  473. rcu_read_lock();
  474. b = bearer_get(net, bearer_id);
  475. if (b)
  476. mtu = b->mtu;
  477. rcu_read_unlock();
  478. return mtu;
  479. }
  480. int tipc_bearer_min_mtu(struct net *net, u32 bearer_id)
  481. {
  482. int mtu = TIPC_MIN_BEARER_MTU;
  483. struct tipc_bearer *b;
  484. rcu_read_lock();
  485. b = bearer_get(net, bearer_id);
  486. if (b)
  487. mtu += b->encap_hlen;
  488. rcu_read_unlock();
  489. return mtu;
  490. }
  491. /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
  492. */
  493. void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
  494. struct sk_buff *skb,
  495. struct tipc_media_addr *dest)
  496. {
  497. struct tipc_msg *hdr = buf_msg(skb);
  498. struct tipc_bearer *b;
  499. rcu_read_lock();
  500. b = bearer_get(net, bearer_id);
  501. if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr)))) {
  502. #ifdef CONFIG_TIPC_CRYPTO
  503. tipc_crypto_xmit(net, &skb, b, dest, NULL);
  504. if (skb)
  505. #endif
  506. b->media->send_msg(net, skb, b, dest);
  507. } else {
  508. kfree_skb(skb);
  509. }
  510. rcu_read_unlock();
  511. }
  512. /* tipc_bearer_xmit() -send buffer to destination over bearer
  513. */
  514. void tipc_bearer_xmit(struct net *net, u32 bearer_id,
  515. struct sk_buff_head *xmitq,
  516. struct tipc_media_addr *dst,
  517. struct tipc_node *__dnode)
  518. {
  519. struct tipc_bearer *b;
  520. struct sk_buff *skb, *tmp;
  521. if (skb_queue_empty(xmitq))
  522. return;
  523. rcu_read_lock();
  524. b = bearer_get(net, bearer_id);
  525. if (unlikely(!b))
  526. __skb_queue_purge(xmitq);
  527. skb_queue_walk_safe(xmitq, skb, tmp) {
  528. __skb_dequeue(xmitq);
  529. if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb)))) {
  530. #ifdef CONFIG_TIPC_CRYPTO
  531. tipc_crypto_xmit(net, &skb, b, dst, __dnode);
  532. if (skb)
  533. #endif
  534. b->media->send_msg(net, skb, b, dst);
  535. } else {
  536. kfree_skb(skb);
  537. }
  538. }
  539. rcu_read_unlock();
  540. }
  541. /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
  542. */
  543. void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
  544. struct sk_buff_head *xmitq)
  545. {
  546. struct tipc_net *tn = tipc_net(net);
  547. struct tipc_media_addr *dst;
  548. int net_id = tn->net_id;
  549. struct tipc_bearer *b;
  550. struct sk_buff *skb, *tmp;
  551. struct tipc_msg *hdr;
  552. rcu_read_lock();
  553. b = bearer_get(net, bearer_id);
  554. if (unlikely(!b || !test_bit(0, &b->up)))
  555. __skb_queue_purge(xmitq);
  556. skb_queue_walk_safe(xmitq, skb, tmp) {
  557. hdr = buf_msg(skb);
  558. msg_set_non_seq(hdr, 1);
  559. msg_set_mc_netid(hdr, net_id);
  560. __skb_dequeue(xmitq);
  561. dst = &b->bcast_addr;
  562. #ifdef CONFIG_TIPC_CRYPTO
  563. tipc_crypto_xmit(net, &skb, b, dst, NULL);
  564. if (skb)
  565. #endif
  566. b->media->send_msg(net, skb, b, dst);
  567. }
  568. rcu_read_unlock();
  569. }
  570. /**
  571. * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
  572. * @skb: the received message
  573. * @dev: the net device that the packet was received on
  574. * @pt: the packet_type structure which was used to register this handler
  575. * @orig_dev: the original receive net device in case the device is a bond
  576. *
  577. * Accept only packets explicitly sent to this node, or broadcast packets;
  578. * ignores packets sent using interface multicast, and traffic sent to other
  579. * nodes (which can happen if interface is running in promiscuous mode).
  580. */
  581. static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
  582. struct packet_type *pt, struct net_device *orig_dev)
  583. {
  584. struct tipc_bearer *b;
  585. rcu_read_lock();
  586. b = rcu_dereference(dev->tipc_ptr) ?:
  587. rcu_dereference(orig_dev->tipc_ptr);
  588. if (likely(b && test_bit(0, &b->up) &&
  589. (skb->pkt_type <= PACKET_MULTICAST))) {
  590. skb_mark_not_on_list(skb);
  591. TIPC_SKB_CB(skb)->flags = 0;
  592. tipc_rcv(dev_net(b->pt.dev), skb, b);
  593. rcu_read_unlock();
  594. return NET_RX_SUCCESS;
  595. }
  596. rcu_read_unlock();
  597. kfree_skb(skb);
  598. return NET_RX_DROP;
  599. }
  600. /**
  601. * tipc_l2_device_event - handle device events from network device
  602. * @nb: the context of the notification
  603. * @evt: the type of event
  604. * @ptr: the net device that the event was on
  605. *
  606. * This function is called by the Ethernet driver in case of link
  607. * change event.
  608. */
  609. static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
  610. void *ptr)
  611. {
  612. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  613. struct net *net = dev_net(dev);
  614. struct tipc_bearer *b;
  615. b = rtnl_dereference(dev->tipc_ptr);
  616. if (!b)
  617. return NOTIFY_DONE;
  618. trace_tipc_l2_device_event(dev, b, evt);
  619. switch (evt) {
  620. case NETDEV_CHANGE:
  621. if (netif_carrier_ok(dev) && netif_oper_up(dev)) {
  622. test_and_set_bit_lock(0, &b->up);
  623. break;
  624. }
  625. fallthrough;
  626. case NETDEV_GOING_DOWN:
  627. clear_bit_unlock(0, &b->up);
  628. tipc_reset_bearer(net, b);
  629. break;
  630. case NETDEV_UP:
  631. test_and_set_bit_lock(0, &b->up);
  632. break;
  633. case NETDEV_CHANGEMTU:
  634. if (tipc_mtu_bad(dev)) {
  635. bearer_disable(net, b);
  636. break;
  637. }
  638. b->mtu = dev->mtu;
  639. tipc_reset_bearer(net, b);
  640. break;
  641. case NETDEV_CHANGEADDR:
  642. b->media->raw2addr(b, &b->addr,
  643. (const char *)dev->dev_addr);
  644. tipc_reset_bearer(net, b);
  645. break;
  646. case NETDEV_UNREGISTER:
  647. case NETDEV_CHANGENAME:
  648. bearer_disable(net, b);
  649. break;
  650. }
  651. return NOTIFY_OK;
  652. }
  653. static struct notifier_block notifier = {
  654. .notifier_call = tipc_l2_device_event,
  655. .priority = 0,
  656. };
  657. int tipc_bearer_setup(void)
  658. {
  659. return register_netdevice_notifier(&notifier);
  660. }
  661. void tipc_bearer_cleanup(void)
  662. {
  663. unregister_netdevice_notifier(&notifier);
  664. }
  665. void tipc_bearer_stop(struct net *net)
  666. {
  667. struct tipc_net *tn = tipc_net(net);
  668. struct tipc_bearer *b;
  669. u32 i;
  670. for (i = 0; i < MAX_BEARERS; i++) {
  671. b = rtnl_dereference(tn->bearer_list[i]);
  672. if (b) {
  673. bearer_disable(net, b);
  674. tn->bearer_list[i] = NULL;
  675. }
  676. }
  677. }
  678. void tipc_clone_to_loopback(struct net *net, struct sk_buff_head *pkts)
  679. {
  680. struct net_device *dev = net->loopback_dev;
  681. struct sk_buff *skb, *_skb;
  682. int exp;
  683. skb_queue_walk(pkts, _skb) {
  684. skb = pskb_copy(_skb, GFP_ATOMIC);
  685. if (!skb)
  686. continue;
  687. exp = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
  688. if (exp > 0 && pskb_expand_head(skb, exp, 0, GFP_ATOMIC)) {
  689. kfree_skb(skb);
  690. continue;
  691. }
  692. skb_reset_network_header(skb);
  693. dev_hard_header(skb, dev, ETH_P_TIPC, dev->dev_addr,
  694. dev->dev_addr, skb->len);
  695. skb->dev = dev;
  696. skb->pkt_type = PACKET_HOST;
  697. skb->ip_summed = CHECKSUM_UNNECESSARY;
  698. skb->protocol = eth_type_trans(skb, dev);
  699. netif_rx(skb);
  700. }
  701. }
  702. static int tipc_loopback_rcv_pkt(struct sk_buff *skb, struct net_device *dev,
  703. struct packet_type *pt, struct net_device *od)
  704. {
  705. consume_skb(skb);
  706. return NET_RX_SUCCESS;
  707. }
  708. int tipc_attach_loopback(struct net *net)
  709. {
  710. struct net_device *dev = net->loopback_dev;
  711. struct tipc_net *tn = tipc_net(net);
  712. if (!dev)
  713. return -ENODEV;
  714. netdev_hold(dev, &tn->loopback_pt.dev_tracker, GFP_KERNEL);
  715. tn->loopback_pt.dev = dev;
  716. tn->loopback_pt.type = htons(ETH_P_TIPC);
  717. tn->loopback_pt.func = tipc_loopback_rcv_pkt;
  718. dev_add_pack(&tn->loopback_pt);
  719. return 0;
  720. }
  721. void tipc_detach_loopback(struct net *net)
  722. {
  723. struct tipc_net *tn = tipc_net(net);
  724. dev_remove_pack(&tn->loopback_pt);
  725. netdev_put(net->loopback_dev, &tn->loopback_pt.dev_tracker);
  726. }
  727. /* Caller should hold rtnl_lock to protect the bearer */
  728. static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
  729. struct tipc_bearer *bearer, int nlflags)
  730. {
  731. void *hdr;
  732. struct nlattr *attrs;
  733. struct nlattr *prop;
  734. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  735. nlflags, TIPC_NL_BEARER_GET);
  736. if (!hdr)
  737. return -EMSGSIZE;
  738. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER);
  739. if (!attrs)
  740. goto msg_full;
  741. if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
  742. goto attr_msg_full;
  743. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_BEARER_PROP);
  744. if (!prop)
  745. goto prop_msg_full;
  746. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
  747. goto prop_msg_full;
  748. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
  749. goto prop_msg_full;
  750. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->max_win))
  751. goto prop_msg_full;
  752. if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP)
  753. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, bearer->mtu))
  754. goto prop_msg_full;
  755. nla_nest_end(msg->skb, prop);
  756. #ifdef CONFIG_TIPC_MEDIA_UDP
  757. if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
  758. if (tipc_udp_nl_add_bearer_data(msg, bearer))
  759. goto attr_msg_full;
  760. }
  761. #endif
  762. nla_nest_end(msg->skb, attrs);
  763. genlmsg_end(msg->skb, hdr);
  764. return 0;
  765. prop_msg_full:
  766. nla_nest_cancel(msg->skb, prop);
  767. attr_msg_full:
  768. nla_nest_cancel(msg->skb, attrs);
  769. msg_full:
  770. genlmsg_cancel(msg->skb, hdr);
  771. return -EMSGSIZE;
  772. }
  773. int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
  774. {
  775. int err;
  776. int i = cb->args[0];
  777. struct tipc_bearer *bearer;
  778. struct tipc_nl_msg msg;
  779. struct net *net = sock_net(skb->sk);
  780. struct tipc_net *tn = tipc_net(net);
  781. if (i == MAX_BEARERS)
  782. return 0;
  783. msg.skb = skb;
  784. msg.portid = NETLINK_CB(cb->skb).portid;
  785. msg.seq = cb->nlh->nlmsg_seq;
  786. rtnl_lock();
  787. for (i = 0; i < MAX_BEARERS; i++) {
  788. bearer = rtnl_dereference(tn->bearer_list[i]);
  789. if (!bearer)
  790. continue;
  791. err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
  792. if (err)
  793. break;
  794. }
  795. rtnl_unlock();
  796. cb->args[0] = i;
  797. return skb->len;
  798. }
  799. int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
  800. {
  801. int err;
  802. char *name;
  803. struct sk_buff *rep;
  804. struct tipc_bearer *bearer;
  805. struct tipc_nl_msg msg;
  806. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  807. struct net *net = genl_info_net(info);
  808. if (!info->attrs[TIPC_NLA_BEARER])
  809. return -EINVAL;
  810. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
  811. info->attrs[TIPC_NLA_BEARER],
  812. tipc_nl_bearer_policy, info->extack);
  813. if (err)
  814. return err;
  815. if (!attrs[TIPC_NLA_BEARER_NAME])
  816. return -EINVAL;
  817. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  818. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  819. if (!rep)
  820. return -ENOMEM;
  821. msg.skb = rep;
  822. msg.portid = info->snd_portid;
  823. msg.seq = info->snd_seq;
  824. rtnl_lock();
  825. bearer = tipc_bearer_find(net, name);
  826. if (!bearer) {
  827. err = -EINVAL;
  828. NL_SET_ERR_MSG(info->extack, "Bearer not found");
  829. goto err_out;
  830. }
  831. err = __tipc_nl_add_bearer(&msg, bearer, 0);
  832. if (err)
  833. goto err_out;
  834. rtnl_unlock();
  835. return genlmsg_reply(rep, info);
  836. err_out:
  837. rtnl_unlock();
  838. nlmsg_free(rep);
  839. return err;
  840. }
  841. int __tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
  842. {
  843. int err;
  844. char *name;
  845. struct tipc_bearer *bearer;
  846. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  847. struct net *net = sock_net(skb->sk);
  848. if (!info->attrs[TIPC_NLA_BEARER])
  849. return -EINVAL;
  850. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
  851. info->attrs[TIPC_NLA_BEARER],
  852. tipc_nl_bearer_policy, info->extack);
  853. if (err)
  854. return err;
  855. if (!attrs[TIPC_NLA_BEARER_NAME])
  856. return -EINVAL;
  857. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  858. bearer = tipc_bearer_find(net, name);
  859. if (!bearer) {
  860. NL_SET_ERR_MSG(info->extack, "Bearer not found");
  861. return -EINVAL;
  862. }
  863. bearer_disable(net, bearer);
  864. return 0;
  865. }
  866. int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
  867. {
  868. int err;
  869. rtnl_lock();
  870. err = __tipc_nl_bearer_disable(skb, info);
  871. rtnl_unlock();
  872. return err;
  873. }
  874. int __tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
  875. {
  876. int err;
  877. char *bearer;
  878. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  879. struct net *net = sock_net(skb->sk);
  880. u32 domain = 0;
  881. u32 prio;
  882. prio = TIPC_MEDIA_LINK_PRI;
  883. if (!info->attrs[TIPC_NLA_BEARER])
  884. return -EINVAL;
  885. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
  886. info->attrs[TIPC_NLA_BEARER],
  887. tipc_nl_bearer_policy, info->extack);
  888. if (err)
  889. return err;
  890. if (!attrs[TIPC_NLA_BEARER_NAME])
  891. return -EINVAL;
  892. bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  893. if (attrs[TIPC_NLA_BEARER_DOMAIN])
  894. domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
  895. if (attrs[TIPC_NLA_BEARER_PROP]) {
  896. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  897. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  898. props);
  899. if (err)
  900. return err;
  901. if (props[TIPC_NLA_PROP_PRIO])
  902. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  903. }
  904. return tipc_enable_bearer(net, bearer, domain, prio, attrs,
  905. info->extack);
  906. }
  907. int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
  908. {
  909. int err;
  910. rtnl_lock();
  911. err = __tipc_nl_bearer_enable(skb, info);
  912. rtnl_unlock();
  913. return err;
  914. }
  915. int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
  916. {
  917. int err;
  918. char *name;
  919. struct tipc_bearer *b;
  920. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  921. struct net *net = sock_net(skb->sk);
  922. if (!info->attrs[TIPC_NLA_BEARER])
  923. return -EINVAL;
  924. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
  925. info->attrs[TIPC_NLA_BEARER],
  926. tipc_nl_bearer_policy, info->extack);
  927. if (err)
  928. return err;
  929. if (!attrs[TIPC_NLA_BEARER_NAME])
  930. return -EINVAL;
  931. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  932. rtnl_lock();
  933. b = tipc_bearer_find(net, name);
  934. if (!b) {
  935. NL_SET_ERR_MSG(info->extack, "Bearer not found");
  936. err = -EINVAL;
  937. goto out;
  938. }
  939. #ifdef CONFIG_TIPC_MEDIA_UDP
  940. if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
  941. if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
  942. NL_SET_ERR_MSG(info->extack, "UDP option is unsupported");
  943. err = -EINVAL;
  944. goto out;
  945. }
  946. err = tipc_udp_nl_bearer_add(b,
  947. attrs[TIPC_NLA_BEARER_UDP_OPTS]);
  948. }
  949. #endif
  950. out:
  951. rtnl_unlock();
  952. return err;
  953. }
  954. int __tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
  955. {
  956. struct tipc_bearer *b;
  957. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  958. struct net *net = sock_net(skb->sk);
  959. char *name;
  960. int err;
  961. if (!info->attrs[TIPC_NLA_BEARER])
  962. return -EINVAL;
  963. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_BEARER_MAX,
  964. info->attrs[TIPC_NLA_BEARER],
  965. tipc_nl_bearer_policy, info->extack);
  966. if (err)
  967. return err;
  968. if (!attrs[TIPC_NLA_BEARER_NAME])
  969. return -EINVAL;
  970. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  971. b = tipc_bearer_find(net, name);
  972. if (!b) {
  973. NL_SET_ERR_MSG(info->extack, "Bearer not found");
  974. return -EINVAL;
  975. }
  976. if (attrs[TIPC_NLA_BEARER_PROP]) {
  977. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  978. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  979. props);
  980. if (err)
  981. return err;
  982. if (props[TIPC_NLA_PROP_TOL]) {
  983. b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  984. tipc_node_apply_property(net, b, TIPC_NLA_PROP_TOL);
  985. }
  986. if (props[TIPC_NLA_PROP_PRIO])
  987. b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  988. if (props[TIPC_NLA_PROP_WIN])
  989. b->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  990. if (props[TIPC_NLA_PROP_MTU]) {
  991. if (b->media->type_id != TIPC_MEDIA_TYPE_UDP) {
  992. NL_SET_ERR_MSG(info->extack,
  993. "MTU property is unsupported");
  994. return -EINVAL;
  995. }
  996. #ifdef CONFIG_TIPC_MEDIA_UDP
  997. if (nla_get_u32(props[TIPC_NLA_PROP_MTU]) <
  998. b->encap_hlen + TIPC_MIN_BEARER_MTU) {
  999. NL_SET_ERR_MSG(info->extack,
  1000. "MTU value is out-of-range");
  1001. return -EINVAL;
  1002. }
  1003. b->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
  1004. tipc_node_apply_property(net, b, TIPC_NLA_PROP_MTU);
  1005. #endif
  1006. }
  1007. }
  1008. return 0;
  1009. }
  1010. int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
  1011. {
  1012. int err;
  1013. rtnl_lock();
  1014. err = __tipc_nl_bearer_set(skb, info);
  1015. rtnl_unlock();
  1016. return err;
  1017. }
  1018. static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
  1019. struct tipc_media *media, int nlflags)
  1020. {
  1021. void *hdr;
  1022. struct nlattr *attrs;
  1023. struct nlattr *prop;
  1024. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1025. nlflags, TIPC_NL_MEDIA_GET);
  1026. if (!hdr)
  1027. return -EMSGSIZE;
  1028. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA);
  1029. if (!attrs)
  1030. goto msg_full;
  1031. if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
  1032. goto attr_msg_full;
  1033. prop = nla_nest_start_noflag(msg->skb, TIPC_NLA_MEDIA_PROP);
  1034. if (!prop)
  1035. goto prop_msg_full;
  1036. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
  1037. goto prop_msg_full;
  1038. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
  1039. goto prop_msg_full;
  1040. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->max_win))
  1041. goto prop_msg_full;
  1042. if (media->type_id == TIPC_MEDIA_TYPE_UDP)
  1043. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_MTU, media->mtu))
  1044. goto prop_msg_full;
  1045. nla_nest_end(msg->skb, prop);
  1046. nla_nest_end(msg->skb, attrs);
  1047. genlmsg_end(msg->skb, hdr);
  1048. return 0;
  1049. prop_msg_full:
  1050. nla_nest_cancel(msg->skb, prop);
  1051. attr_msg_full:
  1052. nla_nest_cancel(msg->skb, attrs);
  1053. msg_full:
  1054. genlmsg_cancel(msg->skb, hdr);
  1055. return -EMSGSIZE;
  1056. }
  1057. int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1058. {
  1059. int err;
  1060. int i = cb->args[0];
  1061. struct tipc_nl_msg msg;
  1062. if (i == MAX_MEDIA)
  1063. return 0;
  1064. msg.skb = skb;
  1065. msg.portid = NETLINK_CB(cb->skb).portid;
  1066. msg.seq = cb->nlh->nlmsg_seq;
  1067. rtnl_lock();
  1068. for (; media_info_array[i] != NULL; i++) {
  1069. err = __tipc_nl_add_media(&msg, media_info_array[i],
  1070. NLM_F_MULTI);
  1071. if (err)
  1072. break;
  1073. }
  1074. rtnl_unlock();
  1075. cb->args[0] = i;
  1076. return skb->len;
  1077. }
  1078. int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
  1079. {
  1080. int err;
  1081. char *name;
  1082. struct tipc_nl_msg msg;
  1083. struct tipc_media *media;
  1084. struct sk_buff *rep;
  1085. struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
  1086. if (!info->attrs[TIPC_NLA_MEDIA])
  1087. return -EINVAL;
  1088. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
  1089. info->attrs[TIPC_NLA_MEDIA],
  1090. tipc_nl_media_policy, info->extack);
  1091. if (err)
  1092. return err;
  1093. if (!attrs[TIPC_NLA_MEDIA_NAME])
  1094. return -EINVAL;
  1095. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  1096. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1097. if (!rep)
  1098. return -ENOMEM;
  1099. msg.skb = rep;
  1100. msg.portid = info->snd_portid;
  1101. msg.seq = info->snd_seq;
  1102. rtnl_lock();
  1103. media = tipc_media_find(name);
  1104. if (!media) {
  1105. NL_SET_ERR_MSG(info->extack, "Media not found");
  1106. err = -EINVAL;
  1107. goto err_out;
  1108. }
  1109. err = __tipc_nl_add_media(&msg, media, 0);
  1110. if (err)
  1111. goto err_out;
  1112. rtnl_unlock();
  1113. return genlmsg_reply(rep, info);
  1114. err_out:
  1115. rtnl_unlock();
  1116. nlmsg_free(rep);
  1117. return err;
  1118. }
  1119. int __tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
  1120. {
  1121. int err;
  1122. char *name;
  1123. struct tipc_media *m;
  1124. struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1];
  1125. if (!info->attrs[TIPC_NLA_MEDIA])
  1126. return -EINVAL;
  1127. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MEDIA_MAX,
  1128. info->attrs[TIPC_NLA_MEDIA],
  1129. tipc_nl_media_policy, info->extack);
  1130. if (!attrs[TIPC_NLA_MEDIA_NAME])
  1131. return -EINVAL;
  1132. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  1133. m = tipc_media_find(name);
  1134. if (!m) {
  1135. NL_SET_ERR_MSG(info->extack, "Media not found");
  1136. return -EINVAL;
  1137. }
  1138. if (attrs[TIPC_NLA_MEDIA_PROP]) {
  1139. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1140. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
  1141. props);
  1142. if (err)
  1143. return err;
  1144. if (props[TIPC_NLA_PROP_TOL])
  1145. m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1146. if (props[TIPC_NLA_PROP_PRIO])
  1147. m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1148. if (props[TIPC_NLA_PROP_WIN])
  1149. m->max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1150. if (props[TIPC_NLA_PROP_MTU]) {
  1151. if (m->type_id != TIPC_MEDIA_TYPE_UDP) {
  1152. NL_SET_ERR_MSG(info->extack,
  1153. "MTU property is unsupported");
  1154. return -EINVAL;
  1155. }
  1156. #ifdef CONFIG_TIPC_MEDIA_UDP
  1157. if (tipc_udp_mtu_bad(nla_get_u32
  1158. (props[TIPC_NLA_PROP_MTU]))) {
  1159. NL_SET_ERR_MSG(info->extack,
  1160. "MTU value is out-of-range");
  1161. return -EINVAL;
  1162. }
  1163. m->mtu = nla_get_u32(props[TIPC_NLA_PROP_MTU]);
  1164. #endif
  1165. }
  1166. }
  1167. return 0;
  1168. }
  1169. int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
  1170. {
  1171. int err;
  1172. rtnl_lock();
  1173. err = __tipc_nl_media_set(skb, info);
  1174. rtnl_unlock();
  1175. return err;
  1176. }