blob: 1e5fadd997b77a999c228dfece4a6d099c101491 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/ipv4/udp.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080035#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080036#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/ndisc.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070044#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/ip6_checksum.h>
46#include <net/xfrm.h>
47
48#include <linux/proc_fs.h>
49#include <linux/seq_file.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080050#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000052int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
53{
54 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
55 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
Eric Dumazetc720c7e2009-10-15 06:30:45 +000056 __be32 sk1_rcv_saddr = inet_sk(sk)->inet_rcv_saddr;
Vlad Yasevich499923c2009-04-09 17:37:33 +000057 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000058 int sk_ipv6only = ipv6_only_sock(sk);
59 int sk2_ipv6only = inet_v6_ipv6only(sk2);
60 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
61 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
62
63 /* if both are mapped, treat as IPv4 */
64 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
Vlad Yasevich499923c2009-04-09 17:37:33 +000065 return (!sk2_ipv6only &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +000066 (!sk1_rcv_saddr || !sk2_rcv_saddr ||
67 sk1_rcv_saddr == sk2_rcv_saddr));
Vlad Yasevichb2f5e7c2009-03-24 16:24:51 +000068
69 if (addr_type2 == IPV6_ADDR_ANY &&
70 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
71 return 1;
72
73 if (addr_type == IPV6_ADDR_ANY &&
74 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
75 return 1;
76
77 if (sk2_rcv_saddr6 &&
78 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
79 return 1;
80
81 return 0;
82}
83
Eric Dumazetd4cada42009-11-08 10:17:30 +000084static unsigned int udp6_portaddr_hash(struct net *net,
85 const struct in6_addr *addr6,
86 unsigned int port)
87{
88 unsigned int hash, mix = net_hash_mix(net);
89
90 if (ipv6_addr_any(addr6))
91 hash = jhash_1word(0, mix);
92 else if (ipv6_addr_type(addr6) == IPV6_ADDR_MAPPED)
93 hash = jhash_1word(addr6->s6_addr32[3], mix);
94 else
95 hash = jhash2(addr6->s6_addr32, 4, mix);
96
97 return hash ^ port;
98}
99
100
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700101int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Eric Dumazetd4cada42009-11-08 10:17:30 +0000103 /* precompute partial secondary hash */
104 udp_sk(sk)->udp_portaddr_hash =
105 udp6_portaddr_hash(sock_net(sk),
106 &inet6_sk(sk)->rcv_saddr,
107 0);
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700108 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Eric Dumazet645ca702008-10-29 01:41:45 -0700111static inline int compute_score(struct sock *sk, struct net *net,
112 unsigned short hnum,
113 struct in6_addr *saddr, __be16 sport,
114 struct in6_addr *daddr, __be16 dport,
115 int dif)
116{
117 int score = -1;
118
Eric Dumazetd4cada42009-11-08 10:17:30 +0000119 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
Eric Dumazet645ca702008-10-29 01:41:45 -0700120 sk->sk_family == PF_INET6) {
121 struct ipv6_pinfo *np = inet6_sk(sk);
122 struct inet_sock *inet = inet_sk(sk);
123
124 score = 0;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000125 if (inet->inet_dport) {
126 if (inet->inet_dport != sport)
Eric Dumazet645ca702008-10-29 01:41:45 -0700127 return -1;
128 score++;
129 }
130 if (!ipv6_addr_any(&np->rcv_saddr)) {
131 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
132 return -1;
133 score++;
134 }
135 if (!ipv6_addr_any(&np->daddr)) {
136 if (!ipv6_addr_equal(&np->daddr, saddr))
137 return -1;
138 score++;
139 }
140 if (sk->sk_bound_dev_if) {
141 if (sk->sk_bound_dev_if != dif)
142 return -1;
143 score++;
144 }
145 }
146 return score;
147}
148
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -0800149static struct sock *__udp6_lib_lookup(struct net *net,
150 struct in6_addr *saddr, __be16 sport,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800151 struct in6_addr *daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700152 int dif, struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700154 struct sock *sk, *result;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800155 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned short hnum = ntohs(dport);
Eric Dumazetf86dcc52009-10-07 00:37:59 +0000157 unsigned int hash = udp_hashfn(net, hnum, udptable->mask);
Eric Dumazet645ca702008-10-29 01:41:45 -0700158 struct udp_hslot *hslot = &udptable->hash[hash];
Eric Dumazet271b72c2008-10-29 02:11:14 -0700159 int score, badness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Eric Dumazet271b72c2008-10-29 02:11:14 -0700161 rcu_read_lock();
162begin:
163 result = NULL;
164 badness = -1;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800165 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
Eric Dumazet645ca702008-10-29 01:41:45 -0700166 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
167 if (score > badness) {
168 result = sk;
169 badness = score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
Eric Dumazet88ab1932008-11-16 19:39:21 -0800172 /*
173 * if the nulls value we got at the end of this lookup is
174 * not the expected one, we must restart lookup.
175 * We probably met an item that was moved to another chain.
176 */
177 if (get_nulls_value(node) != hash)
178 goto begin;
179
Eric Dumazet271b72c2008-10-29 02:11:14 -0700180 if (result) {
181 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
182 result = NULL;
183 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
184 daddr, dport, dif) < badness)) {
185 sock_put(result);
186 goto begin;
187 }
188 }
189 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return result;
191}
192
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700193static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
194 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700195 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700196{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700197 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700198 struct ipv6hdr *iph = ipv6_hdr(skb);
199
KOVACS Krisztian23542612008-10-07 12:41:01 -0700200 if (unlikely(sk = skb_steal_sock(skb)))
201 return sk;
Eric Dumazetadf30902009-06-02 05:19:30 +0000202 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
203 &iph->daddr, dport, inet6_iif(skb),
204 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * This should be easy, if there is something there we
209 * return it, otherwise we block.
210 */
211
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800212int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct msghdr *msg, size_t len,
214 int noblock, int flags, int *addr_len)
215{
216 struct ipv6_pinfo *np = inet6_sk(sk);
217 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900218 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700219 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800220 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700221 int err;
222 int is_udplite = IS_UDPLITE(sk);
Wei Yongjunf26ba172008-11-02 16:11:01 +0000223 int is_udp4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900225 if (addr_len)
226 *addr_len=sizeof(struct sockaddr_in6);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (flags & MSG_ERRQUEUE)
229 return ipv6_recv_error(sk, msg, len);
230
231try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800232 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
233 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!skb)
235 goto out;
236
Herbert Xu759e5d02007-03-25 20:10:56 -0700237 ulen = skb->len - sizeof(struct udphdr);
238 copied = len;
239 if (copied > ulen)
240 copied = ulen;
241 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900242 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Wei Yongjunf26ba172008-11-02 16:11:01 +0000244 is_udp4 = (skb->protocol == htons(ETH_P_IP));
245
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800246 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700247 * If checksum is needed at all, try to do it while copying the
248 * data. If the data is truncated, or if we only want a partial
249 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800250 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800251
Herbert Xu759e5d02007-03-25 20:10:56 -0700252 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
253 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800255 }
256
Herbert Xu60476372007-04-09 11:59:39 -0700257 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800258 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
259 msg->msg_iov, copied );
260 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
262 if (err == -EINVAL)
263 goto csum_copy_err;
264 }
265 if (err)
266 goto out_free;
267
Wei Yongjunf26ba172008-11-02 16:11:01 +0000268 if (!peeked) {
269 if (is_udp4)
270 UDP_INC_STATS_USER(sock_net(sk),
271 UDP_MIB_INDATAGRAMS, is_udplite);
272 else
273 UDP6_INC_STATS_USER(sock_net(sk),
274 UDP_MIB_INDATAGRAMS, is_udplite);
275 }
Wang Chencb759942007-12-03 22:33:28 +1100276
Neil Horman3b885782009-10-12 13:26:31 -0700277 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Copy the address. */
280 if (msg->msg_name) {
281 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sin6 = (struct sockaddr_in6 *) msg->msg_name;
284 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300285 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sin6->sin6_flowinfo = 0;
287 sin6->sin6_scope_id = 0;
288
Wei Yongjunf26ba172008-11-02 16:11:01 +0000289 if (is_udp4)
Brian Haleyb301e822009-10-07 13:58:25 -0700290 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
291 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700293 ipv6_addr_copy(&sin6->sin6_addr,
294 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
296 sin6->sin6_scope_id = IP6CB(skb)->iif;
297 }
298
299 }
Wei Yongjunf26ba172008-11-02 16:11:01 +0000300 if (is_udp4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (inet->cmsg_flags)
302 ip_cmsg_recv(msg, skb);
303 } else {
304 if (np->rxopt.all)
305 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 err = copied;
309 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700310 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312out_free:
Eric Dumazet9d410c72009-10-30 05:03:53 +0000313 skb_free_datagram_locked(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314out:
315 return err;
316
317csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800318 lock_sock(sk);
Wei Yongjun0856f932008-11-02 16:14:27 +0000319 if (!skb_kill_datagram(sk, skb, flags)) {
320 if (is_udp4)
321 UDP_INC_STATS_USER(sock_net(sk),
322 UDP_MIB_INERRORS, is_udplite);
323 else
324 UDP6_INC_STATS_USER(sock_net(sk),
325 UDP_MIB_INERRORS, is_udplite);
326 }
Hideo Aoki95766ff2007-12-31 00:29:24 -0800327 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Mitsuru Chinen7a0ff7162007-11-05 21:29:17 -0800329 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto try_again;
332}
333
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800334void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700335 u8 type, u8 code, int offset, __be32 info,
Eric Dumazet645ca702008-10-29 01:41:45 -0700336 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct ipv6_pinfo *np;
339 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct in6_addr *saddr = &hdr->saddr;
341 struct in6_addr *daddr = &hdr->daddr;
342 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
343 struct sock *sk;
344 int err;
345
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900346 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800347 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (sk == NULL)
349 return;
350
351 np = inet6_sk(sk);
352
353 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
354 goto out;
355
356 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
357 goto out;
358
359 if (np->recverr)
360 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
361
362 sk->sk_err = err;
363 sk->sk_error_report(sk);
364out:
365 sock_put(sk);
366}
367
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800368static __inline__ void udpv6_err(struct sk_buff *skb,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700369 struct inet6_skb_parm *opt, u8 type,
370 u8 code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Eric Dumazet645ca702008-10-29 01:41:45 -0700372 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800373}
374
375int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
376{
377 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700378 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100379 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700380
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800381 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
382 goto drop;
383
384 /*
385 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
386 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100387 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800388
389 if (up->pcrlen == 0) { /* full coverage was set */
390 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
391 " %d while full coverage %d requested\n",
392 UDP_SKB_CB(skb)->cscov, skb->len);
393 goto drop;
394 }
395 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
396 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
397 "too small, need min %d\n",
398 UDP_SKB_CB(skb)->cscov, up->pcrlen);
399 goto drop;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800403 if (sk->sk_filter) {
404 if (udp_lib_checksum_complete(skb))
405 goto drop;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Eric Dumazet766e90372009-10-14 20:40:11 -0700408 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
David S. Millera18135e2006-08-15 00:00:09 -0700409 /* Note that an ENOMEM error is charged twice */
Eric Dumazet766e90372009-10-14 20:40:11 -0700410 if (rc == -ENOMEM)
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700411 UDP6_INC_STATS_BH(sock_net(sk),
412 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000413 goto drop_no_sk_drops_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Wang Chencb759942007-12-03 22:33:28 +1100415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800417drop:
Eric Dumazet8edf19c2009-10-15 00:12:40 +0000418 atomic_inc(&sk->sk_drops);
419drop_no_sk_drops_inc:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700420 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800421 kfree_skb(skb);
422 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Eric Dumazet920a4612008-11-01 21:22:23 -0700425static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
Al Viroe69a4adc2006-11-14 20:56:00 -0800426 __be16 loc_port, struct in6_addr *loc_addr,
427 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int dif)
429{
Eric Dumazet88ab1932008-11-16 19:39:21 -0800430 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct sock *s = sk;
432 unsigned short num = ntohs(loc_port);
433
Eric Dumazet88ab1932008-11-16 19:39:21 -0800434 sk_nulls_for_each_from(s, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct inet_sock *inet = inet_sk(s);
436
Eric Dumazet920a4612008-11-01 21:22:23 -0700437 if (!net_eq(sock_net(s), net))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800438 continue;
439
Eric Dumazetd4cada42009-11-08 10:17:30 +0000440 if (udp_sk(s)->udp_port_hash == num &&
441 s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct ipv6_pinfo *np = inet6_sk(s);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000443 if (inet->inet_dport) {
444 if (inet->inet_dport != rmt_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 continue;
446 }
447 if (!ipv6_addr_any(&np->daddr) &&
448 !ipv6_addr_equal(&np->daddr, rmt_addr))
449 continue;
450
451 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
452 continue;
453
454 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700455 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
456 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800458 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 continue;
460 return s;
461 }
462 }
463 return NULL;
464}
465
466/*
467 * Note: called only from the BH handler context,
468 * so we don't need to lock the hashes.
469 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700470static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
471 struct in6_addr *saddr, struct in6_addr *daddr,
Eric Dumazet645ca702008-10-29 01:41:45 -0700472 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300475 const struct udphdr *uh = udp_hdr(skb);
Eric Dumazetf86dcc52009-10-07 00:37:59 +0000476 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int dif;
478
Eric Dumazet645ca702008-10-29 01:41:45 -0700479 spin_lock(&hslot->lock);
Eric Dumazet88ab1932008-11-16 19:39:21 -0800480 sk = sk_nulls_head(&hslot->head);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800481 dif = inet6_iif(skb);
Eric Dumazet920a4612008-11-01 21:22:23 -0700482 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!sk) {
484 kfree_skb(skb);
485 goto out;
486 }
487
488 sk2 = sk;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800489 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 uh->source, saddr, dif))) {
491 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800492 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700493 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800494 if (!sock_owned_by_user(sk2))
495 udpv6_queue_rcv_skb(sk2, buff);
496 else
497 sk_add_backlog(sk2, buff);
498 bh_unlock_sock(sk2);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Herbert Xud97106e2008-08-09 00:35:05 -0700501 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800502 if (!sock_owned_by_user(sk))
503 udpv6_queue_rcv_skb(sk, skb);
504 else
505 sk_add_backlog(sk, skb);
506 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507out:
Eric Dumazet645ca702008-10-29 01:41:45 -0700508 spin_unlock(&hslot->lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Herbert Xu759e5d02007-03-25 20:10:56 -0700512static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
513 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800514{
Herbert Xu759e5d02007-03-25 20:10:56 -0700515 int err;
516
517 UDP_SKB_CB(skb)->partial_cov = 0;
518 UDP_SKB_CB(skb)->cscov = skb->len;
519
David S. Millerdb8dac22008-03-06 16:22:02 -0800520 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700521 err = udplite_checksum_init(skb, uh);
522 if (err)
523 return err;
524 }
525
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800526 if (uh->check == 0) {
527 /* RFC 2460 section 8.1 says that we SHOULD log
528 this error. Well, it is reasonable.
529 */
530 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
531 return 1;
532 }
533 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700534 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700535 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800536 skb->ip_summed = CHECKSUM_UNNECESSARY;
537
Herbert Xu60476372007-04-09 11:59:39 -0700538 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700539 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
540 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700541 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800542
Herbert Xu759e5d02007-03-25 20:10:56 -0700543 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800544}
545
Eric Dumazet645ca702008-10-29 01:41:45 -0700546int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700547 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct net_device *dev = skb->dev;
552 struct in6_addr *saddr, *daddr;
553 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700554 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
557 goto short_packet;
558
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700559 saddr = &ipv6_hdr(skb)->saddr;
560 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300561 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800564 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto short_packet;
566
Herbert Xu759e5d02007-03-25 20:10:56 -0700567 if (proto == IPPROTO_UDP) {
568 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800570 /* Check for jumbo payload */
571 if (ulen == 0)
572 ulen = skb->len;
573
574 if (ulen < sizeof(*uh))
575 goto short_packet;
576
577 if (ulen < skb->len) {
578 if (pskb_trim_rcsum(skb, ulen))
579 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700580 saddr = &ipv6_hdr(skb)->saddr;
581 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300582 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Herbert Xu759e5d02007-03-25 20:10:56 -0700586 if (udp6_csum_init(skb, uh, proto))
587 goto discard;
588
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900589 /*
590 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800592 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700593 return __udp6_lib_mcast_deliver(net, skb,
594 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* Unicast */
597
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900598 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * check socket cache ... must talk to Alan about his plans
600 * for sock caches... i'll skip this for now.
601 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700602 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (sk == NULL) {
605 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
606 goto discard;
607
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800608 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700610 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
611 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
614
615 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900620
Herbert Xud97106e2008-08-09 00:35:05 -0700621 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800622 if (!sock_owned_by_user(sk))
623 udpv6_queue_rcv_skb(sk, skb);
624 else
625 sk_add_backlog(sk, skb);
626 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900630short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800631 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800632 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700633 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700636 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800640
Herbert Xue5bbef22007-10-15 12:50:28 -0700641static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800642{
Eric Dumazet645ca702008-10-29 01:41:45 -0700643 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Throw away all pending data and cancel the corking. Socket is locked.
648 */
649static void udp_v6_flush_pending_frames(struct sock *sk)
650{
651 struct udp_sock *up = udp_sk(sk);
652
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400653 if (up->pending == AF_INET)
654 udp_flush_pending_frames(sk);
655 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 up->len = 0;
657 up->pending = 0;
658 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000662/**
663 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
664 * @sk: socket we are sending on
665 * @skb: sk_buff containing the filled-in UDP header
666 * (checksum field must be zeroed out)
667 */
668static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
669 const struct in6_addr *saddr,
670 const struct in6_addr *daddr, int len)
671{
672 unsigned int offset;
673 struct udphdr *uh = udp_hdr(skb);
674 __wsum csum = 0;
675
676 if (skb_queue_len(&sk->sk_write_queue) == 1) {
677 /* Only one fragment on the socket. */
678 skb->csum_start = skb_transport_header(skb) - skb->head;
679 skb->csum_offset = offsetof(struct udphdr, check);
680 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
681 } else {
682 /*
683 * HW-checksum won't work as there are two or more
684 * fragments on the socket so that all csums of sk_buffs
685 * should be together
686 */
687 offset = skb_transport_offset(skb);
688 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
689
690 skb->ip_summed = CHECKSUM_NONE;
691
692 skb_queue_walk(&sk->sk_write_queue, skb) {
693 csum = csum_add(csum, skb->csum);
694 }
695
696 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
697 csum);
698 if (uh->check == 0)
699 uh->check = CSUM_MANGLED_0;
700 }
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/*
704 * Sending
705 */
706
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800707static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct sk_buff *skb;
710 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800711 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct inet_sock *inet = inet_sk(sk);
713 struct flowi *fl = &inet->cork.fl;
714 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100715 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800716 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* Grab the skbuff where UDP header space exists. */
719 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
720 goto out;
721
722 /*
723 * Create a UDP header
724 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300725 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 uh->source = fl->fl_ip_sport;
727 uh->dest = fl->fl_ip_dport;
728 uh->len = htons(up->len);
729 uh->check = 0;
730
Wang Chenb2bf1e22007-12-03 22:34:16 +1100731 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800732 csum = udplite_csum_outgoing(sk, skb);
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000733 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
734 udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst,
735 up->len);
736 goto send;
737 } else
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800738 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800740 /* add protocol-dependent pseudo-header */
741 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
742 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800744 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000746send:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 err = ip6_push_pending_frames(sk);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700748 if (err) {
749 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
750 UDP6_INC_STATS_USER(sock_net(sk),
751 UDP_MIB_SNDBUFERRORS, is_udplite);
752 err = 0;
753 }
754 } else
755 UDP6_INC_STATS_USER(sock_net(sk),
756 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757out:
758 up->len = 0;
759 up->pending = 0;
760 return err;
761}
762
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800763int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct msghdr *msg, size_t len)
765{
766 struct ipv6_txoptions opt_space;
767 struct udp_sock *up = udp_sk(sk);
768 struct inet_sock *inet = inet_sk(sk);
769 struct ipv6_pinfo *np = inet6_sk(sk);
770 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
771 struct in6_addr *daddr, *final_p = NULL, final;
772 struct ipv6_txoptions *opt = NULL;
773 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700774 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct dst_entry *dst;
776 int addr_len = msg->msg_namelen;
777 int ulen = len;
778 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900779 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
781 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700782 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100783 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800784 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* destination address check */
787 if (sin6) {
788 if (addr_len < offsetof(struct sockaddr, sa_data))
789 return -EINVAL;
790
791 switch (sin6->sin6_family) {
792 case AF_INET6:
793 if (addr_len < SIN6_LEN_RFC2133)
794 return -EINVAL;
795 daddr = &sin6->sin6_addr;
796 break;
797 case AF_INET:
798 goto do_udp_sendmsg;
799 case AF_UNSPEC:
800 msg->msg_name = sin6 = NULL;
801 msg->msg_namelen = addr_len = 0;
802 daddr = NULL;
803 break;
804 default:
805 return -EINVAL;
806 }
807 } else if (!up->pending) {
808 if (sk->sk_state != TCP_ESTABLISHED)
809 return -EDESTADDRREQ;
810 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900811 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 daddr = NULL;
813
814 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700815 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct sockaddr_in sin;
817 sin.sin_family = AF_INET;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000818 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sin.sin_addr.s_addr = daddr->s6_addr32[3];
820 msg->msg_name = &sin;
821 msg->msg_namelen = sizeof(sin);
822do_udp_sendmsg:
823 if (__ipv6_only_sock(sk))
824 return -ENETUNREACH;
825 return udp_sendmsg(iocb, sk, msg, len);
826 }
827 }
828
829 if (up->pending == AF_INET)
830 return udp_sendmsg(iocb, sk, msg, len);
831
832 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700833 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
835 if (len > INT_MAX - sizeof(struct udphdr))
836 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (up->pending) {
839 /*
840 * There are pending frames.
841 * The socket lock must be held while it's corked.
842 */
843 lock_sock(sk);
844 if (likely(up->pending)) {
845 if (unlikely(up->pending != AF_INET6)) {
846 release_sock(sk);
847 return -EAFNOSUPPORT;
848 }
849 dst = NULL;
850 goto do_append_data;
851 }
852 release_sock(sk);
853 }
854 ulen += sizeof(struct udphdr);
855
Herbert Xu132a55f2006-10-03 14:34:00 -0700856 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (sin6) {
859 if (sin6->sin6_port == 0)
860 return -EINVAL;
861
Herbert Xu132a55f2006-10-03 14:34:00 -0700862 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 daddr = &sin6->sin6_addr;
864
865 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700866 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
867 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
868 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (flowlabel == NULL)
870 return -EINVAL;
871 daddr = &flowlabel->dst;
872 }
873 }
874
875 /*
876 * Otherwise it will be difficult to maintain
877 * sk->sk_dst_cache.
878 */
879 if (sk->sk_state == TCP_ESTABLISHED &&
880 ipv6_addr_equal(daddr, &np->daddr))
881 daddr = &np->daddr;
882
883 if (addr_len >= sizeof(struct sockaddr_in6) &&
884 sin6->sin6_scope_id &&
885 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700886 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else {
888 if (sk->sk_state != TCP_ESTABLISHED)
889 return -EDESTADDRREQ;
890
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000891 fl.fl_ip_dport = inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700893 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700894 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
Herbert Xu132a55f2006-10-03 14:34:00 -0700897 if (!fl.oif)
898 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Yang Hongyang9f690db2008-12-16 02:08:29 -0800900 if (!fl.oif)
901 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
902
Brian Haley51953d52009-10-05 08:24:16 +0000903 fl.mark = sk->sk_mark;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (msg->msg_controllen) {
906 opt = &opt_space;
907 memset(opt, 0, sizeof(struct ipv6_txoptions));
908 opt->tot_len = sizeof(*opt);
909
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900910 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (err < 0) {
912 fl6_sock_release(flowlabel);
913 return err;
914 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700915 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
916 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (flowlabel == NULL)
918 return -EINVAL;
919 }
920 if (!(opt->opt_nflen|opt->opt_flen))
921 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700922 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 if (opt == NULL)
925 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900926 if (flowlabel)
927 opt = fl6_merge_options(&opt_space, flowlabel, opt);
928 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800930 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400931 if (!ipv6_addr_any(daddr))
932 ipv6_addr_copy(&fl.fl6_dst, daddr);
933 else
934 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700935 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
936 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000937 fl.fl_ip_sport = inet->inet_sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* merge ip6_build_xmit from ip6_output */
940 if (opt && opt->srcrt) {
941 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700942 ipv6_addr_copy(&final, &fl.fl6_dst);
943 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700945 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947
Herbert Xu132a55f2006-10-03 14:34:00 -0700948 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
949 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700950 connected = 0;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Herbert Xu132a55f2006-10-03 14:34:00 -0700953 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700954
Herbert Xu132a55f2006-10-03 14:34:00 -0700955 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (err)
957 goto out;
958 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700959 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800961 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
962 if (err < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700963 if (err == -EREMOTE)
964 err = ip6_dst_blackhole(sk, &dst, &fl);
965 if (err < 0)
966 goto out;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700970 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 hlimit = np->mcast_hops;
972 else
973 hlimit = np->hop_limit;
974 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400975 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Gerrit Renkere651f032009-08-09 08:12:48 +0000978 if (tclass < 0)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900979 tclass = np->tclass;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (msg->msg_flags&MSG_CONFIRM)
982 goto do_confirm;
983back_from_confirm:
984
985 lock_sock(sk);
986 if (unlikely(up->pending)) {
987 /* The socket is already corked while preparing it. */
988 /* ... which is an evident application bug. --ANK */
989 release_sock(sk);
990
Patrick McHardy64ce2072005-08-09 20:50:53 -0700991 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 err = -EINVAL;
993 goto out;
994 }
995
996 up->pending = AF_INET6;
997
998do_append_data:
999 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001000 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1001 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -07001002 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +09001003 (struct rt6_info*)dst,
1004 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (err)
1006 udp_v6_flush_pending_frames(sk);
1007 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001008 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -07001009 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1010 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David S. Millera5e7c212005-10-03 14:21:58 -07001012 if (dst) {
1013 if (connected) {
1014 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -07001015 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001016 &np->daddr : NULL,
1017#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -07001018 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001019 &np->saddr :
1020#endif
1021 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -07001022 } else {
1023 dst_release(dst);
1024 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001025 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -07001026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (err > 0)
1029 err = np->recverr ? net_xmit_errno(err) : 0;
1030 release_sock(sk);
1031out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001032 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -07001034 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return len;
David S. Millera18135e2006-08-15 00:00:09 -07001036 /*
1037 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1038 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1039 * we don't have a good statistic (IpOutDiscards but it can be too many
1040 * things). We could add another new stat but at least for now that
1041 * seems like overkill.
1042 */
1043 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -07001044 UDP6_INC_STATS_USER(sock_net(sk),
1045 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -07001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return err;
1048
1049do_confirm:
1050 dst_confirm(dst);
1051 if (!(msg->msg_flags&MSG_PROBE) || len)
1052 goto back_from_confirm;
1053 err = 0;
1054 goto out;
1055}
1056
Brian Haley7d06b2e2008-06-14 17:04:49 -07001057void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 lock_sock(sk);
1060 udp_v6_flush_pending_frames(sk);
1061 release_sock(sk);
1062
1063 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
1067 * Socket option code for UDP
1068 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001069int udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001070 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001071{
David S. Millerdb8dac22008-03-06 16:22:02 -08001072 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001073 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1074 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001075 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001076}
1077
1078#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001079int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001080 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001081{
David S. Millerdb8dac22008-03-06 16:22:02 -08001082 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001083 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1084 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001085 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001086}
1087#endif
1088
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001089int udpv6_getsockopt(struct sock *sk, int level, int optname,
1090 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001091{
David S. Millerdb8dac22008-03-06 16:22:02 -08001092 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001093 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001094 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001095}
1096
1097#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001098int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1099 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001100{
David S. Millerdb8dac22008-03-06 16:22:02 -08001101 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001102 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001103 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001104}
1105#endif
1106
Sridhar Samudralaba735422009-07-09 08:10:04 +00001107static int udp6_ufo_send_check(struct sk_buff *skb)
1108{
1109 struct ipv6hdr *ipv6h;
1110 struct udphdr *uh;
1111
1112 if (!pskb_may_pull(skb, sizeof(*uh)))
1113 return -EINVAL;
1114
1115 ipv6h = ipv6_hdr(skb);
1116 uh = udp_hdr(skb);
1117
1118 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1119 IPPROTO_UDP, 0);
1120 skb->csum_start = skb_transport_header(skb) - skb->head;
1121 skb->csum_offset = offsetof(struct udphdr, check);
1122 skb->ip_summed = CHECKSUM_PARTIAL;
1123 return 0;
1124}
1125
1126static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features)
1127{
1128 struct sk_buff *segs = ERR_PTR(-EINVAL);
1129 unsigned int mss;
1130 unsigned int unfrag_ip6hlen, unfrag_len;
1131 struct frag_hdr *fptr;
1132 u8 *mac_start, *prevhdr;
1133 u8 nexthdr;
1134 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1135 int offset;
1136 __wsum csum;
1137
1138 mss = skb_shinfo(skb)->gso_size;
1139 if (unlikely(skb->len <= mss))
1140 goto out;
1141
1142 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1143 /* Packet is from an untrusted source, reset gso_segs. */
1144 int type = skb_shinfo(skb)->gso_type;
1145
1146 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1147 !(type & (SKB_GSO_UDP))))
1148 goto out;
1149
1150 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1151
1152 segs = NULL;
1153 goto out;
1154 }
1155
1156 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1157 * do checksum of UDP packets sent as multiple IP fragments.
1158 */
1159 offset = skb->csum_start - skb_headroom(skb);
1160 csum = skb_checksum(skb, offset, skb->len- offset, 0);
1161 offset += skb->csum_offset;
1162 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1163 skb->ip_summed = CHECKSUM_NONE;
1164
1165 /* Check if there is enough headroom to insert fragment header. */
1166 if ((skb_headroom(skb) < frag_hdr_sz) &&
1167 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1168 goto out;
1169
1170 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1171 * bytes to insert fragment header.
1172 */
1173 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1174 nexthdr = *prevhdr;
1175 *prevhdr = NEXTHDR_FRAGMENT;
1176 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1177 unfrag_ip6hlen;
1178 mac_start = skb_mac_header(skb);
1179 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1180
1181 skb->mac_header -= frag_hdr_sz;
1182 skb->network_header -= frag_hdr_sz;
1183
1184 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1185 fptr->nexthdr = nexthdr;
1186 fptr->reserved = 0;
1187 ipv6_select_ident(fptr);
1188
1189 /* Fragment the skb. ipv6 header and the remaining fields of the
1190 * fragment header are updated in ipv6_gso_segment()
1191 */
1192 segs = skb_segment(skb, features);
1193
1194out:
1195 return segs;
1196}
1197
Alexey Dobriyan41135cc2009-09-14 12:22:28 +00001198static const struct inet6_protocol udpv6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 .handler = udpv6_rcv,
1200 .err_handler = udpv6_err,
Sridhar Samudralaba735422009-07-09 08:10:04 +00001201 .gso_send_check = udp6_ufo_send_check,
1202 .gso_segment = udp6_ufo_fragment,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1204};
1205
1206/* ------------------------------------------------------------------------ */
1207#ifdef CONFIG_PROC_FS
1208
1209static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1210{
1211 struct inet_sock *inet = inet_sk(sp);
1212 struct ipv6_pinfo *np = inet6_sk(sp);
1213 struct in6_addr *dest, *src;
1214 __u16 destp, srcp;
1215
1216 dest = &np->daddr;
1217 src = &np->rcv_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001218 destp = ntohs(inet->inet_dport);
1219 srcp = ntohs(inet->inet_sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 seq_printf(seq,
Eric Dumazetf86dcc52009-10-07 00:37:59 +00001221 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001222 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 bucket,
1224 src->s6_addr32[0], src->s6_addr32[1],
1225 src->s6_addr32[2], src->s6_addr32[3], srcp,
1226 dest->s6_addr32[0], dest->s6_addr32[1],
1227 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001228 sp->sk_state,
Eric Dumazet31e6d362009-06-17 19:05:41 -07001229 sk_wmem_alloc_get(sp),
1230 sk_rmem_alloc_get(sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 0, 0L, 0,
1232 sock_i_uid(sp), 0,
1233 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001234 atomic_read(&sp->sk_refcnt), sp,
1235 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001238int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 if (v == SEQ_START_TOKEN)
1241 seq_printf(seq,
1242 " sl "
1243 "local_address "
1244 "remote_address "
1245 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001246 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 else
1248 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1249 return 0;
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .name = "udp6",
1254 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001255 .udp_table = &udp_table,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001256 .seq_fops = {
1257 .owner = THIS_MODULE,
1258 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001259 .seq_ops = {
1260 .show = udp6_seq_show,
1261 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262};
1263
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001264int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001266 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001269void udp6_proc_exit(struct net *net) {
1270 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272#endif /* CONFIG_PROC_FS */
1273
1274/* ------------------------------------------------------------------------ */
1275
1276struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001277 .name = "UDPv6",
1278 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001279 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001280 .connect = ip6_datagram_connect,
1281 .disconnect = udp_disconnect,
1282 .ioctl = udp_ioctl,
1283 .destroy = udpv6_destroy_sock,
1284 .setsockopt = udpv6_setsockopt,
1285 .getsockopt = udpv6_getsockopt,
1286 .sendmsg = udpv6_sendmsg,
1287 .recvmsg = udpv6_recvmsg,
1288 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001289 .hash = udp_lib_hash,
1290 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001291 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001292 .memory_allocated = &udp_memory_allocated,
1293 .sysctl_mem = sysctl_udp_mem,
1294 .sysctl_wmem = &sysctl_udp_wmem_min,
1295 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001296 .obj_size = sizeof(struct udp6_sock),
Eric Dumazet271b72c2008-10-29 02:11:14 -07001297 .slab_flags = SLAB_DESTROY_BY_RCU,
Eric Dumazet645ca702008-10-29 01:41:45 -07001298 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001299#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001300 .compat_setsockopt = compat_udpv6_setsockopt,
1301 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303};
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static struct inet_protosw udpv6_protosw = {
1306 .type = SOCK_DGRAM,
1307 .protocol = IPPROTO_UDP,
1308 .prot = &udpv6_prot,
1309 .ops = &inet6_dgram_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 .no_check = UDP_CSUM_DEFAULT,
1311 .flags = INET_PROTOSW_PERMANENT,
1312};
1313
1314
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001315int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001317 int ret;
1318
1319 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1320 if (ret)
1321 goto out;
1322
1323 ret = inet6_register_protosw(&udpv6_protosw);
1324 if (ret)
1325 goto out_udpv6_protocol;
1326out:
1327 return ret;
1328
1329out_udpv6_protocol:
1330 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1331 goto out;
1332}
1333
Daniel Lezcano09f77092007-12-13 05:34:58 -08001334void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001335{
1336 inet6_unregister_protosw(&udpv6_protosw);
1337 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}