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 <linux/in6.h> 8 #include <net/udp.h> 9 #include <net/udp_tunnel.h> 10 #include <net/net_namespace.h> 11 #include <net/netns/generic.h> 12 #include <net/ip6_tunnel.h> 13 #include <net/ip6_checksum.h> 14 15 int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg, 16 struct socket **sockp) 17 { 18 struct sockaddr_in6 udp6_addr = {}; 19 int err; 20 struct socket *sock = NULL; 21 22 err = sock_create_kern(net, AF_INET6, SOCK_DGRAM, 0, &sock); 23 if (err < 0) 24 goto error; 25 26 if (cfg->ipv6_v6only) { 27 int val = 1; 28 29 err = kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 30 (char *) &val, sizeof(val)); 31 if (err < 0) 32 goto error; 33 } 34 if (cfg->bind_ifindex) { 35 struct net_device *dev; 36 37 dev = dev_get_by_index(net, cfg->bind_ifindex); 38 if (!dev) { 39 err = -ENODEV; 40 goto error; 41 } 42 43 err = kernel_setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, 44 dev->name, strlen(dev->name) + 1); 45 dev_put(dev); 46 47 if (err < 0) 48 goto error; 49 } 50 51 udp6_addr.sin6_family = AF_INET6; 52 memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6, 53 sizeof(udp6_addr.sin6_addr)); 54 udp6_addr.sin6_port = cfg->local_udp_port; 55 err = kernel_bind(sock, (struct sockaddr *)&udp6_addr, 56 sizeof(udp6_addr)); 57 if (err < 0) 58 goto error; 59 60 if (cfg->peer_udp_port) { 61 memset(&udp6_addr, 0, sizeof(udp6_addr)); 62 udp6_addr.sin6_family = AF_INET6; 63 memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6, 64 sizeof(udp6_addr.sin6_addr)); 65 udp6_addr.sin6_port = cfg->peer_udp_port; 66 err = kernel_connect(sock, 67 (struct sockaddr *)&udp6_addr, 68 sizeof(udp6_addr), 0); 69 } 70 if (err < 0) 71 goto error; 72 73 udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums); 74 udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums); 75 76 *sockp = sock; 77 return 0; 78 79 error: 80 if (sock) { 81 kernel_sock_shutdown(sock, SHUT_RDWR); 82 sock_release(sock); 83 } 84 *sockp = NULL; 85 return err; 86 } 87 EXPORT_SYMBOL_GPL(udp_sock_create6); 88 89 int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk, 90 struct sk_buff *skb, 91 struct net_device *dev, struct in6_addr *saddr, 92 struct in6_addr *daddr, 93 __u8 prio, __u8 ttl, __be32 label, 94 __be16 src_port, __be16 dst_port, bool nocheck) 95 { 96 struct udphdr *uh; 97 struct ipv6hdr *ip6h; 98 99 __skb_push(skb, sizeof(*uh)); 100 skb_reset_transport_header(skb); 101 uh = udp_hdr(skb); 102 103 uh->dest = dst_port; 104 uh->source = src_port; 105 106 uh->len = htons(skb->len); 107 108 skb_dst_set(skb, dst); 109 110 udp6_set_csum(nocheck, skb, saddr, daddr, skb->len); 111 112 __skb_push(skb, sizeof(*ip6h)); 113 skb_reset_network_header(skb); 114 ip6h = ipv6_hdr(skb); 115 ip6_flow_hdr(ip6h, prio, label); 116 ip6h->payload_len = htons(skb->len); 117 ip6h->nexthdr = IPPROTO_UDP; 118 ip6h->hop_limit = ttl; 119 ip6h->daddr = *daddr; 120 ip6h->saddr = *saddr; 121 122 ip6tunnel_xmit(sk, skb, dev); 123 return 0; 124 } 125 EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb); 126 127 MODULE_LICENSE("GPL"); 128