ipv6.c 28 KB

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