blob: 0ec08814f37d9e904b5f85bbd86cd2fa607c4307 [file] [log] [blame]
Tom Herbert8024e022014-07-13 19:49:37 -07001#include <linux/module.h>
2#include <linux/errno.h>
3#include <linux/socket.h>
4#include <linux/udp.h>
5#include <linux/types.h>
6#include <linux/kernel.h>
Pravin B Shelarc29a70d2015-08-26 23:46:50 -07007#include <net/dst_metadata.h>
8#include <net/net_namespace.h>
Tom Herbert8024e022014-07-13 19:49:37 -07009#include <net/udp.h>
10#include <net/udp_tunnel.h>
Tom Herbert8024e022014-07-13 19:49:37 -070011
Andy Zhoufd384412014-09-16 17:31:16 -070012int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
13 struct socket **sockp)
Tom Herbert8024e022014-07-13 19:49:37 -070014{
Andy Zhou6a93cc92014-09-16 17:31:17 -070015 int err;
Tom Herbert8024e022014-07-13 19:49:37 -070016 struct socket *sock = NULL;
Andy Zhoufd384412014-09-16 17:31:16 -070017 struct sockaddr_in udp_addr;
Tom Herbert8024e022014-07-13 19:49:37 -070018
Eric W. Biederman26abe142015-05-08 21:10:31 -050019 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
Andy Zhoufd384412014-09-16 17:31:16 -070020 if (err < 0)
21 goto error;
Tom Herbert8024e022014-07-13 19:49:37 -070022
Andy Zhoufd384412014-09-16 17:31:16 -070023 udp_addr.sin_family = AF_INET;
24 udp_addr.sin_addr = cfg->local_ip;
25 udp_addr.sin_port = cfg->local_udp_port;
26 err = kernel_bind(sock, (struct sockaddr *)&udp_addr,
27 sizeof(udp_addr));
28 if (err < 0)
29 goto error;
Tom Herbert8024e022014-07-13 19:49:37 -070030
Andy Zhoufd384412014-09-16 17:31:16 -070031 if (cfg->peer_udp_port) {
Tom Herbert8024e022014-07-13 19:49:37 -070032 udp_addr.sin_family = AF_INET;
Andy Zhoufd384412014-09-16 17:31:16 -070033 udp_addr.sin_addr = cfg->peer_ip;
34 udp_addr.sin_port = cfg->peer_udp_port;
35 err = kernel_connect(sock, (struct sockaddr *)&udp_addr,
36 sizeof(udp_addr), 0);
Tom Herbert8024e022014-07-13 19:49:37 -070037 if (err < 0)
38 goto error;
Tom Herbert8024e022014-07-13 19:49:37 -070039 }
40
Andy Zhoufd384412014-09-16 17:31:16 -070041 sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
Tom Herbert8024e022014-07-13 19:49:37 -070042
43 *sockp = sock;
Tom Herbert8024e022014-07-13 19:49:37 -070044 return 0;
45
46error:
47 if (sock) {
48 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -050049 sock_release(sock);
Tom Herbert8024e022014-07-13 19:49:37 -070050 }
51 *sockp = NULL;
52 return err;
53}
Andy Zhoufd384412014-09-16 17:31:16 -070054EXPORT_SYMBOL(udp_sock_create4);
Tom Herbert8024e022014-07-13 19:49:37 -070055
Andy Zhou6a93cc92014-09-16 17:31:17 -070056void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
57 struct udp_tunnel_sock_cfg *cfg)
58{
59 struct sock *sk = sock->sk;
60
61 /* Disable multicast loopback */
62 inet_sk(sk)->mc_loop = 0;
63
64 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
Tom Herbert224d0192015-01-05 13:56:14 -080065 inet_inc_convert_csum(sk);
Andy Zhou6a93cc92014-09-16 17:31:17 -070066
67 rcu_assign_sk_user_data(sk, cfg->sk_user_data);
68
69 udp_sk(sk)->encap_type = cfg->encap_type;
70 udp_sk(sk)->encap_rcv = cfg->encap_rcv;
71 udp_sk(sk)->encap_destroy = cfg->encap_destroy;
72
73 udp_tunnel_encap_enable(sock);
74}
75EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
76
Pravin B Shelar039f5062015-12-24 14:34:54 -080077void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
78 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
79 __be16 df, __be16 src_port, __be16 dst_port,
80 bool xnet, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -070081{
82 struct udphdr *uh;
83
84 __skb_push(skb, sizeof(*uh));
85 skb_reset_transport_header(skb);
86 uh = udp_hdr(skb);
87
88 uh->dest = dst_port;
89 uh->source = src_port;
90 uh->len = htons(skb->len);
91
Tom Herbertd998f8e2015-01-20 11:23:04 -080092 udp_set_csum(nocheck, skb, src, dst, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -070093
Pravin B Shelar039f5062015-12-24 14:34:54 -080094 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
Andy Zhou6a93cc92014-09-16 17:31:17 -070095}
96EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
97
98void udp_tunnel_sock_release(struct socket *sock)
99{
100 rcu_assign_sk_user_data(sock->sk, NULL);
101 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -0500102 sock_release(sock);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700103}
104EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
105
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700106struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
107 __be16 flags, __be64 tunnel_id, int md_size)
108{
109 struct metadata_dst *tun_dst;
110 struct ip_tunnel_info *info;
111
112 if (family == AF_INET)
113 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
114 else
115 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
116 if (!tun_dst)
117 return NULL;
118
119 info = &tun_dst->u.tun_info;
120 info->key.tp_src = udp_hdr(skb)->source;
121 info->key.tp_dst = udp_hdr(skb)->dest;
122 if (udp_hdr(skb)->check)
123 info->key.tun_flags |= TUNNEL_CSUM;
124 return tun_dst;
125}
126EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
127
Tom Herbert8024e022014-07-13 19:49:37 -0700128MODULE_LICENSE("GPL");