udp_media.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /* net/tipc/udp_media.c: IP bearer support for TIPC
  2. *
  3. * Copyright (c) 2015, Ericsson AB
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/socket.h>
  35. #include <linux/ip.h>
  36. #include <linux/udp.h>
  37. #include <linux/inet.h>
  38. #include <linux/inetdevice.h>
  39. #include <linux/igmp.h>
  40. #include <linux/kernel.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/list.h>
  43. #include <net/sock.h>
  44. #include <net/ip.h>
  45. #include <net/udp_tunnel.h>
  46. #include <net/addrconf.h>
  47. #include <linux/tipc_netlink.h>
  48. #include "core.h"
  49. #include "bearer.h"
  50. #include "netlink.h"
  51. #include "msg.h"
  52. /* IANA assigned UDP port */
  53. #define UDP_PORT_DEFAULT 6118
  54. #define UDP_MIN_HEADROOM 48
  55. /**
  56. * struct udp_media_addr - IP/UDP addressing information
  57. *
  58. * This is the bearer level originating address used in neighbor discovery
  59. * messages, and all fields should be in network byte order
  60. */
  61. struct udp_media_addr {
  62. __be16 proto;
  63. __be16 port;
  64. union {
  65. struct in_addr ipv4;
  66. struct in6_addr ipv6;
  67. };
  68. };
  69. /* struct udp_replicast - container for UDP remote addresses */
  70. struct udp_replicast {
  71. struct udp_media_addr addr;
  72. struct rcu_head rcu;
  73. struct list_head list;
  74. };
  75. /**
  76. * struct udp_bearer - ip/udp bearer data structure
  77. * @bearer: associated generic tipc bearer
  78. * @ubsock: bearer associated socket
  79. * @ifindex: local address scope
  80. * @work: used to schedule deferred work on a bearer
  81. */
  82. struct udp_bearer {
  83. struct tipc_bearer __rcu *bearer;
  84. struct socket *ubsock;
  85. u32 ifindex;
  86. struct work_struct work;
  87. struct udp_replicast rcast;
  88. };
  89. static int tipc_udp_is_mcast_addr(struct udp_media_addr *addr)
  90. {
  91. if (ntohs(addr->proto) == ETH_P_IP)
  92. return ipv4_is_multicast(addr->ipv4.s_addr);
  93. #if IS_ENABLED(CONFIG_IPV6)
  94. else
  95. return ipv6_addr_is_multicast(&addr->ipv6);
  96. #endif
  97. return 0;
  98. }
  99. /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
  100. static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
  101. struct udp_media_addr *ua)
  102. {
  103. memset(addr, 0, sizeof(struct tipc_media_addr));
  104. addr->media_id = TIPC_MEDIA_TYPE_UDP;
  105. memcpy(addr->value, ua, sizeof(struct udp_media_addr));
  106. if (tipc_udp_is_mcast_addr(ua))
  107. addr->broadcast = 1;
  108. }
  109. /* tipc_udp_addr2str - convert ip/udp address to string */
  110. static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
  111. {
  112. struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
  113. if (ntohs(ua->proto) == ETH_P_IP)
  114. snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->port));
  115. else if (ntohs(ua->proto) == ETH_P_IPV6)
  116. snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->port));
  117. else
  118. pr_err("Invalid UDP media address\n");
  119. return 0;
  120. }
  121. /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
  122. static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
  123. char *msg)
  124. {
  125. struct udp_media_addr *ua;
  126. ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
  127. if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
  128. return -EINVAL;
  129. tipc_udp_media_addr_set(a, ua);
  130. return 0;
  131. }
  132. /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
  133. static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
  134. {
  135. memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
  136. msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
  137. memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
  138. sizeof(struct udp_media_addr));
  139. return 0;
  140. }
  141. /* tipc_send_msg - enqueue a send request */
  142. static int tipc_udp_xmit(struct net *net, struct sk_buff *skb,
  143. struct udp_bearer *ub, struct udp_media_addr *src,
  144. struct udp_media_addr *dst)
  145. {
  146. int ttl, err = 0;
  147. struct rtable *rt;
  148. if (dst->proto == htons(ETH_P_IP)) {
  149. struct flowi4 fl = {
  150. .daddr = dst->ipv4.s_addr,
  151. .saddr = src->ipv4.s_addr,
  152. .flowi4_mark = skb->mark,
  153. .flowi4_proto = IPPROTO_UDP
  154. };
  155. rt = ip_route_output_key(net, &fl);
  156. if (IS_ERR(rt)) {
  157. err = PTR_ERR(rt);
  158. goto tx_error;
  159. }
  160. ttl = ip4_dst_hoplimit(&rt->dst);
  161. udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
  162. dst->ipv4.s_addr, 0, ttl, 0, src->port,
  163. dst->port, false, true);
  164. #if IS_ENABLED(CONFIG_IPV6)
  165. } else {
  166. struct dst_entry *ndst;
  167. struct flowi6 fl6 = {
  168. .flowi6_oif = ub->ifindex,
  169. .daddr = dst->ipv6,
  170. .saddr = src->ipv6,
  171. .flowi6_proto = IPPROTO_UDP
  172. };
  173. err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
  174. &fl6);
  175. if (err)
  176. goto tx_error;
  177. ttl = ip6_dst_hoplimit(ndst);
  178. err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb, NULL,
  179. &src->ipv6, &dst->ipv6, 0, ttl, 0,
  180. src->port, dst->port, false);
  181. #endif
  182. }
  183. return err;
  184. tx_error:
  185. kfree_skb(skb);
  186. return err;
  187. }
  188. static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
  189. struct tipc_bearer *b,
  190. struct tipc_media_addr *addr)
  191. {
  192. struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
  193. struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
  194. struct udp_replicast *rcast;
  195. struct udp_bearer *ub;
  196. int err = 0;
  197. if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
  198. err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
  199. if (err)
  200. goto out;
  201. }
  202. skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
  203. ub = rcu_dereference_rtnl(b->media_ptr);
  204. if (!ub) {
  205. err = -ENODEV;
  206. goto out;
  207. }
  208. if (!addr->broadcast || list_empty(&ub->rcast.list))
  209. return tipc_udp_xmit(net, skb, ub, src, dst);
  210. /* Replicast, send an skb to each configured IP address */
  211. list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
  212. struct sk_buff *_skb;
  213. _skb = pskb_copy(skb, GFP_ATOMIC);
  214. if (!_skb) {
  215. err = -ENOMEM;
  216. goto out;
  217. }
  218. err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
  219. if (err)
  220. goto out;
  221. }
  222. err = 0;
  223. out:
  224. kfree_skb(skb);
  225. return err;
  226. }
  227. static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
  228. struct udp_media_addr *addr)
  229. {
  230. struct udp_replicast *rcast, *tmp;
  231. struct udp_bearer *ub;
  232. ub = rcu_dereference_rtnl(b->media_ptr);
  233. if (!ub) {
  234. pr_err_ratelimited("UDP bearer instance not found\n");
  235. return false;
  236. }
  237. list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
  238. if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
  239. return true;
  240. }
  241. return false;
  242. }
  243. static int tipc_udp_rcast_add(struct tipc_bearer *b,
  244. struct udp_media_addr *addr)
  245. {
  246. struct udp_replicast *rcast;
  247. struct udp_bearer *ub;
  248. ub = rcu_dereference_rtnl(b->media_ptr);
  249. if (!ub)
  250. return -ENODEV;
  251. rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
  252. if (!rcast)
  253. return -ENOMEM;
  254. memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
  255. if (ntohs(addr->proto) == ETH_P_IP)
  256. pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
  257. #if IS_ENABLED(CONFIG_IPV6)
  258. else if (ntohs(addr->proto) == ETH_P_IPV6)
  259. pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
  260. #endif
  261. list_add_rcu(&rcast->list, &ub->rcast.list);
  262. return 0;
  263. }
  264. static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
  265. {
  266. struct udp_media_addr src = {0};
  267. struct udp_media_addr *dst;
  268. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  269. if (tipc_udp_is_mcast_addr(dst))
  270. return 0;
  271. src.port = udp_hdr(skb)->source;
  272. if (ip_hdr(skb)->version == 4) {
  273. struct iphdr *iphdr = ip_hdr(skb);
  274. src.proto = htons(ETH_P_IP);
  275. src.ipv4.s_addr = iphdr->saddr;
  276. if (ipv4_is_multicast(iphdr->daddr))
  277. return 0;
  278. #if IS_ENABLED(CONFIG_IPV6)
  279. } else if (ip_hdr(skb)->version == 6) {
  280. struct ipv6hdr *iphdr = ipv6_hdr(skb);
  281. src.proto = htons(ETH_P_IPV6);
  282. src.ipv6 = iphdr->saddr;
  283. if (ipv6_addr_is_multicast(&iphdr->daddr))
  284. return 0;
  285. #endif
  286. } else {
  287. return 0;
  288. }
  289. if (likely(tipc_udp_is_known_peer(b, &src)))
  290. return 0;
  291. return tipc_udp_rcast_add(b, &src);
  292. }
  293. /* tipc_udp_recv - read data from bearer socket */
  294. static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
  295. {
  296. struct udp_bearer *ub;
  297. struct tipc_bearer *b;
  298. struct tipc_msg *hdr;
  299. int err;
  300. ub = rcu_dereference_sk_user_data(sk);
  301. if (!ub) {
  302. pr_err_ratelimited("Failed to get UDP bearer reference");
  303. goto out;
  304. }
  305. skb_pull(skb, sizeof(struct udphdr));
  306. hdr = buf_msg(skb);
  307. rcu_read_lock();
  308. b = rcu_dereference_rtnl(ub->bearer);
  309. if (!b)
  310. goto rcu_out;
  311. if (b && test_bit(0, &b->up)) {
  312. tipc_rcv(sock_net(sk), skb, b);
  313. rcu_read_unlock();
  314. return 0;
  315. }
  316. if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
  317. err = tipc_udp_rcast_disc(b, skb);
  318. if (err)
  319. goto rcu_out;
  320. }
  321. rcu_out:
  322. rcu_read_unlock();
  323. out:
  324. kfree_skb(skb);
  325. return 0;
  326. }
  327. static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
  328. {
  329. int err = 0;
  330. struct ip_mreqn mreqn;
  331. struct sock *sk = ub->ubsock->sk;
  332. if (ntohs(remote->proto) == ETH_P_IP) {
  333. mreqn.imr_multiaddr = remote->ipv4;
  334. mreqn.imr_ifindex = ub->ifindex;
  335. err = ip_mc_join_group(sk, &mreqn);
  336. #if IS_ENABLED(CONFIG_IPV6)
  337. } else {
  338. err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
  339. &remote->ipv6);
  340. #endif
  341. }
  342. return err;
  343. }
  344. static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
  345. struct udp_media_addr *addr, int nla_t)
  346. {
  347. if (ntohs(addr->proto) == ETH_P_IP) {
  348. struct sockaddr_in ip4;
  349. memset(&ip4, 0, sizeof(ip4));
  350. ip4.sin_family = AF_INET;
  351. ip4.sin_port = addr->port;
  352. ip4.sin_addr.s_addr = addr->ipv4.s_addr;
  353. if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
  354. return -EMSGSIZE;
  355. #if IS_ENABLED(CONFIG_IPV6)
  356. } else if (ntohs(addr->proto) == ETH_P_IPV6) {
  357. struct sockaddr_in6 ip6;
  358. memset(&ip6, 0, sizeof(ip6));
  359. ip6.sin6_family = AF_INET6;
  360. ip6.sin6_port = addr->port;
  361. memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
  362. if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
  363. return -EMSGSIZE;
  364. #endif
  365. }
  366. return 0;
  367. }
  368. int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
  369. {
  370. u32 bid = cb->args[0];
  371. u32 skip_cnt = cb->args[1];
  372. u32 portid = NETLINK_CB(cb->skb).portid;
  373. struct udp_replicast *rcast, *tmp;
  374. struct tipc_bearer *b;
  375. struct udp_bearer *ub;
  376. void *hdr;
  377. int err;
  378. int i;
  379. if (!bid && !skip_cnt) {
  380. struct net *net = sock_net(skb->sk);
  381. struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
  382. struct nlattr **attrs;
  383. char *bname;
  384. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  385. if (err)
  386. return err;
  387. if (!attrs[TIPC_NLA_BEARER])
  388. return -EINVAL;
  389. err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
  390. attrs[TIPC_NLA_BEARER],
  391. tipc_nl_bearer_policy);
  392. if (err)
  393. return err;
  394. if (!battrs[TIPC_NLA_BEARER_NAME])
  395. return -EINVAL;
  396. bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
  397. rtnl_lock();
  398. b = tipc_bearer_find(net, bname);
  399. if (!b) {
  400. rtnl_unlock();
  401. return -EINVAL;
  402. }
  403. bid = b->identity;
  404. } else {
  405. struct net *net = sock_net(skb->sk);
  406. struct tipc_net *tn = net_generic(net, tipc_net_id);
  407. rtnl_lock();
  408. b = rtnl_dereference(tn->bearer_list[bid]);
  409. if (!b) {
  410. rtnl_unlock();
  411. return -EINVAL;
  412. }
  413. }
  414. ub = rcu_dereference_rtnl(b->media_ptr);
  415. if (!ub) {
  416. rtnl_unlock();
  417. return -EINVAL;
  418. }
  419. i = 0;
  420. list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
  421. if (i < skip_cnt)
  422. goto count;
  423. hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
  424. &tipc_genl_family, NLM_F_MULTI,
  425. TIPC_NL_BEARER_GET);
  426. if (!hdr)
  427. goto done;
  428. err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
  429. TIPC_NLA_UDP_REMOTE);
  430. if (err) {
  431. genlmsg_cancel(skb, hdr);
  432. goto done;
  433. }
  434. genlmsg_end(skb, hdr);
  435. count:
  436. i++;
  437. }
  438. done:
  439. rtnl_unlock();
  440. cb->args[0] = bid;
  441. cb->args[1] = i;
  442. return skb->len;
  443. }
  444. int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
  445. {
  446. struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
  447. struct udp_media_addr *dst;
  448. struct udp_bearer *ub;
  449. struct nlattr *nest;
  450. ub = rcu_dereference_rtnl(b->media_ptr);
  451. if (!ub)
  452. return -ENODEV;
  453. nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
  454. if (!nest)
  455. goto msg_full;
  456. if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
  457. goto msg_full;
  458. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  459. if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
  460. goto msg_full;
  461. if (!list_empty(&ub->rcast.list)) {
  462. if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
  463. goto msg_full;
  464. }
  465. nla_nest_end(msg->skb, nest);
  466. return 0;
  467. msg_full:
  468. nla_nest_cancel(msg->skb, nest);
  469. return -EMSGSIZE;
  470. }
  471. /**
  472. * tipc_parse_udp_addr - build udp media address from netlink data
  473. * @nlattr: netlink attribute containing sockaddr storage aligned address
  474. * @addr: tipc media address to fill with address, port and protocol type
  475. * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
  476. */
  477. static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
  478. u32 *scope_id)
  479. {
  480. struct sockaddr_storage sa;
  481. nla_memcpy(&sa, nla, sizeof(sa));
  482. if (sa.ss_family == AF_INET) {
  483. struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
  484. addr->proto = htons(ETH_P_IP);
  485. addr->port = ip4->sin_port;
  486. addr->ipv4.s_addr = ip4->sin_addr.s_addr;
  487. return 0;
  488. #if IS_ENABLED(CONFIG_IPV6)
  489. } else if (sa.ss_family == AF_INET6) {
  490. struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
  491. addr->proto = htons(ETH_P_IPV6);
  492. addr->port = ip6->sin6_port;
  493. memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
  494. /* Scope ID is only interesting for local addresses */
  495. if (scope_id) {
  496. int atype;
  497. atype = ipv6_addr_type(&ip6->sin6_addr);
  498. if (__ipv6_addr_needs_scope_id(atype) &&
  499. !ip6->sin6_scope_id) {
  500. return -EINVAL;
  501. }
  502. *scope_id = ip6->sin6_scope_id ? : 0;
  503. }
  504. return 0;
  505. #endif
  506. }
  507. return -EADDRNOTAVAIL;
  508. }
  509. int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
  510. {
  511. int err;
  512. struct udp_media_addr addr = {0};
  513. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  514. struct udp_media_addr *dst;
  515. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy))
  516. return -EINVAL;
  517. if (!opts[TIPC_NLA_UDP_REMOTE])
  518. return -EINVAL;
  519. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
  520. if (err)
  521. return err;
  522. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  523. if (tipc_udp_is_mcast_addr(dst)) {
  524. pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
  525. return -EINVAL;
  526. }
  527. if (tipc_udp_is_known_peer(b, &addr))
  528. return 0;
  529. return tipc_udp_rcast_add(b, &addr);
  530. }
  531. /**
  532. * tipc_udp_enable - callback to create a new udp bearer instance
  533. * @net: network namespace
  534. * @b: pointer to generic tipc_bearer
  535. * @attrs: netlink bearer configuration
  536. *
  537. * validate the bearer parameters and initialize the udp bearer
  538. * rtnl_lock should be held
  539. */
  540. static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
  541. struct nlattr *attrs[])
  542. {
  543. int err = -EINVAL;
  544. struct udp_bearer *ub;
  545. struct udp_media_addr remote = {0};
  546. struct udp_media_addr local = {0};
  547. struct udp_port_cfg udp_conf = {0};
  548. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  549. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  550. ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
  551. if (!ub)
  552. return -ENOMEM;
  553. INIT_LIST_HEAD(&ub->rcast.list);
  554. if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
  555. goto err;
  556. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
  557. attrs[TIPC_NLA_BEARER_UDP_OPTS],
  558. tipc_nl_udp_policy))
  559. goto err;
  560. if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
  561. pr_err("Invalid UDP bearer configuration");
  562. err = -EINVAL;
  563. goto err;
  564. }
  565. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
  566. &ub->ifindex);
  567. if (err)
  568. goto err;
  569. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
  570. if (err)
  571. goto err;
  572. if (remote.proto != local.proto) {
  573. err = -EINVAL;
  574. goto err;
  575. }
  576. b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
  577. b->bcast_addr.broadcast = 1;
  578. rcu_assign_pointer(b->media_ptr, ub);
  579. rcu_assign_pointer(ub->bearer, b);
  580. tipc_udp_media_addr_set(&b->addr, &local);
  581. if (local.proto == htons(ETH_P_IP)) {
  582. struct net_device *dev;
  583. dev = __ip_dev_find(net, local.ipv4.s_addr, false);
  584. if (!dev) {
  585. err = -ENODEV;
  586. goto err;
  587. }
  588. udp_conf.family = AF_INET;
  589. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  590. udp_conf.use_udp_checksums = false;
  591. ub->ifindex = dev->ifindex;
  592. if (tipc_mtu_bad(dev, sizeof(struct iphdr) +
  593. sizeof(struct udphdr))) {
  594. err = -EINVAL;
  595. goto err;
  596. }
  597. b->mtu = dev->mtu - sizeof(struct iphdr)
  598. - sizeof(struct udphdr);
  599. #if IS_ENABLED(CONFIG_IPV6)
  600. } else if (local.proto == htons(ETH_P_IPV6)) {
  601. udp_conf.family = AF_INET6;
  602. udp_conf.use_udp6_tx_checksums = true;
  603. udp_conf.use_udp6_rx_checksums = true;
  604. udp_conf.local_ip6 = in6addr_any;
  605. b->mtu = 1280;
  606. #endif
  607. } else {
  608. err = -EAFNOSUPPORT;
  609. goto err;
  610. }
  611. udp_conf.local_udp_port = local.port;
  612. err = udp_sock_create(net, &udp_conf, &ub->ubsock);
  613. if (err)
  614. goto err;
  615. tuncfg.sk_user_data = ub;
  616. tuncfg.encap_type = 1;
  617. tuncfg.encap_rcv = tipc_udp_recv;
  618. tuncfg.encap_destroy = NULL;
  619. setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
  620. /**
  621. * The bcast media address port is used for all peers and the ip
  622. * is used if it's a multicast address.
  623. */
  624. memcpy(&b->bcast_addr.value, &remote, sizeof(remote));
  625. if (tipc_udp_is_mcast_addr(&remote))
  626. err = enable_mcast(ub, &remote);
  627. else
  628. err = tipc_udp_rcast_add(b, &remote);
  629. if (err)
  630. goto err;
  631. return 0;
  632. err:
  633. if (ub->ubsock)
  634. udp_tunnel_sock_release(ub->ubsock);
  635. kfree(ub);
  636. return err;
  637. }
  638. /* cleanup_bearer - break the socket/bearer association */
  639. static void cleanup_bearer(struct work_struct *work)
  640. {
  641. struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
  642. struct udp_replicast *rcast, *tmp;
  643. list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
  644. list_del_rcu(&rcast->list);
  645. kfree_rcu(rcast, rcu);
  646. }
  647. if (ub->ubsock)
  648. udp_tunnel_sock_release(ub->ubsock);
  649. synchronize_net();
  650. kfree(ub);
  651. }
  652. /* tipc_udp_disable - detach bearer from socket */
  653. static void tipc_udp_disable(struct tipc_bearer *b)
  654. {
  655. struct udp_bearer *ub;
  656. ub = rcu_dereference_rtnl(b->media_ptr);
  657. if (!ub) {
  658. pr_err("UDP bearer instance not found\n");
  659. return;
  660. }
  661. if (ub->ubsock)
  662. sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
  663. RCU_INIT_POINTER(ub->bearer, NULL);
  664. /* sock_release need to be done outside of rtnl lock */
  665. INIT_WORK(&ub->work, cleanup_bearer);
  666. schedule_work(&ub->work);
  667. }
  668. struct tipc_media udp_media_info = {
  669. .send_msg = tipc_udp_send_msg,
  670. .enable_media = tipc_udp_enable,
  671. .disable_media = tipc_udp_disable,
  672. .addr2str = tipc_udp_addr2str,
  673. .addr2msg = tipc_udp_addr2msg,
  674. .msg2addr = tipc_udp_msg2addr,
  675. .priority = TIPC_DEF_LINK_PRI,
  676. .tolerance = TIPC_DEF_LINK_TOL,
  677. .window = TIPC_DEF_LINK_WIN,
  678. .type_id = TIPC_MEDIA_TYPE_UDP,
  679. .hwaddr_len = 0,
  680. .name = "udp"
  681. };