udp_media.c 13 KB

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