ipv6.c 28 KB

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