]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-nexthop.c
network/nexthop: cache requested nexthop IDs
[thirdparty/systemd.git] / src / network / networkd-nexthop.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2019 VMware, Inc.
3 */
4
5 #include <net/if.h>
6 #include <linux/nexthop.h>
7
8 #include "alloc-util.h"
9 #include "netlink-util.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "networkd-nexthop.h"
14 #include "networkd-queue.h"
15 #include "networkd-route-util.h"
16 #include "parse-util.h"
17 #include "set.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
20
21 NextHop *nexthop_free(NextHop *nexthop) {
22 if (!nexthop)
23 return NULL;
24
25 if (nexthop->network) {
26 assert(nexthop->section);
27 ordered_hashmap_remove(nexthop->network->nexthops_by_section, nexthop->section);
28 }
29
30 config_section_free(nexthop->section);
31
32 if (nexthop->manager) {
33 assert(nexthop->id > 0);
34 hashmap_remove(nexthop->manager->nexthops_by_id, UINT32_TO_PTR(nexthop->id));
35 }
36
37 hashmap_free_free(nexthop->group);
38
39 return mfree(nexthop);
40 }
41
42 DEFINE_SECTION_CLEANUP_FUNCTIONS(NextHop, nexthop_free);
43
44 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
45 nexthop_hash_ops,
46 void,
47 trivial_hash_func,
48 trivial_compare_func,
49 NextHop,
50 nexthop_free);
51
52 static int nexthop_new(NextHop **ret) {
53 _cleanup_(nexthop_freep) NextHop *nexthop = NULL;
54
55 nexthop = new(NextHop, 1);
56 if (!nexthop)
57 return -ENOMEM;
58
59 *nexthop = (NextHop) {
60 .family = AF_UNSPEC,
61 .onlink = -1,
62 };
63
64 *ret = TAKE_PTR(nexthop);
65
66 return 0;
67 }
68
69 static int nexthop_new_static(Network *network, const char *filename, unsigned section_line, NextHop **ret) {
70 _cleanup_(config_section_freep) ConfigSection *n = NULL;
71 _cleanup_(nexthop_freep) NextHop *nexthop = NULL;
72 int r;
73
74 assert(network);
75 assert(ret);
76 assert(filename);
77 assert(section_line > 0);
78
79 r = config_section_new(filename, section_line, &n);
80 if (r < 0)
81 return r;
82
83 nexthop = ordered_hashmap_get(network->nexthops_by_section, n);
84 if (nexthop) {
85 *ret = TAKE_PTR(nexthop);
86 return 0;
87 }
88
89 r = nexthop_new(&nexthop);
90 if (r < 0)
91 return r;
92
93 nexthop->protocol = RTPROT_STATIC;
94 nexthop->network = network;
95 nexthop->section = TAKE_PTR(n);
96 nexthop->source = NETWORK_CONFIG_SOURCE_STATIC;
97
98 r = ordered_hashmap_ensure_put(&network->nexthops_by_section, &config_section_hash_ops, nexthop->section, nexthop);
99 if (r < 0)
100 return r;
101
102 *ret = TAKE_PTR(nexthop);
103 return 0;
104 }
105
106 static void nexthop_hash_func(const NextHop *nexthop, struct siphash *state) {
107 assert(nexthop);
108 assert(state);
109
110 siphash24_compress(&nexthop->id, sizeof(nexthop->id), state);
111 }
112
113 static int nexthop_compare_func(const NextHop *a, const NextHop *b) {
114 assert(a);
115 assert(b);
116
117 return CMP(a->id, b->id);
118 }
119
120 static int nexthop_compare_full(const NextHop *a, const NextHop *b) {
121 int r;
122
123 assert(a);
124 assert(b);
125
126 /* This compares detailed configs, except for ID and ifindex. */
127
128 r = CMP(a->protocol, b->protocol);
129 if (r != 0)
130 return r;
131
132 r = CMP(a->flags, b->flags);
133 if (r != 0)
134 return r;
135
136 r = CMP(hashmap_size(a->group), hashmap_size(b->group));
137 if (r != 0)
138 return r;
139
140 if (!hashmap_isempty(a->group)) {
141 struct nexthop_grp *ga;
142
143 HASHMAP_FOREACH(ga, a->group) {
144 struct nexthop_grp *gb;
145
146 gb = hashmap_get(b->group, UINT32_TO_PTR(ga->id));
147 if (!gb)
148 return CMP(ga, gb);
149
150 r = CMP(ga->weight, gb->weight);
151 if (r != 0)
152 return r;
153 }
154 }
155
156 r = CMP(a->blackhole, b->blackhole);
157 if (r != 0)
158 return r;
159
160 r = CMP(a->family, b->family);
161 if (r != 0)
162 return r;
163
164 if (IN_SET(a->family, AF_INET, AF_INET6)) {
165 r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
166 if (r != 0)
167 return r;
168 }
169
170 return 0;
171 }
172
173 static int nexthop_dup(const NextHop *src, NextHop **ret) {
174 _cleanup_(nexthop_freep) NextHop *dest = NULL;
175 struct nexthop_grp *nhg;
176 int r;
177
178 assert(src);
179 assert(ret);
180
181 dest = newdup(NextHop, src, 1);
182 if (!dest)
183 return -ENOMEM;
184
185 /* unset all pointers */
186 dest->manager = NULL;
187 dest->network = NULL;
188 dest->section = NULL;
189 dest->group = NULL;
190
191 HASHMAP_FOREACH(nhg, src->group) {
192 _cleanup_free_ struct nexthop_grp *g = NULL;
193
194 g = newdup(struct nexthop_grp, nhg, 1);
195 if (!g)
196 return -ENOMEM;
197
198 r = hashmap_ensure_put(&dest->group, NULL, UINT32_TO_PTR(g->id), g);
199 if (r < 0)
200 return r;
201 if (r > 0)
202 TAKE_PTR(g);
203 }
204
205 *ret = TAKE_PTR(dest);
206 return 0;
207 }
208
209 static bool nexthop_bound_to_link(const NextHop *nexthop) {
210 assert(nexthop);
211 return !nexthop->blackhole && hashmap_isempty(nexthop->group);
212 }
213
214 int nexthop_get_by_id(Manager *manager, uint32_t id, NextHop **ret) {
215 NextHop *nh;
216
217 assert(manager);
218
219 if (id == 0)
220 return -EINVAL;
221
222 nh = hashmap_get(manager->nexthops_by_id, UINT32_TO_PTR(id));
223 if (!nh)
224 return -ENOENT;
225
226 if (ret)
227 *ret = nh;
228 return 0;
229 }
230
231 static int nexthop_get(Link *link, const NextHop *in, NextHop **ret) {
232 NextHop *nexthop;
233 int ifindex;
234
235 assert(link);
236 assert(link->manager);
237 assert(in);
238
239 if (in->id > 0)
240 return nexthop_get_by_id(link->manager, in->id, ret);
241
242 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
243 * nexthop_section_verify(). */
244 assert(link->manager->manage_foreign_nexthops);
245
246 ifindex = nexthop_bound_to_link(in) ? link->ifindex : 0;
247
248 HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
249 if (nexthop->ifindex != ifindex)
250 continue;
251 if (nexthop_compare_full(nexthop, in) != 0)
252 continue;
253
254 if (ret)
255 *ret = nexthop;
256 return 0;
257 }
258
259 return -ENOENT;
260 }
261
262 static int nexthop_get_request_by_id(Manager *manager, uint32_t id, Request **ret) {
263 Request *req;
264
265 assert(manager);
266
267 req = ordered_set_get(
268 manager->request_queue,
269 &(Request) {
270 .type = REQUEST_TYPE_NEXTHOP,
271 .userdata = (void*) &(const NextHop) { .id = id },
272 .hash_func = (hash_func_t) nexthop_hash_func,
273 .compare_func = (compare_func_t) nexthop_compare_func,
274 });
275 if (!req)
276 return -ENOENT;
277
278 if (ret)
279 *ret = req;
280 return 0;
281 }
282
283 static int nexthop_get_request(Link *link, const NextHop *in, Request **ret) {
284 Request *req;
285 int ifindex;
286
287 assert(link);
288 assert(link->manager);
289 assert(in);
290
291 if (in->id > 0)
292 return nexthop_get_request_by_id(link->manager, in->id, ret);
293
294 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
295 * nexthop_section_verify(). */
296 assert(link->manager->manage_foreign_nexthops);
297
298 ifindex = nexthop_bound_to_link(in) ? link->ifindex : 0;
299
300 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
301 if (req->type != REQUEST_TYPE_NEXTHOP)
302 continue;
303
304 NextHop *nexthop = ASSERT_PTR(req->userdata);
305 if (nexthop->ifindex != ifindex)
306 continue;
307 if (nexthop_compare_full(nexthop, in) != 0)
308 continue;
309
310 if (ret)
311 *ret = req;
312 return 0;
313 }
314
315 return -ENOENT;
316 }
317
318 static int nexthop_add_new(Manager *manager, uint32_t id, NextHop **ret) {
319 _cleanup_(nexthop_freep) NextHop *nexthop = NULL;
320 int r;
321
322 assert(manager);
323 assert(id > 0);
324
325 r = nexthop_new(&nexthop);
326 if (r < 0)
327 return r;
328
329 nexthop->id = id;
330
331 r = hashmap_ensure_put(&manager->nexthops_by_id, &nexthop_hash_ops, UINT32_TO_PTR(nexthop->id), nexthop);
332 if (r < 0)
333 return r;
334 if (r == 0)
335 return -EEXIST;
336
337 nexthop->manager = manager;
338
339 if (ret)
340 *ret = nexthop;
341
342 TAKE_PTR(nexthop);
343 return 0;
344 }
345
346 static int nexthop_acquire_id(Manager *manager, NextHop *nexthop) {
347 assert(manager);
348 assert(nexthop);
349
350 if (nexthop->id > 0)
351 return 0;
352
353 /* If ManageForeignNextHops=no, nexthop with id == 0 should be already filtered by
354 * nexthop_section_verify(). */
355 assert(manager->manage_foreign_nexthops);
356
357 /* Find the lowest unused ID. */
358
359 for (uint32_t id = 1; id < UINT32_MAX; id++) {
360 if (nexthop_get_by_id(manager, id, NULL) >= 0)
361 continue;
362 if (nexthop_get_request_by_id(manager, id, NULL) >= 0)
363 continue;
364 if (set_contains(manager->nexthop_ids, UINT32_TO_PTR(id)))
365 continue;
366
367 nexthop->id = id;
368 return 0;
369 }
370
371 return -EBUSY;
372 }
373
374 static void log_nexthop_debug(const NextHop *nexthop, const char *str, Manager *manager) {
375 _cleanup_free_ char *state = NULL, *group = NULL, *flags = NULL;
376 struct nexthop_grp *nhg;
377 Link *link = NULL;
378
379 assert(nexthop);
380 assert(str);
381 assert(manager);
382
383 if (!DEBUG_LOGGING)
384 return;
385
386 (void) link_get_by_index(manager, nexthop->ifindex, &link);
387 (void) network_config_state_to_string_alloc(nexthop->state, &state);
388 (void) route_flags_to_string_alloc(nexthop->flags, &flags);
389
390 HASHMAP_FOREACH(nhg, nexthop->group)
391 (void) strextendf_with_separator(&group, ",", "%"PRIu32":%"PRIu32, nhg->id, nhg->weight+1u);
392
393 log_link_debug(link, "%s %s nexthop (%s): id: %"PRIu32", gw: %s, blackhole: %s, group: %s, flags: %s",
394 str, strna(network_config_source_to_string(nexthop->source)), strna(state),
395 nexthop->id,
396 IN_ADDR_TO_STRING(nexthop->family, &nexthop->gw),
397 yes_no(nexthop->blackhole), strna(group), strna(flags));
398 }
399
400 static int nexthop_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
401 int r;
402
403 assert(m);
404
405 /* link may be NULL. */
406
407 if (link && IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
408 return 1;
409
410 r = sd_netlink_message_get_errno(m);
411 if (r < 0 && r != -ENOENT)
412 log_link_message_warning_errno(link, m, r, "Could not drop nexthop, ignoring");
413
414 return 1;
415 }
416
417 static int nexthop_remove(NextHop *nexthop) {
418 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
419 Manager *manager;
420 Link *link = NULL;
421 Request *req;
422 int r;
423
424 assert(nexthop);
425 assert(nexthop->id > 0);
426
427 manager = ASSERT_PTR(nexthop->manager);
428
429 /* link may be NULL. */
430 (void) link_get_by_index(manager, nexthop->ifindex, &link);
431
432 log_nexthop_debug(nexthop, "Removing", manager);
433
434 r = sd_rtnl_message_new_nexthop(manager->rtnl, &m, RTM_DELNEXTHOP, AF_UNSPEC, RTPROT_UNSPEC);
435 if (r < 0)
436 return log_link_error_errno(link, r, "Could not create RTM_DELNEXTHOP message: %m");
437
438 r = sd_netlink_message_append_u32(m, NHA_ID, nexthop->id);
439 if (r < 0)
440 return log_link_error_errno(link, r, "Could not append NHA_ID attribute: %m");
441
442 r = netlink_call_async(manager->rtnl, NULL, m, nexthop_remove_handler,
443 link ? link_netlink_destroy_callback : NULL, link);
444 if (r < 0)
445 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
446
447 link_ref(link); /* link may be NULL, link_ref() is OK with that */
448
449 nexthop_enter_removing(nexthop);
450 if (nexthop_get_request_by_id(manager, nexthop->id, &req) >= 0)
451 nexthop_enter_removing(req->userdata);
452
453 return 0;
454 }
455
456 static int nexthop_configure(NextHop *nexthop, Link *link, Request *req) {
457 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
458 int r;
459
460 assert(nexthop);
461 assert(nexthop->id > 0);
462 assert(IN_SET(nexthop->family, AF_UNSPEC, AF_INET, AF_INET6));
463 assert(link);
464 assert(link->manager);
465 assert(link->manager->rtnl);
466 assert(link->ifindex > 0);
467 assert(req);
468
469 log_nexthop_debug(nexthop, "Configuring", link->manager);
470
471 r = sd_rtnl_message_new_nexthop(link->manager->rtnl, &m, RTM_NEWNEXTHOP, nexthop->family, nexthop->protocol);
472 if (r < 0)
473 return r;
474
475 r = sd_netlink_message_append_u32(m, NHA_ID, nexthop->id);
476 if (r < 0)
477 return r;
478
479 if (!hashmap_isempty(nexthop->group)) {
480 _cleanup_free_ struct nexthop_grp *group = NULL;
481 struct nexthop_grp *p, *nhg;
482
483 group = new(struct nexthop_grp, hashmap_size(nexthop->group));
484 if (!group)
485 return log_oom();
486
487 p = group;
488 HASHMAP_FOREACH(nhg, nexthop->group)
489 *p++ = *nhg;
490
491 r = sd_netlink_message_append_data(m, NHA_GROUP, group, sizeof(struct nexthop_grp) * hashmap_size(nexthop->group));
492 if (r < 0)
493 return r;
494
495 } else if (nexthop->blackhole) {
496 r = sd_netlink_message_append_flag(m, NHA_BLACKHOLE);
497 if (r < 0)
498 return r;
499 } else {
500 assert(nexthop->ifindex == link->ifindex);
501
502 r = sd_netlink_message_append_u32(m, NHA_OIF, nexthop->ifindex);
503 if (r < 0)
504 return r;
505
506 if (in_addr_is_set(nexthop->family, &nexthop->gw)) {
507 r = netlink_message_append_in_addr_union(m, NHA_GATEWAY, nexthop->family, &nexthop->gw);
508 if (r < 0)
509 return r;
510
511 r = sd_rtnl_message_nexthop_set_flags(m, nexthop->flags & RTNH_F_ONLINK);
512 if (r < 0)
513 return r;
514 }
515 }
516
517 return request_call_netlink_async(link->manager->rtnl, m, req);
518 }
519
520 static int static_nexthop_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, NextHop *nexthop) {
521 int r;
522
523 assert(m);
524 assert(link);
525
526 r = sd_netlink_message_get_errno(m);
527 if (r < 0 && r != -EEXIST) {
528 log_link_message_warning_errno(link, m, r, "Could not set nexthop");
529 link_enter_failed(link);
530 return 1;
531 }
532
533 if (link->static_nexthop_messages == 0) {
534 log_link_debug(link, "Nexthops set");
535 link->static_nexthops_configured = true;
536 link_check_ready(link);
537 }
538
539 return 1;
540 }
541
542 static bool nexthop_is_ready_to_configure(Link *link, const NextHop *nexthop) {
543 struct nexthop_grp *nhg;
544
545 assert(link);
546 assert(nexthop);
547 assert(nexthop->id > 0);
548
549 if (!link_is_ready_to_configure(link, false))
550 return false;
551
552 if (nexthop_bound_to_link(nexthop)) {
553 assert(nexthop->ifindex == link->ifindex);
554
555 /* TODO: fdb nexthop does not require IFF_UP. The conditions below needs to be updated
556 * when fdb nexthop support is added. See rtm_to_nh_config() in net/ipv4/nexthop.c of
557 * kernel. */
558 if (link->set_flags_messages > 0)
559 return false;
560 if (!FLAGS_SET(link->flags, IFF_UP))
561 return false;
562 }
563
564 /* All group members must be configured first. */
565 HASHMAP_FOREACH(nhg, nexthop->group) {
566 NextHop *g;
567
568 if (nexthop_get_by_id(link->manager, nhg->id, &g) < 0)
569 return false;
570
571 if (!nexthop_exists(g))
572 return false;
573 }
574
575 return gateway_is_ready(link, FLAGS_SET(nexthop->flags, RTNH_F_ONLINK), nexthop->family, &nexthop->gw);
576 }
577
578 static int nexthop_process_request(Request *req, Link *link, NextHop *nexthop) {
579 NextHop *existing;
580 int r;
581
582 assert(req);
583 assert(link);
584 assert(link->manager);
585 assert(nexthop);
586
587 if (!nexthop_is_ready_to_configure(link, nexthop))
588 return 0;
589
590 r = nexthop_configure(nexthop, link, req);
591 if (r < 0)
592 return log_link_warning_errno(link, r, "Failed to configure nexthop");
593
594 nexthop_enter_configuring(nexthop);
595 if (nexthop_get_by_id(link->manager, nexthop->id, &existing) >= 0)
596 nexthop_enter_configuring(existing);
597
598 return 1;
599 }
600
601 static int link_request_nexthop(Link *link, const NextHop *nexthop) {
602 _cleanup_(nexthop_freep) NextHop *tmp = NULL;
603 NextHop *existing = NULL;
604 int r;
605
606 assert(link);
607 assert(link->manager);
608 assert(nexthop);
609 assert(nexthop->source != NETWORK_CONFIG_SOURCE_FOREIGN);
610
611 if (nexthop_get_request(link, nexthop, NULL) >= 0)
612 return 0; /* already requested, skipping. */
613
614 r = nexthop_dup(nexthop, &tmp);
615 if (r < 0)
616 return r;
617
618 if (nexthop_get(link, nexthop, &existing) < 0) {
619 r = nexthop_acquire_id(link->manager, tmp);
620 if (r < 0)
621 return r;
622 } else {
623 /* Copy ID */
624 assert(tmp->id == 0 || tmp->id == existing->id);
625 tmp->id = existing->id;
626
627 /* Copy state for logging below. */
628 tmp->state = existing->state;
629 }
630
631 if (nexthop_bound_to_link(tmp))
632 tmp->ifindex = link->ifindex;
633
634 log_nexthop_debug(tmp, "Requesting", link->manager);
635 r = link_queue_request_safe(link, REQUEST_TYPE_NEXTHOP,
636 tmp,
637 nexthop_free,
638 nexthop_hash_func,
639 nexthop_compare_func,
640 nexthop_process_request,
641 &link->static_nexthop_messages,
642 static_nexthop_handler,
643 NULL);
644 if (r <= 0)
645 return r;
646
647 nexthop_enter_requesting(tmp);
648 if (existing)
649 nexthop_enter_requesting(existing);
650
651 TAKE_PTR(tmp);
652 return 1;
653 }
654
655 int link_request_static_nexthops(Link *link, bool only_ipv4) {
656 NextHop *nh;
657 int r;
658
659 assert(link);
660 assert(link->network);
661
662 link->static_nexthops_configured = false;
663
664 ORDERED_HASHMAP_FOREACH(nh, link->network->nexthops_by_section) {
665 if (only_ipv4 && nh->family != AF_INET)
666 continue;
667
668 r = link_request_nexthop(link, nh);
669 if (r < 0)
670 return log_link_warning_errno(link, r, "Could not request nexthop: %m");
671 }
672
673 if (link->static_nexthop_messages == 0) {
674 link->static_nexthops_configured = true;
675 link_check_ready(link);
676 } else {
677 log_link_debug(link, "Requesting nexthops");
678 link_set_state(link, LINK_STATE_CONFIGURING);
679 }
680
681 return 0;
682 }
683
684 static bool nexthop_can_update(const NextHop *assigned_nexthop, const NextHop *requested_nexthop) {
685 assert(assigned_nexthop);
686 assert(assigned_nexthop->manager);
687 assert(requested_nexthop);
688 assert(requested_nexthop->network);
689
690 /* A group nexthop cannot be replaced with a non-group nexthop, and vice versa.
691 * See replace_nexthop_grp() and replace_nexthop_single() in net/ipv4/nexthop.c of the kernel. */
692 if (hashmap_isempty(assigned_nexthop->group) != hashmap_isempty(requested_nexthop->group))
693 return false;
694
695 /* There are several more conditions if we can replace a group nexthop, e.g. hash threshold and
696 * resilience. But, currently we do not support to modify that. Let's add checks for them in the
697 * future when we support to configure them.*/
698
699 /* When a nexthop is replaced with a blackhole nexthop, and a group nexthop has multiple nexthops
700 * including this nexthop, then the kernel refuses to replace the existing nexthop.
701 * So, here, for simplicity, let's unconditionally refuse to replace a non-blackhole nexthop with
702 * a blackhole nexthop. See replace_nexthop() in net/ipv4/nexthop.c of the kernel. */
703 if (!assigned_nexthop->blackhole && requested_nexthop->blackhole)
704 return false;
705
706 return true;
707 }
708
709 static void link_mark_nexthops(Link *link, bool foreign) {
710 NextHop *nexthop;
711 Link *other;
712
713 assert(link);
714 assert(link->manager);
715
716 /* First, mark all nexthops. */
717 HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
718 /* do not touch nexthop created by the kernel */
719 if (nexthop->protocol == RTPROT_KERNEL)
720 continue;
721
722 /* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
723 if (foreign != (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN))
724 continue;
725
726 /* Ignore nexthops not assigned yet or already removed. */
727 if (!nexthop_exists(nexthop))
728 continue;
729
730 /* Ignore nexthops bound to other links. */
731 if (nexthop->ifindex > 0 && nexthop->ifindex != link->ifindex)
732 continue;
733
734 nexthop_mark(nexthop);
735 }
736
737 /* Then, unmark all nexthops requested by active links. */
738 HASHMAP_FOREACH(other, link->manager->links_by_index) {
739 if (!foreign && other == link)
740 continue;
741
742 if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
743 continue;
744
745 ORDERED_HASHMAP_FOREACH(nexthop, other->network->nexthops_by_section) {
746 NextHop *existing;
747
748 if (nexthop_get(other, nexthop, &existing) < 0)
749 continue;
750
751 if (!nexthop_can_update(existing, nexthop))
752 continue;
753
754 /* Found matching static configuration. Keep the existing nexthop. */
755 nexthop_unmark(existing);
756 }
757 }
758 }
759
760 int link_drop_nexthops(Link *link, bool foreign) {
761 NextHop *nexthop;
762 int r = 0;
763
764 assert(link);
765 assert(link->manager);
766
767 link_mark_nexthops(link, foreign);
768
769 HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
770 if (!nexthop_is_marked(nexthop))
771 continue;
772
773 RET_GATHER(r, nexthop_remove(nexthop));
774 }
775
776 return r;
777 }
778
779 void link_foreignize_nexthops(Link *link) {
780 NextHop *nexthop;
781
782 assert(link);
783 assert(link->manager);
784
785 link_mark_nexthops(link, /* foreign = */ false);
786
787 HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
788 if (!nexthop_is_marked(nexthop))
789 continue;
790
791 nexthop->source = NETWORK_CONFIG_SOURCE_FOREIGN;
792 }
793 }
794
795 static int nexthop_update_group(NextHop *nexthop, const struct nexthop_grp *group, size_t size) {
796 _cleanup_hashmap_free_free_ Hashmap *h = NULL;
797 size_t n_group;
798 int r;
799
800 assert(nexthop);
801 assert(group || size == 0);
802
803 if (size == 0 || size % sizeof(struct nexthop_grp) != 0)
804 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
805 "rtnl: received nexthop message with invalid nexthop group size, ignoring.");
806
807 if ((uintptr_t) group % alignof(struct nexthop_grp) != 0)
808 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
809 "rtnl: received nexthop message with invalid alignment, ignoring.");
810
811 n_group = size / sizeof(struct nexthop_grp);
812 for (size_t i = 0; i < n_group; i++) {
813 _cleanup_free_ struct nexthop_grp *nhg = NULL;
814
815 if (group[i].id == 0) {
816 log_debug("rtnl: received nexthop message with invalid ID in group, ignoring.");
817 continue;
818 }
819
820 if (group[i].weight > 254) {
821 log_debug("rtnl: received nexthop message with invalid weight in group, ignoring.");
822 continue;
823 }
824
825 nhg = newdup(struct nexthop_grp, group + i, 1);
826 if (!nhg)
827 return log_oom();
828
829 r = hashmap_ensure_put(&h, NULL, UINT32_TO_PTR(nhg->id), nhg);
830 if (r == -ENOMEM)
831 return log_oom();
832 if (r < 0) {
833 log_debug_errno(r, "Failed to store nexthop group, ignoring: %m");
834 continue;
835 }
836 if (r > 0)
837 TAKE_PTR(nhg);
838 }
839
840 hashmap_free_free(nexthop->group);
841 nexthop->group = TAKE_PTR(h);
842 return 0;
843 }
844
845 int manager_rtnl_process_nexthop(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
846 _cleanup_free_ void *raw_group = NULL;
847 size_t raw_group_size;
848 uint16_t type;
849 uint32_t id, ifindex;
850 NextHop *nexthop = NULL;
851 Request *req = NULL;
852 bool is_new = false;
853 int r;
854
855 assert(rtnl);
856 assert(message);
857 assert(m);
858
859 if (sd_netlink_message_is_error(message)) {
860 r = sd_netlink_message_get_errno(message);
861 if (r < 0)
862 log_message_warning_errno(message, r, "rtnl: failed to receive rule message, ignoring");
863
864 return 0;
865 }
866
867 r = sd_netlink_message_get_type(message, &type);
868 if (r < 0) {
869 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
870 return 0;
871 } else if (!IN_SET(type, RTM_NEWNEXTHOP, RTM_DELNEXTHOP)) {
872 log_warning("rtnl: received unexpected message type %u when processing nexthop, ignoring.", type);
873 return 0;
874 }
875
876 r = sd_netlink_message_read_u32(message, NHA_ID, &id);
877 if (r == -ENODATA) {
878 log_warning_errno(r, "rtnl: received nexthop message without NHA_ID attribute, ignoring: %m");
879 return 0;
880 } else if (r < 0) {
881 log_warning_errno(r, "rtnl: could not get NHA_ID attribute, ignoring: %m");
882 return 0;
883 } else if (id == 0) {
884 log_warning("rtnl: received nexthop message with invalid nexthop ID, ignoring: %m");
885 return 0;
886 }
887
888 (void) nexthop_get_by_id(m, id, &nexthop);
889 (void) nexthop_get_request_by_id(m, id, &req);
890
891 if (type == RTM_DELNEXTHOP) {
892 if (nexthop) {
893 nexthop_enter_removed(nexthop);
894 log_nexthop_debug(nexthop, "Forgetting removed", m);
895 nexthop_free(nexthop);
896 } else
897 log_nexthop_debug(&(const NextHop) { .id = id }, "Kernel removed unknown", m);
898
899 if (req)
900 nexthop_enter_removed(req->userdata);
901
902 return 0;
903 }
904
905 /* If we did not know the nexthop, then save it. */
906 if (!nexthop) {
907 r = nexthop_add_new(m, id, &nexthop);
908 if (r < 0) {
909 log_warning_errno(r, "Failed to add received nexthop, ignoring: %m");
910 return 0;
911 }
912
913 is_new = true;
914 }
915
916 /* Also update information that cannot be obtained through netlink notification. */
917 if (req && req->waiting_reply) {
918 NextHop *n = ASSERT_PTR(req->userdata);
919
920 nexthop->source = n->source;
921 }
922
923 r = sd_rtnl_message_get_family(message, &nexthop->family);
924 if (r < 0)
925 log_debug_errno(r, "rtnl: could not get nexthop family, ignoring: %m");
926
927 r = sd_rtnl_message_nexthop_get_protocol(message, &nexthop->protocol);
928 if (r < 0)
929 log_debug_errno(r, "rtnl: could not get nexthop protocol, ignoring: %m");
930
931 r = sd_rtnl_message_nexthop_get_flags(message, &nexthop->flags);
932 if (r < 0)
933 log_debug_errno(r, "rtnl: could not get nexthop flags, ignoring: %m");
934
935 r = sd_netlink_message_read_data(message, NHA_GROUP, &raw_group_size, &raw_group);
936 if (r == -ENODATA)
937 nexthop->group = hashmap_free_free(nexthop->group);
938 else if (r < 0)
939 log_debug_errno(r, "rtnl: could not get NHA_GROUP attribute, ignoring: %m");
940 else
941 (void) nexthop_update_group(nexthop, raw_group, raw_group_size);
942
943 if (nexthop->family != AF_UNSPEC) {
944 r = netlink_message_read_in_addr_union(message, NHA_GATEWAY, nexthop->family, &nexthop->gw);
945 if (r == -ENODATA)
946 nexthop->gw = IN_ADDR_NULL;
947 else if (r < 0)
948 log_debug_errno(r, "rtnl: could not get NHA_GATEWAY attribute, ignoring: %m");
949 }
950
951 r = sd_netlink_message_has_flag(message, NHA_BLACKHOLE);
952 if (r < 0)
953 log_debug_errno(r, "rtnl: could not get NHA_BLACKHOLE attribute, ignoring: %m");
954 else
955 nexthop->blackhole = r;
956
957 r = sd_netlink_message_read_u32(message, NHA_OIF, &ifindex);
958 if (r == -ENODATA)
959 nexthop->ifindex = 0;
960 else if (r < 0)
961 log_debug_errno(r, "rtnl: could not get NHA_OIF attribute, ignoring: %m");
962 else if (ifindex > INT32_MAX)
963 log_debug_errno(r, "rtnl: received invalid NHA_OIF attribute, ignoring: %m");
964 else
965 nexthop->ifindex = (int) ifindex;
966
967 /* All blackhole or group nexthops are managed by Manager. Note that the linux kernel does not
968 * set NHA_OID attribute when NHA_BLACKHOLE or NHA_GROUP is set. Just for safety. */
969 if (!nexthop_bound_to_link(nexthop))
970 nexthop->ifindex = 0;
971
972 nexthop_enter_configured(nexthop);
973 if (req)
974 nexthop_enter_configured(req->userdata);
975
976 log_nexthop_debug(nexthop, is_new ? "Remembering" : "Received remembered", m);
977 return 1;
978 }
979
980 static int nexthop_section_verify(NextHop *nh) {
981 if (section_is_invalid(nh->section))
982 return -EINVAL;
983
984 if (!nh->network->manager->manage_foreign_nexthops && nh->id == 0)
985 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
986 "%s: [NextHop] section without specifying Id= is not supported "
987 "if ManageForeignNextHops=no is set in networkd.conf. "
988 "Ignoring [NextHop] section from line %u.",
989 nh->section->filename, nh->section->line);
990
991 if (!hashmap_isempty(nh->group)) {
992 if (in_addr_is_set(nh->family, &nh->gw))
993 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
994 "%s: nexthop group cannot have gateway address. "
995 "Ignoring [NextHop] section from line %u.",
996 nh->section->filename, nh->section->line);
997
998 if (nh->family != AF_UNSPEC)
999 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1000 "%s: nexthop group cannot have Family= setting. "
1001 "Ignoring [NextHop] section from line %u.",
1002 nh->section->filename, nh->section->line);
1003
1004 if (nh->blackhole)
1005 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1006 "%s: nexthop group cannot be a blackhole. "
1007 "Ignoring [NextHop] section from line %u.",
1008 nh->section->filename, nh->section->line);
1009
1010 if (nh->onlink > 0)
1011 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1012 "%s: nexthop group cannot have on-link flag. "
1013 "Ignoring [NextHop] section from line %u.",
1014 nh->section->filename, nh->section->line);
1015 } else if (nh->family == AF_UNSPEC)
1016 /* When neither Family=, Gateway=, nor Group= is specified, assume IPv4. */
1017 nh->family = AF_INET;
1018
1019 if (nh->blackhole) {
1020 if (in_addr_is_set(nh->family, &nh->gw))
1021 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1022 "%s: blackhole nexthop cannot have gateway address. "
1023 "Ignoring [NextHop] section from line %u.",
1024 nh->section->filename, nh->section->line);
1025
1026 if (nh->onlink > 0)
1027 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1028 "%s: blackhole nexthop cannot have on-link flag. "
1029 "Ignoring [NextHop] section from line %u.",
1030 nh->section->filename, nh->section->line);
1031 }
1032
1033 if (nh->onlink < 0 && in_addr_is_set(nh->family, &nh->gw) &&
1034 ordered_hashmap_isempty(nh->network->addresses_by_section)) {
1035 /* If no address is configured, in most cases the gateway cannot be reachable.
1036 * TODO: we may need to improve the condition above. */
1037 log_warning("%s: Gateway= without static address configured. "
1038 "Enabling OnLink= option.",
1039 nh->section->filename);
1040 nh->onlink = true;
1041 }
1042
1043 if (nh->onlink >= 0)
1044 SET_FLAG(nh->flags, RTNH_F_ONLINK, nh->onlink);
1045
1046 return 0;
1047 }
1048
1049 int network_drop_invalid_nexthops(Network *network) {
1050 _cleanup_hashmap_free_ Hashmap *nexthops = NULL;
1051 NextHop *nh;
1052 int r;
1053
1054 assert(network);
1055
1056 ORDERED_HASHMAP_FOREACH(nh, network->nexthops_by_section) {
1057 if (nexthop_section_verify(nh) < 0) {
1058 nexthop_free(nh);
1059 continue;
1060 }
1061
1062 if (nh->id == 0)
1063 continue;
1064
1065 /* Always use the setting specified later. So, remove the previously assigned setting. */
1066 NextHop *dup = hashmap_remove(nexthops, UINT32_TO_PTR(nh->id));
1067 if (dup) {
1068 log_warning("%s: Duplicated nexthop settings for ID %"PRIu32" is specified at line %u and %u, "
1069 "dropping the nexthop setting specified at line %u.",
1070 dup->section->filename,
1071 nh->id, nh->section->line,
1072 dup->section->line, dup->section->line);
1073 /* nexthop_free() will drop the nexthop from nexthops_by_section. */
1074 nexthop_free(dup);
1075 }
1076
1077 r = hashmap_ensure_put(&nexthops, NULL, UINT32_TO_PTR(nh->id), nh);
1078 if (r < 0)
1079 return log_oom();
1080 assert(r > 0);
1081 }
1082
1083 return 0;
1084 }
1085
1086 int manager_build_nexthop_ids(Manager *manager) {
1087 Network *network;
1088 int r;
1089
1090 assert(manager);
1091
1092 if (!manager->manage_foreign_nexthops)
1093 return 0;
1094
1095 manager->nexthop_ids = set_free(manager->nexthop_ids);
1096
1097 ORDERED_HASHMAP_FOREACH(network, manager->networks) {
1098 NextHop *nh;
1099
1100 ORDERED_HASHMAP_FOREACH(nh, network->nexthops_by_section) {
1101 if (nh->id == 0)
1102 continue;
1103
1104 r = set_ensure_put(&manager->nexthop_ids, NULL, UINT32_TO_PTR(nh->id));
1105 if (r < 0)
1106 return r;
1107 }
1108 }
1109
1110 return 0;
1111 }
1112
1113 int config_parse_nexthop_id(
1114 const char *unit,
1115 const char *filename,
1116 unsigned line,
1117 const char *section,
1118 unsigned section_line,
1119 const char *lvalue,
1120 int ltype,
1121 const char *rvalue,
1122 void *data,
1123 void *userdata) {
1124
1125 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1126 Network *network = userdata;
1127 uint32_t id;
1128 int r;
1129
1130 assert(filename);
1131 assert(section);
1132 assert(lvalue);
1133 assert(rvalue);
1134 assert(data);
1135
1136 r = nexthop_new_static(network, filename, section_line, &n);
1137 if (r < 0)
1138 return log_oom();
1139
1140 if (isempty(rvalue)) {
1141 n->id = 0;
1142 TAKE_PTR(n);
1143 return 0;
1144 }
1145
1146 r = safe_atou32(rvalue, &id);
1147 if (r < 0) {
1148 log_syntax(unit, LOG_WARNING, filename, line, r,
1149 "Could not parse nexthop id \"%s\", ignoring assignment: %m", rvalue);
1150 return 0;
1151 }
1152 if (id == 0) {
1153 log_syntax(unit, LOG_WARNING, filename, line, 0,
1154 "Invalid nexthop id \"%s\", ignoring assignment: %m", rvalue);
1155 return 0;
1156 }
1157
1158 n->id = id;
1159 TAKE_PTR(n);
1160 return 0;
1161 }
1162
1163 int config_parse_nexthop_gateway(
1164 const char *unit,
1165 const char *filename,
1166 unsigned line,
1167 const char *section,
1168 unsigned section_line,
1169 const char *lvalue,
1170 int ltype,
1171 const char *rvalue,
1172 void *data,
1173 void *userdata) {
1174
1175 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1176 Network *network = userdata;
1177 int r;
1178
1179 assert(filename);
1180 assert(section);
1181 assert(lvalue);
1182 assert(rvalue);
1183 assert(data);
1184
1185 r = nexthop_new_static(network, filename, section_line, &n);
1186 if (r < 0)
1187 return log_oom();
1188
1189 if (isempty(rvalue)) {
1190 n->family = AF_UNSPEC;
1191 n->gw = IN_ADDR_NULL;
1192
1193 TAKE_PTR(n);
1194 return 0;
1195 }
1196
1197 r = in_addr_from_string_auto(rvalue, &n->family, &n->gw);
1198 if (r < 0) {
1199 log_syntax(unit, LOG_WARNING, filename, line, r,
1200 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1201 return 0;
1202 }
1203
1204 TAKE_PTR(n);
1205 return 0;
1206 }
1207
1208 int config_parse_nexthop_family(
1209 const char *unit,
1210 const char *filename,
1211 unsigned line,
1212 const char *section,
1213 unsigned section_line,
1214 const char *lvalue,
1215 int ltype,
1216 const char *rvalue,
1217 void *data,
1218 void *userdata) {
1219
1220 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1221 Network *network = userdata;
1222 AddressFamily a;
1223 int r;
1224
1225 assert(filename);
1226 assert(section);
1227 assert(lvalue);
1228 assert(rvalue);
1229 assert(data);
1230
1231 r = nexthop_new_static(network, filename, section_line, &n);
1232 if (r < 0)
1233 return log_oom();
1234
1235 if (isempty(rvalue) &&
1236 !in_addr_is_set(n->family, &n->gw)) {
1237 /* Accept an empty string only when Gateway= is null or not specified. */
1238 n->family = AF_UNSPEC;
1239 TAKE_PTR(n);
1240 return 0;
1241 }
1242
1243 a = nexthop_address_family_from_string(rvalue);
1244 if (a < 0) {
1245 log_syntax(unit, LOG_WARNING, filename, line, 0,
1246 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1247 return 0;
1248 }
1249
1250 if (in_addr_is_set(n->family, &n->gw) &&
1251 ((a == ADDRESS_FAMILY_IPV4 && n->family == AF_INET6) ||
1252 (a == ADDRESS_FAMILY_IPV6 && n->family == AF_INET))) {
1253 log_syntax(unit, LOG_WARNING, filename, line, 0,
1254 "Specified family '%s' conflicts with the family of the previously specified Gateway=, "
1255 "ignoring assignment.", rvalue);
1256 return 0;
1257 }
1258
1259 switch (a) {
1260 case ADDRESS_FAMILY_IPV4:
1261 n->family = AF_INET;
1262 break;
1263 case ADDRESS_FAMILY_IPV6:
1264 n->family = AF_INET6;
1265 break;
1266 default:
1267 assert_not_reached();
1268 }
1269
1270 TAKE_PTR(n);
1271 return 0;
1272 }
1273
1274 int config_parse_nexthop_onlink(
1275 const char *unit,
1276 const char *filename,
1277 unsigned line,
1278 const char *section,
1279 unsigned section_line,
1280 const char *lvalue,
1281 int ltype,
1282 const char *rvalue,
1283 void *data,
1284 void *userdata) {
1285
1286 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1287 Network *network = userdata;
1288 int r;
1289
1290 assert(filename);
1291 assert(section);
1292 assert(lvalue);
1293 assert(rvalue);
1294 assert(data);
1295
1296 r = nexthop_new_static(network, filename, section_line, &n);
1297 if (r < 0)
1298 return log_oom();
1299
1300 r = parse_tristate(rvalue, &n->onlink);
1301 if (r < 0) {
1302 log_syntax(unit, LOG_WARNING, filename, line, r,
1303 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
1304 return 0;
1305 }
1306
1307 TAKE_PTR(n);
1308 return 0;
1309 }
1310
1311 int config_parse_nexthop_blackhole(
1312 const char *unit,
1313 const char *filename,
1314 unsigned line,
1315 const char *section,
1316 unsigned section_line,
1317 const char *lvalue,
1318 int ltype,
1319 const char *rvalue,
1320 void *data,
1321 void *userdata) {
1322
1323 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1324 Network *network = userdata;
1325 int r;
1326
1327 assert(filename);
1328 assert(section);
1329 assert(lvalue);
1330 assert(rvalue);
1331 assert(data);
1332
1333 r = nexthop_new_static(network, filename, section_line, &n);
1334 if (r < 0)
1335 return log_oom();
1336
1337 r = parse_boolean(rvalue);
1338 if (r < 0) {
1339 log_syntax(unit, LOG_WARNING, filename, line, r,
1340 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
1341 return 0;
1342 }
1343
1344 n->blackhole = r;
1345
1346 TAKE_PTR(n);
1347 return 0;
1348 }
1349
1350 int config_parse_nexthop_group(
1351 const char *unit,
1352 const char *filename,
1353 unsigned line,
1354 const char *section,
1355 unsigned section_line,
1356 const char *lvalue,
1357 int ltype,
1358 const char *rvalue,
1359 void *data,
1360 void *userdata) {
1361
1362 _cleanup_(nexthop_free_or_set_invalidp) NextHop *n = NULL;
1363 Network *network = userdata;
1364 int r;
1365
1366 assert(filename);
1367 assert(section);
1368 assert(lvalue);
1369 assert(rvalue);
1370 assert(data);
1371
1372 r = nexthop_new_static(network, filename, section_line, &n);
1373 if (r < 0)
1374 return log_oom();
1375
1376 if (isempty(rvalue)) {
1377 n->group = hashmap_free_free(n->group);
1378 TAKE_PTR(n);
1379 return 0;
1380 }
1381
1382 for (const char *p = rvalue;;) {
1383 _cleanup_free_ struct nexthop_grp *nhg = NULL;
1384 _cleanup_free_ char *word = NULL;
1385 uint32_t w;
1386 char *sep;
1387
1388 r = extract_first_word(&p, &word, NULL, 0);
1389 if (r == -ENOMEM)
1390 return log_oom();
1391 if (r < 0) {
1392 log_syntax(unit, LOG_WARNING, filename, line, r,
1393 "Invalid %s=, ignoring assignment: %s", lvalue, rvalue);
1394 return 0;
1395 }
1396 if (r == 0)
1397 break;
1398
1399 nhg = new0(struct nexthop_grp, 1);
1400 if (!nhg)
1401 return log_oom();
1402
1403 sep = strchr(word, ':');
1404 if (sep) {
1405 *sep++ = '\0';
1406 r = safe_atou32(sep, &w);
1407 if (r < 0) {
1408 log_syntax(unit, LOG_WARNING, filename, line, r,
1409 "Failed to parse weight for nexthop group, ignoring assignment: %s:%s",
1410 word, sep);
1411 continue;
1412 }
1413 if (w == 0 || w > 256) {
1414 log_syntax(unit, LOG_WARNING, filename, line, 0,
1415 "Invalid weight for nexthop group, ignoring assignment: %s:%s",
1416 word, sep);
1417 continue;
1418 }
1419 /* See comments in config_parse_multipath_route(). */
1420 nhg->weight = w - 1;
1421 }
1422
1423 r = safe_atou32(word, &nhg->id);
1424 if (r < 0) {
1425 log_syntax(unit, LOG_WARNING, filename, line, r,
1426 "Failed to parse nexthop ID in %s=, ignoring assignment: %s%s%s",
1427 lvalue, word, sep ? ":" : "", strempty(sep));
1428 continue;
1429 }
1430 if (nhg->id == 0) {
1431 log_syntax(unit, LOG_WARNING, filename, line, 0,
1432 "Nexthop ID in %s= must be positive, ignoring assignment: %s%s%s",
1433 lvalue, word, sep ? ":" : "", strempty(sep));
1434 continue;
1435 }
1436
1437 r = hashmap_ensure_put(&n->group, NULL, UINT32_TO_PTR(nhg->id), nhg);
1438 if (r == -ENOMEM)
1439 return log_oom();
1440 if (r == -EEXIST) {
1441 log_syntax(unit, LOG_WARNING, filename, line, r,
1442 "Nexthop ID %"PRIu32" is specified multiple times in %s=, ignoring assignment: %s%s%s",
1443 nhg->id, lvalue, word, sep ? ":" : "", strempty(sep));
1444 continue;
1445 }
1446 assert(r > 0);
1447 TAKE_PTR(nhg);
1448 }
1449
1450 TAKE_PTR(n);
1451 return 0;
1452 }