bearer.c 28 KB

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