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. skb->dev = rt->dst.dev;
  161. ttl = ip4_dst_hoplimit(&rt->dst);
  162. udp_tunnel_xmit_skb(rt, ub->ubsock->sk, skb, src->ipv4.s_addr,
  163. dst->ipv4.s_addr, 0, ttl, 0, src->port,
  164. dst->port, false, true);
  165. #if IS_ENABLED(CONFIG_IPV6)
  166. } else {
  167. struct dst_entry *ndst;
  168. struct flowi6 fl6 = {
  169. .flowi6_oif = ub->ifindex,
  170. .daddr = dst->ipv6,
  171. .saddr = src->ipv6,
  172. .flowi6_proto = IPPROTO_UDP
  173. };
  174. err = ipv6_stub->ipv6_dst_lookup(net, ub->ubsock->sk, &ndst,
  175. &fl6);
  176. if (err)
  177. goto tx_error;
  178. ttl = ip6_dst_hoplimit(ndst);
  179. err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, skb,
  180. ndst->dev, &src->ipv6,
  181. &dst->ipv6, 0, ttl, 0, src->port,
  182. dst->port, false);
  183. #endif
  184. }
  185. return err;
  186. tx_error:
  187. kfree_skb(skb);
  188. return err;
  189. }
  190. static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
  191. struct tipc_bearer *b,
  192. struct tipc_media_addr *addr)
  193. {
  194. struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
  195. struct udp_media_addr *dst = (struct udp_media_addr *)&addr->value;
  196. struct udp_replicast *rcast;
  197. struct udp_bearer *ub;
  198. int err = 0;
  199. if (skb_headroom(skb) < UDP_MIN_HEADROOM) {
  200. err = pskb_expand_head(skb, UDP_MIN_HEADROOM, 0, GFP_ATOMIC);
  201. if (err)
  202. goto out;
  203. }
  204. skb_set_inner_protocol(skb, htons(ETH_P_TIPC));
  205. ub = rcu_dereference_rtnl(b->media_ptr);
  206. if (!ub) {
  207. err = -ENODEV;
  208. goto out;
  209. }
  210. if (!addr->broadcast || list_empty(&ub->rcast.list))
  211. return tipc_udp_xmit(net, skb, ub, src, dst);
  212. /* Replicast, send an skb to each configured IP address */
  213. list_for_each_entry_rcu(rcast, &ub->rcast.list, list) {
  214. struct sk_buff *_skb;
  215. _skb = pskb_copy(skb, GFP_ATOMIC);
  216. if (!_skb) {
  217. err = -ENOMEM;
  218. goto out;
  219. }
  220. err = tipc_udp_xmit(net, _skb, ub, src, &rcast->addr);
  221. if (err) {
  222. kfree_skb(_skb);
  223. goto out;
  224. }
  225. }
  226. err = 0;
  227. out:
  228. kfree_skb(skb);
  229. return err;
  230. }
  231. static bool tipc_udp_is_known_peer(struct tipc_bearer *b,
  232. struct udp_media_addr *addr)
  233. {
  234. struct udp_replicast *rcast, *tmp;
  235. struct udp_bearer *ub;
  236. ub = rcu_dereference_rtnl(b->media_ptr);
  237. if (!ub) {
  238. pr_err_ratelimited("UDP bearer instance not found\n");
  239. return false;
  240. }
  241. list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
  242. if (!memcmp(&rcast->addr, addr, sizeof(struct udp_media_addr)))
  243. return true;
  244. }
  245. return false;
  246. }
  247. static int tipc_udp_rcast_add(struct tipc_bearer *b,
  248. struct udp_media_addr *addr)
  249. {
  250. struct udp_replicast *rcast;
  251. struct udp_bearer *ub;
  252. ub = rcu_dereference_rtnl(b->media_ptr);
  253. if (!ub)
  254. return -ENODEV;
  255. rcast = kmalloc(sizeof(*rcast), GFP_ATOMIC);
  256. if (!rcast)
  257. return -ENOMEM;
  258. memcpy(&rcast->addr, addr, sizeof(struct udp_media_addr));
  259. if (ntohs(addr->proto) == ETH_P_IP)
  260. pr_info("New replicast peer: %pI4\n", &rcast->addr.ipv4);
  261. #if IS_ENABLED(CONFIG_IPV6)
  262. else if (ntohs(addr->proto) == ETH_P_IPV6)
  263. pr_info("New replicast peer: %pI6\n", &rcast->addr.ipv6);
  264. #endif
  265. list_add_rcu(&rcast->list, &ub->rcast.list);
  266. return 0;
  267. }
  268. static int tipc_udp_rcast_disc(struct tipc_bearer *b, struct sk_buff *skb)
  269. {
  270. struct udp_media_addr src = {0};
  271. struct udp_media_addr *dst;
  272. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  273. if (tipc_udp_is_mcast_addr(dst))
  274. return 0;
  275. src.port = udp_hdr(skb)->source;
  276. if (ip_hdr(skb)->version == 4) {
  277. struct iphdr *iphdr = ip_hdr(skb);
  278. src.proto = htons(ETH_P_IP);
  279. src.ipv4.s_addr = iphdr->saddr;
  280. if (ipv4_is_multicast(iphdr->daddr))
  281. return 0;
  282. #if IS_ENABLED(CONFIG_IPV6)
  283. } else if (ip_hdr(skb)->version == 6) {
  284. struct ipv6hdr *iphdr = ipv6_hdr(skb);
  285. src.proto = htons(ETH_P_IPV6);
  286. src.ipv6 = iphdr->saddr;
  287. if (ipv6_addr_is_multicast(&iphdr->daddr))
  288. return 0;
  289. #endif
  290. } else {
  291. return 0;
  292. }
  293. if (likely(tipc_udp_is_known_peer(b, &src)))
  294. return 0;
  295. return tipc_udp_rcast_add(b, &src);
  296. }
  297. /* tipc_udp_recv - read data from bearer socket */
  298. static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
  299. {
  300. struct udp_bearer *ub;
  301. struct tipc_bearer *b;
  302. struct tipc_msg *hdr;
  303. int err;
  304. ub = rcu_dereference_sk_user_data(sk);
  305. if (!ub) {
  306. pr_err_ratelimited("Failed to get UDP bearer reference");
  307. goto out;
  308. }
  309. skb_pull(skb, sizeof(struct udphdr));
  310. hdr = buf_msg(skb);
  311. rcu_read_lock();
  312. b = rcu_dereference_rtnl(ub->bearer);
  313. if (!b)
  314. goto rcu_out;
  315. if (b && test_bit(0, &b->up)) {
  316. tipc_rcv(sock_net(sk), skb, b);
  317. rcu_read_unlock();
  318. return 0;
  319. }
  320. if (unlikely(msg_user(hdr) == LINK_CONFIG)) {
  321. err = tipc_udp_rcast_disc(b, skb);
  322. if (err)
  323. goto rcu_out;
  324. }
  325. rcu_out:
  326. rcu_read_unlock();
  327. out:
  328. kfree_skb(skb);
  329. return 0;
  330. }
  331. static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
  332. {
  333. int err = 0;
  334. struct ip_mreqn mreqn;
  335. struct sock *sk = ub->ubsock->sk;
  336. if (ntohs(remote->proto) == ETH_P_IP) {
  337. mreqn.imr_multiaddr = remote->ipv4;
  338. mreqn.imr_ifindex = ub->ifindex;
  339. err = ip_mc_join_group(sk, &mreqn);
  340. #if IS_ENABLED(CONFIG_IPV6)
  341. } else {
  342. err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
  343. &remote->ipv6);
  344. #endif
  345. }
  346. return err;
  347. }
  348. static int __tipc_nl_add_udp_addr(struct sk_buff *skb,
  349. struct udp_media_addr *addr, int nla_t)
  350. {
  351. if (ntohs(addr->proto) == ETH_P_IP) {
  352. struct sockaddr_in ip4;
  353. memset(&ip4, 0, sizeof(ip4));
  354. ip4.sin_family = AF_INET;
  355. ip4.sin_port = addr->port;
  356. ip4.sin_addr.s_addr = addr->ipv4.s_addr;
  357. if (nla_put(skb, nla_t, sizeof(ip4), &ip4))
  358. return -EMSGSIZE;
  359. #if IS_ENABLED(CONFIG_IPV6)
  360. } else if (ntohs(addr->proto) == ETH_P_IPV6) {
  361. struct sockaddr_in6 ip6;
  362. memset(&ip6, 0, sizeof(ip6));
  363. ip6.sin6_family = AF_INET6;
  364. ip6.sin6_port = addr->port;
  365. memcpy(&ip6.sin6_addr, &addr->ipv6, sizeof(struct in6_addr));
  366. if (nla_put(skb, nla_t, sizeof(ip6), &ip6))
  367. return -EMSGSIZE;
  368. #endif
  369. }
  370. return 0;
  371. }
  372. int tipc_udp_nl_dump_remoteip(struct sk_buff *skb, struct netlink_callback *cb)
  373. {
  374. u32 bid = cb->args[0];
  375. u32 skip_cnt = cb->args[1];
  376. u32 portid = NETLINK_CB(cb->skb).portid;
  377. struct udp_replicast *rcast, *tmp;
  378. struct tipc_bearer *b;
  379. struct udp_bearer *ub;
  380. void *hdr;
  381. int err;
  382. int i;
  383. if (!bid && !skip_cnt) {
  384. struct net *net = sock_net(skb->sk);
  385. struct nlattr *battrs[TIPC_NLA_BEARER_MAX + 1];
  386. struct nlattr **attrs;
  387. char *bname;
  388. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  389. if (err)
  390. return err;
  391. if (!attrs[TIPC_NLA_BEARER])
  392. return -EINVAL;
  393. err = nla_parse_nested(battrs, TIPC_NLA_BEARER_MAX,
  394. attrs[TIPC_NLA_BEARER],
  395. tipc_nl_bearer_policy);
  396. if (err)
  397. return err;
  398. if (!battrs[TIPC_NLA_BEARER_NAME])
  399. return -EINVAL;
  400. bname = nla_data(battrs[TIPC_NLA_BEARER_NAME]);
  401. rtnl_lock();
  402. b = tipc_bearer_find(net, bname);
  403. if (!b) {
  404. rtnl_unlock();
  405. return -EINVAL;
  406. }
  407. bid = b->identity;
  408. } else {
  409. struct net *net = sock_net(skb->sk);
  410. struct tipc_net *tn = net_generic(net, tipc_net_id);
  411. rtnl_lock();
  412. b = rtnl_dereference(tn->bearer_list[bid]);
  413. if (!b) {
  414. rtnl_unlock();
  415. return -EINVAL;
  416. }
  417. }
  418. ub = rcu_dereference_rtnl(b->media_ptr);
  419. if (!ub) {
  420. rtnl_unlock();
  421. return -EINVAL;
  422. }
  423. i = 0;
  424. list_for_each_entry_safe(rcast, tmp, &ub->rcast.list, list) {
  425. if (i < skip_cnt)
  426. goto count;
  427. hdr = genlmsg_put(skb, portid, cb->nlh->nlmsg_seq,
  428. &tipc_genl_family, NLM_F_MULTI,
  429. TIPC_NL_BEARER_GET);
  430. if (!hdr)
  431. goto done;
  432. err = __tipc_nl_add_udp_addr(skb, &rcast->addr,
  433. TIPC_NLA_UDP_REMOTE);
  434. if (err) {
  435. genlmsg_cancel(skb, hdr);
  436. goto done;
  437. }
  438. genlmsg_end(skb, hdr);
  439. count:
  440. i++;
  441. }
  442. done:
  443. rtnl_unlock();
  444. cb->args[0] = bid;
  445. cb->args[1] = i;
  446. return skb->len;
  447. }
  448. int tipc_udp_nl_add_bearer_data(struct tipc_nl_msg *msg, struct tipc_bearer *b)
  449. {
  450. struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
  451. struct udp_media_addr *dst;
  452. struct udp_bearer *ub;
  453. struct nlattr *nest;
  454. ub = rcu_dereference_rtnl(b->media_ptr);
  455. if (!ub)
  456. return -ENODEV;
  457. nest = nla_nest_start(msg->skb, TIPC_NLA_BEARER_UDP_OPTS);
  458. if (!nest)
  459. goto msg_full;
  460. if (__tipc_nl_add_udp_addr(msg->skb, src, TIPC_NLA_UDP_LOCAL))
  461. goto msg_full;
  462. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  463. if (__tipc_nl_add_udp_addr(msg->skb, dst, TIPC_NLA_UDP_REMOTE))
  464. goto msg_full;
  465. if (!list_empty(&ub->rcast.list)) {
  466. if (nla_put_flag(msg->skb, TIPC_NLA_UDP_MULTI_REMOTEIP))
  467. goto msg_full;
  468. }
  469. nla_nest_end(msg->skb, nest);
  470. return 0;
  471. msg_full:
  472. nla_nest_cancel(msg->skb, nest);
  473. return -EMSGSIZE;
  474. }
  475. /**
  476. * tipc_parse_udp_addr - build udp media address from netlink data
  477. * @nlattr: netlink attribute containing sockaddr storage aligned address
  478. * @addr: tipc media address to fill with address, port and protocol type
  479. * @scope_id: IPv6 scope id pointer, not NULL indicates it's required
  480. */
  481. static int tipc_parse_udp_addr(struct nlattr *nla, struct udp_media_addr *addr,
  482. u32 *scope_id)
  483. {
  484. struct sockaddr_storage sa;
  485. nla_memcpy(&sa, nla, sizeof(sa));
  486. if (sa.ss_family == AF_INET) {
  487. struct sockaddr_in *ip4 = (struct sockaddr_in *)&sa;
  488. addr->proto = htons(ETH_P_IP);
  489. addr->port = ip4->sin_port;
  490. addr->ipv4.s_addr = ip4->sin_addr.s_addr;
  491. return 0;
  492. #if IS_ENABLED(CONFIG_IPV6)
  493. } else if (sa.ss_family == AF_INET6) {
  494. struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)&sa;
  495. addr->proto = htons(ETH_P_IPV6);
  496. addr->port = ip6->sin6_port;
  497. memcpy(&addr->ipv6, &ip6->sin6_addr, sizeof(struct in6_addr));
  498. /* Scope ID is only interesting for local addresses */
  499. if (scope_id) {
  500. int atype;
  501. atype = ipv6_addr_type(&ip6->sin6_addr);
  502. if (__ipv6_addr_needs_scope_id(atype) &&
  503. !ip6->sin6_scope_id) {
  504. return -EINVAL;
  505. }
  506. *scope_id = ip6->sin6_scope_id ? : 0;
  507. }
  508. return 0;
  509. #endif
  510. }
  511. return -EADDRNOTAVAIL;
  512. }
  513. int tipc_udp_nl_bearer_add(struct tipc_bearer *b, struct nlattr *attr)
  514. {
  515. int err;
  516. struct udp_media_addr addr = {0};
  517. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  518. struct udp_media_addr *dst;
  519. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX, attr, tipc_nl_udp_policy))
  520. return -EINVAL;
  521. if (!opts[TIPC_NLA_UDP_REMOTE])
  522. return -EINVAL;
  523. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &addr, NULL);
  524. if (err)
  525. return err;
  526. dst = (struct udp_media_addr *)&b->bcast_addr.value;
  527. if (tipc_udp_is_mcast_addr(dst)) {
  528. pr_err("Can't add remote ip to TIPC UDP multicast bearer\n");
  529. return -EINVAL;
  530. }
  531. if (tipc_udp_is_known_peer(b, &addr))
  532. return 0;
  533. return tipc_udp_rcast_add(b, &addr);
  534. }
  535. /**
  536. * tipc_udp_enable - callback to create a new udp bearer instance
  537. * @net: network namespace
  538. * @b: pointer to generic tipc_bearer
  539. * @attrs: netlink bearer configuration
  540. *
  541. * validate the bearer parameters and initialize the udp bearer
  542. * rtnl_lock should be held
  543. */
  544. static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
  545. struct nlattr *attrs[])
  546. {
  547. int err = -EINVAL;
  548. struct udp_bearer *ub;
  549. struct udp_media_addr remote = {0};
  550. struct udp_media_addr local = {0};
  551. struct udp_port_cfg udp_conf = {0};
  552. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  553. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  554. ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
  555. if (!ub)
  556. return -ENOMEM;
  557. INIT_LIST_HEAD(&ub->rcast.list);
  558. if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
  559. goto err;
  560. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
  561. attrs[TIPC_NLA_BEARER_UDP_OPTS],
  562. tipc_nl_udp_policy))
  563. goto err;
  564. if (!opts[TIPC_NLA_UDP_LOCAL] || !opts[TIPC_NLA_UDP_REMOTE]) {
  565. pr_err("Invalid UDP bearer configuration");
  566. err = -EINVAL;
  567. goto err;
  568. }
  569. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_LOCAL], &local,
  570. &ub->ifindex);
  571. if (err)
  572. goto err;
  573. err = tipc_parse_udp_addr(opts[TIPC_NLA_UDP_REMOTE], &remote, NULL);
  574. if (err)
  575. goto err;
  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. };