Re: [PATCH] net: ipv6: Add support for default gateway discovery.

2023-04-01 Thread Ramon Fried
On Thu, Mar 2, 2023 at 6:58 PM  wrote:
>
> From: Ehsan Mohandesi 
>
> In IPv6, the default gateway and prefix length are determined by receiving
> a router advertisement as defined in -
> https://www.rfc-editor.org/rfc/rfc4861.
>
> Add support for sending router solicitation (RS) and processing router
> advertisements (RA).
>
> If the RA has prefix info option and following conditions are met, then
> gatewayip6 and net_prefix_length of ip6addr env variables are initialized.
> These are later consumed by IPv6 code for non-local destination IP.
>
> - "Router Lifetime" != 0
> - Prefix is NOT link-local prefix (0xfe80::/10)
> - L flag is 1
> - "Valid Lifetime" != 0
>
> Timing Parameters:
> - MAX_RTR_SOLICITATION_DELAY (0-1s)
> - RTR_SOLICITATION_INTERVAL (4s) (min retransmit delay)
> - MAX_RTR_SOLICITATIONS (3 RS transmissions)
>
> The functionality is enabled by CONFIG_IPV6_ROUTER_DISCOVERY and invoked
> automatically from net_init_loop().
>
> Signed-off-by: Ehsan Mohandesi 
>
> Conflicts:
> cmd/Kconfig
> include/net.h
> net/net.c
> ---
>  cmd/Kconfig |   7 ++
>  include/ndisc.h |  23 ++
>  include/net.h   |   2 +-
>  include/net6.h  |  40 ++
>  net/ndisc.c | 243 
> +---
>  net/net.c   |  23 +-
>  net/net6.c  |   1 +
>  7 files changed, 327 insertions(+), 12 deletions(-)
>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 2caa4af..c46613e 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1894,6 +1894,13 @@ config CMD_NCSI
>   Normally this happens automatically before other network
>   operations.
>
> +config IPV6_ROUTER_DISCOVERY
> +   bool "Do router discovery"
> +   depends on IPV6
> +   help
> + Will automatically perform router solicitation on first IPv6
> + network operation
> +
>  endif
>
>  config CMD_ETHSW
> diff --git a/include/ndisc.h b/include/ndisc.h
> index f6f8eb6..362d707 100644
> --- a/include/ndisc.h
> +++ b/include/ndisc.h
> @@ -19,6 +19,20 @@ struct nd_msg {
> __u8opt[0];
>  };
>
> +/* struct rs_msg - ICMPv6 Router Solicitation message format */
> +struct rs_msg {
> +   struct icmp6hdr icmph;
> +   __u8opt[0];
> +};
> +
> +/* struct ra_msg - ICMPv6 Router Advertisement message format */
> +struct ra_msg {
> +   struct icmp6hdr icmph;
> +   __u32   reachable_time;
> +   __u32   retransmission_timer;
> +   __u8opt[0];
> +};
> +
>  /* struct echo_msg - ICMPv6 echo request/reply message format */
>  struct echo_msg {
> struct icmp6hdr icmph;
> @@ -57,6 +71,11 @@ extern int net_nd_try;
>   */
>  void ndisc_init(void);
>
> +/*
> + * ip6_send_rs() - Send IPv6 Router Solicitation Message
> + */
> +void ip6_send_rs(void);
> +
>  /**
>   * ndisc_receive() - Handle ND packet
>   *
> @@ -97,6 +116,10 @@ static inline int ndisc_timeout_check(void)
>  {
> return 0;
>  }
> +
> +void ip6_send_rs(void)
> +{
> +}
>  #endif
>
>  #endif /* __NDISC_H__ */
> diff --git a/include/net.h b/include/net.h
> index 399af5e..25c43b3 100644
> --- a/include/net.h
> +++ b/include/net.h
> @@ -505,7 +505,7 @@ extern int  net_restart_wrap;   /* Tried all 
> network devices */
>
>  enum proto_t {
> BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS,
> -   SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET
> +   SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET, RS
>  };
>
>  extern charnet_boot_file_name[1024];/* Boot File name */
> diff --git a/include/net6.h b/include/net6.h
> index 2d7c5a0..beafc05 100644
> --- a/include/net6.h
> +++ b/include/net6.h
> @@ -81,8 +81,17 @@ struct udp_hdr {
>   0x00, 0x00, 0x00, 0x00, \
>   0x00, 0x00, 0x00, 0x00, \
>   0x00, 0x00, 0x00, 0x00 } } }
> +/*
> + * All-routers multicast address is the link-local scope address to reach all
> + * routers.
> + */
> +#define ALL_ROUTERS_MULT_ADDR { { { 0xFF, 0x02, 0x00, 0x00, \
> + 0x00, 0x00, 0x00, 0x00, \
> + 0x00, 0x00, 0x00, 0x00, \
> + 0x00, 0x00, 0x00, 0x02 } } }
>
>  #define IPV6_LINK_LOCAL_PREFIX 0xfe80
> +#define IPV6_LINK_LOCAL_MASK   0xffb0 /* The first 10-bit of address mask. */
>
>  /* hop limit for neighbour discovery packets */
>  #define IPV6_NDISC_HOPLIMIT 255
> @@ -166,6 +175,37 @@ struct icmp6hdr {
>  #define icmp6_rt_lifetime  icmp6_dataun.u_nd_ra.rt_lifetime
>  } __packed;
>
> +/*
> + * struct icmp6_ra_prefix_info - Prefix Information option of the ICMPv6 
> message
> + * The Prefix Information option provides hosts with on-link prefixes and
> + * prefixes for Address Autoconfiguration. Refer to RFC 4861 for more info.
> + */
> +struct icmp6_ra_prefix_info {
> +   u8  type;   /* Type is 3 for Prefix Information. */
> +   u8  

RE: [PATCH] net: ipv6: Add support for default gateway discovery.

2023-03-23 Thread Ehsan Mohandesi
Hi Viacheslav,

> -Original Message-
> From: Vyacheslav V. Mitrofanov 
> Sent: Thursday, March 16, 2023 3:47 AM
> To: u-boot@lists.denx.de; emohand...@linux.microsoft.com
> Cc: joe.hershber...@ni.com; xypron.g...@gmx.de;
> dpha...@linux.microsoft.com; sap...@gmail.com; rfried@gmail.com;
> ilias.apalodi...@linaro.org; Ehsan Mohandesi ;
> j...@metanate.com; s...@chromium.org; masahisa.koj...@linaro.org
> Subject: [EXTERNAL] Re: [PATCH] net: ipv6: Add support for default gateway
> discovery.
>
> On Thu, 2023-03-02 at 08:58 -0800, emohand...@linux.microsoft.com
> wrote:
> >
> > From: Ehsan Mohandesi 
> >
> > In IPv6, the default gateway and prefix length are determined by
> > receiving a router advertisement as defined in -
> >
> https://www.rf/
> c-
> editor.org%2Frfc%2Frfc4861=05%7C01%7Cemohandesi%40microsoft.co
> m%7C6dec635abc8c4861feb708db25fb05d6%7C72f988bf86f141af91ab2d7cd01
> 1db47%7C1%7C0%7C638145532341238481%7CUnknown%7CTWFpbGZsb3d8ey
> JWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C
> 3000%7C%7C%7C=tAhREBvBgVQKOFqEQT2%2FKphGxYXUMo3UF5vvQpY
> B%2Be0%3D=0.
> >
> > Add support for sending router solicitation (RS) and processing router
> > advertisements (RA).
> >
> > If the RA has prefix info option and following conditions are met,
> > then
> > gatewayip6 and net_prefix_length of ip6addr env variables are
> > initialized.
> > These are later consumed by IPv6 code for non-local destination IP.
> >
> > - "Router Lifetime" != 0
> > - Prefix is NOT link-local prefix (0xfe80::/10)
> > - L flag is 1
> > - "Valid Lifetime" != 0
> >
> > Timing Parameters:
> > - MAX_RTR_SOLICITATION_DELAY (0-1s)
> > - RTR_SOLICITATION_INTERVAL (4s) (min retransmit delay)
> > - MAX_RTR_SOLICITATIONS (3 RS transmissions)
> >
> > The functionality is enabled by CONFIG_IPV6_ROUTER_DISCOVERY and
> > invoked automatically from net_init_loop().
> >
> > Signed-off-by: Ehsan Mohandesi 
> >
> > Conflicts:
> > cmd/Kconfig
> > include/net.h
> > net/net.c
> > ---
> >  cmd/Kconfig |   7 ++
> >  include/ndisc.h |  23 ++
> >  include/net.h   |   2 +-
> >  include/net6.h  |  40 ++
> >  net/ndisc.c | 243
> > +---
> >  net/net.c   |  23 +-
> >  net/net6.c  |   1 +
> >  7 files changed, 327 insertions(+), 12 deletions(-)
> >
>
> I reviewed this patch and it looks good. I have no critical remarks, only some
> small notes.
>
> I've tested it on SiFive Unmatched board.
>
>
> >
> > +config IPV6_ROUTER_DISCOVERY
> > +   bool "Do router discovery"
> > +   depends on IPV6
> > +   help
> > + Will automatically perform router solicitation on first
> > IPv6
> > + network operation
> > +
> >  endif
> >
> I think it is better to write sth like Do IPv6 router discovery because
> IPv4 has also router discovery protocol and it could lead to misunderstanding
>
>
> >
> > net_set_timeout_handler(0, 0);
> >
> Maybe net_set_timeout_handler(0, NULL); is better
>
>
>
> > +/*
> > + * validate_ra() - Validate the router advertisement message.
> > + *
> > + * @ip6:
> > + * @len: Length of the router advertisement packet
> > + *
> > + * Check if the router advertisement message is valid. Conditions
> > are
> > + * according to RFC 4861 section 6.1.2. Validation of Router
> > Advertisement
> > + * Messages.
> > + *
> > + * Return: true if the message is valid and false if it is invalid.
> > + */
> > +static bool validate_ra(struct ip6_hdr *ip6, int len) {
> > +   struct icmp6hdr *icmp = (struct icmp6hdr *)(ip6 + 1);
> > +
> > +   /* ICMP length (derived from the IP length) should be 16 or
> > more octets. */
> > +   if (ip6->payload_len < 16)
> > +   return false;
> > +
> > +   /* Source IP Address should be a valid link-local address. */
> > +   if ((ntohs(ip6->saddr.s6_addr16[0]) & IPV6_LINK_LOCAL_MASK)
> > !=
> > +   IPV6_LINK_LOCAL_PREFIX)
> > +   return false;
> > +
> > +   /*
> > +* The IP Hop Limit field should have a value of 255, i.e.,
> > the packet
> > +* could not possibly have been forwarded by a router.
> > +*/
> > +   if (ip6->hop_limit != 255)
> > +   return false;
> > +
> Uni

Re: [PATCH] net: ipv6: Add support for default gateway discovery.

2023-03-23 Thread Vyacheslav V. Mitrofanov
On Thu, 2023-03-23 at 16:44 +, Ehsan Mohandesi wrote:
> 
> Hi Viacheslav,
> 
> > -Original Message-
> > From: Vyacheslav V. Mitrofanov 
> > Sent: Thursday, March 16, 2023 3:47 AM
> > To: u-boot@lists.denx.de; emohand...@linux.microsoft.com
> > Cc: joe.hershber...@ni.com; xypron.g...@gmx.de;
> > dpha...@linux.microsoft.com; sap...@gmail.com; rfried@gmail.com
> > ;
> > ilias.apalodi...@linaro.org; Ehsan Mohandesi <
> > emohand...@microsoft.com>;
> > j...@metanate.com; s...@chromium.org; masahisa.koj...@linaro.org
> > Subject: [EXTERNAL] Re: [PATCH] net: ipv6: Add support for default
> > gateway
> > discovery.
> > 
> > On Thu, 2023-03-02 at 08:58 -0800, emohand...@linux.microsoft.com
> > wrote:
> > > From: Ehsan Mohandesi 
> > > 
> > > In IPv6, the default gateway and prefix length are determined by
> > > receiving a router advertisement as defined in -
> > > 
> > https://www.rf/
> > c-
> > editor.org%2Frfc%2Frfc4861=05%7C01%7Cemohandesi%40microsoft.co
> > m%7C6dec635abc8c4861feb708db25fb05d6%7C72f988bf86f141af91ab2d7cd01
> > 1db47%7C1%7C0%7C638145532341238481%7CUnknown%7CTWFpbGZsb3d8ey
> > JWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C
> > 3000%7C%7C%7C=tAhREBvBgVQKOFqEQT2%2FKphGxYXUMo3UF5vvQpY
> > B%2Be0%3D=0.
> > > Add support for sending router solicitation (RS) and processing
> > > router
> > > advertisements (RA).
> > > 
> > > If the RA has prefix info option and following conditions are
> > > met,
> > > then
> > > gatewayip6 and net_prefix_length of ip6addr env variables are
> > > initialized.
> > > These are later consumed by IPv6 code for non-local destination
> > > IP.
> > > 
> > > - "Router Lifetime" != 0
> > > - Prefix is NOT link-local prefix (0xfe80::/10)
> > > - L flag is 1
> > > - "Valid Lifetime" != 0
> > > 
> > > Timing Parameters:
> > > - MAX_RTR_SOLICITATION_DELAY (0-1s)
> > > - RTR_SOLICITATION_INTERVAL (4s) (min retransmit delay)
> > > - MAX_RTR_SOLICITATIONS (3 RS transmissions)
> > > 
> > > The functionality is enabled by CONFIG_IPV6_ROUTER_DISCOVERY and
> > > invoked automatically from net_init_loop().
> > > 
> > > Signed-off-by: Ehsan Mohandesi 
> > > 
> > > Conflicts:
> > > cmd/Kconfig
> > > include/net.h
> > > net/net.c
> > > ---
> > >  cmd/Kconfig |   7 ++
> > >  include/ndisc.h |  23 ++
> > >  include/net.h   |   2 +-
> > >  include/net6.h  |  40 ++
> > >  net/ndisc.c | 243
> > > +---
> > >  net/net.c   |  23 +-
> > >  net/net6.c  |   1 +
> > >  7 files changed, 327 insertions(+), 12 deletions(-)
> > > 
> > 
> > I reviewed this patch and it looks good. I have no critical
> > remarks, only some
> > small notes.
> > 
> > I've tested it on SiFive Unmatched board.
> > 
> > 
> > > +config IPV6_ROUTER_DISCOVERY
> > > +   bool "Do router discovery"
> > > +   depends on IPV6
> > > +   help
> > > + Will automatically perform router solicitation on first
> > > IPv6
> > > + network operation
> > > +
> > >  endif
> > > 
> > I think it is better to write sth like Do IPv6 router discovery
> > because
> > IPv4 has also router discovery protocol and it could lead to
> > misunderstanding
> > 
> > 
> > > net_set_timeout_handler(0, 0);
> > > 
> > Maybe net_set_timeout_handler(0, NULL); is better
> > 
> > 
> > 
> > > +/*
> > > + * validate_ra() - Validate the router advertisement message.
> > > + *
> > > + * @ip6:
> > > + * @len: Length of the router advertisement packet
> > > + *
> > > + * Check if the router advertisement message is valid.
> > > Conditions
> > > are
> > > + * according to RFC 4861 section 6.1.2. Validation of Router
> > > Advertisement
> > > + * Messages.
> > > + *
> > > + * Return: true if the message is valid and false if it is
> > > invalid.
> > > + */
> > > +static bool validate_ra(struct ip6_hdr *ip6, int len) {
> > > +   struct icmp6hdr *icmp = (struct icmp6hdr *)(ip6 + 1);
> > > +
> > > +   /* ICMP length (

Re: [PATCH] net: ipv6: Add support for default gateway discovery.

2023-03-16 Thread Vyacheslav V. Mitrofanov
On Thu, 2023-03-02 at 08:58 -0800, emohand...@linux.microsoft.com
wrote:
> 
> From: Ehsan Mohandesi 
> 
> In IPv6, the default gateway and prefix length are determined by
> receiving
> a router advertisement as defined in -
> https://www.rfc-editor.org/rfc/rfc4861.
> 
> Add support for sending router solicitation (RS) and processing
> router
> advertisements (RA).
> 
> If the RA has prefix info option and following conditions are met,
> then
> gatewayip6 and net_prefix_length of ip6addr env variables are
> initialized.
> These are later consumed by IPv6 code for non-local destination IP.
> 
> - "Router Lifetime" != 0
> - Prefix is NOT link-local prefix (0xfe80::/10)
> - L flag is 1
> - "Valid Lifetime" != 0
> 
> Timing Parameters:
> - MAX_RTR_SOLICITATION_DELAY (0-1s)
> - RTR_SOLICITATION_INTERVAL (4s) (min retransmit delay)
> - MAX_RTR_SOLICITATIONS (3 RS transmissions)
> 
> The functionality is enabled by CONFIG_IPV6_ROUTER_DISCOVERY and
> invoked
> automatically from net_init_loop().
> 
> Signed-off-by: Ehsan Mohandesi 
> 
> Conflicts:
> cmd/Kconfig
> include/net.h
> net/net.c
> ---
>  cmd/Kconfig |   7 ++
>  include/ndisc.h |  23 ++
>  include/net.h   |   2 +-
>  include/net6.h  |  40 ++
>  net/ndisc.c | 243
> +---
>  net/net.c   |  23 +-
>  net/net6.c  |   1 +
>  7 files changed, 327 insertions(+), 12 deletions(-)
> 

I reviewed this patch and it looks good. I have no critical remarks,
only some small notes. 

I've tested it on SiFive Unmatched board.


> 
> +config IPV6_ROUTER_DISCOVERY
> +   bool "Do router discovery"
> +   depends on IPV6
> +   help
> + Will automatically perform router solicitation on first
> IPv6
> + network operation
> +
>  endif
> 
I think it is better to write sth like Do IPv6 router discovery because
IPv4 has also router discovery protocol and it could lead to
misunderstanding


> 
> net_set_timeout_handler(0, 0);
> 
Maybe net_set_timeout_handler(0, NULL); is better



> +/*
> + * validate_ra() - Validate the router advertisement message.
> + *
> + * @ip6:
> + * @len: Length of the router advertisement packet
> + *
> + * Check if the router advertisement message is valid. Conditions
> are
> + * according to RFC 4861 section 6.1.2. Validation of Router
> Advertisement
> + * Messages.
> + *
> + * Return: true if the message is valid and false if it is invalid.
> + */
> +static bool validate_ra(struct ip6_hdr *ip6, int len)
> +{
> +   struct icmp6hdr *icmp = (struct icmp6hdr *)(ip6 + 1);
> +
> +   /* ICMP length (derived from the IP length) should be 16 or
> more octets. */
> +   if (ip6->payload_len < 16)
> +   return false;
> +
> +   /* Source IP Address should be a valid link-local address. */
> +   if ((ntohs(ip6->saddr.s6_addr16[0]) & IPV6_LINK_LOCAL_MASK)
> !=
> +   IPV6_LINK_LOCAL_PREFIX)
> +   return false;
> +
> +   /*
> +* The IP Hop Limit field should have a value of 255, i.e.,
> the packet
> +* could not possibly have been forwarded by a router.
> +*/
> +   if (ip6->hop_limit != 255)
> +   return false;
> +
Unicast hop limit only?

> diff --git a/net/net.c b/net/net.c
> index c9a749f..39f0b81 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -24,7 +24,7 @@
>   * - name of bootfile
>   * Next step:  ARP
>   *
> - * LINK_LOCAL:
> + * LINKLOCAL:
> 
Maybe it is better to move to other patch?!


Reviewed-by: Viacheslav Mitrofanov 
Tested-by: Viacheslav Mitrofanov