b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | |
| 2 | #include "piped.h" |
| 3 | #include "piped_uci.h" |
| 4 | #include "piped_util.h" |
| 5 | #include "piped_nw.h" |
| 6 | #include "piped_dhcp.h" |
| 7 | #include "piped_dns.h" |
| 8 | |
| 9 | int pd_nw_add(struct pd_context *pdc, struct pd_iface *pdi) |
| 10 | { |
| 11 | char lan_alias[MAX_IP_STR_LEN]; |
| 12 | char *lan_ip = NULL; |
| 13 | char *network_lan_ip = NULL; |
| 14 | struct uci_context *c = pdc->c; |
| 15 | int i = 0; |
| 16 | int last_oct; |
| 17 | int third_oct; |
| 18 | |
| 19 | lan_ip = pd_uci_get_opt(c, UCI_PKG_PIPE, UCI_SEC_LAN, UCI_OPT_IPADDR); |
| 20 | if (!lan_ip) { |
| 21 | PD_ERR(pd_nw_add, "pd_uci_get_opt failed %s\n", UCI_OPT_IPADDR); |
| 22 | return 1; |
| 23 | } |
| 24 | |
| 25 | ipv4_get_alias(pdi->ipaddr, lan_alias); |
| 26 | last_oct = ipv4_get_last_oct(pdi->ipaddr); |
| 27 | third_oct = ipv4_get_third_oct(pdi->ipaddr); |
| 28 | |
| 29 | PD_ERR(pd_nw_add_m0, "pd_nw_add: lan_ip=%s, lan_alias=%s\n", lan_ip, lan_alias); |
| 30 | |
| 31 | network_lan_ip = pd_uci_get_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR); |
| 32 | PD_ERR(pd_nw_add_m27, "pd_nw_add: network_lan_ip=%s\n", network_lan_ip); |
| 33 | |
| 34 | if (pd_uci_del(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, |
| 35 | lan_ip)) { |
| 36 | PD_ERR(pd_nw_add1, "pd_uci_del failed %s\n", lan_ip); |
| 37 | // return 1; |
| 38 | } |
| 39 | |
| 40 | PD_ERR(pd_nw_add_m0, "pd_nw_add: begin del ipaddr list, i=%d", i); |
| 41 | while(!pd_uci_del(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, NULL)) { |
| 42 | PD_ERR(pd_nw_add_m35, "pd_nw_add: del ipaddr, i=%d", i); |
| 43 | i++; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | if (pd_uci_add_list(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, |
| 48 | lan_alias)) { |
| 49 | PD_ERR(pd_nw_add2, "pd_uci_add_listt failed %s\n", lan_alias); |
| 50 | return 1; |
| 51 | } |
| 52 | /* |
| 53 | if (pd_uci_add_list(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, |
| 54 | lan_ip)) { |
| 55 | PD_ERR(pd_nw_add3, "pd_uci_add_list failed %s\n", lan_ip); |
| 56 | return 1; |
| 57 | } |
| 58 | */ |
| 59 | if (last_oct != 255 && last_oct != 0) { |
| 60 | if (pd_uci_set_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_NETMASK, |
| 61 | UCI_DHCP_DFLT_SNMASK)) { |
| 62 | PD_ERR(pd_nw_add59, "pd_uci_set_opt failed %s\n", UCI_DHCP_DFLT_SNMASK); |
| 63 | } |
| 64 | } else { |
| 65 | if (third_oct != 255 && third_oct != 0) { |
| 66 | if (pd_uci_set_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_NETMASK, |
| 67 | UCI_DHCP_DFLT_SNMASK1)) { |
| 68 | PD_ERR(pd_nw_add66, "pd_uci_set_opt failed %s\n", UCI_DHCP_DFLT_SNMASK1); |
| 69 | } |
| 70 | } else { |
| 71 | if (pd_uci_set_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_NETMASK, |
| 72 | UCI_DHCP_DFLT_SNMASK2)) { |
| 73 | PD_ERR(pd_nw_add71, "pd_uci_set_opt failed %s\n", UCI_DHCP_DFLT_SNMASK2); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pd_set(&pdc->status, PDC_NETWORK_CHANGED); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int pd_nw_del(struct pd_context *pdc, struct pd_iface *pdi) |
| 84 | { |
| 85 | char lan_alias[MAX_IP_STR_LEN]; |
| 86 | struct uci_context *c = pdc->c; |
| 87 | |
| 88 | ipv4_get_alias(pdi->ipaddr, lan_alias); |
| 89 | PD_ERR(pd_nw_del_m0, "pd_uci_del_from_list %s\n", lan_alias); |
| 90 | if (pd_uci_del_from_list(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, |
| 91 | lan_alias)) { |
| 92 | PD_ERR(pd_nw_del, "pd_uci_del_from_list failed %s\n", lan_alias); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | pd_set(&pdc->status, PDC_NETWORK_CHANGED); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int pd_nw_change(struct pd_context *pdc, struct pd_iface *oldi, |
| 102 | struct pd_iface *newi) |
| 103 | { |
| 104 | int ret = 0; |
| 105 | if (pd_nw_del(pdc, oldi)) { |
| 106 | PD_ERR(pd_nw_change, "pd_nw_del failed for %s\n", oldi->sec); |
| 107 | return 1; |
| 108 | } |
| 109 | if (pd_nw_add(pdc, newi)) { |
| 110 | PD_ERR(pd_nw_change1, "pd_nw_add failed for %s\n", newi->sec); |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | #ifdef CONFIG_NETWORK_WAN_IN_PIPE |
| 115 | if (!strncasecmp(oldi->sec, "wan", 3)) { |
| 116 | ret = pd_nw_wan_config(oldi, false); |
| 117 | pd_set(&pdc->status, PDC_NETWORK_CHANGED); |
| 118 | } |
| 119 | if (!strncasecmp(newi->sec, "wan", 3)) { |
| 120 | ret |= pd_nw_wan_config(newi, true); |
| 121 | pd_set(&pdc->status, PDC_NETWORK_CHANGED); |
| 122 | } |
| 123 | #endif |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | void pd_nw_clean(struct pd_context *pdc) |
| 128 | { |
| 129 | struct uci_context *c = pdc->c; |
| 130 | |
| 131 | PD_INFO(pd_nw_clean, "cleaning network configuration\n"); |
| 132 | |
| 133 | if (pd_uci_set_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_IPADDR, |
| 134 | UCI_NW_DFLT_IP)) { |
| 135 | PD_ERR(pd_nw_clean2, "pd_uci_set_opt failed %s\n", UCI_NW_DFLT_IP); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (pd_uci_set_opt(c, UCI_PKG_NW, UCI_SEC_LAN, UCI_OPT_NETMASK, |
| 140 | UCI_DHCP_DFLT_SNMASK)) { |
| 141 | PD_ERR(pd_nw_clean133, "pd_uci_set_opt failed %s\n", UCI_DHCP_DFLT_SNMASK); |
| 142 | } |
| 143 | pd_set(&pdc->status, PDC_NETWORK_CHANGED); |
| 144 | } |
| 145 | |
| 146 | #ifdef CONFIG_NETWORK_WAN_IN_PIPE |
| 147 | int pd_nw_wan_add_del_addr(struct uci_context *c, struct pd_iface *pdi, |
| 148 | bool add) |
| 149 | { |
| 150 | int ret = 0; |
| 151 | char ip6addr[MAX_IP6_STR_LEN]; |
| 152 | |
| 153 | if (add){ |
| 154 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_PROT, UCI_STATIC); |
| 155 | ret |= pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_MTU, UCI_DFLT_MTU); |
| 156 | } |
| 157 | else{ |
| 158 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_PROT, UCI_NONE); |
| 159 | ret |= pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_MTU, UCI_DFLT_MTU); |
| 160 | } |
| 161 | |
| 162 | if(ret) |
| 163 | goto err; |
| 164 | |
| 165 | if (add && pdi->flags & PDI_AUTO) |
| 166 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_AUTO, "1"); |
| 167 | else if (pdi->flags & PDI_AUTO) |
| 168 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_AUTO, "1"); |
| 169 | |
| 170 | if (ret) |
| 171 | goto err; |
| 172 | |
| 173 | if(pdi->flags & PDI_DEDICATEAPN) { |
| 174 | if (add) |
| 175 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DEDICATE_OPT, "1"); |
| 176 | else if (pdi->flags & PDI_DEDICATEAPN) |
| 177 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DEDICATE_OPT, "1"); |
| 178 | } else { |
| 179 | if (add) |
| 180 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DEDICATE_OPT, "0"); |
| 181 | else if (pdi->flags & PDI_DEDICATEAPN) |
| 182 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DEDICATE_OPT, "0"); |
| 183 | } |
| 184 | |
| 185 | if (ret) |
| 186 | goto err; |
| 187 | |
| 188 | if (add && pdi->flags & PDI_COMBINE_IF) |
| 189 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_COMBINE_IF_OPT, pdi->combine_if); |
| 190 | else if (pdi->flags & PDI_COMBINE_IF) |
| 191 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_COMBINE_IF_OPT, pdi->combine_if); |
| 192 | |
| 193 | if (ret) |
| 194 | goto err; |
| 195 | |
| 196 | PD_INFO(pd_nw_wan_add_del_addr0, "pd_nw_wan_add_del_addr: ip=%s\n", pdi->ipaddr); |
| 197 | if (add && pdi->flags & PDI_IPADDR) |
| 198 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_IPADDR, pdi->ipaddr); |
| 199 | else if (pdi->flags & PDI_IPADDR) |
| 200 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_IPADDR, pdi->ipaddr); |
| 201 | |
| 202 | if (ret) |
| 203 | goto err; |
| 204 | |
| 205 | sprintf(ip6addr, "%s/%d", pdi->ip6addr, UCI_IPV6_DFLT_PREF); |
| 206 | PD_INFO(pd_nw_wan_add_del_addr1, "pd_nw_wan_add_del_addr: ip6=%s\n", ip6addr); |
| 207 | if (add && pdi->flags & PDI_IP6ADDR) { |
| 208 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_IP6ADDR, ip6addr); |
| 209 | } else if (pdi->flags & PDI_IP6ADDR) { |
| 210 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_IP6ADDR, ip6addr); |
| 211 | } |
| 212 | |
| 213 | if(ret) |
| 214 | goto err; |
| 215 | |
| 216 | return 0; |
| 217 | err: |
| 218 | PD_ERR(pd_nw_wan_add_del_addr, "failed to [%s] ip address for [%s]\n", add ? "add" : "del", pdi->sec); |
| 219 | return ret; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | int pd_nw_wan_add_del_dns(struct uci_context *c, struct pd_iface *pdi, |
| 224 | bool add) |
| 225 | { |
| 226 | int ret = 0; |
| 227 | uint i = 0; |
| 228 | int flags[] = {pdi->flags & PDI_DNS4P, pdi->flags & PDI_DNS4S, |
| 229 | pdi->flags & PDI_DNS6P, pdi->flags & PDI_DNS6S}; |
| 230 | char *addr[] = {pdi->dns4p, pdi->dns4s, pdi->dns6p, pdi->dns6s}; |
| 231 | |
| 232 | PD_INFO(pd_nw_wan_add_del_dns, "%s, %d, add: %d\n", __FUNCTION__, __LINE__, add); |
| 233 | |
| 234 | for (i = 0; i < sizeof(flags)/sizeof(int); i++) { |
| 235 | if (flags[i] && add) |
| 236 | ret = pd_uci_add_list(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DNS, |
| 237 | addr[i]); |
| 238 | else if (flags[i]) |
| 239 | ret = pd_uci_del_from_list(c, UCI_PKG_NW, pdi->sec, UCI_OPT_DNS, |
| 240 | addr[i]); |
| 241 | } |
| 242 | if (ret) |
| 243 | goto err; |
| 244 | |
| 245 | return 0; |
| 246 | err: |
| 247 | PD_ERR(pd_nw_wan_add_del_dns2, "failed to [%s] dns for [%s]\n", add ? "add" : "del", pdi->sec); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | int pd_nw_wan_add_del_mask(struct uci_context *c, struct pd_iface *pdi, |
| 252 | bool add) |
| 253 | { |
| 254 | int ret = 0; |
| 255 | char netmask[MAX_IP_STR_LEN]; |
| 256 | int last_oct; |
| 257 | int third_oct; |
| 258 | |
| 259 | if (pdi->flags & PDI_IP6ADDR) |
| 260 | return 0; |
| 261 | |
| 262 | last_oct = ipv4_get_last_oct(pdi->ipaddr); |
| 263 | third_oct = ipv4_get_third_oct(pdi->ipaddr); |
| 264 | |
| 265 | if (last_oct != 255 && last_oct != 0) |
| 266 | memcpy(netmask, UCI_DHCP_DFLT_SNMASK, MAX_IP_STR_LEN); |
| 267 | else { |
| 268 | if (third_oct != 255 && third_oct != 0) |
| 269 | memcpy(netmask, UCI_DHCP_DFLT_SNMASK1, MAX_IP_STR_LEN); |
| 270 | else |
| 271 | memcpy(netmask, UCI_DHCP_DFLT_SNMASK2, MAX_IP_STR_LEN); |
| 272 | } |
| 273 | |
| 274 | if (add) |
| 275 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_NETMASK, |
| 276 | netmask); |
| 277 | else |
| 278 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_NETMASK, |
| 279 | netmask); |
| 280 | |
| 281 | if (ret) |
| 282 | goto err; |
| 283 | |
| 284 | return 0; |
| 285 | err: |
| 286 | PD_ERR(pd_nw_wan_add_del_mask, "failed to [%s] netmask for [%s]\n", add ? "add" : "del", pdi->sec); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | int pd_nw_wan_add_del_gw(struct uci_context *c, struct pd_iface *pdi, |
| 291 | bool add) |
| 292 | { |
| 293 | int ret = 0; |
| 294 | |
| 295 | if (add) |
| 296 | ret = pd_uci_set_opt(c, UCI_PKG_NW, pdi->sec, UCI_OPT_GW, pdi->ipaddr); |
| 297 | else |
| 298 | ret = pd_uci_del(c, UCI_PKG_NW, pdi->sec, UCI_OPT_GW, pdi->ipaddr); |
| 299 | |
| 300 | if (ret) |
| 301 | goto err; |
| 302 | |
| 303 | return 0; |
| 304 | err: |
| 305 | PD_ERR(pd_nw_wan_add_del_gw, "failed to [%s] gw for [%s]\n", add ? "add" : "del", pdi->sec); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | int pd_nw_wan_uci_init(struct uci_context **c) |
| 310 | { |
| 311 | struct uci_package *p = NULL; |
| 312 | |
| 313 | *c = uci_alloc_context(); |
| 314 | if (!*c) { |
| 315 | PD_ERR(pd_nw_wan_uci_init, "failed to allocate context\n"); |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | uci_load(*c, UCI_PKG_NW, &p); |
| 320 | if (!p) { |
| 321 | uci_free_context(*c); |
| 322 | PD_ERR(pd_nw_wan_uci_init337, "failed to load network package\n"); |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | void pd_nw_wan_uci_done(struct uci_context *c, bool commit) |
| 330 | { |
| 331 | struct uci_package *p; |
| 332 | |
| 333 | if (!c) |
| 334 | return; |
| 335 | |
| 336 | if (commit) { |
| 337 | if((p = uci_lookup_package(c, UCI_PKG_NW))) |
| 338 | uci_commit(c, &p, false); |
| 339 | } |
| 340 | uci_free_context(c); |
| 341 | } |
| 342 | |
| 343 | int pd_nw_wan_config(struct pd_iface *pdi, bool add) |
| 344 | { |
| 345 | struct uci_context *c = NULL; |
| 346 | |
| 347 | PD_INFO(pd_nw_wan_config, "Enter pd_nw_wan_config, add: %d\n", add); |
| 348 | |
| 349 | if (pd_nw_wan_uci_init(&c)) |
| 350 | goto err; |
| 351 | |
| 352 | if (pd_nw_wan_add_del_addr(c, pdi, add)) |
| 353 | goto err; |
| 354 | |
| 355 | if (pd_nw_wan_add_del_gw(c, pdi, add)) |
| 356 | goto err; |
| 357 | |
| 358 | if (pd_nw_wan_add_del_mask(c, pdi, add)) |
| 359 | goto err; |
| 360 | |
| 361 | if (pd_nw_wan_add_del_dns(c, pdi, add)) |
| 362 | goto err; |
| 363 | |
| 364 | pd_nw_wan_uci_done(c, true); |
| 365 | |
| 366 | return 0; |
| 367 | err: |
| 368 | PD_ERR(pd_nw_wan_config414, "pd_nw_wan_config failed\n"); |
| 369 | pd_nw_wan_uci_done(c, false); |
| 370 | return -1; |
| 371 | |
| 372 | } |
| 373 | #endif |
| 374 | |