bearer.c 13 KB

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