bearer.c 28 KB

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