bearer.c 28 KB

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