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>
8 #include <net/udp_tunnel.h>
9 #include <net/net_namespace.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(AF_INET
, SOCK_DGRAM
, 0, &sock
);
22 sk_change_net(sock
->sk
, net
);
24 udp_addr
.sin_family
= AF_INET
;
25 udp_addr
.sin_addr
= cfg
->local_ip
;
26 udp_addr
.sin_port
= cfg
->local_udp_port
;
27 err
= kernel_bind(sock
, (struct sockaddr
*)&udp_addr
,
32 if (cfg
->peer_udp_port
) {
33 udp_addr
.sin_family
= AF_INET
;
34 udp_addr
.sin_addr
= cfg
->peer_ip
;
35 udp_addr
.sin_port
= cfg
->peer_udp_port
;
36 err
= kernel_connect(sock
, (struct sockaddr
*)&udp_addr
,
42 sock
->sk
->sk_no_check_tx
= !cfg
->use_udp_checksums
;
49 kernel_sock_shutdown(sock
, SHUT_RDWR
);
50 sk_release_kernel(sock
->sk
);
55 EXPORT_SYMBOL(udp_sock_create4
);
57 void setup_udp_tunnel_sock(struct net
*net
, struct socket
*sock
,
58 struct udp_tunnel_sock_cfg
*cfg
)
60 struct sock
*sk
= sock
->sk
;
62 /* Disable multicast loopback */
63 inet_sk(sk
)->mc_loop
= 0;
65 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
66 udp_set_convert_csum(sk
, true);
68 rcu_assign_sk_user_data(sk
, cfg
->sk_user_data
);
70 udp_sk(sk
)->encap_type
= cfg
->encap_type
;
71 udp_sk(sk
)->encap_rcv
= cfg
->encap_rcv
;
72 udp_sk(sk
)->encap_destroy
= cfg
->encap_destroy
;
74 udp_tunnel_encap_enable(sock
);
76 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock
);
78 int udp_tunnel_xmit_skb(struct socket
*sock
, struct rtable
*rt
,
79 struct sk_buff
*skb
, __be32 src
, __be32 dst
,
80 __u8 tos
, __u8 ttl
, __be16 df
, __be16 src_port
,
81 __be16 dst_port
, bool xnet
)
85 __skb_push(skb
, sizeof(*uh
));
86 skb_reset_transport_header(skb
);
90 uh
->source
= src_port
;
91 uh
->len
= htons(skb
->len
);
93 udp_set_csum(sock
->sk
->sk_no_check_tx
, skb
, src
, dst
, skb
->len
);
95 return iptunnel_xmit(sock
->sk
, rt
, skb
, src
, dst
, IPPROTO_UDP
,
98 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb
);
100 void udp_tunnel_sock_release(struct socket
*sock
)
102 rcu_assign_sk_user_data(sock
->sk
, NULL
);
103 kernel_sock_shutdown(sock
, SHUT_RDWR
);
104 sk_release_kernel(sock
->sk
);
106 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release
);
108 MODULE_LICENSE("GPL");