allow the user to disable the defaultroute option for ppp
[openwrt/openwrt.git] / package / ppp / patches / 108-debian_defaultroute.patch
1 Index: ppp-2.4.3/pppd/ipcp.c
2 ===================================================================
3 --- ppp-2.4.3.orig/pppd/ipcp.c 2007-06-04 13:22:09.003486544 +0200
4 +++ ppp-2.4.3/pppd/ipcp.c 2007-06-04 13:22:11.387124176 +0200
5 @@ -197,6 +197,16 @@
6 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
7 &ipcp_wantoptions[0].default_route },
8
9 +#ifdef __linux__
10 + { "replacedefaultroute", o_bool,
11 + &ipcp_wantoptions[0].replace_default_route,
12 + "Replace default route", 1
13 + },
14 + { "noreplacedefaultroute", o_bool,
15 + &ipcp_allowoptions[0].replace_default_route,
16 + "Never replace default route", OPT_A2COPY,
17 + &ipcp_wantoptions[0].replace_default_route },
18 +#endif
19 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
20 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
21 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
22 @@ -263,7 +273,7 @@
23 ip_active_pkt
24 };
25
26 -static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t));
27 +static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t, bool));
28 static void ipcp_script __P((char *)); /* Run an up/down script */
29 static void ipcp_script_done __P((void *));
30
31 @@ -1659,7 +1669,12 @@
32 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
33 return 0;
34 if (wo->default_route)
35 +#ifndef __linux__
36 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
37 +#else
38 + if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
39 + wo->replace_default_route))
40 +#endif
41 default_route_set[u] = 1;
42 if (wo->proxy_arp)
43 if (sifproxyarp(u, wo->hisaddr))
44 @@ -1741,7 +1756,8 @@
45 */
46 if (demand) {
47 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
48 - ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
49 + ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr,
50 + wo->replace_default_route);
51 if (go->ouraddr != wo->ouraddr) {
52 warn("Local IP address changed to %I", go->ouraddr);
53 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
54 @@ -1766,7 +1782,12 @@
55
56 /* assign a default route through the interface if required */
57 if (ipcp_wantoptions[f->unit].default_route)
58 +#ifndef __linux__
59 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
60 +#else
61 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
62 + wo->replace_default_route))
63 +#endif
64 default_route_set[f->unit] = 1;
65
66 /* Make a proxy ARP entry if requested. */
67 @@ -1813,7 +1834,12 @@
68
69 /* assign a default route through the interface if required */
70 if (ipcp_wantoptions[f->unit].default_route)
71 +#ifndef __linux__
72 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
73 +#else
74 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
75 + wo->replace_default_route))
76 +#endif
77 default_route_set[f->unit] = 1;
78
79 /* Make a proxy ARP entry if requested. */
80 @@ -1890,7 +1916,7 @@
81 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
82 sifdown(f->unit);
83 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
84 - ipcp_hisoptions[f->unit].hisaddr);
85 + ipcp_hisoptions[f->unit].hisaddr, 0);
86 }
87
88 /* Execute the ip-down script */
89 @@ -1906,16 +1932,25 @@
90 * proxy arp entries, etc.
91 */
92 static void
93 -ipcp_clear_addrs(unit, ouraddr, hisaddr)
94 +ipcp_clear_addrs(unit, ouraddr, hisaddr, replacedefaultroute)
95 int unit;
96 u_int32_t ouraddr; /* local address */
97 u_int32_t hisaddr; /* remote address */
98 + bool replacedefaultroute;
99 {
100 if (proxy_arp_set[unit]) {
101 cifproxyarp(unit, hisaddr);
102 proxy_arp_set[unit] = 0;
103 }
104 - if (default_route_set[unit]) {
105 + /* If replacedefaultroute, sifdefaultroute will be called soon
106 + * with replacedefaultroute set and that will overwrite the current
107 + * default route. This is the case only when doing demand, otherwise
108 + * during demand, this cifdefaultroute would restore the old default
109 + * route which is not what we want in this case. In the non-demand
110 + * case, we'll delete the default route and restore the old if there
111 + * is one saved by an sifdefaultroute with replacedefaultroute.
112 + */
113 + if (!replacedefaultroute && default_route_set[unit]) {
114 cifdefaultroute(unit, ouraddr, hisaddr);
115 default_route_set[unit] = 0;
116 }
117 Index: ppp-2.4.3/pppd/ipcp.h
118 ===================================================================
119 --- ppp-2.4.3.orig/pppd/ipcp.h 2007-06-04 13:22:08.263599024 +0200
120 +++ ppp-2.4.3/pppd/ipcp.h 2007-06-04 13:22:11.387124176 +0200
121 @@ -70,6 +70,7 @@
122 bool old_addrs; /* Use old (IP-Addresses) option? */
123 bool req_addr; /* Ask peer to send IP address? */
124 bool default_route; /* Assign default route through interface? */
125 + bool replace_default_route; /* Replace default route through interface? */
126 bool proxy_arp; /* Make proxy ARP entry for peer? */
127 bool neg_vj; /* Van Jacobson Compression? */
128 bool old_vj; /* use old (short) form of VJ option? */
129 Index: ppp-2.4.3/pppd/pppd.h
130 ===================================================================
131 --- ppp-2.4.3.orig/pppd/pppd.h 2007-06-04 13:22:09.005486240 +0200
132 +++ ppp-2.4.3/pppd/pppd.h 2007-06-04 13:22:11.388124024 +0200
133 @@ -642,7 +642,11 @@
134 int cif6addr __P((int, eui64_t, eui64_t));
135 /* Remove an IPv6 address from i/f */
136 #endif
137 +#ifndef __linux__
138 int sifdefaultroute __P((int, u_int32_t, u_int32_t));
139 +#else
140 +int sifdefaultroute __P((int, u_int32_t, u_int32_t, bool replace_default_rt));
141 +#endif
142 /* Create default route through i/f */
143 int cifdefaultroute __P((int, u_int32_t, u_int32_t));
144 /* Delete default route through i/f */
145 Index: ppp-2.4.3/pppd/sys-linux.c
146 ===================================================================
147 --- ppp-2.4.3.orig/pppd/sys-linux.c 2007-06-04 13:22:08.807516336 +0200
148 +++ ppp-2.4.3/pppd/sys-linux.c 2007-06-04 13:22:11.389123872 +0200
149 @@ -206,6 +206,8 @@
150
151 static int if_is_up; /* Interface has been marked up */
152 static u_int32_t default_route_gateway; /* Gateway for default route added */
153 +static struct rtentry old_def_rt; /* Old default route */
154 +static int default_rt_repl_rest; /* replace and restore old default rt */
155 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
156 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
157 static u_int32_t our_old_addr; /* for detecting address changes */
158 @@ -1520,6 +1522,9 @@
159 p = NULL;
160 }
161
162 + SET_SA_FAMILY (rt->rt_dst, AF_INET);
163 + SET_SA_FAMILY (rt->rt_gateway, AF_INET);
164 +
165 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
166 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
167 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
168 @@ -1589,19 +1594,53 @@
169 /********************************************************************
170 *
171 * sifdefaultroute - assign a default route through the address given.
172 - */
173 -
174 -int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
175 -{
176 - struct rtentry rt;
177 -
178 - if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
179 - u_int32_t old_gateway = SIN_ADDR(rt.rt_gateway);
180 -
181 - if (old_gateway != gateway)
182 - error("not replacing existing default route to %s [%I]",
183 - rt.rt_dev, old_gateway);
184 - return 0;
185 + *
186 + * If the global default_rt_repl_rest flag is set, then this function
187 + * already replaced the original system defaultroute with some other
188 + * route and it should just replace the current defaultroute with
189 + * another one, without saving the current route. Use: demand mode,
190 + * when pppd sets first a defaultroute it it's temporary ppp0 addresses
191 + * and then changes the temporary addresses to the addresses for the real
192 + * ppp connection when it has come up.
193 + */
194 +
195 +int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
196 +{
197 + struct rtentry rt, tmp_rt;
198 + struct rtentry *del_rt = NULL;
199 +
200 +
201 + if (default_rt_repl_rest) {
202 + /* We have already reclaced the original defaultroute, if we
203 + * are called again, we will delete the current default route
204 + * and set the new default route in this function.
205 + * - this is normally only the case the doing demand: */
206 + if (defaultroute_exists( &tmp_rt ))
207 + del_rt = &tmp_rt;
208 + } else if ( defaultroute_exists( &old_def_rt ) &&
209 + strcmp( old_def_rt.rt_dev, ifname ) != 0) {
210 + /* We did not yet replace an existing default route, let's
211 + * check if we should save and replace a default route:
212 + */
213 + u_int32_t old_gateway = SIN_ADDR(old_def_rt.rt_gateway);
214 +
215 + if (old_gateway != gateway) {
216 + if (!replace) {
217 + error("not replacing default route to %s [%I]",
218 + old_def_rt.rt_dev, old_gateway);
219 + return 0;
220 + } else {
221 + // we need to copy rt_dev because we need it permanent too:
222 + char * tmp_dev = malloc(strlen(old_def_rt.rt_dev)+1);
223 + strcpy(tmp_dev, old_def_rt.rt_dev);
224 + old_def_rt.rt_dev = tmp_dev;
225 +
226 + notice("replacing old default route to %s [%I]",
227 + old_def_rt.rt_dev, old_gateway);
228 + default_rt_repl_rest = 1;
229 + del_rt = &old_def_rt;
230 + }
231 + }
232 }
233
234 memset (&rt, '\0', sizeof (rt));
235 @@ -1623,6 +1662,12 @@
236 error("default route ioctl(SIOCADDRT): %m");
237 return 0;
238 }
239 + if (default_rt_repl_rest && del_rt)
240 + if (ioctl(sock_fd, SIOCDELRT, del_rt) < 0) {
241 + if ( ! ok_error ( errno ))
242 + error("del old default route ioctl(SIOCDELRT): %m(%d)", errno);
243 + return 0;
244 + }
245
246 default_route_gateway = gateway;
247 return 1;
248 @@ -1658,6 +1703,16 @@
249 return 0;
250 }
251 }
252 + if (default_rt_repl_rest) {
253 + notice("restoring old default route to %s [%I]",
254 + old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
255 + if (ioctl(sock_fd, SIOCADDRT, &old_def_rt) < 0) {
256 + if ( ! ok_error ( errno ))
257 + error("restore default route ioctl(SIOCADDRT): %m(%d)", errno);
258 + return 0;
259 + }
260 + default_rt_repl_rest = 0;
261 + }
262
263 return 1;
264 }