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>
9 #include <net/inet_dscp.h>
11 int udp_sock_create4(struct net
*net
, struct udp_port_cfg
*cfg
,
12 struct socket
**sockp
)
15 struct socket
*sock
= NULL
;
16 struct sockaddr_in udp_addr
;
18 err
= sock_create_kern(net
, AF_INET
, SOCK_DGRAM
, 0, &sock
);
22 if (cfg
->bind_ifindex
) {
23 err
= sock_bindtoindex(sock
->sk
, cfg
->bind_ifindex
, true);
28 udp_addr
.sin_family
= AF_INET
;
29 udp_addr
.sin_addr
= cfg
->local_ip
;
30 udp_addr
.sin_port
= cfg
->local_udp_port
;
31 err
= kernel_bind(sock
, (struct sockaddr
*)&udp_addr
,
36 if (cfg
->peer_udp_port
) {
37 udp_addr
.sin_family
= AF_INET
;
38 udp_addr
.sin_addr
= cfg
->peer_ip
;
39 udp_addr
.sin_port
= cfg
->peer_udp_port
;
40 err
= kernel_connect(sock
, (struct sockaddr
*)&udp_addr
,
46 sock
->sk
->sk_no_check_tx
= !cfg
->use_udp_checksums
;
53 kernel_sock_shutdown(sock
, SHUT_RDWR
);
59 EXPORT_SYMBOL(udp_sock_create4
);
61 void setup_udp_tunnel_sock(struct net
*net
, struct socket
*sock
,
62 struct udp_tunnel_sock_cfg
*cfg
)
64 struct sock
*sk
= sock
->sk
;
66 /* Disable multicast loopback */
67 inet_clear_bit(MC_LOOP
, sk
);
69 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
70 inet_inc_convert_csum(sk
);
72 rcu_assign_sk_user_data(sk
, cfg
->sk_user_data
);
74 udp_sk(sk
)->encap_type
= cfg
->encap_type
;
75 udp_sk(sk
)->encap_rcv
= cfg
->encap_rcv
;
76 udp_sk(sk
)->encap_err_rcv
= cfg
->encap_err_rcv
;
77 udp_sk(sk
)->encap_err_lookup
= cfg
->encap_err_lookup
;
78 udp_sk(sk
)->encap_destroy
= cfg
->encap_destroy
;
79 udp_sk(sk
)->gro_receive
= cfg
->gro_receive
;
80 udp_sk(sk
)->gro_complete
= cfg
->gro_complete
;
82 udp_tunnel_encap_enable(sk
);
84 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock
);
86 void udp_tunnel_push_rx_port(struct net_device
*dev
, struct socket
*sock
,
89 struct sock
*sk
= sock
->sk
;
90 struct udp_tunnel_info ti
;
93 ti
.sa_family
= sk
->sk_family
;
94 ti
.port
= inet_sk(sk
)->inet_sport
;
96 udp_tunnel_nic_add_port(dev
, &ti
);
98 EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port
);
100 void udp_tunnel_drop_rx_port(struct net_device
*dev
, struct socket
*sock
,
103 struct sock
*sk
= sock
->sk
;
104 struct udp_tunnel_info ti
;
107 ti
.sa_family
= sk
->sk_family
;
108 ti
.port
= inet_sk(sk
)->inet_sport
;
110 udp_tunnel_nic_del_port(dev
, &ti
);
112 EXPORT_SYMBOL_GPL(udp_tunnel_drop_rx_port
);
114 /* Notify netdevs that UDP port started listening */
115 void udp_tunnel_notify_add_rx_port(struct socket
*sock
, unsigned short type
)
117 struct sock
*sk
= sock
->sk
;
118 struct net
*net
= sock_net(sk
);
119 struct udp_tunnel_info ti
;
120 struct net_device
*dev
;
123 ti
.sa_family
= sk
->sk_family
;
124 ti
.port
= inet_sk(sk
)->inet_sport
;
127 for_each_netdev_rcu(net
, dev
) {
128 udp_tunnel_nic_add_port(dev
, &ti
);
132 EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port
);
134 /* Notify netdevs that UDP port is no more listening */
135 void udp_tunnel_notify_del_rx_port(struct socket
*sock
, unsigned short type
)
137 struct sock
*sk
= sock
->sk
;
138 struct net
*net
= sock_net(sk
);
139 struct udp_tunnel_info ti
;
140 struct net_device
*dev
;
143 ti
.sa_family
= sk
->sk_family
;
144 ti
.port
= inet_sk(sk
)->inet_sport
;
147 for_each_netdev_rcu(net
, dev
) {
148 udp_tunnel_nic_del_port(dev
, &ti
);
152 EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port
);
154 void udp_tunnel_xmit_skb(struct rtable
*rt
, struct sock
*sk
, struct sk_buff
*skb
,
155 __be32 src
, __be32 dst
, __u8 tos
, __u8 ttl
,
156 __be16 df
, __be16 src_port
, __be16 dst_port
,
157 bool xnet
, bool nocheck
)
161 __skb_push(skb
, sizeof(*uh
));
162 skb_reset_transport_header(skb
);
166 uh
->source
= src_port
;
167 uh
->len
= htons(skb
->len
);
169 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
171 udp_set_csum(nocheck
, skb
, src
, dst
, skb
->len
);
173 iptunnel_xmit(sk
, rt
, skb
, src
, dst
, IPPROTO_UDP
, tos
, ttl
, df
, xnet
);
175 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb
);
177 void udp_tunnel_sock_release(struct socket
*sock
)
179 rcu_assign_sk_user_data(sock
->sk
, NULL
);
181 kernel_sock_shutdown(sock
, SHUT_RDWR
);
184 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release
);
186 struct metadata_dst
*udp_tun_rx_dst(struct sk_buff
*skb
, unsigned short family
,
187 const unsigned long *flags
,
188 __be64 tunnel_id
, int md_size
)
190 struct metadata_dst
*tun_dst
;
191 struct ip_tunnel_info
*info
;
193 if (family
== AF_INET
)
194 tun_dst
= ip_tun_rx_dst(skb
, flags
, tunnel_id
, md_size
);
196 tun_dst
= ipv6_tun_rx_dst(skb
, flags
, tunnel_id
, md_size
);
200 info
= &tun_dst
->u
.tun_info
;
201 info
->key
.tp_src
= udp_hdr(skb
)->source
;
202 info
->key
.tp_dst
= udp_hdr(skb
)->dest
;
203 if (udp_hdr(skb
)->check
)
204 __set_bit(IP_TUNNEL_CSUM_BIT
, info
->key
.tun_flags
);
207 EXPORT_SYMBOL_GPL(udp_tun_rx_dst
);
209 struct rtable
*udp_tunnel_dst_lookup(struct sk_buff
*skb
,
210 struct net_device
*dev
,
211 struct net
*net
, int oif
,
213 const struct ip_tunnel_key
*key
,
214 __be16 sport
, __be16 dport
, u8 tos
,
215 struct dst_cache
*dst_cache
)
217 struct rtable
*rt
= NULL
;
220 #ifdef CONFIG_DST_CACHE
222 rt
= dst_cache_get_ip4(dst_cache
, saddr
);
228 memset(&fl4
, 0, sizeof(fl4
));
229 fl4
.flowi4_mark
= skb
->mark
;
230 fl4
.flowi4_proto
= IPPROTO_UDP
;
231 fl4
.flowi4_oif
= oif
;
232 fl4
.daddr
= key
->u
.ipv4
.dst
;
233 fl4
.saddr
= key
->u
.ipv4
.src
;
234 fl4
.fl4_dport
= dport
;
235 fl4
.fl4_sport
= sport
;
236 fl4
.flowi4_tos
= tos
& INET_DSCP_MASK
;
237 fl4
.flowi4_flags
= key
->flow_flags
;
239 rt
= ip_route_output_key(net
, &fl4
);
241 netdev_dbg(dev
, "no route to %pI4\n", &fl4
.daddr
);
242 return ERR_PTR(-ENETUNREACH
);
244 if (rt
->dst
.dev
== dev
) { /* is this necessary? */
245 netdev_dbg(dev
, "circular route to %pI4\n", &fl4
.daddr
);
247 return ERR_PTR(-ELOOP
);
249 #ifdef CONFIG_DST_CACHE
251 dst_cache_set_ip4(dst_cache
, &rt
->dst
, fl4
.saddr
);
256 EXPORT_SYMBOL_GPL(udp_tunnel_dst_lookup
);
258 MODULE_DESCRIPTION("IPv4 Foo over UDP tunnel driver");
259 MODULE_LICENSE("GPL");