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