udp_media.c 13 KB

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