1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
* Common ioctl functions for Solaris.
* Copyright (C) 1997, 98 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "linklist.h"
#include "if.h"
#include "prefix.h"
#include "ioctl.h"
#include "log.h"
#include "privs.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
extern struct zebra_privs_t zserv_privs;
/* clear and set interface name string */
void
lifreq_set_name (struct lifreq *lifreq, struct interface *ifp)
{
strncpy (lifreq->lifr_name, ifp->name, IFNAMSIZ);
}
/* call ioctl system call */
int
if_ioctl (u_long request, caddr_t buffer)
{
int sock;
int ret;
int err;
if (zserv_privs.change(ZPRIVS_RAISE))
zlog (NULL, LOG_ERR, "Can't raise privileges");
sock = socket (AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
if (zserv_privs.change(ZPRIVS_LOWER))
zlog (NULL, LOG_ERR, "Can't lower privileges");
perror ("socket");
exit (1);
}
if ((ret = ioctl (sock, request, buffer)) < 0)
err = errno;
if (zserv_privs.change(ZPRIVS_LOWER))
zlog (NULL, LOG_ERR, "Can't lower privileges");
close (sock);
if (ret < 0)
{
errno = err;
return ret;
}
return 0;
}
int
if_ioctl_ipv6 (u_long request, caddr_t buffer)
{
#ifdef HAVE_IPV6
int sock;
int ret;
int err;
if (zserv_privs.change(ZPRIVS_RAISE))
zlog (NULL, LOG_ERR, "Can't raise privileges");
sock = socket (AF_INET6, SOCK_DGRAM, 0);
if (sock < 0)
{
if (zserv_privs.change(ZPRIVS_LOWER))
zlog (NULL, LOG_ERR, "Can't lower privileges");
perror ("socket");
exit (1);
}
if ((ret = ioctl (sock, request, buffer)) < 0)
err = errno;
if (zserv_privs.change(ZPRIVS_LOWER))
zlog (NULL, LOG_ERR, "Can't lower privileges");
close (sock);
if (ret < 0)
{
errno = err;
return ret;
}
#endif /* HAVE_IPV6 */
return 0;
}
/*
* get interface metric
* -- if value is not avaliable set -1
*/
void
if_get_metric (struct interface *ifp)
{
struct lifreq lifreq;
int ret;
lifreq_set_name (&lifreq, ifp);
if (ifp->flags & IFF_IPV4)
ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
#ifdef SOLARIS_IPV6
else if (ifp->flags & IFF_IPV6)
ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
#endif /* SOLARIS_IPV6 */
else
ret = -1;
if (ret < 0)
return;
ifp->metric = lifreq.lifr_metric;
if (ifp->metric == 0)
ifp->metric = 1;
}
/* get interface MTU */
void
if_get_mtu (struct interface *ifp)
{
struct lifreq lifreq;
int ret;
if (ifp->flags & IFF_IPV4)
{
lifreq_set_name (&lifreq, ifp);
ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
if (ret < 0)
{
zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
ifp->name);
ifp->mtu = -1;
}
else
{
ifp->mtu = lifreq.lifr_metric;
}
}
#ifdef HAVE_IPV6
if ((ifp->flags & IFF_IPV6) == 0)
return;
memset(&lifreq, 0, sizeof(lifreq));
lifreq_set_name (&lifreq, ifp);
ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
if (ret < 0)
{
zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
ifp->mtu6 = -1;
}
else
{
ifp->mtu6 = lifreq.lifr_metric;
}
#endif /* HAVE_IPV6 */
}
/* Set up interface's address, netmask (and broadcast? ).
Solaris uses ifname:number semantics to set IP address aliases. */
int
if_set_prefix (struct interface *ifp, struct connected *ifc)
{
int ret;
struct ifreq ifreq;
struct sockaddr_in addr;
struct sockaddr_in broad;
struct sockaddr_in mask;
struct prefix_ipv4 ifaddr;
struct prefix_ipv4 *p;
p = (struct prefix_ipv4 *) ifc->address;
ifaddr = *p;
strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
addr.sin_addr = p->prefix;
addr.sin_family = p->family;
memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
if (ret < 0)
return ret;
/* We need mask for make broadcast addr. */
masklen2ip (p->prefixlen, &mask.sin_addr);
if (if_is_broadcast (ifp))
{
apply_mask_ipv4 (&ifaddr);
addr.sin_addr = ifaddr.prefix;
broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
broad.sin_family = p->family;
memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
if (ret < 0)
return ret;
}
mask.sin_family = p->family;
#ifdef SUNOS_5
memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
#else
memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
#endif /* SUNOS_5 */
ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
return ((ret < 0) ? ret : 0);
}
/* Set up interface's address, netmask (and broadcast).
Solaris uses ifname:number semantics to set IP address aliases. */
int
if_unset_prefix (struct interface *ifp, struct connected *ifc)
{
int ret;
struct ifreq ifreq;
struct sockaddr_in addr;
struct prefix_ipv4 *p;
p = (struct prefix_ipv4 *) ifc->address;
strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
memset (&addr, 0, sizeof (struct sockaddr_in));
addr.sin_family = p->family;
memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
if (ret < 0)
return ret;
return 0;
}
/* get interface flags */
void
if_get_flags (struct interface *ifp)
{
int ret;
struct lifreq lifreq;
unsigned long flags4 = 0, flags6 = 0;
if (ifp->flags & IFF_IPV4)
{
lifreq_set_name (&lifreq, ifp);
ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
flags4 = (lifreq.lifr_flags & 0xffffffff);
if (!(flags4 & IFF_UP))
flags4 &= ~IFF_IPV4;
}
if (ifp->flags & IFF_IPV6)
{
lifreq_set_name (&lifreq, ifp);
ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
flags6 = (lifreq.lifr_flags & 0xffffffff);
if (!(flags6 & IFF_UP))
flags6 &= ~IFF_IPV6;
}
ifp->flags = (flags4 | flags6);
}
/* Set interface flags */
int
if_set_flags (struct interface *ifp, unsigned long flags)
{
int ret;
struct lifreq lifreq;
lifreq_set_name (&lifreq, ifp);
lifreq.lifr_flags = ifp->flags;
lifreq.lifr_flags |= flags;
if (ifp->flags & IFF_IPV4)
ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
else if (ifp->flags & IFF_IPV6)
ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
else
ret = -1;
if (ret < 0)
zlog_info ("can't set interface flags on %s: %s", ifp->name,
safe_strerror (errno));
else
ret = 0;
return ret;
}
/* Unset interface's flag. */
int
if_unset_flags (struct interface *ifp, unsigned long flags)
{
int ret;
struct lifreq lifreq;
lifreq_set_name (&lifreq, ifp);
lifreq.lifr_flags = ifp->flags;
lifreq.lifr_flags &= ~flags;
if (ifp->flags & IFF_IPV4)
ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
else if (ifp->flags & IFF_IPV6)
ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
else
ret = -1;
if (ret < 0)
zlog_info ("can't unset interface flags");
else
ret = 0;
return ret;
}
#ifdef HAVE_IPV6
/* Interface's address add/delete functions. */
int
if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
{
char addrbuf[INET_ADDRSTRLEN];
inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
addrbuf, sizeof (addrbuf));
zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
return 0;
}
int
if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
{
char addrbuf[INET_ADDRSTRLEN];
inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
addrbuf, sizeof (addrbuf));
zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
return 0;
}
#endif /* HAVE_IPV6 */
|