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