blob: 3b602c45ed41dcc3852b2bcaeaf5de202cf85bb0 [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;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000580 void *addr;
581 void *broad;
paul718e3742002-12-13 20:15:29 +0000582 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
Andrew J. Schorre4529632006-12-12 19:18:21 +0000640 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
641 if (tb[IFA_LOCAL] == NULL)
642 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
paul31a476c2003-09-29 19:54:53 +0000643 if (tb[IFA_ADDRESS] == NULL)
644 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
645
Andrew J. Schorre4529632006-12-12 19:18:21 +0000646 /* local interface address */
647 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
648
649 /* is there a peer address? */
650 /* N.B. I do not understand why the memcmp compares 4 bytes regardless
651 of address family, but this is exactly how it appears in
652 print_addrinfo. I wonder if it should be RTA_PAYLOAD(tb[IFA_ADDRESS])
653 instead of 4... */
654 if (tb[IFA_ADDRESS] &&
655 memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), 4))
paul7021c422003-07-15 12:52:22 +0000656 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000657 broad = RTA_DATA(tb[IFA_ADDRESS]);
658 SET_FLAG (flags, ZEBRA_IFA_PEER);
paul7021c422003-07-15 12:52:22 +0000659 }
paul31a476c2003-09-29 19:54:53 +0000660 else
Andrew J. Schorre4529632006-12-12 19:18:21 +0000661 /* seeking a broadcast address */
662 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL);
paul00df0c12002-12-13 21:07:36 +0000663
Paul Jakma27b47252006-07-02 16:38:54 +0000664 /* addr is primary key, SOL if we don't have one */
665 if (addr == NULL)
666 {
667 zlog_debug ("%s: NULL address", __func__);
668 return -1;
669 }
670
paul718e3742002-12-13 20:15:29 +0000671 /* Flags. */
672 if (ifa->ifa_flags & IFA_F_SECONDARY)
673 SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
674
675 /* Label */
676 if (tb[IFA_LABEL])
677 label = (char *) RTA_DATA (tb[IFA_LABEL]);
678
679 if (ifp && label && strcmp (ifp->name, label) == 0)
680 label = NULL;
681
682 /* Register interface address to the interface. */
683 if (ifa->ifa_family == AF_INET)
684 {
paul7021c422003-07-15 12:52:22 +0000685 if (h->nlmsg_type == RTM_NEWADDR)
686 connected_add_ipv4 (ifp, flags,
687 (struct in_addr *) addr, ifa->ifa_prefixlen,
688 (struct in_addr *) broad, label);
689 else
690 connected_delete_ipv4 (ifp, flags,
691 (struct in_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000692 (struct in_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000693 }
694#ifdef HAVE_IPV6
695 if (ifa->ifa_family == AF_INET6)
696 {
697 if (h->nlmsg_type == RTM_NEWADDR)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000698 connected_add_ipv6 (ifp, flags,
paul7021c422003-07-15 12:52:22 +0000699 (struct in6_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000700 (struct in6_addr *) broad, label);
paul718e3742002-12-13 20:15:29 +0000701 else
paul7021c422003-07-15 12:52:22 +0000702 connected_delete_ipv6 (ifp,
703 (struct in6_addr *) addr, ifa->ifa_prefixlen,
704 (struct in6_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000705 }
paul7021c422003-07-15 12:52:22 +0000706#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000707
708 return 0;
709}
710
711/* Looking up routing table by netlink interface. */
712int
713netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h)
714{
715 int len;
716 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000717 struct rtattr *tb[RTA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000718 u_char flags = 0;
paul7021c422003-07-15 12:52:22 +0000719
720 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000721
722 int index;
723 int table;
hasso34195bf2004-04-06 12:07:06 +0000724 int metric;
725
paul718e3742002-12-13 20:15:29 +0000726 void *dest;
727 void *gate;
Paul Jakma7514fb72007-05-02 16:05:35 +0000728 void *src;
paul718e3742002-12-13 20:15:29 +0000729
730 rtm = NLMSG_DATA (h);
731
732 if (h->nlmsg_type != RTM_NEWROUTE)
733 return 0;
734 if (rtm->rtm_type != RTN_UNICAST)
735 return 0;
736
737 table = rtm->rtm_table;
paul7021c422003-07-15 12:52:22 +0000738#if 0 /* we weed them out later in rib_weed_tables () */
paulb21b19c2003-06-15 01:28:29 +0000739 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000740 return 0;
741#endif
742
paul7021c422003-07-15 12:52:22 +0000743 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000744 if (len < 0)
745 return -1;
746
747 memset (tb, 0, sizeof tb);
748 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
749
750 if (rtm->rtm_flags & RTM_F_CLONED)
751 return 0;
752 if (rtm->rtm_protocol == RTPROT_REDIRECT)
753 return 0;
754 if (rtm->rtm_protocol == RTPROT_KERNEL)
755 return 0;
756
757 if (rtm->rtm_src_len != 0)
758 return 0;
759
760 /* Route which inserted by Zebra. */
761 if (rtm->rtm_protocol == RTPROT_ZEBRA)
762 flags |= ZEBRA_FLAG_SELFROUTE;
paul7021c422003-07-15 12:52:22 +0000763
paul718e3742002-12-13 20:15:29 +0000764 index = 0;
hasso34195bf2004-04-06 12:07:06 +0000765 metric = 0;
paul718e3742002-12-13 20:15:29 +0000766 dest = NULL;
767 gate = NULL;
Paul Jakma7514fb72007-05-02 16:05:35 +0000768 src = NULL;
paul718e3742002-12-13 20:15:29 +0000769
770 if (tb[RTA_OIF])
771 index = *(int *) RTA_DATA (tb[RTA_OIF]);
772
773 if (tb[RTA_DST])
774 dest = RTA_DATA (tb[RTA_DST]);
775 else
776 dest = anyaddr;
777
Paul Jakma7514fb72007-05-02 16:05:35 +0000778 if (tb[RTA_PREFSRC])
779 src = RTA_DATA (tb[RTA_PREFSRC]);
780
paul718e3742002-12-13 20:15:29 +0000781 /* Multipath treatment is needed. */
782 if (tb[RTA_GATEWAY])
783 gate = RTA_DATA (tb[RTA_GATEWAY]);
784
hasso34195bf2004-04-06 12:07:06 +0000785 if (tb[RTA_PRIORITY])
786 metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
787
paul718e3742002-12-13 20:15:29 +0000788 if (rtm->rtm_family == AF_INET)
789 {
790 struct prefix_ipv4 p;
791 p.family = AF_INET;
792 memcpy (&p.prefix, dest, 4);
793 p.prefixlen = rtm->rtm_dst_len;
794
Paul Jakma7514fb72007-05-02 16:05:35 +0000795 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index, table, metric, 0);
paul718e3742002-12-13 20:15:29 +0000796 }
797#ifdef HAVE_IPV6
798 if (rtm->rtm_family == AF_INET6)
799 {
800 struct prefix_ipv6 p;
801 p.family = AF_INET6;
802 memcpy (&p.prefix, dest, 16);
803 p.prefixlen = rtm->rtm_dst_len;
804
hassobe61c4e2005-08-27 06:05:47 +0000805 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table,
806 metric, 0);
paul718e3742002-12-13 20:15:29 +0000807 }
808#endif /* HAVE_IPV6 */
809
810 return 0;
811}
812
paul7021c422003-07-15 12:52:22 +0000813struct message rtproto_str[] = {
paul718e3742002-12-13 20:15:29 +0000814 {RTPROT_REDIRECT, "redirect"},
815 {RTPROT_KERNEL, "kernel"},
816 {RTPROT_BOOT, "boot"},
817 {RTPROT_STATIC, "static"},
818 {RTPROT_GATED, "GateD"},
819 {RTPROT_RA, "router advertisement"},
820 {RTPROT_MRT, "MRT"},
821 {RTPROT_ZEBRA, "Zebra"},
822#ifdef RTPROT_BIRD
823 {RTPROT_BIRD, "BIRD"},
824#endif /* RTPROT_BIRD */
825 {0, NULL}
826};
827
828/* Routing information change from the kernel. */
829int
830netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
831{
832 int len;
833 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000834 struct rtattr *tb[RTA_MAX + 1];
835
836 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000837
838 int index;
839 int table;
840 void *dest;
841 void *gate;
Paul Jakma7514fb72007-05-02 16:05:35 +0000842 void *src;
paul718e3742002-12-13 20:15:29 +0000843
844 rtm = NLMSG_DATA (h);
845
paul7021c422003-07-15 12:52:22 +0000846 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
paul718e3742002-12-13 20:15:29 +0000847 {
848 /* If this is not route add/delete message print warning. */
849 zlog_warn ("Kernel message: %d\n", h->nlmsg_type);
850 return 0;
851 }
852
853 /* Connected route. */
854 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000855 zlog_debug ("%s %s %s proto %s",
paul7021c422003-07-15 12:52:22 +0000856 h->nlmsg_type ==
857 RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
858 rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
859 rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
860 lookup (rtproto_str, rtm->rtm_protocol));
paul718e3742002-12-13 20:15:29 +0000861
862 if (rtm->rtm_type != RTN_UNICAST)
863 {
864 return 0;
865 }
866
867 table = rtm->rtm_table;
paulb21b19c2003-06-15 01:28:29 +0000868 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000869 {
870 return 0;
871 }
872
paul7021c422003-07-15 12:52:22 +0000873 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000874 if (len < 0)
875 return -1;
876
877 memset (tb, 0, sizeof tb);
878 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
879
880 if (rtm->rtm_flags & RTM_F_CLONED)
881 return 0;
882 if (rtm->rtm_protocol == RTPROT_REDIRECT)
883 return 0;
884 if (rtm->rtm_protocol == RTPROT_KERNEL)
885 return 0;
886
887 if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
888 return 0;
889
890 if (rtm->rtm_src_len != 0)
891 {
892 zlog_warn ("netlink_route_change(): no src len");
893 return 0;
894 }
paul7021c422003-07-15 12:52:22 +0000895
paul718e3742002-12-13 20:15:29 +0000896 index = 0;
897 dest = NULL;
898 gate = NULL;
Paul Jakma7514fb72007-05-02 16:05:35 +0000899 src = NULL;
paul718e3742002-12-13 20:15:29 +0000900
901 if (tb[RTA_OIF])
902 index = *(int *) RTA_DATA (tb[RTA_OIF]);
903
904 if (tb[RTA_DST])
905 dest = RTA_DATA (tb[RTA_DST]);
906 else
907 dest = anyaddr;
908
909 if (tb[RTA_GATEWAY])
910 gate = RTA_DATA (tb[RTA_GATEWAY]);
911
Paul Jakma7514fb72007-05-02 16:05:35 +0000912 if (tb[RTA_PREFSRC])
913 src = RTA_DATA (tb[RTA_PREFSRC]);
914
paul718e3742002-12-13 20:15:29 +0000915 if (rtm->rtm_family == AF_INET)
916 {
917 struct prefix_ipv4 p;
918 p.family = AF_INET;
919 memcpy (&p.prefix, dest, 4);
920 p.prefixlen = rtm->rtm_dst_len;
921
922 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000923 {
924 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000925 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000926 inet_ntoa (p.prefix), p.prefixlen);
927 else
ajsb6178002004-12-07 21:12:56 +0000928 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000929 inet_ntoa (p.prefix), p.prefixlen);
930 }
paul718e3742002-12-13 20:15:29 +0000931
932 if (h->nlmsg_type == RTM_NEWROUTE)
Paul Jakma7514fb72007-05-02 16:05:35 +0000933 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, table, 0, 0);
paul718e3742002-12-13 20:15:29 +0000934 else
paul7021c422003-07-15 12:52:22 +0000935 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
paul718e3742002-12-13 20:15:29 +0000936 }
937
938#ifdef HAVE_IPV6
939 if (rtm->rtm_family == AF_INET6)
940 {
941 struct prefix_ipv6 p;
942 char buf[BUFSIZ];
943
944 p.family = AF_INET6;
945 memcpy (&p.prefix, dest, 16);
946 p.prefixlen = rtm->rtm_dst_len;
947
948 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000949 {
950 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000951 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000952 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
953 p.prefixlen);
954 else
ajsb6178002004-12-07 21:12:56 +0000955 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000956 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
957 p.prefixlen);
958 }
paul718e3742002-12-13 20:15:29 +0000959
960 if (h->nlmsg_type == RTM_NEWROUTE)
hassobe61c4e2005-08-27 06:05:47 +0000961 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0, 0, 0);
paul718e3742002-12-13 20:15:29 +0000962 else
paul7021c422003-07-15 12:52:22 +0000963 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0);
paul718e3742002-12-13 20:15:29 +0000964 }
965#endif /* HAVE_IPV6 */
966
967 return 0;
968}
969
970int
971netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
972{
973 int len;
974 struct ifinfomsg *ifi;
paul7021c422003-07-15 12:52:22 +0000975 struct rtattr *tb[IFLA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000976 struct interface *ifp;
977 char *name;
978
979 ifi = NLMSG_DATA (h);
980
paul7021c422003-07-15 12:52:22 +0000981 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
paul718e3742002-12-13 20:15:29 +0000982 {
983 /* If this is not link add/delete message so print warning. */
984 zlog_warn ("netlink_link_change: wrong kernel message %d\n",
paul7021c422003-07-15 12:52:22 +0000985 h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +0000986 return 0;
987 }
988
989 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
990 if (len < 0)
991 return -1;
992
993 /* Looking up interface name. */
994 memset (tb, 0, sizeof tb);
995 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000996
paul1e193152005-02-14 23:53:05 +0000997#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000998 /* check for wireless messages to ignore */
999 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
1000 {
1001 if (IS_ZEBRA_DEBUG_KERNEL)
1002 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
1003 return 0;
1004 }
paul1e193152005-02-14 23:53:05 +00001005#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +00001006
paul718e3742002-12-13 20:15:29 +00001007 if (tb[IFLA_IFNAME] == NULL)
1008 return -1;
paul7021c422003-07-15 12:52:22 +00001009 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +00001010
1011 /* Add interface. */
1012 if (h->nlmsg_type == RTM_NEWLINK)
1013 {
1014 ifp = if_lookup_by_name (name);
1015
paul7021c422003-07-15 12:52:22 +00001016 if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1017 {
1018 if (ifp == NULL)
1019 ifp = if_get_by_name (name);
paul718e3742002-12-13 20:15:29 +00001020
ajsd2fc8892005-04-02 18:38:43 +00001021 set_ifindex(ifp, ifi->ifi_index);
paul7021c422003-07-15 12:52:22 +00001022 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +00001023 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +00001024 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +00001025
paul7021c422003-07-15 12:52:22 +00001026 /* If new link is added. */
1027 if_add_update (ifp);
1028 }
paul718e3742002-12-13 20:15:29 +00001029 else
paul7021c422003-07-15 12:52:22 +00001030 {
1031 /* Interface status change. */
ajsd2fc8892005-04-02 18:38:43 +00001032 set_ifindex(ifp, ifi->ifi_index);
paul44145db2004-05-09 11:00:23 +00001033 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +00001034 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +00001035
paul7021c422003-07-15 12:52:22 +00001036 if (if_is_operative (ifp))
1037 {
1038 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1039 if (!if_is_operative (ifp))
1040 if_down (ifp);
ajsa608bbf2005-03-29 17:03:49 +00001041 else
1042 /* Must notify client daemons of new interface status. */
1043 zebra_interface_up_update (ifp);
paul7021c422003-07-15 12:52:22 +00001044 }
1045 else
1046 {
1047 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1048 if (if_is_operative (ifp))
1049 if_up (ifp);
1050 }
1051 }
paul718e3742002-12-13 20:15:29 +00001052 }
1053 else
1054 {
1055 /* RTM_DELLINK. */
1056 ifp = if_lookup_by_name (name);
1057
1058 if (ifp == NULL)
paul7021c422003-07-15 12:52:22 +00001059 {
1060 zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find",
paul718e3742002-12-13 20:15:29 +00001061 name);
paul7021c422003-07-15 12:52:22 +00001062 return 0;
1063 }
1064
paul718e3742002-12-13 20:15:29 +00001065 if_delete_update (ifp);
1066 }
1067
1068 return 0;
1069}
1070
1071int
1072netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h)
1073{
1074 switch (h->nlmsg_type)
1075 {
1076 case RTM_NEWROUTE:
1077 return netlink_route_change (snl, h);
1078 break;
1079 case RTM_DELROUTE:
1080 return netlink_route_change (snl, h);
1081 break;
1082 case RTM_NEWLINK:
1083 return netlink_link_change (snl, h);
1084 break;
1085 case RTM_DELLINK:
1086 return netlink_link_change (snl, h);
1087 break;
1088 case RTM_NEWADDR:
1089 return netlink_interface_addr (snl, h);
1090 break;
1091 case RTM_DELADDR:
1092 return netlink_interface_addr (snl, h);
1093 break;
1094 default:
1095 zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type);
1096 break;
1097 }
1098 return 0;
1099}
1100
1101/* Interface lookup by netlink socket. */
1102int
paul6621ca82005-11-23 13:02:08 +00001103interface_lookup_netlink (void)
paul718e3742002-12-13 20:15:29 +00001104{
1105 int ret;
paul5f37d862003-04-19 00:11:28 +00001106 int flags;
1107 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001108
paul5f37d862003-04-19 00:11:28 +00001109 /*
1110 * Change netlink socket flags to blocking to ensure we get
1111 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001112 */
1113 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1114 if (snb_ret < 0)
1115 zlog (NULL, LOG_WARNING,
1116 "%s:%i Warning: Could not set netlink socket to blocking.",
1117 __FUNCTION__, __LINE__);
1118
paul718e3742002-12-13 20:15:29 +00001119 /* Get interface information. */
1120 ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd);
1121 if (ret < 0)
1122 return ret;
1123 ret = netlink_parse_info (netlink_interface, &netlink_cmd);
1124 if (ret < 0)
1125 return ret;
1126
1127 /* Get IPv4 address of the interfaces. */
1128 ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd);
1129 if (ret < 0)
1130 return ret;
1131 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1132 if (ret < 0)
1133 return ret;
1134
1135#ifdef HAVE_IPV6
1136 /* Get IPv6 address of the interfaces. */
1137 ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd);
1138 if (ret < 0)
1139 return ret;
1140 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1141 if (ret < 0)
1142 return ret;
1143#endif /* HAVE_IPV6 */
1144
paul7021c422003-07-15 12:52:22 +00001145 /* restore socket flags */
1146 if (snb_ret == 0)
1147 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001148 return 0;
1149}
1150
1151/* Routing table read function using netlink interface. Only called
1152 bootstrap time. */
1153int
paul6621ca82005-11-23 13:02:08 +00001154netlink_route_read (void)
paul718e3742002-12-13 20:15:29 +00001155{
1156 int ret;
paul5f37d862003-04-19 00:11:28 +00001157 int flags;
1158 int snb_ret;
paul7021c422003-07-15 12:52:22 +00001159
paul5f37d862003-04-19 00:11:28 +00001160 /*
1161 * Change netlink socket flags to blocking to ensure we get
1162 * a reply via nelink_parse_info
paul7021c422003-07-15 12:52:22 +00001163 */
1164 snb_ret = set_netlink_blocking (&netlink_cmd, &flags);
1165 if (snb_ret < 0)
1166 zlog (NULL, LOG_WARNING,
1167 "%s:%i Warning: Could not set netlink socket to blocking.",
1168 __FUNCTION__, __LINE__);
1169
paul718e3742002-12-13 20:15:29 +00001170 /* Get IPv4 routing table. */
1171 ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd);
1172 if (ret < 0)
1173 return ret;
1174 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1175 if (ret < 0)
1176 return ret;
1177
1178#ifdef HAVE_IPV6
1179 /* Get IPv6 routing table. */
1180 ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd);
1181 if (ret < 0)
1182 return ret;
1183 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1184 if (ret < 0)
1185 return ret;
1186#endif /* HAVE_IPV6 */
1187
paul5f37d862003-04-19 00:11:28 +00001188 /* restore flags */
paul7021c422003-07-15 12:52:22 +00001189 if (snb_ret == 0)
1190 set_netlink_nonblocking (&netlink_cmd, &flags);
paul718e3742002-12-13 20:15:29 +00001191 return 0;
1192}
1193
1194/* Utility function comes from iproute2.
1195 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1196int
1197addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
1198{
1199 int len;
1200 struct rtattr *rta;
1201
paul7021c422003-07-15 12:52:22 +00001202 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001203
paul7021c422003-07-15 12:52:22 +00001204 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001205 return -1;
1206
paul7021c422003-07-15 12:52:22 +00001207 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001208 rta->rta_type = type;
1209 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001210 memcpy (RTA_DATA (rta), data, alen);
paul718e3742002-12-13 20:15:29 +00001211 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1212
1213 return 0;
1214}
1215
1216int
1217rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
1218{
1219 int len;
1220 struct rtattr *subrta;
1221
paul7021c422003-07-15 12:52:22 +00001222 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001223
paul7021c422003-07-15 12:52:22 +00001224 if (RTA_ALIGN (rta->rta_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001225 return -1;
1226
paul7021c422003-07-15 12:52:22 +00001227 subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
paul718e3742002-12-13 20:15:29 +00001228 subrta->rta_type = type;
1229 subrta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001230 memcpy (RTA_DATA (subrta), data, alen);
paul718e3742002-12-13 20:15:29 +00001231 rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
1232
1233 return 0;
1234}
1235
1236/* Utility function comes from iproute2.
1237 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1238int
1239addattr32 (struct nlmsghdr *n, int maxlen, int type, int data)
1240{
1241 int len;
1242 struct rtattr *rta;
paul7021c422003-07-15 12:52:22 +00001243
1244 len = RTA_LENGTH (4);
1245
paul718e3742002-12-13 20:15:29 +00001246 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1247 return -1;
1248
paul7021c422003-07-15 12:52:22 +00001249 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001250 rta->rta_type = type;
1251 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001252 memcpy (RTA_DATA (rta), &data, 4);
paul718e3742002-12-13 20:15:29 +00001253 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1254
1255 return 0;
1256}
1257
1258static int
1259netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h)
1260{
hassob7ed1ec2005-03-31 20:13:49 +00001261 zlog_warn ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +00001262 return 0;
1263}
1264
1265/* sendmsg() to netlink socket then recvmsg(). */
1266int
1267netlink_talk (struct nlmsghdr *n, struct nlsock *nl)
1268{
1269 int status;
1270 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +00001271 struct iovec iov = { (void *) n, n->nlmsg_len };
1272 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +00001273 int flags = 0;
paul5f37d862003-04-19 00:11:28 +00001274 int snb_ret;
ajs4be019d2005-01-29 16:12:41 +00001275 int save_errno;
paul7021c422003-07-15 12:52:22 +00001276
paul718e3742002-12-13 20:15:29 +00001277 memset (&snl, 0, sizeof snl);
1278 snl.nl_family = AF_NETLINK;
paul7021c422003-07-15 12:52:22 +00001279
hassob7ed1ec2005-03-31 20:13:49 +00001280 n->nlmsg_seq = ++nl->seq;
paul718e3742002-12-13 20:15:29 +00001281
1282 /* Request an acknowledgement by setting NLM_F_ACK */
1283 n->nlmsg_flags |= NLM_F_ACK;
paul7021c422003-07-15 12:52:22 +00001284
1285 if (IS_ZEBRA_DEBUG_KERNEL)
hassob7ed1ec2005-03-31 20:13:49 +00001286 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name,
paul7021c422003-07-15 12:52:22 +00001287 lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
1288 n->nlmsg_seq);
paul718e3742002-12-13 20:15:29 +00001289
1290 /* Send message to netlink interface. */
paul7021c422003-07-15 12:52:22 +00001291 if (zserv_privs.change (ZPRIVS_RAISE))
1292 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +00001293 status = sendmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +00001294 save_errno = errno;
paul7021c422003-07-15 12:52:22 +00001295 if (zserv_privs.change (ZPRIVS_LOWER))
1296 zlog (NULL, LOG_ERR, "Can't lower privileges");
1297
paul718e3742002-12-13 20:15:29 +00001298 if (status < 0)
1299 {
1300 zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
ajs4be019d2005-01-29 16:12:41 +00001301 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +00001302 return -1;
1303 }
paul7021c422003-07-15 12:52:22 +00001304
paul718e3742002-12-13 20:15:29 +00001305 /*
1306 * Change socket flags for blocking I/O.
1307 * This ensures we wait for a reply in netlink_parse_info().
1308 */
paul7021c422003-07-15 12:52:22 +00001309 snb_ret = set_netlink_blocking (nl, &flags);
1310 if (snb_ret < 0)
1311 zlog (NULL, LOG_WARNING,
1312 "%s:%i Warning: Could not set netlink socket to blocking.",
1313 __FUNCTION__, __LINE__);
paul718e3742002-12-13 20:15:29 +00001314
1315 /*
1316 * Get reply from netlink socket.
1317 * The reply should either be an acknowlegement or an error.
1318 */
1319 status = netlink_parse_info (netlink_talk_filter, nl);
paul7021c422003-07-15 12:52:22 +00001320
paul718e3742002-12-13 20:15:29 +00001321 /* Restore socket flags for nonblocking I/O */
paul7021c422003-07-15 12:52:22 +00001322 if (snb_ret == 0)
1323 set_netlink_nonblocking (nl, &flags);
1324
paul718e3742002-12-13 20:15:29 +00001325 return status;
1326}
1327
1328/* Routing table change via netlink interface. */
1329int
1330netlink_route (int cmd, int family, void *dest, int length, void *gate,
paul7021c422003-07-15 12:52:22 +00001331 int index, int zebra_flags, int table)
paul718e3742002-12-13 20:15:29 +00001332{
1333 int ret;
1334 int bytelen;
1335 struct sockaddr_nl snl;
1336 int discard;
1337
paul7021c422003-07-15 12:52:22 +00001338 struct
paul718e3742002-12-13 20:15:29 +00001339 {
1340 struct nlmsghdr n;
1341 struct rtmsg r;
1342 char buf[1024];
1343 } req;
1344
1345 memset (&req, 0, sizeof req);
1346
1347 bytelen = (family == AF_INET ? 4 : 16);
1348
1349 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1350 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1351 req.n.nlmsg_type = cmd;
1352 req.r.rtm_family = family;
1353 req.r.rtm_table = table;
1354 req.r.rtm_dst_len = length;
1355
hasso81dfcaa2003-05-25 19:21:25 +00001356 if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1357 || (zebra_flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001358 discard = 1;
1359 else
1360 discard = 0;
1361
paul7021c422003-07-15 12:52:22 +00001362 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001363 {
1364 req.r.rtm_protocol = RTPROT_ZEBRA;
1365 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1366
paul7021c422003-07-15 12:52:22 +00001367 if (discard)
paul595db7f2003-05-25 21:35:06 +00001368 {
1369 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1370 req.r.rtm_type = RTN_BLACKHOLE;
1371 else if (zebra_flags & ZEBRA_FLAG_REJECT)
1372 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001373 else
1374 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1375 }
paul595db7f2003-05-25 21:35:06 +00001376 else
paul7021c422003-07-15 12:52:22 +00001377 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001378 }
1379
1380 if (dest)
1381 addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen);
1382
paul7021c422003-07-15 12:52:22 +00001383 if (!discard)
paul718e3742002-12-13 20:15:29 +00001384 {
1385 if (gate)
paul7021c422003-07-15 12:52:22 +00001386 addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen);
paul718e3742002-12-13 20:15:29 +00001387 if (index > 0)
paul7021c422003-07-15 12:52:22 +00001388 addattr32 (&req.n, sizeof req, RTA_OIF, index);
paul718e3742002-12-13 20:15:29 +00001389 }
1390
1391 /* Destination netlink address. */
1392 memset (&snl, 0, sizeof snl);
1393 snl.nl_family = AF_NETLINK;
1394
1395 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001396 ret = netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001397 if (ret < 0)
1398 return -1;
1399
1400 return 0;
1401}
1402
1403/* Routing table change via netlink interface. */
1404int
1405netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
paul7021c422003-07-15 12:52:22 +00001406 int family)
paul718e3742002-12-13 20:15:29 +00001407{
1408 int bytelen;
1409 struct sockaddr_nl snl;
1410 struct nexthop *nexthop = NULL;
1411 int nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +00001412 int discard;
1413
paul7021c422003-07-15 12:52:22 +00001414 struct
paul718e3742002-12-13 20:15:29 +00001415 {
1416 struct nlmsghdr n;
1417 struct rtmsg r;
1418 char buf[1024];
1419 } req;
1420
1421 memset (&req, 0, sizeof req);
1422
1423 bytelen = (family == AF_INET ? 4 : 16);
1424
1425 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1426 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1427 req.n.nlmsg_type = cmd;
1428 req.r.rtm_family = family;
1429 req.r.rtm_table = rib->table;
1430 req.r.rtm_dst_len = p->prefixlen;
1431
paul7021c422003-07-15 12:52:22 +00001432 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001433 discard = 1;
1434 else
1435 discard = 0;
1436
paul7021c422003-07-15 12:52:22 +00001437 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001438 {
1439 req.r.rtm_protocol = RTPROT_ZEBRA;
1440 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1441
paul7021c422003-07-15 12:52:22 +00001442 if (discard)
paul595db7f2003-05-25 21:35:06 +00001443 {
1444 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
1445 req.r.rtm_type = RTN_BLACKHOLE;
1446 else if (rib->flags & ZEBRA_FLAG_REJECT)
1447 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001448 else
1449 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1450 }
paul595db7f2003-05-25 21:35:06 +00001451 else
paul7021c422003-07-15 12:52:22 +00001452 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001453 }
1454
1455 addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1456
1457 /* Metric. */
1458 addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
1459
1460 if (discard)
1461 {
1462 if (cmd == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +00001463 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1464 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001465 goto skip;
1466 }
1467
1468 /* Multipath case. */
1469 if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
1470 {
1471 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
paul7021c422003-07-15 12:52:22 +00001472 {
paul5ec90d22003-06-19 01:41:37 +00001473
paul7021c422003-07-15 12:52:22 +00001474 if ((cmd == RTM_NEWROUTE
1475 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1476 || (cmd == RTM_DELROUTE
1477 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1478 {
paul5ec90d22003-06-19 01:41:37 +00001479
paul7021c422003-07-15 12:52:22 +00001480 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1481 {
1482 if (IS_ZEBRA_DEBUG_KERNEL)
1483 {
ajsb6178002004-12-07 21:12:56 +00001484 zlog_debug
paul7021c422003-07-15 12:52:22 +00001485 ("netlink_route_multipath() (recursive, 1 hop): "
hasso206d8052005-04-09 16:38:51 +00001486 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001487#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001488 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001489 inet6_ntoa (p->u.prefix6),
1490#else
1491 inet_ntoa (p->u.prefix4),
1492#endif /* HAVE_IPV6 */
1493
1494 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001495 }
paul5ec90d22003-06-19 01:41:37 +00001496
paul7021c422003-07-15 12:52:22 +00001497 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1498 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001499 {
1500 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1501 &nexthop->rgate.ipv4, bytelen);
Paul Jakma7514fb72007-05-02 16:05:35 +00001502 if (nexthop->src.ipv4.s_addr)
1503 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1504 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001505 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);
Paul Jakma7514fb72007-05-02 16:05:35 +00001534 if ((nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1535 || nexthop->rtype == NEXTHOP_TYPE_IFINDEX)
1536 && nexthop->src.ipv4.s_addr)
1537 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1538 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001539
1540 if (IS_ZEBRA_DEBUG_KERNEL)
1541 zlog_debug("netlink_route_multipath() (recursive, "
1542 "1 hop): nexthop via if %u",
1543 nexthop->rifindex);
1544 }
paul7021c422003-07-15 12:52:22 +00001545 }
1546 else
1547 {
1548 if (IS_ZEBRA_DEBUG_KERNEL)
1549 {
ajsb6178002004-12-07 21:12:56 +00001550 zlog_debug
hasso206d8052005-04-09 16:38:51 +00001551 ("netlink_route_multipath() (single hop): "
1552 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001553#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001554 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001555 inet6_ntoa (p->u.prefix6),
1556#else
1557 inet_ntoa (p->u.prefix4),
1558#endif /* HAVE_IPV6 */
1559 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001560 }
paul5ec90d22003-06-19 01:41:37 +00001561
paul7021c422003-07-15 12:52:22 +00001562 if (nexthop->type == NEXTHOP_TYPE_IPV4
1563 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001564 {
1565 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1566 &nexthop->gate.ipv4, bytelen);
Paul Jakma7514fb72007-05-02 16:05:35 +00001567 if (nexthop->src.ipv4.s_addr)
1568 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1569 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001570
1571 if (IS_ZEBRA_DEBUG_KERNEL)
1572 zlog_debug("netlink_route_multipath() (single hop): "
1573 "nexthop via %s if %u",
1574 inet_ntoa (nexthop->gate.ipv4),
1575 nexthop->ifindex);
1576 }
paul718e3742002-12-13 20:15:29 +00001577#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001578 if (nexthop->type == NEXTHOP_TYPE_IPV6
1579 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1580 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001581 {
1582 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1583 &nexthop->gate.ipv6, bytelen);
1584
1585 if (IS_ZEBRA_DEBUG_KERNEL)
1586 zlog_debug("netlink_route_multipath() (single hop): "
1587 "nexthop via %s if %u",
1588 inet6_ntoa (nexthop->gate.ipv6),
1589 nexthop->ifindex);
1590 }
paul718e3742002-12-13 20:15:29 +00001591#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001592 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1593 || nexthop->type == NEXTHOP_TYPE_IFNAME
Paul Jakma7514fb72007-05-02 16:05:35 +00001594 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1595 {
1596 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1597
1598 if (nexthop->src.ipv4.s_addr)
1599 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1600 &nexthop->src.ipv4, bytelen);
1601
1602 if (IS_ZEBRA_DEBUG_KERNEL)
1603 zlog_debug("netlink_route_multipath() (single hop): "
1604 "nexthop via if %u", nexthop->ifindex);
1605 }
1606 else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
paul7021c422003-07-15 12:52:22 +00001607 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001608 {
1609 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1610
1611 if (IS_ZEBRA_DEBUG_KERNEL)
1612 zlog_debug("netlink_route_multipath() (single hop): "
1613 "nexthop via if %u", nexthop->ifindex);
1614 }
paul7021c422003-07-15 12:52:22 +00001615 }
paul718e3742002-12-13 20:15:29 +00001616
paul7021c422003-07-15 12:52:22 +00001617 if (cmd == RTM_NEWROUTE)
1618 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001619
paul7021c422003-07-15 12:52:22 +00001620 nexthop_num++;
1621 break;
1622 }
1623 }
paul718e3742002-12-13 20:15:29 +00001624 }
1625 else
1626 {
1627 char buf[1024];
1628 struct rtattr *rta = (void *) buf;
1629 struct rtnexthop *rtnh;
Paul Jakma7514fb72007-05-02 16:05:35 +00001630 union g_addr *src = NULL;
paul718e3742002-12-13 20:15:29 +00001631
1632 rta->rta_type = RTA_MULTIPATH;
paul7021c422003-07-15 12:52:22 +00001633 rta->rta_len = RTA_LENGTH (0);
1634 rtnh = RTA_DATA (rta);
paul718e3742002-12-13 20:15:29 +00001635
1636 nexthop_num = 0;
1637 for (nexthop = rib->nexthop;
paul7021c422003-07-15 12:52:22 +00001638 nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM);
1639 nexthop = nexthop->next)
1640 {
1641 if ((cmd == RTM_NEWROUTE
1642 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1643 || (cmd == RTM_DELROUTE
1644 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1645 {
1646 nexthop_num++;
paul718e3742002-12-13 20:15:29 +00001647
paul7021c422003-07-15 12:52:22 +00001648 rtnh->rtnh_len = sizeof (*rtnh);
1649 rtnh->rtnh_flags = 0;
1650 rtnh->rtnh_hops = 0;
1651 rta->rta_len += rtnh->rtnh_len;
paul718e3742002-12-13 20:15:29 +00001652
paul7021c422003-07-15 12:52:22 +00001653 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1654 {
1655 if (IS_ZEBRA_DEBUG_KERNEL)
1656 {
ajsb6178002004-12-07 21:12:56 +00001657 zlog_debug ("netlink_route_multipath() "
hasso206d8052005-04-09 16:38:51 +00001658 "(recursive, multihop): %s %s/%d type %s",
hasso1ada8192005-06-12 11:28:18 +00001659 lookup (nlmsg_str, cmd),
1660#ifdef HAVE_IPV6
1661 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
1662 inet6_ntoa (p->u.prefix6),
1663#else
1664 inet_ntoa (p->u.prefix4),
1665#endif /* HAVE_IPV6 */
hasso206d8052005-04-09 16:38:51 +00001666 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001667 }
1668 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1669 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
1670 {
1671 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1672 &nexthop->rgate.ipv4, bytelen);
1673 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
hasso206d8052005-04-09 16:38:51 +00001674
Paul Jakma7514fb72007-05-02 16:05:35 +00001675 if (nexthop->src.ipv4.s_addr)
1676 src = &nexthop->src;
1677
hasso206d8052005-04-09 16:38:51 +00001678 if (IS_ZEBRA_DEBUG_KERNEL)
1679 zlog_debug("netlink_route_multipath() (recursive, "
1680 "multihop): nexthop via %s if %u",
1681 inet_ntoa (nexthop->rgate.ipv4),
1682 nexthop->rifindex);
paul7021c422003-07-15 12:52:22 +00001683 }
paul718e3742002-12-13 20:15:29 +00001684#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001685 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1686 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
1687 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001688 {
1689 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1690 &nexthop->rgate.ipv6, bytelen);
1691
1692 if (IS_ZEBRA_DEBUG_KERNEL)
1693 zlog_debug("netlink_route_multipath() (recursive, "
1694 "multihop): nexthop via %s if %u",
1695 inet6_ntoa (nexthop->rgate.ipv6),
1696 nexthop->rifindex);
1697 }
paul718e3742002-12-13 20:15:29 +00001698#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001699 /* ifindex */
Paul Jakma7514fb72007-05-02 16:05:35 +00001700 if (nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1701 || nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1702 || nexthop->rtype == NEXTHOP_TYPE_IFNAME)
1703 {
1704 rtnh->rtnh_ifindex = nexthop->rifindex;
1705 if (nexthop->src.ipv4.s_addr)
1706 src = &nexthop->src;
1707
1708 if (IS_ZEBRA_DEBUG_KERNEL)
1709 zlog_debug("netlink_route_multipath() (recursive, "
1710 "multihop): nexthop via if %u",
1711 nexthop->rifindex);
1712 }
1713 else if (nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
paul7021c422003-07-15 12:52:22 +00001714 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001715 {
1716 rtnh->rtnh_ifindex = nexthop->rifindex;
1717
1718 if (IS_ZEBRA_DEBUG_KERNEL)
1719 zlog_debug("netlink_route_multipath() (recursive, "
1720 "multihop): nexthop via if %u",
1721 nexthop->rifindex);
1722 }
paul7021c422003-07-15 12:52:22 +00001723 else
hasso206d8052005-04-09 16:38:51 +00001724 {
1725 rtnh->rtnh_ifindex = 0;
1726 }
paul7021c422003-07-15 12:52:22 +00001727 }
1728 else
1729 {
1730 if (IS_ZEBRA_DEBUG_KERNEL)
1731 {
hasso206d8052005-04-09 16:38:51 +00001732 zlog_debug ("netlink_route_multipath() (multihop): "
1733 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001734#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001735 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001736 inet6_ntoa (p->u.prefix6),
1737#else
1738 inet_ntoa (p->u.prefix4),
1739#endif /* HAVE_IPV6 */
1740 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001741 }
1742 if (nexthop->type == NEXTHOP_TYPE_IPV4
1743 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1744 {
hasso206d8052005-04-09 16:38:51 +00001745 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1746 &nexthop->gate.ipv4, bytelen);
1747 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
1748
Paul Jakma7514fb72007-05-02 16:05:35 +00001749 if (nexthop->src.ipv4.s_addr)
1750 src = &nexthop->src;
1751
hasso206d8052005-04-09 16:38:51 +00001752 if (IS_ZEBRA_DEBUG_KERNEL)
1753 zlog_debug("netlink_route_multipath() (multihop): "
1754 "nexthop via %s if %u",
1755 inet_ntoa (nexthop->gate.ipv4),
1756 nexthop->ifindex);
paul7021c422003-07-15 12:52:22 +00001757 }
paul718e3742002-12-13 20:15:29 +00001758#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001759 if (nexthop->type == NEXTHOP_TYPE_IPV6
1760 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1761 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001762 {
1763 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1764 &nexthop->gate.ipv6, bytelen);
1765
1766 if (IS_ZEBRA_DEBUG_KERNEL)
1767 zlog_debug("netlink_route_multipath() (multihop): "
1768 "nexthop via %s if %u",
1769 inet6_ntoa (nexthop->gate.ipv6),
1770 nexthop->ifindex);
1771 }
paul718e3742002-12-13 20:15:29 +00001772#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001773 /* ifindex */
Paul Jakma7514fb72007-05-02 16:05:35 +00001774 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1775 || nexthop->type == NEXTHOP_TYPE_IFINDEX
1776 || nexthop->type == NEXTHOP_TYPE_IFNAME)
1777 {
1778 rtnh->rtnh_ifindex = nexthop->ifindex;
1779 if (nexthop->src.ipv4.s_addr)
1780 src = &nexthop->src;
1781 if (IS_ZEBRA_DEBUG_KERNEL)
1782 zlog_debug("netlink_route_multipath() (multihop): "
1783 "nexthop via if %u", nexthop->ifindex);
1784 }
1785 else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
paul7021c422003-07-15 12:52:22 +00001786 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001787 {
1788 rtnh->rtnh_ifindex = nexthop->ifindex;
1789
1790 if (IS_ZEBRA_DEBUG_KERNEL)
1791 zlog_debug("netlink_route_multipath() (multihop): "
1792 "nexthop via if %u", nexthop->ifindex);
1793 }
paul7021c422003-07-15 12:52:22 +00001794 else
hasso206d8052005-04-09 16:38:51 +00001795 {
1796 rtnh->rtnh_ifindex = 0;
1797 }
paul7021c422003-07-15 12:52:22 +00001798 }
1799 rtnh = RTNH_NEXT (rtnh);
paul718e3742002-12-13 20:15:29 +00001800
paul7021c422003-07-15 12:52:22 +00001801 if (cmd == RTM_NEWROUTE)
1802 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1803 }
1804 }
Paul Jakma7514fb72007-05-02 16:05:35 +00001805 if (src)
1806 addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen);
paul718e3742002-12-13 20:15:29 +00001807
1808 if (rta->rta_len > RTA_LENGTH (0))
paul7021c422003-07-15 12:52:22 +00001809 addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta),
1810 RTA_PAYLOAD (rta));
paul718e3742002-12-13 20:15:29 +00001811 }
1812
1813 /* If there is no useful nexthop then return. */
1814 if (nexthop_num == 0)
1815 {
1816 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001817 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +00001818 return 0;
1819 }
1820
paul7021c422003-07-15 12:52:22 +00001821skip:
paul718e3742002-12-13 20:15:29 +00001822
1823 /* Destination netlink address. */
1824 memset (&snl, 0, sizeof snl);
1825 snl.nl_family = AF_NETLINK;
1826
paul718e3742002-12-13 20:15:29 +00001827 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001828 return netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001829}
1830
1831int
1832kernel_add_ipv4 (struct prefix *p, struct rib *rib)
1833{
1834 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
1835}
1836
1837int
1838kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
1839{
1840 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
1841}
1842
1843#ifdef HAVE_IPV6
1844int
1845kernel_add_ipv6 (struct prefix *p, struct rib *rib)
1846{
1847 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
1848}
1849
1850int
1851kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
1852{
1853 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
1854}
1855
1856/* Delete IPv6 route from the kernel. */
1857int
1858kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul6621ca82005-11-23 13:02:08 +00001859 unsigned int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +00001860{
paul7021c422003-07-15 12:52:22 +00001861 return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix,
1862 dest->prefixlen, gate, index, flags, table);
paul718e3742002-12-13 20:15:29 +00001863}
1864#endif /* HAVE_IPV6 */
1865
1866/* Interface address modification. */
1867int
1868netlink_address (int cmd, int family, struct interface *ifp,
paul7021c422003-07-15 12:52:22 +00001869 struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001870{
1871 int bytelen;
1872 struct prefix *p;
1873
paul7021c422003-07-15 12:52:22 +00001874 struct
paul718e3742002-12-13 20:15:29 +00001875 {
1876 struct nlmsghdr n;
1877 struct ifaddrmsg ifa;
1878 char buf[1024];
1879 } req;
1880
1881 p = ifc->address;
1882 memset (&req, 0, sizeof req);
1883
1884 bytelen = (family == AF_INET ? 4 : 16);
1885
paul7021c422003-07-15 12:52:22 +00001886 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +00001887 req.n.nlmsg_flags = NLM_F_REQUEST;
1888 req.n.nlmsg_type = cmd;
1889 req.ifa.ifa_family = family;
1890
1891 req.ifa.ifa_index = ifp->ifindex;
1892 req.ifa.ifa_prefixlen = p->prefixlen;
1893
1894 addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1895
1896 if (family == AF_INET && cmd == RTM_NEWADDR)
1897 {
Andrew J. Schorre4529632006-12-12 19:18:21 +00001898 if (!CONNECTED_PEER(ifc) && ifc->destination)
paul7021c422003-07-15 12:52:22 +00001899 {
1900 p = ifc->destination;
1901 addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
1902 bytelen);
1903 }
paul718e3742002-12-13 20:15:29 +00001904 }
1905
1906 if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
1907 SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
paul7021c422003-07-15 12:52:22 +00001908
paul718e3742002-12-13 20:15:29 +00001909 if (ifc->label)
1910 addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
paul7021c422003-07-15 12:52:22 +00001911 strlen (ifc->label) + 1);
paul718e3742002-12-13 20:15:29 +00001912
1913 return netlink_talk (&req.n, &netlink_cmd);
1914}
1915
1916int
1917kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
1918{
1919 return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
1920}
1921
1922int
1923kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
1924{
1925 return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
1926}
1927
paul718e3742002-12-13 20:15:29 +00001928
1929extern struct thread_master *master;
1930
1931/* Kernel route reflection. */
1932int
1933kernel_read (struct thread *thread)
1934{
1935 int ret;
1936 int sock;
1937
1938 sock = THREAD_FD (thread);
1939 ret = netlink_parse_info (netlink_information_fetch, &netlink);
paulb21b19c2003-06-15 01:28:29 +00001940 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001941
1942 return 0;
1943}
1944
1945/* Exported interface function. This function simply calls
1946 netlink_socket (). */
1947void
paul6621ca82005-11-23 13:02:08 +00001948kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001949{
1950 unsigned long groups;
1951
paul7021c422003-07-15 12:52:22 +00001952 groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
paul718e3742002-12-13 20:15:29 +00001953#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001954 groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
paul718e3742002-12-13 20:15:29 +00001955#endif /* HAVE_IPV6 */
1956 netlink_socket (&netlink, groups);
1957 netlink_socket (&netlink_cmd, 0);
1958
1959 /* Register kernel socket. */
1960 if (netlink.sock > 0)
paulb21b19c2003-06-15 01:28:29 +00001961 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001962}