File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / zebra / ipforward_proc.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 17:26:11 2012 UTC (12 years, 4 months ago) by misho
Branches: quagga, MAIN
CVS tags: v0_99_20_1, v0_99_20, HEAD
quagga

    1: /*
    2:  * Fetch ipforward value by reading /proc filesystem.
    3:  * Copyright (C) 1997 Kunihiro Ishiguro
    4:  *
    5:  * This file is part of GNU Zebra.
    6:  *
    7:  * GNU Zebra is free software; you can redistribute it and/or modify it
    8:  * under the terms of the GNU General Public License as published by the
    9:  * Free Software Foundation; either version 2, or (at your option) any
   10:  * later version.
   11:  *
   12:  * GNU Zebra is distributed in the hope that it will be useful, but
   13:  * WITHOUT ANY WARRANTY; without even the implied warranty of
   14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15:  * General Public License for more details.
   16:  *
   17:  * You should have received a copy of the GNU General Public License
   18:  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
   19:  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   20:  * 02111-1307, USA.  
   21:  */
   22: 
   23: #include <zebra.h>
   24: 
   25: #include "log.h"
   26: #include "privs.h"
   27: 
   28: #include "zebra/ipforward.h"
   29: 
   30: extern struct zebra_privs_t zserv_privs;
   31: 
   32: char proc_net_snmp[] = "/proc/net/snmp";
   33: 
   34: static void
   35: dropline (FILE *fp)
   36: {
   37:   int c;
   38: 
   39:   while ((c = getc (fp)) != '\n')
   40:     ;
   41: }
   42: 
   43: int
   44: ipforward (void)
   45: {
   46:   FILE *fp;
   47:   int ipforwarding = 0;
   48:   char *pnt;
   49:   char buf[10];
   50: 
   51:   fp = fopen (proc_net_snmp, "r");
   52: 
   53:   if (fp == NULL)
   54:     return -1;
   55: 
   56:   /* We don't care about the first line. */
   57:   dropline (fp);
   58:   
   59:   /* Get ip_statistics.IpForwarding : 
   60:      1 => ip forwarding enabled 
   61:      2 => ip forwarding off. */
   62:   pnt = fgets (buf, 6, fp);
   63:   sscanf (buf, "Ip: %d", &ipforwarding);
   64: 
   65:   fclose(fp);
   66:   
   67:   if (ipforwarding == 1)
   68:     return 1;
   69: 
   70:   return 0;
   71: }
   72: 
   73: /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
   74: char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
   75: 
   76: int
   77: ipforward_on (void)
   78: {
   79:   FILE *fp;
   80:   
   81:   if ( zserv_privs.change(ZPRIVS_RAISE) )
   82:   	zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
   83: 
   84:   fp = fopen (proc_ipv4_forwarding, "w");
   85: 
   86:   if (fp == NULL) {
   87:     if ( zserv_privs.change(ZPRIVS_LOWER) )
   88:       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
   89:     return -1;
   90:   }
   91: 
   92:   fprintf (fp, "1\n");
   93: 
   94:   fclose (fp);
   95: 
   96:   if ( zserv_privs.change(ZPRIVS_LOWER) )
   97:     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
   98: 
   99:   return ipforward ();
  100: }
  101: 
  102: int
  103: ipforward_off (void)
  104: {
  105:   FILE *fp;
  106: 
  107:   if ( zserv_privs.change(ZPRIVS_RAISE) )
  108:   	zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  109: 
  110:   fp = fopen (proc_ipv4_forwarding, "w");
  111: 
  112:   if (fp == NULL) {
  113:     if ( zserv_privs.change(ZPRIVS_LOWER) )
  114:       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  115:     return -1;
  116:   }
  117: 
  118:   fprintf (fp, "0\n");
  119: 
  120:   fclose (fp);
  121: 
  122:   if ( zserv_privs.change(ZPRIVS_LOWER) )
  123:     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  124: 
  125:   return ipforward ();
  126: }
  127: #ifdef HAVE_IPV6
  128: 
  129: char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
  130: 
  131: int
  132: ipforward_ipv6 (void)
  133: {
  134:   FILE *fp;
  135:   char buf[5];
  136:   int ipforwarding = 0;
  137: 
  138:   fp = fopen (proc_ipv6_forwarding, "r");
  139: 
  140:   if (fp == NULL)
  141:     return -1;
  142: 
  143:   fgets (buf, 2, fp);
  144:   sscanf (buf, "%d", &ipforwarding);
  145: 
  146:   fclose (fp);
  147:   return ipforwarding;
  148: }
  149: 
  150: int
  151: ipforward_ipv6_on (void)
  152: {
  153:   FILE *fp;
  154: 
  155:   if ( zserv_privs.change(ZPRIVS_RAISE) )
  156:   	zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  157: 
  158:   fp = fopen (proc_ipv6_forwarding, "w");
  159: 
  160:   if (fp == NULL) {
  161:     if ( zserv_privs.change(ZPRIVS_LOWER) )
  162:       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  163:     return -1;
  164:   }
  165: 
  166:   fprintf (fp, "1\n");
  167: 
  168:   fclose (fp);
  169: 
  170:   if ( zserv_privs.change(ZPRIVS_LOWER) )
  171:     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  172: 
  173:   return ipforward_ipv6 ();
  174: }
  175: 
  176: int
  177: ipforward_ipv6_off (void)
  178: {
  179:   FILE *fp;
  180: 
  181:   if ( zserv_privs.change(ZPRIVS_RAISE) )
  182:   	zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
  183: 
  184:   fp = fopen (proc_ipv6_forwarding, "w");
  185: 
  186:   if (fp == NULL) {
  187:     if ( zserv_privs.change(ZPRIVS_LOWER) )
  188:       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  189:     return -1;
  190:   }
  191: 
  192:   fprintf (fp, "0\n");
  193: 
  194:   fclose (fp);
  195: 
  196:   if ( zserv_privs.change(ZPRIVS_LOWER) )
  197:     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
  198: 
  199:   return ipforward_ipv6 ();
  200: }
  201: #endif /* HAVE_IPV6 */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>