From 98a838e487af16004bbb582ef35d4eb52433d283 Mon Sep 17 00:00:00 2001 From: Satoshi Fujimoto Date: Mon, 21 Aug 2017 16:50:55 +0900 Subject: [PATCH] packet/bmp: Correctly parse peer address with IPv4 Currently, Ryu expects that the IPv4 peer address format is "4 bytes with IPv4 address, followed by 12 bytes padding". However RFC 7854 says the format is "12 bytes padding, followed by 4 bytes with IPv4 address". This patch fixes it to correctly parse/serialize peer address. Reported-by: Henk Smit Signed-off-by: Satoshi Fujimoto Signed-off-by: FUJITA Tomonori --- ryu/lib/packet/bmp.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ryu/lib/packet/bmp.py b/ryu/lib/packet/bmp.py index 9d1cda3b..bc49c577 100644 --- a/ryu/lib/packet/bmp.py +++ b/ryu/lib/packet/bmp.py @@ -208,7 +208,7 @@ class BMPPeerMessage(BMPMessage): if peer_flags & (1 << 7): peer_address = addrconv.ipv6.bin_to_text(peer_address) else: - peer_address = addrconv.ipv4.bin_to_text(peer_address[:4]) + peer_address = addrconv.ipv4.bin_to_text(peer_address[-4:]) peer_bgp_id = addrconv.ipv4.bin_to_text(peer_bgp_id) @@ -234,7 +234,8 @@ class BMPPeerMessage(BMPMessage): flags |= (1 << 7) peer_address = addrconv.ipv6.text_to_bin(self.peer_address) else: - peer_address = addrconv.ipv4.text_to_bin(self.peer_address) + peer_address = struct.pack( + '!12x4s', addrconv.ipv4.text_to_bin(self.peer_address)) peer_bgp_id = addrconv.ipv4.text_to_bin(self.peer_bgp_id) @@ -562,7 +563,7 @@ class BMPPeerUpNotification(BMPPeerMessage): remote_port) = struct.unpack_from(cls._PACK_STR, six.binary_type(rest)) if '.' in kwargs['peer_address']: - local_address = addrconv.ipv4.bin_to_text(local_address[:4]) + local_address = addrconv.ipv4.bin_to_text(local_address[-4:]) elif ':' in kwargs['peer_address']: local_address = addrconv.ipv6.bin_to_text(local_address) else: @@ -586,7 +587,8 @@ class BMPPeerUpNotification(BMPPeerMessage): msg = super(BMPPeerUpNotification, self).serialize_tail() if '.' in self.local_address: - local_address = addrconv.ipv4.text_to_bin(self.local_address) + local_address = struct.pack( + '!12x4s', addrconv.ipv4.text_to_bin(self.local_address)) elif ':' in self.local_address: local_address = addrconv.ipv6.text_to_bin(self.local_address) else: