bearer.c 25 KB

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