Commit 979402b16cde048ced4839e21cc49e0779352b80

Authored by Eric Dumazet
Committed by David S. Miller
1 parent 521130d11f

udp: increment UDP_MIB_INERRORS if copy failed

In UDP recvmsg(), we miss an increase of UDP_MIB_INERRORS if the copy
of skb to userspace failed for whatever reason.

Reported-by: Shawn Bohrer <sbohrer@rgmadvisors.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 2 changed files with 16 additions and 0 deletions Inline Diff

1 /* 1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX 2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket 3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level. 4 * interface as the means of communication with the user level.
5 * 5 *
6 * The User Datagram Protocol (UDP). 6 * The User Datagram Protocol (UDP).
7 * 7 *
8 * Authors: Ross Biro 8 * Authors: Ross Biro
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11 * Alan Cox, <alan@lxorguk.ukuu.org.uk> 11 * Alan Cox, <alan@lxorguk.ukuu.org.uk>
12 * Hirokazu Takahashi, <taka@valinux.co.jp> 12 * Hirokazu Takahashi, <taka@valinux.co.jp>
13 * 13 *
14 * Fixes: 14 * Fixes:
15 * Alan Cox : verify_area() calls 15 * Alan Cox : verify_area() calls
16 * Alan Cox : stopped close while in use off icmp 16 * Alan Cox : stopped close while in use off icmp
17 * messages. Not a fix but a botch that 17 * messages. Not a fix but a botch that
18 * for udp at least is 'valid'. 18 * for udp at least is 'valid'.
19 * Alan Cox : Fixed icmp handling properly 19 * Alan Cox : Fixed icmp handling properly
20 * Alan Cox : Correct error for oversized datagrams 20 * Alan Cox : Correct error for oversized datagrams
21 * Alan Cox : Tidied select() semantics. 21 * Alan Cox : Tidied select() semantics.
22 * Alan Cox : udp_err() fixed properly, also now 22 * Alan Cox : udp_err() fixed properly, also now
23 * select and read wake correctly on errors 23 * select and read wake correctly on errors
24 * Alan Cox : udp_send verify_area moved to avoid mem leak 24 * Alan Cox : udp_send verify_area moved to avoid mem leak
25 * Alan Cox : UDP can count its memory 25 * Alan Cox : UDP can count its memory
26 * Alan Cox : send to an unknown connection causes 26 * Alan Cox : send to an unknown connection causes
27 * an ECONNREFUSED off the icmp, but 27 * an ECONNREFUSED off the icmp, but
28 * does NOT close. 28 * does NOT close.
29 * Alan Cox : Switched to new sk_buff handlers. No more backlog! 29 * Alan Cox : Switched to new sk_buff handlers. No more backlog!
30 * Alan Cox : Using generic datagram code. Even smaller and the PEEK 30 * Alan Cox : Using generic datagram code. Even smaller and the PEEK
31 * bug no longer crashes it. 31 * bug no longer crashes it.
32 * Fred Van Kempen : Net2e support for sk->broadcast. 32 * Fred Van Kempen : Net2e support for sk->broadcast.
33 * Alan Cox : Uses skb_free_datagram 33 * Alan Cox : Uses skb_free_datagram
34 * Alan Cox : Added get/set sockopt support. 34 * Alan Cox : Added get/set sockopt support.
35 * Alan Cox : Broadcasting without option set returns EACCES. 35 * Alan Cox : Broadcasting without option set returns EACCES.
36 * Alan Cox : No wakeup calls. Instead we now use the callbacks. 36 * Alan Cox : No wakeup calls. Instead we now use the callbacks.
37 * Alan Cox : Use ip_tos and ip_ttl 37 * Alan Cox : Use ip_tos and ip_ttl
38 * Alan Cox : SNMP Mibs 38 * Alan Cox : SNMP Mibs
39 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support. 39 * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
40 * Matt Dillon : UDP length checks. 40 * Matt Dillon : UDP length checks.
41 * Alan Cox : Smarter af_inet used properly. 41 * Alan Cox : Smarter af_inet used properly.
42 * Alan Cox : Use new kernel side addressing. 42 * Alan Cox : Use new kernel side addressing.
43 * Alan Cox : Incorrect return on truncated datagram receive. 43 * Alan Cox : Incorrect return on truncated datagram receive.
44 * Arnt Gulbrandsen : New udp_send and stuff 44 * Arnt Gulbrandsen : New udp_send and stuff
45 * Alan Cox : Cache last socket 45 * Alan Cox : Cache last socket
46 * Alan Cox : Route cache 46 * Alan Cox : Route cache
47 * Jon Peatfield : Minor efficiency fix to sendto(). 47 * Jon Peatfield : Minor efficiency fix to sendto().
48 * Mike Shaver : RFC1122 checks. 48 * Mike Shaver : RFC1122 checks.
49 * Alan Cox : Nonblocking error fix. 49 * Alan Cox : Nonblocking error fix.
50 * Willy Konynenberg : Transparent proxying support. 50 * Willy Konynenberg : Transparent proxying support.
51 * Mike McLagan : Routing by source 51 * Mike McLagan : Routing by source
52 * David S. Miller : New socket lookup architecture. 52 * David S. Miller : New socket lookup architecture.
53 * Last socket cache retained as it 53 * Last socket cache retained as it
54 * does have a high hit rate. 54 * does have a high hit rate.
55 * Olaf Kirch : Don't linearise iovec on sendmsg. 55 * Olaf Kirch : Don't linearise iovec on sendmsg.
56 * Andi Kleen : Some cleanups, cache destination entry 56 * Andi Kleen : Some cleanups, cache destination entry
57 * for connect. 57 * for connect.
58 * Vitaly E. Lavrov : Transparent proxy revived after year coma. 58 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
59 * Melvin Smith : Check msg_name not msg_namelen in sendto(), 59 * Melvin Smith : Check msg_name not msg_namelen in sendto(),
60 * return ENOTCONN for unconnected sockets (POSIX) 60 * return ENOTCONN for unconnected sockets (POSIX)
61 * Janos Farkas : don't deliver multi/broadcasts to a different 61 * Janos Farkas : don't deliver multi/broadcasts to a different
62 * bound-to-device socket 62 * bound-to-device socket
63 * Hirokazu Takahashi : HW checksumming for outgoing UDP 63 * Hirokazu Takahashi : HW checksumming for outgoing UDP
64 * datagrams. 64 * datagrams.
65 * Hirokazu Takahashi : sendfile() on UDP works now. 65 * Hirokazu Takahashi : sendfile() on UDP works now.
66 * Arnaldo C. Melo : convert /proc/net/udp to seq_file 66 * Arnaldo C. Melo : convert /proc/net/udp to seq_file
67 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 67 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
68 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind 68 * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
69 * a single port at the same time. 69 * a single port at the same time.
70 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support 70 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
71 * James Chapman : Add L2TP encapsulation type. 71 * James Chapman : Add L2TP encapsulation type.
72 * 72 *
73 * 73 *
74 * This program is free software; you can redistribute it and/or 74 * This program is free software; you can redistribute it and/or
75 * modify it under the terms of the GNU General Public License 75 * modify it under the terms of the GNU General Public License
76 * as published by the Free Software Foundation; either version 76 * as published by the Free Software Foundation; either version
77 * 2 of the License, or (at your option) any later version. 77 * 2 of the License, or (at your option) any later version.
78 */ 78 */
79 79
80 #define pr_fmt(fmt) "UDP: " fmt 80 #define pr_fmt(fmt) "UDP: " fmt
81 81
82 #include <asm/uaccess.h> 82 #include <asm/uaccess.h>
83 #include <asm/ioctls.h> 83 #include <asm/ioctls.h>
84 #include <linux/bootmem.h> 84 #include <linux/bootmem.h>
85 #include <linux/highmem.h> 85 #include <linux/highmem.h>
86 #include <linux/swap.h> 86 #include <linux/swap.h>
87 #include <linux/types.h> 87 #include <linux/types.h>
88 #include <linux/fcntl.h> 88 #include <linux/fcntl.h>
89 #include <linux/module.h> 89 #include <linux/module.h>
90 #include <linux/socket.h> 90 #include <linux/socket.h>
91 #include <linux/sockios.h> 91 #include <linux/sockios.h>
92 #include <linux/igmp.h> 92 #include <linux/igmp.h>
93 #include <linux/in.h> 93 #include <linux/in.h>
94 #include <linux/errno.h> 94 #include <linux/errno.h>
95 #include <linux/timer.h> 95 #include <linux/timer.h>
96 #include <linux/mm.h> 96 #include <linux/mm.h>
97 #include <linux/inet.h> 97 #include <linux/inet.h>
98 #include <linux/netdevice.h> 98 #include <linux/netdevice.h>
99 #include <linux/slab.h> 99 #include <linux/slab.h>
100 #include <net/tcp_states.h> 100 #include <net/tcp_states.h>
101 #include <linux/skbuff.h> 101 #include <linux/skbuff.h>
102 #include <linux/proc_fs.h> 102 #include <linux/proc_fs.h>
103 #include <linux/seq_file.h> 103 #include <linux/seq_file.h>
104 #include <net/net_namespace.h> 104 #include <net/net_namespace.h>
105 #include <net/icmp.h> 105 #include <net/icmp.h>
106 #include <net/route.h> 106 #include <net/route.h>
107 #include <net/checksum.h> 107 #include <net/checksum.h>
108 #include <net/xfrm.h> 108 #include <net/xfrm.h>
109 #include <trace/events/udp.h> 109 #include <trace/events/udp.h>
110 #include <linux/static_key.h> 110 #include <linux/static_key.h>
111 #include <trace/events/skb.h> 111 #include <trace/events/skb.h>
112 #include "udp_impl.h" 112 #include "udp_impl.h"
113 113
114 struct udp_table udp_table __read_mostly; 114 struct udp_table udp_table __read_mostly;
115 EXPORT_SYMBOL(udp_table); 115 EXPORT_SYMBOL(udp_table);
116 116
117 long sysctl_udp_mem[3] __read_mostly; 117 long sysctl_udp_mem[3] __read_mostly;
118 EXPORT_SYMBOL(sysctl_udp_mem); 118 EXPORT_SYMBOL(sysctl_udp_mem);
119 119
120 int sysctl_udp_rmem_min __read_mostly; 120 int sysctl_udp_rmem_min __read_mostly;
121 EXPORT_SYMBOL(sysctl_udp_rmem_min); 121 EXPORT_SYMBOL(sysctl_udp_rmem_min);
122 122
123 int sysctl_udp_wmem_min __read_mostly; 123 int sysctl_udp_wmem_min __read_mostly;
124 EXPORT_SYMBOL(sysctl_udp_wmem_min); 124 EXPORT_SYMBOL(sysctl_udp_wmem_min);
125 125
126 atomic_long_t udp_memory_allocated; 126 atomic_long_t udp_memory_allocated;
127 EXPORT_SYMBOL(udp_memory_allocated); 127 EXPORT_SYMBOL(udp_memory_allocated);
128 128
129 #define MAX_UDP_PORTS 65536 129 #define MAX_UDP_PORTS 65536
130 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN) 130 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
131 131
132 static int udp_lib_lport_inuse(struct net *net, __u16 num, 132 static int udp_lib_lport_inuse(struct net *net, __u16 num,
133 const struct udp_hslot *hslot, 133 const struct udp_hslot *hslot,
134 unsigned long *bitmap, 134 unsigned long *bitmap,
135 struct sock *sk, 135 struct sock *sk,
136 int (*saddr_comp)(const struct sock *sk1, 136 int (*saddr_comp)(const struct sock *sk1,
137 const struct sock *sk2), 137 const struct sock *sk2),
138 unsigned int log) 138 unsigned int log)
139 { 139 {
140 struct sock *sk2; 140 struct sock *sk2;
141 struct hlist_nulls_node *node; 141 struct hlist_nulls_node *node;
142 142
143 sk_nulls_for_each(sk2, node, &hslot->head) 143 sk_nulls_for_each(sk2, node, &hslot->head)
144 if (net_eq(sock_net(sk2), net) && 144 if (net_eq(sock_net(sk2), net) &&
145 sk2 != sk && 145 sk2 != sk &&
146 (bitmap || udp_sk(sk2)->udp_port_hash == num) && 146 (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
147 (!sk2->sk_reuse || !sk->sk_reuse) && 147 (!sk2->sk_reuse || !sk->sk_reuse) &&
148 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 148 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
149 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 149 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
150 (*saddr_comp)(sk, sk2)) { 150 (*saddr_comp)(sk, sk2)) {
151 if (bitmap) 151 if (bitmap)
152 __set_bit(udp_sk(sk2)->udp_port_hash >> log, 152 __set_bit(udp_sk(sk2)->udp_port_hash >> log,
153 bitmap); 153 bitmap);
154 else 154 else
155 return 1; 155 return 1;
156 } 156 }
157 return 0; 157 return 0;
158 } 158 }
159 159
160 /* 160 /*
161 * Note: we still hold spinlock of primary hash chain, so no other writer 161 * Note: we still hold spinlock of primary hash chain, so no other writer
162 * can insert/delete a socket with local_port == num 162 * can insert/delete a socket with local_port == num
163 */ 163 */
164 static int udp_lib_lport_inuse2(struct net *net, __u16 num, 164 static int udp_lib_lport_inuse2(struct net *net, __u16 num,
165 struct udp_hslot *hslot2, 165 struct udp_hslot *hslot2,
166 struct sock *sk, 166 struct sock *sk,
167 int (*saddr_comp)(const struct sock *sk1, 167 int (*saddr_comp)(const struct sock *sk1,
168 const struct sock *sk2)) 168 const struct sock *sk2))
169 { 169 {
170 struct sock *sk2; 170 struct sock *sk2;
171 struct hlist_nulls_node *node; 171 struct hlist_nulls_node *node;
172 int res = 0; 172 int res = 0;
173 173
174 spin_lock(&hslot2->lock); 174 spin_lock(&hslot2->lock);
175 udp_portaddr_for_each_entry(sk2, node, &hslot2->head) 175 udp_portaddr_for_each_entry(sk2, node, &hslot2->head)
176 if (net_eq(sock_net(sk2), net) && 176 if (net_eq(sock_net(sk2), net) &&
177 sk2 != sk && 177 sk2 != sk &&
178 (udp_sk(sk2)->udp_port_hash == num) && 178 (udp_sk(sk2)->udp_port_hash == num) &&
179 (!sk2->sk_reuse || !sk->sk_reuse) && 179 (!sk2->sk_reuse || !sk->sk_reuse) &&
180 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if || 180 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
181 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && 181 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
182 (*saddr_comp)(sk, sk2)) { 182 (*saddr_comp)(sk, sk2)) {
183 res = 1; 183 res = 1;
184 break; 184 break;
185 } 185 }
186 spin_unlock(&hslot2->lock); 186 spin_unlock(&hslot2->lock);
187 return res; 187 return res;
188 } 188 }
189 189
190 /** 190 /**
191 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 191 * udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6
192 * 192 *
193 * @sk: socket struct in question 193 * @sk: socket struct in question
194 * @snum: port number to look up 194 * @snum: port number to look up
195 * @saddr_comp: AF-dependent comparison of bound local IP addresses 195 * @saddr_comp: AF-dependent comparison of bound local IP addresses
196 * @hash2_nulladdr: AF-dependent hash value in secondary hash chains, 196 * @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
197 * with NULL address 197 * with NULL address
198 */ 198 */
199 int udp_lib_get_port(struct sock *sk, unsigned short snum, 199 int udp_lib_get_port(struct sock *sk, unsigned short snum,
200 int (*saddr_comp)(const struct sock *sk1, 200 int (*saddr_comp)(const struct sock *sk1,
201 const struct sock *sk2), 201 const struct sock *sk2),
202 unsigned int hash2_nulladdr) 202 unsigned int hash2_nulladdr)
203 { 203 {
204 struct udp_hslot *hslot, *hslot2; 204 struct udp_hslot *hslot, *hslot2;
205 struct udp_table *udptable = sk->sk_prot->h.udp_table; 205 struct udp_table *udptable = sk->sk_prot->h.udp_table;
206 int error = 1; 206 int error = 1;
207 struct net *net = sock_net(sk); 207 struct net *net = sock_net(sk);
208 208
209 if (!snum) { 209 if (!snum) {
210 int low, high, remaining; 210 int low, high, remaining;
211 unsigned int rand; 211 unsigned int rand;
212 unsigned short first, last; 212 unsigned short first, last;
213 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 213 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
214 214
215 inet_get_local_port_range(&low, &high); 215 inet_get_local_port_range(&low, &high);
216 remaining = (high - low) + 1; 216 remaining = (high - low) + 1;
217 217
218 rand = net_random(); 218 rand = net_random();
219 first = (((u64)rand * remaining) >> 32) + low; 219 first = (((u64)rand * remaining) >> 32) + low;
220 /* 220 /*
221 * force rand to be an odd multiple of UDP_HTABLE_SIZE 221 * force rand to be an odd multiple of UDP_HTABLE_SIZE
222 */ 222 */
223 rand = (rand | 1) * (udptable->mask + 1); 223 rand = (rand | 1) * (udptable->mask + 1);
224 last = first + udptable->mask + 1; 224 last = first + udptable->mask + 1;
225 do { 225 do {
226 hslot = udp_hashslot(udptable, net, first); 226 hslot = udp_hashslot(udptable, net, first);
227 bitmap_zero(bitmap, PORTS_PER_CHAIN); 227 bitmap_zero(bitmap, PORTS_PER_CHAIN);
228 spin_lock_bh(&hslot->lock); 228 spin_lock_bh(&hslot->lock);
229 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk, 229 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
230 saddr_comp, udptable->log); 230 saddr_comp, udptable->log);
231 231
232 snum = first; 232 snum = first;
233 /* 233 /*
234 * Iterate on all possible values of snum for this hash. 234 * Iterate on all possible values of snum for this hash.
235 * Using steps of an odd multiple of UDP_HTABLE_SIZE 235 * Using steps of an odd multiple of UDP_HTABLE_SIZE
236 * give us randomization and full range coverage. 236 * give us randomization and full range coverage.
237 */ 237 */
238 do { 238 do {
239 if (low <= snum && snum <= high && 239 if (low <= snum && snum <= high &&
240 !test_bit(snum >> udptable->log, bitmap) && 240 !test_bit(snum >> udptable->log, bitmap) &&
241 !inet_is_reserved_local_port(snum)) 241 !inet_is_reserved_local_port(snum))
242 goto found; 242 goto found;
243 snum += rand; 243 snum += rand;
244 } while (snum != first); 244 } while (snum != first);
245 spin_unlock_bh(&hslot->lock); 245 spin_unlock_bh(&hslot->lock);
246 } while (++first != last); 246 } while (++first != last);
247 goto fail; 247 goto fail;
248 } else { 248 } else {
249 hslot = udp_hashslot(udptable, net, snum); 249 hslot = udp_hashslot(udptable, net, snum);
250 spin_lock_bh(&hslot->lock); 250 spin_lock_bh(&hslot->lock);
251 if (hslot->count > 10) { 251 if (hslot->count > 10) {
252 int exist; 252 int exist;
253 unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum; 253 unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
254 254
255 slot2 &= udptable->mask; 255 slot2 &= udptable->mask;
256 hash2_nulladdr &= udptable->mask; 256 hash2_nulladdr &= udptable->mask;
257 257
258 hslot2 = udp_hashslot2(udptable, slot2); 258 hslot2 = udp_hashslot2(udptable, slot2);
259 if (hslot->count < hslot2->count) 259 if (hslot->count < hslot2->count)
260 goto scan_primary_hash; 260 goto scan_primary_hash;
261 261
262 exist = udp_lib_lport_inuse2(net, snum, hslot2, 262 exist = udp_lib_lport_inuse2(net, snum, hslot2,
263 sk, saddr_comp); 263 sk, saddr_comp);
264 if (!exist && (hash2_nulladdr != slot2)) { 264 if (!exist && (hash2_nulladdr != slot2)) {
265 hslot2 = udp_hashslot2(udptable, hash2_nulladdr); 265 hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
266 exist = udp_lib_lport_inuse2(net, snum, hslot2, 266 exist = udp_lib_lport_inuse2(net, snum, hslot2,
267 sk, saddr_comp); 267 sk, saddr_comp);
268 } 268 }
269 if (exist) 269 if (exist)
270 goto fail_unlock; 270 goto fail_unlock;
271 else 271 else
272 goto found; 272 goto found;
273 } 273 }
274 scan_primary_hash: 274 scan_primary_hash:
275 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 275 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
276 saddr_comp, 0)) 276 saddr_comp, 0))
277 goto fail_unlock; 277 goto fail_unlock;
278 } 278 }
279 found: 279 found:
280 inet_sk(sk)->inet_num = snum; 280 inet_sk(sk)->inet_num = snum;
281 udp_sk(sk)->udp_port_hash = snum; 281 udp_sk(sk)->udp_port_hash = snum;
282 udp_sk(sk)->udp_portaddr_hash ^= snum; 282 udp_sk(sk)->udp_portaddr_hash ^= snum;
283 if (sk_unhashed(sk)) { 283 if (sk_unhashed(sk)) {
284 sk_nulls_add_node_rcu(sk, &hslot->head); 284 sk_nulls_add_node_rcu(sk, &hslot->head);
285 hslot->count++; 285 hslot->count++;
286 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 286 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
287 287
288 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 288 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
289 spin_lock(&hslot2->lock); 289 spin_lock(&hslot2->lock);
290 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 290 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
291 &hslot2->head); 291 &hslot2->head);
292 hslot2->count++; 292 hslot2->count++;
293 spin_unlock(&hslot2->lock); 293 spin_unlock(&hslot2->lock);
294 } 294 }
295 error = 0; 295 error = 0;
296 fail_unlock: 296 fail_unlock:
297 spin_unlock_bh(&hslot->lock); 297 spin_unlock_bh(&hslot->lock);
298 fail: 298 fail:
299 return error; 299 return error;
300 } 300 }
301 EXPORT_SYMBOL(udp_lib_get_port); 301 EXPORT_SYMBOL(udp_lib_get_port);
302 302
303 static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) 303 static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
304 { 304 {
305 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2); 305 struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
306 306
307 return (!ipv6_only_sock(sk2) && 307 return (!ipv6_only_sock(sk2) &&
308 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr || 308 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
309 inet1->inet_rcv_saddr == inet2->inet_rcv_saddr)); 309 inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
310 } 310 }
311 311
312 static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr, 312 static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
313 unsigned int port) 313 unsigned int port)
314 { 314 {
315 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; 315 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
316 } 316 }
317 317
318 int udp_v4_get_port(struct sock *sk, unsigned short snum) 318 int udp_v4_get_port(struct sock *sk, unsigned short snum)
319 { 319 {
320 unsigned int hash2_nulladdr = 320 unsigned int hash2_nulladdr =
321 udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum); 321 udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
322 unsigned int hash2_partial = 322 unsigned int hash2_partial =
323 udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0); 323 udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
324 324
325 /* precompute partial secondary hash */ 325 /* precompute partial secondary hash */
326 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 326 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
327 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr); 327 return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
328 } 328 }
329 329
330 static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr, 330 static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
331 unsigned short hnum, 331 unsigned short hnum,
332 __be16 sport, __be32 daddr, __be16 dport, int dif) 332 __be16 sport, __be32 daddr, __be16 dport, int dif)
333 { 333 {
334 int score = -1; 334 int score = -1;
335 335
336 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 336 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
337 !ipv6_only_sock(sk)) { 337 !ipv6_only_sock(sk)) {
338 struct inet_sock *inet = inet_sk(sk); 338 struct inet_sock *inet = inet_sk(sk);
339 339
340 score = (sk->sk_family == PF_INET ? 1 : 0); 340 score = (sk->sk_family == PF_INET ? 1 : 0);
341 if (inet->inet_rcv_saddr) { 341 if (inet->inet_rcv_saddr) {
342 if (inet->inet_rcv_saddr != daddr) 342 if (inet->inet_rcv_saddr != daddr)
343 return -1; 343 return -1;
344 score += 2; 344 score += 2;
345 } 345 }
346 if (inet->inet_daddr) { 346 if (inet->inet_daddr) {
347 if (inet->inet_daddr != saddr) 347 if (inet->inet_daddr != saddr)
348 return -1; 348 return -1;
349 score += 2; 349 score += 2;
350 } 350 }
351 if (inet->inet_dport) { 351 if (inet->inet_dport) {
352 if (inet->inet_dport != sport) 352 if (inet->inet_dport != sport)
353 return -1; 353 return -1;
354 score += 2; 354 score += 2;
355 } 355 }
356 if (sk->sk_bound_dev_if) { 356 if (sk->sk_bound_dev_if) {
357 if (sk->sk_bound_dev_if != dif) 357 if (sk->sk_bound_dev_if != dif)
358 return -1; 358 return -1;
359 score += 2; 359 score += 2;
360 } 360 }
361 } 361 }
362 return score; 362 return score;
363 } 363 }
364 364
365 /* 365 /*
366 * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num) 366 * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
367 */ 367 */
368 #define SCORE2_MAX (1 + 2 + 2 + 2) 368 #define SCORE2_MAX (1 + 2 + 2 + 2)
369 static inline int compute_score2(struct sock *sk, struct net *net, 369 static inline int compute_score2(struct sock *sk, struct net *net,
370 __be32 saddr, __be16 sport, 370 __be32 saddr, __be16 sport,
371 __be32 daddr, unsigned int hnum, int dif) 371 __be32 daddr, unsigned int hnum, int dif)
372 { 372 {
373 int score = -1; 373 int score = -1;
374 374
375 if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) { 375 if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
376 struct inet_sock *inet = inet_sk(sk); 376 struct inet_sock *inet = inet_sk(sk);
377 377
378 if (inet->inet_rcv_saddr != daddr) 378 if (inet->inet_rcv_saddr != daddr)
379 return -1; 379 return -1;
380 if (inet->inet_num != hnum) 380 if (inet->inet_num != hnum)
381 return -1; 381 return -1;
382 382
383 score = (sk->sk_family == PF_INET ? 1 : 0); 383 score = (sk->sk_family == PF_INET ? 1 : 0);
384 if (inet->inet_daddr) { 384 if (inet->inet_daddr) {
385 if (inet->inet_daddr != saddr) 385 if (inet->inet_daddr != saddr)
386 return -1; 386 return -1;
387 score += 2; 387 score += 2;
388 } 388 }
389 if (inet->inet_dport) { 389 if (inet->inet_dport) {
390 if (inet->inet_dport != sport) 390 if (inet->inet_dport != sport)
391 return -1; 391 return -1;
392 score += 2; 392 score += 2;
393 } 393 }
394 if (sk->sk_bound_dev_if) { 394 if (sk->sk_bound_dev_if) {
395 if (sk->sk_bound_dev_if != dif) 395 if (sk->sk_bound_dev_if != dif)
396 return -1; 396 return -1;
397 score += 2; 397 score += 2;
398 } 398 }
399 } 399 }
400 return score; 400 return score;
401 } 401 }
402 402
403 403
404 /* called with read_rcu_lock() */ 404 /* called with read_rcu_lock() */
405 static struct sock *udp4_lib_lookup2(struct net *net, 405 static struct sock *udp4_lib_lookup2(struct net *net,
406 __be32 saddr, __be16 sport, 406 __be32 saddr, __be16 sport,
407 __be32 daddr, unsigned int hnum, int dif, 407 __be32 daddr, unsigned int hnum, int dif,
408 struct udp_hslot *hslot2, unsigned int slot2) 408 struct udp_hslot *hslot2, unsigned int slot2)
409 { 409 {
410 struct sock *sk, *result; 410 struct sock *sk, *result;
411 struct hlist_nulls_node *node; 411 struct hlist_nulls_node *node;
412 int score, badness; 412 int score, badness;
413 413
414 begin: 414 begin:
415 result = NULL; 415 result = NULL;
416 badness = -1; 416 badness = -1;
417 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) { 417 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
418 score = compute_score2(sk, net, saddr, sport, 418 score = compute_score2(sk, net, saddr, sport,
419 daddr, hnum, dif); 419 daddr, hnum, dif);
420 if (score > badness) { 420 if (score > badness) {
421 result = sk; 421 result = sk;
422 badness = score; 422 badness = score;
423 if (score == SCORE2_MAX) 423 if (score == SCORE2_MAX)
424 goto exact_match; 424 goto exact_match;
425 } 425 }
426 } 426 }
427 /* 427 /*
428 * if the nulls value we got at the end of this lookup is 428 * if the nulls value we got at the end of this lookup is
429 * not the expected one, we must restart lookup. 429 * not the expected one, we must restart lookup.
430 * We probably met an item that was moved to another chain. 430 * We probably met an item that was moved to another chain.
431 */ 431 */
432 if (get_nulls_value(node) != slot2) 432 if (get_nulls_value(node) != slot2)
433 goto begin; 433 goto begin;
434 434
435 if (result) { 435 if (result) {
436 exact_match: 436 exact_match:
437 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 437 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
438 result = NULL; 438 result = NULL;
439 else if (unlikely(compute_score2(result, net, saddr, sport, 439 else if (unlikely(compute_score2(result, net, saddr, sport,
440 daddr, hnum, dif) < badness)) { 440 daddr, hnum, dif) < badness)) {
441 sock_put(result); 441 sock_put(result);
442 goto begin; 442 goto begin;
443 } 443 }
444 } 444 }
445 return result; 445 return result;
446 } 446 }
447 447
448 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try 448 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
449 * harder than this. -DaveM 449 * harder than this. -DaveM
450 */ 450 */
451 struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, 451 struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
452 __be16 sport, __be32 daddr, __be16 dport, 452 __be16 sport, __be32 daddr, __be16 dport,
453 int dif, struct udp_table *udptable) 453 int dif, struct udp_table *udptable)
454 { 454 {
455 struct sock *sk, *result; 455 struct sock *sk, *result;
456 struct hlist_nulls_node *node; 456 struct hlist_nulls_node *node;
457 unsigned short hnum = ntohs(dport); 457 unsigned short hnum = ntohs(dport);
458 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 458 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
459 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 459 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
460 int score, badness; 460 int score, badness;
461 461
462 rcu_read_lock(); 462 rcu_read_lock();
463 if (hslot->count > 10) { 463 if (hslot->count > 10) {
464 hash2 = udp4_portaddr_hash(net, daddr, hnum); 464 hash2 = udp4_portaddr_hash(net, daddr, hnum);
465 slot2 = hash2 & udptable->mask; 465 slot2 = hash2 & udptable->mask;
466 hslot2 = &udptable->hash2[slot2]; 466 hslot2 = &udptable->hash2[slot2];
467 if (hslot->count < hslot2->count) 467 if (hslot->count < hslot2->count)
468 goto begin; 468 goto begin;
469 469
470 result = udp4_lib_lookup2(net, saddr, sport, 470 result = udp4_lib_lookup2(net, saddr, sport,
471 daddr, hnum, dif, 471 daddr, hnum, dif,
472 hslot2, slot2); 472 hslot2, slot2);
473 if (!result) { 473 if (!result) {
474 hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 474 hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
475 slot2 = hash2 & udptable->mask; 475 slot2 = hash2 & udptable->mask;
476 hslot2 = &udptable->hash2[slot2]; 476 hslot2 = &udptable->hash2[slot2];
477 if (hslot->count < hslot2->count) 477 if (hslot->count < hslot2->count)
478 goto begin; 478 goto begin;
479 479
480 result = udp4_lib_lookup2(net, saddr, sport, 480 result = udp4_lib_lookup2(net, saddr, sport,
481 htonl(INADDR_ANY), hnum, dif, 481 htonl(INADDR_ANY), hnum, dif,
482 hslot2, slot2); 482 hslot2, slot2);
483 } 483 }
484 rcu_read_unlock(); 484 rcu_read_unlock();
485 return result; 485 return result;
486 } 486 }
487 begin: 487 begin:
488 result = NULL; 488 result = NULL;
489 badness = -1; 489 badness = -1;
490 sk_nulls_for_each_rcu(sk, node, &hslot->head) { 490 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
491 score = compute_score(sk, net, saddr, hnum, sport, 491 score = compute_score(sk, net, saddr, hnum, sport,
492 daddr, dport, dif); 492 daddr, dport, dif);
493 if (score > badness) { 493 if (score > badness) {
494 result = sk; 494 result = sk;
495 badness = score; 495 badness = score;
496 } 496 }
497 } 497 }
498 /* 498 /*
499 * if the nulls value we got at the end of this lookup is 499 * if the nulls value we got at the end of this lookup is
500 * not the expected one, we must restart lookup. 500 * not the expected one, we must restart lookup.
501 * We probably met an item that was moved to another chain. 501 * We probably met an item that was moved to another chain.
502 */ 502 */
503 if (get_nulls_value(node) != slot) 503 if (get_nulls_value(node) != slot)
504 goto begin; 504 goto begin;
505 505
506 if (result) { 506 if (result) {
507 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 507 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
508 result = NULL; 508 result = NULL;
509 else if (unlikely(compute_score(result, net, saddr, hnum, sport, 509 else if (unlikely(compute_score(result, net, saddr, hnum, sport,
510 daddr, dport, dif) < badness)) { 510 daddr, dport, dif) < badness)) {
511 sock_put(result); 511 sock_put(result);
512 goto begin; 512 goto begin;
513 } 513 }
514 } 514 }
515 rcu_read_unlock(); 515 rcu_read_unlock();
516 return result; 516 return result;
517 } 517 }
518 EXPORT_SYMBOL_GPL(__udp4_lib_lookup); 518 EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
519 519
520 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb, 520 static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
521 __be16 sport, __be16 dport, 521 __be16 sport, __be16 dport,
522 struct udp_table *udptable) 522 struct udp_table *udptable)
523 { 523 {
524 struct sock *sk; 524 struct sock *sk;
525 const struct iphdr *iph = ip_hdr(skb); 525 const struct iphdr *iph = ip_hdr(skb);
526 526
527 if (unlikely(sk = skb_steal_sock(skb))) 527 if (unlikely(sk = skb_steal_sock(skb)))
528 return sk; 528 return sk;
529 else 529 else
530 return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport, 530 return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
531 iph->daddr, dport, inet_iif(skb), 531 iph->daddr, dport, inet_iif(skb),
532 udptable); 532 udptable);
533 } 533 }
534 534
535 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 535 struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
536 __be32 daddr, __be16 dport, int dif) 536 __be32 daddr, __be16 dport, int dif)
537 { 537 {
538 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); 538 return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
539 } 539 }
540 EXPORT_SYMBOL_GPL(udp4_lib_lookup); 540 EXPORT_SYMBOL_GPL(udp4_lib_lookup);
541 541
542 static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, 542 static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
543 __be16 loc_port, __be32 loc_addr, 543 __be16 loc_port, __be32 loc_addr,
544 __be16 rmt_port, __be32 rmt_addr, 544 __be16 rmt_port, __be32 rmt_addr,
545 int dif) 545 int dif)
546 { 546 {
547 struct hlist_nulls_node *node; 547 struct hlist_nulls_node *node;
548 struct sock *s = sk; 548 struct sock *s = sk;
549 unsigned short hnum = ntohs(loc_port); 549 unsigned short hnum = ntohs(loc_port);
550 550
551 sk_nulls_for_each_from(s, node) { 551 sk_nulls_for_each_from(s, node) {
552 struct inet_sock *inet = inet_sk(s); 552 struct inet_sock *inet = inet_sk(s);
553 553
554 if (!net_eq(sock_net(s), net) || 554 if (!net_eq(sock_net(s), net) ||
555 udp_sk(s)->udp_port_hash != hnum || 555 udp_sk(s)->udp_port_hash != hnum ||
556 (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 556 (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
557 (inet->inet_dport != rmt_port && inet->inet_dport) || 557 (inet->inet_dport != rmt_port && inet->inet_dport) ||
558 (inet->inet_rcv_saddr && 558 (inet->inet_rcv_saddr &&
559 inet->inet_rcv_saddr != loc_addr) || 559 inet->inet_rcv_saddr != loc_addr) ||
560 ipv6_only_sock(s) || 560 ipv6_only_sock(s) ||
561 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)) 561 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
562 continue; 562 continue;
563 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif)) 563 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
564 continue; 564 continue;
565 goto found; 565 goto found;
566 } 566 }
567 s = NULL; 567 s = NULL;
568 found: 568 found:
569 return s; 569 return s;
570 } 570 }
571 571
572 /* 572 /*
573 * This routine is called by the ICMP module when it gets some 573 * This routine is called by the ICMP module when it gets some
574 * sort of error condition. If err < 0 then the socket should 574 * sort of error condition. If err < 0 then the socket should
575 * be closed and the error returned to the user. If err > 0 575 * be closed and the error returned to the user. If err > 0
576 * it's just the icmp type << 8 | icmp code. 576 * it's just the icmp type << 8 | icmp code.
577 * Header points to the ip header of the error packet. We move 577 * Header points to the ip header of the error packet. We move
578 * on past this. Then (as it used to claim before adjustment) 578 * on past this. Then (as it used to claim before adjustment)
579 * header points to the first 8 bytes of the udp header. We need 579 * header points to the first 8 bytes of the udp header. We need
580 * to find the appropriate port. 580 * to find the appropriate port.
581 */ 581 */
582 582
583 void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable) 583 void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
584 { 584 {
585 struct inet_sock *inet; 585 struct inet_sock *inet;
586 const struct iphdr *iph = (const struct iphdr *)skb->data; 586 const struct iphdr *iph = (const struct iphdr *)skb->data;
587 struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2)); 587 struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
588 const int type = icmp_hdr(skb)->type; 588 const int type = icmp_hdr(skb)->type;
589 const int code = icmp_hdr(skb)->code; 589 const int code = icmp_hdr(skb)->code;
590 struct sock *sk; 590 struct sock *sk;
591 int harderr; 591 int harderr;
592 int err; 592 int err;
593 struct net *net = dev_net(skb->dev); 593 struct net *net = dev_net(skb->dev);
594 594
595 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 595 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
596 iph->saddr, uh->source, skb->dev->ifindex, udptable); 596 iph->saddr, uh->source, skb->dev->ifindex, udptable);
597 if (sk == NULL) { 597 if (sk == NULL) {
598 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 598 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
599 return; /* No socket for error */ 599 return; /* No socket for error */
600 } 600 }
601 601
602 err = 0; 602 err = 0;
603 harderr = 0; 603 harderr = 0;
604 inet = inet_sk(sk); 604 inet = inet_sk(sk);
605 605
606 switch (type) { 606 switch (type) {
607 default: 607 default:
608 case ICMP_TIME_EXCEEDED: 608 case ICMP_TIME_EXCEEDED:
609 err = EHOSTUNREACH; 609 err = EHOSTUNREACH;
610 break; 610 break;
611 case ICMP_SOURCE_QUENCH: 611 case ICMP_SOURCE_QUENCH:
612 goto out; 612 goto out;
613 case ICMP_PARAMETERPROB: 613 case ICMP_PARAMETERPROB:
614 err = EPROTO; 614 err = EPROTO;
615 harderr = 1; 615 harderr = 1;
616 break; 616 break;
617 case ICMP_DEST_UNREACH: 617 case ICMP_DEST_UNREACH:
618 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */ 618 if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
619 ipv4_sk_update_pmtu(skb, sk, info); 619 ipv4_sk_update_pmtu(skb, sk, info);
620 if (inet->pmtudisc != IP_PMTUDISC_DONT) { 620 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
621 err = EMSGSIZE; 621 err = EMSGSIZE;
622 harderr = 1; 622 harderr = 1;
623 break; 623 break;
624 } 624 }
625 goto out; 625 goto out;
626 } 626 }
627 err = EHOSTUNREACH; 627 err = EHOSTUNREACH;
628 if (code <= NR_ICMP_UNREACH) { 628 if (code <= NR_ICMP_UNREACH) {
629 harderr = icmp_err_convert[code].fatal; 629 harderr = icmp_err_convert[code].fatal;
630 err = icmp_err_convert[code].errno; 630 err = icmp_err_convert[code].errno;
631 } 631 }
632 break; 632 break;
633 case ICMP_REDIRECT: 633 case ICMP_REDIRECT:
634 ipv4_sk_redirect(skb, sk); 634 ipv4_sk_redirect(skb, sk);
635 break; 635 break;
636 } 636 }
637 637
638 /* 638 /*
639 * RFC1122: OK. Passes ICMP errors back to application, as per 639 * RFC1122: OK. Passes ICMP errors back to application, as per
640 * 4.1.3.3. 640 * 4.1.3.3.
641 */ 641 */
642 if (!inet->recverr) { 642 if (!inet->recverr) {
643 if (!harderr || sk->sk_state != TCP_ESTABLISHED) 643 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
644 goto out; 644 goto out;
645 } else 645 } else
646 ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1)); 646 ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
647 647
648 sk->sk_err = err; 648 sk->sk_err = err;
649 sk->sk_error_report(sk); 649 sk->sk_error_report(sk);
650 out: 650 out:
651 sock_put(sk); 651 sock_put(sk);
652 } 652 }
653 653
654 void udp_err(struct sk_buff *skb, u32 info) 654 void udp_err(struct sk_buff *skb, u32 info)
655 { 655 {
656 __udp4_lib_err(skb, info, &udp_table); 656 __udp4_lib_err(skb, info, &udp_table);
657 } 657 }
658 658
659 /* 659 /*
660 * Throw away all pending data and cancel the corking. Socket is locked. 660 * Throw away all pending data and cancel the corking. Socket is locked.
661 */ 661 */
662 void udp_flush_pending_frames(struct sock *sk) 662 void udp_flush_pending_frames(struct sock *sk)
663 { 663 {
664 struct udp_sock *up = udp_sk(sk); 664 struct udp_sock *up = udp_sk(sk);
665 665
666 if (up->pending) { 666 if (up->pending) {
667 up->len = 0; 667 up->len = 0;
668 up->pending = 0; 668 up->pending = 0;
669 ip_flush_pending_frames(sk); 669 ip_flush_pending_frames(sk);
670 } 670 }
671 } 671 }
672 EXPORT_SYMBOL(udp_flush_pending_frames); 672 EXPORT_SYMBOL(udp_flush_pending_frames);
673 673
674 /** 674 /**
675 * udp4_hwcsum - handle outgoing HW checksumming 675 * udp4_hwcsum - handle outgoing HW checksumming
676 * @skb: sk_buff containing the filled-in UDP header 676 * @skb: sk_buff containing the filled-in UDP header
677 * (checksum field must be zeroed out) 677 * (checksum field must be zeroed out)
678 * @src: source IP address 678 * @src: source IP address
679 * @dst: destination IP address 679 * @dst: destination IP address
680 */ 680 */
681 static void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst) 681 static void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
682 { 682 {
683 struct udphdr *uh = udp_hdr(skb); 683 struct udphdr *uh = udp_hdr(skb);
684 struct sk_buff *frags = skb_shinfo(skb)->frag_list; 684 struct sk_buff *frags = skb_shinfo(skb)->frag_list;
685 int offset = skb_transport_offset(skb); 685 int offset = skb_transport_offset(skb);
686 int len = skb->len - offset; 686 int len = skb->len - offset;
687 int hlen = len; 687 int hlen = len;
688 __wsum csum = 0; 688 __wsum csum = 0;
689 689
690 if (!frags) { 690 if (!frags) {
691 /* 691 /*
692 * Only one fragment on the socket. 692 * Only one fragment on the socket.
693 */ 693 */
694 skb->csum_start = skb_transport_header(skb) - skb->head; 694 skb->csum_start = skb_transport_header(skb) - skb->head;
695 skb->csum_offset = offsetof(struct udphdr, check); 695 skb->csum_offset = offsetof(struct udphdr, check);
696 uh->check = ~csum_tcpudp_magic(src, dst, len, 696 uh->check = ~csum_tcpudp_magic(src, dst, len,
697 IPPROTO_UDP, 0); 697 IPPROTO_UDP, 0);
698 } else { 698 } else {
699 /* 699 /*
700 * HW-checksum won't work as there are two or more 700 * HW-checksum won't work as there are two or more
701 * fragments on the socket so that all csums of sk_buffs 701 * fragments on the socket so that all csums of sk_buffs
702 * should be together 702 * should be together
703 */ 703 */
704 do { 704 do {
705 csum = csum_add(csum, frags->csum); 705 csum = csum_add(csum, frags->csum);
706 hlen -= frags->len; 706 hlen -= frags->len;
707 } while ((frags = frags->next)); 707 } while ((frags = frags->next));
708 708
709 csum = skb_checksum(skb, offset, hlen, csum); 709 csum = skb_checksum(skb, offset, hlen, csum);
710 skb->ip_summed = CHECKSUM_NONE; 710 skb->ip_summed = CHECKSUM_NONE;
711 711
712 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum); 712 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
713 if (uh->check == 0) 713 if (uh->check == 0)
714 uh->check = CSUM_MANGLED_0; 714 uh->check = CSUM_MANGLED_0;
715 } 715 }
716 } 716 }
717 717
718 static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4) 718 static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
719 { 719 {
720 struct sock *sk = skb->sk; 720 struct sock *sk = skb->sk;
721 struct inet_sock *inet = inet_sk(sk); 721 struct inet_sock *inet = inet_sk(sk);
722 struct udphdr *uh; 722 struct udphdr *uh;
723 int err = 0; 723 int err = 0;
724 int is_udplite = IS_UDPLITE(sk); 724 int is_udplite = IS_UDPLITE(sk);
725 int offset = skb_transport_offset(skb); 725 int offset = skb_transport_offset(skb);
726 int len = skb->len - offset; 726 int len = skb->len - offset;
727 __wsum csum = 0; 727 __wsum csum = 0;
728 728
729 /* 729 /*
730 * Create a UDP header 730 * Create a UDP header
731 */ 731 */
732 uh = udp_hdr(skb); 732 uh = udp_hdr(skb);
733 uh->source = inet->inet_sport; 733 uh->source = inet->inet_sport;
734 uh->dest = fl4->fl4_dport; 734 uh->dest = fl4->fl4_dport;
735 uh->len = htons(len); 735 uh->len = htons(len);
736 uh->check = 0; 736 uh->check = 0;
737 737
738 if (is_udplite) /* UDP-Lite */ 738 if (is_udplite) /* UDP-Lite */
739 csum = udplite_csum(skb); 739 csum = udplite_csum(skb);
740 740
741 else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */ 741 else if (sk->sk_no_check == UDP_CSUM_NOXMIT) { /* UDP csum disabled */
742 742
743 skb->ip_summed = CHECKSUM_NONE; 743 skb->ip_summed = CHECKSUM_NONE;
744 goto send; 744 goto send;
745 745
746 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 746 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
747 747
748 udp4_hwcsum(skb, fl4->saddr, fl4->daddr); 748 udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
749 goto send; 749 goto send;
750 750
751 } else 751 } else
752 csum = udp_csum(skb); 752 csum = udp_csum(skb);
753 753
754 /* add protocol-dependent pseudo-header */ 754 /* add protocol-dependent pseudo-header */
755 uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len, 755 uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
756 sk->sk_protocol, csum); 756 sk->sk_protocol, csum);
757 if (uh->check == 0) 757 if (uh->check == 0)
758 uh->check = CSUM_MANGLED_0; 758 uh->check = CSUM_MANGLED_0;
759 759
760 send: 760 send:
761 err = ip_send_skb(sock_net(sk), skb); 761 err = ip_send_skb(sock_net(sk), skb);
762 if (err) { 762 if (err) {
763 if (err == -ENOBUFS && !inet->recverr) { 763 if (err == -ENOBUFS && !inet->recverr) {
764 UDP_INC_STATS_USER(sock_net(sk), 764 UDP_INC_STATS_USER(sock_net(sk),
765 UDP_MIB_SNDBUFERRORS, is_udplite); 765 UDP_MIB_SNDBUFERRORS, is_udplite);
766 err = 0; 766 err = 0;
767 } 767 }
768 } else 768 } else
769 UDP_INC_STATS_USER(sock_net(sk), 769 UDP_INC_STATS_USER(sock_net(sk),
770 UDP_MIB_OUTDATAGRAMS, is_udplite); 770 UDP_MIB_OUTDATAGRAMS, is_udplite);
771 return err; 771 return err;
772 } 772 }
773 773
774 /* 774 /*
775 * Push out all pending data as one UDP datagram. Socket is locked. 775 * Push out all pending data as one UDP datagram. Socket is locked.
776 */ 776 */
777 static int udp_push_pending_frames(struct sock *sk) 777 static int udp_push_pending_frames(struct sock *sk)
778 { 778 {
779 struct udp_sock *up = udp_sk(sk); 779 struct udp_sock *up = udp_sk(sk);
780 struct inet_sock *inet = inet_sk(sk); 780 struct inet_sock *inet = inet_sk(sk);
781 struct flowi4 *fl4 = &inet->cork.fl.u.ip4; 781 struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
782 struct sk_buff *skb; 782 struct sk_buff *skb;
783 int err = 0; 783 int err = 0;
784 784
785 skb = ip_finish_skb(sk, fl4); 785 skb = ip_finish_skb(sk, fl4);
786 if (!skb) 786 if (!skb)
787 goto out; 787 goto out;
788 788
789 err = udp_send_skb(skb, fl4); 789 err = udp_send_skb(skb, fl4);
790 790
791 out: 791 out:
792 up->len = 0; 792 up->len = 0;
793 up->pending = 0; 793 up->pending = 0;
794 return err; 794 return err;
795 } 795 }
796 796
797 int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 797 int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
798 size_t len) 798 size_t len)
799 { 799 {
800 struct inet_sock *inet = inet_sk(sk); 800 struct inet_sock *inet = inet_sk(sk);
801 struct udp_sock *up = udp_sk(sk); 801 struct udp_sock *up = udp_sk(sk);
802 struct flowi4 fl4_stack; 802 struct flowi4 fl4_stack;
803 struct flowi4 *fl4; 803 struct flowi4 *fl4;
804 int ulen = len; 804 int ulen = len;
805 struct ipcm_cookie ipc; 805 struct ipcm_cookie ipc;
806 struct rtable *rt = NULL; 806 struct rtable *rt = NULL;
807 int free = 0; 807 int free = 0;
808 int connected = 0; 808 int connected = 0;
809 __be32 daddr, faddr, saddr; 809 __be32 daddr, faddr, saddr;
810 __be16 dport; 810 __be16 dport;
811 u8 tos; 811 u8 tos;
812 int err, is_udplite = IS_UDPLITE(sk); 812 int err, is_udplite = IS_UDPLITE(sk);
813 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 813 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
814 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 814 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
815 struct sk_buff *skb; 815 struct sk_buff *skb;
816 struct ip_options_data opt_copy; 816 struct ip_options_data opt_copy;
817 817
818 if (len > 0xFFFF) 818 if (len > 0xFFFF)
819 return -EMSGSIZE; 819 return -EMSGSIZE;
820 820
821 /* 821 /*
822 * Check the flags. 822 * Check the flags.
823 */ 823 */
824 824
825 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */ 825 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
826 return -EOPNOTSUPP; 826 return -EOPNOTSUPP;
827 827
828 ipc.opt = NULL; 828 ipc.opt = NULL;
829 ipc.tx_flags = 0; 829 ipc.tx_flags = 0;
830 830
831 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 831 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
832 832
833 fl4 = &inet->cork.fl.u.ip4; 833 fl4 = &inet->cork.fl.u.ip4;
834 if (up->pending) { 834 if (up->pending) {
835 /* 835 /*
836 * There are pending frames. 836 * There are pending frames.
837 * The socket lock must be held while it's corked. 837 * The socket lock must be held while it's corked.
838 */ 838 */
839 lock_sock(sk); 839 lock_sock(sk);
840 if (likely(up->pending)) { 840 if (likely(up->pending)) {
841 if (unlikely(up->pending != AF_INET)) { 841 if (unlikely(up->pending != AF_INET)) {
842 release_sock(sk); 842 release_sock(sk);
843 return -EINVAL; 843 return -EINVAL;
844 } 844 }
845 goto do_append_data; 845 goto do_append_data;
846 } 846 }
847 release_sock(sk); 847 release_sock(sk);
848 } 848 }
849 ulen += sizeof(struct udphdr); 849 ulen += sizeof(struct udphdr);
850 850
851 /* 851 /*
852 * Get and verify the address. 852 * Get and verify the address.
853 */ 853 */
854 if (msg->msg_name) { 854 if (msg->msg_name) {
855 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name; 855 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
856 if (msg->msg_namelen < sizeof(*usin)) 856 if (msg->msg_namelen < sizeof(*usin))
857 return -EINVAL; 857 return -EINVAL;
858 if (usin->sin_family != AF_INET) { 858 if (usin->sin_family != AF_INET) {
859 if (usin->sin_family != AF_UNSPEC) 859 if (usin->sin_family != AF_UNSPEC)
860 return -EAFNOSUPPORT; 860 return -EAFNOSUPPORT;
861 } 861 }
862 862
863 daddr = usin->sin_addr.s_addr; 863 daddr = usin->sin_addr.s_addr;
864 dport = usin->sin_port; 864 dport = usin->sin_port;
865 if (dport == 0) 865 if (dport == 0)
866 return -EINVAL; 866 return -EINVAL;
867 } else { 867 } else {
868 if (sk->sk_state != TCP_ESTABLISHED) 868 if (sk->sk_state != TCP_ESTABLISHED)
869 return -EDESTADDRREQ; 869 return -EDESTADDRREQ;
870 daddr = inet->inet_daddr; 870 daddr = inet->inet_daddr;
871 dport = inet->inet_dport; 871 dport = inet->inet_dport;
872 /* Open fast path for connected socket. 872 /* Open fast path for connected socket.
873 Route will not be used, if at least one option is set. 873 Route will not be used, if at least one option is set.
874 */ 874 */
875 connected = 1; 875 connected = 1;
876 } 876 }
877 ipc.addr = inet->inet_saddr; 877 ipc.addr = inet->inet_saddr;
878 878
879 ipc.oif = sk->sk_bound_dev_if; 879 ipc.oif = sk->sk_bound_dev_if;
880 err = sock_tx_timestamp(sk, &ipc.tx_flags); 880 err = sock_tx_timestamp(sk, &ipc.tx_flags);
881 if (err) 881 if (err)
882 return err; 882 return err;
883 if (msg->msg_controllen) { 883 if (msg->msg_controllen) {
884 err = ip_cmsg_send(sock_net(sk), msg, &ipc); 884 err = ip_cmsg_send(sock_net(sk), msg, &ipc);
885 if (err) 885 if (err)
886 return err; 886 return err;
887 if (ipc.opt) 887 if (ipc.opt)
888 free = 1; 888 free = 1;
889 connected = 0; 889 connected = 0;
890 } 890 }
891 if (!ipc.opt) { 891 if (!ipc.opt) {
892 struct ip_options_rcu *inet_opt; 892 struct ip_options_rcu *inet_opt;
893 893
894 rcu_read_lock(); 894 rcu_read_lock();
895 inet_opt = rcu_dereference(inet->inet_opt); 895 inet_opt = rcu_dereference(inet->inet_opt);
896 if (inet_opt) { 896 if (inet_opt) {
897 memcpy(&opt_copy, inet_opt, 897 memcpy(&opt_copy, inet_opt,
898 sizeof(*inet_opt) + inet_opt->opt.optlen); 898 sizeof(*inet_opt) + inet_opt->opt.optlen);
899 ipc.opt = &opt_copy.opt; 899 ipc.opt = &opt_copy.opt;
900 } 900 }
901 rcu_read_unlock(); 901 rcu_read_unlock();
902 } 902 }
903 903
904 saddr = ipc.addr; 904 saddr = ipc.addr;
905 ipc.addr = faddr = daddr; 905 ipc.addr = faddr = daddr;
906 906
907 if (ipc.opt && ipc.opt->opt.srr) { 907 if (ipc.opt && ipc.opt->opt.srr) {
908 if (!daddr) 908 if (!daddr)
909 return -EINVAL; 909 return -EINVAL;
910 faddr = ipc.opt->opt.faddr; 910 faddr = ipc.opt->opt.faddr;
911 connected = 0; 911 connected = 0;
912 } 912 }
913 tos = RT_TOS(inet->tos); 913 tos = RT_TOS(inet->tos);
914 if (sock_flag(sk, SOCK_LOCALROUTE) || 914 if (sock_flag(sk, SOCK_LOCALROUTE) ||
915 (msg->msg_flags & MSG_DONTROUTE) || 915 (msg->msg_flags & MSG_DONTROUTE) ||
916 (ipc.opt && ipc.opt->opt.is_strictroute)) { 916 (ipc.opt && ipc.opt->opt.is_strictroute)) {
917 tos |= RTO_ONLINK; 917 tos |= RTO_ONLINK;
918 connected = 0; 918 connected = 0;
919 } 919 }
920 920
921 if (ipv4_is_multicast(daddr)) { 921 if (ipv4_is_multicast(daddr)) {
922 if (!ipc.oif) 922 if (!ipc.oif)
923 ipc.oif = inet->mc_index; 923 ipc.oif = inet->mc_index;
924 if (!saddr) 924 if (!saddr)
925 saddr = inet->mc_addr; 925 saddr = inet->mc_addr;
926 connected = 0; 926 connected = 0;
927 } else if (!ipc.oif) 927 } else if (!ipc.oif)
928 ipc.oif = inet->uc_index; 928 ipc.oif = inet->uc_index;
929 929
930 if (connected) 930 if (connected)
931 rt = (struct rtable *)sk_dst_check(sk, 0); 931 rt = (struct rtable *)sk_dst_check(sk, 0);
932 932
933 if (rt == NULL) { 933 if (rt == NULL) {
934 struct net *net = sock_net(sk); 934 struct net *net = sock_net(sk);
935 935
936 fl4 = &fl4_stack; 936 fl4 = &fl4_stack;
937 flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos, 937 flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
938 RT_SCOPE_UNIVERSE, sk->sk_protocol, 938 RT_SCOPE_UNIVERSE, sk->sk_protocol,
939 inet_sk_flowi_flags(sk)|FLOWI_FLAG_CAN_SLEEP, 939 inet_sk_flowi_flags(sk)|FLOWI_FLAG_CAN_SLEEP,
940 faddr, saddr, dport, inet->inet_sport); 940 faddr, saddr, dport, inet->inet_sport);
941 941
942 security_sk_classify_flow(sk, flowi4_to_flowi(fl4)); 942 security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
943 rt = ip_route_output_flow(net, fl4, sk); 943 rt = ip_route_output_flow(net, fl4, sk);
944 if (IS_ERR(rt)) { 944 if (IS_ERR(rt)) {
945 err = PTR_ERR(rt); 945 err = PTR_ERR(rt);
946 rt = NULL; 946 rt = NULL;
947 if (err == -ENETUNREACH) 947 if (err == -ENETUNREACH)
948 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); 948 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
949 goto out; 949 goto out;
950 } 950 }
951 951
952 err = -EACCES; 952 err = -EACCES;
953 if ((rt->rt_flags & RTCF_BROADCAST) && 953 if ((rt->rt_flags & RTCF_BROADCAST) &&
954 !sock_flag(sk, SOCK_BROADCAST)) 954 !sock_flag(sk, SOCK_BROADCAST))
955 goto out; 955 goto out;
956 if (connected) 956 if (connected)
957 sk_dst_set(sk, dst_clone(&rt->dst)); 957 sk_dst_set(sk, dst_clone(&rt->dst));
958 } 958 }
959 959
960 if (msg->msg_flags&MSG_CONFIRM) 960 if (msg->msg_flags&MSG_CONFIRM)
961 goto do_confirm; 961 goto do_confirm;
962 back_from_confirm: 962 back_from_confirm:
963 963
964 saddr = fl4->saddr; 964 saddr = fl4->saddr;
965 if (!ipc.addr) 965 if (!ipc.addr)
966 daddr = ipc.addr = fl4->daddr; 966 daddr = ipc.addr = fl4->daddr;
967 967
968 /* Lockless fast path for the non-corking case. */ 968 /* Lockless fast path for the non-corking case. */
969 if (!corkreq) { 969 if (!corkreq) {
970 skb = ip_make_skb(sk, fl4, getfrag, msg->msg_iov, ulen, 970 skb = ip_make_skb(sk, fl4, getfrag, msg->msg_iov, ulen,
971 sizeof(struct udphdr), &ipc, &rt, 971 sizeof(struct udphdr), &ipc, &rt,
972 msg->msg_flags); 972 msg->msg_flags);
973 err = PTR_ERR(skb); 973 err = PTR_ERR(skb);
974 if (skb && !IS_ERR(skb)) 974 if (skb && !IS_ERR(skb))
975 err = udp_send_skb(skb, fl4); 975 err = udp_send_skb(skb, fl4);
976 goto out; 976 goto out;
977 } 977 }
978 978
979 lock_sock(sk); 979 lock_sock(sk);
980 if (unlikely(up->pending)) { 980 if (unlikely(up->pending)) {
981 /* The socket is already corked while preparing it. */ 981 /* The socket is already corked while preparing it. */
982 /* ... which is an evident application bug. --ANK */ 982 /* ... which is an evident application bug. --ANK */
983 release_sock(sk); 983 release_sock(sk);
984 984
985 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("cork app bug 2\n")); 985 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("cork app bug 2\n"));
986 err = -EINVAL; 986 err = -EINVAL;
987 goto out; 987 goto out;
988 } 988 }
989 /* 989 /*
990 * Now cork the socket to pend data. 990 * Now cork the socket to pend data.
991 */ 991 */
992 fl4 = &inet->cork.fl.u.ip4; 992 fl4 = &inet->cork.fl.u.ip4;
993 fl4->daddr = daddr; 993 fl4->daddr = daddr;
994 fl4->saddr = saddr; 994 fl4->saddr = saddr;
995 fl4->fl4_dport = dport; 995 fl4->fl4_dport = dport;
996 fl4->fl4_sport = inet->inet_sport; 996 fl4->fl4_sport = inet->inet_sport;
997 up->pending = AF_INET; 997 up->pending = AF_INET;
998 998
999 do_append_data: 999 do_append_data:
1000 up->len += ulen; 1000 up->len += ulen;
1001 err = ip_append_data(sk, fl4, getfrag, msg->msg_iov, ulen, 1001 err = ip_append_data(sk, fl4, getfrag, msg->msg_iov, ulen,
1002 sizeof(struct udphdr), &ipc, &rt, 1002 sizeof(struct udphdr), &ipc, &rt,
1003 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); 1003 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1004 if (err) 1004 if (err)
1005 udp_flush_pending_frames(sk); 1005 udp_flush_pending_frames(sk);
1006 else if (!corkreq) 1006 else if (!corkreq)
1007 err = udp_push_pending_frames(sk); 1007 err = udp_push_pending_frames(sk);
1008 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1008 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1009 up->pending = 0; 1009 up->pending = 0;
1010 release_sock(sk); 1010 release_sock(sk);
1011 1011
1012 out: 1012 out:
1013 ip_rt_put(rt); 1013 ip_rt_put(rt);
1014 if (free) 1014 if (free)
1015 kfree(ipc.opt); 1015 kfree(ipc.opt);
1016 if (!err) 1016 if (!err)
1017 return len; 1017 return len;
1018 /* 1018 /*
1019 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1019 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1020 * ENOBUFS might not be good (it's not tunable per se), but otherwise 1020 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1021 * we don't have a good statistic (IpOutDiscards but it can be too many 1021 * we don't have a good statistic (IpOutDiscards but it can be too many
1022 * things). We could add another new stat but at least for now that 1022 * things). We could add another new stat but at least for now that
1023 * seems like overkill. 1023 * seems like overkill.
1024 */ 1024 */
1025 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1025 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1026 UDP_INC_STATS_USER(sock_net(sk), 1026 UDP_INC_STATS_USER(sock_net(sk),
1027 UDP_MIB_SNDBUFERRORS, is_udplite); 1027 UDP_MIB_SNDBUFERRORS, is_udplite);
1028 } 1028 }
1029 return err; 1029 return err;
1030 1030
1031 do_confirm: 1031 do_confirm:
1032 dst_confirm(&rt->dst); 1032 dst_confirm(&rt->dst);
1033 if (!(msg->msg_flags&MSG_PROBE) || len) 1033 if (!(msg->msg_flags&MSG_PROBE) || len)
1034 goto back_from_confirm; 1034 goto back_from_confirm;
1035 err = 0; 1035 err = 0;
1036 goto out; 1036 goto out;
1037 } 1037 }
1038 EXPORT_SYMBOL(udp_sendmsg); 1038 EXPORT_SYMBOL(udp_sendmsg);
1039 1039
1040 int udp_sendpage(struct sock *sk, struct page *page, int offset, 1040 int udp_sendpage(struct sock *sk, struct page *page, int offset,
1041 size_t size, int flags) 1041 size_t size, int flags)
1042 { 1042 {
1043 struct inet_sock *inet = inet_sk(sk); 1043 struct inet_sock *inet = inet_sk(sk);
1044 struct udp_sock *up = udp_sk(sk); 1044 struct udp_sock *up = udp_sk(sk);
1045 int ret; 1045 int ret;
1046 1046
1047 if (!up->pending) { 1047 if (!up->pending) {
1048 struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1048 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
1049 1049
1050 /* Call udp_sendmsg to specify destination address which 1050 /* Call udp_sendmsg to specify destination address which
1051 * sendpage interface can't pass. 1051 * sendpage interface can't pass.
1052 * This will succeed only when the socket is connected. 1052 * This will succeed only when the socket is connected.
1053 */ 1053 */
1054 ret = udp_sendmsg(NULL, sk, &msg, 0); 1054 ret = udp_sendmsg(NULL, sk, &msg, 0);
1055 if (ret < 0) 1055 if (ret < 0)
1056 return ret; 1056 return ret;
1057 } 1057 }
1058 1058
1059 lock_sock(sk); 1059 lock_sock(sk);
1060 1060
1061 if (unlikely(!up->pending)) { 1061 if (unlikely(!up->pending)) {
1062 release_sock(sk); 1062 release_sock(sk);
1063 1063
1064 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("udp cork app bug 3\n")); 1064 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("udp cork app bug 3\n"));
1065 return -EINVAL; 1065 return -EINVAL;
1066 } 1066 }
1067 1067
1068 ret = ip_append_page(sk, &inet->cork.fl.u.ip4, 1068 ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1069 page, offset, size, flags); 1069 page, offset, size, flags);
1070 if (ret == -EOPNOTSUPP) { 1070 if (ret == -EOPNOTSUPP) {
1071 release_sock(sk); 1071 release_sock(sk);
1072 return sock_no_sendpage(sk->sk_socket, page, offset, 1072 return sock_no_sendpage(sk->sk_socket, page, offset,
1073 size, flags); 1073 size, flags);
1074 } 1074 }
1075 if (ret < 0) { 1075 if (ret < 0) {
1076 udp_flush_pending_frames(sk); 1076 udp_flush_pending_frames(sk);
1077 goto out; 1077 goto out;
1078 } 1078 }
1079 1079
1080 up->len += size; 1080 up->len += size;
1081 if (!(up->corkflag || (flags&MSG_MORE))) 1081 if (!(up->corkflag || (flags&MSG_MORE)))
1082 ret = udp_push_pending_frames(sk); 1082 ret = udp_push_pending_frames(sk);
1083 if (!ret) 1083 if (!ret)
1084 ret = size; 1084 ret = size;
1085 out: 1085 out:
1086 release_sock(sk); 1086 release_sock(sk);
1087 return ret; 1087 return ret;
1088 } 1088 }
1089 1089
1090 1090
1091 /** 1091 /**
1092 * first_packet_length - return length of first packet in receive queue 1092 * first_packet_length - return length of first packet in receive queue
1093 * @sk: socket 1093 * @sk: socket
1094 * 1094 *
1095 * Drops all bad checksum frames, until a valid one is found. 1095 * Drops all bad checksum frames, until a valid one is found.
1096 * Returns the length of found skb, or 0 if none is found. 1096 * Returns the length of found skb, or 0 if none is found.
1097 */ 1097 */
1098 static unsigned int first_packet_length(struct sock *sk) 1098 static unsigned int first_packet_length(struct sock *sk)
1099 { 1099 {
1100 struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue; 1100 struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
1101 struct sk_buff *skb; 1101 struct sk_buff *skb;
1102 unsigned int res; 1102 unsigned int res;
1103 1103
1104 __skb_queue_head_init(&list_kill); 1104 __skb_queue_head_init(&list_kill);
1105 1105
1106 spin_lock_bh(&rcvq->lock); 1106 spin_lock_bh(&rcvq->lock);
1107 while ((skb = skb_peek(rcvq)) != NULL && 1107 while ((skb = skb_peek(rcvq)) != NULL &&
1108 udp_lib_checksum_complete(skb)) { 1108 udp_lib_checksum_complete(skb)) {
1109 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, 1109 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1110 IS_UDPLITE(sk)); 1110 IS_UDPLITE(sk));
1111 atomic_inc(&sk->sk_drops); 1111 atomic_inc(&sk->sk_drops);
1112 __skb_unlink(skb, rcvq); 1112 __skb_unlink(skb, rcvq);
1113 __skb_queue_tail(&list_kill, skb); 1113 __skb_queue_tail(&list_kill, skb);
1114 } 1114 }
1115 res = skb ? skb->len : 0; 1115 res = skb ? skb->len : 0;
1116 spin_unlock_bh(&rcvq->lock); 1116 spin_unlock_bh(&rcvq->lock);
1117 1117
1118 if (!skb_queue_empty(&list_kill)) { 1118 if (!skb_queue_empty(&list_kill)) {
1119 bool slow = lock_sock_fast(sk); 1119 bool slow = lock_sock_fast(sk);
1120 1120
1121 __skb_queue_purge(&list_kill); 1121 __skb_queue_purge(&list_kill);
1122 sk_mem_reclaim_partial(sk); 1122 sk_mem_reclaim_partial(sk);
1123 unlock_sock_fast(sk, slow); 1123 unlock_sock_fast(sk, slow);
1124 } 1124 }
1125 return res; 1125 return res;
1126 } 1126 }
1127 1127
1128 /* 1128 /*
1129 * IOCTL requests applicable to the UDP protocol 1129 * IOCTL requests applicable to the UDP protocol
1130 */ 1130 */
1131 1131
1132 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) 1132 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
1133 { 1133 {
1134 switch (cmd) { 1134 switch (cmd) {
1135 case SIOCOUTQ: 1135 case SIOCOUTQ:
1136 { 1136 {
1137 int amount = sk_wmem_alloc_get(sk); 1137 int amount = sk_wmem_alloc_get(sk);
1138 1138
1139 return put_user(amount, (int __user *)arg); 1139 return put_user(amount, (int __user *)arg);
1140 } 1140 }
1141 1141
1142 case SIOCINQ: 1142 case SIOCINQ:
1143 { 1143 {
1144 unsigned int amount = first_packet_length(sk); 1144 unsigned int amount = first_packet_length(sk);
1145 1145
1146 if (amount) 1146 if (amount)
1147 /* 1147 /*
1148 * We will only return the amount 1148 * We will only return the amount
1149 * of this packet since that is all 1149 * of this packet since that is all
1150 * that will be read. 1150 * that will be read.
1151 */ 1151 */
1152 amount -= sizeof(struct udphdr); 1152 amount -= sizeof(struct udphdr);
1153 1153
1154 return put_user(amount, (int __user *)arg); 1154 return put_user(amount, (int __user *)arg);
1155 } 1155 }
1156 1156
1157 default: 1157 default:
1158 return -ENOIOCTLCMD; 1158 return -ENOIOCTLCMD;
1159 } 1159 }
1160 1160
1161 return 0; 1161 return 0;
1162 } 1162 }
1163 EXPORT_SYMBOL(udp_ioctl); 1163 EXPORT_SYMBOL(udp_ioctl);
1164 1164
1165 /* 1165 /*
1166 * This should be easy, if there is something there we 1166 * This should be easy, if there is something there we
1167 * return it, otherwise we block. 1167 * return it, otherwise we block.
1168 */ 1168 */
1169 1169
1170 int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 1170 int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1171 size_t len, int noblock, int flags, int *addr_len) 1171 size_t len, int noblock, int flags, int *addr_len)
1172 { 1172 {
1173 struct inet_sock *inet = inet_sk(sk); 1173 struct inet_sock *inet = inet_sk(sk);
1174 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; 1174 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
1175 struct sk_buff *skb; 1175 struct sk_buff *skb;
1176 unsigned int ulen, copied; 1176 unsigned int ulen, copied;
1177 int peeked, off = 0; 1177 int peeked, off = 0;
1178 int err; 1178 int err;
1179 int is_udplite = IS_UDPLITE(sk); 1179 int is_udplite = IS_UDPLITE(sk);
1180 bool slow; 1180 bool slow;
1181 1181
1182 /* 1182 /*
1183 * Check any passed addresses 1183 * Check any passed addresses
1184 */ 1184 */
1185 if (addr_len) 1185 if (addr_len)
1186 *addr_len = sizeof(*sin); 1186 *addr_len = sizeof(*sin);
1187 1187
1188 if (flags & MSG_ERRQUEUE) 1188 if (flags & MSG_ERRQUEUE)
1189 return ip_recv_error(sk, msg, len); 1189 return ip_recv_error(sk, msg, len);
1190 1190
1191 try_again: 1191 try_again:
1192 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 1192 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
1193 &peeked, &off, &err); 1193 &peeked, &off, &err);
1194 if (!skb) 1194 if (!skb)
1195 goto out; 1195 goto out;
1196 1196
1197 ulen = skb->len - sizeof(struct udphdr); 1197 ulen = skb->len - sizeof(struct udphdr);
1198 copied = len; 1198 copied = len;
1199 if (copied > ulen) 1199 if (copied > ulen)
1200 copied = ulen; 1200 copied = ulen;
1201 else if (copied < ulen) 1201 else if (copied < ulen)
1202 msg->msg_flags |= MSG_TRUNC; 1202 msg->msg_flags |= MSG_TRUNC;
1203 1203
1204 /* 1204 /*
1205 * If checksum is needed at all, try to do it while copying the 1205 * If checksum is needed at all, try to do it while copying the
1206 * data. If the data is truncated, or if we only want a partial 1206 * data. If the data is truncated, or if we only want a partial
1207 * coverage checksum (UDP-Lite), do it before the copy. 1207 * coverage checksum (UDP-Lite), do it before the copy.
1208 */ 1208 */
1209 1209
1210 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 1210 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1211 if (udp_lib_checksum_complete(skb)) 1211 if (udp_lib_checksum_complete(skb))
1212 goto csum_copy_err; 1212 goto csum_copy_err;
1213 } 1213 }
1214 1214
1215 if (skb_csum_unnecessary(skb)) 1215 if (skb_csum_unnecessary(skb))
1216 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 1216 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
1217 msg->msg_iov, copied); 1217 msg->msg_iov, copied);
1218 else { 1218 else {
1219 err = skb_copy_and_csum_datagram_iovec(skb, 1219 err = skb_copy_and_csum_datagram_iovec(skb,
1220 sizeof(struct udphdr), 1220 sizeof(struct udphdr),
1221 msg->msg_iov); 1221 msg->msg_iov);
1222 1222
1223 if (err == -EINVAL) 1223 if (err == -EINVAL)
1224 goto csum_copy_err; 1224 goto csum_copy_err;
1225 } 1225 }
1226 1226
1227 if (unlikely(err)) { 1227 if (unlikely(err)) {
1228 trace_kfree_skb(skb, udp_recvmsg); 1228 trace_kfree_skb(skb, udp_recvmsg);
1229 if (!peeked) {
1230 atomic_inc(&sk->sk_drops);
1231 UDP_INC_STATS_USER(sock_net(sk),
1232 UDP_MIB_INERRORS, is_udplite);
1233 }
1229 goto out_free; 1234 goto out_free;
1230 } 1235 }
1231 1236
1232 if (!peeked) 1237 if (!peeked)
1233 UDP_INC_STATS_USER(sock_net(sk), 1238 UDP_INC_STATS_USER(sock_net(sk),
1234 UDP_MIB_INDATAGRAMS, is_udplite); 1239 UDP_MIB_INDATAGRAMS, is_udplite);
1235 1240
1236 sock_recv_ts_and_drops(msg, sk, skb); 1241 sock_recv_ts_and_drops(msg, sk, skb);
1237 1242
1238 /* Copy the address. */ 1243 /* Copy the address. */
1239 if (sin) { 1244 if (sin) {
1240 sin->sin_family = AF_INET; 1245 sin->sin_family = AF_INET;
1241 sin->sin_port = udp_hdr(skb)->source; 1246 sin->sin_port = udp_hdr(skb)->source;
1242 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1247 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1243 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1248 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1244 } 1249 }
1245 if (inet->cmsg_flags) 1250 if (inet->cmsg_flags)
1246 ip_cmsg_recv(msg, skb); 1251 ip_cmsg_recv(msg, skb);
1247 1252
1248 err = copied; 1253 err = copied;
1249 if (flags & MSG_TRUNC) 1254 if (flags & MSG_TRUNC)
1250 err = ulen; 1255 err = ulen;
1251 1256
1252 out_free: 1257 out_free:
1253 skb_free_datagram_locked(sk, skb); 1258 skb_free_datagram_locked(sk, skb);
1254 out: 1259 out:
1255 return err; 1260 return err;
1256 1261
1257 csum_copy_err: 1262 csum_copy_err:
1258 slow = lock_sock_fast(sk); 1263 slow = lock_sock_fast(sk);
1259 if (!skb_kill_datagram(sk, skb, flags)) 1264 if (!skb_kill_datagram(sk, skb, flags))
1260 UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1265 UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1261 unlock_sock_fast(sk, slow); 1266 unlock_sock_fast(sk, slow);
1262 1267
1263 if (noblock) 1268 if (noblock)
1264 return -EAGAIN; 1269 return -EAGAIN;
1265 1270
1266 /* starting over for a new packet */ 1271 /* starting over for a new packet */
1267 msg->msg_flags &= ~MSG_TRUNC; 1272 msg->msg_flags &= ~MSG_TRUNC;
1268 goto try_again; 1273 goto try_again;
1269 } 1274 }
1270 1275
1271 1276
1272 int udp_disconnect(struct sock *sk, int flags) 1277 int udp_disconnect(struct sock *sk, int flags)
1273 { 1278 {
1274 struct inet_sock *inet = inet_sk(sk); 1279 struct inet_sock *inet = inet_sk(sk);
1275 /* 1280 /*
1276 * 1003.1g - break association. 1281 * 1003.1g - break association.
1277 */ 1282 */
1278 1283
1279 sk->sk_state = TCP_CLOSE; 1284 sk->sk_state = TCP_CLOSE;
1280 inet->inet_daddr = 0; 1285 inet->inet_daddr = 0;
1281 inet->inet_dport = 0; 1286 inet->inet_dport = 0;
1282 sock_rps_reset_rxhash(sk); 1287 sock_rps_reset_rxhash(sk);
1283 sk->sk_bound_dev_if = 0; 1288 sk->sk_bound_dev_if = 0;
1284 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 1289 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
1285 inet_reset_saddr(sk); 1290 inet_reset_saddr(sk);
1286 1291
1287 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) { 1292 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
1288 sk->sk_prot->unhash(sk); 1293 sk->sk_prot->unhash(sk);
1289 inet->inet_sport = 0; 1294 inet->inet_sport = 0;
1290 } 1295 }
1291 sk_dst_reset(sk); 1296 sk_dst_reset(sk);
1292 return 0; 1297 return 0;
1293 } 1298 }
1294 EXPORT_SYMBOL(udp_disconnect); 1299 EXPORT_SYMBOL(udp_disconnect);
1295 1300
1296 void udp_lib_unhash(struct sock *sk) 1301 void udp_lib_unhash(struct sock *sk)
1297 { 1302 {
1298 if (sk_hashed(sk)) { 1303 if (sk_hashed(sk)) {
1299 struct udp_table *udptable = sk->sk_prot->h.udp_table; 1304 struct udp_table *udptable = sk->sk_prot->h.udp_table;
1300 struct udp_hslot *hslot, *hslot2; 1305 struct udp_hslot *hslot, *hslot2;
1301 1306
1302 hslot = udp_hashslot(udptable, sock_net(sk), 1307 hslot = udp_hashslot(udptable, sock_net(sk),
1303 udp_sk(sk)->udp_port_hash); 1308 udp_sk(sk)->udp_port_hash);
1304 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1309 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1305 1310
1306 spin_lock_bh(&hslot->lock); 1311 spin_lock_bh(&hslot->lock);
1307 if (sk_nulls_del_node_init_rcu(sk)) { 1312 if (sk_nulls_del_node_init_rcu(sk)) {
1308 hslot->count--; 1313 hslot->count--;
1309 inet_sk(sk)->inet_num = 0; 1314 inet_sk(sk)->inet_num = 0;
1310 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 1315 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1311 1316
1312 spin_lock(&hslot2->lock); 1317 spin_lock(&hslot2->lock);
1313 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1318 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1314 hslot2->count--; 1319 hslot2->count--;
1315 spin_unlock(&hslot2->lock); 1320 spin_unlock(&hslot2->lock);
1316 } 1321 }
1317 spin_unlock_bh(&hslot->lock); 1322 spin_unlock_bh(&hslot->lock);
1318 } 1323 }
1319 } 1324 }
1320 EXPORT_SYMBOL(udp_lib_unhash); 1325 EXPORT_SYMBOL(udp_lib_unhash);
1321 1326
1322 /* 1327 /*
1323 * inet_rcv_saddr was changed, we must rehash secondary hash 1328 * inet_rcv_saddr was changed, we must rehash secondary hash
1324 */ 1329 */
1325 void udp_lib_rehash(struct sock *sk, u16 newhash) 1330 void udp_lib_rehash(struct sock *sk, u16 newhash)
1326 { 1331 {
1327 if (sk_hashed(sk)) { 1332 if (sk_hashed(sk)) {
1328 struct udp_table *udptable = sk->sk_prot->h.udp_table; 1333 struct udp_table *udptable = sk->sk_prot->h.udp_table;
1329 struct udp_hslot *hslot, *hslot2, *nhslot2; 1334 struct udp_hslot *hslot, *hslot2, *nhslot2;
1330 1335
1331 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash); 1336 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1332 nhslot2 = udp_hashslot2(udptable, newhash); 1337 nhslot2 = udp_hashslot2(udptable, newhash);
1333 udp_sk(sk)->udp_portaddr_hash = newhash; 1338 udp_sk(sk)->udp_portaddr_hash = newhash;
1334 if (hslot2 != nhslot2) { 1339 if (hslot2 != nhslot2) {
1335 hslot = udp_hashslot(udptable, sock_net(sk), 1340 hslot = udp_hashslot(udptable, sock_net(sk),
1336 udp_sk(sk)->udp_port_hash); 1341 udp_sk(sk)->udp_port_hash);
1337 /* we must lock primary chain too */ 1342 /* we must lock primary chain too */
1338 spin_lock_bh(&hslot->lock); 1343 spin_lock_bh(&hslot->lock);
1339 1344
1340 spin_lock(&hslot2->lock); 1345 spin_lock(&hslot2->lock);
1341 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node); 1346 hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1342 hslot2->count--; 1347 hslot2->count--;
1343 spin_unlock(&hslot2->lock); 1348 spin_unlock(&hslot2->lock);
1344 1349
1345 spin_lock(&nhslot2->lock); 1350 spin_lock(&nhslot2->lock);
1346 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node, 1351 hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1347 &nhslot2->head); 1352 &nhslot2->head);
1348 nhslot2->count++; 1353 nhslot2->count++;
1349 spin_unlock(&nhslot2->lock); 1354 spin_unlock(&nhslot2->lock);
1350 1355
1351 spin_unlock_bh(&hslot->lock); 1356 spin_unlock_bh(&hslot->lock);
1352 } 1357 }
1353 } 1358 }
1354 } 1359 }
1355 EXPORT_SYMBOL(udp_lib_rehash); 1360 EXPORT_SYMBOL(udp_lib_rehash);
1356 1361
1357 static void udp_v4_rehash(struct sock *sk) 1362 static void udp_v4_rehash(struct sock *sk)
1358 { 1363 {
1359 u16 new_hash = udp4_portaddr_hash(sock_net(sk), 1364 u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1360 inet_sk(sk)->inet_rcv_saddr, 1365 inet_sk(sk)->inet_rcv_saddr,
1361 inet_sk(sk)->inet_num); 1366 inet_sk(sk)->inet_num);
1362 udp_lib_rehash(sk, new_hash); 1367 udp_lib_rehash(sk, new_hash);
1363 } 1368 }
1364 1369
1365 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1370 static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1366 { 1371 {
1367 int rc; 1372 int rc;
1368 1373
1369 if (inet_sk(sk)->inet_daddr) 1374 if (inet_sk(sk)->inet_daddr)
1370 sock_rps_save_rxhash(sk, skb); 1375 sock_rps_save_rxhash(sk, skb);
1371 1376
1372 rc = sock_queue_rcv_skb(sk, skb); 1377 rc = sock_queue_rcv_skb(sk, skb);
1373 if (rc < 0) { 1378 if (rc < 0) {
1374 int is_udplite = IS_UDPLITE(sk); 1379 int is_udplite = IS_UDPLITE(sk);
1375 1380
1376 /* Note that an ENOMEM error is charged twice */ 1381 /* Note that an ENOMEM error is charged twice */
1377 if (rc == -ENOMEM) 1382 if (rc == -ENOMEM)
1378 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 1383 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1379 is_udplite); 1384 is_udplite);
1380 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1385 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1381 kfree_skb(skb); 1386 kfree_skb(skb);
1382 trace_udp_fail_queue_rcv_skb(rc, sk); 1387 trace_udp_fail_queue_rcv_skb(rc, sk);
1383 return -1; 1388 return -1;
1384 } 1389 }
1385 1390
1386 return 0; 1391 return 0;
1387 1392
1388 } 1393 }
1389 1394
1390 static struct static_key udp_encap_needed __read_mostly; 1395 static struct static_key udp_encap_needed __read_mostly;
1391 void udp_encap_enable(void) 1396 void udp_encap_enable(void)
1392 { 1397 {
1393 if (!static_key_enabled(&udp_encap_needed)) 1398 if (!static_key_enabled(&udp_encap_needed))
1394 static_key_slow_inc(&udp_encap_needed); 1399 static_key_slow_inc(&udp_encap_needed);
1395 } 1400 }
1396 EXPORT_SYMBOL(udp_encap_enable); 1401 EXPORT_SYMBOL(udp_encap_enable);
1397 1402
1398 /* returns: 1403 /* returns:
1399 * -1: error 1404 * -1: error
1400 * 0: success 1405 * 0: success
1401 * >0: "udp encap" protocol resubmission 1406 * >0: "udp encap" protocol resubmission
1402 * 1407 *
1403 * Note that in the success and error cases, the skb is assumed to 1408 * Note that in the success and error cases, the skb is assumed to
1404 * have either been requeued or freed. 1409 * have either been requeued or freed.
1405 */ 1410 */
1406 int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 1411 int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1407 { 1412 {
1408 struct udp_sock *up = udp_sk(sk); 1413 struct udp_sock *up = udp_sk(sk);
1409 int rc; 1414 int rc;
1410 int is_udplite = IS_UDPLITE(sk); 1415 int is_udplite = IS_UDPLITE(sk);
1411 1416
1412 /* 1417 /*
1413 * Charge it to the socket, dropping if the queue is full. 1418 * Charge it to the socket, dropping if the queue is full.
1414 */ 1419 */
1415 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) 1420 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1416 goto drop; 1421 goto drop;
1417 nf_reset(skb); 1422 nf_reset(skb);
1418 1423
1419 if (static_key_false(&udp_encap_needed) && up->encap_type) { 1424 if (static_key_false(&udp_encap_needed) && up->encap_type) {
1420 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 1425 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
1421 1426
1422 /* 1427 /*
1423 * This is an encapsulation socket so pass the skb to 1428 * This is an encapsulation socket so pass the skb to
1424 * the socket's udp_encap_rcv() hook. Otherwise, just 1429 * the socket's udp_encap_rcv() hook. Otherwise, just
1425 * fall through and pass this up the UDP socket. 1430 * fall through and pass this up the UDP socket.
1426 * up->encap_rcv() returns the following value: 1431 * up->encap_rcv() returns the following value:
1427 * =0 if skb was successfully passed to the encap 1432 * =0 if skb was successfully passed to the encap
1428 * handler or was discarded by it. 1433 * handler or was discarded by it.
1429 * >0 if skb should be passed on to UDP. 1434 * >0 if skb should be passed on to UDP.
1430 * <0 if skb should be resubmitted as proto -N 1435 * <0 if skb should be resubmitted as proto -N
1431 */ 1436 */
1432 1437
1433 /* if we're overly short, let UDP handle it */ 1438 /* if we're overly short, let UDP handle it */
1434 encap_rcv = ACCESS_ONCE(up->encap_rcv); 1439 encap_rcv = ACCESS_ONCE(up->encap_rcv);
1435 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) { 1440 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
1436 int ret; 1441 int ret;
1437 1442
1438 ret = encap_rcv(sk, skb); 1443 ret = encap_rcv(sk, skb);
1439 if (ret <= 0) { 1444 if (ret <= 0) {
1440 UDP_INC_STATS_BH(sock_net(sk), 1445 UDP_INC_STATS_BH(sock_net(sk),
1441 UDP_MIB_INDATAGRAMS, 1446 UDP_MIB_INDATAGRAMS,
1442 is_udplite); 1447 is_udplite);
1443 return -ret; 1448 return -ret;
1444 } 1449 }
1445 } 1450 }
1446 1451
1447 /* FALLTHROUGH -- it's a UDP Packet */ 1452 /* FALLTHROUGH -- it's a UDP Packet */
1448 } 1453 }
1449 1454
1450 /* 1455 /*
1451 * UDP-Lite specific tests, ignored on UDP sockets 1456 * UDP-Lite specific tests, ignored on UDP sockets
1452 */ 1457 */
1453 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 1458 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
1454 1459
1455 /* 1460 /*
1456 * MIB statistics other than incrementing the error count are 1461 * MIB statistics other than incrementing the error count are
1457 * disabled for the following two types of errors: these depend 1462 * disabled for the following two types of errors: these depend
1458 * on the application settings, not on the functioning of the 1463 * on the application settings, not on the functioning of the
1459 * protocol stack as such. 1464 * protocol stack as such.
1460 * 1465 *
1461 * RFC 3828 here recommends (sec 3.3): "There should also be a 1466 * RFC 3828 here recommends (sec 3.3): "There should also be a
1462 * way ... to ... at least let the receiving application block 1467 * way ... to ... at least let the receiving application block
1463 * delivery of packets with coverage values less than a value 1468 * delivery of packets with coverage values less than a value
1464 * provided by the application." 1469 * provided by the application."
1465 */ 1470 */
1466 if (up->pcrlen == 0) { /* full coverage was set */ 1471 if (up->pcrlen == 0) { /* full coverage was set */
1467 LIMIT_NETDEBUG(KERN_WARNING "UDPLite: partial coverage %d while full coverage %d requested\n", 1472 LIMIT_NETDEBUG(KERN_WARNING "UDPLite: partial coverage %d while full coverage %d requested\n",
1468 UDP_SKB_CB(skb)->cscov, skb->len); 1473 UDP_SKB_CB(skb)->cscov, skb->len);
1469 goto drop; 1474 goto drop;
1470 } 1475 }
1471 /* The next case involves violating the min. coverage requested 1476 /* The next case involves violating the min. coverage requested
1472 * by the receiver. This is subtle: if receiver wants x and x is 1477 * by the receiver. This is subtle: if receiver wants x and x is
1473 * greater than the buffersize/MTU then receiver will complain 1478 * greater than the buffersize/MTU then receiver will complain
1474 * that it wants x while sender emits packets of smaller size y. 1479 * that it wants x while sender emits packets of smaller size y.
1475 * Therefore the above ...()->partial_cov statement is essential. 1480 * Therefore the above ...()->partial_cov statement is essential.
1476 */ 1481 */
1477 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 1482 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
1478 LIMIT_NETDEBUG(KERN_WARNING "UDPLite: coverage %d too small, need min %d\n", 1483 LIMIT_NETDEBUG(KERN_WARNING "UDPLite: coverage %d too small, need min %d\n",
1479 UDP_SKB_CB(skb)->cscov, up->pcrlen); 1484 UDP_SKB_CB(skb)->cscov, up->pcrlen);
1480 goto drop; 1485 goto drop;
1481 } 1486 }
1482 } 1487 }
1483 1488
1484 if (rcu_access_pointer(sk->sk_filter) && 1489 if (rcu_access_pointer(sk->sk_filter) &&
1485 udp_lib_checksum_complete(skb)) 1490 udp_lib_checksum_complete(skb))
1486 goto drop; 1491 goto drop;
1487 1492
1488 1493
1489 if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf)) 1494 if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
1490 goto drop; 1495 goto drop;
1491 1496
1492 rc = 0; 1497 rc = 0;
1493 1498
1494 ipv4_pktinfo_prepare(skb); 1499 ipv4_pktinfo_prepare(skb);
1495 bh_lock_sock(sk); 1500 bh_lock_sock(sk);
1496 if (!sock_owned_by_user(sk)) 1501 if (!sock_owned_by_user(sk))
1497 rc = __udp_queue_rcv_skb(sk, skb); 1502 rc = __udp_queue_rcv_skb(sk, skb);
1498 else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) { 1503 else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
1499 bh_unlock_sock(sk); 1504 bh_unlock_sock(sk);
1500 goto drop; 1505 goto drop;
1501 } 1506 }
1502 bh_unlock_sock(sk); 1507 bh_unlock_sock(sk);
1503 1508
1504 return rc; 1509 return rc;
1505 1510
1506 drop: 1511 drop:
1507 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 1512 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1508 atomic_inc(&sk->sk_drops); 1513 atomic_inc(&sk->sk_drops);
1509 kfree_skb(skb); 1514 kfree_skb(skb);
1510 return -1; 1515 return -1;
1511 } 1516 }
1512 1517
1513 1518
1514 static void flush_stack(struct sock **stack, unsigned int count, 1519 static void flush_stack(struct sock **stack, unsigned int count,
1515 struct sk_buff *skb, unsigned int final) 1520 struct sk_buff *skb, unsigned int final)
1516 { 1521 {
1517 unsigned int i; 1522 unsigned int i;
1518 struct sk_buff *skb1 = NULL; 1523 struct sk_buff *skb1 = NULL;
1519 struct sock *sk; 1524 struct sock *sk;
1520 1525
1521 for (i = 0; i < count; i++) { 1526 for (i = 0; i < count; i++) {
1522 sk = stack[i]; 1527 sk = stack[i];
1523 if (likely(skb1 == NULL)) 1528 if (likely(skb1 == NULL))
1524 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC); 1529 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
1525 1530
1526 if (!skb1) { 1531 if (!skb1) {
1527 atomic_inc(&sk->sk_drops); 1532 atomic_inc(&sk->sk_drops);
1528 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 1533 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1529 IS_UDPLITE(sk)); 1534 IS_UDPLITE(sk));
1530 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, 1535 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1531 IS_UDPLITE(sk)); 1536 IS_UDPLITE(sk));
1532 } 1537 }
1533 1538
1534 if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0) 1539 if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
1535 skb1 = NULL; 1540 skb1 = NULL;
1536 } 1541 }
1537 if (unlikely(skb1)) 1542 if (unlikely(skb1))
1538 kfree_skb(skb1); 1543 kfree_skb(skb1);
1539 } 1544 }
1540 1545
1541 /* 1546 /*
1542 * Multicasts and broadcasts go to each listener. 1547 * Multicasts and broadcasts go to each listener.
1543 * 1548 *
1544 * Note: called only from the BH handler context. 1549 * Note: called only from the BH handler context.
1545 */ 1550 */
1546 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 1551 static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1547 struct udphdr *uh, 1552 struct udphdr *uh,
1548 __be32 saddr, __be32 daddr, 1553 __be32 saddr, __be32 daddr,
1549 struct udp_table *udptable) 1554 struct udp_table *udptable)
1550 { 1555 {
1551 struct sock *sk, *stack[256 / sizeof(struct sock *)]; 1556 struct sock *sk, *stack[256 / sizeof(struct sock *)];
1552 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest)); 1557 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
1553 int dif; 1558 int dif;
1554 unsigned int i, count = 0; 1559 unsigned int i, count = 0;
1555 1560
1556 spin_lock(&hslot->lock); 1561 spin_lock(&hslot->lock);
1557 sk = sk_nulls_head(&hslot->head); 1562 sk = sk_nulls_head(&hslot->head);
1558 dif = skb->dev->ifindex; 1563 dif = skb->dev->ifindex;
1559 sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 1564 sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
1560 while (sk) { 1565 while (sk) {
1561 stack[count++] = sk; 1566 stack[count++] = sk;
1562 sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest, 1567 sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
1563 daddr, uh->source, saddr, dif); 1568 daddr, uh->source, saddr, dif);
1564 if (unlikely(count == ARRAY_SIZE(stack))) { 1569 if (unlikely(count == ARRAY_SIZE(stack))) {
1565 if (!sk) 1570 if (!sk)
1566 break; 1571 break;
1567 flush_stack(stack, count, skb, ~0); 1572 flush_stack(stack, count, skb, ~0);
1568 count = 0; 1573 count = 0;
1569 } 1574 }
1570 } 1575 }
1571 /* 1576 /*
1572 * before releasing chain lock, we must take a reference on sockets 1577 * before releasing chain lock, we must take a reference on sockets
1573 */ 1578 */
1574 for (i = 0; i < count; i++) 1579 for (i = 0; i < count; i++)
1575 sock_hold(stack[i]); 1580 sock_hold(stack[i]);
1576 1581
1577 spin_unlock(&hslot->lock); 1582 spin_unlock(&hslot->lock);
1578 1583
1579 /* 1584 /*
1580 * do the slow work with no lock held 1585 * do the slow work with no lock held
1581 */ 1586 */
1582 if (count) { 1587 if (count) {
1583 flush_stack(stack, count, skb, count - 1); 1588 flush_stack(stack, count, skb, count - 1);
1584 1589
1585 for (i = 0; i < count; i++) 1590 for (i = 0; i < count; i++)
1586 sock_put(stack[i]); 1591 sock_put(stack[i]);
1587 } else { 1592 } else {
1588 kfree_skb(skb); 1593 kfree_skb(skb);
1589 } 1594 }
1590 return 0; 1595 return 0;
1591 } 1596 }
1592 1597
1593 /* Initialize UDP checksum. If exited with zero value (success), 1598 /* Initialize UDP checksum. If exited with zero value (success),
1594 * CHECKSUM_UNNECESSARY means, that no more checks are required. 1599 * CHECKSUM_UNNECESSARY means, that no more checks are required.
1595 * Otherwise, csum completion requires chacksumming packet body, 1600 * Otherwise, csum completion requires chacksumming packet body,
1596 * including udp header and folding it to skb->csum. 1601 * including udp header and folding it to skb->csum.
1597 */ 1602 */
1598 static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, 1603 static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1599 int proto) 1604 int proto)
1600 { 1605 {
1601 const struct iphdr *iph; 1606 const struct iphdr *iph;
1602 int err; 1607 int err;
1603 1608
1604 UDP_SKB_CB(skb)->partial_cov = 0; 1609 UDP_SKB_CB(skb)->partial_cov = 0;
1605 UDP_SKB_CB(skb)->cscov = skb->len; 1610 UDP_SKB_CB(skb)->cscov = skb->len;
1606 1611
1607 if (proto == IPPROTO_UDPLITE) { 1612 if (proto == IPPROTO_UDPLITE) {
1608 err = udplite_checksum_init(skb, uh); 1613 err = udplite_checksum_init(skb, uh);
1609 if (err) 1614 if (err)
1610 return err; 1615 return err;
1611 } 1616 }
1612 1617
1613 iph = ip_hdr(skb); 1618 iph = ip_hdr(skb);
1614 if (uh->check == 0) { 1619 if (uh->check == 0) {
1615 skb->ip_summed = CHECKSUM_UNNECESSARY; 1620 skb->ip_summed = CHECKSUM_UNNECESSARY;
1616 } else if (skb->ip_summed == CHECKSUM_COMPLETE) { 1621 } else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1617 if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 1622 if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1618 proto, skb->csum)) 1623 proto, skb->csum))
1619 skb->ip_summed = CHECKSUM_UNNECESSARY; 1624 skb->ip_summed = CHECKSUM_UNNECESSARY;
1620 } 1625 }
1621 if (!skb_csum_unnecessary(skb)) 1626 if (!skb_csum_unnecessary(skb))
1622 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, 1627 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1623 skb->len, proto, 0); 1628 skb->len, proto, 0);
1624 /* Probably, we should checksum udp header (it should be in cache 1629 /* Probably, we should checksum udp header (it should be in cache
1625 * in any case) and data in tiny packets (< rx copybreak). 1630 * in any case) and data in tiny packets (< rx copybreak).
1626 */ 1631 */
1627 1632
1628 return 0; 1633 return 0;
1629 } 1634 }
1630 1635
1631 /* 1636 /*
1632 * All we need to do is get the socket, and then do a checksum. 1637 * All we need to do is get the socket, and then do a checksum.
1633 */ 1638 */
1634 1639
1635 int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 1640 int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1636 int proto) 1641 int proto)
1637 { 1642 {
1638 struct sock *sk; 1643 struct sock *sk;
1639 struct udphdr *uh; 1644 struct udphdr *uh;
1640 unsigned short ulen; 1645 unsigned short ulen;
1641 struct rtable *rt = skb_rtable(skb); 1646 struct rtable *rt = skb_rtable(skb);
1642 __be32 saddr, daddr; 1647 __be32 saddr, daddr;
1643 struct net *net = dev_net(skb->dev); 1648 struct net *net = dev_net(skb->dev);
1644 1649
1645 /* 1650 /*
1646 * Validate the packet. 1651 * Validate the packet.
1647 */ 1652 */
1648 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 1653 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1649 goto drop; /* No space for header. */ 1654 goto drop; /* No space for header. */
1650 1655
1651 uh = udp_hdr(skb); 1656 uh = udp_hdr(skb);
1652 ulen = ntohs(uh->len); 1657 ulen = ntohs(uh->len);
1653 saddr = ip_hdr(skb)->saddr; 1658 saddr = ip_hdr(skb)->saddr;
1654 daddr = ip_hdr(skb)->daddr; 1659 daddr = ip_hdr(skb)->daddr;
1655 1660
1656 if (ulen > skb->len) 1661 if (ulen > skb->len)
1657 goto short_packet; 1662 goto short_packet;
1658 1663
1659 if (proto == IPPROTO_UDP) { 1664 if (proto == IPPROTO_UDP) {
1660 /* UDP validates ulen. */ 1665 /* UDP validates ulen. */
1661 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) 1666 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1662 goto short_packet; 1667 goto short_packet;
1663 uh = udp_hdr(skb); 1668 uh = udp_hdr(skb);
1664 } 1669 }
1665 1670
1666 if (udp4_csum_init(skb, uh, proto)) 1671 if (udp4_csum_init(skb, uh, proto))
1667 goto csum_error; 1672 goto csum_error;
1668 1673
1669 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1674 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1670 return __udp4_lib_mcast_deliver(net, skb, uh, 1675 return __udp4_lib_mcast_deliver(net, skb, uh,
1671 saddr, daddr, udptable); 1676 saddr, daddr, udptable);
1672 1677
1673 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1678 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1674 1679
1675 if (sk != NULL) { 1680 if (sk != NULL) {
1676 int ret = udp_queue_rcv_skb(sk, skb); 1681 int ret = udp_queue_rcv_skb(sk, skb);
1677 sock_put(sk); 1682 sock_put(sk);
1678 1683
1679 /* a return value > 0 means to resubmit the input, but 1684 /* a return value > 0 means to resubmit the input, but
1680 * it wants the return to be -protocol, or 0 1685 * it wants the return to be -protocol, or 0
1681 */ 1686 */
1682 if (ret > 0) 1687 if (ret > 0)
1683 return -ret; 1688 return -ret;
1684 return 0; 1689 return 0;
1685 } 1690 }
1686 1691
1687 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 1692 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1688 goto drop; 1693 goto drop;
1689 nf_reset(skb); 1694 nf_reset(skb);
1690 1695
1691 /* No socket. Drop packet silently, if checksum is wrong */ 1696 /* No socket. Drop packet silently, if checksum is wrong */
1692 if (udp_lib_checksum_complete(skb)) 1697 if (udp_lib_checksum_complete(skb))
1693 goto csum_error; 1698 goto csum_error;
1694 1699
1695 UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 1700 UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1696 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); 1701 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1697 1702
1698 /* 1703 /*
1699 * Hmm. We got an UDP packet to a port to which we 1704 * Hmm. We got an UDP packet to a port to which we
1700 * don't wanna listen. Ignore it. 1705 * don't wanna listen. Ignore it.
1701 */ 1706 */
1702 kfree_skb(skb); 1707 kfree_skb(skb);
1703 return 0; 1708 return 0;
1704 1709
1705 short_packet: 1710 short_packet:
1706 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n", 1711 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1707 proto == IPPROTO_UDPLITE ? "Lite" : "", 1712 proto == IPPROTO_UDPLITE ? "Lite" : "",
1708 &saddr, ntohs(uh->source), 1713 &saddr, ntohs(uh->source),
1709 ulen, skb->len, 1714 ulen, skb->len,
1710 &daddr, ntohs(uh->dest)); 1715 &daddr, ntohs(uh->dest));
1711 goto drop; 1716 goto drop;
1712 1717
1713 csum_error: 1718 csum_error:
1714 /* 1719 /*
1715 * RFC1122: OK. Discards the bad packet silently (as far as 1720 * RFC1122: OK. Discards the bad packet silently (as far as
1716 * the network is concerned, anyway) as per 4.1.3.4 (MUST). 1721 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1717 */ 1722 */
1718 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n", 1723 LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1719 proto == IPPROTO_UDPLITE ? "Lite" : "", 1724 proto == IPPROTO_UDPLITE ? "Lite" : "",
1720 &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest), 1725 &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
1721 ulen); 1726 ulen);
1722 drop: 1727 drop:
1723 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 1728 UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1724 kfree_skb(skb); 1729 kfree_skb(skb);
1725 return 0; 1730 return 0;
1726 } 1731 }
1727 1732
1728 int udp_rcv(struct sk_buff *skb) 1733 int udp_rcv(struct sk_buff *skb)
1729 { 1734 {
1730 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1735 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1731 } 1736 }
1732 1737
1733 void udp_destroy_sock(struct sock *sk) 1738 void udp_destroy_sock(struct sock *sk)
1734 { 1739 {
1735 bool slow = lock_sock_fast(sk); 1740 bool slow = lock_sock_fast(sk);
1736 udp_flush_pending_frames(sk); 1741 udp_flush_pending_frames(sk);
1737 unlock_sock_fast(sk, slow); 1742 unlock_sock_fast(sk, slow);
1738 } 1743 }
1739 1744
1740 /* 1745 /*
1741 * Socket option code for UDP 1746 * Socket option code for UDP
1742 */ 1747 */
1743 int udp_lib_setsockopt(struct sock *sk, int level, int optname, 1748 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1744 char __user *optval, unsigned int optlen, 1749 char __user *optval, unsigned int optlen,
1745 int (*push_pending_frames)(struct sock *)) 1750 int (*push_pending_frames)(struct sock *))
1746 { 1751 {
1747 struct udp_sock *up = udp_sk(sk); 1752 struct udp_sock *up = udp_sk(sk);
1748 int val; 1753 int val;
1749 int err = 0; 1754 int err = 0;
1750 int is_udplite = IS_UDPLITE(sk); 1755 int is_udplite = IS_UDPLITE(sk);
1751 1756
1752 if (optlen < sizeof(int)) 1757 if (optlen < sizeof(int))
1753 return -EINVAL; 1758 return -EINVAL;
1754 1759
1755 if (get_user(val, (int __user *)optval)) 1760 if (get_user(val, (int __user *)optval))
1756 return -EFAULT; 1761 return -EFAULT;
1757 1762
1758 switch (optname) { 1763 switch (optname) {
1759 case UDP_CORK: 1764 case UDP_CORK:
1760 if (val != 0) { 1765 if (val != 0) {
1761 up->corkflag = 1; 1766 up->corkflag = 1;
1762 } else { 1767 } else {
1763 up->corkflag = 0; 1768 up->corkflag = 0;
1764 lock_sock(sk); 1769 lock_sock(sk);
1765 (*push_pending_frames)(sk); 1770 (*push_pending_frames)(sk);
1766 release_sock(sk); 1771 release_sock(sk);
1767 } 1772 }
1768 break; 1773 break;
1769 1774
1770 case UDP_ENCAP: 1775 case UDP_ENCAP:
1771 switch (val) { 1776 switch (val) {
1772 case 0: 1777 case 0:
1773 case UDP_ENCAP_ESPINUDP: 1778 case UDP_ENCAP_ESPINUDP:
1774 case UDP_ENCAP_ESPINUDP_NON_IKE: 1779 case UDP_ENCAP_ESPINUDP_NON_IKE:
1775 up->encap_rcv = xfrm4_udp_encap_rcv; 1780 up->encap_rcv = xfrm4_udp_encap_rcv;
1776 /* FALLTHROUGH */ 1781 /* FALLTHROUGH */
1777 case UDP_ENCAP_L2TPINUDP: 1782 case UDP_ENCAP_L2TPINUDP:
1778 up->encap_type = val; 1783 up->encap_type = val;
1779 udp_encap_enable(); 1784 udp_encap_enable();
1780 break; 1785 break;
1781 default: 1786 default:
1782 err = -ENOPROTOOPT; 1787 err = -ENOPROTOOPT;
1783 break; 1788 break;
1784 } 1789 }
1785 break; 1790 break;
1786 1791
1787 /* 1792 /*
1788 * UDP-Lite's partial checksum coverage (RFC 3828). 1793 * UDP-Lite's partial checksum coverage (RFC 3828).
1789 */ 1794 */
1790 /* The sender sets actual checksum coverage length via this option. 1795 /* The sender sets actual checksum coverage length via this option.
1791 * The case coverage > packet length is handled by send module. */ 1796 * The case coverage > packet length is handled by send module. */
1792 case UDPLITE_SEND_CSCOV: 1797 case UDPLITE_SEND_CSCOV:
1793 if (!is_udplite) /* Disable the option on UDP sockets */ 1798 if (!is_udplite) /* Disable the option on UDP sockets */
1794 return -ENOPROTOOPT; 1799 return -ENOPROTOOPT;
1795 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */ 1800 if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1796 val = 8; 1801 val = 8;
1797 else if (val > USHRT_MAX) 1802 else if (val > USHRT_MAX)
1798 val = USHRT_MAX; 1803 val = USHRT_MAX;
1799 up->pcslen = val; 1804 up->pcslen = val;
1800 up->pcflag |= UDPLITE_SEND_CC; 1805 up->pcflag |= UDPLITE_SEND_CC;
1801 break; 1806 break;
1802 1807
1803 /* The receiver specifies a minimum checksum coverage value. To make 1808 /* The receiver specifies a minimum checksum coverage value. To make
1804 * sense, this should be set to at least 8 (as done below). If zero is 1809 * sense, this should be set to at least 8 (as done below). If zero is
1805 * used, this again means full checksum coverage. */ 1810 * used, this again means full checksum coverage. */
1806 case UDPLITE_RECV_CSCOV: 1811 case UDPLITE_RECV_CSCOV:
1807 if (!is_udplite) /* Disable the option on UDP sockets */ 1812 if (!is_udplite) /* Disable the option on UDP sockets */
1808 return -ENOPROTOOPT; 1813 return -ENOPROTOOPT;
1809 if (val != 0 && val < 8) /* Avoid silly minimal values. */ 1814 if (val != 0 && val < 8) /* Avoid silly minimal values. */
1810 val = 8; 1815 val = 8;
1811 else if (val > USHRT_MAX) 1816 else if (val > USHRT_MAX)
1812 val = USHRT_MAX; 1817 val = USHRT_MAX;
1813 up->pcrlen = val; 1818 up->pcrlen = val;
1814 up->pcflag |= UDPLITE_RECV_CC; 1819 up->pcflag |= UDPLITE_RECV_CC;
1815 break; 1820 break;
1816 1821
1817 default: 1822 default:
1818 err = -ENOPROTOOPT; 1823 err = -ENOPROTOOPT;
1819 break; 1824 break;
1820 } 1825 }
1821 1826
1822 return err; 1827 return err;
1823 } 1828 }
1824 EXPORT_SYMBOL(udp_lib_setsockopt); 1829 EXPORT_SYMBOL(udp_lib_setsockopt);
1825 1830
1826 int udp_setsockopt(struct sock *sk, int level, int optname, 1831 int udp_setsockopt(struct sock *sk, int level, int optname,
1827 char __user *optval, unsigned int optlen) 1832 char __user *optval, unsigned int optlen)
1828 { 1833 {
1829 if (level == SOL_UDP || level == SOL_UDPLITE) 1834 if (level == SOL_UDP || level == SOL_UDPLITE)
1830 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1835 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1831 udp_push_pending_frames); 1836 udp_push_pending_frames);
1832 return ip_setsockopt(sk, level, optname, optval, optlen); 1837 return ip_setsockopt(sk, level, optname, optval, optlen);
1833 } 1838 }
1834 1839
1835 #ifdef CONFIG_COMPAT 1840 #ifdef CONFIG_COMPAT
1836 int compat_udp_setsockopt(struct sock *sk, int level, int optname, 1841 int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1837 char __user *optval, unsigned int optlen) 1842 char __user *optval, unsigned int optlen)
1838 { 1843 {
1839 if (level == SOL_UDP || level == SOL_UDPLITE) 1844 if (level == SOL_UDP || level == SOL_UDPLITE)
1840 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1845 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1841 udp_push_pending_frames); 1846 udp_push_pending_frames);
1842 return compat_ip_setsockopt(sk, level, optname, optval, optlen); 1847 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1843 } 1848 }
1844 #endif 1849 #endif
1845 1850
1846 int udp_lib_getsockopt(struct sock *sk, int level, int optname, 1851 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
1847 char __user *optval, int __user *optlen) 1852 char __user *optval, int __user *optlen)
1848 { 1853 {
1849 struct udp_sock *up = udp_sk(sk); 1854 struct udp_sock *up = udp_sk(sk);
1850 int val, len; 1855 int val, len;
1851 1856
1852 if (get_user(len, optlen)) 1857 if (get_user(len, optlen))
1853 return -EFAULT; 1858 return -EFAULT;
1854 1859
1855 len = min_t(unsigned int, len, sizeof(int)); 1860 len = min_t(unsigned int, len, sizeof(int));
1856 1861
1857 if (len < 0) 1862 if (len < 0)
1858 return -EINVAL; 1863 return -EINVAL;
1859 1864
1860 switch (optname) { 1865 switch (optname) {
1861 case UDP_CORK: 1866 case UDP_CORK:
1862 val = up->corkflag; 1867 val = up->corkflag;
1863 break; 1868 break;
1864 1869
1865 case UDP_ENCAP: 1870 case UDP_ENCAP:
1866 val = up->encap_type; 1871 val = up->encap_type;
1867 break; 1872 break;
1868 1873
1869 /* The following two cannot be changed on UDP sockets, the return is 1874 /* The following two cannot be changed on UDP sockets, the return is
1870 * always 0 (which corresponds to the full checksum coverage of UDP). */ 1875 * always 0 (which corresponds to the full checksum coverage of UDP). */
1871 case UDPLITE_SEND_CSCOV: 1876 case UDPLITE_SEND_CSCOV:
1872 val = up->pcslen; 1877 val = up->pcslen;
1873 break; 1878 break;
1874 1879
1875 case UDPLITE_RECV_CSCOV: 1880 case UDPLITE_RECV_CSCOV:
1876 val = up->pcrlen; 1881 val = up->pcrlen;
1877 break; 1882 break;
1878 1883
1879 default: 1884 default:
1880 return -ENOPROTOOPT; 1885 return -ENOPROTOOPT;
1881 } 1886 }
1882 1887
1883 if (put_user(len, optlen)) 1888 if (put_user(len, optlen))
1884 return -EFAULT; 1889 return -EFAULT;
1885 if (copy_to_user(optval, &val, len)) 1890 if (copy_to_user(optval, &val, len))
1886 return -EFAULT; 1891 return -EFAULT;
1887 return 0; 1892 return 0;
1888 } 1893 }
1889 EXPORT_SYMBOL(udp_lib_getsockopt); 1894 EXPORT_SYMBOL(udp_lib_getsockopt);
1890 1895
1891 int udp_getsockopt(struct sock *sk, int level, int optname, 1896 int udp_getsockopt(struct sock *sk, int level, int optname,
1892 char __user *optval, int __user *optlen) 1897 char __user *optval, int __user *optlen)
1893 { 1898 {
1894 if (level == SOL_UDP || level == SOL_UDPLITE) 1899 if (level == SOL_UDP || level == SOL_UDPLITE)
1895 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1900 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1896 return ip_getsockopt(sk, level, optname, optval, optlen); 1901 return ip_getsockopt(sk, level, optname, optval, optlen);
1897 } 1902 }
1898 1903
1899 #ifdef CONFIG_COMPAT 1904 #ifdef CONFIG_COMPAT
1900 int compat_udp_getsockopt(struct sock *sk, int level, int optname, 1905 int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1901 char __user *optval, int __user *optlen) 1906 char __user *optval, int __user *optlen)
1902 { 1907 {
1903 if (level == SOL_UDP || level == SOL_UDPLITE) 1908 if (level == SOL_UDP || level == SOL_UDPLITE)
1904 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1909 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1905 return compat_ip_getsockopt(sk, level, optname, optval, optlen); 1910 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1906 } 1911 }
1907 #endif 1912 #endif
1908 /** 1913 /**
1909 * udp_poll - wait for a UDP event. 1914 * udp_poll - wait for a UDP event.
1910 * @file - file struct 1915 * @file - file struct
1911 * @sock - socket 1916 * @sock - socket
1912 * @wait - poll table 1917 * @wait - poll table
1913 * 1918 *
1914 * This is same as datagram poll, except for the special case of 1919 * This is same as datagram poll, except for the special case of
1915 * blocking sockets. If application is using a blocking fd 1920 * blocking sockets. If application is using a blocking fd
1916 * and a packet with checksum error is in the queue; 1921 * and a packet with checksum error is in the queue;
1917 * then it could get return from select indicating data available 1922 * then it could get return from select indicating data available
1918 * but then block when reading it. Add special case code 1923 * but then block when reading it. Add special case code
1919 * to work around these arguably broken applications. 1924 * to work around these arguably broken applications.
1920 */ 1925 */
1921 unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait) 1926 unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
1922 { 1927 {
1923 unsigned int mask = datagram_poll(file, sock, wait); 1928 unsigned int mask = datagram_poll(file, sock, wait);
1924 struct sock *sk = sock->sk; 1929 struct sock *sk = sock->sk;
1925 1930
1926 /* Check for false positives due to checksum errors */ 1931 /* Check for false positives due to checksum errors */
1927 if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) && 1932 if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
1928 !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk)) 1933 !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
1929 mask &= ~(POLLIN | POLLRDNORM); 1934 mask &= ~(POLLIN | POLLRDNORM);
1930 1935
1931 return mask; 1936 return mask;
1932 1937
1933 } 1938 }
1934 EXPORT_SYMBOL(udp_poll); 1939 EXPORT_SYMBOL(udp_poll);
1935 1940
1936 struct proto udp_prot = { 1941 struct proto udp_prot = {
1937 .name = "UDP", 1942 .name = "UDP",
1938 .owner = THIS_MODULE, 1943 .owner = THIS_MODULE,
1939 .close = udp_lib_close, 1944 .close = udp_lib_close,
1940 .connect = ip4_datagram_connect, 1945 .connect = ip4_datagram_connect,
1941 .disconnect = udp_disconnect, 1946 .disconnect = udp_disconnect,
1942 .ioctl = udp_ioctl, 1947 .ioctl = udp_ioctl,
1943 .destroy = udp_destroy_sock, 1948 .destroy = udp_destroy_sock,
1944 .setsockopt = udp_setsockopt, 1949 .setsockopt = udp_setsockopt,
1945 .getsockopt = udp_getsockopt, 1950 .getsockopt = udp_getsockopt,
1946 .sendmsg = udp_sendmsg, 1951 .sendmsg = udp_sendmsg,
1947 .recvmsg = udp_recvmsg, 1952 .recvmsg = udp_recvmsg,
1948 .sendpage = udp_sendpage, 1953 .sendpage = udp_sendpage,
1949 .backlog_rcv = __udp_queue_rcv_skb, 1954 .backlog_rcv = __udp_queue_rcv_skb,
1950 .hash = udp_lib_hash, 1955 .hash = udp_lib_hash,
1951 .unhash = udp_lib_unhash, 1956 .unhash = udp_lib_unhash,
1952 .rehash = udp_v4_rehash, 1957 .rehash = udp_v4_rehash,
1953 .get_port = udp_v4_get_port, 1958 .get_port = udp_v4_get_port,
1954 .memory_allocated = &udp_memory_allocated, 1959 .memory_allocated = &udp_memory_allocated,
1955 .sysctl_mem = sysctl_udp_mem, 1960 .sysctl_mem = sysctl_udp_mem,
1956 .sysctl_wmem = &sysctl_udp_wmem_min, 1961 .sysctl_wmem = &sysctl_udp_wmem_min,
1957 .sysctl_rmem = &sysctl_udp_rmem_min, 1962 .sysctl_rmem = &sysctl_udp_rmem_min,
1958 .obj_size = sizeof(struct udp_sock), 1963 .obj_size = sizeof(struct udp_sock),
1959 .slab_flags = SLAB_DESTROY_BY_RCU, 1964 .slab_flags = SLAB_DESTROY_BY_RCU,
1960 .h.udp_table = &udp_table, 1965 .h.udp_table = &udp_table,
1961 #ifdef CONFIG_COMPAT 1966 #ifdef CONFIG_COMPAT
1962 .compat_setsockopt = compat_udp_setsockopt, 1967 .compat_setsockopt = compat_udp_setsockopt,
1963 .compat_getsockopt = compat_udp_getsockopt, 1968 .compat_getsockopt = compat_udp_getsockopt,
1964 #endif 1969 #endif
1965 .clear_sk = sk_prot_clear_portaddr_nulls, 1970 .clear_sk = sk_prot_clear_portaddr_nulls,
1966 }; 1971 };
1967 EXPORT_SYMBOL(udp_prot); 1972 EXPORT_SYMBOL(udp_prot);
1968 1973
1969 /* ------------------------------------------------------------------------ */ 1974 /* ------------------------------------------------------------------------ */
1970 #ifdef CONFIG_PROC_FS 1975 #ifdef CONFIG_PROC_FS
1971 1976
1972 static struct sock *udp_get_first(struct seq_file *seq, int start) 1977 static struct sock *udp_get_first(struct seq_file *seq, int start)
1973 { 1978 {
1974 struct sock *sk; 1979 struct sock *sk;
1975 struct udp_iter_state *state = seq->private; 1980 struct udp_iter_state *state = seq->private;
1976 struct net *net = seq_file_net(seq); 1981 struct net *net = seq_file_net(seq);
1977 1982
1978 for (state->bucket = start; state->bucket <= state->udp_table->mask; 1983 for (state->bucket = start; state->bucket <= state->udp_table->mask;
1979 ++state->bucket) { 1984 ++state->bucket) {
1980 struct hlist_nulls_node *node; 1985 struct hlist_nulls_node *node;
1981 struct udp_hslot *hslot = &state->udp_table->hash[state->bucket]; 1986 struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
1982 1987
1983 if (hlist_nulls_empty(&hslot->head)) 1988 if (hlist_nulls_empty(&hslot->head))
1984 continue; 1989 continue;
1985 1990
1986 spin_lock_bh(&hslot->lock); 1991 spin_lock_bh(&hslot->lock);
1987 sk_nulls_for_each(sk, node, &hslot->head) { 1992 sk_nulls_for_each(sk, node, &hslot->head) {
1988 if (!net_eq(sock_net(sk), net)) 1993 if (!net_eq(sock_net(sk), net))
1989 continue; 1994 continue;
1990 if (sk->sk_family == state->family) 1995 if (sk->sk_family == state->family)
1991 goto found; 1996 goto found;
1992 } 1997 }
1993 spin_unlock_bh(&hslot->lock); 1998 spin_unlock_bh(&hslot->lock);
1994 } 1999 }
1995 sk = NULL; 2000 sk = NULL;
1996 found: 2001 found:
1997 return sk; 2002 return sk;
1998 } 2003 }
1999 2004
2000 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) 2005 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
2001 { 2006 {
2002 struct udp_iter_state *state = seq->private; 2007 struct udp_iter_state *state = seq->private;
2003 struct net *net = seq_file_net(seq); 2008 struct net *net = seq_file_net(seq);
2004 2009
2005 do { 2010 do {
2006 sk = sk_nulls_next(sk); 2011 sk = sk_nulls_next(sk);
2007 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family)); 2012 } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
2008 2013
2009 if (!sk) { 2014 if (!sk) {
2010 if (state->bucket <= state->udp_table->mask) 2015 if (state->bucket <= state->udp_table->mask)
2011 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 2016 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2012 return udp_get_first(seq, state->bucket + 1); 2017 return udp_get_first(seq, state->bucket + 1);
2013 } 2018 }
2014 return sk; 2019 return sk;
2015 } 2020 }
2016 2021
2017 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos) 2022 static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
2018 { 2023 {
2019 struct sock *sk = udp_get_first(seq, 0); 2024 struct sock *sk = udp_get_first(seq, 0);
2020 2025
2021 if (sk) 2026 if (sk)
2022 while (pos && (sk = udp_get_next(seq, sk)) != NULL) 2027 while (pos && (sk = udp_get_next(seq, sk)) != NULL)
2023 --pos; 2028 --pos;
2024 return pos ? NULL : sk; 2029 return pos ? NULL : sk;
2025 } 2030 }
2026 2031
2027 static void *udp_seq_start(struct seq_file *seq, loff_t *pos) 2032 static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
2028 { 2033 {
2029 struct udp_iter_state *state = seq->private; 2034 struct udp_iter_state *state = seq->private;
2030 state->bucket = MAX_UDP_PORTS; 2035 state->bucket = MAX_UDP_PORTS;
2031 2036
2032 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN; 2037 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
2033 } 2038 }
2034 2039
2035 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2040 static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2036 { 2041 {
2037 struct sock *sk; 2042 struct sock *sk;
2038 2043
2039 if (v == SEQ_START_TOKEN) 2044 if (v == SEQ_START_TOKEN)
2040 sk = udp_get_idx(seq, 0); 2045 sk = udp_get_idx(seq, 0);
2041 else 2046 else
2042 sk = udp_get_next(seq, v); 2047 sk = udp_get_next(seq, v);
2043 2048
2044 ++*pos; 2049 ++*pos;
2045 return sk; 2050 return sk;
2046 } 2051 }
2047 2052
2048 static void udp_seq_stop(struct seq_file *seq, void *v) 2053 static void udp_seq_stop(struct seq_file *seq, void *v)
2049 { 2054 {
2050 struct udp_iter_state *state = seq->private; 2055 struct udp_iter_state *state = seq->private;
2051 2056
2052 if (state->bucket <= state->udp_table->mask) 2057 if (state->bucket <= state->udp_table->mask)
2053 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock); 2058 spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2054 } 2059 }
2055 2060
2056 int udp_seq_open(struct inode *inode, struct file *file) 2061 int udp_seq_open(struct inode *inode, struct file *file)
2057 { 2062 {
2058 struct udp_seq_afinfo *afinfo = PDE(inode)->data; 2063 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
2059 struct udp_iter_state *s; 2064 struct udp_iter_state *s;
2060 int err; 2065 int err;
2061 2066
2062 err = seq_open_net(inode, file, &afinfo->seq_ops, 2067 err = seq_open_net(inode, file, &afinfo->seq_ops,
2063 sizeof(struct udp_iter_state)); 2068 sizeof(struct udp_iter_state));
2064 if (err < 0) 2069 if (err < 0)
2065 return err; 2070 return err;
2066 2071
2067 s = ((struct seq_file *)file->private_data)->private; 2072 s = ((struct seq_file *)file->private_data)->private;
2068 s->family = afinfo->family; 2073 s->family = afinfo->family;
2069 s->udp_table = afinfo->udp_table; 2074 s->udp_table = afinfo->udp_table;
2070 return err; 2075 return err;
2071 } 2076 }
2072 EXPORT_SYMBOL(udp_seq_open); 2077 EXPORT_SYMBOL(udp_seq_open);
2073 2078
2074 /* ------------------------------------------------------------------------ */ 2079 /* ------------------------------------------------------------------------ */
2075 int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo) 2080 int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
2076 { 2081 {
2077 struct proc_dir_entry *p; 2082 struct proc_dir_entry *p;
2078 int rc = 0; 2083 int rc = 0;
2079 2084
2080 afinfo->seq_ops.start = udp_seq_start; 2085 afinfo->seq_ops.start = udp_seq_start;
2081 afinfo->seq_ops.next = udp_seq_next; 2086 afinfo->seq_ops.next = udp_seq_next;
2082 afinfo->seq_ops.stop = udp_seq_stop; 2087 afinfo->seq_ops.stop = udp_seq_stop;
2083 2088
2084 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, 2089 p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
2085 afinfo->seq_fops, afinfo); 2090 afinfo->seq_fops, afinfo);
2086 if (!p) 2091 if (!p)
2087 rc = -ENOMEM; 2092 rc = -ENOMEM;
2088 return rc; 2093 return rc;
2089 } 2094 }
2090 EXPORT_SYMBOL(udp_proc_register); 2095 EXPORT_SYMBOL(udp_proc_register);
2091 2096
2092 void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo) 2097 void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
2093 { 2098 {
2094 proc_net_remove(net, afinfo->name); 2099 proc_net_remove(net, afinfo->name);
2095 } 2100 }
2096 EXPORT_SYMBOL(udp_proc_unregister); 2101 EXPORT_SYMBOL(udp_proc_unregister);
2097 2102
2098 /* ------------------------------------------------------------------------ */ 2103 /* ------------------------------------------------------------------------ */
2099 static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2104 static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2100 int bucket, int *len) 2105 int bucket, int *len)
2101 { 2106 {
2102 struct inet_sock *inet = inet_sk(sp); 2107 struct inet_sock *inet = inet_sk(sp);
2103 __be32 dest = inet->inet_daddr; 2108 __be32 dest = inet->inet_daddr;
2104 __be32 src = inet->inet_rcv_saddr; 2109 __be32 src = inet->inet_rcv_saddr;
2105 __u16 destp = ntohs(inet->inet_dport); 2110 __u16 destp = ntohs(inet->inet_dport);
2106 __u16 srcp = ntohs(inet->inet_sport); 2111 __u16 srcp = ntohs(inet->inet_sport);
2107 2112
2108 seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2113 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2109 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n", 2114 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
2110 bucket, src, srcp, dest, destp, sp->sk_state, 2115 bucket, src, srcp, dest, destp, sp->sk_state,
2111 sk_wmem_alloc_get(sp), 2116 sk_wmem_alloc_get(sp),
2112 sk_rmem_alloc_get(sp), 2117 sk_rmem_alloc_get(sp),
2113 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp), 2118 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
2114 atomic_read(&sp->sk_refcnt), sp, 2119 atomic_read(&sp->sk_refcnt), sp,
2115 atomic_read(&sp->sk_drops), len); 2120 atomic_read(&sp->sk_drops), len);
2116 } 2121 }
2117 2122
2118 int udp4_seq_show(struct seq_file *seq, void *v) 2123 int udp4_seq_show(struct seq_file *seq, void *v)
2119 { 2124 {
2120 if (v == SEQ_START_TOKEN) 2125 if (v == SEQ_START_TOKEN)
2121 seq_printf(seq, "%-127s\n", 2126 seq_printf(seq, "%-127s\n",
2122 " sl local_address rem_address st tx_queue " 2127 " sl local_address rem_address st tx_queue "
2123 "rx_queue tr tm->when retrnsmt uid timeout " 2128 "rx_queue tr tm->when retrnsmt uid timeout "
2124 "inode ref pointer drops"); 2129 "inode ref pointer drops");
2125 else { 2130 else {
2126 struct udp_iter_state *state = seq->private; 2131 struct udp_iter_state *state = seq->private;
2127 int len; 2132 int len;
2128 2133
2129 udp4_format_sock(v, seq, state->bucket, &len); 2134 udp4_format_sock(v, seq, state->bucket, &len);
2130 seq_printf(seq, "%*s\n", 127 - len, ""); 2135 seq_printf(seq, "%*s\n", 127 - len, "");
2131 } 2136 }
2132 return 0; 2137 return 0;
2133 } 2138 }
2134 2139
2135 static const struct file_operations udp_afinfo_seq_fops = { 2140 static const struct file_operations udp_afinfo_seq_fops = {
2136 .owner = THIS_MODULE, 2141 .owner = THIS_MODULE,
2137 .open = udp_seq_open, 2142 .open = udp_seq_open,
2138 .read = seq_read, 2143 .read = seq_read,
2139 .llseek = seq_lseek, 2144 .llseek = seq_lseek,
2140 .release = seq_release_net 2145 .release = seq_release_net
2141 }; 2146 };
2142 2147
2143 /* ------------------------------------------------------------------------ */ 2148 /* ------------------------------------------------------------------------ */
2144 static struct udp_seq_afinfo udp4_seq_afinfo = { 2149 static struct udp_seq_afinfo udp4_seq_afinfo = {
2145 .name = "udp", 2150 .name = "udp",
2146 .family = AF_INET, 2151 .family = AF_INET,
2147 .udp_table = &udp_table, 2152 .udp_table = &udp_table,
2148 .seq_fops = &udp_afinfo_seq_fops, 2153 .seq_fops = &udp_afinfo_seq_fops,
2149 .seq_ops = { 2154 .seq_ops = {
2150 .show = udp4_seq_show, 2155 .show = udp4_seq_show,
2151 }, 2156 },
2152 }; 2157 };
2153 2158
2154 static int __net_init udp4_proc_init_net(struct net *net) 2159 static int __net_init udp4_proc_init_net(struct net *net)
2155 { 2160 {
2156 return udp_proc_register(net, &udp4_seq_afinfo); 2161 return udp_proc_register(net, &udp4_seq_afinfo);
2157 } 2162 }
2158 2163
2159 static void __net_exit udp4_proc_exit_net(struct net *net) 2164 static void __net_exit udp4_proc_exit_net(struct net *net)
2160 { 2165 {
2161 udp_proc_unregister(net, &udp4_seq_afinfo); 2166 udp_proc_unregister(net, &udp4_seq_afinfo);
2162 } 2167 }
2163 2168
2164 static struct pernet_operations udp4_net_ops = { 2169 static struct pernet_operations udp4_net_ops = {
2165 .init = udp4_proc_init_net, 2170 .init = udp4_proc_init_net,
2166 .exit = udp4_proc_exit_net, 2171 .exit = udp4_proc_exit_net,
2167 }; 2172 };
2168 2173
2169 int __init udp4_proc_init(void) 2174 int __init udp4_proc_init(void)
2170 { 2175 {
2171 return register_pernet_subsys(&udp4_net_ops); 2176 return register_pernet_subsys(&udp4_net_ops);
2172 } 2177 }
2173 2178
2174 void udp4_proc_exit(void) 2179 void udp4_proc_exit(void)
2175 { 2180 {
2176 unregister_pernet_subsys(&udp4_net_ops); 2181 unregister_pernet_subsys(&udp4_net_ops);
2177 } 2182 }
2178 #endif /* CONFIG_PROC_FS */ 2183 #endif /* CONFIG_PROC_FS */
2179 2184
2180 static __initdata unsigned long uhash_entries; 2185 static __initdata unsigned long uhash_entries;
2181 static int __init set_uhash_entries(char *str) 2186 static int __init set_uhash_entries(char *str)
2182 { 2187 {
2183 ssize_t ret; 2188 ssize_t ret;
2184 2189
2185 if (!str) 2190 if (!str)
2186 return 0; 2191 return 0;
2187 2192
2188 ret = kstrtoul(str, 0, &uhash_entries); 2193 ret = kstrtoul(str, 0, &uhash_entries);
2189 if (ret) 2194 if (ret)
2190 return 0; 2195 return 0;
2191 2196
2192 if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN) 2197 if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2193 uhash_entries = UDP_HTABLE_SIZE_MIN; 2198 uhash_entries = UDP_HTABLE_SIZE_MIN;
2194 return 1; 2199 return 1;
2195 } 2200 }
2196 __setup("uhash_entries=", set_uhash_entries); 2201 __setup("uhash_entries=", set_uhash_entries);
2197 2202
2198 void __init udp_table_init(struct udp_table *table, const char *name) 2203 void __init udp_table_init(struct udp_table *table, const char *name)
2199 { 2204 {
2200 unsigned int i; 2205 unsigned int i;
2201 2206
2202 table->hash = alloc_large_system_hash(name, 2207 table->hash = alloc_large_system_hash(name,
2203 2 * sizeof(struct udp_hslot), 2208 2 * sizeof(struct udp_hslot),
2204 uhash_entries, 2209 uhash_entries,
2205 21, /* one slot per 2 MB */ 2210 21, /* one slot per 2 MB */
2206 0, 2211 0,
2207 &table->log, 2212 &table->log,
2208 &table->mask, 2213 &table->mask,
2209 UDP_HTABLE_SIZE_MIN, 2214 UDP_HTABLE_SIZE_MIN,
2210 64 * 1024); 2215 64 * 1024);
2211 2216
2212 table->hash2 = table->hash + (table->mask + 1); 2217 table->hash2 = table->hash + (table->mask + 1);
2213 for (i = 0; i <= table->mask; i++) { 2218 for (i = 0; i <= table->mask; i++) {
2214 INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i); 2219 INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2215 table->hash[i].count = 0; 2220 table->hash[i].count = 0;
2216 spin_lock_init(&table->hash[i].lock); 2221 spin_lock_init(&table->hash[i].lock);
2217 } 2222 }
2218 for (i = 0; i <= table->mask; i++) { 2223 for (i = 0; i <= table->mask; i++) {
2219 INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i); 2224 INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2220 table->hash2[i].count = 0; 2225 table->hash2[i].count = 0;
2221 spin_lock_init(&table->hash2[i].lock); 2226 spin_lock_init(&table->hash2[i].lock);
2222 } 2227 }
2223 } 2228 }
2224 2229
2225 void __init udp_init(void) 2230 void __init udp_init(void)
2226 { 2231 {
2227 unsigned long limit; 2232 unsigned long limit;
2228 2233
2229 udp_table_init(&udp_table, "UDP"); 2234 udp_table_init(&udp_table, "UDP");
2230 limit = nr_free_buffer_pages() / 8; 2235 limit = nr_free_buffer_pages() / 8;
2231 limit = max(limit, 128UL); 2236 limit = max(limit, 128UL);
2232 sysctl_udp_mem[0] = limit / 4 * 3; 2237 sysctl_udp_mem[0] = limit / 4 * 3;
2233 sysctl_udp_mem[1] = limit; 2238 sysctl_udp_mem[1] = limit;
2234 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2; 2239 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
2235 2240
2236 sysctl_udp_rmem_min = SK_MEM_QUANTUM; 2241 sysctl_udp_rmem_min = SK_MEM_QUANTUM;
2237 sysctl_udp_wmem_min = SK_MEM_QUANTUM; 2242 sysctl_udp_wmem_min = SK_MEM_QUANTUM;
2238 } 2243 }
2239 2244
2240 int udp4_ufo_send_check(struct sk_buff *skb) 2245 int udp4_ufo_send_check(struct sk_buff *skb)
2241 { 2246 {
2242 const struct iphdr *iph; 2247 const struct iphdr *iph;
2243 struct udphdr *uh; 2248 struct udphdr *uh;
2244 2249
2245 if (!pskb_may_pull(skb, sizeof(*uh))) 2250 if (!pskb_may_pull(skb, sizeof(*uh)))
2246 return -EINVAL; 2251 return -EINVAL;
2247 2252
2248 iph = ip_hdr(skb); 2253 iph = ip_hdr(skb);
2249 uh = udp_hdr(skb); 2254 uh = udp_hdr(skb);
2250 2255
2251 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, 2256 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
2252 IPPROTO_UDP, 0); 2257 IPPROTO_UDP, 0);
2253 skb->csum_start = skb_transport_header(skb) - skb->head; 2258 skb->csum_start = skb_transport_header(skb) - skb->head;
2254 skb->csum_offset = offsetof(struct udphdr, check); 2259 skb->csum_offset = offsetof(struct udphdr, check);
2255 skb->ip_summed = CHECKSUM_PARTIAL; 2260 skb->ip_summed = CHECKSUM_PARTIAL;
2256 return 0; 2261 return 0;
2257 } 2262 }
2258 2263
2259 struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, 2264 struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
2260 netdev_features_t features) 2265 netdev_features_t features)
2261 { 2266 {
2262 struct sk_buff *segs = ERR_PTR(-EINVAL); 2267 struct sk_buff *segs = ERR_PTR(-EINVAL);
2263 unsigned int mss; 2268 unsigned int mss;
2264 int offset; 2269 int offset;
2265 __wsum csum; 2270 __wsum csum;
2266 2271
2267 mss = skb_shinfo(skb)->gso_size; 2272 mss = skb_shinfo(skb)->gso_size;
2268 if (unlikely(skb->len <= mss)) 2273 if (unlikely(skb->len <= mss))
2269 goto out; 2274 goto out;
2270 2275
2271 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { 2276 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
2272 /* Packet is from an untrusted source, reset gso_segs. */ 2277 /* Packet is from an untrusted source, reset gso_segs. */
2273 int type = skb_shinfo(skb)->gso_type; 2278 int type = skb_shinfo(skb)->gso_type;
2274 2279
2275 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) || 2280 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
2276 !(type & (SKB_GSO_UDP)))) 2281 !(type & (SKB_GSO_UDP))))
2277 goto out; 2282 goto out;
2278 2283
2279 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); 2284 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
2280 2285
2281 segs = NULL; 2286 segs = NULL;
2282 goto out; 2287 goto out;
2283 } 2288 }
2284 2289
2285 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot 2290 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
2286 * do checksum of UDP packets sent as multiple IP fragments. 2291 * do checksum of UDP packets sent as multiple IP fragments.
2287 */ 2292 */
2288 offset = skb_checksum_start_offset(skb); 2293 offset = skb_checksum_start_offset(skb);
2289 csum = skb_checksum(skb, offset, skb->len - offset, 0); 2294 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2290 offset += skb->csum_offset; 2295 offset += skb->csum_offset;
2291 *(__sum16 *)(skb->data + offset) = csum_fold(csum); 2296 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
2292 skb->ip_summed = CHECKSUM_NONE; 2297 skb->ip_summed = CHECKSUM_NONE;
2293 2298
2294 /* Fragment the skb. IP headers of the fragments are updated in 2299 /* Fragment the skb. IP headers of the fragments are updated in
2295 * inet_gso_segment() 2300 * inet_gso_segment()
2296 */ 2301 */
2297 segs = skb_segment(skb, features); 2302 segs = skb_segment(skb, features);
2298 out: 2303 out:
2299 return segs; 2304 return segs;
2300 } 2305 }
2301 2306
2302 2307
1 /* 1 /*
2 * UDP over IPv6 2 * UDP over IPv6
3 * Linux INET6 implementation 3 * Linux INET6 implementation
4 * 4 *
5 * Authors: 5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt> 6 * Pedro Roque <roque@di.fc.ul.pt>
7 * 7 *
8 * Based on linux/ipv4/udp.c 8 * Based on linux/ipv4/udp.c
9 * 9 *
10 * Fixes: 10 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support 11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which 12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind 13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time. 14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data 15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file. 16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 * 17 *
18 * This program is free software; you can redistribute it and/or 18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License 19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version. 21 * 2 of the License, or (at your option) any later version.
22 */ 22 */
23 23
24 #include <linux/errno.h> 24 #include <linux/errno.h>
25 #include <linux/types.h> 25 #include <linux/types.h>
26 #include <linux/socket.h> 26 #include <linux/socket.h>
27 #include <linux/sockios.h> 27 #include <linux/sockios.h>
28 #include <linux/net.h> 28 #include <linux/net.h>
29 #include <linux/in6.h> 29 #include <linux/in6.h>
30 #include <linux/netdevice.h> 30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h> 31 #include <linux/if_arp.h>
32 #include <linux/ipv6.h> 32 #include <linux/ipv6.h>
33 #include <linux/icmpv6.h> 33 #include <linux/icmpv6.h>
34 #include <linux/init.h> 34 #include <linux/init.h>
35 #include <linux/module.h> 35 #include <linux/module.h>
36 #include <linux/skbuff.h> 36 #include <linux/skbuff.h>
37 #include <linux/slab.h> 37 #include <linux/slab.h>
38 #include <asm/uaccess.h> 38 #include <asm/uaccess.h>
39 39
40 #include <net/ndisc.h> 40 #include <net/ndisc.h>
41 #include <net/protocol.h> 41 #include <net/protocol.h>
42 #include <net/transp_v6.h> 42 #include <net/transp_v6.h>
43 #include <net/ip6_route.h> 43 #include <net/ip6_route.h>
44 #include <net/raw.h> 44 #include <net/raw.h>
45 #include <net/tcp_states.h> 45 #include <net/tcp_states.h>
46 #include <net/ip6_checksum.h> 46 #include <net/ip6_checksum.h>
47 #include <net/xfrm.h> 47 #include <net/xfrm.h>
48 48
49 #include <linux/proc_fs.h> 49 #include <linux/proc_fs.h>
50 #include <linux/seq_file.h> 50 #include <linux/seq_file.h>
51 #include <trace/events/skb.h> 51 #include <trace/events/skb.h>
52 #include "udp_impl.h" 52 #include "udp_impl.h"
53 53
54 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 54 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
55 { 55 {
56 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr; 56 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
57 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 57 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
58 __be32 sk1_rcv_saddr = sk_rcv_saddr(sk); 58 __be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
59 __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2); 59 __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
60 int sk_ipv6only = ipv6_only_sock(sk); 60 int sk_ipv6only = ipv6_only_sock(sk);
61 int sk2_ipv6only = inet_v6_ipv6only(sk2); 61 int sk2_ipv6only = inet_v6_ipv6only(sk2);
62 int addr_type = ipv6_addr_type(sk_rcv_saddr6); 62 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
63 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 63 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
64 64
65 /* if both are mapped, treat as IPv4 */ 65 /* if both are mapped, treat as IPv4 */
66 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) 66 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
67 return (!sk2_ipv6only && 67 return (!sk2_ipv6only &&
68 (!sk1_rcv_saddr || !sk2_rcv_saddr || 68 (!sk1_rcv_saddr || !sk2_rcv_saddr ||
69 sk1_rcv_saddr == sk2_rcv_saddr)); 69 sk1_rcv_saddr == sk2_rcv_saddr));
70 70
71 if (addr_type2 == IPV6_ADDR_ANY && 71 if (addr_type2 == IPV6_ADDR_ANY &&
72 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 72 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
73 return 1; 73 return 1;
74 74
75 if (addr_type == IPV6_ADDR_ANY && 75 if (addr_type == IPV6_ADDR_ANY &&
76 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED)) 76 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
77 return 1; 77 return 1;
78 78
79 if (sk2_rcv_saddr6 && 79 if (sk2_rcv_saddr6 &&
80 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 80 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
81 return 1; 81 return 1;
82 82
83 return 0; 83 return 0;
84 } 84 }
85 85
86 static unsigned int udp6_portaddr_hash(struct net *net, 86 static unsigned int udp6_portaddr_hash(struct net *net,
87 const struct in6_addr *addr6, 87 const struct in6_addr *addr6,
88 unsigned int port) 88 unsigned int port)
89 { 89 {
90 unsigned int hash, mix = net_hash_mix(net); 90 unsigned int hash, mix = net_hash_mix(net);
91 91
92 if (ipv6_addr_any(addr6)) 92 if (ipv6_addr_any(addr6))
93 hash = jhash_1word(0, mix); 93 hash = jhash_1word(0, mix);
94 else if (ipv6_addr_v4mapped(addr6)) 94 else if (ipv6_addr_v4mapped(addr6))
95 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix); 95 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
96 else 96 else
97 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix); 97 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
98 98
99 return hash ^ port; 99 return hash ^ port;
100 } 100 }
101 101
102 102
103 int udp_v6_get_port(struct sock *sk, unsigned short snum) 103 int udp_v6_get_port(struct sock *sk, unsigned short snum)
104 { 104 {
105 unsigned int hash2_nulladdr = 105 unsigned int hash2_nulladdr =
106 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 106 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
107 unsigned int hash2_partial = 107 unsigned int hash2_partial =
108 udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0); 108 udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
109 109
110 /* precompute partial secondary hash */ 110 /* precompute partial secondary hash */
111 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 111 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
112 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr); 112 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
113 } 113 }
114 114
115 static void udp_v6_rehash(struct sock *sk) 115 static void udp_v6_rehash(struct sock *sk)
116 { 116 {
117 u16 new_hash = udp6_portaddr_hash(sock_net(sk), 117 u16 new_hash = udp6_portaddr_hash(sock_net(sk),
118 &inet6_sk(sk)->rcv_saddr, 118 &inet6_sk(sk)->rcv_saddr,
119 inet_sk(sk)->inet_num); 119 inet_sk(sk)->inet_num);
120 120
121 udp_lib_rehash(sk, new_hash); 121 udp_lib_rehash(sk, new_hash);
122 } 122 }
123 123
124 static inline int compute_score(struct sock *sk, struct net *net, 124 static inline int compute_score(struct sock *sk, struct net *net,
125 unsigned short hnum, 125 unsigned short hnum,
126 const struct in6_addr *saddr, __be16 sport, 126 const struct in6_addr *saddr, __be16 sport,
127 const struct in6_addr *daddr, __be16 dport, 127 const struct in6_addr *daddr, __be16 dport,
128 int dif) 128 int dif)
129 { 129 {
130 int score = -1; 130 int score = -1;
131 131
132 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 132 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
133 sk->sk_family == PF_INET6) { 133 sk->sk_family == PF_INET6) {
134 struct ipv6_pinfo *np = inet6_sk(sk); 134 struct ipv6_pinfo *np = inet6_sk(sk);
135 struct inet_sock *inet = inet_sk(sk); 135 struct inet_sock *inet = inet_sk(sk);
136 136
137 score = 0; 137 score = 0;
138 if (inet->inet_dport) { 138 if (inet->inet_dport) {
139 if (inet->inet_dport != sport) 139 if (inet->inet_dport != sport)
140 return -1; 140 return -1;
141 score++; 141 score++;
142 } 142 }
143 if (!ipv6_addr_any(&np->rcv_saddr)) { 143 if (!ipv6_addr_any(&np->rcv_saddr)) {
144 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 144 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
145 return -1; 145 return -1;
146 score++; 146 score++;
147 } 147 }
148 if (!ipv6_addr_any(&np->daddr)) { 148 if (!ipv6_addr_any(&np->daddr)) {
149 if (!ipv6_addr_equal(&np->daddr, saddr)) 149 if (!ipv6_addr_equal(&np->daddr, saddr))
150 return -1; 150 return -1;
151 score++; 151 score++;
152 } 152 }
153 if (sk->sk_bound_dev_if) { 153 if (sk->sk_bound_dev_if) {
154 if (sk->sk_bound_dev_if != dif) 154 if (sk->sk_bound_dev_if != dif)
155 return -1; 155 return -1;
156 score++; 156 score++;
157 } 157 }
158 } 158 }
159 return score; 159 return score;
160 } 160 }
161 161
162 #define SCORE2_MAX (1 + 1 + 1) 162 #define SCORE2_MAX (1 + 1 + 1)
163 static inline int compute_score2(struct sock *sk, struct net *net, 163 static inline int compute_score2(struct sock *sk, struct net *net,
164 const struct in6_addr *saddr, __be16 sport, 164 const struct in6_addr *saddr, __be16 sport,
165 const struct in6_addr *daddr, unsigned short hnum, 165 const struct in6_addr *daddr, unsigned short hnum,
166 int dif) 166 int dif)
167 { 167 {
168 int score = -1; 168 int score = -1;
169 169
170 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 170 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
171 sk->sk_family == PF_INET6) { 171 sk->sk_family == PF_INET6) {
172 struct ipv6_pinfo *np = inet6_sk(sk); 172 struct ipv6_pinfo *np = inet6_sk(sk);
173 struct inet_sock *inet = inet_sk(sk); 173 struct inet_sock *inet = inet_sk(sk);
174 174
175 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 175 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
176 return -1; 176 return -1;
177 score = 0; 177 score = 0;
178 if (inet->inet_dport) { 178 if (inet->inet_dport) {
179 if (inet->inet_dport != sport) 179 if (inet->inet_dport != sport)
180 return -1; 180 return -1;
181 score++; 181 score++;
182 } 182 }
183 if (!ipv6_addr_any(&np->daddr)) { 183 if (!ipv6_addr_any(&np->daddr)) {
184 if (!ipv6_addr_equal(&np->daddr, saddr)) 184 if (!ipv6_addr_equal(&np->daddr, saddr))
185 return -1; 185 return -1;
186 score++; 186 score++;
187 } 187 }
188 if (sk->sk_bound_dev_if) { 188 if (sk->sk_bound_dev_if) {
189 if (sk->sk_bound_dev_if != dif) 189 if (sk->sk_bound_dev_if != dif)
190 return -1; 190 return -1;
191 score++; 191 score++;
192 } 192 }
193 } 193 }
194 return score; 194 return score;
195 } 195 }
196 196
197 197
198 /* called with read_rcu_lock() */ 198 /* called with read_rcu_lock() */
199 static struct sock *udp6_lib_lookup2(struct net *net, 199 static struct sock *udp6_lib_lookup2(struct net *net,
200 const struct in6_addr *saddr, __be16 sport, 200 const struct in6_addr *saddr, __be16 sport,
201 const struct in6_addr *daddr, unsigned int hnum, int dif, 201 const struct in6_addr *daddr, unsigned int hnum, int dif,
202 struct udp_hslot *hslot2, unsigned int slot2) 202 struct udp_hslot *hslot2, unsigned int slot2)
203 { 203 {
204 struct sock *sk, *result; 204 struct sock *sk, *result;
205 struct hlist_nulls_node *node; 205 struct hlist_nulls_node *node;
206 int score, badness; 206 int score, badness;
207 207
208 begin: 208 begin:
209 result = NULL; 209 result = NULL;
210 badness = -1; 210 badness = -1;
211 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) { 211 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
212 score = compute_score2(sk, net, saddr, sport, 212 score = compute_score2(sk, net, saddr, sport,
213 daddr, hnum, dif); 213 daddr, hnum, dif);
214 if (score > badness) { 214 if (score > badness) {
215 result = sk; 215 result = sk;
216 badness = score; 216 badness = score;
217 if (score == SCORE2_MAX) 217 if (score == SCORE2_MAX)
218 goto exact_match; 218 goto exact_match;
219 } 219 }
220 } 220 }
221 /* 221 /*
222 * if the nulls value we got at the end of this lookup is 222 * if the nulls value we got at the end of this lookup is
223 * not the expected one, we must restart lookup. 223 * not the expected one, we must restart lookup.
224 * We probably met an item that was moved to another chain. 224 * We probably met an item that was moved to another chain.
225 */ 225 */
226 if (get_nulls_value(node) != slot2) 226 if (get_nulls_value(node) != slot2)
227 goto begin; 227 goto begin;
228 228
229 if (result) { 229 if (result) {
230 exact_match: 230 exact_match:
231 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 231 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
232 result = NULL; 232 result = NULL;
233 else if (unlikely(compute_score2(result, net, saddr, sport, 233 else if (unlikely(compute_score2(result, net, saddr, sport,
234 daddr, hnum, dif) < badness)) { 234 daddr, hnum, dif) < badness)) {
235 sock_put(result); 235 sock_put(result);
236 goto begin; 236 goto begin;
237 } 237 }
238 } 238 }
239 return result; 239 return result;
240 } 240 }
241 241
242 struct sock *__udp6_lib_lookup(struct net *net, 242 struct sock *__udp6_lib_lookup(struct net *net,
243 const struct in6_addr *saddr, __be16 sport, 243 const struct in6_addr *saddr, __be16 sport,
244 const struct in6_addr *daddr, __be16 dport, 244 const struct in6_addr *daddr, __be16 dport,
245 int dif, struct udp_table *udptable) 245 int dif, struct udp_table *udptable)
246 { 246 {
247 struct sock *sk, *result; 247 struct sock *sk, *result;
248 struct hlist_nulls_node *node; 248 struct hlist_nulls_node *node;
249 unsigned short hnum = ntohs(dport); 249 unsigned short hnum = ntohs(dport);
250 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); 250 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
251 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; 251 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
252 int score, badness; 252 int score, badness;
253 253
254 rcu_read_lock(); 254 rcu_read_lock();
255 if (hslot->count > 10) { 255 if (hslot->count > 10) {
256 hash2 = udp6_portaddr_hash(net, daddr, hnum); 256 hash2 = udp6_portaddr_hash(net, daddr, hnum);
257 slot2 = hash2 & udptable->mask; 257 slot2 = hash2 & udptable->mask;
258 hslot2 = &udptable->hash2[slot2]; 258 hslot2 = &udptable->hash2[slot2];
259 if (hslot->count < hslot2->count) 259 if (hslot->count < hslot2->count)
260 goto begin; 260 goto begin;
261 261
262 result = udp6_lib_lookup2(net, saddr, sport, 262 result = udp6_lib_lookup2(net, saddr, sport,
263 daddr, hnum, dif, 263 daddr, hnum, dif,
264 hslot2, slot2); 264 hslot2, slot2);
265 if (!result) { 265 if (!result) {
266 hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum); 266 hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
267 slot2 = hash2 & udptable->mask; 267 slot2 = hash2 & udptable->mask;
268 hslot2 = &udptable->hash2[slot2]; 268 hslot2 = &udptable->hash2[slot2];
269 if (hslot->count < hslot2->count) 269 if (hslot->count < hslot2->count)
270 goto begin; 270 goto begin;
271 271
272 result = udp6_lib_lookup2(net, saddr, sport, 272 result = udp6_lib_lookup2(net, saddr, sport,
273 &in6addr_any, hnum, dif, 273 &in6addr_any, hnum, dif,
274 hslot2, slot2); 274 hslot2, slot2);
275 } 275 }
276 rcu_read_unlock(); 276 rcu_read_unlock();
277 return result; 277 return result;
278 } 278 }
279 begin: 279 begin:
280 result = NULL; 280 result = NULL;
281 badness = -1; 281 badness = -1;
282 sk_nulls_for_each_rcu(sk, node, &hslot->head) { 282 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
283 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif); 283 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
284 if (score > badness) { 284 if (score > badness) {
285 result = sk; 285 result = sk;
286 badness = score; 286 badness = score;
287 } 287 }
288 } 288 }
289 /* 289 /*
290 * if the nulls value we got at the end of this lookup is 290 * if the nulls value we got at the end of this lookup is
291 * not the expected one, we must restart lookup. 291 * not the expected one, we must restart lookup.
292 * We probably met an item that was moved to another chain. 292 * We probably met an item that was moved to another chain.
293 */ 293 */
294 if (get_nulls_value(node) != slot) 294 if (get_nulls_value(node) != slot)
295 goto begin; 295 goto begin;
296 296
297 if (result) { 297 if (result) {
298 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2))) 298 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
299 result = NULL; 299 result = NULL;
300 else if (unlikely(compute_score(result, net, hnum, saddr, sport, 300 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
301 daddr, dport, dif) < badness)) { 301 daddr, dport, dif) < badness)) {
302 sock_put(result); 302 sock_put(result);
303 goto begin; 303 goto begin;
304 } 304 }
305 } 305 }
306 rcu_read_unlock(); 306 rcu_read_unlock();
307 return result; 307 return result;
308 } 308 }
309 EXPORT_SYMBOL_GPL(__udp6_lib_lookup); 309 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
310 310
311 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb, 311 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
312 __be16 sport, __be16 dport, 312 __be16 sport, __be16 dport,
313 struct udp_table *udptable) 313 struct udp_table *udptable)
314 { 314 {
315 struct sock *sk; 315 struct sock *sk;
316 const struct ipv6hdr *iph = ipv6_hdr(skb); 316 const struct ipv6hdr *iph = ipv6_hdr(skb);
317 317
318 if (unlikely(sk = skb_steal_sock(skb))) 318 if (unlikely(sk = skb_steal_sock(skb)))
319 return sk; 319 return sk;
320 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport, 320 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
321 &iph->daddr, dport, inet6_iif(skb), 321 &iph->daddr, dport, inet6_iif(skb),
322 udptable); 322 udptable);
323 } 323 }
324 324
325 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport, 325 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
326 const struct in6_addr *daddr, __be16 dport, int dif) 326 const struct in6_addr *daddr, __be16 dport, int dif)
327 { 327 {
328 return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table); 328 return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
329 } 329 }
330 EXPORT_SYMBOL_GPL(udp6_lib_lookup); 330 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
331 331
332 332
333 /* 333 /*
334 * This should be easy, if there is something there we 334 * This should be easy, if there is something there we
335 * return it, otherwise we block. 335 * return it, otherwise we block.
336 */ 336 */
337 337
338 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 338 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
339 struct msghdr *msg, size_t len, 339 struct msghdr *msg, size_t len,
340 int noblock, int flags, int *addr_len) 340 int noblock, int flags, int *addr_len)
341 { 341 {
342 struct ipv6_pinfo *np = inet6_sk(sk); 342 struct ipv6_pinfo *np = inet6_sk(sk);
343 struct inet_sock *inet = inet_sk(sk); 343 struct inet_sock *inet = inet_sk(sk);
344 struct sk_buff *skb; 344 struct sk_buff *skb;
345 unsigned int ulen, copied; 345 unsigned int ulen, copied;
346 int peeked, off = 0; 346 int peeked, off = 0;
347 int err; 347 int err;
348 int is_udplite = IS_UDPLITE(sk); 348 int is_udplite = IS_UDPLITE(sk);
349 int is_udp4; 349 int is_udp4;
350 bool slow; 350 bool slow;
351 351
352 if (addr_len) 352 if (addr_len)
353 *addr_len = sizeof(struct sockaddr_in6); 353 *addr_len = sizeof(struct sockaddr_in6);
354 354
355 if (flags & MSG_ERRQUEUE) 355 if (flags & MSG_ERRQUEUE)
356 return ipv6_recv_error(sk, msg, len); 356 return ipv6_recv_error(sk, msg, len);
357 357
358 if (np->rxpmtu && np->rxopt.bits.rxpmtu) 358 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
359 return ipv6_recv_rxpmtu(sk, msg, len); 359 return ipv6_recv_rxpmtu(sk, msg, len);
360 360
361 try_again: 361 try_again:
362 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 362 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
363 &peeked, &off, &err); 363 &peeked, &off, &err);
364 if (!skb) 364 if (!skb)
365 goto out; 365 goto out;
366 366
367 ulen = skb->len - sizeof(struct udphdr); 367 ulen = skb->len - sizeof(struct udphdr);
368 copied = len; 368 copied = len;
369 if (copied > ulen) 369 if (copied > ulen)
370 copied = ulen; 370 copied = ulen;
371 else if (copied < ulen) 371 else if (copied < ulen)
372 msg->msg_flags |= MSG_TRUNC; 372 msg->msg_flags |= MSG_TRUNC;
373 373
374 is_udp4 = (skb->protocol == htons(ETH_P_IP)); 374 is_udp4 = (skb->protocol == htons(ETH_P_IP));
375 375
376 /* 376 /*
377 * If checksum is needed at all, try to do it while copying the 377 * If checksum is needed at all, try to do it while copying the
378 * data. If the data is truncated, or if we only want a partial 378 * data. If the data is truncated, or if we only want a partial
379 * coverage checksum (UDP-Lite), do it before the copy. 379 * coverage checksum (UDP-Lite), do it before the copy.
380 */ 380 */
381 381
382 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { 382 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
383 if (udp_lib_checksum_complete(skb)) 383 if (udp_lib_checksum_complete(skb))
384 goto csum_copy_err; 384 goto csum_copy_err;
385 } 385 }
386 386
387 if (skb_csum_unnecessary(skb)) 387 if (skb_csum_unnecessary(skb))
388 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), 388 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
389 msg->msg_iov, copied); 389 msg->msg_iov, copied);
390 else { 390 else {
391 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov); 391 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
392 if (err == -EINVAL) 392 if (err == -EINVAL)
393 goto csum_copy_err; 393 goto csum_copy_err;
394 } 394 }
395 if (unlikely(err)) { 395 if (unlikely(err)) {
396 trace_kfree_skb(skb, udpv6_recvmsg); 396 trace_kfree_skb(skb, udpv6_recvmsg);
397 if (!peeked) {
398 atomic_inc(&sk->sk_drops);
399 if (is_udp4)
400 UDP_INC_STATS_USER(sock_net(sk),
401 UDP_MIB_INERRORS,
402 is_udplite);
403 else
404 UDP6_INC_STATS_USER(sock_net(sk),
405 UDP_MIB_INERRORS,
406 is_udplite);
407 }
397 goto out_free; 408 goto out_free;
398 } 409 }
399 if (!peeked) { 410 if (!peeked) {
400 if (is_udp4) 411 if (is_udp4)
401 UDP_INC_STATS_USER(sock_net(sk), 412 UDP_INC_STATS_USER(sock_net(sk),
402 UDP_MIB_INDATAGRAMS, is_udplite); 413 UDP_MIB_INDATAGRAMS, is_udplite);
403 else 414 else
404 UDP6_INC_STATS_USER(sock_net(sk), 415 UDP6_INC_STATS_USER(sock_net(sk),
405 UDP_MIB_INDATAGRAMS, is_udplite); 416 UDP_MIB_INDATAGRAMS, is_udplite);
406 } 417 }
407 418
408 sock_recv_ts_and_drops(msg, sk, skb); 419 sock_recv_ts_and_drops(msg, sk, skb);
409 420
410 /* Copy the address. */ 421 /* Copy the address. */
411 if (msg->msg_name) { 422 if (msg->msg_name) {
412 struct sockaddr_in6 *sin6; 423 struct sockaddr_in6 *sin6;
413 424
414 sin6 = (struct sockaddr_in6 *) msg->msg_name; 425 sin6 = (struct sockaddr_in6 *) msg->msg_name;
415 sin6->sin6_family = AF_INET6; 426 sin6->sin6_family = AF_INET6;
416 sin6->sin6_port = udp_hdr(skb)->source; 427 sin6->sin6_port = udp_hdr(skb)->source;
417 sin6->sin6_flowinfo = 0; 428 sin6->sin6_flowinfo = 0;
418 sin6->sin6_scope_id = 0; 429 sin6->sin6_scope_id = 0;
419 430
420 if (is_udp4) 431 if (is_udp4)
421 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr, 432 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
422 &sin6->sin6_addr); 433 &sin6->sin6_addr);
423 else { 434 else {
424 sin6->sin6_addr = ipv6_hdr(skb)->saddr; 435 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
425 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 436 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
426 sin6->sin6_scope_id = IP6CB(skb)->iif; 437 sin6->sin6_scope_id = IP6CB(skb)->iif;
427 } 438 }
428 439
429 } 440 }
430 if (is_udp4) { 441 if (is_udp4) {
431 if (inet->cmsg_flags) 442 if (inet->cmsg_flags)
432 ip_cmsg_recv(msg, skb); 443 ip_cmsg_recv(msg, skb);
433 } else { 444 } else {
434 if (np->rxopt.all) 445 if (np->rxopt.all)
435 datagram_recv_ctl(sk, msg, skb); 446 datagram_recv_ctl(sk, msg, skb);
436 } 447 }
437 448
438 err = copied; 449 err = copied;
439 if (flags & MSG_TRUNC) 450 if (flags & MSG_TRUNC)
440 err = ulen; 451 err = ulen;
441 452
442 out_free: 453 out_free:
443 skb_free_datagram_locked(sk, skb); 454 skb_free_datagram_locked(sk, skb);
444 out: 455 out:
445 return err; 456 return err;
446 457
447 csum_copy_err: 458 csum_copy_err:
448 slow = lock_sock_fast(sk); 459 slow = lock_sock_fast(sk);
449 if (!skb_kill_datagram(sk, skb, flags)) { 460 if (!skb_kill_datagram(sk, skb, flags)) {
450 if (is_udp4) 461 if (is_udp4)
451 UDP_INC_STATS_USER(sock_net(sk), 462 UDP_INC_STATS_USER(sock_net(sk),
452 UDP_MIB_INERRORS, is_udplite); 463 UDP_MIB_INERRORS, is_udplite);
453 else 464 else
454 UDP6_INC_STATS_USER(sock_net(sk), 465 UDP6_INC_STATS_USER(sock_net(sk),
455 UDP_MIB_INERRORS, is_udplite); 466 UDP_MIB_INERRORS, is_udplite);
456 } 467 }
457 unlock_sock_fast(sk, slow); 468 unlock_sock_fast(sk, slow);
458 469
459 if (noblock) 470 if (noblock)
460 return -EAGAIN; 471 return -EAGAIN;
461 472
462 /* starting over for a new packet */ 473 /* starting over for a new packet */
463 msg->msg_flags &= ~MSG_TRUNC; 474 msg->msg_flags &= ~MSG_TRUNC;
464 goto try_again; 475 goto try_again;
465 } 476 }
466 477
467 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 478 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
468 u8 type, u8 code, int offset, __be32 info, 479 u8 type, u8 code, int offset, __be32 info,
469 struct udp_table *udptable) 480 struct udp_table *udptable)
470 { 481 {
471 struct ipv6_pinfo *np; 482 struct ipv6_pinfo *np;
472 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 483 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
473 const struct in6_addr *saddr = &hdr->saddr; 484 const struct in6_addr *saddr = &hdr->saddr;
474 const struct in6_addr *daddr = &hdr->daddr; 485 const struct in6_addr *daddr = &hdr->daddr;
475 struct udphdr *uh = (struct udphdr*)(skb->data+offset); 486 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
476 struct sock *sk; 487 struct sock *sk;
477 int err; 488 int err;
478 489
479 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest, 490 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
480 saddr, uh->source, inet6_iif(skb), udptable); 491 saddr, uh->source, inet6_iif(skb), udptable);
481 if (sk == NULL) 492 if (sk == NULL)
482 return; 493 return;
483 494
484 if (type == ICMPV6_PKT_TOOBIG) 495 if (type == ICMPV6_PKT_TOOBIG)
485 ip6_sk_update_pmtu(skb, sk, info); 496 ip6_sk_update_pmtu(skb, sk, info);
486 if (type == NDISC_REDIRECT) 497 if (type == NDISC_REDIRECT)
487 ip6_sk_redirect(skb, sk); 498 ip6_sk_redirect(skb, sk);
488 499
489 np = inet6_sk(sk); 500 np = inet6_sk(sk);
490 501
491 if (!icmpv6_err_convert(type, code, &err) && !np->recverr) 502 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
492 goto out; 503 goto out;
493 504
494 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr) 505 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
495 goto out; 506 goto out;
496 507
497 if (np->recverr) 508 if (np->recverr)
498 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1)); 509 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
499 510
500 sk->sk_err = err; 511 sk->sk_err = err;
501 sk->sk_error_report(sk); 512 sk->sk_error_report(sk);
502 out: 513 out:
503 sock_put(sk); 514 sock_put(sk);
504 } 515 }
505 516
506 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 517 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
507 { 518 {
508 int rc; 519 int rc;
509 520
510 if (!ipv6_addr_any(&inet6_sk(sk)->daddr)) 521 if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
511 sock_rps_save_rxhash(sk, skb); 522 sock_rps_save_rxhash(sk, skb);
512 523
513 rc = sock_queue_rcv_skb(sk, skb); 524 rc = sock_queue_rcv_skb(sk, skb);
514 if (rc < 0) { 525 if (rc < 0) {
515 int is_udplite = IS_UDPLITE(sk); 526 int is_udplite = IS_UDPLITE(sk);
516 527
517 /* Note that an ENOMEM error is charged twice */ 528 /* Note that an ENOMEM error is charged twice */
518 if (rc == -ENOMEM) 529 if (rc == -ENOMEM)
519 UDP6_INC_STATS_BH(sock_net(sk), 530 UDP6_INC_STATS_BH(sock_net(sk),
520 UDP_MIB_RCVBUFERRORS, is_udplite); 531 UDP_MIB_RCVBUFERRORS, is_udplite);
521 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 532 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
522 kfree_skb(skb); 533 kfree_skb(skb);
523 return -1; 534 return -1;
524 } 535 }
525 return 0; 536 return 0;
526 } 537 }
527 538
528 static __inline__ void udpv6_err(struct sk_buff *skb, 539 static __inline__ void udpv6_err(struct sk_buff *skb,
529 struct inet6_skb_parm *opt, u8 type, 540 struct inet6_skb_parm *opt, u8 type,
530 u8 code, int offset, __be32 info ) 541 u8 code, int offset, __be32 info )
531 { 542 {
532 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table); 543 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
533 } 544 }
534 545
535 static struct static_key udpv6_encap_needed __read_mostly; 546 static struct static_key udpv6_encap_needed __read_mostly;
536 void udpv6_encap_enable(void) 547 void udpv6_encap_enable(void)
537 { 548 {
538 if (!static_key_enabled(&udpv6_encap_needed)) 549 if (!static_key_enabled(&udpv6_encap_needed))
539 static_key_slow_inc(&udpv6_encap_needed); 550 static_key_slow_inc(&udpv6_encap_needed);
540 } 551 }
541 EXPORT_SYMBOL(udpv6_encap_enable); 552 EXPORT_SYMBOL(udpv6_encap_enable);
542 553
543 int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb) 554 int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
544 { 555 {
545 struct udp_sock *up = udp_sk(sk); 556 struct udp_sock *up = udp_sk(sk);
546 int rc; 557 int rc;
547 int is_udplite = IS_UDPLITE(sk); 558 int is_udplite = IS_UDPLITE(sk);
548 559
549 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) 560 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
550 goto drop; 561 goto drop;
551 562
552 if (static_key_false(&udpv6_encap_needed) && up->encap_type) { 563 if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
553 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); 564 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
554 565
555 /* 566 /*
556 * This is an encapsulation socket so pass the skb to 567 * This is an encapsulation socket so pass the skb to
557 * the socket's udp_encap_rcv() hook. Otherwise, just 568 * the socket's udp_encap_rcv() hook. Otherwise, just
558 * fall through and pass this up the UDP socket. 569 * fall through and pass this up the UDP socket.
559 * up->encap_rcv() returns the following value: 570 * up->encap_rcv() returns the following value:
560 * =0 if skb was successfully passed to the encap 571 * =0 if skb was successfully passed to the encap
561 * handler or was discarded by it. 572 * handler or was discarded by it.
562 * >0 if skb should be passed on to UDP. 573 * >0 if skb should be passed on to UDP.
563 * <0 if skb should be resubmitted as proto -N 574 * <0 if skb should be resubmitted as proto -N
564 */ 575 */
565 576
566 /* if we're overly short, let UDP handle it */ 577 /* if we're overly short, let UDP handle it */
567 encap_rcv = ACCESS_ONCE(up->encap_rcv); 578 encap_rcv = ACCESS_ONCE(up->encap_rcv);
568 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) { 579 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
569 int ret; 580 int ret;
570 581
571 ret = encap_rcv(sk, skb); 582 ret = encap_rcv(sk, skb);
572 if (ret <= 0) { 583 if (ret <= 0) {
573 UDP_INC_STATS_BH(sock_net(sk), 584 UDP_INC_STATS_BH(sock_net(sk),
574 UDP_MIB_INDATAGRAMS, 585 UDP_MIB_INDATAGRAMS,
575 is_udplite); 586 is_udplite);
576 return -ret; 587 return -ret;
577 } 588 }
578 } 589 }
579 590
580 /* FALLTHROUGH -- it's a UDP Packet */ 591 /* FALLTHROUGH -- it's a UDP Packet */
581 } 592 }
582 593
583 /* 594 /*
584 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c). 595 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
585 */ 596 */
586 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) { 597 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
587 598
588 if (up->pcrlen == 0) { /* full coverage was set */ 599 if (up->pcrlen == 0) { /* full coverage was set */
589 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage" 600 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
590 " %d while full coverage %d requested\n", 601 " %d while full coverage %d requested\n",
591 UDP_SKB_CB(skb)->cscov, skb->len); 602 UDP_SKB_CB(skb)->cscov, skb->len);
592 goto drop; 603 goto drop;
593 } 604 }
594 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) { 605 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
595 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d " 606 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
596 "too small, need min %d\n", 607 "too small, need min %d\n",
597 UDP_SKB_CB(skb)->cscov, up->pcrlen); 608 UDP_SKB_CB(skb)->cscov, up->pcrlen);
598 goto drop; 609 goto drop;
599 } 610 }
600 } 611 }
601 612
602 if (rcu_access_pointer(sk->sk_filter)) { 613 if (rcu_access_pointer(sk->sk_filter)) {
603 if (udp_lib_checksum_complete(skb)) 614 if (udp_lib_checksum_complete(skb))
604 goto drop; 615 goto drop;
605 } 616 }
606 617
607 if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf)) 618 if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
608 goto drop; 619 goto drop;
609 620
610 skb_dst_drop(skb); 621 skb_dst_drop(skb);
611 622
612 bh_lock_sock(sk); 623 bh_lock_sock(sk);
613 rc = 0; 624 rc = 0;
614 if (!sock_owned_by_user(sk)) 625 if (!sock_owned_by_user(sk))
615 rc = __udpv6_queue_rcv_skb(sk, skb); 626 rc = __udpv6_queue_rcv_skb(sk, skb);
616 else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) { 627 else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
617 bh_unlock_sock(sk); 628 bh_unlock_sock(sk);
618 goto drop; 629 goto drop;
619 } 630 }
620 bh_unlock_sock(sk); 631 bh_unlock_sock(sk);
621 632
622 return rc; 633 return rc;
623 drop: 634 drop:
624 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite); 635 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
625 atomic_inc(&sk->sk_drops); 636 atomic_inc(&sk->sk_drops);
626 kfree_skb(skb); 637 kfree_skb(skb);
627 return -1; 638 return -1;
628 } 639 }
629 640
630 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk, 641 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
631 __be16 loc_port, const struct in6_addr *loc_addr, 642 __be16 loc_port, const struct in6_addr *loc_addr,
632 __be16 rmt_port, const struct in6_addr *rmt_addr, 643 __be16 rmt_port, const struct in6_addr *rmt_addr,
633 int dif) 644 int dif)
634 { 645 {
635 struct hlist_nulls_node *node; 646 struct hlist_nulls_node *node;
636 struct sock *s = sk; 647 struct sock *s = sk;
637 unsigned short num = ntohs(loc_port); 648 unsigned short num = ntohs(loc_port);
638 649
639 sk_nulls_for_each_from(s, node) { 650 sk_nulls_for_each_from(s, node) {
640 struct inet_sock *inet = inet_sk(s); 651 struct inet_sock *inet = inet_sk(s);
641 652
642 if (!net_eq(sock_net(s), net)) 653 if (!net_eq(sock_net(s), net))
643 continue; 654 continue;
644 655
645 if (udp_sk(s)->udp_port_hash == num && 656 if (udp_sk(s)->udp_port_hash == num &&
646 s->sk_family == PF_INET6) { 657 s->sk_family == PF_INET6) {
647 struct ipv6_pinfo *np = inet6_sk(s); 658 struct ipv6_pinfo *np = inet6_sk(s);
648 if (inet->inet_dport) { 659 if (inet->inet_dport) {
649 if (inet->inet_dport != rmt_port) 660 if (inet->inet_dport != rmt_port)
650 continue; 661 continue;
651 } 662 }
652 if (!ipv6_addr_any(&np->daddr) && 663 if (!ipv6_addr_any(&np->daddr) &&
653 !ipv6_addr_equal(&np->daddr, rmt_addr)) 664 !ipv6_addr_equal(&np->daddr, rmt_addr))
654 continue; 665 continue;
655 666
656 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) 667 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
657 continue; 668 continue;
658 669
659 if (!ipv6_addr_any(&np->rcv_saddr)) { 670 if (!ipv6_addr_any(&np->rcv_saddr)) {
660 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 671 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
661 continue; 672 continue;
662 } 673 }
663 if (!inet6_mc_check(s, loc_addr, rmt_addr)) 674 if (!inet6_mc_check(s, loc_addr, rmt_addr))
664 continue; 675 continue;
665 return s; 676 return s;
666 } 677 }
667 } 678 }
668 return NULL; 679 return NULL;
669 } 680 }
670 681
671 static void flush_stack(struct sock **stack, unsigned int count, 682 static void flush_stack(struct sock **stack, unsigned int count,
672 struct sk_buff *skb, unsigned int final) 683 struct sk_buff *skb, unsigned int final)
673 { 684 {
674 struct sk_buff *skb1 = NULL; 685 struct sk_buff *skb1 = NULL;
675 struct sock *sk; 686 struct sock *sk;
676 unsigned int i; 687 unsigned int i;
677 688
678 for (i = 0; i < count; i++) { 689 for (i = 0; i < count; i++) {
679 sk = stack[i]; 690 sk = stack[i];
680 if (likely(skb1 == NULL)) 691 if (likely(skb1 == NULL))
681 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC); 692 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
682 if (!skb1) { 693 if (!skb1) {
683 atomic_inc(&sk->sk_drops); 694 atomic_inc(&sk->sk_drops);
684 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 695 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
685 IS_UDPLITE(sk)); 696 IS_UDPLITE(sk));
686 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, 697 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
687 IS_UDPLITE(sk)); 698 IS_UDPLITE(sk));
688 } 699 }
689 700
690 if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0) 701 if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
691 skb1 = NULL; 702 skb1 = NULL;
692 } 703 }
693 if (unlikely(skb1)) 704 if (unlikely(skb1))
694 kfree_skb(skb1); 705 kfree_skb(skb1);
695 } 706 }
696 /* 707 /*
697 * Note: called only from the BH handler context, 708 * Note: called only from the BH handler context,
698 * so we don't need to lock the hashes. 709 * so we don't need to lock the hashes.
699 */ 710 */
700 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, 711 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
701 const struct in6_addr *saddr, const struct in6_addr *daddr, 712 const struct in6_addr *saddr, const struct in6_addr *daddr,
702 struct udp_table *udptable) 713 struct udp_table *udptable)
703 { 714 {
704 struct sock *sk, *stack[256 / sizeof(struct sock *)]; 715 struct sock *sk, *stack[256 / sizeof(struct sock *)];
705 const struct udphdr *uh = udp_hdr(skb); 716 const struct udphdr *uh = udp_hdr(skb);
706 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest)); 717 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
707 int dif; 718 int dif;
708 unsigned int i, count = 0; 719 unsigned int i, count = 0;
709 720
710 spin_lock(&hslot->lock); 721 spin_lock(&hslot->lock);
711 sk = sk_nulls_head(&hslot->head); 722 sk = sk_nulls_head(&hslot->head);
712 dif = inet6_iif(skb); 723 dif = inet6_iif(skb);
713 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif); 724 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
714 while (sk) { 725 while (sk) {
715 stack[count++] = sk; 726 stack[count++] = sk;
716 sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr, 727 sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
717 uh->source, saddr, dif); 728 uh->source, saddr, dif);
718 if (unlikely(count == ARRAY_SIZE(stack))) { 729 if (unlikely(count == ARRAY_SIZE(stack))) {
719 if (!sk) 730 if (!sk)
720 break; 731 break;
721 flush_stack(stack, count, skb, ~0); 732 flush_stack(stack, count, skb, ~0);
722 count = 0; 733 count = 0;
723 } 734 }
724 } 735 }
725 /* 736 /*
726 * before releasing the lock, we must take reference on sockets 737 * before releasing the lock, we must take reference on sockets
727 */ 738 */
728 for (i = 0; i < count; i++) 739 for (i = 0; i < count; i++)
729 sock_hold(stack[i]); 740 sock_hold(stack[i]);
730 741
731 spin_unlock(&hslot->lock); 742 spin_unlock(&hslot->lock);
732 743
733 if (count) { 744 if (count) {
734 flush_stack(stack, count, skb, count - 1); 745 flush_stack(stack, count, skb, count - 1);
735 746
736 for (i = 0; i < count; i++) 747 for (i = 0; i < count; i++)
737 sock_put(stack[i]); 748 sock_put(stack[i]);
738 } else { 749 } else {
739 kfree_skb(skb); 750 kfree_skb(skb);
740 } 751 }
741 return 0; 752 return 0;
742 } 753 }
743 754
744 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, 755 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
745 int proto) 756 int proto)
746 { 757 {
747 int err; 758 int err;
748 759
749 UDP_SKB_CB(skb)->partial_cov = 0; 760 UDP_SKB_CB(skb)->partial_cov = 0;
750 UDP_SKB_CB(skb)->cscov = skb->len; 761 UDP_SKB_CB(skb)->cscov = skb->len;
751 762
752 if (proto == IPPROTO_UDPLITE) { 763 if (proto == IPPROTO_UDPLITE) {
753 err = udplite_checksum_init(skb, uh); 764 err = udplite_checksum_init(skb, uh);
754 if (err) 765 if (err)
755 return err; 766 return err;
756 } 767 }
757 768
758 if (uh->check == 0) { 769 if (uh->check == 0) {
759 /* RFC 2460 section 8.1 says that we SHOULD log 770 /* RFC 2460 section 8.1 says that we SHOULD log
760 this error. Well, it is reasonable. 771 this error. Well, it is reasonable.
761 */ 772 */
762 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n"); 773 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
763 return 1; 774 return 1;
764 } 775 }
765 if (skb->ip_summed == CHECKSUM_COMPLETE && 776 if (skb->ip_summed == CHECKSUM_COMPLETE &&
766 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, 777 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
767 skb->len, proto, skb->csum)) 778 skb->len, proto, skb->csum))
768 skb->ip_summed = CHECKSUM_UNNECESSARY; 779 skb->ip_summed = CHECKSUM_UNNECESSARY;
769 780
770 if (!skb_csum_unnecessary(skb)) 781 if (!skb_csum_unnecessary(skb))
771 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 782 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
772 &ipv6_hdr(skb)->daddr, 783 &ipv6_hdr(skb)->daddr,
773 skb->len, proto, 0)); 784 skb->len, proto, 0));
774 785
775 return 0; 786 return 0;
776 } 787 }
777 788
778 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable, 789 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
779 int proto) 790 int proto)
780 { 791 {
781 struct net *net = dev_net(skb->dev); 792 struct net *net = dev_net(skb->dev);
782 struct sock *sk; 793 struct sock *sk;
783 struct udphdr *uh; 794 struct udphdr *uh;
784 const struct in6_addr *saddr, *daddr; 795 const struct in6_addr *saddr, *daddr;
785 u32 ulen = 0; 796 u32 ulen = 0;
786 797
787 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 798 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
788 goto discard; 799 goto discard;
789 800
790 saddr = &ipv6_hdr(skb)->saddr; 801 saddr = &ipv6_hdr(skb)->saddr;
791 daddr = &ipv6_hdr(skb)->daddr; 802 daddr = &ipv6_hdr(skb)->daddr;
792 uh = udp_hdr(skb); 803 uh = udp_hdr(skb);
793 804
794 ulen = ntohs(uh->len); 805 ulen = ntohs(uh->len);
795 if (ulen > skb->len) 806 if (ulen > skb->len)
796 goto short_packet; 807 goto short_packet;
797 808
798 if (proto == IPPROTO_UDP) { 809 if (proto == IPPROTO_UDP) {
799 /* UDP validates ulen. */ 810 /* UDP validates ulen. */
800 811
801 /* Check for jumbo payload */ 812 /* Check for jumbo payload */
802 if (ulen == 0) 813 if (ulen == 0)
803 ulen = skb->len; 814 ulen = skb->len;
804 815
805 if (ulen < sizeof(*uh)) 816 if (ulen < sizeof(*uh))
806 goto short_packet; 817 goto short_packet;
807 818
808 if (ulen < skb->len) { 819 if (ulen < skb->len) {
809 if (pskb_trim_rcsum(skb, ulen)) 820 if (pskb_trim_rcsum(skb, ulen))
810 goto short_packet; 821 goto short_packet;
811 saddr = &ipv6_hdr(skb)->saddr; 822 saddr = &ipv6_hdr(skb)->saddr;
812 daddr = &ipv6_hdr(skb)->daddr; 823 daddr = &ipv6_hdr(skb)->daddr;
813 uh = udp_hdr(skb); 824 uh = udp_hdr(skb);
814 } 825 }
815 } 826 }
816 827
817 if (udp6_csum_init(skb, uh, proto)) 828 if (udp6_csum_init(skb, uh, proto))
818 goto discard; 829 goto discard;
819 830
820 /* 831 /*
821 * Multicast receive code 832 * Multicast receive code
822 */ 833 */
823 if (ipv6_addr_is_multicast(daddr)) 834 if (ipv6_addr_is_multicast(daddr))
824 return __udp6_lib_mcast_deliver(net, skb, 835 return __udp6_lib_mcast_deliver(net, skb,
825 saddr, daddr, udptable); 836 saddr, daddr, udptable);
826 837
827 /* Unicast */ 838 /* Unicast */
828 839
829 /* 840 /*
830 * check socket cache ... must talk to Alan about his plans 841 * check socket cache ... must talk to Alan about his plans
831 * for sock caches... i'll skip this for now. 842 * for sock caches... i'll skip this for now.
832 */ 843 */
833 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 844 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
834 if (sk != NULL) { 845 if (sk != NULL) {
835 int ret = udpv6_queue_rcv_skb(sk, skb); 846 int ret = udpv6_queue_rcv_skb(sk, skb);
836 sock_put(sk); 847 sock_put(sk);
837 848
838 /* a return value > 0 means to resubmit the input, but 849 /* a return value > 0 means to resubmit the input, but
839 * it wants the return to be -protocol, or 0 850 * it wants the return to be -protocol, or 0
840 */ 851 */
841 if (ret > 0) 852 if (ret > 0)
842 return -ret; 853 return -ret;
843 854
844 return 0; 855 return 0;
845 } 856 }
846 857
847 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) 858 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
848 goto discard; 859 goto discard;
849 860
850 if (udp_lib_checksum_complete(skb)) 861 if (udp_lib_checksum_complete(skb))
851 goto discard; 862 goto discard;
852 863
853 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); 864 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
854 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0); 865 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
855 866
856 kfree_skb(skb); 867 kfree_skb(skb);
857 return 0; 868 return 0;
858 869
859 short_packet: 870 short_packet:
860 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n", 871 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
861 proto == IPPROTO_UDPLITE ? "-Lite" : "", 872 proto == IPPROTO_UDPLITE ? "-Lite" : "",
862 saddr, 873 saddr,
863 ntohs(uh->source), 874 ntohs(uh->source),
864 ulen, 875 ulen,
865 skb->len, 876 skb->len,
866 daddr, 877 daddr,
867 ntohs(uh->dest)); 878 ntohs(uh->dest));
868 879
869 discard: 880 discard:
870 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); 881 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
871 kfree_skb(skb); 882 kfree_skb(skb);
872 return 0; 883 return 0;
873 } 884 }
874 885
875 static __inline__ int udpv6_rcv(struct sk_buff *skb) 886 static __inline__ int udpv6_rcv(struct sk_buff *skb)
876 { 887 {
877 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP); 888 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
878 } 889 }
879 890
880 /* 891 /*
881 * Throw away all pending data and cancel the corking. Socket is locked. 892 * Throw away all pending data and cancel the corking. Socket is locked.
882 */ 893 */
883 static void udp_v6_flush_pending_frames(struct sock *sk) 894 static void udp_v6_flush_pending_frames(struct sock *sk)
884 { 895 {
885 struct udp_sock *up = udp_sk(sk); 896 struct udp_sock *up = udp_sk(sk);
886 897
887 if (up->pending == AF_INET) 898 if (up->pending == AF_INET)
888 udp_flush_pending_frames(sk); 899 udp_flush_pending_frames(sk);
889 else if (up->pending) { 900 else if (up->pending) {
890 up->len = 0; 901 up->len = 0;
891 up->pending = 0; 902 up->pending = 0;
892 ip6_flush_pending_frames(sk); 903 ip6_flush_pending_frames(sk);
893 } 904 }
894 } 905 }
895 906
896 /** 907 /**
897 * udp6_hwcsum_outgoing - handle outgoing HW checksumming 908 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
898 * @sk: socket we are sending on 909 * @sk: socket we are sending on
899 * @skb: sk_buff containing the filled-in UDP header 910 * @skb: sk_buff containing the filled-in UDP header
900 * (checksum field must be zeroed out) 911 * (checksum field must be zeroed out)
901 */ 912 */
902 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb, 913 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
903 const struct in6_addr *saddr, 914 const struct in6_addr *saddr,
904 const struct in6_addr *daddr, int len) 915 const struct in6_addr *daddr, int len)
905 { 916 {
906 unsigned int offset; 917 unsigned int offset;
907 struct udphdr *uh = udp_hdr(skb); 918 struct udphdr *uh = udp_hdr(skb);
908 __wsum csum = 0; 919 __wsum csum = 0;
909 920
910 if (skb_queue_len(&sk->sk_write_queue) == 1) { 921 if (skb_queue_len(&sk->sk_write_queue) == 1) {
911 /* Only one fragment on the socket. */ 922 /* Only one fragment on the socket. */
912 skb->csum_start = skb_transport_header(skb) - skb->head; 923 skb->csum_start = skb_transport_header(skb) - skb->head;
913 skb->csum_offset = offsetof(struct udphdr, check); 924 skb->csum_offset = offsetof(struct udphdr, check);
914 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0); 925 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
915 } else { 926 } else {
916 /* 927 /*
917 * HW-checksum won't work as there are two or more 928 * HW-checksum won't work as there are two or more
918 * fragments on the socket so that all csums of sk_buffs 929 * fragments on the socket so that all csums of sk_buffs
919 * should be together 930 * should be together
920 */ 931 */
921 offset = skb_transport_offset(skb); 932 offset = skb_transport_offset(skb);
922 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 933 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
923 934
924 skb->ip_summed = CHECKSUM_NONE; 935 skb->ip_summed = CHECKSUM_NONE;
925 936
926 skb_queue_walk(&sk->sk_write_queue, skb) { 937 skb_queue_walk(&sk->sk_write_queue, skb) {
927 csum = csum_add(csum, skb->csum); 938 csum = csum_add(csum, skb->csum);
928 } 939 }
929 940
930 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 941 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
931 csum); 942 csum);
932 if (uh->check == 0) 943 if (uh->check == 0)
933 uh->check = CSUM_MANGLED_0; 944 uh->check = CSUM_MANGLED_0;
934 } 945 }
935 } 946 }
936 947
937 /* 948 /*
938 * Sending 949 * Sending
939 */ 950 */
940 951
941 static int udp_v6_push_pending_frames(struct sock *sk) 952 static int udp_v6_push_pending_frames(struct sock *sk)
942 { 953 {
943 struct sk_buff *skb; 954 struct sk_buff *skb;
944 struct udphdr *uh; 955 struct udphdr *uh;
945 struct udp_sock *up = udp_sk(sk); 956 struct udp_sock *up = udp_sk(sk);
946 struct inet_sock *inet = inet_sk(sk); 957 struct inet_sock *inet = inet_sk(sk);
947 struct flowi6 *fl6 = &inet->cork.fl.u.ip6; 958 struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
948 int err = 0; 959 int err = 0;
949 int is_udplite = IS_UDPLITE(sk); 960 int is_udplite = IS_UDPLITE(sk);
950 __wsum csum = 0; 961 __wsum csum = 0;
951 962
952 /* Grab the skbuff where UDP header space exists. */ 963 /* Grab the skbuff where UDP header space exists. */
953 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL) 964 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
954 goto out; 965 goto out;
955 966
956 /* 967 /*
957 * Create a UDP header 968 * Create a UDP header
958 */ 969 */
959 uh = udp_hdr(skb); 970 uh = udp_hdr(skb);
960 uh->source = fl6->fl6_sport; 971 uh->source = fl6->fl6_sport;
961 uh->dest = fl6->fl6_dport; 972 uh->dest = fl6->fl6_dport;
962 uh->len = htons(up->len); 973 uh->len = htons(up->len);
963 uh->check = 0; 974 uh->check = 0;
964 975
965 if (is_udplite) 976 if (is_udplite)
966 csum = udplite_csum_outgoing(sk, skb); 977 csum = udplite_csum_outgoing(sk, skb);
967 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ 978 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
968 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, 979 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
969 up->len); 980 up->len);
970 goto send; 981 goto send;
971 } else 982 } else
972 csum = udp_csum_outgoing(sk, skb); 983 csum = udp_csum_outgoing(sk, skb);
973 984
974 /* add protocol-dependent pseudo-header */ 985 /* add protocol-dependent pseudo-header */
975 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr, 986 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
976 up->len, fl6->flowi6_proto, csum); 987 up->len, fl6->flowi6_proto, csum);
977 if (uh->check == 0) 988 if (uh->check == 0)
978 uh->check = CSUM_MANGLED_0; 989 uh->check = CSUM_MANGLED_0;
979 990
980 send: 991 send:
981 err = ip6_push_pending_frames(sk); 992 err = ip6_push_pending_frames(sk);
982 if (err) { 993 if (err) {
983 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) { 994 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
984 UDP6_INC_STATS_USER(sock_net(sk), 995 UDP6_INC_STATS_USER(sock_net(sk),
985 UDP_MIB_SNDBUFERRORS, is_udplite); 996 UDP_MIB_SNDBUFERRORS, is_udplite);
986 err = 0; 997 err = 0;
987 } 998 }
988 } else 999 } else
989 UDP6_INC_STATS_USER(sock_net(sk), 1000 UDP6_INC_STATS_USER(sock_net(sk),
990 UDP_MIB_OUTDATAGRAMS, is_udplite); 1001 UDP_MIB_OUTDATAGRAMS, is_udplite);
991 out: 1002 out:
992 up->len = 0; 1003 up->len = 0;
993 up->pending = 0; 1004 up->pending = 0;
994 return err; 1005 return err;
995 } 1006 }
996 1007
997 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 1008 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
998 struct msghdr *msg, size_t len) 1009 struct msghdr *msg, size_t len)
999 { 1010 {
1000 struct ipv6_txoptions opt_space; 1011 struct ipv6_txoptions opt_space;
1001 struct udp_sock *up = udp_sk(sk); 1012 struct udp_sock *up = udp_sk(sk);
1002 struct inet_sock *inet = inet_sk(sk); 1013 struct inet_sock *inet = inet_sk(sk);
1003 struct ipv6_pinfo *np = inet6_sk(sk); 1014 struct ipv6_pinfo *np = inet6_sk(sk);
1004 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name; 1015 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
1005 struct in6_addr *daddr, *final_p, final; 1016 struct in6_addr *daddr, *final_p, final;
1006 struct ipv6_txoptions *opt = NULL; 1017 struct ipv6_txoptions *opt = NULL;
1007 struct ip6_flowlabel *flowlabel = NULL; 1018 struct ip6_flowlabel *flowlabel = NULL;
1008 struct flowi6 fl6; 1019 struct flowi6 fl6;
1009 struct dst_entry *dst; 1020 struct dst_entry *dst;
1010 int addr_len = msg->msg_namelen; 1021 int addr_len = msg->msg_namelen;
1011 int ulen = len; 1022 int ulen = len;
1012 int hlimit = -1; 1023 int hlimit = -1;
1013 int tclass = -1; 1024 int tclass = -1;
1014 int dontfrag = -1; 1025 int dontfrag = -1;
1015 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; 1026 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1016 int err; 1027 int err;
1017 int connected = 0; 1028 int connected = 0;
1018 int is_udplite = IS_UDPLITE(sk); 1029 int is_udplite = IS_UDPLITE(sk);
1019 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1030 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1020 1031
1021 /* destination address check */ 1032 /* destination address check */
1022 if (sin6) { 1033 if (sin6) {
1023 if (addr_len < offsetof(struct sockaddr, sa_data)) 1034 if (addr_len < offsetof(struct sockaddr, sa_data))
1024 return -EINVAL; 1035 return -EINVAL;
1025 1036
1026 switch (sin6->sin6_family) { 1037 switch (sin6->sin6_family) {
1027 case AF_INET6: 1038 case AF_INET6:
1028 if (addr_len < SIN6_LEN_RFC2133) 1039 if (addr_len < SIN6_LEN_RFC2133)
1029 return -EINVAL; 1040 return -EINVAL;
1030 daddr = &sin6->sin6_addr; 1041 daddr = &sin6->sin6_addr;
1031 break; 1042 break;
1032 case AF_INET: 1043 case AF_INET:
1033 goto do_udp_sendmsg; 1044 goto do_udp_sendmsg;
1034 case AF_UNSPEC: 1045 case AF_UNSPEC:
1035 msg->msg_name = sin6 = NULL; 1046 msg->msg_name = sin6 = NULL;
1036 msg->msg_namelen = addr_len = 0; 1047 msg->msg_namelen = addr_len = 0;
1037 daddr = NULL; 1048 daddr = NULL;
1038 break; 1049 break;
1039 default: 1050 default:
1040 return -EINVAL; 1051 return -EINVAL;
1041 } 1052 }
1042 } else if (!up->pending) { 1053 } else if (!up->pending) {
1043 if (sk->sk_state != TCP_ESTABLISHED) 1054 if (sk->sk_state != TCP_ESTABLISHED)
1044 return -EDESTADDRREQ; 1055 return -EDESTADDRREQ;
1045 daddr = &np->daddr; 1056 daddr = &np->daddr;
1046 } else 1057 } else
1047 daddr = NULL; 1058 daddr = NULL;
1048 1059
1049 if (daddr) { 1060 if (daddr) {
1050 if (ipv6_addr_v4mapped(daddr)) { 1061 if (ipv6_addr_v4mapped(daddr)) {
1051 struct sockaddr_in sin; 1062 struct sockaddr_in sin;
1052 sin.sin_family = AF_INET; 1063 sin.sin_family = AF_INET;
1053 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport; 1064 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1054 sin.sin_addr.s_addr = daddr->s6_addr32[3]; 1065 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1055 msg->msg_name = &sin; 1066 msg->msg_name = &sin;
1056 msg->msg_namelen = sizeof(sin); 1067 msg->msg_namelen = sizeof(sin);
1057 do_udp_sendmsg: 1068 do_udp_sendmsg:
1058 if (__ipv6_only_sock(sk)) 1069 if (__ipv6_only_sock(sk))
1059 return -ENETUNREACH; 1070 return -ENETUNREACH;
1060 return udp_sendmsg(iocb, sk, msg, len); 1071 return udp_sendmsg(iocb, sk, msg, len);
1061 } 1072 }
1062 } 1073 }
1063 1074
1064 if (up->pending == AF_INET) 1075 if (up->pending == AF_INET)
1065 return udp_sendmsg(iocb, sk, msg, len); 1076 return udp_sendmsg(iocb, sk, msg, len);
1066 1077
1067 /* Rough check on arithmetic overflow, 1078 /* Rough check on arithmetic overflow,
1068 better check is made in ip6_append_data(). 1079 better check is made in ip6_append_data().
1069 */ 1080 */
1070 if (len > INT_MAX - sizeof(struct udphdr)) 1081 if (len > INT_MAX - sizeof(struct udphdr))
1071 return -EMSGSIZE; 1082 return -EMSGSIZE;
1072 1083
1073 if (up->pending) { 1084 if (up->pending) {
1074 /* 1085 /*
1075 * There are pending frames. 1086 * There are pending frames.
1076 * The socket lock must be held while it's corked. 1087 * The socket lock must be held while it's corked.
1077 */ 1088 */
1078 lock_sock(sk); 1089 lock_sock(sk);
1079 if (likely(up->pending)) { 1090 if (likely(up->pending)) {
1080 if (unlikely(up->pending != AF_INET6)) { 1091 if (unlikely(up->pending != AF_INET6)) {
1081 release_sock(sk); 1092 release_sock(sk);
1082 return -EAFNOSUPPORT; 1093 return -EAFNOSUPPORT;
1083 } 1094 }
1084 dst = NULL; 1095 dst = NULL;
1085 goto do_append_data; 1096 goto do_append_data;
1086 } 1097 }
1087 release_sock(sk); 1098 release_sock(sk);
1088 } 1099 }
1089 ulen += sizeof(struct udphdr); 1100 ulen += sizeof(struct udphdr);
1090 1101
1091 memset(&fl6, 0, sizeof(fl6)); 1102 memset(&fl6, 0, sizeof(fl6));
1092 1103
1093 if (sin6) { 1104 if (sin6) {
1094 if (sin6->sin6_port == 0) 1105 if (sin6->sin6_port == 0)
1095 return -EINVAL; 1106 return -EINVAL;
1096 1107
1097 fl6.fl6_dport = sin6->sin6_port; 1108 fl6.fl6_dport = sin6->sin6_port;
1098 daddr = &sin6->sin6_addr; 1109 daddr = &sin6->sin6_addr;
1099 1110
1100 if (np->sndflow) { 1111 if (np->sndflow) {
1101 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 1112 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1102 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 1113 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1103 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1114 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1104 if (flowlabel == NULL) 1115 if (flowlabel == NULL)
1105 return -EINVAL; 1116 return -EINVAL;
1106 daddr = &flowlabel->dst; 1117 daddr = &flowlabel->dst;
1107 } 1118 }
1108 } 1119 }
1109 1120
1110 /* 1121 /*
1111 * Otherwise it will be difficult to maintain 1122 * Otherwise it will be difficult to maintain
1112 * sk->sk_dst_cache. 1123 * sk->sk_dst_cache.
1113 */ 1124 */
1114 if (sk->sk_state == TCP_ESTABLISHED && 1125 if (sk->sk_state == TCP_ESTABLISHED &&
1115 ipv6_addr_equal(daddr, &np->daddr)) 1126 ipv6_addr_equal(daddr, &np->daddr))
1116 daddr = &np->daddr; 1127 daddr = &np->daddr;
1117 1128
1118 if (addr_len >= sizeof(struct sockaddr_in6) && 1129 if (addr_len >= sizeof(struct sockaddr_in6) &&
1119 sin6->sin6_scope_id && 1130 sin6->sin6_scope_id &&
1120 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL) 1131 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
1121 fl6.flowi6_oif = sin6->sin6_scope_id; 1132 fl6.flowi6_oif = sin6->sin6_scope_id;
1122 } else { 1133 } else {
1123 if (sk->sk_state != TCP_ESTABLISHED) 1134 if (sk->sk_state != TCP_ESTABLISHED)
1124 return -EDESTADDRREQ; 1135 return -EDESTADDRREQ;
1125 1136
1126 fl6.fl6_dport = inet->inet_dport; 1137 fl6.fl6_dport = inet->inet_dport;
1127 daddr = &np->daddr; 1138 daddr = &np->daddr;
1128 fl6.flowlabel = np->flow_label; 1139 fl6.flowlabel = np->flow_label;
1129 connected = 1; 1140 connected = 1;
1130 } 1141 }
1131 1142
1132 if (!fl6.flowi6_oif) 1143 if (!fl6.flowi6_oif)
1133 fl6.flowi6_oif = sk->sk_bound_dev_if; 1144 fl6.flowi6_oif = sk->sk_bound_dev_if;
1134 1145
1135 if (!fl6.flowi6_oif) 1146 if (!fl6.flowi6_oif)
1136 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; 1147 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1137 1148
1138 fl6.flowi6_mark = sk->sk_mark; 1149 fl6.flowi6_mark = sk->sk_mark;
1139 1150
1140 if (msg->msg_controllen) { 1151 if (msg->msg_controllen) {
1141 opt = &opt_space; 1152 opt = &opt_space;
1142 memset(opt, 0, sizeof(struct ipv6_txoptions)); 1153 memset(opt, 0, sizeof(struct ipv6_txoptions));
1143 opt->tot_len = sizeof(*opt); 1154 opt->tot_len = sizeof(*opt);
1144 1155
1145 err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt, 1156 err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1146 &hlimit, &tclass, &dontfrag); 1157 &hlimit, &tclass, &dontfrag);
1147 if (err < 0) { 1158 if (err < 0) {
1148 fl6_sock_release(flowlabel); 1159 fl6_sock_release(flowlabel);
1149 return err; 1160 return err;
1150 } 1161 }
1151 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 1162 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1152 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1163 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1153 if (flowlabel == NULL) 1164 if (flowlabel == NULL)
1154 return -EINVAL; 1165 return -EINVAL;
1155 } 1166 }
1156 if (!(opt->opt_nflen|opt->opt_flen)) 1167 if (!(opt->opt_nflen|opt->opt_flen))
1157 opt = NULL; 1168 opt = NULL;
1158 connected = 0; 1169 connected = 0;
1159 } 1170 }
1160 if (opt == NULL) 1171 if (opt == NULL)
1161 opt = np->opt; 1172 opt = np->opt;
1162 if (flowlabel) 1173 if (flowlabel)
1163 opt = fl6_merge_options(&opt_space, flowlabel, opt); 1174 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1164 opt = ipv6_fixup_options(&opt_space, opt); 1175 opt = ipv6_fixup_options(&opt_space, opt);
1165 1176
1166 fl6.flowi6_proto = sk->sk_protocol; 1177 fl6.flowi6_proto = sk->sk_protocol;
1167 if (!ipv6_addr_any(daddr)) 1178 if (!ipv6_addr_any(daddr))
1168 fl6.daddr = *daddr; 1179 fl6.daddr = *daddr;
1169 else 1180 else
1170 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */ 1181 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1171 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr)) 1182 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1172 fl6.saddr = np->saddr; 1183 fl6.saddr = np->saddr;
1173 fl6.fl6_sport = inet->inet_sport; 1184 fl6.fl6_sport = inet->inet_sport;
1174 1185
1175 final_p = fl6_update_dst(&fl6, opt, &final); 1186 final_p = fl6_update_dst(&fl6, opt, &final);
1176 if (final_p) 1187 if (final_p)
1177 connected = 0; 1188 connected = 0;
1178 1189
1179 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) { 1190 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1180 fl6.flowi6_oif = np->mcast_oif; 1191 fl6.flowi6_oif = np->mcast_oif;
1181 connected = 0; 1192 connected = 0;
1182 } else if (!fl6.flowi6_oif) 1193 } else if (!fl6.flowi6_oif)
1183 fl6.flowi6_oif = np->ucast_oif; 1194 fl6.flowi6_oif = np->ucast_oif;
1184 1195
1185 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 1196 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1186 1197
1187 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true); 1198 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
1188 if (IS_ERR(dst)) { 1199 if (IS_ERR(dst)) {
1189 err = PTR_ERR(dst); 1200 err = PTR_ERR(dst);
1190 dst = NULL; 1201 dst = NULL;
1191 goto out; 1202 goto out;
1192 } 1203 }
1193 1204
1194 if (hlimit < 0) { 1205 if (hlimit < 0) {
1195 if (ipv6_addr_is_multicast(&fl6.daddr)) 1206 if (ipv6_addr_is_multicast(&fl6.daddr))
1196 hlimit = np->mcast_hops; 1207 hlimit = np->mcast_hops;
1197 else 1208 else
1198 hlimit = np->hop_limit; 1209 hlimit = np->hop_limit;
1199 if (hlimit < 0) 1210 if (hlimit < 0)
1200 hlimit = ip6_dst_hoplimit(dst); 1211 hlimit = ip6_dst_hoplimit(dst);
1201 } 1212 }
1202 1213
1203 if (tclass < 0) 1214 if (tclass < 0)
1204 tclass = np->tclass; 1215 tclass = np->tclass;
1205 1216
1206 if (dontfrag < 0) 1217 if (dontfrag < 0)
1207 dontfrag = np->dontfrag; 1218 dontfrag = np->dontfrag;
1208 1219
1209 if (msg->msg_flags&MSG_CONFIRM) 1220 if (msg->msg_flags&MSG_CONFIRM)
1210 goto do_confirm; 1221 goto do_confirm;
1211 back_from_confirm: 1222 back_from_confirm:
1212 1223
1213 lock_sock(sk); 1224 lock_sock(sk);
1214 if (unlikely(up->pending)) { 1225 if (unlikely(up->pending)) {
1215 /* The socket is already corked while preparing it. */ 1226 /* The socket is already corked while preparing it. */
1216 /* ... which is an evident application bug. --ANK */ 1227 /* ... which is an evident application bug. --ANK */
1217 release_sock(sk); 1228 release_sock(sk);
1218 1229
1219 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); 1230 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1220 err = -EINVAL; 1231 err = -EINVAL;
1221 goto out; 1232 goto out;
1222 } 1233 }
1223 1234
1224 up->pending = AF_INET6; 1235 up->pending = AF_INET6;
1225 1236
1226 do_append_data: 1237 do_append_data:
1227 up->len += ulen; 1238 up->len += ulen;
1228 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 1239 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1229 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen, 1240 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
1230 sizeof(struct udphdr), hlimit, tclass, opt, &fl6, 1241 sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1231 (struct rt6_info*)dst, 1242 (struct rt6_info*)dst,
1232 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag); 1243 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1233 if (err) 1244 if (err)
1234 udp_v6_flush_pending_frames(sk); 1245 udp_v6_flush_pending_frames(sk);
1235 else if (!corkreq) 1246 else if (!corkreq)
1236 err = udp_v6_push_pending_frames(sk); 1247 err = udp_v6_push_pending_frames(sk);
1237 else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) 1248 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1238 up->pending = 0; 1249 up->pending = 0;
1239 1250
1240 if (dst) { 1251 if (dst) {
1241 if (connected) { 1252 if (connected) {
1242 ip6_dst_store(sk, dst, 1253 ip6_dst_store(sk, dst,
1243 ipv6_addr_equal(&fl6.daddr, &np->daddr) ? 1254 ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1244 &np->daddr : NULL, 1255 &np->daddr : NULL,
1245 #ifdef CONFIG_IPV6_SUBTREES 1256 #ifdef CONFIG_IPV6_SUBTREES
1246 ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 1257 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1247 &np->saddr : 1258 &np->saddr :
1248 #endif 1259 #endif
1249 NULL); 1260 NULL);
1250 } else { 1261 } else {
1251 dst_release(dst); 1262 dst_release(dst);
1252 } 1263 }
1253 dst = NULL; 1264 dst = NULL;
1254 } 1265 }
1255 1266
1256 if (err > 0) 1267 if (err > 0)
1257 err = np->recverr ? net_xmit_errno(err) : 0; 1268 err = np->recverr ? net_xmit_errno(err) : 0;
1258 release_sock(sk); 1269 release_sock(sk);
1259 out: 1270 out:
1260 dst_release(dst); 1271 dst_release(dst);
1261 fl6_sock_release(flowlabel); 1272 fl6_sock_release(flowlabel);
1262 if (!err) 1273 if (!err)
1263 return len; 1274 return len;
1264 /* 1275 /*
1265 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting 1276 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1266 * ENOBUFS might not be good (it's not tunable per se), but otherwise 1277 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1267 * we don't have a good statistic (IpOutDiscards but it can be too many 1278 * we don't have a good statistic (IpOutDiscards but it can be too many
1268 * things). We could add another new stat but at least for now that 1279 * things). We could add another new stat but at least for now that
1269 * seems like overkill. 1280 * seems like overkill.
1270 */ 1281 */
1271 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 1282 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1272 UDP6_INC_STATS_USER(sock_net(sk), 1283 UDP6_INC_STATS_USER(sock_net(sk),
1273 UDP_MIB_SNDBUFERRORS, is_udplite); 1284 UDP_MIB_SNDBUFERRORS, is_udplite);
1274 } 1285 }
1275 return err; 1286 return err;
1276 1287
1277 do_confirm: 1288 do_confirm:
1278 dst_confirm(dst); 1289 dst_confirm(dst);
1279 if (!(msg->msg_flags&MSG_PROBE) || len) 1290 if (!(msg->msg_flags&MSG_PROBE) || len)
1280 goto back_from_confirm; 1291 goto back_from_confirm;
1281 err = 0; 1292 err = 0;
1282 goto out; 1293 goto out;
1283 } 1294 }
1284 1295
1285 void udpv6_destroy_sock(struct sock *sk) 1296 void udpv6_destroy_sock(struct sock *sk)
1286 { 1297 {
1287 lock_sock(sk); 1298 lock_sock(sk);
1288 udp_v6_flush_pending_frames(sk); 1299 udp_v6_flush_pending_frames(sk);
1289 release_sock(sk); 1300 release_sock(sk);
1290 1301
1291 inet6_destroy_sock(sk); 1302 inet6_destroy_sock(sk);
1292 } 1303 }
1293 1304
1294 /* 1305 /*
1295 * Socket option code for UDP 1306 * Socket option code for UDP
1296 */ 1307 */
1297 int udpv6_setsockopt(struct sock *sk, int level, int optname, 1308 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1298 char __user *optval, unsigned int optlen) 1309 char __user *optval, unsigned int optlen)
1299 { 1310 {
1300 if (level == SOL_UDP || level == SOL_UDPLITE) 1311 if (level == SOL_UDP || level == SOL_UDPLITE)
1301 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1312 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1302 udp_v6_push_pending_frames); 1313 udp_v6_push_pending_frames);
1303 return ipv6_setsockopt(sk, level, optname, optval, optlen); 1314 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1304 } 1315 }
1305 1316
1306 #ifdef CONFIG_COMPAT 1317 #ifdef CONFIG_COMPAT
1307 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 1318 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1308 char __user *optval, unsigned int optlen) 1319 char __user *optval, unsigned int optlen)
1309 { 1320 {
1310 if (level == SOL_UDP || level == SOL_UDPLITE) 1321 if (level == SOL_UDP || level == SOL_UDPLITE)
1311 return udp_lib_setsockopt(sk, level, optname, optval, optlen, 1322 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1312 udp_v6_push_pending_frames); 1323 udp_v6_push_pending_frames);
1313 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen); 1324 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1314 } 1325 }
1315 #endif 1326 #endif
1316 1327
1317 int udpv6_getsockopt(struct sock *sk, int level, int optname, 1328 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1318 char __user *optval, int __user *optlen) 1329 char __user *optval, int __user *optlen)
1319 { 1330 {
1320 if (level == SOL_UDP || level == SOL_UDPLITE) 1331 if (level == SOL_UDP || level == SOL_UDPLITE)
1321 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1332 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1322 return ipv6_getsockopt(sk, level, optname, optval, optlen); 1333 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1323 } 1334 }
1324 1335
1325 #ifdef CONFIG_COMPAT 1336 #ifdef CONFIG_COMPAT
1326 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 1337 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1327 char __user *optval, int __user *optlen) 1338 char __user *optval, int __user *optlen)
1328 { 1339 {
1329 if (level == SOL_UDP || level == SOL_UDPLITE) 1340 if (level == SOL_UDP || level == SOL_UDPLITE)
1330 return udp_lib_getsockopt(sk, level, optname, optval, optlen); 1341 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1331 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen); 1342 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1332 } 1343 }
1333 #endif 1344 #endif
1334 1345
1335 static int udp6_ufo_send_check(struct sk_buff *skb) 1346 static int udp6_ufo_send_check(struct sk_buff *skb)
1336 { 1347 {
1337 const struct ipv6hdr *ipv6h; 1348 const struct ipv6hdr *ipv6h;
1338 struct udphdr *uh; 1349 struct udphdr *uh;
1339 1350
1340 if (!pskb_may_pull(skb, sizeof(*uh))) 1351 if (!pskb_may_pull(skb, sizeof(*uh)))
1341 return -EINVAL; 1352 return -EINVAL;
1342 1353
1343 ipv6h = ipv6_hdr(skb); 1354 ipv6h = ipv6_hdr(skb);
1344 uh = udp_hdr(skb); 1355 uh = udp_hdr(skb);
1345 1356
1346 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len, 1357 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1347 IPPROTO_UDP, 0); 1358 IPPROTO_UDP, 0);
1348 skb->csum_start = skb_transport_header(skb) - skb->head; 1359 skb->csum_start = skb_transport_header(skb) - skb->head;
1349 skb->csum_offset = offsetof(struct udphdr, check); 1360 skb->csum_offset = offsetof(struct udphdr, check);
1350 skb->ip_summed = CHECKSUM_PARTIAL; 1361 skb->ip_summed = CHECKSUM_PARTIAL;
1351 return 0; 1362 return 0;
1352 } 1363 }
1353 1364
1354 static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, 1365 static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
1355 netdev_features_t features) 1366 netdev_features_t features)
1356 { 1367 {
1357 struct sk_buff *segs = ERR_PTR(-EINVAL); 1368 struct sk_buff *segs = ERR_PTR(-EINVAL);
1358 unsigned int mss; 1369 unsigned int mss;
1359 unsigned int unfrag_ip6hlen, unfrag_len; 1370 unsigned int unfrag_ip6hlen, unfrag_len;
1360 struct frag_hdr *fptr; 1371 struct frag_hdr *fptr;
1361 u8 *mac_start, *prevhdr; 1372 u8 *mac_start, *prevhdr;
1362 u8 nexthdr; 1373 u8 nexthdr;
1363 u8 frag_hdr_sz = sizeof(struct frag_hdr); 1374 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1364 int offset; 1375 int offset;
1365 __wsum csum; 1376 __wsum csum;
1366 1377
1367 mss = skb_shinfo(skb)->gso_size; 1378 mss = skb_shinfo(skb)->gso_size;
1368 if (unlikely(skb->len <= mss)) 1379 if (unlikely(skb->len <= mss))
1369 goto out; 1380 goto out;
1370 1381
1371 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { 1382 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1372 /* Packet is from an untrusted source, reset gso_segs. */ 1383 /* Packet is from an untrusted source, reset gso_segs. */
1373 int type = skb_shinfo(skb)->gso_type; 1384 int type = skb_shinfo(skb)->gso_type;
1374 1385
1375 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) || 1386 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1376 !(type & (SKB_GSO_UDP)))) 1387 !(type & (SKB_GSO_UDP))))
1377 goto out; 1388 goto out;
1378 1389
1379 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); 1390 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1380 1391
1381 segs = NULL; 1392 segs = NULL;
1382 goto out; 1393 goto out;
1383 } 1394 }
1384 1395
1385 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot 1396 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1386 * do checksum of UDP packets sent as multiple IP fragments. 1397 * do checksum of UDP packets sent as multiple IP fragments.
1387 */ 1398 */
1388 offset = skb_checksum_start_offset(skb); 1399 offset = skb_checksum_start_offset(skb);
1389 csum = skb_checksum(skb, offset, skb->len - offset, 0); 1400 csum = skb_checksum(skb, offset, skb->len - offset, 0);
1390 offset += skb->csum_offset; 1401 offset += skb->csum_offset;
1391 *(__sum16 *)(skb->data + offset) = csum_fold(csum); 1402 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1392 skb->ip_summed = CHECKSUM_NONE; 1403 skb->ip_summed = CHECKSUM_NONE;
1393 1404
1394 /* Check if there is enough headroom to insert fragment header. */ 1405 /* Check if there is enough headroom to insert fragment header. */
1395 if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) && 1406 if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) &&
1396 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC)) 1407 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1397 goto out; 1408 goto out;
1398 1409
1399 /* Find the unfragmentable header and shift it left by frag_hdr_sz 1410 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1400 * bytes to insert fragment header. 1411 * bytes to insert fragment header.
1401 */ 1412 */
1402 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 1413 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1403 nexthdr = *prevhdr; 1414 nexthdr = *prevhdr;
1404 *prevhdr = NEXTHDR_FRAGMENT; 1415 *prevhdr = NEXTHDR_FRAGMENT;
1405 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) + 1416 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1406 unfrag_ip6hlen; 1417 unfrag_ip6hlen;
1407 mac_start = skb_mac_header(skb); 1418 mac_start = skb_mac_header(skb);
1408 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len); 1419 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1409 1420
1410 skb->mac_header -= frag_hdr_sz; 1421 skb->mac_header -= frag_hdr_sz;
1411 skb->network_header -= frag_hdr_sz; 1422 skb->network_header -= frag_hdr_sz;
1412 1423
1413 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen); 1424 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1414 fptr->nexthdr = nexthdr; 1425 fptr->nexthdr = nexthdr;
1415 fptr->reserved = 0; 1426 fptr->reserved = 0;
1416 ipv6_select_ident(fptr, (struct rt6_info *)skb_dst(skb)); 1427 ipv6_select_ident(fptr, (struct rt6_info *)skb_dst(skb));
1417 1428
1418 /* Fragment the skb. ipv6 header and the remaining fields of the 1429 /* Fragment the skb. ipv6 header and the remaining fields of the
1419 * fragment header are updated in ipv6_gso_segment() 1430 * fragment header are updated in ipv6_gso_segment()
1420 */ 1431 */
1421 segs = skb_segment(skb, features); 1432 segs = skb_segment(skb, features);
1422 1433
1423 out: 1434 out:
1424 return segs; 1435 return segs;
1425 } 1436 }
1426 1437
1427 static const struct inet6_protocol udpv6_protocol = { 1438 static const struct inet6_protocol udpv6_protocol = {
1428 .handler = udpv6_rcv, 1439 .handler = udpv6_rcv,
1429 .err_handler = udpv6_err, 1440 .err_handler = udpv6_err,
1430 .gso_send_check = udp6_ufo_send_check, 1441 .gso_send_check = udp6_ufo_send_check,
1431 .gso_segment = udp6_ufo_fragment, 1442 .gso_segment = udp6_ufo_fragment,
1432 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, 1443 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1433 }; 1444 };
1434 1445
1435 /* ------------------------------------------------------------------------ */ 1446 /* ------------------------------------------------------------------------ */
1436 #ifdef CONFIG_PROC_FS 1447 #ifdef CONFIG_PROC_FS
1437 1448
1438 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket) 1449 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1439 { 1450 {
1440 struct inet_sock *inet = inet_sk(sp); 1451 struct inet_sock *inet = inet_sk(sp);
1441 struct ipv6_pinfo *np = inet6_sk(sp); 1452 struct ipv6_pinfo *np = inet6_sk(sp);
1442 const struct in6_addr *dest, *src; 1453 const struct in6_addr *dest, *src;
1443 __u16 destp, srcp; 1454 __u16 destp, srcp;
1444 1455
1445 dest = &np->daddr; 1456 dest = &np->daddr;
1446 src = &np->rcv_saddr; 1457 src = &np->rcv_saddr;
1447 destp = ntohs(inet->inet_dport); 1458 destp = ntohs(inet->inet_dport);
1448 srcp = ntohs(inet->inet_sport); 1459 srcp = ntohs(inet->inet_sport);
1449 seq_printf(seq, 1460 seq_printf(seq,
1450 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 1461 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1451 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n", 1462 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1452 bucket, 1463 bucket,
1453 src->s6_addr32[0], src->s6_addr32[1], 1464 src->s6_addr32[0], src->s6_addr32[1],
1454 src->s6_addr32[2], src->s6_addr32[3], srcp, 1465 src->s6_addr32[2], src->s6_addr32[3], srcp,
1455 dest->s6_addr32[0], dest->s6_addr32[1], 1466 dest->s6_addr32[0], dest->s6_addr32[1],
1456 dest->s6_addr32[2], dest->s6_addr32[3], destp, 1467 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1457 sp->sk_state, 1468 sp->sk_state,
1458 sk_wmem_alloc_get(sp), 1469 sk_wmem_alloc_get(sp),
1459 sk_rmem_alloc_get(sp), 1470 sk_rmem_alloc_get(sp),
1460 0, 0L, 0, 1471 0, 0L, 0,
1461 sock_i_uid(sp), 0, 1472 sock_i_uid(sp), 0,
1462 sock_i_ino(sp), 1473 sock_i_ino(sp),
1463 atomic_read(&sp->sk_refcnt), sp, 1474 atomic_read(&sp->sk_refcnt), sp,
1464 atomic_read(&sp->sk_drops)); 1475 atomic_read(&sp->sk_drops));
1465 } 1476 }
1466 1477
1467 int udp6_seq_show(struct seq_file *seq, void *v) 1478 int udp6_seq_show(struct seq_file *seq, void *v)
1468 { 1479 {
1469 if (v == SEQ_START_TOKEN) 1480 if (v == SEQ_START_TOKEN)
1470 seq_printf(seq, 1481 seq_printf(seq,
1471 " sl " 1482 " sl "
1472 "local_address " 1483 "local_address "
1473 "remote_address " 1484 "remote_address "
1474 "st tx_queue rx_queue tr tm->when retrnsmt" 1485 "st tx_queue rx_queue tr tm->when retrnsmt"
1475 " uid timeout inode ref pointer drops\n"); 1486 " uid timeout inode ref pointer drops\n");
1476 else 1487 else
1477 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket); 1488 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1478 return 0; 1489 return 0;
1479 } 1490 }
1480 1491
1481 static const struct file_operations udp6_afinfo_seq_fops = { 1492 static const struct file_operations udp6_afinfo_seq_fops = {
1482 .owner = THIS_MODULE, 1493 .owner = THIS_MODULE,
1483 .open = udp_seq_open, 1494 .open = udp_seq_open,
1484 .read = seq_read, 1495 .read = seq_read,
1485 .llseek = seq_lseek, 1496 .llseek = seq_lseek,
1486 .release = seq_release_net 1497 .release = seq_release_net
1487 }; 1498 };
1488 1499
1489 static struct udp_seq_afinfo udp6_seq_afinfo = { 1500 static struct udp_seq_afinfo udp6_seq_afinfo = {
1490 .name = "udp6", 1501 .name = "udp6",
1491 .family = AF_INET6, 1502 .family = AF_INET6,
1492 .udp_table = &udp_table, 1503 .udp_table = &udp_table,
1493 .seq_fops = &udp6_afinfo_seq_fops, 1504 .seq_fops = &udp6_afinfo_seq_fops,
1494 .seq_ops = { 1505 .seq_ops = {
1495 .show = udp6_seq_show, 1506 .show = udp6_seq_show,
1496 }, 1507 },
1497 }; 1508 };
1498 1509
1499 int __net_init udp6_proc_init(struct net *net) 1510 int __net_init udp6_proc_init(struct net *net)
1500 { 1511 {
1501 return udp_proc_register(net, &udp6_seq_afinfo); 1512 return udp_proc_register(net, &udp6_seq_afinfo);
1502 } 1513 }
1503 1514
1504 void udp6_proc_exit(struct net *net) { 1515 void udp6_proc_exit(struct net *net) {
1505 udp_proc_unregister(net, &udp6_seq_afinfo); 1516 udp_proc_unregister(net, &udp6_seq_afinfo);
1506 } 1517 }
1507 #endif /* CONFIG_PROC_FS */ 1518 #endif /* CONFIG_PROC_FS */
1508 1519
1509 /* ------------------------------------------------------------------------ */ 1520 /* ------------------------------------------------------------------------ */
1510 1521
1511 struct proto udpv6_prot = { 1522 struct proto udpv6_prot = {
1512 .name = "UDPv6", 1523 .name = "UDPv6",
1513 .owner = THIS_MODULE, 1524 .owner = THIS_MODULE,
1514 .close = udp_lib_close, 1525 .close = udp_lib_close,
1515 .connect = ip6_datagram_connect, 1526 .connect = ip6_datagram_connect,
1516 .disconnect = udp_disconnect, 1527 .disconnect = udp_disconnect,
1517 .ioctl = udp_ioctl, 1528 .ioctl = udp_ioctl,
1518 .destroy = udpv6_destroy_sock, 1529 .destroy = udpv6_destroy_sock,
1519 .setsockopt = udpv6_setsockopt, 1530 .setsockopt = udpv6_setsockopt,
1520 .getsockopt = udpv6_getsockopt, 1531 .getsockopt = udpv6_getsockopt,
1521 .sendmsg = udpv6_sendmsg, 1532 .sendmsg = udpv6_sendmsg,
1522 .recvmsg = udpv6_recvmsg, 1533 .recvmsg = udpv6_recvmsg,
1523 .backlog_rcv = __udpv6_queue_rcv_skb, 1534 .backlog_rcv = __udpv6_queue_rcv_skb,
1524 .hash = udp_lib_hash, 1535 .hash = udp_lib_hash,
1525 .unhash = udp_lib_unhash, 1536 .unhash = udp_lib_unhash,
1526 .rehash = udp_v6_rehash, 1537 .rehash = udp_v6_rehash,
1527 .get_port = udp_v6_get_port, 1538 .get_port = udp_v6_get_port,
1528 .memory_allocated = &udp_memory_allocated, 1539 .memory_allocated = &udp_memory_allocated,
1529 .sysctl_mem = sysctl_udp_mem, 1540 .sysctl_mem = sysctl_udp_mem,
1530 .sysctl_wmem = &sysctl_udp_wmem_min, 1541 .sysctl_wmem = &sysctl_udp_wmem_min,
1531 .sysctl_rmem = &sysctl_udp_rmem_min, 1542 .sysctl_rmem = &sysctl_udp_rmem_min,
1532 .obj_size = sizeof(struct udp6_sock), 1543 .obj_size = sizeof(struct udp6_sock),
1533 .slab_flags = SLAB_DESTROY_BY_RCU, 1544 .slab_flags = SLAB_DESTROY_BY_RCU,
1534 .h.udp_table = &udp_table, 1545 .h.udp_table = &udp_table,
1535 #ifdef CONFIG_COMPAT 1546 #ifdef CONFIG_COMPAT
1536 .compat_setsockopt = compat_udpv6_setsockopt, 1547 .compat_setsockopt = compat_udpv6_setsockopt,
1537 .compat_getsockopt = compat_udpv6_getsockopt, 1548 .compat_getsockopt = compat_udpv6_getsockopt,
1538 #endif 1549 #endif
1539 .clear_sk = sk_prot_clear_portaddr_nulls, 1550 .clear_sk = sk_prot_clear_portaddr_nulls,
1540 }; 1551 };
1541 1552
1542 static struct inet_protosw udpv6_protosw = { 1553 static struct inet_protosw udpv6_protosw = {
1543 .type = SOCK_DGRAM, 1554 .type = SOCK_DGRAM,
1544 .protocol = IPPROTO_UDP, 1555 .protocol = IPPROTO_UDP,
1545 .prot = &udpv6_prot, 1556 .prot = &udpv6_prot,
1546 .ops = &inet6_dgram_ops, 1557 .ops = &inet6_dgram_ops,
1547 .no_check = UDP_CSUM_DEFAULT, 1558 .no_check = UDP_CSUM_DEFAULT,
1548 .flags = INET_PROTOSW_PERMANENT, 1559 .flags = INET_PROTOSW_PERMANENT,
1549 }; 1560 };
1550 1561
1551 1562
1552 int __init udpv6_init(void) 1563 int __init udpv6_init(void)
1553 { 1564 {
1554 int ret; 1565 int ret;
1555 1566
1556 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP); 1567 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1557 if (ret) 1568 if (ret)
1558 goto out; 1569 goto out;
1559 1570
1560 ret = inet6_register_protosw(&udpv6_protosw); 1571 ret = inet6_register_protosw(&udpv6_protosw);
1561 if (ret) 1572 if (ret)
1562 goto out_udpv6_protocol; 1573 goto out_udpv6_protocol;
1563 out: 1574 out:
1564 return ret; 1575 return ret;
1565 1576
1566 out_udpv6_protocol: 1577 out_udpv6_protocol:
1567 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1578 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1568 goto out; 1579 goto out;
1569 } 1580 }
1570 1581
1571 void udpv6_exit(void) 1582 void udpv6_exit(void)
1572 { 1583 {
1573 inet6_unregister_protosw(&udpv6_protosw); 1584 inet6_unregister_protosw(&udpv6_protosw);
1574 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP); 1585 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1575 } 1586 }
1576 1587