]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_ioctl_solaris.c
isisd: implement the 'lsp-too-large' notification
[mirror_frr.git] / zebra / if_ioctl_solaris.c
CommitLineData
8842468c 1/*
2 * Interface looking up by ioctl () on Solaris.
3 * Copyright (C) 1997, 98 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 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
8842468c 20 */
21
22#include <zebra.h>
23
ddfeb486
DL
24#ifdef SUNOS_5
25
8842468c 26#include "if.h"
27#include "sockunion.h"
28#include "prefix.h"
29#include "ioctl.h"
30#include "connected.h"
31#include "memory.h"
4a1ab8e4 32#include "zebra_memory.h"
8842468c 33#include "log.h"
48a46fa0 34#include "privs.h"
8f7d9fc0 35#include "vrf.h"
82283592 36#include "vty.h"
174482ef 37#include "lib_errors.h"
8842468c 38
39#include "zebra/interface.h"
8d610213 40#include "zebra/ioctl_solaris.h"
8f7d9fc0 41#include "zebra/rib.h"
ae9eebca 42#include "zebra/rt.h"
364fed6b 43#include "zebra/zebra_errors.h"
8842468c 44
d62a17ae 45static int if_get_addr(struct interface *, struct sockaddr *, const char *);
46static void interface_info_ioctl(struct interface *);
48a46fa0 47extern struct zebra_privs_t zserv_privs;
8842468c 48
d62a17ae 49static int interface_list_ioctl(int af)
8842468c 50{
d62a17ae 51 int ret;
52 int sock;
8842468c 53#define IFNUM_BASE 32
d62a17ae 54 struct lifnum lifn;
55 int ifnum;
56 struct lifreq *lifreq;
57 struct lifconf lifconf;
58 struct interface *ifp;
59 int n;
d62a17ae 60 size_t needed, lastneeded = 0;
61 char *buf = NULL;
62
01b9e3fd 63 frr_elevate_privs(&zserv_privs) {
01b9e3fd 64 sock = socket(af, SOCK_DGRAM, 0);
6bb30c2c 65 }
d62a17ae 66
6bb30c2c 67 if (sock < 0) {
450971aa 68 flog_err_sys(EC_LIB_SOCKET, "Can't make %s socket stream: %s",
9df414fe
QY
69 (af == AF_INET ? "AF_INET" : "AF_INET6"),
70 safe_strerror(errno));
6bb30c2c
DL
71 return -1;
72 }
d62a17ae 73
6bb30c2c
DL
74calculate_lifc_len:
75 frr_elevate_privs(&zserv_privs) {
01b9e3fd 76 lifn.lifn_family = af;
6bb30c2c
DL
77 lifn.lifn_flags = LIFC_NOXMIT;
78 /* we want NOXMIT interfaces too */
01b9e3fd 79 ret = ioctl(sock, SIOCGLIFNUM, &lifn);
01b9e3fd 80 }
d62a17ae 81
82 if (ret < 0) {
450971aa 83 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe 84 "interface_list_ioctl: SIOCGLIFNUM failed %s",
e991eff5 85 safe_strerror(errno));
d62a17ae 86 close(sock);
87 return -1;
88 }
89 ifnum = lifn.lifn_count;
90
91 /*
92 * When calculating the buffer size needed, add a small number
93 * of interfaces to those we counted. We do this to capture
94 * the interface status of potential interfaces which may have
95 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
96 */
97 needed = (ifnum + 4) * sizeof(struct lifreq);
98 if (needed > lastneeded || needed < lastneeded / 2) {
99 if (buf != NULL)
100 XFREE(MTYPE_TMP, buf);
0ce1ca80 101 buf = XMALLOC(MTYPE_TMP, needed);
d62a17ae 102 }
103 lastneeded = needed;
104
105 lifconf.lifc_family = af;
106 lifconf.lifc_flags = LIFC_NOXMIT;
107 lifconf.lifc_len = needed;
108 lifconf.lifc_buf = buf;
109
6bb30c2c
DL
110 frr_elevate_privs(&zserv_privs) {
111 ret = ioctl(sock, SIOCGLIFCONF, &lifconf);
112 }
d62a17ae 113
114 if (ret < 0) {
115 if (errno == EINVAL)
6bb30c2c 116 goto calculate_lifc_len;
d62a17ae 117
450971aa 118 flog_err_sys(EC_LIB_SYSTEM_CALL, "SIOCGLIFCONF: %s",
9df414fe 119 safe_strerror(errno));
d62a17ae 120 goto end;
121 }
122
d62a17ae 123 /* Allocate interface. */
124 lifreq = lifconf.lifc_req;
125
126 for (n = 0; n < lifconf.lifc_len; n += sizeof(struct lifreq)) {
127 /* we treat Solaris logical interfaces as addresses, because
128 * that is
129 * how PF_ROUTE on Solaris treats them. Hence we can not
130 * directly use
131 * the lifreq_name to get the ifp. We need to normalise the
132 * name
133 * before attempting get.
134 *
135 * Solaris logical interface names are in the form of:
136 * <interface name>:<logical interface id>
137 */
138 unsigned int normallen = 0;
139 uint64_t lifflags;
140
141 /* We should exclude ~IFF_UP interfaces, as we'll find out about
142 * them
143 * coming up later through RTM_NEWADDR message on the route
144 * socket.
145 */
146 if (if_get_flags_direct(lifreq->lifr_name, &lifflags,
147 lifreq->lifr_addr.ss_family)
148 || !CHECK_FLAG(lifflags, IFF_UP)) {
149 lifreq++;
150 continue;
151 }
152
153 /* Find the normalised name */
154 while ((normallen < sizeof(lifreq->lifr_name))
155 && (*(lifreq->lifr_name + normallen) != '\0')
156 && (*(lifreq->lifr_name + normallen) != ':'))
157 normallen++;
158
8f90d89b 159 ifp = if_get_by_name(lifreq->lifr_name, VRF_DEFAULT);
d62a17ae 160
161 if (lifreq->lifr_addr.ss_family == AF_INET)
162 ifp->flags |= IFF_IPV4;
163
164 if (lifreq->lifr_addr.ss_family == AF_INET6) {
165 ifp->flags |= IFF_IPV6;
166 }
167
168 if_add_update(ifp);
169
170 interface_info_ioctl(ifp);
171
172 /* If a logical interface pass the full name so it can be
173 * as a label on the address
174 */
175 if (*(lifreq->lifr_name + normallen) != '\0')
176 if_get_addr(ifp, (struct sockaddr *)&lifreq->lifr_addr,
177 lifreq->lifr_name);
178 else
179 if_get_addr(ifp, (struct sockaddr *)&lifreq->lifr_addr,
180 NULL);
181
182 /* Poke the interface flags. Lets IFF_UP mangling kick in */
183 if_flags_update(ifp, ifp->flags);
184
185 lifreq++;
186 }
8842468c 187
188end:
d62a17ae 189 close(sock);
190 XFREE(MTYPE_TMP, lifconf.lifc_buf);
191 return ret;
8842468c 192}
193
194/* Get interface's index by ioctl. */
d62a17ae 195static int if_get_index(struct interface *ifp)
8842468c 196{
d62a17ae 197 int ret;
198 struct lifreq lifreq;
8842468c 199
d62a17ae 200 lifreq_set_name(&lifreq, ifp->name);
8842468c 201
d62a17ae 202 if (ifp->flags & IFF_IPV4)
203 ret = AF_IOCTL(AF_INET, SIOCGLIFINDEX, (caddr_t)&lifreq);
204 else if (ifp->flags & IFF_IPV6)
205 ret = AF_IOCTL(AF_INET6, SIOCGLIFINDEX, (caddr_t)&lifreq);
206 else
207 ret = -1;
8842468c 208
d62a17ae 209 if (ret < 0) {
450971aa 210 flog_err_sys(EC_LIB_SYSTEM_CALL, "SIOCGLIFINDEX(%s) failed",
9df414fe 211 ifp->name);
d62a17ae 212 return ret;
213 }
8842468c 214
d62a17ae 215/* OK we got interface index. */
8842468c 216#ifdef ifr_ifindex
ff880b78 217 if_set_index(ifp, lifreq.lifr_ifindex);
8842468c 218#else
ff880b78 219 if_set_index(ifp, lifreq.lifr_index);
8842468c 220#endif
d62a17ae 221 return ifp->ifindex;
8842468c 222}
223
224
225/* Interface address lookup by ioctl. This function only looks up
226 IPv4 address. */
d62a17ae 227#define ADDRLEN(sa) \
228 (((sa)->sa_family == AF_INET ? sizeof(struct sockaddr_in) \
229 : sizeof(struct sockaddr_in6)))
8842468c 230
231#define SIN(s) ((struct sockaddr_in *)(s))
232#define SIN6(s) ((struct sockaddr_in6 *)(s))
233
0752ef0b 234/* Retrieve address information for the given ifp */
d62a17ae 235static int if_get_addr(struct interface *ifp, struct sockaddr *addr,
236 const char *label)
8842468c 237{
d62a17ae 238 int ret;
239 struct lifreq lifreq;
240 struct sockaddr_storage mask, dest;
241 char *dest_pnt = NULL;
d7c0a89a 242 uint8_t prefixlen = 0;
d62a17ae 243 afi_t af;
244 int flags = 0;
245
246 /* Interface's name and address family.
247 * We need to use the logical interface name / label, if we've been
248 * given one, in order to get the right address
249 */
250 strncpy(lifreq.lifr_name, (label ? label : ifp->name), IFNAMSIZ);
251
252 /* Interface's address. */
253 memcpy(&lifreq.lifr_addr, addr, ADDRLEN(addr));
254 af = addr->sa_family;
255
256 /* Point to point or broad cast address pointer init. */
257 dest_pnt = NULL;
258
259 if (AF_IOCTL(af, SIOCGLIFDSTADDR, (caddr_t)&lifreq) >= 0) {
260 memcpy(&dest, &lifreq.lifr_dstaddr, ADDRLEN(addr));
261 if (af == AF_INET)
262 dest_pnt = (char *)&(SIN(&dest)->sin_addr);
263 else
264 dest_pnt = (char *)&(SIN6(&dest)->sin6_addr);
265 flags = ZEBRA_IFA_PEER;
fb6724a6 266 }
d62a17ae 267
268 if (af == AF_INET) {
269 ret = if_ioctl(SIOCGLIFNETMASK, (caddr_t)&lifreq);
270
271 if (ret < 0) {
272 if (errno != EADDRNOTAVAIL) {
450971aa 273 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe
QY
274 "SIOCGLIFNETMASK (%s) fail: %s",
275 ifp->name, safe_strerror(errno));
d62a17ae 276 return ret;
277 }
278 return 0;
279 }
280 memcpy(&mask, &lifreq.lifr_addr, ADDRLEN(addr));
281
282 prefixlen = ip_masklen(SIN(&mask)->sin_addr);
283 if (!dest_pnt
284 && (if_ioctl(SIOCGLIFBRDADDR, (caddr_t)&lifreq) >= 0)) {
285 memcpy(&dest, &lifreq.lifr_broadaddr,
286 sizeof(struct sockaddr_in));
287 dest_pnt = (char *)&SIN(&dest)->sin_addr;
288 }
289 } else if (af == AF_INET6) {
290 if (if_ioctl_ipv6(SIOCGLIFSUBNET, (caddr_t)&lifreq) < 0) {
291 if (ifp->flags & IFF_POINTOPOINT)
292 prefixlen = IPV6_MAX_BITLEN;
293 else
450971aa 294 flog_err_sys(EC_LIB_SYSTEM_CALL,
9df414fe
QY
295 "SIOCGLIFSUBNET (%s) fail: %s",
296 ifp->name, safe_strerror(errno));
d62a17ae 297 } else {
298 prefixlen = lifreq.lifr_addrlen;
299 }
fb6724a6 300 }
8842468c 301
d62a17ae 302 /* Set address to the interface. */
303 if (af == AF_INET)
304 connected_add_ipv4(ifp, flags, &SIN(addr)->sin_addr, prefixlen,
305 (struct in_addr *)dest_pnt, label);
306 else if (af == AF_INET6)
60c0687a 307 connected_add_ipv6(ifp, flags, &SIN6(addr)->sin6_addr, NULL,
608105a7 308 prefixlen, label);
8842468c 309
d62a17ae 310 return 0;
8842468c 311}
312
313/* Fetch interface information via ioctl(). */
d62a17ae 314static void interface_info_ioctl(struct interface *ifp)
8842468c 315{
d62a17ae 316 if_get_index(ifp);
317 if_get_flags(ifp);
318 if_get_mtu(ifp);
319 if_get_metric(ifp);
8842468c 320}
321
322/* Lookup all interface information. */
d62a17ae 323void interface_list(struct zebra_ns *zns)
8842468c 324{
d62a17ae 325 if (zns->ns_id != NS_DEFAULT) {
9df414fe 326 zlog_debug("interface_list: ignore NS %u", zns->ns_id);
d62a17ae 327 return;
328 }
329 interface_list_ioctl(AF_INET);
330 interface_list_ioctl(AF_INET6);
331 interface_list_ioctl(AF_UNSPEC);
8842468c 332}
333
d62a17ae 334struct connected *if_lookup_linklocal(struct interface *ifp)
8842468c 335{
d62a17ae 336 struct listnode *node;
337 struct connected *ifc;
8842468c 338
d62a17ae 339 if (ifp == NULL)
340 return NULL;
8842468c 341
d62a17ae 342 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
343 if ((ifc->address->family == AF_INET6)
344 && (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)))
345 return ifc;
346 }
5b73a671 347
d62a17ae 348 return NULL;
8842468c 349}
ddfeb486
DL
350
351#endif /* SUNOS_5 */