bearer.c 26 KB

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