bearer.c 28 KB

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