import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / ipv4 / protocol.c
1 /*
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  *              INET protocol dispatch tables.
7  *
8  * Version:     $Id: protocol.c,v 1.14 2001/05/18 02:25:49 davem Exp $
9  *
10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12  *
13  * Fixes:
14  *              Alan Cox        : Ahah! udp icmp errors don't work because
15  *                                udp_err is never called!
16  *              Alan Cox        : Added new fields for init and ready for
17  *                                proper fragmentation (_NO_ 4K limits!)
18  *              Richard Colella : Hang on hash collision
19  *              Vince Laviano   : Modified inet_del_protocol() to correctly
20  *                                maintain copy bit.
21  *
22  *              This program is free software; you can redistribute it and/or
23  *              modify it under the terms of the GNU General Public License
24  *              as published by the Free Software Foundation; either version
25  *              2 of the License, or (at your option) any later version.
26  */
27
28 #include <asm/uaccess.h>
29 #include <asm/system.h>
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/string.h>
34 #include <linux/config.h>
35 #include <linux/socket.h>
36 #include <linux/in.h>
37 #include <linux/inet.h>
38 #include <linux/netdevice.h>
39 #include <linux/timer.h>
40 #include <linux/brlock.h>
41 #include <net/ip.h>
42 #include <net/protocol.h>
43 #include <net/tcp.h>
44 #include <linux/skbuff.h>
45 #include <net/sock.h>
46 #include <net/icmp.h>
47 #include <net/udp.h>
48 #include <net/ipip.h>
49 #include <linux/igmp.h>
50
51 #define IPPROTO_PREVIOUS NULL
52
53 #ifdef CONFIG_IP_MULTICAST
54
55 static struct inet_protocol igmp_protocol = {
56         handler:        igmp_rcv,
57         next:           IPPROTO_PREVIOUS,
58         protocol:       IPPROTO_IGMP,
59         name:           "IGMP"
60 };
61
62 #undef  IPPROTO_PREVIOUS
63 #define IPPROTO_PREVIOUS &igmp_protocol
64
65 #endif
66
67 static struct inet_protocol tcp_protocol = {
68         handler:        tcp_v4_rcv,
69         err_handler:    tcp_v4_err,
70         next:           IPPROTO_PREVIOUS,
71         protocol:       IPPROTO_TCP,
72         name:           "TCP"
73 };
74
75 #undef  IPPROTO_PREVIOUS
76 #define IPPROTO_PREVIOUS &tcp_protocol
77
78 static struct inet_protocol udp_protocol = {
79         handler:        udp_rcv,
80         err_handler:    udp_err,
81         next:           IPPROTO_PREVIOUS,
82         protocol:       IPPROTO_UDP,
83         name:           "UDP"
84 };
85
86 #undef  IPPROTO_PREVIOUS
87 #define IPPROTO_PREVIOUS &udp_protocol
88
89 static struct inet_protocol icmp_protocol = {
90         handler:        icmp_rcv,
91         next:           IPPROTO_PREVIOUS,
92         protocol:       IPPROTO_ICMP,
93         name:           "ICMP"
94 };
95
96 #undef  IPPROTO_PREVIOUS
97 #define IPPROTO_PREVIOUS &icmp_protocol
98
99
100 struct inet_protocol *inet_protocol_base = IPPROTO_PREVIOUS;
101
102 struct inet_protocol *inet_protos[MAX_INET_PROTOS];
103
104 /*
105  *      Add a protocol handler to the hash tables
106  */
107
108 void inet_add_protocol(struct inet_protocol *prot)
109 {
110         unsigned char hash;
111         struct inet_protocol *p2;
112
113         hash = prot->protocol & (MAX_INET_PROTOS - 1);
114         br_write_lock_bh(BR_NETPROTO_LOCK);
115         prot ->next = inet_protos[hash];
116         inet_protos[hash] = prot;
117         prot->copy = 0;
118
119         /*
120          *      Set the copy bit if we need to. 
121          */
122          
123         p2 = (struct inet_protocol *) prot->next;
124         while (p2) {
125                 if (p2->protocol == prot->protocol) {
126                         prot->copy = 1;
127                         break;
128                 }
129                 p2 = (struct inet_protocol *) p2->next;
130         }
131         br_write_unlock_bh(BR_NETPROTO_LOCK);
132 }
133
134 /*
135  *      Remove a protocol from the hash tables.
136  */
137  
138 int inet_del_protocol(struct inet_protocol *prot)
139 {
140         struct inet_protocol *p;
141         struct inet_protocol *lp = NULL;
142         unsigned char hash;
143
144         hash = prot->protocol & (MAX_INET_PROTOS - 1);
145         br_write_lock_bh(BR_NETPROTO_LOCK);
146         if (prot == inet_protos[hash]) {
147                 inet_protos[hash] = (struct inet_protocol *) inet_protos[hash]->next;
148                 br_write_unlock_bh(BR_NETPROTO_LOCK);
149                 return 0;
150         }
151
152         p = (struct inet_protocol *) inet_protos[hash];
153
154         if (p != NULL && p->protocol == prot->protocol)
155                 lp = p;
156
157         while (p) {
158                 /*
159                  * We have to worry if the protocol being deleted is
160                  * the last one on the list, then we may need to reset
161                  * someone's copied bit.
162                  */
163                 if (p->next && p->next == prot) {
164                         /*
165                          * if we are the last one with this protocol and
166                          * there is a previous one, reset its copy bit.
167                          */
168                         if (prot->copy == 0 && lp != NULL)
169                                 lp->copy = 0;
170                         p->next = prot->next;
171                         br_write_unlock_bh(BR_NETPROTO_LOCK);
172                         return 0;
173                 }
174                 if (p->next != NULL && p->next->protocol == prot->protocol) 
175                         lp = p->next;
176
177                 p = (struct inet_protocol *) p->next;
178         }
179         br_write_unlock_bh(BR_NETPROTO_LOCK);
180         return -1;
181 }