bearer.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. int __tipc_nl_add_bearer(struct tipc_nl_msg *msg, struct tipc_bearer *bearer)
  572. {
  573. void *hdr;
  574. struct nlattr *attrs;
  575. struct nlattr *prop;
  576. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
  577. NLM_F_MULTI, TIPC_NL_BEARER_GET);
  578. if (!hdr)
  579. return -EMSGSIZE;
  580. attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
  581. if (!attrs)
  582. goto msg_full;
  583. if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
  584. goto attr_msg_full;
  585. prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
  586. if (!prop)
  587. goto prop_msg_full;
  588. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
  589. goto prop_msg_full;
  590. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
  591. goto prop_msg_full;
  592. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
  593. goto prop_msg_full;
  594. nla_nest_end(msg->skb, prop);
  595. nla_nest_end(msg->skb, attrs);
  596. genlmsg_end(msg->skb, hdr);
  597. return 0;
  598. prop_msg_full:
  599. nla_nest_cancel(msg->skb, prop);
  600. attr_msg_full:
  601. nla_nest_cancel(msg->skb, attrs);
  602. msg_full:
  603. genlmsg_cancel(msg->skb, hdr);
  604. return -EMSGSIZE;
  605. }
  606. int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
  607. {
  608. int err;
  609. int i = cb->args[0];
  610. struct tipc_bearer *bearer;
  611. struct tipc_nl_msg msg;
  612. if (i == MAX_BEARERS)
  613. return 0;
  614. msg.skb = skb;
  615. msg.portid = NETLINK_CB(cb->skb).portid;
  616. msg.seq = cb->nlh->nlmsg_seq;
  617. rtnl_lock();
  618. for (i = 0; i < MAX_BEARERS; i++) {
  619. bearer = rtnl_dereference(bearer_list[i]);
  620. if (!bearer)
  621. continue;
  622. err = __tipc_nl_add_bearer(&msg, bearer);
  623. if (err)
  624. break;
  625. }
  626. rtnl_unlock();
  627. cb->args[0] = i;
  628. return skb->len;
  629. }
  630. int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
  631. {
  632. int err;
  633. char *name;
  634. struct sk_buff *rep;
  635. struct tipc_bearer *bearer;
  636. struct tipc_nl_msg msg;
  637. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  638. if (!info->attrs[TIPC_NLA_BEARER])
  639. return -EINVAL;
  640. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  641. info->attrs[TIPC_NLA_BEARER],
  642. tipc_nl_bearer_policy);
  643. if (err)
  644. return err;
  645. if (!attrs[TIPC_NLA_BEARER_NAME])
  646. return -EINVAL;
  647. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  648. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  649. if (!rep)
  650. return -ENOMEM;
  651. msg.skb = rep;
  652. msg.portid = info->snd_portid;
  653. msg.seq = info->snd_seq;
  654. rtnl_lock();
  655. bearer = tipc_bearer_find(name);
  656. if (!bearer) {
  657. err = -EINVAL;
  658. goto err_out;
  659. }
  660. err = __tipc_nl_add_bearer(&msg, bearer);
  661. if (err)
  662. goto err_out;
  663. rtnl_unlock();
  664. return genlmsg_reply(rep, info);
  665. err_out:
  666. rtnl_unlock();
  667. nlmsg_free(rep);
  668. return err;
  669. }
  670. int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
  671. {
  672. int err;
  673. char *name;
  674. struct tipc_bearer *bearer;
  675. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  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);
  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. rtnl_lock();
  687. bearer = tipc_bearer_find(name);
  688. if (!bearer) {
  689. rtnl_unlock();
  690. return -EINVAL;
  691. }
  692. bearer_disable(bearer, false);
  693. rtnl_unlock();
  694. return 0;
  695. }
  696. int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
  697. {
  698. int err;
  699. char *bearer;
  700. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  701. u32 domain;
  702. u32 prio;
  703. prio = TIPC_MEDIA_LINK_PRI;
  704. domain = tipc_own_addr & TIPC_CLUSTER_MASK;
  705. if (!info->attrs[TIPC_NLA_BEARER])
  706. return -EINVAL;
  707. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  708. info->attrs[TIPC_NLA_BEARER],
  709. tipc_nl_bearer_policy);
  710. if (err)
  711. return err;
  712. if (!attrs[TIPC_NLA_BEARER_NAME])
  713. return -EINVAL;
  714. bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  715. if (attrs[TIPC_NLA_BEARER_DOMAIN])
  716. domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
  717. if (attrs[TIPC_NLA_BEARER_PROP]) {
  718. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  719. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  720. props);
  721. if (err)
  722. return err;
  723. if (props[TIPC_NLA_PROP_PRIO])
  724. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  725. }
  726. rtnl_lock();
  727. err = tipc_enable_bearer(bearer, domain, prio);
  728. if (err) {
  729. rtnl_unlock();
  730. return err;
  731. }
  732. rtnl_unlock();
  733. return 0;
  734. }
  735. int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
  736. {
  737. int err;
  738. char *name;
  739. struct tipc_bearer *b;
  740. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  741. if (!info->attrs[TIPC_NLA_BEARER])
  742. return -EINVAL;
  743. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  744. info->attrs[TIPC_NLA_BEARER],
  745. tipc_nl_bearer_policy);
  746. if (err)
  747. return err;
  748. if (!attrs[TIPC_NLA_BEARER_NAME])
  749. return -EINVAL;
  750. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  751. rtnl_lock();
  752. b = tipc_bearer_find(name);
  753. if (!b) {
  754. rtnl_unlock();
  755. return -EINVAL;
  756. }
  757. if (attrs[TIPC_NLA_BEARER_PROP]) {
  758. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  759. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  760. props);
  761. if (err) {
  762. rtnl_unlock();
  763. return err;
  764. }
  765. if (props[TIPC_NLA_PROP_TOL])
  766. b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  767. if (props[TIPC_NLA_PROP_PRIO])
  768. b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  769. if (props[TIPC_NLA_PROP_WIN])
  770. b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  771. }
  772. rtnl_unlock();
  773. return 0;
  774. }
  775. int __tipc_nl_add_media(struct tipc_nl_msg *msg, struct tipc_media *media)
  776. {
  777. void *hdr;
  778. struct nlattr *attrs;
  779. struct nlattr *prop;
  780. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
  781. NLM_F_MULTI, TIPC_NL_MEDIA_GET);
  782. if (!hdr)
  783. return -EMSGSIZE;
  784. attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
  785. if (!attrs)
  786. goto msg_full;
  787. if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
  788. goto attr_msg_full;
  789. prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
  790. if (!prop)
  791. goto prop_msg_full;
  792. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
  793. goto prop_msg_full;
  794. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
  795. goto prop_msg_full;
  796. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
  797. goto prop_msg_full;
  798. nla_nest_end(msg->skb, prop);
  799. nla_nest_end(msg->skb, attrs);
  800. genlmsg_end(msg->skb, hdr);
  801. return 0;
  802. prop_msg_full:
  803. nla_nest_cancel(msg->skb, prop);
  804. attr_msg_full:
  805. nla_nest_cancel(msg->skb, attrs);
  806. msg_full:
  807. genlmsg_cancel(msg->skb, hdr);
  808. return -EMSGSIZE;
  809. }
  810. int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
  811. {
  812. int err;
  813. int i = cb->args[0];
  814. struct tipc_nl_msg msg;
  815. if (i == MAX_MEDIA)
  816. return 0;
  817. msg.skb = skb;
  818. msg.portid = NETLINK_CB(cb->skb).portid;
  819. msg.seq = cb->nlh->nlmsg_seq;
  820. rtnl_lock();
  821. for (; media_info_array[i] != NULL; i++) {
  822. err = __tipc_nl_add_media(&msg, media_info_array[i]);
  823. if (err)
  824. break;
  825. }
  826. rtnl_unlock();
  827. cb->args[0] = i;
  828. return skb->len;
  829. }
  830. int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
  831. {
  832. int err;
  833. char *name;
  834. struct tipc_nl_msg msg;
  835. struct tipc_media *media;
  836. struct sk_buff *rep;
  837. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  838. if (!info->attrs[TIPC_NLA_MEDIA])
  839. return -EINVAL;
  840. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  841. info->attrs[TIPC_NLA_MEDIA],
  842. tipc_nl_media_policy);
  843. if (err)
  844. return err;
  845. if (!attrs[TIPC_NLA_MEDIA_NAME])
  846. return -EINVAL;
  847. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  848. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  849. if (!rep)
  850. return -ENOMEM;
  851. msg.skb = rep;
  852. msg.portid = info->snd_portid;
  853. msg.seq = info->snd_seq;
  854. rtnl_lock();
  855. media = tipc_media_find(name);
  856. if (!media) {
  857. err = -EINVAL;
  858. goto err_out;
  859. }
  860. err = __tipc_nl_add_media(&msg, media);
  861. if (err)
  862. goto err_out;
  863. rtnl_unlock();
  864. return genlmsg_reply(rep, info);
  865. err_out:
  866. rtnl_unlock();
  867. nlmsg_free(rep);
  868. return err;
  869. }
  870. int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
  871. {
  872. int err;
  873. char *name;
  874. struct tipc_media *m;
  875. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  876. if (!info->attrs[TIPC_NLA_MEDIA])
  877. return -EINVAL;
  878. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  879. info->attrs[TIPC_NLA_MEDIA],
  880. tipc_nl_media_policy);
  881. if (!attrs[TIPC_NLA_MEDIA_NAME])
  882. return -EINVAL;
  883. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  884. rtnl_lock();
  885. m = tipc_media_find(name);
  886. if (!m) {
  887. rtnl_unlock();
  888. return -EINVAL;
  889. }
  890. if (attrs[TIPC_NLA_MEDIA_PROP]) {
  891. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  892. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
  893. props);
  894. if (err) {
  895. rtnl_unlock();
  896. return err;
  897. }
  898. if (props[TIPC_NLA_PROP_TOL])
  899. m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  900. if (props[TIPC_NLA_PROP_PRIO])
  901. m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  902. if (props[TIPC_NLA_PROP_WIN])
  903. m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  904. }
  905. rtnl_unlock();
  906. return 0;
  907. }