blob: 0ea974bf7962756035153b0543c1b69a9fc9bed1 [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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
Alan Cox113aa832008-10-13 19:01:08 -070011 * Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Hirokazu Takahashi, <taka@valinux.co.jp>
13 *
14 * Fixes:
15 * Alan Cox : verify_area() calls
16 * Alan Cox : stopped close while in use off icmp
17 * messages. Not a fix but a botch that
18 * for udp at least is 'valid'.
19 * Alan Cox : Fixed icmp handling properly
20 * Alan Cox : Correct error for oversized datagrams
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090021 * Alan Cox : Tidied select() semantics.
22 * Alan Cox : udp_err() fixed properly, also now
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * select and read wake correctly on errors
24 * Alan Cox : udp_send verify_area moved to avoid mem leak
25 * Alan Cox : UDP can count its memory
26 * Alan Cox : send to an unknown connection causes
27 * an ECONNREFUSED off the icmp, but
28 * does NOT close.
29 * Alan Cox : Switched to new sk_buff handlers. No more backlog!
30 * Alan Cox : Using generic datagram code. Even smaller and the PEEK
31 * bug no longer crashes it.
32 * Fred Van Kempen : Net2e support for sk->broadcast.
33 * Alan Cox : Uses skb_free_datagram
34 * Alan Cox : Added get/set sockopt support.
35 * Alan Cox : Broadcasting without option set returns EACCES.
36 * Alan Cox : No wakeup calls. Instead we now use the callbacks.
37 * Alan Cox : Use ip_tos and ip_ttl
38 * Alan Cox : SNMP Mibs
39 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
40 * Matt Dillon : UDP length checks.
41 * Alan Cox : Smarter af_inet used properly.
42 * Alan Cox : Use new kernel side addressing.
43 * Alan Cox : Incorrect return on truncated datagram receive.
44 * Arnt Gulbrandsen : New udp_send and stuff
45 * Alan Cox : Cache last socket
46 * Alan Cox : Route cache
47 * Jon Peatfield : Minor efficiency fix to sendto().
48 * Mike Shaver : RFC1122 checks.
49 * Alan Cox : Nonblocking error fix.
50 * Willy Konynenberg : Transparent proxying support.
51 * Mike McLagan : Routing by source
52 * David S. Miller : New socket lookup architecture.
53 * Last socket cache retained as it
54 * does have a high hit rate.
55 * Olaf Kirch : Don't linearise iovec on sendmsg.
56 * Andi Kleen : Some cleanups, cache destination entry
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090057 * for connect.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
59 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
60 * return ENOTCONN for unconnected sockets (POSIX)
61 * Janos Farkas : don't deliver multi/broadcasts to a different
62 * bound-to-device socket
63 * Hirokazu Takahashi : HW checksumming for outgoing UDP
64 * datagrams.
65 * Hirokazu Takahashi : sendfile() on UDP works now.
66 * Arnaldo C. Melo : convert /proc/net/udp to seq_file
67 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
68 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
69 * a single port at the same time.
70 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
James Chapman342f0232007-06-27 15:37:46 -070071 * James Chapman : Add L2TP encapsulation type.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 *
74 * This program is free software; you can redistribute it and/or
75 * modify it under the terms of the GNU General Public License
76 * as published by the Free Software Foundation; either version
77 * 2 of the License, or (at your option) any later version.
78 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/system.h>
81#include <asm/uaccess.h>
82#include <asm/ioctls.h>
Hideo Aoki95766ff2007-12-31 00:29:24 -080083#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200100#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <net/icmp.h>
102#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <net/checksum.h>
104#include <net/xfrm.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800105#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Eric Dumazet645ca702008-10-29 01:41:45 -0700107struct udp_table udp_table;
108EXPORT_SYMBOL(udp_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Hideo Aoki95766ff2007-12-31 00:29:24 -0800110int sysctl_udp_mem[3] __read_mostly;
111int sysctl_udp_rmem_min __read_mostly;
112int sysctl_udp_wmem_min __read_mostly;
113
114EXPORT_SYMBOL(sysctl_udp_mem);
115EXPORT_SYMBOL(sysctl_udp_rmem_min);
116EXPORT_SYMBOL(sysctl_udp_wmem_min);
117
118atomic_t udp_memory_allocated;
119EXPORT_SYMBOL(udp_memory_allocated);
120
Eric Dumazetf24d43c2008-10-09 14:51:27 -0700121static int udp_lib_lport_inuse(struct net *net, __u16 num,
Eric Dumazet645ca702008-10-29 01:41:45 -0700122 const struct udp_hslot *hslot,
Eric Dumazetf24d43c2008-10-09 14:51:27 -0700123 struct sock *sk,
124 int (*saddr_comp)(const struct sock *sk1,
125 const struct sock *sk2))
Gerrit Renker25030a72006-08-26 20:06:05 -0700126{
Eric Dumazetf24d43c2008-10-09 14:51:27 -0700127 struct sock *sk2;
Gerrit Renker25030a72006-08-26 20:06:05 -0700128 struct hlist_node *node;
129
Eric Dumazet645ca702008-10-29 01:41:45 -0700130 sk_for_each(sk2, node, &hslot->head)
Eric Dumazetf24d43c2008-10-09 14:51:27 -0700131 if (net_eq(sock_net(sk2), net) &&
132 sk2 != sk &&
133 sk2->sk_hash == num &&
134 (!sk2->sk_reuse || !sk->sk_reuse) &&
135 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
136 || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
137 (*saddr_comp)(sk, sk2))
Gerrit Renker25030a72006-08-26 20:06:05 -0700138 return 1;
139 return 0;
140}
141
142/**
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700143 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
Gerrit Renker25030a72006-08-26 20:06:05 -0700144 *
145 * @sk: socket struct in question
146 * @snum: port number to look up
David S. Millerdf2bc452007-06-05 15:18:43 -0700147 * @saddr_comp: AF-dependent comparison of bound local IP addresses
Gerrit Renker25030a72006-08-26 20:06:05 -0700148 */
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700149int udp_lib_get_port(struct sock *sk, unsigned short snum,
David S. Millerdf2bc452007-06-05 15:18:43 -0700150 int (*saddr_comp)(const struct sock *sk1,
151 const struct sock *sk2 ) )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Eric Dumazet645ca702008-10-29 01:41:45 -0700153 struct udp_hslot *hslot;
154 struct udp_table *udptable = sk->sk_prot->h.udp_table;
Gerrit Renker25030a72006-08-26 20:06:05 -0700155 int error = 1;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900156 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Stephen Hemminger32c1da72007-08-24 23:09:41 -0700158 if (!snum) {
Eric Dumazet9088c562008-10-08 11:44:17 -0700159 int low, high, remaining;
160 unsigned rand;
161 unsigned short first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700163 inet_get_local_port_range(&low, &high);
Anton Arapova25de532007-10-18 22:00:17 -0700164 remaining = (high - low) + 1;
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700165
Eric Dumazet9088c562008-10-08 11:44:17 -0700166 rand = net_random();
167 snum = first = rand % remaining + low;
168 rand |= 1;
Eric Dumazet645ca702008-10-29 01:41:45 -0700169 for (;;) {
170 hslot = &udptable->hash[udp_hashfn(net, snum)];
171 spin_lock_bh(&hslot->lock);
172 if (!udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp))
173 break;
174 spin_unlock_bh(&hslot->lock);
Eric Dumazet9088c562008-10-08 11:44:17 -0700175 do {
176 snum = snum + rand;
177 } while (snum < low || snum > high);
178 if (snum == first)
179 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Eric Dumazet645ca702008-10-29 01:41:45 -0700181 } else {
182 hslot = &udptable->hash[udp_hashfn(net, snum)];
183 spin_lock_bh(&hslot->lock);
184 if (udp_lib_lport_inuse(net, snum, hslot, sk, saddr_comp))
185 goto fail_unlock;
186 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700187 inet_sk(sk)->num = snum;
David S. Millerdf2bc452007-06-05 15:18:43 -0700188 sk->sk_hash = snum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (sk_unhashed(sk)) {
Eric Dumazet271b72c2008-10-29 02:11:14 -0700190 sk_add_node_rcu(sk, &hslot->head);
Pavel Emelyanovc29a0bc2008-03-31 19:41:46 -0700191 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Gerrit Renker25030a72006-08-26 20:06:05 -0700193 error = 0;
Eric Dumazet645ca702008-10-29 01:41:45 -0700194fail_unlock:
195 spin_unlock_bh(&hslot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196fail:
Gerrit Renker25030a72006-08-26 20:06:05 -0700197 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700200static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
David S. Millerdb8dac22008-03-06 16:22:02 -0800201{
202 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
203
204 return ( !ipv6_only_sock(sk2) &&
205 (!inet1->rcv_saddr || !inet2->rcv_saddr ||
206 inet1->rcv_saddr == inet2->rcv_saddr ));
207}
208
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700209int udp_v4_get_port(struct sock *sk, unsigned short snum)
David S. Millerdb8dac22008-03-06 16:22:02 -0800210{
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -0700211 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal);
David S. Millerdb8dac22008-03-06 16:22:02 -0800212}
213
Eric Dumazet645ca702008-10-29 01:41:45 -0700214static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
215 unsigned short hnum,
216 __be16 sport, __be32 daddr, __be16 dport, int dif)
217{
218 int score = -1;
219
220 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
221 !ipv6_only_sock(sk)) {
222 struct inet_sock *inet = inet_sk(sk);
223
224 score = (sk->sk_family == PF_INET ? 1 : 0);
225 if (inet->rcv_saddr) {
226 if (inet->rcv_saddr != daddr)
227 return -1;
228 score += 2;
229 }
230 if (inet->daddr) {
231 if (inet->daddr != saddr)
232 return -1;
233 score += 2;
234 }
235 if (inet->dport) {
236 if (inet->dport != sport)
237 return -1;
238 score += 2;
239 }
240 if (sk->sk_bound_dev_if) {
241 if (sk->sk_bound_dev_if != dif)
242 return -1;
243 score += 2;
244 }
245 }
246 return score;
247}
248
David S. Millerdb8dac22008-03-06 16:22:02 -0800249/* UDP is nearly always wildcards out the wazoo, it makes no sense to try
250 * harder than this. -DaveM
251 */
252static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
253 __be16 sport, __be32 daddr, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700254 int dif, struct udp_table *udptable)
David S. Millerdb8dac22008-03-06 16:22:02 -0800255{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700256 struct sock *sk, *result;
David S. Millerdb8dac22008-03-06 16:22:02 -0800257 struct hlist_node *node;
258 unsigned short hnum = ntohs(dport);
Eric Dumazet645ca702008-10-29 01:41:45 -0700259 unsigned int hash = udp_hashfn(net, hnum);
260 struct udp_hslot *hslot = &udptable->hash[hash];
Eric Dumazet271b72c2008-10-29 02:11:14 -0700261 int score, badness;
David S. Millerdb8dac22008-03-06 16:22:02 -0800262
Eric Dumazet271b72c2008-10-29 02:11:14 -0700263 rcu_read_lock();
264begin:
265 result = NULL;
266 badness = -1;
267 sk_for_each_rcu(sk, node, &hslot->head) {
268 /*
269 * lockless reader, and SLAB_DESTROY_BY_RCU items:
270 * We must check this item was not moved to another chain
271 */
272 if (udp_hashfn(net, sk->sk_hash) != hash)
273 goto begin;
Eric Dumazet645ca702008-10-29 01:41:45 -0700274 score = compute_score(sk, net, saddr, hnum, sport,
275 daddr, dport, dif);
276 if (score > badness) {
277 result = sk;
278 badness = score;
David S. Millerdb8dac22008-03-06 16:22:02 -0800279 }
280 }
Eric Dumazet271b72c2008-10-29 02:11:14 -0700281 if (result) {
282 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
283 result = NULL;
284 else if (unlikely(compute_score(result, net, saddr, hnum, sport,
285 daddr, dport, dif) < badness)) {
286 sock_put(result);
287 goto begin;
288 }
289 }
290 rcu_read_unlock();
David S. Millerdb8dac22008-03-06 16:22:02 -0800291 return result;
292}
293
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700294static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
295 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700296 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700297{
KOVACS Krisztian23542612008-10-07 12:41:01 -0700298 struct sock *sk;
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700299 const struct iphdr *iph = ip_hdr(skb);
300
KOVACS Krisztian23542612008-10-07 12:41:01 -0700301 if (unlikely(sk = skb_steal_sock(skb)))
302 return sk;
303 else
304 return __udp4_lib_lookup(dev_net(skb->dst->dev), iph->saddr, sport,
305 iph->daddr, dport, inet_iif(skb),
306 udptable);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700307}
308
KOVACS Krisztianbcd41302008-10-01 07:48:10 -0700309struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
310 __be32 daddr, __be16 dport, int dif)
311{
Eric Dumazet645ca702008-10-29 01:41:45 -0700312 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
KOVACS Krisztianbcd41302008-10-01 07:48:10 -0700313}
314EXPORT_SYMBOL_GPL(udp4_lib_lookup);
315
David S. Millerdb8dac22008-03-06 16:22:02 -0800316static inline struct sock *udp_v4_mcast_next(struct sock *sk,
317 __be16 loc_port, __be32 loc_addr,
318 __be16 rmt_port, __be32 rmt_addr,
319 int dif)
320{
321 struct hlist_node *node;
322 struct sock *s = sk;
323 unsigned short hnum = ntohs(loc_port);
324
325 sk_for_each_from(s, node) {
326 struct inet_sock *inet = inet_sk(s);
327
328 if (s->sk_hash != hnum ||
329 (inet->daddr && inet->daddr != rmt_addr) ||
330 (inet->dport != rmt_port && inet->dport) ||
331 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
332 ipv6_only_sock(s) ||
333 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
334 continue;
335 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
336 continue;
337 goto found;
338 }
339 s = NULL;
340found:
341 return s;
342}
343
344/*
345 * This routine is called by the ICMP module when it gets some
346 * sort of error condition. If err < 0 then the socket should
347 * be closed and the error returned to the user. If err > 0
348 * it's just the icmp type << 8 | icmp code.
349 * Header points to the ip header of the error packet. We move
350 * on past this. Then (as it used to claim before adjustment)
351 * header points to the first 8 bytes of the udp header. We need
352 * to find the appropriate port.
353 */
354
Eric Dumazet645ca702008-10-29 01:41:45 -0700355void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
David S. Millerdb8dac22008-03-06 16:22:02 -0800356{
357 struct inet_sock *inet;
358 struct iphdr *iph = (struct iphdr*)skb->data;
359 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
360 const int type = icmp_hdr(skb)->type;
361 const int code = icmp_hdr(skb)->code;
362 struct sock *sk;
363 int harderr;
364 int err;
Pavel Emelyanovfd54d712008-07-14 23:01:40 -0700365 struct net *net = dev_net(skb->dev);
David S. Millerdb8dac22008-03-06 16:22:02 -0800366
Pavel Emelyanovfd54d712008-07-14 23:01:40 -0700367 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
David S. Millerdb8dac22008-03-06 16:22:02 -0800368 iph->saddr, uh->source, skb->dev->ifindex, udptable);
369 if (sk == NULL) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700370 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
David S. Millerdb8dac22008-03-06 16:22:02 -0800371 return; /* No socket for error */
372 }
373
374 err = 0;
375 harderr = 0;
376 inet = inet_sk(sk);
377
378 switch (type) {
379 default:
380 case ICMP_TIME_EXCEEDED:
381 err = EHOSTUNREACH;
382 break;
383 case ICMP_SOURCE_QUENCH:
384 goto out;
385 case ICMP_PARAMETERPROB:
386 err = EPROTO;
387 harderr = 1;
388 break;
389 case ICMP_DEST_UNREACH:
390 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
391 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
392 err = EMSGSIZE;
393 harderr = 1;
394 break;
395 }
396 goto out;
397 }
398 err = EHOSTUNREACH;
399 if (code <= NR_ICMP_UNREACH) {
400 harderr = icmp_err_convert[code].fatal;
401 err = icmp_err_convert[code].errno;
402 }
403 break;
404 }
405
406 /*
407 * RFC1122: OK. Passes ICMP errors back to application, as per
408 * 4.1.3.3.
409 */
410 if (!inet->recverr) {
411 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
412 goto out;
413 } else {
414 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
415 }
416 sk->sk_err = err;
417 sk->sk_error_report(sk);
418out:
419 sock_put(sk);
420}
421
422void udp_err(struct sk_buff *skb, u32 info)
423{
Eric Dumazet645ca702008-10-29 01:41:45 -0700424 __udp4_lib_err(skb, info, &udp_table);
David S. Millerdb8dac22008-03-06 16:22:02 -0800425}
426
427/*
428 * Throw away all pending data and cancel the corking. Socket is locked.
429 */
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400430void udp_flush_pending_frames(struct sock *sk)
David S. Millerdb8dac22008-03-06 16:22:02 -0800431{
432 struct udp_sock *up = udp_sk(sk);
433
434 if (up->pending) {
435 up->len = 0;
436 up->pending = 0;
437 ip_flush_pending_frames(sk);
438 }
439}
Denis V. Lunev36d926b2008-06-04 15:49:07 +0400440EXPORT_SYMBOL(udp_flush_pending_frames);
David S. Millerdb8dac22008-03-06 16:22:02 -0800441
442/**
443 * udp4_hwcsum_outgoing - handle outgoing HW checksumming
444 * @sk: socket we are sending on
445 * @skb: sk_buff containing the filled-in UDP header
446 * (checksum field must be zeroed out)
447 */
448static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
449 __be32 src, __be32 dst, int len )
450{
451 unsigned int offset;
452 struct udphdr *uh = udp_hdr(skb);
453 __wsum csum = 0;
454
455 if (skb_queue_len(&sk->sk_write_queue) == 1) {
456 /*
457 * Only one fragment on the socket.
458 */
459 skb->csum_start = skb_transport_header(skb) - skb->head;
460 skb->csum_offset = offsetof(struct udphdr, check);
461 uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
462 } else {
463 /*
464 * HW-checksum won't work as there are two or more
465 * fragments on the socket so that all csums of sk_buffs
466 * should be together
467 */
468 offset = skb_transport_offset(skb);
469 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
470
471 skb->ip_summed = CHECKSUM_NONE;
472
473 skb_queue_walk(&sk->sk_write_queue, skb) {
474 csum = csum_add(csum, skb->csum);
475 }
476
477 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
478 if (uh->check == 0)
479 uh->check = CSUM_MANGLED_0;
480 }
481}
482
483/*
484 * Push out all pending data as one UDP datagram. Socket is locked.
485 */
486static int udp_push_pending_frames(struct sock *sk)
487{
488 struct udp_sock *up = udp_sk(sk);
489 struct inet_sock *inet = inet_sk(sk);
490 struct flowi *fl = &inet->cork.fl;
491 struct sk_buff *skb;
492 struct udphdr *uh;
493 int err = 0;
494 int is_udplite = IS_UDPLITE(sk);
495 __wsum csum = 0;
496
497 /* Grab the skbuff where UDP header space exists. */
498 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
499 goto out;
500
501 /*
502 * Create a UDP header
503 */
504 uh = udp_hdr(skb);
505 uh->source = fl->fl_ip_sport;
506 uh->dest = fl->fl_ip_dport;
507 uh->len = htons(up->len);
508 uh->check = 0;
509
510 if (is_udplite) /* UDP-Lite */
511 csum = udplite_csum_outgoing(sk, skb);
512
513 else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
514
515 skb->ip_summed = CHECKSUM_NONE;
516 goto send;
517
518 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
519
520 udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len);
521 goto send;
522
523 } else /* `normal' UDP */
524 csum = udp_csum_outgoing(sk, skb);
525
526 /* add protocol-dependent pseudo-header */
527 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
528 sk->sk_protocol, csum );
529 if (uh->check == 0)
530 uh->check = CSUM_MANGLED_0;
531
532send:
533 err = ip_push_pending_frames(sk);
534out:
535 up->len = 0;
536 up->pending = 0;
537 if (!err)
Pavel Emelyanov629ca232008-07-05 21:18:07 -0700538 UDP_INC_STATS_USER(sock_net(sk),
539 UDP_MIB_OUTDATAGRAMS, is_udplite);
David S. Millerdb8dac22008-03-06 16:22:02 -0800540 return err;
541}
542
543int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
544 size_t len)
545{
546 struct inet_sock *inet = inet_sk(sk);
547 struct udp_sock *up = udp_sk(sk);
548 int ulen = len;
549 struct ipcm_cookie ipc;
550 struct rtable *rt = NULL;
551 int free = 0;
552 int connected = 0;
553 __be32 daddr, faddr, saddr;
554 __be16 dport;
555 u8 tos;
556 int err, is_udplite = IS_UDPLITE(sk);
557 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
558 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
559
560 if (len > 0xFFFF)
561 return -EMSGSIZE;
562
563 /*
564 * Check the flags.
565 */
566
567 if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
568 return -EOPNOTSUPP;
569
570 ipc.opt = NULL;
571
572 if (up->pending) {
573 /*
574 * There are pending frames.
575 * The socket lock must be held while it's corked.
576 */
577 lock_sock(sk);
578 if (likely(up->pending)) {
579 if (unlikely(up->pending != AF_INET)) {
580 release_sock(sk);
581 return -EINVAL;
582 }
583 goto do_append_data;
584 }
585 release_sock(sk);
586 }
587 ulen += sizeof(struct udphdr);
588
589 /*
590 * Get and verify the address.
591 */
592 if (msg->msg_name) {
593 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
594 if (msg->msg_namelen < sizeof(*usin))
595 return -EINVAL;
596 if (usin->sin_family != AF_INET) {
597 if (usin->sin_family != AF_UNSPEC)
598 return -EAFNOSUPPORT;
599 }
600
601 daddr = usin->sin_addr.s_addr;
602 dport = usin->sin_port;
603 if (dport == 0)
604 return -EINVAL;
605 } else {
606 if (sk->sk_state != TCP_ESTABLISHED)
607 return -EDESTADDRREQ;
608 daddr = inet->daddr;
609 dport = inet->dport;
610 /* Open fast path for connected socket.
611 Route will not be used, if at least one option is set.
612 */
613 connected = 1;
614 }
615 ipc.addr = inet->saddr;
616
617 ipc.oif = sk->sk_bound_dev_if;
618 if (msg->msg_controllen) {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900619 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
David S. Millerdb8dac22008-03-06 16:22:02 -0800620 if (err)
621 return err;
622 if (ipc.opt)
623 free = 1;
624 connected = 0;
625 }
626 if (!ipc.opt)
627 ipc.opt = inet->opt;
628
629 saddr = ipc.addr;
630 ipc.addr = faddr = daddr;
631
632 if (ipc.opt && ipc.opt->srr) {
633 if (!daddr)
634 return -EINVAL;
635 faddr = ipc.opt->faddr;
636 connected = 0;
637 }
638 tos = RT_TOS(inet->tos);
639 if (sock_flag(sk, SOCK_LOCALROUTE) ||
640 (msg->msg_flags & MSG_DONTROUTE) ||
641 (ipc.opt && ipc.opt->is_strictroute)) {
642 tos |= RTO_ONLINK;
643 connected = 0;
644 }
645
646 if (ipv4_is_multicast(daddr)) {
647 if (!ipc.oif)
648 ipc.oif = inet->mc_index;
649 if (!saddr)
650 saddr = inet->mc_addr;
651 connected = 0;
652 }
653
654 if (connected)
655 rt = (struct rtable*)sk_dst_check(sk, 0);
656
657 if (rt == NULL) {
658 struct flowi fl = { .oif = ipc.oif,
659 .nl_u = { .ip4_u =
660 { .daddr = faddr,
661 .saddr = saddr,
662 .tos = tos } },
663 .proto = sk->sk_protocol,
664 .uli_u = { .ports =
665 { .sport = inet->sport,
666 .dport = dport } } };
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700667 struct net *net = sock_net(sk);
668
David S. Millerdb8dac22008-03-06 16:22:02 -0800669 security_sk_classify_flow(sk, &fl);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700670 err = ip_route_output_flow(net, &rt, &fl, sk, 1);
David S. Millerdb8dac22008-03-06 16:22:02 -0800671 if (err) {
672 if (err == -ENETUNREACH)
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700673 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
David S. Millerdb8dac22008-03-06 16:22:02 -0800674 goto out;
675 }
676
677 err = -EACCES;
678 if ((rt->rt_flags & RTCF_BROADCAST) &&
679 !sock_flag(sk, SOCK_BROADCAST))
680 goto out;
681 if (connected)
682 sk_dst_set(sk, dst_clone(&rt->u.dst));
683 }
684
685 if (msg->msg_flags&MSG_CONFIRM)
686 goto do_confirm;
687back_from_confirm:
688
689 saddr = rt->rt_src;
690 if (!ipc.addr)
691 daddr = ipc.addr = rt->rt_dst;
692
693 lock_sock(sk);
694 if (unlikely(up->pending)) {
695 /* The socket is already corked while preparing it. */
696 /* ... which is an evident application bug. --ANK */
697 release_sock(sk);
698
699 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
700 err = -EINVAL;
701 goto out;
702 }
703 /*
704 * Now cork the socket to pend data.
705 */
706 inet->cork.fl.fl4_dst = daddr;
707 inet->cork.fl.fl_ip_dport = dport;
708 inet->cork.fl.fl4_src = saddr;
709 inet->cork.fl.fl_ip_sport = inet->sport;
710 up->pending = AF_INET;
711
712do_append_data:
713 up->len += ulen;
714 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
715 err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
716 sizeof(struct udphdr), &ipc, rt,
717 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
718 if (err)
719 udp_flush_pending_frames(sk);
720 else if (!corkreq)
721 err = udp_push_pending_frames(sk);
722 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
723 up->pending = 0;
724 release_sock(sk);
725
726out:
727 ip_rt_put(rt);
728 if (free)
729 kfree(ipc.opt);
730 if (!err)
731 return len;
732 /*
733 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
734 * ENOBUFS might not be good (it's not tunable per se), but otherwise
735 * we don't have a good statistic (IpOutDiscards but it can be too many
736 * things). We could add another new stat but at least for now that
737 * seems like overkill.
738 */
739 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Pavel Emelyanov629ca232008-07-05 21:18:07 -0700740 UDP_INC_STATS_USER(sock_net(sk),
741 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millerdb8dac22008-03-06 16:22:02 -0800742 }
743 return err;
744
745do_confirm:
746 dst_confirm(&rt->u.dst);
747 if (!(msg->msg_flags&MSG_PROBE) || len)
748 goto back_from_confirm;
749 err = 0;
750 goto out;
751}
752
753int udp_sendpage(struct sock *sk, struct page *page, int offset,
754 size_t size, int flags)
755{
756 struct udp_sock *up = udp_sk(sk);
757 int ret;
758
759 if (!up->pending) {
760 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
761
762 /* Call udp_sendmsg to specify destination address which
763 * sendpage interface can't pass.
764 * This will succeed only when the socket is connected.
765 */
766 ret = udp_sendmsg(NULL, sk, &msg, 0);
767 if (ret < 0)
768 return ret;
769 }
770
771 lock_sock(sk);
772
773 if (unlikely(!up->pending)) {
774 release_sock(sk);
775
776 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
777 return -EINVAL;
778 }
779
780 ret = ip_append_page(sk, page, offset, size, flags);
781 if (ret == -EOPNOTSUPP) {
782 release_sock(sk);
783 return sock_no_sendpage(sk->sk_socket, page, offset,
784 size, flags);
785 }
786 if (ret < 0) {
787 udp_flush_pending_frames(sk);
788 goto out;
789 }
790
791 up->len += size;
792 if (!(up->corkflag || (flags&MSG_MORE)))
793 ret = udp_push_pending_frames(sk);
794 if (!ret)
795 ret = size;
796out:
797 release_sock(sk);
798 return ret;
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * IOCTL requests applicable to the UDP protocol
803 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
806{
Stephen Hemminger6516c652007-03-08 20:41:55 -0800807 switch (cmd) {
808 case SIOCOUTQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 {
Stephen Hemminger6516c652007-03-08 20:41:55 -0800810 int amount = atomic_read(&sk->sk_wmem_alloc);
811 return put_user(amount, (int __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Stephen Hemminger6516c652007-03-08 20:41:55 -0800813
814 case SIOCINQ:
815 {
816 struct sk_buff *skb;
817 unsigned long amount;
818
819 amount = 0;
820 spin_lock_bh(&sk->sk_receive_queue.lock);
821 skb = skb_peek(&sk->sk_receive_queue);
822 if (skb != NULL) {
823 /*
824 * We will only return the amount
825 * of this packet since that is all
826 * that will be read.
827 */
828 amount = skb->len - sizeof(struct udphdr);
829 }
830 spin_unlock_bh(&sk->sk_receive_queue.lock);
831 return put_user(amount, (int __user *)arg);
832 }
833
834 default:
835 return -ENOIOCTLCMD;
836 }
837
838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
David S. Millerdb8dac22008-03-06 16:22:02 -0800841/*
842 * This should be easy, if there is something there we
843 * return it, otherwise we block.
844 */
845
846int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
847 size_t len, int noblock, int flags, int *addr_len)
848{
849 struct inet_sock *inet = inet_sk(sk);
850 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
851 struct sk_buff *skb;
852 unsigned int ulen, copied;
853 int peeked;
854 int err;
855 int is_udplite = IS_UDPLITE(sk);
856
857 /*
858 * Check any passed addresses
859 */
860 if (addr_len)
861 *addr_len=sizeof(*sin);
862
863 if (flags & MSG_ERRQUEUE)
864 return ip_recv_error(sk, msg, len);
865
866try_again:
867 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
868 &peeked, &err);
869 if (!skb)
870 goto out;
871
872 ulen = skb->len - sizeof(struct udphdr);
873 copied = len;
874 if (copied > ulen)
875 copied = ulen;
876 else if (copied < ulen)
877 msg->msg_flags |= MSG_TRUNC;
878
879 /*
880 * If checksum is needed at all, try to do it while copying the
881 * data. If the data is truncated, or if we only want a partial
882 * coverage checksum (UDP-Lite), do it before the copy.
883 */
884
885 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
886 if (udp_lib_checksum_complete(skb))
887 goto csum_copy_err;
888 }
889
890 if (skb_csum_unnecessary(skb))
891 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
892 msg->msg_iov, copied );
893 else {
894 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
895
896 if (err == -EINVAL)
897 goto csum_copy_err;
898 }
899
900 if (err)
901 goto out_free;
902
903 if (!peeked)
Pavel Emelyanov629ca232008-07-05 21:18:07 -0700904 UDP_INC_STATS_USER(sock_net(sk),
905 UDP_MIB_INDATAGRAMS, is_udplite);
David S. Millerdb8dac22008-03-06 16:22:02 -0800906
907 sock_recv_timestamp(msg, sk, skb);
908
909 /* Copy the address. */
910 if (sin)
911 {
912 sin->sin_family = AF_INET;
913 sin->sin_port = udp_hdr(skb)->source;
914 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
915 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
916 }
917 if (inet->cmsg_flags)
918 ip_cmsg_recv(msg, skb);
919
920 err = copied;
921 if (flags & MSG_TRUNC)
922 err = ulen;
923
924out_free:
925 lock_sock(sk);
926 skb_free_datagram(sk, skb);
927 release_sock(sk);
928out:
929 return err;
930
931csum_copy_err:
932 lock_sock(sk);
933 if (!skb_kill_datagram(sk, skb, flags))
Pavel Emelyanov629ca232008-07-05 21:18:07 -0700934 UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
David S. Millerdb8dac22008-03-06 16:22:02 -0800935 release_sock(sk);
936
937 if (noblock)
938 return -EAGAIN;
939 goto try_again;
940}
941
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943int udp_disconnect(struct sock *sk, int flags)
944{
945 struct inet_sock *inet = inet_sk(sk);
946 /*
947 * 1003.1g - break association.
948 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sk->sk_state = TCP_CLOSE;
951 inet->daddr = 0;
952 inet->dport = 0;
953 sk->sk_bound_dev_if = 0;
954 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
955 inet_reset_saddr(sk);
956
957 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
958 sk->sk_prot->unhash(sk);
959 inet->sport = 0;
960 }
961 sk_dst_reset(sk);
962 return 0;
963}
964
Eric Dumazet645ca702008-10-29 01:41:45 -0700965void udp_lib_unhash(struct sock *sk)
966{
967 struct udp_table *udptable = sk->sk_prot->h.udp_table;
968 unsigned int hash = udp_hashfn(sock_net(sk), sk->sk_hash);
969 struct udp_hslot *hslot = &udptable->hash[hash];
970
971 spin_lock(&hslot->lock);
Eric Dumazet271b72c2008-10-29 02:11:14 -0700972 if (sk_del_node_init_rcu(sk)) {
Eric Dumazet645ca702008-10-29 01:41:45 -0700973 inet_sk(sk)->num = 0;
974 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
975 }
976 spin_unlock(&hslot->lock);
977}
978EXPORT_SYMBOL(udp_lib_unhash);
979
Herbert Xu93821772008-09-15 11:48:46 -0700980static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
981{
982 int is_udplite = IS_UDPLITE(sk);
983 int rc;
984
985 if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) {
986 /* Note that an ENOMEM error is charged twice */
987 if (rc == -ENOMEM)
988 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
989 is_udplite);
990 goto drop;
991 }
992
993 return 0;
994
995drop:
996 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
997 kfree_skb(skb);
998 return -1;
999}
1000
David S. Millerdb8dac22008-03-06 16:22:02 -08001001/* returns:
1002 * -1: error
1003 * 0: success
1004 * >0: "udp encap" protocol resubmission
1005 *
1006 * Note that in the success and error cases, the skb is assumed to
1007 * have either been requeued or freed.
1008 */
1009int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1010{
1011 struct udp_sock *up = udp_sk(sk);
1012 int rc;
1013 int is_udplite = IS_UDPLITE(sk);
1014
1015 /*
1016 * Charge it to the socket, dropping if the queue is full.
1017 */
1018 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1019 goto drop;
1020 nf_reset(skb);
1021
1022 if (up->encap_type) {
1023 /*
1024 * This is an encapsulation socket so pass the skb to
1025 * the socket's udp_encap_rcv() hook. Otherwise, just
1026 * fall through and pass this up the UDP socket.
1027 * up->encap_rcv() returns the following value:
1028 * =0 if skb was successfully passed to the encap
1029 * handler or was discarded by it.
1030 * >0 if skb should be passed on to UDP.
1031 * <0 if skb should be resubmitted as proto -N
1032 */
1033
1034 /* if we're overly short, let UDP handle it */
1035 if (skb->len > sizeof(struct udphdr) &&
1036 up->encap_rcv != NULL) {
1037 int ret;
1038
1039 ret = (*up->encap_rcv)(sk, skb);
1040 if (ret <= 0) {
Pavel Emelyanov02833282008-07-05 21:18:48 -07001041 UDP_INC_STATS_BH(sock_net(sk),
1042 UDP_MIB_INDATAGRAMS,
David S. Millerdb8dac22008-03-06 16:22:02 -08001043 is_udplite);
1044 return -ret;
1045 }
1046 }
1047
1048 /* FALLTHROUGH -- it's a UDP Packet */
1049 }
1050
1051 /*
1052 * UDP-Lite specific tests, ignored on UDP sockets
1053 */
1054 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
1055
1056 /*
1057 * MIB statistics other than incrementing the error count are
1058 * disabled for the following two types of errors: these depend
1059 * on the application settings, not on the functioning of the
1060 * protocol stack as such.
1061 *
1062 * RFC 3828 here recommends (sec 3.3): "There should also be a
1063 * way ... to ... at least let the receiving application block
1064 * delivery of packets with coverage values less than a value
1065 * provided by the application."
1066 */
1067 if (up->pcrlen == 0) { /* full coverage was set */
1068 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
1069 "%d while full coverage %d requested\n",
1070 UDP_SKB_CB(skb)->cscov, skb->len);
1071 goto drop;
1072 }
1073 /* The next case involves violating the min. coverage requested
1074 * by the receiver. This is subtle: if receiver wants x and x is
1075 * greater than the buffersize/MTU then receiver will complain
1076 * that it wants x while sender emits packets of smaller size y.
1077 * Therefore the above ...()->partial_cov statement is essential.
1078 */
1079 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
1080 LIMIT_NETDEBUG(KERN_WARNING
1081 "UDPLITE: coverage %d too small, need min %d\n",
1082 UDP_SKB_CB(skb)->cscov, up->pcrlen);
1083 goto drop;
1084 }
1085 }
1086
1087 if (sk->sk_filter) {
1088 if (udp_lib_checksum_complete(skb))
1089 goto drop;
1090 }
1091
Herbert Xu93821772008-09-15 11:48:46 -07001092 rc = 0;
David S. Millerdb8dac22008-03-06 16:22:02 -08001093
Herbert Xu93821772008-09-15 11:48:46 -07001094 bh_lock_sock(sk);
1095 if (!sock_owned_by_user(sk))
1096 rc = __udp_queue_rcv_skb(sk, skb);
1097 else
1098 sk_add_backlog(sk, skb);
1099 bh_unlock_sock(sk);
1100
1101 return rc;
David S. Millerdb8dac22008-03-06 16:22:02 -08001102
1103drop:
Pavel Emelyanov02833282008-07-05 21:18:48 -07001104 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
David S. Millerdb8dac22008-03-06 16:22:02 -08001105 kfree_skb(skb);
1106 return -1;
1107}
1108
1109/*
1110 * Multicasts and broadcasts go to each listener.
1111 *
1112 * Note: called only from the BH handler context,
1113 * so we don't need to lock the hashes.
1114 */
Pavel Emelyanove3163492008-06-16 17:12:11 -07001115static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
David S. Millerdb8dac22008-03-06 16:22:02 -08001116 struct udphdr *uh,
1117 __be32 saddr, __be32 daddr,
Eric Dumazet645ca702008-10-29 01:41:45 -07001118 struct udp_table *udptable)
David S. Millerdb8dac22008-03-06 16:22:02 -08001119{
1120 struct sock *sk;
Eric Dumazet645ca702008-10-29 01:41:45 -07001121 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
David S. Millerdb8dac22008-03-06 16:22:02 -08001122 int dif;
1123
Eric Dumazet645ca702008-10-29 01:41:45 -07001124 spin_lock(&hslot->lock);
1125 sk = sk_head(&hslot->head);
David S. Millerdb8dac22008-03-06 16:22:02 -08001126 dif = skb->dev->ifindex;
1127 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1128 if (sk) {
1129 struct sock *sknext = NULL;
1130
1131 do {
1132 struct sk_buff *skb1 = skb;
1133
1134 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1135 uh->source, saddr, dif);
1136 if (sknext)
1137 skb1 = skb_clone(skb, GFP_ATOMIC);
1138
1139 if (skb1) {
Herbert Xu93821772008-09-15 11:48:46 -07001140 int ret = udp_queue_rcv_skb(sk, skb1);
David S. Millerdb8dac22008-03-06 16:22:02 -08001141 if (ret > 0)
1142 /* we should probably re-process instead
1143 * of dropping packets here. */
1144 kfree_skb(skb1);
1145 }
1146 sk = sknext;
1147 } while (sknext);
1148 } else
1149 kfree_skb(skb);
Eric Dumazet645ca702008-10-29 01:41:45 -07001150 spin_unlock(&hslot->lock);
David S. Millerdb8dac22008-03-06 16:22:02 -08001151 return 0;
1152}
1153
1154/* Initialize UDP checksum. If exited with zero value (success),
1155 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1156 * Otherwise, csum completion requires chacksumming packet body,
1157 * including udp header and folding it to skb->csum.
1158 */
1159static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1160 int proto)
1161{
1162 const struct iphdr *iph;
1163 int err;
1164
1165 UDP_SKB_CB(skb)->partial_cov = 0;
1166 UDP_SKB_CB(skb)->cscov = skb->len;
1167
1168 if (proto == IPPROTO_UDPLITE) {
1169 err = udplite_checksum_init(skb, uh);
1170 if (err)
1171 return err;
1172 }
1173
1174 iph = ip_hdr(skb);
1175 if (uh->check == 0) {
1176 skb->ip_summed = CHECKSUM_UNNECESSARY;
1177 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1178 if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1179 proto, skb->csum))
1180 skb->ip_summed = CHECKSUM_UNNECESSARY;
1181 }
1182 if (!skb_csum_unnecessary(skb))
1183 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1184 skb->len, proto, 0);
1185 /* Probably, we should checksum udp header (it should be in cache
1186 * in any case) and data in tiny packets (< rx copybreak).
1187 */
1188
1189 return 0;
1190}
1191
1192/*
1193 * All we need to do is get the socket, and then do a checksum.
1194 */
1195
Eric Dumazet645ca702008-10-29 01:41:45 -07001196int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
David S. Millerdb8dac22008-03-06 16:22:02 -08001197 int proto)
1198{
1199 struct sock *sk;
1200 struct udphdr *uh = udp_hdr(skb);
1201 unsigned short ulen;
1202 struct rtable *rt = (struct rtable*)skb->dst;
1203 __be32 saddr = ip_hdr(skb)->saddr;
1204 __be32 daddr = ip_hdr(skb)->daddr;
Pavel Emelyanov02833282008-07-05 21:18:48 -07001205 struct net *net = dev_net(skb->dev);
David S. Millerdb8dac22008-03-06 16:22:02 -08001206
1207 /*
1208 * Validate the packet.
1209 */
1210 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1211 goto drop; /* No space for header. */
1212
1213 ulen = ntohs(uh->len);
1214 if (ulen > skb->len)
1215 goto short_packet;
1216
1217 if (proto == IPPROTO_UDP) {
1218 /* UDP validates ulen. */
1219 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1220 goto short_packet;
1221 uh = udp_hdr(skb);
1222 }
1223
1224 if (udp4_csum_init(skb, uh, proto))
1225 goto csum_error;
1226
1227 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
Pavel Emelyanove3163492008-06-16 17:12:11 -07001228 return __udp4_lib_mcast_deliver(net, skb, uh,
1229 saddr, daddr, udptable);
David S. Millerdb8dac22008-03-06 16:22:02 -08001230
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -07001231 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
David S. Millerdb8dac22008-03-06 16:22:02 -08001232
1233 if (sk != NULL) {
Herbert Xu93821772008-09-15 11:48:46 -07001234 int ret = udp_queue_rcv_skb(sk, skb);
David S. Millerdb8dac22008-03-06 16:22:02 -08001235 sock_put(sk);
1236
1237 /* a return value > 0 means to resubmit the input, but
1238 * it wants the return to be -protocol, or 0
1239 */
1240 if (ret > 0)
1241 return -ret;
1242 return 0;
1243 }
1244
1245 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1246 goto drop;
1247 nf_reset(skb);
1248
1249 /* No socket. Drop packet silently, if checksum is wrong */
1250 if (udp_lib_checksum_complete(skb))
1251 goto csum_error;
1252
Pavel Emelyanov02833282008-07-05 21:18:48 -07001253 UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
David S. Millerdb8dac22008-03-06 16:22:02 -08001254 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1255
1256 /*
1257 * Hmm. We got an UDP packet to a port to which we
1258 * don't wanna listen. Ignore it.
1259 */
1260 kfree_skb(skb);
1261 return 0;
1262
1263short_packet:
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001264 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n",
David S. Millerdb8dac22008-03-06 16:22:02 -08001265 proto == IPPROTO_UDPLITE ? "-Lite" : "",
1266 NIPQUAD(saddr),
1267 ntohs(uh->source),
1268 ulen,
1269 skb->len,
1270 NIPQUAD(daddr),
1271 ntohs(uh->dest));
1272 goto drop;
1273
1274csum_error:
1275 /*
1276 * RFC1122: OK. Discards the bad packet silently (as far as
1277 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1278 */
YOSHIFUJI Hideakia7d632b2008-04-14 04:09:00 -07001279 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n",
David S. Millerdb8dac22008-03-06 16:22:02 -08001280 proto == IPPROTO_UDPLITE ? "-Lite" : "",
1281 NIPQUAD(saddr),
1282 ntohs(uh->source),
1283 NIPQUAD(daddr),
1284 ntohs(uh->dest),
1285 ulen);
1286drop:
Pavel Emelyanov02833282008-07-05 21:18:48 -07001287 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
David S. Millerdb8dac22008-03-06 16:22:02 -08001288 kfree_skb(skb);
1289 return 0;
1290}
1291
1292int udp_rcv(struct sk_buff *skb)
1293{
Eric Dumazet645ca702008-10-29 01:41:45 -07001294 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
David S. Millerdb8dac22008-03-06 16:22:02 -08001295}
1296
Brian Haley7d06b2e2008-06-14 17:04:49 -07001297void udp_destroy_sock(struct sock *sk)
David S. Millerdb8dac22008-03-06 16:22:02 -08001298{
1299 lock_sock(sk);
1300 udp_flush_pending_frames(sk);
1301 release_sock(sk);
David S. Millerdb8dac22008-03-06 16:22:02 -08001302}
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304/*
1305 * Socket option code for UDP
1306 */
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001307int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1308 char __user *optval, int optlen,
1309 int (*push_pending_frames)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct udp_sock *up = udp_sk(sk);
1312 int val;
1313 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +11001314 int is_udplite = IS_UDPLITE(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Stephen Hemminger6516c652007-03-08 20:41:55 -08001316 if (optlen<sizeof(int))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return -EINVAL;
1318
1319 if (get_user(val, (int __user *)optval))
1320 return -EFAULT;
1321
Stephen Hemminger6516c652007-03-08 20:41:55 -08001322 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 case UDP_CORK:
1324 if (val != 0) {
1325 up->corkflag = 1;
1326 } else {
1327 up->corkflag = 0;
1328 lock_sock(sk);
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001329 (*push_pending_frames)(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 release_sock(sk);
1331 }
1332 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 case UDP_ENCAP:
1335 switch (val) {
1336 case 0:
1337 case UDP_ENCAP_ESPINUDP:
1338 case UDP_ENCAP_ESPINUDP_NON_IKE:
James Chapman067b2072007-07-05 17:08:05 -07001339 up->encap_rcv = xfrm4_udp_encap_rcv;
1340 /* FALLTHROUGH */
James Chapman342f0232007-06-27 15:37:46 -07001341 case UDP_ENCAP_L2TPINUDP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 up->encap_type = val;
1343 break;
1344 default:
1345 err = -ENOPROTOOPT;
1346 break;
1347 }
1348 break;
1349
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001350 /*
1351 * UDP-Lite's partial checksum coverage (RFC 3828).
1352 */
1353 /* The sender sets actual checksum coverage length via this option.
1354 * The case coverage > packet length is handled by send module. */
1355 case UDPLITE_SEND_CSCOV:
Wang Chenb2bf1e22007-12-03 22:34:16 +11001356 if (!is_udplite) /* Disable the option on UDP sockets */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001357 return -ENOPROTOOPT;
1358 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1359 val = 8;
Gerrit Renker47112e22008-07-21 13:35:08 -07001360 else if (val > USHORT_MAX)
1361 val = USHORT_MAX;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001362 up->pcslen = val;
1363 up->pcflag |= UDPLITE_SEND_CC;
1364 break;
1365
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001366 /* The receiver specifies a minimum checksum coverage value. To make
1367 * sense, this should be set to at least 8 (as done below). If zero is
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001368 * used, this again means full checksum coverage. */
1369 case UDPLITE_RECV_CSCOV:
Wang Chenb2bf1e22007-12-03 22:34:16 +11001370 if (!is_udplite) /* Disable the option on UDP sockets */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001371 return -ENOPROTOOPT;
1372 if (val != 0 && val < 8) /* Avoid silly minimal values. */
1373 val = 8;
Gerrit Renker47112e22008-07-21 13:35:08 -07001374 else if (val > USHORT_MAX)
1375 val = USHORT_MAX;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001376 up->pcrlen = val;
1377 up->pcflag |= UDPLITE_RECV_CC;
1378 break;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 default:
1381 err = -ENOPROTOOPT;
1382 break;
Stephen Hemminger6516c652007-03-08 20:41:55 -08001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 return err;
1386}
1387
David S. Millerdb8dac22008-03-06 16:22:02 -08001388int udp_setsockopt(struct sock *sk, int level, int optname,
1389 char __user *optval, int optlen)
1390{
1391 if (level == SOL_UDP || level == SOL_UDPLITE)
1392 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1393 udp_push_pending_frames);
1394 return ip_setsockopt(sk, level, optname, optval, optlen);
1395}
1396
1397#ifdef CONFIG_COMPAT
1398int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1399 char __user *optval, int optlen)
1400{
1401 if (level == SOL_UDP || level == SOL_UDPLITE)
1402 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1403 udp_push_pending_frames);
1404 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1405}
1406#endif
1407
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001408int udp_lib_getsockopt(struct sock *sk, int level, int optname,
1409 char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 struct udp_sock *up = udp_sk(sk);
1412 int val, len;
1413
Stephen Hemminger6516c652007-03-08 20:41:55 -08001414 if (get_user(len,optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EFAULT;
1416
1417 len = min_t(unsigned int, len, sizeof(int));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001418
Stephen Hemminger6516c652007-03-08 20:41:55 -08001419 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
1421
Stephen Hemminger6516c652007-03-08 20:41:55 -08001422 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 case UDP_CORK:
1424 val = up->corkflag;
1425 break;
1426
1427 case UDP_ENCAP:
1428 val = up->encap_type;
1429 break;
1430
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001431 /* The following two cannot be changed on UDP sockets, the return is
1432 * always 0 (which corresponds to the full checksum coverage of UDP). */
1433 case UDPLITE_SEND_CSCOV:
1434 val = up->pcslen;
1435 break;
1436
1437 case UDPLITE_RECV_CSCOV:
1438 val = up->pcrlen;
1439 break;
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 default:
1442 return -ENOPROTOOPT;
Stephen Hemminger6516c652007-03-08 20:41:55 -08001443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Stephen Hemminger6516c652007-03-08 20:41:55 -08001445 if (put_user(len, optlen))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001446 return -EFAULT;
Stephen Hemminger6516c652007-03-08 20:41:55 -08001447 if (copy_to_user(optval, &val,len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EFAULT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
David S. Millerdb8dac22008-03-06 16:22:02 -08001452int udp_getsockopt(struct sock *sk, int level, int optname,
1453 char __user *optval, int __user *optlen)
1454{
1455 if (level == SOL_UDP || level == SOL_UDPLITE)
1456 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1457 return ip_getsockopt(sk, level, optname, optval, optlen);
1458}
1459
1460#ifdef CONFIG_COMPAT
1461int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1462 char __user *optval, int __user *optlen)
1463{
1464 if (level == SOL_UDP || level == SOL_UDPLITE)
1465 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1466 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1467}
1468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/**
1470 * udp_poll - wait for a UDP event.
1471 * @file - file struct
1472 * @sock - socket
1473 * @wait - poll table
1474 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001475 * This is same as datagram poll, except for the special case of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * blocking sockets. If application is using a blocking fd
1477 * and a packet with checksum error is in the queue;
1478 * then it could get return from select indicating data available
1479 * but then block when reading it. Add special case code
1480 * to work around these arguably broken applications.
1481 */
1482unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1483{
1484 unsigned int mask = datagram_poll(file, sock, wait);
1485 struct sock *sk = sock->sk;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001486 int is_lite = IS_UDPLITE(sk);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* Check for false positives due to checksum errors */
1489 if ( (mask & POLLRDNORM) &&
1490 !(file->f_flags & O_NONBLOCK) &&
1491 !(sk->sk_shutdown & RCV_SHUTDOWN)){
1492 struct sk_buff_head *rcvq = &sk->sk_receive_queue;
1493 struct sk_buff *skb;
1494
Herbert Xu208d8982005-05-30 15:50:15 -07001495 spin_lock_bh(&rcvq->lock);
Herbert Xu759e5d02007-03-25 20:10:56 -07001496 while ((skb = skb_peek(rcvq)) != NULL &&
1497 udp_lib_checksum_complete(skb)) {
Pavel Emelyanov02833282008-07-05 21:18:48 -07001498 UDP_INC_STATS_BH(sock_net(sk),
1499 UDP_MIB_INERRORS, is_lite);
Herbert Xu759e5d02007-03-25 20:10:56 -07001500 __skb_unlink(skb, rcvq);
1501 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Herbert Xu208d8982005-05-30 15:50:15 -07001503 spin_unlock_bh(&rcvq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /* nothing to see, move along */
1506 if (skb == NULL)
1507 mask &= ~(POLLIN | POLLRDNORM);
1508 }
1509
1510 return mask;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
David S. Millerdb8dac22008-03-06 16:22:02 -08001514struct proto udp_prot = {
1515 .name = "UDP",
1516 .owner = THIS_MODULE,
1517 .close = udp_lib_close,
1518 .connect = ip4_datagram_connect,
1519 .disconnect = udp_disconnect,
1520 .ioctl = udp_ioctl,
1521 .destroy = udp_destroy_sock,
1522 .setsockopt = udp_setsockopt,
1523 .getsockopt = udp_getsockopt,
1524 .sendmsg = udp_sendmsg,
1525 .recvmsg = udp_recvmsg,
1526 .sendpage = udp_sendpage,
Herbert Xu93821772008-09-15 11:48:46 -07001527 .backlog_rcv = __udp_queue_rcv_skb,
David S. Millerdb8dac22008-03-06 16:22:02 -08001528 .hash = udp_lib_hash,
1529 .unhash = udp_lib_unhash,
1530 .get_port = udp_v4_get_port,
1531 .memory_allocated = &udp_memory_allocated,
1532 .sysctl_mem = sysctl_udp_mem,
1533 .sysctl_wmem = &sysctl_udp_wmem_min,
1534 .sysctl_rmem = &sysctl_udp_rmem_min,
1535 .obj_size = sizeof(struct udp_sock),
Eric Dumazet271b72c2008-10-29 02:11:14 -07001536 .slab_flags = SLAB_DESTROY_BY_RCU,
Eric Dumazet645ca702008-10-29 01:41:45 -07001537 .h.udp_table = &udp_table,
David S. Millerdb8dac22008-03-06 16:22:02 -08001538#ifdef CONFIG_COMPAT
1539 .compat_setsockopt = compat_udp_setsockopt,
1540 .compat_getsockopt = compat_udp_getsockopt,
1541#endif
David S. Millerdb8dac22008-03-06 16:22:02 -08001542};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544/* ------------------------------------------------------------------------ */
1545#ifdef CONFIG_PROC_FS
1546
Eric Dumazet645ca702008-10-29 01:41:45 -07001547static struct sock *udp_get_first(struct seq_file *seq, int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct sock *sk;
1550 struct udp_iter_state *state = seq->private;
Denis V. Lunev6f191ef2008-03-28 18:23:33 -07001551 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Eric Dumazet645ca702008-10-29 01:41:45 -07001553 for (state->bucket = start; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct hlist_node *node;
Eric Dumazet645ca702008-10-29 01:41:45 -07001555 struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
1556 spin_lock_bh(&hslot->lock);
1557 sk_for_each(sk, node, &hslot->head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001558 if (!net_eq(sock_net(sk), net))
Daniel Lezcanoa91275e2008-03-21 04:11:58 -07001559 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (sk->sk_family == state->family)
1561 goto found;
1562 }
Eric Dumazet645ca702008-10-29 01:41:45 -07001563 spin_unlock_bh(&hslot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565 sk = NULL;
1566found:
1567 return sk;
1568}
1569
1570static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1571{
1572 struct udp_iter_state *state = seq->private;
Denis V. Lunev6f191ef2008-03-28 18:23:33 -07001573 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 do {
1576 sk = sk_next(sk);
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001577 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Eric Dumazet645ca702008-10-29 01:41:45 -07001579 if (!sk) {
1580 spin_unlock(&state->udp_table->hash[state->bucket].lock);
1581 return udp_get_first(seq, state->bucket + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 return sk;
1584}
1585
1586static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1587{
Eric Dumazet645ca702008-10-29 01:41:45 -07001588 struct sock *sk = udp_get_first(seq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (sk)
Stephen Hemminger6516c652007-03-08 20:41:55 -08001591 while (pos && (sk = udp_get_next(seq, sk)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 --pos;
1593 return pos ? NULL : sk;
1594}
1595
1596static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1597{
YOSHIFUJI Hideakib50660f2008-03-31 19:38:15 -07001598 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1602{
1603 struct sock *sk;
1604
YOSHIFUJI Hideakib50660f2008-03-31 19:38:15 -07001605 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 sk = udp_get_idx(seq, 0);
1607 else
1608 sk = udp_get_next(seq, v);
1609
1610 ++*pos;
1611 return sk;
1612}
1613
1614static void udp_seq_stop(struct seq_file *seq, void *v)
1615{
Eric Dumazet645ca702008-10-29 01:41:45 -07001616 struct udp_iter_state *state = seq->private;
1617
1618 if (state->bucket < UDP_HTABLE_SIZE)
1619 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622static int udp_seq_open(struct inode *inode, struct file *file)
1623{
1624 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
Denis V. Luneva2be75c2008-03-28 18:25:06 -07001625 struct udp_iter_state *s;
1626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Denis V. Luneva2be75c2008-03-28 18:25:06 -07001628 err = seq_open_net(inode, file, &afinfo->seq_ops,
1629 sizeof(struct udp_iter_state));
1630 if (err < 0)
1631 return err;
Daniel Lezcanoa91275e2008-03-21 04:11:58 -07001632
Denis V. Luneva2be75c2008-03-28 18:25:06 -07001633 s = ((struct seq_file *)file->private_data)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 s->family = afinfo->family;
Eric Dumazet645ca702008-10-29 01:41:45 -07001635 s->udp_table = afinfo->udp_table;
Denis V. Luneva2be75c2008-03-28 18:25:06 -07001636 return err;
Daniel Lezcanoa91275e2008-03-21 04:11:58 -07001637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/* ------------------------------------------------------------------------ */
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001640int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct proc_dir_entry *p;
1643 int rc = 0;
1644
Denis V. Lunev3ba94412008-03-28 18:25:32 -07001645 afinfo->seq_fops.open = udp_seq_open;
1646 afinfo->seq_fops.read = seq_read;
1647 afinfo->seq_fops.llseek = seq_lseek;
1648 afinfo->seq_fops.release = seq_release_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Denis V. Lunevdda61922008-03-28 18:24:26 -07001650 afinfo->seq_ops.start = udp_seq_start;
1651 afinfo->seq_ops.next = udp_seq_next;
1652 afinfo->seq_ops.stop = udp_seq_stop;
1653
Denis V. Lunev84841c32008-05-02 04:10:08 -07001654 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
1655 &afinfo->seq_fops, afinfo);
1656 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 rc = -ENOMEM;
1658 return rc;
1659}
1660
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001661void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001663 proc_net_remove(net, afinfo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
David S. Millerdb8dac22008-03-06 16:22:02 -08001665
1666/* ------------------------------------------------------------------------ */
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001667static void udp4_format_sock(struct sock *sp, struct seq_file *f,
1668 int bucket, int *len)
David S. Millerdb8dac22008-03-06 16:22:02 -08001669{
1670 struct inet_sock *inet = inet_sk(sp);
1671 __be32 dest = inet->daddr;
1672 __be32 src = inet->rcv_saddr;
1673 __u16 destp = ntohs(inet->dport);
1674 __u16 srcp = ntohs(inet->sport);
1675
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001676 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001677 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
David S. Millerdb8dac22008-03-06 16:22:02 -08001678 bucket, src, srcp, dest, destp, sp->sk_state,
1679 atomic_read(&sp->sk_wmem_alloc),
1680 atomic_read(&sp->sk_rmem_alloc),
1681 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001682 atomic_read(&sp->sk_refcnt), sp,
1683 atomic_read(&sp->sk_drops), len);
David S. Millerdb8dac22008-03-06 16:22:02 -08001684}
1685
1686int udp4_seq_show(struct seq_file *seq, void *v)
1687{
1688 if (v == SEQ_START_TOKEN)
1689 seq_printf(seq, "%-127s\n",
1690 " sl local_address rem_address st tx_queue "
1691 "rx_queue tr tm->when retrnsmt uid timeout "
Eric Dumazetcb61cb92008-06-17 21:04:56 -07001692 "inode ref pointer drops");
David S. Millerdb8dac22008-03-06 16:22:02 -08001693 else {
David S. Millerdb8dac22008-03-06 16:22:02 -08001694 struct udp_iter_state *state = seq->private;
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001695 int len;
David S. Millerdb8dac22008-03-06 16:22:02 -08001696
Pavel Emelyanov5e659e42008-04-24 01:02:16 -07001697 udp4_format_sock(v, seq, state->bucket, &len);
1698 seq_printf(seq, "%*s\n", 127 - len ,"");
David S. Millerdb8dac22008-03-06 16:22:02 -08001699 }
1700 return 0;
1701}
1702
1703/* ------------------------------------------------------------------------ */
David S. Millerdb8dac22008-03-06 16:22:02 -08001704static struct udp_seq_afinfo udp4_seq_afinfo = {
David S. Millerdb8dac22008-03-06 16:22:02 -08001705 .name = "udp",
1706 .family = AF_INET,
Eric Dumazet645ca702008-10-29 01:41:45 -07001707 .udp_table = &udp_table,
Denis V. Lunev4ad96d32008-03-28 18:25:53 -07001708 .seq_fops = {
1709 .owner = THIS_MODULE,
1710 },
Denis V. Lunevdda61922008-03-28 18:24:26 -07001711 .seq_ops = {
1712 .show = udp4_seq_show,
1713 },
David S. Millerdb8dac22008-03-06 16:22:02 -08001714};
1715
Pavel Emelyanov15439fe2008-03-24 14:53:49 -07001716static int udp4_proc_init_net(struct net *net)
1717{
1718 return udp_proc_register(net, &udp4_seq_afinfo);
1719}
1720
1721static void udp4_proc_exit_net(struct net *net)
1722{
1723 udp_proc_unregister(net, &udp4_seq_afinfo);
1724}
1725
1726static struct pernet_operations udp4_net_ops = {
1727 .init = udp4_proc_init_net,
1728 .exit = udp4_proc_exit_net,
1729};
1730
David S. Millerdb8dac22008-03-06 16:22:02 -08001731int __init udp4_proc_init(void)
1732{
Pavel Emelyanov15439fe2008-03-24 14:53:49 -07001733 return register_pernet_subsys(&udp4_net_ops);
David S. Millerdb8dac22008-03-06 16:22:02 -08001734}
1735
1736void udp4_proc_exit(void)
1737{
Pavel Emelyanov15439fe2008-03-24 14:53:49 -07001738 unregister_pernet_subsys(&udp4_net_ops);
David S. Millerdb8dac22008-03-06 16:22:02 -08001739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#endif /* CONFIG_PROC_FS */
1741
Eric Dumazet645ca702008-10-29 01:41:45 -07001742void __init udp_table_init(struct udp_table *table)
1743{
1744 int i;
1745
1746 for (i = 0; i < UDP_HTABLE_SIZE; i++) {
1747 INIT_HLIST_HEAD(&table->hash[i].head);
1748 spin_lock_init(&table->hash[i].lock);
1749 }
1750}
1751
Hideo Aoki95766ff2007-12-31 00:29:24 -08001752void __init udp_init(void)
1753{
1754 unsigned long limit;
1755
Eric Dumazet645ca702008-10-29 01:41:45 -07001756 udp_table_init(&udp_table);
Hideo Aoki95766ff2007-12-31 00:29:24 -08001757 /* Set the pressure threshold up by the same strategy of TCP. It is a
1758 * fraction of global memory that is up to 1/2 at 256 MB, decreasing
1759 * toward zero with the amount of memory, with a floor of 128 pages.
1760 */
1761 limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
1762 limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
1763 limit = max(limit, 128UL);
1764 sysctl_udp_mem[0] = limit / 4 * 3;
1765 sysctl_udp_mem[1] = limit;
1766 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
1767
1768 sysctl_udp_rmem_min = SK_MEM_QUANTUM;
1769 sysctl_udp_wmem_min = SK_MEM_QUANTUM;
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772EXPORT_SYMBOL(udp_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773EXPORT_SYMBOL(udp_ioctl);
David S. Millerdb8dac22008-03-06 16:22:02 -08001774EXPORT_SYMBOL(udp_prot);
1775EXPORT_SYMBOL(udp_sendmsg);
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001776EXPORT_SYMBOL(udp_lib_getsockopt);
1777EXPORT_SYMBOL(udp_lib_setsockopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778EXPORT_SYMBOL(udp_poll);
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -07001779EXPORT_SYMBOL(udp_lib_get_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781#ifdef CONFIG_PROC_FS
1782EXPORT_SYMBOL(udp_proc_register);
1783EXPORT_SYMBOL(udp_proc_unregister);
1784#endif