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 <linux/tipc_netlink.h>
51 /* IANA assigned UDP port */
52 #define UDP_PORT_DEFAULT 6118
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
)},
63 * struct udp_media_addr - IP/UDP addressing information
65 * This is the bearer level originating address used in neighbor discovery
66 * messages, and all fields should be in network byte order
68 struct udp_media_addr
{
78 * struct udp_bearer - ip/udp bearer data structure
79 * @bearer: associated generic tipc bearer
80 * @ubsock: bearer associated socket
81 * @ifindex: local address scope
82 * @work: used to schedule deferred work on a bearer
85 struct tipc_bearer __rcu
*bearer
;
86 struct socket
*ubsock
;
88 struct work_struct work
;
91 /* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
92 static void tipc_udp_media_addr_set(struct tipc_media_addr
*addr
,
93 struct udp_media_addr
*ua
)
95 memset(addr
, 0, sizeof(struct tipc_media_addr
));
96 addr
->media_id
= TIPC_MEDIA_TYPE_UDP
;
97 memcpy(addr
->value
, ua
, sizeof(struct udp_media_addr
));
98 if (ntohs(ua
->proto
) == ETH_P_IP
) {
99 if (ipv4_is_multicast(ua
->ipv4
.s_addr
))
101 } else if (ntohs(ua
->proto
) == ETH_P_IPV6
) {
102 if (ipv6_addr_type(&ua
->ipv6
) & IPV6_ADDR_MULTICAST
)
105 pr_err("Invalid UDP media address\n");
109 /* tipc_udp_addr2str - convert ip/udp address to string */
110 static int tipc_udp_addr2str(struct tipc_media_addr
*a
, char *buf
, int size
)
112 struct udp_media_addr
*ua
= (struct udp_media_addr
*)&a
->value
;
114 if (ntohs(ua
->proto
) == ETH_P_IP
)
115 snprintf(buf
, size
, "%pI4:%u", &ua
->ipv4
, ntohs(ua
->udp_port
));
116 else if (ntohs(ua
->proto
) == ETH_P_IPV6
)
117 snprintf(buf
, size
, "%pI6:%u", &ua
->ipv6
, ntohs(ua
->udp_port
));
119 pr_err("Invalid UDP media address\n");
123 /* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
124 static int tipc_udp_msg2addr(struct tipc_bearer
*b
, struct tipc_media_addr
*a
,
127 struct udp_media_addr
*ua
;
129 ua
= (struct udp_media_addr
*) (msg
+ TIPC_MEDIA_ADDR_OFFSET
);
130 if (msg
[TIPC_MEDIA_TYPE_OFFSET
] != TIPC_MEDIA_TYPE_UDP
)
132 tipc_udp_media_addr_set(a
, ua
);
136 /* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
137 static int tipc_udp_addr2msg(char *msg
, struct tipc_media_addr
*a
)
139 memset(msg
, 0, TIPC_MEDIA_INFO_SIZE
);
140 msg
[TIPC_MEDIA_TYPE_OFFSET
] = TIPC_MEDIA_TYPE_UDP
;
141 memcpy(msg
+ TIPC_MEDIA_ADDR_OFFSET
, a
->value
,
142 sizeof(struct udp_media_addr
));
146 /* tipc_send_msg - enqueue a send request */
147 static int tipc_udp_send_msg(struct net
*net
, struct sk_buff
*skb
,
148 struct tipc_bearer
*b
,
149 struct tipc_media_addr
*dest
)
152 struct udp_bearer
*ub
;
153 struct udp_media_addr
*dst
= (struct udp_media_addr
*)&dest
->value
;
154 struct udp_media_addr
*src
= (struct udp_media_addr
*)&b
->addr
.value
;
155 struct sk_buff
*clone
;
158 clone
= skb_clone(skb
, GFP_ATOMIC
);
159 skb_set_inner_protocol(clone
, htons(ETH_P_TIPC
));
160 ub
= rcu_dereference_rtnl(b
->media_ptr
);
165 if (htons(dst
->proto
) == ETH_P_IP
) {
167 .daddr
= dst
->ipv4
.s_addr
,
168 .saddr
= src
->ipv4
.s_addr
,
169 .flowi4_mark
= clone
->mark
,
170 .flowi4_proto
= IPPROTO_UDP
172 rt
= ip_route_output_key(net
, &fl
);
177 ttl
= ip4_dst_hoplimit(&rt
->dst
);
178 err
= udp_tunnel_xmit_skb(rt
, clone
, src
->ipv4
.s_addr
,
179 dst
->ipv4
.s_addr
, 0, ttl
, 0,
180 src
->udp_port
, dst
->udp_port
,
186 #if IS_ENABLED(CONFIG_IPV6)
188 struct dst_entry
*ndst
;
189 struct flowi6 fl6
= {
190 .flowi6_oif
= ub
->ifindex
,
193 .flowi6_proto
= IPPROTO_UDP
195 err
= ip6_dst_lookup(ub
->ubsock
->sk
, &ndst
, &fl6
);
198 ttl
= ip6_dst_hoplimit(ndst
);
199 err
= udp_tunnel6_xmit_skb(ndst
, clone
, ndst
->dev
, &src
->ipv6
,
200 &dst
->ipv6
, 0, ttl
, src
->udp_port
,
201 dst
->udp_port
, false);
211 /* tipc_udp_recv - read data from bearer socket */
212 static int tipc_udp_recv(struct sock
*sk
, struct sk_buff
*skb
)
214 struct udp_bearer
*ub
;
215 struct tipc_bearer
*b
;
217 ub
= rcu_dereference_sk_user_data(sk
);
219 pr_err_ratelimited("Failed to get UDP bearer reference");
224 skb_pull(skb
, sizeof(struct udphdr
));
226 b
= rcu_dereference_rtnl(ub
->bearer
);
229 tipc_rcv(sock_net(sk
), skb
, b
);
238 static int enable_mcast(struct udp_bearer
*ub
, struct udp_media_addr
*remote
)
241 struct ip_mreqn mreqn
;
242 struct sock
*sk
= ub
->ubsock
->sk
;
244 if (ntohs(remote
->proto
) == ETH_P_IP
) {
245 if (!ipv4_is_multicast(remote
->ipv4
.s_addr
))
247 mreqn
.imr_multiaddr
= remote
->ipv4
;
248 mreqn
.imr_ifindex
= ub
->ifindex
;
249 err
= __ip_mc_join_group(sk
, &mreqn
);
251 if (!ipv6_addr_is_multicast(&remote
->ipv6
))
253 err
= __ipv6_sock_mc_join(sk
, ub
->ifindex
, &remote
->ipv6
);
259 * parse_options - build local/remote addresses from configuration
260 * @attrs: netlink config data
261 * @ub: UDP bearer instance
262 * @local: local bearer IP address/port
263 * @remote: peer or multicast IP/port
265 static int parse_options(struct nlattr
*attrs
[], struct udp_bearer
*ub
,
266 struct udp_media_addr
*local
,
267 struct udp_media_addr
*remote
)
269 struct nlattr
*opts
[TIPC_NLA_UDP_MAX
+ 1];
270 struct sockaddr_storage
*sa_local
, *sa_remote
;
272 if (!attrs
[TIPC_NLA_BEARER_UDP_OPTS
])
274 if (nla_parse_nested(opts
, TIPC_NLA_UDP_MAX
,
275 attrs
[TIPC_NLA_BEARER_UDP_OPTS
],
278 if (opts
[TIPC_NLA_UDP_LOCAL
] && opts
[TIPC_NLA_UDP_REMOTE
]) {
279 sa_local
= nla_data(opts
[TIPC_NLA_UDP_LOCAL
]);
280 sa_remote
= nla_data(opts
[TIPC_NLA_UDP_REMOTE
]);
283 pr_err("Invalid UDP bearer configuration");
286 if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET
) {
287 struct sockaddr_in
*ip4
;
289 ip4
= (struct sockaddr_in
*)sa_local
;
290 local
->proto
= htons(ETH_P_IP
);
291 local
->udp_port
= ip4
->sin_port
;
292 local
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
294 ip4
= (struct sockaddr_in
*)sa_remote
;
295 remote
->proto
= htons(ETH_P_IP
);
296 remote
->udp_port
= ip4
->sin_port
;
297 remote
->ipv4
.s_addr
= ip4
->sin_addr
.s_addr
;
300 #if IS_ENABLED(CONFIG_IPV6)
301 } else if ((sa_local
->ss_family
& sa_remote
->ss_family
) == AF_INET6
) {
302 struct sockaddr_in6
*ip6
;
304 ip6
= (struct sockaddr_in6
*)sa_local
;
305 local
->proto
= htons(ETH_P_IPV6
);
306 local
->udp_port
= ip6
->sin6_port
;
307 local
->ipv6
= ip6
->sin6_addr
;
308 ub
->ifindex
= ip6
->sin6_scope_id
;
310 ip6
= (struct sockaddr_in6
*)sa_remote
;
311 remote
->proto
= htons(ETH_P_IPV6
);
312 remote
->udp_port
= ip6
->sin6_port
;
313 remote
->ipv6
= ip6
->sin6_addr
;
317 return -EADDRNOTAVAIL
;
321 * tipc_udp_enable - callback to create a new udp bearer instance
322 * @net: network namespace
323 * @b: pointer to generic tipc_bearer
324 * @attrs: netlink bearer configuration
326 * validate the bearer parameters and initialize the udp bearer
327 * rtnl_lock should be held
329 static int tipc_udp_enable(struct net
*net
, struct tipc_bearer
*b
,
330 struct nlattr
*attrs
[])
333 struct udp_bearer
*ub
;
334 struct udp_media_addr
*remote
;
335 struct udp_media_addr local
= {0};
336 struct udp_port_cfg udp_conf
= {0};
337 struct udp_tunnel_sock_cfg tuncfg
= {0};
339 ub
= kzalloc(sizeof(*ub
), GFP_ATOMIC
);
343 remote
= (struct udp_media_addr
*)&b
->bcast_addr
.value
;
344 memset(remote
, 0, sizeof(struct udp_media_addr
));
345 err
= parse_options(attrs
, ub
, &local
, remote
);
349 b
->bcast_addr
.media_id
= TIPC_MEDIA_TYPE_UDP
;
350 b
->bcast_addr
.broadcast
= 1;
351 rcu_assign_pointer(b
->media_ptr
, ub
);
352 rcu_assign_pointer(ub
->bearer
, b
);
353 tipc_udp_media_addr_set(&b
->addr
, &local
);
354 if (htons(local
.proto
) == ETH_P_IP
) {
355 struct net_device
*dev
;
357 dev
= __ip_dev_find(net
, local
.ipv4
.s_addr
, false);
362 udp_conf
.family
= AF_INET
;
363 udp_conf
.local_ip
.s_addr
= htonl(INADDR_ANY
);
364 udp_conf
.use_udp_checksums
= false;
365 ub
->ifindex
= dev
->ifindex
;
366 b
->mtu
= dev
->mtu
- sizeof(struct iphdr
)
367 - sizeof(struct udphdr
);
368 #if IS_ENABLED(CONFIG_IPV6)
369 } else if (htons(local
.proto
) == ETH_P_IPV6
) {
370 udp_conf
.family
= AF_INET6
;
371 udp_conf
.use_udp6_tx_checksums
= true;
372 udp_conf
.use_udp6_rx_checksums
= true;
373 udp_conf
.local_ip6
= in6addr_any
;
380 udp_conf
.local_udp_port
= local
.udp_port
;
381 err
= udp_sock_create(net
, &udp_conf
, &ub
->ubsock
);
384 tuncfg
.sk_user_data
= ub
;
385 tuncfg
.encap_type
= 1;
386 tuncfg
.encap_rcv
= tipc_udp_recv
;
387 tuncfg
.encap_destroy
= NULL
;
388 setup_udp_tunnel_sock(net
, ub
->ubsock
, &tuncfg
);
390 if (enable_mcast(ub
, remote
))
398 /* cleanup_bearer - break the socket/bearer association */
399 static void cleanup_bearer(struct work_struct
*work
)
401 struct udp_bearer
*ub
= container_of(work
, struct udp_bearer
, work
);
404 udp_tunnel_sock_release(ub
->ubsock
);
409 /* tipc_udp_disable - detach bearer from socket */
410 static void tipc_udp_disable(struct tipc_bearer
*b
)
412 struct udp_bearer
*ub
;
414 ub
= rcu_dereference_rtnl(b
->media_ptr
);
416 pr_err("UDP bearer instance not found\n");
420 sock_set_flag(ub
->ubsock
->sk
, SOCK_DEAD
);
421 RCU_INIT_POINTER(b
->media_ptr
, NULL
);
422 RCU_INIT_POINTER(ub
->bearer
, NULL
);
424 /* sock_release need to be done outside of rtnl lock */
425 INIT_WORK(&ub
->work
, cleanup_bearer
);
426 schedule_work(&ub
->work
);
429 struct tipc_media udp_media_info
= {
430 .send_msg
= tipc_udp_send_msg
,
431 .enable_media
= tipc_udp_enable
,
432 .disable_media
= tipc_udp_disable
,
433 .addr2str
= tipc_udp_addr2str
,
434 .addr2msg
= tipc_udp_addr2msg
,
435 .msg2addr
= tipc_udp_msg2addr
,
436 .priority
= TIPC_DEF_LINK_PRI
,
437 .tolerance
= TIPC_DEF_LINK_TOL
,
438 .window
= TIPC_DEF_LINK_WIN
,
439 .type_id
= TIPC_MEDIA_TYPE_UDP
,