Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config] Fix config int add incorrect ip #1414

Merged
merged 16 commits into from
Jun 2, 2021

Conversation

d-dashkov
Copy link
Contributor

@d-dashkov d-dashkov commented Feb 9, 2021

- What I did
Added checking if the IP address is correct or return error.
fixes sonic-net/sonic-buildimage#6693
fixes sonic-net/sonic-buildimage#6776

- How I did it
Added function that check correctness of IP address and mask.

- How to verify it

config interface ip add Ethernet44 1000.000.000.001/24
Error: 'ip_addr' is not valid.

config interface ip add Ethernet40 100.0.0.10/35
Error: ip mask is not valid.

config interface ip add Vlan200 3000::001/200
Error: ip mask is not valid.

In some cases it will fix the IP and everything will work fine:

config interface ip add Ethernet44 100.000.000.001/24
show runningconfiguration all | grep Ethernet44
"Ethernet44|100.0.0.1/24": {}

config interface ip add Vlan200 3000::001/64
show runningconfiguration all | grep Vlan200
"Vlan200|3001::1/64": {}

config interface ip add Ethernet40 100.0.0.10/024
show runningconfiguration all | grep Ethernet40
"Ethernet40|100.0.0.10/24": {}

- New command output (if the output of a command-line utility has changed)
In case of wrong IP:

Error: 'ip_addr' is not valid.

In case of wrong mask:

Error: ip mask is not valid.

@d-dashkov d-dashkov changed the title [config]Fix config int add incorrect ip [config] Fix config int add incorrect ip Feb 9, 2021
Signed-off-by: d-dashkov <[email protected]>
config/main.py Outdated

try:
ip_addr = str(ipaddress.IPv4Address(split_ip_mask[0]))
except ipaddress.AddressValueError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look good to me (catching as exception for IPv6 address).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Suggest using ipaddress.ip_address() and checking whether the type of the result is IPv4Address or IPv6Address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

config/main.py Outdated
@@ -2656,6 +2675,8 @@ def add(ctx, interface_name, ip_addr, gw):
if '/' not in ip_addr:
ip_addr = str(net)

ip_addr = validate_ip_address(ctx, ip_addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you want to call this "validate". Since this is for IPv6, why dont we just call if address is v6 and update the leading zeros?

Copy link
Contributor Author

@d-dashkov d-dashkov Feb 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not only for IPv6, it checks the range of digits for v4 and v6 IP addresses and additionally removes leading zeros.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d-dashkov it seems to me the validation is only on the mast for ipv4 and ipv6. in this case we can do the following

  • rename the function to validate_ip_mask
  • add more validation points so it can be generic function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the code, and now all IP checking has been moved to my separate function and the try-except construct has been removed from the add and remove ip interface functions. So now it is a generic function that returns the IP string without leading zeros, or False if something is wrong.

@prsunny prsunny requested review from jleveque and arlakshm February 16, 2021 22:00
Signed-off-by: d-dashkov <[email protected]>
@lguohan
Copy link
Contributor

lguohan commented Feb 17, 2021

adding unit test for those fixes?

@d-dashkov
Copy link
Contributor Author

@lguohan, ok, I will provide unit tests for this fix

Signed-off-by: d-dashkov <[email protected]>
@d-dashkov d-dashkov requested a review from prsunny February 22, 2021 22:15
@liat-grozovik
Copy link
Collaborator

@prsunny and @jleveque any further comments? it will be nice to have these fixes in 202012 as well. Do you agree to add the label?

Signed-off-by: d-dashkov <[email protected]>
Signed-off-by: d-dashkov <[email protected]>
Signed-off-by: d-dashkov <[email protected]>
@d-dashkov d-dashkov requested a review from liat-grozovik March 22, 2021 19:00
Signed-off-by: d-dashkov <[email protected]>
@dprital
Copy link
Collaborator

dprital commented May 25, 2021

@d-dashkov are you about to fix the conflicts so this PR will be able to be approved ?

@d-dashkov
Copy link
Contributor Author

@d-dashkov are you about to fix the conflicts so this PR will be able to be approved ?

Yep, tomorrow I will rename function to validate_ip_mask as suggested Liat. Will that be enough or do you need something else from my side to resolve conflict

@prsunny prsunny merged commit 56db162 into sonic-net:master Jun 2, 2021
@dprital
Copy link
Collaborator

dprital commented Jun 3, 2021

Is this PR planned to be cherry picked to 202012 ?

gitsabari pushed a commit to gitsabari/sonic-utilities that referenced this pull request Jun 15, 2021
* Added ip and mask check to config int
Signed-off-by: d-dashkov <[email protected]>
@dprital
Copy link
Collaborator

dprital commented Jun 15, 2021

@prsunny - Can you please merge also to 202012 ?

qiluo-msft pushed a commit that referenced this pull request Jun 16, 2021
* Added ip and mask check to config int
Signed-off-by: d-dashkov <[email protected]>
raphaelt-nvidia pushed a commit to raphaelt-nvidia/sonic-utilities that referenced this pull request Aug 10, 2021
* Added ip and mask check to config int
Signed-off-by: d-dashkov <[email protected]>
stepanblyschak pushed a commit to stepanblyschak/sonic-utilities that referenced this pull request Apr 28, 2022
[portsorch] add buffer drop FC group 
[bitmap_vnet] Fix VNET route priority issue (sonic-net#1421)
[vnet] Maintain the reference count of the nexthop when creating a
vn… (sonic-net#1414)
[intfsorch] Retrieve Port object before setting NAT zone on router
interfaces. (sonic-net#1372)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants