This source file includes following definitions.
- udp_err
- udp_check
- udp_send_check
- udp_send
- udp_sendto
- udp_write
- udp_ioctl
- udp_recvfrom
- udp_read
- udp_connect
- udp_close
- udp_rcv
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 #include <asm/system.h>
52 #include <asm/segment.h>
53 #include <linux/types.h>
54 #include <linux/sched.h>
55 #include <linux/fcntl.h>
56 #include <linux/socket.h>
57 #include <linux/sockios.h>
58 #include <linux/in.h>
59 #include <linux/errno.h>
60 #include <linux/timer.h>
61 #include <linux/termios.h>
62 #include <linux/mm.h>
63 #include <linux/inet.h>
64 #include <linux/netdevice.h>
65 #include "snmp.h"
66 #include "ip.h"
67 #include "protocol.h"
68 #include "tcp.h"
69 #include <linux/skbuff.h>
70 #include "sock.h"
71 #include "udp.h"
72 #include "icmp.h"
73
74
75
76
77
78 struct udp_mib udp_statistics;
79
80
81
82
83 #define min(a,b) ((a)<(b)?(a):(b))
84
85
86
87
88
89
90
91
92
93
94
95
96
97 void udp_err(int err, unsigned char *header, unsigned long daddr,
98 unsigned long saddr, struct inet_protocol *protocol)
99 {
100 struct udphdr *th;
101 struct sock *sk;
102 struct iphdr *ip=(struct iphdr *)header;
103
104 header += 4*ip->ihl;
105
106
107
108
109
110 th = (struct udphdr *)header;
111
112 sk = get_sock(&udp_prot, th->source, daddr, th->dest, saddr);
113
114 if (sk == NULL)
115 return;
116
117 if (err & 0xff00 ==(ICMP_SOURCE_QUENCH << 8))
118 {
119 if (sk->cong_window > 1)
120 sk->cong_window = sk->cong_window/2;
121 return;
122 }
123
124
125
126
127
128
129
130
131
132 #ifdef CONFIG_I_AM_A_BROKEN_BSD_WEENIE
133
134
135
136
137
138 if (icmp_err_convert[err & 0xff].fatal && sk->state == TCP_ESTABLISHED)
139 {
140 sk->err = icmp_err_convert[err & 0xff].errno;
141 sk->error_report(sk);
142 }
143 #else
144 if (icmp_err_convert[err & 0xff].fatal)
145 {
146 sk->err = icmp_err_convert[err & 0xff].errno;
147 sk->error_report(sk);
148 }
149 #endif
150 }
151
152
153 static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr)
154 {
155 unsigned long sum;
156
157 __asm__( "\t addl %%ecx,%%ebx\n"
158 "\t adcl %%edx,%%ebx\n"
159 "\t adcl $0, %%ebx\n"
160 : "=b"(sum)
161 : "0"(daddr), "c"(saddr), "d"((ntohs(len) << 16) + IPPROTO_UDP*256)
162 : "cx","bx","dx" );
163
164 if (len > 3)
165 {
166 __asm__("\tclc\n"
167 "1:\n"
168 "\t lodsl\n"
169 "\t adcl %%eax, %%ebx\n"
170 "\t loop 1b\n"
171 "\t adcl $0, %%ebx\n"
172 : "=b"(sum) , "=S"(uh)
173 : "0"(sum), "c"(len/4) ,"1"(uh)
174 : "ax", "cx", "bx", "si" );
175 }
176
177
178
179
180
181 __asm__("\t movl %%ebx, %%ecx\n"
182 "\t shrl $16,%%ecx\n"
183 "\t addw %%cx, %%bx\n"
184 "\t adcw $0, %%bx\n"
185 : "=b"(sum)
186 : "0"(sum)
187 : "bx", "cx");
188
189
190
191
192
193 if ((len & 2) != 0)
194 {
195 __asm__("\t lodsw\n"
196 "\t addw %%ax,%%bx\n"
197 "\t adcw $0, %%bx\n"
198 : "=b"(sum), "=S"(uh)
199 : "0"(sum) ,"1"(uh)
200 : "si", "ax", "bx");
201 }
202
203
204
205
206
207 if ((len & 1) != 0)
208 {
209 __asm__("\t lodsb\n"
210 "\t movb $0,%%ah\n"
211 "\t addw %%ax,%%bx\n"
212 "\t adcw $0, %%bx\n"
213 : "=b"(sum)
214 : "0"(sum) ,"S"(uh)
215 : "si", "ax", "bx");
216 }
217
218
219
220
221
222 return((~sum) & 0xffff);
223 }
224
225
226
227
228
229
230
231 static void udp_send_check(struct udphdr *uh, unsigned long saddr,
232 unsigned long daddr, int len, struct sock *sk)
233 {
234 uh->check = 0;
235 if (sk && sk->no_check)
236 return;
237 uh->check = udp_check(uh, len, saddr, daddr);
238
239
240
241
242
243
244 if (uh->check == 0)
245 uh->check = 0xffff;
246 }
247
248
249 static int udp_send(struct sock *sk, struct sockaddr_in *sin,
250 unsigned char *from, int len, int rt)
251 {
252 struct sk_buff *skb;
253 struct device *dev;
254 struct udphdr *uh;
255 unsigned char *buff;
256 unsigned long saddr;
257 int size, tmp;
258
259
260
261
262
263 size = sk->prot->max_header + len;
264 skb = sk->prot->wmalloc(sk, size, 0, GFP_KERNEL);
265
266
267 if (skb == NULL)
268 return(-ENOBUFS);
269
270 skb->sk = NULL;
271 skb->free = 1;
272 skb->localroute = sk->localroute|(rt&MSG_DONTROUTE);
273
274
275
276
277
278 buff = skb->data;
279 saddr = 0;
280 dev = NULL;
281 tmp = sk->prot->build_header(skb, saddr, sin->sin_addr.s_addr,
282 &dev, IPPROTO_UDP, sk->opt, skb->mem_len,sk->ip_tos,sk->ip_ttl);
283 skb->sk=sk;
284
285
286
287
288
289 if (tmp < 0 )
290 {
291 sk->prot->wfree(sk, skb->mem_addr, skb->mem_len);
292 return(tmp);
293 }
294
295 buff += tmp;
296 saddr = skb->saddr;
297 skb->len = tmp + sizeof(struct udphdr) + len;
298 skb->dev = dev;
299
300
301
302
303
304 uh = (struct udphdr *) buff;
305 uh->len = htons(len + sizeof(struct udphdr));
306 uh->source = sk->dummy_th.source;
307 uh->dest = sin->sin_port;
308 buff = (unsigned char *) (uh + 1);
309
310
311
312
313
314 memcpy_fromfs(buff, from, len);
315
316
317
318
319
320 udp_send_check(uh, saddr, sin->sin_addr.s_addr, skb->len - tmp, sk);
321
322
323
324
325
326 udp_statistics.UdpOutDatagrams++;
327
328 sk->prot->queue_xmit(sk, dev, skb, 1);
329 return(len);
330 }
331
332
333 static int udp_sendto(struct sock *sk, unsigned char *from, int len, int noblock,
334 unsigned flags, struct sockaddr_in *usin, int addr_len)
335 {
336 struct sockaddr_in sin;
337 int tmp;
338
339
340
341
342 if (flags&~MSG_DONTROUTE)
343 return(-EINVAL);
344
345
346
347
348 if (usin)
349 {
350 if (addr_len < sizeof(sin))
351 return(-EINVAL);
352 memcpy(&sin,usin,sizeof(sin));
353 if (sin.sin_family && sin.sin_family != AF_INET)
354 return(-EINVAL);
355 if (sin.sin_port == 0)
356 return(-EINVAL);
357 }
358 else
359 {
360 if (sk->state != TCP_ESTABLISHED)
361 return(-EINVAL);
362 sin.sin_family = AF_INET;
363 sin.sin_port = sk->dummy_th.dest;
364 sin.sin_addr.s_addr = sk->daddr;
365 }
366
367
368
369
370
371
372 if(sin.sin_addr.s_addr==INADDR_ANY)
373 sin.sin_addr.s_addr=ip_my_addr();
374
375 if(!sk->broadcast && ip_chk_addr(sin.sin_addr.s_addr)==IS_BROADCAST)
376 return -EACCES;
377
378 sk->inuse = 1;
379
380
381 tmp = udp_send(sk, &sin, from, len, flags);
382
383
384 release_sock(sk);
385 return(tmp);
386 }
387
388
389
390
391
392 static int udp_write(struct sock *sk, unsigned char *buff, int len, int noblock,
393 unsigned flags)
394 {
395 return(udp_sendto(sk, buff, len, noblock, flags, NULL, 0));
396 }
397
398
399
400
401
402
403 int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
404 {
405 int err;
406 switch(cmd)
407 {
408 case TIOCOUTQ:
409 {
410 unsigned long amount;
411
412 if (sk->state == TCP_LISTEN) return(-EINVAL);
413 amount = sk->prot->wspace(sk);
414 err=verify_area(VERIFY_WRITE,(void *)arg,
415 sizeof(unsigned long));
416 if(err)
417 return(err);
418 put_fs_long(amount,(unsigned long *)arg);
419 return(0);
420 }
421
422 case TIOCINQ:
423 {
424 struct sk_buff *skb;
425 unsigned long amount;
426
427 if (sk->state == TCP_LISTEN) return(-EINVAL);
428 amount = 0;
429 skb = skb_peek(&sk->receive_queue);
430 if (skb != NULL) {
431
432
433
434
435
436 amount = skb->len;
437 }
438 err=verify_area(VERIFY_WRITE,(void *)arg,
439 sizeof(unsigned long));
440 if(err)
441 return(err);
442 put_fs_long(amount,(unsigned long *)arg);
443 return(0);
444 }
445
446 default:
447 return(-EINVAL);
448 }
449 return(0);
450 }
451
452
453
454
455
456
457
458 int udp_recvfrom(struct sock *sk, unsigned char *to, int len,
459 int noblock, unsigned flags, struct sockaddr_in *sin,
460 int *addr_len)
461 {
462 int copied = 0;
463 int truesize;
464 struct sk_buff *skb;
465 int er;
466
467
468
469
470
471 if (addr_len)
472 *addr_len=sizeof(*sin);
473
474
475
476
477
478
479 skb=skb_recv_datagram(sk,flags,noblock,&er);
480 if(skb==NULL)
481 return er;
482
483 truesize = skb->len;
484 copied = min(len, truesize);
485
486
487
488
489
490 skb_copy_datagram(skb,sizeof(struct udphdr),to,copied);
491 sk->stamp=skb->stamp;
492
493
494 if (sin)
495 {
496 sin->sin_family = AF_INET;
497 sin->sin_port = skb->h.uh->source;
498 sin->sin_addr.s_addr = skb->daddr;
499 }
500
501 skb_free_datagram(skb);
502 release_sock(sk);
503 return(truesize);
504 }
505
506
507
508
509
510 int udp_read(struct sock *sk, unsigned char *buff, int len, int noblock,
511 unsigned flags)
512 {
513 return(udp_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
514 }
515
516
517 int udp_connect(struct sock *sk, struct sockaddr_in *usin, int addr_len)
518 {
519 if (addr_len < sizeof(*usin))
520 return(-EINVAL);
521
522 if (usin->sin_family && usin->sin_family != AF_INET)
523 return(-EAFNOSUPPORT);
524 if (usin->sin_addr.s_addr==INADDR_ANY)
525 usin->sin_addr.s_addr=ip_my_addr();
526
527 if(!sk->broadcast && ip_chk_addr(usin->sin_addr.s_addr)==IS_BROADCAST)
528 return -EACCES;
529
530 sk->daddr = usin->sin_addr.s_addr;
531 sk->dummy_th.dest = usin->sin_port;
532 sk->state = TCP_ESTABLISHED;
533 return(0);
534 }
535
536
537 static void udp_close(struct sock *sk, int timeout)
538 {
539 sk->inuse = 1;
540 sk->state = TCP_CLOSE;
541 if (sk->dead)
542 destroy_sock(sk);
543 else
544 release_sock(sk);
545 }
546
547
548
549
550
551
552 int udp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
553 unsigned long daddr, unsigned short len,
554 unsigned long saddr, int redo, struct inet_protocol *protocol)
555 {
556 struct sock *sk;
557 struct udphdr *uh;
558 unsigned short ulen;
559
560
561
562
563 uh = (struct udphdr *) skb->h.uh;
564
565 ip_statistics.IpInDelivers++;
566
567
568
569
570
571 ulen = ntohs(uh->len);
572
573 if (ulen > len || len < sizeof(*uh) || ulen < sizeof(*uh))
574 {
575 printk("UDP: short packet: %d/%d\n", ulen, len);
576 udp_statistics.UdpInErrors++;
577 kfree_skb(skb, FREE_WRITE);
578 return(0);
579 }
580 len=ulen;
581
582 sk = get_sock(&udp_prot, uh->dest, saddr, uh->source, daddr);
583 if (sk == NULL)
584 {
585 udp_statistics.UdpNoPorts++;
586 if (ip_chk_addr(daddr) == IS_MYADDR)
587 {
588 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, dev);
589 }
590
591
592
593
594 skb->sk = NULL;
595 kfree_skb(skb, FREE_WRITE);
596 return(0);
597 }
598
599 if (uh->check && udp_check(uh, len, saddr, daddr))
600 {
601 printk("UDP: bad checksum.\n");
602 udp_statistics.UdpInErrors++;
603 kfree_skb(skb, FREE_WRITE);
604 return(0);
605 }
606
607 skb->sk = sk;
608 skb->dev = dev;
609 skb->len = len;
610
611
612
613
614
615 skb->daddr = saddr;
616 skb->saddr = daddr;
617
618
619
620
621
622
623 if (sk->rmem_alloc + skb->mem_len >= sk->rcvbuf)
624 {
625 udp_statistics.UdpInErrors++;
626 ip_statistics.IpInDiscards++;
627 ip_statistics.IpInDelivers--;
628 skb->sk = NULL;
629 kfree_skb(skb, FREE_WRITE);
630 release_sock(sk);
631 return(0);
632 }
633 sk->rmem_alloc += skb->mem_len;
634 udp_statistics.UdpInDatagrams++;
635
636
637
638
639
640 skb->len = len - sizeof(*uh);
641 skb_queue_tail(&sk->receive_queue,skb);
642
643
644 if (!sk->dead)
645 sk->data_ready(sk,skb->len);
646
647 release_sock(sk);
648 return(0);
649 }
650
651
652 struct proto udp_prot = {
653 sock_wmalloc,
654 sock_rmalloc,
655 sock_wfree,
656 sock_rfree,
657 sock_rspace,
658 sock_wspace,
659 udp_close,
660 udp_read,
661 udp_write,
662 udp_sendto,
663 udp_recvfrom,
664 ip_build_header,
665 udp_connect,
666 NULL,
667 ip_queue_xmit,
668 ip_retransmit,
669 NULL,
670 NULL,
671 udp_rcv,
672 datagram_select,
673 udp_ioctl,
674 NULL,
675 NULL,
676 ip_setsockopt,
677 ip_getsockopt,
678 128,
679 0,
680 {NULL,},
681 "UDP"
682 };
683