bearer.c 25 KB

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