bearer.c 33 KB

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