bearer.c 26 KB

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