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>
22 #include "nhrp_protocol.h"
25 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_PEER
, "NHRP peer entry");
28 uint8_t priority_version
;
33 struct in6_addr saddr
;
34 struct in6_addr daddr
;
37 static void nhrp_packet_debug(struct zbuf
*zb
, const char *dir
);
39 static void nhrp_peer_check_delete(struct nhrp_peer
*p
)
41 struct nhrp_interface
*nifp
= p
->ifp
->info
;
43 if (p
->ref
|| notifier_active(&p
->notifier_list
))
46 debugf(NHRP_DEBUG_COMMON
, "Deleting peer ref:%d remote:%pSU local:%pSU",
47 p
->ref
, &p
->vc
->remote
.nbma
, &p
->vc
->local
.nbma
);
49 THREAD_OFF(p
->t_fallback
);
50 THREAD_OFF(p
->t_timer
);
51 hash_release(nifp
->peer_hash
, p
);
52 nhrp_interface_notify_del(p
->ifp
, &p
->ifp_notifier
);
53 nhrp_vc_notify_del(p
->vc
, &p
->vc_notifier
);
54 XFREE(MTYPE_NHRP_PEER
, p
);
57 static void nhrp_peer_notify_up(struct thread
*t
)
59 struct nhrp_peer
*p
= THREAD_ARG(t
);
60 struct nhrp_vc
*vc
= p
->vc
;
61 struct interface
*ifp
= p
->ifp
;
62 struct nhrp_interface
*nifp
= ifp
->info
;
65 if (nifp
->enabled
&& (!nifp
->ipsec_profile
|| vc
->ipsec
)) {
68 notifier_call(&p
->notifier_list
, NOTIFY_PEER_UP
);
73 static void __nhrp_peer_check(struct nhrp_peer
*p
)
75 struct nhrp_vc
*vc
= p
->vc
;
76 struct interface
*ifp
= p
->ifp
;
77 struct nhrp_interface
*nifp
= ifp
->info
;
80 online
= nifp
->enabled
&& (!nifp
->ipsec_profile
|| vc
->ipsec
);
81 if (p
->online
!= online
) {
82 THREAD_OFF(p
->t_fallback
);
83 if (online
&& notifier_active(&p
->notifier_list
)) {
84 /* If we requested the IPsec connection, delay
85 * the up notification a bit to allow things
86 * settle down. This allows IKE to install
88 thread_add_timer_msec(master
, nhrp_peer_notify_up
, p
,
94 notifier_call(&p
->notifier_list
,
97 p
->requested
= p
->fallback_requested
= 0;
98 notifier_call(&p
->notifier_list
,
106 static void nhrp_peer_vc_notify(struct notifier_block
*n
, unsigned long cmd
)
108 struct nhrp_peer
*p
= container_of(n
, struct nhrp_peer
, vc_notifier
);
111 case NOTIFY_VC_IPSEC_CHANGED
:
112 __nhrp_peer_check(p
);
114 case NOTIFY_VC_IPSEC_UPDATE_NBMA
:
116 notifier_call(&p
->notifier_list
, NOTIFY_PEER_NBMA_CHANGING
);
122 static void nhrp_peer_ifp_notify(struct notifier_block
*n
, unsigned long cmd
)
124 struct nhrp_peer
*p
= container_of(n
, struct nhrp_peer
, ifp_notifier
);
125 struct nhrp_interface
*nifp
;
130 case NOTIFY_INTERFACE_UP
:
131 case NOTIFY_INTERFACE_DOWN
:
132 __nhrp_peer_check(p
);
134 case NOTIFY_INTERFACE_NBMA_CHANGED
:
135 /* Source NBMA changed, rebind to new VC */
137 vc
= nhrp_vc_get(&nifp
->nbma
, &p
->vc
->remote
.nbma
, 1);
138 if (vc
&& p
->vc
!= vc
) {
139 nhrp_vc_notify_del(p
->vc
, &p
->vc_notifier
);
141 nhrp_vc_notify_add(p
->vc
, &p
->vc_notifier
,
142 nhrp_peer_vc_notify
);
143 __nhrp_peer_check(p
);
145 /* fallthru */ /* to post config update */
146 case NOTIFY_INTERFACE_ADDRESS_CHANGED
:
147 notifier_call(&p
->notifier_list
, NOTIFY_PEER_IFCONFIG_CHANGED
);
149 case NOTIFY_INTERFACE_IPSEC_CHANGED
:
150 __nhrp_peer_check(p
);
151 notifier_call(&p
->notifier_list
, NOTIFY_PEER_IFCONFIG_CHANGED
);
153 case NOTIFY_INTERFACE_MTU_CHANGED
:
154 notifier_call(&p
->notifier_list
, NOTIFY_PEER_MTU_CHANGED
);
160 static unsigned int nhrp_peer_key(const void *peer_data
)
162 const struct nhrp_peer
*p
= peer_data
;
163 return sockunion_hash(&p
->vc
->remote
.nbma
);
166 static bool nhrp_peer_cmp(const void *cache_data
, const void *key_data
)
168 const struct nhrp_peer
*a
= cache_data
;
169 const struct nhrp_peer
*b
= key_data
;
171 return a
->ifp
== b
->ifp
&& a
->vc
== b
->vc
;
174 static void *nhrp_peer_create(void *data
)
176 struct nhrp_peer
*p
, *key
= data
;
178 p
= XMALLOC(MTYPE_NHRP_PEER
, sizeof(*p
));
180 *p
= (struct nhrp_peer
){
184 .notifier_list
= NOTIFIER_LIST_INITIALIZER(&p
->notifier_list
),
186 nhrp_vc_notify_add(p
->vc
, &p
->vc_notifier
, nhrp_peer_vc_notify
);
187 nhrp_interface_notify_add(p
->ifp
, &p
->ifp_notifier
,
188 nhrp_peer_ifp_notify
);
193 static void do_peer_hash_free(void *hb_data
)
195 struct nhrp_peer
*p
= (struct nhrp_peer
*)hb_data
;
197 nhrp_peer_check_delete(p
);
200 void nhrp_peer_interface_del(struct interface
*ifp
)
202 struct nhrp_interface
*nifp
= ifp
->info
;
204 debugf(NHRP_DEBUG_COMMON
, "Cleaning up undeleted peer entries (%lu)",
205 nifp
->peer_hash
? nifp
->peer_hash
->count
: 0);
207 if (nifp
->peer_hash
) {
208 hash_clean(nifp
->peer_hash
, do_peer_hash_free
);
209 assert(nifp
->peer_hash
->count
== 0);
210 hash_free(nifp
->peer_hash
);
211 nifp
->peer_hash
= NULL
;
215 struct nhrp_peer
*nhrp_peer_get(struct interface
*ifp
,
216 const union sockunion
*remote_nbma
)
218 struct nhrp_interface
*nifp
= ifp
->info
;
219 struct nhrp_peer key
, *p
;
222 if (!nifp
->peer_hash
) {
223 nifp
->peer_hash
= hash_create(nhrp_peer_key
, nhrp_peer_cmp
,
225 if (!nifp
->peer_hash
)
229 vc
= nhrp_vc_get(&nifp
->nbma
, remote_nbma
, 1);
236 p
= hash_get(nifp
->peer_hash
, &key
, nhrp_peer_create
);
239 __nhrp_peer_check(p
);
244 struct nhrp_peer
*nhrp_peer_ref(struct nhrp_peer
*p
)
251 void nhrp_peer_unref(struct nhrp_peer
*p
)
255 nhrp_peer_check_delete(p
);
259 static void nhrp_peer_request_timeout(struct thread
*t
)
261 struct nhrp_peer
*p
= THREAD_ARG(t
);
262 struct nhrp_vc
*vc
= p
->vc
;
263 struct interface
*ifp
= p
->ifp
;
264 struct nhrp_interface
*nifp
= ifp
->info
;
270 if (nifp
->ipsec_fallback_profile
&& !p
->prio
271 && !p
->fallback_requested
) {
272 p
->fallback_requested
= 1;
273 vici_request_vc(nifp
->ipsec_fallback_profile
, &vc
->local
.nbma
,
274 &vc
->remote
.nbma
, p
->prio
);
275 thread_add_timer(master
, nhrp_peer_request_timeout
, p
, 30,
278 p
->requested
= p
->fallback_requested
= 0;
282 static void nhrp_peer_defer_vici_request(struct thread
*t
)
284 struct nhrp_peer
*p
= THREAD_ARG(t
);
285 struct nhrp_vc
*vc
= p
->vc
;
286 struct interface
*ifp
= p
->ifp
;
287 struct nhrp_interface
*nifp
= ifp
->info
;
289 THREAD_OFF(p
->t_timer
);
292 debugf(NHRP_DEBUG_COMMON
,
293 "IPsec connection to %pSU already established",
296 vici_request_vc(nifp
->ipsec_profile
, &vc
->local
.nbma
,
297 &vc
->remote
.nbma
, p
->prio
);
299 master
, nhrp_peer_request_timeout
, p
,
300 (nifp
->ipsec_fallback_profile
&& !p
->prio
) ? 15 : 30,
305 int nhrp_peer_check(struct nhrp_peer
*p
, int establish
)
307 struct nhrp_vc
*vc
= p
->vc
;
308 struct interface
*ifp
= p
->ifp
;
309 struct nhrp_interface
*nifp
= ifp
->info
;
317 if (!nifp
->ipsec_profile
)
319 if (sockunion_family(&vc
->local
.nbma
) == AF_UNSPEC
)
324 p
->prio
= establish
> 1;
327 /* All NHRP registration requests are prioritized */
329 vici_request_vc(nifp
->ipsec_profile
, &vc
->local
.nbma
,
330 &vc
->remote
.nbma
, p
->prio
);
332 master
, nhrp_peer_request_timeout
, p
,
333 (nifp
->ipsec_fallback_profile
&& !p
->prio
) ? 15 : 30,
336 /* Maximum timeout is 1 second */
337 int r_time_ms
= rand() % 1000;
339 debugf(NHRP_DEBUG_COMMON
,
340 "Initiating IPsec connection request to %pSU after %d ms:",
341 &vc
->remote
.nbma
, r_time_ms
);
342 thread_add_timer_msec(master
, nhrp_peer_defer_vici_request
,
343 p
, r_time_ms
, &p
->t_timer
);
349 void nhrp_peer_notify_add(struct nhrp_peer
*p
, struct notifier_block
*n
,
352 notifier_add(n
, &p
->notifier_list
, fn
);
355 void nhrp_peer_notify_del(struct nhrp_peer
*p
, struct notifier_block
*n
)
357 notifier_del(n
, &p
->notifier_list
);
358 nhrp_peer_check_delete(p
);
361 void nhrp_peer_send(struct nhrp_peer
*p
, struct zbuf
*zb
)
363 nhrp_packet_debug(zb
, "Send");
368 debugf(NHRP_DEBUG_KERNEL
, "PACKET: Send %pSU -> %pSU",
369 &p
->vc
->local
.nbma
, &p
->vc
->remote
.nbma
);
371 os_sendmsg(zb
->head
, zbuf_used(zb
), p
->ifp
->ifindex
,
372 sockunion_get_addr(&p
->vc
->remote
.nbma
),
373 sockunion_get_addrlen(&p
->vc
->remote
.nbma
), ETH_P_NHRP
);
377 static void nhrp_process_nat_extension(struct nhrp_packet_parser
*pp
,
378 union sockunion
*proto
,
379 union sockunion
*cie_nbma
)
381 union sockunion cie_proto
;
383 struct nhrp_extension_header
*ext
;
384 struct zbuf
*extensions
;
389 sockunion_family(cie_nbma
) = AF_UNSPEC
;
391 if (!proto
|| sockunion_family(proto
) == AF_UNSPEC
)
394 /* Handle extensions */
395 extensions
= zbuf_alloc(zbuf_used(&pp
->extensions
));
397 zbuf_copy_peek(extensions
, &pp
->extensions
,
398 zbuf_used(&pp
->extensions
));
399 while ((ext
= nhrp_ext_pull(extensions
, &payload
)) != NULL
) {
400 switch (htons(ext
->type
)
401 & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
402 case NHRP_EXTENSION_NAT_ADDRESS
:
403 /* Process the NBMA and proto address in NAT
404 * extension and update the cache without which
405 * the neighbor table in the kernel contains the
406 * source NBMA address which is not reachable
407 * since it is behind a NAT device
409 debugf(NHRP_DEBUG_COMMON
,
410 "shortcut res_resp: Processing NAT Extension for %pSU",
412 while (nhrp_cie_pull(&payload
, pp
->hdr
,
413 cie_nbma
, &cie_proto
)) {
414 if (sockunion_family(&cie_proto
)
418 if (!sockunion_cmp(proto
, &cie_proto
)) {
419 debugf(NHRP_DEBUG_COMMON
,
420 "cie_nbma for proto %pSU is %pSU",
427 zbuf_free(extensions
);
431 static void nhrp_handle_resolution_req(struct nhrp_packet_parser
*pp
)
433 struct interface
*ifp
= pp
->ifp
;
434 struct zbuf
*zb
, payload
;
435 struct nhrp_packet_header
*hdr
;
436 struct nhrp_cie_header
*cie
;
437 struct nhrp_extension_header
*ext
;
438 struct nhrp_cache
*c
;
439 union sockunion cie_nbma
, cie_nbma_nat
, cie_proto
, *proto_addr
,
440 *nbma_addr
, *claimed_nbma_addr
;
441 int holdtime
, prefix_len
, hostprefix_len
;
442 struct nhrp_interface
*nifp
= ifp
->info
;
443 struct nhrp_peer
*peer
;
446 if (!(pp
->if_ad
->flags
& NHRP_IFF_SHORTCUT
)) {
447 debugf(NHRP_DEBUG_COMMON
, "Shortcuts disabled");
448 /* FIXME: Send error indication? */
452 if (pp
->if_ad
->network_id
&& pp
->route_type
== NHRP_ROUTE_OFF_NBMA
453 && pp
->route_prefix
.prefixlen
< 8) {
454 debugf(NHRP_DEBUG_COMMON
,
455 "Shortcut to more generic than /8 dropped");
459 debugf(NHRP_DEBUG_COMMON
, "Parsing and replying to Resolution Req");
461 if (nhrp_route_address(ifp
, &pp
->src_proto
, NULL
, &peer
)
462 != NHRP_ROUTE_NBMA_NEXTHOP
)
465 /* Copy payload CIE */
466 hostprefix_len
= 8 * sockunion_get_addrlen(&pp
->if_ad
->addr
);
467 paylen
= zbuf_used(&pp
->payload
);
468 debugf(NHRP_DEBUG_COMMON
, "shortcut res_rep: paylen %zu", paylen
);
470 while ((cie
= nhrp_cie_pull(&pp
->payload
, pp
->hdr
, &cie_nbma
,
473 prefix_len
= cie
->prefix_length
;
474 debugf(NHRP_DEBUG_COMMON
,
475 "shortcut res_rep: parsing CIE with prefixlen=%u",
477 if (prefix_len
== 0 || prefix_len
>= hostprefix_len
)
478 prefix_len
= hostprefix_len
;
480 if (prefix_len
!= hostprefix_len
482 & htons(NHRP_FLAG_REGISTRATION_UNIQUE
))) {
483 cie
->code
= NHRP_CODE_BINDING_NON_UNIQUE
;
487 /* We currently support only unique prefix registrations */
488 if (prefix_len
!= hostprefix_len
) {
489 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
493 proto_addr
= (sockunion_family(&cie_proto
) == AF_UNSPEC
)
497 /* Check for this proto_addr in NHRP_NAT_EXTENSION */
498 nhrp_process_nat_extension(pp
, proto_addr
, &cie_nbma_nat
);
500 if (sockunion_family(&cie_nbma_nat
) == AF_UNSPEC
) {
501 /* It may be possible that this resolution reply is
502 * coming directly from NATTED Spoke and there is not
503 * NAT Extension present
505 debugf(NHRP_DEBUG_COMMON
,
506 "shortcut res_rep: No NAT Extension for %pSU",
509 if (!sockunion_same(&pp
->src_nbma
,
510 &pp
->peer
->vc
->remote
.nbma
)
511 && !nhrp_nhs_match_ip(&pp
->peer
->vc
->remote
.nbma
,
513 cie_nbma_nat
= pp
->peer
->vc
->remote
.nbma
;
514 debugf(NHRP_DEBUG_COMMON
,
515 "shortcut res_rep: NAT detected using %pSU as cie_nbma",
520 if (sockunion_family(&cie_nbma_nat
) != AF_UNSPEC
)
521 nbma_addr
= &cie_nbma_nat
;
522 else if (sockunion_family(&cie_nbma
) != AF_UNSPEC
)
523 nbma_addr
= &cie_nbma
;
525 nbma_addr
= &pp
->src_nbma
;
527 if (sockunion_family(&cie_nbma
) != AF_UNSPEC
)
528 claimed_nbma_addr
= &cie_nbma
;
530 claimed_nbma_addr
= &pp
->src_nbma
;
532 holdtime
= htons(cie
->holding_time
);
533 debugf(NHRP_DEBUG_COMMON
,
534 "shortcut res_rep: holdtime is %u (if 0, using %u)",
535 holdtime
, pp
->if_ad
->holdtime
);
537 holdtime
= pp
->if_ad
->holdtime
;
539 c
= nhrp_cache_get(ifp
, proto_addr
, 1);
541 debugf(NHRP_DEBUG_COMMON
,
542 "shortcut res_rep: no cache found");
543 cie
->code
= NHRP_CODE_INSUFFICIENT_RESOURCES
;
547 debugf(NHRP_DEBUG_COMMON
,
548 "shortcut res_rep: updating binding for nmba addr %pSU",
550 if (!nhrp_cache_update_binding(
551 c
, NHRP_CACHE_DYNAMIC
, holdtime
,
552 nhrp_peer_get(pp
->ifp
, nbma_addr
), htons(cie
->mtu
),
553 nbma_addr
, claimed_nbma_addr
)) {
554 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
558 cie
->code
= NHRP_CODE_SUCCESS
;
562 zb
= zbuf_alloc(1500);
563 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_RESOLUTION_REPLY
, &pp
->src_nbma
,
564 &pp
->src_proto
, &pp
->dst_proto
);
566 /* Copied information from request */
567 hdr
->flags
= pp
->hdr
->flags
568 & htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER
569 | NHRP_FLAG_RESOLUTION_SOURCE_STABLE
);
570 hdr
->flags
|= htons(NHRP_FLAG_RESOLUTION_DESTINATION_STABLE
571 | NHRP_FLAG_RESOLUTION_AUTHORATIVE
);
572 hdr
->u
.request_id
= pp
->hdr
->u
.request_id
;
574 /* CIE payload for the reply packet */
575 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
, &nifp
->nbma
,
577 cie
->holding_time
= htons(pp
->if_ad
->holdtime
);
578 cie
->mtu
= htons(pp
->if_ad
->mtu
);
579 if (pp
->if_ad
->network_id
&& pp
->route_type
== NHRP_ROUTE_OFF_NBMA
)
580 cie
->prefix_length
= pp
->route_prefix
.prefixlen
;
583 8 * sockunion_get_addrlen(&pp
->if_ad
->addr
);
585 /* Handle extensions */
586 while ((ext
= nhrp_ext_pull(&pp
->extensions
, &payload
)) != NULL
) {
587 switch (htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
588 case NHRP_EXTENSION_NAT_ADDRESS
:
589 ext
= nhrp_ext_push(zb
, hdr
,
590 NHRP_EXTENSION_NAT_ADDRESS
);
593 if (sockunion_family(&nifp
->nat_nbma
) != AF_UNSPEC
) {
594 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
600 8 * sockunion_get_addrlen(
603 cie
->mtu
= htons(pp
->if_ad
->mtu
);
604 nhrp_ext_complete(zb
, ext
);
608 if (nhrp_ext_reply(zb
, hdr
, ifp
, ext
, &payload
) < 0)
613 nhrp_packet_complete(zb
, hdr
);
614 nhrp_peer_send(peer
, zb
);
616 nhrp_peer_unref(peer
);
620 static void nhrp_handle_registration_request(struct nhrp_packet_parser
*p
)
622 struct interface
*ifp
= p
->ifp
;
623 struct zbuf
*zb
, payload
;
624 struct nhrp_packet_header
*hdr
;
625 struct nhrp_cie_header
*cie
;
626 struct nhrp_extension_header
*ext
;
627 struct nhrp_cache
*c
;
628 union sockunion cie_nbma
, cie_proto
, *proto_addr
, *nbma_addr
,
630 int holdtime
, prefix_len
, hostprefix_len
, natted
= 0;
634 debugf(NHRP_DEBUG_COMMON
, "Parsing and replying to Registration Req");
635 hostprefix_len
= 8 * sockunion_get_addrlen(&p
->if_ad
->addr
);
637 if (!sockunion_same(&p
->src_nbma
, &p
->peer
->vc
->remote
.nbma
))
641 zb
= zbuf_alloc(1500);
642 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_REGISTRATION_REPLY
, &p
->src_nbma
,
643 &p
->src_proto
, &p
->if_ad
->addr
);
645 /* Copied information from request */
646 hdr
->flags
= p
->hdr
->flags
& htons(NHRP_FLAG_REGISTRATION_UNIQUE
647 | NHRP_FLAG_REGISTRATION_NAT
);
648 hdr
->u
.request_id
= p
->hdr
->u
.request_id
;
650 /* Copy payload CIEs */
651 paylen
= zbuf_used(&p
->payload
);
652 pay
= zbuf_pushn(zb
, paylen
);
655 memcpy(pay
, zbuf_pulln(&p
->payload
, paylen
), paylen
);
656 zbuf_init(&payload
, pay
, paylen
, paylen
);
658 while ((cie
= nhrp_cie_pull(&payload
, hdr
, &cie_nbma
, &cie_proto
))
660 prefix_len
= cie
->prefix_length
;
661 if (prefix_len
== 0 || prefix_len
>= hostprefix_len
)
662 prefix_len
= hostprefix_len
;
664 if (prefix_len
!= hostprefix_len
666 & htons(NHRP_FLAG_REGISTRATION_UNIQUE
))) {
667 cie
->code
= NHRP_CODE_BINDING_NON_UNIQUE
;
671 /* We currently support only unique prefix registrations */
672 if (prefix_len
!= hostprefix_len
) {
673 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
677 proto_addr
= (sockunion_family(&cie_proto
) == AF_UNSPEC
)
680 nbma_addr
= (sockunion_family(&cie_nbma
) == AF_UNSPEC
)
686 (sockunion_family(&p
->peer
->vc
->remote
.nbma
)
689 : &p
->peer
->vc
->remote
.nbma
;
692 holdtime
= htons(cie
->holding_time
);
694 holdtime
= p
->if_ad
->holdtime
;
696 c
= nhrp_cache_get(ifp
, proto_addr
, 1);
698 cie
->code
= NHRP_CODE_INSUFFICIENT_RESOURCES
;
702 if (!nhrp_cache_update_binding(c
, NHRP_CACHE_DYNAMIC
, holdtime
,
703 nhrp_peer_ref(p
->peer
),
704 htons(cie
->mtu
), nbma_natoa
,
706 cie
->code
= NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
;
710 cie
->code
= NHRP_CODE_SUCCESS
;
713 /* Handle extensions */
714 while ((ext
= nhrp_ext_pull(&p
->extensions
, &payload
)) != NULL
) {
715 switch (htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
) {
716 case NHRP_EXTENSION_NAT_ADDRESS
:
717 ext
= nhrp_ext_push(zb
, hdr
,
718 NHRP_EXTENSION_NAT_ADDRESS
);
721 zbuf_copy(zb
, &payload
, zbuf_used(&payload
));
723 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
724 &p
->peer
->vc
->remote
.nbma
,
727 8 * sockunion_get_addrlen(
729 cie
->mtu
= htons(p
->if_ad
->mtu
);
731 nhrp_ext_complete(zb
, ext
);
734 if (nhrp_ext_reply(zb
, hdr
, ifp
, ext
, &payload
) < 0)
740 nhrp_packet_complete(zb
, hdr
);
741 nhrp_peer_send(p
->peer
, zb
);
746 static int parse_ether_packet(struct zbuf
*zb
, uint16_t protocol_type
,
747 union sockunion
*src
, union sockunion
*dst
)
749 switch (protocol_type
) {
751 struct iphdr
*iph
= zbuf_pull(zb
, struct iphdr
);
754 sockunion_set(src
, AF_INET
,
755 (uint8_t *)&iph
->saddr
,
758 sockunion_set(dst
, AF_INET
,
759 (uint8_t *)&iph
->daddr
,
764 struct ipv6hdr
*iph
= zbuf_pull(zb
, struct ipv6hdr
);
767 sockunion_set(src
, AF_INET6
,
768 (uint8_t *)&iph
->saddr
,
771 sockunion_set(dst
, AF_INET6
,
772 (uint8_t *)&iph
->daddr
,
782 void nhrp_peer_send_indication(struct interface
*ifp
, uint16_t protocol_type
,
786 struct zbuf
*zb
, payload
;
787 struct nhrp_interface
*nifp
= ifp
->info
;
788 struct nhrp_afi_data
*if_ad
;
789 struct nhrp_packet_header
*hdr
;
796 if (!parse_ether_packet(&payload
, protocol_type
, &dst
, NULL
))
799 if (nhrp_route_address(ifp
, &dst
, NULL
, &p
) != NHRP_ROUTE_NBMA_NEXTHOP
)
802 if_ad
= &nifp
->afi
[family2afi(sockunion_family(&dst
))];
803 if (!(if_ad
->flags
& NHRP_IFF_REDIRECT
)) {
804 debugf(NHRP_DEBUG_COMMON
,
805 "Send Traffic Indication to %pSU about packet to %pSU ignored",
806 &p
->vc
->remote
.nbma
, &dst
);
810 debugf(NHRP_DEBUG_COMMON
,
811 "Send Traffic Indication to %pSU (online=%d) about packet to %pSU",
812 &p
->vc
->remote
.nbma
, p
->online
, &dst
);
815 zb
= zbuf_alloc(1500);
816 hdr
= nhrp_packet_push(zb
, NHRP_PACKET_TRAFFIC_INDICATION
, &nifp
->nbma
,
820 /* Payload is the packet causing indication */
821 zbuf_copy(zb
, pkt
, zbuf_used(pkt
));
822 nhrp_packet_complete(zb
, hdr
);
823 nhrp_peer_send(p
, zb
);
828 static void nhrp_handle_error_ind(struct nhrp_packet_parser
*pp
)
830 struct zbuf origmsg
= pp
->payload
;
831 struct nhrp_packet_header
*hdr
;
832 struct nhrp_reqid
*reqid
;
833 union sockunion src_nbma
, src_proto
, dst_proto
;
835 hdr
= nhrp_packet_pull(&origmsg
, &src_nbma
, &src_proto
, &dst_proto
);
839 debugf(NHRP_DEBUG_COMMON
,
840 "Error Indication from %pSU about packet to %pSU ignored",
841 &pp
->src_proto
, &dst_proto
);
843 reqid
= nhrp_reqid_lookup(&nhrp_packet_reqid
, htonl(hdr
->u
.request_id
));
845 reqid
->cb(reqid
, pp
);
848 static void nhrp_handle_traffic_ind(struct nhrp_packet_parser
*p
)
852 if (!parse_ether_packet(&p
->payload
, htons(p
->hdr
->protocol_type
), NULL
,
856 debugf(NHRP_DEBUG_COMMON
,
857 "Traffic Indication from %pSU about packet to %pSU: %s",
859 (p
->if_ad
->flags
& NHRP_IFF_SHORTCUT
) ? "trying shortcut"
862 if (p
->if_ad
->flags
& NHRP_IFF_SHORTCUT
)
863 nhrp_shortcut_initiate(&dst
);
874 enum packet_type_t type
;
876 void (*handler
)(struct nhrp_packet_parser
*);
877 } packet_types
[] = {[0] =
879 .type
= PACKET_UNKNOWN
,
882 [NHRP_PACKET_RESOLUTION_REQUEST
] =
884 .type
= PACKET_REQUEST
,
885 .name
= "Resolution-Request",
886 .handler
= nhrp_handle_resolution_req
,
888 [NHRP_PACKET_RESOLUTION_REPLY
] =
890 .type
= PACKET_REPLY
,
891 .name
= "Resolution-Reply",
893 [NHRP_PACKET_REGISTRATION_REQUEST
] =
895 .type
= PACKET_REQUEST
,
896 .name
= "Registration-Request",
897 .handler
= nhrp_handle_registration_request
,
899 [NHRP_PACKET_REGISTRATION_REPLY
] =
901 .type
= PACKET_REPLY
,
902 .name
= "Registration-Reply",
904 [NHRP_PACKET_PURGE_REQUEST
] =
906 .type
= PACKET_REQUEST
,
907 .name
= "Purge-Request",
909 [NHRP_PACKET_PURGE_REPLY
] =
911 .type
= PACKET_REPLY
,
912 .name
= "Purge-Reply",
914 [NHRP_PACKET_ERROR_INDICATION
] =
916 .type
= PACKET_INDICATION
,
917 .name
= "Error-Indication",
918 .handler
= nhrp_handle_error_ind
,
920 [NHRP_PACKET_TRAFFIC_INDICATION
] = {
921 .type
= PACKET_INDICATION
,
922 .name
= "Traffic-Indication",
923 .handler
= nhrp_handle_traffic_ind
,
926 static void nhrp_peer_forward(struct nhrp_peer
*p
,
927 struct nhrp_packet_parser
*pp
)
929 struct zbuf
*zb
, *zb_copy
, extpl
;
930 struct nhrp_packet_header
*hdr
;
931 struct nhrp_extension_header
*ext
, *dst
;
932 struct nhrp_cie_header
*cie
;
933 struct nhrp_interface
*nifp
= pp
->ifp
->info
;
934 struct nhrp_afi_data
*if_ad
= pp
->if_ad
;
935 union sockunion cie_nbma
, cie_protocol
, cie_protocol_mandatory
, *proto
;
937 struct nhrp_cache
*c
;
939 if (pp
->hdr
->hop_count
== 0)
942 /* Create forward packet - copy header */
943 zb
= zbuf_alloc(1500);
944 zb_copy
= zbuf_alloc(1500);
946 hdr
= nhrp_packet_push(zb
, pp
->hdr
->type
, &pp
->src_nbma
, &pp
->src_proto
,
948 hdr
->flags
= pp
->hdr
->flags
;
949 hdr
->hop_count
= pp
->hdr
->hop_count
- 1;
950 hdr
->u
.request_id
= pp
->hdr
->u
.request_id
;
953 zbuf_copy_peek(zb_copy
, &pp
->payload
, zbuf_used(&pp
->payload
));
954 zbuf_copy(zb
, &pp
->payload
, zbuf_used(&pp
->payload
));
956 /* Get CIE Extension from Mandatory part */
957 sockunion_family(&cie_protocol_mandatory
) = AF_UNSPEC
;
958 nhrp_cie_pull(zb_copy
, pp
->hdr
, &cie_nbma
, &cie_protocol_mandatory
);
960 /* Copy extensions */
961 while ((ext
= nhrp_ext_pull(&pp
->extensions
, &extpl
)) != NULL
) {
962 type
= htons(ext
->type
) & ~NHRP_EXTENSION_FLAG_COMPULSORY
;
963 len
= htons(ext
->length
);
965 if (type
== NHRP_EXTENSION_END
)
968 dst
= nhrp_ext_push(zb
, hdr
, htons(ext
->type
));
973 case NHRP_EXTENSION_FORWARD_TRANSIT_NHS
:
974 case NHRP_EXTENSION_REVERSE_TRANSIT_NHS
:
975 zbuf_put(zb
, extpl
.head
, len
);
976 if ((type
== NHRP_EXTENSION_REVERSE_TRANSIT_NHS
)
977 == (packet_types
[hdr
->type
].type
== PACKET_REPLY
)) {
978 /* Check NHS list for forwarding loop */
979 while (nhrp_cie_pull(&extpl
, pp
->hdr
,
981 &cie_protocol
) != NULL
) {
982 if (sockunion_same(&p
->vc
->remote
.nbma
,
986 /* Append our selves to the list */
987 cie
= nhrp_cie_push(zb
, NHRP_CODE_SUCCESS
,
988 &nifp
->nbma
, &if_ad
->addr
);
991 cie
->mtu
= htons(if_ad
->mtu
);
992 cie
->holding_time
= htons(if_ad
->holdtime
);
995 case NHRP_EXTENSION_NAT_ADDRESS
:
999 /* If NAT extension is empty then attempt to populate
1000 * it with cached NBMA information
1003 if (packet_types
[hdr
->type
].type
1004 == PACKET_REQUEST
) {
1005 debugf(NHRP_DEBUG_COMMON
,
1006 "Processing NHRP_EXTENSION_NAT_ADDRESS while forwarding the request packet");
1007 proto
= &pp
->src_proto
;
1008 } else if (packet_types
[hdr
->type
].type
1010 debugf(NHRP_DEBUG_COMMON
,
1011 "Processing NHRP_EXTENSION_NAT_ADDRESS while forwarding the reply packet");
1012 /* For reply packet use protocol
1013 * specified in CIE of mandatory part
1016 if (sockunion_family(
1017 &cie_protocol_mandatory
)
1019 proto
= &cie_protocol_mandatory
;
1024 debugf(NHRP_DEBUG_COMMON
, "Proto is %pSU",
1026 c
= nhrp_cache_get(nifp
->ifp
, proto
, 0);
1030 debugf(NHRP_DEBUG_COMMON
,
1031 "c->cur.remote_nbma_natoa is %pSU",
1032 &c
->cur
.remote_nbma_natoa
);
1033 if (sockunion_family(&c
->cur
.remote_nbma_natoa
)
1035 cie
= nhrp_cie_push(
1038 &c
->cur
.remote_nbma_natoa
,
1045 debugf(NHRP_DEBUG_COMMON
,
1046 "No cache entry for proto %pSU",
1048 /* Copy existing NAT extension to new packet if
1049 * either it was already not-empty, or we do not
1050 * have valid cache information
1052 zbuf_put(zb
, extpl
.head
, len
);
1056 if (htons(ext
->type
) & NHRP_EXTENSION_FLAG_COMPULSORY
)
1057 /* FIXME: RFC says to just copy, but not
1058 * append our selves to the transit NHS list
1062 case NHRP_EXTENSION_RESPONDER_ADDRESS
:
1063 /* Supported compulsory extensions, and any
1064 * non-compulsory that is not explicitly handled,
1065 * should be just copied.
1067 zbuf_copy(zb
, &extpl
, len
);
1070 nhrp_ext_complete(zb
, dst
);
1073 nhrp_packet_complete(zb
, hdr
);
1074 nhrp_peer_send(p
, zb
);
1079 nhrp_packet_debug(pp
->pkt
, "FWD-FAIL");
1084 static void nhrp_packet_debug(struct zbuf
*zb
, const char *dir
)
1086 union sockunion src_nbma
, src_proto
, dst_proto
;
1087 struct nhrp_packet_header
*hdr
;
1091 if (likely(!(debug_flags
& NHRP_DEBUG_COMMON
)))
1094 zbuf_init(&zhdr
, zb
->buf
, zb
->tail
- zb
->buf
, zb
->tail
- zb
->buf
);
1095 hdr
= nhrp_packet_pull(&zhdr
, &src_nbma
, &src_proto
, &dst_proto
);
1097 reply
= packet_types
[hdr
->type
].type
== PACKET_REPLY
;
1098 debugf(NHRP_DEBUG_COMMON
, "%s %s(%d) %pSU -> %pSU", dir
,
1099 (packet_types
[hdr
->type
].name
? packet_types
[hdr
->type
].name
1101 hdr
->type
, reply
? &dst_proto
: &src_proto
,
1102 reply
? &src_proto
: &dst_proto
);
1105 static int proto2afi(uint16_t proto
)
1116 struct nhrp_route_info
{
1118 struct interface
*ifp
;
1122 void nhrp_peer_recv(struct nhrp_peer
*p
, struct zbuf
*zb
)
1124 struct nhrp_packet_header
*hdr
;
1125 struct nhrp_vc
*vc
= p
->vc
;
1126 struct interface
*ifp
= p
->ifp
;
1127 struct nhrp_interface
*nifp
= ifp
->info
;
1128 struct nhrp_packet_parser pp
;
1129 struct nhrp_peer
*peer
= NULL
;
1130 struct nhrp_reqid
*reqid
;
1131 const char *info
= NULL
;
1132 union sockunion
*target_addr
;
1133 unsigned paylen
, extoff
, extlen
, realsize
;
1134 afi_t nbma_afi
, proto_afi
;
1136 debugf(NHRP_DEBUG_KERNEL
, "PACKET: Recv %pSU -> %pSU", &vc
->remote
.nbma
,
1140 info
= "peer not online";
1144 if (nhrp_packet_calculate_checksum(zb
->head
, zbuf_used(zb
)) != 0) {
1145 info
= "bad checksum";
1149 realsize
= zbuf_used(zb
);
1150 hdr
= nhrp_packet_pull(zb
, &pp
.src_nbma
, &pp
.src_proto
, &pp
.dst_proto
);
1152 info
= "corrupt header";
1161 nbma_afi
= htons(hdr
->afnum
);
1162 proto_afi
= proto2afi(htons(hdr
->protocol_type
));
1163 if (hdr
->type
> NHRP_PACKET_MAX
|| hdr
->version
!= NHRP_VERSION_RFC2332
1164 || nbma_afi
>= AFI_MAX
|| proto_afi
== AF_UNSPEC
1165 || packet_types
[hdr
->type
].type
== PACKET_UNKNOWN
1166 || htons(hdr
->packet_size
) > realsize
) {
1168 "From %pSU: error: packet type %d, version %d, AFI %d, proto %x, size %d (real size %d)",
1169 &vc
->remote
.nbma
, (int)hdr
->type
, (int)hdr
->version
,
1170 (int)nbma_afi
, (int)htons(hdr
->protocol_type
),
1171 (int)htons(hdr
->packet_size
), (int)realsize
);
1174 pp
.if_ad
= &((struct nhrp_interface
*)ifp
->info
)->afi
[proto_afi
];
1176 extoff
= htons(hdr
->extension_offset
);
1178 assert(zb
->head
> zb
->buf
);
1179 uint32_t header_offset
= zb
->head
- zb
->buf
;
1180 if (extoff
>= realsize
) {
1181 info
= "extoff larger than packet";
1184 if (extoff
< header_offset
) {
1185 info
= "extoff smaller than header offset";
1188 paylen
= extoff
- header_offset
;
1190 paylen
= zbuf_used(zb
);
1192 zbuf_init(&pp
.payload
, zbuf_pulln(zb
, paylen
), paylen
, paylen
);
1193 extlen
= zbuf_used(zb
);
1194 zbuf_init(&pp
.extensions
, zbuf_pulln(zb
, extlen
), extlen
, extlen
);
1196 if (!nifp
->afi
[proto_afi
].network_id
) {
1197 info
= "nhrp not enabled";
1201 nhrp_packet_debug(zb
, "Recv");
1203 /* FIXME: Check authentication here. This extension needs to be
1206 /* Figure out if this is local */
1207 target_addr
= (packet_types
[hdr
->type
].type
== PACKET_REPLY
)
1211 if (sockunion_same(&pp
.src_proto
, &pp
.dst_proto
))
1212 pp
.route_type
= NHRP_ROUTE_LOCAL
;
1214 pp
.route_type
= nhrp_route_address(pp
.ifp
, target_addr
,
1215 &pp
.route_prefix
, &peer
);
1217 switch (pp
.route_type
) {
1218 case NHRP_ROUTE_LOCAL
:
1219 nhrp_packet_debug(zb
, "!LOCAL");
1220 if (packet_types
[hdr
->type
].type
== PACKET_REPLY
) {
1221 reqid
= nhrp_reqid_lookup(&nhrp_packet_reqid
,
1222 htonl(hdr
->u
.request_id
));
1224 reqid
->cb(reqid
, &pp
);
1227 nhrp_packet_debug(zb
, "!UNKNOWN-REQID");
1228 /* FIXME: send error-indication */
1231 /* fallthru */ /* FIXME: double check, is this correct? */
1232 case NHRP_ROUTE_OFF_NBMA
:
1233 if (packet_types
[hdr
->type
].handler
) {
1234 packet_types
[hdr
->type
].handler(&pp
);
1238 case NHRP_ROUTE_NBMA_NEXTHOP
:
1239 nhrp_peer_forward(peer
, &pp
);
1241 case NHRP_ROUTE_BLACKHOLE
:
1247 zlog_info("From %pSU: error: %s", &vc
->remote
.nbma
, info
);
1250 nhrp_peer_unref(peer
);