1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/errno.h>
4 #include <linux/socket.h>
5 #include <linux/kernel.h>
6 #include <net/dst_metadata.h>
8 #include <net/udp_tunnel.h>
10 int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
11 struct socket **sockp)
14 struct socket *sock = NULL;
15 struct sockaddr_in udp_addr;
17 err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
21 if (cfg->bind_ifindex) {
22 err = sock_bindtoindex(sock->sk, cfg->bind_ifindex, true);
27 udp_addr.sin_family = AF_INET;
28 udp_addr.sin_addr = cfg->local_ip;
29 udp_addr.sin_port = cfg->local_udp_port;
30 err = kernel_bind(sock, (struct sockaddr *)&udp_addr,
35 if (cfg->peer_udp_port) {
36 udp_addr.sin_family = AF_INET;
37 udp_addr.sin_addr = cfg->peer_ip;
38 udp_addr.sin_port = cfg->peer_udp_port;
39 err = kernel_connect(sock, (struct sockaddr *)&udp_addr,
45 sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
52 kernel_sock_shutdown(sock, SHUT_RDWR);
58 EXPORT_SYMBOL(udp_sock_create4);
60 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
61 struct udp_tunnel_sock_cfg *cfg)
63 struct sock *sk = sock->sk;
65 /* Disable multicast loopback */
66 inet_clear_bit(MC_LOOP, sk);
68 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
69 inet_inc_convert_csum(sk);
71 rcu_assign_sk_user_data(sk, cfg->sk_user_data);
73 udp_sk(sk)->encap_type = cfg->encap_type;
74 udp_sk(sk)->encap_rcv = cfg->encap_rcv;
75 udp_sk(sk)->encap_err_rcv = cfg->encap_err_rcv;
76 udp_sk(sk)->encap_err_lookup = cfg->encap_err_lookup;
77 udp_sk(sk)->encap_destroy = cfg->encap_destroy;
78 udp_sk(sk)->gro_receive = cfg->gro_receive;
79 udp_sk(sk)->gro_complete = cfg->gro_complete;
81 udp_tunnel_encap_enable(sock);
83 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
85 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
88 struct sock *sk = sock->sk;
89 struct udp_tunnel_info ti;
92 ti.sa_family = sk->sk_family;
93 ti.port = inet_sk(sk)->inet_sport;
95 udp_tunnel_nic_add_port(dev, &ti);
97 EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
99 void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
102 struct sock *sk = sock->sk;
103 struct udp_tunnel_info ti;
106 ti.sa_family = sk->sk_family;
107 ti.port = inet_sk(sk)->inet_sport;
109 udp_tunnel_nic_del_port(dev, &ti);
111 EXPORT_SYMBOL_GPL(udp_tunnel_drop_rx_port);
113 /* Notify netdevs that UDP port started listening */
114 void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
116 struct sock *sk = sock->sk;
117 struct net *net = sock_net(sk);
118 struct udp_tunnel_info ti;
119 struct net_device *dev;
122 ti.sa_family = sk->sk_family;
123 ti.port = inet_sk(sk)->inet_sport;
126 for_each_netdev_rcu(net, dev) {
127 udp_tunnel_nic_add_port(dev, &ti);
131 EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
133 /* Notify netdevs that UDP port is no more listening */
134 void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
136 struct sock *sk = sock->sk;
137 struct net *net = sock_net(sk);
138 struct udp_tunnel_info ti;
139 struct net_device *dev;
142 ti.sa_family = sk->sk_family;
143 ti.port = inet_sk(sk)->inet_sport;
146 for_each_netdev_rcu(net, dev) {
147 udp_tunnel_nic_del_port(dev, &ti);
151 EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
153 void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
154 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
155 __be16 df, __be16 src_port, __be16 dst_port,
156 bool xnet, bool nocheck)
160 __skb_push(skb, sizeof(*uh));
161 skb_reset_transport_header(skb);
165 uh->source = src_port;
166 uh->len = htons(skb->len);
168 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
170 udp_set_csum(nocheck, skb, src, dst, skb->len);
172 iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
174 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
176 void udp_tunnel_sock_release(struct socket *sock)
178 rcu_assign_sk_user_data(sock->sk, NULL);
180 kernel_sock_shutdown(sock, SHUT_RDWR);
183 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
185 struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
186 __be16 flags, __be64 tunnel_id, int md_size)
188 struct metadata_dst *tun_dst;
189 struct ip_tunnel_info *info;
191 if (family == AF_INET)
192 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
194 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
198 info = &tun_dst->u.tun_info;
199 info->key.tp_src = udp_hdr(skb)->source;
200 info->key.tp_dst = udp_hdr(skb)->dest;
201 if (udp_hdr(skb)->check)
202 info->key.tun_flags |= TUNNEL_CSUM;
205 EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
207 MODULE_LICENSE("GPL");