bearer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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 = TIPC_BROADCAST_SUPPORT;
  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. bool tipc_bearer_bcast_support(struct net *net, u32 bearer_id)
  431. {
  432. bool supp = false;
  433. struct tipc_bearer *b;
  434. rcu_read_lock();
  435. b = bearer_get(net, bearer_id);
  436. if (b)
  437. supp = (b->bcast_addr.broadcast == TIPC_BROADCAST_SUPPORT);
  438. rcu_read_unlock();
  439. return supp;
  440. }
  441. int tipc_bearer_mtu(struct net *net, u32 bearer_id)
  442. {
  443. int mtu = 0;
  444. struct tipc_bearer *b;
  445. rcu_read_lock();
  446. b = rcu_dereference_rtnl(tipc_net(net)->bearer_list[bearer_id]);
  447. if (b)
  448. mtu = b->mtu;
  449. rcu_read_unlock();
  450. return mtu;
  451. }
  452. /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
  453. */
  454. void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
  455. struct sk_buff *skb,
  456. struct tipc_media_addr *dest)
  457. {
  458. struct tipc_msg *hdr = buf_msg(skb);
  459. struct tipc_bearer *b;
  460. rcu_read_lock();
  461. b = bearer_get(net, bearer_id);
  462. if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr))))
  463. b->media->send_msg(net, skb, b, dest);
  464. else
  465. kfree_skb(skb);
  466. rcu_read_unlock();
  467. }
  468. /* tipc_bearer_xmit() -send buffer to destination over bearer
  469. */
  470. void tipc_bearer_xmit(struct net *net, u32 bearer_id,
  471. struct sk_buff_head *xmitq,
  472. struct tipc_media_addr *dst)
  473. {
  474. struct tipc_bearer *b;
  475. struct sk_buff *skb, *tmp;
  476. if (skb_queue_empty(xmitq))
  477. return;
  478. rcu_read_lock();
  479. b = bearer_get(net, bearer_id);
  480. if (unlikely(!b))
  481. __skb_queue_purge(xmitq);
  482. skb_queue_walk_safe(xmitq, skb, tmp) {
  483. __skb_dequeue(xmitq);
  484. if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb))))
  485. b->media->send_msg(net, skb, b, dst);
  486. else
  487. kfree_skb(skb);
  488. }
  489. rcu_read_unlock();
  490. }
  491. /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
  492. */
  493. void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
  494. struct sk_buff_head *xmitq)
  495. {
  496. struct tipc_net *tn = tipc_net(net);
  497. int net_id = tn->net_id;
  498. struct tipc_bearer *b;
  499. struct sk_buff *skb, *tmp;
  500. struct tipc_msg *hdr;
  501. rcu_read_lock();
  502. b = bearer_get(net, bearer_id);
  503. if (unlikely(!b || !test_bit(0, &b->up)))
  504. __skb_queue_purge(xmitq);
  505. skb_queue_walk_safe(xmitq, skb, tmp) {
  506. hdr = buf_msg(skb);
  507. msg_set_non_seq(hdr, 1);
  508. msg_set_mc_netid(hdr, net_id);
  509. __skb_dequeue(xmitq);
  510. b->media->send_msg(net, skb, b, &b->bcast_addr);
  511. }
  512. rcu_read_unlock();
  513. }
  514. /**
  515. * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
  516. * @buf: the received packet
  517. * @dev: the net device that the packet was received on
  518. * @pt: the packet_type structure which was used to register this handler
  519. * @orig_dev: the original receive net device in case the device is a bond
  520. *
  521. * Accept only packets explicitly sent to this node, or broadcast packets;
  522. * ignores packets sent using interface multicast, and traffic sent to other
  523. * nodes (which can happen if interface is running in promiscuous mode).
  524. */
  525. static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
  526. struct packet_type *pt, struct net_device *orig_dev)
  527. {
  528. struct tipc_bearer *b;
  529. rcu_read_lock();
  530. b = rcu_dereference_rtnl(dev->tipc_ptr);
  531. if (likely(b && test_bit(0, &b->up) &&
  532. (skb->pkt_type <= PACKET_BROADCAST))) {
  533. skb->next = NULL;
  534. tipc_rcv(dev_net(dev), skb, b);
  535. rcu_read_unlock();
  536. return NET_RX_SUCCESS;
  537. }
  538. rcu_read_unlock();
  539. kfree_skb(skb);
  540. return NET_RX_DROP;
  541. }
  542. /**
  543. * tipc_l2_device_event - handle device events from network device
  544. * @nb: the context of the notification
  545. * @evt: the type of event
  546. * @ptr: the net device that the event was on
  547. *
  548. * This function is called by the Ethernet driver in case of link
  549. * change event.
  550. */
  551. static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
  552. void *ptr)
  553. {
  554. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  555. struct net *net = dev_net(dev);
  556. struct tipc_bearer *b;
  557. b = rtnl_dereference(dev->tipc_ptr);
  558. if (!b)
  559. return NOTIFY_DONE;
  560. switch (evt) {
  561. case NETDEV_CHANGE:
  562. if (netif_carrier_ok(dev))
  563. break;
  564. case NETDEV_UP:
  565. test_and_set_bit_lock(0, &b->up);
  566. break;
  567. case NETDEV_GOING_DOWN:
  568. clear_bit_unlock(0, &b->up);
  569. tipc_reset_bearer(net, b);
  570. break;
  571. case NETDEV_CHANGEMTU:
  572. if (tipc_mtu_bad(dev, 0)) {
  573. bearer_disable(net, b);
  574. break;
  575. }
  576. b->mtu = dev->mtu;
  577. tipc_reset_bearer(net, b);
  578. break;
  579. case NETDEV_CHANGEADDR:
  580. b->media->raw2addr(b, &b->addr,
  581. (char *)dev->dev_addr);
  582. tipc_reset_bearer(net, b);
  583. break;
  584. case NETDEV_UNREGISTER:
  585. case NETDEV_CHANGENAME:
  586. bearer_disable(dev_net(dev), b);
  587. break;
  588. }
  589. return NOTIFY_OK;
  590. }
  591. static struct packet_type tipc_packet_type __read_mostly = {
  592. .type = htons(ETH_P_TIPC),
  593. .func = tipc_l2_rcv_msg,
  594. };
  595. static struct notifier_block notifier = {
  596. .notifier_call = tipc_l2_device_event,
  597. .priority = 0,
  598. };
  599. int tipc_bearer_setup(void)
  600. {
  601. int err;
  602. err = register_netdevice_notifier(&notifier);
  603. if (err)
  604. return err;
  605. dev_add_pack(&tipc_packet_type);
  606. return 0;
  607. }
  608. void tipc_bearer_cleanup(void)
  609. {
  610. unregister_netdevice_notifier(&notifier);
  611. dev_remove_pack(&tipc_packet_type);
  612. }
  613. void tipc_bearer_stop(struct net *net)
  614. {
  615. struct tipc_net *tn = net_generic(net, tipc_net_id);
  616. struct tipc_bearer *b;
  617. u32 i;
  618. for (i = 0; i < MAX_BEARERS; i++) {
  619. b = rtnl_dereference(tn->bearer_list[i]);
  620. if (b) {
  621. bearer_disable(net, b);
  622. tn->bearer_list[i] = NULL;
  623. }
  624. }
  625. }
  626. /* Caller should hold rtnl_lock to protect the bearer */
  627. static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
  628. struct tipc_bearer *bearer, int nlflags)
  629. {
  630. void *hdr;
  631. struct nlattr *attrs;
  632. struct nlattr *prop;
  633. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  634. nlflags, TIPC_NL_BEARER_GET);
  635. if (!hdr)
  636. return -EMSGSIZE;
  637. attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
  638. if (!attrs)
  639. goto msg_full;
  640. if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
  641. goto attr_msg_full;
  642. prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
  643. if (!prop)
  644. goto prop_msg_full;
  645. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
  646. goto prop_msg_full;
  647. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
  648. goto prop_msg_full;
  649. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
  650. goto prop_msg_full;
  651. nla_nest_end(msg->skb, prop);
  652. #ifdef CONFIG_TIPC_MEDIA_UDP
  653. if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
  654. if (tipc_udp_nl_add_bearer_data(msg, bearer))
  655. goto attr_msg_full;
  656. }
  657. #endif
  658. nla_nest_end(msg->skb, attrs);
  659. genlmsg_end(msg->skb, hdr);
  660. return 0;
  661. prop_msg_full:
  662. nla_nest_cancel(msg->skb, prop);
  663. attr_msg_full:
  664. nla_nest_cancel(msg->skb, attrs);
  665. msg_full:
  666. genlmsg_cancel(msg->skb, hdr);
  667. return -EMSGSIZE;
  668. }
  669. int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
  670. {
  671. int err;
  672. int i = cb->args[0];
  673. struct tipc_bearer *bearer;
  674. struct tipc_nl_msg msg;
  675. struct net *net = sock_net(skb->sk);
  676. struct tipc_net *tn = net_generic(net, tipc_net_id);
  677. if (i == MAX_BEARERS)
  678. return 0;
  679. msg.skb = skb;
  680. msg.portid = NETLINK_CB(cb->skb).portid;
  681. msg.seq = cb->nlh->nlmsg_seq;
  682. rtnl_lock();
  683. for (i = 0; i < MAX_BEARERS; i++) {
  684. bearer = rtnl_dereference(tn->bearer_list[i]);
  685. if (!bearer)
  686. continue;
  687. err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
  688. if (err)
  689. break;
  690. }
  691. rtnl_unlock();
  692. cb->args[0] = i;
  693. return skb->len;
  694. }
  695. int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
  696. {
  697. int err;
  698. char *name;
  699. struct sk_buff *rep;
  700. struct tipc_bearer *bearer;
  701. struct tipc_nl_msg msg;
  702. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  703. struct net *net = genl_info_net(info);
  704. if (!info->attrs[TIPC_NLA_BEARER])
  705. return -EINVAL;
  706. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  707. info->attrs[TIPC_NLA_BEARER],
  708. tipc_nl_bearer_policy, info->extack);
  709. if (err)
  710. return err;
  711. if (!attrs[TIPC_NLA_BEARER_NAME])
  712. return -EINVAL;
  713. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  714. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  715. if (!rep)
  716. return -ENOMEM;
  717. msg.skb = rep;
  718. msg.portid = info->snd_portid;
  719. msg.seq = info->snd_seq;
  720. rtnl_lock();
  721. bearer = tipc_bearer_find(net, name);
  722. if (!bearer) {
  723. err = -EINVAL;
  724. goto err_out;
  725. }
  726. err = __tipc_nl_add_bearer(&msg, bearer, 0);
  727. if (err)
  728. goto err_out;
  729. rtnl_unlock();
  730. return genlmsg_reply(rep, info);
  731. err_out:
  732. rtnl_unlock();
  733. nlmsg_free(rep);
  734. return err;
  735. }
  736. int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
  737. {
  738. int err;
  739. char *name;
  740. struct tipc_bearer *bearer;
  741. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  742. struct net *net = sock_net(skb->sk);
  743. if (!info->attrs[TIPC_NLA_BEARER])
  744. return -EINVAL;
  745. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  746. info->attrs[TIPC_NLA_BEARER],
  747. tipc_nl_bearer_policy, info->extack);
  748. if (err)
  749. return err;
  750. if (!attrs[TIPC_NLA_BEARER_NAME])
  751. return -EINVAL;
  752. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  753. rtnl_lock();
  754. bearer = tipc_bearer_find(net, name);
  755. if (!bearer) {
  756. rtnl_unlock();
  757. return -EINVAL;
  758. }
  759. bearer_disable(net, bearer);
  760. rtnl_unlock();
  761. return 0;
  762. }
  763. int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
  764. {
  765. int err;
  766. char *bearer;
  767. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  768. struct net *net = sock_net(skb->sk);
  769. struct tipc_net *tn = net_generic(net, tipc_net_id);
  770. u32 domain;
  771. u32 prio;
  772. prio = TIPC_MEDIA_LINK_PRI;
  773. domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK;
  774. if (!info->attrs[TIPC_NLA_BEARER])
  775. return -EINVAL;
  776. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  777. info->attrs[TIPC_NLA_BEARER],
  778. tipc_nl_bearer_policy, info->extack);
  779. if (err)
  780. return err;
  781. if (!attrs[TIPC_NLA_BEARER_NAME])
  782. return -EINVAL;
  783. bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  784. if (attrs[TIPC_NLA_BEARER_DOMAIN])
  785. domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
  786. if (attrs[TIPC_NLA_BEARER_PROP]) {
  787. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  788. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  789. props);
  790. if (err)
  791. return err;
  792. if (props[TIPC_NLA_PROP_PRIO])
  793. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  794. }
  795. rtnl_lock();
  796. err = tipc_enable_bearer(net, bearer, domain, prio, attrs);
  797. if (err) {
  798. rtnl_unlock();
  799. return err;
  800. }
  801. rtnl_unlock();
  802. return 0;
  803. }
  804. int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
  805. {
  806. int err;
  807. char *name;
  808. struct tipc_bearer *b;
  809. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  810. struct net *net = sock_net(skb->sk);
  811. if (!info->attrs[TIPC_NLA_BEARER])
  812. return -EINVAL;
  813. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  814. info->attrs[TIPC_NLA_BEARER],
  815. tipc_nl_bearer_policy, info->extack);
  816. if (err)
  817. return err;
  818. if (!attrs[TIPC_NLA_BEARER_NAME])
  819. return -EINVAL;
  820. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  821. rtnl_lock();
  822. b = tipc_bearer_find(net, name);
  823. if (!b) {
  824. rtnl_unlock();
  825. return -EINVAL;
  826. }
  827. #ifdef CONFIG_TIPC_MEDIA_UDP
  828. if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
  829. err = tipc_udp_nl_bearer_add(b,
  830. attrs[TIPC_NLA_BEARER_UDP_OPTS]);
  831. if (err) {
  832. rtnl_unlock();
  833. return err;
  834. }
  835. }
  836. #endif
  837. rtnl_unlock();
  838. return 0;
  839. }
  840. int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
  841. {
  842. int err;
  843. char *name;
  844. struct tipc_bearer *b;
  845. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  846. struct net *net = sock_net(skb->sk);
  847. if (!info->attrs[TIPC_NLA_BEARER])
  848. return -EINVAL;
  849. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  850. info->attrs[TIPC_NLA_BEARER],
  851. tipc_nl_bearer_policy, info->extack);
  852. if (err)
  853. return err;
  854. if (!attrs[TIPC_NLA_BEARER_NAME])
  855. return -EINVAL;
  856. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  857. rtnl_lock();
  858. b = tipc_bearer_find(net, name);
  859. if (!b) {
  860. rtnl_unlock();
  861. return -EINVAL;
  862. }
  863. if (attrs[TIPC_NLA_BEARER_PROP]) {
  864. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  865. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  866. props);
  867. if (err) {
  868. rtnl_unlock();
  869. return err;
  870. }
  871. if (props[TIPC_NLA_PROP_TOL])
  872. b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  873. if (props[TIPC_NLA_PROP_PRIO])
  874. b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  875. if (props[TIPC_NLA_PROP_WIN])
  876. b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  877. }
  878. rtnl_unlock();
  879. return 0;
  880. }
  881. static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
  882. struct tipc_media *media, int nlflags)
  883. {
  884. void *hdr;
  885. struct nlattr *attrs;
  886. struct nlattr *prop;
  887. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  888. nlflags, TIPC_NL_MEDIA_GET);
  889. if (!hdr)
  890. return -EMSGSIZE;
  891. attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
  892. if (!attrs)
  893. goto msg_full;
  894. if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
  895. goto attr_msg_full;
  896. prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
  897. if (!prop)
  898. goto prop_msg_full;
  899. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
  900. goto prop_msg_full;
  901. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
  902. goto prop_msg_full;
  903. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
  904. goto prop_msg_full;
  905. nla_nest_end(msg->skb, prop);
  906. nla_nest_end(msg->skb, attrs);
  907. genlmsg_end(msg->skb, hdr);
  908. return 0;
  909. prop_msg_full:
  910. nla_nest_cancel(msg->skb, prop);
  911. attr_msg_full:
  912. nla_nest_cancel(msg->skb, attrs);
  913. msg_full:
  914. genlmsg_cancel(msg->skb, hdr);
  915. return -EMSGSIZE;
  916. }
  917. int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
  918. {
  919. int err;
  920. int i = cb->args[0];
  921. struct tipc_nl_msg msg;
  922. if (i == MAX_MEDIA)
  923. return 0;
  924. msg.skb = skb;
  925. msg.portid = NETLINK_CB(cb->skb).portid;
  926. msg.seq = cb->nlh->nlmsg_seq;
  927. rtnl_lock();
  928. for (; media_info_array[i] != NULL; i++) {
  929. err = __tipc_nl_add_media(&msg, media_info_array[i],
  930. NLM_F_MULTI);
  931. if (err)
  932. break;
  933. }
  934. rtnl_unlock();
  935. cb->args[0] = i;
  936. return skb->len;
  937. }
  938. int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
  939. {
  940. int err;
  941. char *name;
  942. struct tipc_nl_msg msg;
  943. struct tipc_media *media;
  944. struct sk_buff *rep;
  945. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  946. if (!info->attrs[TIPC_NLA_MEDIA])
  947. return -EINVAL;
  948. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  949. info->attrs[TIPC_NLA_MEDIA],
  950. tipc_nl_media_policy, info->extack);
  951. if (err)
  952. return err;
  953. if (!attrs[TIPC_NLA_MEDIA_NAME])
  954. return -EINVAL;
  955. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  956. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  957. if (!rep)
  958. return -ENOMEM;
  959. msg.skb = rep;
  960. msg.portid = info->snd_portid;
  961. msg.seq = info->snd_seq;
  962. rtnl_lock();
  963. media = tipc_media_find(name);
  964. if (!media) {
  965. err = -EINVAL;
  966. goto err_out;
  967. }
  968. err = __tipc_nl_add_media(&msg, media, 0);
  969. if (err)
  970. goto err_out;
  971. rtnl_unlock();
  972. return genlmsg_reply(rep, info);
  973. err_out:
  974. rtnl_unlock();
  975. nlmsg_free(rep);
  976. return err;
  977. }
  978. int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
  979. {
  980. int err;
  981. char *name;
  982. struct tipc_media *m;
  983. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  984. if (!info->attrs[TIPC_NLA_MEDIA])
  985. return -EINVAL;
  986. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  987. info->attrs[TIPC_NLA_MEDIA],
  988. tipc_nl_media_policy, info->extack);
  989. if (!attrs[TIPC_NLA_MEDIA_NAME])
  990. return -EINVAL;
  991. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  992. rtnl_lock();
  993. m = tipc_media_find(name);
  994. if (!m) {
  995. rtnl_unlock();
  996. return -EINVAL;
  997. }
  998. if (attrs[TIPC_NLA_MEDIA_PROP]) {
  999. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1000. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
  1001. props);
  1002. if (err) {
  1003. rtnl_unlock();
  1004. return err;
  1005. }
  1006. if (props[TIPC_NLA_PROP_TOL])
  1007. m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1008. if (props[TIPC_NLA_PROP_PRIO])
  1009. m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1010. if (props[TIPC_NLA_PROP_WIN])
  1011. m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1012. }
  1013. rtnl_unlock();
  1014. return 0;
  1015. }