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