udp_media.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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, clone, src->ipv4.s_addr,
  167. dst->ipv4.s_addr, 0, ttl, 0,
  168. src->udp_port, dst->udp_port,
  169. false, true);
  170. if (err < 0) {
  171. ip_rt_put(rt);
  172. goto tx_error;
  173. }
  174. #if IS_ENABLED(CONFIG_IPV6)
  175. } else {
  176. struct dst_entry *ndst;
  177. struct flowi6 fl6 = {
  178. .flowi6_oif = ub->ifindex,
  179. .daddr = dst->ipv6,
  180. .saddr = src->ipv6,
  181. .flowi6_proto = IPPROTO_UDP
  182. };
  183. err = ip6_dst_lookup(ub->ubsock->sk, &ndst, &fl6);
  184. if (err)
  185. goto tx_error;
  186. ttl = ip6_dst_hoplimit(ndst);
  187. err = udp_tunnel6_xmit_skb(ndst, clone, ndst->dev, &src->ipv6,
  188. &dst->ipv6, 0, ttl, src->udp_port,
  189. dst->udp_port, false);
  190. #endif
  191. }
  192. return err;
  193. tx_error:
  194. kfree_skb(clone);
  195. return err;
  196. }
  197. /* tipc_udp_recv - read data from bearer socket */
  198. static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
  199. {
  200. struct udp_bearer *ub;
  201. struct tipc_bearer *b;
  202. ub = rcu_dereference_sk_user_data(sk);
  203. if (!ub) {
  204. pr_err_ratelimited("Failed to get UDP bearer reference");
  205. kfree_skb(skb);
  206. return 0;
  207. }
  208. skb_pull(skb, sizeof(struct udphdr));
  209. rcu_read_lock();
  210. b = rcu_dereference_rtnl(ub->bearer);
  211. if (b) {
  212. tipc_rcv(sock_net(sk), skb, b);
  213. rcu_read_unlock();
  214. return 0;
  215. }
  216. rcu_read_unlock();
  217. kfree_skb(skb);
  218. return 0;
  219. }
  220. static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
  221. {
  222. int err = 0;
  223. struct ip_mreqn mreqn;
  224. struct sock *sk = ub->ubsock->sk;
  225. if (ntohs(remote->proto) == ETH_P_IP) {
  226. if (!ipv4_is_multicast(remote->ipv4.s_addr))
  227. return 0;
  228. mreqn.imr_multiaddr = remote->ipv4;
  229. mreqn.imr_ifindex = ub->ifindex;
  230. err = ip_mc_join_group(sk, &mreqn);
  231. #if IS_ENABLED(CONFIG_IPV6)
  232. } else {
  233. if (!ipv6_addr_is_multicast(&remote->ipv6))
  234. return 0;
  235. err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
  236. &remote->ipv6);
  237. #endif
  238. }
  239. return err;
  240. }
  241. /**
  242. * parse_options - build local/remote addresses from configuration
  243. * @attrs: netlink config data
  244. * @ub: UDP bearer instance
  245. * @local: local bearer IP address/port
  246. * @remote: peer or multicast IP/port
  247. */
  248. static int parse_options(struct nlattr *attrs[], struct udp_bearer *ub,
  249. struct udp_media_addr *local,
  250. struct udp_media_addr *remote)
  251. {
  252. struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
  253. struct sockaddr_storage *sa_local, *sa_remote;
  254. if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
  255. goto err;
  256. if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
  257. attrs[TIPC_NLA_BEARER_UDP_OPTS],
  258. tipc_nl_udp_policy))
  259. goto err;
  260. if (opts[TIPC_NLA_UDP_LOCAL] && opts[TIPC_NLA_UDP_REMOTE]) {
  261. sa_local = nla_data(opts[TIPC_NLA_UDP_LOCAL]);
  262. sa_remote = nla_data(opts[TIPC_NLA_UDP_REMOTE]);
  263. } else {
  264. err:
  265. pr_err("Invalid UDP bearer configuration");
  266. return -EINVAL;
  267. }
  268. if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET) {
  269. struct sockaddr_in *ip4;
  270. ip4 = (struct sockaddr_in *)sa_local;
  271. local->proto = htons(ETH_P_IP);
  272. local->udp_port = ip4->sin_port;
  273. local->ipv4.s_addr = ip4->sin_addr.s_addr;
  274. ip4 = (struct sockaddr_in *)sa_remote;
  275. remote->proto = htons(ETH_P_IP);
  276. remote->udp_port = ip4->sin_port;
  277. remote->ipv4.s_addr = ip4->sin_addr.s_addr;
  278. return 0;
  279. #if IS_ENABLED(CONFIG_IPV6)
  280. } else if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET6) {
  281. struct sockaddr_in6 *ip6;
  282. ip6 = (struct sockaddr_in6 *)sa_local;
  283. local->proto = htons(ETH_P_IPV6);
  284. local->udp_port = ip6->sin6_port;
  285. local->ipv6 = ip6->sin6_addr;
  286. ub->ifindex = ip6->sin6_scope_id;
  287. ip6 = (struct sockaddr_in6 *)sa_remote;
  288. remote->proto = htons(ETH_P_IPV6);
  289. remote->udp_port = ip6->sin6_port;
  290. remote->ipv6 = ip6->sin6_addr;
  291. return 0;
  292. #endif
  293. }
  294. return -EADDRNOTAVAIL;
  295. }
  296. /**
  297. * tipc_udp_enable - callback to create a new udp bearer instance
  298. * @net: network namespace
  299. * @b: pointer to generic tipc_bearer
  300. * @attrs: netlink bearer configuration
  301. *
  302. * validate the bearer parameters and initialize the udp bearer
  303. * rtnl_lock should be held
  304. */
  305. static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
  306. struct nlattr *attrs[])
  307. {
  308. int err = -EINVAL;
  309. struct udp_bearer *ub;
  310. struct udp_media_addr *remote;
  311. struct udp_media_addr local = {0};
  312. struct udp_port_cfg udp_conf = {0};
  313. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  314. ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
  315. if (!ub)
  316. return -ENOMEM;
  317. remote = (struct udp_media_addr *)&b->bcast_addr.value;
  318. memset(remote, 0, sizeof(struct udp_media_addr));
  319. err = parse_options(attrs, ub, &local, remote);
  320. if (err)
  321. goto err;
  322. b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
  323. b->bcast_addr.broadcast = 1;
  324. rcu_assign_pointer(b->media_ptr, ub);
  325. rcu_assign_pointer(ub->bearer, b);
  326. tipc_udp_media_addr_set(&b->addr, &local);
  327. if (local.proto == htons(ETH_P_IP)) {
  328. struct net_device *dev;
  329. dev = __ip_dev_find(net, local.ipv4.s_addr, false);
  330. if (!dev) {
  331. err = -ENODEV;
  332. goto err;
  333. }
  334. udp_conf.family = AF_INET;
  335. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  336. udp_conf.use_udp_checksums = false;
  337. ub->ifindex = dev->ifindex;
  338. b->mtu = dev->mtu - sizeof(struct iphdr)
  339. - sizeof(struct udphdr);
  340. #if IS_ENABLED(CONFIG_IPV6)
  341. } else if (local.proto == htons(ETH_P_IPV6)) {
  342. udp_conf.family = AF_INET6;
  343. udp_conf.use_udp6_tx_checksums = true;
  344. udp_conf.use_udp6_rx_checksums = true;
  345. udp_conf.local_ip6 = in6addr_any;
  346. b->mtu = 1280;
  347. #endif
  348. } else {
  349. err = -EAFNOSUPPORT;
  350. goto err;
  351. }
  352. udp_conf.local_udp_port = local.udp_port;
  353. err = udp_sock_create(net, &udp_conf, &ub->ubsock);
  354. if (err)
  355. goto err;
  356. tuncfg.sk_user_data = ub;
  357. tuncfg.encap_type = 1;
  358. tuncfg.encap_rcv = tipc_udp_recv;
  359. tuncfg.encap_destroy = NULL;
  360. setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
  361. if (enable_mcast(ub, remote))
  362. goto err;
  363. return 0;
  364. err:
  365. kfree(ub);
  366. return err;
  367. }
  368. /* cleanup_bearer - break the socket/bearer association */
  369. static void cleanup_bearer(struct work_struct *work)
  370. {
  371. struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
  372. if (ub->ubsock)
  373. udp_tunnel_sock_release(ub->ubsock);
  374. synchronize_net();
  375. kfree(ub);
  376. }
  377. /* tipc_udp_disable - detach bearer from socket */
  378. static void tipc_udp_disable(struct tipc_bearer *b)
  379. {
  380. struct udp_bearer *ub;
  381. ub = rcu_dereference_rtnl(b->media_ptr);
  382. if (!ub) {
  383. pr_err("UDP bearer instance not found\n");
  384. return;
  385. }
  386. if (ub->ubsock)
  387. sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
  388. RCU_INIT_POINTER(b->media_ptr, NULL);
  389. RCU_INIT_POINTER(ub->bearer, NULL);
  390. /* sock_release need to be done outside of rtnl lock */
  391. INIT_WORK(&ub->work, cleanup_bearer);
  392. schedule_work(&ub->work);
  393. }
  394. struct tipc_media udp_media_info = {
  395. .send_msg = tipc_udp_send_msg,
  396. .enable_media = tipc_udp_enable,
  397. .disable_media = tipc_udp_disable,
  398. .addr2str = tipc_udp_addr2str,
  399. .addr2msg = tipc_udp_addr2msg,
  400. .msg2addr = tipc_udp_msg2addr,
  401. .priority = TIPC_DEF_LINK_PRI,
  402. .tolerance = TIPC_DEF_LINK_TOL,
  403. .window = TIPC_DEF_LINK_WIN,
  404. .type_id = TIPC_MEDIA_TYPE_UDP,
  405. .hwaddr_len = 0,
  406. .name = "udp"
  407. };