From 182a78957d60a4c91c1ff8d1ff0f09b1b64b70ba Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Sun, 29 Apr 2012 01:35:52 +0200 Subject: [PATCH 1/8] Allows some modifications of dest attribute in filters. --- doc/bird.sgml | 11 ++++++++++- filter/config.Y | 2 +- filter/filter.c | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/doc/bird.sgml b/doc/bird.sgml index a94fb9e1..3edd6e0e 100644 --- a/doc/bird.sgml +++ b/doc/bird.sgml @@ -1072,7 +1072,16 @@ undefined value is regarded as empty clist for most purposes. routes). Read-only. - Type of destination the packets should be sent to ( The optional attribute that can be used to specify a distance diff --git a/filter/config.Y b/filter/config.Y index 2e8b522e..0eeb2ce1 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -703,7 +703,7 @@ static_attr: | SOURCE { $$ = f_new_inst(); $$->aux = T_ENUM_RTS; $$->a2.i = OFFSETOF(struct rta, source); } | SCOPE { $$ = f_new_inst(); $$->aux = T_ENUM_SCOPE; $$->a2.i = OFFSETOF(struct rta, scope); $$->a1.i = 1; } | CAST { $$ = f_new_inst(); $$->aux = T_ENUM_RTC; $$->a2.i = OFFSETOF(struct rta, cast); } - | DEST { $$ = f_new_inst(); $$->aux = T_ENUM_RTD; $$->a2.i = OFFSETOF(struct rta, dest); } + | DEST { $$ = f_new_inst(); $$->aux = T_ENUM_RTD; $$->a2.i = OFFSETOF(struct rta, dest); $$->a1.i = 1; } ; term: diff --git a/filter/filter.c b/filter/filter.c index acdcfd2b..49b67391 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -852,12 +852,25 @@ interpret(struct f_inst *what) { struct rta *rta = (*f_rte)->attrs; switch (what->aux) { - case T_ENUM: - * ((char *) rta + what->a2.i) = v1.val.i; - break; + case T_IP: * (ip_addr *) ((char *) rta + what->a2.i) = v1.val.px.ip; break; + + case T_ENUM_SCOPE: + rta->scope = v1.val.i; + break; + + case T_ENUM_RTD: + i = v1.val.i; + if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT)) + runtime( "Destination can be changed only to blackhole, unreachable or prohibit" ); + rta->dest = i; + rta->gw = IPA_NONE; + rta->iface = NULL; + rta->nexthops = NULL; + break; + default: bug( "Unknown type in set of static attribute" ); } From 396dfa9042305f62da1f56589c4b98fac57fc2f6 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Mon, 30 Apr 2012 15:31:32 +0200 Subject: [PATCH 2/8] Cleanup in sysdep KRT code, part 1. OS-dependent functions renamed to be more consistent, prepared to merge krt-set and krt-scan headers. Name changes: struct krt_if_params -> struct kif_params struct krt_if_status -> struct kif_status struct krt_set/scan_params -> struct krt_params struct krt_set/scan_status -> struct krt_status krt_if_params_same -> kif_sys_reconfigure krt_if_copy_params -> kif_sys_copy_config krt_set/scan_params_same -> krt_sys_reconfigure krt_set/scan_copy_params -> krt_sys_copy_config krt_if_scan -> kif_do_scan krt_set_notify -> krt_do_notify krt_scan_fire -> krt_do_scan krt_if_ -> kif_sys_ krt_scan_ -> krt_sys_ krt_set_ -> krt_sys_ --- sysdep/bsd/krt-iface.h | 16 +- sysdep/bsd/krt-scan.h | 14 +- sysdep/bsd/krt-sock.c | 143 ++++++-------- sysdep/bsd/krt-sock.h | 23 +-- sysdep/linux/netlink/krt-iface.h | 18 +- sysdep/linux/netlink/krt-scan.h | 12 +- sysdep/linux/netlink/netlink.Y | 2 +- sysdep/linux/netlink/netlink.c | 85 +++++---- sysdep/unix/krt-iface.c | 13 +- sysdep/unix/krt-iface.h | 14 +- sysdep/unix/krt-set.c | 15 +- sysdep/unix/krt-set.h | 16 +- sysdep/unix/krt.Y | 21 +- sysdep/unix/krt.c | 316 +++++++++++++++++-------------- sysdep/unix/krt.h | 57 +++--- 15 files changed, 384 insertions(+), 381 deletions(-) diff --git a/sysdep/bsd/krt-iface.h b/sysdep/bsd/krt-iface.h index 7f0d52bd..278c4be9 100644 --- a/sysdep/bsd/krt-iface.h +++ b/sysdep/bsd/krt-iface.h @@ -13,13 +13,21 @@ * We don't have split iface/scan/set parts. See krt-sock.h. */ -struct krt_if_params { +struct kif_params { }; -struct krt_if_status { +struct kif_status { }; -static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; } -static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } + +static inline void kif_sys_init(struct kif_proto *p UNUSED) { } +static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } + +static inline void kif_sys_preconfig(struct config *c UNUSED) { } +static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } +static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } +static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } + +static inline void kif_sys_io_init(void) { } #endif diff --git a/sysdep/bsd/krt-scan.h b/sysdep/bsd/krt-scan.h index 19cd930d..045b9e77 100644 --- a/sysdep/bsd/krt-scan.h +++ b/sysdep/bsd/krt-scan.h @@ -13,10 +13,18 @@ struct krt_scan_params { }; struct krt_scan_status { - list temp_ifs; /* Temporary interfaces */ }; -static inline int krt_scan_params_same(struct krt_scan_params *o UNUSED, struct krt_scan_params *n UNUSED) { return 1; } -static inline void krt_scan_copy_params(struct krt_scan_params *d UNUSED, struct krt_scan_params *s UNUSED) { } + +static inline void krt_sys_init(struct krt_proto *p UNUSED) { } +static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; } + +static inline void krt_sys_preconfig(struct config *c UNUSED) { } +static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { } +static inline void krt_sys_init_config(struct krt_config *c UNUSED) { } +static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { } + + + #endif diff --git a/sysdep/bsd/krt-sock.c b/sysdep/bsd/krt-sock.c index 9ca36d83..5fb5f9f7 100644 --- a/sysdep/bsd/krt-sock.c +++ b/sysdep/bsd/krt-sock.c @@ -33,7 +33,19 @@ #include "lib/string.h" #include "lib/socket.h" -int rt_sock = 0; + +#ifndef RTAX_MAX +#define RTAX_MAX 8 +#endif + +struct ks_msg +{ + struct rt_msghdr rtm; + struct sockaddr_storage buf[RTAX_MAX]; +}; + + +static int rt_sock = 0; int krt_capable(rte *e) @@ -189,8 +201,8 @@ krt_sock_send(int cmd, rte *e) } void -krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, - struct ea_list *eattrs UNUSED) +krt_do_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, + struct ea_list *eattrs UNUSED) { int err = 0; @@ -206,45 +218,6 @@ krt_set_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, n->n.flags &= ~KRF_SYNC_ERROR; } -static int -krt_set_hook(sock *sk, int size UNUSED) -{ - struct ks_msg msg; - int l = read(sk->fd, (char *)&msg, sizeof(msg)); - - if(l <= 0) - log(L_ERR "krt-sock: read failed"); - else - krt_read_msg((struct proto *)sk->data, &msg, 0); - - return 0; -} - -void -krt_set_start(struct krt_proto *x, int first UNUSED) -{ - sock *sk_rt; - static int ks_open_tried = 0; - - if (ks_open_tried) - return; - - ks_open_tried = 1; - - DBG("KRT: Opening kernel socket\n"); - - if( (rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) - die("Cannot open kernel socket for routes"); - - sk_rt = sk_new(krt_pool); - sk_rt->type = SK_MAGIC; - sk_rt->rx_hook = krt_set_hook; - sk_rt->fd = rt_sock; - sk_rt->data = x; - if (sk_open(sk_rt)) - bug("krt-sock: sk_open failed"); -} - #define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0) static void @@ -648,32 +621,6 @@ krt_read_msg(struct proto *p, struct ks_msg *msg, int scan) } } -void -krt_scan_construct(struct krt_config *c UNUSED) -{ -} - -void -krt_scan_preconfig(struct config *c UNUSED) -{ -} - -void -krt_scan_postconfig(struct krt_config *c UNUSED) -{ -} - -void -krt_scan_start(struct krt_proto *x, int first UNUSED) -{ - init_list(&x->scan.temp_ifs); -} - -void -krt_scan_shutdown(struct krt_proto *x UNUSED, int last UNUSED) -{ -} - static void krt_sysctl_scan(struct proto *p, pool *pool, byte **buf, size_t *bl, int cmd) { @@ -732,13 +679,13 @@ static size_t krt_buflen = 32768; static size_t kif_buflen = 4096; void -krt_scan_fire(struct krt_proto *p) +krt_do_scan(struct krt_proto *p) { krt_sysctl_scan((struct proto *)p, p->krt_pool, &krt_buffer, &krt_buflen, NET_RT_DUMP); } void -krt_if_scan(struct kif_proto *p) +kif_do_scan(struct kif_proto *p) { struct proto *P = (struct proto *)p; if_start_update(); @@ -746,14 +693,47 @@ krt_if_scan(struct kif_proto *p) if_end_update(); } - -void -krt_set_construct(struct krt_config *c UNUSED) +static int +krt_sock_hook(sock *sk, int size UNUSED) { + struct ks_msg msg; + int l = read(sk->fd, (char *)&msg, sizeof(msg)); + + if(l <= 0) + log(L_ERR "krt-sock: read failed"); + else + krt_read_msg((struct proto *)sk->data, &msg, 0); + + return 0; } void -krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED) +krt_sys_start(struct krt_proto *x, int first UNUSED) +{ + sock *sk_rt; + static int ks_open_tried = 0; + + if (ks_open_tried) + return; + + ks_open_tried = 1; + + DBG("KRT: Opening kernel socket\n"); + + if( (rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) < 0) + die("Cannot open kernel socket for routes"); + + sk_rt = sk_new(krt_pool); + sk_rt->type = SK_MAGIC; + sk_rt->rx_hook = krt_sock_hook; + sk_rt->fd = rt_sock; + sk_rt->data = x; + if (sk_open(sk_rt)) + bug("krt-sock: sk_open failed"); +} + +void +krt_sys_shutdown(struct krt_proto *x UNUSED, int last UNUSED) { if (!krt_buffer) return; @@ -762,23 +742,14 @@ krt_set_shutdown(struct krt_proto *x UNUSED, int last UNUSED) krt_buffer = NULL; } + void -krt_if_io_init(void) +kif_sys_start(struct kif_proto *p UNUSED) { } void -krt_if_construct(struct kif_config *c UNUSED) -{ -} - -void -krt_if_start(struct kif_proto *p UNUSED) -{ -} - -void -krt_if_shutdown(struct kif_proto *p UNUSED) +kif_sys_shutdown(struct kif_proto *p UNUSED) { if (!kif_buffer) return; diff --git a/sysdep/bsd/krt-sock.h b/sysdep/bsd/krt-sock.h index aab639c4..d9a7ca5d 100644 --- a/sysdep/bsd/krt-sock.h +++ b/sysdep/bsd/krt-sock.h @@ -9,29 +9,10 @@ #ifndef _BIRD_KRT_SOCK_H_ #define _BIRD_KRT_SOCK_H_ -#include -#include -#include "lib/socket.h" - -#ifndef RTAX_MAX -#define RTAX_MAX 8 -#endif - - -struct ks_msg -{ - struct rt_msghdr rtm; - struct sockaddr_storage buf[RTAX_MAX]; +struct krt_params { }; - - -extern int krt_set_sock; - -struct krt_set_params { -}; - -struct krt_set_status { +struct krt_status { }; static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; } diff --git a/sysdep/linux/netlink/krt-iface.h b/sysdep/linux/netlink/krt-iface.h index 770c6e2e..8cfe1073 100644 --- a/sysdep/linux/netlink/krt-iface.h +++ b/sysdep/linux/netlink/krt-iface.h @@ -13,17 +13,21 @@ * We don't have split iface/scan/set parts. See krt-scan.h. */ -struct krt_if_params { +struct kif_params { }; -struct krt_if_status { +struct kif_status { }; -static inline void krt_if_construct(struct kif_config *c UNUSED) { }; -static inline void krt_if_shutdown(struct kif_proto *p UNUSED) { }; -static inline void krt_if_io_init(void) { }; -static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; } -static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } +static inline void kif_sys_init(struct kif_proto *p UNUSED) { } +static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } + +static inline void kif_sys_preconfig(struct config *c UNUSED) { } +static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } +static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } +static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } + +static inline void kif_sys_io_init(void) { } #endif diff --git a/sysdep/linux/netlink/krt-scan.h b/sysdep/linux/netlink/krt-scan.h index 9b5e075b..302b6093 100644 --- a/sysdep/linux/netlink/krt-scan.h +++ b/sysdep/linux/netlink/krt-scan.h @@ -17,20 +17,14 @@ #define NL_NUM_TABLES 256 -struct krt_scan_params { +struct krt_params { int table_id; /* Kernel table ID we sync with */ }; -struct krt_scan_status { - list temp_ifs; /* Temporary interfaces */ +struct krt_status { }; -static inline int krt_scan_params_same(struct krt_scan_params *o, struct krt_scan_params *n) -{ - return o->table_id == n->table_id; -} -static inline void krt_scan_copy_params(struct krt_scan_params *d UNUSED, struct krt_scan_params *s UNUSED) { } -/* table_id copied in krt_copy_config() */ +static inline void krt_sys_init(struct krt_proto *p UNUSED) { } #endif diff --git a/sysdep/linux/netlink/netlink.Y b/sysdep/linux/netlink/netlink.Y index b00b0eee..51689ff9 100644 --- a/sysdep/linux/netlink/netlink.Y +++ b/sysdep/linux/netlink/netlink.Y @@ -20,7 +20,7 @@ nl_item: KERNEL TABLE expr { if ($3 <= 0 || $3 >= NL_NUM_TABLES) cf_error("Kernel routing table number out of range"); - THIS_KRT->scan.table_id = $3; + THIS_KRT->sys.table_id = $3; } ; diff --git a/sysdep/linux/netlink/netlink.c b/sysdep/linux/netlink/netlink.c index 182088a1..e3faf043 100644 --- a/sysdep/linux/netlink/netlink.c +++ b/sysdep/linux/netlink/netlink.c @@ -548,7 +548,7 @@ nl_parse_addr(struct nlmsghdr *h) } void -krt_if_scan(struct kif_proto *p UNUSED) +kif_do_scan(struct kif_proto *p UNUSED) { struct nlmsghdr *h; @@ -634,7 +634,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) r.r.rtm_family = BIRD_AF; r.r.rtm_dst_len = net->n.pxlen; r.r.rtm_tos = 0; - r.r.rtm_table = KRT_CF->scan.table_id; + r.r.rtm_table = KRT_CF->sys.table_id; r.r.rtm_protocol = RTPROT_BIRD; r.r.rtm_scope = RT_SCOPE_UNIVERSE; nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix); @@ -687,7 +687,7 @@ nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) } void -krt_set_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) +krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) { int err = 0; @@ -940,7 +940,7 @@ nl_parse_route(struct nlmsghdr *h, int scan) } void -krt_scan_fire(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ +krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ { struct nlmsghdr *h; @@ -1084,36 +1084,9 @@ nl_open_async(void) static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8]; void -krt_scan_preconfig(struct config *c UNUSED) +krt_sys_start(struct krt_proto *p, int first) { - bzero(&nl_cf_table, sizeof(nl_cf_table)); -} - -void -krt_scan_postconfig(struct krt_config *x) -{ - int id = x->scan.table_id; - - if (nl_cf_table[id/8] & (1 << (id%8))) - cf_error("Multiple kernel syncers defined for table #%d", id); - nl_cf_table[id/8] |= (1 << (id%8)); -} - -void -krt_scan_construct(struct krt_config *x) -{ -#ifndef IPV6 - x->scan.table_id = RT_TABLE_MAIN; -#else - x->scan.table_id = 254; -#endif -} - -void -krt_scan_start(struct krt_proto *p, int first) -{ - init_list(&p->scan.temp_ifs); - nl_table_map[KRT_CF->scan.table_id] = p; + nl_table_map[KRT_CF->sys.table_id] = p; if (first) { nl_open(); @@ -1122,13 +1095,55 @@ krt_scan_start(struct krt_proto *p, int first) } void -krt_scan_shutdown(struct krt_proto *p UNUSED, int last UNUSED) +krt_sys_shutdown(struct krt_proto *p UNUSED, int last UNUSED) { } +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; +} + + void -krt_if_start(struct kif_proto *p UNUSED) +krt_sys_preconfig(struct config *c UNUSED) +{ + bzero(&nl_cf_table, sizeof(nl_cf_table)); +} + +void +krt_sys_postconfig(struct krt_config *x) +{ + int id = x->sys.table_id; + + if (nl_cf_table[id/8] & (1 << (id%8))) + cf_error("Multiple kernel syncers defined for table #%d", id); + nl_cf_table[id/8] |= (1 << (id%8)); +} + +void +krt_sys_init_config(struct krt_config *cf) +{ + cf->sys.table_id = RT_TABLE_MAIN; +} + +void +krt_sys_copy_config(struct krt_config *d, struct krt_config *s) +{ + d->sys.table_id = s->sys.table_id; +} + + + +void +kif_sys_start(struct kif_proto *p UNUSED) { nl_open(); nl_open_async(); } + +void +kif_sys_shutdown(struct kif_proto *p UNUSED) +{ +} diff --git a/sysdep/unix/krt-iface.c b/sysdep/unix/krt-iface.c index 69048ae8..88c17ecd 100644 --- a/sysdep/unix/krt-iface.c +++ b/sysdep/unix/krt-iface.c @@ -186,7 +186,7 @@ scan_ifs(struct ifreq *r, int cnt) } void -krt_if_scan(struct kif_proto *p) +kif_do_scan(struct kif_proto *p) { struct ifconf ic; static int last_ifbuf_size = 4*sizeof(struct ifreq); @@ -208,22 +208,17 @@ krt_if_scan(struct kif_proto *p) } void -krt_if_construct(struct kif_config *c) +kif_start(struct kif_proto *p) { } void -krt_if_start(struct kif_proto *p) +kif_shutdown(struct kif_proto *p) { } void -krt_if_shutdown(struct kif_proto *p) -{ -} - -void -krt_if_io_init(void) +kif_io_init(void) { if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); DBG("Using socket %d for interface and route scanning\n", if_scan_sock); diff --git a/sysdep/unix/krt-iface.h b/sysdep/unix/krt-iface.h index 9e12bcc3..e9e8e507 100644 --- a/sysdep/unix/krt-iface.h +++ b/sysdep/unix/krt-iface.h @@ -9,15 +9,21 @@ #ifndef _BIRD_KRT_IFACE_H_ #define _BIRD_KRT_IFACE_H_ -struct krt_if_params { +struct kif_params { }; -struct krt_if_status { +struct kif_status { }; extern int if_scan_sock; -static inline int kif_params_same(struct krt_if_params *old UNUSED, struct krt_if_params *new UNUSED) { return 1; } -static inline void kif_copy_params(struct krt_if_params *dest UNUSED, struct krt_if_params *src UNUSED) { } + +static inline void kif_sys_init(struct kif_proto *p UNUSED) { } +static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } + +static inline void kif_sys_preconfig(struct config *c UNUSED) { } +static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } +static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } +static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } #endif diff --git a/sysdep/unix/krt-set.c b/sysdep/unix/krt-set.c index 23cbe5c5..252c5d3e 100644 --- a/sysdep/unix/krt-set.c +++ b/sysdep/unix/krt-set.c @@ -79,7 +79,7 @@ krt_ioctl(int ioc, rte *e, char *name) } void -krt_set_notify(struct krt_proto *p, net *net, rte *new, rte *old) +krt_do_notify(struct krt_proto *p, net *net, rte *new, rte *old) { if (old) { @@ -94,18 +94,19 @@ krt_set_notify(struct krt_proto *p, net *net, rte *new, rte *old) } void -krt_set_start(struct krt_proto *x, int first) +krt_sys_start(struct krt_proto *x, int first) { if (if_scan_sock < 0) bug("krt set: missing socket"); } void -krt_set_construct(struct krt_config *c) +krt_sys_shutdown(struct krt_proto *x, int last) +{ +} + +void +krt_sys_construct(struct krt_config *c) { } -void -krt_set_shutdown(struct krt_proto *x, int last) -{ -} diff --git a/sysdep/unix/krt-set.h b/sysdep/unix/krt-set.h index 87cffcfc..8ef3170e 100644 --- a/sysdep/unix/krt-set.h +++ b/sysdep/unix/krt-set.h @@ -1,5 +1,5 @@ /* - * BIRD -- Unix Kernel Route Syncer -- Setting + * BIRD -- Unix Kernel Route Syncer * * (c) 1998--2000 Martin Mares * @@ -9,13 +9,19 @@ #ifndef _BIRD_KRT_SET_H_ #define _BIRD_KRT_SET_H_ -struct krt_set_params { +struct krt_params { }; -struct krt_set_status { +struct krt_status { }; -static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; } -static inline void krt_set_copy_params(struct krt_set_params *d UNUSED, struct krt_set_params *s UNUSED) { } + +static inline void krt_sys_init(struct krt_proto *p UNUSED) { } +static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; } + +static inline void krt_sys_preconfig(struct config *c UNUSED) { } +static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { } +static inline void krt_sys_init_config(struct krt_config *c UNUSED) { } +static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { } #endif diff --git a/sysdep/unix/krt.Y b/sysdep/unix/krt.Y index c0141f57..469c136d 100644 --- a/sysdep/unix/krt.Y +++ b/sysdep/unix/krt.Y @@ -25,17 +25,7 @@ CF_GRAMMAR CF_ADDTO(proto, kern_proto '}') -kern_proto_start: proto_start KERNEL { -#ifndef CONFIG_MULTIPLE_TABLES - if (cf_krt) - cf_error("Kernel protocol already defined"); -#endif - cf_krt = this_proto = proto_config_new(&proto_unix_kernel, sizeof(struct krt_config), $1); - THIS_KRT->scan_time = 60; - THIS_KRT->learn = THIS_KRT->persist = 0; - krt_scan_construct(THIS_KRT); - krt_set_construct(THIS_KRT); - } +kern_proto_start: proto_start KERNEL { this_proto = krt_init_config($1); } ; CF_ADDTO(kern_proto, kern_proto_start proto_name '{') @@ -62,14 +52,7 @@ kern_item: CF_ADDTO(proto, kif_proto '}') -kif_proto_start: proto_start DEVICE { - if (cf_kif) - cf_error("Kernel device protocol already defined"); - cf_kif = this_proto = proto_config_new(&proto_unix_iface, sizeof(struct kif_config), $1); - THIS_KIF->scan_time = 60; - init_list(&THIS_KIF->primary); - krt_if_construct(THIS_KIF); - } +kif_proto_start: proto_start DEVICE { this_proto = kif_init_config($1); } ; CF_ADDTO(kif_proto, kif_proto_start proto_name '{') diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index de97a092..cc03bb88 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -66,25 +66,18 @@ krt_io_init(void) { krt_pool = rp_new(&root_pool, "Kernel Syncer"); krt_filter_lp = lp_new(krt_pool, 4080); - krt_if_io_init(); + kif_sys_io_init(); } /* * Interfaces */ -struct proto_config *cf_kif; - +static struct kif_config *kif_cf; static struct kif_proto *kif_proto; static timer *kif_scan_timer; static bird_clock_t kif_last_shot; -static void -kif_preconfig(struct protocol *P UNUSED, struct config *c UNUSED) -{ - cf_kif = NULL; -} - static void kif_scan(timer *t) { @@ -92,7 +85,7 @@ kif_scan(timer *t) KRT_TRACE(p, D_EVENTS, "Scanning interfaces"); kif_last_shot = now; - krt_if_scan(p); + kif_do_scan(p); } static void @@ -112,45 +105,6 @@ kif_request_scan(void) tm_start(kif_scan_timer, 1); } -static struct proto * -kif_init(struct proto_config *c) -{ - struct kif_proto *p = proto_new(c, sizeof(struct kif_proto)); - return &p->p; -} - -static int -kif_start(struct proto *P) -{ - struct kif_proto *p = (struct kif_proto *) P; - - kif_proto = p; - krt_if_start(p); - - /* Start periodic interface scanning */ - kif_scan_timer = tm_new(P->pool); - kif_scan_timer->hook = kif_scan; - kif_scan_timer->data = p; - kif_scan_timer->recurrent = KIF_CF->scan_time; - kif_scan(kif_scan_timer); - tm_start(kif_scan_timer, KIF_CF->scan_time); - - return PS_UP; -} - -static int -kif_shutdown(struct proto *P) -{ - struct kif_proto *p = (struct kif_proto *) P; - - tm_stop(kif_scan_timer); - krt_if_shutdown(p); - kif_proto = NULL; - - return PS_DOWN; -} - - static inline int prefer_scope(struct ifa *a, struct ifa *b) { return (a->scope > SCOPE_LINK) && (b->scope <= SCOPE_LINK); } @@ -193,13 +147,53 @@ kif_choose_primary(struct iface *i) } +static struct proto * +kif_init(struct proto_config *c) +{ + struct kif_proto *p = proto_new(c, sizeof(struct kif_proto)); + + kif_sys_init(p); + return &p->p; +} + +static int +kif_start(struct proto *P) +{ + struct kif_proto *p = (struct kif_proto *) P; + + kif_proto = p; + kif_sys_start(p); + + /* Start periodic interface scanning */ + kif_scan_timer = tm_new(P->pool); + kif_scan_timer->hook = kif_scan; + kif_scan_timer->data = p; + kif_scan_timer->recurrent = KIF_CF->scan_time; + kif_scan(kif_scan_timer); + tm_start(kif_scan_timer, KIF_CF->scan_time); + + return PS_UP; +} + +static int +kif_shutdown(struct proto *P) +{ + struct kif_proto *p = (struct kif_proto *) P; + + tm_stop(kif_scan_timer); + kif_sys_shutdown(p); + kif_proto = NULL; + + return PS_DOWN; +} + static int kif_reconfigure(struct proto *p, struct proto_config *new) { struct kif_config *o = (struct kif_config *) p->cf; struct kif_config *n = (struct kif_config *) new; - if (!kif_params_same(&o->iface, &n->iface)) + if (!kif_sys_reconfigure((struct kif_proto *) p, n, o)) return 0; if (o->scan_time != n->scan_time) @@ -224,6 +218,28 @@ kif_reconfigure(struct proto *p, struct proto_config *new) return 1; } + +static void +kif_preconfig(struct protocol *P UNUSED, struct config *c) +{ + kif_cf = NULL; + kif_sys_preconfig(c); +} + +struct proto_config * +kif_init_config(int class) +{ + if (kif_cf) + cf_error("Kernel device protocol already defined"); + + kif_cf = (struct kif_config *) proto_config_new(&proto_unix_iface, sizeof(struct kif_config), class); + kif_cf->scan_time = 60; + init_list(&kif_cf->primary); + + kif_sys_init_config(kif_cf); + return (struct proto_config *) kif_cf; +} + static void kif_copy_config(struct proto_config *dest, struct proto_config *src) { @@ -231,13 +247,13 @@ kif_copy_config(struct proto_config *dest, struct proto_config *src) struct kif_config *s = (struct kif_config *) src; /* Shallow copy of everything (just scan_time currently) */ - proto_copy_rest(dest, src, sizeof(struct krt_config)); + proto_copy_rest(dest, src, sizeof(struct kif_config)); /* Copy primary addr list */ cfg_copy_list(&d->primary, &s->primary, sizeof(struct kif_primary_item)); /* Fix sysdep parts */ - kif_copy_params(&d->iface, &s->iface); + kif_sys_copy_config(d, s); } @@ -558,7 +574,7 @@ krt_flush_routes(struct krt_proto *p) a->source != RTS_DEVICE && a->source != RTS_INHERIT) { /* FIXME: this does not work if gw is changed in export filter */ - krt_set_notify(p, e->net, NULL, e, NULL); + krt_do_notify(p, e->net, NULL, e, NULL); n->n.flags &= ~KRF_INSTALLED; } } @@ -717,7 +733,7 @@ krt_prune(struct krt_proto *p) if (new && (f->flags & KRF_INSTALLED)) { krt_trace_in(p, new, "reinstalling"); - krt_set_notify(p, n, new, NULL, tmpa); + krt_do_notify(p, n, new, NULL, tmpa); } break; case KRF_SEEN: @@ -726,11 +742,11 @@ krt_prune(struct krt_proto *p) break; case KRF_UPDATE: krt_trace_in(p, new, "updating"); - krt_set_notify(p, n, new, old, tmpa); + krt_do_notify(p, n, new, old, tmpa); break; case KRF_DELETE: krt_trace_in(p, old, "deleting"); - krt_set_notify(p, n, NULL, old, NULL); + krt_do_notify(p, n, NULL, old, NULL); break; default: bug("krt_prune: invalid route status"); @@ -766,7 +782,7 @@ krt_got_route_async(struct krt_proto *p, rte *e, int new) if (new) { krt_trace_in(p, e, "[redirect] deleting"); - krt_set_notify(p, net, NULL, e, NULL); + krt_do_notify(p, net, NULL, e, NULL); } /* If !new, it is probably echo of our deletion */ break; @@ -800,7 +816,7 @@ krt_scan(timer *t UNUSED) p = SKIP_BACK(struct krt_proto, instance_node, HEAD(krt_instance_list)); if (p->instance_node.next) KRT_TRACE(p, D_EVENTS, "Scanning routing table"); - krt_scan_fire(NULL); + krt_do_scan(NULL); WALK_LIST(q, krt_instance_list) { p = SKIP_BACK(struct krt_proto, instance_node, q); @@ -810,14 +826,45 @@ krt_scan(timer *t UNUSED) #else p = t->data; KRT_TRACE(p, D_EVENTS, "Scanning routing table"); - krt_scan_fire(p); + krt_do_scan(p); krt_prune(p); #endif } + /* * Updates */ + +static struct ea_list * +krt_make_tmp_attrs(rte *rt, struct linpool *pool) +{ + struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2 * sizeof(eattr)); + + l->next = NULL; + l->flags = EALF_SORTED; + l->count = 2; + + l->attrs[0].id = EA_KRT_SOURCE; + l->attrs[0].flags = 0; + l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP; + l->attrs[0].u.data = rt->u.krt.proto; + + l->attrs[1].id = EA_KRT_METRIC; + l->attrs[1].flags = 0; + l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP; + l->attrs[1].u.data = rt->u.krt.metric; + + return l; +} + +static void +krt_store_tmp_attrs(rte *rt, struct ea_list *attrs) +{ + /* EA_KRT_SOURCE is read-only */ + rt->u.krt.metric = ea_get_int(attrs, EA_KRT_METRIC, 0); +} + static int krt_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool) { @@ -853,37 +900,37 @@ krt_notify(struct proto *P, struct rtable *table UNUSED, net *net, else net->n.flags &= ~KRF_INSTALLED; if (p->initialized) /* Before first scan we don't touch the routes */ - krt_set_notify(p, net, new, old, eattrs); + krt_do_notify(p, net, new, old, eattrs); } +static int +krt_rte_same(rte *a, rte *b) +{ + /* src is always KRT_SRC_ALIEN and type is irrelevant */ + return (a->u.krt.proto == b->u.krt.proto) && (a->u.krt.metric == b->u.krt.metric); +} + + /* * Protocol glue */ -struct proto_config *cf_krt; +struct krt_config *krt_cf; -static void -krt_preconfig(struct protocol *P UNUSED, struct config *c) +static struct proto * +krt_init(struct proto_config *c) { - cf_krt = NULL; - krt_scan_preconfig(c); -} + struct krt_proto *p = proto_new(c, sizeof(struct krt_proto)); -static void -krt_postconfig(struct proto_config *C) -{ - struct krt_config *c = (struct krt_config *) C; + p->p.accept_ra_types = RA_OPTIMAL; + p->p.make_tmp_attrs = krt_make_tmp_attrs; + p->p.store_tmp_attrs = krt_store_tmp_attrs; + p->p.import_control = krt_import_control; + p->p.rt_notify = krt_notify; + p->p.rte_same = krt_rte_same; -#ifdef CONFIG_ALL_TABLES_AT_ONCE - struct krt_config *first = (struct krt_config *) cf_krt; - if (first->scan_time != c->scan_time) - cf_error("All kernel syncers must use the same table scan interval"); -#endif - - if (C->table->krt_attached) - cf_error("Kernel syncer (%s) already attached to table %s", C->table->krt_attached->name, C->table->name); - C->table->krt_attached = C; - krt_scan_postconfig(c); + krt_sys_init(p); + return &p->p; } static timer * @@ -920,8 +967,7 @@ krt_start(struct proto *P) krt_learn_init(p); #endif - krt_scan_start(p, first); - krt_set_start(p, first); + krt_sys_start(p, first); /* Start periodic routing table scanning */ #ifdef CONFIG_ALL_TABLES_AT_ONCE @@ -955,8 +1001,7 @@ krt_shutdown(struct proto *P) if (p->initialized && !KRT_CF->persist) krt_flush_routes(p); - krt_set_shutdown(p, last); - krt_scan_shutdown(p, last); + krt_sys_shutdown(p, last); #ifdef CONFIG_ALL_TABLES_AT_ONCE if (last) @@ -966,69 +1011,55 @@ krt_shutdown(struct proto *P) return PS_DOWN; } -static struct ea_list * -krt_make_tmp_attrs(rte *rt, struct linpool *pool) -{ - struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 2 * sizeof(eattr)); - - l->next = NULL; - l->flags = EALF_SORTED; - l->count = 2; - - l->attrs[0].id = EA_KRT_SOURCE; - l->attrs[0].flags = 0; - l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP; - l->attrs[0].u.data = rt->u.krt.proto; - - l->attrs[1].id = EA_KRT_METRIC; - l->attrs[1].flags = 0; - l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP; - l->attrs[1].u.data = rt->u.krt.metric; - - return l; -} - -static void -krt_store_tmp_attrs(rte *rt, struct ea_list *attrs) -{ - /* EA_KRT_SOURCE is read-only */ - rt->u.krt.metric = ea_get_int(attrs, EA_KRT_METRIC, 0); -} - -static int -krt_rte_same(rte *a, rte *b) -{ - /* src is always KRT_SRC_ALIEN and type is irrelevant */ - return (a->u.krt.proto == b->u.krt.proto) && (a->u.krt.metric == b->u.krt.metric); -} - -static struct proto * -krt_init(struct proto_config *c) -{ - struct krt_proto *p = proto_new(c, sizeof(struct krt_proto)); - - p->p.accept_ra_types = RA_OPTIMAL; - p->p.make_tmp_attrs = krt_make_tmp_attrs; - p->p.store_tmp_attrs = krt_store_tmp_attrs; - p->p.import_control = krt_import_control; - p->p.rt_notify = krt_notify; - p->p.rte_same = krt_rte_same; - - return &p->p; -} - static int krt_reconfigure(struct proto *p, struct proto_config *new) { struct krt_config *o = (struct krt_config *) p->cf; struct krt_config *n = (struct krt_config *) new; - return o->scan_time == n->scan_time - && o->learn == n->learn /* persist needn't be the same */ - && o->devroutes == n->devroutes - && krt_set_params_same(&o->set, &n->set) - && krt_scan_params_same(&o->scan, &n->scan) - ; + if (!krt_sys_reconfigure((struct krt_proto *) p, n, o)) + return 0; + + /* persist needn't be the same */ + return o->scan_time == n->scan_time && o->learn == n->learn && o->devroutes == n->devroutes; +} + +static void +krt_preconfig(struct protocol *P UNUSED, struct config *c) +{ + krt_cf = NULL; + krt_sys_preconfig(c); +} + +static void +krt_postconfig(struct proto_config *C) +{ + struct krt_config *c = (struct krt_config *) C; + +#ifdef CONFIG_ALL_TABLES_AT_ONCE + if (krt_cf->scan_time != c->scan_time) + cf_error("All kernel syncers must use the same table scan interval"); +#endif + + if (C->table->krt_attached) + cf_error("Kernel syncer (%s) already attached to table %s", C->table->krt_attached->name, C->table->name); + C->table->krt_attached = C; + krt_sys_postconfig(c); +} + +struct proto_config * +krt_init_config(int class) +{ +#ifndef CONFIG_MULTIPLE_TABLES + if (krt_cf) + cf_error("Kernel protocol already defined"); +#endif + + krt_cf = (struct krt_config *) proto_config_new(&proto_unix_kernel, sizeof(struct krt_config), class); + krt_cf->scan_time = 60; + + krt_sys_init_config(krt_cf); + return (struct proto_config *) krt_cf; } static void @@ -1041,8 +1072,7 @@ krt_copy_config(struct proto_config *dest, struct proto_config *src) proto_copy_rest(dest, src, sizeof(struct krt_config)); /* Fix sysdep parts */ - krt_set_copy_params(&d->set, &s->set); - krt_scan_copy_params(&d->scan, &s->scan); + krt_sys_copy_config(d, s); } static int diff --git a/sysdep/unix/krt.h b/sysdep/unix/krt.h index 19b69e49..a3b5658a 100644 --- a/sysdep/unix/krt.h +++ b/sysdep/unix/krt.h @@ -45,8 +45,7 @@ extern struct protocol proto_unix_kernel; struct krt_config { struct proto_config c; - struct krt_set_params set; - struct krt_scan_params scan; + struct krt_params sys; /* Sysdep params */ int persist; /* Keep routes when we exit */ int scan_time; /* How often we re-scan routes */ int learn; /* Learn routes from other sources */ @@ -55,9 +54,7 @@ struct krt_config { struct krt_proto { struct proto p; - struct krt_set_status set; - struct krt_scan_status scan; - struct krt_if_status iface; + struct krt_status sys; /* Sysdep state */ #ifdef KRT_ALLOW_LEARN struct rtable krt_table; /* Internal table of inherited routes */ #endif @@ -69,7 +66,6 @@ struct krt_proto { int initialized; /* First scan has already been finished */ }; -extern struct proto_config *cf_krt; extern pool *krt_pool; #define KRT_CF ((struct krt_config *)p->p.cf) @@ -79,6 +75,7 @@ extern pool *krt_pool; if (pr->p.debug & fl) \ { log(L_TRACE "%s: " msg, pr->p.name , ## args); } } while(0) +struct proto_config * kif_init_config(int class); void kif_request_scan(void); void krt_got_route(struct krt_proto *p, struct rte *e); void krt_got_route_async(struct krt_proto *p, struct rte *e, int new); @@ -101,46 +98,50 @@ struct kif_primary_item { struct kif_config { struct proto_config c; - struct krt_if_params iface; + struct kif_params sys; /* Sysdep params */ int scan_time; /* How often we re-scan interfaces */ list primary; /* Preferences for primary addresses (struct kif_primary_item) */ }; struct kif_proto { struct proto p; - struct krt_if_status iface; + struct kif_status sys; /* Sysdep state */ }; -extern struct proto_config *cf_kif; - #define KIF_CF ((struct kif_config *)p->p.cf) +struct proto_config * krt_init_config(int class); + + /* krt-scan.c */ -void krt_scan_preconfig(struct config *); -void krt_scan_postconfig(struct krt_config *); -void krt_scan_construct(struct krt_config *); -void krt_scan_start(struct krt_proto *, int); -void krt_scan_shutdown(struct krt_proto *, int); +void krt_sys_init(struct krt_proto *); +void krt_sys_start(struct krt_proto *, int); +void krt_sys_shutdown(struct krt_proto *, int); +int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o); -void krt_scan_fire(struct krt_proto *); +void krt_sys_preconfig(struct config *); +void krt_sys_postconfig(struct krt_config *); +void krt_sys_init_config(struct krt_config *); +void krt_sys_copy_config(struct krt_config *, struct krt_config *); -/* krt-set.c */ +int krt_capable(rte *e); +void krt_do_scan(struct krt_proto *); +void krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs); -void krt_set_construct(struct krt_config *); -void krt_set_start(struct krt_proto *, int); -void krt_set_shutdown(struct krt_proto *, int); - -int krt_capable(rte *e); -void krt_set_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs); /* krt-iface.c */ -void krt_if_construct(struct kif_config *); -void krt_if_start(struct kif_proto *); -void krt_if_shutdown(struct kif_proto *); +void kif_sys_init(struct kif_proto *); +void kif_sys_start(struct kif_proto *); +void kif_sys_shutdown(struct kif_proto *); +int kif_sys_reconfigure(struct kif_proto *, struct kif_config *, struct kif_config *); -void krt_if_scan(struct kif_proto *); -void krt_if_io_init(void); +void kif_sys_init_config(struct kif_config *); +void kif_sys_copy_config(struct kif_config *, struct kif_config *); + +void kif_do_scan(struct kif_proto *); + +// void kif_sys_io_init(void); #endif From f1aceff59bbf942bc11c2e9a4c51e381c06f2b20 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Mon, 30 Apr 2012 22:21:52 +0200 Subject: [PATCH 3/8] Cleanup in sysdep KRT code, part 2. Remove support for historic Linux kernels, merge krt-iface, krt-set and krt-scan stub headers. --- aclocal.m4 | 17 - configure.in | 14 +- nest/a-path.c | 1 - sysdep/bsd/Modules | 7 +- sysdep/bsd/krt-iface.h | 33 - sysdep/bsd/krt-scan.h | 30 - sysdep/bsd/krt-set.h | 17 - sysdep/bsd/krt-sock.h | 23 - sysdep/cf/README | 16 +- sysdep/cf/bsd-v6.h | 6 - sysdep/cf/bsd.h | 7 - sysdep/cf/linux-20.h | 26 - sysdep/cf/linux-21.h | 26 - sysdep/cf/linux-22.h | 26 - sysdep/cf/linux-v6.h | 9 +- sysdep/linux/Modules | 7 +- sysdep/linux/krt-scan.c | 199 ------ sysdep/linux/krt-scan.h | 21 - sysdep/linux/netlink/Modules | 5 - sysdep/linux/netlink/krt-iface.h | 33 - sysdep/linux/netlink/krt-scan.h | 30 - sysdep/linux/netlink/krt-set.h | 28 - sysdep/linux/netlink/netlink.Y | 32 - sysdep/linux/netlink/netlink.c | 1149 ------------------------------ sysdep/linux/sysio.h | 71 +- sysdep/unix/Modules | 10 - sysdep/unix/io.c | 2 +- sysdep/unix/krt-iface.c | 228 ------ sysdep/unix/krt-iface.h | 29 - sysdep/unix/krt-set.c | 112 --- sysdep/unix/krt-set.h | 27 - sysdep/unix/krt.c | 12 +- sysdep/unix/krt.h | 9 +- 33 files changed, 38 insertions(+), 2224 deletions(-) delete mode 100644 sysdep/bsd/krt-iface.h delete mode 100644 sysdep/bsd/krt-scan.h delete mode 100644 sysdep/bsd/krt-set.h delete mode 100644 sysdep/bsd/krt-sock.h delete mode 100644 sysdep/cf/linux-20.h delete mode 100644 sysdep/cf/linux-21.h delete mode 100644 sysdep/cf/linux-22.h delete mode 100644 sysdep/linux/krt-scan.c delete mode 100644 sysdep/linux/krt-scan.h delete mode 100644 sysdep/linux/netlink/Modules delete mode 100644 sysdep/linux/netlink/krt-iface.h delete mode 100644 sysdep/linux/netlink/krt-scan.h delete mode 100644 sysdep/linux/netlink/krt-set.h delete mode 100644 sysdep/linux/netlink/netlink.Y delete mode 100644 sysdep/linux/netlink/netlink.c delete mode 100644 sysdep/unix/krt-iface.c delete mode 100644 sysdep/unix/krt-iface.h delete mode 100644 sysdep/unix/krt-set.c delete mode 100644 sysdep/unix/krt-set.h diff --git a/aclocal.m4 b/aclocal.m4 index ee545252..75b3f92a 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -133,23 +133,6 @@ if test "$bird_cv_struct_ip_mreqn" = yes ; then fi ]) -AC_DEFUN(BIRD_CHECK_LINUX_VERSION, -[AC_CACHE_CHECK([Linux kernel version], bird_cv_sys_linux_version, [ -AC_REQUIRE_CPP()dnl -cat > conftest.$ac_ext < -VERSION: UTS_RELEASE -EOF -bird_cv_sys_linux_version=`eval "$ac_cpp conftest.$ac_ext" 2>&AC_FD_CC | sed '/^VERSION/!d;s/^VERSION: "//;s/".*//'` -rm -rf conftest* -if test -z "$bird_cv_sys_linux_version" ; then - AC_MSG_RESULT([unknown]) - AC_MSG_ERROR([Cannot determine kernel version]) -fi -])]) - AC_DEFUN(BIRD_CHECK_GCC_OPTIONS, [AC_CACHE_VAL(bird_cv_c_option_no_pointer_sign, [ cat >conftest.c < #include diff --git a/nest/a-path.c b/nest/a-path.c index 058b4344..63ac402e 100644 --- a/nest/a-path.c +++ b/nest/a-path.c @@ -15,7 +15,6 @@ #include "lib/string.h" #include "filter/filter.h" - // static inline void put_as(byte *data, u32 as) { put_u32(data, as); } // static inline u32 get_as(byte *data) { return get_u32(data); } diff --git a/sysdep/bsd/Modules b/sysdep/bsd/Modules index 84abffdb..3729587d 100644 --- a/sysdep/bsd/Modules +++ b/sysdep/bsd/Modules @@ -1,6 +1,3 @@ -krt-scan.h -krt-iface.h -sysio.h -krt-set.h krt-sock.c -krt-sock.h +krt-sys.h +sysio.h diff --git a/sysdep/bsd/krt-iface.h b/sysdep/bsd/krt-iface.h deleted file mode 100644 index 278c4be9..00000000 --- a/sysdep/bsd/krt-iface.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * BIRD -- Unix Kernel Socket Route Syncer -- Dummy Include File - * - * (c) 2004 Ondrej Filip - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_IFACE_H_ -#define _BIRD_KRT_IFACE_H_ - -/* - * We don't have split iface/scan/set parts. See krt-sock.h. - */ - -struct kif_params { -}; - -struct kif_status { -}; - - -static inline void kif_sys_init(struct kif_proto *p UNUSED) { } -static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } - -static inline void kif_sys_preconfig(struct config *c UNUSED) { } -static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } -static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } -static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } - -static inline void kif_sys_io_init(void) { } - -#endif diff --git a/sysdep/bsd/krt-scan.h b/sysdep/bsd/krt-scan.h deleted file mode 100644 index 045b9e77..00000000 --- a/sysdep/bsd/krt-scan.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * BIRD -- *BSD Kernel Route Syncer -- Scanning - * - * (c) 2004 Ondrej Filip - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SCAN_H_ -#define _BIRD_KRT_SCAN_H_ - -struct krt_scan_params { -}; - -struct krt_scan_status { -}; - - -static inline void krt_sys_init(struct krt_proto *p UNUSED) { } -static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; } - -static inline void krt_sys_preconfig(struct config *c UNUSED) { } -static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { } -static inline void krt_sys_init_config(struct krt_config *c UNUSED) { } -static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { } - - - - -#endif diff --git a/sysdep/bsd/krt-set.h b/sysdep/bsd/krt-set.h deleted file mode 100644 index b5453d4b..00000000 --- a/sysdep/bsd/krt-set.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - * BIRD -- Unix Kernel Socket Route Syncer -- Dummy Include File - * - * (c) 2004 Ondrej Filip - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SET_H_ -#define _BIRD_KRT_SET_H_ - -/* - * We don't have split iface/scan/set parts. See krt-sock.h. - */ -#include "lib/krt-sock.h" - -#endif diff --git a/sysdep/bsd/krt-sock.h b/sysdep/bsd/krt-sock.h deleted file mode 100644 index d9a7ca5d..00000000 --- a/sysdep/bsd/krt-sock.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * BIRD -- Unix Kernel Route Syncer -- Setting - * - * (c) 2004 Ondrej Filip - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SOCK_H_ -#define _BIRD_KRT_SOCK_H_ - -struct krt_params { -}; - -struct krt_status { -}; - -static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; } -static inline void krt_set_copy_params(struct krt_set_params *d UNUSED, struct krt_set_params *s UNUSED) { } - -void krt_read_msg(struct proto *p, struct ks_msg *msg, int scan); - -#endif diff --git a/sysdep/cf/README b/sysdep/cf/README index 3b5bcd4f..1c11edcf 100644 --- a/sysdep/cf/README +++ b/sysdep/cf/README @@ -5,19 +5,9 @@ CONFIG_AUTO_ROUTES Device routes are added automagically by the kernel CONFIG_SELF_CONSCIOUS We're able to recognize whether route was installed by us CONFIG_MULTIPLE_TABLES The kernel supports multiple routing tables CONFIG_ALL_TABLES_AT_ONCE Kernel scanner wants to process all tables at once + CONFIG_MC_PROPER_SRC Multicast packets have source address according to socket saddr field -CONFIG_RESTRICTED_PRIVILEGES Implements restricted privileges using drop_uid() - -CONFIG_UNIX_IFACE Use Unix interface scanner -CONFIG_UNIX_SET Use Unix route setting -CONFIG_UNIX_DONTROUTE Use setsockopts DONTROUTE (undef for *BSD) CONFIG_SKIP_MC_BIND Don't call bind on multicast socket (def for *BSD) -CONFIG_LINUX_SCAN Use Linux /proc/net/route scanner - -CONFIG_ALL_MULTICAST krt-iface: All devices support multicasting (i.e., ignore IFF_MULTICAST) -CONFIG_UNNUM_MULTICAST krt-iface: We support multicasts on unnumbered PtP devices - -CONFIG_LINUX_MC_MREQN Linux: Use struct mreqn for multicasting -CONFIG_LINUX_MC_MREQ Linux: Use struct mreq -CONFIG_LINUX_MC_MREQ_BIND Linux: Use struct mreq and SO_BINDTODEVICE +CONFIG_UNIX_DONTROUTE Use setsockopts DONTROUTE (undef for *BSD) +CONFIG_RESTRICTED_PRIVILEGES Implements restricted privileges using drop_uid() diff --git a/sysdep/cf/bsd-v6.h b/sysdep/cf/bsd-v6.h index 66985abf..b7f25f64 100644 --- a/sysdep/cf/bsd-v6.h +++ b/sysdep/cf/bsd-v6.h @@ -10,14 +10,8 @@ #define CONFIG_AUTO_ROUTES #define CONFIG_SELF_CONSCIOUS -#undef CONFIG_MULTIPLE_TABLES - -#undef CONFIG_UNIX_IFACE -#undef CONFIG_UNIX_SET #define CONFIG_SKIP_MC_BIND -#define CONFIG_ALL_MULTICAST -#define CONFIG_UNNUM_MULTICAST /* Link: sysdep/unix diff --git a/sysdep/cf/bsd.h b/sysdep/cf/bsd.h index acd1b58b..e7cc135f 100644 --- a/sysdep/cf/bsd.h +++ b/sysdep/cf/bsd.h @@ -8,15 +8,8 @@ #define CONFIG_AUTO_ROUTES #define CONFIG_SELF_CONSCIOUS -#undef CONFIG_MULTIPLE_TABLES - -#undef CONFIG_UNIX_IFACE -#undef CONFIG_UNIX_SET -#undef CONFIG_UNIX_DONTROUTE #define CONFIG_SKIP_MC_BIND -#define CONFIG_ALL_MULTICAST -#define CONFIG_UNNUM_MULTICAST /* Link: sysdep/unix diff --git a/sysdep/cf/linux-20.h b/sysdep/cf/linux-20.h deleted file mode 100644 index e409706a..00000000 --- a/sysdep/cf/linux-20.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Configuration for Linux 2.0 based systems - * - * (c) 1998--1999 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#undef CONFIG_AUTO_ROUTES -#undef CONFIG_SELF_CONSCIOUS -#undef CONFIG_MULTIPLE_TABLES - -#define CONFIG_UNIX_IFACE -#define CONFIG_UNIX_SET -#define CONFIG_UNIX_DONTROUTE -#undef CONFIG_SKIP_MC_BIND -#define CONFIG_LINUX_SCAN - -#define CONFIG_LINUX_MC_MREQ_BIND -#define CONFIG_ALL_MULTICAST -#define CONFIG_UNNUM_MULTICAST - -/* -Link: sysdep/linux -Link: sysdep/unix - */ diff --git a/sysdep/cf/linux-21.h b/sysdep/cf/linux-21.h deleted file mode 100644 index 0fce7053..00000000 --- a/sysdep/cf/linux-21.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Configuration for Linux 2.1/2.2 based systems without Netlink - * - * (c) 1998--1999 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#define CONFIG_AUTO_ROUTES -#undef CONFIG_SELF_CONSCIOUS -#undef CONFIG_MULTIPLE_TABLES - -#define CONFIG_UNIX_IFACE -#define CONFIG_UNIX_SET -#define CONFIG_UNIX_DONTROUTE -#undef CONFIG_SKIP_MC_BIND -#define CONFIG_LINUX_SCAN - -#define CONFIG_LINUX_MC_MREQN -#define CONFIG_ALL_MULTICAST -#define CONFIG_UNNUM_MULTICAST - -/* -Link: sysdep/linux -Link: sysdep/unix - */ diff --git a/sysdep/cf/linux-22.h b/sysdep/cf/linux-22.h deleted file mode 100644 index 51b339d1..00000000 --- a/sysdep/cf/linux-22.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Configuration for Linux 2.2 based systems - * - * (c) 1998--1999 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#define CONFIG_AUTO_ROUTES -#define CONFIG_SELF_CONSCIOUS -#define CONFIG_MULTIPLE_TABLES -#define CONFIG_ALL_TABLES_AT_ONCE -#define CONFIG_MC_PROPER_SRC - -#undef CONFIG_SKIP_MC_BIND - -#define CONFIG_LINUX_MC_MREQN -#define CONFIG_UNIX_DONTROUTE - -#define CONFIG_RESTRICTED_PRIVILEGES - -/* -Link: sysdep/linux/netlink -Link: sysdep/linux -Link: sysdep/unix - */ diff --git a/sysdep/cf/linux-v6.h b/sysdep/cf/linux-v6.h index 467d7728..09f60377 100644 --- a/sysdep/cf/linux-v6.h +++ b/sysdep/cf/linux-v6.h @@ -1,5 +1,5 @@ /* - * Configuration for Linux 2.2 based systems running IPv6 + * Configuration for Linux based systems running IPv6 * * (c) 1998--1999 Martin Mares * @@ -9,20 +9,13 @@ #define IPV6 #define CONFIG_AUTO_ROUTES -#define CONFIG_ALL_MULTICAST #define CONFIG_SELF_CONSCIOUS - -/* - * Netlink supports multiple tables, but kernel IPv6 code doesn't, so we - * treat it as a multiple table system with number of tables set to 1. - */ #define CONFIG_MULTIPLE_TABLES #define CONFIG_ALL_TABLES_AT_ONCE #define CONFIG_RESTRICTED_PRIVILEGES /* -Link: sysdep/linux/netlink Link: sysdep/linux Link: sysdep/unix */ diff --git a/sysdep/linux/Modules b/sysdep/linux/Modules index 09f4a470..940660b6 100644 --- a/sysdep/linux/Modules +++ b/sysdep/linux/Modules @@ -1,6 +1,5 @@ -#ifdef CONFIG_LINUX_SCAN -krt-scan.c -krt-scan.h -#endif +krt-sys.h +netlink.c +netlink.Y sysio.h syspriv.h diff --git a/sysdep/linux/krt-scan.c b/sysdep/linux/krt-scan.c deleted file mode 100644 index 8591607e..00000000 --- a/sysdep/linux/krt-scan.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * BIRD -- Linux Routing Table Scanning - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#include -#include -#include -#include -#include - -#undef LOCAL_DEBUG - -#include "nest/bird.h" -#include "nest/route.h" -#include "nest/protocol.h" -#include "nest/iface.h" -#include "lib/timer.h" -#include "lib/unix.h" -#include "lib/krt.h" -#include "lib/string.h" - -static int krt_scan_fd = -1; - -struct iface * -krt_temp_iface(struct krt_proto *p, char *name) -{ - struct iface *i; - - WALK_LIST(i, p->scan.temp_ifs) - if (!strcmp(i->name, name)) - return i; - i = mb_allocz(p->p.pool, sizeof(struct iface)); - strcpy(i->name, name); - add_tail(&p->scan.temp_ifs, &i->n); - return i; -} - -static void -krt_parse_entry(byte *ent, struct krt_proto *p) -{ - u32 dest0, gw0, mask0; - ip_addr dest, gw, mask; - unsigned int flags; - int masklen; - net *net; - byte *iface = ent; - rte *e; - - if (sscanf(ent, "%*s\t%x\t%x\t%x\t%*d\t%*d\t%*d\t%x\t", &dest0, &gw0, &flags, &mask0) != 4) - { - log(L_ERR "krt read: unable to parse `%s'", ent); - return; - } - while (*ent != '\t') - ent++; - *ent = 0; - - dest = ipa_from_u32(dest0); - ipa_ntoh(dest); - gw = ipa_from_u32(gw0); - ipa_ntoh(gw); - mask = ipa_from_u32(mask0); - ipa_ntoh(mask); - if ((masklen = ipa_mklen(mask)) < 0) - { - log(L_ERR "krt read: invalid netmask %08x", mask0); - return; - } - DBG("Got %I/%d via %I flags %x\n", dest, masklen, gw, flags); - - if (!(flags & RTF_UP)) - { - DBG("Down.\n"); - return; - } - if (flags & RTF_HOST) - masklen = 32; - if (flags & (RTF_DYNAMIC | RTF_MODIFIED)) /* Redirect route */ - { - log(L_WARN "krt: Ignoring redirect to %I/%d via %I", dest, masklen, gw); - return; - } - - net = net_get(p->p.table, dest, masklen); - - rta a = { - .proto = &p->p, - .source = RTS_INHERIT, - .scope = SCOPE_UNIVERSE, - .cast = RTC_UNICAST - }; - - if (flags & RTF_GATEWAY) - { - neighbor *ng = neigh_find(&p->p, &gw, 0); - if (ng && ng->scope) - a.iface = ng->iface; - else - { - log(L_WARN "Kernel told us to use non-neighbor %I for %I/%d", gw, net->n.prefix, net->n.pxlen); - return; - } - a.dest = RTD_ROUTER; - a.gw = gw; - } - else if (flags & RTF_REJECT) - { - a.dest = RTD_UNREACHABLE; - a.gw = IPA_NONE; - } - else if (isalpha(iface[0])) - { - a.dest = RTD_DEVICE; - a.gw = IPA_NONE; - a.iface = krt_temp_iface(p, iface); - } - else - { - log(L_WARN "Kernel reporting unknown route type to %I/%d", net->n.prefix, net->n.pxlen); - return; - } - - e = rte_get_temp(&a); - e->net = net; - e->u.krt.src = KRT_SRC_UNKNOWN; - krt_got_route(p, e); -} - -void -krt_scan_fire(struct krt_proto *p) -{ - byte buf[32768]; - int l, seen_hdr; - - if (krt_scan_fd < 0) - { - krt_scan_fd = open("/proc/net/route", O_RDONLY); - if (krt_scan_fd < 0) - die("/proc/net/route: %m"); - } - else if (lseek(krt_scan_fd, 0, SEEK_SET) < 0) - { - log(L_ERR "krt seek: %m"); - return; - } - seen_hdr = 0; - while ((l = read(krt_scan_fd, buf, sizeof(buf))) > 0) - { - byte *z = buf; - if (l & 127) - { - log(L_ERR "krt read: misaligned entry: l=%d", l); - return; - } - while (l >= 128) - { - if (seen_hdr++) - krt_parse_entry(z, p); - z += 128; - l -= 128; - } - } - if (l < 0) - { - log(L_ERR "krt read: %m"); - return; - } - DBG("KRT scan done, seen %d lines\n", seen_hdr); -} - -void -krt_scan_construct(struct krt_config *c) -{ -} - -void -krt_scan_preconfig(struct config *c) -{ -} - -void -krt_scan_postconfig(struct krt_config *c) -{ -} - -void -krt_scan_start(struct krt_proto *x, int first) -{ - init_list(&x->scan.temp_ifs); -} - -void -krt_scan_shutdown(struct krt_proto *x, int last) -{ -} diff --git a/sysdep/linux/krt-scan.h b/sysdep/linux/krt-scan.h deleted file mode 100644 index 6c7e440f..00000000 --- a/sysdep/linux/krt-scan.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * BIRD -- Linux Kernel Route Syncer -- Scanning - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SCAN_H_ -#define _BIRD_KRT_SCAN_H_ - -struct krt_scan_params { -}; - -struct krt_scan_status { - list temp_ifs; /* Temporary interfaces */ -}; - -static inline int krt_scan_params_same(struct krt_scan_params *o, struct krt_scan_params *n) { return 1; } - -#endif diff --git a/sysdep/linux/netlink/Modules b/sysdep/linux/netlink/Modules deleted file mode 100644 index c26f7f72..00000000 --- a/sysdep/linux/netlink/Modules +++ /dev/null @@ -1,5 +0,0 @@ -krt-iface.h -krt-set.h -krt-scan.h -netlink.c -netlink.Y diff --git a/sysdep/linux/netlink/krt-iface.h b/sysdep/linux/netlink/krt-iface.h deleted file mode 100644 index 8cfe1073..00000000 --- a/sysdep/linux/netlink/krt-iface.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * BIRD -- Unix Kernel Netlink Interface Syncer -- Dummy Include File - * - * (c) 1998--1999 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_IFACE_H_ -#define _BIRD_KRT_IFACE_H_ - -/* - * We don't have split iface/scan/set parts. See krt-scan.h. - */ - -struct kif_params { -}; - -struct kif_status { -}; - - -static inline void kif_sys_init(struct kif_proto *p UNUSED) { } -static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } - -static inline void kif_sys_preconfig(struct config *c UNUSED) { } -static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } -static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } -static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } - -static inline void kif_sys_io_init(void) { } - -#endif diff --git a/sysdep/linux/netlink/krt-scan.h b/sysdep/linux/netlink/krt-scan.h deleted file mode 100644 index 302b6093..00000000 --- a/sysdep/linux/netlink/krt-scan.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * BIRD -- Linux Kernel Netlink Route Syncer -- Scanning - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SCAN_H_ -#define _BIRD_KRT_SCAN_H_ - -/* - * We don't have split iface/scan/set for Netlink. All options - * and run-time parameters are declared here instead of splitting - * to krt-set.h, krt-iface.h and this file. - */ - -#define NL_NUM_TABLES 256 - -struct krt_params { - int table_id; /* Kernel table ID we sync with */ -}; - -struct krt_status { -}; - - -static inline void krt_sys_init(struct krt_proto *p UNUSED) { } - -#endif diff --git a/sysdep/linux/netlink/krt-set.h b/sysdep/linux/netlink/krt-set.h deleted file mode 100644 index 4a08217b..00000000 --- a/sysdep/linux/netlink/krt-set.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * BIRD -- Unix Kernel Netlink Route Syncer -- Dummy Include File - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SET_H_ -#define _BIRD_KRT_SET_H_ - -/* - * We don't have split iface/scan/set parts. See krt-scan.h. - */ - -struct krt_set_params { -}; - -struct krt_set_status { -}; - -static inline void krt_set_construct(struct krt_config *c UNUSED) { }; -static inline void krt_set_start(struct krt_proto *p UNUSED, int first UNUSED) { }; -static inline void krt_set_shutdown(struct krt_proto *p UNUSED, int last UNUSED) { }; -static inline int krt_set_params_same(struct krt_set_params *o UNUSED, struct krt_set_params *n UNUSED) { return 1; } -static inline void krt_set_copy_params(struct krt_set_params *d UNUSED, struct krt_set_params *s UNUSED) { } - -#endif diff --git a/sysdep/linux/netlink/netlink.Y b/sysdep/linux/netlink/netlink.Y deleted file mode 100644 index 51689ff9..00000000 --- a/sysdep/linux/netlink/netlink.Y +++ /dev/null @@ -1,32 +0,0 @@ -/* - * BIRD -- Linux Netlink Configuration - * - * (c) 1999--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -CF_HDR - -CF_DECLS - -CF_KEYWORDS(ASYNC, KERNEL, TABLE, KRT_PREFSRC, KRT_REALM) - -CF_GRAMMAR - -CF_ADDTO(kern_proto, kern_proto nl_item ';') - -nl_item: - KERNEL TABLE expr { - if ($3 <= 0 || $3 >= NL_NUM_TABLES) - cf_error("Kernel routing table number out of range"); - THIS_KRT->sys.table_id = $3; - } - ; - -CF_ADDTO(dynamic_attr, KRT_PREFSRC { $$ = f_new_dynamic_attr(EAF_TYPE_IP_ADDRESS, T_IP, EA_KRT_PREFSRC); }) -CF_ADDTO(dynamic_attr, KRT_REALM { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_KRT_REALM); }) - -CF_CODE - -CF_END diff --git a/sysdep/linux/netlink/netlink.c b/sysdep/linux/netlink/netlink.c deleted file mode 100644 index e3faf043..00000000 --- a/sysdep/linux/netlink/netlink.c +++ /dev/null @@ -1,1149 +0,0 @@ -/* - * BIRD -- Linux Netlink Interface - * - * (c) 1999--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#include -#include -#include -#include -#include - -#undef LOCAL_DEBUG - -#include "nest/bird.h" -#include "nest/route.h" -#include "nest/protocol.h" -#include "nest/iface.h" -#include "lib/alloca.h" -#include "lib/timer.h" -#include "lib/unix.h" -#include "lib/krt.h" -#include "lib/socket.h" -#include "lib/string.h" -#include "conf/conf.h" - -#include -#include -#include -#include - -#ifndef MSG_TRUNC /* Hack: Several versions of glibc miss this one :( */ -#define MSG_TRUNC 0x20 -#endif - -#ifndef IFF_LOWER_UP -#define IFF_LOWER_UP 0x10000 -#endif - -/* - * Synchronous Netlink interface - */ - -struct nl_sock -{ - int fd; - u32 seq; - byte *rx_buffer; /* Receive buffer */ - struct nlmsghdr *last_hdr; /* Recently received packet */ - unsigned int last_size; -}; - -#define NL_RX_SIZE 8192 - -static struct nl_sock nl_scan = {.fd = -1}; /* Netlink socket for synchronous scan */ -static struct nl_sock nl_req = {.fd = -1}; /* Netlink socket for requests */ - -static void -nl_open_sock(struct nl_sock *nl) -{ - if (nl->fd < 0) - { - nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); - if (nl->fd < 0) - die("Unable to open rtnetlink socket: %m"); - nl->seq = now; - nl->rx_buffer = xmalloc(NL_RX_SIZE); - nl->last_hdr = NULL; - nl->last_size = 0; - } -} - -static void -nl_open(void) -{ - nl_open_sock(&nl_scan); - nl_open_sock(&nl_req); -} - -static void -nl_send(struct nl_sock *nl, struct nlmsghdr *nh) -{ - struct sockaddr_nl sa; - - memset(&sa, 0, sizeof(sa)); - sa.nl_family = AF_NETLINK; - nh->nlmsg_pid = 0; - nh->nlmsg_seq = ++(nl->seq); - if (sendto(nl->fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) - die("rtnetlink sendto: %m"); - nl->last_hdr = NULL; -} - -static void -nl_request_dump(int cmd) -{ - struct { - struct nlmsghdr nh; - struct rtgenmsg g; - } req; - req.nh.nlmsg_type = cmd; - req.nh.nlmsg_len = sizeof(req); - req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; - /* Is it important which PF_* is used for link-level interface scan? - It seems that some information is available only when PF_INET is used. */ - req.g.rtgen_family = (cmd == RTM_GETLINK) ? PF_INET : BIRD_PF; - nl_send(&nl_scan, &req.nh); -} - -static struct nlmsghdr * -nl_get_reply(struct nl_sock *nl) -{ - for(;;) - { - if (!nl->last_hdr) - { - struct iovec iov = { nl->rx_buffer, NL_RX_SIZE }; - struct sockaddr_nl sa; - struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; - int x = recvmsg(nl->fd, &m, 0); - if (x < 0) - die("nl_get_reply: %m"); - if (sa.nl_pid) /* It isn't from the kernel */ - { - DBG("Non-kernel packet\n"); - continue; - } - nl->last_size = x; - nl->last_hdr = (void *) nl->rx_buffer; - if (m.msg_flags & MSG_TRUNC) - bug("nl_get_reply: got truncated reply which should be impossible"); - } - if (NLMSG_OK(nl->last_hdr, nl->last_size)) - { - struct nlmsghdr *h = nl->last_hdr; - nl->last_hdr = NLMSG_NEXT(h, nl->last_size); - if (h->nlmsg_seq != nl->seq) - { - log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)", - h->nlmsg_seq, nl->seq); - continue; - } - return h; - } - if (nl->last_size) - log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl->last_size); - nl->last_hdr = NULL; - } -} - -static struct rate_limit rl_netlink_err; - -static int -nl_error(struct nlmsghdr *h) -{ - struct nlmsgerr *e; - int ec; - - if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) - { - log(L_WARN "Netlink: Truncated error message received"); - return ENOBUFS; - } - e = (struct nlmsgerr *) NLMSG_DATA(h); - ec = -e->error; - if (ec) - log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec)); - return ec; -} - -static struct nlmsghdr * -nl_get_scan(void) -{ - struct nlmsghdr *h = nl_get_reply(&nl_scan); - - if (h->nlmsg_type == NLMSG_DONE) - return NULL; - if (h->nlmsg_type == NLMSG_ERROR) - { - nl_error(h); - return NULL; - } - return h; -} - -static int -nl_exchange(struct nlmsghdr *pkt) -{ - struct nlmsghdr *h; - - nl_send(&nl_req, pkt); - for(;;) - { - h = nl_get_reply(&nl_req); - if (h->nlmsg_type == NLMSG_ERROR) - break; - log(L_WARN "nl_exchange: Unexpected reply received"); - } - return nl_error(h) ? -1 : 0; -} - -/* - * Netlink attributes - */ - -static int nl_attr_len; - -static void * -nl_checkin(struct nlmsghdr *h, int lsize) -{ - nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize); - if (nl_attr_len < 0) - { - log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len); - return NULL; - } - return NLMSG_DATA(h); -} - -static int -nl_parse_attrs(struct rtattr *a, struct rtattr **k, int ksize) -{ - int max = ksize / sizeof(struct rtattr *); - bzero(k, ksize); - while (RTA_OK(a, nl_attr_len)) - { - if (a->rta_type < max) - k[a->rta_type] = a; - a = RTA_NEXT(a, nl_attr_len); - } - if (nl_attr_len) - { - log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len); - return 0; - } - else - return 1; -} - -void -nl_add_attr(struct nlmsghdr *h, unsigned bufsize, unsigned code, - void *data, unsigned dlen) -{ - unsigned len = RTA_LENGTH(dlen); - unsigned pos = NLMSG_ALIGN(h->nlmsg_len); - struct rtattr *a; - - if (pos + len > bufsize) - bug("nl_add_attr: packet buffer overflow"); - a = (struct rtattr *)((char *)h + pos); - a->rta_type = code; - a->rta_len = len; - h->nlmsg_len = pos + len; - memcpy(RTA_DATA(a), data, dlen); -} - -static inline void -nl_add_attr_u32(struct nlmsghdr *h, unsigned bufsize, int code, u32 data) -{ - nl_add_attr(h, bufsize, code, &data, 4); -} - -static inline void -nl_add_attr_ipa(struct nlmsghdr *h, unsigned bufsize, int code, ip_addr ipa) -{ - ipa_hton(ipa); - nl_add_attr(h, bufsize, code, &ipa, sizeof(ipa)); -} - -#define RTNH_SIZE (sizeof(struct rtnexthop) + sizeof(struct rtattr) + sizeof(ip_addr)) - -static inline void -add_mpnexthop(char *buf, ip_addr ipa, unsigned iface, unsigned char weight) -{ - struct rtnexthop *nh = (void *) buf; - struct rtattr *rt = (void *) (buf + sizeof(*nh)); - nh->rtnh_len = RTNH_SIZE; - nh->rtnh_flags = 0; - nh->rtnh_hops = weight; - nh->rtnh_ifindex = iface; - rt->rta_len = sizeof(*rt) + sizeof(ipa); - rt->rta_type = RTA_GATEWAY; - ipa_hton(ipa); - memcpy(buf + sizeof(*nh) + sizeof(*rt), &ipa, sizeof(ipa)); -} - - -static void -nl_add_multipath(struct nlmsghdr *h, unsigned bufsize, struct mpnh *nh) -{ - unsigned len = sizeof(struct rtattr); - unsigned pos = NLMSG_ALIGN(h->nlmsg_len); - char *buf = (char *)h + pos; - struct rtattr *rt = (void *) buf; - buf += len; - - for (; nh; nh = nh->next) - { - len += RTNH_SIZE; - if (pos + len > bufsize) - bug("nl_add_multipath: packet buffer overflow"); - - add_mpnexthop(buf, nh->gw, nh->iface->index, nh->weight); - buf += RTNH_SIZE; - } - - rt->rta_type = RTA_MULTIPATH; - rt->rta_len = len; - h->nlmsg_len = pos + len; -} - - -static struct mpnh * -nl_parse_multipath(struct krt_proto *p, struct rtattr *ra) -{ - /* Temporary buffer for multicast nexthops */ - static struct mpnh *nh_buffer; - static int nh_buf_size; /* in number of structures */ - static int nh_buf_used; - - struct rtattr *a[RTA_CACHEINFO+1]; - struct rtnexthop *nh = RTA_DATA(ra); - struct mpnh *rv, *first, **last; - int len = RTA_PAYLOAD(ra); - - first = NULL; - last = &first; - nh_buf_used = 0; - - while (len) - { - /* Use RTNH_OK(nh,len) ?? */ - if ((len < sizeof(*nh)) || (len < nh->rtnh_len)) - return NULL; - - if (nh_buf_used == nh_buf_size) - { - nh_buf_size = nh_buf_size ? (nh_buf_size * 2) : 4; - nh_buffer = xrealloc(nh_buffer, nh_buf_size * sizeof(struct mpnh)); - } - *last = rv = nh_buffer + nh_buf_used++; - rv->next = NULL; - last = &(rv->next); - - rv->weight = nh->rtnh_hops; - rv->iface = if_find_by_index(nh->rtnh_ifindex); - if (!rv->iface) - return NULL; - - /* Nonexistent RTNH_PAYLOAD ?? */ - nl_attr_len = nh->rtnh_len - RTNH_LENGTH(0); - nl_parse_attrs(RTNH_DATA(nh), a, sizeof(a)); - if (a[RTA_GATEWAY]) - { - if (RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)) - return NULL; - - memcpy(&rv->gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ip_addr)); - ipa_ntoh(rv->gw); - - neighbor *ng = neigh_find2(&p->p, &rv->gw, rv->iface, - (nh->rtnh_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0); - if (!ng || (ng->scope == SCOPE_HOST)) - return NULL; - } - else - return NULL; - - len -= NLMSG_ALIGN(nh->rtnh_len); - nh = RTNH_NEXT(nh); - } - - return first; -} - - -/* - * Scanning of interfaces - */ - -static void -nl_parse_link(struct nlmsghdr *h, int scan) -{ - struct ifinfomsg *i; - struct rtattr *a[IFLA_WIRELESS+1]; - int new = h->nlmsg_type == RTM_NEWLINK; - struct iface f = {}; - struct iface *ifi; - char *name; - u32 mtu; - unsigned int fl; - - if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), a, sizeof(a))) - return; - if (!a[IFLA_IFNAME] || RTA_PAYLOAD(a[IFLA_IFNAME]) < 2 || - !a[IFLA_MTU] || RTA_PAYLOAD(a[IFLA_MTU]) != 4) - { - if (scan || !a[IFLA_WIRELESS]) - log(L_ERR "nl_parse_link: Malformed message received"); - return; - } - name = RTA_DATA(a[IFLA_IFNAME]); - memcpy(&mtu, RTA_DATA(a[IFLA_MTU]), sizeof(u32)); - - ifi = if_find_by_index(i->ifi_index); - if (!new) - { - DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name); - if (!ifi) - return; - - if_delete(ifi); - } - else - { - DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags); - if (ifi && strncmp(ifi->name, name, sizeof(ifi->name)-1)) - if_delete(ifi); - - strncpy(f.name, name, sizeof(f.name)-1); - f.index = i->ifi_index; - f.mtu = mtu; - - fl = i->ifi_flags; - if (fl & IFF_UP) - f.flags |= IF_ADMIN_UP; - if (fl & IFF_LOWER_UP) - f.flags |= IF_LINK_UP; - 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 */ - if_update(&f); - } -} - -static void -nl_parse_addr(struct nlmsghdr *h) -{ - struct ifaddrmsg *i; - struct rtattr *a[IFA_ANYCAST+1]; - int new = h->nlmsg_type == RTM_NEWADDR; - struct ifa ifa; - struct iface *ifi; - int scope; - - if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFA_RTA(i), a, sizeof(a))) - return; - if (i->ifa_family != BIRD_AF) - return; - if (!a[IFA_ADDRESS] || RTA_PAYLOAD(a[IFA_ADDRESS]) != sizeof(ip_addr) -#ifdef IPV6 - || a[IFA_LOCAL] && RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr) -#else - || !a[IFA_LOCAL] || RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr) - || (a[IFA_BROADCAST] && RTA_PAYLOAD(a[IFA_BROADCAST]) != sizeof(ip_addr)) -#endif - ) - { - log(L_ERR "nl_parse_addr: Malformed message received"); - return; - } - - ifi = if_find_by_index(i->ifa_index); - if (!ifi) - { - log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index); - return; - } - - bzero(&ifa, sizeof(ifa)); - ifa.iface = ifi; - if (i->ifa_flags & IFA_F_SECONDARY) - ifa.flags |= IA_SECONDARY; - - /* IFA_LOCAL can be unset for IPv6 interfaces */ - memcpy(&ifa.ip, RTA_DATA(a[IFA_LOCAL] ? : a[IFA_ADDRESS]), sizeof(ifa.ip)); - ipa_ntoh(ifa.ip); - ifa.pxlen = i->ifa_prefixlen; - if (i->ifa_prefixlen > BITS_PER_IP_ADDRESS) - { - log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen); - new = 0; - } - if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS) - { - ip_addr addr; - memcpy(&addr, RTA_DATA(a[IFA_ADDRESS]), sizeof(addr)); - ipa_ntoh(addr); - ifa.prefix = ifa.brd = addr; - - /* It is either a host address or a peer address */ - if (ipa_equal(ifa.ip, addr)) - ifa.flags |= IA_HOST; - else - { - ifa.flags |= IA_PEER; - ifa.opposite = addr; - } - } - else - { - ip_addr netmask = ipa_mkmask(ifa.pxlen); - ifa.prefix = ipa_and(ifa.ip, netmask); - ifa.brd = ipa_or(ifa.ip, ipa_not(netmask)); - if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 1) - ifa.opposite = ipa_opposite_m1(ifa.ip); - -#ifndef IPV6 - if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 2) - ifa.opposite = ipa_opposite_m2(ifa.ip); - - if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST]) - { - ip_addr xbrd; - memcpy(&xbrd, RTA_DATA(a[IFA_BROADCAST]), sizeof(xbrd)); - ipa_ntoh(xbrd); - if (ipa_equal(xbrd, ifa.prefix) || ipa_equal(xbrd, ifa.brd)) - ifa.brd = xbrd; - else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */ - log(L_ERR "KIF: Invalid broadcast address %I for %s", xbrd, ifi->name); - } -#endif - } - - scope = ipa_classify(ifa.ip); - if (scope < 0) - { - log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name); - return; - } - ifa.scope = scope & IADDR_SCOPE_MASK; - - DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %I/%d, brd %I, opp %I\n", - ifi->index, ifi->name, - new ? "added" : "removed", - ifa.ip, ifa.flags, ifa.prefix, ifa.pxlen, ifa.brd, ifa.opposite); - if (new) - ifa_update(&ifa); - else - ifa_delete(&ifa); -} - -void -kif_do_scan(struct kif_proto *p UNUSED) -{ - struct nlmsghdr *h; - - if_start_update(); - - nl_request_dump(RTM_GETLINK); - while (h = nl_get_scan()) - if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK) - nl_parse_link(h, 1); - else - log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type); - - nl_request_dump(RTM_GETADDR); - while (h = nl_get_scan()) - if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) - nl_parse_addr(h); - else - log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type); - - if_end_update(); -} - -/* - * Routes - */ - -static struct krt_proto *nl_table_map[NL_NUM_TABLES]; - -int -krt_capable(rte *e) -{ - rta *a = e->attrs; - - if (a->cast != RTC_UNICAST) - return 0; - - switch (a->dest) - { - case RTD_ROUTER: - case RTD_DEVICE: - if (a->iface == NULL) - return 0; - case RTD_BLACKHOLE: - case RTD_UNREACHABLE: - case RTD_PROHIBIT: - case RTD_MULTIPATH: - break; - default: - return 0; - } - return 1; -} - -static inline int -nh_bufsize(struct mpnh *nh) -{ - int rv = 0; - for (; nh != NULL; nh = nh->next) - rv += RTNH_SIZE; - return rv; -} - -static int -nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) -{ - eattr *ea; - net *net = e->net; - rta *a = e->attrs; - struct { - struct nlmsghdr h; - struct rtmsg r; - char buf[128 + nh_bufsize(a->nexthops)]; - } r; - - DBG("nl_send_route(%I/%d,new=%d)\n", net->n.prefix, net->n.pxlen, new); - - bzero(&r.h, sizeof(r.h)); - bzero(&r.r, sizeof(r.r)); - r.h.nlmsg_type = new ? RTM_NEWROUTE : RTM_DELROUTE; - r.h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); - r.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | (new ? NLM_F_CREATE|NLM_F_EXCL : 0); - - r.r.rtm_family = BIRD_AF; - r.r.rtm_dst_len = net->n.pxlen; - r.r.rtm_tos = 0; - r.r.rtm_table = KRT_CF->sys.table_id; - r.r.rtm_protocol = RTPROT_BIRD; - r.r.rtm_scope = RT_SCOPE_UNIVERSE; - nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix); - - u32 metric = 0; - if (new && e->attrs->source == RTS_INHERIT) - metric = e->u.krt.metric; - if (ea = ea_find(eattrs, EA_KRT_METRIC)) - metric = ea->u.data; - if (metric != 0) - nl_add_attr_u32(&r.h, sizeof(r), RTA_PRIORITY, metric); - - if (ea = ea_find(eattrs, EA_KRT_PREFSRC)) - nl_add_attr_ipa(&r.h, sizeof(r), RTA_PREFSRC, *(ip_addr *)ea->u.ptr->data); - - if (ea = ea_find(eattrs, EA_KRT_REALM)) - nl_add_attr_u32(&r.h, sizeof(r), RTA_FLOW, ea->u.data); - - /* a->iface != NULL checked in krt_capable() for router and device routes */ - - switch (a->dest) - { - case RTD_ROUTER: - r.r.rtm_type = RTN_UNICAST; - nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index); - nl_add_attr_ipa(&r.h, sizeof(r), RTA_GATEWAY, a->gw); - break; - case RTD_DEVICE: - r.r.rtm_type = RTN_UNICAST; - nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index); - break; - case RTD_BLACKHOLE: - r.r.rtm_type = RTN_BLACKHOLE; - break; - case RTD_UNREACHABLE: - r.r.rtm_type = RTN_UNREACHABLE; - break; - case RTD_PROHIBIT: - r.r.rtm_type = RTN_PROHIBIT; - break; - case RTD_MULTIPATH: - r.r.rtm_type = RTN_UNICAST; - nl_add_multipath(&r.h, sizeof(r), a->nexthops); - break; - default: - bug("krt_capable inconsistent with nl_send_route"); - } - - return nl_exchange(&r.h); -} - -void -krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) -{ - int err = 0; - - /* - * NULL for eattr of the old route is a little hack, but we don't - * get proper eattrs for old in rt_notify() anyway. NULL means no - * extended route attributes and therefore matches if the kernel - * route has any of them. - */ - - if (old) - nl_send_route(p, old, NULL, 0); - - if (new) - err = nl_send_route(p, new, eattrs, 1); - - if (err < 0) - n->n.flags |= KRF_SYNC_ERROR; - else - n->n.flags &= ~KRF_SYNC_ERROR; -} - - -#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0) - -static void -nl_parse_route(struct nlmsghdr *h, int scan) -{ - struct krt_proto *p; - struct rtmsg *i; - struct rtattr *a[RTA_CACHEINFO+1]; - int new = h->nlmsg_type == RTM_NEWROUTE; - - ip_addr dst = IPA_NONE; - u32 oif = ~0; - int src; - - if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(RTM_RTA(i), a, sizeof(a))) - return; - if (i->rtm_family != BIRD_AF) - return; - if ((a[RTA_DST] && RTA_PAYLOAD(a[RTA_DST]) != sizeof(ip_addr)) || -#ifdef IPV6 - (a[RTA_IIF] && RTA_PAYLOAD(a[RTA_IIF]) != 4) || -#endif - (a[RTA_OIF] && RTA_PAYLOAD(a[RTA_OIF]) != 4) || - (a[RTA_GATEWAY] && RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)) || - (a[RTA_PRIORITY] && RTA_PAYLOAD(a[RTA_PRIORITY]) != 4) || - (a[RTA_PREFSRC] && RTA_PAYLOAD(a[RTA_PREFSRC]) != sizeof(ip_addr)) || - (a[RTA_FLOW] && RTA_PAYLOAD(a[RTA_OIF]) != 4)) - { - log(L_ERR "KRT: Malformed message received"); - return; - } - - if (a[RTA_DST]) - { - memcpy(&dst, RTA_DATA(a[RTA_DST]), sizeof(dst)); - ipa_ntoh(dst); - } - - if (a[RTA_OIF]) - memcpy(&oif, RTA_DATA(a[RTA_OIF]), sizeof(oif)); - - p = nl_table_map[i->rtm_table]; /* Do we know this table? */ - DBG("KRT: Got %I/%d, type=%d, oif=%d, table=%d, prid=%d, proto=%s\n", dst, i->rtm_dst_len, i->rtm_type, oif, i->rtm_table, i->rtm_protocol, p ? p->p.name : "(none)"); - if (!p) - SKIP("unknown table %d\n", i->rtm_table); - - -#ifdef IPV6 - if (a[RTA_IIF]) - SKIP("IIF set\n"); -#else - if (i->rtm_tos != 0) /* We don't support TOS */ - SKIP("TOS %02x\n", i->rtm_tos); -#endif - - if (scan && !new) - SKIP("RTM_DELROUTE in scan\n"); - - int c = ipa_classify_net(dst); - if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK)) - SKIP("strange class/scope\n"); - - // ignore rtm_scope, it is not a real scope - // if (i->rtm_scope != RT_SCOPE_UNIVERSE) - // SKIP("scope %u\n", i->rtm_scope); - - switch (i->rtm_protocol) - { - case RTPROT_UNSPEC: - SKIP("proto unspec\n"); - - case RTPROT_REDIRECT: - src = KRT_SRC_REDIRECT; - break; - - case RTPROT_KERNEL: - src = KRT_SRC_KERNEL; - return; - - case RTPROT_BIRD: - if (!scan) - SKIP("echo\n"); - src = KRT_SRC_BIRD; - break; - - case RTPROT_BOOT: - default: - src = KRT_SRC_ALIEN; - } - - net *net = net_get(p->p.table, dst, i->rtm_dst_len); - - rta ra = { - .proto = &p->p, - .source = RTS_INHERIT, - .scope = SCOPE_UNIVERSE, - .cast = RTC_UNICAST - }; - - switch (i->rtm_type) - { - case RTN_UNICAST: - - if (a[RTA_MULTIPATH]) - { - ra.dest = RTD_MULTIPATH; - ra.nexthops = nl_parse_multipath(p, a[RTA_MULTIPATH]); - if (!ra.nexthops) - { - log(L_ERR "KRT: Received strange multipath route %I/%d", - net->n.prefix, net->n.pxlen); - return; - } - - break; - } - - ra.iface = if_find_by_index(oif); - if (!ra.iface) - { - log(L_ERR "KRT: Received route %I/%d with unknown ifindex %u", - net->n.prefix, net->n.pxlen, oif); - return; - } - - if (a[RTA_GATEWAY]) - { - neighbor *ng; - ra.dest = RTD_ROUTER; - memcpy(&ra.gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ra.gw)); - ipa_ntoh(ra.gw); - - /* Silently skip strange 6to4 routes */ - if (ipa_in_net(ra.gw, IPA_NONE, 96)) - return; - - ng = neigh_find2(&p->p, &ra.gw, ra.iface, - (i->rtm_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0); - if (!ng || (ng->scope == SCOPE_HOST)) - { - log(L_ERR "KRT: Received route %I/%d with strange next-hop %I", - net->n.prefix, net->n.pxlen, ra.gw); - return; - } - } - else - { - ra.dest = RTD_DEVICE; - - /* - * In Linux IPv6, 'native' device routes have proto - * RTPROT_BOOT and not RTPROT_KERNEL (which they have in - * IPv4 and which is expected). We cannot distinguish - * 'native' and user defined device routes, so we ignore all - * such device routes and for consistency, we have the same - * behavior in IPv4. Anyway, users should use RTPROT_STATIC - * for their 'alien' routes. - */ - - if (i->rtm_protocol == RTPROT_BOOT) - src = KRT_SRC_KERNEL; - } - - break; - case RTN_BLACKHOLE: - ra.dest = RTD_BLACKHOLE; - break; - case RTN_UNREACHABLE: - ra.dest = RTD_UNREACHABLE; - break; - case RTN_PROHIBIT: - ra.dest = RTD_PROHIBIT; - break; - /* FIXME: What about RTN_THROW? */ - default: - SKIP("type %d\n", i->rtm_type); - return; - } - - rte *e = rte_get_temp(&ra); - e->net = net; - e->u.krt.src = src; - e->u.krt.proto = i->rtm_protocol; - e->u.krt.type = i->rtm_type; - - if (a[RTA_PRIORITY]) - memcpy(&e->u.krt.metric, RTA_DATA(a[RTA_PRIORITY]), sizeof(e->u.krt.metric)); - else - e->u.krt.metric = 0; - - if (a[RTA_PREFSRC]) - { - ip_addr ps; - memcpy(&ps, RTA_DATA(a[RTA_PREFSRC]), sizeof(ps)); - ipa_ntoh(ps); - - ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr)); - ea->next = ra.eattrs; - ra.eattrs = ea; - ea->flags = EALF_SORTED; - ea->count = 1; - ea->attrs[0].id = EA_KRT_PREFSRC; - ea->attrs[0].flags = 0; - ea->attrs[0].type = EAF_TYPE_IP_ADDRESS; - ea->attrs[0].u.ptr = alloca(sizeof(struct adata) + sizeof(ps)); - ea->attrs[0].u.ptr->length = sizeof(ps); - memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps)); - } - - if (a[RTA_FLOW]) - { - ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr)); - ea->next = ra.eattrs; - ra.eattrs = ea; - ea->flags = EALF_SORTED; - ea->count = 1; - ea->attrs[0].id = EA_KRT_REALM; - ea->attrs[0].flags = 0; - ea->attrs[0].type = EAF_TYPE_INT; - memcpy(&ea->attrs[0].u.data, RTA_DATA(a[RTA_FLOW]), 4); - } - - if (scan) - krt_got_route(p, e); - else - krt_got_route_async(p, e, new); -} - -void -krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ -{ - struct nlmsghdr *h; - - nl_request_dump(RTM_GETROUTE); - while (h = nl_get_scan()) - if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE) - nl_parse_route(h, 1); - else - log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type); -} - -/* - * Asynchronous Netlink interface - */ - -static sock *nl_async_sk; /* BIRD socket for asynchronous notifications */ -static byte *nl_async_rx_buffer; /* Receive buffer */ - -static void -nl_async_msg(struct nlmsghdr *h) -{ - switch (h->nlmsg_type) - { - case RTM_NEWROUTE: - case RTM_DELROUTE: - DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type); - nl_parse_route(h, 0); - break; - case RTM_NEWLINK: - case RTM_DELLINK: - DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type); - nl_parse_link(h, 0); - break; - case RTM_NEWADDR: - case RTM_DELADDR: - DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type); - nl_parse_addr(h); - break; - default: - DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type); - } -} - -static int -nl_async_hook(sock *sk, int size UNUSED) -{ - struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE }; - struct sockaddr_nl sa; - struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; - struct nlmsghdr *h; - int x; - unsigned int len; - - x = recvmsg(sk->fd, &m, 0); - if (x < 0) - { - if (errno == ENOBUFS) - { - /* - * Netlink reports some packets have been thrown away. - * One day we might react to it by asking for route table - * scan in near future. - */ - return 1; /* More data are likely to be ready */ - } - else if (errno != EWOULDBLOCK) - log(L_ERR "Netlink recvmsg: %m"); - return 0; - } - if (sa.nl_pid) /* It isn't from the kernel */ - { - DBG("Non-kernel packet\n"); - return 1; - } - h = (void *) nl_async_rx_buffer; - len = x; - if (m.msg_flags & MSG_TRUNC) - { - log(L_WARN "Netlink got truncated asynchronous message"); - return 1; - } - while (NLMSG_OK(h, len)) - { - nl_async_msg(h); - h = NLMSG_NEXT(h, len); - } - if (len) - log(L_WARN "nl_async_hook: Found packet remnant of size %d", len); - return 1; -} - -static void -nl_open_async(void) -{ - sock *sk; - struct sockaddr_nl sa; - int fd; - static int nl_open_tried = 0; - - if (nl_open_tried) - return; - nl_open_tried = 1; - - DBG("KRT: Opening async netlink socket\n"); - - fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); - if (fd < 0) - { - log(L_ERR "Unable to open asynchronous rtnetlink socket: %m"); - return; - } - - bzero(&sa, sizeof(sa)); - sa.nl_family = AF_NETLINK; -#ifdef IPV6 - sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; -#else - sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE; -#endif - if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) - { - log(L_ERR "Unable to bind asynchronous rtnetlink socket: %m"); - return; - } - - sk = nl_async_sk = sk_new(krt_pool); - sk->type = SK_MAGIC; - sk->rx_hook = nl_async_hook; - sk->fd = fd; - if (sk_open(sk)) - bug("Netlink: sk_open failed"); - - if (!nl_async_rx_buffer) - nl_async_rx_buffer = xmalloc(NL_RX_SIZE); -} - -/* - * Interface to the UNIX krt module - */ - -static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8]; - -void -krt_sys_start(struct krt_proto *p, int first) -{ - nl_table_map[KRT_CF->sys.table_id] = p; - if (first) - { - nl_open(); - nl_open_async(); - } -} - -void -krt_sys_shutdown(struct krt_proto *p UNUSED, int last UNUSED) -{ -} - -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; -} - - -void -krt_sys_preconfig(struct config *c UNUSED) -{ - bzero(&nl_cf_table, sizeof(nl_cf_table)); -} - -void -krt_sys_postconfig(struct krt_config *x) -{ - int id = x->sys.table_id; - - if (nl_cf_table[id/8] & (1 << (id%8))) - cf_error("Multiple kernel syncers defined for table #%d", id); - nl_cf_table[id/8] |= (1 << (id%8)); -} - -void -krt_sys_init_config(struct krt_config *cf) -{ - cf->sys.table_id = RT_TABLE_MAIN; -} - -void -krt_sys_copy_config(struct krt_config *d, struct krt_config *s) -{ - d->sys.table_id = s->sys.table_id; -} - - - -void -kif_sys_start(struct kif_proto *p UNUSED) -{ - nl_open(); - nl_open_async(); -} - -void -kif_sys_shutdown(struct kif_proto *p UNUSED) -{ -} diff --git a/sysdep/linux/sysio.h b/sysdep/linux/sysio.h index bb522804..705a20ae 100644 --- a/sysdep/linux/sysio.h +++ b/sysdep/linux/sysio.h @@ -57,45 +57,6 @@ get_inaddr(ip_addr *a, struct in_addr *ia) ipa_ntoh(*a); } -/* - * Multicasting in Linux systems is a real mess. Not only different kernels - * have different interfaces, but also different libc's export it in different - * ways. Horrible. - */ - - -#if defined(CONFIG_LINUX_MC_MREQ) || defined(CONFIG_LINUX_MC_MREQ_BIND) -/* - * Older kernels support only struct mreq which matches interfaces by their - * addresses and thus fails on unnumbered devices. On newer 2.0 kernels - * we can use SO_BINDTODEVICE to circumvent this problem. - */ - -#define MREQ_IFA struct in_addr -#define MREQ_GRP struct ip_mreq -static inline void fill_mreq_ifa(struct in_addr *m, struct iface *ifa UNUSED, ip_addr saddr, ip_addr maddr UNUSED) -{ - set_inaddr(m, saddr); -} - -static inline void fill_mreq_grp(struct ip_mreq *m, struct iface *ifa, ip_addr saddr, ip_addr maddr) -{ - bzero(m, sizeof(*m)); -#ifdef CONFIG_LINUX_MC_MREQ_BIND - m->imr_interface.s_addr = INADDR_ANY; -#else - set_inaddr(&m->imr_interface, saddr); -#endif - set_inaddr(&m->imr_multiaddr, maddr); -} -#endif - - -#ifdef CONFIG_LINUX_MC_MREQN -/* - * 2.1 and newer kernels use struct mreqn which passes ifindex, so no - * problems with unnumbered devices. - */ #ifndef HAVE_STRUCT_IP_MREQN /* Several versions of glibc don't define this structure, so we have to do it ourselves */ @@ -107,24 +68,19 @@ struct ip_mreqn }; #endif -#define MREQ_IFA struct ip_mreqn -#define MREQ_GRP struct ip_mreqn -#define fill_mreq_ifa fill_mreq -#define fill_mreq_grp fill_mreq -static inline void fill_mreq(struct ip_mreqn *m, struct iface *ifa, ip_addr saddr, ip_addr maddr) +static inline void fill_mreqn(struct ip_mreqn *m, struct iface *ifa, ip_addr saddr, ip_addr maddr) { bzero(m, sizeof(*m)); m->imr_ifindex = ifa->index; set_inaddr(&m->imr_address, saddr); set_inaddr(&m->imr_multiaddr, maddr); } -#endif static inline char * sysio_setup_multicast(sock *s) { - MREQ_IFA m; + struct ip_mreqn m; int zero = 0; if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) @@ -134,18 +90,15 @@ sysio_setup_multicast(sock *s) return "IP_MULTICAST_TTL"; /* This defines where should we send _outgoing_ multicasts */ - fill_mreq_ifa(&m, s->iface, s->saddr, IPA_NONE); + fill_mreqn(&m, s->iface, s->saddr, IPA_NONE); if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0) return "IP_MULTICAST_IF"; -#if defined(CONFIG_LINUX_MC_MREQ_BIND) || defined(CONFIG_LINUX_MC_MREQN) - { - struct ifreq ifr; - strcpy(ifr.ifr_name, s->iface->name); - if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) - return "SO_BINDTODEVICE"; - } -#endif + /* Is this necessary? */ + struct ifreq ifr; + strcpy(ifr.ifr_name, s->iface->name); + if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) + return "SO_BINDTODEVICE"; return NULL; } @@ -153,10 +106,10 @@ sysio_setup_multicast(sock *s) static inline char * sysio_join_group(sock *s, ip_addr maddr) { - MREQ_GRP m; + struct ip_mreqn m; /* And this one sets interface for _receiving_ multicasts from */ - fill_mreq_grp(&m, s->iface, s->saddr, maddr); + fill_mreqn(&m, s->iface, s->saddr, maddr); if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0) return "IP_ADD_MEMBERSHIP"; @@ -166,10 +119,10 @@ sysio_join_group(sock *s, ip_addr maddr) static inline char * sysio_leave_group(sock *s, ip_addr maddr) { - MREQ_GRP m; + struct ip_mreqn m; /* And this one sets interface for _receiving_ multicasts from */ - fill_mreq_grp(&m, s->iface, s->saddr, maddr); + fill_mreqn(&m, s->iface, s->saddr, maddr); if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0) return "IP_DROP_MEMBERSHIP"; diff --git a/sysdep/unix/Modules b/sysdep/unix/Modules index 2666f9d6..2c6514df 100644 --- a/sysdep/unix/Modules +++ b/sysdep/unix/Modules @@ -10,13 +10,3 @@ random.c krt.c krt.h krt.Y - -#ifdef CONFIG_UNIX_IFACE -krt-iface.c -krt-iface.h -#endif - -#ifdef CONFIG_UNIX_SET -krt-set.c -krt-set.h -#endif diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 2dced67a..475d660c 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -950,7 +950,7 @@ int sk_join_group(sock *s, ip_addr maddr) { struct ipv6_mreq mreq; - + set_inaddr(&mreq.ipv6mr_multiaddr, maddr); #ifdef CONFIG_IPV6_GLIBC_20 diff --git a/sysdep/unix/krt-iface.c b/sysdep/unix/krt-iface.c deleted file mode 100644 index 88c17ecd..00000000 --- a/sysdep/unix/krt-iface.c +++ /dev/null @@ -1,228 +0,0 @@ -/* - * BIRD -- Unix Interface Scanning and Syncing - * - * (c) 1998--2000 Martin Mares - * (c) 2004 Ondrej Filip - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#include -#include -#include -#include -#include -#include - -#undef LOCAL_DEBUG - -#include "nest/bird.h" -#include "nest/iface.h" -#include "nest/route.h" -#include "nest/protocol.h" -#include "lib/timer.h" -#include "lib/krt.h" -#include "lib/string.h" - -#include "unix.h" - -int if_scan_sock = -1; - -static void -scan_ifs(struct ifreq *r, int cnt) -{ - struct iface i, *pi; - struct ifa a; - char *err, *colon; - unsigned fl; - ip_addr netmask; - int l, scope; - sockaddr *sa; - - if_start_update(); - for (cnt /= sizeof(struct ifreq); cnt; cnt--, r++) - { - int sec = 0; - bzero(&i, sizeof(i)); - bzero(&a, sizeof(a)); - if (colon = strchr(r->ifr_name, ':')) - { - /* It's an alias -- let's interpret it as a secondary interface address */ - sec = 1; - *colon = 0; - } - strncpy(i.name, r->ifr_name, sizeof(i.name) - 1); - - if(ioctl(if_scan_sock, SIOCGIFADDR,r)<0) continue; - - get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.ip, NULL, 1); - if (ipa_nonzero(a.ip)) - { - l = ipa_classify(a.ip); - if (l < 0 || !(l & IADDR_HOST)) - { - log(L_ERR "%s: Invalid interface address", i.name); - a.ip = IPA_NONE; - } - else - { - a.scope = l & IADDR_SCOPE_MASK; - if (a.scope == SCOPE_HOST) - i.flags |= IF_LOOPBACK | IF_IGNORE; - } - } - - if (ioctl(if_scan_sock, SIOCGIFFLAGS, r) < 0) - { - err = "SIOCGIFFLAGS"; - faulty: - log(L_ERR "%s(%s): %m", err, i.name); - bad: - i.flags = (i.flags & ~IF_ADMIN_UP) | IF_SHUTDOWN; - continue; - } - fl = r->ifr_flags; - if (fl & IFF_UP) - i.flags |= IF_ADMIN_UP; - - if (ioctl(if_scan_sock, SIOCGIFNETMASK, r) < 0) - { err = "SIOCGIFNETMASK"; goto faulty; } - get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &netmask, NULL, 0); - l = ipa_mklen(netmask); - if (l < 0) - { - log(L_ERR "%s: Invalid netmask (%x)", i.name, netmask); - goto bad; - } - a.pxlen = l; - - if (fl & IFF_POINTOPOINT) - { - a.flags |= IA_PEER; - if (ioctl(if_scan_sock, SIOCGIFDSTADDR, r) < 0) - { err = "SIOCGIFDSTADDR"; goto faulty; } - get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.opposite, NULL, 1); - a.prefix = a.opposite; - a.pxlen = BITS_PER_IP_ADDRESS; - } - else - a.prefix = ipa_and(a.ip, ipa_mkmask(a.pxlen)); - if (fl & IFF_LOOPBACK) - i.flags |= IF_LOOPBACK | IF_IGNORE; - if (1 -#ifndef CONFIG_ALL_MULTICAST - && (fl & IFF_MULTICAST) -#endif -#ifndef CONFIG_UNNUM_MULTICAST - && !(a.flags & IA_PEER) -#endif - ) - i.flags |= IF_MULTICAST; - - scope = ipa_classify(a.ip); - if (scope < 0) - { - log(L_ERR "%s: Invalid address", i.name); - goto bad; - } - a.scope = scope & IADDR_SCOPE_MASK; - - if (a.pxlen < 32) - { - a.brd = ipa_or(a.prefix, ipa_not(ipa_mkmask(a.pxlen))); - if (ipa_equal(a.ip, a.prefix) || ipa_equal(a.ip, a.brd)) - { - log(L_ERR "%s: Using network or broadcast address for interface", i.name); - goto bad; - } - if (fl & IFF_BROADCAST) - i.flags |= IF_BROADCAST; - if (a.pxlen < 30) - i.flags |= IF_MULTIACCESS; - if (a.pxlen == 30) - ifa.opposite = ipa_opposite_m2(ifa.ip); - if (a.pxlen == 31) - ifa.opposite = ipa_opposite_m1(ifa.ip); - } - else - a.brd = a.opposite; - a.scope = SCOPE_UNIVERSE; - - if (ioctl(if_scan_sock, SIOCGIFMTU, r) < 0) - { err = "SIOCGIFMTU"; goto faulty; } - i.mtu = r->ifr_mtu; - -#ifdef SIOCGIFINDEX - if (ioctl(if_scan_sock, SIOCGIFINDEX, r) >= 0) - i.index = r->ifr_ifindex; - else if (errno != EINVAL) - DBG("SIOCGIFINDEX failed: %m\n"); - else /* defined, but not supported by the kernel */ -#endif - /* - * The kernel doesn't give us real ifindices, but we still need them - * at least for OSPF unnumbered links. So let's make them up ourselves. - */ - if (pi = if_find_by_name(i.name)) - i.index = pi->index; - else - { - static int if_index_counter = 1; - i.index = if_index_counter++; - } - - pi = NULL; - if (sec) - { - a.flags |= IA_SECONDARY; - pi = if_find_by_index(i.index); - } - if (!pi) - pi = if_update(&i); - a.iface = pi; - ifa_update(&a); - } - if_end_update(); -} - -void -kif_do_scan(struct kif_proto *p) -{ - struct ifconf ic; - static int last_ifbuf_size = 4*sizeof(struct ifreq); - int res; - - for(;;) - { - ic.ifc_buf = alloca(last_ifbuf_size); - ic.ifc_len = last_ifbuf_size; - res = ioctl(if_scan_sock, SIOCGIFCONF, &ic); - if (res < 0 && errno != EFAULT) - die("SIOCCGIFCONF: %m"); - if (res >= 0 && ic.ifc_len <= last_ifbuf_size) - break; - last_ifbuf_size *= 2; - DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size); - } - scan_ifs(ic.ifc_req, ic.ifc_len); -} - -void -kif_start(struct kif_proto *p) -{ -} - -void -kif_shutdown(struct kif_proto *p) -{ -} - -void -kif_io_init(void) -{ - if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - DBG("Using socket %d for interface and route scanning\n", if_scan_sock); - if (if_scan_sock < 0) - die("Cannot create scanning socket: %m"); -} - diff --git a/sysdep/unix/krt-iface.h b/sysdep/unix/krt-iface.h deleted file mode 100644 index e9e8e507..00000000 --- a/sysdep/unix/krt-iface.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * BIRD -- Unix Kernel Interface Syncer - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_IFACE_H_ -#define _BIRD_KRT_IFACE_H_ - -struct kif_params { -}; - -struct kif_status { -}; - -extern int if_scan_sock; - - -static inline void kif_sys_init(struct kif_proto *p UNUSED) { } -static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } - -static inline void kif_sys_preconfig(struct config *c UNUSED) { } -static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } -static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } -static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } - -#endif diff --git a/sysdep/unix/krt-set.c b/sysdep/unix/krt-set.c deleted file mode 100644 index 252c5d3e..00000000 --- a/sysdep/unix/krt-set.c +++ /dev/null @@ -1,112 +0,0 @@ -/* - * BIRD -- Unix Routing Table Syncing - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#include -#include -#include -#include -#include - -#undef LOCAL_DEBUG - -#include "nest/bird.h" -#include "nest/iface.h" -#include "nest/route.h" -#include "nest/protocol.h" -#include "lib/unix.h" -#include "lib/krt.h" -#include "lib/string.h" - -int -krt_capable(rte *e) -{ - rta *a = e->attrs; - -#ifdef CONFIG_AUTO_ROUTES - if (a->source == RTS_DEVICE) - return 0; -#endif - return - a->cast == RTC_UNICAST && - (a->dest == RTD_ROUTER - || a->dest == RTD_DEVICE -#ifdef RTF_REJECT - || a->dest == RTD_UNREACHABLE -#endif - ); -} - -static void -krt_ioctl(int ioc, rte *e, char *name) -{ - net *net = e->net; - struct rtentry re; - rta *a = e->attrs; - - bzero(&re, sizeof(re)); - fill_in_sockaddr((struct sockaddr_in *) &re.rt_dst, net->n.prefix, 0); - fill_in_sockaddr((struct sockaddr_in *) &re.rt_genmask, ipa_mkmask(net->n.pxlen), 0); - re.rt_flags = RTF_UP; - if (net->n.pxlen == 32) - re.rt_flags |= RTF_HOST; - switch (a->dest) - { - case RTD_ROUTER: - fill_in_sockaddr((struct sockaddr_in *) &re.rt_gateway, a->gw, 0); - re.rt_flags |= RTF_GATEWAY; - break; - case RTD_DEVICE: - if (!a->iface) - return; - re.rt_dev = a->iface->name; - break; -#ifdef RTF_REJECT - case RTD_UNREACHABLE: - re.rt_flags |= RTF_REJECT; - break; -#endif - default: - bug("krt set: unknown flags, but not filtered"); - } - - if (ioctl(if_scan_sock, ioc, &re) < 0) - log(L_ERR "%s(%I/%d): %m", name, net->n.prefix, net->n.pxlen); -} - -void -krt_do_notify(struct krt_proto *p, net *net, rte *new, rte *old) -{ - if (old) - { - DBG("krt_remove_route(%I/%d)\n", net->n.prefix, net->n.pxlen); - krt_ioctl(SIOCDELRT, old, "SIOCDELRT"); - } - if (new) - { - DBG("krt_add_route(%I/%d)\n", net->n.prefix, net->n.pxlen); - krt_ioctl(SIOCADDRT, new, "SIOCADDRT"); - } -} - -void -krt_sys_start(struct krt_proto *x, int first) -{ - if (if_scan_sock < 0) - bug("krt set: missing socket"); -} - -void -krt_sys_shutdown(struct krt_proto *x, int last) -{ -} - -void -krt_sys_construct(struct krt_config *c) -{ -} - diff --git a/sysdep/unix/krt-set.h b/sysdep/unix/krt-set.h deleted file mode 100644 index 8ef3170e..00000000 --- a/sysdep/unix/krt-set.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * BIRD -- Unix Kernel Route Syncer - * - * (c) 1998--2000 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL. - */ - -#ifndef _BIRD_KRT_SET_H_ -#define _BIRD_KRT_SET_H_ - -struct krt_params { -}; - -struct krt_status { -}; - - -static inline void krt_sys_init(struct krt_proto *p UNUSED) { } -static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; } - -static inline void krt_sys_preconfig(struct config *c UNUSED) { } -static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { } -static inline void krt_sys_init_config(struct krt_config *c UNUSED) { } -static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { } - -#endif diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index cc03bb88..b0d22227 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -23,7 +23,7 @@ * Either with a single routing table and single KRT protocol [traditional UNIX] * or with many routing tables and separate KRT protocols for all of them * or with many routing tables, but every scan including all tables, so we start - * separate KRT protocols which cooperate with each other [Linux 2.2]. + * separate KRT protocols which cooperate with each other [Linux]. * In this case, we keep only a single scan timer. * * We use FIB node flags in the routing table to keep track of route @@ -34,6 +34,15 @@ * When starting up, we cheat by looking if there is another * KRT instance to be initialized later and performing table scan * only once for all the instances. + * + * The code uses OS-dependent parts for kernel updates and scans. These parts are + * in more specific sysdep directories (e.g. sysdep/linux) in functions krt_sys_* + * and kif_sys_* (and some others like krt_do_notify()) and krt-sys.h header file. + * This is also used for platform specific protocol options and route attributes. + * + * There was also an old code that used traditional UNIX ioctls for these tasks. + * It was unmaintained and later removed. For reference, see sysdep/krt-* files + * in commit 396dfa9042305f62da1f56589c4b98fac57fc2f6 */ /* @@ -66,7 +75,6 @@ krt_io_init(void) { krt_pool = rp_new(&root_pool, "Kernel Syncer"); krt_filter_lp = lp_new(krt_pool, 4080); - kif_sys_io_init(); } /* diff --git a/sysdep/unix/krt.h b/sysdep/unix/krt.h index a3b5658a..9d5639f2 100644 --- a/sysdep/unix/krt.h +++ b/sysdep/unix/krt.h @@ -15,9 +15,7 @@ struct krt_proto; struct kif_config; struct kif_proto; -#include "lib/krt-scan.h" -#include "lib/krt-set.h" -#include "lib/krt-iface.h" +#include "lib/krt-sys.h" /* Flags stored in net->n.flags, rest are in nest/route.h */ @@ -113,7 +111,7 @@ struct kif_proto { struct proto_config * krt_init_config(int class); -/* krt-scan.c */ +/* krt sysdep */ void krt_sys_init(struct krt_proto *); void krt_sys_start(struct krt_proto *, int); @@ -130,7 +128,7 @@ void krt_do_scan(struct krt_proto *); void krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs); -/* krt-iface.c */ +/* kif sysdep */ void kif_sys_init(struct kif_proto *); void kif_sys_start(struct kif_proto *); @@ -142,6 +140,5 @@ void kif_sys_copy_config(struct kif_config *, struct kif_config *); void kif_do_scan(struct kif_proto *); -// void kif_sys_io_init(void); #endif From 7a2c48dafce9420a23fd57408c31eecfc20c4fe0 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Mon, 30 Apr 2012 22:34:06 +0200 Subject: [PATCH 4/8] Cleanup in sysdep KRT code, part 3. Just one more renaming, old krt_set_notify() to krt_replace_rte(). --- sysdep/bsd/krt-sock.c | 4 ++-- sysdep/unix/krt.c | 14 +++++++------- sysdep/unix/krt.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sysdep/bsd/krt-sock.c b/sysdep/bsd/krt-sock.c index 5fb5f9f7..e970d6bd 100644 --- a/sysdep/bsd/krt-sock.c +++ b/sysdep/bsd/krt-sock.c @@ -201,8 +201,8 @@ krt_sock_send(int cmd, rte *e) } void -krt_do_notify(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, - struct ea_list *eattrs UNUSED) +krt_replace_rte(struct krt_proto *p UNUSED, net *n, rte *new, rte *old, + struct ea_list *eattrs UNUSED) { int err = 0; diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index b0d22227..bf098774 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -37,7 +37,7 @@ * * The code uses OS-dependent parts for kernel updates and scans. These parts are * in more specific sysdep directories (e.g. sysdep/linux) in functions krt_sys_* - * and kif_sys_* (and some others like krt_do_notify()) and krt-sys.h header file. + * and kif_sys_* (and some others like krt_replace_rte()) and krt-sys.h header file. * This is also used for platform specific protocol options and route attributes. * * There was also an old code that used traditional UNIX ioctls for these tasks. @@ -582,7 +582,7 @@ krt_flush_routes(struct krt_proto *p) a->source != RTS_DEVICE && a->source != RTS_INHERIT) { /* FIXME: this does not work if gw is changed in export filter */ - krt_do_notify(p, e->net, NULL, e, NULL); + krt_replace_rte(p, e->net, NULL, e, NULL); n->n.flags &= ~KRF_INSTALLED; } } @@ -741,7 +741,7 @@ krt_prune(struct krt_proto *p) if (new && (f->flags & KRF_INSTALLED)) { krt_trace_in(p, new, "reinstalling"); - krt_do_notify(p, n, new, NULL, tmpa); + krt_replace_rte(p, n, new, NULL, tmpa); } break; case KRF_SEEN: @@ -750,11 +750,11 @@ krt_prune(struct krt_proto *p) break; case KRF_UPDATE: krt_trace_in(p, new, "updating"); - krt_do_notify(p, n, new, old, tmpa); + krt_replace_rte(p, n, new, old, tmpa); break; case KRF_DELETE: krt_trace_in(p, old, "deleting"); - krt_do_notify(p, n, NULL, old, NULL); + krt_replace_rte(p, n, NULL, old, NULL); break; default: bug("krt_prune: invalid route status"); @@ -790,7 +790,7 @@ krt_got_route_async(struct krt_proto *p, rte *e, int new) if (new) { krt_trace_in(p, e, "[redirect] deleting"); - krt_do_notify(p, net, NULL, e, NULL); + krt_replace_rte(p, net, NULL, e, NULL); } /* If !new, it is probably echo of our deletion */ break; @@ -908,7 +908,7 @@ krt_notify(struct proto *P, struct rtable *table UNUSED, net *net, else net->n.flags &= ~KRF_INSTALLED; if (p->initialized) /* Before first scan we don't touch the routes */ - krt_do_notify(p, net, new, old, eattrs); + krt_replace_rte(p, net, new, old, eattrs); } static int diff --git a/sysdep/unix/krt.h b/sysdep/unix/krt.h index 9d5639f2..d6fbf721 100644 --- a/sysdep/unix/krt.h +++ b/sysdep/unix/krt.h @@ -125,7 +125,7 @@ void krt_sys_copy_config(struct krt_config *, struct krt_config *); int krt_capable(rte *e); void krt_do_scan(struct krt_proto *); -void krt_do_notify(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs); +void krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs); /* kif sysdep */ From ab188fb76d7822350724b182106a19995a73d719 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Thu, 3 May 2012 12:25:15 +0200 Subject: [PATCH 5/8] Implements build options to specify socket dir and suffix. --- configure.in | 46 ++++++++++++++++++++++++++++++-------------- sysdep/autoconf.h.in | 1 + sysdep/config.h | 19 ------------------ sysdep/unix/main.c | 2 +- tools/Makefile.in | 16 +++++++-------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/configure.in b/configure.in index 7d9560d3..dd57ab51 100644 --- a/configure.in +++ b/configure.in @@ -6,17 +6,20 @@ AC_REVISION($Id$) AC_INIT(conf/confbase.Y) AC_CONFIG_AUX_DIR(tools) -AC_ARG_ENABLE(debug,[ --enable-debug enable internal debugging routines (default: disabled)],,enable_debug=no) -AC_ARG_ENABLE(memcheck,[ --enable-memcheck check memory allocations when debugging (default: enabled)],,enable_memcheck=yes) -AC_ARG_ENABLE(client,[ --enable-client enable building of BIRD client (default: enabled)],,enable_client=yes) -AC_ARG_ENABLE(ipv6,[ --enable-ipv6 enable building of IPv6 version (default: disabled)],,enable_ipv6=no) -AC_ARG_WITH(sysconfig,[ --with-sysconfig=FILE use specified BIRD system configuration file]) -AC_ARG_WITH(protocols,[ --with-protocols=LIST include specified routing protocols (default: all)],,[with_protocols="all"]) -AC_ARG_WITH(sysinclude,[ --with-sysinclude=PATH search for system includes on specified place]) -AC_ARG_WITH(iproutedir,[ --with-iproutedir=PATH path to iproute2 config files (default: /etc/iproute2)],[given_iproutedir="yes"]) -AC_ARG_VAR([FLEX], [location of the Flex program]) -AC_ARG_VAR([BISON], [location of the Bison program]) -AC_ARG_VAR([M4], [location of the M4 program]) +AC_ARG_ENABLE(debug, [ --enable-debug enable internal debugging routines (default: disabled)],,enable_debug=no) +AC_ARG_ENABLE(memcheck, [ --enable-memcheck check memory allocations when debugging (default: enabled)],,enable_memcheck=yes) +AC_ARG_ENABLE(client, [ --enable-client enable building of BIRD client (default: enabled)],,enable_client=yes) +AC_ARG_ENABLE(ipv6, [ --enable-ipv6 enable building of IPv6 version (default: disabled)],,enable_ipv6=no) +AC_ARG_WITH(suffix, [ --with-suffix=STRING use specified suffix for BIRD files (default: 6 for IPv6 version)],[given_suffix="yes"]) +AC_ARG_WITH(sysconfig, [ --with-sysconfig=FILE use specified BIRD system configuration file]) +AC_ARG_WITH(protocols, [ --with-protocols=LIST include specified routing protocols (default: all)],,[with_protocols="all"]) +AC_ARG_WITH(sysinclude, [ --with-sysinclude=PATH search for system includes on specified place]) +AC_ARG_WITH(runtimedir, [ --with-runtimedir=PATH path for runtime files (default: $(localstatedir)/run)],[runtimedir="$with_runtimedir"],[runtimedir="\$(localstatedir)/run"]) +AC_ARG_WITH(iproutedir, [ --with-iproutedir=PATH path to iproute2 config files (default: /etc/iproute2)],[given_iproutedir="yes"]) +AC_ARG_VAR([FLEX], [location of the Flex program]) +AC_ARG_VAR([BISON], [location of the Bison program]) +AC_ARG_VAR([M4], [location of the M4 program]) + if test "$srcdir" = . ; then # Building in current directory => create obj directory holding all objects @@ -39,21 +42,37 @@ esac AC_SUBST(objdir) AC_SUBST(exedir) AC_SUBST(srcdir_rel_mf) +AC_SUBST(runtimedir) if test "$enable_ipv6" = yes ; then ip=ipv6 - SUFFIX6=6 + SUFFIX=6 all_protocols=bgp,ospf,pipe,radv,rip,static else ip=ipv4 - SUFFIX6="" + SUFFIX="" all_protocols=bgp,ospf,pipe,rip,static fi +if test "$given_suffix" = yes ; then + SUFFIX="$with_suffix" +fi +AC_SUBST(SUFFIX) + if test "$with_protocols" = all ; then with_protocols="$all_protocols" fi +if test "$enable_debug" = yes ; then + CONFIG_FILE="bird$SUFFIX.conf" + CONTROL_SOCKET="bird$SUFFIX.ctl" +else + CONFIG_FILE="\$(sysconfdir)/bird$SUFFIX.conf" + CONTROL_SOCKET="$runtimedir/bird$SUFFIX.ctl" +fi +AC_SUBST(CONFIG_FILE) +AC_SUBST(CONTROL_SOCKET) + AC_SEARCH_LIBS(clock_gettime,[c rt posix4]) AC_CANONICAL_HOST @@ -225,7 +244,6 @@ if test "$enable_client" = yes ; then fi AC_SUBST(CLIENT) AC_SUBST(CLIENT_LIBS) -AC_SUBST(SUFFIX6) mkdir -p $objdir/sysdep AC_CONFIG_HEADERS([$objdir/sysdep/autoconf.h:sysdep/autoconf.h.in]) diff --git a/sysdep/autoconf.h.in b/sysdep/autoconf.h.in index d029e2a7..ac6f7a87 100644 --- a/sysdep/autoconf.h.in +++ b/sysdep/autoconf.h.in @@ -62,3 +62,4 @@ /* We have stdint.h */ #undef HAVE_STDINT_H +#define CONFIG_PATH ? diff --git a/sysdep/config.h b/sysdep/config.h index 7e6fad8b..8d93d381 100644 --- a/sysdep/config.h +++ b/sysdep/config.h @@ -37,23 +37,4 @@ typedef u16 word; #endif -/* Path to configuration file */ -#ifdef IPV6 -# ifdef DEBUGGING -# define PATH_CONFIG "bird6.conf" -# define PATH_CONTROL_SOCKET "bird6.ctl" -# else -# define PATH_CONFIG PATH_CONFIG_DIR "/bird6.conf" -# define PATH_CONTROL_SOCKET PATH_CONTROL_SOCKET_DIR "/bird6.ctl" -# endif -#else -# ifdef DEBUGGING -# define PATH_CONFIG "bird.conf" -# define PATH_CONTROL_SOCKET "bird.ctl" -# else -# define PATH_CONFIG PATH_CONFIG_DIR "/bird.conf" -# define PATH_CONTROL_SOCKET PATH_CONTROL_SOCKET_DIR "/bird.ctl" -# endif -#endif - #endif diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index dfe0b89c..e0563aae 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -151,7 +151,7 @@ read_iproute_table(char *file, char *prefix, int max) #endif // PATH_IPROUTE_DIR -static char *config_name = PATH_CONFIG; +static char *config_name = PATH_CONFIG_FILE; static int cf_read(byte *dest, unsigned int len, int fd) diff --git a/tools/Makefile.in b/tools/Makefile.in index 556eba5f..728e5797 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -48,23 +48,23 @@ userdocs progdocs: .dir-stamp sysdep/paths.h: echo >sysdep/paths.h "/* Generated by Makefile, don't edit manually! */" - echo >>sysdep/paths.h "#define PATH_CONFIG_DIR \"$(sysconfdir)\"" - echo >>sysdep/paths.h "#define PATH_CONTROL_SOCKET_DIR \"$(localstatedir)/run\"" + echo >>sysdep/paths.h "#define PATH_CONFIG_FILE \"@CONFIG_FILE@\"" + echo >>sysdep/paths.h "#define PATH_CONTROL_SOCKET \"@CONTROL_SOCKET@\"" if test -n "@iproutedir@" ; then echo >>sysdep/paths.h "#define PATH_IPROUTE_DIR \"@iproutedir@\"" ; fi tags: cd $(srcdir) ; etags -lc `find $(static-dirs) $(addprefix $(objdir)/,$(dynamic-dirs)) $(client-dirs) -name *.[chY]` install: all - $(INSTALL) -d $(DESTDIR)/$(sbindir) $(DESTDIR)/$(sysconfdir) $(DESTDIR)/$(localstatedir)/run - $(INSTALL_PROGRAM) -s $(exedir)/bird $(DESTDIR)/$(sbindir)/bird@SUFFIX6@ + $(INSTALL) -d $(DESTDIR)/$(sbindir) $(DESTDIR)/$(sysconfdir) $(DESTDIR)/@runtimedir@ + $(INSTALL_PROGRAM) -s $(exedir)/bird $(DESTDIR)/$(sbindir)/bird@SUFFIX@ if test -n "@CLIENT@" ; then \ - $(INSTALL_PROGRAM) -s $(exedir)/birdc $(DESTDIR)/$(sbindir)/birdc@SUFFIX6@ ; \ + $(INSTALL_PROGRAM) -s $(exedir)/birdc $(DESTDIR)/$(sbindir)/birdc@SUFFIX@ ; \ fi - if ! test -f $(DESTDIR)/$(sysconfdir)/bird@SUFFIX6@.conf ; then \ - $(INSTALL_DATA) $(srcdir)/doc/bird.conf.example $(DESTDIR)/$(sysconfdir)/bird@SUFFIX6@.conf ; \ + if ! test -f $(DESTDIR)/@CONFIG_FILE@ ; then \ + $(INSTALL_DATA) $(srcdir)/doc/bird.conf.example $(DESTDIR)/@CONFIG_FILE@ ; \ else \ - echo "Not overwriting old bird@SUFFIX@.conf" ; \ + echo "Not overwriting old bird@SUFFIX@.conf" ; \ fi install-docs: From e14bd38087ed8ef1945dd0a3878cc560478145f0 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Thu, 3 May 2012 14:04:56 +0200 Subject: [PATCH 6/8] Fixes flushing of device routes. --- sysdep/unix/krt.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index bf098774..2bd1bc44 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -575,16 +575,11 @@ krt_flush_routes(struct krt_proto *p) { net *n = (net *) f; rte *e = n->routes; - if (e) + if (e && (n->n.flags & KRF_INSTALLED)) { - rta *a = e->attrs; - if ((n->n.flags & KRF_INSTALLED) && - a->source != RTS_DEVICE && a->source != RTS_INHERIT) - { - /* FIXME: this does not work if gw is changed in export filter */ - krt_replace_rte(p, e->net, NULL, e, NULL); - n->n.flags &= ~KRF_INSTALLED; - } + /* FIXME: this does not work if gw is changed in export filter */ + krt_replace_rte(p, e->net, NULL, e, NULL); + n->n.flags &= ~KRF_INSTALLED; } } FIB_WALK_END; From 064e7be5cd4dffd564b4ea41ba6d843492a55c97 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Fri, 4 May 2012 00:20:23 +0200 Subject: [PATCH 7/8] History deduplication in birdc. --- client/client.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client/client.c b/client/client.c index 7f9e0ef4..8711cf0a 100644 --- a/client/client.c +++ b/client/client.c @@ -135,6 +135,14 @@ submit_server_command(char *cmd) num_lines = 2; } +static void +add_history_dedup(char *cmd) +{ + /* Add history line if it differs from the last one */ + HIST_ENTRY *he = history_get(history_length); + if (!he || strcmp(he->line, cmd)) + add_history(cmd); +} static void got_line(char *cmd_buffer) @@ -151,7 +159,7 @@ got_line(char *cmd_buffer) cmd = cmd_expand(cmd_buffer); if (cmd) { - add_history(cmd); + add_history_dedup(cmd); if (!handle_internal_command(cmd)) submit_server_command(cmd); @@ -159,7 +167,7 @@ got_line(char *cmd_buffer) free(cmd); } else - add_history(cmd_buffer); + add_history_dedup(cmd_buffer); } free(cmd_buffer); } From 95616c820248018f4999972cad315f2da60e4960 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Fri, 4 May 2012 16:38:25 +0200 Subject: [PATCH 8/8] Cleanup in sysdep KRT code, part 4. Adding some files that was accidentally removed (instead of moved) in cleanup part 2. --- sysdep/bsd/krt-sys.h | 49 ++ sysdep/cf/linux.h | 22 + sysdep/linux/krt-sys.h | 46 ++ sysdep/linux/netlink.Y | 32 ++ sysdep/linux/netlink.c | 1149 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1298 insertions(+) create mode 100644 sysdep/bsd/krt-sys.h create mode 100644 sysdep/cf/linux.h create mode 100644 sysdep/linux/krt-sys.h create mode 100644 sysdep/linux/netlink.Y create mode 100644 sysdep/linux/netlink.c diff --git a/sysdep/bsd/krt-sys.h b/sysdep/bsd/krt-sys.h new file mode 100644 index 00000000..88915dde --- /dev/null +++ b/sysdep/bsd/krt-sys.h @@ -0,0 +1,49 @@ +/* + * BIRD -- *BSD Kernel Route Syncer + * + * (c) 2004 Ondrej Filip + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_KRT_SYS_H_ +#define _BIRD_KRT_SYS_H_ + + +/* Kernel interfaces */ + +struct kif_params { +}; + +struct kif_status { +}; + + +static inline void kif_sys_init(struct kif_proto *p UNUSED) { } +static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } + +static inline void kif_sys_preconfig(struct config *c UNUSED) { } +static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } +static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } +static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } + + +/* Kernel routes */ + +struct krt_params { +}; + +struct krt_status { +}; + + +static inline void krt_sys_init(struct krt_proto *p UNUSED) { } +static inline int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n UNUSED, struct krt_config *o UNUSED) { return 1; } + +static inline void krt_sys_preconfig(struct config *c UNUSED) { } +static inline void krt_sys_postconfig(struct krt_config *c UNUSED) { } +static inline void krt_sys_init_config(struct krt_config *c UNUSED) { } +static inline void krt_sys_copy_config(struct krt_config *d UNUSED, struct krt_config *s UNUSED) { } + + +#endif diff --git a/sysdep/cf/linux.h b/sysdep/cf/linux.h new file mode 100644 index 00000000..9e34f869 --- /dev/null +++ b/sysdep/cf/linux.h @@ -0,0 +1,22 @@ +/* + * Configuration for Linux based systems + * + * (c) 1998--1999 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#define CONFIG_AUTO_ROUTES +#define CONFIG_SELF_CONSCIOUS +#define CONFIG_MULTIPLE_TABLES +#define CONFIG_ALL_TABLES_AT_ONCE + +#define CONFIG_MC_PROPER_SRC +#define CONFIG_UNIX_DONTROUTE + +#define CONFIG_RESTRICTED_PRIVILEGES + +/* +Link: sysdep/linux +Link: sysdep/unix + */ diff --git a/sysdep/linux/krt-sys.h b/sysdep/linux/krt-sys.h new file mode 100644 index 00000000..cdee7fe3 --- /dev/null +++ b/sysdep/linux/krt-sys.h @@ -0,0 +1,46 @@ +/* + * BIRD -- Linux Kernel Netlink Route Syncer + * + * (c) 1998--2000 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#ifndef _BIRD_KRT_SYS_H_ +#define _BIRD_KRT_SYS_H_ + + +/* Kernel interfaces */ + +struct kif_params { +}; + +struct kif_status { +}; + + +static inline void kif_sys_init(struct kif_proto *p UNUSED) { } +static inline int kif_sys_reconfigure(struct kif_proto *p UNUSED, struct kif_config *n UNUSED, struct kif_config *o UNUSED) { return 1; } + +static inline void kif_sys_preconfig(struct config *c UNUSED) { } +static inline void kif_sys_postconfig(struct kif_config *c UNUSED) { } +static inline void kif_sys_init_config(struct kif_config *c UNUSED) { } +static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_config *s UNUSED) { } + + +/* Kernel routes */ + +#define NL_NUM_TABLES 256 + +struct krt_params { + int table_id; /* Kernel table ID we sync with */ +}; + +struct krt_status { +}; + + +static inline void krt_sys_init(struct krt_proto *p UNUSED) { } + + +#endif diff --git a/sysdep/linux/netlink.Y b/sysdep/linux/netlink.Y new file mode 100644 index 00000000..51689ff9 --- /dev/null +++ b/sysdep/linux/netlink.Y @@ -0,0 +1,32 @@ +/* + * BIRD -- Linux Netlink Configuration + * + * (c) 1999--2000 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +CF_HDR + +CF_DECLS + +CF_KEYWORDS(ASYNC, KERNEL, TABLE, KRT_PREFSRC, KRT_REALM) + +CF_GRAMMAR + +CF_ADDTO(kern_proto, kern_proto nl_item ';') + +nl_item: + KERNEL TABLE expr { + if ($3 <= 0 || $3 >= NL_NUM_TABLES) + cf_error("Kernel routing table number out of range"); + THIS_KRT->sys.table_id = $3; + } + ; + +CF_ADDTO(dynamic_attr, KRT_PREFSRC { $$ = f_new_dynamic_attr(EAF_TYPE_IP_ADDRESS, T_IP, EA_KRT_PREFSRC); }) +CF_ADDTO(dynamic_attr, KRT_REALM { $$ = f_new_dynamic_attr(EAF_TYPE_INT, T_INT, EA_KRT_REALM); }) + +CF_CODE + +CF_END diff --git a/sysdep/linux/netlink.c b/sysdep/linux/netlink.c new file mode 100644 index 00000000..eaaf048e --- /dev/null +++ b/sysdep/linux/netlink.c @@ -0,0 +1,1149 @@ +/* + * BIRD -- Linux Netlink Interface + * + * (c) 1999--2000 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include +#include +#include +#include +#include + +#undef LOCAL_DEBUG + +#include "nest/bird.h" +#include "nest/route.h" +#include "nest/protocol.h" +#include "nest/iface.h" +#include "lib/alloca.h" +#include "lib/timer.h" +#include "lib/unix.h" +#include "lib/krt.h" +#include "lib/socket.h" +#include "lib/string.h" +#include "conf/conf.h" + +#include +#include +#include +#include + +#ifndef MSG_TRUNC /* Hack: Several versions of glibc miss this one :( */ +#define MSG_TRUNC 0x20 +#endif + +#ifndef IFF_LOWER_UP +#define IFF_LOWER_UP 0x10000 +#endif + +/* + * Synchronous Netlink interface + */ + +struct nl_sock +{ + int fd; + u32 seq; + byte *rx_buffer; /* Receive buffer */ + struct nlmsghdr *last_hdr; /* Recently received packet */ + unsigned int last_size; +}; + +#define NL_RX_SIZE 8192 + +static struct nl_sock nl_scan = {.fd = -1}; /* Netlink socket for synchronous scan */ +static struct nl_sock nl_req = {.fd = -1}; /* Netlink socket for requests */ + +static void +nl_open_sock(struct nl_sock *nl) +{ + if (nl->fd < 0) + { + nl->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + if (nl->fd < 0) + die("Unable to open rtnetlink socket: %m"); + nl->seq = now; + nl->rx_buffer = xmalloc(NL_RX_SIZE); + nl->last_hdr = NULL; + nl->last_size = 0; + } +} + +static void +nl_open(void) +{ + nl_open_sock(&nl_scan); + nl_open_sock(&nl_req); +} + +static void +nl_send(struct nl_sock *nl, struct nlmsghdr *nh) +{ + struct sockaddr_nl sa; + + memset(&sa, 0, sizeof(sa)); + sa.nl_family = AF_NETLINK; + nh->nlmsg_pid = 0; + nh->nlmsg_seq = ++(nl->seq); + if (sendto(nl->fd, nh, nh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) + die("rtnetlink sendto: %m"); + nl->last_hdr = NULL; +} + +static void +nl_request_dump(int cmd) +{ + struct { + struct nlmsghdr nh; + struct rtgenmsg g; + } req; + req.nh.nlmsg_type = cmd; + req.nh.nlmsg_len = sizeof(req); + req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; + /* Is it important which PF_* is used for link-level interface scan? + It seems that some information is available only when PF_INET is used. */ + req.g.rtgen_family = (cmd == RTM_GETLINK) ? PF_INET : BIRD_PF; + nl_send(&nl_scan, &req.nh); +} + +static struct nlmsghdr * +nl_get_reply(struct nl_sock *nl) +{ + for(;;) + { + if (!nl->last_hdr) + { + struct iovec iov = { nl->rx_buffer, NL_RX_SIZE }; + struct sockaddr_nl sa; + struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; + int x = recvmsg(nl->fd, &m, 0); + if (x < 0) + die("nl_get_reply: %m"); + if (sa.nl_pid) /* It isn't from the kernel */ + { + DBG("Non-kernel packet\n"); + continue; + } + nl->last_size = x; + nl->last_hdr = (void *) nl->rx_buffer; + if (m.msg_flags & MSG_TRUNC) + bug("nl_get_reply: got truncated reply which should be impossible"); + } + if (NLMSG_OK(nl->last_hdr, nl->last_size)) + { + struct nlmsghdr *h = nl->last_hdr; + nl->last_hdr = NLMSG_NEXT(h, nl->last_size); + if (h->nlmsg_seq != nl->seq) + { + log(L_WARN "nl_get_reply: Ignoring out of sequence netlink packet (%x != %x)", + h->nlmsg_seq, nl->seq); + continue; + } + return h; + } + if (nl->last_size) + log(L_WARN "nl_get_reply: Found packet remnant of size %d", nl->last_size); + nl->last_hdr = NULL; + } +} + +static struct rate_limit rl_netlink_err; + +static int +nl_error(struct nlmsghdr *h) +{ + struct nlmsgerr *e; + int ec; + + if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) + { + log(L_WARN "Netlink: Truncated error message received"); + return ENOBUFS; + } + e = (struct nlmsgerr *) NLMSG_DATA(h); + ec = -e->error; + if (ec) + log_rl(&rl_netlink_err, L_WARN "Netlink: %s", strerror(ec)); + return ec; +} + +static struct nlmsghdr * +nl_get_scan(void) +{ + struct nlmsghdr *h = nl_get_reply(&nl_scan); + + if (h->nlmsg_type == NLMSG_DONE) + return NULL; + if (h->nlmsg_type == NLMSG_ERROR) + { + nl_error(h); + return NULL; + } + return h; +} + +static int +nl_exchange(struct nlmsghdr *pkt) +{ + struct nlmsghdr *h; + + nl_send(&nl_req, pkt); + for(;;) + { + h = nl_get_reply(&nl_req); + if (h->nlmsg_type == NLMSG_ERROR) + break; + log(L_WARN "nl_exchange: Unexpected reply received"); + } + return nl_error(h) ? -1 : 0; +} + +/* + * Netlink attributes + */ + +static int nl_attr_len; + +static void * +nl_checkin(struct nlmsghdr *h, int lsize) +{ + nl_attr_len = h->nlmsg_len - NLMSG_LENGTH(lsize); + if (nl_attr_len < 0) + { + log(L_ERR "nl_checkin: underrun by %d bytes", -nl_attr_len); + return NULL; + } + return NLMSG_DATA(h); +} + +static int +nl_parse_attrs(struct rtattr *a, struct rtattr **k, int ksize) +{ + int max = ksize / sizeof(struct rtattr *); + bzero(k, ksize); + while (RTA_OK(a, nl_attr_len)) + { + if (a->rta_type < max) + k[a->rta_type] = a; + a = RTA_NEXT(a, nl_attr_len); + } + if (nl_attr_len) + { + log(L_ERR "nl_parse_attrs: remnant of size %d", nl_attr_len); + return 0; + } + else + return 1; +} + +void +nl_add_attr(struct nlmsghdr *h, unsigned bufsize, unsigned code, + void *data, unsigned dlen) +{ + unsigned len = RTA_LENGTH(dlen); + unsigned pos = NLMSG_ALIGN(h->nlmsg_len); + struct rtattr *a; + + if (pos + len > bufsize) + bug("nl_add_attr: packet buffer overflow"); + a = (struct rtattr *)((char *)h + pos); + a->rta_type = code; + a->rta_len = len; + h->nlmsg_len = pos + len; + memcpy(RTA_DATA(a), data, dlen); +} + +static inline void +nl_add_attr_u32(struct nlmsghdr *h, unsigned bufsize, int code, u32 data) +{ + nl_add_attr(h, bufsize, code, &data, 4); +} + +static inline void +nl_add_attr_ipa(struct nlmsghdr *h, unsigned bufsize, int code, ip_addr ipa) +{ + ipa_hton(ipa); + nl_add_attr(h, bufsize, code, &ipa, sizeof(ipa)); +} + +#define RTNH_SIZE (sizeof(struct rtnexthop) + sizeof(struct rtattr) + sizeof(ip_addr)) + +static inline void +add_mpnexthop(char *buf, ip_addr ipa, unsigned iface, unsigned char weight) +{ + struct rtnexthop *nh = (void *) buf; + struct rtattr *rt = (void *) (buf + sizeof(*nh)); + nh->rtnh_len = RTNH_SIZE; + nh->rtnh_flags = 0; + nh->rtnh_hops = weight; + nh->rtnh_ifindex = iface; + rt->rta_len = sizeof(*rt) + sizeof(ipa); + rt->rta_type = RTA_GATEWAY; + ipa_hton(ipa); + memcpy(buf + sizeof(*nh) + sizeof(*rt), &ipa, sizeof(ipa)); +} + + +static void +nl_add_multipath(struct nlmsghdr *h, unsigned bufsize, struct mpnh *nh) +{ + unsigned len = sizeof(struct rtattr); + unsigned pos = NLMSG_ALIGN(h->nlmsg_len); + char *buf = (char *)h + pos; + struct rtattr *rt = (void *) buf; + buf += len; + + for (; nh; nh = nh->next) + { + len += RTNH_SIZE; + if (pos + len > bufsize) + bug("nl_add_multipath: packet buffer overflow"); + + add_mpnexthop(buf, nh->gw, nh->iface->index, nh->weight); + buf += RTNH_SIZE; + } + + rt->rta_type = RTA_MULTIPATH; + rt->rta_len = len; + h->nlmsg_len = pos + len; +} + + +static struct mpnh * +nl_parse_multipath(struct krt_proto *p, struct rtattr *ra) +{ + /* Temporary buffer for multicast nexthops */ + static struct mpnh *nh_buffer; + static int nh_buf_size; /* in number of structures */ + static int nh_buf_used; + + struct rtattr *a[RTA_CACHEINFO+1]; + struct rtnexthop *nh = RTA_DATA(ra); + struct mpnh *rv, *first, **last; + int len = RTA_PAYLOAD(ra); + + first = NULL; + last = &first; + nh_buf_used = 0; + + while (len) + { + /* Use RTNH_OK(nh,len) ?? */ + if ((len < sizeof(*nh)) || (len < nh->rtnh_len)) + return NULL; + + if (nh_buf_used == nh_buf_size) + { + nh_buf_size = nh_buf_size ? (nh_buf_size * 2) : 4; + nh_buffer = xrealloc(nh_buffer, nh_buf_size * sizeof(struct mpnh)); + } + *last = rv = nh_buffer + nh_buf_used++; + rv->next = NULL; + last = &(rv->next); + + rv->weight = nh->rtnh_hops; + rv->iface = if_find_by_index(nh->rtnh_ifindex); + if (!rv->iface) + return NULL; + + /* Nonexistent RTNH_PAYLOAD ?? */ + nl_attr_len = nh->rtnh_len - RTNH_LENGTH(0); + nl_parse_attrs(RTNH_DATA(nh), a, sizeof(a)); + if (a[RTA_GATEWAY]) + { + if (RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)) + return NULL; + + memcpy(&rv->gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ip_addr)); + ipa_ntoh(rv->gw); + + neighbor *ng = neigh_find2(&p->p, &rv->gw, rv->iface, + (nh->rtnh_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0); + if (!ng || (ng->scope == SCOPE_HOST)) + return NULL; + } + else + return NULL; + + len -= NLMSG_ALIGN(nh->rtnh_len); + nh = RTNH_NEXT(nh); + } + + return first; +} + + +/* + * Scanning of interfaces + */ + +static void +nl_parse_link(struct nlmsghdr *h, int scan) +{ + struct ifinfomsg *i; + struct rtattr *a[IFLA_WIRELESS+1]; + int new = h->nlmsg_type == RTM_NEWLINK; + struct iface f = {}; + struct iface *ifi; + char *name; + u32 mtu; + unsigned int fl; + + if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFLA_RTA(i), a, sizeof(a))) + return; + if (!a[IFLA_IFNAME] || RTA_PAYLOAD(a[IFLA_IFNAME]) < 2 || + !a[IFLA_MTU] || RTA_PAYLOAD(a[IFLA_MTU]) != 4) + { + if (scan || !a[IFLA_WIRELESS]) + log(L_ERR "nl_parse_link: Malformed message received"); + return; + } + name = RTA_DATA(a[IFLA_IFNAME]); + memcpy(&mtu, RTA_DATA(a[IFLA_MTU]), sizeof(u32)); + + ifi = if_find_by_index(i->ifi_index); + if (!new) + { + DBG("KIF: IF%d(%s) goes down\n", i->ifi_index, name); + if (!ifi) + return; + + if_delete(ifi); + } + else + { + DBG("KIF: IF%d(%s) goes up (mtu=%d,flg=%x)\n", i->ifi_index, name, mtu, i->ifi_flags); + if (ifi && strncmp(ifi->name, name, sizeof(ifi->name)-1)) + if_delete(ifi); + + strncpy(f.name, name, sizeof(f.name)-1); + f.index = i->ifi_index; + f.mtu = mtu; + + fl = i->ifi_flags; + if (fl & IFF_UP) + f.flags |= IF_ADMIN_UP; + if (fl & IFF_LOWER_UP) + f.flags |= IF_LINK_UP; + 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 */ + if_update(&f); + } +} + +static void +nl_parse_addr(struct nlmsghdr *h) +{ + struct ifaddrmsg *i; + struct rtattr *a[IFA_ANYCAST+1]; + int new = h->nlmsg_type == RTM_NEWADDR; + struct ifa ifa; + struct iface *ifi; + int scope; + + if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(IFA_RTA(i), a, sizeof(a))) + return; + if (i->ifa_family != BIRD_AF) + return; + if (!a[IFA_ADDRESS] || RTA_PAYLOAD(a[IFA_ADDRESS]) != sizeof(ip_addr) +#ifdef IPV6 + || a[IFA_LOCAL] && RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr) +#else + || !a[IFA_LOCAL] || RTA_PAYLOAD(a[IFA_LOCAL]) != sizeof(ip_addr) + || (a[IFA_BROADCAST] && RTA_PAYLOAD(a[IFA_BROADCAST]) != sizeof(ip_addr)) +#endif + ) + { + log(L_ERR "nl_parse_addr: Malformed message received"); + return; + } + + ifi = if_find_by_index(i->ifa_index); + if (!ifi) + { + log(L_ERR "KIF: Received address message for unknown interface %d", i->ifa_index); + return; + } + + bzero(&ifa, sizeof(ifa)); + ifa.iface = ifi; + if (i->ifa_flags & IFA_F_SECONDARY) + ifa.flags |= IA_SECONDARY; + + /* IFA_LOCAL can be unset for IPv6 interfaces */ + memcpy(&ifa.ip, RTA_DATA(a[IFA_LOCAL] ? : a[IFA_ADDRESS]), sizeof(ifa.ip)); + ipa_ntoh(ifa.ip); + ifa.pxlen = i->ifa_prefixlen; + if (i->ifa_prefixlen > BITS_PER_IP_ADDRESS) + { + log(L_ERR "KIF: Invalid prefix length for interface %s: %d", ifi->name, i->ifa_prefixlen); + new = 0; + } + if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS) + { + ip_addr addr; + memcpy(&addr, RTA_DATA(a[IFA_ADDRESS]), sizeof(addr)); + ipa_ntoh(addr); + ifa.prefix = ifa.brd = addr; + + /* It is either a host address or a peer address */ + if (ipa_equal(ifa.ip, addr)) + ifa.flags |= IA_HOST; + else + { + ifa.flags |= IA_PEER; + ifa.opposite = addr; + } + } + else + { + ip_addr netmask = ipa_mkmask(ifa.pxlen); + ifa.prefix = ipa_and(ifa.ip, netmask); + ifa.brd = ipa_or(ifa.ip, ipa_not(netmask)); + if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 1) + ifa.opposite = ipa_opposite_m1(ifa.ip); + +#ifndef IPV6 + if (i->ifa_prefixlen == BITS_PER_IP_ADDRESS - 2) + ifa.opposite = ipa_opposite_m2(ifa.ip); + + if ((ifi->flags & IF_BROADCAST) && a[IFA_BROADCAST]) + { + ip_addr xbrd; + memcpy(&xbrd, RTA_DATA(a[IFA_BROADCAST]), sizeof(xbrd)); + ipa_ntoh(xbrd); + if (ipa_equal(xbrd, ifa.prefix) || ipa_equal(xbrd, ifa.brd)) + ifa.brd = xbrd; + else if (ifi->flags & IF_TMP_DOWN) /* Complain only during the first scan */ + log(L_ERR "KIF: Invalid broadcast address %I for %s", xbrd, ifi->name); + } +#endif + } + + scope = ipa_classify(ifa.ip); + if (scope < 0) + { + log(L_ERR "KIF: Invalid interface address %I for %s", ifa.ip, ifi->name); + return; + } + ifa.scope = scope & IADDR_SCOPE_MASK; + + DBG("KIF: IF%d(%s): %s IPA %I, flg %x, net %I/%d, brd %I, opp %I\n", + ifi->index, ifi->name, + new ? "added" : "removed", + ifa.ip, ifa.flags, ifa.prefix, ifa.pxlen, ifa.brd, ifa.opposite); + if (new) + ifa_update(&ifa); + else + ifa_delete(&ifa); +} + +void +kif_do_scan(struct kif_proto *p UNUSED) +{ + struct nlmsghdr *h; + + if_start_update(); + + nl_request_dump(RTM_GETLINK); + while (h = nl_get_scan()) + if (h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK) + nl_parse_link(h, 1); + else + log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type); + + nl_request_dump(RTM_GETADDR); + while (h = nl_get_scan()) + if (h->nlmsg_type == RTM_NEWADDR || h->nlmsg_type == RTM_DELADDR) + nl_parse_addr(h); + else + log(L_DEBUG "nl_scan_ifaces: Unknown packet received (type=%d)", h->nlmsg_type); + + if_end_update(); +} + +/* + * Routes + */ + +static struct krt_proto *nl_table_map[NL_NUM_TABLES]; + +int +krt_capable(rte *e) +{ + rta *a = e->attrs; + + if (a->cast != RTC_UNICAST) + return 0; + + switch (a->dest) + { + case RTD_ROUTER: + case RTD_DEVICE: + if (a->iface == NULL) + return 0; + case RTD_BLACKHOLE: + case RTD_UNREACHABLE: + case RTD_PROHIBIT: + case RTD_MULTIPATH: + break; + default: + return 0; + } + return 1; +} + +static inline int +nh_bufsize(struct mpnh *nh) +{ + int rv = 0; + for (; nh != NULL; nh = nh->next) + rv += RTNH_SIZE; + return rv; +} + +static int +nl_send_route(struct krt_proto *p, rte *e, struct ea_list *eattrs, int new) +{ + eattr *ea; + net *net = e->net; + rta *a = e->attrs; + struct { + struct nlmsghdr h; + struct rtmsg r; + char buf[128 + nh_bufsize(a->nexthops)]; + } r; + + DBG("nl_send_route(%I/%d,new=%d)\n", net->n.prefix, net->n.pxlen, new); + + bzero(&r.h, sizeof(r.h)); + bzero(&r.r, sizeof(r.r)); + r.h.nlmsg_type = new ? RTM_NEWROUTE : RTM_DELROUTE; + r.h.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); + r.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | (new ? NLM_F_CREATE|NLM_F_EXCL : 0); + + r.r.rtm_family = BIRD_AF; + r.r.rtm_dst_len = net->n.pxlen; + r.r.rtm_tos = 0; + r.r.rtm_table = KRT_CF->sys.table_id; + r.r.rtm_protocol = RTPROT_BIRD; + r.r.rtm_scope = RT_SCOPE_UNIVERSE; + nl_add_attr_ipa(&r.h, sizeof(r), RTA_DST, net->n.prefix); + + u32 metric = 0; + if (new && e->attrs->source == RTS_INHERIT) + metric = e->u.krt.metric; + if (ea = ea_find(eattrs, EA_KRT_METRIC)) + metric = ea->u.data; + if (metric != 0) + nl_add_attr_u32(&r.h, sizeof(r), RTA_PRIORITY, metric); + + if (ea = ea_find(eattrs, EA_KRT_PREFSRC)) + nl_add_attr_ipa(&r.h, sizeof(r), RTA_PREFSRC, *(ip_addr *)ea->u.ptr->data); + + if (ea = ea_find(eattrs, EA_KRT_REALM)) + nl_add_attr_u32(&r.h, sizeof(r), RTA_FLOW, ea->u.data); + + /* a->iface != NULL checked in krt_capable() for router and device routes */ + + switch (a->dest) + { + case RTD_ROUTER: + r.r.rtm_type = RTN_UNICAST; + nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index); + nl_add_attr_ipa(&r.h, sizeof(r), RTA_GATEWAY, a->gw); + break; + case RTD_DEVICE: + r.r.rtm_type = RTN_UNICAST; + nl_add_attr_u32(&r.h, sizeof(r), RTA_OIF, a->iface->index); + break; + case RTD_BLACKHOLE: + r.r.rtm_type = RTN_BLACKHOLE; + break; + case RTD_UNREACHABLE: + r.r.rtm_type = RTN_UNREACHABLE; + break; + case RTD_PROHIBIT: + r.r.rtm_type = RTN_PROHIBIT; + break; + case RTD_MULTIPATH: + r.r.rtm_type = RTN_UNICAST; + nl_add_multipath(&r.h, sizeof(r), a->nexthops); + break; + default: + bug("krt_capable inconsistent with nl_send_route"); + } + + return nl_exchange(&r.h); +} + +void +krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old, struct ea_list *eattrs) +{ + int err = 0; + + /* + * NULL for eattr of the old route is a little hack, but we don't + * get proper eattrs for old in rt_notify() anyway. NULL means no + * extended route attributes and therefore matches if the kernel + * route has any of them. + */ + + if (old) + nl_send_route(p, old, NULL, 0); + + if (new) + err = nl_send_route(p, new, eattrs, 1); + + if (err < 0) + n->n.flags |= KRF_SYNC_ERROR; + else + n->n.flags &= ~KRF_SYNC_ERROR; +} + + +#define SKIP(ARG...) do { DBG("KRT: Ignoring route - " ARG); return; } while(0) + +static void +nl_parse_route(struct nlmsghdr *h, int scan) +{ + struct krt_proto *p; + struct rtmsg *i; + struct rtattr *a[RTA_CACHEINFO+1]; + int new = h->nlmsg_type == RTM_NEWROUTE; + + ip_addr dst = IPA_NONE; + u32 oif = ~0; + int src; + + if (!(i = nl_checkin(h, sizeof(*i))) || !nl_parse_attrs(RTM_RTA(i), a, sizeof(a))) + return; + if (i->rtm_family != BIRD_AF) + return; + if ((a[RTA_DST] && RTA_PAYLOAD(a[RTA_DST]) != sizeof(ip_addr)) || +#ifdef IPV6 + (a[RTA_IIF] && RTA_PAYLOAD(a[RTA_IIF]) != 4) || +#endif + (a[RTA_OIF] && RTA_PAYLOAD(a[RTA_OIF]) != 4) || + (a[RTA_GATEWAY] && RTA_PAYLOAD(a[RTA_GATEWAY]) != sizeof(ip_addr)) || + (a[RTA_PRIORITY] && RTA_PAYLOAD(a[RTA_PRIORITY]) != 4) || + (a[RTA_PREFSRC] && RTA_PAYLOAD(a[RTA_PREFSRC]) != sizeof(ip_addr)) || + (a[RTA_FLOW] && RTA_PAYLOAD(a[RTA_OIF]) != 4)) + { + log(L_ERR "KRT: Malformed message received"); + return; + } + + if (a[RTA_DST]) + { + memcpy(&dst, RTA_DATA(a[RTA_DST]), sizeof(dst)); + ipa_ntoh(dst); + } + + if (a[RTA_OIF]) + memcpy(&oif, RTA_DATA(a[RTA_OIF]), sizeof(oif)); + + p = nl_table_map[i->rtm_table]; /* Do we know this table? */ + DBG("KRT: Got %I/%d, type=%d, oif=%d, table=%d, prid=%d, proto=%s\n", dst, i->rtm_dst_len, i->rtm_type, oif, i->rtm_table, i->rtm_protocol, p ? p->p.name : "(none)"); + if (!p) + SKIP("unknown table %d\n", i->rtm_table); + + +#ifdef IPV6 + if (a[RTA_IIF]) + SKIP("IIF set\n"); +#else + if (i->rtm_tos != 0) /* We don't support TOS */ + SKIP("TOS %02x\n", i->rtm_tos); +#endif + + if (scan && !new) + SKIP("RTM_DELROUTE in scan\n"); + + int c = ipa_classify_net(dst); + if ((c < 0) || !(c & IADDR_HOST) || ((c & IADDR_SCOPE_MASK) <= SCOPE_LINK)) + SKIP("strange class/scope\n"); + + // ignore rtm_scope, it is not a real scope + // if (i->rtm_scope != RT_SCOPE_UNIVERSE) + // SKIP("scope %u\n", i->rtm_scope); + + switch (i->rtm_protocol) + { + case RTPROT_UNSPEC: + SKIP("proto unspec\n"); + + case RTPROT_REDIRECT: + src = KRT_SRC_REDIRECT; + break; + + case RTPROT_KERNEL: + src = KRT_SRC_KERNEL; + return; + + case RTPROT_BIRD: + if (!scan) + SKIP("echo\n"); + src = KRT_SRC_BIRD; + break; + + case RTPROT_BOOT: + default: + src = KRT_SRC_ALIEN; + } + + net *net = net_get(p->p.table, dst, i->rtm_dst_len); + + rta ra = { + .proto = &p->p, + .source = RTS_INHERIT, + .scope = SCOPE_UNIVERSE, + .cast = RTC_UNICAST + }; + + switch (i->rtm_type) + { + case RTN_UNICAST: + + if (a[RTA_MULTIPATH]) + { + ra.dest = RTD_MULTIPATH; + ra.nexthops = nl_parse_multipath(p, a[RTA_MULTIPATH]); + if (!ra.nexthops) + { + log(L_ERR "KRT: Received strange multipath route %I/%d", + net->n.prefix, net->n.pxlen); + return; + } + + break; + } + + ra.iface = if_find_by_index(oif); + if (!ra.iface) + { + log(L_ERR "KRT: Received route %I/%d with unknown ifindex %u", + net->n.prefix, net->n.pxlen, oif); + return; + } + + if (a[RTA_GATEWAY]) + { + neighbor *ng; + ra.dest = RTD_ROUTER; + memcpy(&ra.gw, RTA_DATA(a[RTA_GATEWAY]), sizeof(ra.gw)); + ipa_ntoh(ra.gw); + + /* Silently skip strange 6to4 routes */ + if (ipa_in_net(ra.gw, IPA_NONE, 96)) + return; + + ng = neigh_find2(&p->p, &ra.gw, ra.iface, + (i->rtm_flags & RTNH_F_ONLINK) ? NEF_ONLINK : 0); + if (!ng || (ng->scope == SCOPE_HOST)) + { + log(L_ERR "KRT: Received route %I/%d with strange next-hop %I", + net->n.prefix, net->n.pxlen, ra.gw); + return; + } + } + else + { + ra.dest = RTD_DEVICE; + + /* + * In Linux IPv6, 'native' device routes have proto + * RTPROT_BOOT and not RTPROT_KERNEL (which they have in + * IPv4 and which is expected). We cannot distinguish + * 'native' and user defined device routes, so we ignore all + * such device routes and for consistency, we have the same + * behavior in IPv4. Anyway, users should use RTPROT_STATIC + * for their 'alien' routes. + */ + + if (i->rtm_protocol == RTPROT_BOOT) + src = KRT_SRC_KERNEL; + } + + break; + case RTN_BLACKHOLE: + ra.dest = RTD_BLACKHOLE; + break; + case RTN_UNREACHABLE: + ra.dest = RTD_UNREACHABLE; + break; + case RTN_PROHIBIT: + ra.dest = RTD_PROHIBIT; + break; + /* FIXME: What about RTN_THROW? */ + default: + SKIP("type %d\n", i->rtm_type); + return; + } + + rte *e = rte_get_temp(&ra); + e->net = net; + e->u.krt.src = src; + e->u.krt.proto = i->rtm_protocol; + e->u.krt.type = i->rtm_type; + + if (a[RTA_PRIORITY]) + memcpy(&e->u.krt.metric, RTA_DATA(a[RTA_PRIORITY]), sizeof(e->u.krt.metric)); + else + e->u.krt.metric = 0; + + if (a[RTA_PREFSRC]) + { + ip_addr ps; + memcpy(&ps, RTA_DATA(a[RTA_PREFSRC]), sizeof(ps)); + ipa_ntoh(ps); + + ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr)); + ea->next = ra.eattrs; + ra.eattrs = ea; + ea->flags = EALF_SORTED; + ea->count = 1; + ea->attrs[0].id = EA_KRT_PREFSRC; + ea->attrs[0].flags = 0; + ea->attrs[0].type = EAF_TYPE_IP_ADDRESS; + ea->attrs[0].u.ptr = alloca(sizeof(struct adata) + sizeof(ps)); + ea->attrs[0].u.ptr->length = sizeof(ps); + memcpy(ea->attrs[0].u.ptr->data, &ps, sizeof(ps)); + } + + if (a[RTA_FLOW]) + { + ea_list *ea = alloca(sizeof(ea_list) + sizeof(eattr)); + ea->next = ra.eattrs; + ra.eattrs = ea; + ea->flags = EALF_SORTED; + ea->count = 1; + ea->attrs[0].id = EA_KRT_REALM; + ea->attrs[0].flags = 0; + ea->attrs[0].type = EAF_TYPE_INT; + memcpy(&ea->attrs[0].u.data, RTA_DATA(a[RTA_FLOW]), 4); + } + + if (scan) + krt_got_route(p, e); + else + krt_got_route_async(p, e, new); +} + +void +krt_do_scan(struct krt_proto *p UNUSED) /* CONFIG_ALL_TABLES_AT_ONCE => p is NULL */ +{ + struct nlmsghdr *h; + + nl_request_dump(RTM_GETROUTE); + while (h = nl_get_scan()) + if (h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE) + nl_parse_route(h, 1); + else + log(L_DEBUG "nl_scan_fire: Unknown packet received (type=%d)", h->nlmsg_type); +} + +/* + * Asynchronous Netlink interface + */ + +static sock *nl_async_sk; /* BIRD socket for asynchronous notifications */ +static byte *nl_async_rx_buffer; /* Receive buffer */ + +static void +nl_async_msg(struct nlmsghdr *h) +{ + switch (h->nlmsg_type) + { + case RTM_NEWROUTE: + case RTM_DELROUTE: + DBG("KRT: Received async route notification (%d)\n", h->nlmsg_type); + nl_parse_route(h, 0); + break; + case RTM_NEWLINK: + case RTM_DELLINK: + DBG("KRT: Received async link notification (%d)\n", h->nlmsg_type); + nl_parse_link(h, 0); + break; + case RTM_NEWADDR: + case RTM_DELADDR: + DBG("KRT: Received async address notification (%d)\n", h->nlmsg_type); + nl_parse_addr(h); + break; + default: + DBG("KRT: Received unknown async notification (%d)\n", h->nlmsg_type); + } +} + +static int +nl_async_hook(sock *sk, int size UNUSED) +{ + struct iovec iov = { nl_async_rx_buffer, NL_RX_SIZE }; + struct sockaddr_nl sa; + struct msghdr m = { (struct sockaddr *) &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; + struct nlmsghdr *h; + int x; + unsigned int len; + + x = recvmsg(sk->fd, &m, 0); + if (x < 0) + { + if (errno == ENOBUFS) + { + /* + * Netlink reports some packets have been thrown away. + * One day we might react to it by asking for route table + * scan in near future. + */ + return 1; /* More data are likely to be ready */ + } + else if (errno != EWOULDBLOCK) + log(L_ERR "Netlink recvmsg: %m"); + return 0; + } + if (sa.nl_pid) /* It isn't from the kernel */ + { + DBG("Non-kernel packet\n"); + return 1; + } + h = (void *) nl_async_rx_buffer; + len = x; + if (m.msg_flags & MSG_TRUNC) + { + log(L_WARN "Netlink got truncated asynchronous message"); + return 1; + } + while (NLMSG_OK(h, len)) + { + nl_async_msg(h); + h = NLMSG_NEXT(h, len); + } + if (len) + log(L_WARN "nl_async_hook: Found packet remnant of size %d", len); + return 1; +} + +static void +nl_open_async(void) +{ + sock *sk; + struct sockaddr_nl sa; + int fd; + static int nl_open_tried = 0; + + if (nl_open_tried) + return; + nl_open_tried = 1; + + DBG("KRT: Opening async netlink socket\n"); + + fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + if (fd < 0) + { + log(L_ERR "Unable to open asynchronous rtnetlink socket: %m"); + return; + } + + bzero(&sa, sizeof(sa)); + sa.nl_family = AF_NETLINK; +#ifdef IPV6 + sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE; +#else + sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE; +#endif + if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) + { + log(L_ERR "Unable to bind asynchronous rtnetlink socket: %m"); + return; + } + + sk = nl_async_sk = sk_new(krt_pool); + sk->type = SK_MAGIC; + sk->rx_hook = nl_async_hook; + sk->fd = fd; + if (sk_open(sk)) + bug("Netlink: sk_open failed"); + + if (!nl_async_rx_buffer) + nl_async_rx_buffer = xmalloc(NL_RX_SIZE); +} + +/* + * Interface to the UNIX krt module + */ + +static u8 nl_cf_table[(NL_NUM_TABLES+7) / 8]; + +void +krt_sys_start(struct krt_proto *p, int first) +{ + nl_table_map[KRT_CF->sys.table_id] = p; + if (first) + { + nl_open(); + nl_open_async(); + } +} + +void +krt_sys_shutdown(struct krt_proto *p UNUSED, int last UNUSED) +{ +} + +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; +} + + +void +krt_sys_preconfig(struct config *c UNUSED) +{ + bzero(&nl_cf_table, sizeof(nl_cf_table)); +} + +void +krt_sys_postconfig(struct krt_config *x) +{ + int id = x->sys.table_id; + + if (nl_cf_table[id/8] & (1 << (id%8))) + cf_error("Multiple kernel syncers defined for table #%d", id); + nl_cf_table[id/8] |= (1 << (id%8)); +} + +void +krt_sys_init_config(struct krt_config *cf) +{ + cf->sys.table_id = RT_TABLE_MAIN; +} + +void +krt_sys_copy_config(struct krt_config *d, struct krt_config *s) +{ + d->sys.table_id = s->sys.table_id; +} + + + +void +kif_sys_start(struct kif_proto *p UNUSED) +{ + nl_open(); + nl_open_async(); +} + +void +kif_sys_shutdown(struct kif_proto *p UNUSED) +{ +}