xref: /openbmc/linux/drivers/s390/net/qeth_l3_sys.c (revision d2073ef3)
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 	struct qeth_ipaddr *addr;
276 	char *tmp;
277 	int rc, i;
278 
279 	if (!card)
280 		return -EINVAL;
281 
282 	if (card->info.type != QETH_CARD_TYPE_IQD)
283 		return -EPERM;
284 	if (card->state != CARD_STATE_DOWN &&
285 	    card->state != CARD_STATE_RECOVER)
286 		return -EPERM;
287 	if (card->options.sniffer)
288 		return -EPERM;
289 	if (card->options.cq == QETH_CQ_NOTAVAILABLE)
290 		return -EPERM;
291 
292 	tmp = strsep((char **)&buf, "\n");
293 	if (strlen(tmp) > 8)
294 		return -EINVAL;
295 
296 	if (card->options.hsuid[0]) {
297 		/* delete old ip address */
298 		addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
299 		if (!addr)
300 			return -ENOMEM;
301 
302 		addr->u.a6.addr.s6_addr32[0] = cpu_to_be32(0xfe800000);
303 		addr->u.a6.addr.s6_addr32[1] = 0x00000000;
304 		for (i = 8; i < 16; i++)
305 			addr->u.a6.addr.s6_addr[i] =
306 				card->options.hsuid[i - 8];
307 		addr->u.a6.pfxlen = 0;
308 		addr->type = QETH_IP_TYPE_NORMAL;
309 
310 		spin_lock_bh(&card->ip_lock);
311 		qeth_l3_delete_ip(card, addr);
312 		spin_unlock_bh(&card->ip_lock);
313 		kfree(addr);
314 	}
315 
316 	if (strlen(tmp) == 0) {
317 		/* delete ip address only */
318 		card->options.hsuid[0] = '\0';
319 		if (card->dev)
320 			memcpy(card->dev->perm_addr, card->options.hsuid, 9);
321 		qeth_configure_cq(card, QETH_CQ_DISABLED);
322 		return count;
323 	}
324 
325 	if (qeth_configure_cq(card, QETH_CQ_ENABLED))
326 		return -EPERM;
327 
328 	snprintf(card->options.hsuid, sizeof(card->options.hsuid),
329 		 "%-8s", tmp);
330 	ASCEBC(card->options.hsuid, 8);
331 	if (card->dev)
332 		memcpy(card->dev->perm_addr, card->options.hsuid, 9);
333 
334 	addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
335 	if (addr != NULL) {
336 		addr->u.a6.addr.s6_addr32[0] = cpu_to_be32(0xfe800000);
337 		addr->u.a6.addr.s6_addr32[1] = 0x00000000;
338 		for (i = 8; i < 16; i++)
339 			addr->u.a6.addr.s6_addr[i] = card->options.hsuid[i - 8];
340 		addr->u.a6.pfxlen = 0;
341 		addr->type = QETH_IP_TYPE_NORMAL;
342 	} else
343 		return -ENOMEM;
344 
345 	spin_lock_bh(&card->ip_lock);
346 	rc = qeth_l3_add_ip(card, addr);
347 	spin_unlock_bh(&card->ip_lock);
348 	kfree(addr);
349 
350 	return rc ? rc : count;
351 }
352 
353 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
354 		   qeth_l3_dev_hsuid_store);
355 
356 
357 static struct attribute *qeth_l3_device_attrs[] = {
358 	&dev_attr_route4.attr,
359 	&dev_attr_route6.attr,
360 	&dev_attr_fake_broadcast.attr,
361 	&dev_attr_sniffer.attr,
362 	&dev_attr_hsuid.attr,
363 	NULL,
364 };
365 
366 static const struct attribute_group qeth_l3_device_attr_group = {
367 	.attrs = qeth_l3_device_attrs,
368 };
369 
370 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
371 			struct device_attribute *attr, char *buf)
372 {
373 	struct qeth_card *card = dev_get_drvdata(dev);
374 
375 	if (!card)
376 		return -EINVAL;
377 
378 	return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
379 }
380 
381 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
382 		struct device_attribute *attr, const char *buf, size_t count)
383 {
384 	struct qeth_card *card = dev_get_drvdata(dev);
385 	bool enable;
386 	int rc = 0;
387 
388 	if (!card)
389 		return -EINVAL;
390 
391 	mutex_lock(&card->conf_mutex);
392 	if ((card->state != CARD_STATE_DOWN) &&
393 	    (card->state != CARD_STATE_RECOVER)) {
394 		rc = -EPERM;
395 		goto out;
396 	}
397 
398 	if (sysfs_streq(buf, "toggle")) {
399 		enable = !card->ipato.enabled;
400 	} else if (kstrtobool(buf, &enable)) {
401 		rc = -EINVAL;
402 		goto out;
403 	}
404 
405 	if (card->ipato.enabled != enable) {
406 		card->ipato.enabled = enable;
407 		spin_lock_bh(&card->ip_lock);
408 		qeth_l3_update_ipato(card);
409 		spin_unlock_bh(&card->ip_lock);
410 	}
411 out:
412 	mutex_unlock(&card->conf_mutex);
413 	return rc ? rc : count;
414 }
415 
416 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
417 			qeth_l3_dev_ipato_enable_show,
418 			qeth_l3_dev_ipato_enable_store);
419 
420 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
421 				struct device_attribute *attr, char *buf)
422 {
423 	struct qeth_card *card = dev_get_drvdata(dev);
424 
425 	if (!card)
426 		return -EINVAL;
427 
428 	return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
429 }
430 
431 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev,
432 				struct device_attribute *attr,
433 				const char *buf, size_t count)
434 {
435 	struct qeth_card *card = dev_get_drvdata(dev);
436 	bool invert;
437 	int rc = 0;
438 
439 	if (!card)
440 		return -EINVAL;
441 
442 	mutex_lock(&card->conf_mutex);
443 	if (sysfs_streq(buf, "toggle")) {
444 		invert = !card->ipato.invert4;
445 	} else if (kstrtobool(buf, &invert)) {
446 		rc = -EINVAL;
447 		goto out;
448 	}
449 
450 	if (card->ipato.invert4 != invert) {
451 		card->ipato.invert4 = invert;
452 		spin_lock_bh(&card->ip_lock);
453 		qeth_l3_update_ipato(card);
454 		spin_unlock_bh(&card->ip_lock);
455 	}
456 out:
457 	mutex_unlock(&card->conf_mutex);
458 	return rc ? rc : count;
459 }
460 
461 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
462 			qeth_l3_dev_ipato_invert4_show,
463 			qeth_l3_dev_ipato_invert4_store);
464 
465 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
466 			enum qeth_prot_versions proto)
467 {
468 	struct qeth_ipato_entry *ipatoe;
469 	char addr_str[40];
470 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
471 	int i = 0;
472 
473 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
474 	/* add strlen for "/<mask>\n" */
475 	entry_len += (proto == QETH_PROT_IPV4)? 5 : 6;
476 	spin_lock_bh(&card->ip_lock);
477 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
478 		if (ipatoe->proto != proto)
479 			continue;
480 		/* String must not be longer than PAGE_SIZE. So we check if
481 		 * string length gets near PAGE_SIZE. Then we can savely display
482 		 * the next IPv6 address (worst case, compared to IPv4) */
483 		if ((PAGE_SIZE - i) <= entry_len)
484 			break;
485 		qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str);
486 		i += snprintf(buf + i, PAGE_SIZE - i,
487 			      "%s/%i\n", addr_str, ipatoe->mask_bits);
488 	}
489 	spin_unlock_bh(&card->ip_lock);
490 	i += snprintf(buf + i, PAGE_SIZE - i, "\n");
491 
492 	return i;
493 }
494 
495 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
496 				struct device_attribute *attr, char *buf)
497 {
498 	struct qeth_card *card = dev_get_drvdata(dev);
499 
500 	if (!card)
501 		return -EINVAL;
502 
503 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
504 }
505 
506 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
507 		  u8 *addr, int *mask_bits)
508 {
509 	const char *start, *end;
510 	char *tmp;
511 	char buffer[40] = {0, };
512 
513 	start = buf;
514 	/* get address string */
515 	end = strchr(start, '/');
516 	if (!end || (end - start >= 40)) {
517 		return -EINVAL;
518 	}
519 	strncpy(buffer, start, end - start);
520 	if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
521 		return -EINVAL;
522 	}
523 	start = end + 1;
524 	*mask_bits = simple_strtoul(start, &tmp, 10);
525 	if (!strlen(start) ||
526 	    (tmp == start) ||
527 	    (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) {
528 		return -EINVAL;
529 	}
530 	return 0;
531 }
532 
533 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count,
534 			 struct qeth_card *card, enum qeth_prot_versions proto)
535 {
536 	struct qeth_ipato_entry *ipatoe;
537 	u8 addr[16];
538 	int mask_bits;
539 	int rc = 0;
540 
541 	mutex_lock(&card->conf_mutex);
542 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
543 	if (rc)
544 		goto out;
545 
546 	ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
547 	if (!ipatoe) {
548 		rc = -ENOMEM;
549 		goto out;
550 	}
551 	ipatoe->proto = proto;
552 	memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
553 	ipatoe->mask_bits = mask_bits;
554 
555 	rc = qeth_l3_add_ipato_entry(card, ipatoe);
556 	if (rc)
557 		kfree(ipatoe);
558 out:
559 	mutex_unlock(&card->conf_mutex);
560 	return rc ? rc : count;
561 }
562 
563 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
564 		struct device_attribute *attr, const char *buf, size_t count)
565 {
566 	struct qeth_card *card = dev_get_drvdata(dev);
567 
568 	if (!card)
569 		return -EINVAL;
570 
571 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
572 }
573 
574 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
575 			qeth_l3_dev_ipato_add4_show,
576 			qeth_l3_dev_ipato_add4_store);
577 
578 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count,
579 			 struct qeth_card *card, enum qeth_prot_versions proto)
580 {
581 	u8 addr[16];
582 	int mask_bits;
583 	int rc = 0;
584 
585 	mutex_lock(&card->conf_mutex);
586 	rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
587 	if (!rc)
588 		rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits);
589 	mutex_unlock(&card->conf_mutex);
590 	return rc ? rc : count;
591 }
592 
593 static ssize_t qeth_l3_dev_ipato_del4_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 	if (!card)
599 		return -EINVAL;
600 
601 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
602 }
603 
604 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
605 			qeth_l3_dev_ipato_del4_store);
606 
607 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
608 		struct device_attribute *attr, char *buf)
609 {
610 	struct qeth_card *card = dev_get_drvdata(dev);
611 
612 	if (!card)
613 		return -EINVAL;
614 
615 	return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
616 }
617 
618 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
619 		struct device_attribute *attr, const char *buf, size_t count)
620 {
621 	struct qeth_card *card = dev_get_drvdata(dev);
622 	bool invert;
623 	int rc = 0;
624 
625 	if (!card)
626 		return -EINVAL;
627 
628 	mutex_lock(&card->conf_mutex);
629 	if (sysfs_streq(buf, "toggle")) {
630 		invert = !card->ipato.invert6;
631 	} else if (kstrtobool(buf, &invert)) {
632 		rc = -EINVAL;
633 		goto out;
634 	}
635 
636 	if (card->ipato.invert6 != invert) {
637 		card->ipato.invert6 = invert;
638 		spin_lock_bh(&card->ip_lock);
639 		qeth_l3_update_ipato(card);
640 		spin_unlock_bh(&card->ip_lock);
641 	}
642 out:
643 	mutex_unlock(&card->conf_mutex);
644 	return rc ? rc : count;
645 }
646 
647 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
648 			qeth_l3_dev_ipato_invert6_show,
649 			qeth_l3_dev_ipato_invert6_store);
650 
651 
652 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
653 				struct device_attribute *attr, char *buf)
654 {
655 	struct qeth_card *card = dev_get_drvdata(dev);
656 
657 	if (!card)
658 		return -EINVAL;
659 
660 	return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
661 }
662 
663 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
664 		struct device_attribute *attr, const char *buf, size_t count)
665 {
666 	struct qeth_card *card = dev_get_drvdata(dev);
667 
668 	if (!card)
669 		return -EINVAL;
670 
671 	return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
672 }
673 
674 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
675 			qeth_l3_dev_ipato_add6_show,
676 			qeth_l3_dev_ipato_add6_store);
677 
678 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
679 		struct device_attribute *attr, const char *buf, size_t count)
680 {
681 	struct qeth_card *card = dev_get_drvdata(dev);
682 
683 	if (!card)
684 		return -EINVAL;
685 
686 	return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
687 }
688 
689 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
690 			qeth_l3_dev_ipato_del6_store);
691 
692 static struct attribute *qeth_ipato_device_attrs[] = {
693 	&dev_attr_ipato_enable.attr,
694 	&dev_attr_ipato_invert4.attr,
695 	&dev_attr_ipato_add4.attr,
696 	&dev_attr_ipato_del4.attr,
697 	&dev_attr_ipato_invert6.attr,
698 	&dev_attr_ipato_add6.attr,
699 	&dev_attr_ipato_del6.attr,
700 	NULL,
701 };
702 
703 static const struct attribute_group qeth_device_ipato_group = {
704 	.name = "ipa_takeover",
705 	.attrs = qeth_ipato_device_attrs,
706 };
707 
708 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf,
709 				       enum qeth_prot_versions proto,
710 				       enum qeth_ip_types type)
711 {
712 	struct qeth_card *card = dev_get_drvdata(dev);
713 	struct qeth_ipaddr *ipaddr;
714 	char addr_str[40];
715 	int str_len = 0;
716 	int entry_len; /* length of 1 entry string, differs between v4 and v6 */
717 	int i;
718 
719 	if (!card)
720 		return -EINVAL;
721 
722 	entry_len = (proto == QETH_PROT_IPV4)? 12 : 40;
723 	entry_len += 2; /* \n + terminator */
724 	spin_lock_bh(&card->ip_lock);
725 	hash_for_each(card->ip_htable, i, ipaddr, hnode) {
726 		if (ipaddr->proto != proto || ipaddr->type != type)
727 			continue;
728 		/* String must not be longer than PAGE_SIZE. So we check if
729 		 * string length gets near PAGE_SIZE. Then we can savely display
730 		 * the next IPv6 address (worst case, compared to IPv4) */
731 		if ((PAGE_SIZE - str_len) <= entry_len)
732 			break;
733 		qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u,
734 			addr_str);
735 		str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n",
736 				    addr_str);
737 	}
738 	spin_unlock_bh(&card->ip_lock);
739 	str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
740 
741 	return str_len;
742 }
743 
744 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
745 					  struct device_attribute *attr,
746 					  char *buf)
747 {
748 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
749 				       QETH_IP_TYPE_VIPA);
750 }
751 
752 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
753 		 u8 *addr)
754 {
755 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
756 		return -EINVAL;
757 	}
758 	return 0;
759 }
760 
761 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count,
762 			struct qeth_card *card, enum qeth_prot_versions proto)
763 {
764 	u8 addr[16] = {0, };
765 	int rc;
766 
767 	mutex_lock(&card->conf_mutex);
768 	rc = qeth_l3_parse_vipae(buf, proto, addr);
769 	if (!rc)
770 		rc = qeth_l3_add_vipa(card, proto, addr);
771 	mutex_unlock(&card->conf_mutex);
772 	return rc ? rc : count;
773 }
774 
775 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
776 		struct device_attribute *attr, const char *buf, size_t count)
777 {
778 	struct qeth_card *card = dev_get_drvdata(dev);
779 
780 	if (!card)
781 		return -EINVAL;
782 
783 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
784 }
785 
786 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
787 			qeth_l3_dev_vipa_add4_show,
788 			qeth_l3_dev_vipa_add4_store);
789 
790 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count,
791 			 struct qeth_card *card, enum qeth_prot_versions proto)
792 {
793 	u8 addr[16];
794 	int rc;
795 
796 	mutex_lock(&card->conf_mutex);
797 	rc = qeth_l3_parse_vipae(buf, proto, addr);
798 	if (!rc)
799 		rc = qeth_l3_del_vipa(card, proto, addr);
800 	mutex_unlock(&card->conf_mutex);
801 	return rc ? rc : count;
802 }
803 
804 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
805 		struct device_attribute *attr, const char *buf, size_t count)
806 {
807 	struct qeth_card *card = dev_get_drvdata(dev);
808 
809 	if (!card)
810 		return -EINVAL;
811 
812 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
813 }
814 
815 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
816 			qeth_l3_dev_vipa_del4_store);
817 
818 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
819 					  struct device_attribute *attr,
820 					  char *buf)
821 {
822 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
823 				       QETH_IP_TYPE_VIPA);
824 }
825 
826 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
827 		struct device_attribute *attr, const char *buf, size_t count)
828 {
829 	struct qeth_card *card = dev_get_drvdata(dev);
830 
831 	if (!card)
832 		return -EINVAL;
833 
834 	return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
835 }
836 
837 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
838 			qeth_l3_dev_vipa_add6_show,
839 			qeth_l3_dev_vipa_add6_store);
840 
841 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
842 		struct device_attribute *attr, const char *buf, size_t count)
843 {
844 	struct qeth_card *card = dev_get_drvdata(dev);
845 
846 	if (!card)
847 		return -EINVAL;
848 
849 	return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
850 }
851 
852 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
853 			qeth_l3_dev_vipa_del6_store);
854 
855 static struct attribute *qeth_vipa_device_attrs[] = {
856 	&dev_attr_vipa_add4.attr,
857 	&dev_attr_vipa_del4.attr,
858 	&dev_attr_vipa_add6.attr,
859 	&dev_attr_vipa_del6.attr,
860 	NULL,
861 };
862 
863 static const struct attribute_group qeth_device_vipa_group = {
864 	.name = "vipa",
865 	.attrs = qeth_vipa_device_attrs,
866 };
867 
868 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
869 					  struct device_attribute *attr,
870 					  char *buf)
871 {
872 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
873 				       QETH_IP_TYPE_RXIP);
874 }
875 
876 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
877 		 u8 *addr)
878 {
879 	__be32 ipv4_addr;
880 	struct in6_addr ipv6_addr;
881 
882 	if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
883 		return -EINVAL;
884 	}
885 	if (proto == QETH_PROT_IPV4) {
886 		memcpy(&ipv4_addr, addr, sizeof(ipv4_addr));
887 		if (ipv4_is_multicast(ipv4_addr)) {
888 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
889 			return -EINVAL;
890 		}
891 	} else if (proto == QETH_PROT_IPV6) {
892 		memcpy(&ipv6_addr, addr, sizeof(ipv6_addr));
893 		if (ipv6_addr_is_multicast(&ipv6_addr)) {
894 			QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n");
895 			return -EINVAL;
896 		}
897 	}
898 
899 	return 0;
900 }
901 
902 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count,
903 			struct qeth_card *card, enum qeth_prot_versions proto)
904 {
905 	u8 addr[16] = {0, };
906 	int rc;
907 
908 	mutex_lock(&card->conf_mutex);
909 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
910 	if (!rc)
911 		rc = qeth_l3_add_rxip(card, proto, addr);
912 	mutex_unlock(&card->conf_mutex);
913 	return rc ? rc : count;
914 }
915 
916 static ssize_t qeth_l3_dev_rxip_add4_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_add_store(buf, count, card, QETH_PROT_IPV4);
925 }
926 
927 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
928 			qeth_l3_dev_rxip_add4_show,
929 			qeth_l3_dev_rxip_add4_store);
930 
931 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count,
932 			struct qeth_card *card, enum qeth_prot_versions proto)
933 {
934 	u8 addr[16];
935 	int rc;
936 
937 	mutex_lock(&card->conf_mutex);
938 	rc = qeth_l3_parse_rxipe(buf, proto, addr);
939 	if (!rc)
940 		rc = qeth_l3_del_rxip(card, proto, addr);
941 	mutex_unlock(&card->conf_mutex);
942 	return rc ? rc : count;
943 }
944 
945 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
946 		struct device_attribute *attr, const char *buf, size_t count)
947 {
948 	struct qeth_card *card = dev_get_drvdata(dev);
949 
950 	if (!card)
951 		return -EINVAL;
952 
953 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
954 }
955 
956 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
957 			qeth_l3_dev_rxip_del4_store);
958 
959 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
960 					  struct device_attribute *attr,
961 					  char *buf)
962 {
963 	return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
964 				       QETH_IP_TYPE_RXIP);
965 }
966 
967 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
968 		struct device_attribute *attr, const char *buf, size_t count)
969 {
970 	struct qeth_card *card = dev_get_drvdata(dev);
971 
972 	if (!card)
973 		return -EINVAL;
974 
975 	return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
976 }
977 
978 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
979 			qeth_l3_dev_rxip_add6_show,
980 			qeth_l3_dev_rxip_add6_store);
981 
982 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
983 		struct device_attribute *attr, const char *buf, size_t count)
984 {
985 	struct qeth_card *card = dev_get_drvdata(dev);
986 
987 	if (!card)
988 		return -EINVAL;
989 
990 	return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
991 }
992 
993 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
994 			qeth_l3_dev_rxip_del6_store);
995 
996 static struct attribute *qeth_rxip_device_attrs[] = {
997 	&dev_attr_rxip_add4.attr,
998 	&dev_attr_rxip_del4.attr,
999 	&dev_attr_rxip_add6.attr,
1000 	&dev_attr_rxip_del6.attr,
1001 	NULL,
1002 };
1003 
1004 static const struct attribute_group qeth_device_rxip_group = {
1005 	.name = "rxip",
1006 	.attrs = qeth_rxip_device_attrs,
1007 };
1008 
1009 static const struct attribute_group *qeth_l3_only_attr_groups[] = {
1010 	&qeth_l3_device_attr_group,
1011 	&qeth_device_ipato_group,
1012 	&qeth_device_vipa_group,
1013 	&qeth_device_rxip_group,
1014 	NULL,
1015 };
1016 
1017 int qeth_l3_create_device_attributes(struct device *dev)
1018 {
1019 	return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
1020 }
1021 
1022 void qeth_l3_remove_device_attributes(struct device *dev)
1023 {
1024 	sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
1025 }
1026 
1027 const struct attribute_group *qeth_l3_attr_groups[] = {
1028 	&qeth_device_attr_group,
1029 	&qeth_device_blkt_group,
1030 	/* l3 specific, see qeth_l3_only_attr_groups: */
1031 	&qeth_l3_device_attr_group,
1032 	&qeth_device_ipato_group,
1033 	&qeth_device_vipa_group,
1034 	&qeth_device_rxip_group,
1035 	NULL,
1036 };
1037