diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2007-04-19 13:01:05 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2007-04-19 13:01:05 +0000 |
commit | b6eb191f36ba849ca45f44600a34e9ff9faf9ad8 (patch) | |
tree | 30f35057b5f371ece62fadb5a6454ddfb855bf66 /usr.sbin/ospfd | |
parent | 8cffb2f1f1aaabbc31453bac3769c8d56354362a (diff) |
Use iovecs to write routing messages to the socket instead of a static
structure. This gives greater flexibility and solves an upcoming problem
with multipath routes. OK norby@
Diffstat (limited to 'usr.sbin/ospfd')
-rw-r--r-- | usr.sbin/ospfd/kroute.c | 96 |
1 files changed, 61 insertions, 35 deletions
diff --git a/usr.sbin/ospfd/kroute.c b/usr.sbin/ospfd/kroute.c index 42f87e39193..2896204b95b 100644 --- a/usr.sbin/ospfd/kroute.c +++ b/usr.sbin/ospfd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.46 2007/04/12 12:51:37 claudio Exp $ */ +/* $OpenBSD: kroute.c,v 1.47 2007/04/19 13:01:04 claudio Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -22,6 +22,7 @@ #include <sys/socket.h> #include <sys/sysctl.h> #include <sys/tree.h> +#include <sys/uio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <net/if.h> @@ -876,55 +877,80 @@ if_announce(void *msg) int send_rtmsg(int fd, int action, struct kroute *kroute) { - struct { - struct rt_msghdr hdr; - struct sockaddr_in prefix; - struct sockaddr_in nexthop; - struct sockaddr_in mask; - } r; + struct iovec iov[4]; + struct rt_msghdr hdr; + struct sockaddr_in prefix; + struct sockaddr_in nexthop; + struct sockaddr_in mask; + int iovcnt = 0; if (kr_state.fib_sync == 0) return (0); - bzero(&r, sizeof(r)); - r.hdr.rtm_msglen = sizeof(r); - r.hdr.rtm_version = RTM_VERSION; - r.hdr.rtm_type = action; - r.hdr.rtm_flags = RTF_PROTO2; + /* initialize header */ + bzero(&hdr, sizeof(hdr)); + hdr.rtm_version = RTM_VERSION; + hdr.rtm_type = action; + hdr.rtm_flags = RTF_PROTO2; if (action == RTM_CHANGE) /* force PROTO2 reset the other flags */ - r.hdr.rtm_fmask = - RTF_PROTO2|RTF_PROTO1|RTF_REJECT|RTF_BLACKHOLE; - r.hdr.rtm_seq = kr_state.rtseq++; /* overflow doesn't matter */ - r.hdr.rtm_addrs = RTA_DST|RTA_GATEWAY|RTA_NETMASK; - r.prefix.sin_len = sizeof(r.prefix); - r.prefix.sin_family = AF_INET; - r.prefix.sin_addr.s_addr = kroute->prefix.s_addr; - - r.nexthop.sin_len = sizeof(r.nexthop); - r.nexthop.sin_family = AF_INET; - r.nexthop.sin_addr.s_addr = kroute->nexthop.s_addr; - if (kroute->nexthop.s_addr != 0) - r.hdr.rtm_flags |= RTF_GATEWAY; - - r.mask.sin_len = sizeof(r.mask); - r.mask.sin_family = AF_INET; - r.mask.sin_addr.s_addr = prefixlen2mask(kroute->prefixlen); + 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); + /* 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); retry: - if (write(fd, &r, sizeof(r)) == -1) { + if (writev(fd, iov, iovcnt) == -1) { switch (errno) { case ESRCH: - if (r.hdr.rtm_type == RTM_CHANGE) { - r.hdr.rtm_type = RTM_ADD; + if (hdr.rtm_type == RTM_CHANGE) { + hdr.rtm_type = RTM_ADD; goto retry; - } else if (r.hdr.rtm_type == RTM_DELETE) { + } 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", r.hdr.rtm_type, + "prefix %s/%u: %s", hdr.rtm_type, inet_ntoa(kroute->prefix), kroute->prefixlen, strerror(errno)); return (0); @@ -932,7 +958,7 @@ retry: break; default: log_warnx("send_rtmsg: action %u, prefix %s/%u: %s", - r.hdr.rtm_type, inet_ntoa(kroute->prefix), + hdr.rtm_type, inet_ntoa(kroute->prefix), kroute->prefixlen, strerror(errno)); return (0); } |