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 if (card->qdio.init_pool.buf_count != 210 QETH_IN_BUF_COUNT_MAX) 211 qeth_realloc_buffer_pool(card, 212 QETH_IN_BUF_COUNT_MAX); 213 } else 214 rc = -EPERM; 215 break; 216 default: 217 rc = -EINVAL; 218 } 219 out: 220 mutex_unlock(&card->conf_mutex); 221 return rc ? rc : count; 222 } 223 224 static DEVICE_ATTR(sniffer, 0644, qeth_l3_dev_sniffer_show, 225 qeth_l3_dev_sniffer_store); 226 227 static ssize_t qeth_l3_dev_hsuid_show(struct device *dev, 228 struct device_attribute *attr, char *buf) 229 { 230 struct qeth_card *card = dev_get_drvdata(dev); 231 char tmp_hsuid[9]; 232 233 if (!IS_IQD(card)) 234 return -EPERM; 235 236 memcpy(tmp_hsuid, card->options.hsuid, sizeof(tmp_hsuid)); 237 EBCASC(tmp_hsuid, 8); 238 return sprintf(buf, "%s\n", tmp_hsuid); 239 } 240 241 static ssize_t qeth_l3_dev_hsuid_store(struct device *dev, 242 struct device_attribute *attr, const char *buf, size_t count) 243 { 244 struct qeth_card *card = dev_get_drvdata(dev); 245 int rc = 0; 246 char *tmp; 247 248 if (!IS_IQD(card)) 249 return -EPERM; 250 251 mutex_lock(&card->conf_mutex); 252 if (card->state != CARD_STATE_DOWN) { 253 rc = -EPERM; 254 goto out; 255 } 256 257 if (card->options.sniffer) { 258 rc = -EPERM; 259 goto out; 260 } 261 262 if (card->options.cq == QETH_CQ_NOTAVAILABLE) { 263 rc = -EPERM; 264 goto out; 265 } 266 267 tmp = strsep((char **)&buf, "\n"); 268 if (strlen(tmp) > 8) { 269 rc = -EINVAL; 270 goto out; 271 } 272 273 if (card->options.hsuid[0]) 274 /* delete old ip address */ 275 qeth_l3_modify_hsuid(card, false); 276 277 if (strlen(tmp) == 0) { 278 /* delete ip address only */ 279 card->options.hsuid[0] = '\0'; 280 memcpy(card->dev->perm_addr, card->options.hsuid, 9); 281 qeth_configure_cq(card, QETH_CQ_DISABLED); 282 goto out; 283 } 284 285 if (qeth_configure_cq(card, QETH_CQ_ENABLED)) { 286 rc = -EPERM; 287 goto out; 288 } 289 290 snprintf(card->options.hsuid, sizeof(card->options.hsuid), 291 "%-8s", tmp); 292 ASCEBC(card->options.hsuid, 8); 293 memcpy(card->dev->perm_addr, card->options.hsuid, 9); 294 295 rc = qeth_l3_modify_hsuid(card, true); 296 297 out: 298 mutex_unlock(&card->conf_mutex); 299 return rc ? rc : count; 300 } 301 302 static DEVICE_ATTR(hsuid, 0644, qeth_l3_dev_hsuid_show, 303 qeth_l3_dev_hsuid_store); 304 305 306 static struct attribute *qeth_l3_device_attrs[] = { 307 &dev_attr_route4.attr, 308 &dev_attr_route6.attr, 309 &dev_attr_fake_broadcast.attr, 310 &dev_attr_sniffer.attr, 311 &dev_attr_hsuid.attr, 312 NULL, 313 }; 314 315 static const struct attribute_group qeth_l3_device_attr_group = { 316 .attrs = qeth_l3_device_attrs, 317 }; 318 319 static ssize_t qeth_l3_dev_ipato_enable_show(struct device *dev, 320 struct device_attribute *attr, char *buf) 321 { 322 struct qeth_card *card = dev_get_drvdata(dev); 323 324 return sprintf(buf, "%i\n", card->ipato.enabled? 1:0); 325 } 326 327 static ssize_t qeth_l3_dev_ipato_enable_store(struct device *dev, 328 struct device_attribute *attr, const char *buf, size_t count) 329 { 330 struct qeth_card *card = dev_get_drvdata(dev); 331 bool enable; 332 int rc = 0; 333 334 mutex_lock(&card->conf_mutex); 335 if (card->state != CARD_STATE_DOWN) { 336 rc = -EPERM; 337 goto out; 338 } 339 340 if (sysfs_streq(buf, "toggle")) { 341 enable = !card->ipato.enabled; 342 } else if (kstrtobool(buf, &enable)) { 343 rc = -EINVAL; 344 goto out; 345 } 346 347 if (card->ipato.enabled != enable) { 348 card->ipato.enabled = enable; 349 mutex_lock(&card->ip_lock); 350 qeth_l3_update_ipato(card); 351 mutex_unlock(&card->ip_lock); 352 } 353 out: 354 mutex_unlock(&card->conf_mutex); 355 return rc ? rc : count; 356 } 357 358 static QETH_DEVICE_ATTR(ipato_enable, enable, 0644, 359 qeth_l3_dev_ipato_enable_show, 360 qeth_l3_dev_ipato_enable_store); 361 362 static ssize_t qeth_l3_dev_ipato_invert4_show(struct device *dev, 363 struct device_attribute *attr, char *buf) 364 { 365 struct qeth_card *card = dev_get_drvdata(dev); 366 367 return sprintf(buf, "%i\n", card->ipato.invert4? 1:0); 368 } 369 370 static ssize_t qeth_l3_dev_ipato_invert4_store(struct device *dev, 371 struct device_attribute *attr, 372 const char *buf, size_t count) 373 { 374 struct qeth_card *card = dev_get_drvdata(dev); 375 bool invert; 376 int rc = 0; 377 378 mutex_lock(&card->conf_mutex); 379 if (sysfs_streq(buf, "toggle")) { 380 invert = !card->ipato.invert4; 381 } else if (kstrtobool(buf, &invert)) { 382 rc = -EINVAL; 383 goto out; 384 } 385 386 if (card->ipato.invert4 != invert) { 387 card->ipato.invert4 = invert; 388 mutex_lock(&card->ip_lock); 389 qeth_l3_update_ipato(card); 390 mutex_unlock(&card->ip_lock); 391 } 392 out: 393 mutex_unlock(&card->conf_mutex); 394 return rc ? rc : count; 395 } 396 397 static QETH_DEVICE_ATTR(ipato_invert4, invert4, 0644, 398 qeth_l3_dev_ipato_invert4_show, 399 qeth_l3_dev_ipato_invert4_store); 400 401 static ssize_t qeth_l3_dev_ipato_add_show(char *buf, struct qeth_card *card, 402 enum qeth_prot_versions proto) 403 { 404 struct qeth_ipato_entry *ipatoe; 405 char addr_str[40]; 406 int entry_len; /* length of 1 entry string, differs between v4 and v6 */ 407 int i = 0; 408 409 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40; 410 /* add strlen for "/<mask>\n" */ 411 entry_len += (proto == QETH_PROT_IPV4)? 5 : 6; 412 mutex_lock(&card->ip_lock); 413 list_for_each_entry(ipatoe, &card->ipato.entries, entry) { 414 if (ipatoe->proto != proto) 415 continue; 416 /* String must not be longer than PAGE_SIZE. So we check if 417 * string length gets near PAGE_SIZE. Then we can savely display 418 * the next IPv6 address (worst case, compared to IPv4) */ 419 if ((PAGE_SIZE - i) <= entry_len) 420 break; 421 qeth_l3_ipaddr_to_string(proto, ipatoe->addr, addr_str); 422 i += snprintf(buf + i, PAGE_SIZE - i, 423 "%s/%i\n", addr_str, ipatoe->mask_bits); 424 } 425 mutex_unlock(&card->ip_lock); 426 i += snprintf(buf + i, PAGE_SIZE - i, "\n"); 427 428 return i; 429 } 430 431 static ssize_t qeth_l3_dev_ipato_add4_show(struct device *dev, 432 struct device_attribute *attr, char *buf) 433 { 434 struct qeth_card *card = dev_get_drvdata(dev); 435 436 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV4); 437 } 438 439 static int qeth_l3_parse_ipatoe(const char *buf, enum qeth_prot_versions proto, 440 u8 *addr, int *mask_bits) 441 { 442 const char *start, *end; 443 char *tmp; 444 char buffer[40] = {0, }; 445 446 start = buf; 447 /* get address string */ 448 end = strchr(start, '/'); 449 if (!end || (end - start >= 40)) { 450 return -EINVAL; 451 } 452 strncpy(buffer, start, end - start); 453 if (qeth_l3_string_to_ipaddr(buffer, proto, addr)) { 454 return -EINVAL; 455 } 456 start = end + 1; 457 *mask_bits = simple_strtoul(start, &tmp, 10); 458 if (!strlen(start) || 459 (tmp == start) || 460 (*mask_bits > ((proto == QETH_PROT_IPV4) ? 32 : 128))) { 461 return -EINVAL; 462 } 463 return 0; 464 } 465 466 static ssize_t qeth_l3_dev_ipato_add_store(const char *buf, size_t count, 467 struct qeth_card *card, enum qeth_prot_versions proto) 468 { 469 struct qeth_ipato_entry *ipatoe; 470 u8 addr[16]; 471 int mask_bits; 472 int rc = 0; 473 474 mutex_lock(&card->conf_mutex); 475 rc = qeth_l3_parse_ipatoe(buf, proto, addr, &mask_bits); 476 if (rc) 477 goto out; 478 479 ipatoe = kzalloc(sizeof(struct qeth_ipato_entry), GFP_KERNEL); 480 if (!ipatoe) { 481 rc = -ENOMEM; 482 goto out; 483 } 484 ipatoe->proto = proto; 485 memcpy(ipatoe->addr, addr, (proto == QETH_PROT_IPV4)? 4:16); 486 ipatoe->mask_bits = mask_bits; 487 488 rc = qeth_l3_add_ipato_entry(card, ipatoe); 489 if (rc) 490 kfree(ipatoe); 491 out: 492 mutex_unlock(&card->conf_mutex); 493 return rc ? rc : count; 494 } 495 496 static ssize_t qeth_l3_dev_ipato_add4_store(struct device *dev, 497 struct device_attribute *attr, const char *buf, size_t count) 498 { 499 struct qeth_card *card = dev_get_drvdata(dev); 500 501 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV4); 502 } 503 504 static QETH_DEVICE_ATTR(ipato_add4, add4, 0644, 505 qeth_l3_dev_ipato_add4_show, 506 qeth_l3_dev_ipato_add4_store); 507 508 static ssize_t qeth_l3_dev_ipato_del_store(const char *buf, size_t count, 509 struct qeth_card *card, enum qeth_prot_versions proto) 510 { 511 u8 addr[16]; 512 int mask_bits; 513 int rc = 0; 514 515 mutex_lock(&card->conf_mutex); 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 mutex_unlock(&card->conf_mutex); 520 return rc ? rc : count; 521 } 522 523 static ssize_t qeth_l3_dev_ipato_del4_store(struct device *dev, 524 struct device_attribute *attr, const char *buf, size_t count) 525 { 526 struct qeth_card *card = dev_get_drvdata(dev); 527 528 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV4); 529 } 530 531 static QETH_DEVICE_ATTR(ipato_del4, del4, 0200, NULL, 532 qeth_l3_dev_ipato_del4_store); 533 534 static ssize_t qeth_l3_dev_ipato_invert6_show(struct device *dev, 535 struct device_attribute *attr, char *buf) 536 { 537 struct qeth_card *card = dev_get_drvdata(dev); 538 539 return sprintf(buf, "%i\n", card->ipato.invert6? 1:0); 540 } 541 542 static ssize_t qeth_l3_dev_ipato_invert6_store(struct device *dev, 543 struct device_attribute *attr, const char *buf, size_t count) 544 { 545 struct qeth_card *card = dev_get_drvdata(dev); 546 bool invert; 547 int rc = 0; 548 549 mutex_lock(&card->conf_mutex); 550 if (sysfs_streq(buf, "toggle")) { 551 invert = !card->ipato.invert6; 552 } else if (kstrtobool(buf, &invert)) { 553 rc = -EINVAL; 554 goto out; 555 } 556 557 if (card->ipato.invert6 != invert) { 558 card->ipato.invert6 = invert; 559 mutex_lock(&card->ip_lock); 560 qeth_l3_update_ipato(card); 561 mutex_unlock(&card->ip_lock); 562 } 563 out: 564 mutex_unlock(&card->conf_mutex); 565 return rc ? rc : count; 566 } 567 568 static QETH_DEVICE_ATTR(ipato_invert6, invert6, 0644, 569 qeth_l3_dev_ipato_invert6_show, 570 qeth_l3_dev_ipato_invert6_store); 571 572 573 static ssize_t qeth_l3_dev_ipato_add6_show(struct device *dev, 574 struct device_attribute *attr, char *buf) 575 { 576 struct qeth_card *card = dev_get_drvdata(dev); 577 578 return qeth_l3_dev_ipato_add_show(buf, card, QETH_PROT_IPV6); 579 } 580 581 static ssize_t qeth_l3_dev_ipato_add6_store(struct device *dev, 582 struct device_attribute *attr, const char *buf, size_t count) 583 { 584 struct qeth_card *card = dev_get_drvdata(dev); 585 586 return qeth_l3_dev_ipato_add_store(buf, count, card, QETH_PROT_IPV6); 587 } 588 589 static QETH_DEVICE_ATTR(ipato_add6, add6, 0644, 590 qeth_l3_dev_ipato_add6_show, 591 qeth_l3_dev_ipato_add6_store); 592 593 static ssize_t qeth_l3_dev_ipato_del6_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 return qeth_l3_dev_ipato_del_store(buf, count, card, QETH_PROT_IPV6); 599 } 600 601 static QETH_DEVICE_ATTR(ipato_del6, del6, 0200, NULL, 602 qeth_l3_dev_ipato_del6_store); 603 604 static struct attribute *qeth_ipato_device_attrs[] = { 605 &dev_attr_ipato_enable.attr, 606 &dev_attr_ipato_invert4.attr, 607 &dev_attr_ipato_add4.attr, 608 &dev_attr_ipato_del4.attr, 609 &dev_attr_ipato_invert6.attr, 610 &dev_attr_ipato_add6.attr, 611 &dev_attr_ipato_del6.attr, 612 NULL, 613 }; 614 615 static const struct attribute_group qeth_device_ipato_group = { 616 .name = "ipa_takeover", 617 .attrs = qeth_ipato_device_attrs, 618 }; 619 620 static ssize_t qeth_l3_dev_ip_add_show(struct device *dev, char *buf, 621 enum qeth_prot_versions proto, 622 enum qeth_ip_types type) 623 { 624 struct qeth_card *card = dev_get_drvdata(dev); 625 struct qeth_ipaddr *ipaddr; 626 char addr_str[40]; 627 int str_len = 0; 628 int entry_len; /* length of 1 entry string, differs between v4 and v6 */ 629 int i; 630 631 entry_len = (proto == QETH_PROT_IPV4)? 12 : 40; 632 entry_len += 2; /* \n + terminator */ 633 mutex_lock(&card->ip_lock); 634 hash_for_each(card->ip_htable, i, ipaddr, hnode) { 635 if (ipaddr->proto != proto || ipaddr->type != type) 636 continue; 637 /* String must not be longer than PAGE_SIZE. So we check if 638 * string length gets near PAGE_SIZE. Then we can savely display 639 * the next IPv6 address (worst case, compared to IPv4) */ 640 if ((PAGE_SIZE - str_len) <= entry_len) 641 break; 642 qeth_l3_ipaddr_to_string(proto, (const u8 *)&ipaddr->u, 643 addr_str); 644 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "%s\n", 645 addr_str); 646 } 647 mutex_unlock(&card->ip_lock); 648 str_len += snprintf(buf + str_len, PAGE_SIZE - str_len, "\n"); 649 650 return str_len; 651 } 652 653 static ssize_t qeth_l3_dev_vipa_add4_show(struct device *dev, 654 struct device_attribute *attr, 655 char *buf) 656 { 657 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4, 658 QETH_IP_TYPE_VIPA); 659 } 660 661 static int qeth_l3_parse_vipae(const char *buf, enum qeth_prot_versions proto, 662 u8 *addr) 663 { 664 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) { 665 return -EINVAL; 666 } 667 return 0; 668 } 669 670 static ssize_t qeth_l3_dev_vipa_add_store(const char *buf, size_t count, 671 struct qeth_card *card, enum qeth_prot_versions proto) 672 { 673 u8 addr[16] = {0, }; 674 int rc; 675 676 mutex_lock(&card->conf_mutex); 677 rc = qeth_l3_parse_vipae(buf, proto, addr); 678 if (!rc) 679 rc = qeth_l3_modify_rxip_vipa(card, true, addr, 680 QETH_IP_TYPE_VIPA, proto); 681 mutex_unlock(&card->conf_mutex); 682 return rc ? rc : count; 683 } 684 685 static ssize_t qeth_l3_dev_vipa_add4_store(struct device *dev, 686 struct device_attribute *attr, const char *buf, size_t count) 687 { 688 struct qeth_card *card = dev_get_drvdata(dev); 689 690 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV4); 691 } 692 693 static QETH_DEVICE_ATTR(vipa_add4, add4, 0644, 694 qeth_l3_dev_vipa_add4_show, 695 qeth_l3_dev_vipa_add4_store); 696 697 static ssize_t qeth_l3_dev_vipa_del_store(const char *buf, size_t count, 698 struct qeth_card *card, enum qeth_prot_versions proto) 699 { 700 u8 addr[16]; 701 int rc; 702 703 mutex_lock(&card->conf_mutex); 704 rc = qeth_l3_parse_vipae(buf, proto, addr); 705 if (!rc) 706 rc = qeth_l3_modify_rxip_vipa(card, false, addr, 707 QETH_IP_TYPE_VIPA, proto); 708 mutex_unlock(&card->conf_mutex); 709 return rc ? rc : count; 710 } 711 712 static ssize_t qeth_l3_dev_vipa_del4_store(struct device *dev, 713 struct device_attribute *attr, const char *buf, size_t count) 714 { 715 struct qeth_card *card = dev_get_drvdata(dev); 716 717 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV4); 718 } 719 720 static QETH_DEVICE_ATTR(vipa_del4, del4, 0200, NULL, 721 qeth_l3_dev_vipa_del4_store); 722 723 static ssize_t qeth_l3_dev_vipa_add6_show(struct device *dev, 724 struct device_attribute *attr, 725 char *buf) 726 { 727 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6, 728 QETH_IP_TYPE_VIPA); 729 } 730 731 static ssize_t qeth_l3_dev_vipa_add6_store(struct device *dev, 732 struct device_attribute *attr, const char *buf, size_t count) 733 { 734 struct qeth_card *card = dev_get_drvdata(dev); 735 736 return qeth_l3_dev_vipa_add_store(buf, count, card, QETH_PROT_IPV6); 737 } 738 739 static QETH_DEVICE_ATTR(vipa_add6, add6, 0644, 740 qeth_l3_dev_vipa_add6_show, 741 qeth_l3_dev_vipa_add6_store); 742 743 static ssize_t qeth_l3_dev_vipa_del6_store(struct device *dev, 744 struct device_attribute *attr, const char *buf, size_t count) 745 { 746 struct qeth_card *card = dev_get_drvdata(dev); 747 748 return qeth_l3_dev_vipa_del_store(buf, count, card, QETH_PROT_IPV6); 749 } 750 751 static QETH_DEVICE_ATTR(vipa_del6, del6, 0200, NULL, 752 qeth_l3_dev_vipa_del6_store); 753 754 static struct attribute *qeth_vipa_device_attrs[] = { 755 &dev_attr_vipa_add4.attr, 756 &dev_attr_vipa_del4.attr, 757 &dev_attr_vipa_add6.attr, 758 &dev_attr_vipa_del6.attr, 759 NULL, 760 }; 761 762 static const struct attribute_group qeth_device_vipa_group = { 763 .name = "vipa", 764 .attrs = qeth_vipa_device_attrs, 765 }; 766 767 static ssize_t qeth_l3_dev_rxip_add4_show(struct device *dev, 768 struct device_attribute *attr, 769 char *buf) 770 { 771 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV4, 772 QETH_IP_TYPE_RXIP); 773 } 774 775 static int qeth_l3_parse_rxipe(const char *buf, enum qeth_prot_versions proto, 776 u8 *addr) 777 { 778 __be32 ipv4_addr; 779 struct in6_addr ipv6_addr; 780 781 if (qeth_l3_string_to_ipaddr(buf, proto, addr)) { 782 return -EINVAL; 783 } 784 if (proto == QETH_PROT_IPV4) { 785 memcpy(&ipv4_addr, addr, sizeof(ipv4_addr)); 786 if (ipv4_is_multicast(ipv4_addr)) { 787 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 788 return -EINVAL; 789 } 790 } else if (proto == QETH_PROT_IPV6) { 791 memcpy(&ipv6_addr, addr, sizeof(ipv6_addr)); 792 if (ipv6_addr_is_multicast(&ipv6_addr)) { 793 QETH_DBF_MESSAGE(2, "multicast rxip not supported.\n"); 794 return -EINVAL; 795 } 796 } 797 798 return 0; 799 } 800 801 static ssize_t qeth_l3_dev_rxip_add_store(const char *buf, size_t count, 802 struct qeth_card *card, enum qeth_prot_versions proto) 803 { 804 u8 addr[16] = {0, }; 805 int rc; 806 807 mutex_lock(&card->conf_mutex); 808 rc = qeth_l3_parse_rxipe(buf, proto, addr); 809 if (!rc) 810 rc = qeth_l3_modify_rxip_vipa(card, true, addr, 811 QETH_IP_TYPE_RXIP, proto); 812 mutex_unlock(&card->conf_mutex); 813 return rc ? rc : count; 814 } 815 816 static ssize_t qeth_l3_dev_rxip_add4_store(struct device *dev, 817 struct device_attribute *attr, const char *buf, size_t count) 818 { 819 struct qeth_card *card = dev_get_drvdata(dev); 820 821 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV4); 822 } 823 824 static QETH_DEVICE_ATTR(rxip_add4, add4, 0644, 825 qeth_l3_dev_rxip_add4_show, 826 qeth_l3_dev_rxip_add4_store); 827 828 static ssize_t qeth_l3_dev_rxip_del_store(const char *buf, size_t count, 829 struct qeth_card *card, enum qeth_prot_versions proto) 830 { 831 u8 addr[16]; 832 int rc; 833 834 mutex_lock(&card->conf_mutex); 835 rc = qeth_l3_parse_rxipe(buf, proto, addr); 836 if (!rc) 837 rc = qeth_l3_modify_rxip_vipa(card, false, addr, 838 QETH_IP_TYPE_RXIP, proto); 839 mutex_unlock(&card->conf_mutex); 840 return rc ? rc : count; 841 } 842 843 static ssize_t qeth_l3_dev_rxip_del4_store(struct device *dev, 844 struct device_attribute *attr, const char *buf, size_t count) 845 { 846 struct qeth_card *card = dev_get_drvdata(dev); 847 848 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV4); 849 } 850 851 static QETH_DEVICE_ATTR(rxip_del4, del4, 0200, NULL, 852 qeth_l3_dev_rxip_del4_store); 853 854 static ssize_t qeth_l3_dev_rxip_add6_show(struct device *dev, 855 struct device_attribute *attr, 856 char *buf) 857 { 858 return qeth_l3_dev_ip_add_show(dev, buf, QETH_PROT_IPV6, 859 QETH_IP_TYPE_RXIP); 860 } 861 862 static ssize_t qeth_l3_dev_rxip_add6_store(struct device *dev, 863 struct device_attribute *attr, const char *buf, size_t count) 864 { 865 struct qeth_card *card = dev_get_drvdata(dev); 866 867 return qeth_l3_dev_rxip_add_store(buf, count, card, QETH_PROT_IPV6); 868 } 869 870 static QETH_DEVICE_ATTR(rxip_add6, add6, 0644, 871 qeth_l3_dev_rxip_add6_show, 872 qeth_l3_dev_rxip_add6_store); 873 874 static ssize_t qeth_l3_dev_rxip_del6_store(struct device *dev, 875 struct device_attribute *attr, const char *buf, size_t count) 876 { 877 struct qeth_card *card = dev_get_drvdata(dev); 878 879 return qeth_l3_dev_rxip_del_store(buf, count, card, QETH_PROT_IPV6); 880 } 881 882 static QETH_DEVICE_ATTR(rxip_del6, del6, 0200, NULL, 883 qeth_l3_dev_rxip_del6_store); 884 885 static struct attribute *qeth_rxip_device_attrs[] = { 886 &dev_attr_rxip_add4.attr, 887 &dev_attr_rxip_del4.attr, 888 &dev_attr_rxip_add6.attr, 889 &dev_attr_rxip_del6.attr, 890 NULL, 891 }; 892 893 static const struct attribute_group qeth_device_rxip_group = { 894 .name = "rxip", 895 .attrs = qeth_rxip_device_attrs, 896 }; 897 898 static const struct attribute_group *qeth_l3_only_attr_groups[] = { 899 &qeth_l3_device_attr_group, 900 &qeth_device_ipato_group, 901 &qeth_device_vipa_group, 902 &qeth_device_rxip_group, 903 NULL, 904 }; 905 906 int qeth_l3_create_device_attributes(struct device *dev) 907 { 908 return sysfs_create_groups(&dev->kobj, qeth_l3_only_attr_groups); 909 } 910 911 void qeth_l3_remove_device_attributes(struct device *dev) 912 { 913 sysfs_remove_groups(&dev->kobj, qeth_l3_only_attr_groups); 914 } 915 916 const struct attribute_group *qeth_l3_attr_groups[] = { 917 &qeth_device_attr_group, 918 &qeth_device_blkt_group, 919 /* l3 specific, see qeth_l3_only_attr_groups: */ 920 &qeth_l3_device_attr_group, 921 &qeth_device_ipato_group, 922 &qeth_device_vipa_group, 923 &qeth_device_rxip_group, 924 NULL, 925 }; 926