[9] | 1 | /*
|
---|
| 2 | inet/inet_config.c
|
---|
| 3 |
|
---|
| 4 | Created: Nov 11, 1992 by Philip Homburg
|
---|
| 5 |
|
---|
| 6 | Modified: Apr 07, 2001 by Kees J. Bot
|
---|
| 7 | Read the configuration file and fill in the xx_conf[] arrays.
|
---|
| 8 |
|
---|
| 9 | Copyright 1995 Philip Homburg
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #define _MINIX_SOURCE 1
|
---|
| 13 | #define _POSIX_SOURCE 1
|
---|
| 14 |
|
---|
| 15 | #include <stdlib.h>
|
---|
| 16 | #include <unistd.h>
|
---|
| 17 | #include <fcntl.h>
|
---|
| 18 | #include <string.h>
|
---|
| 19 | #include <errno.h>
|
---|
| 20 | #include <sys/types.h>
|
---|
| 21 | #include <sys/stat.h>
|
---|
| 22 | #include <minix/type.h>
|
---|
| 23 | #include <minix/sysutil.h>
|
---|
| 24 | #include <minix/syslib.h>
|
---|
| 25 | #include "inet_config.h"
|
---|
| 26 |
|
---|
| 27 | struct eth_conf eth_conf[IP_PORT_MAX];
|
---|
| 28 | struct psip_conf psip_conf[IP_PORT_MAX];
|
---|
| 29 | struct ip_conf ip_conf[IP_PORT_MAX];
|
---|
| 30 | struct tcp_conf tcp_conf[IP_PORT_MAX];
|
---|
| 31 | struct udp_conf udp_conf[IP_PORT_MAX];
|
---|
| 32 | dev_t ip_dev;
|
---|
| 33 |
|
---|
| 34 | int eth_conf_nr;
|
---|
| 35 | int psip_conf_nr;
|
---|
| 36 | int ip_conf_nr;
|
---|
| 37 | int tcp_conf_nr;
|
---|
| 38 | int udp_conf_nr;
|
---|
| 39 |
|
---|
| 40 | int ip_forward_directed_bcast= 0; /* Default is off */
|
---|
| 41 |
|
---|
| 42 | static u8_t iftype[IP_PORT_MAX]; /* Interface in use as? */
|
---|
| 43 | static int ifdefault= -1; /* Default network interface. */
|
---|
| 44 |
|
---|
| 45 | static void fatal(char *label)
|
---|
| 46 | {
|
---|
| 47 | printf("init: %s: %s\n", label, strerror(errno));
|
---|
| 48 | exit(1);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | static void check_rm(char *device)
|
---|
| 52 | /* Check if a device is not among the living. */
|
---|
| 53 | {
|
---|
| 54 | if (unlink(device) < 0) {
|
---|
| 55 | if (errno == ENOENT) return;
|
---|
| 56 | fatal(device);
|
---|
| 57 | }
|
---|
| 58 | printf("rm %s\n", device);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static void check_mknod(char *device, mode_t mode, int minor)
|
---|
| 62 | /* Check if a device exists with the proper device number. */
|
---|
| 63 | {
|
---|
| 64 | struct stat st;
|
---|
| 65 | dev_t dev;
|
---|
| 66 |
|
---|
| 67 | dev= (ip_dev & 0xFF00) | minor;
|
---|
| 68 |
|
---|
| 69 | if (stat(device, &st) < 0) {
|
---|
| 70 | if (errno != ENOENT) fatal(device);
|
---|
| 71 | } else {
|
---|
| 72 | if (S_ISCHR(st.st_mode) && st.st_rdev == dev) return;
|
---|
| 73 | if (unlink(device) < 0) fatal(device);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if (mknod(device, S_IFCHR | mode, dev) < 0) fatal(device);
|
---|
| 77 | printf("mknod %s c %d %d\n", device, (ip_dev >> 8), minor);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | static void check_ln(char *old, char *new)
|
---|
| 81 | /* Check if 'old' and 'new' are still properly linked. */
|
---|
| 82 | {
|
---|
| 83 | struct stat st_old, st_new;
|
---|
| 84 |
|
---|
| 85 | if (stat(old, &st_old) < 0) fatal(old);
|
---|
| 86 | if (stat(new, &st_new) < 0) {
|
---|
| 87 | if (errno != ENOENT) fatal(new);
|
---|
| 88 | } else {
|
---|
| 89 | if (st_new.st_dev == st_old.st_dev
|
---|
| 90 | && st_new.st_ino == st_old.st_ino) {
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 | if (unlink(new) < 0) fatal(new);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if (link(old, new) < 0) fatal(new);
|
---|
| 97 | printf("ln %s %s\n", old, new);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | static void check_dev(int type, int ifno)
|
---|
| 101 | /* Check if the device group with interface number 'ifno' exists and has the
|
---|
| 102 | * proper device numbers. If 'type' is -1 then the device group must be
|
---|
| 103 | * removed.
|
---|
| 104 | */
|
---|
| 105 | {
|
---|
| 106 | static struct devlist {
|
---|
| 107 | char *defname;
|
---|
| 108 | mode_t mode;
|
---|
| 109 | u8_t minor_off;
|
---|
| 110 | } devlist[] = {
|
---|
| 111 | { "/dev/eth", 0600, ETH_DEV_OFF },
|
---|
| 112 | { "/dev/psip", 0600, PSIP_DEV_OFF },
|
---|
| 113 | { "/dev/ip", 0600, IP_DEV_OFF },
|
---|
| 114 | { "/dev/tcp", 0666, TCP_DEV_OFF },
|
---|
| 115 | { "/dev/udp", 0666, UDP_DEV_OFF },
|
---|
| 116 | };
|
---|
| 117 | struct devlist *dvp;
|
---|
| 118 | int i;
|
---|
| 119 | char device[sizeof("/dev/psip99")];
|
---|
| 120 | char *dp;
|
---|
| 121 |
|
---|
| 122 | for (i= 0; i < sizeof(devlist) / sizeof(devlist[0]); i++) {
|
---|
| 123 | dvp= &devlist[i];
|
---|
| 124 | strcpy(device, dvp->defname);
|
---|
| 125 | dp= device + strlen(device);
|
---|
| 126 | if (ifno >= 10) *dp++ = '0' + (ifno / 10);
|
---|
| 127 | *dp++ = '0' + (ifno % 10);
|
---|
| 128 | *dp = 0;
|
---|
| 129 |
|
---|
| 130 | if (type == 0
|
---|
| 131 | || (i == 0 && type != NETTYPE_ETH)
|
---|
| 132 | || (i == 1 && type != NETTYPE_PSIP)
|
---|
| 133 | ) {
|
---|
| 134 | check_rm(device);
|
---|
| 135 | if (ifno == ifdefault) check_rm(dvp->defname);
|
---|
| 136 | } else {
|
---|
| 137 | check_mknod(device, dvp->mode,
|
---|
| 138 | if2minor(ifno, dvp->minor_off));
|
---|
| 139 | if (ifno == ifdefault) check_ln(device, dvp->defname);
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static int cfg_fd;
|
---|
| 145 | static char word[16];
|
---|
| 146 | static unsigned line;
|
---|
| 147 |
|
---|
| 148 | static void error(void)
|
---|
| 149 | {
|
---|
| 150 | printf("inet: error on line %u\n", line);
|
---|
| 151 | exit(1);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static void token(int need)
|
---|
| 155 | {
|
---|
| 156 | /* Read a word from the configuration file. Return a null string on
|
---|
| 157 | * EOF. Return a punctiation as a one character word. If 'need' is
|
---|
| 158 | * true then an actual word is expected at this point, so err out if
|
---|
| 159 | * not.
|
---|
| 160 | */
|
---|
| 161 | unsigned char *wp;
|
---|
| 162 | static unsigned char c= '\n';
|
---|
| 163 |
|
---|
| 164 | wp= (unsigned char *) word;
|
---|
| 165 | *wp = 0;
|
---|
| 166 |
|
---|
| 167 | while (c <= ' ') {
|
---|
| 168 | if (c == '\n') line++;
|
---|
| 169 | if (read(cfg_fd, &c, 1) != 1) {
|
---|
| 170 | if (need) error();
|
---|
| 171 | return;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | do {
|
---|
| 176 | if (wp < (unsigned char *) word + sizeof(word)-1) *wp++ = c;
|
---|
| 177 | if (read(cfg_fd, &c, 1) != 1) c= ' ';
|
---|
| 178 | if (word[0] == ';' || word[0] == '{' || word[0] == '}') {
|
---|
| 179 | if (need) error();
|
---|
| 180 | break;
|
---|
| 181 | }
|
---|
| 182 | } while (c > ' ' && c != ';' && c != '{' && c != '}');
|
---|
| 183 | *wp = 0;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | static unsigned number(char *str, unsigned max)
|
---|
| 187 | {
|
---|
| 188 | /* Interpret a string as an unsigned decimal number, no bigger than
|
---|
| 189 | * 'max'. Return this number.
|
---|
| 190 | */
|
---|
| 191 | char *s;
|
---|
| 192 | unsigned n, d;
|
---|
| 193 |
|
---|
| 194 | s= str;
|
---|
| 195 | n= 0;
|
---|
| 196 | while ((d= (*s - '0')) < 10 && n <= max) {
|
---|
| 197 | n= n * 10 + d;
|
---|
| 198 | s++;
|
---|
| 199 | }
|
---|
| 200 | if (*s != 0 || n > max) {
|
---|
| 201 | printf("inet: '%s' is not a number <= %u\n", str, max);
|
---|
| 202 | error();
|
---|
| 203 | }
|
---|
| 204 | return n;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | void read_conf(void)
|
---|
| 208 | {
|
---|
| 209 | int i, j, ifno, type, port, enable;
|
---|
| 210 | struct eth_conf *ecp;
|
---|
| 211 | struct psip_conf *pcp;
|
---|
| 212 | struct ip_conf *icp;
|
---|
| 213 | struct stat st;
|
---|
| 214 |
|
---|
| 215 | /* Open the configuration file. */
|
---|
| 216 | if ((cfg_fd= open(PATH_INET_CONF, O_RDONLY)) == -1)
|
---|
| 217 | fatal(PATH_INET_CONF);
|
---|
| 218 |
|
---|
| 219 | ecp= eth_conf;
|
---|
| 220 | pcp= psip_conf;
|
---|
| 221 | icp= ip_conf;
|
---|
| 222 |
|
---|
| 223 | while (token(0), word[0] != 0) {
|
---|
| 224 | if (strncmp(word, "eth", 3) == 0) {
|
---|
| 225 | ecp->ec_ifno= ifno= number(word+3, IP_PORT_MAX-1);
|
---|
| 226 | type= NETTYPE_ETH;
|
---|
| 227 | port= eth_conf_nr;
|
---|
| 228 | token(1);
|
---|
| 229 | if (strcmp(word, "vlan") == 0) {
|
---|
| 230 | token(1);
|
---|
| 231 | ecp->ec_vlan= number(word, (1<<12)-1);
|
---|
| 232 | token(1);
|
---|
| 233 | if (strncmp(word, "eth", 3) != 0) {
|
---|
| 234 | printf(
|
---|
| 235 | "inet: VLAN eth%d can't be built on %s\n",
|
---|
| 236 | ifno, word);
|
---|
| 237 | exit(1);
|
---|
| 238 | }
|
---|
| 239 | ecp->ec_port= number(word+3, IP_PORT_MAX-1);
|
---|
| 240 | } else {
|
---|
| 241 | ecp->ec_task= alloc(strlen(word)+1);
|
---|
| 242 | strcpy(ecp->ec_task, word);
|
---|
| 243 | token(1);
|
---|
| 244 | ecp->ec_port= number(word, IP_PORT_MAX-1);
|
---|
| 245 | }
|
---|
| 246 | ecp++;
|
---|
| 247 | eth_conf_nr++;
|
---|
| 248 | } else
|
---|
| 249 | if (strncmp(word, "psip", 4) == 0) {
|
---|
| 250 | pcp->pc_ifno= ifno= number(word+4, IP_PORT_MAX-1);
|
---|
| 251 | type= NETTYPE_PSIP;
|
---|
| 252 | port= psip_conf_nr;
|
---|
| 253 | pcp++;
|
---|
| 254 | psip_conf_nr++;
|
---|
| 255 | } else {
|
---|
| 256 | printf("inet: Unknown device '%s'\n", word);
|
---|
| 257 | error();
|
---|
| 258 | }
|
---|
| 259 | iftype[ifno]= type;
|
---|
| 260 | icp->ic_ifno= ifno;
|
---|
| 261 | icp->ic_devtype= type;
|
---|
| 262 | icp->ic_port= port;
|
---|
| 263 | tcp_conf[tcp_conf_nr].tc_port= ip_conf_nr;
|
---|
| 264 | udp_conf[udp_conf_nr].uc_port= ip_conf_nr;
|
---|
| 265 |
|
---|
| 266 | enable= 7; /* 1 = IP, 2 = TCP, 4 = UDP */
|
---|
| 267 |
|
---|
| 268 | token(0);
|
---|
| 269 | if (word[0] == '{') {
|
---|
| 270 | token(0);
|
---|
| 271 | while (word[0] != '}') {
|
---|
| 272 | if (strcmp(word, "default") == 0) {
|
---|
| 273 | if (ifdefault != -1) {
|
---|
| 274 | printf(
|
---|
| 275 | "inet: ip%d and ip%d can't both be default\n",
|
---|
| 276 | ifdefault, ifno);
|
---|
| 277 | error();
|
---|
| 278 | }
|
---|
| 279 | ifdefault= ifno;
|
---|
| 280 | token(0);
|
---|
| 281 | } else
|
---|
| 282 | if (strcmp(word, "no") == 0) {
|
---|
| 283 | token(1);
|
---|
| 284 | if (strcmp(word, "ip") == 0) {
|
---|
| 285 | enable= 0;
|
---|
| 286 | } else
|
---|
| 287 | if (strcmp(word, "tcp") == 0) {
|
---|
| 288 | enable &= ~2;
|
---|
| 289 | } else
|
---|
| 290 | if (strcmp(word, "udp") == 0) {
|
---|
| 291 | enable &= ~4;
|
---|
| 292 | } else {
|
---|
| 293 | printf(
|
---|
| 294 | "inet: Can't do 'no %s'\n",
|
---|
| 295 | word);
|
---|
| 296 | exit(1);
|
---|
| 297 | }
|
---|
| 298 | token(0);
|
---|
| 299 | } else {
|
---|
| 300 | printf("inet: Unknown option '%s'\n",
|
---|
| 301 | word);
|
---|
| 302 | exit(1);
|
---|
| 303 | }
|
---|
| 304 | if (word[0] == ';') token(0);
|
---|
| 305 | else
|
---|
| 306 | if (word[0] != '}') error();
|
---|
| 307 | }
|
---|
| 308 | token(0);
|
---|
| 309 | }
|
---|
| 310 | if (word[0] != ';' && word[0] != 0) error();
|
---|
| 311 |
|
---|
| 312 | if (enable & 1) icp++, ip_conf_nr++;
|
---|
| 313 | if (enable & 2) tcp_conf_nr++;
|
---|
| 314 | if (enable & 4) udp_conf_nr++;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | if (ifdefault == -1) {
|
---|
| 318 | printf("inet: No networks or no default network defined\n");
|
---|
| 319 | exit(1);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /* Translate VLAN network references to port numbers. */
|
---|
| 323 | for (i= 0; i < eth_conf_nr; i++) {
|
---|
| 324 | ecp= ð_conf[i];
|
---|
| 325 | if (eth_is_vlan(ecp)) {
|
---|
| 326 | for (j= 0; j < eth_conf_nr; j++) {
|
---|
| 327 | if (eth_conf[j].ec_ifno == ecp->ec_port
|
---|
| 328 | && !eth_is_vlan(ð_conf[j])
|
---|
| 329 | ) {
|
---|
| 330 | ecp->ec_port= j;
|
---|
| 331 | break;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | if (j == eth_conf_nr) {
|
---|
| 335 | printf(
|
---|
| 336 | "inet: VLAN eth%d can't be built on eth%d\n",
|
---|
| 337 | ecp->ec_ifno, ecp->ec_port);
|
---|
| 338 | exit(1);
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | /* Set umask 0 so we can creat mode 666 devices. */
|
---|
| 344 | (void) umask(0);
|
---|
| 345 |
|
---|
| 346 | /* See what the device number of /dev/ip is. That's what we
|
---|
| 347 | * used last time for the network devices, so we keep doing so.
|
---|
| 348 | */
|
---|
| 349 | if (stat("/dev/ip", &st) < 0) fatal("/dev/ip");
|
---|
| 350 | ip_dev= st.st_rdev;
|
---|
| 351 |
|
---|
| 352 | for (i= 0; i < IP_PORT_MAX; i++) {
|
---|
| 353 | /* Create network devices. */
|
---|
| 354 | check_dev(iftype[i], i);
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | void *alloc(size_t size)
|
---|
| 359 | {
|
---|
| 360 | /* Allocate memory on the heap with sbrk(). */
|
---|
| 361 |
|
---|
| 362 | return sbrk((size + (sizeof(char *) - 1)) & ~(sizeof(char *) - 1));
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /*
|
---|
| 366 | * $PchId: inet_config.c,v 1.10 2003/08/21 09:26:02 philip Exp $
|
---|
| 367 | */
|
---|