[1/3] Add new function: device_get_by_assigned_ip_address()

Message ID 1519383935-3556-1-git-send-email-jonatan.schlag@ipfire.org
State Accepted
Commit 905b13b37cfd96011d12617cc3b55b11b2a76a55
Headers
Series [1/3] Add new function: device_get_by_assigned_ip_address() |

Commit Message

Jonatan Schlag via network Feb. 23, 2018, 10:05 p.m. UTC
  This function is used to get a device from an IP address
which is assigned to the device.
This function needs to be introduced
to set the routes for IPsec correctly.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/functions/functions.device | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
  

Comments

Jonatan Schlag via network Feb. 24, 2018, 10:50 p.m. UTC | #1
Hello,

what happens when the same IP address is assigned to multiple interfaces?

That should not be because it doesn't make much sense, but people configure
stupid things and we should make sure that that doesn't crash other parts of
networking.

-Michael

On Fri, 2018-02-23 at 11:05 +0000, Jonatan Schlag via network wrote:
> This function is used to get a device from an IP address
> which is assigned to the device.
> This function needs to be introduced
> to set the routes for IPsec correctly.
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
>  src/functions/functions.device | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/src/functions/functions.device b/src/functions/functions.device
> index cb4911f..2de1ad9 100644
> --- a/src/functions/functions.device
> +++ b/src/functions/functions.device
> @@ -1058,3 +1058,30 @@ device_queue_set_smp_affinity() {
>  
>  	__processor_id_to_bitmap ${processor} > ${path}
>  }
> +
> +# Tries to find a device which has the given IP address assigned
> +device_get_by_assigned_ip_address() {
> +	local ip=${1}
> +
> +	assert isset ip
> +
> +	local device
> +
> +	# Read the first line of ip addr show to
> +	read -r device <<< $(ip addr show to "${ip}")
> +
> +	# If we did not found a device we return with ${EXIT_ERROR}
> +	if ! isset device; then
> +		return ${EXIT_ERROR}
> +	fi
> +
> +	# We get something like:
> +	# 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state
> UP group default qlen 1000
> +	# and we want upl0 so we take the second word and removing the :
> +	device=(${device})
> +	device=${device[1]}
> +	device=${device%:}
> +
> +	print "${device}"
> +	return ${EXIT_OK}
> +}
  
Jonatan Schlag via network Feb. 28, 2018, 2:06 a.m. UTC | #2
Hi,

Am Sa, 24. Feb, 2018 um 12:50 schrieb Michael Tremer 
<michael.tremer@ipfire.org>:
> Hello,
> 
> what happens when the same IP address is assigned to multiple 
> interfaces?
> 
> That should not be because it doesn't make much sense, but people 
> configure
> stupid things and we should make sure that that doesn't crash other 
> parts of
> networking.
> 
> -Michael

We take only the first line of the $(ip addr show to ...), so we take 
the first device we get.
But I see no way to catch this stupid configuration. When the 
configuration is wrong, I see no problem when the code "fails"

Jonatan
> 
> 
> On Fri, 2018-02-23 at 11:05 +0000, Jonatan Schlag via network wrote:
>>  This function is used to get a device from an IP address
>>  which is assigned to the device.
>>  This function needs to be introduced
>>  to set the routes for IPsec correctly.
>> 
>>  Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
>>  ---
>>   src/functions/functions.device | 27 +++++++++++++++++++++++++++
>>   1 file changed, 27 insertions(+)
>> 
>>  diff --git a/src/functions/functions.device 
>> b/src/functions/functions.device
>>  index cb4911f..2de1ad9 100644
>>  --- a/src/functions/functions.device
>>  +++ b/src/functions/functions.device
>>  @@ -1058,3 +1058,30 @@ device_queue_set_smp_affinity() {
>> 
>>   	__processor_id_to_bitmap ${processor} > ${path}
>>   }
>>  +
>>  +# Tries to find a device which has the given IP address assigned
>>  +device_get_by_assigned_ip_address() {
>>  +	local ip=${1}
>>  +
>>  +	assert isset ip
>>  +
>>  +	local device
>>  +
>>  +	# Read the first line of ip addr show to
>>  +	read -r device <<< $(ip addr show to "${ip}")
>>  +
>>  +	# If we did not found a device we return with ${EXIT_ERROR}
>>  +	if ! isset device; then
>>  +		return ${EXIT_ERROR}
>>  +	fi
>>  +
>>  +	# We get something like:
>>  +	# 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq 
>> state
>>  UP group default qlen 1000
>>  +	# and we want upl0 so we take the second word and removing the :
>>  +	device=(${device})
>>  +	device=${device[1]}
>>  +	device=${device%:}
>>  +
>>  +	print "${device}"
>>  +	return ${EXIT_OK}
>>  +}
Hi,<br><br>Am Sa, 24. Feb, 2018 um 12:50  schrieb Michael Tremer &lt;michael.tremer@ipfire.org&gt;:<br>
<blockquote type="cite"><div class="plaintext" style="white-space: pre-wrap;">Hello,

what happens when the same IP address is assigned to multiple interfaces?

That should not be because it doesn't make much sense, but people configure
stupid things and we should make sure that that doesn't crash other parts of
networking.

-Michael</div></blockquote><div><br></div><div>We take only the first line of the $(ip addr show to ...), so we take the first device we get.</div><div>But I see no way to catch this stupid configuration. When the configuration is wrong, I see no problem when the code "fails"</div><div><br></div>Jonatan<br><blockquote type="cite"><div class="plaintext" style="white-space: pre-wrap;">

On Fri, 2018-02-23 at 11:05 +0000, Jonatan Schlag via network wrote:
<blockquote> This function is used to get a device from an IP address
 which is assigned to the device.
 This function needs to be introduced
 to set the routes for IPsec correctly.
 
 Signed-off-by: Jonatan Schlag &lt;<a href="mailto:jonatan.schlag@ipfire.org">jonatan.schlag@ipfire.org</a>&gt;
 ---
  src/functions/functions.device | 27 +++++++++++++++++++++++++++
  1 file changed, 27 insertions(+)
 
 diff --git a/src/functions/functions.device b/src/functions/functions.device
 index cb4911f..2de1ad9 100644
 --- a/src/functions/functions.device
 +++ b/src/functions/functions.device
 @@ -1058,3 +1058,30 @@ device_queue_set_smp_affinity() {
  
  	__processor_id_to_bitmap ${processor} &gt; ${path}
  }
 +
 +# Tries to find a device which has the given IP address assigned
 +device_get_by_assigned_ip_address() {
 +	local ip=${1}
 +
 +	assert isset ip
 +
 +	local device
 +
 +	# Read the first line of ip addr show to
 +	read -r device &lt;&lt;&lt; $(ip addr show to "${ip}")
 +
 +	# If we did not found a device we return with ${EXIT_ERROR}
 +	if ! isset device; then
 +		return ${EXIT_ERROR}
 +	fi
 +
 +	# We get something like:
 +	# 3: upl0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state
 UP group default qlen 1000
 +	# and we want upl0 so we take the second word and removing the :
 +	device=(${device})
 +	device=${device[1]}
 +	device=${device%:}
 +
 +	print "${device}"
 +	return ${EXIT_OK}
 +}
</blockquote></div></blockquote>
  

Patch

diff --git a/src/functions/functions.device b/src/functions/functions.device
index cb4911f..2de1ad9 100644
--- a/src/functions/functions.device
+++ b/src/functions/functions.device
@@ -1058,3 +1058,30 @@  device_queue_set_smp_affinity() {
 
 	__processor_id_to_bitmap ${processor} > ${path}
 }
+
+# Tries to find a device which has the given IP address assigned
+device_get_by_assigned_ip_address() {
+	local ip=${1}
+
+	assert isset ip
+
+	local device
+
+	# Read the first line of ip addr show to
+	read -r device <<< $(ip addr show to "${ip}")
+
+	# If we did not found a device we return with ${EXIT_ERROR}
+	if ! isset device; then
+		return ${EXIT_ERROR}
+	fi
+
+	# We get something like:
+	# 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
+	# and we want upl0 so we take the second word and removing the :
+	device=(${device})
+	device=${device[1]}
+	device=${device%:}
+
+	print "${device}"
+	return ${EXIT_OK}
+}