Files

171 lines
4.6 KiB
Go
Raw Permalink Normal View History

2020-02-20 15:19:40 -08:00
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package inet
2023-07-24 15:42:22 -07:00
import (
2024-03-04 12:18:19 -08:00
goContext "context"
2023-07-24 15:42:22 -07:00
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/nsfs"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
)
2020-02-20 15:19:40 -08:00
// Namespace represents a network namespace. See network_namespaces(7).
//
// +stateify savable
type Namespace struct {
2023-07-24 15:42:22 -07:00
inode *nsfs.Inode
2022-06-13 15:02:23 -07:00
2020-02-20 15:19:40 -08:00
// stack is the network stack implementation of this network namespace.
stack Stack
2020-02-20 15:19:40 -08:00
// creator allows kernel to create new network stack for network namespaces.
// If nil, no networking will function if network is namespaced.
2020-03-05 17:39:11 -08:00
//
// At afterLoad(), creator will be used to create network stack. Stateify
// needs to wait for this field to be loaded before calling afterLoad().
creator NetworkStackCreator `state:"wait"`
2020-02-20 15:19:40 -08:00
// isRoot indicates whether this is the root network namespace.
isRoot bool
2023-07-24 15:42:22 -07:00
userNS *auth.UserNamespace
// abstractSockets tracks abstract sockets that are in use.
abstractSockets AbstractSocketNamespace
2020-02-20 15:19:40 -08:00
}
// NewRootNamespace creates the root network namespace, with creator
// allowing new network namespaces to be created. If creator is nil, no
// networking will function if the network is namespaced.
2023-07-24 15:42:22 -07:00
func NewRootNamespace(stack Stack, creator NetworkStackCreator, userNS *auth.UserNamespace) *Namespace {
2022-06-13 15:02:23 -07:00
n := &Namespace{
2020-02-20 15:19:40 -08:00
stack: stack,
creator: creator,
isRoot: true,
2023-07-24 15:42:22 -07:00
userNS: userNS,
2020-02-20 15:19:40 -08:00
}
n.abstractSockets.init()
2022-06-13 15:02:23 -07:00
return n
2020-02-20 15:19:40 -08:00
}
2023-07-24 15:42:22 -07:00
// UserNamespace returns the user namespace associated with this namespace.
func (n *Namespace) UserNamespace() *auth.UserNamespace {
return n.userNS
}
// SetInode sets the nsfs `inode` to the namespace.
func (n *Namespace) SetInode(inode *nsfs.Inode) {
n.inode = inode
}
// GetInode returns the nsfs inode associated with this namespace.
func (n *Namespace) GetInode() *nsfs.Inode {
return n.inode
}
2020-02-20 15:19:40 -08:00
// NewNamespace creates a new network namespace from the root.
2023-07-24 15:42:22 -07:00
func NewNamespace(root *Namespace, userNS *auth.UserNamespace) *Namespace {
2020-02-20 15:19:40 -08:00
n := &Namespace{
creator: root.creator,
2023-07-24 15:42:22 -07:00
userNS: userNS,
2020-02-20 15:19:40 -08:00
}
n.init()
return n
}
2023-07-24 15:42:22 -07:00
// Destroy implements nsfs.Namespace.Destroy.
func (n *Namespace) Destroy(ctx context.Context) {
if s := n.Stack(); s != nil {
s.Destroy()
}
}
// Type implements nsfs.Namespace.Type.
func (n *Namespace) Type() string {
return "net"
}
// IncRef increments the Namespace's refcount.
func (n *Namespace) IncRef() {
n.inode.IncRef()
}
2022-06-13 15:02:23 -07:00
// DecRef decrements the Namespace's refcount.
2023-07-24 15:42:22 -07:00
func (n *Namespace) DecRef(ctx context.Context) {
n.inode.DecRef(ctx)
2022-06-13 15:02:23 -07:00
}
2020-02-20 15:19:40 -08:00
// Stack returns the network stack of n. Stack may return nil if no network
// stack is configured.
func (n *Namespace) Stack() Stack {
return n.stack
}
// IsRoot returns whether n is the root network namespace.
func (n *Namespace) IsRoot() bool {
return n.isRoot
}
// RestoreRootStack restores the root network namespace with stack. This should
// only be called when restoring kernel.
func (n *Namespace) RestoreRootStack(stack Stack) {
if !n.isRoot {
panic("RestoreRootStack can only be called on root network namespace")
}
if n.stack != nil {
panic("RestoreRootStack called after a stack has already been set")
}
n.stack = stack
}
2024-02-02 16:53:18 -08:00
// ResetStack resets the stack in the network namespace to nil. This should
// only be called when restoring kernel.
func (n *Namespace) ResetStack() {
n.stack = nil
}
2020-02-20 15:19:40 -08:00
func (n *Namespace) init() {
// Root network namespace will have stack assigned later.
if n.isRoot {
return
}
if n.creator != nil {
var err error
n.stack, err = n.creator.CreateStack()
if err != nil {
panic(err)
}
}
n.abstractSockets.init()
2020-02-20 15:19:40 -08:00
}
// afterLoad is invoked by stateify.
2024-03-04 12:18:19 -08:00
func (n *Namespace) afterLoad(goContext.Context) {
2020-02-20 15:19:40 -08:00
n.init()
}
// AbstractSockets returns AbstractSocketNamespace.
func (n *Namespace) AbstractSockets() *AbstractSocketNamespace {
return &n.abstractSockets
}
2020-02-20 15:19:40 -08:00
// NetworkStackCreator allows new instances of a network stack to be created. It
// is used by the kernel to create new network namespaces when requested.
type NetworkStackCreator interface {
// CreateStack creates a new network stack for a network namespace.
CreateStack() (Stack, error)
}