ipv6.c 28 KB

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