diff options
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/ldpd/kroute.c | 466 | ||||
-rw-r--r-- | usr.sbin/ldpd/lde.c | 11 | ||||
-rw-r--r-- | usr.sbin/ldpd/lde_lib.c | 8 | ||||
-rw-r--r-- | usr.sbin/ldpd/ldpd.c | 11 | ||||
-rw-r--r-- | usr.sbin/ldpd/ldpd.h | 19 |
5 files changed, 244 insertions, 271 deletions
diff --git a/usr.sbin/ldpd/kroute.c b/usr.sbin/ldpd/kroute.c index 6030549d87c..54565af283d 100644 --- a/usr.sbin/ldpd/kroute.c +++ b/usr.sbin/ldpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.6 2010/02/19 12:47:29 claudio Exp $ */ +/* $OpenBSD: kroute.c,v 1.7 2010/03/03 10:17:05 claudio Exp $ */ /* * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> @@ -53,6 +53,7 @@ struct { struct kroute_node { RB_ENTRY(kroute_node) entry; struct kroute r; + struct kroute_node *next; }; struct kif_node { @@ -62,14 +63,15 @@ struct kif_node { }; void kr_redist_remove(struct kroute_node *); -int kr_redist_eval(struct kroute *, struct rroute *); +int kr_redist_eval(struct kroute *, struct kroute *); void kr_redistribute(struct kroute_node *); int kroute_compare(struct kroute_node *, struct kroute_node *); int kif_compare(struct kif_node *, struct kif_node *); int kr_change_fib(struct kroute_node *, struct kroute *, int); int kr_delete_fib(struct kroute_node *); -struct kroute_node *kroute_find(in_addr_t, u_int8_t); +struct kroute_node *kroute_find(in_addr_t, u_int8_t, u_int8_t); +struct kroute_node *kroute_matchgw(struct kroute_node *, struct in_addr); int kroute_insert(struct kroute_node *); int kroute_remove(struct kroute_node *); void kroute_clear(void); @@ -92,11 +94,11 @@ void if_newaddr(u_short, struct sockaddr_in *, struct sockaddr_in *, struct sockaddr_in *); void if_announce(void *); -int send_rtmsg(int, int, struct kroute *); -int send_rtlabelmsg(int, int, struct kroute *, u_int32_t); +int send_rtmsg(int, int, struct kroute *, u_int32_t); int dispatch_rtmsg(void); int fetchtable(void); int fetchifs(u_short); +int rtmsg_process(char *, int); RB_HEAD(kroute_tree, kroute_node) krt; RB_PROTOTYPE(kroute_tree, kroute_node, entry, kroute_compare) @@ -114,6 +116,9 @@ int kif_init(void) { RB_INIT(&kit); + /* init also krt tree so that we can call kr_shutdown() */ + RB_INIT(&krt); + kr_state.fib_sync = 0; /* decoupled */ if (fetchifs(0) == -1) return (-1); @@ -155,8 +160,6 @@ kr_init(int fs) kr_state.pid = getpid(); kr_state.rtseq = 1; - RB_INIT(&krt); - if (fetchtable() == -1) return (-1); @@ -166,9 +169,10 @@ kr_init(int fs) kr_all_routers.prefix.s_addr = inet_addr(AllRouters); kr_all_routers.prefixlen = mask2prefixlen(INADDR_BROADCAST); kr_all_routers.nexthop.s_addr = htonl(INADDR_LOOPBACK); + kr_all_routers.remote_label = NO_LABEL; kr_state.fib_sync = 1; /* force addition of multicast route */ - if (send_rtmsg(kr_state.fd, RTM_ADD, &kr_all_routers) != -1) + if (send_rtmsg(kr_state.fd, RTM_ADD, &kr_all_routers, AF_INET) != -1) flag_all_routers = 1; kr_state.fib_sync = fs; /* now set correct sync mode */ @@ -183,46 +187,30 @@ kr_init(int fs) int kr_change_fib(struct kroute_node *kr, struct kroute *kroute, int action) { - struct kroute_node *kn; - /* nexthop within 127/8 -> ignore silently */ if ((kroute->nexthop.s_addr & htonl(IN_CLASSA_NET)) == htonl(INADDR_LOOPBACK & IN_CLASSA_NET)) return (0); /* send update */ - if (send_rtlabelmsg(kr_state.fd, action, kroute, AF_MPLS) == -1) + if (send_rtmsg(kr_state.fd, action, kroute, AF_MPLS) == -1) return (-1); - if (kroute->nexthop.s_addr != NULL) { - if (send_rtlabelmsg(kr_state.fd, action, kroute, - AF_INET) == -1) + if (kroute->nexthop.s_addr != INADDR_ANY && + kroute->remote_label != NO_LABEL) { + if (send_rtmsg(kr_state.fd, RTM_CHANGE, kroute, AF_INET) == -1) return (-1); } - /* create new entry unless we are changing the first entry */ - if (action == RTM_ADD) { - if ((kn = calloc(1, sizeof(*kn))) == NULL) - fatal(NULL); - } else - kn = kr; - - kn->r.prefix.s_addr = kroute->prefix.s_addr; - kn->r.prefixlen = kroute->prefixlen; - kn->r.local_label = kroute->local_label; - kn->r.remote_label = kroute->remote_label; - kn->r.nexthop.s_addr = kroute->nexthop.s_addr; - kn->r.flags = kroute->flags | F_LDPD_INSERTED; - kn->r.ext_tag = kroute->ext_tag; - rtlabel_unref(kn->r.rtlabel); /* for RTM_CHANGE */ - kn->r.rtlabel = kroute->rtlabel; - - if (action == RTM_ADD) { - if (kroute_insert(kn) == -1) { - log_debug("kr_update_fib: cannot insert %s", - inet_ntoa(kn->r.nexthop)); - } - } + kr->r.prefix.s_addr = kroute->prefix.s_addr; + kr->r.prefixlen = kroute->prefixlen; + kr->r.local_label = kroute->local_label; + kr->r.remote_label = kroute->remote_label; + kr->r.nexthop.s_addr = kroute->nexthop.s_addr; + kr->r.flags = kroute->flags | F_LDPD_INSERTED; + kr->r.ext_tag = kroute->ext_tag; + rtlabel_unref(kr->r.rtlabel); /* for RTM_CHANGE */ + kr->r.rtlabel = kroute->rtlabel; return (0); } @@ -235,9 +223,15 @@ kr_change(struct kroute *kroute) kroute->rtlabel = rtlabel_tag2id(kroute->ext_tag); - kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen); + kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen, RTP_ANY); + + if (kr == NULL) { + log_warnx("kr_change: lost FEC %s/%d", + inet_ntoa(kroute->prefix), kroute->prefixlen); + return (-1); + } - if (kr != NULL && kr->r.flags & F_KERNEL) + if (kr->r.flags & F_LDPD_INSERTED) action = RTM_CHANGE; return (kr_change_fib(kr, kroute, action)); @@ -255,7 +249,7 @@ kr_delete_fib(struct kroute_node *kr) return (0); } - if (send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r) == -1) + if (send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r, AF_MPLS) == -1) return (-1); if (kroute_remove(kr) == -1) @@ -267,15 +261,21 @@ kr_delete_fib(struct kroute_node *kr) int kr_delete(struct kroute *kroute) { - struct kroute_node *kr; + struct kroute_node *kr, *nkr; - if ((kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen)) == - NULL) + if ((kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen, + kroute->priority)) == NULL) return (0); if (kr_delete_fib(kr) == -1) return (-1); + while (kr != NULL) { + nkr = kr->next; + if (kr_delete_fib(kr) == -1) + return (-1); + kr = nkr; + } return (0); } @@ -286,7 +286,8 @@ kr_shutdown(void) if (flag_all_routers) { kr_state.fib_sync = 1; /* force removal of mulitcast route */ - (void)send_rtmsg(kr_state.fd, RTM_DELETE, &kr_all_routers); + (void)send_rtmsg(kr_state.fd, RTM_DELETE, &kr_all_routers, + AF_INET); } kroute_clear(); @@ -304,8 +305,15 @@ kr_lfib_couple(void) kr_state.fib_sync = 1; RB_FOREACH(kr, kroute_tree, &krt) - if (!(kr->r.flags & F_KERNEL)) - send_rtmsg(kr_state.fd, RTM_ADD, &kr->r); + if (kr->r.flags & F_LDPD_INSERTED) { + send_rtmsg(kr_state.fd, RTM_ADD, &kr->r, AF_MPLS); + + if (kr->r.nexthop.s_addr != INADDR_ANY && + kr->r.remote_label != NO_LABEL) { + send_rtmsg(kr_state.fd, RTM_CHANGE, + &kr->r, AF_INET); + } + } log_info("kernel routing table coupled"); } @@ -314,18 +322,23 @@ void kr_lfib_decouple(void) { struct kroute_node *kr; + u_int32_t rl; if (kr_state.fib_sync == 0) /* already decoupled */ return; RB_FOREACH(kr, kroute_tree, &krt) { if (kr->r.flags & F_LDPD_INSERTED) { - send_rtlabelmsg(kr_state.fd, RTM_DELETE, + send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r, AF_MPLS); - if (kr->r.nexthop.s_addr != NULL) { - send_rtlabelmsg(kr_state.fd, RTM_DELETE, + if (kr->r.nexthop.s_addr != INADDR_ANY && + kr->r.remote_label != NO_LABEL) { + rl = kr->r.remote_label; + kr->r.remote_label = NO_LABEL; + send_rtmsg(kr_state.fd, RTM_CHANGE, &kr->r, AF_INET); + kr->r.remote_label = rl; } } } @@ -345,7 +358,7 @@ kr_dispatch_msg(int fd, short event, void *bula) void kr_show_route(struct imsg *imsg) { - struct kroute_node *kr; + struct kroute_node *kr, *kn; int flags; struct in_addr addr; @@ -358,8 +371,12 @@ kr_show_route(struct imsg *imsg) memcpy(&flags, imsg->data, sizeof(flags)); RB_FOREACH(kr, kroute_tree, &krt) if (!flags || kr->r.flags & flags) { - main_imsg_compose_ldpe(IMSG_CTL_KROUTE, - imsg->hdr.pid, &kr->r, sizeof(kr->r)); + kn = kr; + do { + main_imsg_compose_ldpe(IMSG_CTL_KROUTE, + imsg->hdr.pid, + &kr->r, sizeof(kr->r)); + } while ((kn = kn->next) != NULL); } break; case IMSG_CTL_KROUTE_ADDR: @@ -400,26 +417,23 @@ kr_ifinfo(char *ifname, pid_t pid) void kr_redist_remove(struct kroute_node *kn) { - struct rroute rr; - /* was the route redistributed? */ if ((kn->r.flags & F_REDISTRIBUTED) == 0) return; /* remove redistributed flag */ kn->r.flags &= ~F_REDISTRIBUTED; - rr.kr = kn->r; if (kn == NULL) { - main_imsg_compose_lde(IMSG_NETWORK_DEL, 0, &rr.kr, - sizeof(struct rroute)); + main_imsg_compose_lde(IMSG_NETWORK_DEL, 0, &kn->r, + sizeof(struct kroute)); } } int -kr_redist_eval(struct kroute *kr, struct rroute *rr) +kr_redist_eval(struct kroute *kr, struct kroute *orig) { - u_int32_t a, metric = 0; + u_int32_t a; /* Only static routes are considered for redistribution. */ if (!(kr->flags & F_KERNEL)) @@ -442,21 +456,16 @@ kr_redist_eval(struct kroute *kr, struct rroute *rr) (a >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) goto dont_redistribute; /* - * Consider networks with nexthop loopback as not redistributable. + * Consider networks with nexthop loopback as not redistributable + * unless it is a reject or blackhole route. */ - if (kr->nexthop.s_addr == htonl(INADDR_LOOPBACK)) + if (kr->nexthop.s_addr == htonl(INADDR_LOOPBACK) && + !(kr->flags & (F_BLACKHOLE|F_REJECT))) goto dont_redistribute; /* prefix should be redistributed */ kr->flags |= F_REDISTRIBUTED; - /* - * only on of all multipath routes can be redistributed so - * redistribute the best one. - */ - if (rr->metric > metric) { - rr->kr = *kr; - rr->metric = metric; - } + *orig = *kr; return (1); dont_redistribute: @@ -471,20 +480,23 @@ dont_redistribute: void kr_redistribute(struct kroute_node *kh) { - struct rroute rr; + struct kroute kr; - bzero(&rr, sizeof(rr)); - rr.metric = UINT_MAX; - if (!kr_redist_eval(&kh->r, &rr)) + /* only the highest prio route can be redistributed */ + if (kroute_find(kh->r.prefix.s_addr, kh->r.prefixlen, RTP_ANY) != kh) return; - if (rr.kr.flags & F_REDISTRIBUTED) { - main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &rr, - sizeof(struct rroute)); + bzero(&kr, sizeof(kr)); + if (!kr_redist_eval(&kh->r, &kr)) + return; + + if (kr.flags & F_REDISTRIBUTED) { + main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr, + sizeof(struct kroute)); } else { - rr.kr = kh->r; - main_imsg_compose_lde(IMSG_NETWORK_DEL, 0, &rr.kr, - sizeof(struct rroute)); + kr = kh->r; + main_imsg_compose_lde(IMSG_NETWORK_DEL, 0, &kr, + sizeof(struct kroute)); } } @@ -511,6 +523,14 @@ kroute_compare(struct kroute_node *a, struct kroute_node *b) return (-1); if (a->r.prefixlen > b->r.prefixlen) return (1); + + /* if the priority is RTP_ANY finish on the first address hit */ + if (a->r.priority == RTP_ANY || b->r.priority == RTP_ANY) + return (0); + if (a->r.priority < b->r.priority) + return (-1); + if (a->r.priority > b->r.priority) + return (1); return (0); } @@ -522,25 +542,60 @@ kif_compare(struct kif_node *a, struct kif_node *b) /* tree management */ struct kroute_node * -kroute_find(in_addr_t prefix, u_int8_t prefixlen) +kroute_find(in_addr_t prefix, u_int8_t prefixlen, u_int8_t prio) { struct kroute_node s; + struct kroute_node *kn, *tmp; s.r.prefix.s_addr = prefix; s.r.prefixlen = prefixlen; + s.r.priority = prio; + + kn = RB_FIND(kroute_tree, &krt, &s); + if (kn && prio == RTP_ANY) { + tmp = RB_PREV(kroute_tree, &krt, kn); + while (tmp) { + if (kroute_compare(&s, tmp) == 0) + kn = tmp; + else + break; + tmp = RB_PREV(kroute_tree, &krt, kn); + } + } + return (kn); +} + +struct kroute_node * +kroute_matchgw(struct kroute_node *kr, struct in_addr nh) +{ + in_addr_t nexthop; + + nexthop = nh.s_addr; + + while (kr) { + if (kr->r.nexthop.s_addr == nexthop) + return (kr); + kr = kr->next; + } - return (RB_FIND(kroute_tree, &krt, &s)); + return (NULL); } int kroute_insert(struct kroute_node *kr) { - if (RB_INSERT(kroute_tree, &krt, kr) != NULL) { - log_warnx("kroute_insert failed for %s/%u", - inet_ntoa(kr->r.prefix), kr->r.prefixlen); - free(kr); - return (-1); - } + struct kroute_node *krm; + + if ((krm = RB_INSERT(kroute_tree, &krt, kr)) != NULL) { + /* + * Multipath route, add at end of list. + */ + while (krm->next != NULL) + krm = krm->next; + krm->next = kr; + kr->next = NULL; /* to be sure */ + } else + krm = kr; if (!(kr->r.flags & F_KERNEL)) { kr->r.flags &= ~F_DOWN; @@ -552,7 +607,7 @@ kroute_insert(struct kroute_node *kr) else kr->r.flags |= F_DOWN; - kr_redistribute(kr); + kr_redistribute(krm); return (0); } @@ -561,7 +616,7 @@ kroute_insert_label(struct kroute *kr) { struct kroute_node *krn; - krn = kroute_find(kr->prefix.s_addr, kr->prefixlen); + krn = kroute_find(kr->prefix.s_addr, kr->prefixlen, RTP_ANY); if (krn == NULL) { log_debug("kroute_insert_label: prefix %s/%d not present", inet_ntoa(kr->prefix), kr->prefixlen); @@ -572,10 +627,10 @@ kroute_insert_label(struct kroute *kr) krn->r.local_label = kr->local_label; krn->r.remote_label = kr->remote_label; - send_rtlabelmsg(kr_state.fd, RTM_ADD, kr, AF_MPLS); + send_rtmsg(kr_state.fd, RTM_ADD, kr, AF_MPLS); - if (kr->nexthop.s_addr != INADDR_ANY) - send_rtlabelmsg(kr_state.fd, RTM_ADD, kr, AF_INET); + if (kr->nexthop.s_addr != INADDR_ANY && kr->remote_label != NO_LABEL) + send_rtmsg(kr_state.fd, RTM_CHANGE, kr, AF_INET); return (0); } @@ -583,12 +638,41 @@ kroute_insert_label(struct kroute *kr) int kroute_remove(struct kroute_node *kr) { - if (RB_REMOVE(kroute_tree, &krt, kr) == NULL) { - log_warnx("kroute_remove failed for %s/%u", + struct kroute_node *krm; + + if ((krm = RB_FIND(kroute_tree, &krt, kr)) == NULL) { + log_warnx("kroute_remove failed to find %s/%u", inet_ntoa(kr->r.prefix), kr->r.prefixlen); return (-1); } + if (krm == kr) { + /* head element */ + if (RB_REMOVE(kroute_tree, &krt, kr) == NULL) { + log_warnx("kroute_remove failed for %s/%u", + inet_ntoa(kr->r.prefix), kr->r.prefixlen); + return (-1); + } + if (kr->next != NULL) { + if (RB_INSERT(kroute_tree, &krt, kr->next) != NULL) { + log_warnx("kroute_remove failed to add %s/%u", + inet_ntoa(kr->r.prefix), kr->r.prefixlen); + return (-1); + } + } + } else { + /* somewhere in the list */ + while (krm->next != kr && krm->next != NULL) + krm = krm->next; + if (krm->next == NULL) { + log_warnx("kroute_remove multipath list corrupted " + "for %s/%u", inet_ntoa(kr->r.prefix), + kr->r.prefixlen); + return (-1); + } + krm->next = kr->next; + } + kr_redist_remove(kr); rtlabel_unref(kr->r.rtlabel); @@ -733,11 +817,12 @@ kroute_match(in_addr_t key) /* we will never match the default route */ for (i = 32; i > 0; i--) - if ((kr = kroute_find(key & prefixlen2mask(i), i)) != NULL) + if ((kr = kroute_find(key & prefixlen2mask(i), i, + RTP_ANY)) != NULL) return (kr); /* if we don't have a match yet, try to find a default route */ - if ((kr = kroute_find(0, 0)) != NULL) + if ((kr = kroute_find(0, 0, RTP_ANY)) != NULL) return (kr); return (NULL); @@ -754,9 +839,11 @@ protect_lo(void) log_warn("protect_lo"); return (-1); } - kr->r.prefix.s_addr = htonl(INADDR_LOOPBACK); + kr->r.prefix.s_addr = htonl(INADDR_LOOPBACK & IN_CLASSA_NET); kr->r.prefixlen = 8; kr->r.flags = F_KERNEL|F_CONNECTED; + kr->r.local_label = NO_LABEL; + kr->r.remote_label = NO_LABEL; if (RB_INSERT(kroute_tree, &krt, kr) != NULL) free(kr); /* kernel route already there, no problem */ @@ -799,8 +886,8 @@ prefixlen2mask(u_int8_t prefixlen) return (htonl(0xffffffff << (32 - prefixlen))); } -#define ROUNDUP(a, size) \ - (((a) & ((size) - 1)) ? (1 + ((a) | ((size) - 1))) : (a)) +#define ROUNDUP(a) \ + (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a)) void get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) @@ -811,7 +898,7 @@ get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) if (addrs & (1 << i)) { rti_info[i] = sa; sa = (struct sockaddr *)((char *)(sa) + - ROUNDUP(sa->sa_len, sizeof(long))); + ROUNDUP(sa->sa_len)); } else rti_info[i] = NULL; } @@ -905,7 +992,7 @@ if_announce(void *msg) /* rtsock */ int -send_rtlabelmsg(int fd, int action, struct kroute *kroute, u_int32_t family) +send_rtmsg(int fd, int action, struct kroute *kroute, u_int32_t family) { struct iovec iov[5]; struct rt_msghdr hdr; @@ -927,22 +1014,12 @@ send_rtlabelmsg(int fd, int action, struct kroute *kroute, u_int32_t family) return (0); } - if (family == AF_INET && kroute->remote_label == 0) - return (0); - /* initialize header */ bzero(&hdr, sizeof(hdr)); hdr.rtm_version = RTM_VERSION; - /* ldpd must not add/delete AF_INET routes, instead it should just - modify existing ones adding/remove relevant MPLS infos */ - if (family == AF_INET) - hdr.rtm_type = RTM_CHANGE; - else - hdr.rtm_type = action; - + hdr.rtm_type = action; hdr.rtm_flags = RTF_UP; - hdr.rtm_priority = 0; hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */ hdr.rtm_msglen = sizeof(hdr); hdr.rtm_hdrlen = sizeof(struct rt_msghdr); @@ -956,6 +1033,7 @@ send_rtlabelmsg(int fd, int action, struct kroute *kroute, u_int32_t family) label_in.smpls_family = AF_MPLS; label_in.smpls_label = kroute->local_label; /* adjust header */ + hdr.rtm_priority = RTP_DEFAULT; hdr.rtm_addrs |= RTA_DST; hdr.rtm_msglen += sizeof(label_in); /* adjust iovec */ @@ -967,6 +1045,7 @@ send_rtlabelmsg(int fd, int action, struct kroute *kroute, u_int32_t family) dst.sin_family = AF_INET; dst.sin_addr.s_addr = kroute->prefix.s_addr; /* adjust header */ + hdr.rtm_priority = 0; hdr.rtm_addrs |= RTA_DST; hdr.rtm_msglen += sizeof(dst); /* adjust iovec */ @@ -1000,7 +1079,7 @@ send_rtlabelmsg(int fd, int action, struct kroute *kroute, u_int32_t family) } /* If action is RTM_DELETE we have to get rid of MPLS infos */ - if (kroute->remote_label != 0 && action != RTM_DELETE) { + if (kroute->remote_label != NO_LABEL && action != RTM_DELETE) { bzero(&label_out, sizeof(label_out)); label_out.smpls_len = sizeof(label_out); label_out.smpls_family = AF_MPLS; @@ -1023,7 +1102,7 @@ retry: if (writev(fd, iov, iovcnt) == -1) { switch (errno) { case ESRCH: - if (hdr.rtm_type == RTM_CHANGE) { + if (hdr.rtm_type == RTM_CHANGE && family == AF_MPLS) { hdr.rtm_type = RTM_ADD; goto retry; } else if (hdr.rtm_type == RTM_DELETE) { @@ -1032,7 +1111,7 @@ retry: kroute->prefixlen); return (0); } else { - log_warnx("send_rtlabelmsg: action %u, " + log_warnx("send_rtmsg: action %u, " "prefix %s/%u: %s", hdr.rtm_type, inet_ntoa(kroute->prefix), kroute->prefixlen, strerror(errno)); @@ -1040,9 +1119,9 @@ retry: } break; default: - log_warnx("send_rtlabelmsg: action %u, " - "prefix %s/%u: %s", hdr.rtm_type, - inet_ntoa(kroute->prefix), + log_warnx("send_rtmsg: action %u, " + "AF %d prefix %s/%u: %s", hdr.rtm_type, + family, inet_ntoa(kroute->prefix), kroute->prefixlen, strerror(errno)); return (0); } @@ -1060,116 +1139,6 @@ retry: } int -send_rtmsg(int fd, int action, struct kroute *kroute) -{ - struct iovec iov[5]; - struct rt_msghdr hdr; - struct sockaddr_in prefix; - struct sockaddr_in nexthop; - struct sockaddr_in mask; - struct sockaddr_rtlabel sa_rl; - int iovcnt = 0; - const char *label; - - /* initialize header */ - bzero(&hdr, sizeof(hdr)); - hdr.rtm_version = RTM_VERSION; - hdr.rtm_type = action; - hdr.rtm_priority = RTP_STATIC; - if (action == RTM_CHANGE) /* force PROTO2 reset the other flags */ - hdr.rtm_fmask = RTF_PROTO2|RTF_PROTO1|RTF_REJECT|RTF_BLACKHOLE; - hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */ - hdr.rtm_msglen = sizeof(hdr); - hdr.rtm_hdrlen = sizeof(struct rt_msghdr); - /* adjust iovec */ - iov[iovcnt].iov_base = &hdr; - iov[iovcnt++].iov_len = sizeof(hdr); - - bzero(&prefix, sizeof(prefix)); - prefix.sin_len = sizeof(prefix); - prefix.sin_family = AF_INET; - prefix.sin_addr.s_addr = kroute->prefix.s_addr; - /* adjust header */ - hdr.rtm_addrs |= RTA_DST; - hdr.rtm_msglen += sizeof(prefix); - /* adjust iovec */ - iov[iovcnt].iov_base = &prefix; - iov[iovcnt++].iov_len = sizeof(prefix); - - if (kroute->nexthop.s_addr != 0) { - bzero(&nexthop, sizeof(nexthop)); - nexthop.sin_len = sizeof(nexthop); - nexthop.sin_family = AF_INET; - nexthop.sin_addr.s_addr = kroute->nexthop.s_addr; - /* adjust header */ - hdr.rtm_flags |= RTF_GATEWAY; - hdr.rtm_addrs |= RTA_GATEWAY; - hdr.rtm_msglen += sizeof(nexthop); - /* adjust iovec */ - iov[iovcnt].iov_base = &nexthop; - iov[iovcnt++].iov_len = sizeof(nexthop); - } - - bzero(&mask, sizeof(mask)); - mask.sin_len = sizeof(mask); - mask.sin_family = AF_INET; - mask.sin_addr.s_addr = prefixlen2mask(kroute->prefixlen); - /* adjust header */ - hdr.rtm_addrs |= RTA_NETMASK; - hdr.rtm_msglen += sizeof(mask); - /* adjust iovec */ - iov[iovcnt].iov_base = &mask; - iov[iovcnt++].iov_len = sizeof(mask); - - if (kroute->rtlabel != 0) { - sa_rl.sr_len = sizeof(sa_rl); - sa_rl.sr_family = AF_UNSPEC; - label = rtlabel_id2name(kroute->rtlabel); - if (strlcpy(sa_rl.sr_label, label, - sizeof(sa_rl.sr_label)) >= sizeof(sa_rl.sr_label)) { - log_warnx("send_rtmsg: invalid rtlabel"); - return (-1); - } - /* adjust header */ - hdr.rtm_addrs |= RTA_LABEL; - hdr.rtm_msglen += sizeof(sa_rl); - /* adjust iovec */ - iov[iovcnt].iov_base = &sa_rl; - iov[iovcnt++].iov_len = sizeof(sa_rl); - } - -retry: - if (writev(fd, iov, iovcnt) == -1) { - switch (errno) { - case ESRCH: - if (hdr.rtm_type == RTM_CHANGE) { - hdr.rtm_type = RTM_ADD; - goto retry; - } else if (hdr.rtm_type == RTM_DELETE) { - log_info("route %s/%u vanished before delete", - inet_ntoa(kroute->prefix), - kroute->prefixlen); - return (0); - } else { - log_warnx("send_rtmsg: action %u, " - "prefix %s/%u: %s", hdr.rtm_type, - inet_ntoa(kroute->prefix), - kroute->prefixlen, strerror(errno)); - return (0); - } - break; - default: - log_warnx("send_rtmsg: action %u, prefix %s/%u: %s", - hdr.rtm_type, inet_ntoa(kroute->prefix), - kroute->prefixlen, strerror(errno)); - return (0); - } - } - - return (0); -} - -int fetchtable(void) { size_t len; @@ -1225,6 +1194,9 @@ fetchtable(void) } kr->r.flags = F_KERNEL; + kr->r.priority = rtm->rtm_priority; + kr->r.local_label = NO_LABEL; + kr->r.remote_label = NO_LABEL; switch (sa->sa_family) { case AF_INET: @@ -1235,8 +1207,6 @@ fetchtable(void) kr->r.flags |= F_STATIC; if (rtm->rtm_flags & RTF_DYNAMIC) kr->r.flags |= F_DYNAMIC; - if (rtm->rtm_flags & RTF_PROTO1) - kr->r.flags |= F_BGPD_INSERTED; if (sa_in != NULL) { if (sa_in->sin_len == 0) break; @@ -1269,19 +1239,14 @@ fetchtable(void) != NULL) kr->r.local_label = sa_mpls->smpls_label; - if (rtm->rtm_flags & RTF_PROTO2) { - send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r); - free(kr); - } else { - if ((label = (struct sockaddr_rtlabel *) - rti_info[RTAX_LABEL]) != NULL) { - kr->r.rtlabel = - rtlabel_name2id(label->sr_label); - kr->r.ext_tag = - rtlabel_id2tag(kr->r.rtlabel); - } - kroute_insert(kr); + if ((label = (struct sockaddr_rtlabel *) + rti_info[RTAX_LABEL]) != NULL) { + kr->r.rtlabel = + rtlabel_name2id(label->sr_label); + kr->r.ext_tag = + rtlabel_id2tag(kr->r.rtlabel); } + kroute_insert(kr); } free(buf); @@ -1377,6 +1342,7 @@ dispatch_rtmsg(void) struct kroute_node *kr; struct in_addr prefix, nexthop; u_int8_t prefixlen; + u_int8_t prio; int flags; u_short ifindex = 0; @@ -1401,6 +1367,7 @@ dispatch_rtmsg(void) flags = F_KERNEL; nexthop.s_addr = 0; + prio = rtm->rtm_priority; if (rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE || rtm->rtm_type == RTM_DELETE) { sa = (struct sockaddr *)(next + rtm->rtm_hdrlen); @@ -1437,8 +1404,6 @@ dispatch_rtmsg(void) flags |= F_STATIC; if (rtm->rtm_flags & RTF_DYNAMIC) flags |= F_DYNAMIC; - if (rtm->rtm_flags & RTF_PROTO1) - flags |= F_BGPD_INSERTED; break; default: continue; @@ -1467,8 +1432,8 @@ dispatch_rtmsg(void) continue; } - if ((kr = kroute_find(prefix.s_addr, prefixlen)) != - NULL) { + if ((kr = kroute_find(prefix.s_addr, prefixlen, + prio)) != NULL) { /* * ldp route overridden by kernel. Preference * of the route is not checked because this is @@ -1511,6 +1476,9 @@ dispatch_rtmsg(void) kr->r.nexthop.s_addr = nexthop.s_addr; kr->r.flags = flags; kr->r.ifindex = ifindex; + kr->r.priority = prio; + kr->r.local_label = NO_LABEL; + kr->r.remote_label = NO_LABEL; if ((label = (struct sockaddr_rtlabel *) rti_info[RTAX_LABEL]) != NULL) { @@ -1524,8 +1492,8 @@ dispatch_rtmsg(void) } break; case RTM_DELETE: - if ((kr = kroute_find(prefix.s_addr, prefixlen)) == - NULL) + if ((kr = kroute_find(prefix.s_addr, prefixlen, + prio)) == NULL) continue; if (!(kr->r.flags & F_KERNEL)) continue; @@ -1534,7 +1502,7 @@ dispatch_rtmsg(void) main_imsg_compose_lde(IMSG_NETWORK_DEL, 0, &kr->r, sizeof(struct kroute)); - send_rtlabelmsg(kr_state.fd, RTM_DELETE, + send_rtmsg(kr_state.fd, RTM_DELETE, &kr->r, AF_MPLS); } diff --git a/usr.sbin/ldpd/lde.c b/usr.sbin/ldpd/lde.c index 536716be62b..8abf71effa0 100644 --- a/usr.sbin/ldpd/lde.c +++ b/usr.sbin/ldpd/lde.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lde.c,v 1.9 2010/03/01 09:05:07 claudio Exp $ */ +/* $OpenBSD: lde.c,v 1.10 2010/03/03 10:17:05 claudio Exp $ */ /* * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org> @@ -354,7 +354,6 @@ lde_dispatch_parent(int fd, short event, void *bula) { struct imsg imsg; struct kroute kr; - struct rroute rr; struct imsgev *iev = bula; struct imsgbuf *ibuf = &iev->ibuf; ssize_t n; @@ -379,15 +378,14 @@ lde_dispatch_parent(int fd, short event, void *bula) switch (imsg.hdr.type) { case IMSG_NETWORK_ADD: - if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(rr)) { + if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(kr)) { log_warnx("lde_dispatch_parent: " "wrong imsg len"); break; } - memcpy(&rr, imsg.data, sizeof(rr)); - - lde_kernel_insert(&rr.kr); + memcpy(&kr, imsg.data, sizeof(kr)); + lde_kernel_insert(&kr); break; case IMSG_NETWORK_DEL: if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(kr)) { @@ -398,7 +396,6 @@ lde_dispatch_parent(int fd, short event, void *bula) memcpy(&kr, imsg.data, sizeof(kr)); lde_kernel_remove(&kr); - break; case IMSG_RECONF_CONF: if ((nconf = malloc(sizeof(struct ldpd_conf))) == diff --git a/usr.sbin/ldpd/lde_lib.c b/usr.sbin/ldpd/lde_lib.c index 9cd84c09fba..fdbb4c8e6dd 100644 --- a/usr.sbin/ldpd/lde_lib.c +++ b/usr.sbin/ldpd/lde_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lde_lib.c,v 1.13 2010/02/25 21:47:08 michele Exp $ */ +/* $OpenBSD: lde_lib.c,v 1.14 2010/03/03 10:17:05 claudio Exp $ */ /* * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> @@ -190,6 +190,7 @@ lde_kernel_insert(struct kroute *kr) rn->prefix.s_addr = kr->prefix.s_addr; rn->prefixlen = kr->prefixlen; rn->remote_label = NO_LABEL; + rn->local_label = NO_LABEL; TAILQ_INIT(&rn->labels_list); rt_insert(rn); @@ -230,7 +231,7 @@ lde_kernel_insert(struct kroute *kr) rn->nexthop.s_addr = kr->nexthop.s_addr; /* There is static assigned label for this route, record it in lib */ - if (kr->local_label) { + if (kr->local_label != NO_LABEL) { rn->local_label = (htonl(kr->local_label) << MPLS_LABEL_OFFSET); return; } @@ -244,7 +245,7 @@ lde_kernel_insert(struct kroute *kr) } } - if (!rn->local_label) { + if (rn->local_label == NO_LABEL) { /* Directly connected route */ if (kr->nexthop.s_addr == INADDR_ANY) { rn->local_label = @@ -291,6 +292,7 @@ lde_kernel_remove(struct kroute *kr) struct lde_nbr *ln; rn = rt_find(kr->prefix.s_addr, kr->prefixlen); + rn = rt_find(kr->prefix.s_addr, kr->prefixlen); if (rn == NULL) return; diff --git a/usr.sbin/ldpd/ldpd.c b/usr.sbin/ldpd/ldpd.c index 1288759ba2a..290822d8ea0 100644 --- a/usr.sbin/ldpd/ldpd.c +++ b/usr.sbin/ldpd/ldpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ldpd.c,v 1.6 2010/02/25 17:40:46 claudio Exp $ */ +/* $OpenBSD: ldpd.c,v 1.7 2010/03/03 10:17:05 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> @@ -454,14 +454,23 @@ main_dispatch_lde(int fd, short event, void *bula) switch (imsg.hdr.type) { case IMSG_KLABEL_INSERT: + if (imsg.hdr.len - IMSG_HEADER_SIZE != + sizeof(struct kroute)) + fatalx("invalid size of IMSG_KLABEL_INSERT"); kroute_insert_label(imsg.data); break; case IMSG_KLABEL_CHANGE: + if (imsg.hdr.len - IMSG_HEADER_SIZE != + sizeof(struct kroute)) + fatalx("invalid size of IMSG_KLABEL_CHANGE"); if (kr_change(imsg.data)) log_warn("main_dispatch_lde: error changing " "route"); break; case IMSG_KLABEL_DELETE: + if (imsg.hdr.len - IMSG_HEADER_SIZE != + sizeof(struct kroute)) + fatalx("invalid size of IMSG_KLABEL_DELETE"); if (kr_delete(imsg.data)) log_warn("main_dispatch_lde: error deleting " "route"); diff --git a/usr.sbin/ldpd/ldpd.h b/usr.sbin/ldpd/ldpd.h index 1f657c34389..9a87a78560c 100644 --- a/usr.sbin/ldpd/ldpd.h +++ b/usr.sbin/ldpd/ldpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ldpd.h,v 1.10 2010/02/25 17:40:46 claudio Exp $ */ +/* $OpenBSD: ldpd.h,v 1.11 2010/03/03 10:17:05 claudio Exp $ */ /* * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> @@ -50,11 +50,12 @@ #define F_LDPD_INSERTED 0x0001 #define F_KERNEL 0x0002 -#define F_BGPD_INSERTED 0x0004 -#define F_CONNECTED 0x0008 -#define F_DOWN 0x0010 -#define F_STATIC 0x0020 -#define F_DYNAMIC 0x0040 +#define F_CONNECTED 0x0004 +#define F_STATIC 0x0008 +#define F_DYNAMIC 0x0010 +#define F_DOWN 0x0020 +#define F_REJECT 0x0040 +#define F_BLACKHOLE 0x0080 #define F_REDISTRIBUTED 0x0100 struct evbuf { @@ -273,11 +274,7 @@ struct kroute { u_int32_t ext_tag; u_short ifindex; u_int8_t prefixlen; -}; - -struct rroute { - struct kroute kr; - u_int32_t metric; + u_int8_t priority; }; struct kif_addr { |