bearer.c 26 KB

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