bearer.c 25 KB

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