blob: 43f687d428460690e339b601b816753c4ca78571 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24/* Hack for GNU libc version 2. */
25#ifndef MSG_TRUNC
26#define MSG_TRUNC 0x20
27#endif /* MSG_TRUNC */
28
29#include "linklist.h"
30#include "if.h"
31#include "log.h"
32#include "prefix.h"
33#include "connected.h"
34#include "table.h"
35#include "rib.h"
paule04ab742003-01-17 23:47:00 +000036#include "thread.h"
pauledd7c242003-06-04 13:59:38 +000037#include "privs.h"
paul718e3742002-12-13 20:15:29 +000038
39#include "zebra/zserv.h"
40#include "zebra/redistribute.h"
41#include "zebra/interface.h"
42#include "zebra/debug.h"
43
44/* Socket interface to kernel */
45struct nlsock
46{
47 int sock;
48 int seq;
49 struct sockaddr_nl snl;
hassofce954f2004-10-07 20:29:24 +000050 const char *name;
paul7021c422003-07-15 12:52:22 +000051} netlink = { -1, 0, {0}, "netlink-listen"}, /* kernel messages */
52 netlink_cmd = { -1, 0, {0}, "netlink-cmd"}, /* command channel */
53 netlink_addr = { -1, 0, {0}, "netlink-addr"}; /* address channel */
paul718e3742002-12-13 20:15:29 +000054
paul7021c422003-07-15 12:52:22 +000055struct message nlmsg_str[] = {
paul718e3742002-12-13 20:15:29 +000056 {RTM_NEWROUTE, "RTM_NEWROUTE"},
57 {RTM_DELROUTE, "RTM_DELROUTE"},
58 {RTM_GETROUTE, "RTM_GETROUTE"},
59 {RTM_NEWLINK, "RTM_NEWLINK"},
60 {RTM_DELLINK, "RTM_DELLINK"},
61 {RTM_GETLINK, "RTM_GETLINK"},
62 {RTM_NEWADDR, "RTM_NEWADDR"},
63 {RTM_DELADDR, "RTM_DELADDR"},
64 {RTM_GETADDR, "RTM_GETADDR"},
paul7021c422003-07-15 12:52:22 +000065 {0, NULL}
paul718e3742002-12-13 20:15:29 +000066};
67
hassofce954f2004-10-07 20:29:24 +000068const char *nexthop_types_desc[] =
paul7021c422003-07-15 12:52:22 +000069{
70 "none",
71 "Directly connected",
72 "Interface route",
73 "IPv4 nexthop",
74 "IPv4 nexthop with ifindex",
75 "IPv4 nexthop with ifname",
76 "IPv6 nexthop"
77 "IPv6 nexthop with ifindex",
78 "IPv6 nexthop with ifname",
79 "Null0 nexthop",
80};
81
82
paulb21b19c2003-06-15 01:28:29 +000083extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000084
pauledd7c242003-06-04 13:59:38 +000085extern struct zebra_privs_t zserv_privs;
86
hassoc34b6b52004-08-31 13:41:49 +000087extern u_int32_t nl_rcvbufsize;
88
paul718e3742002-12-13 20:15:29 +000089/* Make socket for Linux netlink interface. */
90static int
91netlink_socket (struct nlsock *nl, unsigned long groups)
92{
93 int ret;
94 struct sockaddr_nl snl;
95 int sock;
96 int namelen;
ajs4be019d2005-01-29 16:12:41 +000097 int save_errno;
paul718e3742002-12-13 20:15:29 +000098
99 sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
100 if (sock < 0)
101 {
102 zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000103 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000104 return -1;
105 }
106
107 ret = fcntl (sock, F_SETFL, O_NONBLOCK);
108 if (ret < 0)
109 {
110 zlog (NULL, LOG_ERR, "Can't set %s socket flags: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000111 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000112 close (sock);
113 return -1;
114 }
paul7021c422003-07-15 12:52:22 +0000115
hassoc34b6b52004-08-31 13:41:49 +0000116 /* Set receive buffer size if it's set from command line */
117 if (nl_rcvbufsize)
118 {
119 u_int32_t oldsize, oldlen;
120 u_int32_t newsize, newlen;
121
122 oldlen = sizeof(oldsize);
123 newlen = sizeof(newsize);
124
125 ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
126 if (ret < 0)
127 {
128 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000129 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000130 close (sock);
131 return -1;
132 }
133
134 ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize,
135 sizeof(nl_rcvbufsize));
136 if (ret < 0)
137 {
138 zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000139 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000140 close (sock);
141 return -1;
142 }
143
144 ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
145 if (ret < 0)
146 {
147 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000148 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000149 close (sock);
150 return -1;
151 }
152
153 zlog (NULL, LOG_INFO,
154 "Setting netlink socket receive buffer size: %u -> %u",
155 oldsize, newsize);
156 }
157
paul718e3742002-12-13 20:15:29 +0000158 memset (&snl, 0, sizeof snl);
159 snl.nl_family = AF_NETLINK;
160 snl.nl_groups = groups;
161
162 /* Bind the socket to the netlink structure for anything. */
paul7021c422003-07-15 12:52:22 +0000163 if (zserv_privs.change (ZPRIVS_RAISE))
164 {
165 zlog (NULL, LOG_ERR, "Can't raise privileges");
166 return -1;
167 }
pauledd7c242003-06-04 13:59:38 +0000168
paul718e3742002-12-13 20:15:29 +0000169 ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000170 save_errno = errno;
hasso55e7ecd2004-08-06 08:41:56 +0000171 if (zserv_privs.change (ZPRIVS_LOWER))
172 zlog (NULL, LOG_ERR, "Can't lower privileges");
173
paul718e3742002-12-13 20:15:29 +0000174 if (ret < 0)
175 {
paul7021c422003-07-15 12:52:22 +0000176 zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
ajs4be019d2005-01-29 16:12:41 +0000177 nl->name, snl.nl_groups, safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000178 close (sock);
179 return -1;
180 }
paul7021c422003-07-15 12:52:22 +0000181
paul718e3742002-12-13 20:15:29 +0000182 /* multiple netlink sockets will have different nl_pid */
183 namelen = sizeof snl;
hassoc9e52be2004-09-26 16:09:34 +0000184 ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);
paul718e3742002-12-13 20:15:29 +0000185 if (ret < 0 || namelen != sizeof snl)
186 {
187 zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000188 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000189 close (sock);
190 return -1;
191 }
192
193 nl->snl = snl;
194 nl->sock = sock;
195 return ret;
196}
197
paul7021c422003-07-15 12:52:22 +0000198int
199set_netlink_blocking (struct nlsock *nl, int *flags)
paul5f37d862003-04-19 00:11:28 +0000200{
201
202 /* Change socket flags for blocking I/O. */
paul7021c422003-07-15 12:52:22 +0000203 if ((*flags = fcntl (nl->sock, F_GETFL, 0)) < 0)
paul5f37d862003-04-19 00:11:28 +0000204 {
paul7021c422003-07-15 12:52:22 +0000205 zlog (NULL, LOG_ERR, "%s:%i F_GETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000206 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000207 return -1;
208 }
209 *flags &= ~O_NONBLOCK;
paul7021c422003-07-15 12:52:22 +0000210 if (fcntl (nl->sock, F_SETFL, *flags) < 0)
paul5f37d862003-04-19 00:11:28 +0000211 {
paul7021c422003-07-15 12:52:22 +0000212 zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000213 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000214 return -1;
215 }
216 return 0;
217}
218
paul7021c422003-07-15 12:52:22 +0000219int
220set_netlink_nonblocking (struct nlsock *nl, int *flags)
221{
paul5f37d862003-04-19 00:11:28 +0000222 /* Restore socket flags for nonblocking I/O */
223 *flags |= O_NONBLOCK;
paul7021c422003-07-15 12:52:22 +0000224 if (fcntl (nl->sock, F_SETFL, *flags) < 0)
paul5f37d862003-04-19 00:11:28 +0000225 {
paul7021c422003-07-15 12:52:22 +0000226 zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000227 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000228 return -1;
229 }
230 return 0;
231}
232
paul718e3742002-12-13 20:15:29 +0000233/* Get type specified information from netlink. */
234static int
235netlink_request (int family, int type, struct nlsock *nl)
236{
237 int ret;
238 struct sockaddr_nl snl;
ajs4be019d2005-01-29 16:12:41 +0000239 int save_errno;
paul718e3742002-12-13 20:15:29 +0000240
241 struct
242 {
243 struct nlmsghdr nlh;
244 struct rtgenmsg g;
245 } req;
246
247
248 /* Check netlink socket. */
249 if (nl->sock < 0)
250 {
251 zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
252 return -1;
253 }
254
255 memset (&snl, 0, sizeof snl);
256 snl.nl_family = AF_NETLINK;
257
258 req.nlh.nlmsg_len = sizeof req;
259 req.nlh.nlmsg_type = type;
260 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
261 req.nlh.nlmsg_pid = 0;
262 req.nlh.nlmsg_seq = ++nl->seq;
263 req.g.rtgen_family = family;
pauledd7c242003-06-04 13:59:38 +0000264
265 /* linux appears to check capabilities on every message
266 * have to raise caps for every message sent
267 */
paul7021c422003-07-15 12:52:22 +0000268 if (zserv_privs.change (ZPRIVS_RAISE))
pauledd7c242003-06-04 13:59:38 +0000269 {
270 zlog (NULL, LOG_ERR, "Can't raise privileges");
271 return -1;
272 }
paul7021c422003-07-15 12:52:22 +0000273
274 ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
275 (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000276 save_errno = errno;
paul7021c422003-07-15 12:52:22 +0000277
278 if (zserv_privs.change (ZPRIVS_LOWER))
279 zlog (NULL, LOG_ERR, "Can't lower privileges");
280
paul718e3742002-12-13 20:15:29 +0000281 if (ret < 0)
paul7021c422003-07-15 12:52:22 +0000282 {
283 zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
ajs4be019d2005-01-29 16:12:41 +0000284 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000285 return -1;
286 }
pauledd7c242003-06-04 13:59:38 +0000287
paul718e3742002-12-13 20:15:29 +0000288 return 0;
289}
290
291/* Receive message from netlink interface and pass those information
292 to the given function. */
293static int
294netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *),
paul7021c422003-07-15 12:52:22 +0000295 struct nlsock *nl)
paul718e3742002-12-13 20:15:29 +0000296{
297 int status;
298 int ret = 0;
299 int error;
300
301 while (1)
302 {
303 char buf[4096];
304 struct iovec iov = { buf, sizeof buf };
305 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +0000306 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +0000307 struct nlmsghdr *h;
ajs4be019d2005-01-29 16:12:41 +0000308 int save_errno;
paul718e3742002-12-13 20:15:29 +0000309
paul7021c422003-07-15 12:52:22 +0000310 if (zserv_privs.change (ZPRIVS_RAISE))
pauledd7c242003-06-04 13:59:38 +0000311 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul7021c422003-07-15 12:52:22 +0000312
paul718e3742002-12-13 20:15:29 +0000313 status = recvmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +0000314 save_errno = errno;
paul7021c422003-07-15 12:52:22 +0000315
316 if (zserv_privs.change (ZPRIVS_LOWER))
pauledd7c242003-06-04 13:59:38 +0000317 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000318
319 if (status < 0)
paul7021c422003-07-15 12:52:22 +0000320 {
ajs4be019d2005-01-29 16:12:41 +0000321 if (save_errno == EINTR)
paul7021c422003-07-15 12:52:22 +0000322 continue;
ajs4be019d2005-01-29 16:12:41 +0000323 if (save_errno == EWOULDBLOCK || save_errno == EAGAIN)
paul7021c422003-07-15 12:52:22 +0000324 break;
ajs4be019d2005-01-29 16:12:41 +0000325 zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s",
326 nl->name, safe_strerror(save_errno));
paul7021c422003-07-15 12:52:22 +0000327 continue;
328 }
paul718e3742002-12-13 20:15:29 +0000329
330 if (status == 0)
paul7021c422003-07-15 12:52:22 +0000331 {
332 zlog (NULL, LOG_ERR, "%s EOF", nl->name);
333 return -1;
334 }
paul718e3742002-12-13 20:15:29 +0000335
336 if (msg.msg_namelen != sizeof snl)
paul7021c422003-07-15 12:52:22 +0000337 {
338 zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
339 nl->name, msg.msg_namelen);
340 return -1;
341 }
paulb84d3a12003-11-17 10:31:01 +0000342
343 /* JF: Ignore messages that aren't from the kernel */
344 if ( snl.nl_pid != 0 )
345 {
346 zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl.nl_pid );
347 continue;
348 }
paul718e3742002-12-13 20:15:29 +0000349
paul7021c422003-07-15 12:52:22 +0000350 for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, status);
351 h = NLMSG_NEXT (h, status))
352 {
353 /* Finish of reading. */
354 if (h->nlmsg_type == NLMSG_DONE)
355 return ret;
paul718e3742002-12-13 20:15:29 +0000356
paul7021c422003-07-15 12:52:22 +0000357 /* Error handling. */
358 if (h->nlmsg_type == NLMSG_ERROR)
359 {
360 struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
361
paul718e3742002-12-13 20:15:29 +0000362 /* If the error field is zero, then this is an ACK */
paul7021c422003-07-15 12:52:22 +0000363 if (err->error == 0)
paul718e3742002-12-13 20:15:29 +0000364 {
paul7021c422003-07-15 12:52:22 +0000365 if (IS_ZEBRA_DEBUG_KERNEL)
366 {
ajsb6178002004-12-07 21:12:56 +0000367 zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%d",
paul7021c422003-07-15 12:52:22 +0000368 __FUNCTION__, nl->name,
369 lookup (nlmsg_str, err->msg.nlmsg_type),
370 err->msg.nlmsg_type, err->msg.nlmsg_seq,
371 err->msg.nlmsg_pid);
paul718e3742002-12-13 20:15:29 +0000372 }
paul7021c422003-07-15 12:52:22 +0000373
374 /* return if not a multipart message, otherwise continue */
375 if (!(h->nlmsg_flags & NLM_F_MULTI))
376 {
377 return 0;
paul718e3742002-12-13 20:15:29 +0000378 }
paul7021c422003-07-15 12:52:22 +0000379 continue;
paul718e3742002-12-13 20:15:29 +0000380 }
paul7021c422003-07-15 12:52:22 +0000381
paul718e3742002-12-13 20:15:29 +0000382 if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
paul7021c422003-07-15 12:52:22 +0000383 {
384 zlog (NULL, LOG_ERR, "%s error: message truncated",
385 nl->name);
386 return -1;
387 }
pauld753e9e2003-01-22 19:45:50 +0000388
paul7021c422003-07-15 12:52:22 +0000389 /* Deal with Error Noise - MAG */
390 {
391 int loglvl = LOG_ERR;
392 int errnum = err->error;
393 int msg_type = err->msg.nlmsg_type;
paul718e3742002-12-13 20:15:29 +0000394
paul7021c422003-07-15 12:52:22 +0000395 if (nl == &netlink_cmd
396 && (-errnum == ENODEV || -errnum == ESRCH)
397 && (msg_type == RTM_NEWROUTE || msg_type == RTM_DELROUTE))
398 loglvl = LOG_DEBUG;
paul718e3742002-12-13 20:15:29 +0000399
paul7021c422003-07-15 12:52:22 +0000400 zlog (NULL, loglvl, "%s error: %s, type=%s(%u), "
401 "seq=%u, pid=%d",
ajs6099b3b2004-11-20 02:06:59 +0000402 nl->name, safe_strerror (-errnum),
paul7021c422003-07-15 12:52:22 +0000403 lookup (nlmsg_str, msg_type),
404 msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
405 }
406 /*
407 ret = -1;
408 continue;
409 */
410 return -1;
411 }
paul718e3742002-12-13 20:15:29 +0000412
paul7021c422003-07-15 12:52:22 +0000413 /* OK we got netlink message. */
414 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000415 zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%d",
paul7021c422003-07-15 12:52:22 +0000416 nl->name,
417 lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
418 h->nlmsg_seq, h->nlmsg_pid);
419
420 /* skip unsolicited messages originating from command socket */
421 if (nl != &netlink_cmd && h->nlmsg_pid == netlink_cmd.snl.nl_pid)
422 {
423 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000424 zlog_debug ("netlink_parse_info: %s packet comes from %s",
paul7021c422003-07-15 12:52:22 +0000425 nl->name, netlink_cmd.name);
426 continue;
427 }
428
429 error = (*filter) (&snl, h);
430 if (error < 0)
431 {
432 zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
433 ret = error;
434 }
435 }
paul718e3742002-12-13 20:15:29 +0000436
437 /* After error care. */
438 if (msg.msg_flags & MSG_TRUNC)
paul7021c422003-07-15 12:52:22 +0000439 {
440 zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name);
441 continue;
442 }
paul718e3742002-12-13 20:15:29 +0000443 if (status)
paul7021c422003-07-15 12:52:22 +0000444 {
445 zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
446 status);
447 return -1;
448 }
paul718e3742002-12-13 20:15:29 +0000449 }
450 return ret;
451}
452
453/* Utility function for parse rtattr. */
454static void
paul7021c422003-07-15 12:52:22 +0000455netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
456 int len)
paul718e3742002-12-13 20:15:29 +0000457{
paul7021c422003-07-15 12:52:22 +0000458 while (RTA_OK (rta, len))
paul718e3742002-12-13 20:15:29 +0000459 {
460 if (rta->rta_type <= max)
paul7021c422003-07-15 12:52:22 +0000461 tb[rta->rta_type] = rta;
462 rta = RTA_NEXT (rta, len);
paul718e3742002-12-13 20:15:29 +0000463 }
464}
465
466/* Called from interface_lookup_netlink(). This function is only used
467 during bootstrap. */
468int
469netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h)
470{
471 int len;
472 struct ifinfomsg *ifi;
473 struct rtattr *tb[IFLA_MAX + 1];
474 struct interface *ifp;
475 char *name;
476 int i;
477
478 ifi = NLMSG_DATA (h);
479
480 if (h->nlmsg_type != RTM_NEWLINK)
481 return 0;
482
483 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
484 if (len < 0)
485 return -1;
486
487 /* Looking up interface name. */
488 memset (tb, 0, sizeof tb);
489 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000490
paul1e193152005-02-14 23:53:05 +0000491#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000492 /* check for wireless messages to ignore */
493 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
494 {
495 if (IS_ZEBRA_DEBUG_KERNEL)
496 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
497 return 0;
498 }
paul1e193152005-02-14 23:53:05 +0000499#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +0000500
paul718e3742002-12-13 20:15:29 +0000501 if (tb[IFLA_IFNAME] == NULL)
502 return -1;
paul7021c422003-07-15 12:52:22 +0000503 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +0000504
505 /* Add interface. */
506 ifp = if_get_by_name (name);
paul7021c422003-07-15 12:52:22 +0000507
paul718e3742002-12-13 20:15:29 +0000508 ifp->ifindex = ifi->ifi_index;
509 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +0000510 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul718e3742002-12-13 20:15:29 +0000511 ifp->metric = 1;
512
513 /* Hardware type and address. */
514 ifp->hw_type = ifi->ifi_type;
515
516 if (tb[IFLA_ADDRESS])
517 {
518 int hw_addr_len;
519
paul7021c422003-07-15 12:52:22 +0000520 hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
paul718e3742002-12-13 20:15:29 +0000521
522 if (hw_addr_len > INTERFACE_HWADDR_MAX)
paul7021c422003-07-15 12:52:22 +0000523 zlog_warn ("Hardware address is too large: %d", hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000524 else
paul7021c422003-07-15 12:52:22 +0000525 {
526 ifp->hw_addr_len = hw_addr_len;
527 memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000528
paul7021c422003-07-15 12:52:22 +0000529 for (i = 0; i < hw_addr_len; i++)
530 if (ifp->hw_addr[i] != 0)
531 break;
paul718e3742002-12-13 20:15:29 +0000532
paul7021c422003-07-15 12:52:22 +0000533 if (i == hw_addr_len)
534 ifp->hw_addr_len = 0;
535 else
536 ifp->hw_addr_len = hw_addr_len;
537 }
paul718e3742002-12-13 20:15:29 +0000538 }
539
540 if_add_update (ifp);
541
542 return 0;
543}
544
545/* Lookup interface IPv4/IPv6 address. */
546int
547netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h)
548{
549 int len;
550 struct ifaddrmsg *ifa;
paul7021c422003-07-15 12:52:22 +0000551 struct rtattr *tb[IFA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000552 struct interface *ifp;
553 void *addr = NULL;
554 void *broad = NULL;
555 u_char flags = 0;
556 char *label = NULL;
557
558 ifa = NLMSG_DATA (h);
559
paul7021c422003-07-15 12:52:22 +0000560 if (ifa->ifa_family != AF_INET
paul718e3742002-12-13 20:15:29 +0000561#ifdef HAVE_IPV6
562 && ifa->ifa_family != AF_INET6
563#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +0000564 )
paul718e3742002-12-13 20:15:29 +0000565 return 0;
566
567 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
568 return 0;
569
paul7021c422003-07-15 12:52:22 +0000570 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +0000571 if (len < 0)
572 return -1;
573
574 memset (tb, 0, sizeof tb);
575 netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
576
577 ifp = if_lookup_by_index (ifa->ifa_index);
578 if (ifp == NULL)
579 {
580 zlog_err ("netlink_interface_addr can't find interface by index %d",
paul7021c422003-07-15 12:52:22 +0000581 ifa->ifa_index);
paul718e3742002-12-13 20:15:29 +0000582 return -1;
583 }
584
paul7021c422003-07-15 12:52:22 +0000585 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
paul718e3742002-12-13 20:15:29 +0000586 {
paul00df0c12002-12-13 21:07:36 +0000587 char buf[BUFSIZ];
ajsb6178002004-12-07 21:12:56 +0000588 zlog_debug ("netlink_interface_addr %s %s/%d:",
paul7021c422003-07-15 12:52:22 +0000589 lookup (nlmsg_str, h->nlmsg_type),
590 ifp->name, ifa->ifa_prefixlen);
paul718e3742002-12-13 20:15:29 +0000591 if (tb[IFA_LOCAL])
ajsb6178002004-12-07 21:12:56 +0000592 zlog_debug (" IFA_LOCAL %s", inet_ntop (ifa->ifa_family,
paul7021c422003-07-15 12:52:22 +0000593 RTA_DATA (tb[IFA_LOCAL]),
594 buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +0000595 if (tb[IFA_ADDRESS])
ajsb6178002004-12-07 21:12:56 +0000596 zlog_debug (" IFA_ADDRESS %s", inet_ntop (ifa->ifa_family,
paul7021c422003-07-15 12:52:22 +0000597 RTA_DATA (tb
598 [IFA_ADDRESS]),
599 buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +0000600 if (tb[IFA_BROADCAST])
ajsb6178002004-12-07 21:12:56 +0000601 zlog_debug (" IFA_BROADCAST %s", inet_ntop (ifa->ifa_family,
paul7021c422003-07-15 12:52:22 +0000602 RTA_DATA (tb
603 [IFA_BROADCAST]),
604 buf, BUFSIZ));
paul00df0c12002-12-13 21:07:36 +0000605 if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
ajsb6178002004-12-07 21:12:56 +0000606 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
paul718e3742002-12-13 20:15:29 +0000607 }
paul31a476c2003-09-29 19:54:53 +0000608
609 if (tb[IFA_ADDRESS] == NULL)
610 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
611
612 if (ifp->flags & IFF_POINTOPOINT)
paul7021c422003-07-15 12:52:22 +0000613 {
paul31a476c2003-09-29 19:54:53 +0000614 if (tb[IFA_LOCAL])
615 {
616 addr = RTA_DATA (tb[IFA_LOCAL]);
hasso3fb9cd62004-10-19 19:44:43 +0000617 if (tb[IFA_ADDRESS] &&
618 memcmp(RTA_DATA(tb[IFA_ADDRESS]),RTA_DATA(tb[IFA_LOCAL]),4))
619 /* if IFA_ADDRESS != IFA_LOCAL, then it's the peer address */
paul31a476c2003-09-29 19:54:53 +0000620 broad = RTA_DATA (tb[IFA_ADDRESS]);
621 else
622 broad = NULL;
623 }
624 else
625 {
626 if (tb[IFA_ADDRESS])
627 addr = RTA_DATA (tb[IFA_ADDRESS]);
628 else
629 addr = NULL;
630 }
paul7021c422003-07-15 12:52:22 +0000631 }
paul31a476c2003-09-29 19:54:53 +0000632 else
paul7021c422003-07-15 12:52:22 +0000633 {
paul31a476c2003-09-29 19:54:53 +0000634 if (tb[IFA_ADDRESS])
635 addr = RTA_DATA (tb[IFA_ADDRESS]);
636 else
637 addr = NULL;
638
639 if (tb[IFA_BROADCAST])
640 broad = RTA_DATA(tb[IFA_BROADCAST]);
641 else
642 broad = NULL;
paul7021c422003-07-15 12:52:22 +0000643 }
paul00df0c12002-12-13 21:07:36 +0000644
paul718e3742002-12-13 20:15:29 +0000645 /* Flags. */
646 if (ifa->ifa_flags & IFA_F_SECONDARY)
647 SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
648
649 /* Label */
650 if (tb[IFA_LABEL])
651 label = (char *) RTA_DATA (tb[IFA_LABEL]);
652
653 if (ifp && label && strcmp (ifp->name, label) == 0)
654 label = NULL;
655
656 /* Register interface address to the interface. */
657 if (ifa->ifa_family == AF_INET)
658 {
paul7021c422003-07-15 12:52:22 +0000659 if (h->nlmsg_type == RTM_NEWADDR)
660 connected_add_ipv4 (ifp, flags,
661 (struct in_addr *) addr, ifa->ifa_prefixlen,
662 (struct in_addr *) broad, label);
663 else
664 connected_delete_ipv4 (ifp, flags,
665 (struct in_addr *) addr, ifa->ifa_prefixlen,
666 (struct in_addr *) broad, label);
paul718e3742002-12-13 20:15:29 +0000667 }
668#ifdef HAVE_IPV6
669 if (ifa->ifa_family == AF_INET6)
670 {
671 if (h->nlmsg_type == RTM_NEWADDR)
paul7021c422003-07-15 12:52:22 +0000672 connected_add_ipv6 (ifp,
673 (struct in6_addr *) addr, ifa->ifa_prefixlen,
674 (struct in6_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000675 else
paul7021c422003-07-15 12:52:22 +0000676 connected_delete_ipv6 (ifp,
677 (struct in6_addr *) addr, ifa->ifa_prefixlen,
678 (struct in6_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000679 }
paul7021c422003-07-15 12:52:22 +0000680#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000681
682 return 0;
683}
684
685/* Looking up routing table by netlink interface. */
686int
687netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h)
688{
689 int len;
690 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000691 struct rtattr *tb[RTA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000692 u_char flags = 0;
paul7021c422003-07-15 12:52:22 +0000693
694 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000695
696 int index;
697 int table;
hasso34195bf2004-04-06 12:07:06 +0000698 int metric;
699
paul718e3742002-12-13 20:15:29 +0000700 void *dest;
701 void *gate;
702
703 rtm = NLMSG_DATA (h);
704
705 if (h->nlmsg_type != RTM_NEWROUTE)
706 return 0;
707 if (rtm->rtm_type != RTN_UNICAST)
708 return 0;
709
710 table = rtm->rtm_table;
paul7021c422003-07-15 12:52:22 +0000711#if 0 /* we weed them out later in rib_weed_tables () */
paulb21b19c2003-06-15 01:28:29 +0000712 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000713 return 0;
714#endif
715
paul7021c422003-07-15 12:52:22 +0000716 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000717 if (len < 0)
718 return -1;
719
720 memset (tb, 0, sizeof tb);
721 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
722
723 if (rtm->rtm_flags & RTM_F_CLONED)
724 return 0;
725 if (rtm->rtm_protocol == RTPROT_REDIRECT)
726 return 0;
727 if (rtm->rtm_protocol == RTPROT_KERNEL)
728 return 0;
729
730 if (rtm->rtm_src_len != 0)
731 return 0;
732
733 /* Route which inserted by Zebra. */
734 if (rtm->rtm_protocol == RTPROT_ZEBRA)
735 flags |= ZEBRA_FLAG_SELFROUTE;
paul7021c422003-07-15 12:52:22 +0000736
paul718e3742002-12-13 20:15:29 +0000737 index = 0;
hasso34195bf2004-04-06 12:07:06 +0000738 metric = 0;
paul718e3742002-12-13 20:15:29 +0000739 dest = NULL;
740 gate = NULL;
741
742 if (tb[RTA_OIF])
743 index = *(int *) RTA_DATA (tb[RTA_OIF]);
744
745 if (tb[RTA_DST])
746 dest = RTA_DATA (tb[RTA_DST]);
747 else
748 dest = anyaddr;
749
750 /* Multipath treatment is needed. */
751 if (tb[RTA_GATEWAY])
752 gate = RTA_DATA (tb[RTA_GATEWAY]);
753
hasso34195bf2004-04-06 12:07:06 +0000754 if (tb[RTA_PRIORITY])
755 metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
756
paul718e3742002-12-13 20:15:29 +0000757 if (rtm->rtm_family == AF_INET)
758 {
759 struct prefix_ipv4 p;
760 p.family = AF_INET;
761 memcpy (&p.prefix, dest, 4);
762 p.prefixlen = rtm->rtm_dst_len;
763
hasso34195bf2004-04-06 12:07:06 +0000764 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table, metric, 0);
paul718e3742002-12-13 20:15:29 +0000765 }
766#ifdef HAVE_IPV6
767 if (rtm->rtm_family == AF_INET6)
768 {
769 struct prefix_ipv6 p;
770 p.family = AF_INET6;
771 memcpy (&p.prefix, dest, 16);
772 p.prefixlen = rtm->rtm_dst_len;
773
774 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table);
775 }
776#endif /* HAVE_IPV6 */
777
778 return 0;
779}
780
paul7021c422003-07-15 12:52:22 +0000781struct message rtproto_str[] = {
paul718e3742002-12-13 20:15:29 +0000782 {RTPROT_REDIRECT, "redirect"},
783 {RTPROT_KERNEL, "kernel"},
784 {RTPROT_BOOT, "boot"},
785 {RTPROT_STATIC, "static"},
786 {RTPROT_GATED, "GateD"},
787 {RTPROT_RA, "router advertisement"},
788 {RTPROT_MRT, "MRT"},
789 {RTPROT_ZEBRA, "Zebra"},
790#ifdef RTPROT_BIRD
791 {RTPROT_BIRD, "BIRD"},
792#endif /* RTPROT_BIRD */
793 {0, NULL}
794};
795
796/* Routing information change from the kernel. */
797int
798netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
799{
800 int len;
801 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000802 struct rtattr *tb[RTA_MAX + 1];
803
804 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000805
806 int index;
807 int table;
808 void *dest;
809 void *gate;
810
811 rtm = NLMSG_DATA (h);
812
paul7021c422003-07-15 12:52:22 +0000813 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
paul718e3742002-12-13 20:15:29 +0000814 {
815 /* If this is not route add/delete message print warning. */
816 zlog_warn ("Kernel message: %d\n", h->nlmsg_type);
817 return 0;
818 }
819
820 /* Connected route. */
821 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000822 zlog_debug ("%s %s %s proto %s",
paul7021c422003-07-15 12:52:22 +0000823 h->nlmsg_type ==
824 RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
825 rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
826 rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
827 lookup (rtproto_str, rtm->rtm_protocol));
paul718e3742002-12-13 20:15:29 +0000828
829 if (rtm->rtm_type != RTN_UNICAST)
830 {
831 return 0;
832 }
833
834 table = rtm->rtm_table;
paulb21b19c2003-06-15 01:28:29 +0000835 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000836 {
837 return 0;
838 }
839
paul7021c422003-07-15 12:52:22 +0000840 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000841 if (len < 0)
842 return -1;
843
844 memset (tb, 0, sizeof tb);
845 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
846
847 if (rtm->rtm_flags & RTM_F_CLONED)
848 return 0;
849 if (rtm->rtm_protocol == RTPROT_REDIRECT)
850 return 0;
851 if (rtm->rtm_protocol == RTPROT_KERNEL)
852 return 0;
853
854 if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
855 return 0;
856
857 if (rtm->rtm_src_len != 0)
858 {
859 zlog_warn ("netlink_route_change(): no src len");
860 return 0;
861 }
paul7021c422003-07-15 12:52:22 +0000862
paul718e3742002-12-13 20:15:29 +0000863 index = 0;
864 dest = NULL;
865 gate = NULL;
866
867 if (tb[RTA_OIF])
868 index = *(int *) RTA_DATA (tb[RTA_OIF]);
869
870 if (tb[RTA_DST])
871 dest = RTA_DATA (tb[RTA_DST]);
872 else
873 dest = anyaddr;
874
875 if (tb[RTA_GATEWAY])
876 gate = RTA_DATA (tb[RTA_GATEWAY]);
877
878 if (rtm->rtm_family == AF_INET)
879 {
880 struct prefix_ipv4 p;
881 p.family = AF_INET;
882 memcpy (&p.prefix, dest, 4);
883 p.prefixlen = rtm->rtm_dst_len;
884
885 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000886 {
887 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000888 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000889 inet_ntoa (p.prefix), p.prefixlen);
890 else
ajsb6178002004-12-07 21:12:56 +0000891 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000892 inet_ntoa (p.prefix), p.prefixlen);
893 }
paul718e3742002-12-13 20:15:29 +0000894
895 if (h->nlmsg_type == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +0000896 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0);
paul718e3742002-12-13 20:15:29 +0000897 else
paul7021c422003-07-15 12:52:22 +0000898 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
paul718e3742002-12-13 20:15:29 +0000899 }
900
901#ifdef HAVE_IPV6
902 if (rtm->rtm_family == AF_INET6)
903 {
904 struct prefix_ipv6 p;
905 char buf[BUFSIZ];
906
907 p.family = AF_INET6;
908 memcpy (&p.prefix, dest, 16);
909 p.prefixlen = rtm->rtm_dst_len;
910
911 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000912 {
913 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000914 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000915 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
916 p.prefixlen);
917 else
ajsb6178002004-12-07 21:12:56 +0000918 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000919 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
920 p.prefixlen);
921 }
paul718e3742002-12-13 20:15:29 +0000922
923 if (h->nlmsg_type == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +0000924 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
paul718e3742002-12-13 20:15:29 +0000925 else
paul7021c422003-07-15 12:52:22 +0000926 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
paul718e3742002-12-13 20:15:29 +0000927 }
928#endif /* HAVE_IPV6 */
929
930 return 0;
931}
932
933int
934netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
935{
936 int len;
937 struct ifinfomsg *ifi;
paul7021c422003-07-15 12:52:22 +0000938 struct rtattr *tb[IFLA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000939 struct interface *ifp;
940 char *name;
941
942 ifi = NLMSG_DATA (h);
943
paul7021c422003-07-15 12:52:22 +0000944 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
paul718e3742002-12-13 20:15:29 +0000945 {
946 /* If this is not link add/delete message so print warning. */
947 zlog_warn ("netlink_link_change: wrong kernel message %d\n",
paul7021c422003-07-15 12:52:22 +0000948 h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +0000949 return 0;
950 }
951
952 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
953 if (len < 0)
954 return -1;
955
956 /* Looking up interface name. */
957 memset (tb, 0, sizeof tb);
958 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000959
paul1e193152005-02-14 23:53:05 +0000960#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000961 /* check for wireless messages to ignore */
962 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
963 {
964 if (IS_ZEBRA_DEBUG_KERNEL)
965 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
966 return 0;
967 }
paul1e193152005-02-14 23:53:05 +0000968#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +0000969
paul718e3742002-12-13 20:15:29 +0000970 if (tb[IFLA_IFNAME] == NULL)
971 return -1;
paul7021c422003-07-15 12:52:22 +0000972 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +0000973
974 /* Add interface. */
975 if (h->nlmsg_type == RTM_NEWLINK)
976 {
977 ifp = if_lookup_by_name (name);
978
paul7021c422003-07-15 12:52:22 +0000979 if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
980 {
981 if (ifp == NULL)
982 ifp = if_get_by_name (name);
paul718e3742002-12-13 20:15:29 +0000983
paul7021c422003-07-15 12:52:22 +0000984 ifp->ifindex = ifi->ifi_index;
985 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +0000986 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +0000987 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +0000988
paul7021c422003-07-15 12:52:22 +0000989 /* If new link is added. */
990 if_add_update (ifp);
991 }
paul718e3742002-12-13 20:15:29 +0000992 else
paul7021c422003-07-15 12:52:22 +0000993 {
994 /* Interface status change. */
995 ifp->ifindex = ifi->ifi_index;
paul44145db2004-05-09 11:00:23 +0000996 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +0000997 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +0000998
paul7021c422003-07-15 12:52:22 +0000999 if (if_is_operative (ifp))
1000 {
1001 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1002 if (!if_is_operative (ifp))
1003 if_down (ifp);
1004 }
1005 else
1006 {
1007 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1008 if (if_is_operative (ifp))
1009 if_up (ifp);
1010 }
1011 }
paul718e3742002-12-13 20:15:29 +00001012 }
1013 else
1014 {
1015 /* RTM_DELLINK. */
1016 ifp = if_lookup_by_name (name);
1017
1018 if (ifp == NULL)
paul7021c422003-07-15 12:52:22 +00001019 {
1020 zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find",
paul718e3742002-12-13 20:15:29 +00001021 name);
paul7021c422003-07-15 12:52:22 +00001022 return 0;
1023 }
1024
paul718e3742002-12-13 20:15:29 +00001025 if_delete_update (ifp);
1026 }
1027
1028 return 0;
1029}
1030
1031int
1032netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h)
1033{
1034 switch (h->nlmsg_type)
1035 {
1036 case RTM_NEWROUTE:
1037 return netlink_route_change (snl, h);
1038 break;
1039 case RTM_DELROUTE:
1040 return netlink_route_change (snl, h);
1041 break;
1042 case RTM_NEWLINK:
1043 return netlink_link_change (snl, h);
1044 break;
1045 case RTM_DELLINK:
1046 return netlink_link_change (snl, h);
1047 break;
1048 case RTM_NEWADDR:
1049 return netlink_interface_addr (snl, h);
1050 break;
1051 case RTM_DELADDR:
1052 return netlink_interface_addr (snl, h);
1053 break;
1054 default:
1055 zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type);
1056 break;
1057 }
1058 return 0;
1059}
1060
1061/* Interface lookup by netlink socket. */
1062int
1063interface_lookup_netlink ()
1064{
1065 int ret;
paul5f37d862003-04-19 00:11:28 +00001066 int flags;
1067 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001068
paul5f37d862003-04-19 00:11:28 +00001069 /*
1070 * Change netlink socket flags to blocking to ensure we get
1071 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001072 */
1073 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1074 if (snb_ret < 0)
1075 zlog (NULL, LOG_WARNING,
1076 "%s:%i Warning: Could not set netlink socket to blocking.",
1077 __FUNCTION__, __LINE__);
1078
paul718e3742002-12-13 20:15:29 +00001079 /* Get interface information. */
1080 ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd);
1081 if (ret < 0)
1082 return ret;
1083 ret = netlink_parse_info (netlink_interface, &netlink_cmd);
1084 if (ret < 0)
1085 return ret;
1086
1087 /* Get IPv4 address of the interfaces. */
1088 ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd);
1089 if (ret < 0)
1090 return ret;
1091 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1092 if (ret < 0)
1093 return ret;
1094
1095#ifdef HAVE_IPV6
1096 /* Get IPv6 address of the interfaces. */
1097 ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd);
1098 if (ret < 0)
1099 return ret;
1100 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1101 if (ret < 0)
1102 return ret;
1103#endif /* HAVE_IPV6 */
1104
paul7021c422003-07-15 12:52:22 +00001105 /* restore socket flags */
1106 if (snb_ret == 0)
1107 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001108 return 0;
1109}
1110
1111/* Routing table read function using netlink interface. Only called
1112 bootstrap time. */
1113int
1114netlink_route_read ()
1115{
1116 int ret;
paul5f37d862003-04-19 00:11:28 +00001117 int flags;
1118 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001119
paul5f37d862003-04-19 00:11:28 +00001120 /*
1121 * Change netlink socket flags to blocking to ensure we get
1122 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001123 */
1124 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1125 if (snb_ret < 0)
1126 zlog (NULL, LOG_WARNING,
1127 "%s:%i Warning: Could not set netlink socket to blocking.",
1128 __FUNCTION__, __LINE__);
1129
paul718e3742002-12-13 20:15:29 +00001130 /* Get IPv4 routing table. */
1131 ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd);
1132 if (ret < 0)
1133 return ret;
1134 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1135 if (ret < 0)
1136 return ret;
1137
1138#ifdef HAVE_IPV6
1139 /* Get IPv6 routing table. */
1140 ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd);
1141 if (ret < 0)
1142 return ret;
1143 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1144 if (ret < 0)
1145 return ret;
1146#endif /* HAVE_IPV6 */
1147
paul5f37d862003-04-19 00:11:28 +00001148 /* restore flags */
paul7021c422003-07-15 12:52:22 +00001149 if (snb_ret == 0)
1150 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001151 return 0;
1152}
1153
1154/* Utility function comes from iproute2.
1155 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1156int
1157addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
1158{
1159 int len;
1160 struct rtattr *rta;
1161
paul7021c422003-07-15 12:52:22 +00001162 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001163
paul7021c422003-07-15 12:52:22 +00001164 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001165 return -1;
1166
paul7021c422003-07-15 12:52:22 +00001167 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001168 rta->rta_type = type;
1169 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001170 memcpy (RTA_DATA (rta), data, alen);
paul718e3742002-12-13 20:15:29 +00001171 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1172
1173 return 0;
1174}
1175
1176int
1177rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
1178{
1179 int len;
1180 struct rtattr *subrta;
1181
paul7021c422003-07-15 12:52:22 +00001182 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001183
paul7021c422003-07-15 12:52:22 +00001184 if (RTA_ALIGN (rta->rta_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001185 return -1;
1186
paul7021c422003-07-15 12:52:22 +00001187 subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
paul718e3742002-12-13 20:15:29 +00001188 subrta->rta_type = type;
1189 subrta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001190 memcpy (RTA_DATA (subrta), data, alen);
paul718e3742002-12-13 20:15:29 +00001191 rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
1192
1193 return 0;
1194}
1195
1196/* Utility function comes from iproute2.
1197 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1198int
1199addattr32 (struct nlmsghdr *n, int maxlen, int type, int data)
1200{
1201 int len;
1202 struct rtattr *rta;
paul7021c422003-07-15 12:52:22 +00001203
1204 len = RTA_LENGTH (4);
1205
paul718e3742002-12-13 20:15:29 +00001206 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1207 return -1;
1208
paul7021c422003-07-15 12:52:22 +00001209 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001210 rta->rta_type = type;
1211 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001212 memcpy (RTA_DATA (rta), &data, 4);
paul718e3742002-12-13 20:15:29 +00001213 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1214
1215 return 0;
1216}
1217
1218static int
1219netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h)
1220{
hassoe8274dc2005-02-20 19:09:23 +00001221 if (IS_ZEBRA_DEBUG_KERNEL)
1222 zlog_debug ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +00001223 return 0;
1224}
1225
1226/* sendmsg() to netlink socket then recvmsg(). */
1227int
1228netlink_talk (struct nlmsghdr *n, struct nlsock *nl)
1229{
1230 int status;
1231 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +00001232 struct iovec iov = { (void *) n, n->nlmsg_len };
1233 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +00001234 int flags = 0;
paul5f37d862003-04-19 00:11:28 +00001235 int snb_ret;
ajs4be019d2005-01-29 16:12:41 +00001236 int save_errno;
paul7021c422003-07-15 12:52:22 +00001237
paul718e3742002-12-13 20:15:29 +00001238 memset (&snl, 0, sizeof snl);
1239 snl.nl_family = AF_NETLINK;
paul7021c422003-07-15 12:52:22 +00001240
paul718e3742002-12-13 20:15:29 +00001241 n->nlmsg_seq = ++netlink_cmd.seq;
1242
1243 /* Request an acknowledgement by setting NLM_F_ACK */
1244 n->nlmsg_flags |= NLM_F_ACK;
paul7021c422003-07-15 12:52:22 +00001245
1246 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001247 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", netlink_cmd.name,
paul7021c422003-07-15 12:52:22 +00001248 lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
1249 n->nlmsg_seq);
paul718e3742002-12-13 20:15:29 +00001250
1251 /* Send message to netlink interface. */
paul7021c422003-07-15 12:52:22 +00001252 if (zserv_privs.change (ZPRIVS_RAISE))
1253 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +00001254 status = sendmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +00001255 save_errno = errno;
paul7021c422003-07-15 12:52:22 +00001256 if (zserv_privs.change (ZPRIVS_LOWER))
1257 zlog (NULL, LOG_ERR, "Can't lower privileges");
1258
paul718e3742002-12-13 20:15:29 +00001259 if (status < 0)
1260 {
1261 zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
ajs4be019d2005-01-29 16:12:41 +00001262 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +00001263 return -1;
1264 }
paul7021c422003-07-15 12:52:22 +00001265
paul718e3742002-12-13 20:15:29 +00001266 /*
1267 * Change socket flags for blocking I/O.
1268 * This ensures we wait for a reply in netlink_parse_info().
1269 */
paul7021c422003-07-15 12:52:22 +00001270 snb_ret = set_netlink_blocking (nl, &flags);
1271 if (snb_ret < 0)
1272 zlog (NULL, LOG_WARNING,
1273 "%s:%i Warning: Could not set netlink socket to blocking.",
1274 __FUNCTION__, __LINE__);
paul718e3742002-12-13 20:15:29 +00001275
1276 /*
1277 * Get reply from netlink socket.
1278 * The reply should either be an acknowlegement or an error.
1279 */
1280 status = netlink_parse_info (netlink_talk_filter, nl);
paul7021c422003-07-15 12:52:22 +00001281
paul718e3742002-12-13 20:15:29 +00001282 /* Restore socket flags for nonblocking I/O */
paul7021c422003-07-15 12:52:22 +00001283 if (snb_ret == 0)
1284 set_netlink_nonblocking (nl, &flags);
1285
paul718e3742002-12-13 20:15:29 +00001286 return status;
1287}
1288
1289/* Routing table change via netlink interface. */
1290int
1291netlink_route (int cmd, int family, void *dest, int length, void *gate,
paul7021c422003-07-15 12:52:22 +00001292 int index, int zebra_flags, int table)
paul718e3742002-12-13 20:15:29 +00001293{
1294 int ret;
1295 int bytelen;
1296 struct sockaddr_nl snl;
1297 int discard;
1298
paul7021c422003-07-15 12:52:22 +00001299 struct
paul718e3742002-12-13 20:15:29 +00001300 {
1301 struct nlmsghdr n;
1302 struct rtmsg r;
1303 char buf[1024];
1304 } req;
1305
1306 memset (&req, 0, sizeof req);
1307
1308 bytelen = (family == AF_INET ? 4 : 16);
1309
1310 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1311 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1312 req.n.nlmsg_type = cmd;
1313 req.r.rtm_family = family;
1314 req.r.rtm_table = table;
1315 req.r.rtm_dst_len = length;
1316
hasso81dfcaa2003-05-25 19:21:25 +00001317 if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1318 || (zebra_flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001319 discard = 1;
1320 else
1321 discard = 0;
1322
paul7021c422003-07-15 12:52:22 +00001323 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001324 {
1325 req.r.rtm_protocol = RTPROT_ZEBRA;
1326 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1327
paul7021c422003-07-15 12:52:22 +00001328 if (discard)
paul595db7f2003-05-25 21:35:06 +00001329 {
1330 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1331 req.r.rtm_type = RTN_BLACKHOLE;
1332 else if (zebra_flags & ZEBRA_FLAG_REJECT)
1333 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001334 else
1335 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1336 }
paul595db7f2003-05-25 21:35:06 +00001337 else
paul7021c422003-07-15 12:52:22 +00001338 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001339 }
1340
1341 if (dest)
1342 addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen);
1343
paul7021c422003-07-15 12:52:22 +00001344 if (!discard)
paul718e3742002-12-13 20:15:29 +00001345 {
1346 if (gate)
paul7021c422003-07-15 12:52:22 +00001347 addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen);
paul718e3742002-12-13 20:15:29 +00001348 if (index > 0)
paul7021c422003-07-15 12:52:22 +00001349 addattr32 (&req.n, sizeof req, RTA_OIF, index);
paul718e3742002-12-13 20:15:29 +00001350 }
1351
1352 /* Destination netlink address. */
1353 memset (&snl, 0, sizeof snl);
1354 snl.nl_family = AF_NETLINK;
1355
1356 /* Talk to netlink socket. */
1357 ret = netlink_talk (&req.n, &netlink);
1358 if (ret < 0)
1359 return -1;
1360
1361 return 0;
1362}
1363
1364/* Routing table change via netlink interface. */
1365int
1366netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
paul7021c422003-07-15 12:52:22 +00001367 int family)
paul718e3742002-12-13 20:15:29 +00001368{
1369 int bytelen;
1370 struct sockaddr_nl snl;
1371 struct nexthop *nexthop = NULL;
1372 int nexthop_num = 0;
1373 struct nlsock *nl;
1374 int discard;
1375
paul7021c422003-07-15 12:52:22 +00001376 struct
paul718e3742002-12-13 20:15:29 +00001377 {
1378 struct nlmsghdr n;
1379 struct rtmsg r;
1380 char buf[1024];
1381 } req;
1382
1383 memset (&req, 0, sizeof req);
1384
1385 bytelen = (family == AF_INET ? 4 : 16);
1386
1387 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1388 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1389 req.n.nlmsg_type = cmd;
1390 req.r.rtm_family = family;
1391 req.r.rtm_table = rib->table;
1392 req.r.rtm_dst_len = p->prefixlen;
1393
paul7021c422003-07-15 12:52:22 +00001394 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001395 discard = 1;
1396 else
1397 discard = 0;
1398
paul7021c422003-07-15 12:52:22 +00001399 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001400 {
1401 req.r.rtm_protocol = RTPROT_ZEBRA;
1402 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1403
paul7021c422003-07-15 12:52:22 +00001404 if (discard)
paul595db7f2003-05-25 21:35:06 +00001405 {
1406 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
1407 req.r.rtm_type = RTN_BLACKHOLE;
1408 else if (rib->flags & ZEBRA_FLAG_REJECT)
1409 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001410 else
1411 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1412 }
paul595db7f2003-05-25 21:35:06 +00001413 else
paul7021c422003-07-15 12:52:22 +00001414 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001415 }
1416
1417 addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1418
1419 /* Metric. */
1420 addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
1421
1422 if (discard)
1423 {
1424 if (cmd == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +00001425 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1426 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001427 goto skip;
1428 }
1429
1430 /* Multipath case. */
1431 if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
1432 {
1433 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
paul7021c422003-07-15 12:52:22 +00001434 {
paul5ec90d22003-06-19 01:41:37 +00001435
paul7021c422003-07-15 12:52:22 +00001436 if ((cmd == RTM_NEWROUTE
1437 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1438 || (cmd == RTM_DELROUTE
1439 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1440 {
paul5ec90d22003-06-19 01:41:37 +00001441
paul7021c422003-07-15 12:52:22 +00001442 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1443 {
1444 if (IS_ZEBRA_DEBUG_KERNEL)
1445 {
ajsb6178002004-12-07 21:12:56 +00001446 zlog_debug
paul7021c422003-07-15 12:52:22 +00001447 ("netlink_route_multipath() (recursive, 1 hop): "
1448 "%s %s/%d via %s if %u, type %s",
1449 lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
1450 p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
1451 nexthop->rifindex,
1452 nexthop_types_desc[nexthop->rtype]);
1453 }
paul5ec90d22003-06-19 01:41:37 +00001454
paul7021c422003-07-15 12:52:22 +00001455 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1456 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
1457 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1458 &nexthop->rgate.ipv4, bytelen);
paul718e3742002-12-13 20:15:29 +00001459#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001460 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1461 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1462 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
1463 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1464 &nexthop->rgate.ipv6, bytelen);
paul718e3742002-12-13 20:15:29 +00001465#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001466 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1467 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
1468 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1469 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1470 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
1471 addattr32 (&req.n, sizeof req, RTA_OIF,
1472 nexthop->rifindex);
1473 }
1474 else
1475 {
1476 if (IS_ZEBRA_DEBUG_KERNEL)
1477 {
ajsb6178002004-12-07 21:12:56 +00001478 zlog_debug
paul7021c422003-07-15 12:52:22 +00001479 ("netlink_route_multipath(): (single hop)"
1480 "%s %s/%d via %s if %u, type %s",
1481 lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
1482 p->prefixlen, inet_ntoa (nexthop->gate.ipv4),
1483 nexthop->ifindex,
1484 nexthop_types_desc[nexthop->type]);
1485 }
paul5ec90d22003-06-19 01:41:37 +00001486
paul7021c422003-07-15 12:52:22 +00001487 if (nexthop->type == NEXTHOP_TYPE_IPV4
1488 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1489 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1490 &nexthop->gate.ipv4, bytelen);
paul718e3742002-12-13 20:15:29 +00001491#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001492 if (nexthop->type == NEXTHOP_TYPE_IPV6
1493 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1494 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1495 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1496 &nexthop->gate.ipv6, bytelen);
paul718e3742002-12-13 20:15:29 +00001497#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001498 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1499 || nexthop->type == NEXTHOP_TYPE_IFNAME
1500 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1501 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
1502 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
1503 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1504 }
paul718e3742002-12-13 20:15:29 +00001505
paul7021c422003-07-15 12:52:22 +00001506 if (cmd == RTM_NEWROUTE)
1507 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001508
paul7021c422003-07-15 12:52:22 +00001509 nexthop_num++;
1510 break;
1511 }
1512 }
paul718e3742002-12-13 20:15:29 +00001513 }
1514 else
1515 {
1516 char buf[1024];
1517 struct rtattr *rta = (void *) buf;
1518 struct rtnexthop *rtnh;
1519
1520 rta->rta_type = RTA_MULTIPATH;
paul7021c422003-07-15 12:52:22 +00001521 rta->rta_len = RTA_LENGTH (0);
1522 rtnh = RTA_DATA (rta);
paul718e3742002-12-13 20:15:29 +00001523
1524 nexthop_num = 0;
1525 for (nexthop = rib->nexthop;
paul7021c422003-07-15 12:52:22 +00001526 nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM);
1527 nexthop = nexthop->next)
1528 {
1529 if ((cmd == RTM_NEWROUTE
1530 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1531 || (cmd == RTM_DELROUTE
1532 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1533 {
1534 nexthop_num++;
paul718e3742002-12-13 20:15:29 +00001535
paul7021c422003-07-15 12:52:22 +00001536 rtnh->rtnh_len = sizeof (*rtnh);
1537 rtnh->rtnh_flags = 0;
1538 rtnh->rtnh_hops = 0;
1539 rta->rta_len += rtnh->rtnh_len;
paul718e3742002-12-13 20:15:29 +00001540
paul7021c422003-07-15 12:52:22 +00001541 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1542 {
1543 if (IS_ZEBRA_DEBUG_KERNEL)
1544 {
ajsb6178002004-12-07 21:12:56 +00001545 zlog_debug ("netlink_route_multipath() "
paul7021c422003-07-15 12:52:22 +00001546 "(recursive, multihop): "
1547 "%s %s/%d via %s if %u, type %s",
1548 lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
1549 p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
1550 nexthop->rifindex,
1551 nexthop_types_desc[nexthop->type]);
1552 }
1553 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1554 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
1555 {
1556 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1557 &nexthop->rgate.ipv4, bytelen);
1558 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
1559 }
paul718e3742002-12-13 20:15:29 +00001560#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001561 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1562 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
1563 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
1564 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1565 &nexthop->rgate.ipv6, bytelen);
paul718e3742002-12-13 20:15:29 +00001566#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001567 /* ifindex */
1568 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1569 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
1570 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1571 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1572 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
1573 rtnh->rtnh_ifindex = nexthop->rifindex;
1574 else
1575 rtnh->rtnh_ifindex = 0;
1576 }
1577 else
1578 {
1579 if (IS_ZEBRA_DEBUG_KERNEL)
1580 {
ajsb6178002004-12-07 21:12:56 +00001581 zlog_debug ("netlink_route_multipath() "
paul7021c422003-07-15 12:52:22 +00001582 "(multihop): "
1583 "%s %s/%d via %s if %u, type %s",
1584 lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4),
1585 p->prefixlen, inet_ntoa (nexthop->rgate.ipv4),
1586 nexthop->rifindex,
1587 nexthop_types_desc[nexthop->type]);
1588 }
1589 if (nexthop->type == NEXTHOP_TYPE_IPV4
1590 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1591 {
1592 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1593 &nexthop->gate.ipv4, bytelen);
1594 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
1595 }
paul718e3742002-12-13 20:15:29 +00001596#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001597 if (nexthop->type == NEXTHOP_TYPE_IPV6
1598 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1599 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1600 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1601 &nexthop->gate.ipv6, bytelen);
paul718e3742002-12-13 20:15:29 +00001602#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001603 /* ifindex */
1604 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1605 || nexthop->type == NEXTHOP_TYPE_IFNAME
1606 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1607 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1608 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1609 rtnh->rtnh_ifindex = nexthop->ifindex;
1610 else
1611 rtnh->rtnh_ifindex = 0;
1612 }
1613 rtnh = RTNH_NEXT (rtnh);
paul718e3742002-12-13 20:15:29 +00001614
paul7021c422003-07-15 12:52:22 +00001615 if (cmd == RTM_NEWROUTE)
1616 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1617 }
1618 }
paul718e3742002-12-13 20:15:29 +00001619
1620 if (rta->rta_len > RTA_LENGTH (0))
paul7021c422003-07-15 12:52:22 +00001621 addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta),
1622 RTA_PAYLOAD (rta));
paul718e3742002-12-13 20:15:29 +00001623 }
1624
1625 /* If there is no useful nexthop then return. */
1626 if (nexthop_num == 0)
1627 {
1628 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001629 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +00001630 return 0;
1631 }
1632
paul7021c422003-07-15 12:52:22 +00001633skip:
paul718e3742002-12-13 20:15:29 +00001634
1635 /* Destination netlink address. */
1636 memset (&snl, 0, sizeof snl);
1637 snl.nl_family = AF_NETLINK;
1638
1639 if (family == AF_INET)
1640 nl = &netlink_cmd;
1641 else
1642 nl = &netlink;
1643
1644 /* Talk to netlink socket. */
1645 return netlink_talk (&req.n, nl);
1646}
1647
1648int
1649kernel_add_ipv4 (struct prefix *p, struct rib *rib)
1650{
1651 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
1652}
1653
1654int
1655kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
1656{
1657 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
1658}
1659
1660#ifdef HAVE_IPV6
1661int
1662kernel_add_ipv6 (struct prefix *p, struct rib *rib)
1663{
1664 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
1665}
1666
1667int
1668kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
1669{
1670 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
1671}
1672
1673/* Delete IPv6 route from the kernel. */
1674int
1675kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul7021c422003-07-15 12:52:22 +00001676 int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +00001677{
paul7021c422003-07-15 12:52:22 +00001678 return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix,
1679 dest->prefixlen, gate, index, flags, table);
paul718e3742002-12-13 20:15:29 +00001680}
1681#endif /* HAVE_IPV6 */
1682
1683/* Interface address modification. */
1684int
1685netlink_address (int cmd, int family, struct interface *ifp,
paul7021c422003-07-15 12:52:22 +00001686 struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001687{
1688 int bytelen;
1689 struct prefix *p;
1690
paul7021c422003-07-15 12:52:22 +00001691 struct
paul718e3742002-12-13 20:15:29 +00001692 {
1693 struct nlmsghdr n;
1694 struct ifaddrmsg ifa;
1695 char buf[1024];
1696 } req;
1697
1698 p = ifc->address;
1699 memset (&req, 0, sizeof req);
1700
1701 bytelen = (family == AF_INET ? 4 : 16);
1702
paul7021c422003-07-15 12:52:22 +00001703 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +00001704 req.n.nlmsg_flags = NLM_F_REQUEST;
1705 req.n.nlmsg_type = cmd;
1706 req.ifa.ifa_family = family;
1707
1708 req.ifa.ifa_index = ifp->ifindex;
1709 req.ifa.ifa_prefixlen = p->prefixlen;
1710
1711 addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1712
1713 if (family == AF_INET && cmd == RTM_NEWADDR)
1714 {
1715 if (if_is_broadcast (ifp) && ifc->destination)
paul7021c422003-07-15 12:52:22 +00001716 {
1717 p = ifc->destination;
1718 addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
1719 bytelen);
1720 }
paul718e3742002-12-13 20:15:29 +00001721 }
1722
1723 if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
1724 SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
paul7021c422003-07-15 12:52:22 +00001725
paul718e3742002-12-13 20:15:29 +00001726 if (ifc->label)
1727 addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
paul7021c422003-07-15 12:52:22 +00001728 strlen (ifc->label) + 1);
paul718e3742002-12-13 20:15:29 +00001729
1730 return netlink_talk (&req.n, &netlink_cmd);
1731}
1732
1733int
1734kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
1735{
1736 return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
1737}
1738
1739int
1740kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
1741{
1742 return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
1743}
1744
paul718e3742002-12-13 20:15:29 +00001745
1746extern struct thread_master *master;
1747
1748/* Kernel route reflection. */
1749int
1750kernel_read (struct thread *thread)
1751{
1752 int ret;
1753 int sock;
1754
1755 sock = THREAD_FD (thread);
1756 ret = netlink_parse_info (netlink_information_fetch, &netlink);
paulb21b19c2003-06-15 01:28:29 +00001757 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001758
1759 return 0;
1760}
1761
1762/* Exported interface function. This function simply calls
1763 netlink_socket (). */
1764void
1765kernel_init ()
1766{
1767 unsigned long groups;
1768
paul7021c422003-07-15 12:52:22 +00001769 groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
paul718e3742002-12-13 20:15:29 +00001770#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001771 groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
paul718e3742002-12-13 20:15:29 +00001772#endif /* HAVE_IPV6 */
1773 netlink_socket (&netlink, groups);
1774 netlink_socket (&netlink_cmd, 0);
1775
1776 /* Register kernel socket. */
1777 if (netlink.sock > 0)
paulb21b19c2003-06-15 01:28:29 +00001778 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001779}