1 #include <linux/module.h>
2 #include <linux/errno.h>
3 #include <linux/socket.h>
5 #include <linux/types.h>
6 #include <linux/kernel.h>
7 #include <net/dst_metadata.h>
8 #include <net/net_namespace.h>
10 #include <net/udp_tunnel.h>
12 int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
13 struct socket **sockp)
16 struct socket *sock = NULL;
17 struct sockaddr_in udp_addr;
19 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
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,
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,
41 sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
48 kernel_sock_shutdown(sock, SHUT_RDWR);
54 EXPORT_SYMBOL(udp_sock_create4);
56 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
57 struct udp_tunnel_sock_cfg *cfg)
59 struct sock *sk = sock->sk;
61 /* Disable multicast loopback */
62 inet_sk(sk)->mc_loop = 0;
64 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
65 inet_inc_convert_csum(sk);
67 rcu_assign_sk_user_data(sk, cfg->sk_user_data);
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;
75 udp_tunnel_encap_enable(sock);
77 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
79 static void __udp_tunnel_push_rx_port(struct net_device *dev,
80 struct udp_tunnel_info *ti)
82 if (dev->netdev_ops->ndo_udp_tunnel_add) {
83 dev->netdev_ops->ndo_udp_tunnel_add(dev, ti);
88 case UDP_TUNNEL_TYPE_VXLAN:
89 if (!dev->netdev_ops->ndo_add_vxlan_port)
92 dev->netdev_ops->ndo_add_vxlan_port(dev,
96 case UDP_TUNNEL_TYPE_GENEVE:
97 if (!dev->netdev_ops->ndo_add_geneve_port)
100 dev->netdev_ops->ndo_add_geneve_port(dev,
109 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
112 struct sock *sk = sock->sk;
113 struct udp_tunnel_info ti;
116 ti.sa_family = sk->sk_family;
117 ti.port = inet_sk(sk)->inet_sport;
119 __udp_tunnel_push_rx_port(dev, &ti);
121 EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
123 /* Notify netdevs that UDP port started listening */
124 void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
126 struct sock *sk = sock->sk;
127 struct net *net = sock_net(sk);
128 struct udp_tunnel_info ti;
129 struct net_device *dev;
132 ti.sa_family = sk->sk_family;
133 ti.port = inet_sk(sk)->inet_sport;
136 for_each_netdev_rcu(net, dev)
137 __udp_tunnel_push_rx_port(dev, &ti);
140 EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
142 static void __udp_tunnel_pull_rx_port(struct net_device *dev,
143 struct udp_tunnel_info *ti)
145 if (dev->netdev_ops->ndo_udp_tunnel_del) {
146 dev->netdev_ops->ndo_udp_tunnel_del(dev, ti);
151 case UDP_TUNNEL_TYPE_VXLAN:
152 if (!dev->netdev_ops->ndo_del_vxlan_port)
155 dev->netdev_ops->ndo_del_vxlan_port(dev,
159 case UDP_TUNNEL_TYPE_GENEVE:
160 if (!dev->netdev_ops->ndo_del_geneve_port)
163 dev->netdev_ops->ndo_del_geneve_port(dev,
172 /* Notify netdevs that UDP port is no more listening */
173 void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
175 struct sock *sk = sock->sk;
176 struct net *net = sock_net(sk);
177 struct udp_tunnel_info ti;
178 struct net_device *dev;
181 ti.sa_family = sk->sk_family;
182 ti.port = inet_sk(sk)->inet_sport;
185 for_each_netdev_rcu(net, dev)
186 __udp_tunnel_pull_rx_port(dev, &ti);
189 EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
191 void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
192 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
193 __be16 df, __be16 src_port, __be16 dst_port,
194 bool xnet, bool nocheck)
198 __skb_push(skb, sizeof(*uh));
199 skb_reset_transport_header(skb);
203 uh->source = src_port;
204 uh->len = htons(skb->len);
206 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
208 udp_set_csum(nocheck, skb, src, dst, skb->len);
210 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
212 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
214 void udp_tunnel_sock_release(struct socket *sock)
216 rcu_assign_sk_user_data(sock->sk, NULL);
217 kernel_sock_shutdown(sock, SHUT_RDWR);
220 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
222 struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
223 __be16 flags, __be64 tunnel_id, int md_size)
225 struct metadata_dst *tun_dst;
226 struct ip_tunnel_info *info;
228 if (family == AF_INET)
229 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
231 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
235 info = &tun_dst->u.tun_info;
236 info->key.tp_src = udp_hdr(skb)->source;
237 info->key.tp_dst = udp_hdr(skb)->dest;
238 if (udp_hdr(skb)->check)
239 info->key.tun_flags |= TUNNEL_CSUM;
242 EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
244 MODULE_LICENSE("GPL");