| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |  *	BIRD -- BSD Routing Table Syncing | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  *	(c) 2004 Ondrej Filip <feela@network.cz> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *	Can be freely distributed and used under the terms of the GNU GPL. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #include <ctype.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <sys/param.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							|  |  |  | #include <sys/sysctl.h>
 | 
					
						
							|  |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #include <netinet/in.h>
 | 
					
						
							|  |  |  | #include <net/route.h>
 | 
					
						
							|  |  |  | #include <net/if.h>
 | 
					
						
							|  |  |  | #include <net/if_dl.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef LOCAL_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "nest/bird.h"
 | 
					
						
							|  |  |  | #include "nest/iface.h"
 | 
					
						
							|  |  |  | #include "nest/route.h"
 | 
					
						
							|  |  |  | #include "nest/protocol.h"
 | 
					
						
							|  |  |  | #include "nest/iface.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-12 11:14:54 +02:00
										 |  |  | #include "sysdep/unix/unix.h"
 | 
					
						
							|  |  |  | #include "sysdep/unix/krt.h"
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #include "lib/string.h"
 | 
					
						
							|  |  |  | #include "lib/socket.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 15:59:44 +01:00
										 |  |  | const int rt_default_ecmp = 0; | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * There are significant differences in multiple tables support between BSD variants. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * OpenBSD has table_id field for routes in route socket protocol, therefore all | 
					
						
							|  |  |  |  * tables could be managed by one kernel socket. FreeBSD lacks such field, | 
					
						
							|  |  |  |  * therefore multiple sockets (locked to specific table using SO_SETFIB socket | 
					
						
							|  |  |  |  * option) must be used. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Both FreeBSD and OpenBSD uses separate scans for each table. In OpenBSD, | 
					
						
							|  |  |  |  * table_id is specified explicitly as sysctl scan argument, while in FreeBSD it | 
					
						
							|  |  |  |  * is handled implicitly by changing default table using setfib() syscall. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * KRT_SHARED_SOCKET	- use shared kernel socked instead of one for each krt_proto | 
					
						
							|  |  |  |  * KRT_USE_SETFIB_SCAN	- use setfib() for sysctl() route scan | 
					
						
							|  |  |  |  * KRT_USE_SETFIB_SOCK	- use SO_SETFIB socket option for kernel sockets | 
					
						
							|  |  |  |  * KRT_USE_SYSCTL_7	- use 7-th arg of sysctl() as table id for route scans | 
					
						
							|  |  |  |  * KRT_USE_SYSCTL_NET_FIBS - use net.fibs sysctl() for dynamic max number of fibs | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __FreeBSD__
 | 
					
						
							|  |  |  | #define KRT_MAX_TABLES 256
 | 
					
						
							|  |  |  | #define KRT_USE_SETFIB_SCAN
 | 
					
						
							|  |  |  | #define KRT_USE_SETFIB_SOCK
 | 
					
						
							|  |  |  | #define KRT_USE_SYSCTL_NET_FIBS
 | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #ifdef __OpenBSD__
 | 
					
						
							|  |  |  | #define KRT_MAX_TABLES (RT_TABLEID_MAX+1)
 | 
					
						
							|  |  |  | #define KRT_SHARED_SOCKET
 | 
					
						
							|  |  |  | #define KRT_USE_SYSCTL_7
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef KRT_MAX_TABLES
 | 
					
						
							|  |  |  | #define KRT_MAX_TABLES 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Dynamic max number of tables */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:56:31 +01:00
										 |  |  | uint krt_max_tables; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_USE_SYSCTL_NET_FIBS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:56:31 +01:00
										 |  |  | static uint | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_get_max_tables(void) | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   int fibs; | 
					
						
							|  |  |  |   size_t fibs_len = sizeof(fibs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (sysctlbyname("net.fibs", &fibs, &fibs_len, NULL, 0) < 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     log(L_WARN "KRT: unable to get max number of fib tables: %m"); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:56:31 +01:00
										 |  |  |   /* Should not happen */ | 
					
						
							|  |  |  |   if (fibs < 1) | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (uint) MIN(fibs, KRT_MAX_TABLES); | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | krt_get_max_tables(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return KRT_MAX_TABLES; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* KRT_USE_SYSCTL_NET_FIBS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* setfib() syscall for FreeBSD scans */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_USE_SETFIB_SCAN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | static int krt_default_fib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | krt_get_active_fib(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int fib; | 
					
						
							|  |  |  |   size_t fib_len = sizeof(fib); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (sysctlbyname("net.my_fibnum", &fib, &fib_len, NULL, 0) < 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     log(L_WARN "KRT: unable to get active fib number: %m"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return fib; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern int setfib(int fib); | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #endif /* KRT_USE_SETFIB_SCAN */
 | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* table_id -> krt_proto map */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_SHARED_SOCKET
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  | static struct krt_proto *krt_table_map[KRT_MAX_TABLES][2]; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | /* Route socket message processing */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | krt_capable(rte *e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   rta *a = e->attrs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return | 
					
						
							| 
									
										
										
										
											2017-12-08 15:59:44 +01:00
										 |  |  |     ((a->dest == RTD_UNICAST && !a->nh.next) /* No multipath support */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #ifdef RTF_REJECT
 | 
					
						
							|  |  |  |      || a->dest == RTD_UNREACHABLE | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef RTF_BLACKHOLE
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |      || a->dest == RTD_BLACKHOLE | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |      ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #ifndef RTAX_MAX
 | 
					
						
							|  |  |  | #define RTAX_MAX 8
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ks_msg | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   struct rt_msghdr rtm; | 
					
						
							|  |  |  |   struct sockaddr_storage buf[RTAX_MAX]; | 
					
						
							| 
									
										
										
										
											2017-09-04 22:32:45 +02:00
										 |  |  | } PACKED; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #define ROUNDUP(a) \
 | 
					
						
							|  |  |  |         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define NEXTADDR(w, u) \
 | 
					
						
							|  |  |  |         if (msg.rtm.rtm_addrs & (w)) {\ | 
					
						
							|  |  |  |           l = ROUNDUP(((struct sockaddr *)&(u))->sa_len);\ | 
					
						
							|  |  |  |           memmove(body, &(u), l); body += l;} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #define GETADDR(p, F) \
 | 
					
						
							|  |  |  |   bzero(p, sizeof(*p));\ | 
					
						
							|  |  |  |   if ((addrs & (F)) && ((struct sockaddr *)body)->sa_len) {\ | 
					
						
							| 
									
										
										
										
											2015-05-19 08:53:34 +02:00
										 |  |  |     uint l = ROUNDUP(((struct sockaddr *)body)->sa_len);\ | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     memcpy(p, body, (l > sizeof(*p) ? sizeof(*p) : l));\ | 
					
						
							|  |  |  |     body += l;} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 14:30:38 +01:00
										 |  |  | static inline void | 
					
						
							|  |  |  | sockaddr_fill_dl(struct sockaddr_dl *sa, struct iface *ifa) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   uint len = OFFSETOF(struct sockaddr_dl, sdl_data); | 
					
						
							|  |  |  |   memset(sa, 0, len); | 
					
						
							|  |  |  |   sa->sdl_len = len; | 
					
						
							|  |  |  |   sa->sdl_family = AF_LINK; | 
					
						
							|  |  |  |   sa->sdl_index = ifa->index; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_send_route(struct krt_proto *p, int cmd, rte *e) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   net *net = e->net; | 
					
						
							|  |  |  |   rta *a = e->attrs; | 
					
						
							|  |  |  |   static int msg_seq; | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |   struct iface *j, *i = a->nh.iface; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   int l; | 
					
						
							|  |  |  |   struct ks_msg msg; | 
					
						
							|  |  |  |   char *body = (char *)msg.buf; | 
					
						
							|  |  |  |   sockaddr gate, mask, dst; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   DBG("krt-sock: send %I/%d via %I\n", net->n.prefix, net->n.pxlen, a->gw); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-01 08:59:47 +00:00
										 |  |  |   bzero(&msg,sizeof (struct rt_msghdr)); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   msg.rtm.rtm_version = RTM_VERSION; | 
					
						
							|  |  |  |   msg.rtm.rtm_type = cmd; | 
					
						
							|  |  |  |   msg.rtm.rtm_seq = msg_seq++; | 
					
						
							|  |  |  |   msg.rtm.rtm_addrs = RTA_DST; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   msg.rtm.rtm_flags = RTF_UP | RTF_PROTO1; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |   /* XXXX */ | 
					
						
							|  |  |  |   if (net_pxlen(net->n.addr) == net_max_prefix_length[net->n.addr->type]) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     msg.rtm.rtm_flags |= RTF_HOST; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     msg.rtm.rtm_addrs |= RTA_NETMASK; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_SHARED_SOCKET
 | 
					
						
							|  |  |  |   msg.rtm.rtm_tableid = KRT_CF->sys.table_id; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef RTF_REJECT
 | 
					
						
							|  |  |  |   if(a->dest == RTD_UNREACHABLE) | 
					
						
							|  |  |  |     msg.rtm.rtm_flags |= RTF_REJECT; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef RTF_BLACKHOLE
 | 
					
						
							|  |  |  |   if(a->dest == RTD_BLACKHOLE) | 
					
						
							|  |  |  |     msg.rtm.rtm_flags |= RTF_BLACKHOLE; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   /*
 | 
					
						
							|  |  |  |    * This is really very nasty, but I'm not able to add reject/blackhole route | 
					
						
							|  |  |  |    * without gateway address. | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   if (!i) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2004-06-01 12:57:13 +00:00
										 |  |  |     WALK_LIST(j, iface_list) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (j->flags & IF_LOOPBACK) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2004-06-01 12:57:13 +00:00
										 |  |  |         i = j; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |     if (!i) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       log(L_ERR "KRT: Cannot find loopback iface"); | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-04-03 11:42:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |   int af = AF_UNSPEC; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch (net->n.addr->type) { | 
					
						
							|  |  |  |     case NET_IP4: | 
					
						
							|  |  |  |       af = AF_INET; | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     case NET_IP6: | 
					
						
							|  |  |  |       af = AF_INET6; | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2017-04-29 00:36:35 +02:00
										 |  |  |       log(L_ERR "KRT: Not sending route %N to kernel", net->n.addr); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |       return -1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sockaddr_fill(&dst,  af, net_prefix(net->n.addr), NULL, 0); | 
					
						
							|  |  |  |   sockaddr_fill(&mask, af, net_pxmask(net->n.addr), NULL, 0); | 
					
						
							| 
									
										
										
										
											2010-04-03 11:42:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   switch (a->dest) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   case RTD_UNICAST: | 
					
						
							|  |  |  |     if (ipa_nonzero(a->nh.gw)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ip_addr gw = a->nh.gw; | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |       /* Embed interface ID to link-local address */ | 
					
						
							|  |  |  |       if (ipa_is_link_local(gw)) | 
					
						
							|  |  |  | 	_I0(gw) = 0xfe800000 | (i->index & 0x0000ffff); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sockaddr_fill(&gate, af, gw, NULL, 0); | 
					
						
							|  |  |  |       msg.rtm.rtm_flags |= RTF_GATEWAY; | 
					
						
							|  |  |  |       msg.rtm.rtm_addrs |= RTA_GATEWAY; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-07-09 18:34:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #ifdef RTF_REJECT
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   case RTD_UNREACHABLE: | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef RTF_BLACKHOLE
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   case RTD_BLACKHOLE: | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   { | 
					
						
							|  |  |  |     /* Fallback for all other valid cases */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 14:30:38 +01:00
										 |  |  | #if __OpenBSD__
 | 
					
						
							|  |  |  |     /* Keeping temporarily old code for OpenBSD */ | 
					
						
							| 
									
										
										
										
											2017-12-07 13:06:01 +01:00
										 |  |  |     struct ifa *addr = (net->n.addr->type == NET_IP4) ? i->addr4 : (i->addr6 ?: i->llv6); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!addr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       log(L_ERR "KRT: interface %s has no IP addess", i->name); | 
					
						
							|  |  |  |       return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 14:30:38 +01:00
										 |  |  |     /* Embed interface ID to link-local address */ | 
					
						
							|  |  |  |     ip_addr gw = addr->ip; | 
					
						
							|  |  |  |     if (ipa_is_link_local(gw)) | 
					
						
							|  |  |  |       _I0(gw) = 0xfe800000 | (i->index & 0x0000ffff); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sockaddr_fill(&gate, af, gw, i, 0); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     sockaddr_fill_dl(&gate, i); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |     msg.rtm.rtm_addrs |= RTA_GATEWAY; | 
					
						
							| 
									
										
										
										
											2017-04-29 00:36:35 +02:00
										 |  |  |     break; | 
					
						
							| 
									
										
										
										
											2017-02-20 02:26:45 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   default: | 
					
						
							|  |  |  |     bug("krt-sock: unknown flags, but not filtered"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-01 10:45:28 +00:00
										 |  |  |   msg.rtm.rtm_index = i->index; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   NEXTADDR(RTA_DST, dst); | 
					
						
							|  |  |  |   NEXTADDR(RTA_GATEWAY, gate); | 
					
						
							|  |  |  |   NEXTADDR(RTA_NETMASK, mask); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   l = body - (char *)&msg; | 
					
						
							|  |  |  |   msg.rtm.rtm_msglen = l; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   if ((l = write(p->sys.sk->fd, (char *)&msg, l)) < 0) { | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |     log(L_ERR "KRT: Error sending route %N to kernel: %m", net->n.addr); | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2018-05-29 12:08:12 +02:00
										 |  |  | krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  |   int err = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if (old) | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     krt_send_route(p, RTM_DELETE, old); | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if (new) | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     err = krt_send_route(p, RTM_ADD, new); | 
					
						
							| 
									
										
										
										
											2011-10-06 22:48:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (err < 0) | 
					
						
							|  |  |  |     n->n.flags |= KRF_SYNC_ERROR; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     n->n.flags &= ~KRF_SYNC_ERROR; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 10:00:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_read_route(struct ks_msg *msg, struct krt_proto *p, int scan) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   /* p is NULL iff KRT_SHARED_SOCKET and !scan */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |   int ipv6; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   rte *e; | 
					
						
							|  |  |  |   net *net; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   sockaddr dst, gate, mask; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   ip_addr idst, igate, imask; | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |   net_addr ndst; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   void *body = (char *)msg->buf; | 
					
						
							| 
									
										
										
										
											2015-04-22 10:53:15 +02:00
										 |  |  |   int new = (msg->rtm.rtm_type != RTM_DELETE); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   char *errmsg = "KRT: Invalid route received"; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   int flags = msg->rtm.rtm_flags; | 
					
						
							|  |  |  |   int addrs = msg->rtm.rtm_addrs; | 
					
						
							| 
									
										
										
										
											2012-03-23 00:26:26 +01:00
										 |  |  |   int src; | 
					
						
							|  |  |  |   byte src2; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (!(flags & RTF_UP) && scan) | 
					
						
							|  |  |  |     SKIP("not up in scan\n"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (!(flags & RTF_DONE) && !scan) | 
					
						
							|  |  |  |     SKIP("not done in async\n"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (flags & RTF_LLINFO) | 
					
						
							|  |  |  |     SKIP("link-local\n"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   GETADDR(&dst, RTA_DST); | 
					
						
							|  |  |  |   GETADDR(&gate, RTA_GATEWAY); | 
					
						
							|  |  |  |   GETADDR(&mask, RTA_NETMASK); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |   switch (dst.sa.sa_family) { | 
					
						
							|  |  |  |     case AF_INET: | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |       ipv6 = 0; | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |     case AF_INET6: | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |       ipv6 = 1; | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |     default: | 
					
						
							|  |  |  |       SKIP("invalid DST"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |   /* We do not test family for RTA_NETMASK, because BSD sends us
 | 
					
						
							|  |  |  |      some strange values, but interpreting them as IPv4/IPv6 works */ | 
					
						
							|  |  |  |   mask.sa.sa_family = dst.sa.sa_family; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 11:42:26 +02:00
										 |  |  |   idst  = ipa_from_sa(&dst); | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   imask = ipa_from_sa(&mask); | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |   igate = (gate.sa.sa_family == dst.sa.sa_family) ? ipa_from_sa(&gate) : IPA_NONE; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  | #ifdef KRT_SHARED_SOCKET
 | 
					
						
							|  |  |  |   if (!scan) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     int table_id = msg->rtm.rtm_tableid; | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |     p = (table_id < KRT_MAX_TABLES) ? krt_table_map[table_id][ipv6] : NULL; | 
					
						
							| 
									
										
										
										
											2014-05-18 11:42:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |     if (!p) | 
					
						
							|  |  |  |       SKIP("unknown table id %d\n", table_id); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-02-01 09:22:18 +01:00
										 |  |  |   if ((!ipv6) && (p->p.main_channel->table->addr_type != NET_IP4)) | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |     SKIP("reading only IPv4 routes"); | 
					
						
							| 
									
										
										
										
											2016-02-01 09:22:18 +01:00
										 |  |  |   if (  ipv6  && (p->p.main_channel->table->addr_type != NET_IP6)) | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |     SKIP("reading only IPv6 routes"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   int c = ipa_classify_net(idst); | 
					
						
							|  |  |  |   if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK)) | 
					
						
							|  |  |  |     SKIP("strange class/scope\n"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   int pxlen; | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |   if (ipv6) | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |     pxlen = (flags & RTF_HOST) ? IP6_MAX_PREFIX_LENGTH : ip6_masklen(&ipa_to_ip6(imask)); | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |   else | 
					
						
							|  |  |  |     pxlen = (flags & RTF_HOST) ? IP4_MAX_PREFIX_LENGTH : ip4_masklen(ipa_to_ip4(imask)); | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (pxlen < 0) | 
					
						
							|  |  |  |     { log(L_ERR "%s (%I) - netmask %I", errmsg, idst, imask); return; } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 10:59:52 +01:00
										 |  |  |   if (ipv6) | 
					
						
							|  |  |  |     net_fill_ip6(&ndst, ipa_to_ip6(idst), pxlen); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     net_fill_ip4(&ndst, ipa_to_ip4(idst), pxlen); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if ((flags & RTF_GATEWAY) && ipa_zero(igate)) | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |     { log(L_ERR "%s (%N) - missing gateway", errmsg, ndst); return; } | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   u32 self_mask = RTF_PROTO1; | 
					
						
							| 
									
										
										
										
											2010-04-08 18:41:17 +02:00
										 |  |  |   u32 alien_mask = RTF_STATIC | RTF_PROTO1 | RTF_GATEWAY; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 00:26:26 +01:00
										 |  |  |   src2 = (flags & RTF_STATIC) ? 1 : 0; | 
					
						
							|  |  |  |   src2 |= (flags & RTF_PROTO1) ? 2 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #ifdef RTF_PROTO2
 | 
					
						
							|  |  |  |   alien_mask |= RTF_PROTO2; | 
					
						
							| 
									
										
										
										
											2012-03-23 00:26:26 +01:00
										 |  |  |   src2 |= (flags & RTF_PROTO2) ? 4 : 0; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #ifdef RTF_PROTO3
 | 
					
						
							|  |  |  |   alien_mask |= RTF_PROTO3; | 
					
						
							| 
									
										
										
										
											2012-03-23 00:26:26 +01:00
										 |  |  |   src2 |= (flags & RTF_PROTO3) ? 8 : 0; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-08 18:41:17 +02:00
										 |  |  | #ifdef RTF_REJECT
 | 
					
						
							|  |  |  |   alien_mask |= RTF_REJECT; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef RTF_BLACKHOLE
 | 
					
						
							|  |  |  |   alien_mask |= RTF_BLACKHOLE; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (flags & (RTF_DYNAMIC | RTF_MODIFIED)) | 
					
						
							|  |  |  |     src = KRT_SRC_REDIRECT; | 
					
						
							|  |  |  |   else if (flags & self_mask) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (!scan) | 
					
						
							|  |  |  | 	SKIP("echo\n"); | 
					
						
							|  |  |  |       src = KRT_SRC_BIRD; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (flags & alien_mask) | 
					
						
							|  |  |  |     src = KRT_SRC_ALIEN; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     src = KRT_SRC_KERNEL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 09:22:18 +01:00
										 |  |  |   net = net_get(p->p.main_channel->table, &ndst); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-05 17:50:19 +02:00
										 |  |  |   rta a = { | 
					
						
							| 
									
										
										
										
											2012-08-14 16:25:22 +02:00
										 |  |  |     .src = p->p.main_source, | 
					
						
							| 
									
										
										
										
											2010-07-05 17:50:19 +02:00
										 |  |  |     .source = RTS_INHERIT, | 
					
						
							|  |  |  |     .scope = SCOPE_UNIVERSE, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   /* reject/blackhole routes have also set RTF_GATEWAY,
 | 
					
						
							|  |  |  |      we wil check them first. */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef RTF_REJECT
 | 
					
						
							|  |  |  |   if(flags & RTF_REJECT) { | 
					
						
							|  |  |  |     a.dest = RTD_UNREACHABLE; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     goto done; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef RTF_BLACKHOLE
 | 
					
						
							|  |  |  |   if(flags & RTF_BLACKHOLE) { | 
					
						
							|  |  |  |     a.dest = RTD_BLACKHOLE; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     goto done; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |   a.nh.iface = if_find_by_index(msg->rtm.rtm_index); | 
					
						
							|  |  |  |   if (!a.nh.iface) | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |       log(L_ERR "KRT: Received route %N with unknown ifindex %u", | 
					
						
							|  |  |  | 	  net->n.addr, msg->rtm.rtm_index); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |   a.dest = RTD_UNICAST; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   if (flags & RTF_GATEWAY) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     neighbor *ng; | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |     a.nh.gw = igate; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-03 11:42:18 +02:00
										 |  |  |     /* Clean up embedded interface ID returned in link-local address */ | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |     if (ipa_is_link_local(a.nh.gw)) | 
					
						
							|  |  |  |       _I0(a.nh.gw) = 0xfe800000; | 
					
						
							| 
									
										
										
										
											2010-04-03 11:42:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-27 17:08:59 +02:00
										 |  |  |     ng = neigh_find(&p->p, a.nh.gw, a.nh.iface, 0); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |     if (!ng || (ng->scope == SCOPE_HOST)) | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2010-04-09 00:56:47 +02:00
										 |  |  | 	/* Ignore routes with next-hop 127.0.0.1, host routes with such
 | 
					
						
							|  |  |  | 	   next-hop appear on OpenBSD for address aliases. */ | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  |         if (ipa_classify(a.nh.gw) == (IADDR_HOST | SCOPE_HOST)) | 
					
						
							| 
									
										
										
										
											2010-04-09 00:56:47 +02:00
										 |  |  |           return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  | 	log(L_ERR "KRT: Received route %N with strange next-hop %I", | 
					
						
							| 
									
										
										
										
											2016-05-06 15:48:35 +02:00
										 |  |  | 	    net->n.addr, a.nh.gw); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | 	return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |  done: | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   e = rte_get_temp(&a); | 
					
						
							|  |  |  |   e->net = net; | 
					
						
							|  |  |  |   e->u.krt.src = src; | 
					
						
							| 
									
										
										
										
											2012-03-23 00:26:26 +01:00
										 |  |  |   e->u.krt.proto = src2; | 
					
						
							| 
									
										
										
										
											2016-03-23 18:25:15 +01:00
										 |  |  |   e->u.krt.seen = 0; | 
					
						
							|  |  |  |   e->u.krt.best = 0; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   e->u.krt.metric = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (scan) | 
					
						
							|  |  |  |     krt_got_route(p, e); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     krt_got_route_async(p, e, new); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 03:15:12 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | krt_read_ifannounce(struct ks_msg *msg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   struct if_announcemsghdr *ifam = (struct if_announcemsghdr *)&msg->rtm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ifam->ifan_what == IFAN_ARRIVAL) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Not enough info to create the iface, so we just trigger iface scan */ | 
					
						
							|  |  |  |     kif_request_scan(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (ifam->ifan_what == IFAN_DEPARTURE) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     struct iface *iface = if_find_by_index(ifam->ifan_index); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Interface is destroyed */ | 
					
						
							|  |  |  |     if (!iface) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       DBG("KRT: unknown interface (%s, #%d) going down. Ignoring\n", ifam->ifan_name, ifam->ifan_index); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if_delete(iface); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DBG("KRT: IFANNOUNCE what: %d index %d name %s\n", ifam->ifan_what, ifam->ifan_index, ifam->ifan_name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 10:00:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  | krt_read_ifinfo(struct ks_msg *msg, int scan) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   struct if_msghdr *ifm = (struct if_msghdr *)&msg->rtm; | 
					
						
							|  |  |  |   void *body = (void *)(ifm + 1); | 
					
						
							|  |  |  |   struct sockaddr_dl *dl = NULL; | 
					
						
							| 
									
										
										
										
											2015-05-19 08:53:34 +02:00
										 |  |  |   uint i; | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   struct iface *iface = NULL, f = {}; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   int fl = ifm->ifm_flags; | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   int nlen = 0; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-08 11:31:12 +01:00
										 |  |  |   for (i = 1; i<=RTA_IFP; i <<= 1) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2011-01-08 11:31:12 +01:00
										 |  |  |     if (i & ifm->ifm_addrs) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-01-08 11:31:12 +01:00
										 |  |  |       if (i == RTA_IFP) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       { | 
					
						
							|  |  |  |         dl = (struct sockaddr_dl *)body; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2011-01-08 11:31:12 +01:00
										 |  |  |       body += ROUNDUP(((struct sockaddr *)&(body))->sa_len); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   if (dl && (dl->sdl_family != AF_LINK)) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2012-01-23 03:15:12 +01:00
										 |  |  |     log(L_WARN "Ignoring strange IFINFO"); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   if (dl) | 
					
						
							|  |  |  |     nlen = MIN(sizeof(f.name)-1, dl->sdl_nlen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Note that asynchronous IFINFO messages do not contain iface
 | 
					
						
							|  |  |  |      name, so we have to found an existing iface by iface index */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   iface = if_find_by_index(ifm->ifm_index); | 
					
						
							|  |  |  |   if (!iface) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							|  |  |  |     /* New interface */ | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |     if (!dl) | 
					
						
							|  |  |  |       return;	/* No interface name, ignoring */ | 
					
						
							| 
									
										
										
										
											2011-01-08 11:22:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |     memcpy(f.name, dl->sdl_data, nlen); | 
					
						
							|  |  |  |     DBG("New interface '%s' found\n", f.name); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (dl && memcmp(iface->name, dl->sdl_data, nlen)) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* Interface renamed */ | 
					
						
							|  |  |  |     if_delete(iface); | 
					
						
							|  |  |  |     memcpy(f.name, dl->sdl_data, nlen); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |     /* Old interface */ | 
					
						
							|  |  |  |     memcpy(f.name, iface->name, sizeof(f.name)); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-23 01:26:40 +01:00
										 |  |  |   f.index = ifm->ifm_index; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   f.mtu = ifm->ifm_data.ifi_mtu; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (fl & IFF_UP) | 
					
						
							| 
									
										
										
										
											2010-11-11 10:03:02 +01:00
										 |  |  |     f.flags |= IF_ADMIN_UP; | 
					
						
							|  |  |  |   if (ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN) | 
					
						
							|  |  |  |     f.flags |= IF_LINK_UP;          /* up or unknown */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if (fl & IFF_LOOPBACK)            /* Loopback */ | 
					
						
							|  |  |  |     f.flags |= IF_MULTIACCESS | IF_LOOPBACK | IF_IGNORE; | 
					
						
							|  |  |  |   else if (fl & IFF_POINTOPOINT)    /* PtP */ | 
					
						
							|  |  |  |     f.flags |= IF_MULTICAST; | 
					
						
							|  |  |  |   else if (fl & IFF_BROADCAST)      /* Broadcast */ | 
					
						
							|  |  |  |     f.flags |= IF_MULTIACCESS | IF_BROADCAST | IF_MULTICAST; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     f.flags |= IF_MULTIACCESS;      /* NBMA */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  |   iface = if_update(&f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!scan) | 
					
						
							|  |  |  |     if_end_partial_update(iface); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 10:00:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  | krt_read_addr(struct ks_msg *msg, int scan) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   struct ifa_msghdr *ifam = (struct ifa_msghdr *)&msg->rtm; | 
					
						
							|  |  |  |   void *body = (void *)(ifam + 1); | 
					
						
							|  |  |  |   sockaddr addr, mask, brd; | 
					
						
							|  |  |  |   struct iface *iface = NULL; | 
					
						
							|  |  |  |   struct ifa ifa; | 
					
						
							|  |  |  |   struct sockaddr null; | 
					
						
							|  |  |  |   ip_addr iaddr, imask, ibrd; | 
					
						
							|  |  |  |   int addrs = ifam->ifam_addrs; | 
					
						
							|  |  |  |   int scope, masklen = -1; | 
					
						
							|  |  |  |   int new = (ifam->ifam_type == RTM_NEWADDR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  |   /* Strange messages with zero (invalid) ifindex appear on OpenBSD */ | 
					
						
							|  |  |  |   if (ifam->ifam_index == 0) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if(!(iface = if_find_by_index(ifam->ifam_index))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     log(L_ERR "KIF: Received address message for unknown interface %d", ifam->ifam_index); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GETADDR (&null, RTA_DST); | 
					
						
							|  |  |  |   GETADDR (&null, RTA_GATEWAY); | 
					
						
							|  |  |  |   GETADDR (&mask, RTA_NETMASK); | 
					
						
							|  |  |  |   GETADDR (&null, RTA_GENMASK); | 
					
						
							|  |  |  |   GETADDR (&null, RTA_IFP); | 
					
						
							|  |  |  |   GETADDR (&addr, RTA_IFA); | 
					
						
							|  |  |  |   GETADDR (&null, RTA_AUTHOR); | 
					
						
							|  |  |  |   GETADDR (&brd, RTA_BRD); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |   /* Is addr family IP4 or IP6? */ | 
					
						
							|  |  |  |   int ipv6; | 
					
						
							|  |  |  |   switch (addr.sa.sa_family) { | 
					
						
							|  |  |  |     case AF_INET: ipv6 = 0; break; | 
					
						
							|  |  |  |     case AF_INET6: ipv6 = 1; break; | 
					
						
							|  |  |  |     default: return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 18:37:51 +02:00
										 |  |  |   /* We do not test family for RTA_NETMASK, because BSD sends us
 | 
					
						
							|  |  |  |      some strange values, but interpreting them as IPv4/IPv6 works */ | 
					
						
							|  |  |  |   mask.sa.sa_family = addr.sa.sa_family; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-18 11:42:26 +02:00
										 |  |  |   iaddr = ipa_from_sa(&addr); | 
					
						
							|  |  |  |   imask = ipa_from_sa(&mask); | 
					
						
							|  |  |  |   ibrd  = ipa_from_sa(&brd); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   if ((ipv6 ? (masklen = ip6_masklen(&ipa_to_ip6(imask))) : (masklen = ip4_masklen(ipa_to_ip4(imask)))) < 0) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |     log(L_ERR "KIF: Invalid mask %I for %s", imask, iface->name); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |   /* Clean up embedded interface ID returned in link-local address */ | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 10:47:17 +02:00
										 |  |  |   if (ipa_is_link_local(iaddr)) | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |     _I0(iaddr) = 0xfe800000; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 10:47:17 +02:00
										 |  |  |   if (ipa_is_link_local(ibrd)) | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |     _I0(ibrd) = 0xfe800000; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |   bzero(&ifa, sizeof(ifa)); | 
					
						
							|  |  |  |   ifa.iface = iface; | 
					
						
							|  |  |  |   ifa.ip = iaddr; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   scope = ipa_classify(ifa.ip); | 
					
						
							|  |  |  |   if (scope < 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, iface->name); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   ifa.scope = scope & IADDR_SCOPE_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |   if (masklen < (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH)) | 
					
						
							| 
									
										
										
										
											2010-04-28 00:39:57 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |     net_fill_ipa(&ifa.prefix, ifa.ip, masklen); | 
					
						
							|  |  |  |     net_normalize(&ifa.prefix); | 
					
						
							| 
									
										
										
										
											2010-04-28 00:39:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |     if (masklen == ((ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH) - 1)) | 
					
						
							| 
									
										
										
										
											2010-04-28 00:39:57 +02:00
										 |  |  |       ifa.opposite = ipa_opposite_m1(ifa.ip); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |     if ((!ipv6) && (masklen == IP4_MAX_PREFIX_LENGTH - 2)) | 
					
						
							| 
									
										
										
										
											2010-04-28 00:39:57 +02:00
										 |  |  |       ifa.opposite = ipa_opposite_m2(ifa.ip); | 
					
						
							| 
									
										
										
										
											2013-09-22 19:15:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |     if (iface->flags & IF_BROADCAST) | 
					
						
							|  |  |  |       ifa.brd = ibrd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 19:15:39 +02:00
										 |  |  |     if (!(iface->flags & IF_MULTIACCESS)) | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |       ifa.opposite = ibrd; | 
					
						
							| 
									
										
										
										
											2010-04-28 00:39:57 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-11-21 13:17:42 +01:00
										 |  |  |   else if (!(iface->flags & IF_MULTIACCESS) && ipa_nonzero(ibrd)) | 
					
						
							| 
									
										
										
										
											2010-03-10 01:04:09 +01:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |     net_fill_ipa(&ifa.prefix, ibrd, (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH)); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:24:18 +01:00
										 |  |  |     ifa.opposite = ibrd; | 
					
						
							| 
									
										
										
										
											2011-03-28 22:46:18 +02:00
										 |  |  |     ifa.flags |= IA_PEER; | 
					
						
							| 
									
										
										
										
											2013-09-22 19:15:39 +02:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2015-12-11 15:35:37 +01:00
										 |  |  |     net_fill_ipa(&ifa.prefix, ifa.ip, (ipv6 ? IP6_MAX_PREFIX_LENGTH : IP4_MAX_PREFIX_LENGTH)); | 
					
						
							| 
									
										
										
										
											2013-09-22 19:15:39 +02:00
										 |  |  |     ifa.flags |= IA_HOST; | 
					
						
							| 
									
										
										
										
											2010-03-10 01:04:09 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if (new) | 
					
						
							|  |  |  |     ifa_update(&ifa); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     ifa_delete(&ifa); | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!scan) | 
					
						
							|  |  |  |     if_end_partial_update(iface); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | krt_read_msg(struct proto *p, struct ks_msg *msg, int scan) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   /* p is NULL iff KRT_SHARED_SOCKET and !scan */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   switch (msg->rtm.rtm_type) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     case RTM_GET: | 
					
						
							|  |  |  |       if(!scan) return; | 
					
						
							|  |  |  |     case RTM_ADD: | 
					
						
							|  |  |  |     case RTM_DELETE: | 
					
						
							| 
									
										
										
										
											2015-04-22 10:53:15 +02:00
										 |  |  |     case RTM_CHANGE: | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |       krt_read_route(msg, (struct krt_proto *)p, scan); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       break; | 
					
						
							| 
									
										
										
										
											2012-01-23 03:15:12 +01:00
										 |  |  |     case RTM_IFANNOUNCE: | 
					
						
							|  |  |  |       krt_read_ifannounce(msg); | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |     case RTM_IFINFO: | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  |       krt_read_ifinfo(msg, scan); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     case RTM_NEWADDR: | 
					
						
							|  |  |  |     case RTM_DELADDR: | 
					
						
							| 
									
										
										
										
											2014-02-26 12:52:00 +01:00
										 |  |  |       krt_read_addr(msg, scan); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |       break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Sysctl based scans */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static byte *krt_buffer; | 
					
						
							|  |  |  | static size_t krt_buflen, krt_bufmin; | 
					
						
							|  |  |  | static struct proto *krt_buffer_owner; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static byte * | 
					
						
							|  |  |  | krt_buffer_update(struct proto *p, size_t *needed) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   size_t req = *needed; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((req > krt_buflen) || | 
					
						
							|  |  |  |       ((p == krt_buffer_owner) && (req < krt_bufmin))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     /* min buflen is 32 kB, step is 8 kB, or 128 kB if > 1 MB */ | 
					
						
							|  |  |  |     size_t step = (req < 0x100000) ? 0x2000 : 0x20000; | 
					
						
							|  |  |  |     krt_buflen = (req < 0x6000) ? 0x8000 : (req + step); | 
					
						
							|  |  |  |     krt_bufmin = (req < 0x8000) ? 0 : (req - 2*step); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (krt_buffer)  | 
					
						
							|  |  |  |       mb_free(krt_buffer); | 
					
						
							|  |  |  |     krt_buffer = mb_alloc(krt_pool, krt_buflen); | 
					
						
							|  |  |  |     krt_buffer_owner = p; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *needed = krt_buflen; | 
					
						
							|  |  |  |   return krt_buffer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 10:00:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_buffer_release(struct proto *p) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   if (p == krt_buffer_owner) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     mb_free(krt_buffer); | 
					
						
							|  |  |  |     krt_buffer = NULL; | 
					
						
							|  |  |  |     krt_buflen = 0; | 
					
						
							|  |  |  |     krt_buffer_owner = 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-07 10:00:29 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_sysctl_scan(struct proto *p, int cmd, int table_id) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   byte *buf, *next; | 
					
						
							|  |  |  |   int mib[7], mcnt; | 
					
						
							|  |  |  |   size_t needed; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   struct ks_msg *m; | 
					
						
							| 
									
										
										
										
											2011-04-07 11:31:56 +02:00
										 |  |  |   int retries = 3; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   int rv; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   mib[0] = CTL_NET; | 
					
						
							|  |  |  |   mib[1] = PF_ROUTE; | 
					
						
							|  |  |  |   mib[2] = 0; | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   mib[3] = 0; // Set AF to 0 for all available families
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   mib[4] = cmd; | 
					
						
							|  |  |  |   mib[5] = 0; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   mcnt = 6; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #ifdef KRT_USE_SYSCTL_7
 | 
					
						
							|  |  |  |   if (table_id >= 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     mib[6] = table_id; | 
					
						
							|  |  |  |     mcnt = 7; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #ifdef KRT_USE_SETFIB_SCAN
 | 
					
						
							|  |  |  |   if (table_id > 0) | 
					
						
							|  |  |  |     if (setfib(table_id) < 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       log(L_ERR "KRT: setfib(%d) failed: %m", table_id); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |  try: | 
					
						
							|  |  |  |   rv = sysctl(mib, mcnt, NULL, &needed, NULL, 0); | 
					
						
							|  |  |  |   if (rv < 0) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     /* OpenBSD returns EINVAL for not yet used tables */ | 
					
						
							|  |  |  |     if ((errno == EINVAL) && (table_id > 0)) | 
					
						
							|  |  |  |       goto exit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log(L_ERR "KRT: Route scan estimate failed: %m"); | 
					
						
							|  |  |  |     goto exit; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   /* The table is empty */ | 
					
						
							|  |  |  |   if (needed == 0) | 
					
						
							|  |  |  |     goto exit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   buf = krt_buffer_update(p, &needed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   rv = sysctl(mib, mcnt, buf, &needed, NULL, 0); | 
					
						
							|  |  |  |   if (rv < 0) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     /* The buffer size changed since last sysctl ('needed' is not changed) */ | 
					
						
							|  |  |  |     if ((errno == ENOMEM) && retries--) | 
					
						
							|  |  |  |       goto try; | 
					
						
							| 
									
										
										
										
											2011-04-07 11:31:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     log(L_ERR "KRT: Route scan failed: %m"); | 
					
						
							|  |  |  |     goto exit; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #ifdef KRT_USE_SETFIB_SCAN
 | 
					
						
							|  |  |  |   if (table_id > 0) | 
					
						
							|  |  |  |     if (setfib(0) < 0) | 
					
						
							|  |  |  |       die("KRT: setfib(%d) failed: %m", 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Process received messages */ | 
					
						
							|  |  |  |   for (next = buf; next < (buf + needed); next += m->rtm.rtm_msglen) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   { | 
					
						
							|  |  |  |     m = (struct ks_msg *)next; | 
					
						
							|  |  |  |     krt_read_msg(p, m, 1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  exit: | 
					
						
							|  |  |  |   krt_buffer_release(p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_USE_SETFIB_SCAN
 | 
					
						
							|  |  |  |   if (table_id > 0) | 
					
						
							|  |  |  |     if (setfib(0) < 0) | 
					
						
							|  |  |  |       die("KRT: setfib(%d) failed: %m", 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | krt_do_scan(struct krt_proto *p) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_sysctl_scan(&p->p, NET_RT_DUMP, KRT_CF->sys.table_id); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | kif_do_scan(struct kif_proto *p) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   if_start_update(); | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_sysctl_scan(&p->p, NET_RT_IFLIST, -1); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  |   if_end_update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Kernel sockets */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2016-10-14 15:37:04 +02:00
										 |  |  | krt_sock_hook(sock *sk, uint size UNUSED) | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   struct ks_msg msg; | 
					
						
							|  |  |  |   int l = read(sk->fd, (char *)&msg, sizeof(msg)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   if (l <= 0) | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  |     log(L_ERR "krt-sock: read failed"); | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |     krt_read_msg((struct proto *) sk->data, &msg, 0); | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 12:00:53 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | krt_sock_err_hook(sock *sk, int e UNUSED) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   krt_sock_hook(sk, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | static sock * | 
					
						
							| 
									
										
										
										
											2016-10-14 15:37:04 +02:00
										 |  |  | krt_sock_open(pool *pool, void *data, int table_id UNUSED) | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   sock *sk; | 
					
						
							|  |  |  |   int fd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fd = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); | 
					
						
							|  |  |  |   if (fd < 0) | 
					
						
							|  |  |  |     die("Cannot open kernel socket for routes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_USE_SETFIB_SOCK
 | 
					
						
							|  |  |  |   if (table_id > 0) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (setsockopt(fd, SOL_SOCKET, SO_SETFIB, &table_id, sizeof(table_id)) < 0) | 
					
						
							|  |  |  |       die("Cannot set FIB %d for kernel socket: %m", table_id); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sk = sk_new(pool); | 
					
						
							|  |  |  |   sk->type = SK_MAGIC; | 
					
						
							|  |  |  |   sk->rx_hook = krt_sock_hook; | 
					
						
							| 
									
										
										
										
											2016-09-29 12:00:53 +02:00
										 |  |  |   sk->err_hook = krt_sock_err_hook; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   sk->fd = fd; | 
					
						
							|  |  |  |   sk->data = data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (sk_open(sk) < 0) | 
					
						
							|  |  |  |     bug("krt-sock: sk_open failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return sk; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  | static u32 krt_table_cf[(KRT_MAX_TABLES+31) / 32][2]; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef KRT_SHARED_SOCKET
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static sock *krt_sock; | 
					
						
							|  |  |  | static int krt_sock_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | krt_sock_open_shared(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!krt_sock_count) | 
					
						
							|  |  |  |     krt_sock = krt_sock_open(krt_pool, NULL, -1); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   krt_sock_count++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | krt_sock_close_shared(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   krt_sock_count--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!krt_sock_count) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     rfree(krt_sock); | 
					
						
							|  |  |  |     krt_sock = NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 14:42:41 +01:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_sys_start(struct krt_proto *p) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   int id = KRT_CF->sys.table_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (krt_table_cf[id/32][!!(p->af == AF_INET6)] & (1 << (id%32))) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       log(L_ERR "%s: Multiple kernel syncers defined for table #%d", p->p.name, id); | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   krt_table_cf[id/32][!!(p->af == AF_INET6)] |= (1 << (id%32)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   krt_table_map[KRT_CF->sys.table_id][!!(p->af == AF_INET6)] = p; | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_sock_open_shared(); | 
					
						
							|  |  |  |   p->sys.sk = krt_sock; | 
					
						
							| 
									
										
										
										
											2015-11-03 14:42:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return 1; | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | void | 
					
						
							|  |  |  | krt_sys_shutdown(struct krt_proto *p) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   krt_table_cf[(KRT_CF->sys.table_id)/32][!!(p->af == AF_INET6)] &= ~(1 << ((KRT_CF->sys.table_id)%32)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_sock_close_shared(); | 
					
						
							|  |  |  |   p->sys.sk = NULL; | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   krt_table_map[KRT_CF->sys.table_id][!!(p->af == AF_INET6)] = NULL; | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_buffer_release(&p->p); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-04-30 15:31:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 14:42:41 +01:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_sys_start(struct krt_proto *p) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   int id = KRT_CF->sys.table_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (krt_table_cf[id/32][!!(p->af == AF_INET6)] & (1 << (id%32))) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       log(L_ERR "%s: Multiple kernel syncers defined for table #%d", p->p.name, id); | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   krt_table_cf[id/32][!!(p->af == AF_INET6)] |= (1 << (id%32)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   p->sys.sk = krt_sock_open(p->p.pool, p, KRT_CF->sys.table_id); | 
					
						
							| 
									
										
										
										
											2015-11-03 14:42:41 +01:00
										 |  |  |   return 1; | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_sys_shutdown(struct krt_proto *p) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-07 14:45:01 +01:00
										 |  |  |   krt_table_cf[(KRT_CF->sys.table_id)/32][!!(p->af == AF_INET6)] &= ~(1 << ((KRT_CF->sys.table_id)%32)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   rfree(p->sys.sk); | 
					
						
							|  |  |  |   p->sys.sk = NULL; | 
					
						
							| 
									
										
										
										
											2011-08-15 02:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_buffer_release(&p->p); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | #endif /* KRT_SHARED_SOCKET */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-15 02:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | /* KRT configuration callbacks */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return n->sys.table_id == o->sys.table_id; | 
					
						
							| 
									
										
										
										
											2010-02-26 10:55:58 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | krt_sys_preconfig(struct config *c UNUSED) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  |   krt_max_tables = krt_get_max_tables(); | 
					
						
							|  |  |  |   bzero(&krt_table_cf, sizeof(krt_table_cf)); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | void krt_sys_init_config(struct krt_config *c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   c->sys.table_id = 0; /* Default table */ | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-08-15 02:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | void krt_sys_copy_config(struct krt_config *d, struct krt_config *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   d->sys.table_id = s->sys.table_id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* KIF misc code */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | kif_sys_start(struct kif_proto *p UNUSED) | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-08-15 02:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 12:11:42 +02:00
										 |  |  | void | 
					
						
							|  |  |  | kif_sys_shutdown(struct kif_proto *p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   krt_buffer_release(&p->p); | 
					
						
							| 
									
										
										
										
											2004-05-31 13:25:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 13:06:01 +01:00
										 |  |  | int | 
					
						
							|  |  |  | kif_update_sysdep_addr(struct iface *i) | 
					
						
							| 
									
										
										
										
											2013-11-25 01:21:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  |   static int fd = -1; | 
					
						
							| 
									
										
										
										
											2017-12-07 13:06:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-25 01:21:39 +01:00
										 |  |  |   if (fd < 0) | 
					
						
							|  |  |  |     fd = socket(AF_INET, SOCK_DGRAM, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   struct ifreq ifr; | 
					
						
							|  |  |  |   memset(&ifr, 0, sizeof(ifr)); | 
					
						
							|  |  |  |   strncpy(ifr.ifr_name, i->name, IFNAMSIZ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int rv = ioctl(fd, SIOCGIFADDR, (char *) &ifr); | 
					
						
							|  |  |  |   if (rv < 0) | 
					
						
							| 
									
										
										
										
											2017-12-07 13:06:01 +01:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2013-11-25 01:21:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 13:06:01 +01:00
										 |  |  |   ip4_addr old = i->sysdep; | 
					
						
							| 
									
										
										
										
											2017-12-07 13:44:00 +01:00
										 |  |  |   i->sysdep = ipa_to_ip4(ipa_from_sa4(&ifr.ifr_addr)); | 
					
						
							| 
									
										
										
										
											2013-11-25 01:21:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-07 13:44:00 +01:00
										 |  |  |   return !ip4_equal(i->sysdep, old); | 
					
						
							| 
									
										
										
										
											2013-11-25 01:21:39 +01:00
										 |  |  | } |