Import Upstream version 1.2.2
[quagga-debian.git] / zebra / rt_netlink.c
1 /* 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 #include <net/if_arp.h>
24
25 /* Hack for GNU libc version 2. */
26 #ifndef MSG_TRUNC
27 #define MSG_TRUNC      0x20
28 #endif /* MSG_TRUNC */
29
30 #include "linklist.h"
31 #include "if.h"
32 #include "log.h"
33 #include "prefix.h"
34 #include "connected.h"
35 #include "table.h"
36 #include "memory.h"
37 #include "rib.h"
38 #include "thread.h"
39 #include "privs.h"
40 #include "vrf.h"
41 #include "nexthop.h"
42
43 #include "zebra/zserv.h"
44 #include "zebra/rt.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/interface.h"
47 #include "zebra/debug.h"
48
49 #include "rt_netlink.h"
50
51 static const struct message nlmsg_str[] = {
52   {RTM_NEWROUTE, "RTM_NEWROUTE"},
53   {RTM_DELROUTE, "RTM_DELROUTE"},
54   {RTM_GETROUTE, "RTM_GETROUTE"},
55   {RTM_NEWLINK,  "RTM_NEWLINK"},
56   {RTM_DELLINK,  "RTM_DELLINK"},
57   {RTM_GETLINK,  "RTM_GETLINK"},
58   {RTM_NEWADDR,  "RTM_NEWADDR"},
59   {RTM_DELADDR,  "RTM_DELADDR"},
60   {RTM_GETADDR,  "RTM_GETADDR"},
61   {0, NULL}
62 };
63
64 extern struct zebra_t zebrad;
65
66 extern struct zebra_privs_t zserv_privs;
67
68 extern u_int32_t nl_rcvbufsize;
69
70 static struct {
71   char *p;
72   size_t size;
73 } nl_rcvbuf;
74
75 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
76    names and ifindex values. */
77 static void
78 set_ifindex(struct interface *ifp, ifindex_t ifi_index)
79 {
80   struct interface *oifp;
81
82   if (((oifp = if_lookup_by_index(ifi_index)) != NULL) && (oifp != ifp))
83     {
84       if (ifi_index == IFINDEX_INTERNAL)
85         zlog_err("Netlink is setting interface %s ifindex to reserved "
86                  "internal value %u", ifp->name, ifi_index);
87       else
88         {
89           if (IS_ZEBRA_DEBUG_KERNEL)
90             zlog_debug("interface index %d was renamed from %s to %s",
91                        ifi_index, oifp->name, ifp->name);
92           if (if_is_up(oifp))
93             zlog_err("interface rename detected on up interface: index %d "
94                      "was renamed from %s to %s, results are uncertain!", 
95                      ifi_index, oifp->name, ifp->name);
96           if_delete_update(oifp);
97         }
98     }
99   ifp->ifindex = ifi_index;
100 }
101
102 #ifndef SO_RCVBUFFORCE
103 #define SO_RCVBUFFORCE  (33)
104 #endif
105
106 static int
107 netlink_recvbuf (struct nlsock *nl, uint32_t newsize)
108 {
109   u_int32_t oldsize;
110   socklen_t newlen = sizeof(newsize);
111   socklen_t oldlen = sizeof(oldsize);
112   int ret;
113
114   ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);
115   if (ret < 0)
116     {
117       zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
118             safe_strerror (errno));
119       return -1;
120     }
121
122   /* Try force option (linux >= 2.6.14) and fall back to normal set */
123   if ( zserv_privs.change (ZPRIVS_RAISE) )
124     zlog_err ("routing_socket: Can't raise privileges");
125   ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize,
126                    sizeof(nl_rcvbufsize));
127   if ( zserv_privs.change (ZPRIVS_LOWER) )
128     zlog_err ("routing_socket: Can't lower privileges");
129   if (ret < 0)
130      ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize,
131                       sizeof(nl_rcvbufsize));
132   if (ret < 0)
133     {
134       zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name,
135             safe_strerror (errno));
136       return -1;
137     }
138
139   ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);
140   if (ret < 0)
141     {
142       zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name,
143             safe_strerror (errno));
144       return -1;
145     }
146
147   zlog (NULL, LOG_INFO,
148         "Setting netlink socket receive buffer size: %u -> %u",
149         oldsize, newsize);
150   return 0;
151 }
152
153 /* Make socket for Linux netlink interface. */
154 static int
155 netlink_socket (struct nlsock *nl, unsigned long groups, vrf_id_t vrf_id)
156 {
157   int ret;
158   struct sockaddr_nl snl;
159   int sock;
160   int namelen;
161   int save_errno;
162
163   if (zserv_privs.change (ZPRIVS_RAISE))
164     {
165       zlog (NULL, LOG_ERR, "Can't raise privileges");
166       return -1;
167     }
168
169   sock = vrf_socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, vrf_id);
170   if (sock < 0)
171     {
172       zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name,
173             safe_strerror (errno));
174       return -1;
175     }
176
177   memset (&snl, 0, sizeof snl);
178   snl.nl_family = AF_NETLINK;
179   snl.nl_groups = groups;
180
181   /* Bind the socket to the netlink structure for anything. */
182   ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);
183   save_errno = errno;
184   if (zserv_privs.change (ZPRIVS_LOWER))
185     zlog (NULL, LOG_ERR, "Can't lower privileges");
186
187   if (ret < 0)
188     {
189       zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s",
190             nl->name, snl.nl_groups, safe_strerror (save_errno));
191       close (sock);
192       return -1;
193     }
194
195   /* multiple netlink sockets will have different nl_pid */
196   namelen = sizeof snl;
197   ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);
198   if (ret < 0 || namelen != sizeof snl)
199     {
200       zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name,
201             safe_strerror (errno));
202       close (sock);
203       return -1;
204     }
205
206   nl->snl = snl;
207   nl->sock = sock;
208   return ret;
209 }
210
211 /* Get type specified information from netlink. */
212 static int
213 netlink_request (int family, int type, struct nlsock *nl)
214 {
215   int ret;
216   struct sockaddr_nl snl;
217   int save_errno;
218
219   struct
220   {
221     struct nlmsghdr nlh;
222     struct rtgenmsg g;
223   } req;
224
225
226   /* Check netlink socket. */
227   if (nl->sock < 0)
228     {
229       zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name);
230       return -1;
231     }
232
233   memset (&snl, 0, sizeof snl);
234   snl.nl_family = AF_NETLINK;
235
236   memset (&req, 0, sizeof req);
237   req.nlh.nlmsg_len = sizeof req;
238   req.nlh.nlmsg_type = type;
239   req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
240   req.nlh.nlmsg_pid = nl->snl.nl_pid;
241   req.nlh.nlmsg_seq = ++nl->seq;
242   req.g.rtgen_family = family;
243
244   /* linux appears to check capabilities on every message 
245    * have to raise caps for every message sent
246    */
247   if (zserv_privs.change (ZPRIVS_RAISE))
248     {
249       zlog (NULL, LOG_ERR, "Can't raise privileges");
250       return -1;
251     }
252
253   ret = sendto (nl->sock, (void *) &req, sizeof req, 0,
254                 (struct sockaddr *) &snl, sizeof snl);
255   save_errno = errno;
256
257   if (zserv_privs.change (ZPRIVS_LOWER))
258     zlog (NULL, LOG_ERR, "Can't lower privileges");
259
260   if (ret < 0)
261     {
262       zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name,
263             safe_strerror (save_errno));
264       return -1;
265     }
266
267   return 0;
268 }
269
270 /* Receive message from netlink interface and pass those information
271    to the given function. */
272 static int
273 netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,
274                                    vrf_id_t),
275                     struct nlsock *nl, struct zebra_vrf *zvrf)
276 {
277   int status;
278   int ret = 0;
279   int error;
280
281   while (1)
282     {
283       struct iovec iov = {
284         .iov_base = nl_rcvbuf.p,
285         .iov_len = nl_rcvbuf.size,
286       };
287       struct sockaddr_nl snl;
288       struct msghdr msg = {
289         .msg_name = (void *) &snl,
290         .msg_namelen = sizeof snl,
291         .msg_iov = &iov,
292         .msg_iovlen = 1
293       };
294       struct nlmsghdr *h;
295
296       status = recvmsg (nl->sock, &msg, 0);
297       if (status < 0)
298         {
299           if (errno == EINTR)
300             continue;
301           if (errno == EWOULDBLOCK || errno == EAGAIN)
302             break;
303           zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s",
304                 nl->name, safe_strerror(errno));
305           continue;
306         }
307
308       if (status == 0)
309         {
310           zlog (NULL, LOG_ERR, "%s EOF", nl->name);
311           return -1;
312         }
313
314       if (msg.msg_namelen != sizeof snl)
315         {
316           zlog (NULL, LOG_ERR, "%s sender address length error: length %d",
317                 nl->name, msg.msg_namelen);
318           return -1;
319         }
320       
321       for (h = (struct nlmsghdr *) nl_rcvbuf.p; 
322            NLMSG_OK (h, (unsigned int) status);
323            h = NLMSG_NEXT (h, status))
324         {
325           /* Finish of reading. */
326           if (h->nlmsg_type == NLMSG_DONE)
327             return ret;
328
329           /* Error handling. */
330           if (h->nlmsg_type == NLMSG_ERROR)
331             {
332               struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h);
333               int errnum = err->error;
334               int msg_type = err->msg.nlmsg_type;
335
336               /* If the error field is zero, then this is an ACK */
337               if (err->error == 0)
338                 {
339                   if (IS_ZEBRA_DEBUG_KERNEL)
340                     {
341                       zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
342                                  __FUNCTION__, nl->name,
343                                  lookup (nlmsg_str, err->msg.nlmsg_type),
344                                  err->msg.nlmsg_type, err->msg.nlmsg_seq,
345                                  err->msg.nlmsg_pid);
346                     }
347
348                   /* return if not a multipart message, otherwise continue */
349                   if (!(h->nlmsg_flags & NLM_F_MULTI))
350                     {
351                       return 0;
352                     }
353                   continue;
354                 }
355
356               if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))
357                 {
358                   zlog (NULL, LOG_ERR, "%s error: message truncated",
359                         nl->name);
360                   return -1;
361                 }
362
363               /* Deal with errors that occur because of races in link handling */
364               if (nl == &zvrf->netlink_cmd
365                   && ((msg_type == RTM_DELROUTE &&
366                        (-errnum == ENODEV || -errnum == ESRCH))
367                       || (msg_type == RTM_NEWROUTE && -errnum == EEXIST)))
368                 {
369                   if (IS_ZEBRA_DEBUG_KERNEL)
370                     zlog_debug ("%s: error: %s type=%s(%u), seq=%u, pid=%u",
371                                 nl->name, safe_strerror (-errnum),
372                                 lookup (nlmsg_str, msg_type),
373                                 msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
374                   return 0;
375                 }
376
377               zlog_err ("%s error: %s, type=%s(%u), seq=%u, pid=%u",
378                         nl->name, safe_strerror (-errnum),
379                         lookup (nlmsg_str, msg_type),
380                         msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid);
381               return -1;
382             }
383
384           /* OK we got netlink message. */
385           if (IS_ZEBRA_DEBUG_KERNEL)
386             zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u",
387                        nl->name,
388                        lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type,
389                        h->nlmsg_seq, h->nlmsg_pid);
390
391           /* skip unsolicited messages originating from command socket
392            * linux sets the originators port-id for {NEW|DEL}ADDR messages,
393            * so this has to be checked here. */
394           if (nl != &zvrf->netlink_cmd
395               && h->nlmsg_pid == zvrf->netlink_cmd.snl.nl_pid
396               && (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR))
397             {
398               if (IS_ZEBRA_DEBUG_KERNEL)
399                 zlog_debug ("netlink_parse_info: %s packet comes from %s",
400                             zvrf->netlink_cmd.name, nl->name);
401               continue;
402             }
403
404           error = (*filter) (&snl, h, zvrf->vrf_id);
405           if (error < 0)
406             {
407               zlog (NULL, LOG_ERR, "%s filter function error", nl->name);
408               ret = error;
409             }
410         }
411
412       /* After error care. */
413       if (msg.msg_flags & MSG_TRUNC)
414         {
415           zlog (NULL, LOG_ERR, "%s error: message truncated!", nl->name);
416           zlog (NULL, LOG_ERR, 
417                 "Must restart with larger --nl-bufsize value!");
418           continue;
419         }
420       if (status)
421         {
422           zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name,
423                 status);
424           return -1;
425         }
426     }
427   return ret;
428 }
429
430 /* Utility function for parse rtattr. */
431 static void
432 netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta,
433                       int len)
434 {
435   while (RTA_OK (rta, len))
436     {
437       if (rta->rta_type <= max)
438         tb[rta->rta_type] = rta;
439       rta = RTA_NEXT (rta, len);
440     }
441 }
442
443 /* Utility function to parse hardware link-layer address and update ifp */
444 static void
445 netlink_interface_update_hw_addr (struct rtattr **tb, struct interface *ifp)
446 {
447   int i;
448
449   if (tb[IFLA_ADDRESS])
450     {
451       int hw_addr_len;
452
453       hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
454
455       if (hw_addr_len > INTERFACE_HWADDR_MAX)
456         zlog_warn ("Hardware address is too large: %d", hw_addr_len);
457       else
458         {
459           ifp->hw_addr_len = hw_addr_len;
460           memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
461
462           for (i = 0; i < hw_addr_len; i++)
463             if (ifp->hw_addr[i] != 0)
464               break;
465
466           if (i == hw_addr_len)
467             ifp->hw_addr_len = 0;
468           else
469             ifp->hw_addr_len = hw_addr_len;
470         }
471     }
472 }
473
474 static enum zebra_link_type
475 netlink_to_zebra_link_type (unsigned int hwt)
476 {
477   switch (hwt)
478   {
479     case ARPHRD_ETHER: return ZEBRA_LLT_ETHER;
480     case ARPHRD_EETHER: return ZEBRA_LLT_EETHER;
481     case ARPHRD_AX25: return ZEBRA_LLT_AX25;
482     case ARPHRD_PRONET: return ZEBRA_LLT_PRONET;
483     case ARPHRD_IEEE802: return ZEBRA_LLT_IEEE802;
484     case ARPHRD_ARCNET: return ZEBRA_LLT_ARCNET;
485     case ARPHRD_APPLETLK: return ZEBRA_LLT_APPLETLK;
486     case ARPHRD_DLCI: return ZEBRA_LLT_DLCI;
487     case ARPHRD_ATM: return ZEBRA_LLT_ATM;
488     case ARPHRD_METRICOM: return ZEBRA_LLT_METRICOM;
489     case ARPHRD_IEEE1394: return ZEBRA_LLT_IEEE1394;
490     case ARPHRD_EUI64: return ZEBRA_LLT_EUI64;
491     case ARPHRD_INFINIBAND: return ZEBRA_LLT_INFINIBAND;
492     case ARPHRD_SLIP: return ZEBRA_LLT_SLIP;
493     case ARPHRD_CSLIP: return ZEBRA_LLT_CSLIP;
494     case ARPHRD_SLIP6: return ZEBRA_LLT_SLIP6;
495     case ARPHRD_CSLIP6: return ZEBRA_LLT_CSLIP6;
496     case ARPHRD_RSRVD: return ZEBRA_LLT_RSRVD;
497     case ARPHRD_ADAPT: return ZEBRA_LLT_ADAPT;
498     case ARPHRD_ROSE: return ZEBRA_LLT_ROSE;
499     case ARPHRD_X25: return ZEBRA_LLT_X25;
500     case ARPHRD_PPP: return ZEBRA_LLT_PPP;
501     case ARPHRD_CISCO: return ZEBRA_LLT_CHDLC;
502     case ARPHRD_LAPB: return ZEBRA_LLT_LAPB;
503     case ARPHRD_RAWHDLC: return ZEBRA_LLT_RAWHDLC;
504     case ARPHRD_TUNNEL: return ZEBRA_LLT_IPIP;
505     case ARPHRD_TUNNEL6: return ZEBRA_LLT_IPIP6;
506     case ARPHRD_FRAD: return ZEBRA_LLT_FRAD;
507     case ARPHRD_SKIP: return ZEBRA_LLT_SKIP;
508     case ARPHRD_LOOPBACK: return ZEBRA_LLT_LOOPBACK;
509     case ARPHRD_LOCALTLK: return ZEBRA_LLT_LOCALTLK;
510     case ARPHRD_FDDI: return ZEBRA_LLT_FDDI;
511     case ARPHRD_SIT: return ZEBRA_LLT_SIT;
512     case ARPHRD_IPDDP: return ZEBRA_LLT_IPDDP;
513     case ARPHRD_IPGRE: return ZEBRA_LLT_IPGRE;
514     case ARPHRD_PIMREG: return ZEBRA_LLT_PIMREG;
515     case ARPHRD_HIPPI: return ZEBRA_LLT_HIPPI;
516     case ARPHRD_ECONET: return ZEBRA_LLT_ECONET;
517     case ARPHRD_IRDA: return ZEBRA_LLT_IRDA;
518     case ARPHRD_FCPP: return ZEBRA_LLT_FCPP;
519     case ARPHRD_FCAL: return ZEBRA_LLT_FCAL;
520     case ARPHRD_FCPL: return ZEBRA_LLT_FCPL;
521     case ARPHRD_FCFABRIC: return ZEBRA_LLT_FCFABRIC;
522     case ARPHRD_IEEE802_TR: return ZEBRA_LLT_IEEE802_TR;
523     case ARPHRD_IEEE80211: return ZEBRA_LLT_IEEE80211;
524     case ARPHRD_IEEE802154: return ZEBRA_LLT_IEEE802154;
525 #ifdef ARPHRD_IP6GRE
526     case ARPHRD_IP6GRE: return ZEBRA_LLT_IP6GRE;
527 #endif
528 #ifdef ARPHRD_IEEE802154_PHY
529     case ARPHRD_IEEE802154_PHY: return ZEBRA_LLT_IEEE802154_PHY;
530 #endif
531
532     default: return ZEBRA_LLT_UNKNOWN;
533   }
534 }
535
536 /* Called from interface_lookup_netlink().  This function is only used
537    during bootstrap. */
538 static int
539 netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h,
540     vrf_id_t vrf_id)
541 {
542   int len;
543   struct ifinfomsg *ifi;
544   struct rtattr *tb[IFLA_MAX + 1];
545   struct interface *ifp;
546   char *name;
547
548   ifi = NLMSG_DATA (h);
549
550   if (h->nlmsg_type != RTM_NEWLINK)
551     return 0;
552
553   len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
554   if (len < 0)
555     return -1;
556
557   /* Looking up interface name. */
558   memset (tb, 0, sizeof tb);
559   netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
560   
561 #ifdef IFLA_WIRELESS
562   /* check for wireless messages to ignore */
563   if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
564     {
565       if (IS_ZEBRA_DEBUG_KERNEL)
566         zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
567       return 0;
568     }
569 #endif /* IFLA_WIRELESS */
570
571   if (tb[IFLA_IFNAME] == NULL)
572     return -1;
573   name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
574
575   /* Add interface. */
576   ifp = if_get_by_name_vrf (name, vrf_id);
577   set_ifindex(ifp, ifi->ifi_index);
578   ifp->flags = ifi->ifi_flags & 0x0000fffff;
579   ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]);
580   ifp->metric = 0;
581
582   /* Hardware type and address. */
583   ifp->ll_type = netlink_to_zebra_link_type (ifi->ifi_type);
584   netlink_interface_update_hw_addr (tb, ifp);
585
586   if_add_update (ifp);
587
588   return 0;
589 }
590
591 /* Lookup interface IPv4/IPv6 address. */
592 static int
593 netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h,
594     vrf_id_t vrf_id)
595 {
596   int len;
597   struct ifaddrmsg *ifa;
598   struct rtattr *tb[IFA_MAX + 1];
599   struct interface *ifp;
600   void *addr;
601   void *broad;
602   u_char flags = 0;
603   char *label = NULL;
604
605   ifa = NLMSG_DATA (h);
606
607   if (ifa->ifa_family != AF_INET
608 #ifdef HAVE_IPV6
609       && ifa->ifa_family != AF_INET6
610 #endif /* HAVE_IPV6 */
611     )
612     return 0;
613
614   if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
615     return 0;
616
617   len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
618   if (len < 0)
619     return -1;
620
621   memset (tb, 0, sizeof tb);
622   netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
623
624   ifp = if_lookup_by_index_vrf (ifa->ifa_index, vrf_id);
625   if (ifp == NULL)
626     {
627       zlog_err ("netlink_interface_addr can't find interface by index %d vrf %u",
628                 ifa->ifa_index, vrf_id);
629       return -1;
630     }
631
632   if (IS_ZEBRA_DEBUG_KERNEL)    /* remove this line to see initial ifcfg */
633     {
634       char buf[BUFSIZ];
635       zlog_debug ("netlink_interface_addr %s %s vrf %u:",
636                  lookup (nlmsg_str, h->nlmsg_type), ifp->name, vrf_id);
637       if (tb[IFA_LOCAL])
638         zlog_debug ("  IFA_LOCAL     %s/%d",
639                     inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]),
640                                buf, BUFSIZ), ifa->ifa_prefixlen);
641       if (tb[IFA_ADDRESS])
642         zlog_debug ("  IFA_ADDRESS   %s/%d",
643                     inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]),
644                                buf, BUFSIZ), ifa->ifa_prefixlen);
645       if (tb[IFA_BROADCAST])
646         zlog_debug ("  IFA_BROADCAST %s/%d",
647                     inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]),
648                                buf, BUFSIZ), ifa->ifa_prefixlen);
649       if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
650         zlog_debug ("  IFA_LABEL     %s", (char *)RTA_DATA (tb[IFA_LABEL]));
651       
652       if (tb[IFA_CACHEINFO])
653         {
654           struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]);
655           zlog_debug ("  IFA_CACHEINFO pref %d, valid %d",
656                       ci->ifa_prefered, ci->ifa_valid);
657         }
658     }
659   
660   /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
661   if (tb[IFA_LOCAL] == NULL)
662     tb[IFA_LOCAL] = tb[IFA_ADDRESS];
663   if (tb[IFA_ADDRESS] == NULL)
664     tb[IFA_ADDRESS] = tb[IFA_LOCAL];
665   
666   /* local interface address */
667   addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
668
669   /* is there a peer address? */
670   if (tb[IFA_ADDRESS] &&
671       memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS])))
672     {
673       broad = RTA_DATA(tb[IFA_ADDRESS]);
674       SET_FLAG (flags, ZEBRA_IFA_PEER);
675     }
676   else
677     /* seeking a broadcast address */
678     broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL);
679
680   /* addr is primary key, SOL if we don't have one */
681   if (addr == NULL)
682     {
683       zlog_debug ("%s: NULL address", __func__);
684       return -1;
685     }
686
687   /* Flags. */
688   if (ifa->ifa_flags & IFA_F_SECONDARY)
689     SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
690
691   /* Label */
692   if (tb[IFA_LABEL])
693     label = (char *) RTA_DATA (tb[IFA_LABEL]);
694
695   if (ifp && label && strcmp (ifp->name, label) == 0)
696     label = NULL;
697
698   /* Register interface address to the interface. */
699   if (ifa->ifa_family == AF_INET)
700     {
701       if (h->nlmsg_type == RTM_NEWADDR)
702         connected_add_ipv4 (ifp, flags,
703                             (struct in_addr *) addr, ifa->ifa_prefixlen,
704                             (struct in_addr *) broad, label);
705       else
706         connected_delete_ipv4 (ifp, flags,
707                                (struct in_addr *) addr, ifa->ifa_prefixlen,
708                                (struct in_addr *) broad);
709     }
710 #ifdef HAVE_IPV6
711   if (ifa->ifa_family == AF_INET6)
712     {
713       if (h->nlmsg_type == RTM_NEWADDR)
714         connected_add_ipv6 (ifp, flags,
715                             (struct in6_addr *) addr, ifa->ifa_prefixlen,
716                             (struct in6_addr *) broad, label);
717       else
718         connected_delete_ipv6 (ifp,
719                                (struct in6_addr *) addr, ifa->ifa_prefixlen,
720                                (struct in6_addr *) broad);
721     }
722 #endif /* HAVE_IPV6 */
723
724   return 0;
725 }
726
727 /* Looking up routing table by netlink interface. */
728 static int
729 netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h,
730     vrf_id_t vrf_id)
731 {
732   int len;
733   struct rtmsg *rtm;
734   struct rtattr *tb[RTA_MAX + 1];
735   u_char flags = 0;
736
737   char anyaddr[16] = { 0 };
738
739   int index;
740   int table;
741   u_int32_t mtu = 0;
742
743   void *dest;
744   void *gate;
745   void *src;
746
747   rtm = NLMSG_DATA (h);
748
749   if (h->nlmsg_type != RTM_NEWROUTE)
750     return 0;
751   if (rtm->rtm_type != RTN_UNICAST)
752     return 0;
753
754   table = rtm->rtm_table;
755 #if 0                           /* we weed them out later in rib_weed_tables () */
756   if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
757     return 0;
758 #endif
759
760   len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
761   if (len < 0)
762     return -1;
763
764   memset (tb, 0, sizeof tb);
765   netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
766
767   if (rtm->rtm_flags & RTM_F_CLONED)
768     return 0;
769   if (rtm->rtm_protocol == RTPROT_REDIRECT)
770     return 0;
771   if (rtm->rtm_protocol == RTPROT_KERNEL)
772     return 0;
773
774   if (rtm->rtm_src_len != 0)
775     return 0;
776
777   /* Route which inserted by Zebra. */
778   if (rtm->rtm_protocol == RTPROT_ZEBRA)
779     flags |= ZEBRA_FLAG_SELFROUTE;
780
781   index = 0;
782   dest = NULL;
783   gate = NULL;
784   src = NULL;
785
786   if (tb[RTA_OIF])
787     index = *(int *) RTA_DATA (tb[RTA_OIF]);
788
789   if (tb[RTA_DST])
790     dest = RTA_DATA (tb[RTA_DST]);
791   else
792     dest = anyaddr;
793
794   if (tb[RTA_PREFSRC])
795     src = RTA_DATA (tb[RTA_PREFSRC]);
796
797   if (tb[RTA_GATEWAY])
798     gate = RTA_DATA (tb[RTA_GATEWAY]);
799
800   if (tb[RTA_METRICS])
801     {
802       struct rtattr *mxrta[RTAX_MAX+1];
803
804       memset (mxrta, 0, sizeof mxrta);
805       netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
806                             RTA_PAYLOAD(tb[RTA_METRICS]));
807
808       if (mxrta[RTAX_MTU])
809         mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]);
810     }
811
812   if (rtm->rtm_family == AF_INET)
813     {
814       struct prefix_ipv4 p;
815       p.family = AF_INET;
816       memcpy (&p.prefix, dest, 4);
817       p.prefixlen = rtm->rtm_dst_len;
818
819       if (!tb[RTA_MULTIPATH])
820           rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index,
821                         vrf_id, table, 0, mtu, 0, SAFI_UNICAST);
822       else
823         {
824           /* This is a multipath route */
825
826           struct rib *rib;
827           struct rtnexthop *rtnh =
828             (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
829
830           len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
831
832           rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
833           rib->type = ZEBRA_ROUTE_KERNEL;
834           rib->distance = 0;
835           rib->flags = flags;
836           rib->metric = 0;
837           rib->mtu = mtu;
838           rib->vrf_id = vrf_id;
839           rib->table = table;
840           rib->nexthop_num = 0;
841           rib->uptime = time (NULL);
842
843           for (;;)
844             {
845               if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
846                 break;
847
848               index = rtnh->rtnh_ifindex;
849               gate = 0;
850               if (rtnh->rtnh_len > sizeof (*rtnh))
851                 {
852                   memset (tb, 0, sizeof (tb));
853                   netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
854                                         rtnh->rtnh_len - sizeof (*rtnh));
855                   if (tb[RTA_GATEWAY])
856                     gate = RTA_DATA (tb[RTA_GATEWAY]);
857                 }
858
859               if (gate)
860                 {
861                   if (index)
862                     rib_nexthop_ipv4_ifindex_add (rib, gate, src, index);
863                   else
864                     rib_nexthop_ipv4_add (rib, gate, src);
865                 }
866               else
867                 rib_nexthop_ifindex_add (rib, index);
868
869               len -= NLMSG_ALIGN(rtnh->rtnh_len);
870               rtnh = RTNH_NEXT(rtnh);
871             }
872
873           if (rib->nexthop_num == 0)
874             XFREE (MTYPE_RIB, rib);
875           else
876             rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
877         }
878     }
879 #ifdef HAVE_IPV6
880   if (rtm->rtm_family == AF_INET6)
881     {
882       struct prefix_ipv6 p;
883       p.family = AF_INET6;
884       memcpy (&p.prefix, dest, 16);
885       p.prefixlen = rtm->rtm_dst_len;
886
887       rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, vrf_id,
888                     table, 0, mtu, 0, SAFI_UNICAST);
889     }
890 #endif /* HAVE_IPV6 */
891
892   return 0;
893 }
894
895 static const struct message rtproto_str[] = {
896   {RTPROT_REDIRECT, "redirect"},
897   {RTPROT_KERNEL,   "kernel"},
898   {RTPROT_BOOT,     "boot"},
899   {RTPROT_STATIC,   "static"},
900   {RTPROT_GATED,    "GateD"},
901   {RTPROT_RA,       "router advertisement"},
902   {RTPROT_MRT,      "MRT"},
903   {RTPROT_ZEBRA,    "Zebra"},
904 #ifdef RTPROT_BIRD
905   {RTPROT_BIRD,     "BIRD"},
906 #endif /* RTPROT_BIRD */
907   {0,               NULL}
908 };
909
910 /* Routing information change from the kernel. */
911 static int
912 netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h,
913     vrf_id_t vrf_id)
914 {
915   int len;
916   struct rtmsg *rtm;
917   struct rtattr *tb[RTA_MAX + 1];
918   u_char zebra_flags = 0;
919
920   char anyaddr[16] = { 0 };
921
922   int index;
923   int table;
924   u_int32_t mtu = 0;
925
926   void *dest;
927   void *gate;
928   void *src;
929
930   rtm = NLMSG_DATA (h);
931
932   if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE))
933     {
934       /* If this is not route add/delete message print warning. */
935       zlog_warn ("Kernel message: %d vrf %u\n", h->nlmsg_type, vrf_id);
936       return 0;
937     }
938
939   /* Connected route. */
940   if (IS_ZEBRA_DEBUG_KERNEL)
941     zlog_debug ("%s %s %s proto %s vrf %u",
942                h->nlmsg_type ==
943                RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
944                rtm->rtm_family == AF_INET ? "ipv4" : "ipv6",
945                rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast",
946                lookup (rtproto_str, rtm->rtm_protocol),
947                vrf_id);
948
949   if (rtm->rtm_type != RTN_UNICAST)
950     {
951       return 0;
952     }
953
954   table = rtm->rtm_table;
955   if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default)
956     {
957       return 0;
958     }
959
960   len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg));
961   if (len < 0)
962     return -1;
963
964   memset (tb, 0, sizeof tb);
965   netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len);
966
967   if (rtm->rtm_flags & RTM_F_CLONED)
968     return 0;
969   if (rtm->rtm_protocol == RTPROT_REDIRECT)
970     return 0;
971   if (rtm->rtm_protocol == RTPROT_KERNEL)
972     return 0;
973
974   if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE)
975     return 0;
976   if (rtm->rtm_protocol == RTPROT_ZEBRA)
977     SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE);
978
979   if (rtm->rtm_src_len != 0)
980     {
981       zlog_warn ("netlink_route_change(): no src len, vrf %u", vrf_id);
982       return 0;
983     }
984
985   index = 0;
986   dest = NULL;
987   gate = NULL;
988   src = NULL;
989
990   if (tb[RTA_OIF])
991     index = *(int *) RTA_DATA (tb[RTA_OIF]);
992
993   if (tb[RTA_DST])
994     dest = RTA_DATA (tb[RTA_DST]);
995   else
996     dest = anyaddr;
997
998   if (tb[RTA_GATEWAY])
999     gate = RTA_DATA (tb[RTA_GATEWAY]);
1000
1001   if (tb[RTA_PREFSRC])
1002     src = RTA_DATA (tb[RTA_PREFSRC]);
1003
1004   if (h->nlmsg_type == RTM_NEWROUTE)
1005     {
1006       if (tb[RTA_METRICS])
1007         {
1008           struct rtattr *mxrta[RTAX_MAX+1];
1009
1010           memset (mxrta, 0, sizeof mxrta);
1011           netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
1012                                 RTA_PAYLOAD(tb[RTA_METRICS]));
1013
1014           if (mxrta[RTAX_MTU])
1015             mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]);
1016         }
1017     }
1018
1019   if (rtm->rtm_family == AF_INET)
1020     {
1021       struct prefix_ipv4 p;
1022       p.family = AF_INET;
1023       memcpy (&p.prefix, dest, 4);
1024       p.prefixlen = rtm->rtm_dst_len;
1025
1026       if (IS_ZEBRA_DEBUG_KERNEL)
1027         {
1028           char buf[PREFIX_STRLEN];
1029           zlog_debug ("%s %s vrf %u",
1030                       h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
1031                       prefix2str (&p, buf, sizeof(buf)), vrf_id);
1032         }
1033
1034       if (h->nlmsg_type == RTM_NEWROUTE)
1035         {
1036           if (!tb[RTA_MULTIPATH])
1037             rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, vrf_id,
1038                           table, 0, mtu, 0, SAFI_UNICAST);
1039           else
1040             {
1041               /* This is a multipath route */
1042
1043               struct rib *rib;
1044               struct rtnexthop *rtnh =
1045                 (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
1046
1047               len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
1048
1049               rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1050               rib->type = ZEBRA_ROUTE_KERNEL;
1051               rib->distance = 0;
1052               rib->flags = 0;
1053               rib->metric = 0;
1054               rib->mtu = mtu;
1055               rib->vrf_id = vrf_id;
1056               rib->table = table;
1057               rib->nexthop_num = 0;
1058               rib->uptime = time (NULL);
1059
1060               for (;;)
1061                 {
1062                   if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
1063                     break;
1064
1065                   index = rtnh->rtnh_ifindex;
1066                   gate = 0;
1067                   if (rtnh->rtnh_len > sizeof (*rtnh))
1068                     {
1069                       memset (tb, 0, sizeof (tb));
1070                       netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
1071                                             rtnh->rtnh_len - sizeof (*rtnh));
1072                       if (tb[RTA_GATEWAY])
1073                         gate = RTA_DATA (tb[RTA_GATEWAY]);
1074                     }
1075
1076                   if (gate)
1077                     {
1078                       if (index)
1079                         rib_nexthop_ipv4_ifindex_add (rib, gate, src, index);
1080                       else
1081                         rib_nexthop_ipv4_add (rib, gate, src);
1082                     }
1083                   else
1084                     rib_nexthop_ifindex_add (rib, index);
1085
1086                   len -= NLMSG_ALIGN(rtnh->rtnh_len);
1087                   rtnh = RTNH_NEXT(rtnh);
1088                 }
1089
1090               if (rib->nexthop_num == 0)
1091                 XFREE (MTYPE_RIB, rib);
1092               else
1093                 rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
1094             }
1095         }
1096       else
1097         rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, gate,
1098                          index, vrf_id, SAFI_UNICAST);
1099     }
1100
1101 #ifdef HAVE_IPV6
1102   if (rtm->rtm_family == AF_INET6)
1103     {
1104       struct prefix_ipv6 p;
1105
1106       p.family = AF_INET6;
1107       memcpy (&p.prefix, dest, 16);
1108       p.prefixlen = rtm->rtm_dst_len;
1109
1110       if (IS_ZEBRA_DEBUG_KERNEL)
1111         {
1112           char buf[PREFIX_STRLEN];
1113           zlog_debug ("%s %s vrf %u",
1114                       h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE",
1115                       prefix2str (&p, buf, sizeof(buf)), vrf_id);
1116         }
1117
1118       if (h->nlmsg_type == RTM_NEWROUTE)
1119         rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id, table,
1120                       0, mtu, 0, SAFI_UNICAST);
1121       else
1122         rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, gate, index, vrf_id,
1123                          SAFI_UNICAST);
1124     }
1125 #endif /* HAVE_IPV6 */
1126
1127   return 0;
1128 }
1129
1130 static int
1131 netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h,
1132     vrf_id_t vrf_id)
1133 {
1134   int len;
1135   struct ifinfomsg *ifi;
1136   struct rtattr *tb[IFLA_MAX + 1];
1137   struct interface *ifp;
1138   char *name;
1139
1140   ifi = NLMSG_DATA (h);
1141
1142   if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
1143     {
1144       /* If this is not link add/delete message so print warning. */
1145       zlog_warn ("netlink_link_change: wrong kernel message %d vrf %u\n",
1146                  h->nlmsg_type, vrf_id);
1147       return 0;
1148     }
1149
1150   len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
1151   if (len < 0)
1152     return -1;
1153
1154   /* Looking up interface name. */
1155   memset (tb, 0, sizeof tb);
1156   netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
1157
1158 #ifdef IFLA_WIRELESS
1159   /* check for wireless messages to ignore */
1160   if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
1161     {
1162       if (IS_ZEBRA_DEBUG_KERNEL)
1163         zlog_debug ("%s: ignoring IFLA_WIRELESS message, vrf %u", __func__,
1164                     vrf_id);
1165       return 0;
1166     }
1167 #endif /* IFLA_WIRELESS */
1168   
1169   if (tb[IFLA_IFNAME] == NULL)
1170     return -1;
1171   name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
1172
1173   /* Add interface. */
1174   if (h->nlmsg_type == RTM_NEWLINK)
1175     {
1176       ifp = if_lookup_by_name_vrf (name, vrf_id);
1177
1178       if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1179         {
1180           if (ifp == NULL)
1181             ifp = if_get_by_name_vrf (name, vrf_id);
1182
1183           set_ifindex(ifp, ifi->ifi_index);
1184           ifp->flags = ifi->ifi_flags & 0x0000fffff;
1185           ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
1186           ifp->metric = 0;
1187
1188           netlink_interface_update_hw_addr (tb, ifp);
1189
1190           /* If new link is added. */
1191           if_add_update (ifp);
1192         }
1193       else
1194         {
1195           /* Interface status change. */
1196           set_ifindex(ifp, ifi->ifi_index);
1197           ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
1198           ifp->metric = 0;
1199
1200           netlink_interface_update_hw_addr (tb, ifp);
1201
1202           if (if_is_operative (ifp))
1203             {
1204               ifp->flags = ifi->ifi_flags & 0x0000fffff;
1205               if (!if_is_operative (ifp))
1206                 if_down (ifp);
1207               else
1208                 /* Must notify client daemons of new interface status. */
1209                 zebra_interface_up_update (ifp);
1210             }
1211           else
1212             {
1213               ifp->flags = ifi->ifi_flags & 0x0000fffff;
1214               if (if_is_operative (ifp))
1215                 if_up (ifp);
1216             }
1217         }
1218     }
1219   else
1220     {
1221       /* RTM_DELLINK. */
1222       ifp = if_lookup_by_name_vrf (name, vrf_id);
1223
1224       if (ifp == NULL)
1225         {
1226           zlog_warn ("interface %s vrf %u is deleted but can't find",
1227                      name, vrf_id);
1228           return 0;
1229         }
1230
1231       if_delete_update (ifp);
1232     }
1233
1234   return 0;
1235 }
1236
1237 static int
1238 netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h,
1239     vrf_id_t vrf_id)
1240 {
1241   /* JF: Ignore messages that aren't from the kernel */
1242   if ( snl->nl_pid != 0 )
1243     {
1244       zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid );
1245       return 0;
1246     }
1247
1248   switch (h->nlmsg_type)
1249     {
1250     case RTM_NEWROUTE:
1251       return netlink_route_change (snl, h, vrf_id);
1252       break;
1253     case RTM_DELROUTE:
1254       return netlink_route_change (snl, h, vrf_id);
1255       break;
1256     case RTM_NEWLINK:
1257       return netlink_link_change (snl, h, vrf_id);
1258       break;
1259     case RTM_DELLINK:
1260       return netlink_link_change (snl, h, vrf_id);
1261       break;
1262     case RTM_NEWADDR:
1263       return netlink_interface_addr (snl, h, vrf_id);
1264       break;
1265     case RTM_DELADDR:
1266       return netlink_interface_addr (snl, h, vrf_id);
1267       break;
1268     default:
1269       zlog_warn ("Unknown netlink nlmsg_type %d vrf %u\n", h->nlmsg_type,
1270                  vrf_id);
1271       break;
1272     }
1273   return 0;
1274 }
1275
1276 /* Interface lookup by netlink socket. */
1277 int
1278 interface_lookup_netlink (struct zebra_vrf *zvrf)
1279 {
1280   int ret;
1281
1282   /* Get interface information. */
1283   ret = netlink_request (AF_PACKET, RTM_GETLINK, &zvrf->netlink_cmd);
1284   if (ret < 0)
1285     return ret;
1286   ret = netlink_parse_info (netlink_interface, &zvrf->netlink_cmd, zvrf);
1287   if (ret < 0)
1288     return ret;
1289
1290   /* Get IPv4 address of the interfaces. */
1291   ret = netlink_request (AF_INET, RTM_GETADDR, &zvrf->netlink_cmd);
1292   if (ret < 0)
1293     return ret;
1294   ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf);
1295   if (ret < 0)
1296     return ret;
1297
1298 #ifdef HAVE_IPV6
1299   /* Get IPv6 address of the interfaces. */
1300   ret = netlink_request (AF_INET6, RTM_GETADDR, &zvrf->netlink_cmd);
1301   if (ret < 0)
1302     return ret;
1303   ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf);
1304   if (ret < 0)
1305     return ret;
1306 #endif /* HAVE_IPV6 */
1307
1308   return 0;
1309 }
1310
1311 /* Routing table read function using netlink interface.  Only called
1312    bootstrap time. */
1313 int
1314 netlink_route_read (struct zebra_vrf *zvrf)
1315 {
1316   int ret;
1317
1318   /* Get IPv4 routing table. */
1319   ret = netlink_request (AF_INET, RTM_GETROUTE, &zvrf->netlink_cmd);
1320   if (ret < 0)
1321     return ret;
1322   ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf);
1323   if (ret < 0)
1324     return ret;
1325
1326 #ifdef HAVE_IPV6
1327   /* Get IPv6 routing table. */
1328   ret = netlink_request (AF_INET6, RTM_GETROUTE, &zvrf->netlink_cmd);
1329   if (ret < 0)
1330     return ret;
1331   ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf);
1332   if (ret < 0)
1333     return ret;
1334 #endif /* HAVE_IPV6 */
1335
1336   return 0;
1337 }
1338
1339 /* Utility function  comes from iproute2. 
1340    Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1341 int
1342 addattr_l (struct nlmsghdr *n, size_t maxlen, int type, void *data, size_t alen)
1343 {
1344   size_t len;
1345   struct rtattr *rta;
1346
1347   len = RTA_LENGTH (alen);
1348
1349   if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1350     return -1;
1351
1352   rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
1353   rta->rta_type = type;
1354   rta->rta_len = len;
1355   memcpy (RTA_DATA (rta), data, alen);
1356   n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1357
1358   return 0;
1359 }
1360
1361 int
1362 rta_addattr_l (struct rtattr *rta, size_t maxlen, int type, void *data, 
1363                size_t alen)
1364 {
1365   size_t len;
1366   struct rtattr *subrta;
1367
1368   len = RTA_LENGTH (alen);
1369
1370   if (RTA_ALIGN (rta->rta_len) + len > maxlen)
1371     return -1;
1372
1373   subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len));
1374   subrta->rta_type = type;
1375   subrta->rta_len = len;
1376   memcpy (RTA_DATA (subrta), data, alen);
1377   rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len;
1378
1379   return 0;
1380 }
1381
1382 /* Utility function comes from iproute2. 
1383    Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1384 int
1385 addattr32 (struct nlmsghdr *n, size_t maxlen, int type, int data)
1386 {
1387   size_t len;
1388   struct rtattr *rta;
1389
1390   len = RTA_LENGTH (4);
1391
1392   if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen)
1393     return -1;
1394
1395   rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len));
1396   rta->rta_type = type;
1397   rta->rta_len = len;
1398   memcpy (RTA_DATA (rta), &data, 4);
1399   n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len;
1400
1401   return 0;
1402 }
1403
1404 static int
1405 netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h,
1406     vrf_id_t vrf_id)
1407 {
1408   zlog_warn ("netlink_talk: ignoring message type 0x%04x vrf %u", h->nlmsg_type,
1409              vrf_id);
1410   return 0;
1411 }
1412
1413 /* sendmsg() to netlink socket then recvmsg(). */
1414 static int
1415 netlink_talk (struct nlmsghdr *n, struct nlsock *nl, struct zebra_vrf *zvrf)
1416 {
1417   int status;
1418   struct sockaddr_nl snl;
1419   struct iovec iov = {
1420     .iov_base = (void *) n,
1421     .iov_len = n->nlmsg_len
1422   };
1423   struct msghdr msg = {
1424     .msg_name = (void *) &snl,
1425     .msg_namelen = sizeof snl,
1426     .msg_iov = &iov,
1427     .msg_iovlen = 1,
1428   };
1429   int save_errno;
1430
1431   memset (&snl, 0, sizeof snl);
1432   snl.nl_family = AF_NETLINK;
1433
1434   n->nlmsg_seq = ++nl->seq;
1435
1436   /* Request an acknowledgement by setting NLM_F_ACK */
1437   n->nlmsg_flags |= NLM_F_ACK;
1438
1439   if (IS_ZEBRA_DEBUG_KERNEL)
1440     zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name,
1441                lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type,
1442                n->nlmsg_seq);
1443
1444   /* Send message to netlink interface. */
1445   if (zserv_privs.change (ZPRIVS_RAISE))
1446     zlog (NULL, LOG_ERR, "Can't raise privileges");
1447   status = sendmsg (nl->sock, &msg, 0);
1448   save_errno = errno;
1449   if (zserv_privs.change (ZPRIVS_LOWER))
1450     zlog (NULL, LOG_ERR, "Can't lower privileges");
1451
1452   if (status < 0)
1453     {
1454       zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s",
1455             safe_strerror (save_errno));
1456       return -1;
1457     }
1458
1459
1460   /* 
1461    * Get reply from netlink socket. 
1462    * The reply should either be an acknowlegement or an error.
1463    */
1464   return netlink_parse_info (netlink_talk_filter, nl, zvrf);
1465 }
1466
1467 /* This function takes a nexthop as argument and adds
1468  * the appropriate netlink attributes to an existing
1469  * netlink message.
1470  *
1471  * @param routedesc: Human readable description of route type
1472  *                   (direct/recursive, single-/multipath)
1473  * @param bytelen: Length of addresses in bytes.
1474  * @param nexthop: Nexthop information
1475  * @param nlmsg: nlmsghdr structure to fill in.
1476  * @param req_size: The size allocated for the message.
1477  */
1478 static void
1479 _netlink_route_build_singlepath(
1480         const char *routedesc,
1481         int bytelen,
1482         struct nexthop *nexthop,
1483         struct nlmsghdr *nlmsg,
1484         struct rtmsg *rtmsg,
1485         size_t req_size)
1486 {
1487   if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK))
1488     rtmsg->rtm_flags |= RTNH_F_ONLINK;
1489   if (nexthop->type == NEXTHOP_TYPE_IPV4
1490       || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1491     {
1492       addattr_l (nlmsg, req_size, RTA_GATEWAY,
1493                  &nexthop->gate.ipv4, bytelen);
1494       if (nexthop->src.ipv4.s_addr)
1495         addattr_l (nlmsg, req_size, RTA_PREFSRC,
1496                    &nexthop->src.ipv4, bytelen);
1497
1498       if (IS_ZEBRA_DEBUG_KERNEL)
1499         zlog_debug("netlink_route_multipath() (%s): "
1500                    "nexthop via %s if %u",
1501                    routedesc,
1502                    inet_ntoa (nexthop->gate.ipv4),
1503                    nexthop->ifindex);
1504     }
1505 #ifdef HAVE_IPV6
1506   if (nexthop->type == NEXTHOP_TYPE_IPV6
1507       || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1508       || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1509     {
1510       addattr_l (nlmsg, req_size, RTA_GATEWAY,
1511                  &nexthop->gate.ipv6, bytelen);
1512
1513       if (IS_ZEBRA_DEBUG_KERNEL)
1514         zlog_debug("netlink_route_multipath() (%s): "
1515                    "nexthop via %s if %u",
1516                    routedesc,
1517                    inet6_ntoa (nexthop->gate.ipv6),
1518                    nexthop->ifindex);
1519     }
1520 #endif /* HAVE_IPV6 */
1521   if (nexthop->type == NEXTHOP_TYPE_IFINDEX
1522       || nexthop->type == NEXTHOP_TYPE_IFNAME
1523       || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1524     {
1525       addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1526
1527       if (nexthop->src.ipv4.s_addr)
1528         addattr_l (nlmsg, req_size, RTA_PREFSRC,
1529                    &nexthop->src.ipv4, bytelen);
1530
1531       if (IS_ZEBRA_DEBUG_KERNEL)
1532         zlog_debug("netlink_route_multipath() (%s): "
1533                    "nexthop via if %u", routedesc, nexthop->ifindex);
1534     }
1535
1536   if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
1537       || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
1538     {
1539       addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex);
1540
1541       if (IS_ZEBRA_DEBUG_KERNEL)
1542         zlog_debug("netlink_route_multipath() (%s): "
1543                    "nexthop via if %u", routedesc, nexthop->ifindex);
1544     }
1545 }
1546
1547 /* This function takes a nexthop as argument and
1548  * appends to the given rtattr/rtnexthop pair the
1549  * representation of the nexthop. If the nexthop
1550  * defines a preferred source, the src parameter
1551  * will be modified to point to that src, otherwise
1552  * it will be kept unmodified.
1553  *
1554  * @param routedesc: Human readable description of route type
1555  *                   (direct/recursive, single-/multipath)
1556  * @param bytelen: Length of addresses in bytes.
1557  * @param nexthop: Nexthop information
1558  * @param rta: rtnetlink attribute structure
1559  * @param rtnh: pointer to an rtnetlink nexthop structure
1560  * @param src: pointer pointing to a location where
1561  *             the prefsrc should be stored.
1562  */
1563 static void
1564 _netlink_route_build_multipath(
1565         const char *routedesc,
1566         int bytelen,
1567         struct nexthop *nexthop,
1568         struct rtattr *rta,
1569         struct rtnexthop *rtnh,
1570         union g_addr **src
1571         )
1572 {
1573   rtnh->rtnh_len = sizeof (*rtnh);
1574   rtnh->rtnh_flags = 0;
1575   rtnh->rtnh_hops = 0;
1576   rta->rta_len += rtnh->rtnh_len;
1577
1578   if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK))
1579     rtnh->rtnh_flags |= RTNH_F_ONLINK;
1580
1581   if (nexthop->type == NEXTHOP_TYPE_IPV4
1582       || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1583     {
1584       rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY,
1585                      &nexthop->gate.ipv4, bytelen);
1586       rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
1587
1588       if (nexthop->src.ipv4.s_addr)
1589         *src = &nexthop->src;
1590
1591       if (IS_ZEBRA_DEBUG_KERNEL)
1592         zlog_debug("netlink_route_multipath() (%s): "
1593                    "nexthop via %s if %u",
1594                    routedesc,
1595                    inet_ntoa (nexthop->gate.ipv4),
1596                    nexthop->ifindex);
1597     }
1598 #ifdef HAVE_IPV6
1599   if (nexthop->type == NEXTHOP_TYPE_IPV6
1600       || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1601       || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1602     {
1603       rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY,
1604                      &nexthop->gate.ipv6, bytelen);
1605       rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
1606
1607       if (IS_ZEBRA_DEBUG_KERNEL)
1608         zlog_debug("netlink_route_multipath() (%s): "
1609                    "nexthop via %s if %u",
1610                    routedesc,
1611                    inet6_ntoa (nexthop->gate.ipv6),
1612                    nexthop->ifindex);
1613     }
1614 #endif /* HAVE_IPV6 */
1615   /* ifindex */
1616   if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
1617       || nexthop->type == NEXTHOP_TYPE_IFINDEX
1618       || nexthop->type == NEXTHOP_TYPE_IFNAME)
1619     {
1620       rtnh->rtnh_ifindex = nexthop->ifindex;
1621       if (nexthop->src.ipv4.s_addr)
1622         *src = &nexthop->src;
1623       if (IS_ZEBRA_DEBUG_KERNEL)
1624         zlog_debug("netlink_route_multipath() (%s): "
1625                    "nexthop via if %u", routedesc, nexthop->ifindex);
1626     }
1627   else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
1628       || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1629     {
1630       rtnh->rtnh_ifindex = nexthop->ifindex;
1631
1632       if (IS_ZEBRA_DEBUG_KERNEL)
1633         zlog_debug("netlink_route_multipath() (%s): "
1634                    "nexthop via if %u", routedesc, nexthop->ifindex);
1635     }
1636   else
1637     {
1638       rtnh->rtnh_ifindex = 0;
1639     }
1640 }
1641
1642 /* Log debug information for netlink_route_multipath
1643  * if debug logging is enabled.
1644  *
1645  * @param cmd: Netlink command which is to be processed
1646  * @param p: Prefix for which the change is due
1647  * @param nexthop: Nexthop which is currently processed
1648  * @param routedesc: Semantic annotation for nexthop
1649  *                     (recursive, multipath, etc.)
1650  * @param family: Address family which the change concerns
1651  */
1652 static void
1653 _netlink_route_debug(
1654         int cmd,
1655         struct prefix *p,
1656         struct nexthop *nexthop,
1657         const char *routedesc,
1658         int family,
1659         struct zebra_vrf *zvrf)
1660 {
1661   if (IS_ZEBRA_DEBUG_KERNEL)
1662     {
1663       char buf[PREFIX_STRLEN];
1664       zlog_debug ("netlink_route_multipath() (%s): %s %s vrf %u type %s",
1665          routedesc,
1666          lookup (nlmsg_str, cmd),
1667          prefix2str (p, buf, sizeof(buf)),
1668          zvrf->vrf_id,
1669          nexthop_type_to_str (nexthop->type));
1670     }
1671 }
1672
1673 /* Routing table change via netlink interface. */
1674 static int
1675 netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib)
1676 {
1677   int bytelen;
1678   struct sockaddr_nl snl;
1679   struct nexthop *nexthop = NULL, *tnexthop;
1680   int recursing;
1681   int nexthop_num;
1682   int discard;
1683   int family = PREFIX_FAMILY(p);
1684   const char *routedesc;
1685
1686   struct
1687   {
1688     struct nlmsghdr n;
1689     struct rtmsg r;
1690     char buf[NL_PKT_BUF_SIZE];
1691   } req;
1692
1693   struct zebra_vrf *zvrf = vrf_info_lookup (rib->vrf_id);
1694
1695   memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE);
1696
1697   bytelen = (family == AF_INET ? 4 : 16);
1698
1699   req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
1700   req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REPLACE | NLM_F_REQUEST;
1701   req.n.nlmsg_type = cmd;
1702   req.r.rtm_family = family;
1703   req.r.rtm_table = rib->table;
1704   req.r.rtm_dst_len = p->prefixlen;
1705   req.r.rtm_protocol = RTPROT_ZEBRA;
1706   req.r.rtm_scope = RT_SCOPE_LINK;
1707
1708   if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
1709     discard = 1;
1710   else
1711     discard = 0;
1712
1713   if (cmd == RTM_NEWROUTE)
1714     {
1715       if (discard)
1716         {
1717           if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
1718             req.r.rtm_type = RTN_BLACKHOLE;
1719           else if (rib->flags & ZEBRA_FLAG_REJECT)
1720             req.r.rtm_type = RTN_UNREACHABLE;
1721           else
1722             assert (RTN_BLACKHOLE != RTN_UNREACHABLE);  /* false */
1723         }
1724       else
1725         req.r.rtm_type = RTN_UNICAST;
1726     }
1727
1728   addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen);
1729
1730   /* Metric. */
1731   addattr32 (&req.n, sizeof req, RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC);
1732
1733   if (rib->mtu || rib->nexthop_mtu)
1734     {
1735       char buf[NL_PKT_BUF_SIZE];
1736       struct rtattr *rta = (void *) buf;
1737       u_int32_t mtu = rib->mtu;
1738       if (!mtu || (rib->nexthop_mtu && rib->nexthop_mtu < mtu))
1739         mtu = rib->nexthop_mtu;
1740       rta->rta_type = RTA_METRICS;
1741       rta->rta_len = RTA_LENGTH(0);
1742       rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu);
1743       addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA (rta),
1744                  RTA_PAYLOAD (rta));
1745     }
1746
1747   if (discard)
1748     {
1749       if (cmd == RTM_NEWROUTE)
1750         for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1751           {
1752             /* We shouldn't encounter recursive nexthops on discard routes,
1753              * but it is probably better to handle that case correctly anyway.
1754              */
1755             if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1756               continue;
1757             SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1758           }
1759       goto skip;
1760     }
1761
1762   /* Count overall nexthops so we can decide whether to use singlepath
1763    * or multipath case. */
1764   nexthop_num = 0;
1765   for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1766     {
1767       if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1768         continue;
1769       if (cmd == RTM_NEWROUTE && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1770         continue;
1771       if (cmd == RTM_DELROUTE && !CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
1772         continue;
1773
1774       if (nexthop->type != NEXTHOP_TYPE_IFINDEX &&
1775           nexthop->type != NEXTHOP_TYPE_IFNAME)
1776         req.r.rtm_scope = RT_SCOPE_UNIVERSE;
1777
1778       nexthop_num++;
1779     }
1780
1781   /* Singlepath case. */
1782   if (nexthop_num == 1 || MULTIPATH_NUM == 1)
1783     {
1784       nexthop_num = 0;
1785       for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1786         {
1787           if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1788             continue;
1789
1790           if ((cmd == RTM_NEWROUTE
1791                && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1792               || (cmd == RTM_DELROUTE
1793                   && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1794             {
1795               routedesc = recursing ? "recursive, 1 hop" : "single hop";
1796
1797               _netlink_route_debug(cmd, p, nexthop, routedesc, family, zvrf);
1798               _netlink_route_build_singlepath(routedesc, bytelen,
1799                                               nexthop, &req.n, &req.r,
1800                                               sizeof req);
1801
1802               if (cmd == RTM_NEWROUTE)
1803                 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1804
1805               nexthop_num++;
1806               break;
1807             }
1808         }
1809     }
1810   else
1811     {
1812       char buf[NL_PKT_BUF_SIZE];
1813       struct rtattr *rta = (void *) buf;
1814       struct rtnexthop *rtnh;
1815       union g_addr *src = NULL;
1816
1817       rta->rta_type = RTA_MULTIPATH;
1818       rta->rta_len = RTA_LENGTH (0);
1819       rtnh = RTA_DATA (rta);
1820
1821       nexthop_num = 0;
1822       for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1823         {
1824           if (nexthop_num >= MULTIPATH_NUM)
1825             break;
1826
1827           if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1828             continue;
1829
1830           if ((cmd == RTM_NEWROUTE
1831                && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1832               || (cmd == RTM_DELROUTE
1833                   && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
1834             {
1835               routedesc = recursing ? "recursive, multihop" : "multihop";
1836               nexthop_num++;
1837
1838               _netlink_route_debug(cmd, p, nexthop,
1839                                    routedesc, family, zvrf);
1840               _netlink_route_build_multipath(routedesc, bytelen,
1841                                              nexthop, rta, rtnh, &src);
1842               rtnh = RTNH_NEXT (rtnh);
1843
1844               if (cmd == RTM_NEWROUTE)
1845                 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1846             }
1847         }
1848       if (src)
1849         addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen);
1850
1851       if (rta->rta_len > RTA_LENGTH (0))
1852         addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, RTA_DATA (rta),
1853                    RTA_PAYLOAD (rta));
1854     }
1855
1856   /* If there is no useful nexthop then return. */
1857   if (nexthop_num == 0)
1858     {
1859       if (IS_ZEBRA_DEBUG_KERNEL)
1860         zlog_debug ("netlink_route_multipath(): No useful nexthop.");
1861       return 0;
1862     }
1863
1864 skip:
1865
1866   /* Destination netlink address. */
1867   memset (&snl, 0, sizeof snl);
1868   snl.nl_family = AF_NETLINK;
1869
1870   /* Talk to netlink socket. */
1871   return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf);
1872 }
1873
1874 int
1875 kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new)
1876 {
1877   if (!old && new)
1878     return netlink_route_multipath (RTM_NEWROUTE, p, new);
1879   if (old && !new)
1880     return netlink_route_multipath (RTM_DELROUTE, p, old);
1881
1882    /* Replace, can be done atomically if metric does not change;
1883     * netlink uses [prefix, tos, priority] to identify prefix.
1884     * Now metric is not sent to kernel, so we can just do atomic replace. */
1885   return netlink_route_multipath (RTM_NEWROUTE, p, new);
1886 }
1887
1888 /* Interface address modification. */
1889 static int
1890 netlink_address (int cmd, int family, struct interface *ifp,
1891                  struct connected *ifc)
1892 {
1893   int bytelen;
1894   struct prefix *p;
1895
1896   struct
1897   {
1898     struct nlmsghdr n;
1899     struct ifaddrmsg ifa;
1900     char buf[NL_PKT_BUF_SIZE];
1901   } req;
1902
1903   struct zebra_vrf *zvrf = vrf_info_lookup (ifp->vrf_id);
1904
1905   p = ifc->address;
1906   memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE);
1907
1908   bytelen = (family == AF_INET ? 4 : 16);
1909
1910   req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
1911   req.n.nlmsg_flags = NLM_F_REQUEST;
1912   req.n.nlmsg_type = cmd;
1913   req.ifa.ifa_family = family;
1914
1915   req.ifa.ifa_index = ifp->ifindex;
1916   req.ifa.ifa_prefixlen = p->prefixlen;
1917
1918   addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1919
1920   if (family == AF_INET && cmd == RTM_NEWADDR)
1921     {
1922       if (!CONNECTED_PEER(ifc) && ifc->destination)
1923         {
1924           p = ifc->destination;
1925           addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
1926                      bytelen);
1927         }
1928     }
1929
1930   if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
1931     SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
1932
1933   if (ifc->label)
1934     addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
1935                strlen (ifc->label) + 1);
1936
1937   return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf);
1938 }
1939
1940 int
1941 kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
1942 {
1943   return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
1944 }
1945
1946 int
1947 kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
1948 {
1949   return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
1950 }
1951
1952
1953 extern struct thread_master *master;
1954
1955 /* Kernel route reflection. */
1956 static int
1957 kernel_read (struct thread *thread)
1958 {
1959   struct zebra_vrf *zvrf = (struct zebra_vrf *)THREAD_ARG (thread);
1960   netlink_parse_info (netlink_information_fetch, &zvrf->netlink, zvrf);
1961   zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf,
1962                                      zvrf->netlink.sock);
1963
1964   return 0;
1965 }
1966
1967 /* Filter out messages from self that occur on listener socket,
1968    caused by our actions on the command socket
1969  */
1970 static void netlink_install_filter (int sock, __u32 pid)
1971 {
1972   struct sock_filter filter[] = {
1973     /* 0: ldh [4]                 */
1974     BPF_STMT(BPF_LD|BPF_ABS|BPF_H, offsetof(struct nlmsghdr, nlmsg_type)),
1975     /* 1: jeq 0x18 jt 3 jf 6  */
1976     BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_NEWROUTE), 1, 0),
1977     /* 2: jeq 0x19 jt 3 jf 6  */
1978     BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_DELROUTE), 0, 3),
1979     /* 3: ldw [12]                */
1980     BPF_STMT(BPF_LD|BPF_ABS|BPF_W, offsetof(struct nlmsghdr, nlmsg_pid)),
1981     /* 4: jeq XX  jt 5 jf 6   */
1982     BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htonl(pid), 0, 1),
1983     /* 5: ret 0    (skip)     */
1984     BPF_STMT(BPF_RET|BPF_K, 0),
1985     /* 6: ret 0xffff (keep)   */
1986     BPF_STMT(BPF_RET|BPF_K, 0xffff),
1987   };
1988
1989   struct sock_fprog prog = {
1990     .len = array_size(filter),
1991     .filter = filter,
1992   };
1993
1994   if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0)
1995     zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno));
1996 }
1997
1998 /* Exported interface function.  This function simply calls
1999    netlink_socket (). */
2000 void
2001 kernel_init (struct zebra_vrf *zvrf)
2002 {
2003   unsigned long groups;
2004
2005   groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR;
2006 #ifdef HAVE_IPV6
2007   groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR;
2008 #endif /* HAVE_IPV6 */
2009   netlink_socket (&zvrf->netlink, groups, zvrf->vrf_id);
2010   netlink_socket (&zvrf->netlink_cmd, 0, zvrf->vrf_id);
2011
2012   /* Register kernel socket. */
2013   if (zvrf->netlink.sock > 0)
2014     {
2015       size_t bufsize = MAX(nl_rcvbufsize, 2 * sysconf(_SC_PAGESIZE));
2016       
2017       /* Only want non-blocking on the netlink event socket */
2018       if (fcntl (zvrf->netlink.sock, F_SETFL, O_NONBLOCK) < 0)
2019         zlog_err ("Can't set %s socket flags: %s", zvrf->netlink.name,
2020                   safe_strerror (errno));
2021
2022       /* Set receive buffer size if it's set from command line */
2023       if (nl_rcvbufsize)
2024         netlink_recvbuf (&zvrf->netlink, nl_rcvbufsize);
2025       
2026       nl_rcvbuf.p = XMALLOC (MTYPE_NETLINK_RCVBUF, bufsize);
2027       nl_rcvbuf.size = bufsize;
2028       
2029       netlink_install_filter (zvrf->netlink.sock, zvrf->netlink_cmd.snl.nl_pid);
2030       zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf,
2031                                          zvrf->netlink.sock);
2032     }
2033 }
2034
2035 void
2036 kernel_terminate (struct zebra_vrf *zvrf)
2037 {
2038   THREAD_READ_OFF (zvrf->t_netlink);
2039
2040   if (zvrf->netlink.sock >= 0)
2041     {
2042       close (zvrf->netlink.sock);
2043       zvrf->netlink.sock = -1;
2044     }
2045
2046   if (zvrf->netlink_cmd.sock >= 0)
2047     {
2048       close (zvrf->netlink_cmd.sock);
2049       zvrf->netlink_cmd.sock = -1;
2050     }
2051 }
2052
2053 /*
2054  * nl_msg_type_to_str
2055  */
2056 const char *
2057 nl_msg_type_to_str (uint16_t msg_type)
2058 {
2059   return lookup (nlmsg_str, msg_type);
2060 }
2061
2062 /*
2063  * nl_rtproto_to_str
2064  */
2065 const char *
2066 nl_rtproto_to_str (u_char rtproto)
2067 {
2068   return lookup (rtproto_str, rtproto);
2069 }