OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [net/] [ipv4/] [af_inet.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
/*
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
 *              AF_INET protocol family socket handler.
7
 *
8
 * Version:     @(#)af_inet.c   (from sock.c) 1.0.17    06/02/93
9
 *
10
 * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
11
 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12
 *              Florian La Roche, <flla@stud.uni-sb.de>
13
 *              Alan Cox, <A.Cox@swansea.ac.uk>
14
 *
15
 * Changes (see also sock.c)
16
 *
17
 *              A.N.Kuznetsov   :       Socket death error in accept().
18
 *              John Richardson :       Fix non blocking error in connect()
19
 *                                      so sockets that fail to connect
20
 *                                      don't return -EINPROGRESS.
21
 *              Alan Cox        :       Asynchronous I/O support
22
 *              Alan Cox        :       Keep correct socket pointer on sock structures
23
 *                                      when accept() ed
24
 *              Alan Cox        :       Semantics of SO_LINGER aren't state moved
25
 *                                      to close when you look carefully. With
26
 *                                      this fixed and the accept bug fixed
27
 *                                      some RPC stuff seems happier.
28
 *              Niibe Yutaka    :       4.4BSD style write async I/O
29
 *              Alan Cox,
30
 *              Tony Gale       :       Fixed reuse semantics.
31
 *              Alan Cox        :       bind() shouldn't abort existing but dead
32
 *                                      sockets. Stops FTP netin:.. I hope.
33
 *              Alan Cox        :       bind() works correctly for RAW sockets. Note
34
 *                                      that FreeBSD at least was broken in this respect
35
 *                                      so be careful with compatibility tests...
36
 *              Alan Cox        :       routing cache support
37
 *              Alan Cox        :       memzero the socket structure for compactness.
38
 *              Matt Day        :       nonblock connect error handler
39
 *              Alan Cox        :       Allow large numbers of pending sockets
40
 *                                      (eg for big web sites), but only if
41
 *                                      specifically application requested.
42
 *              Alan Cox        :       New buffering throughout IP. Used dumbly.
43
 *              Alan Cox        :       New buffering now used smartly.
44
 *              Alan Cox        :       BSD rather than common sense interpretation of
45
 *                                      listen.
46
 *              Germano Caronni :       Assorted small races.
47
 *              Alan Cox        :       sendmsg/recvmsg basic support.
48
 *              Alan Cox        :       Only sendmsg/recvmsg now supported.
49
 *              Alan Cox        :       Locked down bind (see security list).
50
 *              Alan Cox        :       Loosened bind a little.
51
 *              Mike McLagan    :       ADD/DEL DLCI Ioctls
52
 *      Willy Konynenberg       :       Transparent proxying support.
53
 *              David S. Miller :       New socket lookup architecture for ISS.
54
 *
55
 *              This program is free software; you can redistribute it and/or
56
 *              modify it under the terms of the GNU General Public License
57
 *              as published by the Free Software Foundation; either version
58
 *              2 of the License, or (at your option) any later version.
59
 */
60
 
61
#include <linux/config.h>
62
#include <linux/errno.h>
63
#include <linux/types.h>
64
#include <linux/socket.h>
65
#include <linux/in.h>
66
#include <linux/kernel.h>
67
#include <linux/major.h>
68
#include <linux/sched.h>
69
#include <linux/timer.h>
70
#include <linux/string.h>
71
#include <linux/sockios.h>
72
#include <linux/net.h>
73
#include <linux/fcntl.h>
74
#include <linux/mm.h>
75
#include <linux/interrupt.h>
76
#include <linux/proc_fs.h>
77
#include <linux/stat.h>
78
 
79
#include <asm/segment.h>
80
#include <asm/system.h>
81
 
82
#include <linux/inet.h>
83
#include <linux/netdevice.h>
84
#include <net/ip.h>
85
#include <net/protocol.h>
86
#include <net/arp.h>
87
#include <net/rarp.h>
88
#include <net/route.h>
89
#include <net/tcp.h>
90
#include <net/udp.h>
91
#include <linux/skbuff.h>
92
#include <net/sock.h>
93
#include <net/raw.h>
94
#include <net/icmp.h>
95
#include <linux/ip_fw.h>
96
#ifdef CONFIG_IP_MASQUERADE
97
#include <net/ip_masq.h>
98
#endif
99
#ifdef CONFIG_IP_ALIAS
100
#include <net/ip_alias.h>
101
#endif
102
#ifdef CONFIG_BRIDGE
103
#include <net/br.h>
104
#endif
105
#ifdef CONFIG_KERNELD
106
#include <linux/kerneld.h>
107
#endif
108
#ifdef CONFIG_NET_RADIO
109
#include <linux/wireless.h>
110
#endif  /* CONFIG_NET_RADIO */
111
 
112
#define min(a,b)        ((a)<(b)?(a):(b))
113
 
114
extern struct proto packet_prot;
115
extern int raw_get_info(char *, char **, off_t, int, int);
116
extern int snmp_get_info(char *, char **, off_t, int, int);
117
extern int afinet_get_info(char *, char **, off_t, int, int);
118
extern int tcp_get_info(char *, char **, off_t, int, int);
119
extern int udp_get_info(char *, char **, off_t, int, int);
120
 
121
#ifdef CONFIG_DLCI
122
extern int dlci_ioctl(unsigned int, void*);
123
#endif
124
 
125
#ifdef CONFIG_DLCI_MODULE
126
int (*dlci_ioctl_hook)(unsigned int, void *) = NULL;
127
#endif
128
 
129
int (*rarp_ioctl_hook)(unsigned int,void*) = NULL;
130
 
131
/*
132
 *      Destroy an AF_INET socket
133
 */
134
 
135
static __inline__ void kill_sk_queues(struct sock *sk)
136
{
137
        struct sk_buff *skb;
138
 
139
        while((skb = tcp_dequeue_partial(sk)) != NULL)
140
                kfree_skb(skb, FREE_WRITE);
141
 
142
        /* Next, the write queue. */
143
        while((skb = skb_dequeue(&sk->write_queue)) != NULL)
144
                kfree_skb(skb, FREE_WRITE);
145
 
146
        /* Then, the receive queue. */
147
        while((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
148
                /* This will take care of closing sockets that were
149
                 * listening and didn't accept everything.
150
                 */
151
                if (skb->sk != NULL && skb->sk != sk)
152
                        skb->sk->prot->close(skb->sk, 0);
153
                kfree_skb(skb, FREE_READ);
154
        }
155
 
156
        /*
157
         *      Now we need to clean up the send head.
158
         */
159
 
160
        cli();
161
        for(skb = sk->send_head; skb != NULL; )
162
        {
163
                struct sk_buff *skb2;
164
 
165
                /*
166
                 * We need to remove skb from the transmit queue,
167
                 * or maybe the arp queue.
168
                 */
169
                if (skb->next  && skb->prev)
170
                {
171
                        IS_SKB(skb);
172
                        skb_unlink(skb);
173
                }
174
                skb->dev = NULL;
175
                skb2 = skb->link3;
176
                kfree_skb(skb, FREE_WRITE);
177
                skb = skb2;
178
        }
179
        sk->send_head = NULL;
180
        sk->send_tail = NULL;
181
        sk->send_next = NULL;
182
        sti();
183
 
184
        /* Finally, the backlog. */
185
        while((skb=skb_dequeue(&sk->back_log)) != NULL) {
186
                /* skb->sk = NULL; */
187
                kfree_skb(skb, FREE_READ);
188
        }
189
}
190
 
191
static __inline__ void kill_sk_now(struct sock *sk)
192
{
193
        /* No longer exists. */
194
        del_from_prot_sklist(sk);
195
 
196
        /* This is gross, but needed for SOCK_PACKET -DaveM */
197
        if(sk->prot->unhash)
198
                sk->prot->unhash(sk);
199
 
200
        if(sk->opt)
201
                kfree(sk->opt);
202
        ip_rt_put(sk->ip_route_cache);
203
        sk_free(sk);
204
}
205
 
206
static __inline__ void kill_sk_later(struct sock *sk)
207
{
208
        /* this should never happen. */
209
        /* actually it can if an ack has just been sent. */
210
        /*
211
         * It's more normal than that...
212
         * It can happen because a skb is still in the device queues
213
         * [PR]
214
         */
215
 
216
        NETDEBUG(printk("Socket destroy delayed (r=%d w=%d)\n",
217
                        sk->rmem_alloc, sk->wmem_alloc));
218
 
219
        sk->destroy = 1;
220
        sk->ack_backlog = 0;
221
        release_sock(sk);
222
        reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
223
}
224
 
225
void destroy_sock(struct sock *sk)
226
{
227
        lock_sock(sk);                  /* just to be safe. */
228
 
229
        /*
230
         *      Now we can no longer get new packets or once the
231
         *      timers are killed, send them.
232
         */
233
 
234
        delete_timer(sk);
235
        del_timer(&sk->delack_timer);
236
        del_timer(&sk->retransmit_timer);
237
 
238
        kill_sk_queues(sk);
239
 
240
        /*
241
         *      Now if it has a half accepted/ closed socket.
242
         */
243
 
244
        if (sk->pair)
245
        {
246
                sk->pair->prot->close(sk->pair, 0);
247
                sk->pair = NULL;
248
        }
249
 
250
        /*
251
         * Now if everything is gone we can free the socket
252
         * structure, otherwise we need to keep it around until
253
         * everything is gone.
254
         */
255
 
256
        if (sk->rmem_alloc == 0 && sk->wmem_alloc == 0)
257
                kill_sk_now(sk);
258
        else
259
                kill_sk_later(sk);
260
}
261
 
262
/*
263
 *      The routines beyond this point handle the behaviour of an AF_INET
264
 *      socket object. Mostly it punts to the subprotocols of IP to do
265
 *      the work.
266
 */
267
 
268
static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
269
{
270
        struct sock *sk;
271
 
272
        sk = (struct sock *) sock->data;
273
 
274
        switch(cmd)
275
        {
276
                case F_SETOWN:
277
                        /*
278
                         * This is a little restrictive, but it's the only
279
                         * way to make sure that you can't send a sigurg to
280
                         * another process.
281
                         */
282
                        if (!suser() && current->pgrp != -arg &&
283
                                current->pid != arg) return(-EPERM);
284
                        sk->proc = arg;
285
                        return(0);
286
                case F_GETOWN:
287
                        return(sk->proc);
288
                default:
289
                        return(-EINVAL);
290
        }
291
}
292
 
293
/*
294
 *      Set socket options on an inet socket.
295
 */
296
 
297
static int inet_setsockopt(struct socket *sock, int level, int optname,
298
                    char *optval, int optlen)
299
{
300
        struct sock *sk = (struct sock *) sock->data;
301
        if (level == SOL_SOCKET)
302
                return sock_setsockopt(sk,level,optname,optval,optlen);
303
        if (sk->prot->setsockopt==NULL)
304
                return(-EOPNOTSUPP);
305
        else
306
                return sk->prot->setsockopt(sk,level,optname,optval,optlen);
307
}
308
 
309
/*
310
 *      Get a socket option on an AF_INET socket.
311
 */
312
 
313
static int inet_getsockopt(struct socket *sock, int level, int optname,
314
                    char *optval, int *optlen)
315
{
316
        struct sock *sk = (struct sock *) sock->data;
317
        if (level == SOL_SOCKET)
318
                return sock_getsockopt(sk,level,optname,optval,optlen);
319
        if(sk->prot->getsockopt==NULL)
320
                return(-EOPNOTSUPP);
321
        else
322
                return sk->prot->getsockopt(sk,level,optname,optval,optlen);
323
}
324
 
325
/*
326
 *      Automatically bind an unbound socket.
327
 */
328
 
329
static int inet_autobind(struct sock *sk)
330
{
331
        /* We may need to bind the socket. */
332
        if (sk->num == 0) {
333
                sk->num = sk->prot->good_socknum();
334
                if (sk->num == 0)
335
                        return(-EAGAIN);
336
                sk->dummy_th.source = ntohs(sk->num);
337
                sk->prot->rehash(sk);
338
                add_to_prot_sklist(sk);
339
        }
340
        return 0;
341
}
342
 
343
/*
344
 *      Move a socket into listening state.
345
 */
346
 
347
static int inet_listen(struct socket *sock, int backlog)
348
{
349
        struct sock *sk = (struct sock *) sock->data;
350
 
351
        if(inet_autobind(sk) != 0)
352
                return -EAGAIN;
353
 
354
        /* We might as well re use these. */
355
        /*
356
         * note that the backlog is "unsigned char", so truncate it
357
         * somewhere. We might as well truncate it to what everybody
358
         * else does..
359
         * Now truncate to 128 not 5.
360
         *
361
         * This was wrong, truncate both cases to SOMAXCONN. -DaveM
362
         */
363
        if (((unsigned) backlog == 0) || ((unsigned) backlog > SOMAXCONN))
364
                backlog = SOMAXCONN;
365
        sk->max_ack_backlog = backlog;
366
        if (sk->state != TCP_LISTEN) {
367
                sk->ack_backlog = 0;
368
                sk->state = TCP_LISTEN;
369
                sk->prot->rehash(sk);
370
                add_to_prot_sklist(sk);
371
        }
372
        return(0);
373
}
374
 
375
/*
376
 *      Default callbacks for user INET sockets. These just wake up
377
 *      the user owning the socket.
378
 */
379
 
380
static void def_callback1(struct sock *sk)
381
{
382
        if(!sk->dead)
383
                wake_up_interruptible(sk->sleep);
384
}
385
 
386
static void def_callback2(struct sock *sk,int len)
387
{
388
        if(!sk->dead)
389
        {
390
                wake_up_interruptible(sk->sleep);
391
                sock_wake_async(sk->socket, 1);
392
        }
393
}
394
 
395
static void def_callback3(struct sock *sk)
396
{
397
        if(!sk->dead && sk->wmem_alloc*2 <= sk->sndbuf)
398
        {
399
                wake_up_interruptible(sk->sleep);
400
                sock_wake_async(sk->socket, 2);
401
        }
402
}
403
 
404
/*
405
 *      Create an inet socket.
406
 *
407
 *      FIXME: Gcc would generate much better code if we set the parameters
408
 *      up in in-memory structure order. Gcc68K even more so
409
 */
410
 
411
static int inet_create(struct socket *sock, int protocol)
412
{
413
        struct sock *sk;
414
        struct proto *prot;
415
 
416
        sk = sk_alloc(GFP_KERNEL);
417
        if (sk == NULL)
418
                goto do_oom;
419
#if 0 /* sk_alloc() does this for us. -DaveM */
420
        memset(sk,0,sizeof(*sk));        /* Efficient way to set most fields to zero */
421
#endif
422
        /*
423
         *      Note for tcp that also wiped the dummy_th block for us.
424
         */
425
        if(sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET) {
426
                if (protocol && protocol != IPPROTO_TCP)
427
                        goto free_and_noproto;
428
                protocol = IPPROTO_TCP;
429
                sk->no_check = TCP_NO_CHECK;
430
                prot = &tcp_prot;
431
        } else if(sock->type == SOCK_DGRAM) {
432
                if (protocol && protocol != IPPROTO_UDP)
433
                        goto free_and_noproto;
434
                protocol = IPPROTO_UDP;
435
                sk->no_check = UDP_NO_CHECK;
436
                prot=&udp_prot;
437
        } else if(sock->type == SOCK_RAW || sock->type == SOCK_PACKET) {
438
                if (!suser())
439
                        goto free_and_badperm;
440
                if (!protocol)
441
                        goto free_and_noproto;
442
                prot = &raw_prot;
443
                prot = (sock->type == SOCK_RAW) ? &raw_prot : &packet_prot;
444
                sk->reuse = 1;
445
                sk->num = protocol;
446
        } else {
447
                goto free_and_badtype;
448
        }
449
 
450
        sk->socket = sock;
451
#ifdef CONFIG_TCP_NAGLE_OFF
452
        sk->nonagle = 1;
453
#endif  
454
        sk->type = sock->type;
455
        sk->protocol = protocol;
456
        sk->allocation = GFP_KERNEL;
457
        sk->sndbuf = SK_WMEM_MAX;
458
        sk->rcvbuf = SK_RMEM_MAX;
459
        sk->rto = TCP_TIMEOUT_INIT;             /*TCP_WRITE_TIME*/
460
        sk->cong_window = 1; /* start with only sending one packet at a time. */
461
        sk->ssthresh = 0x7fffffff;
462
        sk->priority = 1;
463
        sk->state = TCP_CLOSE;
464
 
465
        /* this is how many unacked bytes we will accept for this socket.  */
466
        sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
467
        sk->delay_acks = 1;
468
        sk->max_ack_backlog = SOMAXCONN;
469
        skb_queue_head_init(&sk->write_queue);
470
        skb_queue_head_init(&sk->receive_queue);
471
        sk->mtu = 576;
472
        sk->prot = prot;
473
        sk->sleep = sock->wait;
474
        init_timer(&sk->timer);
475
        init_timer(&sk->delack_timer);
476
        init_timer(&sk->retransmit_timer);
477
        sk->timer.data = (unsigned long)sk;
478
        sk->timer.function = &net_timer;
479
        skb_queue_head_init(&sk->back_log);
480
        sock->data =(void *) sk;
481
        sk->ip_ttl=ip_statistics.IpDefaultTTL;
482
        if(sk->type==SOCK_RAW && protocol==IPPROTO_RAW)
483
                sk->ip_hdrincl=1;
484
        else
485
                sk->ip_hdrincl=0;
486
#ifdef CONFIG_IP_MULTICAST
487
        sk->ip_mc_loop=1;
488
        sk->ip_mc_ttl=1;
489
        *sk->ip_mc_name=0;
490
        sk->ip_mc_list=NULL;
491
#endif
492
        /*
493
         *      Speed up by setting some standard state for the dummy_th
494
         *      if TCP uses it (maybe move to tcp_init later)
495
         */
496
 
497
        sk->dummy_th.ack=1;
498
        sk->dummy_th.doff=sizeof(struct tcphdr)>>2;
499
 
500
        sk->state_change = def_callback1;
501
        sk->data_ready = def_callback2;
502
        sk->write_space = def_callback3;
503
        sk->error_report = def_callback1;
504
 
505
        if (sk->num) {
506
                /* It assumes that any protocol which allows
507
                 * the user to assign a number at socket
508
                 * creation time automatically
509
                 * shares.
510
                 */
511
                sk->dummy_th.source = ntohs(sk->num);
512
 
513
                /* This is gross, but needed for SOCK_PACKET -DaveM */
514
                if(sk->prot->hash)
515
                        sk->prot->hash(sk);
516
                add_to_prot_sklist(sk);
517
        }
518
 
519
        if (sk->prot->init) {
520
                int err = sk->prot->init(sk);
521
                if (err != 0) {
522
                        destroy_sock(sk);
523
                        return(err);
524
                }
525
        }
526
        return(0);
527
 
528
free_and_badtype:
529
        sk_free(sk);
530
        return -ESOCKTNOSUPPORT;
531
 
532
free_and_badperm:
533
        sk_free(sk);
534
        return -EPERM;
535
 
536
free_and_noproto:
537
        sk_free(sk);
538
        return -EPROTONOSUPPORT;
539
 
540
do_oom:
541
        return -ENOBUFS;
542
}
543
 
544
 
545
/*
546
 *      Duplicate a socket.
547
 */
548
 
549
static int inet_dup(struct socket *newsock, struct socket *oldsock)
550
{
551
        return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
552
}
553
 
554
/*
555
 *      The peer socket should always be NULL (or else). When we call this
556
 *      function we are destroying the object and from then on nobody
557
 *      should refer to it.
558
 */
559
 
560
static int inet_release(struct socket *sock, struct socket *peer)
561
{
562
        struct sock *sk = (struct sock *) sock->data;
563
 
564
        if (sk) {
565
                unsigned long timeout;
566
 
567
                sk->state_change(sk);
568
 
569
                /* Start closing the connection.  This may take a while. */
570
 
571
#ifdef CONFIG_IP_MULTICAST
572
                /* Applications forget to leave groups before exiting */
573
                ip_mc_drop_socket(sk);
574
#endif
575
                /*
576
                 * If linger is set, we don't return until the close
577
                 * is complete.  Otherwise we return immediately. The
578
                 * actually closing is done the same either way.
579
                 *
580
                 * If the close is due to the process exiting, we never
581
                 * linger..
582
                 */
583
                timeout = 0;
584
                if (sk->linger && !(current->flags & PF_EXITING)) {
585
                        if (sk->lingertime)
586
                                timeout = jiffies + HZ*sk->lingertime;
587
                }
588
 
589
                sock->data = NULL;
590
                sk->socket = NULL;
591
 
592
                sk->prot->close(sk, timeout);
593
        }
594
        return(0);
595
}
596
 
597
 
598
static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
599
{
600
        struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
601
        struct sock *sk=(struct sock *)sock->data;
602
        unsigned short snum;
603
        int chk_addr_ret;
604
 
605
        /* If the socket has its own bind function then use it. (RAW AND PACKET) */
606
        if(sk->prot->bind)
607
                return sk->prot->bind(sk, uaddr, addr_len);
608
 
609
        /* Check these errors (active socket, bad address length, double bind). */
610
        if ((sk->state != TCP_CLOSE)                    ||
611
            (addr_len < sizeof(struct sockaddr_in))     ||
612
            (sk->num != 0))
613
                return -EINVAL;
614
 
615
        snum = ntohs(addr->sin_port);
616
#ifdef CONFIG_IP_MASQUERADE
617
        /* The kernel masquerader needs some ports. */
618
        if(snum>=PORT_MASQ_BEGIN && snum<=PORT_MASQ_END)
619
                return -EADDRINUSE;
620
#endif           
621
        if (snum == 0)
622
                snum = sk->prot->good_socknum();
623
        if (snum < PROT_SOCK) {
624
                if (!suser())
625
                return(-EACCES);
626
                if (snum == 0)
627
                        return(-EAGAIN);
628
        }
629
 
630
        chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
631
        if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR &&
632
            chk_addr_ret != IS_MULTICAST && chk_addr_ret != IS_BROADCAST) {
633
#ifdef CONFIG_IP_TRANSPARENT_PROXY
634
                /* Superuser may bind to any address to allow transparent proxying. */
635
                if(!suser())
636
#endif
637
                        return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
638
        }
639
 
640
        /*
641
         *      We keep a pair of addresses. rcv_saddr is the one
642
         *      used by hash lookups, and saddr is used for transmit.
643
         *
644
         *      In the BSD API these are the same except where it
645
         *      would be illegal to use them (multicast/broadcast) in
646
         *      which case the sending device address is used.
647
         */
648
        sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
649
        if(chk_addr_ret == IS_MULTICAST || chk_addr_ret == IS_BROADCAST)
650
                sk->saddr = 0;  /* Use device */
651
 
652
        /* Make sure we are allowed to bind here. */
653
        if(sk->prot->verify_bind(sk, snum))
654
                return -EADDRINUSE;
655
 
656
        sk->num = snum;
657
        sk->dummy_th.source = ntohs(sk->num);
658
        sk->daddr = 0;
659
        sk->dummy_th.dest = 0;
660
        sk->prot->rehash(sk);
661
        add_to_prot_sklist(sk);
662
 
663
        ip_rt_put(sk->ip_route_cache);
664
        sk->ip_route_cache=NULL;
665
        return(0);
666
}
667
 
668
/*
669
 *      Connect to a remote host. There is regrettably still a little
670
 *      TCP 'magic' in here.
671
 */
672
 
673
static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
674
                  int addr_len, int flags)
675
{
676
        struct sock *sk=(struct sock *)sock->data;
677
        int err;
678
        sock->conn = NULL;
679
 
680
        if (sock->state == SS_CONNECTING && tcp_connected(sk->state)) {
681
                sock->state = SS_CONNECTED;
682
                /* Connection completing after a connect/EINPROGRESS/select/connect */
683
                return 0;        /* Rock and roll */
684
        }
685
 
686
        if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK)) {
687
                if(sk->err!=0)
688
                        return sock_error(sk);
689
                return -EALREADY;       /* Connecting is currently in progress */
690
        }
691
 
692
        if (sock->state != SS_CONNECTING) {
693
                /* We may need to bind the socket. */
694
                if(inet_autobind(sk) != 0)
695
                        return(-EAGAIN);
696
                if (sk->prot->connect == NULL)
697
                        return(-EOPNOTSUPP);
698
                err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
699
                if (err < 0)
700
                        return(err);
701
                sock->state = SS_CONNECTING;
702
        }
703
 
704
        if (sk->state > TCP_FIN_WAIT2 && sock->state==SS_CONNECTING) {
705
                sock->state=SS_UNCONNECTED;
706
                return sock_error(sk);
707
        }
708
 
709
        if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK))
710
                return(-EINPROGRESS);
711
 
712
        cli(); /* avoid the race condition */
713
        while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
714
                interruptible_sleep_on(sk->sleep);
715
                if (current->signal & ~current->blocked) {
716
                        sti();
717
                        return(-ERESTARTSYS);
718
                }
719
                /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
720
                   icmp error packets wanting to close a tcp or udp socket. */
721
                if(sk->err && sk->protocol == IPPROTO_TCP) {
722
                        sock->state = SS_UNCONNECTED;
723
                        sti();
724
                        return sock_error(sk); /* set by tcp_err() */
725
                }
726
        }
727
        sti();
728
        sock->state = SS_CONNECTED;
729
 
730
        if (sk->state != TCP_ESTABLISHED && sk->err) {
731
                sock->state = SS_UNCONNECTED;
732
                return sock_error(sk);
733
        }
734
        return(0);
735
}
736
 
737
 
738
static int inet_socketpair(struct socket *sock1, struct socket *sock2)
739
{
740
         return(-EOPNOTSUPP);
741
}
742
 
743
 
744
/*
745
 *      Accept a pending connection. The TCP layer now gives BSD semantics.
746
 */
747
 
748
static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
749
{
750
        struct sock *sk1, *sk2;
751
        int err;
752
 
753
        sk1 = (struct sock *) sock->data;
754
 
755
        /*
756
         *      We've been passed an extra socket.
757
         *      We need to free it up because the tcp module creates
758
         *      its own when it accepts one.
759
         */
760
 
761
        if (newsock->data) {
762
                struct sock *sk=(struct sock *)newsock->data;
763
                newsock->data=NULL;
764
                destroy_sock(sk);
765
        }
766
 
767
        if (sk1->prot->accept == NULL)
768
                return(-EOPNOTSUPP);
769
 
770
        /*
771
         *      Restore the state if we have been interrupted, and then returned.
772
         */
773
 
774
        if (sk1->pair != NULL) {
775
                sk2 = sk1->pair;
776
                sk1->pair = NULL;
777
        } else {
778
                sk2 = sk1->prot->accept(sk1,flags);
779
                if (sk2 == NULL)
780
                        return sock_error(sk1);
781
        }
782
        newsock->data = (void *)sk2;
783
        sk2->sleep = newsock->wait;
784
        sk2->socket = newsock;
785
        newsock->conn = NULL;
786
        if (flags & O_NONBLOCK)
787
                return(0);
788
 
789
        cli(); /* avoid the race. */
790
        while(sk2->state == TCP_SYN_RECV) {
791
                interruptible_sleep_on(sk2->sleep);
792
                if (current->signal & ~current->blocked) {
793
                        sti();
794
                        sk1->pair = sk2;
795
                        sk2->sleep = NULL;
796
                        sk2->socket=NULL;
797
                        newsock->data = NULL;
798
                        return(-ERESTARTSYS);
799
                }
800
        }
801
        sti();
802
 
803
        if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) {
804
                err = sock_error(sk2);
805
                destroy_sock(sk2);
806
                newsock->data = NULL;
807
                return err;
808
        }
809
 
810
        if (sk2->state == TCP_CLOSE) {
811
                destroy_sock(sk2);
812
                newsock->data=NULL;
813
                return -ECONNABORTED;
814
        }
815
        newsock->state = SS_CONNECTED;
816
        return(0);
817
}
818
 
819
 
820
/*
821
 *      This does both peername and sockname.
822
 */
823
 
824
static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
825
                 int *uaddr_len, int peer)
826
{
827
        struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
828
        struct sock *sk;
829
 
830
        sin->sin_family = AF_INET;
831
        sk = (struct sock *) sock->data;
832
        if (peer) {
833
                if (!tcp_connected(sk->state))
834
                        return(-ENOTCONN);
835
                sin->sin_port = sk->dummy_th.dest;
836
                sin->sin_addr.s_addr = sk->daddr;
837
        } else {
838
                __u32 addr = sk->rcv_saddr;
839
                if (!addr) {
840
                        addr = sk->saddr;
841
                }
842
                sin->sin_port = sk->dummy_th.source;
843
                sin->sin_addr.s_addr = addr;
844
        }
845
        *uaddr_len = sizeof(*sin);
846
        return(0);
847
}
848
 
849
 
850
 
851
static int inet_recvmsg(struct socket *sock, struct msghdr *ubuf, int size, int noblock,
852
                   int flags, int *addr_len )
853
{
854
        struct sock *sk = (struct sock *) sock->data;
855
 
856
        if (sk->prot->recvmsg == NULL)
857
                return(-EOPNOTSUPP);
858
        if(sk->err)
859
                return sock_error(sk);
860
 
861
        /* We may need to bind the socket. */
862
        if(inet_autobind(sk) != 0)
863
                return(-EAGAIN);
864
 
865
        return(sk->prot->recvmsg(sk, ubuf, size, noblock, flags,addr_len));
866
}
867
 
868
 
869
static int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size, int noblock,
870
           int flags)
871
{
872
        struct sock *sk = (struct sock *) sock->data;
873
        if (sk->shutdown & SEND_SHUTDOWN) {
874
                send_sig(SIGPIPE, current, 1);
875
                return(-EPIPE);
876
        }
877
        if (sk->prot->sendmsg == NULL)
878
                return(-EOPNOTSUPP);
879
        if(sk->err)
880
                return sock_error(sk);
881
 
882
        /* We may need to bind the socket. */
883
        if(inet_autobind(sk) != 0)
884
                return -EAGAIN;
885
 
886
        return(sk->prot->sendmsg(sk, msg, size, noblock, flags));
887
 
888
}
889
 
890
 
891
static int inet_shutdown(struct socket *sock, int how)
892
{
893
        struct sock *sk=(struct sock*)sock->data;
894
 
895
        /*
896
         * This should really check to make sure
897
         * the socket is a TCP socket. (WHY AC...)
898
         */
899
        how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
900
                       1->2 bit 2 snds.
901
                       2->3 */
902
        if ((how & ~SHUTDOWN_MASK) || how==0)    /* MAXINT->0 */
903
                return(-EINVAL);
904
        if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
905
                sock->state = SS_CONNECTED;
906
        if (!sk || !tcp_connected(sk->state))
907
                return(-ENOTCONN);
908
        sk->shutdown |= how;
909
        if (sk->prot->shutdown)
910
                sk->prot->shutdown(sk, how);
911
        return(0);
912
}
913
 
914
 
915
static int inet_select(struct socket *sock, int sel_type, select_table *wait )
916
{
917
        struct sock *sk=(struct sock *) sock->data;
918
        if (sk->prot->select == NULL)
919
                return(0);
920
 
921
        return(sk->prot->select(sk, sel_type, wait));
922
}
923
 
924
/*
925
 *      ioctl() calls you can issue on an INET socket. Most of these are
926
 *      device configuration and stuff and very rarely used. Some ioctls
927
 *      pass on to the socket itself.
928
 *
929
 *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
930
 *      loads the devconfigure module does its configuring and unloads it.
931
 *      There's a good 20K of config code hanging around the kernel.
932
 */
933
 
934
static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
935
{
936
        struct sock *sk=(struct sock *)sock->data;
937
        int err;
938
        int pid;
939
 
940
        switch(cmd)
941
        {
942
                case FIOSETOWN:
943
                case SIOCSPGRP:
944
                        err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
945
                        if(err)
946
                                return err;
947
                        pid = get_user((int *) arg);
948
                        /* see inet_fcntl */
949
                        if (current->pid != pid && current->pgrp != -pid && !suser())
950
                                return -EPERM;
951
                        sk->proc = pid;
952
                        return(0);
953
                case FIOGETOWN:
954
                case SIOCGPGRP:
955
                        err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
956
                        if(err)
957
                                return err;
958
                        put_fs_long(sk->proc,(int *)arg);
959
                        return(0);
960
                case SIOCGSTAMP:
961
                        if(sk->stamp.tv_sec==0)
962
                                return -ENOENT;
963
                        err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
964
                        if(err)
965
                                return err;
966
                        memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
967
                        return 0;
968
                case SIOCADDRT:
969
                case SIOCDELRT:
970
                        return(ip_rt_ioctl(cmd,(void *) arg));
971
                case SIOCDARP:
972
                case SIOCGARP:
973
                case SIOCSARP:
974
                case OLD_SIOCDARP:
975
                case OLD_SIOCGARP:
976
                case OLD_SIOCSARP:
977
                        return(arp_ioctl(cmd,(void *) arg));
978
                case SIOCDRARP:
979
                case SIOCGRARP:
980
                case SIOCSRARP:
981
#ifdef CONFIG_KERNELD
982
                        if (rarp_ioctl_hook == NULL)
983
                                request_module("rarp");
984
#endif
985
                        if (rarp_ioctl_hook != NULL)
986
                                return(rarp_ioctl_hook(cmd,(void *) arg));
987
                case SIOCGIFCONF:
988
                case SIOCGIFFLAGS:
989
                case SIOCSIFFLAGS:
990
                case SIOCGIFADDR:
991
                case SIOCSIFADDR:
992
                case SIOCADDMULTI:
993
                case SIOCDELMULTI:
994
                case SIOCGIFDSTADDR:
995
                case SIOCSIFDSTADDR:
996
                case SIOCGIFBRDADDR:
997
                case SIOCSIFBRDADDR:
998
                case SIOCGIFNETMASK:
999
                case SIOCSIFNETMASK:
1000
                case SIOCGIFMETRIC:
1001
                case SIOCSIFMETRIC:
1002
                case SIOCGIFMEM:
1003
                case SIOCSIFMEM:
1004
                case SIOCGIFMTU:
1005
                case SIOCSIFMTU:
1006
                case SIOCSIFLINK:
1007
                case SIOCGIFHWADDR:
1008
                case SIOCSIFHWADDR:
1009
                case SIOCSIFMAP:
1010
                case SIOCGIFMAP:
1011
                case SIOCSIFSLAVE:
1012
                case SIOCGIFSLAVE:
1013
                        return(dev_ioctl(cmd,(void *) arg));
1014
 
1015
                case SIOCGIFBR:
1016
                case SIOCSIFBR:
1017
#ifdef CONFIG_BRIDGE            
1018
                        return(br_ioctl(cmd,(void *) arg));
1019
#else
1020
                        return -ENOPKG;
1021
#endif                                          
1022
 
1023
                case SIOCADDDLCI:
1024
                case SIOCDELDLCI:
1025
#ifdef CONFIG_DLCI
1026
                        return(dlci_ioctl(cmd, (void *) arg));
1027
#endif
1028
 
1029
#ifdef CONFIG_DLCI_MODULE
1030
 
1031
#ifdef CONFIG_KERNELD
1032
                        if (dlci_ioctl_hook == NULL)
1033
                                request_module("dlci");
1034
#endif
1035
 
1036
                        if (dlci_ioctl_hook)
1037
                                return((*dlci_ioctl_hook)(cmd, (void *) arg));
1038
#endif
1039
                        return -ENOPKG;
1040
 
1041
                default:
1042
                        if ((cmd >= SIOCDEVPRIVATE) &&
1043
                           (cmd <= (SIOCDEVPRIVATE + 15)))
1044
                                return(dev_ioctl(cmd,(void *) arg));
1045
 
1046
#ifdef CONFIG_NET_RADIO
1047
                        if((cmd >= SIOCIWFIRST) &&
1048
                           (cmd <= SIOCIWLAST))
1049
                                return(dev_ioctl(cmd,(void *) arg));
1050
#endif  /* CONFIG_NET_RADIO */
1051
 
1052
                        if (sk->prot->ioctl==NULL)
1053
                                return(-EINVAL);
1054
                        return(sk->prot->ioctl(sk, cmd, arg));
1055
        }
1056
        /*NOTREACHED*/
1057
        return(0);
1058
}
1059
 
1060
static struct proto_ops inet_proto_ops = {
1061
        AF_INET,
1062
 
1063
        inet_create,
1064
        inet_dup,
1065
        inet_release,
1066
        inet_bind,
1067
        inet_connect,
1068
        inet_socketpair,
1069
        inet_accept,
1070
        inet_getname,
1071
        inet_select,
1072
        inet_ioctl,
1073
        inet_listen,
1074
        inet_shutdown,
1075
        inet_setsockopt,
1076
        inet_getsockopt,
1077
        inet_fcntl,
1078
        inet_sendmsg,
1079
        inet_recvmsg
1080
};
1081
 
1082
extern unsigned long seq_offset;
1083
 
1084
/*
1085
 *      Called by socket.c on kernel startup.
1086
 */
1087
 
1088
#ifdef CONFIG_PROC_FS
1089
 
1090
#ifdef CONFIG_INET_RARP
1091
static struct proc_dir_entry pde1 = {
1092
                PROC_NET_RARP, 4, "rarp",
1093
                S_IFREG | S_IRUGO, 1, 0, 0,
1094
                0, &proc_net_inode_operations,
1095
                rarp_get_info
1096
        };
1097
#endif          /* RARP */
1098
 
1099
static struct proc_dir_entry pde2 = {
1100
                PROC_NET_RAW, 3, "raw",
1101
                S_IFREG | S_IRUGO, 1, 0, 0,
1102
                0, &proc_net_inode_operations,
1103
                raw_get_info
1104
        };
1105
static struct proc_dir_entry pde3 = {
1106
                PROC_NET_SNMP, 4, "snmp",
1107
                S_IFREG | S_IRUGO, 1, 0, 0,
1108
                0, &proc_net_inode_operations,
1109
                snmp_get_info
1110
        };
1111
static struct proc_dir_entry pde4 = {
1112
                PROC_NET_SOCKSTAT, 8, "sockstat",
1113
                S_IFREG | S_IRUGO, 1, 0, 0,
1114
                0, &proc_net_inode_operations,
1115
                afinet_get_info
1116
        };
1117
static struct proc_dir_entry pde5 = {
1118
                PROC_NET_TCP, 3, "tcp",
1119
                S_IFREG | S_IRUGO, 1, 0, 0,
1120
                0, &proc_net_inode_operations,
1121
                tcp_get_info
1122
        };
1123
static struct proc_dir_entry pde6 = {
1124
                PROC_NET_UDP, 3, "udp",
1125
                S_IFREG | S_IRUGO, 1, 0, 0,
1126
                0, &proc_net_inode_operations,
1127
                udp_get_info
1128
        };
1129
static struct proc_dir_entry pde7 = {
1130
                PROC_NET_ROUTE, 5, "route",
1131
                S_IFREG | S_IRUGO, 1, 0, 0,
1132
                0, &proc_net_inode_operations,
1133
                rt_get_info
1134
        };
1135
static struct proc_dir_entry pde8 = {
1136
                PROC_NET_RTCACHE, 8, "rt_cache",
1137
                S_IFREG | S_IRUGO, 1, 0, 0,
1138
                0, &proc_net_inode_operations,
1139
                rt_cache_get_info
1140
        };
1141
#endif          /* CONFIG_PROC_FS */
1142
 
1143
 
1144
void inet_proto_init(struct net_proto *pro)
1145
{
1146
        struct inet_protocol *p;
1147
 
1148
        printk("Swansea University Computer Society TCP/IP for NET3.034\n");
1149
 
1150
        /*
1151
         *      Tell SOCKET that we are alive...
1152
         */
1153
 
1154
        (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1155
 
1156
        seq_offset = CURRENT_TIME*250;
1157
 
1158
        /*
1159
         *      Add all the protocols.
1160
         */
1161
 
1162
        printk("IP Protocols: ");
1163
        for(p = inet_protocol_base; p != NULL;)
1164
        {
1165
                struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1166
                inet_add_protocol(p);
1167
                printk("%s%s",p->name,tmp?", ":"\n");
1168
                p = tmp;
1169
        }
1170
 
1171
        /*
1172
         *      Set the ARP module up
1173
         */
1174
        arp_init();
1175
        /*
1176
         *      Set the IP module up
1177
         */
1178
        ip_init();
1179
        /*
1180
         *      Set the ICMP layer up
1181
         */
1182
        icmp_init(&inet_proto_ops);
1183
        /*
1184
         *      Set the firewalling up
1185
         */
1186
#if defined(CONFIG_IP_ACCT)||defined(CONFIG_IP_FIREWALL)|| \
1187
    defined(CONFIG_IP_MASQUERADE)
1188
        ip_fw_init();
1189
#endif
1190
        /*
1191
         *      Initialise the multicast router
1192
         */
1193
#if defined(CONFIG_IP_MROUTE)
1194
        ip_mr_init();
1195
#endif
1196
 
1197
        /*
1198
         *  Initialise AF_INET alias type (register net_alias_type)
1199
         */
1200
 
1201
#if defined(CONFIG_IP_ALIAS)
1202
        ip_alias_init();
1203
#endif
1204
 
1205
#ifdef CONFIG_INET_RARP
1206
        rarp_ioctl_hook = rarp_ioctl;
1207
#endif
1208
        /*
1209
         *      Create all the /proc entries.
1210
         */
1211
 
1212
#ifdef CONFIG_PROC_FS
1213
 
1214
#ifdef CONFIG_INET_RARP
1215
        proc_net_register(&pde1);
1216
#endif          /* RARP */
1217
 
1218
        proc_net_register(&pde2);
1219
        proc_net_register(&pde3);
1220
        proc_net_register(&pde4);
1221
        proc_net_register(&pde5);
1222
        proc_net_register(&pde6);
1223
        proc_net_register(&pde7);
1224
        proc_net_register(&pde8);
1225
#endif          /* CONFIG_PROC_FS */
1226
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.