ipv6.c 28 KB

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