1 /* net/tipc/udp_media.c: IP bearer support for TIPC
3 * Copyright (c) 2015, Ericsson AB
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
35 #include <linux/socket.h>
37 #include <linux/udp.h>
38 #include <linux/inet.h>
39 #include <linux/inetdevice.h>
40 #include <linux/igmp.h>
41 #include <linux/kernel.h>
42 #include <linux/workqueue.h>
43 #include <linux/list.h>
46 #include <net/udp_tunnel.h>
47 #include <net/addrconf.h>
48 #include <linux/tipc_netlink.h>
52 /* IANA assigned UDP port */
53 #define UDP_PORT_DEFAULT 6118
55 static const struct nla_policy tipc_nl_udp_policy
[TIPC_NLA_UDP_MAX
+ 1] = {
56 [TIPC_NLA_UDP_UNSPEC
] = {.type
= NLA_UNSPEC
},
57 [TIPC_NLA_UDP_LOCAL
] = {.type
= NLA_BINARY
,
58 .len
= sizeof(struct sockaddr_storage
)},
59 [TIPC_NLA_UDP_REMOTE
] = {.type
= NLA_BINARY
,
60 .len
= sizeof(struct sockaddr_storage
)},
64 * struct udp_media_addr - IP/UDP addressing information
66 * This is the bearer level originating address used in neighbor discovery
67 * messages, and all fields should be in network byte order
69 struct udp_media_addr
{
79 * struct udp_bearer - ip/udp bearer data structure
80 * @bearer: associated generic tipc bearer
81 * @ubsock: bearer associated socket
82 * @ifindex: local address scope
83 * @work: used to schedule deferred work on a bearer
86 struct tipc_bearer __rcu
*bearer
;
87 struct socket
*ubsock
;
89 struct work_struct work
;
92 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
93 static void tipc_udp_media_addr_set(struct tipc_media_addr
*addr
,
94 struct udp_media_addr
*ua
)
96 memset(addr
, 0, sizeof(struct tipc_media_addr
));
97 addr
->media_id
= TIPC_MEDIA_TYPE_UDP
;
98 memcpy(addr
->value
, ua
, sizeof(struct udp_media_addr
));
99 if (ntohs(ua
->proto
) == ETH_P_IP
) {
100 if (ipv4_is_multicast(ua
->ipv4
.s_addr
))
102 } else if (ntohs(ua
->proto
) == ETH_P_IPV6
) {
103 if (ipv6_addr_type(&ua
->ipv6
) & IPV6_ADDR_MULTICAST
)
106 pr_err("Invalid UDP media address\n");
110 /* tipc_udp_addr2str - convert ip/udp address to string */
111 static int tipc_udp_addr2str(struct tipc_media_addr
*a
, char *buf
, int size
)
113 struct udp_media_addr
*ua
= (struct udp_media_addr
*)&a
->value
;
115 if (ntohs(ua
->proto
) == ETH_P_IP
)
116 snprintf(buf
, size
, "%pI4:%u", &ua
->ipv4
, ntohs(ua
->udp_port
));
117 else if (ntohs(ua
->proto
) == ETH_P_IPV6
)
118 snprintf(buf
, size
, "%pI6:%u", &ua
->ipv6
, ntohs(ua
->udp_port
));
120 pr_err("Invalid UDP media address\n");
124 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
125 static int tipc_udp_msg2addr(struct tipc_bearer
*b
, struct tipc_media_addr
*a
,
128 struct udp_media_addr
*ua
;
130 ua
= (struct udp_media_addr
*) (msg
+ TIPC_MEDIA_ADDR_OFFSET
);
131 if (msg
[TIPC_MEDIA_TYPE_OFFSET
] != TIPC_MEDIA_TYPE_UDP
)
133 tipc_udp_media_addr_set(a
, ua
);
137 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
138 static int tipc_udp_addr2msg(char *msg
, struct tipc_media_addr
*a
)
140 memset(msg
, 0, TIPC_MEDIA_INFO_SIZE
);
141 msg
[TIPC_MEDIA_TYPE_OFFSET
] = TIPC_MEDIA_TYPE_UDP
;
142 memcpy(msg
+ TIPC_MEDIA_ADDR_OFFSET
, a
->value
,
143 sizeof(struct udp_media_addr
));
147 /* tipc_send_msg - enqueue a send request */
148 static int tipc_udp_send_msg(struct net
*net
, struct sk_buff
*skb
,
149 struct tipc_bearer
*b
,
150 struct tipc_media_addr
*dest
)
153 struct udp_bearer
*ub
;
154 struct udp_media_addr
*dst
= (struct udp_media_addr
*)&dest
->value
;
155 struct udp_media_addr
*src
= (struct udp_media_addr
*)&b
->addr
.value
;
156 struct sk_buff
*clone
;
159 clone
= skb_clone(skb
, GFP_ATOMIC
);
160 skb_set_inner_protocol(clone
, htons(ETH_P_TIPC
));
161 ub
= rcu_dereference_rtnl(b
->media_ptr
);
166 if (dst
->proto
== htons(ETH_P_IP
)) {
168 .daddr
= dst
->ipv4
.s_addr
,
169 .saddr
= src
->ipv4
.s_addr
,
170 .flowi4_mark
= clone
->mark
,
171 .flowi4_proto
= IPPROTO_UDP
173 rt
= ip_route_output_key(net
, &fl
);
178 ttl
= ip4_dst_hoplimit(&rt
->dst
);
179 err
= udp_tunnel_xmit_skb(rt
, ub
->ubsock
->sk
, clone
,
181 dst
->ipv4
.s_addr
, 0, ttl
, 0,
182 src
->udp_port
, dst
->udp_port
,
188 #if IS_ENABLED(CONFIG_IPV6)
190 struct dst_entry
*ndst
;
191 struct flowi6 fl6
= {
192 .flowi6_oif
= ub
->ifindex
,
195 .flowi6_proto
= IPPROTO_UDP
197 err
= ipv6_stub
->ipv6_dst_lookup(ub
->ubsock
->sk
, &ndst
, &fl6
);
200 ttl
= ip6_dst_hoplimit(ndst
);
201 err
= udp_tunnel6_xmit_skb(ndst
, ub
->ubsock
->sk
, clone
,
202 ndst
->dev
, &src
->ipv6
,
203 &dst
->ipv6
, 0, ttl
, src
->udp_port
,
204 dst
->udp_port
, false);
214 /* tipc_udp_recv - read data from bearer socket */
215 static int tipc_udp_recv(struct sock
*sk
, struct sk_buff
*skb
)
217 struct udp_bearer
*ub
;
218 struct tipc_bearer
*b
;
220 ub
= rcu_dereference_sk_user_data(sk
);
222 pr_err_ratelimited("Failed to get UDP bearer reference");
227 skb_pull(skb
, sizeof(struct udphdr
));
229 b
= rcu_dereference_rtnl(ub
->bearer
);
232 tipc_rcv(sock_net(sk
), skb
, b
);
241 static int enable_mcast(struct udp_bearer
*ub
, struct udp_media_addr
*remote
)
244 struct ip_mreqn mreqn
;
245 struct sock
*sk
= ub
->ubsock
->sk
;
247 if (ntohs(remote
->proto
) == ETH_P_IP
) {
248 if (!ipv4_is_multicast(remote
->ipv4
.s_addr
))
250 mreqn
.imr_multiaddr
= remote
->ipv4
;
251 mreqn
.imr_ifindex
= ub
->ifindex
;
252 err
= ip_mc_join_group(sk
, &mreqn
);
253 #if IS_ENABLED(CONFIG_IPV6)
255 if (!ipv6_addr_is_multicast(&remote
->ipv6
))
257 err
= ipv6_stub
->ipv6_sock_mc_join(sk
, ub
->ifindex
,
265 * parse_options - build local/remote addresses from configuration
266 * @attrs: netlink config data
267 * @ub: UDP bearer instance
268 * @local: local bearer IP address/port
269 * @remote: peer or multicast IP/port
271 static int parse_options(struct nlattr
*attrs
[], struct udp_bearer
*ub
,
272 struct udp_media_addr
*local
,
273 struct udp_media_addr
*remote
)
275 struct nlattr
*opts
[TIPC_NLA_UDP_MAX
+ 1];
276 struct sockaddr_storage
*sa_local
, *sa_remote
;
278 if (!attrs
[TIPC_NLA_BEARER_UDP_OPTS
])
280 if (nla_parse_nested(opts
, TIPC_NLA_UDP_MAX
,
281 attrs
[TIPC_NLA_BEARER_UDP_OPTS
],
284 if (opts
[TIPC_NLA_UDP_LOCAL
] && opts
[TIPC_NLA_UDP_REMOTE
]) {
285 sa_local
= nla_data(opts
[TIPC_NLA_UDP_LOCAL
]);
286 sa_remote
= nla_data(opts
[TIPC_NLA_UDP_REMOTE
]);
289 pr_err("Invalid UDP bearer configuration");
292 if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET
) {
293 struct sockaddr_in
*ip4
;
295 ip4
= (struct sockaddr_in
*)sa_local
;
296 local
->proto
= htons(ETH_P_IP
);
297 local
->udp_port
= ip4
->sin_port
;
298 local
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
300 ip4
= (struct sockaddr_in
*)sa_remote
;
301 remote
->proto
= htons(ETH_P_IP
);
302 remote
->udp_port
= ip4
->sin_port
;
303 remote
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
306 #if IS_ENABLED(CONFIG_IPV6)
307 } else if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET6
) {
308 struct sockaddr_in6
*ip6
;
310 ip6
= (struct sockaddr_in6
*)sa_local
;
311 local
->proto
= htons(ETH_P_IPV6
);
312 local
->udp_port
= ip6
->sin6_port
;
313 local
->ipv6
= ip6
->sin6_addr
;
314 ub
->ifindex
= ip6
->sin6_scope_id
;
316 ip6
= (struct sockaddr_in6
*)sa_remote
;
317 remote
->proto
= htons(ETH_P_IPV6
);
318 remote
->udp_port
= ip6
->sin6_port
;
319 remote
->ipv6
= ip6
->sin6_addr
;
323 return -EADDRNOTAVAIL
;
327 * tipc_udp_enable - callback to create a new udp bearer instance
328 * @net: network namespace
329 * @b: pointer to generic tipc_bearer
330 * @attrs: netlink bearer configuration
332 * validate the bearer parameters and initialize the udp bearer
333 * rtnl_lock should be held
335 static int tipc_udp_enable(struct net
*net
, struct tipc_bearer
*b
,
336 struct nlattr
*attrs
[])
339 struct udp_bearer
*ub
;
340 struct udp_media_addr
*remote
;
341 struct udp_media_addr local
= {0};
342 struct udp_port_cfg udp_conf
= {0};
343 struct udp_tunnel_sock_cfg tuncfg
= {NULL
};
345 ub
= kzalloc(sizeof(*ub
), GFP_ATOMIC
);
349 remote
= (struct udp_media_addr
*)&b
->bcast_addr
.value
;
350 memset(remote
, 0, sizeof(struct udp_media_addr
));
351 err
= parse_options(attrs
, ub
, &local
, remote
);
355 b
->bcast_addr
.media_id
= TIPC_MEDIA_TYPE_UDP
;
356 b
->bcast_addr
.broadcast
= 1;
357 rcu_assign_pointer(b
->media_ptr
, ub
);
358 rcu_assign_pointer(ub
->bearer
, b
);
359 tipc_udp_media_addr_set(&b
->addr
, &local
);
360 if (local
.proto
== htons(ETH_P_IP
)) {
361 struct net_device
*dev
;
363 dev
= __ip_dev_find(net
, local
.ipv4
.s_addr
, false);
368 udp_conf
.family
= AF_INET
;
369 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
370 udp_conf
.use_udp_checksums
= false;
371 ub
->ifindex
= dev
->ifindex
;
372 b
->mtu
= dev
->mtu
- sizeof(struct iphdr
)
373 - sizeof(struct udphdr
);
374 #if IS_ENABLED(CONFIG_IPV6)
375 } else if (local
.proto
== htons(ETH_P_IPV6
)) {
376 udp_conf
.family
= AF_INET6
;
377 udp_conf
.use_udp6_tx_checksums
= true;
378 udp_conf
.use_udp6_rx_checksums
= true;
379 udp_conf
.local_ip6
= in6addr_any
;
386 udp_conf
.local_udp_port
= local
.udp_port
;
387 err
= udp_sock_create(net
, &udp_conf
, &ub
->ubsock
);
390 tuncfg
.sk_user_data
= ub
;
391 tuncfg
.encap_type
= 1;
392 tuncfg
.encap_rcv
= tipc_udp_recv
;
393 tuncfg
.encap_destroy
= NULL
;
394 setup_udp_tunnel_sock(net
, ub
->ubsock
, &tuncfg
);
396 if (enable_mcast(ub
, remote
))
404 /* cleanup_bearer - break the socket/bearer association */
405 static void cleanup_bearer(struct work_struct
*work
)
407 struct udp_bearer
*ub
= container_of(work
, struct udp_bearer
, work
);
410 udp_tunnel_sock_release(ub
->ubsock
);
415 /* tipc_udp_disable - detach bearer from socket */
416 static void tipc_udp_disable(struct tipc_bearer
*b
)
418 struct udp_bearer
*ub
;
420 ub
= rcu_dereference_rtnl(b
->media_ptr
);
422 pr_err("UDP bearer instance not found\n");
426 sock_set_flag(ub
->ubsock
->sk
, SOCK_DEAD
);
427 RCU_INIT_POINTER(b
->media_ptr
, NULL
);
428 RCU_INIT_POINTER(ub
->bearer
, NULL
);
430 /* sock_release need to be done outside of rtnl lock */
431 INIT_WORK(&ub
->work
, cleanup_bearer
);
432 schedule_work(&ub
->work
);
435 struct tipc_media udp_media_info
= {
436 .send_msg
= tipc_udp_send_msg
,
437 .enable_media
= tipc_udp_enable
,
438 .disable_media
= tipc_udp_disable
,
439 .addr2str
= tipc_udp_addr2str
,
440 .addr2msg
= tipc_udp_addr2msg
,
441 .msg2addr
= tipc_udp_msg2addr
,
442 .priority
= TIPC_DEF_LINK_PRI
,
443 .tolerance
= TIPC_DEF_LINK_TOL
,
444 .window
= TIPC_DEF_LINK_WIN
,
445 .type_id
= TIPC_MEDIA_TYPE_UDP
,