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
25char proc_net_snmp[] = "/proc/net/snmp";
26
27static void
28dropline (FILE *fp)
29{
30  int c;
31
32  while ((c = getc (fp)) != '\n')
33    ;
34}
35
36int
37ipforward ()
38{
39  FILE *fp;
40  int ipforwarding = 0;
41  char *pnt;
42  char buf[10];
43
44  fp = fopen (proc_net_snmp, "r");
45
46  if (fp == NULL)
47    return -1;
48
49  /* We don't care about the first line. */
50  dropline (fp);
51
52  /* Get ip_statistics.IpForwarding :
53     1 => ip forwarding enabled
54     2 => ip forwarding off. */
55  pnt = fgets (buf, 6, fp);
56  sscanf (buf, "Ip: %d", &ipforwarding);
57
58  if (ipforwarding == 1)
59    return 1;
60
61  return 0;
62}
63
64/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
65char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
66
67int
68ipforward_on ()
69{
70  FILE *fp;
71
72  fp = fopen (proc_ipv4_forwarding, "w");
73
74  if (fp == NULL)
75    return -1;
76
77  fprintf (fp, "1\n");
78
79  fclose (fp);
80
81  return ipforward ();
82}
83
84int
85ipforward_off ()
86{
87  FILE *fp;
88
89  fp = fopen (proc_ipv4_forwarding, "w");
90
91  if (fp == NULL)
92    return -1;
93
94  fprintf (fp, "0\n");
95
96  fclose (fp);
97
98  return ipforward ();
99}
100#ifdef HAVE_IPV6
101
102char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding";
103
104int
105ipforward_ipv6 ()
106{
107  FILE *fp;
108  char buf[5];
109  int ipforwarding = 0;
110
111  fp = fopen (proc_ipv6_forwarding, "r");
112
113  if (fp == NULL)
114    return -1;
115
116  fgets (buf, 2, fp);
117  sscanf (buf, "%d", &ipforwarding);
118
119  return ipforwarding;
120}
121
122int
123ipforward_ipv6_on ()
124{
125  FILE *fp;
126
127  fp = fopen (proc_ipv6_forwarding, "w");
128
129  if (fp == NULL)
130    return -1;
131
132  fprintf (fp, "1\n");
133
134  fclose (fp);
135
136  return ipforward_ipv6 ();
137}
138
139int
140ipforward_ipv6_off ()
141{
142  FILE *fp;
143
144  fp = fopen (proc_ipv6_forwarding, "w");
145
146  if (fp == NULL)
147    return -1;
148
149  fprintf (fp, "0\n");
150
151  fclose (fp);
152
153  return ipforward_ipv6 ();
154}
155#endif /* HAVE_IPV6 */
156