fb9f95a5176ceba9b5251fe6d84d0e8ce58d94a9
[openwrt/staging/chunkeey.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 for i in $router; do
22 proto_add_ipv4_route "$i" 32 "" "$ip"
23 proto_add_ipv4_route 0.0.0.0 0 "$i" "$ip"
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
37 proto_add_data
38 [ -n "$ZONE" ] && json_add_string zone "$ZONE"
39 [ -n "$ntpsrv" ] && json_add_string ntpserver "$ntpsrv"
40 [ -n "$timesvr" ] && json_add_string timeserver "$timesvr"
41 [ -n "$hostname" ] && json_add_string hostname "$hostname"
42 [ -n "$message" ] && json_add_string message "$message"
43 [ -n "$timezone" ] && json_add_int timezone "$timezone"
44 [ -n "$lease" ] && json_add_int leasetime "$lease"
45 proto_close_data
46
47 proto_send_update "$INTERFACE"
48
49
50 if [ "$IFACE6RD" != 0 -a -n "$ip6rd" ]; then
51 local v4mask="${ip6rd%% *}"
52 ip6rd="${ip6rd#* }"
53 local ip6rdprefixlen="${ip6rd%% *}"
54 ip6rd="${ip6rd#* }"
55 local ip6rdprefix="${ip6rd%% *}"
56 ip6rd="${ip6rd#* }"
57 local ip6rdbr="${ip6rd%% *}"
58
59 [ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE)
60 [ -z "$IFACE6RD" -o "$IFACE6RD" = 1 ] && IFACE6RD=${INTERFACE}_6
61
62 json_init
63 json_add_string name "$IFACE6RD"
64 json_add_string ifname "@$INTERFACE"
65 json_add_string proto "6rd"
66 json_add_string peeraddr "$ip6rdbr"
67 json_add_int ip4prefixlen "$v4mask"
68 json_add_string ip6prefix "$ip6rdprefix"
69 json_add_int ip6prefixlen "$ip6rdprefixlen"
70 json_add_string tunlink "$INTERFACE"
71 [ -n "$IFACE6RD_DELEGATE" ] && json_add_boolean delegate "$IFACE6RD_DELEGATE"
72 [ -n "$ZONE6RD" ] || ZONE6RD=$ZONE
73 [ -n "$ZONE6RD" ] && json_add_string zone "$ZONE6RD"
74 [ -n "$MTU6RD" ] && json_add_string mtu "$MTU6RD"
75 json_close_object
76
77 ubus call network add_dynamic "$(json_dump)"
78 fi
79 }
80
81 deconfig_interface() {
82 proto_init_update "*" 0
83 proto_send_update "$INTERFACE"
84 }
85
86 case "$1" in
87 deconfig)
88 deconfig_interface
89 ;;
90 renew|bound)
91 setup_interface
92 ;;
93 esac
94
95 # user rules
96 [ -f /etc/udhcpc.user ] && . /etc/udhcpc.user
97
98 exit 0