ipv6.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* SCTP kernel implementation
  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. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * This SCTP implementation is free software;
  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. *
  17. * This SCTP implementation is distributed in the hope that it
  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
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  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
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/types.h>
  46. #include <linux/socket.h>
  47. #include <linux/sockios.h>
  48. #include <linux/net.h>
  49. #include <linux/in.h>
  50. #include <linux/in6.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/init.h>
  53. #include <linux/ipsec.h>
  54. #include <linux/slab.h>
  55. #include <linux/ipv6.h>
  56. #include <linux/icmpv6.h>
  57. #include <linux/random.h>
  58. #include <linux/seq_file.h>
  59. #include <net/protocol.h>
  60. #include <net/ndisc.h>
  61. #include <net/ip.h>
  62. #include <net/ipv6.h>
  63. #include <net/transp_v6.h>
  64. #include <net/addrconf.h>
  65. #include <net/ip6_route.h>
  66. #include <net/inet_common.h>
  67. #include <net/inet_ecn.h>
  68. #include <net/sctp/sctp.h>
  69. #include <linux/uaccess.h>
  70. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  71. union sctp_addr *s2);
  72. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  73. __be16 port);
  74. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  75. const union sctp_addr *addr2);
  76. /* Event handler for inet6 address addition/deletion events.
  77. * The sctp_local_addr_list needs to be protocted by a spin lock since
  78. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  79. * time and thus corrupt the list.
  80. * The reader side is protected with RCU.
  81. */
  82. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  83. void *ptr)
  84. {
  85. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  86. struct sctp_sockaddr_entry *addr = NULL;
  87. struct sctp_sockaddr_entry *temp;
  88. struct net *net = dev_net(ifa->idev->dev);
  89. int found = 0;
  90. switch (ev) {
  91. case NETDEV_UP:
  92. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  93. if (addr) {
  94. addr->a.v6.sin6_family = AF_INET6;
  95. addr->a.v6.sin6_port = 0;
  96. addr->a.v6.sin6_addr = ifa->addr;
  97. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  98. addr->valid = 1;
  99. spin_lock_bh(&net->sctp.local_addr_lock);
  100. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  101. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  102. spin_unlock_bh(&net->sctp.local_addr_lock);
  103. }
  104. break;
  105. case NETDEV_DOWN:
  106. spin_lock_bh(&net->sctp.local_addr_lock);
  107. list_for_each_entry_safe(addr, temp,
  108. &net->sctp.local_addr_list, list) {
  109. if (addr->a.sa.sa_family == AF_INET6 &&
  110. ipv6_addr_equal(&addr->a.v6.sin6_addr,
  111. &ifa->addr)) {
  112. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  113. found = 1;
  114. addr->valid = 0;
  115. list_del_rcu(&addr->list);
  116. break;
  117. }
  118. }
  119. spin_unlock_bh(&net->sctp.local_addr_lock);
  120. if (found)
  121. kfree_rcu(addr, rcu);
  122. break;
  123. }
  124. return NOTIFY_DONE;
  125. }
  126. static struct notifier_block sctp_inet6addr_notifier = {
  127. .notifier_call = sctp_inet6addr_event,
  128. };
  129. /* ICMP error handler. */
  130. static void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  131. u8 type, u8 code, int offset, __be32 info)
  132. {
  133. struct inet6_dev *idev;
  134. struct sock *sk;
  135. struct sctp_association *asoc;
  136. struct sctp_transport *transport;
  137. struct ipv6_pinfo *np;
  138. __u16 saveip, savesctp;
  139. int err;
  140. struct net *net = dev_net(skb->dev);
  141. idev = in6_dev_get(skb->dev);
  142. /* Fix up skb to look at the embedded net header. */
  143. saveip = skb->network_header;
  144. savesctp = skb->transport_header;
  145. skb_reset_network_header(skb);
  146. skb_set_transport_header(skb, offset);
  147. sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  148. /* Put back, the original pointers. */
  149. skb->network_header = saveip;
  150. skb->transport_header = savesctp;
  151. if (!sk) {
  152. __ICMP6_INC_STATS(net, idev, ICMP6_MIB_INERRORS);
  153. goto out;
  154. }
  155. /* Warning: The sock lock is held. Remember to call
  156. * sctp_err_finish!
  157. */
  158. switch (type) {
  159. case ICMPV6_PKT_TOOBIG:
  160. if (ip6_sk_accept_pmtu(sk))
  161. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  162. goto out_unlock;
  163. case ICMPV6_PARAMPROB:
  164. if (ICMPV6_UNK_NEXTHDR == code) {
  165. sctp_icmp_proto_unreachable(sk, asoc, transport);
  166. goto out_unlock;
  167. }
  168. break;
  169. case NDISC_REDIRECT:
  170. sctp_icmp_redirect(sk, transport, skb);
  171. goto out_unlock;
  172. default:
  173. break;
  174. }
  175. np = inet6_sk(sk);
  176. icmpv6_err_convert(type, code, &err);
  177. if (!sock_owned_by_user(sk) && np->recverr) {
  178. sk->sk_err = err;
  179. sk->sk_error_report(sk);
  180. } else { /* Only an error on timeout */
  181. sk->sk_err_soft = err;
  182. }
  183. out_unlock:
  184. sctp_err_finish(sk, transport);
  185. out:
  186. if (likely(idev != NULL))
  187. in6_dev_put(idev);
  188. }
  189. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
  190. {
  191. struct sock *sk = skb->sk;
  192. struct ipv6_pinfo *np = inet6_sk(sk);
  193. struct flowi6 *fl6 = &transport->fl.u.ip6;
  194. int res;
  195. pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
  196. skb->len, &fl6->saddr, &fl6->daddr);
  197. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  198. if (!(transport->param_flags & SPP_PMTUD_ENABLE))
  199. skb->ignore_df = 1;
  200. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  201. rcu_read_lock();
  202. res = ip6_xmit(sk, skb, fl6, sk->sk_mark, rcu_dereference(np->opt),
  203. np->tclass);
  204. rcu_read_unlock();
  205. return res;
  206. }
  207. /* Returns the dst cache entry for the given source and destination ip
  208. * addresses.
  209. */
  210. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  211. struct flowi *fl, struct sock *sk)
  212. {
  213. struct sctp_association *asoc = t->asoc;
  214. struct dst_entry *dst = NULL;
  215. struct flowi6 *fl6 = &fl->u.ip6;
  216. struct sctp_bind_addr *bp;
  217. struct ipv6_pinfo *np = inet6_sk(sk);
  218. struct sctp_sockaddr_entry *laddr;
  219. union sctp_addr *daddr = &t->ipaddr;
  220. union sctp_addr dst_saddr;
  221. struct in6_addr *final_p, final;
  222. enum sctp_scope scope;
  223. __u8 matchlen = 0;
  224. memset(fl6, 0, sizeof(struct flowi6));
  225. fl6->daddr = daddr->v6.sin6_addr;
  226. fl6->fl6_dport = daddr->v6.sin6_port;
  227. fl6->flowi6_proto = IPPROTO_SCTP;
  228. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  229. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  230. else if (asoc)
  231. fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if;
  232. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  233. if (asoc)
  234. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  235. if (saddr) {
  236. fl6->saddr = saddr->v6.sin6_addr;
  237. fl6->fl6_sport = saddr->v6.sin6_port;
  238. pr_debug("src=%pI6 - ", &fl6->saddr);
  239. }
  240. rcu_read_lock();
  241. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  242. rcu_read_unlock();
  243. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  244. if (!asoc || saddr)
  245. goto out;
  246. bp = &asoc->base.bind_addr;
  247. scope = sctp_scope(daddr);
  248. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  249. * to see if we can use it.
  250. */
  251. if (!IS_ERR(dst)) {
  252. /* Walk through the bind address list and look for a bind
  253. * address that matches the source address of the returned dst.
  254. */
  255. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  256. rcu_read_lock();
  257. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  258. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  259. (laddr->state != SCTP_ADDR_SRC &&
  260. !asoc->src_out_of_asoc_ok))
  261. continue;
  262. /* Do not compare against v4 addrs */
  263. if ((laddr->a.sa.sa_family == AF_INET6) &&
  264. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  265. rcu_read_unlock();
  266. goto out;
  267. }
  268. }
  269. rcu_read_unlock();
  270. /* None of the bound addresses match the source address of the
  271. * dst. So release it.
  272. */
  273. dst_release(dst);
  274. dst = NULL;
  275. }
  276. /* Walk through the bind address list and try to get the
  277. * best source address for a given destination.
  278. */
  279. rcu_read_lock();
  280. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  281. struct dst_entry *bdst;
  282. __u8 bmatchlen;
  283. if (!laddr->valid ||
  284. laddr->state != SCTP_ADDR_SRC ||
  285. laddr->a.sa.sa_family != AF_INET6 ||
  286. scope > sctp_scope(&laddr->a))
  287. continue;
  288. fl6->saddr = laddr->a.v6.sin6_addr;
  289. fl6->fl6_sport = laddr->a.v6.sin6_port;
  290. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  291. bdst = ip6_dst_lookup_flow(sk, fl6, final_p);
  292. if (!IS_ERR(bdst) &&
  293. ipv6_chk_addr(dev_net(bdst->dev),
  294. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  295. if (!IS_ERR_OR_NULL(dst))
  296. dst_release(dst);
  297. dst = bdst;
  298. break;
  299. }
  300. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  301. if (matchlen > bmatchlen)
  302. continue;
  303. if (!IS_ERR_OR_NULL(dst))
  304. dst_release(dst);
  305. dst = bdst;
  306. matchlen = bmatchlen;
  307. }
  308. rcu_read_unlock();
  309. out:
  310. if (!IS_ERR_OR_NULL(dst)) {
  311. struct rt6_info *rt;
  312. rt = (struct rt6_info *)dst;
  313. t->dst = dst;
  314. t->dst_cookie = rt6_get_cookie(rt);
  315. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  316. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  317. &fl6->saddr);
  318. } else {
  319. t->dst = NULL;
  320. pr_debug("no route\n");
  321. }
  322. }
  323. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  324. * addresses.
  325. */
  326. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  327. union sctp_addr *s2)
  328. {
  329. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  330. }
  331. /* Fills in the source address(saddr) based on the destination address(daddr)
  332. * and asoc's bind address list.
  333. */
  334. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  335. struct sctp_transport *t,
  336. struct flowi *fl)
  337. {
  338. struct flowi6 *fl6 = &fl->u.ip6;
  339. union sctp_addr *saddr = &t->saddr;
  340. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  341. if (t->dst) {
  342. saddr->v6.sin6_family = AF_INET6;
  343. saddr->v6.sin6_addr = fl6->saddr;
  344. }
  345. }
  346. /* Make a copy of all potential local addresses. */
  347. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  348. struct net_device *dev)
  349. {
  350. struct inet6_dev *in6_dev;
  351. struct inet6_ifaddr *ifp;
  352. struct sctp_sockaddr_entry *addr;
  353. rcu_read_lock();
  354. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  355. rcu_read_unlock();
  356. return;
  357. }
  358. read_lock_bh(&in6_dev->lock);
  359. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  360. /* Add the address to the local list. */
  361. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  362. if (addr) {
  363. addr->a.v6.sin6_family = AF_INET6;
  364. addr->a.v6.sin6_port = 0;
  365. addr->a.v6.sin6_addr = ifp->addr;
  366. addr->a.v6.sin6_scope_id = dev->ifindex;
  367. addr->valid = 1;
  368. INIT_LIST_HEAD(&addr->list);
  369. list_add_tail(&addr->list, addrlist);
  370. }
  371. }
  372. read_unlock_bh(&in6_dev->lock);
  373. rcu_read_unlock();
  374. }
  375. /* Initialize a sockaddr_storage from in incoming skb. */
  376. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  377. int is_saddr)
  378. {
  379. /* Always called on head skb, so this is safe */
  380. struct sctphdr *sh = sctp_hdr(skb);
  381. struct sockaddr_in6 *sa = &addr->v6;
  382. addr->v6.sin6_family = AF_INET6;
  383. addr->v6.sin6_flowinfo = 0; /* FIXME */
  384. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  385. if (is_saddr) {
  386. sa->sin6_port = sh->source;
  387. sa->sin6_addr = ipv6_hdr(skb)->saddr;
  388. } else {
  389. sa->sin6_port = sh->dest;
  390. sa->sin6_addr = ipv6_hdr(skb)->daddr;
  391. }
  392. }
  393. /* Initialize an sctp_addr from a socket. */
  394. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  395. {
  396. addr->v6.sin6_family = AF_INET6;
  397. addr->v6.sin6_port = 0;
  398. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  399. }
  400. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  401. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  402. {
  403. if (addr->sa.sa_family == AF_INET) {
  404. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  405. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  406. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  407. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  408. addr->v4.sin_addr.s_addr;
  409. } else {
  410. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  411. }
  412. }
  413. /* Initialize sk->sk_daddr from sctp_addr. */
  414. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  415. {
  416. if (addr->sa.sa_family == AF_INET) {
  417. sk->sk_v6_daddr.s6_addr32[0] = 0;
  418. sk->sk_v6_daddr.s6_addr32[1] = 0;
  419. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  420. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  421. } else {
  422. sk->sk_v6_daddr = addr->v6.sin6_addr;
  423. }
  424. }
  425. /* Initialize a sctp_addr from an address parameter. */
  426. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  427. union sctp_addr_param *param,
  428. __be16 port, int iif)
  429. {
  430. addr->v6.sin6_family = AF_INET6;
  431. addr->v6.sin6_port = port;
  432. addr->v6.sin6_flowinfo = 0; /* BUG */
  433. addr->v6.sin6_addr = param->v6.addr;
  434. addr->v6.sin6_scope_id = iif;
  435. }
  436. /* Initialize an address parameter from a sctp_addr and return the length
  437. * of the address parameter.
  438. */
  439. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  440. union sctp_addr_param *param)
  441. {
  442. int length = sizeof(struct sctp_ipv6addr_param);
  443. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  444. param->v6.param_hdr.length = htons(length);
  445. param->v6.addr = addr->v6.sin6_addr;
  446. return length;
  447. }
  448. /* Initialize a sctp_addr from struct in6_addr. */
  449. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  450. __be16 port)
  451. {
  452. addr->sa.sa_family = AF_INET6;
  453. addr->v6.sin6_port = port;
  454. addr->v6.sin6_addr = *saddr;
  455. }
  456. /* Compare addresses exactly.
  457. * v4-mapped-v6 is also in consideration.
  458. */
  459. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  460. const union sctp_addr *addr2)
  461. {
  462. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  463. if (addr1->sa.sa_family == AF_INET &&
  464. addr2->sa.sa_family == AF_INET6 &&
  465. ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
  466. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  467. addr2->v6.sin6_addr.s6_addr32[3] ==
  468. addr1->v4.sin_addr.s_addr)
  469. return 1;
  470. }
  471. if (addr2->sa.sa_family == AF_INET &&
  472. addr1->sa.sa_family == AF_INET6 &&
  473. ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
  474. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  475. addr1->v6.sin6_addr.s6_addr32[3] ==
  476. addr2->v4.sin_addr.s_addr)
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. if (addr1->v6.sin6_port != addr2->v6.sin6_port)
  482. return 0;
  483. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  484. return 0;
  485. /* If this is a linklocal address, compare the scope_id. */
  486. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  487. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  488. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  489. return 0;
  490. }
  491. }
  492. return 1;
  493. }
  494. /* Initialize addr struct to INADDR_ANY. */
  495. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  496. {
  497. memset(addr, 0x00, sizeof(union sctp_addr));
  498. addr->v6.sin6_family = AF_INET6;
  499. addr->v6.sin6_port = port;
  500. }
  501. /* Is this a wildcard address? */
  502. static int sctp_v6_is_any(const union sctp_addr *addr)
  503. {
  504. return ipv6_addr_any(&addr->v6.sin6_addr);
  505. }
  506. /* Should this be available for binding? */
  507. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  508. {
  509. int type;
  510. struct net *net = sock_net(&sp->inet.sk);
  511. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  512. type = ipv6_addr_type(in6);
  513. if (IPV6_ADDR_ANY == type)
  514. return 1;
  515. if (type == IPV6_ADDR_MAPPED) {
  516. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  517. return 0;
  518. sctp_v6_map_v4(addr);
  519. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  520. }
  521. if (!(type & IPV6_ADDR_UNICAST))
  522. return 0;
  523. return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind ||
  524. ipv6_chk_addr(net, in6, NULL, 0);
  525. }
  526. /* This function checks if the address is a valid address to be used for
  527. * SCTP.
  528. *
  529. * Output:
  530. * Return 0 - If the address is a non-unicast or an illegal address.
  531. * Return 1 - If the address is a unicast.
  532. */
  533. static int sctp_v6_addr_valid(union sctp_addr *addr,
  534. struct sctp_sock *sp,
  535. const struct sk_buff *skb)
  536. {
  537. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  538. /* Support v4-mapped-v6 address. */
  539. if (ret == IPV6_ADDR_MAPPED) {
  540. /* Note: This routine is used in input, so v4-mapped-v6
  541. * are disallowed here when there is no sctp_sock.
  542. */
  543. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  544. return 0;
  545. sctp_v6_map_v4(addr);
  546. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  547. }
  548. /* Is this a non-unicast address */
  549. if (!(ret & IPV6_ADDR_UNICAST))
  550. return 0;
  551. return 1;
  552. }
  553. /* What is the scope of 'addr'? */
  554. static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
  555. {
  556. enum sctp_scope retval;
  557. int v6scope;
  558. /* The IPv6 scope is really a set of bit fields.
  559. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  560. */
  561. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  562. switch (v6scope) {
  563. case IFA_HOST:
  564. retval = SCTP_SCOPE_LOOPBACK;
  565. break;
  566. case IFA_LINK:
  567. retval = SCTP_SCOPE_LINK;
  568. break;
  569. case IFA_SITE:
  570. retval = SCTP_SCOPE_PRIVATE;
  571. break;
  572. default:
  573. retval = SCTP_SCOPE_GLOBAL;
  574. break;
  575. }
  576. return retval;
  577. }
  578. /* Create and initialize a new sk for the socket to be returned by accept(). */
  579. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  580. struct sctp_association *asoc,
  581. bool kern)
  582. {
  583. struct sock *newsk;
  584. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  585. struct sctp6_sock *newsctp6sk;
  586. struct ipv6_txoptions *opt;
  587. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
  588. if (!newsk)
  589. goto out;
  590. sock_init_data(NULL, newsk);
  591. sctp_copy_sock(newsk, sk, asoc);
  592. sock_reset_flag(sk, SOCK_ZAPPED);
  593. newsctp6sk = (struct sctp6_sock *)newsk;
  594. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  595. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  596. newnp = inet6_sk(newsk);
  597. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  598. newnp->ipv6_mc_list = NULL;
  599. newnp->ipv6_ac_list = NULL;
  600. newnp->ipv6_fl_list = NULL;
  601. rcu_read_lock();
  602. opt = rcu_dereference(np->opt);
  603. if (opt)
  604. opt = ipv6_dup_options(newsk, opt);
  605. RCU_INIT_POINTER(newnp->opt, opt);
  606. rcu_read_unlock();
  607. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  608. * and getpeername().
  609. */
  610. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  611. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  612. sk_refcnt_debug_inc(newsk);
  613. if (newsk->sk_prot->init(newsk)) {
  614. sk_common_release(newsk);
  615. newsk = NULL;
  616. }
  617. out:
  618. return newsk;
  619. }
  620. /* Format a sockaddr for return to user space. This makes sure the return is
  621. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  622. */
  623. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  624. {
  625. if (sp->v4mapped) {
  626. if (addr->sa.sa_family == AF_INET)
  627. sctp_v4_map_v6(addr);
  628. } else {
  629. if (addr->sa.sa_family == AF_INET6 &&
  630. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  631. sctp_v6_map_v4(addr);
  632. }
  633. if (addr->sa.sa_family == AF_INET)
  634. return sizeof(struct sockaddr_in);
  635. return sizeof(struct sockaddr_in6);
  636. }
  637. /* Where did this skb come from? */
  638. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  639. {
  640. return IP6CB(skb)->iif;
  641. }
  642. /* Was this packet marked by Explicit Congestion Notification? */
  643. static int sctp_v6_is_ce(const struct sk_buff *skb)
  644. {
  645. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  646. }
  647. /* Dump the v6 addr to the seq file. */
  648. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  649. {
  650. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  651. }
  652. static void sctp_v6_ecn_capable(struct sock *sk)
  653. {
  654. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  655. }
  656. /* Initialize a PF_INET msgname from a ulpevent. */
  657. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  658. char *msgname, int *addrlen)
  659. {
  660. union sctp_addr *addr;
  661. struct sctp_association *asoc;
  662. union sctp_addr *paddr;
  663. if (!msgname)
  664. return;
  665. addr = (union sctp_addr *)msgname;
  666. asoc = event->asoc;
  667. paddr = &asoc->peer.primary_addr;
  668. if (paddr->sa.sa_family == AF_INET) {
  669. addr->v4.sin_family = AF_INET;
  670. addr->v4.sin_port = htons(asoc->peer.port);
  671. addr->v4.sin_addr = paddr->v4.sin_addr;
  672. } else {
  673. addr->v6.sin6_family = AF_INET6;
  674. addr->v6.sin6_flowinfo = 0;
  675. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  676. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  677. else
  678. addr->v6.sin6_scope_id = 0;
  679. addr->v6.sin6_port = htons(asoc->peer.port);
  680. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  681. }
  682. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  683. }
  684. /* Initialize a msg_name from an inbound skb. */
  685. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  686. int *addr_len)
  687. {
  688. union sctp_addr *addr;
  689. struct sctphdr *sh;
  690. if (!msgname)
  691. return;
  692. addr = (union sctp_addr *)msgname;
  693. sh = sctp_hdr(skb);
  694. if (ip_hdr(skb)->version == 4) {
  695. addr->v4.sin_family = AF_INET;
  696. addr->v4.sin_port = sh->source;
  697. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  698. } else {
  699. addr->v6.sin6_family = AF_INET6;
  700. addr->v6.sin6_flowinfo = 0;
  701. addr->v6.sin6_port = sh->source;
  702. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  703. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  704. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  705. }
  706. }
  707. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  708. }
  709. /* Do we support this AF? */
  710. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  711. {
  712. switch (family) {
  713. case AF_INET6:
  714. return 1;
  715. /* v4-mapped-v6 addresses */
  716. case AF_INET:
  717. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  718. return 1;
  719. default:
  720. return 0;
  721. }
  722. }
  723. /* Address matching with wildcards allowed. This extra level
  724. * of indirection lets us choose whether a PF_INET6 should
  725. * disallow any v4 addresses if we so choose.
  726. */
  727. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  728. const union sctp_addr *addr2,
  729. struct sctp_sock *opt)
  730. {
  731. struct sctp_af *af1, *af2;
  732. struct sock *sk = sctp_opt2sk(opt);
  733. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  734. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  735. if (!af1 || !af2)
  736. return 0;
  737. /* If the socket is IPv6 only, v4 addrs will not match */
  738. if (__ipv6_only_sock(sk) && af1 != af2)
  739. return 0;
  740. /* Today, wildcard AF_INET/AF_INET6. */
  741. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  742. return 1;
  743. if (addr1->sa.sa_family != addr2->sa.sa_family)
  744. return 0;
  745. return af1->cmp_addr(addr1, addr2);
  746. }
  747. /* Verify that the provided sockaddr looks bindable. Common verification,
  748. * has already been taken care of.
  749. */
  750. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  751. {
  752. struct sctp_af *af;
  753. /* ASSERT: address family has already been verified. */
  754. if (addr->sa.sa_family != AF_INET6)
  755. af = sctp_get_af_specific(addr->sa.sa_family);
  756. else {
  757. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  758. struct net_device *dev;
  759. if (type & IPV6_ADDR_LINKLOCAL) {
  760. struct net *net;
  761. if (!addr->v6.sin6_scope_id)
  762. return 0;
  763. net = sock_net(&opt->inet.sk);
  764. rcu_read_lock();
  765. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  766. if (!dev ||
  767. !ipv6_chk_addr(net, &addr->v6.sin6_addr, dev, 0)) {
  768. rcu_read_unlock();
  769. return 0;
  770. }
  771. rcu_read_unlock();
  772. }
  773. af = opt->pf->af;
  774. }
  775. return af->available(addr, opt);
  776. }
  777. /* Verify that the provided sockaddr looks sendable. Common verification,
  778. * has already been taken care of.
  779. */
  780. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  781. {
  782. struct sctp_af *af = NULL;
  783. /* ASSERT: address family has already been verified. */
  784. if (addr->sa.sa_family != AF_INET6)
  785. af = sctp_get_af_specific(addr->sa.sa_family);
  786. else {
  787. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  788. struct net_device *dev;
  789. if (type & IPV6_ADDR_LINKLOCAL) {
  790. if (!addr->v6.sin6_scope_id)
  791. return 0;
  792. rcu_read_lock();
  793. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  794. addr->v6.sin6_scope_id);
  795. rcu_read_unlock();
  796. if (!dev)
  797. return 0;
  798. }
  799. af = opt->pf->af;
  800. }
  801. return af != NULL;
  802. }
  803. /* Fill in Supported Address Type information for INIT and INIT-ACK
  804. * chunks. Note: In the future, we may want to look at sock options
  805. * to determine whether a PF_INET6 socket really wants to have IPV4
  806. * addresses.
  807. * Returns number of addresses supported.
  808. */
  809. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  810. __be16 *types)
  811. {
  812. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  813. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  814. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  815. return 2;
  816. }
  817. return 1;
  818. }
  819. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  820. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  821. int *uaddr_len, int peer)
  822. {
  823. int rc;
  824. rc = inet6_getname(sock, uaddr, uaddr_len, peer);
  825. if (rc != 0)
  826. return rc;
  827. *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  828. (union sctp_addr *)uaddr);
  829. return rc;
  830. }
  831. static const struct proto_ops inet6_seqpacket_ops = {
  832. .family = PF_INET6,
  833. .owner = THIS_MODULE,
  834. .release = inet6_release,
  835. .bind = inet6_bind,
  836. .connect = inet_dgram_connect,
  837. .socketpair = sock_no_socketpair,
  838. .accept = inet_accept,
  839. .getname = sctp_getname,
  840. .poll = sctp_poll,
  841. .ioctl = inet6_ioctl,
  842. .listen = sctp_inet_listen,
  843. .shutdown = inet_shutdown,
  844. .setsockopt = sock_common_setsockopt,
  845. .getsockopt = sock_common_getsockopt,
  846. .sendmsg = inet_sendmsg,
  847. .recvmsg = inet_recvmsg,
  848. .mmap = sock_no_mmap,
  849. #ifdef CONFIG_COMPAT
  850. .compat_setsockopt = compat_sock_common_setsockopt,
  851. .compat_getsockopt = compat_sock_common_getsockopt,
  852. #endif
  853. };
  854. static struct inet_protosw sctpv6_seqpacket_protosw = {
  855. .type = SOCK_SEQPACKET,
  856. .protocol = IPPROTO_SCTP,
  857. .prot = &sctpv6_prot,
  858. .ops = &inet6_seqpacket_ops,
  859. .flags = SCTP_PROTOSW_FLAG
  860. };
  861. static struct inet_protosw sctpv6_stream_protosw = {
  862. .type = SOCK_STREAM,
  863. .protocol = IPPROTO_SCTP,
  864. .prot = &sctpv6_prot,
  865. .ops = &inet6_seqpacket_ops,
  866. .flags = SCTP_PROTOSW_FLAG,
  867. };
  868. static int sctp6_rcv(struct sk_buff *skb)
  869. {
  870. return sctp_rcv(skb) ? -1 : 0;
  871. }
  872. static const struct inet6_protocol sctpv6_protocol = {
  873. .handler = sctp6_rcv,
  874. .err_handler = sctp_v6_err,
  875. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  876. };
  877. static struct sctp_af sctp_af_inet6 = {
  878. .sa_family = AF_INET6,
  879. .sctp_xmit = sctp_v6_xmit,
  880. .setsockopt = ipv6_setsockopt,
  881. .getsockopt = ipv6_getsockopt,
  882. .get_dst = sctp_v6_get_dst,
  883. .get_saddr = sctp_v6_get_saddr,
  884. .copy_addrlist = sctp_v6_copy_addrlist,
  885. .from_skb = sctp_v6_from_skb,
  886. .from_sk = sctp_v6_from_sk,
  887. .from_addr_param = sctp_v6_from_addr_param,
  888. .to_addr_param = sctp_v6_to_addr_param,
  889. .cmp_addr = sctp_v6_cmp_addr,
  890. .scope = sctp_v6_scope,
  891. .addr_valid = sctp_v6_addr_valid,
  892. .inaddr_any = sctp_v6_inaddr_any,
  893. .is_any = sctp_v6_is_any,
  894. .available = sctp_v6_available,
  895. .skb_iif = sctp_v6_skb_iif,
  896. .is_ce = sctp_v6_is_ce,
  897. .seq_dump_addr = sctp_v6_seq_dump_addr,
  898. .ecn_capable = sctp_v6_ecn_capable,
  899. .net_header_len = sizeof(struct ipv6hdr),
  900. .sockaddr_len = sizeof(struct sockaddr_in6),
  901. #ifdef CONFIG_COMPAT
  902. .compat_setsockopt = compat_ipv6_setsockopt,
  903. .compat_getsockopt = compat_ipv6_getsockopt,
  904. #endif
  905. };
  906. static struct sctp_pf sctp_pf_inet6 = {
  907. .event_msgname = sctp_inet6_event_msgname,
  908. .skb_msgname = sctp_inet6_skb_msgname,
  909. .af_supported = sctp_inet6_af_supported,
  910. .cmp_addr = sctp_inet6_cmp_addr,
  911. .bind_verify = sctp_inet6_bind_verify,
  912. .send_verify = sctp_inet6_send_verify,
  913. .supported_addrs = sctp_inet6_supported_addrs,
  914. .create_accept_sk = sctp_v6_create_accept_sk,
  915. .addr_to_user = sctp_v6_addr_to_user,
  916. .to_sk_saddr = sctp_v6_to_sk_saddr,
  917. .to_sk_daddr = sctp_v6_to_sk_daddr,
  918. .af = &sctp_af_inet6,
  919. };
  920. /* Initialize IPv6 support and register with socket layer. */
  921. void sctp_v6_pf_init(void)
  922. {
  923. /* Register the SCTP specific PF_INET6 functions. */
  924. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  925. /* Register the SCTP specific AF_INET6 functions. */
  926. sctp_register_af(&sctp_af_inet6);
  927. }
  928. void sctp_v6_pf_exit(void)
  929. {
  930. list_del(&sctp_af_inet6.list);
  931. }
  932. /* Initialize IPv6 support and register with socket layer. */
  933. int sctp_v6_protosw_init(void)
  934. {
  935. int rc;
  936. rc = proto_register(&sctpv6_prot, 1);
  937. if (rc)
  938. return rc;
  939. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  940. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  941. inet6_register_protosw(&sctpv6_stream_protosw);
  942. return 0;
  943. }
  944. void sctp_v6_protosw_exit(void)
  945. {
  946. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  947. inet6_unregister_protosw(&sctpv6_stream_protosw);
  948. proto_unregister(&sctpv6_prot);
  949. }
  950. /* Register with inet6 layer. */
  951. int sctp_v6_add_protocol(void)
  952. {
  953. /* Register notifier for inet6 address additions/deletions. */
  954. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  955. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  956. return -EAGAIN;
  957. return 0;
  958. }
  959. /* Unregister with inet6 layer. */
  960. void sctp_v6_del_protocol(void)
  961. {
  962. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  963. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  964. }