xref: /openbmc/linux/drivers/s390/net/qeth_l3_sys.c (revision dc6a81c3)
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 	int rc = 0;
246 	char *tmp;
247 
248 	if (!IS_IQD(card))
249 		return -EPERM;
250 
251 	mutex_lock(&card->conf_mutex);
252 	if (card->state != CARD_STATE_DOWN) {
253 		rc = -EPERM;
254 		goto out;
255 	}
256 
257 	if (card->options.sniffer) {
258 		rc = -EPERM;
259 		goto out;
260 	}
261 
262 	if (card->options.cq == QETH_CQ_NOTAVAILABLE) {
263 		rc = -EPERM;
264 		goto out;
265 	}
266 
267 	tmp = strsep((char **)&buf, "\n");
268 	if (strlen(tmp) > 8) {
269 		rc = -EINVAL;
270 		goto out;
271 	}
272 
273 	if (card->options.hsuid[0])
274 		/* delete old ip address */
275 		qeth_l3_modify_hsuid(card, false);
276 
277 	if (strlen(tmp) == 0) {
278 		/* delete ip address only */
279 		card->options.hsuid[0] = '\0';
280 		memcpy(card->dev->perm_addr, card->options.hsuid, 9);
281 		qeth_configure_cq(card, QETH_CQ_DISABLED);
282 		goto out;
283 	}
284 
285 	if (qeth_configure_cq(card, QETH_CQ_ENABLED)) {
286 		rc = -EPERM;
287 		goto out;
288 	}
289 
290 	snprintf(card->options.hsuid, sizeof(card->options.hsuid),
291 		 "%-8s", tmp);
292 	ASCEBC(card->options.hsuid, 8);
293 	memcpy(card->dev->perm_addr, card->options.hsuid, 9);
294 
295 	rc = qeth_l3_modify_hsuid(card, true);
296 
297 out:
298 	mutex_unlock(&card->conf_mutex);
299 	return rc ? rc : count;
300 }
301 
302 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
303 		   qeth_l3_dev_hsuid_store);
304 
305 
306 static struct attribute *qeth_l3_device_attrs[] = {
307 	&dev_attr_route4.attr,
308 	&dev_attr_route6.attr,
309 	&dev_attr_fake_broadcast.attr,
310 	&dev_attr_sniffer.attr,
311 	&dev_attr_hsuid.attr,
312 	NULL,
313 };
314 
315 static const struct attribute_group qeth_l3_device_attr_group = {
316 	.attrs = qeth_l3_device_attrs,
317 };
318 
319 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
320 			struct device_attribute *attr, char *buf)
321 {
322 	struct qeth_card *card = dev_get_drvdata(dev);
323 
324 	return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
325 }
326 
327 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
328 		struct device_attribute *attr, const char *buf, size_t count)
329 {
330 	struct qeth_card *card = dev_get_drvdata(dev);
331 	bool enable;
332 	int rc = 0;
333 
334 	mutex_lock(&card->conf_mutex);
335 	if (card->state != CARD_STATE_DOWN) {
336 		rc = -EPERM;
337 		goto out;
338 	}
339 
340 	if (sysfs_streq(buf, "toggle")) {
341 		enable = !card->ipato.enabled;
342 	} else if (kstrtobool(buf, &enable)) {
343 		rc = -EINVAL;
344 		goto out;
345 	}
346 
347 	if (card->ipato.enabled != enable) {
348 		card->ipato.enabled = enable;
349 		mutex_lock(&card->ip_lock);
350 		qeth_l3_update_ipato(card);
351 		mutex_unlock(&card->ip_lock);
352 	}
353 out:
354 	mutex_unlock(&card->conf_mutex);
355 	return rc ? rc : count;
356 }
357 
358 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
359 			qeth_l3_dev_ipato_enable_show,
360 			qeth_l3_dev_ipato_enable_store);
361 
362 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
363 				struct device_attribute *attr, char *buf)
364 {
365 	struct qeth_card *card = dev_get_drvdata(dev);
366 
367 	return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
368 }
369 
370 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
371 				struct device_attribute *attr,
372 				const char *buf, size_t count)
373 {
374 	struct qeth_card *card = dev_get_drvdata(dev);
375 	bool invert;
376 	int rc = 0;
377 
378 	mutex_lock(&card->conf_mutex);
379 	if (sysfs_streq(buf, "toggle")) {
380 		invert = !card->ipato.invert4;
381 	} else if (kstrtobool(buf, &invert)) {
382 		rc = -EINVAL;
383 		goto out;
384 	}
385 
386 	if (card->ipato.invert4 != invert) {
387 		card->ipato.invert4 = invert;
388 		mutex_lock(&card->ip_lock);
389 		qeth_l3_update_ipato(card);
390 		mutex_unlock(&card->ip_lock);
391 	}
392 out:
393 	mutex_unlock(&card->conf_mutex);
394 	return rc ? rc : count;
395 }
396 
397 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
398 			qeth_l3_dev_ipato_invert4_show,
399 			qeth_l3_dev_ipato_invert4_store);
400 
401 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
402 			enum qeth_prot_versions proto)
403 {
404 	struct qeth_ipato_entry *ipatoe;
405 	int str_len = 0;
406 
407 	mutex_lock(&card->ip_lock);
408 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
409 		char addr_str[40];
410 		int entry_len;
411 
412 		if (ipatoe->proto != proto)
413 			continue;
414 
415 		entry_len = qeth_l3_ipaddr_to_string(proto, ipatoe->addr,
416 						     addr_str);
417 		if (entry_len < 0)
418 			continue;
419 
420 		/* Append /%mask to the entry: */
421 		entry_len += 1 + ((proto == QETH_PROT_IPV4) ? 2 : 3);
422 		/* Enough room to format %entry\n into null terminated page? */
423 		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
424 			break;
425 
426 		entry_len = scnprintf(buf, PAGE_SIZE - str_len,
427 				      "%s/%i\n", addr_str, ipatoe->mask_bits);
428 		str_len += entry_len;
429 		buf += entry_len;
430 	}
431 	mutex_unlock(&card->ip_lock);
432 
433 	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
434 }
435 
436 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
437 				struct device_attribute *attr, char *buf)
438 {
439 	struct qeth_card *card = dev_get_drvdata(dev);
440 
441 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
442 }
443 
444 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
445 		  u8 *addr, int *mask_bits)
446 {
447 	const char *start, *end;
448 	char *tmp;
449 	char buffer[40] = {0, };
450 
451 	start = buf;
452 	/* get address string */
453 	end = strchr(start, '/');
454 	if (!end || (end - start >= 40)) {
455 		return -EINVAL;
456 	}
457 	strncpy(buffer, start, end - start);
458 	if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
459 		return -EINVAL;
460 	}
461 	start = end + 1;
462 	*mask_bits = simple_strtoul(start, &tmp, 10);
463 	if (!strlen(start) ||
464 	    (tmp == start) ||
465 	    (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
466 		return -EINVAL;
467 	}
468 	return 0;
469 }
470 
471 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
472 			 struct qeth_card *card, enum qeth_prot_versions proto)
473 {
474 	struct qeth_ipato_entry *ipatoe;
475 	u8 addr[16];
476 	int mask_bits;
477 	int rc = 0;
478 
479 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
480 	if (rc)
481 		return rc;
482 
483 	ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
484 	if (!ipatoe)
485 		return -ENOMEM;
486 
487 	ipatoe->proto = proto;
488 	memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
489 	ipatoe->mask_bits = mask_bits;
490 
491 	rc = qeth_l3_add_ipato_entry(card, ipatoe);
492 	if (rc)
493 		kfree(ipatoe);
494 
495 	return rc ? rc : count;
496 }
497 
498 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
499 		struct device_attribute *attr, const char *buf, size_t count)
500 {
501 	struct qeth_card *card = dev_get_drvdata(dev);
502 
503 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
504 }
505 
506 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
507 			qeth_l3_dev_ipato_add4_show,
508 			qeth_l3_dev_ipato_add4_store);
509 
510 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
511 			 struct qeth_card *card, enum qeth_prot_versions proto)
512 {
513 	u8 addr[16];
514 	int mask_bits;
515 	int rc = 0;
516 
517 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
518 	if (!rc)
519 		rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
520 	return rc ? rc : count;
521 }
522 
523 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
524 		struct device_attribute *attr, const char *buf, size_t count)
525 {
526 	struct qeth_card *card = dev_get_drvdata(dev);
527 
528 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
529 }
530 
531 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
532 			qeth_l3_dev_ipato_del4_store);
533 
534 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
535 		struct device_attribute *attr, char *buf)
536 {
537 	struct qeth_card *card = dev_get_drvdata(dev);
538 
539 	return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
540 }
541 
542 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
543 		struct device_attribute *attr, const char *buf, size_t count)
544 {
545 	struct qeth_card *card = dev_get_drvdata(dev);
546 	bool invert;
547 	int rc = 0;
548 
549 	mutex_lock(&card->conf_mutex);
550 	if (sysfs_streq(buf, "toggle")) {
551 		invert = !card->ipato.invert6;
552 	} else if (kstrtobool(buf, &invert)) {
553 		rc = -EINVAL;
554 		goto out;
555 	}
556 
557 	if (card->ipato.invert6 != invert) {
558 		card->ipato.invert6 = invert;
559 		mutex_lock(&card->ip_lock);
560 		qeth_l3_update_ipato(card);
561 		mutex_unlock(&card->ip_lock);
562 	}
563 out:
564 	mutex_unlock(&card->conf_mutex);
565 	return rc ? rc : count;
566 }
567 
568 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
569 			qeth_l3_dev_ipato_invert6_show,
570 			qeth_l3_dev_ipato_invert6_store);
571 
572 
573 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
574 				struct device_attribute *attr, char *buf)
575 {
576 	struct qeth_card *card = dev_get_drvdata(dev);
577 
578 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
579 }
580 
581 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
582 		struct device_attribute *attr, const char *buf, size_t count)
583 {
584 	struct qeth_card *card = dev_get_drvdata(dev);
585 
586 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
587 }
588 
589 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
590 			qeth_l3_dev_ipato_add6_show,
591 			qeth_l3_dev_ipato_add6_store);
592 
593 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
594 		struct device_attribute *attr, const char *buf, size_t count)
595 {
596 	struct qeth_card *card = dev_get_drvdata(dev);
597 
598 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
599 }
600 
601 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
602 			qeth_l3_dev_ipato_del6_store);
603 
604 static struct attribute *qeth_ipato_device_attrs[] = {
605 	&dev_attr_ipato_enable.attr,
606 	&dev_attr_ipato_invert4.attr,
607 	&dev_attr_ipato_add4.attr,
608 	&dev_attr_ipato_del4.attr,
609 	&dev_attr_ipato_invert6.attr,
610 	&dev_attr_ipato_add6.attr,
611 	&dev_attr_ipato_del6.attr,
612 	NULL,
613 };
614 
615 static const struct attribute_group qeth_device_ipato_group = {
616 	.name = "ipa_takeover",
617 	.attrs = qeth_ipato_device_attrs,
618 };
619 
620 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
621 				       enum qeth_prot_versions proto,
622 				       enum qeth_ip_types type)
623 {
624 	struct qeth_card *card = dev_get_drvdata(dev);
625 	struct qeth_ipaddr *ipaddr;
626 	int str_len = 0;
627 	int i;
628 
629 	mutex_lock(&card->ip_lock);
630 	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
631 		char addr_str[40];
632 		int entry_len;
633 
634 		if (ipaddr->proto != proto || ipaddr->type != type)
635 			continue;
636 
637 		entry_len = qeth_l3_ipaddr_to_string(proto, (u8 *)&ipaddr->u,
638 						     addr_str);
639 		if (entry_len < 0)
640 			continue;
641 
642 		/* Enough room to format %addr\n into null terminated page? */
643 		if (entry_len + 1 > PAGE_SIZE - str_len - 1)
644 			break;
645 
646 		entry_len = scnprintf(buf, PAGE_SIZE - str_len, "%s\n",
647 				      addr_str);
648 		str_len += entry_len;
649 		buf += entry_len;
650 	}
651 	mutex_unlock(&card->ip_lock);
652 
653 	return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n");
654 }
655 
656 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
657 					  struct device_attribute *attr,
658 					  char *buf)
659 {
660 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
661 				       QETH_IP_TYPE_VIPA);
662 }
663 
664 static ssize_t qeth_l3_vipa_store(struct device *dev, const char *buf, bool add,
665 				  size_t count, enum qeth_prot_versions proto)
666 {
667 	struct qeth_card *card = dev_get_drvdata(dev);
668 	u8 addr[16] = {0, };
669 	int rc;
670 
671 	rc = qeth_l3_string_to_ipaddr(buf, proto, addr);
672 	if (!rc)
673 		rc = qeth_l3_modify_rxip_vipa(card, add, addr,
674 					      QETH_IP_TYPE_VIPA, proto);
675 	return rc ? rc : count;
676 }
677 
678 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
679 		struct device_attribute *attr, const char *buf, size_t count)
680 {
681 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4);
682 }
683 
684 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
685 			qeth_l3_dev_vipa_add4_show,
686 			qeth_l3_dev_vipa_add4_store);
687 
688 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
689 		struct device_attribute *attr, const char *buf, size_t count)
690 {
691 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4);
692 }
693 
694 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
695 			qeth_l3_dev_vipa_del4_store);
696 
697 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
698 					  struct device_attribute *attr,
699 					  char *buf)
700 {
701 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
702 				       QETH_IP_TYPE_VIPA);
703 }
704 
705 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
706 		struct device_attribute *attr, const char *buf, size_t count)
707 {
708 	return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV6);
709 }
710 
711 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
712 			qeth_l3_dev_vipa_add6_show,
713 			qeth_l3_dev_vipa_add6_store);
714 
715 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
716 		struct device_attribute *attr, const char *buf, size_t count)
717 {
718 	return qeth_l3_vipa_store(dev, buf, false, count, QETH_PROT_IPV6);
719 }
720 
721 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
722 			qeth_l3_dev_vipa_del6_store);
723 
724 static struct attribute *qeth_vipa_device_attrs[] = {
725 	&dev_attr_vipa_add4.attr,
726 	&dev_attr_vipa_del4.attr,
727 	&dev_attr_vipa_add6.attr,
728 	&dev_attr_vipa_del6.attr,
729 	NULL,
730 };
731 
732 static const struct attribute_group qeth_device_vipa_group = {
733 	.name = "vipa",
734 	.attrs = qeth_vipa_device_attrs,
735 };
736 
737 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
738 					  struct device_attribute *attr,
739 					  char *buf)
740 {
741 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
742 				       QETH_IP_TYPE_RXIP);
743 }
744 
745 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
746 		 u8 *addr)
747 {
748 	__be32 ipv4_addr;
749 	struct in6_addr ipv6_addr;
750 
751 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
752 		return -EINVAL;
753 	}
754 	if (proto == QETH_PROT_IPV4) {
755 		memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
756 		if (ipv4_is_multicast(ipv4_addr)) {
757 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
758 			return -EINVAL;
759 		}
760 	} else if (proto == QETH_PROT_IPV6) {
761 		memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
762 		if (ipv6_addr_is_multicast(&ipv6_addr)) {
763 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
764 			return -EINVAL;
765 		}
766 	}
767 
768 	return 0;
769 }
770 
771 static ssize_t qeth_l3_rxip_store(struct device *dev, const char *buf, bool add,
772 				  size_t count, enum qeth_prot_versions proto)
773 {
774 	struct qeth_card *card = dev_get_drvdata(dev);
775 	u8 addr[16] = {0, };
776 	int rc;
777 
778 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
779 	if (!rc)
780 		rc = qeth_l3_modify_rxip_vipa(card, add, addr,
781 					      QETH_IP_TYPE_RXIP, proto);
782 	return rc ? rc : count;
783 }
784 
785 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
786 		struct device_attribute *attr, const char *buf, size_t count)
787 {
788 	return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV4);
789 }
790 
791 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
792 			qeth_l3_dev_rxip_add4_show,
793 			qeth_l3_dev_rxip_add4_store);
794 
795 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
796 		struct device_attribute *attr, const char *buf, size_t count)
797 {
798 	return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV4);
799 }
800 
801 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
802 			qeth_l3_dev_rxip_del4_store);
803 
804 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
805 					  struct device_attribute *attr,
806 					  char *buf)
807 {
808 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
809 				       QETH_IP_TYPE_RXIP);
810 }
811 
812 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
813 		struct device_attribute *attr, const char *buf, size_t count)
814 {
815 	return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV6);
816 }
817 
818 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
819 			qeth_l3_dev_rxip_add6_show,
820 			qeth_l3_dev_rxip_add6_store);
821 
822 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
823 		struct device_attribute *attr, const char *buf, size_t count)
824 {
825 	return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV6);
826 }
827 
828 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
829 			qeth_l3_dev_rxip_del6_store);
830 
831 static struct attribute *qeth_rxip_device_attrs[] = {
832 	&dev_attr_rxip_add4.attr,
833 	&dev_attr_rxip_del4.attr,
834 	&dev_attr_rxip_add6.attr,
835 	&dev_attr_rxip_del6.attr,
836 	NULL,
837 };
838 
839 static const struct attribute_group qeth_device_rxip_group = {
840 	.name = "rxip",
841 	.attrs = qeth_rxip_device_attrs,
842 };
843 
844 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
845 	&qeth_l3_device_attr_group,
846 	&qeth_device_ipato_group,
847 	&qeth_device_vipa_group,
848 	&qeth_device_rxip_group,
849 	NULL,
850 };
851 
852 int qeth_l3_create_device_attributes(struct device *dev)
853 {
854 	return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
855 }
856 
857 void qeth_l3_remove_device_attributes(struct device *dev)
858 {
859 	sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
860 }
861 
862 const struct attribute_group *qeth_l3_attr_groups[] = {
863 	&qeth_device_attr_group,
864 	&qeth_device_blkt_group,
865 	/* l3 specific, see qeth_l3_only_attr_groups: */
866 	&qeth_l3_device_attr_group,
867 	&qeth_device_ipato_group,
868 	&qeth_device_vipa_group,
869 	&qeth_device_rxip_group,
870 	NULL,
871 };
872