bearer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. kfree(b);
  290. return -EINVAL;
  291. }
  292. b->identity = bearer_id;
  293. b->tolerance = m->tolerance;
  294. b->window = m->window;
  295. b->domain = disc_domain;
  296. b->net_plane = bearer_id + 'A';
  297. b->priority = priority;
  298. test_and_set_bit_lock(0, &b->up);
  299. res = tipc_disc_create(net, b, &b->bcast_addr, &skb);
  300. if (res) {
  301. bearer_disable(net, b);
  302. pr_warn("Bearer <%s> rejected, discovery object creation failed\n",
  303. name);
  304. return -EINVAL;
  305. }
  306. rcu_assign_pointer(tn->bearer_list[bearer_id], b);
  307. if (skb)
  308. tipc_bearer_xmit_skb(net, bearer_id, skb, &b->bcast_addr);
  309. if (tipc_mon_create(net, bearer_id)) {
  310. bearer_disable(net, b);
  311. return -ENOMEM;
  312. }
  313. pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
  314. name,
  315. tipc_addr_string_fill(addr_string, disc_domain), priority);
  316. return res;
  317. }
  318. /**
  319. * tipc_reset_bearer - Reset all links established over this bearer
  320. */
  321. static int tipc_reset_bearer(struct net *net, struct tipc_bearer *b)
  322. {
  323. pr_info("Resetting bearer <%s>\n", b->name);
  324. tipc_node_delete_links(net, b->identity);
  325. tipc_disc_reset(net, b);
  326. return 0;
  327. }
  328. /* tipc_bearer_reset_all - reset all links on all bearers
  329. */
  330. void tipc_bearer_reset_all(struct net *net)
  331. {
  332. struct tipc_bearer *b;
  333. int i;
  334. for (i = 0; i < MAX_BEARERS; i++) {
  335. b = bearer_get(net, i);
  336. if (b)
  337. clear_bit_unlock(0, &b->up);
  338. }
  339. for (i = 0; i < MAX_BEARERS; i++) {
  340. b = bearer_get(net, i);
  341. if (b)
  342. tipc_reset_bearer(net, b);
  343. }
  344. for (i = 0; i < MAX_BEARERS; i++) {
  345. b = bearer_get(net, i);
  346. if (b)
  347. test_and_set_bit_lock(0, &b->up);
  348. }
  349. }
  350. /**
  351. * bearer_disable
  352. *
  353. * Note: This routine assumes caller holds RTNL lock.
  354. */
  355. static void bearer_disable(struct net *net, struct tipc_bearer *b)
  356. {
  357. struct tipc_net *tn = tipc_net(net);
  358. int bearer_id = b->identity;
  359. pr_info("Disabling bearer <%s>\n", b->name);
  360. clear_bit_unlock(0, &b->up);
  361. tipc_node_delete_links(net, bearer_id);
  362. b->media->disable_media(b);
  363. RCU_INIT_POINTER(b->media_ptr, NULL);
  364. if (b->link_req)
  365. tipc_disc_delete(b->link_req);
  366. RCU_INIT_POINTER(tn->bearer_list[bearer_id], NULL);
  367. kfree_rcu(b, rcu);
  368. tipc_mon_delete(net, bearer_id);
  369. }
  370. int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
  371. struct nlattr *attr[])
  372. {
  373. struct net_device *dev;
  374. char *driver_name = strchr((const char *)b->name, ':') + 1;
  375. /* Find device with specified name */
  376. dev = dev_get_by_name(net, driver_name);
  377. if (!dev)
  378. return -ENODEV;
  379. if (tipc_mtu_bad(dev, 0)) {
  380. dev_put(dev);
  381. return -EINVAL;
  382. }
  383. /* Associate TIPC bearer with L2 bearer */
  384. rcu_assign_pointer(b->media_ptr, dev);
  385. memset(&b->bcast_addr, 0, sizeof(b->bcast_addr));
  386. memcpy(b->bcast_addr.value, dev->broadcast, b->media->hwaddr_len);
  387. b->bcast_addr.media_id = b->media->type_id;
  388. b->bcast_addr.broadcast = 1;
  389. b->mtu = dev->mtu;
  390. b->media->raw2addr(b, &b->addr, (char *)dev->dev_addr);
  391. rcu_assign_pointer(dev->tipc_ptr, b);
  392. return 0;
  393. }
  394. /* tipc_disable_l2_media - detach TIPC bearer from an L2 interface
  395. *
  396. * Mark L2 bearer as inactive so that incoming buffers are thrown away
  397. */
  398. void tipc_disable_l2_media(struct tipc_bearer *b)
  399. {
  400. struct net_device *dev;
  401. dev = (struct net_device *)rtnl_dereference(b->media_ptr);
  402. RCU_INIT_POINTER(dev->tipc_ptr, NULL);
  403. synchronize_net();
  404. dev_put(dev);
  405. }
  406. /**
  407. * tipc_l2_send_msg - send a TIPC packet out over an L2 interface
  408. * @skb: the packet to be sent
  409. * @b: the bearer through which the packet is to be sent
  410. * @dest: peer destination address
  411. */
  412. int tipc_l2_send_msg(struct net *net, struct sk_buff *skb,
  413. struct tipc_bearer *b, struct tipc_media_addr *dest)
  414. {
  415. struct net_device *dev;
  416. int delta;
  417. dev = (struct net_device *)rcu_dereference_rtnl(b->media_ptr);
  418. if (!dev)
  419. return 0;
  420. delta = SKB_DATA_ALIGN(dev->hard_header_len - skb_headroom(skb));
  421. if ((delta > 0) && pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) {
  422. kfree_skb(skb);
  423. return 0;
  424. }
  425. skb_reset_network_header(skb);
  426. skb->dev = dev;
  427. skb->protocol = htons(ETH_P_TIPC);
  428. dev_hard_header(skb, dev, ETH_P_TIPC, dest->value,
  429. dev->dev_addr, skb->len);
  430. dev_queue_xmit(skb);
  431. return 0;
  432. }
  433. int tipc_bearer_mtu(struct net *net, u32 bearer_id)
  434. {
  435. int mtu = 0;
  436. struct tipc_bearer *b;
  437. rcu_read_lock();
  438. b = rcu_dereference_rtnl(tipc_net(net)->bearer_list[bearer_id]);
  439. if (b)
  440. mtu = b->mtu;
  441. rcu_read_unlock();
  442. return mtu;
  443. }
  444. /* tipc_bearer_xmit_skb - sends buffer to destination over bearer
  445. */
  446. void tipc_bearer_xmit_skb(struct net *net, u32 bearer_id,
  447. struct sk_buff *skb,
  448. struct tipc_media_addr *dest)
  449. {
  450. struct tipc_msg *hdr = buf_msg(skb);
  451. struct tipc_bearer *b;
  452. rcu_read_lock();
  453. b = bearer_get(net, bearer_id);
  454. if (likely(b && (test_bit(0, &b->up) || msg_is_reset(hdr))))
  455. b->media->send_msg(net, skb, b, dest);
  456. else
  457. kfree_skb(skb);
  458. rcu_read_unlock();
  459. }
  460. /* tipc_bearer_xmit() -send buffer to destination over bearer
  461. */
  462. void tipc_bearer_xmit(struct net *net, u32 bearer_id,
  463. struct sk_buff_head *xmitq,
  464. struct tipc_media_addr *dst)
  465. {
  466. struct tipc_bearer *b;
  467. struct sk_buff *skb, *tmp;
  468. if (skb_queue_empty(xmitq))
  469. return;
  470. rcu_read_lock();
  471. b = bearer_get(net, bearer_id);
  472. if (unlikely(!b))
  473. __skb_queue_purge(xmitq);
  474. skb_queue_walk_safe(xmitq, skb, tmp) {
  475. __skb_dequeue(xmitq);
  476. if (likely(test_bit(0, &b->up) || msg_is_reset(buf_msg(skb))))
  477. b->media->send_msg(net, skb, b, dst);
  478. else
  479. kfree_skb(skb);
  480. }
  481. rcu_read_unlock();
  482. }
  483. /* tipc_bearer_bc_xmit() - broadcast buffers to all destinations
  484. */
  485. void tipc_bearer_bc_xmit(struct net *net, u32 bearer_id,
  486. struct sk_buff_head *xmitq)
  487. {
  488. struct tipc_net *tn = tipc_net(net);
  489. int net_id = tn->net_id;
  490. struct tipc_bearer *b;
  491. struct sk_buff *skb, *tmp;
  492. struct tipc_msg *hdr;
  493. rcu_read_lock();
  494. b = bearer_get(net, bearer_id);
  495. if (unlikely(!b || !test_bit(0, &b->up)))
  496. __skb_queue_purge(xmitq);
  497. skb_queue_walk_safe(xmitq, skb, tmp) {
  498. hdr = buf_msg(skb);
  499. msg_set_non_seq(hdr, 1);
  500. msg_set_mc_netid(hdr, net_id);
  501. __skb_dequeue(xmitq);
  502. b->media->send_msg(net, skb, b, &b->bcast_addr);
  503. }
  504. rcu_read_unlock();
  505. }
  506. /**
  507. * tipc_l2_rcv_msg - handle incoming TIPC message from an interface
  508. * @buf: the received packet
  509. * @dev: the net device that the packet was received on
  510. * @pt: the packet_type structure which was used to register this handler
  511. * @orig_dev: the original receive net device in case the device is a bond
  512. *
  513. * Accept only packets explicitly sent to this node, or broadcast packets;
  514. * ignores packets sent using interface multicast, and traffic sent to other
  515. * nodes (which can happen if interface is running in promiscuous mode).
  516. */
  517. static int tipc_l2_rcv_msg(struct sk_buff *skb, struct net_device *dev,
  518. struct packet_type *pt, struct net_device *orig_dev)
  519. {
  520. struct tipc_bearer *b;
  521. rcu_read_lock();
  522. b = rcu_dereference_rtnl(dev->tipc_ptr);
  523. if (likely(b && test_bit(0, &b->up) &&
  524. (skb->pkt_type <= PACKET_BROADCAST))) {
  525. skb->next = NULL;
  526. tipc_rcv(dev_net(dev), skb, b);
  527. rcu_read_unlock();
  528. return NET_RX_SUCCESS;
  529. }
  530. rcu_read_unlock();
  531. kfree_skb(skb);
  532. return NET_RX_DROP;
  533. }
  534. /**
  535. * tipc_l2_device_event - handle device events from network device
  536. * @nb: the context of the notification
  537. * @evt: the type of event
  538. * @ptr: the net device that the event was on
  539. *
  540. * This function is called by the Ethernet driver in case of link
  541. * change event.
  542. */
  543. static int tipc_l2_device_event(struct notifier_block *nb, unsigned long evt,
  544. void *ptr)
  545. {
  546. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  547. struct net *net = dev_net(dev);
  548. struct tipc_bearer *b;
  549. b = rtnl_dereference(dev->tipc_ptr);
  550. if (!b)
  551. return NOTIFY_DONE;
  552. switch (evt) {
  553. case NETDEV_CHANGE:
  554. if (netif_carrier_ok(dev))
  555. break;
  556. case NETDEV_UP:
  557. test_and_set_bit_lock(0, &b->up);
  558. break;
  559. case NETDEV_GOING_DOWN:
  560. clear_bit_unlock(0, &b->up);
  561. tipc_reset_bearer(net, b);
  562. break;
  563. case NETDEV_CHANGEMTU:
  564. if (tipc_mtu_bad(dev, 0)) {
  565. bearer_disable(net, b);
  566. break;
  567. }
  568. b->mtu = dev->mtu;
  569. tipc_reset_bearer(net, b);
  570. break;
  571. case NETDEV_CHANGEADDR:
  572. b->media->raw2addr(b, &b->addr,
  573. (char *)dev->dev_addr);
  574. tipc_reset_bearer(net, b);
  575. break;
  576. case NETDEV_UNREGISTER:
  577. case NETDEV_CHANGENAME:
  578. bearer_disable(dev_net(dev), b);
  579. break;
  580. }
  581. return NOTIFY_OK;
  582. }
  583. static struct packet_type tipc_packet_type __read_mostly = {
  584. .type = htons(ETH_P_TIPC),
  585. .func = tipc_l2_rcv_msg,
  586. };
  587. static struct notifier_block notifier = {
  588. .notifier_call = tipc_l2_device_event,
  589. .priority = 0,
  590. };
  591. int tipc_bearer_setup(void)
  592. {
  593. int err;
  594. err = register_netdevice_notifier(&notifier);
  595. if (err)
  596. return err;
  597. dev_add_pack(&tipc_packet_type);
  598. return 0;
  599. }
  600. void tipc_bearer_cleanup(void)
  601. {
  602. unregister_netdevice_notifier(&notifier);
  603. dev_remove_pack(&tipc_packet_type);
  604. }
  605. void tipc_bearer_stop(struct net *net)
  606. {
  607. struct tipc_net *tn = net_generic(net, tipc_net_id);
  608. struct tipc_bearer *b;
  609. u32 i;
  610. for (i = 0; i < MAX_BEARERS; i++) {
  611. b = rtnl_dereference(tn->bearer_list[i]);
  612. if (b) {
  613. bearer_disable(net, b);
  614. tn->bearer_list[i] = NULL;
  615. }
  616. }
  617. }
  618. /* Caller should hold rtnl_lock to protect the bearer */
  619. static int __tipc_nl_add_bearer(struct tipc_nl_msg *msg,
  620. struct tipc_bearer *bearer, int nlflags)
  621. {
  622. void *hdr;
  623. struct nlattr *attrs;
  624. struct nlattr *prop;
  625. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  626. nlflags, TIPC_NL_BEARER_GET);
  627. if (!hdr)
  628. return -EMSGSIZE;
  629. attrs = nla_nest_start(msg->skb, TIPC_NLA_BEARER);
  630. if (!attrs)
  631. goto msg_full;
  632. if (nla_put_string(msg->skb, TIPC_NLA_BEARER_NAME, bearer->name))
  633. goto attr_msg_full;
  634. prop = nla_nest_start(msg->skb, TIPC_NLA_BEARER_PROP);
  635. if (!prop)
  636. goto prop_msg_full;
  637. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, bearer->priority))
  638. goto prop_msg_full;
  639. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, bearer->tolerance))
  640. goto prop_msg_full;
  641. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bearer->window))
  642. goto prop_msg_full;
  643. nla_nest_end(msg->skb, prop);
  644. #ifdef CONFIG_TIPC_MEDIA_UDP
  645. if (bearer->media->type_id == TIPC_MEDIA_TYPE_UDP) {
  646. if (tipc_udp_nl_add_bearer_data(msg, bearer))
  647. goto attr_msg_full;
  648. }
  649. #endif
  650. nla_nest_end(msg->skb, attrs);
  651. genlmsg_end(msg->skb, hdr);
  652. return 0;
  653. prop_msg_full:
  654. nla_nest_cancel(msg->skb, prop);
  655. attr_msg_full:
  656. nla_nest_cancel(msg->skb, attrs);
  657. msg_full:
  658. genlmsg_cancel(msg->skb, hdr);
  659. return -EMSGSIZE;
  660. }
  661. int tipc_nl_bearer_dump(struct sk_buff *skb, struct netlink_callback *cb)
  662. {
  663. int err;
  664. int i = cb->args[0];
  665. struct tipc_bearer *bearer;
  666. struct tipc_nl_msg msg;
  667. struct net *net = sock_net(skb->sk);
  668. struct tipc_net *tn = net_generic(net, tipc_net_id);
  669. if (i == MAX_BEARERS)
  670. return 0;
  671. msg.skb = skb;
  672. msg.portid = NETLINK_CB(cb->skb).portid;
  673. msg.seq = cb->nlh->nlmsg_seq;
  674. rtnl_lock();
  675. for (i = 0; i < MAX_BEARERS; i++) {
  676. bearer = rtnl_dereference(tn->bearer_list[i]);
  677. if (!bearer)
  678. continue;
  679. err = __tipc_nl_add_bearer(&msg, bearer, NLM_F_MULTI);
  680. if (err)
  681. break;
  682. }
  683. rtnl_unlock();
  684. cb->args[0] = i;
  685. return skb->len;
  686. }
  687. int tipc_nl_bearer_get(struct sk_buff *skb, struct genl_info *info)
  688. {
  689. int err;
  690. char *name;
  691. struct sk_buff *rep;
  692. struct tipc_bearer *bearer;
  693. struct tipc_nl_msg msg;
  694. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  695. struct net *net = genl_info_net(info);
  696. if (!info->attrs[TIPC_NLA_BEARER])
  697. return -EINVAL;
  698. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  699. info->attrs[TIPC_NLA_BEARER],
  700. tipc_nl_bearer_policy);
  701. if (err)
  702. return err;
  703. if (!attrs[TIPC_NLA_BEARER_NAME])
  704. return -EINVAL;
  705. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  706. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  707. if (!rep)
  708. return -ENOMEM;
  709. msg.skb = rep;
  710. msg.portid = info->snd_portid;
  711. msg.seq = info->snd_seq;
  712. rtnl_lock();
  713. bearer = tipc_bearer_find(net, name);
  714. if (!bearer) {
  715. err = -EINVAL;
  716. goto err_out;
  717. }
  718. err = __tipc_nl_add_bearer(&msg, bearer, 0);
  719. if (err)
  720. goto err_out;
  721. rtnl_unlock();
  722. return genlmsg_reply(rep, info);
  723. err_out:
  724. rtnl_unlock();
  725. nlmsg_free(rep);
  726. return err;
  727. }
  728. int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info)
  729. {
  730. int err;
  731. char *name;
  732. struct tipc_bearer *bearer;
  733. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  734. struct net *net = sock_net(skb->sk);
  735. if (!info->attrs[TIPC_NLA_BEARER])
  736. return -EINVAL;
  737. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  738. info->attrs[TIPC_NLA_BEARER],
  739. tipc_nl_bearer_policy);
  740. if (err)
  741. return err;
  742. if (!attrs[TIPC_NLA_BEARER_NAME])
  743. return -EINVAL;
  744. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  745. rtnl_lock();
  746. bearer = tipc_bearer_find(net, name);
  747. if (!bearer) {
  748. rtnl_unlock();
  749. return -EINVAL;
  750. }
  751. bearer_disable(net, bearer);
  752. rtnl_unlock();
  753. return 0;
  754. }
  755. int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
  756. {
  757. int err;
  758. char *bearer;
  759. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  760. struct net *net = sock_net(skb->sk);
  761. struct tipc_net *tn = net_generic(net, tipc_net_id);
  762. u32 domain;
  763. u32 prio;
  764. prio = TIPC_MEDIA_LINK_PRI;
  765. domain = tn->own_addr & TIPC_ZONE_CLUSTER_MASK;
  766. if (!info->attrs[TIPC_NLA_BEARER])
  767. return -EINVAL;
  768. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  769. info->attrs[TIPC_NLA_BEARER],
  770. tipc_nl_bearer_policy);
  771. if (err)
  772. return err;
  773. if (!attrs[TIPC_NLA_BEARER_NAME])
  774. return -EINVAL;
  775. bearer = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  776. if (attrs[TIPC_NLA_BEARER_DOMAIN])
  777. domain = nla_get_u32(attrs[TIPC_NLA_BEARER_DOMAIN]);
  778. if (attrs[TIPC_NLA_BEARER_PROP]) {
  779. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  780. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  781. props);
  782. if (err)
  783. return err;
  784. if (props[TIPC_NLA_PROP_PRIO])
  785. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  786. }
  787. rtnl_lock();
  788. err = tipc_enable_bearer(net, bearer, domain, prio, attrs);
  789. if (err) {
  790. rtnl_unlock();
  791. return err;
  792. }
  793. rtnl_unlock();
  794. return 0;
  795. }
  796. int tipc_nl_bearer_add(struct sk_buff *skb, struct genl_info *info)
  797. {
  798. int err;
  799. char *name;
  800. struct tipc_bearer *b;
  801. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  802. struct net *net = sock_net(skb->sk);
  803. if (!info->attrs[TIPC_NLA_BEARER])
  804. return -EINVAL;
  805. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  806. info->attrs[TIPC_NLA_BEARER],
  807. tipc_nl_bearer_policy);
  808. if (err)
  809. return err;
  810. if (!attrs[TIPC_NLA_BEARER_NAME])
  811. return -EINVAL;
  812. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  813. rtnl_lock();
  814. b = tipc_bearer_find(net, name);
  815. if (!b) {
  816. rtnl_unlock();
  817. return -EINVAL;
  818. }
  819. #ifdef CONFIG_TIPC_MEDIA_UDP
  820. if (attrs[TIPC_NLA_BEARER_UDP_OPTS]) {
  821. err = tipc_udp_nl_bearer_add(b,
  822. attrs[TIPC_NLA_BEARER_UDP_OPTS]);
  823. if (err) {
  824. rtnl_unlock();
  825. return err;
  826. }
  827. }
  828. #endif
  829. rtnl_unlock();
  830. return 0;
  831. }
  832. int tipc_nl_bearer_set(struct sk_buff *skb, struct genl_info *info)
  833. {
  834. int err;
  835. char *name;
  836. struct tipc_bearer *b;
  837. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  838. struct net *net = sock_net(skb->sk);
  839. if (!info->attrs[TIPC_NLA_BEARER])
  840. return -EINVAL;
  841. err = nla_parse_nested(attrs, TIPC_NLA_BEARER_MAX,
  842. info->attrs[TIPC_NLA_BEARER],
  843. tipc_nl_bearer_policy);
  844. if (err)
  845. return err;
  846. if (!attrs[TIPC_NLA_BEARER_NAME])
  847. return -EINVAL;
  848. name = nla_data(attrs[TIPC_NLA_BEARER_NAME]);
  849. rtnl_lock();
  850. b = tipc_bearer_find(net, name);
  851. if (!b) {
  852. rtnl_unlock();
  853. return -EINVAL;
  854. }
  855. if (attrs[TIPC_NLA_BEARER_PROP]) {
  856. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  857. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_BEARER_PROP],
  858. props);
  859. if (err) {
  860. rtnl_unlock();
  861. return err;
  862. }
  863. if (props[TIPC_NLA_PROP_TOL])
  864. b->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  865. if (props[TIPC_NLA_PROP_PRIO])
  866. b->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  867. if (props[TIPC_NLA_PROP_WIN])
  868. b->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  869. }
  870. rtnl_unlock();
  871. return 0;
  872. }
  873. static int __tipc_nl_add_media(struct tipc_nl_msg *msg,
  874. struct tipc_media *media, int nlflags)
  875. {
  876. void *hdr;
  877. struct nlattr *attrs;
  878. struct nlattr *prop;
  879. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  880. nlflags, TIPC_NL_MEDIA_GET);
  881. if (!hdr)
  882. return -EMSGSIZE;
  883. attrs = nla_nest_start(msg->skb, TIPC_NLA_MEDIA);
  884. if (!attrs)
  885. goto msg_full;
  886. if (nla_put_string(msg->skb, TIPC_NLA_MEDIA_NAME, media->name))
  887. goto attr_msg_full;
  888. prop = nla_nest_start(msg->skb, TIPC_NLA_MEDIA_PROP);
  889. if (!prop)
  890. goto prop_msg_full;
  891. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, media->priority))
  892. goto prop_msg_full;
  893. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, media->tolerance))
  894. goto prop_msg_full;
  895. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, media->window))
  896. goto prop_msg_full;
  897. nla_nest_end(msg->skb, prop);
  898. nla_nest_end(msg->skb, attrs);
  899. genlmsg_end(msg->skb, hdr);
  900. return 0;
  901. prop_msg_full:
  902. nla_nest_cancel(msg->skb, prop);
  903. attr_msg_full:
  904. nla_nest_cancel(msg->skb, attrs);
  905. msg_full:
  906. genlmsg_cancel(msg->skb, hdr);
  907. return -EMSGSIZE;
  908. }
  909. int tipc_nl_media_dump(struct sk_buff *skb, struct netlink_callback *cb)
  910. {
  911. int err;
  912. int i = cb->args[0];
  913. struct tipc_nl_msg msg;
  914. if (i == MAX_MEDIA)
  915. return 0;
  916. msg.skb = skb;
  917. msg.portid = NETLINK_CB(cb->skb).portid;
  918. msg.seq = cb->nlh->nlmsg_seq;
  919. rtnl_lock();
  920. for (; media_info_array[i] != NULL; i++) {
  921. err = __tipc_nl_add_media(&msg, media_info_array[i],
  922. NLM_F_MULTI);
  923. if (err)
  924. break;
  925. }
  926. rtnl_unlock();
  927. cb->args[0] = i;
  928. return skb->len;
  929. }
  930. int tipc_nl_media_get(struct sk_buff *skb, struct genl_info *info)
  931. {
  932. int err;
  933. char *name;
  934. struct tipc_nl_msg msg;
  935. struct tipc_media *media;
  936. struct sk_buff *rep;
  937. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  938. if (!info->attrs[TIPC_NLA_MEDIA])
  939. return -EINVAL;
  940. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  941. info->attrs[TIPC_NLA_MEDIA],
  942. tipc_nl_media_policy);
  943. if (err)
  944. return err;
  945. if (!attrs[TIPC_NLA_MEDIA_NAME])
  946. return -EINVAL;
  947. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  948. rep = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  949. if (!rep)
  950. return -ENOMEM;
  951. msg.skb = rep;
  952. msg.portid = info->snd_portid;
  953. msg.seq = info->snd_seq;
  954. rtnl_lock();
  955. media = tipc_media_find(name);
  956. if (!media) {
  957. err = -EINVAL;
  958. goto err_out;
  959. }
  960. err = __tipc_nl_add_media(&msg, media, 0);
  961. if (err)
  962. goto err_out;
  963. rtnl_unlock();
  964. return genlmsg_reply(rep, info);
  965. err_out:
  966. rtnl_unlock();
  967. nlmsg_free(rep);
  968. return err;
  969. }
  970. int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info)
  971. {
  972. int err;
  973. char *name;
  974. struct tipc_media *m;
  975. struct nlattr *attrs[TIPC_NLA_BEARER_MAX + 1];
  976. if (!info->attrs[TIPC_NLA_MEDIA])
  977. return -EINVAL;
  978. err = nla_parse_nested(attrs, TIPC_NLA_MEDIA_MAX,
  979. info->attrs[TIPC_NLA_MEDIA],
  980. tipc_nl_media_policy);
  981. if (!attrs[TIPC_NLA_MEDIA_NAME])
  982. return -EINVAL;
  983. name = nla_data(attrs[TIPC_NLA_MEDIA_NAME]);
  984. rtnl_lock();
  985. m = tipc_media_find(name);
  986. if (!m) {
  987. rtnl_unlock();
  988. return -EINVAL;
  989. }
  990. if (attrs[TIPC_NLA_MEDIA_PROP]) {
  991. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  992. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_MEDIA_PROP],
  993. props);
  994. if (err) {
  995. rtnl_unlock();
  996. return err;
  997. }
  998. if (props[TIPC_NLA_PROP_TOL])
  999. m->tolerance = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1000. if (props[TIPC_NLA_PROP_PRIO])
  1001. m->priority = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1002. if (props[TIPC_NLA_PROP_WIN])
  1003. m->window = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1004. }
  1005. rtnl_unlock();
  1006. return 0;
  1007. }