1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2007 4 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>, 5 * Frank Pavlic <fpavlic@de.ibm.com>, 6 * Thomas Spatzier <tspat@de.ibm.com>, 7 * Frank Blaschka <frank.blaschka@de.ibm.com> 8 */ 9 10 #include <linux/slab.h> 11 #include <asm/ebcdic.h> 12 #include <linux/hashtable.h> 13 #include <linux/inet.h> 14 #include "qeth_l3.h" 15 16 #define QETH_DEVICE_ATTR(_id, _name, _mode, _show, _store) \ 17 struct device_attribute dev_attr_##_id = __ATTR(_name, _mode, _show, _store) 18 19 static int qeth_l3_string_to_ipaddr(const char *buf, 20 enum qeth_prot_versions proto, u8 *addr) 21 { 22 const char *end; 23 24 if ((proto == QETH_PROT_IPV4 && !in4_pton(buf, -1, addr, -1, &end)) || 25 (proto == QETH_PROT_IPV6 && !in6_pton(buf, -1, addr, -1, &end))) 26 return -EINVAL; 27 return 0; 28 } 29 30 static ssize_t qeth_l3_dev_route_show(struct qeth_card *card, 31 struct qeth_routing_info *route, char *buf) 32 { 33 switch (route->type) { 34 case PRIMARY_ROUTER: 35 return sprintf(buf, "%s\n", "primary router"); 36 case SECONDARY_ROUTER: 37 return sprintf(buf, "%s\n", "secondary router"); 38 case MULTICAST_ROUTER: 39 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO) 40 return sprintf(buf, "%s\n", "multicast router+"); 41 else 42 return sprintf(buf, "%s\n", "multicast router"); 43 case PRIMARY_CONNECTOR: 44 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO) 45 return sprintf(buf, "%s\n", "primary connector+"); 46 else 47 return sprintf(buf, "%s\n", "primary connector"); 48 case SECONDARY_CONNECTOR: 49 if (card->info.broadcast_capable == QETH_BROADCAST_WITHOUT_ECHO) 50 return sprintf(buf, "%s\n", "secondary connector+"); 51 else 52 return sprintf(buf, "%s\n", "secondary connector"); 53 default: 54 return sprintf(buf, "%s\n", "no"); 55 } 56 } 57 58 static ssize_t qeth_l3_dev_route4_show(struct device *dev, 59 struct device_attribute *attr, char *buf) 60 { 61 struct qeth_card *card = dev_get_drvdata(dev); 62 63 return qeth_l3_dev_route_show(card, &card->options.route4, buf); 64 } 65 66 static ssize_t qeth_l3_dev_route_store(struct qeth_card *card, 67 struct qeth_routing_info *route, enum qeth_prot_versions prot, 68 const char *buf, size_t count) 69 { 70 enum qeth_routing_types old_route_type = route->type; 71 int rc = 0; 72 73 mutex_lock(&card->conf_mutex); 74 if (sysfs_streq(buf, "no_router")) { 75 route->type = NO_ROUTER; 76 } else if (sysfs_streq(buf, "primary_connector")) { 77 route->type = PRIMARY_CONNECTOR; 78 } else if (sysfs_streq(buf, "secondary_connector")) { 79 route->type = SECONDARY_CONNECTOR; 80 } else if (sysfs_streq(buf, "primary_router")) { 81 route->type = PRIMARY_ROUTER; 82 } else if (sysfs_streq(buf, "secondary_router")) { 83 route->type = SECONDARY_ROUTER; 84 } else if (sysfs_streq(buf, "multicast_router")) { 85 route->type = MULTICAST_ROUTER; 86 } else { 87 rc = -EINVAL; 88 goto out; 89 } 90 if (qeth_card_hw_is_reachable(card) && 91 (old_route_type != route->type)) { 92 if (prot == QETH_PROT_IPV4) 93 rc = qeth_l3_setrouting_v4(card); 94 else if (prot == QETH_PROT_IPV6) 95 rc = qeth_l3_setrouting_v6(card); 96 } 97 out: 98 if (rc) 99 route->type = old_route_type; 100 mutex_unlock(&card->conf_mutex); 101 return rc ? rc : count; 102 } 103 104 static ssize_t qeth_l3_dev_route4_store(struct device *dev, 105 struct device_attribute *attr, const char *buf, size_t count) 106 { 107 struct qeth_card *card = dev_get_drvdata(dev); 108 109 return qeth_l3_dev_route_store(card, &card->options.route4, 110 QETH_PROT_IPV4, buf, count); 111 } 112 113 static DEVICE_ATTR(route4, 0644, qeth_l3_dev_route4_show, 114 qeth_l3_dev_route4_store); 115 116 static ssize_t qeth_l3_dev_route6_show(struct device *dev, 117 struct device_attribute *attr, char *buf) 118 { 119 struct qeth_card *card = dev_get_drvdata(dev); 120 121 return qeth_l3_dev_route_show(card, &card->options.route6, buf); 122 } 123 124 static ssize_t qeth_l3_dev_route6_store(struct device *dev, 125 struct device_attribute *attr, const char *buf, size_t count) 126 { 127 struct qeth_card *card = dev_get_drvdata(dev); 128 129 return qeth_l3_dev_route_store(card, &card->options.route6, 130 QETH_PROT_IPV6, buf, count); 131 } 132 133 static DEVICE_ATTR(route6, 0644, qeth_l3_dev_route6_show, 134 qeth_l3_dev_route6_store); 135 136 static ssize_t qeth_l3_dev_fake_broadcast_show(struct device *dev, 137 struct device_attribute *attr, char *buf) 138 { 139 struct qeth_card *card = dev_get_drvdata(dev); 140 141 return sprintf(buf, "%i\n", card->options.fake_broadcast? 1:0); 142 } 143 144 static ssize_t qeth_l3_dev_fake_broadcast_store(struct device *dev, 145 struct device_attribute *attr, const char *buf, size_t count) 146 { 147 struct qeth_card *card = dev_get_drvdata(dev); 148 char *tmp; 149 int i, rc = 0; 150 151 mutex_lock(&card->conf_mutex); 152 if (card->state != CARD_STATE_DOWN) { 153 rc = -EPERM; 154 goto out; 155 } 156 157 i = simple_strtoul(buf, &tmp, 16); 158 if ((i == 0) || (i == 1)) 159 card->options.fake_broadcast = i; 160 else 161 rc = -EINVAL; 162 out: 163 mutex_unlock(&card->conf_mutex); 164 return rc ? rc : count; 165 } 166 167 static DEVICE_ATTR(fake_broadcast, 0644, qeth_l3_dev_fake_broadcast_show, 168 qeth_l3_dev_fake_broadcast_store); 169 170 static ssize_t qeth_l3_dev_sniffer_show(struct device *dev, 171 struct device_attribute *attr, char *buf) 172 { 173 struct qeth_card *card = dev_get_drvdata(dev); 174 175 return sprintf(buf, "%i\n", card->options.sniffer ? 1 : 0); 176 } 177 178 static ssize_t qeth_l3_dev_sniffer_store(struct device *dev, 179 struct device_attribute *attr, const char *buf, size_t count) 180 { 181 struct qeth_card *card = dev_get_drvdata(dev); 182 int rc = 0; 183 unsigned long i; 184 185 if (!IS_IQD(card)) 186 return -EPERM; 187 if (card->options.cq == QETH_CQ_ENABLED) 188 return -EPERM; 189 190 mutex_lock(&card->conf_mutex); 191 if (card->state != CARD_STATE_DOWN) { 192 rc = -EPERM; 193 goto out; 194 } 195 196 rc = kstrtoul(buf, 16, &i); 197 if (rc) { 198 rc = -EINVAL; 199 goto out; 200 } 201 switch (i) { 202 case 0: 203 card->options.sniffer = i; 204 break; 205 case 1: 206 qdio_get_ssqd_desc(CARD_DDEV(card), &card->ssqd); 207 if (card->ssqd.qdioac2 & CHSC_AC2_SNIFFER_AVAILABLE) { 208 card->options.sniffer = i; 209 qeth_resize_buffer_pool(card, QETH_IN_BUF_COUNT_MAX); 210 } else { 211 rc = -EPERM; 212 } 213 214 break; 215 default: 216 rc = -EINVAL; 217 } 218 out: 219 mutex_unlock(&card->conf_mutex); 220 return rc ? rc : count; 221 } 222 223 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show, 224 qeth_l3_dev_sniffer_store); 225 226 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev, 227 struct device_attribute *attr, char *buf) 228 { 229 struct qeth_card *card = dev_get_drvdata(dev); 230 char tmp_hsuid[9]; 231 232 if (!IS_IQD(card)) 233 return -EPERM; 234 235 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid)); 236 EBCASC(tmp_hsuid, 8); 237 return sprintf(buf, "%s\n", tmp_hsuid); 238 } 239 240 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev, 241 struct device_attribute *attr, const char *buf, size_t count) 242 { 243 struct qeth_card *card = dev_get_drvdata(dev); 244 int rc = 0; 245 char *tmp; 246 247 if (!IS_IQD(card)) 248 return -EPERM; 249 250 mutex_lock(&card->conf_mutex); 251 if (card->state != CARD_STATE_DOWN) { 252 rc = -EPERM; 253 goto out; 254 } 255 256 if (card->options.sniffer) { 257 rc = -EPERM; 258 goto out; 259 } 260 261 if (card->options.cq == QETH_CQ_NOTAVAILABLE) { 262 rc = -EPERM; 263 goto out; 264 } 265 266 tmp = strsep((char **)&buf, "\n"); 267 if (strlen(tmp) > 8) { 268 rc = -EINVAL; 269 goto out; 270 } 271 272 if (card->options.hsuid[0]) 273 /* delete old ip address */ 274 qeth_l3_modify_hsuid(card, false); 275 276 if (strlen(tmp) == 0) { 277 /* delete ip address only */ 278 card->options.hsuid[0] = '\0'; 279 memcpy(card->dev->perm_addr, card->options.hsuid, 9); 280 qeth_configure_cq(card, QETH_CQ_DISABLED); 281 goto out; 282 } 283 284 if (qeth_configure_cq(card, QETH_CQ_ENABLED)) { 285 rc = -EPERM; 286 goto out; 287 } 288 289 snprintf(card->options.hsuid, sizeof(card->options.hsuid), 290 "%-8s", tmp); 291 ASCEBC(card->options.hsuid, 8); 292 memcpy(card->dev->perm_addr, card->options.hsuid, 9); 293 294 rc = qeth_l3_modify_hsuid(card, true); 295 296 out: 297 mutex_unlock(&card->conf_mutex); 298 return rc ? rc : count; 299 } 300 301 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show, 302 qeth_l3_dev_hsuid_store); 303 304 305 static struct attribute *qeth_l3_device_attrs[] = { 306 &dev_attr_route4.attr, 307 &dev_attr_route6.attr, 308 &dev_attr_fake_broadcast.attr, 309 &dev_attr_sniffer.attr, 310 &dev_attr_hsuid.attr, 311 NULL, 312 }; 313 314 static const struct attribute_group qeth_l3_device_attr_group = { 315 .attrs = qeth_l3_device_attrs, 316 }; 317 318 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev, 319 struct device_attribute *attr, char *buf) 320 { 321 struct qeth_card *card = dev_get_drvdata(dev); 322 323 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0); 324 } 325 326 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev, 327 struct device_attribute *attr, const char *buf, size_t count) 328 { 329 struct qeth_card *card = dev_get_drvdata(dev); 330 bool enable; 331 int rc = 0; 332 333 mutex_lock(&card->conf_mutex); 334 if (card->state != CARD_STATE_DOWN) { 335 rc = -EPERM; 336 goto out; 337 } 338 339 if (sysfs_streq(buf, "toggle")) { 340 enable = !card->ipato.enabled; 341 } else if (kstrtobool(buf, &enable)) { 342 rc = -EINVAL; 343 goto out; 344 } 345 346 if (card->ipato.enabled != enable) { 347 card->ipato.enabled = enable; 348 mutex_lock(&card->ip_lock); 349 qeth_l3_update_ipato(card); 350 mutex_unlock(&card->ip_lock); 351 } 352 out: 353 mutex_unlock(&card->conf_mutex); 354 return rc ? rc : count; 355 } 356 357 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644, 358 qeth_l3_dev_ipato_enable_show, 359 qeth_l3_dev_ipato_enable_store); 360 361 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev, 362 struct device_attribute *attr, char *buf) 363 { 364 struct qeth_card *card = dev_get_drvdata(dev); 365 366 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0); 367 } 368 369 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev, 370 struct device_attribute *attr, 371 const char *buf, size_t count) 372 { 373 struct qeth_card *card = dev_get_drvdata(dev); 374 bool invert; 375 int rc = 0; 376 377 mutex_lock(&card->conf_mutex); 378 if (sysfs_streq(buf, "toggle")) { 379 invert = !card->ipato.invert4; 380 } else if (kstrtobool(buf, &invert)) { 381 rc = -EINVAL; 382 goto out; 383 } 384 385 if (card->ipato.invert4 != invert) { 386 card->ipato.invert4 = invert; 387 mutex_lock(&card->ip_lock); 388 qeth_l3_update_ipato(card); 389 mutex_unlock(&card->ip_lock); 390 } 391 out: 392 mutex_unlock(&card->conf_mutex); 393 return rc ? rc : count; 394 } 395 396 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644, 397 qeth_l3_dev_ipato_invert4_show, 398 qeth_l3_dev_ipato_invert4_store); 399 400 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card, 401 enum qeth_prot_versions proto) 402 { 403 struct qeth_ipato_entry *ipatoe; 404 int str_len = 0; 405 406 mutex_lock(&card->ip_lock); 407 list_for_each_entry(ipatoe, &card->ipato.entries, entry) { 408 char addr_str[40]; 409 int entry_len; 410 411 if (ipatoe->proto != proto) 412 continue; 413 414 entry_len = qeth_l3_ipaddr_to_string(proto, ipatoe->addr, 415 addr_str); 416 if (entry_len < 0) 417 continue; 418 419 /* Append /%mask to the entry: */ 420 entry_len += 1 + ((proto == QETH_PROT_IPV4) ? 2 : 3); 421 /* Enough room to format %entry\n into null terminated page? */ 422 if (entry_len + 1 > PAGE_SIZE - str_len - 1) 423 break; 424 425 entry_len = scnprintf(buf, PAGE_SIZE - str_len, 426 "%s/%i\n", addr_str, ipatoe->mask_bits); 427 str_len += entry_len; 428 buf += entry_len; 429 } 430 mutex_unlock(&card->ip_lock); 431 432 return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n"); 433 } 434 435 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev, 436 struct device_attribute *attr, char *buf) 437 { 438 struct qeth_card *card = dev_get_drvdata(dev); 439 440 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4); 441 } 442 443 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto, 444 u8 *addr, int *mask_bits) 445 { 446 const char *start, *end; 447 char *tmp; 448 char buffer[40] = {0, }; 449 450 start = buf; 451 /* get address string */ 452 end = strchr(start, '/'); 453 if (!end || (end - start >= 40)) { 454 return -EINVAL; 455 } 456 strncpy(buffer, start, end - start); 457 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) { 458 return -EINVAL; 459 } 460 start = end + 1; 461 *mask_bits = simple_strtoul(start, &tmp, 10); 462 if (!strlen(start) || 463 (tmp == start) || 464 (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) { 465 return -EINVAL; 466 } 467 return 0; 468 } 469 470 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count, 471 struct qeth_card *card, enum qeth_prot_versions proto) 472 { 473 struct qeth_ipato_entry *ipatoe; 474 u8 addr[16]; 475 int mask_bits; 476 int rc = 0; 477 478 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits); 479 if (rc) 480 return rc; 481 482 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL); 483 if (!ipatoe) 484 return -ENOMEM; 485 486 ipatoe->proto = proto; 487 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16); 488 ipatoe->mask_bits = mask_bits; 489 490 rc = qeth_l3_add_ipato_entry(card, ipatoe); 491 if (rc) 492 kfree(ipatoe); 493 494 return rc ? rc : count; 495 } 496 497 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev, 498 struct device_attribute *attr, const char *buf, size_t count) 499 { 500 struct qeth_card *card = dev_get_drvdata(dev); 501 502 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4); 503 } 504 505 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644, 506 qeth_l3_dev_ipato_add4_show, 507 qeth_l3_dev_ipato_add4_store); 508 509 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count, 510 struct qeth_card *card, enum qeth_prot_versions proto) 511 { 512 u8 addr[16]; 513 int mask_bits; 514 int rc = 0; 515 516 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits); 517 if (!rc) 518 rc = qeth_l3_del_ipato_entry(card, proto, addr, mask_bits); 519 return rc ? rc : count; 520 } 521 522 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev, 523 struct device_attribute *attr, const char *buf, size_t count) 524 { 525 struct qeth_card *card = dev_get_drvdata(dev); 526 527 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4); 528 } 529 530 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL, 531 qeth_l3_dev_ipato_del4_store); 532 533 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev, 534 struct device_attribute *attr, char *buf) 535 { 536 struct qeth_card *card = dev_get_drvdata(dev); 537 538 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0); 539 } 540 541 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev, 542 struct device_attribute *attr, const char *buf, size_t count) 543 { 544 struct qeth_card *card = dev_get_drvdata(dev); 545 bool invert; 546 int rc = 0; 547 548 mutex_lock(&card->conf_mutex); 549 if (sysfs_streq(buf, "toggle")) { 550 invert = !card->ipato.invert6; 551 } else if (kstrtobool(buf, &invert)) { 552 rc = -EINVAL; 553 goto out; 554 } 555 556 if (card->ipato.invert6 != invert) { 557 card->ipato.invert6 = invert; 558 mutex_lock(&card->ip_lock); 559 qeth_l3_update_ipato(card); 560 mutex_unlock(&card->ip_lock); 561 } 562 out: 563 mutex_unlock(&card->conf_mutex); 564 return rc ? rc : count; 565 } 566 567 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644, 568 qeth_l3_dev_ipato_invert6_show, 569 qeth_l3_dev_ipato_invert6_store); 570 571 572 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev, 573 struct device_attribute *attr, char *buf) 574 { 575 struct qeth_card *card = dev_get_drvdata(dev); 576 577 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6); 578 } 579 580 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev, 581 struct device_attribute *attr, const char *buf, size_t count) 582 { 583 struct qeth_card *card = dev_get_drvdata(dev); 584 585 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6); 586 } 587 588 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644, 589 qeth_l3_dev_ipato_add6_show, 590 qeth_l3_dev_ipato_add6_store); 591 592 static ssize_t qeth_l3_dev_ipato_del6_store(struct device *dev, 593 struct device_attribute *attr, const char *buf, size_t count) 594 { 595 struct qeth_card *card = dev_get_drvdata(dev); 596 597 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6); 598 } 599 600 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL, 601 qeth_l3_dev_ipato_del6_store); 602 603 static struct attribute *qeth_ipato_device_attrs[] = { 604 &dev_attr_ipato_enable.attr, 605 &dev_attr_ipato_invert4.attr, 606 &dev_attr_ipato_add4.attr, 607 &dev_attr_ipato_del4.attr, 608 &dev_attr_ipato_invert6.attr, 609 &dev_attr_ipato_add6.attr, 610 &dev_attr_ipato_del6.attr, 611 NULL, 612 }; 613 614 static const struct attribute_group qeth_device_ipato_group = { 615 .name = "ipa_takeover", 616 .attrs = qeth_ipato_device_attrs, 617 }; 618 619 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf, 620 enum qeth_prot_versions proto, 621 enum qeth_ip_types type) 622 { 623 struct qeth_card *card = dev_get_drvdata(dev); 624 struct qeth_ipaddr *ipaddr; 625 int str_len = 0; 626 int i; 627 628 mutex_lock(&card->ip_lock); 629 hash_for_each(card->ip_htable, i, ipaddr, hnode) { 630 char addr_str[40]; 631 int entry_len; 632 633 if (ipaddr->proto != proto || ipaddr->type != type) 634 continue; 635 636 entry_len = qeth_l3_ipaddr_to_string(proto, (u8 *)&ipaddr->u, 637 addr_str); 638 if (entry_len < 0) 639 continue; 640 641 /* Enough room to format %addr\n into null terminated page? */ 642 if (entry_len + 1 > PAGE_SIZE - str_len - 1) 643 break; 644 645 entry_len = scnprintf(buf, PAGE_SIZE - str_len, "%s\n", 646 addr_str); 647 str_len += entry_len; 648 buf += entry_len; 649 } 650 mutex_unlock(&card->ip_lock); 651 652 return str_len ? str_len : scnprintf(buf, PAGE_SIZE, "\n"); 653 } 654 655 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev, 656 struct device_attribute *attr, 657 char *buf) 658 { 659 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4, 660 QETH_IP_TYPE_VIPA); 661 } 662 663 static ssize_t qeth_l3_vipa_store(struct device *dev, const char *buf, bool add, 664 size_t count, enum qeth_prot_versions proto) 665 { 666 struct qeth_card *card = dev_get_drvdata(dev); 667 u8 addr[16] = {0, }; 668 int rc; 669 670 rc = qeth_l3_string_to_ipaddr(buf, proto, addr); 671 if (!rc) 672 rc = qeth_l3_modify_rxip_vipa(card, add, addr, 673 QETH_IP_TYPE_VIPA, proto); 674 return rc ? rc : count; 675 } 676 677 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev, 678 struct device_attribute *attr, const char *buf, size_t count) 679 { 680 return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4); 681 } 682 683 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644, 684 qeth_l3_dev_vipa_add4_show, 685 qeth_l3_dev_vipa_add4_store); 686 687 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev, 688 struct device_attribute *attr, const char *buf, size_t count) 689 { 690 return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV4); 691 } 692 693 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL, 694 qeth_l3_dev_vipa_del4_store); 695 696 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev, 697 struct device_attribute *attr, 698 char *buf) 699 { 700 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6, 701 QETH_IP_TYPE_VIPA); 702 } 703 704 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev, 705 struct device_attribute *attr, const char *buf, size_t count) 706 { 707 return qeth_l3_vipa_store(dev, buf, true, count, QETH_PROT_IPV6); 708 } 709 710 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644, 711 qeth_l3_dev_vipa_add6_show, 712 qeth_l3_dev_vipa_add6_store); 713 714 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev, 715 struct device_attribute *attr, const char *buf, size_t count) 716 { 717 return qeth_l3_vipa_store(dev, buf, false, count, QETH_PROT_IPV6); 718 } 719 720 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL, 721 qeth_l3_dev_vipa_del6_store); 722 723 static struct attribute *qeth_vipa_device_attrs[] = { 724 &dev_attr_vipa_add4.attr, 725 &dev_attr_vipa_del4.attr, 726 &dev_attr_vipa_add6.attr, 727 &dev_attr_vipa_del6.attr, 728 NULL, 729 }; 730 731 static const struct attribute_group qeth_device_vipa_group = { 732 .name = "vipa", 733 .attrs = qeth_vipa_device_attrs, 734 }; 735 736 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev, 737 struct device_attribute *attr, 738 char *buf) 739 { 740 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4, 741 QETH_IP_TYPE_RXIP); 742 } 743 744 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto, 745 u8 *addr) 746 { 747 __be32 ipv4_addr; 748 struct in6_addr ipv6_addr; 749 750 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) { 751 return -EINVAL; 752 } 753 if (proto == QETH_PROT_IPV4) { 754 memcpy(&ipv4_addr, addr, sizeof(ipv4_addr)); 755 if (ipv4_is_multicast(ipv4_addr)) { 756 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 757 return -EINVAL; 758 } 759 } else if (proto == QETH_PROT_IPV6) { 760 memcpy(&ipv6_addr, addr, sizeof(ipv6_addr)); 761 if (ipv6_addr_is_multicast(&ipv6_addr)) { 762 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 763 return -EINVAL; 764 } 765 } 766 767 return 0; 768 } 769 770 static ssize_t qeth_l3_rxip_store(struct device *dev, const char *buf, bool add, 771 size_t count, enum qeth_prot_versions proto) 772 { 773 struct qeth_card *card = dev_get_drvdata(dev); 774 u8 addr[16] = {0, }; 775 int rc; 776 777 rc = qeth_l3_parse_rxipe(buf, proto, addr); 778 if (!rc) 779 rc = qeth_l3_modify_rxip_vipa(card, add, addr, 780 QETH_IP_TYPE_RXIP, proto); 781 return rc ? rc : count; 782 } 783 784 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev, 785 struct device_attribute *attr, const char *buf, size_t count) 786 { 787 return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV4); 788 } 789 790 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644, 791 qeth_l3_dev_rxip_add4_show, 792 qeth_l3_dev_rxip_add4_store); 793 794 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev, 795 struct device_attribute *attr, const char *buf, size_t count) 796 { 797 return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV4); 798 } 799 800 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL, 801 qeth_l3_dev_rxip_del4_store); 802 803 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev, 804 struct device_attribute *attr, 805 char *buf) 806 { 807 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6, 808 QETH_IP_TYPE_RXIP); 809 } 810 811 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev, 812 struct device_attribute *attr, const char *buf, size_t count) 813 { 814 return qeth_l3_rxip_store(dev, buf, true, count, QETH_PROT_IPV6); 815 } 816 817 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644, 818 qeth_l3_dev_rxip_add6_show, 819 qeth_l3_dev_rxip_add6_store); 820 821 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev, 822 struct device_attribute *attr, const char *buf, size_t count) 823 { 824 return qeth_l3_rxip_store(dev, buf, false, count, QETH_PROT_IPV6); 825 } 826 827 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL, 828 qeth_l3_dev_rxip_del6_store); 829 830 static struct attribute *qeth_rxip_device_attrs[] = { 831 &dev_attr_rxip_add4.attr, 832 &dev_attr_rxip_del4.attr, 833 &dev_attr_rxip_add6.attr, 834 &dev_attr_rxip_del6.attr, 835 NULL, 836 }; 837 838 static const struct attribute_group qeth_device_rxip_group = { 839 .name = "rxip", 840 .attrs = qeth_rxip_device_attrs, 841 }; 842 843 static const struct attribute_group *qeth_l3_only_attr_groups[] = { 844 &qeth_l3_device_attr_group, 845 &qeth_device_ipato_group, 846 &qeth_device_vipa_group, 847 &qeth_device_rxip_group, 848 NULL, 849 }; 850 851 int qeth_l3_create_device_attributes(struct device *dev) 852 { 853 return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups); 854 } 855 856 void qeth_l3_remove_device_attributes(struct device *dev) 857 { 858 sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups); 859 } 860 861 const struct attribute_group *qeth_l3_attr_groups[] = { 862 &qeth_device_attr_group, 863 &qeth_device_blkt_group, 864 /* l3 specific, see qeth_l3_only_attr_groups: */ 865 &qeth_l3_device_attr_group, 866 &qeth_device_ipato_group, 867 &qeth_device_vipa_group, 868 &qeth_device_rxip_group, 869 NULL, 870 }; 871