blob: 8174753e64941f720a58cfea43a05b59ca6d1ec2 [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;
Tom Herbert38fd2af2016-04-05 08:22:52 -070072 udp_sk(sk)->gro_receive = cfg->gro_receive;
73 udp_sk(sk)->gro_complete = cfg->gro_complete;
Andy Zhou6a93cc92014-09-16 17:31:17 -070074
75 udp_tunnel_encap_enable(sock);
76}
77EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
78
Alexander Duycke7b3db52016-06-16 12:20:52 -070079static void __udp_tunnel_push_rx_port(struct net_device *dev,
80 struct udp_tunnel_info *ti)
81{
82 switch (ti->type) {
83 case UDP_TUNNEL_TYPE_VXLAN:
84 if (!dev->netdev_ops->ndo_add_vxlan_port)
85 break;
86
87 dev->netdev_ops->ndo_add_vxlan_port(dev,
88 ti->sa_family,
89 ti->port);
90 break;
91 case UDP_TUNNEL_TYPE_GENEVE:
92 if (!dev->netdev_ops->ndo_add_geneve_port)
93 break;
94
95 dev->netdev_ops->ndo_add_geneve_port(dev,
96 ti->sa_family,
97 ti->port);
98 break;
99 default:
100 break;
101 }
102}
103
104void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
105 unsigned short type)
106{
107 struct sock *sk = sock->sk;
108 struct udp_tunnel_info ti;
109
110 ti.type = type;
111 ti.sa_family = sk->sk_family;
112 ti.port = inet_sk(sk)->inet_sport;
113
114 __udp_tunnel_push_rx_port(dev, &ti);
115}
116EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
117
118/* Notify netdevs that UDP port started listening */
119void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
120{
121 struct sock *sk = sock->sk;
122 struct net *net = sock_net(sk);
123 struct udp_tunnel_info ti;
124 struct net_device *dev;
125
126 ti.type = type;
127 ti.sa_family = sk->sk_family;
128 ti.port = inet_sk(sk)->inet_sport;
129
130 rcu_read_lock();
131 for_each_netdev_rcu(net, dev)
132 __udp_tunnel_push_rx_port(dev, &ti);
133 rcu_read_unlock();
134}
135EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
136
137static void __udp_tunnel_pull_rx_port(struct net_device *dev,
138 struct udp_tunnel_info *ti)
139{
140 switch (ti->type) {
141 case UDP_TUNNEL_TYPE_VXLAN:
142 if (!dev->netdev_ops->ndo_del_vxlan_port)
143 break;
144
145 dev->netdev_ops->ndo_del_vxlan_port(dev,
146 ti->sa_family,
147 ti->port);
148 break;
149 case UDP_TUNNEL_TYPE_GENEVE:
150 if (!dev->netdev_ops->ndo_del_geneve_port)
151 break;
152
153 dev->netdev_ops->ndo_del_geneve_port(dev,
154 ti->sa_family,
155 ti->port);
156 break;
157 default:
158 break;
159 }
160}
161
162/* Notify netdevs that UDP port is no more listening */
163void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
164{
165 struct sock *sk = sock->sk;
166 struct net *net = sock_net(sk);
167 struct udp_tunnel_info ti;
168 struct net_device *dev;
169
170 ti.type = type;
171 ti.sa_family = sk->sk_family;
172 ti.port = inet_sk(sk)->inet_sport;
173
174 rcu_read_lock();
175 for_each_netdev_rcu(net, dev)
176 __udp_tunnel_pull_rx_port(dev, &ti);
177 rcu_read_unlock();
178}
179EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
180
Pravin B Shelar039f5062015-12-24 14:34:54 -0800181void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
182 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
183 __be16 df, __be16 src_port, __be16 dst_port,
184 bool xnet, bool nocheck)
Andy Zhou6a93cc92014-09-16 17:31:17 -0700185{
186 struct udphdr *uh;
187
188 __skb_push(skb, sizeof(*uh));
189 skb_reset_transport_header(skb);
190 uh = udp_hdr(skb);
191
192 uh->dest = dst_port;
193 uh->source = src_port;
194 uh->len = htons(skb->len);
195
Bernie Harris5146d1f2016-02-22 12:58:05 +1300196 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
197
Tom Herbertd998f8e2015-01-20 11:23:04 -0800198 udp_set_csum(nocheck, skb, src, dst, skb->len);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700199
Pravin B Shelar039f5062015-12-24 14:34:54 -0800200 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700201}
202EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
203
204void udp_tunnel_sock_release(struct socket *sock)
205{
206 rcu_assign_sk_user_data(sock->sk, NULL);
207 kernel_sock_shutdown(sock, SHUT_RDWR);
Eric W. Biederman26abe142015-05-08 21:10:31 -0500208 sock_release(sock);
Andy Zhou6a93cc92014-09-16 17:31:17 -0700209}
210EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
211
Pravin B Shelarc29a70d2015-08-26 23:46:50 -0700212struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
213 __be16 flags, __be64 tunnel_id, int md_size)
214{
215 struct metadata_dst *tun_dst;
216 struct ip_tunnel_info *info;
217
218 if (family == AF_INET)
219 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
220 else
221 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
222 if (!tun_dst)
223 return NULL;
224
225 info = &tun_dst->u.tun_info;
226 info->key.tp_src = udp_hdr(skb)->source;
227 info->key.tp_dst = udp_hdr(skb)->dest;
228 if (udp_hdr(skb)->check)
229 info->key.tun_flags |= TUNNEL_CSUM;
230 return tun_dst;
231}
232EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
233
Tom Herbert8024e022014-07-13 19:49:37 -0700234MODULE_LICENSE("GPL");