ipv6.c 29 KB

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