108-debian_defaultroute.patch 9.7 KB

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