blob: 865d75214a9ab1d741f3d8351359e95a0d8394e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020089#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/in.h>
91#include <linux/errno.h>
92#include <linux/timer.h>
93#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <linux/inet.h>
95#include <linux/ipv6.h>
96#include <linux/netdevice.h>
97#include <net/snmp.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070098#include <net/ip.h>
99#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#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
Eric Dumazetba899662005-08-26 12:05:31 -0700116DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118struct hlist_head udp_hash[UDP_HTABLE_SIZE];
119DEFINE_RWLOCK(udp_hash_lock);
120
David S. Millerbed53ea2006-08-26 20:06:49 -0700121static int udp_port_rover;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Gerrit Renker25030a72006-08-26 20:06:05 -0700123static inline int udp_lport_inuse(u16 num)
124{
125 struct sock *sk;
126 struct hlist_node *node;
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,
David S. Millere3b4ead2006-08-26 20:10:15 -0700142 int (*saddr_cmp)(const struct sock *sk1, const struct sock *sk2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct hlist_node *node;
Gerrit Renker25030a72006-08-26 20:06:05 -0700145 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct sock *sk2;
Gerrit Renker25030a72006-08-26 20:06:05 -0700147 int error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
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++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int size;
160
Gerrit Renker25030a72006-08-26 20:06:05 -0700161 head = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
162 if (hlist_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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;
Gerrit Renker25030a72006-08-26 20:06:05 -0700170 sk_for_each(sk2, node, head)
171 if (++size < best_size_so_far) {
172 best_size_so_far = size;
173 best = result;
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
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 {
Gerrit Renker25030a72006-08-26 20:06:05 -0700190 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Gerrit Renker25030a72006-08-26 20:06:05 -0700192 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) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700201 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (sk_unhashed(sk)) {
Gerrit Renker25030a72006-08-26 20:06:05 -0700203 head = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
204 sk_add_node(sk, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 sock_prot_inc_use(sk->sk_prot);
206 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700207 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208fail:
209 write_unlock_bh(&udp_hash_lock);
Gerrit Renker25030a72006-08-26 20:06:05 -0700210 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
David S. Millere3b4ead2006-08-26 20:10:15 -0700213static inline int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
Gerrit Renker25030a72006-08-26 20:06:05 -0700214{
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);
225}
226
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static 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 */
Al Viro734ab872006-09-27 18:37:41 -0700246static struct sock *udp_v4_lookup_longway(__be32 saddr, __be16 sport,
247 __be32 daddr, __be16 dport, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
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
Al Viro734ab872006-09-27 18:37:41 -0700291static __inline__ struct sock *udp_v4_lookup(__be32 saddr, __be16 sport,
292 __be32 daddr, __be16 dport, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
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,
Al Viro734ab872006-09-27 18:37:41 -0700305 __be16 loc_port, __be32 loc_addr,
306 __be16 rmt_port, __be32 rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 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 */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700455 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 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 */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700474 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 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
Al Viro734ab872006-09-27 18:37:41 -0700501static unsigned short udp_check(struct udphdr *uh, int len, __be32 saddr, __be32 daddr, unsigned long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
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;
Al Viro3ca3c682006-09-27 18:28:07 -0700516 __be32 daddr, faddr, saddr;
Al Viro734ab872006-09-27 18:37:41 -0700517 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 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 } } };
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700629 security_sk_classify_flow(sk, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 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
Patrick McHardy64ce2072005-08-09 20:50:53 -0700656 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 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);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700678 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
679 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 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 }
Martin Bligh81aa6462006-08-14 23:57:10 -0700690 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 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
Patrick McHardy64ce2072005-08-09 20:50:53 -0700733 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 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;
Herbert Xu208d8982005-05-30 15:50:15 -0700778 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 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 }
Herbert Xu208d8982005-05-30 15:50:15 -0700788 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 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{
Herbert Xufb286bb2005-11-10 13:01:24 -0800800 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
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
Herbert Xu3305b802005-12-13 23:16:37 -0800885 skb_kill_datagram(sk, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
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);
931 struct udphdr *uh = skb->h.uh;
932 struct iphdr *iph;
933 int iphlen, len;
934
935 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
Al Viro734ab872006-09-27 18:37:41 -0700936 __be32 *udpdata32 = (__be32 *)udpdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 __u16 encap_type = up->encap_type;
938
939 /* if we're overly short, let UDP handle it */
940 if (udpdata > skb->tail)
941 return 1;
942
943 /* if this is not encapsulated socket, then just return now */
944 if (!encap_type)
945 return 1;
946
947 len = skb->tail - udpdata;
948
949 switch (encap_type) {
950 default:
951 case UDP_ENCAP_ESPINUDP:
952 /* Check if this is a keepalive packet. If so, eat it. */
953 if (len == 1 && udpdata[0] == 0xff) {
954 return 0;
955 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
956 /* ESP Packet without Non-ESP header */
957 len = sizeof(struct udphdr);
958 } else
959 /* Must be an IKE packet.. pass it through */
960 return 1;
961 break;
962 case UDP_ENCAP_ESPINUDP_NON_IKE:
963 /* Check if this is a keepalive packet. If so, eat it. */
964 if (len == 1 && udpdata[0] == 0xff) {
965 return 0;
966 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
967 udpdata32[0] == 0 && udpdata32[1] == 0) {
968
969 /* ESP Packet with Non-IKE marker */
970 len = sizeof(struct udphdr) + 2 * sizeof(u32);
971 } else
972 /* Must be an IKE packet.. pass it through */
973 return 1;
974 break;
975 }
976
977 /* At this point we are sure that this is an ESPinUDP packet,
978 * so we need to remove 'len' bytes from the packet (the UDP
979 * header and optional ESP marker bytes) and then modify the
980 * protocol to ESP, and then call into the transform receiver.
981 */
Herbert Xu4d78b6c2005-04-19 22:48:59 -0700982 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* Now we can update and verify the packet length... */
986 iph = skb->nh.iph;
987 iphlen = iph->ihl << 2;
988 iph->tot_len = htons(ntohs(iph->tot_len) - len);
989 if (skb->len < iphlen + len) {
990 /* packet is too small!?! */
991 return 0;
992 }
993
994 /* pull the data buffer up to the ESP header and set the
995 * transport header to point to ESP. Keep UDP on the stack
996 * for later.
997 */
998 skb->h.raw = skb_pull(skb, len);
999
1000 /* modify the protocol (it's ESP!) */
1001 iph->protocol = IPPROTO_ESP;
1002
1003 /* and let the caller know to send this into the ESP processor... */
1004 return -1;
1005#endif
1006}
1007
1008/* returns:
1009 * -1: error
1010 * 0: success
1011 * >0: "udp encap" protocol resubmission
1012 *
1013 * Note that in the success and error cases, the skb is assumed to
1014 * have either been requeued or freed.
1015 */
1016static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1017{
1018 struct udp_sock *up = udp_sk(sk);
Martin Bligh81aa6462006-08-14 23:57:10 -07001019 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /*
1022 * Charge it to the socket, dropping if the queue is full.
1023 */
1024 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1025 kfree_skb(skb);
1026 return -1;
1027 }
Patrick McHardyb59c2702006-01-06 23:06:10 -08001028 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (up->encap_type) {
1031 /*
1032 * This is an encapsulation socket, so let's see if this is
1033 * an encapsulated packet.
1034 * If it's a keepalive packet, then just eat it.
1035 * If it's an encapsulateed packet, then pass it to the
1036 * IPsec xfrm input and return the response
1037 * appropriately. Otherwise, just fall through and
1038 * pass this up the UDP socket.
1039 */
1040 int ret;
1041
1042 ret = udp_encap_rcv(sk, skb);
1043 if (ret == 0) {
1044 /* Eat the packet .. */
1045 kfree_skb(skb);
1046 return 0;
1047 }
1048 if (ret < 0) {
1049 /* process the ESP packet */
1050 ret = xfrm4_rcv_encap(skb, up->encap_type);
1051 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1052 return -ret;
1053 }
1054 /* FALLTHROUGH -- it's a UDP Packet */
1055 }
1056
1057 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1058 if (__udp_checksum_complete(skb)) {
1059 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1060 kfree_skb(skb);
1061 return -1;
1062 }
1063 skb->ip_summed = CHECKSUM_UNNECESSARY;
1064 }
1065
Martin Bligh81aa6462006-08-14 23:57:10 -07001066 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
1067 /* Note that an ENOMEM error is charged twice */
1068 if (rc == -ENOMEM)
1069 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1071 kfree_skb(skb);
1072 return -1;
1073 }
1074 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
1075 return 0;
1076}
1077
1078/*
1079 * Multicasts and broadcasts go to each listener.
1080 *
1081 * Note: called only from the BH handler context,
1082 * so we don't need to lock the hashes.
1083 */
1084static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
Al Viro734ab872006-09-27 18:37:41 -07001085 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct sock *sk;
1088 int dif;
1089
1090 read_lock(&udp_hash_lock);
1091 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1092 dif = skb->dev->ifindex;
1093 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1094 if (sk) {
1095 struct sock *sknext = NULL;
1096
1097 do {
1098 struct sk_buff *skb1 = skb;
1099
1100 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1101 uh->source, saddr, dif);
1102 if(sknext)
1103 skb1 = skb_clone(skb, GFP_ATOMIC);
1104
1105 if(skb1) {
1106 int ret = udp_queue_rcv_skb(sk, skb1);
1107 if (ret > 0)
1108 /* we should probably re-process instead
1109 * of dropping packets here. */
1110 kfree_skb(skb1);
1111 }
1112 sk = sknext;
1113 } while(sknext);
1114 } else
1115 kfree_skb(skb);
1116 read_unlock(&udp_hash_lock);
1117 return 0;
1118}
1119
1120/* Initialize UDP checksum. If exited with zero value (success),
1121 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1122 * Otherwise, csum completion requires chacksumming packet body,
1123 * including udp header and folding it to skb->csum.
1124 */
Stephen Hemminger65a45442005-12-13 23:17:02 -08001125static void udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
Al Viro734ab872006-09-27 18:37:41 -07001126 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 if (uh->check == 0) {
1129 skb->ip_summed = CHECKSUM_UNNECESSARY;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001130 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -08001132 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1135 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1136 /* Probably, we should checksum udp header (it should be in cache
1137 * in any case) and data in tiny packets (< rx copybreak).
1138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/*
1142 * All we need to do is get the socket, and then do a checksum.
1143 */
1144
1145int udp_rcv(struct sk_buff *skb)
1146{
1147 struct sock *sk;
1148 struct udphdr *uh;
1149 unsigned short ulen;
1150 struct rtable *rt = (struct rtable*)skb->dst;
Al Viro734ab872006-09-27 18:37:41 -07001151 __be32 saddr = skb->nh.iph->saddr;
1152 __be32 daddr = skb->nh.iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 int len = skb->len;
1154
1155 /*
1156 * Validate the packet and the UDP length.
1157 */
1158 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1159 goto no_header;
1160
1161 uh = skb->h.uh;
1162
1163 ulen = ntohs(uh->len);
1164
1165 if (ulen > len || ulen < sizeof(*uh))
1166 goto short_packet;
1167
Stephen Hemmingere308e252005-09-08 12:32:21 -07001168 if (pskb_trim_rcsum(skb, ulen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto short_packet;
1170
Stephen Hemminger65a45442005-12-13 23:17:02 -08001171 udp_checksum_init(skb, uh, ulen, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1174 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1175
1176 sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1177
1178 if (sk != NULL) {
1179 int ret = udp_queue_rcv_skb(sk, skb);
1180 sock_put(sk);
1181
1182 /* a return value > 0 means to resubmit the input, but
1183 * it it wants the return to be -protocol, or 0
1184 */
1185 if (ret > 0)
1186 return -ret;
1187 return 0;
1188 }
1189
1190 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1191 goto drop;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001192 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* No socket. Drop packet silently, if checksum is wrong */
1195 if (udp_checksum_complete(skb))
1196 goto csum_error;
1197
1198 UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
1199 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1200
1201 /*
1202 * Hmm. We got an UDP packet to a port to which we
1203 * don't wanna listen. Ignore it.
1204 */
1205 kfree_skb(skb);
1206 return(0);
1207
1208short_packet:
Patrick McHardy64ce2072005-08-09 20:50:53 -07001209 LIMIT_NETDEBUG(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1210 NIPQUAD(saddr),
1211 ntohs(uh->source),
1212 ulen,
1213 len,
1214 NIPQUAD(daddr),
1215 ntohs(uh->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216no_header:
1217 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1218 kfree_skb(skb);
1219 return(0);
1220
1221csum_error:
1222 /*
1223 * RFC1122: OK. Discards the bad packet silently (as far as
1224 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1225 */
Patrick McHardy64ce2072005-08-09 20:50:53 -07001226 LIMIT_NETDEBUG(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1227 NIPQUAD(saddr),
1228 ntohs(uh->source),
1229 NIPQUAD(daddr),
1230 ntohs(uh->dest),
1231 ulen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232drop:
1233 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1234 kfree_skb(skb);
1235 return(0);
1236}
1237
1238static int udp_destroy_sock(struct sock *sk)
1239{
1240 lock_sock(sk);
1241 udp_flush_pending_frames(sk);
1242 release_sock(sk);
1243 return 0;
1244}
1245
1246/*
1247 * Socket option code for UDP
1248 */
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001249static int do_udp_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 char __user *optval, int optlen)
1251{
1252 struct udp_sock *up = udp_sk(sk);
1253 int val;
1254 int err = 0;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if(optlen<sizeof(int))
1257 return -EINVAL;
1258
1259 if (get_user(val, (int __user *)optval))
1260 return -EFAULT;
1261
1262 switch(optname) {
1263 case UDP_CORK:
1264 if (val != 0) {
1265 up->corkflag = 1;
1266 } else {
1267 up->corkflag = 0;
1268 lock_sock(sk);
1269 udp_push_pending_frames(sk, up);
1270 release_sock(sk);
1271 }
1272 break;
1273
1274 case UDP_ENCAP:
1275 switch (val) {
1276 case 0:
1277 case UDP_ENCAP_ESPINUDP:
1278 case UDP_ENCAP_ESPINUDP_NON_IKE:
1279 up->encap_type = val;
1280 break;
1281 default:
1282 err = -ENOPROTOOPT;
1283 break;
1284 }
1285 break;
1286
1287 default:
1288 err = -ENOPROTOOPT;
1289 break;
1290 };
1291
1292 return err;
1293}
1294
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001295static int udp_setsockopt(struct sock *sk, int level, int optname,
1296 char __user *optval, int optlen)
1297{
1298 if (level != SOL_UDP)
1299 return ip_setsockopt(sk, level, optname, optval, optlen);
1300 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1301}
1302
1303#ifdef CONFIG_COMPAT
1304static int compat_udp_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001305 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001306{
1307 if (level != SOL_UDP)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001308 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001309 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1310}
1311#endif
1312
1313static int do_udp_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 char __user *optval, int __user *optlen)
1315{
1316 struct udp_sock *up = udp_sk(sk);
1317 int val, len;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if(get_user(len,optlen))
1320 return -EFAULT;
1321
1322 len = min_t(unsigned int, len, sizeof(int));
1323
1324 if(len < 0)
1325 return -EINVAL;
1326
1327 switch(optname) {
1328 case UDP_CORK:
1329 val = up->corkflag;
1330 break;
1331
1332 case UDP_ENCAP:
1333 val = up->encap_type;
1334 break;
1335
1336 default:
1337 return -ENOPROTOOPT;
1338 };
1339
1340 if(put_user(len, optlen))
1341 return -EFAULT;
1342 if(copy_to_user(optval, &val,len))
1343 return -EFAULT;
1344 return 0;
1345}
1346
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001347static int udp_getsockopt(struct sock *sk, int level, int optname,
1348 char __user *optval, int __user *optlen)
1349{
1350 if (level != SOL_UDP)
1351 return ip_getsockopt(sk, level, optname, optval, optlen);
1352 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1353}
1354
1355#ifdef CONFIG_COMPAT
1356static int compat_udp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001357 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001358{
1359 if (level != SOL_UDP)
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001360 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001361 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1362}
1363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/**
1365 * udp_poll - wait for a UDP event.
1366 * @file - file struct
1367 * @sock - socket
1368 * @wait - poll table
1369 *
1370 * This is same as datagram poll, except for the special case of
1371 * blocking sockets. If application is using a blocking fd
1372 * and a packet with checksum error is in the queue;
1373 * then it could get return from select indicating data available
1374 * but then block when reading it. Add special case code
1375 * to work around these arguably broken applications.
1376 */
1377unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1378{
1379 unsigned int mask = datagram_poll(file, sock, wait);
1380 struct sock *sk = sock->sk;
1381
1382 /* Check for false positives due to checksum errors */
1383 if ( (mask & POLLRDNORM) &&
1384 !(file->f_flags & O_NONBLOCK) &&
1385 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1386 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1387 struct sk_buff *skb;
1388
Herbert Xu208d8982005-05-30 15:50:15 -07001389 spin_lock_bh(&rcvq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 while ((skb = skb_peek(rcvq)) != NULL) {
1391 if (udp_checksum_complete(skb)) {
1392 UDP_INC_STATS_BH(UDP_MIB_INERRORS);
1393 __skb_unlink(skb, rcvq);
1394 kfree_skb(skb);
1395 } else {
1396 skb->ip_summed = CHECKSUM_UNNECESSARY;
1397 break;
1398 }
1399 }
Herbert Xu208d8982005-05-30 15:50:15 -07001400 spin_unlock_bh(&rcvq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /* nothing to see, move along */
1403 if (skb == NULL)
1404 mask &= ~(POLLIN | POLLRDNORM);
1405 }
1406
1407 return mask;
1408
1409}
1410
1411struct proto udp_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001412 .name = "UDP",
1413 .owner = THIS_MODULE,
1414 .close = udp_close,
1415 .connect = ip4_datagram_connect,
1416 .disconnect = udp_disconnect,
1417 .ioctl = udp_ioctl,
1418 .destroy = udp_destroy_sock,
1419 .setsockopt = udp_setsockopt,
1420 .getsockopt = udp_getsockopt,
1421 .sendmsg = udp_sendmsg,
1422 .recvmsg = udp_recvmsg,
1423 .sendpage = udp_sendpage,
1424 .backlog_rcv = udp_queue_rcv_skb,
1425 .hash = udp_v4_hash,
1426 .unhash = udp_v4_unhash,
1427 .get_port = udp_v4_get_port,
1428 .obj_size = sizeof(struct udp_sock),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001429#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001430 .compat_setsockopt = compat_udp_setsockopt,
1431 .compat_getsockopt = compat_udp_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433};
1434
1435/* ------------------------------------------------------------------------ */
1436#ifdef CONFIG_PROC_FS
1437
1438static struct sock *udp_get_first(struct seq_file *seq)
1439{
1440 struct sock *sk;
1441 struct udp_iter_state *state = seq->private;
1442
1443 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1444 struct hlist_node *node;
1445 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1446 if (sk->sk_family == state->family)
1447 goto found;
1448 }
1449 }
1450 sk = NULL;
1451found:
1452 return sk;
1453}
1454
1455static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1456{
1457 struct udp_iter_state *state = seq->private;
1458
1459 do {
1460 sk = sk_next(sk);
1461try_again:
1462 ;
1463 } while (sk && sk->sk_family != state->family);
1464
1465 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1466 sk = sk_head(&udp_hash[state->bucket]);
1467 goto try_again;
1468 }
1469 return sk;
1470}
1471
1472static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1473{
1474 struct sock *sk = udp_get_first(seq);
1475
1476 if (sk)
1477 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1478 --pos;
1479 return pos ? NULL : sk;
1480}
1481
1482static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1483{
1484 read_lock(&udp_hash_lock);
1485 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1486}
1487
1488static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1489{
1490 struct sock *sk;
1491
1492 if (v == (void *)1)
1493 sk = udp_get_idx(seq, 0);
1494 else
1495 sk = udp_get_next(seq, v);
1496
1497 ++*pos;
1498 return sk;
1499}
1500
1501static void udp_seq_stop(struct seq_file *seq, void *v)
1502{
1503 read_unlock(&udp_hash_lock);
1504}
1505
1506static int udp_seq_open(struct inode *inode, struct file *file)
1507{
1508 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1509 struct seq_file *seq;
1510 int rc = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001511 struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (!s)
1514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 s->family = afinfo->family;
1516 s->seq_ops.start = udp_seq_start;
1517 s->seq_ops.next = udp_seq_next;
1518 s->seq_ops.show = afinfo->seq_show;
1519 s->seq_ops.stop = udp_seq_stop;
1520
1521 rc = seq_open(file, &s->seq_ops);
1522 if (rc)
1523 goto out_kfree;
1524
1525 seq = file->private_data;
1526 seq->private = s;
1527out:
1528 return rc;
1529out_kfree:
1530 kfree(s);
1531 goto out;
1532}
1533
1534/* ------------------------------------------------------------------------ */
1535int udp_proc_register(struct udp_seq_afinfo *afinfo)
1536{
1537 struct proc_dir_entry *p;
1538 int rc = 0;
1539
1540 if (!afinfo)
1541 return -EINVAL;
1542 afinfo->seq_fops->owner = afinfo->owner;
1543 afinfo->seq_fops->open = udp_seq_open;
1544 afinfo->seq_fops->read = seq_read;
1545 afinfo->seq_fops->llseek = seq_lseek;
1546 afinfo->seq_fops->release = seq_release_private;
1547
1548 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1549 if (p)
1550 p->data = afinfo;
1551 else
1552 rc = -ENOMEM;
1553 return rc;
1554}
1555
1556void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1557{
1558 if (!afinfo)
1559 return;
1560 proc_net_remove(afinfo->name);
1561 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1562}
1563
1564/* ------------------------------------------------------------------------ */
1565static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1566{
1567 struct inet_sock *inet = inet_sk(sp);
Al Viro734ab872006-09-27 18:37:41 -07001568 __be32 dest = inet->daddr;
1569 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 __u16 destp = ntohs(inet->dport);
1571 __u16 srcp = ntohs(inet->sport);
1572
1573 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1574 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1575 bucket, src, srcp, dest, destp, sp->sk_state,
1576 atomic_read(&sp->sk_wmem_alloc),
1577 atomic_read(&sp->sk_rmem_alloc),
1578 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1579 atomic_read(&sp->sk_refcnt), sp);
1580}
1581
1582static int udp4_seq_show(struct seq_file *seq, void *v)
1583{
1584 if (v == SEQ_START_TOKEN)
1585 seq_printf(seq, "%-127s\n",
1586 " sl local_address rem_address st tx_queue "
1587 "rx_queue tr tm->when retrnsmt uid timeout "
1588 "inode");
1589 else {
1590 char tmpbuf[129];
1591 struct udp_iter_state *state = seq->private;
1592
1593 udp4_format_sock(v, tmpbuf, state->bucket);
1594 seq_printf(seq, "%-127s\n", tmpbuf);
1595 }
1596 return 0;
1597}
1598
1599/* ------------------------------------------------------------------------ */
1600static struct file_operations udp4_seq_fops;
1601static struct udp_seq_afinfo udp4_seq_afinfo = {
1602 .owner = THIS_MODULE,
1603 .name = "udp",
1604 .family = AF_INET,
1605 .seq_show = udp4_seq_show,
1606 .seq_fops = &udp4_seq_fops,
1607};
1608
1609int __init udp4_proc_init(void)
1610{
1611 return udp_proc_register(&udp4_seq_afinfo);
1612}
1613
1614void udp4_proc_exit(void)
1615{
1616 udp_proc_unregister(&udp4_seq_afinfo);
1617}
1618#endif /* CONFIG_PROC_FS */
1619
1620EXPORT_SYMBOL(udp_disconnect);
1621EXPORT_SYMBOL(udp_hash);
1622EXPORT_SYMBOL(udp_hash_lock);
1623EXPORT_SYMBOL(udp_ioctl);
Gerrit Renker25030a72006-08-26 20:06:05 -07001624EXPORT_SYMBOL(udp_get_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625EXPORT_SYMBOL(udp_prot);
1626EXPORT_SYMBOL(udp_sendmsg);
1627EXPORT_SYMBOL(udp_poll);
1628
1629#ifdef CONFIG_PROC_FS
1630EXPORT_SYMBOL(udp_proc_register);
1631EXPORT_SYMBOL(udp_proc_unregister);
1632#endif