1 /* $OpenBSD: pfe_route.c,v 1.15 2026/03/02 19:28:01 rsadowski Exp $ */
4 * Copyright (c) 2009 - 2011 Reyk Floeter <reyk@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <net/route.h>
26 #include <arpa/inet.h>
39 init_routes(struct relayd *env)
43 if (!(env->sc_conf.flags & F_NEEDRT))
46 if ((env->sc_rtsock = socket(AF_ROUTE, SOCK_RAW, 0)) == -1)
47 fatal("%s: failed to open routing socket", __func__);
49 rtfilter = ROUTE_FILTER(0);
50 if (setsockopt(env->sc_rtsock, AF_ROUTE, ROUTE_MSGFILTER,
51 &rtfilter, sizeof(rtfilter)) == -1)
52 fatal("%s: ROUTE_MSGFILTER", __func__);
56 sync_routes(struct relayd *env, struct router *rt)
60 char buf[HOST_NAME_MAX+1];
61 struct ctl_netroute crt;
63 if (!(env->sc_conf.flags & F_NEEDRT))
66 TAILQ_FOREACH(nr, &rt->rt_netroutes, nr_entry) {
67 print_host(&nr->nr_conf.ss, buf, sizeof(buf));
68 TAILQ_FOREACH(host, &rt->rt_gwtable->hosts, entry) {
69 if (host->up == HOST_UNKNOWN)
73 "router %s route %s/%d gateway %s %s priority %d",
75 rt->rt_conf.name, buf, nr->nr_conf.prefixlen,
77 HOST_ISUP(host->up) ? "up" : "down",
81 memcpy(&crt.nr, &nr->nr_conf, sizeof(nr->nr_conf));
82 memcpy(&crt.host, &host->conf, sizeof(host->conf));
83 memcpy(&crt.rt, &rt->rt_conf, sizeof(rt->rt_conf));
85 proc_compose(env->sc_ps, PROC_PARENT,
86 IMSG_RTMSG, &crt, sizeof(crt));
92 pfe_apply_prefixlen(struct sockaddr_storage *ss, int af, int len)
95 uint8_t *b = (uint8_t *)ss;
100 bzero(ss, sizeof(*ss));
104 ss->ss_len = sizeof(struct sockaddr_in);
105 off = offsetof(struct sockaddr_in, sin_addr);
108 ss->ss_len = sizeof(struct sockaddr_in6);
109 off = offsetof(struct sockaddr_in6, sin6_addr);
112 fatal("%s: invalid address family", __func__);
115 memset(b + off, 0xff, q);
117 b[off + q] = (0xff00 >> r) & 0xff;
121 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
124 pfe_route(struct relayd *env, struct ctl_netroute *crt)
127 struct rt_msghdr hdr;
128 struct sockaddr_storage dst, gw, mask, label;
129 struct sockaddr_rtlabel *sr = (struct sockaddr_rtlabel *)&label;
133 bzero(&hdr, sizeof(hdr));
134 hdr.rtm_msglen = sizeof(hdr);
135 hdr.rtm_version = RTM_VERSION;
136 hdr.rtm_type = HOST_ISUP(crt->up) ? RTM_ADD : RTM_DELETE;
137 hdr.rtm_flags = RTF_STATIC | RTF_GATEWAY | RTF_MPATH;
138 hdr.rtm_seq = env->sc_rtseq++;
139 hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
140 hdr.rtm_tableid = crt->rt.rtable;
141 hdr.rtm_priority = crt->host.priority;
143 iov[iovcnt].iov_base = &hdr;
144 iov[iovcnt++].iov_len = sizeof(hdr);
148 gwname = crt->host.name;
149 pfe_apply_prefixlen(&mask, dst.ss_family, crt->nr.prefixlen);
151 iov[iovcnt].iov_base = &dst;
152 iov[iovcnt++].iov_len = ROUNDUP(dst.ss_len);
153 hdr.rtm_msglen += ROUNDUP(dst.ss_len);
155 iov[iovcnt].iov_base = &gw;
156 iov[iovcnt++].iov_len = ROUNDUP(gw.ss_len);
157 hdr.rtm_msglen += ROUNDUP(gw.ss_len);
159 iov[iovcnt].iov_base = &mask;
160 iov[iovcnt++].iov_len = ROUNDUP(mask.ss_len);
161 hdr.rtm_msglen += ROUNDUP(mask.ss_len);
163 if (strlen(crt->rt.label)) {
164 sr->sr_len = sizeof(*sr);
165 strlcpy(sr->sr_label, crt->rt.label, sizeof(sr->sr_label));
167 iov[iovcnt].iov_base = &label;
168 iov[iovcnt++].iov_len = ROUNDUP(label.ss_len);
169 hdr.rtm_msglen += ROUNDUP(label.ss_len);
170 hdr.rtm_addrs |= RTA_LABEL;
174 if (writev(env->sc_rtsock, iov, iovcnt) == -1) {
178 if (hdr.rtm_type == RTM_ADD) {
179 hdr.rtm_type = RTM_CHANGE;
181 } else if (hdr.rtm_type == RTM_DELETE) {
191 log_debug("%s: gateway %s %s", __func__, gwname,
192 HOST_ISUP(crt->up) ? "added" : "deleted");
197 log_debug("%s: failed to %s gateway %s: %d %s", __func__,
198 HOST_ISUP(crt->up) ? "add" : "delete", gwname,
199 errno, strerror(errno));