2 * Copyright (c) 2014-2015 Timo Teräs
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
14 #include <netinet/if_ether.h>
23 #include "nhrp_protocol.h"
26 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_PEER
, "NHRP peer entry");
29 uint8_t priority_version
;
34 struct in6_addr saddr
;
35 struct in6_addr daddr
;
38 static void nhrp_packet_debug(struct zbuf
*zb
, const char *dir
);
40 static void nhrp_peer_check_delete(struct nhrp_peer
*p
)
42 struct nhrp_interface
*nifp
= p
->ifp
->info
;
44 if (p
->ref
|| notifier_active(&p
->notifier_list
))
47 debugf(NHRP_DEBUG_COMMON
, "Deleting peer ref:%d remote:%pSU local:%pSU",
48 p
->ref
, &p
->vc
->remote
.nbma
, &p
->vc
->local
.nbma
);
50 THREAD_OFF(p
->t_fallback
);
51 THREAD_OFF(p
->t_timer
);
52 hash_release(nifp
->peer_hash
, p
);
53 nhrp_interface_notify_del(p
->ifp
, &p
->ifp_notifier
);
54 nhrp_vc_notify_del(p
->vc
, &p
->vc_notifier
);
55 XFREE(MTYPE_NHRP_PEER
, p
);
58 static void nhrp_peer_notify_up(struct thread
*t
)
60 struct nhrp_peer
*p
= THREAD_ARG(t
);
61 struct nhrp_vc
*vc
= p
->vc
;
62 struct interface
*ifp
= p
->ifp
;
63 struct nhrp_interface
*nifp
= ifp
->info
;
66 if (nifp
->enabled
&& (!nifp
->ipsec_profile
|| vc
->ipsec
)) {
69 notifier_call(&p
->notifier_list
, NOTIFY_PEER_UP
);
74 static void __nhrp_peer_check(struct nhrp_peer
*p
)
76 struct nhrp_vc
*vc
= p
->vc
;
77 struct interface
*ifp
= p
->ifp
;
78 struct nhrp_interface
*nifp
= ifp
->info
;
81 online
= nifp
->enabled
&& (!nifp
->ipsec_profile
|| vc
->ipsec
);
82 if (p
->online
!= online
) {
83 THREAD_OFF(p
->t_fallback
);
84 if (online
&& notifier_active(&p
->notifier_list
)) {
85 /* If we requested the IPsec connection, delay
86 * the up notification a bit to allow things
87 * settle down. This allows IKE to install
89 thread_add_timer_msec(master
, nhrp_peer_notify_up
, p
,
95 notifier_call(&p
->notifier_list
,
98 p
->requested
= p
->fallback_requested
= 0;
99 notifier_call(&p
->notifier_list
,
107 static void nhrp_peer_vc_notify(struct notifier_block
*n
, unsigned long cmd
)
109 struct nhrp_peer
*p
= container_of(n
, struct nhrp_peer
, vc_notifier
);
112 case NOTIFY_VC_IPSEC_CHANGED
:
113 __nhrp_peer_check(p
);
115 case NOTIFY_VC_IPSEC_UPDATE_NBMA
:
117 notifier_call(&p
->notifier_list
, NOTIFY_PEER_NBMA_CHANGING
);
123 static void nhrp_peer_ifp_notify(struct notifier_block
*n
, unsigned long cmd
)
125 struct nhrp_peer
*p
= container_of(n
, struct nhrp_peer
, ifp_notifier
);
126 struct nhrp_interface
*nifp
;
131 case NOTIFY_INTERFACE_UP
:
132 case NOTIFY_INTERFACE_DOWN
:
133 __nhrp_peer_check(p
);
135 case NOTIFY_INTERFACE_NBMA_CHANGED
:
136 /* Source NBMA changed, rebind to new VC */
138 vc
= nhrp_vc_get(&nifp
->nbma
, &p
->vc
->remote
.nbma
, 1);
139 if (vc
&& p
->vc
!= vc
) {
140 nhrp_vc_notify_del(p
->vc
, &p
->vc_notifier
);
142 nhrp_vc_notify_add(p
->vc
, &p
->vc_notifier
,
143 nhrp_peer_vc_notify
);
144 __nhrp_peer_check(p
);
146 /* fallthru */ /* to post config update */
147 case NOTIFY_INTERFACE_ADDRESS_CHANGED
:
148 notifier_call(&p
->notifier_list
, NOTIFY_PEER_IFCONFIG_CHANGED
);
150 case NOTIFY_INTERFACE_IPSEC_CHANGED
:
151 __nhrp_peer_check(p
);
152 notifier_call(&p
->notifier_list
, NOTIFY_PEER_IFCONFIG_CHANGED
);
154 case NOTIFY_INTERFACE_MTU_CHANGED
:
155 notifier_call(&p
->notifier_list
, NOTIFY_PEER_MTU_CHANGED
);
161 static unsigned int nhrp_peer_key(const void *peer_data
)
163 const struct nhrp_peer
*p
= peer_data
;
164 return sockunion_hash(&p
->vc
->remote
.nbma
);
167 static bool nhrp_peer_cmp(const void *cache_data
, const void *key_data
)
169 const struct nhrp_peer
*a
= cache_data
;
170 const struct nhrp_peer
*b
= key_data
;
172 return a
->ifp
== b
->ifp
&& a
->vc
== b
->vc
;
175 static void *nhrp_peer_create(void *data
)
177 struct nhrp_peer
*p
, *key
= data
;
179 p
= XMALLOC(MTYPE_NHRP_PEER
, sizeof(*p
));
181 *p
= (struct nhrp_peer
){
185 .notifier_list
= NOTIFIER_LIST_INITIALIZER(&p
->notifier_list
),
187 nhrp_vc_notify_add(p
->vc
, &p
->vc_notifier
, nhrp_peer_vc_notify
);
188 nhrp_interface_notify_add(p
->ifp
, &p
->ifp_notifier
,
189 nhrp_peer_ifp_notify
);
194 static void do_peer_hash_free(void *hb_data
)
196 struct nhrp_peer
*p
= (struct nhrp_peer
*)hb_data
;
198 nhrp_peer_check_delete(p
);
201 void nhrp_peer_interface_del(struct interface
*ifp
)
203 struct nhrp_interface
*nifp
= ifp
->info
;
205 debugf(NHRP_DEBUG_COMMON
, "Cleaning up undeleted peer entries (%lu)",
206 nifp
->peer_hash
? nifp
->peer_hash
->count
: 0);
208 if (nifp
->peer_hash
) {
209 hash_clean(nifp
->peer_hash
, do_peer_hash_free
);
210 assert(nifp
->peer_hash
->count
== 0);
211 hash_free(nifp
->peer_hash
);
212 nifp
->peer_hash
= NULL
;
216 struct nhrp_peer
*nhrp_peer_get(struct interface
*ifp
,
217 const union sockunion
*remote_nbma
)
219 struct nhrp_interface
*nifp
= ifp
->info
;
220 struct nhrp_peer key
, *p
;
223 if (!nifp
->peer_hash
) {
224 nifp
->peer_hash
= hash_create(nhrp_peer_key
, nhrp_peer_cmp
,
226 if (!nifp
->peer_hash
)
230 vc
= nhrp_vc_get(&nifp
->nbma
, remote_nbma
, 1);
237 p
= hash_get(nifp
->peer_hash
, &key
, nhrp_peer_create
);
240 __nhrp_peer_check(p
);
245 struct nhrp_peer
*nhrp_peer_ref(struct nhrp_peer
*p
)
252 void nhrp_peer_unref(struct nhrp_peer
*p
)
256 nhrp_peer_check_delete(p
);
260 static void nhrp_peer_request_timeout(struct thread
*t
)
262 struct nhrp_peer
*p
= THREAD_ARG(t
);
263 struct nhrp_vc
*vc
= p
->vc
;
264 struct interface
*ifp
= p
->ifp
;
265 struct nhrp_interface
*nifp
= ifp
->info
;
271 if (nifp
->ipsec_fallback_profile
&& !p
->prio
272 && !p
->fallback_requested
) {
273 p
->fallback_requested
= 1;
274 vici_request_vc(nifp
->ipsec_fallback_profile
, &vc
->local
.nbma
,
275 &vc
->remote
.nbma
, p
->prio
);
276 thread_add_timer(master
, nhrp_peer_request_timeout
, p
, 30,
279 p
->requested
= p
->fallback_requested
= 0;
283 static void nhrp_peer_defer_vici_request(struct thread
*t
)
285 struct nhrp_peer
*p
= THREAD_ARG(t
);
286 struct nhrp_vc
*vc
= p
->vc
;
287 struct interface
*ifp
= p
->ifp
;
288 struct nhrp_interface
*nifp
= ifp
->info
;
290 THREAD_OFF(p
->t_timer
);
293 debugf(NHRP_DEBUG_COMMON
,
294 "IPsec connection to %pSU already established",
297 vici_request_vc(nifp
->ipsec_profile
, &vc
->local
.nbma
,
298 &vc
->remote
.nbma
, p
->prio
);
300 master
, nhrp_peer_request_timeout
, p
,
301 (nifp
->ipsec_fallback_profile
&& !p
->prio
) ? 15 : 30,
306 int nhrp_peer_check(struct nhrp_peer
*p
, int establish
)
308 struct nhrp_vc
*vc
= p
->vc
;
309 struct interface
*ifp
= p
->ifp
;
310 struct nhrp_interface
*nifp
= ifp
->info
;
318 if (!nifp
->ipsec_profile
)
320 if (sockunion_family(&vc
->local
.nbma
) == AF_UNSPEC
)
325 p
->prio
= establish
> 1;
328 /* All NHRP registration requests are prioritized */
330 vici_request_vc(nifp
->ipsec_profile
, &vc
->local
.nbma
,
331 &vc
->remote
.nbma
, p
->prio
);
333 master
, nhrp_peer_request_timeout
, p
,
334 (nifp
->ipsec_fallback_profile
&& !p
->prio
) ? 15 : 30,
337 /* Maximum timeout is 1 second */
338 int r_time_ms
= frr_weak_random() % 1000;
340 debugf(NHRP_DEBUG_COMMON
,
341 "Initiating IPsec connection request to %pSU after %d ms:",
342 &vc
->remote
.nbma
, r_time_ms
);
343 thread_add_timer_msec(master
, nhrp_peer_defer_vici_request
,
344 p
, r_time_ms
, &p
->t_timer
);
350 void nhrp_peer_notify_add(struct nhrp_peer
*p
, struct notifier_block
*n
,
353 notifier_add(n
, &p
->notifier_list
, fn
);
356 void nhrp_peer_notify_del(struct nhrp_peer
*p
, struct notifier_block
*n
)
358 notifier_del(n
, &p
->notifier_list
);
359 nhrp_peer_check_delete(p
);
362 void nhrp_peer_send(struct nhrp_peer
*p
, struct zbuf
*zb
)
364 nhrp_packet_debug(zb
, "Send");
369 debugf(NHRP_DEBUG_KERNEL
, "PACKET: Send %pSU -> %pSU",
370 &p
->vc
->local
.nbma
, &p
->vc
->remote
.nbma
);
372 os_sendmsg(zb
->head
, zbuf_used(zb
), p
->ifp
->ifindex
,
373 sockunion_get_addr(&p
->vc
->remote
.nbma
),
374 sockunion_get_addrlen(&p
->vc
->remote
.nbma
), ETH_P_NHRP
);
378 static void nhrp_process_nat_extension(struct nhrp_packet_parser
*pp
,
379 union sockunion
*proto
,
380 union sockunion
*cie_nbma
)
382 union sockunion cie_proto
;
384 struct nhrp_extension_header
*ext
;
385 struct zbuf
*extensions
;
390 sockunion_family(cie_nbma
) = AF_UNSPEC
;
392 if (!proto
|| sockunion_family(proto
) == AF_UNSPEC
)
395 /* Handle extensions */
396 extensions
= zbuf_alloc(zbuf_used(&pp
->extensions
));
398 zbuf_copy_peek(extensions
, &pp
->extensions
,
399 zbuf_used(&pp
->extensions
));
400 while ((ext
= nhrp_ext_pull(extensions
, &payload
)) != NULL
) {
401 switch (htons(ext
->type
)
402 & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
403 case NHRP_EXTENSION_NAT_ADDRESS
:
404 /* Process the NBMA and proto address in NAT
405 * extension and update the cache without which
406 * the neighbor table in the kernel contains the
407 * source NBMA address which is not reachable
408 * since it is behind a NAT device
410 debugf(NHRP_DEBUG_COMMON
,
411 "shortcut res_resp: Processing NAT Extension for %pSU",
413 while (nhrp_cie_pull(&payload
, pp
->hdr
,
414 cie_nbma
, &cie_proto
)) {
415 if (sockunion_family(&cie_proto
)
419 if (!sockunion_cmp(proto
, &cie_proto
)) {
420 debugf(NHRP_DEBUG_COMMON
,
421 "cie_nbma for proto %pSU is %pSU",
428 zbuf_free(extensions
);
432 static void nhrp_handle_resolution_req(struct nhrp_packet_parser
*pp
)
434 struct interface
*ifp
= pp
->ifp
;
435 struct zbuf
*zb
, payload
;
436 struct nhrp_packet_header
*hdr
;
437 struct nhrp_cie_header
*cie
;
438 struct nhrp_extension_header
*ext
;
439 struct nhrp_cache
*c
;
440 union sockunion cie_nbma
, cie_nbma_nat
, cie_proto
, *proto_addr
,
441 *nbma_addr
, *claimed_nbma_addr
;
442 int holdtime
, prefix_len
, hostprefix_len
;
443 struct nhrp_interface
*nifp
= ifp
->info
;
444 struct nhrp_peer
*peer
;
447 if (!(pp
->if_ad
->flags
& NHRP_IFF_SHORTCUT
)) {
448 debugf(NHRP_DEBUG_COMMON
, "Shortcuts disabled");
449 /* FIXME: Send error indication? */
453 if (pp
->if_ad
->network_id
&& pp
->route_type
== NHRP_ROUTE_OFF_NBMA
454 && pp
->route_prefix
.prefixlen
< 8) {
455 debugf(NHRP_DEBUG_COMMON
,
456 "Shortcut to more generic than /8 dropped");
460 debugf(NHRP_DEBUG_COMMON
, "Parsing and replying to Resolution Req");
462 if (nhrp_route_address(ifp
, &pp
->src_proto
, NULL
, &peer
)
463 != NHRP_ROUTE_NBMA_NEXTHOP
)
466 /* Copy payload CIE */
467 hostprefix_len
= 8 * sockunion_get_addrlen(&pp
->if_ad
->addr
);
468 paylen
= zbuf_used(&pp
->payload
);
469 debugf(NHRP_DEBUG_COMMON
, "shortcut res_rep: paylen %zu", paylen
);
471 while ((cie
= nhrp_cie_pull(&pp
->payload
, pp
->hdr
, &cie_nbma
,
474 prefix_len
= cie
->prefix_length
;
475 debugf(NHRP_DEBUG_COMMON
,
476 "shortcut res_rep: parsing CIE with prefixlen=%u",
478 if (prefix_len
== 0 || prefix_len
>= hostprefix_len
)
479 prefix_len
= hostprefix_len
;
481 if (prefix_len
!= hostprefix_len
483 & htons(NHRP_FLAG_REGISTRATION_UNIQUE
))) {
484 cie
->code
= NHRP_CODE_BINDING_NON_UNIQUE
;
488 /* We currently support only unique prefix registrations */
489 if (prefix_len
!= hostprefix_len
) {
490 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
494 proto_addr
= (sockunion_family(&cie_proto
) == AF_UNSPEC
)
498 /* Check for this proto_addr in NHRP_NAT_EXTENSION */
499 nhrp_process_nat_extension(pp
, proto_addr
, &cie_nbma_nat
);
501 if (sockunion_family(&cie_nbma_nat
) == AF_UNSPEC
) {
502 /* It may be possible that this resolution reply is
503 * coming directly from NATTED Spoke and there is not
504 * NAT Extension present
506 debugf(NHRP_DEBUG_COMMON
,
507 "shortcut res_rep: No NAT Extension for %pSU",
510 if (!sockunion_same(&pp
->src_nbma
,
511 &pp
->peer
->vc
->remote
.nbma
)
512 && !nhrp_nhs_match_ip(&pp
->peer
->vc
->remote
.nbma
,
514 cie_nbma_nat
= pp
->peer
->vc
->remote
.nbma
;
515 debugf(NHRP_DEBUG_COMMON
,
516 "shortcut res_rep: NAT detected using %pSU as cie_nbma",
521 if (sockunion_family(&cie_nbma_nat
) != AF_UNSPEC
)
522 nbma_addr
= &cie_nbma_nat
;
523 else if (sockunion_family(&cie_nbma
) != AF_UNSPEC
)
524 nbma_addr
= &cie_nbma
;
526 nbma_addr
= &pp
->src_nbma
;
528 if (sockunion_family(&cie_nbma
) != AF_UNSPEC
)
529 claimed_nbma_addr
= &cie_nbma
;
531 claimed_nbma_addr
= &pp
->src_nbma
;
533 holdtime
= htons(cie
->holding_time
);
534 debugf(NHRP_DEBUG_COMMON
,
535 "shortcut res_rep: holdtime is %u (if 0, using %u)",
536 holdtime
, pp
->if_ad
->holdtime
);
538 holdtime
= pp
->if_ad
->holdtime
;
540 c
= nhrp_cache_get(ifp
, proto_addr
, 1);
542 debugf(NHRP_DEBUG_COMMON
,
543 "shortcut res_rep: no cache found");
544 cie
->code
= NHRP_CODE_INSUFFICIENT_RESOURCES
;
548 debugf(NHRP_DEBUG_COMMON
,
549 "shortcut res_rep: updating binding for nmba addr %pSU",
551 if (!nhrp_cache_update_binding(
552 c
, NHRP_CACHE_DYNAMIC
, holdtime
,
553 nhrp_peer_get(pp
->ifp
, nbma_addr
), htons(cie
->mtu
),
554 nbma_addr
, claimed_nbma_addr
)) {
555 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
559 cie
->code
= NHRP_CODE_SUCCESS
;
563 zb
= zbuf_alloc(1500);
564 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_RESOLUTION_REPLY
, &pp
->src_nbma
,
565 &pp
->src_proto
, &pp
->dst_proto
);
567 /* Copied information from request */
568 hdr
->flags
= pp
->hdr
->flags
569 & htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER
570 | NHRP_FLAG_RESOLUTION_SOURCE_STABLE
);
571 hdr
->flags
|= htons(NHRP_FLAG_RESOLUTION_DESTINATION_STABLE
572 | NHRP_FLAG_RESOLUTION_AUTHORATIVE
);
573 hdr
->u
.request_id
= pp
->hdr
->u
.request_id
;
575 /* CIE payload for the reply packet */
576 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
, &nifp
->nbma
,
578 cie
->holding_time
= htons(pp
->if_ad
->holdtime
);
579 cie
->mtu
= htons(pp
->if_ad
->mtu
);
580 if (pp
->if_ad
->network_id
&& pp
->route_type
== NHRP_ROUTE_OFF_NBMA
)
581 cie
->prefix_length
= pp
->route_prefix
.prefixlen
;
584 8 * sockunion_get_addrlen(&pp
->if_ad
->addr
);
586 /* Handle extensions */
587 while ((ext
= nhrp_ext_pull(&pp
->extensions
, &payload
)) != NULL
) {
588 switch (htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
589 case NHRP_EXTENSION_NAT_ADDRESS
:
590 ext
= nhrp_ext_push(zb
, hdr
,
591 NHRP_EXTENSION_NAT_ADDRESS
);
594 if (sockunion_family(&nifp
->nat_nbma
) != AF_UNSPEC
) {
595 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
601 8 * sockunion_get_addrlen(
604 cie
->mtu
= htons(pp
->if_ad
->mtu
);
605 nhrp_ext_complete(zb
, ext
);
609 if (nhrp_ext_reply(zb
, hdr
, ifp
, ext
, &payload
) < 0)
614 nhrp_packet_complete(zb
, hdr
);
615 nhrp_peer_send(peer
, zb
);
617 nhrp_peer_unref(peer
);
621 static void nhrp_handle_registration_request(struct nhrp_packet_parser
*p
)
623 struct interface
*ifp
= p
->ifp
;
624 struct zbuf
*zb
, payload
;
625 struct nhrp_packet_header
*hdr
;
626 struct nhrp_cie_header
*cie
;
627 struct nhrp_extension_header
*ext
;
628 struct nhrp_cache
*c
;
629 union sockunion cie_nbma
, cie_proto
, *proto_addr
, *nbma_addr
,
631 int holdtime
, prefix_len
, hostprefix_len
, natted
= 0;
635 debugf(NHRP_DEBUG_COMMON
, "Parsing and replying to Registration Req");
636 hostprefix_len
= 8 * sockunion_get_addrlen(&p
->if_ad
->addr
);
638 if (!sockunion_same(&p
->src_nbma
, &p
->peer
->vc
->remote
.nbma
))
642 zb
= zbuf_alloc(1500);
643 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_REGISTRATION_REPLY
, &p
->src_nbma
,
644 &p
->src_proto
, &p
->if_ad
->addr
);
646 /* Copied information from request */
647 hdr
->flags
= p
->hdr
->flags
& htons(NHRP_FLAG_REGISTRATION_UNIQUE
648 | NHRP_FLAG_REGISTRATION_NAT
);
649 hdr
->u
.request_id
= p
->hdr
->u
.request_id
;
651 /* Copy payload CIEs */
652 paylen
= zbuf_used(&p
->payload
);
653 pay
= zbuf_pushn(zb
, paylen
);
656 memcpy(pay
, zbuf_pulln(&p
->payload
, paylen
), paylen
);
657 zbuf_init(&payload
, pay
, paylen
, paylen
);
659 while ((cie
= nhrp_cie_pull(&payload
, hdr
, &cie_nbma
, &cie_proto
))
661 prefix_len
= cie
->prefix_length
;
662 if (prefix_len
== 0 || prefix_len
>= hostprefix_len
)
663 prefix_len
= hostprefix_len
;
665 if (prefix_len
!= hostprefix_len
667 & htons(NHRP_FLAG_REGISTRATION_UNIQUE
))) {
668 cie
->code
= NHRP_CODE_BINDING_NON_UNIQUE
;
672 /* We currently support only unique prefix registrations */
673 if (prefix_len
!= hostprefix_len
) {
674 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
678 proto_addr
= (sockunion_family(&cie_proto
) == AF_UNSPEC
)
681 nbma_addr
= (sockunion_family(&cie_nbma
) == AF_UNSPEC
)
687 (sockunion_family(&p
->peer
->vc
->remote
.nbma
)
690 : &p
->peer
->vc
->remote
.nbma
;
693 holdtime
= htons(cie
->holding_time
);
695 holdtime
= p
->if_ad
->holdtime
;
697 c
= nhrp_cache_get(ifp
, proto_addr
, 1);
699 cie
->code
= NHRP_CODE_INSUFFICIENT_RESOURCES
;
703 if (!nhrp_cache_update_binding(c
, NHRP_CACHE_DYNAMIC
, holdtime
,
704 nhrp_peer_ref(p
->peer
),
705 htons(cie
->mtu
), nbma_natoa
,
707 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
711 cie
->code
= NHRP_CODE_SUCCESS
;
714 /* Handle extensions */
715 while ((ext
= nhrp_ext_pull(&p
->extensions
, &payload
)) != NULL
) {
716 switch (htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
717 case NHRP_EXTENSION_NAT_ADDRESS
:
718 ext
= nhrp_ext_push(zb
, hdr
,
719 NHRP_EXTENSION_NAT_ADDRESS
);
722 zbuf_copy(zb
, &payload
, zbuf_used(&payload
));
724 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
725 &p
->peer
->vc
->remote
.nbma
,
728 8 * sockunion_get_addrlen(
730 cie
->mtu
= htons(p
->if_ad
->mtu
);
732 nhrp_ext_complete(zb
, ext
);
735 if (nhrp_ext_reply(zb
, hdr
, ifp
, ext
, &payload
) < 0)
741 nhrp_packet_complete(zb
, hdr
);
742 nhrp_peer_send(p
->peer
, zb
);
747 static int parse_ether_packet(struct zbuf
*zb
, uint16_t protocol_type
,
748 union sockunion
*src
, union sockunion
*dst
)
750 switch (protocol_type
) {
752 struct iphdr
*iph
= zbuf_pull(zb
, struct iphdr
);
755 sockunion_set(src
, AF_INET
,
756 (uint8_t *)&iph
->saddr
,
759 sockunion_set(dst
, AF_INET
,
760 (uint8_t *)&iph
->daddr
,
765 struct ipv6hdr
*iph
= zbuf_pull(zb
, struct ipv6hdr
);
768 sockunion_set(src
, AF_INET6
,
769 (uint8_t *)&iph
->saddr
,
772 sockunion_set(dst
, AF_INET6
,
773 (uint8_t *)&iph
->daddr
,
783 void nhrp_peer_send_indication(struct interface
*ifp
, uint16_t protocol_type
,
787 struct zbuf
*zb
, payload
;
788 struct nhrp_interface
*nifp
= ifp
->info
;
789 struct nhrp_afi_data
*if_ad
;
790 struct nhrp_packet_header
*hdr
;
797 if (!parse_ether_packet(&payload
, protocol_type
, &dst
, NULL
))
800 if (nhrp_route_address(ifp
, &dst
, NULL
, &p
) != NHRP_ROUTE_NBMA_NEXTHOP
)
803 if_ad
= &nifp
->afi
[family2afi(sockunion_family(&dst
))];
804 if (!(if_ad
->flags
& NHRP_IFF_REDIRECT
)) {
805 debugf(NHRP_DEBUG_COMMON
,
806 "Send Traffic Indication to %pSU about packet to %pSU ignored",
807 &p
->vc
->remote
.nbma
, &dst
);
811 debugf(NHRP_DEBUG_COMMON
,
812 "Send Traffic Indication to %pSU (online=%d) about packet to %pSU",
813 &p
->vc
->remote
.nbma
, p
->online
, &dst
);
816 zb
= zbuf_alloc(1500);
817 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_TRAFFIC_INDICATION
, &nifp
->nbma
,
821 /* Payload is the packet causing indication */
822 zbuf_copy(zb
, pkt
, zbuf_used(pkt
));
823 nhrp_packet_complete(zb
, hdr
);
824 nhrp_peer_send(p
, zb
);
829 static void nhrp_handle_error_ind(struct nhrp_packet_parser
*pp
)
831 struct zbuf origmsg
= pp
->payload
;
832 struct nhrp_packet_header
*hdr
;
833 struct nhrp_reqid
*reqid
;
834 union sockunion src_nbma
, src_proto
, dst_proto
;
836 hdr
= nhrp_packet_pull(&origmsg
, &src_nbma
, &src_proto
, &dst_proto
);
840 debugf(NHRP_DEBUG_COMMON
,
841 "Error Indication from %pSU about packet to %pSU ignored",
842 &pp
->src_proto
, &dst_proto
);
844 reqid
= nhrp_reqid_lookup(&nhrp_packet_reqid
, htonl(hdr
->u
.request_id
));
846 reqid
->cb(reqid
, pp
);
849 static void nhrp_handle_traffic_ind(struct nhrp_packet_parser
*p
)
853 if (!parse_ether_packet(&p
->payload
, htons(p
->hdr
->protocol_type
), NULL
,
857 debugf(NHRP_DEBUG_COMMON
,
858 "Traffic Indication from %pSU about packet to %pSU: %s",
860 (p
->if_ad
->flags
& NHRP_IFF_SHORTCUT
) ? "trying shortcut"
863 if (p
->if_ad
->flags
& NHRP_IFF_SHORTCUT
)
864 nhrp_shortcut_initiate(&dst
);
875 enum packet_type_t type
;
877 void (*handler
)(struct nhrp_packet_parser
*);
878 } packet_types
[] = {[0] =
880 .type
= PACKET_UNKNOWN
,
883 [NHRP_PACKET_RESOLUTION_REQUEST
] =
885 .type
= PACKET_REQUEST
,
886 .name
= "Resolution-Request",
887 .handler
= nhrp_handle_resolution_req
,
889 [NHRP_PACKET_RESOLUTION_REPLY
] =
891 .type
= PACKET_REPLY
,
892 .name
= "Resolution-Reply",
894 [NHRP_PACKET_REGISTRATION_REQUEST
] =
896 .type
= PACKET_REQUEST
,
897 .name
= "Registration-Request",
898 .handler
= nhrp_handle_registration_request
,
900 [NHRP_PACKET_REGISTRATION_REPLY
] =
902 .type
= PACKET_REPLY
,
903 .name
= "Registration-Reply",
905 [NHRP_PACKET_PURGE_REQUEST
] =
907 .type
= PACKET_REQUEST
,
908 .name
= "Purge-Request",
910 [NHRP_PACKET_PURGE_REPLY
] =
912 .type
= PACKET_REPLY
,
913 .name
= "Purge-Reply",
915 [NHRP_PACKET_ERROR_INDICATION
] =
917 .type
= PACKET_INDICATION
,
918 .name
= "Error-Indication",
919 .handler
= nhrp_handle_error_ind
,
921 [NHRP_PACKET_TRAFFIC_INDICATION
] = {
922 .type
= PACKET_INDICATION
,
923 .name
= "Traffic-Indication",
924 .handler
= nhrp_handle_traffic_ind
,
927 static void nhrp_peer_forward(struct nhrp_peer
*p
,
928 struct nhrp_packet_parser
*pp
)
930 struct zbuf
*zb
, *zb_copy
, extpl
;
931 struct nhrp_packet_header
*hdr
;
932 struct nhrp_extension_header
*ext
, *dst
;
933 struct nhrp_cie_header
*cie
;
934 struct nhrp_interface
*nifp
= pp
->ifp
->info
;
935 struct nhrp_afi_data
*if_ad
= pp
->if_ad
;
936 union sockunion cie_nbma
, cie_protocol
, cie_protocol_mandatory
, *proto
;
938 struct nhrp_cache
*c
;
940 if (pp
->hdr
->hop_count
== 0)
943 /* Create forward packet - copy header */
944 zb
= zbuf_alloc(1500);
945 zb_copy
= zbuf_alloc(1500);
947 hdr
= nhrp_packet_push(zb
, pp
->hdr
->type
, &pp
->src_nbma
, &pp
->src_proto
,
949 hdr
->flags
= pp
->hdr
->flags
;
950 hdr
->hop_count
= pp
->hdr
->hop_count
- 1;
951 hdr
->u
.request_id
= pp
->hdr
->u
.request_id
;
954 zbuf_copy_peek(zb_copy
, &pp
->payload
, zbuf_used(&pp
->payload
));
955 zbuf_copy(zb
, &pp
->payload
, zbuf_used(&pp
->payload
));
957 /* Get CIE Extension from Mandatory part */
958 sockunion_family(&cie_protocol_mandatory
) = AF_UNSPEC
;
959 nhrp_cie_pull(zb_copy
, pp
->hdr
, &cie_nbma
, &cie_protocol_mandatory
);
961 /* Copy extensions */
962 while ((ext
= nhrp_ext_pull(&pp
->extensions
, &extpl
)) != NULL
) {
963 type
= htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
;
964 len
= htons(ext
->length
);
966 if (type
== NHRP_EXTENSION_END
)
969 dst
= nhrp_ext_push(zb
, hdr
, htons(ext
->type
));
974 case NHRP_EXTENSION_FORWARD_TRANSIT_NHS
:
975 case NHRP_EXTENSION_REVERSE_TRANSIT_NHS
:
976 zbuf_put(zb
, extpl
.head
, len
);
977 if ((type
== NHRP_EXTENSION_REVERSE_TRANSIT_NHS
)
978 == (packet_types
[hdr
->type
].type
== PACKET_REPLY
)) {
979 /* Check NHS list for forwarding loop */
980 while (nhrp_cie_pull(&extpl
, pp
->hdr
,
982 &cie_protocol
) != NULL
) {
983 if (sockunion_same(&p
->vc
->remote
.nbma
,
987 /* Append our selves to the list */
988 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
989 &nifp
->nbma
, &if_ad
->addr
);
992 cie
->mtu
= htons(if_ad
->mtu
);
993 cie
->holding_time
= htons(if_ad
->holdtime
);
996 case NHRP_EXTENSION_NAT_ADDRESS
:
1000 /* If NAT extension is empty then attempt to populate
1001 * it with cached NBMA information
1004 if (packet_types
[hdr
->type
].type
1005 == PACKET_REQUEST
) {
1006 debugf(NHRP_DEBUG_COMMON
,
1007 "Processing NHRP_EXTENSION_NAT_ADDRESS while forwarding the request packet");
1008 proto
= &pp
->src_proto
;
1009 } else if (packet_types
[hdr
->type
].type
1011 debugf(NHRP_DEBUG_COMMON
,
1012 "Processing NHRP_EXTENSION_NAT_ADDRESS while forwarding the reply packet");
1013 /* For reply packet use protocol
1014 * specified in CIE of mandatory part
1017 if (sockunion_family(
1018 &cie_protocol_mandatory
)
1020 proto
= &cie_protocol_mandatory
;
1025 debugf(NHRP_DEBUG_COMMON
, "Proto is %pSU",
1027 c
= nhrp_cache_get(nifp
->ifp
, proto
, 0);
1031 debugf(NHRP_DEBUG_COMMON
,
1032 "c->cur.remote_nbma_natoa is %pSU",
1033 &c
->cur
.remote_nbma_natoa
);
1034 if (sockunion_family(&c
->cur
.remote_nbma_natoa
)
1036 cie
= nhrp_cie_push(
1039 &c
->cur
.remote_nbma_natoa
,
1046 debugf(NHRP_DEBUG_COMMON
,
1047 "No cache entry for proto %pSU",
1049 /* Copy existing NAT extension to new packet if
1050 * either it was already not-empty, or we do not
1051 * have valid cache information
1053 zbuf_put(zb
, extpl
.head
, len
);
1057 if (htons(ext
->type
) & NHRP_EXTENSION_FLAG_COMPULSORY
)
1058 /* FIXME: RFC says to just copy, but not
1059 * append our selves to the transit NHS list
1063 case NHRP_EXTENSION_RESPONDER_ADDRESS
:
1064 /* Supported compulsory extensions, and any
1065 * non-compulsory that is not explicitly handled,
1066 * should be just copied.
1068 zbuf_copy(zb
, &extpl
, len
);
1071 nhrp_ext_complete(zb
, dst
);
1074 nhrp_packet_complete(zb
, hdr
);
1075 nhrp_peer_send(p
, zb
);
1080 nhrp_packet_debug(pp
->pkt
, "FWD-FAIL");
1085 static void nhrp_packet_debug(struct zbuf
*zb
, const char *dir
)
1087 union sockunion src_nbma
, src_proto
, dst_proto
;
1088 struct nhrp_packet_header
*hdr
;
1092 if (likely(!(debug_flags
& NHRP_DEBUG_COMMON
)))
1095 zbuf_init(&zhdr
, zb
->buf
, zb
->tail
- zb
->buf
, zb
->tail
- zb
->buf
);
1096 hdr
= nhrp_packet_pull(&zhdr
, &src_nbma
, &src_proto
, &dst_proto
);
1098 reply
= packet_types
[hdr
->type
].type
== PACKET_REPLY
;
1099 debugf(NHRP_DEBUG_COMMON
, "%s %s(%d) %pSU -> %pSU", dir
,
1100 (packet_types
[hdr
->type
].name
? packet_types
[hdr
->type
].name
1102 hdr
->type
, reply
? &dst_proto
: &src_proto
,
1103 reply
? &src_proto
: &dst_proto
);
1106 static int proto2afi(uint16_t proto
)
1117 struct nhrp_route_info
{
1119 struct interface
*ifp
;
1123 void nhrp_peer_recv(struct nhrp_peer
*p
, struct zbuf
*zb
)
1125 struct nhrp_packet_header
*hdr
;
1126 struct nhrp_vc
*vc
= p
->vc
;
1127 struct interface
*ifp
= p
->ifp
;
1128 struct nhrp_interface
*nifp
= ifp
->info
;
1129 struct nhrp_packet_parser pp
;
1130 struct nhrp_peer
*peer
= NULL
;
1131 struct nhrp_reqid
*reqid
;
1132 const char *info
= NULL
;
1133 union sockunion
*target_addr
;
1134 unsigned paylen
, extoff
, extlen
, realsize
;
1135 afi_t nbma_afi
, proto_afi
;
1137 debugf(NHRP_DEBUG_KERNEL
, "PACKET: Recv %pSU -> %pSU", &vc
->remote
.nbma
,
1141 info
= "peer not online";
1145 if (nhrp_packet_calculate_checksum(zb
->head
, zbuf_used(zb
)) != 0) {
1146 info
= "bad checksum";
1150 realsize
= zbuf_used(zb
);
1151 hdr
= nhrp_packet_pull(zb
, &pp
.src_nbma
, &pp
.src_proto
, &pp
.dst_proto
);
1153 info
= "corrupt header";
1162 nbma_afi
= htons(hdr
->afnum
);
1163 proto_afi
= proto2afi(htons(hdr
->protocol_type
));
1164 if (hdr
->type
> NHRP_PACKET_MAX
|| hdr
->version
!= NHRP_VERSION_RFC2332
1165 || nbma_afi
>= AFI_MAX
|| proto_afi
== AF_UNSPEC
1166 || packet_types
[hdr
->type
].type
== PACKET_UNKNOWN
1167 || htons(hdr
->packet_size
) > realsize
) {
1169 "From %pSU: error: packet type %d, version %d, AFI %d, proto %x, size %d (real size %d)",
1170 &vc
->remote
.nbma
, (int)hdr
->type
, (int)hdr
->version
,
1171 (int)nbma_afi
, (int)htons(hdr
->protocol_type
),
1172 (int)htons(hdr
->packet_size
), (int)realsize
);
1175 pp
.if_ad
= &((struct nhrp_interface
*)ifp
->info
)->afi
[proto_afi
];
1177 extoff
= htons(hdr
->extension_offset
);
1179 assert(zb
->head
> zb
->buf
);
1180 uint32_t header_offset
= zb
->head
- zb
->buf
;
1181 if (extoff
>= realsize
) {
1182 info
= "extoff larger than packet";
1185 if (extoff
< header_offset
) {
1186 info
= "extoff smaller than header offset";
1189 paylen
= extoff
- header_offset
;
1191 paylen
= zbuf_used(zb
);
1193 zbuf_init(&pp
.payload
, zbuf_pulln(zb
, paylen
), paylen
, paylen
);
1194 extlen
= zbuf_used(zb
);
1195 zbuf_init(&pp
.extensions
, zbuf_pulln(zb
, extlen
), extlen
, extlen
);
1197 if (!nifp
->afi
[proto_afi
].network_id
) {
1198 info
= "nhrp not enabled";
1202 nhrp_packet_debug(zb
, "Recv");
1204 /* FIXME: Check authentication here. This extension needs to be
1207 /* Figure out if this is local */
1208 target_addr
= (packet_types
[hdr
->type
].type
== PACKET_REPLY
)
1212 if (sockunion_same(&pp
.src_proto
, &pp
.dst_proto
))
1213 pp
.route_type
= NHRP_ROUTE_LOCAL
;
1215 pp
.route_type
= nhrp_route_address(pp
.ifp
, target_addr
,
1216 &pp
.route_prefix
, &peer
);
1218 switch (pp
.route_type
) {
1219 case NHRP_ROUTE_LOCAL
:
1220 nhrp_packet_debug(zb
, "!LOCAL");
1221 if (packet_types
[hdr
->type
].type
== PACKET_REPLY
) {
1222 reqid
= nhrp_reqid_lookup(&nhrp_packet_reqid
,
1223 htonl(hdr
->u
.request_id
));
1225 reqid
->cb(reqid
, &pp
);
1228 nhrp_packet_debug(zb
, "!UNKNOWN-REQID");
1229 /* FIXME: send error-indication */
1232 /* fallthru */ /* FIXME: double check, is this correct? */
1233 case NHRP_ROUTE_OFF_NBMA
:
1234 if (packet_types
[hdr
->type
].handler
) {
1235 packet_types
[hdr
->type
].handler(&pp
);
1239 case NHRP_ROUTE_NBMA_NEXTHOP
:
1240 nhrp_peer_forward(peer
, &pp
);
1242 case NHRP_ROUTE_BLACKHOLE
:
1248 zlog_info("From %pSU: error: %s", &vc
->remote
.nbma
, info
);
1251 nhrp_peer_unref(peer
);