[ppp] Make patches apply agagin.
[openwrt/svn-archive/archive.git] / package / ppp / patches / 110-debian_defaultroute.patch
1 diff -Naur ppp-2.4.4.orig/pppd/ipcp.c ppp-2.4.4/pppd/ipcp.c
2 --- ppp-2.4.4.orig/pppd/ipcp.c 2009-05-09 02:55:46.000000000 -0400
3 +++ ppp-2.4.4/pppd/ipcp.c 2009-05-09 02:58:31.000000000 -0400
4 @@ -197,6 +197,14 @@
5 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
6 &ipcp_wantoptions[0].default_route },
7
8 + { "replacedefaultroute", o_bool,
9 + &ipcp_wantoptions[0].replace_default_route,
10 + "Replace default route", 1
11 + },
12 + { "noreplacedefaultroute", o_bool,
13 + &ipcp_allowoptions[0].replace_default_route,
14 + "Never replace default route", OPT_A2COPY,
15 + &ipcp_wantoptions[0].replace_default_route },
16 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
17 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
18 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
19 @@ -263,7 +271,7 @@
20 ip_active_pkt
21 };
22
23 -static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t));
24 +static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t, bool));
25 static void ipcp_script __P((char *, int)); /* Run an up/down script */
26 static void ipcp_script_done __P((void *));
27
28 @@ -1660,7 +1668,8 @@
29 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
30 return 0;
31 if (wo->default_route)
32 - if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
33 + if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
34 + wo->replace_default_route))
35 default_route_set[u] = 1;
36 if (wo->proxy_arp)
37 if (sifproxyarp(u, wo->hisaddr))
38 @@ -1742,7 +1751,8 @@
39 */
40 if (demand) {
41 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
42 - ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
43 + ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr,
44 + wo->replace_default_route);
45 if (go->ouraddr != wo->ouraddr) {
46 warn("Local IP address changed to %I", go->ouraddr);
47 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
48 @@ -1767,7 +1777,8 @@
49
50 /* assign a default route through the interface if required */
51 if (ipcp_wantoptions[f->unit].default_route)
52 - if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
53 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
54 + wo->replace_default_route))
55 default_route_set[f->unit] = 1;
56
57 /* Make a proxy ARP entry if requested. */
58 @@ -1817,7 +1828,8 @@
59
60 /* assign a default route through the interface if required */
61 if (ipcp_wantoptions[f->unit].default_route)
62 - if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
63 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
64 + wo->replace_default_route))
65 default_route_set[f->unit] = 1;
66
67 /* Make a proxy ARP entry if requested. */
68 @@ -1894,7 +1906,7 @@
69 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
70 sifdown(f->unit);
71 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
72 - ipcp_hisoptions[f->unit].hisaddr);
73 + ipcp_hisoptions[f->unit].hisaddr, 0);
74 }
75
76 /* Execute the ip-down script */
77 @@ -1910,16 +1922,25 @@
78 * proxy arp entries, etc.
79 */
80 static void
81 -ipcp_clear_addrs(unit, ouraddr, hisaddr)
82 +ipcp_clear_addrs(unit, ouraddr, hisaddr, replacedefaultroute)
83 int unit;
84 u_int32_t ouraddr; /* local address */
85 u_int32_t hisaddr; /* remote address */
86 + bool replacedefaultroute;
87 {
88 if (proxy_arp_set[unit]) {
89 cifproxyarp(unit, hisaddr);
90 proxy_arp_set[unit] = 0;
91 }
92 - if (default_route_set[unit]) {
93 + /* If replacedefaultroute, sifdefaultroute will be called soon
94 + * with replacedefaultroute set and that will overwrite the current
95 + * default route. This is the case only when doing demand, otherwise
96 + * during demand, this cifdefaultroute would restore the old default
97 + * route which is not what we want in this case. In the non-demand
98 + * case, we'll delete the default route and restore the old if there
99 + * is one saved by an sifdefaultroute with replacedefaultroute.
100 + */
101 + if (!replacedefaultroute && default_route_set[unit]) {
102 cifdefaultroute(unit, ouraddr, hisaddr);
103 default_route_set[unit] = 0;
104 }
105 diff -Naur ppp-2.4.4.orig/pppd/ipcp.h ppp-2.4.4/pppd/ipcp.h
106 --- ppp-2.4.4.orig/pppd/ipcp.h 2009-05-09 02:54:59.000000000 -0400
107 +++ ppp-2.4.4/pppd/ipcp.h 2009-05-09 02:58:31.000000000 -0400
108 @@ -70,6 +70,7 @@
109 bool old_addrs; /* Use old (IP-Addresses) option? */
110 bool req_addr; /* Ask peer to send IP address? */
111 bool default_route; /* Assign default route through interface? */
112 + bool replace_default_route; /* Replace default route through interface? */
113 bool proxy_arp; /* Make proxy ARP entry for peer? */
114 bool neg_vj; /* Van Jacobson Compression? */
115 bool old_vj; /* use old (short) form of VJ option? */
116 diff -Naur ppp-2.4.4.orig/pppd/pppd.8 ppp-2.4.4/pppd/pppd.8
117 --- ppp-2.4.4.orig/pppd/pppd.8 2009-05-09 02:54:59.000000000 -0400
118 +++ ppp-2.4.4/pppd/pppd.8 2009-05-09 02:58:31.000000000 -0400
119 @@ -121,6 +121,11 @@
120 This entry is removed when the PPP connection is broken. This option
121 is privileged if the \fInodefaultroute\fR option has been specified.
122 .TP
123 +.B replacedefaultroute
124 +This option is a flag to the defaultroute option. If defaultroute is
125 +set and this flag is also set, pppd replaces an existing default route
126 +with the new default route.
127 +.TP
128 .B disconnect \fIscript
129 Execute the command specified by \fIscript\fR, by passing it to a
130 shell, after
131 @@ -706,7 +711,12 @@
132 .TP
133 .B nodefaultroute
134 Disable the \fIdefaultroute\fR option. The system administrator who
135 -wishes to prevent users from creating default routes with pppd
136 +wishes to prevent users from adding a default route with pppd
137 +can do so by placing this option in the /etc/ppp/options file.
138 +.TP
139 +.B noreplacedefaultroute
140 +Disable the \fIreplacedefaultroute\fR option. The system administrator who
141 +wishes to prevent users from replacing a default route with pppd
142 can do so by placing this option in the /etc/ppp/options file.
143 .TP
144 .B nodeflate
145 diff -Naur ppp-2.4.4.orig/pppd/pppd.h ppp-2.4.4/pppd/pppd.h
146 --- ppp-2.4.4.orig/pppd/pppd.h 2009-05-09 02:55:46.000000000 -0400
147 +++ ppp-2.4.4/pppd/pppd.h 2009-05-09 02:58:31.000000000 -0400
148 @@ -644,7 +644,7 @@
149 int cif6addr __P((int, eui64_t, eui64_t));
150 /* Remove an IPv6 address from i/f */
151 #endif
152 -int sifdefaultroute __P((int, u_int32_t, u_int32_t));
153 +int sifdefaultroute __P((int, u_int32_t, u_int32_t, bool replace_default_rt));
154 /* Create default route through i/f */
155 int cifdefaultroute __P((int, u_int32_t, u_int32_t));
156 /* Delete default route through i/f */
157 diff -Naur ppp-2.4.4.orig/pppd/sys-linux.c ppp-2.4.4/pppd/sys-linux.c
158 --- ppp-2.4.4.orig/pppd/sys-linux.c 2009-05-09 02:55:46.000000000 -0400
159 +++ ppp-2.4.4/pppd/sys-linux.c 2009-05-09 02:58:31.000000000 -0400
160 @@ -206,6 +206,8 @@
161
162 static int if_is_up; /* Interface has been marked up */
163 static int have_default_route; /* Gateway for default route added */
164 +static struct rtentry old_def_rt; /* Old default route */
165 +static int default_rt_repl_rest; /* replace and restore old default rt */
166 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
167 static char proxy_arp_dev[16]; /* Device for proxy arp entry */
168 static u_int32_t our_old_addr; /* for detecting address changes */
169 @@ -1520,6 +1522,9 @@
170 p = NULL;
171 }
172
173 + SET_SA_FAMILY (rt->rt_dst, AF_INET);
174 + SET_SA_FAMILY (rt->rt_gateway, AF_INET);
175 +
176 SIN_ADDR(rt->rt_dst) = strtoul(cols[route_dest_col], NULL, 16);
177 SIN_ADDR(rt->rt_gateway) = strtoul(cols[route_gw_col], NULL, 16);
178 SIN_ADDR(rt->rt_genmask) = strtoul(cols[route_mask_col], NULL, 16);
179 @@ -1589,20 +1594,51 @@
180 /********************************************************************
181 *
182 * sifdefaultroute - assign a default route through the address given.
183 - */
184 -
185 -int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
186 -{
187 - struct rtentry rt;
188 -
189 - if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
190 - if (rt.rt_flags & RTF_GATEWAY)
191 - error("not replacing existing default route via %I",
192 - SIN_ADDR(rt.rt_gateway));
193 - else
194 + *
195 + * If the global default_rt_repl_rest flag is set, then this function
196 + * already replaced the original system defaultroute with some other
197 + * route and it should just replace the current defaultroute with
198 + * another one, without saving the current route. Use: demand mode,
199 + * when pppd sets first a defaultroute it it's temporary ppp0 addresses
200 + * and then changes the temporary addresses to the addresses for the real
201 + * ppp connection when it has come up.
202 + */
203 +
204 +int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
205 +{
206 + struct rtentry rt, tmp_rt;
207 + struct rtentry *del_rt = NULL;
208 +
209 + if (default_rt_repl_rest) {
210 + /* We have already reclaced the original defaultroute, if we
211 + are called again, we will delete the current default route
212 + and set the new default route in this function.
213 + - this is normally only the case the doing demand: */
214 + if (defaultroute_exists(&tmp_rt))
215 + del_rt = &tmp_rt;
216 + } else if (defaultroute_exists(&old_def_rt) &&
217 + strcmp(old_def_rt.rt_dev, ifname) != 0) {
218 + /* We did not yet replace an existing default route, let's
219 + check if we should save and replace a default route: */
220 + if (old_def_rt.rt_flags & RTF_GATEWAY) {
221 + if (!replace) {
222 + error("not replacing existing default route via %I",
223 + SIN_ADDR(old_def_rt.rt_gateway));
224 + return 0;
225 + } else {
226 + /* we need to copy rt_dev because we need it permanent too: */
227 + char *tmp_dev = malloc(strlen(old_def_rt.rt_dev) + 1);
228 + strcpy(tmp_dev, old_def_rt.rt_dev);
229 + old_def_rt.rt_dev = tmp_dev;
230 +
231 + notice("replacing old default route to %s [%I]",
232 + old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
233 + default_rt_repl_rest = 1;
234 + del_rt = &old_def_rt;
235 + }
236 + } else
237 error("not replacing existing default route through %s",
238 - rt.rt_dev);
239 - return 0;
240 + old_def_rt.rt_dev);
241 }
242
243 memset (&rt, 0, sizeof (rt));
244 @@ -1617,10 +1653,16 @@
245
246 rt.rt_flags = RTF_UP;
247 if (ioctl(sock_fd, SIOCADDRT, &rt) < 0) {
248 - if ( ! ok_error ( errno ))
249 + if (!ok_error(errno))
250 error("default route ioctl(SIOCADDRT): %m");
251 return 0;
252 }
253 + if (default_rt_repl_rest && del_rt)
254 + if (ioctl(sock_fd, SIOCDELRT, del_rt) < 0) {
255 + if (!ok_error(errno))
256 + error("del old default route ioctl(SIOCDELRT): %m");
257 + return 0;
258 + }
259
260 have_default_route = 1;
261 return 1;
262 @@ -1649,11 +1691,21 @@
263 rt.rt_flags = RTF_UP;
264 if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH) {
265 if (still_ppp()) {
266 - if ( ! ok_error ( errno ))
267 + if (!ok_error(errno))
268 error("default route ioctl(SIOCDELRT): %m");
269 return 0;
270 }
271 }
272 + if (default_rt_repl_rest) {
273 + notice("restoring old default route to %s [%I]",
274 + old_def_rt.rt_dev, SIN_ADDR(old_def_rt.rt_gateway));
275 + if (ioctl(sock_fd, SIOCADDRT, &old_def_rt) < 0) {
276 + if (!ok_error(errno))
277 + error("restore default route ioctl(SIOCADDRT): %m");
278 + return 0;
279 + }
280 + default_rt_repl_rest = 0;
281 + }
282
283 return 1;
284 }
285 diff -Naur ppp-2.4.4.orig/pppd/sys-solaris.c ppp-2.4.4/pppd/sys-solaris.c
286 --- ppp-2.4.4.orig/pppd/sys-solaris.c 2009-05-09 02:54:59.000000000 -0400
287 +++ ppp-2.4.4/pppd/sys-solaris.c 2009-05-09 02:58:31.000000000 -0400
288 @@ -2036,12 +2036,18 @@
289 * sifdefaultroute - assign a default route through the address given.
290 */
291 int
292 -sifdefaultroute(u, l, g)
293 +sifdefaultroute(u, l, g, replace)
294 int u;
295 u_int32_t l, g;
296 + bool replace;
297 {
298 struct rtentry rt;
299
300 + if (replace) {
301 + error("replacedefaultroute not supported on this platform");
302 + return 0;
303 + }
304 +
305 #if defined(__USLC__)
306 g = l; /* use the local address as gateway */
307 #endif