ipv6.c 28 KB

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