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 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 qeth_l3_add_ip(card, addr); 347 spin_unlock_bh(&card->ip_lock); 348 kfree(addr); 349 350 return 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 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_vipa_add_show(char *buf, struct qeth_card *card, 709 enum qeth_prot_versions proto) 710 { 711 struct qeth_ipaddr *ipaddr; 712 char addr_str[40]; 713 int str_len = 0; 714 int entry_len; /* length of 1 entry string, differs between v4 and v6 */ 715 int i; 716 717 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40; 718 entry_len += 2; /* \n + terminator */ 719 spin_lock_bh(&card->ip_lock); 720 hash_for_each(card->ip_htable, i, ipaddr, hnode) { 721 if (ipaddr->proto != proto) 722 continue; 723 if (ipaddr->type != QETH_IP_TYPE_VIPA) 724 continue; 725 /* String must not be longer than PAGE_SIZE. So we check if 726 * string length gets near PAGE_SIZE. Then we can savely display 727 * the next IPv6 address (worst case, compared to IPv4) */ 728 if ((PAGE_SIZE - str_len) <= entry_len) 729 break; 730 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, 731 addr_str); 732 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n", 733 addr_str); 734 } 735 spin_unlock_bh(&card->ip_lock); 736 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n"); 737 738 return str_len; 739 } 740 741 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev, 742 struct device_attribute *attr, char *buf) 743 { 744 struct qeth_card *card = dev_get_drvdata(dev); 745 746 if (!card) 747 return -EINVAL; 748 749 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV4); 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 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, char *buf) 820 { 821 struct qeth_card *card = dev_get_drvdata(dev); 822 823 if (!card) 824 return -EINVAL; 825 826 return qeth_l3_dev_vipa_add_show(buf, card, QETH_PROT_IPV6); 827 } 828 829 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev, 830 struct device_attribute *attr, const char *buf, size_t count) 831 { 832 struct qeth_card *card = dev_get_drvdata(dev); 833 834 if (!card) 835 return -EINVAL; 836 837 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6); 838 } 839 840 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644, 841 qeth_l3_dev_vipa_add6_show, 842 qeth_l3_dev_vipa_add6_store); 843 844 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev, 845 struct device_attribute *attr, const char *buf, size_t count) 846 { 847 struct qeth_card *card = dev_get_drvdata(dev); 848 849 if (!card) 850 return -EINVAL; 851 852 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6); 853 } 854 855 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL, 856 qeth_l3_dev_vipa_del6_store); 857 858 static struct attribute *qeth_vipa_device_attrs[] = { 859 &dev_attr_vipa_add4.attr, 860 &dev_attr_vipa_del4.attr, 861 &dev_attr_vipa_add6.attr, 862 &dev_attr_vipa_del6.attr, 863 NULL, 864 }; 865 866 static const struct attribute_group qeth_device_vipa_group = { 867 .name = "vipa", 868 .attrs = qeth_vipa_device_attrs, 869 }; 870 871 static ssize_t qeth_l3_dev_rxip_add_show(char *buf, struct qeth_card *card, 872 enum qeth_prot_versions proto) 873 { 874 struct qeth_ipaddr *ipaddr; 875 char addr_str[40]; 876 int str_len = 0; 877 int entry_len; /* length of 1 entry string, differs between v4 and v6 */ 878 int i; 879 880 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40; 881 entry_len += 2; /* \n + terminator */ 882 spin_lock_bh(&card->ip_lock); 883 hash_for_each(card->ip_htable, i, ipaddr, hnode) { 884 if (ipaddr->proto != proto) 885 continue; 886 if (ipaddr->type != QETH_IP_TYPE_RXIP) 887 continue; 888 /* String must not be longer than PAGE_SIZE. So we check if 889 * string length gets near PAGE_SIZE. Then we can savely display 890 * the next IPv6 address (worst case, compared to IPv4) */ 891 if ((PAGE_SIZE - str_len) <= entry_len) 892 break; 893 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, 894 addr_str); 895 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n", 896 addr_str); 897 } 898 spin_unlock_bh(&card->ip_lock); 899 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n"); 900 901 return str_len; 902 } 903 904 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev, 905 struct device_attribute *attr, char *buf) 906 { 907 struct qeth_card *card = dev_get_drvdata(dev); 908 909 if (!card) 910 return -EINVAL; 911 912 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV4); 913 } 914 915 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto, 916 u8 *addr) 917 { 918 __be32 ipv4_addr; 919 struct in6_addr ipv6_addr; 920 921 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) { 922 return -EINVAL; 923 } 924 if (proto == QETH_PROT_IPV4) { 925 memcpy(&ipv4_addr, addr, sizeof(ipv4_addr)); 926 if (ipv4_is_multicast(ipv4_addr)) { 927 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 928 return -EINVAL; 929 } 930 } else if (proto == QETH_PROT_IPV6) { 931 memcpy(&ipv6_addr, addr, sizeof(ipv6_addr)); 932 if (ipv6_addr_is_multicast(&ipv6_addr)) { 933 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 934 return -EINVAL; 935 } 936 } 937 938 return 0; 939 } 940 941 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count, 942 struct qeth_card *card, enum qeth_prot_versions proto) 943 { 944 u8 addr[16] = {0, }; 945 int rc; 946 947 mutex_lock(&card->conf_mutex); 948 rc = qeth_l3_parse_rxipe(buf, proto, addr); 949 if (!rc) 950 rc = qeth_l3_add_rxip(card, proto, addr); 951 mutex_unlock(&card->conf_mutex); 952 return rc ? rc : count; 953 } 954 955 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev, 956 struct device_attribute *attr, const char *buf, size_t count) 957 { 958 struct qeth_card *card = dev_get_drvdata(dev); 959 960 if (!card) 961 return -EINVAL; 962 963 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4); 964 } 965 966 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644, 967 qeth_l3_dev_rxip_add4_show, 968 qeth_l3_dev_rxip_add4_store); 969 970 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count, 971 struct qeth_card *card, enum qeth_prot_versions proto) 972 { 973 u8 addr[16]; 974 int rc; 975 976 mutex_lock(&card->conf_mutex); 977 rc = qeth_l3_parse_rxipe(buf, proto, addr); 978 if (!rc) 979 qeth_l3_del_rxip(card, proto, addr); 980 mutex_unlock(&card->conf_mutex); 981 return rc ? rc : count; 982 } 983 984 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev, 985 struct device_attribute *attr, const char *buf, size_t count) 986 { 987 struct qeth_card *card = dev_get_drvdata(dev); 988 989 if (!card) 990 return -EINVAL; 991 992 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4); 993 } 994 995 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL, 996 qeth_l3_dev_rxip_del4_store); 997 998 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev, 999 struct device_attribute *attr, char *buf) 1000 { 1001 struct qeth_card *card = dev_get_drvdata(dev); 1002 1003 if (!card) 1004 return -EINVAL; 1005 1006 return qeth_l3_dev_rxip_add_show(buf, card, QETH_PROT_IPV6); 1007 } 1008 1009 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev, 1010 struct device_attribute *attr, const char *buf, size_t count) 1011 { 1012 struct qeth_card *card = dev_get_drvdata(dev); 1013 1014 if (!card) 1015 return -EINVAL; 1016 1017 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6); 1018 } 1019 1020 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644, 1021 qeth_l3_dev_rxip_add6_show, 1022 qeth_l3_dev_rxip_add6_store); 1023 1024 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev, 1025 struct device_attribute *attr, const char *buf, size_t count) 1026 { 1027 struct qeth_card *card = dev_get_drvdata(dev); 1028 1029 if (!card) 1030 return -EINVAL; 1031 1032 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6); 1033 } 1034 1035 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL, 1036 qeth_l3_dev_rxip_del6_store); 1037 1038 static struct attribute *qeth_rxip_device_attrs[] = { 1039 &dev_attr_rxip_add4.attr, 1040 &dev_attr_rxip_del4.attr, 1041 &dev_attr_rxip_add6.attr, 1042 &dev_attr_rxip_del6.attr, 1043 NULL, 1044 }; 1045 1046 static const struct attribute_group qeth_device_rxip_group = { 1047 .name = "rxip", 1048 .attrs = qeth_rxip_device_attrs, 1049 }; 1050 1051 static const struct attribute_group *qeth_l3_only_attr_groups[] = { 1052 &qeth_l3_device_attr_group, 1053 &qeth_device_ipato_group, 1054 &qeth_device_vipa_group, 1055 &qeth_device_rxip_group, 1056 NULL, 1057 }; 1058 1059 int qeth_l3_create_device_attributes(struct device *dev) 1060 { 1061 return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups); 1062 } 1063 1064 void qeth_l3_remove_device_attributes(struct device *dev) 1065 { 1066 sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups); 1067 } 1068 1069 const struct attribute_group *qeth_l3_attr_groups[] = { 1070 &qeth_device_attr_group, 1071 &qeth_device_blkt_group, 1072 /* l3 specific, see qeth_l3_only_attr_groups: */ 1073 &qeth_l3_device_attr_group, 1074 &qeth_device_ipato_group, 1075 &qeth_device_vipa_group, 1076 &qeth_device_rxip_group, 1077 NULL, 1078 }; 1079