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
6 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
7 &ipcp_wantoptions[0].default_route },
10 + { "replacedefaultroute", o_bool,
11 + &ipcp_wantoptions[0].replace_default_route,
12 + "Replace default route", 1
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 },
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,
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 *));
31 @@ -1659,7 +1669,12 @@
32 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
34 if (wo->default_route)
36 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
38 + if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
39 + wo->replace_default_route))
41 default_route_set[u] = 1;
43 if (sifproxyarp(u, wo->hisaddr))
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 @@
56 /* assign a default route through the interface if required */
57 if (ipcp_wantoptions[f->unit].default_route)
59 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
61 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
62 + wo->replace_default_route))
64 default_route_set[f->unit] = 1;
66 /* Make a proxy ARP entry if requested. */
67 @@ -1813,7 +1834,12 @@
69 /* assign a default route through the interface if required */
70 if (ipcp_wantoptions[f->unit].default_route)
72 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
74 + if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
75 + wo->replace_default_route))
77 default_route_set[f->unit] = 1;
79 /* Make a proxy ARP entry if requested. */
81 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
83 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
84 - ipcp_hisoptions[f->unit].hisaddr);
85 + ipcp_hisoptions[f->unit].hisaddr, 0);
88 /* Execute the ip-down script */
89 @@ -1906,16 +1932,25 @@
90 * proxy arp entries, etc.
93 -ipcp_clear_addrs(unit, ouraddr, hisaddr)
94 +ipcp_clear_addrs(unit, ouraddr, hisaddr, replacedefaultroute)
96 u_int32_t ouraddr; /* local address */
97 u_int32_t hisaddr; /* remote address */
98 + bool replacedefaultroute;
100 if (proxy_arp_set[unit]) {
101 cifproxyarp(unit, hisaddr);
102 proxy_arp_set[unit] = 0;
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.
113 + if (!replacedefaultroute && default_route_set[unit]) {
114 cifdefaultroute(unit, ouraddr, hisaddr);
115 default_route_set[unit] = 0;
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
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
134 int cif6addr __P((int, eui64_t, eui64_t));
135 /* Remove an IPv6 address from i/f */
138 int sifdefaultroute __P((int, u_int32_t, u_int32_t));
140 +int sifdefaultroute __P((int, u_int32_t, u_int32_t, bool replace_default_rt));
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
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 @@
162 + SET_SA_FAMILY (rt->rt_dst, AF_INET);
163 + SET_SA_FAMILY (rt->rt_gateway, AF_INET);
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 /********************************************************************
171 * sifdefaultroute - assign a default route through the address given.
174 -int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
178 - if (defaultroute_exists(&rt) && strcmp(rt.rt_dev, ifname) != 0) {
179 - u_int32_t old_gateway = SIN_ADDR(rt.rt_gateway);
181 - if (old_gateway != gateway)
182 - error("not replacing existing default route to %s [%I]",
183 - rt.rt_dev, old_gateway);
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.
195 +int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway, bool replace)
197 + struct rtentry rt, tmp_rt;
198 + struct rtentry *del_rt = NULL;
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 ))
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:
213 + u_int32_t old_gateway = SIN_ADDR(old_def_rt.rt_gateway);
215 + if (old_gateway != gateway) {
217 + error("not replacing default route to %s [%I]",
218 + old_def_rt.rt_dev, old_gateway);
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;
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;
234 memset (&rt, '\0', sizeof (rt));
235 @@ -1623,6 +1662,12 @@
236 error("default route ioctl(SIOCADDRT): %m");
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);
246 default_route_gateway = gateway;
248 @@ -1658,6 +1703,16 @@
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);
260 + default_rt_repl_rest = 0;