udp_media.c 13 KB

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