]>
Commit | Line | Data |
---|---|---|
60c778b2 | 1 | /* SCTP kernel implementation |
1da177e4 LT |
2 | * (C) Copyright IBM Corp. 2002, 2004 |
3 | * Copyright (c) 2001 Nokia, Inc. | |
4 | * Copyright (c) 2001 La Monte H.P. Yarroll | |
5 | * Copyright (c) 2002-2003 Intel Corp. | |
6 | * | |
60c778b2 | 7 | * This file is part of the SCTP kernel implementation |
1da177e4 LT |
8 | * |
9 | * SCTP over IPv6. | |
10 | * | |
60c778b2 | 11 | * This SCTP implementation is free software; |
1da177e4 LT |
12 | * you can redistribute it and/or modify it under the terms of |
13 | * the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2, or (at your option) | |
15 | * any later version. | |
16 | * | |
60c778b2 | 17 | * This SCTP implementation is distributed in the hope that it |
1da177e4 LT |
18 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
19 | * ************************ | |
20 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
21 | * See the GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
4b2f13a2 JK |
24 | * along with GNU CC; see the file COPYING. If not, see |
25 | * <http://www.gnu.org/licenses/>. | |
1da177e4 LT |
26 | * |
27 | * Please send any bug reports or fixes you make to the | |
28 | * email address(es): | |
91705c61 | 29 | * lksctp developers <linux-sctp@vger.kernel.org> |
1da177e4 | 30 | * |
1da177e4 LT |
31 | * Written or modified by: |
32 | * Le Yanqun <yanqun.le@nokia.com> | |
33 | * Hui Huang <hui.huang@nokia.com> | |
34 | * La Monte H.P. Yarroll <piggy@acm.org> | |
35 | * Sridhar Samudrala <sri@us.ibm.com> | |
36 | * Jon Grimm <jgrimm@us.ibm.com> | |
37 | * Ardelle Fan <ardelle.fan@intel.com> | |
38 | * | |
39 | * Based on: | |
40 | * linux/net/ipv6/tcp_ipv6.c | |
1da177e4 LT |
41 | */ |
42 | ||
145ce502 JP |
43 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
44 | ||
1da177e4 LT |
45 | #include <linux/module.h> |
46 | #include <linux/errno.h> | |
47 | #include <linux/types.h> | |
48 | #include <linux/socket.h> | |
49 | #include <linux/sockios.h> | |
50 | #include <linux/net.h> | |
1da177e4 LT |
51 | #include <linux/in.h> |
52 | #include <linux/in6.h> | |
53 | #include <linux/netdevice.h> | |
54 | #include <linux/init.h> | |
55 | #include <linux/ipsec.h> | |
5a0e3ad6 | 56 | #include <linux/slab.h> |
1da177e4 LT |
57 | |
58 | #include <linux/ipv6.h> | |
59 | #include <linux/icmpv6.h> | |
60 | #include <linux/random.h> | |
61 | #include <linux/seq_file.h> | |
62 | ||
63 | #include <net/protocol.h> | |
1da177e4 | 64 | #include <net/ndisc.h> |
c752f073 | 65 | #include <net/ip.h> |
1da177e4 LT |
66 | #include <net/ipv6.h> |
67 | #include <net/transp_v6.h> | |
68 | #include <net/addrconf.h> | |
69 | #include <net/ip6_route.h> | |
70 | #include <net/inet_common.h> | |
71 | #include <net/inet_ecn.h> | |
72 | #include <net/sctp/sctp.h> | |
73 | ||
7c0f6ba6 | 74 | #include <linux/uaccess.h> |
1da177e4 | 75 | |
62503411 WS |
76 | static inline int sctp_v6_addr_match_len(union sctp_addr *s1, |
77 | union sctp_addr *s2); | |
9914ae3c VY |
78 | static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr, |
79 | __be16 port); | |
80 | static int sctp_v6_cmp_addr(const union sctp_addr *addr1, | |
81 | const union sctp_addr *addr2); | |
62503411 | 82 | |
29303547 VY |
83 | /* Event handler for inet6 address addition/deletion events. |
84 | * The sctp_local_addr_list needs to be protocted by a spin lock since | |
85 | * multiple notifiers (say IPv4 and IPv6) may be running at the same | |
86 | * time and thus corrupt the list. | |
87 | * The reader side is protected with RCU. | |
88 | */ | |
24123186 AB |
89 | static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev, |
90 | void *ptr) | |
29c7cf96 SS |
91 | { |
92 | struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; | |
29303547 VY |
93 | struct sctp_sockaddr_entry *addr = NULL; |
94 | struct sctp_sockaddr_entry *temp; | |
4db67e80 | 95 | struct net *net = dev_net(ifa->idev->dev); |
22626216 | 96 | int found = 0; |
29c7cf96 SS |
97 | |
98 | switch (ev) { | |
99 | case NETDEV_UP: | |
400b8b9a | 100 | addr = kzalloc(sizeof(*addr), GFP_ATOMIC); |
29c7cf96 SS |
101 | if (addr) { |
102 | addr->a.v6.sin6_family = AF_INET6; | |
4e3fd7a0 | 103 | addr->a.v6.sin6_addr = ifa->addr; |
29c7cf96 | 104 | addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex; |
29303547 | 105 | addr->valid = 1; |
4db67e80 EB |
106 | spin_lock_bh(&net->sctp.local_addr_lock); |
107 | list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list); | |
108 | sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW); | |
109 | spin_unlock_bh(&net->sctp.local_addr_lock); | |
29c7cf96 SS |
110 | } |
111 | break; | |
112 | case NETDEV_DOWN: | |
4db67e80 | 113 | spin_lock_bh(&net->sctp.local_addr_lock); |
29303547 | 114 | list_for_each_entry_safe(addr, temp, |
4db67e80 | 115 | &net->sctp.local_addr_list, list) { |
a40a7d15 PE |
116 | if (addr->a.sa.sa_family == AF_INET6 && |
117 | ipv6_addr_equal(&addr->a.v6.sin6_addr, | |
118 | &ifa->addr)) { | |
4db67e80 | 119 | sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL); |
22626216 | 120 | found = 1; |
29303547 VY |
121 | addr->valid = 0; |
122 | list_del_rcu(&addr->list); | |
29c7cf96 SS |
123 | break; |
124 | } | |
125 | } | |
4db67e80 | 126 | spin_unlock_bh(&net->sctp.local_addr_lock); |
22626216 | 127 | if (found) |
1231f0ba | 128 | kfree_rcu(addr, rcu); |
29c7cf96 SS |
129 | break; |
130 | } | |
131 | ||
132 | return NOTIFY_DONE; | |
133 | } | |
134 | ||
1da177e4 | 135 | static struct notifier_block sctp_inet6addr_notifier = { |
29c7cf96 | 136 | .notifier_call = sctp_inet6addr_event, |
1da177e4 LT |
137 | }; |
138 | ||
139 | /* ICMP error handler. */ | |
32bbd879 | 140 | static int sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
dda91928 | 141 | u8 type, u8 code, int offset, __be32 info) |
1da177e4 LT |
142 | { |
143 | struct inet6_dev *idev; | |
1da177e4 | 144 | struct sock *sk; |
1da177e4 LT |
145 | struct sctp_association *asoc; |
146 | struct sctp_transport *transport; | |
147 | struct ipv6_pinfo *np; | |
2c928e0e | 148 | __u16 saveip, savesctp; |
32bbd879 | 149 | int err, ret = 0; |
4110cc25 | 150 | struct net *net = dev_net(skb->dev); |
1da177e4 LT |
151 | |
152 | idev = in6_dev_get(skb->dev); | |
153 | ||
154 | /* Fix up skb to look at the embedded net header. */ | |
b0e380b1 ACM |
155 | saveip = skb->network_header; |
156 | savesctp = skb->transport_header; | |
1ced98e8 | 157 | skb_reset_network_header(skb); |
a27ef749 | 158 | skb_set_transport_header(skb, offset); |
4110cc25 | 159 | sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport); |
1da177e4 | 160 | /* Put back, the original pointers. */ |
b0e380b1 ACM |
161 | skb->network_header = saveip; |
162 | skb->transport_header = savesctp; | |
1da177e4 | 163 | if (!sk) { |
a16292a0 | 164 | __ICMP6_INC_STATS(net, idev, ICMP6_MIB_INERRORS); |
32bbd879 | 165 | ret = -ENOENT; |
1da177e4 LT |
166 | goto out; |
167 | } | |
168 | ||
169 | /* Warning: The sock lock is held. Remember to call | |
170 | * sctp_err_finish! | |
171 | */ | |
172 | ||
173 | switch (type) { | |
174 | case ICMPV6_PKT_TOOBIG: | |
790e38bc HFS |
175 | if (ip6_sk_accept_pmtu(sk)) |
176 | sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info)); | |
1da177e4 LT |
177 | goto out_unlock; |
178 | case ICMPV6_PARAMPROB: | |
179 | if (ICMPV6_UNK_NEXTHDR == code) { | |
d1ad1ff2 | 180 | sctp_icmp_proto_unreachable(sk, asoc, transport); |
1da177e4 LT |
181 | goto out_unlock; |
182 | } | |
183 | break; | |
ec18d9a2 DM |
184 | case NDISC_REDIRECT: |
185 | sctp_icmp_redirect(sk, transport, skb); | |
3f96a532 | 186 | goto out_unlock; |
1da177e4 LT |
187 | default: |
188 | break; | |
189 | } | |
190 | ||
191 | np = inet6_sk(sk); | |
192 | icmpv6_err_convert(type, code, &err); | |
193 | if (!sock_owned_by_user(sk) && np->recverr) { | |
194 | sk->sk_err = err; | |
195 | sk->sk_error_report(sk); | |
196 | } else { /* Only an error on timeout */ | |
197 | sk->sk_err_soft = err; | |
198 | } | |
199 | ||
200 | out_unlock: | |
dae399d7 | 201 | sctp_err_finish(sk, transport); |
1da177e4 LT |
202 | out: |
203 | if (likely(idev != NULL)) | |
204 | in6_dev_put(idev); | |
32bbd879 SB |
205 | |
206 | return ret; | |
1da177e4 LT |
207 | } |
208 | ||
f880374c | 209 | static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport) |
1da177e4 LT |
210 | { |
211 | struct sock *sk = skb->sk; | |
212 | struct ipv6_pinfo *np = inet6_sk(sk); | |
95ee6208 | 213 | struct flowi6 *fl6 = &transport->fl.u.ip6; |
8a9c58d2 | 214 | __u8 tclass = np->tclass; |
c836a8ba | 215 | int res; |
1da177e4 | 216 | |
bb33381d | 217 | pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb, |
95ee6208 | 218 | skb->len, &fl6->saddr, &fl6->daddr); |
1da177e4 | 219 | |
8a9c58d2 XL |
220 | if (transport->dscp & SCTP_DSCP_SET_MASK) |
221 | tclass = transport->dscp & SCTP_DSCP_VAL_MASK; | |
222 | ||
223 | if (INET_ECN_is_capable(tclass)) | |
224 | IP6_ECN_flow_xmit(sk, fl6->flowlabel); | |
1da177e4 | 225 | |
f880374c | 226 | if (!(transport->param_flags & SPP_PMTUD_ENABLE)) |
60ff7467 | 227 | skb->ignore_df = 1; |
f880374c | 228 | |
95ee6208 DB |
229 | SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS); |
230 | ||
c836a8ba | 231 | rcu_read_lock(); |
92e55f41 | 232 | res = ip6_xmit(sk, skb, fl6, sk->sk_mark, rcu_dereference(np->opt), |
8a9c58d2 | 233 | tclass); |
c836a8ba ED |
234 | rcu_read_unlock(); |
235 | return res; | |
1da177e4 LT |
236 | } |
237 | ||
238 | /* Returns the dst cache entry for the given source and destination ip | |
239 | * addresses. | |
240 | */ | |
da0420be VY |
241 | static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr, |
242 | struct flowi *fl, struct sock *sk) | |
1da177e4 | 243 | { |
da0420be | 244 | struct sctp_association *asoc = t->asoc; |
62503411 | 245 | struct dst_entry *dst = NULL; |
9914ae3c | 246 | struct flowi6 *fl6 = &fl->u.ip6; |
62503411 | 247 | struct sctp_bind_addr *bp; |
95ee6208 | 248 | struct ipv6_pinfo *np = inet6_sk(sk); |
62503411 | 249 | struct sctp_sockaddr_entry *laddr; |
da0420be | 250 | union sctp_addr *daddr = &t->ipaddr; |
9914ae3c | 251 | union sctp_addr dst_saddr; |
95ee6208 | 252 | struct in6_addr *final_p, final; |
1c662018 | 253 | enum sctp_scope scope; |
62503411 | 254 | __u8 matchlen = 0; |
1da177e4 | 255 | |
9914ae3c | 256 | memset(fl6, 0, sizeof(struct flowi6)); |
4e3fd7a0 | 257 | fl6->daddr = daddr->v6.sin6_addr; |
9c6a02f4 VY |
258 | fl6->fl6_dport = daddr->v6.sin6_port; |
259 | fl6->flowi6_proto = IPPROTO_SCTP; | |
1da177e4 | 260 | if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) |
9914ae3c | 261 | fl6->flowi6_oif = daddr->v6.sin6_scope_id; |
25e7f2de ZL |
262 | else if (asoc) |
263 | fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if; | |
8a9c58d2 XL |
264 | if (t->flowlabel & SCTP_FLOWLABEL_SET_MASK) |
265 | fl6->flowlabel = htonl(t->flowlabel & SCTP_FLOWLABEL_VAL_MASK); | |
d808ad9a | 266 | |
0999f021 XL |
267 | if (np->sndflow && (fl6->flowlabel & IPV6_FLOWLABEL_MASK)) { |
268 | struct ip6_flowlabel *flowlabel; | |
269 | ||
270 | flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); | |
271 | if (!flowlabel) | |
272 | goto out; | |
273 | fl6_sock_release(flowlabel); | |
274 | } | |
275 | ||
bb33381d | 276 | pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr); |
1da177e4 | 277 | |
9c6a02f4 VY |
278 | if (asoc) |
279 | fl6->fl6_sport = htons(asoc->base.bind_addr.port); | |
1da177e4 LT |
280 | |
281 | if (saddr) { | |
4e3fd7a0 | 282 | fl6->saddr = saddr->v6.sin6_addr; |
ecf938fe XL |
283 | if (!fl6->fl6_sport) |
284 | fl6->fl6_sport = saddr->v6.sin6_port; | |
bb33381d DB |
285 | |
286 | pr_debug("src=%pI6 - ", &fl6->saddr); | |
1da177e4 LT |
287 | } |
288 | ||
c836a8ba ED |
289 | rcu_read_lock(); |
290 | final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final); | |
291 | rcu_read_unlock(); | |
292 | ||
0e0d44ab | 293 | dst = ip6_dst_lookup_flow(sk, fl6, final_p); |
62503411 WS |
294 | if (!asoc || saddr) |
295 | goto out; | |
296 | ||
9914ae3c VY |
297 | bp = &asoc->base.bind_addr; |
298 | scope = sctp_scope(daddr); | |
299 | /* ip6_dst_lookup has filled in the fl6->saddr for us. Check | |
300 | * to see if we can use it. | |
301 | */ | |
9c6a02f4 | 302 | if (!IS_ERR(dst)) { |
9914ae3c VY |
303 | /* Walk through the bind address list and look for a bind |
304 | * address that matches the source address of the returned dst. | |
62503411 | 305 | */ |
9914ae3c | 306 | sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port)); |
62503411 WS |
307 | rcu_read_lock(); |
308 | list_for_each_entry_rcu(laddr, &bp->address_list, list) { | |
fecda034 DB |
309 | if (!laddr->valid || laddr->state == SCTP_ADDR_DEL || |
310 | (laddr->state != SCTP_ADDR_SRC && | |
311 | !asoc->src_out_of_asoc_ok)) | |
62503411 | 312 | continue; |
9914ae3c VY |
313 | |
314 | /* Do not compare against v4 addrs */ | |
315 | if ((laddr->a.sa.sa_family == AF_INET6) && | |
316 | (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) { | |
317 | rcu_read_unlock(); | |
318 | goto out; | |
62503411 WS |
319 | } |
320 | } | |
321 | rcu_read_unlock(); | |
9914ae3c VY |
322 | /* None of the bound addresses match the source address of the |
323 | * dst. So release it. | |
324 | */ | |
325 | dst_release(dst); | |
326 | dst = NULL; | |
1da177e4 LT |
327 | } |
328 | ||
9914ae3c VY |
329 | /* Walk through the bind address list and try to get the |
330 | * best source address for a given destination. | |
331 | */ | |
332 | rcu_read_lock(); | |
333 | list_for_each_entry_rcu(laddr, &bp->address_list, list) { | |
dbc2b5e9 XL |
334 | struct dst_entry *bdst; |
335 | __u8 bmatchlen; | |
336 | ||
337 | if (!laddr->valid || | |
338 | laddr->state != SCTP_ADDR_SRC || | |
339 | laddr->a.sa.sa_family != AF_INET6 || | |
340 | scope > sctp_scope(&laddr->a)) | |
9914ae3c | 341 | continue; |
dbc2b5e9 XL |
342 | |
343 | fl6->saddr = laddr->a.v6.sin6_addr; | |
344 | fl6->fl6_sport = laddr->a.v6.sin6_port; | |
c836a8ba | 345 | final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final); |
dbc2b5e9 XL |
346 | bdst = ip6_dst_lookup_flow(sk, fl6, final_p); |
347 | ||
957d761c AK |
348 | if (IS_ERR(bdst)) |
349 | continue; | |
350 | ||
351 | if (ipv6_chk_addr(dev_net(bdst->dev), | |
dbc2b5e9 XL |
352 | &laddr->a.v6.sin6_addr, bdst->dev, 1)) { |
353 | if (!IS_ERR_OR_NULL(dst)) | |
354 | dst_release(dst); | |
355 | dst = bdst; | |
356 | break; | |
357 | } | |
358 | ||
359 | bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a); | |
957d761c AK |
360 | if (matchlen > bmatchlen) { |
361 | dst_release(bdst); | |
dbc2b5e9 | 362 | continue; |
957d761c | 363 | } |
dbc2b5e9 XL |
364 | |
365 | if (!IS_ERR_OR_NULL(dst)) | |
366 | dst_release(dst); | |
367 | dst = bdst; | |
368 | matchlen = bmatchlen; | |
9914ae3c | 369 | } |
69ce6487 | 370 | rcu_read_unlock(); |
9914ae3c | 371 | |
62503411 | 372 | out: |
ee3f34e8 | 373 | if (!IS_ERR_OR_NULL(dst)) { |
1da177e4 | 374 | struct rt6_info *rt; |
bb33381d | 375 | |
1da177e4 | 376 | rt = (struct rt6_info *)dst; |
da0420be | 377 | t->dst = dst; |
b197df4f | 378 | t->dst_cookie = rt6_get_cookie(rt); |
fd0273d7 MKL |
379 | pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n", |
380 | &rt->rt6i_dst.addr, rt->rt6i_dst.plen, | |
bb33381d | 381 | &fl6->saddr); |
da0420be VY |
382 | } else { |
383 | t->dst = NULL; | |
bb33381d DB |
384 | |
385 | pr_debug("no route\n"); | |
1da177e4 | 386 | } |
1da177e4 LT |
387 | } |
388 | ||
389 | /* Returns the number of consecutive initial bits that match in the 2 ipv6 | |
390 | * addresses. | |
391 | */ | |
392 | static inline int sctp_v6_addr_match_len(union sctp_addr *s1, | |
393 | union sctp_addr *s2) | |
394 | { | |
de7737e0 | 395 | return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr); |
1da177e4 LT |
396 | } |
397 | ||
398 | /* Fills in the source address(saddr) based on the destination address(daddr) | |
399 | * and asoc's bind address list. | |
400 | */ | |
e5117101 | 401 | static void sctp_v6_get_saddr(struct sctp_sock *sk, |
9914ae3c | 402 | struct sctp_transport *t, |
9914ae3c | 403 | struct flowi *fl) |
1da177e4 | 404 | { |
9914ae3c VY |
405 | struct flowi6 *fl6 = &fl->u.ip6; |
406 | union sctp_addr *saddr = &t->saddr; | |
1da177e4 | 407 | |
bb33381d | 408 | pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst); |
1da177e4 | 409 | |
9914ae3c VY |
410 | if (t->dst) { |
411 | saddr->v6.sin6_family = AF_INET6; | |
4e3fd7a0 | 412 | saddr->v6.sin6_addr = fl6->saddr; |
1da177e4 | 413 | } |
1da177e4 LT |
414 | } |
415 | ||
416 | /* Make a copy of all potential local addresses. */ | |
417 | static void sctp_v6_copy_addrlist(struct list_head *addrlist, | |
418 | struct net_device *dev) | |
419 | { | |
420 | struct inet6_dev *in6_dev; | |
421 | struct inet6_ifaddr *ifp; | |
422 | struct sctp_sockaddr_entry *addr; | |
423 | ||
8814c4b5 | 424 | rcu_read_lock(); |
1da177e4 | 425 | if ((in6_dev = __in6_dev_get(dev)) == NULL) { |
8814c4b5 | 426 | rcu_read_unlock(); |
1da177e4 LT |
427 | return; |
428 | } | |
429 | ||
e2eb8d45 | 430 | read_lock_bh(&in6_dev->lock); |
502a2ffd | 431 | list_for_each_entry(ifp, &in6_dev->addr_list, if_list) { |
1da177e4 | 432 | /* Add the address to the local list. */ |
939cfa75 | 433 | addr = kzalloc(sizeof(*addr), GFP_ATOMIC); |
1da177e4 | 434 | if (addr) { |
2a6fd78a | 435 | addr->a.v6.sin6_family = AF_INET6; |
4e3fd7a0 | 436 | addr->a.v6.sin6_addr = ifp->addr; |
2a6fd78a | 437 | addr->a.v6.sin6_scope_id = dev->ifindex; |
29303547 | 438 | addr->valid = 1; |
1da177e4 LT |
439 | INIT_LIST_HEAD(&addr->list); |
440 | list_add_tail(&addr->list, addrlist); | |
441 | } | |
442 | } | |
443 | ||
e2eb8d45 | 444 | read_unlock_bh(&in6_dev->lock); |
8814c4b5 | 445 | rcu_read_unlock(); |
1da177e4 LT |
446 | } |
447 | ||
b7e10c25 RH |
448 | /* Copy over any ip options */ |
449 | static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk) | |
450 | { | |
451 | struct ipv6_pinfo *newnp, *np = inet6_sk(sk); | |
452 | struct ipv6_txoptions *opt; | |
453 | ||
454 | newnp = inet6_sk(newsk); | |
455 | ||
456 | rcu_read_lock(); | |
457 | opt = rcu_dereference(np->opt); | |
458 | if (opt) { | |
459 | opt = ipv6_dup_options(newsk, opt); | |
460 | if (!opt) | |
461 | pr_err("%s: Failed to copy ip options\n", __func__); | |
462 | } | |
463 | RCU_INIT_POINTER(newnp->opt, opt); | |
464 | rcu_read_unlock(); | |
465 | } | |
466 | ||
467 | /* Account for the IP options */ | |
468 | static int sctp_v6_ip_options_len(struct sock *sk) | |
469 | { | |
470 | struct ipv6_pinfo *np = inet6_sk(sk); | |
471 | struct ipv6_txoptions *opt; | |
472 | int len = 0; | |
473 | ||
474 | rcu_read_lock(); | |
475 | opt = rcu_dereference(np->opt); | |
476 | if (opt) | |
477 | len = opt->opt_flen + opt->opt_nflen; | |
478 | ||
479 | rcu_read_unlock(); | |
480 | return len; | |
481 | } | |
482 | ||
1da177e4 | 483 | /* Initialize a sockaddr_storage from in incoming skb. */ |
cb3f837b | 484 | static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb, |
1da177e4 LT |
485 | int is_saddr) |
486 | { | |
0630c56e MRL |
487 | /* Always called on head skb, so this is safe */ |
488 | struct sctphdr *sh = sctp_hdr(skb); | |
489 | struct sockaddr_in6 *sa = &addr->v6; | |
1da177e4 | 490 | |
1da177e4 LT |
491 | addr->v6.sin6_family = AF_INET6; |
492 | addr->v6.sin6_flowinfo = 0; /* FIXME */ | |
493 | addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif; | |
494 | ||
1da177e4 | 495 | if (is_saddr) { |
0630c56e MRL |
496 | sa->sin6_port = sh->source; |
497 | sa->sin6_addr = ipv6_hdr(skb)->saddr; | |
1da177e4 | 498 | } else { |
0630c56e MRL |
499 | sa->sin6_port = sh->dest; |
500 | sa->sin6_addr = ipv6_hdr(skb)->daddr; | |
1da177e4 | 501 | } |
1da177e4 LT |
502 | } |
503 | ||
504 | /* Initialize an sctp_addr from a socket. */ | |
505 | static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk) | |
506 | { | |
507 | addr->v6.sin6_family = AF_INET6; | |
7dcdbd95 | 508 | addr->v6.sin6_port = 0; |
efe4208f | 509 | addr->v6.sin6_addr = sk->sk_v6_rcv_saddr; |
1da177e4 LT |
510 | } |
511 | ||
512 | /* Initialize sk->sk_rcv_saddr from sctp_addr. */ | |
513 | static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk) | |
514 | { | |
299ee123 | 515 | if (addr->sa.sa_family == AF_INET) { |
efe4208f ED |
516 | sk->sk_v6_rcv_saddr.s6_addr32[0] = 0; |
517 | sk->sk_v6_rcv_saddr.s6_addr32[1] = 0; | |
518 | sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff); | |
519 | sk->sk_v6_rcv_saddr.s6_addr32[3] = | |
1da177e4 LT |
520 | addr->v4.sin_addr.s_addr; |
521 | } else { | |
efe4208f | 522 | sk->sk_v6_rcv_saddr = addr->v6.sin6_addr; |
1da177e4 LT |
523 | } |
524 | } | |
525 | ||
526 | /* Initialize sk->sk_daddr from sctp_addr. */ | |
527 | static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk) | |
528 | { | |
299ee123 | 529 | if (addr->sa.sa_family == AF_INET) { |
efe4208f ED |
530 | sk->sk_v6_daddr.s6_addr32[0] = 0; |
531 | sk->sk_v6_daddr.s6_addr32[1] = 0; | |
532 | sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff); | |
533 | sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr; | |
1da177e4 | 534 | } else { |
efe4208f | 535 | sk->sk_v6_daddr = addr->v6.sin6_addr; |
1da177e4 LT |
536 | } |
537 | } | |
538 | ||
539 | /* Initialize a sctp_addr from an address parameter. */ | |
540 | static void sctp_v6_from_addr_param(union sctp_addr *addr, | |
541 | union sctp_addr_param *param, | |
dd86d136 | 542 | __be16 port, int iif) |
1da177e4 LT |
543 | { |
544 | addr->v6.sin6_family = AF_INET6; | |
545 | addr->v6.sin6_port = port; | |
546 | addr->v6.sin6_flowinfo = 0; /* BUG */ | |
4e3fd7a0 | 547 | addr->v6.sin6_addr = param->v6.addr; |
1da177e4 LT |
548 | addr->v6.sin6_scope_id = iif; |
549 | } | |
550 | ||
551 | /* Initialize an address parameter from a sctp_addr and return the length | |
552 | * of the address parameter. | |
553 | */ | |
554 | static int sctp_v6_to_addr_param(const union sctp_addr *addr, | |
555 | union sctp_addr_param *param) | |
556 | { | |
00987cc0 | 557 | int length = sizeof(struct sctp_ipv6addr_param); |
1da177e4 LT |
558 | |
559 | param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS; | |
dbc16db1 | 560 | param->v6.param_hdr.length = htons(length); |
4e3fd7a0 | 561 | param->v6.addr = addr->v6.sin6_addr; |
1da177e4 LT |
562 | |
563 | return length; | |
564 | } | |
565 | ||
9914ae3c VY |
566 | /* Initialize a sctp_addr from struct in6_addr. */ |
567 | static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr, | |
854d43a4 | 568 | __be16 port) |
1da177e4 | 569 | { |
1da177e4 LT |
570 | addr->sa.sa_family = AF_INET6; |
571 | addr->v6.sin6_port = port; | |
15339e44 | 572 | addr->v6.sin6_flowinfo = 0; |
4e3fd7a0 | 573 | addr->v6.sin6_addr = *saddr; |
15339e44 | 574 | addr->v6.sin6_scope_id = 0; |
1da177e4 LT |
575 | } |
576 | ||
1071ec9d XL |
577 | static int __sctp_v6_cmp_addr(const union sctp_addr *addr1, |
578 | const union sctp_addr *addr2) | |
1da177e4 LT |
579 | { |
580 | if (addr1->sa.sa_family != addr2->sa.sa_family) { | |
581 | if (addr1->sa.sa_family == AF_INET && | |
582 | addr2->sa.sa_family == AF_INET6 && | |
1071ec9d XL |
583 | ipv6_addr_v4mapped(&addr2->v6.sin6_addr) && |
584 | addr2->v6.sin6_addr.s6_addr32[3] == | |
585 | addr1->v4.sin_addr.s_addr) | |
586 | return 1; | |
587 | ||
1da177e4 LT |
588 | if (addr2->sa.sa_family == AF_INET && |
589 | addr1->sa.sa_family == AF_INET6 && | |
1071ec9d XL |
590 | ipv6_addr_v4mapped(&addr1->v6.sin6_addr) && |
591 | addr1->v6.sin6_addr.s6_addr32[3] == | |
592 | addr2->v4.sin_addr.s_addr) | |
593 | return 1; | |
594 | ||
1da177e4 LT |
595 | return 0; |
596 | } | |
1071ec9d | 597 | |
1da177e4 LT |
598 | if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr)) |
599 | return 0; | |
1071ec9d | 600 | |
1da177e4 | 601 | /* If this is a linklocal address, compare the scope_id. */ |
1071ec9d XL |
602 | if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) && |
603 | addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id && | |
604 | addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id) | |
605 | return 0; | |
1da177e4 LT |
606 | |
607 | return 1; | |
608 | } | |
609 | ||
1071ec9d XL |
610 | /* Compare addresses exactly. |
611 | * v4-mapped-v6 is also in consideration. | |
612 | */ | |
613 | static int sctp_v6_cmp_addr(const union sctp_addr *addr1, | |
614 | const union sctp_addr *addr2) | |
615 | { | |
616 | return __sctp_v6_cmp_addr(addr1, addr2) && | |
617 | addr1->v6.sin6_port == addr2->v6.sin6_port; | |
618 | } | |
619 | ||
1da177e4 | 620 | /* Initialize addr struct to INADDR_ANY. */ |
6fbfa9f9 | 621 | static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port) |
1da177e4 LT |
622 | { |
623 | memset(addr, 0x00, sizeof(union sctp_addr)); | |
624 | addr->v6.sin6_family = AF_INET6; | |
625 | addr->v6.sin6_port = port; | |
626 | } | |
627 | ||
628 | /* Is this a wildcard address? */ | |
629 | static int sctp_v6_is_any(const union sctp_addr *addr) | |
630 | { | |
b9b9e10f | 631 | return ipv6_addr_any(&addr->v6.sin6_addr); |
1da177e4 LT |
632 | } |
633 | ||
634 | /* Should this be available for binding? */ | |
635 | static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp) | |
636 | { | |
637 | int type; | |
9b974202 | 638 | struct net *net = sock_net(&sp->inet.sk); |
b71d1d42 | 639 | const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr; |
1da177e4 LT |
640 | |
641 | type = ipv6_addr_type(in6); | |
642 | if (IPV6_ADDR_ANY == type) | |
643 | return 1; | |
644 | if (type == IPV6_ADDR_MAPPED) { | |
1da177e4 LT |
645 | if (sp && ipv6_only_sock(sctp_opt2sk(sp))) |
646 | return 0; | |
647 | sctp_v6_map_v4(addr); | |
648 | return sctp_get_af_specific(AF_INET)->available(addr, sp); | |
649 | } | |
650 | if (!(type & IPV6_ADDR_UNICAST)) | |
651 | return 0; | |
652 | ||
9b974202 XL |
653 | return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind || |
654 | ipv6_chk_addr(net, in6, NULL, 0); | |
1da177e4 LT |
655 | } |
656 | ||
657 | /* This function checks if the address is a valid address to be used for | |
658 | * SCTP. | |
659 | * | |
660 | * Output: | |
661 | * Return 0 - If the address is a non-unicast or an illegal address. | |
662 | * Return 1 - If the address is a unicast. | |
663 | */ | |
5636bef7 VY |
664 | static int sctp_v6_addr_valid(union sctp_addr *addr, |
665 | struct sctp_sock *sp, | |
666 | const struct sk_buff *skb) | |
1da177e4 LT |
667 | { |
668 | int ret = ipv6_addr_type(&addr->v6.sin6_addr); | |
669 | ||
670 | /* Support v4-mapped-v6 address. */ | |
671 | if (ret == IPV6_ADDR_MAPPED) { | |
672 | /* Note: This routine is used in input, so v4-mapped-v6 | |
673 | * are disallowed here when there is no sctp_sock. | |
674 | */ | |
1da177e4 LT |
675 | if (sp && ipv6_only_sock(sctp_opt2sk(sp))) |
676 | return 0; | |
677 | sctp_v6_map_v4(addr); | |
5636bef7 | 678 | return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb); |
1da177e4 LT |
679 | } |
680 | ||
681 | /* Is this a non-unicast address */ | |
682 | if (!(ret & IPV6_ADDR_UNICAST)) | |
683 | return 0; | |
684 | ||
685 | return 1; | |
686 | } | |
687 | ||
688 | /* What is the scope of 'addr'? */ | |
1c662018 | 689 | static enum sctp_scope sctp_v6_scope(union sctp_addr *addr) |
1da177e4 | 690 | { |
1c662018 | 691 | enum sctp_scope retval; |
1da177e4 | 692 | int v6scope; |
1da177e4 LT |
693 | |
694 | /* The IPv6 scope is really a set of bit fields. | |
695 | * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope. | |
696 | */ | |
697 | ||
698 | v6scope = ipv6_addr_scope(&addr->v6.sin6_addr); | |
699 | switch (v6scope) { | |
700 | case IFA_HOST: | |
701 | retval = SCTP_SCOPE_LOOPBACK; | |
702 | break; | |
703 | case IFA_LINK: | |
704 | retval = SCTP_SCOPE_LINK; | |
705 | break; | |
706 | case IFA_SITE: | |
707 | retval = SCTP_SCOPE_PRIVATE; | |
708 | break; | |
709 | default: | |
710 | retval = SCTP_SCOPE_GLOBAL; | |
711 | break; | |
3ff50b79 | 712 | } |
1da177e4 LT |
713 | |
714 | return retval; | |
715 | } | |
716 | ||
717 | /* Create and initialize a new sk for the socket to be returned by accept(). */ | |
718 | static struct sock *sctp_v6_create_accept_sk(struct sock *sk, | |
cdfbabfb DH |
719 | struct sctp_association *asoc, |
720 | bool kern) | |
1da177e4 | 721 | { |
1da177e4 | 722 | struct sock *newsk; |
1da177e4 LT |
723 | struct ipv6_pinfo *newnp, *np = inet6_sk(sk); |
724 | struct sctp6_sock *newsctp6sk; | |
725 | ||
cdfbabfb | 726 | newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern); |
1da177e4 LT |
727 | if (!newsk) |
728 | goto out; | |
729 | ||
730 | sock_init_data(NULL, newsk); | |
731 | ||
914e1c8b | 732 | sctp_copy_sock(newsk, sk, asoc); |
1da177e4 LT |
733 | sock_reset_flag(sk, SOCK_ZAPPED); |
734 | ||
735 | newsctp6sk = (struct sctp6_sock *)newsk; | |
736 | inet_sk(newsk)->pinet6 = &newsctp6sk->inet6; | |
737 | ||
b225b884 DJ |
738 | sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped; |
739 | ||
1da177e4 LT |
740 | newnp = inet6_sk(newsk); |
741 | ||
742 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); | |
fdcee2cb ED |
743 | newnp->ipv6_mc_list = NULL; |
744 | newnp->ipv6_ac_list = NULL; | |
745 | newnp->ipv6_fl_list = NULL; | |
1da177e4 | 746 | |
b7e10c25 | 747 | sctp_v6_copy_ip_options(sk, newsk); |
9470e24f | 748 | |
1da177e4 LT |
749 | /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname() |
750 | * and getpeername(). | |
751 | */ | |
1da177e4 LT |
752 | sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk); |
753 | ||
661dbf34 MGP |
754 | newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr; |
755 | ||
e6848976 | 756 | sk_refcnt_debug_inc(newsk); |
1da177e4 LT |
757 | |
758 | if (newsk->sk_prot->init(newsk)) { | |
759 | sk_common_release(newsk); | |
760 | newsk = NULL; | |
761 | } | |
762 | ||
763 | out: | |
764 | return newsk; | |
765 | } | |
766 | ||
299ee123 JG |
767 | /* Format a sockaddr for return to user space. This makes sure the return is |
768 | * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option. | |
769 | */ | |
770 | static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr) | |
1da177e4 | 771 | { |
299ee123 JG |
772 | if (sp->v4mapped) { |
773 | if (addr->sa.sa_family == AF_INET) | |
774 | sctp_v4_map_v6(addr); | |
775 | } else { | |
776 | if (addr->sa.sa_family == AF_INET6 && | |
777 | ipv6_addr_v4mapped(&addr->v6.sin6_addr)) | |
778 | sctp_v6_map_v4(addr); | |
779 | } | |
780 | ||
6780db24 ED |
781 | if (addr->sa.sa_family == AF_INET) { |
782 | memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); | |
299ee123 | 783 | return sizeof(struct sockaddr_in); |
6780db24 | 784 | } |
299ee123 | 785 | return sizeof(struct sockaddr_in6); |
1da177e4 LT |
786 | } |
787 | ||
788 | /* Where did this skb come from? */ | |
789 | static int sctp_v6_skb_iif(const struct sk_buff *skb) | |
790 | { | |
1f45f78f | 791 | return IP6CB(skb)->iif; |
1da177e4 LT |
792 | } |
793 | ||
794 | /* Was this packet marked by Explicit Congestion Notification? */ | |
795 | static int sctp_v6_is_ce(const struct sk_buff *skb) | |
796 | { | |
978aa047 | 797 | return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20); |
1da177e4 LT |
798 | } |
799 | ||
800 | /* Dump the v6 addr to the seq file. */ | |
801 | static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) | |
802 | { | |
5b095d98 | 803 | seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr); |
1da177e4 LT |
804 | } |
805 | ||
b9031d9d VY |
806 | static void sctp_v6_ecn_capable(struct sock *sk) |
807 | { | |
808 | inet6_sk(sk)->tclass |= INET_ECN_ECT_0; | |
809 | } | |
810 | ||
1da177e4 LT |
811 | /* Initialize a PF_INET msgname from a ulpevent. */ |
812 | static void sctp_inet6_event_msgname(struct sctp_ulpevent *event, | |
813 | char *msgname, int *addrlen) | |
814 | { | |
299ee123 JG |
815 | union sctp_addr *addr; |
816 | struct sctp_association *asoc; | |
817 | union sctp_addr *paddr; | |
1da177e4 | 818 | |
299ee123 JG |
819 | if (!msgname) |
820 | return; | |
1da177e4 | 821 | |
299ee123 JG |
822 | addr = (union sctp_addr *)msgname; |
823 | asoc = event->asoc; | |
824 | paddr = &asoc->peer.primary_addr; | |
1da177e4 | 825 | |
299ee123 JG |
826 | if (paddr->sa.sa_family == AF_INET) { |
827 | addr->v4.sin_family = AF_INET; | |
828 | addr->v4.sin_port = htons(asoc->peer.port); | |
829 | addr->v4.sin_addr = paddr->v4.sin_addr; | |
830 | } else { | |
831 | addr->v6.sin6_family = AF_INET6; | |
832 | addr->v6.sin6_flowinfo = 0; | |
833 | if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) | |
834 | addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id; | |
835 | else | |
836 | addr->v6.sin6_scope_id = 0; | |
837 | addr->v6.sin6_port = htons(asoc->peer.port); | |
838 | addr->v6.sin6_addr = paddr->v6.sin6_addr; | |
1da177e4 | 839 | } |
299ee123 JG |
840 | |
841 | *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr); | |
1da177e4 LT |
842 | } |
843 | ||
844 | /* Initialize a msg_name from an inbound skb. */ | |
845 | static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname, | |
846 | int *addr_len) | |
847 | { | |
299ee123 | 848 | union sctp_addr *addr; |
1da177e4 | 849 | struct sctphdr *sh; |
1da177e4 | 850 | |
299ee123 JG |
851 | if (!msgname) |
852 | return; | |
853 | ||
854 | addr = (union sctp_addr *)msgname; | |
855 | sh = sctp_hdr(skb); | |
856 | ||
857 | if (ip_hdr(skb)->version == 4) { | |
858 | addr->v4.sin_family = AF_INET; | |
859 | addr->v4.sin_port = sh->source; | |
1f45f78f | 860 | addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr; |
299ee123 JG |
861 | } else { |
862 | addr->v6.sin6_family = AF_INET6; | |
863 | addr->v6.sin6_flowinfo = 0; | |
864 | addr->v6.sin6_port = sh->source; | |
865 | addr->v6.sin6_addr = ipv6_hdr(skb)->saddr; | |
7c8a61d9 | 866 | if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) |
1f45f78f | 867 | addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb); |
7c8a61d9 EB |
868 | else |
869 | addr->v6.sin6_scope_id = 0; | |
1da177e4 | 870 | } |
299ee123 JG |
871 | |
872 | *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr); | |
1da177e4 LT |
873 | } |
874 | ||
875 | /* Do we support this AF? */ | |
876 | static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp) | |
877 | { | |
878 | switch (family) { | |
879 | case AF_INET6: | |
880 | return 1; | |
881 | /* v4-mapped-v6 addresses */ | |
882 | case AF_INET: | |
7dab83de | 883 | if (!__ipv6_only_sock(sctp_opt2sk(sp))) |
1da177e4 | 884 | return 1; |
37f47bc9 | 885 | /* fallthru */ |
1da177e4 LT |
886 | default: |
887 | return 0; | |
888 | } | |
889 | } | |
890 | ||
891 | /* Address matching with wildcards allowed. This extra level | |
892 | * of indirection lets us choose whether a PF_INET6 should | |
893 | * disallow any v4 addresses if we so choose. | |
894 | */ | |
895 | static int sctp_inet6_cmp_addr(const union sctp_addr *addr1, | |
896 | const union sctp_addr *addr2, | |
897 | struct sctp_sock *opt) | |
898 | { | |
52cae8f0 | 899 | struct sock *sk = sctp_opt2sk(opt); |
1071ec9d | 900 | struct sctp_af *af1, *af2; |
1da177e4 LT |
901 | |
902 | af1 = sctp_get_af_specific(addr1->sa.sa_family); | |
903 | af2 = sctp_get_af_specific(addr2->sa.sa_family); | |
904 | ||
905 | if (!af1 || !af2) | |
906 | return 0; | |
7dab83de VY |
907 | |
908 | /* If the socket is IPv6 only, v4 addrs will not match */ | |
52cae8f0 | 909 | if (__ipv6_only_sock(sk) && af1 != af2) |
7dab83de VY |
910 | return 0; |
911 | ||
1da177e4 | 912 | /* Today, wildcard AF_INET/AF_INET6. */ |
52cae8f0 | 913 | if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2)) |
1da177e4 LT |
914 | return 1; |
915 | ||
d625329b XL |
916 | if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET) |
917 | return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr; | |
918 | ||
1071ec9d | 919 | return __sctp_v6_cmp_addr(addr1, addr2); |
1da177e4 LT |
920 | } |
921 | ||
922 | /* Verify that the provided sockaddr looks bindable. Common verification, | |
923 | * has already been taken care of. | |
924 | */ | |
925 | static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) | |
926 | { | |
927 | struct sctp_af *af; | |
928 | ||
929 | /* ASSERT: address family has already been verified. */ | |
930 | if (addr->sa.sa_family != AF_INET6) | |
931 | af = sctp_get_af_specific(addr->sa.sa_family); | |
932 | else { | |
1da177e4 | 933 | int type = ipv6_addr_type(&addr->v6.sin6_addr); |
6a6ddb2a SS |
934 | struct net_device *dev; |
935 | ||
1da177e4 | 936 | if (type & IPV6_ADDR_LINKLOCAL) { |
bb2db45b | 937 | struct net *net; |
6a6ddb2a SS |
938 | if (!addr->v6.sin6_scope_id) |
939 | return 0; | |
bb2db45b | 940 | net = sock_net(&opt->inet.sk); |
40c9c31e | 941 | rcu_read_lock(); |
bb2db45b | 942 | dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id); |
b71d21c2 LT |
943 | if (!dev || !(opt->inet.freebind || |
944 | net->ipv6.sysctl.ip_nonlocal_bind || | |
945 | ipv6_chk_addr(net, &addr->v6.sin6_addr, | |
946 | dev, 0))) { | |
40c9c31e | 947 | rcu_read_unlock(); |
1669d857 VY |
948 | return 0; |
949 | } | |
40c9c31e | 950 | rcu_read_unlock(); |
1da177e4 | 951 | } |
7dab83de | 952 | |
1da177e4 LT |
953 | af = opt->pf->af; |
954 | } | |
955 | return af->available(addr, opt); | |
956 | } | |
957 | ||
6a6ddb2a | 958 | /* Verify that the provided sockaddr looks sendable. Common verification, |
1da177e4 LT |
959 | * has already been taken care of. |
960 | */ | |
961 | static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr) | |
962 | { | |
963 | struct sctp_af *af = NULL; | |
964 | ||
965 | /* ASSERT: address family has already been verified. */ | |
966 | if (addr->sa.sa_family != AF_INET6) | |
967 | af = sctp_get_af_specific(addr->sa.sa_family); | |
968 | else { | |
1da177e4 | 969 | int type = ipv6_addr_type(&addr->v6.sin6_addr); |
6a6ddb2a SS |
970 | struct net_device *dev; |
971 | ||
1da177e4 | 972 | if (type & IPV6_ADDR_LINKLOCAL) { |
6a6ddb2a SS |
973 | if (!addr->v6.sin6_scope_id) |
974 | return 0; | |
40c9c31e | 975 | rcu_read_lock(); |
bb2db45b | 976 | dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk), |
40c9c31e ED |
977 | addr->v6.sin6_scope_id); |
978 | rcu_read_unlock(); | |
6a6ddb2a | 979 | if (!dev) |
1da177e4 LT |
980 | return 0; |
981 | } | |
982 | af = opt->pf->af; | |
983 | } | |
984 | ||
985 | return af != NULL; | |
986 | } | |
987 | ||
988 | /* Fill in Supported Address Type information for INIT and INIT-ACK | |
989 | * chunks. Note: In the future, we may want to look at sock options | |
990 | * to determine whether a PF_INET6 socket really wants to have IPV4 | |
991 | * addresses. | |
992 | * Returns number of addresses supported. | |
993 | */ | |
994 | static int sctp_inet6_supported_addrs(const struct sctp_sock *opt, | |
3dbe8656 | 995 | __be16 *types) |
1da177e4 | 996 | { |
7dab83de VY |
997 | types[0] = SCTP_PARAM_IPV6_ADDRESS; |
998 | if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) { | |
999 | types[1] = SCTP_PARAM_IPV4_ADDRESS; | |
1000 | return 2; | |
1001 | } | |
1002 | return 1; | |
1da177e4 LT |
1003 | } |
1004 | ||
299ee123 JG |
1005 | /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */ |
1006 | static int sctp_getname(struct socket *sock, struct sockaddr *uaddr, | |
9b2c45d4 | 1007 | int peer) |
299ee123 JG |
1008 | { |
1009 | int rc; | |
1010 | ||
9b2c45d4 | 1011 | rc = inet6_getname(sock, uaddr, peer); |
299ee123 | 1012 | |
9b2c45d4 | 1013 | if (rc < 0) |
299ee123 JG |
1014 | return rc; |
1015 | ||
9b2c45d4 | 1016 | rc = sctp_v6_addr_to_user(sctp_sk(sock->sk), |
299ee123 JG |
1017 | (union sctp_addr *)uaddr); |
1018 | ||
1019 | return rc; | |
1020 | } | |
1021 | ||
90ddc4f0 | 1022 | static const struct proto_ops inet6_seqpacket_ops = { |
543d9cfe ACM |
1023 | .family = PF_INET6, |
1024 | .owner = THIS_MODULE, | |
1025 | .release = inet6_release, | |
1026 | .bind = inet6_bind, | |
644fbdea | 1027 | .connect = sctp_inet_connect, |
543d9cfe ACM |
1028 | .socketpair = sock_no_socketpair, |
1029 | .accept = inet_accept, | |
299ee123 | 1030 | .getname = sctp_getname, |
a11e1d43 | 1031 | .poll = sctp_poll, |
543d9cfe ACM |
1032 | .ioctl = inet6_ioctl, |
1033 | .listen = sctp_inet_listen, | |
1034 | .shutdown = inet_shutdown, | |
1035 | .setsockopt = sock_common_setsockopt, | |
1036 | .getsockopt = sock_common_getsockopt, | |
1037 | .sendmsg = inet_sendmsg, | |
fd2d180a | 1038 | .recvmsg = inet_recvmsg, |
543d9cfe | 1039 | .mmap = sock_no_mmap, |
3fdadf7d | 1040 | #ifdef CONFIG_COMPAT |
543d9cfe ACM |
1041 | .compat_setsockopt = compat_sock_common_setsockopt, |
1042 | .compat_getsockopt = compat_sock_common_getsockopt, | |
3fdadf7d | 1043 | #endif |
1da177e4 LT |
1044 | }; |
1045 | ||
1046 | static struct inet_protosw sctpv6_seqpacket_protosw = { | |
1047 | .type = SOCK_SEQPACKET, | |
1048 | .protocol = IPPROTO_SCTP, | |
1049 | .prot = &sctpv6_prot, | |
1050 | .ops = &inet6_seqpacket_ops, | |
1da177e4 LT |
1051 | .flags = SCTP_PROTOSW_FLAG |
1052 | }; | |
1053 | static struct inet_protosw sctpv6_stream_protosw = { | |
1054 | .type = SOCK_STREAM, | |
1055 | .protocol = IPPROTO_SCTP, | |
1056 | .prot = &sctpv6_prot, | |
1057 | .ops = &inet6_seqpacket_ops, | |
1da177e4 LT |
1058 | .flags = SCTP_PROTOSW_FLAG, |
1059 | }; | |
1060 | ||
e5bbef20 | 1061 | static int sctp6_rcv(struct sk_buff *skb) |
1da177e4 | 1062 | { |
e5bbef20 | 1063 | return sctp_rcv(skb) ? -1 : 0; |
1da177e4 LT |
1064 | } |
1065 | ||
41135cc8 | 1066 | static const struct inet6_protocol sctpv6_protocol = { |
1da177e4 LT |
1067 | .handler = sctp6_rcv, |
1068 | .err_handler = sctp_v6_err, | |
1069 | .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL, | |
1070 | }; | |
1071 | ||
15efbe76 | 1072 | static struct sctp_af sctp_af_inet6 = { |
543d9cfe ACM |
1073 | .sa_family = AF_INET6, |
1074 | .sctp_xmit = sctp_v6_xmit, | |
1075 | .setsockopt = ipv6_setsockopt, | |
1076 | .getsockopt = ipv6_getsockopt, | |
1077 | .get_dst = sctp_v6_get_dst, | |
1078 | .get_saddr = sctp_v6_get_saddr, | |
1079 | .copy_addrlist = sctp_v6_copy_addrlist, | |
1080 | .from_skb = sctp_v6_from_skb, | |
1081 | .from_sk = sctp_v6_from_sk, | |
543d9cfe ACM |
1082 | .from_addr_param = sctp_v6_from_addr_param, |
1083 | .to_addr_param = sctp_v6_to_addr_param, | |
543d9cfe ACM |
1084 | .cmp_addr = sctp_v6_cmp_addr, |
1085 | .scope = sctp_v6_scope, | |
1086 | .addr_valid = sctp_v6_addr_valid, | |
1087 | .inaddr_any = sctp_v6_inaddr_any, | |
1088 | .is_any = sctp_v6_is_any, | |
1089 | .available = sctp_v6_available, | |
1090 | .skb_iif = sctp_v6_skb_iif, | |
1091 | .is_ce = sctp_v6_is_ce, | |
1092 | .seq_dump_addr = sctp_v6_seq_dump_addr, | |
b9031d9d | 1093 | .ecn_capable = sctp_v6_ecn_capable, |
543d9cfe ACM |
1094 | .net_header_len = sizeof(struct ipv6hdr), |
1095 | .sockaddr_len = sizeof(struct sockaddr_in6), | |
b7e10c25 | 1096 | .ip_options_len = sctp_v6_ip_options_len, |
3fdadf7d | 1097 | #ifdef CONFIG_COMPAT |
543d9cfe ACM |
1098 | .compat_setsockopt = compat_ipv6_setsockopt, |
1099 | .compat_getsockopt = compat_ipv6_getsockopt, | |
3fdadf7d | 1100 | #endif |
1da177e4 LT |
1101 | }; |
1102 | ||
15efbe76 | 1103 | static struct sctp_pf sctp_pf_inet6 = { |
1da177e4 LT |
1104 | .event_msgname = sctp_inet6_event_msgname, |
1105 | .skb_msgname = sctp_inet6_skb_msgname, | |
1106 | .af_supported = sctp_inet6_af_supported, | |
1107 | .cmp_addr = sctp_inet6_cmp_addr, | |
1108 | .bind_verify = sctp_inet6_bind_verify, | |
1109 | .send_verify = sctp_inet6_send_verify, | |
1110 | .supported_addrs = sctp_inet6_supported_addrs, | |
1111 | .create_accept_sk = sctp_v6_create_accept_sk, | |
299ee123 JG |
1112 | .addr_to_user = sctp_v6_addr_to_user, |
1113 | .to_sk_saddr = sctp_v6_to_sk_saddr, | |
1114 | .to_sk_daddr = sctp_v6_to_sk_daddr, | |
b7e10c25 | 1115 | .copy_ip_options = sctp_v6_copy_ip_options, |
15efbe76 | 1116 | .af = &sctp_af_inet6, |
1da177e4 LT |
1117 | }; |
1118 | ||
827bf122 | 1119 | /* Initialize IPv6 support and register with socket layer. */ |
270637ab | 1120 | void sctp_v6_pf_init(void) |
1da177e4 | 1121 | { |
827bf122 | 1122 | /* Register the SCTP specific PF_INET6 functions. */ |
15efbe76 | 1123 | sctp_register_pf(&sctp_pf_inet6, PF_INET6); |
827bf122 SS |
1124 | |
1125 | /* Register the SCTP specific AF_INET6 functions. */ | |
15efbe76 | 1126 | sctp_register_af(&sctp_af_inet6); |
270637ab VY |
1127 | } |
1128 | ||
1129 | void sctp_v6_pf_exit(void) | |
1130 | { | |
1131 | list_del(&sctp_af_inet6.list); | |
1132 | } | |
1133 | ||
1134 | /* Initialize IPv6 support and register with socket layer. */ | |
1135 | int sctp_v6_protosw_init(void) | |
1136 | { | |
1137 | int rc; | |
827bf122 SS |
1138 | |
1139 | rc = proto_register(&sctpv6_prot, 1); | |
1da177e4 | 1140 | if (rc) |
827bf122 | 1141 | return rc; |
1da177e4 LT |
1142 | |
1143 | /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */ | |
1144 | inet6_register_protosw(&sctpv6_seqpacket_protosw); | |
1145 | inet6_register_protosw(&sctpv6_stream_protosw); | |
1146 | ||
827bf122 SS |
1147 | return 0; |
1148 | } | |
1da177e4 | 1149 | |
270637ab VY |
1150 | void sctp_v6_protosw_exit(void) |
1151 | { | |
1152 | inet6_unregister_protosw(&sctpv6_seqpacket_protosw); | |
1153 | inet6_unregister_protosw(&sctpv6_stream_protosw); | |
1154 | proto_unregister(&sctpv6_prot); | |
1155 | } | |
1156 | ||
1157 | ||
827bf122 SS |
1158 | /* Register with inet6 layer. */ |
1159 | int sctp_v6_add_protocol(void) | |
1160 | { | |
1da177e4 LT |
1161 | /* Register notifier for inet6 address additions/deletions. */ |
1162 | register_inet6addr_notifier(&sctp_inet6addr_notifier); | |
827bf122 SS |
1163 | |
1164 | if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0) | |
1165 | return -EAGAIN; | |
1166 | ||
1167 | return 0; | |
1da177e4 LT |
1168 | } |
1169 | ||
827bf122 SS |
1170 | /* Unregister with inet6 layer. */ |
1171 | void sctp_v6_del_protocol(void) | |
1172 | { | |
1173 | inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP); | |
1174 | unregister_inet6addr_notifier(&sctp_inet6addr_notifier); | |
1da177e4 | 1175 | } |