]> git.ipfire.org Git - thirdparty/bird.git/blob - sysdep/linux/krt-scan.c
Cleaned up debugging in kernel syncer. Netlink has still LOCAL_DEBUG
[thirdparty/bird.git] / sysdep / linux / krt-scan.c
1 /*
2 * BIRD -- Linux Routing Table Scanning
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <net/route.h>
15
16 #undef LOCAL_DEBUG
17
18 #include "nest/bird.h"
19 #include "nest/route.h"
20 #include "nest/protocol.h"
21 #include "nest/iface.h"
22 #include "lib/timer.h"
23 #include "lib/unix.h"
24 #include "lib/krt.h"
25
26 static int krt_scan_fd = -1;
27
28 struct iface *
29 krt_temp_iface(struct krt_proto *p, char *name)
30 {
31 struct iface *i;
32
33 WALK_LIST(i, p->scan.temp_ifs)
34 if (!strcmp(i->name, name))
35 return i;
36 i = mb_allocz(p->p.pool, sizeof(struct iface));
37 strcpy(i->name, name);
38 add_tail(&p->scan.temp_ifs, &i->n);
39 return i;
40 }
41
42 static void
43 krt_parse_entry(byte *ent, struct krt_proto *p)
44 {
45 u32 dest0, gw0, mask0;
46 ip_addr dest, gw, mask;
47 unsigned int flags;
48 int masklen;
49 net *net;
50 byte *iface = ent;
51 rta a;
52 rte *e;
53
54 if (sscanf(ent, "%*s\t%x\t%x\t%x\t%*d\t%*d\t%*d\t%x\t", &dest0, &gw0, &flags, &mask0) != 4)
55 {
56 log(L_ERR "krt read: unable to parse `%s'", ent);
57 return;
58 }
59 while (*ent != '\t')
60 ent++;
61 *ent = 0;
62
63 dest = ipa_from_u32(dest0);
64 ipa_ntoh(dest);
65 gw = ipa_from_u32(gw0);
66 ipa_ntoh(gw);
67 mask = ipa_from_u32(mask0);
68 ipa_ntoh(mask);
69 if ((masklen = ipa_mklen(mask)) < 0)
70 {
71 log(L_ERR "krt read: invalid netmask %08x", mask0);
72 return;
73 }
74 DBG("Got %I/%d via %I flags %x\n", dest, masklen, gw, flags);
75
76 if (!(flags & RTF_UP))
77 {
78 DBG("Down.\n");
79 return;
80 }
81 if (flags & RTF_HOST)
82 masklen = 32;
83 if (flags & (RTF_DYNAMIC | RTF_MODIFIED)) /* Redirect route */
84 {
85 log(L_WARN "krt: Ignoring redirect to %I/%d via %I", dest, masklen, gw);
86 return;
87 }
88
89 net = net_get(p->p.table, dest, masklen);
90
91 a.proto = &p->p;
92 a.source = RTS_INHERIT;
93 a.scope = SCOPE_UNIVERSE;
94 a.cast = RTC_UNICAST;
95 a.flags = a.aflags = 0;
96 a.from = IPA_NONE;
97 a.iface = NULL;
98 a.eattrs = NULL;
99
100 if (flags & RTF_GATEWAY)
101 {
102 neighbor *ng = neigh_find(&p->p, &gw, 0);
103 if (ng)
104 a.iface = ng->iface;
105 else
106 /* FIXME: Remove this warning? Handle it somehow... */
107 log(L_WARN "Kernel told us to use non-neighbor %I for %I/%d", gw, net->n.prefix, net->n.pxlen);
108 a.dest = RTD_ROUTER;
109 a.gw = gw;
110 }
111 else if (flags & RTF_REJECT)
112 {
113 a.dest = RTD_UNREACHABLE;
114 a.gw = IPA_NONE;
115 }
116 else if (isalpha(iface[0]))
117 {
118 a.dest = RTD_DEVICE;
119 a.gw = IPA_NONE;
120 a.iface = krt_temp_iface(p, iface);
121 }
122 else
123 {
124 log(L_WARN "Kernel reporting unknown route type to %I/%d", net->n.prefix, net->n.pxlen);
125 return;
126 }
127
128 e = rte_get_temp(&a);
129 e->net = net;
130 e->u.krt.src = KRT_SRC_UNKNOWN;
131 krt_got_route(p, e);
132 }
133
134 void
135 krt_scan_fire(struct krt_proto *p)
136 {
137 byte buf[32768];
138 int l, seen_hdr;
139
140 if (krt_scan_fd < 0)
141 {
142 krt_scan_fd = open("/proc/net/route", O_RDONLY);
143 if (krt_scan_fd < 0)
144 die("/proc/net/route: %m");
145 }
146 else if (lseek(krt_scan_fd, 0, SEEK_SET) < 0)
147 {
148 log(L_ERR "krt seek: %m");
149 return;
150 }
151 seen_hdr = 0;
152 while ((l = read(krt_scan_fd, buf, sizeof(buf))) > 0)
153 {
154 byte *z = buf;
155 if (l & 127)
156 {
157 log(L_ERR "krt read: misaligned entry: l=%d", l);
158 return;
159 }
160 while (l >= 128)
161 {
162 if (seen_hdr++)
163 krt_parse_entry(z, p);
164 z += 128;
165 l -= 128;
166 }
167 }
168 if (l < 0)
169 {
170 log(L_ERR "krt read: %m");
171 return;
172 }
173 DBG("KRT scan done, seen %d lines\n", seen_hdr);
174 }
175
176 void
177 krt_scan_construct(struct krt_config *c)
178 {
179 }
180
181 void
182 krt_scan_preconfig(struct config *c)
183 {
184 }
185
186 void
187 krt_scan_postconfig(struct krt_config *c)
188 {
189 }
190
191 void
192 krt_scan_start(struct krt_proto *x, int first)
193 {
194 init_list(&x->scan.temp_ifs);
195 }
196
197 void
198 krt_scan_shutdown(struct krt_proto *x, int last)
199 {
200 }