netifd: read udhcpc user scripts from directory
[openwrt/staging/hauke.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 while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
10 proto_add_ipv4_route "${1%%/*}" "${1##*/}" "$2" "$ip"
11 max=$(($max-1))
12 shift 2
13 done
14 }
15
16 setup_interface () {
17 proto_init_update "*" 1
18 proto_add_ipv4_address "$ip" "${subnet:-255.255.255.0}"
19 # TODO: apply $broadcast
20
21 local ip_net
22 eval "$(ipcalc.sh "$ip/$mask")";ip_net="$NETWORK"
23
24 local i
25 for i in $router; do
26 local gw_net
27 eval "$(ipcalc.sh "$i/$mask")";gw_net="$NETWORK"
28
29 [ "$ip_net" != "$gw_net" ] && proto_add_ipv4_route "$i" 32 "" "$ip"
30 proto_add_ipv4_route 0.0.0.0 0 "$i" "$ip"
31
32 local r
33 for r in $CUSTOMROUTES; do
34 proto_add_ipv4_route "${r%%/*}" "${r##*/}" "$i" "$ip"
35 done
36 done
37
38 # CIDR STATIC ROUTES (rfc3442)
39 [ -n "$staticroutes" ] && set_classless_routes $staticroutes
40 [ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes
41
42 for i in $dns; do
43 proto_add_dns_server "$i"
44 done
45 for i in $domain; do
46 proto_add_dns_search "$i"
47 done
48
49 # TODO: Deprecate timesvr in favor of timesrv
50 if [ -n "$timesvr" -a -z "$timesrv" ]; then
51 timesrv="$timesvr"
52 echo "Environment variable 'timesvr' will be deprecated; use 'timesrv' instead."
53 fi
54
55 proto_add_data
56 [ -n "$ZONE" ] && json_add_string zone "$ZONE"
57 [ -n "$ntpsrv" ] && json_add_string ntpserver "$ntpsrv"
58 [ -n "$timesrv" ] && json_add_string timeserver "$timesrv"
59 [ -n "$hostname" ] && json_add_string hostname "$hostname"
60 [ -n "$message" ] && json_add_string message "$message"
61 [ -n "$timezone" ] && json_add_int timezone "$timezone"
62 [ -n "$lease" ] && json_add_int leasetime "$lease"
63 proto_close_data
64
65 proto_send_update "$INTERFACE"
66
67
68 if [ "$IFACE6RD" != 0 -a -n "$ip6rd" ]; then
69 local v4mask="${ip6rd%% *}"
70 ip6rd="${ip6rd#* }"
71 local ip6rdprefixlen="${ip6rd%% *}"
72 ip6rd="${ip6rd#* }"
73 local ip6rdprefix="${ip6rd%% *}"
74 ip6rd="${ip6rd#* }"
75 local ip6rdbr="${ip6rd%% *}"
76
77 [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE 2>/dev/null)
78 [ -z "$IFACE6RD" -o "$IFACE6RD" = 1 ] && IFACE6RD=${INTERFACE}_6
79
80 json_init
81 json_add_string name "$IFACE6RD"
82 json_add_string ifname "@$INTERFACE"
83 json_add_string proto "6rd"
84 json_add_string peeraddr "$ip6rdbr"
85 json_add_int ip4prefixlen "$v4mask"
86 json_add_string ip6prefix "$ip6rdprefix"
87 json_add_int ip6prefixlen "$ip6rdprefixlen"
88 json_add_string tunlink "$INTERFACE"
89 [ -n "$IFACE6RD_DELEGATE" ] && json_add_boolean delegate "$IFACE6RD_DELEGATE"
90 [ -n "$ZONE6RD" ] || ZONE6RD=$ZONE
91 [ -n "$ZONE6RD" ] && json_add_string zone "$ZONE6RD"
92 [ -n "$MTU6RD" ] && json_add_string mtu "$MTU6RD"
93 json_close_object
94
95 ubus call network add_dynamic "$(json_dump)"
96 fi
97 }
98
99 deconfig_interface() {
100 proto_init_update "*" 0
101 proto_send_update "$INTERFACE"
102 }
103
104 case "$1" in
105 deconfig)
106 deconfig_interface
107 ;;
108 renew|bound)
109 setup_interface
110 ;;
111 esac
112
113 # user rules
114 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user "$@"
115 for f in /etc/udhcpc.user.d/*; do
116 [ -f "$f" ] && (. "$f" "$@")
117 done
118
119 exit 0