2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* INET An implementation of the TCP/IP protocol suite for the LINUX
|
|
|
|
* operating system. INET is implemented using the BSD Socket
|
|
|
|
* interface as the means of communication with the user level.
|
|
|
|
*
|
|
|
|
* The User Datagram Protocol (UDP).
|
|
|
|
*
|
|
|
|
* Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
|
|
|
|
*
|
2005-05-06 07:16:16 +08:00
|
|
|
* Authors: Ross Biro
|
2005-04-17 06:20:36 +08:00
|
|
|
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
|
|
|
|
* Arnt Gulbrandsen, <agulbra@nvg.unit.no>
|
|
|
|
* Alan Cox, <Alan.Cox@linux.org>
|
|
|
|
* Hirokazu Takahashi, <taka@valinux.co.jp>
|
|
|
|
*
|
|
|
|
* Fixes:
|
|
|
|
* Alan Cox : verify_area() calls
|
|
|
|
* Alan Cox : stopped close while in use off icmp
|
|
|
|
* messages. Not a fix but a botch that
|
|
|
|
* for udp at least is 'valid'.
|
|
|
|
* Alan Cox : Fixed icmp handling properly
|
|
|
|
* Alan Cox : Correct error for oversized datagrams
|
2007-02-09 22:24:47 +08:00
|
|
|
* Alan Cox : Tidied select() semantics.
|
|
|
|
* Alan Cox : udp_err() fixed properly, also now
|
2005-04-17 06:20:36 +08:00
|
|
|
* select and read wake correctly on errors
|
|
|
|
* Alan Cox : udp_send verify_area moved to avoid mem leak
|
|
|
|
* Alan Cox : UDP can count its memory
|
|
|
|
* Alan Cox : send to an unknown connection causes
|
|
|
|
* an ECONNREFUSED off the icmp, but
|
|
|
|
* does NOT close.
|
|
|
|
* Alan Cox : Switched to new sk_buff handlers. No more backlog!
|
|
|
|
* Alan Cox : Using generic datagram code. Even smaller and the PEEK
|
|
|
|
* bug no longer crashes it.
|
|
|
|
* Fred Van Kempen : Net2e support for sk->broadcast.
|
|
|
|
* Alan Cox : Uses skb_free_datagram
|
|
|
|
* Alan Cox : Added get/set sockopt support.
|
|
|
|
* Alan Cox : Broadcasting without option set returns EACCES.
|
|
|
|
* Alan Cox : No wakeup calls. Instead we now use the callbacks.
|
|
|
|
* Alan Cox : Use ip_tos and ip_ttl
|
|
|
|
* Alan Cox : SNMP Mibs
|
|
|
|
* Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
|
|
|
|
* Matt Dillon : UDP length checks.
|
|
|
|
* Alan Cox : Smarter af_inet used properly.
|
|
|
|
* Alan Cox : Use new kernel side addressing.
|
|
|
|
* Alan Cox : Incorrect return on truncated datagram receive.
|
|
|
|
* Arnt Gulbrandsen : New udp_send and stuff
|
|
|
|
* Alan Cox : Cache last socket
|
|
|
|
* Alan Cox : Route cache
|
|
|
|
* Jon Peatfield : Minor efficiency fix to sendto().
|
|
|
|
* Mike Shaver : RFC1122 checks.
|
|
|
|
* Alan Cox : Nonblocking error fix.
|
|
|
|
* Willy Konynenberg : Transparent proxying support.
|
|
|
|
* Mike McLagan : Routing by source
|
|
|
|
* David S. Miller : New socket lookup architecture.
|
|
|
|
* Last socket cache retained as it
|
|
|
|
* does have a high hit rate.
|
|
|
|
* Olaf Kirch : Don't linearise iovec on sendmsg.
|
|
|
|
* Andi Kleen : Some cleanups, cache destination entry
|
2007-02-09 22:24:47 +08:00
|
|
|
* for connect.
|
2005-04-17 06:20:36 +08:00
|
|
|
* Vitaly E. Lavrov : Transparent proxy revived after year coma.
|
|
|
|
* Melvin Smith : Check msg_name not msg_namelen in sendto(),
|
|
|
|
* return ENOTCONN for unconnected sockets (POSIX)
|
|
|
|
* Janos Farkas : don't deliver multi/broadcasts to a different
|
|
|
|
* bound-to-device socket
|
|
|
|
* Hirokazu Takahashi : HW checksumming for outgoing UDP
|
|
|
|
* datagrams.
|
|
|
|
* Hirokazu Takahashi : sendfile() on UDP works now.
|
|
|
|
* Arnaldo C. Melo : convert /proc/net/udp to seq_file
|
|
|
|
* YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
|
|
|
|
* Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
|
|
|
|
* a single port at the same time.
|
|
|
|
* Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
|
2007-06-28 06:37:46 +08:00
|
|
|
* James Chapman : Add L2TP encapsulation type.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
#include <asm/ioctls.h>
|
2007-12-31 16:29:24 +08:00
|
|
|
#include <linux/bootmem.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/sockios.h>
|
2005-12-27 12:43:12 +08:00
|
|
|
#include <linux/igmp.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/inet.h>
|
|
|
|
#include <linux/netdevice.h>
|
2005-08-10 11:08:28 +08:00
|
|
|
#include <net/tcp_states.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/seq_file.h>
|
2007-09-12 18:01:34 +08:00
|
|
|
#include <net/net_namespace.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/icmp.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/xfrm.h>
|
2006-11-28 03:10:57 +08:00
|
|
|
#include "udp_impl.h"
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Snmp MIB for the UDP layer
|
|
|
|
*/
|
|
|
|
|
2005-08-27 03:05:31 +08:00
|
|
|
DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly;
|
2007-12-12 03:30:32 +08:00
|
|
|
EXPORT_SYMBOL(udp_statistics);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-12-15 03:25:26 +08:00
|
|
|
DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
|
|
|
|
EXPORT_SYMBOL(udp_stats_in6);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hlist_head udp_hash[UDP_HTABLE_SIZE];
|
|
|
|
DEFINE_RWLOCK(udp_hash_lock);
|
|
|
|
|
2007-12-31 16:29:24 +08:00
|
|
|
int sysctl_udp_mem[3] __read_mostly;
|
|
|
|
int sysctl_udp_rmem_min __read_mostly;
|
|
|
|
int sysctl_udp_wmem_min __read_mostly;
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(sysctl_udp_mem);
|
|
|
|
EXPORT_SYMBOL(sysctl_udp_rmem_min);
|
|
|
|
EXPORT_SYMBOL(sysctl_udp_wmem_min);
|
|
|
|
|
|
|
|
atomic_t udp_memory_allocated;
|
|
|
|
EXPORT_SYMBOL(udp_memory_allocated);
|
|
|
|
|
2008-01-31 21:07:57 +08:00
|
|
|
static inline int __udp_lib_lport_inuse(struct net *net, __u16 num,
|
2007-08-25 14:09:41 +08:00
|
|
|
const struct hlist_head udptable[])
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-27 11:06:05 +08:00
|
|
|
struct sock *sk;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hlist_node *node;
|
2006-08-27 11:06:05 +08:00
|
|
|
|
2007-06-06 06:18:43 +08:00
|
|
|
sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)])
|
2008-01-31 21:07:57 +08:00
|
|
|
if (sk->sk_net == net && sk->sk_hash == num)
|
2006-08-27 11:06:05 +08:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-11-28 03:10:57 +08:00
|
|
|
* __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
|
2006-08-27 11:06:05 +08:00
|
|
|
*
|
|
|
|
* @sk: socket struct in question
|
|
|
|
* @snum: port number to look up
|
2006-11-28 03:10:57 +08:00
|
|
|
* @udptable: hash list table, must be of UDP_HTABLE_SIZE
|
2007-06-06 06:18:43 +08:00
|
|
|
* @saddr_comp: AF-dependent comparison of bound local IP addresses
|
2006-08-27 11:06:05 +08:00
|
|
|
*/
|
2006-11-28 03:10:57 +08:00
|
|
|
int __udp_lib_get_port(struct sock *sk, unsigned short snum,
|
2007-08-25 14:09:41 +08:00
|
|
|
struct hlist_head udptable[],
|
2007-06-06 06:18:43 +08:00
|
|
|
int (*saddr_comp)(const struct sock *sk1,
|
|
|
|
const struct sock *sk2 ) )
|
2006-08-27 11:06:05 +08:00
|
|
|
{
|
|
|
|
struct hlist_node *node;
|
|
|
|
struct hlist_head *head;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct sock *sk2;
|
2006-08-27 11:06:05 +08:00
|
|
|
int error = 1;
|
2008-01-31 21:07:57 +08:00
|
|
|
struct net *net = sk->sk_net;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
write_lock_bh(&udp_hash_lock);
|
2007-08-25 14:09:41 +08:00
|
|
|
|
|
|
|
if (!snum) {
|
2007-10-19 13:00:17 +08:00
|
|
|
int i, low, high, remaining;
|
2007-08-25 14:09:41 +08:00
|
|
|
unsigned rover, best, best_size_so_far;
|
|
|
|
|
2007-10-11 08:30:46 +08:00
|
|
|
inet_get_local_port_range(&low, &high);
|
2007-10-19 13:00:17 +08:00
|
|
|
remaining = (high - low) + 1;
|
2007-10-11 08:30:46 +08:00
|
|
|
|
2007-08-25 14:09:41 +08:00
|
|
|
best_size_so_far = UINT_MAX;
|
2007-10-19 13:00:17 +08:00
|
|
|
best = rover = net_random() % remaining + low;
|
2007-08-25 14:09:41 +08:00
|
|
|
|
|
|
|
/* 1st pass: look for empty (or shortest) hash chain */
|
|
|
|
for (i = 0; i < UDP_HTABLE_SIZE; i++) {
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
head = &udptable[rover & (UDP_HTABLE_SIZE - 1)];
|
|
|
|
if (hlist_empty(head))
|
2005-04-17 06:20:36 +08:00
|
|
|
goto gotit;
|
2007-08-25 14:09:41 +08:00
|
|
|
|
2006-12-23 03:42:26 +08:00
|
|
|
sk_for_each(sk2, node, head) {
|
|
|
|
if (++size >= best_size_so_far)
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
best_size_so_far = size;
|
2007-08-25 14:09:41 +08:00
|
|
|
best = rover;
|
2006-12-23 03:42:26 +08:00
|
|
|
next:
|
2007-08-25 14:09:41 +08:00
|
|
|
/* fold back if end of range */
|
|
|
|
if (++rover > high)
|
|
|
|
rover = low + ((rover - low)
|
|
|
|
& (UDP_HTABLE_SIZE - 1));
|
|
|
|
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-08-25 14:09:41 +08:00
|
|
|
|
|
|
|
/* 2nd pass: find hole in shortest hash chain */
|
|
|
|
rover = best;
|
|
|
|
for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) {
|
2008-01-31 21:07:57 +08:00
|
|
|
if (! __udp_lib_lport_inuse(net, rover, udptable))
|
2007-08-25 14:09:41 +08:00
|
|
|
goto gotit;
|
|
|
|
rover += UDP_HTABLE_SIZE;
|
|
|
|
if (rover > high)
|
|
|
|
rover = low + ((rover - low)
|
|
|
|
& (UDP_HTABLE_SIZE - 1));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-08-25 14:09:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* All ports in use! */
|
|
|
|
goto fail;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
gotit:
|
2007-08-25 14:09:41 +08:00
|
|
|
snum = rover;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2007-06-06 06:18:43 +08:00
|
|
|
head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
|
2006-08-27 11:06:05 +08:00
|
|
|
|
|
|
|
sk_for_each(sk2, node, head)
|
2007-06-06 06:18:43 +08:00
|
|
|
if (sk2->sk_hash == snum &&
|
|
|
|
sk2 != sk &&
|
2008-01-31 21:07:57 +08:00
|
|
|
sk2->sk_net == net &&
|
2007-06-06 06:18:43 +08:00
|
|
|
(!sk2->sk_reuse || !sk->sk_reuse) &&
|
|
|
|
(!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
|
|
|
|
|| sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
|
|
|
|
(*saddr_comp)(sk, sk2) )
|
2005-04-17 06:20:36 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
2007-08-25 14:09:41 +08:00
|
|
|
|
2006-08-27 11:06:05 +08:00
|
|
|
inet_sk(sk)->num = snum;
|
2007-06-06 06:18:43 +08:00
|
|
|
sk->sk_hash = snum;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (sk_unhashed(sk)) {
|
2007-06-06 06:18:43 +08:00
|
|
|
head = &udptable[snum & (UDP_HTABLE_SIZE - 1)];
|
2006-08-27 11:06:05 +08:00
|
|
|
sk_add_node(sk, head);
|
2008-01-04 12:46:48 +08:00
|
|
|
sock_prot_inuse_add(sk->sk_prot, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-08-27 11:06:05 +08:00
|
|
|
error = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
fail:
|
|
|
|
write_unlock_bh(&udp_hash_lock);
|
2006-08-27 11:06:05 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2007-03-09 12:46:41 +08:00
|
|
|
int udp_get_port(struct sock *sk, unsigned short snum,
|
2007-06-06 06:18:43 +08:00
|
|
|
int (*scmp)(const struct sock *, const struct sock *))
|
2006-11-28 03:10:57 +08:00
|
|
|
{
|
2007-08-25 14:09:41 +08:00
|
|
|
return __udp_lib_get_port(sk, snum, udp_hash, scmp);
|
2006-11-28 03:10:57 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* IOCTL requests applicable to the UDP protocol
|
|
|
|
*/
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
|
|
|
|
{
|
2007-03-09 12:41:55 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case SIOCOUTQ:
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-03-09 12:41:55 +08:00
|
|
|
int amount = atomic_read(&sk->sk_wmem_alloc);
|
|
|
|
return put_user(amount, (int __user *)arg);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
case SIOCINQ:
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
unsigned long amount;
|
|
|
|
|
|
|
|
amount = 0;
|
|
|
|
spin_lock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
skb = skb_peek(&sk->sk_receive_queue);
|
|
|
|
if (skb != NULL) {
|
|
|
|
/*
|
|
|
|
* We will only return the amount
|
|
|
|
* of this packet since that is all
|
|
|
|
* that will be read.
|
|
|
|
*/
|
|
|
|
amount = skb->len - sizeof(struct udphdr);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:41:55 +08:00
|
|
|
spin_unlock_bh(&sk->sk_receive_queue.lock);
|
|
|
|
return put_user(amount, (int __user *)arg);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
default:
|
|
|
|
return -ENOIOCTLCMD;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-03-09 12:41:55 +08:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int udp_disconnect(struct sock *sk, int flags)
|
|
|
|
{
|
|
|
|
struct inet_sock *inet = inet_sk(sk);
|
|
|
|
/*
|
|
|
|
* 1003.1g - break association.
|
|
|
|
*/
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
sk->sk_state = TCP_CLOSE;
|
|
|
|
inet->daddr = 0;
|
|
|
|
inet->dport = 0;
|
|
|
|
sk->sk_bound_dev_if = 0;
|
|
|
|
if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
|
|
|
|
inet_reset_saddr(sk);
|
|
|
|
|
|
|
|
if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
|
|
|
|
sk->sk_prot->unhash(sk);
|
|
|
|
inet->sport = 0;
|
|
|
|
}
|
|
|
|
sk_dst_reset(sk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Socket option code for UDP
|
|
|
|
*/
|
2006-11-28 01:29:59 +08:00
|
|
|
int udp_lib_setsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, int optlen,
|
|
|
|
int (*push_pending_frames)(struct sock *))
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct udp_sock *up = udp_sk(sk);
|
|
|
|
int val;
|
|
|
|
int err = 0;
|
2008-03-01 00:06:47 +08:00
|
|
|
#ifdef CONFIG_IP_UDPLITE
|
2007-12-03 19:34:16 +08:00
|
|
|
int is_udplite = IS_UDPLITE(sk);
|
2008-03-01 00:06:47 +08:00
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
if (optlen<sizeof(int))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (get_user(val, (int __user *)optval))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
switch (optname) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case UDP_CORK:
|
|
|
|
if (val != 0) {
|
|
|
|
up->corkflag = 1;
|
|
|
|
} else {
|
|
|
|
up->corkflag = 0;
|
|
|
|
lock_sock(sk);
|
2006-11-28 01:29:59 +08:00
|
|
|
(*push_pending_frames)(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
release_sock(sk);
|
|
|
|
}
|
|
|
|
break;
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case UDP_ENCAP:
|
|
|
|
switch (val) {
|
|
|
|
case 0:
|
|
|
|
case UDP_ENCAP_ESPINUDP:
|
|
|
|
case UDP_ENCAP_ESPINUDP_NON_IKE:
|
2007-07-06 08:08:05 +08:00
|
|
|
up->encap_rcv = xfrm4_udp_encap_rcv;
|
|
|
|
/* FALLTHROUGH */
|
2007-06-28 06:37:46 +08:00
|
|
|
case UDP_ENCAP_L2TPINUDP:
|
2005-04-17 06:20:36 +08:00
|
|
|
up->encap_type = val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-03-01 00:06:47 +08:00
|
|
|
#ifdef CONFIG_IP_UDPLITE
|
2006-11-28 03:10:57 +08:00
|
|
|
/*
|
|
|
|
* UDP-Lite's partial checksum coverage (RFC 3828).
|
|
|
|
*/
|
|
|
|
/* The sender sets actual checksum coverage length via this option.
|
|
|
|
* The case coverage > packet length is handled by send module. */
|
|
|
|
case UDPLITE_SEND_CSCOV:
|
2007-12-03 19:34:16 +08:00
|
|
|
if (!is_udplite) /* Disable the option on UDP sockets */
|
2006-11-28 03:10:57 +08:00
|
|
|
return -ENOPROTOOPT;
|
|
|
|
if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
|
|
|
|
val = 8;
|
|
|
|
up->pcslen = val;
|
|
|
|
up->pcflag |= UDPLITE_SEND_CC;
|
|
|
|
break;
|
|
|
|
|
2007-02-09 22:24:47 +08:00
|
|
|
/* The receiver specifies a minimum checksum coverage value. To make
|
|
|
|
* sense, this should be set to at least 8 (as done below). If zero is
|
2006-11-28 03:10:57 +08:00
|
|
|
* used, this again means full checksum coverage. */
|
|
|
|
case UDPLITE_RECV_CSCOV:
|
2007-12-03 19:34:16 +08:00
|
|
|
if (!is_udplite) /* Disable the option on UDP sockets */
|
2006-11-28 03:10:57 +08:00
|
|
|
return -ENOPROTOOPT;
|
|
|
|
if (val != 0 && val < 8) /* Avoid silly minimal values. */
|
|
|
|
val = 8;
|
|
|
|
up->pcrlen = val;
|
|
|
|
up->pcflag |= UDPLITE_RECV_CC;
|
|
|
|
break;
|
2008-03-01 00:06:47 +08:00
|
|
|
#endif
|
2006-11-28 03:10:57 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
break;
|
2007-03-09 12:41:55 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-11-28 01:29:59 +08:00
|
|
|
int udp_lib_getsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct udp_sock *up = udp_sk(sk);
|
|
|
|
int val, len;
|
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
if (get_user(len,optlen))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
len = min_t(unsigned int, len, sizeof(int));
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
if (len < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
switch (optname) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case UDP_CORK:
|
|
|
|
val = up->corkflag;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UDP_ENCAP:
|
|
|
|
val = up->encap_type;
|
|
|
|
break;
|
|
|
|
|
2006-11-28 03:10:57 +08:00
|
|
|
/* The following two cannot be changed on UDP sockets, the return is
|
|
|
|
* always 0 (which corresponds to the full checksum coverage of UDP). */
|
|
|
|
case UDPLITE_SEND_CSCOV:
|
|
|
|
val = up->pcslen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UDPLITE_RECV_CSCOV:
|
|
|
|
val = up->pcrlen;
|
|
|
|
break;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
return -ENOPROTOOPT;
|
2007-03-09 12:41:55 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-09 12:41:55 +08:00
|
|
|
if (put_user(len, optlen))
|
2007-02-09 22:24:47 +08:00
|
|
|
return -EFAULT;
|
2007-03-09 12:41:55 +08:00
|
|
|
if (copy_to_user(optval, &val,len))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EFAULT;
|
2007-02-09 22:24:47 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* udp_poll - wait for a UDP event.
|
|
|
|
* @file - file struct
|
|
|
|
* @sock - socket
|
|
|
|
* @wait - poll table
|
|
|
|
*
|
2007-02-09 22:24:47 +08:00
|
|
|
* This is same as datagram poll, except for the special case of
|
2005-04-17 06:20:36 +08:00
|
|
|
* blocking sockets. If application is using a blocking fd
|
|
|
|
* and a packet with checksum error is in the queue;
|
|
|
|
* then it could get return from select indicating data available
|
|
|
|
* but then block when reading it. Add special case code
|
|
|
|
* to work around these arguably broken applications.
|
|
|
|
*/
|
|
|
|
unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
|
|
|
|
{
|
|
|
|
unsigned int mask = datagram_poll(file, sock, wait);
|
|
|
|
struct sock *sk = sock->sk;
|
2006-11-28 03:10:57 +08:00
|
|
|
int is_lite = IS_UDPLITE(sk);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Check for false positives due to checksum errors */
|
|
|
|
if ( (mask & POLLRDNORM) &&
|
|
|
|
!(file->f_flags & O_NONBLOCK) &&
|
|
|
|
!(sk->sk_shutdown & RCV_SHUTDOWN)){
|
|
|
|
struct sk_buff_head *rcvq = &sk->sk_receive_queue;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2005-05-31 06:50:15 +08:00
|
|
|
spin_lock_bh(&rcvq->lock);
|
2007-03-26 11:10:56 +08:00
|
|
|
while ((skb = skb_peek(rcvq)) != NULL &&
|
|
|
|
udp_lib_checksum_complete(skb)) {
|
|
|
|
UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_lite);
|
|
|
|
__skb_unlink(skb, rcvq);
|
|
|
|
kfree_skb(skb);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-05-31 06:50:15 +08:00
|
|
|
spin_unlock_bh(&rcvq->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* nothing to see, move along */
|
|
|
|
if (skb == NULL)
|
|
|
|
mask &= ~(POLLIN | POLLRDNORM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
2007-02-09 22:24:47 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
|
|
|
|
static struct sock *udp_get_first(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
struct udp_iter_state *state = seq->private;
|
|
|
|
|
|
|
|
for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
|
|
|
|
struct hlist_node *node;
|
2006-11-28 03:10:57 +08:00
|
|
|
sk_for_each(sk, node, state->hashtable + state->bucket) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (sk->sk_family == state->family)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sk = NULL;
|
|
|
|
found:
|
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
|
|
|
|
{
|
|
|
|
struct udp_iter_state *state = seq->private;
|
|
|
|
|
|
|
|
do {
|
|
|
|
sk = sk_next(sk);
|
|
|
|
try_again:
|
|
|
|
;
|
|
|
|
} while (sk && sk->sk_family != state->family);
|
|
|
|
|
|
|
|
if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
|
2006-11-28 03:10:57 +08:00
|
|
|
sk = sk_head(state->hashtable + state->bucket);
|
2005-04-17 06:20:36 +08:00
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct sock *sk = udp_get_first(seq);
|
|
|
|
|
|
|
|
if (sk)
|
2007-03-09 12:41:55 +08:00
|
|
|
while (pos && (sk = udp_get_next(seq, sk)) != NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
--pos;
|
|
|
|
return pos ? NULL : sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
|
2008-01-02 13:58:02 +08:00
|
|
|
__acquires(udp_hash_lock)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
read_lock(&udp_hash_lock);
|
|
|
|
return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
|
|
|
|
if (v == (void *)1)
|
|
|
|
sk = udp_get_idx(seq, 0);
|
|
|
|
else
|
|
|
|
sk = udp_get_next(seq, v);
|
|
|
|
|
|
|
|
++*pos;
|
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void udp_seq_stop(struct seq_file *seq, void *v)
|
2008-01-02 13:58:02 +08:00
|
|
|
__releases(udp_hash_lock)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
read_unlock(&udp_hash_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int udp_seq_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct udp_seq_afinfo *afinfo = PDE(inode)->data;
|
|
|
|
struct seq_file *seq;
|
|
|
|
int rc = -ENOMEM;
|
2006-07-22 05:51:30 +08:00
|
|
|
struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
goto out;
|
|
|
|
s->family = afinfo->family;
|
2006-11-28 03:10:57 +08:00
|
|
|
s->hashtable = afinfo->hashtable;
|
2005-04-17 06:20:36 +08:00
|
|
|
s->seq_ops.start = udp_seq_start;
|
|
|
|
s->seq_ops.next = udp_seq_next;
|
|
|
|
s->seq_ops.show = afinfo->seq_show;
|
|
|
|
s->seq_ops.stop = udp_seq_stop;
|
|
|
|
|
|
|
|
rc = seq_open(file, &s->seq_ops);
|
|
|
|
if (rc)
|
|
|
|
goto out_kfree;
|
|
|
|
|
|
|
|
seq = file->private_data;
|
|
|
|
seq->private = s;
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
out_kfree:
|
|
|
|
kfree(s);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
int udp_proc_register(struct udp_seq_afinfo *afinfo)
|
|
|
|
{
|
|
|
|
struct proc_dir_entry *p;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (!afinfo)
|
|
|
|
return -EINVAL;
|
|
|
|
afinfo->seq_fops->owner = afinfo->owner;
|
|
|
|
afinfo->seq_fops->open = udp_seq_open;
|
|
|
|
afinfo->seq_fops->read = seq_read;
|
|
|
|
afinfo->seq_fops->llseek = seq_lseek;
|
|
|
|
afinfo->seq_fops->release = seq_release_private;
|
|
|
|
|
2007-09-12 18:01:34 +08:00
|
|
|
p = proc_net_fops_create(&init_net, afinfo->name, S_IRUGO, afinfo->seq_fops);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (p)
|
|
|
|
p->data = afinfo;
|
|
|
|
else
|
|
|
|
rc = -ENOMEM;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
|
|
|
|
{
|
|
|
|
if (!afinfo)
|
|
|
|
return;
|
2007-09-12 18:01:34 +08:00
|
|
|
proc_net_remove(&init_net, afinfo->name);
|
2005-04-17 06:20:36 +08:00
|
|
|
memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PROC_FS */
|
|
|
|
|
2007-12-31 16:29:24 +08:00
|
|
|
void __init udp_init(void)
|
|
|
|
{
|
|
|
|
unsigned long limit;
|
|
|
|
|
|
|
|
/* Set the pressure threshold up by the same strategy of TCP. It is a
|
|
|
|
* fraction of global memory that is up to 1/2 at 256 MB, decreasing
|
|
|
|
* toward zero with the amount of memory, with a floor of 128 pages.
|
|
|
|
*/
|
|
|
|
limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
|
|
|
|
limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
|
|
|
|
limit = max(limit, 128UL);
|
|
|
|
sysctl_udp_mem[0] = limit / 4 * 3;
|
|
|
|
sysctl_udp_mem[1] = limit;
|
|
|
|
sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
|
|
|
|
|
|
|
|
sysctl_udp_rmem_min = SK_MEM_QUANTUM;
|
|
|
|
sysctl_udp_wmem_min = SK_MEM_QUANTUM;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
EXPORT_SYMBOL(udp_disconnect);
|
|
|
|
EXPORT_SYMBOL(udp_hash);
|
|
|
|
EXPORT_SYMBOL(udp_hash_lock);
|
|
|
|
EXPORT_SYMBOL(udp_ioctl);
|
2006-08-27 11:06:05 +08:00
|
|
|
EXPORT_SYMBOL(udp_get_port);
|
2006-11-28 01:29:59 +08:00
|
|
|
EXPORT_SYMBOL(udp_lib_getsockopt);
|
|
|
|
EXPORT_SYMBOL(udp_lib_setsockopt);
|
2005-04-17 06:20:36 +08:00
|
|
|
EXPORT_SYMBOL(udp_poll);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
EXPORT_SYMBOL(udp_proc_register);
|
|
|
|
EXPORT_SYMBOL(udp_proc_unregister);
|
|
|
|
#endif
|