1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Copyright (c) 2021 Taehee Yoo <ap420073@gmail.com> 4 */ 5 #ifndef _NET_AMT_H_ 6 #define _NET_AMT_H_ 7 8 #include <linux/siphash.h> 9 #include <linux/jhash.h> 10 #include <linux/netdevice.h> 11 #include <net/gro_cells.h> 12 #include <net/rtnetlink.h> 13 14 enum amt_msg_type { 15 AMT_MSG_DISCOVERY = 1, 16 AMT_MSG_ADVERTISEMENT, 17 AMT_MSG_REQUEST, 18 AMT_MSG_MEMBERSHIP_QUERY, 19 AMT_MSG_MEMBERSHIP_UPDATE, 20 AMT_MSG_MULTICAST_DATA, 21 AMT_MSG_TEARDOWN, 22 __AMT_MSG_MAX, 23 }; 24 25 #define AMT_MSG_MAX (__AMT_MSG_MAX - 1) 26 27 enum amt_ops { 28 /* A*B */ 29 AMT_OPS_INT, 30 /* A+B */ 31 AMT_OPS_UNI, 32 /* A-B */ 33 AMT_OPS_SUB, 34 /* B-A */ 35 AMT_OPS_SUB_REV, 36 __AMT_OPS_MAX, 37 }; 38 39 #define AMT_OPS_MAX (__AMT_OPS_MAX - 1) 40 41 enum amt_filter { 42 AMT_FILTER_FWD, 43 AMT_FILTER_D_FWD, 44 AMT_FILTER_FWD_NEW, 45 AMT_FILTER_D_FWD_NEW, 46 AMT_FILTER_ALL, 47 AMT_FILTER_NONE_NEW, 48 AMT_FILTER_BOTH, 49 AMT_FILTER_BOTH_NEW, 50 __AMT_FILTER_MAX, 51 }; 52 53 #define AMT_FILTER_MAX (__AMT_FILTER_MAX - 1) 54 55 enum amt_act { 56 AMT_ACT_GMI, 57 AMT_ACT_GMI_ZERO, 58 AMT_ACT_GT, 59 AMT_ACT_STATUS_FWD_NEW, 60 AMT_ACT_STATUS_D_FWD_NEW, 61 AMT_ACT_STATUS_NONE_NEW, 62 __AMT_ACT_MAX, 63 }; 64 65 #define AMT_ACT_MAX (__AMT_ACT_MAX - 1) 66 67 enum amt_status { 68 AMT_STATUS_INIT, 69 AMT_STATUS_SENT_DISCOVERY, 70 AMT_STATUS_RECEIVED_DISCOVERY, 71 AMT_STATUS_SENT_ADVERTISEMENT, 72 AMT_STATUS_RECEIVED_ADVERTISEMENT, 73 AMT_STATUS_SENT_REQUEST, 74 AMT_STATUS_RECEIVED_REQUEST, 75 AMT_STATUS_SENT_QUERY, 76 AMT_STATUS_RECEIVED_QUERY, 77 AMT_STATUS_SENT_UPDATE, 78 AMT_STATUS_RECEIVED_UPDATE, 79 __AMT_STATUS_MAX, 80 }; 81 82 #define AMT_STATUS_MAX (__AMT_STATUS_MAX - 1) 83 84 /* Gateway events only */ 85 enum amt_event { 86 AMT_EVENT_NONE, 87 AMT_EVENT_RECEIVE, 88 AMT_EVENT_SEND_DISCOVERY, 89 AMT_EVENT_SEND_REQUEST, 90 __AMT_EVENT_MAX, 91 }; 92 93 struct amt_header { 94 #if defined(__LITTLE_ENDIAN_BITFIELD) 95 u8 type:4, 96 version:4; 97 #elif defined(__BIG_ENDIAN_BITFIELD) 98 u8 version:4, 99 type:4; 100 #else 101 #error "Please fix <asm/byteorder.h>" 102 #endif 103 } __packed; 104 105 struct amt_header_discovery { 106 #if defined(__LITTLE_ENDIAN_BITFIELD) 107 u32 type:4, 108 version:4, 109 reserved:24; 110 #elif defined(__BIG_ENDIAN_BITFIELD) 111 u32 version:4, 112 type:4, 113 reserved:24; 114 #else 115 #error "Please fix <asm/byteorder.h>" 116 #endif 117 __be32 nonce; 118 } __packed; 119 120 struct amt_header_advertisement { 121 #if defined(__LITTLE_ENDIAN_BITFIELD) 122 u32 type:4, 123 version:4, 124 reserved:24; 125 #elif defined(__BIG_ENDIAN_BITFIELD) 126 u32 version:4, 127 type:4, 128 reserved:24; 129 #else 130 #error "Please fix <asm/byteorder.h>" 131 #endif 132 __be32 nonce; 133 __be32 ip4; 134 } __packed; 135 136 struct amt_header_request { 137 #if defined(__LITTLE_ENDIAN_BITFIELD) 138 u32 type:4, 139 version:4, 140 reserved1:7, 141 p:1, 142 reserved2:16; 143 #elif defined(__BIG_ENDIAN_BITFIELD) 144 u32 version:4, 145 type:4, 146 p:1, 147 reserved1:7, 148 reserved2:16; 149 #else 150 #error "Please fix <asm/byteorder.h>" 151 #endif 152 __be32 nonce; 153 } __packed; 154 155 struct amt_header_membership_query { 156 #if defined(__LITTLE_ENDIAN_BITFIELD) 157 u64 type:4, 158 version:4, 159 reserved:6, 160 l:1, 161 g:1, 162 response_mac:48; 163 #elif defined(__BIG_ENDIAN_BITFIELD) 164 u64 version:4, 165 type:4, 166 g:1, 167 l:1, 168 reserved:6, 169 response_mac:48; 170 #else 171 #error "Please fix <asm/byteorder.h>" 172 #endif 173 __be32 nonce; 174 } __packed; 175 176 struct amt_header_membership_update { 177 #if defined(__LITTLE_ENDIAN_BITFIELD) 178 u64 type:4, 179 version:4, 180 reserved:8, 181 response_mac:48; 182 #elif defined(__BIG_ENDIAN_BITFIELD) 183 u64 version:4, 184 type:4, 185 reserved:8, 186 response_mac:48; 187 #else 188 #error "Please fix <asm/byteorder.h>" 189 #endif 190 __be32 nonce; 191 } __packed; 192 193 struct amt_header_mcast_data { 194 #if defined(__LITTLE_ENDIAN_BITFIELD) 195 u16 type:4, 196 version:4, 197 reserved:8; 198 #elif defined(__BIG_ENDIAN_BITFIELD) 199 u16 version:4, 200 type:4, 201 reserved:8; 202 #else 203 #error "Please fix <asm/byteorder.h>" 204 #endif 205 } __packed; 206 207 struct amt_headers { 208 union { 209 struct amt_header_discovery discovery; 210 struct amt_header_advertisement advertisement; 211 struct amt_header_request request; 212 struct amt_header_membership_query query; 213 struct amt_header_membership_update update; 214 struct amt_header_mcast_data data; 215 }; 216 } __packed; 217 218 struct amt_gw_headers { 219 union { 220 struct amt_header_discovery discovery; 221 struct amt_header_request request; 222 struct amt_header_membership_update update; 223 }; 224 } __packed; 225 226 struct amt_relay_headers { 227 union { 228 struct amt_header_advertisement advertisement; 229 struct amt_header_membership_query query; 230 struct amt_header_mcast_data data; 231 }; 232 } __packed; 233 234 struct amt_tunnel_list { 235 struct list_head list; 236 /* Protect All resources under an amt_tunne_list */ 237 spinlock_t lock; 238 struct amt_dev *amt; 239 u32 nr_groups; 240 u32 nr_sources; 241 enum amt_status status; 242 struct delayed_work gc_wq; 243 /* Last time a Membership Update refreshed this tunnel, under ->lock. 244 * gc_wq re-arms itself from this rather than being re-armed by the 245 * Update path, so the Update path cannot re-queue the work of a 246 * tunnel that amt_tunnel_expire() is freeing. 247 */ 248 unsigned long last_seen; 249 /* Set under amt->lock and tunnel->lock by whichever freer reaches the 250 * tunnel first; the other then leaves it alone, so the tunnel is freed 251 * exactly once. 252 */ 253 bool dead; 254 __be16 source_port; 255 __be32 ip4; 256 struct rcu_head rcu; 257 struct hlist_head groups[]; 258 }; 259 260 union amt_addr { 261 __be32 ip4; 262 #if IS_ENABLED(CONFIG_IPV6) 263 struct in6_addr ip6; 264 #endif 265 }; 266 267 /* RFC 3810 268 * 269 * When the router is in EXCLUDE mode, the router state is represented 270 * by the notation EXCLUDE (X,Y), where X is called the "Requested List" 271 * and Y is called the "Exclude List". All sources, except those from 272 * the Exclude List, will be forwarded by the router 273 */ 274 enum amt_source_status { 275 AMT_SOURCE_STATUS_NONE, 276 /* Node of Requested List */ 277 AMT_SOURCE_STATUS_FWD, 278 /* Node of Exclude List */ 279 AMT_SOURCE_STATUS_D_FWD, 280 }; 281 282 /* protected by gnode->lock */ 283 struct amt_source_node { 284 struct hlist_node node; 285 struct hlist_node gc_node; 286 struct amt_group_node *gnode; 287 struct delayed_work source_timer; 288 union amt_addr source_addr; 289 enum amt_source_status status; 290 #define AMT_SOURCE_OLD 0 291 #define AMT_SOURCE_NEW 1 292 u8 flags; 293 /* Protected by gnode->tunnel_list->lock. */ 294 bool dead; 295 struct rcu_head rcu; 296 }; 297 298 /* Protected by amt_tunnel_list->lock */ 299 struct amt_group_node { 300 struct amt_dev *amt; 301 union amt_addr group_addr; 302 union amt_addr host_addr; 303 bool v6; 304 u8 filter_mode; 305 u32 nr_sources; 306 struct amt_tunnel_list *tunnel_list; 307 struct hlist_node node; 308 struct hlist_node gc_node; 309 struct delayed_work group_timer; 310 /* Protected by tunnel_list->lock. */ 311 bool dead; 312 struct rcu_head rcu; 313 struct hlist_head sources[]; 314 }; 315 316 #define AMT_MAX_EVENTS 16 317 struct amt_events { 318 enum amt_event event; 319 struct sk_buff *skb; 320 }; 321 322 struct amt_dev { 323 struct net_device *dev; 324 struct net_device *stream_dev; 325 struct net *net; 326 /* Global lock for amt device */ 327 spinlock_t lock; 328 /* Used only in relay mode */ 329 struct list_head tunnel_list; 330 struct gro_cells gro_cells; 331 332 /* Protected by RTNL */ 333 struct delayed_work discovery_wq; 334 /* Protected by RTNL */ 335 struct delayed_work req_wq; 336 /* Protected by RTNL */ 337 struct delayed_work secret_wq; 338 struct work_struct event_wq; 339 /* AMT status */ 340 enum amt_status status; 341 /* Current key and three previous generations keep MACs valid for 342 * 180-240s, beyond the advertised QQIC (see amt_secret_work()). 343 */ 344 siphash_key_t key[4]; 345 /* For lockless readers of key; writers hold lock */ 346 seqcount_spinlock_t key_seq; 347 struct sock __rcu *sk; 348 u32 max_groups; 349 u32 max_sources; 350 u32 hash_buckets; 351 u32 hash_seed; 352 /* Default 128 */ 353 u32 max_tunnels; 354 /* Default 128 */ 355 u32 nr_tunnels; 356 /* Gateway or Relay mode */ 357 u32 mode; 358 /* Default 2268 */ 359 __be16 relay_port; 360 /* Default 2268 */ 361 __be16 gw_port; 362 /* Outer local ip */ 363 __be32 local_ip; 364 /* Outer remote ip */ 365 __be32 remote_ip; 366 /* Outer discovery ip */ 367 __be32 discovery_ip; 368 /* Only used in gateway mode */ 369 __be32 nonce; 370 /* Gateway sent request and received query */ 371 bool ready4; 372 bool ready6; 373 u8 req_cnt; 374 u8 qi; 375 u64 qrv; 376 u64 qri; 377 /* Used only in gateway mode */ 378 u64 mac:48, 379 reserved:16; 380 /* AMT gateway side message handler queue */ 381 struct amt_events events[AMT_MAX_EVENTS]; 382 u8 event_idx; 383 u8 nr_events; 384 }; 385 386 #define AMT_TOS 0xc0 387 #define AMT_IPHDR_OPTS 4 388 #define AMT_IP6HDR_OPTS 8 389 #define AMT_GC_INTERVAL (30 * 1000) 390 #define AMT_GC_KICK 2048 391 #define AMT_MAX_GROUP 32 392 #define AMT_MAX_SOURCE 128 393 #define AMT_HSIZE_SHIFT 8 394 #define AMT_HSIZE (1 << AMT_HSIZE_SHIFT) 395 396 #define AMT_DISCOVERY_TIMEOUT 5000 397 #define AMT_INIT_REQ_TIMEOUT 1 398 #define AMT_INIT_QUERY_INTERVAL 125 399 #define AMT_MAX_REQ_TIMEOUT 120 400 #define AMT_MAX_REQ_COUNT 3 401 #define AMT_SECRET_TIMEOUT 60000 402 #define IANA_AMT_UDP_PORT 2268 403 #define AMT_MAX_TUNNELS 128 404 /* Upper bound of amt_max_tunnels_per_src(), the per-address tunnel cap */ 405 #define AMT_MAX_TUNNELS_PER_SRC 16 406 #define AMT_MAX_REQS 128 407 #define AMT_GW_HLEN (sizeof(struct iphdr) + \ 408 sizeof(struct udphdr) + \ 409 sizeof(struct amt_gw_headers)) 410 #define AMT_RELAY_HLEN (sizeof(struct iphdr) + \ 411 sizeof(struct udphdr) + \ 412 sizeof(struct amt_relay_headers)) 413 414 static inline bool netif_is_amt(const struct net_device *dev) 415 { 416 return dev->rtnl_link_ops && !strcmp(dev->rtnl_link_ops->kind, "amt"); 417 } 418 419 static inline u64 amt_gmi(const struct amt_dev *amt) 420 { 421 return ((amt->qrv * amt->qi) + amt->qri) * 1000; 422 } 423 424 #endif /* _NET_AMT_H_ */ 425