]>
Commit | Line | Data |
---|---|---|
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 | * | |
10 | * Authors: Ross Biro | |
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 | * James Chapman : Add L2TP encapsulation type. | |
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 | */ | |
81 | ||
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> | |
90 | #include <linux/igmp.h> | |
91 | #include <linux/in.h> | |
92 | #include <linux/errno.h> | |
93 | #include <linux/timer.h> | |
94 | #include <linux/mm.h> | |
95 | #include <linux/inet.h> | |
96 | #include <linux/netdevice.h> | |
97 | #include <net/tcp_states.h> | |
98 | #include <linux/skbuff.h> | |
99 | #include <linux/proc_fs.h> | |
100 | #include <linux/seq_file.h> | |
101 | #include <net/icmp.h> | |
102 | #include <net/route.h> | |
103 | #include <net/checksum.h> | |
104 | #include <net/xfrm.h> | |
105 | #include "udp_impl.h" | |
106 | ||
107 | /* | |
108 | * Snmp MIB for the UDP layer | |
109 | */ | |
110 | ||
111 | DEFINE_SNMP_STAT(struct udp_mib, udp_statistics) __read_mostly; | |
112 | ||
113 | struct hlist_head udp_hash[UDP_HTABLE_SIZE]; | |
114 | DEFINE_RWLOCK(udp_hash_lock); | |
115 | ||
116 | static inline int __udp_lib_lport_inuse(__u16 num, | |
117 | const struct hlist_head udptable[]) | |
118 | { | |
119 | struct sock *sk; | |
120 | struct hlist_node *node; | |
121 | ||
122 | sk_for_each(sk, node, &udptable[num & (UDP_HTABLE_SIZE - 1)]) | |
123 | if (sk->sk_hash == num) | |
124 | return 1; | |
125 | return 0; | |
126 | } | |
127 | ||
128 | /** | |
129 | * __udp_lib_get_port - UDP/-Lite port lookup for IPv4 and IPv6 | |
130 | * | |
131 | * @sk: socket struct in question | |
132 | * @snum: port number to look up | |
133 | * @udptable: hash list table, must be of UDP_HTABLE_SIZE | |
134 | * @saddr_comp: AF-dependent comparison of bound local IP addresses | |
135 | */ | |
136 | int __udp_lib_get_port(struct sock *sk, unsigned short snum, | |
137 | struct hlist_head udptable[], | |
138 | int (*saddr_comp)(const struct sock *sk1, | |
139 | const struct sock *sk2 ) ) | |
140 | { | |
141 | struct hlist_node *node; | |
142 | struct hlist_head *head; | |
143 | struct sock *sk2; | |
144 | int error = 1; | |
145 | ||
146 | write_lock_bh(&udp_hash_lock); | |
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)) | |
163 | goto gotit; | |
164 | ||
165 | sk_for_each(sk2, node, head) { | |
166 | if (++size >= best_size_so_far) | |
167 | goto next; | |
168 | } | |
169 | best_size_so_far = size; | |
170 | best = rover; | |
171 | next: | |
172 | /* fold back if end of range */ | |
173 | if (++rover > high) | |
174 | rover = low + ((rover - low) | |
175 | & (UDP_HTABLE_SIZE - 1)); | |
176 | ||
177 | ||
178 | } | |
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)); | |
189 | } | |
190 | ||
191 | ||
192 | /* All ports in use! */ | |
193 | goto fail; | |
194 | ||
195 | gotit: | |
196 | snum = rover; | |
197 | } else { | |
198 | head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; | |
199 | ||
200 | sk_for_each(sk2, node, head) | |
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) ) | |
207 | goto fail; | |
208 | } | |
209 | ||
210 | inet_sk(sk)->num = snum; | |
211 | sk->sk_hash = snum; | |
212 | if (sk_unhashed(sk)) { | |
213 | head = &udptable[snum & (UDP_HTABLE_SIZE - 1)]; | |
214 | sk_add_node(sk, head); | |
215 | sock_prot_inc_use(sk->sk_prot); | |
216 | } | |
217 | error = 0; | |
218 | fail: | |
219 | write_unlock_bh(&udp_hash_lock); | |
220 | return error; | |
221 | } | |
222 | ||
223 | int udp_get_port(struct sock *sk, unsigned short snum, | |
224 | int (*scmp)(const struct sock *, const struct sock *)) | |
225 | { | |
226 | return __udp_lib_get_port(sk, snum, udp_hash, scmp); | |
227 | } | |
228 | ||
229 | int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2) | |
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 | { | |
240 | return udp_get_port(sk, snum, ipv4_rcv_saddr_equal); | |
241 | } | |
242 | ||
243 | /* UDP is nearly always wildcards out the wazoo, it makes no sense to try | |
244 | * harder than this. -DaveM | |
245 | */ | |
246 | static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport, | |
247 | __be32 daddr, __be16 dport, | |
248 | int dif, struct hlist_head udptable[]) | |
249 | { | |
250 | struct sock *sk, *result = NULL; | |
251 | struct hlist_node *node; | |
252 | unsigned short hnum = ntohs(dport); | |
253 | int badness = -1; | |
254 | ||
255 | read_lock(&udp_hash_lock); | |
256 | sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) { | |
257 | struct inet_sock *inet = inet_sk(sk); | |
258 | ||
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 | } | |
288 | } | |
289 | } | |
290 | if (result) | |
291 | sock_hold(result); | |
292 | read_unlock(&udp_hash_lock); | |
293 | return result; | |
294 | } | |
295 | ||
296 | static inline struct sock *udp_v4_mcast_next(struct sock *sk, | |
297 | __be16 loc_port, __be32 loc_addr, | |
298 | __be16 rmt_port, __be32 rmt_addr, | |
299 | int dif) | |
300 | { | |
301 | struct hlist_node *node; | |
302 | struct sock *s = sk; | |
303 | unsigned short hnum = ntohs(loc_port); | |
304 | ||
305 | sk_for_each_from(s, node) { | |
306 | struct inet_sock *inet = inet_sk(s); | |
307 | ||
308 | if (s->sk_hash != hnum || | |
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; | |
318 | } | |
319 | s = NULL; | |
320 | found: | |
321 | return s; | |
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 | |
328 | * it's just the icmp type << 8 | icmp code. | |
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 | ||
335 | void __udp4_lib_err(struct sk_buff *skb, u32 info, struct hlist_head udptable[]) | |
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)); | |
340 | const int type = icmp_hdr(skb)->type; | |
341 | const int code = icmp_hdr(skb)->code; | |
342 | struct sock *sk; | |
343 | int harderr; | |
344 | int err; | |
345 | ||
346 | sk = __udp4_lib_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, | |
347 | skb->dev->ifindex, udptable ); | |
348 | if (sk == NULL) { | |
349 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
350 | return; /* No socket for error */ | |
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 | /* | |
386 | * RFC1122: OK. Passes ICMP errors back to application, as per | |
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 | ||
401 | void udp_err(struct sk_buff *skb, u32 info) | |
402 | { | |
403 | return __udp4_lib_err(skb, info, udp_hash); | |
404 | } | |
405 | ||
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 | ||
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 | { | |
429 | unsigned int offset; | |
430 | struct udphdr *uh = udp_hdr(skb); | |
431 | __wsum csum = 0; | |
432 | ||
433 | if (skb_queue_len(&sk->sk_write_queue) == 1) { | |
434 | /* | |
435 | * Only one fragment on the socket. | |
436 | */ | |
437 | skb->csum_start = skb_transport_header(skb) - skb->head; | |
438 | skb->csum_offset = offsetof(struct udphdr, check); | |
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 | */ | |
446 | offset = skb_transport_offset(skb); | |
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) | |
457 | uh->check = CSUM_MANGLED_0; | |
458 | } | |
459 | } | |
460 | ||
461 | /* | |
462 | * Push out all pending data as one UDP datagram. Socket is locked. | |
463 | */ | |
464 | static int udp_push_pending_frames(struct sock *sk) | |
465 | { | |
466 | struct udp_sock *up = udp_sk(sk); | |
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; | |
472 | __wsum csum = 0; | |
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 | */ | |
481 | uh = udp_hdr(skb); | |
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 | ||
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 | ||
492 | skb->ip_summed = CHECKSUM_NONE; | |
493 | goto send; | |
494 | ||
495 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */ | |
496 | ||
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) | |
507 | uh->check = CSUM_MANGLED_0; | |
508 | ||
509 | send: | |
510 | err = ip_push_pending_frames(sk); | |
511 | out: | |
512 | up->len = 0; | |
513 | up->pending = 0; | |
514 | if (!err) | |
515 | UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, up->pcflag); | |
516 | return err; | |
517 | } | |
518 | ||
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; | |
529 | __be32 daddr, faddr, saddr; | |
530 | __be16 dport; | |
531 | u8 tos; | |
532 | int err, is_udplite = up->pcflag; | |
533 | int corkreq = up->corkflag || msg->msg_flags&MSG_MORE; | |
534 | int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); | |
535 | ||
536 | if (len > 0xFFFF) | |
537 | return -EMSGSIZE; | |
538 | ||
539 | /* | |
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. | |
551 | * The socket lock must be held while it's corked. | |
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 | } | |
559 | goto do_append_data; | |
560 | } | |
561 | release_sock(sk); | |
562 | } | |
563 | ulen += sizeof(struct udphdr); | |
564 | ||
565 | /* | |
566 | * Get and verify the address. | |
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; | |
590 | } | |
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) || | |
616 | (msg->msg_flags & MSG_DONTROUTE) || | |
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 } }, | |
639 | .proto = sk->sk_protocol, | |
640 | .uli_u = { .ports = | |
641 | { .sport = inet->sport, | |
642 | .dport = dport } } }; | |
643 | security_sk_classify_flow(sk, &fl); | |
644 | err = ip_route_output_flow(&rt, &fl, sk, 1); | |
645 | if (err) { | |
646 | if (err == -ENETUNREACH) | |
647 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); | |
648 | goto out; | |
649 | } | |
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 | ||
673 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n"); | |
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; | |
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, | |
691 | corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags); | |
692 | if (err) | |
693 | udp_flush_pending_frames(sk); | |
694 | else if (!corkreq) | |
695 | err = udp_push_pending_frames(sk); | |
696 | else if (unlikely(skb_queue_empty(&sk->sk_write_queue))) | |
697 | up->pending = 0; | |
698 | release_sock(sk); | |
699 | ||
700 | out: | |
701 | ip_rt_put(rt); | |
702 | if (free) | |
703 | kfree(ipc.opt); | |
704 | if (!err) | |
705 | return len; | |
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)) { | |
714 | UDP_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite); | |
715 | } | |
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 | ||
726 | int udp_sendpage(struct sock *sk, struct page *page, int offset, | |
727 | size_t size, int flags) | |
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 | ||
749 | LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n"); | |
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))) | |
766 | ret = udp_push_pending_frames(sk); | |
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 | */ | |
777 | ||
778 | int udp_ioctl(struct sock *sk, int cmd, unsigned long arg) | |
779 | { | |
780 | switch (cmd) { | |
781 | case SIOCOUTQ: | |
782 | { | |
783 | int amount = atomic_read(&sk->sk_wmem_alloc); | |
784 | return put_user(amount, (int __user *)arg); | |
785 | } | |
786 | ||
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); | |
802 | } | |
803 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
804 | return put_user(amount, (int __user *)arg); | |
805 | } | |
806 | ||
807 | default: | |
808 | return -ENOIOCTLCMD; | |
809 | } | |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
814 | /* | |
815 | * This should be easy, if there is something there we | |
816 | * return it, otherwise we block. | |
817 | */ | |
818 | ||
819 | int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |
820 | size_t len, int noblock, int flags, int *addr_len) | |
821 | { | |
822 | struct inet_sock *inet = inet_sk(sk); | |
823 | struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name; | |
824 | struct sk_buff *skb; | |
825 | unsigned int ulen, copied; | |
826 | int err; | |
827 | int is_udplite = IS_UDPLITE(sk); | |
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; | |
842 | ||
843 | ulen = skb->len - sizeof(struct udphdr); | |
844 | copied = len; | |
845 | if (copied > ulen) | |
846 | copied = ulen; | |
847 | else if (copied < ulen) | |
848 | msg->msg_flags |= MSG_TRUNC; | |
849 | ||
850 | /* | |
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. | |
854 | */ | |
855 | ||
856 | if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) { | |
857 | if (udp_lib_checksum_complete(skb)) | |
858 | goto csum_copy_err; | |
859 | } | |
860 | ||
861 | if (skb_csum_unnecessary(skb)) | |
862 | err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), | |
863 | msg->msg_iov, copied ); | |
864 | else { | |
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; | |
880 | sin->sin_port = udp_hdr(skb)->source; | |
881 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; | |
882 | memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
883 | } | |
884 | if (inet->cmsg_flags) | |
885 | ip_cmsg_recv(msg, skb); | |
886 | ||
887 | err = copied; | |
888 | if (flags & MSG_TRUNC) | |
889 | err = ulen; | |
890 | ||
891 | out_free: | |
892 | skb_free_datagram(sk, skb); | |
893 | out: | |
894 | return err; | |
895 | ||
896 | csum_copy_err: | |
897 | UDP_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite); | |
898 | ||
899 | skb_kill_datagram(sk, skb, flags); | |
900 | ||
901 | if (noblock) | |
902 | return -EAGAIN; | |
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 | */ | |
913 | ||
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 | ||
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 | */ | |
937 | int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb) | |
938 | { | |
939 | struct udp_sock *up = udp_sk(sk); | |
940 | int rc; | |
941 | ||
942 | /* | |
943 | * Charge it to the socket, dropping if the queue is full. | |
944 | */ | |
945 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) | |
946 | goto drop; | |
947 | nf_reset(skb); | |
948 | ||
949 | if (up->encap_type) { | |
950 | /* | |
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 | |
959 | */ | |
960 | ||
961 | /* if we're overly short, let UDP handle it */ | |
962 | if (skb->len > sizeof(struct udphdr) && | |
963 | up->encap_rcv != NULL) { | |
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; | |
970 | } | |
971 | } | |
972 | ||
973 | /* FALLTHROUGH -- it's a UDP Packet */ | |
974 | } | |
975 | ||
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; | |
997 | } | |
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 | ||
1012 | if (sk->sk_filter) { | |
1013 | if (udp_lib_checksum_complete(skb)) | |
1014 | goto drop; | |
1015 | } | |
1016 | ||
1017 | if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) { | |
1018 | /* Note that an ENOMEM error is charged twice */ | |
1019 | if (rc == -ENOMEM) | |
1020 | UDP_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, up->pcflag); | |
1021 | goto drop; | |
1022 | } | |
1023 | ||
1024 | UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS, up->pcflag); | |
1025 | return 0; | |
1026 | ||
1027 | drop: | |
1028 | UDP_INC_STATS_BH(UDP_MIB_INERRORS, up->pcflag); | |
1029 | kfree_skb(skb); | |
1030 | return -1; | |
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 | */ | |
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[]) | |
1043 | { | |
1044 | struct sock *sk; | |
1045 | int dif; | |
1046 | ||
1047 | read_lock(&udp_hash_lock); | |
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); | |
1051 | if (sk) { | |
1052 | struct sock *sknext = NULL; | |
1053 | ||
1054 | do { | |
1055 | struct sk_buff *skb1 = skb; | |
1056 | ||
1057 | sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr, | |
1058 | uh->source, saddr, dif); | |
1059 | if (sknext) | |
1060 | skb1 = skb_clone(skb, GFP_ATOMIC); | |
1061 | ||
1062 | if (skb1) { | |
1063 | int ret = udp_queue_rcv_skb(sk, skb1); | |
1064 | if (ret > 0) | |
1065 | /* we should probably re-process instead | |
1066 | * of dropping packets here. */ | |
1067 | kfree_skb(skb1); | |
1068 | } | |
1069 | sk = sknext; | |
1070 | } while (sknext); | |
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 | */ | |
1082 | static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh, | |
1083 | int proto) | |
1084 | { | |
1085 | const struct iphdr *iph; | |
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 | ||
1097 | iph = ip_hdr(skb); | |
1098 | if (uh->check == 0) { | |
1099 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1100 | } else if (skb->ip_summed == CHECKSUM_COMPLETE) { | |
1101 | if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len, | |
1102 | proto, skb->csum)) | |
1103 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1104 | } | |
1105 | if (!skb_csum_unnecessary(skb)) | |
1106 | skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, | |
1107 | skb->len, proto, 0); | |
1108 | /* Probably, we should checksum udp header (it should be in cache | |
1109 | * in any case) and data in tiny packets (< rx copybreak). | |
1110 | */ | |
1111 | ||
1112 | return 0; | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * All we need to do is get the socket, and then do a checksum. | |
1117 | */ | |
1118 | ||
1119 | int __udp4_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[], | |
1120 | int proto) | |
1121 | { | |
1122 | struct sock *sk; | |
1123 | struct udphdr *uh = udp_hdr(skb); | |
1124 | unsigned short ulen; | |
1125 | struct rtable *rt = (struct rtable*)skb->dst; | |
1126 | __be32 saddr = ip_hdr(skb)->saddr; | |
1127 | __be32 daddr = ip_hdr(skb)->daddr; | |
1128 | ||
1129 | /* | |
1130 | * Validate the packet. | |
1131 | */ | |
1132 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) | |
1133 | goto drop; /* No space for header. */ | |
1134 | ||
1135 | ulen = ntohs(uh->len); | |
1136 | if (ulen > skb->len) | |
1137 | goto short_packet; | |
1138 | ||
1139 | if (proto == IPPROTO_UDP) { | |
1140 | /* UDP validates ulen. */ | |
1141 | if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen)) | |
1142 | goto short_packet; | |
1143 | uh = udp_hdr(skb); | |
1144 | } | |
1145 | ||
1146 | if (udp4_csum_init(skb, uh, proto)) | |
1147 | goto csum_error; | |
1148 | ||
1149 | if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) | |
1150 | return __udp4_lib_mcast_deliver(skb, uh, saddr, daddr, udptable); | |
1151 | ||
1152 | sk = __udp4_lib_lookup(saddr, uh->source, daddr, uh->dest, | |
1153 | skb->dev->ifindex, udptable ); | |
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 | |
1160 | * it wants the return to be -protocol, or 0 | |
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; | |
1169 | nf_reset(skb); | |
1170 | ||
1171 | /* No socket. Drop packet silently, if checksum is wrong */ | |
1172 | if (udp_lib_checksum_complete(skb)) | |
1173 | goto csum_error; | |
1174 | ||
1175 | UDP_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE); | |
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); | |
1183 | return 0; | |
1184 | ||
1185 | short_packet: | |
1186 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n", | |
1187 | proto == IPPROTO_UDPLITE ? "-Lite" : "", | |
1188 | NIPQUAD(saddr), | |
1189 | ntohs(uh->source), | |
1190 | ulen, | |
1191 | skb->len, | |
1192 | NIPQUAD(daddr), | |
1193 | ntohs(uh->dest)); | |
1194 | goto drop; | |
1195 | ||
1196 | csum_error: | |
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). | |
1200 | */ | |
1201 | LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n", | |
1202 | proto == IPPROTO_UDPLITE ? "-Lite" : "", | |
1203 | NIPQUAD(saddr), | |
1204 | ntohs(uh->source), | |
1205 | NIPQUAD(daddr), | |
1206 | ntohs(uh->dest), | |
1207 | ulen); | |
1208 | drop: | |
1209 | UDP_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE); | |
1210 | kfree_skb(skb); | |
1211 | return 0; | |
1212 | } | |
1213 | ||
1214 | int udp_rcv(struct sk_buff *skb) | |
1215 | { | |
1216 | return __udp4_lib_rcv(skb, udp_hash, IPPROTO_UDP); | |
1217 | } | |
1218 | ||
1219 | int udp_destroy_sock(struct sock *sk) | |
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 | */ | |
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 *)) | |
1233 | { | |
1234 | struct udp_sock *up = udp_sk(sk); | |
1235 | int val; | |
1236 | int err = 0; | |
1237 | ||
1238 | if (optlen<sizeof(int)) | |
1239 | return -EINVAL; | |
1240 | ||
1241 | if (get_user(val, (int __user *)optval)) | |
1242 | return -EFAULT; | |
1243 | ||
1244 | switch (optname) { | |
1245 | case UDP_CORK: | |
1246 | if (val != 0) { | |
1247 | up->corkflag = 1; | |
1248 | } else { | |
1249 | up->corkflag = 0; | |
1250 | lock_sock(sk); | |
1251 | (*push_pending_frames)(sk); | |
1252 | release_sock(sk); | |
1253 | } | |
1254 | break; | |
1255 | ||
1256 | case UDP_ENCAP: | |
1257 | switch (val) { | |
1258 | case 0: | |
1259 | case UDP_ENCAP_ESPINUDP: | |
1260 | case UDP_ENCAP_ESPINUDP_NON_IKE: | |
1261 | up->encap_rcv = xfrm4_udp_encap_rcv; | |
1262 | /* FALLTHROUGH */ | |
1263 | case UDP_ENCAP_L2TPINUDP: | |
1264 | up->encap_type = val; | |
1265 | break; | |
1266 | default: | |
1267 | err = -ENOPROTOOPT; | |
1268 | break; | |
1269 | } | |
1270 | break; | |
1271 | ||
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 | ||
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 | |
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 | ||
1298 | default: | |
1299 | err = -ENOPROTOOPT; | |
1300 | break; | |
1301 | } | |
1302 | ||
1303 | return err; | |
1304 | } | |
1305 | ||
1306 | int udp_setsockopt(struct sock *sk, int level, int optname, | |
1307 | char __user *optval, int optlen) | |
1308 | { | |
1309 | if (level == SOL_UDP || level == SOL_UDPLITE) | |
1310 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, | |
1311 | udp_push_pending_frames); | |
1312 | return ip_setsockopt(sk, level, optname, optval, optlen); | |
1313 | } | |
1314 | ||
1315 | #ifdef CONFIG_COMPAT | |
1316 | int compat_udp_setsockopt(struct sock *sk, int level, int optname, | |
1317 | char __user *optval, int optlen) | |
1318 | { | |
1319 | if (level == SOL_UDP || level == SOL_UDPLITE) | |
1320 | return udp_lib_setsockopt(sk, level, optname, optval, optlen, | |
1321 | udp_push_pending_frames); | |
1322 | return compat_ip_setsockopt(sk, level, optname, optval, optlen); | |
1323 | } | |
1324 | #endif | |
1325 | ||
1326 | int udp_lib_getsockopt(struct sock *sk, int level, int optname, | |
1327 | char __user *optval, int __user *optlen) | |
1328 | { | |
1329 | struct udp_sock *up = udp_sk(sk); | |
1330 | int val, len; | |
1331 | ||
1332 | if (get_user(len,optlen)) | |
1333 | return -EFAULT; | |
1334 | ||
1335 | len = min_t(unsigned int, len, sizeof(int)); | |
1336 | ||
1337 | if (len < 0) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | switch (optname) { | |
1341 | case UDP_CORK: | |
1342 | val = up->corkflag; | |
1343 | break; | |
1344 | ||
1345 | case UDP_ENCAP: | |
1346 | val = up->encap_type; | |
1347 | break; | |
1348 | ||
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 | ||
1359 | default: | |
1360 | return -ENOPROTOOPT; | |
1361 | } | |
1362 | ||
1363 | if (put_user(len, optlen)) | |
1364 | return -EFAULT; | |
1365 | if (copy_to_user(optval, &val,len)) | |
1366 | return -EFAULT; | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | int udp_getsockopt(struct sock *sk, int level, int optname, | |
1371 | char __user *optval, int __user *optlen) | |
1372 | { | |
1373 | if (level == SOL_UDP || level == SOL_UDPLITE) | |
1374 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); | |
1375 | return ip_getsockopt(sk, level, optname, optval, optlen); | |
1376 | } | |
1377 | ||
1378 | #ifdef CONFIG_COMPAT | |
1379 | int compat_udp_getsockopt(struct sock *sk, int level, int optname, | |
1380 | char __user *optval, int __user *optlen) | |
1381 | { | |
1382 | if (level == SOL_UDP || level == SOL_UDPLITE) | |
1383 | return udp_lib_getsockopt(sk, level, optname, optval, optlen); | |
1384 | return compat_ip_getsockopt(sk, level, optname, optval, optlen); | |
1385 | } | |
1386 | #endif | |
1387 | /** | |
1388 | * udp_poll - wait for a UDP event. | |
1389 | * @file - file struct | |
1390 | * @sock - socket | |
1391 | * @wait - poll table | |
1392 | * | |
1393 | * This is same as datagram poll, except for the special case of | |
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; | |
1404 | int is_lite = IS_UDPLITE(sk); | |
1405 | ||
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 | ||
1413 | spin_lock_bh(&rcvq->lock); | |
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); | |
1419 | } | |
1420 | spin_unlock_bh(&rcvq->lock); | |
1421 | ||
1422 | /* nothing to see, move along */ | |
1423 | if (skb == NULL) | |
1424 | mask &= ~(POLLIN | POLLRDNORM); | |
1425 | } | |
1426 | ||
1427 | return mask; | |
1428 | ||
1429 | } | |
1430 | ||
1431 | struct proto udp_prot = { | |
1432 | .name = "UDP", | |
1433 | .owner = THIS_MODULE, | |
1434 | .close = udp_lib_close, | |
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, | |
1445 | .hash = udp_lib_hash, | |
1446 | .unhash = udp_lib_unhash, | |
1447 | .get_port = udp_v4_get_port, | |
1448 | .obj_size = sizeof(struct udp_sock), | |
1449 | #ifdef CONFIG_COMPAT | |
1450 | .compat_setsockopt = compat_udp_setsockopt, | |
1451 | .compat_getsockopt = compat_udp_getsockopt, | |
1452 | #endif | |
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; | |
1465 | sk_for_each(sk, node, state->hashtable + state->bucket) { | |
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) { | |
1486 | sk = sk_head(state->hashtable + state->bucket); | |
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) | |
1497 | while (pos && (sk = udp_get_next(seq, sk)) != NULL) | |
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; | |
1531 | struct udp_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL); | |
1532 | ||
1533 | if (!s) | |
1534 | goto out; | |
1535 | s->family = afinfo->family; | |
1536 | s->hashtable = afinfo->hashtable; | |
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); | |
1589 | __be32 dest = inet->daddr; | |
1590 | __be32 src = inet->rcv_saddr; | |
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", | |
1596 | bucket, src, srcp, dest, destp, sp->sk_state, | |
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 | ||
1603 | int udp4_seq_show(struct seq_file *seq, void *v) | |
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, | |
1626 | .hashtable = udp_hash, | |
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); | |
1646 | EXPORT_SYMBOL(udp_get_port); | |
1647 | EXPORT_SYMBOL(udp_prot); | |
1648 | EXPORT_SYMBOL(udp_sendmsg); | |
1649 | EXPORT_SYMBOL(udp_lib_getsockopt); | |
1650 | EXPORT_SYMBOL(udp_lib_setsockopt); | |
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 |