~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/udp_tunnel.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/dst_metadata.h>
  8 #include <net/net_namespace.h>
  9 #include <net/udp.h>
 10 #include <net/udp_tunnel.h>
 11 
 12 int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
 13                      struct socket **sockp)
 14 {
 15         int err;
 16         struct socket *sock = NULL;
 17         struct sockaddr_in udp_addr;
 18 
 19         err = sock_create_kern(net, AF_INET, SOCK_DGRAM, 0, &sock);
 20         if (err < 0)
 21                 goto error;
 22 
 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;
 30 
 31         if (cfg->peer_udp_port) {
 32                 udp_addr.sin_family = AF_INET;
 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);
 37                 if (err < 0)
 38                         goto error;
 39         }
 40 
 41         sock->sk->sk_no_check_tx = !cfg->use_udp_checksums;
 42 
 43         *sockp = sock;
 44         return 0;
 45 
 46 error:
 47         if (sock) {
 48                 kernel_sock_shutdown(sock, SHUT_RDWR);
 49                 sock_release(sock);
 50         }
 51         *sockp = NULL;
 52         return err;
 53 }
 54 EXPORT_SYMBOL(udp_sock_create4);
 55 
 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 */
 65         inet_inc_convert_csum(sk);
 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;
 72         udp_sk(sk)->gro_receive = cfg->gro_receive;
 73         udp_sk(sk)->gro_complete = cfg->gro_complete;
 74 
 75         udp_tunnel_encap_enable(sock);
 76 }
 77 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
 78 
 79 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
 80                              unsigned short type)
 81 {
 82         struct sock *sk = sock->sk;
 83         struct udp_tunnel_info ti;
 84 
 85         if (!dev->netdev_ops->ndo_udp_tunnel_add)
 86                 return;
 87 
 88         ti.type = type;
 89         ti.sa_family = sk->sk_family;
 90         ti.port = inet_sk(sk)->inet_sport;
 91 
 92         dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
 93 }
 94 EXPORT_SYMBOL_GPL(udp_tunnel_push_rx_port);
 95 
 96 /* Notify netdevs that UDP port started listening */
 97 void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type)
 98 {
 99         struct sock *sk = sock->sk;
100         struct net *net = sock_net(sk);
101         struct udp_tunnel_info ti;
102         struct net_device *dev;
103 
104         ti.type = type;
105         ti.sa_family = sk->sk_family;
106         ti.port = inet_sk(sk)->inet_sport;
107 
108         rcu_read_lock();
109         for_each_netdev_rcu(net, dev) {
110                 if (!dev->netdev_ops->ndo_udp_tunnel_add)
111                         continue;
112                 dev->netdev_ops->ndo_udp_tunnel_add(dev, &ti);
113         }
114         rcu_read_unlock();
115 }
116 EXPORT_SYMBOL_GPL(udp_tunnel_notify_add_rx_port);
117 
118 /* Notify netdevs that UDP port is no more listening */
119 void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type)
120 {
121         struct sock *sk = sock->sk;
122         struct net *net = sock_net(sk);
123         struct udp_tunnel_info ti;
124         struct net_device *dev;
125 
126         ti.type = type;
127         ti.sa_family = sk->sk_family;
128         ti.port = inet_sk(sk)->inet_sport;
129 
130         rcu_read_lock();
131         for_each_netdev_rcu(net, dev) {
132                 if (!dev->netdev_ops->ndo_udp_tunnel_del)
133                         continue;
134                 dev->netdev_ops->ndo_udp_tunnel_del(dev, &ti);
135         }
136         rcu_read_unlock();
137 }
138 EXPORT_SYMBOL_GPL(udp_tunnel_notify_del_rx_port);
139 
140 void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
141                          __be32 src, __be32 dst, __u8 tos, __u8 ttl,
142                          __be16 df, __be16 src_port, __be16 dst_port,
143                          bool xnet, bool nocheck)
144 {
145         struct udphdr *uh;
146 
147         __skb_push(skb, sizeof(*uh));
148         skb_reset_transport_header(skb);
149         uh = udp_hdr(skb);
150 
151         uh->dest = dst_port;
152         uh->source = src_port;
153         uh->len = htons(skb->len);
154 
155         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
156 
157         udp_set_csum(nocheck, skb, src, dst, skb->len);
158 
159         iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, xnet);
160 }
161 EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
162 
163 void udp_tunnel_sock_release(struct socket *sock)
164 {
165         rcu_assign_sk_user_data(sock->sk, NULL);
166         kernel_sock_shutdown(sock, SHUT_RDWR);
167         sock_release(sock);
168 }
169 EXPORT_SYMBOL_GPL(udp_tunnel_sock_release);
170 
171 struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb,  unsigned short family,
172                                     __be16 flags, __be64 tunnel_id, int md_size)
173 {
174         struct metadata_dst *tun_dst;
175         struct ip_tunnel_info *info;
176 
177         if (family == AF_INET)
178                 tun_dst = ip_tun_rx_dst(skb, flags, tunnel_id, md_size);
179         else
180                 tun_dst = ipv6_tun_rx_dst(skb, flags, tunnel_id, md_size);
181         if (!tun_dst)
182                 return NULL;
183 
184         info = &tun_dst->u.tun_info;
185         info->key.tp_src = udp_hdr(skb)->source;
186         info->key.tp_dst = udp_hdr(skb)->dest;
187         if (udp_hdr(skb)->check)
188                 info->key.tun_flags |= TUNNEL_CSUM;
189         return tun_dst;
190 }
191 EXPORT_SYMBOL_GPL(udp_tun_rx_dst);
192 
193 MODULE_LICENSE("GPL");
194 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp