ipv6.c 27 KB

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