bearer.c 27 KB

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