1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2007, 2009 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 #define KMSG_COMPONENT "qeth" 11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 12 13 #include <linux/module.h> 14 #include <linux/moduleparam.h> 15 #include <linux/bitops.h> 16 #include <linux/string.h> 17 #include <linux/errno.h> 18 #include <linux/kernel.h> 19 #include <linux/etherdevice.h> 20 #include <linux/ip.h> 21 #include <linux/in.h> 22 #include <linux/ipv6.h> 23 #include <linux/inetdevice.h> 24 #include <linux/igmp.h> 25 #include <linux/slab.h> 26 #include <linux/if_ether.h> 27 #include <linux/if_vlan.h> 28 #include <linux/skbuff.h> 29 30 #include <net/ip.h> 31 #include <net/arp.h> 32 #include <net/route.h> 33 #include <net/ipv6.h> 34 #include <net/ip6_route.h> 35 #include <net/iucv/af_iucv.h> 36 #include <linux/hashtable.h> 37 38 #include "qeth_l3.h" 39 40 static int qeth_l3_register_addr_entry(struct qeth_card *, 41 struct qeth_ipaddr *); 42 static int qeth_l3_deregister_addr_entry(struct qeth_card *, 43 struct qeth_ipaddr *); 44 45 int qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const u8 *addr, 46 char *buf) 47 { 48 if (proto == QETH_PROT_IPV4) 49 return sprintf(buf, "%pI4", addr); 50 else 51 return sprintf(buf, "%pI6", addr); 52 } 53 54 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card, 55 struct qeth_ipaddr *query) 56 { 57 u32 key = qeth_l3_ipaddr_hash(query); 58 struct qeth_ipaddr *addr; 59 60 if (query->is_multicast) { 61 hash_for_each_possible(card->rx_mode_addrs, addr, hnode, key) 62 if (qeth_l3_addr_match_ip(addr, query)) 63 return addr; 64 } else { 65 hash_for_each_possible(card->ip_htable, addr, hnode, key) 66 if (qeth_l3_addr_match_ip(addr, query)) 67 return addr; 68 } 69 return NULL; 70 } 71 72 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len) 73 { 74 int i, j; 75 u8 octet; 76 77 for (i = 0; i < len; ++i) { 78 octet = addr[i]; 79 for (j = 7; j >= 0; --j) { 80 bits[i*8 + j] = octet & 1; 81 octet >>= 1; 82 } 83 } 84 } 85 86 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card, 87 struct qeth_ipaddr *addr) 88 { 89 struct qeth_ipato_entry *ipatoe; 90 u8 addr_bits[128] = {0, }; 91 u8 ipatoe_bits[128] = {0, }; 92 int rc = 0; 93 94 if (!card->ipato.enabled) 95 return false; 96 if (addr->type != QETH_IP_TYPE_NORMAL) 97 return false; 98 99 qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits, 100 (addr->proto == QETH_PROT_IPV4) ? 4 : 16); 101 list_for_each_entry(ipatoe, &card->ipato.entries, entry) { 102 if (addr->proto != ipatoe->proto) 103 continue; 104 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits, 105 (ipatoe->proto == QETH_PROT_IPV4) ? 106 4 : 16); 107 rc = !memcmp(addr_bits, ipatoe_bits, ipatoe->mask_bits); 108 if (rc) 109 break; 110 } 111 /* invert? */ 112 if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4) 113 rc = !rc; 114 else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6) 115 rc = !rc; 116 117 return rc; 118 } 119 120 static int qeth_l3_delete_ip(struct qeth_card *card, 121 struct qeth_ipaddr *tmp_addr) 122 { 123 int rc = 0; 124 struct qeth_ipaddr *addr; 125 126 if (tmp_addr->type == QETH_IP_TYPE_RXIP) 127 QETH_CARD_TEXT(card, 2, "delrxip"); 128 else if (tmp_addr->type == QETH_IP_TYPE_VIPA) 129 QETH_CARD_TEXT(card, 2, "delvipa"); 130 else 131 QETH_CARD_TEXT(card, 2, "delip"); 132 133 if (tmp_addr->proto == QETH_PROT_IPV4) 134 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4); 135 else { 136 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8); 137 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8); 138 } 139 140 addr = qeth_l3_find_addr_by_ip(card, tmp_addr); 141 if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr)) 142 return -ENOENT; 143 144 addr->ref_counter--; 145 if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0) 146 return rc; 147 148 if (qeth_card_hw_is_reachable(card)) 149 rc = qeth_l3_deregister_addr_entry(card, addr); 150 151 hash_del(&addr->hnode); 152 kfree(addr); 153 154 return rc; 155 } 156 157 static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr) 158 { 159 int rc = 0; 160 struct qeth_ipaddr *addr; 161 char buf[40]; 162 163 if (tmp_addr->type == QETH_IP_TYPE_RXIP) 164 QETH_CARD_TEXT(card, 2, "addrxip"); 165 else if (tmp_addr->type == QETH_IP_TYPE_VIPA) 166 QETH_CARD_TEXT(card, 2, "addvipa"); 167 else 168 QETH_CARD_TEXT(card, 2, "addip"); 169 170 if (tmp_addr->proto == QETH_PROT_IPV4) 171 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4); 172 else { 173 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8); 174 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8); 175 } 176 177 addr = qeth_l3_find_addr_by_ip(card, tmp_addr); 178 if (addr) { 179 if (tmp_addr->type != QETH_IP_TYPE_NORMAL) 180 return -EADDRINUSE; 181 if (qeth_l3_addr_match_all(addr, tmp_addr)) { 182 addr->ref_counter++; 183 return 0; 184 } 185 qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u, 186 buf); 187 dev_warn(&card->gdev->dev, 188 "Registering IP address %s failed\n", buf); 189 return -EADDRINUSE; 190 } else { 191 addr = kmemdup(tmp_addr, sizeof(*tmp_addr), GFP_KERNEL); 192 if (!addr) 193 return -ENOMEM; 194 195 if (qeth_l3_is_addr_covered_by_ipato(card, addr)) { 196 QETH_CARD_TEXT(card, 2, "tkovaddr"); 197 addr->ipato = 1; 198 } 199 hash_add(card->ip_htable, &addr->hnode, 200 qeth_l3_ipaddr_hash(addr)); 201 202 if (!qeth_card_hw_is_reachable(card)) { 203 addr->disp_flag = QETH_DISP_ADDR_ADD; 204 return 0; 205 } 206 207 rc = qeth_l3_register_addr_entry(card, addr); 208 209 if (!rc || rc == -EADDRINUSE || rc == -ENETDOWN) { 210 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 211 } else { 212 hash_del(&addr->hnode); 213 kfree(addr); 214 } 215 } 216 return rc; 217 } 218 219 static int qeth_l3_modify_ip(struct qeth_card *card, struct qeth_ipaddr *addr, 220 bool add) 221 { 222 int rc; 223 224 mutex_lock(&card->ip_lock); 225 rc = add ? qeth_l3_add_ip(card, addr) : qeth_l3_delete_ip(card, addr); 226 mutex_unlock(&card->ip_lock); 227 228 return rc; 229 } 230 231 static void qeth_l3_drain_rx_mode_cache(struct qeth_card *card) 232 { 233 struct qeth_ipaddr *addr; 234 struct hlist_node *tmp; 235 int i; 236 237 hash_for_each_safe(card->rx_mode_addrs, i, tmp, addr, hnode) { 238 hash_del(&addr->hnode); 239 kfree(addr); 240 } 241 } 242 243 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover) 244 { 245 struct qeth_ipaddr *addr; 246 struct hlist_node *tmp; 247 int i; 248 249 QETH_CARD_TEXT(card, 4, "clearip"); 250 251 mutex_lock(&card->ip_lock); 252 253 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) { 254 if (!recover) { 255 hash_del(&addr->hnode); 256 kfree(addr); 257 continue; 258 } 259 addr->disp_flag = QETH_DISP_ADDR_ADD; 260 } 261 262 mutex_unlock(&card->ip_lock); 263 } 264 265 static void qeth_l3_recover_ip(struct qeth_card *card) 266 { 267 struct qeth_ipaddr *addr; 268 struct hlist_node *tmp; 269 int i; 270 int rc; 271 272 QETH_CARD_TEXT(card, 4, "recovrip"); 273 274 mutex_lock(&card->ip_lock); 275 276 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) { 277 if (addr->disp_flag == QETH_DISP_ADDR_ADD) { 278 rc = qeth_l3_register_addr_entry(card, addr); 279 280 if (!rc) { 281 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 282 } else { 283 hash_del(&addr->hnode); 284 kfree(addr); 285 } 286 } 287 } 288 289 mutex_unlock(&card->ip_lock); 290 } 291 292 static int qeth_l3_setdelip_cb(struct qeth_card *card, struct qeth_reply *reply, 293 unsigned long data) 294 { 295 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data; 296 297 switch (cmd->hdr.return_code) { 298 case IPA_RC_SUCCESS: 299 return 0; 300 case IPA_RC_DUPLICATE_IP_ADDRESS: 301 return -EADDRINUSE; 302 case IPA_RC_MC_ADDR_NOT_FOUND: 303 return -ENOENT; 304 case IPA_RC_LAN_OFFLINE: 305 return -ENETDOWN; 306 default: 307 return -EIO; 308 } 309 } 310 311 static int qeth_l3_send_setdelmc(struct qeth_card *card, 312 struct qeth_ipaddr *addr, 313 enum qeth_ipa_cmds ipacmd) 314 { 315 struct qeth_cmd_buffer *iob; 316 struct qeth_ipa_cmd *cmd; 317 318 QETH_CARD_TEXT(card, 4, "setdelmc"); 319 320 iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto, 321 IPA_DATA_SIZEOF(setdelipm)); 322 if (!iob) 323 return -ENOMEM; 324 cmd = __ipa_cmd(iob); 325 if (addr->proto == QETH_PROT_IPV6) { 326 cmd->data.setdelipm.ip = addr->u.a6.addr; 327 ipv6_eth_mc_map(&addr->u.a6.addr, cmd->data.setdelipm.mac); 328 } else { 329 cmd->data.setdelipm.ip.s6_addr32[3] = addr->u.a4.addr; 330 ip_eth_mc_map(addr->u.a4.addr, cmd->data.setdelipm.mac); 331 } 332 333 return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL); 334 } 335 336 static void qeth_l3_set_ipv6_prefix(struct in6_addr *prefix, unsigned int len) 337 { 338 unsigned int i = 0; 339 340 while (len && i < 4) { 341 int mask_len = min_t(int, len, 32); 342 343 prefix->s6_addr32[i] = inet_make_mask(mask_len); 344 len -= mask_len; 345 i++; 346 } 347 } 348 349 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set) 350 { 351 switch (addr->type) { 352 case QETH_IP_TYPE_RXIP: 353 return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0; 354 case QETH_IP_TYPE_VIPA: 355 return (set) ? QETH_IPA_SETIP_VIPA_FLAG : 356 QETH_IPA_DELIP_VIPA_FLAG; 357 default: 358 return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0; 359 } 360 } 361 362 static int qeth_l3_send_setdelip(struct qeth_card *card, 363 struct qeth_ipaddr *addr, 364 enum qeth_ipa_cmds ipacmd) 365 { 366 struct qeth_cmd_buffer *iob; 367 struct qeth_ipa_cmd *cmd; 368 u32 flags; 369 370 QETH_CARD_TEXT(card, 4, "setdelip"); 371 372 iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto, 373 IPA_DATA_SIZEOF(setdelip6)); 374 if (!iob) 375 return -ENOMEM; 376 cmd = __ipa_cmd(iob); 377 378 flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP); 379 QETH_CARD_TEXT_(card, 4, "flags%02X", flags); 380 381 if (addr->proto == QETH_PROT_IPV6) { 382 cmd->data.setdelip6.addr = addr->u.a6.addr; 383 qeth_l3_set_ipv6_prefix(&cmd->data.setdelip6.prefix, 384 addr->u.a6.pfxlen); 385 cmd->data.setdelip6.flags = flags; 386 } else { 387 cmd->data.setdelip4.addr = addr->u.a4.addr; 388 cmd->data.setdelip4.mask = addr->u.a4.mask; 389 cmd->data.setdelip4.flags = flags; 390 } 391 392 return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL); 393 } 394 395 static int qeth_l3_send_setrouting(struct qeth_card *card, 396 enum qeth_routing_types type, enum qeth_prot_versions prot) 397 { 398 int rc; 399 struct qeth_ipa_cmd *cmd; 400 struct qeth_cmd_buffer *iob; 401 402 QETH_CARD_TEXT(card, 4, "setroutg"); 403 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETRTG, prot, 404 IPA_DATA_SIZEOF(setrtg)); 405 if (!iob) 406 return -ENOMEM; 407 cmd = __ipa_cmd(iob); 408 cmd->data.setrtg.type = (type); 409 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL); 410 411 return rc; 412 } 413 414 static int qeth_l3_correct_routing_type(struct qeth_card *card, 415 enum qeth_routing_types *type, enum qeth_prot_versions prot) 416 { 417 if (IS_IQD(card)) { 418 switch (*type) { 419 case NO_ROUTER: 420 case PRIMARY_CONNECTOR: 421 case SECONDARY_CONNECTOR: 422 case MULTICAST_ROUTER: 423 return 0; 424 default: 425 goto out_inval; 426 } 427 } else { 428 switch (*type) { 429 case NO_ROUTER: 430 case PRIMARY_ROUTER: 431 case SECONDARY_ROUTER: 432 return 0; 433 case MULTICAST_ROUTER: 434 if (qeth_is_ipafunc_supported(card, prot, 435 IPA_OSA_MC_ROUTER)) 436 return 0; 437 default: 438 goto out_inval; 439 } 440 } 441 out_inval: 442 *type = NO_ROUTER; 443 return -EINVAL; 444 } 445 446 int qeth_l3_setrouting_v4(struct qeth_card *card) 447 { 448 int rc; 449 450 QETH_CARD_TEXT(card, 3, "setrtg4"); 451 452 rc = qeth_l3_correct_routing_type(card, &card->options.route4.type, 453 QETH_PROT_IPV4); 454 if (rc) 455 return rc; 456 457 rc = qeth_l3_send_setrouting(card, card->options.route4.type, 458 QETH_PROT_IPV4); 459 if (rc) { 460 card->options.route4.type = NO_ROUTER; 461 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n", 462 rc, CARD_DEVID(card)); 463 } 464 return rc; 465 } 466 467 int qeth_l3_setrouting_v6(struct qeth_card *card) 468 { 469 int rc = 0; 470 471 QETH_CARD_TEXT(card, 3, "setrtg6"); 472 473 if (!qeth_is_supported(card, IPA_IPV6)) 474 return 0; 475 rc = qeth_l3_correct_routing_type(card, &card->options.route6.type, 476 QETH_PROT_IPV6); 477 if (rc) 478 return rc; 479 480 rc = qeth_l3_send_setrouting(card, card->options.route6.type, 481 QETH_PROT_IPV6); 482 if (rc) { 483 card->options.route6.type = NO_ROUTER; 484 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n", 485 rc, CARD_DEVID(card)); 486 } 487 return rc; 488 } 489 490 /* 491 * IP address takeover related functions 492 */ 493 494 /** 495 * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs. 496 * 497 * Caller must hold ip_lock. 498 */ 499 void qeth_l3_update_ipato(struct qeth_card *card) 500 { 501 struct qeth_ipaddr *addr; 502 unsigned int i; 503 504 hash_for_each(card->ip_htable, i, addr, hnode) { 505 if (addr->type != QETH_IP_TYPE_NORMAL) 506 continue; 507 addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr); 508 } 509 } 510 511 static void qeth_l3_clear_ipato_list(struct qeth_card *card) 512 { 513 struct qeth_ipato_entry *ipatoe, *tmp; 514 515 mutex_lock(&card->ip_lock); 516 517 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) { 518 list_del(&ipatoe->entry); 519 kfree(ipatoe); 520 } 521 522 qeth_l3_update_ipato(card); 523 mutex_unlock(&card->ip_lock); 524 } 525 526 int qeth_l3_add_ipato_entry(struct qeth_card *card, 527 struct qeth_ipato_entry *new) 528 { 529 struct qeth_ipato_entry *ipatoe; 530 int rc = 0; 531 532 QETH_CARD_TEXT(card, 2, "addipato"); 533 534 mutex_lock(&card->ip_lock); 535 536 list_for_each_entry(ipatoe, &card->ipato.entries, entry) { 537 if (ipatoe->proto != new->proto) 538 continue; 539 if (!memcmp(ipatoe->addr, new->addr, 540 (ipatoe->proto == QETH_PROT_IPV4) ? 4 : 16) && 541 (ipatoe->mask_bits == new->mask_bits)) { 542 rc = -EEXIST; 543 break; 544 } 545 } 546 547 if (!rc) { 548 list_add_tail(&new->entry, &card->ipato.entries); 549 qeth_l3_update_ipato(card); 550 } 551 552 mutex_unlock(&card->ip_lock); 553 554 return rc; 555 } 556 557 int qeth_l3_del_ipato_entry(struct qeth_card *card, 558 enum qeth_prot_versions proto, u8 *addr, 559 unsigned int mask_bits) 560 { 561 struct qeth_ipato_entry *ipatoe, *tmp; 562 int rc = -ENOENT; 563 564 QETH_CARD_TEXT(card, 2, "delipato"); 565 566 mutex_lock(&card->ip_lock); 567 568 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) { 569 if (ipatoe->proto != proto) 570 continue; 571 if (!memcmp(ipatoe->addr, addr, 572 (proto == QETH_PROT_IPV4) ? 4 : 16) && 573 (ipatoe->mask_bits == mask_bits)) { 574 list_del(&ipatoe->entry); 575 qeth_l3_update_ipato(card); 576 kfree(ipatoe); 577 rc = 0; 578 } 579 } 580 581 mutex_unlock(&card->ip_lock); 582 583 return rc; 584 } 585 586 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip, 587 enum qeth_ip_types type, 588 enum qeth_prot_versions proto) 589 { 590 struct qeth_ipaddr addr; 591 592 qeth_l3_init_ipaddr(&addr, type, proto); 593 if (proto == QETH_PROT_IPV4) 594 memcpy(&addr.u.a4.addr, ip, 4); 595 else 596 memcpy(&addr.u.a6.addr, ip, 16); 597 598 return qeth_l3_modify_ip(card, &addr, add); 599 } 600 601 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add) 602 { 603 struct qeth_ipaddr addr; 604 unsigned int i; 605 606 qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6); 607 addr.u.a6.addr.s6_addr[0] = 0xfe; 608 addr.u.a6.addr.s6_addr[1] = 0x80; 609 for (i = 0; i < 8; i++) 610 addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i]; 611 612 return qeth_l3_modify_ip(card, &addr, add); 613 } 614 615 static int qeth_l3_register_addr_entry(struct qeth_card *card, 616 struct qeth_ipaddr *addr) 617 { 618 char buf[50]; 619 int rc = 0; 620 int cnt = 3; 621 622 if (card->options.sniffer) 623 return 0; 624 625 if (addr->proto == QETH_PROT_IPV4) { 626 QETH_CARD_TEXT(card, 2, "setaddr4"); 627 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int)); 628 } else if (addr->proto == QETH_PROT_IPV6) { 629 QETH_CARD_TEXT(card, 2, "setaddr6"); 630 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8); 631 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8); 632 } else { 633 QETH_CARD_TEXT(card, 2, "setaddr?"); 634 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr)); 635 } 636 do { 637 if (addr->is_multicast) 638 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM); 639 else 640 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP); 641 if (rc) 642 QETH_CARD_TEXT(card, 2, "failed"); 643 } while ((--cnt > 0) && rc); 644 if (rc) { 645 QETH_CARD_TEXT(card, 2, "FAILED"); 646 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf); 647 dev_warn(&card->gdev->dev, 648 "Registering IP address %s failed\n", buf); 649 } 650 return rc; 651 } 652 653 static int qeth_l3_deregister_addr_entry(struct qeth_card *card, 654 struct qeth_ipaddr *addr) 655 { 656 int rc = 0; 657 658 if (card->options.sniffer) 659 return 0; 660 661 if (addr->proto == QETH_PROT_IPV4) { 662 QETH_CARD_TEXT(card, 2, "deladdr4"); 663 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int)); 664 } else if (addr->proto == QETH_PROT_IPV6) { 665 QETH_CARD_TEXT(card, 2, "deladdr6"); 666 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8); 667 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8); 668 } else { 669 QETH_CARD_TEXT(card, 2, "deladdr?"); 670 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr)); 671 } 672 if (addr->is_multicast) 673 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM); 674 else 675 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP); 676 if (rc) 677 QETH_CARD_TEXT(card, 2, "failed"); 678 679 return rc; 680 } 681 682 static int qeth_l3_setadapter_parms(struct qeth_card *card) 683 { 684 int rc = 0; 685 686 QETH_CARD_TEXT(card, 2, "setadprm"); 687 688 if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) { 689 rc = qeth_setadpparms_change_macaddr(card); 690 if (rc) 691 dev_warn(&card->gdev->dev, "Reading the adapter MAC" 692 " address failed\n"); 693 } 694 695 return rc; 696 } 697 698 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card) 699 { 700 int rc; 701 702 QETH_CARD_TEXT(card, 3, "ipaarp"); 703 704 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 705 dev_info(&card->gdev->dev, 706 "ARP processing not supported on %s!\n", 707 netdev_name(card->dev)); 708 return 0; 709 } 710 rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING, 711 IPA_CMD_ASS_START, NULL); 712 if (rc) { 713 dev_warn(&card->gdev->dev, 714 "Starting ARP processing support for %s failed\n", 715 netdev_name(card->dev)); 716 } 717 return rc; 718 } 719 720 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card) 721 { 722 int rc; 723 724 QETH_CARD_TEXT(card, 3, "stsrcmac"); 725 726 if (!qeth_is_supported(card, IPA_SOURCE_MAC)) { 727 dev_info(&card->gdev->dev, 728 "Inbound source MAC-address not supported on %s\n", 729 netdev_name(card->dev)); 730 return -EOPNOTSUPP; 731 } 732 733 rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC, 734 IPA_CMD_ASS_START, NULL); 735 if (rc) 736 dev_warn(&card->gdev->dev, 737 "Starting source MAC-address support for %s failed\n", 738 netdev_name(card->dev)); 739 return rc; 740 } 741 742 static int qeth_l3_start_ipa_vlan(struct qeth_card *card) 743 { 744 int rc = 0; 745 746 QETH_CARD_TEXT(card, 3, "strtvlan"); 747 748 if (!qeth_is_supported(card, IPA_FULL_VLAN)) { 749 dev_info(&card->gdev->dev, 750 "VLAN not supported on %s\n", netdev_name(card->dev)); 751 return -EOPNOTSUPP; 752 } 753 754 rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO, 755 IPA_CMD_ASS_START, NULL); 756 if (rc) { 757 dev_warn(&card->gdev->dev, 758 "Starting VLAN support for %s failed\n", 759 netdev_name(card->dev)); 760 } else { 761 dev_info(&card->gdev->dev, "VLAN enabled\n"); 762 } 763 return rc; 764 } 765 766 static int qeth_l3_start_ipa_multicast(struct qeth_card *card) 767 { 768 int rc; 769 770 QETH_CARD_TEXT(card, 3, "stmcast"); 771 772 if (!qeth_is_supported(card, IPA_MULTICASTING)) { 773 dev_info(&card->gdev->dev, 774 "Multicast not supported on %s\n", 775 netdev_name(card->dev)); 776 return -EOPNOTSUPP; 777 } 778 779 rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING, 780 IPA_CMD_ASS_START, NULL); 781 if (rc) { 782 dev_warn(&card->gdev->dev, 783 "Starting multicast support for %s failed\n", 784 netdev_name(card->dev)); 785 } else { 786 dev_info(&card->gdev->dev, "Multicast enabled\n"); 787 card->dev->flags |= IFF_MULTICAST; 788 } 789 return rc; 790 } 791 792 static int qeth_l3_softsetup_ipv6(struct qeth_card *card) 793 { 794 u32 ipv6_data = 3; 795 int rc; 796 797 QETH_CARD_TEXT(card, 3, "softipv6"); 798 799 if (IS_IQD(card)) 800 goto out; 801 802 rc = qeth_send_simple_setassparms(card, IPA_IPV6, IPA_CMD_ASS_START, 803 &ipv6_data); 804 if (rc) { 805 dev_err(&card->gdev->dev, 806 "Activating IPv6 support for %s failed\n", 807 netdev_name(card->dev)); 808 return rc; 809 } 810 rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6, IPA_CMD_ASS_START, 811 NULL); 812 if (rc) { 813 dev_err(&card->gdev->dev, 814 "Activating IPv6 support for %s failed\n", 815 netdev_name(card->dev)); 816 return rc; 817 } 818 rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU, 819 IPA_CMD_ASS_START, NULL); 820 if (rc) { 821 dev_warn(&card->gdev->dev, 822 "Enabling the passthrough mode for %s failed\n", 823 netdev_name(card->dev)); 824 return rc; 825 } 826 out: 827 dev_info(&card->gdev->dev, "IPV6 enabled\n"); 828 return 0; 829 } 830 831 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card) 832 { 833 QETH_CARD_TEXT(card, 3, "strtipv6"); 834 835 if (!qeth_is_supported(card, IPA_IPV6)) { 836 dev_info(&card->gdev->dev, 837 "IPv6 not supported on %s\n", netdev_name(card->dev)); 838 return 0; 839 } 840 return qeth_l3_softsetup_ipv6(card); 841 } 842 843 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card) 844 { 845 u32 filter_data = 1; 846 int rc; 847 848 QETH_CARD_TEXT(card, 3, "stbrdcst"); 849 card->info.broadcast_capable = 0; 850 if (!qeth_is_supported(card, IPA_FILTERING)) { 851 dev_info(&card->gdev->dev, 852 "Broadcast not supported on %s\n", 853 netdev_name(card->dev)); 854 rc = -EOPNOTSUPP; 855 goto out; 856 } 857 rc = qeth_send_simple_setassparms(card, IPA_FILTERING, 858 IPA_CMD_ASS_START, NULL); 859 if (rc) { 860 dev_warn(&card->gdev->dev, 861 "Enabling broadcast filtering for %s failed\n", 862 netdev_name(card->dev)); 863 goto out; 864 } 865 866 rc = qeth_send_simple_setassparms(card, IPA_FILTERING, 867 IPA_CMD_ASS_CONFIGURE, &filter_data); 868 if (rc) { 869 dev_warn(&card->gdev->dev, 870 "Setting up broadcast filtering for %s failed\n", 871 netdev_name(card->dev)); 872 goto out; 873 } 874 card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO; 875 dev_info(&card->gdev->dev, "Broadcast enabled\n"); 876 rc = qeth_send_simple_setassparms(card, IPA_FILTERING, 877 IPA_CMD_ASS_ENABLE, &filter_data); 878 if (rc) { 879 dev_warn(&card->gdev->dev, 880 "Setting up broadcast echo filtering for %s failed\n", 881 netdev_name(card->dev)); 882 goto out; 883 } 884 card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO; 885 out: 886 if (card->info.broadcast_capable) 887 card->dev->flags |= IFF_BROADCAST; 888 else 889 card->dev->flags &= ~IFF_BROADCAST; 890 return rc; 891 } 892 893 static void qeth_l3_start_ipassists(struct qeth_card *card) 894 { 895 QETH_CARD_TEXT(card, 3, "strtipas"); 896 897 qeth_l3_start_ipa_arp_processing(card); /* go on*/ 898 qeth_l3_start_ipa_source_mac(card); /* go on*/ 899 qeth_l3_start_ipa_vlan(card); /* go on*/ 900 qeth_l3_start_ipa_multicast(card); /* go on*/ 901 qeth_l3_start_ipa_ipv6(card); /* go on*/ 902 qeth_l3_start_ipa_broadcast(card); /* go on*/ 903 } 904 905 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card, 906 struct qeth_reply *reply, unsigned long data) 907 { 908 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data; 909 910 if (cmd->hdr.return_code) 911 return -EIO; 912 if (!is_valid_ether_addr(cmd->data.create_destroy_addr.mac_addr)) 913 return -EADDRNOTAVAIL; 914 915 ether_addr_copy(card->dev->dev_addr, 916 cmd->data.create_destroy_addr.mac_addr); 917 return 0; 918 } 919 920 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card) 921 { 922 int rc = 0; 923 struct qeth_cmd_buffer *iob; 924 925 QETH_CARD_TEXT(card, 2, "hsrmac"); 926 927 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6, 928 IPA_DATA_SIZEOF(create_destroy_addr)); 929 if (!iob) 930 return -ENOMEM; 931 932 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb, 933 NULL); 934 return rc; 935 } 936 937 static int qeth_l3_get_unique_id_cb(struct qeth_card *card, 938 struct qeth_reply *reply, unsigned long data) 939 { 940 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data; 941 u16 *uid = reply->param; 942 943 if (cmd->hdr.return_code == 0) { 944 *uid = cmd->data.create_destroy_addr.uid; 945 return 0; 946 } 947 948 dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n"); 949 return -EIO; 950 } 951 952 static u16 qeth_l3_get_unique_id(struct qeth_card *card, u16 uid) 953 { 954 struct qeth_cmd_buffer *iob; 955 956 QETH_CARD_TEXT(card, 2, "guniqeid"); 957 958 if (!qeth_is_supported(card, IPA_IPV6)) 959 goto out; 960 961 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6, 962 IPA_DATA_SIZEOF(create_destroy_addr)); 963 if (!iob) 964 goto out; 965 966 __ipa_cmd(iob)->data.create_destroy_addr.uid = uid; 967 qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, &uid); 968 969 out: 970 return uid; 971 } 972 973 static int 974 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply, 975 unsigned long data) 976 { 977 struct qeth_ipa_cmd *cmd; 978 __u16 rc; 979 980 QETH_CARD_TEXT(card, 2, "diastrcb"); 981 982 cmd = (struct qeth_ipa_cmd *)data; 983 rc = cmd->hdr.return_code; 984 if (rc) 985 QETH_CARD_TEXT_(card, 2, "dxter%x", rc); 986 switch (cmd->data.diagass.action) { 987 case QETH_DIAGS_CMD_TRACE_QUERY: 988 break; 989 case QETH_DIAGS_CMD_TRACE_DISABLE: 990 switch (rc) { 991 case 0: 992 case IPA_RC_INVALID_SUBCMD: 993 card->info.promisc_mode = SET_PROMISC_MODE_OFF; 994 dev_info(&card->gdev->dev, "The HiperSockets network " 995 "traffic analyzer is deactivated\n"); 996 break; 997 default: 998 break; 999 } 1000 break; 1001 case QETH_DIAGS_CMD_TRACE_ENABLE: 1002 switch (rc) { 1003 case 0: 1004 card->info.promisc_mode = SET_PROMISC_MODE_ON; 1005 dev_info(&card->gdev->dev, "The HiperSockets network " 1006 "traffic analyzer is activated\n"); 1007 break; 1008 case IPA_RC_HARDWARE_AUTH_ERROR: 1009 dev_warn(&card->gdev->dev, "The device is not " 1010 "authorized to run as a HiperSockets network " 1011 "traffic analyzer\n"); 1012 break; 1013 case IPA_RC_TRACE_ALREADY_ACTIVE: 1014 dev_warn(&card->gdev->dev, "A HiperSockets " 1015 "network traffic analyzer is already " 1016 "active in the HiperSockets LAN\n"); 1017 break; 1018 default: 1019 break; 1020 } 1021 break; 1022 default: 1023 QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n", 1024 cmd->data.diagass.action, CARD_DEVID(card)); 1025 } 1026 1027 return rc ? -EIO : 0; 1028 } 1029 1030 static int 1031 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd) 1032 { 1033 struct qeth_cmd_buffer *iob; 1034 struct qeth_ipa_cmd *cmd; 1035 1036 QETH_CARD_TEXT(card, 2, "diagtrac"); 1037 1038 iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0); 1039 if (!iob) 1040 return -ENOMEM; 1041 cmd = __ipa_cmd(iob); 1042 cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET; 1043 cmd->data.diagass.action = diags_cmd; 1044 return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL); 1045 } 1046 1047 static int qeth_l3_add_mcast_rtnl(struct net_device *dev, int vid, void *arg) 1048 { 1049 struct qeth_card *card = arg; 1050 struct inet6_dev *in6_dev; 1051 struct in_device *in4_dev; 1052 struct qeth_ipaddr *ipm; 1053 struct qeth_ipaddr tmp; 1054 struct ip_mc_list *im4; 1055 struct ifmcaddr6 *im6; 1056 1057 QETH_CARD_TEXT(card, 4, "addmc"); 1058 1059 if (!dev || !(dev->flags & IFF_UP)) 1060 goto out; 1061 1062 in4_dev = __in_dev_get_rtnl(dev); 1063 if (!in4_dev) 1064 goto walk_ipv6; 1065 1066 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4); 1067 tmp.disp_flag = QETH_DISP_ADDR_ADD; 1068 tmp.is_multicast = 1; 1069 1070 for (im4 = rtnl_dereference(in4_dev->mc_list); im4 != NULL; 1071 im4 = rtnl_dereference(im4->next_rcu)) { 1072 tmp.u.a4.addr = im4->multiaddr; 1073 1074 ipm = qeth_l3_find_addr_by_ip(card, &tmp); 1075 if (ipm) { 1076 /* for mcast, by-IP match means full match */ 1077 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 1078 continue; 1079 } 1080 1081 ipm = kmemdup(&tmp, sizeof(tmp), GFP_KERNEL); 1082 if (!ipm) 1083 continue; 1084 1085 hash_add(card->rx_mode_addrs, &ipm->hnode, 1086 qeth_l3_ipaddr_hash(ipm)); 1087 } 1088 1089 walk_ipv6: 1090 if (!qeth_is_supported(card, IPA_IPV6)) 1091 goto out; 1092 1093 in6_dev = __in6_dev_get(dev); 1094 if (!in6_dev) 1095 goto out; 1096 1097 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6); 1098 tmp.disp_flag = QETH_DISP_ADDR_ADD; 1099 tmp.is_multicast = 1; 1100 1101 read_lock_bh(&in6_dev->lock); 1102 for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) { 1103 tmp.u.a6.addr = im6->mca_addr; 1104 1105 ipm = qeth_l3_find_addr_by_ip(card, &tmp); 1106 if (ipm) { 1107 /* for mcast, by-IP match means full match */ 1108 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 1109 continue; 1110 } 1111 1112 ipm = kmemdup(&tmp, sizeof(tmp), GFP_ATOMIC); 1113 if (!ipm) 1114 continue; 1115 1116 hash_add(card->rx_mode_addrs, &ipm->hnode, 1117 qeth_l3_ipaddr_hash(ipm)); 1118 1119 } 1120 read_unlock_bh(&in6_dev->lock); 1121 1122 out: 1123 return 0; 1124 } 1125 1126 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev, 1127 __be16 proto, u16 vid) 1128 { 1129 struct qeth_card *card = dev->ml_priv; 1130 1131 QETH_CARD_TEXT_(card, 4, "aid:%d", vid); 1132 return 0; 1133 } 1134 1135 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev, 1136 __be16 proto, u16 vid) 1137 { 1138 struct qeth_card *card = dev->ml_priv; 1139 1140 QETH_CARD_TEXT_(card, 4, "kid:%d", vid); 1141 return 0; 1142 } 1143 1144 static void qeth_l3_set_promisc_mode(struct qeth_card *card) 1145 { 1146 bool enable = card->dev->flags & IFF_PROMISC; 1147 1148 if (card->info.promisc_mode == enable) 1149 return; 1150 1151 if (IS_VM_NIC(card)) { /* Guestlan trace */ 1152 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) 1153 qeth_setadp_promisc_mode(card, enable); 1154 } else if (card->options.sniffer && /* HiperSockets trace */ 1155 qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) { 1156 if (enable) { 1157 QETH_CARD_TEXT(card, 3, "+promisc"); 1158 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE); 1159 } else { 1160 QETH_CARD_TEXT(card, 3, "-promisc"); 1161 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE); 1162 } 1163 } 1164 } 1165 1166 static void qeth_l3_rx_mode_work(struct work_struct *work) 1167 { 1168 struct qeth_card *card = container_of(work, struct qeth_card, 1169 rx_mode_work); 1170 struct qeth_ipaddr *addr; 1171 struct hlist_node *tmp; 1172 int i, rc; 1173 1174 QETH_CARD_TEXT(card, 3, "setmulti"); 1175 1176 if (!card->options.sniffer) { 1177 rtnl_lock(); 1178 qeth_l3_add_mcast_rtnl(card->dev, 0, card); 1179 if (qeth_is_supported(card, IPA_FULL_VLAN)) 1180 vlan_for_each(card->dev, qeth_l3_add_mcast_rtnl, card); 1181 rtnl_unlock(); 1182 1183 hash_for_each_safe(card->rx_mode_addrs, i, tmp, addr, hnode) { 1184 switch (addr->disp_flag) { 1185 case QETH_DISP_ADDR_DELETE: 1186 rc = qeth_l3_deregister_addr_entry(card, addr); 1187 if (!rc || rc == -ENOENT) { 1188 hash_del(&addr->hnode); 1189 kfree(addr); 1190 } 1191 break; 1192 case QETH_DISP_ADDR_ADD: 1193 rc = qeth_l3_register_addr_entry(card, addr); 1194 if (rc && rc != -ENETDOWN) { 1195 hash_del(&addr->hnode); 1196 kfree(addr); 1197 break; 1198 } 1199 fallthrough; 1200 default: 1201 /* for next call to set_rx_mode(): */ 1202 addr->disp_flag = QETH_DISP_ADDR_DELETE; 1203 } 1204 } 1205 } 1206 1207 qeth_l3_set_promisc_mode(card); 1208 } 1209 1210 static int qeth_l3_arp_makerc(u16 rc) 1211 { 1212 switch (rc) { 1213 case IPA_RC_SUCCESS: 1214 return 0; 1215 case QETH_IPA_ARP_RC_NOTSUPP: 1216 case QETH_IPA_ARP_RC_Q_NOTSUPP: 1217 return -EOPNOTSUPP; 1218 case QETH_IPA_ARP_RC_OUT_OF_RANGE: 1219 return -EINVAL; 1220 case QETH_IPA_ARP_RC_Q_NO_DATA: 1221 return -ENOENT; 1222 default: 1223 return -EIO; 1224 } 1225 } 1226 1227 static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply, 1228 unsigned long data) 1229 { 1230 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data; 1231 1232 qeth_setassparms_cb(card, reply, data); 1233 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1234 } 1235 1236 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries) 1237 { 1238 struct qeth_cmd_buffer *iob; 1239 int rc; 1240 1241 QETH_CARD_TEXT(card, 3, "arpstnoe"); 1242 1243 /* 1244 * currently GuestLAN only supports the ARP assist function 1245 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES; 1246 * thus we say EOPNOTSUPP for this ARP function 1247 */ 1248 if (IS_VM_NIC(card)) 1249 return -EOPNOTSUPP; 1250 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1251 return -EOPNOTSUPP; 1252 } 1253 1254 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1255 IPA_CMD_ASS_ARP_SET_NO_ENTRIES, 1256 SETASS_DATA_SIZEOF(flags_32bit), 1257 QETH_PROT_IPV4); 1258 if (!iob) 1259 return -ENOMEM; 1260 1261 __ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries; 1262 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1263 if (rc) 1264 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n", 1265 CARD_DEVID(card), rc); 1266 return rc; 1267 } 1268 1269 static __u32 get_arp_entry_size(struct qeth_card *card, 1270 struct qeth_arp_query_data *qdata, 1271 struct qeth_arp_entrytype *type, __u8 strip_entries) 1272 { 1273 __u32 rc; 1274 __u8 is_hsi; 1275 1276 is_hsi = qdata->reply_bits == 5; 1277 if (type->ip == QETHARP_IP_ADDR_V4) { 1278 QETH_CARD_TEXT(card, 4, "arpev4"); 1279 if (strip_entries) { 1280 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) : 1281 sizeof(struct qeth_arp_qi_entry7_short); 1282 } else { 1283 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) : 1284 sizeof(struct qeth_arp_qi_entry7); 1285 } 1286 } else if (type->ip == QETHARP_IP_ADDR_V6) { 1287 QETH_CARD_TEXT(card, 4, "arpev6"); 1288 if (strip_entries) { 1289 rc = is_hsi ? 1290 sizeof(struct qeth_arp_qi_entry5_short_ipv6) : 1291 sizeof(struct qeth_arp_qi_entry7_short_ipv6); 1292 } else { 1293 rc = is_hsi ? 1294 sizeof(struct qeth_arp_qi_entry5_ipv6) : 1295 sizeof(struct qeth_arp_qi_entry7_ipv6); 1296 } 1297 } else { 1298 QETH_CARD_TEXT(card, 4, "arpinv"); 1299 rc = 0; 1300 } 1301 1302 return rc; 1303 } 1304 1305 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot) 1306 { 1307 return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) || 1308 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6); 1309 } 1310 1311 static int qeth_l3_arp_query_cb(struct qeth_card *card, 1312 struct qeth_reply *reply, unsigned long data) 1313 { 1314 struct qeth_ipa_cmd *cmd; 1315 struct qeth_arp_query_data *qdata; 1316 struct qeth_arp_query_info *qinfo; 1317 int e; 1318 int entrybytes_done; 1319 int stripped_bytes; 1320 __u8 do_strip_entries; 1321 1322 QETH_CARD_TEXT(card, 3, "arpquecb"); 1323 1324 qinfo = (struct qeth_arp_query_info *) reply->param; 1325 cmd = (struct qeth_ipa_cmd *) data; 1326 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version); 1327 if (cmd->hdr.return_code) { 1328 QETH_CARD_TEXT(card, 4, "arpcberr"); 1329 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code); 1330 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1331 } 1332 if (cmd->data.setassparms.hdr.return_code) { 1333 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code; 1334 QETH_CARD_TEXT(card, 4, "setaperr"); 1335 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code); 1336 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1337 } 1338 qdata = &cmd->data.setassparms.data.query_arp; 1339 QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries); 1340 1341 do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0; 1342 stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0; 1343 entrybytes_done = 0; 1344 for (e = 0; e < qdata->no_entries; ++e) { 1345 char *cur_entry; 1346 __u32 esize; 1347 struct qeth_arp_entrytype *etype; 1348 1349 cur_entry = &qdata->data + entrybytes_done; 1350 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type; 1351 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) { 1352 QETH_CARD_TEXT(card, 4, "pmis"); 1353 QETH_CARD_TEXT_(card, 4, "%i", etype->ip); 1354 break; 1355 } 1356 esize = get_arp_entry_size(card, qdata, etype, 1357 do_strip_entries); 1358 QETH_CARD_TEXT_(card, 5, "esz%i", esize); 1359 if (!esize) 1360 break; 1361 1362 if ((qinfo->udata_len - qinfo->udata_offset) < esize) { 1363 QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC); 1364 memset(qinfo->udata, 0, 4); 1365 return -ENOSPC; 1366 } 1367 1368 memcpy(qinfo->udata + qinfo->udata_offset, 1369 &qdata->data + entrybytes_done + stripped_bytes, 1370 esize); 1371 entrybytes_done += esize + stripped_bytes; 1372 qinfo->udata_offset += esize; 1373 ++qinfo->no_entries; 1374 } 1375 /* check if all replies received ... */ 1376 if (cmd->data.setassparms.hdr.seq_no < 1377 cmd->data.setassparms.hdr.number_of_replies) 1378 return 1; 1379 QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries); 1380 memcpy(qinfo->udata, &qinfo->no_entries, 4); 1381 /* keep STRIP_ENTRIES flag so the user program can distinguish 1382 * stripped entries from normal ones */ 1383 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) 1384 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES; 1385 memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2); 1386 QETH_CARD_TEXT_(card, 4, "rc%i", 0); 1387 return 0; 1388 } 1389 1390 static int qeth_l3_query_arp_cache_info(struct qeth_card *card, 1391 enum qeth_prot_versions prot, 1392 struct qeth_arp_query_info *qinfo) 1393 { 1394 struct qeth_cmd_buffer *iob; 1395 struct qeth_ipa_cmd *cmd; 1396 int rc; 1397 1398 QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot); 1399 1400 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1401 IPA_CMD_ASS_ARP_QUERY_INFO, 1402 SETASS_DATA_SIZEOF(query_arp), prot); 1403 if (!iob) 1404 return -ENOMEM; 1405 cmd = __ipa_cmd(iob); 1406 cmd->data.setassparms.data.query_arp.request_bits = 0x000F; 1407 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo); 1408 if (rc) 1409 QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n", 1410 CARD_DEVID(card), rc); 1411 return rc; 1412 } 1413 1414 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata) 1415 { 1416 struct qeth_arp_query_info qinfo = {0, }; 1417 int rc; 1418 1419 QETH_CARD_TEXT(card, 3, "arpquery"); 1420 1421 if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/ 1422 IPA_ARP_PROCESSING)) { 1423 QETH_CARD_TEXT(card, 3, "arpqnsup"); 1424 rc = -EOPNOTSUPP; 1425 goto out; 1426 } 1427 /* get size of userspace buffer and mask_bits -> 6 bytes */ 1428 if (copy_from_user(&qinfo, udata, 6)) { 1429 rc = -EFAULT; 1430 goto out; 1431 } 1432 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL); 1433 if (!qinfo.udata) { 1434 rc = -ENOMEM; 1435 goto out; 1436 } 1437 qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET; 1438 rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo); 1439 if (rc) { 1440 if (copy_to_user(udata, qinfo.udata, 4)) 1441 rc = -EFAULT; 1442 goto free_and_out; 1443 } 1444 if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) { 1445 /* fails in case of GuestLAN QDIO mode */ 1446 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo); 1447 } 1448 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) { 1449 QETH_CARD_TEXT(card, 4, "qactf"); 1450 rc = -EFAULT; 1451 goto free_and_out; 1452 } 1453 QETH_CARD_TEXT(card, 4, "qacts"); 1454 1455 free_and_out: 1456 kfree(qinfo.udata); 1457 out: 1458 return rc; 1459 } 1460 1461 static int qeth_l3_arp_modify_entry(struct qeth_card *card, 1462 struct qeth_arp_cache_entry *entry, 1463 enum qeth_arp_process_subcmds arp_cmd) 1464 { 1465 struct qeth_arp_cache_entry *cmd_entry; 1466 struct qeth_cmd_buffer *iob; 1467 int rc; 1468 1469 if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY) 1470 QETH_CARD_TEXT(card, 3, "arpadd"); 1471 else 1472 QETH_CARD_TEXT(card, 3, "arpdel"); 1473 1474 /* 1475 * currently GuestLAN only supports the ARP assist function 1476 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY; 1477 * thus we say EOPNOTSUPP for this ARP function 1478 */ 1479 if (IS_VM_NIC(card)) 1480 return -EOPNOTSUPP; 1481 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1482 return -EOPNOTSUPP; 1483 } 1484 1485 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd, 1486 SETASS_DATA_SIZEOF(arp_entry), 1487 QETH_PROT_IPV4); 1488 if (!iob) 1489 return -ENOMEM; 1490 1491 cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry; 1492 ether_addr_copy(cmd_entry->macaddr, entry->macaddr); 1493 memcpy(cmd_entry->ipaddr, entry->ipaddr, 4); 1494 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1495 if (rc) 1496 QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n", 1497 arp_cmd, CARD_DEVID(card), rc); 1498 return rc; 1499 } 1500 1501 static int qeth_l3_arp_flush_cache(struct qeth_card *card) 1502 { 1503 struct qeth_cmd_buffer *iob; 1504 int rc; 1505 1506 QETH_CARD_TEXT(card, 3, "arpflush"); 1507 1508 /* 1509 * currently GuestLAN only supports the ARP assist function 1510 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE; 1511 * thus we say EOPNOTSUPP for this ARP function 1512 */ 1513 if (IS_VM_NIC(card) || IS_IQD(card)) 1514 return -EOPNOTSUPP; 1515 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1516 return -EOPNOTSUPP; 1517 } 1518 1519 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1520 IPA_CMD_ASS_ARP_FLUSH_CACHE, 0, 1521 QETH_PROT_IPV4); 1522 if (!iob) 1523 return -ENOMEM; 1524 1525 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1526 if (rc) 1527 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n", 1528 CARD_DEVID(card), rc); 1529 return rc; 1530 } 1531 1532 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1533 { 1534 struct qeth_card *card = dev->ml_priv; 1535 struct qeth_arp_cache_entry arp_entry; 1536 enum qeth_arp_process_subcmds arp_cmd; 1537 int rc = 0; 1538 1539 switch (cmd) { 1540 case SIOC_QETH_ARP_SET_NO_ENTRIES: 1541 if (!capable(CAP_NET_ADMIN)) { 1542 rc = -EPERM; 1543 break; 1544 } 1545 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue); 1546 break; 1547 case SIOC_QETH_ARP_QUERY_INFO: 1548 if (!capable(CAP_NET_ADMIN)) { 1549 rc = -EPERM; 1550 break; 1551 } 1552 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data); 1553 break; 1554 case SIOC_QETH_ARP_ADD_ENTRY: 1555 case SIOC_QETH_ARP_REMOVE_ENTRY: 1556 if (!capable(CAP_NET_ADMIN)) 1557 return -EPERM; 1558 if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry))) 1559 return -EFAULT; 1560 1561 arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ? 1562 IPA_CMD_ASS_ARP_ADD_ENTRY : 1563 IPA_CMD_ASS_ARP_REMOVE_ENTRY; 1564 return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd); 1565 case SIOC_QETH_ARP_FLUSH_CACHE: 1566 if (!capable(CAP_NET_ADMIN)) { 1567 rc = -EPERM; 1568 break; 1569 } 1570 rc = qeth_l3_arp_flush_cache(card); 1571 break; 1572 default: 1573 rc = -EOPNOTSUPP; 1574 } 1575 return rc; 1576 } 1577 1578 static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst, 1579 int ipv) 1580 { 1581 struct neighbour *n = NULL; 1582 1583 if (dst) 1584 n = dst_neigh_lookup_skb(dst, skb); 1585 1586 if (n) { 1587 int cast_type = n->type; 1588 1589 neigh_release(n); 1590 if ((cast_type == RTN_BROADCAST) || 1591 (cast_type == RTN_MULTICAST) || 1592 (cast_type == RTN_ANYCAST)) 1593 return cast_type; 1594 return RTN_UNICAST; 1595 } 1596 1597 /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */ 1598 switch (ipv) { 1599 case 4: 1600 if (ipv4_is_lbcast(ip_hdr(skb)->daddr)) 1601 return RTN_BROADCAST; 1602 return ipv4_is_multicast(ip_hdr(skb)->daddr) ? 1603 RTN_MULTICAST : RTN_UNICAST; 1604 case 6: 1605 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ? 1606 RTN_MULTICAST : RTN_UNICAST; 1607 default: 1608 /* ... and MAC address */ 1609 return qeth_get_ether_cast_type(skb); 1610 } 1611 } 1612 1613 static int qeth_l3_get_cast_type(struct sk_buff *skb) 1614 { 1615 int ipv = qeth_get_ip_version(skb); 1616 struct dst_entry *dst; 1617 int cast_type; 1618 1619 rcu_read_lock(); 1620 dst = qeth_dst_check_rcu(skb, ipv); 1621 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv); 1622 rcu_read_unlock(); 1623 1624 return cast_type; 1625 } 1626 1627 static u8 qeth_l3_cast_type_to_flag(int cast_type) 1628 { 1629 if (cast_type == RTN_MULTICAST) 1630 return QETH_CAST_MULTICAST; 1631 if (cast_type == RTN_ANYCAST) 1632 return QETH_CAST_ANYCAST; 1633 if (cast_type == RTN_BROADCAST) 1634 return QETH_CAST_BROADCAST; 1635 return QETH_CAST_UNICAST; 1636 } 1637 1638 static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue, 1639 struct qeth_hdr *hdr, struct sk_buff *skb, 1640 int ipv, unsigned int data_len) 1641 { 1642 struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3; 1643 struct vlan_ethhdr *veth = vlan_eth_hdr(skb); 1644 struct qeth_card *card = queue->card; 1645 struct dst_entry *dst; 1646 int cast_type; 1647 1648 hdr->hdr.l3.length = data_len; 1649 1650 if (skb_is_gso(skb)) { 1651 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO; 1652 } else { 1653 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3; 1654 1655 if (skb->protocol == htons(ETH_P_AF_IUCV)) { 1656 l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST; 1657 l3_hdr->next_hop.addr.s6_addr16[0] = htons(0xfe80); 1658 memcpy(&l3_hdr->next_hop.addr.s6_addr32[2], 1659 iucv_trans_hdr(skb)->destUserID, 8); 1660 return; 1661 } 1662 1663 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1664 qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv); 1665 /* some HW requires combined L3+L4 csum offload: */ 1666 if (ipv == 4) 1667 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ; 1668 } 1669 } 1670 1671 if (ipv == 4 || IS_IQD(card)) { 1672 /* NETIF_F_HW_VLAN_CTAG_TX */ 1673 if (skb_vlan_tag_present(skb)) { 1674 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME; 1675 hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb); 1676 } 1677 } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) { 1678 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG; 1679 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI); 1680 } 1681 1682 rcu_read_lock(); 1683 dst = qeth_dst_check_rcu(skb, ipv); 1684 1685 if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ) 1686 cast_type = RTN_UNICAST; 1687 else 1688 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv); 1689 l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type); 1690 1691 if (ipv == 4) { 1692 l3_hdr->next_hop.addr.s6_addr32[3] = 1693 qeth_next_hop_v4_rcu(skb, dst); 1694 } else if (ipv == 6) { 1695 l3_hdr->next_hop.addr = *qeth_next_hop_v6_rcu(skb, dst); 1696 1697 hdr->hdr.l3.flags |= QETH_HDR_IPV6; 1698 if (!IS_IQD(card)) 1699 hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU; 1700 } else { 1701 /* OSA only: */ 1702 l3_hdr->flags |= QETH_HDR_PASSTHRU; 1703 } 1704 rcu_read_unlock(); 1705 } 1706 1707 static void qeth_l3_fixup_headers(struct sk_buff *skb) 1708 { 1709 struct iphdr *iph = ip_hdr(skb); 1710 1711 /* this is safe, IPv6 traffic takes a different path */ 1712 if (skb->ip_summed == CHECKSUM_PARTIAL) 1713 iph->check = 0; 1714 if (skb_is_gso(skb)) { 1715 iph->tot_len = 0; 1716 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr, 1717 iph->daddr, 0); 1718 } 1719 } 1720 1721 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb, 1722 struct qeth_qdio_out_q *queue, int ipv) 1723 { 1724 unsigned int hw_hdr_len; 1725 int rc; 1726 1727 /* re-use the L2 header area for the HW header: */ 1728 hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) : 1729 sizeof(struct qeth_hdr); 1730 rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN); 1731 if (rc) 1732 return rc; 1733 skb_pull(skb, ETH_HLEN); 1734 1735 qeth_l3_fixup_headers(skb); 1736 return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header); 1737 } 1738 1739 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb, 1740 struct net_device *dev) 1741 { 1742 struct qeth_card *card = dev->ml_priv; 1743 u16 txq = skb_get_queue_mapping(skb); 1744 int ipv = qeth_get_ip_version(skb); 1745 struct qeth_qdio_out_q *queue; 1746 int rc; 1747 1748 if (!skb_is_gso(skb)) 1749 qdisc_skb_cb(skb)->pkt_len = skb->len; 1750 if (IS_IQD(card)) { 1751 queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)]; 1752 1753 if (card->options.sniffer) 1754 goto tx_drop; 1755 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) || 1756 (card->options.cq == QETH_CQ_ENABLED && 1757 skb->protocol != htons(ETH_P_AF_IUCV))) 1758 goto tx_drop; 1759 } else { 1760 queue = card->qdio.out_qs[txq]; 1761 } 1762 1763 if (!(dev->flags & IFF_BROADCAST) && 1764 qeth_l3_get_cast_type(skb) == RTN_BROADCAST) 1765 goto tx_drop; 1766 1767 if (ipv == 4 || IS_IQD(card)) 1768 rc = qeth_l3_xmit(card, skb, queue, ipv); 1769 else 1770 rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header); 1771 1772 if (!rc) 1773 return NETDEV_TX_OK; 1774 1775 tx_drop: 1776 QETH_TXQ_STAT_INC(queue, tx_dropped); 1777 kfree_skb(skb); 1778 return NETDEV_TX_OK; 1779 } 1780 1781 static void qeth_l3_set_rx_mode(struct net_device *dev) 1782 { 1783 struct qeth_card *card = dev->ml_priv; 1784 1785 schedule_work(&card->rx_mode_work); 1786 } 1787 1788 /* 1789 * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting 1790 * NOARP on the netdevice is no option because it also turns off neighbor 1791 * solicitation. For IPv4 we install a neighbor_setup function. We don't want 1792 * arp resolution but we want the hard header (packet socket will work 1793 * e.g. tcpdump) 1794 */ 1795 static int qeth_l3_neigh_setup_noarp(struct neighbour *n) 1796 { 1797 n->nud_state = NUD_NOARP; 1798 memcpy(n->ha, "FAKELL", 6); 1799 n->output = n->ops->connected_output; 1800 return 0; 1801 } 1802 1803 static int 1804 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np) 1805 { 1806 if (np->tbl->family == AF_INET) 1807 np->neigh_setup = qeth_l3_neigh_setup_noarp; 1808 1809 return 0; 1810 } 1811 1812 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb, 1813 struct net_device *dev, 1814 netdev_features_t features) 1815 { 1816 if (qeth_get_ip_version(skb) != 4) 1817 features &= ~NETIF_F_HW_VLAN_CTAG_TX; 1818 return qeth_features_check(skb, dev, features); 1819 } 1820 1821 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb, 1822 struct net_device *sb_dev) 1823 { 1824 return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb), 1825 sb_dev); 1826 } 1827 1828 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb, 1829 struct net_device *sb_dev) 1830 { 1831 struct qeth_card *card = dev->ml_priv; 1832 1833 if (qeth_uses_tx_prio_queueing(card)) 1834 return qeth_get_priority_queue(card, skb); 1835 1836 return netdev_pick_tx(dev, skb, sb_dev); 1837 } 1838 1839 static const struct net_device_ops qeth_l3_netdev_ops = { 1840 .ndo_open = qeth_open, 1841 .ndo_stop = qeth_stop, 1842 .ndo_get_stats64 = qeth_get_stats64, 1843 .ndo_start_xmit = qeth_l3_hard_start_xmit, 1844 .ndo_select_queue = qeth_l3_iqd_select_queue, 1845 .ndo_validate_addr = eth_validate_addr, 1846 .ndo_set_rx_mode = qeth_l3_set_rx_mode, 1847 .ndo_do_ioctl = qeth_do_ioctl, 1848 .ndo_fix_features = qeth_fix_features, 1849 .ndo_set_features = qeth_set_features, 1850 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid, 1851 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid, 1852 .ndo_tx_timeout = qeth_tx_timeout, 1853 }; 1854 1855 static const struct net_device_ops qeth_l3_osa_netdev_ops = { 1856 .ndo_open = qeth_open, 1857 .ndo_stop = qeth_stop, 1858 .ndo_get_stats64 = qeth_get_stats64, 1859 .ndo_start_xmit = qeth_l3_hard_start_xmit, 1860 .ndo_features_check = qeth_l3_osa_features_check, 1861 .ndo_select_queue = qeth_l3_osa_select_queue, 1862 .ndo_validate_addr = eth_validate_addr, 1863 .ndo_set_rx_mode = qeth_l3_set_rx_mode, 1864 .ndo_do_ioctl = qeth_do_ioctl, 1865 .ndo_fix_features = qeth_fix_features, 1866 .ndo_set_features = qeth_set_features, 1867 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid, 1868 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid, 1869 .ndo_tx_timeout = qeth_tx_timeout, 1870 .ndo_neigh_setup = qeth_l3_neigh_setup, 1871 }; 1872 1873 static int qeth_l3_setup_netdev(struct qeth_card *card) 1874 { 1875 struct net_device *dev = card->dev; 1876 unsigned int headroom; 1877 int rc; 1878 1879 if (IS_OSD(card) || IS_OSX(card)) { 1880 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops; 1881 1882 /*IPv6 address autoconfiguration stuff*/ 1883 dev->dev_id = qeth_l3_get_unique_id(card, dev->dev_id); 1884 1885 if (!IS_VM_NIC(card)) { 1886 card->dev->features |= NETIF_F_SG; 1887 card->dev->hw_features |= NETIF_F_TSO | 1888 NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1889 card->dev->vlan_features |= NETIF_F_TSO | 1890 NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1891 } 1892 1893 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) { 1894 card->dev->hw_features |= NETIF_F_IPV6_CSUM; 1895 card->dev->vlan_features |= NETIF_F_IPV6_CSUM; 1896 } 1897 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) { 1898 card->dev->hw_features |= NETIF_F_TSO6; 1899 card->dev->vlan_features |= NETIF_F_TSO6; 1900 } 1901 1902 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */ 1903 if (card->dev->hw_features & NETIF_F_TSO6) 1904 headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN; 1905 else if (card->dev->hw_features & NETIF_F_TSO) 1906 headroom = sizeof(struct qeth_hdr_tso); 1907 else 1908 headroom = sizeof(struct qeth_hdr) + VLAN_HLEN; 1909 } else if (IS_IQD(card)) { 1910 card->dev->flags |= IFF_NOARP; 1911 card->dev->netdev_ops = &qeth_l3_netdev_ops; 1912 headroom = sizeof(struct qeth_hdr) - ETH_HLEN; 1913 1914 rc = qeth_l3_iqd_read_initial_mac(card); 1915 if (rc) 1916 return rc; 1917 } else 1918 return -ENODEV; 1919 1920 card->dev->needed_headroom = headroom; 1921 card->dev->features |= NETIF_F_HW_VLAN_CTAG_TX | 1922 NETIF_F_HW_VLAN_CTAG_RX | 1923 NETIF_F_HW_VLAN_CTAG_FILTER; 1924 1925 netif_keep_dst(card->dev); 1926 if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) 1927 netif_set_gso_max_size(card->dev, 1928 PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1)); 1929 1930 netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT); 1931 return register_netdev(card->dev); 1932 } 1933 1934 static const struct device_type qeth_l3_devtype = { 1935 .name = "qeth_layer3", 1936 .groups = qeth_l3_attr_groups, 1937 }; 1938 1939 static int qeth_l3_probe_device(struct ccwgroup_device *gdev) 1940 { 1941 struct qeth_card *card = dev_get_drvdata(&gdev->dev); 1942 int rc; 1943 1944 hash_init(card->ip_htable); 1945 mutex_init(&card->ip_lock); 1946 card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0, 1947 dev_name(&gdev->dev)); 1948 if (!card->cmd_wq) 1949 return -ENOMEM; 1950 1951 if (gdev->dev.type == &qeth_generic_devtype) { 1952 rc = device_add_groups(&gdev->dev, qeth_l3_attr_groups); 1953 if (rc) { 1954 destroy_workqueue(card->cmd_wq); 1955 return rc; 1956 } 1957 } 1958 1959 INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work); 1960 return 0; 1961 } 1962 1963 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev) 1964 { 1965 struct qeth_card *card = dev_get_drvdata(&cgdev->dev); 1966 1967 if (cgdev->dev.type == &qeth_generic_devtype) 1968 device_remove_groups(&cgdev->dev, qeth_l3_attr_groups); 1969 1970 qeth_set_allowed_threads(card, 0, 1); 1971 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0); 1972 1973 if (cgdev->state == CCWGROUP_ONLINE) 1974 qeth_set_offline(card, false); 1975 1976 cancel_work_sync(&card->close_dev_work); 1977 if (card->dev->reg_state == NETREG_REGISTERED) 1978 unregister_netdev(card->dev); 1979 1980 flush_workqueue(card->cmd_wq); 1981 destroy_workqueue(card->cmd_wq); 1982 qeth_l3_clear_ip_htable(card, 0); 1983 qeth_l3_clear_ipato_list(card); 1984 } 1985 1986 static int qeth_l3_set_online(struct qeth_card *card, bool carrier_ok) 1987 { 1988 struct net_device *dev = card->dev; 1989 int rc = 0; 1990 1991 /* softsetup */ 1992 QETH_CARD_TEXT(card, 2, "softsetp"); 1993 1994 rc = qeth_l3_setadapter_parms(card); 1995 if (rc) 1996 QETH_CARD_TEXT_(card, 2, "2err%04x", rc); 1997 if (!card->options.sniffer) { 1998 qeth_l3_start_ipassists(card); 1999 2000 rc = qeth_l3_setrouting_v4(card); 2001 if (rc) 2002 QETH_CARD_TEXT_(card, 2, "4err%04x", rc); 2003 rc = qeth_l3_setrouting_v6(card); 2004 if (rc) 2005 QETH_CARD_TEXT_(card, 2, "5err%04x", rc); 2006 } 2007 2008 card->state = CARD_STATE_SOFTSETUP; 2009 2010 qeth_set_allowed_threads(card, 0xffffffff, 0); 2011 qeth_l3_recover_ip(card); 2012 2013 if (dev->reg_state != NETREG_REGISTERED) { 2014 rc = qeth_l3_setup_netdev(card); 2015 if (rc) 2016 goto err_setup; 2017 2018 if (carrier_ok) 2019 netif_carrier_on(dev); 2020 } else { 2021 rtnl_lock(); 2022 rc = qeth_set_real_num_tx_queues(card, 2023 qeth_tx_actual_queues(card)); 2024 if (rc) { 2025 rtnl_unlock(); 2026 goto err_set_queues; 2027 } 2028 2029 if (carrier_ok) 2030 netif_carrier_on(dev); 2031 else 2032 netif_carrier_off(dev); 2033 2034 netif_device_attach(dev); 2035 qeth_enable_hw_features(dev); 2036 2037 if (card->info.open_when_online) { 2038 card->info.open_when_online = 0; 2039 dev_open(dev, NULL); 2040 } 2041 rtnl_unlock(); 2042 } 2043 return 0; 2044 2045 err_set_queues: 2046 err_setup: 2047 qeth_set_allowed_threads(card, 0, 1); 2048 card->state = CARD_STATE_DOWN; 2049 qeth_l3_clear_ip_htable(card, 1); 2050 return rc; 2051 } 2052 2053 static void qeth_l3_set_offline(struct qeth_card *card) 2054 { 2055 qeth_set_allowed_threads(card, 0, 1); 2056 qeth_l3_drain_rx_mode_cache(card); 2057 2058 if (card->options.sniffer && 2059 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) 2060 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE); 2061 2062 if (card->state == CARD_STATE_SOFTSETUP) { 2063 card->state = CARD_STATE_DOWN; 2064 qeth_l3_clear_ip_htable(card, 1); 2065 } 2066 } 2067 2068 /* Returns zero if the command is successfully "consumed" */ 2069 static int qeth_l3_control_event(struct qeth_card *card, 2070 struct qeth_ipa_cmd *cmd) 2071 { 2072 return 1; 2073 } 2074 2075 const struct qeth_discipline qeth_l3_discipline = { 2076 .devtype = &qeth_l3_devtype, 2077 .setup = qeth_l3_probe_device, 2078 .remove = qeth_l3_remove_device, 2079 .set_online = qeth_l3_set_online, 2080 .set_offline = qeth_l3_set_offline, 2081 .do_ioctl = qeth_l3_do_ioctl, 2082 .control_event_handler = qeth_l3_control_event, 2083 }; 2084 EXPORT_SYMBOL_GPL(qeth_l3_discipline); 2085 2086 static int qeth_l3_handle_ip_event(struct qeth_card *card, 2087 struct qeth_ipaddr *addr, 2088 unsigned long event) 2089 { 2090 switch (event) { 2091 case NETDEV_UP: 2092 qeth_l3_modify_ip(card, addr, true); 2093 return NOTIFY_OK; 2094 case NETDEV_DOWN: 2095 qeth_l3_modify_ip(card, addr, false); 2096 return NOTIFY_OK; 2097 default: 2098 return NOTIFY_DONE; 2099 } 2100 } 2101 2102 struct qeth_l3_ip_event_work { 2103 struct work_struct work; 2104 struct qeth_card *card; 2105 struct qeth_ipaddr addr; 2106 }; 2107 2108 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work) 2109 2110 static void qeth_l3_add_ip_worker(struct work_struct *work) 2111 { 2112 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work); 2113 2114 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true); 2115 kfree(work); 2116 } 2117 2118 static void qeth_l3_delete_ip_worker(struct work_struct *work) 2119 { 2120 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work); 2121 2122 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false); 2123 kfree(work); 2124 } 2125 2126 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev) 2127 { 2128 if (is_vlan_dev(dev)) 2129 dev = vlan_dev_real_dev(dev); 2130 if (dev->netdev_ops == &qeth_l3_osa_netdev_ops || 2131 dev->netdev_ops == &qeth_l3_netdev_ops) 2132 return (struct qeth_card *) dev->ml_priv; 2133 return NULL; 2134 } 2135 2136 static int qeth_l3_ip_event(struct notifier_block *this, 2137 unsigned long event, void *ptr) 2138 { 2139 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 2140 struct net_device *dev = ifa->ifa_dev->dev; 2141 struct qeth_ipaddr addr; 2142 struct qeth_card *card; 2143 2144 card = qeth_l3_get_card_from_dev(dev); 2145 if (!card) 2146 return NOTIFY_DONE; 2147 QETH_CARD_TEXT(card, 3, "ipevent"); 2148 2149 qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4); 2150 addr.u.a4.addr = ifa->ifa_address; 2151 addr.u.a4.mask = ifa->ifa_mask; 2152 2153 return qeth_l3_handle_ip_event(card, &addr, event); 2154 } 2155 2156 static struct notifier_block qeth_l3_ip_notifier = { 2157 qeth_l3_ip_event, 2158 NULL, 2159 }; 2160 2161 static int qeth_l3_ip6_event(struct notifier_block *this, 2162 unsigned long event, void *ptr) 2163 { 2164 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; 2165 struct net_device *dev = ifa->idev->dev; 2166 struct qeth_l3_ip_event_work *ip_work; 2167 struct qeth_card *card; 2168 2169 if (event != NETDEV_UP && event != NETDEV_DOWN) 2170 return NOTIFY_DONE; 2171 2172 card = qeth_l3_get_card_from_dev(dev); 2173 if (!card) 2174 return NOTIFY_DONE; 2175 QETH_CARD_TEXT(card, 3, "ip6event"); 2176 if (!qeth_is_supported(card, IPA_IPV6)) 2177 return NOTIFY_DONE; 2178 2179 ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC); 2180 if (!ip_work) 2181 return NOTIFY_DONE; 2182 2183 if (event == NETDEV_UP) 2184 INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker); 2185 else 2186 INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker); 2187 2188 ip_work->card = card; 2189 qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL, 2190 QETH_PROT_IPV6); 2191 ip_work->addr.u.a6.addr = ifa->addr; 2192 ip_work->addr.u.a6.pfxlen = ifa->prefix_len; 2193 2194 queue_work(card->cmd_wq, &ip_work->work); 2195 return NOTIFY_OK; 2196 } 2197 2198 static struct notifier_block qeth_l3_ip6_notifier = { 2199 qeth_l3_ip6_event, 2200 NULL, 2201 }; 2202 2203 static int qeth_l3_register_notifiers(void) 2204 { 2205 int rc; 2206 2207 QETH_DBF_TEXT(SETUP, 5, "regnotif"); 2208 rc = register_inetaddr_notifier(&qeth_l3_ip_notifier); 2209 if (rc) 2210 return rc; 2211 rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier); 2212 if (rc) { 2213 unregister_inetaddr_notifier(&qeth_l3_ip_notifier); 2214 return rc; 2215 } 2216 return 0; 2217 } 2218 2219 static void qeth_l3_unregister_notifiers(void) 2220 { 2221 QETH_DBF_TEXT(SETUP, 5, "unregnot"); 2222 WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier)); 2223 WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier)); 2224 } 2225 2226 static int __init qeth_l3_init(void) 2227 { 2228 pr_info("register layer 3 discipline\n"); 2229 return qeth_l3_register_notifiers(); 2230 } 2231 2232 static void __exit qeth_l3_exit(void) 2233 { 2234 qeth_l3_unregister_notifiers(); 2235 pr_info("unregister layer 3 discipline\n"); 2236 } 2237 2238 module_init(qeth_l3_init); 2239 module_exit(qeth_l3_exit); 2240 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>"); 2241 MODULE_DESCRIPTION("qeth layer 3 discipline"); 2242 MODULE_LICENSE("GPL"); 2243