b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | |
| 2 | #ifndef __PIPED__ |
| 3 | #define __PIPED__ |
| 4 | |
| 5 | #include <uci.h> |
| 6 | #include <libubus.h> |
| 7 | #include <stdbool.h> |
| 8 | |
| 9 | |
| 10 | #define MAX_DHCP_OPT_STR_LEN 256 |
| 11 | #define MAX_FNAME 256 |
| 12 | #define MAX_IFNAME 16 |
| 13 | #define MAX_SEC_NAME 16 |
| 14 | #define MAX_IP_STR_LEN 16 |
| 15 | #define MAX_IP6_STR_LEN 46 |
| 16 | #define MAX_PREFIX_STR_LEN 4 |
| 17 | |
| 18 | /* pd_iface flags definition */ |
| 19 | #define PDI_LAN 0x00000001 |
| 20 | #define PDI_UP 0x00000002 |
| 21 | #define PDI_UP6 0x00000004 |
| 22 | #define PDI_AUTO 0x00000008 |
| 23 | #define PDI_IPADDR 0x00000010 |
| 24 | #define PDI_DNS4P 0x00000020 |
| 25 | #define PDI_DNS4S 0x00000040 |
| 26 | #define PDI_IP6ADDR 0x00000080 |
| 27 | #define PDI_DNS6P 0x00000100 |
| 28 | #define PDI_DNS6S 0x00000200 |
| 29 | #define PDI_DEDICATEAPN 0x00000400 |
| 30 | #define PDI_COMBINE_IF 0x00000800 |
| 31 | #define PDI_ZVLAN_ID 0x00001000 |
| 32 | |
| 33 | /* pd_context flags definition */ |
| 34 | #define PDC_NETWORK_CHANGED 0x00000001 |
| 35 | #define PDC_ODHCPD_CHANGED 0x00000002 |
| 36 | #define PDC_DNSMASQ_CHANGED 0x00000004 |
| 37 | |
| 38 | #define MPIPE_PATH "/sys/kernel/mpipe/devices/" |
| 39 | #define USB_EN_PATH "/sys/class/android_usb/android0/enable" |
| 40 | |
| 41 | #define MPIPE_FNAME_UP "up" |
| 42 | #define MPIPE_FNAME_UP6 "up6" |
| 43 | #define MPIPE_FNAME_IP "ipaddr" |
| 44 | #define MPIPE_FNAME_GW "gateway" |
| 45 | #define MPIPE_FNAME_IP6 "ll6addr" |
| 46 | #define MPIPE_FNAME_DNS6P "dns6p" |
| 47 | #define MPIPE_FNAME_DNS6S "dns6s" |
| 48 | #define MPIPE_FNAME_COMBINEIF "combineif" |
| 49 | #define MPIPE_FNAME_GB6ADDR "gb6addr" |
| 50 | |
| 51 | |
| 52 | |
| 53 | #define UCI_PKG_PIPE "pipe" |
| 54 | #define UCI_PKG_NW "network" |
| 55 | #define UCI_PKG_DHCP "dhcp" |
| 56 | |
| 57 | #define UCI_SEC_LAN "lan" |
| 58 | #define UCI_SEC_WAN "wan" |
| 59 | #define UCI_SEC_INTERFACE "interface" |
| 60 | |
| 61 | #define UCI_OPT_DEVICE "device" |
| 62 | #define UCI_OPT_IPADDR "ipaddr" |
| 63 | #define UCI_OPT_IP6ADDR "ip6addr" |
| 64 | #define UCI_OPT_AUTO "auto" |
| 65 | #define UCI_OPT_DNS "dns" |
| 66 | #define UCI_OPT_START "start" |
| 67 | #define UCI_OPT_LIMIT "limit" |
| 68 | #define UCI_OPT_DHCP_OPT "dhcp_option" |
| 69 | #define UCI_OPT_DEDICATE_OPT "dedicate" |
| 70 | #define UCI_OPT_NETMASK "netmask" |
| 71 | #define UCI_OPT_GW "gateway" |
| 72 | #define UCI_OPT_COMBINE_IF_OPT "combineif" |
| 73 | #define UCI_OPT_VID_OPT "vid" |
| 74 | #define UCI_OPT_PROT "proto" |
| 75 | #define UCI_OPT_MTU "mtu" |
| 76 | #define UCI_DFLT_MTU "1500" |
| 77 | |
| 78 | #define UCI_STATIC "static" |
| 79 | #define UCI_NONE "none" |
| 80 | |
| 81 | #define UCI_DHCP_OPTNUM_SNMASK "1" |
| 82 | #define UCI_DHCP_OPTNUM_GW "3" |
| 83 | #define UCI_DHCP_OPTNUM_DNS "6" |
| 84 | |
| 85 | #define UCI_DHCP_DFLT_START 100 |
| 86 | #define UCI_DHCP_DFLT_LIMIT 150 |
| 87 | #define UCI_DHCP_DFLT_SNMASK "255.255.255.0" |
| 88 | #define UCI_DHCP_DFLT_SNMASK1 "255.255.0.0" |
| 89 | #define UCI_DHCP_DFLT_SNMASK2 "255.0.0.0" |
| 90 | |
| 91 | |
| 92 | #define UCI_NW_DFLT_IP "192.168.1.1" |
| 93 | |
| 94 | #define EMPTY_IPV4 "0.0.0.0" |
| 95 | #define EMPTY_IPV6 "::" |
| 96 | #define IPV4_FMT "%d.%d.%d.%d" |
| 97 | |
| 98 | #define UCI_IPV6_DFLT_PREF 64 |
| 99 | |
| 100 | struct pd_path { |
| 101 | char buf[MAX_FNAME]; |
| 102 | char *iptr; |
| 103 | char *fptr; |
| 104 | }; |
| 105 | |
| 106 | struct pd_context { |
| 107 | struct uci_context *c; |
| 108 | struct pd_path path; |
| 109 | unsigned int status; |
| 110 | }; |
| 111 | |
| 112 | struct pdi_status { |
| 113 | unsigned int added; |
| 114 | unsigned int changed; |
| 115 | unsigned int removed; |
| 116 | unsigned int same; |
| 117 | }; |
| 118 | |
| 119 | struct pd_iface { |
| 120 | int idx; |
| 121 | char ifname[MAX_IFNAME]; |
| 122 | char sec[MAX_SEC_NAME]; |
| 123 | |
| 124 | char ipaddr[MAX_IP_STR_LEN]; |
| 125 | char dns4p[MAX_IP_STR_LEN]; |
| 126 | char dns4s[MAX_IP_STR_LEN]; |
| 127 | |
| 128 | char ip6addr[MAX_IP6_STR_LEN]; |
| 129 | char dns6p[MAX_IP6_STR_LEN]; |
| 130 | char dns6s[MAX_IP6_STR_LEN]; |
| 131 | char combine_if[MAX_IFNAME]; |
| 132 | |
| 133 | |
| 134 | struct list_head list; |
| 135 | struct list_head uplist; |
| 136 | struct list_head up6list; |
| 137 | |
| 138 | unsigned int flags; |
| 139 | }; |
| 140 | |
| 141 | struct piped { |
| 142 | struct pd_iface *lan; |
| 143 | struct list_head plist; |
| 144 | struct list_head uplist; |
| 145 | struct list_head up6list; |
| 146 | struct pd_context pdc; |
| 147 | struct ubus_context *u_ctx; |
| 148 | bool rand_gw; |
| 149 | }; |
| 150 | |
| 151 | static inline bool pd_test(unsigned int *flags, unsigned int mask) |
| 152 | { |
| 153 | return *flags & mask; |
| 154 | } |
| 155 | |
| 156 | static inline void pd_set(unsigned int *flags, unsigned int mask) |
| 157 | { |
| 158 | *flags |= mask; |
| 159 | } |
| 160 | |
| 161 | static inline void pd_clear(unsigned int *flags, unsigned int mask) |
| 162 | { |
| 163 | *flags &= ~mask; |
| 164 | } |
| 165 | |
| 166 | static inline bool pdi_test(struct pd_iface *pdi, unsigned int mask) |
| 167 | { |
| 168 | return pd_test(&pdi->flags, mask); |
| 169 | } |
| 170 | |
| 171 | static inline void pdi_set(struct pd_iface *pdi, unsigned int mask) |
| 172 | { |
| 173 | pd_set(&pdi->flags, mask); |
| 174 | } |
| 175 | |
| 176 | static inline void pdi_clear(struct pd_iface *pdi, unsigned int mask) |
| 177 | { |
| 178 | pd_clear(&pdi->flags, mask); |
| 179 | } |
| 180 | |
| 181 | void pd_uevent_cb(char *ifname, bool added); |
| 182 | |
| 183 | int pd_system(const char *cmd); |
| 184 | |
| 185 | #endif |