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