ipv6.c 29 KB

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