]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv6/udp.c
Revert "af_packet: add interframe drop cmsg (v6)"
[mirror_ubuntu-zesty-kernel.git] / net / ipv6 / udp.c
CommitLineData
1da177e4
LT
1/*
2 * UDP over IPv6
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
7 *
8 * Based on linux/ipv4/udp.c
9 *
1da177e4
LT
10 * 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
1da177e4
LT
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
1da177e4
LT
28#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>
1781f7f5 35#include <linux/module.h>
3305b80c 36#include <linux/skbuff.h>
1da177e4
LT
37#include <asm/uaccess.h>
38
1da177e4
LT
39#include <net/ndisc.h>
40#include <net/protocol.h>
41#include <net/transp_v6.h>
42#include <net/ip6_route.h>
1da177e4 43#include <net/raw.h>
c752f073 44#include <net/tcp_states.h>
1da177e4
LT
45#include <net/ip6_checksum.h>
46#include <net/xfrm.h>
47
48#include <linux/proc_fs.h>
49#include <linux/seq_file.h>
ba4e58ec 50#include "udp_impl.h"
1da177e4 51
b2f5e7cd
VY
52int 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);
499923c7
VY
56 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
57 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
b2f5e7cd
VY
58 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)
499923c7
VY
65 return (!sk2_ipv6only &&
66 (!sk_rcv_saddr || !sk2_rcv_saddr ||
67 sk_rcv_saddr == sk2_rcv_saddr));
b2f5e7cd
VY
68
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
6ba5a3c5 84int udp_v6_get_port(struct sock *sk, unsigned short snum)
1da177e4 85{
6ba5a3c5 86 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
1da177e4
LT
87}
88
645ca708
ED
89static 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;
103 if (inet->dport) {
104 if (inet->dport != sport)
105 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
fa4d3c62
PE
127static struct sock *__udp6_lib_lookup(struct net *net,
128 struct in6_addr *saddr, __be16 sport,
ba4e58ec 129 struct in6_addr *daddr, __be16 dport,
645ca708 130 int dif, struct udp_table *udptable)
1da177e4 131{
271b72c7 132 struct sock *sk, *result;
88ab1932 133 struct hlist_nulls_node *node;
1da177e4 134 unsigned short hnum = ntohs(dport);
f86dcc5a 135 unsigned int hash = udp_hashfn(net, hnum, udptable->mask);
645ca708 136 struct udp_hslot *hslot = &udptable->hash[hash];
271b72c7 137 int score, badness;
645ca708 138
271b72c7
ED
139 rcu_read_lock();
140begin:
141 result = NULL;
142 badness = -1;
88ab1932 143 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
645ca708
ED
144 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
145 if (score > badness) {
146 result = sk;
147 badness = score;
1da177e4
LT
148 }
149 }
88ab1932
ED
150 /*
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
271b72c7
ED
158 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();
1da177e4
LT
168 return result;
169}
170
607c4aaf
KK
171static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
172 __be16 sport, __be16 dport,
645ca708 173 struct udp_table *udptable)
607c4aaf 174{
23542618 175 struct sock *sk;
607c4aaf
KK
176 struct ipv6hdr *iph = ipv6_hdr(skb);
177
23542618
KK
178 if (unlikely(sk = skb_steal_sock(skb)))
179 return sk;
adf30907
ED
180 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
181 &iph->daddr, dport, inet6_iif(skb),
182 udptable);
607c4aaf
KK
183}
184
1da177e4
LT
185/*
186 * This should be easy, if there is something there we
187 * return it, otherwise we block.
188 */
189
ba4e58ec 190int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
1da177e4
LT
191 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);
1ab1457c 196 struct sk_buff *skb;
759e5d00 197 unsigned int ulen, copied;
a59322be 198 int peeked;
759e5d00
HX
199 int err;
200 int is_udplite = IS_UDPLITE(sk);
f26ba175 201 int is_udp4;
1da177e4 202
1ab1457c
YH
203 if (addr_len)
204 *addr_len=sizeof(struct sockaddr_in6);
205
1da177e4
LT
206 if (flags & MSG_ERRQUEUE)
207 return ipv6_recv_error(sk, msg, len);
208
209try_again:
a59322be
HX
210 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
211 &peeked, &err);
1da177e4
LT
212 if (!skb)
213 goto out;
214
759e5d00
HX
215 ulen = skb->len - sizeof(struct udphdr);
216 copied = len;
217 if (copied > ulen)
218 copied = ulen;
219 else if (copied < ulen)
1ab1457c 220 msg->msg_flags |= MSG_TRUNC;
1da177e4 221
f26ba175
WY
222 is_udp4 = (skb->protocol == htons(ETH_P_IP));
223
ba4e58ec 224 /*
759e5d00
HX
225 * 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.
ba4e58ec 228 */
ba4e58ec 229
759e5d00
HX
230 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
231 if (udp_lib_checksum_complete(skb))
1da177e4 232 goto csum_copy_err;
ba4e58ec
GR
233 }
234
60476372 235 if (skb_csum_unnecessary(skb))
ba4e58ec
GR
236 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
237 msg->msg_iov, copied );
238 else {
1da177e4
LT
239 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
f26ba175
WY
246 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 }
cb75994e 254
1da177e4
LT
255 sock_recv_timestamp(msg, sk, skb);
256
257 /* Copy the address. */
258 if (msg->msg_name) {
259 struct sockaddr_in6 *sin6;
1ab1457c 260
1da177e4
LT
261 sin6 = (struct sockaddr_in6 *) msg->msg_name;
262 sin6->sin6_family = AF_INET6;
4bedb452 263 sin6->sin6_port = udp_hdr(skb)->source;
1da177e4
LT
264 sin6->sin6_flowinfo = 0;
265 sin6->sin6_scope_id = 0;
266
f26ba175 267 if (is_udp4)
b301e82c
BH
268 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
269 &sin6->sin6_addr);
1da177e4 270 else {
0660e03f
ACM
271 ipv6_addr_copy(&sin6->sin6_addr,
272 &ipv6_hdr(skb)->saddr);
1da177e4
LT
273 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
274 sin6->sin6_scope_id = IP6CB(skb)->iif;
275 }
276
277 }
f26ba175 278 if (is_udp4) {
1da177e4
LT
279 if (inet->cmsg_flags)
280 ip_cmsg_recv(msg, skb);
281 } else {
282 if (np->rxopt.all)
283 datagram_recv_ctl(sk, msg, skb);
1ab1457c 284 }
1da177e4
LT
285
286 err = copied;
287 if (flags & MSG_TRUNC)
759e5d00 288 err = ulen;
1da177e4
LT
289
290out_free:
95766fff 291 lock_sock(sk);
1da177e4 292 skb_free_datagram(sk, skb);
95766fff 293 release_sock(sk);
1da177e4
LT
294out:
295 return err;
296
297csum_copy_err:
95766fff 298 lock_sock(sk);
0856f939
WY
299 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 }
95766fff 307 release_sock(sk);
1da177e4 308
7a0ff716 309 if (flags & MSG_DONTWAIT)
1da177e4 310 return -EAGAIN;
1da177e4
LT
311 goto try_again;
312}
313
ba4e58ec 314void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6ba 315 u8 type, u8 code, int offset, __be32 info,
645ca708 316 struct udp_table *udptable)
1da177e4
LT
317{
318 struct ipv6_pinfo *np;
319 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
1da177e4
LT
320 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
c346dca1 326 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
ba4e58ec 327 saddr, uh->source, inet6_iif(skb), udptable);
1da177e4
LT
328 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
ba4e58ec 348static __inline__ void udpv6_err(struct sk_buff *skb,
d5fdd6ba
BH
349 struct inet6_skb_parm *opt, u8 type,
350 u8 code, int offset, __be32 info )
ba4e58ec 351{
645ca708 352 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
ba4e58ec
GR
353}
354
355int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1da177e4 356{
ba4e58ec 357 struct udp_sock *up = udp_sk(sk);
a18135eb 358 int rc;
b2bf1e26 359 int is_udplite = IS_UDPLITE(sk);
a18135eb 360
ba4e58ec
GR
361 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
362 goto drop;
1da177e4 363
ba4e58ec
GR
364 /*
365 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
366 */
b2bf1e26 367 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
ba4e58ec
GR
368
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 }
1da177e4
LT
381 }
382
1ab6eb62
HX
383 if (sk->sk_filter) {
384 if (udp_lib_checksum_complete(skb))
385 goto drop;
386 }
ba4e58ec 387
a18135eb
DM
388 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
389 /* Note that an ENOMEM error is charged twice */
cb61cb9b 390 if (rc == -ENOMEM) {
ef28d1a2
PE
391 UDP6_INC_STATS_BH(sock_net(sk),
392 UDP_MIB_RCVBUFERRORS, is_udplite);
cb61cb9b
ED
393 atomic_inc(&sk->sk_drops);
394 }
ba4e58ec 395 goto drop;
1da177e4 396 }
cb75994e 397
1da177e4 398 return 0;
ba4e58ec 399drop:
ef28d1a2 400 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
ba4e58ec
GR
401 kfree_skb(skb);
402 return -1;
1da177e4
LT
403}
404
920a4611 405static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
e69a4adc
AV
406 __be16 loc_port, struct in6_addr *loc_addr,
407 __be16 rmt_port, struct in6_addr *rmt_addr,
1da177e4
LT
408 int dif)
409{
88ab1932 410 struct hlist_nulls_node *node;
1da177e4
LT
411 struct sock *s = sk;
412 unsigned short num = ntohs(loc_port);
413
88ab1932 414 sk_nulls_for_each_from(s, node) {
1da177e4
LT
415 struct inet_sock *inet = inet_sk(s);
416
920a4611 417 if (!net_eq(sock_net(s), net))
b8ad0cbc
DL
418 continue;
419
95f30b33 420 if (s->sk_hash == num && s->sk_family == PF_INET6) {
1da177e4
LT
421 struct ipv6_pinfo *np = inet6_sk(s);
422 if (inet->dport) {
423 if (inet->dport != rmt_port)
424 continue;
425 }
426 if (!ipv6_addr_any(&np->daddr) &&
427 !ipv6_addr_equal(&np->daddr, rmt_addr))
428 continue;
429
430 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
431 continue;
432
433 if (!ipv6_addr_any(&np->rcv_saddr)) {
40796c5e
DS
434 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
435 continue;
1da177e4 436 }
add459aa 437 if (!inet6_mc_check(s, loc_addr, rmt_addr))
1da177e4
LT
438 continue;
439 return s;
440 }
441 }
442 return NULL;
443}
444
445/*
446 * Note: called only from the BH handler context,
447 * so we don't need to lock the hashes.
448 */
e3163493
PE
449static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
450 struct in6_addr *saddr, struct in6_addr *daddr,
645ca708 451 struct udp_table *udptable)
1da177e4
LT
452{
453 struct sock *sk, *sk2;
4bedb452 454 const struct udphdr *uh = udp_hdr(skb);
f86dcc5a 455 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
1da177e4
LT
456 int dif;
457
645ca708 458 spin_lock(&hslot->lock);
88ab1932 459 sk = sk_nulls_head(&hslot->head);
f2776ff0 460 dif = inet6_iif(skb);
920a4611 461 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
1da177e4
LT
462 if (!sk) {
463 kfree_skb(skb);
464 goto out;
465 }
466
467 sk2 = sk;
88ab1932 468 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
1da177e4
LT
469 uh->source, saddr, dif))) {
470 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
95766fff 471 if (buff) {
d97106ea 472 bh_lock_sock(sk2);
95766fff
HA
473 if (!sock_owned_by_user(sk2))
474 udpv6_queue_rcv_skb(sk2, buff);
475 else
476 sk_add_backlog(sk2, buff);
477 bh_unlock_sock(sk2);
478 }
1da177e4 479 }
d97106ea 480 bh_lock_sock(sk);
95766fff
HA
481 if (!sock_owned_by_user(sk))
482 udpv6_queue_rcv_skb(sk, skb);
483 else
484 sk_add_backlog(sk, skb);
485 bh_unlock_sock(sk);
1da177e4 486out:
645ca708 487 spin_unlock(&hslot->lock);
ba4e58ec 488 return 0;
1da177e4
LT
489}
490
759e5d00
HX
491static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
492 int proto)
ba4e58ec 493{
759e5d00
HX
494 int err;
495
496 UDP_SKB_CB(skb)->partial_cov = 0;
497 UDP_SKB_CB(skb)->cscov = skb->len;
498
db8dac20 499 if (proto == IPPROTO_UDPLITE) {
759e5d00
HX
500 err = udplite_checksum_init(skb, uh);
501 if (err)
502 return err;
503 }
504
ba4e58ec
GR
505 if (uh->check == 0) {
506 /* RFC 2460 section 8.1 says that we SHOULD log
507 this error. Well, it is reasonable.
508 */
509 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
510 return 1;
511 }
512 if (skb->ip_summed == CHECKSUM_COMPLETE &&
0660e03f 513 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
759e5d00 514 skb->len, proto, skb->csum))
ba4e58ec
GR
515 skb->ip_summed = CHECKSUM_UNNECESSARY;
516
60476372 517 if (!skb_csum_unnecessary(skb))
0660e03f
ACM
518 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
519 &ipv6_hdr(skb)->daddr,
759e5d00 520 skb->len, proto, 0));
ba4e58ec 521
759e5d00 522 return 0;
ba4e58ec
GR
523}
524
645ca708 525int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
759e5d00 526 int proto)
1da177e4 527{
1da177e4 528 struct sock *sk;
1ab1457c 529 struct udphdr *uh;
1da177e4
LT
530 struct net_device *dev = skb->dev;
531 struct in6_addr *saddr, *daddr;
532 u32 ulen = 0;
ef28d1a2 533 struct net *net = dev_net(skb->dev);
1da177e4
LT
534
535 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
536 goto short_packet;
537
0660e03f
ACM
538 saddr = &ipv6_hdr(skb)->saddr;
539 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 540 uh = udp_hdr(skb);
1da177e4
LT
541
542 ulen = ntohs(uh->len);
ba4e58ec
GR
543 if (ulen > skb->len)
544 goto short_packet;
1da177e4 545
759e5d00
HX
546 if (proto == IPPROTO_UDP) {
547 /* UDP validates ulen. */
1da177e4 548
ba4e58ec
GR
549 /* Check for jumbo payload */
550 if (ulen == 0)
551 ulen = skb->len;
1da177e4 552
ba4e58ec
GR
553 if (ulen < sizeof(*uh))
554 goto short_packet;
1da177e4 555
ba4e58ec
GR
556 if (ulen < skb->len) {
557 if (pskb_trim_rcsum(skb, ulen))
558 goto short_packet;
0660e03f
ACM
559 saddr = &ipv6_hdr(skb)->saddr;
560 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 561 uh = udp_hdr(skb);
ba4e58ec 562 }
ba4e58ec 563 }
1da177e4 564
759e5d00
HX
565 if (udp6_csum_init(skb, uh, proto))
566 goto discard;
567
1ab1457c
YH
568 /*
569 * Multicast receive code
1da177e4 570 */
ba4e58ec 571 if (ipv6_addr_is_multicast(daddr))
e3163493
PE
572 return __udp6_lib_mcast_deliver(net, skb,
573 saddr, daddr, udptable);
1da177e4
LT
574
575 /* Unicast */
576
1ab1457c 577 /*
1da177e4
LT
578 * check socket cache ... must talk to Alan about his plans
579 * for sock caches... i'll skip this for now.
580 */
607c4aaf 581 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1da177e4
LT
582
583 if (sk == NULL) {
584 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
585 goto discard;
586
ba4e58ec 587 if (udp_lib_checksum_complete(skb))
1da177e4 588 goto discard;
ef28d1a2
PE
589 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
590 proto == IPPROTO_UDPLITE);
1da177e4
LT
591
592 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
593
594 kfree_skb(skb);
add459aa 595 return 0;
1da177e4 596 }
1ab1457c 597
1da177e4 598 /* deliver */
1ab1457c 599
d97106ea 600 bh_lock_sock(sk);
95766fff
HA
601 if (!sock_owned_by_user(sk))
602 udpv6_queue_rcv_skb(sk, skb);
603 else
604 sk_add_backlog(sk, skb);
605 bh_unlock_sock(sk);
1da177e4 606 sock_put(sk);
add459aa 607 return 0;
1da177e4 608
1ab1457c 609short_packet:
ba4e58ec 610 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
db8dac20 611 proto == IPPROTO_UDPLITE ? "-Lite" : "",
759e5d00 612 ulen, skb->len);
1da177e4
LT
613
614discard:
ef28d1a2 615 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1da177e4 616 kfree_skb(skb);
add459aa 617 return 0;
1da177e4 618}
ba4e58ec 619
e5bbef20 620static __inline__ int udpv6_rcv(struct sk_buff *skb)
ba4e58ec 621{
645ca708 622 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
ba4e58ec
GR
623}
624
1da177e4
LT
625/*
626 * Throw away all pending data and cancel the corking. Socket is locked.
627 */
628static void udp_v6_flush_pending_frames(struct sock *sk)
629{
630 struct udp_sock *up = udp_sk(sk);
631
36d926b9
DL
632 if (up->pending == AF_INET)
633 udp_flush_pending_frames(sk);
634 else if (up->pending) {
1da177e4
LT
635 up->len = 0;
636 up->pending = 0;
637 ip6_flush_pending_frames(sk);
1ab1457c 638 }
1da177e4
LT
639}
640
493c6be3
SS
641/**
642 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
643 * @sk: socket we are sending on
644 * @skb: sk_buff containing the filled-in UDP header
645 * (checksum field must be zeroed out)
646 */
647static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
648 const struct in6_addr *saddr,
649 const struct in6_addr *daddr, int len)
650{
651 unsigned int offset;
652 struct udphdr *uh = udp_hdr(skb);
653 __wsum csum = 0;
654
655 if (skb_queue_len(&sk->sk_write_queue) == 1) {
656 /* Only one fragment on the socket. */
657 skb->csum_start = skb_transport_header(skb) - skb->head;
658 skb->csum_offset = offsetof(struct udphdr, check);
659 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
660 } else {
661 /*
662 * HW-checksum won't work as there are two or more
663 * fragments on the socket so that all csums of sk_buffs
664 * should be together
665 */
666 offset = skb_transport_offset(skb);
667 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
668
669 skb->ip_summed = CHECKSUM_NONE;
670
671 skb_queue_walk(&sk->sk_write_queue, skb) {
672 csum = csum_add(csum, skb->csum);
673 }
674
675 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
676 csum);
677 if (uh->check == 0)
678 uh->check = CSUM_MANGLED_0;
679 }
680}
681
1da177e4
LT
682/*
683 * Sending
684 */
685
4c0a6cb0 686static int udp_v6_push_pending_frames(struct sock *sk)
1da177e4
LT
687{
688 struct sk_buff *skb;
689 struct udphdr *uh;
4c0a6cb0 690 struct udp_sock *up = udp_sk(sk);
1da177e4
LT
691 struct inet_sock *inet = inet_sk(sk);
692 struct flowi *fl = &inet->cork.fl;
693 int err = 0;
b2bf1e26 694 int is_udplite = IS_UDPLITE(sk);
868c86bc 695 __wsum csum = 0;
1da177e4
LT
696
697 /* Grab the skbuff where UDP header space exists. */
698 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
699 goto out;
700
701 /*
702 * Create a UDP header
703 */
4bedb452 704 uh = udp_hdr(skb);
1da177e4
LT
705 uh->source = fl->fl_ip_sport;
706 uh->dest = fl->fl_ip_dport;
707 uh->len = htons(up->len);
708 uh->check = 0;
709
b2bf1e26 710 if (is_udplite)
ba4e58ec 711 csum = udplite_csum_outgoing(sk, skb);
493c6be3
SS
712 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
713 udp6_hwcsum_outgoing(sk, skb, &fl->fl6_src, &fl->fl6_dst,
714 up->len);
715 goto send;
716 } else
ba4e58ec 717 csum = udp_csum_outgoing(sk, skb);
1da177e4 718
ba4e58ec
GR
719 /* add protocol-dependent pseudo-header */
720 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
721 up->len, fl->proto, csum );
1da177e4 722 if (uh->check == 0)
f6ab0288 723 uh->check = CSUM_MANGLED_0;
1da177e4 724
493c6be3 725send:
1da177e4 726 err = ip6_push_pending_frames(sk);
6ce9e7b5
ED
727 if (err) {
728 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
729 UDP6_INC_STATS_USER(sock_net(sk),
730 UDP_MIB_SNDBUFERRORS, is_udplite);
731 err = 0;
732 }
733 } else
734 UDP6_INC_STATS_USER(sock_net(sk),
735 UDP_MIB_OUTDATAGRAMS, is_udplite);
1da177e4
LT
736out:
737 up->len = 0;
738 up->pending = 0;
739 return err;
740}
741
ba4e58ec 742int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1da177e4
LT
743 struct msghdr *msg, size_t len)
744{
745 struct ipv6_txoptions opt_space;
746 struct udp_sock *up = udp_sk(sk);
747 struct inet_sock *inet = inet_sk(sk);
748 struct ipv6_pinfo *np = inet6_sk(sk);
749 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
750 struct in6_addr *daddr, *final_p = NULL, final;
751 struct ipv6_txoptions *opt = NULL;
752 struct ip6_flowlabel *flowlabel = NULL;
132a55f3 753 struct flowi fl;
1da177e4
LT
754 struct dst_entry *dst;
755 int addr_len = msg->msg_namelen;
756 int ulen = len;
757 int hlimit = -1;
41a1f8ea 758 int tclass = -1;
1da177e4
LT
759 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
760 int err;
987905de 761 int connected = 0;
b2bf1e26 762 int is_udplite = IS_UDPLITE(sk);
ba4e58ec 763 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1da177e4
LT
764
765 /* destination address check */
766 if (sin6) {
767 if (addr_len < offsetof(struct sockaddr, sa_data))
768 return -EINVAL;
769
770 switch (sin6->sin6_family) {
771 case AF_INET6:
772 if (addr_len < SIN6_LEN_RFC2133)
773 return -EINVAL;
774 daddr = &sin6->sin6_addr;
775 break;
776 case AF_INET:
777 goto do_udp_sendmsg;
778 case AF_UNSPEC:
779 msg->msg_name = sin6 = NULL;
780 msg->msg_namelen = addr_len = 0;
781 daddr = NULL;
782 break;
783 default:
784 return -EINVAL;
785 }
786 } else if (!up->pending) {
787 if (sk->sk_state != TCP_ESTABLISHED)
788 return -EDESTADDRREQ;
789 daddr = &np->daddr;
1ab1457c 790 } else
1da177e4
LT
791 daddr = NULL;
792
793 if (daddr) {
e773e4fa 794 if (ipv6_addr_v4mapped(daddr)) {
1da177e4
LT
795 struct sockaddr_in sin;
796 sin.sin_family = AF_INET;
797 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
798 sin.sin_addr.s_addr = daddr->s6_addr32[3];
799 msg->msg_name = &sin;
800 msg->msg_namelen = sizeof(sin);
801do_udp_sendmsg:
802 if (__ipv6_only_sock(sk))
803 return -ENETUNREACH;
804 return udp_sendmsg(iocb, sk, msg, len);
805 }
806 }
807
808 if (up->pending == AF_INET)
809 return udp_sendmsg(iocb, sk, msg, len);
810
811 /* Rough check on arithmetic overflow,
b59e139b 812 better check is made in ip6_append_data().
1da177e4
LT
813 */
814 if (len > INT_MAX - sizeof(struct udphdr))
815 return -EMSGSIZE;
1ab1457c 816
1da177e4
LT
817 if (up->pending) {
818 /*
819 * There are pending frames.
820 * The socket lock must be held while it's corked.
821 */
822 lock_sock(sk);
823 if (likely(up->pending)) {
824 if (unlikely(up->pending != AF_INET6)) {
825 release_sock(sk);
826 return -EAFNOSUPPORT;
827 }
828 dst = NULL;
829 goto do_append_data;
830 }
831 release_sock(sk);
832 }
833 ulen += sizeof(struct udphdr);
834
132a55f3 835 memset(&fl, 0, sizeof(fl));
1da177e4
LT
836
837 if (sin6) {
838 if (sin6->sin6_port == 0)
839 return -EINVAL;
840
132a55f3 841 fl.fl_ip_dport = sin6->sin6_port;
1da177e4
LT
842 daddr = &sin6->sin6_addr;
843
844 if (np->sndflow) {
132a55f3
HX
845 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
846 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
847 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
1da177e4
LT
848 if (flowlabel == NULL)
849 return -EINVAL;
850 daddr = &flowlabel->dst;
851 }
852 }
853
854 /*
855 * Otherwise it will be difficult to maintain
856 * sk->sk_dst_cache.
857 */
858 if (sk->sk_state == TCP_ESTABLISHED &&
859 ipv6_addr_equal(daddr, &np->daddr))
860 daddr = &np->daddr;
861
862 if (addr_len >= sizeof(struct sockaddr_in6) &&
863 sin6->sin6_scope_id &&
864 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
132a55f3 865 fl.oif = sin6->sin6_scope_id;
1da177e4
LT
866 } else {
867 if (sk->sk_state != TCP_ESTABLISHED)
868 return -EDESTADDRREQ;
869
132a55f3 870 fl.fl_ip_dport = inet->dport;
1da177e4 871 daddr = &np->daddr;
132a55f3 872 fl.fl6_flowlabel = np->flow_label;
987905de 873 connected = 1;
1da177e4
LT
874 }
875
132a55f3
HX
876 if (!fl.oif)
877 fl.oif = sk->sk_bound_dev_if;
1da177e4 878
9f690db7
YH
879 if (!fl.oif)
880 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
881
51953d5b
BH
882 fl.mark = sk->sk_mark;
883
1da177e4
LT
884 if (msg->msg_controllen) {
885 opt = &opt_space;
886 memset(opt, 0, sizeof(struct ipv6_txoptions));
887 opt->tot_len = sizeof(*opt);
888
91e1908f 889 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
1da177e4
LT
890 if (err < 0) {
891 fl6_sock_release(flowlabel);
892 return err;
893 }
132a55f3
HX
894 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
895 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
1da177e4
LT
896 if (flowlabel == NULL)
897 return -EINVAL;
898 }
899 if (!(opt->opt_nflen|opt->opt_flen))
900 opt = NULL;
987905de 901 connected = 0;
1da177e4
LT
902 }
903 if (opt == NULL)
904 opt = np->opt;
df9890c3
YH
905 if (flowlabel)
906 opt = fl6_merge_options(&opt_space, flowlabel, opt);
907 opt = ipv6_fixup_options(&opt_space, opt);
1da177e4 908
ba4e58ec 909 fl.proto = sk->sk_protocol;
876c7f41
BH
910 if (!ipv6_addr_any(daddr))
911 ipv6_addr_copy(&fl.fl6_dst, daddr);
912 else
913 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
132a55f3
HX
914 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
915 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
916 fl.fl_ip_sport = inet->sport;
1ab1457c 917
1da177e4
LT
918 /* merge ip6_build_xmit from ip6_output */
919 if (opt && opt->srcrt) {
920 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
132a55f3
HX
921 ipv6_addr_copy(&final, &fl.fl6_dst);
922 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1da177e4 923 final_p = &final;
987905de 924 connected = 0;
1da177e4
LT
925 }
926
132a55f3
HX
927 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
928 fl.oif = np->mcast_oif;
987905de
MK
929 connected = 0;
930 }
1da177e4 931
132a55f3 932 security_sk_classify_flow(sk, &fl);
beb8d13b 933
132a55f3 934 err = ip6_sk_dst_lookup(sk, &dst, &fl);
1da177e4
LT
935 if (err)
936 goto out;
937 if (final_p)
132a55f3 938 ipv6_addr_copy(&fl.fl6_dst, final_p);
1da177e4 939
52479b62
AD
940 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
941 if (err < 0) {
14e50e57
DM
942 if (err == -EREMOTE)
943 err = ip6_dst_blackhole(sk, &dst, &fl);
944 if (err < 0)
945 goto out;
946 }
1da177e4
LT
947
948 if (hlimit < 0) {
132a55f3 949 if (ipv6_addr_is_multicast(&fl.fl6_dst))
1da177e4
LT
950 hlimit = np->mcast_hops;
951 else
952 hlimit = np->hop_limit;
953 if (hlimit < 0)
6b75d090 954 hlimit = ip6_dst_hoplimit(dst);
1da177e4
LT
955 }
956
e651f03a 957 if (tclass < 0)
41a1f8ea 958 tclass = np->tclass;
41a1f8ea 959
1da177e4
LT
960 if (msg->msg_flags&MSG_CONFIRM)
961 goto do_confirm;
962back_from_confirm:
963
964 lock_sock(sk);
965 if (unlikely(up->pending)) {
966 /* The socket is already corked while preparing it. */
967 /* ... which is an evident application bug. --ANK */
968 release_sock(sk);
969
64ce2073 970 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1da177e4
LT
971 err = -EINVAL;
972 goto out;
973 }
974
975 up->pending = AF_INET6;
976
977do_append_data:
978 up->len += ulen;
ba4e58ec
GR
979 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
980 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
132a55f3 981 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
41a1f8ea
YH
982 (struct rt6_info*)dst,
983 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1da177e4
LT
984 if (err)
985 udp_v6_flush_pending_frames(sk);
986 else if (!corkreq)
4c0a6cb0 987 err = udp_v6_push_pending_frames(sk);
1e0c14f4
HX
988 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
989 up->pending = 0;
1da177e4 990
a5e7c210
DM
991 if (dst) {
992 if (connected) {
993 ip6_dst_store(sk, dst,
132a55f3 994 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
8e1ef0a9
YH
995 &np->daddr : NULL,
996#ifdef CONFIG_IPV6_SUBTREES
132a55f3 997 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
8e1ef0a9
YH
998 &np->saddr :
999#endif
1000 NULL);
a5e7c210
DM
1001 } else {
1002 dst_release(dst);
1003 }
a3c96089 1004 dst = NULL;
a5e7c210
DM
1005 }
1006
1da177e4
LT
1007 if (err > 0)
1008 err = np->recverr ? net_xmit_errno(err) : 0;
1009 release_sock(sk);
1010out:
a3c96089 1011 dst_release(dst);
1da177e4 1012 fl6_sock_release(flowlabel);
cd562c98 1013 if (!err)
1da177e4 1014 return len;
a18135eb
DM
1015 /*
1016 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1017 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1018 * we don't have a good statistic (IpOutDiscards but it can be too many
1019 * things). We could add another new stat but at least for now that
1020 * seems like overkill.
1021 */
1022 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
235b9f7a
PE
1023 UDP6_INC_STATS_USER(sock_net(sk),
1024 UDP_MIB_SNDBUFERRORS, is_udplite);
a18135eb 1025 }
1da177e4
LT
1026 return err;
1027
1028do_confirm:
1029 dst_confirm(dst);
1030 if (!(msg->msg_flags&MSG_PROBE) || len)
1031 goto back_from_confirm;
1032 err = 0;
1033 goto out;
1034}
1035
7d06b2e0 1036void udpv6_destroy_sock(struct sock *sk)
1da177e4
LT
1037{
1038 lock_sock(sk);
1039 udp_v6_flush_pending_frames(sk);
1040 release_sock(sk);
1041
1042 inet6_destroy_sock(sk);
1da177e4
LT
1043}
1044
1045/*
1046 * Socket option code for UDP
1047 */
ba4e58ec 1048int udpv6_setsockopt(struct sock *sk, int level, int optname,
b7058842 1049 char __user *optval, unsigned int optlen)
3fdadf7d 1050{
db8dac20 1051 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0
GR
1052 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1053 udp_v6_push_pending_frames);
ba4e58ec 1054 return ipv6_setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1055}
1056
1057#ifdef CONFIG_COMPAT
ba4e58ec 1058int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
b7058842 1059 char __user *optval, unsigned int optlen)
3fdadf7d 1060{
db8dac20 1061 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0
GR
1062 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1063 udp_v6_push_pending_frames);
ba4e58ec 1064 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1065}
1066#endif
1067
ba4e58ec
GR
1068int udpv6_getsockopt(struct sock *sk, int level, int optname,
1069 char __user *optval, int __user *optlen)
3fdadf7d 1070{
db8dac20 1071 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0 1072 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
ba4e58ec 1073 return ipv6_getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1074}
1075
1076#ifdef CONFIG_COMPAT
ba4e58ec
GR
1077int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1078 char __user *optval, int __user *optlen)
3fdadf7d 1079{
db8dac20 1080 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0 1081 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
ba4e58ec 1082 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1083}
1084#endif
1085
ba735425
SS
1086static int udp6_ufo_send_check(struct sk_buff *skb)
1087{
1088 struct ipv6hdr *ipv6h;
1089 struct udphdr *uh;
1090
1091 if (!pskb_may_pull(skb, sizeof(*uh)))
1092 return -EINVAL;
1093
1094 ipv6h = ipv6_hdr(skb);
1095 uh = udp_hdr(skb);
1096
1097 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1098 IPPROTO_UDP, 0);
1099 skb->csum_start = skb_transport_header(skb) - skb->head;
1100 skb->csum_offset = offsetof(struct udphdr, check);
1101 skb->ip_summed = CHECKSUM_PARTIAL;
1102 return 0;
1103}
1104
1105static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, int features)
1106{
1107 struct sk_buff *segs = ERR_PTR(-EINVAL);
1108 unsigned int mss;
1109 unsigned int unfrag_ip6hlen, unfrag_len;
1110 struct frag_hdr *fptr;
1111 u8 *mac_start, *prevhdr;
1112 u8 nexthdr;
1113 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1114 int offset;
1115 __wsum csum;
1116
1117 mss = skb_shinfo(skb)->gso_size;
1118 if (unlikely(skb->len <= mss))
1119 goto out;
1120
1121 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1122 /* Packet is from an untrusted source, reset gso_segs. */
1123 int type = skb_shinfo(skb)->gso_type;
1124
1125 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1126 !(type & (SKB_GSO_UDP))))
1127 goto out;
1128
1129 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1130
1131 segs = NULL;
1132 goto out;
1133 }
1134
1135 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1136 * do checksum of UDP packets sent as multiple IP fragments.
1137 */
1138 offset = skb->csum_start - skb_headroom(skb);
1139 csum = skb_checksum(skb, offset, skb->len- offset, 0);
1140 offset += skb->csum_offset;
1141 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1142 skb->ip_summed = CHECKSUM_NONE;
1143
1144 /* Check if there is enough headroom to insert fragment header. */
1145 if ((skb_headroom(skb) < frag_hdr_sz) &&
1146 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1147 goto out;
1148
1149 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1150 * bytes to insert fragment header.
1151 */
1152 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1153 nexthdr = *prevhdr;
1154 *prevhdr = NEXTHDR_FRAGMENT;
1155 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1156 unfrag_ip6hlen;
1157 mac_start = skb_mac_header(skb);
1158 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1159
1160 skb->mac_header -= frag_hdr_sz;
1161 skb->network_header -= frag_hdr_sz;
1162
1163 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1164 fptr->nexthdr = nexthdr;
1165 fptr->reserved = 0;
1166 ipv6_select_ident(fptr);
1167
1168 /* Fragment the skb. ipv6 header and the remaining fields of the
1169 * fragment header are updated in ipv6_gso_segment()
1170 */
1171 segs = skb_segment(skb, features);
1172
1173out:
1174 return segs;
1175}
1176
41135cc8 1177static const struct inet6_protocol udpv6_protocol = {
1da177e4
LT
1178 .handler = udpv6_rcv,
1179 .err_handler = udpv6_err,
ba735425
SS
1180 .gso_send_check = udp6_ufo_send_check,
1181 .gso_segment = udp6_ufo_fragment,
1da177e4
LT
1182 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1183};
1184
1185/* ------------------------------------------------------------------------ */
1186#ifdef CONFIG_PROC_FS
1187
1188static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1189{
1190 struct inet_sock *inet = inet_sk(sp);
1191 struct ipv6_pinfo *np = inet6_sk(sp);
1192 struct in6_addr *dest, *src;
1193 __u16 destp, srcp;
1194
1195 dest = &np->daddr;
1196 src = &np->rcv_saddr;
1197 destp = ntohs(inet->dport);
1198 srcp = ntohs(inet->sport);
1199 seq_printf(seq,
f86dcc5a 1200 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
cb61cb9b 1201 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1da177e4
LT
1202 bucket,
1203 src->s6_addr32[0], src->s6_addr32[1],
1204 src->s6_addr32[2], src->s6_addr32[3], srcp,
1205 dest->s6_addr32[0], dest->s6_addr32[1],
1206 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1ab1457c 1207 sp->sk_state,
31e6d363
ED
1208 sk_wmem_alloc_get(sp),
1209 sk_rmem_alloc_get(sp),
1da177e4
LT
1210 0, 0L, 0,
1211 sock_i_uid(sp), 0,
1212 sock_i_ino(sp),
cb61cb9b
ED
1213 atomic_read(&sp->sk_refcnt), sp,
1214 atomic_read(&sp->sk_drops));
1da177e4
LT
1215}
1216
ba4e58ec 1217int udp6_seq_show(struct seq_file *seq, void *v)
1da177e4
LT
1218{
1219 if (v == SEQ_START_TOKEN)
1220 seq_printf(seq,
1221 " sl "
1222 "local_address "
1223 "remote_address "
1224 "st tx_queue rx_queue tr tm->when retrnsmt"
cb61cb9b 1225 " uid timeout inode ref pointer drops\n");
1da177e4
LT
1226 else
1227 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1228 return 0;
1229}
1230
1da177e4 1231static struct udp_seq_afinfo udp6_seq_afinfo = {
1da177e4
LT
1232 .name = "udp6",
1233 .family = AF_INET6,
645ca708 1234 .udp_table = &udp_table,
4ad96d39
DL
1235 .seq_fops = {
1236 .owner = THIS_MODULE,
1237 },
dda61925
DL
1238 .seq_ops = {
1239 .show = udp6_seq_show,
1240 },
1da177e4
LT
1241};
1242
0c96d8c5 1243int udp6_proc_init(struct net *net)
1da177e4 1244{
0c96d8c5 1245 return udp_proc_register(net, &udp6_seq_afinfo);
1da177e4
LT
1246}
1247
0c96d8c5
DL
1248void udp6_proc_exit(struct net *net) {
1249 udp_proc_unregister(net, &udp6_seq_afinfo);
1da177e4
LT
1250}
1251#endif /* CONFIG_PROC_FS */
1252
1253/* ------------------------------------------------------------------------ */
1254
1255struct proto udpv6_prot = {
543d9cfe
ACM
1256 .name = "UDPv6",
1257 .owner = THIS_MODULE,
ba4e58ec 1258 .close = udp_lib_close,
543d9cfe
ACM
1259 .connect = ip6_datagram_connect,
1260 .disconnect = udp_disconnect,
1261 .ioctl = udp_ioctl,
1262 .destroy = udpv6_destroy_sock,
1263 .setsockopt = udpv6_setsockopt,
1264 .getsockopt = udpv6_getsockopt,
1265 .sendmsg = udpv6_sendmsg,
1266 .recvmsg = udpv6_recvmsg,
1267 .backlog_rcv = udpv6_queue_rcv_skb,
ba4e58ec
GR
1268 .hash = udp_lib_hash,
1269 .unhash = udp_lib_unhash,
543d9cfe 1270 .get_port = udp_v6_get_port,
95766fff
HA
1271 .memory_allocated = &udp_memory_allocated,
1272 .sysctl_mem = sysctl_udp_mem,
1273 .sysctl_wmem = &sysctl_udp_wmem_min,
1274 .sysctl_rmem = &sysctl_udp_rmem_min,
543d9cfe 1275 .obj_size = sizeof(struct udp6_sock),
271b72c7 1276 .slab_flags = SLAB_DESTROY_BY_RCU,
645ca708 1277 .h.udp_table = &udp_table,
3fdadf7d 1278#ifdef CONFIG_COMPAT
543d9cfe
ACM
1279 .compat_setsockopt = compat_udpv6_setsockopt,
1280 .compat_getsockopt = compat_udpv6_getsockopt,
3fdadf7d 1281#endif
1da177e4
LT
1282};
1283
1da177e4
LT
1284static struct inet_protosw udpv6_protosw = {
1285 .type = SOCK_DGRAM,
1286 .protocol = IPPROTO_UDP,
1287 .prot = &udpv6_prot,
1288 .ops = &inet6_dgram_ops,
1289 .capability =-1,
1290 .no_check = UDP_CSUM_DEFAULT,
1291 .flags = INET_PROTOSW_PERMANENT,
1292};
1293
1294
7f4e4868 1295int __init udpv6_init(void)
1da177e4 1296{
7f4e4868
DL
1297 int ret;
1298
1299 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1300 if (ret)
1301 goto out;
1302
1303 ret = inet6_register_protosw(&udpv6_protosw);
1304 if (ret)
1305 goto out_udpv6_protocol;
1306out:
1307 return ret;
1308
1309out_udpv6_protocol:
1310 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1311 goto out;
1312}
1313
09f7709f 1314void udpv6_exit(void)
7f4e4868
DL
1315{
1316 inet6_unregister_protosw(&udpv6_protosw);
1317 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1da177e4 1318}