1 /*
2 * NET3 IP device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the IP parts of dev.c 1.0.19
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 */
17
18 #include <asm/segment.h>
19 #include <asm/system.h>
20 #include <asm/bitops.h>
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/in.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/if_ether.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include "ip.h"
37 #include "route.h"
38 #include "protocol.h"
39 #include "tcp.h"
40 #include <linux/skbuff.h>
41 #include "sock.h"
42 #include "arp.h"
43
44 /*
45 * Determine a default network mask, based on the IP address.
46 */
47
48 unsigned long ip_get_mask(unsigned long addr)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
49 {
50 unsigned long dst;
51
52 if (addr == 0L)
53 return(0L); /* special case */
54
55 dst = ntohl(addr);
56 if (IN_CLASSA(dst))
57 return(htonl(IN_CLASSA_NET));
58 if (IN_CLASSB(dst))
59 return(htonl(IN_CLASSB_NET));
60 if (IN_CLASSC(dst))
61 return(htonl(IN_CLASSC_NET));
62
63 /*
64 * Something else, probably a multicast.
65 */
66
67 return(0);
68 }
69
70 /*
71 * Check the address for our address, broadcasts, etc.
72 *
73 * I intend to fix this to at the very least cache the last
74 * resolved entry.
75 */
76
77 int ip_chk_addr(unsigned long addr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
78 {
79 struct device *dev;
80 unsigned long mask;
81
82 /*
83 * Accept both `all ones' and `all zeros' as BROADCAST.
84 * (Support old BSD in other words). This old BSD
85 * support will go very soon as it messes other things
86 * up.
87 * Also accept `loopback broadcast' as BROADCAST.
88 */
89
90 if (addr == INADDR_ANY || addr == INADDR_BROADCAST ||
91 addr == htonl(0x7FFFFFFFL))
92 return IS_BROADCAST;
93
94 mask = ip_get_mask(addr);
95
96 /*
97 * Accept all of the `loopback' class A net.
98 */
99
100 if ((addr & mask) == htonl(0x7F000000L))
101 return IS_MYADDR;
102
103 /*
104 * OK, now check the interface addresses.
105 */
106
107 for (dev = dev_base; dev != NULL; dev = dev->next)
108 {
109 if (!(dev->flags & IFF_UP))
110 continue;
111 /*
112 * If the protocol address of the device is 0 this is special
113 * and means we are address hunting (eg bootp).
114 */
115
116 if ((dev->pa_addr == 0)/* || (dev->flags&IFF_PROMISC)*/)
117 return IS_MYADDR;
118 /*
119 * Is it the exact IP address?
120 */
121
122 if (addr == dev->pa_addr)
123 return IS_MYADDR;
124 /*
125 * Is it our broadcast address?
126 */
127
128 if ((dev->flags & IFF_BROADCAST) && addr == dev->pa_brdaddr)
129 return IS_BROADCAST;
130 /*
131 * Nope. Check for a subnetwork broadcast.
132 */
133
134 if (((addr ^ dev->pa_addr) & dev->pa_mask) == 0)
135 {
136 if ((addr & ~dev->pa_mask) == 0)
137 return IS_BROADCAST;
138 if ((addr & ~dev->pa_mask) == ~dev->pa_mask)
139 return IS_BROADCAST;
140 }
141
142 /*
143 * Nope. Check for Network broadcast.
144 */
145
146 if (((addr ^ dev->pa_addr) & mask) == 0)
147 {
148 if ((addr & ~mask) == 0)
149 return IS_BROADCAST;
150 if ((addr & ~mask) == ~mask)
151 return IS_BROADCAST;
152 }
153 }
154 if(IN_MULTICAST(ntohl(addr)))
155 return IS_MULTICAST;
156 return 0; /* no match at all */
157 }
158
159
160 /*
161 * Retrieve our own address.
162 *
163 * Because the loopback address (127.0.0.1) is already recognized
164 * automatically, we can use the loopback interface's address as
165 * our "primary" interface. This is the address used by IP et
166 * al when it doesn't know which address to use (i.e. it does not
167 * yet know from or to which interface to go...).
168 */
169
170 unsigned long ip_my_addr(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
171 {
172 struct device *dev;
173
174 for (dev = dev_base; dev != NULL; dev = dev->next)
175 {
176 if (dev->flags & IFF_LOOPBACK)
177 return(dev->pa_addr);
178 }
179 return(0);
180 }
181
182 /*
183 * Find an interface that can handle addresses for a certain address.
184 *
185 * This needs optimising, since its relatively trivial to collapse
186 * the two loops into one.
187 */
188
189 struct device * ip_dev_check(unsigned long addr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
190 {
191 struct device *dev;
192
193 for (dev = dev_base; dev; dev = dev->next)
194 {
195 if (!(dev->flags & IFF_UP))
196 continue;
197 if (!(dev->flags & IFF_POINTOPOINT))
198 continue;
199 if (addr != dev->pa_dstaddr)
200 continue;
201 return dev;
202 }
203 for (dev = dev_base; dev; dev = dev->next)
204 {
205 if (!(dev->flags & IFF_UP))
206 continue;
207 if (dev->flags & IFF_POINTOPOINT)
208 continue;
209 if (dev->pa_mask & (addr ^ dev->pa_addr))
210 continue;
211 return dev;
212 }
213 return NULL;
214 }