1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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>
89#include <linux/in.h>
90#include <linux/errno.h>
91#include <linux/timer.h>
92#include <linux/mm.h>
93#include <linux/config.h>
94#include <linux/inet.h>
95#include <linux/ipv6.h>
96#include <linux/netdevice.h>
97#include <net/snmp.h>
98#include <net/tcp.h>
99#include <net/protocol.h>
100#include <linux/skbuff.h>
101#include <linux/proc_fs.h>
102#include <linux/seq_file.h>
103#include <net/sock.h>
104#include <net/udp.h>
105#include <net/icmp.h>
106#include <net/route.h>
107#include <net/inet_common.h>
108#include <net/checksum.h>
109#include <net/xfrm.h>
110
111
112
113
114
115DEFINE_SNMP_STAT(struct udp_mib, udp_statistics);
116
117struct hlist_head udp_hash[UDP_HTABLE_SIZE];
118rwlock_t udp_hash_lock = RW_LOCK_UNLOCKED;
119
120
121int udp_port_rover;
122
123static int udp_v4_get_port(struct sock *sk, unsigned short snum)
124{
125 struct hlist_node *node;
126 struct sock *sk2;
127 struct inet_opt *inet = inet_sk(sk);
128
129 write_lock_bh(&udp_hash_lock);
130 if (snum == 0) {
131 int best_size_so_far, best, result, i;
132
133 if (udp_port_rover > sysctl_local_port_range[1] ||
134 udp_port_rover < sysctl_local_port_range[0])
135 udp_port_rover = sysctl_local_port_range[0];
136 best_size_so_far = 32767;
137 best = result = udp_port_rover;
138 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
139 struct hlist_head *list;
140 int size;
141
142 list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
143 if (hlist_empty(list)) {
144 if (result > sysctl_local_port_range[1])
145 result = sysctl_local_port_range[0] +
146 ((result - sysctl_local_port_range[0]) &
147 (UDP_HTABLE_SIZE - 1));
148 goto gotit;
149 }
150 size = 0;
151 sk_for_each(sk2, node, list)
152 if (++size >= best_size_so_far)
153 goto next;
154 best_size_so_far = size;
155 best = result;
156 next:;
157 }
158 result = best;
159 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
160 if (result > sysctl_local_port_range[1])
161 result = sysctl_local_port_range[0]
162 + ((result - sysctl_local_port_range[0]) &
163 (UDP_HTABLE_SIZE - 1));
164 if (!udp_lport_inuse(result))
165 break;
166 }
167 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
168 goto fail;
169gotit:
170 udp_port_rover = snum = result;
171 } else {
172 sk_for_each(sk2, node,
173 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
174 struct inet_opt *inet2 = inet_sk(sk2);
175
176 if (inet2->num == snum &&
177 sk2 != sk &&
178 !ipv6_only_sock(sk2) &&
179 (!sk2->sk_bound_dev_if ||
180 !sk->sk_bound_dev_if ||
181 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
182 (!inet2->rcv_saddr ||
183 !inet->rcv_saddr ||
184 inet2->rcv_saddr == inet->rcv_saddr) &&
185 (!sk2->sk_reuse || !sk->sk_reuse))
186 goto fail;
187 }
188 }
189 inet->num = snum;
190 if (sk_unhashed(sk)) {
191 struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
192
193 sk_add_node(sk, h);
194 sock_prot_inc_use(sk->sk_prot);
195 }
196 write_unlock_bh(&udp_hash_lock);
197 return 0;
198
199fail:
200 write_unlock_bh(&udp_hash_lock);
201 return 1;
202}
203
204static void udp_v4_hash(struct sock *sk)
205{
206 BUG();
207}
208
209static void udp_v4_unhash(struct sock *sk)
210{
211 write_lock_bh(&udp_hash_lock);
212 if (sk_del_node_init(sk)) {
213 inet_sk(sk)->num = 0;
214 sock_prot_dec_use(sk->sk_prot);
215 }
216 write_unlock_bh(&udp_hash_lock);
217}
218
219
220
221
222struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif)
223{
224 struct sock *sk, *result = NULL;
225 struct hlist_node *node;
226 unsigned short hnum = ntohs(dport);
227 int badness = -1;
228
229 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
230 struct inet_opt *inet = inet_sk(sk);
231
232 if (inet->num == hnum && !ipv6_only_sock(sk)) {
233 int score = (sk->sk_family == PF_INET ? 1 : 0);
234 if (inet->rcv_saddr) {
235 if (inet->rcv_saddr != daddr)
236 continue;
237 score+=2;
238 }
239 if (inet->daddr) {
240 if (inet->daddr != saddr)
241 continue;
242 score+=2;
243 }
244 if (inet->dport) {
245 if (inet->dport != sport)
246 continue;
247 score+=2;
248 }
249 if (sk->sk_bound_dev_if) {
250 if (sk->sk_bound_dev_if != dif)
251 continue;
252 score+=2;
253 }
254 if(score == 9) {
255 result = sk;
256 break;
257 } else if(score > badness) {
258 result = sk;
259 badness = score;
260 }
261 }
262 }
263 return result;
264}
265
266__inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif)
267{
268 struct sock *sk;
269
270 read_lock(&udp_hash_lock);
271 sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
272 if (sk)
273 sock_hold(sk);
274 read_unlock(&udp_hash_lock);
275 return sk;
276}
277
278static inline struct sock *udp_v4_mcast_next(struct sock *sk,
279 u16 loc_port, u32 loc_addr,
280 u16 rmt_port, u32 rmt_addr,
281 int dif)
282{
283 struct hlist_node *node;
284 struct sock *s = sk;
285 unsigned short hnum = ntohs(loc_port);
286
287 sk_for_each_from(s, node) {
288 struct inet_opt *inet = inet_sk(s);
289
290 if (inet->num != hnum ||
291 (inet->daddr && inet->daddr != rmt_addr) ||
292 (inet->dport != rmt_port && inet->dport) ||
293 (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
294 ipv6_only_sock(s) ||
295 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
296 continue;
297 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
298 continue;
299 goto found;
300 }
301 s = NULL;
302found:
303 return s;
304}
305
306
307
308
309
310
311
312
313
314
315
316
317void udp_err(struct sk_buff *skb, u32 info)
318{
319 struct inet_opt *inet;
320 struct iphdr *iph = (struct iphdr*)skb->data;
321 struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
322 int type = skb->h.icmph->type;
323 int code = skb->h.icmph->code;
324 struct sock *sk;
325 int harderr;
326 int err;
327
328 sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
329 if (sk == NULL) {
330 ICMP_INC_STATS_BH(IcmpInErrors);
331 return;
332 }
333
334 err = 0;
335 harderr = 0;
336 inet = inet_sk(sk);
337
338 switch (type) {
339 default:
340 case ICMP_TIME_EXCEEDED:
341 err = EHOSTUNREACH;
342 break;
343 case ICMP_SOURCE_QUENCH:
344 goto out;
345 case ICMP_PARAMETERPROB:
346 err = EPROTO;
347 harderr = 1;
348 break;
349 case ICMP_DEST_UNREACH:
350 if (code == ICMP_FRAG_NEEDED) {
351 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
352 err = EMSGSIZE;
353 harderr = 1;
354 break;
355 }
356 goto out;
357 }
358 err = EHOSTUNREACH;
359 if (code <= NR_ICMP_UNREACH) {
360 harderr = icmp_err_convert[code].fatal;
361 err = icmp_err_convert[code].errno;
362 }
363 break;
364 }
365
366
367
368
369
370 if (!inet->recverr) {
371 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
372 goto out;
373 } else {
374 ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
375 }
376 sk->sk_err = err;
377 sk->sk_error_report(sk);
378out:
379 sock_put(sk);
380}
381
382
383
384
385static void udp_flush_pending_frames(struct sock *sk)
386{
387 struct udp_opt *up = udp_sk(sk);
388
389 if (up->pending) {
390 up->len = 0;
391 up->pending = 0;
392 ip_flush_pending_frames(sk);
393 }
394}
395
396
397
398
399static int udp_push_pending_frames(struct sock *sk, struct udp_opt *up)
400{
401 struct inet_opt *inet = inet_sk(sk);
402 struct flowi *fl = &inet->cork.fl;
403 struct sk_buff *skb;
404 struct udphdr *uh;
405 int err = 0;
406
407
408 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
409 goto out;
410
411
412
413
414 uh = skb->h.uh;
415 uh->source = fl->fl_ip_sport;
416 uh->dest = fl->fl_ip_dport;
417 uh->len = htons(up->len);
418 uh->check = 0;
419
420 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
421 skb->ip_summed = CHECKSUM_NONE;
422 goto send;
423 }
424
425 if (skb_queue_len(&sk->sk_write_queue) == 1) {
426
427
428
429 if (skb->ip_summed == CHECKSUM_HW) {
430 skb->csum = offsetof(struct udphdr, check);
431 uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
432 up->len, IPPROTO_UDP, 0);
433 } else {
434 skb->csum = csum_partial((char *)uh,
435 sizeof(struct udphdr), skb->csum);
436 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
437 up->len, IPPROTO_UDP, skb->csum);
438 if (uh->check == 0)
439 uh->check = -1;
440 }
441 } else {
442 unsigned int csum = 0;
443
444
445
446
447
448 if (skb->ip_summed == CHECKSUM_HW) {
449 int offset = (unsigned char *)uh - skb->data;
450 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
451
452 skb->ip_summed = CHECKSUM_NONE;
453 } else {
454 skb->csum = csum_partial((char *)uh,
455 sizeof(struct udphdr), skb->csum);
456 }
457
458 skb_queue_walk(&sk->sk_write_queue, skb) {
459 csum = csum_add(csum, skb->csum);
460 }
461 uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
462 up->len, IPPROTO_UDP, csum);
463 if (uh->check == 0)
464 uh->check = -1;
465 }
466send:
467 err = ip_push_pending_frames(sk);
468out:
469 up->len = 0;
470 up->pending = 0;
471 return err;
472}
473
474
475static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
476{
477 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
478}
479
480int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
481 int len)
482{
483 struct inet_opt *inet = inet_sk(sk);
484 struct udp_opt *up = udp_sk(sk);
485 int ulen = len;
486 struct ipcm_cookie ipc;
487 struct rtable *rt = NULL;
488 int free = 0;
489 int connected = 0;
490 u32 daddr, faddr, saddr;
491 u16 dport;
492 u8 tos;
493 int err;
494 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
495
496
497
498
499
500
501
502
503
504
505
506
507 if (len < 0 || len > 0xFFFF)
508 return -EMSGSIZE;
509
510
511
512
513
514 if (msg->msg_flags&MSG_OOB)
515 return -EOPNOTSUPP;
516
517 ipc.opt = NULL;
518
519 if (up->pending) {
520
521
522
523
524 lock_sock(sk);
525 if (likely(up->pending)) {
526 if (unlikely(up->pending != AF_INET)) {
527 release_sock(sk);
528 return -EINVAL;
529 }
530 goto do_append_data;
531 }
532 release_sock(sk);
533 }
534 ulen += sizeof(struct udphdr);
535
536
537
538
539 if (msg->msg_name) {
540 struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
541 if (msg->msg_namelen < sizeof(*usin))
542 return -EINVAL;
543 if (usin->sin_family != AF_INET) {
544 if (usin->sin_family != AF_UNSPEC)
545 return -EINVAL;
546 }
547
548 daddr = usin->sin_addr.s_addr;
549 dport = usin->sin_port;
550 if (dport == 0)
551 return -EINVAL;
552 } else {
553 if (sk->sk_state != TCP_ESTABLISHED)
554 return -EDESTADDRREQ;
555 daddr = inet->daddr;
556 dport = inet->dport;
557
558
559
560 connected = 1;
561 }
562 ipc.addr = inet->saddr;
563
564 ipc.oif = sk->sk_bound_dev_if;
565 if (msg->msg_controllen) {
566 err = ip_cmsg_send(msg, &ipc);
567 if (err)
568 return err;
569 if (ipc.opt)
570 free = 1;
571 connected = 0;
572 }
573 if (!ipc.opt)
574 ipc.opt = inet->opt;
575
576 saddr = ipc.addr;
577 ipc.addr = faddr = daddr;
578
579 if (ipc.opt && ipc.opt->srr) {
580 if (!daddr)
581 return -EINVAL;
582 faddr = ipc.opt->faddr;
583 connected = 0;
584 }
585 tos = RT_TOS(inet->tos);
586 if (sk->sk_localroute || (msg->msg_flags & MSG_DONTROUTE) ||
587 (ipc.opt && ipc.opt->is_strictroute)) {
588 tos |= RTO_ONLINK;
589 connected = 0;
590 }
591
592 if (MULTICAST(daddr)) {
593 if (!ipc.oif)
594 ipc.oif = inet->mc_index;
595 if (!saddr)
596 saddr = inet->mc_addr;
597 connected = 0;
598 }
599
600 if (connected)
601 rt = (struct rtable*)sk_dst_check(sk, 0);
602
603 if (rt == NULL) {
604 struct flowi fl = { .oif = ipc.oif,
605 .nl_u = { .ip4_u =
606 { .daddr = faddr,
607 .saddr = saddr,
608 .tos = tos } },
609 .proto = IPPROTO_UDP,
610 .uli_u = { .ports =
611 { .sport = inet->sport,
612 .dport = dport } } };
613 err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
614 if (err)
615 goto out;
616
617 err = -EACCES;
618 if ((rt->rt_flags & RTCF_BROADCAST) &&
619 !sock_flag(sk, SOCK_BROADCAST))
620 goto out;
621 if (connected)
622 sk_dst_set(sk, dst_clone(&rt->u.dst));
623 }
624
625 if (msg->msg_flags&MSG_CONFIRM)
626 goto do_confirm;
627back_from_confirm:
628
629 saddr = rt->rt_src;
630 if (!ipc.addr)
631 daddr = ipc.addr = rt->rt_dst;
632
633 lock_sock(sk);
634 if (unlikely(up->pending)) {
635
636
637 release_sock(sk);
638
639 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp cork app bug 2\n"));
640 err = -EINVAL;
641 goto out;
642 }
643
644
645
646 inet->cork.fl.fl4_dst = daddr;
647 inet->cork.fl.fl_ip_dport = dport;
648 inet->cork.fl.fl4_src = saddr;
649 inet->cork.fl.fl_ip_sport = inet->sport;
650 up->pending = AF_INET;
651
652do_append_data:
653 up->len += ulen;
654 err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
655 sizeof(struct udphdr), &ipc, rt,
656 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
657 if (err)
658 udp_flush_pending_frames(sk);
659 else if (!corkreq)
660 err = udp_push_pending_frames(sk, up);
661 release_sock(sk);
662
663out:
664 ip_rt_put(rt);
665 if (free)
666 kfree(ipc.opt);
667 if (!err) {
668 UDP_INC_STATS_USER(UdpOutDatagrams);
669 return len;
670 }
671 return err;
672
673do_confirm:
674 dst_confirm(&rt->u.dst);
675 if (!(msg->msg_flags&MSG_PROBE) || len)
676 goto back_from_confirm;
677 err = 0;
678 goto out;
679}
680
681int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size, int flags)
682{
683 struct udp_opt *up = udp_sk(sk);
684 int ret;
685
686 if (!up->pending) {
687 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
688
689
690
691
692
693 ret = udp_sendmsg(NULL, sk, &msg, 0);
694 if (ret < 0)
695 return ret;
696 }
697
698 lock_sock(sk);
699
700 if (unlikely(!up->pending)) {
701 release_sock(sk);
702
703 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp cork app bug 3\n"));
704 return -EINVAL;
705 }
706
707 ret = ip_append_page(sk, page, offset, size, flags);
708 if (ret == -EOPNOTSUPP) {
709 release_sock(sk);
710 return sock_no_sendpage(sk->sk_socket, page, offset,
711 size, flags);
712 }
713 if (ret < 0) {
714 udp_flush_pending_frames(sk);
715 goto out;
716 }
717
718 up->len += size;
719 if (!(up->corkflag || (flags&MSG_MORE)))
720 ret = udp_push_pending_frames(sk, up);
721 if (!ret)
722 ret = size;
723out:
724 release_sock(sk);
725 return ret;
726}
727
728
729
730
731
732int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
733{
734 switch(cmd)
735 {
736 case SIOCOUTQ:
737 {
738 int amount = atomic_read(&sk->sk_wmem_alloc);
739 return put_user(amount, (int *)arg);
740 }
741
742 case SIOCINQ:
743 {
744 struct sk_buff *skb;
745 unsigned long amount;
746
747 amount = 0;
748 spin_lock_irq(&sk->sk_receive_queue.lock);
749 skb = skb_peek(&sk->sk_receive_queue);
750 if (skb != NULL) {
751
752
753
754
755
756 amount = skb->len - sizeof(struct udphdr);
757 }
758 spin_unlock_irq(&sk->sk_receive_queue.lock);
759 return put_user(amount, (int *)arg);
760 }
761
762 default:
763 return -ENOIOCTLCMD;
764 }
765 return(0);
766}
767
768static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
769{
770 return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
771}
772
773static __inline__ int udp_checksum_complete(struct sk_buff *skb)
774{
775 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
776 __udp_checksum_complete(skb);
777}
778
779
780
781
782
783
784int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
785 int len, int noblock, int flags, int *addr_len)
786{
787 struct inet_opt *inet = inet_sk(sk);
788 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
789 struct sk_buff *skb;
790 int copied, err;
791
792
793
794
795 if (addr_len)
796 *addr_len=sizeof(*sin);
797
798 if (flags & MSG_ERRQUEUE)
799 return ip_recv_error(sk, msg, len);
800
801 skb = skb_recv_datagram(sk, flags, noblock, &err);
802 if (!skb)
803 goto out;
804
805 copied = skb->len - sizeof(struct udphdr);
806 if (copied > len) {
807 copied = len;
808 msg->msg_flags |= MSG_TRUNC;
809 }
810
811 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
812 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
813 copied);
814 } else if (msg->msg_flags&MSG_TRUNC) {
815 if (__udp_checksum_complete(skb))
816 goto csum_copy_err;
817 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
818 copied);
819 } else {
820 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
821
822 if (err == -EINVAL)
823 goto csum_copy_err;
824 }
825
826 if (err)
827 goto out_free;
828
829 sock_recv_timestamp(msg, sk, skb);
830
831
832 if (sin)
833 {
834 sin->sin_family = AF_INET;
835 sin->sin_port = skb->h.uh->source;
836 sin->sin_addr.s_addr = skb->nh.iph->saddr;
837 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
838 }
839 if (inet->cmsg_flags)
840 ip_cmsg_recv(msg, skb);
841 err = copied;
842
843out_free:
844 skb_free_datagram(sk, skb);
845out:
846 return err;
847
848csum_copy_err:
849 UDP_INC_STATS_BH(UdpInErrors);
850
851
852 if (flags&MSG_PEEK) {
853 int clear = 0;
854 spin_lock_irq(&sk->sk_receive_queue.lock);
855 if (skb == skb_peek(&sk->sk_receive_queue)) {
856 __skb_unlink(skb, &sk->sk_receive_queue);
857 clear = 1;
858 }
859 spin_unlock_irq(&sk->sk_receive_queue.lock);
860 if (clear)
861 kfree_skb(skb);
862 }
863
864 skb_free_datagram(sk, skb);
865
866 return -EAGAIN;
867}
868
869int udp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
870{
871 struct inet_opt *inet = inet_sk(sk);
872 struct sockaddr_in *usin = (struct sockaddr_in *) uaddr;
873 struct rtable *rt;
874 u32 saddr;
875 int oif;
876 int err;
877
878
879 if (addr_len < sizeof(*usin))
880 return -EINVAL;
881
882 if (usin->sin_family != AF_INET)
883 return -EAFNOSUPPORT;
884
885 sk_dst_reset(sk);
886
887 oif = sk->sk_bound_dev_if;
888 saddr = inet->saddr;
889 if (MULTICAST(usin->sin_addr.s_addr)) {
890 if (!oif)
891 oif = inet->mc_index;
892 if (!saddr)
893 saddr = inet->mc_addr;
894 }
895 err = ip_route_connect(&rt, usin->sin_addr.s_addr, saddr,
896 RT_CONN_FLAGS(sk), oif,
897 IPPROTO_UDP,
898 inet->sport, usin->sin_port, sk);
899 if (err)
900 return err;
901 if ((rt->rt_flags & RTCF_BROADCAST) && !sock_flag(sk, SOCK_BROADCAST)) {
902 ip_rt_put(rt);
903 return -EACCES;
904 }
905 if (!inet->saddr)
906 inet->saddr = rt->rt_src;
907 if (!inet->rcv_saddr)
908 inet->rcv_saddr = rt->rt_src;
909 inet->daddr = rt->rt_dst;
910 inet->dport = usin->sin_port;
911 sk->sk_state = TCP_ESTABLISHED;
912 inet->id = jiffies;
913
914 sk_dst_set(sk, &rt->u.dst);
915 return(0);
916}
917
918int udp_disconnect(struct sock *sk, int flags)
919{
920 struct inet_opt *inet = inet_sk(sk);
921
922
923
924
925 sk->sk_state = TCP_CLOSE;
926 inet->daddr = 0;
927 inet->dport = 0;
928 sk->sk_bound_dev_if = 0;
929 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
930 inet_reset_saddr(sk);
931
932 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
933 sk->sk_prot->unhash(sk);
934 inet->sport = 0;
935 }
936 sk_dst_reset(sk);
937 return 0;
938}
939
940static void udp_close(struct sock *sk, long timeout)
941{
942 inet_sock_release(sk);
943}
944
945
946
947
948
949
950static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
951{
952#ifndef CONFIG_XFRM
953 return 1;
954#else
955 struct udp_opt *up = udp_sk(sk);
956 struct udphdr *uh = skb->h.uh;
957 struct iphdr *iph;
958 int iphlen, len;
959
960 __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
961 __u32 *udpdata32 = (__u32 *)udpdata;
962 __u16 encap_type = up->encap_type;
963
964
965 if (udpdata > skb->tail)
966 return 1;
967
968
969 if (!encap_type)
970 return 1;
971
972 len = skb->tail - udpdata;
973
974 switch (encap_type) {
975 case UDP_ENCAP_ESPINUDP:
976
977 if (len == 1 && udpdata[0] == 0xff) {
978 return 0;
979 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
980
981 len = sizeof(struct udphdr);
982 } else
983
984 return 1;
985
986
987
988
989
990
991
992
993 iph = skb->nh.iph;
994 iphlen = iph->ihl << 2;
995 iph->tot_len = htons(ntohs(iph->tot_len) - len);
996 if (skb->len < iphlen + len) {
997
998 return 0;
999 }
1000
1001
1002
1003
1004
1005 skb->h.raw = skb_pull(skb, len);
1006
1007
1008 iph->protocol = IPPROTO_ESP;
1009
1010
1011 return -1;
1012
1013 default:
1014 if (net_ratelimit())
1015 printk(KERN_INFO "udp_encap_rcv(): Unhandled UDP encap type: %u\n",
1016 encap_type);
1017 return 1;
1018 }
1019#endif
1020}
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
1031{
1032 struct udp_opt *up = udp_sk(sk);
1033
1034
1035
1036
1037 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1038 kfree_skb(skb);
1039 return -1;
1040 }
1041
1042 if (up->encap_type) {
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 int ret;
1053
1054 ret = udp_encap_rcv(sk, skb);
1055 if (ret == 0) {
1056
1057 kfree_skb(skb);
1058 return 0;
1059 }
1060 if (ret < 0) {
1061
1062 ret = xfrm4_rcv_encap(skb, up->encap_type);
1063 UDP_INC_STATS_BH(UdpInDatagrams);
1064 return -ret;
1065 }
1066
1067 }
1068
1069 if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
1070 if (__udp_checksum_complete(skb)) {
1071 UDP_INC_STATS_BH(UdpInErrors);
1072 kfree_skb(skb);
1073 return -1;
1074 }
1075 skb->ip_summed = CHECKSUM_UNNECESSARY;
1076 }
1077
1078 if (sock_queue_rcv_skb(sk,skb)<0) {
1079 UDP_INC_STATS_BH(UdpInErrors);
1080 kfree_skb(skb);
1081 return -1;
1082 }
1083 UDP_INC_STATS_BH(UdpInDatagrams);
1084 return 0;
1085}
1086
1087
1088
1089
1090
1091
1092
1093static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
1094 u32 saddr, u32 daddr)
1095{
1096 struct sock *sk;
1097 int dif;
1098
1099 read_lock(&udp_hash_lock);
1100 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
1101 dif = skb->dev->ifindex;
1102 sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
1103 if (sk) {
1104 struct sock *sknext = NULL;
1105
1106 do {
1107 struct sk_buff *skb1 = skb;
1108
1109 sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
1110 uh->source, saddr, dif);
1111 if(sknext)
1112 skb1 = skb_clone(skb, GFP_ATOMIC);
1113
1114 if(skb1) {
1115 int ret = udp_queue_rcv_skb(sk, skb1);
1116 if (ret > 0)
1117
1118
1119 kfree_skb(skb1);
1120 }
1121 sk = sknext;
1122 } while(sknext);
1123 } else
1124 kfree_skb(skb);
1125 read_unlock(&udp_hash_lock);
1126 return 0;
1127}
1128
1129
1130
1131
1132
1133
1134static int udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
1135 unsigned short ulen, u32 saddr, u32 daddr)
1136{
1137 if (uh->check == 0) {
1138 skb->ip_summed = CHECKSUM_UNNECESSARY;
1139 } else if (skb->ip_summed == CHECKSUM_HW) {
1140 skb->ip_summed = CHECKSUM_UNNECESSARY;
1141 if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
1142 return 0;
1143 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "udp v4 hw csum failure.\n"));
1144 skb->ip_summed = CHECKSUM_NONE;
1145 }
1146 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
1147 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
1148
1149
1150
1151 return 0;
1152}
1153
1154
1155
1156
1157
1158int udp_rcv(struct sk_buff *skb)
1159{
1160 struct sock *sk;
1161 struct udphdr *uh;
1162 unsigned short ulen;
1163 struct rtable *rt = (struct rtable*)skb->dst;
1164 u32 saddr = skb->nh.iph->saddr;
1165 u32 daddr = skb->nh.iph->daddr;
1166 int len = skb->len;
1167
1168
1169
1170
1171 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1172 goto no_header;
1173
1174 uh = skb->h.uh;
1175
1176 ulen = ntohs(uh->len);
1177
1178 if (ulen > len || ulen < sizeof(*uh))
1179 goto short_packet;
1180
1181 if (pskb_trim(skb, ulen))
1182 goto short_packet;
1183
1184 if (udp_checksum_init(skb, uh, ulen, saddr, daddr) < 0)
1185 goto csum_error;
1186
1187 if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1188 return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
1189
1190 sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
1191
1192 if (sk != NULL) {
1193 int ret = udp_queue_rcv_skb(sk, skb);
1194 sock_put(sk);
1195
1196
1197
1198
1199 if (ret > 0)
1200 return -ret;
1201 return 0;
1202 }
1203
1204 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1205 goto drop;
1206
1207
1208 if (udp_checksum_complete(skb))
1209 goto csum_error;
1210
1211 UDP_INC_STATS_BH(UdpNoPorts);
1212 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1213
1214
1215
1216
1217
1218 kfree_skb(skb);
1219 return(0);
1220
1221short_packet:
1222 NETDEBUG(if (net_ratelimit())
1223 printk(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
1224 NIPQUAD(saddr),
1225 ntohs(uh->source),
1226 ulen,
1227 len,
1228 NIPQUAD(daddr),
1229 ntohs(uh->dest)));
1230no_header:
1231 UDP_INC_STATS_BH(UdpInErrors);
1232 kfree_skb(skb);
1233 return(0);
1234
1235csum_error:
1236
1237
1238
1239
1240 NETDEBUG(if (net_ratelimit())
1241 printk(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
1242 NIPQUAD(saddr),
1243 ntohs(uh->source),
1244 NIPQUAD(daddr),
1245 ntohs(uh->dest),
1246 ulen));
1247drop:
1248 UDP_INC_STATS_BH(UdpInErrors);
1249 kfree_skb(skb);
1250 return(0);
1251}
1252
1253static int udp_destroy_sock(struct sock *sk)
1254{
1255 lock_sock(sk);
1256 udp_flush_pending_frames(sk);
1257 release_sock(sk);
1258 return 0;
1259}
1260
1261
1262
1263
1264static int udp_setsockopt(struct sock *sk, int level, int optname,
1265 char *optval, int optlen)
1266{
1267 struct udp_opt *up = udp_sk(sk);
1268 int val;
1269 int err = 0;
1270
1271 if (level != SOL_UDP)
1272 return ip_setsockopt(sk, level, optname, optval, optlen);
1273
1274 if(optlen<sizeof(int))
1275 return -EINVAL;
1276
1277 if (get_user(val, (int *)optval))
1278 return -EFAULT;
1279
1280 switch(optname) {
1281 case UDP_CORK:
1282 if (val != 0) {
1283 up->corkflag = 1;
1284 } else {
1285 up->corkflag = 0;
1286 lock_sock(sk);
1287 udp_push_pending_frames(sk, up);
1288 release_sock(sk);
1289 }
1290 break;
1291
1292 case UDP_ENCAP:
1293 up->encap_type = val;
1294 break;
1295
1296 default:
1297 err = -ENOPROTOOPT;
1298 break;
1299 };
1300
1301 return err;
1302}
1303
1304static int udp_getsockopt(struct sock *sk, int level, int optname,
1305 char *optval, int *optlen)
1306{
1307 struct udp_opt *up = udp_sk(sk);
1308 int val, len;
1309
1310 if (level != SOL_UDP)
1311 return ip_getsockopt(sk, level, optname, optval, optlen);
1312
1313 if(get_user(len,optlen))
1314 return -EFAULT;
1315
1316 len = min_t(unsigned int, len, sizeof(int));
1317
1318 if(len < 0)
1319 return -EINVAL;
1320
1321 switch(optname) {
1322 case UDP_CORK:
1323 val = up->corkflag;
1324 break;
1325
1326 case UDP_ENCAP:
1327 val = up->encap_type;
1328 break;
1329
1330 default:
1331 return -ENOPROTOOPT;
1332 };
1333
1334 if(put_user(len, optlen))
1335 return -EFAULT;
1336 if(copy_to_user(optval, &val,len))
1337 return -EFAULT;
1338 return 0;
1339}
1340
1341
1342struct proto udp_prot = {
1343 .name = "UDP",
1344 .close = udp_close,
1345 .connect = udp_connect,
1346 .disconnect = udp_disconnect,
1347 .ioctl = udp_ioctl,
1348 .destroy = udp_destroy_sock,
1349 .setsockopt = udp_setsockopt,
1350 .getsockopt = udp_getsockopt,
1351 .sendmsg = udp_sendmsg,
1352 .recvmsg = udp_recvmsg,
1353 .sendpage = udp_sendpage,
1354 .backlog_rcv = udp_queue_rcv_skb,
1355 .hash = udp_v4_hash,
1356 .unhash = udp_v4_unhash,
1357 .get_port = udp_v4_get_port,
1358};
1359
1360
1361#ifdef CONFIG_PROC_FS
1362
1363static struct sock *udp_get_first(struct seq_file *seq)
1364{
1365 struct sock *sk;
1366 struct udp_iter_state *state = seq->private;
1367
1368 for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
1369 struct hlist_node *node;
1370 sk_for_each(sk, node, &udp_hash[state->bucket]) {
1371 if (sk->sk_family == state->family)
1372 goto found;
1373 }
1374 }
1375 sk = NULL;
1376found:
1377 return sk;
1378}
1379
1380static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
1381{
1382 struct udp_iter_state *state = seq->private;
1383
1384 do {
1385 sk = sk_next(sk);
1386try_again:
1387 ;
1388 } while (sk && sk->sk_family != state->family);
1389
1390 if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
1391 sk = sk_head(&udp_hash[state->bucket]);
1392 goto try_again;
1393 }
1394 return sk;
1395}
1396
1397static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
1398{
1399 struct sock *sk = udp_get_first(seq);
1400
1401 if (sk)
1402 while(pos && (sk = udp_get_next(seq, sk)) != NULL)
1403 --pos;
1404 return pos ? NULL : sk;
1405}
1406
1407static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
1408{
1409 read_lock(&udp_hash_lock);
1410 return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
1411}
1412
1413static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1414{
1415 struct sock *sk;
1416
1417 if (v == (void *)1)
1418 sk = udp_get_idx(seq, 0);
1419 else
1420 sk = udp_get_next(seq, v);
1421
1422 ++*pos;
1423 return sk;
1424}
1425
1426static void udp_seq_stop(struct seq_file *seq, void *v)
1427{
1428 read_unlock(&udp_hash_lock);
1429}
1430
1431static int udp_seq_open(struct inode *inode, struct file *file)
1432{
1433 struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1434 struct seq_file *seq;
1435 int rc = -ENOMEM;
1436 struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1437
1438 if (!s)
1439 goto out;
1440 memset(s, 0, sizeof(*s));
1441 s->family = afinfo->family;
1442 s->seq_ops.start = udp_seq_start;
1443 s->seq_ops.next = udp_seq_next;
1444 s->seq_ops.show = afinfo->seq_show;
1445 s->seq_ops.stop = udp_seq_stop;
1446
1447 rc = seq_open(file, &s->seq_ops);
1448 if (rc)
1449 goto out_kfree;
1450
1451 seq = file->private_data;
1452 seq->private = s;
1453out:
1454 return rc;
1455out_kfree:
1456 kfree(s);
1457 goto out;
1458}
1459
1460
1461int udp_proc_register(struct udp_seq_afinfo *afinfo)
1462{
1463 struct proc_dir_entry *p;
1464 int rc = 0;
1465
1466 if (!afinfo)
1467 return -EINVAL;
1468 afinfo->seq_fops->owner = afinfo->owner;
1469 afinfo->seq_fops->open = udp_seq_open;
1470 afinfo->seq_fops->read = seq_read;
1471 afinfo->seq_fops->llseek = seq_lseek;
1472 afinfo->seq_fops->release = seq_release_private;
1473
1474 p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
1475 if (p)
1476 p->data = afinfo;
1477 else
1478 rc = -ENOMEM;
1479 return rc;
1480}
1481
1482void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
1483{
1484 if (!afinfo)
1485 return;
1486 proc_net_remove(afinfo->name);
1487 memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
1488}
1489
1490
1491static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
1492{
1493 struct inet_opt *inet = inet_sk(sp);
1494 unsigned int dest = inet->daddr;
1495 unsigned int src = inet->rcv_saddr;
1496 __u16 destp = ntohs(inet->dport);
1497 __u16 srcp = ntohs(inet->sport);
1498
1499 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
1500 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
1501 bucket, src, srcp, dest, destp, sp->sk_state,
1502 atomic_read(&sp->sk_wmem_alloc),
1503 atomic_read(&sp->sk_rmem_alloc),
1504 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1505 atomic_read(&sp->sk_refcnt), sp);
1506}
1507
1508static int udp4_seq_show(struct seq_file *seq, void *v)
1509{
1510 if (v == SEQ_START_TOKEN)
1511 seq_printf(seq, "%-127s\n",
1512 " sl local_address rem_address st tx_queue "
1513 "rx_queue tr tm->when retrnsmt uid timeout "
1514 "inode");
1515 else {
1516 char tmpbuf[129];
1517 struct udp_iter_state *state = seq->private;
1518
1519 udp4_format_sock(v, tmpbuf, state->bucket);
1520 seq_printf(seq, "%-127s\n", tmpbuf);
1521 }
1522 return 0;
1523}
1524
1525
1526static struct file_operations udp4_seq_fops;
1527static struct udp_seq_afinfo udp4_seq_afinfo = {
1528 .owner = THIS_MODULE,
1529 .name = "udp",
1530 .family = AF_INET,
1531 .seq_show = udp4_seq_show,
1532 .seq_fops = &udp4_seq_fops,
1533};
1534
1535int __init udp4_proc_init(void)
1536{
1537 return udp_proc_register(&udp4_seq_afinfo);
1538}
1539
1540void udp4_proc_exit(void)
1541{
1542 udp_proc_unregister(&udp4_seq_afinfo);
1543}
1544#endif
1545
1546EXPORT_SYMBOL(udp_connect);
1547EXPORT_SYMBOL(udp_disconnect);
1548EXPORT_SYMBOL(udp_hash);
1549EXPORT_SYMBOL(udp_hash_lock);
1550EXPORT_SYMBOL(udp_ioctl);
1551EXPORT_SYMBOL(udp_port_rover);
1552EXPORT_SYMBOL(udp_prot);
1553EXPORT_SYMBOL(udp_sendmsg);
1554
1555#ifdef CONFIG_PROC_FS
1556EXPORT_SYMBOL(udp_proc_register);
1557EXPORT_SYMBOL(udp_proc_unregister);
1558#endif
1559