]>
Commit | Line | Data |
---|---|---|
1 | /* $KAME: rrenum.c,v 1.12 2002/06/10 19:59:47 itojun Exp $ */ | |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | #include <sys/types.h> | |
32 | #include <sys/param.h> | |
33 | #include <sys/ioctl.h> | |
34 | #include <sys/socket.h> | |
35 | #include <sys/sysctl.h> | |
36 | ||
37 | #include <net/if.h> | |
38 | #include <net/if_var.h> | |
39 | #include <net/route.h> | |
40 | #include <netinet/in.h> | |
41 | #include <netinet/in_var.h> | |
42 | #include <netinet/icmp6.h> | |
43 | ||
44 | #include <arpa/inet.h> | |
45 | ||
46 | #include <errno.h> | |
47 | #include <string.h> | |
48 | #include <stdlib.h> | |
49 | #include "rtadvd.h" | |
50 | #include "rrenum.h" | |
51 | #include "if.h" | |
52 | ||
53 | #define RR_ISSET_SEGNUM(segnum_bits, segnum) \ | |
54 | ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0) | |
55 | #define RR_SET_SEGNUM(segnum_bits, segnum) \ | |
56 | (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31))) | |
57 | ||
58 | struct rr_operation { | |
59 | u_long rro_seqnum; | |
60 | u_long rro_segnum_bits[8]; | |
61 | }; | |
62 | ||
63 | static struct rr_operation rro; | |
64 | static int rr_rcvifindex; | |
65 | static int rrcmd2pco[RPM_PCO_MAX] = { | |
66 | 0, | |
67 | SIOCAIFPREFIX_IN6, | |
68 | SIOCCIFPREFIX_IN6, | |
69 | SIOCSGIFPREFIX_IN6 | |
70 | }; | |
71 | static int s = -1; | |
72 | ||
73 | /* | |
74 | * Check validity of a Prefix Control Operation(PCO). | |
75 | * Return 0 on success, 1 on failure. | |
76 | */ | |
77 | static int | |
78 | rr_pco_check(int len, struct rr_pco_match *rpm) | |
79 | { | |
80 | struct rr_pco_use *rpu, *rpulim; | |
81 | int checklen; | |
82 | ||
83 | /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */ | |
84 | if ((rpm->rpm_len - 3) < 0 || /* must be at least 3 */ | |
85 | (rpm->rpm_len - 3) & 0x3) { /* must be multiple of 4 */ | |
86 | errorlog("<%s> rpm_len %d is not 4N * 3", | |
87 | __func__, rpm->rpm_len); | |
88 | return 1; | |
89 | } | |
90 | /* rpm->rpm_code must be valid value */ | |
91 | switch (rpm->rpm_code) { | |
92 | case RPM_PCO_ADD: | |
93 | case RPM_PCO_CHANGE: | |
94 | case RPM_PCO_SETGLOBAL: | |
95 | break; | |
96 | default: | |
97 | errorlog("<%s> unknown rpm_code %d", __func__, | |
98 | rpm->rpm_code); | |
99 | return 1; | |
100 | } | |
101 | /* rpm->rpm_matchlen must be 0 to 128 inclusive */ | |
102 | if (rpm->rpm_matchlen > 128) { | |
103 | errorlog("<%s> rpm_matchlen %d is over 128", | |
104 | __func__, rpm->rpm_matchlen); | |
105 | return 1; | |
106 | } | |
107 | ||
108 | /* | |
109 | * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be | |
110 | * between 0 and 128 inclusive | |
111 | */ | |
112 | for (rpu = (struct rr_pco_use *)(rpm + 1), | |
113 | rpulim = (struct rr_pco_use *)((char *)rpm + len); | |
114 | rpu < rpulim; | |
115 | rpu += 1) { | |
116 | checklen = rpu->rpu_uselen; | |
117 | checklen += rpu->rpu_keeplen; | |
118 | /* | |
119 | * omit these check, because either of rpu_uselen | |
120 | * and rpu_keeplen is unsigned char | |
121 | * (128 > rpu_uselen > 0) | |
122 | * (128 > rpu_keeplen > 0) | |
123 | * (rpu_uselen + rpu_keeplen > 0) | |
124 | */ | |
125 | if (checklen > 128) { | |
126 | errorlog("<%s> sum of rpu_uselen %d and" | |
127 | " rpu_keeplen %d is %d(over 128)", | |
128 | __func__, rpu->rpu_uselen, | |
129 | rpu->rpu_keeplen, | |
130 | rpu->rpu_uselen + rpu->rpu_keeplen); | |
131 | return 1; | |
132 | } | |
133 | } | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static void | |
138 | do_use_prefix(int len, struct rr_pco_match *rpm, | |
139 | struct in6_rrenumreq *irr, int ifindex) | |
140 | { | |
141 | struct rr_pco_use *rpu, *rpulim; | |
142 | struct rainfo *rai; | |
143 | struct prefix *pp; | |
144 | ||
145 | rpu = (struct rr_pco_use *)(rpm + 1); | |
146 | rpulim = (struct rr_pco_use *)((char *)rpm + len); | |
147 | ||
148 | if (rpu == rpulim) { /* no use prefix */ | |
149 | if (rpm->rpm_code == RPM_PCO_ADD) | |
150 | return; | |
151 | ||
152 | irr->irr_u_uselen = 0; | |
153 | irr->irr_u_keeplen = 0; | |
154 | irr->irr_raf_mask_onlink = 0; | |
155 | irr->irr_raf_mask_auto = 0; | |
156 | irr->irr_vltime = 0; | |
157 | irr->irr_pltime = 0; | |
158 | memset(&irr->irr_flags, 0, sizeof(irr->irr_flags)); | |
159 | irr->irr_useprefix.sin6_len = 0; /* let it mean, no addition */ | |
160 | irr->irr_useprefix.sin6_family = 0; | |
161 | irr->irr_useprefix.sin6_addr = in6addr_any; | |
162 | if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 && | |
163 | errno != EADDRNOTAVAIL) | |
164 | errorlog("<%s> ioctl: %s", __func__, | |
165 | strerror(errno)); | |
166 | return; | |
167 | } | |
168 | ||
169 | for (rpu = (struct rr_pco_use *)(rpm + 1), | |
170 | rpulim = (struct rr_pco_use *)((char *)rpm + len); | |
171 | rpu < rpulim; | |
172 | rpu += 1) { | |
173 | /* init in6_rrenumreq fields */ | |
174 | irr->irr_u_uselen = rpu->rpu_uselen; | |
175 | irr->irr_u_keeplen = rpu->rpu_keeplen; | |
176 | irr->irr_raf_mask_onlink = | |
177 | !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK); | |
178 | irr->irr_raf_mask_auto = | |
179 | !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_AUTO); | |
180 | irr->irr_vltime = ntohl(rpu->rpu_vltime); | |
181 | irr->irr_pltime = ntohl(rpu->rpu_pltime); | |
182 | irr->irr_raf_onlink = | |
183 | (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK) == 0 ? 0 : 1; | |
184 | irr->irr_raf_auto = | |
185 | (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_AUTO) == 0 ? 0 : 1; | |
186 | irr->irr_rrf_decrvalid = | |
187 | (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME) == 0 ? 0 : 1; | |
188 | irr->irr_rrf_decrprefd = | |
189 | (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME) == 0 ? 0 : 1; | |
190 | irr->irr_useprefix.sin6_len = sizeof(irr->irr_useprefix); | |
191 | irr->irr_useprefix.sin6_family = AF_INET6; | |
192 | irr->irr_useprefix.sin6_addr = rpu->rpu_prefix; | |
193 | ||
194 | if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 && | |
195 | errno != EADDRNOTAVAIL) | |
196 | errorlog("<%s> ioctl: %s", __func__, | |
197 | strerror(errno)); | |
198 | ||
199 | /* very adhoc: should be rewritten */ | |
200 | if (rpm->rpm_code == RPM_PCO_CHANGE && | |
201 | IN6_ARE_ADDR_EQUAL(&rpm->rpm_prefix, &rpu->rpu_prefix) && | |
202 | rpm->rpm_matchlen == rpu->rpu_uselen && | |
203 | rpu->rpu_uselen == rpu->rpu_keeplen) { | |
204 | if ((rai = if_indextorainfo(ifindex)) == NULL) | |
205 | continue; /* non-advertising IF */ | |
206 | ||
207 | for (pp = rai->prefix.next; pp != &rai->prefix; | |
208 | pp = pp->next) { | |
209 | struct timeval now; | |
210 | ||
211 | if (prefix_match(&pp->prefix, pp->prefixlen, | |
212 | &rpm->rpm_prefix, | |
213 | rpm->rpm_matchlen)) { | |
214 | /* change parameters */ | |
215 | pp->validlifetime = ntohl(rpu->rpu_vltime); | |
216 | pp->preflifetime = ntohl(rpu->rpu_pltime); | |
217 | if (irr->irr_rrf_decrvalid) { | |
218 | gettimeofday(&now, 0); | |
219 | pp->vltimeexpire = | |
220 | now.tv_sec + pp->validlifetime; | |
221 | } else | |
222 | pp->vltimeexpire = 0; | |
223 | if (irr->irr_rrf_decrprefd) { | |
224 | gettimeofday(&now, 0); | |
225 | pp->pltimeexpire = | |
226 | now.tv_sec + pp->preflifetime; | |
227 | } else | |
228 | pp->pltimeexpire = 0; | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | /* | |
236 | * process a Prefix Control Operation(PCO). | |
237 | * return 0 on success, 1 on failure | |
238 | */ | |
239 | static int | |
240 | do_pco(struct icmp6_router_renum *rr, int len, struct rr_pco_match *rpm) | |
241 | { | |
242 | int ifindex = 0; | |
243 | struct in6_rrenumreq irr; | |
244 | ||
245 | if ((rr_pco_check(len, rpm) != 0)) | |
246 | return 1; | |
247 | ||
248 | if (s == -1 && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { | |
249 | errorlog("<%s> socket: %s", __func__, | |
250 | strerror(errno)); | |
251 | exit(1); | |
252 | } | |
253 | ||
254 | memset(&irr, 0, sizeof(irr)); | |
255 | irr.irr_origin = PR_ORIG_RR; | |
256 | irr.irr_m_len = rpm->rpm_matchlen; | |
257 | irr.irr_m_minlen = rpm->rpm_minlen; | |
258 | irr.irr_m_maxlen = rpm->rpm_maxlen; | |
259 | irr.irr_matchprefix.sin6_len = sizeof(irr.irr_matchprefix); | |
260 | irr.irr_matchprefix.sin6_family = AF_INET6; | |
261 | irr.irr_matchprefix.sin6_addr = rpm->rpm_prefix; | |
262 | ||
263 | while (if_indextoname(++ifindex, irr.irr_name)) { | |
264 | struct if_msghdr * ifm = get_interface_entry(ifindex); | |
265 | if (ifm == NULL) { | |
266 | debuglog("Couldn't find interface entry for %d. Skipping.", ifindex); | |
267 | continue; | |
268 | } | |
269 | /* | |
270 | * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and IFF_UP is off, | |
271 | * the interface is not applied | |
272 | */ | |
273 | if ((rr->rr_flags & ICMP6_RR_FLAGS_FORCEAPPLY) == 0 && | |
274 | (ifm->ifm_flags & IFF_UP) == 0) | |
275 | continue; | |
276 | /* TODO: interface scope check */ | |
277 | do_use_prefix(len, rpm, &irr, ifindex); | |
278 | } | |
279 | if (errno == ENXIO) | |
280 | return 0; | |
281 | else if (errno) { | |
282 | errorlog("<%s> if_indextoname: %s", __func__, | |
283 | strerror(errno)); | |
284 | return 1; | |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
289 | /* | |
290 | * call do_pco() for each Prefix Control Operations(PCOs) in a received | |
291 | * Router Renumbering Command packet. | |
292 | * return 0 on success, 1 on failure | |
293 | */ | |
294 | static int | |
295 | do_rr(int len, struct icmp6_router_renum *rr) | |
296 | { | |
297 | struct rr_pco_match *rpm; | |
298 | char *cp, *lim; | |
299 | ||
300 | lim = (char *)rr + len; | |
301 | cp = (char *)(rr + 1); | |
302 | len -= sizeof(struct icmp6_router_renum); | |
303 | ||
304 | /* get iflist block from kernel again, to get up-to-date information */ | |
305 | init_iflist(); | |
306 | ||
307 | while (cp < lim) { | |
308 | int rpmlen; | |
309 | ||
310 | rpm = (struct rr_pco_match *)cp; | |
311 | if (len < sizeof(struct rr_pco_match)) { | |
312 | tooshort: | |
313 | errorlog("<%s> pkt too short. left len = %d. " | |
314 | "gabage at end of pkt?", __func__, len); | |
315 | return 1; | |
316 | } | |
317 | rpmlen = rpm->rpm_len << 3; | |
318 | if (len < rpmlen) | |
319 | goto tooshort; | |
320 | ||
321 | if (do_pco(rr, rpmlen, rpm)) { | |
322 | errorlog("<%s> invalid PCO", __func__); | |
323 | goto next; | |
324 | } | |
325 | ||
326 | next: | |
327 | cp += rpmlen; | |
328 | len -= rpmlen; | |
329 | } | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
334 | /* | |
335 | * check validity of a router renumbering command packet | |
336 | * return 0 on success, 1 on failure | |
337 | */ | |
338 | static int | |
339 | rr_command_check(int len, struct icmp6_router_renum *rr, struct in6_addr *from, | |
340 | struct in6_addr *dst) | |
341 | { | |
342 | char ntopbuf[INET6_ADDRSTRLEN]; | |
343 | ||
344 | /* omit rr minimal length check. hope kernel have done it. */ | |
345 | /* rr_command length check */ | |
346 | if (len < (sizeof(struct icmp6_router_renum) + | |
347 | sizeof(struct rr_pco_match))) { | |
348 | errorlog("<%s> rr_command len %d is too short", | |
349 | __func__, len); | |
350 | return 1; | |
351 | } | |
352 | ||
353 | /* destination check. only for multicast. omit unicast check. */ | |
354 | if (IN6_IS_ADDR_MULTICAST(dst) && !IN6_IS_ADDR_MC_LINKLOCAL(dst) && | |
355 | !IN6_IS_ADDR_MC_SITELOCAL(dst)) { | |
356 | errorlog("<%s> dst mcast addr %s is illegal", | |
357 | __func__, | |
358 | inet_ntop(AF_INET6, dst, ntopbuf, INET6_ADDRSTRLEN)); | |
359 | return 1; | |
360 | } | |
361 | ||
362 | /* seqnum and segnum check */ | |
363 | if (rro.rro_seqnum > rr->rr_seqnum) { | |
364 | errorlog("<%s> rcvd old seqnum %d from %s", | |
365 | __func__, (u_int32_t)ntohl(rr->rr_seqnum), | |
366 | inet_ntop(AF_INET6, from, ntopbuf, INET6_ADDRSTRLEN)); | |
367 | return 1; | |
368 | } | |
369 | if (rro.rro_seqnum == rr->rr_seqnum && | |
370 | (rr->rr_flags & ICMP6_RR_FLAGS_TEST) == 0 && | |
371 | RR_ISSET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum)) { | |
372 | if ((rr->rr_flags & ICMP6_RR_FLAGS_REQRESULT) != 0) | |
373 | errorlog("<%s> rcvd duped segnum %d from %s", | |
374 | __func__, rr->rr_segnum, | |
375 | inet_ntop(AF_INET6, from, ntopbuf, | |
376 | INET6_ADDRSTRLEN)); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | /* update seqnum */ | |
381 | if (rro.rro_seqnum != rr->rr_seqnum) { | |
382 | /* then must be "<" */ | |
383 | ||
384 | /* init rro_segnum_bits */ | |
385 | memset(rro.rro_segnum_bits, 0, | |
386 | sizeof(rro.rro_segnum_bits)); | |
387 | } | |
388 | rro.rro_seqnum = rr->rr_seqnum; | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
393 | static void | |
394 | rr_command_input(int len, struct icmp6_router_renum *rr, | |
395 | struct in6_addr *from, struct in6_addr *dst) | |
396 | { | |
397 | /* rr_command validity check */ | |
398 | if (rr_command_check(len, rr, from, dst)) | |
399 | goto failed; | |
400 | if ((rr->rr_flags & (ICMP6_RR_FLAGS_TEST|ICMP6_RR_FLAGS_REQRESULT)) == | |
401 | ICMP6_RR_FLAGS_TEST) | |
402 | return; | |
403 | ||
404 | /* do router renumbering */ | |
405 | if (do_rr(len, rr)) { | |
406 | goto failed; | |
407 | } | |
408 | ||
409 | /* update segnum */ | |
410 | RR_SET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum); | |
411 | ||
412 | return; | |
413 | ||
414 | failed: | |
415 | errorlog("<%s> received RR was invalid", __func__); | |
416 | return; | |
417 | } | |
418 | ||
419 | void | |
420 | rr_input(int len, struct icmp6_router_renum *rr, struct in6_pktinfo *pi, | |
421 | struct sockaddr_in6 *from, struct in6_addr *dst) | |
422 | { | |
423 | char ntopbuf[2][INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; | |
424 | ||
425 | debuglog("<%s> RR received from %s to %s on %s", | |
426 | __func__, | |
427 | inet_ntop(AF_INET6, &from->sin6_addr, | |
428 | ntopbuf[0], INET6_ADDRSTRLEN), | |
429 | inet_ntop(AF_INET6, &dst, ntopbuf[1], INET6_ADDRSTRLEN), | |
430 | if_indextoname(pi->ipi6_ifindex, ifnamebuf)); | |
431 | ||
432 | /* packet validation based on Section 4.1 of RFC2894 */ | |
433 | if (len < sizeof(struct icmp6_router_renum)) { | |
434 | noticelog("<%s>: RR short message (size %d) from %s to %s on %s", | |
435 | __func__, len, | |
436 | inet_ntop(AF_INET6, &from->sin6_addr, | |
437 | ntopbuf[0], INET6_ADDRSTRLEN), | |
438 | inet_ntop(AF_INET6, &dst, ntopbuf[1], INET6_ADDRSTRLEN), | |
439 | if_indextoname(pi->ipi6_ifindex, ifnamebuf)); | |
440 | return; | |
441 | } | |
442 | ||
443 | /* | |
444 | * If the IPv6 destination address is neither an All Routers multicast | |
445 | * address [AARCH] nor one of the receiving router's unicast addresses, | |
446 | * the message MUST be discarded and SHOULD be logged to network | |
447 | * management. | |
448 | * We rely on the kernel input routine for unicast addresses, and thus | |
449 | * check multicast destinations only. | |
450 | */ | |
451 | if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && | |
452 | !IN6_ARE_ADDR_EQUAL(&in6a_site_allrouters, &pi->ipi6_addr)) { | |
453 | noticelog("<%s>: RR message with invalid destination (%s) " | |
454 | "from %s on %s", | |
455 | __func__, | |
456 | inet_ntop(AF_INET6, &dst, ntopbuf[0], INET6_ADDRSTRLEN), | |
457 | inet_ntop(AF_INET6, &from->sin6_addr, | |
458 | ntopbuf[1], INET6_ADDRSTRLEN), | |
459 | if_indextoname(pi->ipi6_ifindex, ifnamebuf)); | |
460 | return; | |
461 | } | |
462 | ||
463 | rr_rcvifindex = pi->ipi6_ifindex; | |
464 | ||
465 | switch (rr->rr_code) { | |
466 | case ICMP6_ROUTER_RENUMBERING_COMMAND: | |
467 | rr_command_input(len, rr, &from->sin6_addr, dst); | |
468 | /* TODO: send reply msg */ | |
469 | break; | |
470 | case ICMP6_ROUTER_RENUMBERING_RESULT: | |
471 | /* RESULT will be processed by rrenumd */ | |
472 | break; | |
473 | case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET: | |
474 | /* TODO: sequence number reset */ | |
475 | break; | |
476 | default: | |
477 | errorlog("<%s> received unknown code %d", | |
478 | __func__, rr->rr_code); | |
479 | break; | |
480 | ||
481 | } | |
482 | ||
483 | return; | |
484 | } |