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