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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
/*
* INET An implementation of the TCP/IP protocol suite for the LINUX
* operating system. INET is implemented using the BSD Socket
* interface as the means of communication with the user level.
*
* The User Datagram Protocol (UDP).
*
* Version: @(#)udp.c 1.0.13 06/02/93
*
* Authors: Ross Biro, <bir7@leland.Stanford.Edu>
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
*
* Fixes:
* Alan Cox : verify_area() calls
* Alan Cox : stopped close while in use off icmp
* messages. Not a fix but a botch that
* for udp at least is 'valid'.
* Alan Cox : Fixed icmp handling properly
* Alan Cox : Correct error for oversized datagrams
* Alan Cox : Tidied select() semantics.
* Alan Cox : udp_err() fixed properly, also now
* select and read wake correctly on errors
* Alan Cox : udp_send verify_area moved to avoid mem leak
* Alan Cox : UDP can count its memory
* Alan Cox : send to an unknown connection causes
* an ECONNREFUSED off the icmp, but
* does NOT close.
* Alan Cox : Switched to new sk_buff handlers. No more backlog!
* Alan Cox : Using generic datagram code. Even smaller and the PEEK
* bug no longer crashes it.
* Fred Van Kempen : Net2e support for sk->broadcast.
* Alan Cox : Uses skb_free_datagram
* Alan Cox : Added get/set sockopt support.
* Alan Cox : Broadcasting without option set returns EACCES.
* Alan Cox : No wakeup calls. Instead we now use the callbacks.
* Alan Cox : Use ip_tos and ip_ttl
* Alan Cox : SNMP Mibs
* Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
* Matt Dillon : UDP length checks.
* Alan Cox : Smarter af_inet used properly.
* Alan Cox : Use new kernel side addressing.
* Alan Cox : Incorrect return on truncated datagram receive.
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <asm/system.h>
#include <asm/segment.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/fcntl.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/termios.h>
#include <linux/mm.h>
#include <linux/config.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include "snmp.h"
#include "ip.h"
#include "protocol.h"
#include "tcp.h"
#include <linux/skbuff.h>
#include "sock.h"
#include "udp.h"
#include "icmp.h"
#include "route.h"
/*
* SNMP MIB for the UDP layer
*/
struct udp_mib udp_statistics;
static int udp_deliver(struct sock *sk, struct udphdr *uh, struct sk_buff *skb, struct device *dev, long saddr, long daddr, int len);
#define min(a,b) ((a)<(b)?(a):(b))
/*
* This routine is called by the ICMP module when it gets some
* sort of error condition. If err < 0 then the socket should
* be closed and the error returned to the user. If err > 0
* it's just the icmp type << 8 | icmp code.
* Header points to the ip header of the error packet. We move
* on past this. Then (as it used to claim before adjustment)
* header points to the first 8 bytes of the udp header. We need
* to find the appropriate port.
*/
void udp_err(int err, unsigned char *header, unsigned long daddr,
unsigned long saddr, struct inet_protocol *protocol)
{
struct udphdr *th;
struct sock *sk;
struct iphdr *ip=(struct iphdr *)header;
header += 4*ip->ihl;
/*
* Find the 8 bytes of post IP header ICMP included for us
*/
th = (struct udphdr *)header;
sk = get_sock(&udp_prot, th->source, daddr, th->dest, saddr);
if (sk == NULL)
return; /* No socket for error */
if (err & 0xff00 ==(ICMP_SOURCE_QUENCH << 8))
{ /* Slow down! */
if (sk->cong_window > 1)
sk->cong_window = sk->cong_window/2;
return;
}
/*
* Various people wanted BSD UDP semantics. Well they've come
* back out because they slow down response to stuff like dead
* or unreachable name servers and they screw term users something
* chronic. Oh and it violates RFC1122. So basically fix your
* client code people.
*/
#ifdef CONFIG_I_AM_A_BROKEN_BSD_WEENIE
/*
* It's only fatal if we have connected to them. I'm not happy
* with this code. Some BSD comparisons need doing.
*/
if (icmp_err_convert[err & 0xff].fatal && sk->state == TCP_ESTABLISHED)
{
sk->err = icmp_err_convert[err & 0xff].errno;
sk->error_report(sk);
}
#else
if (icmp_err_convert[err & 0xff].fatal)
{
sk->err = icmp_err_convert[err & 0xff].errno;
sk->error_report(sk);
}
#endif
}
static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr)
{
unsigned long sum;
__asm__( "\t addl %%ecx,%%ebx\n"
"\t adcl %%edx,%%ebx\n"
"\t adcl $0, %%ebx\n"
: "=b"(sum)
: "0"(daddr), "c"(saddr), "d"((ntohs(len) << 16) + IPPROTO_UDP*256)
: "cx","bx","dx" );
if (len > 3)
{
__asm__("\tclc\n"
"1:\n"
"\t lodsl\n"
"\t adcl %%eax, %%ebx\n"
"\t loop 1b\n"
"\t adcl $0, %%ebx\n"
: "=b"(sum) , "=S"(uh)
: "0"(sum), "c"(len/4) ,"1"(uh)
: "ax", "cx", "bx", "si" );
}
/*
* Convert from 32 bits to 16 bits.
*/
__asm__("\t movl %%ebx, %%ecx\n"
"\t shrl $16,%%ecx\n"
"\t addw %%cx, %%bx\n"
"\t adcw $0, %%bx\n"
: "=b"(sum)
: "0"(sum)
: "bx", "cx");
/*
* Check for an extra word.
*/
if ((len & 2) != 0)
{
__asm__("\t lodsw\n"
"\t addw %%ax,%%bx\n"
"\t adcw $0, %%bx\n"
: "=b"(sum), "=S"(uh)
: "0"(sum) ,"1"(uh)
: "si", "ax", "bx");
}
/*
* Now check for the extra byte.
*/
if ((len & 1) != 0)
{
__asm__("\t lodsb\n"
"\t movb $0,%%ah\n"
"\t addw %%ax,%%bx\n"
"\t adcw $0, %%bx\n"
: "=b"(sum)
: "0"(sum) ,"S"(uh)
: "si", "ax", "bx");
}
/*
* We only want the bottom 16 bits, but we never cleared the top 16.
*/
return((~sum) & 0xffff);
}
/*
* Generate UDP checksums. These may be disabled, eg for fast NFS over ethernet
* We default them enabled.. if you turn them off you either know what you are
* doing or get burned...
*/
static void udp_send_check(struct udphdr *uh, unsigned long saddr,
unsigned long daddr, int len, struct sock *sk)
{
uh->check = 0;
if (sk && sk->no_check)
return;
uh->check = udp_check(uh, len, saddr, daddr);
/*
* FFFF and 0 are the same, pick the right one as 0 in the
* actual field means no checksum.
*/
if (uh->check == 0)
uh->check = 0xffff;
}
static int udp_send(struct sock *sk, struct sockaddr_in *sin,
unsigned char *from, int len, int rt)
{
struct sk_buff *skb;
struct device *dev;
struct udphdr *uh;
unsigned char *buff;
unsigned long saddr;
int size, tmp;
int ttl;
/*
* Allocate an sk_buff copy of the packet.
*/
size = sk->prot->max_header + len;
skb = sock_alloc_send_skb(sk, size, 0, &tmp);
if (skb == NULL)
return tmp;
skb->sk = NULL; /* to avoid changing sk->saddr */
skb->free = 1;
skb->localroute = sk->localroute|(rt&MSG_DONTROUTE);
/*
* Now build the IP and MAC header.
*/
buff = skb->data;
saddr = sk->saddr;
dev = NULL;
ttl = sk->ip_ttl;
#ifdef CONFIG_IP_MULTICAST
if (MULTICAST(sin->sin_addr.s_addr))
ttl = sk->ip_mc_ttl;
#endif
tmp = sk->prot->build_header(skb, saddr, sin->sin_addr.s_addr,
&dev, IPPROTO_UDP, sk->opt, skb->mem_len,sk->ip_tos,ttl);
skb->sk=sk; /* So memory is freed correctly */
/*
* Unable to put a header on the packet.
*/
if (tmp < 0 )
{
sk->prot->wfree(sk, skb->mem_addr, skb->mem_len);
return(tmp);
}
buff += tmp;
saddr = skb->saddr; /*dev->pa_addr;*/
skb->len = tmp + sizeof(struct udphdr) + len; /* len + UDP + IP + MAC */
skb->dev = dev;
/*
* Fill in the UDP header.
*/
uh = (struct udphdr *) buff;
uh->len = htons(len + sizeof(struct udphdr));
uh->source = sk->dummy_th.source;
uh->dest = sin->sin_port;
buff = (unsigned char *) (uh + 1);
/*
* Copy the user data.
*/
memcpy_fromfs(buff, from, len);
/*
* Set up the UDP checksum.
*/
udp_send_check(uh, saddr, sin->sin_addr.s_addr, skb->len - tmp, sk);
/*
* Send the datagram to the interface.
*/
udp_statistics.UdpOutDatagrams++;
sk->prot->queue_xmit(sk, dev, skb, 1);
return(len);
}
static int udp_sendto(struct sock *sk, unsigned char *from, int len, int noblock,
unsigned flags, struct sockaddr_in *usin, int addr_len)
{
struct sockaddr_in sin;
int tmp;
/*
* Check the flags. We support no flags for UDP sending
*/
if (flags&~MSG_DONTROUTE)
return(-EINVAL);
/*
* Get and verify the address.
*/
if (usin)
{
if (addr_len < sizeof(sin))
return(-EINVAL);
memcpy(&sin,usin,sizeof(sin));
if (sin.sin_family && sin.sin_family != AF_INET)
return(-EINVAL);
if (sin.sin_port == 0)
return(-EINVAL);
}
else
{
if (sk->state != TCP_ESTABLISHED)
return(-EINVAL);
sin.sin_family = AF_INET;
sin.sin_port = sk->dummy_th.dest;
sin.sin_addr.s_addr = sk->daddr;
}
/*
* BSD socket semantics. You must set SO_BROADCAST to permit
* broadcasting of data.
*/
if(sin.sin_addr.s_addr==INADDR_ANY)
sin.sin_addr.s_addr=ip_my_addr();
if(!sk->broadcast && ip_chk_addr(sin.sin_addr.s_addr)==IS_BROADCAST)
return -EACCES; /* Must turn broadcast on first */
sk->inuse = 1;
/* Send the packet. */
tmp = udp_send(sk, &sin, from, len, flags);
/* The datagram has been sent off. Release the socket. */
release_sock(sk);
return(tmp);
}
/*
* In BSD SOCK_DGRAM a write is just like a send.
*/
static int udp_write(struct sock *sk, unsigned char *buff, int len, int noblock,
unsigned flags)
{
return(udp_sendto(sk, buff, len, noblock, flags, NULL, 0));
}
/*
* IOCTL requests applicable to the UDP protocol
*/
int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
{
int err;
switch(cmd)
{
case TIOCOUTQ:
{
unsigned long amount;
if (sk->state == TCP_LISTEN) return(-EINVAL);
amount = sk->prot->wspace(sk)/*/2*/;
err=verify_area(VERIFY_WRITE,(void *)arg,
sizeof(unsigned long));
if(err)
return(err);
put_fs_long(amount,(unsigned long *)arg);
return(0);
}
case TIOCINQ:
{
struct sk_buff *skb;
unsigned long amount;
if (sk->state == TCP_LISTEN) return(-EINVAL);
amount = 0;
skb = skb_peek(&sk->receive_queue);
if (skb != NULL) {
/*
* We will only return the amount
* of this packet since that is all
* that will be read.
*/
amount = skb->len;
}
err=verify_area(VERIFY_WRITE,(void *)arg,
sizeof(unsigned long));
if(err)
return(err);
put_fs_long(amount,(unsigned long *)arg);
return(0);
}
default:
return(-EINVAL);
}
return(0);
}
/*
* This should be easy, if there is something there we\
* return it, otherwise we block.
*/
int udp_recvfrom(struct sock *sk, unsigned char *to, int len,
int noblock, unsigned flags, struct sockaddr_in *sin,
int *addr_len)
{
int copied = 0;
int truesize;
struct sk_buff *skb;
int er;
/*
* Check any passed addresses
*/
if (addr_len)
*addr_len=sizeof(*sin);
/*
* From here the generic datagram does a lot of the work. Come
* the finished NET3, it will do _ALL_ the work!
*/
skb=skb_recv_datagram(sk,flags,noblock,&er);
if(skb==NULL)
return er;
truesize = skb->len;
copied = min(len, truesize);
/*
* FIXME : should use udp header size info value
*/
skb_copy_datagram(skb,sizeof(struct udphdr),to,copied);
sk->stamp=skb->stamp;
/* Copy the address. */
if (sin)
{
sin->sin_family = AF_INET;
sin->sin_port = skb->h.uh->source;
sin->sin_addr.s_addr = skb->daddr;
}
skb_free_datagram(skb);
release_sock(sk);
return(truesize);
}
/*
* Read has the same semantics as recv in SOCK_DGRAM
*/
int udp_read(struct sock *sk, unsigned char *buff, int len, int noblock,
unsigned flags)
{
return(udp_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
}
int udp_connect(struct sock *sk, struct sockaddr_in *usin, int addr_len)
{
struct rtable *rt;
unsigned long sa;
if (addr_len < sizeof(*usin))
return(-EINVAL);
if (usin->sin_family && usin->sin_family != AF_INET)
return(-EAFNOSUPPORT);
if (usin->sin_addr.s_addr==INADDR_ANY)
usin->sin_addr.s_addr=ip_my_addr();
if(!sk->broadcast && ip_chk_addr(usin->sin_addr.s_addr)==IS_BROADCAST)
return -EACCES; /* Must turn broadcast on first */
rt=ip_rt_route(usin->sin_addr.s_addr, NULL, &sa);
if(rt==NULL)
return -ENETUNREACH;
sk->saddr = sa; /* Update source address */
sk->daddr = usin->sin_addr.s_addr;
sk->dummy_th.dest = usin->sin_port;
sk->state = TCP_ESTABLISHED;
return(0);
}
static void udp_close(struct sock *sk, int timeout)
{
sk->inuse = 1;
sk->state = TCP_CLOSE;
if (sk->dead)
destroy_sock(sk);
else
release_sock(sk);
}
/*
* All we need to do is get the socket, and then do a checksum.
*/
int udp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
unsigned long daddr, unsigned short len,
unsigned long saddr, int redo, struct inet_protocol *protocol)
{
struct sock *sk;
struct udphdr *uh;
unsigned short ulen;
int addr_type = IS_MYADDR;
if(!dev || dev->pa_addr!=daddr)
addr_type=ip_chk_addr(daddr);
/*
* Get the header.
*/
uh = (struct udphdr *) skb->h.uh;
ip_statistics.IpInDelivers++;
/*
* Validate the packet and the UDP length.
*/
ulen = ntohs(uh->len);
if (ulen > len || len < sizeof(*uh) || ulen < sizeof(*uh))
{
printk("UDP: short packet: %d/%d\n", ulen, len);
udp_statistics.UdpInErrors++;
kfree_skb(skb, FREE_WRITE);
return(0);
}
if (uh->check && udp_check(uh, len, saddr, daddr))
{
/* <mea@utu.fi> wants to know, who sent it, to
go and stomp on the garbage sender... */
printk("UDP: bad checksum. From %08lX:%d to %08lX:%d ulen %d\n",
ntohl(saddr),ntohs(uh->source),
ntohl(daddr),ntohs(uh->dest),
ulen);
udp_statistics.UdpInErrors++;
kfree_skb(skb, FREE_WRITE);
return(0);
}
len=ulen;
#ifdef CONFIG_IP_MULTICAST
if (addr_type!=IS_MYADDR)
{
/*
* Multicasts and broadcasts go to each listener.
*/
struct sock *sknext=NULL;
sk=get_sock_mcast(udp_prot.sock_array[ntohs(uh->dest)&(SOCK_ARRAY_SIZE-1)], uh->dest,
saddr, uh->source, daddr);
if(sk)
{
do
{
struct sk_buff *skb1;
sknext=get_sock_mcast(sk->next, uh->dest, saddr, uh->source, daddr);
if(sknext)
skb1=skb_clone(skb,GFP_ATOMIC);
else
skb1=skb;
if(skb1)
udp_deliver(sk, uh, skb1, dev,saddr,daddr,len);
sk=sknext;
}
while(sknext!=NULL);
}
else
kfree_skb(skb, FREE_READ);
return 0;
}
#endif
sk = get_sock(&udp_prot, uh->dest, saddr, uh->source, daddr);
if (sk == NULL)
{
udp_statistics.UdpNoPorts++;
if (addr_type == IS_MYADDR)
{
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0, dev);
}
/*
* Hmm. We got an UDP broadcast to a port to which we
* don't wanna listen. Ignore it.
*/
skb->sk = NULL;
kfree_skb(skb, FREE_WRITE);
return(0);
}
return udp_deliver(sk,uh,skb,dev, saddr, daddr, len);
}
static int udp_deliver(struct sock *sk, struct udphdr *uh, struct sk_buff *skb, struct device *dev, long saddr, long daddr, int len)
{
skb->sk = sk;
skb->dev = dev;
skb->len = len;
/*
* These are supposed to be switched.
*/
skb->daddr = saddr;
skb->saddr = daddr;
/*
* Charge it to the socket, dropping if the queue is full.
*/
skb->len = len - sizeof(*uh);
if (sock_queue_rcv_skb(sk,skb)<0)
{
udp_statistics.UdpInErrors++;
ip_statistics.IpInDiscards++;
ip_statistics.IpInDelivers--;
skb->sk = NULL;
kfree_skb(skb, FREE_WRITE);
release_sock(sk);
return(0);
}
udp_statistics.UdpInDatagrams++;
release_sock(sk);
return(0);
}
struct proto udp_prot = {
sock_wmalloc,
sock_rmalloc,
sock_wfree,
sock_rfree,
sock_rspace,
sock_wspace,
udp_close,
udp_read,
udp_write,
udp_sendto,
udp_recvfrom,
ip_build_header,
udp_connect,
NULL,
ip_queue_xmit,
NULL,
NULL,
NULL,
udp_rcv,
datagram_select,
udp_ioctl,
NULL,
NULL,
ip_setsockopt,
ip_getsockopt,
128,
0,
{NULL,},
"UDP",
0, 0
};
|