ipv6.c 28 KB

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