blob: 829d300a6f35b08e9fd1c10df4e718eff3500c00 [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
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070084int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070086 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Eric Dumazet645ca702008-10-29 01:41:45 -070089static inline int compute_score(struct sock *sk, struct net *net,
90 unsigned short hnum,
91 struct in6_addr *saddr, __be16 sport,
92 struct in6_addr *daddr, __be16 dport,
93 int dif)
94{
95 int score = -1;
96
97 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
98 sk->sk_family == PF_INET6) {
99 struct ipv6_pinfo *np = inet6_sk(sk);
100 struct inet_sock *inet = inet_sk(sk);
101
102 score = 0;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000103 if (inet->inet_dport) {
104 if (inet->inet_dport != sport)
Eric Dumazet645ca702008-10-29 01:41:45 -0700105 return -1;
106 score++;
107 }
108 if (!ipv6_addr_any(&np->rcv_saddr)) {
109 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
110 return -1;
111 score++;
112 }
113 if (!ipv6_addr_any(&np->daddr)) {
114 if (!ipv6_addr_equal(&np->daddr, saddr))
115 return -1;
116 score++;
117 }
118 if (sk->sk_bound_dev_if) {
119 if (sk->sk_bound_dev_if != dif)
120 return -1;
121 score++;
122 }
123 }
124 return score;
125}
126
Pavel Emelyanovfa4d3c62008-01-31 05:07:57 -0800127static struct sock *__udp6_lib_lookup(struct net *net,
128 struct in6_addr *saddr, __be16 sport,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800129 struct in6_addr *daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700130 int dif, struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700132 struct sock *sk, *result;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800133 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned short hnum = ntohs(dport);
Eric Dumazetf86dcc52009-10-07 00:37:59 +0000135 unsigned int hash = udp_hashfn(net, hnum, udptable->mask);
Eric Dumazet645ca702008-10-29 01:41:45 -0700136 struct udp_hslot *hslot = &udptable->hash[hash];
Eric Dumazet271b72c2008-10-29 02:11:14 -0700137 int score, badness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Eric Dumazet271b72c2008-10-29 02:11:14 -0700139 rcu_read_lock();
140begin:
141 result = NULL;
142 badness = -1;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800143 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
Eric Dumazet645ca702008-10-29 01:41:45 -0700144 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
145 if (score > badness) {
146 result = sk;
147 badness = score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 }
Eric Dumazet88ab1932008-11-16 19:39:21 -0800150 /*
151 * if the nulls value we got at the end of this lookup is
152 * not the expected one, we must restart lookup.
153 * We probably met an item that was moved to another chain.
154 */
155 if (get_nulls_value(node) != hash)
156 goto begin;
157
Eric Dumazet271b72c2008-10-29 02:11:14 -0700158 if (result) {
159 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
160 result = NULL;
161 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
162 daddr, dport, dif) < badness)) {
163 sock_put(result);
164 goto begin;
165 }
166 }
167 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return result;
169}
170
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700171static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
172 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700173 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700174{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700175 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700176 struct ipv6hdr *iph = ipv6_hdr(skb);
177
KOVACS Krisztian23542612008-10-07 12:41:01 -0700178 if (unlikely(sk = skb_steal_sock(skb)))
179 return sk;
Eric Dumazetadf30902009-06-02 05:19:30 +0000180 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
181 &iph->daddr, dport, inet6_iif(skb),
182 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * This should be easy, if there is something there we
187 * return it, otherwise we block.
188 */
189
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800190int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct msghdr *msg, size_t len,
192 int noblock, int flags, int *addr_len)
193{
194 struct ipv6_pinfo *np = inet6_sk(sk);
195 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900196 struct sk_buff *skb;
Herbert Xu759e5d02007-03-25 20:10:56 -0700197 unsigned int ulen, copied;
Herbert Xua59322b2007-12-05 01:53:40 -0800198 int peeked;
Herbert Xu759e5d02007-03-25 20:10:56 -0700199 int err;
200 int is_udplite = IS_UDPLITE(sk);
Wei Yongjunf26ba172008-11-02 16:11:01 +0000201 int is_udp4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900203 if (addr_len)
204 *addr_len=sizeof(struct sockaddr_in6);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (flags & MSG_ERRQUEUE)
207 return ipv6_recv_error(sk, msg, len);
208
209try_again:
Herbert Xua59322b2007-12-05 01:53:40 -0800210 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
211 &peeked, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!skb)
213 goto out;
214
Herbert Xu759e5d02007-03-25 20:10:56 -0700215 ulen = skb->len - sizeof(struct udphdr);
216 copied = len;
217 if (copied > ulen)
218 copied = ulen;
219 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900220 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Wei Yongjunf26ba172008-11-02 16:11:01 +0000222 is_udp4 = (skb->protocol == htons(ETH_P_IP));
223
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800224 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700225 * If checksum is needed at all, try to do it while copying the
226 * data. If the data is truncated, or if we only want a partial
227 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800228 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800229
Herbert Xu759e5d02007-03-25 20:10:56 -0700230 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
231 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800233 }
234
Herbert Xu60476372007-04-09 11:59:39 -0700235 if (skb_csum_unnecessary(skb))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800236 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
237 msg->msg_iov, copied );
238 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
240 if (err == -EINVAL)
241 goto csum_copy_err;
242 }
243 if (err)
244 goto out_free;
245
Wei Yongjunf26ba172008-11-02 16:11:01 +0000246 if (!peeked) {
247 if (is_udp4)
248 UDP_INC_STATS_USER(sock_net(sk),
249 UDP_MIB_INDATAGRAMS, is_udplite);
250 else
251 UDP6_INC_STATS_USER(sock_net(sk),
252 UDP_MIB_INDATAGRAMS, is_udplite);
253 }
Wang Chencb759942007-12-03 22:33:28 +1100254
Neil Horman3b885782009-10-12 13:26:31 -0700255 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Copy the address. */
258 if (msg->msg_name) {
259 struct sockaddr_in6 *sin6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sin6 = (struct sockaddr_in6 *) msg->msg_name;
262 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300263 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 sin6->sin6_flowinfo = 0;
265 sin6->sin6_scope_id = 0;
266
Wei Yongjunf26ba172008-11-02 16:11:01 +0000267 if (is_udp4)
Brian Haleyb301e822009-10-07 13:58:25 -0700268 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
269 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700271 ipv6_addr_copy(&sin6->sin6_addr,
272 &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
274 sin6->sin6_scope_id = IP6CB(skb)->iif;
275 }
276
277 }
Wei Yongjunf26ba172008-11-02 16:11:01 +0000278 if (is_udp4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (inet->cmsg_flags)
280 ip_cmsg_recv(msg, skb);
281 } else {
282 if (np->rxopt.all)
283 datagram_recv_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 err = copied;
287 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700288 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290out_free:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800291 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 skb_free_datagram(sk, skb);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800293 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294out:
295 return err;
296
297csum_copy_err:
Hideo Aoki95766ff2007-12-31 00:29:24 -0800298 lock_sock(sk);
Wei Yongjun0856f932008-11-02 16:14:27 +0000299 if (!skb_kill_datagram(sk, skb, flags)) {
300 if (is_udp4)
301 UDP_INC_STATS_USER(sock_net(sk),
302 UDP_MIB_INERRORS, is_udplite);
303 else
304 UDP6_INC_STATS_USER(sock_net(sk),
305 UDP_MIB_INERRORS, is_udplite);
306 }
Hideo Aoki95766ff2007-12-31 00:29:24 -0800307 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mitsuru Chinen7a0ff7162007-11-05 21:29:17 -0800309 if (flags & MSG_DONTWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 goto try_again;
312}
313
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800314void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700315 u8 type, u8 code, int offset, __be32 info,
Eric Dumazet645ca702008-10-29 01:41:45 -0700316 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct ipv6_pinfo *np;
319 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct in6_addr *saddr = &hdr->saddr;
321 struct in6_addr *daddr = &hdr->daddr;
322 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
323 struct sock *sk;
324 int err;
325
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900326 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800327 saddr, uh->source, inet6_iif(skb), udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (sk == NULL)
329 return;
330
331 np = inet6_sk(sk);
332
333 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
334 goto out;
335
336 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
337 goto out;
338
339 if (np->recverr)
340 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
341
342 sk->sk_err = err;
343 sk->sk_error_report(sk);
344out:
345 sock_put(sk);
346}
347
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800348static __inline__ void udpv6_err(struct sk_buff *skb,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700349 struct inet6_skb_parm *opt, u8 type,
350 u8 code, int offset, __be32 info )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Eric Dumazet645ca702008-10-29 01:41:45 -0700352 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800353}
354
355int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
356{
357 struct udp_sock *up = udp_sk(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700358 int rc;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100359 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700360
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800361 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
362 goto drop;
363
364 /*
365 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
366 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100367 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800368
369 if (up->pcrlen == 0) { /* full coverage was set */
370 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
371 " %d while full coverage %d requested\n",
372 UDP_SKB_CB(skb)->cscov, skb->len);
373 goto drop;
374 }
375 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
376 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
377 "too small, need min %d\n",
378 UDP_SKB_CB(skb)->cscov, up->pcrlen);
379 goto drop;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Herbert Xu1ab6eb62007-03-06 20:29:58 -0800383 if (sk->sk_filter) {
384 if (udp_lib_checksum_complete(skb))
385 goto drop;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Eric Dumazet766e90372009-10-14 20:40:11 -0700388 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
David S. Millera18135e2006-08-15 00:00:09 -0700389 /* Note that an ENOMEM error is charged twice */
Eric Dumazet766e90372009-10-14 20:40:11 -0700390 if (rc == -ENOMEM)
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700391 UDP6_INC_STATS_BH(sock_net(sk),
392 UDP_MIB_RCVBUFERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800393 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Wang Chencb759942007-12-03 22:33:28 +1100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800397drop:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700398 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800399 kfree_skb(skb);
400 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Eric Dumazet920a4612008-11-01 21:22:23 -0700403static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
Al Viroe69a4adc2006-11-14 20:56:00 -0800404 __be16 loc_port, struct in6_addr *loc_addr,
405 __be16 rmt_port, struct in6_addr *rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int dif)
407{
Eric Dumazet88ab1932008-11-16 19:39:21 -0800408 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct sock *s = sk;
410 unsigned short num = ntohs(loc_port);
411
Eric Dumazet88ab1932008-11-16 19:39:21 -0800412 sk_nulls_for_each_from(s, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct inet_sock *inet = inet_sk(s);
414
Eric Dumazet920a4612008-11-01 21:22:23 -0700415 if (!net_eq(sock_net(s), net))
Daniel Lezcanob8ad0cb2008-03-07 11:16:55 -0800416 continue;
417
Eric Dumazet95f30b32007-02-09 15:44:52 -0800418 if (s->sk_hash == num && s->sk_family == PF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct ipv6_pinfo *np = inet6_sk(s);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000420 if (inet->inet_dport) {
421 if (inet->inet_dport != rmt_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 continue;
423 }
424 if (!ipv6_addr_any(&np->daddr) &&
425 !ipv6_addr_equal(&np->daddr, rmt_addr))
426 continue;
427
428 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
429 continue;
430
431 if (!ipv6_addr_any(&np->rcv_saddr)) {
David L Stevens40796c52005-09-14 21:10:20 -0700432 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
433 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800435 if (!inet6_mc_check(s, loc_addr, rmt_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 continue;
437 return s;
438 }
439 }
440 return NULL;
441}
442
443/*
444 * Note: called only from the BH handler context,
445 * so we don't need to lock the hashes.
446 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700447static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
448 struct in6_addr *saddr, struct in6_addr *daddr,
Eric Dumazet645ca702008-10-29 01:41:45 -0700449 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct sock *sk, *sk2;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300452 const struct udphdr *uh = udp_hdr(skb);
Eric Dumazetf86dcc52009-10-07 00:37:59 +0000453 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int dif;
455
Eric Dumazet645ca702008-10-29 01:41:45 -0700456 spin_lock(&hslot->lock);
Eric Dumazet88ab1932008-11-16 19:39:21 -0800457 sk = sk_nulls_head(&hslot->head);
YOSHIFUJI Hideakif2776ff2006-11-21 17:41:56 -0800458 dif = inet6_iif(skb);
Eric Dumazet920a4612008-11-01 21:22:23 -0700459 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!sk) {
461 kfree_skb(skb);
462 goto out;
463 }
464
465 sk2 = sk;
Eric Dumazet88ab1932008-11-16 19:39:21 -0800466 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 uh->source, saddr, dif))) {
468 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800469 if (buff) {
Herbert Xud97106e2008-08-09 00:35:05 -0700470 bh_lock_sock(sk2);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800471 if (!sock_owned_by_user(sk2))
472 udpv6_queue_rcv_skb(sk2, buff);
473 else
474 sk_add_backlog(sk2, buff);
475 bh_unlock_sock(sk2);
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Herbert Xud97106e2008-08-09 00:35:05 -0700478 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800479 if (!sock_owned_by_user(sk))
480 udpv6_queue_rcv_skb(sk, skb);
481 else
482 sk_add_backlog(sk, skb);
483 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484out:
Eric Dumazet645ca702008-10-29 01:41:45 -0700485 spin_unlock(&hslot->lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Herbert Xu759e5d02007-03-25 20:10:56 -0700489static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
490 int proto)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800491{
Herbert Xu759e5d02007-03-25 20:10:56 -0700492 int err;
493
494 UDP_SKB_CB(skb)->partial_cov = 0;
495 UDP_SKB_CB(skb)->cscov = skb->len;
496
David S. Millerdb8dac22008-03-06 16:22:02 -0800497 if (proto == IPPROTO_UDPLITE) {
Herbert Xu759e5d02007-03-25 20:10:56 -0700498 err = udplite_checksum_init(skb, uh);
499 if (err)
500 return err;
501 }
502
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800503 if (uh->check == 0) {
504 /* RFC 2460 section 8.1 says that we SHOULD log
505 this error. Well, it is reasonable.
506 */
507 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
508 return 1;
509 }
510 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700511 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700512 skb->len, proto, skb->csum))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800513 skb->ip_summed = CHECKSUM_UNNECESSARY;
514
Herbert Xu60476372007-04-09 11:59:39 -0700515 if (!skb_csum_unnecessary(skb))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700516 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
517 &ipv6_hdr(skb)->daddr,
Herbert Xu759e5d02007-03-25 20:10:56 -0700518 skb->len, proto, 0));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800519
Herbert Xu759e5d02007-03-25 20:10:56 -0700520 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800521}
522
Eric Dumazet645ca702008-10-29 01:41:45 -0700523int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700524 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct sock *sk;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900527 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct net_device *dev = skb->dev;
529 struct in6_addr *saddr, *daddr;
530 u32 ulen = 0;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700531 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
534 goto short_packet;
535
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700536 saddr = &ipv6_hdr(skb)->saddr;
537 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300538 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800541 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto short_packet;
543
Herbert Xu759e5d02007-03-25 20:10:56 -0700544 if (proto == IPPROTO_UDP) {
545 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800547 /* Check for jumbo payload */
548 if (ulen == 0)
549 ulen = skb->len;
550
551 if (ulen < sizeof(*uh))
552 goto short_packet;
553
554 if (ulen < skb->len) {
555 if (pskb_trim_rcsum(skb, ulen))
556 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700557 saddr = &ipv6_hdr(skb)->saddr;
558 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300559 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Herbert Xu759e5d02007-03-25 20:10:56 -0700563 if (udp6_csum_init(skb, uh, proto))
564 goto discard;
565
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900566 /*
567 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800569 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700570 return __udp6_lib_mcast_deliver(net, skb,
571 saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Unicast */
574
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900575 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * check socket cache ... must talk to Alan about his plans
577 * for sock caches... i'll skip this for now.
578 */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700579 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (sk == NULL) {
582 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
583 goto discard;
584
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800585 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto discard;
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700587 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
588 proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
591
592 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* deliver */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900597
Herbert Xud97106e2008-08-09 00:35:05 -0700598 bh_lock_sock(sk);
Hideo Aoki95766ff2007-12-31 00:29:24 -0800599 if (!sock_owned_by_user(sk))
600 udpv6_queue_rcv_skb(sk, skb);
601 else
602 sk_add_backlog(sk, skb);
603 bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 sock_put(sk);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900607short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800608 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -0800609 proto == IPPROTO_UDPLITE ? "-Lite" : "",
Herbert Xu759e5d02007-03-25 20:10:56 -0700610 ulen, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612discard:
Pavel Emelyanovef28d1a2008-07-05 21:19:40 -0700613 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800617
Herbert Xue5bbef22007-10-15 12:50:28 -0700618static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800619{
Eric Dumazet645ca702008-10-29 01:41:45 -0700620 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Throw away all pending data and cancel the corking. Socket is locked.
625 */
626static void udp_v6_flush_pending_frames(struct sock *sk)
627{
628 struct udp_sock *up = udp_sk(sk);
629
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400630 if (up->pending == AF_INET)
631 udp_flush_pending_frames(sk);
632 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 up->len = 0;
634 up->pending = 0;
635 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000639/**
640 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
641 * @sk: socket we are sending on
642 * @skb: sk_buff containing the filled-in UDP header
643 * (checksum field must be zeroed out)
644 */
645static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
646 const struct in6_addr *saddr,
647 const struct in6_addr *daddr, int len)
648{
649 unsigned int offset;
650 struct udphdr *uh = udp_hdr(skb);
651 __wsum csum = 0;
652
653 if (skb_queue_len(&sk->sk_write_queue) == 1) {
654 /* Only one fragment on the socket. */
655 skb->csum_start = skb_transport_header(skb) - skb->head;
656 skb->csum_offset = offsetof(struct udphdr, check);
657 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
658 } else {
659 /*
660 * HW-checksum won't work as there are two or more
661 * fragments on the socket so that all csums of sk_buffs
662 * should be together
663 */
664 offset = skb_transport_offset(skb);
665 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
666
667 skb->ip_summed = CHECKSUM_NONE;
668
669 skb_queue_walk(&sk->sk_write_queue, skb) {
670 csum = csum_add(csum, skb->csum);
671 }
672
673 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
674 csum);
675 if (uh->check == 0)
676 uh->check = CSUM_MANGLED_0;
677 }
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * Sending
682 */
683
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800684static int udp_v6_push_pending_frames(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct sk_buff *skb;
687 struct udphdr *uh;
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800688 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct inet_sock *inet = inet_sk(sk);
690 struct flowi *fl = &inet->cork.fl;
691 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100692 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -0800693 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* Grab the skbuff where UDP header space exists. */
696 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
697 goto out;
698
699 /*
700 * Create a UDP header
701 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300702 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 uh->source = fl->fl_ip_sport;
704 uh->dest = fl->fl_ip_dport;
705 uh->len = htons(up->len);
706 uh->check = 0;
707
Wang Chenb2bf1e22007-12-03 22:34:16 +1100708 if (is_udplite)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800709 csum = udplite_csum_outgoing(sk, skb);
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000710 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
711 udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst,
712 up->len);
713 goto send;
714 } else
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800715 csum = udp_csum_outgoing(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800717 /* add protocol-dependent pseudo-header */
718 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
719 up->len, fl->proto, csum );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800721 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Sridhar Samudrala493c6be2009-07-09 08:09:54 +0000723send:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 err = ip6_push_pending_frames(sk);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -0700725 if (err) {
726 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
727 UDP6_INC_STATS_USER(sock_net(sk),
728 UDP_MIB_SNDBUFERRORS, is_udplite);
729 err = 0;
730 }
731 } else
732 UDP6_INC_STATS_USER(sock_net(sk),
733 UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734out:
735 up->len = 0;
736 up->pending = 0;
737 return err;
738}
739
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800740int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct msghdr *msg, size_t len)
742{
743 struct ipv6_txoptions opt_space;
744 struct udp_sock *up = udp_sk(sk);
745 struct inet_sock *inet = inet_sk(sk);
746 struct ipv6_pinfo *np = inet6_sk(sk);
747 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
748 struct in6_addr *daddr, *final_p = NULL, final;
749 struct ipv6_txoptions *opt = NULL;
750 struct ip6_flowlabel *flowlabel = NULL;
Herbert Xu132a55f2006-10-03 14:34:00 -0700751 struct flowi fl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct dst_entry *dst;
753 int addr_len = msg->msg_namelen;
754 int ulen = len;
755 int hlimit = -1;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900756 int tclass = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
758 int err;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700759 int connected = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +1100760 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800761 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* destination address check */
764 if (sin6) {
765 if (addr_len < offsetof(struct sockaddr, sa_data))
766 return -EINVAL;
767
768 switch (sin6->sin6_family) {
769 case AF_INET6:
770 if (addr_len < SIN6_LEN_RFC2133)
771 return -EINVAL;
772 daddr = &sin6->sin6_addr;
773 break;
774 case AF_INET:
775 goto do_udp_sendmsg;
776 case AF_UNSPEC:
777 msg->msg_name = sin6 = NULL;
778 msg->msg_namelen = addr_len = 0;
779 daddr = NULL;
780 break;
781 default:
782 return -EINVAL;
783 }
784 } else if (!up->pending) {
785 if (sk->sk_state != TCP_ESTABLISHED)
786 return -EDESTADDRREQ;
787 daddr = &np->daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900788 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 daddr = NULL;
790
791 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700792 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct sockaddr_in sin;
794 sin.sin_family = AF_INET;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000795 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sin.sin_addr.s_addr = daddr->s6_addr32[3];
797 msg->msg_name = &sin;
798 msg->msg_namelen = sizeof(sin);
799do_udp_sendmsg:
800 if (__ipv6_only_sock(sk))
801 return -ENETUNREACH;
802 return udp_sendmsg(iocb, sk, msg, len);
803 }
804 }
805
806 if (up->pending == AF_INET)
807 return udp_sendmsg(iocb, sk, msg, len);
808
809 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -0700810 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812 if (len > INT_MAX - sizeof(struct udphdr))
813 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (up->pending) {
816 /*
817 * There are pending frames.
818 * The socket lock must be held while it's corked.
819 */
820 lock_sock(sk);
821 if (likely(up->pending)) {
822 if (unlikely(up->pending != AF_INET6)) {
823 release_sock(sk);
824 return -EAFNOSUPPORT;
825 }
826 dst = NULL;
827 goto do_append_data;
828 }
829 release_sock(sk);
830 }
831 ulen += sizeof(struct udphdr);
832
Herbert Xu132a55f2006-10-03 14:34:00 -0700833 memset(&fl, 0, sizeof(fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (sin6) {
836 if (sin6->sin6_port == 0)
837 return -EINVAL;
838
Herbert Xu132a55f2006-10-03 14:34:00 -0700839 fl.fl_ip_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 daddr = &sin6->sin6_addr;
841
842 if (np->sndflow) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700843 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
844 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
845 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (flowlabel == NULL)
847 return -EINVAL;
848 daddr = &flowlabel->dst;
849 }
850 }
851
852 /*
853 * Otherwise it will be difficult to maintain
854 * sk->sk_dst_cache.
855 */
856 if (sk->sk_state == TCP_ESTABLISHED &&
857 ipv6_addr_equal(daddr, &np->daddr))
858 daddr = &np->daddr;
859
860 if (addr_len >= sizeof(struct sockaddr_in6) &&
861 sin6->sin6_scope_id &&
862 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
Herbert Xu132a55f2006-10-03 14:34:00 -0700863 fl.oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 } else {
865 if (sk->sk_state != TCP_ESTABLISHED)
866 return -EDESTADDRREQ;
867
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000868 fl.fl_ip_dport = inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 daddr = &np->daddr;
Herbert Xu132a55f2006-10-03 14:34:00 -0700870 fl.fl6_flowlabel = np->flow_label;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700871 connected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Herbert Xu132a55f2006-10-03 14:34:00 -0700874 if (!fl.oif)
875 fl.oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Yang Hongyang9f690db2008-12-16 02:08:29 -0800877 if (!fl.oif)
878 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
879
Brian Haley51953d52009-10-05 08:24:16 +0000880 fl.mark = sk->sk_mark;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (msg->msg_controllen) {
883 opt = &opt_space;
884 memset(opt, 0, sizeof(struct ipv6_txoptions));
885 opt->tot_len = sizeof(*opt);
886
YOSHIFUJI Hideaki91e19082008-06-04 13:02:49 +0900887 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (err < 0) {
889 fl6_sock_release(flowlabel);
890 return err;
891 }
Herbert Xu132a55f2006-10-03 14:34:00 -0700892 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
893 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (flowlabel == NULL)
895 return -EINVAL;
896 }
897 if (!(opt->opt_nflen|opt->opt_flen))
898 opt = NULL;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700899 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901 if (opt == NULL)
902 opt = np->opt;
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +0900903 if (flowlabel)
904 opt = fl6_merge_options(&opt_space, flowlabel, opt);
905 opt = ipv6_fixup_options(&opt_space, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800907 fl.proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -0400908 if (!ipv6_addr_any(daddr))
909 ipv6_addr_copy(&fl.fl6_dst, daddr);
910 else
911 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
Herbert Xu132a55f2006-10-03 14:34:00 -0700912 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
913 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000914 fl.fl_ip_sport = inet->inet_sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* merge ip6_build_xmit from ip6_output */
917 if (opt && opt->srcrt) {
918 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
Herbert Xu132a55f2006-10-03 14:34:00 -0700919 ipv6_addr_copy(&final, &fl.fl6_dst);
920 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 final_p = &final;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700922 connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Herbert Xu132a55f2006-10-03 14:34:00 -0700925 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
926 fl.oif = np->mcast_oif;
Mitsuru KANDA987905d2005-09-18 00:30:08 -0700927 connected = 0;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Herbert Xu132a55f2006-10-03 14:34:00 -0700930 security_sk_classify_flow(sk, &fl);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700931
Herbert Xu132a55f2006-10-03 14:34:00 -0700932 err = ip6_sk_dst_lookup(sk, &dst, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (err)
934 goto out;
935 if (final_p)
Herbert Xu132a55f2006-10-03 14:34:00 -0700936 ipv6_addr_copy(&fl.fl6_dst, final_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800938 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
939 if (err < 0) {
David S. Miller14e50e52007-05-24 18:17:54 -0700940 if (err == -EREMOTE)
941 err = ip6_dst_blackhole(sk, &dst, &fl);
942 if (err < 0)
943 goto out;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (hlimit < 0) {
Herbert Xu132a55f2006-10-03 14:34:00 -0700947 if (ipv6_addr_is_multicast(&fl.fl6_dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 hlimit = np->mcast_hops;
949 else
950 hlimit = np->hop_limit;
951 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400952 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
Gerrit Renkere651f032009-08-09 08:12:48 +0000955 if (tclass < 0)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900956 tclass = np->tclass;
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (msg->msg_flags&MSG_CONFIRM)
959 goto do_confirm;
960back_from_confirm:
961
962 lock_sock(sk);
963 if (unlikely(up->pending)) {
964 /* The socket is already corked while preparing it. */
965 /* ... which is an evident application bug. --ANK */
966 release_sock(sk);
967
Patrick McHardy64ce2072005-08-09 20:50:53 -0700968 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 err = -EINVAL;
970 goto out;
971 }
972
973 up->pending = AF_INET6;
974
975do_append_data:
976 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800977 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
978 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
Herbert Xu132a55f2006-10-03 14:34:00 -0700979 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900980 (struct rt6_info*)dst,
981 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (err)
983 udp_v6_flush_pending_frames(sk);
984 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -0800985 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700986 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
987 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David S. Millera5e7c212005-10-03 14:21:58 -0700989 if (dst) {
990 if (connected) {
991 ip6_dst_store(sk, dst,
Herbert Xu132a55f2006-10-03 14:34:00 -0700992 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700993 &np->daddr : NULL,
994#ifdef CONFIG_IPV6_SUBTREES
Herbert Xu132a55f2006-10-03 14:34:00 -0700995 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700996 &np->saddr :
997#endif
998 NULL);
David S. Millera5e7c212005-10-03 14:21:58 -0700999 } else {
1000 dst_release(dst);
1001 }
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001002 dst = NULL;
David S. Millera5e7c212005-10-03 14:21:58 -07001003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (err > 0)
1006 err = np->recverr ? net_xmit_errno(err) : 0;
1007 release_sock(sk);
1008out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001009 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 fl6_sock_release(flowlabel);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -07001011 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return len;
David S. Millera18135e2006-08-15 00:00:09 -07001013 /*
1014 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1015 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1016 * we don't have a good statistic (IpOutDiscards but it can be too many
1017 * things). We could add another new stat but at least for now that
1018 * seems like overkill.
1019 */
1020 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov235b9f72008-07-05 21:19:20 -07001021 UDP6_INC_STATS_USER(sock_net(sk),
1022 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -07001023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return err;
1025
1026do_confirm:
1027 dst_confirm(dst);
1028 if (!(msg->msg_flags&MSG_PROBE) || len)
1029 goto back_from_confirm;
1030 err = 0;
1031 goto out;
1032}
1033
Brian Haley7d06b2e2008-06-14 17:04:49 -07001034void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 lock_sock(sk);
1037 udp_v6_flush_pending_frames(sk);
1038 release_sock(sk);
1039
1040 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/*
1044 * Socket option code for UDP
1045 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001046int udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001047 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001048{
David S. Millerdb8dac22008-03-06 16:22:02 -08001049 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001050 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1051 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001052 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001053}
1054
1055#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001056int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001057 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001058{
David S. Millerdb8dac22008-03-06 16:22:02 -08001059 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001060 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1061 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001062 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001063}
1064#endif
1065
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001066int udpv6_getsockopt(struct sock *sk, int level, int optname,
1067 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001068{
David S. Millerdb8dac22008-03-06 16:22:02 -08001069 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001070 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001071 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001072}
1073
1074#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001075int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1076 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001077{
David S. Millerdb8dac22008-03-06 16:22:02 -08001078 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001079 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001080 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001081}
1082#endif
1083
Sridhar Samudralaba735422009-07-09 08:10:04 +00001084static int udp6_ufo_send_check(struct sk_buff *skb)
1085{
1086 struct ipv6hdr *ipv6h;
1087 struct udphdr *uh;
1088
1089 if (!pskb_may_pull(skb, sizeof(*uh)))
1090 return -EINVAL;
1091
1092 ipv6h = ipv6_hdr(skb);
1093 uh = udp_hdr(skb);
1094
1095 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1096 IPPROTO_UDP, 0);
1097 skb->csum_start = skb_transport_header(skb) - skb->head;
1098 skb->csum_offset = offsetof(struct udphdr, check);
1099 skb->ip_summed = CHECKSUM_PARTIAL;
1100 return 0;
1101}
1102
1103static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features)
1104{
1105 struct sk_buff *segs = ERR_PTR(-EINVAL);
1106 unsigned int mss;
1107 unsigned int unfrag_ip6hlen, unfrag_len;
1108 struct frag_hdr *fptr;
1109 u8 *mac_start, *prevhdr;
1110 u8 nexthdr;
1111 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1112 int offset;
1113 __wsum csum;
1114
1115 mss = skb_shinfo(skb)->gso_size;
1116 if (unlikely(skb->len <= mss))
1117 goto out;
1118
1119 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1120 /* Packet is from an untrusted source, reset gso_segs. */
1121 int type = skb_shinfo(skb)->gso_type;
1122
1123 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1124 !(type & (SKB_GSO_UDP))))
1125 goto out;
1126
1127 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1128
1129 segs = NULL;
1130 goto out;
1131 }
1132
1133 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1134 * do checksum of UDP packets sent as multiple IP fragments.
1135 */
1136 offset = skb->csum_start - skb_headroom(skb);
1137 csum = skb_checksum(skb, offset, skb->len- offset, 0);
1138 offset += skb->csum_offset;
1139 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1140 skb->ip_summed = CHECKSUM_NONE;
1141
1142 /* Check if there is enough headroom to insert fragment header. */
1143 if ((skb_headroom(skb) < frag_hdr_sz) &&
1144 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1145 goto out;
1146
1147 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1148 * bytes to insert fragment header.
1149 */
1150 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1151 nexthdr = *prevhdr;
1152 *prevhdr = NEXTHDR_FRAGMENT;
1153 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1154 unfrag_ip6hlen;
1155 mac_start = skb_mac_header(skb);
1156 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1157
1158 skb->mac_header -= frag_hdr_sz;
1159 skb->network_header -= frag_hdr_sz;
1160
1161 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1162 fptr->nexthdr = nexthdr;
1163 fptr->reserved = 0;
1164 ipv6_select_ident(fptr);
1165
1166 /* Fragment the skb. ipv6 header and the remaining fields of the
1167 * fragment header are updated in ipv6_gso_segment()
1168 */
1169 segs = skb_segment(skb, features);
1170
1171out:
1172 return segs;
1173}
1174
Alexey Dobriyan41135cc2009-09-14 12:22:28 +00001175static const struct inet6_protocol udpv6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 .handler = udpv6_rcv,
1177 .err_handler = udpv6_err,
Sridhar Samudralaba735422009-07-09 08:10:04 +00001178 .gso_send_check = udp6_ufo_send_check,
1179 .gso_segment = udp6_ufo_fragment,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1181};
1182
1183/* ------------------------------------------------------------------------ */
1184#ifdef CONFIG_PROC_FS
1185
1186static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1187{
1188 struct inet_sock *inet = inet_sk(sp);
1189 struct ipv6_pinfo *np = inet6_sk(sp);
1190 struct in6_addr *dest, *src;
1191 __u16 destp, srcp;
1192
1193 dest = &np->daddr;
1194 src = &np->rcv_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001195 destp = ntohs(inet->inet_dport);
1196 srcp = ntohs(inet->inet_sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 seq_printf(seq,
Eric Dumazetf86dcc52009-10-07 00:37:59 +00001198 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001199 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bucket,
1201 src->s6_addr32[0], src->s6_addr32[1],
1202 src->s6_addr32[2], src->s6_addr32[3], srcp,
1203 dest->s6_addr32[0], dest->s6_addr32[1],
1204 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001205 sp->sk_state,
Eric Dumazet31e6d362009-06-17 19:05:41 -07001206 sk_wmem_alloc_get(sp),
1207 sk_rmem_alloc_get(sp),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 0, 0L, 0,
1209 sock_i_uid(sp), 0,
1210 sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001211 atomic_read(&sp->sk_refcnt), sp,
1212 atomic_read(&sp->sk_drops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001215int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 if (v == SEQ_START_TOKEN)
1218 seq_printf(seq,
1219 " sl "
1220 "local_address "
1221 "remote_address "
1222 "st tx_queue rx_queue tr tm->when retrnsmt"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001223 " uid timeout inode ref pointer drops\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 else
1225 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1226 return 0;
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .name = "udp6",
1231 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001232 .udp_table = &udp_table,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001233 .seq_fops = {
1234 .owner = THIS_MODULE,
1235 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001236 .seq_ops = {
1237 .show = udp6_seq_show,
1238 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001241int udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001243 return udp_proc_register(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001246void udp6_proc_exit(struct net *net) {
1247 udp_proc_unregister(net, &udp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249#endif /* CONFIG_PROC_FS */
1250
1251/* ------------------------------------------------------------------------ */
1252
1253struct proto udpv6_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001254 .name = "UDPv6",
1255 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001256 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001257 .connect = ip6_datagram_connect,
1258 .disconnect = udp_disconnect,
1259 .ioctl = udp_ioctl,
1260 .destroy = udpv6_destroy_sock,
1261 .setsockopt = udpv6_setsockopt,
1262 .getsockopt = udpv6_getsockopt,
1263 .sendmsg = udpv6_sendmsg,
1264 .recvmsg = udpv6_recvmsg,
1265 .backlog_rcv = udpv6_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001266 .hash = udp_lib_hash,
1267 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001268 .get_port = udp_v6_get_port,
Hideo Aoki95766ff2007-12-31 00:29:24 -08001269 .memory_allocated = &udp_memory_allocated,
1270 .sysctl_mem = sysctl_udp_mem,
1271 .sysctl_wmem = &sysctl_udp_wmem_min,
1272 .sysctl_rmem = &sysctl_udp_rmem_min,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001273 .obj_size = sizeof(struct udp6_sock),
Eric Dumazet271b72c2008-10-29 02:11:14 -07001274 .slab_flags = SLAB_DESTROY_BY_RCU,
Eric Dumazet645ca702008-10-29 01:41:45 -07001275 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001276#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001277 .compat_setsockopt = compat_udpv6_setsockopt,
1278 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280};
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282static struct inet_protosw udpv6_protosw = {
1283 .type = SOCK_DGRAM,
1284 .protocol = IPPROTO_UDP,
1285 .prot = &udpv6_prot,
1286 .ops = &inet6_dgram_ops,
1287 .capability =-1,
1288 .no_check = UDP_CSUM_DEFAULT,
1289 .flags = INET_PROTOSW_PERMANENT,
1290};
1291
1292
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001293int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001295 int ret;
1296
1297 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1298 if (ret)
1299 goto out;
1300
1301 ret = inet6_register_protosw(&udpv6_protosw);
1302 if (ret)
1303 goto out_udpv6_protocol;
1304out:
1305 return ret;
1306
1307out_udpv6_protocol:
1308 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1309 goto out;
1310}
1311
Daniel Lezcano09f77092007-12-13 05:34:58 -08001312void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001313{
1314 inet6_unregister_protosw(&udpv6_protosw);
1315 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}