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