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