ipv6.c 29 KB

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