xref: /openbmc/linux/drivers/s390/net/qeth_l3_sys.c (revision 0c874100)
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 	if (!card)
64 		return -EINVAL;
65 
66 	return qeth_l3_dev_route_show(card, &card->options.route4, buf);
67 }
68 
69 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card,
70 		struct qeth_routing_info *route, enum qeth_prot_versions prot,
71 		const char *buf, size_t count)
72 {
73 	enum qeth_routing_types old_route_type = route->type;
74 	int rc = 0;
75 
76 	mutex_lock(&card->conf_mutex);
77 	if (sysfs_streq(buf, "no_router")) {
78 		route->type = NO_ROUTER;
79 	} else if (sysfs_streq(buf, "primary_connector")) {
80 		route->type = PRIMARY_CONNECTOR;
81 	} else if (sysfs_streq(buf, "secondary_connector")) {
82 		route->type = SECONDARY_CONNECTOR;
83 	} else if (sysfs_streq(buf, "primary_router")) {
84 		route->type = PRIMARY_ROUTER;
85 	} else if (sysfs_streq(buf, "secondary_router")) {
86 		route->type = SECONDARY_ROUTER;
87 	} else if (sysfs_streq(buf, "multicast_router")) {
88 		route->type = MULTICAST_ROUTER;
89 	} else {
90 		rc = -EINVAL;
91 		goto out;
92 	}
93 	if (qeth_card_hw_is_reachable(card) &&
94 	    (old_route_type != route->type)) {
95 		if (prot == QETH_PROT_IPV4)
96 			rc = qeth_l3_setrouting_v4(card);
97 		else if (prot == QETH_PROT_IPV6)
98 			rc = qeth_l3_setrouting_v6(card);
99 	}
100 out:
101 	if (rc)
102 		route->type = old_route_type;
103 	mutex_unlock(&card->conf_mutex);
104 	return rc ? rc : count;
105 }
106 
107 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
108 		struct device_attribute *attr, const char *buf, size_t count)
109 {
110 	struct qeth_card *card = dev_get_drvdata(dev);
111 
112 	if (!card)
113 		return -EINVAL;
114 
115 	return qeth_l3_dev_route_store(card, &card->options.route4,
116 				QETH_PROT_IPV4, buf, count);
117 }
118 
119 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
120 			qeth_l3_dev_route4_store);
121 
122 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
123 			struct device_attribute *attr, char *buf)
124 {
125 	struct qeth_card *card = dev_get_drvdata(dev);
126 
127 	if (!card)
128 		return -EINVAL;
129 
130 	return qeth_l3_dev_route_show(card, &card->options.route6, buf);
131 }
132 
133 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
134 		struct device_attribute *attr, const char *buf, size_t count)
135 {
136 	struct qeth_card *card = dev_get_drvdata(dev);
137 
138 	if (!card)
139 		return -EINVAL;
140 
141 	return qeth_l3_dev_route_store(card, &card->options.route6,
142 				QETH_PROT_IPV6, buf, count);
143 }
144 
145 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
146 			qeth_l3_dev_route6_store);
147 
148 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
149 			struct device_attribute *attr, char *buf)
150 {
151 	struct qeth_card *card = dev_get_drvdata(dev);
152 
153 	if (!card)
154 		return -EINVAL;
155 
156 	return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
157 }
158 
159 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
160 		struct device_attribute *attr, const char *buf, size_t count)
161 {
162 	struct qeth_card *card = dev_get_drvdata(dev);
163 	char *tmp;
164 	int i, rc = 0;
165 
166 	if (!card)
167 		return -EINVAL;
168 
169 	mutex_lock(&card->conf_mutex);
170 	if ((card->state != CARD_STATE_DOWN) &&
171 	    (card->state != CARD_STATE_RECOVER)) {
172 		rc = -EPERM;
173 		goto out;
174 	}
175 
176 	i = simple_strtoul(buf, &tmp, 16);
177 	if ((i == 0) || (i == 1))
178 		card->options.fake_broadcast = i;
179 	else
180 		rc = -EINVAL;
181 out:
182 	mutex_unlock(&card->conf_mutex);
183 	return rc ? rc : count;
184 }
185 
186 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
187 		   qeth_l3_dev_fake_broadcast_store);
188 
189 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
190 		struct device_attribute *attr, char *buf)
191 {
192 	struct qeth_card *card = dev_get_drvdata(dev);
193 
194 	if (!card)
195 		return -EINVAL;
196 
197 	return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
198 }
199 
200 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
201 		struct device_attribute *attr, const char *buf, size_t count)
202 {
203 	struct qeth_card *card = dev_get_drvdata(dev);
204 	int rc = 0;
205 	unsigned long i;
206 
207 	if (!card)
208 		return -EINVAL;
209 
210 	if (card->info.type != QETH_CARD_TYPE_IQD)
211 		return -EPERM;
212 	if (card->options.cq == QETH_CQ_ENABLED)
213 		return -EPERM;
214 
215 	mutex_lock(&card->conf_mutex);
216 	if ((card->state != CARD_STATE_DOWN) &&
217 	    (card->state != CARD_STATE_RECOVER)) {
218 		rc = -EPERM;
219 		goto out;
220 	}
221 
222 	rc = kstrtoul(buf, 16, &i);
223 	if (rc) {
224 		rc = -EINVAL;
225 		goto out;
226 	}
227 	switch (i) {
228 	case 0:
229 		card->options.sniffer = i;
230 		break;
231 	case 1:
232 		qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd);
233 		if (card->ssqd.qdioac2 & QETH_SNIFF_AVAIL) {
234 			card->options.sniffer = i;
235 			if (card->qdio.init_pool.buf_count !=
236 					QETH_IN_BUF_COUNT_MAX)
237 				qeth_realloc_buffer_pool(card,
238 					QETH_IN_BUF_COUNT_MAX);
239 		} else
240 			rc = -EPERM;
241 		break;
242 	default:
243 		rc = -EINVAL;
244 	}
245 out:
246 	mutex_unlock(&card->conf_mutex);
247 	return rc ? rc : count;
248 }
249 
250 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
251 		qeth_l3_dev_sniffer_store);
252 
253 
254 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
255 		struct device_attribute *attr, char *buf)
256 {
257 	struct qeth_card *card = dev_get_drvdata(dev);
258 	char tmp_hsuid[9];
259 
260 	if (!card)
261 		return -EINVAL;
262 
263 	if (card->info.type != QETH_CARD_TYPE_IQD)
264 		return -EPERM;
265 
266 	memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
267 	EBCASC(tmp_hsuid, 8);
268 	return sprintf(buf, "%s\n", tmp_hsuid);
269 }
270 
271 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
272 		struct device_attribute *attr, const char *buf, size_t count)
273 {
274 	struct qeth_card *card = dev_get_drvdata(dev);
275 	char *tmp;
276 	int rc;
277 
278 	if (!card)
279 		return -EINVAL;
280 
281 	if (card->info.type != QETH_CARD_TYPE_IQD)
282 		return -EPERM;
283 	if (card->state != CARD_STATE_DOWN &&
284 	    card->state != CARD_STATE_RECOVER)
285 		return -EPERM;
286 	if (card->options.sniffer)
287 		return -EPERM;
288 	if (card->options.cq == QETH_CQ_NOTAVAILABLE)
289 		return -EPERM;
290 
291 	tmp = strsep((char **)&buf, "\n");
292 	if (strlen(tmp) > 8)
293 		return -EINVAL;
294 
295 	if (card->options.hsuid[0])
296 		/* delete old ip address */
297 		qeth_l3_modify_hsuid(card, false);
298 
299 	if (strlen(tmp) == 0) {
300 		/* delete ip address only */
301 		card->options.hsuid[0] = '\0';
302 		memcpy(card->dev->perm_addr, card->options.hsuid, 9);
303 		qeth_configure_cq(card, QETH_CQ_DISABLED);
304 		return count;
305 	}
306 
307 	if (qeth_configure_cq(card, QETH_CQ_ENABLED))
308 		return -EPERM;
309 
310 	snprintf(card->options.hsuid, sizeof(card->options.hsuid),
311 		 "%-8s", tmp);
312 	ASCEBC(card->options.hsuid, 8);
313 	memcpy(card->dev->perm_addr, card->options.hsuid, 9);
314 
315 	rc = qeth_l3_modify_hsuid(card, true);
316 
317 	return rc ? rc : count;
318 }
319 
320 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
321 		   qeth_l3_dev_hsuid_store);
322 
323 
324 static struct attribute *qeth_l3_device_attrs[] = {
325 	&dev_attr_route4.attr,
326 	&dev_attr_route6.attr,
327 	&dev_attr_fake_broadcast.attr,
328 	&dev_attr_sniffer.attr,
329 	&dev_attr_hsuid.attr,
330 	NULL,
331 };
332 
333 static const struct attribute_group qeth_l3_device_attr_group = {
334 	.attrs = qeth_l3_device_attrs,
335 };
336 
337 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
338 			struct device_attribute *attr, char *buf)
339 {
340 	struct qeth_card *card = dev_get_drvdata(dev);
341 
342 	if (!card)
343 		return -EINVAL;
344 
345 	return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
346 }
347 
348 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
349 		struct device_attribute *attr, const char *buf, size_t count)
350 {
351 	struct qeth_card *card = dev_get_drvdata(dev);
352 	bool enable;
353 	int rc = 0;
354 
355 	if (!card)
356 		return -EINVAL;
357 
358 	mutex_lock(&card->conf_mutex);
359 	if ((card->state != CARD_STATE_DOWN) &&
360 	    (card->state != CARD_STATE_RECOVER)) {
361 		rc = -EPERM;
362 		goto out;
363 	}
364 
365 	if (sysfs_streq(buf, "toggle")) {
366 		enable = !card->ipato.enabled;
367 	} else if (kstrtobool(buf, &enable)) {
368 		rc = -EINVAL;
369 		goto out;
370 	}
371 
372 	if (card->ipato.enabled != enable) {
373 		card->ipato.enabled = enable;
374 		spin_lock_bh(&card->ip_lock);
375 		qeth_l3_update_ipato(card);
376 		spin_unlock_bh(&card->ip_lock);
377 	}
378 out:
379 	mutex_unlock(&card->conf_mutex);
380 	return rc ? rc : count;
381 }
382 
383 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
384 			qeth_l3_dev_ipato_enable_show,
385 			qeth_l3_dev_ipato_enable_store);
386 
387 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
388 				struct device_attribute *attr, char *buf)
389 {
390 	struct qeth_card *card = dev_get_drvdata(dev);
391 
392 	if (!card)
393 		return -EINVAL;
394 
395 	return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
396 }
397 
398 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
399 				struct device_attribute *attr,
400 				const char *buf, size_t count)
401 {
402 	struct qeth_card *card = dev_get_drvdata(dev);
403 	bool invert;
404 	int rc = 0;
405 
406 	if (!card)
407 		return -EINVAL;
408 
409 	mutex_lock(&card->conf_mutex);
410 	if (sysfs_streq(buf, "toggle")) {
411 		invert = !card->ipato.invert4;
412 	} else if (kstrtobool(buf, &invert)) {
413 		rc = -EINVAL;
414 		goto out;
415 	}
416 
417 	if (card->ipato.invert4 != invert) {
418 		card->ipato.invert4 = invert;
419 		spin_lock_bh(&card->ip_lock);
420 		qeth_l3_update_ipato(card);
421 		spin_unlock_bh(&card->ip_lock);
422 	}
423 out:
424 	mutex_unlock(&card->conf_mutex);
425 	return rc ? rc : count;
426 }
427 
428 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
429 			qeth_l3_dev_ipato_invert4_show,
430 			qeth_l3_dev_ipato_invert4_store);
431 
432 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
433 			enum qeth_prot_versions proto)
434 {
435 	struct qeth_ipato_entry *ipatoe;
436 	char addr_str[40];
437 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
438 	int i = 0;
439 
440 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
441 	/* add strlen for "/<mask>\n" */
442 	entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
443 	spin_lock_bh(&card->ip_lock);
444 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
445 		if (ipatoe->proto != proto)
446 			continue;
447 		/* String must not be longer than PAGE_SIZE. So we check if
448 		 * string length gets near PAGE_SIZE. Then we can savely display
449 		 * the next IPv6 address (worst case, compared to IPv4) */
450 		if ((PAGE_SIZE - i) <= entry_len)
451 			break;
452 		qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
453 		i += snprintf(buf + i, PAGE_SIZE - i,
454 			      "%s/%i\n", addr_str, ipatoe->mask_bits);
455 	}
456 	spin_unlock_bh(&card->ip_lock);
457 	i += snprintf(buf + i, PAGE_SIZE - i, "\n");
458 
459 	return i;
460 }
461 
462 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
463 				struct device_attribute *attr, char *buf)
464 {
465 	struct qeth_card *card = dev_get_drvdata(dev);
466 
467 	if (!card)
468 		return -EINVAL;
469 
470 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
471 }
472 
473 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
474 		  u8 *addr, int *mask_bits)
475 {
476 	const char *start, *end;
477 	char *tmp;
478 	char buffer[40] = {0, };
479 
480 	start = buf;
481 	/* get address string */
482 	end = strchr(start, '/');
483 	if (!end || (end - start >= 40)) {
484 		return -EINVAL;
485 	}
486 	strncpy(buffer, start, end - start);
487 	if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
488 		return -EINVAL;
489 	}
490 	start = end + 1;
491 	*mask_bits = simple_strtoul(start, &tmp, 10);
492 	if (!strlen(start) ||
493 	    (tmp == start) ||
494 	    (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
495 		return -EINVAL;
496 	}
497 	return 0;
498 }
499 
500 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
501 			 struct qeth_card *card, enum qeth_prot_versions proto)
502 {
503 	struct qeth_ipato_entry *ipatoe;
504 	u8 addr[16];
505 	int mask_bits;
506 	int rc = 0;
507 
508 	mutex_lock(&card->conf_mutex);
509 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
510 	if (rc)
511 		goto out;
512 
513 	ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
514 	if (!ipatoe) {
515 		rc = -ENOMEM;
516 		goto out;
517 	}
518 	ipatoe->proto = proto;
519 	memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
520 	ipatoe->mask_bits = mask_bits;
521 
522 	rc = qeth_l3_add_ipato_entry(card, ipatoe);
523 	if (rc)
524 		kfree(ipatoe);
525 out:
526 	mutex_unlock(&card->conf_mutex);
527 	return rc ? rc : count;
528 }
529 
530 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
531 		struct device_attribute *attr, const char *buf, size_t count)
532 {
533 	struct qeth_card *card = dev_get_drvdata(dev);
534 
535 	if (!card)
536 		return -EINVAL;
537 
538 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
539 }
540 
541 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
542 			qeth_l3_dev_ipato_add4_show,
543 			qeth_l3_dev_ipato_add4_store);
544 
545 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
546 			 struct qeth_card *card, enum qeth_prot_versions proto)
547 {
548 	u8 addr[16];
549 	int mask_bits;
550 	int rc = 0;
551 
552 	mutex_lock(&card->conf_mutex);
553 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
554 	if (!rc)
555 		rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
556 	mutex_unlock(&card->conf_mutex);
557 	return rc ? rc : count;
558 }
559 
560 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
561 		struct device_attribute *attr, const char *buf, size_t count)
562 {
563 	struct qeth_card *card = dev_get_drvdata(dev);
564 
565 	if (!card)
566 		return -EINVAL;
567 
568 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
569 }
570 
571 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
572 			qeth_l3_dev_ipato_del4_store);
573 
574 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
575 		struct device_attribute *attr, char *buf)
576 {
577 	struct qeth_card *card = dev_get_drvdata(dev);
578 
579 	if (!card)
580 		return -EINVAL;
581 
582 	return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
583 }
584 
585 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
586 		struct device_attribute *attr, const char *buf, size_t count)
587 {
588 	struct qeth_card *card = dev_get_drvdata(dev);
589 	bool invert;
590 	int rc = 0;
591 
592 	if (!card)
593 		return -EINVAL;
594 
595 	mutex_lock(&card->conf_mutex);
596 	if (sysfs_streq(buf, "toggle")) {
597 		invert = !card->ipato.invert6;
598 	} else if (kstrtobool(buf, &invert)) {
599 		rc = -EINVAL;
600 		goto out;
601 	}
602 
603 	if (card->ipato.invert6 != invert) {
604 		card->ipato.invert6 = invert;
605 		spin_lock_bh(&card->ip_lock);
606 		qeth_l3_update_ipato(card);
607 		spin_unlock_bh(&card->ip_lock);
608 	}
609 out:
610 	mutex_unlock(&card->conf_mutex);
611 	return rc ? rc : count;
612 }
613 
614 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
615 			qeth_l3_dev_ipato_invert6_show,
616 			qeth_l3_dev_ipato_invert6_store);
617 
618 
619 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
620 				struct device_attribute *attr, char *buf)
621 {
622 	struct qeth_card *card = dev_get_drvdata(dev);
623 
624 	if (!card)
625 		return -EINVAL;
626 
627 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
628 }
629 
630 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
631 		struct device_attribute *attr, const char *buf, size_t count)
632 {
633 	struct qeth_card *card = dev_get_drvdata(dev);
634 
635 	if (!card)
636 		return -EINVAL;
637 
638 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
639 }
640 
641 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
642 			qeth_l3_dev_ipato_add6_show,
643 			qeth_l3_dev_ipato_add6_store);
644 
645 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
646 		struct device_attribute *attr, const char *buf, size_t count)
647 {
648 	struct qeth_card *card = dev_get_drvdata(dev);
649 
650 	if (!card)
651 		return -EINVAL;
652 
653 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
654 }
655 
656 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
657 			qeth_l3_dev_ipato_del6_store);
658 
659 static struct attribute *qeth_ipato_device_attrs[] = {
660 	&dev_attr_ipato_enable.attr,
661 	&dev_attr_ipato_invert4.attr,
662 	&dev_attr_ipato_add4.attr,
663 	&dev_attr_ipato_del4.attr,
664 	&dev_attr_ipato_invert6.attr,
665 	&dev_attr_ipato_add6.attr,
666 	&dev_attr_ipato_del6.attr,
667 	NULL,
668 };
669 
670 static const struct attribute_group qeth_device_ipato_group = {
671 	.name = "ipa_takeover",
672 	.attrs = qeth_ipato_device_attrs,
673 };
674 
675 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
676 				       enum qeth_prot_versions proto,
677 				       enum qeth_ip_types type)
678 {
679 	struct qeth_card *card = dev_get_drvdata(dev);
680 	struct qeth_ipaddr *ipaddr;
681 	char addr_str[40];
682 	int str_len = 0;
683 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
684 	int i;
685 
686 	if (!card)
687 		return -EINVAL;
688 
689 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
690 	entry_len += 2; /* \n + terminator */
691 	spin_lock_bh(&card->ip_lock);
692 	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
693 		if (ipaddr->proto != proto || ipaddr->type != type)
694 			continue;
695 		/* String must not be longer than PAGE_SIZE. So we check if
696 		 * string length gets near PAGE_SIZE. Then we can savely display
697 		 * the next IPv6 address (worst case, compared to IPv4) */
698 		if ((PAGE_SIZE - str_len) <= entry_len)
699 			break;
700 		qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
701 			addr_str);
702 		str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
703 				    addr_str);
704 	}
705 	spin_unlock_bh(&card->ip_lock);
706 	str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
707 
708 	return str_len;
709 }
710 
711 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
712 					  struct device_attribute *attr,
713 					  char *buf)
714 {
715 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
716 				       QETH_IP_TYPE_VIPA);
717 }
718 
719 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
720 		 u8 *addr)
721 {
722 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
723 		return -EINVAL;
724 	}
725 	return 0;
726 }
727 
728 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
729 			struct qeth_card *card, enum qeth_prot_versions proto)
730 {
731 	u8 addr[16] = {0, };
732 	int rc;
733 
734 	mutex_lock(&card->conf_mutex);
735 	rc = qeth_l3_parse_vipae(buf, proto, addr);
736 	if (!rc)
737 		rc = qeth_l3_modify_rxip_vipa(card, true, addr,
738 					      QETH_IP_TYPE_VIPA, proto);
739 	mutex_unlock(&card->conf_mutex);
740 	return rc ? rc : count;
741 }
742 
743 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
744 		struct device_attribute *attr, const char *buf, size_t count)
745 {
746 	struct qeth_card *card = dev_get_drvdata(dev);
747 
748 	if (!card)
749 		return -EINVAL;
750 
751 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
752 }
753 
754 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
755 			qeth_l3_dev_vipa_add4_show,
756 			qeth_l3_dev_vipa_add4_store);
757 
758 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
759 			 struct qeth_card *card, enum qeth_prot_versions proto)
760 {
761 	u8 addr[16];
762 	int rc;
763 
764 	mutex_lock(&card->conf_mutex);
765 	rc = qeth_l3_parse_vipae(buf, proto, addr);
766 	if (!rc)
767 		rc = qeth_l3_modify_rxip_vipa(card, false, addr,
768 					      QETH_IP_TYPE_VIPA, proto);
769 	mutex_unlock(&card->conf_mutex);
770 	return rc ? rc : count;
771 }
772 
773 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
774 		struct device_attribute *attr, const char *buf, size_t count)
775 {
776 	struct qeth_card *card = dev_get_drvdata(dev);
777 
778 	if (!card)
779 		return -EINVAL;
780 
781 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
782 }
783 
784 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
785 			qeth_l3_dev_vipa_del4_store);
786 
787 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
788 					  struct device_attribute *attr,
789 					  char *buf)
790 {
791 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
792 				       QETH_IP_TYPE_VIPA);
793 }
794 
795 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
796 		struct device_attribute *attr, const char *buf, size_t count)
797 {
798 	struct qeth_card *card = dev_get_drvdata(dev);
799 
800 	if (!card)
801 		return -EINVAL;
802 
803 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
804 }
805 
806 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
807 			qeth_l3_dev_vipa_add6_show,
808 			qeth_l3_dev_vipa_add6_store);
809 
810 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
811 		struct device_attribute *attr, const char *buf, size_t count)
812 {
813 	struct qeth_card *card = dev_get_drvdata(dev);
814 
815 	if (!card)
816 		return -EINVAL;
817 
818 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
819 }
820 
821 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
822 			qeth_l3_dev_vipa_del6_store);
823 
824 static struct attribute *qeth_vipa_device_attrs[] = {
825 	&dev_attr_vipa_add4.attr,
826 	&dev_attr_vipa_del4.attr,
827 	&dev_attr_vipa_add6.attr,
828 	&dev_attr_vipa_del6.attr,
829 	NULL,
830 };
831 
832 static const struct attribute_group qeth_device_vipa_group = {
833 	.name = "vipa",
834 	.attrs = qeth_vipa_device_attrs,
835 };
836 
837 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
838 					  struct device_attribute *attr,
839 					  char *buf)
840 {
841 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
842 				       QETH_IP_TYPE_RXIP);
843 }
844 
845 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
846 		 u8 *addr)
847 {
848 	__be32 ipv4_addr;
849 	struct in6_addr ipv6_addr;
850 
851 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
852 		return -EINVAL;
853 	}
854 	if (proto == QETH_PROT_IPV4) {
855 		memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
856 		if (ipv4_is_multicast(ipv4_addr)) {
857 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
858 			return -EINVAL;
859 		}
860 	} else if (proto == QETH_PROT_IPV6) {
861 		memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
862 		if (ipv6_addr_is_multicast(&ipv6_addr)) {
863 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
864 			return -EINVAL;
865 		}
866 	}
867 
868 	return 0;
869 }
870 
871 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
872 			struct qeth_card *card, enum qeth_prot_versions proto)
873 {
874 	u8 addr[16] = {0, };
875 	int rc;
876 
877 	mutex_lock(&card->conf_mutex);
878 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
879 	if (!rc)
880 		rc = qeth_l3_modify_rxip_vipa(card, true, addr,
881 					      QETH_IP_TYPE_RXIP, proto);
882 	mutex_unlock(&card->conf_mutex);
883 	return rc ? rc : count;
884 }
885 
886 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
887 		struct device_attribute *attr, const char *buf, size_t count)
888 {
889 	struct qeth_card *card = dev_get_drvdata(dev);
890 
891 	if (!card)
892 		return -EINVAL;
893 
894 	return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
895 }
896 
897 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
898 			qeth_l3_dev_rxip_add4_show,
899 			qeth_l3_dev_rxip_add4_store);
900 
901 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
902 			struct qeth_card *card, enum qeth_prot_versions proto)
903 {
904 	u8 addr[16];
905 	int rc;
906 
907 	mutex_lock(&card->conf_mutex);
908 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
909 	if (!rc)
910 		rc = qeth_l3_modify_rxip_vipa(card, false, addr,
911 					      QETH_IP_TYPE_RXIP, proto);
912 	mutex_unlock(&card->conf_mutex);
913 	return rc ? rc : count;
914 }
915 
916 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
917 		struct device_attribute *attr, const char *buf, size_t count)
918 {
919 	struct qeth_card *card = dev_get_drvdata(dev);
920 
921 	if (!card)
922 		return -EINVAL;
923 
924 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
925 }
926 
927 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
928 			qeth_l3_dev_rxip_del4_store);
929 
930 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
931 					  struct device_attribute *attr,
932 					  char *buf)
933 {
934 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
935 				       QETH_IP_TYPE_RXIP);
936 }
937 
938 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
939 		struct device_attribute *attr, const char *buf, size_t count)
940 {
941 	struct qeth_card *card = dev_get_drvdata(dev);
942 
943 	if (!card)
944 		return -EINVAL;
945 
946 	return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
947 }
948 
949 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
950 			qeth_l3_dev_rxip_add6_show,
951 			qeth_l3_dev_rxip_add6_store);
952 
953 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
954 		struct device_attribute *attr, const char *buf, size_t count)
955 {
956 	struct qeth_card *card = dev_get_drvdata(dev);
957 
958 	if (!card)
959 		return -EINVAL;
960 
961 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
962 }
963 
964 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
965 			qeth_l3_dev_rxip_del6_store);
966 
967 static struct attribute *qeth_rxip_device_attrs[] = {
968 	&dev_attr_rxip_add4.attr,
969 	&dev_attr_rxip_del4.attr,
970 	&dev_attr_rxip_add6.attr,
971 	&dev_attr_rxip_del6.attr,
972 	NULL,
973 };
974 
975 static const struct attribute_group qeth_device_rxip_group = {
976 	.name = "rxip",
977 	.attrs = qeth_rxip_device_attrs,
978 };
979 
980 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
981 	&qeth_l3_device_attr_group,
982 	&qeth_device_ipato_group,
983 	&qeth_device_vipa_group,
984 	&qeth_device_rxip_group,
985 	NULL,
986 };
987 
988 int qeth_l3_create_device_attributes(struct device *dev)
989 {
990 	return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
991 }
992 
993 void qeth_l3_remove_device_attributes(struct device *dev)
994 {
995 	sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
996 }
997 
998 const struct attribute_group *qeth_l3_attr_groups[] = {
999 	&qeth_device_attr_group,
1000 	&qeth_device_blkt_group,
1001 	/* l3 specific, see qeth_l3_only_attr_groups: */
1002 	&qeth_l3_device_attr_group,
1003 	&qeth_device_ipato_group,
1004 	&qeth_device_vipa_group,
1005 	&qeth_device_rxip_group,
1006 	NULL,
1007 };
1008