bearer.c 32 KB

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