xref: /openbmc/linux/drivers/s390/net/qeth_l3_sys.c (revision 068ac0db)
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 	char addr_str[40];
390 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
391 	int i = 0;
392 
393 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
394 	/* add strlen for "/<mask>\n" */
395 	entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
396 	mutex_lock(&card->ip_lock);
397 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
398 		if (ipatoe->proto != proto)
399 			continue;
400 		/* String must not be longer than PAGE_SIZE. So we check if
401 		 * string length gets near PAGE_SIZE. Then we can savely display
402 		 * the next IPv6 address (worst case, compared to IPv4) */
403 		if ((PAGE_SIZE - i) <= entry_len)
404 			break;
405 		qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
406 		i += snprintf(buf + i, PAGE_SIZE - i,
407 			      "%s/%i\n", addr_str, ipatoe->mask_bits);
408 	}
409 	mutex_unlock(&card->ip_lock);
410 	i += snprintf(buf + i, PAGE_SIZE - i, "\n");
411 
412 	return i;
413 }
414 
415 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
416 				struct device_attribute *attr, char *buf)
417 {
418 	struct qeth_card *card = dev_get_drvdata(dev);
419 
420 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
421 }
422 
423 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
424 		  u8 *addr, int *mask_bits)
425 {
426 	const char *start, *end;
427 	char *tmp;
428 	char buffer[40] = {0, };
429 
430 	start = buf;
431 	/* get address string */
432 	end = strchr(start, '/');
433 	if (!end || (end - start >= 40)) {
434 		return -EINVAL;
435 	}
436 	strncpy(buffer, start, end - start);
437 	if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
438 		return -EINVAL;
439 	}
440 	start = end + 1;
441 	*mask_bits = simple_strtoul(start, &tmp, 10);
442 	if (!strlen(start) ||
443 	    (tmp == start) ||
444 	    (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
445 		return -EINVAL;
446 	}
447 	return 0;
448 }
449 
450 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
451 			 struct qeth_card *card, enum qeth_prot_versions proto)
452 {
453 	struct qeth_ipato_entry *ipatoe;
454 	u8 addr[16];
455 	int mask_bits;
456 	int rc = 0;
457 
458 	mutex_lock(&card->conf_mutex);
459 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
460 	if (rc)
461 		goto out;
462 
463 	ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
464 	if (!ipatoe) {
465 		rc = -ENOMEM;
466 		goto out;
467 	}
468 	ipatoe->proto = proto;
469 	memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
470 	ipatoe->mask_bits = mask_bits;
471 
472 	rc = qeth_l3_add_ipato_entry(card, ipatoe);
473 	if (rc)
474 		kfree(ipatoe);
475 out:
476 	mutex_unlock(&card->conf_mutex);
477 	return rc ? rc : count;
478 }
479 
480 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
481 		struct device_attribute *attr, const char *buf, size_t count)
482 {
483 	struct qeth_card *card = dev_get_drvdata(dev);
484 
485 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
486 }
487 
488 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
489 			qeth_l3_dev_ipato_add4_show,
490 			qeth_l3_dev_ipato_add4_store);
491 
492 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
493 			 struct qeth_card *card, enum qeth_prot_versions proto)
494 {
495 	u8 addr[16];
496 	int mask_bits;
497 	int rc = 0;
498 
499 	mutex_lock(&card->conf_mutex);
500 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
501 	if (!rc)
502 		rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
503 	mutex_unlock(&card->conf_mutex);
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 	char addr_str[40];
611 	int str_len = 0;
612 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
613 	int i;
614 
615 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
616 	entry_len += 2; /* \n + terminator */
617 	mutex_lock(&card->ip_lock);
618 	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
619 		if (ipaddr->proto != proto || ipaddr->type != type)
620 			continue;
621 		/* String must not be longer than PAGE_SIZE. So we check if
622 		 * string length gets near PAGE_SIZE. Then we can savely display
623 		 * the next IPv6 address (worst case, compared to IPv4) */
624 		if ((PAGE_SIZE - str_len) <= entry_len)
625 			break;
626 		qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
627 			addr_str);
628 		str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
629 				    addr_str);
630 	}
631 	mutex_unlock(&card->ip_lock);
632 	str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
633 
634 	return str_len;
635 }
636 
637 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
638 					  struct device_attribute *attr,
639 					  char *buf)
640 {
641 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
642 				       QETH_IP_TYPE_VIPA);
643 }
644 
645 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
646 		 u8 *addr)
647 {
648 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
649 		return -EINVAL;
650 	}
651 	return 0;
652 }
653 
654 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
655 			struct qeth_card *card, enum qeth_prot_versions proto)
656 {
657 	u8 addr[16] = {0, };
658 	int rc;
659 
660 	mutex_lock(&card->conf_mutex);
661 	rc = qeth_l3_parse_vipae(buf, proto, addr);
662 	if (!rc)
663 		rc = qeth_l3_modify_rxip_vipa(card, true, addr,
664 					      QETH_IP_TYPE_VIPA, proto);
665 	mutex_unlock(&card->conf_mutex);
666 	return rc ? rc : count;
667 }
668 
669 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
670 		struct device_attribute *attr, const char *buf, size_t count)
671 {
672 	struct qeth_card *card = dev_get_drvdata(dev);
673 
674 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
675 }
676 
677 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
678 			qeth_l3_dev_vipa_add4_show,
679 			qeth_l3_dev_vipa_add4_store);
680 
681 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
682 			 struct qeth_card *card, enum qeth_prot_versions proto)
683 {
684 	u8 addr[16];
685 	int rc;
686 
687 	mutex_lock(&card->conf_mutex);
688 	rc = qeth_l3_parse_vipae(buf, proto, addr);
689 	if (!rc)
690 		rc = qeth_l3_modify_rxip_vipa(card, false, addr,
691 					      QETH_IP_TYPE_VIPA, proto);
692 	mutex_unlock(&card->conf_mutex);
693 	return rc ? rc : count;
694 }
695 
696 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
697 		struct device_attribute *attr, const char *buf, size_t count)
698 {
699 	struct qeth_card *card = dev_get_drvdata(dev);
700 
701 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
702 }
703 
704 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
705 			qeth_l3_dev_vipa_del4_store);
706 
707 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
708 					  struct device_attribute *attr,
709 					  char *buf)
710 {
711 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
712 				       QETH_IP_TYPE_VIPA);
713 }
714 
715 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
716 		struct device_attribute *attr, const char *buf, size_t count)
717 {
718 	struct qeth_card *card = dev_get_drvdata(dev);
719 
720 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
721 }
722 
723 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
724 			qeth_l3_dev_vipa_add6_show,
725 			qeth_l3_dev_vipa_add6_store);
726 
727 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
728 		struct device_attribute *attr, const char *buf, size_t count)
729 {
730 	struct qeth_card *card = dev_get_drvdata(dev);
731 
732 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
733 }
734 
735 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
736 			qeth_l3_dev_vipa_del6_store);
737 
738 static struct attribute *qeth_vipa_device_attrs[] = {
739 	&dev_attr_vipa_add4.attr,
740 	&dev_attr_vipa_del4.attr,
741 	&dev_attr_vipa_add6.attr,
742 	&dev_attr_vipa_del6.attr,
743 	NULL,
744 };
745 
746 static const struct attribute_group qeth_device_vipa_group = {
747 	.name = "vipa",
748 	.attrs = qeth_vipa_device_attrs,
749 };
750 
751 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
752 					  struct device_attribute *attr,
753 					  char *buf)
754 {
755 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
756 				       QETH_IP_TYPE_RXIP);
757 }
758 
759 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
760 		 u8 *addr)
761 {
762 	__be32 ipv4_addr;
763 	struct in6_addr ipv6_addr;
764 
765 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
766 		return -EINVAL;
767 	}
768 	if (proto == QETH_PROT_IPV4) {
769 		memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
770 		if (ipv4_is_multicast(ipv4_addr)) {
771 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
772 			return -EINVAL;
773 		}
774 	} else if (proto == QETH_PROT_IPV6) {
775 		memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
776 		if (ipv6_addr_is_multicast(&ipv6_addr)) {
777 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
778 			return -EINVAL;
779 		}
780 	}
781 
782 	return 0;
783 }
784 
785 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
786 			struct qeth_card *card, enum qeth_prot_versions proto)
787 {
788 	u8 addr[16] = {0, };
789 	int rc;
790 
791 	mutex_lock(&card->conf_mutex);
792 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
793 	if (!rc)
794 		rc = qeth_l3_modify_rxip_vipa(card, true, addr,
795 					      QETH_IP_TYPE_RXIP, proto);
796 	mutex_unlock(&card->conf_mutex);
797 	return rc ? rc : count;
798 }
799 
800 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
801 		struct device_attribute *attr, const char *buf, size_t count)
802 {
803 	struct qeth_card *card = dev_get_drvdata(dev);
804 
805 	return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
806 }
807 
808 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
809 			qeth_l3_dev_rxip_add4_show,
810 			qeth_l3_dev_rxip_add4_store);
811 
812 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
813 			struct qeth_card *card, enum qeth_prot_versions proto)
814 {
815 	u8 addr[16];
816 	int rc;
817 
818 	mutex_lock(&card->conf_mutex);
819 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
820 	if (!rc)
821 		rc = qeth_l3_modify_rxip_vipa(card, false, addr,
822 					      QETH_IP_TYPE_RXIP, proto);
823 	mutex_unlock(&card->conf_mutex);
824 	return rc ? rc : count;
825 }
826 
827 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
828 		struct device_attribute *attr, const char *buf, size_t count)
829 {
830 	struct qeth_card *card = dev_get_drvdata(dev);
831 
832 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
833 }
834 
835 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
836 			qeth_l3_dev_rxip_del4_store);
837 
838 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
839 					  struct device_attribute *attr,
840 					  char *buf)
841 {
842 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
843 				       QETH_IP_TYPE_RXIP);
844 }
845 
846 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
847 		struct device_attribute *attr, const char *buf, size_t count)
848 {
849 	struct qeth_card *card = dev_get_drvdata(dev);
850 
851 	return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
852 }
853 
854 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
855 			qeth_l3_dev_rxip_add6_show,
856 			qeth_l3_dev_rxip_add6_store);
857 
858 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
859 		struct device_attribute *attr, const char *buf, size_t count)
860 {
861 	struct qeth_card *card = dev_get_drvdata(dev);
862 
863 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
864 }
865 
866 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
867 			qeth_l3_dev_rxip_del6_store);
868 
869 static struct attribute *qeth_rxip_device_attrs[] = {
870 	&dev_attr_rxip_add4.attr,
871 	&dev_attr_rxip_del4.attr,
872 	&dev_attr_rxip_add6.attr,
873 	&dev_attr_rxip_del6.attr,
874 	NULL,
875 };
876 
877 static const struct attribute_group qeth_device_rxip_group = {
878 	.name = "rxip",
879 	.attrs = qeth_rxip_device_attrs,
880 };
881 
882 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
883 	&qeth_l3_device_attr_group,
884 	&qeth_device_ipato_group,
885 	&qeth_device_vipa_group,
886 	&qeth_device_rxip_group,
887 	NULL,
888 };
889 
890 int qeth_l3_create_device_attributes(struct device *dev)
891 {
892 	return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
893 }
894 
895 void qeth_l3_remove_device_attributes(struct device *dev)
896 {
897 	sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
898 }
899 
900 const struct attribute_group *qeth_l3_attr_groups[] = {
901 	&qeth_device_attr_group,
902 	&qeth_device_blkt_group,
903 	/* l3 specific, see qeth_l3_only_attr_groups: */
904 	&qeth_l3_device_attr_group,
905 	&qeth_device_ipato_group,
906 	&qeth_device_vipa_group,
907 	&qeth_device_rxip_group,
908 	NULL,
909 };
910