1#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>
7#include <net/dst_metadata.h>
8#include <net/net_namespace.h>
9#include <net/udp.h>
10#include <net/udp_tunnel.h>
11
12int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
13 struct socket **sockp)
14{
15 int err;
16 struct socket *sock = NULL;
17 struct sockaddr_in udp_addr;
18
19 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
20 if (err < 0)
21 goto error;
22
23 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;
30
31 if (cfg->peer_udp_port) {
32 udp_addr.sin_family = AF_INET;
33 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);
37 if (err < 0)
38 goto error;
39 }
40
41 sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
42
43 *sockp = sock;
44 return 0;
45
46error:
47 if (sock) {
48 kernel_sock_shutdown(sock, SHUT_RDWR);
49 sock_release(sock);
50 }
51 *sockp = NULL;
52 return err;
53}
54EXPORT_SYMBOL(udp_sock_create4);
55
56void 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
62 inet_sk(sk)->mc_loop = 0;
63
64
65 inet_inc_convert_csum(sk);
66
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 udp_sk(sk)->gro_receive = cfg->gro_receive;
73 udp_sk(sk)->gro_complete = cfg->gro_complete;
74
75 udp_tunnel_encap_enable(sock);
76}
77EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
78
79void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
80 unsigned short type)
81{
82 struct sock *sk = sock->sk;
83 struct udp_tunnel_info ti;
84
85 if (!dev->netdev_ops->ndo_udp_tunnel_add ||
86 !(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
87 return;
88
89 ti.type = type;
90 ti.sa_family = sk->sk_family;
91 ti.port = inet_sk(sk)->inet_sport;
92
93 dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
94}
95EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
96
97void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
98 unsigned short type)
99{
100 struct sock *sk = sock->sk;
101 struct udp_tunnel_info ti;
102
103 if (!dev->netdev_ops->ndo_udp_tunnel_del ||
104 !(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
105 return;
106
107 ti.type = type;
108 ti.sa_family = sk->sk_family;
109 ti.port = inet_sk(sk)->inet_sport;
110
111 dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
112}
113EXPORT_SYMBOL_GPL(udp_tunnel_drop_rx_port);
114
115
116void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
117{
118 struct sock *sk = sock->sk;
119 struct net *net = sock_net(sk);
120 struct udp_tunnel_info ti;
121 struct net_device *dev;
122
123 ti.type = type;
124 ti.sa_family = sk->sk_family;
125 ti.port = inet_sk(sk)->inet_sport;
126
127 rcu_read_lock();
128 for_each_netdev_rcu(net, dev) {
129 if (!dev->netdev_ops->ndo_udp_tunnel_add)
130 continue;
131 if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
132 continue;
133 dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
134 }
135 rcu_read_unlock();
136}
137EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
138
139
140void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
141{
142 struct sock *sk = sock->sk;
143 struct net *net = sock_net(sk);
144 struct udp_tunnel_info ti;
145 struct net_device *dev;
146
147 ti.type = type;
148 ti.sa_family = sk->sk_family;
149 ti.port = inet_sk(sk)->inet_sport;
150
151 rcu_read_lock();
152 for_each_netdev_rcu(net, dev) {
153 if (!dev->netdev_ops->ndo_udp_tunnel_del)
154 continue;
155 if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
156 continue;
157 dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
158 }
159 rcu_read_unlock();
160}
161EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
162
163void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
164 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
165 __be16 df, __be16 src_port, __be16 dst_port,
166 bool xnet, bool nocheck)
167{
168 struct udphdr *uh;
169
170 __skb_push(skb, sizeof(*uh));
171 skb_reset_transport_header(skb);
172 uh = udp_hdr(skb);
173
174 uh->dest = dst_port;
175 uh->source = src_port;
176 uh->len = htons(skb->len);
177
178 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
179
180 udp_set_csum(nocheck, skb, src, dst, skb->len);
181
182 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
183}
184EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
185
186void udp_tunnel_sock_release(struct socket *sock)
187{
188 rcu_assign_sk_user_data(sock->sk, NULL);
189 kernel_sock_shutdown(sock, SHUT_RDWR);
190 sock_release(sock);
191}
192EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
193
194struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
195 __be16 flags, __be64 tunnel_id, int md_size)
196{
197 struct metadata_dst *tun_dst;
198 struct ip_tunnel_info *info;
199
200 if (family == AF_INET)
201 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
202 else
203 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
204 if (!tun_dst)
205 return NULL;
206
207 info = &tun_dst->u.tun_info;
208 info->key.tp_src = udp_hdr(skb)->source;
209 info->key.tp_dst = udp_hdr(skb)->dest;
210 if (udp_hdr(skb)->check)
211 info->key.tun_flags |= TUNNEL_CSUM;
212 return tun_dst;
213}
214EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
215
216MODULE_LICENSE("GPL");
217