0004-smarter-killaddr.patch 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. From 83c2de652b2b53a23b9e87d6eb3cb4085fe797c2 Mon Sep 17 00:00:00 2001
  2. From: Mike Frysinger <vapier@gentoo.org>
  3. Date: Fri, 3 Jan 2020 12:56:45 +0100
  4. Subject: [PATCH 04/14] smarter killaddr
  5. ---
  6. pppd/options.c | 8 ++++++++
  7. pppd/pppd.h | 3 +++
  8. pppd/sys-linux.c | 42 +++++++++++++++++++++++++++++++++++++++---
  9. 3 files changed, 50 insertions(+), 3 deletions(-)
  10. diff --git a/pppd/options.c b/pppd/options.c
  11. index f8d6c00..12d12fd 100644
  12. --- a/pppd/options.c
  13. +++ b/pppd/options.c
  14. @@ -102,6 +102,9 @@ int maxconnect = 0; /* Maximum connect time */
  15. char user[MAXNAMELEN]; /* Username for PAP */
  16. char passwd[MAXSECRETLEN]; /* Password for PAP */
  17. bool persist = 0; /* Reopen link after it goes down */
  18. +bool killoldaddr = 0; /* If our IP is reassigned on
  19. + reconnect, kill active TCP
  20. + connections using the old IP. */
  21. char our_name[MAXNAMELEN]; /* Our name for authentication purposes */
  22. bool demand = 0; /* do dial-on-demand */
  23. char *ipparam = NULL; /* Extra parameter for ip up/down scripts */
  24. @@ -252,6 +255,11 @@ option_t general_options[] = {
  25. { "demand", o_bool, &demand,
  26. "Dial on demand", OPT_INITONLY | 1, &persist },
  27. + { "killoldaddr", o_bool, &killoldaddr,
  28. + "Kill connections from an old source address", 1},
  29. + { "nokilloldaddr", o_bool,&killoldaddr,
  30. + "Don't kill connections from an old source address" },
  31. +
  32. { "--version", o_special_noarg, (void *)showversion,
  33. "Show version number" },
  34. { "--help", o_special_noarg, (void *)showhelp,
  35. diff --git a/pppd/pppd.h b/pppd/pppd.h
  36. index 612902f..bdb8778 100644
  37. --- a/pppd/pppd.h
  38. +++ b/pppd/pppd.h
  39. @@ -310,6 +310,9 @@ extern char our_name[MAXNAMELEN];/* Our name for authentication purposes */
  40. extern char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
  41. extern bool explicit_remote;/* remote_name specified with remotename opt */
  42. extern bool demand; /* Do dial-on-demand */
  43. +extern bool killoldaddr; /* If our IP is reassigned on
  44. + reconnect, kill active TCP
  45. + connections using the old IP. */
  46. extern char *ipparam; /* Extra parameter for ip up/down scripts */
  47. extern bool cryptpap; /* Others' PAP passwords are encrypted */
  48. extern int idle_time_limit;/* Shut down link if idle for this long */
  49. diff --git a/pppd/sys-linux.c b/pppd/sys-linux.c
  50. index 85b1512..95f5a72 100644
  51. --- a/pppd/sys-linux.c
  52. +++ b/pppd/sys-linux.c
  53. @@ -172,6 +172,10 @@ struct in6_ifreq {
  54. static const eui64_t nulleui64;
  55. #endif /* INET6 */
  56. +#ifndef SIOCKILLADDR
  57. +#define SIOCKILLADDR 0x8939
  58. +#endif
  59. +
  60. /* We can get an EIO error on an ioctl if the modem has hung up */
  61. #define ok_error(num) ((num)==EIO)
  62. @@ -220,6 +224,7 @@ static int default_rt_repl_rest; /* replace and restore old default rt */
  63. static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
  64. static char proxy_arp_dev[16]; /* Device for proxy arp entry */
  65. static u_int32_t our_old_addr; /* for detecting address changes */
  66. +static u_int32_t our_current_addr;
  67. static int dynaddr_set; /* 1 if ip_dynaddr set */
  68. static int looped; /* 1 if using loop */
  69. static int link_mtu; /* mtu for the link (not bundle) */
  70. @@ -566,6 +571,27 @@ int generic_establish_ppp (int fd)
  71. return -1;
  72. }
  73. +static void do_killaddr(u_int32_t oldaddr)
  74. +{
  75. + struct ifreq ifr;
  76. +
  77. + memset(&ifr,0,sizeof ifr);
  78. +
  79. + SET_SA_FAMILY (ifr.ifr_addr, AF_INET);
  80. + SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET);
  81. + SET_SA_FAMILY (ifr.ifr_netmask, AF_INET);
  82. +
  83. + SIN_ADDR(ifr.ifr_addr) = oldaddr;
  84. +
  85. + strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  86. +
  87. + if(ioctl(sock_fd,SIOCKILLADDR,&ifr) < 0) {
  88. + if (!ok_error (errno))
  89. + error("ioctl(SIOCKILLADDR): %m(%d)", errno);
  90. + return;
  91. + }
  92. +}
  93. +
  94. /********************************************************************
  95. *
  96. * tty_disestablish_ppp - Restore the serial port to normal operation.
  97. @@ -2747,9 +2773,14 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
  98. }
  99. }
  100. - /* set ip_dynaddr in demand mode if address changes */
  101. - if (demand && tune_kernel && !dynaddr_set
  102. - && our_old_addr && our_old_addr != our_adr) {
  103. + if(persist && our_old_addr && our_old_addr != our_adr) {
  104. +
  105. + if(killoldaddr)
  106. + do_killaddr(our_old_addr);
  107. +
  108. +
  109. + /* set ip_dynaddr in demand mode if address changes */
  110. + if (tune_kernel && !dynaddr_set) {
  111. /* set ip_dynaddr if possible */
  112. char *path;
  113. int fd;
  114. @@ -2761,7 +2792,10 @@ int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
  115. close(fd);
  116. }
  117. dynaddr_set = 1; /* only 1 attempt */
  118. + }
  119. }
  120. +
  121. + our_current_addr = our_adr;
  122. our_old_addr = 0;
  123. return 1;
  124. @@ -2818,6 +2852,8 @@ int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
  125. our_old_addr = our_adr;
  126. + our_current_addr = 0;
  127. +
  128. return 1;
  129. }
  130. --
  131. 2.30.0