This source file includes following definitions.
- udp_cache_zap
- udp_err
- udp_check
- udp_getfrag
- udp_getfrag_nosum
- udp_send
- udp_sendto
- udp_write
- udp_ioctl
- udp_recvfrom
- udp_read
- udp_connect
- udp_close
- udp_rcv
- udp_deliver
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
82
83
84 #include <asm/system.h>
85 #include <asm/segment.h>
86 #include <linux/types.h>
87 #include <linux/sched.h>
88 #include <linux/fcntl.h>
89 #include <linux/socket.h>
90 #include <linux/sockios.h>
91 #include <linux/in.h>
92 #include <linux/errno.h>
93 #include <linux/timer.h>
94 #include <linux/termios.h>
95 #include <linux/mm.h>
96 #include <linux/config.h>
97 #include <linux/inet.h>
98 #include <linux/netdevice.h>
99 #include <net/snmp.h>
100 #include <net/ip.h>
101 #include <net/protocol.h>
102 #include <net/tcp.h>
103 #include <linux/skbuff.h>
104 #include <net/sock.h>
105 #include <net/udp.h>
106 #include <net/icmp.h>
107 #include <net/route.h>
108 #include <net/checksum.h>
109
110
111
112
113
114 struct udp_mib udp_statistics;
115
116
117
118
119
120 volatile unsigned long uh_cache_saddr,uh_cache_daddr;
121 volatile unsigned short uh_cache_dport, uh_cache_sport;
122 volatile struct sock *uh_cache_sk;
123
124 void udp_cache_zap(void)
125 {
126 unsigned long flags;
127 save_flags(flags);
128 cli();
129 uh_cache_saddr=0;
130 uh_cache_daddr=0;
131 uh_cache_dport=0;
132 uh_cache_sport=0;
133 uh_cache_sk=NULL;
134 restore_flags(flags);
135 }
136
137 static int udp_deliver(struct sock *sk, struct udphdr *uh, struct sk_buff *skb, struct device *dev, long saddr, long daddr, int len);
138
139 #define min(a,b) ((a)<(b)?(a):(b))
140
141
142
143
144
145
146
147
148
149
150
151
152
153 void udp_err(int type, int code, unsigned char *header, __u32 daddr,
154 __u32 saddr, struct inet_protocol *protocol)
155 {
156 struct udphdr *uh;
157 struct sock *sk;
158
159
160
161
162
163 uh = (struct udphdr *)header;
164
165 sk = get_sock(&udp_prot, uh->source, daddr, uh->dest, saddr);
166
167 if (sk == NULL)
168 return;
169
170 if (type == ICMP_SOURCE_QUENCH)
171 {
172 if (sk->cong_window > 1)
173 sk->cong_window = sk->cong_window/2;
174 return;
175 }
176
177 if (type == ICMP_PARAMETERPROB)
178 {
179 sk->err = EPROTO;
180 sk->error_report(sk);
181 return;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196 if (code < 13 && icmp_err_convert[code].fatal)
197 {
198 sk->err = icmp_err_convert[code].errno;
199 sk->error_report(sk);
200 }
201 }
202
203
204 static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
205 {
206 return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
207 }
208
209 struct udpfakehdr
210 {
211 struct udphdr uh;
212 int daddr;
213 int other;
214 const char *from;
215 int wcheck;
216 };
217
218
219
220
221
222
223
224 static void udp_getfrag(const void *p, __u32 saddr, char * to, unsigned int offset, unsigned int fraglen)
225 {
226 struct udpfakehdr *ufh = (struct udpfakehdr *)p;
227 const char *src;
228 char *dst;
229 unsigned int len;
230
231 if (offset)
232 {
233 len = fraglen;
234 src = ufh->from+(offset-sizeof(struct udphdr));
235 dst = to;
236 }
237 else
238 {
239 len = fraglen-sizeof(struct udphdr);
240 src = ufh->from;
241 dst = to+sizeof(struct udphdr);
242 }
243 ufh->wcheck = csum_partial_copy_fromuser(src, dst, len, ufh->wcheck);
244 if (offset == 0)
245 {
246 ufh->wcheck = csum_partial((char *)ufh, sizeof(struct udphdr),
247 ufh->wcheck);
248 ufh->uh.check = csum_tcpudp_magic(saddr, ufh->daddr,
249 ntohs(ufh->uh.len),
250 IPPROTO_UDP, ufh->wcheck);
251 if (ufh->uh.check == 0)
252 ufh->uh.check = -1;
253 memcpy(to, ufh, sizeof(struct udphdr));
254 }
255 }
256
257
258
259
260
261
262
263
264 static void udp_getfrag_nosum(const void *p, __u32 saddr, char * to, unsigned int offset, unsigned int fraglen)
265 {
266 struct udpfakehdr *ufh = (struct udpfakehdr *)p;
267 const char *src;
268 char *dst;
269 unsigned int len;
270
271 if (offset)
272 {
273 len = fraglen;
274 src = ufh->from+(offset-sizeof(struct udphdr));
275 dst = to;
276 }
277 else
278 {
279 len = fraglen-sizeof(struct udphdr);
280 src = ufh->from;
281 dst = to+sizeof(struct udphdr);
282 }
283 memcpy_fromfs(dst,src,len);
284 if (offset == 0)
285 memcpy(to, ufh, sizeof(struct udphdr));
286 }
287
288
289
290
291
292
293 static int udp_send(struct sock *sk, struct sockaddr_in *sin,
294 const unsigned char *from, int len, int rt)
295 {
296 int ulen = len + sizeof(struct udphdr);
297 int a;
298 struct udpfakehdr ufh;
299
300 ufh.uh.source = sk->dummy_th.source;
301 ufh.uh.dest = sin->sin_port;
302 ufh.uh.len = htons(ulen);
303 ufh.uh.check = 0;
304 ufh.daddr = sin->sin_addr.s_addr;
305 ufh.other = (htons(ulen) << 16) + IPPROTO_UDP*256;
306 ufh.from = from;
307 ufh.wcheck = 0;
308
309
310
311
312
313
314
315
316
317
318
319 if(sk->no_check)
320 a = ip_build_xmit(sk, udp_getfrag_nosum, &ufh, ulen,
321 sin->sin_addr.s_addr, rt, IPPROTO_UDP);
322 else
323 a = ip_build_xmit(sk, udp_getfrag, &ufh, ulen,
324 sin->sin_addr.s_addr, rt, IPPROTO_UDP);
325 if(a<0)
326 return a;
327 udp_statistics.UdpOutDatagrams++;
328 return len;
329 }
330
331
332 static int udp_sendto(struct sock *sk, const unsigned char *from, int len, int noblock,
333 unsigned flags, struct sockaddr_in *usin, int addr_len)
334 {
335 struct sockaddr_in sin;
336 int tmp;
337
338
339
340
341 if (flags&~MSG_DONTROUTE)
342 return(-EINVAL);
343
344
345
346
347 if (usin)
348 {
349 if (addr_len < sizeof(sin))
350 return(-EINVAL);
351 if (usin->sin_family && usin->sin_family != AF_INET)
352 return(-EINVAL);
353 if (usin->sin_port == 0)
354 return(-EINVAL);
355 }
356 else
357 {
358 if (sk->state != TCP_ESTABLISHED)
359 return(-EINVAL);
360 sin.sin_family = AF_INET;
361 sin.sin_port = sk->dummy_th.dest;
362 sin.sin_addr.s_addr = sk->daddr;
363 usin = &sin;
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381 if(usin->sin_addr.s_addr==INADDR_ANY)
382 usin->sin_addr.s_addr=ip_my_addr();
383
384 if(!sk->broadcast && ip_chk_addr(usin->sin_addr.s_addr)==IS_BROADCAST)
385 return -EACCES;
386
387 sk->inuse = 1;
388
389
390 tmp = udp_send(sk, usin, from, len, flags);
391
392
393 release_sock(sk);
394 return(tmp);
395 }
396
397
398
399
400
401 static int udp_write(struct sock *sk, const unsigned char *buff, int len, int noblock,
402 unsigned flags)
403 {
404 return(udp_sendto(sk, buff, len, noblock, flags, NULL, 0));
405 }
406
407
408
409
410
411
412 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
413 {
414 int err;
415 switch(cmd)
416 {
417 case TIOCOUTQ:
418 {
419 unsigned long amount;
420
421 if (sk->state == TCP_LISTEN) return(-EINVAL);
422 amount = sk->prot->wspace(sk);
423 err=verify_area(VERIFY_WRITE,(void *)arg,
424 sizeof(unsigned long));
425 if(err)
426 return(err);
427 put_fs_long(amount,(unsigned long *)arg);
428 return(0);
429 }
430
431 case TIOCINQ:
432 {
433 struct sk_buff *skb;
434 unsigned long amount;
435
436 if (sk->state == TCP_LISTEN) return(-EINVAL);
437 amount = 0;
438 skb = skb_peek(&sk->receive_queue);
439 if (skb != NULL) {
440
441
442
443
444
445 amount = skb->len;
446 }
447 err=verify_area(VERIFY_WRITE,(void *)arg,
448 sizeof(unsigned long));
449 if(err)
450 return(err);
451 put_fs_long(amount,(unsigned long *)arg);
452 return(0);
453 }
454
455 default:
456 return(-EINVAL);
457 }
458 return(0);
459 }
460
461
462
463
464
465
466
467 int udp_recvfrom(struct sock *sk, unsigned char *to, int len,
468 int noblock, unsigned flags, struct sockaddr_in *sin,
469 int *addr_len)
470 {
471 int copied = 0;
472 int truesize;
473 struct sk_buff *skb;
474 int er;
475
476
477
478
479
480 if (addr_len)
481 *addr_len=sizeof(*sin);
482
483
484
485
486
487
488 skb=skb_recv_datagram(sk,flags,noblock,&er);
489 if(skb==NULL)
490 return er;
491
492 truesize = skb->len - sizeof(struct udphdr);
493 copied = min(len, truesize);
494
495
496
497
498
499 skb_copy_datagram(skb,sizeof(struct udphdr),to,copied);
500 sk->stamp=skb->stamp;
501
502
503 if (sin)
504 {
505 sin->sin_family = AF_INET;
506 sin->sin_port = skb->h.uh->source;
507 sin->sin_addr.s_addr = skb->daddr;
508 }
509
510 skb_free_datagram(skb);
511 release_sock(sk);
512 return(copied);
513 }
514
515
516
517
518
519 int udp_read(struct sock *sk, unsigned char *buff, int len, int noblock,
520 unsigned flags)
521 {
522 return(udp_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
523 }
524
525
526 int udp_connect(struct sock *sk, struct sockaddr_in *usin, int addr_len)
527 {
528 struct rtable *rt;
529 __u32 sa;
530 if (addr_len < sizeof(*usin))
531 return(-EINVAL);
532
533 if (usin->sin_family && usin->sin_family != AF_INET)
534 return(-EAFNOSUPPORT);
535 if (usin->sin_addr.s_addr==INADDR_ANY)
536 usin->sin_addr.s_addr=ip_my_addr();
537
538 if(!sk->broadcast && ip_chk_addr(usin->sin_addr.s_addr)==IS_BROADCAST)
539 return -EACCES;
540
541 rt=(sk->localroute?ip_rt_local:ip_rt_route)((__u32)usin->sin_addr.s_addr, NULL, &sa);
542 if(rt==NULL)
543 return -ENETUNREACH;
544 sk->saddr = sa;
545 sk->daddr = usin->sin_addr.s_addr;
546 sk->dummy_th.dest = usin->sin_port;
547 sk->state = TCP_ESTABLISHED;
548 udp_cache_zap();
549 sk->ip_route_cache = rt;
550 sk->ip_route_stamp = rt_stamp;
551 return(0);
552 }
553
554
555 static void udp_close(struct sock *sk, int timeout)
556 {
557 sk->inuse = 1;
558 sk->state = TCP_CLOSE;
559 if(uh_cache_sk==sk)
560 udp_cache_zap();
561 if (sk->dead)
562 destroy_sock(sk);
563 else
564 release_sock(sk);
565 }
566
567
568
569
570
571
572 int udp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
573 __u32 daddr, unsigned short len,
574 __u32 saddr, int redo, struct inet_protocol *protocol)
575 {
576 struct sock *sk;
577 struct udphdr *uh;
578 unsigned short ulen;
579 int addr_type = IS_MYADDR;
580
581 if(!dev || dev->pa_addr!=daddr)
582 addr_type=ip_chk_addr(daddr);
583
584
585
586
587
588 uh = (struct udphdr *) skb->h.uh;
589
590 ip_statistics.IpInDelivers++;
591
592
593
594
595
596 ulen = ntohs(uh->len);
597
598 if (ulen > len || len < sizeof(*uh) || ulen < sizeof(*uh))
599 {
600 NETDEBUG(printk("UDP: short packet: %d/%d\n", ulen, len));
601 udp_statistics.UdpInErrors++;
602 kfree_skb(skb, FREE_WRITE);
603 return(0);
604 }
605
606
607
608
609
610
611
612
613 if (uh->check && (
614 ( skb->ip_summed && udp_check(uh, len, saddr, daddr, skb->csum ) ) ||
615 ( !skb->ip_summed && udp_check(uh, len, saddr, daddr,csum_partial((char*)uh, len, 0)))
616 )
617 )
618 {
619
620
621
622
623
624
625 NETDEBUG(printk("UDP: bad checksum. From %08lX:%d to %08lX:%d ulen %d\n",
626 ntohl(saddr),ntohs(uh->source),
627 ntohl(daddr),ntohs(uh->dest),
628 ulen));
629 udp_statistics.UdpInErrors++;
630 kfree_skb(skb, FREE_WRITE);
631 return(0);
632 }
633
634
635 len=ulen;
636
637 #ifdef CONFIG_IP_MULTICAST
638 if (addr_type!=IS_MYADDR)
639 {
640
641
642
643 struct sock *sknext=NULL;
644 sk=get_sock_mcast(udp_prot.sock_array[ntohs(uh->dest)&(SOCK_ARRAY_SIZE-1)], uh->dest,
645 saddr, uh->source, daddr);
646 if(sk)
647 {
648 do
649 {
650 struct sk_buff *skb1;
651
652 sknext=get_sock_mcast(sk->next, uh->dest, saddr, uh->source, daddr);
653 if(sknext)
654 skb1=skb_clone(skb,GFP_ATOMIC);
655 else
656 skb1=skb;
657 if(skb1)
658 udp_deliver(sk, uh, skb1, dev,saddr,daddr,len);
659 sk=sknext;
660 }
661 while(sknext!=NULL);
662 }
663 else
664 kfree_skb(skb, FREE_READ);
665 return 0;
666 }
667 #endif
668 if(saddr==uh_cache_saddr && daddr==uh_cache_daddr && uh->dest==uh_cache_dport && uh->source==uh_cache_sport)
669 sk=(struct sock *)uh_cache_sk;
670 else
671 {
672 sk = get_sock(&udp_prot, uh->dest, saddr, uh->source, daddr);
673 uh_cache_saddr=saddr;
674 uh_cache_daddr=daddr;
675 uh_cache_dport=uh->dest;
676 uh_cache_sport=uh->source;
677 uh_cache_sk=sk;
678 }
679
680 if (sk == NULL)
681 {
682 udp_statistics.UdpNoPorts++;
683 if (addr_type == IS_MYADDR)
684 {
685 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0, dev);
686 }
687
688
689
690
691 skb->sk = NULL;
692 kfree_skb(skb, FREE_WRITE);
693 return(0);
694 }
695 return udp_deliver(sk,uh,skb,dev, saddr, daddr, len);
696 }
697
698 static int udp_deliver(struct sock *sk, struct udphdr *uh, struct sk_buff *skb, struct device *dev, long saddr, long daddr, int len)
699 {
700 skb->sk = sk;
701 skb->dev = dev;
702 skb_trim(skb,len);
703
704
705
706
707
708 skb->daddr = saddr;
709 skb->saddr = daddr;
710
711
712
713
714
715
716
717
718
719 if (sock_queue_rcv_skb(sk,skb)<0)
720 {
721 udp_statistics.UdpInErrors++;
722 ip_statistics.IpInDiscards++;
723 ip_statistics.IpInDelivers--;
724 skb->sk = NULL;
725 kfree_skb(skb, FREE_WRITE);
726 release_sock(sk);
727 return(0);
728 }
729 udp_statistics.UdpInDatagrams++;
730 release_sock(sk);
731 return(0);
732 }
733
734
735 struct proto udp_prot = {
736 sock_wmalloc,
737 sock_rmalloc,
738 sock_wfree,
739 sock_rfree,
740 sock_rspace,
741 sock_wspace,
742 udp_close,
743 udp_read,
744 udp_write,
745 udp_sendto,
746 udp_recvfrom,
747 ip_build_header,
748 udp_connect,
749 NULL,
750 ip_queue_xmit,
751 NULL,
752 NULL,
753 NULL,
754 udp_rcv,
755 datagram_select,
756 udp_ioctl,
757 NULL,
758 NULL,
759 ip_setsockopt,
760 ip_getsockopt,
761 128,
762 0,
763 "UDP",
764 0, 0,
765 {NULL,}
766 };