bearer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * net/tipc/bearer.c: TIPC bearer code
  3. *
  4. * Copyright (c) 1996-2006, Ericsson AB
  5. * Copyright (c) 2004-2006, 2010-2011, 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 "core.h"
  37. #include "config.h"
  38. #include "bearer.h"
  39. #include "discover.h"
  40. #define MAX_ADDR_STR 60
  41. static struct tipc_media *media_list[MAX_MEDIA];
  42. static u32 media_count;
  43. struct tipc_bearer tipc_bearers[MAX_BEARERS];
  44. static void bearer_disable(struct tipc_bearer *b_ptr);
  45. /**
  46. * tipc_media_find - locates specified media object by name
  47. */
  48. struct tipc_media *tipc_media_find(const char *name)
  49. {
  50. u32 i;
  51. for (i = 0; i < media_count; i++) {
  52. if (!strcmp(media_list[i]->name, name))
  53. return media_list[i];
  54. }
  55. return NULL;
  56. }
  57. /**
  58. * media_find_id - locates specified media object by type identifier
  59. */
  60. static struct tipc_media *media_find_id(u8 type)
  61. {
  62. u32 i;
  63. for (i = 0; i < media_count; i++) {
  64. if (media_list[i]->type_id == type)
  65. return media_list[i];
  66. }
  67. return NULL;
  68. }
  69. /**
  70. * tipc_register_media - register a media type
  71. *
  72. * Bearers for this media type must be activated separately at a later stage.
  73. */
  74. int tipc_register_media(struct tipc_media *m_ptr)
  75. {
  76. int res = -EINVAL;
  77. write_lock_bh(&tipc_net_lock);
  78. if ((strlen(m_ptr->name) + 1) > TIPC_MAX_MEDIA_NAME)
  79. goto exit;
  80. if (m_ptr->priority > TIPC_MAX_LINK_PRI)
  81. goto exit;
  82. if ((m_ptr->tolerance < TIPC_MIN_LINK_TOL) ||
  83. (m_ptr->tolerance > TIPC_MAX_LINK_TOL))
  84. goto exit;
  85. if (media_count >= MAX_MEDIA)
  86. goto exit;
  87. if (tipc_media_find(m_ptr->name) || media_find_id(m_ptr->type_id))
  88. goto exit;
  89. media_list[media_count] = m_ptr;
  90. media_count++;
  91. res = 0;
  92. exit:
  93. write_unlock_bh(&tipc_net_lock);
  94. if (res)
  95. pr_warn("Media <%s> registration error\n", m_ptr->name);
  96. return res;
  97. }
  98. /**
  99. * tipc_media_addr_printf - record media address in print buffer
  100. */
  101. void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a)
  102. {
  103. char addr_str[MAX_ADDR_STR];
  104. struct tipc_media *m_ptr;
  105. int ret;
  106. m_ptr = media_find_id(a->media_id);
  107. if (m_ptr && !m_ptr->addr2str(a, addr_str, sizeof(addr_str)))
  108. ret = tipc_snprintf(buf, len, "%s(%s)", m_ptr->name, addr_str);
  109. else {
  110. u32 i;
  111. ret = tipc_snprintf(buf, len, "UNKNOWN(%u)", a->media_id);
  112. for (i = 0; i < sizeof(a->value); i++)
  113. ret += tipc_snprintf(buf - ret, len + ret,
  114. "-%02x", a->value[i]);
  115. }
  116. }
  117. /**
  118. * tipc_media_get_names - record names of registered media in buffer
  119. */
  120. struct sk_buff *tipc_media_get_names(void)
  121. {
  122. struct sk_buff *buf;
  123. int i;
  124. buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
  125. if (!buf)
  126. return NULL;
  127. read_lock_bh(&tipc_net_lock);
  128. for (i = 0; i < media_count; i++) {
  129. tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME,
  130. media_list[i]->name,
  131. strlen(media_list[i]->name) + 1);
  132. }
  133. read_unlock_bh(&tipc_net_lock);
  134. return buf;
  135. }
  136. /**
  137. * bearer_name_validate - validate & (optionally) deconstruct bearer name
  138. * @name: ptr to bearer name string
  139. * @name_parts: ptr to area for bearer name components (or NULL if not needed)
  140. *
  141. * Returns 1 if bearer name is valid, otherwise 0.
  142. */
  143. static int bearer_name_validate(const char *name,
  144. struct tipc_bearer_names *name_parts)
  145. {
  146. char name_copy[TIPC_MAX_BEARER_NAME];
  147. char *media_name;
  148. char *if_name;
  149. u32 media_len;
  150. u32 if_len;
  151. /* copy bearer name & ensure length is OK */
  152. name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
  153. /* need above in case non-Posix strncpy() doesn't pad with nulls */
  154. strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
  155. if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
  156. return 0;
  157. /* ensure all component parts of bearer name are present */
  158. media_name = name_copy;
  159. if_name = strchr(media_name, ':');
  160. if (if_name == NULL)
  161. return 0;
  162. *(if_name++) = 0;
  163. media_len = if_name - media_name;
  164. if_len = strlen(if_name) + 1;
  165. /* validate component parts of bearer name */
  166. if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
  167. (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME))
  168. return 0;
  169. /* return bearer name components, if necessary */
  170. if (name_parts) {
  171. strcpy(name_parts->media_name, media_name);
  172. strcpy(name_parts->if_name, if_name);
  173. }
  174. return 1;
  175. }
  176. /**
  177. * tipc_bearer_find - locates bearer object with matching bearer name
  178. */
  179. struct tipc_bearer *tipc_bearer_find(const char *name)
  180. {
  181. struct tipc_bearer *b_ptr;
  182. u32 i;
  183. for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
  184. if (b_ptr->active && (!strcmp(b_ptr->name, name)))
  185. return b_ptr;
  186. }
  187. return NULL;
  188. }
  189. /**
  190. * tipc_bearer_find_interface - locates bearer object with matching interface name
  191. */
  192. struct tipc_bearer *tipc_bearer_find_interface(const char *if_name)
  193. {
  194. struct tipc_bearer *b_ptr;
  195. char *b_if_name;
  196. u32 i;
  197. for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
  198. if (!b_ptr->active)
  199. continue;
  200. b_if_name = strchr(b_ptr->name, ':') + 1;
  201. if (!strcmp(b_if_name, if_name))
  202. return b_ptr;
  203. }
  204. return NULL;
  205. }
  206. /**
  207. * tipc_bearer_get_names - record names of bearers in buffer
  208. */
  209. struct sk_buff *tipc_bearer_get_names(void)
  210. {
  211. struct sk_buff *buf;
  212. struct tipc_bearer *b_ptr;
  213. int i, j;
  214. buf = tipc_cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
  215. if (!buf)
  216. return NULL;
  217. read_lock_bh(&tipc_net_lock);
  218. for (i = 0; i < media_count; i++) {
  219. for (j = 0; j < MAX_BEARERS; j++) {
  220. b_ptr = &tipc_bearers[j];
  221. if (b_ptr->active && (b_ptr->media == media_list[i])) {
  222. tipc_cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
  223. b_ptr->name,
  224. strlen(b_ptr->name) + 1);
  225. }
  226. }
  227. }
  228. read_unlock_bh(&tipc_net_lock);
  229. return buf;
  230. }
  231. void tipc_bearer_add_dest(struct tipc_bearer *b_ptr, u32 dest)
  232. {
  233. tipc_nmap_add(&b_ptr->nodes, dest);
  234. tipc_bcbearer_sort();
  235. tipc_disc_add_dest(b_ptr->link_req);
  236. }
  237. void tipc_bearer_remove_dest(struct tipc_bearer *b_ptr, u32 dest)
  238. {
  239. tipc_nmap_remove(&b_ptr->nodes, dest);
  240. tipc_bcbearer_sort();
  241. tipc_disc_remove_dest(b_ptr->link_req);
  242. }
  243. /*
  244. * Interrupt enabling new requests after bearer blocking:
  245. * See bearer_send().
  246. */
  247. void tipc_continue(struct tipc_bearer *b)
  248. {
  249. spin_lock_bh(&b->lock);
  250. b->blocked = 0;
  251. spin_unlock_bh(&b->lock);
  252. }
  253. /*
  254. * tipc_bearer_blocked - determines if bearer is currently blocked
  255. */
  256. int tipc_bearer_blocked(struct tipc_bearer *b)
  257. {
  258. int res;
  259. spin_lock_bh(&b->lock);
  260. res = b->blocked;
  261. spin_unlock_bh(&b->lock);
  262. return res;
  263. }
  264. /**
  265. * tipc_enable_bearer - enable bearer with the given name
  266. */
  267. int tipc_enable_bearer(const char *name, u32 disc_domain, u32 priority)
  268. {
  269. struct tipc_bearer *b_ptr;
  270. struct tipc_media *m_ptr;
  271. struct tipc_bearer_names b_names;
  272. char addr_string[16];
  273. u32 bearer_id;
  274. u32 with_this_prio;
  275. u32 i;
  276. int res = -EINVAL;
  277. if (!tipc_own_addr) {
  278. pr_warn("Bearer <%s> rejected, not supported in standalone mode\n",
  279. name);
  280. return -ENOPROTOOPT;
  281. }
  282. if (!bearer_name_validate(name, &b_names)) {
  283. pr_warn("Bearer <%s> rejected, illegal name\n", name);
  284. return -EINVAL;
  285. }
  286. if (tipc_addr_domain_valid(disc_domain) &&
  287. (disc_domain != tipc_own_addr)) {
  288. if (tipc_in_scope(disc_domain, tipc_own_addr)) {
  289. disc_domain = tipc_own_addr & TIPC_CLUSTER_MASK;
  290. res = 0; /* accept any node in own cluster */
  291. } else if (in_own_cluster_exact(disc_domain))
  292. res = 0; /* accept specified node in own cluster */
  293. }
  294. if (res) {
  295. pr_warn("Bearer <%s> rejected, illegal discovery domain\n",
  296. name);
  297. return -EINVAL;
  298. }
  299. if ((priority > TIPC_MAX_LINK_PRI) &&
  300. (priority != TIPC_MEDIA_LINK_PRI)) {
  301. pr_warn("Bearer <%s> rejected, illegal priority\n", name);
  302. return -EINVAL;
  303. }
  304. write_lock_bh(&tipc_net_lock);
  305. m_ptr = tipc_media_find(b_names.media_name);
  306. if (!m_ptr) {
  307. pr_warn("Bearer <%s> rejected, media <%s> not registered\n",
  308. name, b_names.media_name);
  309. goto exit;
  310. }
  311. if (priority == TIPC_MEDIA_LINK_PRI)
  312. priority = m_ptr->priority;
  313. restart:
  314. bearer_id = MAX_BEARERS;
  315. with_this_prio = 1;
  316. for (i = MAX_BEARERS; i-- != 0; ) {
  317. if (!tipc_bearers[i].active) {
  318. bearer_id = i;
  319. continue;
  320. }
  321. if (!strcmp(name, tipc_bearers[i].name)) {
  322. pr_warn("Bearer <%s> rejected, already enabled\n",
  323. name);
  324. goto exit;
  325. }
  326. if ((tipc_bearers[i].priority == priority) &&
  327. (++with_this_prio > 2)) {
  328. if (priority-- == 0) {
  329. pr_warn("Bearer <%s> rejected, duplicate priority\n",
  330. name);
  331. goto exit;
  332. }
  333. pr_warn("Bearer <%s> priority adjustment required %u->%u\n",
  334. name, priority + 1, priority);
  335. goto restart;
  336. }
  337. }
  338. if (bearer_id >= MAX_BEARERS) {
  339. pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
  340. name, MAX_BEARERS);
  341. goto exit;
  342. }
  343. b_ptr = &tipc_bearers[bearer_id];
  344. strcpy(b_ptr->name, name);
  345. res = m_ptr->enable_bearer(b_ptr);
  346. if (res) {
  347. pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
  348. name, -res);
  349. goto exit;
  350. }
  351. b_ptr->identity = bearer_id;
  352. b_ptr->media = m_ptr;
  353. b_ptr->tolerance = m_ptr->tolerance;
  354. b_ptr->window = m_ptr->window;
  355. b_ptr->net_plane = bearer_id + 'A';
  356. b_ptr->active = 1;
  357. b_ptr->priority = priority;
  358. INIT_LIST_HEAD(&b_ptr->links);
  359. spin_lock_init(&b_ptr->lock);
  360. res = tipc_disc_create(b_ptr, &b_ptr->bcast_addr, disc_domain);
  361. if (res) {
  362. bearer_disable(b_ptr);
  363. pr_warn("Bearer <%s> rejected, discovery object creation failed\n",
  364. name);
  365. goto exit;
  366. }
  367. pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
  368. name,
  369. tipc_addr_string_fill(addr_string, disc_domain), priority);
  370. exit:
  371. write_unlock_bh(&tipc_net_lock);
  372. return res;
  373. }
  374. /**
  375. * tipc_block_bearer - Block the bearer with the given name, and reset all its links
  376. */
  377. int tipc_block_bearer(const char *name)
  378. {
  379. struct tipc_bearer *b_ptr = NULL;
  380. struct tipc_link *l_ptr;
  381. struct tipc_link *temp_l_ptr;
  382. read_lock_bh(&tipc_net_lock);
  383. b_ptr = tipc_bearer_find(name);
  384. if (!b_ptr) {
  385. pr_warn("Attempt to block unknown bearer <%s>\n", name);
  386. read_unlock_bh(&tipc_net_lock);
  387. return -EINVAL;
  388. }
  389. pr_info("Blocking bearer <%s>\n", name);
  390. spin_lock_bh(&b_ptr->lock);
  391. b_ptr->blocked = 1;
  392. list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
  393. struct tipc_node *n_ptr = l_ptr->owner;
  394. spin_lock_bh(&n_ptr->lock);
  395. tipc_link_reset(l_ptr);
  396. spin_unlock_bh(&n_ptr->lock);
  397. }
  398. spin_unlock_bh(&b_ptr->lock);
  399. read_unlock_bh(&tipc_net_lock);
  400. return 0;
  401. }
  402. /**
  403. * bearer_disable
  404. *
  405. * Note: This routine assumes caller holds tipc_net_lock.
  406. */
  407. static void bearer_disable(struct tipc_bearer *b_ptr)
  408. {
  409. struct tipc_link *l_ptr;
  410. struct tipc_link *temp_l_ptr;
  411. pr_info("Disabling bearer <%s>\n", b_ptr->name);
  412. spin_lock_bh(&b_ptr->lock);
  413. b_ptr->blocked = 1;
  414. b_ptr->media->disable_bearer(b_ptr);
  415. list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
  416. tipc_link_delete(l_ptr);
  417. }
  418. if (b_ptr->link_req)
  419. tipc_disc_delete(b_ptr->link_req);
  420. spin_unlock_bh(&b_ptr->lock);
  421. memset(b_ptr, 0, sizeof(struct tipc_bearer));
  422. }
  423. int tipc_disable_bearer(const char *name)
  424. {
  425. struct tipc_bearer *b_ptr;
  426. int res;
  427. write_lock_bh(&tipc_net_lock);
  428. b_ptr = tipc_bearer_find(name);
  429. if (b_ptr == NULL) {
  430. pr_warn("Attempt to disable unknown bearer <%s>\n", name);
  431. res = -EINVAL;
  432. } else {
  433. bearer_disable(b_ptr);
  434. res = 0;
  435. }
  436. write_unlock_bh(&tipc_net_lock);
  437. return res;
  438. }
  439. void tipc_bearer_stop(void)
  440. {
  441. u32 i;
  442. for (i = 0; i < MAX_BEARERS; i++) {
  443. if (tipc_bearers[i].active)
  444. bearer_disable(&tipc_bearers[i]);
  445. }
  446. media_count = 0;
  447. }