bearer.c 26 KB

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