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
16 #include "../afs/param.h"
18 #include <afs/param.h>
20 #include <afsconfig.h>
35 #include "../afs/sysincludes.h"
36 #include "../afs/afsincludes.h"
39 #include <sys/types.h>
41 #include <netinet/in.h>
43 #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 */
55 * The line parameter is a pointer to a buffer containing a string of
57 ** w.x.y.z # machineName
58 * returns the network interface IP Address in NBO
61 extract_Addr(line, maxSize)
71 /* skip empty spaces */
72 while ( isspace(*line) && maxSize ) {
76 /* skip empty lines */
77 if ( !maxSize || !*line ) return AFS_IPINVALIDIGNORE;
80 while ( ( *line != '.' )&&!isspace(*line) && maxSize) { /* extract nth byte */
81 if ( !isdigit(*line) ) return AFS_IPINVALID;
82 if ( i > 31 ) return AFS_IPINVALID; /* no space */
83 bytes[n][i++] = *line++;
86 if ( !maxSize ) return AFS_IPINVALID;
90 val[n] = strtol(bytes[n], &endPtr, 10);
91 if (( val[n]== 0 ) && ( errno != 0 || bytes[n] == endPtr)) /* no conversion */
95 retval = ( val[0] << 24 ) | ( val[1] << 16 ) | ( val[2] << 8 ) | val[3];
102 /* parseNetRestrictFile()
103 * Get a list of IP addresses for this host removing any address found
104 * in the config file (fileName parameter): /usr/vice/etc/NetRestrict
105 * for clients and /usr/afs/local/NetRestrict for servers.
107 * Returns the number of valid addresses in outAddrs[] and count in
108 * nAddrs. Returns 0 on success; or 1 if the config file was not
109 * there or empty (we still return the host's IP addresses). Returns
110 * -1 on fatal failure with reason in the reason argument (so the
111 * caller can choose to ignore the entire file but should write
112 * something to a log file).
114 * All addresses should be in NBO (as returned by rx_getAllAddr() and
115 * parsed by extract_Addr().
117 int parseNetRestrictFile(outAddrs, mask, mtu, maxAddrs, nAddrs, reason, fileName)
118 afs_uint32 outAddrs[]; /* output address array */
119 afs_uint32 *mask, *mtu; /* optional mask and mtu */
120 afs_uint32 maxAddrs; /* max number of addresses */
121 afs_uint32 *nAddrs; /* number of Addresses in output array */
122 char reason[]; /* reason for failure */
123 const char *fileName; /* filename to parse */
126 char line[MAX_NETFILE_LINE];
127 int lineNo, usedfile = 0;
128 afs_uint32 i, neaddrs, nOutaddrs;
129 afs_uint32 addr, eAddrs[MAXIPADDRS], eMask[MAXIPADDRS], eMtu[MAXIPADDRS];
135 if (mask) assert(mtu);
139 for (i=0; i<maxAddrs; i++) outAddrs[i] = 0;
142 /* get all network interfaces from the kernel */
143 neaddrs = rxi_getAllAddrMaskMtu(eAddrs, eMask, eMtu, MAXIPADDRS);
145 sprintf(reason,"No existing IP interfaces found");
149 if ( (fp = fopen(fileName, "r")) == 0 ) {
150 sprintf(reason, "Could not open file %s for reading:%s",
151 fileName, strerror(errno));
155 /* For each line in the NetRestrict file */
158 while (fgets(line, MAX_NETFILE_LINE, fp) != NULL) {
159 lineNo++; /* input line number */
160 addr = extract_Addr(line, strlen(line));
161 if (addr == AFS_IPINVALID) { /* syntactically invalid */
162 fprintf(stderr,"%s : line %d : parse error - invalid IP\n", fileName, lineNo);
165 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
166 fprintf(stderr, "%s : line %d : invalid address ... ignoring\n",fileName, lineNo);
171 /* Check if we need to exclude this address */
172 for (i=0; i<neaddrs; i++) {
173 if (eAddrs[i] && (eAddrs[i] == addr)) {
174 eAddrs[i] = 0; /* Yes - exclude it by zeroing it for now */
182 sprintf(reason,"No valid IP addresses in %s\n",fileName);
187 /* Collect the addresses we have left to return */
189 for (i=0; i<neaddrs; i++) {
190 if (!eAddrs[i]) continue;
191 outAddrs[nOutaddrs] = eAddrs[i];
193 mask[nOutaddrs] = eMask[i];
194 mtu[nOutaddrs] = eMtu[i];
196 if (++nOutaddrs >= maxAddrs) break;
198 if (nOutaddrs == 0) {
199 sprintf(reason, "No addresses to use after parsing %s",fileName);
203 return (usedfile ? 0 : 1); /* 0=>used the file. 1=>didn't use file */
209 * this function reads in stuff from InterfaceAddr file in
210 * /usr/vice/etc ( if it exists ) and verifies the addresses
212 * 'final' contains all those addresses that are found to
213 * be valid. This function returns the number of valid
214 * interface addresses. Pulled out from afsd.c
217 ParseNetInfoFile(final, mask, mtu, max, reason, fileName)
218 afs_uint32 *final, *mask, *mtu;
219 int max; /* max number of interfaces */
221 const char *fileName;
224 afs_uint32 existingAddr[MAXIPADDRS], existingMask[MAXIPADDRS], existingMtu[MAXIPADDRS];
225 char line[MAX_NETFILE_LINE];
227 int i, existNu, count = 0;
238 /* get all network interfaces from the kernel */
239 existNu = rxi_getAllAddrMaskMtu(existingAddr,existingMask,existingMtu,MAXIPADDRS);
243 if ( (fp = fopen(fileName, "r")) == 0 ) {
244 /* If file does not exist or is not readable, then
245 * use all interface addresses.
247 sprintf(reason,"Failed to open %s(%s)\nUsing all configured addresses\n",
248 fileName, strerror(errno));
249 for ( i=0; i < existNu; i++) {
250 final[i] = existingAddr[i];
251 mask[i] = existingMask[i];
252 mtu[i] = existingMtu[i];
257 /* For each line in the NetInfo file */
258 while ( fgets(line, MAX_NETFILE_LINE, fp) != NULL ) {
259 lineNo++; /* input line number */
260 addr = extract_Addr(line, MAX_NETFILE_LINE);
262 if (addr == AFS_IPINVALID) { /* syntactically invalid */
263 fprintf(stderr,"afs:%s : line %d : parse error\n", fileName, lineNo);
266 if (addr == AFS_IPINVALIDIGNORE) { /* ignore error */
270 /* See if it is an address that really exists */
271 for (i=0; i < existNu; i++) {
272 if (existingAddr[i] == addr) break;
274 if (i >= existNu) continue; /* not found - ignore */
276 /* Check if it is a duplicate address we alread have */
277 for (l=0; l < count; l++) {
278 if ( final[l] == addr ) break;
281 fprintf(stderr,"afs:%x specified twice in NetInfo file\n", ntohl(addr));
282 continue; /* duplicate addr - ignore */
285 if ( count == max ) { /* no more space */
286 fprintf(stderr,"afs:Too many interfaces. The current kernel configuration supports a maximum of %d interfaces\n", max);
288 final[count] = existingAddr[i];
289 mask[count] = existingMask[i];
290 mtu[count] = existingMtu[i];
295 /* in case of any error, we use all the interfaces present */
297 sprintf(reason,"Error in reading/parsing Interface file\nUsing all configured interface addresses \n");
298 for ( i=0; i < existNu; i++) {
299 final[i] = existingAddr[i];
300 mask[i] = existingMask[i];
301 mtu[i] = existingMtu[i];
311 * Given two arrays of addresses, masks and mtus find the common ones
312 * and return them in the first buffer. Return number of common
315 int filterAddrs(addr1,addr2,mask1,mask2,mtu1,mtu2,n1,n2)
316 afs_uint32 addr1[],addr2[];
317 afs_uint32 mask1[], mask2[];
318 afs_uint32 mtu1[], mtu2[];
320 afs_uint32 taddr[MAXIPADDRS];
321 afs_uint32 tmask[MAXIPADDRS];
322 afs_uint32 tmtu[MAXIPADDRS];
323 int count=0,i=0,j=0,found=0;
335 if(addr1[i]==addr2[j]) {
341 taddr[count]=addr1[i];
342 tmask[count]=mask1[i];
347 /* copy everything into addr1, mask1 and mtu1 */
348 for(i=0;i<count;i++) {
355 /* and zero out the rest */
356 for(i=count;i<n1;i++) {
367 * parse both netinfo and netrerstrict files and return the final
368 * set of IP addresses to use
370 int parseNetFiles(addrbuf, maskbuf,mtubuf,max,reason, niFileName, nrFileName)
371 afs_uint32 addrbuf[];
372 afs_uint32 maskbuf[];
374 afs_uint32 max; /* Entries in addrbuf, maskbuf and mtubuf */
376 const char *niFileName;
377 const char *nrFileName;
379 afs_uint32 addrbuf1[MAXIPADDRS],maskbuf1[MAXIPADDRS], mtubuf1[MAXIPADDRS];
380 afs_uint32 addrbuf2[MAXIPADDRS],maskbuf2[MAXIPADDRS], mtubuf2[MAXIPADDRS];
382 afs_uint32 nAddrs2=0;
385 nAddrs1 = ParseNetInfoFile(addrbuf1, maskbuf1, mtubuf1, MAXIPADDRS,reason,
387 code = parseNetRestrictFile(addrbuf2,maskbuf2,mtubuf2,
388 MAXIPADDRS,&nAddrs2,reason,
390 if ((nAddrs1 < 0) && (code)) {
394 else if ((nAddrs1 > 0) && (code)) {
395 /* netinfo succeeded and netrestrict failed */
396 for(i=0;((i<nAddrs1)&&(i<max));i++) {
397 addrbuf[i]=addrbuf1[i];
399 maskbuf[i]=maskbuf1[i];
400 mtubuf[i]=mtubuf1[i];
405 else if ((!code) && (nAddrs1 < 0)) {
406 /* netrestrict succeeded and netinfo failed */
407 for (i=0;((i<nAddrs2)&&(i<max));i++) {
408 addrbuf[i]=addrbuf2[i];
410 maskbuf[i]=maskbuf2[i];
411 mtubuf[i]=mtubuf2[i];
416 else if ((!code) && (nAddrs1 >= 0)) {
418 /* take the intersection of addrbuf1 and addrbuf2 */
419 code=filterAddrs(addrbuf1,addrbuf2,maskbuf1,maskbuf2,mtubuf1,mtubuf2,
421 for(i=0;((i<code)&&(i<max));i++) {
422 addrbuf[i]=addrbuf1[i];
424 maskbuf[i]=maskbuf1[i];
425 mtubuf[i]=mtubuf1[i];