blob: 1807a30694d98f2d8e080580717fc73fdf794a7a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/netdevice.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070096#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/skbuff.h>
98#include <linux/proc_fs.h>
99#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <net/icmp.h>
101#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/checksum.h>
103#include <net/xfrm.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800104#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * Snmp MIB for the UDP layer
108 */
109
Eric Dumazetba899662005-08-26 12:05:31 -0700110DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112struct hlist_head udp_hash[UDP_HTABLE_SIZE];
113DEFINE_RWLOCK(udp_hash_lock);
114
David S. Millerbed53ea2006-08-26 20:06:49 -0700115static int udp_port_rover;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Al Viro8e5200f2006-11-20 18:06:37 -0800117static inline int __udp_lib_lport_inuse(__u16 num, struct hlist_head udptable[])
Gerrit Renker25030a72006-08-26 20:06:05 -0700118{
119 struct sock *sk;
120 struct hlist_node *node;
121
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800122 sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)])
Gerrit Renker25030a72006-08-26 20:06:05 -0700123 if (inet_sk(sk)->num == num)
124 return 1;
125 return 0;
126}
127
128/**
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800129 * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
Gerrit Renker25030a72006-08-26 20:06:05 -0700130 *
131 * @sk: socket struct in question
132 * @snum: port number to look up
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800133 * @udptable: hash list table, must be of UDP_HTABLE_SIZE
134 * @port_rover: pointer to record of last unallocated port
Gerrit Renker25030a72006-08-26 20:06:05 -0700135 * @saddr_comp: AF-dependent comparison of bound local IP addresses
136 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800137int __udp_lib_get_port(struct sock *sk, unsigned short snum,
138 struct hlist_head udptable[], int *port_rover,
139 int (*saddr_comp)(const struct sock *sk1,
140 const struct sock *sk2 ) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct hlist_node *node;
Gerrit Renker25030a72006-08-26 20:06:05 -0700143 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct sock *sk2;
Gerrit Renker25030a72006-08-26 20:06:05 -0700145 int error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 write_lock_bh(&udp_hash_lock);
148 if (snum == 0) {
149 int best_size_so_far, best, result, i;
150
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800151 if (*port_rover > sysctl_local_port_range[1] ||
152 *port_rover < sysctl_local_port_range[0])
153 *port_rover = sysctl_local_port_range[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 best_size_so_far = 32767;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800155 best = result = *port_rover;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 int size;
158
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800159 head = &udptable[result & (UDP_HTABLE_SIZE - 1)];
Gerrit Renker25030a72006-08-26 20:06:05 -0700160 if (hlist_empty(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (result > sysctl_local_port_range[1])
162 result = sysctl_local_port_range[0] +
163 ((result - sysctl_local_port_range[0]) &
164 (UDP_HTABLE_SIZE - 1));
165 goto gotit;
166 }
167 size = 0;
Gerrit Renker25030a72006-08-26 20:06:05 -0700168 sk_for_each(sk2, node, head)
169 if (++size < best_size_so_far) {
170 best_size_so_far = size;
171 best = result;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 result = best;
175 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
176 if (result > sysctl_local_port_range[1])
177 result = sysctl_local_port_range[0]
178 + ((result - sysctl_local_port_range[0]) &
179 (UDP_HTABLE_SIZE - 1));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800180 if (! __udp_lib_lport_inuse(result, udptable))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 }
183 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
184 goto fail;
185gotit:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800186 *port_rover = snum = result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } else {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800188 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Gerrit Renker25030a72006-08-26 20:06:05 -0700190 sk_for_each(sk2, node, head)
191 if (inet_sk(sk2)->num == snum &&
192 sk2 != sk &&
193 (!sk2->sk_reuse || !sk->sk_reuse) &&
194 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
195 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800196 (*saddr_comp)(sk, sk2) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700199 inet_sk(sk)->num = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (sk_unhashed(sk)) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800201 head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
Gerrit Renker25030a72006-08-26 20:06:05 -0700202 sk_add_node(sk, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 sock_prot_inc_use(sk->sk_prot);
204 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700205 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206fail:
207 write_unlock_bh(&udp_hash_lock);
Gerrit Renker25030a72006-08-26 20:06:05 -0700208 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800211__inline__ int udp_get_port(struct sock *sk, unsigned short snum,
212 int (*scmp)(const struct sock *, const struct sock *))
213{
214 return __udp_lib_get_port(sk, snum, udp_hash, &udp_port_rover, scmp);
215}
216
217inline int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
Gerrit Renker25030a72006-08-26 20:06:05 -0700218{
219 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
220
221 return ( !ipv6_only_sock(sk2) &&
222 (!inet1->rcv_saddr || !inet2->rcv_saddr ||
223 inet1->rcv_saddr == inet2->rcv_saddr ));
224}
225
226static inline int udp_v4_get_port(struct sock *sk, unsigned short snum)
227{
228 return udp_get_port(sk, snum, ipv4_rcv_saddr_equal);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
232 * harder than this. -DaveM
233 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800234static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport,
235 __be32 daddr, __be16 dport,
236 int dif, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct sock *sk, *result = NULL;
239 struct hlist_node *node;
240 unsigned short hnum = ntohs(dport);
241 int badness = -1;
242
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800243 read_lock(&udp_hash_lock);
244 sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct inet_sock *inet = inet_sk(sk);
246
247 if (inet->num == hnum && !ipv6_only_sock(sk)) {
248 int score = (sk->sk_family == PF_INET ? 1 : 0);
249 if (inet->rcv_saddr) {
250 if (inet->rcv_saddr != daddr)
251 continue;
252 score+=2;
253 }
254 if (inet->daddr) {
255 if (inet->daddr != saddr)
256 continue;
257 score+=2;
258 }
259 if (inet->dport) {
260 if (inet->dport != sport)
261 continue;
262 score+=2;
263 }
264 if (sk->sk_bound_dev_if) {
265 if (sk->sk_bound_dev_if != dif)
266 continue;
267 score+=2;
268 }
269 if(score == 9) {
270 result = sk;
271 break;
272 } else if(score > badness) {
273 result = sk;
274 badness = score;
275 }
276 }
277 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800278 if (result)
279 sock_hold(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 read_unlock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800281 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static inline struct sock *udp_v4_mcast_next(struct sock *sk,
Al Viro734ab872006-09-27 18:37:41 -0700285 __be16 loc_port, __be32 loc_addr,
286 __be16 rmt_port, __be32 rmt_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int dif)
288{
289 struct hlist_node *node;
290 struct sock *s = sk;
291 unsigned short hnum = ntohs(loc_port);
292
293 sk_for_each_from(s, node) {
294 struct inet_sock *inet = inet_sk(s);
295
296 if (inet->num != hnum ||
297 (inet->daddr && inet->daddr != rmt_addr) ||
298 (inet->dport != rmt_port && inet->dport) ||
299 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
300 ipv6_only_sock(s) ||
301 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
302 continue;
303 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
304 continue;
305 goto found;
306 }
307 s = NULL;
308found:
309 return s;
310}
311
312/*
313 * This routine is called by the ICMP module when it gets some
314 * sort of error condition. If err < 0 then the socket should
315 * be closed and the error returned to the user. If err > 0
316 * it's just the icmp type << 8 | icmp code.
317 * Header points to the ip header of the error packet. We move
318 * on past this. Then (as it used to claim before adjustment)
319 * header points to the first 8 bytes of the udp header. We need
320 * to find the appropriate port.
321 */
322
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800323void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct inet_sock *inet;
326 struct iphdr *iph = (struct iphdr*)skb->data;
327 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
328 int type = skb->h.icmph->type;
329 int code = skb->h.icmph->code;
330 struct sock *sk;
331 int harderr;
332 int err;
333
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800334 sk = __udp4_lib_lookup(iph->daddr, uh->dest, iph->saddr, uh->source,
335 skb->dev->ifindex, udptable );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (sk == NULL) {
337 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
338 return; /* No socket for error */
339 }
340
341 err = 0;
342 harderr = 0;
343 inet = inet_sk(sk);
344
345 switch (type) {
346 default:
347 case ICMP_TIME_EXCEEDED:
348 err = EHOSTUNREACH;
349 break;
350 case ICMP_SOURCE_QUENCH:
351 goto out;
352 case ICMP_PARAMETERPROB:
353 err = EPROTO;
354 harderr = 1;
355 break;
356 case ICMP_DEST_UNREACH:
357 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
358 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
359 err = EMSGSIZE;
360 harderr = 1;
361 break;
362 }
363 goto out;
364 }
365 err = EHOSTUNREACH;
366 if (code <= NR_ICMP_UNREACH) {
367 harderr = icmp_err_convert[code].fatal;
368 err = icmp_err_convert[code].errno;
369 }
370 break;
371 }
372
373 /*
374 * RFC1122: OK. Passes ICMP errors back to application, as per
375 * 4.1.3.3.
376 */
377 if (!inet->recverr) {
378 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
379 goto out;
380 } else {
381 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
382 }
383 sk->sk_err = err;
384 sk->sk_error_report(sk);
385out:
386 sock_put(sk);
387}
388
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800389__inline__ void udp_err(struct sk_buff *skb, u32 info)
390{
391 return __udp4_lib_err(skb, info, udp_hash);
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * Throw away all pending data and cancel the corking. Socket is locked.
396 */
397static void udp_flush_pending_frames(struct sock *sk)
398{
399 struct udp_sock *up = udp_sk(sk);
400
401 if (up->pending) {
402 up->len = 0;
403 up->pending = 0;
404 ip_flush_pending_frames(sk);
405 }
406}
407
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800408/**
409 * udp4_hwcsum_outgoing - handle outgoing HW checksumming
410 * @sk: socket we are sending on
411 * @skb: sk_buff containing the filled-in UDP header
412 * (checksum field must be zeroed out)
413 */
414static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
415 __be32 src, __be32 dst, int len )
416{
Al Viro6b116872006-11-14 21:28:24 -0800417 unsigned int offset;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800418 struct udphdr *uh = skb->h.uh;
Al Viro6b116872006-11-14 21:28:24 -0800419 __wsum csum = 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800420
421 if (skb_queue_len(&sk->sk_write_queue) == 1) {
422 /*
423 * Only one fragment on the socket.
424 */
Al Viroff1dcad2006-11-20 18:07:29 -0800425 skb->csum_offset = offsetof(struct udphdr, check);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800426 uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
427 } else {
428 /*
429 * HW-checksum won't work as there are two or more
430 * fragments on the socket so that all csums of sk_buffs
431 * should be together
432 */
433 offset = skb->h.raw - skb->data;
434 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
435
436 skb->ip_summed = CHECKSUM_NONE;
437
438 skb_queue_walk(&sk->sk_write_queue, skb) {
439 csum = csum_add(csum, skb->csum);
440 }
441
442 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
443 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800444 uh->check = CSUM_MANGLED_0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800445 }
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
449 * Push out all pending data as one UDP datagram. Socket is locked.
450 */
Gerrit Renker078250d2006-11-15 00:44:49 -0800451static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct inet_sock *inet = inet_sk(sk);
454 struct flowi *fl = &inet->cork.fl;
455 struct sk_buff *skb;
456 struct udphdr *uh;
457 int err = 0;
Al Viro8e5200f2006-11-20 18:06:37 -0800458 __wsum csum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* Grab the skbuff where UDP header space exists. */
461 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
462 goto out;
463
464 /*
465 * Create a UDP header
466 */
467 uh = skb->h.uh;
468 uh->source = fl->fl_ip_sport;
469 uh->dest = fl->fl_ip_dport;
470 uh->len = htons(up->len);
471 uh->check = 0;
472
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800473 if (up->pcflag) /* UDP-Lite */
474 csum = udplite_csum_outgoing(sk, skb);
475
476 else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 skb->ip_summed = CHECKSUM_NONE;
479 goto send;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800481 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800483 udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len);
484 goto send;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800486 } else /* `normal' UDP */
487 csum = udp_csum_outgoing(sk, skb);
488
489 /* add protocol-dependent pseudo-header */
490 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
491 sk->sk_protocol, csum );
492 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -0800493 uh->check = CSUM_MANGLED_0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495send:
496 err = ip_push_pending_frames(sk);
497out:
498 up->len = 0;
499 up->pending = 0;
500 return err;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
504 size_t len)
505{
506 struct inet_sock *inet = inet_sk(sk);
507 struct udp_sock *up = udp_sk(sk);
508 int ulen = len;
509 struct ipcm_cookie ipc;
510 struct rtable *rt = NULL;
511 int free = 0;
512 int connected = 0;
Al Viro3ca3c682006-09-27 18:28:07 -0700513 __be32 daddr, faddr, saddr;
Al Viro734ab872006-09-27 18:37:41 -0700514 __be16 dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 u8 tos;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800516 int err, is_udplite = up->pcflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800518 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (len > 0xFFFF)
521 return -EMSGSIZE;
522
523 /*
524 * Check the flags.
525 */
526
527 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
528 return -EOPNOTSUPP;
529
530 ipc.opt = NULL;
531
532 if (up->pending) {
533 /*
534 * There are pending frames.
535 * The socket lock must be held while it's corked.
536 */
537 lock_sock(sk);
538 if (likely(up->pending)) {
539 if (unlikely(up->pending != AF_INET)) {
540 release_sock(sk);
541 return -EINVAL;
542 }
543 goto do_append_data;
544 }
545 release_sock(sk);
546 }
547 ulen += sizeof(struct udphdr);
548
549 /*
550 * Get and verify the address.
551 */
552 if (msg->msg_name) {
553 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
554 if (msg->msg_namelen < sizeof(*usin))
555 return -EINVAL;
556 if (usin->sin_family != AF_INET) {
557 if (usin->sin_family != AF_UNSPEC)
558 return -EAFNOSUPPORT;
559 }
560
561 daddr = usin->sin_addr.s_addr;
562 dport = usin->sin_port;
563 if (dport == 0)
564 return -EINVAL;
565 } else {
566 if (sk->sk_state != TCP_ESTABLISHED)
567 return -EDESTADDRREQ;
568 daddr = inet->daddr;
569 dport = inet->dport;
570 /* Open fast path for connected socket.
571 Route will not be used, if at least one option is set.
572 */
573 connected = 1;
574 }
575 ipc.addr = inet->saddr;
576
577 ipc.oif = sk->sk_bound_dev_if;
578 if (msg->msg_controllen) {
579 err = ip_cmsg_send(msg, &ipc);
580 if (err)
581 return err;
582 if (ipc.opt)
583 free = 1;
584 connected = 0;
585 }
586 if (!ipc.opt)
587 ipc.opt = inet->opt;
588
589 saddr = ipc.addr;
590 ipc.addr = faddr = daddr;
591
592 if (ipc.opt && ipc.opt->srr) {
593 if (!daddr)
594 return -EINVAL;
595 faddr = ipc.opt->faddr;
596 connected = 0;
597 }
598 tos = RT_TOS(inet->tos);
599 if (sock_flag(sk, SOCK_LOCALROUTE) ||
600 (msg->msg_flags & MSG_DONTROUTE) ||
601 (ipc.opt && ipc.opt->is_strictroute)) {
602 tos |= RTO_ONLINK;
603 connected = 0;
604 }
605
606 if (MULTICAST(daddr)) {
607 if (!ipc.oif)
608 ipc.oif = inet->mc_index;
609 if (!saddr)
610 saddr = inet->mc_addr;
611 connected = 0;
612 }
613
614 if (connected)
615 rt = (struct rtable*)sk_dst_check(sk, 0);
616
617 if (rt == NULL) {
618 struct flowi fl = { .oif = ipc.oif,
619 .nl_u = { .ip4_u =
620 { .daddr = faddr,
621 .saddr = saddr,
622 .tos = tos } },
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800623 .proto = sk->sk_protocol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 .uli_u = { .ports =
625 { .sport = inet->sport,
626 .dport = dport } } };
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700627 security_sk_classify_flow(sk, &fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
629 if (err)
630 goto out;
631
632 err = -EACCES;
633 if ((rt->rt_flags & RTCF_BROADCAST) &&
634 !sock_flag(sk, SOCK_BROADCAST))
635 goto out;
636 if (connected)
637 sk_dst_set(sk, dst_clone(&rt->u.dst));
638 }
639
640 if (msg->msg_flags&MSG_CONFIRM)
641 goto do_confirm;
642back_from_confirm:
643
644 saddr = rt->rt_src;
645 if (!ipc.addr)
646 daddr = ipc.addr = rt->rt_dst;
647
648 lock_sock(sk);
649 if (unlikely(up->pending)) {
650 /* The socket is already corked while preparing it. */
651 /* ... which is an evident application bug. --ANK */
652 release_sock(sk);
653
Patrick McHardy64ce2072005-08-09 20:50:53 -0700654 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 err = -EINVAL;
656 goto out;
657 }
658 /*
659 * Now cork the socket to pend data.
660 */
661 inet->cork.fl.fl4_dst = daddr;
662 inet->cork.fl.fl_ip_dport = dport;
663 inet->cork.fl.fl4_src = saddr;
664 inet->cork.fl.fl_ip_sport = inet->sport;
665 up->pending = AF_INET;
666
667do_append_data:
668 up->len += ulen;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800669 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
670 err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
671 sizeof(struct udphdr), &ipc, rt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
673 if (err)
674 udp_flush_pending_frames(sk);
675 else if (!corkreq)
676 err = udp_push_pending_frames(sk, up);
Herbert Xu1e0c14f2006-10-03 14:35:49 -0700677 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
678 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 release_sock(sk);
680
681out:
682 ip_rt_put(rt);
683 if (free)
684 kfree(ipc.opt);
685 if (!err) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800686 UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return len;
688 }
Martin Bligh81aa6462006-08-14 23:57:10 -0700689 /*
690 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
691 * ENOBUFS might not be good (it's not tunable per se), but otherwise
692 * we don't have a good statistic (IpOutDiscards but it can be too many
693 * things). We could add another new stat but at least for now that
694 * seems like overkill.
695 */
696 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800697 UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
Martin Bligh81aa6462006-08-14 23:57:10 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return err;
700
701do_confirm:
702 dst_confirm(&rt->u.dst);
703 if (!(msg->msg_flags&MSG_PROBE) || len)
704 goto back_from_confirm;
705 err = 0;
706 goto out;
707}
708
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800709int udp_sendpage(struct sock *sk, struct page *page, int offset,
710 size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct udp_sock *up = udp_sk(sk);
713 int ret;
714
715 if (!up->pending) {
716 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
717
718 /* Call udp_sendmsg to specify destination address which
719 * sendpage interface can't pass.
720 * This will succeed only when the socket is connected.
721 */
722 ret = udp_sendmsg(NULL, sk, &msg, 0);
723 if (ret < 0)
724 return ret;
725 }
726
727 lock_sock(sk);
728
729 if (unlikely(!up->pending)) {
730 release_sock(sk);
731
Patrick McHardy64ce2072005-08-09 20:50:53 -0700732 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EINVAL;
734 }
735
736 ret = ip_append_page(sk, page, offset, size, flags);
737 if (ret == -EOPNOTSUPP) {
738 release_sock(sk);
739 return sock_no_sendpage(sk->sk_socket, page, offset,
740 size, flags);
741 }
742 if (ret < 0) {
743 udp_flush_pending_frames(sk);
744 goto out;
745 }
746
747 up->len += size;
748 if (!(up->corkflag || (flags&MSG_MORE)))
749 ret = udp_push_pending_frames(sk, up);
750 if (!ret)
751 ret = size;
752out:
753 release_sock(sk);
754 return ret;
755}
756
757/*
758 * IOCTL requests applicable to the UDP protocol
759 */
760
761int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
762{
763 switch(cmd)
764 {
765 case SIOCOUTQ:
766 {
767 int amount = atomic_read(&sk->sk_wmem_alloc);
768 return put_user(amount, (int __user *)arg);
769 }
770
771 case SIOCINQ:
772 {
773 struct sk_buff *skb;
774 unsigned long amount;
775
776 amount = 0;
Herbert Xu208d8982005-05-30 15:50:15 -0700777 spin_lock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 skb = skb_peek(&sk->sk_receive_queue);
779 if (skb != NULL) {
780 /*
781 * We will only return the amount
782 * of this packet since that is all
783 * that will be read.
784 */
785 amount = skb->len - sizeof(struct udphdr);
786 }
Herbert Xu208d8982005-05-30 15:50:15 -0700787 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return put_user(amount, (int __user *)arg);
789 }
790
791 default:
792 return -ENOIOCTLCMD;
793 }
794 return(0);
795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/*
798 * This should be easy, if there is something there we
799 * return it, otherwise we block.
800 */
801
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800802int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
803 size_t len, int noblock, int flags, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct inet_sock *inet = inet_sk(sk);
806 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
807 struct sk_buff *skb;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800808 int copied, err, copy_only, is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * Check any passed addresses
812 */
813 if (addr_len)
814 *addr_len=sizeof(*sin);
815
816 if (flags & MSG_ERRQUEUE)
817 return ip_recv_error(sk, msg, len);
818
819try_again:
820 skb = skb_recv_datagram(sk, flags, noblock, &err);
821 if (!skb)
822 goto out;
823
824 copied = skb->len - sizeof(struct udphdr);
825 if (copied > len) {
826 copied = len;
827 msg->msg_flags |= MSG_TRUNC;
828 }
829
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800830 /*
831 * Decide whether to checksum and/or copy data.
832 *
833 * UDP: checksum may have been computed in HW,
834 * (re-)compute it if message is truncated.
835 * UDP-Lite: always needs to checksum, no HW support.
836 */
837 copy_only = (skb->ip_summed==CHECKSUM_UNNECESSARY);
838
839 if (is_udplite || (!copy_only && msg->msg_flags&MSG_TRUNC)) {
840 if (__udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800842 copy_only = 1;
843 }
844
845 if (copy_only)
846 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
847 msg->msg_iov, copied );
848 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
850
851 if (err == -EINVAL)
852 goto csum_copy_err;
853 }
854
855 if (err)
856 goto out_free;
857
858 sock_recv_timestamp(msg, sk, skb);
859
860 /* Copy the address. */
861 if (sin)
862 {
863 sin->sin_family = AF_INET;
864 sin->sin_port = skb->h.uh->source;
865 sin->sin_addr.s_addr = skb->nh.iph->saddr;
866 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
867 }
868 if (inet->cmsg_flags)
869 ip_cmsg_recv(msg, skb);
870
871 err = copied;
872 if (flags & MSG_TRUNC)
873 err = skb->len - sizeof(struct udphdr);
874
875out_free:
876 skb_free_datagram(sk, skb);
877out:
878 return err;
879
880csum_copy_err:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800881 UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Herbert Xu3305b802005-12-13 23:16:37 -0800883 skb_kill_datagram(sk, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (noblock)
886 return -EAGAIN;
887 goto try_again;
888}
889
890
891int udp_disconnect(struct sock *sk, int flags)
892{
893 struct inet_sock *inet = inet_sk(sk);
894 /*
895 * 1003.1g - break association.
896 */
897
898 sk->sk_state = TCP_CLOSE;
899 inet->daddr = 0;
900 inet->dport = 0;
901 sk->sk_bound_dev_if = 0;
902 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
903 inet_reset_saddr(sk);
904
905 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
906 sk->sk_prot->unhash(sk);
907 inet->sport = 0;
908 }
909 sk_dst_reset(sk);
910 return 0;
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/* return:
914 * 1 if the the UDP system should process it
915 * 0 if we should drop this packet
916 * -1 if it should get processed by xfrm4_rcv_encap
917 */
918static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
919{
920#ifndef CONFIG_XFRM
921 return 1;
922#else
923 struct udp_sock *up = udp_sk(sk);
Olaf Kirch753eab72006-11-22 20:11:42 -0800924 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct iphdr *iph;
926 int iphlen, len;
927
Olaf Kirch753eab72006-11-22 20:11:42 -0800928 __u8 *udpdata;
929 __be32 *udpdata32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 __u16 encap_type = up->encap_type;
931
932 /* if we're overly short, let UDP handle it */
Olaf Kirch753eab72006-11-22 20:11:42 -0800933 len = skb->len - sizeof(struct udphdr);
934 if (len <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 1;
936
937 /* if this is not encapsulated socket, then just return now */
938 if (!encap_type)
939 return 1;
940
Olaf Kirch753eab72006-11-22 20:11:42 -0800941 /* If this is a paged skb, make sure we pull up
942 * whatever data we need to look at. */
943 if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
944 return 1;
945
946 /* Now we can get the pointers */
947 uh = skb->h.uh;
948 udpdata = (__u8 *)uh + sizeof(struct udphdr);
949 udpdata32 = (__be32 *)udpdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 switch (encap_type) {
952 default:
953 case UDP_ENCAP_ESPINUDP:
954 /* Check if this is a keepalive packet. If so, eat it. */
955 if (len == 1 && udpdata[0] == 0xff) {
956 return 0;
957 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
958 /* ESP Packet without Non-ESP header */
959 len = sizeof(struct udphdr);
960 } else
961 /* Must be an IKE packet.. pass it through */
962 return 1;
963 break;
964 case UDP_ENCAP_ESPINUDP_NON_IKE:
965 /* Check if this is a keepalive packet. If so, eat it. */
966 if (len == 1 && udpdata[0] == 0xff) {
967 return 0;
968 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
969 udpdata32[0] == 0 && udpdata32[1] == 0) {
970
971 /* ESP Packet with Non-IKE marker */
972 len = sizeof(struct udphdr) + 2 * sizeof(u32);
973 } else
974 /* Must be an IKE packet.. pass it through */
975 return 1;
976 break;
977 }
978
979 /* At this point we are sure that this is an ESPinUDP packet,
980 * so we need to remove 'len' bytes from the packet (the UDP
981 * header and optional ESP marker bytes) and then modify the
982 * protocol to ESP, and then call into the transform receiver.
983 */
Herbert Xu4d78b6c2005-04-19 22:48:59 -0700984 if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Now we can update and verify the packet length... */
988 iph = skb->nh.iph;
989 iphlen = iph->ihl << 2;
990 iph->tot_len = htons(ntohs(iph->tot_len) - len);
991 if (skb->len < iphlen + len) {
992 /* packet is too small!?! */
993 return 0;
994 }
995
996 /* pull the data buffer up to the ESP header and set the
997 * transport header to point to ESP. Keep UDP on the stack
998 * for later.
999 */
1000 skb->h.raw = skb_pull(skb, len);
1001
1002 /* modify the protocol (it's ESP!) */
1003 iph->protocol = IPPROTO_ESP;
1004
1005 /* and let the caller know to send this into the ESP processor... */
1006 return -1;
1007#endif
1008}
1009
1010/* returns:
1011 * -1: error
1012 * 0: success
1013 * >0: "udp encap" protocol resubmission
1014 *
1015 * Note that in the success and error cases, the skb is assumed to
1016 * have either been requeued or freed.
1017 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001018int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 struct udp_sock *up = udp_sk(sk);
Martin Bligh81aa6462006-08-14 23:57:10 -07001021 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /*
1024 * Charge it to the socket, dropping if the queue is full.
1025 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001026 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1027 goto drop;
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);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001051 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -ret;
1053 }
1054 /* FALLTHROUGH -- it's a UDP Packet */
1055 }
1056
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001057 /*
1058 * UDP-Lite specific tests, ignored on UDP sockets
1059 */
1060 if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
1061
1062 /*
1063 * MIB statistics other than incrementing the error count are
1064 * disabled for the following two types of errors: these depend
1065 * on the application settings, not on the functioning of the
1066 * protocol stack as such.
1067 *
1068 * RFC 3828 here recommends (sec 3.3): "There should also be a
1069 * way ... to ... at least let the receiving application block
1070 * delivery of packets with coverage values less than a value
1071 * provided by the application."
1072 */
1073 if (up->pcrlen == 0) { /* full coverage was set */
1074 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
1075 "%d while full coverage %d requested\n",
1076 UDP_SKB_CB(skb)->cscov, skb->len);
1077 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001079 /* The next case involves violating the min. coverage requested
1080 * by the receiver. This is subtle: if receiver wants x and x is
1081 * greater than the buffersize/MTU then receiver will complain
1082 * that it wants x while sender emits packets of smaller size y.
1083 * Therefore the above ...()->partial_cov statement is essential.
1084 */
1085 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
1086 LIMIT_NETDEBUG(KERN_WARNING
1087 "UDPLITE: coverage %d too small, need min %d\n",
1088 UDP_SKB_CB(skb)->cscov, up->pcrlen);
1089 goto drop;
1090 }
1091 }
1092
1093 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1094 if (__udp_lib_checksum_complete(skb))
1095 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 skb->ip_summed = CHECKSUM_UNNECESSARY;
1097 }
1098
Martin Bligh81aa6462006-08-14 23:57:10 -07001099 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
1100 /* Note that an ENOMEM error is charged twice */
1101 if (rc == -ENOMEM)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001102 UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, up->pcflag);
1103 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001105
1106 UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001108
1109drop:
1110 UDP_INC_STATS_BH(UDP_MIB_INERRORS, up->pcflag);
1111 kfree_skb(skb);
1112 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/*
1116 * Multicasts and broadcasts go to each listener.
1117 *
1118 * Note: called only from the BH handler context,
1119 * so we don't need to lock the hashes.
1120 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001121static int __udp4_lib_mcast_deliver(struct sk_buff *skb,
1122 struct udphdr *uh,
1123 __be32 saddr, __be32 daddr,
1124 struct hlist_head udptable[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct sock *sk;
1127 int dif;
1128
1129 read_lock(&udp_hash_lock);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001130 sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 dif = skb->dev->ifindex;
1132 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1133 if (sk) {
1134 struct sock *sknext = NULL;
1135
1136 do {
1137 struct sk_buff *skb1 = skb;
1138
1139 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1140 uh->source, saddr, dif);
1141 if(sknext)
1142 skb1 = skb_clone(skb, GFP_ATOMIC);
1143
1144 if(skb1) {
1145 int ret = udp_queue_rcv_skb(sk, skb1);
1146 if (ret > 0)
1147 /* we should probably re-process instead
1148 * of dropping packets here. */
1149 kfree_skb(skb1);
1150 }
1151 sk = sknext;
1152 } while(sknext);
1153 } else
1154 kfree_skb(skb);
1155 read_unlock(&udp_hash_lock);
1156 return 0;
1157}
1158
1159/* Initialize UDP checksum. If exited with zero value (success),
1160 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1161 * Otherwise, csum completion requires chacksumming packet body,
1162 * including udp header and folding it to skb->csum.
1163 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001164static inline void udp4_csum_init(struct sk_buff *skb, struct udphdr *uh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 if (uh->check == 0) {
1167 skb->ip_summed = CHECKSUM_UNNECESSARY;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001168 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001169 if (!csum_tcpudp_magic(skb->nh.iph->saddr, skb->nh.iph->daddr,
1170 skb->len, IPPROTO_UDP, skb->csum ))
Herbert Xufb286bb2005-11-10 13:01:24 -08001171 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001174 skb->csum = csum_tcpudp_nofold(skb->nh.iph->saddr,
1175 skb->nh.iph->daddr,
1176 skb->len, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /* Probably, we should checksum udp header (it should be in cache
1178 * in any case) and data in tiny packets (< rx copybreak).
1179 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001180
1181 /* UDP = UDP-Lite with a non-partial checksum coverage */
1182 UDP_SKB_CB(skb)->partial_cov = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185/*
1186 * All we need to do is get the socket, and then do a checksum.
1187 */
1188
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001189int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
1190 int is_udplite)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct sock *sk;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001193 struct udphdr *uh = skb->h.uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 unsigned short ulen;
1195 struct rtable *rt = (struct rtable*)skb->dst;
Al Viro734ab872006-09-27 18:37:41 -07001196 __be32 saddr = skb->nh.iph->saddr;
1197 __be32 daddr = skb->nh.iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001200 * Validate the packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001203 goto drop; /* No space for header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001206 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto short_packet;
1208
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001209 if(! is_udplite ) { /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001211 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1212 goto short_packet;
1213
1214 udp4_csum_init(skb, uh);
1215
1216 } else { /* UDP-Lite validates cscov. */
1217 if (udplite4_csum_init(skb, uh))
1218 goto csum_error;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001222 return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001224 sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest,
1225 skb->dev->ifindex, udptable );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (sk != NULL) {
1228 int ret = udp_queue_rcv_skb(sk, skb);
1229 sock_put(sk);
1230
1231 /* a return value > 0 means to resubmit the input, but
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001232 * it wants the return to be -protocol, or 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
1234 if (ret > 0)
1235 return -ret;
1236 return 0;
1237 }
1238
1239 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1240 goto drop;
Patrick McHardyb59c2702006-01-06 23:06:10 -08001241 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* No socket. Drop packet silently, if checksum is wrong */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001244 if (udp_lib_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 goto csum_error;
1246
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001247 UDP_INC_STATS_BH(UDP_MIB_NOPORTS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1249
1250 /*
1251 * Hmm. We got an UDP packet to a port to which we
1252 * don't wanna listen. Ignore it.
1253 */
1254 kfree_skb(skb);
1255 return(0);
1256
1257short_packet:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001258 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1259 is_udplite? "-Lite" : "",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001260 NIPQUAD(saddr),
1261 ntohs(uh->source),
1262 ulen,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001263 skb->len,
Patrick McHardy64ce2072005-08-09 20:50:53 -07001264 NIPQUAD(daddr),
1265 ntohs(uh->dest));
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001266 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268csum_error:
1269 /*
1270 * RFC1122: OK. Discards the bad packet silently (as far as
1271 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1272 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001273 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1274 is_udplite? "-Lite" : "",
Patrick McHardy64ce2072005-08-09 20:50:53 -07001275 NIPQUAD(saddr),
1276 ntohs(uh->source),
1277 NIPQUAD(daddr),
1278 ntohs(uh->dest),
1279 ulen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280drop:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001281 UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 kfree_skb(skb);
1283 return(0);
1284}
1285
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001286__inline__ int udp_rcv(struct sk_buff *skb)
1287{
1288 return __udp4_lib_rcv(skb, udp_hash, 0);
1289}
1290
1291int udp_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 lock_sock(sk);
1294 udp_flush_pending_frames(sk);
1295 release_sock(sk);
1296 return 0;
1297}
1298
1299/*
1300 * Socket option code for UDP
1301 */
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001302static int do_udp_setsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 char __user *optval, int optlen)
1304{
1305 struct udp_sock *up = udp_sk(sk);
1306 int val;
1307 int err = 0;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if(optlen<sizeof(int))
1310 return -EINVAL;
1311
1312 if (get_user(val, (int __user *)optval))
1313 return -EFAULT;
1314
1315 switch(optname) {
1316 case UDP_CORK:
1317 if (val != 0) {
1318 up->corkflag = 1;
1319 } else {
1320 up->corkflag = 0;
1321 lock_sock(sk);
1322 udp_push_pending_frames(sk, up);
1323 release_sock(sk);
1324 }
1325 break;
1326
1327 case UDP_ENCAP:
1328 switch (val) {
1329 case 0:
1330 case UDP_ENCAP_ESPINUDP:
1331 case UDP_ENCAP_ESPINUDP_NON_IKE:
1332 up->encap_type = val;
1333 break;
1334 default:
1335 err = -ENOPROTOOPT;
1336 break;
1337 }
1338 break;
1339
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001340 /*
1341 * UDP-Lite's partial checksum coverage (RFC 3828).
1342 */
1343 /* The sender sets actual checksum coverage length via this option.
1344 * The case coverage > packet length is handled by send module. */
1345 case UDPLITE_SEND_CSCOV:
1346 if (!up->pcflag) /* Disable the option on UDP sockets */
1347 return -ENOPROTOOPT;
1348 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1349 val = 8;
1350 up->pcslen = val;
1351 up->pcflag |= UDPLITE_SEND_CC;
1352 break;
1353
1354 /* The receiver specifies a minimum checksum coverage value. To make
1355 * sense, this should be set to at least 8 (as done below). If zero is
1356 * used, this again means full checksum coverage. */
1357 case UDPLITE_RECV_CSCOV:
1358 if (!up->pcflag) /* Disable the option on UDP sockets */
1359 return -ENOPROTOOPT;
1360 if (val != 0 && val < 8) /* Avoid silly minimal values. */
1361 val = 8;
1362 up->pcrlen = val;
1363 up->pcflag |= UDPLITE_RECV_CC;
1364 break;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 default:
1367 err = -ENOPROTOOPT;
1368 break;
1369 };
1370
1371 return err;
1372}
1373
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001374int udp_setsockopt(struct sock *sk, int level, int optname,
1375 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001376{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001377 if (level == SOL_UDP || level == SOL_UDPLITE)
1378 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1379 return ip_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001380}
1381
1382#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001383int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1384 char __user *optval, int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001385{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001386 if (level == SOL_UDP || level == SOL_UDPLITE)
1387 return do_udp_setsockopt(sk, level, optname, optval, optlen);
1388 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001389}
1390#endif
1391
1392static int do_udp_getsockopt(struct sock *sk, int level, int optname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 char __user *optval, int __user *optlen)
1394{
1395 struct udp_sock *up = udp_sk(sk);
1396 int val, len;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if(get_user(len,optlen))
1399 return -EFAULT;
1400
1401 len = min_t(unsigned int, len, sizeof(int));
1402
1403 if(len < 0)
1404 return -EINVAL;
1405
1406 switch(optname) {
1407 case UDP_CORK:
1408 val = up->corkflag;
1409 break;
1410
1411 case UDP_ENCAP:
1412 val = up->encap_type;
1413 break;
1414
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001415 /* The following two cannot be changed on UDP sockets, the return is
1416 * always 0 (which corresponds to the full checksum coverage of UDP). */
1417 case UDPLITE_SEND_CSCOV:
1418 val = up->pcslen;
1419 break;
1420
1421 case UDPLITE_RECV_CSCOV:
1422 val = up->pcrlen;
1423 break;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 default:
1426 return -ENOPROTOOPT;
1427 };
1428
1429 if(put_user(len, optlen))
1430 return -EFAULT;
1431 if(copy_to_user(optval, &val,len))
1432 return -EFAULT;
1433 return 0;
1434}
1435
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001436int udp_getsockopt(struct sock *sk, int level, int optname,
1437 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001438{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001439 if (level == SOL_UDP || level == SOL_UDPLITE)
1440 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1441 return ip_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001442}
1443
1444#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001445int compat_udp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001446 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001447{
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001448 if (level == SOL_UDP || level == SOL_UDPLITE)
1449 return do_udp_getsockopt(sk, level, optname, optval, optlen);
1450 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001451}
1452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/**
1454 * udp_poll - wait for a UDP event.
1455 * @file - file struct
1456 * @sock - socket
1457 * @wait - poll table
1458 *
1459 * This is same as datagram poll, except for the special case of
1460 * blocking sockets. If application is using a blocking fd
1461 * and a packet with checksum error is in the queue;
1462 * then it could get return from select indicating data available
1463 * but then block when reading it. Add special case code
1464 * to work around these arguably broken applications.
1465 */
1466unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1467{
1468 unsigned int mask = datagram_poll(file, sock, wait);
1469 struct sock *sk = sock->sk;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001470 int is_lite = IS_UDPLITE(sk);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Check for false positives due to checksum errors */
1473 if ( (mask & POLLRDNORM) &&
1474 !(file->f_flags & O_NONBLOCK) &&
1475 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1476 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1477 struct sk_buff *skb;
1478
Herbert Xu208d8982005-05-30 15:50:15 -07001479 spin_lock_bh(&rcvq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 while ((skb = skb_peek(rcvq)) != NULL) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001481 if (udp_lib_checksum_complete(skb)) {
1482 UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 __skb_unlink(skb, rcvq);
1484 kfree_skb(skb);
1485 } else {
1486 skb->ip_summed = CHECKSUM_UNNECESSARY;
1487 break;
1488 }
1489 }
Herbert Xu208d8982005-05-30 15:50:15 -07001490 spin_unlock_bh(&rcvq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /* nothing to see, move along */
1493 if (skb == NULL)
1494 mask &= ~(POLLIN | POLLRDNORM);
1495 }
1496
1497 return mask;
1498
1499}
1500
1501struct proto udp_prot = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001502 .name = "UDP",
1503 .owner = THIS_MODULE,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001504 .close = udp_lib_close,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001505 .connect = ip4_datagram_connect,
1506 .disconnect = udp_disconnect,
1507 .ioctl = udp_ioctl,
1508 .destroy = udp_destroy_sock,
1509 .setsockopt = udp_setsockopt,
1510 .getsockopt = udp_getsockopt,
1511 .sendmsg = udp_sendmsg,
1512 .recvmsg = udp_recvmsg,
1513 .sendpage = udp_sendpage,
1514 .backlog_rcv = udp_queue_rcv_skb,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001515 .hash = udp_lib_hash,
1516 .unhash = udp_lib_unhash,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001517 .get_port = udp_v4_get_port,
1518 .obj_size = sizeof(struct udp_sock),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001519#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001520 .compat_setsockopt = compat_udp_setsockopt,
1521 .compat_getsockopt = compat_udp_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523};
1524
1525/* ------------------------------------------------------------------------ */
1526#ifdef CONFIG_PROC_FS
1527
1528static struct sock *udp_get_first(struct seq_file *seq)
1529{
1530 struct sock *sk;
1531 struct udp_iter_state *state = seq->private;
1532
1533 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1534 struct hlist_node *node;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001535 sk_for_each(sk, node, state->hashtable + state->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (sk->sk_family == state->family)
1537 goto found;
1538 }
1539 }
1540 sk = NULL;
1541found:
1542 return sk;
1543}
1544
1545static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1546{
1547 struct udp_iter_state *state = seq->private;
1548
1549 do {
1550 sk = sk_next(sk);
1551try_again:
1552 ;
1553 } while (sk && sk->sk_family != state->family);
1554
1555 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001556 sk = sk_head(state->hashtable + state->bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto try_again;
1558 }
1559 return sk;
1560}
1561
1562static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1563{
1564 struct sock *sk = udp_get_first(seq);
1565
1566 if (sk)
1567 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1568 --pos;
1569 return pos ? NULL : sk;
1570}
1571
1572static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1573{
1574 read_lock(&udp_hash_lock);
1575 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1576}
1577
1578static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1579{
1580 struct sock *sk;
1581
1582 if (v == (void *)1)
1583 sk = udp_get_idx(seq, 0);
1584 else
1585 sk = udp_get_next(seq, v);
1586
1587 ++*pos;
1588 return sk;
1589}
1590
1591static void udp_seq_stop(struct seq_file *seq, void *v)
1592{
1593 read_unlock(&udp_hash_lock);
1594}
1595
1596static int udp_seq_open(struct inode *inode, struct file *file)
1597{
1598 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1599 struct seq_file *seq;
1600 int rc = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001601 struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (!s)
1604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 s->family = afinfo->family;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001606 s->hashtable = afinfo->hashtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 s->seq_ops.start = udp_seq_start;
1608 s->seq_ops.next = udp_seq_next;
1609 s->seq_ops.show = afinfo->seq_show;
1610 s->seq_ops.stop = udp_seq_stop;
1611
1612 rc = seq_open(file, &s->seq_ops);
1613 if (rc)
1614 goto out_kfree;
1615
1616 seq = file->private_data;
1617 seq->private = s;
1618out:
1619 return rc;
1620out_kfree:
1621 kfree(s);
1622 goto out;
1623}
1624
1625/* ------------------------------------------------------------------------ */
1626int udp_proc_register(struct udp_seq_afinfo *afinfo)
1627{
1628 struct proc_dir_entry *p;
1629 int rc = 0;
1630
1631 if (!afinfo)
1632 return -EINVAL;
1633 afinfo->seq_fops->owner = afinfo->owner;
1634 afinfo->seq_fops->open = udp_seq_open;
1635 afinfo->seq_fops->read = seq_read;
1636 afinfo->seq_fops->llseek = seq_lseek;
1637 afinfo->seq_fops->release = seq_release_private;
1638
1639 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1640 if (p)
1641 p->data = afinfo;
1642 else
1643 rc = -ENOMEM;
1644 return rc;
1645}
1646
1647void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1648{
1649 if (!afinfo)
1650 return;
1651 proc_net_remove(afinfo->name);
1652 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1653}
1654
1655/* ------------------------------------------------------------------------ */
1656static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1657{
1658 struct inet_sock *inet = inet_sk(sp);
Al Viro734ab872006-09-27 18:37:41 -07001659 __be32 dest = inet->daddr;
1660 __be32 src = inet->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 __u16 destp = ntohs(inet->dport);
1662 __u16 srcp = ntohs(inet->sport);
1663
1664 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1665 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1666 bucket, src, srcp, dest, destp, sp->sk_state,
1667 atomic_read(&sp->sk_wmem_alloc),
1668 atomic_read(&sp->sk_rmem_alloc),
1669 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1670 atomic_read(&sp->sk_refcnt), sp);
1671}
1672
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001673int udp4_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 if (v == SEQ_START_TOKEN)
1676 seq_printf(seq, "%-127s\n",
1677 " sl local_address rem_address st tx_queue "
1678 "rx_queue tr tm->when retrnsmt uid timeout "
1679 "inode");
1680 else {
1681 char tmpbuf[129];
1682 struct udp_iter_state *state = seq->private;
1683
1684 udp4_format_sock(v, tmpbuf, state->bucket);
1685 seq_printf(seq, "%-127s\n", tmpbuf);
1686 }
1687 return 0;
1688}
1689
1690/* ------------------------------------------------------------------------ */
1691static struct file_operations udp4_seq_fops;
1692static struct udp_seq_afinfo udp4_seq_afinfo = {
1693 .owner = THIS_MODULE,
1694 .name = "udp",
1695 .family = AF_INET,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001696 .hashtable = udp_hash,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .seq_show = udp4_seq_show,
1698 .seq_fops = &udp4_seq_fops,
1699};
1700
1701int __init udp4_proc_init(void)
1702{
1703 return udp_proc_register(&udp4_seq_afinfo);
1704}
1705
1706void udp4_proc_exit(void)
1707{
1708 udp_proc_unregister(&udp4_seq_afinfo);
1709}
1710#endif /* CONFIG_PROC_FS */
1711
1712EXPORT_SYMBOL(udp_disconnect);
1713EXPORT_SYMBOL(udp_hash);
1714EXPORT_SYMBOL(udp_hash_lock);
1715EXPORT_SYMBOL(udp_ioctl);
Gerrit Renker25030a72006-08-26 20:06:05 -07001716EXPORT_SYMBOL(udp_get_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717EXPORT_SYMBOL(udp_prot);
1718EXPORT_SYMBOL(udp_sendmsg);
1719EXPORT_SYMBOL(udp_poll);
1720
1721#ifdef CONFIG_PROC_FS
1722EXPORT_SYMBOL(udp_proc_register);
1723EXPORT_SYMBOL(udp_proc_unregister);
1724#endif