@@ -3350,14 +3350,16 @@ def _read_opt_mnp(self, schema: 'Schema_MobileNetworkPrefixOption', *,
33503350 if schema.length != 18:
33513351 raise ProtocolError(f'{self.alias}: [Opt {schema.type}] invalid format')
33523352
3353- prefix = cast('IPv6Network',
3354- ipaddress.ip_network((schema.prefix, schema.prefix_length)))
3355-
3353+ # NOTE: The prefix and its length stay apart, as captured: an IPv6Network
3354+ # holds neither bits set past the length nor a length past 128. RFC 3963
3355+ # section 6.2 reads both fields as given, and answers a prefix the home
3356+ # agent cannot use with a status rather than discarding the option (#2702).
33563357 data = Data_MobileNetworkPrefixOption(
33573358 type=schema.type,
33583359 length=self._mh_option_length(schema.length),
33593360 reserved=schema.reserved,
3360- prefix=prefix,
3361+ prefix_length=schema.prefix_length,
3362+ prefix=schema.prefix,
33613363 )
33623364 return data
33633365
@@ -4020,9 +4022,10 @@ def _read_opt_hnp(self, schema: 'Schema_HomeNetworkPrefixOption', *,
40204022 """
40214023 if schema.length != 18:
40224024 raise ProtocolError(f'{self.alias}: [Opt {schema.type}] invalid format')
4023- if schema.prefix_length > 128:
4024- raise ProtocolError(f'{self.alias}: [Opt {schema.type}] invalid format')
40254025
4026+ # NOTE: A prefix length past 128 is kept, as for option 6: RFC 5213
4027+ # section 5.3.2 answers a prefix the local mobility anchor cannot
4028+ # authorise with a status rather than discarding the option (#2702).
40264029 data = Data_HomeNetworkPrefixOption(
40274030 type=schema.type,
40284031 length=self._mh_option_length(schema.length),
@@ -4237,12 +4240,23 @@ def _read_opt_timestamp(self, schema: 'Schema_TimestampOption', *,
42374240 seconds = schema.timestamp['seconds']
42384241 fraction = schema.timestamp['fraction']
42394242
4243+ try:
4244+ # NOTE: Not the stdlib's ``fromtimestamp``, which takes no time
4245+ # after 3000 on Windows (#2099). The whole seconds go in as an int
4246+ # and the fraction as a timedelta, which rounds it to microseconds
4247+ # half to even as one float did, but loses no precision to the
4248+ # seconds, so the last second of 9999 is not carried past it.
4249+ timestamp = (fromtimestamp(seconds, datetime.timezone.utc)
4250+ + datetime.timedelta(seconds=fraction / 65536)) # type: Optional[dt_type]
4251+ except ValueError:
4252+ # past the year 9999, which the 48-bit count reaches and datetime
4253+ # cannot hold, so pmip_timestamp alone keeps it (#2702)
4254+ timestamp = None
4255+
42404256 data = Data_TimestampOption(
42414257 type=schema.type,
42424258 length=self._mh_option_length(schema.length),
4243- # not the stdlib's ``fromtimestamp``, which takes no time after 3000
4244- # on Windows (#2099)
4245- timestamp=fromtimestamp(seconds + fraction / 65536, datetime.timezone.utc),
4259+ timestamp=timestamp,
42464260 pmip_timestamp=Data_PMIPv6Timestamp(seconds=seconds, fraction=fraction),
42474261 )
42484262 return data
@@ -8258,32 +8272,62 @@ def _make_opt_bad(self, type: 'Enum_Option', option: 'Optional[Data_Authorizatio
82588272 )
82598273
82608274 def _make_opt_mnp(self, type: 'Enum_Option', option: 'Optional[Data_MobileNetworkPrefixOption]' = None, *,
8261- prefix: 'bytes | str | IPv6Network' = '::/0',
8275+ prefix: 'bytes | str | int | IPv6Address | IPv6Network' = '::/0',
8276+ prefix_length: 'Optional[int]' = None,
82628277 reserved: 'bytes' = b'\x00',
82638278 **kwargs: 'Any') -> 'Schema_MobileNetworkPrefixOption':
82648279 """Make MH mobile network prefix option.
82658280
82668281 Args:
82678282 type: Option type.
82688283 option: Option data model.
8269- prefix: Mobile network prefix.
8284+ prefix: Mobile network prefix, an IPv6 address or network, whose
8285+ bits past ``prefix_length`` are written as given.
8286+ prefix_length: Prefix length; defaults to that of a network or of
8287+ text ending in ``/<length>``, and to 128 for an address.
82708288 reserved: The reserved octet as captured, truncated or zero-filled to 1
82718289 octet.
82728290 **kwargs: Arbitrary keyword arguments.
82738291
82748292 Returns:
82758293 Constructed option schema.
82768294
8295+ Raises:
8296+ ProtocolError: If ``prefix`` is not an IPv6 address or network, or
8297+ ``prefix_length`` is negative, a :obj:`bool`, or does not fit in
8298+ one octet.
8299+
8300+ Note:
8301+ The prefix length is checked against its 8-bit field alone, as
8302+ :rfc:`3963#section-6.2` has a home agent answer a prefix it cannot
8303+ use with a status rather than discard the option, so whatever the
8304+ reader keeps is written back.
8305+
8306+ .. versionchanged:: 1.6.0
8307+
8308+ New argument ``prefix_length``. ``prefix`` keeps the bits set past
8309+ its length, rather than raising :exc:`ValueError`, and a prefix
8310+ length past 128 is written (:issue:`2702`).
8311+
82778312 """
82788313 if option is not None:
82798314 reserved = getattr(option, 'reserved', b'\x00')
8280- prefix = option.prefix
8281-
8282- prefix_val = ipaddress.ip_network(prefix)
8283- if prefix_val.version != 6:
8284- raise ProtocolError(f'{self.alias}: [OptNo {type}] invalid mobile network prefix: {prefix!r}')
8285- prefix_length = prefix_val.prefixlen
8286- prefix_addr = prefix_val.network_address
8315+ prefix_length = option.prefix_length
8316+ prefix_addr = option.prefix
8317+ else:
8318+ try:
8319+ # NOTE: An interface, unlike a network, keeps the bits past its
8320+ # length, and takes an address, a network or text alike. A bool
8321+ # is refused first, as it would pass for the address ``::1``.
8322+ prefix_val = None if isinstance(prefix, bool) else ipaddress.IPv6Interface(prefix)
8323+ except ValueError:
8324+ prefix_val = None
8325+ if prefix_val is None:
8326+ raise ProtocolError(f'{self.alias}: [OptNo {type}] invalid mobile network prefix: {prefix!r}')
8327+ if prefix_length is None:
8328+ prefix_length = prefix_val.network.prefixlen
8329+ prefix_addr = prefix_val.ip
8330+ prefix_length = self._make_uint(prefix_length, bits=8, name='prefix length')
82878331
82888332 return Schema_MobileNetworkPrefixOption(
82898333 type=type,
@@ -9088,7 +9132,18 @@ def _make_opt_hnp(self, type: 'Enum_Option', option: 'Optional[Data_HomeNetworkP
90889132
90899133 Raises:
90909134 ProtocolError: If ``prefix_length`` is negative, a :obj:`bool`,
9091- or longer than the address.
9135+ or does not fit in one octet.
9136+
9137+ Note:
9138+ The prefix length is checked against its 8-bit field alone, as
9139+ :rfc:`5213#section-5.3.2` has a local mobility anchor answer a
9140+ prefix it cannot authorise with a status rather than discard the
9141+ option, so whatever the reader keeps is written back.
9142+
9143+ .. versionchanged:: 1.6.0
9144+
9145+ A prefix length longer than the address is written, rather than
9146+ refused, as the reader keeps one (:issue:`2702`).
90929147
90939148 """
90949149 if option is not None:
@@ -9097,8 +9152,6 @@ def _make_opt_hnp(self, type: 'Enum_Option', option: 'Optional[Data_HomeNetworkP
90979152 prefix = option.prefix
90989153
90999154 prefix_length = self._make_uint(prefix_length, bits=8, name='prefix length')
9100- if prefix_length > 128:
9101- raise ProtocolError(f'{self.alias}: [OptNo {type}] invalid prefix length: {prefix_length}')
91029155
91039156 return Schema_HomeNetworkPrefixOption(
91049157 type=type,
0 commit comments