bearer.c 25 KB

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