1 /*
2  * Copyright (c) 2015 Tom Herbert <tom@herbertland.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  */
10 
11 #ifndef __ILA_H
12 #define __ILA_H
13 
14 #include <linux/errno.h>
15 #include <linux/ip.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/socket.h>
19 #include <linux/skbuff.h>
20 #include <linux/types.h>
21 #include <net/checksum.h>
22 #include <net/genetlink.h>
23 #include <net/ip.h>
24 #include <net/protocol.h>
25 #include <uapi/linux/ila.h>
26 
27 struct ila_locator {
28 	union {
29 		__u8            v8[8];
30 		__be16          v16[4];
31 		__be32          v32[2];
32 		__be64		v64;
33 	};
34 };
35 
36 struct ila_identifier {
37 	union {
38 		struct {
39 #if defined(__LITTLE_ENDIAN_BITFIELD)
40 			u8 __space:4;
41 			u8 csum_neutral:1;
42 			u8 type:3;
43 #elif defined(__BIG_ENDIAN_BITFIELD)
44 			u8 type:3;
45 			u8 csum_neutral:1;
46 			u8 __space:4;
47 #else
48 #error  "Adjust your <asm/byteorder.h> defines"
49 #endif
50 			u8 __space2[7];
51 		};
52 		__u8            v8[8];
53 		__be16          v16[4];
54 		__be32          v32[2];
55 		__be64		v64;
56 	};
57 };
58 
59 #define CSUM_NEUTRAL_FLAG	htonl(0x10000000)
60 
61 struct ila_addr {
62 	union {
63 		struct in6_addr addr;
64 		struct {
65 			struct ila_locator loc;
66 			struct ila_identifier ident;
67 		};
68 	};
69 };
70 
ila_a2i(struct in6_addr * addr)71 static inline struct ila_addr *ila_a2i(struct in6_addr *addr)
72 {
73 	return (struct ila_addr *)addr;
74 }
75 
ila_addr_is_ila(struct ila_addr * iaddr)76 static inline bool ila_addr_is_ila(struct ila_addr *iaddr)
77 {
78 	return (iaddr->ident.type != ILA_ATYPE_IID);
79 }
80 
81 struct ila_params {
82 	struct ila_locator locator;
83 	struct ila_locator locator_match;
84 	__wsum csum_diff;
85 	u8 csum_mode;
86 	u8 ident_type;
87 };
88 
compute_csum_diff8(const __be32 * from,const __be32 * to)89 static inline __wsum compute_csum_diff8(const __be32 *from, const __be32 *to)
90 {
91 	__be32 diff[] = {
92 		~from[0], ~from[1], to[0], to[1],
93 	};
94 
95 	return csum_partial(diff, sizeof(diff), 0);
96 }
97 
ila_csum_neutral_set(struct ila_identifier ident)98 static inline bool ila_csum_neutral_set(struct ila_identifier ident)
99 {
100 	return !!(ident.csum_neutral);
101 }
102 
103 void ila_update_ipv6_locator(struct sk_buff *skb, struct ila_params *p,
104 			     bool set_csum_neutral);
105 
106 void ila_init_saved_csum(struct ila_params *p);
107 
108 struct ila_net {
109 	struct {
110 		struct rhashtable rhash_table;
111 		spinlock_t *locks; /* Bucket locks for entry manipulation */
112 		unsigned int locks_mask;
113 		bool hooks_registered;
114 	} xlat;
115 };
116 
117 int ila_lwt_init(void);
118 void ila_lwt_fini(void);
119 
120 int ila_xlat_init_net(struct net *net);
121 void ila_xlat_exit_net(struct net *net);
122 
123 int ila_xlat_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info);
124 int ila_xlat_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info);
125 int ila_xlat_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info);
126 int ila_xlat_nl_cmd_flush(struct sk_buff *skb, struct genl_info *info);
127 int ila_xlat_nl_dump_start(struct netlink_callback *cb);
128 int ila_xlat_nl_dump_done(struct netlink_callback *cb);
129 int ila_xlat_nl_dump(struct sk_buff *skb, struct netlink_callback *cb);
130 
131 extern unsigned int ila_net_id;
132 
133 extern struct genl_family ila_nl_family;
134 
135 #endif /* __ILA_H */
136