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