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/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
25 #include <linux/mm.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/in.h>
29 #include <linux/errno.h>
30 #include <linux/interrupt.h>
31 #include <linux/if_ether.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <net/ip.h>
36 #include <net/route.h>
37 #include <net/protocol.h>
38 #include <net/tcp.h>
39 #include <linux/skbuff.h>
40 #include <net/sock.h>
41 #include <net/arp.h>
42
43 /* 44 * Determine a default network mask, based on the IP address. 45 */ 46
47 unsignedlongip_get_mask(unsignedlongaddr)
/* */ 48 { 49 unsignedlongdst;
50
51 if (addr == 0L)
52 return(0L); /* special case */ 53
54 dst = ntohl(addr);
55 if (IN_CLASSA(dst))
56 return(htonl(IN_CLASSA_NET));
57 if (IN_CLASSB(dst))
58 return(htonl(IN_CLASSB_NET));
59 if (IN_CLASSC(dst))
60 return(htonl(IN_CLASSC_NET));
61
62 /* 63 * Something else, probably a multicast. 64 */ 65
66 return(0);
67 } 68
69 /* 70 * Check the address for our address, broadcasts, etc. 71 * 72 * I intend to fix this to at the very least cache the last 73 * resolved entry. 74 */ 75
76 intip_chk_addr(unsignedlongaddr)
/* */ 77 { 78 structdevice *dev;
79 unsignedlongmask;
80
81 /* 82 * Accept both `all ones' and `all zeros' as BROADCAST. 83 * (Support old BSD in other words). This old BSD 84 * support will go very soon as it messes other things 85 * up. 86 * Also accept `loopback broadcast' as BROADCAST. 87 */ 88
89 if (addr == INADDR_ANY || addr == INADDR_BROADCAST ||
90 addr == htonl(0x7FFFFFFFL))
91 returnIS_BROADCAST;
92
93 mask = ip_get_mask(addr);
94
95 /* 96 * Accept all of the `loopback' class A net. 97 */ 98
99 if ((addr & mask) == htonl(0x7F000000L))
100 returnIS_MYADDR;
101
102 /* 103 * OK, now check the interface addresses. We could 104 * speed this by keeping a dev and a dev_up chain. 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 returnIS_MYADDR;
118 /* 119 * Is it the exact IP address? 120 */ 121
122 if (addr == dev->pa_addr)
123 returnIS_MYADDR;
124 /* 125 * Is it our broadcast address? 126 */ 127
128 if ((dev->flags & IFF_BROADCAST) && addr == dev->pa_brdaddr)
129 returnIS_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 returnIS_BROADCAST;
138 if ((addr & ~dev->pa_mask) == ~dev->pa_mask)
139 returnIS_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 returnIS_BROADCAST;
150 if ((addr & ~mask) == ~mask)
151 returnIS_BROADCAST;
152 } 153 } 154 if(IN_MULTICAST(ntohl(addr)))
155 returnIS_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 unsignedlongip_my_addr(void)
/* */ 171 { 172 structdevice *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 it's relatively trivial to collapse 186 * the two loops into one. 187 */ 188
189 structdevice * ip_dev_check(unsignedlongaddr)
/* */ 190 { 191 structdevice *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 returndev;
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 returndev;
212 } 213 returnNULL;
214 } 215