2 * Code for encoding/decoding FPM messages that are in netlink format.
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of GNU Zebra.
10 * GNU Zebra is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * GNU Zebra is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with GNU Zebra; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 #include "zebra/zserv.h"
33 #include "zebra/zebra_ns.h"
34 #include "zebra/zebra_vrf.h"
35 #include "zebra/kernel_netlink.h"
36 #include "zebra/rt_netlink.h"
39 #include "zebra/zebra_fpm_private.h"
44 * Returns string representation of an address of the given AF.
46 static inline const char *
47 addr_to_a (u_char af
, void *addr
)
50 return "<No address>";
56 return inet_ntoa (*((struct in_addr
*) addr
));
59 return inet6_ntoa (*((struct in6_addr
*) addr
));
62 return "<Addr in unknown AF>";
70 * Convience wrapper that returns a human-readable string for the
71 * address in a prefix.
74 prefix_addr_to_a (struct prefix
*prefix
)
77 return "<No address>";
79 return addr_to_a (prefix
->family
, &prefix
->u
.prefix
);
85 * The size of an address in a given address family.
88 af_addr_size (u_char af
)
108 * Holds information about a single nexthop for netlink. These info
109 * structures are transient and may contain pointers into rib
110 * data structures for convenience.
112 typedef struct netlink_nh_info_t_
115 union g_addr
*gateway
;
118 * Information from the struct nexthop from which this nh was
119 * derived. For debug purposes only.
122 enum nexthop_types_t type
;
126 * netlink_route_info_t
128 * A structure for holding information for a netlink route message.
130 typedef struct netlink_route_info_t_
137 struct prefix
*prefix
;
144 netlink_nh_info_t nhs
[MULTIPATH_NUM
];
145 union g_addr
*pref_src
;
146 } netlink_route_info_t
;
149 * netlink_route_info_add_nh
151 * Add information about the given nexthop to the given route info
154 * Returns TRUE if a nexthop was added, FALSE otherwise.
157 netlink_route_info_add_nh (netlink_route_info_t
*ri
, struct nexthop
*nexthop
,
160 netlink_nh_info_t nhi
;
163 memset (&nhi
, 0, sizeof (nhi
));
166 if (ri
->num_nhs
>= (int) ZEBRA_NUM_OF (ri
->nhs
))
169 nhi
.recursive
= recursive
;
170 nhi
.type
= nexthop
->type
;
171 nhi
.if_index
= nexthop
->ifindex
;
173 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
174 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
176 nhi
.gateway
= &nexthop
->gate
;
177 if (nexthop
->src
.ipv4
.s_addr
)
181 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
182 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
184 nhi
.gateway
= &nexthop
->gate
;
187 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
189 if (nexthop
->src
.ipv4
.s_addr
)
193 if (!nhi
.gateway
&& nhi
.if_index
== 0)
197 * We have a valid nhi. Copy the structure over to the route_info.
199 ri
->nhs
[ri
->num_nhs
] = nhi
;
202 if (src
&& !ri
->pref_src
)
209 * netlink_proto_from_route_type
212 netlink_proto_from_route_type (int type
)
216 case ZEBRA_ROUTE_KERNEL
:
217 case ZEBRA_ROUTE_CONNECT
:
218 return RTPROT_KERNEL
;
226 * netlink_route_info_fill
228 * Fill out the route information object from the given route.
230 * Returns TRUE on success and FALSE on failure.
233 netlink_route_info_fill (netlink_route_info_t
*ri
, int cmd
,
234 rib_dest_t
*dest
, struct rib
*rib
)
236 struct nexthop
*nexthop
, *tnexthop
;
240 memset (ri
, 0, sizeof (*ri
));
242 ri
->prefix
= rib_dest_prefix (dest
);
243 ri
->af
= rib_dest_af (dest
);
245 ri
->nlmsg_type
= cmd
;
246 ri
->rtm_table
= zvrf_id (rib_dest_vrf (dest
));
247 ri
->rtm_protocol
= RTPROT_UNSPEC
;
250 * An RTM_DELROUTE need not be accompanied by any nexthops,
251 * particularly in our communication with the FPM.
253 if (cmd
== RTM_DELROUTE
&& !rib
)
257 ri
->rtm_protocol
= netlink_proto_from_route_type (rib
->type
);
259 if ((rib
->flags
& ZEBRA_FLAG_BLACKHOLE
) || (rib
->flags
& ZEBRA_FLAG_REJECT
))
264 if (cmd
== RTM_NEWROUTE
)
268 if (rib
->flags
& ZEBRA_FLAG_BLACKHOLE
)
269 ri
->rtm_type
= RTN_BLACKHOLE
;
270 else if (rib
->flags
& ZEBRA_FLAG_REJECT
)
271 ri
->rtm_type
= RTN_UNREACHABLE
;
276 ri
->rtm_type
= RTN_UNICAST
;
279 ri
->metric
= &rib
->metric
;
286 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
288 if (ri
->num_nhs
>= MULTIPATH_NUM
)
291 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
294 if ((cmd
== RTM_NEWROUTE
295 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
296 || (cmd
== RTM_DELROUTE
297 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
299 netlink_route_info_add_nh (ri
, nexthop
, recursing
);
303 /* If there is no useful nexthop then return. */
304 if (ri
->num_nhs
== 0)
306 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
315 * netlink_route_info_encode
317 * Returns the number of bytes written to the buffer. 0 or a negative
318 * value indicates an error.
321 netlink_route_info_encode (netlink_route_info_t
*ri
, char *in_buf
,
327 netlink_nh_info_t
*nhi
;
336 req
= (void *) in_buf
;
338 buf_offset
= ((char *) req
->buf
) - ((char *) req
);
340 if (in_buf_len
< buf_offset
) {
345 memset (req
, 0, buf_offset
);
347 bytelen
= af_addr_size (ri
->af
);
349 req
->n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
350 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
351 req
->n
.nlmsg_type
= ri
->nlmsg_type
;
352 req
->r
.rtm_family
= ri
->af
;
353 req
->r
.rtm_table
= ri
->rtm_table
;
354 req
->r
.rtm_dst_len
= ri
->prefix
->prefixlen
;
355 req
->r
.rtm_protocol
= ri
->rtm_protocol
;
356 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
358 addattr_l (&req
->n
, in_buf_len
, RTA_DST
, &ri
->prefix
->u
.prefix
, bytelen
);
360 req
->r
.rtm_type
= ri
->rtm_type
;
364 addattr32 (&req
->n
, in_buf_len
, RTA_PRIORITY
, *ri
->metric
);
366 if (ri
->num_nhs
== 0)
369 if (ri
->num_nhs
== 1)
375 addattr_l (&req
->n
, in_buf_len
, RTA_GATEWAY
, nhi
->gateway
,
381 addattr32 (&req
->n
, in_buf_len
, RTA_OIF
, nhi
->if_index
);
391 char buf
[NL_PKT_BUF_SIZE
];
392 struct rtattr
*rta
= (void *) buf
;
393 struct rtnexthop
*rtnh
;
395 rta
->rta_type
= RTA_MULTIPATH
;
396 rta
->rta_len
= RTA_LENGTH (0);
397 rtnh
= RTA_DATA (rta
);
399 for (nexthop_num
= 0; nexthop_num
< ri
->num_nhs
; nexthop_num
++)
401 nhi
= &ri
->nhs
[nexthop_num
];
403 rtnh
->rtnh_len
= sizeof (*rtnh
);
404 rtnh
->rtnh_flags
= 0;
406 rtnh
->rtnh_ifindex
= 0;
407 rta
->rta_len
+= rtnh
->rtnh_len
;
411 rta_addattr_l (rta
, sizeof (buf
), RTA_GATEWAY
, nhi
->gateway
, bytelen
);
412 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + bytelen
;
417 rtnh
->rtnh_ifindex
= nhi
->if_index
;
420 rtnh
= RTNH_NEXT (rtnh
);
423 assert (rta
->rta_len
> RTA_LENGTH (0));
424 addattr_l (&req
->n
, in_buf_len
, RTA_MULTIPATH
, RTA_DATA (rta
),
431 addattr_l (&req
->n
, in_buf_len
, RTA_PREFSRC
, &ri
->pref_src
, bytelen
);
434 assert (req
->n
.nlmsg_len
< in_buf_len
);
435 return req
->n
.nlmsg_len
;
439 * zfpm_log_route_info
441 * Helper function to log the information in a route_info structure.
444 zfpm_log_route_info (netlink_route_info_t
*ri
, const char *label
)
446 netlink_nh_info_t
*nhi
;
449 zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label
,
450 nl_msg_type_to_str (ri
->nlmsg_type
),
451 prefix_addr_to_a (ri
->prefix
), ri
->prefix
->prefixlen
,
452 nl_rtproto_to_str (ri
->rtm_protocol
),
453 ri
->metric
? *ri
->metric
: 0);
455 for (i
= 0; i
< ri
->num_nhs
; i
++)
458 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
459 nhi
->if_index
, addr_to_a (ri
->af
, nhi
->gateway
),
460 nhi
->recursive
? "yes" : "no",
461 nexthop_type_to_str (nhi
->type
));
466 * zfpm_netlink_encode_route
468 * Create a netlink message corresponding to the given route in the
469 * given buffer space.
471 * Returns the number of bytes written to the buffer. 0 or a negative
472 * value indicates an error.
475 zfpm_netlink_encode_route (int cmd
, rib_dest_t
*dest
, struct rib
*rib
,
476 char *in_buf
, size_t in_buf_len
)
478 netlink_route_info_t ri_space
, *ri
;
482 if (!netlink_route_info_fill (ri
, cmd
, dest
, rib
))
485 zfpm_log_route_info (ri
, __FUNCTION__
);
487 return netlink_route_info_encode (ri
, in_buf
, in_buf_len
);