LCOV - code coverage report
Current view: top level - zebra - ipforward_proc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 76 0.0 %
Date: 2015-11-19 Functions: 0 7 0.0 %

          Line data    Source code
       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           0 : dropline (FILE *fp)
      36             : {
      37             :   int c;
      38             : 
      39           0 :   while ((c = getc (fp)) != '\n')
      40             :     ;
      41           0 : }
      42             : 
      43             : int
      44           0 : ipforward (void)
      45             : {
      46             :   FILE *fp;
      47           0 :   int ipforwarding = 0;
      48             :   char buf[10];
      49             : 
      50           0 :   fp = fopen (proc_net_snmp, "r");
      51             : 
      52           0 :   if (fp == NULL)
      53           0 :     return -1;
      54             : 
      55             :   /* We don't care about the first line. */
      56           0 :   dropline (fp);
      57             :   
      58             :   /* Get ip_statistics.IpForwarding : 
      59             :      1 => ip forwarding enabled 
      60             :      2 => ip forwarding off. */
      61           0 :   if (fgets (buf, 6, fp))
      62           0 :     sscanf (buf, "Ip: %d", &ipforwarding);
      63             : 
      64           0 :   fclose(fp);
      65             :   
      66           0 :   if (ipforwarding == 1)
      67           0 :     return 1;
      68             : 
      69           0 :   return 0;
      70             : }
      71             : 
      72             : /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
      73             : char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
      74             : 
      75             : int
      76           0 : ipforward_on (void)
      77             : {
      78             :   FILE *fp;
      79             :   
      80           0 :   if ( zserv_privs.change(ZPRIVS_RAISE) )
      81           0 :         zlog_err ("Can't raise privileges, %s", safe_strerror (errno) );
      82             : 
      83           0 :   fp = fopen (proc_ipv4_forwarding, "w");
      84             : 
      85           0 :   if (fp == NULL) {
      86           0 :     if ( zserv_privs.change(ZPRIVS_LOWER) )
      87           0 :       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
      88           0 :     return -1;
      89             :   }
      90             : 
      91           0 :   fprintf (fp, "1\n");
      92             : 
      93           0 :   fclose (fp);
      94             : 
      95           0 :   if ( zserv_privs.change(ZPRIVS_LOWER) )
      96           0 :     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
      97             : 
      98           0 :   return ipforward ();
      99             : }
     100             : 
     101             : int
     102           0 : ipforward_off (void)
     103             : {
     104             :   FILE *fp;
     105             : 
     106           0 :   if ( zserv_privs.change(ZPRIVS_RAISE) )
     107           0 :         zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
     108             : 
     109           0 :   fp = fopen (proc_ipv4_forwarding, "w");
     110             : 
     111           0 :   if (fp == NULL) {
     112           0 :     if ( zserv_privs.change(ZPRIVS_LOWER) )
     113           0 :       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     114           0 :     return -1;
     115             :   }
     116             : 
     117           0 :   fprintf (fp, "0\n");
     118             : 
     119           0 :   fclose (fp);
     120             : 
     121           0 :   if ( zserv_privs.change(ZPRIVS_LOWER) )
     122           0 :     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     123             : 
     124           0 :   return ipforward ();
     125             : }
     126             : #ifdef HAVE_IPV6
     127             : 
     128             : char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
     129             : 
     130             : int
     131           0 : ipforward_ipv6 (void)
     132             : {
     133             :   FILE *fp;
     134             :   char buf[5];
     135           0 :   int ipforwarding = 0;
     136             : 
     137           0 :   fp = fopen (proc_ipv6_forwarding, "r");
     138             : 
     139           0 :   if (fp == NULL)
     140           0 :     return -1;
     141             : 
     142           0 :   if (fgets (buf, 2, fp))
     143           0 :     sscanf (buf, "%d", &ipforwarding);
     144             : 
     145           0 :   fclose (fp);
     146           0 :   return ipforwarding;
     147             : }
     148             : 
     149             : int
     150           0 : ipforward_ipv6_on (void)
     151             : {
     152             :   FILE *fp;
     153             : 
     154           0 :   if ( zserv_privs.change(ZPRIVS_RAISE) )
     155           0 :         zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
     156             : 
     157           0 :   fp = fopen (proc_ipv6_forwarding, "w");
     158             : 
     159           0 :   if (fp == NULL) {
     160           0 :     if ( zserv_privs.change(ZPRIVS_LOWER) )
     161           0 :       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     162           0 :     return -1;
     163             :   }
     164             : 
     165           0 :   fprintf (fp, "1\n");
     166             : 
     167           0 :   fclose (fp);
     168             : 
     169           0 :   if ( zserv_privs.change(ZPRIVS_LOWER) )
     170           0 :     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     171             : 
     172           0 :   return ipforward_ipv6 ();
     173             : }
     174             : 
     175             : int
     176           0 : ipforward_ipv6_off (void)
     177             : {
     178             :   FILE *fp;
     179             : 
     180           0 :   if ( zserv_privs.change(ZPRIVS_RAISE) )
     181           0 :         zlog_err ("Can't raise privileges, %s", safe_strerror (errno));
     182             : 
     183           0 :   fp = fopen (proc_ipv6_forwarding, "w");
     184             : 
     185           0 :   if (fp == NULL) {
     186           0 :     if ( zserv_privs.change(ZPRIVS_LOWER) )
     187           0 :       zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     188           0 :     return -1;
     189             :   }
     190             : 
     191           0 :   fprintf (fp, "0\n");
     192             : 
     193           0 :   fclose (fp);
     194             : 
     195           0 :   if ( zserv_privs.change(ZPRIVS_LOWER) )
     196           0 :     zlog_err ("Can't lower privileges, %s", safe_strerror (errno));
     197             : 
     198           0 :   return ipforward_ipv6 ();
     199             : }
     200             : #endif /* HAVE_IPV6 */

Generated by: LCOV version 1.10