if_gif.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the project nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
  33. */
  34. #include <sys/cdefs.h>
  35. #include "opt_inet.h"
  36. #include "opt_inet6.h"
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/kernel.h>
  40. #include <sys/lock.h>
  41. #include <sys/malloc.h>
  42. #include <sys/mbuf.h>
  43. #include <sys/module.h>
  44. #include <sys/rmlock.h>
  45. #include <sys/socket.h>
  46. #include <sys/sockio.h>
  47. #include <sys/sx.h>
  48. #include <sys/errno.h>
  49. #include <sys/time.h>
  50. #include <sys/sysctl.h>
  51. #include <sys/syslog.h>
  52. #include <sys/priv.h>
  53. #include <sys/proc.h>
  54. #include <sys/conf.h>
  55. #include <machine/cpu.h>
  56. #include <net/if.h>
  57. #include <net/if_var.h>
  58. #include <net/if_private.h>
  59. #include <net/if_clone.h>
  60. #include <net/if_types.h>
  61. #include <net/netisr.h>
  62. #include <net/route.h>
  63. #include <net/bpf.h>
  64. #include <net/vnet.h>
  65. #include <netinet/in.h>
  66. #include <netinet/in_systm.h>
  67. #include <netinet/ip.h>
  68. #include <netinet/ip_ecn.h>
  69. #ifdef INET
  70. #include <netinet/in_var.h>
  71. #include <netinet/ip_var.h>
  72. #endif /* INET */
  73. #ifdef INET6
  74. #ifndef INET
  75. #include <netinet/in.h>
  76. #endif
  77. #include <netinet6/in6_var.h>
  78. #include <netinet/ip6.h>
  79. #include <netinet6/ip6_ecn.h>
  80. #include <netinet6/ip6_var.h>
  81. #endif /* INET6 */
  82. #include <netinet/ip_encap.h>
  83. #include <net/ethernet.h>
  84. #include <net/if_bridgevar.h>
  85. #include <net/if_gif.h>
  86. #include <security/mac/mac_framework.h>
  87. static const char gifname[] = "gif";
  88. MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
  89. static struct sx gif_ioctl_sx;
  90. SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
  91. void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
  92. void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
  93. void (*ng_gif_attach_p)(struct ifnet *ifp);
  94. void (*ng_gif_detach_p)(struct ifnet *ifp);
  95. #ifdef VIMAGE
  96. static void gif_reassign(struct ifnet *, struct vnet *, char *);
  97. #endif
  98. static void gif_delete_tunnel(struct gif_softc *);
  99. static int gif_ioctl(struct ifnet *, u_long, caddr_t);
  100. static int gif_transmit(struct ifnet *, struct mbuf *);
  101. static void gif_qflush(struct ifnet *);
  102. static int gif_clone_create(struct if_clone *, int, caddr_t);
  103. static void gif_clone_destroy(struct ifnet *);
  104. VNET_DEFINE_STATIC(struct if_clone *, gif_cloner);
  105. #define V_gif_cloner VNET(gif_cloner)
  106. SYSCTL_DECL(_net_link);
  107. static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  108. "Generic Tunnel Interface");
  109. #ifndef MAX_GIF_NEST
  110. /*
  111. * This macro controls the default upper limitation on nesting of gif tunnels.
  112. * Since, setting a large value to this macro with a careless configuration
  113. * may introduce system crash, we don't allow any nestings by default.
  114. * If you need to configure nested gif tunnels, you can define this macro
  115. * in your kernel configuration file. However, if you do so, please be
  116. * careful to configure the tunnels so that it won't make a loop.
  117. */
  118. #define MAX_GIF_NEST 1
  119. #endif
  120. VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST;
  121. #define V_max_gif_nesting VNET(max_gif_nesting)
  122. SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
  123. &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
  124. static int
  125. gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  126. {
  127. struct gif_softc *sc;
  128. sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
  129. sc->gif_fibnum = curthread->td_proc->p_fibnum;
  130. GIF2IFP(sc) = if_alloc(IFT_GIF);
  131. GIF2IFP(sc)->if_softc = sc;
  132. if_initname(GIF2IFP(sc), gifname, unit);
  133. GIF2IFP(sc)->if_addrlen = 0;
  134. GIF2IFP(sc)->if_mtu = GIF_MTU;
  135. GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  136. GIF2IFP(sc)->if_ioctl = gif_ioctl;
  137. GIF2IFP(sc)->if_transmit = gif_transmit;
  138. GIF2IFP(sc)->if_qflush = gif_qflush;
  139. GIF2IFP(sc)->if_output = gif_output;
  140. #ifdef VIMAGE
  141. GIF2IFP(sc)->if_reassign = gif_reassign;
  142. #endif
  143. GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
  144. GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
  145. if_attach(GIF2IFP(sc));
  146. bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
  147. if (ng_gif_attach_p != NULL)
  148. (*ng_gif_attach_p)(GIF2IFP(sc));
  149. return (0);
  150. }
  151. #ifdef VIMAGE
  152. static void
  153. gif_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
  154. char *unused __unused)
  155. {
  156. struct gif_softc *sc;
  157. sx_xlock(&gif_ioctl_sx);
  158. sc = ifp->if_softc;
  159. if (sc != NULL)
  160. gif_delete_tunnel(sc);
  161. sx_xunlock(&gif_ioctl_sx);
  162. }
  163. #endif /* VIMAGE */
  164. static void
  165. gif_clone_destroy(struct ifnet *ifp)
  166. {
  167. struct gif_softc *sc;
  168. sx_xlock(&gif_ioctl_sx);
  169. sc = ifp->if_softc;
  170. gif_delete_tunnel(sc);
  171. if (ng_gif_detach_p != NULL)
  172. (*ng_gif_detach_p)(ifp);
  173. bpfdetach(ifp);
  174. if_detach(ifp);
  175. ifp->if_softc = NULL;
  176. sx_xunlock(&gif_ioctl_sx);
  177. GIF_WAIT();
  178. if_free(ifp);
  179. free(sc, M_GIF);
  180. }
  181. static void
  182. vnet_gif_init(const void *unused __unused)
  183. {
  184. V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
  185. gif_clone_destroy, 0);
  186. #ifdef INET
  187. in_gif_init();
  188. #endif
  189. #ifdef INET6
  190. in6_gif_init();
  191. #endif
  192. }
  193. VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
  194. vnet_gif_init, NULL);
  195. static void
  196. vnet_gif_uninit(const void *unused __unused)
  197. {
  198. if_clone_detach(V_gif_cloner);
  199. #ifdef INET
  200. in_gif_uninit();
  201. #endif
  202. #ifdef INET6
  203. in6_gif_uninit();
  204. #endif
  205. }
  206. VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
  207. vnet_gif_uninit, NULL);
  208. static int
  209. gifmodevent(module_t mod, int type, void *data)
  210. {
  211. switch (type) {
  212. case MOD_LOAD:
  213. case MOD_UNLOAD:
  214. break;
  215. default:
  216. return (EOPNOTSUPP);
  217. }
  218. return (0);
  219. }
  220. static moduledata_t gif_mod = {
  221. "if_gif",
  222. gifmodevent,
  223. 0
  224. };
  225. DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  226. MODULE_VERSION(if_gif, 1);
  227. struct gif_list *
  228. gif_hashinit(void)
  229. {
  230. struct gif_list *hash;
  231. int i;
  232. hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE,
  233. M_GIF, M_WAITOK);
  234. for (i = 0; i < GIF_HASH_SIZE; i++)
  235. CK_LIST_INIT(&hash[i]);
  236. return (hash);
  237. }
  238. void
  239. gif_hashdestroy(struct gif_list *hash)
  240. {
  241. free(hash, M_GIF);
  242. }
  243. #define MTAG_GIF 1080679712
  244. static int
  245. gif_transmit(struct ifnet *ifp, struct mbuf *m)
  246. {
  247. struct gif_softc *sc;
  248. struct etherip_header *eth;
  249. #ifdef INET
  250. struct ip *ip;
  251. #endif
  252. #ifdef INET6
  253. struct ip6_hdr *ip6;
  254. uint32_t t;
  255. #endif
  256. uint32_t af;
  257. uint8_t proto, ecn;
  258. int error;
  259. NET_EPOCH_ASSERT();
  260. #ifdef MAC
  261. error = mac_ifnet_check_transmit(ifp, m);
  262. if (error) {
  263. m_freem(m);
  264. goto err;
  265. }
  266. #endif
  267. error = ENETDOWN;
  268. sc = ifp->if_softc;
  269. if ((ifp->if_flags & IFF_MONITOR) != 0 ||
  270. (ifp->if_flags & IFF_UP) == 0 ||
  271. (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
  272. sc->gif_family == 0 ||
  273. (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF,
  274. V_max_gif_nesting)) != 0) {
  275. m_freem(m);
  276. goto err;
  277. }
  278. /* Now pull back the af that we stashed in the csum_data. */
  279. if (ifp->if_bridge)
  280. af = AF_LINK;
  281. else
  282. af = m->m_pkthdr.csum_data;
  283. m->m_flags &= ~(M_BCAST|M_MCAST);
  284. M_SETFIB(m, sc->gif_fibnum);
  285. BPF_MTAP2(ifp, &af, sizeof(af), m);
  286. if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
  287. if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
  288. /* inner AF-specific encapsulation */
  289. ecn = 0;
  290. switch (af) {
  291. #ifdef INET
  292. case AF_INET:
  293. proto = IPPROTO_IPV4;
  294. if (m->m_len < sizeof(struct ip))
  295. m = m_pullup(m, sizeof(struct ip));
  296. if (m == NULL) {
  297. error = ENOBUFS;
  298. goto err;
  299. }
  300. ip = mtod(m, struct ip *);
  301. ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
  302. ECN_NOCARE, &ecn, &ip->ip_tos);
  303. break;
  304. #endif
  305. #ifdef INET6
  306. case AF_INET6:
  307. proto = IPPROTO_IPV6;
  308. if (m->m_len < sizeof(struct ip6_hdr))
  309. m = m_pullup(m, sizeof(struct ip6_hdr));
  310. if (m == NULL) {
  311. error = ENOBUFS;
  312. goto err;
  313. }
  314. t = 0;
  315. ip6 = mtod(m, struct ip6_hdr *);
  316. ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
  317. ECN_NOCARE, &t, &ip6->ip6_flow);
  318. ecn = (ntohl(t) >> 20) & 0xff;
  319. break;
  320. #endif
  321. case AF_LINK:
  322. proto = IPPROTO_ETHERIP;
  323. M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
  324. if (m == NULL) {
  325. error = ENOBUFS;
  326. goto err;
  327. }
  328. eth = mtod(m, struct etherip_header *);
  329. eth->eip_resvh = 0;
  330. eth->eip_ver = ETHERIP_VERSION;
  331. eth->eip_resvl = 0;
  332. break;
  333. default:
  334. error = EAFNOSUPPORT;
  335. m_freem(m);
  336. goto err;
  337. }
  338. /* XXX should we check if our outer source is legal? */
  339. /* dispatch to output logic based on outer AF */
  340. switch (sc->gif_family) {
  341. #ifdef INET
  342. case AF_INET:
  343. error = in_gif_output(ifp, m, proto, ecn);
  344. break;
  345. #endif
  346. #ifdef INET6
  347. case AF_INET6:
  348. error = in6_gif_output(ifp, m, proto, ecn);
  349. break;
  350. #endif
  351. default:
  352. m_freem(m);
  353. }
  354. err:
  355. if (error)
  356. if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  357. return (error);
  358. }
  359. static void
  360. gif_qflush(struct ifnet *ifp __unused)
  361. {
  362. }
  363. int
  364. gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
  365. struct route *ro)
  366. {
  367. uint32_t af;
  368. KASSERT(ifp->if_bridge == NULL,
  369. ("%s: unexpectedly called with bridge attached", __func__));
  370. /* BPF writes need to be handled specially. */
  371. if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
  372. memcpy(&af, dst->sa_data, sizeof(af));
  373. else
  374. af = RO_GET_FAMILY(ro, dst);
  375. /*
  376. * Now save the af in the inbound pkt csum data, this is a cheat since
  377. * we are using the inbound csum_data field to carry the af over to
  378. * the gif_transmit() routine, avoiding using yet another mtag.
  379. */
  380. m->m_pkthdr.csum_data = af;
  381. return (ifp->if_transmit(ifp, m));
  382. }
  383. void
  384. gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
  385. {
  386. struct etherip_header *eip;
  387. #ifdef INET
  388. struct ip *ip;
  389. #endif
  390. #ifdef INET6
  391. struct ip6_hdr *ip6;
  392. uint32_t t;
  393. #endif
  394. struct ether_header *eh;
  395. struct ifnet *oldifp;
  396. int isr, n, af;
  397. NET_EPOCH_ASSERT();
  398. if (ifp == NULL) {
  399. /* just in case */
  400. m_freem(m);
  401. return;
  402. }
  403. m->m_pkthdr.rcvif = ifp;
  404. m_clrprotoflags(m);
  405. switch (proto) {
  406. #ifdef INET
  407. case IPPROTO_IPV4:
  408. af = AF_INET;
  409. if (m->m_len < sizeof(struct ip))
  410. m = m_pullup(m, sizeof(struct ip));
  411. if (m == NULL)
  412. goto drop;
  413. ip = mtod(m, struct ip *);
  414. if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
  415. ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
  416. m_freem(m);
  417. goto drop;
  418. }
  419. break;
  420. #endif
  421. #ifdef INET6
  422. case IPPROTO_IPV6:
  423. af = AF_INET6;
  424. if (m->m_len < sizeof(struct ip6_hdr))
  425. m = m_pullup(m, sizeof(struct ip6_hdr));
  426. if (m == NULL)
  427. goto drop;
  428. t = htonl((uint32_t)ecn << 20);
  429. ip6 = mtod(m, struct ip6_hdr *);
  430. if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
  431. ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
  432. m_freem(m);
  433. goto drop;
  434. }
  435. break;
  436. #endif
  437. case IPPROTO_ETHERIP:
  438. af = AF_LINK;
  439. break;
  440. default:
  441. m_freem(m);
  442. goto drop;
  443. }
  444. #ifdef MAC
  445. mac_ifnet_create_mbuf(ifp, m);
  446. #endif
  447. if (bpf_peers_present(ifp->if_bpf)) {
  448. uint32_t af1 = af;
  449. bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
  450. }
  451. if ((ifp->if_flags & IFF_MONITOR) != 0) {
  452. if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  453. if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  454. m_freem(m);
  455. return;
  456. }
  457. if (ng_gif_input_p != NULL) {
  458. (*ng_gif_input_p)(ifp, &m, af);
  459. if (m == NULL)
  460. goto drop;
  461. }
  462. /*
  463. * Put the packet to the network layer input queue according to the
  464. * specified address family.
  465. * Note: older versions of gif_input directly called network layer
  466. * input functions, e.g. ip6_input, here. We changed the policy to
  467. * prevent too many recursive calls of such input functions, which
  468. * might cause kernel panic. But the change may introduce another
  469. * problem; if the input queue is full, packets are discarded.
  470. * The kernel stack overflow really happened, and we believed
  471. * queue-full rarely occurs, so we changed the policy.
  472. */
  473. switch (af) {
  474. #ifdef INET
  475. case AF_INET:
  476. isr = NETISR_IP;
  477. break;
  478. #endif
  479. #ifdef INET6
  480. case AF_INET6:
  481. isr = NETISR_IPV6;
  482. break;
  483. #endif
  484. case AF_LINK:
  485. n = sizeof(struct etherip_header) +
  486. sizeof(struct ether_header);
  487. if (n > m->m_len)
  488. m = m_pullup(m, n);
  489. if (m == NULL)
  490. goto drop;
  491. eip = mtod(m, struct etherip_header *);
  492. if (eip->eip_ver != ETHERIP_VERSION) {
  493. /* discard unknown versions */
  494. m_freem(m);
  495. goto drop;
  496. }
  497. m_adj_decap(m, sizeof(struct etherip_header));
  498. m->m_flags &= ~(M_BCAST|M_MCAST);
  499. m->m_pkthdr.rcvif = ifp;
  500. if (ifp->if_bridge) {
  501. oldifp = ifp;
  502. eh = mtod(m, struct ether_header *);
  503. if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  504. if (ETHER_IS_BROADCAST(eh->ether_dhost))
  505. m->m_flags |= M_BCAST;
  506. else
  507. m->m_flags |= M_MCAST;
  508. if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
  509. }
  510. BRIDGE_INPUT(ifp, m);
  511. if (m != NULL && ifp != oldifp) {
  512. /*
  513. * The bridge gave us back itself or one of the
  514. * members for which the frame is addressed.
  515. */
  516. ether_demux(ifp, m);
  517. return;
  518. }
  519. }
  520. if (m != NULL)
  521. m_freem(m);
  522. return;
  523. default:
  524. if (ng_gif_input_orphan_p != NULL)
  525. (*ng_gif_input_orphan_p)(ifp, m, af);
  526. else
  527. m_freem(m);
  528. return;
  529. }
  530. if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  531. if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  532. M_SETFIB(m, ifp->if_fib);
  533. netisr_dispatch(isr, m);
  534. return;
  535. drop:
  536. if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  537. }
  538. static int
  539. gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  540. {
  541. struct ifreq *ifr = (struct ifreq*)data;
  542. struct gif_softc *sc;
  543. u_int options;
  544. int error;
  545. switch (cmd) {
  546. case SIOCSIFADDR:
  547. ifp->if_flags |= IFF_UP;
  548. case SIOCADDMULTI:
  549. case SIOCDELMULTI:
  550. case SIOCGIFMTU:
  551. case SIOCSIFFLAGS:
  552. return (0);
  553. case SIOCSIFMTU:
  554. if (ifr->ifr_mtu < GIF_MTU_MIN ||
  555. ifr->ifr_mtu > GIF_MTU_MAX)
  556. return (EINVAL);
  557. else
  558. ifp->if_mtu = ifr->ifr_mtu;
  559. return (0);
  560. }
  561. sx_xlock(&gif_ioctl_sx);
  562. sc = ifp->if_softc;
  563. if (sc == NULL) {
  564. error = ENXIO;
  565. goto bad;
  566. }
  567. error = 0;
  568. switch (cmd) {
  569. case SIOCDIFPHYADDR:
  570. if (sc->gif_family == 0)
  571. break;
  572. gif_delete_tunnel(sc);
  573. break;
  574. #ifdef INET
  575. case SIOCSIFPHYADDR:
  576. case SIOCGIFPSRCADDR:
  577. case SIOCGIFPDSTADDR:
  578. error = in_gif_ioctl(sc, cmd, data);
  579. break;
  580. #endif
  581. #ifdef INET6
  582. case SIOCSIFPHYADDR_IN6:
  583. case SIOCGIFPSRCADDR_IN6:
  584. case SIOCGIFPDSTADDR_IN6:
  585. error = in6_gif_ioctl(sc, cmd, data);
  586. break;
  587. #endif
  588. case SIOCGTUNFIB:
  589. ifr->ifr_fib = sc->gif_fibnum;
  590. break;
  591. case SIOCSTUNFIB:
  592. if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
  593. break;
  594. if (ifr->ifr_fib >= rt_numfibs)
  595. error = EINVAL;
  596. else
  597. sc->gif_fibnum = ifr->ifr_fib;
  598. break;
  599. case GIFGOPTS:
  600. options = sc->gif_options;
  601. error = copyout(&options, ifr_data_get_ptr(ifr),
  602. sizeof(options));
  603. break;
  604. case GIFSOPTS:
  605. if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
  606. break;
  607. error = copyin(ifr_data_get_ptr(ifr), &options,
  608. sizeof(options));
  609. if (error)
  610. break;
  611. if (options & ~GIF_OPTMASK) {
  612. error = EINVAL;
  613. break;
  614. }
  615. if (sc->gif_options != options) {
  616. switch (sc->gif_family) {
  617. #ifdef INET
  618. case AF_INET:
  619. error = in_gif_setopts(sc, options);
  620. break;
  621. #endif
  622. #ifdef INET6
  623. case AF_INET6:
  624. error = in6_gif_setopts(sc, options);
  625. break;
  626. #endif
  627. default:
  628. /* No need to invoke AF-handler */
  629. sc->gif_options = options;
  630. }
  631. }
  632. break;
  633. default:
  634. error = EINVAL;
  635. break;
  636. }
  637. if (error == 0 && sc->gif_family != 0) {
  638. if (
  639. #ifdef INET
  640. cmd == SIOCSIFPHYADDR ||
  641. #endif
  642. #ifdef INET6
  643. cmd == SIOCSIFPHYADDR_IN6 ||
  644. #endif
  645. 0) {
  646. if_link_state_change(ifp, LINK_STATE_UP);
  647. }
  648. }
  649. bad:
  650. sx_xunlock(&gif_ioctl_sx);
  651. return (error);
  652. }
  653. static void
  654. gif_delete_tunnel(struct gif_softc *sc)
  655. {
  656. sx_assert(&gif_ioctl_sx, SA_XLOCKED);
  657. if (sc->gif_family != 0) {
  658. CK_LIST_REMOVE(sc, srchash);
  659. CK_LIST_REMOVE(sc, chain);
  660. /* Wait until it become safe to free gif_hdr */
  661. GIF_WAIT();
  662. free(sc->gif_hdr, M_GIF);
  663. }
  664. sc->gif_family = 0;
  665. GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
  666. if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN);
  667. }