blob: 7652f80a99f65cecbc15b91aabdbbddd55058e97 [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
Stephen Hemminger1423c802008-08-14 17:59:25 +010055static const struct 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
Stephen Hemminger6072b242008-08-14 16:52:26 +010068static const 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
paulb21b19c2003-06-15 01:28:29 +000082extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000083
pauledd7c242003-06-04 13:59:38 +000084extern struct zebra_privs_t zserv_privs;
85
hassoc34b6b52004-08-31 13:41:49 +000086extern u_int32_t nl_rcvbufsize;
87
ajsd2fc8892005-04-02 18:38:43 +000088/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
89 names and ifindex values. */
90static void
91set_ifindex(struct interface *ifp, unsigned int ifi_index)
92{
93 struct interface *oifp;
94
95 if (((oifp = if_lookup_by_index(ifi_index)) != NULL) && (oifp != ifp))
96 {
97 if (ifi_index == IFINDEX_INTERNAL)
98 zlog_err("Netlink is setting interface %s ifindex to reserved "
99 "internal value %u", ifp->name, ifi_index);
100 else
101 {
102 if (IS_ZEBRA_DEBUG_KERNEL)
103 zlog_debug("interface index %d was renamed from %s to %s",
104 ifi_index, oifp->name, ifp->name);
105 if (if_is_up(oifp))
106 zlog_err("interface rename detected on up interface: index %d "
107 "was renamed from %s to %s, results are uncertain!",
108 ifi_index, oifp->name, ifp->name);
109 if_delete_update(oifp);
110 }
111 }
112 ifp->ifindex = ifi_index;
113}
114
Stephen Hemminger30afea32008-08-16 18:25:47 +0100115static int
116netlink_recvbuf (struct nlsock *nl, uint32_t newsize)
117{
118 u_int32_t oldsize;
119 socklen_t newlen = sizeof(newsize);
120 socklen_t oldlen = sizeof(oldsize);
121 int ret;
122
123 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
124 if (ret < 0)
125 {
126 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
127 safe_strerror (errno));
128 return -1;
129 }
130
131 ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize,
132 sizeof(nl_rcvbufsize));
133 if (ret < 0)
134 {
135 zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name,
136 safe_strerror (errno));
137 return -1;
138 }
139
140 ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
141 if (ret < 0)
142 {
143 zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
144 safe_strerror (errno));
145 return -1;
146 }
147
148 zlog (NULL, LOG_INFO,
149 "Setting netlink socket receive buffer size: %u -> %u",
150 oldsize, newsize);
151 return 0;
152}
153
paul718e3742002-12-13 20:15:29 +0000154/* Make socket for Linux netlink interface. */
155static int
156netlink_socket (struct nlsock *nl, unsigned long groups)
157{
158 int ret;
159 struct sockaddr_nl snl;
160 int sock;
161 int namelen;
ajs4be019d2005-01-29 16:12:41 +0000162 int save_errno;
paul718e3742002-12-13 20:15:29 +0000163
164 sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
165 if (sock < 0)
166 {
167 zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000168 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000169 return -1;
170 }
171
paul718e3742002-12-13 20:15:29 +0000172 memset (&snl, 0, sizeof snl);
173 snl.nl_family = AF_NETLINK;
174 snl.nl_groups = groups;
175
176 /* Bind the socket to the netlink structure for anything. */
paul7021c422003-07-15 12:52:22 +0000177 if (zserv_privs.change (ZPRIVS_RAISE))
178 {
179 zlog (NULL, LOG_ERR, "Can't raise privileges");
180 return -1;
181 }
pauledd7c242003-06-04 13:59:38 +0000182
paul718e3742002-12-13 20:15:29 +0000183 ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000184 save_errno = errno;
hasso55e7ecd2004-08-06 08:41:56 +0000185 if (zserv_privs.change (ZPRIVS_LOWER))
186 zlog (NULL, LOG_ERR, "Can't lower privileges");
187
paul718e3742002-12-13 20:15:29 +0000188 if (ret < 0)
189 {
paul7021c422003-07-15 12:52:22 +0000190 zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
ajs4be019d2005-01-29 16:12:41 +0000191 nl->name, snl.nl_groups, safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000192 close (sock);
193 return -1;
194 }
paul7021c422003-07-15 12:52:22 +0000195
paul718e3742002-12-13 20:15:29 +0000196 /* multiple netlink sockets will have different nl_pid */
197 namelen = sizeof snl;
hassoc9e52be2004-09-26 16:09:34 +0000198 ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);
paul718e3742002-12-13 20:15:29 +0000199 if (ret < 0 || namelen != sizeof snl)
200 {
201 zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
ajs6099b3b2004-11-20 02:06:59 +0000202 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000203 close (sock);
204 return -1;
205 }
206
207 nl->snl = snl;
208 nl->sock = sock;
209 return ret;
210}
211
212/* Get type specified information from netlink. */
213static int
214netlink_request (int family, int type, struct nlsock *nl)
215{
216 int ret;
217 struct sockaddr_nl snl;
ajs4be019d2005-01-29 16:12:41 +0000218 int save_errno;
paul718e3742002-12-13 20:15:29 +0000219
220 struct
221 {
222 struct nlmsghdr nlh;
223 struct rtgenmsg g;
224 } req;
225
226
227 /* Check netlink socket. */
228 if (nl->sock < 0)
229 {
230 zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
231 return -1;
232 }
233
234 memset (&snl, 0, sizeof snl);
235 snl.nl_family = AF_NETLINK;
236
ajsc05612b2005-10-01 16:36:54 +0000237 memset (&req, 0, sizeof req);
paul718e3742002-12-13 20:15:29 +0000238 req.nlh.nlmsg_len = sizeof req;
239 req.nlh.nlmsg_type = type;
240 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
Stephen Hemminger3d265b42008-08-16 17:30:39 +0100241 req.nlh.nlmsg_pid = nl->snl.nl_pid;
paul718e3742002-12-13 20:15:29 +0000242 req.nlh.nlmsg_seq = ++nl->seq;
243 req.g.rtgen_family = family;
pauledd7c242003-06-04 13:59:38 +0000244
245 /* linux appears to check capabilities on every message
246 * have to raise caps for every message sent
247 */
paul7021c422003-07-15 12:52:22 +0000248 if (zserv_privs.change (ZPRIVS_RAISE))
pauledd7c242003-06-04 13:59:38 +0000249 {
250 zlog (NULL, LOG_ERR, "Can't raise privileges");
251 return -1;
252 }
paul7021c422003-07-15 12:52:22 +0000253
254 ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
255 (struct sockaddr *) &snl, sizeof snl);
ajs4be019d2005-01-29 16:12:41 +0000256 save_errno = errno;
paul7021c422003-07-15 12:52:22 +0000257
258 if (zserv_privs.change (ZPRIVS_LOWER))
259 zlog (NULL, LOG_ERR, "Can't lower privileges");
260
paul718e3742002-12-13 20:15:29 +0000261 if (ret < 0)
paul7021c422003-07-15 12:52:22 +0000262 {
263 zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
ajs4be019d2005-01-29 16:12:41 +0000264 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +0000265 return -1;
266 }
pauledd7c242003-06-04 13:59:38 +0000267
paul718e3742002-12-13 20:15:29 +0000268 return 0;
269}
270
271/* Receive message from netlink interface and pass those information
272 to the given function. */
273static int
274netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *),
paul7021c422003-07-15 12:52:22 +0000275 struct nlsock *nl)
paul718e3742002-12-13 20:15:29 +0000276{
277 int status;
278 int ret = 0;
279 int error;
280
281 while (1)
282 {
283 char buf[4096];
284 struct iovec iov = { buf, sizeof buf };
285 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +0000286 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
paul718e3742002-12-13 20:15:29 +0000287 struct nlmsghdr *h;
paul7021c422003-07-15 12:52:22 +0000288
paul718e3742002-12-13 20:15:29 +0000289 status = recvmsg (nl->sock, &msg, 0);
paul718e3742002-12-13 20:15:29 +0000290 if (status < 0)
paul7021c422003-07-15 12:52:22 +0000291 {
Stephen Hemminger4c699472008-08-17 17:01:44 +0100292 if (errno == EINTR)
paul7021c422003-07-15 12:52:22 +0000293 continue;
Stephen Hemminger4c699472008-08-17 17:01:44 +0100294 if (errno == EWOULDBLOCK || errno == EAGAIN)
paul7021c422003-07-15 12:52:22 +0000295 break;
ajs4be019d2005-01-29 16:12:41 +0000296 zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s",
Stephen Hemminger4c699472008-08-17 17:01:44 +0100297 nl->name, safe_strerror(errno));
paul7021c422003-07-15 12:52:22 +0000298 continue;
299 }
paul718e3742002-12-13 20:15:29 +0000300
301 if (status == 0)
paul7021c422003-07-15 12:52:22 +0000302 {
303 zlog (NULL, LOG_ERR, "%s EOF", nl->name);
304 return -1;
305 }
paul718e3742002-12-13 20:15:29 +0000306
307 if (msg.msg_namelen != sizeof snl)
paul7021c422003-07-15 12:52:22 +0000308 {
309 zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
310 nl->name, msg.msg_namelen);
311 return -1;
312 }
paulb84d3a12003-11-17 10:31:01 +0000313
hasso206d8052005-04-09 16:38:51 +0000314 for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, (unsigned int) status);
paul7021c422003-07-15 12:52:22 +0000315 h = NLMSG_NEXT (h, status))
316 {
317 /* Finish of reading. */
318 if (h->nlmsg_type == NLMSG_DONE)
319 return ret;
paul718e3742002-12-13 20:15:29 +0000320
paul7021c422003-07-15 12:52:22 +0000321 /* Error handling. */
322 if (h->nlmsg_type == NLMSG_ERROR)
323 {
324 struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
Stephen Hemminger898987e2008-08-17 16:56:15 +0100325 int errnum = err->error;
326 int msg_type = err->msg.nlmsg_type;
paul7021c422003-07-15 12:52:22 +0000327
paul718e3742002-12-13 20:15:29 +0000328 /* If the error field is zero, then this is an ACK */
paul7021c422003-07-15 12:52:22 +0000329 if (err->error == 0)
paul718e3742002-12-13 20:15:29 +0000330 {
paul7021c422003-07-15 12:52:22 +0000331 if (IS_ZEBRA_DEBUG_KERNEL)
332 {
hasso1ada8192005-06-12 11:28:18 +0000333 zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
paul7021c422003-07-15 12:52:22 +0000334 __FUNCTION__, nl->name,
335 lookup (nlmsg_str, err->msg.nlmsg_type),
336 err->msg.nlmsg_type, err->msg.nlmsg_seq,
337 err->msg.nlmsg_pid);
paul718e3742002-12-13 20:15:29 +0000338 }
paul7021c422003-07-15 12:52:22 +0000339
340 /* return if not a multipart message, otherwise continue */
341 if (!(h->nlmsg_flags & NLM_F_MULTI))
342 {
343 return 0;
paul718e3742002-12-13 20:15:29 +0000344 }
paul7021c422003-07-15 12:52:22 +0000345 continue;
paul718e3742002-12-13 20:15:29 +0000346 }
paul7021c422003-07-15 12:52:22 +0000347
paul718e3742002-12-13 20:15:29 +0000348 if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
paul7021c422003-07-15 12:52:22 +0000349 {
350 zlog (NULL, LOG_ERR, "%s error: message truncated",
351 nl->name);
352 return -1;
353 }
pauld753e9e2003-01-22 19:45:50 +0000354
Stephen Hemminger898987e2008-08-17 16:56:15 +0100355 /* Deal with errors that occur because of races in link handling */
356 if (nl == &netlink_cmd
357 && ((msg_type == RTM_DELROUTE &&
358 (-errnum == ENODEV || -errnum == ESRCH))
359 || (msg_type == RTM_NEWROUTE && -errnum == EEXIST)))
360 {
361 if (IS_ZEBRA_DEBUG_KERNEL)
362 zlog_debug ("%s: error: %s type=%s(%u), seq=%u, pid=%u",
363 nl->name, safe_strerror (-errnum),
364 lookup (nlmsg_str, msg_type),
365 msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
366 return 0;
367 }
paul718e3742002-12-13 20:15:29 +0000368
Stephen Hemminger898987e2008-08-17 16:56:15 +0100369 zlog_err ("%s error: %s, type=%s(%u), seq=%u, pid=%u",
370 nl->name, safe_strerror (-errnum),
371 lookup (nlmsg_str, msg_type),
372 msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
paul7021c422003-07-15 12:52:22 +0000373 return -1;
374 }
paul718e3742002-12-13 20:15:29 +0000375
paul7021c422003-07-15 12:52:22 +0000376 /* OK we got netlink message. */
377 if (IS_ZEBRA_DEBUG_KERNEL)
hasso1ada8192005-06-12 11:28:18 +0000378 zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u",
paul7021c422003-07-15 12:52:22 +0000379 nl->name,
380 lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
381 h->nlmsg_seq, h->nlmsg_pid);
382
383 /* skip unsolicited messages originating from command socket */
384 if (nl != &netlink_cmd && h->nlmsg_pid == netlink_cmd.snl.nl_pid)
385 {
386 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000387 zlog_debug ("netlink_parse_info: %s packet comes from %s",
hasso1ada8192005-06-12 11:28:18 +0000388 netlink_cmd.name, nl->name);
paul7021c422003-07-15 12:52:22 +0000389 continue;
390 }
391
392 error = (*filter) (&snl, h);
393 if (error < 0)
394 {
395 zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
396 ret = error;
397 }
398 }
paul718e3742002-12-13 20:15:29 +0000399
400 /* After error care. */
401 if (msg.msg_flags & MSG_TRUNC)
paul7021c422003-07-15 12:52:22 +0000402 {
403 zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name);
404 continue;
405 }
paul718e3742002-12-13 20:15:29 +0000406 if (status)
paul7021c422003-07-15 12:52:22 +0000407 {
408 zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
409 status);
410 return -1;
411 }
paul718e3742002-12-13 20:15:29 +0000412 }
413 return ret;
414}
415
416/* Utility function for parse rtattr. */
417static void
paul7021c422003-07-15 12:52:22 +0000418netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
419 int len)
paul718e3742002-12-13 20:15:29 +0000420{
paul7021c422003-07-15 12:52:22 +0000421 while (RTA_OK (rta, len))
paul718e3742002-12-13 20:15:29 +0000422 {
423 if (rta->rta_type <= max)
paul7021c422003-07-15 12:52:22 +0000424 tb[rta->rta_type] = rta;
425 rta = RTA_NEXT (rta, len);
paul718e3742002-12-13 20:15:29 +0000426 }
427}
428
429/* Called from interface_lookup_netlink(). This function is only used
430 during bootstrap. */
Stephen Hemminger6072b242008-08-14 16:52:26 +0100431static int
paul718e3742002-12-13 20:15:29 +0000432netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h)
433{
434 int len;
435 struct ifinfomsg *ifi;
436 struct rtattr *tb[IFLA_MAX + 1];
437 struct interface *ifp;
438 char *name;
439 int i;
440
441 ifi = NLMSG_DATA (h);
442
443 if (h->nlmsg_type != RTM_NEWLINK)
444 return 0;
445
446 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
447 if (len < 0)
448 return -1;
449
450 /* Looking up interface name. */
451 memset (tb, 0, sizeof tb);
452 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000453
paul1e193152005-02-14 23:53:05 +0000454#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000455 /* check for wireless messages to ignore */
456 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
457 {
458 if (IS_ZEBRA_DEBUG_KERNEL)
459 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
460 return 0;
461 }
paul1e193152005-02-14 23:53:05 +0000462#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +0000463
paul718e3742002-12-13 20:15:29 +0000464 if (tb[IFLA_IFNAME] == NULL)
465 return -1;
paul7021c422003-07-15 12:52:22 +0000466 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +0000467
468 /* Add interface. */
469 ifp = if_get_by_name (name);
ajsd2fc8892005-04-02 18:38:43 +0000470 set_ifindex(ifp, ifi->ifi_index);
paul718e3742002-12-13 20:15:29 +0000471 ifp->flags = ifi->ifi_flags & 0x0000fffff;
Stephen Hemminger4308abb2008-12-01 14:19:38 -0800472 ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]);
paul718e3742002-12-13 20:15:29 +0000473 ifp->metric = 1;
474
475 /* Hardware type and address. */
476 ifp->hw_type = ifi->ifi_type;
477
478 if (tb[IFLA_ADDRESS])
479 {
480 int hw_addr_len;
481
paul7021c422003-07-15 12:52:22 +0000482 hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
paul718e3742002-12-13 20:15:29 +0000483
484 if (hw_addr_len > INTERFACE_HWADDR_MAX)
paul7021c422003-07-15 12:52:22 +0000485 zlog_warn ("Hardware address is too large: %d", hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000486 else
paul7021c422003-07-15 12:52:22 +0000487 {
488 ifp->hw_addr_len = hw_addr_len;
489 memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
paul718e3742002-12-13 20:15:29 +0000490
paul7021c422003-07-15 12:52:22 +0000491 for (i = 0; i < hw_addr_len; i++)
492 if (ifp->hw_addr[i] != 0)
493 break;
paul718e3742002-12-13 20:15:29 +0000494
paul7021c422003-07-15 12:52:22 +0000495 if (i == hw_addr_len)
496 ifp->hw_addr_len = 0;
497 else
498 ifp->hw_addr_len = hw_addr_len;
499 }
paul718e3742002-12-13 20:15:29 +0000500 }
501
502 if_add_update (ifp);
503
504 return 0;
505}
506
507/* Lookup interface IPv4/IPv6 address. */
Stephen Hemminger6072b242008-08-14 16:52:26 +0100508static int
paul718e3742002-12-13 20:15:29 +0000509netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h)
510{
511 int len;
512 struct ifaddrmsg *ifa;
paul7021c422003-07-15 12:52:22 +0000513 struct rtattr *tb[IFA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000514 struct interface *ifp;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000515 void *addr;
516 void *broad;
paul718e3742002-12-13 20:15:29 +0000517 u_char flags = 0;
518 char *label = NULL;
519
520 ifa = NLMSG_DATA (h);
521
paul7021c422003-07-15 12:52:22 +0000522 if (ifa->ifa_family != AF_INET
paul718e3742002-12-13 20:15:29 +0000523#ifdef HAVE_IPV6
524 && ifa->ifa_family != AF_INET6
525#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +0000526 )
paul718e3742002-12-13 20:15:29 +0000527 return 0;
528
529 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
530 return 0;
531
paul7021c422003-07-15 12:52:22 +0000532 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +0000533 if (len < 0)
534 return -1;
535
536 memset (tb, 0, sizeof tb);
537 netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
538
539 ifp = if_lookup_by_index (ifa->ifa_index);
540 if (ifp == NULL)
541 {
542 zlog_err ("netlink_interface_addr can't find interface by index %d",
paul7021c422003-07-15 12:52:22 +0000543 ifa->ifa_index);
paul718e3742002-12-13 20:15:29 +0000544 return -1;
545 }
546
paul7021c422003-07-15 12:52:22 +0000547 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
paul718e3742002-12-13 20:15:29 +0000548 {
paul00df0c12002-12-13 21:07:36 +0000549 char buf[BUFSIZ];
hasso206d8052005-04-09 16:38:51 +0000550 zlog_debug ("netlink_interface_addr %s %s:",
551 lookup (nlmsg_str, h->nlmsg_type), ifp->name);
paul718e3742002-12-13 20:15:29 +0000552 if (tb[IFA_LOCAL])
hasso206d8052005-04-09 16:38:51 +0000553 zlog_debug (" IFA_LOCAL %s/%d",
554 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]),
555 buf, BUFSIZ), ifa->ifa_prefixlen);
paul718e3742002-12-13 20:15:29 +0000556 if (tb[IFA_ADDRESS])
hasso206d8052005-04-09 16:38:51 +0000557 zlog_debug (" IFA_ADDRESS %s/%d",
558 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]),
559 buf, BUFSIZ), ifa->ifa_prefixlen);
paul718e3742002-12-13 20:15:29 +0000560 if (tb[IFA_BROADCAST])
hasso206d8052005-04-09 16:38:51 +0000561 zlog_debug (" IFA_BROADCAST %s/%d",
562 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]),
563 buf, BUFSIZ), ifa->ifa_prefixlen);
paul00df0c12002-12-13 21:07:36 +0000564 if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
ajsb6178002004-12-07 21:12:56 +0000565 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
pauld34b8992006-01-17 18:03:04 +0000566
567 if (tb[IFA_CACHEINFO])
568 {
569 struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]);
570 zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
571 ci->ifa_prefered, ci->ifa_valid);
572 }
paul718e3742002-12-13 20:15:29 +0000573 }
paul31a476c2003-09-29 19:54:53 +0000574
Andrew J. Schorre4529632006-12-12 19:18:21 +0000575 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
576 if (tb[IFA_LOCAL] == NULL)
577 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
paul31a476c2003-09-29 19:54:53 +0000578 if (tb[IFA_ADDRESS] == NULL)
579 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
580
Andrew J. Schorre4529632006-12-12 19:18:21 +0000581 /* local interface address */
582 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
583
584 /* is there a peer address? */
Andrew J. Schorre4529632006-12-12 19:18:21 +0000585 if (tb[IFA_ADDRESS] &&
vize068fd772007-08-10 06:25:20 +0000586 memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS])))
paul7021c422003-07-15 12:52:22 +0000587 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000588 broad = RTA_DATA(tb[IFA_ADDRESS]);
589 SET_FLAG (flags, ZEBRA_IFA_PEER);
paul7021c422003-07-15 12:52:22 +0000590 }
paul31a476c2003-09-29 19:54:53 +0000591 else
Andrew J. Schorre4529632006-12-12 19:18:21 +0000592 /* seeking a broadcast address */
593 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL);
paul00df0c12002-12-13 21:07:36 +0000594
Paul Jakma27b47252006-07-02 16:38:54 +0000595 /* addr is primary key, SOL if we don't have one */
596 if (addr == NULL)
597 {
598 zlog_debug ("%s: NULL address", __func__);
599 return -1;
600 }
601
paul718e3742002-12-13 20:15:29 +0000602 /* Flags. */
603 if (ifa->ifa_flags & IFA_F_SECONDARY)
604 SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
605
606 /* Label */
607 if (tb[IFA_LABEL])
608 label = (char *) RTA_DATA (tb[IFA_LABEL]);
609
610 if (ifp && label && strcmp (ifp->name, label) == 0)
611 label = NULL;
612
613 /* Register interface address to the interface. */
614 if (ifa->ifa_family == AF_INET)
615 {
paul7021c422003-07-15 12:52:22 +0000616 if (h->nlmsg_type == RTM_NEWADDR)
617 connected_add_ipv4 (ifp, flags,
618 (struct in_addr *) addr, ifa->ifa_prefixlen,
619 (struct in_addr *) broad, label);
620 else
621 connected_delete_ipv4 (ifp, flags,
622 (struct in_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000623 (struct in_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000624 }
625#ifdef HAVE_IPV6
626 if (ifa->ifa_family == AF_INET6)
627 {
628 if (h->nlmsg_type == RTM_NEWADDR)
Andrew J. Schorre4529632006-12-12 19:18:21 +0000629 connected_add_ipv6 (ifp, flags,
paul7021c422003-07-15 12:52:22 +0000630 (struct in6_addr *) addr, ifa->ifa_prefixlen,
paul0752ef02005-11-03 12:35:21 +0000631 (struct in6_addr *) broad, label);
paul718e3742002-12-13 20:15:29 +0000632 else
paul7021c422003-07-15 12:52:22 +0000633 connected_delete_ipv6 (ifp,
634 (struct in6_addr *) addr, ifa->ifa_prefixlen,
635 (struct in6_addr *) broad);
paul718e3742002-12-13 20:15:29 +0000636 }
paul7021c422003-07-15 12:52:22 +0000637#endif /* HAVE_IPV6 */
paul718e3742002-12-13 20:15:29 +0000638
639 return 0;
640}
641
642/* Looking up routing table by netlink interface. */
Stephen Hemminger6072b242008-08-14 16:52:26 +0100643static int
paul718e3742002-12-13 20:15:29 +0000644netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h)
645{
646 int len;
647 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000648 struct rtattr *tb[RTA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000649 u_char flags = 0;
paul7021c422003-07-15 12:52:22 +0000650
651 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000652
653 int index;
654 int table;
hasso34195bf2004-04-06 12:07:06 +0000655 int metric;
656
paul718e3742002-12-13 20:15:29 +0000657 void *dest;
658 void *gate;
Paul Jakma7514fb72007-05-02 16:05:35 +0000659 void *src;
paul718e3742002-12-13 20:15:29 +0000660
661 rtm = NLMSG_DATA (h);
662
663 if (h->nlmsg_type != RTM_NEWROUTE)
664 return 0;
665 if (rtm->rtm_type != RTN_UNICAST)
666 return 0;
667
668 table = rtm->rtm_table;
paul7021c422003-07-15 12:52:22 +0000669#if 0 /* we weed them out later in rib_weed_tables () */
paulb21b19c2003-06-15 01:28:29 +0000670 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000671 return 0;
672#endif
673
paul7021c422003-07-15 12:52:22 +0000674 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000675 if (len < 0)
676 return -1;
677
678 memset (tb, 0, sizeof tb);
679 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
680
681 if (rtm->rtm_flags & RTM_F_CLONED)
682 return 0;
683 if (rtm->rtm_protocol == RTPROT_REDIRECT)
684 return 0;
685 if (rtm->rtm_protocol == RTPROT_KERNEL)
686 return 0;
687
688 if (rtm->rtm_src_len != 0)
689 return 0;
690
691 /* Route which inserted by Zebra. */
692 if (rtm->rtm_protocol == RTPROT_ZEBRA)
693 flags |= ZEBRA_FLAG_SELFROUTE;
paul7021c422003-07-15 12:52:22 +0000694
paul718e3742002-12-13 20:15:29 +0000695 index = 0;
hasso34195bf2004-04-06 12:07:06 +0000696 metric = 0;
paul718e3742002-12-13 20:15:29 +0000697 dest = NULL;
698 gate = NULL;
Paul Jakma7514fb72007-05-02 16:05:35 +0000699 src = NULL;
paul718e3742002-12-13 20:15:29 +0000700
701 if (tb[RTA_OIF])
702 index = *(int *) RTA_DATA (tb[RTA_OIF]);
703
704 if (tb[RTA_DST])
705 dest = RTA_DATA (tb[RTA_DST]);
706 else
707 dest = anyaddr;
708
Paul Jakma7514fb72007-05-02 16:05:35 +0000709 if (tb[RTA_PREFSRC])
710 src = RTA_DATA (tb[RTA_PREFSRC]);
711
paul718e3742002-12-13 20:15:29 +0000712 /* Multipath treatment is needed. */
713 if (tb[RTA_GATEWAY])
714 gate = RTA_DATA (tb[RTA_GATEWAY]);
715
hasso34195bf2004-04-06 12:07:06 +0000716 if (tb[RTA_PRIORITY])
717 metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]);
718
paul718e3742002-12-13 20:15:29 +0000719 if (rtm->rtm_family == AF_INET)
720 {
721 struct prefix_ipv4 p;
722 p.family = AF_INET;
723 memcpy (&p.prefix, dest, 4);
724 p.prefixlen = rtm->rtm_dst_len;
725
Paul Jakma7514fb72007-05-02 16:05:35 +0000726 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index, table, metric, 0);
paul718e3742002-12-13 20:15:29 +0000727 }
728#ifdef HAVE_IPV6
729 if (rtm->rtm_family == AF_INET6)
730 {
731 struct prefix_ipv6 p;
732 p.family = AF_INET6;
733 memcpy (&p.prefix, dest, 16);
734 p.prefixlen = rtm->rtm_dst_len;
735
hassobe61c4e2005-08-27 06:05:47 +0000736 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table,
737 metric, 0);
paul718e3742002-12-13 20:15:29 +0000738 }
739#endif /* HAVE_IPV6 */
740
741 return 0;
742}
743
Stephen Hemminger1423c802008-08-14 17:59:25 +0100744static const struct message rtproto_str[] = {
paul718e3742002-12-13 20:15:29 +0000745 {RTPROT_REDIRECT, "redirect"},
746 {RTPROT_KERNEL, "kernel"},
747 {RTPROT_BOOT, "boot"},
748 {RTPROT_STATIC, "static"},
749 {RTPROT_GATED, "GateD"},
750 {RTPROT_RA, "router advertisement"},
751 {RTPROT_MRT, "MRT"},
752 {RTPROT_ZEBRA, "Zebra"},
753#ifdef RTPROT_BIRD
754 {RTPROT_BIRD, "BIRD"},
755#endif /* RTPROT_BIRD */
756 {0, NULL}
757};
758
759/* Routing information change from the kernel. */
Stephen Hemminger6072b242008-08-14 16:52:26 +0100760static int
paul718e3742002-12-13 20:15:29 +0000761netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
762{
763 int len;
764 struct rtmsg *rtm;
paul7021c422003-07-15 12:52:22 +0000765 struct rtattr *tb[RTA_MAX + 1];
766
767 char anyaddr[16] = { 0 };
paul718e3742002-12-13 20:15:29 +0000768
769 int index;
770 int table;
771 void *dest;
772 void *gate;
Paul Jakma7514fb72007-05-02 16:05:35 +0000773 void *src;
paul718e3742002-12-13 20:15:29 +0000774
775 rtm = NLMSG_DATA (h);
776
paul7021c422003-07-15 12:52:22 +0000777 if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
paul718e3742002-12-13 20:15:29 +0000778 {
779 /* If this is not route add/delete message print warning. */
780 zlog_warn ("Kernel message: %d\n", h->nlmsg_type);
781 return 0;
782 }
783
784 /* Connected route. */
785 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +0000786 zlog_debug ("%s %s %s proto %s",
paul7021c422003-07-15 12:52:22 +0000787 h->nlmsg_type ==
788 RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
789 rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
790 rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
791 lookup (rtproto_str, rtm->rtm_protocol));
paul718e3742002-12-13 20:15:29 +0000792
793 if (rtm->rtm_type != RTN_UNICAST)
794 {
795 return 0;
796 }
797
798 table = rtm->rtm_table;
paulb21b19c2003-06-15 01:28:29 +0000799 if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
paul718e3742002-12-13 20:15:29 +0000800 {
801 return 0;
802 }
803
paul7021c422003-07-15 12:52:22 +0000804 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
paul718e3742002-12-13 20:15:29 +0000805 if (len < 0)
806 return -1;
807
808 memset (tb, 0, sizeof tb);
809 netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
810
811 if (rtm->rtm_flags & RTM_F_CLONED)
812 return 0;
813 if (rtm->rtm_protocol == RTPROT_REDIRECT)
814 return 0;
815 if (rtm->rtm_protocol == RTPROT_KERNEL)
816 return 0;
817
818 if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
819 return 0;
820
821 if (rtm->rtm_src_len != 0)
822 {
823 zlog_warn ("netlink_route_change(): no src len");
824 return 0;
825 }
paul7021c422003-07-15 12:52:22 +0000826
paul718e3742002-12-13 20:15:29 +0000827 index = 0;
828 dest = NULL;
829 gate = NULL;
Paul Jakma7514fb72007-05-02 16:05:35 +0000830 src = NULL;
paul718e3742002-12-13 20:15:29 +0000831
832 if (tb[RTA_OIF])
833 index = *(int *) RTA_DATA (tb[RTA_OIF]);
834
835 if (tb[RTA_DST])
836 dest = RTA_DATA (tb[RTA_DST]);
837 else
838 dest = anyaddr;
839
840 if (tb[RTA_GATEWAY])
841 gate = RTA_DATA (tb[RTA_GATEWAY]);
842
Paul Jakma7514fb72007-05-02 16:05:35 +0000843 if (tb[RTA_PREFSRC])
844 src = RTA_DATA (tb[RTA_PREFSRC]);
845
paul718e3742002-12-13 20:15:29 +0000846 if (rtm->rtm_family == AF_INET)
847 {
848 struct prefix_ipv4 p;
849 p.family = AF_INET;
850 memcpy (&p.prefix, dest, 4);
851 p.prefixlen = rtm->rtm_dst_len;
852
853 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000854 {
855 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000856 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000857 inet_ntoa (p.prefix), p.prefixlen);
858 else
ajsb6178002004-12-07 21:12:56 +0000859 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000860 inet_ntoa (p.prefix), p.prefixlen);
861 }
paul718e3742002-12-13 20:15:29 +0000862
863 if (h->nlmsg_type == RTM_NEWROUTE)
Paul Jakma7514fb72007-05-02 16:05:35 +0000864 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, table, 0, 0);
paul718e3742002-12-13 20:15:29 +0000865 else
paul7021c422003-07-15 12:52:22 +0000866 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
paul718e3742002-12-13 20:15:29 +0000867 }
868
869#ifdef HAVE_IPV6
870 if (rtm->rtm_family == AF_INET6)
871 {
872 struct prefix_ipv6 p;
873 char buf[BUFSIZ];
874
875 p.family = AF_INET6;
876 memcpy (&p.prefix, dest, 16);
877 p.prefixlen = rtm->rtm_dst_len;
878
879 if (IS_ZEBRA_DEBUG_KERNEL)
paul7021c422003-07-15 12:52:22 +0000880 {
881 if (h->nlmsg_type == RTM_NEWROUTE)
ajsb6178002004-12-07 21:12:56 +0000882 zlog_debug ("RTM_NEWROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000883 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
884 p.prefixlen);
885 else
ajsb6178002004-12-07 21:12:56 +0000886 zlog_debug ("RTM_DELROUTE %s/%d",
paul7021c422003-07-15 12:52:22 +0000887 inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ),
888 p.prefixlen);
889 }
paul718e3742002-12-13 20:15:29 +0000890
891 if (h->nlmsg_type == RTM_NEWROUTE)
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100892 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0);
paul718e3742002-12-13 20:15:29 +0000893 else
Mathieu Goessensd13c3b42009-06-23 15:59:45 +0100894 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table);
paul718e3742002-12-13 20:15:29 +0000895 }
896#endif /* HAVE_IPV6 */
897
898 return 0;
899}
900
Stephen Hemminger6072b242008-08-14 16:52:26 +0100901static int
paul718e3742002-12-13 20:15:29 +0000902netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h)
903{
904 int len;
905 struct ifinfomsg *ifi;
paul7021c422003-07-15 12:52:22 +0000906 struct rtattr *tb[IFLA_MAX + 1];
paul718e3742002-12-13 20:15:29 +0000907 struct interface *ifp;
908 char *name;
909
910 ifi = NLMSG_DATA (h);
911
paul7021c422003-07-15 12:52:22 +0000912 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
paul718e3742002-12-13 20:15:29 +0000913 {
914 /* If this is not link add/delete message so print warning. */
915 zlog_warn ("netlink_link_change: wrong kernel message %d\n",
paul7021c422003-07-15 12:52:22 +0000916 h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +0000917 return 0;
918 }
919
920 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
921 if (len < 0)
922 return -1;
923
924 /* Looking up interface name. */
925 memset (tb, 0, sizeof tb);
926 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
paulc15cb242005-01-24 09:05:27 +0000927
paul1e193152005-02-14 23:53:05 +0000928#ifdef IFLA_WIRELESS
paulc15cb242005-01-24 09:05:27 +0000929 /* check for wireless messages to ignore */
930 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
931 {
932 if (IS_ZEBRA_DEBUG_KERNEL)
933 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
934 return 0;
935 }
paul1e193152005-02-14 23:53:05 +0000936#endif /* IFLA_WIRELESS */
paulc15cb242005-01-24 09:05:27 +0000937
paul718e3742002-12-13 20:15:29 +0000938 if (tb[IFLA_IFNAME] == NULL)
939 return -1;
paul7021c422003-07-15 12:52:22 +0000940 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
paul718e3742002-12-13 20:15:29 +0000941
942 /* Add interface. */
943 if (h->nlmsg_type == RTM_NEWLINK)
944 {
945 ifp = if_lookup_by_name (name);
946
paul7021c422003-07-15 12:52:22 +0000947 if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
948 {
949 if (ifp == NULL)
950 ifp = if_get_by_name (name);
paul718e3742002-12-13 20:15:29 +0000951
ajsd2fc8892005-04-02 18:38:43 +0000952 set_ifindex(ifp, ifi->ifi_index);
paul7021c422003-07-15 12:52:22 +0000953 ifp->flags = ifi->ifi_flags & 0x0000fffff;
paul44145db2004-05-09 11:00:23 +0000954 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +0000955 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +0000956
paul7021c422003-07-15 12:52:22 +0000957 /* If new link is added. */
958 if_add_update (ifp);
959 }
paul718e3742002-12-13 20:15:29 +0000960 else
paul7021c422003-07-15 12:52:22 +0000961 {
962 /* Interface status change. */
ajsd2fc8892005-04-02 18:38:43 +0000963 set_ifindex(ifp, ifi->ifi_index);
paul44145db2004-05-09 11:00:23 +0000964 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
paul7021c422003-07-15 12:52:22 +0000965 ifp->metric = 1;
paul718e3742002-12-13 20:15:29 +0000966
paul7021c422003-07-15 12:52:22 +0000967 if (if_is_operative (ifp))
968 {
969 ifp->flags = ifi->ifi_flags & 0x0000fffff;
970 if (!if_is_operative (ifp))
971 if_down (ifp);
ajsa608bbf2005-03-29 17:03:49 +0000972 else
973 /* Must notify client daemons of new interface status. */
974 zebra_interface_up_update (ifp);
paul7021c422003-07-15 12:52:22 +0000975 }
976 else
977 {
978 ifp->flags = ifi->ifi_flags & 0x0000fffff;
979 if (if_is_operative (ifp))
980 if_up (ifp);
981 }
982 }
paul718e3742002-12-13 20:15:29 +0000983 }
984 else
985 {
986 /* RTM_DELLINK. */
987 ifp = if_lookup_by_name (name);
988
989 if (ifp == NULL)
paul7021c422003-07-15 12:52:22 +0000990 {
991 zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find",
paul718e3742002-12-13 20:15:29 +0000992 name);
paul7021c422003-07-15 12:52:22 +0000993 return 0;
994 }
995
paul718e3742002-12-13 20:15:29 +0000996 if_delete_update (ifp);
997 }
998
999 return 0;
1000}
1001
Stephen Hemminger6072b242008-08-14 16:52:26 +01001002static int
paul718e3742002-12-13 20:15:29 +00001003netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h)
1004{
Stephen Hemminger3d265b42008-08-16 17:30:39 +01001005 /* JF: Ignore messages that aren't from the kernel */
1006 if ( snl->nl_pid != 0 )
1007 {
1008 zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid );
1009 return 0;
1010 }
1011
paul718e3742002-12-13 20:15:29 +00001012 switch (h->nlmsg_type)
1013 {
1014 case RTM_NEWROUTE:
1015 return netlink_route_change (snl, h);
1016 break;
1017 case RTM_DELROUTE:
1018 return netlink_route_change (snl, h);
1019 break;
1020 case RTM_NEWLINK:
1021 return netlink_link_change (snl, h);
1022 break;
1023 case RTM_DELLINK:
1024 return netlink_link_change (snl, h);
1025 break;
1026 case RTM_NEWADDR:
1027 return netlink_interface_addr (snl, h);
1028 break;
1029 case RTM_DELADDR:
1030 return netlink_interface_addr (snl, h);
1031 break;
1032 default:
1033 zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type);
1034 break;
1035 }
1036 return 0;
1037}
1038
1039/* Interface lookup by netlink socket. */
1040int
paul6621ca82005-11-23 13:02:08 +00001041interface_lookup_netlink (void)
paul718e3742002-12-13 20:15:29 +00001042{
1043 int ret;
paul7021c422003-07-15 12:52:22 +00001044
paul718e3742002-12-13 20:15:29 +00001045 /* Get interface information. */
1046 ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd);
1047 if (ret < 0)
1048 return ret;
1049 ret = netlink_parse_info (netlink_interface, &netlink_cmd);
1050 if (ret < 0)
1051 return ret;
1052
1053 /* Get IPv4 address of the interfaces. */
1054 ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd);
1055 if (ret < 0)
1056 return ret;
1057 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1058 if (ret < 0)
1059 return ret;
1060
1061#ifdef HAVE_IPV6
1062 /* Get IPv6 address of the interfaces. */
1063 ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd);
1064 if (ret < 0)
1065 return ret;
1066 ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd);
1067 if (ret < 0)
1068 return ret;
1069#endif /* HAVE_IPV6 */
1070
1071 return 0;
1072}
1073
1074/* Routing table read function using netlink interface. Only called
1075 bootstrap time. */
1076int
paul6621ca82005-11-23 13:02:08 +00001077netlink_route_read (void)
paul718e3742002-12-13 20:15:29 +00001078{
1079 int ret;
paul7021c422003-07-15 12:52:22 +00001080
paul718e3742002-12-13 20:15:29 +00001081 /* Get IPv4 routing table. */
1082 ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd);
1083 if (ret < 0)
1084 return ret;
1085 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1086 if (ret < 0)
1087 return ret;
1088
1089#ifdef HAVE_IPV6
1090 /* Get IPv6 routing table. */
1091 ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd);
1092 if (ret < 0)
1093 return ret;
1094 ret = netlink_parse_info (netlink_routing_table, &netlink_cmd);
1095 if (ret < 0)
1096 return ret;
1097#endif /* HAVE_IPV6 */
1098
1099 return 0;
1100}
1101
1102/* Utility function comes from iproute2.
1103 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001104static int
paul718e3742002-12-13 20:15:29 +00001105addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen)
1106{
1107 int len;
1108 struct rtattr *rta;
1109
paul7021c422003-07-15 12:52:22 +00001110 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001111
paul7021c422003-07-15 12:52:22 +00001112 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001113 return -1;
1114
paul7021c422003-07-15 12:52:22 +00001115 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001116 rta->rta_type = type;
1117 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001118 memcpy (RTA_DATA (rta), data, alen);
paul718e3742002-12-13 20:15:29 +00001119 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1120
1121 return 0;
1122}
1123
Stephen Hemminger6072b242008-08-14 16:52:26 +01001124static int
paul718e3742002-12-13 20:15:29 +00001125rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen)
1126{
1127 int len;
1128 struct rtattr *subrta;
1129
paul7021c422003-07-15 12:52:22 +00001130 len = RTA_LENGTH (alen);
paul718e3742002-12-13 20:15:29 +00001131
paul7021c422003-07-15 12:52:22 +00001132 if (RTA_ALIGN (rta->rta_len) + len > maxlen)
paul718e3742002-12-13 20:15:29 +00001133 return -1;
1134
paul7021c422003-07-15 12:52:22 +00001135 subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
paul718e3742002-12-13 20:15:29 +00001136 subrta->rta_type = type;
1137 subrta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001138 memcpy (RTA_DATA (subrta), data, alen);
paul718e3742002-12-13 20:15:29 +00001139 rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
1140
1141 return 0;
1142}
1143
1144/* Utility function comes from iproute2.
1145 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001146static int
paul718e3742002-12-13 20:15:29 +00001147addattr32 (struct nlmsghdr *n, int maxlen, int type, int data)
1148{
1149 int len;
1150 struct rtattr *rta;
paul7021c422003-07-15 12:52:22 +00001151
1152 len = RTA_LENGTH (4);
1153
paul718e3742002-12-13 20:15:29 +00001154 if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1155 return -1;
1156
paul7021c422003-07-15 12:52:22 +00001157 rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
paul718e3742002-12-13 20:15:29 +00001158 rta->rta_type = type;
1159 rta->rta_len = len;
paul7021c422003-07-15 12:52:22 +00001160 memcpy (RTA_DATA (rta), &data, 4);
paul718e3742002-12-13 20:15:29 +00001161 n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1162
1163 return 0;
1164}
1165
1166static int
1167netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h)
1168{
hassob7ed1ec2005-03-31 20:13:49 +00001169 zlog_warn ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type);
paul718e3742002-12-13 20:15:29 +00001170 return 0;
1171}
1172
1173/* sendmsg() to netlink socket then recvmsg(). */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001174static int
paul718e3742002-12-13 20:15:29 +00001175netlink_talk (struct nlmsghdr *n, struct nlsock *nl)
1176{
1177 int status;
1178 struct sockaddr_nl snl;
paul7021c422003-07-15 12:52:22 +00001179 struct iovec iov = { (void *) n, n->nlmsg_len };
1180 struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 };
ajs4be019d2005-01-29 16:12:41 +00001181 int save_errno;
paul7021c422003-07-15 12:52:22 +00001182
paul718e3742002-12-13 20:15:29 +00001183 memset (&snl, 0, sizeof snl);
1184 snl.nl_family = AF_NETLINK;
paul7021c422003-07-15 12:52:22 +00001185
hassob7ed1ec2005-03-31 20:13:49 +00001186 n->nlmsg_seq = ++nl->seq;
paul718e3742002-12-13 20:15:29 +00001187
1188 /* Request an acknowledgement by setting NLM_F_ACK */
1189 n->nlmsg_flags |= NLM_F_ACK;
paul7021c422003-07-15 12:52:22 +00001190
1191 if (IS_ZEBRA_DEBUG_KERNEL)
hassob7ed1ec2005-03-31 20:13:49 +00001192 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name,
paul7021c422003-07-15 12:52:22 +00001193 lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
1194 n->nlmsg_seq);
paul718e3742002-12-13 20:15:29 +00001195
1196 /* Send message to netlink interface. */
paul7021c422003-07-15 12:52:22 +00001197 if (zserv_privs.change (ZPRIVS_RAISE))
1198 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +00001199 status = sendmsg (nl->sock, &msg, 0);
ajs4be019d2005-01-29 16:12:41 +00001200 save_errno = errno;
paul7021c422003-07-15 12:52:22 +00001201 if (zserv_privs.change (ZPRIVS_LOWER))
1202 zlog (NULL, LOG_ERR, "Can't lower privileges");
1203
paul718e3742002-12-13 20:15:29 +00001204 if (status < 0)
1205 {
1206 zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
ajs4be019d2005-01-29 16:12:41 +00001207 safe_strerror (save_errno));
paul718e3742002-12-13 20:15:29 +00001208 return -1;
1209 }
paul7021c422003-07-15 12:52:22 +00001210
paul718e3742002-12-13 20:15:29 +00001211
1212 /*
1213 * Get reply from netlink socket.
1214 * The reply should either be an acknowlegement or an error.
1215 */
Stephen Hemminger4cde9312008-08-16 17:51:27 +01001216 return netlink_parse_info (netlink_talk_filter, nl);
paul718e3742002-12-13 20:15:29 +00001217}
1218
1219/* Routing table change via netlink interface. */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001220static int
paul718e3742002-12-13 20:15:29 +00001221netlink_route (int cmd, int family, void *dest, int length, void *gate,
paul7021c422003-07-15 12:52:22 +00001222 int index, int zebra_flags, int table)
paul718e3742002-12-13 20:15:29 +00001223{
1224 int ret;
1225 int bytelen;
1226 struct sockaddr_nl snl;
1227 int discard;
1228
paul7021c422003-07-15 12:52:22 +00001229 struct
paul718e3742002-12-13 20:15:29 +00001230 {
1231 struct nlmsghdr n;
1232 struct rtmsg r;
1233 char buf[1024];
1234 } req;
1235
1236 memset (&req, 0, sizeof req);
1237
1238 bytelen = (family == AF_INET ? 4 : 16);
1239
1240 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1241 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1242 req.n.nlmsg_type = cmd;
1243 req.r.rtm_family = family;
1244 req.r.rtm_table = table;
1245 req.r.rtm_dst_len = length;
Timo Teräs40da2212008-08-13 17:37:14 +01001246 req.r.rtm_protocol = RTPROT_ZEBRA;
1247 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
paul718e3742002-12-13 20:15:29 +00001248
hasso81dfcaa2003-05-25 19:21:25 +00001249 if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1250 || (zebra_flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001251 discard = 1;
1252 else
1253 discard = 0;
1254
paul7021c422003-07-15 12:52:22 +00001255 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001256 {
paul7021c422003-07-15 12:52:22 +00001257 if (discard)
paul595db7f2003-05-25 21:35:06 +00001258 {
1259 if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
1260 req.r.rtm_type = RTN_BLACKHOLE;
1261 else if (zebra_flags & ZEBRA_FLAG_REJECT)
1262 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001263 else
1264 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1265 }
paul595db7f2003-05-25 21:35:06 +00001266 else
paul7021c422003-07-15 12:52:22 +00001267 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001268 }
1269
1270 if (dest)
1271 addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen);
1272
paul7021c422003-07-15 12:52:22 +00001273 if (!discard)
paul718e3742002-12-13 20:15:29 +00001274 {
1275 if (gate)
paul7021c422003-07-15 12:52:22 +00001276 addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen);
paul718e3742002-12-13 20:15:29 +00001277 if (index > 0)
paul7021c422003-07-15 12:52:22 +00001278 addattr32 (&req.n, sizeof req, RTA_OIF, index);
paul718e3742002-12-13 20:15:29 +00001279 }
1280
1281 /* Destination netlink address. */
1282 memset (&snl, 0, sizeof snl);
1283 snl.nl_family = AF_NETLINK;
1284
1285 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001286 ret = netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001287 if (ret < 0)
1288 return -1;
1289
1290 return 0;
1291}
1292
1293/* Routing table change via netlink interface. */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001294static int
paul718e3742002-12-13 20:15:29 +00001295netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib,
paul7021c422003-07-15 12:52:22 +00001296 int family)
paul718e3742002-12-13 20:15:29 +00001297{
1298 int bytelen;
1299 struct sockaddr_nl snl;
1300 struct nexthop *nexthop = NULL;
1301 int nexthop_num = 0;
paul718e3742002-12-13 20:15:29 +00001302 int discard;
1303
paul7021c422003-07-15 12:52:22 +00001304 struct
paul718e3742002-12-13 20:15:29 +00001305 {
1306 struct nlmsghdr n;
1307 struct rtmsg r;
1308 char buf[1024];
1309 } req;
1310
1311 memset (&req, 0, sizeof req);
1312
1313 bytelen = (family == AF_INET ? 4 : 16);
1314
1315 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1316 req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
1317 req.n.nlmsg_type = cmd;
1318 req.r.rtm_family = family;
1319 req.r.rtm_table = rib->table;
1320 req.r.rtm_dst_len = p->prefixlen;
Timo Teräs40da2212008-08-13 17:37:14 +01001321 req.r.rtm_protocol = RTPROT_ZEBRA;
1322 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
paul718e3742002-12-13 20:15:29 +00001323
paul7021c422003-07-15 12:52:22 +00001324 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
paul718e3742002-12-13 20:15:29 +00001325 discard = 1;
1326 else
1327 discard = 0;
1328
paul7021c422003-07-15 12:52:22 +00001329 if (cmd == RTM_NEWROUTE)
paul718e3742002-12-13 20:15:29 +00001330 {
paul7021c422003-07-15 12:52:22 +00001331 if (discard)
paul595db7f2003-05-25 21:35:06 +00001332 {
1333 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
1334 req.r.rtm_type = RTN_BLACKHOLE;
1335 else if (rib->flags & ZEBRA_FLAG_REJECT)
1336 req.r.rtm_type = RTN_UNREACHABLE;
paul7021c422003-07-15 12:52:22 +00001337 else
1338 assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */
1339 }
paul595db7f2003-05-25 21:35:06 +00001340 else
paul7021c422003-07-15 12:52:22 +00001341 req.r.rtm_type = RTN_UNICAST;
paul718e3742002-12-13 20:15:29 +00001342 }
1343
1344 addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1345
1346 /* Metric. */
1347 addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric);
1348
1349 if (discard)
1350 {
1351 if (cmd == RTM_NEWROUTE)
paul7021c422003-07-15 12:52:22 +00001352 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1353 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001354 goto skip;
1355 }
1356
1357 /* Multipath case. */
1358 if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
1359 {
1360 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
paul7021c422003-07-15 12:52:22 +00001361 {
paul5ec90d22003-06-19 01:41:37 +00001362
paul7021c422003-07-15 12:52:22 +00001363 if ((cmd == RTM_NEWROUTE
1364 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1365 || (cmd == RTM_DELROUTE
1366 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1367 {
paul5ec90d22003-06-19 01:41:37 +00001368
paul7021c422003-07-15 12:52:22 +00001369 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1370 {
1371 if (IS_ZEBRA_DEBUG_KERNEL)
1372 {
ajsb6178002004-12-07 21:12:56 +00001373 zlog_debug
paul7021c422003-07-15 12:52:22 +00001374 ("netlink_route_multipath() (recursive, 1 hop): "
hasso206d8052005-04-09 16:38:51 +00001375 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001376#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001377 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001378 inet6_ntoa (p->u.prefix6),
1379#else
1380 inet_ntoa (p->u.prefix4),
1381#endif /* HAVE_IPV6 */
1382
1383 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001384 }
paul5ec90d22003-06-19 01:41:37 +00001385
paul7021c422003-07-15 12:52:22 +00001386 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1387 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001388 {
1389 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1390 &nexthop->rgate.ipv4, bytelen);
Paul Jakma7514fb72007-05-02 16:05:35 +00001391 if (nexthop->src.ipv4.s_addr)
1392 addattr_l(&req.n, sizeof req, RTA_PREFSRC,
1393 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001394 if (IS_ZEBRA_DEBUG_KERNEL)
1395 zlog_debug("netlink_route_multipath() (recursive, "
1396 "1 hop): nexthop via %s if %u",
1397 inet_ntoa (nexthop->rgate.ipv4),
1398 nexthop->rifindex);
1399 }
paul718e3742002-12-13 20:15:29 +00001400#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001401 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1402 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1403 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001404 {
1405 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1406 &nexthop->rgate.ipv6, bytelen);
1407
1408 if (IS_ZEBRA_DEBUG_KERNEL)
1409 zlog_debug("netlink_route_multipath() (recursive, "
1410 "1 hop): nexthop via %s if %u",
1411 inet6_ntoa (nexthop->rgate.ipv6),
1412 nexthop->rifindex);
1413 }
paul718e3742002-12-13 20:15:29 +00001414#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001415 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1416 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
1417 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1418 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
1419 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001420 {
1421 addattr32 (&req.n, sizeof req, RTA_OIF,
1422 nexthop->rifindex);
Paul Jakma7514fb72007-05-02 16:05:35 +00001423 if ((nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1424 || nexthop->rtype == NEXTHOP_TYPE_IFINDEX)
1425 && nexthop->src.ipv4.s_addr)
1426 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1427 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001428
1429 if (IS_ZEBRA_DEBUG_KERNEL)
1430 zlog_debug("netlink_route_multipath() (recursive, "
1431 "1 hop): nexthop via if %u",
1432 nexthop->rifindex);
1433 }
paul7021c422003-07-15 12:52:22 +00001434 }
1435 else
1436 {
1437 if (IS_ZEBRA_DEBUG_KERNEL)
1438 {
ajsb6178002004-12-07 21:12:56 +00001439 zlog_debug
hasso206d8052005-04-09 16:38:51 +00001440 ("netlink_route_multipath() (single hop): "
1441 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001442#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001443 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001444 inet6_ntoa (p->u.prefix6),
1445#else
1446 inet_ntoa (p->u.prefix4),
1447#endif /* HAVE_IPV6 */
1448 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001449 }
paul5ec90d22003-06-19 01:41:37 +00001450
paul7021c422003-07-15 12:52:22 +00001451 if (nexthop->type == NEXTHOP_TYPE_IPV4
1452 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001453 {
1454 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1455 &nexthop->gate.ipv4, bytelen);
Paul Jakma7514fb72007-05-02 16:05:35 +00001456 if (nexthop->src.ipv4.s_addr)
1457 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1458 &nexthop->src.ipv4, bytelen);
hasso206d8052005-04-09 16:38:51 +00001459
1460 if (IS_ZEBRA_DEBUG_KERNEL)
1461 zlog_debug("netlink_route_multipath() (single hop): "
1462 "nexthop via %s if %u",
1463 inet_ntoa (nexthop->gate.ipv4),
1464 nexthop->ifindex);
1465 }
paul718e3742002-12-13 20:15:29 +00001466#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001467 if (nexthop->type == NEXTHOP_TYPE_IPV6
1468 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1469 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001470 {
1471 addattr_l (&req.n, sizeof req, RTA_GATEWAY,
1472 &nexthop->gate.ipv6, bytelen);
1473
1474 if (IS_ZEBRA_DEBUG_KERNEL)
1475 zlog_debug("netlink_route_multipath() (single hop): "
1476 "nexthop via %s if %u",
1477 inet6_ntoa (nexthop->gate.ipv6),
1478 nexthop->ifindex);
1479 }
paul718e3742002-12-13 20:15:29 +00001480#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001481 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1482 || nexthop->type == NEXTHOP_TYPE_IFNAME
Paul Jakma7514fb72007-05-02 16:05:35 +00001483 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1484 {
1485 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1486
1487 if (nexthop->src.ipv4.s_addr)
1488 addattr_l (&req.n, sizeof req, RTA_PREFSRC,
1489 &nexthop->src.ipv4, bytelen);
1490
1491 if (IS_ZEBRA_DEBUG_KERNEL)
1492 zlog_debug("netlink_route_multipath() (single hop): "
1493 "nexthop via if %u", nexthop->ifindex);
1494 }
1495 else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
paul7021c422003-07-15 12:52:22 +00001496 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001497 {
1498 addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex);
1499
1500 if (IS_ZEBRA_DEBUG_KERNEL)
1501 zlog_debug("netlink_route_multipath() (single hop): "
1502 "nexthop via if %u", nexthop->ifindex);
1503 }
paul7021c422003-07-15 12:52:22 +00001504 }
paul718e3742002-12-13 20:15:29 +00001505
paul7021c422003-07-15 12:52:22 +00001506 if (cmd == RTM_NEWROUTE)
1507 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
paul718e3742002-12-13 20:15:29 +00001508
paul7021c422003-07-15 12:52:22 +00001509 nexthop_num++;
1510 break;
1511 }
1512 }
paul718e3742002-12-13 20:15:29 +00001513 }
1514 else
1515 {
1516 char buf[1024];
1517 struct rtattr *rta = (void *) buf;
1518 struct rtnexthop *rtnh;
Paul Jakma7514fb72007-05-02 16:05:35 +00001519 union g_addr *src = NULL;
paul718e3742002-12-13 20:15:29 +00001520
1521 rta->rta_type = RTA_MULTIPATH;
paul7021c422003-07-15 12:52:22 +00001522 rta->rta_len = RTA_LENGTH (0);
1523 rtnh = RTA_DATA (rta);
paul718e3742002-12-13 20:15:29 +00001524
1525 nexthop_num = 0;
1526 for (nexthop = rib->nexthop;
paul7021c422003-07-15 12:52:22 +00001527 nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM);
1528 nexthop = nexthop->next)
1529 {
1530 if ((cmd == RTM_NEWROUTE
1531 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1532 || (cmd == RTM_DELROUTE
1533 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1534 {
1535 nexthop_num++;
paul718e3742002-12-13 20:15:29 +00001536
paul7021c422003-07-15 12:52:22 +00001537 rtnh->rtnh_len = sizeof (*rtnh);
1538 rtnh->rtnh_flags = 0;
1539 rtnh->rtnh_hops = 0;
1540 rta->rta_len += rtnh->rtnh_len;
paul718e3742002-12-13 20:15:29 +00001541
paul7021c422003-07-15 12:52:22 +00001542 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1543 {
1544 if (IS_ZEBRA_DEBUG_KERNEL)
1545 {
ajsb6178002004-12-07 21:12:56 +00001546 zlog_debug ("netlink_route_multipath() "
hasso206d8052005-04-09 16:38:51 +00001547 "(recursive, multihop): %s %s/%d type %s",
hasso1ada8192005-06-12 11:28:18 +00001548 lookup (nlmsg_str, cmd),
1549#ifdef HAVE_IPV6
1550 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
1551 inet6_ntoa (p->u.prefix6),
1552#else
1553 inet_ntoa (p->u.prefix4),
1554#endif /* HAVE_IPV6 */
hasso206d8052005-04-09 16:38:51 +00001555 p->prefixlen, nexthop_types_desc[nexthop->rtype]);
paul7021c422003-07-15 12:52:22 +00001556 }
1557 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
1558 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
1559 {
1560 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1561 &nexthop->rgate.ipv4, bytelen);
1562 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
hasso206d8052005-04-09 16:38:51 +00001563
Paul Jakma7514fb72007-05-02 16:05:35 +00001564 if (nexthop->src.ipv4.s_addr)
1565 src = &nexthop->src;
1566
hasso206d8052005-04-09 16:38:51 +00001567 if (IS_ZEBRA_DEBUG_KERNEL)
1568 zlog_debug("netlink_route_multipath() (recursive, "
1569 "multihop): nexthop via %s if %u",
1570 inet_ntoa (nexthop->rgate.ipv4),
1571 nexthop->rifindex);
paul7021c422003-07-15 12:52:22 +00001572 }
paul718e3742002-12-13 20:15:29 +00001573#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001574 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
1575 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME
1576 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001577 {
1578 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1579 &nexthop->rgate.ipv6, bytelen);
1580
1581 if (IS_ZEBRA_DEBUG_KERNEL)
1582 zlog_debug("netlink_route_multipath() (recursive, "
1583 "multihop): nexthop via %s if %u",
1584 inet6_ntoa (nexthop->rgate.ipv6),
1585 nexthop->rifindex);
1586 }
paul718e3742002-12-13 20:15:29 +00001587#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001588 /* ifindex */
Paul Jakma7514fb72007-05-02 16:05:35 +00001589 if (nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
1590 || nexthop->rtype == NEXTHOP_TYPE_IFINDEX
1591 || nexthop->rtype == NEXTHOP_TYPE_IFNAME)
1592 {
1593 rtnh->rtnh_ifindex = nexthop->rifindex;
1594 if (nexthop->src.ipv4.s_addr)
1595 src = &nexthop->src;
1596
1597 if (IS_ZEBRA_DEBUG_KERNEL)
1598 zlog_debug("netlink_route_multipath() (recursive, "
1599 "multihop): nexthop via if %u",
1600 nexthop->rifindex);
1601 }
1602 else if (nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
paul7021c422003-07-15 12:52:22 +00001603 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
hasso206d8052005-04-09 16:38:51 +00001604 {
1605 rtnh->rtnh_ifindex = nexthop->rifindex;
1606
1607 if (IS_ZEBRA_DEBUG_KERNEL)
1608 zlog_debug("netlink_route_multipath() (recursive, "
1609 "multihop): nexthop via if %u",
1610 nexthop->rifindex);
1611 }
paul7021c422003-07-15 12:52:22 +00001612 else
hasso206d8052005-04-09 16:38:51 +00001613 {
1614 rtnh->rtnh_ifindex = 0;
1615 }
paul7021c422003-07-15 12:52:22 +00001616 }
1617 else
1618 {
1619 if (IS_ZEBRA_DEBUG_KERNEL)
1620 {
hasso206d8052005-04-09 16:38:51 +00001621 zlog_debug ("netlink_route_multipath() (multihop): "
1622 "%s %s/%d, type %s", lookup (nlmsg_str, cmd),
hasso1ada8192005-06-12 11:28:18 +00001623#ifdef HAVE_IPV6
hasso206d8052005-04-09 16:38:51 +00001624 (family == AF_INET) ? inet_ntoa (p->u.prefix4) :
hasso1ada8192005-06-12 11:28:18 +00001625 inet6_ntoa (p->u.prefix6),
1626#else
1627 inet_ntoa (p->u.prefix4),
1628#endif /* HAVE_IPV6 */
1629 p->prefixlen, nexthop_types_desc[nexthop->type]);
paul7021c422003-07-15 12:52:22 +00001630 }
1631 if (nexthop->type == NEXTHOP_TYPE_IPV4
1632 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1633 {
hasso206d8052005-04-09 16:38:51 +00001634 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1635 &nexthop->gate.ipv4, bytelen);
1636 rtnh->rtnh_len += sizeof (struct rtattr) + 4;
1637
Paul Jakma7514fb72007-05-02 16:05:35 +00001638 if (nexthop->src.ipv4.s_addr)
1639 src = &nexthop->src;
1640
hasso206d8052005-04-09 16:38:51 +00001641 if (IS_ZEBRA_DEBUG_KERNEL)
1642 zlog_debug("netlink_route_multipath() (multihop): "
1643 "nexthop via %s if %u",
1644 inet_ntoa (nexthop->gate.ipv4),
1645 nexthop->ifindex);
paul7021c422003-07-15 12:52:22 +00001646 }
paul718e3742002-12-13 20:15:29 +00001647#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001648 if (nexthop->type == NEXTHOP_TYPE_IPV6
1649 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1650 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001651 {
1652 rta_addattr_l (rta, 4096, RTA_GATEWAY,
1653 &nexthop->gate.ipv6, bytelen);
1654
1655 if (IS_ZEBRA_DEBUG_KERNEL)
1656 zlog_debug("netlink_route_multipath() (multihop): "
1657 "nexthop via %s if %u",
1658 inet6_ntoa (nexthop->gate.ipv6),
1659 nexthop->ifindex);
1660 }
paul718e3742002-12-13 20:15:29 +00001661#endif /* HAVE_IPV6 */
paul7021c422003-07-15 12:52:22 +00001662 /* ifindex */
Paul Jakma7514fb72007-05-02 16:05:35 +00001663 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1664 || nexthop->type == NEXTHOP_TYPE_IFINDEX
1665 || nexthop->type == NEXTHOP_TYPE_IFNAME)
1666 {
1667 rtnh->rtnh_ifindex = nexthop->ifindex;
1668 if (nexthop->src.ipv4.s_addr)
1669 src = &nexthop->src;
1670 if (IS_ZEBRA_DEBUG_KERNEL)
1671 zlog_debug("netlink_route_multipath() (multihop): "
1672 "nexthop via if %u", nexthop->ifindex);
1673 }
1674 else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
paul7021c422003-07-15 12:52:22 +00001675 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
hasso206d8052005-04-09 16:38:51 +00001676 {
1677 rtnh->rtnh_ifindex = nexthop->ifindex;
1678
1679 if (IS_ZEBRA_DEBUG_KERNEL)
1680 zlog_debug("netlink_route_multipath() (multihop): "
1681 "nexthop via if %u", nexthop->ifindex);
1682 }
paul7021c422003-07-15 12:52:22 +00001683 else
hasso206d8052005-04-09 16:38:51 +00001684 {
1685 rtnh->rtnh_ifindex = 0;
1686 }
paul7021c422003-07-15 12:52:22 +00001687 }
1688 rtnh = RTNH_NEXT (rtnh);
paul718e3742002-12-13 20:15:29 +00001689
paul7021c422003-07-15 12:52:22 +00001690 if (cmd == RTM_NEWROUTE)
1691 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1692 }
1693 }
Paul Jakma7514fb72007-05-02 16:05:35 +00001694 if (src)
1695 addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen);
paul718e3742002-12-13 20:15:29 +00001696
1697 if (rta->rta_len > RTA_LENGTH (0))
paul7021c422003-07-15 12:52:22 +00001698 addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta),
1699 RTA_PAYLOAD (rta));
paul718e3742002-12-13 20:15:29 +00001700 }
1701
1702 /* If there is no useful nexthop then return. */
1703 if (nexthop_num == 0)
1704 {
1705 if (IS_ZEBRA_DEBUG_KERNEL)
ajsb6178002004-12-07 21:12:56 +00001706 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
paul718e3742002-12-13 20:15:29 +00001707 return 0;
1708 }
1709
paul7021c422003-07-15 12:52:22 +00001710skip:
paul718e3742002-12-13 20:15:29 +00001711
1712 /* Destination netlink address. */
1713 memset (&snl, 0, sizeof snl);
1714 snl.nl_family = AF_NETLINK;
1715
paul718e3742002-12-13 20:15:29 +00001716 /* Talk to netlink socket. */
hassob7ed1ec2005-03-31 20:13:49 +00001717 return netlink_talk (&req.n, &netlink_cmd);
paul718e3742002-12-13 20:15:29 +00001718}
1719
1720int
1721kernel_add_ipv4 (struct prefix *p, struct rib *rib)
1722{
1723 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET);
1724}
1725
1726int
1727kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
1728{
1729 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET);
1730}
1731
1732#ifdef HAVE_IPV6
1733int
1734kernel_add_ipv6 (struct prefix *p, struct rib *rib)
1735{
1736 return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6);
1737}
1738
1739int
1740kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
1741{
1742 return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6);
1743}
1744
1745/* Delete IPv6 route from the kernel. */
1746int
1747kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate,
paul6621ca82005-11-23 13:02:08 +00001748 unsigned int index, int flags, int table)
paul718e3742002-12-13 20:15:29 +00001749{
paul7021c422003-07-15 12:52:22 +00001750 return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix,
1751 dest->prefixlen, gate, index, flags, table);
paul718e3742002-12-13 20:15:29 +00001752}
1753#endif /* HAVE_IPV6 */
1754
1755/* Interface address modification. */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001756static int
paul718e3742002-12-13 20:15:29 +00001757netlink_address (int cmd, int family, struct interface *ifp,
paul7021c422003-07-15 12:52:22 +00001758 struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001759{
1760 int bytelen;
1761 struct prefix *p;
1762
paul7021c422003-07-15 12:52:22 +00001763 struct
paul718e3742002-12-13 20:15:29 +00001764 {
1765 struct nlmsghdr n;
1766 struct ifaddrmsg ifa;
1767 char buf[1024];
1768 } req;
1769
1770 p = ifc->address;
1771 memset (&req, 0, sizeof req);
1772
1773 bytelen = (family == AF_INET ? 4 : 16);
1774
paul7021c422003-07-15 12:52:22 +00001775 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
paul718e3742002-12-13 20:15:29 +00001776 req.n.nlmsg_flags = NLM_F_REQUEST;
1777 req.n.nlmsg_type = cmd;
1778 req.ifa.ifa_family = family;
1779
1780 req.ifa.ifa_index = ifp->ifindex;
1781 req.ifa.ifa_prefixlen = p->prefixlen;
1782
1783 addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1784
1785 if (family == AF_INET && cmd == RTM_NEWADDR)
1786 {
Andrew J. Schorre4529632006-12-12 19:18:21 +00001787 if (!CONNECTED_PEER(ifc) && ifc->destination)
paul7021c422003-07-15 12:52:22 +00001788 {
1789 p = ifc->destination;
1790 addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
1791 bytelen);
1792 }
paul718e3742002-12-13 20:15:29 +00001793 }
1794
1795 if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
1796 SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
paul7021c422003-07-15 12:52:22 +00001797
paul718e3742002-12-13 20:15:29 +00001798 if (ifc->label)
1799 addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
paul7021c422003-07-15 12:52:22 +00001800 strlen (ifc->label) + 1);
paul718e3742002-12-13 20:15:29 +00001801
1802 return netlink_talk (&req.n, &netlink_cmd);
1803}
1804
1805int
1806kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
1807{
1808 return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
1809}
1810
1811int
1812kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
1813{
1814 return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
1815}
1816
paul718e3742002-12-13 20:15:29 +00001817
1818extern struct thread_master *master;
1819
1820/* Kernel route reflection. */
Stephen Hemminger6072b242008-08-14 16:52:26 +01001821static int
paul718e3742002-12-13 20:15:29 +00001822kernel_read (struct thread *thread)
1823{
1824 int ret;
1825 int sock;
1826
1827 sock = THREAD_FD (thread);
1828 ret = netlink_parse_info (netlink_information_fetch, &netlink);
paulb21b19c2003-06-15 01:28:29 +00001829 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
paul718e3742002-12-13 20:15:29 +00001830
1831 return 0;
1832}
1833
Stephen Hemminger3d265b42008-08-16 17:30:39 +01001834/* Filter out messages from self that occur on listener socket,
1835 caused by our actions on the command socket
1836 */
1837static void netlink_install_filter (int sock, __u32 pid)
Paul Jakma768a27e2008-05-29 18:23:08 +00001838{
Paul Jakma768a27e2008-05-29 18:23:08 +00001839 struct sock_filter filter[] = {
Stephen Hemminger3d265b42008-08-16 17:30:39 +01001840 /* 0: ldh [4] */
1841 BPF_STMT(BPF_LD|BPF_ABS|BPF_H, offsetof(struct nlmsghdr, nlmsg_type)),
1842 /* 1: jeq 0x18 jt 3 jf 6 */
1843 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_NEWROUTE), 1, 0),
1844 /* 2: jeq 0x19 jt 3 jf 6 */
1845 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_DELROUTE), 0, 3),
1846 /* 3: ldw [12] */
1847 BPF_STMT(BPF_LD|BPF_ABS|BPF_W, offsetof(struct nlmsghdr, nlmsg_pid)),
1848 /* 4: jeq XX jt 5 jf 6 */
1849 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htonl(pid), 0, 1),
1850 /* 5: ret 0 (skip) */
1851 BPF_STMT(BPF_RET|BPF_K, 0),
1852 /* 6: ret 0xffff (keep) */
1853 BPF_STMT(BPF_RET|BPF_K, 0xffff),
Paul Jakma768a27e2008-05-29 18:23:08 +00001854 };
1855
1856 struct sock_fprog prog = {
1857 .len = sizeof(filter) / sizeof(filter[0]),
1858 .filter = filter,
1859 };
1860
1861 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0)
1862 zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno));
1863}
1864
paul718e3742002-12-13 20:15:29 +00001865/* Exported interface function. This function simply calls
1866 netlink_socket (). */
1867void
paul6621ca82005-11-23 13:02:08 +00001868kernel_init (void)
paul718e3742002-12-13 20:15:29 +00001869{
1870 unsigned long groups;
1871
paul7021c422003-07-15 12:52:22 +00001872 groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
paul718e3742002-12-13 20:15:29 +00001873#ifdef HAVE_IPV6
paul7021c422003-07-15 12:52:22 +00001874 groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
paul718e3742002-12-13 20:15:29 +00001875#endif /* HAVE_IPV6 */
1876 netlink_socket (&netlink, groups);
1877 netlink_socket (&netlink_cmd, 0);
1878
1879 /* Register kernel socket. */
1880 if (netlink.sock > 0)
Paul Jakma768a27e2008-05-29 18:23:08 +00001881 {
Stephen Hemminger4cde9312008-08-16 17:51:27 +01001882 /* Only want non-blocking on the netlink event socket */
1883 if (fcntl (netlink.sock, F_SETFL, O_NONBLOCK) < 0)
1884 zlog (NULL, LOG_ERR, "Can't set %s socket flags: %s", netlink.name,
1885 safe_strerror (errno));
1886
Stephen Hemminger30afea32008-08-16 18:25:47 +01001887 /* Set receive buffer size if it's set from command line */
1888 if (nl_rcvbufsize)
1889 netlink_recvbuf (&netlink, nl_rcvbufsize);
1890
Stephen Hemminger3d265b42008-08-16 17:30:39 +01001891 netlink_install_filter (netlink.sock, netlink_cmd.snl.nl_pid);
Paul Jakma768a27e2008-05-29 18:23:08 +00001892 thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock);
1893 }
paul718e3742002-12-13 20:15:29 +00001894}