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
18 #include "../afs/param.h"
19 #include "../afs/sysincludes.h"
20 #include "../afs/afsincludes.h"
22 #include <netinet/in.h>
24 #include <sys/errno.h>
30 #define AFS_IPINVALID 0xffffffff /* invalid IP address */
31 #define AFS_IPINVALIDIGNORE 0xfffffffe /* no input given to extractAddr */
32 #define MAX_NETFILE_LINE 2048 /* length of a line in the netrestrict file */
33 #define MAXIPADDRS 1024 /* from afsd.c */
36 * The line parameter is a pointer to a buffer containing a string of
38 ** w.x.y.z # machineName
39 * returns the network interface IP Address in NBO
42 extract_Addr(line, maxSize)
52 /* skip empty spaces */
53 while ( isspace(*line) && maxSize ) {
57 /* skip empty lines */
58 if ( !maxSize || !*line ) return AFS_IPINVALIDIGNORE;
61 while ( ( *line != '.' )&&!isspace(*line) && maxSize) { /* extract nth byte */
62 if ( !isdigit(*line) ) return AFS_IPINVALID;
63 if ( i > 31 ) return AFS_IPINVALID; /* no space */
64 bytes[n][i++] = *line++;
67 if ( !maxSize ) return AFS_IPINVALID;
71 val[n] = strtol(bytes[n], &endPtr, 10);
72 if (( val[n]== 0 ) && ( errno != 0 || bytes[n] == endPtr)) /* no conversion */
76 retval = ( val[0] << 24 ) | ( val[1] << 16 ) | ( val[2] << 8 ) | val[3];
83 /* parseNetRestrictFile()
84 * Get a list of IP addresses for this host removing any address found
85 * in the config file (fileName parameter): /usr/vice/etc/NetRestrict
86 * for clients and /usr/afs/local/NetRestrict for servers.
88 * Returns the number of valid addresses in outAddrs[] and count in
89 * nAddrs. Returns 0 on success; or 1 if the config file was not
90 * there or empty (we still return the host's IP addresses). Returns
91 * -1 on fatal failure with reason in the reason argument (so the
92 * caller can choose to ignore the entire file but should write
93 * something to a log file).
95 * All addresses should be in NBO (as returned by rx_getAllAddr() and
96 * parsed by extract_Addr().
98 int parseNetRestrictFile(outAddrs, mask, mtu, maxAddrs, nAddrs, reason, fileName)
99 afs_int32 outAddrs[]; /* output address array */
100 afs_int32 *mask, *mtu; /* optional mask and mtu */
101 afs_uint32 maxAddrs; /* max number of addresses */
102 afs_uint32 *nAddrs; /* number of Addresses in output array */
103 char reason[]; /* reason for failure */
104 const char *fileName; /* filename to parse */
107 char line[MAX_NETFILE_LINE];
108 int lineNo, usedfile;
109 afs_uint32 i, neaddrs, nfaddrs, nOutaddrs;
110 afs_int32 addr, eAddrs[MAXIPADDRS], eMask[MAXIPADDRS], eMtu[MAXIPADDRS];
116 if (mask) assert(mtu);
120 for (i=0; i<maxAddrs; i++) outAddrs[i] = 0;
123 /* get all network interfaces from the kernel */
124 neaddrs = rxi_getAllAddrMaskMtu(eAddrs, eMask, eMtu, MAXIPADDRS);
126 sprintf(reason,"No existing IP interfaces found");
130 if ( (fp = fopen(fileName, "r")) == 0 ) {
131 sprintf(reason, "Could not open file %s for reading:%s",
132 fileName, strerror(errno));
136 /* For each line in the NetRestrict file */
139 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
140 lineNo++; /* input line number */
141 addr = extract_Addr(line, strlen(line));
142 if (addr == AFS_IPINVALID) { /* syntactically invalid */
143 fprintf(stderr,"%s : line %d : parse error - invalid IP\n", fileName, lineNo);
146 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
147 fprintf(stderr, "%s : line %d : invalid address ... ignoring\n",fileName, lineNo);
152 /* Check if we need to exclude this address */
153 for (i=0; i<neaddrs; i++) {
154 if (eAddrs[i] && (eAddrs[i] == addr)) {
155 eAddrs[i] = 0; /* Yes - exclude it by zeroing it for now */
163 sprintf(reason,"No valid IP addresses in %s\n",fileName);
168 /* Collect the addresses we have left to return */
170 for (i=0; i<neaddrs; i++) {
171 if (!eAddrs[i]) continue;
172 outAddrs[nOutaddrs] = eAddrs[i];
174 mask[nOutaddrs] = eMask[i];
175 mtu[nOutaddrs] = eMtu[i];
177 if (++nOutaddrs >= maxAddrs) break;
179 if (nOutaddrs == 0) {
180 sprintf(reason, "No addresses to use after parsing %s",fileName);
184 return (usedfile ? 0 : 1); /* 0=>used the file. 1=>didn't use file */
190 * this function reads in stuff from InterfaceAddr file in
191 * /usr/vice/etc ( if it exists ) and verifies the addresses
193 * 'final' contains all those addresses that are found to
194 * be valid. This function returns the number of valid
195 * interface addresses. Pulled out from afsd.c
198 ParseNetInfoFile(final, mask, mtu, max, reason, fileName)
199 afs_int32 *final, *mask, *mtu;
200 int max; /* max number of interfaces */
202 const char *fileName;
205 afs_int32 existingAddr[MAXIPADDRS], existingMask[MAXIPADDRS], existingMtu[MAXIPADDRS];
206 char line[MAX_NETFILE_LINE];
208 int i, existNu, count = 0;
219 /* get all network interfaces from the kernel */
220 existNu = rxi_getAllAddrMaskMtu(existingAddr,existingMask,existingMtu,MAXIPADDRS);
224 if ( (fp = fopen(fileName, "r")) == 0 ) {
225 /* If file does not exist or is not readable, then
226 * use all interface addresses.
228 sprintf(reason,"Failed to open %s(%s)\nUsing all configured addresses\n",
229 fileName, strerror(errno));
230 for ( i=0; i < existNu; i++) {
231 final[i] = existingAddr[i];
232 mask[i] = existingMask[i];
233 mtu[i] = existingMtu[i];
238 /* For each line in the NetInfo file */
239 while ( fgets(line, MAX_NETFILE_LINE, fp) != NULL ) {
240 lineNo++; /* input line number */
241 addr = extract_Addr(line, MAX_NETFILE_LINE);
243 if (addr == AFS_IPINVALID) { /* syntactically invalid */
244 fprintf(stderr,"afs:%s : line %d : parse error\n", fileName, lineNo);
247 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
251 /* See if it is an address that really exists */
252 for (i=0; i < existNu; i++) {
253 if (existingAddr[i] == addr) break;
255 if (i >= existNu) continue; /* not found - ignore */
257 /* Check if it is a duplicate address we alread have */
258 for (l=0; l < count; l++) {
259 if ( final[l] == addr ) break;
262 fprintf(stderr,"afs:%x specified twice in NetInfo file\n", ntohl(addr));
263 continue; /* duplicate addr - ignore */
266 if ( count == max ) { /* no more space */
267 fprintf(stderr,"afs:Too many interfaces. The current kernel configuration supports a maximum of %d interfaces\n", max);
269 final[count] = existingAddr[i];
270 mask[count] = existingMask[i];
271 mtu[count] = existingMtu[i];
276 /* in case of any error, we use all the interfaces present */
278 sprintf(reason,"Error in reading/parsing Interface file\nUsing all configured interface addresses \n");
279 for ( i=0; i < existNu; i++) {
280 final[i] = existingAddr[i];
281 mask[i] = existingMask[i];
282 mtu[i] = existingMtu[i];
292 * Given two arrays of addresses, masks and mtus find the common ones
293 * and return them in the first buffer. Return number of common
296 int filterAddrs(addr1,addr2,mask1,mask2,mtu1,mtu2,n1,n2)
297 u_long addr1[],addr2[];
298 afs_int32 mask1[], mask2[];
299 afs_int32 mtu1[], mtu2[];
301 u_long taddr[MAXIPADDRS];
302 afs_int32 tmask[MAXIPADDRS];
303 afs_int32 tmtu[MAXIPADDRS];
304 int count=0,i=0,j=0,found=0;
316 if(addr1[i]==addr2[j]) {
322 taddr[count]=addr1[i];
323 tmask[count]=mask1[i];
328 /* copy everything into addr1, mask1 and mtu1 */
329 for(i=0;i<count;i++) {
336 /* and zero out the rest */
337 for(i=count;i<n1;i++) {
348 * parse both netinfo and netrerstrict files and return the final
349 * set of IP addresses to use
351 int parseNetFiles(addrbuf, maskbuf,mtubuf,max,reason, niFileName, nrFileName)
355 u_long max; /* Entries in addrbuf, maskbuf and mtubuf */
357 const char *niFileName;
358 const char *nrFileName;
360 afs_int32 addrbuf1[MAXIPADDRS],maskbuf1[MAXIPADDRS], mtubuf1[MAXIPADDRS];
361 afs_int32 addrbuf2[MAXIPADDRS],maskbuf2[MAXIPADDRS], mtubuf2[MAXIPADDRS];
363 afs_uint32 nAddrs2=0;
366 nAddrs1 = ParseNetInfoFile(addrbuf1, maskbuf1, mtubuf1, MAXIPADDRS,reason,
368 code = parseNetRestrictFile(addrbuf2,maskbuf2,mtubuf2,
369 MAXIPADDRS,&nAddrs2,reason,
371 if ((nAddrs1 < 0) && (code)) {
375 else if ((nAddrs1 > 0) && (code)) {
376 /* netinfo succeeded and netrestrict failed */
377 for(i=0;((i<nAddrs1)&&(i<max));i++) {
378 addrbuf[i]=addrbuf1[i];
380 maskbuf[i]=maskbuf1[i];
381 mtubuf[i]=mtubuf1[i];
386 else if ((!code) && (nAddrs1 < 0)) {
387 /* netrestrict succeeded and netinfo failed */
388 for (i=0;((i<nAddrs2)&&(i<max));i++) {
389 addrbuf[i]=addrbuf2[i];
391 maskbuf[i]=maskbuf2[i];
392 mtubuf[i]=mtubuf2[i];
397 else if ((!code) && (nAddrs1 >= 0)) {
399 /* take the intersection of addrbuf1 and addrbuf2 */
400 code=filterAddrs(addrbuf1,addrbuf2,maskbuf1,maskbuf2,mtubuf1,mtubuf2,
402 for(i=0;((i<code)&&(i<max));i++) {
403 addrbuf[i]=addrbuf1[i];
405 maskbuf[i]=maskbuf1[i];
406 mtubuf[i]=mtubuf1[i];