]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/udp.c
udp: RCU handling for Unicast packets.
[net-next-2.6.git] / net / ipv4 / udp.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The User Datagram Protocol (UDP).
7 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
113aa838 11 * Alan Cox, <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
12 * Hirokazu Takahashi, <taka@valinux.co.jp>
13 *
14 * Fixes:
15 * Alan Cox : verify_area() calls
16 * Alan Cox : stopped close while in use off icmp
17 * messages. Not a fix but a botch that
18 * for udp at least is 'valid'.
19 * Alan Cox : Fixed icmp handling properly
20 * Alan Cox : Correct error for oversized datagrams
e905a9ed
YH
21 * Alan Cox : Tidied select() semantics.
22 * Alan Cox : udp_err() fixed properly, also now
1da177e4
LT
23 * select and read wake correctly on errors
24 * Alan Cox : udp_send verify_area moved to avoid mem leak
25 * Alan Cox : UDP can count its memory
26 * Alan Cox : send to an unknown connection causes
27 * an ECONNREFUSED off the icmp, but
28 * does NOT close.
29 * Alan Cox : Switched to new sk_buff handlers. No more backlog!
30 * Alan Cox : Using generic datagram code. Even smaller and the PEEK
31 * bug no longer crashes it.
32 * Fred Van Kempen : Net2e support for sk->broadcast.
33 * Alan Cox : Uses skb_free_datagram
34 * Alan Cox : Added get/set sockopt support.
35 * Alan Cox : Broadcasting without option set returns EACCES.
36 * Alan Cox : No wakeup calls. Instead we now use the callbacks.
37 * Alan Cox : Use ip_tos and ip_ttl
38 * Alan Cox : SNMP Mibs
39 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
40 * Matt Dillon : UDP length checks.
41 * Alan Cox : Smarter af_inet used properly.
42 * Alan Cox : Use new kernel side addressing.
43 * Alan Cox : Incorrect return on truncated datagram receive.
44 * Arnt Gulbrandsen : New udp_send and stuff
45 * Alan Cox : Cache last socket
46 * Alan Cox : Route cache
47 * Jon Peatfield : Minor efficiency fix to sendto().
48 * Mike Shaver : RFC1122 checks.
49 * Alan Cox : Nonblocking error fix.
50 * Willy Konynenberg : Transparent proxying support.
51 * Mike McLagan : Routing by source
52 * David S. Miller : New socket lookup architecture.
53 * Last socket cache retained as it
54 * does have a high hit rate.
55 * Olaf Kirch : Don't linearise iovec on sendmsg.
56 * Andi Kleen : Some cleanups, cache destination entry
e905a9ed 57 * for connect.
1da177e4
LT
58 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
59 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
60 * return ENOTCONN for unconnected sockets (POSIX)
61 * Janos Farkas : don't deliver multi/broadcasts to a different
62 * bound-to-device socket
63 * Hirokazu Takahashi : HW checksumming for outgoing UDP
64 * datagrams.
65 * Hirokazu Takahashi : sendfile() on UDP works now.
66 * Arnaldo C. Melo : convert /proc/net/udp to seq_file
67 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
68 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
69 * a single port at the same time.
70 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
342f0234 71 * James Chapman : Add L2TP encapsulation type.
1da177e4
LT
72 *
73 *
74 * This program is free software; you can redistribute it and/or
75 * modify it under the terms of the GNU General Public License
76 * as published by the Free Software Foundation; either version
77 * 2 of the License, or (at your option) any later version.
78 */
e905a9ed 79
1da177e4
LT
80#include <asm/system.h>
81#include <asm/uaccess.h>
82#include <asm/ioctls.h>
95766fff 83#include <linux/bootmem.h>
1da177e4
LT
84#include <linux/types.h>
85#include <linux/fcntl.h>
86#include <linux/module.h>
87#include <linux/socket.h>
88#include <linux/sockios.h>
14c85021 89#include <linux/igmp.h>
1da177e4
LT
90#include <linux/in.h>
91#include <linux/errno.h>
92#include <linux/timer.h>
93#include <linux/mm.h>
1da177e4 94#include <linux/inet.h>
1da177e4 95#include <linux/netdevice.h>
c752f073 96#include <net/tcp_states.h>
1da177e4
LT
97#include <linux/skbuff.h>
98#include <linux/proc_fs.h>
99#include <linux/seq_file.h>
457c4cbc 100#include <net/net_namespace.h>
1da177e4
LT
101#include <net/icmp.h>
102#include <net/route.h>
1da177e4
LT
103#include <net/checksum.h>
104#include <net/xfrm.h>
ba4e58ec 105#include "udp_impl.h"
1da177e4 106
645ca708
ED
107struct udp_table udp_table;
108EXPORT_SYMBOL(udp_table);
1da177e4 109
95766fff
HA
110int sysctl_udp_mem[3] __read_mostly;
111int sysctl_udp_rmem_min __read_mostly;
112int sysctl_udp_wmem_min __read_mostly;
113
114EXPORT_SYMBOL(sysctl_udp_mem);
115EXPORT_SYMBOL(sysctl_udp_rmem_min);
116EXPORT_SYMBOL(sysctl_udp_wmem_min);
117
118atomic_t udp_memory_allocated;
119EXPORT_SYMBOL(udp_memory_allocated);
120
f24d43c0 121static int udp_lib_lport_inuse(struct net *net, __u16 num,
645ca708 122 const struct udp_hslot *hslot,
f24d43c0
ED
123 struct sock *sk,
124 int (*saddr_comp)(const struct sock *sk1,
125 const struct sock *sk2))
1da177e4 126{
f24d43c0 127 struct sock *sk2;
1da177e4 128 struct hlist_node *node;
25030a7f 129
645ca708 130 sk_for_each(sk2, node, &hslot->head)
f24d43c0
ED
131 if (net_eq(sock_net(sk2), net) &&
132 sk2 != sk &&
133 sk2->sk_hash == num &&
134 (!sk2->sk_reuse || !sk->sk_reuse) &&
135 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
136 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
137 (*saddr_comp)(sk, sk2))
25030a7f
GR
138 return 1;
139 return 0;
140}
141
142/**
6ba5a3c5 143 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
25030a7f
GR
144 *
145 * @sk: socket struct in question
146 * @snum: port number to look up
df2bc459 147 * @saddr_comp: AF-dependent comparison of bound local IP addresses
25030a7f 148 */
6ba5a3c5 149int udp_lib_get_port(struct sock *sk, unsigned short snum,
df2bc459
DM
150 int (*saddr_comp)(const struct sock *sk1,
151 const struct sock *sk2 ) )
25030a7f 152{
645ca708
ED
153 struct udp_hslot *hslot;
154 struct udp_table *udptable = sk->sk_prot->h.udp_table;
25030a7f 155 int error = 1;
3b1e0a65 156 struct net *net = sock_net(sk);
1da177e4 157
32c1da70 158 if (!snum) {
9088c560
ED
159 int low, high, remaining;
160 unsigned rand;
161 unsigned short first;
32c1da70 162
227b60f5 163 inet_get_local_port_range(&low, &high);
a25de534 164 remaining = (high - low) + 1;
227b60f5 165
9088c560
ED
166 rand = net_random();
167 snum = first = rand % remaining + low;
168 rand |= 1;
645ca708
ED
169 for (;;) {
170 hslot = &udptable->hash[udp_hashfn(net, snum)];
171 spin_lock_bh(&hslot->lock);
172 if (!udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp))
173 break;
174 spin_unlock_bh(&hslot->lock);
9088c560
ED
175 do {
176 snum = snum + rand;
177 } while (snum < low || snum > high);
178 if (snum == first)
179 goto fail;
1da177e4 180 }
645ca708
ED
181 } else {
182 hslot = &udptable->hash[udp_hashfn(net, snum)];
183 spin_lock_bh(&hslot->lock);
184 if (udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp))
185 goto fail_unlock;
186 }
25030a7f 187 inet_sk(sk)->num = snum;
df2bc459 188 sk->sk_hash = snum;
1da177e4 189 if (sk_unhashed(sk)) {
271b72c7 190 sk_add_node_rcu(sk, &hslot->head);
c29a0bc4 191 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
1da177e4 192 }
25030a7f 193 error = 0;
645ca708
ED
194fail_unlock:
195 spin_unlock_bh(&hslot->lock);
1da177e4 196fail:
25030a7f
GR
197 return error;
198}
199
6ba5a3c5 200static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
db8dac20
DM
201{
202 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
203
204 return ( !ipv6_only_sock(sk2) &&
205 (!inet1->rcv_saddr || !inet2->rcv_saddr ||
206 inet1->rcv_saddr == inet2->rcv_saddr ));
207}
208
6ba5a3c5 209int udp_v4_get_port(struct sock *sk, unsigned short snum)
db8dac20 210{
6ba5a3c5 211 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal);
db8dac20
DM
212}
213
645ca708
ED
214static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
215 unsigned short hnum,
216 __be16 sport, __be32 daddr, __be16 dport, int dif)
217{
218 int score = -1;
219
220 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
221 !ipv6_only_sock(sk)) {
222 struct inet_sock *inet = inet_sk(sk);
223
224 score = (sk->sk_family == PF_INET ? 1 : 0);
225 if (inet->rcv_saddr) {
226 if (inet->rcv_saddr != daddr)
227 return -1;
228 score += 2;
229 }
230 if (inet->daddr) {
231 if (inet->daddr != saddr)
232 return -1;
233 score += 2;
234 }
235 if (inet->dport) {
236 if (inet->dport != sport)
237 return -1;
238 score += 2;
239 }
240 if (sk->sk_bound_dev_if) {
241 if (sk->sk_bound_dev_if != dif)
242 return -1;
243 score += 2;
244 }
245 }
246 return score;
247}
248
db8dac20
DM
249/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
250 * harder than this. -DaveM
251 */
252static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
253 __be16 sport, __be32 daddr, __be16 dport,
645ca708 254 int dif, struct udp_table *udptable)
db8dac20 255{
271b72c7 256 struct sock *sk, *result;
db8dac20
DM
257 struct hlist_node *node;
258 unsigned short hnum = ntohs(dport);
645ca708
ED
259 unsigned int hash = udp_hashfn(net, hnum);
260 struct udp_hslot *hslot = &udptable->hash[hash];
271b72c7 261 int score, badness;
645ca708 262
271b72c7
ED
263 rcu_read_lock();
264begin:
265 result = NULL;
266 badness = -1;
267 sk_for_each_rcu(sk, node, &hslot->head) {
268 /*
269 * lockless reader, and SLAB_DESTROY_BY_RCU items:
270 * We must check this item was not moved to another chain
271 */
272 if (udp_hashfn(net, sk->sk_hash) != hash)
273 goto begin;
645ca708
ED
274 score = compute_score(sk, net, saddr, hnum, sport,
275 daddr, dport, dif);
276 if (score > badness) {
277 result = sk;
278 badness = score;
db8dac20
DM
279 }
280 }
271b72c7
ED
281 if (result) {
282 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
283 result = NULL;
284 else if (unlikely(compute_score(result, net, saddr, hnum, sport,
285 daddr, dport, dif) < badness)) {
286 sock_put(result);
287 goto begin;
288 }
289 }
290 rcu_read_unlock();
db8dac20
DM
291 return result;
292}
293
607c4aaf
KK
294static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
295 __be16 sport, __be16 dport,
645ca708 296 struct udp_table *udptable)
607c4aaf 297{
23542618 298 struct sock *sk;
607c4aaf
KK
299 const struct iphdr *iph = ip_hdr(skb);
300
23542618
KK
301 if (unlikely(sk = skb_steal_sock(skb)))
302 return sk;
303 else
304 return __udp4_lib_lookup(dev_net(skb->dst->dev), iph->saddr, sport,
305 iph->daddr, dport, inet_iif(skb),
306 udptable);
607c4aaf
KK
307}
308
bcd41303
KK
309struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
310 __be32 daddr, __be16 dport, int dif)
311{
645ca708 312 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
bcd41303
KK
313}
314EXPORT_SYMBOL_GPL(udp4_lib_lookup);
315
db8dac20
DM
316static inline struct sock *udp_v4_mcast_next(struct sock *sk,
317 __be16 loc_port, __be32 loc_addr,
318 __be16 rmt_port, __be32 rmt_addr,
319 int dif)
320{
321 struct hlist_node *node;
322 struct sock *s = sk;
323 unsigned short hnum = ntohs(loc_port);
324
325 sk_for_each_from(s, node) {
326 struct inet_sock *inet = inet_sk(s);
327
328 if (s->sk_hash != hnum ||
329 (inet->daddr && inet->daddr != rmt_addr) ||
330 (inet->dport != rmt_port && inet->dport) ||
331 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
332 ipv6_only_sock(s) ||
333 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
334 continue;
335 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
336 continue;
337 goto found;
338 }
339 s = NULL;
340found:
341 return s;
342}
343
344/*
345 * This routine is called by the ICMP module when it gets some
346 * sort of error condition. If err < 0 then the socket should
347 * be closed and the error returned to the user. If err > 0
348 * it's just the icmp type << 8 | icmp code.
349 * Header points to the ip header of the error packet. We move
350 * on past this. Then (as it used to claim before adjustment)
351 * header points to the first 8 bytes of the udp header. We need
352 * to find the appropriate port.
353 */
354
645ca708 355void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
db8dac20
DM
356{
357 struct inet_sock *inet;
358 struct iphdr *iph = (struct iphdr*)skb->data;
359 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
360 const int type = icmp_hdr(skb)->type;
361 const int code = icmp_hdr(skb)->code;
362 struct sock *sk;
363 int harderr;
364 int err;
fd54d716 365 struct net *net = dev_net(skb->dev);
db8dac20 366
fd54d716 367 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
db8dac20
DM
368 iph->saddr, uh->source, skb->dev->ifindex, udptable);
369 if (sk == NULL) {
dcfc23ca 370 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
db8dac20
DM
371 return; /* No socket for error */
372 }
373
374 err = 0;
375 harderr = 0;
376 inet = inet_sk(sk);
377
378 switch (type) {
379 default:
380 case ICMP_TIME_EXCEEDED:
381 err = EHOSTUNREACH;
382 break;
383 case ICMP_SOURCE_QUENCH:
384 goto out;
385 case ICMP_PARAMETERPROB:
386 err = EPROTO;
387 harderr = 1;
388 break;
389 case ICMP_DEST_UNREACH:
390 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
391 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
392 err = EMSGSIZE;
393 harderr = 1;
394 break;
395 }
396 goto out;
397 }
398 err = EHOSTUNREACH;
399 if (code <= NR_ICMP_UNREACH) {
400 harderr = icmp_err_convert[code].fatal;
401 err = icmp_err_convert[code].errno;
402 }
403 break;
404 }
405
406 /*
407 * RFC1122: OK. Passes ICMP errors back to application, as per
408 * 4.1.3.3.
409 */
410 if (!inet->recverr) {
411 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
412 goto out;
413 } else {
414 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
415 }
416 sk->sk_err = err;
417 sk->sk_error_report(sk);
418out:
419 sock_put(sk);
420}
421
422void udp_err(struct sk_buff *skb, u32 info)
423{
645ca708 424 __udp4_lib_err(skb, info, &udp_table);
db8dac20
DM
425}
426
427/*
428 * Throw away all pending data and cancel the corking. Socket is locked.
429 */
36d926b9 430void udp_flush_pending_frames(struct sock *sk)
db8dac20
DM
431{
432 struct udp_sock *up = udp_sk(sk);
433
434 if (up->pending) {
435 up->len = 0;
436 up->pending = 0;
437 ip_flush_pending_frames(sk);
438 }
439}
36d926b9 440EXPORT_SYMBOL(udp_flush_pending_frames);
db8dac20
DM
441
442/**
443 * udp4_hwcsum_outgoing - handle outgoing HW checksumming
444 * @sk: socket we are sending on
445 * @skb: sk_buff containing the filled-in UDP header
446 * (checksum field must be zeroed out)
447 */
448static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
449 __be32 src, __be32 dst, int len )
450{
451 unsigned int offset;
452 struct udphdr *uh = udp_hdr(skb);
453 __wsum csum = 0;
454
455 if (skb_queue_len(&sk->sk_write_queue) == 1) {
456 /*
457 * Only one fragment on the socket.
458 */
459 skb->csum_start = skb_transport_header(skb) - skb->head;
460 skb->csum_offset = offsetof(struct udphdr, check);
461 uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
462 } else {
463 /*
464 * HW-checksum won't work as there are two or more
465 * fragments on the socket so that all csums of sk_buffs
466 * should be together
467 */
468 offset = skb_transport_offset(skb);
469 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
470
471 skb->ip_summed = CHECKSUM_NONE;
472
473 skb_queue_walk(&sk->sk_write_queue, skb) {
474 csum = csum_add(csum, skb->csum);
475 }
476
477 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
478 if (uh->check == 0)
479 uh->check = CSUM_MANGLED_0;
480 }
481}
482
483/*
484 * Push out all pending data as one UDP datagram. Socket is locked.
485 */
486static int udp_push_pending_frames(struct sock *sk)
487{
488 struct udp_sock *up = udp_sk(sk);
489 struct inet_sock *inet = inet_sk(sk);
490 struct flowi *fl = &inet->cork.fl;
491 struct sk_buff *skb;
492 struct udphdr *uh;
493 int err = 0;
494 int is_udplite = IS_UDPLITE(sk);
495 __wsum csum = 0;
496
497 /* Grab the skbuff where UDP header space exists. */
498 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
499 goto out;
500
501 /*
502 * Create a UDP header
503 */
504 uh = udp_hdr(skb);
505 uh->source = fl->fl_ip_sport;
506 uh->dest = fl->fl_ip_dport;
507 uh->len = htons(up->len);
508 uh->check = 0;
509
510 if (is_udplite) /* UDP-Lite */
511 csum = udplite_csum_outgoing(sk, skb);
512
513 else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
514
515 skb->ip_summed = CHECKSUM_NONE;
516 goto send;
517
518 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
519
520 udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len);
521 goto send;
522
523 } else /* `normal' UDP */
524 csum = udp_csum_outgoing(sk, skb);
525
526 /* add protocol-dependent pseudo-header */
527 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
528 sk->sk_protocol, csum );
529 if (uh->check == 0)
530 uh->check = CSUM_MANGLED_0;
531
532send:
533 err = ip_push_pending_frames(sk);
534out:
535 up->len = 0;
536 up->pending = 0;
537 if (!err)
629ca23c
PE
538 UDP_INC_STATS_USER(sock_net(sk),
539 UDP_MIB_OUTDATAGRAMS, is_udplite);
db8dac20
DM
540 return err;
541}
542
543int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
544 size_t len)
545{
546 struct inet_sock *inet = inet_sk(sk);
547 struct udp_sock *up = udp_sk(sk);
548 int ulen = len;
549 struct ipcm_cookie ipc;
550 struct rtable *rt = NULL;
551 int free = 0;
552 int connected = 0;
553 __be32 daddr, faddr, saddr;
554 __be16 dport;
555 u8 tos;
556 int err, is_udplite = IS_UDPLITE(sk);
557 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
558 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
559
560 if (len > 0xFFFF)
561 return -EMSGSIZE;
562
563 /*
564 * Check the flags.
565 */
566
567 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
568 return -EOPNOTSUPP;
569
570 ipc.opt = NULL;
571
572 if (up->pending) {
573 /*
574 * There are pending frames.
575 * The socket lock must be held while it's corked.
576 */
577 lock_sock(sk);
578 if (likely(up->pending)) {
579 if (unlikely(up->pending != AF_INET)) {
580 release_sock(sk);
581 return -EINVAL;
582 }
583 goto do_append_data;
584 }
585 release_sock(sk);
586 }
587 ulen += sizeof(struct udphdr);
588
589 /*
590 * Get and verify the address.
591 */
592 if (msg->msg_name) {
593 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
594 if (msg->msg_namelen < sizeof(*usin))
595 return -EINVAL;
596 if (usin->sin_family != AF_INET) {
597 if (usin->sin_family != AF_UNSPEC)
598 return -EAFNOSUPPORT;
599 }
600
601 daddr = usin->sin_addr.s_addr;
602 dport = usin->sin_port;
603 if (dport == 0)
604 return -EINVAL;
605 } else {
606 if (sk->sk_state != TCP_ESTABLISHED)
607 return -EDESTADDRREQ;
608 daddr = inet->daddr;
609 dport = inet->dport;
610 /* Open fast path for connected socket.
611 Route will not be used, if at least one option is set.
612 */
613 connected = 1;
614 }
615 ipc.addr = inet->saddr;
616
617 ipc.oif = sk->sk_bound_dev_if;
618 if (msg->msg_controllen) {
3b1e0a65 619 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
db8dac20
DM
620 if (err)
621 return err;
622 if (ipc.opt)
623 free = 1;
624 connected = 0;
625 }
626 if (!ipc.opt)
627 ipc.opt = inet->opt;
628
629 saddr = ipc.addr;
630 ipc.addr = faddr = daddr;
631
632 if (ipc.opt && ipc.opt->srr) {
633 if (!daddr)
634 return -EINVAL;
635 faddr = ipc.opt->faddr;
636 connected = 0;
637 }
638 tos = RT_TOS(inet->tos);
639 if (sock_flag(sk, SOCK_LOCALROUTE) ||
640 (msg->msg_flags & MSG_DONTROUTE) ||
641 (ipc.opt && ipc.opt->is_strictroute)) {
642 tos |= RTO_ONLINK;
643 connected = 0;
644 }
645
646 if (ipv4_is_multicast(daddr)) {
647 if (!ipc.oif)
648 ipc.oif = inet->mc_index;
649 if (!saddr)
650 saddr = inet->mc_addr;
651 connected = 0;
652 }
653
654 if (connected)
655 rt = (struct rtable*)sk_dst_check(sk, 0);
656
657 if (rt == NULL) {
658 struct flowi fl = { .oif = ipc.oif,
659 .nl_u = { .ip4_u =
660 { .daddr = faddr,
661 .saddr = saddr,
662 .tos = tos } },
663 .proto = sk->sk_protocol,
664 .uli_u = { .ports =
665 { .sport = inet->sport,
666 .dport = dport } } };
84a3aa00
PE
667 struct net *net = sock_net(sk);
668
db8dac20 669 security_sk_classify_flow(sk, &fl);
84a3aa00 670 err = ip_route_output_flow(net, &rt, &fl, sk, 1);
db8dac20
DM
671 if (err) {
672 if (err == -ENETUNREACH)
7c73a6fa 673 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
db8dac20
DM
674 goto out;
675 }
676
677 err = -EACCES;
678 if ((rt->rt_flags & RTCF_BROADCAST) &&
679 !sock_flag(sk, SOCK_BROADCAST))
680 goto out;
681 if (connected)
682 sk_dst_set(sk, dst_clone(&rt->u.dst));
683 }
684
685 if (msg->msg_flags&MSG_CONFIRM)
686 goto do_confirm;
687back_from_confirm:
688
689 saddr = rt->rt_src;
690 if (!ipc.addr)
691 daddr = ipc.addr = rt->rt_dst;
692
693 lock_sock(sk);
694 if (unlikely(up->pending)) {
695 /* The socket is already corked while preparing it. */
696 /* ... which is an evident application bug. --ANK */
697 release_sock(sk);
698
699 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
700 err = -EINVAL;
701 goto out;
702 }
703 /*
704 * Now cork the socket to pend data.
705 */
706 inet->cork.fl.fl4_dst = daddr;
707 inet->cork.fl.fl_ip_dport = dport;
708 inet->cork.fl.fl4_src = saddr;
709 inet->cork.fl.fl_ip_sport = inet->sport;
710 up->pending = AF_INET;
711
712do_append_data:
713 up->len += ulen;
714 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
715 err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
716 sizeof(struct udphdr), &ipc, rt,
717 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
718 if (err)
719 udp_flush_pending_frames(sk);
720 else if (!corkreq)
721 err = udp_push_pending_frames(sk);
722 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
723 up->pending = 0;
724 release_sock(sk);
725
726out:
727 ip_rt_put(rt);
728 if (free)
729 kfree(ipc.opt);
730 if (!err)
731 return len;
732 /*
733 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
734 * ENOBUFS might not be good (it's not tunable per se), but otherwise
735 * we don't have a good statistic (IpOutDiscards but it can be too many
736 * things). We could add another new stat but at least for now that
737 * seems like overkill.
738 */
739 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
629ca23c
PE
740 UDP_INC_STATS_USER(sock_net(sk),
741 UDP_MIB_SNDBUFERRORS, is_udplite);
db8dac20
DM
742 }
743 return err;
744
745do_confirm:
746 dst_confirm(&rt->u.dst);
747 if (!(msg->msg_flags&MSG_PROBE) || len)
748 goto back_from_confirm;
749 err = 0;
750 goto out;
751}
752
753int udp_sendpage(struct sock *sk, struct page *page, int offset,
754 size_t size, int flags)
755{
756 struct udp_sock *up = udp_sk(sk);
757 int ret;
758
759 if (!up->pending) {
760 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
761
762 /* Call udp_sendmsg to specify destination address which
763 * sendpage interface can't pass.
764 * This will succeed only when the socket is connected.
765 */
766 ret = udp_sendmsg(NULL, sk, &msg, 0);
767 if (ret < 0)
768 return ret;
769 }
770
771 lock_sock(sk);
772
773 if (unlikely(!up->pending)) {
774 release_sock(sk);
775
776 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
777 return -EINVAL;
778 }
779
780 ret = ip_append_page(sk, page, offset, size, flags);
781 if (ret == -EOPNOTSUPP) {
782 release_sock(sk);
783 return sock_no_sendpage(sk->sk_socket, page, offset,
784 size, flags);
785 }
786 if (ret < 0) {
787 udp_flush_pending_frames(sk);
788 goto out;
789 }
790
791 up->len += size;
792 if (!(up->corkflag || (flags&MSG_MORE)))
793 ret = udp_push_pending_frames(sk);
794 if (!ret)
795 ret = size;
796out:
797 release_sock(sk);
798 return ret;
799}
800
1da177e4
LT
801/*
802 * IOCTL requests applicable to the UDP protocol
803 */
e905a9ed 804
1da177e4
LT
805int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
806{
6516c655
SH
807 switch (cmd) {
808 case SIOCOUTQ:
1da177e4 809 {
6516c655
SH
810 int amount = atomic_read(&sk->sk_wmem_alloc);
811 return put_user(amount, (int __user *)arg);
812 }
1da177e4 813
6516c655
SH
814 case SIOCINQ:
815 {
816 struct sk_buff *skb;
817 unsigned long amount;
818
819 amount = 0;
820 spin_lock_bh(&sk->sk_receive_queue.lock);
821 skb = skb_peek(&sk->sk_receive_queue);
822 if (skb != NULL) {
823 /*
824 * We will only return the amount
825 * of this packet since that is all
826 * that will be read.
827 */
828 amount = skb->len - sizeof(struct udphdr);
1da177e4 829 }
6516c655
SH
830 spin_unlock_bh(&sk->sk_receive_queue.lock);
831 return put_user(amount, (int __user *)arg);
832 }
1da177e4 833
6516c655
SH
834 default:
835 return -ENOIOCTLCMD;
1da177e4 836 }
6516c655
SH
837
838 return 0;
1da177e4
LT
839}
840
db8dac20
DM
841/*
842 * This should be easy, if there is something there we
843 * return it, otherwise we block.
844 */
845
846int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
847 size_t len, int noblock, int flags, int *addr_len)
848{
849 struct inet_sock *inet = inet_sk(sk);
850 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
851 struct sk_buff *skb;
852 unsigned int ulen, copied;
853 int peeked;
854 int err;
855 int is_udplite = IS_UDPLITE(sk);
856
857 /*
858 * Check any passed addresses
859 */
860 if (addr_len)
861 *addr_len=sizeof(*sin);
862
863 if (flags & MSG_ERRQUEUE)
864 return ip_recv_error(sk, msg, len);
865
866try_again:
867 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
868 &peeked, &err);
869 if (!skb)
870 goto out;
871
872 ulen = skb->len - sizeof(struct udphdr);
873 copied = len;
874 if (copied > ulen)
875 copied = ulen;
876 else if (copied < ulen)
877 msg->msg_flags |= MSG_TRUNC;
878
879 /*
880 * If checksum is needed at all, try to do it while copying the
881 * data. If the data is truncated, or if we only want a partial
882 * coverage checksum (UDP-Lite), do it before the copy.
883 */
884
885 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
886 if (udp_lib_checksum_complete(skb))
887 goto csum_copy_err;
888 }
889
890 if (skb_csum_unnecessary(skb))
891 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
892 msg->msg_iov, copied );
893 else {
894 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
895
896 if (err == -EINVAL)
897 goto csum_copy_err;
898 }
899
900 if (err)
901 goto out_free;
902
903 if (!peeked)
629ca23c
PE
904 UDP_INC_STATS_USER(sock_net(sk),
905 UDP_MIB_INDATAGRAMS, is_udplite);
db8dac20
DM
906
907 sock_recv_timestamp(msg, sk, skb);
908
909 /* Copy the address. */
910 if (sin)
911 {
912 sin->sin_family = AF_INET;
913 sin->sin_port = udp_hdr(skb)->source;
914 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
915 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
916 }
917 if (inet->cmsg_flags)
918 ip_cmsg_recv(msg, skb);
919
920 err = copied;
921 if (flags & MSG_TRUNC)
922 err = ulen;
923
924out_free:
925 lock_sock(sk);
926 skb_free_datagram(sk, skb);
927 release_sock(sk);
928out:
929 return err;
930
931csum_copy_err:
932 lock_sock(sk);
933 if (!skb_kill_datagram(sk, skb, flags))
629ca23c 934 UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
db8dac20
DM
935 release_sock(sk);
936
937 if (noblock)
938 return -EAGAIN;
939 goto try_again;
940}
941
942
1da177e4
LT
943int udp_disconnect(struct sock *sk, int flags)
944{
945 struct inet_sock *inet = inet_sk(sk);
946 /*
947 * 1003.1g - break association.
948 */
e905a9ed 949
1da177e4
LT
950 sk->sk_state = TCP_CLOSE;
951 inet->daddr = 0;
952 inet->dport = 0;
953 sk->sk_bound_dev_if = 0;
954 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
955 inet_reset_saddr(sk);
956
957 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
958 sk->sk_prot->unhash(sk);
959 inet->sport = 0;
960 }
961 sk_dst_reset(sk);
962 return 0;
963}
964
645ca708
ED
965void udp_lib_unhash(struct sock *sk)
966{
967 struct udp_table *udptable = sk->sk_prot->h.udp_table;
968 unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash);
969 struct udp_hslot *hslot = &udptable->hash[hash];
970
971 spin_lock(&hslot->lock);
271b72c7 972 if (sk_del_node_init_rcu(sk)) {
645ca708
ED
973 inet_sk(sk)->num = 0;
974 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
975 }
976 spin_unlock(&hslot->lock);
977}
978EXPORT_SYMBOL(udp_lib_unhash);
979
93821778
HX
980static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
981{
982 int is_udplite = IS_UDPLITE(sk);
983 int rc;
984
985 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
986 /* Note that an ENOMEM error is charged twice */
987 if (rc == -ENOMEM)
988 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
989 is_udplite);
990 goto drop;
991 }
992
993 return 0;
994
995drop:
996 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
997 kfree_skb(skb);
998 return -1;
999}
1000
db8dac20
DM
1001/* returns:
1002 * -1: error
1003 * 0: success
1004 * >0: "udp encap" protocol resubmission
1005 *
1006 * Note that in the success and error cases, the skb is assumed to
1007 * have either been requeued or freed.
1008 */
1009int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1010{
1011 struct udp_sock *up = udp_sk(sk);
1012 int rc;
1013 int is_udplite = IS_UDPLITE(sk);
1014
1015 /*
1016 * Charge it to the socket, dropping if the queue is full.
1017 */
1018 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1019 goto drop;
1020 nf_reset(skb);
1021
1022 if (up->encap_type) {
1023 /*
1024 * This is an encapsulation socket so pass the skb to
1025 * the socket's udp_encap_rcv() hook. Otherwise, just
1026 * fall through and pass this up the UDP socket.
1027 * up->encap_rcv() returns the following value:
1028 * =0 if skb was successfully passed to the encap
1029 * handler or was discarded by it.
1030 * >0 if skb should be passed on to UDP.
1031 * <0 if skb should be resubmitted as proto -N
1032 */
1033
1034 /* if we're overly short, let UDP handle it */
1035 if (skb->len > sizeof(struct udphdr) &&
1036 up->encap_rcv != NULL) {
1037 int ret;
1038
1039 ret = (*up->encap_rcv)(sk, skb);
1040 if (ret <= 0) {
0283328e
PE
1041 UDP_INC_STATS_BH(sock_net(sk),
1042 UDP_MIB_INDATAGRAMS,
db8dac20
DM
1043 is_udplite);
1044 return -ret;
1045 }
1046 }
1047
1048 /* FALLTHROUGH -- it's a UDP Packet */
1049 }
1050
1051 /*
1052 * UDP-Lite specific tests, ignored on UDP sockets
1053 */
1054 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
1055
1056 /*
1057 * MIB statistics other than incrementing the error count are
1058 * disabled for the following two types of errors: these depend
1059 * on the application settings, not on the functioning of the
1060 * protocol stack as such.
1061 *
1062 * RFC 3828 here recommends (sec 3.3): "There should also be a
1063 * way ... to ... at least let the receiving application block
1064 * delivery of packets with coverage values less than a value
1065 * provided by the application."
1066 */
1067 if (up->pcrlen == 0) { /* full coverage was set */
1068 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
1069 "%d while full coverage %d requested\n",
1070 UDP_SKB_CB(skb)->cscov, skb->len);
1071 goto drop;
1072 }
1073 /* The next case involves violating the min. coverage requested
1074 * by the receiver. This is subtle: if receiver wants x and x is
1075 * greater than the buffersize/MTU then receiver will complain
1076 * that it wants x while sender emits packets of smaller size y.
1077 * Therefore the above ...()->partial_cov statement is essential.
1078 */
1079 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
1080 LIMIT_NETDEBUG(KERN_WARNING
1081 "UDPLITE: coverage %d too small, need min %d\n",
1082 UDP_SKB_CB(skb)->cscov, up->pcrlen);
1083 goto drop;
1084 }
1085 }
1086
1087 if (sk->sk_filter) {
1088 if (udp_lib_checksum_complete(skb))
1089 goto drop;
1090 }
1091
93821778 1092 rc = 0;
db8dac20 1093
93821778
HX
1094 bh_lock_sock(sk);
1095 if (!sock_owned_by_user(sk))
1096 rc = __udp_queue_rcv_skb(sk, skb);
1097 else
1098 sk_add_backlog(sk, skb);
1099 bh_unlock_sock(sk);
1100
1101 return rc;
db8dac20
DM
1102
1103drop:
0283328e 1104 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
db8dac20
DM
1105 kfree_skb(skb);
1106 return -1;
1107}
1108
1109/*
1110 * Multicasts and broadcasts go to each listener.
1111 *
1112 * Note: called only from the BH handler context,
1113 * so we don't need to lock the hashes.
1114 */
e3163493 1115static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
db8dac20
DM
1116 struct udphdr *uh,
1117 __be32 saddr, __be32 daddr,
645ca708 1118 struct udp_table *udptable)
db8dac20
DM
1119{
1120 struct sock *sk;
645ca708 1121 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
db8dac20
DM
1122 int dif;
1123
645ca708
ED
1124 spin_lock(&hslot->lock);
1125 sk = sk_head(&hslot->head);
db8dac20
DM
1126 dif = skb->dev->ifindex;
1127 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1128 if (sk) {
1129 struct sock *sknext = NULL;
1130
1131 do {
1132 struct sk_buff *skb1 = skb;
1133
1134 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1135 uh->source, saddr, dif);
1136 if (sknext)
1137 skb1 = skb_clone(skb, GFP_ATOMIC);
1138
1139 if (skb1) {
93821778 1140 int ret = udp_queue_rcv_skb(sk, skb1);
db8dac20
DM
1141 if (ret > 0)
1142 /* we should probably re-process instead
1143 * of dropping packets here. */
1144 kfree_skb(skb1);
1145 }
1146 sk = sknext;
1147 } while (sknext);
1148 } else
1149 kfree_skb(skb);
645ca708 1150 spin_unlock(&hslot->lock);
db8dac20
DM
1151 return 0;
1152}
1153
1154/* Initialize UDP checksum. If exited with zero value (success),
1155 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1156 * Otherwise, csum completion requires chacksumming packet body,
1157 * including udp header and folding it to skb->csum.
1158 */
1159static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1160 int proto)
1161{
1162 const struct iphdr *iph;
1163 int err;
1164
1165 UDP_SKB_CB(skb)->partial_cov = 0;
1166 UDP_SKB_CB(skb)->cscov = skb->len;
1167
1168 if (proto == IPPROTO_UDPLITE) {
1169 err = udplite_checksum_init(skb, uh);
1170 if (err)
1171 return err;
1172 }
1173
1174 iph = ip_hdr(skb);
1175 if (uh->check == 0) {
1176 skb->ip_summed = CHECKSUM_UNNECESSARY;
1177 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1178 if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1179 proto, skb->csum))
1180 skb->ip_summed = CHECKSUM_UNNECESSARY;
1181 }
1182 if (!skb_csum_unnecessary(skb))
1183 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1184 skb->len, proto, 0);
1185 /* Probably, we should checksum udp header (it should be in cache
1186 * in any case) and data in tiny packets (< rx copybreak).
1187 */
1188
1189 return 0;
1190}
1191
1192/*
1193 * All we need to do is get the socket, and then do a checksum.
1194 */
1195
645ca708 1196int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
db8dac20
DM
1197 int proto)
1198{
1199 struct sock *sk;
1200 struct udphdr *uh = udp_hdr(skb);
1201 unsigned short ulen;
1202 struct rtable *rt = (struct rtable*)skb->dst;
1203 __be32 saddr = ip_hdr(skb)->saddr;
1204 __be32 daddr = ip_hdr(skb)->daddr;
0283328e 1205 struct net *net = dev_net(skb->dev);
db8dac20
DM
1206
1207 /*
1208 * Validate the packet.
1209 */
1210 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1211 goto drop; /* No space for header. */
1212
1213 ulen = ntohs(uh->len);
1214 if (ulen > skb->len)
1215 goto short_packet;
1216
1217 if (proto == IPPROTO_UDP) {
1218 /* UDP validates ulen. */
1219 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1220 goto short_packet;
1221 uh = udp_hdr(skb);
1222 }
1223
1224 if (udp4_csum_init(skb, uh, proto))
1225 goto csum_error;
1226
1227 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
e3163493
PE
1228 return __udp4_lib_mcast_deliver(net, skb, uh,
1229 saddr, daddr, udptable);
db8dac20 1230
607c4aaf 1231 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
db8dac20
DM
1232
1233 if (sk != NULL) {
93821778 1234 int ret = udp_queue_rcv_skb(sk, skb);
db8dac20
DM
1235 sock_put(sk);
1236
1237 /* a return value > 0 means to resubmit the input, but
1238 * it wants the return to be -protocol, or 0
1239 */
1240 if (ret > 0)
1241 return -ret;
1242 return 0;
1243 }
1244
1245 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1246 goto drop;
1247 nf_reset(skb);
1248
1249 /* No socket. Drop packet silently, if checksum is wrong */
1250 if (udp_lib_checksum_complete(skb))
1251 goto csum_error;
1252
0283328e 1253 UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
db8dac20
DM
1254 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1255
1256 /*
1257 * Hmm. We got an UDP packet to a port to which we
1258 * don't wanna listen. Ignore it.
1259 */
1260 kfree_skb(skb);
1261 return 0;
1262
1263short_packet:
a7d632b6 1264 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n",
db8dac20
DM
1265 proto == IPPROTO_UDPLITE ? "-Lite" : "",
1266 NIPQUAD(saddr),
1267 ntohs(uh->source),
1268 ulen,
1269 skb->len,
1270 NIPQUAD(daddr),
1271 ntohs(uh->dest));
1272 goto drop;
1273
1274csum_error:
1275 /*
1276 * RFC1122: OK. Discards the bad packet silently (as far as
1277 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1278 */
a7d632b6 1279 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n",
db8dac20
DM
1280 proto == IPPROTO_UDPLITE ? "-Lite" : "",
1281 NIPQUAD(saddr),
1282 ntohs(uh->source),
1283 NIPQUAD(daddr),
1284 ntohs(uh->dest),
1285 ulen);
1286drop:
0283328e 1287 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
db8dac20
DM
1288 kfree_skb(skb);
1289 return 0;
1290}
1291
1292int udp_rcv(struct sk_buff *skb)
1293{
645ca708 1294 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
db8dac20
DM
1295}
1296
7d06b2e0 1297void udp_destroy_sock(struct sock *sk)
db8dac20
DM
1298{
1299 lock_sock(sk);
1300 udp_flush_pending_frames(sk);
1301 release_sock(sk);
db8dac20
DM
1302}
1303
1da177e4
LT
1304/*
1305 * Socket option code for UDP
1306 */
4c0a6cb0
GR
1307int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1308 char __user *optval, int optlen,
1309 int (*push_pending_frames)(struct sock *))
1da177e4
LT
1310{
1311 struct udp_sock *up = udp_sk(sk);
1312 int val;
1313 int err = 0;
b2bf1e26 1314 int is_udplite = IS_UDPLITE(sk);
1da177e4 1315
6516c655 1316 if (optlen<sizeof(int))
1da177e4
LT
1317 return -EINVAL;
1318
1319 if (get_user(val, (int __user *)optval))
1320 return -EFAULT;
1321
6516c655 1322 switch (optname) {
1da177e4
LT
1323 case UDP_CORK:
1324 if (val != 0) {
1325 up->corkflag = 1;
1326 } else {
1327 up->corkflag = 0;
1328 lock_sock(sk);
4c0a6cb0 1329 (*push_pending_frames)(sk);
1da177e4
LT
1330 release_sock(sk);
1331 }
1332 break;
e905a9ed 1333
1da177e4
LT
1334 case UDP_ENCAP:
1335 switch (val) {
1336 case 0:
1337 case UDP_ENCAP_ESPINUDP:
1338 case UDP_ENCAP_ESPINUDP_NON_IKE:
067b207b
JC
1339 up->encap_rcv = xfrm4_udp_encap_rcv;
1340 /* FALLTHROUGH */
342f0234 1341 case UDP_ENCAP_L2TPINUDP:
1da177e4
LT
1342 up->encap_type = val;
1343 break;
1344 default:
1345 err = -ENOPROTOOPT;
1346 break;
1347 }
1348 break;
1349
ba4e58ec
GR
1350 /*
1351 * UDP-Lite's partial checksum coverage (RFC 3828).
1352 */
1353 /* The sender sets actual checksum coverage length via this option.
1354 * The case coverage > packet length is handled by send module. */
1355 case UDPLITE_SEND_CSCOV:
b2bf1e26 1356 if (!is_udplite) /* Disable the option on UDP sockets */
ba4e58ec
GR
1357 return -ENOPROTOOPT;
1358 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1359 val = 8;
47112e25
GR
1360 else if (val > USHORT_MAX)
1361 val = USHORT_MAX;
ba4e58ec
GR
1362 up->pcslen = val;
1363 up->pcflag |= UDPLITE_SEND_CC;
1364 break;
1365
e905a9ed
YH
1366 /* The receiver specifies a minimum checksum coverage value. To make
1367 * sense, this should be set to at least 8 (as done below). If zero is
ba4e58ec
GR
1368 * used, this again means full checksum coverage. */
1369 case UDPLITE_RECV_CSCOV:
b2bf1e26 1370 if (!is_udplite) /* Disable the option on UDP sockets */
ba4e58ec
GR
1371 return -ENOPROTOOPT;
1372 if (val != 0 && val < 8) /* Avoid silly minimal values. */
1373 val = 8;
47112e25
GR
1374 else if (val > USHORT_MAX)
1375 val = USHORT_MAX;
ba4e58ec
GR
1376 up->pcrlen = val;
1377 up->pcflag |= UDPLITE_RECV_CC;
1378 break;
1379
1da177e4
LT
1380 default:
1381 err = -ENOPROTOOPT;
1382 break;
6516c655 1383 }
1da177e4
LT
1384
1385 return err;
1386}
1387
db8dac20
DM
1388int udp_setsockopt(struct sock *sk, int level, int optname,
1389 char __user *optval, int optlen)
1390{
1391 if (level == SOL_UDP || level == SOL_UDPLITE)
1392 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1393 udp_push_pending_frames);
1394 return ip_setsockopt(sk, level, optname, optval, optlen);
1395}
1396
1397#ifdef CONFIG_COMPAT
1398int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1399 char __user *optval, int optlen)
1400{
1401 if (level == SOL_UDP || level == SOL_UDPLITE)
1402 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1403 udp_push_pending_frames);
1404 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1405}
1406#endif
1407
4c0a6cb0
GR
1408int udp_lib_getsockopt(struct sock *sk, int level, int optname,
1409 char __user *optval, int __user *optlen)
1da177e4
LT
1410{
1411 struct udp_sock *up = udp_sk(sk);
1412 int val, len;
1413
6516c655 1414 if (get_user(len,optlen))
1da177e4
LT
1415 return -EFAULT;
1416
1417 len = min_t(unsigned int, len, sizeof(int));
e905a9ed 1418
6516c655 1419 if (len < 0)
1da177e4
LT
1420 return -EINVAL;
1421
6516c655 1422 switch (optname) {
1da177e4
LT
1423 case UDP_CORK:
1424 val = up->corkflag;
1425 break;
1426
1427 case UDP_ENCAP:
1428 val = up->encap_type;
1429 break;
1430
ba4e58ec
GR
1431 /* The following two cannot be changed on UDP sockets, the return is
1432 * always 0 (which corresponds to the full checksum coverage of UDP). */
1433 case UDPLITE_SEND_CSCOV:
1434 val = up->pcslen;
1435 break;
1436
1437 case UDPLITE_RECV_CSCOV:
1438 val = up->pcrlen;
1439 break;
1440
1da177e4
LT
1441 default:
1442 return -ENOPROTOOPT;
6516c655 1443 }
1da177e4 1444
6516c655 1445 if (put_user(len, optlen))
e905a9ed 1446 return -EFAULT;
6516c655 1447 if (copy_to_user(optval, &val,len))
1da177e4 1448 return -EFAULT;
e905a9ed 1449 return 0;
1da177e4
LT
1450}
1451
db8dac20
DM
1452int udp_getsockopt(struct sock *sk, int level, int optname,
1453 char __user *optval, int __user *optlen)
1454{
1455 if (level == SOL_UDP || level == SOL_UDPLITE)
1456 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1457 return ip_getsockopt(sk, level, optname, optval, optlen);
1458}
1459
1460#ifdef CONFIG_COMPAT
1461int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1462 char __user *optval, int __user *optlen)
1463{
1464 if (level == SOL_UDP || level == SOL_UDPLITE)
1465 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1466 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1467}
1468#endif
1da177e4
LT
1469/**
1470 * udp_poll - wait for a UDP event.
1471 * @file - file struct
1472 * @sock - socket
1473 * @wait - poll table
1474 *
e905a9ed 1475 * This is same as datagram poll, except for the special case of
1da177e4
LT
1476 * blocking sockets. If application is using a blocking fd
1477 * and a packet with checksum error is in the queue;
1478 * then it could get return from select indicating data available
1479 * but then block when reading it. Add special case code
1480 * to work around these arguably broken applications.
1481 */
1482unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1483{
1484 unsigned int mask = datagram_poll(file, sock, wait);
1485 struct sock *sk = sock->sk;
ba4e58ec
GR
1486 int is_lite = IS_UDPLITE(sk);
1487
1da177e4
LT
1488 /* Check for false positives due to checksum errors */
1489 if ( (mask & POLLRDNORM) &&
1490 !(file->f_flags & O_NONBLOCK) &&
1491 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1492 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1493 struct sk_buff *skb;
1494
208d8984 1495 spin_lock_bh(&rcvq->lock);
759e5d00
HX
1496 while ((skb = skb_peek(rcvq)) != NULL &&
1497 udp_lib_checksum_complete(skb)) {
0283328e
PE
1498 UDP_INC_STATS_BH(sock_net(sk),
1499 UDP_MIB_INERRORS, is_lite);
759e5d00
HX
1500 __skb_unlink(skb, rcvq);
1501 kfree_skb(skb);
1da177e4 1502 }
208d8984 1503 spin_unlock_bh(&rcvq->lock);
1da177e4
LT
1504
1505 /* nothing to see, move along */
1506 if (skb == NULL)
1507 mask &= ~(POLLIN | POLLRDNORM);
1508 }
1509
1510 return mask;
e905a9ed 1511
1da177e4
LT
1512}
1513
db8dac20
DM
1514struct proto udp_prot = {
1515 .name = "UDP",
1516 .owner = THIS_MODULE,
1517 .close = udp_lib_close,
1518 .connect = ip4_datagram_connect,
1519 .disconnect = udp_disconnect,
1520 .ioctl = udp_ioctl,
1521 .destroy = udp_destroy_sock,
1522 .setsockopt = udp_setsockopt,
1523 .getsockopt = udp_getsockopt,
1524 .sendmsg = udp_sendmsg,
1525 .recvmsg = udp_recvmsg,
1526 .sendpage = udp_sendpage,
93821778 1527 .backlog_rcv = __udp_queue_rcv_skb,
db8dac20
DM
1528 .hash = udp_lib_hash,
1529 .unhash = udp_lib_unhash,
1530 .get_port = udp_v4_get_port,
1531 .memory_allocated = &udp_memory_allocated,
1532 .sysctl_mem = sysctl_udp_mem,
1533 .sysctl_wmem = &sysctl_udp_wmem_min,
1534 .sysctl_rmem = &sysctl_udp_rmem_min,
1535 .obj_size = sizeof(struct udp_sock),
271b72c7 1536 .slab_flags = SLAB_DESTROY_BY_RCU,
645ca708 1537 .h.udp_table = &udp_table,
db8dac20
DM
1538#ifdef CONFIG_COMPAT
1539 .compat_setsockopt = compat_udp_setsockopt,
1540 .compat_getsockopt = compat_udp_getsockopt,
1541#endif
db8dac20 1542};
1da177e4
LT
1543
1544/* ------------------------------------------------------------------------ */
1545#ifdef CONFIG_PROC_FS
1546
645ca708 1547static struct sock *udp_get_first(struct seq_file *seq, int start)
1da177e4
LT
1548{
1549 struct sock *sk;
1550 struct udp_iter_state *state = seq->private;
6f191efe 1551 struct net *net = seq_file_net(seq);
1da177e4 1552
645ca708 1553 for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1da177e4 1554 struct hlist_node *node;
645ca708
ED
1555 struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
1556 spin_lock_bh(&hslot->lock);
1557 sk_for_each(sk, node, &hslot->head) {
878628fb 1558 if (!net_eq(sock_net(sk), net))
a91275ef 1559 continue;
1da177e4
LT
1560 if (sk->sk_family == state->family)
1561 goto found;
1562 }
645ca708 1563 spin_unlock_bh(&hslot->lock);
1da177e4
LT
1564 }
1565 sk = NULL;
1566found:
1567 return sk;
1568}
1569
1570static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1571{
1572 struct udp_iter_state *state = seq->private;
6f191efe 1573 struct net *net = seq_file_net(seq);
1da177e4
LT
1574
1575 do {
1576 sk = sk_next(sk);
878628fb 1577 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
1da177e4 1578
645ca708
ED
1579 if (!sk) {
1580 spin_unlock(&state->udp_table->hash[state->bucket].lock);
1581 return udp_get_first(seq, state->bucket + 1);
1da177e4
LT
1582 }
1583 return sk;
1584}
1585
1586static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1587{
645ca708 1588 struct sock *sk = udp_get_first(seq, 0);
1da177e4
LT
1589
1590 if (sk)
6516c655 1591 while (pos && (sk = udp_get_next(seq, sk)) != NULL)
1da177e4
LT
1592 --pos;
1593 return pos ? NULL : sk;
1594}
1595
1596static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1597{
b50660f1 1598 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
1da177e4
LT
1599}
1600
1601static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1602{
1603 struct sock *sk;
1604
b50660f1 1605 if (v == SEQ_START_TOKEN)
1da177e4
LT
1606 sk = udp_get_idx(seq, 0);
1607 else
1608 sk = udp_get_next(seq, v);
1609
1610 ++*pos;
1611 return sk;
1612}
1613
1614static void udp_seq_stop(struct seq_file *seq, void *v)
1615{
645ca708
ED
1616 struct udp_iter_state *state = seq->private;
1617
1618 if (state->bucket < UDP_HTABLE_SIZE)
1619 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
1da177e4
LT
1620}
1621
1622static int udp_seq_open(struct inode *inode, struct file *file)
1623{
1624 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
a2be75c1
DL
1625 struct udp_iter_state *s;
1626 int err;
a91275ef 1627
a2be75c1
DL
1628 err = seq_open_net(inode, file, &afinfo->seq_ops,
1629 sizeof(struct udp_iter_state));
1630 if (err < 0)
1631 return err;
a91275ef 1632
a2be75c1 1633 s = ((struct seq_file *)file->private_data)->private;
1da177e4 1634 s->family = afinfo->family;
645ca708 1635 s->udp_table = afinfo->udp_table;
a2be75c1 1636 return err;
a91275ef
DL
1637}
1638
1da177e4 1639/* ------------------------------------------------------------------------ */
0c96d8c5 1640int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
1da177e4
LT
1641{
1642 struct proc_dir_entry *p;
1643 int rc = 0;
1644
3ba9441b
DL
1645 afinfo->seq_fops.open = udp_seq_open;
1646 afinfo->seq_fops.read = seq_read;
1647 afinfo->seq_fops.llseek = seq_lseek;
1648 afinfo->seq_fops.release = seq_release_net;
1da177e4 1649
dda61925
DL
1650 afinfo->seq_ops.start = udp_seq_start;
1651 afinfo->seq_ops.next = udp_seq_next;
1652 afinfo->seq_ops.stop = udp_seq_stop;
1653
84841c3c
DL
1654 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
1655 &afinfo->seq_fops, afinfo);
1656 if (!p)
1da177e4
LT
1657 rc = -ENOMEM;
1658 return rc;
1659}
1660
0c96d8c5 1661void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
1da177e4 1662{
0c96d8c5 1663 proc_net_remove(net, afinfo->name);
1da177e4 1664}
db8dac20
DM
1665
1666/* ------------------------------------------------------------------------ */
5e659e4c
PE
1667static void udp4_format_sock(struct sock *sp, struct seq_file *f,
1668 int bucket, int *len)
db8dac20
DM
1669{
1670 struct inet_sock *inet = inet_sk(sp);
1671 __be32 dest = inet->daddr;
1672 __be32 src = inet->rcv_saddr;
1673 __u16 destp = ntohs(inet->dport);
1674 __u16 srcp = ntohs(inet->sport);
1675
5e659e4c 1676 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
cb61cb9b 1677 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
db8dac20
DM
1678 bucket, src, srcp, dest, destp, sp->sk_state,
1679 atomic_read(&sp->sk_wmem_alloc),
1680 atomic_read(&sp->sk_rmem_alloc),
1681 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
cb61cb9b
ED
1682 atomic_read(&sp->sk_refcnt), sp,
1683 atomic_read(&sp->sk_drops), len);
db8dac20
DM
1684}
1685
1686int udp4_seq_show(struct seq_file *seq, void *v)
1687{
1688 if (v == SEQ_START_TOKEN)
1689 seq_printf(seq, "%-127s\n",
1690 " sl local_address rem_address st tx_queue "
1691 "rx_queue tr tm->when retrnsmt uid timeout "
cb61cb9b 1692 "inode ref pointer drops");
db8dac20 1693 else {
db8dac20 1694 struct udp_iter_state *state = seq->private;
5e659e4c 1695 int len;
db8dac20 1696
5e659e4c
PE
1697 udp4_format_sock(v, seq, state->bucket, &len);
1698 seq_printf(seq, "%*s\n", 127 - len ,"");
db8dac20
DM
1699 }
1700 return 0;
1701}
1702
1703/* ------------------------------------------------------------------------ */
db8dac20 1704static struct udp_seq_afinfo udp4_seq_afinfo = {
db8dac20
DM
1705 .name = "udp",
1706 .family = AF_INET,
645ca708 1707 .udp_table = &udp_table,
4ad96d39
DL
1708 .seq_fops = {
1709 .owner = THIS_MODULE,
1710 },
dda61925
DL
1711 .seq_ops = {
1712 .show = udp4_seq_show,
1713 },
db8dac20
DM
1714};
1715
15439feb
PE
1716static int udp4_proc_init_net(struct net *net)
1717{
1718 return udp_proc_register(net, &udp4_seq_afinfo);
1719}
1720
1721static void udp4_proc_exit_net(struct net *net)
1722{
1723 udp_proc_unregister(net, &udp4_seq_afinfo);
1724}
1725
1726static struct pernet_operations udp4_net_ops = {
1727 .init = udp4_proc_init_net,
1728 .exit = udp4_proc_exit_net,
1729};
1730
db8dac20
DM
1731int __init udp4_proc_init(void)
1732{
15439feb 1733 return register_pernet_subsys(&udp4_net_ops);
db8dac20
DM
1734}
1735
1736void udp4_proc_exit(void)
1737{
15439feb 1738 unregister_pernet_subsys(&udp4_net_ops);
db8dac20 1739}
1da177e4
LT
1740#endif /* CONFIG_PROC_FS */
1741
645ca708
ED
1742void __init udp_table_init(struct udp_table *table)
1743{
1744 int i;
1745
1746 for (i = 0; i < UDP_HTABLE_SIZE; i++) {
1747 INIT_HLIST_HEAD(&table->hash[i].head);
1748 spin_lock_init(&table->hash[i].lock);
1749 }
1750}
1751
95766fff
HA
1752void __init udp_init(void)
1753{
1754 unsigned long limit;
1755
645ca708 1756 udp_table_init(&udp_table);
95766fff
HA
1757 /* Set the pressure threshold up by the same strategy of TCP. It is a
1758 * fraction of global memory that is up to 1/2 at 256 MB, decreasing
1759 * toward zero with the amount of memory, with a floor of 128 pages.
1760 */
1761 limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
1762 limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
1763 limit = max(limit, 128UL);
1764 sysctl_udp_mem[0] = limit / 4 * 3;
1765 sysctl_udp_mem[1] = limit;
1766 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
1767
1768 sysctl_udp_rmem_min = SK_MEM_QUANTUM;
1769 sysctl_udp_wmem_min = SK_MEM_QUANTUM;
1770}
1771
1da177e4 1772EXPORT_SYMBOL(udp_disconnect);
1da177e4 1773EXPORT_SYMBOL(udp_ioctl);
db8dac20
DM
1774EXPORT_SYMBOL(udp_prot);
1775EXPORT_SYMBOL(udp_sendmsg);
4c0a6cb0
GR
1776EXPORT_SYMBOL(udp_lib_getsockopt);
1777EXPORT_SYMBOL(udp_lib_setsockopt);
1da177e4 1778EXPORT_SYMBOL(udp_poll);
6ba5a3c5 1779EXPORT_SYMBOL(udp_lib_get_port);
1da177e4
LT
1780
1781#ifdef CONFIG_PROC_FS
1782EXPORT_SYMBOL(udp_proc_register);
1783EXPORT_SYMBOL(udp_proc_unregister);
1784#endif