bearer.c 28 KB

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