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