linux/net/ipv6/ip6_udp_tunnel.c
<<
>>
Prefs
   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
  15int 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(AF_INET6, SOCK_DGRAM, 0, &sock);
  23        if (err < 0)
  24                goto error;
  25
  26        sk_change_net(sock->sk, net);
  27
  28        if (cfg->ipv6_v6only) {
  29                int val = 1;
  30
  31                err = kernel_setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
  32                                        (char *) &val, sizeof(val));
  33                if (err < 0)
  34                        goto error;
  35        }
  36
  37        udp6_addr.sin6_family = AF_INET6;
  38        memcpy(&udp6_addr.sin6_addr, &cfg->local_ip6,
  39               sizeof(udp6_addr.sin6_addr));
  40        udp6_addr.sin6_port = cfg->local_udp_port;
  41        err = kernel_bind(sock, (struct sockaddr *)&udp6_addr,
  42                          sizeof(udp6_addr));
  43        if (err < 0)
  44                goto error;
  45
  46        if (cfg->peer_udp_port) {
  47                memset(&udp6_addr, 0, sizeof(udp6_addr));
  48                udp6_addr.sin6_family = AF_INET6;
  49                memcpy(&udp6_addr.sin6_addr, &cfg->peer_ip6,
  50                       sizeof(udp6_addr.sin6_addr));
  51                udp6_addr.sin6_port = cfg->peer_udp_port;
  52                err = kernel_connect(sock,
  53                                     (struct sockaddr *)&udp6_addr,
  54                                     sizeof(udp6_addr), 0);
  55        }
  56        if (err < 0)
  57                goto error;
  58
  59        udp_set_no_check6_tx(sock->sk, !cfg->use_udp6_tx_checksums);
  60        udp_set_no_check6_rx(sock->sk, !cfg->use_udp6_rx_checksums);
  61
  62        *sockp = sock;
  63        return 0;
  64
  65error:
  66        if (sock) {
  67                kernel_sock_shutdown(sock, SHUT_RDWR);
  68                sk_release_kernel(sock->sk);
  69        }
  70        *sockp = NULL;
  71        return err;
  72}
  73EXPORT_SYMBOL_GPL(udp_sock_create6);
  74
  75int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
  76                         struct sk_buff *skb,
  77                         struct net_device *dev, struct in6_addr *saddr,
  78                         struct in6_addr *daddr,
  79                         __u8 prio, __u8 ttl, __be32 label,
  80                         __be16 src_port, __be16 dst_port, bool nocheck)
  81{
  82        struct udphdr *uh;
  83        struct ipv6hdr *ip6h;
  84
  85        __skb_push(skb, sizeof(*uh));
  86        skb_reset_transport_header(skb);
  87        uh = udp_hdr(skb);
  88
  89        uh->dest = dst_port;
  90        uh->source = src_port;
  91
  92        uh->len = htons(skb->len);
  93
  94        memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  95        IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
  96                            | IPSKB_REROUTED);
  97        skb_dst_set(skb, dst);
  98
  99        udp6_set_csum(nocheck, skb, saddr, daddr, skb->len);
 100
 101        __skb_push(skb, sizeof(*ip6h));
 102        skb_reset_network_header(skb);
 103        ip6h              = ipv6_hdr(skb);
 104        ip6_flow_hdr(ip6h, prio, label);
 105        ip6h->payload_len = htons(skb->len);
 106        ip6h->nexthdr     = IPPROTO_UDP;
 107        ip6h->hop_limit   = ttl;
 108        ip6h->daddr       = *daddr;
 109        ip6h->saddr       = *saddr;
 110
 111        ip6tunnel_xmit(sk, skb, dev);
 112        return 0;
 113}
 114EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
 115
 116MODULE_LICENSE("GPL");
 117