ipv6.c 27 KB

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