summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/milestone/net-physical
blob: 5c459a362298f586f9c9baef878341db89095fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
#!/usr/bin/ksh93
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
# All rights reserved.
# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2012 Milan Jurik. All rights reserved.
# Copyright 2021 Joyent, Inc.
# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
#

#
# NOTE: Upstream illumos has IPMP configuration and upgrading of ipadm.conf in
# here.  SmartOS does not.  Recent improvements to IPMP specifically for
# upgrading ipadm.conf do not appear here, therefore.
#

. /lib/svc/share/smf_include.sh
. /lib/sdc/config.sh
. /lib/sdc/network.sh

set -o errexit
set -o xtrace

#
# In a shared-IP zone we need this service to be up, but all of the work
# it tries to do is irrelevant (and will actually lead to the service
# failing if we try to do it), so just bail out.
# In the global zone and exclusive-IP zones we proceed.
#
smf_configure_ip || exit ${SMF_EXIT_OK}

# Make sure that the libraries essential to this stage of booting can be found.
LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH

# Time (in seconds) to wait for admin NIC to get DHCP address before continuing.
ADMIN_DHCP_TIMEOUT=300
ActiveAggrLinks=
typeset -A ActiveAggrLinks

smf_netstrategy

function add_active_aggr_links
{
    set -o xtrace
    typeset alink

    for alink in ${2//,/ }; do
        ActiveAggrLinks[$alink]=$1
    done
}

# Waits for up to 10 seconds for the link state to change to the given value
function wait_for_admin_nic_state
{
    wait_for_nic_state "${SYSINFO_NIC_admin}" "$1"
}

# Plumbs the admin interface, and attempts to work around poorly-behaved
# drivers that can't handle plumb commands too quickly after one another
function plumb_admin
{
    set -o xtrace
    driver=${SYSINFO_NIC_admin%%[0-9]*}
    get_link_state ${SYSINFO_NIC_admin}
    if [[ "$link_state" == "down" ]]; then
        echo "admin nic '${SYSINFO_NIC_admin}' is down: unplumbing"
        /sbin/ifconfig ${SYSINFO_NIC_admin} down unplumb
        wait_for_admin_nic_state "unknown"
    fi

    # There's some sort of race condition in the bnx driver: if the plumb
    # command comes too soon after the unplumb, the interface can come up
    # in a state where it never fires interrupts for link state changes.
    if [[ "$driver" == "bnx" ]]; then
        sleep 5
    fi
    /sbin/ifconfig ${SYSINFO_NIC_admin} plumb mtu ${CONFIG_admin_mtu:-1500}
    wait_for_admin_nic_state "up"
}

# Creates, plumbs and brings up a vnic with the specified inet parameters
function vnic_up
{
    set -o xtrace

    typeset link="$1"
    typeset iface="$2"
    typeset ip="$3"
    typeset netmask="$4"
    typeset vlan_id="$5"
    typeset mac_addr="$6"
    typeset dhcp_primary="$7"
    typeset mtu="$8"
    typeset details=
    typeset vlan_opt=
    typeset mac_addr_opt=
    typeset prop_opt=

    details="link='${link}', iface='${iface}', ip='${ip}'"
    details="${details}, netmask='${netmask}, vlan_id='${vlan_id}'"

    if [[ -z ${link} ]] || [[ -z ${iface} ]] || \
       [[ -z ${ip} ]] || ([[ ${ip} != "dhcp" ]] && [[ -z ${netmask} ]]); then
        echo "WARNING: not bringing up nic (insufficient configuration): " \
             "$details"
        return
    fi

    eval "vnic_already_up=\${vnic_${iface}_up}"
    if [[ -n "${vnic_already_up}" ]]; then
        echo "vnic already up: $details"
        return
    fi

    if [[ -n ${mac_addr} ]] && [[ -n ${ActiveAggrLinks[${mac_addr}]} ]]; then
        echo "WARNING: trying to assign MAC address \"${mac_addr}\" to vnic," \
            " but it already belongs to link aggr " \
            "\"${ActiveAggrLinks[${mac_addr}]}\""
        return
    fi

    echo "Bringing up nic: $details"

    if [[ -n ${vlan_id} ]] && [[ ${vlan_id} != 0 ]]; then
        vlan_opt="-v ${vlan_id}"
    fi

    if [[ -n ${mac_addr} ]]; then
        mac_addr_opt="-m ${mac_addr}"
    fi

    if [[ -n ${mtu} ]]; then
        valid_mtu ${iface} ${mtu}
        prop_opt="-p mtu=${mtu}"
    fi

    /usr/sbin/dladm create-vnic -t -l ${link} ${prop_opt} ${vlan_opt} \
        ${mac_addr_opt} ${iface}
    if [[ $? -ne 0 ]]; then
        echo "Failed to create VNIC ${iface}"
        exit $SMF_EXIT_ERR_FATAL
    fi

    /sbin/ifconfig ${iface} plumb
    if [[ $? -ne 0 ]]; then
        echo "Failed to plumb ${iface}"
        exit $SMF_EXIT_ERR_FATAL
    fi

    if [[ ${ip} == "dhcp" ]]; then
        # We ignore errors here because the most common one is that DHCP
        # is already running.

        if [[ -n ${dhcp_primary} ]]; then
            /sbin/ifconfig ${iface} dhcp primary || /bin/true
        else
            /sbin/ifconfig ${iface} dhcp || /bin/true
        fi
    else
        /sbin/ifconfig ${iface} inet ${ip} netmask ${netmask} up
    fi
    eval "vnic_${iface}_up=true"
}

# Creates, plumbs and brings up a vnic with the specified inet6 parameters
function vnic_up6
{
    set -o xtrace

    typeset link="$1"
    typeset iface="$2"
    typeset ip="$3"
    typeset vlan_id="$4"
    typeset mac_addr="$5"
    typeset mtu="$6"
    typeset details=
    typeset vlan_opt=
    typeset mac_addr_opt=
    typeset prop_opt=

    details="link='${link}', iface='${iface}', ip6='${ip}'"
    details="${details}, vlan_id='${vlan_id}'"

    if [[ -z ${link} ]] || [[ -z ${iface} ]] || [[ -z ${ip} ]]; then
        echo "WARNING: not bringing up nic (insufficient configuration): " \
             "$details"
        return
    fi

    if [[ -n ${mac_addr} ]] && [[ -n ${ActiveAggrLinks[${mac_addr}]} ]]; then
        echo "WARNING: trying to assign MAC address \"${mac_addr}\" to vnic," \
            " but it already belongs to link aggr " \
            "\"${ActiveAggrLinks[${mac_addr}]}\""
        return
    fi

    # only bring up nic if not already up
    eval "vnic_already_up=\${vnic_${iface}_up}"
    if [[ -z "${vnic_already_up}" ]]; then
        echo "Bringing up nic: $details"

        if [[ -n ${vlan_id} ]] && [[ ${vlan_id} != 0 ]]; then
            vlan_opt="-v ${vlan_id}"
        fi

        if [[ -n ${mac_addr} ]]; then
            mac_addr_opt="-m ${mac_addr}"
        fi

        if [[ -n ${mtu} ]]; then
            valid_mtu ${iface} ${mtu}
            prop_opt="-p mtu=${mtu}"
        fi

        /usr/sbin/dladm create-vnic -t -l ${link} ${prop_opt} ${vlan_opt} \
            ${mac_addr_opt} ${iface}
        if [[ $? -ne 0 ]]; then
            echo "Failed to create VNIC ${iface}"
            exit $SMF_EXIT_ERR_FATAL
        fi
    fi

    /sbin/ifconfig ${iface} inet6 plumb
    if [[ $? -ne 0 ]]; then
        echo "Failed to plumb ${iface}"
        exit $SMF_EXIT_ERR_FATAL
    fi

    if [[ -n ${ip} ]]; then
        /sbin/ifconfig ${iface} inet6 up
    fi
    if [[ ${ip} != "addrconf" ]]; then
        /sbin/ifconfig ${iface} inet6 \
            addif ${ip} preferred up
    fi
    eval "vnic_${iface}_up=true"
}

# If there are aggregations in sysinfo, set them up.
function create_aggrs
{
    set -o xtrace
    typeset links macs mode mtu
    if [[ -z "${SYSINFO_Aggregations}" ]]; then
        return 0
    fi

    aggrs=(${SYSINFO_Aggregations//,/ })
    for aggr in "${aggrs[@]}"; do
        eval "links=\${SYSINFO_Aggregation_${aggr}_Interfaces}"
        eval "macs=\${SYSINFO_Aggregation_${aggr}_MACs}"
        eval "mode=\${SYSINFO_Aggregation_${aggr}_LACP_mode}"
        eval "mtu=\${CONFIG_${aggr}_mtu}"
        [[ -z "$mode" ]] && mode="off"

        echo "Creating aggr: ${aggr} (mode=${mode}, links=${links})"
        dladm create-aggr -l ${links//,/ -l } -L ${mode} ${aggr}
        if [[ $? -eq 0 ]]; then
            add_active_aggr_links ${aggr} ${macs}
        fi

        if [[ -n "$mtu" ]]; then
            dladm set-linkprop -p mtu=${mtu} ${aggr}
            if [[ $? -ne 0 ]]; then
                echo "Failed to set mtu on aggr ${aggr} to ${mtu}"
                exit $SMF_EXIT_ERR_FATAL
            fi
        fi
    done

    # Creating the aggregations may affect the nic tags in sysinfo, so update:
    /usr/bin/sysinfo -u
    load_sdc_sysinfo
}

#
# Try various config parameters to set the default route
#
function set_default_route
{
    set -o xtrace
    typeset default_gw

    if [[ -n "${CONFIG_headnode_default_gateway}" ]]; then
        default_gw="${CONFIG_headnode_default_gateway}"

    elif [[ -n ${CONFIG_admin_gateway} ]]; then
        default_gw="${CONFIG_admin_gateway}"

    elif [[ -n ${BOOT_admin_gateway} ]]; then
        default_gw=${BOOT_admin_gateway}

    elif [[ -n ${CONFIG_external_gateway} ]]; then
        default_gw=${CONFIG_external_gateway}
    fi

    if [[ -n ${default_gw} ]]; then
        echo "${default_gw}" > /etc/defaultrouter
    fi

    if [[ -n ${CONFIG_admin_gateway6} ]]; then
        default_gw6="${CONFIG_admin_gateway6}"

    elif [[ -n ${BOOT_admin_gateway6} ]]; then
        default_gw6=${BOOT_admin_gateway6}

    elif [[ -n ${CONFIG_external_gateway6} ]]; then
        default_gw6=${CONFIG_external_gateway6}
    fi

    if [[ -n ${default_gw6} ]]; then
        # add static route
        /usr/sbin/route add -inet6 default ${default_gw6}
    fi
}

#
# Go through and set up the MTU for all of the various nic tags
#
function setup_mtu
{
    set -o xtrace
    typeset tag oldifs val mac link curmtu
    typeset -A mtus
    typeset -A tagmap

    set -o xtrace
    oldifs=$IFS
    IFS=,
    for tag in ${SYSINFO_Nic_Tags}; do
        eval "val=\${CONFIG_${tag}_mtu}"
        eval "mac=\${CONFIG_${tag}_nic}"
        [[ -z "$val" ]] && continue

        valid_mtu ${tag} $val

        #
        # Note, it doesn't matter what tag we use for a given mac
        # address, because we'll always get the same link name later on.
        #
        if [[ -z "${tagmap[$mac]}" ]]; then
            tagmap[$mac]=$tag
        fi

        if [[ -z "${mtus[$mac]}" ]]; then
            mtus[$mac]=$val
        elif [[ "${mtus[$mac]}" -lt $val ]]; then
            mtus[$mac]=$val
        fi
    done
    IFS=$oldifs

    for mac in ${!mtus[@]}; do
        tag=${tagmap[$mac]}
        eval "link=\${SYSINFO_NIC_${tag}}"
        if [[ -z "${link}" ]]; then
            echo "/usbkey/config error: Missing link name for ${tag}"
            exit $SMF_EXIT_ERR_FATAL
        fi

        #
        # Check the current MTU of the device. To help out devices which
        # don't support the setting of the MTU (here's looking at you
        # bnx), if the MTU is identical to its default, don't do
        # anything and save the poor folks stuck with bnx some grief.
        #
        curmtu=$(/usr/sbin/dladm show-linkprop -c -o value -p mtu ${link})
        [[ $? -eq 0 ]] && [[ "$curmtu" -eq "${mtus[$mac]}" ]] && continue

            if ! /usr/sbin/dladm set-linkprop -p mtu=${mtus[$mac]} ${link}; then
                echo "Failed to set mtu to ${mtus[$mac]} for link ${link}"
                exit $SMF_EXIT_ERR_FATAL
            fi
    done
}

#
# Set up resolvers, if we can
#
function configure_resolv_conf
{
    if [[ ${RESOLV_CONF_DONE} == true ]]; then
        return
    elif [[ -n ${CONFIG_dns_domain} ]] && [[ -n ${CONFIG_dns_resolvers} ]]; then
        echo "search ${CONFIG_dns_domain}" > /etc/resolv.conf
        if [[ -n ${CONFIG_binder_admin_ips} ]]; then
            for serv in $(echo "${CONFIG_binder_admin_ips}" | sed -e "s/,/ /g"); do
                echo "nameserver ${serv}" >> /etc/resolv.conf
            done
        fi
        for serv in $(echo "${CONFIG_dns_resolvers}" | sed -e "s/,/ /g"); do
            echo "nameserver ${serv}" >> /etc/resolv.conf
        done
        RESOLV_CONF_DONE=true
    fi
}

# Helper function for plumbing an interface for an address family once
typeset -A plumbedifs
function plumbif
{
    iface=$1
    inet=$2
    addrtype=$3
    if [[ -z ${plumbedifs[${iface},${inet}]} ]]; then
        plumbedifs[${iface},${inet}]="true"
        /sbin/ifconfig ${iface} ${inet} plumb
        if [[ ! ${addrtype} =~ "^vrrp" ]]; then
            /sbin/ifconfig ${iface} ${inet} up
        fi
    fi
}

if smf_is_globalzone; then
    EARLY_ADMIN=
    [[ -f /etc/svc/volatile/.early_admin_setup ]] && EARLY_ADMIN=1
    [[ -n "$EARLY_ADMIN" ]] || /usr/sbin/dladm init-phys

    # The next command is for logging purposes only
    log_if_state before

    # Load sysinfo variables with SYSINFO_ prefix: we primarily care about
    # the NIC_variables, which contain the actual interface name for a nic
    # tag (it has mapped the foo_nic=<MAC address> variables to interface
    # names for us).
    load_sdc_sysinfo

    if boot_file_config_enabled; then
        # We have a boot-time networking file present - use its values rather
        # than ones from the config file or bootparams
        if ! boot_file_config_valid; then
            echo "ERROR: boot-time network config file incorrect"
            exit ${SMF_EXIT_ERR_CONFIG}
        fi

        load_boot_file_config

        # NOTE: some of the routes boot_file_config_init tries to add may
        # fail if they are admin network routes added by the
        # network/early-admin service.  This is expected and not a problem.
        boot_file_config_init
    else
        # Load config variables with CONFIG_ prefix,
        # and sets the headnode variable
        load_sdc_config
        # Load boot params with BOOT_ prefix
        load_sdc_bootparams
    fi

    # Set up etherstubs
    for stub in $(echo "${CONFIG_etherstub}" | sed -e "s/,/ /g"); do
        /usr/sbin/dladm create-etherstub -t $stub || echo "ERROR: could not create etherstub ${stub}."
    done

    # Create aggregations
    create_aggrs

    # Make any mtu adjustments that may be necessary
    setup_mtu

    # Setup admin NIC
    ADMIN_NIC_TAG=${CONFIG_admin_tag:-"admin"}

    # If there is no NIC with the admin tag, and the config has
    # admin_nic_autoselect=true, designate the first NIC reported
    # by dladm for admin use. This is useful in environments where
    # the NICs are known to change beneath us.
    if [[ "${BOOT_smartos}" == "true" ]] && \
        [[ "${CONFIG_admin_nic_autoselect}" == "true" ]] && \
        ! nictagadm exists $ADMIN_NIC_TAG ; then
        autoselected_admin_nic=$(dladm show-phys -m -p -o address | head -n1)
        if [[ -z ${autoselected_admin_nic} ]] ; then
            echo "ERROR: no NICs found, unable to autoselect admin NIC."
            exit ${SMF_EXIT_ERR_CONFIG}
        fi

        nictagadm add $ADMIN_NIC_TAG "${autoselected_admin_nic}"
        if [[ $? -ne 0 ]] ; then
            echo "ERROR: unable to add admin tag to NIC ${autoselected_admin_nic}"
            exit ${SMF_EXIT_ERR_FATAL}
        fi

        SYSINFO_NIC_admin=$(dladm show-phys -m -p -o link | head -n1)
        if [[ -n ${SYSINFO_NIC_admin} ]] ; then
            echo "Autoselected ${SYSINFO_NIC_admin} for use as admin NIC."
        fi

        nictagadm list
    elif [[ -v CONFIG_admin_tag ]]; then
        #
        # This handles the case when the 'admin_tag' property is set to
        # override the default admin nic tag.
        #
        eval SYSINFO_NIC_admin='$'SYSINFO_NIC_${CONFIG_admin_tag}

        eval CONFIG_admin_ip='$'CONFIG_${CONFIG_admin_tag}_ip
        eval CONFIG_admin_ip6='$'CONFIG_${CONFIG_admin_tag}_ip6
        eval CONFIG_admin_netmask='$'CONFIG_${CONFIG_admin_tag}_netmask
        eval CONFIG_admin_mtu='$'CONFIG_${CONFIG_admin_tag}_mtu
        eval CONFIG_admin_gateway='$'CONFIG_${CONFIG_admin_tag}_gateway
        eval CONFIG_admin_gateway6='$'CONFIG_${CONFIG_admin_tag}_gateway6
    fi

    if [[ -z "${SYSINFO_NIC_admin}" ]]; then
        echo "ERROR: admin NIC not found, unable to bring up admin network."
        exit ${SMF_EXIT_ERR_CONFIG}
    fi

    [[ -n "$EARLY_ADMIN" ]] || plumb_admin

    # If we performed early configuration of the admin network and
    # the admin interface is on a link aggregation, the aggregation
    # has already been configured.  Add it to the list of active aggrs
    # so the checks in vnic_up[6] are aware of it.
    if [[ -n "$EARLY_ADMIN" && "${SYSINFO_NIC_admin}" =~ aggr[0-9]+$ ]]; then
        eval "macs=\${SYSINFO_Aggregation_${SYSINFO_NIC_admin}_MACs}"

        add_active_aggr_links ${SYSINFO_NIC_admin} $macs
    fi

    # Prefer the config file for admin nic values, but use
    # bootparams if present
    admin_ip=${CONFIG_admin_ip}
    if [[ -z "$admin_ip" ]]; then
        admin_ip=${BOOT_admin_ip}
    fi

    admin_netmask=${CONFIG_admin_netmask}
    if [[ -z "$admin_netmask" ]]; then
        admin_netmask=${BOOT_admin_netmask}
    fi

    admin_ip6=${CONFIG_admin_ip6}
    if [[ -z "$admin_ip6" ]]; then
        admin_ip6=${BOOT_admin_ip6}
    fi

    if [[ $admin_ip == 'none' ]]; then
        echo 'INFO: not configuring IP on admin interface (admin_ip=none)'
    elif [[ -n "$EARLY_ADMIN" ]]; then
        echo 'INFO: admin interface already configured (early setup)'
        ADMIN_NIC_UP=true
    elif [[ -n $admin_ip ]] && [[ -n $admin_netmask ]]; then
        /sbin/ifconfig ${SYSINFO_NIC_admin} inet ${admin_ip} \
            netmask ${admin_netmask} up
        ADMIN_NIC_UP=true

        # also setup resolv.conf if we can
        configure_resolv_conf
    else
        if [[ ${headnode} == "true" ]]; then
            echo "ERROR: headnode but no admin_{ip,netmask} in config, not bringing up admin network."
            # Set a flag, but try to plumb the other interfaces anyway
            ADMIN_NIC_MISCONFIGURED=true
        else
            # We ignore errors here because the most common one is that DHCP is
            # already running.
            /sbin/ifconfig ${SYSINFO_NIC_admin} dhcp || /bin/true

            # Wait for DHCP
            timeout=${ADMIN_DHCP_TIMEOUT}
            dhcp_admin_ip=$(/sbin/ifconfig ${SYSINFO_NIC_admin} | grep inet | awk '{ print $2 }')
            while [[ (-z ${dhcp_admin_ip} || ${dhcp_admin_ip} == "0.0.0.0") && ${timeout} -gt 0 ]]; do
                dhcp_admin_ip=$(/sbin/ifconfig ${SYSINFO_NIC_admin} | grep inet | awk '{ print $2 }')
                timeout=$((${timeout} - 1))
                sleep 1
            done

            ADMIN_NIC_UP=true
        fi
    fi

    if [[ -n ${admin_ip6} && -z "$EARLY_ADMIN" ]]; then
        # Plumb interface for inet6
        ifconfig ${SYSINFO_NIC_admin} inet6 \
            plumb mtu ${CONFIG_admin_mtu:-1500} up

        # Autodiscovery IPv6 using SLAAC
        # NOTE: in.ndpd will be started later, due to plumbing the interface.
        #       this means autodiscovery also happens when configuring a
        #       static address.
        #
        #       in.ndpd also sets a default route and this can't be disabled.

        # Configure static IPv6
        if [[ ${admin_ip6} != "addrconf" ]]; then
            /sbin/ifconfig ${SYSINFO_NIC_admin} inet6 \
                addif ${admin_ip6} preferred up
        fi

        ADMIN_NIC_UP=true

        # don't setup resolv.conf, IPv6 addresses already work
    fi

    # If on Parallels or VirtualBox, create a bridge which
    # allows traffic to flow correctly to the host-only network
    if [[ "${ADMIN_NIC_UP}" == "true" ]] \
        && [[ ${SYSINFO_Product} == "Parallels Virtual Platform" \
              || ${SYSINFO_Product} == "VirtualBox" ]] \
        && [[ -z $(/usr/sbin/dladm show-bridge -p vmwarebr) ]]; then
        /usr/sbin/dladm create-bridge -l ${SYSINFO_NIC_admin} vmwarebr
    fi

    # Setup the external NIC. The installer may have already set up external0,
    # so, if it exists, we're not going to try and set up the vnic again.
    if [[ -n ${SYSINFO_NIC_external} ]] \
        && ! dladm show-vnic external0 > /dev/null; then

        if [[ -n "${CONFIG_external_ip}" ]]; then
            vnic_up "${SYSINFO_NIC_external}" "external0" \
                "${CONFIG_external_ip}" "${CONFIG_external_netmask}" \
                "${CONFIG_external_vlan_id}" "${CONFIG_external_mac}" \
                "primary" "${CONFIG_external_mtu}"
        fi

        if [[ -n "${CONFIG_external_ip6}" ]]; then
            vnic_up6 "${SYSINFO_NIC_external}" "external0" \
                "${CONFIG_external_ip6}" \
                "${CONFIG_external_vlan_id}" "${CONFIG_external_mac}" \
                "${CONFIG_external_mtu}"
        fi
    fi

    set_default_route

    # Setup extra nics, if specified in the config file
    nic_tags="${SYSINFO_Nic_Tags}"
    if [[ -n "${nic_tags}" ]]; then
        tags=(${nic_tags//,/ })

        if boot_file_config_enabled; then
            bootparam_ip_keys=""
            bootparam_ip6_keys=""
            config_ip_keys=${CONFIG_bootfile_ip_keys//,/ }
            config_ip6_keys=${CONFIG_bootfile_ip6_keys//,/ }
        else
            bootparam_ip_keys=$(sdc_bootparams_keys | grep -- "-ip$" || true)
            bootparam_ip6_keys=$(sdc_bootparams_keys | grep -- "-ip6$" || true)
            config_ip_keys=$(sdc_config_keys | grep "_ip$" || true)
            config_ip6_keys=$(sdc_config_keys | grep "_ip6$" || true)
        fi

        for tag in "${tags[@]}"; do

            eval "link=\${SYSINFO_NIC_${tag}}"
            if [[ -z "${link}" ]]; then
                echo "WARNING: No link found with tag '${tag}'"
                continue
            fi

            for key in ${config_ip_keys}; do
                if [[ ${key} == ${tag}[0-9]_ip ]] || [[ ${key} == ${tag}[0-9][0-9]_ip ]]; then
                    iface=${key//_ip/}
                    #echo "   iface=$iface"
                    eval "ip=\${CONFIG_${iface}_ip}"
                    eval "netmask=\${CONFIG_${iface}_netmask}"
                    eval "vlan=\${CONFIG_${iface}_vlan_id}"
                    eval "macaddr=\${CONFIG_${iface}_mac}"
                    eval "mtu=\${CONFIG_${iface}_mtu}"

                    echo vnic_up "${link}" "${iface}" "${ip}" "${netmask}" "${vlan}" "${macaddr}" "${mtu}"
                    vnic_up "${link}" "${iface}" "${ip}" "${netmask}" \
                        "${vlan}" "${macaddr}" "" "${mtu}"
                fi
            done

            for key in ${bootparam_ip_keys}; do
                if [[ ${key} == ${tag}[0-9]-ip ]] || [[ ${key} == ${tag}[0-9][0-9]-ip ]]; then
                    iface=${key//-ip/}
                    eval "ip=\${BOOT_${iface}_ip}"
                    eval "netmask=\${BOOT_${iface}_netmask}"
                    eval "vlan=\${BOOT_${iface}_vlan_id}"
                    eval "macaddr=\${BOOT_${iface}_mac}"
                    eval "mtu=\${CONFIG_${iface}_mtu}"
                    echo vnic_up "${link}" "${iface}" "${ip}" "${netmask}" "${vlan}" "${macaddr}" "${mtu}"
                    vnic_up "${link}" "${iface}" "${ip}" "${netmask}" \
                        "${vlan}" "${macaddr}" "" "${mtu}"
                fi
            done

            for key in ${config_ip6_keys}; do
                if [[ ${key} == ${tag}[0-9]_ip6 ]] || [[ ${key} == ${tag}[0-9][0-9]_ip6 ]]; then
                    iface=${key//_ip6/}
                    #echo "   iface=$iface"
                    eval "ip=\${CONFIG_${iface}_ip6}"
                    eval "vlan=\${CONFIG_${iface}_vlan_id}"
                    eval "macaddr=\${CONFIG_${iface}_mac}"
                    eval "mtu=\${CONFIG_${iface}_mtu}"

                    echo vnic_up6 "${link}" "${iface}" "${ip}" "${vlan}" "${macaddr}" "${mtu}"
                    vnic_up6 "${link}" "${iface}" "${ip}" \
                        "${vlan}" "${macaddr}" "${mtu}"
                fi
            done

            for key in ${bootparam_ip6_keys}; do
                if [[ ${key} == ${tag}[0-9]-ip6 ]] || [[ ${key} == ${tag}[0-9][0-9]-ip6 ]]; then
                    iface=${key//-ip6/}
                    eval "ip=\${BOOT_${iface}_ip6}"
                    eval "vlan=\${BOOT_${iface}_vlan_id}"
                    eval "macaddr=\${BOOT_${iface}_mac}"
                    eval "mtu=\${CONFIG_${iface}_mtu}"
                    echo vnic_up6 "${link}" "${iface}" "${ip}" "${vlan}" "${macaddr}" "${mtu}"
                    vnic_up6 "${link}" "${iface}" "${ip}" \
                        "${vlan}" "${macaddr}" "${mtu}"
                fi
            done

        done

        # All vnics are done. If the config has admin_ip=none, then we won't
        # have resolvers set up yet, so try again here.
        configure_resolv_conf
    fi
else
    # Non-global zones

    # Bring up statically assigned interfaces, and find the primary DHCP
    # interface, if it exists
    while IFS=: read -r iface addrtype; do
        # Keep track of whether or not we've configured our first IPv4
        # address on this interface
        first_ipv4_configured=""
        iface_configured=""

        if [[ -f /etc/dhcp.${iface} ]]; then
            plumbif ${iface} inet ${addrtype}
            if [[ -z "${primary}" ]]; then
                /sbin/ifconfig ${iface} auto-dhcp primary
                primary=${iface}
            else
                /sbin/ifconfig ${iface} auto-dhcp
            fi
            first_ipv4_configured="true"
            iface_configured="true"
        fi

        if [[ -f /etc/hostname.${iface} ]]; then
            while read ifparams; do
                # For IPv4, we need to set the address on the first logical
                # interface. For IPv6, the address on the first interface is
                # the link-local address, and can't be changed.
                #
                # Lines starting with inet indicate the hostname for that
                # interface, and are used by dhcpagent. Skip over them.
                if [[ -f /etc/dhcp.${iface} && "${ifparams}" == inet* ]]; then
                    continue
                elif [[ "${ifparams}" == {3}({1,3}(\d).){1,3}(\d)* ]]; then
                    plumbif ${iface} inet ${addrtype}
                    if [[ -z "${first_ipv4_configured}" ]]; then
                        first_ipv4_configured="true"
                        ifcommand="inet"
                    else
                        ifcommand="inet addif"
                    fi
                else
                    plumbif ${iface} inet6 ${addrtype}
                    ifcommand="inet6 addif"
                fi

                # vrrp interfaces can't be brought up with ifconfig: vrrpadm
                # handles that instead
                if [[ "${addrtype}" =~ "^vrrp" ]]; then
                    /sbin/ifconfig ${iface} ${ifcommand} \
                        `printf "%s" "${ifparams}" | sed -e 's/ up//'`
                else
                    /sbin/ifconfig ${iface} ${ifcommand} ${ifparams} up
                fi

                iface_configured="true"
            done < /etc/hostname.${iface}
        fi

        if [[ -f /etc/addrconf.${iface} ]]; then
            # Enable the NDP daemon, so that once this script finishes, we'll
            # be able to pick up router advertisments and finish configuring
            # the network interface. We then just need to plumb the interface,
            # and let in.ndpd take care of configuring addresses.
            svcadm enable svc:/network/routing/ndp:default
            plumbif ${iface} inet6 ${addrtype}
            iface_configured="true"
        fi

        # If we didn't configure the device at all, mark it for DHCP if we
        # don't do DHCP on anyone else.
        if [[ -z ${iface_configured} && -z "${first_iface}" ]]; then
            first_iface=${iface}
        fi
    done < <(/usr/sbin/dladm show-vnic -p -o link,macaddrtype 2>/dev/null)

    if [[ -z "${primary}" && -n "${first_iface}" ]]; then
        first_iface_type=$(dladm show-vnic ${first_iface} -p -o macaddrtype)
        plumbif ${first_iface} inet ${first_iface_type}
        /sbin/ifconfig ${first_iface} auto-dhcp
        primary=${first_iface}
    fi

    while IFS=: read -r iface addrtype; do
        if [[ "${iface}" == "${primary}"
              || -f /etc/hostname.${iface}
              || -f /etc/dhcp.${iface}
              || -f /etc/addrconf.${iface}
              || "${addrtype}" =~ "^vrrp" ]]; then
            continue
        fi

        plumbif ${iface} inet ${addrtype}
        /sbin/ifconfig ${iface} auto-dhcp start wait 0
    done < <(/usr/sbin/dladm show-vnic -p -o link,macaddrtype 2>/dev/null)
fi

log_if_state after

# Since we hopefully made networking changes here, update the sysinfo cache
if smf_is_globalzone; then
    /usr/bin/sysinfo -u
fi

# Enable symmetric routing: when there are multiple nics configured, always
# take into account the interface a packet is being sent over when
# selecting a route. This prevents packets being sent with another nic's
# source IP.
/usr/sbin/ndd -set /dev/ip ip_strict_src_multihoming 1

# If the admin nic was missing config options, exit with a config error
if [[ -n "${ADMIN_NIC_MISCONFIGURED}" ]]; then
    exit ${SMF_EXIT_ERR_CONFIG}
fi

if [[ $admin_ip == 'none' ]]; then
    #
    # We're done, even if there are not any usable IP addresses.
    #
    exit $SMF_EXIT_OK
fi

# Any non-loopback IPv4 interfaces with usable addresses up?
if [[ -n "`/sbin/ifconfig -a4u`" ]]; then
    /sbin/ifconfig -a4u | while read intf addr rest; do
        [[ ${intf} == "inet" ]] && [[ ${addr} != "127.0.0.1" ]] &&
        [[ ${addr} != "0.0.0.0" ]] && exit ${SMF_EXIT_OK}
    done && exit ${SMF_EXIT_OK}
fi

# Any DHCP interfaces started?
[[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ]] && exit ${SMF_EXIT_OK}

# Any non-loopback IPv6 interfaces up?
if [[ -n "`/sbin/ifconfig -au6`" ]]; then
    /sbin/ifconfig -au6 | while read intf addr rest; do
        [[ ${intf} = "inet6" ]] && [[ ${addr} != "::1/128" ]] && exit ${SMF_EXIT_OK}
    done && exit ${SMF_EXIT_OK}
fi

# This service was supposed to configure something yet didn't.  Exit
# with config error.
exit ${SMF_EXIT_ERR_CONFIG}