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