xref: /linux/include/net/ndisc.h (revision 72503791edffe516848d0f01d377fa9cd0711970)
1 #ifndef _NDISC_H
2 #define _NDISC_H
3 
4 /*
5  *	ICMP codes for neighbour discovery messages
6  */
7 
8 #define NDISC_ROUTER_SOLICITATION	133
9 #define NDISC_ROUTER_ADVERTISEMENT	134
10 #define NDISC_NEIGHBOUR_SOLICITATION	135
11 #define NDISC_NEIGHBOUR_ADVERTISEMENT	136
12 #define NDISC_REDIRECT			137
13 
14 /*
15  * Router type: cross-layer information from link-layer to
16  * IPv6 layer reported by certain link types (e.g., RFC4214).
17  */
18 #define NDISC_NODETYPE_UNSPEC		0	/* unspecified (default) */
19 #define NDISC_NODETYPE_HOST		1	/* host or unauthorized router */
20 #define NDISC_NODETYPE_NODEFAULT	2	/* non-default router */
21 #define NDISC_NODETYPE_DEFAULT		3	/* default router */
22 
23 /*
24  *	ndisc options
25  */
26 
27 enum {
28 	__ND_OPT_PREFIX_INFO_END = 0,
29 	ND_OPT_SOURCE_LL_ADDR = 1,	/* RFC2461 */
30 	ND_OPT_TARGET_LL_ADDR = 2,	/* RFC2461 */
31 	ND_OPT_PREFIX_INFO = 3,		/* RFC2461 */
32 	ND_OPT_REDIRECT_HDR = 4,	/* RFC2461 */
33 	ND_OPT_MTU = 5,			/* RFC2461 */
34 	__ND_OPT_ARRAY_MAX,
35 	ND_OPT_ROUTE_INFO = 24,		/* RFC4191 */
36 	ND_OPT_RDNSS = 25,		/* RFC5006 */
37 	ND_OPT_DNSSL = 31,		/* RFC6106 */
38 	__ND_OPT_MAX
39 };
40 
41 #define MAX_RTR_SOLICITATION_DELAY	HZ
42 
43 #define ND_REACHABLE_TIME		(30*HZ)
44 #define ND_RETRANS_TIMER		HZ
45 
46 #include <linux/compiler.h>
47 #include <linux/icmpv6.h>
48 #include <linux/in6.h>
49 #include <linux/types.h>
50 #include <linux/if_arp.h>
51 #include <linux/netdevice.h>
52 #include <linux/hash.h>
53 
54 #include <net/neighbour.h>
55 
56 struct ctl_table;
57 struct inet6_dev;
58 struct net_device;
59 struct net_proto_family;
60 struct sk_buff;
61 
62 extern struct neigh_table nd_tbl;
63 
64 struct nd_msg {
65         struct icmp6hdr	icmph;
66         struct in6_addr	target;
67 	__u8		opt[0];
68 };
69 
70 struct rs_msg {
71 	struct icmp6hdr	icmph;
72 	__u8		opt[0];
73 };
74 
75 struct ra_msg {
76         struct icmp6hdr		icmph;
77 	__be32			reachable_time;
78 	__be32			retrans_timer;
79 };
80 
81 struct nd_opt_hdr {
82 	__u8		nd_opt_type;
83 	__u8		nd_opt_len;
84 } __packed;
85 
86 /* ND options */
87 struct ndisc_options {
88 	struct nd_opt_hdr *nd_opt_array[__ND_OPT_ARRAY_MAX];
89 #ifdef CONFIG_IPV6_ROUTE_INFO
90 	struct nd_opt_hdr *nd_opts_ri;
91 	struct nd_opt_hdr *nd_opts_ri_end;
92 #endif
93 	struct nd_opt_hdr *nd_useropts;
94 	struct nd_opt_hdr *nd_useropts_end;
95 };
96 
97 #define nd_opts_src_lladdr	nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
98 #define nd_opts_tgt_lladdr	nd_opt_array[ND_OPT_TARGET_LL_ADDR]
99 #define nd_opts_pi		nd_opt_array[ND_OPT_PREFIX_INFO]
100 #define nd_opts_pi_end		nd_opt_array[__ND_OPT_PREFIX_INFO_END]
101 #define nd_opts_rh		nd_opt_array[ND_OPT_REDIRECT_HDR]
102 #define nd_opts_mtu		nd_opt_array[ND_OPT_MTU]
103 
104 #define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
105 
106 extern struct ndisc_options *ndisc_parse_options(u8 *opt, int opt_len,
107 						 struct ndisc_options *ndopts);
108 
109 /*
110  * Return the padding between the option length and the start of the
111  * link addr.  Currently only IP-over-InfiniBand needs this, although
112  * if RFC 3831 IPv6-over-Fibre Channel is ever implemented it may
113  * also need a pad of 2.
114  */
115 static int ndisc_addr_option_pad(unsigned short type)
116 {
117 	switch (type) {
118 	case ARPHRD_INFINIBAND: return 2;
119 	default:                return 0;
120 	}
121 }
122 
123 static inline u8 *ndisc_opt_addr_data(struct nd_opt_hdr *p,
124 				      struct net_device *dev)
125 {
126 	u8 *lladdr = (u8 *)(p + 1);
127 	int lladdrlen = p->nd_opt_len << 3;
128 	int prepad = ndisc_addr_option_pad(dev->type);
129 	if (lladdrlen != NDISC_OPT_SPACE(dev->addr_len + prepad))
130 		return NULL;
131 	return lladdr + prepad;
132 }
133 
134 static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
135 {
136 	const u32 *p32 = pkey;
137 
138 	return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
139 		(p32[1] * hash_rnd[1]) +
140 		(p32[2] * hash_rnd[2]) +
141 		(p32[3] * hash_rnd[3]));
142 }
143 
144 static inline struct neighbour *__ipv6_neigh_lookup(struct neigh_table *tbl, struct net_device *dev, const void *pkey)
145 {
146 	struct neigh_hash_table *nht;
147 	const u32 *p32 = pkey;
148 	struct neighbour *n;
149 	u32 hash_val;
150 
151 	rcu_read_lock_bh();
152 	nht = rcu_dereference_bh(tbl->nht);
153 	hash_val = ndisc_hashfn(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
154 	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
155 	     n != NULL;
156 	     n = rcu_dereference_bh(n->next)) {
157 		u32 *n32 = (u32 *) n->primary_key;
158 		if (n->dev == dev &&
159 		    ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
160 		     (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0) {
161 			if (!atomic_inc_not_zero(&n->refcnt))
162 				n = NULL;
163 			break;
164 		}
165 	}
166 	rcu_read_unlock_bh();
167 
168 	return n;
169 }
170 
171 extern int			ndisc_init(void);
172 
173 extern void			ndisc_cleanup(void);
174 
175 extern int			ndisc_rcv(struct sk_buff *skb);
176 
177 extern void			ndisc_send_ns(struct net_device *dev,
178 					      struct neighbour *neigh,
179 					      const struct in6_addr *solicit,
180 					      const struct in6_addr *daddr,
181 					      const struct in6_addr *saddr);
182 
183 extern void			ndisc_send_rs(struct net_device *dev,
184 					      const struct in6_addr *saddr,
185 					      const struct in6_addr *daddr);
186 
187 extern void			ndisc_send_redirect(struct sk_buff *skb,
188 						    const struct in6_addr *target);
189 
190 extern int			ndisc_mc_map(const struct in6_addr *addr, char *buf,
191 					     struct net_device *dev, int dir);
192 
193 extern struct sk_buff		*ndisc_build_skb(struct net_device *dev,
194 						 const struct in6_addr *daddr,
195 						 const struct in6_addr *saddr,
196 						 struct icmp6hdr *icmp6h,
197 						 const struct in6_addr *target,
198 						 int llinfo);
199 
200 extern void			ndisc_send_skb(struct sk_buff *skb,
201 					       struct net_device *dev,
202 					       struct neighbour *neigh,
203 					       const struct in6_addr *daddr,
204 					       const struct in6_addr *saddr,
205 					       struct icmp6hdr *icmp6h);
206 
207 
208 
209 /*
210  *	IGMP
211  */
212 extern int			igmp6_init(void);
213 
214 extern void			igmp6_cleanup(void);
215 
216 extern int			igmp6_event_query(struct sk_buff *skb);
217 
218 extern int			igmp6_event_report(struct sk_buff *skb);
219 
220 
221 #ifdef CONFIG_SYSCTL
222 extern int 			ndisc_ifinfo_sysctl_change(struct ctl_table *ctl,
223 							   int write,
224 							   void __user *buffer,
225 							   size_t *lenp,
226 							   loff_t *ppos);
227 int ndisc_ifinfo_sysctl_strategy(ctl_table *ctl,
228 				 void __user *oldval, size_t __user *oldlenp,
229 				 void __user *newval, size_t newlen);
230 #endif
231 
232 extern void 			inet6_ifinfo_notify(int event,
233 						    struct inet6_dev *idev);
234 
235 #endif
236