ipv6.c 28 KB

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