amt.h 8.3 KB

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