blob: 216625e93e850fd16a804044fccc502ad664337a [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"
paul6621ca82005-11-23 13:02:08 +000040#include "zebra/rt.h"
paul718e3742002-12-13 20:15:29 +000041#include "zebra/redistribute.h"
42#include "zebra/interface.h"
43#include "zebra/debug.h"
44
45/* Socket interface to kernel */
46struct nlsock
47{
48 int sock;
49 int seq;
50 struct sockaddr_nl snl;
hassofce954f2004-10-07 20:29:24 +000051 const char *name;
paul7021c422003-07-15 12:52:22 +000052} netlink = { -1, 0, {0}, "netlink-listen"}, /* kernel messages */
hasso1ada8192005-06-12 11:28:18 +000053 netlink_cmd = { -1, 0, {0}, "netlink-cmd"}; /* command 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",
hassofa599802005-04-09 16:59:28 +000076 "IPv6 nexthop",
paul7021c422003-07-15 12:52:22 +000077 "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
ajsd2fc8892005-04-02 18:38:43 +000089/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
90 names and ifindex values. */
91static void
92set_ifindex(struct interface *ifp, unsigned int ifi_index)
93{
94 struct interface *oifp;
95
96 if (((oifp = if_lookup_by_index(ifi_index)) != NULL) && (oifp != ifp))
97 {
98 if (ifi_index == IFINDEX_INTERNAL)
99 zlog_err("Netlink is setting interface %s ifindex to reserved "
100 "internal value %u", ifp->name, ifi_index);
101 else
102 {
103 if (IS_ZEBRA_DEBUG_KERNEL)
104 zlog_debug("interface index %d was renamed from %s to %s",
105 ifi_index, oifp->name, ifp->name);
106 if (if_is_up(oifp))
107 zlog_err("interface rename detected on up interface: index %d "
108 "was renamed from %s to %s, results are uncertain!",
109 ifi_index, oifp->name, ifp->name);
110 if_delete_update(oifp);
111 }
112 }
113 ifp->ifindex = ifi_index;
114}
115
paul718e3742002-12-13 20:15:29 +0000116/* Make socket for Linux netlink interface. */
117static int
118netlink_socket (struct nlsock *nl, unsigned long groups)
119{
120 int ret;
121 struct sockaddr_nl snl;
122 int sock;
123 int namelen;
ajs4be019d2005-01-29 16:12:41 +0000124 int save_errno;
paul718e3742002-12-13 20:15:29 +0000125
126 sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
127 if (sock < 0)
128 {
129 zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000130 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000131 return -1;
132 }
133
134 ret = fcntl (sock, F_SETFL, O_NONBLOCK);
135 if (ret < 0)
136 {
137 zlog (NULL, LOG_ERR, "Can't set %s socket flags: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000138 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000139 close (sock);
140 return -1;
141 }
paul7021c422003-07-15 12:52:22 +0000142
hassoc34b6b52004-08-31 13:41:49 +0000143 /* Set receive buffer size if it's set from command line */
144 if (nl_rcvbufsize)
145 {
146 u_int32_t oldsize, oldlen;
147 u_int32_t newsize, newlen;
148
149 oldlen = sizeof(oldsize);
150 newlen = sizeof(newsize);
151
152 ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
153 if (ret < 0)
154 {
155 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000156 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000157 close (sock);
158 return -1;
159 }
160
161 ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize,
162 sizeof(nl_rcvbufsize));
163 if (ret < 0)
164 {
165 zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000166 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000167 close (sock);
168 return -1;
169 }
170
171 ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
172 if (ret < 0)
173 {
174 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000175 safe_strerror (errno));
hassoc34b6b52004-08-31 13:41:49 +0000176 close (sock);
177 return -1;
178 }
179
180 zlog (NULL, LOG_INFO,
181 "Setting netlink socket receive buffer size: %u -> %u",
182 oldsize, newsize);
183 }
184
paul718e3742002-12-13 20:15:29 +0000185 memset (&snl, 0, sizeof snl);
186 snl.nl_family = AF_NETLINK;
187 snl.nl_groups = groups;
188
189 /* Bind the socket to the netlink structure for anything. */
paul7021c422003-07-15 12:52:22 +0000190 if (zserv_privs.change (ZPRIVS_RAISE))
191 {
192 zlog (NULL, LOG_ERR, "Can't raise privileges");
193 return -1;
194 }
pauledd7c242003-06-04 13:59:38 +0000195
paul718e3742002-12-13 20:15:29 +0000196 ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000197 save_errno = errno;
hasso55e7ecd2004-08-06 08:41:56 +0000198 if (zserv_privs.change (ZPRIVS_LOWER))
199 zlog (NULL, LOG_ERR, "Can't lower privileges");
200
paul718e3742002-12-13 20:15:29 +0000201 if (ret < 0)
202 {
paul7021c422003-07-15 12:52:22 +0000203 zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
ajs4be019d2005-01-29 16:12:41 +0000204 nl->name, snl.nl_groups, safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000205 close (sock);
206 return -1;
207 }
paul7021c422003-07-15 12:52:22 +0000208
paul718e3742002-12-13 20:15:29 +0000209 /* multiple netlink sockets will have different nl_pid */
210 namelen = sizeof snl;
hassoc9e52be2004-09-26 16:09:34 +0000211 ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);
paul718e3742002-12-13 20:15:29 +0000212 if (ret < 0 || namelen != sizeof snl)
213 {
214 zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000215 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000216 close (sock);
217 return -1;
218 }
219
220 nl->snl = snl;
221 nl->sock = sock;
222 return ret;
223}
224
paul7021c422003-07-15 12:52:22 +0000225int
226set_netlink_blocking (struct nlsock *nl, int *flags)
paul5f37d862003-04-19 00:11:28 +0000227{
228
229 /* Change socket flags for blocking I/O. */
paul7021c422003-07-15 12:52:22 +0000230 if ((*flags = fcntl (nl->sock, F_GETFL, 0)) < 0)
paul5f37d862003-04-19 00:11:28 +0000231 {
paul7021c422003-07-15 12:52:22 +0000232 zlog (NULL, LOG_ERR, "%s:%i F_GETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000233 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000234 return -1;
235 }
236 *flags &= ~O_NONBLOCK;
paul7021c422003-07-15 12:52:22 +0000237 if (fcntl (nl->sock, F_SETFL, *flags) < 0)
paul5f37d862003-04-19 00:11:28 +0000238 {
paul7021c422003-07-15 12:52:22 +0000239 zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000240 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000241 return -1;
242 }
243 return 0;
244}
245
paul7021c422003-07-15 12:52:22 +0000246int
247set_netlink_nonblocking (struct nlsock *nl, int *flags)
248{
paul5f37d862003-04-19 00:11:28 +0000249 /* Restore socket flags for nonblocking I/O */
250 *flags |= O_NONBLOCK;
paul7021c422003-07-15 12:52:22 +0000251 if (fcntl (nl->sock, F_SETFL, *flags) < 0)
paul5f37d862003-04-19 00:11:28 +0000252 {
paul7021c422003-07-15 12:52:22 +0000253 zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s",
ajs6099b3b2004-11-20 02:06:59 +0000254 __FUNCTION__, __LINE__, safe_strerror (errno));
paul5f37d862003-04-19 00:11:28 +0000255 return -1;
256 }
257 return 0;
258}
259
paul718e3742002-12-13 20:15:29 +0000260/* Get type specified information from netlink. */
261static int
262netlink_request (int family, int type, struct nlsock *nl)
263{
264 int ret;
265 struct sockaddr_nl snl;
ajs4be019d2005-01-29 16:12:41 +0000266 int save_errno;
paul718e3742002-12-13 20:15:29 +0000267
268 struct
269 {
270 struct nlmsghdr nlh;
271 struct rtgenmsg g;
272 } req;
273
274
275 /* Check netlink socket. */
276 if (nl->sock < 0)
277 {
278 zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
279 return -1;
280 }
281
282 memset (&snl, 0, sizeof snl);
283 snl.nl_family = AF_NETLINK;
284
ajsc05612b2005-10-01 16:36:54 +0000285 memset (&req, 0, sizeof req);
paul718e3742002-12-13 20:15:29 +0000286 req.nlh.nlmsg_len = sizeof req;
287 req.nlh.nlmsg_type = type;
288 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
289 req.nlh.nlmsg_pid = 0;
290 req.nlh.nlmsg_seq = ++nl->seq;
291 req.g.rtgen_family = family;
pauledd7c242003-06-04 13:59:38 +0000292
293 /* linux appears to check capabilities on every message
294 * have to raise caps for every message sent
295 */
paul7021c422003-07-15 12:52:22 +0000296 if (zserv_privs.change (ZPRIVS_RAISE))
pauledd7c242003-06-04 13:59:38 +0000297 {
298 zlog (NULL, LOG_ERR, "Can't raise privileges");
299 return -1;
300 }
paul7021c422003-07-15 12:52:22 +0000301
302 ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
303 (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000304 save_errno = errno;
paul7021c422003-07-15 12:52:22 +0000305
306 if (zserv_privs.change (ZPRIVS_LOWER))
307 zlog (NULL, LOG_ERR, "Can't lower privileges");
308
paul718e3742002-12-13 20:15:29 +0000309 if (ret < 0)
paul7021c422003-07-15 12:52:22 +0000310 {
311 zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
ajs4be019d2005-01-29 16:12:41 +0000312 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000313 return -1;
314 }
pauledd7c242003-06-04 13:59:38 +0000315
paul718e3742002-12-13 20:15:29 +0000316 return 0;
317}
318
319/* Receive message from netlink interface and pass those information
320 to the given function. */
321static int
322netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *),
paul7021c422003-07-15 12:52:22 +0000323 struct nlsock *nl)
paul718e3742002-12-13 20:15:29 +0000324{
325 int status;
326 int ret = 0;
327 int error;
328
329 while (1)
330 {
331 char buf[4096];
332 struct iovec iov = { buf, sizeof buf };
333 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +0000334 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +0000335 struct nlmsghdr *h;
ajs4be019d2005-01-29 16:12:41 +0000336 int save_errno;
paul718e3742002-12-13 20:15:29 +0000337
paul7021c422003-07-15 12:52:22 +0000338 if (zserv_privs.change (ZPRIVS_RAISE))
pauledd7c242003-06-04 13:59:38 +0000339 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul7021c422003-07-15 12:52:22 +0000340
paul718e3742002-12-13 20:15:29 +0000341 status = recvmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +0000342 save_errno = errno;
paul7021c422003-07-15 12:52:22 +0000343
344 if (zserv_privs.change (ZPRIVS_LOWER))
pauledd7c242003-06-04 13:59:38 +0000345 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000346
347 if (status < 0)
paul7021c422003-07-15 12:52:22 +0000348 {
ajs4be019d2005-01-29 16:12:41 +0000349 if (save_errno == EINTR)
paul7021c422003-07-15 12:52:22 +0000350 continue;
ajs4be019d2005-01-29 16:12:41 +0000351 if (save_errno == EWOULDBLOCK || save_errno == EAGAIN)
paul7021c422003-07-15 12:52:22 +0000352 break;
ajs4be019d2005-01-29 16:12:41 +0000353 zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s",
354 nl->name, safe_strerror(save_errno));
paul7021c422003-07-15 12:52:22 +0000355 continue;
356 }
paul718e3742002-12-13 20:15:29 +0000357
358 if (status == 0)
paul7021c422003-07-15 12:52:22 +0000359 {
360 zlog (NULL, LOG_ERR, "%s EOF", nl->name);
361 return -1;
362 }
paul718e3742002-12-13 20:15:29 +0000363
364 if (msg.msg_namelen != sizeof snl)
paul7021c422003-07-15 12:52:22 +0000365 {
366 zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
367 nl->name, msg.msg_namelen);
368 return -1;
369 }
paulb84d3a12003-11-17 10:31:01 +0000370
371 /* JF: Ignore messages that aren't from the kernel */
372 if ( snl.nl_pid != 0 )
373 {
374 zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl.nl_pid );
375 continue;
376 }
paul718e3742002-12-13 20:15:29 +0000377
hasso206d8052005-04-09 16:38:51 +0000378 for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, (unsigned int) status);
paul7021c422003-07-15 12:52:22 +0000379 h = NLMSG_NEXT (h, status))
380 {
381 /* Finish of reading. */
382 if (h->nlmsg_type == NLMSG_DONE)
383 return ret;
paul718e3742002-12-13 20:15:29 +0000384
paul7021c422003-07-15 12:52:22 +0000385 /* Error handling. */
386 if (h->nlmsg_type == NLMSG_ERROR)
387 {
388 struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
389
paul718e3742002-12-13 20:15:29 +0000390 /* If the error field is zero, then this is an ACK */
paul7021c422003-07-15 12:52:22 +0000391 if (err->error == 0)
paul718e3742002-12-13 20:15:29 +0000392 {
paul7021c422003-07-15 12:52:22 +0000393 if (IS_ZEBRA_DEBUG_KERNEL)
394 {
hasso1ada8192005-06-12 11:28:18 +0000395 zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
paul7021c422003-07-15 12:52:22 +0000396 __FUNCTION__, nl->name,
397 lookup (nlmsg_str, err->msg.nlmsg_type),
398 err->msg.nlmsg_type, err->msg.nlmsg_seq,
399 err->msg.nlmsg_pid);
paul718e3742002-12-13 20:15:29 +0000400 }
paul7021c422003-07-15 12:52:22 +0000401
402 /* return if not a multipart message, otherwise continue */
403 if (!(h->nlmsg_flags & NLM_F_MULTI))
404 {
405 return 0;
paul718e3742002-12-13 20:15:29 +0000406 }
paul7021c422003-07-15 12:52:22 +0000407 continue;
paul718e3742002-12-13 20:15:29 +0000408 }
paul7021c422003-07-15 12:52:22 +0000409
paul718e3742002-12-13 20:15:29 +0000410 if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
paul7021c422003-07-15 12:52:22 +0000411 {
412 zlog (NULL, LOG_ERR, "%s error: message truncated",
413 nl->name);
414 return -1;
415 }
pauld753e9e2003-01-22 19:45:50 +0000416
paul7021c422003-07-15 12:52:22 +0000417 /* Deal with Error Noise - MAG */
418 {
419 int loglvl = LOG_ERR;
420 int errnum = err->error;
421 int msg_type = err->msg.nlmsg_type;
paul718e3742002-12-13 20:15:29 +0000422
paul7021c422003-07-15 12:52:22 +0000423 if (nl == &netlink_cmd
424 && (-errnum == ENODEV || -errnum == ESRCH)
425 && (msg_type == RTM_NEWROUTE || msg_type == RTM_DELROUTE))
426 loglvl = LOG_DEBUG;
paul718e3742002-12-13 20:15:29 +0000427
paul7021c422003-07-15 12:52:22 +0000428 zlog (NULL, loglvl, "%s error: %s, type=%s(%u), "
hasso1ada8192005-06-12 11:28:18 +0000429 "seq=%u, pid=%u",
ajs6099b3b2004-11-20 02:06:59 +0000430 nl->name, safe_strerror (-errnum),
paul7021c422003-07-15 12:52:22 +0000431 lookup (nlmsg_str, msg_type),
432 msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
433 }
434 /*
435 ret = -1;
436 continue;
437 */
438 return -1;
439 }
paul718e3742002-12-13 20:15:29 +0000440
paul7021c422003-07-15 12:52:22 +0000441 /* OK we got netlink message. */
442 if (IS_ZEBRA_DEBUG_KERNEL)
hasso1ada8192005-06-12 11:28:18 +0000443 zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u",
paul7021c422003-07-15 12:52:22 +0000444 nl->name,
445 lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
446 h->nlmsg_seq, h->nlmsg_pid);
447
448 /* skip unsolicited messages originating from command socket */
449 if (nl != &netlink_cmd && h->nlmsg_pid == netlink_cmd.snl.nl_pid)
450 {
451 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000452 zlog_debug ("netlink_parse_info: %s packet comes from %s",
hasso1ada8192005-06-12 11:28:18 +0000453 netlink_cmd.name, nl->name);
paul7021c422003-07-15 12:52:22 +0000454 continue;
455 }
456
457 error = (*filter) (&snl, h);
458 if (error < 0)
459 {
460 zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
461 ret = error;
462 }
463 }
paul718e3742002-12-13 20:15:29 +0000464
465 /* After error care. */
466 if (msg.msg_flags & MSG_TRUNC)
paul7021c422003-07-15 12:52:22 +0000467 {
468 zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name);
469 continue;
470 }
paul718e3742002-12-13 20:15:29 +0000471 if (status)
paul7021c422003-07-15 12:52:22 +0000472 {
473 zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
474 status);
475 return -1;
476 }
paul718e3742002-12-13 20:15:29 +0000477 }
478 return ret;
479}
480
481/* Utility function for parse rtattr. */
482static void
paul7021c422003-07-15 12:52:22 +0000483netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
484 int len)
paul718e3742002-12-13 20:15:29 +0000485{
paul7021c422003-07-15 12:52:22 +0000486 while (RTA_OK (rta, len))
paul718e3742002-12-13 20:15:29 +0000487 {
488 if (rta->rta_type <= max)
paul7021c422003-07-15 12:52:22 +0000489 tb[rta->rta_type] = rta;
490 rta = RTA_NEXT (rta, len);
paul718e3742002-12-13 20:15:29 +0000491 }
492}
493
494/* Called from interface_lookup_netlink(). This function is only used
495 during bootstrap. */
496int
497netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h)
498{
499 int len;
500 struct ifinfomsg *ifi;
501 struct rtattr *tb[IFLA_MAX + 1];
502 struct interface *ifp;
503 char *name;
504 int i;
505
506 ifi = NLMSG_DATA (h);
507
508 if (h->nlmsg_type != RTM_NEWLINK)
509 return 0;
510
511 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
512 if (len < 0)
513 return -1;
514
515 /* Looking up interface name. */
516 memset (tb, 0, sizeof tb);
517 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000518
paul1e193152005-02-14 23:53:05 +0000519#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000520 /* check for wireless messages to ignore */
521 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
522 {
523 if (IS_ZEBRA_DEBUG_KERNEL)
524 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
525 return 0;
526 }
paul1e193152005-02-14 23:53:05 +0000527#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +0000528
paul718e3742002-12-13 20:15:29 +0000529 if (tb[IFLA_IFNAME] == NULL)
530 return -1;
paul7021c422003-07-15 12:52:22 +0000531 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +0000532
533 /* Add interface. */
534 ifp = if_get_by_name (name);
ajsd2fc8892005-04-02 18:38:43 +0000535 set_ifindex(ifp, ifi->ifi_index);
paul718e3742002-12-13 20:15:29 +0000536 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +0000537 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul718e3742002-12-13 20:15:29 +0000538 ifp->metric = 1;
539
540 /* Hardware type and address. */
541 ifp->hw_type = ifi->ifi_type;
542
543 if (tb[IFLA_ADDRESS])
544 {
545 int hw_addr_len;
546
paul7021c422003-07-15 12:52:22 +0000547 hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
paul718e3742002-12-13 20:15:29 +0000548
549 if (hw_addr_len > INTERFACE_HWADDR_MAX)
paul7021c422003-07-15 12:52:22 +0000550 zlog_warn ("Hardware address is too large: %d", hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000551 else
paul7021c422003-07-15 12:52:22 +0000552 {
553 ifp->hw_addr_len = hw_addr_len;
554 memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000555
paul7021c422003-07-15 12:52:22 +0000556 for (i = 0; i < hw_addr_len; i++)
557 if (ifp->hw_addr[i] != 0)
558 break;
paul718e3742002-12-13 20:15:29 +0000559
paul7021c422003-07-15 12:52:22 +0000560 if (i == hw_addr_len)
561 ifp->hw_addr_len = 0;
562 else
563 ifp->hw_addr_len = hw_addr_len;
564 }
paul718e3742002-12-13 20:15:29 +0000565 }
566
567 if_add_update (ifp);
568
569 return 0;
570}
571
572/* Lookup interface IPv4/IPv6 address. */
573int
574netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h)
575{
576 int len;
577 struct ifaddrmsg *ifa;
paul7021c422003-07-15 12:52:22 +0000578 struct rtattr *tb[IFA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000579 struct interface *ifp;
580 void *addr = NULL;
581 void *broad = NULL;
582 u_char flags = 0;
583 char *label = NULL;
584
585 ifa = NLMSG_DATA (h);
586
paul7021c422003-07-15 12:52:22 +0000587 if (ifa->ifa_family != AF_INET
paul718e3742002-12-13 20:15:29 +0000588#ifdef HAVE_IPV6
589 && ifa->ifa_family != AF_INET6
590#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +0000591 )
paul718e3742002-12-13 20:15:29 +0000592 return 0;
593
594 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
595 return 0;
596
paul7021c422003-07-15 12:52:22 +0000597 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +0000598 if (len < 0)
599 return -1;
600
601 memset (tb, 0, sizeof tb);
602 netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
603
604 ifp = if_lookup_by_index (ifa->ifa_index);
605 if (ifp == NULL)
606 {
607 zlog_err ("netlink_interface_addr can't find interface by index %d",
paul7021c422003-07-15 12:52:22 +0000608 ifa->ifa_index);
paul718e3742002-12-13 20:15:29 +0000609 return -1;
610 }
611
paul7021c422003-07-15 12:52:22 +0000612 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
paul718e3742002-12-13 20:15:29 +0000613 {
paul00df0c12002-12-13 21:07:36 +0000614 char buf[BUFSIZ];
hasso206d8052005-04-09 16:38:51 +0000615 zlog_debug ("netlink_interface_addr %s %s:",
616 lookup (nlmsg_str, h->nlmsg_type), ifp->name);
paul718e3742002-12-13 20:15:29 +0000617 if (tb[IFA_LOCAL])
hasso206d8052005-04-09 16:38:51 +0000618 zlog_debug (" IFA_LOCAL %s/%d",
619 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]),
620 buf, BUFSIZ), ifa->ifa_prefixlen);
paul718e3742002-12-13 20:15:29 +0000621 if (tb[IFA_ADDRESS])
hasso206d8052005-04-09 16:38:51 +0000622 zlog_debug (" IFA_ADDRESS %s/%d",
623 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]),
624 buf, BUFSIZ), ifa->ifa_prefixlen);
paul718e3742002-12-13 20:15:29 +0000625 if (tb[IFA_BROADCAST])
hasso206d8052005-04-09 16:38:51 +0000626 zlog_debug (" IFA_BROADCAST %s/%d",
627 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]),
628 buf, BUFSIZ), ifa->ifa_prefixlen);
paul00df0c12002-12-13 21:07:36 +0000629 if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
ajsb6178002004-12-07 21:12:56 +0000630 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
pauld34b8992006-01-17 18:03:04 +0000631
632 if (tb[IFA_CACHEINFO])
633 {
634 struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]);
635 zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
636 ci->ifa_prefered, ci->ifa_valid);
637 }
paul718e3742002-12-13 20:15:29 +0000638 }
paul31a476c2003-09-29 19:54:53 +0000639
640 if (tb[IFA_ADDRESS] == NULL)
641 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
642
643 if (ifp->flags & IFF_POINTOPOINT)
paul7021c422003-07-15 12:52:22 +0000644 {
paul31a476c2003-09-29 19:54:53 +0000645 if (tb[IFA_LOCAL])
646 {
647 addr = RTA_DATA (tb[IFA_LOCAL]);
hasso3fb9cd62004-10-19 19:44:43 +0000648 if (tb[IFA_ADDRESS] &&
649 memcmp(RTA_DATA(tb[IFA_ADDRESS]),RTA_DATA(tb[IFA_LOCAL]),4))
650 /* if IFA_ADDRESS != IFA_LOCAL, then it's the peer address */
paul31a476c2003-09-29 19:54:53 +0000651 broad = RTA_DATA (tb[IFA_ADDRESS]);
652 else
653 broad = NULL;
654 }
655 else
656 {
657 if (tb[IFA_ADDRESS])
658 addr = RTA_DATA (tb[IFA_ADDRESS]);
659 else
660 addr = NULL;
661 }
paul7021c422003-07-15 12:52:22 +0000662 }
paul31a476c2003-09-29 19:54:53 +0000663 else
paul7021c422003-07-15 12:52:22 +0000664 {
paul31a476c2003-09-29 19:54:53 +0000665 if (tb[IFA_ADDRESS])
666 addr = RTA_DATA (tb[IFA_ADDRESS]);
667 else
668 addr = NULL;
669
670 if (tb[IFA_BROADCAST])
671 broad = RTA_DATA(tb[IFA_BROADCAST]);
672 else
673 broad = NULL;
paul7021c422003-07-15 12:52:22 +0000674 }
paul00df0c12002-12-13 21:07:36 +0000675
Paul Jakma27b47252006-07-02 16:38:54 +0000676 /* addr is primary key, SOL if we don't have one */
677 if (addr == NULL)
678 {
679 zlog_debug ("%s: NULL address", __func__);
680 return -1;
681 }
682
paul718e3742002-12-13 20:15:29 +0000683 /* Flags. */
684 if (ifa->ifa_flags & IFA_F_SECONDARY)
685 SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
686
687 /* Label */
688 if (tb[IFA_LABEL])
689 label = (char *) RTA_DATA (tb[IFA_LABEL]);
690
691 if (ifp && label && strcmp (ifp->name, label) == 0)
692 label = NULL;
693
694 /* Register interface address to the interface. */
695 if (ifa->ifa_family == AF_INET)
696 {
paul7021c422003-07-15 12:52:22 +0000697 if (h->nlmsg_type == RTM_NEWADDR)
698 connected_add_ipv4 (ifp, flags,
699 (struct in_addr *) addr, ifa->ifa_prefixlen,
700 (struct in_addr *) broad, label);
701 else
702 connected_delete_ipv4 (ifp, flags,
703 (struct in_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000704 (struct in_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000705 }
706#ifdef HAVE_IPV6
707 if (ifa->ifa_family == AF_INET6)
708 {
709 if (h->nlmsg_type == RTM_NEWADDR)
paul7021c422003-07-15 12:52:22 +0000710 connected_add_ipv6 (ifp,
711 (struct in6_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000712 (struct in6_addr *) broad, label);
paul718e3742002-12-13 20:15:29 +0000713 else
paul7021c422003-07-15 12:52:22 +0000714 connected_delete_ipv6 (ifp,
715 (struct in6_addr *) addr, ifa->ifa_prefixlen,
716 (struct in6_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000717 }
paul7021c422003-07-15 12:52:22 +0000718#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000719
720 return 0;
721}
722
723/* Looking up routing table by netlink interface. */
724int
725netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h)
726{
727 int len;
728 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000729 struct rtattr *tb[RTA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000730 u_char flags = 0;
paul7021c422003-07-15 12:52:22 +0000731
732 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000733
734 int index;
735 int table;
hasso34195bf2004-04-06 12:07:06 +0000736 int metric;
737
paul718e3742002-12-13 20:15:29 +0000738 void *dest;
739 void *gate;
740
741 rtm = NLMSG_DATA (h);
742
743 if (h->nlmsg_type != RTM_NEWROUTE)
744 return 0;
745 if (rtm->rtm_type != RTN_UNICAST)
746 return 0;
747
748 table = rtm->rtm_table;
paul7021c422003-07-15 12:52:22 +0000749#if 0 /* we weed them out later in rib_weed_tables () */
paulb21b19c2003-06-15 01:28:29 +0000750 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000751 return 0;
752#endif
753
paul7021c422003-07-15 12:52:22 +0000754 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000755 if (len < 0)
756 return -1;
757
758 memset (tb, 0, sizeof tb);
759 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
760
761 if (rtm->rtm_flags & RTM_F_CLONED)
762 return 0;
763 if (rtm->rtm_protocol == RTPROT_REDIRECT)
764 return 0;
765 if (rtm->rtm_protocol == RTPROT_KERNEL)
766 return 0;
767
768 if (rtm->rtm_src_len != 0)
769 return 0;
770
771 /* Route which inserted by Zebra. */
772 if (rtm->rtm_protocol == RTPROT_ZEBRA)
773 flags |= ZEBRA_FLAG_SELFROUTE;
paul7021c422003-07-15 12:52:22 +0000774
paul718e3742002-12-13 20:15:29 +0000775 index = 0;
hasso34195bf2004-04-06 12:07:06 +0000776 metric = 0;
paul718e3742002-12-13 20:15:29 +0000777 dest = NULL;
778 gate = NULL;
779
780 if (tb[RTA_OIF])
781 index = *(int *) RTA_DATA (tb[RTA_OIF]);
782
783 if (tb[RTA_DST])
784 dest = RTA_DATA (tb[RTA_DST]);
785 else
786 dest = anyaddr;
787
788 /* Multipath treatment is needed. */
789 if (tb[RTA_GATEWAY])
790 gate = RTA_DATA (tb[RTA_GATEWAY]);
791
hasso34195bf2004-04-06 12:07:06 +0000792 if (tb[RTA_PRIORITY])
793 metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
794
paul718e3742002-12-13 20:15:29 +0000795 if (rtm->rtm_family == AF_INET)
796 {
797 struct prefix_ipv4 p;
798 p.family = AF_INET;
799 memcpy (&p.prefix, dest, 4);
800 p.prefixlen = rtm->rtm_dst_len;
801
hasso34195bf2004-04-06 12:07:06 +0000802 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table, metric, 0);
paul718e3742002-12-13 20:15:29 +0000803 }
804#ifdef HAVE_IPV6
805 if (rtm->rtm_family == AF_INET6)
806 {
807 struct prefix_ipv6 p;
808 p.family = AF_INET6;
809 memcpy (&p.prefix, dest, 16);
810 p.prefixlen = rtm->rtm_dst_len;
811
hassobe61c4e2005-08-27 06:05:47 +0000812 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table,
813 metric, 0);
paul718e3742002-12-13 20:15:29 +0000814 }
815#endif /* HAVE_IPV6 */
816
817 return 0;
818}
819
paul7021c422003-07-15 12:52:22 +0000820struct message rtproto_str[] = {
paul718e3742002-12-13 20:15:29 +0000821 {RTPROT_REDIRECT, "redirect"},
822 {RTPROT_KERNEL, "kernel"},
823 {RTPROT_BOOT, "boot"},
824 {RTPROT_STATIC, "static"},
825 {RTPROT_GATED, "GateD"},
826 {RTPROT_RA, "router advertisement"},
827 {RTPROT_MRT, "MRT"},
828 {RTPROT_ZEBRA, "Zebra"},
829#ifdef RTPROT_BIRD
830 {RTPROT_BIRD, "BIRD"},
831#endif /* RTPROT_BIRD */
832 {0, NULL}
833};
834
835/* Routing information change from the kernel. */
836int
837netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
838{
839 int len;
840 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000841 struct rtattr *tb[RTA_MAX + 1];
842
843 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000844
845 int index;
846 int table;
847 void *dest;
848 void *gate;
849
850 rtm = NLMSG_DATA (h);
851
paul7021c422003-07-15 12:52:22 +0000852 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
paul718e3742002-12-13 20:15:29 +0000853 {
854 /* If this is not route add/delete message print warning. */
855 zlog_warn ("Kernel message: %d\n", h->nlmsg_type);
856 return 0;
857 }
858
859 /* Connected route. */
860 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000861 zlog_debug ("%s %s %s proto %s",
paul7021c422003-07-15 12:52:22 +0000862 h->nlmsg_type ==
863 RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
864 rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
865 rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
866 lookup (rtproto_str, rtm->rtm_protocol));
paul718e3742002-12-13 20:15:29 +0000867
868 if (rtm->rtm_type != RTN_UNICAST)
869 {
870 return 0;
871 }
872
873 table = rtm->rtm_table;
paulb21b19c2003-06-15 01:28:29 +0000874 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000875 {
876 return 0;
877 }
878
paul7021c422003-07-15 12:52:22 +0000879 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000880 if (len < 0)
881 return -1;
882
883 memset (tb, 0, sizeof tb);
884 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
885
886 if (rtm->rtm_flags & RTM_F_CLONED)
887 return 0;
888 if (rtm->rtm_protocol == RTPROT_REDIRECT)
889 return 0;
890 if (rtm->rtm_protocol == RTPROT_KERNEL)
891 return 0;
892
893 if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
894 return 0;
895
896 if (rtm->rtm_src_len != 0)
897 {
898 zlog_warn ("netlink_route_change(): no src len");
899 return 0;
900 }
paul7021c422003-07-15 12:52:22 +0000901
paul718e3742002-12-13 20:15:29 +0000902 index = 0;
903 dest = NULL;
904 gate = NULL;
905
906 if (tb[RTA_OIF])
907 index = *(int *) RTA_DATA (tb[RTA_OIF]);
908
909 if (tb[RTA_DST])
910 dest = RTA_DATA (tb[RTA_DST]);
911 else
912 dest = anyaddr;
913
914 if (tb[RTA_GATEWAY])
915 gate = RTA_DATA (tb[RTA_GATEWAY]);
916
917 if (rtm->rtm_family == AF_INET)
918 {
919 struct prefix_ipv4 p;
920 p.family = AF_INET;
921 memcpy (&p.prefix, dest, 4);
922 p.prefixlen = rtm->rtm_dst_len;
923
924 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000925 {
926 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000927 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000928 inet_ntoa (p.prefix), p.prefixlen);
929 else
ajsb6178002004-12-07 21:12:56 +0000930 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000931 inet_ntoa (p.prefix), p.prefixlen);
932 }
paul718e3742002-12-13 20:15:29 +0000933
934 if (h->nlmsg_type == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +0000935 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0);
paul718e3742002-12-13 20:15:29 +0000936 else
paul7021c422003-07-15 12:52:22 +0000937 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
paul718e3742002-12-13 20:15:29 +0000938 }
939
940#ifdef HAVE_IPV6
941 if (rtm->rtm_family == AF_INET6)
942 {
943 struct prefix_ipv6 p;
944 char buf[BUFSIZ];
945
946 p.family = AF_INET6;
947 memcpy (&p.prefix, dest, 16);
948 p.prefixlen = rtm->rtm_dst_len;
949
950 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000951 {
952 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000953 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000954 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
955 p.prefixlen);
956 else
ajsb6178002004-12-07 21:12:56 +0000957 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000958 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
959 p.prefixlen);
960 }
paul718e3742002-12-13 20:15:29 +0000961
962 if (h->nlmsg_type == RTM_NEWROUTE)
hassobe61c4e2005-08-27 06:05:47 +0000963 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0, 0, 0);
paul718e3742002-12-13 20:15:29 +0000964 else
paul7021c422003-07-15 12:52:22 +0000965 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
paul718e3742002-12-13 20:15:29 +0000966 }
967#endif /* HAVE_IPV6 */
968
969 return 0;
970}
971
972int
973netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
974{
975 int len;
976 struct ifinfomsg *ifi;
paul7021c422003-07-15 12:52:22 +0000977 struct rtattr *tb[IFLA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000978 struct interface *ifp;
979 char *name;
980
981 ifi = NLMSG_DATA (h);
982
paul7021c422003-07-15 12:52:22 +0000983 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
paul718e3742002-12-13 20:15:29 +0000984 {
985 /* If this is not link add/delete message so print warning. */
986 zlog_warn ("netlink_link_change: wrong kernel message %d\n",
paul7021c422003-07-15 12:52:22 +0000987 h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +0000988 return 0;
989 }
990
991 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
992 if (len < 0)
993 return -1;
994
995 /* Looking up interface name. */
996 memset (tb, 0, sizeof tb);
997 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000998
paul1e193152005-02-14 23:53:05 +0000999#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +00001000 /* check for wireless messages to ignore */
1001 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
1002 {
1003 if (IS_ZEBRA_DEBUG_KERNEL)
1004 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
1005 return 0;
1006 }
paul1e193152005-02-14 23:53:05 +00001007#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +00001008
paul718e3742002-12-13 20:15:29 +00001009 if (tb[IFLA_IFNAME] == NULL)
1010 return -1;
paul7021c422003-07-15 12:52:22 +00001011 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +00001012
1013 /* Add interface. */
1014 if (h->nlmsg_type == RTM_NEWLINK)
1015 {
1016 ifp = if_lookup_by_name (name);
1017
paul7021c422003-07-15 12:52:22 +00001018 if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1019 {
1020 if (ifp == NULL)
1021 ifp = if_get_by_name (name);
paul718e3742002-12-13 20:15:29 +00001022
ajsd2fc8892005-04-02 18:38:43 +00001023 set_ifindex(ifp, ifi->ifi_index);
paul7021c422003-07-15 12:52:22 +00001024 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +00001025 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +00001026 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +00001027
paul7021c422003-07-15 12:52:22 +00001028 /* If new link is added. */
1029 if_add_update (ifp);
1030 }
paul718e3742002-12-13 20:15:29 +00001031 else
paul7021c422003-07-15 12:52:22 +00001032 {
1033 /* Interface status change. */
ajsd2fc8892005-04-02 18:38:43 +00001034 set_ifindex(ifp, ifi->ifi_index);
paul44145db2004-05-09 11:00:23 +00001035 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +00001036 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +00001037
paul7021c422003-07-15 12:52:22 +00001038 if (if_is_operative (ifp))
1039 {
1040 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1041 if (!if_is_operative (ifp))
1042 if_down (ifp);
ajsa608bbf2005-03-29 17:03:49 +00001043 else
1044 /* Must notify client daemons of new interface status. */
1045 zebra_interface_up_update (ifp);
paul7021c422003-07-15 12:52:22 +00001046 }
1047 else
1048 {
1049 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1050 if (if_is_operative (ifp))
1051 if_up (ifp);
1052 }
1053 }
paul718e3742002-12-13 20:15:29 +00001054 }
1055 else
1056 {
1057 /* RTM_DELLINK. */
1058 ifp = if_lookup_by_name (name);
1059
1060 if (ifp == NULL)
paul7021c422003-07-15 12:52:22 +00001061 {
1062 zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find",
paul718e3742002-12-13 20:15:29 +00001063 name);
paul7021c422003-07-15 12:52:22 +00001064 return 0;
1065 }
1066
paul718e3742002-12-13 20:15:29 +00001067 if_delete_update (ifp);
1068 }
1069
1070 return 0;
1071}
1072
1073int
1074netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h)
1075{
1076 switch (h->nlmsg_type)
1077 {
1078 case RTM_NEWROUTE:
1079 return netlink_route_change (snl, h);
1080 break;
1081 case RTM_DELROUTE:
1082 return netlink_route_change (snl, h);
1083 break;
1084 case RTM_NEWLINK:
1085 return netlink_link_change (snl, h);
1086 break;
1087 case RTM_DELLINK:
1088 return netlink_link_change (snl, h);
1089 break;
1090 case RTM_NEWADDR:
1091 return netlink_interface_addr (snl, h);
1092 break;
1093 case RTM_DELADDR:
1094 return netlink_interface_addr (snl, h);
1095 break;
1096 default:
1097 zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type);
1098 break;
1099 }
1100 return 0;
1101}
1102
1103/* Interface lookup by netlink socket. */
1104int
paul6621ca82005-11-23 13:02:08 +00001105interface_lookup_netlink (void)
paul718e3742002-12-13 20:15:29 +00001106{
1107 int ret;
paul5f37d862003-04-19 00:11:28 +00001108 int flags;
1109 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001110
paul5f37d862003-04-19 00:11:28 +00001111 /*
1112 * Change netlink socket flags to blocking to ensure we get
1113 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001114 */
1115 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1116 if (snb_ret < 0)
1117 zlog (NULL, LOG_WARNING,
1118 "%s:%i Warning: Could not set netlink socket to blocking.",
1119 __FUNCTION__, __LINE__);
1120
paul718e3742002-12-13 20:15:29 +00001121 /* Get interface information. */
1122 ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd);
1123 if (ret < 0)
1124 return ret;
1125 ret = netlink_parse_info (netlink_interface, &netlink_cmd);
1126 if (ret < 0)
1127 return ret;
1128
1129 /* Get IPv4 address of the interfaces. */
1130 ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd);
1131 if (ret < 0)
1132 return ret;
1133 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1134 if (ret < 0)
1135 return ret;
1136
1137#ifdef HAVE_IPV6
1138 /* Get IPv6 address of the interfaces. */
1139 ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd);
1140 if (ret < 0)
1141 return ret;
1142 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1143 if (ret < 0)
1144 return ret;
1145#endif /* HAVE_IPV6 */
1146
paul7021c422003-07-15 12:52:22 +00001147 /* restore socket flags */
1148 if (snb_ret == 0)
1149 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001150 return 0;
1151}
1152
1153/* Routing table read function using netlink interface. Only called
1154 bootstrap time. */
1155int
paul6621ca82005-11-23 13:02:08 +00001156netlink_route_read (void)
paul718e3742002-12-13 20:15:29 +00001157{
1158 int ret;
paul5f37d862003-04-19 00:11:28 +00001159 int flags;
1160 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001161
paul5f37d862003-04-19 00:11:28 +00001162 /*
1163 * Change netlink socket flags to blocking to ensure we get
1164 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001165 */
1166 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1167 if (snb_ret < 0)
1168 zlog (NULL, LOG_WARNING,
1169 "%s:%i Warning: Could not set netlink socket to blocking.",
1170 __FUNCTION__, __LINE__);
1171
paul718e3742002-12-13 20:15:29 +00001172 /* Get IPv4 routing table. */
1173 ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd);
1174 if (ret < 0)
1175 return ret;
1176 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1177 if (ret < 0)
1178 return ret;
1179
1180#ifdef HAVE_IPV6
1181 /* Get IPv6 routing table. */
1182 ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd);
1183 if (ret < 0)
1184 return ret;
1185 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1186 if (ret < 0)
1187 return ret;
1188#endif /* HAVE_IPV6 */
1189
paul5f37d862003-04-19 00:11:28 +00001190 /* restore flags */
paul7021c422003-07-15 12:52:22 +00001191 if (snb_ret == 0)
1192 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001193 return 0;
1194}
1195
1196/* Utility function comes from iproute2.
1197 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1198int
1199addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
1200{
1201 int len;
1202 struct rtattr *rta;
1203
paul7021c422003-07-15 12:52:22 +00001204 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001205
paul7021c422003-07-15 12:52:22 +00001206 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001207 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, alen);
paul718e3742002-12-13 20:15:29 +00001213 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1214
1215 return 0;
1216}
1217
1218int
1219rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
1220{
1221 int len;
1222 struct rtattr *subrta;
1223
paul7021c422003-07-15 12:52:22 +00001224 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001225
paul7021c422003-07-15 12:52:22 +00001226 if (RTA_ALIGN (rta->rta_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001227 return -1;
1228
paul7021c422003-07-15 12:52:22 +00001229 subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
paul718e3742002-12-13 20:15:29 +00001230 subrta->rta_type = type;
1231 subrta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001232 memcpy (RTA_DATA (subrta), data, alen);
paul718e3742002-12-13 20:15:29 +00001233 rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
1234
1235 return 0;
1236}
1237
1238/* Utility function comes from iproute2.
1239 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1240int
1241addattr32 (struct nlmsghdr *n, int maxlen, int type, int data)
1242{
1243 int len;
1244 struct rtattr *rta;
paul7021c422003-07-15 12:52:22 +00001245
1246 len = RTA_LENGTH (4);
1247
paul718e3742002-12-13 20:15:29 +00001248 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1249 return -1;
1250
paul7021c422003-07-15 12:52:22 +00001251 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001252 rta->rta_type = type;
1253 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001254 memcpy (RTA_DATA (rta), &data, 4);
paul718e3742002-12-13 20:15:29 +00001255 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1256
1257 return 0;
1258}
1259
1260static int
1261netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h)
1262{
hassob7ed1ec2005-03-31 20:13:49 +00001263 zlog_warn ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +00001264 return 0;
1265}
1266
1267/* sendmsg() to netlink socket then recvmsg(). */
1268int
1269netlink_talk (struct nlmsghdr *n, struct nlsock *nl)
1270{
1271 int status;
1272 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +00001273 struct iovec iov = { (void *) n, n->nlmsg_len };
1274 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +00001275 int flags = 0;
paul5f37d862003-04-19 00:11:28 +00001276 int snb_ret;
ajs4be019d2005-01-29 16:12:41 +00001277 int save_errno;
paul7021c422003-07-15 12:52:22 +00001278
paul718e3742002-12-13 20:15:29 +00001279 memset (&snl, 0, sizeof snl);
1280 snl.nl_family = AF_NETLINK;
paul7021c422003-07-15 12:52:22 +00001281
hassob7ed1ec2005-03-31 20:13:49 +00001282 n->nlmsg_seq = ++nl->seq;
paul718e3742002-12-13 20:15:29 +00001283
1284 /* Request an acknowledgement by setting NLM_F_ACK */
1285 n->nlmsg_flags |= NLM_F_ACK;
paul7021c422003-07-15 12:52:22 +00001286
1287 if (IS_ZEBRA_DEBUG_KERNEL)
hassob7ed1ec2005-03-31 20:13:49 +00001288 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name,
paul7021c422003-07-15 12:52:22 +00001289 lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
1290 n->nlmsg_seq);
paul718e3742002-12-13 20:15:29 +00001291
1292 /* Send message to netlink interface. */
paul7021c422003-07-15 12:52:22 +00001293 if (zserv_privs.change (ZPRIVS_RAISE))
1294 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +00001295 status = sendmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +00001296 save_errno = errno;
paul7021c422003-07-15 12:52:22 +00001297 if (zserv_privs.change (ZPRIVS_LOWER))
1298 zlog (NULL, LOG_ERR, "Can't lower privileges");
1299
paul718e3742002-12-13 20:15:29 +00001300 if (status < 0)
1301 {
1302 zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
ajs4be019d2005-01-29 16:12:41 +00001303 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +00001304 return -1;
1305 }
paul7021c422003-07-15 12:52:22 +00001306
paul718e3742002-12-13 20:15:29 +00001307 /*
1308 * Change socket flags for blocking I/O.
1309 * This ensures we wait for a reply in netlink_parse_info().
1310 */
paul7021c422003-07-15 12:52:22 +00001311 snb_ret = set_netlink_blocking (nl, &flags);
1312 if (snb_ret < 0)
1313 zlog (NULL, LOG_WARNING,
1314 "%s:%i Warning: Could not set netlink socket to blocking.",
1315 __FUNCTION__, __LINE__);
paul718e3742002-12-13 20:15:29 +00001316
1317 /*
1318 * Get reply from netlink socket.
1319 * The reply should either be an acknowlegement or an error.
1320 */
1321 status = netlink_parse_info (netlink_talk_filter, nl);
paul7021c422003-07-15 12:52:22 +00001322
paul718e3742002-12-13 20:15:29 +00001323 /* Restore socket flags for nonblocking I/O */
paul7021c422003-07-15 12:52:22 +00001324 if (snb_ret == 0)
1325 set_netlink_nonblocking (nl, &flags);
1326
paul718e3742002-12-13 20:15:29 +00001327 return status;
1328}
1329
1330/* Routing table change via netlink interface. */
1331int
1332netlink_route (int cmd, int family, void *dest, int length, void *gate,
paul7021c422003-07-15 12:52:22 +00001333 int index, int zebra_flags, int table)
paul718e3742002-12-13 20:15:29 +00001334{
1335 int ret;
1336 int bytelen;
1337 struct sockaddr_nl snl;
1338 int discard;
1339
paul7021c422003-07-15 12:52:22 +00001340 struct
paul718e3742002-12-13 20:15:29 +00001341 {
1342 struct nlmsghdr n;
1343 struct rtmsg r;
1344 char buf[1024];
1345 } req;
1346
1347 memset (&req, 0, sizeof req);
1348
1349 bytelen = (family == AF_INET ? 4 : 16);
1350
1351 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1352 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1353 req.n.nlmsg_type = cmd;
1354 req.r.rtm_family = family;
1355 req.r.rtm_table = table;
1356 req.r.rtm_dst_len = length;
1357
hasso81dfcaa2003-05-25 19:21:25 +00001358 if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1359 || (zebra_flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001360 discard = 1;
1361 else
1362 discard = 0;
1363
paul7021c422003-07-15 12:52:22 +00001364 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001365 {
1366 req.r.rtm_protocol = RTPROT_ZEBRA;
1367 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1368
paul7021c422003-07-15 12:52:22 +00001369 if (discard)
paul595db7f2003-05-25 21:35:06 +00001370 {
1371 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1372 req.r.rtm_type = RTN_BLACKHOLE;
1373 else if (zebra_flags & ZEBRA_FLAG_REJECT)
1374 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001375 else
1376 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1377 }
paul595db7f2003-05-25 21:35:06 +00001378 else
paul7021c422003-07-15 12:52:22 +00001379 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001380 }
1381
1382 if (dest)
1383 addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen);
1384
paul7021c422003-07-15 12:52:22 +00001385 if (!discard)
paul718e3742002-12-13 20:15:29 +00001386 {
1387 if (gate)
paul7021c422003-07-15 12:52:22 +00001388 addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen);
paul718e3742002-12-13 20:15:29 +00001389 if (index > 0)
paul7021c422003-07-15 12:52:22 +00001390 addattr32 (&req.n, sizeof req, RTA_OIF, index);
paul718e3742002-12-13 20:15:29 +00001391 }
1392
1393 /* Destination netlink address. */
1394 memset (&snl, 0, sizeof snl);
1395 snl.nl_family = AF_NETLINK;
1396
1397 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001398 ret = netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001399 if (ret < 0)
1400 return -1;
1401
1402 return 0;
1403}
1404
1405/* Routing table change via netlink interface. */
1406int
1407netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
paul7021c422003-07-15 12:52:22 +00001408 int family)
paul718e3742002-12-13 20:15:29 +00001409{
1410 int bytelen;
1411 struct sockaddr_nl snl;
1412 struct nexthop *nexthop = NULL;
1413 int nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +00001414 int discard;
1415
paul7021c422003-07-15 12:52:22 +00001416 struct
paul718e3742002-12-13 20:15:29 +00001417 {
1418 struct nlmsghdr n;
1419 struct rtmsg r;
1420 char buf[1024];
1421 } req;
1422
1423 memset (&req, 0, sizeof req);
1424
1425 bytelen = (family == AF_INET ? 4 : 16);
1426
1427 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1428 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1429 req.n.nlmsg_type = cmd;
1430 req.r.rtm_family = family;
1431 req.r.rtm_table = rib->table;
1432 req.r.rtm_dst_len = p->prefixlen;
1433
paul7021c422003-07-15 12:52:22 +00001434 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001435 discard = 1;
1436 else
1437 discard = 0;
1438
paul7021c422003-07-15 12:52:22 +00001439 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001440 {
1441 req.r.rtm_protocol = RTPROT_ZEBRA;
1442 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1443
paul7021c422003-07-15 12:52:22 +00001444 if (discard)
paul595db7f2003-05-25 21:35:06 +00001445 {
1446 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
1447 req.r.rtm_type = RTN_BLACKHOLE;
1448 else if (rib->flags & ZEBRA_FLAG_REJECT)
1449 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001450 else
1451 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1452 }
paul595db7f2003-05-25 21:35:06 +00001453 else
paul7021c422003-07-15 12:52:22 +00001454 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001455 }
1456
1457 addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1458
1459 /* Metric. */
1460 addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
1461
1462 if (discard)
1463 {
1464 if (cmd == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +00001465 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1466 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001467 goto skip;
1468 }
1469
1470 /* Multipath case. */
1471 if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
1472 {
1473 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
paul7021c422003-07-15 12:52:22 +00001474 {
paul5ec90d22003-06-19 01:41:37 +00001475
paul7021c422003-07-15 12:52:22 +00001476 if ((cmd == RTM_NEWROUTE
1477 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1478 || (cmd == RTM_DELROUTE
1479 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1480 {
paul5ec90d22003-06-19 01:41:37 +00001481
paul7021c422003-07-15 12:52:22 +00001482 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1483 {
1484 if (IS_ZEBRA_DEBUG_KERNEL)
1485 {
ajsb6178002004-12-07 21:12:56 +00001486 zlog_debug
paul7021c422003-07-15 12:52:22 +00001487 ("netlink_route_multipath() (recursive, 1 hop): "
hasso206d8052005-04-09 16:38:51 +00001488 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001489#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001490 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001491 inet6_ntoa (p->u.prefix6),
1492#else
1493 inet_ntoa (p->u.prefix4),
1494#endif /* HAVE_IPV6 */
1495
1496 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001497 }
paul5ec90d22003-06-19 01:41:37 +00001498
paul7021c422003-07-15 12:52:22 +00001499 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1500 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001501 {
1502 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1503 &nexthop->rgate.ipv4, bytelen);
1504
1505 if (IS_ZEBRA_DEBUG_KERNEL)
1506 zlog_debug("netlink_route_multipath() (recursive, "
1507 "1 hop): nexthop via %s if %u",
1508 inet_ntoa (nexthop->rgate.ipv4),
1509 nexthop->rifindex);
1510 }
paul718e3742002-12-13 20:15:29 +00001511#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001512 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1513 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1514 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001515 {
1516 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1517 &nexthop->rgate.ipv6, bytelen);
1518
1519 if (IS_ZEBRA_DEBUG_KERNEL)
1520 zlog_debug("netlink_route_multipath() (recursive, "
1521 "1 hop): nexthop via %s if %u",
1522 inet6_ntoa (nexthop->rgate.ipv6),
1523 nexthop->rifindex);
1524 }
paul718e3742002-12-13 20:15:29 +00001525#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001526 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1527 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
1528 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1529 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1530 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001531 {
1532 addattr32 (&req.n, sizeof req, RTA_OIF,
1533 nexthop->rifindex);
1534
1535 if (IS_ZEBRA_DEBUG_KERNEL)
1536 zlog_debug("netlink_route_multipath() (recursive, "
1537 "1 hop): nexthop via if %u",
1538 nexthop->rifindex);
1539 }
paul7021c422003-07-15 12:52:22 +00001540 }
1541 else
1542 {
1543 if (IS_ZEBRA_DEBUG_KERNEL)
1544 {
ajsb6178002004-12-07 21:12:56 +00001545 zlog_debug
hasso206d8052005-04-09 16:38:51 +00001546 ("netlink_route_multipath() (single hop): "
1547 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001548#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001549 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001550 inet6_ntoa (p->u.prefix6),
1551#else
1552 inet_ntoa (p->u.prefix4),
1553#endif /* HAVE_IPV6 */
1554 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001555 }
paul5ec90d22003-06-19 01:41:37 +00001556
paul7021c422003-07-15 12:52:22 +00001557 if (nexthop->type == NEXTHOP_TYPE_IPV4
1558 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001559 {
1560 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1561 &nexthop->gate.ipv4, bytelen);
1562
1563 if (IS_ZEBRA_DEBUG_KERNEL)
1564 zlog_debug("netlink_route_multipath() (single hop): "
1565 "nexthop via %s if %u",
1566 inet_ntoa (nexthop->gate.ipv4),
1567 nexthop->ifindex);
1568 }
paul718e3742002-12-13 20:15:29 +00001569#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001570 if (nexthop->type == NEXTHOP_TYPE_IPV6
1571 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1572 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001573 {
1574 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1575 &nexthop->gate.ipv6, bytelen);
1576
1577 if (IS_ZEBRA_DEBUG_KERNEL)
1578 zlog_debug("netlink_route_multipath() (single hop): "
1579 "nexthop via %s if %u",
1580 inet6_ntoa (nexthop->gate.ipv6),
1581 nexthop->ifindex);
1582 }
paul718e3742002-12-13 20:15:29 +00001583#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001584 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1585 || nexthop->type == NEXTHOP_TYPE_IFNAME
1586 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1587 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
1588 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001589 {
1590 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1591
1592 if (IS_ZEBRA_DEBUG_KERNEL)
1593 zlog_debug("netlink_route_multipath() (single hop): "
1594 "nexthop via if %u", nexthop->ifindex);
1595 }
paul7021c422003-07-15 12:52:22 +00001596 }
paul718e3742002-12-13 20:15:29 +00001597
paul7021c422003-07-15 12:52:22 +00001598 if (cmd == RTM_NEWROUTE)
1599 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001600
paul7021c422003-07-15 12:52:22 +00001601 nexthop_num++;
1602 break;
1603 }
1604 }
paul718e3742002-12-13 20:15:29 +00001605 }
1606 else
1607 {
1608 char buf[1024];
1609 struct rtattr *rta = (void *) buf;
1610 struct rtnexthop *rtnh;
1611
1612 rta->rta_type = RTA_MULTIPATH;
paul7021c422003-07-15 12:52:22 +00001613 rta->rta_len = RTA_LENGTH (0);
1614 rtnh = RTA_DATA (rta);
paul718e3742002-12-13 20:15:29 +00001615
1616 nexthop_num = 0;
1617 for (nexthop = rib->nexthop;
paul7021c422003-07-15 12:52:22 +00001618 nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM);
1619 nexthop = nexthop->next)
1620 {
1621 if ((cmd == RTM_NEWROUTE
1622 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1623 || (cmd == RTM_DELROUTE
1624 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1625 {
1626 nexthop_num++;
paul718e3742002-12-13 20:15:29 +00001627
paul7021c422003-07-15 12:52:22 +00001628 rtnh->rtnh_len = sizeof (*rtnh);
1629 rtnh->rtnh_flags = 0;
1630 rtnh->rtnh_hops = 0;
1631 rta->rta_len += rtnh->rtnh_len;
paul718e3742002-12-13 20:15:29 +00001632
paul7021c422003-07-15 12:52:22 +00001633 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1634 {
1635 if (IS_ZEBRA_DEBUG_KERNEL)
1636 {
ajsb6178002004-12-07 21:12:56 +00001637 zlog_debug ("netlink_route_multipath() "
hasso206d8052005-04-09 16:38:51 +00001638 "(recursive, multihop): %s %s/%d type %s",
hasso1ada8192005-06-12 11:28:18 +00001639 lookup (nlmsg_str, cmd),
1640#ifdef HAVE_IPV6
1641 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
1642 inet6_ntoa (p->u.prefix6),
1643#else
1644 inet_ntoa (p->u.prefix4),
1645#endif /* HAVE_IPV6 */
hasso206d8052005-04-09 16:38:51 +00001646 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001647 }
1648 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1649 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
1650 {
1651 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1652 &nexthop->rgate.ipv4, bytelen);
1653 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
hasso206d8052005-04-09 16:38:51 +00001654
1655 if (IS_ZEBRA_DEBUG_KERNEL)
1656 zlog_debug("netlink_route_multipath() (recursive, "
1657 "multihop): nexthop via %s if %u",
1658 inet_ntoa (nexthop->rgate.ipv4),
1659 nexthop->rifindex);
paul7021c422003-07-15 12:52:22 +00001660 }
paul718e3742002-12-13 20:15:29 +00001661#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001662 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1663 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
1664 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001665 {
1666 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1667 &nexthop->rgate.ipv6, bytelen);
1668
1669 if (IS_ZEBRA_DEBUG_KERNEL)
1670 zlog_debug("netlink_route_multipath() (recursive, "
1671 "multihop): nexthop via %s if %u",
1672 inet6_ntoa (nexthop->rgate.ipv6),
1673 nexthop->rifindex);
1674 }
paul718e3742002-12-13 20:15:29 +00001675#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001676 /* ifindex */
1677 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1678 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
1679 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1680 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1681 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001682 {
1683 rtnh->rtnh_ifindex = nexthop->rifindex;
1684
1685 if (IS_ZEBRA_DEBUG_KERNEL)
1686 zlog_debug("netlink_route_multipath() (recursive, "
1687 "multihop): nexthop via if %u",
1688 nexthop->rifindex);
1689 }
paul7021c422003-07-15 12:52:22 +00001690 else
hasso206d8052005-04-09 16:38:51 +00001691 {
1692 rtnh->rtnh_ifindex = 0;
1693 }
paul7021c422003-07-15 12:52:22 +00001694 }
1695 else
1696 {
1697 if (IS_ZEBRA_DEBUG_KERNEL)
1698 {
hasso206d8052005-04-09 16:38:51 +00001699 zlog_debug ("netlink_route_multipath() (multihop): "
1700 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001701#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001702 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001703 inet6_ntoa (p->u.prefix6),
1704#else
1705 inet_ntoa (p->u.prefix4),
1706#endif /* HAVE_IPV6 */
1707 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001708 }
1709 if (nexthop->type == NEXTHOP_TYPE_IPV4
1710 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1711 {
hasso206d8052005-04-09 16:38:51 +00001712 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1713 &nexthop->gate.ipv4, bytelen);
1714 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
1715
1716 if (IS_ZEBRA_DEBUG_KERNEL)
1717 zlog_debug("netlink_route_multipath() (multihop): "
1718 "nexthop via %s if %u",
1719 inet_ntoa (nexthop->gate.ipv4),
1720 nexthop->ifindex);
paul7021c422003-07-15 12:52:22 +00001721 }
paul718e3742002-12-13 20:15:29 +00001722#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001723 if (nexthop->type == NEXTHOP_TYPE_IPV6
1724 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1725 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001726 {
1727 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1728 &nexthop->gate.ipv6, bytelen);
1729
1730 if (IS_ZEBRA_DEBUG_KERNEL)
1731 zlog_debug("netlink_route_multipath() (multihop): "
1732 "nexthop via %s if %u",
1733 inet6_ntoa (nexthop->gate.ipv6),
1734 nexthop->ifindex);
1735 }
paul718e3742002-12-13 20:15:29 +00001736#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001737 /* ifindex */
1738 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1739 || nexthop->type == NEXTHOP_TYPE_IFNAME
1740 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1741 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1742 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001743 {
1744 rtnh->rtnh_ifindex = nexthop->ifindex;
1745
1746 if (IS_ZEBRA_DEBUG_KERNEL)
1747 zlog_debug("netlink_route_multipath() (multihop): "
1748 "nexthop via if %u", nexthop->ifindex);
1749 }
paul7021c422003-07-15 12:52:22 +00001750 else
hasso206d8052005-04-09 16:38:51 +00001751 {
1752 rtnh->rtnh_ifindex = 0;
1753 }
paul7021c422003-07-15 12:52:22 +00001754 }
1755 rtnh = RTNH_NEXT (rtnh);
paul718e3742002-12-13 20:15:29 +00001756
paul7021c422003-07-15 12:52:22 +00001757 if (cmd == RTM_NEWROUTE)
1758 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1759 }
1760 }
paul718e3742002-12-13 20:15:29 +00001761
1762 if (rta->rta_len > RTA_LENGTH (0))
paul7021c422003-07-15 12:52:22 +00001763 addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta),
1764 RTA_PAYLOAD (rta));
paul718e3742002-12-13 20:15:29 +00001765 }
1766
1767 /* If there is no useful nexthop then return. */
1768 if (nexthop_num == 0)
1769 {
1770 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001771 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +00001772 return 0;
1773 }
1774
paul7021c422003-07-15 12:52:22 +00001775skip:
paul718e3742002-12-13 20:15:29 +00001776
1777 /* Destination netlink address. */
1778 memset (&snl, 0, sizeof snl);
1779 snl.nl_family = AF_NETLINK;
1780
paul718e3742002-12-13 20:15:29 +00001781 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001782 return netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001783}
1784
1785int
1786kernel_add_ipv4 (struct prefix *p, struct rib *rib)
1787{
1788 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
1789}
1790
1791int
1792kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
1793{
1794 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
1795}
1796
1797#ifdef HAVE_IPV6
1798int
1799kernel_add_ipv6 (struct prefix *p, struct rib *rib)
1800{
1801 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
1802}
1803
1804int
1805kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
1806{
1807 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
1808}
1809
1810/* Delete IPv6 route from the kernel. */
1811int
1812kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul6621ca82005-11-23 13:02:08 +00001813 unsigned int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +00001814{
paul7021c422003-07-15 12:52:22 +00001815 return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix,
1816 dest->prefixlen, gate, index, flags, table);
paul718e3742002-12-13 20:15:29 +00001817}
1818#endif /* HAVE_IPV6 */
1819
1820/* Interface address modification. */
1821int
1822netlink_address (int cmd, int family, struct interface *ifp,
paul7021c422003-07-15 12:52:22 +00001823 struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001824{
1825 int bytelen;
1826 struct prefix *p;
1827
paul7021c422003-07-15 12:52:22 +00001828 struct
paul718e3742002-12-13 20:15:29 +00001829 {
1830 struct nlmsghdr n;
1831 struct ifaddrmsg ifa;
1832 char buf[1024];
1833 } req;
1834
1835 p = ifc->address;
1836 memset (&req, 0, sizeof req);
1837
1838 bytelen = (family == AF_INET ? 4 : 16);
1839
paul7021c422003-07-15 12:52:22 +00001840 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +00001841 req.n.nlmsg_flags = NLM_F_REQUEST;
1842 req.n.nlmsg_type = cmd;
1843 req.ifa.ifa_family = family;
1844
1845 req.ifa.ifa_index = ifp->ifindex;
1846 req.ifa.ifa_prefixlen = p->prefixlen;
1847
1848 addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1849
1850 if (family == AF_INET && cmd == RTM_NEWADDR)
1851 {
1852 if (if_is_broadcast (ifp) && ifc->destination)
paul7021c422003-07-15 12:52:22 +00001853 {
1854 p = ifc->destination;
1855 addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
1856 bytelen);
1857 }
paul718e3742002-12-13 20:15:29 +00001858 }
1859
1860 if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
1861 SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
paul7021c422003-07-15 12:52:22 +00001862
paul718e3742002-12-13 20:15:29 +00001863 if (ifc->label)
1864 addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
paul7021c422003-07-15 12:52:22 +00001865 strlen (ifc->label) + 1);
paul718e3742002-12-13 20:15:29 +00001866
1867 return netlink_talk (&req.n, &netlink_cmd);
1868}
1869
1870int
1871kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
1872{
1873 return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
1874}
1875
1876int
1877kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
1878{
1879 return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
1880}
1881
paul718e3742002-12-13 20:15:29 +00001882
1883extern struct thread_master *master;
1884
1885/* Kernel route reflection. */
1886int
1887kernel_read (struct thread *thread)
1888{
1889 int ret;
1890 int sock;
1891
1892 sock = THREAD_FD (thread);
1893 ret = netlink_parse_info (netlink_information_fetch, &netlink);
paulb21b19c2003-06-15 01:28:29 +00001894 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001895
1896 return 0;
1897}
1898
1899/* Exported interface function. This function simply calls
1900 netlink_socket (). */
1901void
paul6621ca82005-11-23 13:02:08 +00001902kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001903{
1904 unsigned long groups;
1905
paul7021c422003-07-15 12:52:22 +00001906 groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
paul718e3742002-12-13 20:15:29 +00001907#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001908 groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
paul718e3742002-12-13 20:15:29 +00001909#endif /* HAVE_IPV6 */
1910 netlink_socket (&netlink, groups);
1911 netlink_socket (&netlink_cmd, 0);
1912
1913 /* Register kernel socket. */
1914 if (netlink.sock > 0)
paulb21b19c2003-06-15 01:28:29 +00001915 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001916}