Coverage Report

Created: 2025-10-08 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/zebra/ipforward_proc.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * Fetch ipforward value by reading /proc filesystem.
4
 * Copyright (C) 1997 Kunihiro Ishiguro
5
 */
6
7
#include <zebra.h>
8
9
#ifdef GNU_LINUX
10
11
#include "log.h"
12
#include "privs.h"
13
14
#include "zebra/ipforward.h"
15
16
extern struct zebra_privs_t zserv_privs;
17
18
static const char proc_net_snmp[] = "/proc/net/snmp";
19
20
static void dropline(FILE *fp)
21
0
{
22
0
  while (getc(fp) != '\n')
23
0
    ;
24
0
}
25
26
int ipforward(void)
27
0
{
28
0
  int ret = 0;
29
0
  FILE *fp;
30
0
  int ipforwarding = 0;
31
0
  char buf[10];
32
33
0
  fp = fopen(proc_net_snmp, "r");
34
35
0
  if (fp == NULL)
36
0
    return -1;
37
38
  /* We don't care about the first line. */
39
0
  dropline(fp);
40
41
  /* Get ip_statistics.IpForwarding :
42
     1 => ip forwarding enabled
43
     2 => ip forwarding off. */
44
0
  if (fgets(buf, 6, fp))
45
0
    ret = sscanf(buf, "Ip: %d", &ipforwarding);
46
47
0
  fclose(fp);
48
49
0
  if (ret == 1 && ipforwarding == 1)
50
0
    return 1;
51
52
0
  return 0;
53
0
}
54
55
/* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */
56
static const char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward";
57
58
int ipforward_on(void)
59
0
{
60
0
  FILE *fp;
61
62
0
  frr_with_privs(&zserv_privs) {
63
64
0
    fp = fopen(proc_ipv4_forwarding, "w");
65
66
0
    if (fp == NULL) {
67
0
      return -1;
68
0
    }
69
70
0
    fprintf(fp, "1\n");
71
72
0
    fclose(fp);
73
74
0
  }
75
76
0
  return ipforward();
77
0
}
78
79
int ipforward_off(void)
80
0
{
81
0
  FILE *fp;
82
83
0
  frr_with_privs(&zserv_privs) {
84
85
0
    fp = fopen(proc_ipv4_forwarding, "w");
86
87
0
    if (fp == NULL) {
88
0
      return -1;
89
0
    }
90
91
0
    fprintf(fp, "0\n");
92
93
0
    fclose(fp);
94
95
0
  }
96
97
0
  return ipforward();
98
0
}
99
100
static const char proc_ipv6_forwarding[] =
101
  "/proc/sys/net/ipv6/conf/all/forwarding";
102
103
int ipforward_ipv6(void)
104
0
{
105
0
  int ret = 0;
106
0
  FILE *fp;
107
0
  char buf[5];
108
0
  int ipforwarding = 0;
109
110
0
  fp = fopen(proc_ipv6_forwarding, "r");
111
112
0
  if (fp == NULL)
113
0
    return -1;
114
115
0
  if (fgets(buf, 2, fp))
116
0
    ret = sscanf(buf, "%d", &ipforwarding);
117
118
0
  fclose(fp);
119
120
0
  if (ret != 1)
121
0
    return 0;
122
123
0
  return ipforwarding;
124
0
}
125
126
int ipforward_ipv6_on(void)
127
0
{
128
0
  FILE *fp;
129
130
0
  frr_with_privs(&zserv_privs) {
131
132
0
    fp = fopen(proc_ipv6_forwarding, "w");
133
134
0
    if (fp == NULL) {
135
0
      return -1;
136
0
    }
137
138
0
    fprintf(fp, "1\n");
139
140
0
    fclose(fp);
141
142
0
  }
143
144
0
  return ipforward_ipv6();
145
0
}
146
147
148
int ipforward_ipv6_off(void)
149
0
{
150
0
  FILE *fp;
151
152
0
  frr_with_privs(&zserv_privs) {
153
154
0
    fp = fopen(proc_ipv6_forwarding, "w");
155
156
0
    if (fp == NULL) {
157
0
      return -1;
158
0
    }
159
160
0
    fprintf(fp, "0\n");
161
162
0
    fclose(fp);
163
164
0
  }
165
166
0
  return ipforward_ipv6();
167
0
}
168
169
#endif /* GNU_LINUX */