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> |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 7 | #include <net/dst_metadata.h> |
| 8 | #include <net/net_namespace.h> |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 9 | #include <net/udp.h> |
| 10 | #include <net/udp_tunnel.h> |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 11 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 12 | int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg, |
| 13 | struct socket **sockp) |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 14 | { |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 15 | int err; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 16 | struct socket *sock = NULL; |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 17 | struct sockaddr_in udp_addr; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 18 | |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 19 | err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock); |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 20 | if (err < 0) |
| 21 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 22 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 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, |
| 27 | sizeof(udp_addr)); |
| 28 | if (err < 0) |
| 29 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 30 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 31 | if (cfg->peer_udp_port) { |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 32 | udp_addr.sin_family = AF_INET; |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 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, |
| 36 | sizeof(udp_addr), 0); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 37 | if (err < 0) |
| 38 | goto error; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 41 | sock->sk->sk_no_check_tx = !cfg->use_udp_checksums; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 42 | |
| 43 | *sockp = sock; |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 44 | return 0; |
| 45 | |
| 46 | error: |
| 47 | if (sock) { |
| 48 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 49 | sock_release(sock); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 50 | } |
| 51 | *sockp = NULL; |
| 52 | return err; |
| 53 | } |
Andy Zhou | fd38441 | 2014-09-16 17:31:16 -0700 | [diff] [blame] | 54 | EXPORT_SYMBOL(udp_sock_create4); |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 55 | |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 56 | void setup_udp_tunnel_sock(struct net *net, struct socket *sock, |
| 57 | struct udp_tunnel_sock_cfg *cfg) |
| 58 | { |
| 59 | struct sock *sk = sock->sk; |
| 60 | |
| 61 | /* Disable multicast loopback */ |
| 62 | inet_sk(sk)->mc_loop = 0; |
| 63 | |
| 64 | /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */ |
Tom Herbert | 224d019 | 2015-01-05 13:56:14 -0800 | [diff] [blame] | 65 | inet_inc_convert_csum(sk); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 66 | |
| 67 | rcu_assign_sk_user_data(sk, cfg->sk_user_data); |
| 68 | |
| 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; |
Tom Herbert | 38fd2af | 2016-04-05 08:22:52 -0700 | [diff] [blame] | 72 | udp_sk(sk)->gro_receive = cfg->gro_receive; |
| 73 | udp_sk(sk)->gro_complete = cfg->gro_complete; |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 74 | |
| 75 | udp_tunnel_encap_enable(sock); |
| 76 | } |
| 77 | EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock); |
| 78 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 79 | void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb, |
| 80 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, |
| 81 | __be16 df, __be16 src_port, __be16 dst_port, |
| 82 | bool xnet, bool nocheck) |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 83 | { |
| 84 | struct udphdr *uh; |
| 85 | |
| 86 | __skb_push(skb, sizeof(*uh)); |
| 87 | skb_reset_transport_header(skb); |
| 88 | uh = udp_hdr(skb); |
| 89 | |
| 90 | uh->dest = dst_port; |
| 91 | uh->source = src_port; |
| 92 | uh->len = htons(skb->len); |
| 93 | |
Bernie Harris | 5146d1f | 2016-02-22 12:58:05 +1300 | [diff] [blame] | 94 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 95 | |
Tom Herbert | d998f8e | 2015-01-20 11:23:04 -0800 | [diff] [blame] | 96 | udp_set_csum(nocheck, skb, src, dst, skb->len); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 97 | |
Pravin B Shelar | 039f506 | 2015-12-24 14:34:54 -0800 | [diff] [blame] | 98 | iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb); |
| 101 | |
| 102 | void udp_tunnel_sock_release(struct socket *sock) |
| 103 | { |
| 104 | rcu_assign_sk_user_data(sock->sk, NULL); |
| 105 | kernel_sock_shutdown(sock, SHUT_RDWR); |
Eric W. Biederman | 26abe14 | 2015-05-08 21:10:31 -0500 | [diff] [blame] | 106 | sock_release(sock); |
Andy Zhou | 6a93cc9 | 2014-09-16 17:31:17 -0700 | [diff] [blame] | 107 | } |
| 108 | EXPORT_SYMBOL_GPL(udp_tunnel_sock_release); |
| 109 | |
Pravin B Shelar | c29a70d | 2015-08-26 23:46:50 -0700 | [diff] [blame] | 110 | struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family, |
| 111 | __be16 flags, __be64 tunnel_id, int md_size) |
| 112 | { |
| 113 | struct metadata_dst *tun_dst; |
| 114 | struct ip_tunnel_info *info; |
| 115 | |
| 116 | if (family == AF_INET) |
| 117 | tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size); |
| 118 | else |
| 119 | tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size); |
| 120 | if (!tun_dst) |
| 121 | return NULL; |
| 122 | |
| 123 | info = &tun_dst->u.tun_info; |
| 124 | info->key.tp_src = udp_hdr(skb)->source; |
| 125 | info->key.tp_dst = udp_hdr(skb)->dest; |
| 126 | if (udp_hdr(skb)->check) |
| 127 | info->key.tun_flags |= TUNNEL_CSUM; |
| 128 | return tun_dst; |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(udp_tun_rx_dst); |
| 131 | |
Tom Herbert | 8024e02 | 2014-07-13 19:49:37 -0700 | [diff] [blame] | 132 | MODULE_LICENSE("GPL"); |