ipv6.c 27 KB

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