bearer.c 25 KB

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