Skip to contentSkip to conversation

ip: Support IP address protocol

PATCH New Petr Machata 1 participant 2 messages Mar 27, 2023 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 ip: Support IP address protocol

IPv4 and IPv6 addresses can be assigned a protocol value that indicates the provenance of the IP address. The attribute is modeled after ip route protocols, and essentially allows the administrator or userspace stack to tag addresses in some way that makes sense to the actor in question. Support for this feature was merged with commit 47f0bd503210 ("net: Add new protocol attribute to IP addresses"), for kernel 5.18.

In this patch, add support for setting the protocol attribute at IP address addition, replacement, and listing requests.

An example session with the feature in action:

	# ip address add dev d 192.0.2.1/28 proto 0xab
	# ip address show dev d
	26: d: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
	    link/ether 06:29:74:fd:1f:eb brd ff:ff:ff:ff:ff:ff
	    inet 192.0.2.1/28 scope global proto 0xab d
	       valid_lft forever preferred_lft forever
	# ip address replace dev d 192.0.2.1/28 proto 0x11
	# ip address show dev d
	26: d: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
	    link/ether 06:29:74:fd:1f:eb brd ff:ff:ff:ff:ff:ff
	    inet 192.0.2.1/28 scope global proto 0x11 d
	       valid_lft forever preferred_lft forever

A JSON dump. The protocol value is always provided as a string, even in numeric mode, to provide a consistent interface.

	# ip -j address show dev d | jq
	[
	  {
	    "ifindex": 26,
	    "ifname": "d",
	    "flags": [
	      "BROADCAST",
	      "NOARP"
	    ],
	    "mtu": 1500,
	    "qdisc": "noop",
	    "operstate": "DOWN",
	    "group": "default",
	    "txqlen": 1000,
	    "link_type": "ether",
	    "address": "06:29:74:fd:1f:eb",
	    "broadcast": "ff:ff:ff:ff:ff:ff",
	    "addr_info": [
	      {
	        "family": "inet",
	        "local": "192.0.2.1",
	        "prefixlen": 28,
	        "scope": "global",
	        "protocol": "0x11",
	        "label": "d",
	        "valid_life_time": 4294967295,
	        "preferred_life_time": 4294967295
	      }
	    ]
	  }
	]
---
 include/rt_names.h |  2 ++
 ip/ip_common.h     |  2 ++
 ip/ipaddress.c     | 34 +++++++++++++++++++++++--
 lib/rt_names.c     | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+), 2 deletions(-)
include/rt_names.h +2 -0
index 6358650db404..e96d80f30554 100644
@@ -5,6 +5,7 @@
 #include <asm/types.h>
 
 const char *rtnl_rtprot_n2a(int id, char *buf, int len);
+const char *rtnl_addrprot_n2a(int id, char *buf, int len);
 const char *rtnl_rtscope_n2a(int id, char *buf, int len);
 const char *rtnl_rttable_n2a(__u32 id, char *buf, int len);
 const char *rtnl_rtrealm_n2a(int id, char *buf, int len);
@@ -13,6 +14,7 @@ const char *rtnl_dsfield_get_name(int id);
 const char *rtnl_group_n2a(int id, char *buf, int len);
 
 int rtnl_rtprot_a2n(__u32 *id, const char *arg);
+int rtnl_addrprot_a2n(__u32 *id, const char *arg);
 int rtnl_rtscope_a2n(__u32 *id, const char *arg);
 int rtnl_rttable_a2n(__u32 *id, const char *arg);
 int rtnl_rtrealm_a2n(__u32 *id, const char *arg);
ip/ip_common.h +2 -0
index c4cb1bcb1e3a..4a20ec3cba62 100644
@@ -28,6 +28,8 @@ struct link_filter {
 	char *kind;
 	char *slave_kind;
 	int target_nsid;
+	bool have_proto;
+	int proto;
 };
 
 const char *get_ip_lib_dir(void);
ip/ipaddress.c +32 -2
index 9ba814380342..41055c43ec13 100644
@@ -57,11 +57,13 @@ static void usage(void)
 		"       ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n"
 		"                         [ nomaster ]\n"
 		"                         [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n"
-		"                         [ label LABEL ] [up] [ vrf NAME ] ]\n"
+		"                         [ label LABEL ] [up] [ vrf NAME ]\n"
+		"                         [ proto ADDRPROTO ] ]\n"
 		"       ip address {showdump|restore}\n"
 		"IFADDR := PREFIX | ADDR peer PREFIX\n"
 		"          [ broadcast ADDR ] [ anycast ADDR ]\n"
 		"          [ label IFNAME ] [ scope SCOPE-ID ] [ metric METRIC ]\n"
+		"          [ proto ADDRPROTO ]\n"
 		"SCOPE-ID := [ host | link | global | NUMBER ]\n"
 		"FLAG-LIST := [ FLAG-LIST ] FLAG\n"
 		"FLAG  := [ permanent | dynamic | secondary | primary |\n"
@@ -70,7 +72,9 @@ static void usage(void)
 		"CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"
 		"CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n"
 		"LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"
-		"LFT := forever | SECONDS\n");
+		"LFT := forever | SECONDS\n"
+		"ADDRPROTO := [ NAME | NUMBER ]\n"
+		);
 	iplink_types_usage();
 
 	exit(-1);
@@ -1568,6 +1572,9 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	if (filter.family && filter.family != ifa->ifa_family)
 		return 0;
+	if (filter.have_proto && rta_tb[IFA_PROTO] &&
+	    filter.proto != rta_getattr_u8(rta_tb[IFA_PROTO]))
+		return 0;
 
 	if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL]))
 		return 0;
@@ -1675,6 +1682,14 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	print_ifa_flags(fp, ifa, ifa_flags);
 
+	if (rta_tb[IFA_PROTO]) {
+		__u8 proto = rta_getattr_u8(rta_tb[IFA_PROTO]);
+
+		if (proto || is_json_context())
+			print_string(PRINT_ANY, "protocol", "proto %s ",
+				     rtnl_addrprot_n2a(proto, b1, sizeof(b1)));
+	}
+
 	if (rta_tb[IFA_LABEL])
 		print_string(PRINT_ANY,
 			     "label",
@@ -2196,6 +2211,14 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
 			} else {
 				filter.kind = *argv;
 			}
+		} else if (strcmp(*argv, "proto") == 0) {
+			__u8 proto;
+
+			NEXT_ARG();
+			if (get_u8(&proto, *argv, 0))
+				invarg("\"proto\" value is invalid\n", *argv);
+			filter.have_proto = true;
+			filter.proto = proto;
 		} else {
 			if (strcmp(*argv, "dev") == 0)
 				NEXT_ARG();
@@ -2520,6 +2543,13 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
 			} else {
 				ifa_flags |= flag_data->mask;
 			}
+		} else if (strcmp(*argv, "proto") == 0) {
+			__u8 proto;
+
+			NEXT_ARG();
+			if (get_u8(&proto, *argv, 0))
+				invarg("\"proto\" value is invalid\n", *argv);
+			addattr8(&req.n, sizeof(req), IFA_PROTO, proto);
 		} else {
 			if (strcmp(*argv, "local") == 0)
 				NEXT_ARG();
lib/rt_names.c +62 -0

68 lines not on this page · raw message

Signature · 1 line
2.39.0
#2 lore raw

PATCH 2/2 man: man8: Add man page coverage for "ip address add ... proto"

In reply to a message not in the archive
---
 man/man8/ip-address.8.in | 49 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
man/man8/ip-address.8.in +47 -2
index 1846252df92f..abdd6a2030a0 100644
@@ -50,7 +50,9 @@ ip-address \- protocol address management
 .B vrf
 .IR NAME " ] [ "
 .BR up " ] ["
-.BR nomaster " ] ]"
+.BR nomaster " ]"
+.B proto
+.IR ADDRPROTO " ] ]"
 
 .ti -8
 .BR "ip address" " { " showdump " | " restore " }"
@@ -66,13 +68,19 @@ ip-address \- protocol address management
 .B  label
 .IR LABEL " ] [ "
 .B  scope
-.IR SCOPE-ID " ]"
+.IR SCOPE-ID " ] [ "
+.B proto
+.IR ADDRPROTO " ]"
 
 .ti -8
 .IR SCOPE-ID " := "
 .RB "[ " host " | " link " | " global " | "
 .IR NUMBER " ]"
 
+.ti -8
+.IR ADDRPROTO " := [ "
+.IR NAME " | " NUMBER " ]"
+
 .ti -8
 .IR FLAG-LIST " := [ "  FLAG-LIST " ] " FLAG
 
@@ -288,6 +296,36 @@ flag when adding a multicast address enables similar functionality for
 Openvswitch VXLAN interfaces as well as other tunneling mechanisms that need to
 receive multicast traffic.
 
+.TP
+.BI proto " ADDRPROTO"
+the protocol identifier of this route.
+.I ADDRPROTO
+may be a number or a string from the file
+.BR "/etc/iproute2/rt_addrprotos" .
+If the protocol ID is not given,
+
+.B ip assumes protocol 0. Several protocol
+values have a fixed interpretation. Namely:
+
+.in +8
+.B kernel_lo
+- The ::1 address that kernel installs on a loopback netdevice has this
+  protocol value
+.sp
+
+.B kernel_ra
+- IPv6 addresses installed in response to router advertisement messages
+.sp
+
+.B kernel_ll
+- Link-local addresses have this protocol value
+.sp
+.in -8
+
+.sp
+The rest of the values are not reserved and the administrator is free
+to assign (or not to assign) protocol tags.
+
 .SS ip address delete - delete protocol address
 .B Arguments:
 coincide with the arguments of
@@ -400,6 +438,13 @@ inverse of
 This is an alias for
 .BR temporary " or " secondary .
 
+.TP
+.BI proto " ADDRPROTO"
+Only show addresses with a given protocol, or those for which the kernel
+response did not include protocol. See the corresponding argument to
+.B ip addr add
+for details about address protocols.
+
 .SS ip address flush - flush protocol addresses
 This command flushes the protocol addresses selected by some criteria.
 
Signature · 1 line
2.39.0