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->ip_mc_htable, 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->ip_mc_htable, 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 953 if (cmd->hdr.return_code == 0) { 954 card->info.unique_id = cmd->data.create_destroy_addr.uid; 955 return 0; 956 } 957 958 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED | 959 UNIQUE_ID_NOT_BY_CARD; 960 dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n"); 961 return -EIO; 962 } 963 964 static int qeth_l3_get_unique_id(struct qeth_card *card) 965 { 966 int rc = 0; 967 struct qeth_cmd_buffer *iob; 968 969 QETH_CARD_TEXT(card, 2, "guniqeid"); 970 971 if (!qeth_is_supported(card, IPA_IPV6)) { 972 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED | 973 UNIQUE_ID_NOT_BY_CARD; 974 return 0; 975 } 976 977 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6, 978 IPA_DATA_SIZEOF(create_destroy_addr)); 979 if (!iob) 980 return -ENOMEM; 981 982 __ipa_cmd(iob)->data.create_destroy_addr.uid = card->info.unique_id; 983 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL); 984 return rc; 985 } 986 987 static int 988 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply, 989 unsigned long data) 990 { 991 struct qeth_ipa_cmd *cmd; 992 __u16 rc; 993 994 QETH_CARD_TEXT(card, 2, "diastrcb"); 995 996 cmd = (struct qeth_ipa_cmd *)data; 997 rc = cmd->hdr.return_code; 998 if (rc) 999 QETH_CARD_TEXT_(card, 2, "dxter%x", rc); 1000 switch (cmd->data.diagass.action) { 1001 case QETH_DIAGS_CMD_TRACE_QUERY: 1002 break; 1003 case QETH_DIAGS_CMD_TRACE_DISABLE: 1004 switch (rc) { 1005 case 0: 1006 case IPA_RC_INVALID_SUBCMD: 1007 card->info.promisc_mode = SET_PROMISC_MODE_OFF; 1008 dev_info(&card->gdev->dev, "The HiperSockets network " 1009 "traffic analyzer is deactivated\n"); 1010 break; 1011 default: 1012 break; 1013 } 1014 break; 1015 case QETH_DIAGS_CMD_TRACE_ENABLE: 1016 switch (rc) { 1017 case 0: 1018 card->info.promisc_mode = SET_PROMISC_MODE_ON; 1019 dev_info(&card->gdev->dev, "The HiperSockets network " 1020 "traffic analyzer is activated\n"); 1021 break; 1022 case IPA_RC_HARDWARE_AUTH_ERROR: 1023 dev_warn(&card->gdev->dev, "The device is not " 1024 "authorized to run as a HiperSockets network " 1025 "traffic analyzer\n"); 1026 break; 1027 case IPA_RC_TRACE_ALREADY_ACTIVE: 1028 dev_warn(&card->gdev->dev, "A HiperSockets " 1029 "network traffic analyzer is already " 1030 "active in the HiperSockets LAN\n"); 1031 break; 1032 default: 1033 break; 1034 } 1035 break; 1036 default: 1037 QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n", 1038 cmd->data.diagass.action, CARD_DEVID(card)); 1039 } 1040 1041 return rc ? -EIO : 0; 1042 } 1043 1044 static int 1045 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd) 1046 { 1047 struct qeth_cmd_buffer *iob; 1048 struct qeth_ipa_cmd *cmd; 1049 1050 QETH_CARD_TEXT(card, 2, "diagtrac"); 1051 1052 iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0); 1053 if (!iob) 1054 return -ENOMEM; 1055 cmd = __ipa_cmd(iob); 1056 cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET; 1057 cmd->data.diagass.action = diags_cmd; 1058 return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL); 1059 } 1060 1061 static int qeth_l3_add_mcast_rtnl(struct net_device *dev, int vid, void *arg) 1062 { 1063 struct qeth_card *card = arg; 1064 struct inet6_dev *in6_dev; 1065 struct in_device *in4_dev; 1066 struct qeth_ipaddr *ipm; 1067 struct qeth_ipaddr tmp; 1068 struct ip_mc_list *im4; 1069 struct ifmcaddr6 *im6; 1070 1071 QETH_CARD_TEXT(card, 4, "addmc"); 1072 1073 if (!dev || !(dev->flags & IFF_UP)) 1074 goto out; 1075 1076 in4_dev = __in_dev_get_rtnl(dev); 1077 if (!in4_dev) 1078 goto walk_ipv6; 1079 1080 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4); 1081 tmp.disp_flag = QETH_DISP_ADDR_ADD; 1082 tmp.is_multicast = 1; 1083 1084 for (im4 = rtnl_dereference(in4_dev->mc_list); im4 != NULL; 1085 im4 = rtnl_dereference(im4->next_rcu)) { 1086 tmp.u.a4.addr = im4->multiaddr; 1087 1088 ipm = qeth_l3_find_addr_by_ip(card, &tmp); 1089 if (ipm) { 1090 /* for mcast, by-IP match means full match */ 1091 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 1092 continue; 1093 } 1094 1095 ipm = kmemdup(&tmp, sizeof(tmp), GFP_KERNEL); 1096 if (!ipm) 1097 continue; 1098 1099 hash_add(card->ip_mc_htable, &ipm->hnode, 1100 qeth_l3_ipaddr_hash(ipm)); 1101 } 1102 1103 walk_ipv6: 1104 if (!qeth_is_supported(card, IPA_IPV6)) 1105 goto out; 1106 1107 in6_dev = __in6_dev_get(dev); 1108 if (!in6_dev) 1109 goto out; 1110 1111 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6); 1112 tmp.disp_flag = QETH_DISP_ADDR_ADD; 1113 tmp.is_multicast = 1; 1114 1115 read_lock_bh(&in6_dev->lock); 1116 for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) { 1117 tmp.u.a6.addr = im6->mca_addr; 1118 1119 ipm = qeth_l3_find_addr_by_ip(card, &tmp); 1120 if (ipm) { 1121 /* for mcast, by-IP match means full match */ 1122 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING; 1123 continue; 1124 } 1125 1126 ipm = kmemdup(&tmp, sizeof(tmp), GFP_ATOMIC); 1127 if (!ipm) 1128 continue; 1129 1130 hash_add(card->ip_mc_htable, 1131 &ipm->hnode, qeth_l3_ipaddr_hash(ipm)); 1132 1133 } 1134 read_unlock_bh(&in6_dev->lock); 1135 1136 out: 1137 return 0; 1138 } 1139 1140 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev, 1141 __be16 proto, u16 vid) 1142 { 1143 struct qeth_card *card = dev->ml_priv; 1144 1145 QETH_CARD_TEXT_(card, 4, "aid:%d", vid); 1146 return 0; 1147 } 1148 1149 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev, 1150 __be16 proto, u16 vid) 1151 { 1152 struct qeth_card *card = dev->ml_priv; 1153 1154 QETH_CARD_TEXT_(card, 4, "kid:%d", vid); 1155 return 0; 1156 } 1157 1158 static void qeth_l3_stop_card(struct qeth_card *card) 1159 { 1160 QETH_CARD_TEXT(card, 2, "stopcard"); 1161 1162 qeth_set_allowed_threads(card, 0, 1); 1163 1164 cancel_work_sync(&card->rx_mode_work); 1165 qeth_l3_drain_rx_mode_cache(card); 1166 1167 if (card->options.sniffer && 1168 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) 1169 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE); 1170 1171 if (card->state == CARD_STATE_SOFTSETUP) { 1172 qeth_l3_clear_ip_htable(card, 1); 1173 qeth_clear_ipacmd_list(card); 1174 qeth_drain_output_queues(card); 1175 card->state = CARD_STATE_DOWN; 1176 } 1177 1178 qeth_qdio_clear_card(card, 0); 1179 qeth_clear_working_pool_list(card); 1180 flush_workqueue(card->event_wq); 1181 card->info.promisc_mode = 0; 1182 } 1183 1184 static void qeth_l3_set_promisc_mode(struct qeth_card *card) 1185 { 1186 bool enable = card->dev->flags & IFF_PROMISC; 1187 1188 if (card->info.promisc_mode == enable) 1189 return; 1190 1191 if (IS_VM_NIC(card)) { /* Guestlan trace */ 1192 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE)) 1193 qeth_setadp_promisc_mode(card, enable); 1194 } else if (card->options.sniffer && /* HiperSockets trace */ 1195 qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) { 1196 if (enable) { 1197 QETH_CARD_TEXT(card, 3, "+promisc"); 1198 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE); 1199 } else { 1200 QETH_CARD_TEXT(card, 3, "-promisc"); 1201 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE); 1202 } 1203 } 1204 } 1205 1206 static void qeth_l3_rx_mode_work(struct work_struct *work) 1207 { 1208 struct qeth_card *card = container_of(work, struct qeth_card, 1209 rx_mode_work); 1210 struct qeth_ipaddr *addr; 1211 struct hlist_node *tmp; 1212 int i, rc; 1213 1214 QETH_CARD_TEXT(card, 3, "setmulti"); 1215 1216 if (!card->options.sniffer) { 1217 rtnl_lock(); 1218 qeth_l3_add_mcast_rtnl(card->dev, 0, card); 1219 if (qeth_is_supported(card, IPA_FULL_VLAN)) 1220 vlan_for_each(card->dev, qeth_l3_add_mcast_rtnl, card); 1221 rtnl_unlock(); 1222 1223 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) { 1224 switch (addr->disp_flag) { 1225 case QETH_DISP_ADDR_DELETE: 1226 rc = qeth_l3_deregister_addr_entry(card, addr); 1227 if (!rc || rc == -ENOENT) { 1228 hash_del(&addr->hnode); 1229 kfree(addr); 1230 } 1231 break; 1232 case QETH_DISP_ADDR_ADD: 1233 rc = qeth_l3_register_addr_entry(card, addr); 1234 if (rc && rc != -ENETDOWN) { 1235 hash_del(&addr->hnode); 1236 kfree(addr); 1237 break; 1238 } 1239 addr->ref_counter = 1; 1240 /* fall through */ 1241 default: 1242 /* for next call to set_rx_mode(): */ 1243 addr->disp_flag = QETH_DISP_ADDR_DELETE; 1244 } 1245 } 1246 } 1247 1248 qeth_l3_set_promisc_mode(card); 1249 } 1250 1251 static int qeth_l3_arp_makerc(u16 rc) 1252 { 1253 switch (rc) { 1254 case IPA_RC_SUCCESS: 1255 return 0; 1256 case QETH_IPA_ARP_RC_NOTSUPP: 1257 case QETH_IPA_ARP_RC_Q_NOTSUPP: 1258 return -EOPNOTSUPP; 1259 case QETH_IPA_ARP_RC_OUT_OF_RANGE: 1260 return -EINVAL; 1261 case QETH_IPA_ARP_RC_Q_NO_DATA: 1262 return -ENOENT; 1263 default: 1264 return -EIO; 1265 } 1266 } 1267 1268 static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply, 1269 unsigned long data) 1270 { 1271 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data; 1272 1273 qeth_setassparms_cb(card, reply, data); 1274 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1275 } 1276 1277 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries) 1278 { 1279 struct qeth_cmd_buffer *iob; 1280 int rc; 1281 1282 QETH_CARD_TEXT(card, 3, "arpstnoe"); 1283 1284 /* 1285 * currently GuestLAN only supports the ARP assist function 1286 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES; 1287 * thus we say EOPNOTSUPP for this ARP function 1288 */ 1289 if (IS_VM_NIC(card)) 1290 return -EOPNOTSUPP; 1291 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1292 return -EOPNOTSUPP; 1293 } 1294 1295 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1296 IPA_CMD_ASS_ARP_SET_NO_ENTRIES, 1297 SETASS_DATA_SIZEOF(flags_32bit), 1298 QETH_PROT_IPV4); 1299 if (!iob) 1300 return -ENOMEM; 1301 1302 __ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries; 1303 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1304 if (rc) 1305 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n", 1306 CARD_DEVID(card), rc); 1307 return rc; 1308 } 1309 1310 static __u32 get_arp_entry_size(struct qeth_card *card, 1311 struct qeth_arp_query_data *qdata, 1312 struct qeth_arp_entrytype *type, __u8 strip_entries) 1313 { 1314 __u32 rc; 1315 __u8 is_hsi; 1316 1317 is_hsi = qdata->reply_bits == 5; 1318 if (type->ip == QETHARP_IP_ADDR_V4) { 1319 QETH_CARD_TEXT(card, 4, "arpev4"); 1320 if (strip_entries) { 1321 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) : 1322 sizeof(struct qeth_arp_qi_entry7_short); 1323 } else { 1324 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) : 1325 sizeof(struct qeth_arp_qi_entry7); 1326 } 1327 } else if (type->ip == QETHARP_IP_ADDR_V6) { 1328 QETH_CARD_TEXT(card, 4, "arpev6"); 1329 if (strip_entries) { 1330 rc = is_hsi ? 1331 sizeof(struct qeth_arp_qi_entry5_short_ipv6) : 1332 sizeof(struct qeth_arp_qi_entry7_short_ipv6); 1333 } else { 1334 rc = is_hsi ? 1335 sizeof(struct qeth_arp_qi_entry5_ipv6) : 1336 sizeof(struct qeth_arp_qi_entry7_ipv6); 1337 } 1338 } else { 1339 QETH_CARD_TEXT(card, 4, "arpinv"); 1340 rc = 0; 1341 } 1342 1343 return rc; 1344 } 1345 1346 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot) 1347 { 1348 return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) || 1349 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6); 1350 } 1351 1352 static int qeth_l3_arp_query_cb(struct qeth_card *card, 1353 struct qeth_reply *reply, unsigned long data) 1354 { 1355 struct qeth_ipa_cmd *cmd; 1356 struct qeth_arp_query_data *qdata; 1357 struct qeth_arp_query_info *qinfo; 1358 int e; 1359 int entrybytes_done; 1360 int stripped_bytes; 1361 __u8 do_strip_entries; 1362 1363 QETH_CARD_TEXT(card, 3, "arpquecb"); 1364 1365 qinfo = (struct qeth_arp_query_info *) reply->param; 1366 cmd = (struct qeth_ipa_cmd *) data; 1367 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version); 1368 if (cmd->hdr.return_code) { 1369 QETH_CARD_TEXT(card, 4, "arpcberr"); 1370 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code); 1371 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1372 } 1373 if (cmd->data.setassparms.hdr.return_code) { 1374 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code; 1375 QETH_CARD_TEXT(card, 4, "setaperr"); 1376 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code); 1377 return qeth_l3_arp_makerc(cmd->hdr.return_code); 1378 } 1379 qdata = &cmd->data.setassparms.data.query_arp; 1380 QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries); 1381 1382 do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0; 1383 stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0; 1384 entrybytes_done = 0; 1385 for (e = 0; e < qdata->no_entries; ++e) { 1386 char *cur_entry; 1387 __u32 esize; 1388 struct qeth_arp_entrytype *etype; 1389 1390 cur_entry = &qdata->data + entrybytes_done; 1391 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type; 1392 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) { 1393 QETH_CARD_TEXT(card, 4, "pmis"); 1394 QETH_CARD_TEXT_(card, 4, "%i", etype->ip); 1395 break; 1396 } 1397 esize = get_arp_entry_size(card, qdata, etype, 1398 do_strip_entries); 1399 QETH_CARD_TEXT_(card, 5, "esz%i", esize); 1400 if (!esize) 1401 break; 1402 1403 if ((qinfo->udata_len - qinfo->udata_offset) < esize) { 1404 QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC); 1405 memset(qinfo->udata, 0, 4); 1406 return -ENOSPC; 1407 } 1408 1409 memcpy(qinfo->udata + qinfo->udata_offset, 1410 &qdata->data + entrybytes_done + stripped_bytes, 1411 esize); 1412 entrybytes_done += esize + stripped_bytes; 1413 qinfo->udata_offset += esize; 1414 ++qinfo->no_entries; 1415 } 1416 /* check if all replies received ... */ 1417 if (cmd->data.setassparms.hdr.seq_no < 1418 cmd->data.setassparms.hdr.number_of_replies) 1419 return 1; 1420 QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries); 1421 memcpy(qinfo->udata, &qinfo->no_entries, 4); 1422 /* keep STRIP_ENTRIES flag so the user program can distinguish 1423 * stripped entries from normal ones */ 1424 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) 1425 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES; 1426 memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2); 1427 QETH_CARD_TEXT_(card, 4, "rc%i", 0); 1428 return 0; 1429 } 1430 1431 static int qeth_l3_query_arp_cache_info(struct qeth_card *card, 1432 enum qeth_prot_versions prot, 1433 struct qeth_arp_query_info *qinfo) 1434 { 1435 struct qeth_cmd_buffer *iob; 1436 struct qeth_ipa_cmd *cmd; 1437 int rc; 1438 1439 QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot); 1440 1441 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1442 IPA_CMD_ASS_ARP_QUERY_INFO, 1443 SETASS_DATA_SIZEOF(query_arp), prot); 1444 if (!iob) 1445 return -ENOMEM; 1446 cmd = __ipa_cmd(iob); 1447 cmd->data.setassparms.data.query_arp.request_bits = 0x000F; 1448 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo); 1449 if (rc) 1450 QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n", 1451 CARD_DEVID(card), rc); 1452 return rc; 1453 } 1454 1455 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata) 1456 { 1457 struct qeth_arp_query_info qinfo = {0, }; 1458 int rc; 1459 1460 QETH_CARD_TEXT(card, 3, "arpquery"); 1461 1462 if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/ 1463 IPA_ARP_PROCESSING)) { 1464 QETH_CARD_TEXT(card, 3, "arpqnsup"); 1465 rc = -EOPNOTSUPP; 1466 goto out; 1467 } 1468 /* get size of userspace buffer and mask_bits -> 6 bytes */ 1469 if (copy_from_user(&qinfo, udata, 6)) { 1470 rc = -EFAULT; 1471 goto out; 1472 } 1473 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL); 1474 if (!qinfo.udata) { 1475 rc = -ENOMEM; 1476 goto out; 1477 } 1478 qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET; 1479 rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo); 1480 if (rc) { 1481 if (copy_to_user(udata, qinfo.udata, 4)) 1482 rc = -EFAULT; 1483 goto free_and_out; 1484 } 1485 if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) { 1486 /* fails in case of GuestLAN QDIO mode */ 1487 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo); 1488 } 1489 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) { 1490 QETH_CARD_TEXT(card, 4, "qactf"); 1491 rc = -EFAULT; 1492 goto free_and_out; 1493 } 1494 QETH_CARD_TEXT(card, 4, "qacts"); 1495 1496 free_and_out: 1497 kfree(qinfo.udata); 1498 out: 1499 return rc; 1500 } 1501 1502 static int qeth_l3_arp_modify_entry(struct qeth_card *card, 1503 struct qeth_arp_cache_entry *entry, 1504 enum qeth_arp_process_subcmds arp_cmd) 1505 { 1506 struct qeth_arp_cache_entry *cmd_entry; 1507 struct qeth_cmd_buffer *iob; 1508 int rc; 1509 1510 if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY) 1511 QETH_CARD_TEXT(card, 3, "arpadd"); 1512 else 1513 QETH_CARD_TEXT(card, 3, "arpdel"); 1514 1515 /* 1516 * currently GuestLAN only supports the ARP assist function 1517 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY; 1518 * thus we say EOPNOTSUPP for this ARP function 1519 */ 1520 if (IS_VM_NIC(card)) 1521 return -EOPNOTSUPP; 1522 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1523 return -EOPNOTSUPP; 1524 } 1525 1526 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd, 1527 SETASS_DATA_SIZEOF(arp_entry), 1528 QETH_PROT_IPV4); 1529 if (!iob) 1530 return -ENOMEM; 1531 1532 cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry; 1533 ether_addr_copy(cmd_entry->macaddr, entry->macaddr); 1534 memcpy(cmd_entry->ipaddr, entry->ipaddr, 4); 1535 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1536 if (rc) 1537 QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n", 1538 arp_cmd, CARD_DEVID(card), rc); 1539 return rc; 1540 } 1541 1542 static int qeth_l3_arp_flush_cache(struct qeth_card *card) 1543 { 1544 struct qeth_cmd_buffer *iob; 1545 int rc; 1546 1547 QETH_CARD_TEXT(card, 3, "arpflush"); 1548 1549 /* 1550 * currently GuestLAN only supports the ARP assist function 1551 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE; 1552 * thus we say EOPNOTSUPP for this ARP function 1553 */ 1554 if (IS_VM_NIC(card) || IS_IQD(card)) 1555 return -EOPNOTSUPP; 1556 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) { 1557 return -EOPNOTSUPP; 1558 } 1559 1560 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, 1561 IPA_CMD_ASS_ARP_FLUSH_CACHE, 0, 1562 QETH_PROT_IPV4); 1563 if (!iob) 1564 return -ENOMEM; 1565 1566 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL); 1567 if (rc) 1568 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n", 1569 CARD_DEVID(card), rc); 1570 return rc; 1571 } 1572 1573 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1574 { 1575 struct qeth_card *card = dev->ml_priv; 1576 struct qeth_arp_cache_entry arp_entry; 1577 enum qeth_arp_process_subcmds arp_cmd; 1578 int rc = 0; 1579 1580 switch (cmd) { 1581 case SIOC_QETH_ARP_SET_NO_ENTRIES: 1582 if (!capable(CAP_NET_ADMIN)) { 1583 rc = -EPERM; 1584 break; 1585 } 1586 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue); 1587 break; 1588 case SIOC_QETH_ARP_QUERY_INFO: 1589 if (!capable(CAP_NET_ADMIN)) { 1590 rc = -EPERM; 1591 break; 1592 } 1593 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data); 1594 break; 1595 case SIOC_QETH_ARP_ADD_ENTRY: 1596 case SIOC_QETH_ARP_REMOVE_ENTRY: 1597 if (!capable(CAP_NET_ADMIN)) 1598 return -EPERM; 1599 if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry))) 1600 return -EFAULT; 1601 1602 arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ? 1603 IPA_CMD_ASS_ARP_ADD_ENTRY : 1604 IPA_CMD_ASS_ARP_REMOVE_ENTRY; 1605 return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd); 1606 case SIOC_QETH_ARP_FLUSH_CACHE: 1607 if (!capable(CAP_NET_ADMIN)) { 1608 rc = -EPERM; 1609 break; 1610 } 1611 rc = qeth_l3_arp_flush_cache(card); 1612 break; 1613 default: 1614 rc = -EOPNOTSUPP; 1615 } 1616 return rc; 1617 } 1618 1619 static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst, 1620 int ipv) 1621 { 1622 struct neighbour *n = NULL; 1623 1624 if (dst) 1625 n = dst_neigh_lookup_skb(dst, skb); 1626 1627 if (n) { 1628 int cast_type = n->type; 1629 1630 neigh_release(n); 1631 if ((cast_type == RTN_BROADCAST) || 1632 (cast_type == RTN_MULTICAST) || 1633 (cast_type == RTN_ANYCAST)) 1634 return cast_type; 1635 return RTN_UNICAST; 1636 } 1637 1638 /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */ 1639 switch (ipv) { 1640 case 4: 1641 if (ipv4_is_lbcast(ip_hdr(skb)->daddr)) 1642 return RTN_BROADCAST; 1643 return ipv4_is_multicast(ip_hdr(skb)->daddr) ? 1644 RTN_MULTICAST : RTN_UNICAST; 1645 case 6: 1646 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ? 1647 RTN_MULTICAST : RTN_UNICAST; 1648 default: 1649 /* ... and MAC address */ 1650 return qeth_get_ether_cast_type(skb); 1651 } 1652 } 1653 1654 static int qeth_l3_get_cast_type(struct sk_buff *skb) 1655 { 1656 int ipv = qeth_get_ip_version(skb); 1657 struct dst_entry *dst; 1658 int cast_type; 1659 1660 rcu_read_lock(); 1661 dst = qeth_dst_check_rcu(skb, ipv); 1662 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv); 1663 rcu_read_unlock(); 1664 1665 return cast_type; 1666 } 1667 1668 static u8 qeth_l3_cast_type_to_flag(int cast_type) 1669 { 1670 if (cast_type == RTN_MULTICAST) 1671 return QETH_CAST_MULTICAST; 1672 if (cast_type == RTN_ANYCAST) 1673 return QETH_CAST_ANYCAST; 1674 if (cast_type == RTN_BROADCAST) 1675 return QETH_CAST_BROADCAST; 1676 return QETH_CAST_UNICAST; 1677 } 1678 1679 static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue, 1680 struct qeth_hdr *hdr, struct sk_buff *skb, 1681 int ipv, unsigned int data_len) 1682 { 1683 struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3; 1684 struct vlan_ethhdr *veth = vlan_eth_hdr(skb); 1685 struct qeth_card *card = queue->card; 1686 struct dst_entry *dst; 1687 int cast_type; 1688 1689 hdr->hdr.l3.length = data_len; 1690 1691 if (skb_is_gso(skb)) { 1692 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO; 1693 } else { 1694 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3; 1695 1696 if (skb->protocol == htons(ETH_P_AF_IUCV)) { 1697 l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST; 1698 l3_hdr->next_hop.ipv6_addr.s6_addr16[0] = htons(0xfe80); 1699 memcpy(&l3_hdr->next_hop.ipv6_addr.s6_addr32[2], 1700 iucv_trans_hdr(skb)->destUserID, 8); 1701 return; 1702 } 1703 1704 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1705 qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv); 1706 /* some HW requires combined L3+L4 csum offload: */ 1707 if (ipv == 4) 1708 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ; 1709 } 1710 } 1711 1712 if (ipv == 4 || IS_IQD(card)) { 1713 /* NETIF_F_HW_VLAN_CTAG_TX */ 1714 if (skb_vlan_tag_present(skb)) { 1715 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME; 1716 hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb); 1717 } 1718 } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) { 1719 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG; 1720 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI); 1721 } 1722 1723 rcu_read_lock(); 1724 dst = qeth_dst_check_rcu(skb, ipv); 1725 1726 if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ) 1727 cast_type = RTN_UNICAST; 1728 else 1729 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv); 1730 l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type); 1731 1732 if (ipv == 4) { 1733 struct rtable *rt = (struct rtable *) dst; 1734 1735 *((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ? 1736 rt_nexthop(rt, ip_hdr(skb)->daddr) : 1737 ip_hdr(skb)->daddr; 1738 } else if (ipv == 6) { 1739 struct rt6_info *rt = (struct rt6_info *) dst; 1740 1741 if (rt && !ipv6_addr_any(&rt->rt6i_gateway)) 1742 l3_hdr->next_hop.ipv6_addr = rt->rt6i_gateway; 1743 else 1744 l3_hdr->next_hop.ipv6_addr = ipv6_hdr(skb)->daddr; 1745 1746 hdr->hdr.l3.flags |= QETH_HDR_IPV6; 1747 if (!IS_IQD(card)) 1748 hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU; 1749 } else { 1750 /* OSA only: */ 1751 l3_hdr->flags |= QETH_HDR_PASSTHRU; 1752 } 1753 rcu_read_unlock(); 1754 } 1755 1756 static void qeth_l3_fixup_headers(struct sk_buff *skb) 1757 { 1758 struct iphdr *iph = ip_hdr(skb); 1759 1760 /* this is safe, IPv6 traffic takes a different path */ 1761 if (skb->ip_summed == CHECKSUM_PARTIAL) 1762 iph->check = 0; 1763 if (skb_is_gso(skb)) { 1764 iph->tot_len = 0; 1765 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr, 1766 iph->daddr, 0); 1767 } 1768 } 1769 1770 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb, 1771 struct qeth_qdio_out_q *queue, int ipv) 1772 { 1773 unsigned int hw_hdr_len; 1774 int rc; 1775 1776 /* re-use the L2 header area for the HW header: */ 1777 hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) : 1778 sizeof(struct qeth_hdr); 1779 rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN); 1780 if (rc) 1781 return rc; 1782 skb_pull(skb, ETH_HLEN); 1783 1784 qeth_l3_fixup_headers(skb); 1785 return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header); 1786 } 1787 1788 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb, 1789 struct net_device *dev) 1790 { 1791 struct qeth_card *card = dev->ml_priv; 1792 u16 txq = skb_get_queue_mapping(skb); 1793 int ipv = qeth_get_ip_version(skb); 1794 struct qeth_qdio_out_q *queue; 1795 int rc; 1796 1797 if (!skb_is_gso(skb)) 1798 qdisc_skb_cb(skb)->pkt_len = skb->len; 1799 if (IS_IQD(card)) { 1800 queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)]; 1801 1802 if (card->options.sniffer) 1803 goto tx_drop; 1804 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) || 1805 (card->options.cq == QETH_CQ_ENABLED && 1806 skb->protocol != htons(ETH_P_AF_IUCV))) 1807 goto tx_drop; 1808 } else { 1809 queue = card->qdio.out_qs[txq]; 1810 } 1811 1812 if (!(dev->flags & IFF_BROADCAST) && 1813 qeth_l3_get_cast_type(skb) == RTN_BROADCAST) 1814 goto tx_drop; 1815 1816 if (ipv == 4 || IS_IQD(card)) 1817 rc = qeth_l3_xmit(card, skb, queue, ipv); 1818 else 1819 rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header); 1820 1821 if (!rc) 1822 return NETDEV_TX_OK; 1823 1824 tx_drop: 1825 QETH_TXQ_STAT_INC(queue, tx_dropped); 1826 kfree_skb(skb); 1827 return NETDEV_TX_OK; 1828 } 1829 1830 static void qeth_l3_set_rx_mode(struct net_device *dev) 1831 { 1832 struct qeth_card *card = dev->ml_priv; 1833 1834 schedule_work(&card->rx_mode_work); 1835 } 1836 1837 /* 1838 * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting 1839 * NOARP on the netdevice is no option because it also turns off neighbor 1840 * solicitation. For IPv4 we install a neighbor_setup function. We don't want 1841 * arp resolution but we want the hard header (packet socket will work 1842 * e.g. tcpdump) 1843 */ 1844 static int qeth_l3_neigh_setup_noarp(struct neighbour *n) 1845 { 1846 n->nud_state = NUD_NOARP; 1847 memcpy(n->ha, "FAKELL", 6); 1848 n->output = n->ops->connected_output; 1849 return 0; 1850 } 1851 1852 static int 1853 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np) 1854 { 1855 if (np->tbl->family == AF_INET) 1856 np->neigh_setup = qeth_l3_neigh_setup_noarp; 1857 1858 return 0; 1859 } 1860 1861 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb, 1862 struct net_device *dev, 1863 netdev_features_t features) 1864 { 1865 if (qeth_get_ip_version(skb) != 4) 1866 features &= ~NETIF_F_HW_VLAN_CTAG_TX; 1867 return qeth_features_check(skb, dev, features); 1868 } 1869 1870 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb, 1871 struct net_device *sb_dev) 1872 { 1873 return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb), 1874 sb_dev); 1875 } 1876 1877 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb, 1878 struct net_device *sb_dev) 1879 { 1880 struct qeth_card *card = dev->ml_priv; 1881 1882 return qeth_get_priority_queue(card, skb); 1883 } 1884 1885 static const struct net_device_ops qeth_l3_netdev_ops = { 1886 .ndo_open = qeth_open, 1887 .ndo_stop = qeth_stop, 1888 .ndo_get_stats64 = qeth_get_stats64, 1889 .ndo_start_xmit = qeth_l3_hard_start_xmit, 1890 .ndo_select_queue = qeth_l3_iqd_select_queue, 1891 .ndo_validate_addr = eth_validate_addr, 1892 .ndo_set_rx_mode = qeth_l3_set_rx_mode, 1893 .ndo_do_ioctl = qeth_do_ioctl, 1894 .ndo_fix_features = qeth_fix_features, 1895 .ndo_set_features = qeth_set_features, 1896 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid, 1897 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid, 1898 .ndo_tx_timeout = qeth_tx_timeout, 1899 }; 1900 1901 static const struct net_device_ops qeth_l3_osa_netdev_ops = { 1902 .ndo_open = qeth_open, 1903 .ndo_stop = qeth_stop, 1904 .ndo_get_stats64 = qeth_get_stats64, 1905 .ndo_start_xmit = qeth_l3_hard_start_xmit, 1906 .ndo_features_check = qeth_l3_osa_features_check, 1907 .ndo_select_queue = qeth_l3_osa_select_queue, 1908 .ndo_validate_addr = eth_validate_addr, 1909 .ndo_set_rx_mode = qeth_l3_set_rx_mode, 1910 .ndo_do_ioctl = qeth_do_ioctl, 1911 .ndo_fix_features = qeth_fix_features, 1912 .ndo_set_features = qeth_set_features, 1913 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid, 1914 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid, 1915 .ndo_tx_timeout = qeth_tx_timeout, 1916 .ndo_neigh_setup = qeth_l3_neigh_setup, 1917 }; 1918 1919 static int qeth_l3_setup_netdev(struct qeth_card *card, bool carrier_ok) 1920 { 1921 unsigned int headroom; 1922 int rc; 1923 1924 if (IS_OSD(card) || IS_OSX(card)) { 1925 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) || 1926 (card->info.link_type == QETH_LINK_TYPE_HSTR)) { 1927 pr_info("qeth_l3: ignoring TR device\n"); 1928 return -ENODEV; 1929 } 1930 1931 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops; 1932 1933 /*IPv6 address autoconfiguration stuff*/ 1934 qeth_l3_get_unique_id(card); 1935 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD)) 1936 card->dev->dev_id = card->info.unique_id & 0xffff; 1937 1938 if (!IS_VM_NIC(card)) { 1939 card->dev->features |= NETIF_F_SG; 1940 card->dev->hw_features |= NETIF_F_TSO | 1941 NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1942 card->dev->vlan_features |= NETIF_F_TSO | 1943 NETIF_F_RXCSUM | NETIF_F_IP_CSUM; 1944 } 1945 1946 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) { 1947 card->dev->hw_features |= NETIF_F_IPV6_CSUM; 1948 card->dev->vlan_features |= NETIF_F_IPV6_CSUM; 1949 } 1950 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) { 1951 card->dev->hw_features |= NETIF_F_TSO6; 1952 card->dev->vlan_features |= NETIF_F_TSO6; 1953 } 1954 1955 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */ 1956 if (card->dev->hw_features & NETIF_F_TSO6) 1957 headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN; 1958 else if (card->dev->hw_features & NETIF_F_TSO) 1959 headroom = sizeof(struct qeth_hdr_tso); 1960 else 1961 headroom = sizeof(struct qeth_hdr) + VLAN_HLEN; 1962 } else if (IS_IQD(card)) { 1963 card->dev->flags |= IFF_NOARP; 1964 card->dev->netdev_ops = &qeth_l3_netdev_ops; 1965 headroom = sizeof(struct qeth_hdr) - ETH_HLEN; 1966 1967 rc = qeth_l3_iqd_read_initial_mac(card); 1968 if (rc) 1969 goto out; 1970 } else 1971 return -ENODEV; 1972 1973 card->dev->needed_headroom = headroom; 1974 card->dev->features |= NETIF_F_HW_VLAN_CTAG_TX | 1975 NETIF_F_HW_VLAN_CTAG_RX | 1976 NETIF_F_HW_VLAN_CTAG_FILTER; 1977 1978 netif_keep_dst(card->dev); 1979 if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6)) 1980 netif_set_gso_max_size(card->dev, 1981 PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1)); 1982 1983 netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT); 1984 rc = register_netdev(card->dev); 1985 if (!rc && carrier_ok) 1986 netif_carrier_on(card->dev); 1987 1988 out: 1989 if (rc) 1990 card->dev->netdev_ops = NULL; 1991 return rc; 1992 } 1993 1994 static const struct device_type qeth_l3_devtype = { 1995 .name = "qeth_layer3", 1996 .groups = qeth_l3_attr_groups, 1997 }; 1998 1999 static int qeth_l3_probe_device(struct ccwgroup_device *gdev) 2000 { 2001 struct qeth_card *card = dev_get_drvdata(&gdev->dev); 2002 int rc; 2003 2004 hash_init(card->ip_htable); 2005 mutex_init(&card->ip_lock); 2006 card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0, 2007 dev_name(&gdev->dev)); 2008 if (!card->cmd_wq) 2009 return -ENOMEM; 2010 2011 if (gdev->dev.type == &qeth_generic_devtype) { 2012 rc = qeth_l3_create_device_attributes(&gdev->dev); 2013 if (rc) { 2014 destroy_workqueue(card->cmd_wq); 2015 return rc; 2016 } 2017 } 2018 2019 hash_init(card->ip_mc_htable); 2020 INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work); 2021 return 0; 2022 } 2023 2024 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev) 2025 { 2026 struct qeth_card *card = dev_get_drvdata(&cgdev->dev); 2027 2028 if (cgdev->dev.type == &qeth_generic_devtype) 2029 qeth_l3_remove_device_attributes(&cgdev->dev); 2030 2031 qeth_set_allowed_threads(card, 0, 1); 2032 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0); 2033 2034 if (cgdev->state == CCWGROUP_ONLINE) 2035 qeth_set_offline(card, false); 2036 2037 cancel_work_sync(&card->close_dev_work); 2038 if (qeth_netdev_is_registered(card->dev)) 2039 unregister_netdev(card->dev); 2040 2041 flush_workqueue(card->cmd_wq); 2042 destroy_workqueue(card->cmd_wq); 2043 qeth_l3_clear_ip_htable(card, 0); 2044 qeth_l3_clear_ipato_list(card); 2045 } 2046 2047 static int qeth_l3_set_online(struct qeth_card *card) 2048 { 2049 struct ccwgroup_device *gdev = card->gdev; 2050 struct net_device *dev = card->dev; 2051 int rc = 0; 2052 bool carrier_ok; 2053 2054 rc = qeth_core_hardsetup_card(card, &carrier_ok); 2055 if (rc) { 2056 QETH_CARD_TEXT_(card, 2, "2err%04x", rc); 2057 rc = -ENODEV; 2058 goto out_remove; 2059 } 2060 2061 qeth_print_status_message(card); 2062 2063 /* softsetup */ 2064 QETH_CARD_TEXT(card, 2, "softsetp"); 2065 2066 rc = qeth_l3_setadapter_parms(card); 2067 if (rc) 2068 QETH_CARD_TEXT_(card, 2, "2err%04x", rc); 2069 if (!card->options.sniffer) { 2070 qeth_l3_start_ipassists(card); 2071 2072 rc = qeth_l3_setrouting_v4(card); 2073 if (rc) 2074 QETH_CARD_TEXT_(card, 2, "4err%04x", rc); 2075 rc = qeth_l3_setrouting_v6(card); 2076 if (rc) 2077 QETH_CARD_TEXT_(card, 2, "5err%04x", rc); 2078 } 2079 2080 card->state = CARD_STATE_SOFTSETUP; 2081 2082 qeth_set_allowed_threads(card, 0xffffffff, 0); 2083 qeth_l3_recover_ip(card); 2084 2085 if (!qeth_netdev_is_registered(dev)) { 2086 rc = qeth_l3_setup_netdev(card, carrier_ok); 2087 if (rc) 2088 goto out_remove; 2089 } else { 2090 rtnl_lock(); 2091 if (carrier_ok) 2092 netif_carrier_on(dev); 2093 else 2094 netif_carrier_off(dev); 2095 2096 netif_device_attach(dev); 2097 qeth_enable_hw_features(dev); 2098 2099 if (card->info.open_when_online) { 2100 card->info.open_when_online = 0; 2101 dev_open(dev, NULL); 2102 } 2103 rtnl_unlock(); 2104 } 2105 qeth_trace_features(card); 2106 /* let user_space know that device is online */ 2107 kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE); 2108 return 0; 2109 out_remove: 2110 qeth_l3_stop_card(card); 2111 qeth_stop_channel(&card->data); 2112 qeth_stop_channel(&card->write); 2113 qeth_stop_channel(&card->read); 2114 qdio_free(CARD_DDEV(card)); 2115 return rc; 2116 } 2117 2118 static void qeth_l3_set_offline(struct qeth_card *card) 2119 { 2120 qeth_l3_stop_card(card); 2121 } 2122 2123 /* Returns zero if the command is successfully "consumed" */ 2124 static int qeth_l3_control_event(struct qeth_card *card, 2125 struct qeth_ipa_cmd *cmd) 2126 { 2127 return 1; 2128 } 2129 2130 struct qeth_discipline qeth_l3_discipline = { 2131 .devtype = &qeth_l3_devtype, 2132 .setup = qeth_l3_probe_device, 2133 .remove = qeth_l3_remove_device, 2134 .set_online = qeth_l3_set_online, 2135 .set_offline = qeth_l3_set_offline, 2136 .do_ioctl = qeth_l3_do_ioctl, 2137 .control_event_handler = qeth_l3_control_event, 2138 }; 2139 EXPORT_SYMBOL_GPL(qeth_l3_discipline); 2140 2141 static int qeth_l3_handle_ip_event(struct qeth_card *card, 2142 struct qeth_ipaddr *addr, 2143 unsigned long event) 2144 { 2145 switch (event) { 2146 case NETDEV_UP: 2147 qeth_l3_modify_ip(card, addr, true); 2148 return NOTIFY_OK; 2149 case NETDEV_DOWN: 2150 qeth_l3_modify_ip(card, addr, false); 2151 return NOTIFY_OK; 2152 default: 2153 return NOTIFY_DONE; 2154 } 2155 } 2156 2157 struct qeth_l3_ip_event_work { 2158 struct work_struct work; 2159 struct qeth_card *card; 2160 struct qeth_ipaddr addr; 2161 }; 2162 2163 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work) 2164 2165 static void qeth_l3_add_ip_worker(struct work_struct *work) 2166 { 2167 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work); 2168 2169 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true); 2170 kfree(work); 2171 } 2172 2173 static void qeth_l3_delete_ip_worker(struct work_struct *work) 2174 { 2175 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work); 2176 2177 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false); 2178 kfree(work); 2179 } 2180 2181 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev) 2182 { 2183 if (is_vlan_dev(dev)) 2184 dev = vlan_dev_real_dev(dev); 2185 if (dev->netdev_ops == &qeth_l3_osa_netdev_ops || 2186 dev->netdev_ops == &qeth_l3_netdev_ops) 2187 return (struct qeth_card *) dev->ml_priv; 2188 return NULL; 2189 } 2190 2191 static int qeth_l3_ip_event(struct notifier_block *this, 2192 unsigned long event, void *ptr) 2193 { 2194 2195 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 2196 struct net_device *dev = ifa->ifa_dev->dev; 2197 struct qeth_ipaddr addr; 2198 struct qeth_card *card; 2199 2200 if (dev_net(dev) != &init_net) 2201 return NOTIFY_DONE; 2202 2203 card = qeth_l3_get_card_from_dev(dev); 2204 if (!card) 2205 return NOTIFY_DONE; 2206 QETH_CARD_TEXT(card, 3, "ipevent"); 2207 2208 qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4); 2209 addr.u.a4.addr = ifa->ifa_address; 2210 addr.u.a4.mask = ifa->ifa_mask; 2211 2212 return qeth_l3_handle_ip_event(card, &addr, event); 2213 } 2214 2215 static struct notifier_block qeth_l3_ip_notifier = { 2216 qeth_l3_ip_event, 2217 NULL, 2218 }; 2219 2220 static int qeth_l3_ip6_event(struct notifier_block *this, 2221 unsigned long event, void *ptr) 2222 { 2223 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; 2224 struct net_device *dev = ifa->idev->dev; 2225 struct qeth_l3_ip_event_work *ip_work; 2226 struct qeth_card *card; 2227 2228 if (event != NETDEV_UP && event != NETDEV_DOWN) 2229 return NOTIFY_DONE; 2230 2231 card = qeth_l3_get_card_from_dev(dev); 2232 if (!card) 2233 return NOTIFY_DONE; 2234 QETH_CARD_TEXT(card, 3, "ip6event"); 2235 if (!qeth_is_supported(card, IPA_IPV6)) 2236 return NOTIFY_DONE; 2237 2238 ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC); 2239 if (!ip_work) 2240 return NOTIFY_DONE; 2241 2242 if (event == NETDEV_UP) 2243 INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker); 2244 else 2245 INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker); 2246 2247 ip_work->card = card; 2248 qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL, 2249 QETH_PROT_IPV6); 2250 ip_work->addr.u.a6.addr = ifa->addr; 2251 ip_work->addr.u.a6.pfxlen = ifa->prefix_len; 2252 2253 queue_work(card->cmd_wq, &ip_work->work); 2254 return NOTIFY_OK; 2255 } 2256 2257 static struct notifier_block qeth_l3_ip6_notifier = { 2258 qeth_l3_ip6_event, 2259 NULL, 2260 }; 2261 2262 static int qeth_l3_register_notifiers(void) 2263 { 2264 int rc; 2265 2266 QETH_DBF_TEXT(SETUP, 5, "regnotif"); 2267 rc = register_inetaddr_notifier(&qeth_l3_ip_notifier); 2268 if (rc) 2269 return rc; 2270 rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier); 2271 if (rc) { 2272 unregister_inetaddr_notifier(&qeth_l3_ip_notifier); 2273 return rc; 2274 } 2275 return 0; 2276 } 2277 2278 static void qeth_l3_unregister_notifiers(void) 2279 { 2280 QETH_DBF_TEXT(SETUP, 5, "unregnot"); 2281 WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier)); 2282 WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier)); 2283 } 2284 2285 static int __init qeth_l3_init(void) 2286 { 2287 pr_info("register layer 3 discipline\n"); 2288 return qeth_l3_register_notifiers(); 2289 } 2290 2291 static void __exit qeth_l3_exit(void) 2292 { 2293 qeth_l3_unregister_notifiers(); 2294 pr_info("unregister layer 3 discipline\n"); 2295 } 2296 2297 module_init(qeth_l3_init); 2298 module_exit(qeth_l3_exit); 2299 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>"); 2300 MODULE_DESCRIPTION("qeth layer 3 discipline"); 2301 MODULE_LICENSE("GPL"); 2302