2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Network utility functions
12 * Parsing NetRestrict file and filtering IP addresses
15 #include <afsconfig.h>
17 #include "afs/param.h"
19 #include <afs/param.h>
28 #include "afs/sysincludes.h"
29 #include "afsincludes.h"
35 #include <sys/types.h>
37 #include <netinet/in.h>
38 #include <sys/errno.h>
49 #define AFS_IPINVALID 0xffffffff /* invalid IP address */
50 #define AFS_IPINVALIDIGNORE 0xfffffffe /* no input given to extractAddr */
51 #define MAX_NETFILE_LINE 2048 /* length of a line in the netrestrict file */
52 #define MAXIPADDRS 1024 /* from afsd.c */
54 int ParseNetInfoFile_int(afs_uint32 *, afs_uint32 *, afs_uint32 *,
55 int, char reason[], const char *,
58 * The line parameter is a pointer to a buffer containing a string of
60 ** w.x.y.z # machineName
61 * returns the network interface IP Address in NBO
64 extract_Addr(char *line, int maxSize)
70 afs_uint32 retval = 0;
72 /* skip empty spaces */
73 while (isspace(*line) && maxSize) {
77 /* skip empty lines */
78 if (!maxSize || !*line)
79 return AFS_IPINVALIDIGNORE;
81 for (n = 0; n < 4; n++) {
82 while ((*line != '.') && !isspace(*line) && maxSize) { /* extract nth byte */
86 return AFS_IPINVALID; /* no space */
87 bytes[n][i++] = *line++;
95 val[n] = strtol(bytes[n], &endPtr, 10);
96 if ((val[n] == 0) && (errno != 0 || bytes[n] == endPtr)) /* no conversion */
100 retval = (val[0] << 24) | (val[1] << 16) | (val[2] << 8) | val[3];
101 return htonl(retval);
107 /* parseNetRestrictFile()
108 * Get a list of IP addresses for this host removing any address found
109 * in the config file (fileName parameter): /usr/vice/etc/NetRestrict
110 * for clients and /usr/afs/local/NetRestrict for servers.
112 * Returns the number of valid addresses in outAddrs[] and count in
113 * nAddrs. Returns 0 on success; or 1 if the config file was not
114 * there or empty (we still return the host's IP addresses). Returns
115 * -1 on fatal failure with reason in the reason argument (so the
116 * caller can choose to ignore the entire file but should write
117 * something to a log file).
119 * All addresses should be in NBO (as returned by rx_getAllAddrMaskMtu() and
120 * parsed by extract_Addr().
123 afs_uint32 outAddrs[]; * output address array *
124 afs_uint32 *mask, *mtu; * optional mask and mtu *
125 afs_uint32 maxAddrs; * max number of addresses *
126 afs_uint32 *nAddrs; * number of Addresses in output array *
127 char reason[]; * reason for failure *
128 const char *fileName; * filename to parse *
132 parseNetRestrictFile_int(afs_uint32 outAddrs[], afs_uint32 * mask,
133 afs_uint32 * mtu, afs_uint32 maxAddrs,
134 afs_uint32 * nAddrs, char reason[],
135 const char *fileName, const char *fileName_ni)
138 char line[MAX_NETFILE_LINE];
139 int lineNo, usedfile = 0;
140 afs_uint32 i, neaddrs, nOutaddrs;
141 afs_uint32 addr, eAddrs[MAXIPADDRS], eMask[MAXIPADDRS], eMtu[MAXIPADDRS];
152 for (i = 0; i < maxAddrs; i++)
156 /* get all network interfaces from the kernel */
157 neaddrs = rx_getAllAddrMaskMtu(eAddrs, eMask, eMtu, MAXIPADDRS);
159 sprintf(reason, "No existing IP interfaces found");
163 if ((neaddrs < MAXIPADDRS) && fileName_ni)
164 i = ParseNetInfoFile_int(&(eAddrs[neaddrs]), &(eMask[neaddrs]),
165 &(eMtu[neaddrs]), MAXIPADDRS-neaddrs, reason,
171 if ((fp = fopen(fileName, "r")) == 0) {
172 sprintf(reason, "Could not open file %s for reading:%s", fileName,
177 /* For each line in the NetRestrict file */
180 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
181 lineNo++; /* input line number */
182 addr = extract_Addr(line, strlen(line));
183 if (addr == AFS_IPINVALID) { /* syntactically invalid */
184 fprintf(stderr, "%s : line %d : parse error - invalid IP\n",
188 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
189 fprintf(stderr, "%s : line %d : invalid address ... ignoring\n",
195 /* Check if we need to exclude this address */
196 for (i = 0; i < neaddrs; i++) {
197 if (eAddrs[i] && (eAddrs[i] == addr)) {
198 eAddrs[i] = 0; /* Yes - exclude it by zeroing it for now */
206 sprintf(reason, "No valid IP addresses in %s\n", fileName);
211 /* Collect the addresses we have left to return */
213 for (i = 0; i < neaddrs; i++) {
216 outAddrs[nOutaddrs] = eAddrs[i];
218 mask[nOutaddrs] = eMask[i];
219 mtu[nOutaddrs] = eMtu[i];
221 if (++nOutaddrs >= maxAddrs)
224 if (nOutaddrs == 0) {
225 sprintf(reason, "No addresses to use after parsing %s", fileName);
229 return (usedfile ? 0 : 1); /* 0=>used the file. 1=>didn't use file */
233 parseNetRestrictFile(afs_uint32 outAddrs[], afs_uint32 * mask,
234 afs_uint32 * mtu, afs_uint32 maxAddrs,
235 afs_uint32 * nAddrs, char reason[],
236 const char *fileName)
238 return parseNetRestrictFile_int(outAddrs, mask, mtu, maxAddrs, nAddrs, reason, fileName, NULL);
242 * this function reads in stuff from InterfaceAddr file in
243 * /usr/vice/etc ( if it exists ) and verifies the addresses
245 * 'final' contains all those addresses that are found to
246 * be valid. This function returns the number of valid
247 * interface addresses. Pulled out from afsd.c
250 ParseNetInfoFile_int(afs_uint32 * final, afs_uint32 * mask, afs_uint32 * mtu,
251 int max, char reason[], const char *fileName,
255 afs_uint32 existingAddr[MAXIPADDRS], existingMask[MAXIPADDRS],
256 existingMtu[MAXIPADDRS];
257 char line[MAX_NETFILE_LINE];
259 int i, existNu, count = 0;
270 /* get all network interfaces from the kernel */
272 rx_getAllAddrMaskMtu(existingAddr, existingMask, existingMtu,
277 if ((fp = fopen(fileName, "r")) == 0) {
278 /* If file does not exist or is not readable, then
279 * use all interface addresses.
282 "Failed to open %s(%s)\nUsing all configured addresses\n",
283 fileName, strerror(errno));
284 for (i = 0; i < existNu; i++) {
285 final[i] = existingAddr[i];
286 mask[i] = existingMask[i];
287 mtu[i] = existingMtu[i];
292 /* For each line in the NetInfo file */
293 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
296 /* See if first char is an 'F' for fake */
297 /* Added to allow the fileserver to advertise fake IPS for use with
298 * the translation tables for NAT-like firewalls - defect 12462 */
299 for (fake = 0; ((fake < strlen(line)) && isspace(line[fake]));
301 if ((fake < strlen(line))
302 && ((line[fake] == 'f') || (line[fake] == 'F'))) {
308 lineNo++; /* input line number */
309 addr = extract_Addr(&line[fake], strlen(&line[fake]));
311 if (addr == AFS_IPINVALID) { /* syntactically invalid */
312 fprintf(stderr, "afs:%s : line %d : parse error\n", fileName,
316 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
320 /* See if it is an address that really exists */
321 for (i = 0; i < existNu; i++) {
322 if (existingAddr[i] == addr)
325 if ((i >= existNu) && (!fake))
326 continue; /* not found/fake - ignore */
328 /* Check if it is a duplicate address we alread have */
329 for (l = 0; l < count; l++) {
330 if (final[l] == addr)
334 fprintf(stderr, "afs:%x specified twice in NetInfo file\n",
336 continue; /* duplicate addr - ignore */
339 if (count > max) { /* no more space */
341 "afs:Too many interfaces. The current kernel configuration supports a maximum of %d interfaces\n",
345 fprintf(stderr, "Client (2) also has address %s\n", line);
347 mask[count] = 0xffffffff;
348 mtu[count] = htonl(1500);
350 } else if (!fakeonly) {
351 final[count] = existingAddr[i];
352 mask[count] = existingMask[i];
353 mtu[count] = existingMtu[i];
358 /* in case of any error, we use all the interfaces present */
361 "Error in reading/parsing Interface file\nUsing all configured interface addresses \n");
362 for (i = 0; i < existNu; i++) {
363 final[i] = existingAddr[i];
364 mask[i] = existingMask[i];
365 mtu[i] = existingMtu[i];
373 ParseNetInfoFile(afs_uint32 * final, afs_uint32 * mask, afs_uint32 * mtu,
374 int max, char reason[], const char *fileName)
376 return ParseNetInfoFile_int(final, mask, mtu, max, reason, fileName, 0);
380 * Given two arrays of addresses, masks and mtus find the common ones
381 * and return them in the first buffer. Return number of common
385 filterAddrs(afs_uint32 addr1[], afs_uint32 addr2[], afs_uint32 mask1[],
386 afs_uint32 mask2[], afs_uint32 mtu1[], afs_uint32 mtu2[], int n1,
389 afs_uint32 taddr[MAXIPADDRS];
390 afs_uint32 tmask[MAXIPADDRS];
391 afs_uint32 tmtu[MAXIPADDRS];
392 int count = 0, i = 0, j = 0, found = 0;
401 for (i = 0; i < n1; i++) {
403 for (j = 0; j < n2; j++) {
404 if (addr1[i] == addr2[j]) {
410 /* Always mask loopback address */
411 if (found && rx_IsLoopbackAddr(addr1[i]))
415 taddr[count] = addr1[i];
416 tmask[count] = mask1[i];
417 tmtu[count] = mtu1[i];
421 /* copy everything into addr1, mask1 and mtu1 */
422 for (i = 0; i < count; i++) {
429 /* and zero out the rest */
430 for (i = count; i < n1; i++) {
441 * parse both netinfo and netrerstrict files and return the final
442 * set of IP addresses to use
444 /* max - Entries in addrbuf, maskbuf and mtubuf */
446 parseNetFiles(afs_uint32 addrbuf[], afs_uint32 maskbuf[], afs_uint32 mtubuf[],
447 afs_uint32 max, char reason[], const char *niFileName,
448 const char *nrFileName)
450 afs_uint32 addrbuf1[MAXIPADDRS], maskbuf1[MAXIPADDRS],
452 afs_uint32 addrbuf2[MAXIPADDRS], maskbuf2[MAXIPADDRS],
455 afs_uint32 nAddrs2 = 0;
459 ParseNetInfoFile(addrbuf1, maskbuf1, mtubuf1, MAXIPADDRS, reason,
462 parseNetRestrictFile_int(addrbuf2, maskbuf2, mtubuf2, MAXIPADDRS,
463 &nAddrs2, reason, nrFileName, niFileName);
464 if ((nAddrs1 < 0) && (code)) {
467 } else if ((nAddrs1 > 0) && (code)) {
468 /* netinfo succeeded and netrestrict failed */
469 for (i = 0; ((i < nAddrs1) && (i < max)); i++) {
470 addrbuf[i] = addrbuf1[i];
472 maskbuf[i] = maskbuf1[i];
473 mtubuf[i] = mtubuf1[i];
477 } else if ((!code) && (nAddrs1 < 0)) {
478 /* netrestrict succeeded and netinfo failed */
479 for (i = 0; ((i < nAddrs2) && (i < max)); i++) {
480 addrbuf[i] = addrbuf2[i];
482 maskbuf[i] = maskbuf2[i];
483 mtubuf[i] = mtubuf2[i];
487 } else if ((!code) && (nAddrs1 >= 0)) {
489 /* take the intersection of addrbuf1 and addrbuf2 */
491 filterAddrs(addrbuf1, addrbuf2, maskbuf1, maskbuf2, mtubuf1,
492 mtubuf2, nAddrs1, nAddrs2);
493 for (i = 0; ((i < code) && (i < max)); i++) {
494 addrbuf[i] = addrbuf1[i];
496 maskbuf[i] = maskbuf1[i];
497 mtubuf[i] = mtubuf1[i];