bearer.c 26 KB

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