1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/proc_fs.h>
3 #include <linux/export.h>
4 #include <net/net_namespace.h>
5 #include <net/netns/generic.h>
6 #include <net/bonding.h>
7 
8 #include "bonding_priv.h"
9 
10 static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
11 	__acquires(RCU)
12 {
13 	struct bonding *bond = seq->private;
14 	struct list_head *iter;
15 	struct slave *slave;
16 	loff_t off = 0;
17 
18 	rcu_read_lock();
19 
20 	if (*pos == 0)
21 		return SEQ_START_TOKEN;
22 
23 	bond_for_each_slave_rcu(bond, slave, iter)
24 		if (++off == *pos)
25 			return slave;
26 
27 	return NULL;
28 }
29 
30 static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
31 {
32 	struct bonding *bond = seq->private;
33 	struct list_head *iter;
34 	struct slave *slave;
35 	bool found = false;
36 
37 	++*pos;
38 	if (v == SEQ_START_TOKEN)
39 		return bond_first_slave_rcu(bond);
40 
41 	bond_for_each_slave_rcu(bond, slave, iter) {
42 		if (found)
43 			return slave;
44 		if (slave == v)
45 			found = true;
46 	}
47 
48 	return NULL;
49 }
50 
51 static void bond_info_seq_stop(struct seq_file *seq, void *v)
52 	__releases(RCU)
53 {
54 	rcu_read_unlock();
55 }
56 
57 static void bond_info_show_master(struct seq_file *seq)
58 {
59 	struct bonding *bond = seq->private;
60 	const struct bond_opt_value *optval;
61 	struct slave *curr, *primary;
62 	int i;
63 
64 	curr = rcu_dereference(bond->curr_active_slave);
65 
66 	seq_printf(seq, "Bonding Mode: %s",
67 		   bond_mode_name(BOND_MODE(bond)));
68 
69 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
70 	    bond->params.fail_over_mac) {
71 		optval = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
72 					  bond->params.fail_over_mac);
73 		seq_printf(seq, " (fail_over_mac %s)", optval->string);
74 	}
75 
76 	seq_printf(seq, "\n");
77 
78 	if (bond_mode_uses_xmit_hash(bond)) {
79 		optval = bond_opt_get_val(BOND_OPT_XMIT_HASH,
80 					  bond->params.xmit_policy);
81 		seq_printf(seq, "Transmit Hash Policy: %s (%d)\n",
82 			   optval->string, bond->params.xmit_policy);
83 	}
84 
85 	if (bond_uses_primary(bond)) {
86 		primary = rcu_dereference(bond->primary_slave);
87 		seq_printf(seq, "Primary Slave: %s",
88 			   primary ? primary->dev->name : "None");
89 		if (primary) {
90 			optval = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
91 						  bond->params.primary_reselect);
92 			seq_printf(seq, " (primary_reselect %s)",
93 				   optval->string);
94 		}
95 
96 		seq_printf(seq, "\nCurrently Active Slave: %s\n",
97 			   (curr) ? curr->dev->name : "None");
98 	}
99 
100 	seq_printf(seq, "MII Status: %s\n", netif_carrier_ok(bond->dev) ?
101 		   "up" : "down");
102 	seq_printf(seq, "MII Polling Interval (ms): %d\n", bond->params.miimon);
103 	seq_printf(seq, "Up Delay (ms): %d\n",
104 		   bond->params.updelay * bond->params.miimon);
105 	seq_printf(seq, "Down Delay (ms): %d\n",
106 		   bond->params.downdelay * bond->params.miimon);
107 
108 
109 	/* ARP information */
110 	if (bond->params.arp_interval > 0) {
111 		int printed = 0;
112 		seq_printf(seq, "ARP Polling Interval (ms): %d\n",
113 				bond->params.arp_interval);
114 
115 		seq_printf(seq, "ARP IP target/s (n.n.n.n form):");
116 
117 		for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
118 			if (!bond->params.arp_targets[i])
119 				break;
120 			if (printed)
121 				seq_printf(seq, ",");
122 			seq_printf(seq, " %pI4", &bond->params.arp_targets[i]);
123 			printed = 1;
124 		}
125 		seq_printf(seq, "\n");
126 	}
127 
128 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
129 		struct ad_info ad_info;
130 
131 		seq_puts(seq, "\n802.3ad info\n");
132 		seq_printf(seq, "LACP rate: %s\n",
133 			   (bond->params.lacp_fast) ? "fast" : "slow");
134 		seq_printf(seq, "Min links: %d\n", bond->params.min_links);
135 		optval = bond_opt_get_val(BOND_OPT_AD_SELECT,
136 					  bond->params.ad_select);
137 		seq_printf(seq, "Aggregator selection policy (ad_select): %s\n",
138 			   optval->string);
139 		if (capable(CAP_NET_ADMIN)) {
140 			seq_printf(seq, "System priority: %d\n",
141 				   BOND_AD_INFO(bond).system.sys_priority);
142 			seq_printf(seq, "System MAC address: %pM\n",
143 				   &BOND_AD_INFO(bond).system.sys_mac_addr);
144 
145 			if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
146 				seq_printf(seq,
147 					   "bond %s has no active aggregator\n",
148 					   bond->dev->name);
149 			} else {
150 				seq_printf(seq, "Active Aggregator Info:\n");
151 
152 				seq_printf(seq, "\tAggregator ID: %d\n",
153 					   ad_info.aggregator_id);
154 				seq_printf(seq, "\tNumber of ports: %d\n",
155 					   ad_info.ports);
156 				seq_printf(seq, "\tActor Key: %d\n",
157 					   ad_info.actor_key);
158 				seq_printf(seq, "\tPartner Key: %d\n",
159 					   ad_info.partner_key);
160 				seq_printf(seq, "\tPartner Mac Address: %pM\n",
161 					   ad_info.partner_system);
162 			}
163 		}
164 	}
165 }
166 
167 static void bond_info_show_slave(struct seq_file *seq,
168 				 const struct slave *slave)
169 {
170 	struct bonding *bond = seq->private;
171 
172 	seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
173 	seq_printf(seq, "MII Status: %s\n", bond_slave_link_status(slave->link));
174 	if (slave->speed == SPEED_UNKNOWN)
175 		seq_printf(seq, "Speed: %s\n", "Unknown");
176 	else
177 		seq_printf(seq, "Speed: %d Mbps\n", slave->speed);
178 
179 	if (slave->duplex == DUPLEX_UNKNOWN)
180 		seq_printf(seq, "Duplex: %s\n", "Unknown");
181 	else
182 		seq_printf(seq, "Duplex: %s\n", slave->duplex ? "full" : "half");
183 
184 	seq_printf(seq, "Link Failure Count: %u\n",
185 		   slave->link_failure_count);
186 
187 	seq_printf(seq, "Permanent HW addr: %*phC\n",
188 		   slave->dev->addr_len, slave->perm_hwaddr);
189 	seq_printf(seq, "Slave queue ID: %d\n", slave->queue_id);
190 
191 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
192 		const struct port *port = &SLAVE_AD_INFO(slave)->port;
193 		const struct aggregator *agg = port->aggregator;
194 
195 		if (agg) {
196 			seq_printf(seq, "Aggregator ID: %d\n",
197 				   agg->aggregator_identifier);
198 			seq_printf(seq, "Actor Churn State: %s\n",
199 				   bond_3ad_churn_desc(port->sm_churn_actor_state));
200 			seq_printf(seq, "Partner Churn State: %s\n",
201 				   bond_3ad_churn_desc(port->sm_churn_partner_state));
202 			seq_printf(seq, "Actor Churned Count: %d\n",
203 				   port->churn_actor_count);
204 			seq_printf(seq, "Partner Churned Count: %d\n",
205 				   port->churn_partner_count);
206 
207 			if (capable(CAP_NET_ADMIN)) {
208 				seq_puts(seq, "details actor lacp pdu:\n");
209 				seq_printf(seq, "    system priority: %d\n",
210 					   port->actor_system_priority);
211 				seq_printf(seq, "    system mac address: %pM\n",
212 					   &port->actor_system);
213 				seq_printf(seq, "    port key: %d\n",
214 					   port->actor_oper_port_key);
215 				seq_printf(seq, "    port priority: %d\n",
216 					   port->actor_port_priority);
217 				seq_printf(seq, "    port number: %d\n",
218 					   port->actor_port_number);
219 				seq_printf(seq, "    port state: %d\n",
220 					   port->actor_oper_port_state);
221 
222 				seq_puts(seq, "details partner lacp pdu:\n");
223 				seq_printf(seq, "    system priority: %d\n",
224 					   port->partner_oper.system_priority);
225 				seq_printf(seq, "    system mac address: %pM\n",
226 					   &port->partner_oper.system);
227 				seq_printf(seq, "    oper key: %d\n",
228 					   port->partner_oper.key);
229 				seq_printf(seq, "    port priority: %d\n",
230 					   port->partner_oper.port_priority);
231 				seq_printf(seq, "    port number: %d\n",
232 					   port->partner_oper.port_number);
233 				seq_printf(seq, "    port state: %d\n",
234 					   port->partner_oper.port_state);
235 			}
236 		} else {
237 			seq_puts(seq, "Aggregator ID: N/A\n");
238 		}
239 	}
240 }
241 
242 static int bond_info_seq_show(struct seq_file *seq, void *v)
243 {
244 	if (v == SEQ_START_TOKEN) {
245 		seq_printf(seq, "%s\n", bond_version);
246 		bond_info_show_master(seq);
247 	} else
248 		bond_info_show_slave(seq, v);
249 
250 	return 0;
251 }
252 
253 static const struct seq_operations bond_info_seq_ops = {
254 	.start = bond_info_seq_start,
255 	.next  = bond_info_seq_next,
256 	.stop  = bond_info_seq_stop,
257 	.show  = bond_info_seq_show,
258 };
259 
260 static int bond_info_open(struct inode *inode, struct file *file)
261 {
262 	struct seq_file *seq;
263 	int res;
264 
265 	res = seq_open(file, &bond_info_seq_ops);
266 	if (!res) {
267 		/* recover the pointer buried in proc_dir_entry data */
268 		seq = file->private_data;
269 		seq->private = PDE_DATA(inode);
270 	}
271 
272 	return res;
273 }
274 
275 static const struct file_operations bond_info_fops = {
276 	.owner   = THIS_MODULE,
277 	.open    = bond_info_open,
278 	.read    = seq_read,
279 	.llseek  = seq_lseek,
280 	.release = seq_release,
281 };
282 
283 void bond_create_proc_entry(struct bonding *bond)
284 {
285 	struct net_device *bond_dev = bond->dev;
286 	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
287 
288 	if (bn->proc_dir) {
289 		bond->proc_entry = proc_create_data(bond_dev->name,
290 						    0444, bn->proc_dir,
291 						    &bond_info_fops, bond);
292 		if (bond->proc_entry == NULL)
293 			netdev_warn(bond_dev, "Cannot create /proc/net/%s/%s\n",
294 				    DRV_NAME, bond_dev->name);
295 		else
296 			memcpy(bond->proc_file_name, bond_dev->name, IFNAMSIZ);
297 	}
298 }
299 
300 void bond_remove_proc_entry(struct bonding *bond)
301 {
302 	struct net_device *bond_dev = bond->dev;
303 	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
304 
305 	if (bn->proc_dir && bond->proc_entry) {
306 		remove_proc_entry(bond->proc_file_name, bn->proc_dir);
307 		memset(bond->proc_file_name, 0, IFNAMSIZ);
308 		bond->proc_entry = NULL;
309 	}
310 }
311 
312 /* Create the bonding directory under /proc/net, if doesn't exist yet.
313  * Caller must hold rtnl_lock.
314  */
315 void __net_init bond_create_proc_dir(struct bond_net *bn)
316 {
317 	if (!bn->proc_dir) {
318 		bn->proc_dir = proc_mkdir(DRV_NAME, bn->net->proc_net);
319 		if (!bn->proc_dir)
320 			pr_warn("Warning: Cannot create /proc/net/%s\n",
321 				DRV_NAME);
322 	}
323 }
324 
325 /* Destroy the bonding directory under /proc/net, if empty.
326  * Caller must hold rtnl_lock.
327  */
328 void __net_exit bond_destroy_proc_dir(struct bond_net *bn)
329 {
330 	if (bn->proc_dir) {
331 		remove_proc_entry(DRV_NAME, bn->net->proc_net);
332 		bn->proc_dir = NULL;
333 	}
334 }
335