udp_media.c 13 KB

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