]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rt_socket.c
Merge pull request #883 from daveolson53/master
[mirror_frr.git] / zebra / rt_socket.c
CommitLineData
718e3744 1/*
2 * Kernel routing table updates by routing socket.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
ddfeb486
DL
23
24#ifndef HAVE_NETLINK
25
fe6c7157 26#ifdef __OpenBSD__
d3e2c74a
RW
27#include <netmpls/mpls.h>
28#endif
718e3744 29
30#include "if.h"
31#include "prefix.h"
32#include "sockunion.h"
33#include "log.h"
edd7c245 34#include "privs.h"
13d60d35 35#include "vxlan.h"
718e3744 36
37#include "zebra/debug.h"
38#include "zebra/rib.h"
6621ca86 39#include "zebra/rt.h"
dc95824a 40#include "zebra/kernel_socket.h"
d3e2c74a 41#include "zebra/zebra_mpls.h"
718e3744 42
edd7c245 43extern struct zebra_privs_t zserv_privs;
44
6621ca86 45/* kernel socket export */
d62a17ae 46extern int rtm_write(int message, union sockunion *dest, union sockunion *mask,
47 union sockunion *gate, union sockunion *mpls,
48 unsigned int index, int zebra_flags, int metric);
718e3744 49
746c4f02 50#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 51/* Adjust netmask socket length. Return value is a adjusted sin_len
52 value. */
d62a17ae 53static int sin_masklen(struct in_addr mask)
718e3744 54{
d62a17ae 55 char *p, *lim;
56 int len;
57 struct sockaddr_in sin;
718e3744 58
d62a17ae 59 if (mask.s_addr == 0)
60 return sizeof(long);
718e3744 61
d62a17ae 62 sin.sin_addr = mask;
63 len = sizeof(struct sockaddr_in);
718e3744 64
d62a17ae 65 lim = (char *)&sin.sin_addr;
66 p = lim + sizeof(sin.sin_addr);
718e3744 67
d62a17ae 68 while (*--p == 0 && p >= lim)
69 len--;
70 return len;
718e3744 71}
746c4f02 72#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 73
74/* Interface between zebra message and rtm message. */
d62a17ae 75static int kernel_rtm_ipv4(int cmd, struct prefix *p, struct route_entry *re)
718e3744 76
77{
d62a17ae 78 struct sockaddr_in *mask = NULL;
79 struct sockaddr_in sin_dest, sin_mask, sin_gate;
fe6c7157 80#ifdef __OpenBSD__
d62a17ae 81 struct sockaddr_mpls smpls;
d3e2c74a 82#endif
d62a17ae 83 union sockunion *smplsp = NULL;
84 struct nexthop *nexthop;
85 int nexthop_num = 0;
86 ifindex_t ifindex = 0;
87 int gate = 0;
88 int error;
89 char prefix_buf[PREFIX_STRLEN];
90
91 if (IS_ZEBRA_DEBUG_RIB)
92 prefix2str(p, prefix_buf, sizeof(prefix_buf));
93 memset(&sin_dest, 0, sizeof(struct sockaddr_in));
94 sin_dest.sin_family = AF_INET;
6f0e3f6e 95#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 96 sin_dest.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 97#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 98 sin_dest.sin_addr = p->u.prefix4;
718e3744 99
d62a17ae 100 memset(&sin_mask, 0, sizeof(struct sockaddr_in));
718e3744 101
d62a17ae 102 memset(&sin_gate, 0, sizeof(struct sockaddr_in));
103 sin_gate.sin_family = AF_INET;
6f0e3f6e 104#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 105 sin_gate.sin_len = sizeof(struct sockaddr_in);
6f0e3f6e 106#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 107
d62a17ae 108 /* Make gateway. */
109 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
110 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
111 continue;
112
113 gate = 0;
114 char gate_buf[INET_ADDRSTRLEN] = "NULL";
115
116 /*
117 * XXX We need to refrain from kernel operations in some cases,
118 * but this if statement seems overly cautious - what about
119 * other than ADD and DELETE?
120 */
121 if ((cmd == RTM_ADD
122 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
123 || (cmd == RTM_DELETE
124 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))) {
125 if (nexthop->type == NEXTHOP_TYPE_IPV4
126 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
127 sin_gate.sin_addr = nexthop->gate.ipv4;
128 gate = 1;
129 }
130 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
131 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
132 ifindex = nexthop->ifindex;
133 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
134 struct in_addr loopback;
135 loopback.s_addr = htonl(INADDR_LOOPBACK);
136 sin_gate.sin_addr = loopback;
137 gate = 1;
138 }
139
140 if (gate && p->prefixlen == 32)
141 mask = NULL;
142 else {
143 masklen2ip(p->prefixlen, &sin_mask.sin_addr);
144 sin_mask.sin_family = AF_INET;
6f0e3f6e 145#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 146 sin_mask.sin_len =
147 sin_masklen(sin_mask.sin_addr);
6f0e3f6e 148#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 149 mask = &sin_mask;
150 }
718e3744 151
fe6c7157 152#ifdef __OpenBSD__
d62a17ae 153 if (nexthop->nh_label) {
154 memset(&smpls, 0, sizeof(smpls));
155 smpls.smpls_len = sizeof(smpls);
156 smpls.smpls_family = AF_MPLS;
157 smpls.smpls_label =
158 htonl(nexthop->nh_label->label[0]
159 << MPLS_LABEL_OFFSET);
160 smplsp = (union sockunion *)&smpls;
161 }
d3e2c74a
RW
162#endif
163
d62a17ae 164 error = rtm_write(
165 cmd, (union sockunion *)&sin_dest,
166 (union sockunion *)mask,
167 gate ? (union sockunion *)&sin_gate : NULL,
168 smplsp, ifindex, re->flags, re->metric);
169
170 if (IS_ZEBRA_DEBUG_RIB) {
171 if (!gate) {
172 zlog_debug(
173 "%s: %s: attention! gate not found for re %p",
174 __func__, prefix_buf, re);
175 route_entry_dump(p, NULL, re);
176 } else
177 inet_ntop(AF_INET, &sin_gate.sin_addr,
178 gate_buf, INET_ADDRSTRLEN);
179 }
180
181 switch (error) {
182 /* We only flag nexthops as being in FIB if rtm_write()
183 * did its work. */
184 case ZEBRA_ERR_NOERROR:
185 nexthop_num++;
186 if (IS_ZEBRA_DEBUG_RIB)
187 zlog_debug(
188 "%s: %s: successfully did NH %s",
189 __func__, prefix_buf, gate_buf);
190 if (cmd == RTM_ADD)
191 SET_FLAG(nexthop->flags,
192 NEXTHOP_FLAG_FIB);
193 break;
194
195 /* The only valid case for this error is kernel's
196 * failure to install
197 * a multipath route, which is common for FreeBSD. This
198 * should be
199 * ignored silently, but logged as an error otherwise.
200 */
201 case ZEBRA_ERR_RTEXIST:
202 if (cmd != RTM_ADD)
203 zlog_err(
204 "%s: rtm_write() returned %d for command %d",
205 __func__, error, cmd);
206 continue;
207 break;
208
209 /* Given that our NEXTHOP_FLAG_FIB matches real kernel
210 * FIB, it isn't
211 * normal to get any other messages in ANY case.
212 */
213 case ZEBRA_ERR_RTNOEXIST:
214 case ZEBRA_ERR_RTUNREACH:
215 default:
216 zlog_err(
217 "%s: %s: rtm_write() unexpectedly returned %d for command %s",
218 __func__,
219 prefix2str(p, prefix_buf,
220 sizeof(prefix_buf)),
221 error,
222 lookup_msg(rtm_type_str, cmd, NULL));
223 break;
224 }
225 } /* if (cmd and flags make sense) */
226 else if (IS_ZEBRA_DEBUG_RIB)
227 zlog_debug("%s: odd command %s for flags %d", __func__,
228 lookup_msg(rtm_type_str, cmd, NULL),
229 nexthop->flags);
230 } /* for (ALL_NEXTHOPS(...))*/
231
232 /* If there was no useful nexthop, then complain. */
233 if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
234 zlog_debug("%s: No useful nexthops were found in RIB entry %p",
235 __func__, re);
236
237 return 0; /*XXX*/
718e3744 238}
239
746c4f02 240#ifdef SIN6_LEN
718e3744 241/* Calculate sin6_len value for netmask socket value. */
d62a17ae 242static int sin6_masklen(struct in6_addr mask)
718e3744 243{
d62a17ae 244 struct sockaddr_in6 sin6;
245 char *p, *lim;
246 int len;
718e3744 247
d62a17ae 248 if (IN6_IS_ADDR_UNSPECIFIED(&mask))
249 return sizeof(long);
718e3744 250
d62a17ae 251 sin6.sin6_addr = mask;
252 len = sizeof(struct sockaddr_in6);
718e3744 253
d62a17ae 254 lim = (char *)&sin6.sin6_addr;
255 p = lim + sizeof(sin6.sin6_addr);
718e3744 256
d62a17ae 257 while (*--p == 0 && p >= lim)
258 len--;
718e3744 259
d62a17ae 260 return len;
718e3744 261}
746c4f02 262#endif /* SIN6_LEN */
718e3744 263
718e3744 264/* Interface between zebra message and rtm message. */
d62a17ae 265static int kernel_rtm_ipv6(int cmd, struct prefix *p, struct route_entry *re)
718e3744 266{
d62a17ae 267 struct sockaddr_in6 *mask;
268 struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
269 struct nexthop *nexthop;
270 int nexthop_num = 0;
271 ifindex_t ifindex = 0;
272 int gate = 0;
273 int error;
274
275 memset(&sin_dest, 0, sizeof(struct sockaddr_in6));
276 sin_dest.sin6_family = AF_INET6;
718e3744 277#ifdef SIN6_LEN
d62a17ae 278 sin_dest.sin6_len = sizeof(struct sockaddr_in6);
718e3744 279#endif /* SIN6_LEN */
d62a17ae 280 sin_dest.sin6_addr = p->u.prefix6;
718e3744 281
d62a17ae 282 memset(&sin_mask, 0, sizeof(struct sockaddr_in6));
718e3744 283
d62a17ae 284 memset(&sin_gate, 0, sizeof(struct sockaddr_in6));
285 sin_gate.sin6_family = AF_INET6;
6f0e3f6e 286#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 287 sin_gate.sin6_len = sizeof(struct sockaddr_in6);
6f0e3f6e 288#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 289
d62a17ae 290 /* Make gateway. */
291 for (ALL_NEXTHOPS(re->nexthop, nexthop)) {
292 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
293 continue;
fa713d9e 294
d62a17ae 295 gate = 0;
718e3744 296
d62a17ae 297 if ((cmd == RTM_ADD
298 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
299 || (cmd == RTM_DELETE
718e3744 300#if 0
301 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
302#endif
d62a17ae 303 )) {
304 if (nexthop->type == NEXTHOP_TYPE_IPV6
305 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
306 sin_gate.sin6_addr = nexthop->gate.ipv6;
307 gate = 1;
308 }
309 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
310 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
311 ifindex = nexthop->ifindex;
312
313 if (cmd == RTM_ADD)
314 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
315 }
316
317/* Under kame set interface index to link local address. */
718e3744 318#ifdef KAME
319
d62a17ae 320#define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
321 do { \
322 (a).s6_addr[2] = ((i) >> 8) & 0xff; \
323 (a).s6_addr[3] = (i)&0xff; \
324 } while (0)
718e3744 325
d62a17ae 326 if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
327 SET_IN6_LINKLOCAL_IFINDEX(sin_gate.sin6_addr, ifindex);
718e3744 328#endif /* KAME */
329
d62a17ae 330 if (gate && p->prefixlen == 128)
331 mask = NULL;
332 else {
333 masklen2ip6(p->prefixlen, &sin_mask.sin6_addr);
334 sin_mask.sin6_family = AF_INET6;
718e3744 335#ifdef SIN6_LEN
d62a17ae 336 sin_mask.sin6_len = sin6_masklen(sin_mask.sin6_addr);
718e3744 337#endif /* SIN6_LEN */
d62a17ae 338 mask = &sin_mask;
339 }
718e3744 340
d62a17ae 341 error = rtm_write(cmd, (union sockunion *)&sin_dest,
342 (union sockunion *)mask,
343 gate ? (union sockunion *)&sin_gate : NULL,
344 NULL, ifindex, re->flags, re->metric);
718e3744 345
346#if 0
347 if (error)
348 {
be717a0a 349 zlog_info ("kernel_rtm_ipv6(): nexthop %d add error=%d.",
718e3744 350 nexthop_num, error);
351 }
746c4f02 352#else
d62a17ae 353 (void)error;
718e3744 354#endif
355
d62a17ae 356 nexthop_num++;
357 }
718e3744 358
d62a17ae 359 /* If there is no useful nexthop then return. */
360 if (nexthop_num == 0) {
361 if (IS_ZEBRA_DEBUG_KERNEL)
362 zlog_debug("kernel_rtm_ipv6(): No useful nexthop.");
363 return 0;
364 }
718e3744 365
d62a17ae 366 return 0; /*XXX*/
718e3744 367}
368
d62a17ae 369static int kernel_rtm(int cmd, struct prefix *p, struct route_entry *re)
62ccf1e5 370{
d62a17ae 371 switch (PREFIX_FAMILY(p)) {
372 case AF_INET:
373 return kernel_rtm_ipv4(cmd, p, re);
374 case AF_INET6:
375 return kernel_rtm_ipv6(cmd, p, re);
376 }
377 return 0;
62ccf1e5
TT
378}
379
d62a17ae 380int kernel_route_rib(struct prefix *p, struct prefix *src_p,
381 struct route_entry *old, struct route_entry *new)
718e3744 382{
d62a17ae 383 int route = 0;
edd7c245 384
d62a17ae 385 if (src_p && src_p->prefixlen) {
386 zlog_err("route add: IPv6 sourcedest routes unsupported!");
387 return 1;
388 }
05737783 389
d62a17ae 390 if (zserv_privs.change(ZPRIVS_RAISE))
391 zlog_err("Can't raise privileges");
be717a0a 392
d62a17ae 393 if (old)
394 route |= kernel_rtm(RTM_DELETE, p, old);
62ccf1e5 395
d62a17ae 396 if (new)
397 route |= kernel_rtm(RTM_ADD, p, new);
be717a0a 398
d62a17ae 399 if (zserv_privs.change(ZPRIVS_LOWER))
400 zlog_err("Can't lower privileges");
edd7c245 401
d62a17ae 402 return route;
718e3744 403}
6b8a5694 404
d62a17ae 405int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla,
406 int llalen)
6b8a5694 407{
d62a17ae 408 /* TODO */
409 return 0;
6b8a5694 410}
1498c059 411
43b5cc5e 412extern int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *mroute)
1498c059 413{
d62a17ae 414 return 0;
1498c059 415}
13d60d35 416
d62a17ae 417int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 418{
d62a17ae 419 return 0;
13d60d35 420}
421
d62a17ae 422int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip)
13d60d35 423{
d62a17ae 424 return 0;
13d60d35 425}
2232a77c 426
d62a17ae 427int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
428 struct in_addr vtep_ip, u_char sticky)
2232a77c 429{
d62a17ae 430 return 0;
2232a77c 431}
432
d62a17ae 433int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac,
434 struct in_addr vtep_ip, int local)
2232a77c 435{
d62a17ae 436 return 0;
2232a77c 437}
438
d62a17ae 439int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip,
440 struct ethaddr *mac)
2232a77c 441{
d62a17ae 442 return 0;
2232a77c 443}
444
d62a17ae 445int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip)
2232a77c 446{
d62a17ae 447 return 0;
2232a77c 448}
e0ae31b8
DS
449
450extern int kernel_interface_set_master(struct interface *master,
451 struct interface *slave)
452{
453 return 0;
454}
0ecfe5bf 455
ddfeb486 456#endif /* !HAVE_NETLINK */