1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/user.h>
29 #include <sys/vfs.h>
30 #include <sys/vnode.h>
31 #include <sys/file.h>
32 #include <sys/stream.h>
33 #include <sys/stropts.h>
34 #include <sys/strsubr.h>
35 #include <sys/dlpi.h>
36 #include <sys/vnode.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <net/if.h>
40
41 #include <sys/cred.h>
42 #include <sys/sysmacros.h>
43
44 #include <sys/sad.h>
45 #include <sys/kstr.h>
46 #include <sys/bootconf.h>
47 #include <sys/bootprops.h>
48
49 #include <sys/errno.h>
50 #include <sys/modctl.h>
51 #include <sys/sunddi.h>
52 #include <sys/sunldi.h>
53 #include <sys/esunddi.h>
54 #include <sys/promif.h>
55
56 #include <sys/strlog.h>
57 #include <sys/log.h>
58 #include <sys/ethernet.h>
59 #include <sys/ddi_implfuncs.h>
60
61 #include <sys/dld.h>
62 #include <sys/mac_client.h>
63
64 /*
65 * Debug Macros
66 */
67 int strplumbdebug = 0;
68
69 extern ib_boot_prop_t *iscsiboot_prop;
70
71 #define DBG0(_f) \
72 if (strplumbdebug != 0) \
73 printf("strplumb: " _f)
74
75 #define DBG1(_f, _a) \
76 if (strplumbdebug != 0) \
77 printf("strplumb: " _f, (_a))
78
79 #define DBG2(_f, _a, _b) \
80 if (strplumbdebug != 0) \
81 printf("strplumb: " _f, (_a), (_b))
82
83 #define DBG3(_f, _a, _b, _c) \
84 if (strplumbdebug != 0) \
85 printf("strplumb: " _f, (_a), (_b), (_c))
86
87 /*
88 * Module linkage information for the kernel.
89 */
90 #define STRPLUMB_IDENT "STREAMS Plumbing Module"
91
92 static struct modlmisc modlmisc = {
93 &mod_miscops,
94 STRPLUMB_IDENT
95 };
96
97 static struct modlinkage modlinkage = {
98 MODREV_1,
99 { &modlmisc, NULL }
100 };
101
102 int
103 _init(void)
104 {
105 return (mod_install(&modlinkage));
106 }
107
108 int
109 _fini(void)
110 {
111 return (mod_remove(&modlinkage));
112 }
113
114 int
115 _info(struct modinfo *modinfop)
116 {
117 return (mod_info(&modlinkage, modinfop));
118 }
119
120 #define ARP "arp"
121 #define TCP "tcp"
122 #define TCP6 "tcp6"
123 #define UDP "udp"
124 #define UDP6 "udp6"
125 #define ICMP "icmp"
126 #define ICMP6 "icmp6"
127 #define IP "ip"
128 #define IP6 "ip6"
129 #define TIMOD "timod"
130
131 #define UDPDEV "/devices/pseudo/udp@0:udp"
132 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6"
133 #define UDP6DEV "/devices/pseudo/udp6@0:udp6"
134 #define IP6DEV "/devices/pseudo/ip6@0:ip6"
135
136 typedef struct strplumb_modspec {
137 char *sm_type;
138 char *sm_name;
139 } strplumb_modspec_t;
140
141 static strplumb_modspec_t strplumb_modlist[] = {
142 { "drv", DLD_DRIVER_NAME },
143 { "drv", IP },
144 { "drv", IP6 },
145 { "drv", TCP },
146 { "drv", TCP6 },
147 { "drv", UDP },
148 { "drv", UDP6 },
149 { "drv", ICMP },
150 { "drv", ICMP6 },
151 { "drv", ARP },
152 { "strmod", TIMOD }
153 };
154
155 /*
156 * Called from swapgeneric.c:loadrootmodules() in the network boot case.
157 */
158 int
159 strplumb_load(void)
160 {
161 uint_t i;
162 strplumb_modspec_t *p;
163
164 DBG0("loading modules\n");
165
166 for (i = 0, p = strplumb_modlist;
167 i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
168 i++, p++) {
169 if (modloadonly(p->sm_type, p->sm_name) < 0) {
170 printf("strplumb: failed to load %s/%s\n",
171 p->sm_type, p->sm_name);
172 return (EFAULT);
173 }
174 }
175
176 return (0);
177 }
178
179 static int
180 strplumb_init(void)
181 {
182 uint_t i;
183 strplumb_modspec_t *p;
184 int err;
185
186 DBG0("initializing modules\n");
187
188 for (i = 0, p = strplumb_modlist;
189 i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
190 i++, p++) {
191 if (strcmp(p->sm_type, "drv") == 0)
192 err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
193 0 : EFAULT;
194 else
195 err = (modload(p->sm_type, p->sm_name) < 0) ?
196 EFAULT : 0;
197
198 if (err != 0) {
199 printf("strplumb: failed to initialize %s/%s\n",
200 p->sm_type, p->sm_name);
201 return (err);
202 }
203 }
204
205 return (0);
206 }
207
208 /*
209 * Can be set in /etc/system in the case of local booting. See comment below.
210 */
211 char *ndev_name = 0;
212 int ndev_unit = 0;
213
214 /*
215 * If we booted diskless then strplumb() will have been called from
216 * swapgeneric.c:rootconf(). All we can do in that case is plumb the
217 * network device that we booted from.
218 *
219 * If we booted from a local disk, we will have been called from main(),
220 * and normally we defer the plumbing of interfaces until network/physical.
221 * This can be overridden by setting "ndev_name" in /etc/system.
222 */
223 static int
224 resolve_boot_path(void)
225 {
226 char *devpath;
227 dev_info_t *dip;
228 const char *driver;
229 int instance;
230 #ifdef _OBP
231 char stripped_path[OBP_MAXPATHLEN];
232 #endif
233
234 if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
235 devpath = rootfs.bo_name;
236 else
237 devpath = strplumb_get_netdev_path();
238
239 if (devpath != NULL) {
240 DBG1("resolving boot-path: %s\n", devpath);
241 #ifdef _OBP
242 /*
243 * OBP passes options e.g, "net:dhcp"
244 * remove them here
245 */
246 prom_strip_options(devpath, stripped_path);
247 devpath = stripped_path;
248 #endif
249 /*
250 * Hold the devi since this is the root device.
251 */
252 if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
253 printf("strplumb: unable to hold root device: %s\n",
254 devpath);
255 return (ENXIO);
256 }
257
258 driver = ddi_driver_name(dip);
259 instance = ddi_get_instance(dip);
260 } else {
261 if (ndev_name == NULL)
262 return (ENODEV);
263
264 DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
265 ndev_unit);
266
267 if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
268 printf("strplumb: cannot load ndev_name '%s'\n",
269 ndev_name);
270 return (ENXIO);
271 }
272
273 driver = ndev_name;
274 instance = ndev_unit;
275 }
276
277 (void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
278 "/devices/pseudo/clone@0:%s", driver);
279 (void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
280 driver, instance);
281 rootfs.bo_ppa = instance;
282 return (0);
283 }
284
285 static int
286 getifflags(ldi_handle_t lh, struct lifreq *lifrp)
287 {
288 struct strioctl iocb;
289 int rval;
290
291 iocb.ic_cmd = SIOCGLIFFLAGS;
292 iocb.ic_timout = 15;
293 iocb.ic_len = sizeof (struct lifreq);
294 iocb.ic_dp = (char *)lifrp;
295
296 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
297
298 }
299
300 static int
301 setifname(ldi_handle_t lh, struct lifreq *lifrp)
302 {
303 struct strioctl iocb;
304 int rval;
305
306 iocb.ic_cmd = SIOCSLIFNAME;
307 iocb.ic_timout = 15;
308 iocb.ic_len = sizeof (struct lifreq);
309 iocb.ic_dp = (char *)lifrp;
310
311 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
312 }
313
314 static int
315 strplumb_dev(ldi_ident_t li)
316 {
317 ldi_handle_t lh = NULL;
318 ldi_handle_t mux_lh = NULL;
319 int err;
320 struct lifreq lifr;
321 struct ifreq ifr;
322 int rval;
323 int af = 0;
324 char *name = NULL;
325
326 bzero(&lifr, sizeof (struct lifreq));
327 bzero(&ifr, sizeof (ifr));
328
329 if (iscsiboot_prop != NULL) {
330 af = iscsiboot_prop->boot_nic.sin_family;
331 }
332
333 /*
334 * Now set up the links. Ultimately, we should have two streams
335 * permanently linked under UDP. One stream consists of the
336 * ARP-[ifname] combination, while the other consists of IP-[ifname].
337 *
338 * We pin underneath UDP here to match what is done in ifconfig(1m);
339 * otherwise, ifconfig will be unable to unplumb the stream (the major
340 * number and mux id must both match for a successful I_PUNLINK).
341 *
342 * There are subtleties in the plumbing which make it essential to
343 * follow the logic used in ifconfig(1m) very closely.
344 */
345
346 /*
347 * Plumb UDP-IP-<dev>
348 */
349
350 if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
351 &lh, li)) != 0) {
352 printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
353 err);
354 goto done;
355 }
356
357
358 if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
359 &rval)) != 0) {
360 printf("strplumb: push IP failed: %d\n", err);
361 goto done;
362 }
363
364 if ((err = getifflags(lh, &lifr)) != 0)
365 goto done;
366
367 if (af == 0 || af == AF_INET) {
368 lifr.lifr_flags |= IFF_IPV4;
369 lifr.lifr_flags &= ~IFF_IPV6;
370 name = UDPDEV;
371 } else {
372 /*
373 * iscsi boot is used with ipv6 enabled
374 */
375 lifr.lifr_flags |= IFF_IPV6;
376 lifr.lifr_flags &= ~IFF_IPV4;
377 name = UDP6DEV;
378 }
379 (void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
380 sizeof (lifr.lifr_name));
381 lifr.lifr_ppa = rootfs.bo_ppa;
382
383 if ((err = setifname(lh, &lifr)) != 0)
384 goto done;
385
386 /* get the flags and check if ARP is needed */
387 if ((err = getifflags(lh, &lifr)) != 0) {
388 printf("strplumb: getifflags %s IP failed, error %d\n",
389 lifr.lifr_name, err);
390 goto done;
391 }
392 if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
393 li)) != 0) {
394 printf("strplumb: open of %s failed: %d\n", name, err);
395 goto done;
396 }
397 if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
398 FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
399 &(ifr.ifr_ip_muxid))) != 0) {
400 printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
401 rootfs.bo_ifname, err);
402 goto done;
403 }
404
405 /* if ARP is not needed, we are done */
406 if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6))
407 goto done;
408
409 DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
410
411 (void) ldi_close(lh, FREAD|FWRITE, CRED());
412 lh = NULL;
413
414 /*
415 * Plumb UDP-ARP-<dev>
416 */
417
418 if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
419 &lh, li)) != 0) {
420 printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
421 err);
422 goto done;
423 }
424
425 if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
426 &rval)) != 0) {
427 printf("strplumb: push ARP failed: %d\n", err);
428 goto done;
429 }
430
431 if ((err = setifname(lh, &lifr)) != 0)
432 goto done;
433
434 if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
435 FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
436 &(ifr.ifr_arp_muxid))) != 0) {
437 printf("strplumb: plink UDP-ARP-%s failed: %d\n",
438 rootfs.bo_ifname, err);
439 goto done;
440 }
441
442 DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
443
444 /*
445 * Cache the mux ids.
446 */
447 (void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
448
449 if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
450 CRED(), &rval)) != 0) {
451 printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
452 goto done;
453 }
454
455 done:
456 if (lh != NULL)
457 (void) ldi_close(lh, FREAD|FWRITE, CRED());
458
459 if (mux_lh != NULL)
460 (void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
461
462 return (err);
463 }
464
465 /*
466 * Do streams plumbing for internet protocols.
467 */
468 int
469 strplumb(void)
470 {
471 ldi_ident_t li;
472 int err;
473
474 if ((err = strplumb_init()) != 0)
475 return (err);
476
477 if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
478 return (err);
479
480 if ((err = resolve_boot_path()) != 0)
481 goto done;
482
483 DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
484 DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
485 DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
486
487 if ((err = strplumb_dev(li)) != 0)
488 goto done;
489
490 done:
491 ldi_ident_release(li);
492
493 return (err);
494 }
495
496 /* multiboot: diskless boot interface discovery */
497
498 #ifndef _OBP
499
500 static uchar_t boot_macaddr[16];
501 static int boot_maclen;
502 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
503 static int matchmac(dev_info_t *dip, void *arg);
504
505 #endif /* !_OBP */
506
507 char *
508 strplumb_get_netdev_path(void)
509 {
510 #ifdef _OBP
511 char fstype[OBP_MAXPROPNAME];
512 static char iscsi_network_path[BO_MAXOBJNAME] = {0};
513 int proplen;
514 char *p = NULL;
515
516 if (bop_getprop("fstype", fstype) == -1)
517 return (NULL);
518
519 if (strncmp(fstype, "nfs", 3) == 0)
520 return (prom_bootpath());
521 else if (iscsiboot_prop != NULL) {
522 proplen = BOP_GETPROPLEN(bootops,
523 BP_ISCSI_NETWORK_BOOTPATH);
524 if (proplen > 0) {
525 if (BOP_GETPROP(bootops,
526 BP_ISCSI_NETWORK_BOOTPATH,
527 iscsi_network_path) > 0) {
528 p = strchr(iscsi_network_path, ':');
529 if (p != NULL) {
530 *p = '\0';
531 }
532 return (iscsi_network_path);
533 }
534 }
535 }
536 return (NULL);
537 #else
538
539 char *macstr, *devpath = NULL;
540 uchar_t *bootp;
541 uint_t bootp_len;
542
543 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
544 DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
545 /*
546 * hard coded ether mac len for booting floppy on
547 * machines with old cards
548 */
549 boot_maclen = ether_aton(macstr, boot_macaddr);
550 if (boot_maclen != 6) {
551 cmn_err(CE_WARN,
552 "malformed boot_mac property, %d bytes",
553 boot_maclen);
554 }
555 ddi_prop_free(macstr);
556 } else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
557 DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
558 == DDI_SUCCESS) {
559
560 /*
561 * These offsets are defined by dhcp standard
562 * Should use structure offsets
563 */
564 boot_maclen = *(bootp + 2);
565 ASSERT(boot_maclen <= 16);
566 bcopy(bootp + 28, boot_macaddr, boot_maclen);
567
568 dhcack = kmem_alloc(bootp_len, KM_SLEEP);
569 bcopy(bootp, dhcack, bootp_len);
570 dhcacklen = bootp_len;
571
572 ddi_prop_free(bootp);
573 } else if (iscsiboot_prop != NULL) {
574 bcopy(iscsiboot_prop->boot_nic.nic_mac,
575 boot_macaddr, IB_BOOT_MACLEN);
576 boot_maclen = IB_BOOT_MACLEN;
577 } else {
578 return (NULL);
579 }
580
581 ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
582 return (devpath);
583
584 #endif /* _OBP */
585 }
586
587 #ifndef _OBP
588
589 /*
590 * Get boot path from the boot_mac address
591 */
592 /*ARGSUSED*/
593 static int
594 matchmac(dev_info_t *dip, void *arg)
595 {
596 char **devpathp = (char **)arg;
597 char *model_str;
598 uchar_t *macaddr;
599 size_t maclen;
600
601 /* XXX Should use "device-type" per IEEE 1275 */
602 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
603 "model", &model_str) != DDI_SUCCESS)
604 return (DDI_WALK_CONTINUE);
605
606 if (strcmp(model_str, "Ethernet controller") != 0) {
607 ddi_prop_free(model_str);
608 return (DDI_WALK_CONTINUE);
609 }
610 ddi_prop_free(model_str);
611
612 /* We have a network device now */
613 if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
614 return (DDI_WALK_CONTINUE);
615 }
616
617 ASSERT(boot_maclen != 0);
618 macaddr = getmacaddr(dip, &maclen);
619 if (macaddr == NULL)
620 return (DDI_WALK_CONTINUE);
621
622 if (maclen != boot_maclen ||
623 bcmp(macaddr, boot_macaddr, maclen) != 0) {
624 kmem_free(macaddr, maclen);
625 return (DDI_WALK_CONTINUE);
626 }
627
628 /* found hardware with the mac address */
629 (void) localetheraddr((struct ether_addr *)macaddr, NULL);
630 kmem_free(macaddr, maclen);
631
632 *devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
633 (void) ddi_pathname(dip, *devpathp);
634
635 /* fill in dhcifname */
636 if (dhcack) {
637 (void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
638 ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
639 }
640 return (DDI_WALK_TERMINATE);
641 }
642
643 static uchar_t *
644 getmacaddr(dev_info_t *dip, size_t *maclenp)
645 {
646 int rc, ppa;
647 ldi_ident_t li;
648 ldi_handle_t lh;
649 const char *drv_name = ddi_driver_name(dip);
650 char *clonepath;
651 uchar_t *macaddr = NULL;
652
653 if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
654 cmn_err(CE_WARN,
655 "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
656 return (NULL);
657 }
658
659 clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
660 (void) snprintf(clonepath, MAXPATHLEN,
661 "/devices/pseudo/clone@0:%s", drv_name);
662
663 rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
664 ldi_ident_release(li);
665 if (rc) {
666 cmn_err(CE_WARN,
667 "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
668 clonepath, rc);
669 kmem_free(clonepath, MAXPATHLEN);
670 return (NULL);
671 }
672 kmem_free(clonepath, MAXPATHLEN);
673
674 ppa = i_ddi_devi_get_ppa(dip);
675 if ((dl_attach(lh, ppa, NULL) != 0) ||
676 (dl_bind(lh, ETHERTYPE_IP, NULL) != 0)) {
677 (void) ldi_close(lh, FREAD|FWRITE, CRED());
678 cmn_err(CE_WARN,
679 "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
680 drv_name, ppa, rc);
681 return (NULL);
682 }
683
684 *maclenp = ETHERADDRL;
685 macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
686 if (dl_phys_addr(lh, macaddr, maclenp, NULL) != 0 ||
687 *maclenp != ETHERADDRL) {
688 kmem_free(macaddr, ETHERADDRL);
689 macaddr = NULL;
690 *maclenp = 0;
691 cmn_err(CE_WARN,
692 "getmacaddr: dl_phys_addr(%s%d) failed: %d\n",
693 drv_name, ppa, rc);
694 }
695 (void) ldi_close(lh, FREAD|FWRITE, CRED());
696 return (macaddr);
697 }
698 #endif /* !_OBP */