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