ipv6.c 28 KB

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