Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 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/udp.h> |
| 8 | #include <net/udp_tunnel.h> |
| 9 | #include <net/net_namespace.h> |
| 10 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 11 | int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg, |
| 12 | struct socket **sockp) |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 13 | { |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 14 | int err; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 15 | struct socket *sock = NULL; |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 16 | struct sockaddr_in udp_addr; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 17 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 18 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 19 | if (err < 0) |
| 20 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 21 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 22 | udp_addr.sin_family = AF_INET; |
| 23 | udp_addr.sin_addr = cfg->local_ip; |
| 24 | udp_addr.sin_port = cfg->local_udp_port; |
| 25 | err = kernel_bind(sock, (struct sockaddr *)&udp_addr, |
| 26 | sizeof(udp_addr)); |
| 27 | if (err < 0) |
| 28 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 29 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 30 | if (cfg->peer_udp_port) { |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 31 | udp_addr.sin_family = AF_INET; |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 32 | udp_addr.sin_addr = cfg->peer_ip; |
| 33 | udp_addr.sin_port = cfg->peer_udp_port; |
| 34 | err = kernel_connect(sock, (struct sockaddr *)&udp_addr, |
| 35 | sizeof(udp_addr), 0); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 36 | if (err < 0) |
| 37 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 40 | sock->sk->sk_no_check_tx = !cfg->use_udp_checksums; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 41 | |
| 42 | *sockp = sock; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | |
| 45 | error: |
| 46 | if (sock) { |
| 47 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 48 | sock_release(sock); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 49 | } |
| 50 | *sockp = NULL; |
| 51 | return err; |
| 52 | } |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 53 | EXPORT_SYMBOL(udp_sock_create4); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 54 | |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 55 | void setup_udp_tunnel_sock(struct net *net, struct socket *sock, |
| 56 | struct udp_tunnel_sock_cfg *cfg) |
| 57 | { |
| 58 | struct sock *sk = sock->sk; |
| 59 | |
| 60 | /* Disable multicast loopback */ |
| 61 | inet_sk(sk)->mc_loop = 0; |
| 62 | |
| 63 | /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */ |
Tom Herbert | 224d019 | 2015-01-05 13:56:14 -0800 | [diff] [blame] | 64 | inet_inc_convert_csum(sk); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 65 | |
| 66 | rcu_assign_sk_user_data(sk, cfg->sk_user_data); |
| 67 | |
| 68 | udp_sk(sk)->encap_type = cfg->encap_type; |
| 69 | udp_sk(sk)->encap_rcv = cfg->encap_rcv; |
| 70 | udp_sk(sk)->encap_destroy = cfg->encap_destroy; |
| 71 | |
| 72 | udp_tunnel_encap_enable(sock); |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock); |
| 75 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 76 | int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb, |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 77 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, |
| 78 | __be16 df, __be16 src_port, __be16 dst_port, |
| 79 | bool xnet, bool nocheck) |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 80 | { |
| 81 | struct udphdr *uh; |
| 82 | |
| 83 | __skb_push(skb, sizeof(*uh)); |
| 84 | skb_reset_transport_header(skb); |
| 85 | uh = udp_hdr(skb); |
| 86 | |
| 87 | uh->dest = dst_port; |
| 88 | uh->source = src_port; |
| 89 | uh->len = htons(skb->len); |
| 90 | |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 91 | udp_set_csum(nocheck, skb, src, dst, skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 92 | |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 93 | return iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 94 | tos, ttl, df, xnet); |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb); |
| 97 | |
| 98 | void udp_tunnel_sock_release(struct socket *sock) |
| 99 | { |
| 100 | rcu_assign_sk_user_data(sock->sk, NULL); |
| 101 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 102 | sock_release(sock); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 103 | } |
| 104 | EXPORT_SYMBOL_GPL(udp_tunnel_sock_release); |
| 105 | |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 106 | MODULE_LICENSE("GPL"); |