kernel: increase some per-process privilege limits.
[minix.git] / servers / inet / inet.c
bloba9278b23d41c7dc3489062cd4573768947a62597
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.
4 Copyright 1995 Philip Homburg
6 The valid messages and their parameters are:
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 |_______________|___________|_________|_______|__________|_________|
32 from DL_ETH:
33 _______________________________________________________________________
34 | | | | | | |
35 | m_type | DL_PORT | DL_PROC | DL_COUNT | DL_STAT | DL_TIME |
36 |_______________|___________|_________|__________|____________|_________|
37 | | | | | | |
38 | DL_CONF_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 |_______________|___________|_________|__________|____________|_________|
45 #include "inet.h"
47 #define _MINIX_SOURCE 1
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <sys/svrctl.h>
52 #include <minix/ds.h>
53 #include <minix/endpoint.h>
54 #include <minix/drivers.h>
55 #include <minix/driver.h>
57 #include "mq.h"
58 #include "qp.h"
59 #include "proto.h"
60 #include "generic/type.h"
62 #include "generic/arp.h"
63 #include "generic/assert.h"
64 #include "generic/buf.h"
65 #include "generic/clock.h"
66 #include "generic/eth.h"
67 #include "generic/event.h"
68 #include "generic/ip.h"
69 #include "generic/psip.h"
70 #include "generic/rand256.h"
71 #include "generic/sr.h"
72 #include "generic/tcp.h"
73 #include "generic/udp.h"
75 THIS_FILE
77 #define RANDOM_DEV_NAME "/dev/random"
79 endpoint_t this_proc; /* Process number of this server. */
81 /* Killing Solaris */
82 int killer_inet= 0;
84 #ifdef BUF_CONSISTENCY_CHECK
85 extern int inet_buf_debug;
86 #endif
88 #if HZ_DYNAMIC
89 u32_t system_hz;
90 #endif
92 _PROTOTYPE( void main, (void) );
94 FORWARD _PROTOTYPE( void nw_conf, (void) );
95 FORWARD _PROTOTYPE( void nw_init, (void) );
96 FORWARD _PROTOTYPE( void ds_event, (void) );
98 /* SEF functions and variables. */
99 FORWARD _PROTOTYPE( void sef_local_startup, (void) );
100 FORWARD _PROTOTYPE( int sef_cb_init_fresh, (int type, sef_init_info_t *info) );
102 PUBLIC void main()
104 mq_t *mq;
105 int ipc_status;
106 int r;
107 endpoint_t source;
108 int m_type;
110 /* SEF local startup. */
111 sef_local_startup();
113 while (TRUE)
115 #ifdef BUF_CONSISTENCY_CHECK
116 if (inet_buf_debug)
118 static int buf_debug_count= 0;
120 if (++buf_debug_count >= inet_buf_debug)
122 buf_debug_count= 0;
123 if (!bf_consistency_check())
124 break;
127 #endif
128 if (ev_head)
130 ev_process();
131 continue;
133 if (clck_call_expire)
135 clck_expire_timers();
136 continue;
138 mq= mq_get();
139 if (!mq)
140 ip_panic(("out of messages"));
142 r= sef_receive_status(ANY, &mq->mq_mess, &ipc_status);
143 if (r<0)
145 ip_panic(("unable to receive: %d", r));
147 reset_time();
148 source= mq->mq_mess.m_source;
149 m_type= mq->mq_mess.m_type;
150 if (source == FS_PROC_NR)
152 sr_rec(mq);
154 else if (is_ipc_notify(ipc_status))
156 if (source == CLOCK)
158 clck_tick(&mq->mq_mess);
159 mq_free(mq);
161 else if (source == PM_PROC_NR)
163 /* signaled */
164 /* probably SIGTERM */
165 mq_free(mq);
167 else if (source == DS_PROC_NR)
169 /* DS notifies us of an event. */
170 ds_event();
171 mq_free(mq);
173 else
175 printf("inet: got unexpected notify from %d\n",
176 mq->mq_mess.m_source);
177 mq_free(mq);
180 else if (m_type == DL_CONF_REPLY || m_type == DL_TASK_REPLY ||
181 m_type == DL_NAME_REPLY || m_type == DL_STAT_REPLY)
183 eth_rec(&mq->mq_mess);
184 mq_free(mq);
186 else
188 printf("inet: got bad message type 0x%x from %d\n",
189 mq->mq_mess.m_type, mq->mq_mess.m_source);
190 mq_free(mq);
193 ip_panic(("task is not allowed to terminate"));
196 /*===========================================================================*
197 * sef_local_startup *
198 *===========================================================================*/
199 PRIVATE void sef_local_startup()
201 /* Register init callbacks. */
202 sef_setcb_init_fresh(sef_cb_init_fresh);
203 sef_setcb_init_restart(sef_cb_init_fresh);
205 /* No live update support for now. */
207 /* Let SEF perform startup. */
208 sef_startup();
211 /*===========================================================================*
212 * sef_cb_init_fresh *
213 *===========================================================================*/
214 PRIVATE int sef_cb_init_fresh(int type, sef_init_info_t *info)
216 /* Initialize the inet server. */
217 int r;
218 int timerand, fd;
219 endpoint_t tasknr;
220 u8_t randbits[32];
221 struct timeval tv;
222 char my_name[32];
224 #if DEBUG
225 printf("Starting inet...\n");
226 printf("%s\n", version);
227 #endif
229 #if HZ_DYNAMIC
230 system_hz = sys_hz();
231 #endif
233 /* Read configuration. */
234 nw_conf();
236 /* Get a random number */
237 timerand= 1;
238 fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
239 if (fd != -1)
241 r= read(fd, randbits, sizeof(randbits));
242 if (r == sizeof(randbits))
243 timerand= 0;
244 else
246 printf("inet: unable to read random data from %s: %s\n",
247 RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
248 r == 0 ? "EOF" : "not enough data");
250 close(fd);
252 else
254 printf("inet: unable to open random device %s: %s\n",
255 RANDOM_DEV_NAME, strerror(errno));
257 if (timerand)
259 printf("inet: using current time for random-number seed\n");
260 r= gettimeofday(&tv, NULL);
261 if (r == -1)
263 printf("sysutime failed: %s\n", strerror(errno));
264 exit(1);
266 memcpy(randbits, &tv, sizeof(tv));
268 init_rand256(randbits);
270 /* Our new identity as a server. */
271 r = sys_whoami(&tasknr, my_name, sizeof(my_name));
272 if (r != OK)
273 ip_panic(("inet: sys_whoami failed for 'inet': %d", r));
274 this_proc= tasknr;
276 #ifdef BUF_CONSISTENCY_CHECK
277 inet_buf_debug= (getenv("inetbufdebug") &&
278 (strcmp(getenv("inetbufdebug"), "on") == 0));
279 inet_buf_debug= 100;
280 if (inet_buf_debug)
282 ip_warning(( "buffer consistency check enabled" ));
284 #endif
286 if (getenv("killerinet"))
288 ip_warning(( "killer inet active" ));
289 killer_inet= 1;
292 nw_init();
294 /* Subscribe to driver events for network drivers. */
295 r = ds_subscribe("drv\\.net\\..*", DSF_INITIAL | DSF_OVERWRITE);
296 if(r != OK) {
297 ip_panic(("inet: can't subscribe to driver events"));
300 /* Announce we are up. INET announces its presence to VFS just like
301 * any other driver.
303 driver_announce();
305 return(OK);
308 PRIVATE void nw_conf()
310 read_conf();
311 eth_prep();
312 arp_prep();
313 psip_prep();
314 ip_prep();
315 tcp_prep();
316 udp_prep();
319 PRIVATE void nw_init()
321 mq_init();
322 bf_init();
323 clck_init();
324 sr_init();
325 qp_init();
326 eth_init();
327 arp_init();
328 psip_init();
329 ip_init();
330 tcp_init();
331 udp_init();
334 /*===========================================================================*
335 * ds_event *
336 *===========================================================================*/
337 PRIVATE void ds_event()
339 char key[DS_MAX_KEYLEN];
340 char *driver_prefix = "drv.net.";
341 u32_t value;
342 int type;
343 endpoint_t owner_endpoint;
344 int r;
346 /* Get the event and the owner from DS. */
347 r = ds_check(key, &type, &owner_endpoint);
348 if(r != OK) {
349 if(r != ENOENT)
350 printf("inet: ds_event: ds_check failed: %d\n", r);
351 return;
353 r = ds_retrieve_u32(key, &value);
354 if(r != OK) {
355 printf("inet: ds_event: ds_retrieve_u32 failed\n");
356 return;
359 /* Only check for network driver up events. */
360 if(strncmp(key, driver_prefix, sizeof(driver_prefix))
361 || value != DS_DRIVER_UP) {
362 return;
365 /* A driver is (re)started. */
366 eth_check_driver(owner_endpoint);
369 PUBLIC void panic0(file, line)
370 char *file;
371 int line;
373 printf("panic at %s, %d: ", file, line);
376 PUBLIC void inet_panic()
378 printf("\ninet stacktrace: ");
379 util_stacktrace();
380 (panic)("aborted due to a panic");
381 for(;;);
384 #if !NDEBUG
385 PUBLIC void bad_assertion(file, line, what)
386 char *file;
387 int line;
388 char *what;
390 panic0(file, line);
391 printf("assertion \"%s\" failed", what);
392 panic();
396 PUBLIC void bad_compare(file, line, lhs, what, rhs)
397 char *file;
398 int line;
399 int lhs;
400 char *what;
401 int rhs;
403 panic0(file, line);
404 printf("compare (%d) %s (%d) failed", lhs, what, rhs);
405 panic();
407 #endif /* !NDEBUG */
410 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $