Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * The User Datagram Protocol (UDP). |
| 7 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 8 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 10 | * Arnt Gulbrandsen, <agulbra@nvg.unit.no> |
| 11 | * Alan Cox, <Alan.Cox@linux.org> |
| 12 | * 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 21 | * Alan Cox : Tidied select() semantics. |
| 22 | * Alan Cox : udp_err() fixed properly, also now |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 57 | * for connect. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * 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 Chapman | 342f023 | 2007-06-27 15:37:46 -0700 | [diff] [blame] | 71 | * James Chapman : Add L2TP encapsulation type. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * |
| 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <asm/system.h> |
| 81 | #include <asm/uaccess.h> |
| 82 | #include <asm/ioctls.h> |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 83 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #include <linux/types.h> |
| 85 | #include <linux/fcntl.h> |
| 86 | #include <linux/module.h> |
| 87 | #include <linux/socket.h> |
| 88 | #include <linux/sockios.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 89 | #include <linux/igmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #include <linux/in.h> |
| 91 | #include <linux/errno.h> |
| 92 | #include <linux/timer.h> |
| 93 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include <linux/inet.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #include <linux/netdevice.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 96 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #include <linux/skbuff.h> |
| 98 | #include <linux/proc_fs.h> |
| 99 | #include <linux/seq_file.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 100 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #include <net/icmp.h> |
| 102 | #include <net/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | #include <net/checksum.h> |
| 104 | #include <net/xfrm.h> |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 105 | #include "udp_impl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Snmp MIB for the UDP layer |
| 109 | */ |
| 110 | |
Herbert Xu | 9055e05 | 2007-12-14 11:25:26 -0800 | [diff] [blame] | 111 | DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly; |
| 112 | EXPORT_SYMBOL(udp_stats_in6); |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | struct hlist_head udp_hash[UDP_HTABLE_SIZE]; |
| 115 | DEFINE_RWLOCK(udp_hash_lock); |
| 116 | |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 117 | int sysctl_udp_mem[3] __read_mostly; |
| 118 | int sysctl_udp_rmem_min __read_mostly; |
| 119 | int sysctl_udp_wmem_min __read_mostly; |
| 120 | |
| 121 | EXPORT_SYMBOL(sysctl_udp_mem); |
| 122 | EXPORT_SYMBOL(sysctl_udp_rmem_min); |
| 123 | EXPORT_SYMBOL(sysctl_udp_wmem_min); |
| 124 | |
| 125 | atomic_t udp_memory_allocated; |
| 126 | EXPORT_SYMBOL(udp_memory_allocated); |
| 127 | |
Pavel Emelyanov | fa4d3c6 | 2008-01-31 05:07:57 -0800 | [diff] [blame] | 128 | static inline int __udp_lib_lport_inuse(struct net *net, __u16 num, |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 129 | const struct hlist_head udptable[]) |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 130 | { |
| 131 | struct sock *sk; |
| 132 | struct hlist_node *node; |
| 133 | |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 134 | sk_for_each(sk, node, &udptable[udp_hashfn(net, num)]) |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 135 | if (net_eq(sock_net(sk), net) && sk->sk_hash == num) |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 136 | return 1; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /** |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 141 | * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 142 | * |
| 143 | * @sk: socket struct in question |
| 144 | * @snum: port number to look up |
David S. Miller | df2bc45 | 2007-06-05 15:18:43 -0700 | [diff] [blame] | 145 | * @saddr_comp: AF-dependent comparison of bound local IP addresses |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 146 | */ |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 147 | int udp_lib_get_port(struct sock *sk, unsigned short snum, |
David S. Miller | df2bc45 | 2007-06-05 15:18:43 -0700 | [diff] [blame] | 148 | int (*saddr_comp)(const struct sock *sk1, |
| 149 | const struct sock *sk2 ) ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 151 | struct hlist_head *udptable = sk->sk_prot->h.udp_hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | struct hlist_node *node; |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 153 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | struct sock *sk2; |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 155 | int error = 1; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 156 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | write_lock_bh(&udp_hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 160 | if (!snum) { |
Anton Arapov | a25de53 | 2007-10-18 22:00:17 -0700 | [diff] [blame] | 161 | int i, low, high, remaining; |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 162 | unsigned rover, best, best_size_so_far; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 164 | inet_get_local_port_range(&low, &high); |
Anton Arapov | a25de53 | 2007-10-18 22:00:17 -0700 | [diff] [blame] | 165 | remaining = (high - low) + 1; |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 166 | |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 167 | best_size_so_far = UINT_MAX; |
Anton Arapov | a25de53 | 2007-10-18 22:00:17 -0700 | [diff] [blame] | 168 | best = rover = net_random() % remaining + low; |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 169 | |
| 170 | /* 1st pass: look for empty (or shortest) hash chain */ |
| 171 | for (i = 0; i < UDP_HTABLE_SIZE; i++) { |
| 172 | int size = 0; |
| 173 | |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 174 | head = &udptable[udp_hashfn(net, rover)]; |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 175 | if (hlist_empty(head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | goto gotit; |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 177 | |
David S. Miller | 5c66870 | 2006-12-22 11:42:26 -0800 | [diff] [blame] | 178 | sk_for_each(sk2, node, head) { |
| 179 | if (++size >= best_size_so_far) |
| 180 | goto next; |
| 181 | } |
| 182 | best_size_so_far = size; |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 183 | best = rover; |
David S. Miller | 5c66870 | 2006-12-22 11:42:26 -0800 | [diff] [blame] | 184 | next: |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 185 | /* fold back if end of range */ |
| 186 | if (++rover > high) |
| 187 | rover = low + ((rover - low) |
| 188 | & (UDP_HTABLE_SIZE - 1)); |
| 189 | |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 192 | |
| 193 | /* 2nd pass: find hole in shortest hash chain */ |
| 194 | rover = best; |
| 195 | for (i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++) { |
Pavel Emelyanov | fa4d3c6 | 2008-01-31 05:07:57 -0800 | [diff] [blame] | 196 | if (! __udp_lib_lport_inuse(net, rover, udptable)) |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 197 | goto gotit; |
| 198 | rover += UDP_HTABLE_SIZE; |
| 199 | if (rover > high) |
| 200 | rover = low + ((rover - low) |
| 201 | & (UDP_HTABLE_SIZE - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 203 | |
| 204 | |
| 205 | /* All ports in use! */ |
| 206 | goto fail; |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | gotit: |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 209 | snum = rover; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } else { |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 211 | head = &udptable[udp_hashfn(net, snum)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 213 | sk_for_each(sk2, node, head) |
David S. Miller | df2bc45 | 2007-06-05 15:18:43 -0700 | [diff] [blame] | 214 | if (sk2->sk_hash == snum && |
| 215 | sk2 != sk && |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 216 | net_eq(sock_net(sk2), net) && |
David S. Miller | df2bc45 | 2007-06-05 15:18:43 -0700 | [diff] [blame] | 217 | (!sk2->sk_reuse || !sk->sk_reuse) && |
| 218 | (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if |
| 219 | || sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && |
| 220 | (*saddr_comp)(sk, sk2) ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Stephen Hemminger | 32c1da7 | 2007-08-24 23:09:41 -0700 | [diff] [blame] | 223 | |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 224 | inet_sk(sk)->num = snum; |
David S. Miller | df2bc45 | 2007-06-05 15:18:43 -0700 | [diff] [blame] | 225 | sk->sk_hash = snum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (sk_unhashed(sk)) { |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 227 | head = &udptable[udp_hashfn(net, snum)]; |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 228 | sk_add_node(sk, head); |
Pavel Emelyanov | c29a0bc | 2008-03-31 19:41:46 -0700 | [diff] [blame] | 229 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 231 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | fail: |
| 233 | write_unlock_bh(&udp_hash_lock); |
Gerrit Renker | 25030a7 | 2006-08-26 20:06:05 -0700 | [diff] [blame] | 234 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 237 | static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 238 | { |
| 239 | struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); |
| 240 | |
| 241 | return ( !ipv6_only_sock(sk2) && |
| 242 | (!inet1->rcv_saddr || !inet2->rcv_saddr || |
| 243 | inet1->rcv_saddr == inet2->rcv_saddr )); |
| 244 | } |
| 245 | |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 246 | int udp_v4_get_port(struct sock *sk, unsigned short snum) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 247 | { |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 248 | return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* UDP is nearly always wildcards out the wazoo, it makes no sense to try |
| 252 | * harder than this. -DaveM |
| 253 | */ |
| 254 | static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, |
| 255 | __be16 sport, __be32 daddr, __be16 dport, |
| 256 | int dif, struct hlist_head udptable[]) |
| 257 | { |
| 258 | struct sock *sk, *result = NULL; |
| 259 | struct hlist_node *node; |
| 260 | unsigned short hnum = ntohs(dport); |
| 261 | int badness = -1; |
| 262 | |
| 263 | read_lock(&udp_hash_lock); |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 264 | sk_for_each(sk, node, &udptable[udp_hashfn(net, hnum)]) { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 265 | struct inet_sock *inet = inet_sk(sk); |
| 266 | |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 267 | if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum && |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 268 | !ipv6_only_sock(sk)) { |
| 269 | int score = (sk->sk_family == PF_INET ? 1 : 0); |
| 270 | if (inet->rcv_saddr) { |
| 271 | if (inet->rcv_saddr != daddr) |
| 272 | continue; |
| 273 | score+=2; |
| 274 | } |
| 275 | if (inet->daddr) { |
| 276 | if (inet->daddr != saddr) |
| 277 | continue; |
| 278 | score+=2; |
| 279 | } |
| 280 | if (inet->dport) { |
| 281 | if (inet->dport != sport) |
| 282 | continue; |
| 283 | score+=2; |
| 284 | } |
| 285 | if (sk->sk_bound_dev_if) { |
| 286 | if (sk->sk_bound_dev_if != dif) |
| 287 | continue; |
| 288 | score+=2; |
| 289 | } |
| 290 | if (score == 9) { |
| 291 | result = sk; |
| 292 | break; |
| 293 | } else if (score > badness) { |
| 294 | result = sk; |
| 295 | badness = score; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | if (result) |
| 300 | sock_hold(result); |
| 301 | read_unlock(&udp_hash_lock); |
| 302 | return result; |
| 303 | } |
| 304 | |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 305 | static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, |
| 306 | __be16 sport, __be16 dport, |
| 307 | struct hlist_head udptable[]) |
| 308 | { |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame^] | 309 | struct sock *sk; |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 310 | const struct iphdr *iph = ip_hdr(skb); |
| 311 | |
KOVACS Krisztian | 2354261 | 2008-10-07 12:41:01 -0700 | [diff] [blame^] | 312 | if (unlikely(sk = skb_steal_sock(skb))) |
| 313 | return sk; |
| 314 | else |
| 315 | return __udp4_lib_lookup(dev_net(skb->dst->dev), iph->saddr, sport, |
| 316 | iph->daddr, dport, inet_iif(skb), |
| 317 | udptable); |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 318 | } |
| 319 | |
KOVACS Krisztian | bcd4130 | 2008-10-01 07:48:10 -0700 | [diff] [blame] | 320 | struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, |
| 321 | __be32 daddr, __be16 dport, int dif) |
| 322 | { |
| 323 | return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, udp_hash); |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(udp4_lib_lookup); |
| 326 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 327 | static inline struct sock *udp_v4_mcast_next(struct sock *sk, |
| 328 | __be16 loc_port, __be32 loc_addr, |
| 329 | __be16 rmt_port, __be32 rmt_addr, |
| 330 | int dif) |
| 331 | { |
| 332 | struct hlist_node *node; |
| 333 | struct sock *s = sk; |
| 334 | unsigned short hnum = ntohs(loc_port); |
| 335 | |
| 336 | sk_for_each_from(s, node) { |
| 337 | struct inet_sock *inet = inet_sk(s); |
| 338 | |
| 339 | if (s->sk_hash != hnum || |
| 340 | (inet->daddr && inet->daddr != rmt_addr) || |
| 341 | (inet->dport != rmt_port && inet->dport) || |
| 342 | (inet->rcv_saddr && inet->rcv_saddr != loc_addr) || |
| 343 | ipv6_only_sock(s) || |
| 344 | (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) |
| 345 | continue; |
| 346 | if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) |
| 347 | continue; |
| 348 | goto found; |
| 349 | } |
| 350 | s = NULL; |
| 351 | found: |
| 352 | return s; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * This routine is called by the ICMP module when it gets some |
| 357 | * sort of error condition. If err < 0 then the socket should |
| 358 | * be closed and the error returned to the user. If err > 0 |
| 359 | * it's just the icmp type << 8 | icmp code. |
| 360 | * Header points to the ip header of the error packet. We move |
| 361 | * on past this. Then (as it used to claim before adjustment) |
| 362 | * header points to the first 8 bytes of the udp header. We need |
| 363 | * to find the appropriate port. |
| 364 | */ |
| 365 | |
| 366 | void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) |
| 367 | { |
| 368 | struct inet_sock *inet; |
| 369 | struct iphdr *iph = (struct iphdr*)skb->data; |
| 370 | struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2)); |
| 371 | const int type = icmp_hdr(skb)->type; |
| 372 | const int code = icmp_hdr(skb)->code; |
| 373 | struct sock *sk; |
| 374 | int harderr; |
| 375 | int err; |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 376 | struct net *net = dev_net(skb->dev); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 377 | |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 378 | sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 379 | iph->saddr, uh->source, skb->dev->ifindex, udptable); |
| 380 | if (sk == NULL) { |
Pavel Emelyanov | dcfc23c | 2008-07-14 23:03:00 -0700 | [diff] [blame] | 381 | ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 382 | return; /* No socket for error */ |
| 383 | } |
| 384 | |
| 385 | err = 0; |
| 386 | harderr = 0; |
| 387 | inet = inet_sk(sk); |
| 388 | |
| 389 | switch (type) { |
| 390 | default: |
| 391 | case ICMP_TIME_EXCEEDED: |
| 392 | err = EHOSTUNREACH; |
| 393 | break; |
| 394 | case ICMP_SOURCE_QUENCH: |
| 395 | goto out; |
| 396 | case ICMP_PARAMETERPROB: |
| 397 | err = EPROTO; |
| 398 | harderr = 1; |
| 399 | break; |
| 400 | case ICMP_DEST_UNREACH: |
| 401 | if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ |
| 402 | if (inet->pmtudisc != IP_PMTUDISC_DONT) { |
| 403 | err = EMSGSIZE; |
| 404 | harderr = 1; |
| 405 | break; |
| 406 | } |
| 407 | goto out; |
| 408 | } |
| 409 | err = EHOSTUNREACH; |
| 410 | if (code <= NR_ICMP_UNREACH) { |
| 411 | harderr = icmp_err_convert[code].fatal; |
| 412 | err = icmp_err_convert[code].errno; |
| 413 | } |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * RFC1122: OK. Passes ICMP errors back to application, as per |
| 419 | * 4.1.3.3. |
| 420 | */ |
| 421 | if (!inet->recverr) { |
| 422 | if (!harderr || sk->sk_state != TCP_ESTABLISHED) |
| 423 | goto out; |
| 424 | } else { |
| 425 | ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1)); |
| 426 | } |
| 427 | sk->sk_err = err; |
| 428 | sk->sk_error_report(sk); |
| 429 | out: |
| 430 | sock_put(sk); |
| 431 | } |
| 432 | |
| 433 | void udp_err(struct sk_buff *skb, u32 info) |
| 434 | { |
| 435 | __udp4_lib_err(skb, info, udp_hash); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Throw away all pending data and cancel the corking. Socket is locked. |
| 440 | */ |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 441 | void udp_flush_pending_frames(struct sock *sk) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 442 | { |
| 443 | struct udp_sock *up = udp_sk(sk); |
| 444 | |
| 445 | if (up->pending) { |
| 446 | up->len = 0; |
| 447 | up->pending = 0; |
| 448 | ip_flush_pending_frames(sk); |
| 449 | } |
| 450 | } |
Denis V. Lunev | 36d926b | 2008-06-04 15:49:07 +0400 | [diff] [blame] | 451 | EXPORT_SYMBOL(udp_flush_pending_frames); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * udp4_hwcsum_outgoing - handle outgoing HW checksumming |
| 455 | * @sk: socket we are sending on |
| 456 | * @skb: sk_buff containing the filled-in UDP header |
| 457 | * (checksum field must be zeroed out) |
| 458 | */ |
| 459 | static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, |
| 460 | __be32 src, __be32 dst, int len ) |
| 461 | { |
| 462 | unsigned int offset; |
| 463 | struct udphdr *uh = udp_hdr(skb); |
| 464 | __wsum csum = 0; |
| 465 | |
| 466 | if (skb_queue_len(&sk->sk_write_queue) == 1) { |
| 467 | /* |
| 468 | * Only one fragment on the socket. |
| 469 | */ |
| 470 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 471 | skb->csum_offset = offsetof(struct udphdr, check); |
| 472 | uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0); |
| 473 | } else { |
| 474 | /* |
| 475 | * HW-checksum won't work as there are two or more |
| 476 | * fragments on the socket so that all csums of sk_buffs |
| 477 | * should be together |
| 478 | */ |
| 479 | offset = skb_transport_offset(skb); |
| 480 | skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); |
| 481 | |
| 482 | skb->ip_summed = CHECKSUM_NONE; |
| 483 | |
| 484 | skb_queue_walk(&sk->sk_write_queue, skb) { |
| 485 | csum = csum_add(csum, skb->csum); |
| 486 | } |
| 487 | |
| 488 | uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); |
| 489 | if (uh->check == 0) |
| 490 | uh->check = CSUM_MANGLED_0; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Push out all pending data as one UDP datagram. Socket is locked. |
| 496 | */ |
| 497 | static int udp_push_pending_frames(struct sock *sk) |
| 498 | { |
| 499 | struct udp_sock *up = udp_sk(sk); |
| 500 | struct inet_sock *inet = inet_sk(sk); |
| 501 | struct flowi *fl = &inet->cork.fl; |
| 502 | struct sk_buff *skb; |
| 503 | struct udphdr *uh; |
| 504 | int err = 0; |
| 505 | int is_udplite = IS_UDPLITE(sk); |
| 506 | __wsum csum = 0; |
| 507 | |
| 508 | /* Grab the skbuff where UDP header space exists. */ |
| 509 | if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) |
| 510 | goto out; |
| 511 | |
| 512 | /* |
| 513 | * Create a UDP header |
| 514 | */ |
| 515 | uh = udp_hdr(skb); |
| 516 | uh->source = fl->fl_ip_sport; |
| 517 | uh->dest = fl->fl_ip_dport; |
| 518 | uh->len = htons(up->len); |
| 519 | uh->check = 0; |
| 520 | |
| 521 | if (is_udplite) /* UDP-Lite */ |
| 522 | csum = udplite_csum_outgoing(sk, skb); |
| 523 | |
| 524 | else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ |
| 525 | |
| 526 | skb->ip_summed = CHECKSUM_NONE; |
| 527 | goto send; |
| 528 | |
| 529 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ |
| 530 | |
| 531 | udp4_hwcsum_outgoing(sk, skb, fl->fl4_src,fl->fl4_dst, up->len); |
| 532 | goto send; |
| 533 | |
| 534 | } else /* `normal' UDP */ |
| 535 | csum = udp_csum_outgoing(sk, skb); |
| 536 | |
| 537 | /* add protocol-dependent pseudo-header */ |
| 538 | uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len, |
| 539 | sk->sk_protocol, csum ); |
| 540 | if (uh->check == 0) |
| 541 | uh->check = CSUM_MANGLED_0; |
| 542 | |
| 543 | send: |
| 544 | err = ip_push_pending_frames(sk); |
| 545 | out: |
| 546 | up->len = 0; |
| 547 | up->pending = 0; |
| 548 | if (!err) |
Pavel Emelyanov | 629ca23 | 2008-07-05 21:18:07 -0700 | [diff] [blame] | 549 | UDP_INC_STATS_USER(sock_net(sk), |
| 550 | UDP_MIB_OUTDATAGRAMS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 551 | return err; |
| 552 | } |
| 553 | |
| 554 | int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
| 555 | size_t len) |
| 556 | { |
| 557 | struct inet_sock *inet = inet_sk(sk); |
| 558 | struct udp_sock *up = udp_sk(sk); |
| 559 | int ulen = len; |
| 560 | struct ipcm_cookie ipc; |
| 561 | struct rtable *rt = NULL; |
| 562 | int free = 0; |
| 563 | int connected = 0; |
| 564 | __be32 daddr, faddr, saddr; |
| 565 | __be16 dport; |
| 566 | u8 tos; |
| 567 | int err, is_udplite = IS_UDPLITE(sk); |
| 568 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; |
| 569 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); |
| 570 | |
| 571 | if (len > 0xFFFF) |
| 572 | return -EMSGSIZE; |
| 573 | |
| 574 | /* |
| 575 | * Check the flags. |
| 576 | */ |
| 577 | |
| 578 | if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */ |
| 579 | return -EOPNOTSUPP; |
| 580 | |
| 581 | ipc.opt = NULL; |
| 582 | |
| 583 | if (up->pending) { |
| 584 | /* |
| 585 | * There are pending frames. |
| 586 | * The socket lock must be held while it's corked. |
| 587 | */ |
| 588 | lock_sock(sk); |
| 589 | if (likely(up->pending)) { |
| 590 | if (unlikely(up->pending != AF_INET)) { |
| 591 | release_sock(sk); |
| 592 | return -EINVAL; |
| 593 | } |
| 594 | goto do_append_data; |
| 595 | } |
| 596 | release_sock(sk); |
| 597 | } |
| 598 | ulen += sizeof(struct udphdr); |
| 599 | |
| 600 | /* |
| 601 | * Get and verify the address. |
| 602 | */ |
| 603 | if (msg->msg_name) { |
| 604 | struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name; |
| 605 | if (msg->msg_namelen < sizeof(*usin)) |
| 606 | return -EINVAL; |
| 607 | if (usin->sin_family != AF_INET) { |
| 608 | if (usin->sin_family != AF_UNSPEC) |
| 609 | return -EAFNOSUPPORT; |
| 610 | } |
| 611 | |
| 612 | daddr = usin->sin_addr.s_addr; |
| 613 | dport = usin->sin_port; |
| 614 | if (dport == 0) |
| 615 | return -EINVAL; |
| 616 | } else { |
| 617 | if (sk->sk_state != TCP_ESTABLISHED) |
| 618 | return -EDESTADDRREQ; |
| 619 | daddr = inet->daddr; |
| 620 | dport = inet->dport; |
| 621 | /* Open fast path for connected socket. |
| 622 | Route will not be used, if at least one option is set. |
| 623 | */ |
| 624 | connected = 1; |
| 625 | } |
| 626 | ipc.addr = inet->saddr; |
| 627 | |
| 628 | ipc.oif = sk->sk_bound_dev_if; |
| 629 | if (msg->msg_controllen) { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 630 | err = ip_cmsg_send(sock_net(sk), msg, &ipc); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 631 | if (err) |
| 632 | return err; |
| 633 | if (ipc.opt) |
| 634 | free = 1; |
| 635 | connected = 0; |
| 636 | } |
| 637 | if (!ipc.opt) |
| 638 | ipc.opt = inet->opt; |
| 639 | |
| 640 | saddr = ipc.addr; |
| 641 | ipc.addr = faddr = daddr; |
| 642 | |
| 643 | if (ipc.opt && ipc.opt->srr) { |
| 644 | if (!daddr) |
| 645 | return -EINVAL; |
| 646 | faddr = ipc.opt->faddr; |
| 647 | connected = 0; |
| 648 | } |
| 649 | tos = RT_TOS(inet->tos); |
| 650 | if (sock_flag(sk, SOCK_LOCALROUTE) || |
| 651 | (msg->msg_flags & MSG_DONTROUTE) || |
| 652 | (ipc.opt && ipc.opt->is_strictroute)) { |
| 653 | tos |= RTO_ONLINK; |
| 654 | connected = 0; |
| 655 | } |
| 656 | |
| 657 | if (ipv4_is_multicast(daddr)) { |
| 658 | if (!ipc.oif) |
| 659 | ipc.oif = inet->mc_index; |
| 660 | if (!saddr) |
| 661 | saddr = inet->mc_addr; |
| 662 | connected = 0; |
| 663 | } |
| 664 | |
| 665 | if (connected) |
| 666 | rt = (struct rtable*)sk_dst_check(sk, 0); |
| 667 | |
| 668 | if (rt == NULL) { |
| 669 | struct flowi fl = { .oif = ipc.oif, |
| 670 | .nl_u = { .ip4_u = |
| 671 | { .daddr = faddr, |
| 672 | .saddr = saddr, |
| 673 | .tos = tos } }, |
| 674 | .proto = sk->sk_protocol, |
| 675 | .uli_u = { .ports = |
| 676 | { .sport = inet->sport, |
| 677 | .dport = dport } } }; |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 678 | struct net *net = sock_net(sk); |
| 679 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 680 | security_sk_classify_flow(sk, &fl); |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 681 | err = ip_route_output_flow(net, &rt, &fl, sk, 1); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 682 | if (err) { |
| 683 | if (err == -ENETUNREACH) |
Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 684 | IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 685 | goto out; |
| 686 | } |
| 687 | |
| 688 | err = -EACCES; |
| 689 | if ((rt->rt_flags & RTCF_BROADCAST) && |
| 690 | !sock_flag(sk, SOCK_BROADCAST)) |
| 691 | goto out; |
| 692 | if (connected) |
| 693 | sk_dst_set(sk, dst_clone(&rt->u.dst)); |
| 694 | } |
| 695 | |
| 696 | if (msg->msg_flags&MSG_CONFIRM) |
| 697 | goto do_confirm; |
| 698 | back_from_confirm: |
| 699 | |
| 700 | saddr = rt->rt_src; |
| 701 | if (!ipc.addr) |
| 702 | daddr = ipc.addr = rt->rt_dst; |
| 703 | |
| 704 | lock_sock(sk); |
| 705 | if (unlikely(up->pending)) { |
| 706 | /* The socket is already corked while preparing it. */ |
| 707 | /* ... which is an evident application bug. --ANK */ |
| 708 | release_sock(sk); |
| 709 | |
| 710 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); |
| 711 | err = -EINVAL; |
| 712 | goto out; |
| 713 | } |
| 714 | /* |
| 715 | * Now cork the socket to pend data. |
| 716 | */ |
| 717 | inet->cork.fl.fl4_dst = daddr; |
| 718 | inet->cork.fl.fl_ip_dport = dport; |
| 719 | inet->cork.fl.fl4_src = saddr; |
| 720 | inet->cork.fl.fl_ip_sport = inet->sport; |
| 721 | up->pending = AF_INET; |
| 722 | |
| 723 | do_append_data: |
| 724 | up->len += ulen; |
| 725 | getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; |
| 726 | err = ip_append_data(sk, getfrag, msg->msg_iov, ulen, |
| 727 | sizeof(struct udphdr), &ipc, rt, |
| 728 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); |
| 729 | if (err) |
| 730 | udp_flush_pending_frames(sk); |
| 731 | else if (!corkreq) |
| 732 | err = udp_push_pending_frames(sk); |
| 733 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) |
| 734 | up->pending = 0; |
| 735 | release_sock(sk); |
| 736 | |
| 737 | out: |
| 738 | ip_rt_put(rt); |
| 739 | if (free) |
| 740 | kfree(ipc.opt); |
| 741 | if (!err) |
| 742 | return len; |
| 743 | /* |
| 744 | * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting |
| 745 | * ENOBUFS might not be good (it's not tunable per se), but otherwise |
| 746 | * we don't have a good statistic (IpOutDiscards but it can be too many |
| 747 | * things). We could add another new stat but at least for now that |
| 748 | * seems like overkill. |
| 749 | */ |
| 750 | if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { |
Pavel Emelyanov | 629ca23 | 2008-07-05 21:18:07 -0700 | [diff] [blame] | 751 | UDP_INC_STATS_USER(sock_net(sk), |
| 752 | UDP_MIB_SNDBUFERRORS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 753 | } |
| 754 | return err; |
| 755 | |
| 756 | do_confirm: |
| 757 | dst_confirm(&rt->u.dst); |
| 758 | if (!(msg->msg_flags&MSG_PROBE) || len) |
| 759 | goto back_from_confirm; |
| 760 | err = 0; |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | int udp_sendpage(struct sock *sk, struct page *page, int offset, |
| 765 | size_t size, int flags) |
| 766 | { |
| 767 | struct udp_sock *up = udp_sk(sk); |
| 768 | int ret; |
| 769 | |
| 770 | if (!up->pending) { |
| 771 | struct msghdr msg = { .msg_flags = flags|MSG_MORE }; |
| 772 | |
| 773 | /* Call udp_sendmsg to specify destination address which |
| 774 | * sendpage interface can't pass. |
| 775 | * This will succeed only when the socket is connected. |
| 776 | */ |
| 777 | ret = udp_sendmsg(NULL, sk, &msg, 0); |
| 778 | if (ret < 0) |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | lock_sock(sk); |
| 783 | |
| 784 | if (unlikely(!up->pending)) { |
| 785 | release_sock(sk); |
| 786 | |
| 787 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | ret = ip_append_page(sk, page, offset, size, flags); |
| 792 | if (ret == -EOPNOTSUPP) { |
| 793 | release_sock(sk); |
| 794 | return sock_no_sendpage(sk->sk_socket, page, offset, |
| 795 | size, flags); |
| 796 | } |
| 797 | if (ret < 0) { |
| 798 | udp_flush_pending_frames(sk); |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | up->len += size; |
| 803 | if (!(up->corkflag || (flags&MSG_MORE))) |
| 804 | ret = udp_push_pending_frames(sk); |
| 805 | if (!ret) |
| 806 | ret = size; |
| 807 | out: |
| 808 | release_sock(sk); |
| 809 | return ret; |
| 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* |
| 813 | * IOCTL requests applicable to the UDP protocol |
| 814 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 817 | { |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 818 | switch (cmd) { |
| 819 | case SIOCOUTQ: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 821 | int amount = atomic_read(&sk->sk_wmem_alloc); |
| 822 | return put_user(amount, (int __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 824 | |
| 825 | case SIOCINQ: |
| 826 | { |
| 827 | struct sk_buff *skb; |
| 828 | unsigned long amount; |
| 829 | |
| 830 | amount = 0; |
| 831 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 832 | skb = skb_peek(&sk->sk_receive_queue); |
| 833 | if (skb != NULL) { |
| 834 | /* |
| 835 | * We will only return the amount |
| 836 | * of this packet since that is all |
| 837 | * that will be read. |
| 838 | */ |
| 839 | amount = skb->len - sizeof(struct udphdr); |
| 840 | } |
| 841 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 842 | return put_user(amount, (int __user *)arg); |
| 843 | } |
| 844 | |
| 845 | default: |
| 846 | return -ENOIOCTLCMD; |
| 847 | } |
| 848 | |
| 849 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 852 | /* |
| 853 | * This should be easy, if there is something there we |
| 854 | * return it, otherwise we block. |
| 855 | */ |
| 856 | |
| 857 | int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, |
| 858 | size_t len, int noblock, int flags, int *addr_len) |
| 859 | { |
| 860 | struct inet_sock *inet = inet_sk(sk); |
| 861 | struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; |
| 862 | struct sk_buff *skb; |
| 863 | unsigned int ulen, copied; |
| 864 | int peeked; |
| 865 | int err; |
| 866 | int is_udplite = IS_UDPLITE(sk); |
| 867 | |
| 868 | /* |
| 869 | * Check any passed addresses |
| 870 | */ |
| 871 | if (addr_len) |
| 872 | *addr_len=sizeof(*sin); |
| 873 | |
| 874 | if (flags & MSG_ERRQUEUE) |
| 875 | return ip_recv_error(sk, msg, len); |
| 876 | |
| 877 | try_again: |
| 878 | skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
| 879 | &peeked, &err); |
| 880 | if (!skb) |
| 881 | goto out; |
| 882 | |
| 883 | ulen = skb->len - sizeof(struct udphdr); |
| 884 | copied = len; |
| 885 | if (copied > ulen) |
| 886 | copied = ulen; |
| 887 | else if (copied < ulen) |
| 888 | msg->msg_flags |= MSG_TRUNC; |
| 889 | |
| 890 | /* |
| 891 | * If checksum is needed at all, try to do it while copying the |
| 892 | * data. If the data is truncated, or if we only want a partial |
| 893 | * coverage checksum (UDP-Lite), do it before the copy. |
| 894 | */ |
| 895 | |
| 896 | if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { |
| 897 | if (udp_lib_checksum_complete(skb)) |
| 898 | goto csum_copy_err; |
| 899 | } |
| 900 | |
| 901 | if (skb_csum_unnecessary(skb)) |
| 902 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), |
| 903 | msg->msg_iov, copied ); |
| 904 | else { |
| 905 | err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); |
| 906 | |
| 907 | if (err == -EINVAL) |
| 908 | goto csum_copy_err; |
| 909 | } |
| 910 | |
| 911 | if (err) |
| 912 | goto out_free; |
| 913 | |
| 914 | if (!peeked) |
Pavel Emelyanov | 629ca23 | 2008-07-05 21:18:07 -0700 | [diff] [blame] | 915 | UDP_INC_STATS_USER(sock_net(sk), |
| 916 | UDP_MIB_INDATAGRAMS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 917 | |
| 918 | sock_recv_timestamp(msg, sk, skb); |
| 919 | |
| 920 | /* Copy the address. */ |
| 921 | if (sin) |
| 922 | { |
| 923 | sin->sin_family = AF_INET; |
| 924 | sin->sin_port = udp_hdr(skb)->source; |
| 925 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 926 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 927 | } |
| 928 | if (inet->cmsg_flags) |
| 929 | ip_cmsg_recv(msg, skb); |
| 930 | |
| 931 | err = copied; |
| 932 | if (flags & MSG_TRUNC) |
| 933 | err = ulen; |
| 934 | |
| 935 | out_free: |
| 936 | lock_sock(sk); |
| 937 | skb_free_datagram(sk, skb); |
| 938 | release_sock(sk); |
| 939 | out: |
| 940 | return err; |
| 941 | |
| 942 | csum_copy_err: |
| 943 | lock_sock(sk); |
| 944 | if (!skb_kill_datagram(sk, skb, flags)) |
Pavel Emelyanov | 629ca23 | 2008-07-05 21:18:07 -0700 | [diff] [blame] | 945 | UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 946 | release_sock(sk); |
| 947 | |
| 948 | if (noblock) |
| 949 | return -EAGAIN; |
| 950 | goto try_again; |
| 951 | } |
| 952 | |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | int udp_disconnect(struct sock *sk, int flags) |
| 955 | { |
| 956 | struct inet_sock *inet = inet_sk(sk); |
| 957 | /* |
| 958 | * 1003.1g - break association. |
| 959 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | sk->sk_state = TCP_CLOSE; |
| 962 | inet->daddr = 0; |
| 963 | inet->dport = 0; |
| 964 | sk->sk_bound_dev_if = 0; |
| 965 | if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) |
| 966 | inet_reset_saddr(sk); |
| 967 | |
| 968 | if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { |
| 969 | sk->sk_prot->unhash(sk); |
| 970 | inet->sport = 0; |
| 971 | } |
| 972 | sk_dst_reset(sk); |
| 973 | return 0; |
| 974 | } |
| 975 | |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 976 | static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 977 | { |
| 978 | int is_udplite = IS_UDPLITE(sk); |
| 979 | int rc; |
| 980 | |
| 981 | if ((rc = sock_queue_rcv_skb(sk, skb)) < 0) { |
| 982 | /* Note that an ENOMEM error is charged twice */ |
| 983 | if (rc == -ENOMEM) |
| 984 | UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, |
| 985 | is_udplite); |
| 986 | goto drop; |
| 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | |
| 991 | drop: |
| 992 | UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
| 993 | kfree_skb(skb); |
| 994 | return -1; |
| 995 | } |
| 996 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 997 | /* returns: |
| 998 | * -1: error |
| 999 | * 0: success |
| 1000 | * >0: "udp encap" protocol resubmission |
| 1001 | * |
| 1002 | * Note that in the success and error cases, the skb is assumed to |
| 1003 | * have either been requeued or freed. |
| 1004 | */ |
| 1005 | int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) |
| 1006 | { |
| 1007 | struct udp_sock *up = udp_sk(sk); |
| 1008 | int rc; |
| 1009 | int is_udplite = IS_UDPLITE(sk); |
| 1010 | |
| 1011 | /* |
| 1012 | * Charge it to the socket, dropping if the queue is full. |
| 1013 | */ |
| 1014 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 1015 | goto drop; |
| 1016 | nf_reset(skb); |
| 1017 | |
| 1018 | if (up->encap_type) { |
| 1019 | /* |
| 1020 | * This is an encapsulation socket so pass the skb to |
| 1021 | * the socket's udp_encap_rcv() hook. Otherwise, just |
| 1022 | * fall through and pass this up the UDP socket. |
| 1023 | * up->encap_rcv() returns the following value: |
| 1024 | * =0 if skb was successfully passed to the encap |
| 1025 | * handler or was discarded by it. |
| 1026 | * >0 if skb should be passed on to UDP. |
| 1027 | * <0 if skb should be resubmitted as proto -N |
| 1028 | */ |
| 1029 | |
| 1030 | /* if we're overly short, let UDP handle it */ |
| 1031 | if (skb->len > sizeof(struct udphdr) && |
| 1032 | up->encap_rcv != NULL) { |
| 1033 | int ret; |
| 1034 | |
| 1035 | ret = (*up->encap_rcv)(sk, skb); |
| 1036 | if (ret <= 0) { |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1037 | UDP_INC_STATS_BH(sock_net(sk), |
| 1038 | UDP_MIB_INDATAGRAMS, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1039 | is_udplite); |
| 1040 | return -ret; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | /* FALLTHROUGH -- it's a UDP Packet */ |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * UDP-Lite specific tests, ignored on UDP sockets |
| 1049 | */ |
| 1050 | if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { |
| 1051 | |
| 1052 | /* |
| 1053 | * MIB statistics other than incrementing the error count are |
| 1054 | * disabled for the following two types of errors: these depend |
| 1055 | * on the application settings, not on the functioning of the |
| 1056 | * protocol stack as such. |
| 1057 | * |
| 1058 | * RFC 3828 here recommends (sec 3.3): "There should also be a |
| 1059 | * way ... to ... at least let the receiving application block |
| 1060 | * delivery of packets with coverage values less than a value |
| 1061 | * provided by the application." |
| 1062 | */ |
| 1063 | if (up->pcrlen == 0) { /* full coverage was set */ |
| 1064 | LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage " |
| 1065 | "%d while full coverage %d requested\n", |
| 1066 | UDP_SKB_CB(skb)->cscov, skb->len); |
| 1067 | goto drop; |
| 1068 | } |
| 1069 | /* The next case involves violating the min. coverage requested |
| 1070 | * by the receiver. This is subtle: if receiver wants x and x is |
| 1071 | * greater than the buffersize/MTU then receiver will complain |
| 1072 | * that it wants x while sender emits packets of smaller size y. |
| 1073 | * Therefore the above ...()->partial_cov statement is essential. |
| 1074 | */ |
| 1075 | if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { |
| 1076 | LIMIT_NETDEBUG(KERN_WARNING |
| 1077 | "UDPLITE: coverage %d too small, need min %d\n", |
| 1078 | UDP_SKB_CB(skb)->cscov, up->pcrlen); |
| 1079 | goto drop; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (sk->sk_filter) { |
| 1084 | if (udp_lib_checksum_complete(skb)) |
| 1085 | goto drop; |
| 1086 | } |
| 1087 | |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1088 | rc = 0; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1089 | |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1090 | bh_lock_sock(sk); |
| 1091 | if (!sock_owned_by_user(sk)) |
| 1092 | rc = __udp_queue_rcv_skb(sk, skb); |
| 1093 | else |
| 1094 | sk_add_backlog(sk, skb); |
| 1095 | bh_unlock_sock(sk); |
| 1096 | |
| 1097 | return rc; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1098 | |
| 1099 | drop: |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1100 | UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1101 | kfree_skb(skb); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Multicasts and broadcasts go to each listener. |
| 1107 | * |
| 1108 | * Note: called only from the BH handler context, |
| 1109 | * so we don't need to lock the hashes. |
| 1110 | */ |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 1111 | static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1112 | struct udphdr *uh, |
| 1113 | __be32 saddr, __be32 daddr, |
| 1114 | struct hlist_head udptable[]) |
| 1115 | { |
| 1116 | struct sock *sk; |
| 1117 | int dif; |
| 1118 | |
| 1119 | read_lock(&udp_hash_lock); |
Pavel Emelyanov | 19c7578 | 2008-06-16 17:12:29 -0700 | [diff] [blame] | 1120 | sk = sk_head(&udptable[udp_hashfn(net, ntohs(uh->dest))]); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1121 | dif = skb->dev->ifindex; |
| 1122 | sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif); |
| 1123 | if (sk) { |
| 1124 | struct sock *sknext = NULL; |
| 1125 | |
| 1126 | do { |
| 1127 | struct sk_buff *skb1 = skb; |
| 1128 | |
| 1129 | sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, |
| 1130 | uh->source, saddr, dif); |
| 1131 | if (sknext) |
| 1132 | skb1 = skb_clone(skb, GFP_ATOMIC); |
| 1133 | |
| 1134 | if (skb1) { |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1135 | int ret = udp_queue_rcv_skb(sk, skb1); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1136 | if (ret > 0) |
| 1137 | /* we should probably re-process instead |
| 1138 | * of dropping packets here. */ |
| 1139 | kfree_skb(skb1); |
| 1140 | } |
| 1141 | sk = sknext; |
| 1142 | } while (sknext); |
| 1143 | } else |
| 1144 | kfree_skb(skb); |
| 1145 | read_unlock(&udp_hash_lock); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /* Initialize UDP checksum. If exited with zero value (success), |
| 1150 | * CHECKSUM_UNNECESSARY means, that no more checks are required. |
| 1151 | * Otherwise, csum completion requires chacksumming packet body, |
| 1152 | * including udp header and folding it to skb->csum. |
| 1153 | */ |
| 1154 | static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, |
| 1155 | int proto) |
| 1156 | { |
| 1157 | const struct iphdr *iph; |
| 1158 | int err; |
| 1159 | |
| 1160 | UDP_SKB_CB(skb)->partial_cov = 0; |
| 1161 | UDP_SKB_CB(skb)->cscov = skb->len; |
| 1162 | |
| 1163 | if (proto == IPPROTO_UDPLITE) { |
| 1164 | err = udplite_checksum_init(skb, uh); |
| 1165 | if (err) |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
| 1169 | iph = ip_hdr(skb); |
| 1170 | if (uh->check == 0) { |
| 1171 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1172 | } else if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 1173 | if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, |
| 1174 | proto, skb->csum)) |
| 1175 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1176 | } |
| 1177 | if (!skb_csum_unnecessary(skb)) |
| 1178 | skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, |
| 1179 | skb->len, proto, 0); |
| 1180 | /* Probably, we should checksum udp header (it should be in cache |
| 1181 | * in any case) and data in tiny packets (< rx copybreak). |
| 1182 | */ |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * All we need to do is get the socket, and then do a checksum. |
| 1189 | */ |
| 1190 | |
| 1191 | int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], |
| 1192 | int proto) |
| 1193 | { |
| 1194 | struct sock *sk; |
| 1195 | struct udphdr *uh = udp_hdr(skb); |
| 1196 | unsigned short ulen; |
| 1197 | struct rtable *rt = (struct rtable*)skb->dst; |
| 1198 | __be32 saddr = ip_hdr(skb)->saddr; |
| 1199 | __be32 daddr = ip_hdr(skb)->daddr; |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1200 | struct net *net = dev_net(skb->dev); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * Validate the packet. |
| 1204 | */ |
| 1205 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 1206 | goto drop; /* No space for header. */ |
| 1207 | |
| 1208 | ulen = ntohs(uh->len); |
| 1209 | if (ulen > skb->len) |
| 1210 | goto short_packet; |
| 1211 | |
| 1212 | if (proto == IPPROTO_UDP) { |
| 1213 | /* UDP validates ulen. */ |
| 1214 | if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) |
| 1215 | goto short_packet; |
| 1216 | uh = udp_hdr(skb); |
| 1217 | } |
| 1218 | |
| 1219 | if (udp4_csum_init(skb, uh, proto)) |
| 1220 | goto csum_error; |
| 1221 | |
| 1222 | if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) |
Pavel Emelyanov | e316349 | 2008-06-16 17:12:11 -0700 | [diff] [blame] | 1223 | return __udp4_lib_mcast_deliver(net, skb, uh, |
| 1224 | saddr, daddr, udptable); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1225 | |
KOVACS Krisztian | 607c4aa | 2008-10-07 12:38:32 -0700 | [diff] [blame] | 1226 | sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1227 | |
| 1228 | if (sk != NULL) { |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1229 | int ret = udp_queue_rcv_skb(sk, skb); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1230 | sock_put(sk); |
| 1231 | |
| 1232 | /* a return value > 0 means to resubmit the input, but |
| 1233 | * it wants the return to be -protocol, or 0 |
| 1234 | */ |
| 1235 | if (ret > 0) |
| 1236 | return -ret; |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 1241 | goto drop; |
| 1242 | nf_reset(skb); |
| 1243 | |
| 1244 | /* No socket. Drop packet silently, if checksum is wrong */ |
| 1245 | if (udp_lib_checksum_complete(skb)) |
| 1246 | goto csum_error; |
| 1247 | |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1248 | UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1249 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
| 1250 | |
| 1251 | /* |
| 1252 | * Hmm. We got an UDP packet to a port to which we |
| 1253 | * don't wanna listen. Ignore it. |
| 1254 | */ |
| 1255 | kfree_skb(skb); |
| 1256 | return 0; |
| 1257 | |
| 1258 | short_packet: |
YOSHIFUJI Hideaki | a7d632b | 2008-04-14 04:09:00 -0700 | [diff] [blame] | 1259 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From " NIPQUAD_FMT ":%u %d/%d to " NIPQUAD_FMT ":%u\n", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1260 | proto == IPPROTO_UDPLITE ? "-Lite" : "", |
| 1261 | NIPQUAD(saddr), |
| 1262 | ntohs(uh->source), |
| 1263 | ulen, |
| 1264 | skb->len, |
| 1265 | NIPQUAD(daddr), |
| 1266 | ntohs(uh->dest)); |
| 1267 | goto drop; |
| 1268 | |
| 1269 | csum_error: |
| 1270 | /* |
| 1271 | * RFC1122: OK. Discards the bad packet silently (as far as |
| 1272 | * the network is concerned, anyway) as per 4.1.3.4 (MUST). |
| 1273 | */ |
YOSHIFUJI Hideaki | a7d632b | 2008-04-14 04:09:00 -0700 | [diff] [blame] | 1274 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From " NIPQUAD_FMT ":%u to " NIPQUAD_FMT ":%u ulen %d\n", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1275 | proto == IPPROTO_UDPLITE ? "-Lite" : "", |
| 1276 | NIPQUAD(saddr), |
| 1277 | ntohs(uh->source), |
| 1278 | NIPQUAD(daddr), |
| 1279 | ntohs(uh->dest), |
| 1280 | ulen); |
| 1281 | drop: |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1282 | UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1283 | kfree_skb(skb); |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | int udp_rcv(struct sk_buff *skb) |
| 1288 | { |
| 1289 | return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); |
| 1290 | } |
| 1291 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 1292 | void udp_destroy_sock(struct sock *sk) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1293 | { |
| 1294 | lock_sock(sk); |
| 1295 | udp_flush_pending_frames(sk); |
| 1296 | release_sock(sk); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | /* |
| 1300 | * Socket option code for UDP |
| 1301 | */ |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1302 | int udp_lib_setsockopt(struct sock *sk, int level, int optname, |
| 1303 | char __user *optval, int optlen, |
| 1304 | int (*push_pending_frames)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | { |
| 1306 | struct udp_sock *up = udp_sk(sk); |
| 1307 | int val; |
| 1308 | int err = 0; |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 1309 | int is_udplite = IS_UDPLITE(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1311 | if (optlen<sizeof(int)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return -EINVAL; |
| 1313 | |
| 1314 | if (get_user(val, (int __user *)optval)) |
| 1315 | return -EFAULT; |
| 1316 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1317 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | case UDP_CORK: |
| 1319 | if (val != 0) { |
| 1320 | up->corkflag = 1; |
| 1321 | } else { |
| 1322 | up->corkflag = 0; |
| 1323 | lock_sock(sk); |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1324 | (*push_pending_frames)(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | release_sock(sk); |
| 1326 | } |
| 1327 | break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | case UDP_ENCAP: |
| 1330 | switch (val) { |
| 1331 | case 0: |
| 1332 | case UDP_ENCAP_ESPINUDP: |
| 1333 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
James Chapman | 067b207 | 2007-07-05 17:08:05 -0700 | [diff] [blame] | 1334 | up->encap_rcv = xfrm4_udp_encap_rcv; |
| 1335 | /* FALLTHROUGH */ |
James Chapman | 342f023 | 2007-06-27 15:37:46 -0700 | [diff] [blame] | 1336 | case UDP_ENCAP_L2TPINUDP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | up->encap_type = val; |
| 1338 | break; |
| 1339 | default: |
| 1340 | err = -ENOPROTOOPT; |
| 1341 | break; |
| 1342 | } |
| 1343 | break; |
| 1344 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1345 | /* |
| 1346 | * UDP-Lite's partial checksum coverage (RFC 3828). |
| 1347 | */ |
| 1348 | /* The sender sets actual checksum coverage length via this option. |
| 1349 | * The case coverage > packet length is handled by send module. */ |
| 1350 | case UDPLITE_SEND_CSCOV: |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 1351 | if (!is_udplite) /* Disable the option on UDP sockets */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1352 | return -ENOPROTOOPT; |
| 1353 | if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ |
| 1354 | val = 8; |
Gerrit Renker | 47112e2 | 2008-07-21 13:35:08 -0700 | [diff] [blame] | 1355 | else if (val > USHORT_MAX) |
| 1356 | val = USHORT_MAX; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1357 | up->pcslen = val; |
| 1358 | up->pcflag |= UDPLITE_SEND_CC; |
| 1359 | break; |
| 1360 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1361 | /* The receiver specifies a minimum checksum coverage value. To make |
| 1362 | * sense, this should be set to at least 8 (as done below). If zero is |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1363 | * used, this again means full checksum coverage. */ |
| 1364 | case UDPLITE_RECV_CSCOV: |
Wang Chen | b2bf1e2 | 2007-12-03 22:34:16 +1100 | [diff] [blame] | 1365 | if (!is_udplite) /* Disable the option on UDP sockets */ |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1366 | return -ENOPROTOOPT; |
| 1367 | if (val != 0 && val < 8) /* Avoid silly minimal values. */ |
| 1368 | val = 8; |
Gerrit Renker | 47112e2 | 2008-07-21 13:35:08 -0700 | [diff] [blame] | 1369 | else if (val > USHORT_MAX) |
| 1370 | val = USHORT_MAX; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1371 | up->pcrlen = val; |
| 1372 | up->pcflag |= UDPLITE_RECV_CC; |
| 1373 | break; |
| 1374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | default: |
| 1376 | err = -ENOPROTOOPT; |
| 1377 | break; |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | return err; |
| 1381 | } |
| 1382 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1383 | int udp_setsockopt(struct sock *sk, int level, int optname, |
| 1384 | char __user *optval, int optlen) |
| 1385 | { |
| 1386 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 1387 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 1388 | udp_push_pending_frames); |
| 1389 | return ip_setsockopt(sk, level, optname, optval, optlen); |
| 1390 | } |
| 1391 | |
| 1392 | #ifdef CONFIG_COMPAT |
| 1393 | int compat_udp_setsockopt(struct sock *sk, int level, int optname, |
| 1394 | char __user *optval, int optlen) |
| 1395 | { |
| 1396 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 1397 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, |
| 1398 | udp_push_pending_frames); |
| 1399 | return compat_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1400 | } |
| 1401 | #endif |
| 1402 | |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1403 | int udp_lib_getsockopt(struct sock *sk, int level, int optname, |
| 1404 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | { |
| 1406 | struct udp_sock *up = udp_sk(sk); |
| 1407 | int val, len; |
| 1408 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1409 | if (get_user(len,optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | return -EFAULT; |
| 1411 | |
| 1412 | len = min_t(unsigned int, len, sizeof(int)); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1413 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1414 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | return -EINVAL; |
| 1416 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1417 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | case UDP_CORK: |
| 1419 | val = up->corkflag; |
| 1420 | break; |
| 1421 | |
| 1422 | case UDP_ENCAP: |
| 1423 | val = up->encap_type; |
| 1424 | break; |
| 1425 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1426 | /* The following two cannot be changed on UDP sockets, the return is |
| 1427 | * always 0 (which corresponds to the full checksum coverage of UDP). */ |
| 1428 | case UDPLITE_SEND_CSCOV: |
| 1429 | val = up->pcslen; |
| 1430 | break; |
| 1431 | |
| 1432 | case UDPLITE_RECV_CSCOV: |
| 1433 | val = up->pcrlen; |
| 1434 | break; |
| 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | default: |
| 1437 | return -ENOPROTOOPT; |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1440 | if (put_user(len, optlen)) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1441 | return -EFAULT; |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1442 | if (copy_to_user(optval, &val,len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | return -EFAULT; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1444 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1447 | int udp_getsockopt(struct sock *sk, int level, int optname, |
| 1448 | char __user *optval, int __user *optlen) |
| 1449 | { |
| 1450 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 1451 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
| 1452 | return ip_getsockopt(sk, level, optname, optval, optlen); |
| 1453 | } |
| 1454 | |
| 1455 | #ifdef CONFIG_COMPAT |
| 1456 | int compat_udp_getsockopt(struct sock *sk, int level, int optname, |
| 1457 | char __user *optval, int __user *optlen) |
| 1458 | { |
| 1459 | if (level == SOL_UDP || level == SOL_UDPLITE) |
| 1460 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); |
| 1461 | return compat_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1462 | } |
| 1463 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | /** |
| 1465 | * udp_poll - wait for a UDP event. |
| 1466 | * @file - file struct |
| 1467 | * @sock - socket |
| 1468 | * @wait - poll table |
| 1469 | * |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1470 | * This is same as datagram poll, except for the special case of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | * blocking sockets. If application is using a blocking fd |
| 1472 | * and a packet with checksum error is in the queue; |
| 1473 | * then it could get return from select indicating data available |
| 1474 | * but then block when reading it. Add special case code |
| 1475 | * to work around these arguably broken applications. |
| 1476 | */ |
| 1477 | unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 1478 | { |
| 1479 | unsigned int mask = datagram_poll(file, sock, wait); |
| 1480 | struct sock *sk = sock->sk; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1481 | int is_lite = IS_UDPLITE(sk); |
| 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* Check for false positives due to checksum errors */ |
| 1484 | if ( (mask & POLLRDNORM) && |
| 1485 | !(file->f_flags & O_NONBLOCK) && |
| 1486 | !(sk->sk_shutdown & RCV_SHUTDOWN)){ |
| 1487 | struct sk_buff_head *rcvq = &sk->sk_receive_queue; |
| 1488 | struct sk_buff *skb; |
| 1489 | |
Herbert Xu | 208d898 | 2005-05-30 15:50:15 -0700 | [diff] [blame] | 1490 | spin_lock_bh(&rcvq->lock); |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 1491 | while ((skb = skb_peek(rcvq)) != NULL && |
| 1492 | udp_lib_checksum_complete(skb)) { |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 1493 | UDP_INC_STATS_BH(sock_net(sk), |
| 1494 | UDP_MIB_INERRORS, is_lite); |
Herbert Xu | 759e5d0 | 2007-03-25 20:10:56 -0700 | [diff] [blame] | 1495 | __skb_unlink(skb, rcvq); |
| 1496 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
Herbert Xu | 208d898 | 2005-05-30 15:50:15 -0700 | [diff] [blame] | 1498 | spin_unlock_bh(&rcvq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
| 1500 | /* nothing to see, move along */ |
| 1501 | if (skb == NULL) |
| 1502 | mask &= ~(POLLIN | POLLRDNORM); |
| 1503 | } |
| 1504 | |
| 1505 | return mask; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1509 | struct proto udp_prot = { |
| 1510 | .name = "UDP", |
| 1511 | .owner = THIS_MODULE, |
| 1512 | .close = udp_lib_close, |
| 1513 | .connect = ip4_datagram_connect, |
| 1514 | .disconnect = udp_disconnect, |
| 1515 | .ioctl = udp_ioctl, |
| 1516 | .destroy = udp_destroy_sock, |
| 1517 | .setsockopt = udp_setsockopt, |
| 1518 | .getsockopt = udp_getsockopt, |
| 1519 | .sendmsg = udp_sendmsg, |
| 1520 | .recvmsg = udp_recvmsg, |
| 1521 | .sendpage = udp_sendpage, |
Herbert Xu | 9382177 | 2008-09-15 11:48:46 -0700 | [diff] [blame] | 1522 | .backlog_rcv = __udp_queue_rcv_skb, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1523 | .hash = udp_lib_hash, |
| 1524 | .unhash = udp_lib_unhash, |
| 1525 | .get_port = udp_v4_get_port, |
| 1526 | .memory_allocated = &udp_memory_allocated, |
| 1527 | .sysctl_mem = sysctl_udp_mem, |
| 1528 | .sysctl_wmem = &sysctl_udp_wmem_min, |
| 1529 | .sysctl_rmem = &sysctl_udp_rmem_min, |
| 1530 | .obj_size = sizeof(struct udp_sock), |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 1531 | .h.udp_hash = udp_hash, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1532 | #ifdef CONFIG_COMPAT |
| 1533 | .compat_setsockopt = compat_udp_setsockopt, |
| 1534 | .compat_getsockopt = compat_udp_getsockopt, |
| 1535 | #endif |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1536 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
| 1538 | /* ------------------------------------------------------------------------ */ |
| 1539 | #ifdef CONFIG_PROC_FS |
| 1540 | |
| 1541 | static struct sock *udp_get_first(struct seq_file *seq) |
| 1542 | { |
| 1543 | struct sock *sk; |
| 1544 | struct udp_iter_state *state = seq->private; |
Denis V. Lunev | 6f191ef | 2008-03-28 18:23:33 -0700 | [diff] [blame] | 1545 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
| 1547 | for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) { |
| 1548 | struct hlist_node *node; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1549 | sk_for_each(sk, node, state->hashtable + state->bucket) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1550 | if (!net_eq(sock_net(sk), net)) |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 1551 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | if (sk->sk_family == state->family) |
| 1553 | goto found; |
| 1554 | } |
| 1555 | } |
| 1556 | sk = NULL; |
| 1557 | found: |
| 1558 | return sk; |
| 1559 | } |
| 1560 | |
| 1561 | static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) |
| 1562 | { |
| 1563 | struct udp_iter_state *state = seq->private; |
Denis V. Lunev | 6f191ef | 2008-03-28 18:23:33 -0700 | [diff] [blame] | 1564 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
| 1566 | do { |
| 1567 | sk = sk_next(sk); |
| 1568 | try_again: |
| 1569 | ; |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1570 | } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
| 1572 | if (!sk && ++state->bucket < UDP_HTABLE_SIZE) { |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1573 | sk = sk_head(state->hashtable + state->bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | goto try_again; |
| 1575 | } |
| 1576 | return sk; |
| 1577 | } |
| 1578 | |
| 1579 | static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) |
| 1580 | { |
| 1581 | struct sock *sk = udp_get_first(seq); |
| 1582 | |
| 1583 | if (sk) |
Stephen Hemminger | 6516c65 | 2007-03-08 20:41:55 -0800 | [diff] [blame] | 1584 | while (pos && (sk = udp_get_next(seq, sk)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | --pos; |
| 1586 | return pos ? NULL : sk; |
| 1587 | } |
| 1588 | |
| 1589 | static void *udp_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 1590 | __acquires(udp_hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | { |
| 1592 | read_lock(&udp_hash_lock); |
YOSHIFUJI Hideaki | b50660f | 2008-03-31 19:38:15 -0700 | [diff] [blame] | 1593 | return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1597 | { |
| 1598 | struct sock *sk; |
| 1599 | |
YOSHIFUJI Hideaki | b50660f | 2008-03-31 19:38:15 -0700 | [diff] [blame] | 1600 | if (v == SEQ_START_TOKEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | sk = udp_get_idx(seq, 0); |
| 1602 | else |
| 1603 | sk = udp_get_next(seq, v); |
| 1604 | |
| 1605 | ++*pos; |
| 1606 | return sk; |
| 1607 | } |
| 1608 | |
| 1609 | static void udp_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 1610 | __releases(udp_hash_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | { |
| 1612 | read_unlock(&udp_hash_lock); |
| 1613 | } |
| 1614 | |
| 1615 | static int udp_seq_open(struct inode *inode, struct file *file) |
| 1616 | { |
| 1617 | struct udp_seq_afinfo *afinfo = PDE(inode)->data; |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 1618 | struct udp_iter_state *s; |
| 1619 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 1621 | err = seq_open_net(inode, file, &afinfo->seq_ops, |
| 1622 | sizeof(struct udp_iter_state)); |
| 1623 | if (err < 0) |
| 1624 | return err; |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 1625 | |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 1626 | s = ((struct seq_file *)file->private_data)->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | s->family = afinfo->family; |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1628 | s->hashtable = afinfo->hashtable; |
Denis V. Lunev | a2be75c | 2008-03-28 18:25:06 -0700 | [diff] [blame] | 1629 | return err; |
Daniel Lezcano | a91275e | 2008-03-21 04:11:58 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | /* ------------------------------------------------------------------------ */ |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1633 | int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | { |
| 1635 | struct proc_dir_entry *p; |
| 1636 | int rc = 0; |
| 1637 | |
Denis V. Lunev | 3ba9441 | 2008-03-28 18:25:32 -0700 | [diff] [blame] | 1638 | afinfo->seq_fops.open = udp_seq_open; |
| 1639 | afinfo->seq_fops.read = seq_read; |
| 1640 | afinfo->seq_fops.llseek = seq_lseek; |
| 1641 | afinfo->seq_fops.release = seq_release_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 1643 | afinfo->seq_ops.start = udp_seq_start; |
| 1644 | afinfo->seq_ops.next = udp_seq_next; |
| 1645 | afinfo->seq_ops.stop = udp_seq_stop; |
| 1646 | |
Denis V. Lunev | 84841c3 | 2008-05-02 04:10:08 -0700 | [diff] [blame] | 1647 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
| 1648 | &afinfo->seq_fops, afinfo); |
| 1649 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | rc = -ENOMEM; |
| 1651 | return rc; |
| 1652 | } |
| 1653 | |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1654 | void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | { |
Daniel Lezcano | 0c96d8c | 2008-03-21 04:14:17 -0700 | [diff] [blame] | 1656 | proc_net_remove(net, afinfo->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1658 | |
| 1659 | /* ------------------------------------------------------------------------ */ |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1660 | static void udp4_format_sock(struct sock *sp, struct seq_file *f, |
| 1661 | int bucket, int *len) |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1662 | { |
| 1663 | struct inet_sock *inet = inet_sk(sp); |
| 1664 | __be32 dest = inet->daddr; |
| 1665 | __be32 src = inet->rcv_saddr; |
| 1666 | __u16 destp = ntohs(inet->dport); |
| 1667 | __u16 srcp = ntohs(inet->sport); |
| 1668 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1669 | seq_printf(f, "%4d: %08X:%04X %08X:%04X" |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1670 | " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n", |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1671 | bucket, src, srcp, dest, destp, sp->sk_state, |
| 1672 | atomic_read(&sp->sk_wmem_alloc), |
| 1673 | atomic_read(&sp->sk_rmem_alloc), |
| 1674 | 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1675 | atomic_read(&sp->sk_refcnt), sp, |
| 1676 | atomic_read(&sp->sk_drops), len); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | int udp4_seq_show(struct seq_file *seq, void *v) |
| 1680 | { |
| 1681 | if (v == SEQ_START_TOKEN) |
| 1682 | seq_printf(seq, "%-127s\n", |
| 1683 | " sl local_address rem_address st tx_queue " |
| 1684 | "rx_queue tr tm->when retrnsmt uid timeout " |
Eric Dumazet | cb61cb9 | 2008-06-17 21:04:56 -0700 | [diff] [blame] | 1685 | "inode ref pointer drops"); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1686 | else { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1687 | struct udp_iter_state *state = seq->private; |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1688 | int len; |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1689 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 1690 | udp4_format_sock(v, seq, state->bucket, &len); |
| 1691 | seq_printf(seq, "%*s\n", 127 - len ,""); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1692 | } |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | /* ------------------------------------------------------------------------ */ |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1697 | static struct udp_seq_afinfo udp4_seq_afinfo = { |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1698 | .name = "udp", |
| 1699 | .family = AF_INET, |
| 1700 | .hashtable = udp_hash, |
Denis V. Lunev | 4ad96d3 | 2008-03-28 18:25:53 -0700 | [diff] [blame] | 1701 | .seq_fops = { |
| 1702 | .owner = THIS_MODULE, |
| 1703 | }, |
Denis V. Lunev | dda6192 | 2008-03-28 18:24:26 -0700 | [diff] [blame] | 1704 | .seq_ops = { |
| 1705 | .show = udp4_seq_show, |
| 1706 | }, |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1707 | }; |
| 1708 | |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 1709 | static int udp4_proc_init_net(struct net *net) |
| 1710 | { |
| 1711 | return udp_proc_register(net, &udp4_seq_afinfo); |
| 1712 | } |
| 1713 | |
| 1714 | static void udp4_proc_exit_net(struct net *net) |
| 1715 | { |
| 1716 | udp_proc_unregister(net, &udp4_seq_afinfo); |
| 1717 | } |
| 1718 | |
| 1719 | static struct pernet_operations udp4_net_ops = { |
| 1720 | .init = udp4_proc_init_net, |
| 1721 | .exit = udp4_proc_exit_net, |
| 1722 | }; |
| 1723 | |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1724 | int __init udp4_proc_init(void) |
| 1725 | { |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 1726 | return register_pernet_subsys(&udp4_net_ops); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | void udp4_proc_exit(void) |
| 1730 | { |
Pavel Emelyanov | 15439fe | 2008-03-24 14:53:49 -0700 | [diff] [blame] | 1731 | unregister_pernet_subsys(&udp4_net_ops); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1732 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | #endif /* CONFIG_PROC_FS */ |
| 1734 | |
Hideo Aoki | 95766ff | 2007-12-31 00:29:24 -0800 | [diff] [blame] | 1735 | void __init udp_init(void) |
| 1736 | { |
| 1737 | unsigned long limit; |
| 1738 | |
| 1739 | /* Set the pressure threshold up by the same strategy of TCP. It is a |
| 1740 | * fraction of global memory that is up to 1/2 at 256 MB, decreasing |
| 1741 | * toward zero with the amount of memory, with a floor of 128 pages. |
| 1742 | */ |
| 1743 | limit = min(nr_all_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT); |
| 1744 | limit = (limit * (nr_all_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11); |
| 1745 | limit = max(limit, 128UL); |
| 1746 | sysctl_udp_mem[0] = limit / 4 * 3; |
| 1747 | sysctl_udp_mem[1] = limit; |
| 1748 | sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; |
| 1749 | |
| 1750 | sysctl_udp_rmem_min = SK_MEM_QUANTUM; |
| 1751 | sysctl_udp_wmem_min = SK_MEM_QUANTUM; |
| 1752 | } |
| 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | EXPORT_SYMBOL(udp_disconnect); |
| 1755 | EXPORT_SYMBOL(udp_hash); |
| 1756 | EXPORT_SYMBOL(udp_hash_lock); |
| 1757 | EXPORT_SYMBOL(udp_ioctl); |
David S. Miller | db8dac2 | 2008-03-06 16:22:02 -0800 | [diff] [blame] | 1758 | EXPORT_SYMBOL(udp_prot); |
| 1759 | EXPORT_SYMBOL(udp_sendmsg); |
Gerrit Renker | 4c0a6cb | 2006-11-27 09:29:59 -0800 | [diff] [blame] | 1760 | EXPORT_SYMBOL(udp_lib_getsockopt); |
| 1761 | EXPORT_SYMBOL(udp_lib_setsockopt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | EXPORT_SYMBOL(udp_poll); |
Pavel Emelyanov | 6ba5a3c | 2008-03-22 16:51:21 -0700 | [diff] [blame] | 1763 | EXPORT_SYMBOL(udp_lib_get_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
| 1765 | #ifdef CONFIG_PROC_FS |
| 1766 | EXPORT_SYMBOL(udp_proc_register); |
| 1767 | EXPORT_SYMBOL(udp_proc_unregister); |
| 1768 | #endif |