poco: moved to github
[packages.git] / ipv6 / wide-dhcpv6 / files / dhcp6c.init
1 #!/bin/sh /etc/rc.common
2
3 DHCP6C_REQUEST_OPTIONS='domain_name_servers domain_name ntp_servers sip_server_address sip_domain_name nis_server_address nis_domain_name nisp_server_address nisp_domain_name bcmcs_server_address bcmcs_domain_name'
4
5 dhcp6c_write_duid() {
6         local mac="${1:-$(ifconfig "$client_device" | sed -ne 's/[[:space:]]*$//; s/.*HWaddr //p')}"
7         local pat="[0-9A-F][0-9A-F]"
8
9         case "$mac" in
10                 # exactly 6 octets -> assume type 3 (DUID-LL - RFC3315, 9.4)
11                 $pat:$pat:$pat:$pat:$pat:$pat)
12                         local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
13
14                         # low endian
15                         if [ "$(printf \\1 | hexdump -n1 -ve '8/2 "" "%04x" ""')" = "0001" ]; then
16                                 printf \\x0a\\x00
17
18                         # big endian
19                         else
20                                 printf \\x00\\x0a
21                         fi
22
23                         printf \\x00\\x03\\x00\\x06\\x$1\\x$2\\x$3\\x$4\\x$5\\x$6
24                         logger -t dhcp6c "Using MAC address DUID 00:03:00:06:$1:$2:$3:$4:$5:$6"
25                 ;;
26                 # at least 7 octets -> could be type 1 or type 2
27                 $pat:$pat:$pat:$pat:$pat:$pat:*)
28                         local len_id=":$(echo "$mac" | sed -e 's/[^:]//g')"
29                         local len_hi=$(printf "%02x" $((${#len_id} / 0xFF)) )
30                         local len_lo=$(printf "%02x" $((${#len_id} % 0xFF)) )
31
32                         # low endian
33                         if [ "$(printf \\1 | hexdump -n1 -ve '8/2 "" "%04x" ""')" = "0001" ]; then
34                                 printf \\x$len_lo\\x$len_hi
35
36                         # big endian
37                         else
38                                 printf \\x$len_hi\\x$len_lo
39                         fi
40
41                         printf $(echo "$mac" | sed -e 's/^/\\x/; s/:/\\x/g')
42                         logger -t dhcp6c "Using user provided DUID $mac"
43                 ;;
44                 *)
45                         logger -t dhcp6c "Unable to derive DUID from interface '$client_device' and no valid user DUID given"
46                 ;;
47         esac
48 }
49
50 dhcp6c_write_interface() {
51         local cfg=$1
52         local sla_id
53         local sla_len
54         local enabled
55         local device
56
57         config_get_bool enabled "$cfg" enabled 0
58
59         if [ $enabled -ne 0 ]; then
60                 config_get sla_id "$cfg" sla_id
61                 config_get sla_len "$cfg" sla_len
62
63                 network_get_device device "$cfg" && {
64                         printf '\tprefix-interface %s {\n' "$device"
65                         printf '\t\tsla-id %s;\n' "$sla_id"
66                         printf '\t\tsla-len %s;\n' "$sla_len"
67                         printf '\t};\n'
68                 }
69         fi
70 }
71
72 dhcp6c_write_request() {
73         printf '\trequest %s;\n' $1 | sed -e s/_/-/g
74 }
75
76 dhcp6c_write_config() {
77         local pd
78         local na
79         config_get_bool pd basic pd 0
80         config_get_bool na basic na 0
81
82         printf 'interface %s {\n' "$client_ifname"
83
84         if [ $pd -ne 0 ]; then
85                 printf '\tsend ia-pd 0;\n'
86         fi
87
88         if [ $na -ne 0 ]; then
89                 printf '\tsend ia-na 0;\n'
90         fi
91
92         local rapid_commit
93         config_get_bool rapid_commit basic rapid_commit 0
94         [ $rapid_commit -ne 0 ] && printf '\tsend rapid-commit;\n'
95
96         local script
97         config_get script basic script
98         [ "$script" != "" ] && printf '\tscript "%s";\n' "$script"
99
100         local request
101         local value
102         for request in $DHCP6C_REQUEST_OPTIONS; do
103                 config_get_bool value basic "$request" 0
104                 [ $value -ne 0 ] && dhcp6c_write_request "$request"
105         done
106
107         printf '};\n\n'
108
109         if [ $pd -ne 0 ]; then
110                 printf 'id-assoc pd 0 {\n'
111                 config_foreach dhcp6c_write_interface interface
112                 printf '};\n\n'
113         fi
114
115         if [ $na -ne 0 ]; then
116                 printf 'id-assoc na 0 {\n'
117                 printf '};\n\n'
118         fi
119
120         return 0
121 }
122
123 start() {
124         [ ! -e /etc/dhcp6cctlkey ] && `dd if=/dev/urandom count=1 2> /dev/null | md5sum | cut -d" " -f1 > /etc/dhcp6cctlkey`
125
126         [ -e /var/run/dhcp6c.pid ] && return 0
127
128         . /lib/functions/network.sh
129
130         config_load "dhcp6c"
131
132         local enabled
133         config_get_bool enabled basic enabled 0
134         [ $enabled -eq 0 ] && return 0
135
136         local user_duid
137         config_get user_duid basic duid
138
139         logger -t dhcp6c starting dhcp6c
140
141         local client_interface client_ifname client_device
142         config_get client_interface basic interface
143         network_get_device  client_ifname "$client_interface"
144         network_get_physdev client_device "$client_interface" || client_device="$client_ifname"
145
146         local config_file="/var/etc/dhcp6c.conf"
147         local duid_file="/var/dhcp6c_duid"
148         mkdir -m 755 -p /var/etc
149         dhcp6c_write_config > $config_file
150         dhcp6c_write_duid "$user_duid" > $duid_file
151
152         local debug
153         local debug_option
154         config_get_bool debug basic debug 0
155         [ $debug -eq 1 ] && debug_option="-D"
156         /usr/sbin/dhcp6c -c $config_file $debug_option $client_ifname
157
158         sleep 3
159         ACTION=start /sbin/hotplug-call dhcp6c
160
161         return 0
162 }
163
164 stop() {
165         logger -t dhcp6c stopping dhcp6c
166         service_kill dhcp6c /var/run/dhcp6c.pid
167         rm -f /var/etc/dhcp6c.conf /var/run/dhcp6c.pid
168         ACTION=stop /sbin/hotplug-call dhcp6c
169         return 0
170 }
171
172