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>
53 /* IANA assigned UDP port */
54 #define UDP_PORT_DEFAULT 6118
56 #define UDP_MIN_HEADROOM 28
58 static const struct nla_policy tipc_nl_udp_policy
[TIPC_NLA_UDP_MAX
+ 1] = {
59 [TIPC_NLA_UDP_UNSPEC
] = {.type
= NLA_UNSPEC
},
60 [TIPC_NLA_UDP_LOCAL
] = {.type
= NLA_BINARY
,
61 .len
= sizeof(struct sockaddr_storage
)},
62 [TIPC_NLA_UDP_REMOTE
] = {.type
= NLA_BINARY
,
63 .len
= sizeof(struct sockaddr_storage
)},
67 * struct udp_media_addr - IP/UDP addressing information
69 * This is the bearer level originating address used in neighbor discovery
70 * messages, and all fields should be in network byte order
72 struct udp_media_addr
{
82 * struct udp_bearer - ip/udp bearer data structure
83 * @bearer: associated generic tipc bearer
84 * @ubsock: bearer associated socket
85 * @ifindex: local address scope
86 * @work: used to schedule deferred work on a bearer
89 struct tipc_bearer __rcu
*bearer
;
90 struct socket
*ubsock
;
92 struct work_struct work
;
95 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
96 static void tipc_udp_media_addr_set(struct tipc_media_addr
*addr
,
97 struct udp_media_addr
*ua
)
99 memset(addr
, 0, sizeof(struct tipc_media_addr
));
100 addr
->media_id
= TIPC_MEDIA_TYPE_UDP
;
101 memcpy(addr
->value
, ua
, sizeof(struct udp_media_addr
));
102 if (ntohs(ua
->proto
) == ETH_P_IP
) {
103 if (ipv4_is_multicast(ua
->ipv4
.s_addr
))
105 } else if (ntohs(ua
->proto
) == ETH_P_IPV6
) {
106 if (ipv6_addr_type(&ua
->ipv6
) & IPV6_ADDR_MULTICAST
)
109 pr_err("Invalid UDP media address\n");
113 /* tipc_udp_addr2str - convert ip/udp address to string */
114 static int tipc_udp_addr2str(struct tipc_media_addr
*a
, char *buf
, int size
)
116 struct udp_media_addr
*ua
= (struct udp_media_addr
*)&a
->value
;
118 if (ntohs(ua
->proto
) == ETH_P_IP
)
119 snprintf(buf
, size
, "%pI4:%u", &ua
->ipv4
, ntohs(ua
->udp_port
));
120 else if (ntohs(ua
->proto
) == ETH_P_IPV6
)
121 snprintf(buf
, size
, "%pI6:%u", &ua
->ipv6
, ntohs(ua
->udp_port
));
123 pr_err("Invalid UDP media address\n");
127 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
128 static int tipc_udp_msg2addr(struct tipc_bearer
*b
, struct tipc_media_addr
*a
,
131 struct udp_media_addr
*ua
;
133 ua
= (struct udp_media_addr
*) (msg
+ TIPC_MEDIA_ADDR_OFFSET
);
134 if (msg
[TIPC_MEDIA_TYPE_OFFSET
] != TIPC_MEDIA_TYPE_UDP
)
136 tipc_udp_media_addr_set(a
, ua
);
140 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
141 static int tipc_udp_addr2msg(char *msg
, struct tipc_media_addr
*a
)
143 memset(msg
, 0, TIPC_MEDIA_INFO_SIZE
);
144 msg
[TIPC_MEDIA_TYPE_OFFSET
] = TIPC_MEDIA_TYPE_UDP
;
145 memcpy(msg
+ TIPC_MEDIA_ADDR_OFFSET
, a
->value
,
146 sizeof(struct udp_media_addr
));
150 /* tipc_send_msg - enqueue a send request */
151 static int tipc_udp_send_msg(struct net
*net
, struct sk_buff
*skb
,
152 struct tipc_bearer
*b
,
153 struct tipc_media_addr
*dest
)
156 struct udp_bearer
*ub
;
157 struct udp_media_addr
*dst
= (struct udp_media_addr
*)&dest
->value
;
158 struct udp_media_addr
*src
= (struct udp_media_addr
*)&b
->addr
.value
;
161 if (skb_headroom(skb
) < UDP_MIN_HEADROOM
) {
162 err
= pskb_expand_head(skb
, UDP_MIN_HEADROOM
, 0, GFP_ATOMIC
);
167 skb_set_inner_protocol(skb
, htons(ETH_P_TIPC
));
168 ub
= rcu_dereference_rtnl(b
->media_ptr
);
173 if (dst
->proto
== htons(ETH_P_IP
)) {
175 .daddr
= dst
->ipv4
.s_addr
,
176 .saddr
= src
->ipv4
.s_addr
,
177 .flowi4_mark
= skb
->mark
,
178 .flowi4_proto
= IPPROTO_UDP
180 rt
= ip_route_output_key(net
, &fl
);
185 ttl
= ip4_dst_hoplimit(&rt
->dst
);
186 err
= udp_tunnel_xmit_skb(rt
, ub
->ubsock
->sk
, skb
,
188 dst
->ipv4
.s_addr
, 0, ttl
, 0,
189 src
->udp_port
, dst
->udp_port
,
195 #if IS_ENABLED(CONFIG_IPV6)
197 struct dst_entry
*ndst
;
198 struct flowi6 fl6
= {
199 .flowi6_oif
= ub
->ifindex
,
202 .flowi6_proto
= IPPROTO_UDP
204 err
= ipv6_stub
->ipv6_dst_lookup(net
, ub
->ubsock
->sk
, &ndst
,
208 ttl
= ip6_dst_hoplimit(ndst
);
209 err
= udp_tunnel6_xmit_skb(ndst
, ub
->ubsock
->sk
, skb
,
210 ndst
->dev
, &src
->ipv6
,
211 &dst
->ipv6
, 0, ttl
, src
->udp_port
,
212 dst
->udp_port
, false);
222 /* tipc_udp_recv - read data from bearer socket */
223 static int tipc_udp_recv(struct sock
*sk
, struct sk_buff
*skb
)
225 struct udp_bearer
*ub
;
226 struct tipc_bearer
*b
;
227 int usr
= msg_user(buf_msg(skb
));
229 if ((usr
== LINK_PROTOCOL
) || (usr
== NAME_DISTRIBUTOR
))
232 ub
= rcu_dereference_sk_user_data(sk
);
234 pr_err_ratelimited("Failed to get UDP bearer reference");
239 skb_pull(skb
, sizeof(struct udphdr
));
241 b
= rcu_dereference_rtnl(ub
->bearer
);
244 tipc_rcv(sock_net(sk
), skb
, b
);
253 static int enable_mcast(struct udp_bearer
*ub
, struct udp_media_addr
*remote
)
256 struct ip_mreqn mreqn
;
257 struct sock
*sk
= ub
->ubsock
->sk
;
259 if (ntohs(remote
->proto
) == ETH_P_IP
) {
260 if (!ipv4_is_multicast(remote
->ipv4
.s_addr
))
262 mreqn
.imr_multiaddr
= remote
->ipv4
;
263 mreqn
.imr_ifindex
= ub
->ifindex
;
264 err
= ip_mc_join_group(sk
, &mreqn
);
265 #if IS_ENABLED(CONFIG_IPV6)
267 if (!ipv6_addr_is_multicast(&remote
->ipv6
))
269 err
= ipv6_stub
->ipv6_sock_mc_join(sk
, ub
->ifindex
,
277 * parse_options - build local/remote addresses from configuration
278 * @attrs: netlink config data
279 * @ub: UDP bearer instance
280 * @local: local bearer IP address/port
281 * @remote: peer or multicast IP/port
283 static int parse_options(struct nlattr
*attrs
[], struct udp_bearer
*ub
,
284 struct udp_media_addr
*local
,
285 struct udp_media_addr
*remote
)
287 struct nlattr
*opts
[TIPC_NLA_UDP_MAX
+ 1];
288 struct sockaddr_storage
*sa_local
, *sa_remote
;
290 if (!attrs
[TIPC_NLA_BEARER_UDP_OPTS
])
292 if (nla_parse_nested(opts
, TIPC_NLA_UDP_MAX
,
293 attrs
[TIPC_NLA_BEARER_UDP_OPTS
],
296 if (opts
[TIPC_NLA_UDP_LOCAL
] && opts
[TIPC_NLA_UDP_REMOTE
]) {
297 sa_local
= nla_data(opts
[TIPC_NLA_UDP_LOCAL
]);
298 sa_remote
= nla_data(opts
[TIPC_NLA_UDP_REMOTE
]);
301 pr_err("Invalid UDP bearer configuration");
304 if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET
) {
305 struct sockaddr_in
*ip4
;
307 ip4
= (struct sockaddr_in
*)sa_local
;
308 local
->proto
= htons(ETH_P_IP
);
309 local
->udp_port
= ip4
->sin_port
;
310 local
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
312 ip4
= (struct sockaddr_in
*)sa_remote
;
313 remote
->proto
= htons(ETH_P_IP
);
314 remote
->udp_port
= ip4
->sin_port
;
315 remote
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
318 #if IS_ENABLED(CONFIG_IPV6)
319 } else if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET6
) {
320 struct sockaddr_in6
*ip6
;
322 ip6
= (struct sockaddr_in6
*)sa_local
;
323 local
->proto
= htons(ETH_P_IPV6
);
324 local
->udp_port
= ip6
->sin6_port
;
325 local
->ipv6
= ip6
->sin6_addr
;
326 ub
->ifindex
= ip6
->sin6_scope_id
;
328 ip6
= (struct sockaddr_in6
*)sa_remote
;
329 remote
->proto
= htons(ETH_P_IPV6
);
330 remote
->udp_port
= ip6
->sin6_port
;
331 remote
->ipv6
= ip6
->sin6_addr
;
335 return -EADDRNOTAVAIL
;
339 * tipc_udp_enable - callback to create a new udp bearer instance
340 * @net: network namespace
341 * @b: pointer to generic tipc_bearer
342 * @attrs: netlink bearer configuration
344 * validate the bearer parameters and initialize the udp bearer
345 * rtnl_lock should be held
347 static int tipc_udp_enable(struct net
*net
, struct tipc_bearer
*b
,
348 struct nlattr
*attrs
[])
351 struct udp_bearer
*ub
;
352 struct udp_media_addr
*remote
;
353 struct udp_media_addr local
= {0};
354 struct udp_port_cfg udp_conf
= {0};
355 struct udp_tunnel_sock_cfg tuncfg
= {NULL
};
357 ub
= kzalloc(sizeof(*ub
), GFP_ATOMIC
);
361 remote
= (struct udp_media_addr
*)&b
->bcast_addr
.value
;
362 memset(remote
, 0, sizeof(struct udp_media_addr
));
363 err
= parse_options(attrs
, ub
, &local
, remote
);
367 b
->bcast_addr
.media_id
= TIPC_MEDIA_TYPE_UDP
;
368 b
->bcast_addr
.broadcast
= 1;
369 rcu_assign_pointer(b
->media_ptr
, ub
);
370 rcu_assign_pointer(ub
->bearer
, b
);
371 tipc_udp_media_addr_set(&b
->addr
, &local
);
372 if (local
.proto
== htons(ETH_P_IP
)) {
373 struct net_device
*dev
;
375 dev
= __ip_dev_find(net
, local
.ipv4
.s_addr
, false);
380 udp_conf
.family
= AF_INET
;
381 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
382 udp_conf
.use_udp_checksums
= false;
383 ub
->ifindex
= dev
->ifindex
;
384 b
->mtu
= dev
->mtu
- sizeof(struct iphdr
)
385 - sizeof(struct udphdr
);
386 #if IS_ENABLED(CONFIG_IPV6)
387 } else if (local
.proto
== htons(ETH_P_IPV6
)) {
388 udp_conf
.family
= AF_INET6
;
389 udp_conf
.use_udp6_tx_checksums
= true;
390 udp_conf
.use_udp6_rx_checksums
= true;
391 udp_conf
.local_ip6
= in6addr_any
;
398 udp_conf
.local_udp_port
= local
.udp_port
;
399 err
= udp_sock_create(net
, &udp_conf
, &ub
->ubsock
);
402 tuncfg
.sk_user_data
= ub
;
403 tuncfg
.encap_type
= 1;
404 tuncfg
.encap_rcv
= tipc_udp_recv
;
405 tuncfg
.encap_destroy
= NULL
;
406 setup_udp_tunnel_sock(net
, ub
->ubsock
, &tuncfg
);
408 if (enable_mcast(ub
, remote
))
416 /* cleanup_bearer - break the socket/bearer association */
417 static void cleanup_bearer(struct work_struct
*work
)
419 struct udp_bearer
*ub
= container_of(work
, struct udp_bearer
, work
);
422 udp_tunnel_sock_release(ub
->ubsock
);
427 /* tipc_udp_disable - detach bearer from socket */
428 static void tipc_udp_disable(struct tipc_bearer
*b
)
430 struct udp_bearer
*ub
;
432 ub
= rcu_dereference_rtnl(b
->media_ptr
);
434 pr_err("UDP bearer instance not found\n");
438 sock_set_flag(ub
->ubsock
->sk
, SOCK_DEAD
);
439 RCU_INIT_POINTER(ub
->bearer
, NULL
);
441 /* sock_release need to be done outside of rtnl lock */
442 INIT_WORK(&ub
->work
, cleanup_bearer
);
443 schedule_work(&ub
->work
);
446 struct tipc_media udp_media_info
= {
447 .send_msg
= tipc_udp_send_msg
,
448 .enable_media
= tipc_udp_enable
,
449 .disable_media
= tipc_udp_disable
,
450 .addr2str
= tipc_udp_addr2str
,
451 .addr2msg
= tipc_udp_addr2msg
,
452 .msg2addr
= tipc_udp_msg2addr
,
453 .priority
= TIPC_DEF_LINK_PRI
,
454 .tolerance
= TIPC_DEF_LINK_TOL
,
455 .window
= TIPC_DEF_LINK_WIN
,
456 .type_id
= TIPC_MEDIA_TYPE_UDP
,