Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * Copyright (c) 2021 Taehee Yoo <ap420073@gmail.com>
0004  */
0005 #ifndef _NET_AMT_H_
0006 #define _NET_AMT_H_
0007 
0008 #include <linux/siphash.h>
0009 #include <linux/jhash.h>
0010 #include <linux/netdevice.h>
0011 #include <net/gro_cells.h>
0012 #include <net/rtnetlink.h>
0013 
0014 enum amt_msg_type {
0015     AMT_MSG_DISCOVERY = 1,
0016     AMT_MSG_ADVERTISEMENT,
0017     AMT_MSG_REQUEST,
0018     AMT_MSG_MEMBERSHIP_QUERY,
0019     AMT_MSG_MEMBERSHIP_UPDATE,
0020     AMT_MSG_MULTICAST_DATA,
0021     AMT_MSG_TEARDOWN,
0022     __AMT_MSG_MAX,
0023 };
0024 
0025 #define AMT_MSG_MAX (__AMT_MSG_MAX - 1)
0026 
0027 enum amt_ops {
0028     /* A*B */
0029     AMT_OPS_INT,
0030     /* A+B */
0031     AMT_OPS_UNI,
0032     /* A-B */
0033     AMT_OPS_SUB,
0034     /* B-A */
0035     AMT_OPS_SUB_REV,
0036     __AMT_OPS_MAX,
0037 };
0038 
0039 #define AMT_OPS_MAX (__AMT_OPS_MAX - 1)
0040 
0041 enum amt_filter {
0042     AMT_FILTER_FWD,
0043     AMT_FILTER_D_FWD,
0044     AMT_FILTER_FWD_NEW,
0045     AMT_FILTER_D_FWD_NEW,
0046     AMT_FILTER_ALL,
0047     AMT_FILTER_NONE_NEW,
0048     AMT_FILTER_BOTH,
0049     AMT_FILTER_BOTH_NEW,
0050     __AMT_FILTER_MAX,
0051 };
0052 
0053 #define AMT_FILTER_MAX (__AMT_FILTER_MAX - 1)
0054 
0055 enum amt_act {
0056     AMT_ACT_GMI,
0057     AMT_ACT_GMI_ZERO,
0058     AMT_ACT_GT,
0059     AMT_ACT_STATUS_FWD_NEW,
0060     AMT_ACT_STATUS_D_FWD_NEW,
0061     AMT_ACT_STATUS_NONE_NEW,
0062     __AMT_ACT_MAX,
0063 };
0064 
0065 #define AMT_ACT_MAX (__AMT_ACT_MAX - 1)
0066 
0067 enum amt_status {
0068     AMT_STATUS_INIT,
0069     AMT_STATUS_SENT_DISCOVERY,
0070     AMT_STATUS_RECEIVED_DISCOVERY,
0071     AMT_STATUS_SENT_ADVERTISEMENT,
0072     AMT_STATUS_RECEIVED_ADVERTISEMENT,
0073     AMT_STATUS_SENT_REQUEST,
0074     AMT_STATUS_RECEIVED_REQUEST,
0075     AMT_STATUS_SENT_QUERY,
0076     AMT_STATUS_RECEIVED_QUERY,
0077     AMT_STATUS_SENT_UPDATE,
0078     AMT_STATUS_RECEIVED_UPDATE,
0079     __AMT_STATUS_MAX,
0080 };
0081 
0082 #define AMT_STATUS_MAX (__AMT_STATUS_MAX - 1)
0083 
0084 /* Gateway events only */
0085 enum amt_event {
0086     AMT_EVENT_NONE,
0087     AMT_EVENT_RECEIVE,
0088     AMT_EVENT_SEND_DISCOVERY,
0089     AMT_EVENT_SEND_REQUEST,
0090     __AMT_EVENT_MAX,
0091 };
0092 
0093 struct amt_header {
0094 #if defined(__LITTLE_ENDIAN_BITFIELD)
0095     u8 type:4,
0096        version:4;
0097 #elif defined(__BIG_ENDIAN_BITFIELD)
0098     u8 version:4,
0099        type:4;
0100 #else
0101 #error  "Please fix <asm/byteorder.h>"
0102 #endif
0103 } __packed;
0104 
0105 struct amt_header_discovery {
0106 #if defined(__LITTLE_ENDIAN_BITFIELD)
0107     u32 type:4,
0108         version:4,
0109         reserved:24;
0110 #elif defined(__BIG_ENDIAN_BITFIELD)
0111     u32 version:4,
0112         type:4,
0113         reserved:24;
0114 #else
0115 #error  "Please fix <asm/byteorder.h>"
0116 #endif
0117     __be32  nonce;
0118 } __packed;
0119 
0120 struct amt_header_advertisement {
0121 #if defined(__LITTLE_ENDIAN_BITFIELD)
0122     u32 type:4,
0123         version:4,
0124         reserved:24;
0125 #elif defined(__BIG_ENDIAN_BITFIELD)
0126     u32 version:4,
0127         type:4,
0128         reserved:24;
0129 #else
0130 #error  "Please fix <asm/byteorder.h>"
0131 #endif
0132     __be32  nonce;
0133     __be32  ip4;
0134 } __packed;
0135 
0136 struct amt_header_request {
0137 #if defined(__LITTLE_ENDIAN_BITFIELD)
0138     u32 type:4,
0139         version:4,
0140         reserved1:7,
0141         p:1,
0142         reserved2:16;
0143 #elif defined(__BIG_ENDIAN_BITFIELD)
0144     u32 version:4,
0145         type:4,
0146         p:1,
0147         reserved1:7,
0148         reserved2:16;
0149 #else
0150 #error  "Please fix <asm/byteorder.h>"
0151 #endif
0152     __be32  nonce;
0153 } __packed;
0154 
0155 struct amt_header_membership_query {
0156 #if defined(__LITTLE_ENDIAN_BITFIELD)
0157     u64 type:4,
0158         version:4,
0159         reserved:6,
0160         l:1,
0161         g:1,
0162         response_mac:48;
0163 #elif defined(__BIG_ENDIAN_BITFIELD)
0164     u64 version:4,
0165         type:4,
0166         g:1,
0167         l:1,
0168         reserved:6,
0169         response_mac:48;
0170 #else
0171 #error  "Please fix <asm/byteorder.h>"
0172 #endif
0173     __be32  nonce;
0174 } __packed;
0175 
0176 struct amt_header_membership_update {
0177 #if defined(__LITTLE_ENDIAN_BITFIELD)
0178     u64 type:4,
0179         version:4,
0180         reserved:8,
0181         response_mac:48;
0182 #elif defined(__BIG_ENDIAN_BITFIELD)
0183     u64 version:4,
0184         type:4,
0185         reserved:8,
0186         response_mac:48;
0187 #else
0188 #error  "Please fix <asm/byteorder.h>"
0189 #endif
0190     __be32  nonce;
0191 } __packed;
0192 
0193 struct amt_header_mcast_data {
0194 #if defined(__LITTLE_ENDIAN_BITFIELD)
0195     u16 type:4,
0196         version:4,
0197         reserved:8;
0198 #elif defined(__BIG_ENDIAN_BITFIELD)
0199     u16 version:4,
0200         type:4,
0201         reserved:8;
0202 #else
0203 #error  "Please fix <asm/byteorder.h>"
0204 #endif
0205 } __packed;
0206 
0207 struct amt_headers {
0208     union {
0209         struct amt_header_discovery discovery;
0210         struct amt_header_advertisement advertisement;
0211         struct amt_header_request request;
0212         struct amt_header_membership_query query;
0213         struct amt_header_membership_update update;
0214         struct amt_header_mcast_data data;
0215     };
0216 } __packed;
0217 
0218 struct amt_gw_headers {
0219     union {
0220         struct amt_header_discovery discovery;
0221         struct amt_header_request request;
0222         struct amt_header_membership_update update;
0223     };
0224 } __packed;
0225 
0226 struct amt_relay_headers {
0227     union {
0228         struct amt_header_advertisement advertisement;
0229         struct amt_header_membership_query query;
0230         struct amt_header_mcast_data data;
0231     };
0232 } __packed;
0233 
0234 struct amt_skb_cb {
0235     struct amt_tunnel_list *tunnel;
0236 };
0237 
0238 struct amt_tunnel_list {
0239     struct list_head    list;
0240     /* Protect All resources under an amt_tunne_list */
0241     spinlock_t      lock;
0242     struct amt_dev      *amt;
0243     u32         nr_groups;
0244     u32         nr_sources;
0245     enum amt_status     status;
0246     struct delayed_work gc_wq;
0247     __be16          source_port;
0248     __be32          ip4;
0249     __be32          nonce;
0250     siphash_key_t       key;
0251     u64         mac:48,
0252                 reserved:16;
0253     struct rcu_head     rcu;
0254     struct hlist_head   groups[];
0255 };
0256 
0257 union amt_addr {
0258     __be32          ip4;
0259 #if IS_ENABLED(CONFIG_IPV6)
0260     struct in6_addr     ip6;
0261 #endif
0262 };
0263 
0264 /* RFC 3810
0265  *
0266  * When the router is in EXCLUDE mode, the router state is represented
0267  * by the notation EXCLUDE (X,Y), where X is called the "Requested List"
0268  * and Y is called the "Exclude List".  All sources, except those from
0269  * the Exclude List, will be forwarded by the router
0270  */
0271 enum amt_source_status {
0272     AMT_SOURCE_STATUS_NONE,
0273     /* Node of Requested List */
0274     AMT_SOURCE_STATUS_FWD,
0275     /* Node of Exclude List */
0276     AMT_SOURCE_STATUS_D_FWD,
0277 };
0278 
0279 /* protected by gnode->lock */
0280 struct amt_source_node {
0281     struct hlist_node   node;
0282     struct amt_group_node   *gnode;
0283     struct delayed_work     source_timer;
0284     union amt_addr      source_addr;
0285     enum amt_source_status  status;
0286 #define AMT_SOURCE_OLD  0
0287 #define AMT_SOURCE_NEW  1
0288     u8          flags;
0289     struct rcu_head     rcu;
0290 };
0291 
0292 /* Protected by amt_tunnel_list->lock */
0293 struct amt_group_node {
0294     struct amt_dev      *amt;
0295     union amt_addr      group_addr;
0296     union amt_addr      host_addr;
0297     bool            v6;
0298     u8          filter_mode;
0299     u32         nr_sources;
0300     struct amt_tunnel_list  *tunnel_list;
0301     struct hlist_node   node;
0302     struct delayed_work     group_timer;
0303     struct rcu_head     rcu;
0304     struct hlist_head   sources[];
0305 };
0306 
0307 #define AMT_MAX_EVENTS  16
0308 struct amt_events {
0309     enum amt_event event;
0310     struct sk_buff *skb;
0311 };
0312 
0313 struct amt_dev {
0314     struct net_device       *dev;
0315     struct net_device       *stream_dev;
0316     struct net      *net;
0317     /* Global lock for amt device */
0318     spinlock_t      lock;
0319     /* Used only in relay mode */
0320     struct list_head        tunnel_list;
0321     struct gro_cells    gro_cells;
0322 
0323     /* Protected by RTNL */
0324     struct delayed_work     discovery_wq;
0325     /* Protected by RTNL */
0326     struct delayed_work     req_wq;
0327     /* Protected by RTNL */
0328     struct delayed_work     secret_wq;
0329     struct work_struct  event_wq;
0330     /* AMT status */
0331     enum amt_status     status;
0332     /* Generated key */
0333     siphash_key_t       key;
0334     struct socket     __rcu *sock;
0335     u32         max_groups;
0336     u32         max_sources;
0337     u32         hash_buckets;
0338     u32         hash_seed;
0339     /* Default 128 */
0340     u32                     max_tunnels;
0341     /* Default 128 */
0342     u32                     nr_tunnels;
0343     /* Gateway or Relay mode */
0344     u32                     mode;
0345     /* Default 2268 */
0346     __be16          relay_port;
0347     /* Default 2268 */
0348     __be16          gw_port;
0349     /* Outer local ip */
0350     __be32          local_ip;
0351     /* Outer remote ip */
0352     __be32          remote_ip;
0353     /* Outer discovery ip */
0354     __be32          discovery_ip;
0355     /* Only used in gateway mode */
0356     __be32          nonce;
0357     /* Gateway sent request and received query */
0358     bool            ready4;
0359     bool            ready6;
0360     u8          req_cnt;
0361     u8          qi;
0362     u64         qrv;
0363     u64         qri;
0364     /* Used only in gateway mode */
0365     u64         mac:48,
0366                 reserved:16;
0367     /* AMT gateway side message handler queue */
0368     struct amt_events   events[AMT_MAX_EVENTS];
0369     u8          event_idx;
0370     u8          nr_events;
0371 };
0372 
0373 #define AMT_TOS         0xc0
0374 #define AMT_IPHDR_OPTS      4
0375 #define AMT_IP6HDR_OPTS     8
0376 #define AMT_GC_INTERVAL     (30 * 1000)
0377 #define AMT_MAX_GROUP       32
0378 #define AMT_MAX_SOURCE      128
0379 #define AMT_HSIZE_SHIFT     8
0380 #define AMT_HSIZE       (1 << AMT_HSIZE_SHIFT)
0381 
0382 #define AMT_DISCOVERY_TIMEOUT   5000
0383 #define AMT_INIT_REQ_TIMEOUT    1
0384 #define AMT_INIT_QUERY_INTERVAL 125
0385 #define AMT_MAX_REQ_TIMEOUT 120
0386 #define AMT_MAX_REQ_COUNT   3
0387 #define AMT_SECRET_TIMEOUT  60000
0388 #define IANA_AMT_UDP_PORT   2268
0389 #define AMT_MAX_TUNNELS         128
0390 #define AMT_MAX_REQS        128
0391 #define AMT_GW_HLEN (sizeof(struct iphdr) + \
0392              sizeof(struct udphdr) + \
0393              sizeof(struct amt_gw_headers))
0394 #define AMT_RELAY_HLEN (sizeof(struct iphdr) + \
0395              sizeof(struct udphdr) + \
0396              sizeof(struct amt_relay_headers))
0397 
0398 static inline bool netif_is_amt(const struct net_device *dev)
0399 {
0400     return dev->rtnl_link_ops && !strcmp(dev->rtnl_link_ops->kind, "amt");
0401 }
0402 
0403 static inline u64 amt_gmi(const struct amt_dev *amt)
0404 {
0405     return ((amt->qrv * amt->qi) + amt->qri) * 1000;
0406 }
0407 
0408 #endif /* _NET_AMT_H_ */