Home | History | Annotate | Download | only in dhcpcd
      1 /*
      2  * dhcpcd - DHCP client daemon
      3  * Copyright (c) 2006-2012 Roy Marples <roy (at) marples.name>
      4  * All rights reserved
      5 
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <errno.h>
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 #include <syslog.h>
     33 
     34 #include "common.h"
     35 #include "platform.h"
     36 
     37 static const char *mproc =
     38 #if defined(__alpha__)
     39 	"system type"
     40 #elif defined(__arm__)
     41 	"Hardware"
     42 #elif defined(__avr32__)
     43 	"cpu family"
     44 #elif defined(__bfin__)
     45 	"BOARD Name"
     46 #elif defined(__cris__)
     47 	"cpu model"
     48 #elif defined(__frv__)
     49 	"System"
     50 #elif defined(__i386__) || defined(__x86_64__)
     51 	"vendor_id"
     52 #elif defined(__ia64__)
     53 	"vendor"
     54 #elif defined(__hppa__)
     55 	"model"
     56 #elif defined(__m68k__)
     57 	"MMU"
     58 #elif defined(__mips__)
     59 	"system type"
     60 #elif defined(__powerpc__) || defined(__powerpc64__)
     61 	"machine"
     62 #elif defined(__s390__) || defined(__s390x__)
     63 	"Manufacturer"
     64 #elif defined(__sh__)
     65 	"machine"
     66 #elif defined(sparc) || defined(__sparc__)
     67 	"cpu"
     68 #elif defined(__vax__)
     69 	"cpu"
     70 #else
     71 	NULL
     72 #endif
     73 	;
     74 
     75 char *
     76 hardware_platform(void)
     77 {
     78 	FILE *fp;
     79 	char *buf, *p;
     80 
     81 	if (mproc == NULL) {
     82 		errno = EINVAL;
     83 		return NULL;
     84 	}
     85 
     86 	fp = fopen("/proc/cpuinfo", "r");
     87 	if (fp == NULL)
     88 		return NULL;
     89 
     90 	p = NULL;
     91 	while ((buf = get_line(fp))) {
     92 		if (strncmp(buf, mproc, strlen(mproc)) == 0) {
     93 			p = strchr(buf, ':');
     94 			if (p != NULL && ++p != NULL) {
     95 				while (*p == ' ')
     96 					p++;
     97 				break;
     98 			}
     99 		}
    100 	}
    101 	fclose(fp);
    102 
    103 	if (p == NULL)
    104 		errno = ESRCH;
    105 	return p;
    106 }
    107 
    108 static int
    109 check_proc_int(const char *path)
    110 {
    111 	FILE *fp;
    112 	char *buf;
    113 
    114 	fp = fopen(path, "r");
    115 	if (fp == NULL)
    116 		return -1;
    117 	buf = get_line(fp);
    118 	fclose(fp);
    119 	if (buf == NULL)
    120 		return -1;
    121 	return atoi(buf);
    122 }
    123 
    124 static const char *prefix = "/proc/sys/net/ipv6/conf";
    125 
    126 int
    127 check_ipv6(const char *ifname)
    128 {
    129 	int r;
    130 	char path[256];
    131 
    132 	if (ifname == NULL)
    133 		ifname = "all";
    134 
    135 	snprintf(path, sizeof(path), "%s/%s/accept_ra", prefix, ifname);
    136 	r = check_proc_int(path);
    137 	if (r != 1 && r != 2) {
    138 		syslog(LOG_WARNING,
    139 		    "%s: not configured to accept IPv6 RAs", ifname);
    140 		return 0;
    141 	}
    142 
    143 	if (r != 2) {
    144 		snprintf(path, sizeof(path), "%s/%s/forwarding",
    145 		    prefix, ifname);
    146 		if (check_proc_int(path) != 0) {
    147 			syslog(LOG_WARNING,
    148 			    "%s: configured as a router, not a host", ifname);
    149 			return 0;
    150 		}
    151 	}
    152 	return 1;
    153 }
    154