xref: /openbmc/linux/drivers/s390/net/qeth_l3_sys.c (revision 2390166a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *		 Frank Pavlic <fpavlic@de.ibm.com>,
6  *		 Thomas Spatzier <tspat@de.ibm.com>,
7  *		 Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9 
10 #include <linux/slab.h>
11 #include <asm/ebcdic.h>
12 #include <linux/hashtable.h>
13 #include <linux/inet.h>
14 #include "qeth_l3.h"
15 
16 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
17 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
18 
19 static int qeth_l3_string_to_ipaddr(const char *buf,
20 				    enum qeth_prot_versions proto, u8 *addr)
21 {
22 	const char *end;
23 
24 	if ((proto == QETH_PROT_IPV4 && !in4_pton(buf, -1, addr, -1, &end)) ||
25 	    (proto == QETH_PROT_IPV6 && !in6_pton(buf, -1, addr, -1, &end)))
26 		return -EINVAL;
27 	return 0;
28 }
29 
30 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
31 			struct qeth_routing_info *route, char *buf)
32 {
33 	switch (route->type) {
34 	case PRIMARY_ROUTER:
35 		return sprintf(buf, "%s\n", "primary router");
36 	case SECONDARY_ROUTER:
37 		return sprintf(buf, "%s\n", "secondary router");
38 	case MULTICAST_ROUTER:
39 		if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
40 			return sprintf(buf, "%s\n", "multicast router+");
41 		else
42 			return sprintf(buf, "%s\n", "multicast router");
43 	case PRIMARY_CONNECTOR:
44 		if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
45 			return sprintf(buf, "%s\n", "primary connector+");
46 		else
47 			return sprintf(buf, "%s\n", "primary connector");
48 	case SECONDARY_CONNECTOR:
49 		if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO)
50 			return sprintf(buf, "%s\n", "secondary connector+");
51 		else
52 			return sprintf(buf, "%s\n", "secondary connector");
53 	default:
54 		return sprintf(buf, "%s\n", "no");
55 	}
56 }
57 
58 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
59 			struct device_attribute *attr, char *buf)
60 {
61 	struct qeth_card *card = dev_get_drvdata(dev);
62 
63 	return qeth_l3_dev_route_show(card, &card->options.route4, buf);
64 }
65 
66 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card,
67 		struct qeth_routing_info *route, enum qeth_prot_versions prot,
68 		const char *buf, size_t count)
69 {
70 	enum qeth_routing_types old_route_type = route->type;
71 	int rc = 0;
72 
73 	mutex_lock(&card->conf_mutex);
74 	if (sysfs_streq(buf, "no_router")) {
75 		route->type = NO_ROUTER;
76 	} else if (sysfs_streq(buf, "primary_connector")) {
77 		route->type = PRIMARY_CONNECTOR;
78 	} else if (sysfs_streq(buf, "secondary_connector")) {
79 		route->type = SECONDARY_CONNECTOR;
80 	} else if (sysfs_streq(buf, "primary_router")) {
81 		route->type = PRIMARY_ROUTER;
82 	} else if (sysfs_streq(buf, "secondary_router")) {
83 		route->type = SECONDARY_ROUTER;
84 	} else if (sysfs_streq(buf, "multicast_router")) {
85 		route->type = MULTICAST_ROUTER;
86 	} else {
87 		rc = -EINVAL;
88 		goto out;
89 	}
90 	if (qeth_card_hw_is_reachable(card) &&
91 	    (old_route_type != route->type)) {
92 		if (prot == QETH_PROT_IPV4)
93 			rc = qeth_l3_setrouting_v4(card);
94 		else if (prot == QETH_PROT_IPV6)
95 			rc = qeth_l3_setrouting_v6(card);
96 	}
97 out:
98 	if (rc)
99 		route->type = old_route_type;
100 	mutex_unlock(&card->conf_mutex);
101 	return rc ? rc : count;
102 }
103 
104 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
105 		struct device_attribute *attr, const char *buf, size_t count)
106 {
107 	struct qeth_card *card = dev_get_drvdata(dev);
108 
109 	return qeth_l3_dev_route_store(card, &card->options.route4,
110 				QETH_PROT_IPV4, buf, count);
111 }
112 
113 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
114 			qeth_l3_dev_route4_store);
115 
116 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
117 			struct device_attribute *attr, char *buf)
118 {
119 	struct qeth_card *card = dev_get_drvdata(dev);
120 
121 	return qeth_l3_dev_route_show(card, &card->options.route6, buf);
122 }
123 
124 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
125 		struct device_attribute *attr, const char *buf, size_t count)
126 {
127 	struct qeth_card *card = dev_get_drvdata(dev);
128 
129 	return qeth_l3_dev_route_store(card, &card->options.route6,
130 				QETH_PROT_IPV6, buf, count);
131 }
132 
133 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
134 			qeth_l3_dev_route6_store);
135 
136 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
137 			struct device_attribute *attr, char *buf)
138 {
139 	struct qeth_card *card = dev_get_drvdata(dev);
140 
141 	return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
142 }
143 
144 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
145 		struct device_attribute *attr, const char *buf, size_t count)
146 {
147 	struct qeth_card *card = dev_get_drvdata(dev);
148 	char *tmp;
149 	int i, rc = 0;
150 
151 	mutex_lock(&card->conf_mutex);
152 	if (card->state != CARD_STATE_DOWN) {
153 		rc = -EPERM;
154 		goto out;
155 	}
156 
157 	i = simple_strtoul(buf, &tmp, 16);
158 	if ((i == 0) || (i == 1))
159 		card->options.fake_broadcast = i;
160 	else
161 		rc = -EINVAL;
162 out:
163 	mutex_unlock(&card->conf_mutex);
164 	return rc ? rc : count;
165 }
166 
167 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
168 		   qeth_l3_dev_fake_broadcast_store);
169 
170 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
171 		struct device_attribute *attr, char *buf)
172 {
173 	struct qeth_card *card = dev_get_drvdata(dev);
174 
175 	return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
176 }
177 
178 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
179 		struct device_attribute *attr, const char *buf, size_t count)
180 {
181 	struct qeth_card *card = dev_get_drvdata(dev);
182 	int rc = 0;
183 	unsigned long i;
184 
185 	if (!IS_IQD(card))
186 		return -EPERM;
187 	if (card->options.cq == QETH_CQ_ENABLED)
188 		return -EPERM;
189 
190 	mutex_lock(&card->conf_mutex);
191 	if (card->state != CARD_STATE_DOWN) {
192 		rc = -EPERM;
193 		goto out;
194 	}
195 
196 	rc = kstrtoul(buf, 16, &i);
197 	if (rc) {
198 		rc = -EINVAL;
199 		goto out;
200 	}
201 	switch (i) {
202 	case 0:
203 		card->options.sniffer = i;
204 		break;
205 	case 1:
206 		qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
207 		if (card->ssqd.qdioac2 & CHSC_AC2_SNIFFER_AVAILABLE) {
208 			card->options.sniffer = i;
209 			if (card->qdio.init_pool.buf_count !=
210 					QETH_IN_BUF_COUNT_MAX)
211 				qeth_realloc_buffer_pool(card,
212 					QETH_IN_BUF_COUNT_MAX);
213 		} else
214 			rc = -EPERM;
215 		break;
216 	default:
217 		rc = -EINVAL;
218 	}
219 out:
220 	mutex_unlock(&card->conf_mutex);
221 	return rc ? rc : count;
222 }
223 
224 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
225 		qeth_l3_dev_sniffer_store);
226 
227 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
228 		struct device_attribute *attr, char *buf)
229 {
230 	struct qeth_card *card = dev_get_drvdata(dev);
231 	char tmp_hsuid[9];
232 
233 	if (!IS_IQD(card))
234 		return -EPERM;
235 
236 	memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
237 	EBCASC(tmp_hsuid, 8);
238 	return sprintf(buf, "%s\n", tmp_hsuid);
239 }
240 
241 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
242 		struct device_attribute *attr, const char *buf, size_t count)
243 {
244 	struct qeth_card *card = dev_get_drvdata(dev);
245 	char *tmp;
246 	int rc;
247 
248 	if (!IS_IQD(card))
249 		return -EPERM;
250 	if (card->state != CARD_STATE_DOWN)
251 		return -EPERM;
252 	if (card->options.sniffer)
253 		return -EPERM;
254 	if (card->options.cq == QETH_CQ_NOTAVAILABLE)
255 		return -EPERM;
256 
257 	tmp = strsep((char **)&buf, "\n");
258 	if (strlen(tmp) > 8)
259 		return -EINVAL;
260 
261 	if (card->options.hsuid[0])
262 		/* delete old ip address */
263 		qeth_l3_modify_hsuid(card, false);
264 
265 	if (strlen(tmp) == 0) {
266 		/* delete ip address only */
267 		card->options.hsuid[0] = '\0';
268 		memcpy(card->dev->perm_addr, card->options.hsuid, 9);
269 		qeth_configure_cq(card, QETH_CQ_DISABLED);
270 		return count;
271 	}
272 
273 	if (qeth_configure_cq(card, QETH_CQ_ENABLED))
274 		return -EPERM;
275 
276 	snprintf(card->options.hsuid, sizeof(card->options.hsuid),
277 		 "%-8s", tmp);
278 	ASCEBC(card->options.hsuid, 8);
279 	memcpy(card->dev->perm_addr, card->options.hsuid, 9);
280 
281 	rc = qeth_l3_modify_hsuid(card, true);
282 
283 	return rc ? rc : count;
284 }
285 
286 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
287 		   qeth_l3_dev_hsuid_store);
288 
289 
290 static struct attribute *qeth_l3_device_attrs[] = {
291 	&dev_attr_route4.attr,
292 	&dev_attr_route6.attr,
293 	&dev_attr_fake_broadcast.attr,
294 	&dev_attr_sniffer.attr,
295 	&dev_attr_hsuid.attr,
296 	NULL,
297 };
298 
299 static const struct attribute_group qeth_l3_device_attr_group = {
300 	.attrs = qeth_l3_device_attrs,
301 };
302 
303 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
304 			struct device_attribute *attr, char *buf)
305 {
306 	struct qeth_card *card = dev_get_drvdata(dev);
307 
308 	return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
309 }
310 
311 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
312 		struct device_attribute *attr, const char *buf, size_t count)
313 {
314 	struct qeth_card *card = dev_get_drvdata(dev);
315 	bool enable;
316 	int rc = 0;
317 
318 	mutex_lock(&card->conf_mutex);
319 	if (card->state != CARD_STATE_DOWN) {
320 		rc = -EPERM;
321 		goto out;
322 	}
323 
324 	if (sysfs_streq(buf, "toggle")) {
325 		enable = !card->ipato.enabled;
326 	} else if (kstrtobool(buf, &enable)) {
327 		rc = -EINVAL;
328 		goto out;
329 	}
330 
331 	if (card->ipato.enabled != enable) {
332 		card->ipato.enabled = enable;
333 		mutex_lock(&card->ip_lock);
334 		qeth_l3_update_ipato(card);
335 		mutex_unlock(&card->ip_lock);
336 	}
337 out:
338 	mutex_unlock(&card->conf_mutex);
339 	return rc ? rc : count;
340 }
341 
342 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
343 			qeth_l3_dev_ipato_enable_show,
344 			qeth_l3_dev_ipato_enable_store);
345 
346 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
347 				struct device_attribute *attr, char *buf)
348 {
349 	struct qeth_card *card = dev_get_drvdata(dev);
350 
351 	return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
352 }
353 
354 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
355 				struct device_attribute *attr,
356 				const char *buf, size_t count)
357 {
358 	struct qeth_card *card = dev_get_drvdata(dev);
359 	bool invert;
360 	int rc = 0;
361 
362 	mutex_lock(&card->conf_mutex);
363 	if (sysfs_streq(buf, "toggle")) {
364 		invert = !card->ipato.invert4;
365 	} else if (kstrtobool(buf, &invert)) {
366 		rc = -EINVAL;
367 		goto out;
368 	}
369 
370 	if (card->ipato.invert4 != invert) {
371 		card->ipato.invert4 = invert;
372 		mutex_lock(&card->ip_lock);
373 		qeth_l3_update_ipato(card);
374 		mutex_unlock(&card->ip_lock);
375 	}
376 out:
377 	mutex_unlock(&card->conf_mutex);
378 	return rc ? rc : count;
379 }
380 
381 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
382 			qeth_l3_dev_ipato_invert4_show,
383 			qeth_l3_dev_ipato_invert4_store);
384 
385 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
386 			enum qeth_prot_versions proto)
387 {
388 	struct qeth_ipato_entry *ipatoe;
389 	int str_len = 0;
390 
391 	mutex_lock(&card->ip_lock);
392 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
393 		char addr_str[40];
394 		int entry_len;
395 
396 		if (ipatoe->proto != proto)
397 			continue;
398 
399 		entry_len = qeth_l3_ipaddr_to_string(proto, ipatoe->addr,
400 						     addr_str);
401 		if (entry_len < 0)
402 			continue;
403 
404 		/* Append /%mask to the entry: */
405 		entry_len += 1 + ((proto == QETH_PROT_IPV4) ? 2 : 3);
406 		/* Enough room to format %entry\n into null terminated page? */
407 		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
408 			break;
409 
410 		entry_len = scnprintf(buf, PAGE_SIZE - str_len,
411 				      "%s/%i\n", addr_str, ipatoe->mask_bits);
412 		str_len += entry_len;
413 		buf += entry_len;
414 	}
415 	mutex_unlock(&card->ip_lock);
416 
417 	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
418 }
419 
420 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
421 				struct device_attribute *attr, char *buf)
422 {
423 	struct qeth_card *card = dev_get_drvdata(dev);
424 
425 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
426 }
427 
428 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
429 		  u8 *addr, int *mask_bits)
430 {
431 	const char *start, *end;
432 	char *tmp;
433 	char buffer[40] = {0, };
434 
435 	start = buf;
436 	/* get address string */
437 	end = strchr(start, '/');
438 	if (!end || (end - start >= 40)) {
439 		return -EINVAL;
440 	}
441 	strncpy(buffer, start, end - start);
442 	if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
443 		return -EINVAL;
444 	}
445 	start = end + 1;
446 	*mask_bits = simple_strtoul(start, &tmp, 10);
447 	if (!strlen(start) ||
448 	    (tmp == start) ||
449 	    (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
450 		return -EINVAL;
451 	}
452 	return 0;
453 }
454 
455 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
456 			 struct qeth_card *card, enum qeth_prot_versions proto)
457 {
458 	struct qeth_ipato_entry *ipatoe;
459 	u8 addr[16];
460 	int mask_bits;
461 	int rc = 0;
462 
463 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
464 	if (rc)
465 		return rc;
466 
467 	ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
468 	if (!ipatoe)
469 		return -ENOMEM;
470 
471 	ipatoe->proto = proto;
472 	memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
473 	ipatoe->mask_bits = mask_bits;
474 
475 	rc = qeth_l3_add_ipato_entry(card, ipatoe);
476 	if (rc)
477 		kfree(ipatoe);
478 
479 	return rc ? rc : count;
480 }
481 
482 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
483 		struct device_attribute *attr, const char *buf, size_t count)
484 {
485 	struct qeth_card *card = dev_get_drvdata(dev);
486 
487 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
488 }
489 
490 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
491 			qeth_l3_dev_ipato_add4_show,
492 			qeth_l3_dev_ipato_add4_store);
493 
494 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
495 			 struct qeth_card *card, enum qeth_prot_versions proto)
496 {
497 	u8 addr[16];
498 	int mask_bits;
499 	int rc = 0;
500 
501 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
502 	if (!rc)
503 		rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
504 	return rc ? rc : count;
505 }
506 
507 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
508 		struct device_attribute *attr, const char *buf, size_t count)
509 {
510 	struct qeth_card *card = dev_get_drvdata(dev);
511 
512 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
513 }
514 
515 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
516 			qeth_l3_dev_ipato_del4_store);
517 
518 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
519 		struct device_attribute *attr, char *buf)
520 {
521 	struct qeth_card *card = dev_get_drvdata(dev);
522 
523 	return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
524 }
525 
526 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
527 		struct device_attribute *attr, const char *buf, size_t count)
528 {
529 	struct qeth_card *card = dev_get_drvdata(dev);
530 	bool invert;
531 	int rc = 0;
532 
533 	mutex_lock(&card->conf_mutex);
534 	if (sysfs_streq(buf, "toggle")) {
535 		invert = !card->ipato.invert6;
536 	} else if (kstrtobool(buf, &invert)) {
537 		rc = -EINVAL;
538 		goto out;
539 	}
540 
541 	if (card->ipato.invert6 != invert) {
542 		card->ipato.invert6 = invert;
543 		mutex_lock(&card->ip_lock);
544 		qeth_l3_update_ipato(card);
545 		mutex_unlock(&card->ip_lock);
546 	}
547 out:
548 	mutex_unlock(&card->conf_mutex);
549 	return rc ? rc : count;
550 }
551 
552 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
553 			qeth_l3_dev_ipato_invert6_show,
554 			qeth_l3_dev_ipato_invert6_store);
555 
556 
557 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
558 				struct device_attribute *attr, char *buf)
559 {
560 	struct qeth_card *card = dev_get_drvdata(dev);
561 
562 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
563 }
564 
565 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
566 		struct device_attribute *attr, const char *buf, size_t count)
567 {
568 	struct qeth_card *card = dev_get_drvdata(dev);
569 
570 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
571 }
572 
573 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
574 			qeth_l3_dev_ipato_add6_show,
575 			qeth_l3_dev_ipato_add6_store);
576 
577 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
578 		struct device_attribute *attr, const char *buf, size_t count)
579 {
580 	struct qeth_card *card = dev_get_drvdata(dev);
581 
582 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
583 }
584 
585 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
586 			qeth_l3_dev_ipato_del6_store);
587 
588 static struct attribute *qeth_ipato_device_attrs[] = {
589 	&dev_attr_ipato_enable.attr,
590 	&dev_attr_ipato_invert4.attr,
591 	&dev_attr_ipato_add4.attr,
592 	&dev_attr_ipato_del4.attr,
593 	&dev_attr_ipato_invert6.attr,
594 	&dev_attr_ipato_add6.attr,
595 	&dev_attr_ipato_del6.attr,
596 	NULL,
597 };
598 
599 static const struct attribute_group qeth_device_ipato_group = {
600 	.name = "ipa_takeover",
601 	.attrs = qeth_ipato_device_attrs,
602 };
603 
604 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
605 				       enum qeth_prot_versions proto,
606 				       enum qeth_ip_types type)
607 {
608 	struct qeth_card *card = dev_get_drvdata(dev);
609 	struct qeth_ipaddr *ipaddr;
610 	int str_len = 0;
611 	int i;
612 
613 	mutex_lock(&card->ip_lock);
614 	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
615 		char addr_str[40];
616 		int entry_len;
617 
618 		if (ipaddr->proto != proto || ipaddr->type != type)
619 			continue;
620 
621 		entry_len = qeth_l3_ipaddr_to_string(proto, (u8 *)&ipaddr->u,
622 						     addr_str);
623 		if (entry_len < 0)
624 			continue;
625 
626 		/* Enough room to format %addr\n into null terminated page? */
627 		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
628 			break;
629 
630 		entry_len = scnprintf(buf, PAGE_SIZE - str_len, "%s\n",
631 				      addr_str);
632 		str_len += entry_len;
633 		buf += entry_len;
634 	}
635 	mutex_unlock(&card->ip_lock);
636 
637 	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
638 }
639 
640 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
641 					  struct device_attribute *attr,
642 					  char *buf)
643 {
644 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
645 				       QETH_IP_TYPE_VIPA);
646 }
647 
648 static ssize_t qeth_l3_vipa_store(struct device *dev, const char *buf, bool add,
649 				  size_t count, enum qeth_prot_versions proto)
650 {
651 	struct qeth_card *card = dev_get_drvdata(dev);
652 	u8 addr[16] = {0, };
653 	int rc;
654 
655 	rc = qeth_l3_string_to_ipaddr(buf, proto, addr);
656 	if (!rc)
657 		rc = qeth_l3_modify_rxip_vipa(card, add, addr,
658 					      QETH_IP_TYPE_VIPA, proto);
659 	return rc ? rc : count;
660 }
661 
662 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
663 		struct device_attribute *attr, const char *buf, size_t count)
664 {
665 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4);
666 }
667 
668 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
669 			qeth_l3_dev_vipa_add4_show,
670 			qeth_l3_dev_vipa_add4_store);
671 
672 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
673 		struct device_attribute *attr, const char *buf, size_t count)
674 {
675 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4);
676 }
677 
678 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
679 			qeth_l3_dev_vipa_del4_store);
680 
681 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
682 					  struct device_attribute *attr,
683 					  char *buf)
684 {
685 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
686 				       QETH_IP_TYPE_VIPA);
687 }
688 
689 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
690 		struct device_attribute *attr, const char *buf, size_t count)
691 {
692 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV6);
693 }
694 
695 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
696 			qeth_l3_dev_vipa_add6_show,
697 			qeth_l3_dev_vipa_add6_store);
698 
699 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
700 		struct device_attribute *attr, const char *buf, size_t count)
701 {
702 	return qeth_l3_vipa_store(dev, buf, false, count, QETH_PROT_IPV6);
703 }
704 
705 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
706 			qeth_l3_dev_vipa_del6_store);
707 
708 static struct attribute *qeth_vipa_device_attrs[] = {
709 	&dev_attr_vipa_add4.attr,
710 	&dev_attr_vipa_del4.attr,
711 	&dev_attr_vipa_add6.attr,
712 	&dev_attr_vipa_del6.attr,
713 	NULL,
714 };
715 
716 static const struct attribute_group qeth_device_vipa_group = {
717 	.name = "vipa",
718 	.attrs = qeth_vipa_device_attrs,
719 };
720 
721 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
722 					  struct device_attribute *attr,
723 					  char *buf)
724 {
725 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
726 				       QETH_IP_TYPE_RXIP);
727 }
728 
729 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
730 		 u8 *addr)
731 {
732 	__be32 ipv4_addr;
733 	struct in6_addr ipv6_addr;
734 
735 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
736 		return -EINVAL;
737 	}
738 	if (proto == QETH_PROT_IPV4) {
739 		memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
740 		if (ipv4_is_multicast(ipv4_addr)) {
741 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
742 			return -EINVAL;
743 		}
744 	} else if (proto == QETH_PROT_IPV6) {
745 		memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
746 		if (ipv6_addr_is_multicast(&ipv6_addr)) {
747 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
748 			return -EINVAL;
749 		}
750 	}
751 
752 	return 0;
753 }
754 
755 static ssize_t qeth_l3_rxip_store(struct device *dev, const char *buf, bool add,
756 				  size_t count, enum qeth_prot_versions proto)
757 {
758 	struct qeth_card *card = dev_get_drvdata(dev);
759 	u8 addr[16] = {0, };
760 	int rc;
761 
762 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
763 	if (!rc)
764 		rc = qeth_l3_modify_rxip_vipa(card, add, addr,
765 					      QETH_IP_TYPE_RXIP, proto);
766 	return rc ? rc : count;
767 }
768 
769 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
770 		struct device_attribute *attr, const char *buf, size_t count)
771 {
772 	return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV4);
773 }
774 
775 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
776 			qeth_l3_dev_rxip_add4_show,
777 			qeth_l3_dev_rxip_add4_store);
778 
779 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
780 		struct device_attribute *attr, const char *buf, size_t count)
781 {
782 	return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV4);
783 }
784 
785 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
786 			qeth_l3_dev_rxip_del4_store);
787 
788 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
789 					  struct device_attribute *attr,
790 					  char *buf)
791 {
792 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
793 				       QETH_IP_TYPE_RXIP);
794 }
795 
796 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
797 		struct device_attribute *attr, const char *buf, size_t count)
798 {
799 	return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV6);
800 }
801 
802 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
803 			qeth_l3_dev_rxip_add6_show,
804 			qeth_l3_dev_rxip_add6_store);
805 
806 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
807 		struct device_attribute *attr, const char *buf, size_t count)
808 {
809 	return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV6);
810 }
811 
812 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
813 			qeth_l3_dev_rxip_del6_store);
814 
815 static struct attribute *qeth_rxip_device_attrs[] = {
816 	&dev_attr_rxip_add4.attr,
817 	&dev_attr_rxip_del4.attr,
818 	&dev_attr_rxip_add6.attr,
819 	&dev_attr_rxip_del6.attr,
820 	NULL,
821 };
822 
823 static const struct attribute_group qeth_device_rxip_group = {
824 	.name = "rxip",
825 	.attrs = qeth_rxip_device_attrs,
826 };
827 
828 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
829 	&qeth_l3_device_attr_group,
830 	&qeth_device_ipato_group,
831 	&qeth_device_vipa_group,
832 	&qeth_device_rxip_group,
833 	NULL,
834 };
835 
836 int qeth_l3_create_device_attributes(struct device *dev)
837 {
838 	return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
839 }
840 
841 void qeth_l3_remove_device_attributes(struct device *dev)
842 {
843 	sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
844 }
845 
846 const struct attribute_group *qeth_l3_attr_groups[] = {
847 	&qeth_device_attr_group,
848 	&qeth_device_blkt_group,
849 	/* l3 specific, see qeth_l3_only_attr_groups: */
850 	&qeth_l3_device_attr_group,
851 	&qeth_device_ipato_group,
852 	&qeth_device_vipa_group,
853 	&qeth_device_rxip_group,
854 	NULL,
855 };
856