50163da514301928849120f678eb4a59300af752
[openwrt.git] / package / network / config / netifd / files / lib / netifd / dhcp.script
1 #!/bin/sh
2 [ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1
3
4 . /lib/functions.sh
5 . /lib/netifd/netifd-proto.sh
6
7 set_classless_routes() {
8         local max=128
9         local type
10         while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
11                 proto_add_ipv4_route "${1%%/*}" "${1##*/}" "$2"
12                 max=$(($max-1))
13                 shift 2
14         done
15 }
16
17 setup_interface () {
18         proto_init_update "*" 1
19         proto_add_ipv4_address "$ip" "${subnet:-255.255.255.0}"
20         # TODO: apply $broadcast
21
22         for i in $router; do
23                 proto_add_ipv4_route 0.0.0.0 0 "$i"
24         done
25
26         # CIDR STATIC ROUTES (rfc3442)
27         [ -n "$staticroutes" ] && set_classless_routes $staticroutes
28         [ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes
29
30         for dns in $dns; do
31                 proto_add_dns_server "$dns"
32         done
33         for domain in $domain; do
34                 proto_add_dns_search "$domain"
35         done
36         proto_send_update "$INTERFACE"
37
38         if [ -n "$IFACE6RD" -a -n "$ip6rd" ]; then
39                 local v4mask="${ip6rd%% *}"
40                 ip6rd="${ip6rd#* }"
41                 local ip6rdprefixlen="${ip6rd%% *}"
42                 ip6rd="${ip6rd#* }"
43                 local ip6rdprefix="${ip6rd%% *}"
44                 ip6rd="${ip6rd#* }"
45                 local ip6rdbr="${ip6rd%% *}"
46
47 uci -q batch <<-EOF >/dev/null
48 set network.$IFACE6RD.proto=6rd
49 set network.$IFACE6RD.auto=0
50 set network.$IFACE6RD.peeraddr=$ip6rdbr
51 set network.$IFACE6RD.ip4prefixlen=$v4mask
52 set network.$IFACE6RD.ip6prefix=$ip6rdprefix
53 set network.$IFACE6RD.ip6prefixlen=$ip6rdprefixlen
54 commit network
55 EOF
56
57                 ifdown "$IFACE6RD"
58                 /etc/init.d/network reload
59                 ifup "$IFACE6RD"
60         fi
61
62         # TODO
63         # [ -n "$ntpsrv" ] &&   change_state network "$ifc" lease_ntpsrv "$ntpsrv"
64         # [ -n "$timesvr" ] &&  change_state network "$ifc" lease_timesrv "$timesvr"
65         # [ -n "$hostname" ] && change_state network "$ifc" lease_hostname "$hostname"
66         # [ -n "$timezone" ] &&         change_state network "$ifc" lease_timezone "$timezone"
67 }
68
69 deconfig_interface() {
70         proto_init_update "*" 0
71         proto_send_update "$INTERFACE"
72 }
73
74 case "$1" in
75         deconfig)
76                 deconfig_interface
77         ;;
78         renew|bound)
79                 setup_interface
80         ;;
81 esac
82
83 # user rules
84 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
85
86 exit 0