ipv6.c 29 KB

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