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>
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>
15 int udp_sock_create6(struct net
*net
, struct udp_port_cfg
*cfg
,
16 struct socket
**sockp
)
18 struct sockaddr_in6 udp6_addr
= {};
20 struct socket
*sock
= NULL
;
22 err
= sock_create_kern(net
, AF_INET6
, SOCK_DGRAM
, 0, &sock
);
26 if (cfg
->ipv6_v6only
) {
29 err
= kernel_setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
,
30 (char *) &val
, sizeof(val
));
35 udp6_addr
.sin6_family
= AF_INET6
;
36 memcpy(&udp6_addr
.sin6_addr
, &cfg
->local_ip6
,
37 sizeof(udp6_addr
.sin6_addr
));
38 udp6_addr
.sin6_port
= cfg
->local_udp_port
;
39 err
= kernel_bind(sock
, (struct sockaddr
*)&udp6_addr
,
44 if (cfg
->peer_udp_port
) {
45 memset(&udp6_addr
, 0, sizeof(udp6_addr
));
46 udp6_addr
.sin6_family
= AF_INET6
;
47 memcpy(&udp6_addr
.sin6_addr
, &cfg
->peer_ip6
,
48 sizeof(udp6_addr
.sin6_addr
));
49 udp6_addr
.sin6_port
= cfg
->peer_udp_port
;
50 err
= kernel_connect(sock
,
51 (struct sockaddr
*)&udp6_addr
,
52 sizeof(udp6_addr
), 0);
57 udp_set_no_check6_tx(sock
->sk
, !cfg
->use_udp6_tx_checksums
);
58 udp_set_no_check6_rx(sock
->sk
, !cfg
->use_udp6_rx_checksums
);
65 kernel_sock_shutdown(sock
, SHUT_RDWR
);
71 EXPORT_SYMBOL_GPL(udp_sock_create6
);
73 int udp_tunnel6_xmit_skb(struct dst_entry
*dst
, struct sock
*sk
,
75 struct net_device
*dev
, struct in6_addr
*saddr
,
76 struct in6_addr
*daddr
,
77 __u8 prio
, __u8 ttl
, __be32 label
,
78 __be16 src_port
, __be16 dst_port
, bool nocheck
)
83 __skb_push(skb
, sizeof(*uh
));
84 skb_reset_transport_header(skb
);
88 uh
->source
= src_port
;
90 uh
->len
= htons(skb
->len
);
92 skb_dst_set(skb
, dst
);
94 udp6_set_csum(nocheck
, skb
, saddr
, daddr
, skb
->len
);
96 __skb_push(skb
, sizeof(*ip6h
));
97 skb_reset_network_header(skb
);
99 ip6_flow_hdr(ip6h
, prio
, label
);
100 ip6h
->payload_len
= htons(skb
->len
);
101 ip6h
->nexthdr
= IPPROTO_UDP
;
102 ip6h
->hop_limit
= ttl
;
103 ip6h
->daddr
= *daddr
;
104 ip6h
->saddr
= *saddr
;
106 ip6tunnel_xmit(sk
, skb
, dev
);
109 EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb
);
111 MODULE_LICENSE("GPL");