udp_media.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. /* IANA assigned UDP port */
  51. #define UDP_PORT_DEFAULT 6118
  52. static const struct nla_policy tipc_nl_udp_policy[TIPC_NLA_UDP_MAX + 1] = {
  53. [TIPC_NLA_UDP_UNSPEC] = {.type = NLA_UNSPEC},
  54. [TIPC_NLA_UDP_LOCAL] = {.type = NLA_BINARY,
  55. .len = sizeof(struct sockaddr_storage)},
  56. [TIPC_NLA_UDP_REMOTE] = {.type = NLA_BINARY,
  57. .len = sizeof(struct sockaddr_storage)},
  58. };
  59. /**
  60. * struct udp_media_addr - IP/UDP addressing information
  61. *
  62. * This is the bearer level originating address used in neighbor discovery
  63. * messages, and all fields should be in network byte order
  64. */
  65. struct udp_media_addr {
  66. __be16 proto;
  67. __be16 udp_port;
  68. union {
  69. struct in_addr ipv4;
  70. struct in6_addr ipv6;
  71. };
  72. };
  73. /**
  74. * struct udp_bearer - ip/udp bearer data structure
  75. * @bearer: associated generic tipc bearer
  76. * @ubsock: bearer associated socket
  77. * @ifindex: local address scope
  78. * @work: used to schedule deferred work on a bearer
  79. */
  80. struct udp_bearer {
  81. struct tipc_bearer __rcu *bearer;
  82. struct socket *ubsock;
  83. u32 ifindex;
  84. struct work_struct work;
  85. };
  86. /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
  87. static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
  88. struct udp_media_addr *ua)
  89. {
  90. memset(addr, 0, sizeof(struct tipc_media_addr));
  91. addr->media_id = TIPC_MEDIA_TYPE_UDP;
  92. memcpy(addr->value, ua, sizeof(struct udp_media_addr));
  93. if (ntohs(ua->proto) == ETH_P_IP) {
  94. if (ipv4_is_multicast(ua->ipv4.s_addr))
  95. addr->broadcast = 1;
  96. } else if (ntohs(ua->proto) == ETH_P_IPV6) {
  97. if (ipv6_addr_type(&ua->ipv6) & IPV6_ADDR_MULTICAST)
  98. addr->broadcast = 1;
  99. } else {
  100. pr_err("Invalid UDP media address\n");
  101. }
  102. }
  103. /* tipc_udp_addr2str - convert ip/udp address to string */
  104. static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
  105. {
  106. struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
  107. if (ntohs(ua->proto) == ETH_P_IP)
  108. snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->udp_port));
  109. else if (ntohs(ua->proto) == ETH_P_IPV6)
  110. snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->udp_port));
  111. else
  112. pr_err("Invalid UDP media address\n");
  113. return 0;
  114. }
  115. /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
  116. static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
  117. char *msg)
  118. {
  119. struct udp_media_addr *ua;
  120. ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
  121. if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
  122. return -EINVAL;
  123. tipc_udp_media_addr_set(a, ua);
  124. return 0;
  125. }
  126. /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
  127. static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
  128. {
  129. memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
  130. msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
  131. memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
  132. sizeof(struct udp_media_addr));
  133. return 0;
  134. }
  135. /* tipc_send_msg - enqueue a send request */
  136. static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
  137. struct tipc_bearer *b,
  138. struct tipc_media_addr *dest)
  139. {
  140. int ttl, err = 0;
  141. struct udp_bearer *ub;
  142. struct udp_media_addr *dst = (struct udp_media_addr *)&dest->value;
  143. struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
  144. struct sk_buff *clone;
  145. struct rtable *rt;
  146. clone = skb_clone(skb, GFP_ATOMIC);
  147. skb_set_inner_protocol(clone, htons(ETH_P_TIPC));
  148. ub = rcu_dereference_rtnl(b->media_ptr);
  149. if (!ub) {
  150. err = -ENODEV;
  151. goto tx_error;
  152. }
  153. if (dst->proto == htons(ETH_P_IP)) {
  154. struct flowi4 fl = {
  155. .daddr = dst->ipv4.s_addr,
  156. .saddr = src->ipv4.s_addr,
  157. .flowi4_mark = clone->mark,
  158. .flowi4_proto = IPPROTO_UDP
  159. };
  160. rt = ip_route_output_key(net, &fl);
  161. if (IS_ERR(rt)) {
  162. err = PTR_ERR(rt);
  163. goto tx_error;
  164. }
  165. ttl = ip4_dst_hoplimit(&rt->dst);
  166. err = udp_tunnel_xmit_skb(rt, ub->ubsock->sk, clone,
  167. src->ipv4.s_addr,
  168. dst->ipv4.s_addr, 0, ttl, 0,
  169. src->udp_port, dst->udp_port,
  170. false, true);
  171. if (err < 0) {
  172. ip_rt_put(rt);
  173. goto tx_error;
  174. }
  175. #if IS_ENABLED(CONFIG_IPV6)
  176. } else {
  177. struct dst_entry *ndst;
  178. struct flowi6 fl6 = {
  179. .flowi6_oif = ub->ifindex,
  180. .daddr = dst->ipv6,
  181. .saddr = src->ipv6,
  182. .flowi6_proto = IPPROTO_UDP
  183. };
  184. err = ipv6_stub->ipv6_dst_lookup(ub->ubsock->sk, &ndst, &fl6);
  185. if (err)
  186. goto tx_error;
  187. ttl = ip6_dst_hoplimit(ndst);
  188. err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, clone,
  189. ndst->dev, &src->ipv6,
  190. &dst->ipv6, 0, ttl, src->udp_port,
  191. dst->udp_port, false);
  192. #endif
  193. }
  194. return err;
  195. tx_error:
  196. kfree_skb(clone);
  197. return err;
  198. }
  199. /* tipc_udp_recv - read data from bearer socket */
  200. static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
  201. {
  202. struct udp_bearer *ub;
  203. struct tipc_bearer *b;
  204. ub = rcu_dereference_sk_user_data(sk);
  205. if (!ub) {
  206. pr_err_ratelimited("Failed to get UDP bearer reference");
  207. kfree_skb(skb);
  208. return 0;
  209. }
  210. skb_pull(skb, sizeof(struct udphdr));
  211. rcu_read_lock();
  212. b = rcu_dereference_rtnl(ub->bearer);
  213. if (b) {
  214. tipc_rcv(sock_net(sk), skb, b);
  215. rcu_read_unlock();
  216. return 0;
  217. }
  218. rcu_read_unlock();
  219. kfree_skb(skb);
  220. return 0;
  221. }
  222. static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
  223. {
  224. int err = 0;
  225. struct ip_mreqn mreqn;
  226. struct sock *sk = ub->ubsock->sk;
  227. if (ntohs(remote->proto) == ETH_P_IP) {
  228. if (!ipv4_is_multicast(remote->ipv4.s_addr))
  229. return 0;
  230. mreqn.imr_multiaddr = remote->ipv4;
  231. mreqn.imr_ifindex = ub->ifindex;
  232. err = ip_mc_join_group(sk, &mreqn);
  233. #if IS_ENABLED(CONFIG_IPV6)
  234. } else {
  235. if (!ipv6_addr_is_multicast(&remote->ipv6))
  236. return 0;
  237. err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
  238. &remote->ipv6);
  239. #endif
  240. }
  241. return err;
  242. }
  243. /**
  244. * parse_options - build local/remote addresses from configuration
  245. * @attrs: netlink config data
  246. * @ub: UDP bearer instance
  247. * @local: local bearer IP address/port
  248. * @remote: peer or multicast IP/port
  249. */
  250. static int parse_options(struct nlattr *attrs[], struct udp_bearer *ub,
  251. struct udp_media_addr *local,
  252. struct udp_media_addr *remote)
  253. {
  254. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  255. struct sockaddr_storage *sa_local, *sa_remote;
  256. if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
  257. goto err;
  258. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
  259. attrs[TIPC_NLA_BEARER_UDP_OPTS],
  260. tipc_nl_udp_policy))
  261. goto err;
  262. if (opts[TIPC_NLA_UDP_LOCAL] && opts[TIPC_NLA_UDP_REMOTE]) {
  263. sa_local = nla_data(opts[TIPC_NLA_UDP_LOCAL]);
  264. sa_remote = nla_data(opts[TIPC_NLA_UDP_REMOTE]);
  265. } else {
  266. err:
  267. pr_err("Invalid UDP bearer configuration");
  268. return -EINVAL;
  269. }
  270. if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET) {
  271. struct sockaddr_in *ip4;
  272. ip4 = (struct sockaddr_in *)sa_local;
  273. local->proto = htons(ETH_P_IP);
  274. local->udp_port = ip4->sin_port;
  275. local->ipv4.s_addr = ip4->sin_addr.s_addr;
  276. ip4 = (struct sockaddr_in *)sa_remote;
  277. remote->proto = htons(ETH_P_IP);
  278. remote->udp_port = ip4->sin_port;
  279. remote->ipv4.s_addr = ip4->sin_addr.s_addr;
  280. return 0;
  281. #if IS_ENABLED(CONFIG_IPV6)
  282. } else if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET6) {
  283. struct sockaddr_in6 *ip6;
  284. ip6 = (struct sockaddr_in6 *)sa_local;
  285. local->proto = htons(ETH_P_IPV6);
  286. local->udp_port = ip6->sin6_port;
  287. local->ipv6 = ip6->sin6_addr;
  288. ub->ifindex = ip6->sin6_scope_id;
  289. ip6 = (struct sockaddr_in6 *)sa_remote;
  290. remote->proto = htons(ETH_P_IPV6);
  291. remote->udp_port = ip6->sin6_port;
  292. remote->ipv6 = ip6->sin6_addr;
  293. return 0;
  294. #endif
  295. }
  296. return -EADDRNOTAVAIL;
  297. }
  298. /**
  299. * tipc_udp_enable - callback to create a new udp bearer instance
  300. * @net: network namespace
  301. * @b: pointer to generic tipc_bearer
  302. * @attrs: netlink bearer configuration
  303. *
  304. * validate the bearer parameters and initialize the udp bearer
  305. * rtnl_lock should be held
  306. */
  307. static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
  308. struct nlattr *attrs[])
  309. {
  310. int err = -EINVAL;
  311. struct udp_bearer *ub;
  312. struct udp_media_addr *remote;
  313. struct udp_media_addr local = {0};
  314. struct udp_port_cfg udp_conf = {0};
  315. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  316. ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
  317. if (!ub)
  318. return -ENOMEM;
  319. remote = (struct udp_media_addr *)&b->bcast_addr.value;
  320. memset(remote, 0, sizeof(struct udp_media_addr));
  321. err = parse_options(attrs, ub, &local, remote);
  322. if (err)
  323. goto err;
  324. b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
  325. b->bcast_addr.broadcast = 1;
  326. rcu_assign_pointer(b->media_ptr, ub);
  327. rcu_assign_pointer(ub->bearer, b);
  328. tipc_udp_media_addr_set(&b->addr, &local);
  329. if (local.proto == htons(ETH_P_IP)) {
  330. struct net_device *dev;
  331. dev = __ip_dev_find(net, local.ipv4.s_addr, false);
  332. if (!dev) {
  333. err = -ENODEV;
  334. goto err;
  335. }
  336. udp_conf.family = AF_INET;
  337. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  338. udp_conf.use_udp_checksums = false;
  339. ub->ifindex = dev->ifindex;
  340. b->mtu = dev->mtu - sizeof(struct iphdr)
  341. - sizeof(struct udphdr);
  342. #if IS_ENABLED(CONFIG_IPV6)
  343. } else if (local.proto == htons(ETH_P_IPV6)) {
  344. udp_conf.family = AF_INET6;
  345. udp_conf.use_udp6_tx_checksums = true;
  346. udp_conf.use_udp6_rx_checksums = true;
  347. udp_conf.local_ip6 = in6addr_any;
  348. b->mtu = 1280;
  349. #endif
  350. } else {
  351. err = -EAFNOSUPPORT;
  352. goto err;
  353. }
  354. udp_conf.local_udp_port = local.udp_port;
  355. err = udp_sock_create(net, &udp_conf, &ub->ubsock);
  356. if (err)
  357. goto err;
  358. tuncfg.sk_user_data = ub;
  359. tuncfg.encap_type = 1;
  360. tuncfg.encap_rcv = tipc_udp_recv;
  361. tuncfg.encap_destroy = NULL;
  362. setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
  363. if (enable_mcast(ub, remote))
  364. goto err;
  365. return 0;
  366. err:
  367. kfree(ub);
  368. return err;
  369. }
  370. /* cleanup_bearer - break the socket/bearer association */
  371. static void cleanup_bearer(struct work_struct *work)
  372. {
  373. struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
  374. if (ub->ubsock)
  375. udp_tunnel_sock_release(ub->ubsock);
  376. synchronize_net();
  377. kfree(ub);
  378. }
  379. /* tipc_udp_disable - detach bearer from socket */
  380. static void tipc_udp_disable(struct tipc_bearer *b)
  381. {
  382. struct udp_bearer *ub;
  383. ub = rcu_dereference_rtnl(b->media_ptr);
  384. if (!ub) {
  385. pr_err("UDP bearer instance not found\n");
  386. return;
  387. }
  388. if (ub->ubsock)
  389. sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
  390. RCU_INIT_POINTER(b->media_ptr, NULL);
  391. RCU_INIT_POINTER(ub->bearer, NULL);
  392. /* sock_release need to be done outside of rtnl lock */
  393. INIT_WORK(&ub->work, cleanup_bearer);
  394. schedule_work(&ub->work);
  395. }
  396. struct tipc_media udp_media_info = {
  397. .send_msg = tipc_udp_send_msg,
  398. .enable_media = tipc_udp_enable,
  399. .disable_media = tipc_udp_disable,
  400. .addr2str = tipc_udp_addr2str,
  401. .addr2msg = tipc_udp_addr2msg,
  402. .msg2addr = tipc_udp_msg2addr,
  403. .priority = TIPC_DEF_LINK_PRI,
  404. .tolerance = TIPC_DEF_LINK_TOL,
  405. .window = TIPC_DEF_LINK_WIN,
  406. .type_id = TIPC_MEDIA_TYPE_UDP,
  407. .hwaddr_len = 0,
  408. .name = "udp"
  409. };