Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / s390 / net / qeth_l3_sys.c
bloba645cfe66ddf724b22e9691467768d44bd620dc5
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 */
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"
16 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \
17 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store)
19 static int qeth_l3_string_to_ipaddr(const char *buf,
20 enum qeth_prot_versions proto, u8 *addr)
22 const char *end;
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;
30 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card,
31 struct qeth_routing_info *route, char *buf)
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");
58 static ssize_t qeth_l3_dev_route4_show(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 struct qeth_card *card = dev_get_drvdata(dev);
63 if (!card)
64 return -EINVAL;
66 return qeth_l3_dev_route_show(card, &card->options.route4, buf);
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)
73 enum qeth_routing_types old_route_type = route->type;
74 int rc = 0;
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;
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);
100 out:
101 if (rc)
102 route->type = old_route_type;
103 mutex_unlock(&card->conf_mutex);
104 return rc ? rc : count;
107 static ssize_t qeth_l3_dev_route4_store(struct device *dev,
108 struct device_attribute *attr, const char *buf, size_t count)
110 struct qeth_card *card = dev_get_drvdata(dev);
112 if (!card)
113 return -EINVAL;
115 return qeth_l3_dev_route_store(card, &card->options.route4,
116 QETH_PROT_IPV4, buf, count);
119 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show,
120 qeth_l3_dev_route4_store);
122 static ssize_t qeth_l3_dev_route6_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 struct qeth_card *card = dev_get_drvdata(dev);
127 if (!card)
128 return -EINVAL;
130 return qeth_l3_dev_route_show(card, &card->options.route6, buf);
133 static ssize_t qeth_l3_dev_route6_store(struct device *dev,
134 struct device_attribute *attr, const char *buf, size_t count)
136 struct qeth_card *card = dev_get_drvdata(dev);
138 if (!card)
139 return -EINVAL;
141 return qeth_l3_dev_route_store(card, &card->options.route6,
142 QETH_PROT_IPV6, buf, count);
145 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show,
146 qeth_l3_dev_route6_store);
148 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev,
149 struct device_attribute *attr, char *buf)
151 struct qeth_card *card = dev_get_drvdata(dev);
153 if (!card)
154 return -EINVAL;
156 return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0);
159 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev,
160 struct device_attribute *attr, const char *buf, size_t count)
162 struct qeth_card *card = dev_get_drvdata(dev);
163 char *tmp;
164 int i, rc = 0;
166 if (!card)
167 return -EINVAL;
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;
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;
186 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show,
187 qeth_l3_dev_fake_broadcast_store);
189 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
192 struct qeth_card *card = dev_get_drvdata(dev);
194 if (!card)
195 return -EINVAL;
197 return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0);
200 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count)
203 struct qeth_card *card = dev_get_drvdata(dev);
204 int rc = 0;
205 unsigned long i;
207 if (!card)
208 return -EINVAL;
210 if (card->info.type != QETH_CARD_TYPE_IQD)
211 return -EPERM;
212 if (card->options.cq == QETH_CQ_ENABLED)
213 return -EPERM;
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;
222 rc = kstrtoul(buf, 16, &i);
223 if (rc) {
224 rc = -EINVAL;
225 goto out;
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;
245 out:
246 mutex_unlock(&card->conf_mutex);
247 return rc ? rc : count;
250 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show,
251 qeth_l3_dev_sniffer_store);
254 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct qeth_card *card = dev_get_drvdata(dev);
258 char tmp_hsuid[9];
260 if (!card)
261 return -EINVAL;
263 if (card->info.type != QETH_CARD_TYPE_IQD)
264 return -EPERM;
266 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid));
267 EBCASC(tmp_hsuid, 8);
268 return sprintf(buf, "%s\n", tmp_hsuid);
271 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev,
272 struct device_attribute *attr, const char *buf, size_t count)
274 struct qeth_card *card = dev_get_drvdata(dev);
275 struct qeth_ipaddr *addr;
276 char *tmp;
277 int rc, i;
279 if (!card)
280 return -EINVAL;
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;
292 tmp = strsep((char **)&buf, "\n");
293 if (strlen(tmp) > 8)
294 return -EINVAL;
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;
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;
310 spin_lock_bh(&card->ip_lock);
311 qeth_l3_delete_ip(card, addr);
312 spin_unlock_bh(&card->ip_lock);
313 kfree(addr);
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;
325 if (qeth_configure_cq(card, QETH_CQ_ENABLED))
326 return -EPERM;
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);
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;
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);
350 return rc ? rc : count;
353 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show,
354 qeth_l3_dev_hsuid_store);
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,
366 static const struct attribute_group qeth_l3_device_attr_group = {
367 .attrs = qeth_l3_device_attrs,
370 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct qeth_card *card = dev_get_drvdata(dev);
375 if (!card)
376 return -EINVAL;
378 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0);
381 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev,
382 struct device_attribute *attr, const char *buf, size_t count)
384 struct qeth_card *card = dev_get_drvdata(dev);
385 bool enable;
386 int rc = 0;
388 if (!card)
389 return -EINVAL;
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;
398 if (sysfs_streq(buf, "toggle")) {
399 enable = !card->ipato.enabled;
400 } else if (kstrtobool(buf, &enable)) {
401 rc = -EINVAL;
402 goto out;
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);
411 out:
412 mutex_unlock(&card->conf_mutex);
413 return rc ? rc : count;
416 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644,
417 qeth_l3_dev_ipato_enable_show,
418 qeth_l3_dev_ipato_enable_store);
420 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct qeth_card *card = dev_get_drvdata(dev);
425 if (!card)
426 return -EINVAL;
428 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0);
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)
435 struct qeth_card *card = dev_get_drvdata(dev);
436 bool invert;
437 int rc = 0;
439 if (!card)
440 return -EINVAL;
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;
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);
456 out:
457 mutex_unlock(&card->conf_mutex);
458 return rc ? rc : count;
461 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644,
462 qeth_l3_dev_ipato_invert4_show,
463 qeth_l3_dev_ipato_invert4_store);
465 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card,
466 enum qeth_prot_versions proto)
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;
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);
489 spin_unlock_bh(&card->ip_lock);
490 i += snprintf(buf + i, PAGE_SIZE - i, "\n");
492 return i;
495 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 struct qeth_card *card = dev_get_drvdata(dev);
500 if (!card)
501 return -EINVAL;
503 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4);
506 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto,
507 u8 *addr, int *mask_bits)
509 const char *start, *end;
510 char *tmp;
511 char buffer[40] = {0, };
513 start = buf;
514 /* get address string */
515 end = strchr(start, '/');
516 if (!end || (end - start >= 40)) {
517 return -EINVAL;
519 strncpy(buffer, start, end - start);
520 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) {
521 return -EINVAL;
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;
530 return 0;
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)
536 struct qeth_ipato_entry *ipatoe;
537 u8 addr[16];
538 int mask_bits;
539 int rc = 0;
541 mutex_lock(&card->conf_mutex);
542 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits);
543 if (rc)
544 goto out;
546 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL);
547 if (!ipatoe) {
548 rc = -ENOMEM;
549 goto out;
551 ipatoe->proto = proto;
552 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16);
553 ipatoe->mask_bits = mask_bits;
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;
563 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev,
564 struct device_attribute *attr, const char *buf, size_t count)
566 struct qeth_card *card = dev_get_drvdata(dev);
568 if (!card)
569 return -EINVAL;
571 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4);
574 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644,
575 qeth_l3_dev_ipato_add4_show,
576 qeth_l3_dev_ipato_add4_store);
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)
581 u8 addr[16];
582 int mask_bits;
583 int rc = 0;
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;
593 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev,
594 struct device_attribute *attr, const char *buf, size_t count)
596 struct qeth_card *card = dev_get_drvdata(dev);
598 if (!card)
599 return -EINVAL;
601 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4);
604 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL,
605 qeth_l3_dev_ipato_del4_store);
607 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct qeth_card *card = dev_get_drvdata(dev);
612 if (!card)
613 return -EINVAL;
615 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0);
618 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev,
619 struct device_attribute *attr, const char *buf, size_t count)
621 struct qeth_card *card = dev_get_drvdata(dev);
622 bool invert;
623 int rc = 0;
625 if (!card)
626 return -EINVAL;
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;
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);
642 out:
643 mutex_unlock(&card->conf_mutex);
644 return rc ? rc : count;
647 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644,
648 qeth_l3_dev_ipato_invert6_show,
649 qeth_l3_dev_ipato_invert6_store);
652 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct qeth_card *card = dev_get_drvdata(dev);
657 if (!card)
658 return -EINVAL;
660 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6);
663 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev,
664 struct device_attribute *attr, const char *buf, size_t count)
666 struct qeth_card *card = dev_get_drvdata(dev);
668 if (!card)
669 return -EINVAL;
671 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6);
674 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644,
675 qeth_l3_dev_ipato_add6_show,
676 qeth_l3_dev_ipato_add6_store);
678 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev,
679 struct device_attribute *attr, const char *buf, size_t count)
681 struct qeth_card *card = dev_get_drvdata(dev);
683 if (!card)
684 return -EINVAL;
686 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6);
689 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL,
690 qeth_l3_dev_ipato_del6_store);
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,
703 static const struct attribute_group qeth_device_ipato_group = {
704 .name = "ipa_takeover",
705 .attrs = qeth_ipato_device_attrs,
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)
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;
719 if (!card)
720 return -EINVAL;
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);
738 spin_unlock_bh(&card->ip_lock);
739 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n");
741 return str_len;
744 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev,
745 struct device_attribute *attr,
746 char *buf)
748 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
749 QETH_IP_TYPE_VIPA);
752 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto,
753 u8 *addr)
755 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
756 return -EINVAL;
758 return 0;
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)
764 u8 addr[16] = {0, };
765 int rc;
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;
775 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev,
776 struct device_attribute *attr, const char *buf, size_t count)
778 struct qeth_card *card = dev_get_drvdata(dev);
780 if (!card)
781 return -EINVAL;
783 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4);
786 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644,
787 qeth_l3_dev_vipa_add4_show,
788 qeth_l3_dev_vipa_add4_store);
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)
793 u8 addr[16];
794 int rc;
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;
804 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev,
805 struct device_attribute *attr, const char *buf, size_t count)
807 struct qeth_card *card = dev_get_drvdata(dev);
809 if (!card)
810 return -EINVAL;
812 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4);
815 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL,
816 qeth_l3_dev_vipa_del4_store);
818 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev,
819 struct device_attribute *attr,
820 char *buf)
822 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
823 QETH_IP_TYPE_VIPA);
826 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev,
827 struct device_attribute *attr, const char *buf, size_t count)
829 struct qeth_card *card = dev_get_drvdata(dev);
831 if (!card)
832 return -EINVAL;
834 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6);
837 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644,
838 qeth_l3_dev_vipa_add6_show,
839 qeth_l3_dev_vipa_add6_store);
841 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev,
842 struct device_attribute *attr, const char *buf, size_t count)
844 struct qeth_card *card = dev_get_drvdata(dev);
846 if (!card)
847 return -EINVAL;
849 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6);
852 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL,
853 qeth_l3_dev_vipa_del6_store);
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,
863 static const struct attribute_group qeth_device_vipa_group = {
864 .name = "vipa",
865 .attrs = qeth_vipa_device_attrs,
868 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev,
869 struct device_attribute *attr,
870 char *buf)
872 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4,
873 QETH_IP_TYPE_RXIP);
876 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto,
877 u8 *addr)
879 __be32 ipv4_addr;
880 struct in6_addr ipv6_addr;
882 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) {
883 return -EINVAL;
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;
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;
899 return 0;
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)
905 u8 addr[16] = {0, };
906 int rc;
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;
916 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev,
917 struct device_attribute *attr, const char *buf, size_t count)
919 struct qeth_card *card = dev_get_drvdata(dev);
921 if (!card)
922 return -EINVAL;
924 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4);
927 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644,
928 qeth_l3_dev_rxip_add4_show,
929 qeth_l3_dev_rxip_add4_store);
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)
934 u8 addr[16];
935 int rc;
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;
945 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev,
946 struct device_attribute *attr, const char *buf, size_t count)
948 struct qeth_card *card = dev_get_drvdata(dev);
950 if (!card)
951 return -EINVAL;
953 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4);
956 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL,
957 qeth_l3_dev_rxip_del4_store);
959 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev,
960 struct device_attribute *attr,
961 char *buf)
963 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6,
964 QETH_IP_TYPE_RXIP);
967 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev,
968 struct device_attribute *attr, const char *buf, size_t count)
970 struct qeth_card *card = dev_get_drvdata(dev);
972 if (!card)
973 return -EINVAL;
975 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6);
978 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644,
979 qeth_l3_dev_rxip_add6_show,
980 qeth_l3_dev_rxip_add6_store);
982 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev,
983 struct device_attribute *attr, const char *buf, size_t count)
985 struct qeth_card *card = dev_get_drvdata(dev);
987 if (!card)
988 return -EINVAL;
990 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6);
993 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL,
994 qeth_l3_dev_rxip_del6_store);
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,
1004 static const struct attribute_group qeth_device_rxip_group = {
1005 .name = "rxip",
1006 .attrs = qeth_rxip_device_attrs,
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,
1017 int qeth_l3_create_device_attributes(struct device *dev)
1019 return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups);
1022 void qeth_l3_remove_device_attributes(struct device *dev)
1024 sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups);
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,