ipv6.c 28 KB

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