ipv6.c 29 KB

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