110-debian_defaultroute.patch 11 KB

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