bearer.c 25 KB

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