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>
36 #include "afs/sysincludes.h"
37 #include "afsincludes.h"
40 #include <sys/types.h>
42 #include <netinet/in.h>
44 #include <sys/errno.h>
51 #define AFS_IPINVALID 0xffffffff /* invalid IP address */
52 #define AFS_IPINVALIDIGNORE 0xfffffffe /* no input given to extractAddr */
53 #define MAX_NETFILE_LINE 2048 /* length of a line in the netrestrict file */
54 #define MAXIPADDRS 1024 /* from afsd.c */
56 #ifndef INADDR_LOOPBACK
57 #define INADDR_LOOPBACK (afs_uint32)0x7f000001
61 * The line parameter is a pointer to a buffer containing a string of
63 ** w.x.y.z # machineName
64 * returns the network interface IP Address in NBO
67 extract_Addr(char *line, int maxSize)
73 afs_uint32 retval = 0;
75 /* skip empty spaces */
76 while (isspace(*line) && maxSize) {
80 /* skip empty lines */
81 if (!maxSize || !*line)
82 return AFS_IPINVALIDIGNORE;
84 for (n = 0; n < 4; n++) {
85 while ((*line != '.') && !isspace(*line) && maxSize) { /* extract nth byte */
89 return AFS_IPINVALID; /* no space */
90 bytes[n][i++] = *line++;
98 val[n] = strtol(bytes[n], &endPtr, 10);
99 if ((val[n] == 0) && (errno != 0 || bytes[n] == endPtr)) /* no conversion */
100 return AFS_IPINVALID;
103 retval = (val[0] << 24) | (val[1] << 16) | (val[2] << 8) | val[3];
104 return htonl(retval);
110 /* parseNetRestrictFile()
111 * Get a list of IP addresses for this host removing any address found
112 * in the config file (fileName parameter): /usr/vice/etc/NetRestrict
113 * for clients and /usr/afs/local/NetRestrict for servers.
115 * Returns the number of valid addresses in outAddrs[] and count in
116 * nAddrs. Returns 0 on success; or 1 if the config file was not
117 * there or empty (we still return the host's IP addresses). Returns
118 * -1 on fatal failure with reason in the reason argument (so the
119 * caller can choose to ignore the entire file but should write
120 * something to a log file).
122 * All addresses should be in NBO (as returned by rx_getAllAddr() and
123 * parsed by extract_Addr().
126 afs_uint32 outAddrs[]; * output address array *
127 afs_uint32 *mask, *mtu; * optional mask and mtu *
128 afs_uint32 maxAddrs; * max number of addresses *
129 afs_uint32 *nAddrs; * number of Addresses in output array *
130 char reason[]; * reason for failure *
131 const char *fileName; * filename to parse *
135 parseNetRestrictFile(afs_uint32 outAddrs[], afs_uint32 * mask,
136 afs_uint32 * mtu, afs_uint32 maxAddrs,
137 afs_uint32 * nAddrs, char reason[], const char *fileName)
140 char line[MAX_NETFILE_LINE];
141 int lineNo, usedfile = 0;
142 afs_uint32 i, neaddrs, nOutaddrs;
143 afs_uint32 addr, eAddrs[MAXIPADDRS], eMask[MAXIPADDRS], eMtu[MAXIPADDRS];
154 for (i = 0; i < maxAddrs; i++)
158 /* get all network interfaces from the kernel */
159 neaddrs = rxi_getAllAddrMaskMtu(eAddrs, eMask, eMtu, MAXIPADDRS);
161 sprintf(reason, "No existing IP interfaces found");
165 if ((fp = fopen(fileName, "r")) == 0) {
166 sprintf(reason, "Could not open file %s for reading:%s", fileName,
171 /* For each line in the NetRestrict file */
174 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
175 lineNo++; /* input line number */
176 addr = extract_Addr(line, strlen(line));
177 if (addr == AFS_IPINVALID) { /* syntactically invalid */
178 fprintf(stderr, "%s : line %d : parse error - invalid IP\n",
182 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
183 fprintf(stderr, "%s : line %d : invalid address ... ignoring\n",
189 /* Check if we need to exclude this address */
190 for (i = 0; i < neaddrs; i++) {
191 if (eAddrs[i] && (eAddrs[i] == addr)) {
192 eAddrs[i] = 0; /* Yes - exclude it by zeroing it for now */
200 sprintf(reason, "No valid IP addresses in %s\n", fileName);
205 /* Collect the addresses we have left to return */
207 for (i = 0; i < neaddrs; i++) {
210 outAddrs[nOutaddrs] = eAddrs[i];
212 mask[nOutaddrs] = eMask[i];
213 mtu[nOutaddrs] = eMtu[i];
215 if (++nOutaddrs >= maxAddrs)
218 if (nOutaddrs == 0) {
219 sprintf(reason, "No addresses to use after parsing %s", fileName);
223 return (usedfile ? 0 : 1); /* 0=>used the file. 1=>didn't use file */
229 * this function reads in stuff from InterfaceAddr file in
230 * /usr/vice/etc ( if it exists ) and verifies the addresses
232 * 'final' contains all those addresses that are found to
233 * be valid. This function returns the number of valid
234 * interface addresses. Pulled out from afsd.c
237 ParseNetInfoFile(afs_uint32 * final, afs_uint32 * mask, afs_uint32 * mtu,
238 int max, char reason[], const char *fileName)
241 afs_uint32 existingAddr[MAXIPADDRS], existingMask[MAXIPADDRS],
242 existingMtu[MAXIPADDRS];
243 char line[MAX_NETFILE_LINE];
245 int i, existNu, count = 0;
256 /* get all network interfaces from the kernel */
258 rxi_getAllAddrMaskMtu(existingAddr, existingMask, existingMtu,
263 if ((fp = fopen(fileName, "r")) == 0) {
264 /* If file does not exist or is not readable, then
265 * use all interface addresses.
268 "Failed to open %s(%s)\nUsing all configured addresses\n",
269 fileName, strerror(errno));
270 for (i = 0; i < existNu; i++) {
271 final[i] = existingAddr[i];
272 mask[i] = existingMask[i];
273 mtu[i] = existingMtu[i];
278 /* For each line in the NetInfo file */
279 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
282 /* See if first char is an 'F' for fake */
283 /* Added to allow the fileserver to advertise fake IPS for use with
284 * the translation tables for NAT-like firewalls - defect 12462 */
285 for (fake = 0; ((fake < strlen(line)) && isspace(line[fake]));
287 if ((fake < strlen(line))
288 && ((line[fake] == 'f') || (line[fake] == 'F'))) {
294 lineNo++; /* input line number */
295 addr = extract_Addr(&line[fake], strlen(&line[fake]));
297 if (addr == AFS_IPINVALID) { /* syntactically invalid */
298 fprintf(stderr, "afs:%s : line %d : parse error\n", fileName,
302 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
306 /* See if it is an address that really exists */
307 for (i = 0; i < existNu; i++) {
308 if (existingAddr[i] == addr)
311 if ((i >= existNu) && (!fake))
312 continue; /* not found/fake - ignore */
314 /* Check if it is a duplicate address we alread have */
315 for (l = 0; l < count; l++) {
316 if (final[l] == addr)
320 fprintf(stderr, "afs:%x specified twice in NetInfo file\n",
322 continue; /* duplicate addr - ignore */
325 if (count > max) { /* no more space */
327 "afs:Too many interfaces. The current kernel configuration supports a maximum of %d interfaces\n",
330 fprintf(stderr, "Client (2) also has address %s\n", line);
332 mask[count] = 0xffffffff;
333 mtu[count] = htonl(1500);
336 final[count] = existingAddr[i];
337 mask[count] = existingMask[i];
338 mtu[count] = existingMtu[i];
343 /* in case of any error, we use all the interfaces present */
346 "Error in reading/parsing Interface file\nUsing all configured interface addresses \n");
347 for (i = 0; i < existNu; i++) {
348 final[i] = existingAddr[i];
349 mask[i] = existingMask[i];
350 mtu[i] = existingMtu[i];
360 * Given two arrays of addresses, masks and mtus find the common ones
361 * and return them in the first buffer. Return number of common
365 filterAddrs(afs_uint32 addr1[], afs_uint32 addr2[], afs_uint32 mask1[],
366 afs_uint32 mask2[], afs_uint32 mtu1[], afs_uint32 mtu2[], int n1,
369 afs_uint32 taddr[MAXIPADDRS];
370 afs_uint32 tmask[MAXIPADDRS];
371 afs_uint32 tmtu[MAXIPADDRS];
372 int count = 0, i = 0, j = 0, found = 0;
381 for (i = 0; i < n1; i++) {
383 for (j = 0; j < n2; j++) {
384 if (addr1[i] == addr2[j]) {
390 /* Always mask loopback address */
391 if (found && addr1[i] == INADDR_LOOPBACK)
395 taddr[count] = addr1[i];
396 tmask[count] = mask1[i];
397 tmtu[count] = mtu1[i];
401 /* copy everything into addr1, mask1 and mtu1 */
402 for (i = 0; i < count; i++) {
409 /* and zero out the rest */
410 for (i = count; i < n1; i++) {
421 * parse both netinfo and netrerstrict files and return the final
422 * set of IP addresses to use
424 /* max - Entries in addrbuf, maskbuf and mtubuf */
426 parseNetFiles(afs_uint32 addrbuf[], afs_uint32 maskbuf[], afs_uint32 mtubuf[],
427 afs_uint32 max, char reason[], const char *niFileName,
428 const char *nrFileName)
430 afs_uint32 addrbuf1[MAXIPADDRS], maskbuf1[MAXIPADDRS],
432 afs_uint32 addrbuf2[MAXIPADDRS], maskbuf2[MAXIPADDRS],
435 afs_uint32 nAddrs2 = 0;
439 ParseNetInfoFile(addrbuf1, maskbuf1, mtubuf1, MAXIPADDRS, reason,
442 parseNetRestrictFile(addrbuf2, maskbuf2, mtubuf2, MAXIPADDRS,
443 &nAddrs2, reason, nrFileName);
444 if ((nAddrs1 < 0) && (code)) {
447 } else if ((nAddrs1 > 0) && (code)) {
448 /* netinfo succeeded and netrestrict failed */
449 for (i = 0; ((i < nAddrs1) && (i < max)); i++) {
450 addrbuf[i] = addrbuf1[i];
452 maskbuf[i] = maskbuf1[i];
453 mtubuf[i] = mtubuf1[i];
457 } else if ((!code) && (nAddrs1 < 0)) {
458 /* netrestrict succeeded and netinfo failed */
459 for (i = 0; ((i < nAddrs2) && (i < max)); i++) {
460 addrbuf[i] = addrbuf2[i];
462 maskbuf[i] = maskbuf2[i];
463 mtubuf[i] = mtubuf2[i];
467 } else if ((!code) && (nAddrs1 >= 0)) {
469 /* take the intersection of addrbuf1 and addrbuf2 */
471 filterAddrs(addrbuf1, addrbuf2, maskbuf1, maskbuf2, mtubuf1,
472 mtubuf2, nAddrs1, nAddrs2);
473 for (i = 0; ((i < code) && (i < max)); i++) {
474 addrbuf[i] = addrbuf1[i];
476 maskbuf[i] = maskbuf1[i];
477 mtubuf[i] = mtubuf1[i];