[9] | 1 | /* this file contains the interface of the network software with rest of
|
---|
| 2 | minix. Furthermore it contains the main loop of the network task.
|
---|
| 3 |
|
---|
| 4 | Copyright 1995 Philip Homburg
|
---|
| 5 |
|
---|
| 6 | The valid messages and their parameters are:
|
---|
| 7 |
|
---|
| 8 | from FS:
|
---|
| 9 | __________________________________________________________________
|
---|
| 10 | | | | | | | |
|
---|
| 11 | | m_type | DEVICE | PROC_NR | COUNT | POSITION | ADDRESS |
|
---|
| 12 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 13 | | | | | | | |
|
---|
| 14 | | NW_OPEN | minor dev | proc nr | mode | | |
|
---|
| 15 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 16 | | | | | | | |
|
---|
| 17 | | NW_CLOSE | minor dev | proc nr | | | |
|
---|
| 18 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 19 | | | | | | | |
|
---|
| 20 | | NW_IOCTL | minor dev | proc nr | | NWIO.. | address |
|
---|
| 21 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 22 | | | | | | | |
|
---|
| 23 | | NW_READ | minor dev | proc nr | count | | address |
|
---|
| 24 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 25 | | | | | | | |
|
---|
| 26 | | NW_WRITE | minor dev | proc nr | count | | address |
|
---|
| 27 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 28 | | | | | | | |
|
---|
| 29 | | NW_CANCEL | minor dev | proc nr | | | |
|
---|
| 30 | |_______________|___________|_________|_______|__________|_________|
|
---|
| 31 |
|
---|
| 32 | from DL_ETH:
|
---|
| 33 | _______________________________________________________________________
|
---|
| 34 | | | | | | | |
|
---|
| 35 | | m_type | DL_PORT | DL_PROC | DL_COUNT | DL_STAT | DL_TIME |
|
---|
| 36 | |_______________|___________|_________|__________|____________|_________|
|
---|
| 37 | | | | | | | |
|
---|
| 38 | | DL_INIT_REPLY | minor dev | proc nr | rd_count | 0 | stat | time |
|
---|
| 39 | |_______________|___________|_________|__________|____________|_________|
|
---|
| 40 | | | | | | | |
|
---|
| 41 | | DL_TASK_REPLY | minor dev | proc nr | rd_count | err | stat | time |
|
---|
| 42 | |_______________|___________|_________|__________|____________|_________|
|
---|
| 43 | */
|
---|
| 44 |
|
---|
| 45 | #include "inet.h"
|
---|
| 46 |
|
---|
| 47 | #define _MINIX_SOURCE 1
|
---|
| 48 |
|
---|
| 49 | #include <fcntl.h>
|
---|
| 50 | #include <time.h>
|
---|
| 51 | #include <unistd.h>
|
---|
| 52 | #include <sys/svrctl.h>
|
---|
| 53 |
|
---|
| 54 | #include "mq.h"
|
---|
| 55 | #include "qp.h"
|
---|
| 56 | #include "proto.h"
|
---|
| 57 | #include "generic/type.h"
|
---|
| 58 |
|
---|
| 59 | #include "generic/arp.h"
|
---|
| 60 | #include "generic/assert.h"
|
---|
| 61 | #include "generic/buf.h"
|
---|
| 62 | #include "generic/clock.h"
|
---|
| 63 | #include "generic/eth.h"
|
---|
| 64 | #include "generic/event.h"
|
---|
| 65 | #include "generic/ip.h"
|
---|
| 66 | #include "generic/psip.h"
|
---|
| 67 | #include "generic/rand256.h"
|
---|
| 68 | #include "generic/sr.h"
|
---|
| 69 | #include "generic/tcp.h"
|
---|
| 70 | #include "generic/udp.h"
|
---|
| 71 |
|
---|
| 72 | THIS_FILE
|
---|
| 73 |
|
---|
| 74 | #define RANDOM_DEV_NAME "/dev/random"
|
---|
| 75 |
|
---|
| 76 | int this_proc; /* Process number of this server. */
|
---|
| 77 |
|
---|
| 78 | #ifdef __minix_vmd
|
---|
| 79 | static int synal_tasknr= ANY;
|
---|
| 80 | #endif
|
---|
| 81 |
|
---|
| 82 | /* Killing Solaris */
|
---|
| 83 | int killer_inet= 0;
|
---|
| 84 |
|
---|
| 85 | #ifdef BUF_CONSISTENCY_CHECK
|
---|
| 86 | extern int inet_buf_debug;
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
| 89 | _PROTOTYPE( void main, (void) );
|
---|
| 90 |
|
---|
| 91 | FORWARD _PROTOTYPE( void nw_conf, (void) );
|
---|
| 92 | FORWARD _PROTOTYPE( void nw_init, (void) );
|
---|
| 93 |
|
---|
| 94 | PUBLIC void main()
|
---|
| 95 | {
|
---|
| 96 | mq_t *mq;
|
---|
| 97 | int r;
|
---|
| 98 | int source, timerand, fd;
|
---|
| 99 | struct fssignon device;
|
---|
| 100 | #ifdef __minix_vmd
|
---|
| 101 | struct systaskinfo info;
|
---|
| 102 | #endif
|
---|
| 103 | u8_t randbits[32];
|
---|
| 104 | struct timeval tv;
|
---|
| 105 |
|
---|
| 106 | #if DEBUG
|
---|
| 107 | printf("Starting inet...\n");
|
---|
| 108 | printf("%s\n", version);
|
---|
| 109 | #endif
|
---|
| 110 |
|
---|
| 111 | /* Read configuration. */
|
---|
| 112 | nw_conf();
|
---|
| 113 |
|
---|
| 114 | /* Get a random number */
|
---|
| 115 | timerand= 1;
|
---|
| 116 | fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
|
---|
| 117 | if (fd != -1)
|
---|
| 118 | {
|
---|
| 119 | r= read(fd, randbits, sizeof(randbits));
|
---|
| 120 | if (r == sizeof(randbits))
|
---|
| 121 | timerand= 0;
|
---|
| 122 | else
|
---|
| 123 | {
|
---|
| 124 | printf("unable to read random data from %s: %s\n",
|
---|
| 125 | RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
|
---|
| 126 | r == 0 ? "EOF" : "not enough data");
|
---|
| 127 | }
|
---|
| 128 | close(fd);
|
---|
| 129 | }
|
---|
| 130 | else
|
---|
| 131 | {
|
---|
| 132 | printf("unable to open random device %s: %s\n",
|
---|
| 133 | RANDOM_DEV_NAME, strerror(errno));
|
---|
| 134 | }
|
---|
| 135 | if (timerand)
|
---|
| 136 | {
|
---|
| 137 | printf("using current time for random-number seed\n");
|
---|
| 138 | #ifdef __minix_vmd
|
---|
| 139 | r= sysutime(UTIME_TIMEOFDAY, &tv);
|
---|
| 140 | #else /* Minix 3 */
|
---|
| 141 | r= gettimeofday(&tv, NULL);
|
---|
| 142 | #endif
|
---|
| 143 | if (r == -1)
|
---|
| 144 | {
|
---|
| 145 | printf("sysutime failed: %s\n", strerror(errno));
|
---|
| 146 | exit(1);
|
---|
| 147 | }
|
---|
| 148 | memcpy(randbits, &tv, sizeof(tv));
|
---|
| 149 | }
|
---|
| 150 | init_rand256(randbits);
|
---|
| 151 |
|
---|
| 152 | #ifdef __minix_vmd
|
---|
| 153 | if (svrctl(SYSSIGNON, (void *) &info) == -1) pause();
|
---|
| 154 |
|
---|
| 155 | /* Our new identity as a server. */
|
---|
| 156 | this_proc = info.proc_nr;
|
---|
| 157 | #else /* Minix 3 */
|
---|
| 158 |
|
---|
| 159 | /* Our new identity as a server. */
|
---|
| 160 | if ((this_proc = getprocnr()) < 0)
|
---|
| 161 | ip_panic(( "unable to get own process nr\n"));
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 | /* Register the device group. */
|
---|
| 165 | device.dev= ip_dev;
|
---|
| 166 | device.style= STYLE_CLONE;
|
---|
| 167 | if (svrctl(FSSIGNON, (void *) &device) == -1) {
|
---|
| 168 | printf("inet: error %d on registering ethernet devices\n",
|
---|
| 169 | errno);
|
---|
| 170 | pause();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | #ifdef BUF_CONSISTENCY_CHECK
|
---|
| 174 | inet_buf_debug= (getenv("inetbufdebug") &&
|
---|
| 175 | (strcmp(getenv("inetbufdebug"), "on") == 0));
|
---|
| 176 | inet_buf_debug= 100;
|
---|
| 177 | if (inet_buf_debug)
|
---|
| 178 | {
|
---|
| 179 | ip_warning(( "buffer consistency check enabled" ));
|
---|
| 180 | }
|
---|
| 181 | #endif
|
---|
| 182 |
|
---|
| 183 | if (getenv("killerinet"))
|
---|
| 184 | {
|
---|
| 185 | ip_warning(( "killer inet active" ));
|
---|
| 186 | killer_inet= 1;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | #ifdef __minix_vmd
|
---|
| 190 | r= sys_findproc(SYN_AL_NAME, &synal_tasknr, 0);
|
---|
| 191 | if (r != OK)
|
---|
| 192 | ip_panic(( "unable to find synchronous alarm task: %d\n", r ));
|
---|
| 193 | #endif
|
---|
| 194 |
|
---|
| 195 | nw_init();
|
---|
| 196 | while (TRUE)
|
---|
| 197 | {
|
---|
| 198 | #ifdef BUF_CONSISTENCY_CHECK
|
---|
| 199 | if (inet_buf_debug)
|
---|
| 200 | {
|
---|
| 201 | static int buf_debug_count= 0;
|
---|
| 202 |
|
---|
| 203 | if (++buf_debug_count >= inet_buf_debug)
|
---|
| 204 | {
|
---|
| 205 | buf_debug_count= 0;
|
---|
| 206 | if (!bf_consistency_check())
|
---|
| 207 | break;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | #endif
|
---|
| 211 | if (ev_head)
|
---|
| 212 | {
|
---|
| 213 | ev_process();
|
---|
| 214 | continue;
|
---|
| 215 | }
|
---|
| 216 | if (clck_call_expire)
|
---|
| 217 | {
|
---|
| 218 | clck_expire_timers();
|
---|
| 219 | continue;
|
---|
| 220 | }
|
---|
| 221 | mq= mq_get();
|
---|
| 222 | if (!mq)
|
---|
| 223 | ip_panic(("out of messages"));
|
---|
| 224 |
|
---|
| 225 | r= receive (ANY, &mq->mq_mess);
|
---|
| 226 | if (r<0)
|
---|
| 227 | {
|
---|
| 228 | ip_panic(("unable to receive: %d", r));
|
---|
| 229 | }
|
---|
| 230 | reset_time();
|
---|
| 231 | source= mq->mq_mess.m_source;
|
---|
| 232 | if (source == FS_PROC_NR)
|
---|
| 233 | {
|
---|
| 234 | sr_rec(mq);
|
---|
| 235 | }
|
---|
| 236 | #ifdef __minix_vmd
|
---|
| 237 | else if (source == synal_tasknr)
|
---|
| 238 | {
|
---|
| 239 | clck_tick (&mq->mq_mess);
|
---|
| 240 | mq_free(mq);
|
---|
| 241 | }
|
---|
| 242 | #else /* Minix 3 */
|
---|
| 243 | else if (mq->mq_mess.m_type == SYN_ALARM)
|
---|
| 244 | {
|
---|
| 245 | clck_tick(&mq->mq_mess);
|
---|
| 246 | mq_free(mq);
|
---|
| 247 | }
|
---|
| 248 | else if (mq->mq_mess.m_type == PROC_EVENT)
|
---|
| 249 | {
|
---|
| 250 | /* signaled */
|
---|
| 251 | /* probably SIGTERM */
|
---|
| 252 | mq_free(mq);
|
---|
| 253 | }
|
---|
| 254 | else if (mq->mq_mess.m_type & NOTIFY_MESSAGE)
|
---|
| 255 | {
|
---|
| 256 | /* A driver is (re)started. */
|
---|
| 257 | eth_check_drivers(&mq->mq_mess);
|
---|
| 258 | mq_free(mq);
|
---|
| 259 | }
|
---|
| 260 | #endif
|
---|
| 261 | else
|
---|
| 262 | {
|
---|
| 263 | compare(mq->mq_mess.m_type, ==, DL_TASK_REPLY);
|
---|
| 264 | eth_rec(&mq->mq_mess);
|
---|
| 265 | mq_free(mq);
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | ip_panic(("task is not allowed to terminate"));
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | PRIVATE void nw_conf()
|
---|
| 272 | {
|
---|
| 273 | read_conf();
|
---|
| 274 | eth_prep();
|
---|
| 275 | arp_prep();
|
---|
| 276 | psip_prep();
|
---|
| 277 | ip_prep();
|
---|
| 278 | tcp_prep();
|
---|
| 279 | udp_prep();
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | PRIVATE void nw_init()
|
---|
| 283 | {
|
---|
| 284 | mq_init();
|
---|
| 285 | qp_init();
|
---|
| 286 | bf_init();
|
---|
| 287 | clck_init();
|
---|
| 288 | sr_init();
|
---|
| 289 | eth_init();
|
---|
| 290 | arp_init();
|
---|
| 291 | psip_init();
|
---|
| 292 | ip_init();
|
---|
| 293 | tcp_init();
|
---|
| 294 | udp_init();
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | PUBLIC void panic0(file, line)
|
---|
| 298 | char *file;
|
---|
| 299 | int line;
|
---|
| 300 | {
|
---|
| 301 | printf("panic at %s, %d: ", file, line);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | PUBLIC void inet_panic()
|
---|
| 305 | {
|
---|
| 306 | printf("\ninet stacktrace: ");
|
---|
| 307 | stacktrace();
|
---|
| 308 | #ifdef __minix_vmd
|
---|
| 309 | sys_abort(RBT_PANIC);
|
---|
| 310 | #else /* Minix 3 */
|
---|
| 311 | (panic)("INET","aborted due to a panic",NO_NUM);
|
---|
| 312 | #endif
|
---|
| 313 | for(;;);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | #if !NDEBUG
|
---|
| 317 | PUBLIC void bad_assertion(file, line, what)
|
---|
| 318 | char *file;
|
---|
| 319 | int line;
|
---|
| 320 | char *what;
|
---|
| 321 | {
|
---|
| 322 | panic0(file, line);
|
---|
| 323 | printf("assertion \"%s\" failed", what);
|
---|
| 324 | panic();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
| 328 | PUBLIC void bad_compare(file, line, lhs, what, rhs)
|
---|
| 329 | char *file;
|
---|
| 330 | int line;
|
---|
| 331 | int lhs;
|
---|
| 332 | char *what;
|
---|
| 333 | int rhs;
|
---|
| 334 | {
|
---|
| 335 | panic0(file, line);
|
---|
| 336 | printf("compare (%d) %s (%d) failed", lhs, what, rhs);
|
---|
| 337 | panic();
|
---|
| 338 | }
|
---|
| 339 | #endif /* !NDEBUG */
|
---|
| 340 |
|
---|
| 341 | /*
|
---|
| 342 | * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $
|
---|
| 343 | */
|
---|